client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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, err;
  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. mdelay(10); /* Wait for hardware disconnection ready */
  410. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  411. } else {
  412. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  413. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  414. }
  415. mutex_unlock(&dev->device_lock);
  416. err = wait_event_timeout(dev->wait_recvd_msg,
  417. MEI_FILE_DISCONNECTED == cl->state,
  418. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  419. mutex_lock(&dev->device_lock);
  420. if (MEI_FILE_DISCONNECTED == cl->state) {
  421. rets = 0;
  422. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  423. } else {
  424. rets = -ENODEV;
  425. if (MEI_FILE_DISCONNECTED != cl->state)
  426. cl_err(dev, cl, "wrong status client disconnect.\n");
  427. if (err)
  428. cl_dbg(dev, cl, "wait failed disconnect err=%d\n", err);
  429. cl_err(dev, cl, "failed to disconnect from FW client.\n");
  430. }
  431. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  432. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  433. free:
  434. cl_dbg(dev, cl, "rpm: autosuspend\n");
  435. pm_runtime_mark_last_busy(&dev->pdev->dev);
  436. pm_runtime_put_autosuspend(&dev->pdev->dev);
  437. mei_io_cb_free(cb);
  438. return rets;
  439. }
  440. /**
  441. * mei_cl_is_other_connecting - checks if other
  442. * client with the same me client id is connecting
  443. *
  444. * @cl: private data of the file object
  445. *
  446. * returns true if other client is connected, false - otherwise.
  447. */
  448. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  449. {
  450. struct mei_device *dev;
  451. struct mei_cl *ocl; /* the other client */
  452. if (WARN_ON(!cl || !cl->dev))
  453. return false;
  454. dev = cl->dev;
  455. list_for_each_entry(ocl, &dev->file_list, link) {
  456. if (ocl->state == MEI_FILE_CONNECTING &&
  457. ocl != cl &&
  458. cl->me_client_id == ocl->me_client_id)
  459. return true;
  460. }
  461. return false;
  462. }
  463. /**
  464. * mei_cl_connect - connect host client to the me one
  465. *
  466. * @cl: host client
  467. *
  468. * Locking: called under "dev->device_lock" lock
  469. *
  470. * returns 0 on success, <0 on failure.
  471. */
  472. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  473. {
  474. struct mei_device *dev;
  475. struct mei_cl_cb *cb;
  476. int rets;
  477. if (WARN_ON(!cl || !cl->dev))
  478. return -ENODEV;
  479. dev = cl->dev;
  480. rets = pm_runtime_get(&dev->pdev->dev);
  481. if (rets < 0 && rets != -EINPROGRESS) {
  482. pm_runtime_put_noidle(&dev->pdev->dev);
  483. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  484. return rets;
  485. }
  486. cb = mei_io_cb_init(cl, file);
  487. if (!cb) {
  488. rets = -ENOMEM;
  489. goto out;
  490. }
  491. cb->fop_type = MEI_FOP_CONNECT;
  492. /* run hbuf acquire last so we don't have to undo */
  493. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  494. cl->state = MEI_FILE_CONNECTING;
  495. if (mei_hbm_cl_connect_req(dev, cl)) {
  496. rets = -ENODEV;
  497. goto out;
  498. }
  499. cl->timer_count = MEI_CONNECT_TIMEOUT;
  500. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  501. } else {
  502. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  503. }
  504. mutex_unlock(&dev->device_lock);
  505. wait_event_timeout(dev->wait_recvd_msg,
  506. (cl->state == MEI_FILE_CONNECTED ||
  507. cl->state == MEI_FILE_DISCONNECTED),
  508. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  509. mutex_lock(&dev->device_lock);
  510. if (cl->state != MEI_FILE_CONNECTED) {
  511. cl->state = MEI_FILE_DISCONNECTED;
  512. /* something went really wrong */
  513. if (!cl->status)
  514. cl->status = -EFAULT;
  515. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  516. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  517. }
  518. rets = cl->status;
  519. out:
  520. cl_dbg(dev, cl, "rpm: autosuspend\n");
  521. pm_runtime_mark_last_busy(&dev->pdev->dev);
  522. pm_runtime_put_autosuspend(&dev->pdev->dev);
  523. mei_io_cb_free(cb);
  524. return rets;
  525. }
  526. /**
  527. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  528. *
  529. * @cl: private data of the file object
  530. *
  531. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  532. * -ENOENT if mei_cl is not present
  533. * -EINVAL if single_recv_buf == 0
  534. */
  535. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  536. {
  537. struct mei_device *dev;
  538. struct mei_me_client *me_cl;
  539. int id;
  540. if (WARN_ON(!cl || !cl->dev))
  541. return -EINVAL;
  542. dev = cl->dev;
  543. if (!dev->me_clients_num)
  544. return 0;
  545. if (cl->mei_flow_ctrl_creds > 0)
  546. return 1;
  547. id = mei_me_cl_by_id(dev, cl->me_client_id);
  548. if (id < 0) {
  549. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  550. return id;
  551. }
  552. me_cl = &dev->me_clients[id];
  553. if (me_cl->mei_flow_ctrl_creds) {
  554. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  555. return -EINVAL;
  556. return 1;
  557. }
  558. return 0;
  559. }
  560. /**
  561. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  562. *
  563. * @cl: private data of the file object
  564. *
  565. * @returns
  566. * 0 on success
  567. * -ENOENT when me client is not found
  568. * -EINVAL when ctrl credits are <= 0
  569. */
  570. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  571. {
  572. struct mei_device *dev;
  573. struct mei_me_client *me_cl;
  574. int id;
  575. if (WARN_ON(!cl || !cl->dev))
  576. return -EINVAL;
  577. dev = cl->dev;
  578. id = mei_me_cl_by_id(dev, cl->me_client_id);
  579. if (id < 0) {
  580. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  581. return id;
  582. }
  583. me_cl = &dev->me_clients[id];
  584. if (me_cl->props.single_recv_buf != 0) {
  585. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  586. return -EINVAL;
  587. me_cl->mei_flow_ctrl_creds--;
  588. } else {
  589. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  590. return -EINVAL;
  591. cl->mei_flow_ctrl_creds--;
  592. }
  593. return 0;
  594. }
  595. /**
  596. * mei_cl_read_start - the start read client message function.
  597. *
  598. * @cl: host client
  599. *
  600. * returns 0 on success, <0 on failure.
  601. */
  602. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  603. {
  604. struct mei_device *dev;
  605. struct mei_cl_cb *cb;
  606. int rets;
  607. int i;
  608. if (WARN_ON(!cl || !cl->dev))
  609. return -ENODEV;
  610. dev = cl->dev;
  611. if (!mei_cl_is_connected(cl))
  612. return -ENODEV;
  613. if (cl->read_cb) {
  614. cl_dbg(dev, cl, "read is pending.\n");
  615. return -EBUSY;
  616. }
  617. i = mei_me_cl_by_id(dev, cl->me_client_id);
  618. if (i < 0) {
  619. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  620. return -ENOTTY;
  621. }
  622. rets = pm_runtime_get(&dev->pdev->dev);
  623. if (rets < 0 && rets != -EINPROGRESS) {
  624. pm_runtime_put_noidle(&dev->pdev->dev);
  625. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  626. return rets;
  627. }
  628. cb = mei_io_cb_init(cl, NULL);
  629. if (!cb) {
  630. rets = -ENOMEM;
  631. goto out;
  632. }
  633. /* always allocate at least client max message */
  634. length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
  635. rets = mei_io_cb_alloc_resp_buf(cb, length);
  636. if (rets)
  637. goto out;
  638. cb->fop_type = MEI_FOP_READ;
  639. if (mei_hbuf_acquire(dev)) {
  640. rets = mei_hbm_cl_flow_control_req(dev, cl);
  641. if (rets < 0)
  642. goto out;
  643. list_add_tail(&cb->list, &dev->read_list.list);
  644. } else {
  645. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  646. }
  647. cl->read_cb = cb;
  648. out:
  649. cl_dbg(dev, cl, "rpm: autosuspend\n");
  650. pm_runtime_mark_last_busy(&dev->pdev->dev);
  651. pm_runtime_put_autosuspend(&dev->pdev->dev);
  652. if (rets)
  653. mei_io_cb_free(cb);
  654. return rets;
  655. }
  656. /**
  657. * mei_cl_irq_write - write a message to device
  658. * from the interrupt thread context
  659. *
  660. * @cl: client
  661. * @cb: callback block.
  662. * @cmpl_list: complete list.
  663. *
  664. * returns 0, OK; otherwise error.
  665. */
  666. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  667. struct mei_cl_cb *cmpl_list)
  668. {
  669. struct mei_device *dev;
  670. struct mei_msg_data *buf;
  671. struct mei_msg_hdr mei_hdr;
  672. size_t len;
  673. u32 msg_slots;
  674. int slots;
  675. int rets;
  676. if (WARN_ON(!cl || !cl->dev))
  677. return -ENODEV;
  678. dev = cl->dev;
  679. buf = &cb->request_buffer;
  680. rets = mei_cl_flow_ctrl_creds(cl);
  681. if (rets < 0)
  682. return rets;
  683. if (rets == 0) {
  684. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  685. return 0;
  686. }
  687. slots = mei_hbuf_empty_slots(dev);
  688. len = buf->size - cb->buf_idx;
  689. msg_slots = mei_data2slots(len);
  690. mei_hdr.host_addr = cl->host_client_id;
  691. mei_hdr.me_addr = cl->me_client_id;
  692. mei_hdr.reserved = 0;
  693. mei_hdr.internal = cb->internal;
  694. if (slots >= msg_slots) {
  695. mei_hdr.length = len;
  696. mei_hdr.msg_complete = 1;
  697. /* Split the message only if we can write the whole host buffer */
  698. } else if (slots == dev->hbuf_depth) {
  699. msg_slots = slots;
  700. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  701. mei_hdr.length = len;
  702. mei_hdr.msg_complete = 0;
  703. } else {
  704. /* wait for next time the host buffer is empty */
  705. return 0;
  706. }
  707. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  708. cb->request_buffer.size, cb->buf_idx);
  709. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  710. if (rets) {
  711. cl->status = rets;
  712. list_move_tail(&cb->list, &cmpl_list->list);
  713. return rets;
  714. }
  715. cl->status = 0;
  716. cl->writing_state = MEI_WRITING;
  717. cb->buf_idx += mei_hdr.length;
  718. if (mei_hdr.msg_complete) {
  719. if (mei_cl_flow_ctrl_reduce(cl))
  720. return -EIO;
  721. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  722. }
  723. return 0;
  724. }
  725. /**
  726. * mei_cl_write - submit a write cb to mei device
  727. assumes device_lock is locked
  728. *
  729. * @cl: host client
  730. * @cl: write callback with filled data
  731. *
  732. * returns number of bytes sent on success, <0 on failure.
  733. */
  734. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  735. {
  736. struct mei_device *dev;
  737. struct mei_msg_data *buf;
  738. struct mei_msg_hdr mei_hdr;
  739. int rets;
  740. if (WARN_ON(!cl || !cl->dev))
  741. return -ENODEV;
  742. if (WARN_ON(!cb))
  743. return -EINVAL;
  744. dev = cl->dev;
  745. buf = &cb->request_buffer;
  746. cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
  747. rets = pm_runtime_get(&dev->pdev->dev);
  748. if (rets < 0 && rets != -EINPROGRESS) {
  749. pm_runtime_put_noidle(&dev->pdev->dev);
  750. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  751. return rets;
  752. }
  753. cb->fop_type = MEI_FOP_WRITE;
  754. cb->buf_idx = 0;
  755. cl->writing_state = MEI_IDLE;
  756. mei_hdr.host_addr = cl->host_client_id;
  757. mei_hdr.me_addr = cl->me_client_id;
  758. mei_hdr.reserved = 0;
  759. mei_hdr.msg_complete = 0;
  760. mei_hdr.internal = cb->internal;
  761. rets = mei_cl_flow_ctrl_creds(cl);
  762. if (rets < 0)
  763. goto err;
  764. if (rets == 0) {
  765. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  766. rets = buf->size;
  767. goto out;
  768. }
  769. if (!mei_hbuf_acquire(dev)) {
  770. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  771. rets = buf->size;
  772. goto out;
  773. }
  774. /* Check for a maximum length */
  775. if (buf->size > mei_hbuf_max_len(dev)) {
  776. mei_hdr.length = mei_hbuf_max_len(dev);
  777. mei_hdr.msg_complete = 0;
  778. } else {
  779. mei_hdr.length = buf->size;
  780. mei_hdr.msg_complete = 1;
  781. }
  782. rets = mei_write_message(dev, &mei_hdr, buf->data);
  783. if (rets)
  784. goto err;
  785. cl->writing_state = MEI_WRITING;
  786. cb->buf_idx = mei_hdr.length;
  787. out:
  788. if (mei_hdr.msg_complete) {
  789. rets = mei_cl_flow_ctrl_reduce(cl);
  790. if (rets < 0)
  791. goto err;
  792. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  793. } else {
  794. list_add_tail(&cb->list, &dev->write_list.list);
  795. }
  796. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  797. mutex_unlock(&dev->device_lock);
  798. rets = wait_event_interruptible(cl->tx_wait,
  799. cl->writing_state == MEI_WRITE_COMPLETE);
  800. mutex_lock(&dev->device_lock);
  801. /* wait_event_interruptible returns -ERESTARTSYS */
  802. if (rets) {
  803. if (signal_pending(current))
  804. rets = -EINTR;
  805. goto err;
  806. }
  807. }
  808. rets = buf->size;
  809. err:
  810. cl_dbg(dev, cl, "rpm: autosuspend\n");
  811. pm_runtime_mark_last_busy(&dev->pdev->dev);
  812. pm_runtime_put_autosuspend(&dev->pdev->dev);
  813. return rets;
  814. }
  815. /**
  816. * mei_cl_complete - processes completed operation for a client
  817. *
  818. * @cl: private data of the file object.
  819. * @cb: callback block.
  820. */
  821. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  822. {
  823. if (cb->fop_type == MEI_FOP_WRITE) {
  824. mei_io_cb_free(cb);
  825. cb = NULL;
  826. cl->writing_state = MEI_WRITE_COMPLETE;
  827. if (waitqueue_active(&cl->tx_wait))
  828. wake_up_interruptible(&cl->tx_wait);
  829. } else if (cb->fop_type == MEI_FOP_READ &&
  830. MEI_READING == cl->reading_state) {
  831. cl->reading_state = MEI_READ_COMPLETE;
  832. if (waitqueue_active(&cl->rx_wait))
  833. wake_up_interruptible(&cl->rx_wait);
  834. else
  835. mei_cl_bus_rx_event(cl);
  836. }
  837. }
  838. /**
  839. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  840. *
  841. * @dev - mei device
  842. */
  843. void mei_cl_all_disconnect(struct mei_device *dev)
  844. {
  845. struct mei_cl *cl;
  846. list_for_each_entry(cl, &dev->file_list, link) {
  847. cl->state = MEI_FILE_DISCONNECTED;
  848. cl->mei_flow_ctrl_creds = 0;
  849. cl->timer_count = 0;
  850. }
  851. }
  852. /**
  853. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  854. *
  855. * @dev - mei device
  856. */
  857. void mei_cl_all_wakeup(struct mei_device *dev)
  858. {
  859. struct mei_cl *cl;
  860. list_for_each_entry(cl, &dev->file_list, link) {
  861. if (waitqueue_active(&cl->rx_wait)) {
  862. cl_dbg(dev, cl, "Waking up reading client!\n");
  863. wake_up_interruptible(&cl->rx_wait);
  864. }
  865. if (waitqueue_active(&cl->tx_wait)) {
  866. cl_dbg(dev, cl, "Waking up writing client!\n");
  867. wake_up_interruptible(&cl->tx_wait);
  868. }
  869. }
  870. }
  871. /**
  872. * mei_cl_all_write_clear - clear all pending writes
  873. * @dev - mei device
  874. */
  875. void mei_cl_all_write_clear(struct mei_device *dev)
  876. {
  877. mei_io_list_free(&dev->write_list, NULL);
  878. mei_io_list_free(&dev->write_waiting_list, NULL);
  879. }