amthif.c 19 KB

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