amthif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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/ioctl.h>
  24. #include <linux/cdev.h>
  25. #include <linux/list.h>
  26. #include <linux/delay.h>
  27. #include <linux/sched.h>
  28. #include <linux/uuid.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/mei.h>
  32. #include "mei_dev.h"
  33. #include "hbm.h"
  34. #include "hw-me.h"
  35. #include "client.h"
  36. const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
  37. 0xac, 0xa8, 0x46, 0xe0,
  38. 0xff, 0x65, 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. dev->iamthif_stall_timer = 0;
  55. dev->iamthif_open_count = 0;
  56. }
  57. /**
  58. * mei_amthif_host_init - mei initialization amthif client.
  59. *
  60. * @dev: the device structure
  61. *
  62. */
  63. int mei_amthif_host_init(struct mei_device *dev)
  64. {
  65. struct mei_cl *cl = &dev->iamthif_cl;
  66. unsigned char *msg_buf;
  67. int ret, i;
  68. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  69. mei_cl_init(cl, dev);
  70. i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
  71. if (i < 0) {
  72. ret = i;
  73. dev_info(&dev->pdev->dev,
  74. "amthif: failed to find the client %d\n", ret);
  75. return ret;
  76. }
  77. cl->me_client_id = dev->me_clients[i].client_id;
  78. /* Assign iamthif_mtu to the value received from ME */
  79. dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
  80. dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
  81. dev->me_clients[i].props.max_msg_length);
  82. kfree(dev->iamthif_msg_buf);
  83. dev->iamthif_msg_buf = NULL;
  84. /* allocate storage for ME message buffer */
  85. msg_buf = kcalloc(dev->iamthif_mtu,
  86. sizeof(unsigned char), GFP_KERNEL);
  87. if (!msg_buf) {
  88. dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
  89. return -ENOMEM;
  90. }
  91. dev->iamthif_msg_buf = msg_buf;
  92. ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
  93. if (ret < 0) {
  94. dev_err(&dev->pdev->dev,
  95. "amthif: failed link client %d\n", ret);
  96. return ret;
  97. }
  98. cl->state = MEI_FILE_CONNECTING;
  99. if (mei_hbm_cl_connect_req(dev, cl)) {
  100. dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
  101. cl->state = MEI_FILE_DISCONNECTED;
  102. cl->host_client_id = 0;
  103. } else {
  104. cl->timer_count = MEI_CONNECT_TIMEOUT;
  105. }
  106. return 0;
  107. }
  108. /**
  109. * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
  110. *
  111. * @dev: the device structure
  112. * @file: pointer to file object
  113. *
  114. * returns returned a list entry on success, NULL on failure.
  115. */
  116. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  117. struct file *file)
  118. {
  119. struct mei_cl_cb *pos = NULL;
  120. struct mei_cl_cb *next = NULL;
  121. list_for_each_entry_safe(pos, next,
  122. &dev->amthif_rd_complete_list.list, list) {
  123. if (pos->cl && pos->cl == &dev->iamthif_cl &&
  124. pos->file_object == file)
  125. return pos;
  126. }
  127. return NULL;
  128. }
  129. /**
  130. * mei_amthif_read - read data from AMTHIF client
  131. *
  132. * @dev: the device structure
  133. * @if_num: minor number
  134. * @file: pointer to file object
  135. * @*ubuf: pointer to user data in user space
  136. * @length: data length to read
  137. * @offset: data read offset
  138. *
  139. * Locking: called under "dev->device_lock" lock
  140. *
  141. * returns
  142. * returned data length on success,
  143. * zero if no data to read,
  144. * negative on failure.
  145. */
  146. int mei_amthif_read(struct mei_device *dev, struct file *file,
  147. char __user *ubuf, size_t length, loff_t *offset)
  148. {
  149. int rets;
  150. int wait_ret;
  151. struct mei_cl_cb *cb = NULL;
  152. struct mei_cl *cl = file->private_data;
  153. unsigned long timeout;
  154. int i;
  155. /* Only possible if we are in timeout */
  156. if (!cl || cl != &dev->iamthif_cl) {
  157. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  158. return -ETIMEDOUT;
  159. }
  160. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  161. if (i < 0) {
  162. dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
  163. return -ENODEV;
  164. }
  165. dev_dbg(&dev->pdev->dev, "checking amthif data\n");
  166. cb = mei_amthif_find_read_list_entry(dev, file);
  167. /* Check for if we can block or not*/
  168. if (cb == NULL && file->f_flags & O_NONBLOCK)
  169. return -EAGAIN;
  170. dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
  171. while (cb == NULL) {
  172. /* unlock the Mutex */
  173. mutex_unlock(&dev->device_lock);
  174. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  175. (cb = mei_amthif_find_read_list_entry(dev, file)));
  176. /* Locking again the Mutex */
  177. mutex_lock(&dev->device_lock);
  178. if (wait_ret)
  179. return -ERESTARTSYS;
  180. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  181. }
  182. dev_dbg(&dev->pdev->dev, "Got amthif data\n");
  183. dev->iamthif_timer = 0;
  184. if (cb) {
  185. timeout = cb->read_time +
  186. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  187. dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
  188. timeout);
  189. if (time_after(jiffies, timeout)) {
  190. dev_dbg(&dev->pdev->dev, "amthif Time out\n");
  191. /* 15 sec for the message has expired */
  192. list_del(&cb->list);
  193. rets = -ETIMEDOUT;
  194. goto free;
  195. }
  196. }
  197. /* if the whole message will fit remove it from the list */
  198. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  199. list_del(&cb->list);
  200. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  201. /* end of the message has been reached */
  202. list_del(&cb->list);
  203. rets = 0;
  204. goto free;
  205. }
  206. /* else means that not full buffer will be read and do not
  207. * remove message from deletion list
  208. */
  209. dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
  210. cb->response_buffer.size);
  211. dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
  212. /* length is being truncated to PAGE_SIZE, however,
  213. * the buf_idx may point beyond */
  214. length = min_t(size_t, length, (cb->buf_idx - *offset));
  215. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  216. rets = -EFAULT;
  217. else {
  218. rets = length;
  219. if ((*offset + length) < cb->buf_idx) {
  220. *offset += length;
  221. goto out;
  222. }
  223. }
  224. free:
  225. dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
  226. *offset = 0;
  227. mei_io_cb_free(cb);
  228. out:
  229. return rets;
  230. }
  231. /**
  232. * mei_amthif_send_cmd - send amthif command to the ME
  233. *
  234. * @dev: the device structure
  235. * @cb: mei call back struct
  236. *
  237. * returns 0 on success, <0 on failure.
  238. *
  239. */
  240. static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
  241. {
  242. struct mei_msg_hdr mei_hdr;
  243. int ret;
  244. if (!dev || !cb)
  245. return -ENODEV;
  246. dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
  247. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  248. dev->iamthif_current_cb = cb;
  249. dev->iamthif_file_object = cb->file_object;
  250. dev->iamthif_canceled = false;
  251. dev->iamthif_ioctl = true;
  252. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  253. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  254. cb->request_buffer.size);
  255. ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
  256. if (ret < 0)
  257. return ret;
  258. if (ret && dev->hbuf_is_ready) {
  259. ret = 0;
  260. dev->hbuf_is_ready = false;
  261. if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
  262. mei_hdr.length = mei_hbuf_max_len(dev);
  263. mei_hdr.msg_complete = 0;
  264. } else {
  265. mei_hdr.length = cb->request_buffer.size;
  266. mei_hdr.msg_complete = 1;
  267. }
  268. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  269. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  270. mei_hdr.reserved = 0;
  271. mei_hdr.internal = 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_WRITE;
  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. mei_hdr.internal = 0;
  408. if (*slots >= msg_slots) {
  409. mei_hdr.length = len;
  410. mei_hdr.msg_complete = 1;
  411. /* Split the message only if we can write the whole host buffer */
  412. } else if (*slots == dev->hbuf_depth) {
  413. msg_slots = *slots;
  414. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  415. mei_hdr.length = len;
  416. mei_hdr.msg_complete = 0;
  417. } else {
  418. /* wait for next time the host buffer is empty */
  419. return 0;
  420. }
  421. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
  422. *slots -= msg_slots;
  423. rets = mei_write_message(dev, &mei_hdr,
  424. dev->iamthif_msg_buf + dev->iamthif_msg_buf_index);
  425. if (rets) {
  426. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  427. cl->status = rets;
  428. list_del(&cb->list);
  429. return rets;
  430. }
  431. if (mei_cl_flow_ctrl_reduce(cl))
  432. return -EIO;
  433. dev->iamthif_msg_buf_index += mei_hdr.length;
  434. cl->status = 0;
  435. if (mei_hdr.msg_complete) {
  436. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  437. dev->iamthif_flow_control_pending = true;
  438. /* save iamthif cb sent to amthif client */
  439. cb->buf_idx = dev->iamthif_msg_buf_index;
  440. dev->iamthif_current_cb = cb;
  441. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  442. }
  443. return 0;
  444. }
  445. /**
  446. * mei_amthif_irq_read_message - read routine after ISR to
  447. * handle the read amthif message
  448. *
  449. * @dev: the device structure
  450. * @mei_hdr: header of amthif message
  451. * @complete_list: An instance of our list structure
  452. *
  453. * returns 0 on success, <0 on failure.
  454. */
  455. int mei_amthif_irq_read_msg(struct mei_device *dev,
  456. struct mei_msg_hdr *mei_hdr,
  457. struct mei_cl_cb *complete_list)
  458. {
  459. struct mei_cl_cb *cb;
  460. unsigned char *buffer;
  461. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  462. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  463. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  464. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  465. mei_read_slots(dev, buffer, mei_hdr->length);
  466. dev->iamthif_msg_buf_index += mei_hdr->length;
  467. if (!mei_hdr->msg_complete)
  468. return 0;
  469. dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
  470. mei_hdr->length);
  471. dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
  472. if (!dev->iamthif_current_cb)
  473. return -ENODEV;
  474. cb = dev->iamthif_current_cb;
  475. dev->iamthif_current_cb = NULL;
  476. if (!cb->cl)
  477. return -ENODEV;
  478. dev->iamthif_stall_timer = 0;
  479. cb->buf_idx = dev->iamthif_msg_buf_index;
  480. cb->read_time = jiffies;
  481. if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
  482. /* found the iamthif cb */
  483. dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
  484. dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
  485. list_add_tail(&cb->list, &complete_list->list);
  486. }
  487. return 0;
  488. }
  489. /**
  490. * mei_amthif_irq_read - prepares to read amthif data.
  491. *
  492. * @dev: the device structure.
  493. * @slots: free slots.
  494. *
  495. * returns 0, OK; otherwise, error.
  496. */
  497. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
  498. {
  499. u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
  500. if (*slots < msg_slots)
  501. return -EMSGSIZE;
  502. *slots -= msg_slots;
  503. if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
  504. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  505. return -EIO;
  506. }
  507. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  508. dev->iamthif_state = MEI_IAMTHIF_READING;
  509. dev->iamthif_flow_control_pending = false;
  510. dev->iamthif_msg_buf_index = 0;
  511. dev->iamthif_msg_buf_size = 0;
  512. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  513. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  514. return 0;
  515. }
  516. /**
  517. * mei_amthif_complete - complete amthif callback.
  518. *
  519. * @dev: the device structure.
  520. * @cb_pos: callback block.
  521. */
  522. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  523. {
  524. if (dev->iamthif_canceled != 1) {
  525. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  526. dev->iamthif_stall_timer = 0;
  527. memcpy(cb->response_buffer.data,
  528. dev->iamthif_msg_buf,
  529. dev->iamthif_msg_buf_index);
  530. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  531. dev_dbg(&dev->pdev->dev, "amthif read completed\n");
  532. dev->iamthif_timer = jiffies;
  533. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  534. dev->iamthif_timer);
  535. } else {
  536. mei_amthif_run_next_cmd(dev);
  537. }
  538. dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
  539. wake_up_interruptible(&dev->iamthif_cl.wait);
  540. }
  541. /**
  542. * mei_clear_list - removes all callbacks associated with file
  543. * from mei_cb_list
  544. *
  545. * @dev: device structure.
  546. * @file: file structure
  547. * @mei_cb_list: callbacks list
  548. *
  549. * mei_clear_list is called to clear resources associated with file
  550. * when application calls close function or Ctrl-C was pressed
  551. *
  552. * returns true if callback removed from the list, false otherwise
  553. */
  554. static bool mei_clear_list(struct mei_device *dev,
  555. const struct file *file, struct list_head *mei_cb_list)
  556. {
  557. struct mei_cl_cb *cb_pos = NULL;
  558. struct mei_cl_cb *cb_next = NULL;
  559. bool removed = false;
  560. /* list all list member */
  561. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
  562. /* check if list member associated with a file */
  563. if (file == cb_pos->file_object) {
  564. /* remove member from the list */
  565. list_del(&cb_pos->list);
  566. /* check if cb equal to current iamthif cb */
  567. if (dev->iamthif_current_cb == cb_pos) {
  568. dev->iamthif_current_cb = NULL;
  569. /* send flow control to iamthif client */
  570. mei_hbm_cl_flow_control_req(dev,
  571. &dev->iamthif_cl);
  572. }
  573. /* free all allocated buffers */
  574. mei_io_cb_free(cb_pos);
  575. cb_pos = NULL;
  576. removed = true;
  577. }
  578. }
  579. return removed;
  580. }
  581. /**
  582. * mei_clear_lists - removes all callbacks associated with file
  583. *
  584. * @dev: device structure
  585. * @file: file structure
  586. *
  587. * mei_clear_lists is called to clear resources associated with file
  588. * when application calls close function or Ctrl-C was pressed
  589. *
  590. * returns true if callback removed from the list, false otherwise
  591. */
  592. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  593. {
  594. bool removed = false;
  595. /* remove callbacks associated with a file */
  596. mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
  597. if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
  598. removed = true;
  599. mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
  600. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
  601. removed = true;
  602. if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
  603. removed = true;
  604. if (mei_clear_list(dev, file, &dev->write_list.list))
  605. removed = true;
  606. /* check if iamthif_current_cb not NULL */
  607. if (dev->iamthif_current_cb && !removed) {
  608. /* check file and iamthif current cb association */
  609. if (dev->iamthif_current_cb->file_object == file) {
  610. /* remove cb */
  611. mei_io_cb_free(dev->iamthif_current_cb);
  612. dev->iamthif_current_cb = NULL;
  613. removed = true;
  614. }
  615. }
  616. return removed;
  617. }
  618. /**
  619. * mei_amthif_release - the release function
  620. *
  621. * @dev: device structure
  622. * @file: pointer to file structure
  623. *
  624. * returns 0 on success, <0 on error
  625. */
  626. int mei_amthif_release(struct mei_device *dev, struct file *file)
  627. {
  628. if (dev->iamthif_open_count > 0)
  629. dev->iamthif_open_count--;
  630. if (dev->iamthif_file_object == file &&
  631. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  632. dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
  633. dev->iamthif_state);
  634. dev->iamthif_canceled = true;
  635. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  636. dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
  637. mei_amthif_run_next_cmd(dev);
  638. }
  639. }
  640. if (mei_clear_lists(dev, file))
  641. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  642. return 0;
  643. }