Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.52
      1 /* $NetBSD: xenbus_probe.c,v 1.52 2020/05/06 16:50:13 bouyer 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.52 2020/05/06 16:50:13 bouyer 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 <sys/mutex.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 static int  xenbus_match(device_t, cfdata_t, void *);
     60 static void xenbus_attach(device_t, device_t, void *);
     61 static int  xenbus_print(void *, const char *);
     62 
     63 /* power management, for save/restore */
     64 static bool xenbus_suspend(device_t, const pmf_qual_t *);
     65 static bool xenbus_resume(device_t, const pmf_qual_t *);
     66 
     67 /* routines gathering device information from XenStore */
     68 static int  read_otherend_details(struct xenbus_device *,
     69 		const char *, const char *);
     70 static int  read_backend_details (struct xenbus_device *);
     71 static int  read_frontend_details(struct xenbus_device *);
     72 static void free_otherend_details(struct xenbus_device *);
     73 
     74 static int  watch_otherend     (struct xenbus_device *);
     75 static void free_otherend_watch(struct xenbus_device *);
     76 
     77 static void xenbus_probe_init(void *);
     78 
     79 static struct xenbus_device *xenbus_lookup_device_path(const char *);
     80 
     81 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
     82     NULL, NULL);
     83 
     84 device_t xenbus_dev;
     85 bus_dma_tag_t xenbus_dmat;
     86 
     87 SLIST_HEAD(, xenbus_device) xenbus_device_list;
     88 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
     89 	SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
     90 
     91 int
     92 xenbus_match(device_t parent, cfdata_t match, void *aux)
     93 {
     94 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
     95 
     96 	if (strcmp(xa->xa_device, "xenbus") == 0)
     97 		return 1;
     98 	return 0;
     99 }
    100 
    101 static void
    102 xenbus_attach(device_t parent, device_t self, void *aux)
    103 {
    104 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
    105 	int err;
    106 
    107 	aprint_normal(": Xen Virtual Bus Interface\n");
    108 	xenbus_dev = self;
    109 	xenbus_dmat = xa->xa_dmat;
    110 	config_pending_incr(self);
    111 
    112 	err = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    113 	    xenbus_probe_init, NULL, 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 		xenbus_unwatch_path(&dev->xbusd_otherend_watch);
    237 }
    238 
    239 static void
    240 otherend_changed(struct xenbus_watch *watch,
    241 			     const char **vec, unsigned int len)
    242 {
    243 	struct xenbus_device *xdev = watch->xbw_dev;
    244 	XenbusState state;
    245 
    246 	/* Protect us against watches firing on old details when the otherend
    247 	   details change, say immediately after a resume. */
    248 	if (!xdev->xbusd_otherend ||
    249 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    250 		    strlen(xdev->xbusd_otherend))) {
    251 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    252 		return;
    253 	}
    254 
    255 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    256 
    257 	DPRINTK("state is %d, %s, %s",
    258 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    259 	if (state == XenbusStateClosed) {
    260 		int error;
    261 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
    262 			error = xdev->xbusd_u.b.b_detach(
    263 			    xdev->xbusd_u.b.b_cookie);
    264 			if (error) {
    265 				printf("could not detach %s: %d\n",
    266 				    xdev->xbusd_path, error);
    267 				return;
    268 			}
    269 		} else {
    270 			error = config_detach(xdev->xbusd_u.f.f_dev,
    271 			    DETACH_FORCE);
    272 			if (error) {
    273 				printf("could not detach %s: %d\n",
    274 				    device_xname(xdev->xbusd_u.f.f_dev), error);
    275 				return;
    276 			}
    277 		}
    278 		xenbus_free_device(xdev);
    279 		return;
    280 	}
    281 	if (xdev->xbusd_otherend_changed)
    282 		xdev->xbusd_otherend_changed(
    283 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
    284 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
    285 }
    286 
    287 static int
    288 watch_otherend(struct xenbus_device *dev)
    289 {
    290 	free_otherend_watch(dev);
    291 
    292 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    293 				  &dev->xbusd_otherend_watch,
    294 				  otherend_changed);
    295 }
    296 
    297 static struct xenbus_device *
    298 xenbus_lookup_device_path(const char *path)
    299 {
    300 	struct xenbus_device *xbusd;
    301 
    302 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    303 		if (strcmp(xbusd->xbusd_path, path) == 0)
    304 			return xbusd;
    305 	}
    306 	return NULL;
    307 }
    308 
    309 static int
    310 xenbus_probe_device_type(const char *path, const char *type,
    311     int (*create)(struct xenbus_device *))
    312 {
    313 	int err, i, pos, msize;
    314 	int *lookup = NULL;
    315 	size_t lookup_sz = 0;
    316 	unsigned long state;
    317 	char **dir;
    318 	unsigned int orig_dir_n = 0, dir_n;
    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, "", &orig_dir_n, &dir);
    325 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    326 	if (err)
    327 		return err;
    328 	dir_n = orig_dir_n;
    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 		size_t id_sz;
    336 
    337 		lookup_sz = sizeof(int) * dir_n;
    338 		lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
    339 
    340 		id_sz = sizeof(unsigned long) * dir_n;
    341 		id = kmem_zalloc(id_sz, KM_SLEEP);
    342 
    343 		/* Convert string values to numeric; skip invalid */
    344 		for (i = 0; i < dir_n; i++) {
    345 			/*
    346 			 * Add one to differentiate numerical zero from invalid
    347 			 * string. Has no effect on sort order.
    348 			 */
    349 			id[i] = strtoul(dir[i], &ep, 10) + 1;
    350 			if (dir[i][0] == '\0' || *ep != '\0')
    351 				id[i] = 0;
    352 		}
    353 
    354 		/* Build lookup table in ascending order */
    355 		for (pos = 0; pos < dir_n; ) {
    356 			minv = UINT32_MAX;
    357 			minp = -1;
    358 			for (i = 0; i < dir_n; i++) {
    359 				if (id[i] < minv && id[i] > 0) {
    360 					minv = id[i];
    361 					minp = i;
    362 				}
    363 			}
    364 			if (minp >= 0) {
    365 				lookup[pos++] = minp;
    366 				id[minp] = 0;
    367 			}
    368 			else
    369 				break;
    370 		}
    371 
    372 		kmem_free(id, id_sz);
    373 
    374 		/* Adjust in case we had to skip non-numeric entries */
    375 		dir_n = pos;
    376 	}
    377 
    378 	for (pos = 0; pos < dir_n; pos++) {
    379 		err = 0;
    380 		if (lookup)
    381 			i = lookup[pos];
    382 		else
    383 			i = pos;
    384 		/*
    385 		 * add size of path to size of xenbus_device. xenbus_device
    386 		 * already has room for one char in xbusd_path.
    387 		 */
    388 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
    389 		xbusd = kmem_zalloc(msize, KM_SLEEP);
    390 		xbusd->xbusd_sz = msize;
    391 		xbusd->xbusd_dmat = xenbus_dmat;
    392 
    393 		snprintf(__UNCONST(xbusd->xbusd_path),
    394 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
    395 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    396 			/* device already registered */
    397 			kmem_free(xbusd, xbusd->xbusd_sz);
    398 			continue;
    399 		}
    400 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    401 		    &state, 10);
    402 		if (err) {
    403 			aprint_error_dev(xenbus_dev, "can't get state "
    404 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    405 			kmem_free(xbusd, xbusd->xbusd_sz);
    406 			err = 0;
    407 			continue;
    408 		}
    409 		if (state != XenbusStateInitialising) {
    410 			/* device is not new */
    411 			kmem_free(xbusd, xbusd->xbusd_sz);
    412 			continue;
    413 		}
    414 
    415 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    416 		DPRINTK("xenbus_probe_device_type probe %s\n",
    417 		    xbusd->xbusd_path);
    418 		if (create != NULL) {
    419 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
    420 			err = read_frontend_details(xbusd);
    421 			if (err != 0) {
    422 				aprint_error_dev(xenbus_dev,
    423 				    "can't get frontend details for %s (%d)\n",
    424 				    xbusd->xbusd_path, err);
    425 				break;
    426 			}
    427 			if (create(xbusd)) {
    428 				kmem_free(xbusd, xbusd->xbusd_sz);
    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 				aprint_error_dev(xenbus_dev,
    438 				    "device type %s: id %s is not a number\n",
    439 				    type, dir[i]);
    440 				err = EFTYPE;
    441 				kmem_free(xbusd, xbusd->xbusd_sz);
    442 				break;
    443 			}
    444 			if (strcmp(xa.xa_type, "vbd") == 0) {
    445 				char dtype[10];
    446 				if (xenbus_read(NULL, xbusd->xbusd_path,
    447 				    "device-type", dtype, sizeof(dtype)) !=0) {
    448 					aprint_error_dev(xenbus_dev,
    449 					    "%s: can't read device-type\n",
    450 					    xbusd->xbusd_path);
    451 					kmem_free(xbusd, xbusd->xbusd_sz);
    452 					break;
    453 				}
    454 				if (strcmp(dtype, "cdrom") == 0) {
    455 					aprint_verbose_dev(xenbus_dev,
    456 					    "ignoring %s type cdrom\n",
    457 					    xbusd->xbusd_path);
    458 					kmem_free(xbusd, xbusd->xbusd_sz);
    459 					continue;
    460 				}
    461 			}
    462 			err = read_backend_details(xbusd);
    463 			if (err != 0) {
    464 				aprint_error_dev(xenbus_dev,
    465 				    "can't get backend details for %s (%d)\n",
    466 				    xbusd->xbusd_path, err);
    467 				kmem_free(xbusd, xbusd->xbusd_sz);
    468 				break;
    469 			}
    470 			xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
    471 			    "xenbus", &xa, xenbus_print);
    472 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    473 				kmem_free(xbusd, xbusd->xbusd_sz);
    474 				continue;
    475 			}
    476 		}
    477 		SLIST_INSERT_HEAD(&xenbus_device_list,
    478 		    xbusd, xbusd_entries);
    479 		watch_otherend(xbusd);
    480 	}
    481 	xenbus_directory_free(orig_dir_n, dir);
    482 	if (lookup)
    483 		kmem_free(lookup, lookup_sz);
    484 
    485 	return err;
    486 }
    487 
    488 static int
    489 xenbus_print(void *aux, const char *pnp)
    490 {
    491 	struct xenbusdev_attach_args *xa = aux;
    492 
    493 	if (pnp) {
    494 		if (strcmp(xa->xa_type, "vbd") == 0)
    495 			aprint_normal("xbd");
    496 		else if (strcmp(xa->xa_type, "vif") == 0)
    497 			aprint_normal("xennet");
    498 		else if (strcmp(xa->xa_type, "balloon") == 0)
    499 			aprint_normal("balloon");
    500 		else
    501 			aprint_normal("unknown type %s", xa->xa_type);
    502 		aprint_normal(" at %s", pnp);
    503 	}
    504 	aprint_normal(" id %d", xa->xa_id);
    505 	return(UNCONF);
    506 }
    507 
    508 static int
    509 xenbus_probe_frontends(void)
    510 {
    511 	int err;
    512 	char **dir;
    513 	unsigned int i, dir_n;
    514 	char path[30];
    515 
    516 	DPRINTK("probe device");
    517 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    518 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    519 	if (err)
    520 		return err;
    521 
    522 	for (i = 0; i < dir_n; i++) {
    523 		/*
    524 		 * console is configured through xen_start_info when
    525 		 * xencons is attaching to hypervisor, so avoid console
    526 		 * probing when configuring xenbus devices
    527 		 */
    528 		if (strcmp(dir[i], "console") == 0)
    529 			continue;
    530 
    531 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    532 		err = xenbus_probe_device_type(path, dir[i], NULL);
    533 		if (err)
    534 			break;
    535 	}
    536 	xenbus_directory_free(dir_n, dir);
    537 	return err;
    538 }
    539 
    540 static int
    541 xenbus_probe_backends(void)
    542 {
    543 	int err;
    544 	char **dirt, **dirid;
    545 	unsigned int type, id, dirt_n, dirid_n;
    546 	char path[30];
    547 	struct xenbus_backend_driver *xbakd;
    548 
    549 	DPRINTK("probe backend");
    550 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    551 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    552 	if (err)
    553 		return err;
    554 
    555 	for (type = 0; type < dirt_n; type++) {
    556 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    557 		    xbakd_entries) {
    558 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    559 				break;
    560 		}
    561 		if (xbakd == NULL)
    562 			continue;
    563 		err = xenbus_directory(NULL, "backend", dirt[type],
    564 		    &dirid_n, &dirid);
    565 		DPRINTK("directory backend/%s err %d dirid_n %d",
    566 		    dirt[type], err, dirid_n);
    567 		if (err)
    568 			goto out;
    569 
    570 		for (id = 0; id < dirid_n; id++) {
    571 			snprintf(path, sizeof(path), "backend/%s/%s",
    572 			    dirt[type], dirid[id]);
    573 			err = xenbus_probe_device_type(path, dirt[type],
    574 			    xbakd->xbakd_create);
    575 			if (err)
    576 				break;
    577 		}
    578 		xenbus_directory_free(dirid_n, dirid);
    579 	}
    580 
    581 out:
    582 	xenbus_directory_free(dirt_n, dirt);
    583 	return err;
    584 }
    585 
    586 int
    587 xenbus_free_device(struct xenbus_device *xbusd)
    588 {
    589 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    590 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    591 	free_otherend_watch(xbusd);
    592 	free_otherend_details(xbusd);
    593 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    594 	kmem_free(xbusd, xbusd->xbusd_sz);
    595 	return 0;
    596 }
    597 
    598 static void
    599 frontend_changed(struct xenbus_watch *watch,
    600 			     const char **vec, unsigned int len)
    601 {
    602 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    603 	xenbus_probe_frontends();
    604 }
    605 
    606 static void
    607 backend_changed(struct xenbus_watch *watch,
    608 			    const char **vec, unsigned int len)
    609 {
    610 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    611 	xenbus_probe_backends();
    612 }
    613 
    614 
    615 /* We watch for devices appearing and vanishing. */
    616 static struct xenbus_watch fe_watch;
    617 
    618 static struct xenbus_watch be_watch;
    619 
    620 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    621 int xenstored_ready = 0;
    622 static kmutex_t xenstored_lock;
    623 static kcondvar_t xenstored_cv;
    624 
    625 void
    626 xenbus_probe(void *unused)
    627 {
    628 	struct xenbusdev_attach_args balloon_xa = {
    629 		.xa_id = 0,
    630 		.xa_type = "balloon"
    631 	};
    632 
    633 	KASSERT((xenstored_ready > 0));
    634 
    635 	/* Enumerate devices in xenstore. */
    636 	xenbus_probe_frontends();
    637 	xenbus_probe_backends();
    638 
    639 	/* Watch for changes. */
    640 	fe_watch.node_sz = strlen("device") + 1;
    641 	fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
    642 	strcpy(fe_watch.node, "device");
    643 	fe_watch.xbw_callback = frontend_changed;
    644 	register_xenbus_watch(&fe_watch);
    645 
    646 	be_watch.node_sz = strlen("backend") + 1;
    647 	be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
    648 	strcpy(be_watch.node, "backend");
    649 	be_watch.xbw_callback = backend_changed;
    650 	register_xenbus_watch(&be_watch);
    651 
    652 	/* attach balloon. */
    653 	config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
    654 
    655 	shutdown_xenbus_setup();
    656 
    657 	/* Notify others that xenstore is up */
    658 	//notifier_call_chain(&xenstore_chain, 0, NULL);
    659 }
    660 
    661 void
    662 xb_xenstored_make_ready(void)
    663 {
    664 	mutex_enter(&xenstored_lock);
    665 	xenstored_ready = 1;
    666 	cv_broadcast(&xenstored_cv);
    667 	mutex_exit(&xenstored_lock);
    668 }
    669 
    670 static void
    671 xenbus_probe_init(void *unused)
    672 {
    673 	int err = 0;
    674 	bool dom0;
    675 	vaddr_t page = 0;
    676 
    677 	DPRINTK("");
    678 
    679 	SLIST_INIT(&xenbus_device_list);
    680 	mutex_init(&xenstored_lock, MUTEX_DEFAULT, IPL_TTY);
    681 	cv_init(&xenstored_cv, "xsready");
    682 
    683 	/*
    684 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
    685 	*/
    686 	dom0 = xendomain_is_dom0();
    687 	if (dom0) {
    688 #if defined(DOM0OPS)
    689 		paddr_t ma;
    690 		evtchn_op_t op = { .cmd = 0 };
    691 
    692 		/* Allocate page. */
    693 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    694 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
    695 		if (!page)
    696 			panic("can't get xenstore page");
    697 
    698 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
    699 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
    700 		xenstore_interface = (void *)page;
    701 
    702 		/* Next allocate a local port which xenstored can bind to */
    703 		op.cmd = EVTCHNOP_alloc_unbound;
    704 		op.u.alloc_unbound.dom        = DOMID_SELF;
    705 		op.u.alloc_unbound.remote_dom = 0;
    706 
    707 		err = HYPERVISOR_event_channel_op(&op);
    708 		if (err) {
    709 			aprint_error_dev(xenbus_dev,
    710 				"can't register xenstore event\n");
    711 			goto err0;
    712 		}
    713 
    714 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
    715 
    716 		DELAY(1000);
    717 #else /* DOM0OPS */
    718 		kthread_exit(0); /* can't get a working xenstore in this case */
    719 #endif /* DOM0OPS */
    720 	}
    721 
    722 	/* Publish xenbus and Xenstore info in /kern/xen */
    723 	xenbus_kernfs_init();
    724 
    725 	/* register event handler */
    726 	xb_init_comms(xenbus_dev);
    727 
    728 	/* Initialize the interface to xenstore. */
    729 	err = xs_init(xenbus_dev);
    730 	if (err) {
    731 		aprint_error_dev(xenbus_dev,
    732 		    "Error initializing xenstore comms: %i\n", err);
    733 		goto err0;
    734 	}
    735 
    736 	if (!dom0) {
    737 		xenstored_ready = 1;
    738 		xenbus_probe(NULL);
    739 	}
    740 
    741 	DPRINTK("done");
    742 	config_pending_decr(xenbus_dev);
    743 #ifdef DOM0OPS
    744 	if (dom0) {
    745 		mutex_enter(&xenstored_lock);
    746 		while (xenstored_ready == 0) {
    747 			cv_wait(&xenstored_cv, &xenstored_lock);
    748 			mutex_exit(&xenstored_lock);
    749 			xenbus_probe(NULL);
    750 			mutex_enter(&xenstored_lock);
    751 		}
    752 		mutex_exit(&xenstored_lock);
    753 	}
    754 #endif
    755 	kthread_exit(0);
    756 
    757 err0:
    758 	if (page)
    759 		uvm_km_free(kernel_map, page, PAGE_SIZE,
    760 				UVM_KMF_ZERO | UVM_KMF_WIRED);
    761 	kthread_exit(err);
    762 }
    763 
    764 /*
    765  * Local variables:
    766  *  c-file-style: "linux"
    767  *  indent-tabs-mode: t
    768  *  c-indent-level: 8
    769  *  c-basic-offset: 8
    770  *  tab-width: 8
    771  * End:
    772  */
    773