xref: /freebsd/sys/xen/xenbus/xenbusb_front.c (revision e453e498cbb88570a3ff7b3679de65c88707da95)
1 /******************************************************************************
2  * Talks to Xen Store to figure out what devices we have.
3  *
4  * Copyright (C) 2009, 2010 Spectra Logic Corporation
5  * Copyright (C) 2008 Doug Rabson
6  * Copyright (C) 2005 Rusty Russell, IBM Corporation
7  * Copyright (C) 2005 Mike Wray, Hewlett-Packard
8  * Copyright (C) 2005 XenSource Ltd
9  *
10  * This file may be distributed separately from the Linux kernel, or
11  * incorporated into other software packages, subject to the following license:
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this source file (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use, copy, modify,
16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  */
31 
32 /**
33  * \file xenbusb_front.c
34  *
35  * XenBus management of the NewBus bus containing the frontend instances of
36  * Xen split devices.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/sbuf.h>
46 #include <sys/stdarg.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/systm.h>
50 #include <sys/sx.h>
51 #include <sys/taskqueue.h>
52 
53 #include <xen/xen-os.h>
54 #include <xen/gnttab.h>
55 #include <xen/xenbus/xenbusvar.h>
56 #include <xen/xenbus/xenbusb.h>
57 
58 /*------------------ Private Device Attachment Functions  --------------------*/
59 /**
60  * \brief Probe for the existence of the XenBus front bus.
61  *
62  * \param dev  NewBus device_t for this XenBus front bus instance.
63  *
64  * \return  Always returns 0 indicating success.
65  */
66 static int
xenbusb_front_probe(device_t dev)67 xenbusb_front_probe(device_t dev)
68 {
69 	device_set_desc(dev, "Xen Frontend Devices");
70 
71 	return (0);
72 }
73 
74 /**
75  * \brief Attach the XenBus front bus.
76  *
77  * \param dev  NewBus device_t for this XenBus front bus instance.
78  *
79  * \return  On success, 0. Otherwise an errno value indicating the
80  *          type of failure.
81  */
82 static int
xenbusb_front_attach(device_t dev)83 xenbusb_front_attach(device_t dev)
84 {
85 	return (xenbusb_attach(dev, "device", /*id_components*/1));
86 }
87 
88 /**
89  * \brief Enumerate all devices of the given type on this bus.
90  *
91  * \param dev   NewBus device_t for this XenBus front bus instance.
92  * \param type  String indicating the device sub-tree (e.g. "vfb", "vif")
93  *              to enumerate.
94  *
95  * \return  On success, 0. Otherwise an errno value indicating the
96  *          type of failure.
97  *
98  * Devices that are found are entered into the NewBus hierarchy via
99  * xenbusb_add_device().  xenbusb_add_device() ignores duplicate detects
100  * and ignores duplicate devices, so it can be called unconditionally
101  * for any device found in the XenStore.
102  */
103 static int
xenbusb_front_enumerate_type(device_t dev,const char * type)104 xenbusb_front_enumerate_type(device_t dev, const char *type)
105 {
106 	struct xenbusb_softc *xbs;
107 	const char **dir;
108 	unsigned int i, count;
109 	int error;
110 
111 	xbs = device_get_softc(dev);
112 	error = xs_directory(XST_NIL, xbs->xbs_node, type, &count, &dir);
113 	if (error)
114 		return (error);
115 	for (i = 0; i < count; i++)
116 		xenbusb_add_device(dev, type, dir[i]);
117 
118 	free(dir, M_XENSTORE);
119 
120 	return (0);
121 }
122 
123 /**
124  * \brief Determine and store the XenStore path for the other end of
125  *        a split device whose local end is represented by ivars.
126  *
127  * If successful, the xd_otherend_path field of the child's instance
128  * variables will be updated.
129  *
130  * \param dev    NewBus device_t for this XenBus front bus instance.
131  * \param ivars  Instance variables from the XenBus child device for
132  *               which to perform this function.
133  *
134  * \return  On success, 0. Otherwise an errno value indicating the
135  *          type of failure.
136  */
137 static int
xenbusb_front_get_otherend_node(device_t dev,struct xenbus_device_ivars * ivars)138 xenbusb_front_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
139 {
140 	char *otherend_path;
141 	int error;
142 
143 	if (ivars->xd_otherend_path != NULL) {
144 		free(ivars->xd_otherend_path, M_XENBUS);
145 		ivars->xd_otherend_path = NULL;
146 	}
147 
148 	error = xs_gather(XST_NIL, ivars->xd_node,
149 	    "backend-id", "%i", &ivars->xd_otherend_id,
150 	    "backend", NULL, &otherend_path,
151 	    NULL);
152 
153 	if (error == 0) {
154 		ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
155 		ivars->xd_otherend_path_len = strlen(otherend_path);
156 		free(otherend_path, M_XENSTORE);
157 	}
158 	return (error);
159 }
160 
161 /*-------------------- Private Device Attachment Data  -----------------------*/
162 static device_method_t xenbusb_front_methods[] = {
163 	/* Device interface */
164 	DEVMETHOD(device_identify,	xenbusb_identify),
165 	DEVMETHOD(device_probe,         xenbusb_front_probe),
166 	DEVMETHOD(device_attach,        xenbusb_front_attach),
167 	DEVMETHOD(device_detach,        bus_generic_detach),
168 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
169 	DEVMETHOD(device_suspend,       bus_generic_suspend),
170 	DEVMETHOD(device_resume,        xenbusb_resume),
171 
172 	/* Bus Interface */
173 	DEVMETHOD(bus_print_child,      xenbusb_print_child),
174 	DEVMETHOD(bus_read_ivar,        xenbusb_read_ivar),
175 	DEVMETHOD(bus_write_ivar,       xenbusb_write_ivar),
176 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
177 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
178 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
179 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
180 
181 	/* XenBus Bus Interface */
182 	DEVMETHOD(xenbusb_enumerate_type, xenbusb_front_enumerate_type),
183 	DEVMETHOD(xenbusb_get_otherend_node, xenbusb_front_get_otherend_node),
184 
185 	DEVMETHOD_END
186 };
187 
188 DEFINE_CLASS_0(xenbusb_front, xenbusb_front_driver, xenbusb_front_methods,
189 	       sizeof(struct xenbusb_softc));
190 
191 DRIVER_MODULE(xenbusb_front, xenstore, xenbusb_front_driver, 0, 0);
192