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