client.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/sched/signal.h>
  17. #include <linux/wait.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. /**
  26. * mei_me_cl_init - initialize me client
  27. *
  28. * @me_cl: me client
  29. */
  30. void mei_me_cl_init(struct mei_me_client *me_cl)
  31. {
  32. INIT_LIST_HEAD(&me_cl->list);
  33. kref_init(&me_cl->refcnt);
  34. }
  35. /**
  36. * mei_me_cl_get - increases me client refcount
  37. *
  38. * @me_cl: me client
  39. *
  40. * Locking: called under "dev->device_lock" lock
  41. *
  42. * Return: me client or NULL
  43. */
  44. struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
  45. {
  46. if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
  47. return me_cl;
  48. return NULL;
  49. }
  50. /**
  51. * mei_me_cl_release - free me client
  52. *
  53. * Locking: called under "dev->device_lock" lock
  54. *
  55. * @ref: me_client refcount
  56. */
  57. static void mei_me_cl_release(struct kref *ref)
  58. {
  59. struct mei_me_client *me_cl =
  60. container_of(ref, struct mei_me_client, refcnt);
  61. kfree(me_cl);
  62. }
  63. /**
  64. * mei_me_cl_put - decrease me client refcount and free client if necessary
  65. *
  66. * Locking: called under "dev->device_lock" lock
  67. *
  68. * @me_cl: me client
  69. */
  70. void mei_me_cl_put(struct mei_me_client *me_cl)
  71. {
  72. if (me_cl)
  73. kref_put(&me_cl->refcnt, mei_me_cl_release);
  74. }
  75. /**
  76. * __mei_me_cl_del - delete me client from the list and decrease
  77. * reference counter
  78. *
  79. * @dev: mei device
  80. * @me_cl: me client
  81. *
  82. * Locking: dev->me_clients_rwsem
  83. */
  84. static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  85. {
  86. if (!me_cl)
  87. return;
  88. list_del_init(&me_cl->list);
  89. mei_me_cl_put(me_cl);
  90. }
  91. /**
  92. * mei_me_cl_del - delete me client from the list and decrease
  93. * reference counter
  94. *
  95. * @dev: mei device
  96. * @me_cl: me client
  97. */
  98. void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
  99. {
  100. down_write(&dev->me_clients_rwsem);
  101. __mei_me_cl_del(dev, me_cl);
  102. up_write(&dev->me_clients_rwsem);
  103. }
  104. /**
  105. * mei_me_cl_add - add me client to the list
  106. *
  107. * @dev: mei device
  108. * @me_cl: me client
  109. */
  110. void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
  111. {
  112. down_write(&dev->me_clients_rwsem);
  113. list_add(&me_cl->list, &dev->me_clients);
  114. up_write(&dev->me_clients_rwsem);
  115. }
  116. /**
  117. * __mei_me_cl_by_uuid - locate me client by uuid
  118. * increases ref count
  119. *
  120. * @dev: mei device
  121. * @uuid: me client uuid
  122. *
  123. * Return: me client or NULL if not found
  124. *
  125. * Locking: dev->me_clients_rwsem
  126. */
  127. static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
  128. const uuid_le *uuid)
  129. {
  130. struct mei_me_client *me_cl;
  131. const uuid_le *pn;
  132. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  133. list_for_each_entry(me_cl, &dev->me_clients, list) {
  134. pn = &me_cl->props.protocol_name;
  135. if (uuid_le_cmp(*uuid, *pn) == 0)
  136. return mei_me_cl_get(me_cl);
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * mei_me_cl_by_uuid - locate me client by uuid
  142. * increases ref count
  143. *
  144. * @dev: mei device
  145. * @uuid: me client uuid
  146. *
  147. * Return: me client or NULL if not found
  148. *
  149. * Locking: dev->me_clients_rwsem
  150. */
  151. struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
  152. const uuid_le *uuid)
  153. {
  154. struct mei_me_client *me_cl;
  155. down_read(&dev->me_clients_rwsem);
  156. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  157. up_read(&dev->me_clients_rwsem);
  158. return me_cl;
  159. }
  160. /**
  161. * mei_me_cl_by_id - locate me client by client id
  162. * increases ref count
  163. *
  164. * @dev: the device structure
  165. * @client_id: me client id
  166. *
  167. * Return: me client or NULL if not found
  168. *
  169. * Locking: dev->me_clients_rwsem
  170. */
  171. struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  172. {
  173. struct mei_me_client *__me_cl, *me_cl = NULL;
  174. down_read(&dev->me_clients_rwsem);
  175. list_for_each_entry(__me_cl, &dev->me_clients, list) {
  176. if (__me_cl->client_id == client_id) {
  177. me_cl = mei_me_cl_get(__me_cl);
  178. break;
  179. }
  180. }
  181. up_read(&dev->me_clients_rwsem);
  182. return me_cl;
  183. }
  184. /**
  185. * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
  186. * increases ref count
  187. *
  188. * @dev: the device structure
  189. * @uuid: me client uuid
  190. * @client_id: me client id
  191. *
  192. * Return: me client or null if not found
  193. *
  194. * Locking: dev->me_clients_rwsem
  195. */
  196. static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
  197. const uuid_le *uuid, u8 client_id)
  198. {
  199. struct mei_me_client *me_cl;
  200. const uuid_le *pn;
  201. WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
  202. list_for_each_entry(me_cl, &dev->me_clients, list) {
  203. pn = &me_cl->props.protocol_name;
  204. if (uuid_le_cmp(*uuid, *pn) == 0 &&
  205. me_cl->client_id == client_id)
  206. return mei_me_cl_get(me_cl);
  207. }
  208. return NULL;
  209. }
  210. /**
  211. * mei_me_cl_by_uuid_id - locate me client by client id and uuid
  212. * increases ref count
  213. *
  214. * @dev: the device structure
  215. * @uuid: me client uuid
  216. * @client_id: me client id
  217. *
  218. * Return: me client or null if not found
  219. */
  220. struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
  221. const uuid_le *uuid, u8 client_id)
  222. {
  223. struct mei_me_client *me_cl;
  224. down_read(&dev->me_clients_rwsem);
  225. me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
  226. up_read(&dev->me_clients_rwsem);
  227. return me_cl;
  228. }
  229. /**
  230. * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
  231. *
  232. * @dev: the device structure
  233. * @uuid: me client uuid
  234. *
  235. * Locking: called under "dev->device_lock" lock
  236. */
  237. void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
  238. {
  239. struct mei_me_client *me_cl;
  240. dev_dbg(dev->dev, "remove %pUl\n", uuid);
  241. down_write(&dev->me_clients_rwsem);
  242. me_cl = __mei_me_cl_by_uuid(dev, uuid);
  243. __mei_me_cl_del(dev, me_cl);
  244. mei_me_cl_put(me_cl);
  245. up_write(&dev->me_clients_rwsem);
  246. }
  247. /**
  248. * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
  249. *
  250. * @dev: the device structure
  251. * @uuid: me client uuid
  252. * @id: me client id
  253. *
  254. * Locking: called under "dev->device_lock" lock
  255. */
  256. void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
  257. {
  258. struct mei_me_client *me_cl;
  259. dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
  260. down_write(&dev->me_clients_rwsem);
  261. me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
  262. __mei_me_cl_del(dev, me_cl);
  263. mei_me_cl_put(me_cl);
  264. up_write(&dev->me_clients_rwsem);
  265. }
  266. /**
  267. * mei_me_cl_rm_all - remove all me clients
  268. *
  269. * @dev: the device structure
  270. *
  271. * Locking: called under "dev->device_lock" lock
  272. */
  273. void mei_me_cl_rm_all(struct mei_device *dev)
  274. {
  275. struct mei_me_client *me_cl, *next;
  276. down_write(&dev->me_clients_rwsem);
  277. list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
  278. __mei_me_cl_del(dev, me_cl);
  279. up_write(&dev->me_clients_rwsem);
  280. }
  281. /**
  282. * mei_cl_cmp_id - tells if the clients are the same
  283. *
  284. * @cl1: host client 1
  285. * @cl2: host client 2
  286. *
  287. * Return: true - if the clients has same host and me ids
  288. * false - otherwise
  289. */
  290. static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
  291. const struct mei_cl *cl2)
  292. {
  293. return cl1 && cl2 &&
  294. (cl1->host_client_id == cl2->host_client_id) &&
  295. (mei_cl_me_id(cl1) == mei_cl_me_id(cl2));
  296. }
  297. /**
  298. * mei_io_cb_free - free mei_cb_private related memory
  299. *
  300. * @cb: mei callback struct
  301. */
  302. void mei_io_cb_free(struct mei_cl_cb *cb)
  303. {
  304. if (cb == NULL)
  305. return;
  306. list_del(&cb->list);
  307. kfree(cb->buf.data);
  308. kfree(cb);
  309. }
  310. /**
  311. * mei_tx_cb_queue - queue tx callback
  312. *
  313. * Locking: called under "dev->device_lock" lock
  314. *
  315. * @cb: mei callback struct
  316. * @head: an instance of list to queue on
  317. */
  318. static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
  319. struct list_head *head)
  320. {
  321. list_add_tail(&cb->list, head);
  322. cb->cl->tx_cb_queued++;
  323. }
  324. /**
  325. * mei_tx_cb_dequeue - dequeue tx callback
  326. *
  327. * Locking: called under "dev->device_lock" lock
  328. *
  329. * @cb: mei callback struct to dequeue and free
  330. */
  331. static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
  332. {
  333. if (!WARN_ON(cb->cl->tx_cb_queued == 0))
  334. cb->cl->tx_cb_queued--;
  335. mei_io_cb_free(cb);
  336. }
  337. /**
  338. * mei_io_cb_init - allocate and initialize io callback
  339. *
  340. * @cl: mei client
  341. * @type: operation type
  342. * @fp: pointer to file structure
  343. *
  344. * Return: mei_cl_cb pointer or NULL;
  345. */
  346. static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
  347. enum mei_cb_file_ops type,
  348. const struct file *fp)
  349. {
  350. struct mei_cl_cb *cb;
  351. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  352. if (!cb)
  353. return NULL;
  354. INIT_LIST_HEAD(&cb->list);
  355. cb->fp = fp;
  356. cb->cl = cl;
  357. cb->buf_idx = 0;
  358. cb->fop_type = type;
  359. return cb;
  360. }
  361. /**
  362. * mei_io_list_flush_cl - removes cbs belonging to the cl.
  363. *
  364. * @head: an instance of our list structure
  365. * @cl: host client
  366. */
  367. static void mei_io_list_flush_cl(struct list_head *head,
  368. const struct mei_cl *cl)
  369. {
  370. struct mei_cl_cb *cb, *next;
  371. list_for_each_entry_safe(cb, next, head, list) {
  372. if (mei_cl_cmp_id(cl, cb->cl))
  373. list_del_init(&cb->list);
  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. */
  382. static void mei_io_tx_list_free_cl(struct list_head *head,
  383. const struct mei_cl *cl)
  384. {
  385. struct mei_cl_cb *cb, *next;
  386. list_for_each_entry_safe(cb, next, head, list) {
  387. if (mei_cl_cmp_id(cl, cb->cl))
  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_alloc_cb - a convenient wrapper for allocating read cb
  406. *
  407. * @cl: host client
  408. * @length: size of the buffer
  409. * @fop_type: operation type
  410. * @fp: associated file pointer (might be NULL)
  411. *
  412. * Return: cb on success and NULL on failure
  413. */
  414. struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
  415. enum mei_cb_file_ops fop_type,
  416. const struct file *fp)
  417. {
  418. struct mei_cl_cb *cb;
  419. cb = mei_io_cb_init(cl, fop_type, fp);
  420. if (!cb)
  421. return NULL;
  422. if (length == 0)
  423. return cb;
  424. cb->buf.data = kmalloc(length, GFP_KERNEL);
  425. if (!cb->buf.data) {
  426. mei_io_cb_free(cb);
  427. return NULL;
  428. }
  429. cb->buf.size = length;
  430. return cb;
  431. }
  432. /**
  433. * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
  434. * and enqueuing of the control commands cb
  435. *
  436. * @cl: host client
  437. * @length: size of the buffer
  438. * @fop_type: operation type
  439. * @fp: associated file pointer (might be NULL)
  440. *
  441. * Return: cb on success and NULL on failure
  442. * Locking: called under "dev->device_lock" lock
  443. */
  444. struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
  445. enum mei_cb_file_ops fop_type,
  446. const struct file *fp)
  447. {
  448. struct mei_cl_cb *cb;
  449. /* for RX always allocate at least client's mtu */
  450. if (length)
  451. length = max_t(size_t, length, mei_cl_mtu(cl));
  452. cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
  453. if (!cb)
  454. return NULL;
  455. list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
  456. return cb;
  457. }
  458. /**
  459. * mei_cl_read_cb - find this cl's callback in the read list
  460. * for a specific file
  461. *
  462. * @cl: host client
  463. * @fp: file pointer (matching cb file object), may be NULL
  464. *
  465. * Return: cb on success, NULL if cb is not found
  466. */
  467. struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
  468. {
  469. struct mei_cl_cb *cb;
  470. list_for_each_entry(cb, &cl->rd_completed, list)
  471. if (!fp || fp == cb->fp)
  472. return cb;
  473. return NULL;
  474. }
  475. /**
  476. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  477. *
  478. * @cl: host client
  479. * @fp: file pointer (matching cb file object), may be NULL
  480. *
  481. * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
  482. */
  483. int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
  484. {
  485. struct mei_device *dev;
  486. if (WARN_ON(!cl || !cl->dev))
  487. return -EINVAL;
  488. dev = cl->dev;
  489. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  490. mei_io_tx_list_free_cl(&cl->dev->write_list, cl);
  491. mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl);
  492. mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
  493. mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
  494. mei_io_list_free_fp(&cl->rd_pending, fp);
  495. mei_io_list_free_fp(&cl->rd_completed, fp);
  496. return 0;
  497. }
  498. /**
  499. * mei_cl_init - initializes cl.
  500. *
  501. * @cl: host client to be initialized
  502. * @dev: mei device
  503. */
  504. static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  505. {
  506. memset(cl, 0, sizeof(struct mei_cl));
  507. init_waitqueue_head(&cl->wait);
  508. init_waitqueue_head(&cl->rx_wait);
  509. init_waitqueue_head(&cl->tx_wait);
  510. init_waitqueue_head(&cl->ev_wait);
  511. INIT_LIST_HEAD(&cl->rd_completed);
  512. INIT_LIST_HEAD(&cl->rd_pending);
  513. INIT_LIST_HEAD(&cl->link);
  514. cl->writing_state = MEI_IDLE;
  515. cl->state = MEI_FILE_UNINITIALIZED;
  516. cl->dev = dev;
  517. }
  518. /**
  519. * mei_cl_allocate - allocates cl structure and sets it up.
  520. *
  521. * @dev: mei device
  522. * Return: The allocated file or NULL on failure
  523. */
  524. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  525. {
  526. struct mei_cl *cl;
  527. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  528. if (!cl)
  529. return NULL;
  530. mei_cl_init(cl, dev);
  531. return cl;
  532. }
  533. /**
  534. * mei_cl_link - allocate host id in the host map
  535. *
  536. * @cl: host client
  537. *
  538. * Return: 0 on success
  539. * -EINVAL on incorrect values
  540. * -EMFILE if open count exceeded.
  541. */
  542. int mei_cl_link(struct mei_cl *cl)
  543. {
  544. struct mei_device *dev;
  545. int id;
  546. if (WARN_ON(!cl || !cl->dev))
  547. return -EINVAL;
  548. dev = cl->dev;
  549. id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
  550. if (id >= MEI_CLIENTS_MAX) {
  551. dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  552. return -EMFILE;
  553. }
  554. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  555. dev_err(dev->dev, "open_handle_count exceeded %d",
  556. MEI_MAX_OPEN_HANDLE_COUNT);
  557. return -EMFILE;
  558. }
  559. dev->open_handle_count++;
  560. cl->host_client_id = id;
  561. list_add_tail(&cl->link, &dev->file_list);
  562. set_bit(id, dev->host_clients_map);
  563. cl->state = MEI_FILE_INITIALIZING;
  564. cl_dbg(dev, cl, "link cl\n");
  565. return 0;
  566. }
  567. /**
  568. * mei_cl_unlink - remove host client from the list
  569. *
  570. * @cl: host client
  571. *
  572. * Return: always 0
  573. */
  574. int mei_cl_unlink(struct mei_cl *cl)
  575. {
  576. struct mei_device *dev;
  577. /* don't shout on error exit path */
  578. if (!cl)
  579. return 0;
  580. if (WARN_ON(!cl->dev))
  581. return 0;
  582. dev = cl->dev;
  583. cl_dbg(dev, cl, "unlink client");
  584. if (dev->open_handle_count > 0)
  585. dev->open_handle_count--;
  586. /* never clear the 0 bit */
  587. if (cl->host_client_id)
  588. clear_bit(cl->host_client_id, dev->host_clients_map);
  589. list_del_init(&cl->link);
  590. cl->state = MEI_FILE_UNINITIALIZED;
  591. cl->writing_state = MEI_IDLE;
  592. WARN_ON(!list_empty(&cl->rd_completed) ||
  593. !list_empty(&cl->rd_pending) ||
  594. !list_empty(&cl->link));
  595. return 0;
  596. }
  597. void mei_host_client_init(struct mei_device *dev)
  598. {
  599. dev->dev_state = MEI_DEV_ENABLED;
  600. dev->reset_count = 0;
  601. schedule_work(&dev->bus_rescan_work);
  602. pm_runtime_mark_last_busy(dev->dev);
  603. dev_dbg(dev->dev, "rpm: autosuspend\n");
  604. pm_request_autosuspend(dev->dev);
  605. }
  606. /**
  607. * mei_hbuf_acquire - try to acquire host buffer
  608. *
  609. * @dev: the device structure
  610. * Return: true if host buffer was acquired
  611. */
  612. bool mei_hbuf_acquire(struct mei_device *dev)
  613. {
  614. if (mei_pg_state(dev) == MEI_PG_ON ||
  615. mei_pg_in_transition(dev)) {
  616. dev_dbg(dev->dev, "device is in pg\n");
  617. return false;
  618. }
  619. if (!dev->hbuf_is_ready) {
  620. dev_dbg(dev->dev, "hbuf is not ready\n");
  621. return false;
  622. }
  623. dev->hbuf_is_ready = false;
  624. return true;
  625. }
  626. /**
  627. * mei_cl_wake_all - wake up readers, writers and event waiters so
  628. * they can be interrupted
  629. *
  630. * @cl: host client
  631. */
  632. static void mei_cl_wake_all(struct mei_cl *cl)
  633. {
  634. struct mei_device *dev = cl->dev;
  635. /* synchronized under device mutex */
  636. if (waitqueue_active(&cl->rx_wait)) {
  637. cl_dbg(dev, cl, "Waking up reading client!\n");
  638. wake_up_interruptible(&cl->rx_wait);
  639. }
  640. /* synchronized under device mutex */
  641. if (waitqueue_active(&cl->tx_wait)) {
  642. cl_dbg(dev, cl, "Waking up writing client!\n");
  643. wake_up_interruptible(&cl->tx_wait);
  644. }
  645. /* synchronized under device mutex */
  646. if (waitqueue_active(&cl->ev_wait)) {
  647. cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
  648. wake_up_interruptible(&cl->ev_wait);
  649. }
  650. /* synchronized under device mutex */
  651. if (waitqueue_active(&cl->wait)) {
  652. cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
  653. wake_up(&cl->wait);
  654. }
  655. }
  656. /**
  657. * mei_cl_set_disconnected - set disconnected state and clear
  658. * associated states and resources
  659. *
  660. * @cl: host client
  661. */
  662. static void mei_cl_set_disconnected(struct mei_cl *cl)
  663. {
  664. struct mei_device *dev = cl->dev;
  665. if (cl->state == MEI_FILE_DISCONNECTED ||
  666. cl->state <= MEI_FILE_INITIALIZING)
  667. return;
  668. cl->state = MEI_FILE_DISCONNECTED;
  669. mei_io_tx_list_free_cl(&dev->write_list, cl);
  670. mei_io_tx_list_free_cl(&dev->write_waiting_list, cl);
  671. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  672. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  673. mei_cl_wake_all(cl);
  674. cl->rx_flow_ctrl_creds = 0;
  675. cl->tx_flow_ctrl_creds = 0;
  676. cl->timer_count = 0;
  677. if (!cl->me_cl)
  678. return;
  679. if (!WARN_ON(cl->me_cl->connect_count == 0))
  680. cl->me_cl->connect_count--;
  681. if (cl->me_cl->connect_count == 0)
  682. cl->me_cl->tx_flow_ctrl_creds = 0;
  683. mei_me_cl_put(cl->me_cl);
  684. cl->me_cl = NULL;
  685. }
  686. static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
  687. {
  688. if (!mei_me_cl_get(me_cl))
  689. return -ENOENT;
  690. /* only one connection is allowed for fixed address clients */
  691. if (me_cl->props.fixed_address) {
  692. if (me_cl->connect_count) {
  693. mei_me_cl_put(me_cl);
  694. return -EBUSY;
  695. }
  696. }
  697. cl->me_cl = me_cl;
  698. cl->state = MEI_FILE_CONNECTING;
  699. cl->me_cl->connect_count++;
  700. return 0;
  701. }
  702. /*
  703. * mei_cl_send_disconnect - send disconnect request
  704. *
  705. * @cl: host client
  706. * @cb: callback block
  707. *
  708. * Return: 0, OK; otherwise, error.
  709. */
  710. static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
  711. {
  712. struct mei_device *dev;
  713. int ret;
  714. dev = cl->dev;
  715. ret = mei_hbm_cl_disconnect_req(dev, cl);
  716. cl->status = ret;
  717. if (ret) {
  718. cl->state = MEI_FILE_DISCONNECT_REPLY;
  719. return ret;
  720. }
  721. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  722. cl->timer_count = MEI_CONNECT_TIMEOUT;
  723. mei_schedule_stall_timer(dev);
  724. return 0;
  725. }
  726. /**
  727. * mei_cl_irq_disconnect - processes close related operation from
  728. * interrupt thread context - send disconnect request
  729. *
  730. * @cl: client
  731. * @cb: callback block.
  732. * @cmpl_list: complete list.
  733. *
  734. * Return: 0, OK; otherwise, error.
  735. */
  736. int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
  737. struct list_head *cmpl_list)
  738. {
  739. struct mei_device *dev = cl->dev;
  740. u32 msg_slots;
  741. int slots;
  742. int ret;
  743. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  744. slots = mei_hbuf_empty_slots(dev);
  745. if (slots < 0)
  746. return -EOVERFLOW;
  747. if ((u32)slots < msg_slots)
  748. return -EMSGSIZE;
  749. ret = mei_cl_send_disconnect(cl, cb);
  750. if (ret)
  751. list_move_tail(&cb->list, cmpl_list);
  752. return ret;
  753. }
  754. /**
  755. * __mei_cl_disconnect - disconnect host client from the me one
  756. * internal function runtime pm has to be already acquired
  757. *
  758. * @cl: host client
  759. *
  760. * Return: 0 on success, <0 on failure.
  761. */
  762. static int __mei_cl_disconnect(struct mei_cl *cl)
  763. {
  764. struct mei_device *dev;
  765. struct mei_cl_cb *cb;
  766. int rets;
  767. dev = cl->dev;
  768. cl->state = MEI_FILE_DISCONNECTING;
  769. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
  770. if (!cb) {
  771. rets = -ENOMEM;
  772. goto out;
  773. }
  774. if (mei_hbuf_acquire(dev)) {
  775. rets = mei_cl_send_disconnect(cl, cb);
  776. if (rets) {
  777. cl_err(dev, cl, "failed to disconnect.\n");
  778. goto out;
  779. }
  780. }
  781. mutex_unlock(&dev->device_lock);
  782. wait_event_timeout(cl->wait,
  783. cl->state == MEI_FILE_DISCONNECT_REPLY ||
  784. cl->state == MEI_FILE_DISCONNECTED,
  785. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  786. mutex_lock(&dev->device_lock);
  787. rets = cl->status;
  788. if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
  789. cl->state != MEI_FILE_DISCONNECTED) {
  790. cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
  791. rets = -ETIME;
  792. }
  793. out:
  794. /* we disconnect also on error */
  795. mei_cl_set_disconnected(cl);
  796. if (!rets)
  797. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  798. mei_io_cb_free(cb);
  799. return rets;
  800. }
  801. /**
  802. * mei_cl_disconnect - disconnect host client from the me one
  803. *
  804. * @cl: host client
  805. *
  806. * Locking: called under "dev->device_lock" lock
  807. *
  808. * Return: 0 on success, <0 on failure.
  809. */
  810. int mei_cl_disconnect(struct mei_cl *cl)
  811. {
  812. struct mei_device *dev;
  813. int rets;
  814. if (WARN_ON(!cl || !cl->dev))
  815. return -ENODEV;
  816. dev = cl->dev;
  817. cl_dbg(dev, cl, "disconnecting");
  818. if (!mei_cl_is_connected(cl))
  819. return 0;
  820. if (mei_cl_is_fixed_address(cl)) {
  821. mei_cl_set_disconnected(cl);
  822. return 0;
  823. }
  824. if (dev->dev_state == MEI_DEV_POWER_DOWN) {
  825. cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
  826. mei_cl_set_disconnected(cl);
  827. return 0;
  828. }
  829. rets = pm_runtime_get(dev->dev);
  830. if (rets < 0 && rets != -EINPROGRESS) {
  831. pm_runtime_put_noidle(dev->dev);
  832. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  833. return rets;
  834. }
  835. rets = __mei_cl_disconnect(cl);
  836. cl_dbg(dev, cl, "rpm: autosuspend\n");
  837. pm_runtime_mark_last_busy(dev->dev);
  838. pm_runtime_put_autosuspend(dev->dev);
  839. return rets;
  840. }
  841. /**
  842. * mei_cl_is_other_connecting - checks if other
  843. * client with the same me client id is connecting
  844. *
  845. * @cl: private data of the file object
  846. *
  847. * Return: true if other client is connected, false - otherwise.
  848. */
  849. static bool mei_cl_is_other_connecting(struct mei_cl *cl)
  850. {
  851. struct mei_device *dev;
  852. struct mei_cl_cb *cb;
  853. dev = cl->dev;
  854. list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
  855. if (cb->fop_type == MEI_FOP_CONNECT &&
  856. mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
  857. return true;
  858. }
  859. return false;
  860. }
  861. /**
  862. * mei_cl_send_connect - send connect request
  863. *
  864. * @cl: host client
  865. * @cb: callback block
  866. *
  867. * Return: 0, OK; otherwise, error.
  868. */
  869. static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
  870. {
  871. struct mei_device *dev;
  872. int ret;
  873. dev = cl->dev;
  874. ret = mei_hbm_cl_connect_req(dev, cl);
  875. cl->status = ret;
  876. if (ret) {
  877. cl->state = MEI_FILE_DISCONNECT_REPLY;
  878. return ret;
  879. }
  880. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  881. cl->timer_count = MEI_CONNECT_TIMEOUT;
  882. mei_schedule_stall_timer(dev);
  883. return 0;
  884. }
  885. /**
  886. * mei_cl_irq_connect - send connect request in irq_thread context
  887. *
  888. * @cl: host client
  889. * @cb: callback block
  890. * @cmpl_list: complete list
  891. *
  892. * Return: 0, OK; otherwise, error.
  893. */
  894. int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
  895. struct list_head *cmpl_list)
  896. {
  897. struct mei_device *dev = cl->dev;
  898. u32 msg_slots;
  899. int slots;
  900. int rets;
  901. if (mei_cl_is_other_connecting(cl))
  902. return 0;
  903. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  904. slots = mei_hbuf_empty_slots(dev);
  905. if (slots < 0)
  906. return -EOVERFLOW;
  907. if ((u32)slots < msg_slots)
  908. return -EMSGSIZE;
  909. rets = mei_cl_send_connect(cl, cb);
  910. if (rets)
  911. list_move_tail(&cb->list, cmpl_list);
  912. return rets;
  913. }
  914. /**
  915. * mei_cl_connect - connect host client to the me one
  916. *
  917. * @cl: host client
  918. * @me_cl: me client
  919. * @fp: pointer to file structure
  920. *
  921. * Locking: called under "dev->device_lock" lock
  922. *
  923. * Return: 0 on success, <0 on failure.
  924. */
  925. int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
  926. const struct file *fp)
  927. {
  928. struct mei_device *dev;
  929. struct mei_cl_cb *cb;
  930. int rets;
  931. if (WARN_ON(!cl || !cl->dev || !me_cl))
  932. return -ENODEV;
  933. dev = cl->dev;
  934. rets = mei_cl_set_connecting(cl, me_cl);
  935. if (rets)
  936. goto nortpm;
  937. if (mei_cl_is_fixed_address(cl)) {
  938. cl->state = MEI_FILE_CONNECTED;
  939. rets = 0;
  940. goto nortpm;
  941. }
  942. rets = pm_runtime_get(dev->dev);
  943. if (rets < 0 && rets != -EINPROGRESS) {
  944. pm_runtime_put_noidle(dev->dev);
  945. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  946. goto nortpm;
  947. }
  948. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
  949. if (!cb) {
  950. rets = -ENOMEM;
  951. goto out;
  952. }
  953. /* run hbuf acquire last so we don't have to undo */
  954. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  955. rets = mei_cl_send_connect(cl, cb);
  956. if (rets)
  957. goto out;
  958. }
  959. mutex_unlock(&dev->device_lock);
  960. wait_event_timeout(cl->wait,
  961. (cl->state == MEI_FILE_CONNECTED ||
  962. cl->state == MEI_FILE_DISCONNECTED ||
  963. cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
  964. cl->state == MEI_FILE_DISCONNECT_REPLY),
  965. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  966. mutex_lock(&dev->device_lock);
  967. if (!mei_cl_is_connected(cl)) {
  968. if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
  969. mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
  970. mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
  971. /* ignore disconnect return valuue;
  972. * in case of failure reset will be invoked
  973. */
  974. __mei_cl_disconnect(cl);
  975. rets = -EFAULT;
  976. goto out;
  977. }
  978. /* timeout or something went really wrong */
  979. if (!cl->status)
  980. cl->status = -EFAULT;
  981. }
  982. rets = cl->status;
  983. out:
  984. cl_dbg(dev, cl, "rpm: autosuspend\n");
  985. pm_runtime_mark_last_busy(dev->dev);
  986. pm_runtime_put_autosuspend(dev->dev);
  987. mei_io_cb_free(cb);
  988. nortpm:
  989. if (!mei_cl_is_connected(cl))
  990. mei_cl_set_disconnected(cl);
  991. return rets;
  992. }
  993. /**
  994. * mei_cl_alloc_linked - allocate and link host client
  995. *
  996. * @dev: the device structure
  997. *
  998. * Return: cl on success ERR_PTR on failure
  999. */
  1000. struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
  1001. {
  1002. struct mei_cl *cl;
  1003. int ret;
  1004. cl = mei_cl_allocate(dev);
  1005. if (!cl) {
  1006. ret = -ENOMEM;
  1007. goto err;
  1008. }
  1009. ret = mei_cl_link(cl);
  1010. if (ret)
  1011. goto err;
  1012. return cl;
  1013. err:
  1014. kfree(cl);
  1015. return ERR_PTR(ret);
  1016. }
  1017. /**
  1018. * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
  1019. *
  1020. * @cl: host client
  1021. *
  1022. * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
  1023. */
  1024. static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
  1025. {
  1026. if (WARN_ON(!cl || !cl->me_cl))
  1027. return -EINVAL;
  1028. if (cl->tx_flow_ctrl_creds > 0)
  1029. return 1;
  1030. if (mei_cl_is_fixed_address(cl))
  1031. return 1;
  1032. if (mei_cl_is_single_recv_buf(cl)) {
  1033. if (cl->me_cl->tx_flow_ctrl_creds > 0)
  1034. return 1;
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
  1040. * for a client
  1041. *
  1042. * @cl: host client
  1043. *
  1044. * Return:
  1045. * 0 on success
  1046. * -EINVAL when ctrl credits are <= 0
  1047. */
  1048. static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
  1049. {
  1050. if (WARN_ON(!cl || !cl->me_cl))
  1051. return -EINVAL;
  1052. if (mei_cl_is_fixed_address(cl))
  1053. return 0;
  1054. if (mei_cl_is_single_recv_buf(cl)) {
  1055. if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
  1056. return -EINVAL;
  1057. cl->me_cl->tx_flow_ctrl_creds--;
  1058. } else {
  1059. if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
  1060. return -EINVAL;
  1061. cl->tx_flow_ctrl_creds--;
  1062. }
  1063. return 0;
  1064. }
  1065. /**
  1066. * mei_cl_notify_fop2req - convert fop to proper request
  1067. *
  1068. * @fop: client notification start response command
  1069. *
  1070. * Return: MEI_HBM_NOTIFICATION_START/STOP
  1071. */
  1072. u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
  1073. {
  1074. if (fop == MEI_FOP_NOTIFY_START)
  1075. return MEI_HBM_NOTIFICATION_START;
  1076. else
  1077. return MEI_HBM_NOTIFICATION_STOP;
  1078. }
  1079. /**
  1080. * mei_cl_notify_req2fop - convert notification request top file operation type
  1081. *
  1082. * @req: hbm notification request type
  1083. *
  1084. * Return: MEI_FOP_NOTIFY_START/STOP
  1085. */
  1086. enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
  1087. {
  1088. if (req == MEI_HBM_NOTIFICATION_START)
  1089. return MEI_FOP_NOTIFY_START;
  1090. else
  1091. return MEI_FOP_NOTIFY_STOP;
  1092. }
  1093. /**
  1094. * mei_cl_irq_notify - send notification request in irq_thread context
  1095. *
  1096. * @cl: client
  1097. * @cb: callback block.
  1098. * @cmpl_list: complete list.
  1099. *
  1100. * Return: 0 on such and error otherwise.
  1101. */
  1102. int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
  1103. struct list_head *cmpl_list)
  1104. {
  1105. struct mei_device *dev = cl->dev;
  1106. u32 msg_slots;
  1107. int slots;
  1108. int ret;
  1109. bool request;
  1110. msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
  1111. slots = mei_hbuf_empty_slots(dev);
  1112. if (slots < 0)
  1113. return -EOVERFLOW;
  1114. if ((u32)slots < msg_slots)
  1115. return -EMSGSIZE;
  1116. request = mei_cl_notify_fop2req(cb->fop_type);
  1117. ret = mei_hbm_cl_notify_req(dev, cl, request);
  1118. if (ret) {
  1119. cl->status = ret;
  1120. list_move_tail(&cb->list, cmpl_list);
  1121. return ret;
  1122. }
  1123. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1124. return 0;
  1125. }
  1126. /**
  1127. * mei_cl_notify_request - send notification stop/start request
  1128. *
  1129. * @cl: host client
  1130. * @fp: associate request with file
  1131. * @request: 1 for start or 0 for stop
  1132. *
  1133. * Locking: called under "dev->device_lock" lock
  1134. *
  1135. * Return: 0 on such and error otherwise.
  1136. */
  1137. int mei_cl_notify_request(struct mei_cl *cl,
  1138. const struct file *fp, u8 request)
  1139. {
  1140. struct mei_device *dev;
  1141. struct mei_cl_cb *cb;
  1142. enum mei_cb_file_ops fop_type;
  1143. int rets;
  1144. if (WARN_ON(!cl || !cl->dev))
  1145. return -ENODEV;
  1146. dev = cl->dev;
  1147. if (!dev->hbm_f_ev_supported) {
  1148. cl_dbg(dev, cl, "notifications not supported\n");
  1149. return -EOPNOTSUPP;
  1150. }
  1151. if (!mei_cl_is_connected(cl))
  1152. return -ENODEV;
  1153. rets = pm_runtime_get(dev->dev);
  1154. if (rets < 0 && rets != -EINPROGRESS) {
  1155. pm_runtime_put_noidle(dev->dev);
  1156. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1157. return rets;
  1158. }
  1159. fop_type = mei_cl_notify_req2fop(request);
  1160. cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
  1161. if (!cb) {
  1162. rets = -ENOMEM;
  1163. goto out;
  1164. }
  1165. if (mei_hbuf_acquire(dev)) {
  1166. if (mei_hbm_cl_notify_req(dev, cl, request)) {
  1167. rets = -ENODEV;
  1168. goto out;
  1169. }
  1170. list_move_tail(&cb->list, &dev->ctrl_rd_list);
  1171. }
  1172. mutex_unlock(&dev->device_lock);
  1173. wait_event_timeout(cl->wait,
  1174. cl->notify_en == request || !mei_cl_is_connected(cl),
  1175. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  1176. mutex_lock(&dev->device_lock);
  1177. if (cl->notify_en != request && !cl->status)
  1178. cl->status = -EFAULT;
  1179. rets = cl->status;
  1180. out:
  1181. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1182. pm_runtime_mark_last_busy(dev->dev);
  1183. pm_runtime_put_autosuspend(dev->dev);
  1184. mei_io_cb_free(cb);
  1185. return rets;
  1186. }
  1187. /**
  1188. * mei_cl_notify - raise notification
  1189. *
  1190. * @cl: host client
  1191. *
  1192. * Locking: called under "dev->device_lock" lock
  1193. */
  1194. void mei_cl_notify(struct mei_cl *cl)
  1195. {
  1196. struct mei_device *dev;
  1197. if (!cl || !cl->dev)
  1198. return;
  1199. dev = cl->dev;
  1200. if (!cl->notify_en)
  1201. return;
  1202. cl_dbg(dev, cl, "notify event");
  1203. cl->notify_ev = true;
  1204. if (!mei_cl_bus_notify_event(cl))
  1205. wake_up_interruptible(&cl->ev_wait);
  1206. if (cl->ev_async)
  1207. kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
  1208. }
  1209. /**
  1210. * mei_cl_notify_get - get or wait for notification event
  1211. *
  1212. * @cl: host client
  1213. * @block: this request is blocking
  1214. * @notify_ev: true if notification event was received
  1215. *
  1216. * Locking: called under "dev->device_lock" lock
  1217. *
  1218. * Return: 0 on such and error otherwise.
  1219. */
  1220. int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
  1221. {
  1222. struct mei_device *dev;
  1223. int rets;
  1224. *notify_ev = false;
  1225. if (WARN_ON(!cl || !cl->dev))
  1226. return -ENODEV;
  1227. dev = cl->dev;
  1228. if (!dev->hbm_f_ev_supported) {
  1229. cl_dbg(dev, cl, "notifications not supported\n");
  1230. return -EOPNOTSUPP;
  1231. }
  1232. if (!mei_cl_is_connected(cl))
  1233. return -ENODEV;
  1234. if (cl->notify_ev)
  1235. goto out;
  1236. if (!block)
  1237. return -EAGAIN;
  1238. mutex_unlock(&dev->device_lock);
  1239. rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
  1240. mutex_lock(&dev->device_lock);
  1241. if (rets < 0)
  1242. return rets;
  1243. out:
  1244. *notify_ev = cl->notify_ev;
  1245. cl->notify_ev = false;
  1246. return 0;
  1247. }
  1248. /**
  1249. * mei_cl_read_start - the start read client message function.
  1250. *
  1251. * @cl: host client
  1252. * @length: number of bytes to read
  1253. * @fp: pointer to file structure
  1254. *
  1255. * Return: 0 on success, <0 on failure.
  1256. */
  1257. int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
  1258. {
  1259. struct mei_device *dev;
  1260. struct mei_cl_cb *cb;
  1261. int rets;
  1262. if (WARN_ON(!cl || !cl->dev))
  1263. return -ENODEV;
  1264. dev = cl->dev;
  1265. if (!mei_cl_is_connected(cl))
  1266. return -ENODEV;
  1267. if (!mei_me_cl_is_active(cl->me_cl)) {
  1268. cl_err(dev, cl, "no such me client\n");
  1269. return -ENOTTY;
  1270. }
  1271. if (mei_cl_is_fixed_address(cl))
  1272. return 0;
  1273. /* HW currently supports only one pending read */
  1274. if (cl->rx_flow_ctrl_creds)
  1275. return -EBUSY;
  1276. cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
  1277. if (!cb)
  1278. return -ENOMEM;
  1279. rets = pm_runtime_get(dev->dev);
  1280. if (rets < 0 && rets != -EINPROGRESS) {
  1281. pm_runtime_put_noidle(dev->dev);
  1282. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  1283. goto nortpm;
  1284. }
  1285. rets = 0;
  1286. if (mei_hbuf_acquire(dev)) {
  1287. rets = mei_hbm_cl_flow_control_req(dev, cl);
  1288. if (rets < 0)
  1289. goto out;
  1290. list_move_tail(&cb->list, &cl->rd_pending);
  1291. }
  1292. cl->rx_flow_ctrl_creds++;
  1293. out:
  1294. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1295. pm_runtime_mark_last_busy(dev->dev);
  1296. pm_runtime_put_autosuspend(dev->dev);
  1297. nortpm:
  1298. if (rets)
  1299. mei_io_cb_free(cb);
  1300. return rets;
  1301. }
  1302. /**
  1303. * mei_msg_hdr_init - initialize mei message header
  1304. *
  1305. * @mei_hdr: mei message header
  1306. * @cb: message callback structure
  1307. */
  1308. static void mei_msg_hdr_init(struct mei_msg_hdr *mei_hdr, struct mei_cl_cb *cb)
  1309. {
  1310. mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
  1311. mei_hdr->me_addr = mei_cl_me_id(cb->cl);
  1312. mei_hdr->length = 0;
  1313. mei_hdr->reserved = 0;
  1314. mei_hdr->msg_complete = 0;
  1315. mei_hdr->dma_ring = 0;
  1316. mei_hdr->internal = cb->internal;
  1317. }
  1318. /**
  1319. * mei_cl_irq_write - write a message to device
  1320. * from the interrupt thread context
  1321. *
  1322. * @cl: client
  1323. * @cb: callback block.
  1324. * @cmpl_list: complete list.
  1325. *
  1326. * Return: 0, OK; otherwise error.
  1327. */
  1328. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  1329. struct list_head *cmpl_list)
  1330. {
  1331. struct mei_device *dev;
  1332. struct mei_msg_data *buf;
  1333. struct mei_msg_hdr mei_hdr;
  1334. size_t hdr_len = sizeof(mei_hdr);
  1335. size_t len;
  1336. size_t hbuf_len;
  1337. int hbuf_slots;
  1338. int rets;
  1339. bool first_chunk;
  1340. if (WARN_ON(!cl || !cl->dev))
  1341. return -ENODEV;
  1342. dev = cl->dev;
  1343. buf = &cb->buf;
  1344. first_chunk = cb->buf_idx == 0;
  1345. rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
  1346. if (rets < 0)
  1347. goto err;
  1348. if (rets == 0) {
  1349. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1350. return 0;
  1351. }
  1352. len = buf->size - cb->buf_idx;
  1353. hbuf_slots = mei_hbuf_empty_slots(dev);
  1354. if (hbuf_slots < 0) {
  1355. rets = -EOVERFLOW;
  1356. goto err;
  1357. }
  1358. hbuf_len = mei_slots2data(hbuf_slots);
  1359. mei_msg_hdr_init(&mei_hdr, cb);
  1360. /**
  1361. * Split the message only if we can write the whole host buffer
  1362. * otherwise wait for next time the host buffer is empty.
  1363. */
  1364. if (len + hdr_len <= hbuf_len) {
  1365. mei_hdr.length = len;
  1366. mei_hdr.msg_complete = 1;
  1367. } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
  1368. mei_hdr.length = hbuf_len - hdr_len;
  1369. } else {
  1370. return 0;
  1371. }
  1372. cl_dbg(dev, cl, "buf: size = %zu idx = %zu\n",
  1373. cb->buf.size, cb->buf_idx);
  1374. rets = mei_write_message(dev, &mei_hdr, hdr_len,
  1375. buf->data + cb->buf_idx, mei_hdr.length);
  1376. if (rets)
  1377. goto err;
  1378. cl->status = 0;
  1379. cl->writing_state = MEI_WRITING;
  1380. cb->buf_idx += mei_hdr.length;
  1381. if (first_chunk) {
  1382. if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
  1383. rets = -EIO;
  1384. goto err;
  1385. }
  1386. }
  1387. if (mei_hdr.msg_complete)
  1388. list_move_tail(&cb->list, &dev->write_waiting_list);
  1389. return 0;
  1390. err:
  1391. cl->status = rets;
  1392. list_move_tail(&cb->list, cmpl_list);
  1393. return rets;
  1394. }
  1395. /**
  1396. * mei_cl_write - submit a write cb to mei device
  1397. * assumes device_lock is locked
  1398. *
  1399. * @cl: host client
  1400. * @cb: write callback with filled data
  1401. *
  1402. * Return: number of bytes sent on success, <0 on failure.
  1403. */
  1404. ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
  1405. {
  1406. struct mei_device *dev;
  1407. struct mei_msg_data *buf;
  1408. struct mei_msg_hdr mei_hdr;
  1409. size_t hdr_len = sizeof(mei_hdr);
  1410. size_t len;
  1411. size_t hbuf_len;
  1412. int hbuf_slots;
  1413. ssize_t rets;
  1414. bool blocking;
  1415. if (WARN_ON(!cl || !cl->dev))
  1416. return -ENODEV;
  1417. if (WARN_ON(!cb))
  1418. return -EINVAL;
  1419. dev = cl->dev;
  1420. buf = &cb->buf;
  1421. len = buf->size;
  1422. blocking = cb->blocking;
  1423. cl_dbg(dev, cl, "len=%zd\n", len);
  1424. rets = pm_runtime_get(dev->dev);
  1425. if (rets < 0 && rets != -EINPROGRESS) {
  1426. pm_runtime_put_noidle(dev->dev);
  1427. cl_err(dev, cl, "rpm: get failed %zd\n", rets);
  1428. goto free;
  1429. }
  1430. cb->buf_idx = 0;
  1431. cl->writing_state = MEI_IDLE;
  1432. rets = mei_cl_tx_flow_ctrl_creds(cl);
  1433. if (rets < 0)
  1434. goto err;
  1435. mei_msg_hdr_init(&mei_hdr, cb);
  1436. if (rets == 0) {
  1437. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  1438. rets = len;
  1439. goto out;
  1440. }
  1441. if (!mei_hbuf_acquire(dev)) {
  1442. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  1443. rets = len;
  1444. goto out;
  1445. }
  1446. hbuf_slots = mei_hbuf_empty_slots(dev);
  1447. if (hbuf_slots < 0) {
  1448. rets = -EOVERFLOW;
  1449. goto out;
  1450. }
  1451. hbuf_len = mei_slots2data(hbuf_slots);
  1452. if (len + hdr_len <= hbuf_len) {
  1453. mei_hdr.length = len;
  1454. mei_hdr.msg_complete = 1;
  1455. } else {
  1456. mei_hdr.length = hbuf_len - hdr_len;
  1457. }
  1458. rets = mei_write_message(dev, &mei_hdr, hdr_len,
  1459. buf->data, mei_hdr.length);
  1460. if (rets)
  1461. goto err;
  1462. rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
  1463. if (rets)
  1464. goto err;
  1465. cl->writing_state = MEI_WRITING;
  1466. cb->buf_idx = mei_hdr.length;
  1467. out:
  1468. if (mei_hdr.msg_complete)
  1469. mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
  1470. else
  1471. mei_tx_cb_enqueue(cb, &dev->write_list);
  1472. cb = NULL;
  1473. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  1474. mutex_unlock(&dev->device_lock);
  1475. rets = wait_event_interruptible(cl->tx_wait,
  1476. cl->writing_state == MEI_WRITE_COMPLETE ||
  1477. (!mei_cl_is_connected(cl)));
  1478. mutex_lock(&dev->device_lock);
  1479. /* wait_event_interruptible returns -ERESTARTSYS */
  1480. if (rets) {
  1481. if (signal_pending(current))
  1482. rets = -EINTR;
  1483. goto err;
  1484. }
  1485. if (cl->writing_state != MEI_WRITE_COMPLETE) {
  1486. rets = -EFAULT;
  1487. goto err;
  1488. }
  1489. }
  1490. rets = len;
  1491. err:
  1492. cl_dbg(dev, cl, "rpm: autosuspend\n");
  1493. pm_runtime_mark_last_busy(dev->dev);
  1494. pm_runtime_put_autosuspend(dev->dev);
  1495. free:
  1496. mei_io_cb_free(cb);
  1497. return rets;
  1498. }
  1499. /**
  1500. * mei_cl_complete - processes completed operation for a client
  1501. *
  1502. * @cl: private data of the file object.
  1503. * @cb: callback block.
  1504. */
  1505. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  1506. {
  1507. struct mei_device *dev = cl->dev;
  1508. switch (cb->fop_type) {
  1509. case MEI_FOP_WRITE:
  1510. mei_tx_cb_dequeue(cb);
  1511. cl->writing_state = MEI_WRITE_COMPLETE;
  1512. if (waitqueue_active(&cl->tx_wait)) {
  1513. wake_up_interruptible(&cl->tx_wait);
  1514. } else {
  1515. pm_runtime_mark_last_busy(dev->dev);
  1516. pm_request_autosuspend(dev->dev);
  1517. }
  1518. break;
  1519. case MEI_FOP_READ:
  1520. list_add_tail(&cb->list, &cl->rd_completed);
  1521. if (!mei_cl_is_fixed_address(cl) &&
  1522. !WARN_ON(!cl->rx_flow_ctrl_creds))
  1523. cl->rx_flow_ctrl_creds--;
  1524. if (!mei_cl_bus_rx_event(cl))
  1525. wake_up_interruptible(&cl->rx_wait);
  1526. break;
  1527. case MEI_FOP_CONNECT:
  1528. case MEI_FOP_DISCONNECT:
  1529. case MEI_FOP_NOTIFY_STOP:
  1530. case MEI_FOP_NOTIFY_START:
  1531. if (waitqueue_active(&cl->wait))
  1532. wake_up(&cl->wait);
  1533. break;
  1534. case MEI_FOP_DISCONNECT_RSP:
  1535. mei_io_cb_free(cb);
  1536. mei_cl_set_disconnected(cl);
  1537. break;
  1538. default:
  1539. BUG_ON(0);
  1540. }
  1541. }
  1542. /**
  1543. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  1544. *
  1545. * @dev: mei device
  1546. */
  1547. void mei_cl_all_disconnect(struct mei_device *dev)
  1548. {
  1549. struct mei_cl *cl;
  1550. list_for_each_entry(cl, &dev->file_list, link)
  1551. mei_cl_set_disconnected(cl);
  1552. }