client.c 23 KB

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