interrupt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 mei_cl_host_addr(cl) == mei_hdr->host_addr &&
  60. mei_cl_me_id(cl) == 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. mei_cl_set_disconnected(cl);
  159. mei_io_cb_free(cb);
  160. mei_me_cl_put(cl->me_cl);
  161. cl->me_cl = NULL;
  162. return ret;
  163. }
  164. /**
  165. * mei_cl_irq_read - processes client read related operation from the
  166. * interrupt thread context - request for flow control credits
  167. *
  168. * @cl: client
  169. * @cb: callback block.
  170. * @cmpl_list: complete list.
  171. *
  172. * Return: 0, OK; otherwise, error.
  173. */
  174. static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
  175. struct mei_cl_cb *cmpl_list)
  176. {
  177. struct mei_device *dev = cl->dev;
  178. u32 msg_slots;
  179. int slots;
  180. int ret;
  181. msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
  182. slots = mei_hbuf_empty_slots(dev);
  183. if (slots < msg_slots)
  184. return -EMSGSIZE;
  185. ret = mei_hbm_cl_flow_control_req(dev, cl);
  186. if (ret) {
  187. cl->status = ret;
  188. cb->buf_idx = 0;
  189. list_move_tail(&cb->list, &cmpl_list->list);
  190. return ret;
  191. }
  192. list_move_tail(&cb->list, &cl->rd_pending);
  193. return 0;
  194. }
  195. /**
  196. * mei_irq_read_handler - bottom half read routine after ISR to
  197. * handle the read processing.
  198. *
  199. * @dev: the device structure
  200. * @cmpl_list: An instance of our list structure
  201. * @slots: slots to read.
  202. *
  203. * Return: 0 on success, <0 on failure.
  204. */
  205. int mei_irq_read_handler(struct mei_device *dev,
  206. struct mei_cl_cb *cmpl_list, s32 *slots)
  207. {
  208. struct mei_msg_hdr *mei_hdr;
  209. struct mei_cl *cl;
  210. int ret;
  211. if (!dev->rd_msg_hdr) {
  212. dev->rd_msg_hdr = mei_read_hdr(dev);
  213. (*slots)--;
  214. dev_dbg(dev->dev, "slots =%08x.\n", *slots);
  215. }
  216. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  217. dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  218. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  219. dev_err(dev->dev, "corrupted message header 0x%08X\n",
  220. dev->rd_msg_hdr);
  221. ret = -EBADMSG;
  222. goto end;
  223. }
  224. if (mei_slots2data(*slots) < mei_hdr->length) {
  225. dev_err(dev->dev, "less data available than length=%08x.\n",
  226. *slots);
  227. /* we can't read the message */
  228. ret = -ENODATA;
  229. goto end;
  230. }
  231. /* HBM message */
  232. if (mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0) {
  233. ret = mei_hbm_dispatch(dev, mei_hdr);
  234. if (ret) {
  235. dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
  236. ret);
  237. goto end;
  238. }
  239. goto reset_slots;
  240. }
  241. /* find recipient cl */
  242. list_for_each_entry(cl, &dev->file_list, link) {
  243. if (mei_cl_hbm_equal(cl, mei_hdr)) {
  244. cl_dbg(dev, cl, "got a message\n");
  245. break;
  246. }
  247. }
  248. /* if no recipient cl was found we assume corrupted header */
  249. if (&cl->link == &dev->file_list) {
  250. dev_err(dev->dev, "no destination client found 0x%08X\n",
  251. dev->rd_msg_hdr);
  252. ret = -EBADMSG;
  253. goto end;
  254. }
  255. if (cl == &dev->iamthif_cl) {
  256. ret = mei_amthif_irq_read_msg(cl, mei_hdr, cmpl_list);
  257. } else {
  258. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  259. }
  260. reset_slots:
  261. /* reset the number of slots and header */
  262. *slots = mei_count_full_read_slots(dev);
  263. dev->rd_msg_hdr = 0;
  264. if (*slots == -EOVERFLOW) {
  265. /* overflow - reset */
  266. dev_err(dev->dev, "resetting due to slots overflow.\n");
  267. /* set the event since message has been read */
  268. ret = -ERANGE;
  269. goto end;
  270. }
  271. end:
  272. return ret;
  273. }
  274. EXPORT_SYMBOL_GPL(mei_irq_read_handler);
  275. /**
  276. * mei_irq_write_handler - dispatch write requests
  277. * after irq received
  278. *
  279. * @dev: the device structure
  280. * @cmpl_list: An instance of our list structure
  281. *
  282. * Return: 0 on success, <0 on failure.
  283. */
  284. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
  285. {
  286. struct mei_cl *cl;
  287. struct mei_cl_cb *cb, *next;
  288. struct mei_cl_cb *list;
  289. s32 slots;
  290. int ret;
  291. if (!mei_hbuf_acquire(dev))
  292. return 0;
  293. slots = mei_hbuf_empty_slots(dev);
  294. if (slots <= 0)
  295. return -EMSGSIZE;
  296. /* complete all waiting for write CB */
  297. dev_dbg(dev->dev, "complete all waiting for write cb.\n");
  298. list = &dev->write_waiting_list;
  299. list_for_each_entry_safe(cb, next, &list->list, list) {
  300. cl = cb->cl;
  301. cl->status = 0;
  302. cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
  303. cl->writing_state = MEI_WRITE_COMPLETE;
  304. list_move_tail(&cb->list, &cmpl_list->list);
  305. }
  306. if (dev->wd_state == MEI_WD_STOPPING) {
  307. dev->wd_state = MEI_WD_IDLE;
  308. wake_up(&dev->wait_stop_wd);
  309. }
  310. if (mei_cl_is_connected(&dev->wd_cl)) {
  311. if (dev->wd_pending &&
  312. mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
  313. ret = mei_wd_send(dev);
  314. if (ret)
  315. return ret;
  316. dev->wd_pending = false;
  317. }
  318. }
  319. /* complete control write list CB */
  320. dev_dbg(dev->dev, "complete control write list cb.\n");
  321. list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
  322. cl = cb->cl;
  323. switch (cb->fop_type) {
  324. case MEI_FOP_DISCONNECT:
  325. /* send disconnect message */
  326. ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
  327. if (ret)
  328. return ret;
  329. break;
  330. case MEI_FOP_READ:
  331. /* send flow control message */
  332. ret = mei_cl_irq_read(cl, cb, cmpl_list);
  333. if (ret)
  334. return ret;
  335. break;
  336. case MEI_FOP_CONNECT:
  337. /* connect message */
  338. ret = mei_cl_irq_connect(cl, cb, cmpl_list);
  339. if (ret)
  340. return ret;
  341. break;
  342. case MEI_FOP_DISCONNECT_RSP:
  343. /* send disconnect resp */
  344. ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
  345. if (ret)
  346. return ret;
  347. break;
  348. default:
  349. BUG();
  350. }
  351. }
  352. /* complete write list CB */
  353. dev_dbg(dev->dev, "complete write list cb.\n");
  354. list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
  355. cl = cb->cl;
  356. if (cl == &dev->iamthif_cl)
  357. ret = mei_amthif_irq_write(cl, cb, cmpl_list);
  358. else
  359. ret = mei_cl_irq_write(cl, cb, cmpl_list);
  360. if (ret)
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(mei_irq_write_handler);
  366. /**
  367. * mei_timer - timer function.
  368. *
  369. * @work: pointer to the work_struct structure
  370. *
  371. */
  372. void mei_timer(struct work_struct *work)
  373. {
  374. unsigned long timeout;
  375. struct mei_cl *cl;
  376. struct mei_device *dev = container_of(work,
  377. struct mei_device, timer_work.work);
  378. mutex_lock(&dev->device_lock);
  379. /* Catch interrupt stalls during HBM init handshake */
  380. if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
  381. dev->hbm_state != MEI_HBM_IDLE) {
  382. if (dev->init_clients_timer) {
  383. if (--dev->init_clients_timer == 0) {
  384. dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
  385. dev->hbm_state);
  386. mei_reset(dev);
  387. goto out;
  388. }
  389. }
  390. }
  391. if (dev->dev_state != MEI_DEV_ENABLED)
  392. goto out;
  393. /*** connect/disconnect timeouts ***/
  394. list_for_each_entry(cl, &dev->file_list, link) {
  395. if (cl->timer_count) {
  396. if (--cl->timer_count == 0) {
  397. dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
  398. mei_reset(dev);
  399. goto out;
  400. }
  401. }
  402. }
  403. if (!mei_cl_is_connected(&dev->iamthif_cl))
  404. goto out;
  405. if (dev->iamthif_stall_timer) {
  406. if (--dev->iamthif_stall_timer == 0) {
  407. dev_err(dev->dev, "timer: amthif hanged.\n");
  408. mei_reset(dev);
  409. dev->iamthif_canceled = false;
  410. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  411. dev->iamthif_timer = 0;
  412. mei_io_cb_free(dev->iamthif_current_cb);
  413. dev->iamthif_current_cb = NULL;
  414. dev->iamthif_file_object = NULL;
  415. mei_amthif_run_next_cmd(dev);
  416. }
  417. }
  418. if (dev->iamthif_timer) {
  419. timeout = dev->iamthif_timer +
  420. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  421. dev_dbg(dev->dev, "dev->iamthif_timer = %ld\n",
  422. dev->iamthif_timer);
  423. dev_dbg(dev->dev, "timeout = %ld\n", timeout);
  424. dev_dbg(dev->dev, "jiffies = %ld\n", jiffies);
  425. if (time_after(jiffies, timeout)) {
  426. /*
  427. * User didn't read the AMTHI data on time (15sec)
  428. * freeing AMTHI for other requests
  429. */
  430. dev_dbg(dev->dev, "freeing AMTHI for other requests\n");
  431. mei_io_list_flush(&dev->amthif_rd_complete_list,
  432. &dev->iamthif_cl);
  433. mei_io_cb_free(dev->iamthif_current_cb);
  434. dev->iamthif_current_cb = NULL;
  435. dev->iamthif_file_object->private_data = NULL;
  436. dev->iamthif_file_object = NULL;
  437. dev->iamthif_timer = 0;
  438. mei_amthif_run_next_cmd(dev);
  439. }
  440. }
  441. out:
  442. if (dev->dev_state != MEI_DEV_DISABLED)
  443. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  444. mutex_unlock(&dev->device_lock);
  445. }