interrupt.c 15 KB

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