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