interrupt.c 20 KB

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