client.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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/mei.h>
  21. #include "mei_dev.h"
  22. #include "hbm.h"
  23. #include "client.h"
  24. /**
  25. * mei_me_cl_by_uuid - locate index of me client
  26. *
  27. * @dev: mei device
  28. * returns me client index or -ENOENT if not found
  29. */
  30. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
  31. {
  32. int i, res = -ENOENT;
  33. for (i = 0; i < dev->me_clients_num; ++i)
  34. if (uuid_le_cmp(*uuid,
  35. dev->me_clients[i].props.protocol_name) == 0) {
  36. res = i;
  37. break;
  38. }
  39. return res;
  40. }
  41. /**
  42. * mei_me_cl_by_id return index to me_clients for client_id
  43. *
  44. * @dev: the device structure
  45. * @client_id: me client id
  46. *
  47. * Locking: called under "dev->device_lock" lock
  48. *
  49. * returns index on success, -ENOENT on failure.
  50. */
  51. int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  52. {
  53. int i;
  54. for (i = 0; i < dev->me_clients_num; i++)
  55. if (dev->me_clients[i].client_id == client_id)
  56. break;
  57. if (WARN_ON(dev->me_clients[i].client_id != client_id))
  58. return -ENOENT;
  59. if (i == dev->me_clients_num)
  60. return -ENOENT;
  61. return i;
  62. }
  63. /**
  64. * mei_io_list_flush - removes list entry belonging to cl.
  65. *
  66. * @list: An instance of our list structure
  67. * @cl: host client
  68. */
  69. void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  70. {
  71. struct mei_cl_cb *cb;
  72. struct mei_cl_cb *next;
  73. list_for_each_entry_safe(cb, next, &list->list, list) {
  74. if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
  75. list_del(&cb->list);
  76. }
  77. }
  78. /**
  79. * mei_io_cb_free - free mei_cb_private related memory
  80. *
  81. * @cb: mei callback struct
  82. */
  83. void mei_io_cb_free(struct mei_cl_cb *cb)
  84. {
  85. if (cb == NULL)
  86. return;
  87. kfree(cb->request_buffer.data);
  88. kfree(cb->response_buffer.data);
  89. kfree(cb);
  90. }
  91. /**
  92. * mei_io_cb_init - allocate and initialize io callback
  93. *
  94. * @cl - mei client
  95. * @file: pointer to file structure
  96. *
  97. * returns mei_cl_cb pointer or NULL;
  98. */
  99. struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
  100. {
  101. struct mei_cl_cb *cb;
  102. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  103. if (!cb)
  104. return NULL;
  105. mei_io_list_init(cb);
  106. cb->file_object = fp;
  107. cb->cl = cl;
  108. cb->buf_idx = 0;
  109. return cb;
  110. }
  111. /**
  112. * mei_io_cb_alloc_req_buf - allocate request buffer
  113. *
  114. * @cb - io callback structure
  115. * @size: size of the buffer
  116. *
  117. * returns 0 on success
  118. * -EINVAL if cb is NULL
  119. * -ENOMEM if allocation failed
  120. */
  121. int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
  122. {
  123. if (!cb)
  124. return -EINVAL;
  125. if (length == 0)
  126. return 0;
  127. cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
  128. if (!cb->request_buffer.data)
  129. return -ENOMEM;
  130. cb->request_buffer.size = length;
  131. return 0;
  132. }
  133. /**
  134. * mei_io_cb_alloc_req_buf - allocate respose buffer
  135. *
  136. * @cb - io callback structure
  137. * @size: size of the buffer
  138. *
  139. * returns 0 on success
  140. * -EINVAL if cb is NULL
  141. * -ENOMEM if allocation failed
  142. */
  143. int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
  144. {
  145. if (!cb)
  146. return -EINVAL;
  147. if (length == 0)
  148. return 0;
  149. cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
  150. if (!cb->response_buffer.data)
  151. return -ENOMEM;
  152. cb->response_buffer.size = length;
  153. return 0;
  154. }
  155. /**
  156. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  157. *
  158. * @dev: the device structure
  159. * @cl: host client
  160. */
  161. int mei_cl_flush_queues(struct mei_cl *cl)
  162. {
  163. if (WARN_ON(!cl || !cl->dev))
  164. return -EINVAL;
  165. dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
  166. mei_io_list_flush(&cl->dev->read_list, cl);
  167. mei_io_list_flush(&cl->dev->write_list, cl);
  168. mei_io_list_flush(&cl->dev->write_waiting_list, cl);
  169. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  170. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  171. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  172. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  173. return 0;
  174. }
  175. /**
  176. * mei_cl_init - initializes intialize cl.
  177. *
  178. * @cl: host client to be initialized
  179. * @dev: mei device
  180. */
  181. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  182. {
  183. memset(cl, 0, sizeof(struct mei_cl));
  184. init_waitqueue_head(&cl->wait);
  185. init_waitqueue_head(&cl->rx_wait);
  186. init_waitqueue_head(&cl->tx_wait);
  187. INIT_LIST_HEAD(&cl->link);
  188. INIT_LIST_HEAD(&cl->device_link);
  189. cl->reading_state = MEI_IDLE;
  190. cl->writing_state = MEI_IDLE;
  191. cl->dev = dev;
  192. }
  193. /**
  194. * mei_cl_allocate - allocates cl structure and sets it up.
  195. *
  196. * @dev: mei device
  197. * returns The allocated file or NULL on failure
  198. */
  199. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  200. {
  201. struct mei_cl *cl;
  202. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  203. if (!cl)
  204. return NULL;
  205. mei_cl_init(cl, dev);
  206. return cl;
  207. }
  208. /**
  209. * mei_cl_find_read_cb - find this cl's callback in the read list
  210. *
  211. * @dev: device structure
  212. * returns cb on success, NULL on error
  213. */
  214. struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
  215. {
  216. struct mei_device *dev = cl->dev;
  217. struct mei_cl_cb *cb = NULL;
  218. struct mei_cl_cb *next = NULL;
  219. list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
  220. if (mei_cl_cmp_id(cl, cb->cl))
  221. return cb;
  222. return NULL;
  223. }
  224. /** mei_cl_link: allocte host id in the host map
  225. *
  226. * @cl - host client
  227. * @id - fixed host id or -1 for genereting one
  228. * returns 0 on success
  229. * -EINVAL on incorrect values
  230. * -ENONET if client not found
  231. */
  232. int mei_cl_link(struct mei_cl *cl, int id)
  233. {
  234. struct mei_device *dev;
  235. if (WARN_ON(!cl || !cl->dev))
  236. return -EINVAL;
  237. dev = cl->dev;
  238. /* If Id is not asigned get one*/
  239. if (id == MEI_HOST_CLIENT_ID_ANY)
  240. id = find_first_zero_bit(dev->host_clients_map,
  241. MEI_CLIENTS_MAX);
  242. if (id >= MEI_CLIENTS_MAX) {
  243. dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
  244. return -ENOENT;
  245. }
  246. dev->open_handle_count++;
  247. cl->host_client_id = id;
  248. list_add_tail(&cl->link, &dev->file_list);
  249. set_bit(id, dev->host_clients_map);
  250. cl->state = MEI_FILE_INITIALIZING;
  251. dev_dbg(&dev->pdev->dev, "link cl host id = %d\n", cl->host_client_id);
  252. return 0;
  253. }
  254. /**
  255. * mei_cl_unlink - remove me_cl from the list
  256. *
  257. * @dev: the device structure
  258. */
  259. int mei_cl_unlink(struct mei_cl *cl)
  260. {
  261. struct mei_device *dev;
  262. struct mei_cl *pos, *next;
  263. /* don't shout on error exit path */
  264. if (!cl)
  265. return 0;
  266. /* wd and amthif might not be initialized */
  267. if (!cl->dev)
  268. return 0;
  269. dev = cl->dev;
  270. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  271. if (cl->host_client_id == pos->host_client_id) {
  272. dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
  273. pos->host_client_id, pos->me_client_id);
  274. list_del_init(&pos->link);
  275. break;
  276. }
  277. }
  278. return 0;
  279. }
  280. void mei_host_client_init(struct work_struct *work)
  281. {
  282. struct mei_device *dev = container_of(work,
  283. struct mei_device, init_work);
  284. struct mei_client_properties *client_props;
  285. int i;
  286. mutex_lock(&dev->device_lock);
  287. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  288. dev->open_handle_count = 0;
  289. /*
  290. * Reserving the first three client IDs
  291. * 0: Reserved for MEI Bus Message communications
  292. * 1: Reserved for Watchdog
  293. * 2: Reserved for AMTHI
  294. */
  295. bitmap_set(dev->host_clients_map, 0, 3);
  296. for (i = 0; i < dev->me_clients_num; i++) {
  297. client_props = &dev->me_clients[i].props;
  298. if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
  299. mei_amthif_host_init(dev);
  300. else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
  301. mei_wd_host_init(dev);
  302. else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
  303. mei_nfc_host_init(dev);
  304. }
  305. dev->dev_state = MEI_DEV_ENABLED;
  306. mutex_unlock(&dev->device_lock);
  307. }
  308. /**
  309. * mei_cl_disconnect - disconnect host clinet form the me one
  310. *
  311. * @cl: host client
  312. *
  313. * Locking: called under "dev->device_lock" lock
  314. *
  315. * returns 0 on success, <0 on failure.
  316. */
  317. int mei_cl_disconnect(struct mei_cl *cl)
  318. {
  319. struct mei_device *dev;
  320. struct mei_cl_cb *cb;
  321. int rets, err;
  322. if (WARN_ON(!cl || !cl->dev))
  323. return -ENODEV;
  324. dev = cl->dev;
  325. if (cl->state != MEI_FILE_DISCONNECTING)
  326. return 0;
  327. cb = mei_io_cb_init(cl, NULL);
  328. if (!cb)
  329. return -ENOMEM;
  330. cb->fop_type = MEI_FOP_CLOSE;
  331. if (dev->hbuf_is_ready) {
  332. dev->hbuf_is_ready = false;
  333. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  334. rets = -ENODEV;
  335. dev_err(&dev->pdev->dev, "failed to disconnect.\n");
  336. goto free;
  337. }
  338. mdelay(10); /* Wait for hardware disconnection ready */
  339. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  340. } else {
  341. dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
  342. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  343. }
  344. mutex_unlock(&dev->device_lock);
  345. err = wait_event_timeout(dev->wait_recvd_msg,
  346. MEI_FILE_DISCONNECTED == cl->state,
  347. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  348. mutex_lock(&dev->device_lock);
  349. if (MEI_FILE_DISCONNECTED == cl->state) {
  350. rets = 0;
  351. dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
  352. } else {
  353. rets = -ENODEV;
  354. if (MEI_FILE_DISCONNECTED != cl->state)
  355. dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
  356. if (err)
  357. dev_dbg(&dev->pdev->dev,
  358. "wait failed disconnect err=%08x\n",
  359. err);
  360. dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
  361. }
  362. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  363. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  364. free:
  365. mei_io_cb_free(cb);
  366. return rets;
  367. }
  368. /**
  369. * mei_cl_is_other_connecting - checks if other
  370. * client with the same me client id is connecting
  371. *
  372. * @cl: private data of the file object
  373. *
  374. * returns ture if other client is connected, 0 - otherwise.
  375. */
  376. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  377. {
  378. struct mei_device *dev;
  379. struct mei_cl *pos;
  380. struct mei_cl *next;
  381. if (WARN_ON(!cl || !cl->dev))
  382. return false;
  383. dev = cl->dev;
  384. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  385. if ((pos->state == MEI_FILE_CONNECTING) &&
  386. (pos != cl) && cl->me_client_id == pos->me_client_id)
  387. return true;
  388. }
  389. return false;
  390. }
  391. /**
  392. * mei_cl_connect - connect host clinet to the me one
  393. *
  394. * @cl: host client
  395. *
  396. * Locking: called under "dev->device_lock" lock
  397. *
  398. * returns 0 on success, <0 on failure.
  399. */
  400. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  401. {
  402. struct mei_device *dev;
  403. struct mei_cl_cb *cb;
  404. long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT);
  405. int rets;
  406. if (WARN_ON(!cl || !cl->dev))
  407. return -ENODEV;
  408. dev = cl->dev;
  409. cb = mei_io_cb_init(cl, file);
  410. if (!cb) {
  411. rets = -ENOMEM;
  412. goto out;
  413. }
  414. cb->fop_type = MEI_FOP_IOCTL;
  415. if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
  416. dev->hbuf_is_ready = false;
  417. if (mei_hbm_cl_connect_req(dev, cl)) {
  418. rets = -ENODEV;
  419. goto out;
  420. }
  421. cl->timer_count = MEI_CONNECT_TIMEOUT;
  422. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  423. } else {
  424. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  425. }
  426. mutex_unlock(&dev->device_lock);
  427. rets = wait_event_timeout(dev->wait_recvd_msg,
  428. (cl->state == MEI_FILE_CONNECTED ||
  429. cl->state == MEI_FILE_DISCONNECTED),
  430. timeout * HZ);
  431. mutex_lock(&dev->device_lock);
  432. if (cl->state != MEI_FILE_CONNECTED) {
  433. rets = -EFAULT;
  434. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  435. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  436. goto out;
  437. }
  438. rets = cl->status;
  439. out:
  440. mei_io_cb_free(cb);
  441. return rets;
  442. }
  443. /**
  444. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  445. *
  446. * @dev: the device structure
  447. * @cl: private data of the file object
  448. *
  449. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  450. * -ENOENT if mei_cl is not present
  451. * -EINVAL if single_recv_buf == 0
  452. */
  453. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  454. {
  455. struct mei_device *dev;
  456. int i;
  457. if (WARN_ON(!cl || !cl->dev))
  458. return -EINVAL;
  459. dev = cl->dev;
  460. if (!dev->me_clients_num)
  461. return 0;
  462. if (cl->mei_flow_ctrl_creds > 0)
  463. return 1;
  464. for (i = 0; i < dev->me_clients_num; i++) {
  465. struct mei_me_client *me_cl = &dev->me_clients[i];
  466. if (me_cl->client_id == cl->me_client_id) {
  467. if (me_cl->mei_flow_ctrl_creds) {
  468. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  469. return -EINVAL;
  470. return 1;
  471. } else {
  472. return 0;
  473. }
  474. }
  475. }
  476. return -ENOENT;
  477. }
  478. /**
  479. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  480. *
  481. * @dev: the device structure
  482. * @cl: private data of the file object
  483. * @returns
  484. * 0 on success
  485. * -ENOENT when me client is not found
  486. * -EINVAL when ctrl credits are <= 0
  487. */
  488. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  489. {
  490. struct mei_device *dev;
  491. int i;
  492. if (WARN_ON(!cl || !cl->dev))
  493. return -EINVAL;
  494. dev = cl->dev;
  495. if (!dev->me_clients_num)
  496. return -ENOENT;
  497. for (i = 0; i < dev->me_clients_num; i++) {
  498. struct mei_me_client *me_cl = &dev->me_clients[i];
  499. if (me_cl->client_id == cl->me_client_id) {
  500. if (me_cl->props.single_recv_buf != 0) {
  501. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  502. return -EINVAL;
  503. dev->me_clients[i].mei_flow_ctrl_creds--;
  504. } else {
  505. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  506. return -EINVAL;
  507. cl->mei_flow_ctrl_creds--;
  508. }
  509. return 0;
  510. }
  511. }
  512. return -ENOENT;
  513. }
  514. /**
  515. * mei_cl_start_read - the start read client message function.
  516. *
  517. * @cl: host client
  518. *
  519. * returns 0 on success, <0 on failure.
  520. */
  521. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  522. {
  523. struct mei_device *dev;
  524. struct mei_cl_cb *cb;
  525. int rets;
  526. int i;
  527. if (WARN_ON(!cl || !cl->dev))
  528. return -ENODEV;
  529. dev = cl->dev;
  530. if (cl->state != MEI_FILE_CONNECTED)
  531. return -ENODEV;
  532. if (dev->dev_state != MEI_DEV_ENABLED)
  533. return -ENODEV;
  534. if (cl->read_cb) {
  535. dev_dbg(&dev->pdev->dev, "read is pending.\n");
  536. return -EBUSY;
  537. }
  538. i = mei_me_cl_by_id(dev, cl->me_client_id);
  539. if (i < 0) {
  540. dev_err(&dev->pdev->dev, "no such me client %d\n",
  541. cl->me_client_id);
  542. return -ENODEV;
  543. }
  544. cb = mei_io_cb_init(cl, NULL);
  545. if (!cb)
  546. return -ENOMEM;
  547. /* always allocate at least client max message */
  548. length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
  549. rets = mei_io_cb_alloc_resp_buf(cb, length);
  550. if (rets)
  551. goto err;
  552. cb->fop_type = MEI_FOP_READ;
  553. cl->read_cb = cb;
  554. if (dev->hbuf_is_ready) {
  555. dev->hbuf_is_ready = false;
  556. if (mei_hbm_cl_flow_control_req(dev, cl)) {
  557. rets = -ENODEV;
  558. goto err;
  559. }
  560. list_add_tail(&cb->list, &dev->read_list.list);
  561. } else {
  562. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  563. }
  564. return rets;
  565. err:
  566. mei_io_cb_free(cb);
  567. return rets;
  568. }
  569. /**
  570. * mei_cl_write - submit a write cb to mei device
  571. assumes device_lock is locked
  572. *
  573. * @cl: host client
  574. * @cl: write callback with filled data
  575. *
  576. * returns numbe of bytes sent on success, <0 on failure.
  577. */
  578. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  579. {
  580. struct mei_device *dev;
  581. struct mei_msg_data *buf;
  582. struct mei_msg_hdr mei_hdr;
  583. int rets;
  584. if (WARN_ON(!cl || !cl->dev))
  585. return -ENODEV;
  586. if (WARN_ON(!cb))
  587. return -EINVAL;
  588. dev = cl->dev;
  589. buf = &cb->request_buffer;
  590. dev_dbg(&dev->pdev->dev, "mei_cl_write %d\n", buf->size);
  591. cb->fop_type = MEI_FOP_WRITE;
  592. rets = mei_cl_flow_ctrl_creds(cl);
  593. if (rets < 0)
  594. goto err;
  595. /* Host buffer is not ready, we queue the request */
  596. if (rets == 0 || !dev->hbuf_is_ready) {
  597. cb->buf_idx = 0;
  598. /* unseting complete will enqueue the cb for write */
  599. mei_hdr.msg_complete = 0;
  600. cl->writing_state = MEI_WRITING;
  601. rets = buf->size;
  602. goto out;
  603. }
  604. dev->hbuf_is_ready = false;
  605. /* Check for a maximum length */
  606. if (buf->size > mei_hbuf_max_len(dev)) {
  607. mei_hdr.length = mei_hbuf_max_len(dev);
  608. mei_hdr.msg_complete = 0;
  609. } else {
  610. mei_hdr.length = buf->size;
  611. mei_hdr.msg_complete = 1;
  612. }
  613. mei_hdr.host_addr = cl->host_client_id;
  614. mei_hdr.me_addr = cl->me_client_id;
  615. mei_hdr.reserved = 0;
  616. dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
  617. MEI_HDR_PRM(&mei_hdr));
  618. if (mei_write_message(dev, &mei_hdr, buf->data)) {
  619. rets = -EIO;
  620. goto err;
  621. }
  622. cl->writing_state = MEI_WRITING;
  623. cb->buf_idx = mei_hdr.length;
  624. rets = buf->size;
  625. out:
  626. if (mei_hdr.msg_complete) {
  627. if (mei_cl_flow_ctrl_reduce(cl)) {
  628. rets = -ENODEV;
  629. goto err;
  630. }
  631. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  632. } else {
  633. list_add_tail(&cb->list, &dev->write_list.list);
  634. }
  635. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  636. mutex_unlock(&dev->device_lock);
  637. if (wait_event_interruptible(cl->tx_wait,
  638. cl->writing_state == MEI_WRITE_COMPLETE)) {
  639. if (signal_pending(current))
  640. rets = -EINTR;
  641. else
  642. rets = -ERESTARTSYS;
  643. }
  644. mutex_lock(&dev->device_lock);
  645. }
  646. err:
  647. return rets;
  648. }
  649. /**
  650. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  651. *
  652. * @dev - mei device
  653. */
  654. void mei_cl_all_disconnect(struct mei_device *dev)
  655. {
  656. struct mei_cl *cl, *next;
  657. list_for_each_entry_safe(cl, next, &dev->file_list, link) {
  658. cl->state = MEI_FILE_DISCONNECTED;
  659. cl->mei_flow_ctrl_creds = 0;
  660. cl->read_cb = NULL;
  661. cl->timer_count = 0;
  662. }
  663. }
  664. /**
  665. * mei_cl_all_read_wakeup - wake up all readings so they can be interrupted
  666. *
  667. * @dev - mei device
  668. */
  669. void mei_cl_all_read_wakeup(struct mei_device *dev)
  670. {
  671. struct mei_cl *cl, *next;
  672. list_for_each_entry_safe(cl, next, &dev->file_list, link) {
  673. if (waitqueue_active(&cl->rx_wait)) {
  674. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  675. wake_up_interruptible(&cl->rx_wait);
  676. }
  677. }
  678. }
  679. /**
  680. * mei_cl_all_write_clear - clear all pending writes
  681. * @dev - mei device
  682. */
  683. void mei_cl_all_write_clear(struct mei_device *dev)
  684. {
  685. struct mei_cl_cb *cb, *next;
  686. list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
  687. list_del(&cb->list);
  688. mei_io_cb_free(cb);
  689. }
  690. }