amthif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/aio.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/cdev.h>
  26. #include <linux/list.h>
  27. #include <linux/delay.h>
  28. #include <linux/sched.h>
  29. #include <linux/uuid.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/uaccess.h>
  32. #include "mei_dev.h"
  33. #include "hw.h"
  34. #include <linux/mei.h>
  35. #include "interface.h"
  36. const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
  37. 0xa8, 0x46, 0xe0, 0xff, 0x65,
  38. 0x81, 0x4c);
  39. /**
  40. * mei_amthif_reset_params - initializes mei device iamthif
  41. *
  42. * @dev: the device structure
  43. */
  44. void mei_amthif_reset_params(struct mei_device *dev)
  45. {
  46. /* reset iamthif parameters. */
  47. dev->iamthif_current_cb = NULL;
  48. dev->iamthif_msg_buf_size = 0;
  49. dev->iamthif_msg_buf_index = 0;
  50. dev->iamthif_canceled = false;
  51. dev->iamthif_ioctl = false;
  52. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  53. dev->iamthif_timer = 0;
  54. }
  55. /**
  56. * mei_amthif_host_init_ - mei initialization amthif client.
  57. *
  58. * @dev: the device structure
  59. *
  60. */
  61. void mei_amthif_host_init(struct mei_device *dev)
  62. {
  63. int i;
  64. unsigned char *msg_buf;
  65. mei_cl_init(&dev->iamthif_cl, dev);
  66. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  67. /* find ME amthi client */
  68. i = mei_me_cl_update_filext(dev, &dev->iamthif_cl,
  69. &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
  70. if (i < 0) {
  71. dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
  72. return;
  73. }
  74. /* Assign iamthif_mtu to the value received from ME */
  75. dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
  76. dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
  77. dev->me_clients[i].props.max_msg_length);
  78. kfree(dev->iamthif_msg_buf);
  79. dev->iamthif_msg_buf = NULL;
  80. /* allocate storage for ME message buffer */
  81. msg_buf = kcalloc(dev->iamthif_mtu,
  82. sizeof(unsigned char), GFP_KERNEL);
  83. if (!msg_buf) {
  84. dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
  85. return;
  86. }
  87. dev->iamthif_msg_buf = msg_buf;
  88. if (mei_connect(dev, &dev->iamthif_cl)) {
  89. dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
  90. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  91. dev->iamthif_cl.host_client_id = 0;
  92. } else {
  93. dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT;
  94. }
  95. }
  96. /**
  97. * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
  98. *
  99. * @dev: the device structure
  100. * @file: pointer to file object
  101. *
  102. * returns returned a list entry on success, NULL on failure.
  103. */
  104. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  105. struct file *file)
  106. {
  107. struct mei_cl *cl_temp;
  108. struct mei_cl_cb *pos = NULL;
  109. struct mei_cl_cb *next = NULL;
  110. list_for_each_entry_safe(pos, next,
  111. &dev->amthi_read_complete_list.list, list) {
  112. cl_temp = (struct mei_cl *)pos->file_private;
  113. if (cl_temp && cl_temp == &dev->iamthif_cl &&
  114. pos->file_object == file)
  115. return pos;
  116. }
  117. return NULL;
  118. }
  119. /**
  120. * mei_amthif_read - read data from AMTHIF client
  121. *
  122. * @dev: the device structure
  123. * @if_num: minor number
  124. * @file: pointer to file object
  125. * @*ubuf: pointer to user data in user space
  126. * @length: data length to read
  127. * @offset: data read offset
  128. *
  129. * Locking: called under "dev->device_lock" lock
  130. *
  131. * returns
  132. * returned data length on success,
  133. * zero if no data to read,
  134. * negative on failure.
  135. */
  136. int mei_amthif_read(struct mei_device *dev, struct file *file,
  137. char __user *ubuf, size_t length, loff_t *offset)
  138. {
  139. int rets;
  140. int wait_ret;
  141. struct mei_cl_cb *cb = NULL;
  142. struct mei_cl *cl = file->private_data;
  143. unsigned long timeout;
  144. int i;
  145. /* Only Posible if we are in timeout */
  146. if (!cl || cl != &dev->iamthif_cl) {
  147. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  148. return -ETIMEDOUT;
  149. }
  150. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  151. if (i < 0) {
  152. dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
  153. return -ENODEV;
  154. }
  155. dev_dbg(&dev->pdev->dev, "checking amthi data\n");
  156. cb = mei_amthif_find_read_list_entry(dev, file);
  157. /* Check for if we can block or not*/
  158. if (cb == NULL && file->f_flags & O_NONBLOCK)
  159. return -EAGAIN;
  160. dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
  161. while (cb == NULL) {
  162. /* unlock the Mutex */
  163. mutex_unlock(&dev->device_lock);
  164. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  165. (cb = mei_amthif_find_read_list_entry(dev, file)));
  166. if (wait_ret)
  167. return -ERESTARTSYS;
  168. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  169. /* Locking again the Mutex */
  170. mutex_lock(&dev->device_lock);
  171. }
  172. dev_dbg(&dev->pdev->dev, "Got amthi data\n");
  173. dev->iamthif_timer = 0;
  174. if (cb) {
  175. timeout = cb->read_time +
  176. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  177. dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
  178. timeout);
  179. if (time_after(jiffies, timeout)) {
  180. dev_dbg(&dev->pdev->dev, "amthi Time out\n");
  181. /* 15 sec for the message has expired */
  182. list_del(&cb->list);
  183. rets = -ETIMEDOUT;
  184. goto free;
  185. }
  186. }
  187. /* if the whole message will fit remove it from the list */
  188. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  189. list_del(&cb->list);
  190. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  191. /* end of the message has been reached */
  192. list_del(&cb->list);
  193. rets = 0;
  194. goto free;
  195. }
  196. /* else means that not full buffer will be read and do not
  197. * remove message from deletion list
  198. */
  199. dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
  200. cb->response_buffer.size);
  201. dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
  202. /* length is being turncated to PAGE_SIZE, however,
  203. * the buf_idx may point beyond */
  204. length = min_t(size_t, length, (cb->buf_idx - *offset));
  205. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  206. rets = -EFAULT;
  207. else {
  208. rets = length;
  209. if ((*offset + length) < cb->buf_idx) {
  210. *offset += length;
  211. goto out;
  212. }
  213. }
  214. free:
  215. dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
  216. *offset = 0;
  217. mei_io_cb_free(cb);
  218. out:
  219. return rets;
  220. }
  221. /**
  222. * mei_amthif_write - write amthif data to amthif client
  223. *
  224. * @dev: the device structure
  225. * @cb: mei call back struct
  226. *
  227. * returns 0 on success, <0 on failure.
  228. */
  229. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
  230. {
  231. struct mei_msg_hdr mei_hdr;
  232. int ret;
  233. if (!dev || !cb)
  234. return -ENODEV;
  235. dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
  236. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  237. dev->iamthif_current_cb = cb;
  238. dev->iamthif_file_object = cb->file_object;
  239. dev->iamthif_canceled = false;
  240. dev->iamthif_ioctl = true;
  241. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  242. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  243. cb->request_buffer.size);
  244. ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
  245. if (ret < 0)
  246. return ret;
  247. if (ret && dev->mei_host_buffer_is_empty) {
  248. ret = 0;
  249. dev->mei_host_buffer_is_empty = false;
  250. if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
  251. mei_hdr.length = mei_hbuf_max_data(dev);
  252. mei_hdr.msg_complete = 0;
  253. } else {
  254. mei_hdr.length = cb->request_buffer.size;
  255. mei_hdr.msg_complete = 1;
  256. }
  257. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  258. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  259. mei_hdr.reserved = 0;
  260. dev->iamthif_msg_buf_index += mei_hdr.length;
  261. if (mei_write_message(dev, &mei_hdr,
  262. (unsigned char *)(dev->iamthif_msg_buf),
  263. mei_hdr.length))
  264. return -ENODEV;
  265. if (mei_hdr.msg_complete) {
  266. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  267. return -ENODEV;
  268. dev->iamthif_flow_control_pending = true;
  269. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  270. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  271. dev->iamthif_current_cb = cb;
  272. dev->iamthif_file_object = cb->file_object;
  273. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  274. } else {
  275. dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
  276. list_add_tail(&cb->list, &dev->write_list.list);
  277. }
  278. } else {
  279. if (!(dev->mei_host_buffer_is_empty))
  280. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  281. dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
  282. list_add_tail(&cb->list, &dev->write_list.list);
  283. }
  284. return 0;
  285. }
  286. /**
  287. * mei_amthif_run_next_cmd
  288. *
  289. * @dev: the device structure
  290. *
  291. * returns 0 on success, <0 on failure.
  292. */
  293. void mei_amthif_run_next_cmd(struct mei_device *dev)
  294. {
  295. struct mei_cl *cl_tmp;
  296. struct mei_cl_cb *pos = NULL;
  297. struct mei_cl_cb *next = NULL;
  298. int status;
  299. if (!dev)
  300. return;
  301. dev->iamthif_msg_buf_size = 0;
  302. dev->iamthif_msg_buf_index = 0;
  303. dev->iamthif_canceled = false;
  304. dev->iamthif_ioctl = true;
  305. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  306. dev->iamthif_timer = 0;
  307. dev->iamthif_file_object = NULL;
  308. dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
  309. list_for_each_entry_safe(pos, next, &dev->amthi_cmd_list.list, list) {
  310. list_del(&pos->list);
  311. cl_tmp = (struct mei_cl *)pos->file_private;
  312. if (cl_tmp && cl_tmp == &dev->iamthif_cl) {
  313. status = mei_amthif_write(dev, pos);
  314. if (status) {
  315. dev_dbg(&dev->pdev->dev,
  316. "amthi write failed status = %d\n",
  317. status);
  318. return;
  319. }
  320. break;
  321. }
  322. }
  323. }
  324. /**
  325. * mei_amthif_irq_process_completed - processes completed iamthif operation.
  326. *
  327. * @dev: the device structure.
  328. * @slots: free slots.
  329. * @cb_pos: callback block.
  330. * @cl: private data of the file object.
  331. * @cmpl_list: complete list.
  332. *
  333. * returns 0, OK; otherwise, error.
  334. */
  335. int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
  336. struct mei_cl_cb *cb_pos,
  337. struct mei_cl *cl,
  338. struct mei_cl_cb *cmpl_list)
  339. {
  340. struct mei_msg_hdr *mei_hdr;
  341. if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
  342. dev->iamthif_msg_buf_size -
  343. dev->iamthif_msg_buf_index)) {
  344. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  345. mei_hdr->host_addr = cl->host_client_id;
  346. mei_hdr->me_addr = cl->me_client_id;
  347. mei_hdr->length = dev->iamthif_msg_buf_size -
  348. dev->iamthif_msg_buf_index;
  349. mei_hdr->msg_complete = 1;
  350. mei_hdr->reserved = 0;
  351. *slots -= mei_data2slots(mei_hdr->length);
  352. if (mei_write_message(dev, mei_hdr,
  353. (dev->iamthif_msg_buf +
  354. dev->iamthif_msg_buf_index),
  355. mei_hdr->length)) {
  356. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  357. cl->status = -ENODEV;
  358. list_del(&cb_pos->list);
  359. return -ENODEV;
  360. } else {
  361. if (mei_flow_ctrl_reduce(dev, cl))
  362. return -ENODEV;
  363. dev->iamthif_msg_buf_index += mei_hdr->length;
  364. cb_pos->buf_idx = dev->iamthif_msg_buf_index;
  365. cl->status = 0;
  366. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  367. dev->iamthif_flow_control_pending = true;
  368. /* save iamthif cb sent to amthi client */
  369. dev->iamthif_current_cb = cb_pos;
  370. list_move_tail(&cb_pos->list,
  371. &dev->write_waiting_list.list);
  372. }
  373. } else if (*slots == dev->hbuf_depth) {
  374. /* buffer is still empty */
  375. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  376. mei_hdr->host_addr = cl->host_client_id;
  377. mei_hdr->me_addr = cl->me_client_id;
  378. mei_hdr->length =
  379. (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  380. mei_hdr->msg_complete = 0;
  381. mei_hdr->reserved = 0;
  382. *slots -= mei_data2slots(mei_hdr->length);
  383. if (mei_write_message(dev, mei_hdr,
  384. (dev->iamthif_msg_buf +
  385. dev->iamthif_msg_buf_index),
  386. mei_hdr->length)) {
  387. cl->status = -ENODEV;
  388. list_del(&cb_pos->list);
  389. } else {
  390. dev->iamthif_msg_buf_index += mei_hdr->length;
  391. }
  392. return -EMSGSIZE;
  393. } else {
  394. return -EBADMSG;
  395. }
  396. return 0;
  397. }
  398. /**
  399. * mei_amthif_irq_read_message - read routine after ISR to
  400. * handle the read amthi message
  401. *
  402. * @complete_list: An instance of our list structure
  403. * @dev: the device structure
  404. * @mei_hdr: header of amthi message
  405. *
  406. * returns 0 on success, <0 on failure.
  407. */
  408. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  409. struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
  410. {
  411. struct mei_cl *cl;
  412. struct mei_cl_cb *cb;
  413. unsigned char *buffer;
  414. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  415. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  416. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  417. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  418. mei_read_slots(dev, buffer, mei_hdr->length);
  419. dev->iamthif_msg_buf_index += mei_hdr->length;
  420. if (!mei_hdr->msg_complete)
  421. return 0;
  422. dev_dbg(&dev->pdev->dev,
  423. "amthi_message_buffer_index =%d\n",
  424. mei_hdr->length);
  425. dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
  426. if (!dev->iamthif_current_cb)
  427. return -ENODEV;
  428. cb = dev->iamthif_current_cb;
  429. dev->iamthif_current_cb = NULL;
  430. cl = (struct mei_cl *)cb->file_private;
  431. if (!cl)
  432. return -ENODEV;
  433. dev->iamthif_stall_timer = 0;
  434. cb->buf_idx = dev->iamthif_msg_buf_index;
  435. cb->read_time = jiffies;
  436. if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
  437. /* found the iamthif cb */
  438. dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
  439. dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
  440. list_add_tail(&cb->list, &complete_list->list);
  441. }
  442. return 0;
  443. }
  444. /**
  445. * mei_amthif_irq_read - prepares to read amthif data.
  446. *
  447. * @dev: the device structure.
  448. * @slots: free slots.
  449. *
  450. * returns 0, OK; otherwise, error.
  451. */
  452. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
  453. {
  454. if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
  455. + sizeof(struct hbm_flow_control))) {
  456. return -EMSGSIZE;
  457. }
  458. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  459. if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
  460. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  461. return -EIO;
  462. }
  463. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  464. dev->iamthif_state = MEI_IAMTHIF_READING;
  465. dev->iamthif_flow_control_pending = false;
  466. dev->iamthif_msg_buf_index = 0;
  467. dev->iamthif_msg_buf_size = 0;
  468. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  469. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  470. return 0;
  471. }
  472. /**
  473. * mei_amthif_complete - complete amthif callback.
  474. *
  475. * @dev: the device structure.
  476. * @cb_pos: callback block.
  477. */
  478. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  479. {
  480. if (dev->iamthif_canceled != 1) {
  481. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  482. dev->iamthif_stall_timer = 0;
  483. memcpy(cb->response_buffer.data,
  484. dev->iamthif_msg_buf,
  485. dev->iamthif_msg_buf_index);
  486. list_add_tail(&cb->list, &dev->amthi_read_complete_list.list);
  487. dev_dbg(&dev->pdev->dev, "amthi read completed\n");
  488. dev->iamthif_timer = jiffies;
  489. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  490. dev->iamthif_timer);
  491. } else {
  492. mei_amthif_run_next_cmd(dev);
  493. }
  494. dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
  495. wake_up_interruptible(&dev->iamthif_cl.wait);
  496. }