interrupt.c 15 KB

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