xref: /freebsd/sys/xen/xenbus/xenbusb_back.c (revision dcc4d2939f789a6d1f272ffeab2068ba2b7525ea)
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_back.c
34  *
35  * XenBus management of the NewBus bus containing the backend instances of
36  * Xen split devices.
37  */
38 #include <sys/cdefs.h>
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/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/systm.h>
49 #include <sys/sx.h>
50 #include <sys/taskqueue.h>
51 
52 #include <machine/stdarg.h>
53 
54 #include <xen/xen-os.h>
55 #include <xen/gnttab.h>
56 #include <xen/xenbus/xenbusvar.h>
57 #include <xen/xenbus/xenbusb.h>
58 
59 /*------------------ Private Device Attachment Functions  --------------------*/
60 /**
61  * \brief Probe for the existence of the XenBus back bus.
62  *
63  * \param dev  NewBus device_t for this XenBus back bus instance.
64  *
65  * \return  Always returns 0 indicating success.
66  */
67 static int
68 xenbusb_back_probe(device_t dev)
69 {
70 	device_set_desc(dev, "Xen Backend Devices");
71 
72 	return (0);
73 }
74 
75 /**
76  * \brief Attach the XenBus back bus.
77  *
78  * \param dev  NewBus device_t for this XenBus back bus instance.
79  *
80  * \return  On success, 0. Otherwise an errno value indicating the
81  *          type of failure.
82  */
83 static int
84 xenbusb_back_attach(device_t dev)
85 {
86 	struct xenbusb_softc *xbs;
87 	int error;
88 
89 	xbs = device_get_softc(dev);
90 	error = xenbusb_attach(dev, "backend", /*id_components*/2);
91 
92 	/*
93 	 * Backend devices operate to serve other domains,
94 	 * so there is no need to hold up boot processing
95 	 * while connections to foreign domains are made.
96 	 */
97 	mtx_lock(&xbs->xbs_lock);
98 	if ((xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) {
99 		xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE;
100 		mtx_unlock(&xbs->xbs_lock);
101 		config_intrhook_disestablish(&xbs->xbs_attach_ch);
102 	} else {
103 		mtx_unlock(&xbs->xbs_lock);
104 	}
105 
106 	return (error);
107 }
108 
109 /**
110  * \brief Enumerate all devices of the given type on this bus.
111  *
112  * \param dev   NewBus device_t for this XenBus backend bus instance.
113  * \param type  String indicating the device sub-tree (e.g. "vfb", "vif")
114  *              to enumerate.
115  *
116  * \return  On success, 0. Otherwise an errno value indicating the
117  *          type of failure.
118  *
119  * Devices that are found are entered into the NewBus hierarchy via
120  * xenbusb_add_device().  xenbusb_add_device() ignores duplicate detects
121  * and ignores duplicate devices, so it can be called unconditionally
122  * for any device found in the XenStore.
123  *
124  * The backend XenStore hierarchy has the following format:
125  *
126  *     backend/<device type>/<frontend vm id>/<device id>
127  *
128  */
129 static int
130 xenbusb_back_enumerate_type(device_t dev, const char *type)
131 {
132 	struct xenbusb_softc *xbs;
133 	const char **vms;
134 	u_int vm_idx;
135 	u_int vm_count;
136 	int error;
137 
138 	xbs = device_get_softc(dev);
139 	error = xs_directory(XST_NIL, xbs->xbs_node, type, &vm_count, &vms);
140 	if (error)
141 		return (error);
142 	for (vm_idx = 0; vm_idx < vm_count; vm_idx++) {
143 		struct sbuf *vm_path;
144 		const char *vm;
145 		const char **devs;
146 		u_int dev_idx;
147 		u_int dev_count;
148 
149 		vm = vms[vm_idx];
150 
151 		vm_path = xs_join(type, vm);
152 		error = xs_directory(XST_NIL, xbs->xbs_node, sbuf_data(vm_path),
153 		    &dev_count, &devs);
154 		sbuf_delete(vm_path);
155 		if (error)
156 			break;
157 
158 		for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
159 			const char *dev_num;
160 			struct sbuf *id;
161 
162 			dev_num = devs[dev_idx];
163 			id = xs_join(vm, dev_num);
164 			xenbusb_add_device(dev, type, sbuf_data(id));
165 			sbuf_delete(id);
166 		}
167 		free(devs, M_XENSTORE);
168 	}
169 
170 	free(vms, M_XENSTORE);
171 
172 	return (0);
173 }
174 
175 /**
176  * \brief Determine and store the XenStore path for the other end of
177  *        a split device whose local end is represented by ivars.
178  *
179  * \param dev    NewBus device_t for this XenBus backend bus instance.
180  * \param ivars  Instance variables from the XenBus child device for
181  *               which to perform this function.
182  *
183  * \return  On success, 0. Otherwise an errno value indicating the
184  *          type of failure.
185  *
186  * If successful, the xd_otherend_path field of the child's instance
187  * variables will be updated.
188  *
189  */
190 static int
191 xenbusb_back_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
192 {
193 	char *otherend_path;
194 	int error;
195 
196 	if (ivars->xd_otherend_path != NULL) {
197 		free(ivars->xd_otherend_path, M_XENBUS);
198 		ivars->xd_otherend_path = NULL;
199 	}
200 
201 	error = xs_gather(XST_NIL, ivars->xd_node,
202 	    "frontend-id", "%i", &ivars->xd_otherend_id,
203 	    "frontend", NULL, &otherend_path,
204 	    NULL);
205 
206 	if (error == 0) {
207 		ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
208 		ivars->xd_otherend_path_len = strlen(otherend_path);
209 		free(otherend_path, M_XENSTORE);
210 	}
211 	return (error);
212 }
213 
214 /**
215  * \brief Backend XenBus method implementing responses to peer state changes.
216  *
217  * \param bus       The XenBus bus parent of child.
218  * \param child     The XenBus child whose peer stat has changed.
219  * \param state     The current state of the peer.
220  */
221 static void
222 xenbusb_back_otherend_changed(device_t bus, device_t child,
223 			      enum xenbus_state peer_state)
224 {
225 	/* Perform default processing of state. */
226 	xenbusb_otherend_changed(bus, child, peer_state);
227 
228 	/*
229 	 * "Online" devices are never fully detached in the
230 	 * newbus sense.  Only the front<->back connection is
231 	 * torn down.  If the front returns to the initialising
232 	 * state after closing a previous connection, signal
233 	 * our willingness to reconnect and that all necessary
234 	 * XenStore data for feature negotiation is present.
235 	 */
236 	if (peer_state == XenbusStateInitialising
237 	 && xenbus_dev_is_online(child) != 0
238 	 && xenbus_get_state(child) == XenbusStateClosed)
239 		xenbus_set_state(child, XenbusStateInitWait);
240 }
241 
242 /**
243  * \brief Backend XenBus method implementing responses to local
244  *        XenStore changes.
245  *
246  * \param bus    The XenBus bus parent of child.
247  * \param child  The XenBus child whose peer stat has changed.
248  * \param_path   The tree relative sub-path to the modified node.  The empty
249  *               string indicates the root of the tree was destroyed.
250  */
251 static void
252 xenbusb_back_localend_changed(device_t bus, device_t child, const char *path)
253 {
254 
255 	xenbusb_localend_changed(bus, child, path);
256 
257 	if (strcmp(path, "/state") != 0
258 	 && strcmp(path, "/online") != 0)
259 		return;
260 
261 	if (xenbus_get_state(child) != XenbusStateClosed
262 	 || xenbus_dev_is_online(child) != 0)
263 		return;
264 
265 	/*
266 	 * Cleanup the hotplug entry in the XenStore if
267 	 * present.  The control domain expects any userland
268 	 * component associated with this device to destroy
269 	 * this node in order to signify it is safe to
270 	 * teardown the device.  However, not all backends
271 	 * rely on userland components, and those that
272 	 * do should either use a communication channel
273 	 * other than the XenStore, or ensure the hotplug
274 	 * data is already cleaned up.
275 	 *
276 	 * This removal ensures that no matter what path
277 	 * is taken to mark a back-end closed, the control
278 	 * domain will understand that it is closed.
279 	 */
280 	xs_rm(XST_NIL, xenbus_get_node(child), "hotplug-status");
281 }
282 
283 /*-------------------- Private Device Attachment Data  -----------------------*/
284 static device_method_t xenbusb_back_methods[] = {
285 	/* Device interface */
286 	DEVMETHOD(device_identify,	xenbusb_identify),
287 	DEVMETHOD(device_probe,         xenbusb_back_probe),
288 	DEVMETHOD(device_attach,        xenbusb_back_attach),
289 	DEVMETHOD(device_detach,        bus_generic_detach),
290 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
291 	DEVMETHOD(device_suspend,       bus_generic_suspend),
292 	DEVMETHOD(device_resume,        xenbusb_resume),
293 
294 	/* Bus Interface */
295 	DEVMETHOD(bus_print_child,      xenbusb_print_child),
296 	DEVMETHOD(bus_read_ivar,        xenbusb_read_ivar),
297 	DEVMETHOD(bus_write_ivar,       xenbusb_write_ivar),
298 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
299 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
300 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
301 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
302 
303 	/* XenBus Bus Interface */
304 	DEVMETHOD(xenbusb_enumerate_type, xenbusb_back_enumerate_type),
305 	DEVMETHOD(xenbusb_get_otherend_node, xenbusb_back_get_otherend_node),
306 	DEVMETHOD(xenbusb_otherend_changed, xenbusb_back_otherend_changed),
307 	DEVMETHOD(xenbusb_localend_changed, xenbusb_back_localend_changed),
308 	{ 0, 0 }
309 };
310 
311 DEFINE_CLASS_0(xenbusb_back, xenbusb_back_driver, xenbusb_back_methods,
312 	       sizeof(struct xenbusb_softc));
313 
314 DRIVER_MODULE(xenbusb_back, xenstore, xenbusb_back_driver, 0, 0);
315