interrupt.c 43 KB

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