client.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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/signal.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_tx_cb_queue - queue tx callback
  310. *
  311. * Locking: called under "dev->device_lock" lock
  312. *
  313. * @cb: mei callback struct
  314. * @head: an instance of list to queue on
  315. */
  316. static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
  317. struct list_head *head)
  318. {
  319. list_add_tail(&cb->list, head);
  320. cb->cl->tx_cb_queued++;
  321. }
  322. /**
  323. * mei_tx_cb_dequeue - dequeue tx callback
  324. *
  325. * Locking: called under "dev->device_lock" lock
  326. *
  327. * @cb: mei callback struct to dequeue and free
  328. */
  329. static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
  330. {
  331. if (!WARN_ON(cb->cl->tx_cb_queued == 0))
  332. cb->cl->tx_cb_queued--;
  333. mei_io_cb_free(cb);
  334. }
  335. /**
  336. * mei_io_cb_init - allocate and initialize io callback
  337. *
  338. * @cl: mei client
  339. * @type: operation type
  340. * @fp: pointer to file structure
  341. *
  342. * Return: mei_cl_cb pointer or NULL;
  343. */
  344. static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
  345. enum mei_cb_file_ops type,
  346. const struct file *fp)
  347. {
  348. struct mei_cl_cb *cb;
  349. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  350. if (!cb)
  351. return NULL;
  352. INIT_LIST_HEAD(&cb->list);
  353. cb->fp = fp;
  354. cb->cl = cl;
  355. cb->buf_idx = 0;
  356. cb->fop_type = type;
  357. return cb;
  358. }
  359. /**
  360. * mei_io_list_flush_cl - removes cbs belonging to the cl.
  361. *
  362. * @head: an instance of our list structure
  363. * @cl: host client
  364. */
  365. static void mei_io_list_flush_cl(struct list_head *head,
  366. const struct mei_cl *cl)
  367. {
  368. struct mei_cl_cb *cb, *next;
  369. list_for_each_entry_safe(cb, next, head, list) {
  370. if (mei_cl_cmp_id(cl, cb->cl))
  371. list_del_init(&cb->list);
  372. }
  373. }
  374. /**
  375. * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
  376. *
  377. * @head: An instance of our list structure
  378. * @cl: host client
  379. */
  380. static void mei_io_tx_list_free_cl(struct list_head *head,
  381. const struct mei_cl *cl)
  382. {
  383. struct mei_cl_cb *cb, *next;
  384. list_for_each_entry_safe(cb, next, head, list) {
  385. if (mei_cl_cmp_id(cl, cb->cl))
  386. mei_tx_cb_dequeue(cb);
  387. }
  388. }
  389. /**
  390. * mei_io_list_free_fp - free cb from a list that matches file pointer
  391. *
  392. * @head: io list
  393. * @fp: file pointer (matching cb file object), may be NULL
  394. */
  395. static void mei_io_list_free_fp(struct list_head *head, const struct file *fp)
  396. {
  397. struct mei_cl_cb *cb, *next;
  398. list_for_each_entry_safe(cb, next, head, list)
  399. if (!fp || fp == cb->fp)
  400. mei_io_cb_free(cb);
  401. }
  402. /**
  403. * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
  404. *
  405. * @cl: host client
  406. * @length: size of the buffer
  407. * @fop_type: operation type
  408. * @fp: associated file pointer (might be NULL)
  409. *
  410. * Return: cb on success and NULL on failure
  411. */
  412. struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
  413. enum mei_cb_file_ops fop_type,
  414. const struct file *fp)
  415. {
  416. struct mei_cl_cb *cb;
  417. cb = mei_io_cb_init(cl, fop_type, fp);
  418. if (!cb)
  419. return NULL;
  420. if (length == 0)
  421. return cb;
  422. cb->buf.data = kmalloc(length, GFP_KERNEL);
  423. if (!cb->buf.data) {
  424. mei_io_cb_free(cb);
  425. return NULL;
  426. }
  427. cb->buf.size = length;
  428. return cb;
  429. }
  430. /**
  431. * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
  432. * and enqueuing of the control commands cb
  433. *
  434. * @cl: host client
  435. * @length: size of the buffer
  436. * @fop_type: operation type
  437. * @fp: associated file pointer (might be NULL)
  438. *
  439. * Return: cb on success and NULL on failure
  440. * Locking: called under "dev->device_lock" lock
  441. */
  442. struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
  443. enum mei_cb_file_ops fop_type,
  444. const struct file *fp)
  445. {
  446. struct mei_cl_cb *cb;
  447. /* for RX always allocate at least client's mtu */
  448. if (length)
  449. length = max_t(size_t, length, mei_cl_mtu(cl));
  450. cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
  451. if (!cb)
  452. return NULL;
  453. list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
  454. return cb;
  455. }
  456. /**
  457. * mei_cl_read_cb - find this cl's callback in the read list
  458. * for a specific file
  459. *
  460. * @cl: host client
  461. * @fp: file pointer (matching cb file object), may be NULL
  462. *
  463. * Return: cb on success, NULL if cb is not found
  464. */
  465. struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
  466. {
  467. struct mei_cl_cb *cb;
  468. list_for_each_entry(cb, &cl->rd_completed, list)
  469. if (!fp || fp == cb->fp)
  470. return cb;
  471. return NULL;
  472. }
  473. /**
  474. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  475. *
  476. * @cl: host client
  477. * @fp: file pointer (matching cb file object), may be NULL
  478. *
  479. * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
  480. */
  481. int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
  482. {
  483. struct mei_device *dev;
  484. if (WARN_ON(!cl || !cl->dev))
  485. return -EINVAL;
  486. dev = cl->dev;
  487. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  488. mei_io_tx_list_free_cl(&cl->dev->write_list, cl);
  489. mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl);
  490. mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
  491. mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
  492. mei_io_list_free_fp(&cl->rd_pending, fp);
  493. mei_io_list_free_fp(&cl->rd_completed, fp);
  494. return 0;
  495. }
  496. /**
  497. * mei_cl_init - initializes cl.
  498. *
  499. * @cl: host client to be initialized
  500. * @dev: mei device
  501. */
  502. static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  503. {
  504. memset(cl, 0, sizeof(struct mei_cl));
  505. init_waitqueue_head(&cl->wait);
  506. init_waitqueue_head(&cl->rx_wait);
  507. init_waitqueue_head(&cl->tx_wait);
  508. init_waitqueue_head(&cl->ev_wait);
  509. INIT_LIST_HEAD(&cl->rd_completed);
  510. INIT_LIST_HEAD(&cl->rd_pending);
  511. INIT_LIST_HEAD(&cl->link);
  512. cl->writing_state = MEI_IDLE;
  513. cl->state = MEI_FILE_UNINITIALIZED;
  514. cl->dev = dev;
  515. }
  516. /**
  517. * mei_cl_allocate - allocates cl structure and sets it up.
  518. *
  519. * @dev: mei device
  520. * Return: The allocated file or NULL on failure
  521. */
  522. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  523. {
  524. struct mei_cl *cl;
  525. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  526. if (!cl)
  527. return NULL;
  528. mei_cl_init(cl, dev);
  529. return cl;
  530. }
  531. /**
  532. * mei_cl_link - allocate host id in the host map
  533. *
  534. * @cl: host client
  535. *
  536. * Return: 0 on success
  537. * -EINVAL on incorrect values
  538. * -EMFILE if open count exceeded.
  539. */
  540. int mei_cl_link(struct mei_cl *cl)
  541. {
  542. struct mei_device *dev;
  543. int id;
  544. if (WARN_ON(!cl || !cl->dev))
  545. return -EINVAL;
  546. dev = cl->dev;
  547. id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
  548. if (id >= MEI_CLIENTS_MAX) {
  549. dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  550. return -EMFILE;
  551. }
  552. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  553. dev_err(dev->dev, "open_handle_count exceeded %d",
  554. MEI_MAX_OPEN_HANDLE_COUNT);
  555. return -EMFILE;
  556. }
  557. dev->open_handle_count++;
  558. cl->host_client_id = id;
  559. list_add_tail(&cl->link, &dev->file_list);
  560. set_bit(id, dev->host_clients_map);
  561. cl->state = MEI_FILE_INITIALIZING;
  562. cl_dbg(dev, cl, "link cl\n");
  563. return 0;
  564. }
  565. /**
  566. * mei_cl_unlink - remove host client from the list
  567. *
  568. * @cl: host client
  569. *
  570. * Return: always 0
  571. */
  572. int mei_cl_unlink(struct mei_cl *cl)
  573. {
  574. struct mei_device *dev;
  575. /* don't shout on error exit path */
  576. if (!cl)
  577. return 0;
  578. if (WARN_ON(!cl->dev))
  579. return 0;
  580. dev = cl->dev;
  581. cl_dbg(dev, cl, "unlink client");
  582. if (dev->open_handle_count > 0)
  583. dev->open_handle_count--;
  584. /* never clear the 0 bit */
  585. if (cl->host_client_id)
  586. clear_bit(cl->host_client_id, dev->host_clients_map);
  587. list_del_init(&cl->link);
  588. cl->state = MEI_FILE_UNINITIALIZED;
  589. cl->writing_state = MEI_IDLE;
  590. WARN_ON(!list_empty(&cl->rd_completed) ||
  591. !list_empty(&cl->rd_pending) ||
  592. !list_empty(&cl->link));
  593. return 0;
  594. }
  595. void mei_host_client_init(struct mei_device *dev)
  596. {
  597. dev->dev_state = MEI_DEV_ENABLED;
  598. dev->reset_count = 0;
  599. schedule_work(&dev->bus_rescan_work);
  600. pm_runtime_mark_last_busy(dev->dev);
  601. dev_dbg(dev->dev, "rpm: autosuspend\n");
  602. pm_request_autosuspend(dev->dev);
  603. }
  604. /**
  605. * mei_hbuf_acquire - try to acquire host buffer
  606. *
  607. * @dev: the device structure
  608. * Return: true if host buffer was acquired
  609. */
  610. bool mei_hbuf_acquire(struct mei_device *dev)
  611. {
  612. if (mei_pg_state(dev) == MEI_PG_ON ||
  613. mei_pg_in_transition(dev)) {
  614. dev_dbg(dev->dev, "device is in pg\n");
  615. return false;
  616. }
  617. if (!dev->hbuf_is_ready) {
  618. dev_dbg(dev->dev, "hbuf is not ready\n");
  619. return false;
  620. }
  621. dev->hbuf_is_ready = false;
  622. return true;
  623. }
  624. /**
  625. * mei_cl_wake_all - wake up readers, writers and event waiters so
  626. * they can be interrupted
  627. *
  628. * @cl: host client
  629. */
  630. static void mei_cl_wake_all(struct mei_cl *cl)
  631. {
  632. struct mei_device *dev = cl->dev;
  633. /* synchronized under device mutex */
  634. if (waitqueue_active(&cl->rx_wait)) {
  635. cl_dbg(dev, cl, "Waking up reading client!\n");
  636. wake_up_interruptible(&cl->rx_wait);
  637. }
  638. /* synchronized under device mutex */
  639. if (waitqueue_active(&cl->tx_wait)) {
  640. cl_dbg(dev, cl, "Waking up writing client!\n");
  641. wake_up_interruptible(&cl->tx_wait);
  642. }
  643. /* synchronized under device mutex */
  644. if (waitqueue_active(&cl->ev_wait)) {
  645. cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
  646. wake_up_interruptible(&cl->ev_wait);
  647. }
  648. /* synchronized under device mutex */
  649. if (waitqueue_active(&cl->wait)) {
  650. cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
  651. wake_up(&cl->wait);
  652. }
  653. }
  654. /**
  655. * mei_cl_set_disconnected - set disconnected state and clear
  656. * associated states and resources
  657. *
  658. * @cl: host client
  659. */
  660. static void mei_cl_set_disconnected(struct mei_cl *cl)
  661. {
  662. struct mei_device *dev = cl->dev;
  663. if (cl->state == MEI_FILE_DISCONNECTED ||
  664. cl->state <= MEI_FILE_INITIALIZING)
  665. return;
  666. cl->state = MEI_FILE_DISCONNECTED;
  667. mei_io_tx_list_free_cl(&dev->write_list, cl);
  668. mei_io_tx_list_free_cl(&dev->write_waiting_list, cl);
  669. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  670. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  671. mei_cl_wake_all(cl);
  672. cl->rx_flow_ctrl_creds = 0;
  673. cl->tx_flow_ctrl_creds = 0;
  674. cl->timer_count = 0;
  675. if (!cl->me_cl)
  676. return;
  677. if (!WARN_ON(cl->me_cl->connect_count == 0))
  678. cl->me_cl->connect_count--;
  679. if (cl->me_cl->connect_count == 0)
  680. cl->me_cl->tx_flow_ctrl_creds = 0;
  681. mei_me_cl_put(cl->me_cl);
  682. cl->me_cl = NULL;
  683. }
  684. static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
  685. {
  686. if (!mei_me_cl_get(me_cl))
  687. return -ENOENT;
  688. /* only one connection is allowed for fixed address clients */
  689. if (me_cl->props.fixed_address) {
  690. if (me_cl->connect_count) {
  691. mei_me_cl_put(me_cl);
  692. return -EBUSY;
  693. }
  694. }
  695. cl->me_cl = me_cl;
  696. cl->state = MEI_FILE_CONNECTING;
  697. cl->me_cl->connect_count++;
  698. return 0;
  699. }
  700. /*
  701. * mei_cl_send_disconnect - send disconnect request
  702. *
  703. * @cl: host client
  704. * @cb: callback block
  705. *
  706. * Return: 0, OK; otherwise, error.
  707. */
  708. static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
  709. {
  710. struct mei_device *dev;
  711. int ret;
  712. dev = cl->dev;
  713. ret = mei_hbm_cl_disconnect_req(dev, cl);
  714. cl->status = ret;
  715. if (ret) {
  716. cl->state = MEI_FILE_DISCONNECT_REPLY;
  717. return ret;
  718. }
  719. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  720. cl->timer_count = MEI_CONNECT_TIMEOUT;
  721. mei_schedule_stall_timer(dev);
  722. return 0;
  723. }
  724. /**
  725. * mei_cl_irq_disconnect - processes close related operation from
  726. * interrupt thread context - send disconnect request
  727. *
  728. * @cl: client
  729. * @cb: callback block.
  730. * @cmpl_list: complete list.
  731. *
  732. * Return: 0, OK; otherwise, error.
  733. */
  734. int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
  735. struct list_head *cmpl_list)
  736. {
  737. struct mei_device *dev = cl->dev;
  738. u32 msg_slots;
  739. int slots;
  740. int ret;
  741. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
  742. slots = mei_hbuf_empty_slots(dev);
  743. if (slots < msg_slots)
  744. return -EMSGSIZE;
  745. ret = mei_cl_send_disconnect(cl, cb);
  746. if (ret)
  747. list_move_tail(&cb->list, cmpl_list);
  748. return ret;
  749. }
  750. /**
  751. * __mei_cl_disconnect - disconnect host client from the me one
  752. * internal function runtime pm has to be already acquired
  753. *
  754. * @cl: host client
  755. *
  756. * Return: 0 on success, <0 on failure.
  757. */
  758. static int __mei_cl_disconnect(struct mei_cl *cl)
  759. {
  760. struct mei_device *dev;
  761. struct mei_cl_cb *cb;
  762. int rets;
  763. dev = cl->dev;
  764. cl->state = MEI_FILE_DISCONNECTING;
  765. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
  766. if (!cb) {
  767. rets = -ENOMEM;
  768. goto out;
  769. }
  770. if (mei_hbuf_acquire(dev)) {
  771. rets = mei_cl_send_disconnect(cl, cb);
  772. if (rets) {
  773. cl_err(dev, cl, "failed to disconnect.\n");
  774. goto out;
  775. }
  776. }
  777. mutex_unlock(&dev->device_lock);
  778. wait_event_timeout(cl->wait,
  779. cl->state == MEI_FILE_DISCONNECT_REPLY ||
  780. cl->state == MEI_FILE_DISCONNECTED,
  781. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  782. mutex_lock(&dev->device_lock);
  783. rets = cl->status;
  784. if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
  785. cl->state != MEI_FILE_DISCONNECTED) {
  786. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  787. rets = -ETIME;
  788. }
  789. out:
  790. /* we disconnect also on error */
  791. mei_cl_set_disconnected(cl);
  792. if (!rets)
  793. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  794. mei_io_cb_free(cb);
  795. return rets;
  796. }
  797. /**
  798. * mei_cl_disconnect - disconnect host client from the me one
  799. *
  800. * @cl: host client
  801. *
  802. * Locking: called under "dev->device_lock" lock
  803. *
  804. * Return: 0 on success, <0 on failure.
  805. */
  806. int mei_cl_disconnect(struct mei_cl *cl)
  807. {
  808. struct mei_device *dev;
  809. int rets;
  810. if (WARN_ON(!cl || !cl->dev))
  811. return -ENODEV;
  812. dev = cl->dev;
  813. cl_dbg(dev, cl, "disconnecting");
  814. if (!mei_cl_is_connected(cl))
  815. return 0;
  816. if (mei_cl_is_fixed_address(cl)) {
  817. mei_cl_set_disconnected(cl);
  818. return 0;
  819. }
  820. if (dev->dev_state == MEI_DEV_POWER_DOWN) {
  821. cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
  822. mei_cl_set_disconnected(cl);
  823. return 0;
  824. }
  825. rets = pm_runtime_get(dev->dev);
  826. if (rets < 0 && rets != -EINPROGRESS) {
  827. pm_runtime_put_noidle(dev->dev);
  828. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  829. return rets;
  830. }
  831. rets = __mei_cl_disconnect(cl);
  832. cl_dbg(dev, cl, "rpm: autosuspend\n");
  833. pm_runtime_mark_last_busy(dev->dev);
  834. pm_runtime_put_autosuspend(dev->dev);
  835. return rets;
  836. }
  837. /**
  838. * mei_cl_is_other_connecting - checks if other
  839. * client with the same me client id is connecting
  840. *
  841. * @cl: private data of the file object
  842. *
  843. * Return: true if other client is connected, false - otherwise.
  844. */
  845. static bool mei_cl_is_other_connecting(struct mei_cl *cl)
  846. {
  847. struct mei_device *dev;
  848. struct mei_cl_cb *cb;
  849. dev = cl->dev;
  850. list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
  851. if (cb->fop_type == MEI_FOP_CONNECT &&
  852. mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
  853. return true;
  854. }
  855. return false;
  856. }
  857. /**
  858. * mei_cl_send_connect - send connect request
  859. *
  860. * @cl: host client
  861. * @cb: callback block
  862. *
  863. * Return: 0, OK; otherwise, error.
  864. */
  865. static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
  866. {
  867. struct mei_device *dev;
  868. int ret;
  869. dev = cl->dev;
  870. ret = mei_hbm_cl_connect_req(dev, cl);
  871. cl->status = ret;
  872. if (ret) {
  873. cl->state = MEI_FILE_DISCONNECT_REPLY;
  874. return ret;
  875. }
  876. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  877. cl->timer_count = MEI_CONNECT_TIMEOUT;
  878. mei_schedule_stall_timer(dev);
  879. return 0;
  880. }
  881. /**
  882. * mei_cl_irq_connect - send connect request in irq_thread context
  883. *
  884. * @cl: host client
  885. * @cb: callback block
  886. * @cmpl_list: complete list
  887. *
  888. * Return: 0, OK; otherwise, error.
  889. */
  890. int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
  891. struct list_head *cmpl_list)
  892. {
  893. struct mei_device *dev = cl->dev;
  894. u32 msg_slots;
  895. int slots;
  896. int rets;
  897. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
  898. slots = mei_hbuf_empty_slots(dev);
  899. if (mei_cl_is_other_connecting(cl))
  900. return 0;
  901. if (slots < msg_slots)
  902. return -EMSGSIZE;
  903. rets = mei_cl_send_connect(cl, cb);
  904. if (rets)
  905. list_move_tail(&cb->list, cmpl_list);
  906. return rets;
  907. }
  908. /**
  909. * mei_cl_connect - connect host client to the me one
  910. *
  911. * @cl: host client
  912. * @me_cl: me client
  913. * @fp: pointer to file structure
  914. *
  915. * Locking: called under "dev->device_lock" lock
  916. *
  917. * Return: 0 on success, <0 on failure.
  918. */
  919. int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
  920. const struct file *fp)
  921. {
  922. struct mei_device *dev;
  923. struct mei_cl_cb *cb;
  924. int rets;
  925. if (WARN_ON(!cl || !cl->dev || !me_cl))
  926. return -ENODEV;
  927. dev = cl->dev;
  928. rets = mei_cl_set_connecting(cl, me_cl);
  929. if (rets)
  930. goto nortpm;
  931. if (mei_cl_is_fixed_address(cl)) {
  932. cl->state = MEI_FILE_CONNECTED;
  933. rets = 0;
  934. goto nortpm;
  935. }
  936. rets = pm_runtime_get(dev->dev);
  937. if (rets < 0 && rets != -EINPROGRESS) {
  938. pm_runtime_put_noidle(dev->dev);
  939. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  940. goto nortpm;
  941. }
  942. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
  943. if (!cb) {
  944. rets = -ENOMEM;
  945. goto out;
  946. }
  947. /* run hbuf acquire last so we don't have to undo */
  948. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  949. rets = mei_cl_send_connect(cl, cb);
  950. if (rets)
  951. goto out;
  952. }
  953. mutex_unlock(&dev->device_lock);
  954. wait_event_timeout(cl->wait,
  955. (cl->state == MEI_FILE_CONNECTED ||
  956. cl->state == MEI_FILE_DISCONNECTED ||
  957. cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
  958. cl->state == MEI_FILE_DISCONNECT_REPLY),
  959. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  960. mutex_lock(&dev->device_lock);
  961. if (!mei_cl_is_connected(cl)) {
  962. if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
  963. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  964. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  965. /* ignore disconnect return valuue;
  966. * in case of failure reset will be invoked
  967. */
  968. __mei_cl_disconnect(cl);
  969. rets = -EFAULT;
  970. goto out;
  971. }
  972. /* timeout or something went really wrong */
  973. if (!cl->status)
  974. cl->status = -EFAULT;
  975. }
  976. rets = cl->status;
  977. out:
  978. cl_dbg(dev, cl, "rpm: autosuspend\n");
  979. pm_runtime_mark_last_busy(dev->dev);
  980. pm_runtime_put_autosuspend(dev->dev);
  981. mei_io_cb_free(cb);
  982. nortpm:
  983. if (!mei_cl_is_connected(cl))
  984. mei_cl_set_disconnected(cl);
  985. return rets;
  986. }
  987. /**
  988. * mei_cl_alloc_linked - allocate and link host client
  989. *
  990. * @dev: the device structure
  991. *
  992. * Return: cl on success ERR_PTR on failure
  993. */
  994. struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
  995. {
  996. struct mei_cl *cl;
  997. int ret;
  998. cl = mei_cl_allocate(dev);
  999. if (!cl) {
  1000. ret = -ENOMEM;
  1001. goto err;
  1002. }
  1003. ret = mei_cl_link(cl);
  1004. if (ret)
  1005. goto err;
  1006. return cl;
  1007. err:
  1008. kfree(cl);
  1009. return ERR_PTR(ret);
  1010. }
  1011. /**
  1012. * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
  1013. *
  1014. * @cl: host client
  1015. *
  1016. * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
  1017. */
  1018. static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
  1019. {
  1020. if (WARN_ON(!cl || !cl->me_cl))
  1021. return -EINVAL;
  1022. if (cl->tx_flow_ctrl_creds > 0)
  1023. return 1;
  1024. if (mei_cl_is_fixed_address(cl))
  1025. return 1;
  1026. if (mei_cl_is_single_recv_buf(cl)) {
  1027. if (cl->me_cl->tx_flow_ctrl_creds > 0)
  1028. return 1;
  1029. }
  1030. return 0;
  1031. }
  1032. /**
  1033. * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
  1034. * for a client
  1035. *
  1036. * @cl: host client
  1037. *
  1038. * Return:
  1039. * 0 on success
  1040. * -EINVAL when ctrl credits are <= 0
  1041. */
  1042. static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
  1043. {
  1044. if (WARN_ON(!cl || !cl->me_cl))
  1045. return -EINVAL;
  1046. if (mei_cl_is_fixed_address(cl))
  1047. return 0;
  1048. if (mei_cl_is_single_recv_buf(cl)) {
  1049. if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
  1050. return -EINVAL;
  1051. cl->me_cl->tx_flow_ctrl_creds--;
  1052. } else {
  1053. if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
  1054. return -EINVAL;
  1055. cl->tx_flow_ctrl_creds--;
  1056. }
  1057. return 0;
  1058. }
  1059. /**
  1060. * mei_cl_notify_fop2req - convert fop to proper request
  1061. *
  1062. * @fop: client notification start response command
  1063. *
  1064. * Return: MEI_HBM_NOTIFICATION_START/STOP
  1065. */
  1066. u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
  1067. {
  1068. if (fop == MEI_FOP_NOTIFY_START)
  1069. return MEI_HBM_NOTIFICATION_START;
  1070. else
  1071. return MEI_HBM_NOTIFICATION_STOP;
  1072. }
  1073. /**
  1074. * mei_cl_notify_req2fop - convert notification request top file operation type
  1075. *
  1076. * @req: hbm notification request type
  1077. *
  1078. * Return: MEI_FOP_NOTIFY_START/STOP
  1079. */
  1080. enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
  1081. {
  1082. if (req == MEI_HBM_NOTIFICATION_START)
  1083. return MEI_FOP_NOTIFY_START;
  1084. else
  1085. return MEI_FOP_NOTIFY_STOP;
  1086. }
  1087. /**
  1088. * mei_cl_irq_notify - send notification request in irq_thread context
  1089. *
  1090. * @cl: client
  1091. * @cb: callback block.
  1092. * @cmpl_list: complete list.
  1093. *
  1094. * Return: 0 on such and error otherwise.
  1095. */
  1096. int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
  1097. struct list_head *cmpl_list)
  1098. {
  1099. struct mei_device *dev = cl->dev;
  1100. u32 msg_slots;
  1101. int slots;
  1102. int ret;
  1103. bool request;
  1104. msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
  1105. slots = mei_hbuf_empty_slots(dev);
  1106. if (slots < msg_slots)
  1107. return -EMSGSIZE;
  1108. request = mei_cl_notify_fop2req(cb->fop_type);
  1109. ret = mei_hbm_cl_notify_req(dev, cl, request);
  1110. if (ret) {
  1111. cl->status = ret;
  1112. list_move_tail(&cb->list, cmpl_list);
  1113. return ret;
  1114. }
  1115. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1116. return 0;
  1117. }
  1118. /**
  1119. * mei_cl_notify_request - send notification stop/start request
  1120. *
  1121. * @cl: host client
  1122. * @fp: associate request with file
  1123. * @request: 1 for start or 0 for stop
  1124. *
  1125. * Locking: called under "dev->device_lock" lock
  1126. *
  1127. * Return: 0 on such and error otherwise.
  1128. */
  1129. int mei_cl_notify_request(struct mei_cl *cl,
  1130. const struct file *fp, u8 request)
  1131. {
  1132. struct mei_device *dev;
  1133. struct mei_cl_cb *cb;
  1134. enum mei_cb_file_ops fop_type;
  1135. int rets;
  1136. if (WARN_ON(!cl || !cl->dev))
  1137. return -ENODEV;
  1138. dev = cl->dev;
  1139. if (!dev->hbm_f_ev_supported) {
  1140. cl_dbg(dev, cl, "notifications not supported\n");
  1141. return -EOPNOTSUPP;
  1142. }
  1143. if (!mei_cl_is_connected(cl))
  1144. return -ENODEV;
  1145. rets = pm_runtime_get(dev->dev);
  1146. if (rets < 0 && rets != -EINPROGRESS) {
  1147. pm_runtime_put_noidle(dev->dev);
  1148. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1149. return rets;
  1150. }
  1151. fop_type = mei_cl_notify_req2fop(request);
  1152. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
  1153. if (!cb) {
  1154. rets = -ENOMEM;
  1155. goto out;
  1156. }
  1157. if (mei_hbuf_acquire(dev)) {
  1158. if (mei_hbm_cl_notify_req(dev, cl, request)) {
  1159. rets = -ENODEV;
  1160. goto out;
  1161. }
  1162. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1163. }
  1164. mutex_unlock(&dev->device_lock);
  1165. wait_event_timeout(cl->wait,
  1166. cl->notify_en == request || !mei_cl_is_connected(cl),
  1167. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  1168. mutex_lock(&dev->device_lock);
  1169. if (cl->notify_en != request && !cl->status)
  1170. cl->status = -EFAULT;
  1171. rets = cl->status;
  1172. out:
  1173. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1174. pm_runtime_mark_last_busy(dev->dev);
  1175. pm_runtime_put_autosuspend(dev->dev);
  1176. mei_io_cb_free(cb);
  1177. return rets;
  1178. }
  1179. /**
  1180. * mei_cl_notify - raise notification
  1181. *
  1182. * @cl: host client
  1183. *
  1184. * Locking: called under "dev->device_lock" lock
  1185. */
  1186. void mei_cl_notify(struct mei_cl *cl)
  1187. {
  1188. struct mei_device *dev;
  1189. if (!cl || !cl->dev)
  1190. return;
  1191. dev = cl->dev;
  1192. if (!cl->notify_en)
  1193. return;
  1194. cl_dbg(dev, cl, "notify event");
  1195. cl->notify_ev = true;
  1196. if (!mei_cl_bus_notify_event(cl))
  1197. wake_up_interruptible(&cl->ev_wait);
  1198. if (cl->ev_async)
  1199. kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
  1200. }
  1201. /**
  1202. * mei_cl_notify_get - get or wait for notification event
  1203. *
  1204. * @cl: host client
  1205. * @block: this request is blocking
  1206. * @notify_ev: true if notification event was received
  1207. *
  1208. * Locking: called under "dev->device_lock" lock
  1209. *
  1210. * Return: 0 on such and error otherwise.
  1211. */
  1212. int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
  1213. {
  1214. struct mei_device *dev;
  1215. int rets;
  1216. *notify_ev = false;
  1217. if (WARN_ON(!cl || !cl->dev))
  1218. return -ENODEV;
  1219. dev = cl->dev;
  1220. if (!dev->hbm_f_ev_supported) {
  1221. cl_dbg(dev, cl, "notifications not supported\n");
  1222. return -EOPNOTSUPP;
  1223. }
  1224. if (!mei_cl_is_connected(cl))
  1225. return -ENODEV;
  1226. if (cl->notify_ev)
  1227. goto out;
  1228. if (!block)
  1229. return -EAGAIN;
  1230. mutex_unlock(&dev->device_lock);
  1231. rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
  1232. mutex_lock(&dev->device_lock);
  1233. if (rets < 0)
  1234. return rets;
  1235. out:
  1236. *notify_ev = cl->notify_ev;
  1237. cl->notify_ev = false;
  1238. return 0;
  1239. }
  1240. /**
  1241. * mei_cl_read_start - the start read client message function.
  1242. *
  1243. * @cl: host client
  1244. * @length: number of bytes to read
  1245. * @fp: pointer to file structure
  1246. *
  1247. * Return: 0 on success, <0 on failure.
  1248. */
  1249. int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
  1250. {
  1251. struct mei_device *dev;
  1252. struct mei_cl_cb *cb;
  1253. int rets;
  1254. if (WARN_ON(!cl || !cl->dev))
  1255. return -ENODEV;
  1256. dev = cl->dev;
  1257. if (!mei_cl_is_connected(cl))
  1258. return -ENODEV;
  1259. if (!mei_me_cl_is_active(cl->me_cl)) {
  1260. cl_err(dev, cl, "no such me client\n");
  1261. return -ENOTTY;
  1262. }
  1263. if (mei_cl_is_fixed_address(cl))
  1264. return 0;
  1265. /* HW currently supports only one pending read */
  1266. if (cl->rx_flow_ctrl_creds)
  1267. return -EBUSY;
  1268. cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
  1269. if (!cb)
  1270. return -ENOMEM;
  1271. rets = pm_runtime_get(dev->dev);
  1272. if (rets < 0 && rets != -EINPROGRESS) {
  1273. pm_runtime_put_noidle(dev->dev);
  1274. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1275. goto nortpm;
  1276. }
  1277. rets = 0;
  1278. if (mei_hbuf_acquire(dev)) {
  1279. rets = mei_hbm_cl_flow_control_req(dev, cl);
  1280. if (rets < 0)
  1281. goto out;
  1282. list_move_tail(&cb->list, &cl->rd_pending);
  1283. }
  1284. cl->rx_flow_ctrl_creds++;
  1285. out:
  1286. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1287. pm_runtime_mark_last_busy(dev->dev);
  1288. pm_runtime_put_autosuspend(dev->dev);
  1289. nortpm:
  1290. if (rets)
  1291. mei_io_cb_free(cb);
  1292. return rets;
  1293. }
  1294. /**
  1295. * mei_cl_irq_write - write a message to device
  1296. * from the interrupt thread context
  1297. *
  1298. * @cl: client
  1299. * @cb: callback block.
  1300. * @cmpl_list: complete list.
  1301. *
  1302. * Return: 0, OK; otherwise error.
  1303. */
  1304. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  1305. struct list_head *cmpl_list)
  1306. {
  1307. struct mei_device *dev;
  1308. struct mei_msg_data *buf;
  1309. struct mei_msg_hdr mei_hdr;
  1310. size_t len;
  1311. u32 msg_slots;
  1312. int slots;
  1313. int rets;
  1314. bool first_chunk;
  1315. if (WARN_ON(!cl || !cl->dev))
  1316. return -ENODEV;
  1317. dev = cl->dev;
  1318. buf = &cb->buf;
  1319. first_chunk = cb->buf_idx == 0;
  1320. rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
  1321. if (rets < 0)
  1322. goto err;
  1323. if (rets == 0) {
  1324. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1325. return 0;
  1326. }
  1327. slots = mei_hbuf_empty_slots(dev);
  1328. len = buf->size - cb->buf_idx;
  1329. msg_slots = mei_data2slots(len);
  1330. mei_hdr.host_addr = mei_cl_host_addr(cl);
  1331. mei_hdr.me_addr = mei_cl_me_id(cl);
  1332. mei_hdr.reserved = 0;
  1333. mei_hdr.internal = cb->internal;
  1334. if (slots >= msg_slots) {
  1335. mei_hdr.length = len;
  1336. mei_hdr.msg_complete = 1;
  1337. /* Split the message only if we can write the whole host buffer */
  1338. } else if (slots == dev->hbuf_depth) {
  1339. msg_slots = slots;
  1340. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  1341. mei_hdr.length = len;
  1342. mei_hdr.msg_complete = 0;
  1343. } else {
  1344. /* wait for next time the host buffer is empty */
  1345. return 0;
  1346. }
  1347. cl_dbg(dev, cl, "buf: size = %zu idx = %zu\n",
  1348. cb->buf.size, cb->buf_idx);
  1349. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  1350. if (rets)
  1351. goto err;
  1352. cl->status = 0;
  1353. cl->writing_state = MEI_WRITING;
  1354. cb->buf_idx += mei_hdr.length;
  1355. cb->completed = mei_hdr.msg_complete == 1;
  1356. if (first_chunk) {
  1357. if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
  1358. rets = -EIO;
  1359. goto err;
  1360. }
  1361. }
  1362. if (mei_hdr.msg_complete)
  1363. list_move_tail(&cb->list, &dev->write_waiting_list);
  1364. return 0;
  1365. err:
  1366. cl->status = rets;
  1367. list_move_tail(&cb->list, cmpl_list);
  1368. return rets;
  1369. }
  1370. /**
  1371. * mei_cl_write - submit a write cb to mei device
  1372. * assumes device_lock is locked
  1373. *
  1374. * @cl: host client
  1375. * @cb: write callback with filled data
  1376. *
  1377. * Return: number of bytes sent on success, <0 on failure.
  1378. */
  1379. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
  1380. {
  1381. struct mei_device *dev;
  1382. struct mei_msg_data *buf;
  1383. struct mei_msg_hdr mei_hdr;
  1384. int size;
  1385. int rets;
  1386. bool blocking;
  1387. if (WARN_ON(!cl || !cl->dev))
  1388. return -ENODEV;
  1389. if (WARN_ON(!cb))
  1390. return -EINVAL;
  1391. dev = cl->dev;
  1392. buf = &cb->buf;
  1393. size = buf->size;
  1394. blocking = cb->blocking;
  1395. cl_dbg(dev, cl, "size=%d\n", size);
  1396. rets = pm_runtime_get(dev->dev);
  1397. if (rets < 0 && rets != -EINPROGRESS) {
  1398. pm_runtime_put_noidle(dev->dev);
  1399. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1400. goto free;
  1401. }
  1402. cb->buf_idx = 0;
  1403. cl->writing_state = MEI_IDLE;
  1404. mei_hdr.host_addr = mei_cl_host_addr(cl);
  1405. mei_hdr.me_addr = mei_cl_me_id(cl);
  1406. mei_hdr.reserved = 0;
  1407. mei_hdr.msg_complete = 0;
  1408. mei_hdr.internal = cb->internal;
  1409. rets = mei_cl_tx_flow_ctrl_creds(cl);
  1410. if (rets < 0)
  1411. goto err;
  1412. if (rets == 0) {
  1413. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1414. rets = size;
  1415. goto out;
  1416. }
  1417. if (!mei_hbuf_acquire(dev)) {
  1418. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  1419. rets = size;
  1420. goto out;
  1421. }
  1422. /* Check for a maximum length */
  1423. if (size > mei_hbuf_max_len(dev)) {
  1424. mei_hdr.length = mei_hbuf_max_len(dev);
  1425. mei_hdr.msg_complete = 0;
  1426. } else {
  1427. mei_hdr.length = size;
  1428. mei_hdr.msg_complete = 1;
  1429. }
  1430. rets = mei_write_message(dev, &mei_hdr, buf->data);
  1431. if (rets)
  1432. goto err;
  1433. rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
  1434. if (rets)
  1435. goto err;
  1436. cl->writing_state = MEI_WRITING;
  1437. cb->buf_idx = mei_hdr.length;
  1438. cb->completed = mei_hdr.msg_complete == 1;
  1439. out:
  1440. if (mei_hdr.msg_complete)
  1441. mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
  1442. else
  1443. mei_tx_cb_enqueue(cb, &dev->write_list);
  1444. cb = NULL;
  1445. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  1446. mutex_unlock(&dev->device_lock);
  1447. rets = wait_event_interruptible(cl->tx_wait,
  1448. cl->writing_state == MEI_WRITE_COMPLETE ||
  1449. (!mei_cl_is_connected(cl)));
  1450. mutex_lock(&dev->device_lock);
  1451. /* wait_event_interruptible returns -ERESTARTSYS */
  1452. if (rets) {
  1453. if (signal_pending(current))
  1454. rets = -EINTR;
  1455. goto err;
  1456. }
  1457. if (cl->writing_state != MEI_WRITE_COMPLETE) {
  1458. rets = -EFAULT;
  1459. goto err;
  1460. }
  1461. }
  1462. rets = size;
  1463. err:
  1464. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1465. pm_runtime_mark_last_busy(dev->dev);
  1466. pm_runtime_put_autosuspend(dev->dev);
  1467. free:
  1468. mei_io_cb_free(cb);
  1469. return rets;
  1470. }
  1471. /**
  1472. * mei_cl_complete - processes completed operation for a client
  1473. *
  1474. * @cl: private data of the file object.
  1475. * @cb: callback block.
  1476. */
  1477. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  1478. {
  1479. struct mei_device *dev = cl->dev;
  1480. switch (cb->fop_type) {
  1481. case MEI_FOP_WRITE:
  1482. mei_tx_cb_dequeue(cb);
  1483. cl->writing_state = MEI_WRITE_COMPLETE;
  1484. if (waitqueue_active(&cl->tx_wait)) {
  1485. wake_up_interruptible(&cl->tx_wait);
  1486. } else {
  1487. pm_runtime_mark_last_busy(dev->dev);
  1488. pm_request_autosuspend(dev->dev);
  1489. }
  1490. break;
  1491. case MEI_FOP_READ:
  1492. list_add_tail(&cb->list, &cl->rd_completed);
  1493. if (!mei_cl_is_fixed_address(cl) &&
  1494. !WARN_ON(!cl->rx_flow_ctrl_creds))
  1495. cl->rx_flow_ctrl_creds--;
  1496. if (!mei_cl_bus_rx_event(cl))
  1497. wake_up_interruptible(&cl->rx_wait);
  1498. break;
  1499. case MEI_FOP_CONNECT:
  1500. case MEI_FOP_DISCONNECT:
  1501. case MEI_FOP_NOTIFY_STOP:
  1502. case MEI_FOP_NOTIFY_START:
  1503. if (waitqueue_active(&cl->wait))
  1504. wake_up(&cl->wait);
  1505. break;
  1506. case MEI_FOP_DISCONNECT_RSP:
  1507. mei_io_cb_free(cb);
  1508. mei_cl_set_disconnected(cl);
  1509. break;
  1510. default:
  1511. BUG_ON(0);
  1512. }
  1513. }
  1514. /**
  1515. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  1516. *
  1517. * @dev: mei device
  1518. */
  1519. void mei_cl_all_disconnect(struct mei_device *dev)
  1520. {
  1521. struct mei_cl *cl;
  1522. list_for_each_entry(cl, &dev->file_list, link)
  1523. mei_cl_set_disconnected(cl);
  1524. }