interrupt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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/pm_runtime.h>
  23. #include <linux/mei.h>
  24. #include "mei_dev.h"
  25. #include "hbm.h"
  26. #include "client.h"
  27. /**
  28. * mei_irq_compl_handler - dispatch complete handlers
  29. * for the completed callbacks
  30. *
  31. * @dev: mei device
  32. * @compl_list: list of completed cbs
  33. */
  34. void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
  35. {
  36. struct mei_cl_cb *cb, *next;
  37. struct mei_cl *cl;
  38. list_for_each_entry_safe(cb, next, &compl_list->list, list) {
  39. cl = cb->cl;
  40. list_del_init(&cb->list);
  41. dev_dbg(dev->dev, "completing call back.\n");
  42. if (cl == &dev->iamthif_cl)
  43. mei_amthif_complete(cl, cb);
  44. else
  45. mei_cl_complete(cl, cb);
  46. }
  47. }
  48. EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
  49. /**
  50. * mei_cl_hbm_equal - check if hbm is addressed to the client
  51. *
  52. * @cl: host client
  53. * @mei_hdr: header of mei client message
  54. *
  55. * Return: true if matches, false otherwise
  56. */
  57. static inline int mei_cl_hbm_equal(struct mei_cl *cl,
  58. struct mei_msg_hdr *mei_hdr)
  59. {
  60. return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
  61. mei_cl_me_id(cl) == mei_hdr->me_addr;
  62. }
  63. /**
  64. * mei_irq_discard_msg - discard received message
  65. *
  66. * @dev: mei device
  67. * @hdr: message header
  68. */
  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. size_t buf_sz;
  96. cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
  97. if (!cb) {
  98. cl_err(dev, cl, "pending read cb not found\n");
  99. goto out;
  100. }
  101. if (!mei_cl_is_connected(cl)) {
  102. cl_dbg(dev, cl, "not connected\n");
  103. cb->status = -ENODEV;
  104. goto out;
  105. }
  106. if (cb->buf.size == 0 || cb->buf.data == NULL) {
  107. cl_err(dev, cl, "response buffer is not allocated.\n");
  108. list_move_tail(&cb->list, &complete_list->list);
  109. cb->status = -ENOMEM;
  110. goto out;
  111. }
  112. buf_sz = mei_hdr->length + cb->buf_idx;
  113. /* catch for integer overflow */
  114. if (buf_sz < cb->buf_idx) {
  115. cl_err(dev, cl, "message is too big len %d idx %zu\n",
  116. mei_hdr->length, cb->buf_idx);
  117. list_move_tail(&cb->list, &complete_list->list);
  118. cb->status = -EMSGSIZE;
  119. goto out;
  120. }
  121. if (cb->buf.size < buf_sz) {
  122. cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
  123. cb->buf.size, mei_hdr->length, cb->buf_idx);
  124. buffer = krealloc(cb->buf.data, buf_sz, GFP_KERNEL);
  125. if (!buffer) {
  126. cb->status = -ENOMEM;
  127. list_move_tail(&cb->list, &complete_list->list);
  128. goto out;
  129. }
  130. cb->buf.data = buffer;
  131. cb->buf.size = buf_sz;
  132. }
  133. buffer = cb->buf.data + cb->buf_idx;
  134. mei_read_slots(dev, buffer, mei_hdr->length);
  135. cb->buf_idx += mei_hdr->length;
  136. if (mei_hdr->msg_complete) {
  137. cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
  138. list_move_tail(&cb->list, &complete_list->list);
  139. } else {
  140. pm_runtime_mark_last_busy(dev->dev);
  141. pm_request_autosuspend(dev->dev);
  142. }
  143. out:
  144. if (!buffer)
  145. mei_irq_discard_msg(dev, mei_hdr);
  146. return 0;
  147. }
  148. /**
  149. * mei_cl_irq_disconnect_rsp - send disconnection response message
  150. *
  151. * @cl: client
  152. * @cb: callback block.
  153. * @cmpl_list: complete list.
  154. *
  155. * Return: 0, OK; otherwise, error.
  156. */
  157. static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
  158. struct mei_cl_cb *cmpl_list)
  159. {
  160. struct mei_device *dev = cl->dev;
  161. u32 msg_slots;
  162. int slots;
  163. int ret;
  164. slots = mei_hbuf_empty_slots(dev);
  165. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_response));
  166. if (slots < msg_slots)
  167. return -EMSGSIZE;
  168. ret = mei_hbm_cl_disconnect_rsp(dev, cl);
  169. list_move_tail(&cb->list, &cmpl_list->list);
  170. return ret;
  171. }
  172. /**
  173. * mei_cl_irq_read - processes client read related operation from the
  174. * interrupt thread context - request for flow control credits
  175. *
  176. * @cl: client
  177. * @cb: callback block.
  178. * @cmpl_list: complete list.
  179. *
  180. * Return: 0, OK; otherwise, error.
  181. */
  182. static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
  183. struct mei_cl_cb *cmpl_list)
  184. {
  185. struct mei_device *dev = cl->dev;
  186. u32 msg_slots;
  187. int slots;
  188. int ret;
  189. msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
  190. slots = mei_hbuf_empty_slots(dev);
  191. if (slots < msg_slots)
  192. return -EMSGSIZE;
  193. ret = mei_hbm_cl_flow_control_req(dev, cl);
  194. if (ret) {
  195. cl->status = ret;
  196. cb->buf_idx = 0;
  197. list_move_tail(&cb->list, &cmpl_list->list);
  198. return ret;
  199. }
  200. list_move_tail(&cb->list, &cl->rd_pending);
  201. return 0;
  202. }
  203. static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
  204. {
  205. return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
  206. }
  207. static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
  208. {
  209. return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
  210. }
  211. /**
  212. * mei_irq_read_handler - bottom half read routine after ISR to
  213. * handle the read processing.
  214. *
  215. * @dev: the device structure
  216. * @cmpl_list: An instance of our list structure
  217. * @slots: slots to read.
  218. *
  219. * Return: 0 on success, <0 on failure.
  220. */
  221. int mei_irq_read_handler(struct mei_device *dev,
  222. struct mei_cl_cb *cmpl_list, s32 *slots)
  223. {
  224. struct mei_msg_hdr *mei_hdr;
  225. struct mei_cl *cl;
  226. int ret;
  227. if (!dev->rd_msg_hdr) {
  228. dev->rd_msg_hdr = mei_read_hdr(dev);
  229. (*slots)--;
  230. dev_dbg(dev->dev, "slots =%08x.\n", *slots);
  231. }
  232. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  233. dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  234. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  235. dev_err(dev->dev, "corrupted message header 0x%08X\n",
  236. dev->rd_msg_hdr);
  237. ret = -EBADMSG;
  238. goto end;
  239. }
  240. if (mei_slots2data(*slots) < mei_hdr->length) {
  241. dev_err(dev->dev, "less data available than length=%08x.\n",
  242. *slots);
  243. /* we can't read the message */
  244. ret = -ENODATA;
  245. goto end;
  246. }
  247. /* HBM message */
  248. if (hdr_is_hbm(mei_hdr)) {
  249. ret = mei_hbm_dispatch(dev, mei_hdr);
  250. if (ret) {
  251. dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
  252. ret);
  253. goto end;
  254. }
  255. goto reset_slots;
  256. }
  257. /* find recipient cl */
  258. list_for_each_entry(cl, &dev->file_list, link) {
  259. if (mei_cl_hbm_equal(cl, mei_hdr)) {
  260. cl_dbg(dev, cl, "got a message\n");
  261. break;
  262. }
  263. }
  264. /* if no recipient cl was found we assume corrupted header */
  265. if (&cl->link == &dev->file_list) {
  266. /* A message for not connected fixed address clients
  267. * should be silently discarded
  268. */
  269. if (hdr_is_fixed(mei_hdr)) {
  270. mei_irq_discard_msg(dev, mei_hdr);
  271. ret = 0;
  272. goto reset_slots;
  273. }
  274. dev_err(dev->dev, "no destination client found 0x%08X\n",
  275. dev->rd_msg_hdr);
  276. ret = -EBADMSG;
  277. goto end;
  278. }
  279. if (cl == &dev->iamthif_cl) {
  280. ret = mei_amthif_irq_read_msg(cl, mei_hdr, cmpl_list);
  281. } else {
  282. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  283. }
  284. reset_slots:
  285. /* reset the number of slots and header */
  286. *slots = mei_count_full_read_slots(dev);
  287. dev->rd_msg_hdr = 0;
  288. if (*slots == -EOVERFLOW) {
  289. /* overflow - reset */
  290. dev_err(dev->dev, "resetting due to slots overflow.\n");
  291. /* set the event since message has been read */
  292. ret = -ERANGE;
  293. goto end;
  294. }
  295. end:
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(mei_irq_read_handler);
  299. /**
  300. * mei_irq_write_handler - dispatch write requests
  301. * after irq received
  302. *
  303. * @dev: the device structure
  304. * @cmpl_list: An instance of our list structure
  305. *
  306. * Return: 0 on success, <0 on failure.
  307. */
  308. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
  309. {
  310. struct mei_cl *cl;
  311. struct mei_cl_cb *cb, *next;
  312. struct mei_cl_cb *list;
  313. s32 slots;
  314. int ret;
  315. if (!mei_hbuf_acquire(dev))
  316. return 0;
  317. slots = mei_hbuf_empty_slots(dev);
  318. if (slots <= 0)
  319. return -EMSGSIZE;
  320. /* complete all waiting for write CB */
  321. dev_dbg(dev->dev, "complete all waiting for write cb.\n");
  322. list = &dev->write_waiting_list;
  323. list_for_each_entry_safe(cb, next, &list->list, list) {
  324. cl = cb->cl;
  325. cl->status = 0;
  326. cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
  327. cl->writing_state = MEI_WRITE_COMPLETE;
  328. list_move_tail(&cb->list, &cmpl_list->list);
  329. }
  330. /* complete control write list CB */
  331. dev_dbg(dev->dev, "complete control write list cb.\n");
  332. list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
  333. cl = cb->cl;
  334. switch (cb->fop_type) {
  335. case MEI_FOP_DISCONNECT:
  336. /* send disconnect message */
  337. ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
  338. if (ret)
  339. return ret;
  340. break;
  341. case MEI_FOP_READ:
  342. /* send flow control message */
  343. ret = mei_cl_irq_read(cl, cb, cmpl_list);
  344. if (ret)
  345. return ret;
  346. break;
  347. case MEI_FOP_CONNECT:
  348. /* connect message */
  349. ret = mei_cl_irq_connect(cl, cb, cmpl_list);
  350. if (ret)
  351. return ret;
  352. break;
  353. case MEI_FOP_DISCONNECT_RSP:
  354. /* send disconnect resp */
  355. ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
  356. if (ret)
  357. return ret;
  358. break;
  359. case MEI_FOP_NOTIFY_START:
  360. case MEI_FOP_NOTIFY_STOP:
  361. ret = mei_cl_irq_notify(cl, cb, cmpl_list);
  362. if (ret)
  363. return ret;
  364. break;
  365. default:
  366. BUG();
  367. }
  368. }
  369. /* complete write list CB */
  370. dev_dbg(dev->dev, "complete write list cb.\n");
  371. list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
  372. cl = cb->cl;
  373. if (cl == &dev->iamthif_cl)
  374. ret = mei_amthif_irq_write(cl, cb, cmpl_list);
  375. else
  376. ret = mei_cl_irq_write(cl, cb, cmpl_list);
  377. if (ret)
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(mei_irq_write_handler);
  383. /**
  384. * mei_connect_timeout - connect/disconnect timeouts
  385. *
  386. * @cl: host client
  387. */
  388. static void mei_connect_timeout(struct mei_cl *cl)
  389. {
  390. struct mei_device *dev = cl->dev;
  391. if (cl->state == MEI_FILE_CONNECTING) {
  392. if (dev->hbm_f_dot_supported) {
  393. cl->state = MEI_FILE_DISCONNECT_REQUIRED;
  394. wake_up(&cl->wait);
  395. return;
  396. }
  397. }
  398. mei_reset(dev);
  399. }
  400. /**
  401. * mei_timer - timer function.
  402. *
  403. * @work: pointer to the work_struct structure
  404. *
  405. */
  406. void mei_timer(struct work_struct *work)
  407. {
  408. struct mei_cl *cl;
  409. struct mei_device *dev = container_of(work,
  410. struct mei_device, timer_work.work);
  411. mutex_lock(&dev->device_lock);
  412. /* Catch interrupt stalls during HBM init handshake */
  413. if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
  414. dev->hbm_state != MEI_HBM_IDLE) {
  415. if (dev->init_clients_timer) {
  416. if (--dev->init_clients_timer == 0) {
  417. dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
  418. dev->hbm_state);
  419. mei_reset(dev);
  420. goto out;
  421. }
  422. }
  423. }
  424. if (dev->dev_state != MEI_DEV_ENABLED)
  425. goto out;
  426. /*** connect/disconnect timeouts ***/
  427. list_for_each_entry(cl, &dev->file_list, link) {
  428. if (cl->timer_count) {
  429. if (--cl->timer_count == 0) {
  430. dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
  431. mei_connect_timeout(cl);
  432. goto out;
  433. }
  434. }
  435. }
  436. if (!mei_cl_is_connected(&dev->iamthif_cl))
  437. goto out;
  438. if (dev->iamthif_stall_timer) {
  439. if (--dev->iamthif_stall_timer == 0) {
  440. dev_err(dev->dev, "timer: amthif hanged.\n");
  441. mei_reset(dev);
  442. dev->iamthif_canceled = false;
  443. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  444. mei_io_cb_free(dev->iamthif_current_cb);
  445. dev->iamthif_current_cb = NULL;
  446. dev->iamthif_fp = NULL;
  447. mei_amthif_run_next_cmd(dev);
  448. }
  449. }
  450. out:
  451. if (dev->dev_state != MEI_DEV_DISABLED)
  452. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  453. mutex_unlock(&dev->device_lock);
  454. }