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 __FBSDID("$FreeBSD: releng/8.2/sys/xen/xenbus/xenbusb_back.c 215788 2010-11-24 01:03:03Z gibbs $");
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/xen/xen-os.h>
55 #include <machine/stdarg.h>
56
57 #include <xen/gnttab.h>
58 #include <xen/xenbus/xenbusvar.h>
59 #include <xen/xenbus/xenbusb.h>
60
61
62 /*------------------ Private Device Attachment Functions --------------------*/
63 /**
64 * \brief Probe for the existance of the XenBus back bus.
65 *
66 * \param dev NewBus device_t for this XenBus back bus instance.
67 *
68 * \return Always returns 0 indicating success.
69 */
70 static int
71 xenbusb_back_probe(device_t dev)
72 {
73 device_set_desc(dev, "Xen Backend Devices");
74
75 return (0);
76 }
77
78 /**
79 * \brief Attach the XenBus back bus.
80 *
81 * \param dev NewBus device_t for this XenBus back bus instance.
82 *
83 * \return On success, 0. Otherwise an errno value indicating the
84 * type of failure.
85 */
86 static int
87 xenbusb_back_attach(device_t dev)
88 {
89 struct xenbusb_softc *xbs;
90 int error;
91
92 xbs = device_get_softc(dev);
93 error = xenbusb_attach(dev, "backend", /*id_components*/2);
94
95 /*
96 * Backend devices operate to serve other domains,
97 * so there is no need to hold up boot processing
98 * while connections to foreign domains are made.
99 */
100 mtx_lock(&xbs->xbs_lock);
101 if ((xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) {
102 xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE;
103 mtx_unlock(&xbs->xbs_lock);
104 config_intrhook_disestablish(&xbs->xbs_attach_ch);
105 } else {
106 mtx_unlock(&xbs->xbs_lock);
107 }
108
109 return (error);
110 }
111
112 /**
113 * \brief Enumerate all devices of the given type on this bus.
114 *
115 * \param dev NewBus device_t for this XenBus backend bus instance.
116 * \param type String indicating the device sub-tree (e.g. "vfb", "vif")
117 * to enumerate.
118 *
119 * \return On success, 0. Otherwise an errno value indicating the
120 * type of failure.
121 *
122 * Devices that are found are entered into the NewBus hierarchy via
123 * xenbusb_add_device(). xenbusb_add_device() ignores duplicate detects
124 * and ignores duplicate devices, so it can be called unconditionally
125 * for any device found in the XenStore.
126 *
127 * The backend XenStore hierarchy has the following format:
128 *
129 * backend/<device type>/<frontend vm id>/<device id>
130 *
131 */
132 static int
133 xenbusb_back_enumerate_type(device_t dev, const char *type)
134 {
135 struct xenbusb_softc *xbs;
136 const char **vms;
137 u_int vm_idx;
138 u_int vm_count;
139 int error;
140
141 xbs = device_get_softc(dev);
142 error = xs_directory(XST_NIL, xbs->xbs_node, type, &vm_count, &vms);
143 if (error)
144 return (error);
145 for (vm_idx = 0; vm_idx < vm_count; vm_idx++) {
146 struct sbuf *vm_path;
147 const char *vm;
148 const char **devs;
149 u_int dev_idx;
150 u_int dev_count;
151
152 vm = vms[vm_idx];
153
154 vm_path = xs_join(type, vm);
155 error = xs_directory(XST_NIL, xbs->xbs_node, sbuf_data(vm_path),
156 &dev_count, &devs);
157 sbuf_delete(vm_path);
158 if (error)
159 break;
160
161 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
162 const char *dev_num;
163 struct sbuf *id;
164
165 dev_num = devs[dev_idx];
166 id = xs_join(vm, dev_num);
167 xenbusb_add_device(dev, type, sbuf_data(id));
168 sbuf_delete(id);
169 }
170 free(devs, M_XENSTORE);
171 }
172
173 free(vms, M_XENSTORE);
174
175 return (0);
176 }
177
178 /**
179 * \brief Determine and store the XenStore path for the other end of
180 * a split device whose local end is represented by ivars.
181 *
182 * \param dev NewBus device_t for this XenBus backend bus instance.
183 * \param ivars Instance variables from the XenBus child device for
184 * which to perform this function.
185 *
186 * \return On success, 0. Otherwise an errno value indicating the
187 * type of failure.
188 *
189 * If successful, the xd_otherend_path field of the child's instance
190 * variables will be updated.
191 *
192 */
193 static int
194 xenbusb_back_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
195 {
196 char *otherend_path;
197 int error;
198
199 if (ivars->xd_otherend_path != NULL) {
200 free(ivars->xd_otherend_path, M_XENBUS);
201 ivars->xd_otherend_path = NULL;
202 }
203
204 error = xs_gather(XST_NIL, ivars->xd_node,
205 "frontend-id", "%i", &ivars->xd_otherend_id,
206 "frontend", NULL, &otherend_path,
207 NULL);
208
209 if (error == 0) {
210 ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
211 free(otherend_path, M_XENSTORE);
212 }
213 return (error);
214 }
215
216 /**
217 * \brief Backend XenBus child instance variable write access method.
218 *
219 * \param dev The NewBus device representing this XenBus bus.
220 * \param child The NewBus device representing a child of dev%'s XenBus bus.
221 * \param index The index of the instance variable to access.
222 * \param value The new value to set in the instance variable accessed.
223 *
224 * \return On success, 0. Otherwise an errno value indicating the
225 * type of failure.
226 *
227 * Xenbus_back overrides this method so that it can trap state transitions
228 * of local backend devices and clean up their XenStore entries as necessary
229 * during device instance teardown.
230 */
231 static int
232 xenbusb_back_write_ivar(device_t dev, device_t child, int index,
233 uintptr_t value)
234 {
235 int error;
236
237 error = xenbusb_write_ivar(dev, child, index, value);
238
239 if (index == XENBUS_IVAR_STATE
240 && (enum xenbus_state)value == XenbusStateClosed
241 && xenbus_dev_is_online(child) == 0) {
242
243 /*
244 * Cleanup the hotplug entry in the XenStore if
245 * present. The control domain expects any userland
246 * component associated with this device to destroy
247 * this node in order to signify it is safe to
248 * teardown the device. However, not all backends
249 * rely on userland components, and those that
250 * do should either use a communication channel
251 * other than the XenStore, or ensure the hotplug
252 * data is already cleaned up.
253 *
254 * This removal ensures that no matter what path
255 * is taken to mark a back-end closed, the control
256 * domain will understand that it is closed.
257 */
258 xs_rm(XST_NIL, xenbus_get_node(child), "hotplug-status");
259 }
260
261 return (error);
262 }
263
264 /*-------------------- Private Device Attachment Data -----------------------*/
265 static device_method_t xenbusb_back_methods[] = {
266 /* Device interface */
267 DEVMETHOD(device_identify, xenbusb_identify),
268 DEVMETHOD(device_probe, xenbusb_back_probe),
269 DEVMETHOD(device_attach, xenbusb_back_attach),
270 DEVMETHOD(device_detach, bus_generic_detach),
271 DEVMETHOD(device_shutdown, bus_generic_shutdown),
272 DEVMETHOD(device_suspend, bus_generic_suspend),
273 DEVMETHOD(device_resume, bus_generic_resume),
274
275 /* Bus Interface */
276 DEVMETHOD(bus_print_child, xenbusb_print_child),
277 DEVMETHOD(bus_read_ivar, xenbusb_read_ivar),
278 DEVMETHOD(bus_write_ivar, xenbusb_back_write_ivar),
279 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
280 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
281 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
282 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
283
284 /* XenBus Bus Interface */
285 DEVMETHOD(xenbusb_enumerate_type, xenbusb_back_enumerate_type),
286 DEVMETHOD(xenbusb_get_otherend_node, xenbusb_back_get_otherend_node),
287 { 0, 0 }
288 };
289
290 DEFINE_CLASS_0(xenbusb_back, xenbusb_back_driver, xenbusb_back_methods,
291 sizeof(struct xenbusb_softc));
292 devclass_t xenbusb_back_devclass;
293
294 DRIVER_MODULE(xenbusb_back, xenstore, xenbusb_back_driver,
295 xenbusb_back_devclass, 0, 0);
Cache object: d97f61a630d9b96939d2c96430a44971
|