client.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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 || 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. * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
  242. */
  243. int mei_cl_flush_queues(struct mei_cl *cl)
  244. {
  245. struct mei_device *dev;
  246. if (WARN_ON(!cl || !cl->dev))
  247. return -EINVAL;
  248. dev = cl->dev;
  249. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  250. mei_io_list_flush(&cl->dev->read_list, cl);
  251. mei_io_list_free(&cl->dev->write_list, cl);
  252. mei_io_list_free(&cl->dev->write_waiting_list, cl);
  253. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  254. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  255. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  256. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  257. return 0;
  258. }
  259. /**
  260. * mei_cl_init - initializes cl.
  261. *
  262. * @cl: host client to be initialized
  263. * @dev: mei device
  264. */
  265. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  266. {
  267. memset(cl, 0, sizeof(struct mei_cl));
  268. init_waitqueue_head(&cl->wait);
  269. init_waitqueue_head(&cl->rx_wait);
  270. init_waitqueue_head(&cl->tx_wait);
  271. INIT_LIST_HEAD(&cl->link);
  272. INIT_LIST_HEAD(&cl->device_link);
  273. cl->reading_state = MEI_IDLE;
  274. cl->writing_state = MEI_IDLE;
  275. cl->dev = dev;
  276. }
  277. /**
  278. * mei_cl_allocate - allocates cl structure and sets it up.
  279. *
  280. * @dev: mei device
  281. * Return: The allocated file or NULL on failure
  282. */
  283. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  284. {
  285. struct mei_cl *cl;
  286. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  287. if (!cl)
  288. return NULL;
  289. mei_cl_init(cl, dev);
  290. return cl;
  291. }
  292. /**
  293. * mei_cl_find_read_cb - find this cl's callback in the read list
  294. *
  295. * @cl: host client
  296. *
  297. * Return: cb on success, NULL on error
  298. */
  299. struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
  300. {
  301. struct mei_device *dev = cl->dev;
  302. struct mei_cl_cb *cb;
  303. list_for_each_entry(cb, &dev->read_list.list, list)
  304. if (mei_cl_cmp_id(cl, cb->cl))
  305. return cb;
  306. return NULL;
  307. }
  308. /** mei_cl_link: allocate host id in the host map
  309. *
  310. * @cl - host client
  311. * @id - fixed host id or -1 for generic one
  312. *
  313. * Return: 0 on success
  314. * -EINVAL on incorrect values
  315. * -ENONET if client not found
  316. */
  317. int mei_cl_link(struct mei_cl *cl, int id)
  318. {
  319. struct mei_device *dev;
  320. long open_handle_count;
  321. if (WARN_ON(!cl || !cl->dev))
  322. return -EINVAL;
  323. dev = cl->dev;
  324. /* If Id is not assigned get one*/
  325. if (id == MEI_HOST_CLIENT_ID_ANY)
  326. id = find_first_zero_bit(dev->host_clients_map,
  327. MEI_CLIENTS_MAX);
  328. if (id >= MEI_CLIENTS_MAX) {
  329. dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  330. return -EMFILE;
  331. }
  332. open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
  333. if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  334. dev_err(dev->dev, "open_handle_count exceeded %d",
  335. MEI_MAX_OPEN_HANDLE_COUNT);
  336. return -EMFILE;
  337. }
  338. dev->open_handle_count++;
  339. cl->host_client_id = id;
  340. list_add_tail(&cl->link, &dev->file_list);
  341. set_bit(id, dev->host_clients_map);
  342. cl->state = MEI_FILE_INITIALIZING;
  343. cl_dbg(dev, cl, "link cl\n");
  344. return 0;
  345. }
  346. /**
  347. * mei_cl_unlink - remove me_cl from the list
  348. *
  349. * @cl: host client
  350. *
  351. * Return: always 0
  352. */
  353. int mei_cl_unlink(struct mei_cl *cl)
  354. {
  355. struct mei_device *dev;
  356. /* don't shout on error exit path */
  357. if (!cl)
  358. return 0;
  359. /* wd and amthif might not be initialized */
  360. if (!cl->dev)
  361. return 0;
  362. dev = cl->dev;
  363. cl_dbg(dev, cl, "unlink client");
  364. if (dev->open_handle_count > 0)
  365. dev->open_handle_count--;
  366. /* never clear the 0 bit */
  367. if (cl->host_client_id)
  368. clear_bit(cl->host_client_id, dev->host_clients_map);
  369. list_del_init(&cl->link);
  370. cl->state = MEI_FILE_INITIALIZING;
  371. return 0;
  372. }
  373. void mei_host_client_init(struct work_struct *work)
  374. {
  375. struct mei_device *dev = container_of(work,
  376. struct mei_device, init_work);
  377. struct mei_me_client *me_cl;
  378. struct mei_client_properties *props;
  379. mutex_lock(&dev->device_lock);
  380. list_for_each_entry(me_cl, &dev->me_clients, list) {
  381. props = &me_cl->props;
  382. if (!uuid_le_cmp(props->protocol_name, mei_amthif_guid))
  383. mei_amthif_host_init(dev);
  384. else if (!uuid_le_cmp(props->protocol_name, mei_wd_guid))
  385. mei_wd_host_init(dev);
  386. else if (!uuid_le_cmp(props->protocol_name, mei_nfc_guid))
  387. mei_nfc_host_init(dev);
  388. }
  389. dev->dev_state = MEI_DEV_ENABLED;
  390. dev->reset_count = 0;
  391. mutex_unlock(&dev->device_lock);
  392. pm_runtime_mark_last_busy(dev->dev);
  393. dev_dbg(dev->dev, "rpm: autosuspend\n");
  394. pm_runtime_autosuspend(dev->dev);
  395. }
  396. /**
  397. * mei_hbuf_acquire - try to acquire host buffer
  398. *
  399. * @dev: the device structure
  400. * Return: true if host buffer was acquired
  401. */
  402. bool mei_hbuf_acquire(struct mei_device *dev)
  403. {
  404. if (mei_pg_state(dev) == MEI_PG_ON ||
  405. dev->pg_event == MEI_PG_EVENT_WAIT) {
  406. dev_dbg(dev->dev, "device is in pg\n");
  407. return false;
  408. }
  409. if (!dev->hbuf_is_ready) {
  410. dev_dbg(dev->dev, "hbuf is not ready\n");
  411. return false;
  412. }
  413. dev->hbuf_is_ready = false;
  414. return true;
  415. }
  416. /**
  417. * mei_cl_disconnect - disconnect host client from the me one
  418. *
  419. * @cl: host client
  420. *
  421. * Locking: called under "dev->device_lock" lock
  422. *
  423. * Return: 0 on success, <0 on failure.
  424. */
  425. int mei_cl_disconnect(struct mei_cl *cl)
  426. {
  427. struct mei_device *dev;
  428. struct mei_cl_cb *cb;
  429. int rets;
  430. if (WARN_ON(!cl || !cl->dev))
  431. return -ENODEV;
  432. dev = cl->dev;
  433. cl_dbg(dev, cl, "disconnecting");
  434. if (cl->state != MEI_FILE_DISCONNECTING)
  435. return 0;
  436. rets = pm_runtime_get(dev->dev);
  437. if (rets < 0 && rets != -EINPROGRESS) {
  438. pm_runtime_put_noidle(dev->dev);
  439. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  440. return rets;
  441. }
  442. cb = mei_io_cb_init(cl, NULL);
  443. if (!cb) {
  444. rets = -ENOMEM;
  445. goto free;
  446. }
  447. cb->fop_type = MEI_FOP_DISCONNECT;
  448. if (mei_hbuf_acquire(dev)) {
  449. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  450. rets = -ENODEV;
  451. cl_err(dev, cl, "failed to disconnect.\n");
  452. goto free;
  453. }
  454. cl->timer_count = MEI_CONNECT_TIMEOUT;
  455. mdelay(10); /* Wait for hardware disconnection ready */
  456. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  457. } else {
  458. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  459. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  460. }
  461. mutex_unlock(&dev->device_lock);
  462. wait_event_timeout(cl->wait,
  463. MEI_FILE_DISCONNECTED == cl->state,
  464. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  465. mutex_lock(&dev->device_lock);
  466. if (MEI_FILE_DISCONNECTED == cl->state) {
  467. rets = 0;
  468. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  469. } else {
  470. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  471. rets = -ETIME;
  472. }
  473. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  474. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  475. free:
  476. cl_dbg(dev, cl, "rpm: autosuspend\n");
  477. pm_runtime_mark_last_busy(dev->dev);
  478. pm_runtime_put_autosuspend(dev->dev);
  479. mei_io_cb_free(cb);
  480. return rets;
  481. }
  482. /**
  483. * mei_cl_is_other_connecting - checks if other
  484. * client with the same me client id is connecting
  485. *
  486. * @cl: private data of the file object
  487. *
  488. * Return: true if other client is connected, false - otherwise.
  489. */
  490. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  491. {
  492. struct mei_device *dev;
  493. struct mei_cl *ocl; /* the other client */
  494. if (WARN_ON(!cl || !cl->dev))
  495. return false;
  496. dev = cl->dev;
  497. list_for_each_entry(ocl, &dev->file_list, link) {
  498. if (ocl->state == MEI_FILE_CONNECTING &&
  499. ocl != cl &&
  500. cl->me_client_id == ocl->me_client_id)
  501. return true;
  502. }
  503. return false;
  504. }
  505. /**
  506. * mei_cl_connect - connect host client to the me one
  507. *
  508. * @cl: host client
  509. * @file: pointer to file structure
  510. *
  511. * Locking: called under "dev->device_lock" lock
  512. *
  513. * Return: 0 on success, <0 on failure.
  514. */
  515. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  516. {
  517. struct mei_device *dev;
  518. struct mei_cl_cb *cb;
  519. int rets;
  520. if (WARN_ON(!cl || !cl->dev))
  521. return -ENODEV;
  522. dev = cl->dev;
  523. rets = pm_runtime_get(dev->dev);
  524. if (rets < 0 && rets != -EINPROGRESS) {
  525. pm_runtime_put_noidle(dev->dev);
  526. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  527. return rets;
  528. }
  529. cb = mei_io_cb_init(cl, file);
  530. if (!cb) {
  531. rets = -ENOMEM;
  532. goto out;
  533. }
  534. cb->fop_type = MEI_FOP_CONNECT;
  535. /* run hbuf acquire last so we don't have to undo */
  536. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  537. cl->state = MEI_FILE_CONNECTING;
  538. if (mei_hbm_cl_connect_req(dev, cl)) {
  539. rets = -ENODEV;
  540. goto out;
  541. }
  542. cl->timer_count = MEI_CONNECT_TIMEOUT;
  543. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  544. } else {
  545. cl->state = MEI_FILE_INITIALIZING;
  546. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  547. }
  548. mutex_unlock(&dev->device_lock);
  549. wait_event_timeout(cl->wait,
  550. (cl->state == MEI_FILE_CONNECTED ||
  551. cl->state == MEI_FILE_DISCONNECTED),
  552. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  553. mutex_lock(&dev->device_lock);
  554. if (cl->state != MEI_FILE_CONNECTED) {
  555. cl->state = MEI_FILE_DISCONNECTED;
  556. /* something went really wrong */
  557. if (!cl->status)
  558. cl->status = -EFAULT;
  559. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  560. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  561. }
  562. rets = cl->status;
  563. out:
  564. cl_dbg(dev, cl, "rpm: autosuspend\n");
  565. pm_runtime_mark_last_busy(dev->dev);
  566. pm_runtime_put_autosuspend(dev->dev);
  567. mei_io_cb_free(cb);
  568. return rets;
  569. }
  570. /**
  571. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  572. *
  573. * @cl: private data of the file object
  574. *
  575. * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  576. * -ENOENT if mei_cl is not present
  577. * -EINVAL if single_recv_buf == 0
  578. */
  579. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  580. {
  581. struct mei_device *dev;
  582. struct mei_me_client *me_cl;
  583. if (WARN_ON(!cl || !cl->dev))
  584. return -EINVAL;
  585. dev = cl->dev;
  586. if (cl->mei_flow_ctrl_creds > 0)
  587. return 1;
  588. me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
  589. if (!me_cl) {
  590. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  591. return -ENOENT;
  592. }
  593. if (me_cl->mei_flow_ctrl_creds) {
  594. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  595. return -EINVAL;
  596. return 1;
  597. }
  598. return 0;
  599. }
  600. /**
  601. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  602. *
  603. * @cl: private data of the file object
  604. *
  605. * Return:
  606. * 0 on success
  607. * -ENOENT when me client is not found
  608. * -EINVAL when ctrl credits are <= 0
  609. */
  610. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  611. {
  612. struct mei_device *dev;
  613. struct mei_me_client *me_cl;
  614. if (WARN_ON(!cl || !cl->dev))
  615. return -EINVAL;
  616. dev = cl->dev;
  617. me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
  618. if (!me_cl) {
  619. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  620. return -ENOENT;
  621. }
  622. if (me_cl->props.single_recv_buf) {
  623. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  624. return -EINVAL;
  625. me_cl->mei_flow_ctrl_creds--;
  626. } else {
  627. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  628. return -EINVAL;
  629. cl->mei_flow_ctrl_creds--;
  630. }
  631. return 0;
  632. }
  633. /**
  634. * mei_cl_read_start - the start read client message function.
  635. *
  636. * @cl: host client
  637. * @length: number of bytes to read
  638. *
  639. * Return: 0 on success, <0 on failure.
  640. */
  641. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  642. {
  643. struct mei_device *dev;
  644. struct mei_cl_cb *cb;
  645. struct mei_me_client *me_cl;
  646. int rets;
  647. if (WARN_ON(!cl || !cl->dev))
  648. return -ENODEV;
  649. dev = cl->dev;
  650. if (!mei_cl_is_connected(cl))
  651. return -ENODEV;
  652. if (cl->read_cb) {
  653. cl_dbg(dev, cl, "read is pending.\n");
  654. return -EBUSY;
  655. }
  656. me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
  657. if (!me_cl) {
  658. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  659. return -ENOTTY;
  660. }
  661. rets = pm_runtime_get(dev->dev);
  662. if (rets < 0 && rets != -EINPROGRESS) {
  663. pm_runtime_put_noidle(dev->dev);
  664. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  665. return rets;
  666. }
  667. cb = mei_io_cb_init(cl, NULL);
  668. if (!cb) {
  669. rets = -ENOMEM;
  670. goto out;
  671. }
  672. /* always allocate at least client max message */
  673. length = max_t(size_t, length, me_cl->props.max_msg_length);
  674. rets = mei_io_cb_alloc_resp_buf(cb, length);
  675. if (rets)
  676. goto out;
  677. cb->fop_type = MEI_FOP_READ;
  678. if (mei_hbuf_acquire(dev)) {
  679. rets = mei_hbm_cl_flow_control_req(dev, cl);
  680. if (rets < 0)
  681. goto out;
  682. list_add_tail(&cb->list, &dev->read_list.list);
  683. } else {
  684. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  685. }
  686. cl->read_cb = cb;
  687. out:
  688. cl_dbg(dev, cl, "rpm: autosuspend\n");
  689. pm_runtime_mark_last_busy(dev->dev);
  690. pm_runtime_put_autosuspend(dev->dev);
  691. if (rets)
  692. mei_io_cb_free(cb);
  693. return rets;
  694. }
  695. /**
  696. * mei_cl_irq_write - write a message to device
  697. * from the interrupt thread context
  698. *
  699. * @cl: client
  700. * @cb: callback block.
  701. * @cmpl_list: complete list.
  702. *
  703. * Return: 0, OK; otherwise error.
  704. */
  705. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  706. struct mei_cl_cb *cmpl_list)
  707. {
  708. struct mei_device *dev;
  709. struct mei_msg_data *buf;
  710. struct mei_msg_hdr mei_hdr;
  711. size_t len;
  712. u32 msg_slots;
  713. int slots;
  714. int rets;
  715. if (WARN_ON(!cl || !cl->dev))
  716. return -ENODEV;
  717. dev = cl->dev;
  718. buf = &cb->request_buffer;
  719. rets = mei_cl_flow_ctrl_creds(cl);
  720. if (rets < 0)
  721. return rets;
  722. if (rets == 0) {
  723. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  724. return 0;
  725. }
  726. slots = mei_hbuf_empty_slots(dev);
  727. len = buf->size - cb->buf_idx;
  728. msg_slots = mei_data2slots(len);
  729. mei_hdr.host_addr = cl->host_client_id;
  730. mei_hdr.me_addr = cl->me_client_id;
  731. mei_hdr.reserved = 0;
  732. mei_hdr.internal = cb->internal;
  733. if (slots >= msg_slots) {
  734. mei_hdr.length = len;
  735. mei_hdr.msg_complete = 1;
  736. /* Split the message only if we can write the whole host buffer */
  737. } else if (slots == dev->hbuf_depth) {
  738. msg_slots = slots;
  739. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  740. mei_hdr.length = len;
  741. mei_hdr.msg_complete = 0;
  742. } else {
  743. /* wait for next time the host buffer is empty */
  744. return 0;
  745. }
  746. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  747. cb->request_buffer.size, cb->buf_idx);
  748. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  749. if (rets) {
  750. cl->status = rets;
  751. list_move_tail(&cb->list, &cmpl_list->list);
  752. return rets;
  753. }
  754. cl->status = 0;
  755. cl->writing_state = MEI_WRITING;
  756. cb->buf_idx += mei_hdr.length;
  757. if (mei_hdr.msg_complete) {
  758. if (mei_cl_flow_ctrl_reduce(cl))
  759. return -EIO;
  760. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  761. }
  762. return 0;
  763. }
  764. /**
  765. * mei_cl_write - submit a write cb to mei device
  766. * assumes device_lock is locked
  767. *
  768. * @cl: host client
  769. * @cb: write callback with filled data
  770. * @blocking: block until completed
  771. *
  772. * Return: number of bytes sent on success, <0 on failure.
  773. */
  774. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  775. {
  776. struct mei_device *dev;
  777. struct mei_msg_data *buf;
  778. struct mei_msg_hdr mei_hdr;
  779. int rets;
  780. if (WARN_ON(!cl || !cl->dev))
  781. return -ENODEV;
  782. if (WARN_ON(!cb))
  783. return -EINVAL;
  784. dev = cl->dev;
  785. buf = &cb->request_buffer;
  786. cl_dbg(dev, cl, "size=%d\n", buf->size);
  787. rets = pm_runtime_get(dev->dev);
  788. if (rets < 0 && rets != -EINPROGRESS) {
  789. pm_runtime_put_noidle(dev->dev);
  790. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  791. return rets;
  792. }
  793. cb->fop_type = MEI_FOP_WRITE;
  794. cb->buf_idx = 0;
  795. cl->writing_state = MEI_IDLE;
  796. mei_hdr.host_addr = cl->host_client_id;
  797. mei_hdr.me_addr = cl->me_client_id;
  798. mei_hdr.reserved = 0;
  799. mei_hdr.msg_complete = 0;
  800. mei_hdr.internal = cb->internal;
  801. rets = mei_cl_flow_ctrl_creds(cl);
  802. if (rets < 0)
  803. goto err;
  804. if (rets == 0) {
  805. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  806. rets = buf->size;
  807. goto out;
  808. }
  809. if (!mei_hbuf_acquire(dev)) {
  810. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  811. rets = buf->size;
  812. goto out;
  813. }
  814. /* Check for a maximum length */
  815. if (buf->size > mei_hbuf_max_len(dev)) {
  816. mei_hdr.length = mei_hbuf_max_len(dev);
  817. mei_hdr.msg_complete = 0;
  818. } else {
  819. mei_hdr.length = buf->size;
  820. mei_hdr.msg_complete = 1;
  821. }
  822. rets = mei_write_message(dev, &mei_hdr, buf->data);
  823. if (rets)
  824. goto err;
  825. cl->writing_state = MEI_WRITING;
  826. cb->buf_idx = mei_hdr.length;
  827. out:
  828. if (mei_hdr.msg_complete) {
  829. rets = mei_cl_flow_ctrl_reduce(cl);
  830. if (rets < 0)
  831. goto err;
  832. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  833. } else {
  834. list_add_tail(&cb->list, &dev->write_list.list);
  835. }
  836. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  837. mutex_unlock(&dev->device_lock);
  838. rets = wait_event_interruptible(cl->tx_wait,
  839. cl->writing_state == MEI_WRITE_COMPLETE);
  840. mutex_lock(&dev->device_lock);
  841. /* wait_event_interruptible returns -ERESTARTSYS */
  842. if (rets) {
  843. if (signal_pending(current))
  844. rets = -EINTR;
  845. goto err;
  846. }
  847. }
  848. rets = buf->size;
  849. err:
  850. cl_dbg(dev, cl, "rpm: autosuspend\n");
  851. pm_runtime_mark_last_busy(dev->dev);
  852. pm_runtime_put_autosuspend(dev->dev);
  853. return rets;
  854. }
  855. /**
  856. * mei_cl_complete - processes completed operation for a client
  857. *
  858. * @cl: private data of the file object.
  859. * @cb: callback block.
  860. */
  861. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  862. {
  863. if (cb->fop_type == MEI_FOP_WRITE) {
  864. mei_io_cb_free(cb);
  865. cb = NULL;
  866. cl->writing_state = MEI_WRITE_COMPLETE;
  867. if (waitqueue_active(&cl->tx_wait))
  868. wake_up_interruptible(&cl->tx_wait);
  869. } else if (cb->fop_type == MEI_FOP_READ &&
  870. MEI_READING == cl->reading_state) {
  871. cl->reading_state = MEI_READ_COMPLETE;
  872. if (waitqueue_active(&cl->rx_wait))
  873. wake_up_interruptible(&cl->rx_wait);
  874. else
  875. mei_cl_bus_rx_event(cl);
  876. }
  877. }
  878. /**
  879. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  880. *
  881. * @dev: mei device
  882. */
  883. void mei_cl_all_disconnect(struct mei_device *dev)
  884. {
  885. struct mei_cl *cl;
  886. list_for_each_entry(cl, &dev->file_list, link) {
  887. cl->state = MEI_FILE_DISCONNECTED;
  888. cl->mei_flow_ctrl_creds = 0;
  889. cl->timer_count = 0;
  890. }
  891. }
  892. /**
  893. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  894. *
  895. * @dev: mei device
  896. */
  897. void mei_cl_all_wakeup(struct mei_device *dev)
  898. {
  899. struct mei_cl *cl;
  900. list_for_each_entry(cl, &dev->file_list, link) {
  901. if (waitqueue_active(&cl->rx_wait)) {
  902. cl_dbg(dev, cl, "Waking up reading client!\n");
  903. wake_up_interruptible(&cl->rx_wait);
  904. }
  905. if (waitqueue_active(&cl->tx_wait)) {
  906. cl_dbg(dev, cl, "Waking up writing client!\n");
  907. wake_up_interruptible(&cl->tx_wait);
  908. }
  909. }
  910. }
  911. /**
  912. * mei_cl_all_write_clear - clear all pending writes
  913. *
  914. * @dev: mei device
  915. */
  916. void mei_cl_all_write_clear(struct mei_device *dev)
  917. {
  918. mei_io_list_free(&dev->write_list, NULL);
  919. mei_io_list_free(&dev->write_waiting_list, NULL);
  920. }