amthif.c 18 KB

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