amthif.c 19 KB

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