amthif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/aio.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/cdev.h>
  26. #include <linux/list.h>
  27. #include <linux/delay.h>
  28. #include <linux/sched.h>
  29. #include <linux/uuid.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/uaccess.h>
  32. #include "mei_dev.h"
  33. #include "hw.h"
  34. #include <linux/mei.h>
  35. #include "interface.h"
  36. const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
  37. 0xa8, 0x46, 0xe0, 0xff, 0x65,
  38. 0x81, 0x4c);
  39. /**
  40. * mei_amthif_reset_params - initializes mei device iamthif
  41. *
  42. * @dev: the device structure
  43. */
  44. void mei_amthif_reset_params(struct mei_device *dev)
  45. {
  46. /* reset iamthif parameters. */
  47. dev->iamthif_current_cb = NULL;
  48. dev->iamthif_msg_buf_size = 0;
  49. dev->iamthif_msg_buf_index = 0;
  50. dev->iamthif_canceled = false;
  51. dev->iamthif_ioctl = false;
  52. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  53. dev->iamthif_timer = 0;
  54. }
  55. /**
  56. * mei_amthif_host_init_ - mei initialization amthif client.
  57. *
  58. * @dev: the device structure
  59. *
  60. */
  61. void mei_amthif_host_init(struct mei_device *dev)
  62. {
  63. int i;
  64. unsigned char *msg_buf;
  65. mei_cl_init(&dev->iamthif_cl, dev);
  66. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  67. /* find ME amthi client */
  68. i = mei_me_cl_link(dev, &dev->iamthif_cl,
  69. &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
  70. if (i < 0) {
  71. dev_info(&dev->pdev->dev, "failed to find iamthif client.\n");
  72. return;
  73. }
  74. /* Assign iamthif_mtu to the value received from ME */
  75. dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
  76. dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
  77. dev->me_clients[i].props.max_msg_length);
  78. kfree(dev->iamthif_msg_buf);
  79. dev->iamthif_msg_buf = NULL;
  80. /* allocate storage for ME message buffer */
  81. msg_buf = kcalloc(dev->iamthif_mtu,
  82. sizeof(unsigned char), GFP_KERNEL);
  83. if (!msg_buf) {
  84. dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
  85. return;
  86. }
  87. dev->iamthif_msg_buf = msg_buf;
  88. if (mei_connect(dev, &dev->iamthif_cl)) {
  89. dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
  90. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  91. dev->iamthif_cl.host_client_id = 0;
  92. } else {
  93. dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT;
  94. }
  95. }
  96. /**
  97. * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
  98. *
  99. * @dev: the device structure
  100. * @file: pointer to file object
  101. *
  102. * returns returned a list entry on success, NULL on failure.
  103. */
  104. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  105. struct file *file)
  106. {
  107. struct mei_cl_cb *pos = NULL;
  108. struct mei_cl_cb *next = NULL;
  109. list_for_each_entry_safe(pos, next,
  110. &dev->amthif_rd_complete_list.list, list) {
  111. if (pos->cl && pos->cl == &dev->iamthif_cl &&
  112. pos->file_object == file)
  113. return pos;
  114. }
  115. return NULL;
  116. }
  117. /**
  118. * mei_amthif_read - read data from AMTHIF client
  119. *
  120. * @dev: the device structure
  121. * @if_num: minor number
  122. * @file: pointer to file object
  123. * @*ubuf: pointer to user data in user space
  124. * @length: data length to read
  125. * @offset: data read offset
  126. *
  127. * Locking: called under "dev->device_lock" lock
  128. *
  129. * returns
  130. * returned data length on success,
  131. * zero if no data to read,
  132. * negative on failure.
  133. */
  134. int mei_amthif_read(struct mei_device *dev, struct file *file,
  135. char __user *ubuf, size_t length, loff_t *offset)
  136. {
  137. int rets;
  138. int wait_ret;
  139. struct mei_cl_cb *cb = NULL;
  140. struct mei_cl *cl = file->private_data;
  141. unsigned long timeout;
  142. int i;
  143. /* Only Posible if we are in timeout */
  144. if (!cl || cl != &dev->iamthif_cl) {
  145. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  146. return -ETIMEDOUT;
  147. }
  148. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  149. if (i < 0) {
  150. dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
  151. return -ENODEV;
  152. }
  153. dev_dbg(&dev->pdev->dev, "checking amthi data\n");
  154. cb = mei_amthif_find_read_list_entry(dev, file);
  155. /* Check for if we can block or not*/
  156. if (cb == NULL && file->f_flags & O_NONBLOCK)
  157. return -EAGAIN;
  158. dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
  159. while (cb == NULL) {
  160. /* unlock the Mutex */
  161. mutex_unlock(&dev->device_lock);
  162. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  163. (cb = mei_amthif_find_read_list_entry(dev, file)));
  164. if (wait_ret)
  165. return -ERESTARTSYS;
  166. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  167. /* Locking again the Mutex */
  168. mutex_lock(&dev->device_lock);
  169. }
  170. dev_dbg(&dev->pdev->dev, "Got amthi data\n");
  171. dev->iamthif_timer = 0;
  172. if (cb) {
  173. timeout = cb->read_time +
  174. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  175. dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
  176. timeout);
  177. if (time_after(jiffies, timeout)) {
  178. dev_dbg(&dev->pdev->dev, "amthi Time out\n");
  179. /* 15 sec for the message has expired */
  180. list_del(&cb->list);
  181. rets = -ETIMEDOUT;
  182. goto free;
  183. }
  184. }
  185. /* if the whole message will fit remove it from the list */
  186. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  187. list_del(&cb->list);
  188. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  189. /* end of the message has been reached */
  190. list_del(&cb->list);
  191. rets = 0;
  192. goto free;
  193. }
  194. /* else means that not full buffer will be read and do not
  195. * remove message from deletion list
  196. */
  197. dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
  198. cb->response_buffer.size);
  199. dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
  200. /* length is being turncated to PAGE_SIZE, however,
  201. * the buf_idx may point beyond */
  202. length = min_t(size_t, length, (cb->buf_idx - *offset));
  203. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  204. rets = -EFAULT;
  205. else {
  206. rets = length;
  207. if ((*offset + length) < cb->buf_idx) {
  208. *offset += length;
  209. goto out;
  210. }
  211. }
  212. free:
  213. dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
  214. *offset = 0;
  215. mei_io_cb_free(cb);
  216. out:
  217. return rets;
  218. }
  219. /**
  220. * mei_amthif_send_cmd - send amthif command to the ME
  221. *
  222. * @dev: the device structure
  223. * @cb: mei call back struct
  224. *
  225. * returns 0 on success, <0 on failure.
  226. *
  227. */
  228. static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
  229. {
  230. struct mei_msg_hdr mei_hdr;
  231. int ret;
  232. if (!dev || !cb)
  233. return -ENODEV;
  234. dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
  235. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  236. dev->iamthif_current_cb = cb;
  237. dev->iamthif_file_object = cb->file_object;
  238. dev->iamthif_canceled = false;
  239. dev->iamthif_ioctl = true;
  240. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  241. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  242. cb->request_buffer.size);
  243. ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
  244. if (ret < 0)
  245. return ret;
  246. if (ret && dev->mei_host_buffer_is_empty) {
  247. ret = 0;
  248. dev->mei_host_buffer_is_empty = false;
  249. if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
  250. mei_hdr.length = mei_hbuf_max_data(dev);
  251. mei_hdr.msg_complete = 0;
  252. } else {
  253. mei_hdr.length = cb->request_buffer.size;
  254. mei_hdr.msg_complete = 1;
  255. }
  256. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  257. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  258. mei_hdr.reserved = 0;
  259. dev->iamthif_msg_buf_index += mei_hdr.length;
  260. if (mei_write_message(dev, &mei_hdr,
  261. (unsigned char *)dev->iamthif_msg_buf))
  262. return -ENODEV;
  263. if (mei_hdr.msg_complete) {
  264. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  265. return -ENODEV;
  266. dev->iamthif_flow_control_pending = true;
  267. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  268. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  269. dev->iamthif_current_cb = cb;
  270. dev->iamthif_file_object = cb->file_object;
  271. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  272. } else {
  273. dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
  274. list_add_tail(&cb->list, &dev->write_list.list);
  275. }
  276. } else {
  277. if (!(dev->mei_host_buffer_is_empty))
  278. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  279. dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
  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_IOCTL;
  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 amthi 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. "amthi 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. mutex_unlock(&dev->device_lock);
  353. poll_wait(file, &dev->iamthif_cl.wait, wait);
  354. mutex_lock(&dev->device_lock);
  355. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
  356. dev->iamthif_file_object == file) {
  357. mask |= (POLLIN | POLLRDNORM);
  358. dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
  359. mei_amthif_run_next_cmd(dev);
  360. }
  361. return mask;
  362. }
  363. /**
  364. * mei_amthif_irq_process_completed - processes completed iamthif operation.
  365. *
  366. * @dev: the device structure.
  367. * @slots: free slots.
  368. * @cb_pos: callback block.
  369. * @cl: private data of the file object.
  370. * @cmpl_list: complete list.
  371. *
  372. * returns 0, OK; otherwise, error.
  373. */
  374. int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
  375. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
  376. {
  377. struct mei_msg_hdr *mei_hdr;
  378. struct mei_cl *cl = cb->cl;
  379. size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
  380. size_t msg_slots = mei_data2slots(len);
  381. mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
  382. mei_hdr->host_addr = cl->host_client_id;
  383. mei_hdr->me_addr = cl->me_client_id;
  384. mei_hdr->reserved = 0;
  385. if (*slots >= msg_slots) {
  386. mei_hdr->length = len;
  387. mei_hdr->msg_complete = 1;
  388. /* Split the message only if we can write the whole host buffer */
  389. } else if (*slots == dev->hbuf_depth) {
  390. msg_slots = *slots;
  391. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  392. mei_hdr->length = len;
  393. mei_hdr->msg_complete = 0;
  394. } else {
  395. /* wait for next time the host buffer is empty */
  396. return 0;
  397. }
  398. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  399. *slots -= msg_slots;
  400. if (mei_write_message(dev, mei_hdr,
  401. dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) {
  402. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  403. cl->status = -ENODEV;
  404. list_del(&cb->list);
  405. return -ENODEV;
  406. }
  407. if (mei_flow_ctrl_reduce(dev, cl))
  408. return -ENODEV;
  409. dev->iamthif_msg_buf_index += mei_hdr->length;
  410. cl->status = 0;
  411. if (mei_hdr->msg_complete) {
  412. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  413. dev->iamthif_flow_control_pending = true;
  414. /* save iamthif cb sent to amthi client */
  415. cb->buf_idx = dev->iamthif_msg_buf_index;
  416. dev->iamthif_current_cb = cb;
  417. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  418. }
  419. return 0;
  420. }
  421. /**
  422. * mei_amthif_irq_read_message - read routine after ISR to
  423. * handle the read amthi message
  424. *
  425. * @complete_list: An instance of our list structure
  426. * @dev: the device structure
  427. * @mei_hdr: header of amthi message
  428. *
  429. * returns 0 on success, <0 on failure.
  430. */
  431. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  432. struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
  433. {
  434. struct mei_cl_cb *cb;
  435. unsigned char *buffer;
  436. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  437. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  438. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  439. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  440. mei_read_slots(dev, buffer, mei_hdr->length);
  441. dev->iamthif_msg_buf_index += mei_hdr->length;
  442. if (!mei_hdr->msg_complete)
  443. return 0;
  444. dev_dbg(&dev->pdev->dev,
  445. "amthi_message_buffer_index =%d\n",
  446. mei_hdr->length);
  447. dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
  448. if (!dev->iamthif_current_cb)
  449. return -ENODEV;
  450. cb = dev->iamthif_current_cb;
  451. dev->iamthif_current_cb = NULL;
  452. if (!cb->cl)
  453. return -ENODEV;
  454. dev->iamthif_stall_timer = 0;
  455. cb->buf_idx = dev->iamthif_msg_buf_index;
  456. cb->read_time = jiffies;
  457. if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
  458. /* found the iamthif cb */
  459. dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
  460. dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
  461. list_add_tail(&cb->list, &complete_list->list);
  462. }
  463. return 0;
  464. }
  465. /**
  466. * mei_amthif_irq_read - prepares to read amthif data.
  467. *
  468. * @dev: the device structure.
  469. * @slots: free slots.
  470. *
  471. * returns 0, OK; otherwise, error.
  472. */
  473. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
  474. {
  475. if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
  476. + sizeof(struct hbm_flow_control))) {
  477. return -EMSGSIZE;
  478. }
  479. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  480. if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
  481. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  482. return -EIO;
  483. }
  484. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  485. dev->iamthif_state = MEI_IAMTHIF_READING;
  486. dev->iamthif_flow_control_pending = false;
  487. dev->iamthif_msg_buf_index = 0;
  488. dev->iamthif_msg_buf_size = 0;
  489. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  490. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  491. return 0;
  492. }
  493. /**
  494. * mei_amthif_complete - complete amthif callback.
  495. *
  496. * @dev: the device structure.
  497. * @cb_pos: callback block.
  498. */
  499. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  500. {
  501. if (dev->iamthif_canceled != 1) {
  502. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  503. dev->iamthif_stall_timer = 0;
  504. memcpy(cb->response_buffer.data,
  505. dev->iamthif_msg_buf,
  506. dev->iamthif_msg_buf_index);
  507. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  508. dev_dbg(&dev->pdev->dev, "amthi read completed\n");
  509. dev->iamthif_timer = jiffies;
  510. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  511. dev->iamthif_timer);
  512. } else {
  513. mei_amthif_run_next_cmd(dev);
  514. }
  515. dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
  516. wake_up_interruptible(&dev->iamthif_cl.wait);
  517. }
  518. /**
  519. * mei_clear_list - removes all callbacks associated with file
  520. * from mei_cb_list
  521. *
  522. * @dev: device structure.
  523. * @file: file structure
  524. * @mei_cb_list: callbacks list
  525. *
  526. * mei_clear_list is called to clear resources associated with file
  527. * when application calls close function or Ctrl-C was pressed
  528. *
  529. * returns true if callback removed from the list, false otherwise
  530. */
  531. static bool mei_clear_list(struct mei_device *dev,
  532. const struct file *file, struct list_head *mei_cb_list)
  533. {
  534. struct mei_cl_cb *cb_pos = NULL;
  535. struct mei_cl_cb *cb_next = NULL;
  536. bool removed = false;
  537. /* list all list member */
  538. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
  539. /* check if list member associated with a file */
  540. if (file == cb_pos->file_object) {
  541. /* remove member from the list */
  542. list_del(&cb_pos->list);
  543. /* check if cb equal to current iamthif cb */
  544. if (dev->iamthif_current_cb == cb_pos) {
  545. dev->iamthif_current_cb = NULL;
  546. /* send flow control to iamthif client */
  547. mei_send_flow_control(dev, &dev->iamthif_cl);
  548. }
  549. /* free all allocated buffers */
  550. mei_io_cb_free(cb_pos);
  551. cb_pos = NULL;
  552. removed = true;
  553. }
  554. }
  555. return removed;
  556. }
  557. /**
  558. * mei_clear_lists - removes all callbacks associated with file
  559. *
  560. * @dev: device structure
  561. * @file: file structure
  562. *
  563. * mei_clear_lists is called to clear resources associated with file
  564. * when application calls close function or Ctrl-C was pressed
  565. *
  566. * returns true if callback removed from the list, false otherwise
  567. */
  568. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  569. {
  570. bool removed = false;
  571. /* remove callbacks associated with a file */
  572. mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
  573. if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
  574. removed = true;
  575. mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
  576. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
  577. removed = true;
  578. if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
  579. removed = true;
  580. if (mei_clear_list(dev, file, &dev->write_list.list))
  581. removed = true;
  582. /* check if iamthif_current_cb not NULL */
  583. if (dev->iamthif_current_cb && !removed) {
  584. /* check file and iamthif current cb association */
  585. if (dev->iamthif_current_cb->file_object == file) {
  586. /* remove cb */
  587. mei_io_cb_free(dev->iamthif_current_cb);
  588. dev->iamthif_current_cb = NULL;
  589. removed = true;
  590. }
  591. }
  592. return removed;
  593. }
  594. /**
  595. * mei_amthif_release - the release function
  596. *
  597. * @inode: pointer to inode structure
  598. * @file: pointer to file structure
  599. *
  600. * returns 0 on success, <0 on error
  601. */
  602. int mei_amthif_release(struct mei_device *dev, struct file *file)
  603. {
  604. if (dev->open_handle_count > 0)
  605. dev->open_handle_count--;
  606. if (dev->iamthif_file_object == file &&
  607. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  608. dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
  609. dev->iamthif_state);
  610. dev->iamthif_canceled = true;
  611. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  612. dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
  613. mei_amthif_run_next_cmd(dev);
  614. }
  615. }
  616. if (mei_clear_lists(dev, file))
  617. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  618. return 0;
  619. }