amthif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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_cb *pos = NULL;
  108. struct mei_cl_cb *next = NULL;
  109. list_for_each_entry_safe(pos, next,
  110. &dev->amthif_rd_complete_list.list, list) {
  111. if (pos->cl && pos->cl == &dev->iamthif_cl &&
  112. pos->file_object == file)
  113. return pos;
  114. }
  115. return NULL;
  116. }
  117. /**
  118. * mei_amthif_read - read data from AMTHIF client
  119. *
  120. * @dev: the device structure
  121. * @if_num: minor number
  122. * @file: pointer to file object
  123. * @*ubuf: pointer to user data in user space
  124. * @length: data length to read
  125. * @offset: data read offset
  126. *
  127. * Locking: called under "dev->device_lock" lock
  128. *
  129. * returns
  130. * returned data length on success,
  131. * zero if no data to read,
  132. * negative on failure.
  133. */
  134. int mei_amthif_read(struct mei_device *dev, struct file *file,
  135. char __user *ubuf, size_t length, loff_t *offset)
  136. {
  137. int rets;
  138. int wait_ret;
  139. struct mei_cl_cb *cb = NULL;
  140. struct mei_cl *cl = file->private_data;
  141. unsigned long timeout;
  142. int i;
  143. /* Only Posible if we are in timeout */
  144. if (!cl || cl != &dev->iamthif_cl) {
  145. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  146. return -ETIMEDOUT;
  147. }
  148. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  149. if (i < 0) {
  150. dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
  151. return -ENODEV;
  152. }
  153. dev_dbg(&dev->pdev->dev, "checking amthi data\n");
  154. cb = mei_amthif_find_read_list_entry(dev, file);
  155. /* Check for if we can block or not*/
  156. if (cb == NULL && file->f_flags & O_NONBLOCK)
  157. return -EAGAIN;
  158. dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
  159. while (cb == NULL) {
  160. /* unlock the Mutex */
  161. mutex_unlock(&dev->device_lock);
  162. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  163. (cb = mei_amthif_find_read_list_entry(dev, file)));
  164. if (wait_ret)
  165. return -ERESTARTSYS;
  166. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  167. /* Locking again the Mutex */
  168. mutex_lock(&dev->device_lock);
  169. }
  170. dev_dbg(&dev->pdev->dev, "Got amthi data\n");
  171. dev->iamthif_timer = 0;
  172. if (cb) {
  173. timeout = cb->read_time +
  174. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  175. dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
  176. timeout);
  177. if (time_after(jiffies, timeout)) {
  178. dev_dbg(&dev->pdev->dev, "amthi Time out\n");
  179. /* 15 sec for the message has expired */
  180. list_del(&cb->list);
  181. rets = -ETIMEDOUT;
  182. goto free;
  183. }
  184. }
  185. /* if the whole message will fit remove it from the list */
  186. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  187. list_del(&cb->list);
  188. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  189. /* end of the message has been reached */
  190. list_del(&cb->list);
  191. rets = 0;
  192. goto free;
  193. }
  194. /* else means that not full buffer will be read and do not
  195. * remove message from deletion list
  196. */
  197. dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
  198. cb->response_buffer.size);
  199. dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
  200. /* length is being turncated to PAGE_SIZE, however,
  201. * the buf_idx may point beyond */
  202. length = min_t(size_t, length, (cb->buf_idx - *offset));
  203. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  204. rets = -EFAULT;
  205. else {
  206. rets = length;
  207. if ((*offset + length) < cb->buf_idx) {
  208. *offset += length;
  209. goto out;
  210. }
  211. }
  212. free:
  213. dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
  214. *offset = 0;
  215. mei_io_cb_free(cb);
  216. out:
  217. return rets;
  218. }
  219. /**
  220. * mei_amthif_send_cmd - send amthif command to the ME
  221. *
  222. * @dev: the device structure
  223. * @cb: mei call back struct
  224. *
  225. * returns 0 on success, <0 on failure.
  226. *
  227. */
  228. static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
  229. {
  230. struct mei_msg_hdr mei_hdr;
  231. int ret;
  232. if (!dev || !cb)
  233. return -ENODEV;
  234. dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
  235. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  236. dev->iamthif_current_cb = cb;
  237. dev->iamthif_file_object = cb->file_object;
  238. dev->iamthif_canceled = false;
  239. dev->iamthif_ioctl = true;
  240. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  241. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  242. cb->request_buffer.size);
  243. ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
  244. if (ret < 0)
  245. return ret;
  246. if (ret && dev->mei_host_buffer_is_empty) {
  247. ret = 0;
  248. dev->mei_host_buffer_is_empty = false;
  249. if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
  250. mei_hdr.length = mei_hbuf_max_data(dev);
  251. mei_hdr.msg_complete = 0;
  252. } else {
  253. mei_hdr.length = cb->request_buffer.size;
  254. mei_hdr.msg_complete = 1;
  255. }
  256. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  257. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  258. mei_hdr.reserved = 0;
  259. dev->iamthif_msg_buf_index += mei_hdr.length;
  260. if (mei_write_message(dev, &mei_hdr,
  261. (unsigned char *)(dev->iamthif_msg_buf),
  262. mei_hdr.length))
  263. return -ENODEV;
  264. if (mei_hdr.msg_complete) {
  265. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  266. return -ENODEV;
  267. dev->iamthif_flow_control_pending = true;
  268. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  269. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  270. dev->iamthif_current_cb = cb;
  271. dev->iamthif_file_object = cb->file_object;
  272. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  273. } else {
  274. dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
  275. list_add_tail(&cb->list, &dev->write_list.list);
  276. }
  277. } else {
  278. if (!(dev->mei_host_buffer_is_empty))
  279. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  280. dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
  281. list_add_tail(&cb->list, &dev->write_list.list);
  282. }
  283. return 0;
  284. }
  285. /**
  286. * mei_amthif_write - write amthif data to amthif client
  287. *
  288. * @dev: the device structure
  289. * @cb: mei call back struct
  290. *
  291. * returns 0 on success, <0 on failure.
  292. *
  293. */
  294. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
  295. {
  296. int ret;
  297. if (!dev || !cb)
  298. return -ENODEV;
  299. ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
  300. if (ret)
  301. return ret;
  302. cb->fop_type = MEI_FOP_IOCTL;
  303. if (!list_empty(&dev->amthif_cmd_list.list) ||
  304. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  305. dev_dbg(&dev->pdev->dev,
  306. "amthif state = %d\n", dev->iamthif_state);
  307. dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
  308. list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
  309. return 0;
  310. }
  311. return mei_amthif_send_cmd(dev, cb);
  312. }
  313. /**
  314. * mei_amthif_run_next_cmd
  315. *
  316. * @dev: the device structure
  317. *
  318. * returns 0 on success, <0 on failure.
  319. */
  320. void mei_amthif_run_next_cmd(struct mei_device *dev)
  321. {
  322. struct mei_cl_cb *pos = NULL;
  323. struct mei_cl_cb *next = NULL;
  324. int status;
  325. if (!dev)
  326. return;
  327. dev->iamthif_msg_buf_size = 0;
  328. dev->iamthif_msg_buf_index = 0;
  329. dev->iamthif_canceled = false;
  330. dev->iamthif_ioctl = true;
  331. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  332. dev->iamthif_timer = 0;
  333. dev->iamthif_file_object = NULL;
  334. dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
  335. list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
  336. list_del(&pos->list);
  337. if (pos->cl && pos->cl == &dev->iamthif_cl) {
  338. status = mei_amthif_send_cmd(dev, pos);
  339. if (status) {
  340. dev_dbg(&dev->pdev->dev,
  341. "amthi write failed status = %d\n",
  342. status);
  343. return;
  344. }
  345. break;
  346. }
  347. }
  348. }
  349. unsigned int mei_amthif_poll(struct mei_device *dev,
  350. struct file *file, poll_table *wait)
  351. {
  352. unsigned int mask = 0;
  353. mutex_unlock(&dev->device_lock);
  354. poll_wait(file, &dev->iamthif_cl.wait, wait);
  355. mutex_lock(&dev->device_lock);
  356. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
  357. dev->iamthif_file_object == file) {
  358. mask |= (POLLIN | POLLRDNORM);
  359. dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
  360. mei_amthif_run_next_cmd(dev);
  361. }
  362. return mask;
  363. }
  364. /**
  365. * mei_amthif_irq_process_completed - processes completed iamthif operation.
  366. *
  367. * @dev: the device structure.
  368. * @slots: free slots.
  369. * @cb_pos: callback block.
  370. * @cl: private data of the file object.
  371. * @cmpl_list: complete list.
  372. *
  373. * returns 0, OK; otherwise, error.
  374. */
  375. int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
  376. struct mei_cl_cb *cb_pos,
  377. struct mei_cl *cl,
  378. struct mei_cl_cb *cmpl_list)
  379. {
  380. struct mei_msg_hdr *mei_hdr;
  381. if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
  382. dev->iamthif_msg_buf_size -
  383. dev->iamthif_msg_buf_index)) {
  384. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  385. mei_hdr->host_addr = cl->host_client_id;
  386. mei_hdr->me_addr = cl->me_client_id;
  387. mei_hdr->length = dev->iamthif_msg_buf_size -
  388. dev->iamthif_msg_buf_index;
  389. mei_hdr->msg_complete = 1;
  390. mei_hdr->reserved = 0;
  391. *slots -= mei_data2slots(mei_hdr->length);
  392. if (mei_write_message(dev, mei_hdr,
  393. (dev->iamthif_msg_buf +
  394. dev->iamthif_msg_buf_index),
  395. mei_hdr->length)) {
  396. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  397. cl->status = -ENODEV;
  398. list_del(&cb_pos->list);
  399. return -ENODEV;
  400. } else {
  401. if (mei_flow_ctrl_reduce(dev, cl))
  402. return -ENODEV;
  403. dev->iamthif_msg_buf_index += mei_hdr->length;
  404. cb_pos->buf_idx = dev->iamthif_msg_buf_index;
  405. cl->status = 0;
  406. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  407. dev->iamthif_flow_control_pending = true;
  408. /* save iamthif cb sent to amthi client */
  409. dev->iamthif_current_cb = cb_pos;
  410. list_move_tail(&cb_pos->list,
  411. &dev->write_waiting_list.list);
  412. }
  413. } else if (*slots == dev->hbuf_depth) {
  414. /* buffer is still empty */
  415. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  416. mei_hdr->host_addr = cl->host_client_id;
  417. mei_hdr->me_addr = cl->me_client_id;
  418. mei_hdr->length =
  419. (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  420. mei_hdr->msg_complete = 0;
  421. mei_hdr->reserved = 0;
  422. *slots -= mei_data2slots(mei_hdr->length);
  423. if (mei_write_message(dev, mei_hdr,
  424. (dev->iamthif_msg_buf +
  425. dev->iamthif_msg_buf_index),
  426. mei_hdr->length)) {
  427. cl->status = -ENODEV;
  428. list_del(&cb_pos->list);
  429. } else {
  430. dev->iamthif_msg_buf_index += mei_hdr->length;
  431. }
  432. return -EMSGSIZE;
  433. } else {
  434. return -EBADMSG;
  435. }
  436. return 0;
  437. }
  438. /**
  439. * mei_amthif_irq_read_message - read routine after ISR to
  440. * handle the read amthi message
  441. *
  442. * @complete_list: An instance of our list structure
  443. * @dev: the device structure
  444. * @mei_hdr: header of amthi message
  445. *
  446. * returns 0 on success, <0 on failure.
  447. */
  448. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  449. struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
  450. {
  451. struct mei_cl_cb *cb;
  452. unsigned char *buffer;
  453. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  454. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  455. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  456. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  457. mei_read_slots(dev, buffer, mei_hdr->length);
  458. dev->iamthif_msg_buf_index += mei_hdr->length;
  459. if (!mei_hdr->msg_complete)
  460. return 0;
  461. dev_dbg(&dev->pdev->dev,
  462. "amthi_message_buffer_index =%d\n",
  463. mei_hdr->length);
  464. dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
  465. if (!dev->iamthif_current_cb)
  466. return -ENODEV;
  467. cb = dev->iamthif_current_cb;
  468. dev->iamthif_current_cb = NULL;
  469. if (!cb->cl)
  470. return -ENODEV;
  471. dev->iamthif_stall_timer = 0;
  472. cb->buf_idx = dev->iamthif_msg_buf_index;
  473. cb->read_time = jiffies;
  474. if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
  475. /* found the iamthif cb */
  476. dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
  477. dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
  478. list_add_tail(&cb->list, &complete_list->list);
  479. }
  480. return 0;
  481. }
  482. /**
  483. * mei_amthif_irq_read - prepares to read amthif data.
  484. *
  485. * @dev: the device structure.
  486. * @slots: free slots.
  487. *
  488. * returns 0, OK; otherwise, error.
  489. */
  490. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
  491. {
  492. if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
  493. + sizeof(struct hbm_flow_control))) {
  494. return -EMSGSIZE;
  495. }
  496. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  497. if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
  498. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  499. return -EIO;
  500. }
  501. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  502. dev->iamthif_state = MEI_IAMTHIF_READING;
  503. dev->iamthif_flow_control_pending = false;
  504. dev->iamthif_msg_buf_index = 0;
  505. dev->iamthif_msg_buf_size = 0;
  506. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  507. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  508. return 0;
  509. }
  510. /**
  511. * mei_amthif_complete - complete amthif callback.
  512. *
  513. * @dev: the device structure.
  514. * @cb_pos: callback block.
  515. */
  516. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  517. {
  518. if (dev->iamthif_canceled != 1) {
  519. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  520. dev->iamthif_stall_timer = 0;
  521. memcpy(cb->response_buffer.data,
  522. dev->iamthif_msg_buf,
  523. dev->iamthif_msg_buf_index);
  524. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  525. dev_dbg(&dev->pdev->dev, "amthi read completed\n");
  526. dev->iamthif_timer = jiffies;
  527. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  528. dev->iamthif_timer);
  529. } else {
  530. mei_amthif_run_next_cmd(dev);
  531. }
  532. dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
  533. wake_up_interruptible(&dev->iamthif_cl.wait);
  534. }
  535. /**
  536. * mei_clear_list - removes all callbacks associated with file
  537. * from mei_cb_list
  538. *
  539. * @dev: device structure.
  540. * @file: file structure
  541. * @mei_cb_list: callbacks list
  542. *
  543. * mei_clear_list is called to clear resources associated with file
  544. * when application calls close function or Ctrl-C was pressed
  545. *
  546. * returns true if callback removed from the list, false otherwise
  547. */
  548. static bool mei_clear_list(struct mei_device *dev,
  549. const struct file *file, struct list_head *mei_cb_list)
  550. {
  551. struct mei_cl_cb *cb_pos = NULL;
  552. struct mei_cl_cb *cb_next = NULL;
  553. bool removed = false;
  554. /* list all list member */
  555. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
  556. /* check if list member associated with a file */
  557. if (file == cb_pos->file_object) {
  558. /* remove member from the list */
  559. list_del(&cb_pos->list);
  560. /* check if cb equal to current iamthif cb */
  561. if (dev->iamthif_current_cb == cb_pos) {
  562. dev->iamthif_current_cb = NULL;
  563. /* send flow control to iamthif client */
  564. mei_send_flow_control(dev, &dev->iamthif_cl);
  565. }
  566. /* free all allocated buffers */
  567. mei_io_cb_free(cb_pos);
  568. cb_pos = NULL;
  569. removed = true;
  570. }
  571. }
  572. return removed;
  573. }
  574. /**
  575. * mei_clear_lists - removes all callbacks associated with file
  576. *
  577. * @dev: device structure
  578. * @file: file structure
  579. *
  580. * mei_clear_lists is called to clear resources associated with file
  581. * when application calls close function or Ctrl-C was pressed
  582. *
  583. * returns true if callback removed from the list, false otherwise
  584. */
  585. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  586. {
  587. bool removed = false;
  588. /* remove callbacks associated with a file */
  589. mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
  590. if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
  591. removed = true;
  592. mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
  593. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
  594. removed = true;
  595. if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
  596. removed = true;
  597. if (mei_clear_list(dev, file, &dev->write_list.list))
  598. removed = true;
  599. /* check if iamthif_current_cb not NULL */
  600. if (dev->iamthif_current_cb && !removed) {
  601. /* check file and iamthif current cb association */
  602. if (dev->iamthif_current_cb->file_object == file) {
  603. /* remove cb */
  604. mei_io_cb_free(dev->iamthif_current_cb);
  605. dev->iamthif_current_cb = NULL;
  606. removed = true;
  607. }
  608. }
  609. return removed;
  610. }
  611. /**
  612. * mei_amthif_release - the release function
  613. *
  614. * @inode: pointer to inode structure
  615. * @file: pointer to file structure
  616. *
  617. * returns 0 on success, <0 on error
  618. */
  619. int mei_amthif_release(struct mei_device *dev, struct file *file)
  620. {
  621. if (dev->open_handle_count > 0)
  622. dev->open_handle_count--;
  623. if (dev->iamthif_file_object == file &&
  624. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  625. dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
  626. dev->iamthif_state);
  627. dev->iamthif_canceled = true;
  628. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  629. dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
  630. mei_amthif_run_next_cmd(dev);
  631. }
  632. }
  633. if (mei_clear_lists(dev, file))
  634. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  635. return 0;
  636. }