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. * @cmpl_list: list of completed cbs
  33. */
  34. void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list)
  35. {
  36. struct mei_cl_cb *cb, *next;
  37. struct mei_cl *cl;
  38. list_for_each_entry_safe(cb, next, cmpl_list, list) {
  39. cl = cb->cl;
  40. list_del_init(&cb->list);
  41. dev_dbg(dev->dev, "completing call back.\n");
  42. mei_cl_complete(cl, cb);
  43. }
  44. }
  45. EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
  46. /**
  47. * mei_cl_hbm_equal - check if hbm is addressed to the client
  48. *
  49. * @cl: host client
  50. * @mei_hdr: header of mei client message
  51. *
  52. * Return: true if matches, false otherwise
  53. */
  54. static inline int mei_cl_hbm_equal(struct mei_cl *cl,
  55. struct mei_msg_hdr *mei_hdr)
  56. {
  57. return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
  58. mei_cl_me_id(cl) == mei_hdr->me_addr;
  59. }
  60. /**
  61. * mei_irq_discard_msg - discard received message
  62. *
  63. * @dev: mei device
  64. * @hdr: message header
  65. */
  66. static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
  67. {
  68. /*
  69. * no need to check for size as it is guarantied
  70. * that length fits into rd_msg_buf
  71. */
  72. mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
  73. dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
  74. MEI_HDR_PRM(hdr));
  75. }
  76. /**
  77. * mei_cl_irq_read_msg - process client message
  78. *
  79. * @cl: reading client
  80. * @mei_hdr: header of mei client message
  81. * @cmpl_list: completion list
  82. *
  83. * Return: always 0
  84. */
  85. static int mei_cl_irq_read_msg(struct mei_cl *cl,
  86. struct mei_msg_hdr *mei_hdr,
  87. struct list_head *cmpl_list)
  88. {
  89. struct mei_device *dev = cl->dev;
  90. struct mei_cl_cb *cb;
  91. size_t buf_sz;
  92. cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
  93. if (!cb) {
  94. if (!mei_cl_is_fixed_address(cl)) {
  95. cl_err(dev, cl, "pending read cb not found\n");
  96. goto discard;
  97. }
  98. cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp);
  99. if (!cb)
  100. goto discard;
  101. list_add_tail(&cb->list, &cl->rd_pending);
  102. }
  103. if (!mei_cl_is_connected(cl)) {
  104. cl_dbg(dev, cl, "not connected\n");
  105. cb->status = -ENODEV;
  106. goto discard;
  107. }
  108. buf_sz = mei_hdr->length + cb->buf_idx;
  109. /* catch for integer overflow */
  110. if (buf_sz < cb->buf_idx) {
  111. cl_err(dev, cl, "message is too big len %d idx %zu\n",
  112. mei_hdr->length, cb->buf_idx);
  113. cb->status = -EMSGSIZE;
  114. goto discard;
  115. }
  116. if (cb->buf.size < buf_sz) {
  117. cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
  118. cb->buf.size, mei_hdr->length, cb->buf_idx);
  119. cb->status = -EMSGSIZE;
  120. goto discard;
  121. }
  122. mei_read_slots(dev, cb->buf.data + cb->buf_idx, mei_hdr->length);
  123. cb->buf_idx += mei_hdr->length;
  124. if (mei_hdr->msg_complete) {
  125. cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
  126. list_move_tail(&cb->list, cmpl_list);
  127. } else {
  128. pm_runtime_mark_last_busy(dev->dev);
  129. pm_request_autosuspend(dev->dev);
  130. }
  131. return 0;
  132. discard:
  133. if (cb)
  134. list_move_tail(&cb->list, cmpl_list);
  135. mei_irq_discard_msg(dev, mei_hdr);
  136. return 0;
  137. }
  138. /**
  139. * mei_cl_irq_disconnect_rsp - send disconnection response message
  140. *
  141. * @cl: client
  142. * @cb: callback block.
  143. * @cmpl_list: complete list.
  144. *
  145. * Return: 0, OK; otherwise, error.
  146. */
  147. static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
  148. struct list_head *cmpl_list)
  149. {
  150. struct mei_device *dev = cl->dev;
  151. u32 msg_slots;
  152. int slots;
  153. int ret;
  154. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_response));
  155. slots = mei_hbuf_empty_slots(dev);
  156. if (slots < 0)
  157. return -EOVERFLOW;
  158. if ((u32)slots < msg_slots)
  159. return -EMSGSIZE;
  160. ret = mei_hbm_cl_disconnect_rsp(dev, cl);
  161. list_move_tail(&cb->list, cmpl_list);
  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 list_head *cmpl_list)
  176. {
  177. struct mei_device *dev = cl->dev;
  178. u32 msg_slots;
  179. int slots;
  180. int ret;
  181. if (!list_empty(&cl->rd_pending))
  182. return 0;
  183. msg_slots = mei_hbm2slots(sizeof(struct hbm_flow_control));
  184. slots = mei_hbuf_empty_slots(dev);
  185. if (slots < 0)
  186. return -EOVERFLOW;
  187. if ((u32)slots < msg_slots)
  188. return -EMSGSIZE;
  189. ret = mei_hbm_cl_flow_control_req(dev, cl);
  190. if (ret) {
  191. cl->status = ret;
  192. cb->buf_idx = 0;
  193. list_move_tail(&cb->list, cmpl_list);
  194. return ret;
  195. }
  196. list_move_tail(&cb->list, &cl->rd_pending);
  197. return 0;
  198. }
  199. static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
  200. {
  201. return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
  202. }
  203. static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
  204. {
  205. return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
  206. }
  207. static inline int hdr_is_valid(u32 msg_hdr)
  208. {
  209. struct mei_msg_hdr *mei_hdr;
  210. mei_hdr = (struct mei_msg_hdr *)&msg_hdr;
  211. if (!msg_hdr || mei_hdr->reserved)
  212. return -EBADMSG;
  213. return 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 list_head *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. ret = hdr_is_valid(dev->rd_msg_hdr);
  236. if (ret) {
  237. dev_err(dev->dev, "corrupted message header 0x%08X\n",
  238. dev->rd_msg_hdr);
  239. goto end;
  240. }
  241. }
  242. mei_hdr = (struct mei_msg_hdr *)&dev->rd_msg_hdr;
  243. dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  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. * On power down client may be force cleaned,
  273. * silently discard such messages
  274. */
  275. if (hdr_is_fixed(mei_hdr) ||
  276. dev->dev_state == MEI_DEV_POWER_DOWN) {
  277. mei_irq_discard_msg(dev, mei_hdr);
  278. ret = 0;
  279. goto reset_slots;
  280. }
  281. dev_err(dev->dev, "no destination client found 0x%08X\n",
  282. dev->rd_msg_hdr);
  283. ret = -EBADMSG;
  284. goto end;
  285. }
  286. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  287. reset_slots:
  288. /* reset the number of slots and header */
  289. *slots = mei_count_full_read_slots(dev);
  290. dev->rd_msg_hdr = 0;
  291. if (*slots == -EOVERFLOW) {
  292. /* overflow - reset */
  293. dev_err(dev->dev, "resetting due to slots overflow.\n");
  294. /* set the event since message has been read */
  295. ret = -ERANGE;
  296. goto end;
  297. }
  298. end:
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(mei_irq_read_handler);
  302. /**
  303. * mei_irq_write_handler - dispatch write requests
  304. * after irq received
  305. *
  306. * @dev: the device structure
  307. * @cmpl_list: An instance of our list structure
  308. *
  309. * Return: 0 on success, <0 on failure.
  310. */
  311. int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list)
  312. {
  313. struct mei_cl *cl;
  314. struct mei_cl_cb *cb, *next;
  315. s32 slots;
  316. int ret;
  317. if (!mei_hbuf_acquire(dev))
  318. return 0;
  319. slots = mei_hbuf_empty_slots(dev);
  320. if (slots < 0)
  321. return -EOVERFLOW;
  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_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) {
  327. cl = cb->cl;
  328. cl->status = 0;
  329. cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
  330. cl->writing_state = MEI_WRITE_COMPLETE;
  331. list_move_tail(&cb->list, cmpl_list);
  332. }
  333. /* complete control write list CB */
  334. dev_dbg(dev->dev, "complete control write list cb.\n");
  335. list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) {
  336. cl = cb->cl;
  337. switch (cb->fop_type) {
  338. case MEI_FOP_DISCONNECT:
  339. /* send disconnect message */
  340. ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
  341. if (ret)
  342. return ret;
  343. break;
  344. case MEI_FOP_READ:
  345. /* send flow control message */
  346. ret = mei_cl_irq_read(cl, cb, cmpl_list);
  347. if (ret)
  348. return ret;
  349. break;
  350. case MEI_FOP_CONNECT:
  351. /* connect message */
  352. ret = mei_cl_irq_connect(cl, cb, cmpl_list);
  353. if (ret)
  354. return ret;
  355. break;
  356. case MEI_FOP_DISCONNECT_RSP:
  357. /* send disconnect resp */
  358. ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
  359. if (ret)
  360. return ret;
  361. break;
  362. case MEI_FOP_NOTIFY_START:
  363. case MEI_FOP_NOTIFY_STOP:
  364. ret = mei_cl_irq_notify(cl, cb, cmpl_list);
  365. if (ret)
  366. return ret;
  367. break;
  368. default:
  369. BUG();
  370. }
  371. }
  372. /* complete write list CB */
  373. dev_dbg(dev->dev, "complete write list cb.\n");
  374. list_for_each_entry_safe(cb, next, &dev->write_list, list) {
  375. cl = cb->cl;
  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. #define MEI_STALL_TIMER_FREQ (2 * HZ)
  401. /**
  402. * mei_schedule_stall_timer - re-arm stall_timer work
  403. *
  404. * Schedule stall timer
  405. *
  406. * @dev: the device structure
  407. */
  408. void mei_schedule_stall_timer(struct mei_device *dev)
  409. {
  410. schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ);
  411. }
  412. /**
  413. * mei_timer - timer function.
  414. *
  415. * @work: pointer to the work_struct structure
  416. *
  417. */
  418. void mei_timer(struct work_struct *work)
  419. {
  420. struct mei_cl *cl;
  421. struct mei_device *dev = container_of(work,
  422. struct mei_device, timer_work.work);
  423. bool reschedule_timer = false;
  424. mutex_lock(&dev->device_lock);
  425. /* Catch interrupt stalls during HBM init handshake */
  426. if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
  427. dev->hbm_state != MEI_HBM_IDLE) {
  428. if (dev->init_clients_timer) {
  429. if (--dev->init_clients_timer == 0) {
  430. dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
  431. dev->hbm_state);
  432. mei_reset(dev);
  433. goto out;
  434. }
  435. reschedule_timer = true;
  436. }
  437. }
  438. if (dev->dev_state != MEI_DEV_ENABLED)
  439. goto out;
  440. /*** connect/disconnect timeouts ***/
  441. list_for_each_entry(cl, &dev->file_list, link) {
  442. if (cl->timer_count) {
  443. if (--cl->timer_count == 0) {
  444. dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
  445. mei_connect_timeout(cl);
  446. goto out;
  447. }
  448. reschedule_timer = true;
  449. }
  450. }
  451. out:
  452. if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer)
  453. mei_schedule_stall_timer(dev);
  454. mutex_unlock(&dev->device_lock);
  455. }