Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.56
      1 /* $NetBSD: xenbus_probe.c,v 1.56 2021/04/24 23:36:52 thorpej 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.56 2021/04/24 23:36:52 thorpej 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 (!xdev->xbusd_otherend ||
    251 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    252 		    strlen(xdev->xbusd_otherend))) {
    253 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    254 		return;
    255 	}
    256 
    257 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    258 
    259 	DPRINTK("state is %d, %s, %s",
    260 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    261 	if (state == XenbusStateClosed) {
    262 		int error;
    263 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
    264 			error = xdev->xbusd_u.b.b_detach(
    265 			    xdev->xbusd_u.b.b_cookie);
    266 			if (error) {
    267 				printf("could not detach %s: %d\n",
    268 				    xdev->xbusd_path, error);
    269 				return;
    270 			}
    271 		} else {
    272 			error = config_detach(xdev->xbusd_u.f.f_dev,
    273 			    DETACH_FORCE);
    274 			if (error) {
    275 				printf("could not detach %s: %d\n",
    276 				    device_xname(xdev->xbusd_u.f.f_dev), error);
    277 				return;
    278 			}
    279 		}
    280 		xenbus_free_device(xdev);
    281 		return;
    282 	}
    283 	if (xdev->xbusd_otherend_changed)
    284 		xdev->xbusd_otherend_changed(
    285 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
    286 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
    287 }
    288 
    289 static int
    290 watch_otherend(struct xenbus_device *dev)
    291 {
    292 	free_otherend_watch(dev);
    293 
    294 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    295 				  &dev->xbusd_otherend_watch,
    296 				  otherend_changed);
    297 }
    298 
    299 static struct xenbus_device *
    300 xenbus_lookup_device_path(const char *path)
    301 {
    302 	struct xenbus_device *xbusd;
    303 
    304 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    305 		if (strcmp(xbusd->xbusd_path, path) == 0)
    306 			return xbusd;
    307 	}
    308 	return NULL;
    309 }
    310 
    311 static int
    312 xenbus_probe_device_type(const char *path, const char *type,
    313     int (*create)(struct xenbus_device *))
    314 {
    315 	int err, i, pos, msize;
    316 	int *lookup = NULL;
    317 	size_t lookup_sz = 0;
    318 	unsigned long state;
    319 	char **dir;
    320 	unsigned int orig_dir_n = 0, dir_n;
    321 	struct xenbus_device *xbusd;
    322 	struct xenbusdev_attach_args xa;
    323 	char *ep;
    324 
    325 	DPRINTK("probe %s type %s", path, type);
    326 	err = xenbus_directory(NULL, path, "", &orig_dir_n, &dir);
    327 	DPRINTK("directory err %d dir_n %d", err, orig_dir_n);
    328 	if (err)
    329 		return err;
    330 	dir_n = orig_dir_n;
    331 
    332 	/* Only sort frontend devices i.e. create == NULL*/
    333 	if (dir_n > 1 && create == NULL) {
    334 		int minp;
    335 		unsigned long minv;
    336 		unsigned long *id;
    337 		size_t id_sz;
    338 
    339 		lookup_sz = sizeof(int) * dir_n;
    340 		lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
    341 
    342 		id_sz = sizeof(unsigned long) * dir_n;
    343 		id = kmem_zalloc(id_sz, KM_SLEEP);
    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 		kmem_free(id, id_sz);
    375 
    376 		/* Adjust in case we had to skip non-numeric entries */
    377 		dir_n = pos;
    378 	}
    379 
    380 	for (pos = 0; pos < dir_n; pos++) {
    381 		err = 0;
    382 		if (lookup)
    383 			i = lookup[pos];
    384 		else
    385 			i = pos;
    386 		/*
    387 		 * add size of path to size of xenbus_device. xenbus_device
    388 		 * already has room for one char in xbusd_path.
    389 		 */
    390 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
    391 		xbusd = kmem_zalloc(msize, KM_SLEEP);
    392 		xbusd->xbusd_sz = msize;
    393 		xbusd->xbusd_dmat = xenbus_dmat;
    394 
    395 		snprintf(__UNCONST(xbusd->xbusd_path),
    396 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
    397 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    398 			/* device already registered */
    399 			kmem_free(xbusd, xbusd->xbusd_sz);
    400 			continue;
    401 		}
    402 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    403 		    &state, 10);
    404 		if (err) {
    405 			aprint_error_dev(xenbus_dev, "can't get state "
    406 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    407 			kmem_free(xbusd, xbusd->xbusd_sz);
    408 			err = 0;
    409 			continue;
    410 		}
    411 		if (state != XenbusStateInitialising) {
    412 			/* device is not new */
    413 			kmem_free(xbusd, xbusd->xbusd_sz);
    414 			continue;
    415 		}
    416 
    417 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    418 		DPRINTK("xenbus_probe_device_type probe %s\n",
    419 		    xbusd->xbusd_path);
    420 		if (create != NULL) {
    421 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
    422 			err = read_frontend_details(xbusd);
    423 			if (err != 0) {
    424 				aprint_error_dev(xenbus_dev,
    425 				    "can't get frontend details for %s (%d)\n",
    426 				    xbusd->xbusd_path, err);
    427 				break;
    428 			}
    429 			if (create(xbusd)) {
    430 				kmem_free(xbusd, xbusd->xbusd_sz);
    431 				continue;
    432 			}
    433 		} else {
    434 			xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
    435 			xa.xa_xbusd = xbusd;
    436 			xa.xa_type = type;
    437 			xa.xa_id = strtoul(dir[i], &ep, 0);
    438 			if (dir[i][0] == '\0' || *ep != '\0') {
    439 				aprint_error_dev(xenbus_dev,
    440 				    "device type %s: id %s is not a number\n",
    441 				    type, dir[i]);
    442 				err = EFTYPE;
    443 				kmem_free(xbusd, xbusd->xbusd_sz);
    444 				break;
    445 			}
    446 			if (strcmp(xa.xa_type, "vbd") == 0) {
    447 				char dtype[10];
    448 				if (xenbus_read(NULL, xbusd->xbusd_path,
    449 				    "device-type", dtype, sizeof(dtype)) !=0) {
    450 					aprint_error_dev(xenbus_dev,
    451 					    "%s: can't read device-type\n",
    452 					    xbusd->xbusd_path);
    453 					kmem_free(xbusd, xbusd->xbusd_sz);
    454 					break;
    455 				}
    456 				if (strcmp(dtype, "cdrom") == 0) {
    457 					aprint_verbose_dev(xenbus_dev,
    458 					    "ignoring %s type cdrom\n",
    459 					    xbusd->xbusd_path);
    460 					kmem_free(xbusd, xbusd->xbusd_sz);
    461 					continue;
    462 				}
    463 			}
    464 			err = read_backend_details(xbusd);
    465 			if (err != 0) {
    466 				aprint_error_dev(xenbus_dev,
    467 				    "can't get backend details for %s (%d)\n",
    468 				    xbusd->xbusd_path, err);
    469 				kmem_free(xbusd, xbusd->xbusd_sz);
    470 				break;
    471 			}
    472 			xbusd->xbusd_u.f.f_dev = config_found(xenbus_dev,
    473 			    &xa, xenbus_print, CFARG_EOL);
    474 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    475 				kmem_free(xbusd, xbusd->xbusd_sz);
    476 				continue;
    477 			}
    478 		}
    479 		SLIST_INSERT_HEAD(&xenbus_device_list,
    480 		    xbusd, xbusd_entries);
    481 		watch_otherend(xbusd);
    482 	}
    483 	xenbus_directory_free(orig_dir_n, dir);
    484 	if (lookup)
    485 		kmem_free(lookup, lookup_sz);
    486 
    487 	return err;
    488 }
    489 
    490 static int
    491 xenbus_print(void *aux, const char *pnp)
    492 {
    493 	struct xenbusdev_attach_args *xa = aux;
    494 
    495 	if (pnp) {
    496 		if (strcmp(xa->xa_type, "vbd") == 0)
    497 			aprint_normal("xbd");
    498 		else if (strcmp(xa->xa_type, "vif") == 0)
    499 			aprint_normal("xennet");
    500 		else if (strcmp(xa->xa_type, "balloon") == 0)
    501 			aprint_normal("balloon");
    502 		else
    503 			aprint_normal("unknown type %s", xa->xa_type);
    504 		aprint_normal(" at %s", pnp);
    505 	}
    506 	aprint_normal(" id %d", xa->xa_id);
    507 	return(UNCONF);
    508 }
    509 
    510 static int
    511 xenbus_probe_frontends(void)
    512 {
    513 	int err;
    514 	char **dir;
    515 	unsigned int i, dir_n;
    516 	char path[30];
    517 
    518 	DPRINTK("probe device");
    519 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    520 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    521 	if (err)
    522 		return err;
    523 
    524 	for (i = 0; i < dir_n; i++) {
    525 		/*
    526 		 * console is configured through xen_start_info when
    527 		 * xencons is attaching to hypervisor, so avoid console
    528 		 * probing when configuring xenbus devices
    529 		 */
    530 		if (strcmp(dir[i], "console") == 0)
    531 			continue;
    532 
    533 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    534 		err = xenbus_probe_device_type(path, dir[i], NULL);
    535 		if (err)
    536 			break;
    537 	}
    538 	xenbus_directory_free(dir_n, dir);
    539 	return err;
    540 }
    541 
    542 static int
    543 xenbus_probe_backends(void)
    544 {
    545 	int err;
    546 	char **dirt, **dirid;
    547 	unsigned int type, id, dirt_n, dirid_n;
    548 	char path[30];
    549 	struct xenbus_backend_driver *xbakd;
    550 
    551 	DPRINTK("probe backend");
    552 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    553 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    554 	if (err)
    555 		return err;
    556 
    557 	for (type = 0; type < dirt_n; type++) {
    558 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    559 		    xbakd_entries) {
    560 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    561 				break;
    562 		}
    563 		if (xbakd == NULL)
    564 			continue;
    565 		err = xenbus_directory(NULL, "backend", dirt[type],
    566 		    &dirid_n, &dirid);
    567 		DPRINTK("directory backend/%s err %d dirid_n %d",
    568 		    dirt[type], err, dirid_n);
    569 		if (err)
    570 			goto out;
    571 
    572 		for (id = 0; id < dirid_n; id++) {
    573 			snprintf(path, sizeof(path), "backend/%s/%s",
    574 			    dirt[type], dirid[id]);
    575 			err = xenbus_probe_device_type(path, dirt[type],
    576 			    xbakd->xbakd_create);
    577 			if (err)
    578 				break;
    579 		}
    580 		xenbus_directory_free(dirid_n, dirid);
    581 	}
    582 
    583 out:
    584 	xenbus_directory_free(dirt_n, dirt);
    585 	return err;
    586 }
    587 
    588 int
    589 xenbus_free_device(struct xenbus_device *xbusd)
    590 {
    591 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    592 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    593 	free_otherend_watch(xbusd);
    594 	free_otherend_details(xbusd);
    595 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    596 	kmem_free(xbusd, xbusd->xbusd_sz);
    597 	return 0;
    598 }
    599 
    600 static void
    601 frontend_changed(struct xenbus_watch *watch,
    602 			     const char **vec, unsigned int len)
    603 {
    604 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    605 	xenbus_probe_frontends();
    606 }
    607 
    608 static void
    609 backend_changed(struct xenbus_watch *watch,
    610 			    const char **vec, unsigned int len)
    611 {
    612 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    613 	xenbus_probe_backends();
    614 }
    615 
    616 
    617 /* We watch for devices appearing and vanishing. */
    618 static struct xenbus_watch fe_watch;
    619 
    620 static struct xenbus_watch be_watch;
    621 
    622 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    623 int xenstored_ready = 0;
    624 static kmutex_t xenstored_lock;
    625 static kcondvar_t xenstored_cv;
    626 
    627 void
    628 xenbus_probe(void *unused)
    629 {
    630 	struct xenbusdev_attach_args balloon_xa = {
    631 		.xa_id = 0,
    632 		.xa_type = "balloon"
    633 	};
    634 
    635 	KASSERT((xenstored_ready > 0));
    636 
    637 	/* Enumerate devices in xenstore. */
    638 	xenbus_probe_frontends();
    639 	xenbus_probe_backends();
    640 
    641 	/* Watch for changes. */
    642 	fe_watch.node_sz = strlen("device") + 1;
    643 	fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
    644 	strcpy(fe_watch.node, "device");
    645 	fe_watch.xbw_callback = frontend_changed;
    646 	register_xenbus_watch(&fe_watch);
    647 
    648 	be_watch.node_sz = strlen("backend") + 1;
    649 	be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
    650 	strcpy(be_watch.node, "backend");
    651 	be_watch.xbw_callback = backend_changed;
    652 	register_xenbus_watch(&be_watch);
    653 
    654 	/* attach balloon. */
    655 	config_found(xenbus_dev, &balloon_xa, xenbus_print,
    656 	    CFARG_EOL);
    657 
    658 	shutdown_xenbus_setup();
    659 
    660 	/* Notify others that xenstore is up */
    661 	//notifier_call_chain(&xenstore_chain, 0, NULL);
    662 }
    663 
    664 void
    665 xb_xenstored_make_ready(void)
    666 {
    667 	mutex_enter(&xenstored_lock);
    668 	xenstored_ready = 1;
    669 	cv_broadcast(&xenstored_cv);
    670 	mutex_exit(&xenstored_lock);
    671 }
    672 
    673 static void
    674 xenbus_probe_init(void *unused)
    675 {
    676 	int err = 0;
    677 	bool dom0;
    678 	vaddr_t page = 0;
    679 
    680 	DPRINTK("");
    681 
    682 	SLIST_INIT(&xenbus_device_list);
    683 	mutex_init(&xenstored_lock, MUTEX_DEFAULT, IPL_TTY);
    684 	cv_init(&xenstored_cv, "xsready");
    685 
    686 	/*
    687 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
    688 	*/
    689 	dom0 = xendomain_is_dom0();
    690 	if (dom0) {
    691 #if defined(DOM0OPS)
    692 		paddr_t ma;
    693 		evtchn_op_t op = { .cmd = 0 };
    694 
    695 		/* Allocate page. */
    696 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    697 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
    698 		if (!page)
    699 			panic("can't get xenstore page");
    700 
    701 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
    702 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
    703 		xenstore_interface = (void *)page;
    704 
    705 		/* Next allocate a local port which xenstored can bind to */
    706 		op.cmd = EVTCHNOP_alloc_unbound;
    707 		op.u.alloc_unbound.dom        = DOMID_SELF;
    708 		op.u.alloc_unbound.remote_dom = 0;
    709 
    710 		err = HYPERVISOR_event_channel_op(&op);
    711 		if (err) {
    712 			aprint_error_dev(xenbus_dev,
    713 				"can't register xenstore event\n");
    714 			goto err0;
    715 		}
    716 
    717 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
    718 
    719 		DELAY(1000);
    720 #else /* DOM0OPS */
    721 		kthread_exit(0); /* can't get a working xenstore in this case */
    722 #endif /* DOM0OPS */
    723 	}
    724 
    725 #if NKERNFS > 0
    726 	/* Publish xenbus and Xenstore info in /kern/xen */
    727 	xenbus_kernfs_init();
    728 #endif
    729 
    730 	/* register event handler */
    731 	xb_init_comms(xenbus_dev);
    732 
    733 	/* Initialize the interface to xenstore. */
    734 	err = xs_init(xenbus_dev);
    735 	if (err) {
    736 		aprint_error_dev(xenbus_dev,
    737 		    "Error initializing xenstore comms: %i\n", err);
    738 		goto err0;
    739 	}
    740 
    741 	if (!dom0) {
    742 		xenstored_ready = 1;
    743 		xenbus_probe(NULL);
    744 	}
    745 
    746 	DPRINTK("done");
    747 	config_pending_decr(xenbus_dev);
    748 #ifdef DOM0OPS
    749 	if (dom0) {
    750 		mutex_enter(&xenstored_lock);
    751 		while (xenstored_ready == 0) {
    752 			cv_wait(&xenstored_cv, &xenstored_lock);
    753 			mutex_exit(&xenstored_lock);
    754 			xenbus_probe(NULL);
    755 			mutex_enter(&xenstored_lock);
    756 		}
    757 		mutex_exit(&xenstored_lock);
    758 	}
    759 #endif
    760 	kthread_exit(0);
    761 
    762 err0:
    763 	if (page)
    764 		uvm_km_free(kernel_map, page, PAGE_SIZE,
    765 				UVM_KMF_ZERO | UVM_KMF_WIRED);
    766 	kthread_exit(err);
    767 }
    768 
    769 /*
    770  * Local variables:
    771  *  c-file-style: "linux"
    772  *  indent-tabs-mode: t
    773  *  c-indent-level: 8
    774  *  c-basic-offset: 8
    775  *  tab-width: 8
    776  * End:
    777  */
    778