bus.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2023, Intel Corporation. All rights reserved.
  4. * Intel Management Engine Interface (Intel MEI) Linux driver
  5. */
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched/signal.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/mei_cl_bus.h>
  17. #include "mei_dev.h"
  18. #include "client.h"
  19. #define to_mei_cl_driver(d) container_of_const(d, struct mei_cl_driver, driver)
  20. /**
  21. * __mei_cl_send - internal client send (write)
  22. *
  23. * @cl: host client
  24. * @buf: buffer to send
  25. * @length: buffer length
  26. * @vtag: virtual tag
  27. * @mode: sending mode
  28. *
  29. * Return: written size bytes or < 0 on error
  30. */
  31. ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
  32. unsigned int mode)
  33. {
  34. return __mei_cl_send_timeout(cl, buf, length, vtag, mode, MAX_SCHEDULE_TIMEOUT);
  35. }
  36. /**
  37. * __mei_cl_send_timeout - internal client send (write)
  38. *
  39. * @cl: host client
  40. * @buf: buffer to send
  41. * @length: buffer length
  42. * @vtag: virtual tag
  43. * @mode: sending mode
  44. * @timeout: send timeout in milliseconds.
  45. * effective only for blocking writes: the MEI_CL_IO_TX_BLOCKING mode bit is set.
  46. * set timeout to the MAX_SCHEDULE_TIMEOUT to maixum allowed wait.
  47. *
  48. * Return: written size bytes or < 0 on error
  49. */
  50. ssize_t __mei_cl_send_timeout(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
  51. unsigned int mode, unsigned long timeout)
  52. {
  53. struct mei_device *bus;
  54. struct mei_cl_cb *cb;
  55. ssize_t rets;
  56. if (WARN_ON(!cl || !cl->dev))
  57. return -ENODEV;
  58. bus = cl->dev;
  59. mutex_lock(&bus->device_lock);
  60. if (bus->dev_state != MEI_DEV_ENABLED &&
  61. bus->dev_state != MEI_DEV_POWERING_DOWN) {
  62. rets = -ENODEV;
  63. goto out;
  64. }
  65. if (!mei_cl_is_connected(cl)) {
  66. rets = -ENODEV;
  67. goto out;
  68. }
  69. /* Check if we have an ME client device */
  70. if (!mei_me_cl_is_active(cl->me_cl)) {
  71. rets = -ENOTTY;
  72. goto out;
  73. }
  74. if (vtag) {
  75. /* Check if vtag is supported by client */
  76. rets = mei_cl_vt_support_check(cl);
  77. if (rets)
  78. goto out;
  79. }
  80. if (length > mei_cl_mtu(cl)) {
  81. rets = -EFBIG;
  82. goto out;
  83. }
  84. while (cl->tx_cb_queued >= bus->tx_queue_limit) {
  85. mutex_unlock(&bus->device_lock);
  86. rets = wait_event_interruptible(cl->tx_wait,
  87. cl->writing_state == MEI_WRITE_COMPLETE ||
  88. (!mei_cl_is_connected(cl)));
  89. mutex_lock(&bus->device_lock);
  90. if (rets) {
  91. if (signal_pending(current))
  92. rets = -EINTR;
  93. goto out;
  94. }
  95. if (!mei_cl_is_connected(cl)) {
  96. rets = -ENODEV;
  97. goto out;
  98. }
  99. }
  100. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
  101. if (!cb) {
  102. rets = -ENOMEM;
  103. goto out;
  104. }
  105. cb->vtag = vtag;
  106. cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
  107. cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
  108. memcpy(cb->buf.data, buf, length);
  109. /* hack we point data to header */
  110. if (mode & MEI_CL_IO_SGL) {
  111. cb->ext_hdr = (struct mei_ext_hdr *)cb->buf.data;
  112. cb->buf.data = NULL;
  113. cb->buf.size = 0;
  114. }
  115. rets = mei_cl_write(cl, cb, timeout);
  116. if (mode & MEI_CL_IO_SGL && rets == 0)
  117. rets = length;
  118. out:
  119. mutex_unlock(&bus->device_lock);
  120. return rets;
  121. }
  122. /**
  123. * __mei_cl_recv - internal client receive (read)
  124. *
  125. * @cl: host client
  126. * @buf: buffer to receive
  127. * @length: buffer length
  128. * @vtag: virtual tag
  129. * @mode: io mode
  130. * @timeout: recv timeout, 0 for infinite timeout
  131. *
  132. * Return: read size in bytes of < 0 on error
  133. */
  134. ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
  135. unsigned int mode, unsigned long timeout)
  136. {
  137. struct mei_device *bus;
  138. struct mei_cl_cb *cb;
  139. size_t r_length;
  140. ssize_t rets;
  141. bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
  142. if (WARN_ON(!cl || !cl->dev))
  143. return -ENODEV;
  144. bus = cl->dev;
  145. mutex_lock(&bus->device_lock);
  146. if (bus->dev_state != MEI_DEV_ENABLED &&
  147. bus->dev_state != MEI_DEV_POWERING_DOWN) {
  148. rets = -ENODEV;
  149. goto out;
  150. }
  151. cb = mei_cl_read_cb(cl, NULL);
  152. if (cb)
  153. goto copy;
  154. rets = mei_cl_read_start(cl, length, NULL);
  155. if (rets && rets != -EBUSY)
  156. goto out;
  157. if (nonblock) {
  158. rets = -EAGAIN;
  159. goto out;
  160. }
  161. /* wait on event only if there is no other waiter */
  162. /* synchronized under device mutex */
  163. if (!waitqueue_active(&cl->rx_wait)) {
  164. mutex_unlock(&bus->device_lock);
  165. if (timeout) {
  166. rets = wait_event_interruptible_timeout
  167. (cl->rx_wait,
  168. mei_cl_read_cb(cl, NULL) ||
  169. (!mei_cl_is_connected(cl)),
  170. msecs_to_jiffies(timeout));
  171. if (rets == 0)
  172. return -ETIME;
  173. if (rets < 0) {
  174. if (signal_pending(current))
  175. return -EINTR;
  176. return -ERESTARTSYS;
  177. }
  178. } else {
  179. if (wait_event_interruptible
  180. (cl->rx_wait,
  181. mei_cl_read_cb(cl, NULL) ||
  182. (!mei_cl_is_connected(cl)))) {
  183. if (signal_pending(current))
  184. return -EINTR;
  185. return -ERESTARTSYS;
  186. }
  187. }
  188. mutex_lock(&bus->device_lock);
  189. if (!mei_cl_is_connected(cl)) {
  190. rets = -ENODEV;
  191. goto out;
  192. }
  193. }
  194. cb = mei_cl_read_cb(cl, NULL);
  195. if (!cb) {
  196. rets = 0;
  197. goto out;
  198. }
  199. copy:
  200. if (cb->status) {
  201. rets = cb->status;
  202. goto free;
  203. }
  204. /* for the GSC type - copy the extended header to the buffer */
  205. if (cb->ext_hdr && cb->ext_hdr->type == MEI_EXT_HDR_GSC) {
  206. r_length = min_t(size_t, length, cb->ext_hdr->length * sizeof(u32));
  207. memcpy(buf, cb->ext_hdr, r_length);
  208. } else {
  209. r_length = min_t(size_t, length, cb->buf_idx);
  210. memcpy(buf, cb->buf.data, r_length);
  211. }
  212. rets = r_length;
  213. if (vtag)
  214. *vtag = cb->vtag;
  215. free:
  216. mei_cl_del_rd_completed(cl, cb);
  217. out:
  218. mutex_unlock(&bus->device_lock);
  219. return rets;
  220. }
  221. /**
  222. * mei_cldev_send_vtag - me device send with vtag (write)
  223. *
  224. * @cldev: me client device
  225. * @buf: buffer to send
  226. * @length: buffer length
  227. * @vtag: virtual tag
  228. *
  229. * Return:
  230. * * written size in bytes
  231. * * < 0 on error
  232. */
  233. ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, const u8 *buf,
  234. size_t length, u8 vtag)
  235. {
  236. struct mei_cl *cl = cldev->cl;
  237. return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING);
  238. }
  239. EXPORT_SYMBOL_GPL(mei_cldev_send_vtag);
  240. /**
  241. * mei_cldev_send_vtag_timeout - me device send with vtag and timeout (write)
  242. *
  243. * @cldev: me client device
  244. * @buf: buffer to send
  245. * @length: buffer length
  246. * @vtag: virtual tag
  247. * @timeout: send timeout in milliseconds, 0 for infinite timeout
  248. *
  249. * Return:
  250. * * written size in bytes
  251. * * < 0 on error
  252. */
  253. ssize_t mei_cldev_send_vtag_timeout(struct mei_cl_device *cldev, const u8 *buf,
  254. size_t length, u8 vtag, unsigned long timeout)
  255. {
  256. struct mei_cl *cl = cldev->cl;
  257. return __mei_cl_send_timeout(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING, timeout);
  258. }
  259. EXPORT_SYMBOL_GPL(mei_cldev_send_vtag_timeout);
  260. /**
  261. * mei_cldev_recv_vtag - client receive with vtag (read)
  262. *
  263. * @cldev: me client device
  264. * @buf: buffer to receive
  265. * @length: buffer length
  266. * @vtag: virtual tag
  267. *
  268. * Return:
  269. * * read size in bytes
  270. * * < 0 on error
  271. */
  272. ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
  273. u8 *vtag)
  274. {
  275. struct mei_cl *cl = cldev->cl;
  276. return __mei_cl_recv(cl, buf, length, vtag, 0, 0);
  277. }
  278. EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag);
  279. /**
  280. * mei_cldev_recv_timeout - client receive with timeout (read)
  281. *
  282. * @cldev: me client device
  283. * @buf: buffer to receive
  284. * @length: buffer length
  285. * @timeout: send timeout in milliseconds, 0 for infinite timeout
  286. *
  287. * Return:
  288. * * read size in bytes
  289. * * < 0 on error
  290. */
  291. ssize_t mei_cldev_recv_timeout(struct mei_cl_device *cldev, u8 *buf, size_t length,
  292. unsigned long timeout)
  293. {
  294. return mei_cldev_recv_vtag_timeout(cldev, buf, length, NULL, timeout);
  295. }
  296. EXPORT_SYMBOL_GPL(mei_cldev_recv_timeout);
  297. /**
  298. * mei_cldev_recv_vtag_timeout - client receive with vtag (read)
  299. *
  300. * @cldev: me client device
  301. * @buf: buffer to receive
  302. * @length: buffer length
  303. * @vtag: virtual tag
  304. * @timeout: recv timeout in milliseconds, 0 for infinite timeout
  305. *
  306. * Return:
  307. * * read size in bytes
  308. * * < 0 on error
  309. */
  310. ssize_t mei_cldev_recv_vtag_timeout(struct mei_cl_device *cldev, u8 *buf, size_t length,
  311. u8 *vtag, unsigned long timeout)
  312. {
  313. struct mei_cl *cl = cldev->cl;
  314. return __mei_cl_recv(cl, buf, length, vtag, 0, timeout);
  315. }
  316. EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag_timeout);
  317. /**
  318. * mei_cldev_send - me device send (write)
  319. *
  320. * @cldev: me client device
  321. * @buf: buffer to send
  322. * @length: buffer length
  323. *
  324. * Return:
  325. * * written size in bytes
  326. * * < 0 on error
  327. */
  328. ssize_t mei_cldev_send(struct mei_cl_device *cldev, const u8 *buf, size_t length)
  329. {
  330. return mei_cldev_send_vtag(cldev, buf, length, 0);
  331. }
  332. EXPORT_SYMBOL_GPL(mei_cldev_send);
  333. /**
  334. * mei_cldev_send_timeout - me device send with timeout (write)
  335. *
  336. * @cldev: me client device
  337. * @buf: buffer to send
  338. * @length: buffer length
  339. * @timeout: send timeout in milliseconds, 0 for infinite timeout
  340. *
  341. * Return:
  342. * * written size in bytes
  343. * * < 0 on error
  344. */
  345. ssize_t mei_cldev_send_timeout(struct mei_cl_device *cldev, const u8 *buf, size_t length,
  346. unsigned long timeout)
  347. {
  348. return mei_cldev_send_vtag_timeout(cldev, buf, length, 0, timeout);
  349. }
  350. EXPORT_SYMBOL_GPL(mei_cldev_send_timeout);
  351. /**
  352. * mei_cldev_recv - client receive (read)
  353. *
  354. * @cldev: me client device
  355. * @buf: buffer to receive
  356. * @length: buffer length
  357. *
  358. * Return: read size in bytes of < 0 on error
  359. */
  360. ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
  361. {
  362. return mei_cldev_recv_vtag(cldev, buf, length, NULL);
  363. }
  364. EXPORT_SYMBOL_GPL(mei_cldev_recv);
  365. /**
  366. * mei_cl_bus_rx_work - dispatch rx event for a bus device
  367. *
  368. * @work: work
  369. */
  370. static void mei_cl_bus_rx_work(struct work_struct *work)
  371. {
  372. struct mei_cl_device *cldev;
  373. struct mei_device *bus;
  374. cldev = container_of(work, struct mei_cl_device, rx_work);
  375. bus = cldev->bus;
  376. if (cldev->rx_cb)
  377. cldev->rx_cb(cldev);
  378. mutex_lock(&bus->device_lock);
  379. if (mei_cl_is_connected(cldev->cl))
  380. mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
  381. mutex_unlock(&bus->device_lock);
  382. }
  383. /**
  384. * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
  385. *
  386. * @work: work
  387. */
  388. static void mei_cl_bus_notif_work(struct work_struct *work)
  389. {
  390. struct mei_cl_device *cldev;
  391. cldev = container_of(work, struct mei_cl_device, notif_work);
  392. if (cldev->notif_cb)
  393. cldev->notif_cb(cldev);
  394. }
  395. /**
  396. * mei_cl_bus_notify_event - schedule notify cb on bus client
  397. *
  398. * @cl: host client
  399. *
  400. * Return: true if event was scheduled
  401. * false if the client is not waiting for event
  402. */
  403. bool mei_cl_bus_notify_event(struct mei_cl *cl)
  404. {
  405. struct mei_cl_device *cldev = cl->cldev;
  406. if (!cldev || !cldev->notif_cb)
  407. return false;
  408. if (!cl->notify_ev)
  409. return false;
  410. schedule_work(&cldev->notif_work);
  411. cl->notify_ev = false;
  412. return true;
  413. }
  414. /**
  415. * mei_cl_bus_rx_event - schedule rx event
  416. *
  417. * @cl: host client
  418. *
  419. * Return: true if event was scheduled
  420. * false if the client is not waiting for event
  421. */
  422. bool mei_cl_bus_rx_event(struct mei_cl *cl)
  423. {
  424. struct mei_cl_device *cldev = cl->cldev;
  425. if (!cldev || !cldev->rx_cb)
  426. return false;
  427. schedule_work(&cldev->rx_work);
  428. return true;
  429. }
  430. /**
  431. * mei_cldev_register_rx_cb - register Rx event callback
  432. *
  433. * @cldev: me client devices
  434. * @rx_cb: callback function
  435. *
  436. * Return: 0 on success
  437. * -EALREADY if an callback is already registered
  438. * <0 on other errors
  439. */
  440. int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
  441. {
  442. struct mei_device *bus = cldev->bus;
  443. int ret;
  444. if (!rx_cb)
  445. return -EINVAL;
  446. if (cldev->rx_cb)
  447. return -EALREADY;
  448. cldev->rx_cb = rx_cb;
  449. INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
  450. mutex_lock(&bus->device_lock);
  451. if (mei_cl_is_connected(cldev->cl))
  452. ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
  453. else
  454. ret = -ENODEV;
  455. mutex_unlock(&bus->device_lock);
  456. if (ret && ret != -EBUSY) {
  457. cancel_work_sync(&cldev->rx_work);
  458. cldev->rx_cb = NULL;
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
  464. /**
  465. * mei_cldev_register_notif_cb - register FW notification event callback
  466. *
  467. * @cldev: me client devices
  468. * @notif_cb: callback function
  469. *
  470. * Return: 0 on success
  471. * -EALREADY if an callback is already registered
  472. * <0 on other errors
  473. */
  474. int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
  475. mei_cldev_cb_t notif_cb)
  476. {
  477. struct mei_device *bus = cldev->bus;
  478. int ret;
  479. if (!notif_cb)
  480. return -EINVAL;
  481. if (cldev->notif_cb)
  482. return -EALREADY;
  483. cldev->notif_cb = notif_cb;
  484. INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
  485. mutex_lock(&bus->device_lock);
  486. ret = mei_cl_notify_request(cldev->cl, NULL, 1);
  487. mutex_unlock(&bus->device_lock);
  488. if (ret) {
  489. cancel_work_sync(&cldev->notif_work);
  490. cldev->notif_cb = NULL;
  491. return ret;
  492. }
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
  496. /**
  497. * mei_cldev_get_drvdata - driver data getter
  498. *
  499. * @cldev: mei client device
  500. *
  501. * Return: driver private data
  502. */
  503. void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
  504. {
  505. return dev_get_drvdata(&cldev->dev);
  506. }
  507. EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
  508. /**
  509. * mei_cldev_set_drvdata - driver data setter
  510. *
  511. * @cldev: mei client device
  512. * @data: data to store
  513. */
  514. void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
  515. {
  516. dev_set_drvdata(&cldev->dev, data);
  517. }
  518. EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
  519. /**
  520. * mei_cldev_ver - return protocol version of the underlying me client
  521. *
  522. * @cldev: mei client device
  523. *
  524. * Return: me client protocol version
  525. */
  526. u8 mei_cldev_ver(const struct mei_cl_device *cldev)
  527. {
  528. return mei_me_cl_ver(cldev->me_cl);
  529. }
  530. EXPORT_SYMBOL_GPL(mei_cldev_ver);
  531. /**
  532. * mei_cldev_mtu - max message that client can send and receive
  533. *
  534. * @cldev: mei client device
  535. *
  536. * Return: mtu or 0 if client is not connected
  537. */
  538. size_t mei_cldev_mtu(const struct mei_cl_device *cldev)
  539. {
  540. return mei_cl_mtu(cldev->cl);
  541. }
  542. EXPORT_SYMBOL_GPL(mei_cldev_mtu);
  543. /**
  544. * mei_cldev_enabled - check whether the device is enabled
  545. *
  546. * @cldev: mei client device
  547. *
  548. * Return: true if me client is initialized and connected
  549. */
  550. bool mei_cldev_enabled(const struct mei_cl_device *cldev)
  551. {
  552. return mei_cl_is_connected(cldev->cl);
  553. }
  554. EXPORT_SYMBOL_GPL(mei_cldev_enabled);
  555. /**
  556. * mei_cl_bus_module_get - acquire module of the underlying
  557. * hw driver.
  558. *
  559. * @cldev: mei client device
  560. *
  561. * Return: true on success; false if the module was removed.
  562. */
  563. static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
  564. {
  565. return try_module_get(cldev->bus->parent->driver->owner);
  566. }
  567. /**
  568. * mei_cl_bus_module_put - release the underlying hw module.
  569. *
  570. * @cldev: mei client device
  571. */
  572. static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
  573. {
  574. module_put(cldev->bus->parent->driver->owner);
  575. }
  576. /**
  577. * mei_cl_bus_vtag - get bus vtag entry wrapper
  578. * The tag for bus client is always first.
  579. *
  580. * @cl: host client
  581. *
  582. * Return: bus vtag or NULL
  583. */
  584. static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
  585. {
  586. return list_first_entry_or_null(&cl->vtag_map,
  587. struct mei_cl_vtag, list);
  588. }
  589. /**
  590. * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
  591. *
  592. * @cldev: me client device
  593. *
  594. * Return:
  595. * * 0 on success
  596. * * -ENOMEM if memory allocation failed
  597. */
  598. static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
  599. {
  600. struct mei_cl *cl = cldev->cl;
  601. struct mei_cl_vtag *cl_vtag;
  602. /*
  603. * Bail out if the client does not supports vtags
  604. * or has already allocated one
  605. */
  606. if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
  607. return 0;
  608. cl_vtag = mei_cl_vtag_alloc(NULL, 0);
  609. if (IS_ERR(cl_vtag))
  610. return -ENOMEM;
  611. list_add_tail(&cl_vtag->list, &cl->vtag_map);
  612. return 0;
  613. }
  614. /**
  615. * mei_cl_bus_vtag_free - remove the bus entry from vtag map
  616. *
  617. * @cldev: me client device
  618. */
  619. static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
  620. {
  621. struct mei_cl *cl = cldev->cl;
  622. struct mei_cl_vtag *cl_vtag;
  623. cl_vtag = mei_cl_bus_vtag(cl);
  624. if (!cl_vtag)
  625. return;
  626. list_del(&cl_vtag->list);
  627. kfree(cl_vtag);
  628. }
  629. void *mei_cldev_dma_map(struct mei_cl_device *cldev, u8 buffer_id, size_t size)
  630. {
  631. struct mei_device *bus;
  632. struct mei_cl *cl;
  633. int ret;
  634. if (!cldev || !buffer_id || !size)
  635. return ERR_PTR(-EINVAL);
  636. if (!IS_ALIGNED(size, MEI_FW_PAGE_SIZE)) {
  637. dev_err(&cldev->dev, "Map size should be aligned to %lu\n",
  638. MEI_FW_PAGE_SIZE);
  639. return ERR_PTR(-EINVAL);
  640. }
  641. cl = cldev->cl;
  642. bus = cldev->bus;
  643. mutex_lock(&bus->device_lock);
  644. if (cl->state == MEI_FILE_UNINITIALIZED) {
  645. ret = mei_cl_link(cl);
  646. if (ret)
  647. goto notlinked;
  648. /* update pointers */
  649. cl->cldev = cldev;
  650. }
  651. ret = mei_cl_dma_alloc_and_map(cl, NULL, buffer_id, size);
  652. if (ret)
  653. mei_cl_unlink(cl);
  654. notlinked:
  655. mutex_unlock(&bus->device_lock);
  656. if (ret)
  657. return ERR_PTR(ret);
  658. return cl->dma.vaddr;
  659. }
  660. EXPORT_SYMBOL_GPL(mei_cldev_dma_map);
  661. int mei_cldev_dma_unmap(struct mei_cl_device *cldev)
  662. {
  663. struct mei_device *bus;
  664. struct mei_cl *cl;
  665. int ret;
  666. if (!cldev)
  667. return -EINVAL;
  668. cl = cldev->cl;
  669. bus = cldev->bus;
  670. mutex_lock(&bus->device_lock);
  671. ret = mei_cl_dma_unmap(cl, NULL);
  672. mei_cl_flush_queues(cl, NULL);
  673. mei_cl_unlink(cl);
  674. mutex_unlock(&bus->device_lock);
  675. return ret;
  676. }
  677. EXPORT_SYMBOL_GPL(mei_cldev_dma_unmap);
  678. /**
  679. * mei_cldev_enable - enable me client device
  680. * create connection with me client
  681. *
  682. * @cldev: me client device
  683. *
  684. * Return: 0 on success and < 0 on error
  685. */
  686. int mei_cldev_enable(struct mei_cl_device *cldev)
  687. {
  688. struct mei_device *bus = cldev->bus;
  689. struct mei_cl *cl;
  690. int ret;
  691. cl = cldev->cl;
  692. mutex_lock(&bus->device_lock);
  693. if (cl->state == MEI_FILE_UNINITIALIZED) {
  694. ret = mei_cl_link(cl);
  695. if (ret)
  696. goto notlinked;
  697. /* update pointers */
  698. cl->cldev = cldev;
  699. }
  700. if (mei_cl_is_connected(cl)) {
  701. ret = 0;
  702. goto out;
  703. }
  704. if (!mei_me_cl_is_active(cldev->me_cl)) {
  705. dev_err(&cldev->dev, "me client is not active\n");
  706. ret = -ENOTTY;
  707. goto out;
  708. }
  709. ret = mei_cl_bus_vtag_alloc(cldev);
  710. if (ret)
  711. goto out;
  712. ret = mei_cl_connect(cl, cldev->me_cl, NULL);
  713. if (ret < 0) {
  714. dev_dbg(&cldev->dev, "cannot connect\n");
  715. mei_cl_bus_vtag_free(cldev);
  716. }
  717. out:
  718. if (ret)
  719. mei_cl_unlink(cl);
  720. notlinked:
  721. mutex_unlock(&bus->device_lock);
  722. return ret;
  723. }
  724. EXPORT_SYMBOL_GPL(mei_cldev_enable);
  725. /**
  726. * mei_cldev_unregister_callbacks - internal wrapper for unregistering
  727. * callbacks.
  728. *
  729. * @cldev: client device
  730. */
  731. static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
  732. {
  733. if (cldev->rx_cb) {
  734. cancel_work_sync(&cldev->rx_work);
  735. cldev->rx_cb = NULL;
  736. }
  737. if (cldev->notif_cb) {
  738. cancel_work_sync(&cldev->notif_work);
  739. cldev->notif_cb = NULL;
  740. }
  741. }
  742. /**
  743. * mei_cldev_disable - disable me client device
  744. * disconnect form the me client
  745. *
  746. * @cldev: me client device
  747. *
  748. * Return: 0 on success and < 0 on error
  749. */
  750. int mei_cldev_disable(struct mei_cl_device *cldev)
  751. {
  752. struct mei_device *bus;
  753. struct mei_cl *cl;
  754. int err;
  755. if (!cldev)
  756. return -ENODEV;
  757. cl = cldev->cl;
  758. bus = cldev->bus;
  759. mei_cldev_unregister_callbacks(cldev);
  760. mutex_lock(&bus->device_lock);
  761. mei_cl_bus_vtag_free(cldev);
  762. if (!mei_cl_is_connected(cl)) {
  763. dev_dbg(&cldev->dev, "Already disconnected\n");
  764. err = 0;
  765. goto out;
  766. }
  767. err = mei_cl_disconnect(cl);
  768. if (err < 0)
  769. dev_err(&cldev->dev, "Could not disconnect from the ME client\n");
  770. out:
  771. /* Flush queues and remove any pending read unless we have mapped DMA */
  772. if (!cl->dma_mapped) {
  773. mei_cl_flush_queues(cl, NULL);
  774. mei_cl_unlink(cl);
  775. }
  776. mutex_unlock(&bus->device_lock);
  777. return err;
  778. }
  779. EXPORT_SYMBOL_GPL(mei_cldev_disable);
  780. /**
  781. * mei_cldev_send_gsc_command - sends a gsc command, by sending
  782. * a gsl mei message to gsc and receiving reply from gsc
  783. *
  784. * @cldev: me client device
  785. * @client_id: client id to send the command to
  786. * @fence_id: fence id to send the command to
  787. * @sg_in: scatter gather list containing addresses for rx message buffer
  788. * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes
  789. * @sg_out: scatter gather list containing addresses for tx message buffer
  790. *
  791. * Return:
  792. * * written size in bytes
  793. * * < 0 on error
  794. */
  795. ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev,
  796. u8 client_id, u32 fence_id,
  797. struct scatterlist *sg_in,
  798. size_t total_in_len,
  799. struct scatterlist *sg_out)
  800. {
  801. struct mei_cl *cl;
  802. struct mei_device *bus;
  803. ssize_t ret = 0;
  804. struct mei_ext_hdr_gsc_h2f *ext_hdr;
  805. size_t buf_sz = sizeof(struct mei_ext_hdr_gsc_h2f);
  806. int sg_out_nents, sg_in_nents;
  807. int i;
  808. struct scatterlist *sg;
  809. struct mei_ext_hdr_gsc_f2h rx_msg;
  810. unsigned int sg_len;
  811. if (!cldev || !sg_in || !sg_out)
  812. return -EINVAL;
  813. cl = cldev->cl;
  814. bus = cldev->bus;
  815. dev_dbg(&cldev->dev, "client_id %u, fence_id %u\n", client_id, fence_id);
  816. if (!bus->hbm_f_gsc_supported)
  817. return -EOPNOTSUPP;
  818. sg_out_nents = sg_nents(sg_out);
  819. sg_in_nents = sg_nents(sg_in);
  820. /* at least one entry in tx and rx sgls must be present */
  821. if (sg_out_nents <= 0 || sg_in_nents <= 0)
  822. return -EINVAL;
  823. buf_sz += (sg_out_nents + sg_in_nents) * sizeof(struct mei_gsc_sgl);
  824. ext_hdr = kzalloc(buf_sz, GFP_KERNEL);
  825. if (!ext_hdr)
  826. return -ENOMEM;
  827. /* construct the GSC message */
  828. ext_hdr->hdr.type = MEI_EXT_HDR_GSC;
  829. ext_hdr->hdr.length = buf_sz / sizeof(u32); /* length is in dw */
  830. ext_hdr->client_id = client_id;
  831. ext_hdr->addr_type = GSC_ADDRESS_TYPE_PHYSICAL_SGL;
  832. ext_hdr->fence_id = fence_id;
  833. ext_hdr->input_address_count = sg_in_nents;
  834. ext_hdr->output_address_count = sg_out_nents;
  835. ext_hdr->reserved[0] = 0;
  836. ext_hdr->reserved[1] = 0;
  837. /* copy in-sgl to the message */
  838. for (i = 0, sg = sg_in; i < sg_in_nents; i++, sg++) {
  839. ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
  840. ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
  841. sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
  842. ext_hdr->sgl[i].length = (sg_len <= total_in_len) ? sg_len : total_in_len;
  843. total_in_len -= ext_hdr->sgl[i].length;
  844. }
  845. /* copy out-sgl to the message */
  846. for (i = sg_in_nents, sg = sg_out; i < sg_in_nents + sg_out_nents; i++, sg++) {
  847. ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
  848. ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
  849. sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
  850. ext_hdr->sgl[i].length = sg_len;
  851. }
  852. /* send the message to GSC */
  853. ret = __mei_cl_send(cl, (u8 *)ext_hdr, buf_sz, 0, MEI_CL_IO_SGL);
  854. if (ret < 0) {
  855. dev_err(&cldev->dev, "__mei_cl_send failed, returned %zd\n", ret);
  856. goto end;
  857. }
  858. if (ret != buf_sz) {
  859. dev_err(&cldev->dev, "__mei_cl_send returned %zd instead of expected %zd\n",
  860. ret, buf_sz);
  861. ret = -EIO;
  862. goto end;
  863. }
  864. /* receive the reply from GSC, note that at this point sg_in should contain the reply */
  865. ret = __mei_cl_recv(cl, (u8 *)&rx_msg, sizeof(rx_msg), NULL, MEI_CL_IO_SGL, 0);
  866. if (ret != sizeof(rx_msg)) {
  867. dev_err(&cldev->dev, "__mei_cl_recv returned %zd instead of expected %zd\n",
  868. ret, sizeof(rx_msg));
  869. if (ret >= 0)
  870. ret = -EIO;
  871. goto end;
  872. }
  873. /* check rx_msg.client_id and rx_msg.fence_id match the ones we send */
  874. if (rx_msg.client_id != client_id || rx_msg.fence_id != fence_id) {
  875. dev_err(&cldev->dev, "received client_id/fence_id %u/%u instead of %u/%u sent\n",
  876. rx_msg.client_id, rx_msg.fence_id, client_id, fence_id);
  877. ret = -EFAULT;
  878. goto end;
  879. }
  880. dev_dbg(&cldev->dev, "gsc command: successfully written %u bytes\n", rx_msg.written);
  881. ret = rx_msg.written;
  882. end:
  883. kfree(ext_hdr);
  884. return ret;
  885. }
  886. EXPORT_SYMBOL_GPL(mei_cldev_send_gsc_command);
  887. /**
  888. * mei_cl_device_find - find matching entry in the driver id table
  889. *
  890. * @cldev: me client device
  891. * @cldrv: me client driver
  892. *
  893. * Return: id on success; NULL if no id is matching
  894. */
  895. static const
  896. struct mei_cl_device_id *mei_cl_device_find(const struct mei_cl_device *cldev,
  897. const struct mei_cl_driver *cldrv)
  898. {
  899. const struct mei_cl_device_id *id;
  900. const uuid_le *uuid;
  901. u8 version;
  902. bool match;
  903. uuid = mei_me_cl_uuid(cldev->me_cl);
  904. version = mei_me_cl_ver(cldev->me_cl);
  905. id = cldrv->id_table;
  906. while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
  907. if (!uuid_le_cmp(*uuid, id->uuid)) {
  908. match = true;
  909. if (cldev->name[0])
  910. if (strncmp(cldev->name, id->name,
  911. sizeof(id->name)))
  912. match = false;
  913. if (id->version != MEI_CL_VERSION_ANY)
  914. if (id->version != version)
  915. match = false;
  916. if (match)
  917. return id;
  918. }
  919. id++;
  920. }
  921. return NULL;
  922. }
  923. /**
  924. * mei_cl_device_match - device match function
  925. *
  926. * @dev: device
  927. * @drv: driver
  928. *
  929. * Return: 1 if matching device was found 0 otherwise
  930. */
  931. static int mei_cl_device_match(struct device *dev, const struct device_driver *drv)
  932. {
  933. const struct mei_cl_device *cldev = to_mei_cl_device(dev);
  934. const struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
  935. const struct mei_cl_device_id *found_id;
  936. if (!cldev->do_match)
  937. return 0;
  938. if (!cldrv || !cldrv->id_table)
  939. return 0;
  940. found_id = mei_cl_device_find(cldev, cldrv);
  941. if (found_id)
  942. return 1;
  943. return 0;
  944. }
  945. /**
  946. * mei_cl_device_probe - bus probe function
  947. *
  948. * @dev: device
  949. *
  950. * Return: 0 on success; < 0 otherwise
  951. */
  952. static int mei_cl_device_probe(struct device *dev)
  953. {
  954. struct mei_cl_device *cldev;
  955. struct mei_cl_driver *cldrv;
  956. const struct mei_cl_device_id *id;
  957. int ret;
  958. cldev = to_mei_cl_device(dev);
  959. cldrv = to_mei_cl_driver(dev->driver);
  960. if (!cldrv || !cldrv->probe)
  961. return -ENODEV;
  962. id = mei_cl_device_find(cldev, cldrv);
  963. if (!id)
  964. return -ENODEV;
  965. if (!mei_cl_bus_module_get(cldev)) {
  966. dev_err(&cldev->dev, "get hw module failed");
  967. return -ENODEV;
  968. }
  969. ret = cldrv->probe(cldev, id);
  970. if (ret) {
  971. mei_cl_bus_module_put(cldev);
  972. return ret;
  973. }
  974. __module_get(THIS_MODULE);
  975. return 0;
  976. }
  977. /**
  978. * mei_cl_device_remove - remove device from the bus
  979. *
  980. * @dev: device
  981. *
  982. * Return: 0 on success; < 0 otherwise
  983. */
  984. static void mei_cl_device_remove(struct device *dev)
  985. {
  986. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  987. struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver);
  988. if (cldrv->remove)
  989. cldrv->remove(cldev);
  990. mei_cldev_unregister_callbacks(cldev);
  991. mei_cl_bus_module_put(cldev);
  992. module_put(THIS_MODULE);
  993. }
  994. static ssize_t name_show(struct device *dev, struct device_attribute *a,
  995. char *buf)
  996. {
  997. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  998. return sysfs_emit(buf, "%s", cldev->name);
  999. }
  1000. static DEVICE_ATTR_RO(name);
  1001. static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
  1002. char *buf)
  1003. {
  1004. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1005. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  1006. return sysfs_emit(buf, "%pUl", uuid);
  1007. }
  1008. static DEVICE_ATTR_RO(uuid);
  1009. static ssize_t version_show(struct device *dev, struct device_attribute *a,
  1010. char *buf)
  1011. {
  1012. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1013. u8 version = mei_me_cl_ver(cldev->me_cl);
  1014. return sysfs_emit(buf, "%02X", version);
  1015. }
  1016. static DEVICE_ATTR_RO(version);
  1017. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  1018. char *buf)
  1019. {
  1020. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1021. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  1022. u8 version = mei_me_cl_ver(cldev->me_cl);
  1023. return sysfs_emit(buf, "mei:%s:%pUl:%02X:", cldev->name, uuid, version);
  1024. }
  1025. static DEVICE_ATTR_RO(modalias);
  1026. static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
  1027. char *buf)
  1028. {
  1029. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1030. u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
  1031. return sysfs_emit(buf, "%d", maxconn);
  1032. }
  1033. static DEVICE_ATTR_RO(max_conn);
  1034. static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
  1035. char *buf)
  1036. {
  1037. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1038. u8 fixed = mei_me_cl_fixed(cldev->me_cl);
  1039. return sysfs_emit(buf, "%d", fixed);
  1040. }
  1041. static DEVICE_ATTR_RO(fixed);
  1042. static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
  1043. char *buf)
  1044. {
  1045. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1046. bool vt = mei_me_cl_vt(cldev->me_cl);
  1047. return sysfs_emit(buf, "%d", vt);
  1048. }
  1049. static DEVICE_ATTR_RO(vtag);
  1050. static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
  1051. char *buf)
  1052. {
  1053. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1054. u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
  1055. return sysfs_emit(buf, "%u", maxlen);
  1056. }
  1057. static DEVICE_ATTR_RO(max_len);
  1058. static struct attribute *mei_cldev_attrs[] = {
  1059. &dev_attr_name.attr,
  1060. &dev_attr_uuid.attr,
  1061. &dev_attr_version.attr,
  1062. &dev_attr_modalias.attr,
  1063. &dev_attr_max_conn.attr,
  1064. &dev_attr_fixed.attr,
  1065. &dev_attr_vtag.attr,
  1066. &dev_attr_max_len.attr,
  1067. NULL,
  1068. };
  1069. ATTRIBUTE_GROUPS(mei_cldev);
  1070. /**
  1071. * mei_cl_device_uevent - me client bus uevent handler
  1072. *
  1073. * @dev: device
  1074. * @env: uevent kobject
  1075. *
  1076. * Return: 0 on success -ENOMEM on when add_uevent_var fails
  1077. */
  1078. static int mei_cl_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1079. {
  1080. const struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1081. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  1082. u8 version = mei_me_cl_ver(cldev->me_cl);
  1083. if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
  1084. return -ENOMEM;
  1085. if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
  1086. return -ENOMEM;
  1087. if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
  1088. return -ENOMEM;
  1089. if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
  1090. cldev->name, uuid, version))
  1091. return -ENOMEM;
  1092. return 0;
  1093. }
  1094. static const struct bus_type mei_cl_bus_type = {
  1095. .name = "mei",
  1096. .dev_groups = mei_cldev_groups,
  1097. .match = mei_cl_device_match,
  1098. .probe = mei_cl_device_probe,
  1099. .remove = mei_cl_device_remove,
  1100. .uevent = mei_cl_device_uevent,
  1101. };
  1102. static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
  1103. {
  1104. if (bus) {
  1105. get_device(&bus->dev);
  1106. get_device(bus->parent);
  1107. }
  1108. return bus;
  1109. }
  1110. static void mei_dev_bus_put(struct mei_device *bus)
  1111. {
  1112. if (bus) {
  1113. put_device(bus->parent);
  1114. put_device(&bus->dev);
  1115. }
  1116. }
  1117. static void mei_cl_bus_dev_release(struct device *dev)
  1118. {
  1119. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  1120. struct mei_device *mdev = cldev->cl->dev;
  1121. struct mei_cl *cl;
  1122. mei_cl_flush_queues(cldev->cl, NULL);
  1123. mei_me_cl_put(cldev->me_cl);
  1124. mei_dev_bus_put(cldev->bus);
  1125. list_for_each_entry(cl, &mdev->file_list, link)
  1126. WARN_ON(cl == cldev->cl);
  1127. kfree(cldev->cl);
  1128. kfree(cldev);
  1129. }
  1130. static const struct device_type mei_cl_device_type = {
  1131. .release = mei_cl_bus_dev_release,
  1132. };
  1133. /**
  1134. * mei_cl_bus_set_name - set device name for me client device
  1135. * <controller>-<client device>
  1136. * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
  1137. *
  1138. * @cldev: me client device
  1139. */
  1140. static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
  1141. {
  1142. dev_set_name(&cldev->dev, "%s-%pUl",
  1143. dev_name(cldev->bus->parent),
  1144. mei_me_cl_uuid(cldev->me_cl));
  1145. }
  1146. /**
  1147. * mei_cl_bus_dev_alloc - initialize and allocate mei client device
  1148. *
  1149. * @bus: mei device
  1150. * @me_cl: me client
  1151. *
  1152. * Return: allocated device structure or NULL on allocation failure
  1153. */
  1154. static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
  1155. struct mei_me_client *me_cl)
  1156. {
  1157. struct mei_cl_device *cldev;
  1158. struct mei_cl *cl;
  1159. cldev = kzalloc_obj(*cldev);
  1160. if (!cldev)
  1161. return NULL;
  1162. cl = mei_cl_allocate(bus);
  1163. if (!cl) {
  1164. kfree(cldev);
  1165. return NULL;
  1166. }
  1167. device_initialize(&cldev->dev);
  1168. cldev->dev.parent = bus->parent;
  1169. cldev->dev.bus = &mei_cl_bus_type;
  1170. cldev->dev.type = &mei_cl_device_type;
  1171. cldev->bus = mei_dev_bus_get(bus);
  1172. cldev->me_cl = mei_me_cl_get(me_cl);
  1173. cldev->cl = cl;
  1174. mei_cl_bus_set_name(cldev);
  1175. cldev->is_added = 0;
  1176. INIT_LIST_HEAD(&cldev->bus_list);
  1177. device_enable_async_suspend(&cldev->dev);
  1178. return cldev;
  1179. }
  1180. /**
  1181. * mei_cl_bus_dev_setup - setup me client device
  1182. * run fix up routines and set the device name
  1183. *
  1184. * @bus: mei device
  1185. * @cldev: me client device
  1186. *
  1187. * Return: true if the device is eligible for enumeration
  1188. */
  1189. static bool mei_cl_bus_dev_setup(struct mei_device *bus,
  1190. struct mei_cl_device *cldev)
  1191. {
  1192. cldev->do_match = 1;
  1193. mei_cl_bus_dev_fixup(cldev);
  1194. /* the device name can change during fix up */
  1195. if (cldev->do_match)
  1196. mei_cl_bus_set_name(cldev);
  1197. return cldev->do_match == 1;
  1198. }
  1199. /**
  1200. * mei_cl_bus_dev_add - add me client devices
  1201. *
  1202. * @cldev: me client device
  1203. *
  1204. * Return: 0 on success; < 0 on failure
  1205. */
  1206. static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
  1207. {
  1208. int ret;
  1209. dev_dbg(&cldev->dev, "adding %pUL:%02X\n",
  1210. mei_me_cl_uuid(cldev->me_cl),
  1211. mei_me_cl_ver(cldev->me_cl));
  1212. ret = device_add(&cldev->dev);
  1213. if (!ret)
  1214. cldev->is_added = 1;
  1215. return ret;
  1216. }
  1217. /**
  1218. * mei_cl_bus_dev_stop - stop the driver
  1219. *
  1220. * @cldev: me client device
  1221. */
  1222. static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
  1223. {
  1224. cldev->do_match = 0;
  1225. if (cldev->is_added)
  1226. device_release_driver(&cldev->dev);
  1227. }
  1228. /**
  1229. * mei_cl_bus_dev_destroy - destroy me client devices object
  1230. *
  1231. * @cldev: me client device
  1232. *
  1233. * Locking: called under "dev->cl_bus_lock" lock
  1234. */
  1235. static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
  1236. {
  1237. WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
  1238. if (!cldev->is_added)
  1239. return;
  1240. device_del(&cldev->dev);
  1241. list_del_init(&cldev->bus_list);
  1242. cldev->is_added = 0;
  1243. put_device(&cldev->dev);
  1244. }
  1245. /**
  1246. * mei_cl_bus_remove_device - remove a devices form the bus
  1247. *
  1248. * @cldev: me client device
  1249. */
  1250. static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
  1251. {
  1252. mei_cl_bus_dev_stop(cldev);
  1253. mei_cl_bus_dev_destroy(cldev);
  1254. }
  1255. /**
  1256. * mei_cl_bus_remove_devices - remove all devices form the bus
  1257. *
  1258. * @bus: mei device
  1259. */
  1260. void mei_cl_bus_remove_devices(struct mei_device *bus)
  1261. {
  1262. struct mei_cl_device *cldev, *next;
  1263. mutex_lock(&bus->cl_bus_lock);
  1264. list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
  1265. mei_cl_bus_remove_device(cldev);
  1266. mutex_unlock(&bus->cl_bus_lock);
  1267. }
  1268. /**
  1269. * mei_cl_bus_dev_init - allocate and initializes an mei client devices
  1270. * based on me client
  1271. *
  1272. * @bus: mei device
  1273. * @me_cl: me client
  1274. *
  1275. * Locking: called under "dev->cl_bus_lock" lock
  1276. */
  1277. static void mei_cl_bus_dev_init(struct mei_device *bus,
  1278. struct mei_me_client *me_cl)
  1279. {
  1280. struct mei_cl_device *cldev;
  1281. WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
  1282. dev_dbg(&bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
  1283. if (me_cl->bus_added)
  1284. return;
  1285. cldev = mei_cl_bus_dev_alloc(bus, me_cl);
  1286. if (!cldev)
  1287. return;
  1288. me_cl->bus_added = true;
  1289. list_add_tail(&cldev->bus_list, &bus->device_list);
  1290. }
  1291. /**
  1292. * mei_cl_bus_rescan - scan me clients list and add create
  1293. * devices for eligible clients
  1294. *
  1295. * @bus: mei device
  1296. */
  1297. static void mei_cl_bus_rescan(struct mei_device *bus)
  1298. {
  1299. struct mei_cl_device *cldev, *n;
  1300. struct mei_me_client *me_cl;
  1301. mutex_lock(&bus->cl_bus_lock);
  1302. down_read(&bus->me_clients_rwsem);
  1303. list_for_each_entry(me_cl, &bus->me_clients, list)
  1304. mei_cl_bus_dev_init(bus, me_cl);
  1305. up_read(&bus->me_clients_rwsem);
  1306. list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
  1307. if (!mei_me_cl_is_active(cldev->me_cl)) {
  1308. mei_cl_bus_remove_device(cldev);
  1309. continue;
  1310. }
  1311. if (cldev->is_added)
  1312. continue;
  1313. if (mei_cl_bus_dev_setup(bus, cldev))
  1314. mei_cl_bus_dev_add(cldev);
  1315. else {
  1316. list_del_init(&cldev->bus_list);
  1317. put_device(&cldev->dev);
  1318. }
  1319. }
  1320. mutex_unlock(&bus->cl_bus_lock);
  1321. dev_dbg(&bus->dev, "rescan end");
  1322. }
  1323. void mei_cl_bus_rescan_work(struct work_struct *work)
  1324. {
  1325. struct mei_device *bus =
  1326. container_of(work, struct mei_device, bus_rescan_work);
  1327. mei_cl_bus_rescan(bus);
  1328. }
  1329. int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
  1330. struct module *owner)
  1331. {
  1332. int err;
  1333. cldrv->driver.name = cldrv->name;
  1334. cldrv->driver.owner = owner;
  1335. cldrv->driver.bus = &mei_cl_bus_type;
  1336. err = driver_register(&cldrv->driver);
  1337. if (err)
  1338. return err;
  1339. pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
  1343. void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
  1344. {
  1345. driver_unregister(&cldrv->driver);
  1346. pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
  1347. }
  1348. EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
  1349. int __init mei_cl_bus_init(void)
  1350. {
  1351. return bus_register(&mei_cl_bus_type);
  1352. }
  1353. void __exit mei_cl_bus_exit(void)
  1354. {
  1355. bus_unregister(&mei_cl_bus_type);
  1356. }