client.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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_init - initialize me client
  27. *
  28. * @me_cl: me client
  29. */
  30. void mei_me_cl_init(struct mei_me_client *me_cl)
  31. {
  32. INIT_LIST_HEAD(&me_cl->list);
  33. kref_init(&me_cl->refcnt);
  34. }
  35. /**
  36. * mei_me_cl_get - increases me client refcount
  37. *
  38. * @me_cl: me client
  39. *
  40. * Locking: called under "dev->device_lock" lock
  41. *
  42. * Return: me client or NULL
  43. */
  44. struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
  45. {
  46. if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
  47. return me_cl;
  48. return NULL;
  49. }
  50. /**
  51. * mei_me_cl_release - free me client
  52. *
  53. * Locking: called under "dev->device_lock" lock
  54. *
  55. * @ref: me_client refcount
  56. */
  57. static void mei_me_cl_release(struct kref *ref)
  58. {
  59. struct mei_me_client *me_cl =
  60. container_of(ref, struct mei_me_client, refcnt);
  61. kfree(me_cl);
  62. }
  63. /**
  64. * mei_me_cl_put - decrease me client refcount and free client if necessary
  65. *
  66. * Locking: called under "dev->device_lock" lock
  67. *
  68. * @me_cl: me client
  69. */
  70. void mei_me_cl_put(struct mei_me_client *me_cl)
  71. {
  72. if (me_cl)
  73. kref_put(&me_cl->refcnt, mei_me_cl_release);
  74. }
  75. /**
  76. * __mei_me_cl_del - delete me client from the list and decrease
  77. * reference counter
  78. *
  79. * @dev: mei device
  80. * @me_cl: me client
  81. *
  82. * Locking: dev->me_clients_rwsem
  83. */
  84. static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  85. {
  86. if (!me_cl)
  87. return;
  88. list_del_init(&me_cl->list);
  89. mei_me_cl_put(me_cl);
  90. }
  91. /**
  92. * mei_me_cl_del - delete me client from the list and decrease
  93. * reference counter
  94. *
  95. * @dev: mei device
  96. * @me_cl: me client
  97. */
  98. void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  99. {
  100. down_write(&dev->me_clients_rwsem);
  101. __mei_me_cl_del(dev, me_cl);
  102. up_write(&dev->me_clients_rwsem);
  103. }
  104. /**
  105. * mei_me_cl_add - add me client to the list
  106. *
  107. * @dev: mei device
  108. * @me_cl: me client
  109. */
  110. void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
  111. {
  112. down_write(&dev->me_clients_rwsem);
  113. list_add(&me_cl->list, &dev->me_clients);
  114. up_write(&dev->me_clients_rwsem);
  115. }
  116. /**
  117. * __mei_me_cl_by_uuid - locate me client by uuid
  118. * increases ref count
  119. *
  120. * @dev: mei device
  121. * @uuid: me client uuid
  122. *
  123. * Return: me client or NULL if not found
  124. *
  125. * Locking: dev->me_clients_rwsem
  126. */
  127. static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
  128. const uuid_le *uuid)
  129. {
  130. struct mei_me_client *me_cl;
  131. const uuid_le *pn;
  132. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  133. list_for_each_entry(me_cl, &dev->me_clients, list) {
  134. pn = &me_cl->props.protocol_name;
  135. if (uuid_le_cmp(*uuid, *pn) == 0)
  136. return mei_me_cl_get(me_cl);
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * mei_me_cl_by_uuid - locate me client by uuid
  142. * increases ref count
  143. *
  144. * @dev: mei device
  145. * @uuid: me client uuid
  146. *
  147. * Return: me client or NULL if not found
  148. *
  149. * Locking: dev->me_clients_rwsem
  150. */
  151. struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
  152. const uuid_le *uuid)
  153. {
  154. struct mei_me_client *me_cl;
  155. down_read(&dev->me_clients_rwsem);
  156. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  157. up_read(&dev->me_clients_rwsem);
  158. return me_cl;
  159. }
  160. /**
  161. * mei_me_cl_by_id - locate me client by client id
  162. * increases ref count
  163. *
  164. * @dev: the device structure
  165. * @client_id: me client id
  166. *
  167. * Return: me client or NULL if not found
  168. *
  169. * Locking: dev->me_clients_rwsem
  170. */
  171. struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  172. {
  173. struct mei_me_client *__me_cl, *me_cl = NULL;
  174. down_read(&dev->me_clients_rwsem);
  175. list_for_each_entry(__me_cl, &dev->me_clients, list) {
  176. if (__me_cl->client_id == client_id) {
  177. me_cl = mei_me_cl_get(__me_cl);
  178. break;
  179. }
  180. }
  181. up_read(&dev->me_clients_rwsem);
  182. return me_cl;
  183. }
  184. /**
  185. * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
  186. * increases ref count
  187. *
  188. * @dev: the device structure
  189. * @uuid: me client uuid
  190. * @client_id: me client id
  191. *
  192. * Return: me client or null if not found
  193. *
  194. * Locking: dev->me_clients_rwsem
  195. */
  196. static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
  197. const uuid_le *uuid, u8 client_id)
  198. {
  199. struct mei_me_client *me_cl;
  200. const uuid_le *pn;
  201. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  202. list_for_each_entry(me_cl, &dev->me_clients, list) {
  203. pn = &me_cl->props.protocol_name;
  204. if (uuid_le_cmp(*uuid, *pn) == 0 &&
  205. me_cl->client_id == client_id)
  206. return mei_me_cl_get(me_cl);
  207. }
  208. return NULL;
  209. }
  210. /**
  211. * mei_me_cl_by_uuid_id - locate me client by client id and uuid
  212. * increases ref count
  213. *
  214. * @dev: the device structure
  215. * @uuid: me client uuid
  216. * @client_id: me client id
  217. *
  218. * Return: me client or null if not found
  219. */
  220. struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
  221. const uuid_le *uuid, u8 client_id)
  222. {
  223. struct mei_me_client *me_cl;
  224. down_read(&dev->me_clients_rwsem);
  225. me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
  226. up_read(&dev->me_clients_rwsem);
  227. return me_cl;
  228. }
  229. /**
  230. * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
  231. *
  232. * @dev: the device structure
  233. * @uuid: me client uuid
  234. *
  235. * Locking: called under "dev->device_lock" lock
  236. */
  237. void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
  238. {
  239. struct mei_me_client *me_cl;
  240. dev_dbg(dev->dev, "remove %pUl\n", uuid);
  241. down_write(&dev->me_clients_rwsem);
  242. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  243. __mei_me_cl_del(dev, me_cl);
  244. up_write(&dev->me_clients_rwsem);
  245. }
  246. /**
  247. * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
  248. *
  249. * @dev: the device structure
  250. * @uuid: me client uuid
  251. * @id: me client id
  252. *
  253. * Locking: called under "dev->device_lock" lock
  254. */
  255. void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
  256. {
  257. struct mei_me_client *me_cl;
  258. dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
  259. down_write(&dev->me_clients_rwsem);
  260. me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
  261. __mei_me_cl_del(dev, me_cl);
  262. up_write(&dev->me_clients_rwsem);
  263. }
  264. /**
  265. * mei_me_cl_rm_all - remove all me clients
  266. *
  267. * @dev: the device structure
  268. *
  269. * Locking: called under "dev->device_lock" lock
  270. */
  271. void mei_me_cl_rm_all(struct mei_device *dev)
  272. {
  273. struct mei_me_client *me_cl, *next;
  274. down_write(&dev->me_clients_rwsem);
  275. list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
  276. __mei_me_cl_del(dev, me_cl);
  277. up_write(&dev->me_clients_rwsem);
  278. }
  279. /**
  280. * mei_cl_cmp_id - tells if the clients are the same
  281. *
  282. * @cl1: host client 1
  283. * @cl2: host client 2
  284. *
  285. * Return: true - if the clients has same host and me ids
  286. * false - otherwise
  287. */
  288. static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
  289. const struct mei_cl *cl2)
  290. {
  291. return cl1 && cl2 &&
  292. (cl1->host_client_id == cl2->host_client_id) &&
  293. (mei_cl_me_id(cl1) == mei_cl_me_id(cl2));
  294. }
  295. /**
  296. * mei_io_cb_free - free mei_cb_private related memory
  297. *
  298. * @cb: mei callback struct
  299. */
  300. void mei_io_cb_free(struct mei_cl_cb *cb)
  301. {
  302. if (cb == NULL)
  303. return;
  304. list_del(&cb->list);
  305. kfree(cb->buf.data);
  306. kfree(cb);
  307. }
  308. /**
  309. * mei_io_cb_init - allocate and initialize io callback
  310. *
  311. * @cl: mei client
  312. * @type: operation type
  313. * @fp: pointer to file structure
  314. *
  315. * Return: mei_cl_cb pointer or NULL;
  316. */
  317. struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
  318. struct file *fp)
  319. {
  320. struct mei_cl_cb *cb;
  321. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  322. if (!cb)
  323. return NULL;
  324. INIT_LIST_HEAD(&cb->list);
  325. cb->file_object = fp;
  326. cb->cl = cl;
  327. cb->buf_idx = 0;
  328. cb->fop_type = type;
  329. return cb;
  330. }
  331. /**
  332. * __mei_io_list_flush - removes and frees cbs belonging to cl.
  333. *
  334. * @list: an instance of our list structure
  335. * @cl: host client, can be NULL for flushing the whole list
  336. * @free: whether to free the cbs
  337. */
  338. static void __mei_io_list_flush(struct mei_cl_cb *list,
  339. struct mei_cl *cl, bool free)
  340. {
  341. struct mei_cl_cb *cb, *next;
  342. /* enable removing everything if no cl is specified */
  343. list_for_each_entry_safe(cb, next, &list->list, list) {
  344. if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
  345. list_del_init(&cb->list);
  346. if (free)
  347. mei_io_cb_free(cb);
  348. }
  349. }
  350. }
  351. /**
  352. * mei_io_list_flush - removes list entry belonging to cl.
  353. *
  354. * @list: An instance of our list structure
  355. * @cl: host client
  356. */
  357. void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  358. {
  359. __mei_io_list_flush(list, cl, false);
  360. }
  361. /**
  362. * mei_io_list_free - removes cb belonging to cl and free them
  363. *
  364. * @list: An instance of our list structure
  365. * @cl: host client
  366. */
  367. static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
  368. {
  369. __mei_io_list_flush(list, cl, true);
  370. }
  371. /**
  372. * mei_io_cb_alloc_buf - allocate callback buffer
  373. *
  374. * @cb: io callback structure
  375. * @length: size of the buffer
  376. *
  377. * Return: 0 on success
  378. * -EINVAL if cb is NULL
  379. * -ENOMEM if allocation failed
  380. */
  381. int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
  382. {
  383. if (!cb)
  384. return -EINVAL;
  385. if (length == 0)
  386. return 0;
  387. cb->buf.data = kmalloc(length, GFP_KERNEL);
  388. if (!cb->buf.data)
  389. return -ENOMEM;
  390. cb->buf.size = length;
  391. return 0;
  392. }
  393. /**
  394. * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
  395. *
  396. * @cl: host client
  397. * @length: size of the buffer
  398. * @type: operation type
  399. * @fp: associated file pointer (might be NULL)
  400. *
  401. * Return: cb on success and NULL on failure
  402. */
  403. struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
  404. enum mei_cb_file_ops type, struct file *fp)
  405. {
  406. struct mei_cl_cb *cb;
  407. cb = mei_io_cb_init(cl, type, fp);
  408. if (!cb)
  409. return NULL;
  410. if (mei_io_cb_alloc_buf(cb, length)) {
  411. mei_io_cb_free(cb);
  412. return NULL;
  413. }
  414. return cb;
  415. }
  416. /**
  417. * mei_cl_read_cb - find this cl's callback in the read list
  418. * for a specific file
  419. *
  420. * @cl: host client
  421. * @fp: file pointer (matching cb file object), may be NULL
  422. *
  423. * Return: cb on success, NULL if cb is not found
  424. */
  425. struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
  426. {
  427. struct mei_cl_cb *cb;
  428. list_for_each_entry(cb, &cl->rd_completed, list)
  429. if (!fp || fp == cb->file_object)
  430. return cb;
  431. return NULL;
  432. }
  433. /**
  434. * mei_cl_read_cb_flush - free client's read pending and completed cbs
  435. * for a specific file
  436. *
  437. * @cl: host client
  438. * @fp: file pointer (matching cb file object), may be NULL
  439. */
  440. void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
  441. {
  442. struct mei_cl_cb *cb, *next;
  443. list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
  444. if (!fp || fp == cb->file_object)
  445. mei_io_cb_free(cb);
  446. list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
  447. if (!fp || fp == cb->file_object)
  448. mei_io_cb_free(cb);
  449. }
  450. /**
  451. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  452. *
  453. * @cl: host client
  454. * @fp: file pointer (matching cb file object), may be NULL
  455. *
  456. * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
  457. */
  458. int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
  459. {
  460. struct mei_device *dev;
  461. if (WARN_ON(!cl || !cl->dev))
  462. return -EINVAL;
  463. dev = cl->dev;
  464. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  465. mei_io_list_free(&cl->dev->write_list, cl);
  466. mei_io_list_free(&cl->dev->write_waiting_list, cl);
  467. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  468. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  469. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  470. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  471. mei_cl_read_cb_flush(cl, fp);
  472. return 0;
  473. }
  474. /**
  475. * mei_cl_init - initializes cl.
  476. *
  477. * @cl: host client to be initialized
  478. * @dev: mei device
  479. */
  480. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  481. {
  482. memset(cl, 0, sizeof(struct mei_cl));
  483. init_waitqueue_head(&cl->wait);
  484. init_waitqueue_head(&cl->rx_wait);
  485. init_waitqueue_head(&cl->tx_wait);
  486. INIT_LIST_HEAD(&cl->rd_completed);
  487. INIT_LIST_HEAD(&cl->rd_pending);
  488. INIT_LIST_HEAD(&cl->link);
  489. INIT_LIST_HEAD(&cl->device_link);
  490. cl->writing_state = MEI_IDLE;
  491. cl->state = MEI_FILE_INITIALIZING;
  492. cl->dev = dev;
  493. }
  494. /**
  495. * mei_cl_allocate - allocates cl structure and sets it up.
  496. *
  497. * @dev: mei device
  498. * Return: The allocated file or NULL on failure
  499. */
  500. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  501. {
  502. struct mei_cl *cl;
  503. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  504. if (!cl)
  505. return NULL;
  506. mei_cl_init(cl, dev);
  507. return cl;
  508. }
  509. /**
  510. * mei_cl_link - allocate host id in the host map
  511. *
  512. * @cl: host client
  513. * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
  514. *
  515. * Return: 0 on success
  516. * -EINVAL on incorrect values
  517. * -EMFILE if open count exceeded.
  518. */
  519. int mei_cl_link(struct mei_cl *cl, int id)
  520. {
  521. struct mei_device *dev;
  522. long open_handle_count;
  523. if (WARN_ON(!cl || !cl->dev))
  524. return -EINVAL;
  525. dev = cl->dev;
  526. /* If Id is not assigned get one*/
  527. if (id == MEI_HOST_CLIENT_ID_ANY)
  528. id = find_first_zero_bit(dev->host_clients_map,
  529. MEI_CLIENTS_MAX);
  530. if (id >= MEI_CLIENTS_MAX) {
  531. dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  532. return -EMFILE;
  533. }
  534. open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
  535. if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  536. dev_err(dev->dev, "open_handle_count exceeded %d",
  537. MEI_MAX_OPEN_HANDLE_COUNT);
  538. return -EMFILE;
  539. }
  540. dev->open_handle_count++;
  541. cl->host_client_id = id;
  542. list_add_tail(&cl->link, &dev->file_list);
  543. set_bit(id, dev->host_clients_map);
  544. cl->state = MEI_FILE_INITIALIZING;
  545. cl_dbg(dev, cl, "link cl\n");
  546. return 0;
  547. }
  548. /**
  549. * mei_cl_unlink - remove host client from the list
  550. *
  551. * @cl: host client
  552. *
  553. * Return: always 0
  554. */
  555. int mei_cl_unlink(struct mei_cl *cl)
  556. {
  557. struct mei_device *dev;
  558. /* don't shout on error exit path */
  559. if (!cl)
  560. return 0;
  561. /* wd and amthif might not be initialized */
  562. if (!cl->dev)
  563. return 0;
  564. dev = cl->dev;
  565. cl_dbg(dev, cl, "unlink client");
  566. if (dev->open_handle_count > 0)
  567. dev->open_handle_count--;
  568. /* never clear the 0 bit */
  569. if (cl->host_client_id)
  570. clear_bit(cl->host_client_id, dev->host_clients_map);
  571. list_del_init(&cl->link);
  572. cl->state = MEI_FILE_INITIALIZING;
  573. return 0;
  574. }
  575. void mei_host_client_init(struct work_struct *work)
  576. {
  577. struct mei_device *dev =
  578. container_of(work, struct mei_device, init_work);
  579. struct mei_me_client *me_cl;
  580. mutex_lock(&dev->device_lock);
  581. me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
  582. if (me_cl)
  583. mei_amthif_host_init(dev, me_cl);
  584. mei_me_cl_put(me_cl);
  585. me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
  586. if (me_cl)
  587. mei_wd_host_init(dev, me_cl);
  588. mei_me_cl_put(me_cl);
  589. me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
  590. if (me_cl)
  591. mei_nfc_host_init(dev, me_cl);
  592. mei_me_cl_put(me_cl);
  593. dev->dev_state = MEI_DEV_ENABLED;
  594. dev->reset_count = 0;
  595. mutex_unlock(&dev->device_lock);
  596. pm_runtime_mark_last_busy(dev->dev);
  597. dev_dbg(dev->dev, "rpm: autosuspend\n");
  598. pm_runtime_autosuspend(dev->dev);
  599. }
  600. /**
  601. * mei_hbuf_acquire - try to acquire host buffer
  602. *
  603. * @dev: the device structure
  604. * Return: true if host buffer was acquired
  605. */
  606. bool mei_hbuf_acquire(struct mei_device *dev)
  607. {
  608. if (mei_pg_state(dev) == MEI_PG_ON ||
  609. mei_pg_in_transition(dev)) {
  610. dev_dbg(dev->dev, "device is in pg\n");
  611. return false;
  612. }
  613. if (!dev->hbuf_is_ready) {
  614. dev_dbg(dev->dev, "hbuf is not ready\n");
  615. return false;
  616. }
  617. dev->hbuf_is_ready = false;
  618. return true;
  619. }
  620. /**
  621. * mei_cl_set_disconnected - set disconnected state and clear
  622. * associated states and resources
  623. *
  624. * @cl: host client
  625. */
  626. void mei_cl_set_disconnected(struct mei_cl *cl)
  627. {
  628. struct mei_device *dev = cl->dev;
  629. if (cl->state == MEI_FILE_DISCONNECTED ||
  630. cl->state == MEI_FILE_INITIALIZING)
  631. return;
  632. cl->state = MEI_FILE_DISCONNECTED;
  633. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  634. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  635. cl->mei_flow_ctrl_creds = 0;
  636. cl->timer_count = 0;
  637. if (!cl->me_cl)
  638. return;
  639. if (!WARN_ON(cl->me_cl->connect_count == 0))
  640. cl->me_cl->connect_count--;
  641. if (cl->me_cl->connect_count == 0)
  642. cl->me_cl->mei_flow_ctrl_creds = 0;
  643. mei_me_cl_put(cl->me_cl);
  644. cl->me_cl = NULL;
  645. }
  646. static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
  647. {
  648. if (!mei_me_cl_get(me_cl))
  649. return -ENOENT;
  650. /* only one connection is allowed for fixed address clients */
  651. if (me_cl->props.fixed_address) {
  652. if (me_cl->connect_count) {
  653. mei_me_cl_put(me_cl);
  654. return -EBUSY;
  655. }
  656. }
  657. cl->me_cl = me_cl;
  658. cl->state = MEI_FILE_CONNECTING;
  659. cl->me_cl->connect_count++;
  660. return 0;
  661. }
  662. /*
  663. * mei_cl_send_disconnect - send disconnect request
  664. *
  665. * @cl: host client
  666. * @cb: callback block
  667. *
  668. * Return: 0, OK; otherwise, error.
  669. */
  670. static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
  671. {
  672. struct mei_device *dev;
  673. int ret;
  674. dev = cl->dev;
  675. ret = mei_hbm_cl_disconnect_req(dev, cl);
  676. cl->status = ret;
  677. if (ret) {
  678. cl->state = MEI_FILE_DISCONNECT_REPLY;
  679. return ret;
  680. }
  681. list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
  682. cl->timer_count = MEI_CONNECT_TIMEOUT;
  683. return 0;
  684. }
  685. /**
  686. * mei_cl_irq_disconnect - processes close related operation from
  687. * interrupt thread context - send disconnect request
  688. *
  689. * @cl: client
  690. * @cb: callback block.
  691. * @cmpl_list: complete list.
  692. *
  693. * Return: 0, OK; otherwise, error.
  694. */
  695. int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
  696. struct mei_cl_cb *cmpl_list)
  697. {
  698. struct mei_device *dev = cl->dev;
  699. u32 msg_slots;
  700. int slots;
  701. int ret;
  702. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
  703. slots = mei_hbuf_empty_slots(dev);
  704. if (slots < msg_slots)
  705. return -EMSGSIZE;
  706. ret = mei_cl_send_disconnect(cl, cb);
  707. if (ret)
  708. list_move_tail(&cb->list, &cmpl_list->list);
  709. return ret;
  710. }
  711. /**
  712. * mei_cl_disconnect - disconnect host client from the me one
  713. *
  714. * @cl: host client
  715. *
  716. * Locking: called under "dev->device_lock" lock
  717. *
  718. * Return: 0 on success, <0 on failure.
  719. */
  720. int mei_cl_disconnect(struct mei_cl *cl)
  721. {
  722. struct mei_device *dev;
  723. struct mei_cl_cb *cb;
  724. int rets;
  725. if (WARN_ON(!cl || !cl->dev))
  726. return -ENODEV;
  727. dev = cl->dev;
  728. cl_dbg(dev, cl, "disconnecting");
  729. if (!mei_cl_is_connected(cl))
  730. return 0;
  731. if (mei_cl_is_fixed_address(cl)) {
  732. mei_cl_set_disconnected(cl);
  733. return 0;
  734. }
  735. rets = pm_runtime_get(dev->dev);
  736. if (rets < 0 && rets != -EINPROGRESS) {
  737. pm_runtime_put_noidle(dev->dev);
  738. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  739. return rets;
  740. }
  741. cl->state = MEI_FILE_DISCONNECTING;
  742. cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
  743. rets = cb ? 0 : -ENOMEM;
  744. if (rets)
  745. goto out;
  746. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  747. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  748. if (mei_hbuf_acquire(dev)) {
  749. rets = mei_cl_send_disconnect(cl, cb);
  750. if (rets) {
  751. cl_err(dev, cl, "failed to disconnect.\n");
  752. goto out;
  753. }
  754. }
  755. mutex_unlock(&dev->device_lock);
  756. wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
  757. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  758. mutex_lock(&dev->device_lock);
  759. rets = cl->status;
  760. if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
  761. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  762. rets = -ETIME;
  763. }
  764. out:
  765. /* we disconnect also on error */
  766. mei_cl_set_disconnected(cl);
  767. if (!rets)
  768. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  769. cl_dbg(dev, cl, "rpm: autosuspend\n");
  770. pm_runtime_mark_last_busy(dev->dev);
  771. pm_runtime_put_autosuspend(dev->dev);
  772. mei_io_cb_free(cb);
  773. return rets;
  774. }
  775. /**
  776. * mei_cl_is_other_connecting - checks if other
  777. * client with the same me client id is connecting
  778. *
  779. * @cl: private data of the file object
  780. *
  781. * Return: true if other client is connected, false - otherwise.
  782. */
  783. static bool mei_cl_is_other_connecting(struct mei_cl *cl)
  784. {
  785. struct mei_device *dev;
  786. struct mei_cl_cb *cb;
  787. dev = cl->dev;
  788. list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
  789. if (cb->fop_type == MEI_FOP_CONNECT &&
  790. mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
  791. return true;
  792. }
  793. return false;
  794. }
  795. /**
  796. * mei_cl_send_connect - send connect request
  797. *
  798. * @cl: host client
  799. * @cb: callback block
  800. *
  801. * Return: 0, OK; otherwise, error.
  802. */
  803. static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
  804. {
  805. struct mei_device *dev;
  806. int ret;
  807. dev = cl->dev;
  808. ret = mei_hbm_cl_connect_req(dev, cl);
  809. cl->status = ret;
  810. if (ret) {
  811. cl->state = MEI_FILE_DISCONNECT_REPLY;
  812. return ret;
  813. }
  814. list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
  815. cl->timer_count = MEI_CONNECT_TIMEOUT;
  816. return 0;
  817. }
  818. /**
  819. * mei_cl_irq_connect - send connect request in irq_thread context
  820. *
  821. * @cl: host client
  822. * @cb: callback block
  823. * @cmpl_list: complete list
  824. *
  825. * Return: 0, OK; otherwise, error.
  826. */
  827. int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
  828. struct mei_cl_cb *cmpl_list)
  829. {
  830. struct mei_device *dev = cl->dev;
  831. u32 msg_slots;
  832. int slots;
  833. int rets;
  834. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
  835. slots = mei_hbuf_empty_slots(dev);
  836. if (mei_cl_is_other_connecting(cl))
  837. return 0;
  838. if (slots < msg_slots)
  839. return -EMSGSIZE;
  840. rets = mei_cl_send_connect(cl, cb);
  841. if (rets)
  842. list_move_tail(&cb->list, &cmpl_list->list);
  843. return rets;
  844. }
  845. /**
  846. * mei_cl_connect - connect host client to the me one
  847. *
  848. * @cl: host client
  849. * @me_cl: me client
  850. * @file: pointer to file structure
  851. *
  852. * Locking: called under "dev->device_lock" lock
  853. *
  854. * Return: 0 on success, <0 on failure.
  855. */
  856. int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
  857. struct file *file)
  858. {
  859. struct mei_device *dev;
  860. struct mei_cl_cb *cb;
  861. int rets;
  862. if (WARN_ON(!cl || !cl->dev || !me_cl))
  863. return -ENODEV;
  864. dev = cl->dev;
  865. rets = mei_cl_set_connecting(cl, me_cl);
  866. if (rets)
  867. return rets;
  868. if (mei_cl_is_fixed_address(cl)) {
  869. cl->state = MEI_FILE_CONNECTED;
  870. return 0;
  871. }
  872. rets = pm_runtime_get(dev->dev);
  873. if (rets < 0 && rets != -EINPROGRESS) {
  874. pm_runtime_put_noidle(dev->dev);
  875. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  876. goto nortpm;
  877. }
  878. cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
  879. rets = cb ? 0 : -ENOMEM;
  880. if (rets)
  881. goto out;
  882. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  883. /* run hbuf acquire last so we don't have to undo */
  884. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  885. rets = mei_cl_send_connect(cl, cb);
  886. if (rets)
  887. goto out;
  888. }
  889. mutex_unlock(&dev->device_lock);
  890. wait_event_timeout(cl->wait,
  891. (cl->state == MEI_FILE_CONNECTED ||
  892. cl->state == MEI_FILE_DISCONNECT_REPLY),
  893. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  894. mutex_lock(&dev->device_lock);
  895. if (!mei_cl_is_connected(cl)) {
  896. /* timeout or something went really wrong */
  897. if (!cl->status)
  898. cl->status = -EFAULT;
  899. }
  900. rets = cl->status;
  901. out:
  902. cl_dbg(dev, cl, "rpm: autosuspend\n");
  903. pm_runtime_mark_last_busy(dev->dev);
  904. pm_runtime_put_autosuspend(dev->dev);
  905. mei_io_cb_free(cb);
  906. nortpm:
  907. if (!mei_cl_is_connected(cl))
  908. mei_cl_set_disconnected(cl);
  909. return rets;
  910. }
  911. /**
  912. * mei_cl_alloc_linked - allocate and link host client
  913. *
  914. * @dev: the device structure
  915. * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
  916. *
  917. * Return: cl on success ERR_PTR on failure
  918. */
  919. struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
  920. {
  921. struct mei_cl *cl;
  922. int ret;
  923. cl = mei_cl_allocate(dev);
  924. if (!cl) {
  925. ret = -ENOMEM;
  926. goto err;
  927. }
  928. ret = mei_cl_link(cl, id);
  929. if (ret)
  930. goto err;
  931. return cl;
  932. err:
  933. kfree(cl);
  934. return ERR_PTR(ret);
  935. }
  936. /**
  937. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  938. *
  939. * @cl: private data of the file object
  940. *
  941. * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  942. */
  943. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  944. {
  945. int rets;
  946. if (WARN_ON(!cl || !cl->me_cl))
  947. return -EINVAL;
  948. if (cl->mei_flow_ctrl_creds > 0)
  949. return 1;
  950. if (mei_cl_is_fixed_address(cl)) {
  951. rets = mei_cl_read_start(cl, mei_cl_mtu(cl), NULL);
  952. if (rets && rets != -EBUSY)
  953. return rets;
  954. return 1;
  955. }
  956. if (mei_cl_is_single_recv_buf(cl)) {
  957. if (cl->me_cl->mei_flow_ctrl_creds > 0)
  958. return 1;
  959. }
  960. return 0;
  961. }
  962. /**
  963. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  964. *
  965. * @cl: private data of the file object
  966. *
  967. * Return:
  968. * 0 on success
  969. * -EINVAL when ctrl credits are <= 0
  970. */
  971. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  972. {
  973. if (WARN_ON(!cl || !cl->me_cl))
  974. return -EINVAL;
  975. if (mei_cl_is_fixed_address(cl))
  976. return 0;
  977. if (mei_cl_is_single_recv_buf(cl)) {
  978. if (WARN_ON(cl->me_cl->mei_flow_ctrl_creds <= 0))
  979. return -EINVAL;
  980. cl->me_cl->mei_flow_ctrl_creds--;
  981. } else {
  982. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  983. return -EINVAL;
  984. cl->mei_flow_ctrl_creds--;
  985. }
  986. return 0;
  987. }
  988. /**
  989. * mei_cl_read_start - the start read client message function.
  990. *
  991. * @cl: host client
  992. * @length: number of bytes to read
  993. * @fp: pointer to file structure
  994. *
  995. * Return: 0 on success, <0 on failure.
  996. */
  997. int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
  998. {
  999. struct mei_device *dev;
  1000. struct mei_cl_cb *cb;
  1001. int rets;
  1002. if (WARN_ON(!cl || !cl->dev))
  1003. return -ENODEV;
  1004. dev = cl->dev;
  1005. if (!mei_cl_is_connected(cl))
  1006. return -ENODEV;
  1007. /* HW currently supports only one pending read */
  1008. if (!list_empty(&cl->rd_pending))
  1009. return -EBUSY;
  1010. if (!mei_me_cl_is_active(cl->me_cl)) {
  1011. cl_err(dev, cl, "no such me client\n");
  1012. return -ENOTTY;
  1013. }
  1014. /* always allocate at least client max message */
  1015. length = max_t(size_t, length, mei_cl_mtu(cl));
  1016. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
  1017. if (!cb)
  1018. return -ENOMEM;
  1019. if (mei_cl_is_fixed_address(cl)) {
  1020. list_add_tail(&cb->list, &cl->rd_pending);
  1021. return 0;
  1022. }
  1023. rets = pm_runtime_get(dev->dev);
  1024. if (rets < 0 && rets != -EINPROGRESS) {
  1025. pm_runtime_put_noidle(dev->dev);
  1026. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1027. goto nortpm;
  1028. }
  1029. if (mei_hbuf_acquire(dev)) {
  1030. rets = mei_hbm_cl_flow_control_req(dev, cl);
  1031. if (rets < 0)
  1032. goto out;
  1033. list_add_tail(&cb->list, &cl->rd_pending);
  1034. } else {
  1035. rets = 0;
  1036. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  1037. }
  1038. out:
  1039. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1040. pm_runtime_mark_last_busy(dev->dev);
  1041. pm_runtime_put_autosuspend(dev->dev);
  1042. nortpm:
  1043. if (rets)
  1044. mei_io_cb_free(cb);
  1045. return rets;
  1046. }
  1047. /**
  1048. * mei_cl_irq_write - write a message to device
  1049. * from the interrupt thread context
  1050. *
  1051. * @cl: client
  1052. * @cb: callback block.
  1053. * @cmpl_list: complete list.
  1054. *
  1055. * Return: 0, OK; otherwise error.
  1056. */
  1057. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  1058. struct mei_cl_cb *cmpl_list)
  1059. {
  1060. struct mei_device *dev;
  1061. struct mei_msg_data *buf;
  1062. struct mei_msg_hdr mei_hdr;
  1063. size_t len;
  1064. u32 msg_slots;
  1065. int slots;
  1066. int rets;
  1067. bool first_chunk;
  1068. if (WARN_ON(!cl || !cl->dev))
  1069. return -ENODEV;
  1070. dev = cl->dev;
  1071. buf = &cb->buf;
  1072. first_chunk = cb->buf_idx == 0;
  1073. rets = first_chunk ? mei_cl_flow_ctrl_creds(cl) : 1;
  1074. if (rets < 0)
  1075. return rets;
  1076. if (rets == 0) {
  1077. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1078. return 0;
  1079. }
  1080. slots = mei_hbuf_empty_slots(dev);
  1081. len = buf->size - cb->buf_idx;
  1082. msg_slots = mei_data2slots(len);
  1083. mei_hdr.host_addr = mei_cl_host_addr(cl);
  1084. mei_hdr.me_addr = mei_cl_me_id(cl);
  1085. mei_hdr.reserved = 0;
  1086. mei_hdr.internal = cb->internal;
  1087. if (slots >= msg_slots) {
  1088. mei_hdr.length = len;
  1089. mei_hdr.msg_complete = 1;
  1090. /* Split the message only if we can write the whole host buffer */
  1091. } else if (slots == dev->hbuf_depth) {
  1092. msg_slots = slots;
  1093. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  1094. mei_hdr.length = len;
  1095. mei_hdr.msg_complete = 0;
  1096. } else {
  1097. /* wait for next time the host buffer is empty */
  1098. return 0;
  1099. }
  1100. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  1101. cb->buf.size, cb->buf_idx);
  1102. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  1103. if (rets) {
  1104. cl->status = rets;
  1105. list_move_tail(&cb->list, &cmpl_list->list);
  1106. return rets;
  1107. }
  1108. cl->status = 0;
  1109. cl->writing_state = MEI_WRITING;
  1110. cb->buf_idx += mei_hdr.length;
  1111. cb->completed = mei_hdr.msg_complete == 1;
  1112. if (first_chunk) {
  1113. if (mei_cl_flow_ctrl_reduce(cl))
  1114. return -EIO;
  1115. }
  1116. if (mei_hdr.msg_complete)
  1117. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  1118. return 0;
  1119. }
  1120. /**
  1121. * mei_cl_write - submit a write cb to mei device
  1122. * assumes device_lock is locked
  1123. *
  1124. * @cl: host client
  1125. * @cb: write callback with filled data
  1126. * @blocking: block until completed
  1127. *
  1128. * Return: number of bytes sent on success, <0 on failure.
  1129. */
  1130. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  1131. {
  1132. struct mei_device *dev;
  1133. struct mei_msg_data *buf;
  1134. struct mei_msg_hdr mei_hdr;
  1135. int rets;
  1136. if (WARN_ON(!cl || !cl->dev))
  1137. return -ENODEV;
  1138. if (WARN_ON(!cb))
  1139. return -EINVAL;
  1140. dev = cl->dev;
  1141. buf = &cb->buf;
  1142. cl_dbg(dev, cl, "size=%d\n", buf->size);
  1143. rets = pm_runtime_get(dev->dev);
  1144. if (rets < 0 && rets != -EINPROGRESS) {
  1145. pm_runtime_put_noidle(dev->dev);
  1146. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1147. return rets;
  1148. }
  1149. cb->buf_idx = 0;
  1150. cl->writing_state = MEI_IDLE;
  1151. mei_hdr.host_addr = mei_cl_host_addr(cl);
  1152. mei_hdr.me_addr = mei_cl_me_id(cl);
  1153. mei_hdr.reserved = 0;
  1154. mei_hdr.msg_complete = 0;
  1155. mei_hdr.internal = cb->internal;
  1156. rets = mei_cl_flow_ctrl_creds(cl);
  1157. if (rets < 0)
  1158. goto err;
  1159. if (rets == 0) {
  1160. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1161. rets = buf->size;
  1162. goto out;
  1163. }
  1164. if (!mei_hbuf_acquire(dev)) {
  1165. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  1166. rets = buf->size;
  1167. goto out;
  1168. }
  1169. /* Check for a maximum length */
  1170. if (buf->size > mei_hbuf_max_len(dev)) {
  1171. mei_hdr.length = mei_hbuf_max_len(dev);
  1172. mei_hdr.msg_complete = 0;
  1173. } else {
  1174. mei_hdr.length = buf->size;
  1175. mei_hdr.msg_complete = 1;
  1176. }
  1177. rets = mei_write_message(dev, &mei_hdr, buf->data);
  1178. if (rets)
  1179. goto err;
  1180. rets = mei_cl_flow_ctrl_reduce(cl);
  1181. if (rets)
  1182. goto err;
  1183. cl->writing_state = MEI_WRITING;
  1184. cb->buf_idx = mei_hdr.length;
  1185. cb->completed = mei_hdr.msg_complete == 1;
  1186. out:
  1187. if (mei_hdr.msg_complete)
  1188. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  1189. else
  1190. list_add_tail(&cb->list, &dev->write_list.list);
  1191. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  1192. mutex_unlock(&dev->device_lock);
  1193. rets = wait_event_interruptible(cl->tx_wait,
  1194. cl->writing_state == MEI_WRITE_COMPLETE);
  1195. mutex_lock(&dev->device_lock);
  1196. /* wait_event_interruptible returns -ERESTARTSYS */
  1197. if (rets) {
  1198. if (signal_pending(current))
  1199. rets = -EINTR;
  1200. goto err;
  1201. }
  1202. }
  1203. rets = buf->size;
  1204. err:
  1205. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1206. pm_runtime_mark_last_busy(dev->dev);
  1207. pm_runtime_put_autosuspend(dev->dev);
  1208. return rets;
  1209. }
  1210. /**
  1211. * mei_cl_complete - processes completed operation for a client
  1212. *
  1213. * @cl: private data of the file object.
  1214. * @cb: callback block.
  1215. */
  1216. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  1217. {
  1218. struct mei_device *dev = cl->dev;
  1219. switch (cb->fop_type) {
  1220. case MEI_FOP_WRITE:
  1221. mei_io_cb_free(cb);
  1222. cl->writing_state = MEI_WRITE_COMPLETE;
  1223. if (waitqueue_active(&cl->tx_wait)) {
  1224. wake_up_interruptible(&cl->tx_wait);
  1225. } else {
  1226. pm_runtime_mark_last_busy(dev->dev);
  1227. pm_request_autosuspend(dev->dev);
  1228. }
  1229. break;
  1230. case MEI_FOP_READ:
  1231. list_add_tail(&cb->list, &cl->rd_completed);
  1232. if (waitqueue_active(&cl->rx_wait))
  1233. wake_up_interruptible_all(&cl->rx_wait);
  1234. else
  1235. mei_cl_bus_rx_event(cl);
  1236. break;
  1237. case MEI_FOP_CONNECT:
  1238. case MEI_FOP_DISCONNECT:
  1239. if (waitqueue_active(&cl->wait))
  1240. wake_up(&cl->wait);
  1241. break;
  1242. default:
  1243. BUG_ON(0);
  1244. }
  1245. }
  1246. /**
  1247. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  1248. *
  1249. * @dev: mei device
  1250. */
  1251. void mei_cl_all_disconnect(struct mei_device *dev)
  1252. {
  1253. struct mei_cl *cl;
  1254. list_for_each_entry(cl, &dev->file_list, link)
  1255. mei_cl_set_disconnected(cl);
  1256. }
  1257. /**
  1258. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  1259. *
  1260. * @dev: mei device
  1261. */
  1262. void mei_cl_all_wakeup(struct mei_device *dev)
  1263. {
  1264. struct mei_cl *cl;
  1265. list_for_each_entry(cl, &dev->file_list, link) {
  1266. if (waitqueue_active(&cl->rx_wait)) {
  1267. cl_dbg(dev, cl, "Waking up reading client!\n");
  1268. wake_up_interruptible(&cl->rx_wait);
  1269. }
  1270. if (waitqueue_active(&cl->tx_wait)) {
  1271. cl_dbg(dev, cl, "Waking up writing client!\n");
  1272. wake_up_interruptible(&cl->tx_wait);
  1273. }
  1274. }
  1275. }
  1276. /**
  1277. * mei_cl_all_write_clear - clear all pending writes
  1278. *
  1279. * @dev: mei device
  1280. */
  1281. void mei_cl_all_write_clear(struct mei_device *dev)
  1282. {
  1283. mei_io_list_free(&dev->write_list, NULL);
  1284. mei_io_list_free(&dev->write_waiting_list, NULL);
  1285. }