client.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2003-2022, Intel Corporation. All rights reserved.
  4. * Intel Management Engine Interface (Intel MEI) Linux driver
  5. */
  6. #include <linux/sched/signal.h>
  7. #include <linux/wait.h>
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/mei.h>
  13. #include "mei_dev.h"
  14. #include "hbm.h"
  15. #include "client.h"
  16. /**
  17. * mei_me_cl_init - initialize me client
  18. *
  19. * @me_cl: me client
  20. */
  21. void mei_me_cl_init(struct mei_me_client *me_cl)
  22. {
  23. INIT_LIST_HEAD(&me_cl->list);
  24. kref_init(&me_cl->refcnt);
  25. }
  26. /**
  27. * mei_me_cl_get - increases me client refcount
  28. *
  29. * @me_cl: me client
  30. *
  31. * Locking: called under "dev->device_lock" lock
  32. *
  33. * Return: me client or NULL
  34. */
  35. struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
  36. {
  37. if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
  38. return me_cl;
  39. return NULL;
  40. }
  41. /**
  42. * mei_me_cl_release - free me client
  43. *
  44. * @ref: me_client refcount
  45. *
  46. * Locking: called under "dev->device_lock" lock
  47. */
  48. static void mei_me_cl_release(struct kref *ref)
  49. {
  50. struct mei_me_client *me_cl =
  51. container_of(ref, struct mei_me_client, refcnt);
  52. kfree(me_cl);
  53. }
  54. /**
  55. * mei_me_cl_put - decrease me client refcount and free client if necessary
  56. *
  57. * @me_cl: me client
  58. *
  59. * Locking: called under "dev->device_lock" lock
  60. */
  61. void mei_me_cl_put(struct mei_me_client *me_cl)
  62. {
  63. if (me_cl)
  64. kref_put(&me_cl->refcnt, mei_me_cl_release);
  65. }
  66. /**
  67. * __mei_me_cl_del - delete me client from the list and decrease
  68. * reference counter
  69. *
  70. * @dev: mei device
  71. * @me_cl: me client
  72. *
  73. * Locking: dev->me_clients_rwsem
  74. */
  75. static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  76. {
  77. if (!me_cl)
  78. return;
  79. list_del_init(&me_cl->list);
  80. mei_me_cl_put(me_cl);
  81. }
  82. /**
  83. * mei_me_cl_del - delete me client from the list and decrease
  84. * reference counter
  85. *
  86. * @dev: mei device
  87. * @me_cl: me client
  88. */
  89. void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  90. {
  91. down_write(&dev->me_clients_rwsem);
  92. __mei_me_cl_del(dev, me_cl);
  93. up_write(&dev->me_clients_rwsem);
  94. }
  95. /**
  96. * mei_me_cl_add - add me client to the list
  97. *
  98. * @dev: mei device
  99. * @me_cl: me client
  100. */
  101. void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
  102. {
  103. down_write(&dev->me_clients_rwsem);
  104. list_add(&me_cl->list, &dev->me_clients);
  105. up_write(&dev->me_clients_rwsem);
  106. }
  107. /**
  108. * __mei_me_cl_by_uuid - locate me client by uuid
  109. * increases ref count
  110. *
  111. * @dev: mei device
  112. * @uuid: me client uuid
  113. *
  114. * Return: me client or NULL if not found
  115. *
  116. * Locking: dev->me_clients_rwsem
  117. */
  118. static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
  119. const uuid_le *uuid)
  120. {
  121. struct mei_me_client *me_cl;
  122. const uuid_le *pn;
  123. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  124. list_for_each_entry(me_cl, &dev->me_clients, list) {
  125. pn = &me_cl->props.protocol_name;
  126. if (uuid_le_cmp(*uuid, *pn) == 0)
  127. return mei_me_cl_get(me_cl);
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * mei_me_cl_by_uuid - locate me client by uuid
  133. * increases ref count
  134. *
  135. * @dev: mei device
  136. * @uuid: me client uuid
  137. *
  138. * Return: me client or NULL if not found
  139. *
  140. * Locking: dev->me_clients_rwsem
  141. */
  142. struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
  143. const uuid_le *uuid)
  144. {
  145. struct mei_me_client *me_cl;
  146. down_read(&dev->me_clients_rwsem);
  147. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  148. up_read(&dev->me_clients_rwsem);
  149. return me_cl;
  150. }
  151. /**
  152. * mei_me_cl_by_id - locate me client by client id
  153. * increases ref count
  154. *
  155. * @dev: the device structure
  156. * @client_id: me client id
  157. *
  158. * Return: me client or NULL if not found
  159. *
  160. * Locking: dev->me_clients_rwsem
  161. */
  162. struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  163. {
  164. struct mei_me_client *__me_cl, *me_cl = NULL;
  165. down_read(&dev->me_clients_rwsem);
  166. list_for_each_entry(__me_cl, &dev->me_clients, list) {
  167. if (__me_cl->client_id == client_id) {
  168. me_cl = mei_me_cl_get(__me_cl);
  169. break;
  170. }
  171. }
  172. up_read(&dev->me_clients_rwsem);
  173. return me_cl;
  174. }
  175. /**
  176. * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
  177. * increases ref count
  178. *
  179. * @dev: the device structure
  180. * @uuid: me client uuid
  181. * @client_id: me client id
  182. *
  183. * Return: me client or null if not found
  184. *
  185. * Locking: dev->me_clients_rwsem
  186. */
  187. static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
  188. const uuid_le *uuid, u8 client_id)
  189. {
  190. struct mei_me_client *me_cl;
  191. const uuid_le *pn;
  192. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  193. list_for_each_entry(me_cl, &dev->me_clients, list) {
  194. pn = &me_cl->props.protocol_name;
  195. if (uuid_le_cmp(*uuid, *pn) == 0 &&
  196. me_cl->client_id == client_id)
  197. return mei_me_cl_get(me_cl);
  198. }
  199. return NULL;
  200. }
  201. /**
  202. * mei_me_cl_by_uuid_id - locate me client by client id and uuid
  203. * increases ref count
  204. *
  205. * @dev: the device structure
  206. * @uuid: me client uuid
  207. * @client_id: me client id
  208. *
  209. * Return: me client or null if not found
  210. */
  211. struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
  212. const uuid_le *uuid, u8 client_id)
  213. {
  214. struct mei_me_client *me_cl;
  215. down_read(&dev->me_clients_rwsem);
  216. me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
  217. up_read(&dev->me_clients_rwsem);
  218. return me_cl;
  219. }
  220. /**
  221. * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
  222. *
  223. * @dev: the device structure
  224. * @uuid: me client uuid
  225. *
  226. * Locking: called under "dev->device_lock" lock
  227. */
  228. void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
  229. {
  230. struct mei_me_client *me_cl;
  231. dev_dbg(&dev->dev, "remove %pUl\n", uuid);
  232. down_write(&dev->me_clients_rwsem);
  233. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  234. __mei_me_cl_del(dev, me_cl);
  235. mei_me_cl_put(me_cl);
  236. up_write(&dev->me_clients_rwsem);
  237. }
  238. /**
  239. * mei_me_cl_rm_all - remove all me clients
  240. *
  241. * @dev: the device structure
  242. *
  243. * Locking: called under "dev->device_lock" lock
  244. */
  245. void mei_me_cl_rm_all(struct mei_device *dev)
  246. {
  247. struct mei_me_client *me_cl, *next;
  248. down_write(&dev->me_clients_rwsem);
  249. list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
  250. __mei_me_cl_del(dev, me_cl);
  251. up_write(&dev->me_clients_rwsem);
  252. }
  253. /**
  254. * mei_io_cb_free - free mei_cb_private related memory
  255. *
  256. * @cb: mei callback struct
  257. */
  258. void mei_io_cb_free(struct mei_cl_cb *cb)
  259. {
  260. if (cb == NULL)
  261. return;
  262. list_del(&cb->list);
  263. kvfree(cb->buf.data);
  264. kfree(cb->ext_hdr);
  265. kfree(cb);
  266. }
  267. /**
  268. * mei_tx_cb_enqueue - queue tx callback
  269. *
  270. * @cb: mei callback struct
  271. * @head: an instance of list to queue on
  272. *
  273. * Locking: called under "dev->device_lock" lock
  274. */
  275. static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
  276. struct list_head *head)
  277. {
  278. list_add_tail(&cb->list, head);
  279. cb->cl->tx_cb_queued++;
  280. }
  281. /**
  282. * mei_tx_cb_dequeue - dequeue tx callback
  283. *
  284. * @cb: mei callback struct to dequeue and free
  285. *
  286. * Locking: called under "dev->device_lock" lock
  287. */
  288. static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
  289. {
  290. if (!WARN_ON(cb->cl->tx_cb_queued == 0))
  291. cb->cl->tx_cb_queued--;
  292. mei_io_cb_free(cb);
  293. }
  294. /**
  295. * mei_cl_set_read_by_fp - set pending_read flag to vtag struct for given fp
  296. *
  297. * @cl: mei client
  298. * @fp: pointer to file structure
  299. *
  300. * Locking: called under "dev->device_lock" lock
  301. */
  302. static void mei_cl_set_read_by_fp(const struct mei_cl *cl,
  303. const struct file *fp)
  304. {
  305. struct mei_cl_vtag *cl_vtag;
  306. list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
  307. if (cl_vtag->fp == fp) {
  308. cl_vtag->pending_read = true;
  309. return;
  310. }
  311. }
  312. }
  313. /**
  314. * mei_io_cb_init - allocate and initialize io callback
  315. *
  316. * @cl: mei client
  317. * @type: operation type
  318. * @fp: pointer to file structure
  319. *
  320. * Return: mei_cl_cb pointer or NULL;
  321. */
  322. static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
  323. enum mei_cb_file_ops type,
  324. const struct file *fp)
  325. {
  326. struct mei_cl_cb *cb;
  327. cb = kzalloc_obj(*cb);
  328. if (!cb)
  329. return NULL;
  330. INIT_LIST_HEAD(&cb->list);
  331. cb->fp = fp;
  332. cb->cl = cl;
  333. cb->buf_idx = 0;
  334. cb->fop_type = type;
  335. cb->vtag = 0;
  336. cb->ext_hdr = NULL;
  337. return cb;
  338. }
  339. /**
  340. * mei_io_list_flush_cl - removes cbs belonging to the cl.
  341. *
  342. * @head: an instance of our list structure
  343. * @cl: host client
  344. */
  345. static void mei_io_list_flush_cl(struct list_head *head,
  346. const struct mei_cl *cl)
  347. {
  348. struct mei_cl_cb *cb, *next;
  349. list_for_each_entry_safe(cb, next, head, list) {
  350. if (cl == cb->cl) {
  351. list_del_init(&cb->list);
  352. if (cb->fop_type == MEI_FOP_READ)
  353. mei_io_cb_free(cb);
  354. }
  355. }
  356. }
  357. /**
  358. * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
  359. *
  360. * @head: An instance of our list structure
  361. * @cl: host client
  362. * @fp: file pointer (matching cb file object), may be NULL
  363. */
  364. static void mei_io_tx_list_free_cl(struct list_head *head,
  365. const struct mei_cl *cl,
  366. const struct file *fp)
  367. {
  368. struct mei_cl_cb *cb, *next;
  369. list_for_each_entry_safe(cb, next, head, list) {
  370. if (cl == cb->cl && (!fp || fp == cb->fp))
  371. mei_tx_cb_dequeue(cb);
  372. }
  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. static 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_free_pending - free pending cb
  389. *
  390. * @cl: host client
  391. */
  392. static void mei_cl_free_pending(struct mei_cl *cl)
  393. {
  394. struct mei_cl_cb *cb;
  395. cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
  396. mei_io_cb_free(cb);
  397. }
  398. /**
  399. * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
  400. *
  401. * @cl: host client
  402. * @length: size of the buffer
  403. * @fop_type: operation type
  404. * @fp: associated file pointer (might be NULL)
  405. *
  406. * Return: cb on success and NULL on failure
  407. */
  408. struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
  409. enum mei_cb_file_ops fop_type,
  410. const struct file *fp)
  411. {
  412. struct mei_cl_cb *cb;
  413. cb = mei_io_cb_init(cl, fop_type, fp);
  414. if (!cb)
  415. return NULL;
  416. if (length == 0)
  417. return cb;
  418. cb->buf.data = kvmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
  419. if (!cb->buf.data) {
  420. mei_io_cb_free(cb);
  421. return NULL;
  422. }
  423. cb->buf.size = length;
  424. return cb;
  425. }
  426. /**
  427. * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
  428. * and enqueuing of the control commands cb
  429. *
  430. * @cl: host client
  431. * @length: size of the buffer
  432. * @fop_type: operation type
  433. * @fp: associated file pointer (might be NULL)
  434. *
  435. * Return: cb on success and NULL on failure
  436. * Locking: called under "dev->device_lock" lock
  437. */
  438. struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
  439. enum mei_cb_file_ops fop_type,
  440. const struct file *fp)
  441. {
  442. struct mei_cl_cb *cb;
  443. /* for RX always allocate at least client's mtu */
  444. if (length)
  445. length = max_t(size_t, length, mei_cl_mtu(cl));
  446. cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
  447. if (!cb)
  448. return NULL;
  449. list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
  450. return cb;
  451. }
  452. /**
  453. * mei_cl_read_cb - find this cl's callback in the read list
  454. * for a specific file
  455. *
  456. * @cl: host client
  457. * @fp: file pointer (matching cb file object), may be NULL
  458. *
  459. * Return: cb on success, NULL if cb is not found
  460. */
  461. struct mei_cl_cb *mei_cl_read_cb(struct mei_cl *cl, const struct file *fp)
  462. {
  463. struct mei_cl_cb *cb;
  464. struct mei_cl_cb *ret_cb = NULL;
  465. spin_lock(&cl->rd_completed_lock);
  466. list_for_each_entry(cb, &cl->rd_completed, list)
  467. if (!fp || fp == cb->fp) {
  468. ret_cb = cb;
  469. break;
  470. }
  471. spin_unlock(&cl->rd_completed_lock);
  472. return ret_cb;
  473. }
  474. /**
  475. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  476. *
  477. * @cl: host client
  478. * @fp: file pointer (matching cb file object), may be NULL
  479. *
  480. * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
  481. */
  482. int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
  483. {
  484. struct mei_device *dev;
  485. if (WARN_ON(!cl || !cl->dev))
  486. return -EINVAL;
  487. dev = cl->dev;
  488. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  489. mei_io_tx_list_free_cl(&cl->dev->write_list, cl, fp);
  490. mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl, fp);
  491. /* free pending and control cb only in final flush */
  492. if (!fp) {
  493. mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
  494. mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
  495. mei_cl_free_pending(cl);
  496. }
  497. spin_lock(&cl->rd_completed_lock);
  498. mei_io_list_free_fp(&cl->rd_completed, fp);
  499. spin_unlock(&cl->rd_completed_lock);
  500. return 0;
  501. }
  502. /**
  503. * mei_cl_init - initializes cl.
  504. *
  505. * @cl: host client to be initialized
  506. * @dev: mei device
  507. */
  508. static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  509. {
  510. memset(cl, 0, sizeof(*cl));
  511. init_waitqueue_head(&cl->wait);
  512. init_waitqueue_head(&cl->rx_wait);
  513. init_waitqueue_head(&cl->tx_wait);
  514. init_waitqueue_head(&cl->ev_wait);
  515. INIT_LIST_HEAD(&cl->vtag_map);
  516. spin_lock_init(&cl->rd_completed_lock);
  517. INIT_LIST_HEAD(&cl->rd_completed);
  518. INIT_LIST_HEAD(&cl->rd_pending);
  519. INIT_LIST_HEAD(&cl->link);
  520. cl->writing_state = MEI_IDLE;
  521. cl->state = MEI_FILE_UNINITIALIZED;
  522. cl->dev = dev;
  523. }
  524. /**
  525. * mei_cl_allocate - allocates cl structure and sets it up.
  526. *
  527. * @dev: mei device
  528. * Return: The allocated file or NULL on failure
  529. */
  530. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  531. {
  532. struct mei_cl *cl;
  533. cl = kmalloc_obj(*cl);
  534. if (!cl)
  535. return NULL;
  536. mei_cl_init(cl, dev);
  537. return cl;
  538. }
  539. /**
  540. * mei_cl_link - allocate host id in the host map
  541. *
  542. * @cl: host client
  543. *
  544. * Return: 0 on success
  545. * -EINVAL on incorrect values
  546. * -EMFILE if open count exceeded.
  547. */
  548. int mei_cl_link(struct mei_cl *cl)
  549. {
  550. struct mei_device *dev;
  551. int id;
  552. if (WARN_ON(!cl || !cl->dev))
  553. return -EINVAL;
  554. dev = cl->dev;
  555. id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
  556. if (id >= MEI_CLIENTS_MAX) {
  557. dev_err(&dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  558. return -EMFILE;
  559. }
  560. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  561. dev_err(&dev->dev, "open_handle_count exceeded %d",
  562. MEI_MAX_OPEN_HANDLE_COUNT);
  563. return -EMFILE;
  564. }
  565. dev->open_handle_count++;
  566. cl->host_client_id = id;
  567. list_add_tail(&cl->link, &dev->file_list);
  568. set_bit(id, dev->host_clients_map);
  569. cl->state = MEI_FILE_INITIALIZING;
  570. cl_dbg(dev, cl, "link cl\n");
  571. return 0;
  572. }
  573. /**
  574. * mei_cl_unlink - remove host client from the list
  575. *
  576. * @cl: host client
  577. *
  578. * Return: always 0
  579. */
  580. int mei_cl_unlink(struct mei_cl *cl)
  581. {
  582. struct mei_device *dev;
  583. /* don't shout on error exit path */
  584. if (!cl)
  585. return 0;
  586. if (WARN_ON(!cl->dev))
  587. return 0;
  588. dev = cl->dev;
  589. cl_dbg(dev, cl, "unlink client");
  590. if (cl->state == MEI_FILE_UNINITIALIZED)
  591. return 0;
  592. if (dev->open_handle_count > 0)
  593. dev->open_handle_count--;
  594. /* never clear the 0 bit */
  595. if (cl->host_client_id)
  596. clear_bit(cl->host_client_id, dev->host_clients_map);
  597. list_del_init(&cl->link);
  598. cl->state = MEI_FILE_UNINITIALIZED;
  599. cl->writing_state = MEI_IDLE;
  600. WARN_ON(!list_empty(&cl->rd_completed) ||
  601. !list_empty(&cl->rd_pending) ||
  602. !list_empty(&cl->link));
  603. return 0;
  604. }
  605. void mei_host_client_init(struct mei_device *dev)
  606. {
  607. mei_set_devstate(dev, MEI_DEV_ENABLED);
  608. dev->reset_count = 0;
  609. schedule_work(&dev->bus_rescan_work);
  610. dev_dbg(&dev->dev, "rpm: autosuspend\n");
  611. pm_request_autosuspend(dev->parent);
  612. }
  613. /**
  614. * mei_hbuf_acquire - try to acquire host buffer
  615. *
  616. * @dev: the device structure
  617. * Return: true if host buffer was acquired
  618. */
  619. bool mei_hbuf_acquire(struct mei_device *dev)
  620. {
  621. if (mei_pg_state(dev) == MEI_PG_ON ||
  622. mei_pg_in_transition(dev)) {
  623. dev_dbg(&dev->dev, "device is in pg\n");
  624. return false;
  625. }
  626. if (!dev->hbuf_is_ready) {
  627. dev_dbg(&dev->dev, "hbuf is not ready\n");
  628. return false;
  629. }
  630. dev->hbuf_is_ready = false;
  631. return true;
  632. }
  633. /**
  634. * mei_cl_wake_all - wake up readers, writers and event waiters so
  635. * they can be interrupted
  636. *
  637. * @cl: host client
  638. */
  639. static void mei_cl_wake_all(struct mei_cl *cl)
  640. {
  641. struct mei_device *dev = cl->dev;
  642. /* synchronized under device mutex */
  643. if (waitqueue_active(&cl->rx_wait)) {
  644. cl_dbg(dev, cl, "Waking up reading client!\n");
  645. wake_up_interruptible(&cl->rx_wait);
  646. }
  647. /* synchronized under device mutex */
  648. if (waitqueue_active(&cl->tx_wait)) {
  649. cl_dbg(dev, cl, "Waking up writing client!\n");
  650. wake_up_interruptible(&cl->tx_wait);
  651. }
  652. /* synchronized under device mutex */
  653. if (waitqueue_active(&cl->ev_wait)) {
  654. cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
  655. wake_up_interruptible(&cl->ev_wait);
  656. }
  657. /* synchronized under device mutex */
  658. if (waitqueue_active(&cl->wait)) {
  659. cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
  660. wake_up(&cl->wait);
  661. }
  662. }
  663. /**
  664. * mei_cl_set_disconnected - set disconnected state and clear
  665. * associated states and resources
  666. *
  667. * @cl: host client
  668. */
  669. static void mei_cl_set_disconnected(struct mei_cl *cl)
  670. {
  671. struct mei_device *dev = cl->dev;
  672. if (cl->state == MEI_FILE_DISCONNECTED ||
  673. cl->state <= MEI_FILE_INITIALIZING)
  674. return;
  675. cl->state = MEI_FILE_DISCONNECTED;
  676. mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
  677. mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
  678. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  679. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  680. mei_cl_wake_all(cl);
  681. cl->rx_flow_ctrl_creds = 0;
  682. cl->tx_flow_ctrl_creds = 0;
  683. cl->timer_count = 0;
  684. if (!cl->me_cl)
  685. return;
  686. if (!WARN_ON(cl->me_cl->connect_count == 0))
  687. cl->me_cl->connect_count--;
  688. if (cl->me_cl->connect_count == 0)
  689. cl->me_cl->tx_flow_ctrl_creds = 0;
  690. mei_me_cl_put(cl->me_cl);
  691. cl->me_cl = NULL;
  692. }
  693. static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
  694. {
  695. if (!mei_me_cl_get(me_cl))
  696. return -ENOENT;
  697. /* only one connection is allowed for fixed address clients */
  698. if (me_cl->props.fixed_address) {
  699. if (me_cl->connect_count) {
  700. mei_me_cl_put(me_cl);
  701. return -EBUSY;
  702. }
  703. }
  704. cl->me_cl = me_cl;
  705. cl->state = MEI_FILE_CONNECTING;
  706. cl->me_cl->connect_count++;
  707. return 0;
  708. }
  709. /*
  710. * mei_cl_send_disconnect - send disconnect request
  711. *
  712. * @cl: host client
  713. * @cb: callback block
  714. *
  715. * Return: 0, OK; otherwise, error.
  716. */
  717. static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
  718. {
  719. struct mei_device *dev;
  720. int ret;
  721. dev = cl->dev;
  722. ret = mei_hbm_cl_disconnect_req(dev, cl);
  723. cl->status = ret;
  724. if (ret) {
  725. cl->state = MEI_FILE_DISCONNECT_REPLY;
  726. return ret;
  727. }
  728. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  729. cl->timer_count = dev->timeouts.connect;
  730. mei_schedule_stall_timer(dev);
  731. return 0;
  732. }
  733. /**
  734. * mei_cl_irq_disconnect - processes close related operation from
  735. * interrupt thread context - send disconnect request
  736. *
  737. * @cl: client
  738. * @cb: callback block.
  739. * @cmpl_list: complete list.
  740. *
  741. * Return: 0, OK; otherwise, error.
  742. */
  743. int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
  744. struct list_head *cmpl_list)
  745. {
  746. struct mei_device *dev = cl->dev;
  747. u32 msg_slots;
  748. int slots;
  749. int ret;
  750. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  751. slots = mei_hbuf_empty_slots(dev);
  752. if (slots < 0)
  753. return -EOVERFLOW;
  754. if ((u32)slots < msg_slots)
  755. return -EMSGSIZE;
  756. ret = mei_cl_send_disconnect(cl, cb);
  757. if (ret)
  758. list_move_tail(&cb->list, cmpl_list);
  759. return ret;
  760. }
  761. /**
  762. * __mei_cl_disconnect - disconnect host client from the me one
  763. * internal function runtime pm has to be already acquired
  764. *
  765. * @cl: host client
  766. *
  767. * Return: 0 on success, <0 on failure.
  768. */
  769. static int __mei_cl_disconnect(struct mei_cl *cl)
  770. {
  771. struct mei_device *dev;
  772. struct mei_cl_cb *cb;
  773. int rets;
  774. dev = cl->dev;
  775. cl->state = MEI_FILE_DISCONNECTING;
  776. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
  777. if (!cb) {
  778. rets = -ENOMEM;
  779. goto out;
  780. }
  781. if (mei_hbuf_acquire(dev)) {
  782. rets = mei_cl_send_disconnect(cl, cb);
  783. if (rets) {
  784. cl_err(dev, cl, "failed to disconnect.\n");
  785. goto out;
  786. }
  787. }
  788. mutex_unlock(&dev->device_lock);
  789. wait_event_timeout(cl->wait,
  790. cl->state == MEI_FILE_DISCONNECT_REPLY ||
  791. cl->state == MEI_FILE_DISCONNECTED,
  792. dev->timeouts.cl_connect);
  793. mutex_lock(&dev->device_lock);
  794. rets = cl->status;
  795. if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
  796. cl->state != MEI_FILE_DISCONNECTED) {
  797. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  798. rets = -ETIME;
  799. }
  800. out:
  801. /* we disconnect also on error */
  802. mei_cl_set_disconnected(cl);
  803. if (!rets)
  804. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  805. mei_io_cb_free(cb);
  806. return rets;
  807. }
  808. /**
  809. * mei_cl_disconnect - disconnect host client from the me one
  810. *
  811. * @cl: host client
  812. *
  813. * Locking: called under "dev->device_lock" lock
  814. *
  815. * Return: 0 on success, <0 on failure.
  816. */
  817. int mei_cl_disconnect(struct mei_cl *cl)
  818. {
  819. struct mei_device *dev;
  820. int rets;
  821. if (WARN_ON(!cl || !cl->dev))
  822. return -ENODEV;
  823. dev = cl->dev;
  824. cl_dbg(dev, cl, "disconnecting");
  825. if (!mei_cl_is_connected(cl))
  826. return 0;
  827. if (mei_cl_is_fixed_address(cl)) {
  828. mei_cl_set_disconnected(cl);
  829. return 0;
  830. }
  831. if (dev->dev_state == MEI_DEV_POWERING_DOWN ||
  832. dev->dev_state == MEI_DEV_POWER_DOWN) {
  833. cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
  834. mei_cl_set_disconnected(cl);
  835. return 0;
  836. }
  837. rets = pm_runtime_get(dev->parent);
  838. if (rets < 0 && rets != -EINPROGRESS) {
  839. pm_runtime_put_noidle(dev->parent);
  840. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  841. return rets;
  842. }
  843. rets = __mei_cl_disconnect(cl);
  844. cl_dbg(dev, cl, "rpm: autosuspend\n");
  845. pm_runtime_put_autosuspend(dev->parent);
  846. return rets;
  847. }
  848. /**
  849. * mei_cl_is_other_connecting - checks if other
  850. * client with the same me client id is connecting
  851. *
  852. * @cl: private data of the file object
  853. *
  854. * Return: true if other client is connected, false - otherwise.
  855. */
  856. static bool mei_cl_is_other_connecting(struct mei_cl *cl)
  857. {
  858. struct mei_device *dev;
  859. struct mei_cl_cb *cb;
  860. dev = cl->dev;
  861. list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
  862. if (cb->fop_type == MEI_FOP_CONNECT &&
  863. mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
  864. return true;
  865. }
  866. return false;
  867. }
  868. /**
  869. * mei_cl_send_connect - send connect request
  870. *
  871. * @cl: host client
  872. * @cb: callback block
  873. *
  874. * Return: 0, OK; otherwise, error.
  875. */
  876. static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
  877. {
  878. struct mei_device *dev;
  879. int ret;
  880. dev = cl->dev;
  881. ret = mei_hbm_cl_connect_req(dev, cl);
  882. cl->status = ret;
  883. if (ret) {
  884. cl->state = MEI_FILE_DISCONNECT_REPLY;
  885. return ret;
  886. }
  887. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  888. cl->timer_count = dev->timeouts.connect;
  889. mei_schedule_stall_timer(dev);
  890. return 0;
  891. }
  892. /**
  893. * mei_cl_irq_connect - send connect request in irq_thread context
  894. *
  895. * @cl: host client
  896. * @cb: callback block
  897. * @cmpl_list: complete list
  898. *
  899. * Return: 0, OK; otherwise, error.
  900. */
  901. int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
  902. struct list_head *cmpl_list)
  903. {
  904. struct mei_device *dev = cl->dev;
  905. u32 msg_slots;
  906. int slots;
  907. int rets;
  908. if (mei_cl_is_other_connecting(cl))
  909. return 0;
  910. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  911. slots = mei_hbuf_empty_slots(dev);
  912. if (slots < 0)
  913. return -EOVERFLOW;
  914. if ((u32)slots < msg_slots)
  915. return -EMSGSIZE;
  916. rets = mei_cl_send_connect(cl, cb);
  917. if (rets)
  918. list_move_tail(&cb->list, cmpl_list);
  919. return rets;
  920. }
  921. /**
  922. * mei_cl_connect - connect host client to the me one
  923. *
  924. * @cl: host client
  925. * @me_cl: me client
  926. * @fp: pointer to file structure
  927. *
  928. * Locking: called under "dev->device_lock" lock
  929. *
  930. * Return: 0 on success, <0 on failure.
  931. */
  932. int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
  933. const struct file *fp)
  934. {
  935. struct mei_device *dev;
  936. struct mei_cl_cb *cb;
  937. int rets;
  938. if (WARN_ON(!cl || !cl->dev || !me_cl))
  939. return -ENODEV;
  940. dev = cl->dev;
  941. rets = mei_cl_set_connecting(cl, me_cl);
  942. if (rets)
  943. goto nortpm;
  944. if (mei_cl_is_fixed_address(cl)) {
  945. cl->state = MEI_FILE_CONNECTED;
  946. rets = 0;
  947. goto nortpm;
  948. }
  949. rets = pm_runtime_get(dev->parent);
  950. if (rets < 0 && rets != -EINPROGRESS) {
  951. pm_runtime_put_noidle(dev->parent);
  952. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  953. goto nortpm;
  954. }
  955. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
  956. if (!cb) {
  957. rets = -ENOMEM;
  958. goto out;
  959. }
  960. /* run hbuf acquire last so we don't have to undo */
  961. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  962. rets = mei_cl_send_connect(cl, cb);
  963. if (rets)
  964. goto out;
  965. }
  966. mutex_unlock(&dev->device_lock);
  967. wait_event_timeout(cl->wait,
  968. (cl->state == MEI_FILE_CONNECTED ||
  969. cl->state == MEI_FILE_DISCONNECTED ||
  970. cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
  971. cl->state == MEI_FILE_DISCONNECT_REPLY),
  972. dev->timeouts.cl_connect);
  973. mutex_lock(&dev->device_lock);
  974. if (!mei_cl_is_connected(cl)) {
  975. if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
  976. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  977. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  978. /* ignore disconnect return valuue;
  979. * in case of failure reset will be invoked
  980. */
  981. __mei_cl_disconnect(cl);
  982. rets = -EFAULT;
  983. goto out;
  984. }
  985. /* timeout or something went really wrong */
  986. if (!cl->status)
  987. cl->status = -EFAULT;
  988. }
  989. rets = cl->status;
  990. out:
  991. cl_dbg(dev, cl, "rpm: autosuspend\n");
  992. pm_runtime_put_autosuspend(dev->parent);
  993. mei_io_cb_free(cb);
  994. nortpm:
  995. if (!mei_cl_is_connected(cl))
  996. mei_cl_set_disconnected(cl);
  997. return rets;
  998. }
  999. /**
  1000. * mei_cl_alloc_linked - allocate and link host client
  1001. *
  1002. * @dev: the device structure
  1003. *
  1004. * Return: cl on success ERR_PTR on failure
  1005. */
  1006. struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
  1007. {
  1008. struct mei_cl *cl;
  1009. int ret;
  1010. cl = mei_cl_allocate(dev);
  1011. if (!cl) {
  1012. ret = -ENOMEM;
  1013. goto err;
  1014. }
  1015. ret = mei_cl_link(cl);
  1016. if (ret)
  1017. goto err;
  1018. return cl;
  1019. err:
  1020. kfree(cl);
  1021. return ERR_PTR(ret);
  1022. }
  1023. /**
  1024. * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
  1025. *
  1026. * @cl: host client
  1027. *
  1028. * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
  1029. */
  1030. static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
  1031. {
  1032. if (WARN_ON(!cl || !cl->me_cl))
  1033. return -EINVAL;
  1034. if (cl->tx_flow_ctrl_creds > 0)
  1035. return 1;
  1036. if (mei_cl_is_fixed_address(cl))
  1037. return 1;
  1038. if (mei_cl_is_single_recv_buf(cl)) {
  1039. if (cl->me_cl->tx_flow_ctrl_creds > 0)
  1040. return 1;
  1041. }
  1042. return 0;
  1043. }
  1044. /**
  1045. * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
  1046. * for a client
  1047. *
  1048. * @cl: host client
  1049. *
  1050. * Return:
  1051. * 0 on success
  1052. * -EINVAL when ctrl credits are <= 0
  1053. */
  1054. static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
  1055. {
  1056. if (WARN_ON(!cl || !cl->me_cl))
  1057. return -EINVAL;
  1058. if (mei_cl_is_fixed_address(cl))
  1059. return 0;
  1060. if (mei_cl_is_single_recv_buf(cl)) {
  1061. if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
  1062. return -EINVAL;
  1063. cl->me_cl->tx_flow_ctrl_creds--;
  1064. } else {
  1065. if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
  1066. return -EINVAL;
  1067. cl->tx_flow_ctrl_creds--;
  1068. }
  1069. return 0;
  1070. }
  1071. /**
  1072. * mei_cl_vtag_alloc - allocate and fill the vtag structure
  1073. *
  1074. * @fp: pointer to file structure
  1075. * @vtag: vm tag
  1076. *
  1077. * Return:
  1078. * * Pointer to allocated struct - on success
  1079. * * ERR_PTR(-ENOMEM) on memory allocation failure
  1080. */
  1081. struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag)
  1082. {
  1083. struct mei_cl_vtag *cl_vtag;
  1084. cl_vtag = kzalloc_obj(*cl_vtag);
  1085. if (!cl_vtag)
  1086. return ERR_PTR(-ENOMEM);
  1087. INIT_LIST_HEAD(&cl_vtag->list);
  1088. cl_vtag->vtag = vtag;
  1089. cl_vtag->fp = fp;
  1090. return cl_vtag;
  1091. }
  1092. /**
  1093. * mei_cl_fp_by_vtag - obtain the file pointer by vtag
  1094. *
  1095. * @cl: host client
  1096. * @vtag: virtual tag
  1097. *
  1098. * Return:
  1099. * * A file pointer - on success
  1100. * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list
  1101. */
  1102. const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag)
  1103. {
  1104. struct mei_cl_vtag *vtag_l;
  1105. list_for_each_entry(vtag_l, &cl->vtag_map, list)
  1106. /* The client on bus has one fixed fp */
  1107. if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
  1108. vtag_l->vtag == vtag)
  1109. return vtag_l->fp;
  1110. return ERR_PTR(-ENOENT);
  1111. }
  1112. /**
  1113. * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag
  1114. *
  1115. * @cl: host client
  1116. * @vtag: vm tag
  1117. */
  1118. static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag)
  1119. {
  1120. struct mei_cl_vtag *vtag_l;
  1121. list_for_each_entry(vtag_l, &cl->vtag_map, list) {
  1122. /* The client on bus has one fixed vtag map */
  1123. if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
  1124. vtag_l->vtag == vtag) {
  1125. vtag_l->pending_read = false;
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. * mei_cl_read_vtag_add_fc - add flow control for next pending reader
  1132. * in the vtag list
  1133. *
  1134. * @cl: host client
  1135. */
  1136. static void mei_cl_read_vtag_add_fc(struct mei_cl *cl)
  1137. {
  1138. struct mei_cl_vtag *cl_vtag;
  1139. list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
  1140. if (cl_vtag->pending_read) {
  1141. if (mei_cl_enqueue_ctrl_wr_cb(cl,
  1142. mei_cl_mtu(cl),
  1143. MEI_FOP_READ,
  1144. cl_vtag->fp))
  1145. cl->rx_flow_ctrl_creds++;
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. /**
  1151. * mei_cl_vt_support_check - check if client support vtags
  1152. *
  1153. * @cl: host client
  1154. *
  1155. * Return:
  1156. * * 0 - supported, or not connected at all
  1157. * * -EOPNOTSUPP - vtags are not supported by client
  1158. */
  1159. int mei_cl_vt_support_check(const struct mei_cl *cl)
  1160. {
  1161. struct mei_device *dev = cl->dev;
  1162. if (!dev->hbm_f_vt_supported)
  1163. return -EOPNOTSUPP;
  1164. if (!cl->me_cl)
  1165. return 0;
  1166. return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
  1167. }
  1168. /**
  1169. * mei_cl_add_rd_completed - add read completed callback to list with lock
  1170. * and vtag check
  1171. *
  1172. * @cl: host client
  1173. * @cb: callback block
  1174. *
  1175. */
  1176. void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
  1177. {
  1178. const struct file *fp;
  1179. if (!mei_cl_vt_support_check(cl)) {
  1180. fp = mei_cl_fp_by_vtag(cl, cb->vtag);
  1181. if (IS_ERR(fp)) {
  1182. /* client already disconnected, discarding */
  1183. mei_io_cb_free(cb);
  1184. return;
  1185. }
  1186. cb->fp = fp;
  1187. mei_cl_reset_read_by_vtag(cl, cb->vtag);
  1188. mei_cl_read_vtag_add_fc(cl);
  1189. }
  1190. spin_lock(&cl->rd_completed_lock);
  1191. list_add_tail(&cb->list, &cl->rd_completed);
  1192. spin_unlock(&cl->rd_completed_lock);
  1193. }
  1194. /**
  1195. * mei_cl_del_rd_completed - free read completed callback with lock
  1196. *
  1197. * @cl: host client
  1198. * @cb: callback block
  1199. *
  1200. */
  1201. void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
  1202. {
  1203. spin_lock(&cl->rd_completed_lock);
  1204. mei_io_cb_free(cb);
  1205. spin_unlock(&cl->rd_completed_lock);
  1206. }
  1207. /**
  1208. * mei_cl_notify_fop2req - convert fop to proper request
  1209. *
  1210. * @fop: client notification start response command
  1211. *
  1212. * Return: MEI_HBM_NOTIFICATION_START/STOP
  1213. */
  1214. u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
  1215. {
  1216. if (fop == MEI_FOP_NOTIFY_START)
  1217. return MEI_HBM_NOTIFICATION_START;
  1218. else
  1219. return MEI_HBM_NOTIFICATION_STOP;
  1220. }
  1221. /**
  1222. * mei_cl_notify_req2fop - convert notification request top file operation type
  1223. *
  1224. * @req: hbm notification request type
  1225. *
  1226. * Return: MEI_FOP_NOTIFY_START/STOP
  1227. */
  1228. enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
  1229. {
  1230. if (req == MEI_HBM_NOTIFICATION_START)
  1231. return MEI_FOP_NOTIFY_START;
  1232. else
  1233. return MEI_FOP_NOTIFY_STOP;
  1234. }
  1235. /**
  1236. * mei_cl_irq_notify - send notification request in irq_thread context
  1237. *
  1238. * @cl: client
  1239. * @cb: callback block.
  1240. * @cmpl_list: complete list.
  1241. *
  1242. * Return: 0 on such and error otherwise.
  1243. */
  1244. int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
  1245. struct list_head *cmpl_list)
  1246. {
  1247. struct mei_device *dev = cl->dev;
  1248. u32 msg_slots;
  1249. int slots;
  1250. int ret;
  1251. bool request;
  1252. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  1253. slots = mei_hbuf_empty_slots(dev);
  1254. if (slots < 0)
  1255. return -EOVERFLOW;
  1256. if ((u32)slots < msg_slots)
  1257. return -EMSGSIZE;
  1258. request = mei_cl_notify_fop2req(cb->fop_type);
  1259. ret = mei_hbm_cl_notify_req(dev, cl, request);
  1260. if (ret) {
  1261. cl->status = ret;
  1262. list_move_tail(&cb->list, cmpl_list);
  1263. return ret;
  1264. }
  1265. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1266. return 0;
  1267. }
  1268. /**
  1269. * mei_cl_notify_request - send notification stop/start request
  1270. *
  1271. * @cl: host client
  1272. * @fp: associate request with file
  1273. * @request: 1 for start or 0 for stop
  1274. *
  1275. * Locking: called under "dev->device_lock" lock
  1276. *
  1277. * Return: 0 on such and error otherwise.
  1278. */
  1279. int mei_cl_notify_request(struct mei_cl *cl,
  1280. const struct file *fp, u8 request)
  1281. {
  1282. struct mei_device *dev;
  1283. struct mei_cl_cb *cb;
  1284. enum mei_cb_file_ops fop_type;
  1285. int rets;
  1286. if (WARN_ON(!cl || !cl->dev))
  1287. return -ENODEV;
  1288. dev = cl->dev;
  1289. if (!dev->hbm_f_ev_supported) {
  1290. cl_dbg(dev, cl, "notifications not supported\n");
  1291. return -EOPNOTSUPP;
  1292. }
  1293. if (!mei_cl_is_connected(cl))
  1294. return -ENODEV;
  1295. rets = pm_runtime_get(dev->parent);
  1296. if (rets < 0 && rets != -EINPROGRESS) {
  1297. pm_runtime_put_noidle(dev->parent);
  1298. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1299. return rets;
  1300. }
  1301. fop_type = mei_cl_notify_req2fop(request);
  1302. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
  1303. if (!cb) {
  1304. rets = -ENOMEM;
  1305. goto out;
  1306. }
  1307. if (mei_hbuf_acquire(dev)) {
  1308. if (mei_hbm_cl_notify_req(dev, cl, request)) {
  1309. rets = -ENODEV;
  1310. goto out;
  1311. }
  1312. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1313. }
  1314. mutex_unlock(&dev->device_lock);
  1315. wait_event_timeout(cl->wait,
  1316. cl->notify_en == request ||
  1317. cl->status ||
  1318. !mei_cl_is_connected(cl),
  1319. dev->timeouts.cl_connect);
  1320. mutex_lock(&dev->device_lock);
  1321. if (cl->notify_en != request && !cl->status)
  1322. cl->status = -EFAULT;
  1323. rets = cl->status;
  1324. out:
  1325. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1326. pm_runtime_put_autosuspend(dev->parent);
  1327. mei_io_cb_free(cb);
  1328. return rets;
  1329. }
  1330. /**
  1331. * mei_cl_notify - raise notification
  1332. *
  1333. * @cl: host client
  1334. *
  1335. * Locking: called under "dev->device_lock" lock
  1336. */
  1337. void mei_cl_notify(struct mei_cl *cl)
  1338. {
  1339. struct mei_device *dev;
  1340. if (!cl || !cl->dev)
  1341. return;
  1342. dev = cl->dev;
  1343. if (!cl->notify_en)
  1344. return;
  1345. cl_dbg(dev, cl, "notify event");
  1346. cl->notify_ev = true;
  1347. if (!mei_cl_bus_notify_event(cl))
  1348. wake_up_interruptible(&cl->ev_wait);
  1349. if (cl->ev_async)
  1350. kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
  1351. }
  1352. /**
  1353. * mei_cl_notify_get - get or wait for notification event
  1354. *
  1355. * @cl: host client
  1356. * @block: this request is blocking
  1357. * @notify_ev: true if notification event was received
  1358. *
  1359. * Locking: called under "dev->device_lock" lock
  1360. *
  1361. * Return: 0 on such and error otherwise.
  1362. */
  1363. int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
  1364. {
  1365. struct mei_device *dev;
  1366. int rets;
  1367. *notify_ev = false;
  1368. if (WARN_ON(!cl || !cl->dev))
  1369. return -ENODEV;
  1370. dev = cl->dev;
  1371. if (!dev->hbm_f_ev_supported) {
  1372. cl_dbg(dev, cl, "notifications not supported\n");
  1373. return -EOPNOTSUPP;
  1374. }
  1375. if (!mei_cl_is_connected(cl))
  1376. return -ENODEV;
  1377. if (cl->notify_ev)
  1378. goto out;
  1379. if (!block)
  1380. return -EAGAIN;
  1381. mutex_unlock(&dev->device_lock);
  1382. rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
  1383. mutex_lock(&dev->device_lock);
  1384. if (rets < 0)
  1385. return rets;
  1386. out:
  1387. *notify_ev = cl->notify_ev;
  1388. cl->notify_ev = false;
  1389. return 0;
  1390. }
  1391. /**
  1392. * mei_cl_read_start - the start read client message function.
  1393. *
  1394. * @cl: host client
  1395. * @length: number of bytes to read
  1396. * @fp: pointer to file structure
  1397. *
  1398. * Return: 0 on success, <0 on failure.
  1399. */
  1400. int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
  1401. {
  1402. struct mei_device *dev;
  1403. struct mei_cl_cb *cb;
  1404. int rets;
  1405. if (WARN_ON(!cl || !cl->dev))
  1406. return -ENODEV;
  1407. dev = cl->dev;
  1408. if (!mei_cl_is_connected(cl))
  1409. return -ENODEV;
  1410. if (!mei_me_cl_is_active(cl->me_cl)) {
  1411. cl_err(dev, cl, "no such me client\n");
  1412. return -ENOTTY;
  1413. }
  1414. if (mei_cl_is_fixed_address(cl))
  1415. return 0;
  1416. /* HW currently supports only one pending read */
  1417. if (cl->rx_flow_ctrl_creds) {
  1418. mei_cl_set_read_by_fp(cl, fp);
  1419. return -EBUSY;
  1420. }
  1421. cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
  1422. if (!cb)
  1423. return -ENOMEM;
  1424. mei_cl_set_read_by_fp(cl, fp);
  1425. rets = pm_runtime_get(dev->parent);
  1426. if (rets < 0 && rets != -EINPROGRESS) {
  1427. pm_runtime_put_noidle(dev->parent);
  1428. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1429. goto nortpm;
  1430. }
  1431. rets = 0;
  1432. if (mei_hbuf_acquire(dev)) {
  1433. rets = mei_hbm_cl_flow_control_req(dev, cl);
  1434. if (rets < 0)
  1435. goto out;
  1436. list_move_tail(&cb->list, &cl->rd_pending);
  1437. }
  1438. cl->rx_flow_ctrl_creds++;
  1439. out:
  1440. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1441. pm_runtime_put_autosuspend(dev->parent);
  1442. nortpm:
  1443. if (rets)
  1444. mei_io_cb_free(cb);
  1445. return rets;
  1446. }
  1447. static inline u8 mei_ext_hdr_set_vtag(void *ext, u8 vtag)
  1448. {
  1449. struct mei_ext_hdr_vtag *vtag_hdr = ext;
  1450. vtag_hdr->hdr.type = MEI_EXT_HDR_VTAG;
  1451. vtag_hdr->hdr.length = mei_data2slots(sizeof(*vtag_hdr));
  1452. vtag_hdr->vtag = vtag;
  1453. vtag_hdr->reserved = 0;
  1454. return vtag_hdr->hdr.length;
  1455. }
  1456. static inline bool mei_ext_hdr_is_gsc(struct mei_ext_hdr *ext)
  1457. {
  1458. return ext && ext->type == MEI_EXT_HDR_GSC;
  1459. }
  1460. static inline u8 mei_ext_hdr_set_gsc(struct mei_ext_hdr *ext, struct mei_ext_hdr *gsc_hdr)
  1461. {
  1462. memcpy(ext, gsc_hdr, mei_ext_hdr_len(gsc_hdr));
  1463. return ext->length;
  1464. }
  1465. /**
  1466. * mei_msg_hdr_init - allocate and initialize mei message header
  1467. *
  1468. * @cb: message callback structure
  1469. *
  1470. * Return: a pointer to initialized header or ERR_PTR on failure
  1471. */
  1472. static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb)
  1473. {
  1474. size_t hdr_len;
  1475. struct mei_ext_meta_hdr *meta;
  1476. struct mei_msg_hdr *mei_hdr;
  1477. bool is_ext, is_hbm, is_gsc, is_vtag;
  1478. struct mei_ext_hdr *next_ext;
  1479. if (!cb)
  1480. return ERR_PTR(-EINVAL);
  1481. /* Extended header for vtag is attached only on the first fragment */
  1482. is_vtag = (cb->vtag && cb->buf_idx == 0);
  1483. is_hbm = cb->cl->me_cl->client_id == 0;
  1484. is_gsc = ((!is_hbm) && cb->cl->dev->hbm_f_gsc_supported && mei_ext_hdr_is_gsc(cb->ext_hdr));
  1485. is_ext = is_vtag || is_gsc;
  1486. /* Compute extended header size */
  1487. hdr_len = sizeof(*mei_hdr);
  1488. if (!is_ext)
  1489. goto setup_hdr;
  1490. hdr_len += sizeof(*meta);
  1491. if (is_vtag)
  1492. hdr_len += sizeof(struct mei_ext_hdr_vtag);
  1493. if (is_gsc)
  1494. hdr_len += mei_ext_hdr_len(cb->ext_hdr);
  1495. setup_hdr:
  1496. mei_hdr = kzalloc(hdr_len, GFP_KERNEL);
  1497. if (!mei_hdr)
  1498. return ERR_PTR(-ENOMEM);
  1499. mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
  1500. mei_hdr->me_addr = mei_cl_me_id(cb->cl);
  1501. mei_hdr->internal = cb->internal;
  1502. mei_hdr->extended = is_ext;
  1503. if (!is_ext)
  1504. goto out;
  1505. meta = (struct mei_ext_meta_hdr *)mei_hdr->extension;
  1506. meta->size = 0;
  1507. next_ext = (struct mei_ext_hdr *)meta->hdrs;
  1508. if (is_vtag) {
  1509. meta->count++;
  1510. meta->size += mei_ext_hdr_set_vtag(next_ext, cb->vtag);
  1511. next_ext = mei_ext_next(next_ext);
  1512. }
  1513. if (is_gsc) {
  1514. meta->count++;
  1515. meta->size += mei_ext_hdr_set_gsc(next_ext, cb->ext_hdr);
  1516. next_ext = mei_ext_next(next_ext);
  1517. }
  1518. out:
  1519. mei_hdr->length = hdr_len - sizeof(*mei_hdr);
  1520. return mei_hdr;
  1521. }
  1522. /**
  1523. * mei_cl_irq_write - write a message to device
  1524. * from the interrupt thread context
  1525. *
  1526. * @cl: client
  1527. * @cb: callback block.
  1528. * @cmpl_list: complete list.
  1529. *
  1530. * Return: 0, OK; otherwise error.
  1531. */
  1532. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  1533. struct list_head *cmpl_list)
  1534. {
  1535. struct mei_device *dev;
  1536. struct mei_msg_data *buf;
  1537. struct mei_msg_hdr *mei_hdr = NULL;
  1538. size_t hdr_len;
  1539. size_t hbuf_len, dr_len;
  1540. size_t buf_len = 0;
  1541. size_t data_len;
  1542. int hbuf_slots;
  1543. u32 dr_slots;
  1544. u32 dma_len;
  1545. int rets;
  1546. bool first_chunk;
  1547. const void *data = NULL;
  1548. if (WARN_ON(!cl || !cl->dev))
  1549. return -ENODEV;
  1550. dev = cl->dev;
  1551. buf = &cb->buf;
  1552. first_chunk = cb->buf_idx == 0;
  1553. rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
  1554. if (rets < 0)
  1555. goto err;
  1556. if (rets == 0) {
  1557. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1558. return 0;
  1559. }
  1560. if (buf->data) {
  1561. buf_len = buf->size - cb->buf_idx;
  1562. data = buf->data + cb->buf_idx;
  1563. }
  1564. hbuf_slots = mei_hbuf_empty_slots(dev);
  1565. if (hbuf_slots < 0) {
  1566. rets = -EOVERFLOW;
  1567. goto err;
  1568. }
  1569. hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
  1570. dr_slots = mei_dma_ring_empty_slots(dev);
  1571. dr_len = mei_slots2data(dr_slots);
  1572. mei_hdr = mei_msg_hdr_init(cb);
  1573. if (IS_ERR(mei_hdr)) {
  1574. rets = PTR_ERR(mei_hdr);
  1575. mei_hdr = NULL;
  1576. goto err;
  1577. }
  1578. hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
  1579. /**
  1580. * Split the message only if we can write the whole host buffer
  1581. * otherwise wait for next time the host buffer is empty.
  1582. */
  1583. if (hdr_len + buf_len <= hbuf_len) {
  1584. data_len = buf_len;
  1585. mei_hdr->msg_complete = 1;
  1586. } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
  1587. mei_hdr->dma_ring = 1;
  1588. if (buf_len > dr_len)
  1589. buf_len = dr_len;
  1590. else
  1591. mei_hdr->msg_complete = 1;
  1592. data_len = sizeof(dma_len);
  1593. dma_len = buf_len;
  1594. data = &dma_len;
  1595. } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
  1596. buf_len = hbuf_len - hdr_len;
  1597. data_len = buf_len;
  1598. } else {
  1599. kfree(mei_hdr);
  1600. return 0;
  1601. }
  1602. mei_hdr->length += data_len;
  1603. if (mei_hdr->dma_ring && buf->data)
  1604. mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len);
  1605. rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
  1606. if (rets)
  1607. goto err;
  1608. cl->status = 0;
  1609. cl->writing_state = MEI_WRITING;
  1610. cb->buf_idx += buf_len;
  1611. if (first_chunk) {
  1612. if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
  1613. rets = -EIO;
  1614. goto err;
  1615. }
  1616. }
  1617. if (mei_hdr->msg_complete)
  1618. list_move_tail(&cb->list, &dev->write_waiting_list);
  1619. kfree(mei_hdr);
  1620. return 0;
  1621. err:
  1622. kfree(mei_hdr);
  1623. cl->status = rets;
  1624. list_move_tail(&cb->list, cmpl_list);
  1625. return rets;
  1626. }
  1627. /**
  1628. * mei_cl_write - submit a write cb to mei device
  1629. * assumes device_lock is locked
  1630. *
  1631. * @cl: host client
  1632. * @cb: write callback with filled data
  1633. * @timeout: send timeout in milliseconds.
  1634. * effective only for blocking writes: the cb->blocking is set.
  1635. * set timeout to the MAX_SCHEDULE_TIMEOUT to maixum allowed wait.
  1636. *
  1637. * Return: number of bytes sent on success, <0 on failure.
  1638. */
  1639. ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, unsigned long timeout)
  1640. {
  1641. struct mei_device *dev;
  1642. struct mei_msg_data *buf;
  1643. struct mei_msg_hdr *mei_hdr = NULL;
  1644. size_t hdr_len;
  1645. size_t hbuf_len, dr_len;
  1646. size_t buf_len;
  1647. size_t data_len;
  1648. int hbuf_slots;
  1649. u32 dr_slots;
  1650. u32 dma_len;
  1651. ssize_t rets;
  1652. bool blocking;
  1653. const void *data;
  1654. if (WARN_ON(!cl || !cl->dev))
  1655. return -ENODEV;
  1656. if (WARN_ON(!cb))
  1657. return -EINVAL;
  1658. dev = cl->dev;
  1659. buf = &cb->buf;
  1660. buf_len = buf->size;
  1661. cl_dbg(dev, cl, "buf_len=%zd\n", buf_len);
  1662. blocking = cb->blocking;
  1663. data = buf->data;
  1664. rets = pm_runtime_get(dev->parent);
  1665. if (rets < 0 && rets != -EINPROGRESS) {
  1666. pm_runtime_put_noidle(dev->parent);
  1667. cl_err(dev, cl, "rpm: get failed %zd\n", rets);
  1668. goto free;
  1669. }
  1670. cb->buf_idx = 0;
  1671. cl->writing_state = MEI_IDLE;
  1672. rets = mei_cl_tx_flow_ctrl_creds(cl);
  1673. if (rets < 0)
  1674. goto err;
  1675. mei_hdr = mei_msg_hdr_init(cb);
  1676. if (IS_ERR(mei_hdr)) {
  1677. rets = PTR_ERR(mei_hdr);
  1678. mei_hdr = NULL;
  1679. goto err;
  1680. }
  1681. hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
  1682. if (rets == 0) {
  1683. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1684. rets = buf_len;
  1685. goto out;
  1686. }
  1687. if (!mei_hbuf_acquire(dev)) {
  1688. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  1689. rets = buf_len;
  1690. goto out;
  1691. }
  1692. hbuf_slots = mei_hbuf_empty_slots(dev);
  1693. if (hbuf_slots < 0) {
  1694. buf_len = -EOVERFLOW;
  1695. goto out;
  1696. }
  1697. hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
  1698. dr_slots = mei_dma_ring_empty_slots(dev);
  1699. dr_len = mei_slots2data(dr_slots);
  1700. if (hdr_len + buf_len <= hbuf_len) {
  1701. data_len = buf_len;
  1702. mei_hdr->msg_complete = 1;
  1703. } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
  1704. mei_hdr->dma_ring = 1;
  1705. if (buf_len > dr_len)
  1706. buf_len = dr_len;
  1707. else
  1708. mei_hdr->msg_complete = 1;
  1709. data_len = sizeof(dma_len);
  1710. dma_len = buf_len;
  1711. data = &dma_len;
  1712. } else {
  1713. buf_len = hbuf_len - hdr_len;
  1714. data_len = buf_len;
  1715. }
  1716. mei_hdr->length += data_len;
  1717. if (mei_hdr->dma_ring && buf->data)
  1718. mei_dma_ring_write(dev, buf->data, buf_len);
  1719. rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
  1720. if (rets)
  1721. goto err;
  1722. rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
  1723. if (rets)
  1724. goto err;
  1725. cl->writing_state = MEI_WRITING;
  1726. cb->buf_idx = buf_len;
  1727. /* restore return value */
  1728. buf_len = buf->size;
  1729. out:
  1730. if (mei_hdr->msg_complete)
  1731. mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
  1732. else
  1733. mei_tx_cb_enqueue(cb, &dev->write_list);
  1734. cb = NULL;
  1735. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  1736. mutex_unlock(&dev->device_lock);
  1737. rets = wait_event_interruptible_timeout(cl->tx_wait,
  1738. cl->writing_state == MEI_WRITE_COMPLETE ||
  1739. (!mei_cl_is_connected(cl)),
  1740. msecs_to_jiffies(timeout));
  1741. mutex_lock(&dev->device_lock);
  1742. /* clean all queue on timeout as something fatal happened */
  1743. if (rets == 0) {
  1744. rets = -ETIME;
  1745. mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
  1746. mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
  1747. }
  1748. /* wait_event_interruptible returns -ERESTARTSYS */
  1749. if (rets > 0)
  1750. rets = 0;
  1751. if (rets) {
  1752. if (signal_pending(current))
  1753. rets = -EINTR;
  1754. goto err;
  1755. }
  1756. if (cl->writing_state != MEI_WRITE_COMPLETE) {
  1757. rets = -EFAULT;
  1758. goto err;
  1759. }
  1760. }
  1761. rets = buf_len;
  1762. err:
  1763. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1764. pm_runtime_put_autosuspend(dev->parent);
  1765. free:
  1766. mei_io_cb_free(cb);
  1767. kfree(mei_hdr);
  1768. return rets;
  1769. }
  1770. /**
  1771. * mei_cl_complete - processes completed operation for a client
  1772. *
  1773. * @cl: private data of the file object.
  1774. * @cb: callback block.
  1775. */
  1776. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  1777. {
  1778. struct mei_device *dev = cl->dev;
  1779. switch (cb->fop_type) {
  1780. case MEI_FOP_WRITE:
  1781. mei_tx_cb_dequeue(cb);
  1782. cl->writing_state = MEI_WRITE_COMPLETE;
  1783. if (waitqueue_active(&cl->tx_wait))
  1784. wake_up_interruptible(&cl->tx_wait);
  1785. else
  1786. pm_request_autosuspend(dev->parent);
  1787. break;
  1788. case MEI_FOP_READ:
  1789. mei_cl_add_rd_completed(cl, cb);
  1790. if (!mei_cl_is_fixed_address(cl) &&
  1791. !WARN_ON(!cl->rx_flow_ctrl_creds))
  1792. cl->rx_flow_ctrl_creds--;
  1793. if (!mei_cl_bus_rx_event(cl))
  1794. wake_up_interruptible(&cl->rx_wait);
  1795. break;
  1796. case MEI_FOP_CONNECT:
  1797. case MEI_FOP_DISCONNECT:
  1798. case MEI_FOP_NOTIFY_STOP:
  1799. case MEI_FOP_NOTIFY_START:
  1800. case MEI_FOP_DMA_MAP:
  1801. case MEI_FOP_DMA_UNMAP:
  1802. if (waitqueue_active(&cl->wait))
  1803. wake_up(&cl->wait);
  1804. break;
  1805. case MEI_FOP_DISCONNECT_RSP:
  1806. mei_io_cb_free(cb);
  1807. mei_cl_set_disconnected(cl);
  1808. break;
  1809. default:
  1810. BUG_ON(0);
  1811. }
  1812. }
  1813. /**
  1814. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  1815. *
  1816. * @dev: mei device
  1817. */
  1818. void mei_cl_all_disconnect(struct mei_device *dev)
  1819. {
  1820. struct mei_cl *cl;
  1821. list_for_each_entry(cl, &dev->file_list, link)
  1822. mei_cl_set_disconnected(cl);
  1823. }
  1824. EXPORT_SYMBOL_GPL(mei_cl_all_disconnect);
  1825. static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id)
  1826. {
  1827. struct mei_cl *cl;
  1828. list_for_each_entry(cl, &dev->file_list, link)
  1829. if (cl->dma.buffer_id == buffer_id)
  1830. return cl;
  1831. return NULL;
  1832. }
  1833. /**
  1834. * mei_cl_irq_dma_map - send client dma map request in irq_thread context
  1835. *
  1836. * @cl: client
  1837. * @cb: callback block.
  1838. * @cmpl_list: complete list.
  1839. *
  1840. * Return: 0 on such and error otherwise.
  1841. */
  1842. int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb,
  1843. struct list_head *cmpl_list)
  1844. {
  1845. struct mei_device *dev = cl->dev;
  1846. u32 msg_slots;
  1847. int slots;
  1848. int ret;
  1849. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request));
  1850. slots = mei_hbuf_empty_slots(dev);
  1851. if (slots < 0)
  1852. return -EOVERFLOW;
  1853. if ((u32)slots < msg_slots)
  1854. return -EMSGSIZE;
  1855. ret = mei_hbm_cl_dma_map_req(dev, cl);
  1856. if (ret) {
  1857. cl->status = ret;
  1858. list_move_tail(&cb->list, cmpl_list);
  1859. return ret;
  1860. }
  1861. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1862. return 0;
  1863. }
  1864. /**
  1865. * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context
  1866. *
  1867. * @cl: client
  1868. * @cb: callback block.
  1869. * @cmpl_list: complete list.
  1870. *
  1871. * Return: 0 on such and error otherwise.
  1872. */
  1873. int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb,
  1874. struct list_head *cmpl_list)
  1875. {
  1876. struct mei_device *dev = cl->dev;
  1877. u32 msg_slots;
  1878. int slots;
  1879. int ret;
  1880. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request));
  1881. slots = mei_hbuf_empty_slots(dev);
  1882. if (slots < 0)
  1883. return -EOVERFLOW;
  1884. if ((u32)slots < msg_slots)
  1885. return -EMSGSIZE;
  1886. ret = mei_hbm_cl_dma_unmap_req(dev, cl);
  1887. if (ret) {
  1888. cl->status = ret;
  1889. list_move_tail(&cb->list, cmpl_list);
  1890. return ret;
  1891. }
  1892. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1893. return 0;
  1894. }
  1895. static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size)
  1896. {
  1897. cl->dma.vaddr = dmam_alloc_coherent(&cl->dev->dev, size,
  1898. &cl->dma.daddr, GFP_KERNEL);
  1899. if (!cl->dma.vaddr)
  1900. return -ENOMEM;
  1901. cl->dma.buffer_id = buf_id;
  1902. cl->dma.size = size;
  1903. return 0;
  1904. }
  1905. static void mei_cl_dma_free(struct mei_cl *cl)
  1906. {
  1907. cl->dma.buffer_id = 0;
  1908. dmam_free_coherent(&cl->dev->dev,
  1909. cl->dma.size, cl->dma.vaddr, cl->dma.daddr);
  1910. cl->dma.size = 0;
  1911. cl->dma.vaddr = NULL;
  1912. cl->dma.daddr = 0;
  1913. }
  1914. /**
  1915. * mei_cl_dma_alloc_and_map - send client dma map request
  1916. *
  1917. * @cl: host client
  1918. * @fp: pointer to file structure
  1919. * @buffer_id: id of the mapped buffer
  1920. * @size: size of the buffer
  1921. *
  1922. * Locking: called under "dev->device_lock" lock
  1923. *
  1924. * Return:
  1925. * * -ENODEV
  1926. * * -EINVAL
  1927. * * -EOPNOTSUPP
  1928. * * -EPROTO
  1929. * * -ENOMEM;
  1930. */
  1931. int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp,
  1932. u8 buffer_id, size_t size)
  1933. {
  1934. struct mei_device *dev;
  1935. struct mei_cl_cb *cb;
  1936. int rets;
  1937. if (WARN_ON(!cl || !cl->dev))
  1938. return -ENODEV;
  1939. dev = cl->dev;
  1940. if (!dev->hbm_f_cd_supported) {
  1941. cl_dbg(dev, cl, "client dma is not supported\n");
  1942. return -EOPNOTSUPP;
  1943. }
  1944. if (buffer_id == 0)
  1945. return -EINVAL;
  1946. if (mei_cl_is_connected(cl))
  1947. return -EPROTO;
  1948. if (cl->dma_mapped)
  1949. return -EPROTO;
  1950. if (mei_cl_dma_map_find(dev, buffer_id)) {
  1951. cl_dbg(dev, cl, "client dma with id %d is already allocated\n",
  1952. cl->dma.buffer_id);
  1953. return -EPROTO;
  1954. }
  1955. rets = pm_runtime_get(dev->parent);
  1956. if (rets < 0 && rets != -EINPROGRESS) {
  1957. pm_runtime_put_noidle(dev->parent);
  1958. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1959. return rets;
  1960. }
  1961. rets = mei_cl_dma_alloc(cl, buffer_id, size);
  1962. if (rets) {
  1963. pm_runtime_put_noidle(dev->parent);
  1964. return rets;
  1965. }
  1966. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp);
  1967. if (!cb) {
  1968. rets = -ENOMEM;
  1969. goto out;
  1970. }
  1971. if (mei_hbuf_acquire(dev)) {
  1972. if (mei_hbm_cl_dma_map_req(dev, cl)) {
  1973. rets = -ENODEV;
  1974. goto out;
  1975. }
  1976. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1977. }
  1978. cl->status = 0;
  1979. mutex_unlock(&dev->device_lock);
  1980. wait_event_timeout(cl->wait,
  1981. cl->dma_mapped || cl->status,
  1982. dev->timeouts.cl_connect);
  1983. mutex_lock(&dev->device_lock);
  1984. if (!cl->dma_mapped && !cl->status)
  1985. cl->status = -EFAULT;
  1986. rets = cl->status;
  1987. out:
  1988. if (rets)
  1989. mei_cl_dma_free(cl);
  1990. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1991. pm_runtime_put_autosuspend(dev->parent);
  1992. mei_io_cb_free(cb);
  1993. return rets;
  1994. }
  1995. /**
  1996. * mei_cl_dma_unmap - send client dma unmap request
  1997. *
  1998. * @cl: host client
  1999. * @fp: pointer to file structure
  2000. *
  2001. * Locking: called under "dev->device_lock" lock
  2002. *
  2003. * Return: 0 on such and error otherwise.
  2004. */
  2005. int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp)
  2006. {
  2007. struct mei_device *dev;
  2008. struct mei_cl_cb *cb;
  2009. int rets;
  2010. if (WARN_ON(!cl || !cl->dev))
  2011. return -ENODEV;
  2012. dev = cl->dev;
  2013. if (!dev->hbm_f_cd_supported) {
  2014. cl_dbg(dev, cl, "client dma is not supported\n");
  2015. return -EOPNOTSUPP;
  2016. }
  2017. /* do not allow unmap for connected client */
  2018. if (mei_cl_is_connected(cl))
  2019. return -EPROTO;
  2020. if (!cl->dma_mapped)
  2021. return -EPROTO;
  2022. rets = pm_runtime_get(dev->parent);
  2023. if (rets < 0 && rets != -EINPROGRESS) {
  2024. pm_runtime_put_noidle(dev->parent);
  2025. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  2026. return rets;
  2027. }
  2028. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp);
  2029. if (!cb) {
  2030. rets = -ENOMEM;
  2031. goto out;
  2032. }
  2033. if (mei_hbuf_acquire(dev)) {
  2034. if (mei_hbm_cl_dma_unmap_req(dev, cl)) {
  2035. rets = -ENODEV;
  2036. goto out;
  2037. }
  2038. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  2039. }
  2040. cl->status = 0;
  2041. mutex_unlock(&dev->device_lock);
  2042. wait_event_timeout(cl->wait,
  2043. !cl->dma_mapped || cl->status,
  2044. dev->timeouts.cl_connect);
  2045. mutex_lock(&dev->device_lock);
  2046. if (cl->dma_mapped && !cl->status)
  2047. cl->status = -EFAULT;
  2048. rets = cl->status;
  2049. if (!rets)
  2050. mei_cl_dma_free(cl);
  2051. out:
  2052. cl_dbg(dev, cl, "rpm: autosuspend\n");
  2053. pm_runtime_put_autosuspend(dev->parent);
  2054. mei_io_cb_free(cb);
  2055. return rets;
  2056. }