amthif.c 19 KB

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