client.c 37 KB

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