interrupt.c 15 KB

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