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