interrupt.c 20 KB

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