xenbus_probe.c revision 1.26.2.5 1 1.26.2.5 riz /* $NetBSD: xenbus_probe.c,v 1.26.2.5 2012/06/12 20:48:58 riz Exp $ */
2 1.1 bouyer /******************************************************************************
3 1.1 bouyer * Talks to Xen Store to figure out what devices we have.
4 1.1 bouyer *
5 1.1 bouyer * Copyright (C) 2005 Rusty Russell, IBM Corporation
6 1.1 bouyer * Copyright (C) 2005 Mike Wray, Hewlett-Packard
7 1.1 bouyer * Copyright (C) 2005 XenSource Ltd
8 1.1 bouyer *
9 1.1 bouyer * This file may be distributed separately from the Linux kernel, or
10 1.1 bouyer * incorporated into other software packages, subject to the following license:
11 1.1 bouyer *
12 1.1 bouyer * Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 bouyer * of this source file (the "Software"), to deal in the Software without
14 1.1 bouyer * restriction, including without limitation the rights to use, copy, modify,
15 1.1 bouyer * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 1.1 bouyer * and to permit persons to whom the Software is furnished to do so, subject to
17 1.1 bouyer * the following conditions:
18 1.1 bouyer *
19 1.1 bouyer * The above copyright notice and this permission notice shall be included in
20 1.1 bouyer * all copies or substantial portions of the Software.
21 1.1 bouyer *
22 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 bouyer * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 bouyer * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 bouyer * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 1.1 bouyer * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 1.1 bouyer * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 1.1 bouyer * IN THE SOFTWARE.
29 1.1 bouyer */
30 1.1 bouyer
31 1.2 bouyer #include <sys/cdefs.h>
32 1.26.2.5 riz __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.26.2.5 2012/06/12 20:48:58 riz Exp $");
33 1.2 bouyer
34 1.1 bouyer #if 0
35 1.1 bouyer #define DPRINTK(fmt, args...) \
36 1.18 perry printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
37 1.1 bouyer #else
38 1.1 bouyer #define DPRINTK(fmt, args...) ((void)0)
39 1.1 bouyer #endif
40 1.1 bouyer
41 1.2 bouyer #include <sys/types.h>
42 1.2 bouyer #include <sys/null.h>
43 1.2 bouyer #include <sys/errno.h>
44 1.2 bouyer #include <sys/malloc.h>
45 1.2 bouyer #include <sys/systm.h>
46 1.2 bouyer #include <sys/param.h>
47 1.2 bouyer #include <sys/kthread.h>
48 1.7 bouyer #include <uvm/uvm.h>
49 1.7 bouyer
50 1.2 bouyer #include <machine/stdarg.h>
51 1.2 bouyer
52 1.26 cegger #include <xen/xen.h> /* for xendomain_is_dom0() */
53 1.17 bouyer #include <xen/hypervisor.h>
54 1.17 bouyer #include <xen/xenbus.h>
55 1.17 bouyer #include <xen/evtchn.h>
56 1.17 bouyer #include <xen/shutdown_xenbus.h>
57 1.1 bouyer
58 1.1 bouyer #include "xenbus_comms.h"
59 1.1 bouyer
60 1.1 bouyer extern struct semaphore xenwatch_mutex;
61 1.1 bouyer
62 1.1 bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
63 1.1 bouyer
64 1.20 cegger static int xenbus_match(device_t, cfdata_t, void *);
65 1.20 cegger static void xenbus_attach(device_t, device_t, void *);
66 1.2 bouyer static int xenbus_print(void *, const char *);
67 1.2 bouyer
68 1.2 bouyer static void xenbus_probe_init(void *);
69 1.2 bouyer
70 1.5 bouyer static struct xenbus_device *xenbus_lookup_device_path(const char *);
71 1.5 bouyer
72 1.20 cegger CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
73 1.2 bouyer NULL, NULL);
74 1.2 bouyer
75 1.24 cegger device_t xenbus_dev;
76 1.5 bouyer
77 1.5 bouyer SLIST_HEAD(, xenbus_device) xenbus_device_list;
78 1.11 bouyer SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
79 1.11 bouyer SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
80 1.2 bouyer
81 1.2 bouyer int
82 1.20 cegger xenbus_match(device_t parent, cfdata_t match, void *aux)
83 1.2 bouyer {
84 1.2 bouyer struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
85 1.2 bouyer
86 1.2 bouyer if (strcmp(xa->xa_device, "xenbus") == 0)
87 1.2 bouyer return 1;
88 1.2 bouyer return 0;
89 1.2 bouyer }
90 1.2 bouyer
91 1.2 bouyer static void
92 1.20 cegger xenbus_attach(device_t parent, device_t self, void *aux)
93 1.2 bouyer {
94 1.15 ad int err;
95 1.15 ad
96 1.2 bouyer aprint_normal(": Xen Virtual Bus Interface\n");
97 1.24 cegger xenbus_dev = self;
98 1.2 bouyer config_pending_incr();
99 1.15 ad
100 1.15 ad err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
101 1.15 ad NULL, "xenbus_probe");
102 1.15 ad if (err)
103 1.24 cegger aprint_error_dev(xenbus_dev,
104 1.23 cegger "kthread_create(xenbus_probe): %d\n", err);
105 1.2 bouyer }
106 1.2 bouyer
107 1.2 bouyer void
108 1.11 bouyer xenbus_backend_register(struct xenbus_backend_driver *xbakd)
109 1.11 bouyer {
110 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
111 1.11 bouyer }
112 1.11 bouyer
113 1.2 bouyer static int
114 1.2 bouyer read_otherend_details(struct xenbus_device *xendev,
115 1.2 bouyer const char *id_node, const char *path_node)
116 1.2 bouyer {
117 1.2 bouyer int err;
118 1.2 bouyer char *val, *ep;
119 1.1 bouyer
120 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
121 1.2 bouyer if (err) {
122 1.2 bouyer printf("reading other end details %s from %s\n",
123 1.2 bouyer id_node, xendev->xbusd_path);
124 1.2 bouyer xenbus_dev_fatal(xendev, err,
125 1.2 bouyer "reading other end details %s from %s",
126 1.2 bouyer id_node, xendev->xbusd_path);
127 1.2 bouyer return err;
128 1.2 bouyer }
129 1.2 bouyer xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
130 1.2 bouyer if (val[0] == '\0' || *ep != '\0') {
131 1.2 bouyer printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
132 1.2 bouyer xenbus_dev_fatal(xendev, err,
133 1.2 bouyer "reading other end details %s from %s: %s is not a number",
134 1.2 bouyer id_node, xendev->xbusd_path, val);
135 1.2 bouyer free(val, M_DEVBUF);
136 1.2 bouyer return EFTYPE;
137 1.2 bouyer }
138 1.2 bouyer free(val, M_DEVBUF);
139 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
140 1.1 bouyer if (err) {
141 1.2 bouyer printf("reading other end details %s from %s (%d)\n",
142 1.2 bouyer path_node, xendev->xbusd_path, err);
143 1.1 bouyer xenbus_dev_fatal(xendev, err,
144 1.2 bouyer "reading other end details %s from %s",
145 1.2 bouyer path_node, xendev->xbusd_path);
146 1.1 bouyer return err;
147 1.1 bouyer }
148 1.3 bouyer DPRINTK("read_otherend_details: read %s/%s returned %s\n",
149 1.3 bouyer xendev->xbusd_path, path_node, val);
150 1.2 bouyer xendev->xbusd_otherend = val;
151 1.2 bouyer
152 1.2 bouyer if (strlen(xendev->xbusd_otherend) == 0 ||
153 1.2 bouyer !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
154 1.2 bouyer printf("missing other end from %s\n", xendev->xbusd_path);
155 1.1 bouyer xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
156 1.2 bouyer xendev->xbusd_path);
157 1.2 bouyer free(xendev->xbusd_otherend, M_DEVBUF);
158 1.2 bouyer xendev->xbusd_otherend = NULL;
159 1.2 bouyer return ENOENT;
160 1.1 bouyer }
161 1.1 bouyer
162 1.1 bouyer return 0;
163 1.1 bouyer }
164 1.1 bouyer
165 1.2 bouyer static int
166 1.2 bouyer read_backend_details(struct xenbus_device *xendev)
167 1.1 bouyer {
168 1.1 bouyer return read_otherend_details(xendev, "backend-id", "backend");
169 1.1 bouyer }
170 1.1 bouyer
171 1.1 bouyer
172 1.2 bouyer static int
173 1.2 bouyer read_frontend_details(struct xenbus_device *xendev)
174 1.1 bouyer {
175 1.1 bouyer return read_otherend_details(xendev, "frontend-id", "frontend");
176 1.1 bouyer }
177 1.1 bouyer
178 1.3 bouyer #if unused
179 1.2 bouyer static void
180 1.2 bouyer free_otherend_details(struct xenbus_device *dev)
181 1.1 bouyer {
182 1.2 bouyer free(dev->xbusd_otherend, M_DEVBUF);
183 1.2 bouyer dev->xbusd_otherend = NULL;
184 1.1 bouyer }
185 1.3 bouyer #endif
186 1.1 bouyer
187 1.1 bouyer
188 1.2 bouyer static void
189 1.2 bouyer free_otherend_watch(struct xenbus_device *dev)
190 1.1 bouyer {
191 1.2 bouyer if (dev->xbusd_otherend_watch.node) {
192 1.2 bouyer unregister_xenbus_watch(&dev->xbusd_otherend_watch);
193 1.3 bouyer free(dev->xbusd_otherend_watch.node, M_DEVBUF);
194 1.2 bouyer dev->xbusd_otherend_watch.node = NULL;
195 1.1 bouyer }
196 1.1 bouyer }
197 1.1 bouyer
198 1.2 bouyer static void
199 1.2 bouyer otherend_changed(struct xenbus_watch *watch,
200 1.1 bouyer const char **vec, unsigned int len)
201 1.1 bouyer {
202 1.3 bouyer struct xenbus_device *xdev = watch->xbw_dev;
203 1.1 bouyer XenbusState state;
204 1.1 bouyer
205 1.1 bouyer /* Protect us against watches firing on old details when the otherend
206 1.1 bouyer details change, say immediately after a resume. */
207 1.3 bouyer if (!xdev->xbusd_otherend ||
208 1.3 bouyer strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
209 1.3 bouyer strlen(xdev->xbusd_otherend))) {
210 1.1 bouyer DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
211 1.1 bouyer return;
212 1.1 bouyer }
213 1.1 bouyer
214 1.3 bouyer state = xenbus_read_driver_state(xdev->xbusd_otherend);
215 1.1 bouyer
216 1.1 bouyer DPRINTK("state is %d, %s, %s",
217 1.3 bouyer state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
218 1.5 bouyer if (state == XenbusStateClosed) {
219 1.5 bouyer int error;
220 1.11 bouyer if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
221 1.11 bouyer error = xdev->xbusd_u.b.b_detach(
222 1.11 bouyer xdev->xbusd_u.b.b_cookie);
223 1.11 bouyer if (error) {
224 1.11 bouyer printf("could not detach %s: %d\n",
225 1.11 bouyer xdev->xbusd_path, error);
226 1.11 bouyer return;
227 1.11 bouyer }
228 1.11 bouyer } else {
229 1.11 bouyer error = config_detach(xdev->xbusd_u.f.f_dev,
230 1.11 bouyer DETACH_FORCE);
231 1.11 bouyer if (error) {
232 1.11 bouyer printf("could not detach %s: %d\n",
233 1.19 cegger device_xname(xdev->xbusd_u.f.f_dev), error);
234 1.11 bouyer return;
235 1.11 bouyer }
236 1.5 bouyer }
237 1.5 bouyer xenbus_free_device(xdev);
238 1.5 bouyer return;
239 1.5 bouyer }
240 1.3 bouyer if (xdev->xbusd_otherend_changed)
241 1.11 bouyer xdev->xbusd_otherend_changed(
242 1.11 bouyer (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
243 1.11 bouyer xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
244 1.1 bouyer }
245 1.1 bouyer
246 1.2 bouyer static int
247 1.2 bouyer talk_to_otherend(struct xenbus_device *dev)
248 1.1 bouyer {
249 1.1 bouyer free_otherend_watch(dev);
250 1.1 bouyer
251 1.2 bouyer return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
252 1.3 bouyer &dev->xbusd_otherend_watch,
253 1.3 bouyer otherend_changed);
254 1.1 bouyer }
255 1.1 bouyer
256 1.5 bouyer static struct xenbus_device *
257 1.5 bouyer xenbus_lookup_device_path(const char *path)
258 1.5 bouyer {
259 1.5 bouyer struct xenbus_device *xbusd;
260 1.5 bouyer
261 1.5 bouyer SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
262 1.5 bouyer if (strcmp(xbusd->xbusd_path, path) == 0)
263 1.5 bouyer return xbusd;
264 1.5 bouyer }
265 1.5 bouyer return NULL;
266 1.5 bouyer }
267 1.5 bouyer
268 1.2 bouyer static int
269 1.11 bouyer xenbus_probe_device_type(const char *path, const char *type,
270 1.11 bouyer int (*create)(struct xenbus_device *))
271 1.1 bouyer {
272 1.26.2.5 riz int err, i, pos, msize;
273 1.26.2.5 riz int *lookup = NULL;
274 1.11 bouyer unsigned long state;
275 1.1 bouyer char **dir;
276 1.1 bouyer unsigned int dir_n = 0;
277 1.2 bouyer struct xenbus_device *xbusd;
278 1.2 bouyer struct xenbusdev_attach_args xa;
279 1.2 bouyer char *ep;
280 1.2 bouyer
281 1.11 bouyer DPRINTK("probe %s type %s", path, type);
282 1.11 bouyer err = xenbus_directory(NULL, path, "", &dir_n, &dir);
283 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
284 1.2 bouyer if (err)
285 1.2 bouyer return err;
286 1.1 bouyer
287 1.26.2.5 riz /* Only sort frontend devices i.e. create == NULL*/
288 1.26.2.5 riz if (dir_n > 1 && create == NULL) {
289 1.26.2.5 riz int minp;
290 1.26.2.5 riz unsigned long minv;
291 1.26.2.5 riz unsigned long *id;
292 1.26.2.5 riz
293 1.26.2.5 riz lookup = malloc(sizeof(int) * dir_n, M_DEVBUF,
294 1.26.2.5 riz M_WAITOK | M_ZERO);
295 1.26.2.5 riz if (lookup == NULL)
296 1.26.2.5 riz panic("can't malloc lookup");
297 1.26.2.5 riz
298 1.26.2.5 riz id = malloc(sizeof(unsigned long) * dir_n, M_DEVBUF,
299 1.26.2.5 riz M_WAITOK | M_ZERO);
300 1.26.2.5 riz if (id == NULL)
301 1.26.2.5 riz panic("can't malloc id");
302 1.26.2.5 riz
303 1.26.2.5 riz /* Convert string values to numeric; skip invalid */
304 1.26.2.5 riz for (i = 0; i < dir_n; i++) {
305 1.26.2.5 riz /*
306 1.26.2.5 riz * Add one to differentiate numerical zero from invalid
307 1.26.2.5 riz * string. Has no effect on sort order.
308 1.26.2.5 riz */
309 1.26.2.5 riz id[i] = strtoul(dir[i], &ep, 10) + 1;
310 1.26.2.5 riz if (dir[i][0] == '\0' || *ep != '\0')
311 1.26.2.5 riz id[i] = 0;
312 1.26.2.5 riz }
313 1.26.2.5 riz
314 1.26.2.5 riz /* Build lookup table in ascending order */
315 1.26.2.5 riz for (pos = 0; pos < dir_n; ) {
316 1.26.2.5 riz minv = UINT32_MAX;
317 1.26.2.5 riz minp = -1;
318 1.26.2.5 riz for (i = 0; i < dir_n; i++) {
319 1.26.2.5 riz if (id[i] < minv && id[i] > 0) {
320 1.26.2.5 riz minv = id[i];
321 1.26.2.5 riz minp = i;
322 1.26.2.5 riz }
323 1.26.2.5 riz }
324 1.26.2.5 riz if (minp >= 0) {
325 1.26.2.5 riz lookup[pos++] = minp;
326 1.26.2.5 riz id[minp] = 0;
327 1.26.2.5 riz }
328 1.26.2.5 riz else
329 1.26.2.5 riz break;
330 1.26.2.5 riz }
331 1.26.2.5 riz
332 1.26.2.5 riz free(id, M_DEVBUF);
333 1.26.2.5 riz /* Adjust in case we had to skip non-numeric entries */
334 1.26.2.5 riz dir_n = pos;
335 1.26.2.5 riz }
336 1.26.2.5 riz
337 1.26.2.5 riz for (pos = 0; pos < dir_n; pos++) {
338 1.26.2.2 riz err = 0;
339 1.26.2.5 riz if (lookup)
340 1.26.2.5 riz i = lookup[pos];
341 1.26.2.5 riz else
342 1.26.2.5 riz i = pos;
343 1.5 bouyer /*
344 1.5 bouyer * add size of path to size of xenbus_device. xenbus_device
345 1.5 bouyer * already has room for one char in xbusd_path.
346 1.5 bouyer */
347 1.11 bouyer msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
348 1.5 bouyer xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
349 1.2 bouyer if (xbusd == NULL)
350 1.2 bouyer panic("can't malloc xbusd");
351 1.2 bouyer
352 1.2 bouyer snprintf(__UNCONST(xbusd->xbusd_path),
353 1.11 bouyer msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
354 1.5 bouyer if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
355 1.5 bouyer /* device already registered */
356 1.5 bouyer free(xbusd, M_DEVBUF);
357 1.5 bouyer continue;
358 1.5 bouyer }
359 1.11 bouyer err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
360 1.12 bouyer &state, 10);
361 1.11 bouyer if (err) {
362 1.11 bouyer printf("xenbus: can't get state "
363 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
364 1.11 bouyer free(xbusd, M_DEVBUF);
365 1.26.2.3 riz err = 0;
366 1.11 bouyer continue;
367 1.11 bouyer }
368 1.11 bouyer if (state != XenbusStateInitialising) {
369 1.11 bouyer /* device is not new */
370 1.11 bouyer free(xbusd, M_DEVBUF);
371 1.11 bouyer continue;
372 1.11 bouyer }
373 1.5 bouyer
374 1.3 bouyer xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
375 1.2 bouyer DPRINTK("xenbus_probe_device_type probe %s\n",
376 1.2 bouyer xbusd->xbusd_path);
377 1.11 bouyer if (create != NULL) {
378 1.11 bouyer xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
379 1.11 bouyer err = read_frontend_details(xbusd);
380 1.11 bouyer if (err != 0) {
381 1.11 bouyer printf("xenbus: can't get frontend details "
382 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
383 1.11 bouyer break;
384 1.11 bouyer }
385 1.11 bouyer if (create(xbusd)) {
386 1.11 bouyer free(xbusd, M_DEVBUF);
387 1.11 bouyer continue;
388 1.11 bouyer }
389 1.11 bouyer } else {
390 1.11 bouyer xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
391 1.11 bouyer xa.xa_xbusd = xbusd;
392 1.11 bouyer xa.xa_type = type;
393 1.11 bouyer xa.xa_id = strtoul(dir[i], &ep, 0);
394 1.11 bouyer if (dir[i][0] == '\0' || *ep != '\0') {
395 1.11 bouyer printf("xenbus device type %s: id %s is not a"
396 1.11 bouyer " number\n", type, dir[i]);
397 1.11 bouyer err = EFTYPE;
398 1.11 bouyer free(xbusd, M_DEVBUF);
399 1.11 bouyer break;
400 1.11 bouyer }
401 1.11 bouyer err = read_backend_details(xbusd);
402 1.11 bouyer if (err != 0) {
403 1.11 bouyer printf("xenbus: can't get backend details "
404 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
405 1.11 bouyer break;
406 1.11 bouyer }
407 1.24 cegger xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
408 1.11 bouyer "xenbus", &xa, xenbus_print);
409 1.11 bouyer if (xbusd->xbusd_u.f.f_dev == NULL) {
410 1.11 bouyer free(xbusd, M_DEVBUF);
411 1.11 bouyer continue;
412 1.11 bouyer }
413 1.3 bouyer }
414 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_device_list,
415 1.11 bouyer xbusd, xbusd_entries);
416 1.11 bouyer talk_to_otherend(xbusd);
417 1.1 bouyer }
418 1.2 bouyer free(dir, M_DEVBUF);
419 1.26.2.5 riz if (lookup)
420 1.26.2.5 riz free(lookup, M_DEVBUF);
421 1.26.2.5 riz
422 1.1 bouyer return err;
423 1.1 bouyer }
424 1.1 bouyer
425 1.2 bouyer static int
426 1.2 bouyer xenbus_print(void *aux, const char *pnp)
427 1.2 bouyer {
428 1.2 bouyer struct xenbusdev_attach_args *xa = aux;
429 1.2 bouyer
430 1.2 bouyer if (pnp) {
431 1.2 bouyer if (strcmp(xa->xa_type, "vbd") == 0)
432 1.2 bouyer aprint_normal("xbd");
433 1.2 bouyer else if (strcmp(xa->xa_type, "vif") == 0)
434 1.2 bouyer aprint_normal("xennet");
435 1.2 bouyer else
436 1.2 bouyer aprint_normal("unknown type %s", xa->xa_type);
437 1.2 bouyer aprint_normal(" at %s", pnp);
438 1.2 bouyer }
439 1.2 bouyer aprint_normal(" id %d", xa->xa_id);
440 1.2 bouyer return(UNCONF);
441 1.2 bouyer }
442 1.2 bouyer
443 1.2 bouyer static int
444 1.11 bouyer xenbus_probe_frontends(void)
445 1.1 bouyer {
446 1.2 bouyer int err;
447 1.1 bouyer char **dir;
448 1.1 bouyer unsigned int i, dir_n;
449 1.11 bouyer char path[30];
450 1.1 bouyer
451 1.11 bouyer DPRINTK("probe device");
452 1.11 bouyer err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
453 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
454 1.2 bouyer if (err)
455 1.2 bouyer return err;
456 1.1 bouyer
457 1.1 bouyer for (i = 0; i < dir_n; i++) {
458 1.26.2.1 snj /*
459 1.26.2.1 snj * console is configured through xen_start_info when
460 1.26.2.1 snj * xencons is attaching to hypervisor, so avoid console
461 1.26.2.1 snj * probing when configuring xenbus devices
462 1.26.2.1 snj */
463 1.26.2.1 snj if (strcmp(dir[i], "console") == 0)
464 1.26.2.1 snj continue;
465 1.26.2.1 snj
466 1.11 bouyer snprintf(path, sizeof(path), "device/%s", dir[i]);
467 1.11 bouyer err = xenbus_probe_device_type(path, dir[i], NULL);
468 1.1 bouyer if (err)
469 1.1 bouyer break;
470 1.1 bouyer }
471 1.2 bouyer free(dir, M_DEVBUF);
472 1.1 bouyer return err;
473 1.1 bouyer }
474 1.1 bouyer
475 1.11 bouyer static int
476 1.11 bouyer xenbus_probe_backends(void)
477 1.11 bouyer {
478 1.11 bouyer int err;
479 1.11 bouyer char **dirt, **dirid;
480 1.11 bouyer unsigned int type, id, dirt_n, dirid_n;
481 1.11 bouyer char path[30];
482 1.11 bouyer struct xenbus_backend_driver *xbakd;
483 1.11 bouyer
484 1.11 bouyer DPRINTK("probe backend");
485 1.11 bouyer err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
486 1.11 bouyer DPRINTK("directory err %d dirt_n %d", err, dirt_n);
487 1.11 bouyer if (err)
488 1.11 bouyer return err;
489 1.11 bouyer
490 1.11 bouyer for (type = 0; type < dirt_n; type++) {
491 1.11 bouyer SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
492 1.11 bouyer xbakd_entries) {
493 1.11 bouyer if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
494 1.11 bouyer break;
495 1.11 bouyer }
496 1.11 bouyer if (xbakd == NULL)
497 1.11 bouyer continue;
498 1.11 bouyer err = xenbus_directory(NULL, "backend", dirt[type],
499 1.11 bouyer &dirid_n, &dirid);
500 1.11 bouyer DPRINTK("directory backend/%s err %d dirid_n %d",
501 1.11 bouyer dirt[type], err, dirid_n);
502 1.14 christos if (err) {
503 1.14 christos free(dirt, M_DEVBUF); /* to be checked */
504 1.11 bouyer return err;
505 1.14 christos }
506 1.11 bouyer for (id = 0; id < dirid_n; id++) {
507 1.11 bouyer snprintf(path, sizeof(path), "backend/%s/%s",
508 1.11 bouyer dirt[type], dirid[id]);
509 1.11 bouyer err = xenbus_probe_device_type(path, dirt[type],
510 1.11 bouyer xbakd->xbakd_create);
511 1.11 bouyer if (err)
512 1.11 bouyer break;
513 1.11 bouyer }
514 1.11 bouyer free(dirid, M_DEVBUF);
515 1.11 bouyer }
516 1.11 bouyer free(dirt, M_DEVBUF);
517 1.11 bouyer return err;
518 1.11 bouyer }
519 1.11 bouyer
520 1.5 bouyer int
521 1.5 bouyer xenbus_free_device(struct xenbus_device *xbusd)
522 1.5 bouyer {
523 1.5 bouyer KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
524 1.5 bouyer SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
525 1.5 bouyer free_otherend_watch(xbusd);
526 1.5 bouyer free(xbusd->xbusd_otherend, M_DEVBUF);
527 1.5 bouyer xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
528 1.5 bouyer free(xbusd, M_DEVBUF);
529 1.5 bouyer return 0;
530 1.5 bouyer }
531 1.5 bouyer
532 1.2 bouyer static void
533 1.2 bouyer frontend_changed(struct xenbus_watch *watch,
534 1.1 bouyer const char **vec, unsigned int len)
535 1.1 bouyer {
536 1.11 bouyer DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
537 1.11 bouyer xenbus_probe_frontends();
538 1.1 bouyer }
539 1.1 bouyer
540 1.2 bouyer static void
541 1.2 bouyer backend_changed(struct xenbus_watch *watch,
542 1.1 bouyer const char **vec, unsigned int len)
543 1.1 bouyer {
544 1.11 bouyer DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
545 1.11 bouyer xenbus_probe_backends();
546 1.1 bouyer }
547 1.1 bouyer
548 1.2 bouyer
549 1.1 bouyer /* We watch for devices appearing and vanishing. */
550 1.3 bouyer static struct xenbus_watch fe_watch;
551 1.1 bouyer
552 1.3 bouyer static struct xenbus_watch be_watch;
553 1.1 bouyer
554 1.1 bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
555 1.1 bouyer int xenstored_ready = 0;
556 1.1 bouyer
557 1.2 bouyer void
558 1.2 bouyer xenbus_probe(void *unused)
559 1.1 bouyer {
560 1.2 bouyer KASSERT((xenstored_ready > 0));
561 1.1 bouyer
562 1.1 bouyer /* Enumerate devices in xenstore. */
563 1.11 bouyer xenbus_probe_frontends();
564 1.11 bouyer xenbus_probe_backends();
565 1.1 bouyer
566 1.1 bouyer /* Watch for changes. */
567 1.3 bouyer fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
568 1.3 bouyer strcpy(fe_watch.node, "device");
569 1.3 bouyer fe_watch.xbw_callback = frontend_changed;
570 1.1 bouyer register_xenbus_watch(&fe_watch);
571 1.3 bouyer be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
572 1.3 bouyer strcpy(be_watch.node, "backend");
573 1.3 bouyer be_watch.xbw_callback = backend_changed;
574 1.1 bouyer register_xenbus_watch(&be_watch);
575 1.13 yamt shutdown_xenbus_setup();
576 1.1 bouyer
577 1.1 bouyer /* Notify others that xenstore is up */
578 1.2 bouyer //notifier_call_chain(&xenstore_chain, 0, NULL);
579 1.1 bouyer }
580 1.1 bouyer
581 1.2 bouyer static void
582 1.2 bouyer xenbus_probe_init(void *unused)
583 1.1 bouyer {
584 1.23 cegger int err = 0;
585 1.23 cegger bool dom0;
586 1.23 cegger vaddr_t page = 0;
587 1.1 bouyer
588 1.1 bouyer DPRINTK("");
589 1.1 bouyer
590 1.5 bouyer SLIST_INIT(&xenbus_device_list);
591 1.5 bouyer
592 1.1 bouyer /*
593 1.1 bouyer ** Domain0 doesn't have a store_evtchn or store_mfn yet.
594 1.1 bouyer */
595 1.23 cegger dom0 = xendomain_is_dom0();
596 1.1 bouyer if (dom0) {
597 1.7 bouyer #if defined(DOM0OPS)
598 1.7 bouyer paddr_t ma;
599 1.22 tron evtchn_op_t op = { .cmd = 0 };
600 1.1 bouyer
601 1.1 bouyer /* Allocate page. */
602 1.7 bouyer page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
603 1.7 bouyer UVM_KMF_ZERO | UVM_KMF_WIRED);
604 1.1 bouyer if (!page)
605 1.7 bouyer panic("can't get xenstore page");
606 1.1 bouyer
607 1.7 bouyer (void)pmap_extract_ma(pmap_kernel(), page, &ma);
608 1.7 bouyer xen_start_info.store_mfn = ma >> PAGE_SHIFT;
609 1.7 bouyer xenstore_interface = (void *)page;
610 1.1 bouyer
611 1.1 bouyer /* Next allocate a local port which xenstored can bind to */
612 1.1 bouyer op.cmd = EVTCHNOP_alloc_unbound;
613 1.1 bouyer op.u.alloc_unbound.dom = DOMID_SELF;
614 1.1 bouyer op.u.alloc_unbound.remote_dom = 0;
615 1.1 bouyer
616 1.23 cegger err = HYPERVISOR_event_channel_op(&op);
617 1.23 cegger if (err) {
618 1.24 cegger aprint_error_dev(xenbus_dev,
619 1.23 cegger "can't register xenstore event\n");
620 1.23 cegger goto err0;
621 1.23 cegger }
622 1.23 cegger
623 1.7 bouyer xen_start_info.store_evtchn = op.u.alloc_unbound.port;
624 1.1 bouyer
625 1.21 cegger DELAY(1000);
626 1.7 bouyer #else /* DOM0OPS */
627 1.23 cegger kthread_exit(0); /* can't get a working xenstore in this case */
628 1.7 bouyer #endif /* DOM0OPS */
629 1.1 bouyer }
630 1.1 bouyer
631 1.26.2.4 sborrill /* Publish xenbus and Xenstore info in /kern/xen */
632 1.26.2.4 sborrill xenbus_kernfs_init();
633 1.26.2.4 sborrill
634 1.10 bouyer /* register event handler */
635 1.24 cegger xb_init_comms(xenbus_dev);
636 1.10 bouyer
637 1.1 bouyer /* Initialize the interface to xenstore. */
638 1.25 jym err = xs_init(xenbus_dev);
639 1.1 bouyer if (err) {
640 1.24 cegger aprint_error_dev(xenbus_dev,
641 1.23 cegger "Error initializing xenstore comms: %i\n", err);
642 1.23 cegger goto err0;
643 1.1 bouyer }
644 1.1 bouyer
645 1.1 bouyer if (!dom0) {
646 1.1 bouyer xenstored_ready = 1;
647 1.1 bouyer xenbus_probe(NULL);
648 1.1 bouyer }
649 1.1 bouyer
650 1.2 bouyer DPRINTK("done");
651 1.2 bouyer config_pending_decr();
652 1.11 bouyer #ifdef DOM0OPS
653 1.11 bouyer if (dom0) {
654 1.11 bouyer int s;
655 1.11 bouyer s = spltty();
656 1.11 bouyer while (xenstored_ready == 0) {
657 1.11 bouyer tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
658 1.11 bouyer xenbus_probe(NULL);
659 1.11 bouyer }
660 1.11 bouyer splx(s);
661 1.11 bouyer }
662 1.11 bouyer #endif
663 1.2 bouyer kthread_exit(0);
664 1.23 cegger
665 1.23 cegger err0:
666 1.23 cegger if (page)
667 1.23 cegger uvm_km_free(kernel_map, page, PAGE_SIZE,
668 1.23 cegger UVM_KMF_ZERO | UVM_KMF_WIRED);
669 1.23 cegger kthread_exit(err);
670 1.1 bouyer }
671 1.1 bouyer
672 1.1 bouyer /*
673 1.1 bouyer * Local variables:
674 1.1 bouyer * c-file-style: "linux"
675 1.1 bouyer * indent-tabs-mode: t
676 1.1 bouyer * c-indent-level: 8
677 1.1 bouyer * c-basic-offset: 8
678 1.1 bouyer * tab-width: 8
679 1.1 bouyer * End:
680 1.1 bouyer */
681