interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2011, 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 "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. * _host_get_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 _host_get_filled_slots(const struct mei_device *dev)
  61. {
  62. char read_ptr, write_ptr;
  63. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  64. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  65. return (unsigned char) (write_ptr - read_ptr);
  66. }
  67. /**
  68. * mei_host_buffer_is_empty - checks if host buffer is empty.
  69. *
  70. * @dev: the device structure
  71. *
  72. * returns 1 if empty, 0 - otherwise.
  73. */
  74. int mei_host_buffer_is_empty(struct mei_device *dev)
  75. {
  76. unsigned char filled_slots;
  77. dev->host_hw_state = mei_hcsr_read(dev);
  78. filled_slots = _host_get_filled_slots(dev);
  79. if (filled_slots == 0)
  80. return 1;
  81. return 0;
  82. }
  83. /**
  84. * mei_count_empty_write_slots - counts write empty slots.
  85. *
  86. * @dev: the device structure
  87. *
  88. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  89. */
  90. int mei_count_empty_write_slots(struct mei_device *dev)
  91. {
  92. unsigned char buffer_depth, filled_slots, empty_slots;
  93. dev->host_hw_state = mei_hcsr_read(dev);
  94. buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
  95. filled_slots = _host_get_filled_slots(dev);
  96. empty_slots = buffer_depth - filled_slots;
  97. /* check for overflow */
  98. if (filled_slots > buffer_depth)
  99. return -EOVERFLOW;
  100. return empty_slots;
  101. }
  102. /**
  103. * mei_write_message - writes a message to mei device.
  104. *
  105. * @dev: the device structure
  106. * @header: header of message
  107. * @write_buffer: message buffer will be written
  108. * @write_length: message size will be written
  109. *
  110. * returns 1 if success, 0 - otherwise.
  111. */
  112. int mei_write_message(struct mei_device *dev,
  113. struct mei_msg_hdr *header,
  114. unsigned char *write_buffer,
  115. unsigned long write_length)
  116. {
  117. u32 temp_msg = 0;
  118. unsigned long bytes_written = 0;
  119. unsigned char buffer_depth, filled_slots, empty_slots;
  120. unsigned long dw_to_write;
  121. dev->host_hw_state = mei_hcsr_read(dev);
  122. dev_dbg(&dev->pdev->dev,
  123. "host_hw_state = 0x%08x.\n",
  124. dev->host_hw_state);
  125. dev_dbg(&dev->pdev->dev,
  126. "mei_write_message header=%08x.\n",
  127. *((u32 *) header));
  128. buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
  129. filled_slots = _host_get_filled_slots(dev);
  130. empty_slots = buffer_depth - filled_slots;
  131. dev_dbg(&dev->pdev->dev,
  132. "filled = %hu, empty = %hu.\n",
  133. filled_slots, empty_slots);
  134. dw_to_write = ((write_length + 3) / 4);
  135. if (dw_to_write > empty_slots)
  136. return 0;
  137. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  138. while (write_length >= 4) {
  139. mei_reg_write(dev, H_CB_WW,
  140. *(u32 *) (write_buffer + bytes_written));
  141. bytes_written += 4;
  142. write_length -= 4;
  143. }
  144. if (write_length > 0) {
  145. memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
  146. mei_reg_write(dev, H_CB_WW, temp_msg);
  147. }
  148. dev->host_hw_state |= H_IG;
  149. mei_hcsr_set(dev);
  150. dev->me_hw_state = mei_mecsr_read(dev);
  151. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  152. return 0;
  153. dev->write_hang = 0;
  154. return 1;
  155. }
  156. /**
  157. * mei_count_full_read_slots - counts read full slots.
  158. *
  159. * @dev: the device structure
  160. *
  161. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  162. */
  163. int mei_count_full_read_slots(struct mei_device *dev)
  164. {
  165. char read_ptr, write_ptr;
  166. unsigned char buffer_depth, filled_slots;
  167. dev->me_hw_state = mei_mecsr_read(dev);
  168. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  169. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  170. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  171. filled_slots = (unsigned char) (write_ptr - read_ptr);
  172. /* check for overflow */
  173. if (filled_slots > buffer_depth)
  174. return -EOVERFLOW;
  175. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  176. return (int)filled_slots;
  177. }
  178. /**
  179. * mei_read_slots - reads a message from mei device.
  180. *
  181. * @dev: the device structure
  182. * @buffer: message buffer will be written
  183. * @buffer_length: message size will be read
  184. */
  185. void mei_read_slots(struct mei_device *dev,
  186. unsigned char *buffer, unsigned long buffer_length)
  187. {
  188. u32 i = 0;
  189. unsigned char temp_buf[sizeof(u32)];
  190. while (buffer_length >= sizeof(u32)) {
  191. ((u32 *) buffer)[i] = mei_mecbrw_read(dev);
  192. dev_dbg(&dev->pdev->dev,
  193. "buffer[%d]= %d\n",
  194. i, ((u32 *) buffer)[i]);
  195. i++;
  196. buffer_length -= sizeof(u32);
  197. }
  198. if (buffer_length > 0) {
  199. *((u32 *) &temp_buf) = mei_mecbrw_read(dev);
  200. memcpy(&buffer[i * 4], temp_buf, buffer_length);
  201. }
  202. dev->host_hw_state |= H_IG;
  203. mei_hcsr_set(dev);
  204. }
  205. /**
  206. * mei_flow_ctrl_creds - checks flow_control credentials.
  207. *
  208. * @dev: the device structure
  209. * @cl: private data of the file object
  210. *
  211. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  212. * -ENOENT if mei_cl is not present
  213. * -EINVAL if single_recv_buf == 0
  214. */
  215. int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
  216. {
  217. int i;
  218. if (!dev->num_mei_me_clients)
  219. return 0;
  220. if (cl->mei_flow_ctrl_creds > 0)
  221. return 1;
  222. for (i = 0; i < dev->num_mei_me_clients; i++) {
  223. struct mei_me_client *me_cl = &dev->me_clients[i];
  224. if (me_cl->client_id == cl->me_client_id) {
  225. if (me_cl->mei_flow_ctrl_creds) {
  226. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  227. return -EINVAL;
  228. return 1;
  229. } else {
  230. return 0;
  231. }
  232. }
  233. }
  234. return -ENOENT;
  235. }
  236. /**
  237. * mei_flow_ctrl_reduce - reduces flow_control.
  238. *
  239. * @dev: the device structure
  240. * @cl: private data of the file object
  241. * @returns
  242. * 0 on success
  243. * -ENOENT when me client is not found
  244. * -EINVAL wehn ctrl credits are <= 0
  245. */
  246. int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
  247. {
  248. int i;
  249. if (!dev->num_mei_me_clients)
  250. return -ENOENT;
  251. for (i = 0; i < dev->num_mei_me_clients; i++) {
  252. struct mei_me_client *me_cl = &dev->me_clients[i];
  253. if (me_cl->client_id == cl->me_client_id) {
  254. if (me_cl->props.single_recv_buf != 0) {
  255. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  256. return -EINVAL;
  257. dev->me_clients[i].mei_flow_ctrl_creds--;
  258. } else {
  259. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  260. return -EINVAL;
  261. cl->mei_flow_ctrl_creds--;
  262. }
  263. return 0;
  264. }
  265. }
  266. return -ENOENT;
  267. }
  268. /**
  269. * mei_send_flow_control - sends flow control to fw.
  270. *
  271. * @dev: the device structure
  272. * @cl: private data of the file object
  273. *
  274. * returns 1 if success, 0 - otherwise.
  275. */
  276. int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
  277. {
  278. struct mei_msg_hdr *mei_hdr;
  279. struct hbm_flow_control *mei_flow_control;
  280. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  281. mei_hdr->host_addr = 0;
  282. mei_hdr->me_addr = 0;
  283. mei_hdr->length = sizeof(struct hbm_flow_control);
  284. mei_hdr->msg_complete = 1;
  285. mei_hdr->reserved = 0;
  286. mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
  287. memset(mei_flow_control, 0, sizeof(mei_flow_control));
  288. mei_flow_control->host_addr = cl->host_client_id;
  289. mei_flow_control->me_addr = cl->me_client_id;
  290. mei_flow_control->cmd.cmd = MEI_FLOW_CONTROL_CMD;
  291. memset(mei_flow_control->reserved, 0,
  292. sizeof(mei_flow_control->reserved));
  293. dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
  294. cl->host_client_id, cl->me_client_id);
  295. if (!mei_write_message(dev, mei_hdr,
  296. (unsigned char *) mei_flow_control,
  297. sizeof(struct hbm_flow_control)))
  298. return 0;
  299. return 1;
  300. }
  301. /**
  302. * mei_other_client_is_connecting - checks if other
  303. * client with the same client id is connected.
  304. *
  305. * @dev: the device structure
  306. * @cl: private data of the file object
  307. *
  308. * returns 1 if other client is connected, 0 - otherwise.
  309. */
  310. int mei_other_client_is_connecting(struct mei_device *dev,
  311. struct mei_cl *cl)
  312. {
  313. struct mei_cl *cl_pos = NULL;
  314. struct mei_cl *cl_next = NULL;
  315. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  316. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  317. (cl_pos != cl) &&
  318. cl->me_client_id == cl_pos->me_client_id)
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /**
  324. * mei_disconnect - sends disconnect message to fw.
  325. *
  326. * @dev: the device structure
  327. * @cl: private data of the file object
  328. *
  329. * returns 1 if success, 0 - otherwise.
  330. */
  331. int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
  332. {
  333. struct mei_msg_hdr *mei_hdr;
  334. struct hbm_client_disconnect_request *mei_cli_disconnect;
  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_disconnect_request);
  339. mei_hdr->msg_complete = 1;
  340. mei_hdr->reserved = 0;
  341. mei_cli_disconnect =
  342. (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
  343. memset(mei_cli_disconnect, 0, sizeof(mei_cli_disconnect));
  344. mei_cli_disconnect->host_addr = cl->host_client_id;
  345. mei_cli_disconnect->me_addr = cl->me_client_id;
  346. mei_cli_disconnect->cmd.cmd = CLIENT_DISCONNECT_REQ_CMD;
  347. mei_cli_disconnect->reserved[0] = 0;
  348. if (!mei_write_message(dev, mei_hdr,
  349. (unsigned char *) mei_cli_disconnect,
  350. sizeof(struct hbm_client_disconnect_request)))
  351. return 0;
  352. return 1;
  353. }
  354. /**
  355. * mei_connect - sends connect message to fw.
  356. *
  357. * @dev: the device structure
  358. * @cl: private data of the file object
  359. *
  360. * returns 1 if success, 0 - otherwise.
  361. */
  362. int mei_connect(struct mei_device *dev, struct mei_cl *cl)
  363. {
  364. struct mei_msg_hdr *mei_hdr;
  365. struct hbm_client_connect_request *mei_cli_connect;
  366. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  367. mei_hdr->host_addr = 0;
  368. mei_hdr->me_addr = 0;
  369. mei_hdr->length = sizeof(struct hbm_client_connect_request);
  370. mei_hdr->msg_complete = 1;
  371. mei_hdr->reserved = 0;
  372. mei_cli_connect =
  373. (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
  374. mei_cli_connect->host_addr = cl->host_client_id;
  375. mei_cli_connect->me_addr = cl->me_client_id;
  376. mei_cli_connect->cmd.cmd = CLIENT_CONNECT_REQ_CMD;
  377. mei_cli_connect->reserved = 0;
  378. if (!mei_write_message(dev, mei_hdr,
  379. (unsigned char *) mei_cli_connect,
  380. sizeof(struct hbm_client_connect_request)))
  381. return 0;
  382. return 1;
  383. }