client.c 20 KB

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