amthif.c 19 KB

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