amthif.c 19 KB

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