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