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