interrupt.c 15 KB

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