interrupt.c 43 KB

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