interrupt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/pci.h>
  17. #include <linux/kthread.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/fs.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "interface.h"
  24. /**
  25. * _mei_cmpl - processes completed operation.
  26. *
  27. * @cl: private data of the file object.
  28. * @cb_pos: callback block.
  29. */
  30. static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
  31. {
  32. if (cb_pos->fop_type == MEI_FOP_WRITE) {
  33. mei_io_cb_free(cb_pos);
  34. cb_pos = NULL;
  35. cl->writing_state = MEI_WRITE_COMPLETE;
  36. if (waitqueue_active(&cl->tx_wait))
  37. wake_up_interruptible(&cl->tx_wait);
  38. } else if (cb_pos->fop_type == MEI_FOP_READ &&
  39. MEI_READING == cl->reading_state) {
  40. cl->reading_state = MEI_READ_COMPLETE;
  41. if (waitqueue_active(&cl->rx_wait))
  42. wake_up_interruptible(&cl->rx_wait);
  43. }
  44. }
  45. /**
  46. * _mei_irq_thread_state_ok - checks if mei header matches file private data
  47. *
  48. * @cl: private data of the file object
  49. * @mei_hdr: header of mei client message
  50. *
  51. * returns !=0 if matches, 0 if no match.
  52. */
  53. static int _mei_irq_thread_state_ok(struct mei_cl *cl,
  54. struct mei_msg_hdr *mei_hdr)
  55. {
  56. return (cl->host_client_id == mei_hdr->host_addr &&
  57. cl->me_client_id == mei_hdr->me_addr &&
  58. cl->state == MEI_FILE_CONNECTED &&
  59. MEI_READ_COMPLETE != cl->reading_state);
  60. }
  61. /**
  62. * mei_irq_thread_read_client_message - bottom half read routine after ISR to
  63. * handle the read mei client message data processing.
  64. *
  65. * @complete_list: An instance of our list structure
  66. * @dev: the device structure
  67. * @mei_hdr: header of mei client message
  68. *
  69. * returns 0 on success, <0 on failure.
  70. */
  71. static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
  72. struct mei_device *dev,
  73. struct mei_msg_hdr *mei_hdr)
  74. {
  75. struct mei_cl *cl;
  76. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  77. unsigned char *buffer = NULL;
  78. dev_dbg(&dev->pdev->dev, "start client msg\n");
  79. if (list_empty(&dev->read_list.list))
  80. goto quit;
  81. list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
  82. cl = cb_pos->cl;
  83. if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
  84. cl->reading_state = MEI_READING;
  85. buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
  86. if (cb_pos->response_buffer.size <
  87. mei_hdr->length + cb_pos->buf_idx) {
  88. dev_dbg(&dev->pdev->dev, "message overflow.\n");
  89. list_del(&cb_pos->list);
  90. return -ENOMEM;
  91. }
  92. if (buffer)
  93. mei_read_slots(dev, buffer, mei_hdr->length);
  94. cb_pos->buf_idx += mei_hdr->length;
  95. if (mei_hdr->msg_complete) {
  96. cl->status = 0;
  97. list_del(&cb_pos->list);
  98. dev_dbg(&dev->pdev->dev,
  99. "completed read H cl = %d, ME cl = %d, length = %lu\n",
  100. cl->host_client_id,
  101. cl->me_client_id,
  102. cb_pos->buf_idx);
  103. list_add_tail(&cb_pos->list,
  104. &complete_list->list);
  105. }
  106. break;
  107. }
  108. }
  109. quit:
  110. dev_dbg(&dev->pdev->dev, "message read\n");
  111. if (!buffer) {
  112. mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
  113. dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
  114. MEI_HDR_PRM(mei_hdr));
  115. }
  116. return 0;
  117. }
  118. /**
  119. * _mei_irq_thread_close - processes close related operation.
  120. *
  121. * @dev: the device structure.
  122. * @slots: free slots.
  123. * @cb_pos: callback block.
  124. * @cl: private data of the file object.
  125. * @cmpl_list: complete list.
  126. *
  127. * returns 0, OK; otherwise, error.
  128. */
  129. static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
  130. struct mei_cl_cb *cb_pos,
  131. struct mei_cl *cl,
  132. struct mei_cl_cb *cmpl_list)
  133. {
  134. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  135. sizeof(struct hbm_client_connect_request)))
  136. return -EBADMSG;
  137. *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
  138. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  139. cl->status = 0;
  140. cb_pos->buf_idx = 0;
  141. list_move_tail(&cb_pos->list, &cmpl_list->list);
  142. return -EMSGSIZE;
  143. } else {
  144. cl->state = MEI_FILE_DISCONNECTING;
  145. cl->status = 0;
  146. cb_pos->buf_idx = 0;
  147. list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
  148. cl->timer_count = MEI_CONNECT_TIMEOUT;
  149. }
  150. return 0;
  151. }
  152. /**
  153. * is_treat_specially_client - checks if the message belongs
  154. * to the file private data.
  155. *
  156. * @cl: private data of the file object
  157. * @rs: connect response bus message
  158. *
  159. */
  160. static bool is_treat_specially_client(struct mei_cl *cl,
  161. struct hbm_client_connect_response *rs)
  162. {
  163. if (cl->host_client_id == rs->host_addr &&
  164. cl->me_client_id == rs->me_addr) {
  165. if (!rs->status) {
  166. cl->state = MEI_FILE_CONNECTED;
  167. cl->status = 0;
  168. } else {
  169. cl->state = MEI_FILE_DISCONNECTED;
  170. cl->status = -ENODEV;
  171. }
  172. cl->timer_count = 0;
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * mei_client_connect_response - connects to response irq routine
  179. *
  180. * @dev: the device structure
  181. * @rs: connect response bus message
  182. */
  183. void mei_client_connect_response(struct mei_device *dev,
  184. struct hbm_client_connect_response *rs)
  185. {
  186. struct mei_cl *cl;
  187. struct mei_cl_cb *pos = NULL, *next = NULL;
  188. dev_dbg(&dev->pdev->dev,
  189. "connect_response:\n"
  190. "ME Client = %d\n"
  191. "Host Client = %d\n"
  192. "Status = %d\n",
  193. rs->me_addr,
  194. rs->host_addr,
  195. rs->status);
  196. /* if WD or iamthif client treat specially */
  197. if (is_treat_specially_client(&(dev->wd_cl), rs)) {
  198. dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
  199. mei_watchdog_register(dev);
  200. return;
  201. }
  202. if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
  203. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  204. return;
  205. }
  206. list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
  207. cl = pos->cl;
  208. if (!cl) {
  209. list_del(&pos->list);
  210. return;
  211. }
  212. if (pos->fop_type == MEI_FOP_IOCTL) {
  213. if (is_treat_specially_client(cl, rs)) {
  214. list_del(&pos->list);
  215. cl->status = 0;
  216. cl->timer_count = 0;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * mei_client_disconnect_response - disconnects from response irq routine
  224. *
  225. * @dev: the device structure
  226. * @rs: disconnect response bus message
  227. */
  228. void mei_client_disconnect_response(struct mei_device *dev,
  229. struct hbm_client_connect_response *rs)
  230. {
  231. struct mei_cl *cl;
  232. struct mei_cl_cb *pos = NULL, *next = NULL;
  233. dev_dbg(&dev->pdev->dev,
  234. "disconnect_response:\n"
  235. "ME Client = %d\n"
  236. "Host Client = %d\n"
  237. "Status = %d\n",
  238. rs->me_addr,
  239. rs->host_addr,
  240. rs->status);
  241. list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
  242. cl = pos->cl;
  243. if (!cl) {
  244. list_del(&pos->list);
  245. return;
  246. }
  247. dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
  248. if (cl->host_client_id == rs->host_addr &&
  249. cl->me_client_id == rs->me_addr) {
  250. list_del(&pos->list);
  251. if (!rs->status)
  252. cl->state = MEI_FILE_DISCONNECTED;
  253. cl->status = 0;
  254. cl->timer_count = 0;
  255. break;
  256. }
  257. }
  258. }
  259. /**
  260. * same_flow_addr - tells if they have the same address.
  261. *
  262. * @file: private data of the file object.
  263. * @flow: flow control.
  264. *
  265. * returns !=0, same; 0,not.
  266. */
  267. static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
  268. {
  269. return (cl->host_client_id == flow->host_addr &&
  270. cl->me_client_id == flow->me_addr);
  271. }
  272. /**
  273. * add_single_flow_creds - adds single buffer credentials.
  274. *
  275. * @file: private data ot the file object.
  276. * @flow: flow control.
  277. */
  278. static void add_single_flow_creds(struct mei_device *dev,
  279. struct hbm_flow_control *flow)
  280. {
  281. struct mei_me_client *client;
  282. int i;
  283. for (i = 0; i < dev->me_clients_num; i++) {
  284. client = &dev->me_clients[i];
  285. if (client && flow->me_addr == client->client_id) {
  286. if (client->props.single_recv_buf) {
  287. client->mei_flow_ctrl_creds++;
  288. dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
  289. flow->me_addr);
  290. dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
  291. client->mei_flow_ctrl_creds);
  292. } else {
  293. BUG(); /* error in flow control */
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * mei_client_flow_control_response - flow control response irq routine
  300. *
  301. * @dev: the device structure
  302. * @flow_control: flow control response bus message
  303. */
  304. void mei_client_flow_control_response(struct mei_device *dev,
  305. struct hbm_flow_control *flow_control)
  306. {
  307. struct mei_cl *cl_pos = NULL;
  308. struct mei_cl *cl_next = NULL;
  309. if (!flow_control->host_addr) {
  310. /* single receive buffer */
  311. add_single_flow_creds(dev, flow_control);
  312. } else {
  313. /* normal connection */
  314. list_for_each_entry_safe(cl_pos, cl_next,
  315. &dev->file_list, link) {
  316. dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
  317. dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
  318. cl_pos->host_client_id,
  319. cl_pos->me_client_id);
  320. dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
  321. flow_control->host_addr,
  322. flow_control->me_addr);
  323. if (same_flow_addr(cl_pos, flow_control)) {
  324. dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
  325. flow_control->host_addr,
  326. flow_control->me_addr);
  327. cl_pos->mei_flow_ctrl_creds++;
  328. dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
  329. cl_pos->mei_flow_ctrl_creds);
  330. break;
  331. }
  332. }
  333. }
  334. }
  335. /**
  336. * _mei_hb_read - processes read related operation.
  337. *
  338. * @dev: the device structure.
  339. * @slots: free slots.
  340. * @cb_pos: callback block.
  341. * @cl: private data of the file object.
  342. * @cmpl_list: complete list.
  343. *
  344. * returns 0, OK; otherwise, error.
  345. */
  346. static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
  347. struct mei_cl_cb *cb_pos,
  348. struct mei_cl *cl,
  349. struct mei_cl_cb *cmpl_list)
  350. {
  351. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  352. sizeof(struct hbm_flow_control))) {
  353. /* return the cancel routine */
  354. list_del(&cb_pos->list);
  355. return -EBADMSG;
  356. }
  357. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  358. if (mei_hbm_cl_flow_control_req(dev, cl)) {
  359. cl->status = -ENODEV;
  360. cb_pos->buf_idx = 0;
  361. list_move_tail(&cb_pos->list, &cmpl_list->list);
  362. return -ENODEV;
  363. }
  364. list_move_tail(&cb_pos->list, &dev->read_list.list);
  365. return 0;
  366. }
  367. /**
  368. * _mei_irq_thread_ioctl - processes ioctl related operation.
  369. *
  370. * @dev: the device structure.
  371. * @slots: free slots.
  372. * @cb_pos: callback block.
  373. * @cl: private data of the file object.
  374. * @cmpl_list: complete list.
  375. *
  376. * returns 0, OK; otherwise, error.
  377. */
  378. static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
  379. struct mei_cl_cb *cb_pos,
  380. struct mei_cl *cl,
  381. struct mei_cl_cb *cmpl_list)
  382. {
  383. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  384. sizeof(struct hbm_client_connect_request))) {
  385. /* return the cancel routine */
  386. list_del(&cb_pos->list);
  387. return -EBADMSG;
  388. }
  389. cl->state = MEI_FILE_CONNECTING;
  390. *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
  391. if (mei_hbm_cl_connect_req(dev, cl)) {
  392. cl->status = -ENODEV;
  393. cb_pos->buf_idx = 0;
  394. list_del(&cb_pos->list);
  395. return -ENODEV;
  396. } else {
  397. list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
  398. cl->timer_count = MEI_CONNECT_TIMEOUT;
  399. }
  400. return 0;
  401. }
  402. /**
  403. * mei_irq_thread_write_complete - write messages to device.
  404. *
  405. * @dev: the device structure.
  406. * @slots: free slots.
  407. * @cb: callback block.
  408. * @cmpl_list: complete list.
  409. *
  410. * returns 0, OK; otherwise, error.
  411. */
  412. static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
  413. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
  414. {
  415. struct mei_msg_hdr mei_hdr;
  416. struct mei_cl *cl = cb->cl;
  417. size_t len = cb->request_buffer.size - cb->buf_idx;
  418. size_t msg_slots = mei_data2slots(len);
  419. mei_hdr.host_addr = cl->host_client_id;
  420. mei_hdr.me_addr = cl->me_client_id;
  421. mei_hdr.reserved = 0;
  422. if (*slots >= msg_slots) {
  423. mei_hdr.length = len;
  424. mei_hdr.msg_complete = 1;
  425. /* Split the message only if we can write the whole host buffer */
  426. } else if (*slots == dev->hbuf_depth) {
  427. msg_slots = *slots;
  428. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  429. mei_hdr.length = len;
  430. mei_hdr.msg_complete = 0;
  431. } else {
  432. /* wait for next time the host buffer is empty */
  433. return 0;
  434. }
  435. dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
  436. cb->request_buffer.size, cb->buf_idx);
  437. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
  438. *slots -= msg_slots;
  439. if (mei_write_message(dev, &mei_hdr,
  440. cb->request_buffer.data + cb->buf_idx)) {
  441. cl->status = -ENODEV;
  442. list_move_tail(&cb->list, &cmpl_list->list);
  443. return -ENODEV;
  444. }
  445. if (mei_flow_ctrl_reduce(dev, cl))
  446. return -ENODEV;
  447. cl->status = 0;
  448. cb->buf_idx += mei_hdr.length;
  449. if (mei_hdr.msg_complete)
  450. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  451. return 0;
  452. }
  453. /**
  454. * mei_irq_thread_read_handler - bottom half read routine after ISR to
  455. * handle the read processing.
  456. *
  457. * @cmpl_list: An instance of our list structure
  458. * @dev: the device structure
  459. * @slots: slots to read.
  460. *
  461. * returns 0 on success, <0 on failure.
  462. */
  463. static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
  464. struct mei_device *dev,
  465. s32 *slots)
  466. {
  467. struct mei_msg_hdr *mei_hdr;
  468. struct mei_cl *cl_pos = NULL;
  469. struct mei_cl *cl_next = NULL;
  470. int ret = 0;
  471. if (!dev->rd_msg_hdr) {
  472. dev->rd_msg_hdr = mei_mecbrw_read(dev);
  473. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  474. (*slots)--;
  475. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  476. }
  477. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  478. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  479. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  480. dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
  481. ret = -EBADMSG;
  482. goto end;
  483. }
  484. if (mei_hdr->host_addr || mei_hdr->me_addr) {
  485. list_for_each_entry_safe(cl_pos, cl_next,
  486. &dev->file_list, link) {
  487. dev_dbg(&dev->pdev->dev,
  488. "list_for_each_entry_safe read host"
  489. " client = %d, ME client = %d\n",
  490. cl_pos->host_client_id,
  491. cl_pos->me_client_id);
  492. if (cl_pos->host_client_id == mei_hdr->host_addr &&
  493. cl_pos->me_client_id == mei_hdr->me_addr)
  494. break;
  495. }
  496. if (&cl_pos->link == &dev->file_list) {
  497. dev_dbg(&dev->pdev->dev, "corrupted message header\n");
  498. ret = -EBADMSG;
  499. goto end;
  500. }
  501. }
  502. if (((*slots) * sizeof(u32)) < mei_hdr->length) {
  503. dev_dbg(&dev->pdev->dev,
  504. "we can't read the message slots =%08x.\n",
  505. *slots);
  506. /* we can't read the message */
  507. ret = -ERANGE;
  508. goto end;
  509. }
  510. /* decide where to read the message too */
  511. if (!mei_hdr->host_addr) {
  512. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
  513. mei_hbm_dispatch(dev, mei_hdr);
  514. dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
  515. } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
  516. (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
  517. (dev->iamthif_state == MEI_IAMTHIF_READING)) {
  518. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
  519. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
  520. ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
  521. if (ret)
  522. goto end;
  523. } else {
  524. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
  525. ret = mei_irq_thread_read_client_message(cmpl_list,
  526. dev, mei_hdr);
  527. if (ret)
  528. goto end;
  529. }
  530. /* reset the number of slots and header */
  531. *slots = mei_count_full_read_slots(dev);
  532. dev->rd_msg_hdr = 0;
  533. if (*slots == -EOVERFLOW) {
  534. /* overflow - reset */
  535. dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
  536. /* set the event since message has been read */
  537. ret = -ERANGE;
  538. goto end;
  539. }
  540. end:
  541. return ret;
  542. }
  543. /**
  544. * mei_irq_thread_write_handler - bottom half write routine after
  545. * ISR to handle the write processing.
  546. *
  547. * @dev: the device structure
  548. * @cmpl_list: An instance of our list structure
  549. *
  550. * returns 0 on success, <0 on failure.
  551. */
  552. static int mei_irq_thread_write_handler(struct mei_device *dev,
  553. struct mei_cl_cb *cmpl_list)
  554. {
  555. struct mei_cl *cl;
  556. struct mei_cl_cb *pos = NULL, *next = NULL;
  557. struct mei_cl_cb *list;
  558. s32 slots;
  559. int ret;
  560. if (!mei_hbuf_is_empty(dev)) {
  561. dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
  562. return 0;
  563. }
  564. slots = mei_hbuf_empty_slots(dev);
  565. if (slots <= 0)
  566. return -EMSGSIZE;
  567. /* complete all waiting for write CB */
  568. dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
  569. list = &dev->write_waiting_list;
  570. list_for_each_entry_safe(pos, next, &list->list, list) {
  571. cl = pos->cl;
  572. if (cl == NULL)
  573. continue;
  574. cl->status = 0;
  575. list_del(&pos->list);
  576. if (MEI_WRITING == cl->writing_state &&
  577. pos->fop_type == MEI_FOP_WRITE &&
  578. cl != &dev->iamthif_cl) {
  579. dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
  580. cl->writing_state = MEI_WRITE_COMPLETE;
  581. list_add_tail(&pos->list, &cmpl_list->list);
  582. }
  583. if (cl == &dev->iamthif_cl) {
  584. dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
  585. if (dev->iamthif_flow_control_pending) {
  586. ret = mei_amthif_irq_read(dev, &slots);
  587. if (ret)
  588. return ret;
  589. }
  590. }
  591. }
  592. if (dev->wd_state == MEI_WD_STOPPING) {
  593. dev->wd_state = MEI_WD_IDLE;
  594. wake_up_interruptible(&dev->wait_stop_wd);
  595. }
  596. if (dev->wr_ext_msg.hdr.length) {
  597. mei_write_message(dev, &dev->wr_ext_msg.hdr,
  598. dev->wr_ext_msg.data);
  599. slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
  600. dev->wr_ext_msg.hdr.length = 0;
  601. }
  602. if (dev->dev_state == MEI_DEV_ENABLED) {
  603. if (dev->wd_pending &&
  604. mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
  605. if (mei_wd_send(dev))
  606. dev_dbg(&dev->pdev->dev, "wd send failed.\n");
  607. else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
  608. return -ENODEV;
  609. dev->wd_pending = false;
  610. if (dev->wd_state == MEI_WD_RUNNING)
  611. slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
  612. else
  613. slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
  614. }
  615. }
  616. /* complete control write list CB */
  617. dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
  618. list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
  619. cl = pos->cl;
  620. if (!cl) {
  621. list_del(&pos->list);
  622. return -ENODEV;
  623. }
  624. switch (pos->fop_type) {
  625. case MEI_FOP_CLOSE:
  626. /* send disconnect message */
  627. ret = _mei_irq_thread_close(dev, &slots, pos,
  628. cl, cmpl_list);
  629. if (ret)
  630. return ret;
  631. break;
  632. case MEI_FOP_READ:
  633. /* send flow control message */
  634. ret = _mei_irq_thread_read(dev, &slots, pos,
  635. cl, cmpl_list);
  636. if (ret)
  637. return ret;
  638. break;
  639. case MEI_FOP_IOCTL:
  640. /* connect message */
  641. if (mei_other_client_is_connecting(dev, cl))
  642. continue;
  643. ret = _mei_irq_thread_ioctl(dev, &slots, pos,
  644. cl, cmpl_list);
  645. if (ret)
  646. return ret;
  647. break;
  648. default:
  649. BUG();
  650. }
  651. }
  652. /* complete write list CB */
  653. dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
  654. list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
  655. cl = pos->cl;
  656. if (cl == NULL)
  657. continue;
  658. if (mei_flow_ctrl_creds(dev, cl) <= 0) {
  659. dev_dbg(&dev->pdev->dev,
  660. "No flow control credentials for client %d, not sending.\n",
  661. cl->host_client_id);
  662. continue;
  663. }
  664. if (cl == &dev->iamthif_cl)
  665. ret = mei_amthif_irq_write_complete(dev, &slots,
  666. pos, cmpl_list);
  667. else
  668. ret = mei_irq_thread_write_complete(dev, &slots, pos,
  669. cmpl_list);
  670. if (ret)
  671. return ret;
  672. }
  673. return 0;
  674. }
  675. /**
  676. * mei_timer - timer function.
  677. *
  678. * @work: pointer to the work_struct structure
  679. *
  680. * NOTE: This function is called by timer interrupt work
  681. */
  682. void mei_timer(struct work_struct *work)
  683. {
  684. unsigned long timeout;
  685. struct mei_cl *cl_pos = NULL;
  686. struct mei_cl *cl_next = NULL;
  687. struct mei_cl_cb *cb_pos = NULL;
  688. struct mei_cl_cb *cb_next = NULL;
  689. struct mei_device *dev = container_of(work,
  690. struct mei_device, timer_work.work);
  691. mutex_lock(&dev->device_lock);
  692. if (dev->dev_state != MEI_DEV_ENABLED) {
  693. if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
  694. if (dev->init_clients_timer) {
  695. if (--dev->init_clients_timer == 0) {
  696. dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
  697. dev->init_clients_state);
  698. mei_reset(dev, 1);
  699. }
  700. }
  701. }
  702. goto out;
  703. }
  704. /*** connect/disconnect timeouts ***/
  705. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  706. if (cl_pos->timer_count) {
  707. if (--cl_pos->timer_count == 0) {
  708. dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
  709. mei_reset(dev, 1);
  710. goto out;
  711. }
  712. }
  713. }
  714. if (dev->iamthif_stall_timer) {
  715. if (--dev->iamthif_stall_timer == 0) {
  716. dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
  717. mei_reset(dev, 1);
  718. dev->iamthif_msg_buf_size = 0;
  719. dev->iamthif_msg_buf_index = 0;
  720. dev->iamthif_canceled = false;
  721. dev->iamthif_ioctl = true;
  722. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  723. dev->iamthif_timer = 0;
  724. mei_io_cb_free(dev->iamthif_current_cb);
  725. dev->iamthif_current_cb = NULL;
  726. dev->iamthif_file_object = NULL;
  727. mei_amthif_run_next_cmd(dev);
  728. }
  729. }
  730. if (dev->iamthif_timer) {
  731. timeout = dev->iamthif_timer +
  732. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  733. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  734. dev->iamthif_timer);
  735. dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
  736. dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
  737. if (time_after(jiffies, timeout)) {
  738. /*
  739. * User didn't read the AMTHI data on time (15sec)
  740. * freeing AMTHI for other requests
  741. */
  742. dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
  743. list_for_each_entry_safe(cb_pos, cb_next,
  744. &dev->amthif_rd_complete_list.list, list) {
  745. cl_pos = cb_pos->file_object->private_data;
  746. /* Finding the AMTHI entry. */
  747. if (cl_pos == &dev->iamthif_cl)
  748. list_del(&cb_pos->list);
  749. }
  750. mei_io_cb_free(dev->iamthif_current_cb);
  751. dev->iamthif_current_cb = NULL;
  752. dev->iamthif_file_object->private_data = NULL;
  753. dev->iamthif_file_object = NULL;
  754. dev->iamthif_timer = 0;
  755. mei_amthif_run_next_cmd(dev);
  756. }
  757. }
  758. out:
  759. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  760. mutex_unlock(&dev->device_lock);
  761. }
  762. /**
  763. * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
  764. * processing.
  765. *
  766. * @irq: The irq number
  767. * @dev_id: pointer to the device structure
  768. *
  769. * returns irqreturn_t
  770. *
  771. */
  772. irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
  773. {
  774. struct mei_device *dev = (struct mei_device *) dev_id;
  775. struct mei_cl_cb complete_list;
  776. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  777. struct mei_cl *cl;
  778. s32 slots;
  779. int rets;
  780. bool bus_message_received;
  781. dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
  782. /* initialize our complete list */
  783. mutex_lock(&dev->device_lock);
  784. mei_io_list_init(&complete_list);
  785. dev->host_hw_state = mei_hcsr_read(dev);
  786. /* Ack the interrupt here
  787. * In case of MSI we don't go through the quick handler */
  788. if (pci_dev_msi_enabled(dev->pdev))
  789. mei_clear_interrupts(dev);
  790. dev->me_hw_state = mei_mecsr_read(dev);
  791. /* check if ME wants a reset */
  792. if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
  793. dev->dev_state != MEI_DEV_RESETING &&
  794. dev->dev_state != MEI_DEV_INITIALIZING) {
  795. dev_dbg(&dev->pdev->dev, "FW not ready.\n");
  796. mei_reset(dev, 1);
  797. mutex_unlock(&dev->device_lock);
  798. return IRQ_HANDLED;
  799. }
  800. /* check if we need to start the dev */
  801. if ((dev->host_hw_state & H_RDY) == 0) {
  802. if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
  803. dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
  804. dev->host_hw_state |= (H_IE | H_IG | H_RDY);
  805. mei_hcsr_set(dev);
  806. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  807. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  808. /* link is established
  809. * start sending messages.
  810. */
  811. mei_hbm_start_req(dev);
  812. mutex_unlock(&dev->device_lock);
  813. return IRQ_HANDLED;
  814. } else {
  815. dev_dbg(&dev->pdev->dev, "FW not ready.\n");
  816. mutex_unlock(&dev->device_lock);
  817. return IRQ_HANDLED;
  818. }
  819. }
  820. /* check slots available for reading */
  821. slots = mei_count_full_read_slots(dev);
  822. while (slots > 0) {
  823. /* we have urgent data to send so break the read */
  824. if (dev->wr_ext_msg.hdr.length)
  825. break;
  826. dev_dbg(&dev->pdev->dev, "slots =%08x\n", slots);
  827. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
  828. rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
  829. if (rets)
  830. goto end;
  831. }
  832. rets = mei_irq_thread_write_handler(dev, &complete_list);
  833. end:
  834. dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
  835. dev->host_hw_state = mei_hcsr_read(dev);
  836. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  837. bus_message_received = false;
  838. if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
  839. dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
  840. bus_message_received = true;
  841. }
  842. mutex_unlock(&dev->device_lock);
  843. if (bus_message_received) {
  844. dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
  845. wake_up_interruptible(&dev->wait_recvd_msg);
  846. bus_message_received = false;
  847. }
  848. if (list_empty(&complete_list.list))
  849. return IRQ_HANDLED;
  850. list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
  851. cl = cb_pos->cl;
  852. list_del(&cb_pos->list);
  853. if (cl) {
  854. if (cl != &dev->iamthif_cl) {
  855. dev_dbg(&dev->pdev->dev, "completing call back.\n");
  856. _mei_cmpl(cl, cb_pos);
  857. cb_pos = NULL;
  858. } else if (cl == &dev->iamthif_cl) {
  859. mei_amthif_complete(dev, cb_pos);
  860. }
  861. }
  862. }
  863. return IRQ_HANDLED;
  864. }