interface.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/pci.h>
  17. #include <linux/mei.h>
  18. #include "mei_dev.h"
  19. #include "interface.h"
  20. /**
  21. * mei_reg_read - Reads 32bit data from the mei device
  22. *
  23. * @dev: the device structure
  24. * @offset: offset from which to read the data
  25. *
  26. * returns register value (u32)
  27. */
  28. static inline u32 mei_reg_read(const struct mei_device *dev,
  29. unsigned long offset)
  30. {
  31. return ioread32(dev->mem_addr + offset);
  32. }
  33. /**
  34. * mei_reg_write - Writes 32bit data to the mei device
  35. *
  36. * @dev: the device structure
  37. * @offset: offset from which to write the data
  38. * @value: register value to write (u32)
  39. */
  40. static inline void mei_reg_write(const struct mei_device *dev,
  41. unsigned long offset, u32 value)
  42. {
  43. iowrite32(value, dev->mem_addr + offset);
  44. }
  45. /**
  46. * mei_hcsr_read - Reads 32bit data from the host CSR
  47. *
  48. * @dev: the device structure
  49. *
  50. * returns the byte read.
  51. */
  52. u32 mei_hcsr_read(const struct mei_device *dev)
  53. {
  54. return mei_reg_read(dev, H_CSR);
  55. }
  56. u32 mei_mecbrw_read(const struct mei_device *dev)
  57. {
  58. return mei_reg_read(dev, ME_CB_RW);
  59. }
  60. /**
  61. * mei_mecsr_read - Reads 32bit data from the ME CSR
  62. *
  63. * @dev: the device structure
  64. *
  65. * returns ME_CSR_HA register value (u32)
  66. */
  67. u32 mei_mecsr_read(const struct mei_device *dev)
  68. {
  69. return mei_reg_read(dev, ME_CSR_HA);
  70. }
  71. /**
  72. * mei_set_csr_register - writes H_CSR register to the mei device,
  73. * and ignores the H_IS bit for it is write-one-to-zero.
  74. *
  75. * @dev: the device structure
  76. */
  77. void mei_hcsr_set(struct mei_device *dev)
  78. {
  79. if ((dev->host_hw_state & H_IS) == H_IS)
  80. dev->host_hw_state &= ~H_IS;
  81. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  82. dev->host_hw_state = mei_hcsr_read(dev);
  83. }
  84. /**
  85. * mei_enable_interrupts - clear and stop interrupts
  86. *
  87. * @dev: the device structure
  88. */
  89. void mei_clear_interrupts(struct mei_device *dev)
  90. {
  91. if ((dev->host_hw_state & H_IS) == H_IS)
  92. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  93. }
  94. /**
  95. * mei_enable_interrupts - enables mei device interrupts
  96. *
  97. * @dev: the device structure
  98. */
  99. void mei_enable_interrupts(struct mei_device *dev)
  100. {
  101. dev->host_hw_state |= H_IE;
  102. mei_hcsr_set(dev);
  103. }
  104. /**
  105. * mei_disable_interrupts - disables mei device interrupts
  106. *
  107. * @dev: the device structure
  108. */
  109. void mei_disable_interrupts(struct mei_device *dev)
  110. {
  111. dev->host_hw_state &= ~H_IE;
  112. mei_hcsr_set(dev);
  113. }
  114. /**
  115. * mei_interrupt_quick_handler - The ISR of the MEI device
  116. *
  117. * @irq: The irq number
  118. * @dev_id: pointer to the device structure
  119. *
  120. * returns irqreturn_t
  121. */
  122. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
  123. {
  124. struct mei_device *dev = (struct mei_device *) dev_id;
  125. u32 csr_reg = mei_hcsr_read(dev);
  126. if ((csr_reg & H_IS) != H_IS)
  127. return IRQ_NONE;
  128. /* clear H_IS bit in H_CSR */
  129. mei_reg_write(dev, H_CSR, csr_reg);
  130. return IRQ_WAKE_THREAD;
  131. }
  132. /**
  133. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  134. *
  135. * @device: the device structure
  136. *
  137. * returns number of filled slots
  138. */
  139. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  140. {
  141. char read_ptr, write_ptr;
  142. dev->host_hw_state = mei_hcsr_read(dev);
  143. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  144. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  145. return (unsigned char) (write_ptr - read_ptr);
  146. }
  147. /**
  148. * mei_hbuf_is_empty - checks if host buffer is empty.
  149. *
  150. * @dev: the device structure
  151. *
  152. * returns true if empty, false - otherwise.
  153. */
  154. bool mei_hbuf_is_empty(struct mei_device *dev)
  155. {
  156. return mei_hbuf_filled_slots(dev) == 0;
  157. }
  158. /**
  159. * mei_hbuf_empty_slots - counts write empty slots.
  160. *
  161. * @dev: the device structure
  162. *
  163. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  164. */
  165. int mei_hbuf_empty_slots(struct mei_device *dev)
  166. {
  167. unsigned char filled_slots, empty_slots;
  168. filled_slots = mei_hbuf_filled_slots(dev);
  169. empty_slots = dev->hbuf_depth - filled_slots;
  170. /* check for overflow */
  171. if (filled_slots > dev->hbuf_depth)
  172. return -EOVERFLOW;
  173. return empty_slots;
  174. }
  175. /**
  176. * mei_write_message - writes a message to mei device.
  177. *
  178. * @dev: the device structure
  179. * @hader: mei HECI header of message
  180. * @buf: message payload will be written
  181. *
  182. * This function returns -EIO if write has failed
  183. */
  184. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  185. unsigned char *buf)
  186. {
  187. unsigned long rem, dw_cnt;
  188. unsigned long length = header->length;
  189. u32 *reg_buf = (u32 *)buf;
  190. int i;
  191. int empty_slots;
  192. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  193. empty_slots = mei_hbuf_empty_slots(dev);
  194. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  195. dw_cnt = mei_data2slots(length);
  196. if (empty_slots < 0 || dw_cnt > empty_slots)
  197. return -EIO;
  198. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  199. for (i = 0; i < length / 4; i++)
  200. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  201. rem = length & 0x3;
  202. if (rem > 0) {
  203. u32 reg = 0;
  204. memcpy(&reg, &buf[length - rem], rem);
  205. mei_reg_write(dev, H_CB_WW, reg);
  206. }
  207. dev->host_hw_state = mei_hcsr_read(dev);
  208. dev->host_hw_state |= H_IG;
  209. mei_hcsr_set(dev);
  210. dev->me_hw_state = mei_mecsr_read(dev);
  211. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  212. return -EIO;
  213. return 0;
  214. }
  215. /**
  216. * mei_count_full_read_slots - counts read full slots.
  217. *
  218. * @dev: the device structure
  219. *
  220. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  221. */
  222. int mei_count_full_read_slots(struct mei_device *dev)
  223. {
  224. char read_ptr, write_ptr;
  225. unsigned char buffer_depth, filled_slots;
  226. dev->me_hw_state = mei_mecsr_read(dev);
  227. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  228. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  229. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  230. filled_slots = (unsigned char) (write_ptr - read_ptr);
  231. /* check for overflow */
  232. if (filled_slots > buffer_depth)
  233. return -EOVERFLOW;
  234. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  235. return (int)filled_slots;
  236. }
  237. /**
  238. * mei_read_slots - reads a message from mei device.
  239. *
  240. * @dev: the device structure
  241. * @buffer: message buffer will be written
  242. * @buffer_length: message size will be read
  243. */
  244. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  245. unsigned long buffer_length)
  246. {
  247. u32 *reg_buf = (u32 *)buffer;
  248. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  249. *reg_buf++ = mei_mecbrw_read(dev);
  250. if (buffer_length > 0) {
  251. u32 reg = mei_mecbrw_read(dev);
  252. memcpy(reg_buf, &reg, buffer_length);
  253. }
  254. dev->host_hw_state |= H_IG;
  255. mei_hcsr_set(dev);
  256. }
  257. /**
  258. * mei_flow_ctrl_creds - checks flow_control credentials.
  259. *
  260. * @dev: the device structure
  261. * @cl: private data of the file object
  262. *
  263. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  264. * -ENOENT if mei_cl is not present
  265. * -EINVAL if single_recv_buf == 0
  266. */
  267. int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
  268. {
  269. int i;
  270. if (!dev->me_clients_num)
  271. return 0;
  272. if (cl->mei_flow_ctrl_creds > 0)
  273. return 1;
  274. for (i = 0; i < dev->me_clients_num; i++) {
  275. struct mei_me_client *me_cl = &dev->me_clients[i];
  276. if (me_cl->client_id == cl->me_client_id) {
  277. if (me_cl->mei_flow_ctrl_creds) {
  278. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  279. return -EINVAL;
  280. return 1;
  281. } else {
  282. return 0;
  283. }
  284. }
  285. }
  286. return -ENOENT;
  287. }
  288. /**
  289. * mei_flow_ctrl_reduce - reduces flow_control.
  290. *
  291. * @dev: the device structure
  292. * @cl: private data of the file object
  293. * @returns
  294. * 0 on success
  295. * -ENOENT when me client is not found
  296. * -EINVAL when ctrl credits are <= 0
  297. */
  298. int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
  299. {
  300. int i;
  301. if (!dev->me_clients_num)
  302. return -ENOENT;
  303. for (i = 0; i < dev->me_clients_num; i++) {
  304. struct mei_me_client *me_cl = &dev->me_clients[i];
  305. if (me_cl->client_id == cl->me_client_id) {
  306. if (me_cl->props.single_recv_buf != 0) {
  307. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  308. return -EINVAL;
  309. dev->me_clients[i].mei_flow_ctrl_creds--;
  310. } else {
  311. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  312. return -EINVAL;
  313. cl->mei_flow_ctrl_creds--;
  314. }
  315. return 0;
  316. }
  317. }
  318. return -ENOENT;
  319. }
  320. /**
  321. * mei_other_client_is_connecting - checks if other
  322. * client with the same client id is connected.
  323. *
  324. * @dev: the device structure
  325. * @cl: private data of the file object
  326. *
  327. * returns 1 if other client is connected, 0 - otherwise.
  328. */
  329. int mei_other_client_is_connecting(struct mei_device *dev,
  330. struct mei_cl *cl)
  331. {
  332. struct mei_cl *cl_pos = NULL;
  333. struct mei_cl *cl_next = NULL;
  334. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  335. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  336. (cl_pos != cl) &&
  337. cl->me_client_id == cl_pos->me_client_id)
  338. return 1;
  339. }
  340. return 0;
  341. }