interface.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * @hader: mei HECI header of message
  101. * @buf: message payload will be written
  102. *
  103. * This function returns -EIO if write has failed
  104. */
  105. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  106. unsigned char *buf)
  107. {
  108. unsigned long rem, dw_cnt;
  109. unsigned long length = header->length;
  110. u32 *reg_buf = (u32 *)buf;
  111. int i;
  112. int empty_slots;
  113. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  114. empty_slots = mei_hbuf_empty_slots(dev);
  115. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  116. dw_cnt = mei_data2slots(length);
  117. if (empty_slots < 0 || dw_cnt > empty_slots)
  118. return -EIO;
  119. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  120. for (i = 0; i < length / 4; i++)
  121. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  122. rem = length & 0x3;
  123. if (rem > 0) {
  124. u32 reg = 0;
  125. memcpy(&reg, &buf[length - rem], rem);
  126. mei_reg_write(dev, H_CB_WW, reg);
  127. }
  128. dev->host_hw_state = mei_hcsr_read(dev);
  129. dev->host_hw_state |= H_IG;
  130. mei_hcsr_set(dev);
  131. dev->me_hw_state = mei_mecsr_read(dev);
  132. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  133. return -EIO;
  134. return 0;
  135. }
  136. /**
  137. * mei_count_full_read_slots - counts read full slots.
  138. *
  139. * @dev: the device structure
  140. *
  141. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  142. */
  143. int mei_count_full_read_slots(struct mei_device *dev)
  144. {
  145. char read_ptr, write_ptr;
  146. unsigned char buffer_depth, filled_slots;
  147. dev->me_hw_state = mei_mecsr_read(dev);
  148. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  149. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  150. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  151. filled_slots = (unsigned char) (write_ptr - read_ptr);
  152. /* check for overflow */
  153. if (filled_slots > buffer_depth)
  154. return -EOVERFLOW;
  155. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  156. return (int)filled_slots;
  157. }
  158. /**
  159. * mei_read_slots - reads a message from mei device.
  160. *
  161. * @dev: the device structure
  162. * @buffer: message buffer will be written
  163. * @buffer_length: message size will be read
  164. */
  165. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  166. unsigned long buffer_length)
  167. {
  168. u32 *reg_buf = (u32 *)buffer;
  169. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  170. *reg_buf++ = mei_mecbrw_read(dev);
  171. if (buffer_length > 0) {
  172. u32 reg = mei_mecbrw_read(dev);
  173. memcpy(reg_buf, &reg, buffer_length);
  174. }
  175. dev->host_hw_state |= H_IG;
  176. mei_hcsr_set(dev);
  177. }
  178. /**
  179. * mei_flow_ctrl_creds - checks flow_control credentials.
  180. *
  181. * @dev: the device structure
  182. * @cl: private data of the file object
  183. *
  184. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  185. * -ENOENT if mei_cl is not present
  186. * -EINVAL if single_recv_buf == 0
  187. */
  188. int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
  189. {
  190. int i;
  191. if (!dev->me_clients_num)
  192. return 0;
  193. if (cl->mei_flow_ctrl_creds > 0)
  194. return 1;
  195. for (i = 0; i < dev->me_clients_num; i++) {
  196. struct mei_me_client *me_cl = &dev->me_clients[i];
  197. if (me_cl->client_id == cl->me_client_id) {
  198. if (me_cl->mei_flow_ctrl_creds) {
  199. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  200. return -EINVAL;
  201. return 1;
  202. } else {
  203. return 0;
  204. }
  205. }
  206. }
  207. return -ENOENT;
  208. }
  209. /**
  210. * mei_flow_ctrl_reduce - reduces flow_control.
  211. *
  212. * @dev: the device structure
  213. * @cl: private data of the file object
  214. * @returns
  215. * 0 on success
  216. * -ENOENT when me client is not found
  217. * -EINVAL when ctrl credits are <= 0
  218. */
  219. int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
  220. {
  221. int i;
  222. if (!dev->me_clients_num)
  223. return -ENOENT;
  224. for (i = 0; i < dev->me_clients_num; i++) {
  225. struct mei_me_client *me_cl = &dev->me_clients[i];
  226. if (me_cl->client_id == cl->me_client_id) {
  227. if (me_cl->props.single_recv_buf != 0) {
  228. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  229. return -EINVAL;
  230. dev->me_clients[i].mei_flow_ctrl_creds--;
  231. } else {
  232. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  233. return -EINVAL;
  234. cl->mei_flow_ctrl_creds--;
  235. }
  236. return 0;
  237. }
  238. }
  239. return -ENOENT;
  240. }
  241. /**
  242. * mei_send_flow_control - sends flow control to fw.
  243. *
  244. * @dev: the device structure
  245. * @cl: private data of the file object
  246. *
  247. * This function returns -EIO on write failure
  248. */
  249. int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
  250. {
  251. struct mei_msg_hdr *mei_hdr;
  252. struct hbm_flow_control *flow_ctrl;
  253. const size_t len = sizeof(struct hbm_flow_control);
  254. mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  255. flow_ctrl = (struct hbm_flow_control *)&dev->wr_msg_buf[1];
  256. memset(flow_ctrl, 0, len);
  257. flow_ctrl->hbm_cmd = MEI_FLOW_CONTROL_CMD;
  258. flow_ctrl->host_addr = cl->host_client_id;
  259. flow_ctrl->me_addr = cl->me_client_id;
  260. /* FIXME: reserved !? */
  261. memset(flow_ctrl->reserved, 0, sizeof(flow_ctrl->reserved));
  262. dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
  263. cl->host_client_id, cl->me_client_id);
  264. return mei_write_message(dev, mei_hdr, (unsigned char *) flow_ctrl);
  265. }
  266. /**
  267. * mei_other_client_is_connecting - checks if other
  268. * client with the same client id is connected.
  269. *
  270. * @dev: the device structure
  271. * @cl: private data of the file object
  272. *
  273. * returns 1 if other client is connected, 0 - otherwise.
  274. */
  275. int mei_other_client_is_connecting(struct mei_device *dev,
  276. struct mei_cl *cl)
  277. {
  278. struct mei_cl *cl_pos = NULL;
  279. struct mei_cl *cl_next = NULL;
  280. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  281. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  282. (cl_pos != cl) &&
  283. cl->me_client_id == cl_pos->me_client_id)
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * mei_disconnect - sends disconnect message to fw.
  290. *
  291. * @dev: the device structure
  292. * @cl: private data of the file object
  293. *
  294. * This function returns -EIO on write failure
  295. */
  296. int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
  297. {
  298. struct mei_msg_hdr *hdr;
  299. struct hbm_client_connect_request *req;
  300. const size_t len = sizeof(struct hbm_client_connect_request);
  301. hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  302. req = (struct hbm_client_connect_request *)&dev->wr_msg_buf[1];
  303. memset(req, 0, len);
  304. req->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
  305. req->host_addr = cl->host_client_id;
  306. req->me_addr = cl->me_client_id;
  307. req->reserved = 0;
  308. return mei_write_message(dev, hdr, (unsigned char *)req);
  309. }
  310. /**
  311. * mei_connect - sends connect message to fw.
  312. *
  313. * @dev: the device structure
  314. * @cl: private data of the file object
  315. *
  316. * This function returns -EIO on write failure
  317. */
  318. int mei_connect(struct mei_device *dev, struct mei_cl *cl)
  319. {
  320. struct mei_msg_hdr *hdr;
  321. struct hbm_client_connect_request *req;
  322. const size_t len = sizeof(struct hbm_client_connect_request);
  323. hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
  324. req = (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
  325. req->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
  326. req->host_addr = cl->host_client_id;
  327. req->me_addr = cl->me_client_id;
  328. req->reserved = 0;
  329. return mei_write_message(dev, hdr, (unsigned char *) req);
  330. }