Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.42
      1 /* $NetBSD: xenbus_probe.c,v 1.42 2020/04/07 11:47:06 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.42 2020/04/07 11:47:06 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 		dev->xbusd_otherend_watch.node = NULL;
    238 	}
    239 }
    240 
    241 static void
    242 otherend_changed(struct xenbus_watch *watch,
    243 			     const char **vec, unsigned int len)
    244 {
    245 	struct xenbus_device *xdev = watch->xbw_dev;
    246 	XenbusState state;
    247 
    248 	/* Protect us against watches firing on old details when the otherend
    249 	   details change, say immediately after a resume. */
    250 	if (!xdev->xbusd_otherend ||
    251 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    252 		    strlen(xdev->xbusd_otherend))) {
    253 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    254 		return;
    255 	}
    256 
    257 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    258 
    259 	DPRINTK("state is %d, %s, %s",
    260 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    261 	if (state == XenbusStateClosed) {
    262 		int error;
    263 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
    264 			error = xdev->xbusd_u.b.b_detach(
    265 			    xdev->xbusd_u.b.b_cookie);
    266 			if (error) {
    267 				printf("could not detach %s: %d\n",
    268 				    xdev->xbusd_path, error);
    269 				return;
    270 			}
    271 		} else {
    272 			error = config_detach(xdev->xbusd_u.f.f_dev,
    273 			    DETACH_FORCE);
    274 			if (error) {
    275 				printf("could not detach %s: %d\n",
    276 				    device_xname(xdev->xbusd_u.f.f_dev), error);
    277 				return;
    278 			}
    279 		}
    280 		xenbus_free_device(xdev);
    281 		return;
    282 	}
    283 	if (xdev->xbusd_otherend_changed)
    284 		xdev->xbusd_otherend_changed(
    285 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
    286 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
    287 }
    288 
    289 static int
    290 watch_otherend(struct xenbus_device *dev)
    291 {
    292 	free_otherend_watch(dev);
    293 
    294 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    295 				  &dev->xbusd_otherend_watch,
    296 				  otherend_changed);
    297 }
    298 
    299 static struct xenbus_device *
    300 xenbus_lookup_device_path(const char *path)
    301 {
    302 	struct xenbus_device *xbusd;
    303 
    304 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    305 		if (strcmp(xbusd->xbusd_path, path) == 0)
    306 			return xbusd;
    307 	}
    308 	return NULL;
    309 }
    310 
    311 static int
    312 xenbus_probe_device_type(const char *path, const char *type,
    313     int (*create)(struct xenbus_device *))
    314 {
    315 	int err, i, pos, msize;
    316 	int *lookup = NULL;
    317 	unsigned long state;
    318 	char **dir;
    319 	unsigned int dir_n = 0;
    320 	struct xenbus_device *xbusd;
    321 	struct xenbusdev_attach_args xa;
    322 	char *ep;
    323 
    324 	DPRINTK("probe %s type %s", path, type);
    325 	err = xenbus_directory(NULL, path, "", &dir_n, &dir);
    326 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    327 	if (err)
    328 		return err;
    329 
    330 	/* Only sort frontend devices i.e. create == NULL*/
    331 	if (dir_n > 1 && create == NULL) {
    332 		int minp;
    333 		unsigned long minv;
    334 		unsigned long *id;
    335 
    336 		lookup = malloc(sizeof(int) * dir_n, M_DEVBUF,
    337 		    M_WAITOK | M_ZERO);
    338 		if (lookup == NULL)
    339 			panic("can't malloc lookup");
    340 
    341 		id = malloc(sizeof(unsigned long) * dir_n, M_DEVBUF,
    342 		    M_WAITOK | M_ZERO);
    343 		if (id == NULL)
    344 			panic("can't malloc id");
    345 
    346 		/* Convert string values to numeric; skip invalid */
    347 		for (i = 0; i < dir_n; i++) {
    348 			/*
    349 			 * Add one to differentiate numerical zero from invalid
    350 			 * string. Has no effect on sort order.
    351 			 */
    352 			id[i] = strtoul(dir[i], &ep, 10) + 1;
    353 			if (dir[i][0] == '\0' || *ep != '\0')
    354 				id[i] = 0;
    355 		}
    356 
    357 		/* Build lookup table in ascending order */
    358 		for (pos = 0; pos < dir_n; ) {
    359 			minv = UINT32_MAX;
    360 			minp = -1;
    361 			for (i = 0; i < dir_n; i++) {
    362 				if (id[i] < minv && id[i] > 0) {
    363 					minv = id[i];
    364 					minp = i;
    365 				}
    366 			}
    367 			if (minp >= 0) {
    368 				lookup[pos++] = minp;
    369 				id[minp] = 0;
    370 			}
    371 			else
    372 				break;
    373 		}
    374 
    375 		free(id, M_DEVBUF);
    376 		/* Adjust in case we had to skip non-numeric entries */
    377 		dir_n = pos;
    378 	}
    379 
    380 	for (pos = 0; pos < dir_n; pos++) {
    381 		err = 0;
    382 		if (lookup)
    383 			i = lookup[pos];
    384 		else
    385 			i = pos;
    386 		/*
    387 		 * add size of path to size of xenbus_device. xenbus_device
    388 		 * already has room for one char in xbusd_path.
    389 		 */
    390 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
    391 		xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
    392 		if (xbusd == NULL)
    393 			panic("can't malloc xbusd");
    394 
    395 		snprintf(__UNCONST(xbusd->xbusd_path),
    396 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
    397 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    398 			/* device already registered */
    399 			free(xbusd, M_DEVBUF);
    400 			continue;
    401 		}
    402 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    403 		    &state, 10);
    404 		if (err) {
    405 			printf("xenbus: can't get state "
    406 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    407 			free(xbusd, M_DEVBUF);
    408 			err = 0;
    409 			continue;
    410 		}
    411 		if (state != XenbusStateInitialising) {
    412 			/* device is not new */
    413 			free(xbusd, M_DEVBUF);
    414 			continue;
    415 		}
    416 
    417 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    418 		DPRINTK("xenbus_probe_device_type probe %s\n",
    419 		    xbusd->xbusd_path);
    420 		if (create != NULL) {
    421 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
    422 			err = read_frontend_details(xbusd);
    423 			if (err != 0) {
    424 				printf("xenbus: can't get frontend details "
    425 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    426 				break;
    427 			}
    428 			if (create(xbusd)) {
    429 				free(xbusd, M_DEVBUF);
    430 				continue;
    431 			}
    432 		} else {
    433 			xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
    434 			xa.xa_xbusd = xbusd;
    435 			xa.xa_type = type;
    436 			xa.xa_id = strtoul(dir[i], &ep, 0);
    437 			if (dir[i][0] == '\0' || *ep != '\0') {
    438 				printf("xenbus device type %s: id %s is not a"
    439 				    " number\n", type, dir[i]);
    440 				err = EFTYPE;
    441 				free(xbusd, M_DEVBUF);
    442 				break;
    443 			}
    444 			err = read_backend_details(xbusd);
    445 			if (err != 0) {
    446 				printf("xenbus: can't get backend details "
    447 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    448 				break;
    449 			}
    450 			xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
    451 			    "xenbus", &xa, xenbus_print);
    452 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    453 				free(xbusd, M_DEVBUF);
    454 				continue;
    455 			}
    456 		}
    457 		SLIST_INSERT_HEAD(&xenbus_device_list,
    458 		    xbusd, xbusd_entries);
    459 		watch_otherend(xbusd);
    460 	}
    461 	free(dir, M_DEVBUF);
    462 	if (lookup)
    463 		free(lookup, M_DEVBUF);
    464 
    465 	return err;
    466 }
    467 
    468 static int
    469 xenbus_print(void *aux, const char *pnp)
    470 {
    471 	struct xenbusdev_attach_args *xa = aux;
    472 
    473 	if (pnp) {
    474 		if (strcmp(xa->xa_type, "vbd") == 0)
    475 			aprint_normal("xbd");
    476 		else if (strcmp(xa->xa_type, "vif") == 0)
    477 			aprint_normal("xennet");
    478 		else if (strcmp(xa->xa_type, "balloon") == 0)
    479 			aprint_normal("balloon");
    480 		else
    481 			aprint_normal("unknown type %s", xa->xa_type);
    482 		aprint_normal(" at %s", pnp);
    483 	}
    484 	aprint_normal(" id %d", xa->xa_id);
    485 	return(UNCONF);
    486 }
    487 
    488 static int
    489 xenbus_probe_frontends(void)
    490 {
    491 	int err;
    492 	char **dir;
    493 	unsigned int i, dir_n;
    494 	char path[30];
    495 
    496 	DPRINTK("probe device");
    497 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    498 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    499 	if (err)
    500 		return err;
    501 
    502 	for (i = 0; i < dir_n; i++) {
    503 		/*
    504 		 * console is configured through xen_start_info when
    505 		 * xencons is attaching to hypervisor, so avoid console
    506 		 * probing when configuring xenbus devices
    507 		 */
    508 		if (strcmp(dir[i], "console") == 0)
    509 			continue;
    510 
    511 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    512 		err = xenbus_probe_device_type(path, dir[i], NULL);
    513 		if (err)
    514 			break;
    515 	}
    516 	free(dir, M_DEVBUF);
    517 	return err;
    518 }
    519 
    520 static int
    521 xenbus_probe_backends(void)
    522 {
    523 	int err;
    524 	char **dirt, **dirid;
    525 	unsigned int type, id, dirt_n, dirid_n;
    526 	char path[30];
    527 	struct xenbus_backend_driver *xbakd;
    528 
    529 	DPRINTK("probe backend");
    530 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    531 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    532 	if (err)
    533 		return err;
    534 
    535 	for (type = 0; type < dirt_n; type++) {
    536 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    537 		    xbakd_entries) {
    538 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    539 				break;
    540 		}
    541 		if (xbakd == NULL)
    542 			continue;
    543 		err = xenbus_directory(NULL, "backend", dirt[type],
    544 		    &dirid_n, &dirid);
    545 		DPRINTK("directory backend/%s err %d dirid_n %d",
    546 		    dirt[type], err, dirid_n);
    547 		if (err) {
    548 			free(dirt, M_DEVBUF); /* to be checked */
    549 			return err;
    550 		}
    551 		for (id = 0; id < dirid_n; id++) {
    552 			snprintf(path, sizeof(path), "backend/%s/%s",
    553 			    dirt[type], dirid[id]);
    554 			err = xenbus_probe_device_type(path, dirt[type],
    555 			    xbakd->xbakd_create);
    556 			if (err)
    557 				break;
    558 		}
    559 		free(dirid, M_DEVBUF);
    560 	}
    561 	free(dirt, M_DEVBUF);
    562 	return err;
    563 }
    564 
    565 int
    566 xenbus_free_device(struct xenbus_device *xbusd)
    567 {
    568 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    569 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    570 	free_otherend_watch(xbusd);
    571 	free_otherend_details(xbusd);
    572 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    573 	free(xbusd, M_DEVBUF);
    574 	return 0;
    575 }
    576 
    577 static void
    578 frontend_changed(struct xenbus_watch *watch,
    579 			     const char **vec, unsigned int len)
    580 {
    581 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    582 	xenbus_probe_frontends();
    583 }
    584 
    585 static void
    586 backend_changed(struct xenbus_watch *watch,
    587 			    const char **vec, unsigned int len)
    588 {
    589 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    590 	xenbus_probe_backends();
    591 }
    592 
    593 
    594 /* We watch for devices appearing and vanishing. */
    595 static struct xenbus_watch fe_watch;
    596 
    597 static struct xenbus_watch be_watch;
    598 
    599 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    600 int xenstored_ready = 0;
    601 
    602 void
    603 xenbus_probe(void *unused)
    604 {
    605 	struct xenbusdev_attach_args balloon_xa = {
    606 		.xa_id = 0,
    607 		.xa_type = "balloon"
    608 	};
    609 
    610 	KASSERT((xenstored_ready > 0));
    611 
    612 	/* Enumerate devices in xenstore. */
    613 	xenbus_probe_frontends();
    614 	xenbus_probe_backends();
    615 
    616 	/* Watch for changes. */
    617 	fe_watch.node = "device";
    618 	fe_watch.xbw_callback = frontend_changed;
    619 	register_xenbus_watch(&fe_watch);
    620 	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