interface.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "mei_dev.h"
  18. #include <linux/mei.h>
  19. #include "interface.h"
  20. /**
  21. * mei_set_csr_register - writes H_CSR register to the mei device,
  22. * and ignores the H_IS bit for it is write-one-to-zero.
  23. *
  24. * @dev: the device structure
  25. */
  26. void mei_hcsr_set(struct mei_device *dev)
  27. {
  28. if ((dev->host_hw_state & H_IS) == H_IS)
  29. dev->host_hw_state &= ~H_IS;
  30. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  31. dev->host_hw_state = mei_hcsr_read(dev);
  32. }
  33. /**
  34. * mei_csr_enable_interrupts - enables mei device interrupts
  35. *
  36. * @dev: the device structure
  37. */
  38. void mei_enable_interrupts(struct mei_device *dev)
  39. {
  40. dev->host_hw_state |= H_IE;
  41. mei_hcsr_set(dev);
  42. }
  43. /**
  44. * mei_csr_disable_interrupts - disables mei device interrupts
  45. *
  46. * @dev: the device structure
  47. */
  48. void mei_disable_interrupts(struct mei_device *dev)
  49. {
  50. dev->host_hw_state &= ~H_IE;
  51. mei_hcsr_set(dev);
  52. }
  53. /**
  54. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  55. *
  56. * @device: the device structure
  57. *
  58. * returns number of filled slots
  59. */
  60. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  61. {
  62. char read_ptr, write_ptr;
  63. dev->host_hw_state = mei_hcsr_read(dev);
  64. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  65. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  66. return (unsigned char) (write_ptr - read_ptr);
  67. }
  68. /**
  69. * mei_hbuf_is_empty - checks if host buffer is empty.
  70. *
  71. * @dev: the device structure
  72. *
  73. * returns true if empty, false - otherwise.
  74. */
  75. bool mei_hbuf_is_empty(struct mei_device *dev)
  76. {
  77. return mei_hbuf_filled_slots(dev) == 0;
  78. }
  79. /**
  80. * mei_hbuf_empty_slots - counts write empty slots.
  81. *
  82. * @dev: the device structure
  83. *
  84. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  85. */
  86. int mei_hbuf_empty_slots(struct mei_device *dev)
  87. {
  88. unsigned char filled_slots, empty_slots;
  89. filled_slots = mei_hbuf_filled_slots(dev);
  90. empty_slots = dev->hbuf_depth - filled_slots;
  91. /* check for overflow */
  92. if (filled_slots > dev->hbuf_depth)
  93. return -EOVERFLOW;
  94. return empty_slots;
  95. }
  96. /**
  97. * mei_write_message - writes a message to mei device.
  98. *
  99. * @dev: the device structure
  100. * @header: header of message
  101. * @write_buffer: message buffer will be written
  102. * @write_length: message size will be written
  103. *
  104. * This function returns -EIO if write has failed
  105. */
  106. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  107. unsigned char *buf, unsigned long length)
  108. {
  109. unsigned long rem, dw_cnt;
  110. u32 *reg_buf = (u32 *)buf;
  111. int i;
  112. int empty_slots;
  113. dev_dbg(&dev->pdev->dev,
  114. "mei_write_message header=%08x.\n",
  115. *((u32 *) header));
  116. empty_slots = mei_hbuf_empty_slots(dev);
  117. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  118. dw_cnt = mei_data2slots(length);
  119. if (empty_slots < 0 || dw_cnt > empty_slots)
  120. return -EIO;
  121. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  122. for (i = 0; i < length / 4; i++)
  123. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  124. rem = length & 0x3;
  125. if (rem > 0) {
  126. u32 reg = 0;
  127. memcpy(&reg, &buf[length - rem], rem);
  128. mei_reg_write(dev, H_CB_WW, reg);
  129. }
  130. dev->host_hw_state = mei_hcsr_read(dev);
  131. dev->host_hw_state |= H_IG;
  132. mei_hcsr_set(dev);
  133. dev->me_hw_state = mei_mecsr_read(dev);
  134. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  135. return -EIO;
  136. return 0;
  137. }
  138. /**
  139. * mei_count_full_read_slots - counts read full slots.
  140. *
  141. * @dev: the device structure
  142. *
  143. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  144. */
  145. int mei_count_full_read_slots(struct mei_device *dev)
  146. {
  147. char read_ptr, write_ptr;
  148. unsigned char buffer_depth, filled_slots;
  149. dev->me_hw_state = mei_mecsr_read(dev);
  150. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  151. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  152. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  153. filled_slots = (unsigned char) (write_ptr - read_ptr);
  154. /* check for overflow */
  155. if (filled_slots > buffer_depth)
  156. return -EOVERFLOW;
  157. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  158. return (int)filled_slots;
  159. }
  160. /**
  161. * mei_read_slots - reads a message from mei device.
  162. *
  163. * @dev: the device structure
  164. * @buffer: message buffer will be written
  165. * @buffer_length: message size will be read
  166. */
  167. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  168. unsigned long buffer_length)
  169. {
  170. u32 *reg_buf = (u32 *)buffer;
  171. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  172. *reg_buf++ = mei_mecbrw_read(dev);
  173. if (buffer_length > 0) {
  174. u32 reg = mei_mecbrw_read(dev);
  175. memcpy(reg_buf, &reg, buffer_length);
  176. }
  177. dev->host_hw_state |= H_IG;
  178. mei_hcsr_set(dev);
  179. }
  180. /**
  181. * mei_flow_ctrl_creds - checks flow_control credentials.
  182. *
  183. * @dev: the device structure
  184. * @cl: private data of the file object
  185. *
  186. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  187. * -ENOENT if mei_cl is not present
  188. * -EINVAL if single_recv_buf == 0
  189. */
  190. int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
  191. {
  192. int i;
  193. if (!dev->me_clients_num)
  194. return 0;
  195. if (cl->mei_flow_ctrl_creds > 0)
  196. return 1;
  197. for (i = 0; i < dev->me_clients_num; i++) {
  198. struct mei_me_client *me_cl = &dev->me_clients[i];
  199. if (me_cl->client_id == cl->me_client_id) {
  200. if (me_cl->mei_flow_ctrl_creds) {
  201. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  202. return -EINVAL;
  203. return 1;
  204. } else {
  205. return 0;
  206. }
  207. }
  208. }
  209. return -ENOENT;
  210. }
  211. /**
  212. * mei_flow_ctrl_reduce - reduces flow_control.
  213. *
  214. * @dev: the device structure
  215. * @cl: private data of the file object
  216. * @returns
  217. * 0 on success
  218. * -ENOENT when me client is not found
  219. * -EINVAL when ctrl credits are <= 0
  220. */
  221. int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
  222. {
  223. int i;
  224. if (!dev->me_clients_num)
  225. return -ENOENT;
  226. for (i = 0; i < dev->me_clients_num; i++) {
  227. struct mei_me_client *me_cl = &dev->me_clients[i];
  228. if (me_cl->client_id == cl->me_client_id) {
  229. if (me_cl->props.single_recv_buf != 0) {
  230. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  231. return -EINVAL;
  232. dev->me_clients[i].mei_flow_ctrl_creds--;
  233. } else {
  234. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  235. return -EINVAL;
  236. cl->mei_flow_ctrl_creds--;
  237. }
  238. return 0;
  239. }
  240. }
  241. return -ENOENT;
  242. }
  243. /**
  244. * mei_send_flow_control - sends flow control to fw.
  245. *
  246. * @dev: the device structure
  247. * @cl: private data of the file object
  248. *
  249. * This function returns -EIO on write failure
  250. */
  251. int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
  252. {
  253. struct mei_msg_hdr *mei_hdr;
  254. struct hbm_flow_control *flow_ctrl;
  255. const size_t len = sizeof(struct hbm_flow_control);
  256. mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  257. flow_ctrl = (struct hbm_flow_control *)&dev->wr_msg_buf[1];
  258. memset(flow_ctrl, 0, len);
  259. flow_ctrl->hbm_cmd = MEI_FLOW_CONTROL_CMD;
  260. flow_ctrl->host_addr = cl->host_client_id;
  261. flow_ctrl->me_addr = cl->me_client_id;
  262. /* FIXME: reserved !? */
  263. memset(flow_ctrl->reserved, 0, sizeof(flow_ctrl->reserved));
  264. dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
  265. cl->host_client_id, cl->me_client_id);
  266. return mei_write_message(dev, mei_hdr,
  267. (unsigned char *) flow_ctrl, len);
  268. }
  269. /**
  270. * mei_other_client_is_connecting - checks if other
  271. * client with the same client id is connected.
  272. *
  273. * @dev: the device structure
  274. * @cl: private data of the file object
  275. *
  276. * returns 1 if other client is connected, 0 - otherwise.
  277. */
  278. int mei_other_client_is_connecting(struct mei_device *dev,
  279. struct mei_cl *cl)
  280. {
  281. struct mei_cl *cl_pos = NULL;
  282. struct mei_cl *cl_next = NULL;
  283. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  284. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  285. (cl_pos != cl) &&
  286. cl->me_client_id == cl_pos->me_client_id)
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. /**
  292. * mei_disconnect - sends disconnect message to fw.
  293. *
  294. * @dev: the device structure
  295. * @cl: private data of the file object
  296. *
  297. * This function returns -EIO on write failure
  298. */
  299. int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
  300. {
  301. struct mei_msg_hdr *mei_hdr;
  302. struct hbm_client_connect_request *req;
  303. const size_t len = sizeof(struct hbm_client_connect_request);
  304. mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  305. req = (struct hbm_client_connect_request *)&dev->wr_msg_buf[1];
  306. memset(req, 0, len);
  307. req->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
  308. req->host_addr = cl->host_client_id;
  309. req->me_addr = cl->me_client_id;
  310. req->reserved = 0;
  311. return mei_write_message(dev, mei_hdr, (unsigned char *)req, len);
  312. }
  313. /**
  314. * mei_connect - sends connect message to fw.
  315. *
  316. * @dev: the device structure
  317. * @cl: private data of the file object
  318. *
  319. * This function returns -EIO on write failure
  320. */
  321. int mei_connect(struct mei_device *dev, struct mei_cl *cl)
  322. {
  323. struct mei_msg_hdr *mei_hdr;
  324. struct hbm_client_connect_request *req;
  325. const size_t len = sizeof(struct hbm_client_connect_request);
  326. mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  327. req = (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
  328. req->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
  329. req->host_addr = cl->host_client_id;
  330. req->me_addr = cl->me_client_id;
  331. req->reserved = 0;
  332. return mei_write_message(dev, mei_hdr, (unsigned char *) req, len);
  333. }