amthif.c 19 KB

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