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_fw_cl_by_id() - return index to fw_clients for client_id
175  * @dev: the ishtp device structure
176  * @client_id: fw client id to search
177  *
178  * Search firmware client using client id.
179  *
180  * Return: index on success, -ENOENT on failure.
181  */
182 int ishtp_fw_cl_by_id(struct ishtp_device *dev, uint8_t client_id)
183 {
184 	int i, res = -ENOENT;
185 	unsigned long	flags;
186 
187 	spin_lock_irqsave(&dev->fw_clients_lock, flags);
188 	for (i = 0; i < dev->fw_clients_num; i++) {
189 		if (dev->fw_clients[i].client_id == client_id) {
190 			res = i;
191 			break;
192 		}
193 	}
194 	spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
195 
196 	return res;
197 }
198 
199 /**
200  * ishtp_cl_device_probe() - Bus probe() callback
201  * @dev: the device structure
202  *
203  * This is a bus probe callback and calls the drive probe function.
204  *
205  * Return: Return value from driver probe() call.
206  */
207 static int ishtp_cl_device_probe(struct device *dev)
208 {
209 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
210 	struct ishtp_cl_driver *driver;
211 
212 	if (!device)
213 		return 0;
214 
215 	driver = to_ishtp_cl_driver(dev->driver);
216 	if (!driver || !driver->probe)
217 		return -ENODEV;
218 
219 	return driver->probe(device);
220 }
221 
222 /**
223  * ishtp_cl_device_remove() - Bus remove() callback
224  * @dev: the device structure
225  *
226  * This is a bus remove callback and calls the drive remove function.
227  * Since the ISH driver model supports only built in, this is
228  * primarily can be called during pci driver init failure.
229  *
230  * Return: Return value from driver remove() call.
231  */
232 static int ishtp_cl_device_remove(struct device *dev)
233 {
234 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
235 	struct ishtp_cl_driver *driver;
236 
237 	if (!device || !dev->driver)
238 		return 0;
239 
240 	if (device->event_cb) {
241 		device->event_cb = NULL;
242 		cancel_work_sync(&device->event_work);
243 	}
244 
245 	driver = to_ishtp_cl_driver(dev->driver);
246 	if (!driver->remove) {
247 		dev->driver = NULL;
248 
249 		return 0;
250 	}
251 
252 	return driver->remove(device);
253 }
254 
255 /**
256  * ishtp_cl_device_suspend() - Bus suspend callback
257  * @dev:	device
258  *
259  * Called during device suspend process.
260  *
261  * Return: Return value from driver suspend() call.
262  */
263 static int ishtp_cl_device_suspend(struct device *dev)
264 {
265 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
266 	struct ishtp_cl_driver *driver;
267 	int ret = 0;
268 
269 	if (!device)
270 		return 0;
271 
272 	driver = to_ishtp_cl_driver(dev->driver);
273 	if (driver && driver->driver.pm) {
274 		if (driver->driver.pm->suspend)
275 			ret = driver->driver.pm->suspend(dev);
276 	}
277 
278 	return ret;
279 }
280 
281 /**
282  * ishtp_cl_device_resume() - Bus resume callback
283  * @dev:	device
284  *
285  * Called during device resume process.
286  *
287  * Return: Return value from driver resume() call.
288  */
289 static int ishtp_cl_device_resume(struct device *dev)
290 {
291 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
292 	struct ishtp_cl_driver *driver;
293 	int ret = 0;
294 
295 	if (!device)
296 		return 0;
297 
298 	/*
299 	 * When ISH needs hard reset, it is done asynchrnously, hence bus
300 	 * resume will  be called before full ISH resume
301 	 */
302 	if (device->ishtp_dev->resume_flag)
303 		return 0;
304 
305 	driver = to_ishtp_cl_driver(dev->driver);
306 	if (driver && driver->driver.pm) {
307 		if (driver->driver.pm->resume)
308 			ret = driver->driver.pm->resume(dev);
309 	}
310 
311 	return ret;
312 }
313 
314 /**
315  * ishtp_cl_device_reset() - Reset callback
316  * @device:	ishtp client device instance
317  *
318  * This is a callback when HW reset is done and the device need
319  * reinit.
320  *
321  * Return: Return value from driver reset() call.
322  */
323 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
324 {
325 	struct ishtp_cl_driver *driver;
326 	int ret = 0;
327 
328 	device->event_cb = NULL;
329 	cancel_work_sync(&device->event_work);
330 
331 	driver = to_ishtp_cl_driver(device->dev.driver);
332 	if (driver && driver->reset)
333 		ret = driver->reset(device);
334 
335 	return ret;
336 }
337 
338 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
339 	char *buf)
340 {
341 	int len;
342 
343 	len = snprintf(buf, PAGE_SIZE, "ishtp:%s\n", dev_name(dev));
344 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
345 }
346 static DEVICE_ATTR_RO(modalias);
347 
348 static struct attribute *ishtp_cl_dev_attrs[] = {
349 	&dev_attr_modalias.attr,
350 	NULL,
351 };
352 ATTRIBUTE_GROUPS(ishtp_cl_dev);
353 
354 static int ishtp_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
355 {
356 	if (add_uevent_var(env, "MODALIAS=ishtp:%s", dev_name(dev)))
357 		return -ENOMEM;
358 	return 0;
359 }
360 
361 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
362 	/* Suspend callbacks */
363 	.suspend = ishtp_cl_device_suspend,
364 	.resume = ishtp_cl_device_resume,
365 	/* Hibernate callbacks */
366 	.freeze = ishtp_cl_device_suspend,
367 	.thaw = ishtp_cl_device_resume,
368 	.restore = ishtp_cl_device_resume,
369 };
370 
371 static struct bus_type ishtp_cl_bus_type = {
372 	.name		= "ishtp",
373 	.dev_groups	= ishtp_cl_dev_groups,
374 	.probe		= ishtp_cl_device_probe,
375 	.remove		= ishtp_cl_device_remove,
376 	.pm		= &ishtp_cl_bus_dev_pm_ops,
377 	.uevent		= ishtp_cl_uevent,
378 };
379 
380 static void ishtp_cl_dev_release(struct device *dev)
381 {
382 	kfree(to_ishtp_cl_device(dev));
383 }
384 
385 static const struct device_type ishtp_cl_device_type = {
386 	.release	= ishtp_cl_dev_release,
387 };
388 
389 /**
390  * ishtp_bus_add_device() - Function to create device on bus
391  * @dev:	ishtp device
392  * @uuid:	uuid of the client
393  * @name:	Name of the client
394  *
395  * Allocate ISHTP bus client device, attach it to uuid
396  * and register with ISHTP bus.
397  *
398  * Return: ishtp_cl_device pointer or NULL on failure
399  */
400 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
401 						    guid_t uuid, char *name)
402 {
403 	struct ishtp_cl_device *device;
404 	int status;
405 	unsigned long flags;
406 
407 	spin_lock_irqsave(&dev->device_list_lock, flags);
408 	list_for_each_entry(device, &dev->device_list, device_link) {
409 		if (!strcmp(name, dev_name(&device->dev))) {
410 			device->fw_client = &dev->fw_clients[
411 				dev->fw_client_presentation_num - 1];
412 			spin_unlock_irqrestore(&dev->device_list_lock, flags);
413 			ishtp_cl_device_reset(device);
414 			return device;
415 		}
416 	}
417 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
418 
419 	device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
420 	if (!device)
421 		return NULL;
422 
423 	device->dev.parent = dev->devc;
424 	device->dev.bus = &ishtp_cl_bus_type;
425 	device->dev.type = &ishtp_cl_device_type;
426 	device->ishtp_dev = dev;
427 
428 	device->fw_client =
429 		&dev->fw_clients[dev->fw_client_presentation_num - 1];
430 
431 	dev_set_name(&device->dev, "%s", name);
432 
433 	spin_lock_irqsave(&dev->device_list_lock, flags);
434 	list_add_tail(&device->device_link, &dev->device_list);
435 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
436 
437 	status = device_register(&device->dev);
438 	if (status) {
439 		spin_lock_irqsave(&dev->device_list_lock, flags);
440 		list_del(&device->device_link);
441 		spin_unlock_irqrestore(&dev->device_list_lock, flags);
442 		dev_err(dev->devc, "Failed to register ISHTP client device\n");
443 		put_device(&device->dev);
444 		return NULL;
445 	}
446 
447 	ishtp_device_ready = true;
448 
449 	return device;
450 }
451 
452 /**
453  * ishtp_bus_remove_device() - Function to relase device on bus
454  * @device:	client device instance
455  *
456  * This is a counterpart of ishtp_bus_add_device.
457  * Device is unregistered.
458  * the device structure is freed in 'ishtp_cl_dev_release' function
459  * Called only during error in pci driver init path.
460  */
461 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
462 {
463 	device_unregister(&device->dev);
464 }
465 
466 /**
467  * __ishtp_cl_driver_register() - Client driver register
468  * @driver:	the client driver instance
469  * @owner:	Owner of this driver module
470  *
471  * Once a client driver is probed, it created a client
472  * instance and registers with the bus.
473  *
474  * Return: Return value of driver_register or -ENODEV if not ready
475  */
476 int __ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
477 			       struct module *owner)
478 {
479 	int err;
480 
481 	if (!ishtp_device_ready)
482 		return -ENODEV;
483 
484 	driver->driver.name = driver->name;
485 	driver->driver.owner = owner;
486 	driver->driver.bus = &ishtp_cl_bus_type;
487 
488 	err = driver_register(&driver->driver);
489 	if (err)
490 		return err;
491 
492 	return 0;
493 }
494 EXPORT_SYMBOL(__ishtp_cl_driver_register);
495 
496 /**
497  * ishtp_cl_driver_unregister() - Client driver unregister
498  * @driver:	the client driver instance
499  *
500  * Unregister client during device removal process.
501  */
502 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
503 {
504 	driver_unregister(&driver->driver);
505 }
506 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
507 
508 /**
509  * ishtp_bus_event_work() - event work function
510  * @work:	work struct pointer
511  *
512  * Once an event is received for a client this work
513  * function is called. If the device has registered a
514  * callback then the callback is called.
515  */
516 static void ishtp_bus_event_work(struct work_struct *work)
517 {
518 	struct ishtp_cl_device *device;
519 
520 	device = container_of(work, struct ishtp_cl_device, event_work);
521 
522 	if (device->event_cb)
523 		device->event_cb(device);
524 }
525 
526 /**
527  * ishtp_cl_bus_rx_event() - schedule event work
528  * @device:	client device instance
529  *
530  * Once an event is received for a client this schedules
531  * a work function to process.
532  */
533 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
534 {
535 	if (!device || !device->event_cb)
536 		return;
537 
538 	if (device->event_cb)
539 		schedule_work(&device->event_work);
540 }
541 
542 /**
543  * ishtp_register_event_cb() - Register callback
544  * @device:	client device instance
545  * @event_cb:	Event processor for an client
546  *
547  * Register a callback for events, called from client driver
548  *
549  * Return: Return 0 or -EALREADY if already registered
550  */
551 int ishtp_register_event_cb(struct ishtp_cl_device *device,
552 	void (*event_cb)(struct ishtp_cl_device *))
553 {
554 	if (device->event_cb)
555 		return -EALREADY;
556 
557 	device->event_cb = event_cb;
558 	INIT_WORK(&device->event_work, ishtp_bus_event_work);
559 
560 	return 0;
561 }
562 EXPORT_SYMBOL(ishtp_register_event_cb);
563 
564 /**
565  * ishtp_get_device() - update usage count for the device
566  * @cl_device:	client device instance
567  *
568  * Increment the usage count. The device can't be deleted
569  */
570 void ishtp_get_device(struct ishtp_cl_device *cl_device)
571 {
572 	cl_device->reference_count++;
573 }
574 EXPORT_SYMBOL(ishtp_get_device);
575 
576 /**
577  * ishtp_put_device() - decrement usage count for the device
578  * @cl_device:	client device instance
579  *
580  * Decrement the usage count. The device can be deleted is count = 0
581  */
582 void ishtp_put_device(struct ishtp_cl_device *cl_device)
583 {
584 	cl_device->reference_count--;
585 }
586 EXPORT_SYMBOL(ishtp_put_device);
587 
588 /**
589  * ishtp_set_drvdata() - set client driver data
590  * @cl_device:	client device instance
591  * @data:	driver data need to be set
592  *
593  * Set client driver data to cl_device->driver_data.
594  */
595 void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
596 {
597 	cl_device->driver_data = data;
598 }
599 EXPORT_SYMBOL(ishtp_set_drvdata);
600 
601 /**
602  * ishtp_get_drvdata() - get client driver data
603  * @cl_device:	client device instance
604  *
605  * Get client driver data from cl_device->driver_data.
606  *
607  * Return: pointer of driver data
608  */
609 void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
610 {
611 	return cl_device->driver_data;
612 }
613 EXPORT_SYMBOL(ishtp_get_drvdata);
614 
615 /**
616  * ishtp_bus_new_client() - Create a new client
617  * @dev:	ISHTP device instance
618  *
619  * Once bus protocol enumerates a client, this is called
620  * to add a device for the client.
621  *
622  * Return: 0 on success or error code on failure
623  */
624 int ishtp_bus_new_client(struct ishtp_device *dev)
625 {
626 	int	i;
627 	char	*dev_name;
628 	struct ishtp_cl_device	*cl_device;
629 	guid_t	device_uuid;
630 
631 	/*
632 	 * For all reported clients, create an unconnected client and add its
633 	 * device to ISHTP bus.
634 	 * If appropriate driver has loaded, this will trigger its probe().
635 	 * Otherwise, probe() will be called when driver is loaded
636 	 */
637 	i = dev->fw_client_presentation_num - 1;
638 	device_uuid = dev->fw_clients[i].props.protocol_name;
639 	dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
640 	if (!dev_name)
641 		return	-ENOMEM;
642 
643 	cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
644 	if (!cl_device) {
645 		kfree(dev_name);
646 		return	-ENOENT;
647 	}
648 
649 	kfree(dev_name);
650 
651 	return	0;
652 }
653 
654 /**
655  * ishtp_cl_device_bind() - bind a device
656  * @cl:		ishtp client device
657  *
658  * Binds connected ishtp_cl to ISHTP bus device
659  *
660  * Return: 0 on success or fault code
661  */
662 int ishtp_cl_device_bind(struct ishtp_cl *cl)
663 {
664 	struct ishtp_cl_device	*cl_device;
665 	unsigned long flags;
666 	int	rv;
667 
668 	if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
669 		return	-EFAULT;
670 
671 	rv = -ENOENT;
672 	spin_lock_irqsave(&cl->dev->device_list_lock, flags);
673 	list_for_each_entry(cl_device, &cl->dev->device_list,
674 			device_link) {
675 		if (cl_device->fw_client &&
676 		    cl_device->fw_client->client_id == cl->fw_client_id) {
677 			cl->device = cl_device;
678 			rv = 0;
679 			break;
680 		}
681 	}
682 	spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
683 	return	rv;
684 }
685 
686 /**
687  * ishtp_bus_remove_all_clients() - Remove all clients
688  * @ishtp_dev:		ishtp device
689  * @warm_reset:		Reset due to FW reset dure to errors or S3 suspend
690  *
691  * This is part of reset/remove flow. This function the main processing
692  * only targets error processing, if the FW has forced reset or
693  * error to remove connected clients. When warm reset the client devices are
694  * not removed.
695  */
696 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
697 				  bool warm_reset)
698 {
699 	struct ishtp_cl_device	*cl_device, *n;
700 	struct ishtp_cl	*cl;
701 	unsigned long	flags;
702 
703 	spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
704 	list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
705 		cl->state = ISHTP_CL_DISCONNECTED;
706 
707 		/*
708 		 * Wake any pending process. The waiter would check dev->state
709 		 * and determine that it's not enabled already,
710 		 * and will return error to its caller
711 		 */
712 		wake_up_interruptible(&cl->wait_ctrl_res);
713 
714 		/* Disband any pending read/write requests and free rb */
715 		ishtp_cl_flush_queues(cl);
716 
717 		/* Remove all free and in_process rings, both Rx and Tx */
718 		ishtp_cl_free_rx_ring(cl);
719 		ishtp_cl_free_tx_ring(cl);
720 
721 		/*
722 		 * Free client and ISHTP bus client device structures
723 		 * don't free host client because it is part of the OS fd
724 		 * structure
725 		 */
726 	}
727 	spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
728 
729 	/* Release DMA buffers for client messages */
730 	ishtp_cl_free_dma_buf(ishtp_dev);
731 
732 	/* remove bus clients */
733 	spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
734 	list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
735 				 device_link) {
736 		cl_device->fw_client = NULL;
737 		if (warm_reset && cl_device->reference_count)
738 			continue;
739 
740 		list_del(&cl_device->device_link);
741 		spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
742 		ishtp_bus_remove_device(cl_device);
743 		spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
744 	}
745 	spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
746 
747 	/* Free all client structures */
748 	spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
749 	kfree(ishtp_dev->fw_clients);
750 	ishtp_dev->fw_clients = NULL;
751 	ishtp_dev->fw_clients_num = 0;
752 	ishtp_dev->fw_client_presentation_num = 0;
753 	ishtp_dev->fw_client_index = 0;
754 	bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
755 	spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
756 }
757 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
758 
759 /**
760  * ishtp_reset_handler() - IPC reset handler
761  * @dev:	ishtp device
762  *
763  * ISHTP Handler for IPC_RESET notification
764  */
765 void ishtp_reset_handler(struct ishtp_device *dev)
766 {
767 	unsigned long	flags;
768 
769 	/* Handle FW-initiated reset */
770 	dev->dev_state = ISHTP_DEV_RESETTING;
771 
772 	/* Clear BH processing queue - no further HBMs */
773 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
774 	dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
775 	spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
776 
777 	/* Handle ISH FW reset against upper layers */
778 	ishtp_bus_remove_all_clients(dev, true);
779 }
780 EXPORT_SYMBOL(ishtp_reset_handler);
781 
782 /**
783  * ishtp_reset_compl_handler() - Reset completion handler
784  * @dev:	ishtp device
785  *
786  * ISHTP handler for IPC_RESET sequence completion to start
787  * host message bus start protocol sequence.
788  */
789 void ishtp_reset_compl_handler(struct ishtp_device *dev)
790 {
791 	dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
792 	dev->hbm_state = ISHTP_HBM_START;
793 	ishtp_hbm_start_req(dev);
794 }
795 EXPORT_SYMBOL(ishtp_reset_compl_handler);
796 
797 /**
798  * ishtp_use_dma_transfer() - Function to use DMA
799  *
800  * This interface is used to enable usage of DMA
801  *
802  * Return non zero if DMA can be enabled
803  */
804 int ishtp_use_dma_transfer(void)
805 {
806 	return ishtp_use_dma;
807 }
808 
809 /**
810  * ishtp_bus_register() - Function to register bus
811  *
812  * This register ishtp bus
813  *
814  * Return: Return output of bus_register
815  */
816 static int  __init ishtp_bus_register(void)
817 {
818 	return bus_register(&ishtp_cl_bus_type);
819 }
820 
821 /**
822  * ishtp_bus_unregister() - Function to unregister bus
823  *
824  * This unregister ishtp bus
825  */
826 static void __exit ishtp_bus_unregister(void)
827 {
828 	bus_unregister(&ishtp_cl_bus_type);
829 }
830 
831 module_init(ishtp_bus_register);
832 module_exit(ishtp_bus_unregister);
833 
834 MODULE_LICENSE("GPL");
835