client.c 50 KB

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