Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.48
      1 /* $NetBSD: xenbus_probe.c,v 1.48 2020/04/10 14:54:33 jdolecek Exp $ */
      2 /******************************************************************************
      3  * Talks to Xen Store to figure out what devices we have.
      4  *
      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:
     11  *
     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:
     18  *
     19  * The above copyright notice and this permission notice shall be included in
     20  * all copies or substantial portions of the Software.
     21  *
     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.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.48 2020/04/10 14:54:33 jdolecek Exp $");
     33 
     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
     40 
     41 #include <sys/types.h>
     42 #include <sys/null.h>
     43 #include <sys/errno.h>
     44 #include <sys/kmem.h>
     45 #include <sys/systm.h>
     46 #include <sys/param.h>
     47 #include <sys/kthread.h>
     48 #include <uvm/uvm.h>
     49 
     50 #include <xen/xen.h>	/* for xendomain_is_dom0() */
     51 #include <xen/hypervisor.h>
     52 #include <xen/xenbus.h>
     53 #include <xen/evtchn.h>
     54 #include <xen/shutdown_xenbus.h>
     55 
     56 #include "xenbus_comms.h"
     57 
     58 static int  xenbus_match(device_t, cfdata_t, void *);
     59 static void xenbus_attach(device_t, device_t, void *);
     60 static int  xenbus_print(void *, const char *);
     61 
     62 /* power management, for save/restore */
     63 static bool xenbus_suspend(device_t, const pmf_qual_t *);
     64 static bool xenbus_resume(device_t, const pmf_qual_t *);
     65 
     66 /* routines gathering device information from XenStore */
     67 static int  read_otherend_details(struct xenbus_device *,
     68 		const char *, const char *);
     69 static int  read_backend_details (struct xenbus_device *);
     70 static int  read_frontend_details(struct xenbus_device *);
     71 static void free_otherend_details(struct xenbus_device *);
     72 
     73 static int  watch_otherend     (struct xenbus_device *);
     74 static void free_otherend_watch(struct xenbus_device *);
     75 
     76 static void xenbus_probe_init(void *);
     77 
     78 static struct xenbus_device *xenbus_lookup_device_path(const char *);
     79 
     80 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
     81     NULL, NULL);
     82 
     83 device_t xenbus_dev;
     84 bus_dma_tag_t xenbus_dmat;
     85 
     86 SLIST_HEAD(, xenbus_device) xenbus_device_list;
     87 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
     88 	SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
     89 
     90 int
     91 xenbus_match(device_t parent, cfdata_t match, void *aux)
     92 {
     93 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
     94 
     95 	if (strcmp(xa->xa_device, "xenbus") == 0)
     96 		return 1;
     97 	return 0;
     98 }
     99 
    100 static void
    101 xenbus_attach(device_t parent, device_t self, void *aux)
    102 {
    103 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
    104 	int err;
    105 
    106 	aprint_normal(": Xen Virtual Bus Interface\n");
    107 	xenbus_dev = self;
    108 	xenbus_dmat = xa->xa_dmat;
    109 	config_pending_incr(self);
    110 
    111 	err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
    112 	    NULL, "xenbus_probe");
    113 	if (err)
    114 		aprint_error_dev(xenbus_dev,
    115 				"kthread_create(xenbus_probe): %d\n", err);
    116 
    117 	if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
    118 		aprint_error_dev(self, "couldn't establish power handler\n");
    119 }
    120 
    121 static bool
    122 xenbus_suspend(device_t dev, const pmf_qual_t *qual)
    123 {
    124 	xs_suspend();
    125 	xb_suspend_comms(dev);
    126 
    127 	return true;
    128 }
    129 
    130 static bool
    131 xenbus_resume(device_t dev, const pmf_qual_t *qual)
    132 {
    133 	xb_init_comms(dev);
    134 	xs_resume();
    135 
    136 	return true;
    137 }
    138 
    139 /*
    140  * Suspend a xenbus device
    141  */
    142 bool
    143 xenbus_device_suspend(struct xenbus_device *dev) {
    144 
    145 	free_otherend_details(dev);
    146 	return true;
    147 }
    148 
    149 /*
    150  * Resume a xenbus device
    151  */
    152 bool
    153 xenbus_device_resume(struct xenbus_device *dev) {
    154 
    155 	if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
    156 		read_backend_details(dev);
    157 	}
    158 
    159 	return true;
    160 }
    161 
    162 void
    163 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
    164 {
    165 	SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
    166 }
    167 
    168 static int
    169 read_otherend_details(struct xenbus_device *xendev,
    170 				 const char *id_node, const char *path_node)
    171 {
    172 	int err;
    173 	unsigned long id;
    174 
    175 	err = xenbus_read_ul(NULL, xendev->xbusd_path, id_node, &id, 10);
    176 	if (err) {
    177 		printf("reading other end details %s from %s\n",
    178 		    id_node, xendev->xbusd_path);
    179 		xenbus_dev_fatal(xendev, err,
    180 				 "reading other end details %s from %s",
    181 				 id_node, xendev->xbusd_path);
    182 		return err;
    183 	}
    184 	xendev->xbusd_otherend_id = (int)id;
    185 
    186 	err = xenbus_read(NULL, xendev->xbusd_path, path_node,
    187 	    xendev->xbusd_otherend, sizeof(xendev->xbusd_otherend));
    188 	if (err) {
    189 		printf("reading other end details %s from %s (%d)\n",
    190 		    path_node, xendev->xbusd_path, err);
    191 		xenbus_dev_fatal(xendev, err,
    192 				 "reading other end details %s from %s",
    193 				 path_node, xendev->xbusd_path);
    194 		return err;
    195 	}
    196 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
    197 	    xendev->xbusd_path, path_node, val);
    198 
    199 	if (strlen(xendev->xbusd_otherend) == 0 ||
    200 	    !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
    201 		printf("missing other end from %s\n", xendev->xbusd_path);
    202 		xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
    203 				 xendev->xbusd_path);
    204 		free_otherend_details(xendev);
    205 		return ENOENT;
    206 	}
    207 
    208 	return 0;
    209 }
    210 
    211 static int
    212 read_backend_details(struct xenbus_device *xendev)
    213 {
    214 	return read_otherend_details(xendev, "backend-id", "backend");
    215 }
    216 
    217 
    218 static int
    219 read_frontend_details(struct xenbus_device *xendev)
    220 {
    221 	return read_otherend_details(xendev, "frontend-id", "frontend");
    222 }
    223 
    224 static void
    225 free_otherend_details(struct xenbus_device *dev)
    226 {
    227 	/* Nothing to free */
    228 	dev->xbusd_otherend[0] = '\0';
    229 }
    230 
    231 static void
    232 free_otherend_watch(struct xenbus_device *dev)
    233 {
    234 	if (dev->xbusd_otherend_watch.node)
    235 		xenbus_unwatch_path(&dev->xbusd_otherend_watch);
    236 }
    237 
    238 static void
    239 otherend_changed(struct xenbus_watch *watch,
    240 			     const char **vec, unsigned int len)
    241 {
    242 	struct xenbus_device *xdev = watch->xbw_dev;
    243 	XenbusState state;
    244 
    245 	/* Protect us against watches firing on old details when the otherend
    246 	   details change, say immediately after a resume. */
    247 	if (!xdev->xbusd_otherend ||
    248 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    249 		    strlen(xdev->xbusd_otherend))) {
    250 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    251 		return;
    252 	}
    253 
    254 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    255 
    256 	DPRINTK("state is %d, %s, %s",
    257 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    258 	if (state == XenbusStateClosed) {
    259 		int error;
    260 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
    261 			error = xdev->xbusd_u.b.b_detach(
    262 			    xdev->xbusd_u.b.b_cookie);
    263 			if (error) {
    264 				printf("could not detach %s: %d\n",
    265 				    xdev->xbusd_path, error);
    266 				return;
    267 			}
    268 		} else {
    269 			error = config_detach(xdev->xbusd_u.f.f_dev,
    270 			    DETACH_FORCE);
    271 			if (error) {
    272 				printf("could not detach %s: %d\n",
    273 				    device_xname(xdev->xbusd_u.f.f_dev), error);
    274 				return;
    275 			}
    276 		}
    277 		xenbus_free_device(xdev);
    278 		return;
    279 	}
    280 	if (xdev->xbusd_otherend_changed)
    281 		xdev->xbusd_otherend_changed(
    282 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
    283 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
    284 }
    285 
    286 static int
    287 watch_otherend(struct xenbus_device *dev)
    288 {
    289 	free_otherend_watch(dev);
    290 
    291 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    292 				  &dev->xbusd_otherend_watch,
    293 				  otherend_changed);
    294 }
    295 
    296 static struct xenbus_device *
    297 xenbus_lookup_device_path(const char *path)
    298 {
    299 	struct xenbus_device *xbusd;
    300 
    301 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    302 		if (strcmp(xbusd->xbusd_path, path) == 0)
    303 			return xbusd;
    304 	}
    305 	return NULL;
    306 }
    307 
    308 static int
    309 xenbus_probe_device_type(const char *path, const char *type,
    310     int (*create)(struct xenbus_device *))
    311 {
    312 	int err, i, pos, msize;
    313 	int *lookup = NULL;
    314 	size_t lookup_sz = 0;
    315 	unsigned long state;
    316 	char **dir;
    317 	unsigned int orig_dir_n = 0, dir_n;
    318 	struct xenbus_device *xbusd;
    319 	struct xenbusdev_attach_args xa;
    320 	char *ep;
    321 
    322 	DPRINTK("probe %s type %s", path, type);
    323 	err = xenbus_directory(NULL, path, "", &orig_dir_n, &dir);
    324 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    325 	if (err)
    326 		return err;
    327 	dir_n = orig_dir_n;
    328 
    329 	/* Only sort frontend devices i.e. create == NULL*/
    330 	if (dir_n > 1 && create == NULL) {
    331 		int minp;
    332 		unsigned long minv;
    333 		unsigned long *id;
    334 		size_t id_sz;
    335 
    336 		lookup_sz = sizeof(int) * dir_n;
    337 		lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
    338 
    339 		id_sz = sizeof(unsigned long) * dir_n;
    340 		id = kmem_zalloc(id_sz, KM_SLEEP);
    341 
    342 		/* Convert string values to numeric; skip invalid */
    343 		for (i = 0; i < dir_n; i++) {
    344 			/*
    345 			 * Add one to differentiate numerical zero from invalid
    346 			 * string. Has no effect on sort order.
    347 			 */
    348 			id[i] = strtoul(dir[i], &ep, 10) + 1;
    349 			if (dir[i][0] == '\0' || *ep != '\0')
    350 				id[i] = 0;
    351 		}
    352 
    353 		/* Build lookup table in ascending order */
    354 		for (pos = 0; pos < dir_n; ) {
    355 			minv = UINT32_MAX;
    356 			minp = -1;
    357 			for (i = 0; i < dir_n; i++) {
    358 				if (id[i] < minv && id[i] > 0) {
    359 					minv = id[i];
    360 					minp = i;
    361 				}
    362 			}
    363 			if (minp >= 0) {
    364 				lookup[pos++] = minp;
    365 				id[minp] = 0;
    366 			}
    367 			else
    368 				break;
    369 		}
    370 
    371 		kmem_free(id, id_sz);
    372 
    373 		/* Adjust in case we had to skip non-numeric entries */
    374 		dir_n = pos;
    375 	}
    376 
    377 	for (pos = 0; pos < dir_n; pos++) {
    378 		err = 0;
    379 		if (lookup)
    380 			i = lookup[pos];
    381 		else
    382 			i = pos;
    383 		/*
    384 		 * add size of path to size of xenbus_device. xenbus_device
    385 		 * already has room for one char in xbusd_path.
    386 		 */
    387 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
    388 		xbusd = kmem_zalloc(msize, KM_SLEEP);
    389 		xbusd->xbusd_sz = msize;
    390 
    391 		snprintf(__UNCONST(xbusd->xbusd_path),
    392 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
    393 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    394 			/* device already registered */
    395 			kmem_free(xbusd, xbusd->xbusd_sz);
    396 			continue;
    397 		}
    398 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    399 		    &state, 10);
    400 		if (err) {
    401 			printf("xenbus: can't get state "
    402 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    403 			kmem_free(xbusd, xbusd->xbusd_sz);
    404 			err = 0;
    405 			continue;
    406 		}
    407 		if (state != XenbusStateInitialising) {
    408 			/* device is not new */
    409 			kmem_free(xbusd, xbusd->xbusd_sz);
    410 			continue;
    411 		}
    412 
    413 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    414 		DPRINTK("xenbus_probe_device_type probe %s\n",
    415 		    xbusd->xbusd_path);
    416 		if (create != NULL) {
    417 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
    418 			err = read_frontend_details(xbusd);
    419 			if (err != 0) {
    420 				printf("xenbus: can't get frontend details "
    421 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    422 				break;
    423 			}
    424 			if (create(xbusd)) {
    425 				kmem_free(xbusd, xbusd->xbusd_sz);
    426 				continue;
    427 			}
    428 		} else {
    429 			xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
    430 			xa.xa_dmat = xenbus_dmat;
    431 			xa.xa_xbusd = xbusd;
    432 			xa.xa_type = type;
    433 			xa.xa_id = strtoul(dir[i], &ep, 0);
    434 			if (dir[i][0] == '\0' || *ep != '\0') {
    435 				printf("xenbus device type %s: id %s is not a"
    436 				    " number\n", type, dir[i]);
    437 				err = EFTYPE;
    438 				kmem_free(xbusd, xbusd->xbusd_sz);
    439 				break;
    440 			}
    441 			err = read_backend_details(xbusd);
    442 			if (err != 0) {
    443 				printf("xenbus: can't get backend details "
    444 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    445 				kmem_free(xbusd, xbusd->xbusd_sz);
    446 				break;
    447 			}
    448 			xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
    449 			    "xenbus", &xa, xenbus_print);
    450 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    451 				kmem_free(xbusd, xbusd->xbusd_sz);
    452 				continue;
    453 			}
    454 		}
    455 		SLIST_INSERT_HEAD(&xenbus_device_list,
    456 		    xbusd, xbusd_entries);
    457 		watch_otherend(xbusd);
    458 	}
    459 	xenbus_directory_free(orig_dir_n, dir);
    460 	if (lookup)
    461 		kmem_free(lookup, lookup_sz);
    462 
    463 	return err;
    464 }
    465 
    466 static int
    467 xenbus_print(void *aux, const char *pnp)
    468 {
    469 	struct xenbusdev_attach_args *xa = aux;
    470 
    471 	if (pnp) {
    472 		if (strcmp(xa->xa_type, "vbd") == 0)
    473 			aprint_normal("xbd");
    474 		else if (strcmp(xa->xa_type, "vif") == 0)
    475 			aprint_normal("xennet");
    476 		else if (strcmp(xa->xa_type, "balloon") == 0)
    477 			aprint_normal("balloon");
    478 		else
    479 			aprint_normal("unknown type %s", xa->xa_type);
    480 		aprint_normal(" at %s", pnp);
    481 	}
    482 	aprint_normal(" id %d", xa->xa_id);
    483 	return(UNCONF);
    484 }
    485 
    486 static int
    487 xenbus_probe_frontends(void)
    488 {
    489 	int err;
    490 	char **dir;
    491 	unsigned int i, dir_n;
    492 	char path[30];
    493 
    494 	DPRINTK("probe device");
    495 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    496 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    497 	if (err)
    498 		return err;
    499 
    500 	for (i = 0; i < dir_n; i++) {
    501 		/*
    502 		 * console is configured through xen_start_info when
    503 		 * xencons is attaching to hypervisor, so avoid console
    504 		 * probing when configuring xenbus devices
    505 		 */
    506 		if (strcmp(dir[i], "console") == 0)
    507 			continue;
    508 
    509 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    510 		err = xenbus_probe_device_type(path, dir[i], NULL);
    511 		if (err)
    512 			break;
    513 	}
    514 	xenbus_directory_free(dir_n, dir);
    515 	return err;
    516 }
    517 
    518 static int
    519 xenbus_probe_backends(void)
    520 {
    521 	int err;
    522 	char **dirt, **dirid;
    523 	unsigned int type, id, dirt_n, dirid_n;
    524 	char path[30];
    525 	struct xenbus_backend_driver *xbakd;
    526 
    527 	DPRINTK("probe backend");
    528 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    529 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    530 	if (err)
    531 		return err;
    532 
    533 	for (type = 0; type < dirt_n; type++) {
    534 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    535 		    xbakd_entries) {
    536 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    537 				break;
    538 		}
    539 		if (xbakd == NULL)
    540 			continue;
    541 		err = xenbus_directory(NULL, "backend", dirt[type],
    542 		    &dirid_n, &dirid);
    543 		DPRINTK("directory backend/%s err %d dirid_n %d",
    544 		    dirt[type], err, dirid_n);
    545 		if (err)
    546 			goto out;
    547 
    548 		for (id = 0; id < dirid_n; id++) {
    549 			snprintf(path, sizeof(path), "backend/%s/%s",
    550 			    dirt[type], dirid[id]);
    551 			err = xenbus_probe_device_type(path, dirt[type],
    552 			    xbakd->xbakd_create);
    553 			if (err)
    554 				break;
    555 		}
    556 		xenbus_directory_free(dirid_n, dirid);
    557 	}
    558 
    559 out:
    560 	xenbus_directory_free(dirt_n, dirt);
    561 	return err;
    562 }
    563 
    564 int
    565 xenbus_free_device(struct xenbus_device *xbusd)
    566 {
    567 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    568 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    569 	free_otherend_watch(xbusd);
    570 	free_otherend_details(xbusd);
    571 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    572 	kmem_free(xbusd, xbusd->xbusd_sz);
    573 	return 0;
    574 }
    575 
    576 static void
    577 frontend_changed(struct xenbus_watch *watch,
    578 			     const char **vec, unsigned int len)
    579 {
    580 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    581 	xenbus_probe_frontends();
    582 }
    583 
    584 static void
    585 backend_changed(struct xenbus_watch *watch,
    586 			    const char **vec, unsigned int len)
    587 {
    588 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    589 	xenbus_probe_backends();
    590 }
    591 
    592 
    593 /* We watch for devices appearing and vanishing. */
    594 static struct xenbus_watch fe_watch;
    595 
    596 static struct xenbus_watch be_watch;
    597 
    598 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    599 int xenstored_ready = 0;
    600 
    601 void
    602 xenbus_probe(void *unused)
    603 {
    604 	struct xenbusdev_attach_args balloon_xa = {
    605 		.xa_id = 0,
    606 		.xa_type = "balloon"
    607 	};
    608 
    609 	KASSERT((xenstored_ready > 0));
    610 
    611 	/* Enumerate devices in xenstore. */
    612 	xenbus_probe_frontends();
    613 	xenbus_probe_backends();
    614 
    615 	/* Watch for changes. */
    616 	fe_watch.node_sz = strlen("device") + 1;
    617 	fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
    618 	strcpy(fe_watch.node, "device");
    619 	fe_watch.xbw_callback = frontend_changed;
    620 	register_xenbus_watch(&fe_watch);
    621 
    622 	be_watch.node_sz = strlen("backend") + 1;
    623 	be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
    624 	strcpy(be_watch.node, "backend");
    625 	be_watch.xbw_callback = backend_changed;
    626 	register_xenbus_watch(&be_watch);
    627 
    628 	/* attach balloon. */
    629 	config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
    630 
    631 	shutdown_xenbus_setup();
    632 
    633 	/* Notify others that xenstore is up */
    634 	//notifier_call_chain(&xenstore_chain, 0, NULL);
    635 }
    636 
    637 static void
    638 xenbus_probe_init(void *unused)
    639 {
    640 	int err = 0;
    641 	bool dom0;
    642 	vaddr_t page = 0;
    643 
    644 	DPRINTK("");
    645 
    646 	SLIST_INIT(&xenbus_device_list);
    647 
    648 	/*
    649 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
    650 	*/
    651 	dom0 = xendomain_is_dom0();
    652 	if (dom0) {
    653 #if defined(DOM0OPS)
    654 		paddr_t ma;
    655 		evtchn_op_t op = { .cmd = 0 };
    656 
    657 		/* Allocate page. */
    658 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    659 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
    660 		if (!page)
    661 			panic("can't get xenstore page");
    662 
    663 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
    664 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
    665 		xenstore_interface = (void *)page;
    666 
    667 		/* Next allocate a local port which xenstored can bind to */
    668 		op.cmd = EVTCHNOP_alloc_unbound;
    669 		op.u.alloc_unbound.dom        = DOMID_SELF;
    670 		op.u.alloc_unbound.remote_dom = 0;
    671 
    672 		err = HYPERVISOR_event_channel_op(&op);
    673 		if (err) {
    674 			aprint_error_dev(xenbus_dev,
    675 				"can't register xenstore event\n");
    676 			goto err0;
    677 		}
    678 
    679 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
    680 
    681 		DELAY(1000);
    682 #else /* DOM0OPS */
    683 		kthread_exit(0); /* can't get a working xenstore in this case */
    684 #endif /* DOM0OPS */
    685 	}
    686 
    687 	/* Publish xenbus and Xenstore info in /kern/xen */
    688 	xenbus_kernfs_init();
    689 
    690 	/* register event handler */
    691 	xb_init_comms(xenbus_dev);
    692 
    693 	/* Initialize the interface to xenstore. */
    694 	err = xs_init(xenbus_dev);
    695 	if (err) {
    696 		aprint_error_dev(xenbus_dev,
    697 		    "Error initializing xenstore comms: %i\n", err);
    698 		goto err0;
    699 	}
    700 
    701 	if (!dom0) {
    702 		xenstored_ready = 1;
    703 		xenbus_probe(NULL);
    704 	}
    705 
    706 	DPRINTK("done");
    707 	config_pending_decr(xenbus_dev);
    708 #ifdef DOM0OPS
    709 	if (dom0) {
    710 		int s;
    711 		s = spltty();
    712 		while (xenstored_ready == 0) {
    713 			tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
    714 			xenbus_probe(NULL);
    715 		}
    716 		splx(s);
    717 	}
    718 #endif
    719 	kthread_exit(0);
    720 
    721 err0:
    722 	if (page)
    723 		uvm_km_free(kernel_map, page, PAGE_SIZE,
    724 				UVM_KMF_ZERO | UVM_KMF_WIRED);
    725 	kthread_exit(err);
    726 }
    727 
    728 /*
    729  * Local variables:
    730  *  c-file-style: "linux"
    731  *  indent-tabs-mode: t
    732  *  c-indent-level: 8
    733  *  c-basic-offset: 8
    734  *  tab-width: 8
    735  * End:
    736  */
    737