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