interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. * _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. * This function returns -EIO if write has failed
  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 -EIO;
  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 -EIO;
  153. return 0;
  154. }
  155. /**
  156. * mei_count_full_read_slots - counts read full slots.
  157. *
  158. * @dev: the device structure
  159. *
  160. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  161. */
  162. int mei_count_full_read_slots(struct mei_device *dev)
  163. {
  164. char read_ptr, write_ptr;
  165. unsigned char buffer_depth, filled_slots;
  166. dev->me_hw_state = mei_mecsr_read(dev);
  167. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  168. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  169. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  170. filled_slots = (unsigned char) (write_ptr - read_ptr);
  171. /* check for overflow */
  172. if (filled_slots > buffer_depth)
  173. return -EOVERFLOW;
  174. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  175. return (int)filled_slots;
  176. }
  177. /**
  178. * mei_read_slots - reads a message from mei device.
  179. *
  180. * @dev: the device structure
  181. * @buffer: message buffer will be written
  182. * @buffer_length: message size will be read
  183. */
  184. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  185. unsigned long buffer_length)
  186. {
  187. u32 *reg_buf = (u32 *)buffer;
  188. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  189. *reg_buf++ = mei_mecbrw_read(dev);
  190. if (buffer_length > 0) {
  191. u32 reg = mei_mecbrw_read(dev);
  192. memcpy(reg_buf, &reg, buffer_length);
  193. }
  194. dev->host_hw_state |= H_IG;
  195. mei_hcsr_set(dev);
  196. }
  197. /**
  198. * mei_flow_ctrl_creds - checks flow_control credentials.
  199. *
  200. * @dev: the device structure
  201. * @cl: private data of the file object
  202. *
  203. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  204. * -ENOENT if mei_cl is not present
  205. * -EINVAL if single_recv_buf == 0
  206. */
  207. int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
  208. {
  209. int i;
  210. if (!dev->me_clients_num)
  211. return 0;
  212. if (cl->mei_flow_ctrl_creds > 0)
  213. return 1;
  214. for (i = 0; i < dev->me_clients_num; i++) {
  215. struct mei_me_client *me_cl = &dev->me_clients[i];
  216. if (me_cl->client_id == cl->me_client_id) {
  217. if (me_cl->mei_flow_ctrl_creds) {
  218. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  219. return -EINVAL;
  220. return 1;
  221. } else {
  222. return 0;
  223. }
  224. }
  225. }
  226. return -ENOENT;
  227. }
  228. /**
  229. * mei_flow_ctrl_reduce - reduces flow_control.
  230. *
  231. * @dev: the device structure
  232. * @cl: private data of the file object
  233. * @returns
  234. * 0 on success
  235. * -ENOENT when me client is not found
  236. * -EINVAL when ctrl credits are <= 0
  237. */
  238. int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
  239. {
  240. int i;
  241. if (!dev->me_clients_num)
  242. return -ENOENT;
  243. for (i = 0; i < dev->me_clients_num; i++) {
  244. struct mei_me_client *me_cl = &dev->me_clients[i];
  245. if (me_cl->client_id == cl->me_client_id) {
  246. if (me_cl->props.single_recv_buf != 0) {
  247. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  248. return -EINVAL;
  249. dev->me_clients[i].mei_flow_ctrl_creds--;
  250. } else {
  251. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  252. return -EINVAL;
  253. cl->mei_flow_ctrl_creds--;
  254. }
  255. return 0;
  256. }
  257. }
  258. return -ENOENT;
  259. }
  260. /**
  261. * mei_send_flow_control - sends flow control to fw.
  262. *
  263. * @dev: the device structure
  264. * @cl: private data of the file object
  265. *
  266. * This function returns -EIO on write failure
  267. */
  268. int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
  269. {
  270. struct mei_msg_hdr *mei_hdr;
  271. struct hbm_flow_control *mei_flow_control;
  272. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  273. mei_hdr->host_addr = 0;
  274. mei_hdr->me_addr = 0;
  275. mei_hdr->length = sizeof(struct hbm_flow_control);
  276. mei_hdr->msg_complete = 1;
  277. mei_hdr->reserved = 0;
  278. mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
  279. memset(mei_flow_control, 0, sizeof(*mei_flow_control));
  280. mei_flow_control->host_addr = cl->host_client_id;
  281. mei_flow_control->me_addr = cl->me_client_id;
  282. mei_flow_control->hbm_cmd = MEI_FLOW_CONTROL_CMD;
  283. memset(mei_flow_control->reserved, 0,
  284. sizeof(mei_flow_control->reserved));
  285. dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
  286. cl->host_client_id, cl->me_client_id);
  287. return mei_write_message(dev, mei_hdr,
  288. (unsigned char *) mei_flow_control,
  289. sizeof(struct hbm_flow_control));
  290. }
  291. /**
  292. * mei_other_client_is_connecting - checks if other
  293. * client with the same client id is connected.
  294. *
  295. * @dev: the device structure
  296. * @cl: private data of the file object
  297. *
  298. * returns 1 if other client is connected, 0 - otherwise.
  299. */
  300. int mei_other_client_is_connecting(struct mei_device *dev,
  301. struct mei_cl *cl)
  302. {
  303. struct mei_cl *cl_pos = NULL;
  304. struct mei_cl *cl_next = NULL;
  305. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  306. if ((cl_pos->state == MEI_FILE_CONNECTING) &&
  307. (cl_pos != cl) &&
  308. cl->me_client_id == cl_pos->me_client_id)
  309. return 1;
  310. }
  311. return 0;
  312. }
  313. /**
  314. * mei_disconnect - sends disconnect 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_disconnect(struct mei_device *dev, struct mei_cl *cl)
  322. {
  323. struct mei_msg_hdr *mei_hdr;
  324. struct hbm_client_disconnect_request *mei_cli_disconnect;
  325. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  326. mei_hdr->host_addr = 0;
  327. mei_hdr->me_addr = 0;
  328. mei_hdr->length = sizeof(struct hbm_client_disconnect_request);
  329. mei_hdr->msg_complete = 1;
  330. mei_hdr->reserved = 0;
  331. mei_cli_disconnect =
  332. (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
  333. memset(mei_cli_disconnect, 0, sizeof(*mei_cli_disconnect));
  334. mei_cli_disconnect->host_addr = cl->host_client_id;
  335. mei_cli_disconnect->me_addr = cl->me_client_id;
  336. mei_cli_disconnect->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
  337. mei_cli_disconnect->reserved[0] = 0;
  338. return mei_write_message(dev, mei_hdr,
  339. (unsigned char *) mei_cli_disconnect,
  340. sizeof(struct hbm_client_disconnect_request));
  341. }
  342. /**
  343. * mei_connect - sends connect message to fw.
  344. *
  345. * @dev: the device structure
  346. * @cl: private data of the file object
  347. *
  348. * This function returns -EIO on write failure
  349. */
  350. int mei_connect(struct mei_device *dev, struct mei_cl *cl)
  351. {
  352. struct mei_msg_hdr *mei_hdr;
  353. struct hbm_client_connect_request *mei_cli_connect;
  354. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  355. mei_hdr->host_addr = 0;
  356. mei_hdr->me_addr = 0;
  357. mei_hdr->length = sizeof(struct hbm_client_connect_request);
  358. mei_hdr->msg_complete = 1;
  359. mei_hdr->reserved = 0;
  360. mei_cli_connect =
  361. (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
  362. mei_cli_connect->host_addr = cl->host_client_id;
  363. mei_cli_connect->me_addr = cl->me_client_id;
  364. mei_cli_connect->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
  365. mei_cli_connect->reserved = 0;
  366. return mei_write_message(dev, mei_hdr,
  367. (unsigned char *) mei_cli_connect,
  368. sizeof(struct hbm_client_connect_request));
  369. }