interrupt.c 12 KB

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