FreeBSD xen subsystem code
xenbusb_back.c
Go to the documentation of this file.
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
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
41#include <sys/param.h>
42#include <sys/bus.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/sbuf.h>
48#include <sys/sysctl.h>
49#include <sys/syslog.h>
50#include <sys/systm.h>
51#include <sys/sx.h>
52#include <sys/taskqueue.h>
53
54#include <machine/stdarg.h>
55
56#include <xen/xen-os.h>
57#include <xen/gnttab.h>
59#include <xen/xenbus/xenbusb.h>
60
61/*------------------ Private Device Attachment Functions --------------------*/
69static int
70xenbusb_back_probe(device_t dev)
71{
72 device_set_desc(dev, "Xen Backend Devices");
73
74 return (0);
75}
76
85static int
87{
88 struct xenbusb_softc *xbs;
89 int error;
90
91 xbs = device_get_softc(dev);
92 error = xenbusb_attach(dev, "backend", /*id_components*/2);
93
94 /*
95 * Backend devices operate to serve other domains,
96 * so there is no need to hold up boot processing
97 * while connections to foreign domains are made.
98 */
99 mtx_lock(&xbs->xbs_lock);
100 if ((xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) {
101 xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE;
102 mtx_unlock(&xbs->xbs_lock);
103 config_intrhook_disestablish(&xbs->xbs_attach_ch);
104 } else {
105 mtx_unlock(&xbs->xbs_lock);
106 }
107
108 return (error);
109}
110
131static int
132xenbusb_back_enumerate_type(device_t dev, const char *type)
133{
134 struct xenbusb_softc *xbs;
135 const char **vms;
136 u_int vm_idx;
137 u_int vm_count;
138 int error;
139
140 xbs = device_get_softc(dev);
141 error = xs_directory(XST_NIL, xbs->xbs_node, type, &vm_count, &vms);
142 if (error)
143 return (error);
144 for (vm_idx = 0; vm_idx < vm_count; vm_idx++) {
145 struct sbuf *vm_path;
146 const char *vm;
147 const char **devs;
148 u_int dev_idx;
149 u_int dev_count;
150
151 vm = vms[vm_idx];
152
153 vm_path = xs_join(type, vm);
154 error = xs_directory(XST_NIL, xbs->xbs_node, sbuf_data(vm_path),
155 &dev_count, &devs);
156 sbuf_delete(vm_path);
157 if (error)
158 break;
159
160 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
161 const char *dev_num;
162 struct sbuf *id;
163
164 dev_num = devs[dev_idx];
165 id = xs_join(vm, dev_num);
166 xenbusb_add_device(dev, type, sbuf_data(id));
167 sbuf_delete(id);
168 }
169 free(devs, M_XENSTORE);
170 }
171
172 free(vms, M_XENSTORE);
173
174 return (0);
175}
176
192static int
194{
195 char *otherend_path;
196 int error;
197
198 if (ivars->xd_otherend_path != NULL) {
199 free(ivars->xd_otherend_path, M_XENBUS);
200 ivars->xd_otherend_path = NULL;
201 }
202
203 error = xs_gather(XST_NIL, ivars->xd_node,
204 "frontend-id", "%i", &ivars->xd_otherend_id,
205 "frontend", NULL, &otherend_path,
206 NULL);
207
208 if (error == 0) {
209 ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
210 ivars->xd_otherend_path_len = strlen(otherend_path);
211 free(otherend_path, M_XENSTORE);
212 }
213 return (error);
214}
215
223static void
224xenbusb_back_otherend_changed(device_t bus, device_t child,
225 enum xenbus_state peer_state)
226{
227 /* Perform default processing of state. */
228 xenbusb_otherend_changed(bus, child, peer_state);
229
230 /*
231 * "Online" devices are never fully detached in the
232 * newbus sense. Only the front<->back connection is
233 * torn down. If the front returns to the initialising
234 * state after closing a previous connection, signal
235 * our willingness to reconnect and that all necessary
236 * XenStore data for feature negotiation is present.
237 */
238 if (peer_state == XenbusStateInitialising
239 && xenbus_dev_is_online(child) != 0
240 && xenbus_get_state(child) == XenbusStateClosed)
241 xenbus_set_state(child, XenbusStateInitWait);
242}
243
253static void
254xenbusb_back_localend_changed(device_t bus, device_t child, const char *path)
255{
256
257 xenbusb_localend_changed(bus, child, path);
258
259 if (strcmp(path, "/state") != 0
260 && strcmp(path, "/online") != 0)
261 return;
262
263 if (xenbus_get_state(child) != XenbusStateClosed
264 || xenbus_dev_is_online(child) != 0)
265 return;
266
267 /*
268 * Cleanup the hotplug entry in the XenStore if
269 * present. The control domain expects any userland
270 * component associated with this device to destroy
271 * this node in order to signify it is safe to
272 * teardown the device. However, not all backends
273 * rely on userland components, and those that
274 * do should either use a communication channel
275 * other than the XenStore, or ensure the hotplug
276 * data is already cleaned up.
277 *
278 * This removal ensures that no matter what path
279 * is taken to mark a back-end closed, the control
280 * domain will understand that it is closed.
281 */
282 xs_rm(XST_NIL, xenbus_get_node(child), "hotplug-status");
283}
284
285/*-------------------- Private Device Attachment Data -----------------------*/
286static device_method_t xenbusb_back_methods[] = {
287 /* Device interface */
288 DEVMETHOD(device_identify, xenbusb_identify),
289 DEVMETHOD(device_probe, xenbusb_back_probe),
290 DEVMETHOD(device_attach, xenbusb_back_attach),
291 DEVMETHOD(device_detach, bus_generic_detach),
292 DEVMETHOD(device_shutdown, bus_generic_shutdown),
293 DEVMETHOD(device_suspend, bus_generic_suspend),
294 DEVMETHOD(device_resume, xenbusb_resume),
295
296 /* Bus Interface */
297 DEVMETHOD(bus_print_child, xenbusb_print_child),
298 DEVMETHOD(bus_read_ivar, xenbusb_read_ivar),
299 DEVMETHOD(bus_write_ivar, xenbusb_write_ivar),
300 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
301 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
302 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
303 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
304
305 /* XenBus Bus Interface */
306 DEVMETHOD(xenbusb_enumerate_type, xenbusb_back_enumerate_type),
307 DEVMETHOD(xenbusb_get_otherend_node, xenbusb_back_get_otherend_node),
310 { 0, 0 }
311};
312
313DEFINE_CLASS_0(xenbusb_back, xenbusb_back_driver, xenbusb_back_methods,
314 sizeof(struct xenbusb_softc));
315devclass_t xenbusb_back_devclass;
316
317DRIVER_MODULE(xenbusb_back, xenstore, xenbusb_back_driver,
318 xenbusb_back_devclass, 0, 0);
char * xd_otherend_path
Definition: xenbusb.h:174
int xd_otherend_path_len
Definition: xenbusb.h:177
Container for all state needed to manage a Xenbus Bus attachment.
Definition: xenbusb.h:62
struct intr_config_hook xbs_attach_ch
Definition: xenbusb.h:87
const char * xbs_node
Definition: xenbusb.h:103
xenbusb_softc_flag xbs_flags
Definition: xenbusb.h:74
struct mtx xbs_lock
Definition: xenbusb.h:71
int xenbus_dev_is_online(device_t dev)
Definition: xenbus.c:198
int xenbusb_attach(device_t dev, char *bus_node, u_int id_components)
Perform common XenBus bus attach processing.
Definition: xenbusb.c:735
int xenbusb_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
Common XenBus child instance variable read access method.
Definition: xenbusb.c:870
int xenbusb_print_child(device_t dev, device_t child)
Pretty-prints information about a child of a XenBus bus.
Definition: xenbusb.c:857
void xenbusb_localend_changed(device_t bus, device_t child, const char *path)
Common XenBus method implementing responses to local XenStore changes.
Definition: xenbusb.c:976
int xenbusb_add_device(device_t dev, const char *type, const char *id)
Attempt to add a XenBus device instance to this XenBus bus.
Definition: xenbusb.c:637
void xenbusb_identify(driver_t *driver __unused, device_t parent)
Identify instances of this device type in the system.
Definition: xenbusb.c:627
void xenbusb_otherend_changed(device_t bus, device_t child, enum xenbus_state state)
Common XenBus method implementing responses to peer state changes.
Definition: xenbusb.c:970
int xenbusb_resume(device_t dev)
Perform common XenBus bus resume handling.
Definition: xenbusb.c:791
int xenbusb_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
Common XenBus child instance variable write access method.
Definition: xenbusb.c:900
static int xenbusb_back_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
Determine and store the XenStore path for the other end of a split device whose local end is represen...
Definition: xenbusb_back.c:193
static int xenbusb_back_attach(device_t dev)
Attach the XenBus back bus.
Definition: xenbusb_back.c:86
static int xenbusb_back_enumerate_type(device_t dev, const char *type)
Enumerate all devices of the given type on this bus.
Definition: xenbusb_back.c:132
static void xenbusb_back_localend_changed(device_t bus, device_t child, const char *path)
Backend XenBus method implementing responses to local XenStore changes.
Definition: xenbusb_back.c:254
static int xenbusb_back_probe(device_t dev)
Probe for the existence of the XenBus back bus.
Definition: xenbusb_back.c:70
static void xenbusb_back_otherend_changed(device_t bus, device_t child, enum xenbus_state peer_state)
Backend XenBus method implementing responses to peer state changes.
Definition: xenbusb_back.c:224
Datastructures and function declarations for usedby device drivers operating on the XenBus.