interrupt.c 42 KB

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