bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ISHTP bus driver
  4. *
  5. * Copyright (c) 2012-2016, Intel Corporation.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include "bus.h"
  14. #include "ishtp-dev.h"
  15. #include "client.h"
  16. #include "hbm.h"
  17. static int ishtp_use_dma;
  18. module_param_named(ishtp_use_dma, ishtp_use_dma, int, 0600);
  19. MODULE_PARM_DESC(ishtp_use_dma, "Use DMA to send messages");
  20. #define to_ishtp_cl_driver(d) container_of(d, struct ishtp_cl_driver, driver)
  21. #define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
  22. static bool ishtp_device_ready;
  23. /**
  24. * ishtp_recv() - process ishtp message
  25. * @dev: ishtp device
  26. *
  27. * If a message with valid header and size is received, then
  28. * this function calls appropriate handler. The host or firmware
  29. * address is zero, then they are host bus management message,
  30. * otherwise they are message fo clients.
  31. */
  32. void ishtp_recv(struct ishtp_device *dev)
  33. {
  34. uint32_t msg_hdr;
  35. struct ishtp_msg_hdr *ishtp_hdr;
  36. /* Read ISHTP header dword */
  37. msg_hdr = dev->ops->ishtp_read_hdr(dev);
  38. if (!msg_hdr)
  39. return;
  40. dev->ops->sync_fw_clock(dev);
  41. ishtp_hdr = (struct ishtp_msg_hdr *)&msg_hdr;
  42. dev->ishtp_msg_hdr = msg_hdr;
  43. /* Sanity check: ISHTP frag. length in header */
  44. if (ishtp_hdr->length > dev->mtu) {
  45. dev_err(dev->devc,
  46. "ISHTP hdr - bad length: %u; dropped [%08X]\n",
  47. (unsigned int)ishtp_hdr->length, msg_hdr);
  48. return;
  49. }
  50. /* ISHTP bus message */
  51. if (!ishtp_hdr->host_addr && !ishtp_hdr->fw_addr)
  52. recv_hbm(dev, ishtp_hdr);
  53. /* ISHTP fixed-client message */
  54. else if (!ishtp_hdr->host_addr)
  55. recv_fixed_cl_msg(dev, ishtp_hdr);
  56. else
  57. /* ISHTP client message */
  58. recv_ishtp_cl_msg(dev, ishtp_hdr);
  59. }
  60. EXPORT_SYMBOL(ishtp_recv);
  61. /**
  62. * ishtp_send_msg() - Send ishtp message
  63. * @dev: ishtp device
  64. * @hdr: Message header
  65. * @msg: Message contents
  66. * @ipc_send_compl: completion callback
  67. * @ipc_send_compl_prm: completion callback parameter
  68. *
  69. * Send a multi fragment message via IPC. After sending the first fragment
  70. * the completion callback is called to schedule transmit of next fragment.
  71. *
  72. * Return: This returns IPC send message status.
  73. */
  74. int ishtp_send_msg(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
  75. void *msg, void(*ipc_send_compl)(void *),
  76. void *ipc_send_compl_prm)
  77. {
  78. unsigned char ipc_msg[IPC_FULL_MSG_SIZE];
  79. uint32_t drbl_val;
  80. drbl_val = dev->ops->ipc_get_header(dev, hdr->length +
  81. sizeof(struct ishtp_msg_hdr),
  82. 1);
  83. memcpy(ipc_msg, &drbl_val, sizeof(uint32_t));
  84. memcpy(ipc_msg + sizeof(uint32_t), hdr, sizeof(uint32_t));
  85. memcpy(ipc_msg + 2 * sizeof(uint32_t), msg, hdr->length);
  86. return dev->ops->write(dev, ipc_send_compl, ipc_send_compl_prm,
  87. ipc_msg, 2 * sizeof(uint32_t) + hdr->length);
  88. }
  89. /**
  90. * ishtp_write_message() - Send ishtp single fragment message
  91. * @dev: ishtp device
  92. * @hdr: Message header
  93. * @buf: message data
  94. *
  95. * Send a single fragment message via IPC. This returns IPC send message
  96. * status.
  97. *
  98. * Return: This returns IPC send message status.
  99. */
  100. int ishtp_write_message(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
  101. void *buf)
  102. {
  103. return ishtp_send_msg(dev, hdr, buf, NULL, NULL);
  104. }
  105. /**
  106. * ishtp_fw_cl_by_uuid() - locate index of fw client
  107. * @dev: ishtp device
  108. * @uuid: uuid of the client to search
  109. *
  110. * Search firmware client using UUID.
  111. *
  112. * Return: fw client index or -ENOENT if not found
  113. */
  114. int ishtp_fw_cl_by_uuid(struct ishtp_device *dev, const guid_t *uuid)
  115. {
  116. unsigned int i;
  117. for (i = 0; i < dev->fw_clients_num; ++i) {
  118. if (guid_equal(uuid, &dev->fw_clients[i].props.protocol_name))
  119. return i;
  120. }
  121. return -ENOENT;
  122. }
  123. EXPORT_SYMBOL(ishtp_fw_cl_by_uuid);
  124. /**
  125. * ishtp_fw_cl_get_client() - return client information to client
  126. * @dev: the ishtp device structure
  127. * @uuid: uuid of the client to search
  128. *
  129. * Search firmware client using UUID and reture related client information.
  130. *
  131. * Return: pointer of client information on success, NULL on failure.
  132. */
  133. struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev,
  134. const guid_t *uuid)
  135. {
  136. int i;
  137. unsigned long flags;
  138. spin_lock_irqsave(&dev->fw_clients_lock, flags);
  139. i = ishtp_fw_cl_by_uuid(dev, uuid);
  140. spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
  141. if (i < 0 || dev->fw_clients[i].props.fixed_address)
  142. return NULL;
  143. return &dev->fw_clients[i];
  144. }
  145. EXPORT_SYMBOL(ishtp_fw_cl_get_client);
  146. /**
  147. * ishtp_get_fw_client_id() - Get fw client id
  148. * @fw_client: firmware client used to fetch the ID
  149. *
  150. * This interface is used to reset HW get FW client id.
  151. *
  152. * Return: firmware client id.
  153. */
  154. int ishtp_get_fw_client_id(struct ishtp_fw_client *fw_client)
  155. {
  156. return fw_client->client_id;
  157. }
  158. EXPORT_SYMBOL(ishtp_get_fw_client_id);
  159. /**
  160. * ishtp_fw_cl_by_id() - return index to fw_clients for client_id
  161. * @dev: the ishtp device structure
  162. * @client_id: fw client id to search
  163. *
  164. * Search firmware client using client id.
  165. *
  166. * Return: index on success, -ENOENT on failure.
  167. */
  168. int ishtp_fw_cl_by_id(struct ishtp_device *dev, uint8_t client_id)
  169. {
  170. int i, res = -ENOENT;
  171. unsigned long flags;
  172. spin_lock_irqsave(&dev->fw_clients_lock, flags);
  173. for (i = 0; i < dev->fw_clients_num; i++) {
  174. if (dev->fw_clients[i].client_id == client_id) {
  175. res = i;
  176. break;
  177. }
  178. }
  179. spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
  180. return res;
  181. }
  182. /**
  183. * ishtp_cl_device_probe() - Bus probe() callback
  184. * @dev: the device structure
  185. *
  186. * This is a bus probe callback and calls the drive probe function.
  187. *
  188. * Return: Return value from driver probe() call.
  189. */
  190. static int ishtp_cl_device_probe(struct device *dev)
  191. {
  192. struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
  193. struct ishtp_cl_driver *driver;
  194. if (!device)
  195. return 0;
  196. driver = to_ishtp_cl_driver(dev->driver);
  197. if (!driver || !driver->probe)
  198. return -ENODEV;
  199. return driver->probe(device);
  200. }
  201. /**
  202. * ishtp_cl_bus_match() - Bus match() callback
  203. * @dev: the device structure
  204. * @drv: the driver structure
  205. *
  206. * This is a bus match callback, called when a new ishtp_cl_device is
  207. * registered during ishtp bus client enumeration. Use the guid_t in
  208. * drv and dev to decide whether they match or not.
  209. *
  210. * Return: 1 if dev & drv matches, 0 otherwise.
  211. */
  212. static int ishtp_cl_bus_match(struct device *dev, struct device_driver *drv)
  213. {
  214. struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
  215. struct ishtp_cl_driver *driver = to_ishtp_cl_driver(drv);
  216. return(device->fw_client ? guid_equal(&driver->id[0].guid,
  217. &device->fw_client->props.protocol_name) : 0);
  218. }
  219. /**
  220. * ishtp_cl_device_remove() - Bus remove() callback
  221. * @dev: the device structure
  222. *
  223. * This is a bus remove callback and calls the drive remove function.
  224. * Since the ISH driver model supports only built in, this is
  225. * primarily can be called during pci driver init failure.
  226. *
  227. * Return: Return value from driver remove() call.
  228. */
  229. static void ishtp_cl_device_remove(struct device *dev)
  230. {
  231. struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
  232. struct ishtp_cl_driver *driver = to_ishtp_cl_driver(dev->driver);
  233. if (device->event_cb) {
  234. device->event_cb = NULL;
  235. cancel_work_sync(&device->event_work);
  236. }
  237. if (driver->remove)
  238. driver->remove(device);
  239. }
  240. /**
  241. * ishtp_cl_device_suspend() - Bus suspend callback
  242. * @dev: device
  243. *
  244. * Called during device suspend process.
  245. *
  246. * Return: Return value from driver suspend() call.
  247. */
  248. static int ishtp_cl_device_suspend(struct device *dev)
  249. {
  250. struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
  251. struct ishtp_cl_driver *driver;
  252. int ret = 0;
  253. if (!device)
  254. return 0;
  255. driver = to_ishtp_cl_driver(dev->driver);
  256. if (driver && driver->driver.pm) {
  257. if (driver->driver.pm->suspend)
  258. ret = driver->driver.pm->suspend(dev);
  259. }
  260. return ret;
  261. }
  262. /**
  263. * ishtp_cl_device_resume() - Bus resume callback
  264. * @dev: device
  265. *
  266. * Called during device resume process.
  267. *
  268. * Return: Return value from driver resume() call.
  269. */
  270. static int ishtp_cl_device_resume(struct device *dev)
  271. {
  272. struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
  273. struct ishtp_cl_driver *driver;
  274. int ret = 0;
  275. if (!device)
  276. return 0;
  277. driver = to_ishtp_cl_driver(dev->driver);
  278. if (driver && driver->driver.pm) {
  279. if (driver->driver.pm->resume)
  280. ret = driver->driver.pm->resume(dev);
  281. }
  282. return ret;
  283. }
  284. /**
  285. * ishtp_cl_device_reset() - Reset callback
  286. * @device: ishtp client device instance
  287. *
  288. * This is a callback when HW reset is done and the device need
  289. * reinit.
  290. *
  291. * Return: Return value from driver reset() call.
  292. */
  293. static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
  294. {
  295. struct ishtp_cl_driver *driver;
  296. int ret = 0;
  297. device->event_cb = NULL;
  298. cancel_work_sync(&device->event_work);
  299. driver = to_ishtp_cl_driver(device->dev.driver);
  300. if (driver && driver->reset)
  301. ret = driver->reset(device);
  302. return ret;
  303. }
  304. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  305. char *buf)
  306. {
  307. int len;
  308. len = snprintf(buf, PAGE_SIZE, ISHTP_MODULE_PREFIX "%s\n", dev_name(dev));
  309. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  310. }
  311. static DEVICE_ATTR_RO(modalias);
  312. static struct attribute *ishtp_cl_dev_attrs[] = {
  313. &dev_attr_modalias.attr,
  314. NULL,
  315. };
  316. ATTRIBUTE_GROUPS(ishtp_cl_dev);
  317. static int ishtp_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
  318. {
  319. if (add_uevent_var(env, "MODALIAS=" ISHTP_MODULE_PREFIX "%s", dev_name(dev)))
  320. return -ENOMEM;
  321. return 0;
  322. }
  323. static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
  324. /* Suspend callbacks */
  325. .suspend = ishtp_cl_device_suspend,
  326. .resume = ishtp_cl_device_resume,
  327. /* Hibernate callbacks */
  328. .freeze = ishtp_cl_device_suspend,
  329. .thaw = ishtp_cl_device_resume,
  330. .restore = ishtp_cl_device_resume,
  331. };
  332. static struct bus_type ishtp_cl_bus_type = {
  333. .name = "ishtp",
  334. .dev_groups = ishtp_cl_dev_groups,
  335. .probe = ishtp_cl_device_probe,
  336. .match = ishtp_cl_bus_match,
  337. .remove = ishtp_cl_device_remove,
  338. .pm = &ishtp_cl_bus_dev_pm_ops,
  339. .uevent = ishtp_cl_uevent,
  340. };
  341. static void ishtp_cl_dev_release(struct device *dev)
  342. {
  343. kfree(to_ishtp_cl_device(dev));
  344. }
  345. static const struct device_type ishtp_cl_device_type = {
  346. .release = ishtp_cl_dev_release,
  347. };
  348. /**
  349. * ishtp_bus_add_device() - Function to create device on bus
  350. * @dev: ishtp device
  351. * @uuid: uuid of the client
  352. * @name: Name of the client
  353. *
  354. * Allocate ISHTP bus client device, attach it to uuid
  355. * and register with ISHTP bus.
  356. *
  357. * Return: ishtp_cl_device pointer or NULL on failure
  358. */
  359. static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
  360. guid_t uuid, char *name)
  361. {
  362. struct ishtp_cl_device *device;
  363. int status;
  364. unsigned long flags;
  365. spin_lock_irqsave(&dev->device_list_lock, flags);
  366. list_for_each_entry(device, &dev->device_list, device_link) {
  367. if (!strcmp(name, dev_name(&device->dev))) {
  368. device->fw_client = &dev->fw_clients[
  369. dev->fw_client_presentation_num - 1];
  370. spin_unlock_irqrestore(&dev->device_list_lock, flags);
  371. ishtp_cl_device_reset(device);
  372. return device;
  373. }
  374. }
  375. spin_unlock_irqrestore(&dev->device_list_lock, flags);
  376. device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
  377. if (!device)
  378. return NULL;
  379. device->dev.parent = dev->devc;
  380. device->dev.bus = &ishtp_cl_bus_type;
  381. device->dev.type = &ishtp_cl_device_type;
  382. device->ishtp_dev = dev;
  383. device->fw_client =
  384. &dev->fw_clients[dev->fw_client_presentation_num - 1];
  385. dev_set_name(&device->dev, "%s", name);
  386. spin_lock_irqsave(&dev->device_list_lock, flags);
  387. list_add_tail(&device->device_link, &dev->device_list);
  388. spin_unlock_irqrestore(&dev->device_list_lock, flags);
  389. status = device_register(&device->dev);
  390. if (status) {
  391. spin_lock_irqsave(&dev->device_list_lock, flags);
  392. list_del(&device->device_link);
  393. spin_unlock_irqrestore(&dev->device_list_lock, flags);
  394. dev_err(dev->devc, "Failed to register ISHTP client device\n");
  395. put_device(&device->dev);
  396. return NULL;
  397. }
  398. ishtp_device_ready = true;
  399. return device;
  400. }
  401. /**
  402. * ishtp_bus_remove_device() - Function to relase device on bus
  403. * @device: client device instance
  404. *
  405. * This is a counterpart of ishtp_bus_add_device.
  406. * Device is unregistered.
  407. * the device structure is freed in 'ishtp_cl_dev_release' function
  408. * Called only during error in pci driver init path.
  409. */
  410. static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
  411. {
  412. device_unregister(&device->dev);
  413. }
  414. /**
  415. * ishtp_cl_driver_register() - Client driver register
  416. * @driver: the client driver instance
  417. * @owner: Owner of this driver module
  418. *
  419. * Once a client driver is probed, it created a client
  420. * instance and registers with the bus.
  421. *
  422. * Return: Return value of driver_register or -ENODEV if not ready
  423. */
  424. int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
  425. struct module *owner)
  426. {
  427. if (!ishtp_device_ready)
  428. return -ENODEV;
  429. driver->driver.name = driver->name;
  430. driver->driver.owner = owner;
  431. driver->driver.bus = &ishtp_cl_bus_type;
  432. return driver_register(&driver->driver);
  433. }
  434. EXPORT_SYMBOL(ishtp_cl_driver_register);
  435. /**
  436. * ishtp_cl_driver_unregister() - Client driver unregister
  437. * @driver: the client driver instance
  438. *
  439. * Unregister client during device removal process.
  440. */
  441. void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
  442. {
  443. driver_unregister(&driver->driver);
  444. }
  445. EXPORT_SYMBOL(ishtp_cl_driver_unregister);
  446. /**
  447. * ishtp_bus_event_work() - event work function
  448. * @work: work struct pointer
  449. *
  450. * Once an event is received for a client this work
  451. * function is called. If the device has registered a
  452. * callback then the callback is called.
  453. */
  454. static void ishtp_bus_event_work(struct work_struct *work)
  455. {
  456. struct ishtp_cl_device *device;
  457. device = container_of(work, struct ishtp_cl_device, event_work);
  458. if (device->event_cb)
  459. device->event_cb(device);
  460. }
  461. /**
  462. * ishtp_cl_bus_rx_event() - schedule event work
  463. * @device: client device instance
  464. *
  465. * Once an event is received for a client this schedules
  466. * a work function to process.
  467. */
  468. void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
  469. {
  470. if (!device || !device->event_cb)
  471. return;
  472. if (device->event_cb)
  473. schedule_work(&device->event_work);
  474. }
  475. /**
  476. * ishtp_register_event_cb() - Register callback
  477. * @device: client device instance
  478. * @event_cb: Event processor for an client
  479. *
  480. * Register a callback for events, called from client driver
  481. *
  482. * Return: Return 0 or -EALREADY if already registered
  483. */
  484. int ishtp_register_event_cb(struct ishtp_cl_device *device,
  485. void (*event_cb)(struct ishtp_cl_device *))
  486. {
  487. if (device->event_cb)
  488. return -EALREADY;
  489. device->event_cb = event_cb;
  490. INIT_WORK(&device->event_work, ishtp_bus_event_work);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(ishtp_register_event_cb);
  494. /**
  495. * ishtp_get_device() - update usage count for the device
  496. * @cl_device: client device instance
  497. *
  498. * Increment the usage count. The device can't be deleted
  499. */
  500. void ishtp_get_device(struct ishtp_cl_device *cl_device)
  501. {
  502. cl_device->reference_count++;
  503. }
  504. EXPORT_SYMBOL(ishtp_get_device);
  505. /**
  506. * ishtp_put_device() - decrement usage count for the device
  507. * @cl_device: client device instance
  508. *
  509. * Decrement the usage count. The device can be deleted is count = 0
  510. */
  511. void ishtp_put_device(struct ishtp_cl_device *cl_device)
  512. {
  513. cl_device->reference_count--;
  514. }
  515. EXPORT_SYMBOL(ishtp_put_device);
  516. /**
  517. * ishtp_set_drvdata() - set client driver data
  518. * @cl_device: client device instance
  519. * @data: driver data need to be set
  520. *
  521. * Set client driver data to cl_device->driver_data.
  522. */
  523. void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
  524. {
  525. cl_device->driver_data = data;
  526. }
  527. EXPORT_SYMBOL(ishtp_set_drvdata);
  528. /**
  529. * ishtp_get_drvdata() - get client driver data
  530. * @cl_device: client device instance
  531. *
  532. * Get client driver data from cl_device->driver_data.
  533. *
  534. * Return: pointer of driver data
  535. */
  536. void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
  537. {
  538. return cl_device->driver_data;
  539. }
  540. EXPORT_SYMBOL(ishtp_get_drvdata);
  541. /**
  542. * ishtp_dev_to_cl_device() - get ishtp_cl_device instance from device instance
  543. * @device: device instance
  544. *
  545. * Get ish_cl_device instance which embeds device instance in it.
  546. *
  547. * Return: pointer to ishtp_cl_device instance
  548. */
  549. struct ishtp_cl_device *ishtp_dev_to_cl_device(struct device *device)
  550. {
  551. return to_ishtp_cl_device(device);
  552. }
  553. EXPORT_SYMBOL(ishtp_dev_to_cl_device);
  554. /**
  555. * ishtp_bus_new_client() - Create a new client
  556. * @dev: ISHTP device instance
  557. *
  558. * Once bus protocol enumerates a client, this is called
  559. * to add a device for the client.
  560. *
  561. * Return: 0 on success or error code on failure
  562. */
  563. int ishtp_bus_new_client(struct ishtp_device *dev)
  564. {
  565. int i;
  566. char *dev_name;
  567. struct ishtp_cl_device *cl_device;
  568. guid_t device_uuid;
  569. /*
  570. * For all reported clients, create an unconnected client and add its
  571. * device to ISHTP bus.
  572. * If appropriate driver has loaded, this will trigger its probe().
  573. * Otherwise, probe() will be called when driver is loaded
  574. */
  575. i = dev->fw_client_presentation_num - 1;
  576. device_uuid = dev->fw_clients[i].props.protocol_name;
  577. dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
  578. if (!dev_name)
  579. return -ENOMEM;
  580. cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
  581. if (!cl_device) {
  582. kfree(dev_name);
  583. return -ENOENT;
  584. }
  585. kfree(dev_name);
  586. return 0;
  587. }
  588. /**
  589. * ishtp_cl_device_bind() - bind a device
  590. * @cl: ishtp client device
  591. *
  592. * Binds connected ishtp_cl to ISHTP bus device
  593. *
  594. * Return: 0 on success or fault code
  595. */
  596. int ishtp_cl_device_bind(struct ishtp_cl *cl)
  597. {
  598. struct ishtp_cl_device *cl_device;
  599. unsigned long flags;
  600. int rv;
  601. if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
  602. return -EFAULT;
  603. rv = -ENOENT;
  604. spin_lock_irqsave(&cl->dev->device_list_lock, flags);
  605. list_for_each_entry(cl_device, &cl->dev->device_list,
  606. device_link) {
  607. if (cl_device->fw_client &&
  608. cl_device->fw_client->client_id == cl->fw_client_id) {
  609. cl->device = cl_device;
  610. rv = 0;
  611. break;
  612. }
  613. }
  614. spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
  615. return rv;
  616. }
  617. /**
  618. * ishtp_bus_remove_all_clients() - Remove all clients
  619. * @ishtp_dev: ishtp device
  620. * @warm_reset: Reset due to FW reset dure to errors or S3 suspend
  621. *
  622. * This is part of reset/remove flow. This function the main processing
  623. * only targets error processing, if the FW has forced reset or
  624. * error to remove connected clients. When warm reset the client devices are
  625. * not removed.
  626. */
  627. void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
  628. bool warm_reset)
  629. {
  630. struct ishtp_cl_device *cl_device, *n;
  631. struct ishtp_cl *cl;
  632. unsigned long flags;
  633. spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
  634. list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
  635. cl->state = ISHTP_CL_DISCONNECTED;
  636. /*
  637. * Wake any pending process. The waiter would check dev->state
  638. * and determine that it's not enabled already,
  639. * and will return error to its caller
  640. */
  641. wake_up_interruptible(&cl->wait_ctrl_res);
  642. /* Disband any pending read/write requests and free rb */
  643. ishtp_cl_flush_queues(cl);
  644. /* Remove all free and in_process rings, both Rx and Tx */
  645. ishtp_cl_free_rx_ring(cl);
  646. ishtp_cl_free_tx_ring(cl);
  647. /*
  648. * Free client and ISHTP bus client device structures
  649. * don't free host client because it is part of the OS fd
  650. * structure
  651. */
  652. }
  653. spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
  654. /* Release DMA buffers for client messages */
  655. ishtp_cl_free_dma_buf(ishtp_dev);
  656. /* remove bus clients */
  657. spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
  658. list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
  659. device_link) {
  660. cl_device->fw_client = NULL;
  661. if (warm_reset && cl_device->reference_count)
  662. continue;
  663. list_del(&cl_device->device_link);
  664. spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
  665. ishtp_bus_remove_device(cl_device);
  666. spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
  667. }
  668. spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
  669. /* Free all client structures */
  670. spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
  671. kfree(ishtp_dev->fw_clients);
  672. ishtp_dev->fw_clients = NULL;
  673. ishtp_dev->fw_clients_num = 0;
  674. ishtp_dev->fw_client_presentation_num = 0;
  675. ishtp_dev->fw_client_index = 0;
  676. bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
  677. spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
  678. }
  679. EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
  680. /**
  681. * ishtp_reset_handler() - IPC reset handler
  682. * @dev: ishtp device
  683. *
  684. * ISHTP Handler for IPC_RESET notification
  685. */
  686. void ishtp_reset_handler(struct ishtp_device *dev)
  687. {
  688. unsigned long flags;
  689. /* Handle FW-initiated reset */
  690. dev->dev_state = ISHTP_DEV_RESETTING;
  691. /* Clear BH processing queue - no further HBMs */
  692. spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
  693. dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
  694. spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
  695. /* Handle ISH FW reset against upper layers */
  696. ishtp_bus_remove_all_clients(dev, true);
  697. }
  698. EXPORT_SYMBOL(ishtp_reset_handler);
  699. /**
  700. * ishtp_reset_compl_handler() - Reset completion handler
  701. * @dev: ishtp device
  702. *
  703. * ISHTP handler for IPC_RESET sequence completion to start
  704. * host message bus start protocol sequence.
  705. */
  706. void ishtp_reset_compl_handler(struct ishtp_device *dev)
  707. {
  708. dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
  709. dev->hbm_state = ISHTP_HBM_START;
  710. ishtp_hbm_start_req(dev);
  711. }
  712. EXPORT_SYMBOL(ishtp_reset_compl_handler);
  713. /**
  714. * ishtp_use_dma_transfer() - Function to use DMA
  715. *
  716. * This interface is used to enable usage of DMA
  717. *
  718. * Return non zero if DMA can be enabled
  719. */
  720. int ishtp_use_dma_transfer(void)
  721. {
  722. return ishtp_use_dma;
  723. }
  724. /**
  725. * ishtp_device() - Return device pointer
  726. * @device: ISH-TP client device instance
  727. *
  728. * This interface is used to return device pointer from ishtp_cl_device
  729. * instance.
  730. *
  731. * Return: device *.
  732. */
  733. struct device *ishtp_device(struct ishtp_cl_device *device)
  734. {
  735. return &device->dev;
  736. }
  737. EXPORT_SYMBOL(ishtp_device);
  738. /**
  739. * ishtp_wait_resume() - Wait for IPC resume
  740. *
  741. * Wait for IPC resume
  742. *
  743. * Return: resume complete or not
  744. */
  745. bool ishtp_wait_resume(struct ishtp_device *dev)
  746. {
  747. /* 50ms to get resume response */
  748. #define WAIT_FOR_RESUME_ACK_MS 50
  749. /* Waiting to get resume response */
  750. if (dev->resume_flag)
  751. wait_event_interruptible_timeout(dev->resume_wait,
  752. !dev->resume_flag,
  753. msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
  754. return (!dev->resume_flag);
  755. }
  756. EXPORT_SYMBOL_GPL(ishtp_wait_resume);
  757. /**
  758. * ishtp_get_pci_device() - Return PCI device dev pointer
  759. * This interface is used to return PCI device pointer
  760. * from ishtp_cl_device instance.
  761. * @device: ISH-TP client device instance
  762. *
  763. * Return: device *.
  764. */
  765. struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
  766. {
  767. return device->ishtp_dev->devc;
  768. }
  769. EXPORT_SYMBOL(ishtp_get_pci_device);
  770. /**
  771. * ishtp_trace_callback() - Return trace callback
  772. * @cl_device: ISH-TP client device instance
  773. *
  774. * This interface is used to return trace callback function pointer.
  775. *
  776. * Return: *ishtp_print_log()
  777. */
  778. ishtp_print_log ishtp_trace_callback(struct ishtp_cl_device *cl_device)
  779. {
  780. return cl_device->ishtp_dev->print_log;
  781. }
  782. EXPORT_SYMBOL(ishtp_trace_callback);
  783. /**
  784. * ish_hw_reset() - Call HW reset IPC callback
  785. * @dev: ISHTP device instance
  786. *
  787. * This interface is used to reset HW in case of error.
  788. *
  789. * Return: value from IPC hw_reset callback
  790. */
  791. int ish_hw_reset(struct ishtp_device *dev)
  792. {
  793. return dev->ops->hw_reset(dev);
  794. }
  795. EXPORT_SYMBOL(ish_hw_reset);
  796. /**
  797. * ishtp_bus_register() - Function to register bus
  798. *
  799. * This register ishtp bus
  800. *
  801. * Return: Return output of bus_register
  802. */
  803. static int __init ishtp_bus_register(void)
  804. {
  805. return bus_register(&ishtp_cl_bus_type);
  806. }
  807. /**
  808. * ishtp_bus_unregister() - Function to unregister bus
  809. *
  810. * This unregister ishtp bus
  811. */
  812. static void __exit ishtp_bus_unregister(void)
  813. {
  814. bus_unregister(&ishtp_cl_bus_type);
  815. }
  816. module_init(ishtp_bus_register);
  817. module_exit(ishtp_bus_unregister);
  818. MODULE_LICENSE("GPL");