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