client.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. * @fp: 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. * @length: 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_resp_buf - allocate respose buffer
  135. *
  136. * @cb: io callback structure
  137. * @length: 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. * @cl: host client
  159. */
  160. int mei_cl_flush_queues(struct mei_cl *cl)
  161. {
  162. struct mei_device *dev;
  163. if (WARN_ON(!cl || !cl->dev))
  164. return -EINVAL;
  165. dev = cl->dev;
  166. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  167. mei_io_list_flush(&cl->dev->read_list, cl);
  168. mei_io_list_flush(&cl->dev->write_list, cl);
  169. mei_io_list_flush(&cl->dev->write_waiting_list, cl);
  170. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  171. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  172. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  173. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  174. return 0;
  175. }
  176. /**
  177. * mei_cl_init - initializes intialize cl.
  178. *
  179. * @cl: host client to be initialized
  180. * @dev: mei device
  181. */
  182. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  183. {
  184. memset(cl, 0, sizeof(struct mei_cl));
  185. init_waitqueue_head(&cl->wait);
  186. init_waitqueue_head(&cl->rx_wait);
  187. init_waitqueue_head(&cl->tx_wait);
  188. INIT_LIST_HEAD(&cl->link);
  189. INIT_LIST_HEAD(&cl->device_link);
  190. cl->reading_state = MEI_IDLE;
  191. cl->writing_state = MEI_IDLE;
  192. cl->dev = dev;
  193. }
  194. /**
  195. * mei_cl_allocate - allocates cl structure and sets it up.
  196. *
  197. * @dev: mei device
  198. * returns The allocated file or NULL on failure
  199. */
  200. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  201. {
  202. struct mei_cl *cl;
  203. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  204. if (!cl)
  205. return NULL;
  206. mei_cl_init(cl, dev);
  207. return cl;
  208. }
  209. /**
  210. * mei_cl_find_read_cb - find this cl's callback in the read list
  211. *
  212. * @cl: host client
  213. *
  214. * returns cb on success, NULL on error
  215. */
  216. struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
  217. {
  218. struct mei_device *dev = cl->dev;
  219. struct mei_cl_cb *cb = NULL;
  220. struct mei_cl_cb *next = NULL;
  221. list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
  222. if (mei_cl_cmp_id(cl, cb->cl))
  223. return cb;
  224. return NULL;
  225. }
  226. /** mei_cl_link: allocte host id in the host map
  227. *
  228. * @cl - host client
  229. * @id - fixed host id or -1 for genereting one
  230. *
  231. * returns 0 on success
  232. * -EINVAL on incorrect values
  233. * -ENONET if client not found
  234. */
  235. int mei_cl_link(struct mei_cl *cl, int id)
  236. {
  237. struct mei_device *dev;
  238. if (WARN_ON(!cl || !cl->dev))
  239. return -EINVAL;
  240. dev = cl->dev;
  241. /* If Id is not asigned get one*/
  242. if (id == MEI_HOST_CLIENT_ID_ANY)
  243. id = find_first_zero_bit(dev->host_clients_map,
  244. MEI_CLIENTS_MAX);
  245. if (id >= MEI_CLIENTS_MAX) {
  246. dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
  247. return -ENOENT;
  248. }
  249. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  250. dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
  251. MEI_MAX_OPEN_HANDLE_COUNT);
  252. return -ENOENT;
  253. }
  254. dev->open_handle_count++;
  255. cl->host_client_id = id;
  256. list_add_tail(&cl->link, &dev->file_list);
  257. set_bit(id, dev->host_clients_map);
  258. cl->state = MEI_FILE_INITIALIZING;
  259. cl_dbg(dev, cl, "link cl\n");
  260. return 0;
  261. }
  262. /**
  263. * mei_cl_unlink - remove me_cl from the list
  264. *
  265. * @cl: host client
  266. */
  267. int mei_cl_unlink(struct mei_cl *cl)
  268. {
  269. struct mei_device *dev;
  270. struct mei_cl *pos, *next;
  271. /* don't shout on error exit path */
  272. if (!cl)
  273. return 0;
  274. /* wd and amthif might not be initialized */
  275. if (!cl->dev)
  276. return 0;
  277. dev = cl->dev;
  278. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  279. if (cl->host_client_id == pos->host_client_id) {
  280. cl_dbg(dev, cl, "remove host client = %d, ME client = %d\n",
  281. pos->host_client_id, pos->me_client_id);
  282. list_del_init(&pos->link);
  283. break;
  284. }
  285. }
  286. return 0;
  287. }
  288. void mei_host_client_init(struct work_struct *work)
  289. {
  290. struct mei_device *dev = container_of(work,
  291. struct mei_device, init_work);
  292. struct mei_client_properties *client_props;
  293. int i;
  294. mutex_lock(&dev->device_lock);
  295. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  296. dev->open_handle_count = 0;
  297. /*
  298. * Reserving the first three client IDs
  299. * 0: Reserved for MEI Bus Message communications
  300. * 1: Reserved for Watchdog
  301. * 2: Reserved for AMTHI
  302. */
  303. bitmap_set(dev->host_clients_map, 0, 3);
  304. for (i = 0; i < dev->me_clients_num; i++) {
  305. client_props = &dev->me_clients[i].props;
  306. if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
  307. mei_amthif_host_init(dev);
  308. else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
  309. mei_wd_host_init(dev);
  310. else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
  311. mei_nfc_host_init(dev);
  312. }
  313. dev->dev_state = MEI_DEV_ENABLED;
  314. mutex_unlock(&dev->device_lock);
  315. }
  316. /**
  317. * mei_cl_disconnect - disconnect host clinet form the me one
  318. *
  319. * @cl: host client
  320. *
  321. * Locking: called under "dev->device_lock" lock
  322. *
  323. * returns 0 on success, <0 on failure.
  324. */
  325. int mei_cl_disconnect(struct mei_cl *cl)
  326. {
  327. struct mei_device *dev;
  328. struct mei_cl_cb *cb;
  329. int rets, err;
  330. if (WARN_ON(!cl || !cl->dev))
  331. return -ENODEV;
  332. dev = cl->dev;
  333. cl_dbg(dev, cl, "disconnecting");
  334. if (cl->state != MEI_FILE_DISCONNECTING)
  335. return 0;
  336. cb = mei_io_cb_init(cl, NULL);
  337. if (!cb)
  338. return -ENOMEM;
  339. cb->fop_type = MEI_FOP_CLOSE;
  340. if (dev->hbuf_is_ready) {
  341. dev->hbuf_is_ready = false;
  342. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  343. rets = -ENODEV;
  344. cl_err(dev, cl, "failed to disconnect.\n");
  345. goto free;
  346. }
  347. mdelay(10); /* Wait for hardware disconnection ready */
  348. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  349. } else {
  350. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  351. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  352. }
  353. mutex_unlock(&dev->device_lock);
  354. err = wait_event_timeout(dev->wait_recvd_msg,
  355. MEI_FILE_DISCONNECTED == cl->state,
  356. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  357. mutex_lock(&dev->device_lock);
  358. if (MEI_FILE_DISCONNECTED == cl->state) {
  359. rets = 0;
  360. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  361. } else {
  362. rets = -ENODEV;
  363. if (MEI_FILE_DISCONNECTED != cl->state)
  364. cl_err(dev, cl, "wrong status client disconnect.\n");
  365. if (err)
  366. cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
  367. err);
  368. cl_err(dev, cl, "failed to disconnect from FW client.\n");
  369. }
  370. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  371. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  372. free:
  373. mei_io_cb_free(cb);
  374. return rets;
  375. }
  376. /**
  377. * mei_cl_is_other_connecting - checks if other
  378. * client with the same me client id is connecting
  379. *
  380. * @cl: private data of the file object
  381. *
  382. * returns ture if other client is connected, 0 - otherwise.
  383. */
  384. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  385. {
  386. struct mei_device *dev;
  387. struct mei_cl *pos;
  388. struct mei_cl *next;
  389. if (WARN_ON(!cl || !cl->dev))
  390. return false;
  391. dev = cl->dev;
  392. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  393. if ((pos->state == MEI_FILE_CONNECTING) &&
  394. (pos != cl) && cl->me_client_id == pos->me_client_id)
  395. return true;
  396. }
  397. return false;
  398. }
  399. /**
  400. * mei_cl_connect - connect host clinet to the me one
  401. *
  402. * @cl: host client
  403. *
  404. * Locking: called under "dev->device_lock" lock
  405. *
  406. * returns 0 on success, <0 on failure.
  407. */
  408. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  409. {
  410. struct mei_device *dev;
  411. struct mei_cl_cb *cb;
  412. int rets;
  413. if (WARN_ON(!cl || !cl->dev))
  414. return -ENODEV;
  415. dev = cl->dev;
  416. cb = mei_io_cb_init(cl, file);
  417. if (!cb) {
  418. rets = -ENOMEM;
  419. goto out;
  420. }
  421. cb->fop_type = MEI_FOP_IOCTL;
  422. if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
  423. dev->hbuf_is_ready = false;
  424. if (mei_hbm_cl_connect_req(dev, cl)) {
  425. rets = -ENODEV;
  426. goto out;
  427. }
  428. cl->timer_count = MEI_CONNECT_TIMEOUT;
  429. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  430. } else {
  431. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  432. }
  433. mutex_unlock(&dev->device_lock);
  434. rets = wait_event_timeout(dev->wait_recvd_msg,
  435. (cl->state == MEI_FILE_CONNECTED ||
  436. cl->state == MEI_FILE_DISCONNECTED),
  437. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  438. mutex_lock(&dev->device_lock);
  439. if (cl->state != MEI_FILE_CONNECTED) {
  440. rets = -EFAULT;
  441. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  442. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  443. goto out;
  444. }
  445. rets = cl->status;
  446. out:
  447. mei_io_cb_free(cb);
  448. return rets;
  449. }
  450. /**
  451. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  452. *
  453. * @cl: private data of the file object
  454. *
  455. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  456. * -ENOENT if mei_cl is not present
  457. * -EINVAL if single_recv_buf == 0
  458. */
  459. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  460. {
  461. struct mei_device *dev;
  462. int i;
  463. if (WARN_ON(!cl || !cl->dev))
  464. return -EINVAL;
  465. dev = cl->dev;
  466. if (!dev->me_clients_num)
  467. return 0;
  468. if (cl->mei_flow_ctrl_creds > 0)
  469. return 1;
  470. for (i = 0; i < dev->me_clients_num; i++) {
  471. struct mei_me_client *me_cl = &dev->me_clients[i];
  472. if (me_cl->client_id == cl->me_client_id) {
  473. if (me_cl->mei_flow_ctrl_creds) {
  474. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  475. return -EINVAL;
  476. return 1;
  477. } else {
  478. return 0;
  479. }
  480. }
  481. }
  482. return -ENOENT;
  483. }
  484. /**
  485. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  486. *
  487. * @cl: private data of the file object
  488. *
  489. * @returns
  490. * 0 on success
  491. * -ENOENT when me client is not found
  492. * -EINVAL when ctrl credits are <= 0
  493. */
  494. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  495. {
  496. struct mei_device *dev;
  497. int i;
  498. if (WARN_ON(!cl || !cl->dev))
  499. return -EINVAL;
  500. dev = cl->dev;
  501. if (!dev->me_clients_num)
  502. return -ENOENT;
  503. for (i = 0; i < dev->me_clients_num; i++) {
  504. struct mei_me_client *me_cl = &dev->me_clients[i];
  505. if (me_cl->client_id == cl->me_client_id) {
  506. if (me_cl->props.single_recv_buf != 0) {
  507. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  508. return -EINVAL;
  509. dev->me_clients[i].mei_flow_ctrl_creds--;
  510. } else {
  511. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  512. return -EINVAL;
  513. cl->mei_flow_ctrl_creds--;
  514. }
  515. return 0;
  516. }
  517. }
  518. return -ENOENT;
  519. }
  520. /**
  521. * mei_cl_read_start - the start read client message function.
  522. *
  523. * @cl: host client
  524. *
  525. * returns 0 on success, <0 on failure.
  526. */
  527. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  528. {
  529. struct mei_device *dev;
  530. struct mei_cl_cb *cb;
  531. int rets;
  532. int i;
  533. if (WARN_ON(!cl || !cl->dev))
  534. return -ENODEV;
  535. dev = cl->dev;
  536. if (!mei_cl_is_connected(cl))
  537. return -ENODEV;
  538. if (cl->read_cb) {
  539. cl_dbg(dev, cl, "read is pending.\n");
  540. return -EBUSY;
  541. }
  542. i = mei_me_cl_by_id(dev, cl->me_client_id);
  543. if (i < 0) {
  544. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  545. return -ENODEV;
  546. }
  547. cb = mei_io_cb_init(cl, NULL);
  548. if (!cb)
  549. return -ENOMEM;
  550. /* always allocate at least client max message */
  551. length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
  552. rets = mei_io_cb_alloc_resp_buf(cb, length);
  553. if (rets)
  554. goto err;
  555. cb->fop_type = MEI_FOP_READ;
  556. cl->read_cb = cb;
  557. if (dev->hbuf_is_ready) {
  558. dev->hbuf_is_ready = false;
  559. if (mei_hbm_cl_flow_control_req(dev, cl)) {
  560. cl_err(dev, cl, "flow control send failed\n");
  561. rets = -ENODEV;
  562. goto err;
  563. }
  564. list_add_tail(&cb->list, &dev->read_list.list);
  565. } else {
  566. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  567. }
  568. return rets;
  569. err:
  570. mei_io_cb_free(cb);
  571. return rets;
  572. }
  573. /**
  574. * mei_cl_irq_write_complete - write a message to device
  575. * from the interrupt thread context
  576. *
  577. * @cl: client
  578. * @cb: callback block.
  579. * @slots: free slots.
  580. * @cmpl_list: complete list.
  581. *
  582. * returns 0, OK; otherwise error.
  583. */
  584. int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
  585. s32 *slots, struct mei_cl_cb *cmpl_list)
  586. {
  587. struct mei_device *dev;
  588. struct mei_msg_data *buf;
  589. struct mei_msg_hdr mei_hdr;
  590. size_t len;
  591. u32 msg_slots;
  592. int rets;
  593. if (WARN_ON(!cl || !cl->dev))
  594. return -ENODEV;
  595. dev = cl->dev;
  596. buf = &cb->request_buffer;
  597. rets = mei_cl_flow_ctrl_creds(cl);
  598. if (rets < 0)
  599. return rets;
  600. if (rets == 0) {
  601. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  602. return 0;
  603. }
  604. len = buf->size - cb->buf_idx;
  605. msg_slots = mei_data2slots(len);
  606. mei_hdr.host_addr = cl->host_client_id;
  607. mei_hdr.me_addr = cl->me_client_id;
  608. mei_hdr.reserved = 0;
  609. if (*slots >= msg_slots) {
  610. mei_hdr.length = len;
  611. mei_hdr.msg_complete = 1;
  612. /* Split the message only if we can write the whole host buffer */
  613. } else if (*slots == dev->hbuf_depth) {
  614. msg_slots = *slots;
  615. len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  616. mei_hdr.length = len;
  617. mei_hdr.msg_complete = 0;
  618. } else {
  619. /* wait for next time the host buffer is empty */
  620. return 0;
  621. }
  622. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  623. cb->request_buffer.size, cb->buf_idx);
  624. *slots -= msg_slots;
  625. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  626. if (rets) {
  627. cl->status = rets;
  628. list_move_tail(&cb->list, &cmpl_list->list);
  629. return rets;
  630. }
  631. cl->status = 0;
  632. cl->writing_state = MEI_WRITING;
  633. cb->buf_idx += mei_hdr.length;
  634. if (mei_hdr.msg_complete) {
  635. if (mei_cl_flow_ctrl_reduce(cl))
  636. return -EIO;
  637. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  638. }
  639. return 0;
  640. }
  641. /**
  642. * mei_cl_write - submit a write cb to mei device
  643. assumes device_lock is locked
  644. *
  645. * @cl: host client
  646. * @cl: write callback with filled data
  647. *
  648. * returns numbe of bytes sent on success, <0 on failure.
  649. */
  650. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  651. {
  652. struct mei_device *dev;
  653. struct mei_msg_data *buf;
  654. struct mei_msg_hdr mei_hdr;
  655. int rets;
  656. if (WARN_ON(!cl || !cl->dev))
  657. return -ENODEV;
  658. if (WARN_ON(!cb))
  659. return -EINVAL;
  660. dev = cl->dev;
  661. buf = &cb->request_buffer;
  662. cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
  663. cb->fop_type = MEI_FOP_WRITE;
  664. rets = mei_cl_flow_ctrl_creds(cl);
  665. if (rets < 0)
  666. goto err;
  667. /* Host buffer is not ready, we queue the request */
  668. if (rets == 0 || !dev->hbuf_is_ready) {
  669. cb->buf_idx = 0;
  670. /* unseting complete will enqueue the cb for write */
  671. mei_hdr.msg_complete = 0;
  672. rets = buf->size;
  673. goto out;
  674. }
  675. dev->hbuf_is_ready = false;
  676. /* Check for a maximum length */
  677. if (buf->size > mei_hbuf_max_len(dev)) {
  678. mei_hdr.length = mei_hbuf_max_len(dev);
  679. mei_hdr.msg_complete = 0;
  680. } else {
  681. mei_hdr.length = buf->size;
  682. mei_hdr.msg_complete = 1;
  683. }
  684. mei_hdr.host_addr = cl->host_client_id;
  685. mei_hdr.me_addr = cl->me_client_id;
  686. mei_hdr.reserved = 0;
  687. rets = mei_write_message(dev, &mei_hdr, buf->data);
  688. if (rets)
  689. goto err;
  690. cl->writing_state = MEI_WRITING;
  691. cb->buf_idx = mei_hdr.length;
  692. rets = buf->size;
  693. out:
  694. if (mei_hdr.msg_complete) {
  695. if (mei_cl_flow_ctrl_reduce(cl)) {
  696. rets = -ENODEV;
  697. goto err;
  698. }
  699. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  700. } else {
  701. list_add_tail(&cb->list, &dev->write_list.list);
  702. }
  703. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  704. mutex_unlock(&dev->device_lock);
  705. if (wait_event_interruptible(cl->tx_wait,
  706. cl->writing_state == MEI_WRITE_COMPLETE)) {
  707. if (signal_pending(current))
  708. rets = -EINTR;
  709. else
  710. rets = -ERESTARTSYS;
  711. }
  712. mutex_lock(&dev->device_lock);
  713. }
  714. err:
  715. return rets;
  716. }
  717. /**
  718. * mei_cl_complete - processes completed operation for a client
  719. *
  720. * @cl: private data of the file object.
  721. * @cb: callback block.
  722. */
  723. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  724. {
  725. if (cb->fop_type == MEI_FOP_WRITE) {
  726. mei_io_cb_free(cb);
  727. cb = NULL;
  728. cl->writing_state = MEI_WRITE_COMPLETE;
  729. if (waitqueue_active(&cl->tx_wait))
  730. wake_up_interruptible(&cl->tx_wait);
  731. } else if (cb->fop_type == MEI_FOP_READ &&
  732. MEI_READING == cl->reading_state) {
  733. cl->reading_state = MEI_READ_COMPLETE;
  734. if (waitqueue_active(&cl->rx_wait))
  735. wake_up_interruptible(&cl->rx_wait);
  736. else
  737. mei_cl_bus_rx_event(cl);
  738. }
  739. }
  740. /**
  741. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  742. *
  743. * @dev - mei device
  744. */
  745. void mei_cl_all_disconnect(struct mei_device *dev)
  746. {
  747. struct mei_cl *cl, *next;
  748. list_for_each_entry_safe(cl, next, &dev->file_list, link) {
  749. cl->state = MEI_FILE_DISCONNECTED;
  750. cl->mei_flow_ctrl_creds = 0;
  751. cl->read_cb = NULL;
  752. cl->timer_count = 0;
  753. }
  754. }
  755. /**
  756. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  757. *
  758. * @dev - mei device
  759. */
  760. void mei_cl_all_wakeup(struct mei_device *dev)
  761. {
  762. struct mei_cl *cl, *next;
  763. list_for_each_entry_safe(cl, next, &dev->file_list, link) {
  764. if (waitqueue_active(&cl->rx_wait)) {
  765. cl_dbg(dev, cl, "Waking up reading client!\n");
  766. wake_up_interruptible(&cl->rx_wait);
  767. }
  768. if (waitqueue_active(&cl->tx_wait)) {
  769. cl_dbg(dev, cl, "Waking up writing client!\n");
  770. wake_up_interruptible(&cl->tx_wait);
  771. }
  772. }
  773. }
  774. /**
  775. * mei_cl_all_write_clear - clear all pending writes
  776. * @dev - mei device
  777. */
  778. void mei_cl_all_write_clear(struct mei_device *dev)
  779. {
  780. struct mei_cl_cb *cb, *next;
  781. list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
  782. list_del(&cb->list);
  783. mei_io_cb_free(cb);
  784. }
  785. }