Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.27.2.8
      1  1.27.2.8       jym /* $NetBSD: xenbus_probe.c,v 1.27.2.8 2011/05/07 17:39:47 jym Exp $ */
      2       1.1    bouyer /******************************************************************************
      3       1.1    bouyer  * Talks to Xen Store to figure out what devices we have.
      4       1.1    bouyer  *
      5       1.1    bouyer  * Copyright (C) 2005 Rusty Russell, IBM Corporation
      6       1.1    bouyer  * Copyright (C) 2005 Mike Wray, Hewlett-Packard
      7       1.1    bouyer  * Copyright (C) 2005 XenSource Ltd
      8       1.1    bouyer  *
      9       1.1    bouyer  * This file may be distributed separately from the Linux kernel, or
     10       1.1    bouyer  * incorporated into other software packages, subject to the following license:
     11       1.1    bouyer  *
     12       1.1    bouyer  * Permission is hereby granted, free of charge, to any person obtaining a copy
     13       1.1    bouyer  * of this source file (the "Software"), to deal in the Software without
     14       1.1    bouyer  * restriction, including without limitation the rights to use, copy, modify,
     15       1.1    bouyer  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
     16       1.1    bouyer  * and to permit persons to whom the Software is furnished to do so, subject to
     17       1.1    bouyer  * the following conditions:
     18       1.1    bouyer  *
     19       1.1    bouyer  * The above copyright notice and this permission notice shall be included in
     20       1.1    bouyer  * all copies or substantial portions of the Software.
     21       1.1    bouyer  *
     22       1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23       1.1    bouyer  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24       1.1    bouyer  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     25       1.1    bouyer  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26       1.1    bouyer  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27       1.1    bouyer  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     28       1.1    bouyer  * IN THE SOFTWARE.
     29       1.1    bouyer  */
     30       1.1    bouyer 
     31       1.2    bouyer #include <sys/cdefs.h>
     32  1.27.2.8       jym __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.27.2.8 2011/05/07 17:39:47 jym Exp $");
     33       1.2    bouyer 
     34       1.1    bouyer #if 0
     35       1.1    bouyer #define DPRINTK(fmt, args...) \
     36      1.18     perry     printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
     37       1.1    bouyer #else
     38       1.1    bouyer #define DPRINTK(fmt, args...) ((void)0)
     39       1.1    bouyer #endif
     40       1.1    bouyer 
     41       1.2    bouyer #include <sys/types.h>
     42       1.2    bouyer #include <sys/null.h>
     43       1.2    bouyer #include <sys/errno.h>
     44       1.2    bouyer #include <sys/malloc.h>
     45       1.2    bouyer #include <sys/systm.h>
     46       1.2    bouyer #include <sys/param.h>
     47       1.2    bouyer #include <sys/kthread.h>
     48       1.7    bouyer #include <uvm/uvm.h>
     49       1.7    bouyer 
     50       1.2    bouyer #include <machine/stdarg.h>
     51       1.2    bouyer 
     52      1.26    cegger #include <xen/xen.h>	/* for xendomain_is_dom0() */
     53      1.17    bouyer #include <xen/hypervisor.h>
     54      1.17    bouyer #include <xen/xenbus.h>
     55      1.17    bouyer #include <xen/evtchn.h>
     56      1.17    bouyer #include <xen/shutdown_xenbus.h>
     57       1.1    bouyer 
     58       1.1    bouyer #include "xenbus_comms.h"
     59       1.1    bouyer 
     60       1.1    bouyer extern struct semaphore xenwatch_mutex;
     61       1.1    bouyer 
     62       1.1    bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
     63       1.1    bouyer 
     64      1.20    cegger static int  xenbus_match(device_t, cfdata_t, void *);
     65      1.20    cegger static void xenbus_attach(device_t, device_t, void *);
     66       1.2    bouyer static int  xenbus_print(void *, const char *);
     67       1.2    bouyer 
     68  1.27.2.4       jym /* power management, for save/restore */
     69  1.27.2.5       jym static bool xenbus_suspend(device_t, const pmf_qual_t *);
     70  1.27.2.5       jym static bool xenbus_resume(device_t, const pmf_qual_t *);
     71  1.27.2.4       jym 
     72  1.27.2.4       jym /* routines gathering device information from XenStore */
     73  1.27.2.4       jym static int  read_otherend_details(struct xenbus_device *,
     74  1.27.2.6       jym 		const char *, const char *);
     75  1.27.2.4       jym static int  read_backend_details (struct xenbus_device *);
     76  1.27.2.4       jym static int  read_frontend_details(struct xenbus_device *);
     77  1.27.2.4       jym static void free_otherend_details(struct xenbus_device *);
     78  1.27.2.4       jym 
     79  1.27.2.6       jym static int  watch_otherend     (struct xenbus_device *);
     80  1.27.2.6       jym static void free_otherend_watch(struct xenbus_device *);
     81  1.27.2.6       jym 
     82  1.27.2.6       jym static void xenbus_probe_init(void *);
     83  1.27.2.6       jym 
     84  1.27.2.6       jym static struct xenbus_device *xenbus_lookup_device_path(const char *);
     85  1.27.2.6       jym 
     86      1.20    cegger CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
     87       1.2    bouyer     NULL, NULL);
     88       1.2    bouyer 
     89      1.24    cegger device_t xenbus_dev;
     90       1.5    bouyer 
     91       1.5    bouyer SLIST_HEAD(, xenbus_device) xenbus_device_list;
     92      1.11    bouyer SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
     93      1.11    bouyer 	SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
     94       1.2    bouyer 
     95       1.2    bouyer int
     96      1.20    cegger xenbus_match(device_t parent, cfdata_t match, void *aux)
     97       1.2    bouyer {
     98       1.2    bouyer 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
     99       1.2    bouyer 
    100       1.2    bouyer 	if (strcmp(xa->xa_device, "xenbus") == 0)
    101       1.2    bouyer 		return 1;
    102       1.2    bouyer 	return 0;
    103       1.2    bouyer }
    104       1.2    bouyer 
    105       1.2    bouyer static void
    106      1.20    cegger xenbus_attach(device_t parent, device_t self, void *aux)
    107       1.2    bouyer {
    108      1.15        ad 	int err;
    109      1.15        ad 
    110       1.2    bouyer 	aprint_normal(": Xen Virtual Bus Interface\n");
    111      1.24    cegger 	xenbus_dev = self;
    112       1.2    bouyer 	config_pending_incr();
    113      1.15        ad 
    114      1.15        ad 	err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
    115      1.15        ad 	    NULL, "xenbus_probe");
    116      1.15        ad 	if (err)
    117      1.24    cegger 		aprint_error_dev(xenbus_dev,
    118      1.23    cegger 				"kthread_create(xenbus_probe): %d\n", err);
    119  1.27.2.4       jym 
    120  1.27.2.4       jym 	if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
    121  1.27.2.4       jym 		aprint_error_dev(self, "couldn't establish power handler\n");
    122  1.27.2.4       jym }
    123  1.27.2.4       jym 
    124  1.27.2.4       jym static bool
    125  1.27.2.8       jym xenbus_suspend(device_t dev, const pmf_qual_t *qual)
    126  1.27.2.8       jym {
    127  1.27.2.4       jym 	xs_suspend();
    128  1.27.2.4       jym 	xb_suspend_comms(dev);
    129  1.27.2.4       jym 
    130  1.27.2.4       jym 	return true;
    131  1.27.2.4       jym }
    132  1.27.2.4       jym 
    133  1.27.2.4       jym static bool
    134  1.27.2.8       jym xenbus_resume(device_t dev, const pmf_qual_t *qual)
    135  1.27.2.8       jym {
    136  1.27.2.4       jym 	xb_init_comms(dev);
    137  1.27.2.4       jym 	xs_resume();
    138  1.27.2.4       jym 
    139  1.27.2.4       jym 	return true;
    140  1.27.2.4       jym }
    141  1.27.2.4       jym 
    142  1.27.2.4       jym /*
    143  1.27.2.4       jym  * Suspend a xenbus device
    144  1.27.2.4       jym  */
    145  1.27.2.4       jym bool
    146  1.27.2.4       jym xenbus_device_suspend(struct xenbus_device *dev) {
    147  1.27.2.4       jym 
    148  1.27.2.4       jym 	free_otherend_details(dev);
    149  1.27.2.4       jym 	return true;
    150  1.27.2.4       jym }
    151  1.27.2.4       jym 
    152  1.27.2.4       jym /*
    153  1.27.2.4       jym  * Resume a xenbus device
    154  1.27.2.4       jym  */
    155  1.27.2.4       jym bool
    156  1.27.2.4       jym xenbus_device_resume(struct xenbus_device *dev) {
    157  1.27.2.4       jym 
    158  1.27.2.4       jym 	if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
    159  1.27.2.4       jym 		read_backend_details(dev);
    160  1.27.2.4       jym 	}
    161  1.27.2.4       jym 
    162  1.27.2.4       jym 	return true;
    163  1.27.2.2       jym }
    164  1.27.2.2       jym 
    165       1.2    bouyer void
    166      1.11    bouyer xenbus_backend_register(struct xenbus_backend_driver *xbakd)
    167      1.11    bouyer {
    168      1.11    bouyer 	SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
    169      1.11    bouyer }
    170      1.11    bouyer 
    171       1.2    bouyer static int
    172       1.2    bouyer read_otherend_details(struct xenbus_device *xendev,
    173       1.2    bouyer 				 const char *id_node, const char *path_node)
    174       1.2    bouyer {
    175       1.2    bouyer 	int err;
    176       1.2    bouyer 	char *val, *ep;
    177       1.1    bouyer 
    178       1.2    bouyer 	err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
    179       1.2    bouyer 	if (err) {
    180       1.2    bouyer 		printf("reading other end details %s from %s\n",
    181       1.2    bouyer 		    id_node, xendev->xbusd_path);
    182       1.2    bouyer 		xenbus_dev_fatal(xendev, err,
    183       1.2    bouyer 				 "reading other end details %s from %s",
    184       1.2    bouyer 				 id_node, xendev->xbusd_path);
    185       1.2    bouyer 		return err;
    186       1.2    bouyer 	}
    187       1.2    bouyer 	xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
    188       1.2    bouyer 	if (val[0] == '\0' || *ep != '\0') {
    189       1.2    bouyer 		printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
    190       1.2    bouyer 		xenbus_dev_fatal(xendev, err,
    191       1.2    bouyer 		    "reading other end details %s from %s: %s is not a number",
    192       1.2    bouyer 		    id_node, xendev->xbusd_path, val);
    193       1.2    bouyer 		free(val, M_DEVBUF);
    194       1.2    bouyer 		return EFTYPE;
    195       1.2    bouyer 	}
    196       1.2    bouyer 	free(val, M_DEVBUF);
    197       1.2    bouyer 	err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
    198       1.1    bouyer 	if (err) {
    199       1.2    bouyer 		printf("reading other end details %s from %s (%d)\n",
    200       1.2    bouyer 		    path_node, xendev->xbusd_path, err);
    201       1.1    bouyer 		xenbus_dev_fatal(xendev, err,
    202       1.2    bouyer 				 "reading other end details %s from %s",
    203       1.2    bouyer 				 path_node, xendev->xbusd_path);
    204       1.1    bouyer 		return err;
    205       1.1    bouyer 	}
    206       1.3    bouyer 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
    207       1.3    bouyer 	    xendev->xbusd_path, path_node, val);
    208       1.2    bouyer 	xendev->xbusd_otherend = val;
    209       1.2    bouyer 
    210       1.2    bouyer 	if (strlen(xendev->xbusd_otherend) == 0 ||
    211       1.2    bouyer 	    !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
    212       1.2    bouyer 		printf("missing other end from %s\n", xendev->xbusd_path);
    213       1.1    bouyer 		xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
    214       1.2    bouyer 				 xendev->xbusd_path);
    215  1.27.2.4       jym 		free_otherend_details(xendev);
    216       1.2    bouyer 		return ENOENT;
    217       1.1    bouyer 	}
    218       1.1    bouyer 
    219       1.1    bouyer 	return 0;
    220       1.1    bouyer }
    221       1.1    bouyer 
    222       1.2    bouyer static int
    223       1.2    bouyer read_backend_details(struct xenbus_device *xendev)
    224       1.1    bouyer {
    225       1.1    bouyer 	return read_otherend_details(xendev, "backend-id", "backend");
    226       1.1    bouyer }
    227       1.1    bouyer 
    228       1.1    bouyer 
    229       1.2    bouyer static int
    230       1.2    bouyer read_frontend_details(struct xenbus_device *xendev)
    231       1.1    bouyer {
    232       1.1    bouyer 	return read_otherend_details(xendev, "frontend-id", "frontend");
    233       1.1    bouyer }
    234       1.1    bouyer 
    235       1.2    bouyer static void
    236       1.2    bouyer free_otherend_details(struct xenbus_device *dev)
    237       1.1    bouyer {
    238       1.2    bouyer 	free(dev->xbusd_otherend, M_DEVBUF);
    239       1.2    bouyer 	dev->xbusd_otherend = NULL;
    240       1.1    bouyer }
    241  1.27.2.3       jym 
    242       1.2    bouyer static void
    243       1.2    bouyer free_otherend_watch(struct xenbus_device *dev)
    244       1.1    bouyer {
    245       1.2    bouyer 	if (dev->xbusd_otherend_watch.node) {
    246       1.2    bouyer 		unregister_xenbus_watch(&dev->xbusd_otherend_watch);
    247       1.3    bouyer 		free(dev->xbusd_otherend_watch.node, M_DEVBUF);
    248       1.2    bouyer 		dev->xbusd_otherend_watch.node = NULL;
    249       1.1    bouyer 	}
    250       1.1    bouyer }
    251       1.1    bouyer 
    252       1.2    bouyer static void
    253       1.2    bouyer otherend_changed(struct xenbus_watch *watch,
    254       1.1    bouyer 			     const char **vec, unsigned int len)
    255       1.1    bouyer {
    256       1.3    bouyer 	struct xenbus_device *xdev = watch->xbw_dev;
    257       1.1    bouyer 	XenbusState state;
    258       1.1    bouyer 
    259       1.1    bouyer 	/* Protect us against watches firing on old details when the otherend
    260       1.1    bouyer 	   details change, say immediately after a resume. */
    261       1.3    bouyer 	if (!xdev->xbusd_otherend ||
    262       1.3    bouyer 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    263       1.3    bouyer 		    strlen(xdev->xbusd_otherend))) {
    264       1.1    bouyer 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    265       1.1    bouyer 		return;
    266       1.1    bouyer 	}
    267       1.1    bouyer 
    268       1.3    bouyer 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    269       1.1    bouyer 
    270       1.1    bouyer 	DPRINTK("state is %d, %s, %s",
    271       1.3    bouyer 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    272       1.5    bouyer 	if (state == XenbusStateClosed) {
    273       1.5    bouyer 		int error;
    274      1.11    bouyer 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
    275      1.11    bouyer 			error = xdev->xbusd_u.b.b_detach(
    276      1.11    bouyer 			    xdev->xbusd_u.b.b_cookie);
    277      1.11    bouyer 			if (error) {
    278      1.11    bouyer 				printf("could not detach %s: %d\n",
    279      1.11    bouyer 				    xdev->xbusd_path, error);
    280      1.11    bouyer 				return;
    281      1.11    bouyer 			}
    282      1.11    bouyer 		} else {
    283      1.11    bouyer 			error = config_detach(xdev->xbusd_u.f.f_dev,
    284      1.11    bouyer 			    DETACH_FORCE);
    285      1.11    bouyer 			if (error) {
    286      1.11    bouyer 				printf("could not detach %s: %d\n",
    287      1.19    cegger 				    device_xname(xdev->xbusd_u.f.f_dev), error);
    288      1.11    bouyer 				return;
    289      1.11    bouyer 			}
    290       1.5    bouyer 		}
    291       1.5    bouyer 		xenbus_free_device(xdev);
    292       1.5    bouyer 		return;
    293       1.5    bouyer 	}
    294       1.3    bouyer 	if (xdev->xbusd_otherend_changed)
    295      1.11    bouyer 		xdev->xbusd_otherend_changed(
    296      1.11    bouyer 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
    297      1.11    bouyer 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
    298       1.1    bouyer }
    299       1.1    bouyer 
    300  1.27.2.4       jym static int
    301  1.27.2.4       jym watch_otherend(struct xenbus_device *dev)
    302  1.27.2.4       jym {
    303  1.27.2.4       jym 	free_otherend_watch(dev);
    304  1.27.2.4       jym 
    305  1.27.2.4       jym 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    306  1.27.2.4       jym 				  &dev->xbusd_otherend_watch,
    307  1.27.2.4       jym 				  otherend_changed);
    308  1.27.2.4       jym }
    309       1.1    bouyer 
    310       1.5    bouyer static struct xenbus_device *
    311       1.5    bouyer xenbus_lookup_device_path(const char *path)
    312       1.5    bouyer {
    313       1.5    bouyer 	struct xenbus_device *xbusd;
    314       1.5    bouyer 
    315       1.5    bouyer 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    316       1.5    bouyer 		if (strcmp(xbusd->xbusd_path, path) == 0)
    317       1.5    bouyer 			return xbusd;
    318       1.5    bouyer 	}
    319       1.5    bouyer 	return NULL;
    320       1.5    bouyer }
    321       1.5    bouyer 
    322       1.2    bouyer static int
    323      1.11    bouyer xenbus_probe_device_type(const char *path, const char *type,
    324      1.11    bouyer     int (*create)(struct xenbus_device *))
    325       1.1    bouyer {
    326      1.11    bouyer 	int err, i, msize;
    327      1.11    bouyer 	unsigned long state;
    328       1.1    bouyer 	char **dir;
    329       1.1    bouyer 	unsigned int dir_n = 0;
    330       1.2    bouyer 	struct xenbus_device *xbusd;
    331       1.2    bouyer 	struct xenbusdev_attach_args xa;
    332       1.2    bouyer 	char *ep;
    333       1.2    bouyer 
    334      1.11    bouyer 	DPRINTK("probe %s type %s", path, type);
    335      1.11    bouyer 	err = xenbus_directory(NULL, path, "", &dir_n, &dir);
    336       1.2    bouyer 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    337       1.2    bouyer 	if (err)
    338       1.2    bouyer 		return err;
    339       1.1    bouyer 
    340       1.1    bouyer 	for (i = 0; i < dir_n; i++) {
    341  1.27.2.7       jym 		err = 0;
    342       1.5    bouyer 		/*
    343       1.5    bouyer 		 * add size of path to size of xenbus_device. xenbus_device
    344       1.5    bouyer 		 * already has room for one char in xbusd_path.
    345       1.5    bouyer 		 */
    346      1.11    bouyer 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
    347       1.5    bouyer 		xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
    348       1.2    bouyer 		if (xbusd == NULL)
    349       1.2    bouyer 			panic("can't malloc xbusd");
    350       1.2    bouyer 
    351       1.2    bouyer 		snprintf(__UNCONST(xbusd->xbusd_path),
    352      1.11    bouyer 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
    353       1.5    bouyer 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    354       1.5    bouyer 			/* device already registered */
    355       1.5    bouyer 			free(xbusd, M_DEVBUF);
    356       1.5    bouyer 			continue;
    357       1.5    bouyer 		}
    358      1.11    bouyer 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
    359      1.12    bouyer 		    &state, 10);
    360      1.11    bouyer 		if (err) {
    361      1.11    bouyer 			printf("xenbus: can't get state "
    362      1.11    bouyer 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    363      1.11    bouyer 			free(xbusd, M_DEVBUF);
    364  1.27.2.7       jym 			err = 0;
    365      1.11    bouyer 			continue;
    366      1.11    bouyer 		}
    367      1.11    bouyer 		if (state != XenbusStateInitialising) {
    368      1.11    bouyer 			/* device is not new */
    369      1.11    bouyer 			free(xbusd, M_DEVBUF);
    370      1.11    bouyer 			continue;
    371      1.11    bouyer 		}
    372       1.5    bouyer 
    373       1.3    bouyer 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    374       1.2    bouyer 		DPRINTK("xenbus_probe_device_type probe %s\n",
    375       1.2    bouyer 		    xbusd->xbusd_path);
    376      1.11    bouyer 		if (create != NULL) {
    377      1.11    bouyer 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
    378      1.11    bouyer 			err = read_frontend_details(xbusd);
    379      1.11    bouyer 			if (err != 0) {
    380      1.11    bouyer 				printf("xenbus: can't get frontend details "
    381      1.11    bouyer 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    382      1.11    bouyer 				break;
    383      1.11    bouyer 			}
    384      1.11    bouyer 			if (create(xbusd)) {
    385      1.11    bouyer 				free(xbusd, M_DEVBUF);
    386      1.11    bouyer 				continue;
    387      1.11    bouyer 			}
    388      1.11    bouyer 		} else {
    389      1.11    bouyer 			xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
    390      1.11    bouyer 			xa.xa_xbusd = xbusd;
    391      1.11    bouyer 			xa.xa_type = type;
    392      1.11    bouyer 			xa.xa_id = strtoul(dir[i], &ep, 0);
    393      1.11    bouyer 			if (dir[i][0] == '\0' || *ep != '\0') {
    394      1.11    bouyer 				printf("xenbus device type %s: id %s is not a"
    395      1.11    bouyer 				    " number\n", type, dir[i]);
    396      1.11    bouyer 				err = EFTYPE;
    397      1.11    bouyer 				free(xbusd, M_DEVBUF);
    398      1.11    bouyer 				break;
    399      1.11    bouyer 			}
    400      1.11    bouyer 			err = read_backend_details(xbusd);
    401      1.11    bouyer 			if (err != 0) {
    402      1.11    bouyer 				printf("xenbus: can't get backend details "
    403      1.11    bouyer 				    "for %s (%d)\n", xbusd->xbusd_path, err);
    404      1.11    bouyer 				break;
    405      1.11    bouyer 			}
    406      1.24    cegger 			xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
    407      1.11    bouyer 			    "xenbus", &xa, xenbus_print);
    408      1.11    bouyer 			if (xbusd->xbusd_u.f.f_dev == NULL) {
    409      1.11    bouyer 				free(xbusd, M_DEVBUF);
    410      1.11    bouyer 				continue;
    411      1.11    bouyer 			}
    412       1.3    bouyer 		}
    413      1.11    bouyer 		SLIST_INSERT_HEAD(&xenbus_device_list,
    414      1.11    bouyer 		    xbusd, xbusd_entries);
    415  1.27.2.4       jym 		watch_otherend(xbusd);
    416       1.1    bouyer 	}
    417       1.2    bouyer 	free(dir, M_DEVBUF);
    418       1.1    bouyer 	return err;
    419       1.1    bouyer }
    420       1.1    bouyer 
    421       1.2    bouyer static int
    422       1.2    bouyer xenbus_print(void *aux, const char *pnp)
    423       1.2    bouyer {
    424       1.2    bouyer 	struct xenbusdev_attach_args *xa = aux;
    425       1.2    bouyer 
    426       1.2    bouyer 	if (pnp) {
    427       1.2    bouyer 		if (strcmp(xa->xa_type, "vbd") == 0)
    428       1.2    bouyer 			aprint_normal("xbd");
    429       1.2    bouyer 		else if (strcmp(xa->xa_type, "vif") == 0)
    430       1.2    bouyer 			aprint_normal("xennet");
    431  1.27.2.7       jym 		else if (strcmp(xa->xa_type, "balloon") == 0)
    432  1.27.2.7       jym 			aprint_normal("balloon");
    433       1.2    bouyer 		else
    434       1.2    bouyer 			aprint_normal("unknown type %s", xa->xa_type);
    435       1.2    bouyer 		aprint_normal(" at %s", pnp);
    436       1.2    bouyer 	}
    437       1.2    bouyer 	aprint_normal(" id %d", xa->xa_id);
    438       1.2    bouyer 	return(UNCONF);
    439       1.2    bouyer }
    440       1.2    bouyer 
    441       1.2    bouyer static int
    442      1.11    bouyer xenbus_probe_frontends(void)
    443       1.1    bouyer {
    444       1.2    bouyer 	int err;
    445       1.1    bouyer 	char **dir;
    446       1.1    bouyer 	unsigned int i, dir_n;
    447      1.11    bouyer 	char path[30];
    448       1.1    bouyer 
    449      1.11    bouyer 	DPRINTK("probe device");
    450      1.11    bouyer 	err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
    451       1.2    bouyer 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    452       1.2    bouyer 	if (err)
    453       1.2    bouyer 		return err;
    454       1.1    bouyer 
    455       1.1    bouyer 	for (i = 0; i < dir_n; i++) {
    456      1.27       jym 		/*
    457      1.27       jym 		 * console is configured through xen_start_info when
    458      1.27       jym 		 * xencons is attaching to hypervisor, so avoid console
    459      1.27       jym 		 * probing when configuring xenbus devices
    460      1.27       jym 		 */
    461      1.27       jym 		if (strcmp(dir[i], "console") == 0)
    462      1.27       jym 			continue;
    463      1.27       jym 
    464      1.11    bouyer 		snprintf(path, sizeof(path), "device/%s", dir[i]);
    465      1.11    bouyer 		err = xenbus_probe_device_type(path, dir[i], NULL);
    466       1.1    bouyer 		if (err)
    467       1.1    bouyer 			break;
    468       1.1    bouyer 	}
    469       1.2    bouyer 	free(dir, M_DEVBUF);
    470       1.1    bouyer 	return err;
    471       1.1    bouyer }
    472       1.1    bouyer 
    473      1.11    bouyer static int
    474      1.11    bouyer xenbus_probe_backends(void)
    475      1.11    bouyer {
    476      1.11    bouyer 	int err;
    477      1.11    bouyer 	char **dirt, **dirid;
    478      1.11    bouyer 	unsigned int type, id, dirt_n, dirid_n;
    479      1.11    bouyer 	char path[30];
    480      1.11    bouyer 	struct xenbus_backend_driver *xbakd;
    481      1.11    bouyer 
    482      1.11    bouyer 	DPRINTK("probe backend");
    483      1.11    bouyer 	err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
    484      1.11    bouyer 	DPRINTK("directory err %d dirt_n %d", err, dirt_n);
    485      1.11    bouyer 	if (err)
    486      1.11    bouyer 		return err;
    487      1.11    bouyer 
    488      1.11    bouyer 	for (type = 0; type < dirt_n; type++) {
    489      1.11    bouyer 		SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
    490      1.11    bouyer 		    xbakd_entries) {
    491      1.11    bouyer 			if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
    492      1.11    bouyer 				break;
    493      1.11    bouyer 		}
    494      1.11    bouyer 		if (xbakd == NULL)
    495      1.11    bouyer 			continue;
    496      1.11    bouyer 		err = xenbus_directory(NULL, "backend", dirt[type],
    497      1.11    bouyer 		    &dirid_n, &dirid);
    498      1.11    bouyer 		DPRINTK("directory backend/%s err %d dirid_n %d",
    499      1.11    bouyer 		    dirt[type], err, dirid_n);
    500      1.14  christos 		if (err) {
    501      1.14  christos 			free(dirt, M_DEVBUF); /* to be checked */
    502      1.11    bouyer 			return err;
    503      1.14  christos 		}
    504      1.11    bouyer 		for (id = 0; id < dirid_n; id++) {
    505      1.11    bouyer 			snprintf(path, sizeof(path), "backend/%s/%s",
    506      1.11    bouyer 			    dirt[type], dirid[id]);
    507      1.11    bouyer 			err = xenbus_probe_device_type(path, dirt[type],
    508      1.11    bouyer 			    xbakd->xbakd_create);
    509      1.11    bouyer 			if (err)
    510      1.11    bouyer 				break;
    511      1.11    bouyer 		}
    512      1.11    bouyer 		free(dirid, M_DEVBUF);
    513      1.11    bouyer 	}
    514      1.11    bouyer 	free(dirt, M_DEVBUF);
    515      1.11    bouyer 	return err;
    516      1.11    bouyer }
    517      1.11    bouyer 
    518       1.5    bouyer int
    519       1.5    bouyer xenbus_free_device(struct xenbus_device *xbusd)
    520       1.5    bouyer {
    521       1.5    bouyer 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    522       1.5    bouyer 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    523       1.5    bouyer 	free_otherend_watch(xbusd);
    524  1.27.2.4       jym 	free_otherend_details(xbusd);
    525       1.5    bouyer 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    526       1.5    bouyer 	free(xbusd, M_DEVBUF);
    527       1.5    bouyer 	return 0;
    528       1.5    bouyer }
    529       1.5    bouyer 
    530       1.2    bouyer static void
    531       1.2    bouyer frontend_changed(struct xenbus_watch *watch,
    532       1.1    bouyer 			     const char **vec, unsigned int len)
    533       1.1    bouyer {
    534      1.11    bouyer 	DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    535      1.11    bouyer 	xenbus_probe_frontends();
    536       1.1    bouyer }
    537       1.1    bouyer 
    538       1.2    bouyer static void
    539       1.2    bouyer backend_changed(struct xenbus_watch *watch,
    540       1.1    bouyer 			    const char **vec, unsigned int len)
    541       1.1    bouyer {
    542      1.11    bouyer 	DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
    543      1.11    bouyer 	xenbus_probe_backends();
    544       1.1    bouyer }
    545       1.1    bouyer 
    546       1.2    bouyer 
    547       1.1    bouyer /* We watch for devices appearing and vanishing. */
    548       1.3    bouyer static struct xenbus_watch fe_watch;
    549       1.1    bouyer 
    550       1.3    bouyer static struct xenbus_watch be_watch;
    551       1.1    bouyer 
    552       1.1    bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
    553       1.1    bouyer int xenstored_ready = 0;
    554       1.1    bouyer 
    555       1.2    bouyer void
    556       1.2    bouyer xenbus_probe(void *unused)
    557       1.1    bouyer {
    558  1.27.2.7       jym 	struct xenbusdev_attach_args balloon_xa = {
    559  1.27.2.7       jym 		.xa_id = 0,
    560  1.27.2.7       jym 		.xa_type = "balloon"
    561  1.27.2.7       jym 	};
    562  1.27.2.7       jym 
    563       1.2    bouyer 	KASSERT((xenstored_ready > 0));
    564       1.1    bouyer 
    565       1.1    bouyer 	/* Enumerate devices in xenstore. */
    566      1.11    bouyer 	xenbus_probe_frontends();
    567      1.11    bouyer 	xenbus_probe_backends();
    568       1.1    bouyer 
    569       1.1    bouyer 	/* Watch for changes. */
    570       1.3    bouyer 	fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
    571       1.3    bouyer 	strcpy(fe_watch.node, "device");
    572       1.3    bouyer 	fe_watch.xbw_callback = frontend_changed;
    573       1.1    bouyer 	register_xenbus_watch(&fe_watch);
    574       1.3    bouyer 	be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
    575       1.3    bouyer 	strcpy(be_watch.node, "backend");
    576       1.3    bouyer 	be_watch.xbw_callback = backend_changed;
    577       1.1    bouyer 	register_xenbus_watch(&be_watch);
    578       1.1    bouyer 
    579  1.27.2.7       jym 	/* attach balloon. */
    580  1.27.2.7       jym 	config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
    581  1.27.2.7       jym 
    582  1.27.2.7       jym 	shutdown_xenbus_setup();
    583  1.27.2.5       jym 
    584       1.1    bouyer 	/* Notify others that xenstore is up */
    585       1.2    bouyer 	//notifier_call_chain(&xenstore_chain, 0, NULL);
    586       1.1    bouyer }
    587       1.1    bouyer 
    588       1.2    bouyer static void
    589       1.2    bouyer xenbus_probe_init(void *unused)
    590       1.1    bouyer {
    591      1.23    cegger 	int err = 0;
    592      1.23    cegger 	bool dom0;
    593      1.23    cegger 	vaddr_t page = 0;
    594       1.1    bouyer 
    595       1.1    bouyer 	DPRINTK("");
    596       1.1    bouyer 
    597       1.5    bouyer 	SLIST_INIT(&xenbus_device_list);
    598       1.5    bouyer 
    599       1.1    bouyer 	/*
    600       1.1    bouyer 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
    601       1.1    bouyer 	*/
    602      1.23    cegger 	dom0 = xendomain_is_dom0();
    603       1.1    bouyer 	if (dom0) {
    604       1.7    bouyer #if defined(DOM0OPS)
    605       1.7    bouyer 		paddr_t ma;
    606      1.22      tron 		evtchn_op_t op = { .cmd = 0 };
    607       1.1    bouyer 
    608       1.1    bouyer 		/* Allocate page. */
    609       1.7    bouyer 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    610       1.7    bouyer 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
    611       1.1    bouyer 		if (!page)
    612       1.7    bouyer 			panic("can't get xenstore page");
    613       1.1    bouyer 
    614       1.7    bouyer 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
    615       1.7    bouyer 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
    616       1.7    bouyer 		xenstore_interface = (void *)page;
    617       1.1    bouyer 
    618       1.1    bouyer 		/* Next allocate a local port which xenstored can bind to */
    619       1.1    bouyer 		op.cmd = EVTCHNOP_alloc_unbound;
    620       1.1    bouyer 		op.u.alloc_unbound.dom        = DOMID_SELF;
    621       1.1    bouyer 		op.u.alloc_unbound.remote_dom = 0;
    622       1.1    bouyer 
    623      1.23    cegger 		err = HYPERVISOR_event_channel_op(&op);
    624      1.23    cegger 		if (err) {
    625      1.24    cegger 			aprint_error_dev(xenbus_dev,
    626      1.23    cegger 				"can't register xenstore event\n");
    627      1.23    cegger 			goto err0;
    628      1.23    cegger 		}
    629      1.23    cegger 
    630       1.7    bouyer 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
    631       1.1    bouyer 
    632       1.7    bouyer 		/* And finally publish the above info in /kern/xen */
    633       1.9    bouyer 		xenbus_kernfs_init();
    634      1.21    cegger 
    635      1.21    cegger 		DELAY(1000);
    636       1.7    bouyer #else /* DOM0OPS */
    637      1.23    cegger 		kthread_exit(0); /* can't get a working xenstore in this case */
    638       1.7    bouyer #endif /* DOM0OPS */
    639       1.1    bouyer 	}
    640       1.1    bouyer 
    641      1.10    bouyer 	/* register event handler */
    642      1.24    cegger 	xb_init_comms(xenbus_dev);
    643      1.10    bouyer 
    644       1.1    bouyer 	/* Initialize the interface to xenstore. */
    645      1.25       jym 	err = xs_init(xenbus_dev);
    646       1.1    bouyer 	if (err) {
    647      1.24    cegger 		aprint_error_dev(xenbus_dev,
    648      1.23    cegger 				"Error initializing xenstore comms: %i\n", err);
    649      1.23    cegger 		goto err0;
    650       1.1    bouyer 	}
    651       1.1    bouyer 
    652       1.1    bouyer 	if (!dom0) {
    653       1.1    bouyer 		xenstored_ready = 1;
    654       1.1    bouyer 		xenbus_probe(NULL);
    655       1.1    bouyer 	}
    656       1.1    bouyer 
    657       1.2    bouyer 	DPRINTK("done");
    658       1.2    bouyer 	config_pending_decr();
    659      1.11    bouyer #ifdef DOM0OPS
    660      1.11    bouyer 	if (dom0) {
    661      1.11    bouyer 		int s;
    662      1.11    bouyer 		s = spltty();
    663      1.11    bouyer 		while (xenstored_ready == 0) {
    664      1.11    bouyer 			tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
    665      1.11    bouyer 			xenbus_probe(NULL);
    666      1.11    bouyer 		}
    667      1.11    bouyer 		splx(s);
    668      1.11    bouyer 	}
    669      1.11    bouyer #endif
    670       1.2    bouyer 	kthread_exit(0);
    671      1.23    cegger 
    672      1.23    cegger err0:
    673      1.23    cegger 	if (page)
    674      1.23    cegger 		uvm_km_free(kernel_map, page, PAGE_SIZE,
    675      1.23    cegger 				UVM_KMF_ZERO | UVM_KMF_WIRED);
    676      1.23    cegger 	kthread_exit(err);
    677       1.1    bouyer }
    678       1.1    bouyer 
    679       1.1    bouyer /*
    680       1.1    bouyer  * Local variables:
    681       1.1    bouyer  *  c-file-style: "linux"
    682       1.1    bouyer  *  indent-tabs-mode: t
    683       1.1    bouyer  *  c-indent-level: 8
    684       1.1    bouyer  *  c-basic-offset: 8
    685       1.1    bouyer  *  tab-width: 8
    686       1.1    bouyer  * End:
    687       1.1    bouyer  */
    688