amthif.c 19 KB

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