interrupt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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/export.h>
  17. #include <linux/pci.h>
  18. #include <linux/kthread.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/fs.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/mei.h>
  23. #include "mei_dev.h"
  24. #include "hbm.h"
  25. #include "hw-me.h"
  26. #include "client.h"
  27. /**
  28. * mei_cl_complete_handler - processes completed operation for a client
  29. *
  30. * @cl: private data of the file object.
  31. * @cb: callback block.
  32. */
  33. static void mei_cl_complete_handler(struct mei_cl *cl, struct mei_cl_cb *cb)
  34. {
  35. if (cb->fop_type == MEI_FOP_WRITE) {
  36. mei_io_cb_free(cb);
  37. cb = NULL;
  38. cl->writing_state = MEI_WRITE_COMPLETE;
  39. if (waitqueue_active(&cl->tx_wait))
  40. wake_up_interruptible(&cl->tx_wait);
  41. } else if (cb->fop_type == MEI_FOP_READ &&
  42. MEI_READING == cl->reading_state) {
  43. cl->reading_state = MEI_READ_COMPLETE;
  44. if (waitqueue_active(&cl->rx_wait))
  45. wake_up_interruptible(&cl->rx_wait);
  46. else
  47. mei_cl_bus_rx_event(cl);
  48. }
  49. }
  50. /**
  51. * mei_irq_compl_handler - dispatch complete handelers
  52. * for the completed callbacks
  53. *
  54. * @dev - mei device
  55. * @compl_list - list of completed cbs
  56. */
  57. void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
  58. {
  59. struct mei_cl_cb *cb, *next;
  60. struct mei_cl *cl;
  61. list_for_each_entry_safe(cb, next, &compl_list->list, list) {
  62. cl = cb->cl;
  63. list_del(&cb->list);
  64. if (!cl)
  65. continue;
  66. dev_dbg(&dev->pdev->dev, "completing call back.\n");
  67. if (cl == &dev->iamthif_cl)
  68. mei_amthif_complete(dev, cb);
  69. else
  70. mei_cl_complete_handler(cl, cb);
  71. }
  72. }
  73. EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
  74. /**
  75. * mei_cl_hbm_equal - check if hbm is addressed to the client
  76. *
  77. * @cl: host client
  78. * @mei_hdr: header of mei client message
  79. *
  80. * returns true if matches, false otherwise
  81. */
  82. static inline int mei_cl_hbm_equal(struct mei_cl *cl,
  83. struct mei_msg_hdr *mei_hdr)
  84. {
  85. return cl->host_client_id == mei_hdr->host_addr &&
  86. cl->me_client_id == mei_hdr->me_addr;
  87. }
  88. /**
  89. * mei_cl_is_reading - checks if the client
  90. is the one to read this message
  91. *
  92. * @cl: mei client
  93. * @mei_hdr: header of mei message
  94. *
  95. * returns true on match and false otherwise
  96. */
  97. static bool mei_cl_is_reading(struct mei_cl *cl, struct mei_msg_hdr *mei_hdr)
  98. {
  99. return mei_cl_hbm_equal(cl, mei_hdr) &&
  100. cl->state == MEI_FILE_CONNECTED &&
  101. cl->reading_state != MEI_READ_COMPLETE;
  102. }
  103. /**
  104. * mei_irq_read_client_message - process client message
  105. *
  106. * @dev: the device structure
  107. * @mei_hdr: header of mei client message
  108. * @complete_list: An instance of our list structure
  109. *
  110. * returns 0 on success, <0 on failure.
  111. */
  112. static int mei_cl_irq_read_msg(struct mei_device *dev,
  113. struct mei_msg_hdr *mei_hdr,
  114. struct mei_cl_cb *complete_list)
  115. {
  116. struct mei_cl *cl;
  117. struct mei_cl_cb *cb, *next;
  118. unsigned char *buffer = NULL;
  119. list_for_each_entry_safe(cb, next, &dev->read_list.list, list) {
  120. cl = cb->cl;
  121. if (!cl || !mei_cl_is_reading(cl, mei_hdr))
  122. continue;
  123. cl->reading_state = MEI_READING;
  124. if (cb->response_buffer.size == 0 ||
  125. cb->response_buffer.data == NULL) {
  126. dev_err(&dev->pdev->dev, "response buffer is not allocated.\n");
  127. list_del(&cb->list);
  128. return -ENOMEM;
  129. }
  130. if (cb->response_buffer.size < mei_hdr->length + cb->buf_idx) {
  131. dev_dbg(&dev->pdev->dev, "message overflow. size %d len %d idx %ld\n",
  132. cb->response_buffer.size,
  133. mei_hdr->length, cb->buf_idx);
  134. buffer = krealloc(cb->response_buffer.data,
  135. mei_hdr->length + cb->buf_idx,
  136. GFP_KERNEL);
  137. if (!buffer) {
  138. dev_err(&dev->pdev->dev, "allocation failed.\n");
  139. list_del(&cb->list);
  140. return -ENOMEM;
  141. }
  142. cb->response_buffer.data = buffer;
  143. cb->response_buffer.size =
  144. mei_hdr->length + cb->buf_idx;
  145. }
  146. buffer = cb->response_buffer.data + cb->buf_idx;
  147. mei_read_slots(dev, buffer, mei_hdr->length);
  148. cb->buf_idx += mei_hdr->length;
  149. if (mei_hdr->msg_complete) {
  150. cl->status = 0;
  151. list_del(&cb->list);
  152. dev_dbg(&dev->pdev->dev, "completed read H cl = %d, ME cl = %d, length = %lu\n",
  153. cl->host_client_id,
  154. cl->me_client_id,
  155. cb->buf_idx);
  156. list_add_tail(&cb->list, &complete_list->list);
  157. }
  158. break;
  159. }
  160. dev_dbg(&dev->pdev->dev, "message read\n");
  161. if (!buffer) {
  162. mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
  163. dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
  164. MEI_HDR_PRM(mei_hdr));
  165. }
  166. return 0;
  167. }
  168. /**
  169. * _mei_irq_thread_close - processes close related operation.
  170. *
  171. * @dev: the device structure.
  172. * @slots: free slots.
  173. * @cb_pos: callback block.
  174. * @cl: private data of the file object.
  175. * @cmpl_list: complete list.
  176. *
  177. * returns 0, OK; otherwise, error.
  178. */
  179. static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
  180. struct mei_cl_cb *cb_pos,
  181. struct mei_cl *cl,
  182. struct mei_cl_cb *cmpl_list)
  183. {
  184. u32 msg_slots =
  185. mei_data2slots(sizeof(struct hbm_client_connect_request));
  186. if (*slots < msg_slots)
  187. return -EMSGSIZE;
  188. *slots -= msg_slots;
  189. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  190. cl->status = 0;
  191. cb_pos->buf_idx = 0;
  192. list_move_tail(&cb_pos->list, &cmpl_list->list);
  193. return -EIO;
  194. }
  195. cl->state = MEI_FILE_DISCONNECTING;
  196. cl->status = 0;
  197. cb_pos->buf_idx = 0;
  198. list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
  199. cl->timer_count = MEI_CONNECT_TIMEOUT;
  200. return 0;
  201. }
  202. /**
  203. * _mei_irq_thread_read - processes read related operation.
  204. *
  205. * @dev: the device structure.
  206. * @slots: free slots.
  207. * @cb_pos: callback block.
  208. * @cl: private data of the file object.
  209. * @cmpl_list: complete list.
  210. *
  211. * returns 0, OK; otherwise, error.
  212. */
  213. static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
  214. struct mei_cl_cb *cb_pos,
  215. struct mei_cl *cl,
  216. struct mei_cl_cb *cmpl_list)
  217. {
  218. u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
  219. if (*slots < msg_slots) {
  220. /* return the cancel routine */
  221. list_del(&cb_pos->list);
  222. return -EMSGSIZE;
  223. }
  224. *slots -= msg_slots;
  225. if (mei_hbm_cl_flow_control_req(dev, cl)) {
  226. cl->status = -ENODEV;
  227. cb_pos->buf_idx = 0;
  228. list_move_tail(&cb_pos->list, &cmpl_list->list);
  229. return -ENODEV;
  230. }
  231. list_move_tail(&cb_pos->list, &dev->read_list.list);
  232. return 0;
  233. }
  234. /**
  235. * _mei_irq_thread_ioctl - processes ioctl related operation.
  236. *
  237. * @dev: the device structure.
  238. * @slots: free slots.
  239. * @cb_pos: callback block.
  240. * @cl: private data of the file object.
  241. * @cmpl_list: complete list.
  242. *
  243. * returns 0, OK; otherwise, error.
  244. */
  245. static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
  246. struct mei_cl_cb *cb_pos,
  247. struct mei_cl *cl,
  248. struct mei_cl_cb *cmpl_list)
  249. {
  250. u32 msg_slots =
  251. mei_data2slots(sizeof(struct hbm_client_connect_request));
  252. if (*slots < msg_slots) {
  253. /* return the cancel routine */
  254. list_del(&cb_pos->list);
  255. return -EMSGSIZE;
  256. }
  257. *slots -= msg_slots;
  258. cl->state = MEI_FILE_CONNECTING;
  259. if (mei_hbm_cl_connect_req(dev, cl)) {
  260. cl->status = -ENODEV;
  261. cb_pos->buf_idx = 0;
  262. list_del(&cb_pos->list);
  263. return -ENODEV;
  264. } else {
  265. list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
  266. cl->timer_count = MEI_CONNECT_TIMEOUT;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * mei_irq_thread_write_complete - write messages to device.
  272. *
  273. * @dev: the device structure.
  274. * @slots: free slots.
  275. * @cb: callback block.
  276. * @cmpl_list: complete list.
  277. *
  278. * returns 0, OK; otherwise, error.
  279. */
  280. static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
  281. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
  282. {
  283. struct mei_msg_hdr mei_hdr;
  284. struct mei_cl *cl = cb->cl;
  285. size_t len = cb->request_buffer.size - cb->buf_idx;
  286. u32 msg_slots = mei_data2slots(len);
  287. mei_hdr.host_addr = cl->host_client_id;
  288. mei_hdr.me_addr = cl->me_client_id;
  289. mei_hdr.reserved = 0;
  290. if (*slots >= msg_slots) {
  291. mei_hdr.length = len;
  292. mei_hdr.msg_complete = 1;
  293. /* Split the message only if we can write the whole host buffer */
  294. } else if (*slots == dev->hbuf_depth) {
  295. msg_slots = *slots;
  296. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  297. mei_hdr.length = len;
  298. mei_hdr.msg_complete = 0;
  299. } else {
  300. /* wait for next time the host buffer is empty */
  301. return 0;
  302. }
  303. dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
  304. cb->request_buffer.size, cb->buf_idx);
  305. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
  306. *slots -= msg_slots;
  307. if (mei_write_message(dev, &mei_hdr,
  308. cb->request_buffer.data + cb->buf_idx)) {
  309. cl->status = -ENODEV;
  310. list_move_tail(&cb->list, &cmpl_list->list);
  311. return -ENODEV;
  312. }
  313. cl->status = 0;
  314. cb->buf_idx += mei_hdr.length;
  315. if (mei_hdr.msg_complete) {
  316. if (mei_cl_flow_ctrl_reduce(cl))
  317. return -ENODEV;
  318. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  319. }
  320. return 0;
  321. }
  322. /**
  323. * mei_irq_read_handler - bottom half read routine after ISR to
  324. * handle the read processing.
  325. *
  326. * @dev: the device structure
  327. * @cmpl_list: An instance of our list structure
  328. * @slots: slots to read.
  329. *
  330. * returns 0 on success, <0 on failure.
  331. */
  332. int mei_irq_read_handler(struct mei_device *dev,
  333. struct mei_cl_cb *cmpl_list, s32 *slots)
  334. {
  335. struct mei_msg_hdr *mei_hdr;
  336. struct mei_cl *cl_pos = NULL;
  337. struct mei_cl *cl_next = NULL;
  338. int ret = 0;
  339. if (!dev->rd_msg_hdr) {
  340. dev->rd_msg_hdr = mei_read_hdr(dev);
  341. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  342. (*slots)--;
  343. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  344. }
  345. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  346. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  347. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  348. dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
  349. ret = -EBADMSG;
  350. goto end;
  351. }
  352. if (mei_hdr->host_addr || mei_hdr->me_addr) {
  353. list_for_each_entry_safe(cl_pos, cl_next,
  354. &dev->file_list, link) {
  355. dev_dbg(&dev->pdev->dev,
  356. "list_for_each_entry_safe read host"
  357. " client = %d, ME client = %d\n",
  358. cl_pos->host_client_id,
  359. cl_pos->me_client_id);
  360. if (mei_cl_hbm_equal(cl_pos, mei_hdr))
  361. break;
  362. }
  363. if (&cl_pos->link == &dev->file_list) {
  364. dev_dbg(&dev->pdev->dev, "corrupted message header\n");
  365. ret = -EBADMSG;
  366. goto end;
  367. }
  368. }
  369. if (((*slots) * sizeof(u32)) < mei_hdr->length) {
  370. dev_err(&dev->pdev->dev,
  371. "we can't read the message slots =%08x.\n",
  372. *slots);
  373. /* we can't read the message */
  374. ret = -ERANGE;
  375. goto end;
  376. }
  377. /* decide where to read the message too */
  378. if (!mei_hdr->host_addr) {
  379. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
  380. mei_hbm_dispatch(dev, mei_hdr);
  381. dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
  382. } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
  383. (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
  384. (dev->iamthif_state == MEI_IAMTHIF_READING)) {
  385. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
  386. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  387. ret = mei_amthif_irq_read_msg(dev, mei_hdr, cmpl_list);
  388. if (ret)
  389. goto end;
  390. } else {
  391. dev_dbg(&dev->pdev->dev, "call mei_cl_irq_read_msg.\n");
  392. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  393. ret = mei_cl_irq_read_msg(dev, mei_hdr, cmpl_list);
  394. if (ret)
  395. goto end;
  396. }
  397. /* reset the number of slots and header */
  398. *slots = mei_count_full_read_slots(dev);
  399. dev->rd_msg_hdr = 0;
  400. if (*slots == -EOVERFLOW) {
  401. /* overflow - reset */
  402. dev_err(&dev->pdev->dev, "resetting due to slots overflow.\n");
  403. /* set the event since message has been read */
  404. ret = -ERANGE;
  405. goto end;
  406. }
  407. end:
  408. return ret;
  409. }
  410. EXPORT_SYMBOL_GPL(mei_irq_read_handler);
  411. /**
  412. * mei_irq_write_handler - dispatch write requests
  413. * after irq received
  414. *
  415. * @dev: the device structure
  416. * @cmpl_list: An instance of our list structure
  417. *
  418. * returns 0 on success, <0 on failure.
  419. */
  420. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
  421. {
  422. struct mei_cl *cl;
  423. struct mei_cl_cb *pos = NULL, *next = NULL;
  424. struct mei_cl_cb *list;
  425. s32 slots;
  426. int ret;
  427. if (!mei_hbuf_is_ready(dev)) {
  428. dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
  429. return 0;
  430. }
  431. slots = mei_hbuf_empty_slots(dev);
  432. if (slots <= 0)
  433. return -EMSGSIZE;
  434. /* complete all waiting for write CB */
  435. dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
  436. list = &dev->write_waiting_list;
  437. list_for_each_entry_safe(pos, next, &list->list, list) {
  438. cl = pos->cl;
  439. if (cl == NULL)
  440. continue;
  441. cl->status = 0;
  442. list_del(&pos->list);
  443. if (MEI_WRITING == cl->writing_state &&
  444. pos->fop_type == MEI_FOP_WRITE &&
  445. cl != &dev->iamthif_cl) {
  446. dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
  447. cl->writing_state = MEI_WRITE_COMPLETE;
  448. list_add_tail(&pos->list, &cmpl_list->list);
  449. }
  450. if (cl == &dev->iamthif_cl) {
  451. dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
  452. if (dev->iamthif_flow_control_pending) {
  453. ret = mei_amthif_irq_read(dev, &slots);
  454. if (ret)
  455. return ret;
  456. }
  457. }
  458. }
  459. if (dev->wd_state == MEI_WD_STOPPING) {
  460. dev->wd_state = MEI_WD_IDLE;
  461. wake_up_interruptible(&dev->wait_stop_wd);
  462. }
  463. if (dev->wr_ext_msg.hdr.length) {
  464. mei_write_message(dev, &dev->wr_ext_msg.hdr,
  465. dev->wr_ext_msg.data);
  466. slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
  467. dev->wr_ext_msg.hdr.length = 0;
  468. }
  469. if (dev->dev_state == MEI_DEV_ENABLED) {
  470. if (dev->wd_pending &&
  471. mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
  472. if (mei_wd_send(dev))
  473. dev_dbg(&dev->pdev->dev, "wd send failed.\n");
  474. else if (mei_cl_flow_ctrl_reduce(&dev->wd_cl))
  475. return -ENODEV;
  476. dev->wd_pending = false;
  477. if (dev->wd_state == MEI_WD_RUNNING)
  478. slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
  479. else
  480. slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
  481. }
  482. }
  483. /* complete control write list CB */
  484. dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
  485. list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
  486. cl = pos->cl;
  487. if (!cl) {
  488. list_del(&pos->list);
  489. return -ENODEV;
  490. }
  491. switch (pos->fop_type) {
  492. case MEI_FOP_CLOSE:
  493. /* send disconnect message */
  494. ret = _mei_irq_thread_close(dev, &slots, pos,
  495. cl, cmpl_list);
  496. if (ret)
  497. return ret;
  498. break;
  499. case MEI_FOP_READ:
  500. /* send flow control message */
  501. ret = _mei_irq_thread_read(dev, &slots, pos,
  502. cl, cmpl_list);
  503. if (ret)
  504. return ret;
  505. break;
  506. case MEI_FOP_IOCTL:
  507. /* connect message */
  508. if (mei_cl_is_other_connecting(cl))
  509. continue;
  510. ret = _mei_irq_thread_ioctl(dev, &slots, pos,
  511. cl, cmpl_list);
  512. if (ret)
  513. return ret;
  514. break;
  515. default:
  516. BUG();
  517. }
  518. }
  519. /* complete write list CB */
  520. dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
  521. list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
  522. cl = pos->cl;
  523. if (cl == NULL)
  524. continue;
  525. if (mei_cl_flow_ctrl_creds(cl) <= 0) {
  526. dev_dbg(&dev->pdev->dev,
  527. "No flow control credentials for client %d, not sending.\n",
  528. cl->host_client_id);
  529. continue;
  530. }
  531. if (cl == &dev->iamthif_cl)
  532. ret = mei_amthif_irq_write_complete(dev, &slots,
  533. pos, cmpl_list);
  534. else
  535. ret = mei_irq_thread_write_complete(dev, &slots, pos,
  536. cmpl_list);
  537. if (ret)
  538. return ret;
  539. }
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(mei_irq_write_handler);
  543. /**
  544. * mei_timer - timer function.
  545. *
  546. * @work: pointer to the work_struct structure
  547. *
  548. * NOTE: This function is called by timer interrupt work
  549. */
  550. void mei_timer(struct work_struct *work)
  551. {
  552. unsigned long timeout;
  553. struct mei_cl *cl_pos = NULL;
  554. struct mei_cl *cl_next = NULL;
  555. struct mei_cl_cb *cb_pos = NULL;
  556. struct mei_cl_cb *cb_next = NULL;
  557. struct mei_device *dev = container_of(work,
  558. struct mei_device, timer_work.work);
  559. mutex_lock(&dev->device_lock);
  560. if (dev->dev_state != MEI_DEV_ENABLED) {
  561. if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
  562. if (dev->init_clients_timer) {
  563. if (--dev->init_clients_timer == 0) {
  564. dev_err(&dev->pdev->dev, "reset: init clients timeout hbm_state = %d.\n",
  565. dev->hbm_state);
  566. mei_reset(dev, 1);
  567. }
  568. }
  569. }
  570. goto out;
  571. }
  572. /*** connect/disconnect timeouts ***/
  573. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  574. if (cl_pos->timer_count) {
  575. if (--cl_pos->timer_count == 0) {
  576. dev_err(&dev->pdev->dev, "reset: connect/disconnect timeout.\n");
  577. mei_reset(dev, 1);
  578. goto out;
  579. }
  580. }
  581. }
  582. if (dev->iamthif_stall_timer) {
  583. if (--dev->iamthif_stall_timer == 0) {
  584. dev_err(&dev->pdev->dev, "reset: amthif hanged.\n");
  585. mei_reset(dev, 1);
  586. dev->iamthif_msg_buf_size = 0;
  587. dev->iamthif_msg_buf_index = 0;
  588. dev->iamthif_canceled = false;
  589. dev->iamthif_ioctl = true;
  590. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  591. dev->iamthif_timer = 0;
  592. mei_io_cb_free(dev->iamthif_current_cb);
  593. dev->iamthif_current_cb = NULL;
  594. dev->iamthif_file_object = NULL;
  595. mei_amthif_run_next_cmd(dev);
  596. }
  597. }
  598. if (dev->iamthif_timer) {
  599. timeout = dev->iamthif_timer +
  600. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  601. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  602. dev->iamthif_timer);
  603. dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
  604. dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
  605. if (time_after(jiffies, timeout)) {
  606. /*
  607. * User didn't read the AMTHI data on time (15sec)
  608. * freeing AMTHI for other requests
  609. */
  610. dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
  611. list_for_each_entry_safe(cb_pos, cb_next,
  612. &dev->amthif_rd_complete_list.list, list) {
  613. cl_pos = cb_pos->file_object->private_data;
  614. /* Finding the AMTHI entry. */
  615. if (cl_pos == &dev->iamthif_cl)
  616. list_del(&cb_pos->list);
  617. }
  618. mei_io_cb_free(dev->iamthif_current_cb);
  619. dev->iamthif_current_cb = NULL;
  620. dev->iamthif_file_object->private_data = NULL;
  621. dev->iamthif_file_object = NULL;
  622. dev->iamthif_timer = 0;
  623. mei_amthif_run_next_cmd(dev);
  624. }
  625. }
  626. out:
  627. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  628. mutex_unlock(&dev->device_lock);
  629. }