client.c 52 KB

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