4 * Copyright (c) 2012-2016, Intel Corporation.
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.
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
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include "ishtp-dev.h"
28 static int ishtp_use_dma;
29 module_param_named(ishtp_use_dma, ishtp_use_dma, int, 0600);
30 MODULE_PARM_DESC(ishtp_use_dma, "Use DMA to send messages");
32 #define to_ishtp_cl_driver(d) container_of(d, struct ishtp_cl_driver, driver)
33 #define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
34 static bool ishtp_device_ready;
37 * ishtp_recv() - process ishtp message
40 * If a message with valid header and size is received, then
41 * this function calls appropriate handler. The host or firmware
42 * address is zero, then they are host bus management message,
43 * otherwise they are message fo clients.
45 void ishtp_recv(struct ishtp_device *dev)
48 struct ishtp_msg_hdr *ishtp_hdr;
50 /* Read ISHTP header dword */
51 msg_hdr = dev->ops->ishtp_read_hdr(dev);
55 dev->ops->sync_fw_clock(dev);
57 ishtp_hdr = (struct ishtp_msg_hdr *)&msg_hdr;
58 dev->ishtp_msg_hdr = msg_hdr;
60 /* Sanity check: ISHTP frag. length in header */
61 if (ishtp_hdr->length > dev->mtu) {
63 "ISHTP hdr - bad length: %u; dropped [%08X]\n",
64 (unsigned int)ishtp_hdr->length, msg_hdr);
68 /* ISHTP bus message */
69 if (!ishtp_hdr->host_addr && !ishtp_hdr->fw_addr)
70 recv_hbm(dev, ishtp_hdr);
71 /* ISHTP fixed-client message */
72 else if (!ishtp_hdr->host_addr)
73 recv_fixed_cl_msg(dev, ishtp_hdr);
75 /* ISHTP client message */
76 recv_ishtp_cl_msg(dev, ishtp_hdr);
78 EXPORT_SYMBOL(ishtp_recv);
81 * ishtp_send_msg() - Send ishtp message
83 * @hdr: Message header
84 * @msg: Message contents
85 * @ipc_send_compl: completion callback
86 * @ipc_send_compl_prm: completion callback parameter
88 * Send a multi fragment message via IPC. After sending the first fragment
89 * the completion callback is called to schedule transmit of next fragment.
91 * Return: This returns IPC send message status.
93 int ishtp_send_msg(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
94 void *msg, void(*ipc_send_compl)(void *),
95 void *ipc_send_compl_prm)
97 unsigned char ipc_msg[IPC_FULL_MSG_SIZE];
100 drbl_val = dev->ops->ipc_get_header(dev, hdr->length +
101 sizeof(struct ishtp_msg_hdr),
104 memcpy(ipc_msg, &drbl_val, sizeof(uint32_t));
105 memcpy(ipc_msg + sizeof(uint32_t), hdr, sizeof(uint32_t));
106 memcpy(ipc_msg + 2 * sizeof(uint32_t), msg, hdr->length);
107 return dev->ops->write(dev, ipc_send_compl, ipc_send_compl_prm,
108 ipc_msg, 2 * sizeof(uint32_t) + hdr->length);
112 * ishtp_write_message() - Send ishtp single fragment message
114 * @hdr: Message header
117 * Send a single fragment message via IPC. This returns IPC send message
120 * Return: This returns IPC send message status.
122 int ishtp_write_message(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
125 return ishtp_send_msg(dev, hdr, buf, NULL, NULL);
129 * ishtp_fw_cl_by_uuid() - locate index of fw client
131 * @uuid: uuid of the client to search
133 * Search firmware client using UUID.
135 * Return: fw client index or -ENOENT if not found
137 int ishtp_fw_cl_by_uuid(struct ishtp_device *dev, const uuid_le *uuid)
139 int i, res = -ENOENT;
141 for (i = 0; i < dev->fw_clients_num; ++i) {
142 if (uuid_le_cmp(*uuid, dev->fw_clients[i].props.protocol_name)
150 EXPORT_SYMBOL(ishtp_fw_cl_by_uuid);
153 * ishtp_fw_cl_by_id() - return index to fw_clients for client_id
154 * @dev: the ishtp device structure
155 * @client_id: fw client id to search
157 * Search firmware client using client id.
159 * Return: index on success, -ENOENT on failure.
161 int ishtp_fw_cl_by_id(struct ishtp_device *dev, uint8_t client_id)
163 int i, res = -ENOENT;
166 spin_lock_irqsave(&dev->fw_clients_lock, flags);
167 for (i = 0; i < dev->fw_clients_num; i++) {
168 if (dev->fw_clients[i].client_id == client_id) {
173 spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
179 * ishtp_cl_device_probe() - Bus probe() callback
180 * @dev: the device structure
182 * This is a bus probe callback and calls the drive probe function.
184 * Return: Return value from driver probe() call.
186 static int ishtp_cl_device_probe(struct device *dev)
188 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
189 struct ishtp_cl_driver *driver;
194 driver = to_ishtp_cl_driver(dev->driver);
195 if (!driver || !driver->probe)
198 return driver->probe(device);
202 * ishtp_cl_device_remove() - Bus remove() callback
203 * @dev: the device structure
205 * This is a bus remove callback and calls the drive remove function.
206 * Since the ISH driver model supports only built in, this is
207 * primarily can be called during pci driver init failure.
209 * Return: Return value from driver remove() call.
211 static int ishtp_cl_device_remove(struct device *dev)
213 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
214 struct ishtp_cl_driver *driver;
216 if (!device || !dev->driver)
219 if (device->event_cb) {
220 device->event_cb = NULL;
221 cancel_work_sync(&device->event_work);
224 driver = to_ishtp_cl_driver(dev->driver);
225 if (!driver->remove) {
231 return driver->remove(device);
235 * ishtp_cl_device_suspend() - Bus suspend callback
238 * Called during device suspend process.
240 * Return: Return value from driver suspend() call.
242 static int ishtp_cl_device_suspend(struct device *dev)
244 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
245 struct ishtp_cl_driver *driver;
251 driver = to_ishtp_cl_driver(dev->driver);
252 if (driver && driver->driver.pm) {
253 if (driver->driver.pm->suspend)
254 ret = driver->driver.pm->suspend(dev);
261 * ishtp_cl_device_resume() - Bus resume callback
264 * Called during device resume process.
266 * Return: Return value from driver resume() call.
268 static int ishtp_cl_device_resume(struct device *dev)
270 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
271 struct ishtp_cl_driver *driver;
278 * When ISH needs hard reset, it is done asynchrnously, hence bus
279 * resume will be called before full ISH resume
281 if (device->ishtp_dev->resume_flag)
284 driver = to_ishtp_cl_driver(dev->driver);
285 if (driver && driver->driver.pm) {
286 if (driver->driver.pm->resume)
287 ret = driver->driver.pm->resume(dev);
294 * ishtp_cl_device_reset() - Reset callback
295 * @device: ishtp client device instance
297 * This is a callback when HW reset is done and the device need
300 * Return: Return value from driver reset() call.
302 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
304 struct ishtp_cl_driver *driver;
307 device->event_cb = NULL;
308 cancel_work_sync(&device->event_work);
310 driver = to_ishtp_cl_driver(device->dev.driver);
311 if (driver && driver->reset)
312 ret = driver->reset(device);
317 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
322 len = snprintf(buf, PAGE_SIZE, "ishtp:%s\n", dev_name(dev));
323 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
326 static struct device_attribute ishtp_cl_dev_attrs[] = {
331 static int ishtp_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
333 if (add_uevent_var(env, "MODALIAS=ishtp:%s", dev_name(dev)))
338 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
339 /* Suspend callbacks */
340 .suspend = ishtp_cl_device_suspend,
341 .resume = ishtp_cl_device_resume,
342 /* Hibernate callbacks */
343 .freeze = ishtp_cl_device_suspend,
344 .thaw = ishtp_cl_device_resume,
345 .restore = ishtp_cl_device_resume,
348 static struct bus_type ishtp_cl_bus_type = {
350 .dev_attrs = ishtp_cl_dev_attrs,
351 .probe = ishtp_cl_device_probe,
352 .remove = ishtp_cl_device_remove,
353 .pm = &ishtp_cl_bus_dev_pm_ops,
354 .uevent = ishtp_cl_uevent,
357 static void ishtp_cl_dev_release(struct device *dev)
359 kfree(to_ishtp_cl_device(dev));
362 static struct device_type ishtp_cl_device_type = {
363 .release = ishtp_cl_dev_release,
367 * ishtp_bus_add_device() - Function to create device on bus
369 * @uuid: uuid of the client
370 * @name: Name of the client
372 * Allocate ISHTP bus client device, attach it to uuid
373 * and register with ISHTP bus.
375 * Return: ishtp_cl_device pointer or NULL on failure
377 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
378 uuid_le uuid, char *name)
380 struct ishtp_cl_device *device;
383 struct list_head *pos;
385 spin_lock_irqsave(&dev->device_list_lock, flags);
386 list_for_each(pos, &dev->device_list) {
387 device = list_entry(pos, struct ishtp_cl_device, device_link);
388 if (!strcmp(name, dev_name(&device->dev))) {
389 device->fw_client = &dev->fw_clients[
390 dev->fw_client_presentation_num - 1];
391 spin_unlock_irqrestore(&dev->device_list_lock, flags);
392 ishtp_cl_device_reset(device);
396 spin_unlock_irqrestore(&dev->device_list_lock, flags);
398 device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
402 device->dev.parent = dev->devc;
403 device->dev.bus = &ishtp_cl_bus_type;
404 device->dev.type = &ishtp_cl_device_type;
405 device->ishtp_dev = dev;
408 &dev->fw_clients[dev->fw_client_presentation_num - 1];
410 dev_set_name(&device->dev, "%s", name);
412 spin_lock_irqsave(&dev->device_list_lock, flags);
413 list_add_tail(&device->device_link, &dev->device_list);
414 spin_unlock_irqrestore(&dev->device_list_lock, flags);
416 status = device_register(&device->dev);
418 spin_lock_irqsave(&dev->device_list_lock, flags);
419 list_del(&device->device_link);
420 spin_unlock_irqrestore(&dev->device_list_lock, flags);
421 dev_err(dev->devc, "Failed to register ISHTP client device\n");
426 ishtp_device_ready = true;
432 * ishtp_bus_remove_device() - Function to relase device on bus
433 * @device: client device instance
435 * This is a counterpart of ishtp_bus_add_device.
436 * Device is unregistered.
437 * the device structure is freed in 'ishtp_cl_dev_release' function
438 * Called only during error in pci driver init path.
440 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
442 device_unregister(&device->dev);
446 * __ishtp_cl_driver_register() - Client driver register
447 * @driver: the client driver instance
448 * @owner: Owner of this driver module
450 * Once a client driver is probed, it created a client
451 * instance and registers with the bus.
453 * Return: Return value of driver_register or -ENODEV if not ready
455 int __ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
456 struct module *owner)
460 if (!ishtp_device_ready)
463 driver->driver.name = driver->name;
464 driver->driver.owner = owner;
465 driver->driver.bus = &ishtp_cl_bus_type;
467 err = driver_register(&driver->driver);
473 EXPORT_SYMBOL(__ishtp_cl_driver_register);
476 * ishtp_cl_driver_unregister() - Client driver unregister
477 * @driver: the client driver instance
479 * Unregister client during device removal process.
481 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
483 driver_unregister(&driver->driver);
485 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
488 * ishtp_bus_event_work() - event work function
489 * @work: work struct pointer
491 * Once an event is received for a client this work
492 * function is called. If the device has registered a
493 * callback then the callback is called.
495 static void ishtp_bus_event_work(struct work_struct *work)
497 struct ishtp_cl_device *device;
499 device = container_of(work, struct ishtp_cl_device, event_work);
501 if (device->event_cb)
502 device->event_cb(device);
506 * ishtp_cl_bus_rx_event() - schedule event work
507 * @device: client device instance
509 * Once an event is received for a client this schedules
510 * a work function to process.
512 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
514 if (!device || !device->event_cb)
517 if (device->event_cb)
518 schedule_work(&device->event_work);
522 * ishtp_register_event_cb() - Register callback
523 * @device: client device instance
524 * @event_cb: Event processor for an client
526 * Register a callback for events, called from client driver
528 * Return: Return 0 or -EALREADY if already registered
530 int ishtp_register_event_cb(struct ishtp_cl_device *device,
531 void (*event_cb)(struct ishtp_cl_device *))
533 if (device->event_cb)
536 device->event_cb = event_cb;
537 INIT_WORK(&device->event_work, ishtp_bus_event_work);
541 EXPORT_SYMBOL(ishtp_register_event_cb);
544 * ishtp_get_device() - update usage count for the device
545 * @cl_device: client device instance
547 * Increment the usage count. The device can't be deleted
549 void ishtp_get_device(struct ishtp_cl_device *cl_device)
551 cl_device->reference_count++;
553 EXPORT_SYMBOL(ishtp_get_device);
556 * ishtp_put_device() - decrement usage count for the device
557 * @cl_device: client device instance
559 * Decrement the usage count. The device can be deleted is count = 0
561 void ishtp_put_device(struct ishtp_cl_device *cl_device)
563 cl_device->reference_count--;
565 EXPORT_SYMBOL(ishtp_put_device);
568 * ishtp_bus_new_client() - Create a new client
569 * @dev: ISHTP device instance
571 * Once bus protocol enumerates a client, this is called
572 * to add a device for the client.
574 * Return: 0 on success or error code on failure
576 int ishtp_bus_new_client(struct ishtp_device *dev)
580 struct ishtp_cl_device *cl_device;
584 * For all reported clients, create an unconnected client and add its
585 * device to ISHTP bus.
586 * If appropriate driver has loaded, this will trigger its probe().
587 * Otherwise, probe() will be called when driver is loaded
589 i = dev->fw_client_presentation_num - 1;
590 device_uuid = dev->fw_clients[i].props.protocol_name;
591 dev_name = kasprintf(GFP_KERNEL,
592 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
593 device_uuid.b[3], device_uuid.b[2], device_uuid.b[1],
594 device_uuid.b[0], device_uuid.b[5], device_uuid.b[4],
595 device_uuid.b[7], device_uuid.b[6], device_uuid.b[8],
596 device_uuid.b[9], device_uuid.b[10], device_uuid.b[11],
597 device_uuid.b[12], device_uuid.b[13], device_uuid.b[14],
602 cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
614 * ishtp_cl_device_bind() - bind a device
615 * @cl: ishtp client device
617 * Binds connected ishtp_cl to ISHTP bus device
619 * Return: 0 on success or fault code
621 int ishtp_cl_device_bind(struct ishtp_cl *cl)
623 struct ishtp_cl_device *cl_device;
627 if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
631 spin_lock_irqsave(&cl->dev->device_list_lock, flags);
632 list_for_each_entry(cl_device, &cl->dev->device_list,
634 if (cl_device->fw_client->client_id == cl->fw_client_id) {
635 cl->device = cl_device;
640 spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
645 * ishtp_bus_remove_all_clients() - Remove all clients
646 * @ishtp_dev: ishtp device
647 * @warm_reset: Reset due to FW reset dure to errors or S3 suspend
649 * This is part of reset/remove flow. This function the main processing
650 * only targets error processing, if the FW has forced reset or
651 * error to remove connected clients. When warm reset the client devices are
654 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
657 struct ishtp_cl_device *cl_device, *n;
661 spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
662 list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
663 cl->state = ISHTP_CL_DISCONNECTED;
666 * Wake any pending process. The waiter would check dev->state
667 * and determine that it's not enabled already,
668 * and will return error to its caller
670 wake_up_interruptible(&cl->wait_ctrl_res);
672 /* Disband any pending read/write requests and free rb */
673 ishtp_cl_flush_queues(cl);
675 /* Remove all free and in_process rings, both Rx and Tx */
676 ishtp_cl_free_rx_ring(cl);
677 ishtp_cl_free_tx_ring(cl);
680 * Free client and ISHTP bus client device structures
681 * don't free host client because it is part of the OS fd
685 spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
687 /* Release DMA buffers for client messages */
688 ishtp_cl_free_dma_buf(ishtp_dev);
690 /* remove bus clients */
691 spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
692 list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
694 if (warm_reset && cl_device->reference_count)
697 list_del(&cl_device->device_link);
698 spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
699 ishtp_bus_remove_device(cl_device);
700 spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
702 spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
704 /* Free all client structures */
705 spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
706 kfree(ishtp_dev->fw_clients);
707 ishtp_dev->fw_clients = NULL;
708 ishtp_dev->fw_clients_num = 0;
709 ishtp_dev->fw_client_presentation_num = 0;
710 ishtp_dev->fw_client_index = 0;
711 bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
712 spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
714 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
717 * ishtp_reset_handler() - IPC reset handler
720 * ISHTP Handler for IPC_RESET notification
722 void ishtp_reset_handler(struct ishtp_device *dev)
726 /* Handle FW-initiated reset */
727 dev->dev_state = ISHTP_DEV_RESETTING;
729 /* Clear BH processing queue - no further HBMs */
730 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
731 dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
732 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
734 /* Handle ISH FW reset against upper layers */
735 ishtp_bus_remove_all_clients(dev, true);
737 EXPORT_SYMBOL(ishtp_reset_handler);
740 * ishtp_reset_compl_handler() - Reset completion handler
743 * ISHTP handler for IPC_RESET sequence completion to start
744 * host message bus start protocol sequence.
746 void ishtp_reset_compl_handler(struct ishtp_device *dev)
748 dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
749 dev->hbm_state = ISHTP_HBM_START;
750 ishtp_hbm_start_req(dev);
752 EXPORT_SYMBOL(ishtp_reset_compl_handler);
755 * ishtp_use_dma_transfer() - Function to use DMA
757 * This interface is used to enable usage of DMA
759 * Return non zero if DMA can be enabled
761 int ishtp_use_dma_transfer(void)
763 return ishtp_use_dma;
767 * ishtp_bus_register() - Function to register bus
769 * This register ishtp bus
771 * Return: Return output of bus_register
773 static int __init ishtp_bus_register(void)
775 return bus_register(&ishtp_cl_bus_type);
779 * ishtp_bus_unregister() - Function to unregister bus
781 * This unregister ishtp bus
783 static void __exit ishtp_bus_unregister(void)
785 bus_unregister(&ishtp_cl_bus_type);
788 module_init(ishtp_bus_register);
789 module_exit(ishtp_bus_unregister);
791 MODULE_LICENSE("GPL");