interrupt.c 12 KB

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