interrupt.c 14 KB

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