client.c 37 KB

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