interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 *mei_flow_control;
  255. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  256. mei_hdr->host_addr = 0;
  257. mei_hdr->me_addr = 0;
  258. mei_hdr->length = sizeof(struct hbm_flow_control);
  259. mei_hdr->msg_complete = 1;
  260. mei_hdr->reserved = 0;
  261. mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
  262. memset(mei_flow_control, 0, sizeof(*mei_flow_control));
  263. mei_flow_control->host_addr = cl->host_client_id;
  264. mei_flow_control->me_addr = cl->me_client_id;
  265. mei_flow_control->hbm_cmd = MEI_FLOW_CONTROL_CMD;
  266. memset(mei_flow_control->reserved, 0,
  267. sizeof(mei_flow_control->reserved));
  268. dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
  269. cl->host_client_id, cl->me_client_id);
  270. return mei_write_message(dev, mei_hdr,
  271. (unsigned char *) mei_flow_control,
  272. sizeof(struct hbm_flow_control));
  273. }
  274. /**
  275. * mei_other_client_is_connecting - checks if other
  276. * client with the same client id is connected.
  277. *
  278. * @dev: the device structure
  279. * @cl: private data of the file object
  280. *
  281. * returns 1 if other client is connected, 0 - otherwise.
  282. */
  283. int mei_other_client_is_connecting(struct mei_device *dev,
  284. struct mei_cl *cl)
  285. {
  286. struct mei_cl *cl_pos = NULL;
  287. struct mei_cl *cl_next = NULL;
  288. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  289. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  290. (cl_pos != cl) &&
  291. cl->me_client_id == cl_pos->me_client_id)
  292. return 1;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * mei_disconnect - sends disconnect message to fw.
  298. *
  299. * @dev: the device structure
  300. * @cl: private data of the file object
  301. *
  302. * This function returns -EIO on write failure
  303. */
  304. int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
  305. {
  306. struct mei_msg_hdr *mei_hdr;
  307. struct hbm_client_connect_request *req;
  308. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  309. mei_hdr->host_addr = 0;
  310. mei_hdr->me_addr = 0;
  311. mei_hdr->length = sizeof(struct hbm_client_connect_request);
  312. mei_hdr->msg_complete = 1;
  313. mei_hdr->reserved = 0;
  314. req = (struct hbm_client_connect_request *)&dev->wr_msg_buf[1];
  315. memset(req, 0, sizeof(*req));
  316. req->host_addr = cl->host_client_id;
  317. req->me_addr = cl->me_client_id;
  318. req->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
  319. req->reserved = 0;
  320. return mei_write_message(dev, mei_hdr, (unsigned char *)req,
  321. sizeof(struct hbm_client_connect_request));
  322. }
  323. /**
  324. * mei_connect - sends connect message to fw.
  325. *
  326. * @dev: the device structure
  327. * @cl: private data of the file object
  328. *
  329. * This function returns -EIO on write failure
  330. */
  331. int mei_connect(struct mei_device *dev, struct mei_cl *cl)
  332. {
  333. struct mei_msg_hdr *mei_hdr;
  334. struct hbm_client_connect_request *mei_cli_connect;
  335. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  336. mei_hdr->host_addr = 0;
  337. mei_hdr->me_addr = 0;
  338. mei_hdr->length = sizeof(struct hbm_client_connect_request);
  339. mei_hdr->msg_complete = 1;
  340. mei_hdr->reserved = 0;
  341. mei_cli_connect =
  342. (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
  343. mei_cli_connect->host_addr = cl->host_client_id;
  344. mei_cli_connect->me_addr = cl->me_client_id;
  345. mei_cli_connect->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
  346. mei_cli_connect->reserved = 0;
  347. return mei_write_message(dev, mei_hdr,
  348. (unsigned char *) mei_cli_connect,
  349. sizeof(struct hbm_client_connect_request));
  350. }