client.c 38 KB

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