amthif.c 19 KB

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