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