interrupt.c 42 KB

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