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