client.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. /**
  26. * mei_me_cl_by_uuid - locate index of me client
  27. *
  28. * @dev: mei device
  29. *
  30. * Locking: called under "dev->device_lock" lock
  31. *
  32. * returns me client index or -ENOENT if not found
  33. */
  34. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
  35. {
  36. int i;
  37. for (i = 0; i < dev->me_clients_num; ++i)
  38. if (uuid_le_cmp(*uuid,
  39. dev->me_clients[i].props.protocol_name) == 0)
  40. return i;
  41. return -ENOENT;
  42. }
  43. /**
  44. * mei_me_cl_by_id return index to me_clients for client_id
  45. *
  46. * @dev: the device structure
  47. * @client_id: me client id
  48. *
  49. * Locking: called under "dev->device_lock" lock
  50. *
  51. * returns index on success, -ENOENT on failure.
  52. */
  53. int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  54. {
  55. int i;
  56. for (i = 0; i < dev->me_clients_num; i++)
  57. if (dev->me_clients[i].client_id == client_id)
  58. return i;
  59. return -ENOENT;
  60. }
  61. /**
  62. * mei_cl_cmp_id - tells if the clients are the same
  63. *
  64. * @cl1: host client 1
  65. * @cl2: host client 2
  66. *
  67. * returns true - if the clients has same host and me ids
  68. * false - otherwise
  69. */
  70. static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
  71. const struct mei_cl *cl2)
  72. {
  73. return cl1 && cl2 &&
  74. (cl1->host_client_id == cl2->host_client_id) &&
  75. (cl1->me_client_id == cl2->me_client_id);
  76. }
  77. /**
  78. * mei_io_list_flush - removes cbs belonging to cl.
  79. *
  80. * @list: an instance of our list structure
  81. * @cl: host client, can be NULL for flushing the whole list
  82. * @free: whether to free the cbs
  83. */
  84. static void __mei_io_list_flush(struct mei_cl_cb *list,
  85. struct mei_cl *cl, bool free)
  86. {
  87. struct mei_cl_cb *cb;
  88. struct mei_cl_cb *next;
  89. /* enable removing everything if no cl is specified */
  90. list_for_each_entry_safe(cb, next, &list->list, list) {
  91. if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
  92. list_del(&cb->list);
  93. if (free)
  94. mei_io_cb_free(cb);
  95. }
  96. }
  97. }
  98. /**
  99. * mei_io_list_flush - removes list entry belonging to cl.
  100. *
  101. * @list: An instance of our list structure
  102. * @cl: host client
  103. */
  104. static inline void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  105. {
  106. __mei_io_list_flush(list, cl, false);
  107. }
  108. /**
  109. * mei_io_list_free - removes cb belonging to cl and free them
  110. *
  111. * @list: An instance of our list structure
  112. * @cl: host client
  113. */
  114. static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
  115. {
  116. __mei_io_list_flush(list, cl, true);
  117. }
  118. /**
  119. * mei_io_cb_free - free mei_cb_private related memory
  120. *
  121. * @cb: mei callback struct
  122. */
  123. void mei_io_cb_free(struct mei_cl_cb *cb)
  124. {
  125. if (cb == NULL)
  126. return;
  127. kfree(cb->request_buffer.data);
  128. kfree(cb->response_buffer.data);
  129. kfree(cb);
  130. }
  131. /**
  132. * mei_io_cb_init - allocate and initialize io callback
  133. *
  134. * @cl - mei client
  135. * @fp: pointer to file structure
  136. *
  137. * returns mei_cl_cb pointer or NULL;
  138. */
  139. struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
  140. {
  141. struct mei_cl_cb *cb;
  142. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  143. if (!cb)
  144. return NULL;
  145. mei_io_list_init(cb);
  146. cb->file_object = fp;
  147. cb->cl = cl;
  148. cb->buf_idx = 0;
  149. return cb;
  150. }
  151. /**
  152. * mei_io_cb_alloc_req_buf - allocate request buffer
  153. *
  154. * @cb: io callback structure
  155. * @length: size of the buffer
  156. *
  157. * returns 0 on success
  158. * -EINVAL if cb is NULL
  159. * -ENOMEM if allocation failed
  160. */
  161. int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
  162. {
  163. if (!cb)
  164. return -EINVAL;
  165. if (length == 0)
  166. return 0;
  167. cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
  168. if (!cb->request_buffer.data)
  169. return -ENOMEM;
  170. cb->request_buffer.size = length;
  171. return 0;
  172. }
  173. /**
  174. * mei_io_cb_alloc_resp_buf - allocate response buffer
  175. *
  176. * @cb: io callback structure
  177. * @length: size of the buffer
  178. *
  179. * returns 0 on success
  180. * -EINVAL if cb is NULL
  181. * -ENOMEM if allocation failed
  182. */
  183. int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
  184. {
  185. if (!cb)
  186. return -EINVAL;
  187. if (length == 0)
  188. return 0;
  189. cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
  190. if (!cb->response_buffer.data)
  191. return -ENOMEM;
  192. cb->response_buffer.size = length;
  193. return 0;
  194. }
  195. /**
  196. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  197. *
  198. * @cl: host client
  199. */
  200. int mei_cl_flush_queues(struct mei_cl *cl)
  201. {
  202. struct mei_device *dev;
  203. if (WARN_ON(!cl || !cl->dev))
  204. return -EINVAL;
  205. dev = cl->dev;
  206. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  207. mei_io_list_flush(&cl->dev->read_list, cl);
  208. mei_io_list_free(&cl->dev->write_list, cl);
  209. mei_io_list_free(&cl->dev->write_waiting_list, cl);
  210. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  211. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  212. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  213. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  214. return 0;
  215. }
  216. /**
  217. * mei_cl_init - initializes cl.
  218. *
  219. * @cl: host client to be initialized
  220. * @dev: mei device
  221. */
  222. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  223. {
  224. memset(cl, 0, sizeof(struct mei_cl));
  225. init_waitqueue_head(&cl->wait);
  226. init_waitqueue_head(&cl->rx_wait);
  227. init_waitqueue_head(&cl->tx_wait);
  228. INIT_LIST_HEAD(&cl->link);
  229. INIT_LIST_HEAD(&cl->device_link);
  230. cl->reading_state = MEI_IDLE;
  231. cl->writing_state = MEI_IDLE;
  232. cl->dev = dev;
  233. }
  234. /**
  235. * mei_cl_allocate - allocates cl structure and sets it up.
  236. *
  237. * @dev: mei device
  238. * returns The allocated file or NULL on failure
  239. */
  240. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  241. {
  242. struct mei_cl *cl;
  243. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  244. if (!cl)
  245. return NULL;
  246. mei_cl_init(cl, dev);
  247. return cl;
  248. }
  249. /**
  250. * mei_cl_find_read_cb - find this cl's callback in the read list
  251. *
  252. * @cl: host client
  253. *
  254. * returns cb on success, NULL on error
  255. */
  256. struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
  257. {
  258. struct mei_device *dev = cl->dev;
  259. struct mei_cl_cb *cb;
  260. list_for_each_entry(cb, &dev->read_list.list, list)
  261. if (mei_cl_cmp_id(cl, cb->cl))
  262. return cb;
  263. return NULL;
  264. }
  265. /** mei_cl_link: allocate host id in the host map
  266. *
  267. * @cl - host client
  268. * @id - fixed host id or -1 for generic one
  269. *
  270. * returns 0 on success
  271. * -EINVAL on incorrect values
  272. * -ENONET if client not found
  273. */
  274. int mei_cl_link(struct mei_cl *cl, int id)
  275. {
  276. struct mei_device *dev;
  277. long open_handle_count;
  278. if (WARN_ON(!cl || !cl->dev))
  279. return -EINVAL;
  280. dev = cl->dev;
  281. /* If Id is not assigned get one*/
  282. if (id == MEI_HOST_CLIENT_ID_ANY)
  283. id = find_first_zero_bit(dev->host_clients_map,
  284. MEI_CLIENTS_MAX);
  285. if (id >= MEI_CLIENTS_MAX) {
  286. dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  287. return -EMFILE;
  288. }
  289. open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
  290. if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  291. dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
  292. MEI_MAX_OPEN_HANDLE_COUNT);
  293. return -EMFILE;
  294. }
  295. dev->open_handle_count++;
  296. cl->host_client_id = id;
  297. list_add_tail(&cl->link, &dev->file_list);
  298. set_bit(id, dev->host_clients_map);
  299. cl->state = MEI_FILE_INITIALIZING;
  300. cl_dbg(dev, cl, "link cl\n");
  301. return 0;
  302. }
  303. /**
  304. * mei_cl_unlink - remove me_cl from the list
  305. *
  306. * @cl: host client
  307. */
  308. int mei_cl_unlink(struct mei_cl *cl)
  309. {
  310. struct mei_device *dev;
  311. /* don't shout on error exit path */
  312. if (!cl)
  313. return 0;
  314. /* wd and amthif might not be initialized */
  315. if (!cl->dev)
  316. return 0;
  317. dev = cl->dev;
  318. cl_dbg(dev, cl, "unlink client");
  319. if (dev->open_handle_count > 0)
  320. dev->open_handle_count--;
  321. /* never clear the 0 bit */
  322. if (cl->host_client_id)
  323. clear_bit(cl->host_client_id, dev->host_clients_map);
  324. list_del_init(&cl->link);
  325. cl->state = MEI_FILE_INITIALIZING;
  326. return 0;
  327. }
  328. void mei_host_client_init(struct work_struct *work)
  329. {
  330. struct mei_device *dev = container_of(work,
  331. struct mei_device, init_work);
  332. struct mei_client_properties *client_props;
  333. int i;
  334. mutex_lock(&dev->device_lock);
  335. for (i = 0; i < dev->me_clients_num; i++) {
  336. client_props = &dev->me_clients[i].props;
  337. if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
  338. mei_amthif_host_init(dev);
  339. else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
  340. mei_wd_host_init(dev);
  341. else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
  342. mei_nfc_host_init(dev);
  343. }
  344. dev->dev_state = MEI_DEV_ENABLED;
  345. dev->reset_count = 0;
  346. mutex_unlock(&dev->device_lock);
  347. pm_runtime_mark_last_busy(&dev->pdev->dev);
  348. dev_dbg(&dev->pdev->dev, "rpm: autosuspend\n");
  349. pm_runtime_autosuspend(&dev->pdev->dev);
  350. }
  351. /**
  352. * mei_hbuf_acquire: try to acquire host buffer
  353. *
  354. * @dev: the device structure
  355. * returns true if host buffer was acquired
  356. */
  357. bool mei_hbuf_acquire(struct mei_device *dev)
  358. {
  359. if (mei_pg_state(dev) == MEI_PG_ON ||
  360. dev->pg_event == MEI_PG_EVENT_WAIT) {
  361. dev_dbg(&dev->pdev->dev, "device is in pg\n");
  362. return false;
  363. }
  364. if (!dev->hbuf_is_ready) {
  365. dev_dbg(&dev->pdev->dev, "hbuf is not ready\n");
  366. return false;
  367. }
  368. dev->hbuf_is_ready = false;
  369. return true;
  370. }
  371. /**
  372. * mei_cl_disconnect - disconnect host client from the me one
  373. *
  374. * @cl: host client
  375. *
  376. * Locking: called under "dev->device_lock" lock
  377. *
  378. * returns 0 on success, <0 on failure.
  379. */
  380. int mei_cl_disconnect(struct mei_cl *cl)
  381. {
  382. struct mei_device *dev;
  383. struct mei_cl_cb *cb;
  384. int rets;
  385. if (WARN_ON(!cl || !cl->dev))
  386. return -ENODEV;
  387. dev = cl->dev;
  388. cl_dbg(dev, cl, "disconnecting");
  389. if (cl->state != MEI_FILE_DISCONNECTING)
  390. return 0;
  391. rets = pm_runtime_get(&dev->pdev->dev);
  392. if (rets < 0 && rets != -EINPROGRESS) {
  393. pm_runtime_put_noidle(&dev->pdev->dev);
  394. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  395. return rets;
  396. }
  397. cb = mei_io_cb_init(cl, NULL);
  398. if (!cb) {
  399. rets = -ENOMEM;
  400. goto free;
  401. }
  402. cb->fop_type = MEI_FOP_CLOSE;
  403. if (mei_hbuf_acquire(dev)) {
  404. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  405. rets = -ENODEV;
  406. cl_err(dev, cl, "failed to disconnect.\n");
  407. goto free;
  408. }
  409. cl->timer_count = MEI_CONNECT_TIMEOUT;
  410. mdelay(10); /* Wait for hardware disconnection ready */
  411. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  412. } else {
  413. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  414. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  415. }
  416. mutex_unlock(&dev->device_lock);
  417. wait_event_timeout(dev->wait_recvd_msg,
  418. MEI_FILE_DISCONNECTED == cl->state,
  419. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  420. mutex_lock(&dev->device_lock);
  421. if (MEI_FILE_DISCONNECTED == cl->state) {
  422. rets = 0;
  423. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  424. } else {
  425. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  426. rets = -ETIME;
  427. }
  428. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  429. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  430. free:
  431. cl_dbg(dev, cl, "rpm: autosuspend\n");
  432. pm_runtime_mark_last_busy(&dev->pdev->dev);
  433. pm_runtime_put_autosuspend(&dev->pdev->dev);
  434. mei_io_cb_free(cb);
  435. return rets;
  436. }
  437. /**
  438. * mei_cl_is_other_connecting - checks if other
  439. * client with the same me client id is connecting
  440. *
  441. * @cl: private data of the file object
  442. *
  443. * returns true if other client is connected, false - otherwise.
  444. */
  445. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  446. {
  447. struct mei_device *dev;
  448. struct mei_cl *ocl; /* the other client */
  449. if (WARN_ON(!cl || !cl->dev))
  450. return false;
  451. dev = cl->dev;
  452. list_for_each_entry(ocl, &dev->file_list, link) {
  453. if (ocl->state == MEI_FILE_CONNECTING &&
  454. ocl != cl &&
  455. cl->me_client_id == ocl->me_client_id)
  456. return true;
  457. }
  458. return false;
  459. }
  460. /**
  461. * mei_cl_connect - connect host client to the me one
  462. *
  463. * @cl: host client
  464. *
  465. * Locking: called under "dev->device_lock" lock
  466. *
  467. * returns 0 on success, <0 on failure.
  468. */
  469. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  470. {
  471. struct mei_device *dev;
  472. struct mei_cl_cb *cb;
  473. int rets;
  474. if (WARN_ON(!cl || !cl->dev))
  475. return -ENODEV;
  476. dev = cl->dev;
  477. rets = pm_runtime_get(&dev->pdev->dev);
  478. if (rets < 0 && rets != -EINPROGRESS) {
  479. pm_runtime_put_noidle(&dev->pdev->dev);
  480. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  481. return rets;
  482. }
  483. cb = mei_io_cb_init(cl, file);
  484. if (!cb) {
  485. rets = -ENOMEM;
  486. goto out;
  487. }
  488. cb->fop_type = MEI_FOP_CONNECT;
  489. /* run hbuf acquire last so we don't have to undo */
  490. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  491. cl->state = MEI_FILE_CONNECTING;
  492. if (mei_hbm_cl_connect_req(dev, cl)) {
  493. rets = -ENODEV;
  494. goto out;
  495. }
  496. cl->timer_count = MEI_CONNECT_TIMEOUT;
  497. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  498. } else {
  499. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  500. }
  501. mutex_unlock(&dev->device_lock);
  502. wait_event_timeout(dev->wait_recvd_msg,
  503. (cl->state == MEI_FILE_CONNECTED ||
  504. cl->state == MEI_FILE_DISCONNECTED),
  505. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  506. mutex_lock(&dev->device_lock);
  507. if (cl->state != MEI_FILE_CONNECTED) {
  508. cl->state = MEI_FILE_DISCONNECTED;
  509. /* something went really wrong */
  510. if (!cl->status)
  511. cl->status = -EFAULT;
  512. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  513. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  514. }
  515. rets = cl->status;
  516. out:
  517. cl_dbg(dev, cl, "rpm: autosuspend\n");
  518. pm_runtime_mark_last_busy(&dev->pdev->dev);
  519. pm_runtime_put_autosuspend(&dev->pdev->dev);
  520. mei_io_cb_free(cb);
  521. return rets;
  522. }
  523. /**
  524. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  525. *
  526. * @cl: private data of the file object
  527. *
  528. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  529. * -ENOENT if mei_cl is not present
  530. * -EINVAL if single_recv_buf == 0
  531. */
  532. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  533. {
  534. struct mei_device *dev;
  535. struct mei_me_client *me_cl;
  536. int id;
  537. if (WARN_ON(!cl || !cl->dev))
  538. return -EINVAL;
  539. dev = cl->dev;
  540. if (!dev->me_clients_num)
  541. return 0;
  542. if (cl->mei_flow_ctrl_creds > 0)
  543. return 1;
  544. id = mei_me_cl_by_id(dev, cl->me_client_id);
  545. if (id < 0) {
  546. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  547. return id;
  548. }
  549. me_cl = &dev->me_clients[id];
  550. if (me_cl->mei_flow_ctrl_creds) {
  551. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  552. return -EINVAL;
  553. return 1;
  554. }
  555. return 0;
  556. }
  557. /**
  558. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  559. *
  560. * @cl: private data of the file object
  561. *
  562. * @returns
  563. * 0 on success
  564. * -ENOENT when me client is not found
  565. * -EINVAL when ctrl credits are <= 0
  566. */
  567. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  568. {
  569. struct mei_device *dev;
  570. struct mei_me_client *me_cl;
  571. int id;
  572. if (WARN_ON(!cl || !cl->dev))
  573. return -EINVAL;
  574. dev = cl->dev;
  575. id = mei_me_cl_by_id(dev, cl->me_client_id);
  576. if (id < 0) {
  577. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  578. return id;
  579. }
  580. me_cl = &dev->me_clients[id];
  581. if (me_cl->props.single_recv_buf != 0) {
  582. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  583. return -EINVAL;
  584. me_cl->mei_flow_ctrl_creds--;
  585. } else {
  586. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  587. return -EINVAL;
  588. cl->mei_flow_ctrl_creds--;
  589. }
  590. return 0;
  591. }
  592. /**
  593. * mei_cl_read_start - the start read client message function.
  594. *
  595. * @cl: host client
  596. *
  597. * returns 0 on success, <0 on failure.
  598. */
  599. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  600. {
  601. struct mei_device *dev;
  602. struct mei_cl_cb *cb;
  603. int rets;
  604. int i;
  605. if (WARN_ON(!cl || !cl->dev))
  606. return -ENODEV;
  607. dev = cl->dev;
  608. if (!mei_cl_is_connected(cl))
  609. return -ENODEV;
  610. if (cl->read_cb) {
  611. cl_dbg(dev, cl, "read is pending.\n");
  612. return -EBUSY;
  613. }
  614. i = mei_me_cl_by_id(dev, cl->me_client_id);
  615. if (i < 0) {
  616. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  617. return -ENOTTY;
  618. }
  619. rets = pm_runtime_get(&dev->pdev->dev);
  620. if (rets < 0 && rets != -EINPROGRESS) {
  621. pm_runtime_put_noidle(&dev->pdev->dev);
  622. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  623. return rets;
  624. }
  625. cb = mei_io_cb_init(cl, NULL);
  626. if (!cb) {
  627. rets = -ENOMEM;
  628. goto out;
  629. }
  630. /* always allocate at least client max message */
  631. length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
  632. rets = mei_io_cb_alloc_resp_buf(cb, length);
  633. if (rets)
  634. goto out;
  635. cb->fop_type = MEI_FOP_READ;
  636. if (mei_hbuf_acquire(dev)) {
  637. rets = mei_hbm_cl_flow_control_req(dev, cl);
  638. if (rets < 0)
  639. goto out;
  640. list_add_tail(&cb->list, &dev->read_list.list);
  641. } else {
  642. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  643. }
  644. cl->read_cb = cb;
  645. out:
  646. cl_dbg(dev, cl, "rpm: autosuspend\n");
  647. pm_runtime_mark_last_busy(&dev->pdev->dev);
  648. pm_runtime_put_autosuspend(&dev->pdev->dev);
  649. if (rets)
  650. mei_io_cb_free(cb);
  651. return rets;
  652. }
  653. /**
  654. * mei_cl_irq_write - write a message to device
  655. * from the interrupt thread context
  656. *
  657. * @cl: client
  658. * @cb: callback block.
  659. * @cmpl_list: complete list.
  660. *
  661. * returns 0, OK; otherwise error.
  662. */
  663. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  664. struct mei_cl_cb *cmpl_list)
  665. {
  666. struct mei_device *dev;
  667. struct mei_msg_data *buf;
  668. struct mei_msg_hdr mei_hdr;
  669. size_t len;
  670. u32 msg_slots;
  671. int slots;
  672. int rets;
  673. if (WARN_ON(!cl || !cl->dev))
  674. return -ENODEV;
  675. dev = cl->dev;
  676. buf = &cb->request_buffer;
  677. rets = mei_cl_flow_ctrl_creds(cl);
  678. if (rets < 0)
  679. return rets;
  680. if (rets == 0) {
  681. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  682. return 0;
  683. }
  684. slots = mei_hbuf_empty_slots(dev);
  685. len = buf->size - cb->buf_idx;
  686. msg_slots = mei_data2slots(len);
  687. mei_hdr.host_addr = cl->host_client_id;
  688. mei_hdr.me_addr = cl->me_client_id;
  689. mei_hdr.reserved = 0;
  690. mei_hdr.internal = cb->internal;
  691. if (slots >= msg_slots) {
  692. mei_hdr.length = len;
  693. mei_hdr.msg_complete = 1;
  694. /* Split the message only if we can write the whole host buffer */
  695. } else if (slots == dev->hbuf_depth) {
  696. msg_slots = slots;
  697. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  698. mei_hdr.length = len;
  699. mei_hdr.msg_complete = 0;
  700. } else {
  701. /* wait for next time the host buffer is empty */
  702. return 0;
  703. }
  704. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  705. cb->request_buffer.size, cb->buf_idx);
  706. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  707. if (rets) {
  708. cl->status = rets;
  709. list_move_tail(&cb->list, &cmpl_list->list);
  710. return rets;
  711. }
  712. cl->status = 0;
  713. cl->writing_state = MEI_WRITING;
  714. cb->buf_idx += mei_hdr.length;
  715. if (mei_hdr.msg_complete) {
  716. if (mei_cl_flow_ctrl_reduce(cl))
  717. return -EIO;
  718. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  719. }
  720. return 0;
  721. }
  722. /**
  723. * mei_cl_write - submit a write cb to mei device
  724. assumes device_lock is locked
  725. *
  726. * @cl: host client
  727. * @cl: write callback with filled data
  728. *
  729. * returns number of bytes sent on success, <0 on failure.
  730. */
  731. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  732. {
  733. struct mei_device *dev;
  734. struct mei_msg_data *buf;
  735. struct mei_msg_hdr mei_hdr;
  736. int rets;
  737. if (WARN_ON(!cl || !cl->dev))
  738. return -ENODEV;
  739. if (WARN_ON(!cb))
  740. return -EINVAL;
  741. dev = cl->dev;
  742. buf = &cb->request_buffer;
  743. cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
  744. rets = pm_runtime_get(&dev->pdev->dev);
  745. if (rets < 0 && rets != -EINPROGRESS) {
  746. pm_runtime_put_noidle(&dev->pdev->dev);
  747. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  748. return rets;
  749. }
  750. cb->fop_type = MEI_FOP_WRITE;
  751. cb->buf_idx = 0;
  752. cl->writing_state = MEI_IDLE;
  753. mei_hdr.host_addr = cl->host_client_id;
  754. mei_hdr.me_addr = cl->me_client_id;
  755. mei_hdr.reserved = 0;
  756. mei_hdr.msg_complete = 0;
  757. mei_hdr.internal = cb->internal;
  758. rets = mei_cl_flow_ctrl_creds(cl);
  759. if (rets < 0)
  760. goto err;
  761. if (rets == 0) {
  762. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  763. rets = buf->size;
  764. goto out;
  765. }
  766. if (!mei_hbuf_acquire(dev)) {
  767. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  768. rets = buf->size;
  769. goto out;
  770. }
  771. /* Check for a maximum length */
  772. if (buf->size > mei_hbuf_max_len(dev)) {
  773. mei_hdr.length = mei_hbuf_max_len(dev);
  774. mei_hdr.msg_complete = 0;
  775. } else {
  776. mei_hdr.length = buf->size;
  777. mei_hdr.msg_complete = 1;
  778. }
  779. rets = mei_write_message(dev, &mei_hdr, buf->data);
  780. if (rets)
  781. goto err;
  782. cl->writing_state = MEI_WRITING;
  783. cb->buf_idx = mei_hdr.length;
  784. out:
  785. if (mei_hdr.msg_complete) {
  786. rets = mei_cl_flow_ctrl_reduce(cl);
  787. if (rets < 0)
  788. goto err;
  789. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  790. } else {
  791. list_add_tail(&cb->list, &dev->write_list.list);
  792. }
  793. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  794. mutex_unlock(&dev->device_lock);
  795. rets = wait_event_interruptible(cl->tx_wait,
  796. cl->writing_state == MEI_WRITE_COMPLETE);
  797. mutex_lock(&dev->device_lock);
  798. /* wait_event_interruptible returns -ERESTARTSYS */
  799. if (rets) {
  800. if (signal_pending(current))
  801. rets = -EINTR;
  802. goto err;
  803. }
  804. }
  805. rets = buf->size;
  806. err:
  807. cl_dbg(dev, cl, "rpm: autosuspend\n");
  808. pm_runtime_mark_last_busy(&dev->pdev->dev);
  809. pm_runtime_put_autosuspend(&dev->pdev->dev);
  810. return rets;
  811. }
  812. /**
  813. * mei_cl_complete - processes completed operation for a client
  814. *
  815. * @cl: private data of the file object.
  816. * @cb: callback block.
  817. */
  818. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  819. {
  820. if (cb->fop_type == MEI_FOP_WRITE) {
  821. mei_io_cb_free(cb);
  822. cb = NULL;
  823. cl->writing_state = MEI_WRITE_COMPLETE;
  824. if (waitqueue_active(&cl->tx_wait))
  825. wake_up_interruptible(&cl->tx_wait);
  826. } else if (cb->fop_type == MEI_FOP_READ &&
  827. MEI_READING == cl->reading_state) {
  828. cl->reading_state = MEI_READ_COMPLETE;
  829. if (waitqueue_active(&cl->rx_wait))
  830. wake_up_interruptible(&cl->rx_wait);
  831. else
  832. mei_cl_bus_rx_event(cl);
  833. }
  834. }
  835. /**
  836. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  837. *
  838. * @dev - mei device
  839. */
  840. void mei_cl_all_disconnect(struct mei_device *dev)
  841. {
  842. struct mei_cl *cl;
  843. list_for_each_entry(cl, &dev->file_list, link) {
  844. cl->state = MEI_FILE_DISCONNECTED;
  845. cl->mei_flow_ctrl_creds = 0;
  846. cl->timer_count = 0;
  847. }
  848. }
  849. /**
  850. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  851. *
  852. * @dev - mei device
  853. */
  854. void mei_cl_all_wakeup(struct mei_device *dev)
  855. {
  856. struct mei_cl *cl;
  857. list_for_each_entry(cl, &dev->file_list, link) {
  858. if (waitqueue_active(&cl->rx_wait)) {
  859. cl_dbg(dev, cl, "Waking up reading client!\n");
  860. wake_up_interruptible(&cl->rx_wait);
  861. }
  862. if (waitqueue_active(&cl->tx_wait)) {
  863. cl_dbg(dev, cl, "Waking up writing client!\n");
  864. wake_up_interruptible(&cl->tx_wait);
  865. }
  866. }
  867. }
  868. /**
  869. * mei_cl_all_write_clear - clear all pending writes
  870. * @dev - mei device
  871. */
  872. void mei_cl_all_write_clear(struct mei_device *dev)
  873. {
  874. mei_io_list_free(&dev->write_list, NULL);
  875. mei_io_list_free(&dev->write_waiting_list, NULL);
  876. }