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