interrupt.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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 "mei_dev.h"
  22. #include <linux/mei.h>
  23. #include "hw.h"
  24. #include "interface.h"
  25. /**
  26. * mei_interrupt_quick_handler - The ISR of the MEI device
  27. *
  28. * @irq: The irq number
  29. * @dev_id: pointer to the device structure
  30. *
  31. * returns irqreturn_t
  32. */
  33. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
  34. {
  35. struct mei_device *dev = (struct mei_device *) dev_id;
  36. u32 csr_reg = mei_hcsr_read(dev);
  37. if ((csr_reg & H_IS) != H_IS)
  38. return IRQ_NONE;
  39. /* clear H_IS bit in H_CSR */
  40. mei_reg_write(dev, H_CSR, csr_reg);
  41. return IRQ_WAKE_THREAD;
  42. }
  43. /**
  44. * _mei_cmpl - processes completed operation.
  45. *
  46. * @cl: private data of the file object.
  47. * @cb_pos: callback block.
  48. */
  49. static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
  50. {
  51. if (cb_pos->major_file_operations == MEI_WRITE) {
  52. mei_free_cb_private(cb_pos);
  53. cb_pos = NULL;
  54. cl->writing_state = MEI_WRITE_COMPLETE;
  55. if (waitqueue_active(&cl->tx_wait))
  56. wake_up_interruptible(&cl->tx_wait);
  57. } else if (cb_pos->major_file_operations == MEI_READ &&
  58. MEI_READING == cl->reading_state) {
  59. cl->reading_state = MEI_READ_COMPLETE;
  60. if (waitqueue_active(&cl->rx_wait))
  61. wake_up_interruptible(&cl->rx_wait);
  62. }
  63. }
  64. /**
  65. * _mei_cmpl_iamthif - processes completed iamthif operation.
  66. *
  67. * @dev: the device structure.
  68. * @cb_pos: callback block.
  69. */
  70. static void _mei_cmpl_iamthif(struct mei_device *dev, struct mei_cl_cb *cb_pos)
  71. {
  72. if (dev->iamthif_canceled != 1) {
  73. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  74. dev->iamthif_stall_timer = 0;
  75. memcpy(cb_pos->response_buffer.data,
  76. dev->iamthif_msg_buf,
  77. dev->iamthif_msg_buf_index);
  78. list_add_tail(&cb_pos->cb_list,
  79. &dev->amthi_read_complete_list.mei_cb.cb_list);
  80. dev_dbg(&dev->pdev->dev, "amthi read completed.\n");
  81. dev->iamthif_timer = jiffies;
  82. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  83. dev->iamthif_timer);
  84. } else {
  85. mei_run_next_iamthif_cmd(dev);
  86. }
  87. dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
  88. wake_up_interruptible(&dev->iamthif_cl.wait);
  89. }
  90. /**
  91. * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to
  92. * handle the read amthi message data processing.
  93. *
  94. * @complete_list: An instance of our list structure
  95. * @dev: the device structure
  96. * @mei_hdr: header of amthi message
  97. *
  98. * returns 0 on success, <0 on failure.
  99. */
  100. static int mei_irq_thread_read_amthi_message(struct mei_io_list *complete_list,
  101. struct mei_device *dev,
  102. struct mei_msg_hdr *mei_hdr)
  103. {
  104. struct mei_cl *cl;
  105. struct mei_cl_cb *cb;
  106. unsigned char *buffer;
  107. BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
  108. BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
  109. buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
  110. BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
  111. mei_read_slots(dev, buffer, mei_hdr->length);
  112. dev->iamthif_msg_buf_index += mei_hdr->length;
  113. if (!mei_hdr->msg_complete)
  114. return 0;
  115. dev_dbg(&dev->pdev->dev,
  116. "amthi_message_buffer_index =%d\n",
  117. mei_hdr->length);
  118. dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
  119. if (!dev->iamthif_current_cb)
  120. return -ENODEV;
  121. cb = dev->iamthif_current_cb;
  122. dev->iamthif_current_cb = NULL;
  123. cl = (struct mei_cl *)cb->file_private;
  124. if (!cl)
  125. return -ENODEV;
  126. dev->iamthif_stall_timer = 0;
  127. cb->information = dev->iamthif_msg_buf_index;
  128. cb->read_time = jiffies;
  129. if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
  130. /* found the iamthif cb */
  131. dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
  132. dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
  133. list_add_tail(&cb->cb_list,
  134. &complete_list->mei_cb.cb_list);
  135. }
  136. return 0;
  137. }
  138. /**
  139. * _mei_irq_thread_state_ok - checks if mei header matches file private data
  140. *
  141. * @cl: private data of the file object
  142. * @mei_hdr: header of mei client message
  143. *
  144. * returns !=0 if matches, 0 if no match.
  145. */
  146. static int _mei_irq_thread_state_ok(struct mei_cl *cl,
  147. struct mei_msg_hdr *mei_hdr)
  148. {
  149. return (cl->host_client_id == mei_hdr->host_addr &&
  150. cl->me_client_id == mei_hdr->me_addr &&
  151. cl->state == MEI_FILE_CONNECTED &&
  152. MEI_READ_COMPLETE != cl->reading_state);
  153. }
  154. /**
  155. * mei_irq_thread_read_client_message - bottom half read routine after ISR to
  156. * handle the read mei client message data processing.
  157. *
  158. * @complete_list: An instance of our list structure
  159. * @dev: the device structure
  160. * @mei_hdr: header of mei client message
  161. *
  162. * returns 0 on success, <0 on failure.
  163. */
  164. static int mei_irq_thread_read_client_message(struct mei_io_list *complete_list,
  165. struct mei_device *dev,
  166. struct mei_msg_hdr *mei_hdr)
  167. {
  168. struct mei_cl *cl;
  169. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  170. unsigned char *buffer = NULL;
  171. dev_dbg(&dev->pdev->dev, "start client msg\n");
  172. if (list_empty(&dev->read_list.mei_cb.cb_list))
  173. goto quit;
  174. list_for_each_entry_safe(cb_pos, cb_next,
  175. &dev->read_list.mei_cb.cb_list, cb_list) {
  176. cl = (struct mei_cl *)cb_pos->file_private;
  177. if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
  178. cl->reading_state = MEI_READING;
  179. buffer = cb_pos->response_buffer.data + cb_pos->information;
  180. if (cb_pos->response_buffer.size <
  181. mei_hdr->length + cb_pos->information) {
  182. dev_dbg(&dev->pdev->dev, "message overflow.\n");
  183. list_del(&cb_pos->cb_list);
  184. return -ENOMEM;
  185. }
  186. if (buffer)
  187. mei_read_slots(dev, buffer, mei_hdr->length);
  188. cb_pos->information += mei_hdr->length;
  189. if (mei_hdr->msg_complete) {
  190. cl->status = 0;
  191. list_del(&cb_pos->cb_list);
  192. dev_dbg(&dev->pdev->dev,
  193. "completed read host client = %d,"
  194. "ME client = %d, "
  195. "data length = %lu\n",
  196. cl->host_client_id,
  197. cl->me_client_id,
  198. cb_pos->information);
  199. *(cb_pos->response_buffer.data +
  200. cb_pos->information) = '\0';
  201. dev_dbg(&dev->pdev->dev, "cb_pos->res_buffer - %s\n",
  202. cb_pos->response_buffer.data);
  203. list_add_tail(&cb_pos->cb_list,
  204. &complete_list->mei_cb.cb_list);
  205. }
  206. break;
  207. }
  208. }
  209. quit:
  210. dev_dbg(&dev->pdev->dev, "message read\n");
  211. if (!buffer) {
  212. mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
  213. dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
  214. *(u32 *) dev->rd_msg_buf);
  215. }
  216. return 0;
  217. }
  218. /**
  219. * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
  220. *
  221. * @dev: the device structure.
  222. * @slots: free slots.
  223. *
  224. * returns 0, OK; otherwise, error.
  225. */
  226. static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
  227. {
  228. if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
  229. + sizeof(struct hbm_flow_control))) {
  230. return -EMSGSIZE;
  231. }
  232. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  233. if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
  234. dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
  235. return -EIO;
  236. }
  237. dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
  238. dev->iamthif_state = MEI_IAMTHIF_READING;
  239. dev->iamthif_flow_control_pending = false;
  240. dev->iamthif_msg_buf_index = 0;
  241. dev->iamthif_msg_buf_size = 0;
  242. dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
  243. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  244. return 0;
  245. }
  246. /**
  247. * _mei_irq_thread_close - processes close related operation.
  248. *
  249. * @dev: the device structure.
  250. * @slots: free slots.
  251. * @cb_pos: callback block.
  252. * @cl: private data of the file object.
  253. * @cmpl_list: complete list.
  254. *
  255. * returns 0, OK; otherwise, error.
  256. */
  257. static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
  258. struct mei_cl_cb *cb_pos,
  259. struct mei_cl *cl,
  260. struct mei_io_list *cmpl_list)
  261. {
  262. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  263. sizeof(struct hbm_client_disconnect_request)))
  264. return -EBADMSG;
  265. *slots -= mei_data2slots(sizeof(struct hbm_client_disconnect_request));
  266. if (mei_disconnect(dev, cl)) {
  267. cl->status = 0;
  268. cb_pos->information = 0;
  269. list_move_tail(&cb_pos->cb_list,
  270. &cmpl_list->mei_cb.cb_list);
  271. return -EMSGSIZE;
  272. } else {
  273. cl->state = MEI_FILE_DISCONNECTING;
  274. cl->status = 0;
  275. cb_pos->information = 0;
  276. list_move_tail(&cb_pos->cb_list,
  277. &dev->ctrl_rd_list.mei_cb.cb_list);
  278. cl->timer_count = MEI_CONNECT_TIMEOUT;
  279. }
  280. return 0;
  281. }
  282. /**
  283. * is_treat_specially_client - checks if the message belongs
  284. * to the file private data.
  285. *
  286. * @cl: private data of the file object
  287. * @rs: connect response bus message
  288. *
  289. */
  290. static bool is_treat_specially_client(struct mei_cl *cl,
  291. struct hbm_client_connect_response *rs)
  292. {
  293. if (cl->host_client_id == rs->host_addr &&
  294. cl->me_client_id == rs->me_addr) {
  295. if (!rs->status) {
  296. cl->state = MEI_FILE_CONNECTED;
  297. cl->status = 0;
  298. } else {
  299. cl->state = MEI_FILE_DISCONNECTED;
  300. cl->status = -ENODEV;
  301. }
  302. cl->timer_count = 0;
  303. return true;
  304. }
  305. return false;
  306. }
  307. /**
  308. * mei_client_connect_response - connects to response irq routine
  309. *
  310. * @dev: the device structure
  311. * @rs: connect response bus message
  312. */
  313. static void mei_client_connect_response(struct mei_device *dev,
  314. struct hbm_client_connect_response *rs)
  315. {
  316. struct mei_cl *cl;
  317. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  318. dev_dbg(&dev->pdev->dev,
  319. "connect_response:\n"
  320. "ME Client = %d\n"
  321. "Host Client = %d\n"
  322. "Status = %d\n",
  323. rs->me_addr,
  324. rs->host_addr,
  325. rs->status);
  326. /* if WD or iamthif client treat specially */
  327. if (is_treat_specially_client(&(dev->wd_cl), rs)) {
  328. dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
  329. mei_watchdog_register(dev);
  330. /* next step in the state maching */
  331. mei_host_init_iamthif(dev);
  332. return;
  333. }
  334. if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
  335. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  336. return;
  337. }
  338. list_for_each_entry_safe(cb_pos, cb_next,
  339. &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
  340. cl = (struct mei_cl *)cb_pos->file_private;
  341. if (!cl) {
  342. list_del(&cb_pos->cb_list);
  343. return;
  344. }
  345. if (MEI_IOCTL == cb_pos->major_file_operations) {
  346. if (is_treat_specially_client(cl, rs)) {
  347. list_del(&cb_pos->cb_list);
  348. cl->status = 0;
  349. cl->timer_count = 0;
  350. break;
  351. }
  352. }
  353. }
  354. }
  355. /**
  356. * mei_client_disconnect_response - disconnects from response irq routine
  357. *
  358. * @dev: the device structure
  359. * @rs: disconnect response bus message
  360. */
  361. static void mei_client_disconnect_response(struct mei_device *dev,
  362. struct hbm_client_connect_response *rs)
  363. {
  364. struct mei_cl *cl;
  365. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  366. dev_dbg(&dev->pdev->dev,
  367. "disconnect_response:\n"
  368. "ME Client = %d\n"
  369. "Host Client = %d\n"
  370. "Status = %d\n",
  371. rs->me_addr,
  372. rs->host_addr,
  373. rs->status);
  374. list_for_each_entry_safe(cb_pos, cb_next,
  375. &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
  376. cl = (struct mei_cl *)cb_pos->file_private;
  377. if (!cl) {
  378. list_del(&cb_pos->cb_list);
  379. return;
  380. }
  381. dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
  382. if (cl->host_client_id == rs->host_addr &&
  383. cl->me_client_id == rs->me_addr) {
  384. list_del(&cb_pos->cb_list);
  385. if (!rs->status)
  386. cl->state = MEI_FILE_DISCONNECTED;
  387. cl->status = 0;
  388. cl->timer_count = 0;
  389. break;
  390. }
  391. }
  392. }
  393. /**
  394. * same_flow_addr - tells if they have the same address.
  395. *
  396. * @file: private data of the file object.
  397. * @flow: flow control.
  398. *
  399. * returns !=0, same; 0,not.
  400. */
  401. static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
  402. {
  403. return (cl->host_client_id == flow->host_addr &&
  404. cl->me_client_id == flow->me_addr);
  405. }
  406. /**
  407. * add_single_flow_creds - adds single buffer credentials.
  408. *
  409. * @file: private data ot the file object.
  410. * @flow: flow control.
  411. */
  412. static void add_single_flow_creds(struct mei_device *dev,
  413. struct hbm_flow_control *flow)
  414. {
  415. struct mei_me_client *client;
  416. int i;
  417. for (i = 0; i < dev->me_clients_num; i++) {
  418. client = &dev->me_clients[i];
  419. if (client && flow->me_addr == client->client_id) {
  420. if (client->props.single_recv_buf) {
  421. client->mei_flow_ctrl_creds++;
  422. dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
  423. flow->me_addr);
  424. dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
  425. client->mei_flow_ctrl_creds);
  426. } else {
  427. BUG(); /* error in flow control */
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * mei_client_flow_control_response - flow control response irq routine
  434. *
  435. * @dev: the device structure
  436. * @flow_control: flow control response bus message
  437. */
  438. static void mei_client_flow_control_response(struct mei_device *dev,
  439. struct hbm_flow_control *flow_control)
  440. {
  441. struct mei_cl *cl_pos = NULL;
  442. struct mei_cl *cl_next = NULL;
  443. if (!flow_control->host_addr) {
  444. /* single receive buffer */
  445. add_single_flow_creds(dev, flow_control);
  446. } else {
  447. /* normal connection */
  448. list_for_each_entry_safe(cl_pos, cl_next,
  449. &dev->file_list, link) {
  450. dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
  451. dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
  452. cl_pos->host_client_id,
  453. cl_pos->me_client_id);
  454. dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
  455. flow_control->host_addr,
  456. flow_control->me_addr);
  457. if (same_flow_addr(cl_pos, flow_control)) {
  458. dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
  459. flow_control->host_addr,
  460. flow_control->me_addr);
  461. cl_pos->mei_flow_ctrl_creds++;
  462. dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
  463. cl_pos->mei_flow_ctrl_creds);
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. /**
  470. * same_disconn_addr - tells if they have the same address
  471. *
  472. * @file: private data of the file object.
  473. * @disconn: disconnection request.
  474. *
  475. * returns !=0, same; 0,not.
  476. */
  477. static int same_disconn_addr(struct mei_cl *cl,
  478. struct hbm_client_disconnect_request *disconn)
  479. {
  480. return (cl->host_client_id == disconn->host_addr &&
  481. cl->me_client_id == disconn->me_addr);
  482. }
  483. /**
  484. * mei_client_disconnect_request - disconnects from request irq routine
  485. *
  486. * @dev: the device structure.
  487. * @disconnect_req: disconnect request bus message.
  488. */
  489. static void mei_client_disconnect_request(struct mei_device *dev,
  490. struct hbm_client_disconnect_request *disconnect_req)
  491. {
  492. struct mei_msg_hdr *mei_hdr;
  493. struct hbm_client_connect_response *disconnect_res;
  494. struct mei_cl *cl_pos = NULL;
  495. struct mei_cl *cl_next = NULL;
  496. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  497. if (same_disconn_addr(cl_pos, disconnect_req)) {
  498. dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
  499. disconnect_req->host_addr,
  500. disconnect_req->me_addr);
  501. cl_pos->state = MEI_FILE_DISCONNECTED;
  502. cl_pos->timer_count = 0;
  503. if (cl_pos == &dev->wd_cl)
  504. dev->wd_pending = false;
  505. else if (cl_pos == &dev->iamthif_cl)
  506. dev->iamthif_timer = 0;
  507. /* prepare disconnect response */
  508. mei_hdr =
  509. (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
  510. mei_hdr->host_addr = 0;
  511. mei_hdr->me_addr = 0;
  512. mei_hdr->length =
  513. sizeof(struct hbm_client_connect_response);
  514. mei_hdr->msg_complete = 1;
  515. mei_hdr->reserved = 0;
  516. disconnect_res =
  517. (struct hbm_client_connect_response *)
  518. &dev->ext_msg_buf[1];
  519. disconnect_res->host_addr = cl_pos->host_client_id;
  520. disconnect_res->me_addr = cl_pos->me_client_id;
  521. disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
  522. disconnect_res->status = 0;
  523. dev->extra_write_index = 2;
  524. break;
  525. }
  526. }
  527. }
  528. /**
  529. * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
  530. * handle the read bus message cmd processing.
  531. *
  532. * @dev: the device structure
  533. * @mei_hdr: header of bus message
  534. */
  535. static void mei_irq_thread_read_bus_message(struct mei_device *dev,
  536. struct mei_msg_hdr *mei_hdr)
  537. {
  538. struct mei_bus_message *mei_msg;
  539. struct hbm_host_version_response *version_res;
  540. struct hbm_client_connect_response *connect_res;
  541. struct hbm_client_connect_response *disconnect_res;
  542. struct hbm_flow_control *flow_control;
  543. struct hbm_props_response *props_res;
  544. struct hbm_host_enum_response *enum_res;
  545. struct hbm_client_disconnect_request *disconnect_req;
  546. struct hbm_host_stop_request *host_stop_req;
  547. int res;
  548. /* read the message to our buffer */
  549. BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
  550. mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
  551. mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
  552. switch (mei_msg->hbm_cmd) {
  553. case HOST_START_RES_CMD:
  554. version_res = (struct hbm_host_version_response *) mei_msg;
  555. if (version_res->host_version_supported) {
  556. dev->version.major_version = HBM_MAJOR_VERSION;
  557. dev->version.minor_version = HBM_MINOR_VERSION;
  558. if (dev->mei_state == MEI_INIT_CLIENTS &&
  559. dev->init_clients_state == MEI_START_MESSAGE) {
  560. dev->init_clients_timer = 0;
  561. mei_host_enum_clients_message(dev);
  562. } else {
  563. dev->recvd_msg = false;
  564. dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
  565. mei_reset(dev, 1);
  566. return;
  567. }
  568. } else {
  569. dev->version = version_res->me_max_version;
  570. /* send stop message */
  571. mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
  572. mei_hdr->host_addr = 0;
  573. mei_hdr->me_addr = 0;
  574. mei_hdr->length = sizeof(struct hbm_host_stop_request);
  575. mei_hdr->msg_complete = 1;
  576. mei_hdr->reserved = 0;
  577. host_stop_req = (struct hbm_host_stop_request *)
  578. &dev->wr_msg_buf[1];
  579. memset(host_stop_req,
  580. 0,
  581. sizeof(struct hbm_host_stop_request));
  582. host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
  583. host_stop_req->reason = DRIVER_STOP_REQUEST;
  584. mei_write_message(dev, mei_hdr,
  585. (unsigned char *) (host_stop_req),
  586. mei_hdr->length);
  587. dev_dbg(&dev->pdev->dev, "version mismatch.\n");
  588. return;
  589. }
  590. dev->recvd_msg = true;
  591. dev_dbg(&dev->pdev->dev, "host start response message received.\n");
  592. break;
  593. case CLIENT_CONNECT_RES_CMD:
  594. connect_res =
  595. (struct hbm_client_connect_response *) mei_msg;
  596. mei_client_connect_response(dev, connect_res);
  597. dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
  598. wake_up(&dev->wait_recvd_msg);
  599. break;
  600. case CLIENT_DISCONNECT_RES_CMD:
  601. disconnect_res =
  602. (struct hbm_client_connect_response *) mei_msg;
  603. mei_client_disconnect_response(dev, disconnect_res);
  604. dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
  605. wake_up(&dev->wait_recvd_msg);
  606. break;
  607. case MEI_FLOW_CONTROL_CMD:
  608. flow_control = (struct hbm_flow_control *) mei_msg;
  609. mei_client_flow_control_response(dev, flow_control);
  610. dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
  611. break;
  612. case HOST_CLIENT_PROPERTIES_RES_CMD:
  613. props_res = (struct hbm_props_response *)mei_msg;
  614. if (props_res->status || !dev->me_clients) {
  615. dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
  616. mei_reset(dev, 1);
  617. return;
  618. }
  619. if (dev->me_clients[dev->me_client_presentation_num]
  620. .client_id == props_res->address) {
  621. dev->me_clients[dev->me_client_presentation_num].props
  622. = props_res->client_properties;
  623. if (dev->mei_state == MEI_INIT_CLIENTS &&
  624. dev->init_clients_state ==
  625. MEI_CLIENT_PROPERTIES_MESSAGE) {
  626. dev->me_client_index++;
  627. dev->me_client_presentation_num++;
  628. /** Send Client Properties request **/
  629. res = mei_host_client_properties(dev);
  630. if (res < 0) {
  631. dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
  632. return;
  633. } else if (!res) {
  634. /*
  635. * No more clients to send to.
  636. * Clear Map for indicating now ME clients
  637. * with associated host client
  638. */
  639. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  640. dev->open_handle_count = 0;
  641. /*
  642. * Reserving the first three client IDs
  643. * Client Id 0 - Reserved for MEI Bus Message communications
  644. * Client Id 1 - Reserved for Watchdog
  645. * Client ID 2 - Reserved for AMTHI
  646. */
  647. bitmap_set(dev->host_clients_map, 0, 3);
  648. dev->mei_state = MEI_ENABLED;
  649. /* if wd initialization fails, initialization the AMTHI client,
  650. * otherwise the AMTHI client will be initialized after the WD client connect response
  651. * will be received
  652. */
  653. if (mei_wd_host_init(dev))
  654. mei_host_init_iamthif(dev);
  655. }
  656. } else {
  657. dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
  658. mei_reset(dev, 1);
  659. return;
  660. }
  661. } else {
  662. dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
  663. mei_reset(dev, 1);
  664. return;
  665. }
  666. break;
  667. case HOST_ENUM_RES_CMD:
  668. enum_res = (struct hbm_host_enum_response *) mei_msg;
  669. memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
  670. if (dev->mei_state == MEI_INIT_CLIENTS &&
  671. dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
  672. dev->init_clients_timer = 0;
  673. dev->me_client_presentation_num = 0;
  674. dev->me_client_index = 0;
  675. mei_allocate_me_clients_storage(dev);
  676. dev->init_clients_state =
  677. MEI_CLIENT_PROPERTIES_MESSAGE;
  678. mei_host_client_properties(dev);
  679. } else {
  680. dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
  681. mei_reset(dev, 1);
  682. return;
  683. }
  684. break;
  685. case HOST_STOP_RES_CMD:
  686. dev->mei_state = MEI_DISABLED;
  687. dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
  688. mei_reset(dev, 1);
  689. break;
  690. case CLIENT_DISCONNECT_REQ_CMD:
  691. /* search for client */
  692. disconnect_req =
  693. (struct hbm_client_disconnect_request *) mei_msg;
  694. mei_client_disconnect_request(dev, disconnect_req);
  695. break;
  696. case ME_STOP_REQ_CMD:
  697. /* prepare stop request */
  698. mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
  699. mei_hdr->host_addr = 0;
  700. mei_hdr->me_addr = 0;
  701. mei_hdr->length = sizeof(struct hbm_host_stop_request);
  702. mei_hdr->msg_complete = 1;
  703. mei_hdr->reserved = 0;
  704. host_stop_req =
  705. (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
  706. memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
  707. host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
  708. host_stop_req->reason = DRIVER_STOP_REQUEST;
  709. host_stop_req->reserved[0] = 0;
  710. host_stop_req->reserved[1] = 0;
  711. dev->extra_write_index = 2;
  712. break;
  713. default:
  714. BUG();
  715. break;
  716. }
  717. }
  718. /**
  719. * _mei_hb_read - processes read related operation.
  720. *
  721. * @dev: the device structure.
  722. * @slots: free slots.
  723. * @cb_pos: callback block.
  724. * @cl: private data of the file object.
  725. * @cmpl_list: complete list.
  726. *
  727. * returns 0, OK; otherwise, error.
  728. */
  729. static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
  730. struct mei_cl_cb *cb_pos,
  731. struct mei_cl *cl,
  732. struct mei_io_list *cmpl_list)
  733. {
  734. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  735. sizeof(struct hbm_flow_control))) {
  736. /* return the cancel routine */
  737. list_del(&cb_pos->cb_list);
  738. return -EBADMSG;
  739. }
  740. *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
  741. if (mei_send_flow_control(dev, cl)) {
  742. cl->status = -ENODEV;
  743. cb_pos->information = 0;
  744. list_move_tail(&cb_pos->cb_list, &cmpl_list->mei_cb.cb_list);
  745. return -ENODEV;
  746. }
  747. list_move_tail(&cb_pos->cb_list, &dev->read_list.mei_cb.cb_list);
  748. return 0;
  749. }
  750. /**
  751. * _mei_irq_thread_ioctl - processes ioctl related operation.
  752. *
  753. * @dev: the device structure.
  754. * @slots: free slots.
  755. * @cb_pos: callback block.
  756. * @cl: private data of the file object.
  757. * @cmpl_list: complete list.
  758. *
  759. * returns 0, OK; otherwise, error.
  760. */
  761. static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
  762. struct mei_cl_cb *cb_pos,
  763. struct mei_cl *cl,
  764. struct mei_io_list *cmpl_list)
  765. {
  766. if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
  767. sizeof(struct hbm_client_connect_request))) {
  768. /* return the cancel routine */
  769. list_del(&cb_pos->cb_list);
  770. return -EBADMSG;
  771. }
  772. cl->state = MEI_FILE_CONNECTING;
  773. *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
  774. if (mei_connect(dev, cl)) {
  775. cl->status = -ENODEV;
  776. cb_pos->information = 0;
  777. list_del(&cb_pos->cb_list);
  778. return -ENODEV;
  779. } else {
  780. list_move_tail(&cb_pos->cb_list,
  781. &dev->ctrl_rd_list.mei_cb.cb_list);
  782. cl->timer_count = MEI_CONNECT_TIMEOUT;
  783. }
  784. return 0;
  785. }
  786. /**
  787. * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
  788. *
  789. * @dev: the device structure.
  790. * @slots: free slots.
  791. * @cb_pos: callback block.
  792. * @cl: private data of the file object.
  793. * @cmpl_list: complete list.
  794. *
  795. * returns 0, OK; otherwise, error.
  796. */
  797. static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
  798. struct mei_cl_cb *cb_pos,
  799. struct mei_cl *cl,
  800. struct mei_io_list *cmpl_list)
  801. {
  802. struct mei_msg_hdr *mei_hdr;
  803. if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
  804. (cb_pos->request_buffer.size -
  805. cb_pos->information))) {
  806. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  807. mei_hdr->host_addr = cl->host_client_id;
  808. mei_hdr->me_addr = cl->me_client_id;
  809. mei_hdr->length = cb_pos->request_buffer.size -
  810. cb_pos->information;
  811. mei_hdr->msg_complete = 1;
  812. mei_hdr->reserved = 0;
  813. dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
  814. "mei_hdr->msg_complete = %d\n",
  815. cb_pos->request_buffer.size,
  816. mei_hdr->msg_complete);
  817. dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
  818. cb_pos->information);
  819. dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
  820. mei_hdr->length);
  821. *slots -= mei_data2slots(mei_hdr->length);
  822. if (mei_write_message(dev, mei_hdr,
  823. (unsigned char *)
  824. (cb_pos->request_buffer.data +
  825. cb_pos->information),
  826. mei_hdr->length)) {
  827. cl->status = -ENODEV;
  828. list_move_tail(&cb_pos->cb_list,
  829. &cmpl_list->mei_cb.cb_list);
  830. return -ENODEV;
  831. } else {
  832. if (mei_flow_ctrl_reduce(dev, cl))
  833. return -ENODEV;
  834. cl->status = 0;
  835. cb_pos->information += mei_hdr->length;
  836. list_move_tail(&cb_pos->cb_list,
  837. &dev->write_waiting_list.mei_cb.cb_list);
  838. }
  839. } else if (*slots == dev->hbuf_depth) {
  840. /* buffer is still empty */
  841. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  842. mei_hdr->host_addr = cl->host_client_id;
  843. mei_hdr->me_addr = cl->me_client_id;
  844. mei_hdr->length =
  845. (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  846. mei_hdr->msg_complete = 0;
  847. mei_hdr->reserved = 0;
  848. *slots -= mei_data2slots(mei_hdr->length);
  849. if (mei_write_message(dev, mei_hdr,
  850. (unsigned char *)
  851. (cb_pos->request_buffer.data +
  852. cb_pos->information),
  853. mei_hdr->length)) {
  854. cl->status = -ENODEV;
  855. list_move_tail(&cb_pos->cb_list,
  856. &cmpl_list->mei_cb.cb_list);
  857. return -ENODEV;
  858. } else {
  859. cb_pos->information += mei_hdr->length;
  860. dev_dbg(&dev->pdev->dev,
  861. "cb_pos->request_buffer.size =%d"
  862. " mei_hdr->msg_complete = %d\n",
  863. cb_pos->request_buffer.size,
  864. mei_hdr->msg_complete);
  865. dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
  866. cb_pos->information);
  867. dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
  868. mei_hdr->length);
  869. }
  870. return -EMSGSIZE;
  871. } else {
  872. return -EBADMSG;
  873. }
  874. return 0;
  875. }
  876. /**
  877. * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
  878. *
  879. * @dev: the device structure.
  880. * @slots: free slots.
  881. * @cb_pos: callback block.
  882. * @cl: private data of the file object.
  883. * @cmpl_list: complete list.
  884. *
  885. * returns 0, OK; otherwise, error.
  886. */
  887. static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
  888. struct mei_cl_cb *cb_pos,
  889. struct mei_cl *cl,
  890. struct mei_io_list *cmpl_list)
  891. {
  892. struct mei_msg_hdr *mei_hdr;
  893. if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
  894. dev->iamthif_msg_buf_size -
  895. dev->iamthif_msg_buf_index)) {
  896. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  897. mei_hdr->host_addr = cl->host_client_id;
  898. mei_hdr->me_addr = cl->me_client_id;
  899. mei_hdr->length = dev->iamthif_msg_buf_size -
  900. dev->iamthif_msg_buf_index;
  901. mei_hdr->msg_complete = 1;
  902. mei_hdr->reserved = 0;
  903. *slots -= mei_data2slots(mei_hdr->length);
  904. if (mei_write_message(dev, mei_hdr,
  905. (dev->iamthif_msg_buf +
  906. dev->iamthif_msg_buf_index),
  907. mei_hdr->length)) {
  908. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  909. cl->status = -ENODEV;
  910. list_del(&cb_pos->cb_list);
  911. return -ENODEV;
  912. } else {
  913. if (mei_flow_ctrl_reduce(dev, cl))
  914. return -ENODEV;
  915. dev->iamthif_msg_buf_index += mei_hdr->length;
  916. cb_pos->information = dev->iamthif_msg_buf_index;
  917. cl->status = 0;
  918. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  919. dev->iamthif_flow_control_pending = true;
  920. /* save iamthif cb sent to amthi client */
  921. dev->iamthif_current_cb = cb_pos;
  922. list_move_tail(&cb_pos->cb_list,
  923. &dev->write_waiting_list.mei_cb.cb_list);
  924. }
  925. } else if (*slots == dev->hbuf_depth) {
  926. /* buffer is still empty */
  927. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  928. mei_hdr->host_addr = cl->host_client_id;
  929. mei_hdr->me_addr = cl->me_client_id;
  930. mei_hdr->length =
  931. (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  932. mei_hdr->msg_complete = 0;
  933. mei_hdr->reserved = 0;
  934. *slots -= mei_data2slots(mei_hdr->length);
  935. if (mei_write_message(dev, mei_hdr,
  936. (dev->iamthif_msg_buf +
  937. dev->iamthif_msg_buf_index),
  938. mei_hdr->length)) {
  939. cl->status = -ENODEV;
  940. list_del(&cb_pos->cb_list);
  941. } else {
  942. dev->iamthif_msg_buf_index += mei_hdr->length;
  943. }
  944. return -EMSGSIZE;
  945. } else {
  946. return -EBADMSG;
  947. }
  948. return 0;
  949. }
  950. /**
  951. * mei_irq_thread_read_handler - bottom half read routine after ISR to
  952. * handle the read processing.
  953. *
  954. * @cmpl_list: An instance of our list structure
  955. * @dev: the device structure
  956. * @slots: slots to read.
  957. *
  958. * returns 0 on success, <0 on failure.
  959. */
  960. static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list,
  961. struct mei_device *dev,
  962. s32 *slots)
  963. {
  964. struct mei_msg_hdr *mei_hdr;
  965. struct mei_cl *cl_pos = NULL;
  966. struct mei_cl *cl_next = NULL;
  967. int ret = 0;
  968. if (!dev->rd_msg_hdr) {
  969. dev->rd_msg_hdr = mei_mecbrw_read(dev);
  970. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  971. (*slots)--;
  972. dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
  973. }
  974. mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
  975. dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
  976. if (mei_hdr->reserved || !dev->rd_msg_hdr) {
  977. dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
  978. ret = -EBADMSG;
  979. goto end;
  980. }
  981. if (mei_hdr->host_addr || mei_hdr->me_addr) {
  982. list_for_each_entry_safe(cl_pos, cl_next,
  983. &dev->file_list, link) {
  984. dev_dbg(&dev->pdev->dev,
  985. "list_for_each_entry_safe read host"
  986. " client = %d, ME client = %d\n",
  987. cl_pos->host_client_id,
  988. cl_pos->me_client_id);
  989. if (cl_pos->host_client_id == mei_hdr->host_addr &&
  990. cl_pos->me_client_id == mei_hdr->me_addr)
  991. break;
  992. }
  993. if (&cl_pos->link == &dev->file_list) {
  994. dev_dbg(&dev->pdev->dev, "corrupted message header\n");
  995. ret = -EBADMSG;
  996. goto end;
  997. }
  998. }
  999. if (((*slots) * sizeof(u32)) < mei_hdr->length) {
  1000. dev_dbg(&dev->pdev->dev,
  1001. "we can't read the message slots =%08x.\n",
  1002. *slots);
  1003. /* we can't read the message */
  1004. ret = -ERANGE;
  1005. goto end;
  1006. }
  1007. /* decide where to read the message too */
  1008. if (!mei_hdr->host_addr) {
  1009. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
  1010. mei_irq_thread_read_bus_message(dev, mei_hdr);
  1011. dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
  1012. } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
  1013. (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
  1014. (dev->iamthif_state == MEI_IAMTHIF_READING)) {
  1015. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
  1016. dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
  1017. mei_hdr->length);
  1018. ret = mei_irq_thread_read_amthi_message(cmpl_list,
  1019. dev, mei_hdr);
  1020. if (ret)
  1021. goto end;
  1022. } else {
  1023. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
  1024. ret = mei_irq_thread_read_client_message(cmpl_list,
  1025. dev, mei_hdr);
  1026. if (ret)
  1027. goto end;
  1028. }
  1029. /* reset the number of slots and header */
  1030. *slots = mei_count_full_read_slots(dev);
  1031. dev->rd_msg_hdr = 0;
  1032. if (*slots == -EOVERFLOW) {
  1033. /* overflow - reset */
  1034. dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
  1035. /* set the event since message has been read */
  1036. ret = -ERANGE;
  1037. goto end;
  1038. }
  1039. end:
  1040. return ret;
  1041. }
  1042. /**
  1043. * mei_irq_thread_write_handler - bottom half write routine after
  1044. * ISR to handle the write processing.
  1045. *
  1046. * @cmpl_list: An instance of our list structure
  1047. * @dev: the device structure
  1048. * @slots: slots to write.
  1049. *
  1050. * returns 0 on success, <0 on failure.
  1051. */
  1052. static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list,
  1053. struct mei_device *dev,
  1054. s32 *slots)
  1055. {
  1056. struct mei_cl *cl;
  1057. struct mei_cl_cb *pos = NULL, *next = NULL;
  1058. struct mei_io_list *list;
  1059. int ret;
  1060. if (!mei_hbuf_is_empty(dev)) {
  1061. dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
  1062. return 0;
  1063. }
  1064. *slots = mei_hbuf_empty_slots(dev);
  1065. if (*slots <= 0)
  1066. return -EMSGSIZE;
  1067. /* complete all waiting for write CB */
  1068. dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
  1069. list = &dev->write_waiting_list;
  1070. list_for_each_entry_safe(pos, next, &list->mei_cb.cb_list, cb_list) {
  1071. cl = (struct mei_cl *)pos->file_private;
  1072. if (cl == NULL)
  1073. continue;
  1074. cl->status = 0;
  1075. list_del(&pos->cb_list);
  1076. if (MEI_WRITING == cl->writing_state &&
  1077. (pos->major_file_operations == MEI_WRITE) &&
  1078. (cl != &dev->iamthif_cl)) {
  1079. dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
  1080. cl->writing_state = MEI_WRITE_COMPLETE;
  1081. list_add_tail(&pos->cb_list,
  1082. &cmpl_list->mei_cb.cb_list);
  1083. }
  1084. if (cl == &dev->iamthif_cl) {
  1085. dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
  1086. if (dev->iamthif_flow_control_pending) {
  1087. ret = _mei_irq_thread_iamthif_read(dev, slots);
  1088. if (ret)
  1089. return ret;
  1090. }
  1091. }
  1092. }
  1093. if (dev->stop && !dev->wd_pending) {
  1094. dev->wd_stopped = true;
  1095. wake_up_interruptible(&dev->wait_stop_wd);
  1096. return 0;
  1097. }
  1098. if (dev->extra_write_index) {
  1099. dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
  1100. dev->extra_write_index);
  1101. mei_write_message(dev,
  1102. (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
  1103. (unsigned char *) &dev->ext_msg_buf[1],
  1104. (dev->extra_write_index - 1) * sizeof(u32));
  1105. *slots -= dev->extra_write_index;
  1106. dev->extra_write_index = 0;
  1107. }
  1108. if (dev->mei_state == MEI_ENABLED) {
  1109. if (dev->wd_pending &&
  1110. mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
  1111. if (mei_wd_send(dev))
  1112. dev_dbg(&dev->pdev->dev, "wd send failed.\n");
  1113. else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
  1114. return -ENODEV;
  1115. dev->wd_pending = false;
  1116. if (dev->wd_timeout)
  1117. *slots -= mei_data2slots(MEI_START_WD_DATA_SIZE);
  1118. else
  1119. *slots -= mei_data2slots(MEI_START_WD_DATA_SIZE);
  1120. }
  1121. }
  1122. if (dev->stop)
  1123. return -ENODEV;
  1124. /* complete control write list CB */
  1125. dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
  1126. list_for_each_entry_safe(pos, next,
  1127. &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) {
  1128. cl = (struct mei_cl *) pos->file_private;
  1129. if (!cl) {
  1130. list_del(&pos->cb_list);
  1131. return -ENODEV;
  1132. }
  1133. switch (pos->major_file_operations) {
  1134. case MEI_CLOSE:
  1135. /* send disconnect message */
  1136. ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
  1137. if (ret)
  1138. return ret;
  1139. break;
  1140. case MEI_READ:
  1141. /* send flow control message */
  1142. ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
  1143. if (ret)
  1144. return ret;
  1145. break;
  1146. case MEI_IOCTL:
  1147. /* connect message */
  1148. if (mei_other_client_is_connecting(dev, cl))
  1149. continue;
  1150. ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
  1151. if (ret)
  1152. return ret;
  1153. break;
  1154. default:
  1155. BUG();
  1156. }
  1157. }
  1158. /* complete write list CB */
  1159. dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
  1160. list_for_each_entry_safe(pos, next,
  1161. &dev->write_list.mei_cb.cb_list, cb_list) {
  1162. cl = (struct mei_cl *)pos->file_private;
  1163. if (cl == NULL)
  1164. continue;
  1165. if (cl != &dev->iamthif_cl) {
  1166. if (mei_flow_ctrl_creds(dev, cl) <= 0) {
  1167. dev_dbg(&dev->pdev->dev,
  1168. "No flow control credentials for client %d, not sending.\n",
  1169. cl->host_client_id);
  1170. continue;
  1171. }
  1172. ret = _mei_irq_thread_cmpl(dev, slots, pos,
  1173. cl, cmpl_list);
  1174. if (ret)
  1175. return ret;
  1176. } else if (cl == &dev->iamthif_cl) {
  1177. /* IAMTHIF IOCTL */
  1178. dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
  1179. if (mei_flow_ctrl_creds(dev, cl) <= 0) {
  1180. dev_dbg(&dev->pdev->dev,
  1181. "No flow control credentials for amthi client %d.\n",
  1182. cl->host_client_id);
  1183. continue;
  1184. }
  1185. ret = _mei_irq_thread_cmpl_iamthif(dev, slots, pos,
  1186. cl, cmpl_list);
  1187. if (ret)
  1188. return ret;
  1189. }
  1190. }
  1191. return 0;
  1192. }
  1193. /**
  1194. * mei_timer - timer function.
  1195. *
  1196. * @work: pointer to the work_struct structure
  1197. *
  1198. * NOTE: This function is called by timer interrupt work
  1199. */
  1200. void mei_timer(struct work_struct *work)
  1201. {
  1202. unsigned long timeout;
  1203. struct mei_cl *cl_pos = NULL;
  1204. struct mei_cl *cl_next = NULL;
  1205. struct list_head *amthi_complete_list = NULL;
  1206. struct mei_cl_cb *cb_pos = NULL;
  1207. struct mei_cl_cb *cb_next = NULL;
  1208. struct mei_device *dev = container_of(work,
  1209. struct mei_device, timer_work.work);
  1210. mutex_lock(&dev->device_lock);
  1211. if (dev->mei_state != MEI_ENABLED) {
  1212. if (dev->mei_state == MEI_INIT_CLIENTS) {
  1213. if (dev->init_clients_timer) {
  1214. if (--dev->init_clients_timer == 0) {
  1215. dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
  1216. dev->init_clients_state);
  1217. mei_reset(dev, 1);
  1218. }
  1219. }
  1220. }
  1221. goto out;
  1222. }
  1223. /*** connect/disconnect timeouts ***/
  1224. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  1225. if (cl_pos->timer_count) {
  1226. if (--cl_pos->timer_count == 0) {
  1227. dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
  1228. mei_reset(dev, 1);
  1229. goto out;
  1230. }
  1231. }
  1232. }
  1233. if (dev->iamthif_stall_timer) {
  1234. if (--dev->iamthif_stall_timer == 0) {
  1235. dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
  1236. mei_reset(dev, 1);
  1237. dev->iamthif_msg_buf_size = 0;
  1238. dev->iamthif_msg_buf_index = 0;
  1239. dev->iamthif_canceled = false;
  1240. dev->iamthif_ioctl = true;
  1241. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  1242. dev->iamthif_timer = 0;
  1243. if (dev->iamthif_current_cb)
  1244. mei_free_cb_private(dev->iamthif_current_cb);
  1245. dev->iamthif_file_object = NULL;
  1246. dev->iamthif_current_cb = NULL;
  1247. mei_run_next_iamthif_cmd(dev);
  1248. }
  1249. }
  1250. if (dev->iamthif_timer) {
  1251. timeout = dev->iamthif_timer +
  1252. msecs_to_jiffies(IAMTHIF_READ_TIMER);
  1253. dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
  1254. dev->iamthif_timer);
  1255. dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
  1256. dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
  1257. if (time_after(jiffies, timeout)) {
  1258. /*
  1259. * User didn't read the AMTHI data on time (15sec)
  1260. * freeing AMTHI for other requests
  1261. */
  1262. dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
  1263. amthi_complete_list = &dev->amthi_read_complete_list.
  1264. mei_cb.cb_list;
  1265. list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) {
  1266. cl_pos = cb_pos->file_object->private_data;
  1267. /* Finding the AMTHI entry. */
  1268. if (cl_pos == &dev->iamthif_cl)
  1269. list_del(&cb_pos->cb_list);
  1270. }
  1271. if (dev->iamthif_current_cb)
  1272. mei_free_cb_private(dev->iamthif_current_cb);
  1273. dev->iamthif_file_object->private_data = NULL;
  1274. dev->iamthif_file_object = NULL;
  1275. dev->iamthif_current_cb = NULL;
  1276. dev->iamthif_timer = 0;
  1277. mei_run_next_iamthif_cmd(dev);
  1278. }
  1279. }
  1280. out:
  1281. schedule_delayed_work(&dev->timer_work, 2 * HZ);
  1282. mutex_unlock(&dev->device_lock);
  1283. }
  1284. /**
  1285. * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
  1286. * processing.
  1287. *
  1288. * @irq: The irq number
  1289. * @dev_id: pointer to the device structure
  1290. *
  1291. * returns irqreturn_t
  1292. *
  1293. */
  1294. irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
  1295. {
  1296. struct mei_device *dev = (struct mei_device *) dev_id;
  1297. struct mei_io_list complete_list;
  1298. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  1299. struct mei_cl *cl;
  1300. s32 slots;
  1301. int rets;
  1302. bool bus_message_received;
  1303. dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
  1304. /* initialize our complete list */
  1305. mutex_lock(&dev->device_lock);
  1306. mei_io_list_init(&complete_list);
  1307. dev->host_hw_state = mei_hcsr_read(dev);
  1308. /* Ack the interrupt here
  1309. * In case of MSI we don't go through the quick handler */
  1310. if (pci_dev_msi_enabled(dev->pdev))
  1311. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  1312. dev->me_hw_state = mei_mecsr_read(dev);
  1313. /* check if ME wants a reset */
  1314. if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
  1315. dev->mei_state != MEI_RESETING &&
  1316. dev->mei_state != MEI_INITIALIZING) {
  1317. dev_dbg(&dev->pdev->dev, "FW not ready.\n");
  1318. mei_reset(dev, 1);
  1319. mutex_unlock(&dev->device_lock);
  1320. return IRQ_HANDLED;
  1321. }
  1322. /* check if we need to start the dev */
  1323. if ((dev->host_hw_state & H_RDY) == 0) {
  1324. if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
  1325. dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
  1326. dev->host_hw_state |= (H_IE | H_IG | H_RDY);
  1327. mei_hcsr_set(dev);
  1328. dev->mei_state = MEI_INIT_CLIENTS;
  1329. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  1330. /* link is established
  1331. * start sending messages.
  1332. */
  1333. mei_host_start_message(dev);
  1334. mutex_unlock(&dev->device_lock);
  1335. return IRQ_HANDLED;
  1336. } else {
  1337. dev_dbg(&dev->pdev->dev, "FW not ready.\n");
  1338. mutex_unlock(&dev->device_lock);
  1339. return IRQ_HANDLED;
  1340. }
  1341. }
  1342. /* check slots available for reading */
  1343. slots = mei_count_full_read_slots(dev);
  1344. dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
  1345. slots, dev->extra_write_index);
  1346. while (slots > 0 && !dev->extra_write_index) {
  1347. dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
  1348. slots, dev->extra_write_index);
  1349. dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
  1350. rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
  1351. if (rets)
  1352. goto end;
  1353. }
  1354. rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
  1355. end:
  1356. dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
  1357. dev->host_hw_state = mei_hcsr_read(dev);
  1358. dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
  1359. bus_message_received = false;
  1360. if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
  1361. dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
  1362. bus_message_received = true;
  1363. }
  1364. mutex_unlock(&dev->device_lock);
  1365. if (bus_message_received) {
  1366. dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
  1367. wake_up_interruptible(&dev->wait_recvd_msg);
  1368. bus_message_received = false;
  1369. }
  1370. if (list_empty(&complete_list.mei_cb.cb_list))
  1371. return IRQ_HANDLED;
  1372. list_for_each_entry_safe(cb_pos, cb_next,
  1373. &complete_list.mei_cb.cb_list, cb_list) {
  1374. cl = (struct mei_cl *)cb_pos->file_private;
  1375. list_del(&cb_pos->cb_list);
  1376. if (cl) {
  1377. if (cl != &dev->iamthif_cl) {
  1378. dev_dbg(&dev->pdev->dev, "completing call back.\n");
  1379. _mei_cmpl(cl, cb_pos);
  1380. cb_pos = NULL;
  1381. } else if (cl == &dev->iamthif_cl) {
  1382. _mei_cmpl_iamthif(dev, cb_pos);
  1383. }
  1384. }
  1385. }
  1386. return IRQ_HANDLED;
  1387. }