interrupt.c 15 KB

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