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 	/*
318 	 * When ISH needs hard reset, it is done asynchrnously, hence bus
319 	 * resume will  be called before full ISH resume
320 	 */
321 	if (device->ishtp_dev->resume_flag)
322 		return 0;
323 
324 	driver = to_ishtp_cl_driver(dev->driver);
325 	if (driver && driver->driver.pm) {
326 		if (driver->driver.pm->resume)
327 			ret = driver->driver.pm->resume(dev);
328 	}
329 
330 	return ret;
331 }
332 
333 /**
334  * ishtp_cl_device_reset() - Reset callback
335  * @device:	ishtp client device instance
336  *
337  * This is a callback when HW reset is done and the device need
338  * reinit.
339  *
340  * Return: Return value from driver reset() call.
341  */
342 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
343 {
344 	struct ishtp_cl_driver *driver;
345 	int ret = 0;
346 
347 	device->event_cb = NULL;
348 	cancel_work_sync(&device->event_work);
349 
350 	driver = to_ishtp_cl_driver(device->dev.driver);
351 	if (driver && driver->reset)
352 		ret = driver->reset(device);
353 
354 	return ret;
355 }
356 
357 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
358 	char *buf)
359 {
360 	int len;
361 
362 	len = snprintf(buf, PAGE_SIZE, "ishtp:%s\n", dev_name(dev));
363 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
364 }
365 static DEVICE_ATTR_RO(modalias);
366 
367 static struct attribute *ishtp_cl_dev_attrs[] = {
368 	&dev_attr_modalias.attr,
369 	NULL,
370 };
371 ATTRIBUTE_GROUPS(ishtp_cl_dev);
372 
373 static int ishtp_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
374 {
375 	if (add_uevent_var(env, "MODALIAS=ishtp:%s", dev_name(dev)))
376 		return -ENOMEM;
377 	return 0;
378 }
379 
380 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
381 	/* Suspend callbacks */
382 	.suspend = ishtp_cl_device_suspend,
383 	.resume = ishtp_cl_device_resume,
384 	/* Hibernate callbacks */
385 	.freeze = ishtp_cl_device_suspend,
386 	.thaw = ishtp_cl_device_resume,
387 	.restore = ishtp_cl_device_resume,
388 };
389 
390 static struct bus_type ishtp_cl_bus_type = {
391 	.name		= "ishtp",
392 	.dev_groups	= ishtp_cl_dev_groups,
393 	.probe		= ishtp_cl_device_probe,
394 	.match		= ishtp_cl_bus_match,
395 	.remove		= ishtp_cl_device_remove,
396 	.pm		= &ishtp_cl_bus_dev_pm_ops,
397 	.uevent		= ishtp_cl_uevent,
398 };
399 
400 static void ishtp_cl_dev_release(struct device *dev)
401 {
402 	kfree(to_ishtp_cl_device(dev));
403 }
404 
405 static const struct device_type ishtp_cl_device_type = {
406 	.release	= ishtp_cl_dev_release,
407 };
408 
409 /**
410  * ishtp_bus_add_device() - Function to create device on bus
411  * @dev:	ishtp device
412  * @uuid:	uuid of the client
413  * @name:	Name of the client
414  *
415  * Allocate ISHTP bus client device, attach it to uuid
416  * and register with ISHTP bus.
417  *
418  * Return: ishtp_cl_device pointer or NULL on failure
419  */
420 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
421 						    guid_t uuid, char *name)
422 {
423 	struct ishtp_cl_device *device;
424 	int status;
425 	unsigned long flags;
426 
427 	spin_lock_irqsave(&dev->device_list_lock, flags);
428 	list_for_each_entry(device, &dev->device_list, device_link) {
429 		if (!strcmp(name, dev_name(&device->dev))) {
430 			device->fw_client = &dev->fw_clients[
431 				dev->fw_client_presentation_num - 1];
432 			spin_unlock_irqrestore(&dev->device_list_lock, flags);
433 			ishtp_cl_device_reset(device);
434 			return device;
435 		}
436 	}
437 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
438 
439 	device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
440 	if (!device)
441 		return NULL;
442 
443 	device->dev.parent = dev->devc;
444 	device->dev.bus = &ishtp_cl_bus_type;
445 	device->dev.type = &ishtp_cl_device_type;
446 	device->ishtp_dev = dev;
447 
448 	device->fw_client =
449 		&dev->fw_clients[dev->fw_client_presentation_num - 1];
450 
451 	dev_set_name(&device->dev, "%s", name);
452 
453 	spin_lock_irqsave(&dev->device_list_lock, flags);
454 	list_add_tail(&device->device_link, &dev->device_list);
455 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
456 
457 	status = device_register(&device->dev);
458 	if (status) {
459 		spin_lock_irqsave(&dev->device_list_lock, flags);
460 		list_del(&device->device_link);
461 		spin_unlock_irqrestore(&dev->device_list_lock, flags);
462 		dev_err(dev->devc, "Failed to register ISHTP client device\n");
463 		put_device(&device->dev);
464 		return NULL;
465 	}
466 
467 	ishtp_device_ready = true;
468 
469 	return device;
470 }
471 
472 /**
473  * ishtp_bus_remove_device() - Function to relase device on bus
474  * @device:	client device instance
475  *
476  * This is a counterpart of ishtp_bus_add_device.
477  * Device is unregistered.
478  * the device structure is freed in 'ishtp_cl_dev_release' function
479  * Called only during error in pci driver init path.
480  */
481 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
482 {
483 	device_unregister(&device->dev);
484 }
485 
486 /**
487  * ishtp_cl_driver_register() - Client driver register
488  * @driver:	the client driver instance
489  * @owner:	Owner of this driver module
490  *
491  * Once a client driver is probed, it created a client
492  * instance and registers with the bus.
493  *
494  * Return: Return value of driver_register or -ENODEV if not ready
495  */
496 int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
497 			     struct module *owner)
498 {
499 	if (!ishtp_device_ready)
500 		return -ENODEV;
501 
502 	driver->driver.name = driver->name;
503 	driver->driver.owner = owner;
504 	driver->driver.bus = &ishtp_cl_bus_type;
505 
506 	return driver_register(&driver->driver);
507 }
508 EXPORT_SYMBOL(ishtp_cl_driver_register);
509 
510 /**
511  * ishtp_cl_driver_unregister() - Client driver unregister
512  * @driver:	the client driver instance
513  *
514  * Unregister client during device removal process.
515  */
516 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
517 {
518 	driver_unregister(&driver->driver);
519 }
520 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
521 
522 /**
523  * ishtp_bus_event_work() - event work function
524  * @work:	work struct pointer
525  *
526  * Once an event is received for a client this work
527  * function is called. If the device has registered a
528  * callback then the callback is called.
529  */
530 static void ishtp_bus_event_work(struct work_struct *work)
531 {
532 	struct ishtp_cl_device *device;
533 
534 	device = container_of(work, struct ishtp_cl_device, event_work);
535 
536 	if (device->event_cb)
537 		device->event_cb(device);
538 }
539 
540 /**
541  * ishtp_cl_bus_rx_event() - schedule event work
542  * @device:	client device instance
543  *
544  * Once an event is received for a client this schedules
545  * a work function to process.
546  */
547 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
548 {
549 	if (!device || !device->event_cb)
550 		return;
551 
552 	if (device->event_cb)
553 		schedule_work(&device->event_work);
554 }
555 
556 /**
557  * ishtp_register_event_cb() - Register callback
558  * @device:	client device instance
559  * @event_cb:	Event processor for an client
560  *
561  * Register a callback for events, called from client driver
562  *
563  * Return: Return 0 or -EALREADY if already registered
564  */
565 int ishtp_register_event_cb(struct ishtp_cl_device *device,
566 	void (*event_cb)(struct ishtp_cl_device *))
567 {
568 	if (device->event_cb)
569 		return -EALREADY;
570 
571 	device->event_cb = event_cb;
572 	INIT_WORK(&device->event_work, ishtp_bus_event_work);
573 
574 	return 0;
575 }
576 EXPORT_SYMBOL(ishtp_register_event_cb);
577 
578 /**
579  * ishtp_get_device() - update usage count for the device
580  * @cl_device:	client device instance
581  *
582  * Increment the usage count. The device can't be deleted
583  */
584 void ishtp_get_device(struct ishtp_cl_device *cl_device)
585 {
586 	cl_device->reference_count++;
587 }
588 EXPORT_SYMBOL(ishtp_get_device);
589 
590 /**
591  * ishtp_put_device() - decrement usage count for the device
592  * @cl_device:	client device instance
593  *
594  * Decrement the usage count. The device can be deleted is count = 0
595  */
596 void ishtp_put_device(struct ishtp_cl_device *cl_device)
597 {
598 	cl_device->reference_count--;
599 }
600 EXPORT_SYMBOL(ishtp_put_device);
601 
602 /**
603  * ishtp_set_drvdata() - set client driver data
604  * @cl_device:	client device instance
605  * @data:	driver data need to be set
606  *
607  * Set client driver data to cl_device->driver_data.
608  */
609 void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
610 {
611 	cl_device->driver_data = data;
612 }
613 EXPORT_SYMBOL(ishtp_set_drvdata);
614 
615 /**
616  * ishtp_get_drvdata() - get client driver data
617  * @cl_device:	client device instance
618  *
619  * Get client driver data from cl_device->driver_data.
620  *
621  * Return: pointer of driver data
622  */
623 void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
624 {
625 	return cl_device->driver_data;
626 }
627 EXPORT_SYMBOL(ishtp_get_drvdata);
628 
629 /**
630  * ishtp_dev_to_cl_device() - get ishtp_cl_device instance from device instance
631  * @device: device instance
632  *
633  * Get ish_cl_device instance which embeds device instance in it.
634  *
635  * Return: pointer to ishtp_cl_device instance
636  */
637 struct ishtp_cl_device *ishtp_dev_to_cl_device(struct device *device)
638 {
639 	return to_ishtp_cl_device(device);
640 }
641 EXPORT_SYMBOL(ishtp_dev_to_cl_device);
642 
643 /**
644  * ishtp_bus_new_client() - Create a new client
645  * @dev:	ISHTP device instance
646  *
647  * Once bus protocol enumerates a client, this is called
648  * to add a device for the client.
649  *
650  * Return: 0 on success or error code on failure
651  */
652 int ishtp_bus_new_client(struct ishtp_device *dev)
653 {
654 	int	i;
655 	char	*dev_name;
656 	struct ishtp_cl_device	*cl_device;
657 	guid_t	device_uuid;
658 
659 	/*
660 	 * For all reported clients, create an unconnected client and add its
661 	 * device to ISHTP bus.
662 	 * If appropriate driver has loaded, this will trigger its probe().
663 	 * Otherwise, probe() will be called when driver is loaded
664 	 */
665 	i = dev->fw_client_presentation_num - 1;
666 	device_uuid = dev->fw_clients[i].props.protocol_name;
667 	dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
668 	if (!dev_name)
669 		return	-ENOMEM;
670 
671 	cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
672 	if (!cl_device) {
673 		kfree(dev_name);
674 		return	-ENOENT;
675 	}
676 
677 	kfree(dev_name);
678 
679 	return	0;
680 }
681 
682 /**
683  * ishtp_cl_device_bind() - bind a device
684  * @cl:		ishtp client device
685  *
686  * Binds connected ishtp_cl to ISHTP bus device
687  *
688  * Return: 0 on success or fault code
689  */
690 int ishtp_cl_device_bind(struct ishtp_cl *cl)
691 {
692 	struct ishtp_cl_device	*cl_device;
693 	unsigned long flags;
694 	int	rv;
695 
696 	if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
697 		return	-EFAULT;
698 
699 	rv = -ENOENT;
700 	spin_lock_irqsave(&cl->dev->device_list_lock, flags);
701 	list_for_each_entry(cl_device, &cl->dev->device_list,
702 			device_link) {
703 		if (cl_device->fw_client &&
704 		    cl_device->fw_client->client_id == cl->fw_client_id) {
705 			cl->device = cl_device;
706 			rv = 0;
707 			break;
708 		}
709 	}
710 	spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
711 	return	rv;
712 }
713 
714 /**
715  * ishtp_bus_remove_all_clients() - Remove all clients
716  * @ishtp_dev:		ishtp device
717  * @warm_reset:		Reset due to FW reset dure to errors or S3 suspend
718  *
719  * This is part of reset/remove flow. This function the main processing
720  * only targets error processing, if the FW has forced reset or
721  * error to remove connected clients. When warm reset the client devices are
722  * not removed.
723  */
724 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
725 				  bool warm_reset)
726 {
727 	struct ishtp_cl_device	*cl_device, *n;
728 	struct ishtp_cl	*cl;
729 	unsigned long	flags;
730 
731 	spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
732 	list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
733 		cl->state = ISHTP_CL_DISCONNECTED;
734 
735 		/*
736 		 * Wake any pending process. The waiter would check dev->state
737 		 * and determine that it's not enabled already,
738 		 * and will return error to its caller
739 		 */
740 		wake_up_interruptible(&cl->wait_ctrl_res);
741 
742 		/* Disband any pending read/write requests and free rb */
743 		ishtp_cl_flush_queues(cl);
744 
745 		/* Remove all free and in_process rings, both Rx and Tx */
746 		ishtp_cl_free_rx_ring(cl);
747 		ishtp_cl_free_tx_ring(cl);
748 
749 		/*
750 		 * Free client and ISHTP bus client device structures
751 		 * don't free host client because it is part of the OS fd
752 		 * structure
753 		 */
754 	}
755 	spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
756 
757 	/* Release DMA buffers for client messages */
758 	ishtp_cl_free_dma_buf(ishtp_dev);
759 
760 	/* remove bus clients */
761 	spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
762 	list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
763 				 device_link) {
764 		cl_device->fw_client = NULL;
765 		if (warm_reset && cl_device->reference_count)
766 			continue;
767 
768 		list_del(&cl_device->device_link);
769 		spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
770 		ishtp_bus_remove_device(cl_device);
771 		spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
772 	}
773 	spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
774 
775 	/* Free all client structures */
776 	spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
777 	kfree(ishtp_dev->fw_clients);
778 	ishtp_dev->fw_clients = NULL;
779 	ishtp_dev->fw_clients_num = 0;
780 	ishtp_dev->fw_client_presentation_num = 0;
781 	ishtp_dev->fw_client_index = 0;
782 	bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
783 	spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
784 }
785 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
786 
787 /**
788  * ishtp_reset_handler() - IPC reset handler
789  * @dev:	ishtp device
790  *
791  * ISHTP Handler for IPC_RESET notification
792  */
793 void ishtp_reset_handler(struct ishtp_device *dev)
794 {
795 	unsigned long	flags;
796 
797 	/* Handle FW-initiated reset */
798 	dev->dev_state = ISHTP_DEV_RESETTING;
799 
800 	/* Clear BH processing queue - no further HBMs */
801 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
802 	dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
803 	spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
804 
805 	/* Handle ISH FW reset against upper layers */
806 	ishtp_bus_remove_all_clients(dev, true);
807 }
808 EXPORT_SYMBOL(ishtp_reset_handler);
809 
810 /**
811  * ishtp_reset_compl_handler() - Reset completion handler
812  * @dev:	ishtp device
813  *
814  * ISHTP handler for IPC_RESET sequence completion to start
815  * host message bus start protocol sequence.
816  */
817 void ishtp_reset_compl_handler(struct ishtp_device *dev)
818 {
819 	dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
820 	dev->hbm_state = ISHTP_HBM_START;
821 	ishtp_hbm_start_req(dev);
822 }
823 EXPORT_SYMBOL(ishtp_reset_compl_handler);
824 
825 /**
826  * ishtp_use_dma_transfer() - Function to use DMA
827  *
828  * This interface is used to enable usage of DMA
829  *
830  * Return non zero if DMA can be enabled
831  */
832 int ishtp_use_dma_transfer(void)
833 {
834 	return ishtp_use_dma;
835 }
836 
837 /**
838  * ishtp_device() - Return device pointer
839  * @device: ISH-TP client device instance
840  *
841  * This interface is used to return device pointer from ishtp_cl_device
842  * instance.
843  *
844  * Return: device *.
845  */
846 struct device *ishtp_device(struct ishtp_cl_device *device)
847 {
848 	return &device->dev;
849 }
850 EXPORT_SYMBOL(ishtp_device);
851 
852 /**
853  * ishtp_get_pci_device() - Return PCI device dev pointer
854  * This interface is used to return PCI device pointer
855  * from ishtp_cl_device instance.
856  * @device: ISH-TP client device instance
857  *
858  * Return: device *.
859  */
860 struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
861 {
862 	return device->ishtp_dev->devc;
863 }
864 EXPORT_SYMBOL(ishtp_get_pci_device);
865 
866 /**
867  * ishtp_trace_callback() - Return trace callback
868  * @cl_device: ISH-TP client device instance
869  *
870  * This interface is used to return trace callback function pointer.
871  *
872  * Return: *ishtp_print_log()
873  */
874 ishtp_print_log ishtp_trace_callback(struct ishtp_cl_device *cl_device)
875 {
876 	return cl_device->ishtp_dev->print_log;
877 }
878 EXPORT_SYMBOL(ishtp_trace_callback);
879 
880 /**
881  * ish_hw_reset() - Call HW reset IPC callback
882  * @dev:	ISHTP device instance
883  *
884  * This interface is used to reset HW in case of error.
885  *
886  * Return: value from IPC hw_reset callback
887  */
888 int ish_hw_reset(struct ishtp_device *dev)
889 {
890 	return dev->ops->hw_reset(dev);
891 }
892 EXPORT_SYMBOL(ish_hw_reset);
893 
894 /**
895  * ishtp_bus_register() - Function to register bus
896  *
897  * This register ishtp bus
898  *
899  * Return: Return output of bus_register
900  */
901 static int  __init ishtp_bus_register(void)
902 {
903 	return bus_register(&ishtp_cl_bus_type);
904 }
905 
906 /**
907  * ishtp_bus_unregister() - Function to unregister bus
908  *
909  * This unregister ishtp bus
910  */
911 static void __exit ishtp_bus_unregister(void)
912 {
913 	bus_unregister(&ishtp_cl_bus_type);
914 }
915 
916 module_init(ishtp_bus_register);
917 module_exit(ishtp_bus_unregister);
918 
919 MODULE_LICENSE("GPL");
920