client.c 37 KB

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