interrupt.c 13 KB

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