xref: /OK3568_Linux_fs/kernel/drivers/visorbus/visorbus_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright � 2010 - 2015 UNISYS CORPORATION
4*4882a593Smuzhiyun  * All rights reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/ctype.h>
8*4882a593Smuzhiyun #include <linux/debugfs.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/visorbus.h>
12*4882a593Smuzhiyun #include <linux/uuid.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "visorbus_private.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* Display string that is guaranteed to be no longer the 99 characters */
19*4882a593Smuzhiyun #define LINESIZE 99
20*4882a593Smuzhiyun #define POLLJIFFIES_NORMALCHANNEL 10
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* stores whether bus_registration was successful */
23*4882a593Smuzhiyun static bool initialized;
24*4882a593Smuzhiyun static struct dentry *visorbus_debugfs_dir;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * DEVICE type attributes
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * The modalias file will contain the guid of the device.
30*4882a593Smuzhiyun  */
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)31*4882a593Smuzhiyun static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
32*4882a593Smuzhiyun 			     char *buf)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	struct visor_device *vdev;
35*4882a593Smuzhiyun 	const guid_t *guid;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	vdev = to_visor_device(dev);
38*4882a593Smuzhiyun 	guid = visorchannel_get_guid(vdev->visorchannel);
39*4882a593Smuzhiyun 	return sprintf(buf, "visorbus:%pUl\n", guid);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun static DEVICE_ATTR_RO(modalias);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static struct attribute *visorbus_dev_attrs[] = {
44*4882a593Smuzhiyun 	&dev_attr_modalias.attr,
45*4882a593Smuzhiyun 	NULL,
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun ATTRIBUTE_GROUPS(visorbus_dev);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* filled in with info about parent chipset driver when we register with it */
51*4882a593Smuzhiyun static struct visor_vbus_deviceinfo chipset_driverinfo;
52*4882a593Smuzhiyun /* filled in with info about this driver, wrt it servicing client busses */
53*4882a593Smuzhiyun static struct visor_vbus_deviceinfo clientbus_driverinfo;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* list of visor_device structs, linked via .list_all */
56*4882a593Smuzhiyun static LIST_HEAD(list_all_bus_instances);
57*4882a593Smuzhiyun /* list of visor_device structs, linked via .list_all */
58*4882a593Smuzhiyun static LIST_HEAD(list_all_device_instances);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun  * Generic function useful for validating any type of channel when it is
62*4882a593Smuzhiyun  * received by the client that will be accessing the channel.
63*4882a593Smuzhiyun  * Note that <logCtx> is only needed for callers in the EFI environment, and
64*4882a593Smuzhiyun  * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
65*4882a593Smuzhiyun  */
visor_check_channel(struct channel_header * ch,struct device * dev,const guid_t * expected_guid,char * chname,u64 expected_min_bytes,u32 expected_version,u64 expected_signature)66*4882a593Smuzhiyun int visor_check_channel(struct channel_header *ch, struct device *dev,
67*4882a593Smuzhiyun 			const guid_t *expected_guid, char *chname,
68*4882a593Smuzhiyun 			u64 expected_min_bytes, u32 expected_version,
69*4882a593Smuzhiyun 			u64 expected_signature)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	if (!guid_is_null(expected_guid)) {
72*4882a593Smuzhiyun 		/* caller wants us to verify type GUID */
73*4882a593Smuzhiyun 		if (!guid_equal(&ch->chtype, expected_guid)) {
74*4882a593Smuzhiyun 			dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
75*4882a593Smuzhiyun 				chname, expected_guid, expected_guid,
76*4882a593Smuzhiyun 				&ch->chtype);
77*4882a593Smuzhiyun 			return 0;
78*4882a593Smuzhiyun 		}
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 	/* verify channel size */
81*4882a593Smuzhiyun 	if (expected_min_bytes > 0) {
82*4882a593Smuzhiyun 		if (ch->size < expected_min_bytes) {
83*4882a593Smuzhiyun 			dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
84*4882a593Smuzhiyun 				chname, expected_guid,
85*4882a593Smuzhiyun 				(unsigned long long)expected_min_bytes,
86*4882a593Smuzhiyun 				ch->size);
87*4882a593Smuzhiyun 			return 0;
88*4882a593Smuzhiyun 		}
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 	/* verify channel version */
91*4882a593Smuzhiyun 	if (expected_version > 0) {
92*4882a593Smuzhiyun 		if (ch->version_id != expected_version) {
93*4882a593Smuzhiyun 			dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
94*4882a593Smuzhiyun 				chname, expected_guid,
95*4882a593Smuzhiyun 				(unsigned long)expected_version,
96*4882a593Smuzhiyun 				ch->version_id);
97*4882a593Smuzhiyun 			return 0;
98*4882a593Smuzhiyun 		}
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	/* verify channel signature */
101*4882a593Smuzhiyun 	if (expected_signature > 0) {
102*4882a593Smuzhiyun 		if (ch->signature != expected_signature) {
103*4882a593Smuzhiyun 			dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
104*4882a593Smuzhiyun 				chname, expected_guid,	expected_signature,
105*4882a593Smuzhiyun 				ch->signature);
106*4882a593Smuzhiyun 			return 0;
107*4882a593Smuzhiyun 		}
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 	return 1;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
visorbus_uevent(struct device * xdev,struct kobj_uevent_env * env)112*4882a593Smuzhiyun static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	struct visor_device *dev;
115*4882a593Smuzhiyun 	const guid_t *guid;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	dev = to_visor_device(xdev);
118*4882a593Smuzhiyun 	guid = visorchannel_get_guid(dev->visorchannel);
119*4882a593Smuzhiyun 	return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun  * visorbus_match() - called automatically upon adding a visor_device
124*4882a593Smuzhiyun  *                    (device_add), or adding a visor_driver
125*4882a593Smuzhiyun  *                    (visorbus_register_visor_driver)
126*4882a593Smuzhiyun  * @xdev: struct device for the device being matched
127*4882a593Smuzhiyun  * @xdrv: struct device_driver for driver to match device against
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * Return: 1 iff the provided driver can control the specified device
130*4882a593Smuzhiyun  */
visorbus_match(struct device * xdev,struct device_driver * xdrv)131*4882a593Smuzhiyun static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	const guid_t *channel_type;
134*4882a593Smuzhiyun 	int i;
135*4882a593Smuzhiyun 	struct visor_device *dev;
136*4882a593Smuzhiyun 	struct visor_driver *drv;
137*4882a593Smuzhiyun 	struct visorchannel *chan;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	dev = to_visor_device(xdev);
140*4882a593Smuzhiyun 	channel_type = visorchannel_get_guid(dev->visorchannel);
141*4882a593Smuzhiyun 	drv = to_visor_driver(xdrv);
142*4882a593Smuzhiyun 	chan = dev->visorchannel;
143*4882a593Smuzhiyun 	if (!drv->channel_types)
144*4882a593Smuzhiyun 		return 0;
145*4882a593Smuzhiyun 	for (i = 0; !guid_is_null(&drv->channel_types[i].guid); i++)
146*4882a593Smuzhiyun 		if (guid_equal(&drv->channel_types[i].guid, channel_type) &&
147*4882a593Smuzhiyun 		    visor_check_channel(visorchannel_get_header(chan),
148*4882a593Smuzhiyun 					xdev,
149*4882a593Smuzhiyun 					&drv->channel_types[i].guid,
150*4882a593Smuzhiyun 					(char *)drv->channel_types[i].name,
151*4882a593Smuzhiyun 					drv->channel_types[i].min_bytes,
152*4882a593Smuzhiyun 					drv->channel_types[i].version,
153*4882a593Smuzhiyun 					VISOR_CHANNEL_SIGNATURE))
154*4882a593Smuzhiyun 			return i + 1;
155*4882a593Smuzhiyun 	return 0;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun  * This describes the TYPE of bus.
160*4882a593Smuzhiyun  * (Don't confuse this with an INSTANCE of the bus.)
161*4882a593Smuzhiyun  */
162*4882a593Smuzhiyun static struct bus_type visorbus_type = {
163*4882a593Smuzhiyun 	.name = "visorbus",
164*4882a593Smuzhiyun 	.match = visorbus_match,
165*4882a593Smuzhiyun 	.uevent = visorbus_uevent,
166*4882a593Smuzhiyun 	.dev_groups = visorbus_dev_groups,
167*4882a593Smuzhiyun };
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun struct visor_busdev {
170*4882a593Smuzhiyun 	u32 bus_no;
171*4882a593Smuzhiyun 	u32 dev_no;
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
match_visorbus_dev_by_id(struct device * dev,const void * data)174*4882a593Smuzhiyun static int match_visorbus_dev_by_id(struct device *dev, const void *data)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
177*4882a593Smuzhiyun 	const struct visor_busdev *id = data;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (vdev->chipset_bus_no == id->bus_no &&
180*4882a593Smuzhiyun 	    vdev->chipset_dev_no == id->dev_no)
181*4882a593Smuzhiyun 		return 1;
182*4882a593Smuzhiyun 	return 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
visorbus_get_device_by_id(u32 bus_no,u32 dev_no,struct visor_device * from)185*4882a593Smuzhiyun struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
186*4882a593Smuzhiyun 					       struct visor_device *from)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	struct device *dev;
189*4882a593Smuzhiyun 	struct device *dev_start = NULL;
190*4882a593Smuzhiyun 	struct visor_busdev id = {
191*4882a593Smuzhiyun 		.bus_no = bus_no,
192*4882a593Smuzhiyun 		.dev_no = dev_no
193*4882a593Smuzhiyun 	};
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (from)
196*4882a593Smuzhiyun 		dev_start = &from->device;
197*4882a593Smuzhiyun 	dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
198*4882a593Smuzhiyun 			      match_visorbus_dev_by_id);
199*4882a593Smuzhiyun 	if (!dev)
200*4882a593Smuzhiyun 		return NULL;
201*4882a593Smuzhiyun 	return to_visor_device(dev);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun  * visorbus_release_busdevice() - called when device_unregister() is called for
206*4882a593Smuzhiyun  *                                the bus device instance, after all other tasks
207*4882a593Smuzhiyun  *                                involved with destroying the dev are complete
208*4882a593Smuzhiyun  * @xdev: struct device for the bus being released
209*4882a593Smuzhiyun  */
visorbus_release_busdevice(struct device * xdev)210*4882a593Smuzhiyun static void visorbus_release_busdevice(struct device *xdev)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct visor_device *dev = dev_get_drvdata(xdev);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	debugfs_remove(dev->debugfs_bus_info);
215*4882a593Smuzhiyun 	debugfs_remove_recursive(dev->debugfs_dir);
216*4882a593Smuzhiyun 	visorchannel_destroy(dev->visorchannel);
217*4882a593Smuzhiyun 	kfree(dev);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun  * visorbus_release_device() - called when device_unregister() is called for
222*4882a593Smuzhiyun  *                             each child device instance
223*4882a593Smuzhiyun  * @xdev: struct device for the visor device being released
224*4882a593Smuzhiyun  */
visorbus_release_device(struct device * xdev)225*4882a593Smuzhiyun static void visorbus_release_device(struct device *xdev)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	struct visor_device *dev = to_visor_device(xdev);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	visorchannel_destroy(dev->visorchannel);
230*4882a593Smuzhiyun 	kfree(dev);
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun  * BUS specific channel attributes to appear under
235*4882a593Smuzhiyun  * /sys/bus/visorbus<x>/dev<y>/channel
236*4882a593Smuzhiyun  */
237*4882a593Smuzhiyun 
physaddr_show(struct device * dev,struct device_attribute * attr,char * buf)238*4882a593Smuzhiyun static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
239*4882a593Smuzhiyun 			     char *buf)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	return sprintf(buf, "0x%llx\n",
244*4882a593Smuzhiyun 		       visorchannel_get_physaddr(vdev->visorchannel));
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun static DEVICE_ATTR_RO(physaddr);
247*4882a593Smuzhiyun 
nbytes_show(struct device * dev,struct device_attribute * attr,char * buf)248*4882a593Smuzhiyun static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
249*4882a593Smuzhiyun 			   char *buf)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	return sprintf(buf, "0x%lx\n",
254*4882a593Smuzhiyun 		       visorchannel_get_nbytes(vdev->visorchannel));
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun static DEVICE_ATTR_RO(nbytes);
257*4882a593Smuzhiyun 
clientpartition_show(struct device * dev,struct device_attribute * attr,char * buf)258*4882a593Smuzhiyun static ssize_t clientpartition_show(struct device *dev,
259*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return sprintf(buf, "0x%llx\n",
264*4882a593Smuzhiyun 		       visorchannel_get_clientpartition(vdev->visorchannel));
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun static DEVICE_ATTR_RO(clientpartition);
267*4882a593Smuzhiyun 
typeguid_show(struct device * dev,struct device_attribute * attr,char * buf)268*4882a593Smuzhiyun static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
269*4882a593Smuzhiyun 			     char *buf)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
272*4882a593Smuzhiyun 	char typeid[LINESIZE];
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return sprintf(buf, "%s\n",
275*4882a593Smuzhiyun 		       visorchannel_id(vdev->visorchannel, typeid));
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun static DEVICE_ATTR_RO(typeguid);
278*4882a593Smuzhiyun 
zoneguid_show(struct device * dev,struct device_attribute * attr,char * buf)279*4882a593Smuzhiyun static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
280*4882a593Smuzhiyun 			     char *buf)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
283*4882a593Smuzhiyun 	char zoneid[LINESIZE];
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return sprintf(buf, "%s\n",
286*4882a593Smuzhiyun 		       visorchannel_zoneid(vdev->visorchannel, zoneid));
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun static DEVICE_ATTR_RO(zoneguid);
289*4882a593Smuzhiyun 
typename_show(struct device * dev,struct device_attribute * attr,char * buf)290*4882a593Smuzhiyun static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
291*4882a593Smuzhiyun 			     char *buf)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	int i = 0;
294*4882a593Smuzhiyun 	struct bus_type *xbus = dev->bus;
295*4882a593Smuzhiyun 	struct device_driver *xdrv = dev->driver;
296*4882a593Smuzhiyun 	struct visor_driver *drv = NULL;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (!xdrv)
299*4882a593Smuzhiyun 		return 0;
300*4882a593Smuzhiyun 	i = xbus->match(dev, xdrv);
301*4882a593Smuzhiyun 	if (!i)
302*4882a593Smuzhiyun 		return 0;
303*4882a593Smuzhiyun 	drv = to_visor_driver(xdrv);
304*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun static DEVICE_ATTR_RO(typename);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun static struct attribute *channel_attrs[] = {
309*4882a593Smuzhiyun 	&dev_attr_physaddr.attr,
310*4882a593Smuzhiyun 	&dev_attr_nbytes.attr,
311*4882a593Smuzhiyun 	&dev_attr_clientpartition.attr,
312*4882a593Smuzhiyun 	&dev_attr_typeguid.attr,
313*4882a593Smuzhiyun 	&dev_attr_zoneguid.attr,
314*4882a593Smuzhiyun 	&dev_attr_typename.attr,
315*4882a593Smuzhiyun 	NULL
316*4882a593Smuzhiyun };
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun ATTRIBUTE_GROUPS(channel);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun  *  BUS instance attributes
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  *  define & implement display of bus attributes under
324*4882a593Smuzhiyun  *  /sys/bus/visorbus/devices/visorbus<n>.
325*4882a593Smuzhiyun  */
partition_handle_show(struct device * dev,struct device_attribute * attr,char * buf)326*4882a593Smuzhiyun static ssize_t partition_handle_show(struct device *dev,
327*4882a593Smuzhiyun 				     struct device_attribute *attr, char *buf)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
330*4882a593Smuzhiyun 	u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	return sprintf(buf, "0x%llx\n", handle);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun static DEVICE_ATTR_RO(partition_handle);
335*4882a593Smuzhiyun 
partition_guid_show(struct device * dev,struct device_attribute * attr,char * buf)336*4882a593Smuzhiyun static ssize_t partition_guid_show(struct device *dev,
337*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun static DEVICE_ATTR_RO(partition_guid);
344*4882a593Smuzhiyun 
partition_name_show(struct device * dev,struct device_attribute * attr,char * buf)345*4882a593Smuzhiyun static ssize_t partition_name_show(struct device *dev,
346*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", vdev->name);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun static DEVICE_ATTR_RO(partition_name);
353*4882a593Smuzhiyun 
channel_addr_show(struct device * dev,struct device_attribute * attr,char * buf)354*4882a593Smuzhiyun static ssize_t channel_addr_show(struct device *dev,
355*4882a593Smuzhiyun 				 struct device_attribute *attr, char *buf)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
358*4882a593Smuzhiyun 	u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return sprintf(buf, "0x%llx\n", addr);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun static DEVICE_ATTR_RO(channel_addr);
363*4882a593Smuzhiyun 
channel_bytes_show(struct device * dev,struct device_attribute * attr,char * buf)364*4882a593Smuzhiyun static ssize_t channel_bytes_show(struct device *dev,
365*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
368*4882a593Smuzhiyun 	u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	return sprintf(buf, "0x%llx\n", nbytes);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun static DEVICE_ATTR_RO(channel_bytes);
373*4882a593Smuzhiyun 
channel_id_show(struct device * dev,struct device_attribute * attr,char * buf)374*4882a593Smuzhiyun static ssize_t channel_id_show(struct device *dev,
375*4882a593Smuzhiyun 			       struct device_attribute *attr, char *buf)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	struct visor_device *vdev = to_visor_device(dev);
378*4882a593Smuzhiyun 	int len = 0;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	visorchannel_id(vdev->visorchannel, buf);
381*4882a593Smuzhiyun 	len = strlen(buf);
382*4882a593Smuzhiyun 	buf[len++] = '\n';
383*4882a593Smuzhiyun 	return len;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun static DEVICE_ATTR_RO(channel_id);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun static struct attribute *visorbus_attrs[] = {
388*4882a593Smuzhiyun 	&dev_attr_partition_handle.attr,
389*4882a593Smuzhiyun 	&dev_attr_partition_guid.attr,
390*4882a593Smuzhiyun 	&dev_attr_partition_name.attr,
391*4882a593Smuzhiyun 	&dev_attr_channel_addr.attr,
392*4882a593Smuzhiyun 	&dev_attr_channel_bytes.attr,
393*4882a593Smuzhiyun 	&dev_attr_channel_id.attr,
394*4882a593Smuzhiyun 	NULL
395*4882a593Smuzhiyun };
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun ATTRIBUTE_GROUPS(visorbus);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun  *  BUS debugfs entries
401*4882a593Smuzhiyun  *
402*4882a593Smuzhiyun  *  define & implement display of debugfs attributes under
403*4882a593Smuzhiyun  *  /sys/kernel/debug/visorbus/visorbus<n>.
404*4882a593Smuzhiyun  */
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /*
407*4882a593Smuzhiyun  * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
408*4882a593Smuzhiyun  *                               and write it to a seq_file
409*4882a593Smuzhiyun  * @devinfo: the struct visor_vbus_deviceinfo to format
410*4882a593Smuzhiyun  * @seq: seq_file to write to
411*4882a593Smuzhiyun  * @devix: the device index to be included in the output data, or -1 if no
412*4882a593Smuzhiyun  *         device index is to be included
413*4882a593Smuzhiyun  *
414*4882a593Smuzhiyun  * Reads @devInfo, and writes it in human-readable notation to @seq.
415*4882a593Smuzhiyun  */
vbuschannel_print_devinfo(struct visor_vbus_deviceinfo * devinfo,struct seq_file * seq,int devix)416*4882a593Smuzhiyun static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
417*4882a593Smuzhiyun 				      struct seq_file *seq, int devix)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	/* uninitialized vbus device entry */
420*4882a593Smuzhiyun 	if (!isprint(devinfo->devtype[0]))
421*4882a593Smuzhiyun 		return;
422*4882a593Smuzhiyun 	if (devix >= 0)
423*4882a593Smuzhiyun 		seq_printf(seq, "[%d]", devix);
424*4882a593Smuzhiyun 	else
425*4882a593Smuzhiyun 		/* vbus device entry is for bus or chipset */
426*4882a593Smuzhiyun 		seq_puts(seq, "   ");
427*4882a593Smuzhiyun 	/*
428*4882a593Smuzhiyun 	 * Note: because the s-Par back-end is free to scribble in this area,
429*4882a593Smuzhiyun 	 * we never assume '\0'-termination.
430*4882a593Smuzhiyun 	 */
431*4882a593Smuzhiyun 	seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
432*4882a593Smuzhiyun 		   (int)sizeof(devinfo->devtype), devinfo->devtype);
433*4882a593Smuzhiyun 	seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
434*4882a593Smuzhiyun 		   (int)sizeof(devinfo->drvname), devinfo->drvname);
435*4882a593Smuzhiyun 	seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
436*4882a593Smuzhiyun 		   devinfo->infostrs);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
bus_info_debugfs_show(struct seq_file * seq,void * v)439*4882a593Smuzhiyun static int bus_info_debugfs_show(struct seq_file *seq, void *v)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	int i = 0;
442*4882a593Smuzhiyun 	unsigned long off;
443*4882a593Smuzhiyun 	struct visor_vbus_deviceinfo dev_info;
444*4882a593Smuzhiyun 	struct visor_device *vdev = seq->private;
445*4882a593Smuzhiyun 	struct visorchannel *channel = vdev->visorchannel;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	if (!channel)
448*4882a593Smuzhiyun 		return 0;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	seq_printf(seq,
451*4882a593Smuzhiyun 		   "Client device/driver info for %s partition (vbus #%u):\n",
452*4882a593Smuzhiyun 		   ((vdev->name) ? (char *)(vdev->name) : ""),
453*4882a593Smuzhiyun 		   vdev->chipset_bus_no);
454*4882a593Smuzhiyun 	if (visorchannel_read(channel,
455*4882a593Smuzhiyun 			      offsetof(struct visor_vbus_channel, chp_info),
456*4882a593Smuzhiyun 			      &dev_info, sizeof(dev_info)) >= 0)
457*4882a593Smuzhiyun 		vbuschannel_print_devinfo(&dev_info, seq, -1);
458*4882a593Smuzhiyun 	if (visorchannel_read(channel,
459*4882a593Smuzhiyun 			      offsetof(struct visor_vbus_channel, bus_info),
460*4882a593Smuzhiyun 			      &dev_info, sizeof(dev_info)) >= 0)
461*4882a593Smuzhiyun 		vbuschannel_print_devinfo(&dev_info, seq, -1);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	off = offsetof(struct visor_vbus_channel, dev_info);
464*4882a593Smuzhiyun 	while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
465*4882a593Smuzhiyun 		if (visorchannel_read(channel, off, &dev_info,
466*4882a593Smuzhiyun 				      sizeof(dev_info)) >= 0)
467*4882a593Smuzhiyun 			vbuschannel_print_devinfo(&dev_info, seq, i);
468*4882a593Smuzhiyun 		off += sizeof(dev_info);
469*4882a593Smuzhiyun 		i++;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 	return 0;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
bus_info_debugfs_open(struct inode * inode,struct file * file)474*4882a593Smuzhiyun static int bus_info_debugfs_open(struct inode *inode, struct file *file)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	return single_open(file, bus_info_debugfs_show, inode->i_private);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun static const struct file_operations bus_info_debugfs_fops = {
480*4882a593Smuzhiyun 	.owner = THIS_MODULE,
481*4882a593Smuzhiyun 	.open = bus_info_debugfs_open,
482*4882a593Smuzhiyun 	.read = seq_read,
483*4882a593Smuzhiyun 	.llseek = seq_lseek,
484*4882a593Smuzhiyun 	.release = single_release,
485*4882a593Smuzhiyun };
486*4882a593Smuzhiyun 
dev_periodic_work(struct timer_list * t)487*4882a593Smuzhiyun static void dev_periodic_work(struct timer_list *t)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	struct visor_device *dev = from_timer(dev, t, timer);
490*4882a593Smuzhiyun 	struct visor_driver *drv = to_visor_driver(dev->device.driver);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	drv->channel_interrupt(dev);
493*4882a593Smuzhiyun 	mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
dev_start_periodic_work(struct visor_device * dev)496*4882a593Smuzhiyun static int dev_start_periodic_work(struct visor_device *dev)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	if (dev->being_removed || dev->timer_active)
499*4882a593Smuzhiyun 		return -EINVAL;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	/* now up by at least 2 */
502*4882a593Smuzhiyun 	get_device(&dev->device);
503*4882a593Smuzhiyun 	dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
504*4882a593Smuzhiyun 	add_timer(&dev->timer);
505*4882a593Smuzhiyun 	dev->timer_active = true;
506*4882a593Smuzhiyun 	return 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
dev_stop_periodic_work(struct visor_device * dev)509*4882a593Smuzhiyun static void dev_stop_periodic_work(struct visor_device *dev)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	if (!dev->timer_active)
512*4882a593Smuzhiyun 		return;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	del_timer_sync(&dev->timer);
515*4882a593Smuzhiyun 	dev->timer_active = false;
516*4882a593Smuzhiyun 	put_device(&dev->device);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun  * visordriver_remove_device() - handle visor device going away
521*4882a593Smuzhiyun  * @xdev: struct device for the visor device being removed
522*4882a593Smuzhiyun  *
523*4882a593Smuzhiyun  * This is called when device_unregister() is called for each child device
524*4882a593Smuzhiyun  * instance, to notify the appropriate visorbus function driver that the device
525*4882a593Smuzhiyun  * is going away, and to decrease the reference count of the device.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * Return: 0 iff successful
528*4882a593Smuzhiyun  */
visordriver_remove_device(struct device * xdev)529*4882a593Smuzhiyun static int visordriver_remove_device(struct device *xdev)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	struct visor_device *dev = to_visor_device(xdev);
532*4882a593Smuzhiyun 	struct visor_driver *drv = to_visor_driver(xdev->driver);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	mutex_lock(&dev->visordriver_callback_lock);
535*4882a593Smuzhiyun 	dev->being_removed = true;
536*4882a593Smuzhiyun 	drv->remove(dev);
537*4882a593Smuzhiyun 	mutex_unlock(&dev->visordriver_callback_lock);
538*4882a593Smuzhiyun 	dev_stop_periodic_work(dev);
539*4882a593Smuzhiyun 	put_device(&dev->device);
540*4882a593Smuzhiyun 	return 0;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * visorbus_unregister_visor_driver() - unregisters the provided driver
545*4882a593Smuzhiyun  * @drv: the driver to unregister
546*4882a593Smuzhiyun  *
547*4882a593Smuzhiyun  * A visor function driver calls this function to unregister the driver,
548*4882a593Smuzhiyun  * i.e., within its module_exit function.
549*4882a593Smuzhiyun  */
visorbus_unregister_visor_driver(struct visor_driver * drv)550*4882a593Smuzhiyun void visorbus_unregister_visor_driver(struct visor_driver *drv)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	driver_unregister(&drv->driver);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun  * visorbus_read_channel() - reads from the designated channel into
558*4882a593Smuzhiyun  *                           the provided buffer
559*4882a593Smuzhiyun  * @dev:    the device whose channel is read from
560*4882a593Smuzhiyun  * @offset: the offset into the channel at which reading starts
561*4882a593Smuzhiyun  * @dest:   the destination buffer that is written into from the channel
562*4882a593Smuzhiyun  * @nbytes: the number of bytes to read from the channel
563*4882a593Smuzhiyun  *
564*4882a593Smuzhiyun  * If receiving a message, use the visorchannel_signalremove() function instead.
565*4882a593Smuzhiyun  *
566*4882a593Smuzhiyun  * Return: integer indicating success (zero) or failure (non-zero)
567*4882a593Smuzhiyun  */
visorbus_read_channel(struct visor_device * dev,unsigned long offset,void * dest,unsigned long nbytes)568*4882a593Smuzhiyun int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
569*4882a593Smuzhiyun 			  void *dest, unsigned long nbytes)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_read_channel);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun /*
576*4882a593Smuzhiyun  * visorbus_write_channel() - writes the provided buffer into the designated
577*4882a593Smuzhiyun  *                            channel
578*4882a593Smuzhiyun  * @dev:    the device whose channel is written to
579*4882a593Smuzhiyun  * @offset: the offset into the channel at which writing starts
580*4882a593Smuzhiyun  * @src:    the source buffer that is written into the channel
581*4882a593Smuzhiyun  * @nbytes: the number of bytes to write into the channel
582*4882a593Smuzhiyun  *
583*4882a593Smuzhiyun  * If sending a message, use the visorchannel_signalinsert() function instead.
584*4882a593Smuzhiyun  *
585*4882a593Smuzhiyun  * Return: integer indicating success (zero) or failure (non-zero)
586*4882a593Smuzhiyun  */
visorbus_write_channel(struct visor_device * dev,unsigned long offset,void * src,unsigned long nbytes)587*4882a593Smuzhiyun int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
588*4882a593Smuzhiyun 			   void *src, unsigned long nbytes)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	return visorchannel_write(dev->visorchannel, offset, src, nbytes);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_write_channel);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /*
595*4882a593Smuzhiyun  * visorbus_enable_channel_interrupts() - enables interrupts on the
596*4882a593Smuzhiyun  *                                        designated device
597*4882a593Smuzhiyun  * @dev: the device on which to enable interrupts
598*4882a593Smuzhiyun  *
599*4882a593Smuzhiyun  * Currently we don't yet have a real interrupt, so for now we just call the
600*4882a593Smuzhiyun  * interrupt function periodically via a timer.
601*4882a593Smuzhiyun  */
visorbus_enable_channel_interrupts(struct visor_device * dev)602*4882a593Smuzhiyun int visorbus_enable_channel_interrupts(struct visor_device *dev)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun 	struct visor_driver *drv = to_visor_driver(dev->device.driver);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if (!drv->channel_interrupt) {
607*4882a593Smuzhiyun 		dev_err(&dev->device, "%s no interrupt function!\n", __func__);
608*4882a593Smuzhiyun 		return -ENOENT;
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	return dev_start_periodic_work(dev);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun /*
616*4882a593Smuzhiyun  * visorbus_disable_channel_interrupts() - disables interrupts on the
617*4882a593Smuzhiyun  *                                         designated device
618*4882a593Smuzhiyun  * @dev: the device on which to disable interrupts
619*4882a593Smuzhiyun  */
visorbus_disable_channel_interrupts(struct visor_device * dev)620*4882a593Smuzhiyun void visorbus_disable_channel_interrupts(struct visor_device *dev)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	dev_stop_periodic_work(dev);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun /*
627*4882a593Smuzhiyun  * create_visor_device() - create visor device as a result of receiving the
628*4882a593Smuzhiyun  *                         controlvm device_create message for a new device
629*4882a593Smuzhiyun  * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
630*4882a593Smuzhiyun  *       for chipset_bus_no and chipset_dev_no, that will be initialized
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * This is how everything starts from the device end.
633*4882a593Smuzhiyun  * This function is called when a channel first appears via a ControlVM
634*4882a593Smuzhiyun  * message.  In response, this function allocates a visor_device to correspond
635*4882a593Smuzhiyun  * to the new channel, and attempts to connect it the appropriate * driver. If
636*4882a593Smuzhiyun  * the appropriate driver is found, the visor_driver.probe() function for that
637*4882a593Smuzhiyun  * driver will be called, and will be passed the new * visor_device that we
638*4882a593Smuzhiyun  * just created.
639*4882a593Smuzhiyun  *
640*4882a593Smuzhiyun  * It's ok if the appropriate driver is not yet loaded, because in that case
641*4882a593Smuzhiyun  * the new device struct will just stick around in the bus' list of devices.
642*4882a593Smuzhiyun  * When the appropriate driver calls visorbus_register_visor_driver(), the
643*4882a593Smuzhiyun  * visor_driver.probe() for the new driver will be called with the new device.
644*4882a593Smuzhiyun  *
645*4882a593Smuzhiyun  * Return: 0 if successful, otherwise the negative value returned by
646*4882a593Smuzhiyun  *         device_add() indicating the reason for failure
647*4882a593Smuzhiyun  */
create_visor_device(struct visor_device * dev)648*4882a593Smuzhiyun int create_visor_device(struct visor_device *dev)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	int err;
651*4882a593Smuzhiyun 	u32 chipset_bus_no = dev->chipset_bus_no;
652*4882a593Smuzhiyun 	u32 chipset_dev_no = dev->chipset_dev_no;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	mutex_init(&dev->visordriver_callback_lock);
655*4882a593Smuzhiyun 	dev->device.bus = &visorbus_type;
656*4882a593Smuzhiyun 	dev->device.groups = channel_groups;
657*4882a593Smuzhiyun 	device_initialize(&dev->device);
658*4882a593Smuzhiyun 	dev->device.release = visorbus_release_device;
659*4882a593Smuzhiyun 	/* keep a reference just for us (now 2) */
660*4882a593Smuzhiyun 	get_device(&dev->device);
661*4882a593Smuzhiyun 	timer_setup(&dev->timer, dev_periodic_work, 0);
662*4882a593Smuzhiyun 	/*
663*4882a593Smuzhiyun 	 * bus_id must be a unique name with respect to this bus TYPE (NOT bus
664*4882a593Smuzhiyun 	 * instance).  That's why we need to include the bus number within the
665*4882a593Smuzhiyun 	 * name.
666*4882a593Smuzhiyun 	 */
667*4882a593Smuzhiyun 	err = dev_set_name(&dev->device, "vbus%u:dev%u",
668*4882a593Smuzhiyun 			   chipset_bus_no, chipset_dev_no);
669*4882a593Smuzhiyun 	if (err)
670*4882a593Smuzhiyun 		goto err_put;
671*4882a593Smuzhiyun 	/*
672*4882a593Smuzhiyun 	 * device_add does this:
673*4882a593Smuzhiyun 	 *    bus_add_device(dev)
674*4882a593Smuzhiyun 	 *    ->device_attach(dev)
675*4882a593Smuzhiyun 	 *      ->for each driver drv registered on the bus that dev is on
676*4882a593Smuzhiyun 	 *          if (dev.drv)  **  device already has a driver **
677*4882a593Smuzhiyun 	 *            ** not sure we could ever get here... **
678*4882a593Smuzhiyun 	 *          else
679*4882a593Smuzhiyun 	 *            if (bus.match(dev,drv)) [visorbus_match]
680*4882a593Smuzhiyun 	 *              dev.drv = drv
681*4882a593Smuzhiyun 	 *              if (!drv.probe(dev))  [visordriver_probe_device]
682*4882a593Smuzhiyun 	 *                dev.drv = NULL
683*4882a593Smuzhiyun 	 *
684*4882a593Smuzhiyun 	 * Note that device_add does NOT fail if no driver failed to claim the
685*4882a593Smuzhiyun 	 * device.  The device will be linked onto bus_type.klist_devices
686*4882a593Smuzhiyun 	 * regardless (use bus_for_each_dev).
687*4882a593Smuzhiyun 	 */
688*4882a593Smuzhiyun 	err = device_add(&dev->device);
689*4882a593Smuzhiyun 	if (err < 0)
690*4882a593Smuzhiyun 		goto err_put;
691*4882a593Smuzhiyun 	list_add_tail(&dev->list_all, &list_all_device_instances);
692*4882a593Smuzhiyun 	dev->state.created = 1;
693*4882a593Smuzhiyun 	visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
694*4882a593Smuzhiyun 	/* success: reference kept via unmatched get_device() */
695*4882a593Smuzhiyun 	return 0;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun err_put:
698*4882a593Smuzhiyun 	put_device(&dev->device);
699*4882a593Smuzhiyun 	dev_err(&dev->device, "Creating visor device failed. %d\n", err);
700*4882a593Smuzhiyun 	return err;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
remove_visor_device(struct visor_device * dev)703*4882a593Smuzhiyun void remove_visor_device(struct visor_device *dev)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun 	list_del(&dev->list_all);
706*4882a593Smuzhiyun 	put_device(&dev->device);
707*4882a593Smuzhiyun 	if (dev->pending_msg_hdr)
708*4882a593Smuzhiyun 		visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
709*4882a593Smuzhiyun 	device_unregister(&dev->device);
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun 
get_vbus_header_info(struct visorchannel * chan,struct device * dev,struct visor_vbus_headerinfo * hdr_info)712*4882a593Smuzhiyun static int get_vbus_header_info(struct visorchannel *chan,
713*4882a593Smuzhiyun 				struct device *dev,
714*4882a593Smuzhiyun 				struct visor_vbus_headerinfo *hdr_info)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	int err;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (!visor_check_channel(visorchannel_get_header(chan),
719*4882a593Smuzhiyun 				 dev,
720*4882a593Smuzhiyun 				 &visor_vbus_channel_guid,
721*4882a593Smuzhiyun 				 "vbus",
722*4882a593Smuzhiyun 				 sizeof(struct visor_vbus_channel),
723*4882a593Smuzhiyun 				 VISOR_VBUS_CHANNEL_VERSIONID,
724*4882a593Smuzhiyun 				 VISOR_CHANNEL_SIGNATURE))
725*4882a593Smuzhiyun 		return -EINVAL;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
728*4882a593Smuzhiyun 				sizeof(*hdr_info));
729*4882a593Smuzhiyun 	if (err < 0)
730*4882a593Smuzhiyun 		return err;
731*4882a593Smuzhiyun 	if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
732*4882a593Smuzhiyun 		return -EINVAL;
733*4882a593Smuzhiyun 	if (hdr_info->device_info_struct_bytes <
734*4882a593Smuzhiyun 	    sizeof(struct visor_vbus_deviceinfo))
735*4882a593Smuzhiyun 		return -EINVAL;
736*4882a593Smuzhiyun 	return 0;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun /*
740*4882a593Smuzhiyun  * write_vbus_chp_info() - write the contents of <info> to the struct
741*4882a593Smuzhiyun  *                         visor_vbus_channel.chp_info
742*4882a593Smuzhiyun  * @chan:     indentifies the s-Par channel that will be updated
743*4882a593Smuzhiyun  * @hdr_info: used to find appropriate channel offset to write data
744*4882a593Smuzhiyun  * @info:     contains the information to write
745*4882a593Smuzhiyun  *
746*4882a593Smuzhiyun  * Writes chipset info into the channel memory to be used for diagnostic
747*4882a593Smuzhiyun  * purposes.
748*4882a593Smuzhiyun  *
749*4882a593Smuzhiyun  * Returns no value since this is debug information and not needed for
750*4882a593Smuzhiyun  * device functionality.
751*4882a593Smuzhiyun  */
write_vbus_chp_info(struct visorchannel * chan,struct visor_vbus_headerinfo * hdr_info,struct visor_vbus_deviceinfo * info)752*4882a593Smuzhiyun static void write_vbus_chp_info(struct visorchannel *chan,
753*4882a593Smuzhiyun 				struct visor_vbus_headerinfo *hdr_info,
754*4882a593Smuzhiyun 				struct visor_vbus_deviceinfo *info)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun 	int off;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	if (hdr_info->chp_info_offset == 0)
759*4882a593Smuzhiyun 		return;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
762*4882a593Smuzhiyun 	visorchannel_write(chan, off, info, sizeof(*info));
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun /*
766*4882a593Smuzhiyun  * write_vbus_bus_info() - write the contents of <info> to the struct
767*4882a593Smuzhiyun  *                         visor_vbus_channel.bus_info
768*4882a593Smuzhiyun  * @chan:     indentifies the s-Par channel that will be updated
769*4882a593Smuzhiyun  * @hdr_info: used to find appropriate channel offset to write data
770*4882a593Smuzhiyun  * @info:     contains the information to write
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  * Writes bus info into the channel memory to be used for diagnostic
773*4882a593Smuzhiyun  * purposes.
774*4882a593Smuzhiyun  *
775*4882a593Smuzhiyun  * Returns no value since this is debug information and not needed for
776*4882a593Smuzhiyun  * device functionality.
777*4882a593Smuzhiyun  */
write_vbus_bus_info(struct visorchannel * chan,struct visor_vbus_headerinfo * hdr_info,struct visor_vbus_deviceinfo * info)778*4882a593Smuzhiyun static void write_vbus_bus_info(struct visorchannel *chan,
779*4882a593Smuzhiyun 				struct visor_vbus_headerinfo *hdr_info,
780*4882a593Smuzhiyun 				struct visor_vbus_deviceinfo *info)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	int off;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	if (hdr_info->bus_info_offset == 0)
785*4882a593Smuzhiyun 		return;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
788*4882a593Smuzhiyun 	visorchannel_write(chan, off, info, sizeof(*info));
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun /*
792*4882a593Smuzhiyun  * write_vbus_dev_info() - write the contents of <info> to the struct
793*4882a593Smuzhiyun  *                         visor_vbus_channel.dev_info[<devix>]
794*4882a593Smuzhiyun  * @chan:     indentifies the s-Par channel that will be updated
795*4882a593Smuzhiyun  * @hdr_info: used to find appropriate channel offset to write data
796*4882a593Smuzhiyun  * @info:     contains the information to write
797*4882a593Smuzhiyun  * @devix:    the relative device number (0..n-1) of the device on the bus
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * Writes device info into the channel memory to be used for diagnostic
800*4882a593Smuzhiyun  * purposes.
801*4882a593Smuzhiyun  *
802*4882a593Smuzhiyun  * Returns no value since this is debug information and not needed for
803*4882a593Smuzhiyun  * device functionality.
804*4882a593Smuzhiyun  */
write_vbus_dev_info(struct visorchannel * chan,struct visor_vbus_headerinfo * hdr_info,struct visor_vbus_deviceinfo * info,unsigned int devix)805*4882a593Smuzhiyun static void write_vbus_dev_info(struct visorchannel *chan,
806*4882a593Smuzhiyun 				struct visor_vbus_headerinfo *hdr_info,
807*4882a593Smuzhiyun 				struct visor_vbus_deviceinfo *info,
808*4882a593Smuzhiyun 				unsigned int devix)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun 	int off;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	if (hdr_info->dev_info_offset == 0)
813*4882a593Smuzhiyun 		return;
814*4882a593Smuzhiyun 	off = (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
815*4882a593Smuzhiyun 	      (hdr_info->device_info_struct_bytes * devix);
816*4882a593Smuzhiyun 	visorchannel_write(chan, off, info, sizeof(*info));
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
bus_device_info_init(struct visor_vbus_deviceinfo * bus_device_info_ptr,const char * dev_type,const char * drv_name)819*4882a593Smuzhiyun static void bus_device_info_init(
820*4882a593Smuzhiyun 		struct visor_vbus_deviceinfo *bus_device_info_ptr,
821*4882a593Smuzhiyun 		const char *dev_type, const char *drv_name)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun 	memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
824*4882a593Smuzhiyun 	snprintf(bus_device_info_ptr->devtype,
825*4882a593Smuzhiyun 		 sizeof(bus_device_info_ptr->devtype),
826*4882a593Smuzhiyun 		 "%s", (dev_type) ? dev_type : "unknownType");
827*4882a593Smuzhiyun 	snprintf(bus_device_info_ptr->drvname,
828*4882a593Smuzhiyun 		 sizeof(bus_device_info_ptr->drvname),
829*4882a593Smuzhiyun 		 "%s", (drv_name) ? drv_name : "unknownDriver");
830*4882a593Smuzhiyun 	snprintf(bus_device_info_ptr->infostrs,
831*4882a593Smuzhiyun 		 sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
832*4882a593Smuzhiyun 		 utsname()->release);
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun /*
836*4882a593Smuzhiyun  * publish_vbus_dev_info() - for a child device just created on a client bus,
837*4882a593Smuzhiyun  *			     fill in information about the driver that is
838*4882a593Smuzhiyun  *			     controlling this device into the appropriate slot
839*4882a593Smuzhiyun  *			     within the vbus channel of the bus instance
840*4882a593Smuzhiyun  * @visordev: struct visor_device for the desired device
841*4882a593Smuzhiyun  */
publish_vbus_dev_info(struct visor_device * visordev)842*4882a593Smuzhiyun static void publish_vbus_dev_info(struct visor_device *visordev)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	int i;
845*4882a593Smuzhiyun 	struct visor_device *bdev;
846*4882a593Smuzhiyun 	struct visor_driver *visordrv;
847*4882a593Smuzhiyun 	u32 bus_no = visordev->chipset_bus_no;
848*4882a593Smuzhiyun 	u32 dev_no = visordev->chipset_dev_no;
849*4882a593Smuzhiyun 	struct visor_vbus_deviceinfo dev_info;
850*4882a593Smuzhiyun 	const char *chan_type_name = NULL;
851*4882a593Smuzhiyun 	struct visor_vbus_headerinfo *hdr_info;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	if (!visordev->device.driver)
854*4882a593Smuzhiyun 		return;
855*4882a593Smuzhiyun 	bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
856*4882a593Smuzhiyun 	if (!bdev)
857*4882a593Smuzhiyun 		return;
858*4882a593Smuzhiyun 	hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
859*4882a593Smuzhiyun 	if (!hdr_info)
860*4882a593Smuzhiyun 		return;
861*4882a593Smuzhiyun 	visordrv = to_visor_driver(visordev->device.driver);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	/*
864*4882a593Smuzhiyun 	 * Within the list of device types (by GUID) that the driver
865*4882a593Smuzhiyun 	 * says it supports, find out which one of those types matches
866*4882a593Smuzhiyun 	 * the type of this device, so that we can include the device
867*4882a593Smuzhiyun 	 * type name
868*4882a593Smuzhiyun 	 */
869*4882a593Smuzhiyun 	for (i = 0; visordrv->channel_types[i].name; i++) {
870*4882a593Smuzhiyun 		if (guid_equal(&visordrv->channel_types[i].guid,
871*4882a593Smuzhiyun 			       &visordev->channel_type_guid)) {
872*4882a593Smuzhiyun 			chan_type_name = visordrv->channel_types[i].name;
873*4882a593Smuzhiyun 			break;
874*4882a593Smuzhiyun 		}
875*4882a593Smuzhiyun 	}
876*4882a593Smuzhiyun 	bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
877*4882a593Smuzhiyun 	write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
878*4882a593Smuzhiyun 	write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
879*4882a593Smuzhiyun 	write_vbus_bus_info(bdev->visorchannel, hdr_info,
880*4882a593Smuzhiyun 			    &clientbus_driverinfo);
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun /*
884*4882a593Smuzhiyun  * visordriver_probe_device() - handle new visor device coming online
885*4882a593Smuzhiyun  * @xdev: struct device for the visor device being probed
886*4882a593Smuzhiyun  *
887*4882a593Smuzhiyun  * This is called automatically upon adding a visor_device (device_add), or
888*4882a593Smuzhiyun  * adding a visor_driver (visorbus_register_visor_driver), but only after
889*4882a593Smuzhiyun  * visorbus_match() has returned 1 to indicate a successful match between
890*4882a593Smuzhiyun  * driver and device.
891*4882a593Smuzhiyun  *
892*4882a593Smuzhiyun  * If successful, a reference to the device will be held onto via get_device().
893*4882a593Smuzhiyun  *
894*4882a593Smuzhiyun  * Return: 0 if successful, meaning the function driver's probe() function
895*4882a593Smuzhiyun  *         was successful with this device, otherwise a negative errno
896*4882a593Smuzhiyun  *         value indicating failure reason
897*4882a593Smuzhiyun  */
visordriver_probe_device(struct device * xdev)898*4882a593Smuzhiyun static int visordriver_probe_device(struct device *xdev)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	int err;
901*4882a593Smuzhiyun 	struct visor_driver *drv = to_visor_driver(xdev->driver);
902*4882a593Smuzhiyun 	struct visor_device *dev = to_visor_device(xdev);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	mutex_lock(&dev->visordriver_callback_lock);
905*4882a593Smuzhiyun 	dev->being_removed = false;
906*4882a593Smuzhiyun 	err = drv->probe(dev);
907*4882a593Smuzhiyun 	if (err) {
908*4882a593Smuzhiyun 		mutex_unlock(&dev->visordriver_callback_lock);
909*4882a593Smuzhiyun 		return err;
910*4882a593Smuzhiyun 	}
911*4882a593Smuzhiyun 	/* success: reference kept via unmatched get_device() */
912*4882a593Smuzhiyun 	get_device(&dev->device);
913*4882a593Smuzhiyun 	publish_vbus_dev_info(dev);
914*4882a593Smuzhiyun 	mutex_unlock(&dev->visordriver_callback_lock);
915*4882a593Smuzhiyun 	return 0;
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun /*
919*4882a593Smuzhiyun  * visorbus_register_visor_driver() - registers the provided visor driver for
920*4882a593Smuzhiyun  *				      handling one or more visor device
921*4882a593Smuzhiyun  *                                    types (channel_types)
922*4882a593Smuzhiyun  * @drv: the driver to register
923*4882a593Smuzhiyun  *
924*4882a593Smuzhiyun  * A visor function driver calls this function to register the driver. The
925*4882a593Smuzhiyun  * caller MUST fill in the following fields within the #drv structure:
926*4882a593Smuzhiyun  *     name, version, owner, channel_types, probe, remove
927*4882a593Smuzhiyun  *
928*4882a593Smuzhiyun  * Here's how the whole Linux bus / driver / device model works.
929*4882a593Smuzhiyun  *
930*4882a593Smuzhiyun  * At system start-up, the visorbus kernel module is loaded, which registers
931*4882a593Smuzhiyun  * visorbus_type as a bus type, using bus_register().
932*4882a593Smuzhiyun  *
933*4882a593Smuzhiyun  * All kernel modules that support particular device types on a
934*4882a593Smuzhiyun  * visorbus bus are loaded.  Each of these kernel modules calls
935*4882a593Smuzhiyun  * visorbus_register_visor_driver() in their init functions, passing a
936*4882a593Smuzhiyun  * visor_driver struct.  visorbus_register_visor_driver() in turn calls
937*4882a593Smuzhiyun  * register_driver(&visor_driver.driver).  This .driver member is
938*4882a593Smuzhiyun  * initialized with generic methods (like probe), whose sole responsibility
939*4882a593Smuzhiyun  * is to act as a broker for the real methods, which are within the
940*4882a593Smuzhiyun  * visor_driver struct.  (This is the way the subclass behavior is
941*4882a593Smuzhiyun  * implemented, since visor_driver is essentially a subclass of the
942*4882a593Smuzhiyun  * generic driver.)  Whenever a driver_register() happens, core bus code in
943*4882a593Smuzhiyun  * the kernel does (see device_attach() in drivers/base/dd.c):
944*4882a593Smuzhiyun  *
945*4882a593Smuzhiyun  *     for each dev associated with the bus (the bus that driver is on) that
946*4882a593Smuzhiyun  *     does not yet have a driver
947*4882a593Smuzhiyun  *         if bus.match(dev,newdriver) == yes_matched  ** .match specified
948*4882a593Smuzhiyun  *                                                ** during bus_register().
949*4882a593Smuzhiyun  *             newdriver.probe(dev)  ** for visor drivers, this will call
950*4882a593Smuzhiyun  *                   ** the generic driver.probe implemented in visorbus.c,
951*4882a593Smuzhiyun  *                   ** which in turn calls the probe specified within the
952*4882a593Smuzhiyun  *                   ** struct visor_driver (which was specified by the
953*4882a593Smuzhiyun  *                   ** actual device driver as part of
954*4882a593Smuzhiyun  *                   ** visorbus_register_visor_driver()).
955*4882a593Smuzhiyun  *
956*4882a593Smuzhiyun  * The above dance also happens when a new device appears.
957*4882a593Smuzhiyun  * So the question is, how are devices created within the system?
958*4882a593Smuzhiyun  * Basically, just call device_add(dev).  See pci_bus_add_devices().
959*4882a593Smuzhiyun  * pci_scan_device() shows an example of how to build a device struct.  It
960*4882a593Smuzhiyun  * returns the newly-created struct to pci_scan_single_device(), who adds it
961*4882a593Smuzhiyun  * to the list of devices at PCIBUS.devices.  That list of devices is what
962*4882a593Smuzhiyun  * is traversed by pci_bus_add_devices().
963*4882a593Smuzhiyun  *
964*4882a593Smuzhiyun  * Return: integer indicating success (zero) or failure (non-zero)
965*4882a593Smuzhiyun  */
visorbus_register_visor_driver(struct visor_driver * drv)966*4882a593Smuzhiyun int visorbus_register_visor_driver(struct visor_driver *drv)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	/* can't register on a nonexistent bus */
969*4882a593Smuzhiyun 	if (!initialized)
970*4882a593Smuzhiyun 		return -ENODEV;
971*4882a593Smuzhiyun 	if (!drv->probe)
972*4882a593Smuzhiyun 		return -EINVAL;
973*4882a593Smuzhiyun 	if (!drv->remove)
974*4882a593Smuzhiyun 		return -EINVAL;
975*4882a593Smuzhiyun 	if (!drv->pause)
976*4882a593Smuzhiyun 		return -EINVAL;
977*4882a593Smuzhiyun 	if (!drv->resume)
978*4882a593Smuzhiyun 		return -EINVAL;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	drv->driver.name = drv->name;
981*4882a593Smuzhiyun 	drv->driver.bus = &visorbus_type;
982*4882a593Smuzhiyun 	drv->driver.probe = visordriver_probe_device;
983*4882a593Smuzhiyun 	drv->driver.remove = visordriver_remove_device;
984*4882a593Smuzhiyun 	drv->driver.owner = drv->owner;
985*4882a593Smuzhiyun 	/*
986*4882a593Smuzhiyun 	 * driver_register does this:
987*4882a593Smuzhiyun 	 *   bus_add_driver(drv)
988*4882a593Smuzhiyun 	 *   ->if (drv.bus)  ** (bus_type) **
989*4882a593Smuzhiyun 	 *       driver_attach(drv)
990*4882a593Smuzhiyun 	 *         for each dev with bus type of drv.bus
991*4882a593Smuzhiyun 	 *           if (!dev.drv)  ** no driver assigned yet **
992*4882a593Smuzhiyun 	 *             if (bus.match(dev,drv))  [visorbus_match]
993*4882a593Smuzhiyun 	 *               dev.drv = drv
994*4882a593Smuzhiyun 	 *               if (!drv.probe(dev))   [visordriver_probe_device]
995*4882a593Smuzhiyun 	 *                 dev.drv = NULL
996*4882a593Smuzhiyun 	 */
997*4882a593Smuzhiyun 	return driver_register(&drv->driver);
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun /*
1002*4882a593Smuzhiyun  * visorbus_create_instance() - create a device instance for the visorbus itself
1003*4882a593Smuzhiyun  * @dev: struct visor_device indicating the bus instance
1004*4882a593Smuzhiyun  *
1005*4882a593Smuzhiyun  * Return: 0 for success, otherwise negative errno value indicating reason for
1006*4882a593Smuzhiyun  *         failure
1007*4882a593Smuzhiyun  */
visorbus_create_instance(struct visor_device * dev)1008*4882a593Smuzhiyun int visorbus_create_instance(struct visor_device *dev)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun 	int id = dev->chipset_bus_no;
1011*4882a593Smuzhiyun 	int err;
1012*4882a593Smuzhiyun 	struct visor_vbus_headerinfo *hdr_info;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1015*4882a593Smuzhiyun 	if (!hdr_info)
1016*4882a593Smuzhiyun 		return -ENOMEM;
1017*4882a593Smuzhiyun 	dev_set_name(&dev->device, "visorbus%d", id);
1018*4882a593Smuzhiyun 	dev->device.bus = &visorbus_type;
1019*4882a593Smuzhiyun 	dev->device.groups = visorbus_groups;
1020*4882a593Smuzhiyun 	dev->device.release = visorbus_release_busdevice;
1021*4882a593Smuzhiyun 	dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1022*4882a593Smuzhiyun 					      visorbus_debugfs_dir);
1023*4882a593Smuzhiyun 	dev->debugfs_bus_info = debugfs_create_file("client_bus_info", 0440,
1024*4882a593Smuzhiyun 						    dev->debugfs_dir, dev,
1025*4882a593Smuzhiyun 						    &bus_info_debugfs_fops);
1026*4882a593Smuzhiyun 	dev_set_drvdata(&dev->device, dev);
1027*4882a593Smuzhiyun 	err = get_vbus_header_info(dev->visorchannel, &dev->device, hdr_info);
1028*4882a593Smuzhiyun 	if (err < 0)
1029*4882a593Smuzhiyun 		goto err_debugfs_dir;
1030*4882a593Smuzhiyun 	err = device_register(&dev->device);
1031*4882a593Smuzhiyun 	if (err < 0)
1032*4882a593Smuzhiyun 		goto err_debugfs_dir;
1033*4882a593Smuzhiyun 	list_add_tail(&dev->list_all, &list_all_bus_instances);
1034*4882a593Smuzhiyun 	dev->state.created = 1;
1035*4882a593Smuzhiyun 	dev->vbus_hdr_info = (void *)hdr_info;
1036*4882a593Smuzhiyun 	write_vbus_chp_info(dev->visorchannel, hdr_info, &chipset_driverinfo);
1037*4882a593Smuzhiyun 	write_vbus_bus_info(dev->visorchannel, hdr_info, &clientbus_driverinfo);
1038*4882a593Smuzhiyun 	visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
1039*4882a593Smuzhiyun 	return 0;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun err_debugfs_dir:
1042*4882a593Smuzhiyun 	debugfs_remove_recursive(dev->debugfs_dir);
1043*4882a593Smuzhiyun 	kfree(hdr_info);
1044*4882a593Smuzhiyun 	dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1045*4882a593Smuzhiyun 	return err;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun /*
1049*4882a593Smuzhiyun  * visorbus_remove_instance() - remove a device instance for the visorbus itself
1050*4882a593Smuzhiyun  * @dev: struct visor_device indentifying the bus to remove
1051*4882a593Smuzhiyun  */
visorbus_remove_instance(struct visor_device * dev)1052*4882a593Smuzhiyun void visorbus_remove_instance(struct visor_device *dev)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun 	/*
1055*4882a593Smuzhiyun 	 * Note that this will result in the release method for
1056*4882a593Smuzhiyun 	 * dev->dev being called, which will call
1057*4882a593Smuzhiyun 	 * visorbus_release_busdevice().  This has something to do with
1058*4882a593Smuzhiyun 	 * the put_device() done in device_unregister(), but I have never
1059*4882a593Smuzhiyun 	 * successfully been able to trace thru the code to see where/how
1060*4882a593Smuzhiyun 	 * release() gets called.  But I know it does.
1061*4882a593Smuzhiyun 	 */
1062*4882a593Smuzhiyun 	kfree(dev->vbus_hdr_info);
1063*4882a593Smuzhiyun 	list_del(&dev->list_all);
1064*4882a593Smuzhiyun 	if (dev->pending_msg_hdr)
1065*4882a593Smuzhiyun 		visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
1066*4882a593Smuzhiyun 	device_unregister(&dev->device);
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun /*
1070*4882a593Smuzhiyun  * remove_all_visor_devices() - remove all child visorbus device instances
1071*4882a593Smuzhiyun  */
remove_all_visor_devices(void)1072*4882a593Smuzhiyun static void remove_all_visor_devices(void)
1073*4882a593Smuzhiyun {
1074*4882a593Smuzhiyun 	struct list_head *listentry, *listtmp;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1077*4882a593Smuzhiyun 		struct visor_device *dev;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		dev = list_entry(listentry, struct visor_device, list_all);
1080*4882a593Smuzhiyun 		remove_visor_device(dev);
1081*4882a593Smuzhiyun 	}
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun /*
1085*4882a593Smuzhiyun  * pause_state_change_complete() - the callback function to be called by a
1086*4882a593Smuzhiyun  *                                 visorbus function driver when a
1087*4882a593Smuzhiyun  *                                 pending "pause device" operation has
1088*4882a593Smuzhiyun  *                                 completed
1089*4882a593Smuzhiyun  * @dev: struct visor_device identifying the paused device
1090*4882a593Smuzhiyun  * @status: 0 iff the pause state change completed successfully, otherwise
1091*4882a593Smuzhiyun  *          a negative errno value indicating the reason for failure
1092*4882a593Smuzhiyun  */
pause_state_change_complete(struct visor_device * dev,int status)1093*4882a593Smuzhiyun static void pause_state_change_complete(struct visor_device *dev, int status)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	if (!dev->pausing)
1096*4882a593Smuzhiyun 		return;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	dev->pausing = false;
1099*4882a593Smuzhiyun 	visorbus_device_changestate_response(dev, status,
1100*4882a593Smuzhiyun 					     segment_state_standby);
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun /*
1104*4882a593Smuzhiyun  * resume_state_change_complete() - the callback function to be called by a
1105*4882a593Smuzhiyun  *                                  visorbus function driver when a
1106*4882a593Smuzhiyun  *                                  pending "resume device" operation has
1107*4882a593Smuzhiyun  *                                  completed
1108*4882a593Smuzhiyun  * @dev: struct visor_device identifying the resumed device
1109*4882a593Smuzhiyun  * @status: 0 iff the resume state change completed successfully, otherwise
1110*4882a593Smuzhiyun  *          a negative errno value indicating the reason for failure
1111*4882a593Smuzhiyun  */
resume_state_change_complete(struct visor_device * dev,int status)1112*4882a593Smuzhiyun static void resume_state_change_complete(struct visor_device *dev, int status)
1113*4882a593Smuzhiyun {
1114*4882a593Smuzhiyun 	if (!dev->resuming)
1115*4882a593Smuzhiyun 		return;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	dev->resuming = false;
1118*4882a593Smuzhiyun 	/*
1119*4882a593Smuzhiyun 	 * Notify the chipset driver that the resume is complete,
1120*4882a593Smuzhiyun 	 * which will presumably want to send some sort of response to
1121*4882a593Smuzhiyun 	 * the initiator.
1122*4882a593Smuzhiyun 	 */
1123*4882a593Smuzhiyun 	visorbus_device_changestate_response(dev, status,
1124*4882a593Smuzhiyun 					     segment_state_running);
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun /*
1128*4882a593Smuzhiyun  * visorchipset_initiate_device_pause_resume() - start a pause or resume
1129*4882a593Smuzhiyun  *                                               operation for a visor device
1130*4882a593Smuzhiyun  * @dev: struct visor_device identifying the device being paused or resumed
1131*4882a593Smuzhiyun  * @is_pause: true to indicate pause operation, false to indicate resume
1132*4882a593Smuzhiyun  *
1133*4882a593Smuzhiyun  * Tell the subordinate function driver for a specific device to pause
1134*4882a593Smuzhiyun  * or resume that device.  Success/failure result is returned asynchronously
1135*4882a593Smuzhiyun  * via a callback function; see pause_state_change_complete() and
1136*4882a593Smuzhiyun  * resume_state_change_complete().
1137*4882a593Smuzhiyun  */
visorchipset_initiate_device_pause_resume(struct visor_device * dev,bool is_pause)1138*4882a593Smuzhiyun static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1139*4882a593Smuzhiyun 						     bool is_pause)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	int err;
1142*4882a593Smuzhiyun 	struct visor_driver *drv;
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	/* If no driver associated with the device nothing to pause/resume */
1145*4882a593Smuzhiyun 	if (!dev->device.driver)
1146*4882a593Smuzhiyun 		return 0;
1147*4882a593Smuzhiyun 	if (dev->pausing || dev->resuming)
1148*4882a593Smuzhiyun 		return -EBUSY;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	drv = to_visor_driver(dev->device.driver);
1151*4882a593Smuzhiyun 	if (is_pause) {
1152*4882a593Smuzhiyun 		dev->pausing = true;
1153*4882a593Smuzhiyun 		err = drv->pause(dev, pause_state_change_complete);
1154*4882a593Smuzhiyun 	} else {
1155*4882a593Smuzhiyun 		/*
1156*4882a593Smuzhiyun 		 * The vbus_dev_info structure in the channel was been cleared,
1157*4882a593Smuzhiyun 		 * make sure it is valid.
1158*4882a593Smuzhiyun 		 */
1159*4882a593Smuzhiyun 		publish_vbus_dev_info(dev);
1160*4882a593Smuzhiyun 		dev->resuming = true;
1161*4882a593Smuzhiyun 		err = drv->resume(dev, resume_state_change_complete);
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun 	return err;
1164*4882a593Smuzhiyun }
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun /*
1167*4882a593Smuzhiyun  * visorchipset_device_pause() - start a pause operation for a visor device
1168*4882a593Smuzhiyun  * @dev_info: struct visor_device identifying the device being paused
1169*4882a593Smuzhiyun  *
1170*4882a593Smuzhiyun  * Tell the subordinate function driver for a specific device to pause
1171*4882a593Smuzhiyun  * that device.  Success/failure result is returned asynchronously
1172*4882a593Smuzhiyun  * via a callback function; see pause_state_change_complete().
1173*4882a593Smuzhiyun  */
visorchipset_device_pause(struct visor_device * dev_info)1174*4882a593Smuzhiyun int visorchipset_device_pause(struct visor_device *dev_info)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	int err;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	err = visorchipset_initiate_device_pause_resume(dev_info, true);
1179*4882a593Smuzhiyun 	if (err < 0) {
1180*4882a593Smuzhiyun 		dev_info->pausing = false;
1181*4882a593Smuzhiyun 		return err;
1182*4882a593Smuzhiyun 	}
1183*4882a593Smuzhiyun 	return 0;
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun /*
1187*4882a593Smuzhiyun  * visorchipset_device_resume() - start a resume operation for a visor device
1188*4882a593Smuzhiyun  * @dev_info: struct visor_device identifying the device being resumed
1189*4882a593Smuzhiyun  *
1190*4882a593Smuzhiyun  * Tell the subordinate function driver for a specific device to resume
1191*4882a593Smuzhiyun  * that device.  Success/failure result is returned asynchronously
1192*4882a593Smuzhiyun  * via a callback function; see resume_state_change_complete().
1193*4882a593Smuzhiyun  */
visorchipset_device_resume(struct visor_device * dev_info)1194*4882a593Smuzhiyun int visorchipset_device_resume(struct visor_device *dev_info)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun 	int err;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	err = visorchipset_initiate_device_pause_resume(dev_info, false);
1199*4882a593Smuzhiyun 	if (err < 0) {
1200*4882a593Smuzhiyun 		dev_info->resuming = false;
1201*4882a593Smuzhiyun 		return err;
1202*4882a593Smuzhiyun 	}
1203*4882a593Smuzhiyun 	return 0;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
visorbus_init(void)1206*4882a593Smuzhiyun int visorbus_init(void)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	int err;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1211*4882a593Smuzhiyun 	bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1212*4882a593Smuzhiyun 	err = bus_register(&visorbus_type);
1213*4882a593Smuzhiyun 	if (err < 0)
1214*4882a593Smuzhiyun 		return err;
1215*4882a593Smuzhiyun 	initialized = true;
1216*4882a593Smuzhiyun 	bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1217*4882a593Smuzhiyun 	return 0;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun 
visorbus_exit(void)1220*4882a593Smuzhiyun void visorbus_exit(void)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun 	struct list_head *listentry, *listtmp;
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	remove_all_visor_devices();
1225*4882a593Smuzhiyun 	list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1226*4882a593Smuzhiyun 		struct visor_device *dev;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 		dev = list_entry(listentry, struct visor_device, list_all);
1229*4882a593Smuzhiyun 		visorbus_remove_instance(dev);
1230*4882a593Smuzhiyun 	}
1231*4882a593Smuzhiyun 	bus_unregister(&visorbus_type);
1232*4882a593Smuzhiyun 	initialized = false;
1233*4882a593Smuzhiyun 	debugfs_remove_recursive(visorbus_debugfs_dir);
1234*4882a593Smuzhiyun }
1235