client.c 38 KB

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