interrupt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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/pci.h>
  18. #include <linux/kthread.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/fs.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/mei.h>
  23. #include "mei_dev.h"
  24. #include "hbm.h"
  25. #include "hw-me.h"
  26. #include "client.h"
  27. /**
  28. * mei_irq_compl_handler - dispatch complete handelers
  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(&cb->list);
  41. if (!cl)
  42. continue;
  43. dev_dbg(&dev->pdev->dev, "completing call back.\n");
  44. if (cl == &dev->iamthif_cl)
  45. mei_amthif_complete(dev, cb);
  46. else
  47. mei_cl_complete(cl, cb);
  48. }
  49. }
  50. EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
  51. /**
  52. * mei_cl_hbm_equal - check if hbm is addressed to the client
  53. *
  54. * @cl: host client
  55. * @mei_hdr: header of mei client message
  56. *
  57. * returns true if matches, false otherwise
  58. */
  59. static inline int mei_cl_hbm_equal(struct mei_cl *cl,
  60. struct mei_msg_hdr *mei_hdr)
  61. {
  62. return cl->host_client_id == mei_hdr->host_addr &&
  63. cl->me_client_id == mei_hdr->me_addr;
  64. }
  65. /**
  66. * mei_cl_is_reading - checks if the client
  67. is the one to read this message
  68. *
  69. * @cl: mei client
  70. * @mei_hdr: header of mei message
  71. *
  72. * returns true on match and false otherwise
  73. */
  74. static bool mei_cl_is_reading(struct mei_cl *cl, struct mei_msg_hdr *mei_hdr)
  75. {
  76. return mei_cl_hbm_equal(cl, mei_hdr) &&
  77. cl->state == MEI_FILE_CONNECTED &&
  78. cl->reading_state != MEI_READ_COMPLETE;
  79. }
  80. /**
  81. * mei_irq_read_client_message - process client message
  82. *
  83. * @dev: the device structure
  84. * @mei_hdr: header of mei client message
  85. * @complete_list: An instance of our list structure
  86. *
  87. * returns 0 on success, <0 on failure.
  88. */
  89. static int mei_cl_irq_read_msg(struct mei_device *dev,
  90. struct mei_msg_hdr *mei_hdr,
  91. struct mei_cl_cb *complete_list)
  92. {
  93. struct mei_cl *cl;
  94. struct mei_cl_cb *cb, *next;
  95. unsigned char *buffer = NULL;
  96. list_for_each_entry_safe(cb, next, &dev->read_list.list, list) {
  97. cl = cb->cl;
  98. if (!cl || !mei_cl_is_reading(cl, mei_hdr))
  99. continue;
  100. cl->reading_state = MEI_READING;
  101. if (cb->response_buffer.size == 0 ||
  102. cb->response_buffer.data == NULL) {
  103. cl_err(dev, cl, "response buffer is not allocated.\n");
  104. list_del(&cb->list);
  105. return -ENOMEM;
  106. }
  107. if (cb->response_buffer.size < mei_hdr->length + cb->buf_idx) {
  108. cl_dbg(dev, cl, "message overflow. size %d len %d idx %ld\n",
  109. cb->response_buffer.size,
  110. mei_hdr->length, cb->buf_idx);
  111. buffer = krealloc(cb->response_buffer.data,
  112. mei_hdr->length + cb->buf_idx,
  113. GFP_KERNEL);
  114. if (!buffer) {
  115. cl_err(dev, cl, "allocation failed.\n");
  116. list_del(&cb->list);
  117. return -ENOMEM;
  118. }
  119. cb->response_buffer.data = buffer;
  120. cb->response_buffer.size =
  121. mei_hdr->length + cb->buf_idx;
  122. }
  123. buffer = cb->response_buffer.data + cb->buf_idx;
  124. mei_read_slots(dev, buffer, mei_hdr->length);
  125. cb->buf_idx += mei_hdr->length;
  126. if (mei_hdr->msg_complete) {
  127. cl->status = 0;
  128. list_del(&cb->list);
  129. cl_dbg(dev, cl, "completed read length = %lu\n",
  130. cb->buf_idx);
  131. list_add_tail(&cb->list, &complete_list->list);
  132. }
  133. break;
  134. }
  135. dev_dbg(&dev->pdev->dev, "message read\n");
  136. if (!buffer) {
  137. mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
  138. dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
  139. MEI_HDR_PRM(mei_hdr));
  140. }
  141. return 0;
  142. }
  143. /**
  144. * mei_cl_irq_close - processes close related operation from
  145. * interrupt thread context - send disconnect request
  146. *
  147. * @cl: client
  148. * @cb: callback block.
  149. * @slots: free slots.
  150. * @cmpl_list: complete list.
  151. *
  152. * returns 0, OK; otherwise, error.
  153. */
  154. static int mei_cl_irq_close(struct mei_cl *cl, struct mei_cl_cb *cb,
  155. s32 *slots, struct mei_cl_cb *cmpl_list)
  156. {
  157. struct mei_device *dev = cl->dev;
  158. u32 msg_slots =
  159. mei_data2slots(sizeof(struct hbm_client_connect_request));
  160. if (*slots < msg_slots)
  161. return -EMSGSIZE;
  162. *slots -= msg_slots;
  163. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  164. cl->status = 0;
  165. cb->buf_idx = 0;
  166. list_move_tail(&cb->list, &cmpl_list->list);
  167. return -EIO;
  168. }
  169. cl->state = MEI_FILE_DISCONNECTING;
  170. cl->status = 0;
  171. cb->buf_idx = 0;
  172. list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
  173. cl->timer_count = MEI_CONNECT_TIMEOUT;
  174. return 0;
  175. }
  176. /**
  177. * mei_cl_irq_close - 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. * @slots: free slots.
  183. * @cmpl_list: complete list.
  184. *
  185. * returns 0, OK; otherwise, error.
  186. */
  187. static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
  188. s32 *slots, struct mei_cl_cb *cmpl_list)
  189. {
  190. struct mei_device *dev = cl->dev;
  191. u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
  192. if (*slots < msg_slots) {
  193. /* return the cancel routine */
  194. list_del(&cb->list);
  195. return -EMSGSIZE;
  196. }
  197. *slots -= msg_slots;
  198. if (mei_hbm_cl_flow_control_req(dev, cl)) {
  199. cl->status = -ENODEV;
  200. cb->buf_idx = 0;
  201. list_move_tail(&cb->list, &cmpl_list->list);
  202. return -ENODEV;
  203. }
  204. list_move_tail(&cb->list, &dev->read_list.list);
  205. return 0;
  206. }
  207. /**
  208. * mei_cl_irq_ioctl - processes client ioctl related operation from the
  209. * interrupt thread context - send connection request
  210. *
  211. * @cl: client
  212. * @cb: callback block.
  213. * @slots: free slots.
  214. * @cmpl_list: complete list.
  215. *
  216. * returns 0, OK; otherwise, error.
  217. */
  218. static int mei_cl_irq_ioctl(struct mei_cl *cl, struct mei_cl_cb *cb,
  219. s32 *slots, struct mei_cl_cb *cmpl_list)
  220. {
  221. struct mei_device *dev = cl->dev;
  222. u32 msg_slots =
  223. mei_data2slots(sizeof(struct hbm_client_connect_request));
  224. if (*slots < msg_slots) {
  225. /* return the cancel routine */
  226. list_del(&cb->list);
  227. return -EMSGSIZE;
  228. }
  229. *slots -= msg_slots;
  230. cl->state = MEI_FILE_CONNECTING;
  231. if (mei_hbm_cl_connect_req(dev, cl)) {
  232. cl->status = -ENODEV;
  233. cb->buf_idx = 0;
  234. list_del(&cb->list);
  235. return -ENODEV;
  236. }
  237. list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
  238. cl->timer_count = MEI_CONNECT_TIMEOUT;
  239. return 0;
  240. }
  241. /**
  242. * mei_irq_read_handler - bottom half read routine after ISR to
  243. * handle the read processing.
  244. *
  245. * @dev: the device structure
  246. * @cmpl_list: An instance of our list structure
  247. * @slots: slots to read.
  248. *
  249. * returns 0 on success, <0 on failure.
  250. */
  251. int mei_irq_read_handler(struct mei_device *dev,
  252. struct mei_cl_cb *cmpl_list, s32 *slots)
  253. {
  254. struct mei_msg_hdr *mei_hdr;
  255. struct mei_cl *cl_pos = NULL;
  256. struct mei_cl *cl_next = NULL;
  257. int ret = 0;
  258. if (!dev->rd_msg_hdr) {
  259. dev->rd_msg_hdr = mei_read_hdr(dev);
  260. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  261. (*slots)--;
  262. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  263. }
  264. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  265. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  266. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  267. dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
  268. ret = -EBADMSG;
  269. goto end;
  270. }
  271. if (mei_hdr->host_addr || mei_hdr->me_addr) {
  272. list_for_each_entry_safe(cl_pos, cl_next,
  273. &dev->file_list, link) {
  274. dev_dbg(&dev->pdev->dev,
  275. "list_for_each_entry_safe read host"
  276. " client = %d, ME client = %d\n",
  277. cl_pos->host_client_id,
  278. cl_pos->me_client_id);
  279. if (mei_cl_hbm_equal(cl_pos, mei_hdr))
  280. break;
  281. }
  282. if (&cl_pos->link == &dev->file_list) {
  283. dev_dbg(&dev->pdev->dev, "corrupted message header\n");
  284. ret = -EBADMSG;
  285. goto end;
  286. }
  287. }
  288. if (((*slots) * sizeof(u32)) < mei_hdr->length) {
  289. dev_err(&dev->pdev->dev,
  290. "we can't read the message slots =%08x.\n",
  291. *slots);
  292. /* we can't read the message */
  293. ret = -ERANGE;
  294. goto end;
  295. }
  296. /* decide where to read the message too */
  297. if (!mei_hdr->host_addr) {
  298. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
  299. mei_hbm_dispatch(dev, mei_hdr);
  300. dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
  301. } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
  302. (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
  303. (dev->iamthif_state == MEI_IAMTHIF_READING)) {
  304. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
  305. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  306. ret = mei_amthif_irq_read_msg(dev, mei_hdr, cmpl_list);
  307. if (ret)
  308. goto end;
  309. } else {
  310. dev_dbg(&dev->pdev->dev, "call mei_cl_irq_read_msg.\n");
  311. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  312. ret = mei_cl_irq_read_msg(dev, mei_hdr, cmpl_list);
  313. if (ret)
  314. goto end;
  315. }
  316. /* reset the number of slots and header */
  317. *slots = mei_count_full_read_slots(dev);
  318. dev->rd_msg_hdr = 0;
  319. if (*slots == -EOVERFLOW) {
  320. /* overflow - reset */
  321. dev_err(&dev->pdev->dev, "resetting due to slots overflow.\n");
  322. /* set the event since message has been read */
  323. ret = -ERANGE;
  324. goto end;
  325. }
  326. end:
  327. return ret;
  328. }
  329. EXPORT_SYMBOL_GPL(mei_irq_read_handler);
  330. /**
  331. * mei_irq_write_handler - dispatch write requests
  332. * after irq received
  333. *
  334. * @dev: the device structure
  335. * @cmpl_list: An instance of our list structure
  336. *
  337. * returns 0 on success, <0 on failure.
  338. */
  339. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
  340. {
  341. struct mei_cl *cl;
  342. struct mei_cl_cb *cb, *next;
  343. struct mei_cl_cb *list;
  344. s32 slots;
  345. int ret;
  346. if (!mei_hbuf_is_ready(dev)) {
  347. dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
  348. return 0;
  349. }
  350. slots = mei_hbuf_empty_slots(dev);
  351. if (slots <= 0)
  352. return -EMSGSIZE;
  353. /* complete all waiting for write CB */
  354. dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
  355. list = &dev->write_waiting_list;
  356. list_for_each_entry_safe(cb, next, &list->list, list) {
  357. cl = cb->cl;
  358. if (cl == NULL)
  359. continue;
  360. cl->status = 0;
  361. list_del(&cb->list);
  362. if (MEI_WRITING == cl->writing_state &&
  363. cb->fop_type == MEI_FOP_WRITE &&
  364. cl != &dev->iamthif_cl) {
  365. cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
  366. cl->writing_state = MEI_WRITE_COMPLETE;
  367. list_add_tail(&cb->list, &cmpl_list->list);
  368. }
  369. if (cl == &dev->iamthif_cl) {
  370. cl_dbg(dev, cl, "check iamthif flow control.\n");
  371. if (dev->iamthif_flow_control_pending) {
  372. ret = mei_amthif_irq_read(dev, &slots);
  373. if (ret)
  374. return ret;
  375. }
  376. }
  377. }
  378. if (dev->wd_state == MEI_WD_STOPPING) {
  379. dev->wd_state = MEI_WD_IDLE;
  380. wake_up_interruptible(&dev->wait_stop_wd);
  381. }
  382. if (dev->wr_ext_msg.hdr.length) {
  383. mei_write_message(dev, &dev->wr_ext_msg.hdr,
  384. dev->wr_ext_msg.data);
  385. slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
  386. dev->wr_ext_msg.hdr.length = 0;
  387. }
  388. if (dev->dev_state == MEI_DEV_ENABLED) {
  389. if (dev->wd_pending &&
  390. mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
  391. if (mei_wd_send(dev))
  392. dev_dbg(&dev->pdev->dev, "wd send failed.\n");
  393. else if (mei_cl_flow_ctrl_reduce(&dev->wd_cl))
  394. return -ENODEV;
  395. dev->wd_pending = false;
  396. if (dev->wd_state == MEI_WD_RUNNING)
  397. slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
  398. else
  399. slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
  400. }
  401. }
  402. /* complete control write list CB */
  403. dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
  404. list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
  405. cl = cb->cl;
  406. if (!cl) {
  407. list_del(&cb->list);
  408. return -ENODEV;
  409. }
  410. switch (cb->fop_type) {
  411. case MEI_FOP_CLOSE:
  412. /* send disconnect message */
  413. ret = mei_cl_irq_close(cl, cb, &slots, cmpl_list);
  414. if (ret)
  415. return ret;
  416. break;
  417. case MEI_FOP_READ:
  418. /* send flow control message */
  419. ret = mei_cl_irq_read(cl, cb, &slots, cmpl_list);
  420. if (ret)
  421. return ret;
  422. break;
  423. case MEI_FOP_IOCTL:
  424. /* connect message */
  425. if (mei_cl_is_other_connecting(cl))
  426. continue;
  427. ret = mei_cl_irq_ioctl(cl, cb, &slots, cmpl_list);
  428. if (ret)
  429. return ret;
  430. break;
  431. default:
  432. BUG();
  433. }
  434. }
  435. /* complete write list CB */
  436. dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
  437. list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
  438. cl = cb->cl;
  439. if (cl == NULL)
  440. continue;
  441. if (mei_cl_flow_ctrl_creds(cl) <= 0) {
  442. cl_dbg(dev, cl, "No flow control credentials, not sending.\n");
  443. continue;
  444. }
  445. if (cl == &dev->iamthif_cl)
  446. ret = mei_amthif_irq_write_complete(cl, cb,
  447. &slots, cmpl_list);
  448. else
  449. ret = mei_cl_irq_write_complete(cl, cb,
  450. &slots, cmpl_list);
  451. if (ret)
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. EXPORT_SYMBOL_GPL(mei_irq_write_handler);
  457. /**
  458. * mei_timer - timer function.
  459. *
  460. * @work: pointer to the work_struct structure
  461. *
  462. * NOTE: This function is called by timer interrupt work
  463. */
  464. void mei_timer(struct work_struct *work)
  465. {
  466. unsigned long timeout;
  467. struct mei_cl *cl_pos = NULL;
  468. struct mei_cl *cl_next = NULL;
  469. struct mei_cl_cb *cb_pos = NULL;
  470. struct mei_cl_cb *cb_next = NULL;
  471. struct mei_device *dev = container_of(work,
  472. struct mei_device, timer_work.work);
  473. mutex_lock(&dev->device_lock);
  474. if (dev->dev_state != MEI_DEV_ENABLED) {
  475. if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
  476. if (dev->init_clients_timer) {
  477. if (--dev->init_clients_timer == 0) {
  478. dev_err(&dev->pdev->dev, "reset: init clients timeout hbm_state = %d.\n",
  479. dev->hbm_state);
  480. mei_reset(dev, 1);
  481. }
  482. }
  483. }
  484. goto out;
  485. }
  486. /*** connect/disconnect timeouts ***/
  487. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  488. if (cl_pos->timer_count) {
  489. if (--cl_pos->timer_count == 0) {
  490. dev_err(&dev->pdev->dev, "reset: connect/disconnect timeout.\n");
  491. mei_reset(dev, 1);
  492. goto out;
  493. }
  494. }
  495. }
  496. if (dev->iamthif_stall_timer) {
  497. if (--dev->iamthif_stall_timer == 0) {
  498. dev_err(&dev->pdev->dev, "reset: amthif hanged.\n");
  499. mei_reset(dev, 1);
  500. dev->iamthif_msg_buf_size = 0;
  501. dev->iamthif_msg_buf_index = 0;
  502. dev->iamthif_canceled = false;
  503. dev->iamthif_ioctl = true;
  504. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  505. dev->iamthif_timer = 0;
  506. mei_io_cb_free(dev->iamthif_current_cb);
  507. dev->iamthif_current_cb = NULL;
  508. dev->iamthif_file_object = NULL;
  509. mei_amthif_run_next_cmd(dev);
  510. }
  511. }
  512. if (dev->iamthif_timer) {
  513. timeout = dev->iamthif_timer +
  514. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  515. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  516. dev->iamthif_timer);
  517. dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
  518. dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
  519. if (time_after(jiffies, timeout)) {
  520. /*
  521. * User didn't read the AMTHI data on time (15sec)
  522. * freeing AMTHI for other requests
  523. */
  524. dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
  525. list_for_each_entry_safe(cb_pos, cb_next,
  526. &dev->amthif_rd_complete_list.list, list) {
  527. cl_pos = cb_pos->file_object->private_data;
  528. /* Finding the AMTHI entry. */
  529. if (cl_pos == &dev->iamthif_cl)
  530. list_del(&cb_pos->list);
  531. }
  532. mei_io_cb_free(dev->iamthif_current_cb);
  533. dev->iamthif_current_cb = NULL;
  534. dev->iamthif_file_object->private_data = NULL;
  535. dev->iamthif_file_object = NULL;
  536. dev->iamthif_timer = 0;
  537. mei_amthif_run_next_cmd(dev);
  538. }
  539. }
  540. out:
  541. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  542. mutex_unlock(&dev->device_lock);
  543. }