Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / xen / xenbus / xenbus_probe.c
blobc7674ee0c3afcb1d3574f748540658fe01535534
1 /* $NetBSD: xenbus_probe.c,v 1.26 2008/10/29 13:53:15 cegger Exp $ */
2 /******************************************************************************
3 * Talks to Xen Store to figure out what devices we have.
5 * Copyright (C) 2005 Rusty Russell, IBM Corporation
6 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
7 * Copyright (C) 2005 XenSource Ltd
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.26 2008/10/29 13:53:15 cegger Exp $");
34 #if 0
35 #define DPRINTK(fmt, args...) \
36 printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
37 #else
38 #define DPRINTK(fmt, args...) ((void)0)
39 #endif
41 #include <sys/types.h>
42 #include <sys/null.h>
43 #include <sys/errno.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/kthread.h>
48 #include <uvm/uvm.h>
50 #include <machine/stdarg.h>
52 #include <xen/xen.h> /* for xendomain_is_dom0() */
53 #include <xen/hypervisor.h>
54 #include <xen/xenbus.h>
55 #include <xen/evtchn.h>
56 #include <xen/shutdown_xenbus.h>
58 #include "xenbus_comms.h"
60 extern struct semaphore xenwatch_mutex;
62 #define streq(a, b) (strcmp((a), (b)) == 0)
64 static int xenbus_match(device_t, cfdata_t, void *);
65 static void xenbus_attach(device_t, device_t, void *);
66 static int xenbus_print(void *, const char *);
68 static void xenbus_probe_init(void *);
70 static struct xenbus_device *xenbus_lookup_device_path(const char *);
72 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
73 NULL, NULL);
75 device_t xenbus_dev;
77 SLIST_HEAD(, xenbus_device) xenbus_device_list;
78 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
79 SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
81 int
82 xenbus_match(device_t parent, cfdata_t match, void *aux)
84 struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
86 if (strcmp(xa->xa_device, "xenbus") == 0)
87 return 1;
88 return 0;
91 static void
92 xenbus_attach(device_t parent, device_t self, void *aux)
94 int err;
96 aprint_normal(": Xen Virtual Bus Interface\n");
97 xenbus_dev = self;
98 config_pending_incr();
100 err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
101 NULL, "xenbus_probe");
102 if (err)
103 aprint_error_dev(xenbus_dev,
104 "kthread_create(xenbus_probe): %d\n", err);
107 void
108 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
110 SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
113 static int
114 read_otherend_details(struct xenbus_device *xendev,
115 const char *id_node, const char *path_node)
117 int err;
118 char *val, *ep;
120 err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
121 if (err) {
122 printf("reading other end details %s from %s\n",
123 id_node, xendev->xbusd_path);
124 xenbus_dev_fatal(xendev, err,
125 "reading other end details %s from %s",
126 id_node, xendev->xbusd_path);
127 return err;
129 xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
130 if (val[0] == '\0' || *ep != '\0') {
131 printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
132 xenbus_dev_fatal(xendev, err,
133 "reading other end details %s from %s: %s is not a number",
134 id_node, xendev->xbusd_path, val);
135 free(val, M_DEVBUF);
136 return EFTYPE;
138 free(val, M_DEVBUF);
139 err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
140 if (err) {
141 printf("reading other end details %s from %s (%d)\n",
142 path_node, xendev->xbusd_path, err);
143 xenbus_dev_fatal(xendev, err,
144 "reading other end details %s from %s",
145 path_node, xendev->xbusd_path);
146 return err;
148 DPRINTK("read_otherend_details: read %s/%s returned %s\n",
149 xendev->xbusd_path, path_node, val);
150 xendev->xbusd_otherend = val;
152 if (strlen(xendev->xbusd_otherend) == 0 ||
153 !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
154 printf("missing other end from %s\n", xendev->xbusd_path);
155 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
156 xendev->xbusd_path);
157 free(xendev->xbusd_otherend, M_DEVBUF);
158 xendev->xbusd_otherend = NULL;
159 return ENOENT;
162 return 0;
165 static int
166 read_backend_details(struct xenbus_device *xendev)
168 return read_otherend_details(xendev, "backend-id", "backend");
172 static int
173 read_frontend_details(struct xenbus_device *xendev)
175 return read_otherend_details(xendev, "frontend-id", "frontend");
178 #if unused
179 static void
180 free_otherend_details(struct xenbus_device *dev)
182 free(dev->xbusd_otherend, M_DEVBUF);
183 dev->xbusd_otherend = NULL;
185 #endif
188 static void
189 free_otherend_watch(struct xenbus_device *dev)
191 if (dev->xbusd_otherend_watch.node) {
192 unregister_xenbus_watch(&dev->xbusd_otherend_watch);
193 free(dev->xbusd_otherend_watch.node, M_DEVBUF);
194 dev->xbusd_otherend_watch.node = NULL;
198 static void
199 otherend_changed(struct xenbus_watch *watch,
200 const char **vec, unsigned int len)
202 struct xenbus_device *xdev = watch->xbw_dev;
203 XenbusState state;
205 /* Protect us against watches firing on old details when the otherend
206 details change, say immediately after a resume. */
207 if (!xdev->xbusd_otherend ||
208 strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
209 strlen(xdev->xbusd_otherend))) {
210 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
211 return;
214 state = xenbus_read_driver_state(xdev->xbusd_otherend);
216 DPRINTK("state is %d, %s, %s",
217 state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
218 if (state == XenbusStateClosed) {
219 int error;
220 if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
221 error = xdev->xbusd_u.b.b_detach(
222 xdev->xbusd_u.b.b_cookie);
223 if (error) {
224 printf("could not detach %s: %d\n",
225 xdev->xbusd_path, error);
226 return;
228 } else {
229 error = config_detach(xdev->xbusd_u.f.f_dev,
230 DETACH_FORCE);
231 if (error) {
232 printf("could not detach %s: %d\n",
233 device_xname(xdev->xbusd_u.f.f_dev), error);
234 return;
237 xenbus_free_device(xdev);
238 return;
240 if (xdev->xbusd_otherend_changed)
241 xdev->xbusd_otherend_changed(
242 (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
243 xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
246 static int
247 talk_to_otherend(struct xenbus_device *dev)
249 free_otherend_watch(dev);
251 return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
252 &dev->xbusd_otherend_watch,
253 otherend_changed);
256 static struct xenbus_device *
257 xenbus_lookup_device_path(const char *path)
259 struct xenbus_device *xbusd;
261 SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
262 if (strcmp(xbusd->xbusd_path, path) == 0)
263 return xbusd;
265 return NULL;
268 static int
269 xenbus_probe_device_type(const char *path, const char *type,
270 int (*create)(struct xenbus_device *))
272 int err, i, msize;
273 unsigned long state;
274 char **dir;
275 unsigned int dir_n = 0;
276 struct xenbus_device *xbusd;
277 struct xenbusdev_attach_args xa;
278 char *ep;
280 DPRINTK("probe %s type %s", path, type);
281 err = xenbus_directory(NULL, path, "", &dir_n, &dir);
282 DPRINTK("directory err %d dir_n %d", err, dir_n);
283 if (err)
284 return err;
286 for (i = 0; i < dir_n; i++) {
288 * add size of path to size of xenbus_device. xenbus_device
289 * already has room for one char in xbusd_path.
291 msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
292 xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
293 if (xbusd == NULL)
294 panic("can't malloc xbusd");
296 snprintf(__UNCONST(xbusd->xbusd_path),
297 msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
298 if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
299 /* device already registered */
300 free(xbusd, M_DEVBUF);
301 continue;
303 err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
304 &state, 10);
305 if (err) {
306 printf("xenbus: can't get state "
307 "for %s (%d)\n", xbusd->xbusd_path, err);
308 free(xbusd, M_DEVBUF);
309 continue;
311 if (state != XenbusStateInitialising) {
312 /* device is not new */
313 free(xbusd, M_DEVBUF);
314 continue;
317 xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
318 DPRINTK("xenbus_probe_device_type probe %s\n",
319 xbusd->xbusd_path);
320 if (create != NULL) {
321 xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
322 err = read_frontend_details(xbusd);
323 if (err != 0) {
324 printf("xenbus: can't get frontend details "
325 "for %s (%d)\n", xbusd->xbusd_path, err);
326 break;
328 if (create(xbusd)) {
329 free(xbusd, M_DEVBUF);
330 continue;
332 } else {
333 xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
334 xa.xa_xbusd = xbusd;
335 xa.xa_type = type;
336 xa.xa_id = strtoul(dir[i], &ep, 0);
337 if (dir[i][0] == '\0' || *ep != '\0') {
338 printf("xenbus device type %s: id %s is not a"
339 " number\n", type, dir[i]);
340 err = EFTYPE;
341 free(xbusd, M_DEVBUF);
342 break;
344 err = read_backend_details(xbusd);
345 if (err != 0) {
346 printf("xenbus: can't get backend details "
347 "for %s (%d)\n", xbusd->xbusd_path, err);
348 break;
350 xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
351 "xenbus", &xa, xenbus_print);
352 if (xbusd->xbusd_u.f.f_dev == NULL) {
353 free(xbusd, M_DEVBUF);
354 continue;
357 SLIST_INSERT_HEAD(&xenbus_device_list,
358 xbusd, xbusd_entries);
359 talk_to_otherend(xbusd);
361 free(dir, M_DEVBUF);
362 return err;
365 static int
366 xenbus_print(void *aux, const char *pnp)
368 struct xenbusdev_attach_args *xa = aux;
370 if (pnp) {
371 if (strcmp(xa->xa_type, "vbd") == 0)
372 aprint_normal("xbd");
373 else if (strcmp(xa->xa_type, "vif") == 0)
374 aprint_normal("xennet");
375 else
376 aprint_normal("unknown type %s", xa->xa_type);
377 aprint_normal(" at %s", pnp);
379 aprint_normal(" id %d", xa->xa_id);
380 return(UNCONF);
383 static int
384 xenbus_probe_frontends(void)
386 int err;
387 char **dir;
388 unsigned int i, dir_n;
389 char path[30];
391 DPRINTK("probe device");
392 err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
393 DPRINTK("directory err %d dir_n %d", err, dir_n);
394 if (err)
395 return err;
397 for (i = 0; i < dir_n; i++) {
399 * console is configured through xen_start_info when
400 * xencons is attaching to hypervisor, so avoid console
401 * probing when configuring xenbus devices
403 if (strcmp(dir[i], "console") == 0)
404 continue;
406 snprintf(path, sizeof(path), "device/%s", dir[i]);
407 err = xenbus_probe_device_type(path, dir[i], NULL);
408 if (err)
409 break;
411 free(dir, M_DEVBUF);
412 return err;
415 static int
416 xenbus_probe_backends(void)
418 int err;
419 char **dirt, **dirid;
420 unsigned int type, id, dirt_n, dirid_n;
421 char path[30];
422 struct xenbus_backend_driver *xbakd;
424 DPRINTK("probe backend");
425 err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
426 DPRINTK("directory err %d dirt_n %d", err, dirt_n);
427 if (err)
428 return err;
430 for (type = 0; type < dirt_n; type++) {
431 SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
432 xbakd_entries) {
433 if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
434 break;
436 if (xbakd == NULL)
437 continue;
438 err = xenbus_directory(NULL, "backend", dirt[type],
439 &dirid_n, &dirid);
440 DPRINTK("directory backend/%s err %d dirid_n %d",
441 dirt[type], err, dirid_n);
442 if (err) {
443 free(dirt, M_DEVBUF); /* to be checked */
444 return err;
446 for (id = 0; id < dirid_n; id++) {
447 snprintf(path, sizeof(path), "backend/%s/%s",
448 dirt[type], dirid[id]);
449 err = xenbus_probe_device_type(path, dirt[type],
450 xbakd->xbakd_create);
451 if (err)
452 break;
454 free(dirid, M_DEVBUF);
456 free(dirt, M_DEVBUF);
457 return err;
461 xenbus_free_device(struct xenbus_device *xbusd)
463 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
464 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
465 free_otherend_watch(xbusd);
466 free(xbusd->xbusd_otherend, M_DEVBUF);
467 xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
468 free(xbusd, M_DEVBUF);
469 return 0;
472 static void
473 frontend_changed(struct xenbus_watch *watch,
474 const char **vec, unsigned int len)
476 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
477 xenbus_probe_frontends();
480 static void
481 backend_changed(struct xenbus_watch *watch,
482 const char **vec, unsigned int len)
484 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
485 xenbus_probe_backends();
489 /* We watch for devices appearing and vanishing. */
490 static struct xenbus_watch fe_watch;
492 static struct xenbus_watch be_watch;
494 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
495 int xenstored_ready = 0;
497 void
498 xenbus_probe(void *unused)
500 KASSERT((xenstored_ready > 0));
502 /* Enumerate devices in xenstore. */
503 xenbus_probe_frontends();
504 xenbus_probe_backends();
506 /* Watch for changes. */
507 fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
508 strcpy(fe_watch.node, "device");
509 fe_watch.xbw_callback = frontend_changed;
510 register_xenbus_watch(&fe_watch);
511 be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
512 strcpy(be_watch.node, "backend");
513 be_watch.xbw_callback = backend_changed;
514 register_xenbus_watch(&be_watch);
515 shutdown_xenbus_setup();
517 /* Notify others that xenstore is up */
518 //notifier_call_chain(&xenstore_chain, 0, NULL);
521 static void
522 xenbus_probe_init(void *unused)
524 int err = 0;
525 bool dom0;
526 vaddr_t page = 0;
528 DPRINTK("");
530 SLIST_INIT(&xenbus_device_list);
533 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
535 dom0 = xendomain_is_dom0();
536 if (dom0) {
537 #if defined(DOM0OPS)
538 paddr_t ma;
539 evtchn_op_t op = { .cmd = 0 };
541 /* Allocate page. */
542 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
543 UVM_KMF_ZERO | UVM_KMF_WIRED);
544 if (!page)
545 panic("can't get xenstore page");
547 (void)pmap_extract_ma(pmap_kernel(), page, &ma);
548 xen_start_info.store_mfn = ma >> PAGE_SHIFT;
549 xenstore_interface = (void *)page;
551 /* Next allocate a local port which xenstored can bind to */
552 op.cmd = EVTCHNOP_alloc_unbound;
553 op.u.alloc_unbound.dom = DOMID_SELF;
554 op.u.alloc_unbound.remote_dom = 0;
556 err = HYPERVISOR_event_channel_op(&op);
557 if (err) {
558 aprint_error_dev(xenbus_dev,
559 "can't register xenstore event\n");
560 goto err0;
563 xen_start_info.store_evtchn = op.u.alloc_unbound.port;
565 /* And finally publish the above info in /kern/xen */
566 xenbus_kernfs_init();
568 DELAY(1000);
569 #else /* DOM0OPS */
570 kthread_exit(0); /* can't get a working xenstore in this case */
571 #endif /* DOM0OPS */
574 /* register event handler */
575 xb_init_comms(xenbus_dev);
577 /* Initialize the interface to xenstore. */
578 err = xs_init(xenbus_dev);
579 if (err) {
580 aprint_error_dev(xenbus_dev,
581 "Error initializing xenstore comms: %i\n", err);
582 goto err0;
585 if (!dom0) {
586 xenstored_ready = 1;
587 xenbus_probe(NULL);
590 DPRINTK("done");
591 config_pending_decr();
592 #ifdef DOM0OPS
593 if (dom0) {
594 int s;
595 s = spltty();
596 while (xenstored_ready == 0) {
597 tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
598 xenbus_probe(NULL);
600 splx(s);
602 #endif
603 kthread_exit(0);
605 err0:
606 if (page)
607 uvm_km_free(kernel_map, page, PAGE_SIZE,
608 UVM_KMF_ZERO | UVM_KMF_WIRED);
609 kthread_exit(err);
613 * Local variables:
614 * c-file-style: "linux"
615 * indent-tabs-mode: t
616 * c-indent-level: 8
617 * c-basic-offset: 8
618 * tab-width: 8
619 * End: