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