amthif.c 19 KB

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