Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.60
      1 /* $NetBSD: xenbus_probe.c,v 1.60 2023/10/17 11:52:45 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.60 2023/10/17 11:52:45 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 #include "kernfs.h"
     60 
     61 static int  xenbus_match(device_t, cfdata_t, void *);
     62 static void xenbus_attach(device_t, device_t, void *);
     63 static int  xenbus_print(void *, const char *);
     64 
     65 /* power management, for save/restore */
     66 static bool xenbus_suspend(device_t, const pmf_qual_t *);
     67 static bool xenbus_resume(device_t, const pmf_qual_t *);
     68 
     69 /* routines gathering device information from XenStore */
     70 static int  read_otherend_details(struct xenbus_device *,
     71 		const char *, const char *);
     72 static int  read_backend_details (struct xenbus_device *);
     73 static int  read_frontend_details(struct xenbus_device *);
     74 static void free_otherend_details(struct xenbus_device *);
     75 
     76 static int  watch_otherend     (struct xenbus_device *);
     77 static void free_otherend_watch(struct xenbus_device *);
     78 
     79 static void xenbus_probe_init(void *);
     80 
     81 static struct xenbus_device *xenbus_lookup_device_path(const char *);
     82 
     83 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
     84     NULL, NULL);
     85 
     86 device_t xenbus_dev;
     87 bus_dma_tag_t xenbus_dmat;
     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 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
    107 	int err;
    108 
    109 	aprint_normal(": Xen Virtual Bus Interface\n");
    110 	xenbus_dev = self;
    111 	xenbus_dmat = xa->xa_dmat;
    112 	config_pending_incr(self);
    113 
    114 	err = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    115 	    xenbus_probe_init, NULL, NULL, "xenbus_probe");
    116 	if (err)
    117 		aprint_error_dev(xenbus_dev,
    118 				"kthread_create(xenbus_probe): %d\n", err);
    119 
    120 	if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
    121 		aprint_error_dev(self, "couldn't establish power handler\n");
    122 }
    123 
    124 static bool
    125 xenbus_suspend(device_t dev, const pmf_qual_t *qual)
    126 {
    127 	xs_suspend();
    128 	xb_suspend_comms(dev);
    129 
    130 	return true;
    131 }
    132 
    133 static bool
    134 xenbus_resume(device_t dev, const pmf_qual_t *qual)
    135 {
    136 	xb_resume_comms(dev);
    137 	xs_resume();
    138 
    139 	return true;
    140 }
    141 
    142 /*
    143  * Suspend a xenbus device
    144  */
    145 bool
    146 xenbus_device_suspend(struct xenbus_device *dev) {
    147 
    148 	free_otherend_details(dev);
    149 	return true;
    150 }
    151 
    152 /*
    153  * Resume a xenbus device
    154  */
    155 bool
    156 xenbus_device_resume(struct xenbus_device *dev) {
    157 
    158 	if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
    159 		read_backend_details(dev);
    160 	}
    161 
    162 	return true;
    163 }
    164 
    165 void
    166 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
    167 {
    168 	SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
    169 }
    170 
    171 static int
    172 read_otherend_details(struct xenbus_device *xendev,
    173 				 const char *id_node, const char *path_node)
    174 {
    175 	int err;
    176 	unsigned long id;
    177 
    178 	err = xenbus_read_ul(NULL, xendev->xbusd_path, id_node, &id, 10);
    179 	if (err) {
    180 		printf("reading other end details %s from %s\n",
    181 		    id_node, xendev->xbusd_path);
    182 		xenbus_dev_fatal(xendev, err,
    183 				 "reading other end details %s from %s",
    184 				 id_node, xendev->xbusd_path);
    185 		return err;
    186 	}
    187 	xendev->xbusd_otherend_id = (int)id;
    188 
    189 	err = xenbus_read(NULL, xendev->xbusd_path, path_node,
    190 	    xendev->xbusd_otherend, sizeof(xendev->xbusd_otherend));
    191 	if (err) {
    192 		printf("reading other end details %s from %s (%d)\n",
    193 		    path_node, xendev->xbusd_path, err);
    194 		xenbus_dev_fatal(xendev, err,
    195 				 "reading other end details %s from %s",
    196 				 path_node, xendev->xbusd_path);
    197 		return err;
    198 	}
    199 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
    200 	    xendev->xbusd_path, path_node, xendev->xbusd_otherend);
    201 
    202 	if (strlen(xendev->xbusd_otherend) == 0 ||
    203 	    !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
    204 		printf("missing other end from %s\n", xendev->xbusd_path);
    205 		xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
    206 				 xendev->xbusd_path);
    207 		free_otherend_details(xendev);
    208 		return ENOENT;
    209 	}
    210 
    211 	return 0;
    212 }
    213 
    214 static int
    215 read_backend_details(struct xenbus_device *xendev)
    216 {
    217 	return read_otherend_details(xendev, "backend-id", "backend");
    218 }
    219 
    220 
    221 static int
    222 read_frontend_details(struct xenbus_device *xendev)
    223 {
    224 	return read_otherend_details(xendev, "frontend-id", "frontend");
    225 }
    226 
    227 static void
    228 free_otherend_details(struct xenbus_device *dev)
    229 {
    230 	/* Nothing to free */
    231 	dev->xbusd_otherend[0] = '\0';
    232 }
    233 
    234 static void
    235 free_otherend_watch(struct xenbus_device *dev)
    236 {
    237 	if (dev->xbusd_otherend_watch.node)
    238 		xenbus_unwatch_path(&dev->xbusd_otherend_watch);
    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 (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 	size_t lookup_sz = 0;
    317 	unsigned long state;
    318 	char **dir;
    319 	unsigned int orig_dir_n = 0, dir_n;
    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, "", &orig_dir_n, &dir);
    326 	DPRINTK("directory err %d dir_n %d", err, orig_dir_n);
    327 	if (err)
    328 		return err;
    329 	dir_n = orig_dir_n;
    330 
    331 	/* Only sort frontend devices i.e. create == NULL*/
    332 	if (dir_n > 1 && create == NULL) {
    333 		int minp;
    334 		unsigned long minv;
    335 		unsigned long *id;
    336 		size_t id_sz;
    337 
    338 		lookup_sz = sizeof(int) * dir_n;
    339 		lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
    340 
    341 		id_sz = sizeof(unsigned long) * dir_n;
    342 		id = kmem_zalloc(id_sz, KM_SLEEP);
    343 
    344 		/* Convert string values to numeric; skip invalid */
    345 		for (i = 0; i < dir_n; i++) {
    346 			/*
    347 			 * Add one to differentiate numerical zero from invalid
    348 			 * string. Has no effect on sort order.
    349 			 */
    350 			id[i] = strtoul(dir[i], &ep, 10) + 1;
    351 			if (dir[i][0] == '\0' || *ep != '\0')
    352 				id[i] = 0;
    353 		}
    354 
    355 		/* Build lookup table in ascending order */
    356 		for (pos = 0; pos < dir_n; ) {
    357 			minv = UINT32_MAX;
    358 			minp = -1;
    359 			for (i = 0; i < dir_n; i++) {
    360 				if (id[i] < minv && id[i] > 0) {
    361 					minv = id[i];
    362 					minp = i;
    363 				}
    364 			}
    365 			if (minp >= 0) {
    366 				lookup[pos++] = minp;
    367 				id[minp] = 0;
    368 			}
    369 			else
    370 				break;
    371 		}
    372 
    373 		kmem_free(id, id_sz);
    374 
    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 = kmem_zalloc(msize, KM_SLEEP);
    391 		xbusd->xbusd_sz = msize;
    392 		xbusd->xbusd_dmat = xenbus_dmat;
    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 			kmem_free(xbusd, xbusd->xbusd_sz);
    399 			continue;
    400 		}
    401 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    402 		    &state, 10);
    403 		if (err) {
    404 			aprint_error_dev(xenbus_dev, "can't get state "
    405 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    406 			kmem_free(xbusd, xbusd->xbusd_sz);
    407 			err = 0;
    408 			continue;
    409 		}
    410 		if (state != XenbusStateInitialising) {
    411 			/* device is not new */
    412 			kmem_free(xbusd, xbusd->xbusd_sz);
    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 				aprint_error_dev(xenbus_dev,
    424 				    "can't get frontend details for %s (%d)\n",
    425 				    xbusd->xbusd_path, err);
    426 				break;
    427 			}
    428 			if (create(xbusd)) {
    429 				kmem_free(xbusd, xbusd->xbusd_sz);
    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 				aprint_error_dev(xenbus_dev,
    439 				    "device type %s: id %s is not a number\n",
    440 				    type, dir[i]);
    441 				err = EFTYPE;
    442 				kmem_free(xbusd, xbusd->xbusd_sz);
    443 				break;
    444 			}
    445 			if (strcmp(xa.xa_type, "vbd") == 0) {
    446 				char dtype[10];
    447 				if (xenbus_read(NULL, xbusd->xbusd_path,
    448 				    "device-type", dtype, sizeof(dtype)) !=0) {
    449 					aprint_error_dev(xenbus_dev,
    450 					    "%s: can't read device-type\n",
    451 					    xbusd->xbusd_path);
    452 					kmem_free(xbusd, xbusd->xbusd_sz);
    453 					break;
    454 				}
    455 				if (strcmp(dtype, "cdrom") == 0) {
    456 					aprint_verbose_dev(xenbus_dev,
    457 					    "ignoring %s type cdrom\n",
    458 					    xbusd->xbusd_path);
    459 					kmem_free(xbusd, xbusd->xbusd_sz);
    460 					continue;
    461 				}
    462 			}
    463 			err = read_backend_details(xbusd);
    464 			if (err != 0) {
    465 				aprint_error_dev(xenbus_dev,
    466 				    "can't get backend details for %s (%d)\n",
    467 				    xbusd->xbusd_path, err);
    468 				kmem_free(xbusd, xbusd->xbusd_sz);
    469 				break;
    470 			}
    471 
    472 			KERNEL_LOCK(1, curlwp);
    473 			xbusd->xbusd_u.f.f_dev = config_found(xenbus_dev,
    474 			    &xa, xenbus_print, CFARGS_NONE);
    475 			KERNEL_UNLOCK_ONE(curlwp);
    476 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    477 				kmem_free(xbusd, xbusd->xbusd_sz);
    478 				continue;
    479 			}
    480 		}
    481 		SLIST_INSERT_HEAD(&xenbus_device_list,
    482 		    xbusd, xbusd_entries);
    483 		watch_otherend(xbusd);
    484 	}
    485 	xenbus_directory_free(orig_dir_n, dir);
    486 	if (lookup)
    487 		kmem_free(lookup, lookup_sz);
    488 
    489 	return err;
    490 }
    491 
    492 static int
    493 xenbus_print(void *aux, const char *pnp)
    494 {
    495 	struct xenbusdev_attach_args *xa = aux;
    496 
    497 	if (pnp) {
    498 		if (strcmp(xa->xa_type, "vbd") == 0)
    499 			aprint_normal("xbd");
    500 		else if (strcmp(xa->xa_type, "vif") == 0)
    501 			aprint_normal("xennet");
    502 		else if (strcmp(xa->xa_type, "balloon") == 0)
    503 			aprint_normal("balloon");
    504 		else
    505 			aprint_normal("unknown type %s", xa->xa_type);
    506 		aprint_normal(" at %s", pnp);
    507 	}
    508 	aprint_normal(" id %d", xa->xa_id);
    509 	return(UNCONF);
    510 }
    511 
    512 static int
    513 xenbus_probe_frontends(void)
    514 {
    515 	int err;
    516 	char **dir;
    517 	unsigned int i, dir_n;
    518 	char path[30];
    519 
    520 	DPRINTK("probe device");
    521 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    522 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    523 	if (err)
    524 		return err;
    525 
    526 	for (i = 0; i < dir_n; i++) {
    527 		/*
    528 		 * console is configured through xen_start_info when
    529 		 * xencons is attaching to hypervisor, so avoid console
    530 		 * probing when configuring xenbus devices
    531 		 */
    532 		if (strcmp(dir[i], "console") == 0)
    533 			continue;
    534 
    535 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    536 		err = xenbus_probe_device_type(path, dir[i], NULL);
    537 		if (err)
    538 			break;
    539 	}
    540 	xenbus_directory_free(dir_n, dir);
    541 	return err;
    542 }
    543 
    544 static int
    545 xenbus_probe_backends(void)
    546 {
    547 	int err;
    548 	char **dirt, **dirid;
    549 	unsigned int type, id, dirt_n, dirid_n;
    550 	char path[30];
    551 	struct xenbus_backend_driver *xbakd;
    552 
    553 	DPRINTK("probe backend");
    554 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    555 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    556 	if (err)
    557 		return err;
    558 
    559 	for (type = 0; type < dirt_n; type++) {
    560 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    561 		    xbakd_entries) {
    562 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    563 				break;
    564 		}
    565 		if (xbakd == NULL)
    566 			continue;
    567 		err = xenbus_directory(NULL, "backend", dirt[type],
    568 		    &dirid_n, &dirid);
    569 		DPRINTK("directory backend/%s err %d dirid_n %d",
    570 		    dirt[type], err, dirid_n);
    571 		if (err)
    572 			goto out;
    573 
    574 		for (id = 0; id < dirid_n; id++) {
    575 			snprintf(path, sizeof(path), "backend/%s/%s",
    576 			    dirt[type], dirid[id]);
    577 			err = xenbus_probe_device_type(path, dirt[type],
    578 			    xbakd->xbakd_create);
    579 			if (err)
    580 				break;
    581 		}
    582 		xenbus_directory_free(dirid_n, dirid);
    583 	}
    584 
    585 out:
    586 	xenbus_directory_free(dirt_n, dirt);
    587 	return err;
    588 }
    589 
    590 int
    591 xenbus_free_device(struct xenbus_device *xbusd)
    592 {
    593 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    594 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    595 	free_otherend_watch(xbusd);
    596 	free_otherend_details(xbusd);
    597 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    598 	kmem_free(xbusd, xbusd->xbusd_sz);
    599 	return 0;
    600 }
    601 
    602 static void
    603 frontend_changed(struct xenbus_watch *watch,
    604 			     const char **vec, unsigned int len)
    605 {
    606 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    607 	xenbus_probe_frontends();
    608 }
    609 
    610 static void
    611 backend_changed(struct xenbus_watch *watch,
    612 			    const char **vec, unsigned int len)
    613 {
    614 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    615 	xenbus_probe_backends();
    616 }
    617 
    618 
    619 /* We watch for devices appearing and vanishing. */
    620 static struct xenbus_watch fe_watch;
    621 
    622 static struct xenbus_watch be_watch;
    623 
    624 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    625 int xenstored_ready = 0;
    626 static kmutex_t xenstored_lock;
    627 static kcondvar_t xenstored_cv;
    628 
    629 void
    630 xenbus_probe(void *unused)
    631 {
    632 	struct xenbusdev_attach_args balloon_xa = {
    633 		.xa_id = 0,
    634 		.xa_type = "balloon"
    635 	};
    636 
    637 	KASSERT((xenstored_ready > 0));
    638 
    639 	/* Enumerate devices in xenstore. */
    640 	xenbus_probe_frontends();
    641 	xenbus_probe_backends();
    642 
    643 	/* Watch for changes. */
    644 	fe_watch.node_sz = strlen("device") + 1;
    645 	fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
    646 	strcpy(fe_watch.node, "device");
    647 	fe_watch.xbw_callback = frontend_changed;
    648 	register_xenbus_watch(&fe_watch);
    649 
    650 	be_watch.node_sz = strlen("backend") + 1;
    651 	be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
    652 	strcpy(be_watch.node, "backend");
    653 	be_watch.xbw_callback = backend_changed;
    654 	register_xenbus_watch(&be_watch);
    655 
    656 	/* attach balloon. */
    657 	KERNEL_LOCK(1, curlwp);
    658 	config_found(xenbus_dev, &balloon_xa, xenbus_print,
    659 	    CFARGS_NONE);
    660 	KERNEL_UNLOCK_ONE(curlwp);
    661 
    662 	shutdown_xenbus_setup();
    663 
    664 	/* Notify others that xenstore is up */
    665 	//notifier_call_chain(&xenstore_chain, 0, NULL);
    666 }
    667 
    668 void
    669 xb_xenstored_make_ready(void)
    670 {
    671 	mutex_enter(&xenstored_lock);
    672 	xenstored_ready = 1;
    673 	cv_broadcast(&xenstored_cv);
    674 	mutex_exit(&xenstored_lock);
    675 }
    676 
    677 static void
    678 xenbus_probe_init(void *unused)
    679 {
    680 	int err = 0;
    681 	bool dom0;
    682 	vaddr_t page = 0;
    683 
    684 	DPRINTK("");
    685 
    686 	SLIST_INIT(&xenbus_device_list);
    687 	mutex_init(&xenstored_lock, MUTEX_DEFAULT, IPL_TTY);
    688 	cv_init(&xenstored_cv, "xsready");
    689 
    690 	/*
    691 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
    692 	*/
    693 	dom0 = xendomain_is_dom0();
    694 	if (dom0) {
    695 #if defined(DOM0OPS)
    696 		paddr_t ma;
    697 		evtchn_op_t op = { .cmd = 0 };
    698 
    699 		/* Allocate page. */
    700 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    701 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
    702 		if (!page)
    703 			panic("can't get xenstore page");
    704 
    705 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
    706 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
    707 		xenstore_interface = (void *)page;
    708 
    709 		/* Next allocate a local port which xenstored can bind to */
    710 		op.cmd = EVTCHNOP_alloc_unbound;
    711 		op.u.alloc_unbound.dom        = DOMID_SELF;
    712 		op.u.alloc_unbound.remote_dom = 0;
    713 
    714 		err = HYPERVISOR_event_channel_op(&op);
    715 		if (err) {
    716 			aprint_error_dev(xenbus_dev,
    717 				"can't register xenstore event\n");
    718 			goto err0;
    719 		}
    720 
    721 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
    722 
    723 		DELAY(1000);
    724 #else /* DOM0OPS */
    725 		panic("dom0 support not compiled in");
    726 #endif /* DOM0OPS */
    727 	}
    728 
    729 #if NKERNFS > 0
    730 	/* Publish xenbus and Xenstore info in /kern/xen */
    731 	xenbus_kernfs_init();
    732 #endif
    733 
    734 	/* register event handler */
    735 	xb_init_comms(xenbus_dev);
    736 
    737 	/* Initialize the interface to xenstore. */
    738 	err = xs_init(xenbus_dev);
    739 	if (err) {
    740 		aprint_error_dev(xenbus_dev,
    741 		    "Error initializing xenstore comms: %i\n", err);
    742 		goto err0;
    743 	}
    744 
    745 	if (!dom0) {
    746 		xenstored_ready = 1;
    747 		xenbus_probe(NULL);
    748 	}
    749 
    750 	DPRINTK("done");
    751 	config_pending_decr(xenbus_dev);
    752 #ifdef DOM0OPS
    753 	if (dom0) {
    754 		mutex_enter(&xenstored_lock);
    755 		while (xenstored_ready == 0) {
    756 			cv_wait(&xenstored_cv, &xenstored_lock);
    757 			mutex_exit(&xenstored_lock);
    758 			xenbus_probe(NULL);
    759 			mutex_enter(&xenstored_lock);
    760 		}
    761 		mutex_exit(&xenstored_lock);
    762 	}
    763 #endif
    764 	kthread_exit(0);
    765 
    766 err0:
    767 	if (page)
    768 		uvm_km_free(kernel_map, page, PAGE_SIZE,
    769 				UVM_KMF_ZERO | UVM_KMF_WIRED);
    770 	kthread_exit(err);
    771 }
    772 
    773 /*
    774  * Local variables:
    775  *  c-file-style: "linux"
    776  *  indent-tabs-mode: t
    777  *  c-indent-level: 8
    778  *  c-basic-offset: 8
    779  *  tab-width: 8
    780  * End:
    781  */
    782