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/device.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include "ishtp-dev.h"
27 static int ishtp_use_dma
;
28 module_param_named(ishtp_use_dma
, ishtp_use_dma
, int, 0600);
29 MODULE_PARM_DESC(ishtp_use_dma
, "Use DMA to send messages");
31 #define to_ishtp_cl_driver(d) container_of(d, struct ishtp_cl_driver, driver)
32 #define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
33 static bool ishtp_device_ready
;
36 * ishtp_recv() - process ishtp message
39 * If a message with valid header and size is received, then
40 * this function calls appropriate handler. The host or firmware
41 * address is zero, then they are host bus management message,
42 * otherwise they are message fo clients.
44 void ishtp_recv(struct ishtp_device
*dev
)
47 struct ishtp_msg_hdr
*ishtp_hdr
;
49 /* Read ISHTP header dword */
50 msg_hdr
= dev
->ops
->ishtp_read_hdr(dev
);
54 dev
->ops
->sync_fw_clock(dev
);
56 ishtp_hdr
= (struct ishtp_msg_hdr
*)&msg_hdr
;
57 dev
->ishtp_msg_hdr
= msg_hdr
;
59 /* Sanity check: ISHTP frag. length in header */
60 if (ishtp_hdr
->length
> dev
->mtu
) {
62 "ISHTP hdr - bad length: %u; dropped [%08X]\n",
63 (unsigned int)ishtp_hdr
->length
, msg_hdr
);
67 /* ISHTP bus message */
68 if (!ishtp_hdr
->host_addr
&& !ishtp_hdr
->fw_addr
)
69 recv_hbm(dev
, ishtp_hdr
);
70 /* ISHTP fixed-client message */
71 else if (!ishtp_hdr
->host_addr
)
72 recv_fixed_cl_msg(dev
, ishtp_hdr
);
74 /* ISHTP client message */
75 recv_ishtp_cl_msg(dev
, ishtp_hdr
);
77 EXPORT_SYMBOL(ishtp_recv
);
80 * ishtp_send_msg() - Send ishtp message
82 * @hdr: Message header
83 * @msg: Message contents
84 * @ipc_send_compl: completion callback
85 * @ipc_send_compl_prm: completion callback parameter
87 * Send a multi fragment message via IPC. After sending the first fragment
88 * the completion callback is called to schedule transmit of next fragment.
90 * Return: This returns IPC send message status.
92 int ishtp_send_msg(struct ishtp_device
*dev
, struct ishtp_msg_hdr
*hdr
,
93 void *msg
, void(*ipc_send_compl
)(void *),
94 void *ipc_send_compl_prm
)
96 unsigned char ipc_msg
[IPC_FULL_MSG_SIZE
];
99 drbl_val
= dev
->ops
->ipc_get_header(dev
, hdr
->length
+
100 sizeof(struct ishtp_msg_hdr
),
103 memcpy(ipc_msg
, &drbl_val
, sizeof(uint32_t));
104 memcpy(ipc_msg
+ sizeof(uint32_t), hdr
, sizeof(uint32_t));
105 memcpy(ipc_msg
+ 2 * sizeof(uint32_t), msg
, hdr
->length
);
106 return dev
->ops
->write(dev
, ipc_send_compl
, ipc_send_compl_prm
,
107 ipc_msg
, 2 * sizeof(uint32_t) + hdr
->length
);
111 * ishtp_write_message() - Send ishtp single fragment message
113 * @hdr: Message header
116 * Send a single fragment message via IPC. This returns IPC send message
119 * Return: This returns IPC send message status.
121 int ishtp_write_message(struct ishtp_device
*dev
, struct ishtp_msg_hdr
*hdr
,
124 return ishtp_send_msg(dev
, hdr
, buf
, NULL
, NULL
);
128 * ishtp_fw_cl_by_uuid() - locate index of fw client
130 * @uuid: uuid of the client to search
132 * Search firmware client using UUID.
134 * Return: fw client index or -ENOENT if not found
136 int ishtp_fw_cl_by_uuid(struct ishtp_device
*dev
, const uuid_le
*uuid
)
138 int i
, res
= -ENOENT
;
140 for (i
= 0; i
< dev
->fw_clients_num
; ++i
) {
141 if (uuid_le_cmp(*uuid
, dev
->fw_clients
[i
].props
.protocol_name
)
149 EXPORT_SYMBOL(ishtp_fw_cl_by_uuid
);
152 * ishtp_fw_cl_by_id() - return index to fw_clients for client_id
153 * @dev: the ishtp device structure
154 * @client_id: fw client id to search
156 * Search firmware client using client id.
158 * Return: index on success, -ENOENT on failure.
160 int ishtp_fw_cl_by_id(struct ishtp_device
*dev
, uint8_t client_id
)
162 int i
, res
= -ENOENT
;
165 spin_lock_irqsave(&dev
->fw_clients_lock
, flags
);
166 for (i
= 0; i
< dev
->fw_clients_num
; i
++) {
167 if (dev
->fw_clients
[i
].client_id
== client_id
) {
172 spin_unlock_irqrestore(&dev
->fw_clients_lock
, flags
);
178 * ishtp_cl_device_probe() - Bus probe() callback
179 * @dev: the device structure
181 * This is a bus probe callback and calls the drive probe function.
183 * Return: Return value from driver probe() call.
185 static int ishtp_cl_device_probe(struct device
*dev
)
187 struct ishtp_cl_device
*device
= to_ishtp_cl_device(dev
);
188 struct ishtp_cl_driver
*driver
;
193 driver
= to_ishtp_cl_driver(dev
->driver
);
194 if (!driver
|| !driver
->probe
)
197 return driver
->probe(device
);
201 * ishtp_cl_device_remove() - Bus remove() callback
202 * @dev: the device structure
204 * This is a bus remove callback and calls the drive remove function.
205 * Since the ISH driver model supports only built in, this is
206 * primarily can be called during pci driver init failure.
208 * Return: Return value from driver remove() call.
210 static int ishtp_cl_device_remove(struct device
*dev
)
212 struct ishtp_cl_device
*device
= to_ishtp_cl_device(dev
);
213 struct ishtp_cl_driver
*driver
;
215 if (!device
|| !dev
->driver
)
218 if (device
->event_cb
) {
219 device
->event_cb
= NULL
;
220 cancel_work_sync(&device
->event_work
);
223 driver
= to_ishtp_cl_driver(dev
->driver
);
224 if (!driver
->remove
) {
230 return driver
->remove(device
);
234 * ishtp_cl_device_suspend() - Bus suspend callback
237 * Called during device suspend process.
239 * Return: Return value from driver suspend() call.
241 static int ishtp_cl_device_suspend(struct device
*dev
)
243 struct ishtp_cl_device
*device
= to_ishtp_cl_device(dev
);
244 struct ishtp_cl_driver
*driver
;
250 driver
= to_ishtp_cl_driver(dev
->driver
);
251 if (driver
&& driver
->driver
.pm
) {
252 if (driver
->driver
.pm
->suspend
)
253 ret
= driver
->driver
.pm
->suspend(dev
);
260 * ishtp_cl_device_resume() - Bus resume callback
263 * Called during device resume process.
265 * Return: Return value from driver resume() call.
267 static int ishtp_cl_device_resume(struct device
*dev
)
269 struct ishtp_cl_device
*device
= to_ishtp_cl_device(dev
);
270 struct ishtp_cl_driver
*driver
;
277 * When ISH needs hard reset, it is done asynchrnously, hence bus
278 * resume will be called before full ISH resume
280 if (device
->ishtp_dev
->resume_flag
)
283 driver
= to_ishtp_cl_driver(dev
->driver
);
284 if (driver
&& driver
->driver
.pm
) {
285 if (driver
->driver
.pm
->resume
)
286 ret
= driver
->driver
.pm
->resume(dev
);
293 * ishtp_cl_device_reset() - Reset callback
294 * @device: ishtp client device instance
296 * This is a callback when HW reset is done and the device need
299 * Return: Return value from driver reset() call.
301 static int ishtp_cl_device_reset(struct ishtp_cl_device
*device
)
303 struct ishtp_cl_driver
*driver
;
306 device
->event_cb
= NULL
;
307 cancel_work_sync(&device
->event_work
);
309 driver
= to_ishtp_cl_driver(device
->dev
.driver
);
310 if (driver
&& driver
->reset
)
311 ret
= driver
->reset(device
);
316 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*a
,
321 len
= snprintf(buf
, PAGE_SIZE
, "ishtp:%s\n", dev_name(dev
));
322 return (len
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : len
;
324 static DEVICE_ATTR_RO(modalias
);
326 static struct attribute
*ishtp_cl_dev_attrs
[] = {
327 &dev_attr_modalias
.attr
,
330 ATTRIBUTE_GROUPS(ishtp_cl_dev
);
332 static int ishtp_cl_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
334 if (add_uevent_var(env
, "MODALIAS=ishtp:%s", dev_name(dev
)))
339 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops
= {
340 /* Suspend callbacks */
341 .suspend
= ishtp_cl_device_suspend
,
342 .resume
= ishtp_cl_device_resume
,
343 /* Hibernate callbacks */
344 .freeze
= ishtp_cl_device_suspend
,
345 .thaw
= ishtp_cl_device_resume
,
346 .restore
= ishtp_cl_device_resume
,
349 static struct bus_type ishtp_cl_bus_type
= {
351 .dev_groups
= ishtp_cl_dev_groups
,
352 .probe
= ishtp_cl_device_probe
,
353 .remove
= ishtp_cl_device_remove
,
354 .pm
= &ishtp_cl_bus_dev_pm_ops
,
355 .uevent
= ishtp_cl_uevent
,
358 static void ishtp_cl_dev_release(struct device
*dev
)
360 kfree(to_ishtp_cl_device(dev
));
363 static const struct device_type ishtp_cl_device_type
= {
364 .release
= ishtp_cl_dev_release
,
368 * ishtp_bus_add_device() - Function to create device on bus
370 * @uuid: uuid of the client
371 * @name: Name of the client
373 * Allocate ISHTP bus client device, attach it to uuid
374 * and register with ISHTP bus.
376 * Return: ishtp_cl_device pointer or NULL on failure
378 static struct ishtp_cl_device
*ishtp_bus_add_device(struct ishtp_device
*dev
,
379 uuid_le uuid
, char *name
)
381 struct ishtp_cl_device
*device
;
385 spin_lock_irqsave(&dev
->device_list_lock
, flags
);
386 list_for_each_entry(device
, &dev
->device_list
, device_link
) {
387 if (!strcmp(name
, dev_name(&device
->dev
))) {
388 device
->fw_client
= &dev
->fw_clients
[
389 dev
->fw_client_presentation_num
- 1];
390 spin_unlock_irqrestore(&dev
->device_list_lock
, flags
);
391 ishtp_cl_device_reset(device
);
395 spin_unlock_irqrestore(&dev
->device_list_lock
, flags
);
397 device
= kzalloc(sizeof(struct ishtp_cl_device
), GFP_KERNEL
);
401 device
->dev
.parent
= dev
->devc
;
402 device
->dev
.bus
= &ishtp_cl_bus_type
;
403 device
->dev
.type
= &ishtp_cl_device_type
;
404 device
->ishtp_dev
= dev
;
407 &dev
->fw_clients
[dev
->fw_client_presentation_num
- 1];
409 dev_set_name(&device
->dev
, "%s", name
);
411 spin_lock_irqsave(&dev
->device_list_lock
, flags
);
412 list_add_tail(&device
->device_link
, &dev
->device_list
);
413 spin_unlock_irqrestore(&dev
->device_list_lock
, flags
);
415 status
= device_register(&device
->dev
);
417 spin_lock_irqsave(&dev
->device_list_lock
, flags
);
418 list_del(&device
->device_link
);
419 spin_unlock_irqrestore(&dev
->device_list_lock
, flags
);
420 dev_err(dev
->devc
, "Failed to register ISHTP client device\n");
425 ishtp_device_ready
= true;
431 * ishtp_bus_remove_device() - Function to relase device on bus
432 * @device: client device instance
434 * This is a counterpart of ishtp_bus_add_device.
435 * Device is unregistered.
436 * the device structure is freed in 'ishtp_cl_dev_release' function
437 * Called only during error in pci driver init path.
439 static void ishtp_bus_remove_device(struct ishtp_cl_device
*device
)
441 device_unregister(&device
->dev
);
445 * __ishtp_cl_driver_register() - Client driver register
446 * @driver: the client driver instance
447 * @owner: Owner of this driver module
449 * Once a client driver is probed, it created a client
450 * instance and registers with the bus.
452 * Return: Return value of driver_register or -ENODEV if not ready
454 int __ishtp_cl_driver_register(struct ishtp_cl_driver
*driver
,
455 struct module
*owner
)
459 if (!ishtp_device_ready
)
462 driver
->driver
.name
= driver
->name
;
463 driver
->driver
.owner
= owner
;
464 driver
->driver
.bus
= &ishtp_cl_bus_type
;
466 err
= driver_register(&driver
->driver
);
472 EXPORT_SYMBOL(__ishtp_cl_driver_register
);
475 * ishtp_cl_driver_unregister() - Client driver unregister
476 * @driver: the client driver instance
478 * Unregister client during device removal process.
480 void ishtp_cl_driver_unregister(struct ishtp_cl_driver
*driver
)
482 driver_unregister(&driver
->driver
);
484 EXPORT_SYMBOL(ishtp_cl_driver_unregister
);
487 * ishtp_bus_event_work() - event work function
488 * @work: work struct pointer
490 * Once an event is received for a client this work
491 * function is called. If the device has registered a
492 * callback then the callback is called.
494 static void ishtp_bus_event_work(struct work_struct
*work
)
496 struct ishtp_cl_device
*device
;
498 device
= container_of(work
, struct ishtp_cl_device
, event_work
);
500 if (device
->event_cb
)
501 device
->event_cb(device
);
505 * ishtp_cl_bus_rx_event() - schedule event work
506 * @device: client device instance
508 * Once an event is received for a client this schedules
509 * a work function to process.
511 void ishtp_cl_bus_rx_event(struct ishtp_cl_device
*device
)
513 if (!device
|| !device
->event_cb
)
516 if (device
->event_cb
)
517 schedule_work(&device
->event_work
);
521 * ishtp_register_event_cb() - Register callback
522 * @device: client device instance
523 * @event_cb: Event processor for an client
525 * Register a callback for events, called from client driver
527 * Return: Return 0 or -EALREADY if already registered
529 int ishtp_register_event_cb(struct ishtp_cl_device
*device
,
530 void (*event_cb
)(struct ishtp_cl_device
*))
532 if (device
->event_cb
)
535 device
->event_cb
= event_cb
;
536 INIT_WORK(&device
->event_work
, ishtp_bus_event_work
);
540 EXPORT_SYMBOL(ishtp_register_event_cb
);
543 * ishtp_get_device() - update usage count for the device
544 * @cl_device: client device instance
546 * Increment the usage count. The device can't be deleted
548 void ishtp_get_device(struct ishtp_cl_device
*cl_device
)
550 cl_device
->reference_count
++;
552 EXPORT_SYMBOL(ishtp_get_device
);
555 * ishtp_put_device() - decrement usage count for the device
556 * @cl_device: client device instance
558 * Decrement the usage count. The device can be deleted is count = 0
560 void ishtp_put_device(struct ishtp_cl_device
*cl_device
)
562 cl_device
->reference_count
--;
564 EXPORT_SYMBOL(ishtp_put_device
);
567 * ishtp_bus_new_client() - Create a new client
568 * @dev: ISHTP device instance
570 * Once bus protocol enumerates a client, this is called
571 * to add a device for the client.
573 * Return: 0 on success or error code on failure
575 int ishtp_bus_new_client(struct ishtp_device
*dev
)
579 struct ishtp_cl_device
*cl_device
;
583 * For all reported clients, create an unconnected client and add its
584 * device to ISHTP bus.
585 * If appropriate driver has loaded, this will trigger its probe().
586 * Otherwise, probe() will be called when driver is loaded
588 i
= dev
->fw_client_presentation_num
- 1;
589 device_uuid
= dev
->fw_clients
[i
].props
.protocol_name
;
590 dev_name
= kasprintf(GFP_KERNEL
, "{%pUL}", device_uuid
.b
);
594 cl_device
= ishtp_bus_add_device(dev
, device_uuid
, dev_name
);
606 * ishtp_cl_device_bind() - bind a device
607 * @cl: ishtp client device
609 * Binds connected ishtp_cl to ISHTP bus device
611 * Return: 0 on success or fault code
613 int ishtp_cl_device_bind(struct ishtp_cl
*cl
)
615 struct ishtp_cl_device
*cl_device
;
619 if (!cl
->fw_client_id
|| cl
->state
!= ISHTP_CL_CONNECTED
)
623 spin_lock_irqsave(&cl
->dev
->device_list_lock
, flags
);
624 list_for_each_entry(cl_device
, &cl
->dev
->device_list
,
626 if (cl_device
->fw_client
->client_id
== cl
->fw_client_id
) {
627 cl
->device
= cl_device
;
632 spin_unlock_irqrestore(&cl
->dev
->device_list_lock
, flags
);
637 * ishtp_bus_remove_all_clients() - Remove all clients
638 * @ishtp_dev: ishtp device
639 * @warm_reset: Reset due to FW reset dure to errors or S3 suspend
641 * This is part of reset/remove flow. This function the main processing
642 * only targets error processing, if the FW has forced reset or
643 * error to remove connected clients. When warm reset the client devices are
646 void ishtp_bus_remove_all_clients(struct ishtp_device
*ishtp_dev
,
649 struct ishtp_cl_device
*cl_device
, *n
;
653 spin_lock_irqsave(&ishtp_dev
->cl_list_lock
, flags
);
654 list_for_each_entry(cl
, &ishtp_dev
->cl_list
, link
) {
655 cl
->state
= ISHTP_CL_DISCONNECTED
;
658 * Wake any pending process. The waiter would check dev->state
659 * and determine that it's not enabled already,
660 * and will return error to its caller
662 wake_up_interruptible(&cl
->wait_ctrl_res
);
664 /* Disband any pending read/write requests and free rb */
665 ishtp_cl_flush_queues(cl
);
667 /* Remove all free and in_process rings, both Rx and Tx */
668 ishtp_cl_free_rx_ring(cl
);
669 ishtp_cl_free_tx_ring(cl
);
672 * Free client and ISHTP bus client device structures
673 * don't free host client because it is part of the OS fd
677 spin_unlock_irqrestore(&ishtp_dev
->cl_list_lock
, flags
);
679 /* Release DMA buffers for client messages */
680 ishtp_cl_free_dma_buf(ishtp_dev
);
682 /* remove bus clients */
683 spin_lock_irqsave(&ishtp_dev
->device_list_lock
, flags
);
684 list_for_each_entry_safe(cl_device
, n
, &ishtp_dev
->device_list
,
686 if (warm_reset
&& cl_device
->reference_count
)
689 list_del(&cl_device
->device_link
);
690 spin_unlock_irqrestore(&ishtp_dev
->device_list_lock
, flags
);
691 ishtp_bus_remove_device(cl_device
);
692 spin_lock_irqsave(&ishtp_dev
->device_list_lock
, flags
);
694 spin_unlock_irqrestore(&ishtp_dev
->device_list_lock
, flags
);
696 /* Free all client structures */
697 spin_lock_irqsave(&ishtp_dev
->fw_clients_lock
, flags
);
698 kfree(ishtp_dev
->fw_clients
);
699 ishtp_dev
->fw_clients
= NULL
;
700 ishtp_dev
->fw_clients_num
= 0;
701 ishtp_dev
->fw_client_presentation_num
= 0;
702 ishtp_dev
->fw_client_index
= 0;
703 bitmap_zero(ishtp_dev
->fw_clients_map
, ISHTP_CLIENTS_MAX
);
704 spin_unlock_irqrestore(&ishtp_dev
->fw_clients_lock
, flags
);
706 EXPORT_SYMBOL(ishtp_bus_remove_all_clients
);
709 * ishtp_reset_handler() - IPC reset handler
712 * ISHTP Handler for IPC_RESET notification
714 void ishtp_reset_handler(struct ishtp_device
*dev
)
718 /* Handle FW-initiated reset */
719 dev
->dev_state
= ISHTP_DEV_RESETTING
;
721 /* Clear BH processing queue - no further HBMs */
722 spin_lock_irqsave(&dev
->rd_msg_spinlock
, flags
);
723 dev
->rd_msg_fifo_head
= dev
->rd_msg_fifo_tail
= 0;
724 spin_unlock_irqrestore(&dev
->rd_msg_spinlock
, flags
);
726 /* Handle ISH FW reset against upper layers */
727 ishtp_bus_remove_all_clients(dev
, true);
729 EXPORT_SYMBOL(ishtp_reset_handler
);
732 * ishtp_reset_compl_handler() - Reset completion handler
735 * ISHTP handler for IPC_RESET sequence completion to start
736 * host message bus start protocol sequence.
738 void ishtp_reset_compl_handler(struct ishtp_device
*dev
)
740 dev
->dev_state
= ISHTP_DEV_INIT_CLIENTS
;
741 dev
->hbm_state
= ISHTP_HBM_START
;
742 ishtp_hbm_start_req(dev
);
744 EXPORT_SYMBOL(ishtp_reset_compl_handler
);
747 * ishtp_use_dma_transfer() - Function to use DMA
749 * This interface is used to enable usage of DMA
751 * Return non zero if DMA can be enabled
753 int ishtp_use_dma_transfer(void)
755 return ishtp_use_dma
;
759 * ishtp_bus_register() - Function to register bus
761 * This register ishtp bus
763 * Return: Return output of bus_register
765 static int __init
ishtp_bus_register(void)
767 return bus_register(&ishtp_cl_bus_type
);
771 * ishtp_bus_unregister() - Function to unregister bus
773 * This unregister ishtp bus
775 static void __exit
ishtp_bus_unregister(void)
777 bus_unregister(&ishtp_cl_bus_type
);
780 module_init(ishtp_bus_register
);
781 module_exit(ishtp_bus_unregister
);
783 MODULE_LICENSE("GPL");