amthif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. mei_hdr.length))
  263. return -ENODEV;
  264. if (mei_hdr.msg_complete) {
  265. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  266. return -ENODEV;
  267. dev->iamthif_flow_control_pending = true;
  268. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  269. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  270. dev->iamthif_current_cb = cb;
  271. dev->iamthif_file_object = cb->file_object;
  272. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  273. } else {
  274. dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
  275. list_add_tail(&cb->list, &dev->write_list.list);
  276. }
  277. } else {
  278. if (!(dev->mei_host_buffer_is_empty))
  279. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  280. dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
  281. list_add_tail(&cb->list, &dev->write_list.list);
  282. }
  283. return 0;
  284. }
  285. /**
  286. * mei_amthif_write - write amthif data to amthif client
  287. *
  288. * @dev: the device structure
  289. * @cb: mei call back struct
  290. *
  291. * returns 0 on success, <0 on failure.
  292. *
  293. */
  294. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
  295. {
  296. int ret;
  297. if (!dev || !cb)
  298. return -ENODEV;
  299. ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
  300. if (ret)
  301. return ret;
  302. cb->fop_type = MEI_FOP_IOCTL;
  303. if (!list_empty(&dev->amthif_cmd_list.list) ||
  304. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  305. dev_dbg(&dev->pdev->dev,
  306. "amthif state = %d\n", dev->iamthif_state);
  307. dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
  308. list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
  309. return 0;
  310. }
  311. return mei_amthif_send_cmd(dev, cb);
  312. }
  313. /**
  314. * mei_amthif_run_next_cmd
  315. *
  316. * @dev: the device structure
  317. *
  318. * returns 0 on success, <0 on failure.
  319. */
  320. void mei_amthif_run_next_cmd(struct mei_device *dev)
  321. {
  322. struct mei_cl_cb *pos = NULL;
  323. struct mei_cl_cb *next = NULL;
  324. int status;
  325. if (!dev)
  326. return;
  327. dev->iamthif_msg_buf_size = 0;
  328. dev->iamthif_msg_buf_index = 0;
  329. dev->iamthif_canceled = false;
  330. dev->iamthif_ioctl = true;
  331. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  332. dev->iamthif_timer = 0;
  333. dev->iamthif_file_object = NULL;
  334. dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
  335. list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
  336. list_del(&pos->list);
  337. if (pos->cl && pos->cl == &dev->iamthif_cl) {
  338. status = mei_amthif_send_cmd(dev, pos);
  339. if (status) {
  340. dev_dbg(&dev->pdev->dev,
  341. "amthi write failed status = %d\n",
  342. status);
  343. return;
  344. }
  345. break;
  346. }
  347. }
  348. }
  349. unsigned int mei_amthif_poll(struct mei_device *dev,
  350. struct file *file, poll_table *wait)
  351. {
  352. unsigned int mask = 0;
  353. mutex_unlock(&dev->device_lock);
  354. poll_wait(file, &dev->iamthif_cl.wait, wait);
  355. mutex_lock(&dev->device_lock);
  356. 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 amthi cb\n");
  360. mei_amthif_run_next_cmd(dev);
  361. }
  362. return mask;
  363. }
  364. /**
  365. * mei_amthif_irq_process_completed - processes completed iamthif operation.
  366. *
  367. * @dev: the device structure.
  368. * @slots: free slots.
  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_complete(struct mei_device *dev, s32 *slots,
  376. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
  377. {
  378. struct mei_msg_hdr *mei_hdr;
  379. struct mei_cl *cl = cb->cl;
  380. size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
  381. size_t msg_slots = mei_data2slots(len);
  382. mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
  383. mei_hdr->host_addr = cl->host_client_id;
  384. mei_hdr->me_addr = cl->me_client_id;
  385. mei_hdr->reserved = 0;
  386. if (*slots >= msg_slots) {
  387. mei_hdr->length = len;
  388. mei_hdr->msg_complete = 1;
  389. /* Split the message only if we can write the whole host buffer */
  390. } else if (*slots == dev->hbuf_depth) {
  391. msg_slots = *slots;
  392. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  393. mei_hdr->length = len;
  394. mei_hdr->msg_complete = 0;
  395. } else {
  396. /* wait for next time the host buffer is empty */
  397. return 0;
  398. }
  399. dev_dbg(&dev->pdev->dev, "msg: len = %d complete = %d\n",
  400. mei_hdr->length, mei_hdr->msg_complete);
  401. *slots -= msg_slots;
  402. if (mei_write_message(dev, mei_hdr,
  403. dev->iamthif_msg_buf + dev->iamthif_msg_buf_index,
  404. mei_hdr->length)) {
  405. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  406. cl->status = -ENODEV;
  407. list_del(&cb->list);
  408. return -ENODEV;
  409. }
  410. if (mei_flow_ctrl_reduce(dev, cl))
  411. return -ENODEV;
  412. dev->iamthif_msg_buf_index += mei_hdr->length;
  413. cl->status = 0;
  414. if (mei_hdr->msg_complete) {
  415. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  416. dev->iamthif_flow_control_pending = true;
  417. /* save iamthif cb sent to amthi client */
  418. cb->buf_idx = dev->iamthif_msg_buf_index;
  419. dev->iamthif_current_cb = cb;
  420. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  421. }
  422. return 0;
  423. }
  424. /**
  425. * mei_amthif_irq_read_message - read routine after ISR to
  426. * handle the read amthi message
  427. *
  428. * @complete_list: An instance of our list structure
  429. * @dev: the device structure
  430. * @mei_hdr: header of amthi message
  431. *
  432. * returns 0 on success, <0 on failure.
  433. */
  434. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  435. struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
  436. {
  437. struct mei_cl_cb *cb;
  438. unsigned char *buffer;
  439. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  440. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  441. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  442. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  443. mei_read_slots(dev, buffer, mei_hdr->length);
  444. dev->iamthif_msg_buf_index += mei_hdr->length;
  445. if (!mei_hdr->msg_complete)
  446. return 0;
  447. dev_dbg(&dev->pdev->dev,
  448. "amthi_message_buffer_index =%d\n",
  449. mei_hdr->length);
  450. dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
  451. if (!dev->iamthif_current_cb)
  452. return -ENODEV;
  453. cb = dev->iamthif_current_cb;
  454. dev->iamthif_current_cb = NULL;
  455. if (!cb->cl)
  456. return -ENODEV;
  457. dev->iamthif_stall_timer = 0;
  458. cb->buf_idx = dev->iamthif_msg_buf_index;
  459. cb->read_time = jiffies;
  460. if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
  461. /* found the iamthif cb */
  462. dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
  463. dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
  464. list_add_tail(&cb->list, &complete_list->list);
  465. }
  466. return 0;
  467. }
  468. /**
  469. * mei_amthif_irq_read - prepares to read amthif data.
  470. *
  471. * @dev: the device structure.
  472. * @slots: free slots.
  473. *
  474. * returns 0, OK; otherwise, error.
  475. */
  476. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
  477. {
  478. if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
  479. + sizeof(struct hbm_flow_control))) {
  480. return -EMSGSIZE;
  481. }
  482. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  483. if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
  484. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  485. return -EIO;
  486. }
  487. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  488. dev->iamthif_state = MEI_IAMTHIF_READING;
  489. dev->iamthif_flow_control_pending = false;
  490. dev->iamthif_msg_buf_index = 0;
  491. dev->iamthif_msg_buf_size = 0;
  492. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  493. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  494. return 0;
  495. }
  496. /**
  497. * mei_amthif_complete - complete amthif callback.
  498. *
  499. * @dev: the device structure.
  500. * @cb_pos: callback block.
  501. */
  502. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  503. {
  504. if (dev->iamthif_canceled != 1) {
  505. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  506. dev->iamthif_stall_timer = 0;
  507. memcpy(cb->response_buffer.data,
  508. dev->iamthif_msg_buf,
  509. dev->iamthif_msg_buf_index);
  510. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  511. dev_dbg(&dev->pdev->dev, "amthi read completed\n");
  512. dev->iamthif_timer = jiffies;
  513. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  514. dev->iamthif_timer);
  515. } else {
  516. mei_amthif_run_next_cmd(dev);
  517. }
  518. dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
  519. wake_up_interruptible(&dev->iamthif_cl.wait);
  520. }
  521. /**
  522. * mei_clear_list - removes all callbacks associated with file
  523. * from mei_cb_list
  524. *
  525. * @dev: device structure.
  526. * @file: file structure
  527. * @mei_cb_list: callbacks list
  528. *
  529. * mei_clear_list is called to clear resources associated with file
  530. * when application calls close function or Ctrl-C was pressed
  531. *
  532. * returns true if callback removed from the list, false otherwise
  533. */
  534. static bool mei_clear_list(struct mei_device *dev,
  535. const struct file *file, struct list_head *mei_cb_list)
  536. {
  537. struct mei_cl_cb *cb_pos = NULL;
  538. struct mei_cl_cb *cb_next = NULL;
  539. bool removed = false;
  540. /* list all list member */
  541. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
  542. /* check if list member associated with a file */
  543. if (file == cb_pos->file_object) {
  544. /* remove member from the list */
  545. list_del(&cb_pos->list);
  546. /* check if cb equal to current iamthif cb */
  547. if (dev->iamthif_current_cb == cb_pos) {
  548. dev->iamthif_current_cb = NULL;
  549. /* send flow control to iamthif client */
  550. mei_send_flow_control(dev, &dev->iamthif_cl);
  551. }
  552. /* free all allocated buffers */
  553. mei_io_cb_free(cb_pos);
  554. cb_pos = NULL;
  555. removed = true;
  556. }
  557. }
  558. return removed;
  559. }
  560. /**
  561. * mei_clear_lists - removes all callbacks associated with file
  562. *
  563. * @dev: device structure
  564. * @file: file structure
  565. *
  566. * mei_clear_lists is called to clear resources associated with file
  567. * when application calls close function or Ctrl-C was pressed
  568. *
  569. * returns true if callback removed from the list, false otherwise
  570. */
  571. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  572. {
  573. bool removed = false;
  574. /* remove callbacks associated with a file */
  575. mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
  576. if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
  577. removed = true;
  578. mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
  579. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
  580. removed = true;
  581. if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
  582. removed = true;
  583. if (mei_clear_list(dev, file, &dev->write_list.list))
  584. removed = true;
  585. /* check if iamthif_current_cb not NULL */
  586. if (dev->iamthif_current_cb && !removed) {
  587. /* check file and iamthif current cb association */
  588. if (dev->iamthif_current_cb->file_object == file) {
  589. /* remove cb */
  590. mei_io_cb_free(dev->iamthif_current_cb);
  591. dev->iamthif_current_cb = NULL;
  592. removed = true;
  593. }
  594. }
  595. return removed;
  596. }
  597. /**
  598. * mei_amthif_release - the release function
  599. *
  600. * @inode: pointer to inode structure
  601. * @file: pointer to file structure
  602. *
  603. * returns 0 on success, <0 on error
  604. */
  605. int mei_amthif_release(struct mei_device *dev, struct file *file)
  606. {
  607. if (dev->open_handle_count > 0)
  608. dev->open_handle_count--;
  609. if (dev->iamthif_file_object == file &&
  610. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  611. dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
  612. dev->iamthif_state);
  613. dev->iamthif_canceled = true;
  614. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  615. dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
  616. mei_amthif_run_next_cmd(dev);
  617. }
  618. }
  619. if (mei_clear_lists(dev, file))
  620. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  621. return 0;
  622. }