Home | History | Annotate | Line # | Download | only in xenbus
xenbus_probe.c revision 1.2.2.1
      1  1.2.2.1    elad /* $NetBSD: xenbus_probe.c,v 1.2.2.1 2006/04/19 02:34:08 elad 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.2.2.1    elad __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.2.2.1 2006/04/19 02:34:08 elad Exp $");
     33      1.2  bouyer 
     34      1.1  bouyer #if 0
     35      1.1  bouyer #define DPRINTK(fmt, args...) \
     36      1.2  bouyer     printf("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __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.2.2.1    elad #include <uvm/uvm.h>
     49      1.2  bouyer 
     50      1.2  bouyer #include <machine/stdarg.h>
     51      1.2  bouyer 
     52      1.2  bouyer #include <machine/hypervisor.h>
     53      1.2  bouyer #include <machine/xenbus.h>
     54      1.2  bouyer #include <machine/evtchn.h>
     55      1.1  bouyer 
     56      1.1  bouyer #include "xenbus_comms.h"
     57      1.1  bouyer 
     58      1.1  bouyer extern struct semaphore xenwatch_mutex;
     59      1.1  bouyer 
     60      1.1  bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
     61      1.1  bouyer 
     62      1.2  bouyer static int  xenbus_match(struct device *, struct cfdata *, void *);
     63      1.2  bouyer static void xenbus_attach(struct device *, struct device *, void *);
     64      1.2  bouyer static int  xenbus_print(void *, const char *);
     65      1.2  bouyer 
     66      1.2  bouyer static void xenbus_kthread_create(void *);
     67      1.2  bouyer static void xenbus_probe_init(void *);
     68      1.2  bouyer 
     69  1.2.2.1    elad static struct xenbus_device *xenbus_lookup_device_path(const char *);
     70  1.2.2.1    elad 
     71      1.2  bouyer CFATTACH_DECL(xenbus, sizeof(struct device), xenbus_match, xenbus_attach,
     72      1.2  bouyer     NULL, NULL);
     73      1.2  bouyer 
     74  1.2.2.1    elad struct device *xenbus_sc;
     75  1.2.2.1    elad 
     76  1.2.2.1    elad SLIST_HEAD(, xenbus_device) xenbus_device_list;
     77      1.2  bouyer 
     78      1.2  bouyer int
     79      1.2  bouyer xenbus_match(struct device *parent, struct cfdata *match, void *aux)
     80      1.2  bouyer {
     81      1.2  bouyer 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
     82      1.2  bouyer 
     83      1.2  bouyer 	if (strcmp(xa->xa_device, "xenbus") == 0)
     84      1.2  bouyer 		return 1;
     85      1.2  bouyer 	return 0;
     86      1.2  bouyer }
     87      1.2  bouyer 
     88      1.2  bouyer static void
     89      1.2  bouyer xenbus_attach(struct device *parent, struct device *self, void *aux)
     90      1.2  bouyer {
     91      1.2  bouyer 	aprint_normal(": Xen Virtual Bus Interface\n");
     92  1.2.2.1    elad 	xenbus_sc = self;
     93      1.2  bouyer 	xb_init_comms(self);
     94      1.2  bouyer 	config_pending_incr();
     95      1.2  bouyer 	kthread_create(xenbus_kthread_create, NULL);
     96      1.2  bouyer }
     97      1.2  bouyer 
     98      1.2  bouyer void
     99      1.2  bouyer xenbus_kthread_create(void *unused)
    100      1.2  bouyer {
    101      1.2  bouyer 	struct proc *p;
    102      1.2  bouyer 	int err;
    103      1.2  bouyer 	err = kthread_create1(xenbus_probe_init, NULL, &p, "xenbus_probe");
    104      1.2  bouyer 	if (err)
    105      1.2  bouyer 		printf("kthread_create1(xenbus_probe): %d\n", err);
    106      1.2  bouyer }
    107      1.2  bouyer 
    108      1.2  bouyer 
    109      1.2  bouyer #if 0
    110      1.1  bouyer static char *kasprintf(const char *fmt, ...);
    111      1.1  bouyer 
    112      1.1  bouyer static struct notifier_block *xenstore_chain;
    113      1.1  bouyer 
    114      1.1  bouyer /* If something in array of ids matches this device, return it. */
    115      1.1  bouyer static const struct xenbus_device_id *
    116      1.1  bouyer match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
    117      1.1  bouyer {
    118      1.1  bouyer 	for (; !streq(arr->devicetype, ""); arr++) {
    119      1.1  bouyer 		if (streq(arr->devicetype, dev->devicetype))
    120      1.1  bouyer 			return arr;
    121      1.1  bouyer 	}
    122      1.1  bouyer 	return NULL;
    123      1.1  bouyer }
    124      1.1  bouyer 
    125      1.2  bouyer static int xenbus_match_device(struct device *_dev, struct device_driver *_drv)
    126      1.1  bouyer {
    127      1.1  bouyer 	struct xenbus_driver *drv = to_xenbus_driver(_drv);
    128      1.1  bouyer 
    129      1.1  bouyer 	if (!drv->ids)
    130      1.1  bouyer 		return 0;
    131      1.1  bouyer 
    132      1.1  bouyer 	return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
    133      1.1  bouyer }
    134      1.2  bouyer #endif
    135      1.1  bouyer 
    136      1.1  bouyer struct xen_bus_type
    137      1.1  bouyer {
    138      1.2  bouyer 	const char *root;
    139      1.1  bouyer 	unsigned int levels;
    140      1.1  bouyer };
    141      1.1  bouyer 
    142      1.2  bouyer #if 0
    143      1.1  bouyer /* device/<type>/<id> => <type>-<id> */
    144      1.2  bouyer static int
    145      1.2  bouyer frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
    146      1.1  bouyer {
    147      1.1  bouyer 	nodename = strchr(nodename, '/');
    148      1.1  bouyer 	if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
    149      1.1  bouyer 		printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
    150      1.2  bouyer 		return EINVAL;
    151      1.1  bouyer 	}
    152      1.1  bouyer 
    153      1.1  bouyer 	strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
    154      1.1  bouyer 	if (!strchr(bus_id, '/')) {
    155      1.1  bouyer 		printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
    156      1.2  bouyer 		return EINVAL;
    157      1.1  bouyer 	}
    158      1.1  bouyer 	*strchr(bus_id, '/') = '-';
    159      1.1  bouyer 	return 0;
    160      1.1  bouyer }
    161      1.2  bouyer #endif
    162      1.1  bouyer 
    163      1.2  bouyer static int
    164      1.2  bouyer read_otherend_details(struct xenbus_device *xendev,
    165      1.2  bouyer 				 const char *id_node, const char *path_node)
    166      1.2  bouyer {
    167      1.2  bouyer 	int err;
    168      1.2  bouyer 	char *val, *ep;
    169      1.1  bouyer 
    170      1.2  bouyer 	err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
    171      1.2  bouyer 	if (err) {
    172      1.2  bouyer 		printf("reading other end details %s from %s\n",
    173      1.2  bouyer 		    id_node, xendev->xbusd_path);
    174      1.2  bouyer 		xenbus_dev_fatal(xendev, err,
    175      1.2  bouyer 				 "reading other end details %s from %s",
    176      1.2  bouyer 				 id_node, xendev->xbusd_path);
    177      1.2  bouyer 		return err;
    178      1.2  bouyer 	}
    179      1.2  bouyer 	xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
    180      1.2  bouyer 	if (val[0] == '\0' || *ep != '\0') {
    181      1.2  bouyer 		printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
    182      1.2  bouyer 		xenbus_dev_fatal(xendev, err,
    183      1.2  bouyer 		    "reading other end details %s from %s: %s is not a number",
    184      1.2  bouyer 		    id_node, xendev->xbusd_path, val);
    185      1.2  bouyer 		free(val, M_DEVBUF);
    186      1.2  bouyer 		return EFTYPE;
    187      1.2  bouyer 	}
    188      1.2  bouyer 	free(val, M_DEVBUF);
    189      1.2  bouyer 	err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
    190      1.1  bouyer 	if (err) {
    191      1.2  bouyer 		printf("reading other end details %s from %s (%d)\n",
    192      1.2  bouyer 		    path_node, xendev->xbusd_path, err);
    193      1.1  bouyer 		xenbus_dev_fatal(xendev, err,
    194      1.2  bouyer 				 "reading other end details %s from %s",
    195      1.2  bouyer 				 path_node, xendev->xbusd_path);
    196      1.1  bouyer 		return err;
    197      1.1  bouyer 	}
    198  1.2.2.1    elad 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
    199  1.2.2.1    elad 	    xendev->xbusd_path, path_node, val);
    200      1.2  bouyer 	xendev->xbusd_otherend = val;
    201      1.2  bouyer 
    202      1.2  bouyer 	if (strlen(xendev->xbusd_otherend) == 0 ||
    203      1.2  bouyer 	    !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
    204      1.2  bouyer 		printf("missing other end from %s\n", xendev->xbusd_path);
    205      1.1  bouyer 		xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
    206      1.2  bouyer 				 xendev->xbusd_path);
    207      1.2  bouyer 		free(xendev->xbusd_otherend, M_DEVBUF);
    208      1.2  bouyer 		xendev->xbusd_otherend = NULL;
    209      1.2  bouyer 		return ENOENT;
    210      1.1  bouyer 	}
    211      1.1  bouyer 
    212      1.1  bouyer 	return 0;
    213      1.1  bouyer }
    214      1.1  bouyer 
    215      1.2  bouyer static int
    216      1.2  bouyer read_backend_details(struct xenbus_device *xendev)
    217      1.1  bouyer {
    218      1.1  bouyer 	return read_otherend_details(xendev, "backend-id", "backend");
    219      1.1  bouyer }
    220      1.1  bouyer 
    221      1.1  bouyer 
    222  1.2.2.1    elad #if 0 //#ifdef DOM0OPS
    223      1.2  bouyer static int
    224      1.2  bouyer read_frontend_details(struct xenbus_device *xendev)
    225      1.1  bouyer {
    226      1.1  bouyer 	return read_otherend_details(xendev, "frontend-id", "frontend");
    227      1.1  bouyer }
    228      1.2  bouyer #endif
    229      1.1  bouyer 
    230  1.2.2.1    elad #if unused
    231      1.2  bouyer static void
    232      1.2  bouyer free_otherend_details(struct xenbus_device *dev)
    233      1.1  bouyer {
    234      1.2  bouyer 	free(dev->xbusd_otherend, M_DEVBUF);
    235      1.2  bouyer 	dev->xbusd_otherend = NULL;
    236      1.1  bouyer }
    237  1.2.2.1    elad #endif
    238      1.1  bouyer 
    239      1.1  bouyer 
    240      1.2  bouyer static void
    241      1.2  bouyer free_otherend_watch(struct xenbus_device *dev)
    242      1.1  bouyer {
    243      1.2  bouyer 	if (dev->xbusd_otherend_watch.node) {
    244      1.2  bouyer 		unregister_xenbus_watch(&dev->xbusd_otherend_watch);
    245  1.2.2.1    elad 		free(dev->xbusd_otherend_watch.node, M_DEVBUF);
    246      1.2  bouyer 		dev->xbusd_otherend_watch.node = NULL;
    247      1.1  bouyer 	}
    248      1.1  bouyer }
    249      1.1  bouyer 
    250  1.2.2.1    elad #if 0
    251      1.1  bouyer 
    252      1.1  bouyer /* Bus type for frontend drivers. */
    253      1.1  bouyer static int xenbus_probe_frontend(const char *type, const char *name);
    254      1.2  bouyer #endif
    255      1.1  bouyer static struct xen_bus_type xenbus_frontend = {
    256      1.1  bouyer 	.root = "device",
    257      1.1  bouyer 	.levels = 2, 		/* device/type/<id> */
    258      1.1  bouyer };
    259      1.1  bouyer 
    260      1.2  bouyer #if 0
    261      1.1  bouyer /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
    262      1.2  bouyer static int
    263      1.2  bouyer backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
    264      1.1  bouyer {
    265      1.1  bouyer 	int domid, err;
    266      1.1  bouyer 	const char *devid, *type, *frontend;
    267      1.1  bouyer 	unsigned int typelen;
    268      1.1  bouyer 
    269      1.1  bouyer 	type = strchr(nodename, '/');
    270      1.1  bouyer 	if (!type)
    271      1.2  bouyer 		return EINVAL;
    272      1.1  bouyer 	type++;
    273      1.1  bouyer 	typelen = strcspn(type, "/");
    274      1.1  bouyer 	if (!typelen || type[typelen] != '/')
    275      1.2  bouyer 		return EINVAL;
    276      1.1  bouyer 
    277      1.1  bouyer 	devid = strrchr(nodename, '/') + 1;
    278      1.1  bouyer 
    279      1.1  bouyer 	err = xenbus_gather(NULL, nodename, "frontend-id", "%i", &domid,
    280      1.1  bouyer 			    "frontend", NULL, &frontend,
    281      1.1  bouyer 			    NULL);
    282      1.1  bouyer 	if (err)
    283      1.1  bouyer 		return err;
    284      1.1  bouyer 	if (strlen(frontend) == 0)
    285      1.2  bouyer 		err = ERANGE;
    286      1.1  bouyer 
    287      1.1  bouyer 	if (!err && !xenbus_exists(NULL, frontend, ""))
    288      1.2  bouyer 		err = ENOENT;
    289      1.1  bouyer 
    290      1.1  bouyer 	if (err) {
    291      1.2  bouyer 		free(frontend, M_DEVBUF);
    292      1.1  bouyer 		return err;
    293      1.1  bouyer 	}
    294      1.1  bouyer 
    295      1.1  bouyer 	if (snprintf(bus_id, BUS_ID_SIZE,
    296      1.1  bouyer 		     "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
    297      1.2  bouyer 		return ENOSPC;
    298      1.1  bouyer 	return 0;
    299      1.1  bouyer }
    300      1.1  bouyer 
    301      1.2  bouyer static int
    302      1.2  bouyer xenbus_hotplug_backend(struct device *dev, char **envp,
    303      1.1  bouyer 				  int num_envp, char *buffer, int buffer_size)
    304      1.1  bouyer {
    305      1.1  bouyer 	struct xenbus_device *xdev;
    306      1.1  bouyer 	struct xenbus_driver *drv = NULL;
    307      1.1  bouyer 	int i = 0;
    308      1.1  bouyer 	int length = 0;
    309      1.1  bouyer 	char *basepath_end;
    310      1.1  bouyer 	char *frontend_id;
    311      1.1  bouyer 
    312      1.1  bouyer 	DPRINTK("");
    313      1.1  bouyer 
    314      1.1  bouyer 	if (dev == NULL)
    315      1.2  bouyer 		return ENODEV;
    316      1.1  bouyer 
    317      1.1  bouyer 	xdev = to_xenbus_device(dev);
    318      1.1  bouyer 	if (xdev == NULL)
    319      1.2  bouyer 		return ENODEV;
    320      1.1  bouyer 
    321      1.1  bouyer 	if (dev->driver)
    322      1.1  bouyer 		drv = to_xenbus_driver(dev->driver);
    323      1.1  bouyer 
    324      1.1  bouyer 	/* stuff we want to pass to /sbin/hotplug */
    325      1.1  bouyer 	add_hotplug_env_var(envp, num_envp, &i,
    326      1.1  bouyer 			    buffer, buffer_size, &length,
    327      1.1  bouyer 			    "XENBUS_TYPE=%s", xdev->devicetype);
    328      1.1  bouyer 
    329      1.1  bouyer 	add_hotplug_env_var(envp, num_envp, &i,
    330      1.1  bouyer 			    buffer, buffer_size, &length,
    331      1.2  bouyer 			    "XENBUS_PATH=%s", xdev->xbusd_path);
    332      1.1  bouyer 
    333      1.1  bouyer 	add_hotplug_env_var(envp, num_envp, &i,
    334      1.1  bouyer 			    buffer, buffer_size, &length,
    335      1.2  bouyer 			    "XENBUS_BASE_PATH=%s", xdev->xbusd_path);
    336      1.1  bouyer 
    337      1.1  bouyer 	basepath_end = strrchr(envp[i - 1], '/');
    338      1.1  bouyer 	length -= strlen(basepath_end);
    339      1.1  bouyer 	*basepath_end = '\0';
    340      1.1  bouyer 	basepath_end = strrchr(envp[i - 1], '/');
    341      1.1  bouyer 	length -= strlen(basepath_end);
    342      1.1  bouyer 	*basepath_end = '\0';
    343      1.1  bouyer 
    344      1.1  bouyer 	basepath_end++;
    345      1.1  bouyer 	frontend_id = kmalloc(strlen(basepath_end) + 1, GFP_KERNEL);
    346      1.1  bouyer 	strcpy(frontend_id, basepath_end);
    347      1.1  bouyer 	add_hotplug_env_var(envp, num_envp, &i,
    348      1.1  bouyer 			    buffer, buffer_size, &length,
    349      1.1  bouyer 			    "XENBUS_FRONTEND_ID=%s", frontend_id);
    350      1.2  bouyer 	free(frontend_id, M_DEVBUF);
    351      1.1  bouyer 
    352      1.1  bouyer 	/* terminate, set to next free slot, shrink available space */
    353      1.1  bouyer 	envp[i] = NULL;
    354      1.1  bouyer 	envp = &envp[i];
    355      1.1  bouyer 	num_envp -= i;
    356      1.1  bouyer 	buffer = &buffer[length];
    357      1.1  bouyer 	buffer_size -= length;
    358      1.1  bouyer 
    359      1.1  bouyer 	if (drv && drv->hotplug)
    360      1.1  bouyer 		return drv->hotplug(xdev, envp, num_envp, buffer,
    361      1.1  bouyer 				    buffer_size);
    362      1.1  bouyer 
    363      1.1  bouyer 	return 0;
    364      1.1  bouyer }
    365      1.1  bouyer 
    366      1.1  bouyer static int xenbus_probe_backend(const char *type, const char *domid);
    367      1.2  bouyer #endif
    368      1.2  bouyer 
    369      1.1  bouyer static struct xen_bus_type xenbus_backend = {
    370      1.1  bouyer 	.root = "backend",
    371      1.1  bouyer 	.levels = 3, 		/* backend/type/<frontend>/<id> */
    372      1.1  bouyer };
    373      1.1  bouyer 
    374      1.2  bouyer static void
    375      1.2  bouyer otherend_changed(struct xenbus_watch *watch,
    376      1.1  bouyer 			     const char **vec, unsigned int len)
    377      1.1  bouyer {
    378  1.2.2.1    elad 	struct xenbus_device *xdev = watch->xbw_dev;
    379      1.1  bouyer 	XenbusState state;
    380      1.1  bouyer 
    381      1.1  bouyer 	/* Protect us against watches firing on old details when the otherend
    382      1.1  bouyer 	   details change, say immediately after a resume. */
    383  1.2.2.1    elad 	if (!xdev->xbusd_otherend ||
    384  1.2.2.1    elad 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
    385  1.2.2.1    elad 		    strlen(xdev->xbusd_otherend))) {
    386      1.1  bouyer 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
    387      1.1  bouyer 		return;
    388      1.1  bouyer 	}
    389      1.1  bouyer 
    390  1.2.2.1    elad 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
    391      1.1  bouyer 
    392      1.1  bouyer 	DPRINTK("state is %d, %s, %s",
    393  1.2.2.1    elad 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
    394  1.2.2.1    elad 	if (state == XenbusStateClosed) {
    395  1.2.2.1    elad 		int error;
    396  1.2.2.1    elad 		error = config_detach(xdev->xbusd_dev, DETACH_FORCE);
    397  1.2.2.1    elad 		if (error) {
    398  1.2.2.1    elad 			printf("could not detach %s: %d\n",
    399  1.2.2.1    elad 			    xdev->xbusd_dev->dv_xname, error);
    400  1.2.2.1    elad 			return;
    401  1.2.2.1    elad 		}
    402  1.2.2.1    elad 		xenbus_free_device(xdev);
    403  1.2.2.1    elad 		return;
    404  1.2.2.1    elad 	}
    405  1.2.2.1    elad 	if (xdev->xbusd_otherend_changed)
    406  1.2.2.1    elad 		xdev->xbusd_otherend_changed(xdev->xbusd_dev, state);
    407      1.1  bouyer }
    408      1.1  bouyer 
    409      1.2  bouyer static int
    410      1.2  bouyer talk_to_otherend(struct xenbus_device *dev)
    411      1.1  bouyer {
    412      1.1  bouyer 	free_otherend_watch(dev);
    413      1.1  bouyer 
    414      1.2  bouyer 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
    415  1.2.2.1    elad 				  &dev->xbusd_otherend_watch,
    416  1.2.2.1    elad 				  otherend_changed);
    417      1.1  bouyer }
    418      1.1  bouyer 
    419  1.2.2.1    elad #if 0
    420      1.1  bouyer 
    421      1.2  bouyer static int
    422      1.2  bouyer xenbus_dev_probe(struct device *_dev)
    423      1.1  bouyer {
    424      1.1  bouyer 	struct xenbus_device *dev = to_xenbus_device(_dev);
    425      1.1  bouyer 	struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
    426      1.1  bouyer 	const struct xenbus_device_id *id;
    427      1.1  bouyer 	int err;
    428      1.1  bouyer 
    429      1.1  bouyer 	DPRINTK("");
    430      1.1  bouyer 
    431      1.1  bouyer 	err = talk_to_otherend(dev);
    432      1.1  bouyer 	if (err) {
    433      1.1  bouyer 		printk(KERN_WARNING
    434      1.1  bouyer 		       "xenbus_probe: talk_to_otherend on %s failed.\n",
    435      1.2  bouyer 		       dev->xbusd_path);
    436      1.1  bouyer 		return err;
    437      1.1  bouyer 	}
    438      1.1  bouyer 
    439      1.1  bouyer 	if (!drv->probe) {
    440      1.2  bouyer 		err = ENODEV;
    441      1.1  bouyer 		goto fail;
    442      1.1  bouyer 	}
    443      1.1  bouyer 
    444      1.1  bouyer 	id = match_device(drv->ids, dev);
    445      1.1  bouyer 	if (!id) {
    446      1.2  bouyer 		err = ENODEV;
    447      1.1  bouyer 		goto fail;
    448      1.1  bouyer 	}
    449      1.1  bouyer 
    450      1.1  bouyer 	err = drv->probe(dev, id);
    451      1.1  bouyer 	if (err)
    452      1.1  bouyer 		goto fail;
    453      1.1  bouyer 
    454      1.1  bouyer 	return 0;
    455      1.1  bouyer fail:
    456      1.2  bouyer 	xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->xbusd_path);
    457      1.1  bouyer 	xenbus_switch_state(dev, NULL, XenbusStateClosed);
    458      1.2  bouyer 	return ENODEV;
    459      1.1  bouyer 
    460      1.1  bouyer }
    461      1.1  bouyer 
    462      1.2  bouyer static int
    463      1.2  bouyer xenbus_dev_remove(struct device *_dev)
    464      1.1  bouyer {
    465      1.1  bouyer 	struct xenbus_device *dev = to_xenbus_device(_dev);
    466      1.1  bouyer 	struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
    467      1.1  bouyer 
    468      1.1  bouyer 	DPRINTK("");
    469      1.1  bouyer 
    470      1.1  bouyer 	free_otherend_watch(dev);
    471      1.1  bouyer 	free_otherend_details(dev);
    472      1.1  bouyer 
    473      1.1  bouyer 	if (drv->remove)
    474      1.1  bouyer 		drv->remove(dev);
    475      1.1  bouyer 
    476      1.1  bouyer 	xenbus_switch_state(dev, NULL, XenbusStateClosed);
    477      1.1  bouyer 	return 0;
    478      1.1  bouyer }
    479      1.1  bouyer 
    480      1.2  bouyer static int
    481      1.2  bouyer xenbus_register_driver_common(struct xenbus_driver *drv,
    482      1.1  bouyer 					 struct xen_bus_type *bus)
    483      1.1  bouyer {
    484      1.1  bouyer 	int ret;
    485      1.1  bouyer 
    486      1.1  bouyer 	drv->driver.name = drv->name;
    487      1.1  bouyer 	drv->driver.bus = &bus->bus;
    488      1.1  bouyer 	drv->driver.owner = drv->owner;
    489      1.1  bouyer 	drv->driver.probe = xenbus_dev_probe;
    490      1.1  bouyer 	drv->driver.remove = xenbus_dev_remove;
    491      1.1  bouyer 
    492      1.1  bouyer 	down(&xenwatch_mutex);
    493      1.1  bouyer 	ret = driver_register(&drv->driver);
    494      1.1  bouyer 	up(&xenwatch_mutex);
    495      1.1  bouyer 	return ret;
    496      1.1  bouyer }
    497      1.1  bouyer 
    498      1.2  bouyer int
    499      1.2  bouyer xenbus_register_frontend(struct xenbus_driver *drv)
    500      1.1  bouyer {
    501      1.1  bouyer 	drv->read_otherend_details = read_backend_details;
    502      1.1  bouyer 
    503      1.1  bouyer 	return xenbus_register_driver_common(drv, &xenbus_frontend);
    504      1.1  bouyer }
    505      1.1  bouyer 
    506      1.2  bouyer int
    507      1.2  bouyer xenbus_register_backend(struct xenbus_driver *drv)
    508      1.1  bouyer {
    509      1.1  bouyer 	drv->read_otherend_details = read_frontend_details;
    510      1.1  bouyer 
    511      1.1  bouyer 	return xenbus_register_driver_common(drv, &xenbus_backend);
    512      1.1  bouyer }
    513      1.1  bouyer 
    514      1.2  bouyer void
    515      1.2  bouyer xenbus_unregister_driver(struct xenbus_driver *drv)
    516      1.1  bouyer {
    517      1.1  bouyer 	driver_unregister(&drv->driver);
    518      1.1  bouyer }
    519      1.1  bouyer 
    520      1.1  bouyer struct xb_find_info
    521      1.1  bouyer {
    522      1.1  bouyer 	struct xenbus_device *dev;
    523      1.1  bouyer 	const char *nodename;
    524      1.1  bouyer };
    525      1.1  bouyer 
    526      1.2  bouyer static int
    527      1.2  bouyer cmp_dev(struct device *dev, void *data)
    528      1.1  bouyer {
    529      1.1  bouyer 	struct xenbus_device *xendev = to_xenbus_device(dev);
    530      1.1  bouyer 	struct xb_find_info *info = data;
    531      1.1  bouyer 
    532      1.2  bouyer 	if (streq(xendev->xbusd_path, info->nodename)) {
    533      1.1  bouyer 		info->dev = xendev;
    534      1.1  bouyer 		get_device(dev);
    535      1.1  bouyer 		return 1;
    536      1.1  bouyer 	}
    537      1.1  bouyer 	return 0;
    538      1.1  bouyer }
    539      1.1  bouyer 
    540      1.2  bouyer struct xenbus_device *
    541      1.2  bouyer xenbus_device_find(const char *nodename, struct bus_type *bus)
    542      1.1  bouyer {
    543      1.1  bouyer 	struct xb_find_info info = { .dev = NULL, .nodename = nodename };
    544      1.1  bouyer 
    545      1.1  bouyer 	bus_for_each_dev(bus, NULL, &info, cmp_dev);
    546      1.1  bouyer 	return info.dev;
    547      1.1  bouyer }
    548      1.1  bouyer 
    549      1.2  bouyer static int
    550      1.2  bouyer cleanup_dev(struct device *dev, void *data)
    551      1.1  bouyer {
    552      1.1  bouyer 	struct xenbus_device *xendev = to_xenbus_device(dev);
    553      1.1  bouyer 	struct xb_find_info *info = data;
    554      1.1  bouyer 	int len = strlen(info->nodename);
    555      1.1  bouyer 
    556      1.1  bouyer 	DPRINTK("%s", info->nodename);
    557      1.1  bouyer 
    558      1.2  bouyer 	if (!strncmp(xendev->xbusd_path, info->nodename, len)) {
    559      1.1  bouyer 		info->dev = xendev;
    560      1.1  bouyer 		get_device(dev);
    561      1.1  bouyer 		return 1;
    562      1.1  bouyer 	}
    563      1.1  bouyer 	return 0;
    564      1.1  bouyer }
    565      1.1  bouyer 
    566      1.2  bouyer static void
    567      1.2  bouyer xenbus_cleanup_devices(const char *path, struct bus_type *bus)
    568      1.1  bouyer {
    569      1.1  bouyer 	struct xb_find_info info = { .nodename = path };
    570      1.1  bouyer 
    571      1.1  bouyer 	do {
    572      1.1  bouyer 		info.dev = NULL;
    573      1.1  bouyer 		bus_for_each_dev(bus, NULL, &info, cleanup_dev);
    574      1.1  bouyer 		if (info.dev) {
    575      1.1  bouyer 			device_unregister(&info.dev->dev);
    576      1.1  bouyer 			put_device(&info.dev->dev);
    577      1.1  bouyer 		}
    578      1.1  bouyer 	} while (info.dev);
    579      1.1  bouyer }
    580      1.1  bouyer 
    581      1.2  bouyer static void
    582      1.2  bouyer xenbus_dev_free(struct xenbus_device *xendev)
    583      1.1  bouyer {
    584      1.2  bouyer 	free(xendev, M_DEVBUF);
    585      1.1  bouyer }
    586      1.1  bouyer 
    587      1.2  bouyer static void
    588      1.2  bouyer xenbus_dev_release(struct device *dev)
    589      1.1  bouyer {
    590      1.1  bouyer 	if (dev) {
    591      1.1  bouyer 		xenbus_dev_free(to_xenbus_device(dev));
    592      1.1  bouyer 	}
    593      1.1  bouyer }
    594      1.1  bouyer 
    595      1.1  bouyer /* Simplified asprintf. */
    596      1.1  bouyer static char *kasprintf(const char *fmt, ...)
    597      1.1  bouyer {
    598      1.1  bouyer 	va_list ap;
    599      1.1  bouyer 	unsigned int len;
    600      1.1  bouyer 	char *p, dummy[1];
    601      1.1  bouyer 
    602      1.1  bouyer 	va_start(ap, fmt);
    603      1.1  bouyer 	/* FIXME: vsnprintf has a bug, NULL should work */
    604      1.1  bouyer 	len = vsnprintf(dummy, 0, fmt, ap);
    605      1.1  bouyer 	va_end(ap);
    606      1.1  bouyer 
    607      1.2  bouyer 	p = malloc(len + 1, M_DEVBUF, M_NOWAIT);
    608      1.1  bouyer 	if (!p)
    609      1.1  bouyer 		return NULL;
    610      1.1  bouyer 	va_start(ap, fmt);
    611      1.1  bouyer 	vsprintf(p, fmt, ap);
    612      1.1  bouyer 	va_end(ap);
    613      1.1  bouyer 	return p;
    614      1.1  bouyer }
    615      1.2  bouyer #endif
    616      1.1  bouyer 
    617      1.2  bouyer #if 0
    618      1.2  bouyer static ssize_t
    619      1.2  bouyer xendev_show_nodename(struct device *dev, char *buf)
    620      1.1  bouyer {
    621      1.1  bouyer 	return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
    622      1.1  bouyer }
    623      1.2  bouyer // XXX implement
    624      1.1  bouyer DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
    625      1.1  bouyer 
    626      1.2  bouyer static ssize_t
    627      1.2  bouyer xendev_show_devtype(struct device *dev, char *buf)
    628      1.1  bouyer {
    629      1.1  bouyer 	return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
    630      1.1  bouyer }
    631      1.1  bouyer DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
    632      1.1  bouyer 
    633      1.1  bouyer 
    634      1.2  bouyer static int
    635      1.2  bouyer xenbus_probe_node(struct xen_bus_type *bus,
    636      1.1  bouyer 			     const char *type,
    637      1.1  bouyer 			     const char *nodename)
    638      1.1  bouyer {
    639      1.1  bouyer #define CHECK_FAIL				\
    640      1.1  bouyer 	do {					\
    641      1.1  bouyer 		if (err)			\
    642      1.1  bouyer 			goto fail;		\
    643      1.1  bouyer 	}					\
    644      1.1  bouyer 	while (0)				\
    645      1.1  bouyer 
    646      1.1  bouyer 
    647      1.1  bouyer 	struct xenbus_device *xendev;
    648      1.1  bouyer 	size_t stringlen;
    649      1.1  bouyer 	char *tmpstring;
    650      1.1  bouyer 
    651      1.1  bouyer 	XenbusState state = xenbus_read_driver_state(nodename);
    652      1.1  bouyer 
    653      1.2  bouyer 	printf("xenbus_probe_node %s %s %s %d\n", bus->root, type, nodename, state);
    654      1.1  bouyer 	if (state != XenbusStateInitialising) {
    655      1.1  bouyer 		/* Device is not new, so ignore it.  This can happen if a
    656      1.1  bouyer 		   device is going away after switching to Closed.  */
    657      1.1  bouyer 		return 0;
    658      1.1  bouyer 	}
    659      1.1  bouyer 
    660      1.2  bouyer 
    661      1.1  bouyer 	stringlen = strlen(nodename) + 1 + strlen(type) + 1;
    662      1.2  bouyer 	xendev = malloc(sizeof(*xendev) + stringlen, M_DEVBUF, M_NOWAIT);
    663      1.1  bouyer 	if (!xendev)
    664      1.2  bouyer 		return ENOMEM;
    665      1.1  bouyer 	memset(xendev, 0, sizeof(*xendev));
    666      1.1  bouyer 
    667      1.1  bouyer 	/* Copy the strings into the extra space. */
    668      1.1  bouyer 
    669      1.1  bouyer 	tmpstring = (char *)(xendev + 1);
    670      1.1  bouyer 	strcpy(tmpstring, nodename);
    671      1.1  bouyer 	xendev->nodename = tmpstring;
    672      1.1  bouyer 
    673      1.1  bouyer 	tmpstring += strlen(tmpstring) + 1;
    674      1.1  bouyer 	strcpy(tmpstring, type);
    675      1.1  bouyer 	xendev->devicetype = tmpstring;
    676      1.1  bouyer 
    677      1.2  bouyer #if 0
    678      1.1  bouyer 	xendev->dev.parent = &bus->dev;
    679      1.1  bouyer 	xendev->dev.bus = &bus->bus;
    680      1.1  bouyer 	xendev->dev.release = xenbus_dev_release;
    681      1.1  bouyer 
    682      1.1  bouyer 	err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
    683      1.1  bouyer 	CHECK_FAIL;
    684      1.1  bouyer 
    685      1.1  bouyer 	/* Register with generic device framework. */
    686      1.1  bouyer 	err = device_register(&xendev->dev);
    687      1.1  bouyer 	CHECK_FAIL;
    688      1.1  bouyer 
    689      1.1  bouyer 	device_create_file(&xendev->dev, &dev_attr_nodename);
    690      1.1  bouyer 	device_create_file(&xendev->dev, &dev_attr_devtype);
    691      1.1  bouyer 
    692      1.2  bouyer #endif
    693      1.1  bouyer 	return 0;
    694      1.1  bouyer 
    695      1.1  bouyer #undef CHECK_FAIL
    696      1.2  bouyer #if 0
    697      1.1  bouyer fail:
    698      1.1  bouyer 	xenbus_dev_free(xendev);
    699      1.1  bouyer 	return err;
    700      1.2  bouyer #endif
    701      1.1  bouyer }
    702      1.2  bouyer #endif
    703      1.1  bouyer 
    704      1.2  bouyer #if 0
    705      1.1  bouyer /* device/<typename>/<name> */
    706      1.2  bouyer static int
    707      1.2  bouyer xenbus_probe_frontend(const char *type, const char *name)
    708      1.1  bouyer {
    709      1.1  bouyer 	char *nodename;
    710      1.1  bouyer 	int err;
    711      1.1  bouyer 
    712      1.1  bouyer 	nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
    713      1.1  bouyer 	if (!nodename)
    714      1.2  bouyer 		return ENOMEM;
    715      1.1  bouyer 
    716      1.1  bouyer 	DPRINTK("%s", nodename);
    717      1.1  bouyer 
    718      1.1  bouyer 	err = xenbus_probe_node(&xenbus_frontend, type, nodename);
    719      1.2  bouyer 	free(nodename, M_DEVBUF);
    720      1.1  bouyer 	return err;
    721      1.1  bouyer }
    722      1.1  bouyer 
    723      1.1  bouyer /* backend/<typename>/<frontend-uuid>/<name> */
    724      1.2  bouyer static int
    725      1.2  bouyer xenbus_probe_backend_unit(const char *dir,
    726      1.1  bouyer 				     const char *type,
    727      1.1  bouyer 				     const char *name)
    728      1.1  bouyer {
    729      1.1  bouyer 	char *nodename;
    730      1.1  bouyer 	int err;
    731      1.1  bouyer 
    732      1.1  bouyer 	nodename = kasprintf("%s/%s", dir, name);
    733      1.1  bouyer 	if (!nodename)
    734      1.2  bouyer 		return ENOMEM;
    735      1.1  bouyer 
    736      1.2  bouyer 	DPRINTK("%s", nodename);
    737      1.1  bouyer 
    738      1.1  bouyer 	err = xenbus_probe_node(&xenbus_backend, type, nodename);
    739      1.2  bouyer 	free(nodename, M_DEVBUF);
    740      1.1  bouyer 	return err;
    741      1.1  bouyer }
    742      1.1  bouyer 
    743      1.1  bouyer /* backend/<typename>/<frontend-domid> */
    744      1.2  bouyer static int
    745      1.2  bouyer xenbus_probe_backend(const char *type, const char *domid)
    746      1.1  bouyer {
    747      1.1  bouyer 	char *nodename;
    748      1.2  bouyer 	int err;
    749      1.1  bouyer 	char **dir;
    750      1.1  bouyer 	unsigned int i, dir_n = 0;
    751      1.1  bouyer 
    752      1.1  bouyer 	DPRINTK("");
    753      1.1  bouyer 
    754      1.1  bouyer 	nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
    755      1.1  bouyer 	if (!nodename)
    756      1.2  bouyer 		return ENOMEM;
    757      1.1  bouyer 
    758      1.2  bouyer 	err = xenbus_directory(NULL, nodename, "", &dir_n, &dir);
    759      1.2  bouyer 	DPRINTK("xenbus_probe_backend err %d dir_n %d", err, dir_n);
    760      1.2  bouyer 	if (err) {
    761      1.2  bouyer 		free(nodename, M_DEVBUF);
    762      1.2  bouyer 		return err;
    763      1.1  bouyer 	}
    764      1.1  bouyer 
    765      1.1  bouyer 	for (i = 0; i < dir_n; i++) {
    766      1.1  bouyer 		err = xenbus_probe_backend_unit(nodename, type, dir[i]);
    767      1.1  bouyer 		if (err)
    768      1.1  bouyer 			break;
    769      1.1  bouyer 	}
    770      1.2  bouyer 	free(dir, M_DEVBUF);
    771      1.2  bouyer 	free(nodename, M_DEVBUF);
    772      1.1  bouyer 	return err;
    773      1.1  bouyer }
    774      1.2  bouyer #endif
    775      1.1  bouyer 
    776  1.2.2.1    elad static struct xenbus_device *
    777  1.2.2.1    elad xenbus_lookup_device_path(const char *path)
    778  1.2.2.1    elad {
    779  1.2.2.1    elad 	struct xenbus_device *xbusd;
    780  1.2.2.1    elad 
    781  1.2.2.1    elad 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
    782  1.2.2.1    elad 		if (strcmp(xbusd->xbusd_path, path) == 0)
    783  1.2.2.1    elad 			return xbusd;
    784  1.2.2.1    elad 	}
    785  1.2.2.1    elad 	return NULL;
    786  1.2.2.1    elad }
    787  1.2.2.1    elad 
    788      1.2  bouyer static int
    789      1.2  bouyer xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
    790      1.1  bouyer {
    791      1.2  bouyer 	int err;
    792      1.1  bouyer 	char **dir;
    793      1.1  bouyer 	unsigned int dir_n = 0;
    794      1.1  bouyer 	int i;
    795      1.2  bouyer 	struct xenbus_device *xbusd;
    796      1.2  bouyer 	struct xenbusdev_attach_args xa;
    797      1.2  bouyer 	char *ep;
    798      1.2  bouyer 
    799      1.2  bouyer 	DPRINTK("probe %s type %s", bus->root, type);
    800      1.2  bouyer 	err = xenbus_directory(NULL, bus->root, type, &dir_n, &dir);
    801      1.2  bouyer 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    802      1.2  bouyer 	if (err)
    803      1.2  bouyer 		return err;
    804      1.1  bouyer 
    805      1.1  bouyer 	for (i = 0; i < dir_n; i++) {
    806  1.2.2.1    elad 		int msize;
    807  1.2.2.1    elad 		/*
    808  1.2.2.1    elad 		 * add size of path to size of xenbus_device. xenbus_device
    809  1.2.2.1    elad 		 * already has room for one char in xbusd_path.
    810  1.2.2.1    elad 		 */
    811  1.2.2.1    elad 		msize = sizeof(*xbusd) + strlen(bus->root) + strlen(type)
    812  1.2.2.1    elad 		    + strlen(dir[i]) + 2;
    813  1.2.2.1    elad 		xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
    814      1.2  bouyer 		if (xbusd == NULL)
    815      1.2  bouyer 			panic("can't malloc xbusd");
    816      1.2  bouyer 
    817      1.2  bouyer 		snprintf(__UNCONST(xbusd->xbusd_path),
    818  1.2.2.1    elad 		    msize - sizeof(*xbusd) + 1, "%s/%s/%s",
    819      1.2  bouyer 		    bus->root, type, dir[i]);
    820  1.2.2.1    elad 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
    821  1.2.2.1    elad 			/* device already registered */
    822  1.2.2.1    elad 			free(xbusd, M_DEVBUF);
    823  1.2.2.1    elad 			continue;
    824  1.2.2.1    elad 		}
    825  1.2.2.1    elad 
    826  1.2.2.1    elad 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
    827      1.2  bouyer 		DPRINTK("xenbus_probe_device_type probe %s\n",
    828      1.2  bouyer 		    xbusd->xbusd_path);
    829      1.2  bouyer 		xa.xa_xbusd = xbusd;
    830      1.2  bouyer 		xa.xa_type = type;
    831      1.2  bouyer 		xa.xa_id = strtoul(dir[i], &ep, 0);
    832      1.2  bouyer 		if (dir[i][0] == '\0' || *ep != '\0') {
    833      1.2  bouyer 			printf("xenbus device type %s: id %s is not a number\n",
    834      1.2  bouyer 			    type, dir[i]);
    835      1.2  bouyer 			err = EFTYPE;
    836      1.2  bouyer 			free(xbusd, M_DEVBUF);
    837      1.2  bouyer 			break;
    838      1.2  bouyer 		}
    839      1.2  bouyer 		err = read_backend_details(xbusd);
    840      1.2  bouyer 		if (err != 0) {
    841      1.2  bouyer 			printf("xenbus: can't get backend details "
    842      1.2  bouyer 			    "for %s (%d)\n", xbusd->xbusd_path, err);
    843      1.1  bouyer 			break;
    844      1.2  bouyer 		}
    845  1.2.2.1    elad 		xbusd->xbusd_dev = config_found_ia(xenbus_sc, "xenbus", &xa,
    846  1.2.2.1    elad 		    xenbus_print);
    847  1.2.2.1    elad 		if (xbusd->xbusd_dev == NULL)
    848      1.2  bouyer 			free(xbusd, M_DEVBUF);
    849  1.2.2.1    elad 		else {
    850  1.2.2.1    elad 			SLIST_INSERT_HEAD(&xenbus_device_list,
    851  1.2.2.1    elad 			    xbusd, xbusd_entries);
    852  1.2.2.1    elad 			talk_to_otherend(xbusd);
    853  1.2.2.1    elad 		}
    854      1.1  bouyer 	}
    855      1.2  bouyer 	free(dir, M_DEVBUF);
    856      1.1  bouyer 	return err;
    857      1.1  bouyer }
    858      1.1  bouyer 
    859      1.2  bouyer static int
    860      1.2  bouyer xenbus_print(void *aux, const char *pnp)
    861      1.2  bouyer {
    862      1.2  bouyer 	struct xenbusdev_attach_args *xa = aux;
    863      1.2  bouyer 
    864      1.2  bouyer 	if (pnp) {
    865      1.2  bouyer 		if (strcmp(xa->xa_type, "vbd") == 0)
    866      1.2  bouyer 			aprint_normal("xbd");
    867      1.2  bouyer 		else if (strcmp(xa->xa_type, "vif") == 0)
    868      1.2  bouyer 			aprint_normal("xennet");
    869      1.2  bouyer 		else
    870      1.2  bouyer 			aprint_normal("unknown type %s", xa->xa_type);
    871      1.2  bouyer 		aprint_normal(" at %s", pnp);
    872      1.2  bouyer 	}
    873      1.2  bouyer 	aprint_normal(" id %d", xa->xa_id);
    874      1.2  bouyer 	return(UNCONF);
    875      1.2  bouyer }
    876      1.2  bouyer 
    877      1.2  bouyer static int
    878      1.2  bouyer xenbus_probe_devices(struct xen_bus_type *bus)
    879      1.1  bouyer {
    880      1.2  bouyer 	int err;
    881      1.1  bouyer 	char **dir;
    882      1.1  bouyer 	unsigned int i, dir_n;
    883      1.1  bouyer 
    884      1.2  bouyer 	DPRINTK("probe %s", bus->root);
    885      1.2  bouyer 	err = xenbus_directory(NULL, bus->root, "", &dir_n, &dir);
    886      1.2  bouyer 	DPRINTK("directory err %d dir_n %d", err, dir_n);
    887      1.2  bouyer 	if (err)
    888      1.2  bouyer 		return err;
    889      1.1  bouyer 
    890      1.1  bouyer 	for (i = 0; i < dir_n; i++) {
    891      1.1  bouyer 		err = xenbus_probe_device_type(bus, dir[i]);
    892      1.1  bouyer 		if (err)
    893      1.1  bouyer 			break;
    894      1.1  bouyer 	}
    895      1.2  bouyer 	free(dir, M_DEVBUF);
    896      1.1  bouyer 	return err;
    897      1.1  bouyer }
    898      1.1  bouyer 
    899  1.2.2.1    elad int
    900  1.2.2.1    elad xenbus_free_device(struct xenbus_device *xbusd)
    901  1.2.2.1    elad {
    902  1.2.2.1    elad 	KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
    903  1.2.2.1    elad 	SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
    904  1.2.2.1    elad 	free_otherend_watch(xbusd);
    905  1.2.2.1    elad 	free(xbusd->xbusd_otherend, M_DEVBUF);
    906  1.2.2.1    elad 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
    907  1.2.2.1    elad 	free(xbusd, M_DEVBUF);
    908  1.2.2.1    elad 	return 0;
    909  1.2.2.1    elad }
    910  1.2.2.1    elad 
    911      1.2  bouyer #if 0
    912      1.2  bouyer static unsigned int
    913      1.2  bouyer char_count(const char *str, char c)
    914      1.1  bouyer {
    915      1.1  bouyer 	unsigned int i, ret = 0;
    916      1.1  bouyer 
    917      1.1  bouyer 	for (i = 0; str[i]; i++)
    918      1.1  bouyer 		if (str[i] == c)
    919      1.1  bouyer 			ret++;
    920      1.1  bouyer 	return ret;
    921      1.1  bouyer }
    922      1.1  bouyer 
    923      1.2  bouyer static int
    924      1.2  bouyer strsep_len(const char *str, char c, unsigned int len)
    925      1.1  bouyer {
    926      1.1  bouyer 	unsigned int i;
    927      1.1  bouyer 
    928      1.1  bouyer 	for (i = 0; str[i]; i++)
    929      1.1  bouyer 		if (str[i] == c) {
    930      1.1  bouyer 			if (len == 0)
    931      1.1  bouyer 				return i;
    932      1.1  bouyer 			len--;
    933      1.1  bouyer 		}
    934      1.1  bouyer 	return (len == 0) ? i : -ERANGE;
    935      1.1  bouyer }
    936      1.1  bouyer 
    937      1.2  bouyer static void
    938      1.2  bouyer dev_changed(const char *node, struct xen_bus_type *bus)
    939      1.1  bouyer {
    940      1.1  bouyer 	int exists, rootlen;
    941      1.1  bouyer 	struct xenbus_device *dev;
    942      1.1  bouyer 	char type[BUS_ID_SIZE];
    943      1.1  bouyer 	const char *p, *root;
    944      1.1  bouyer 
    945      1.1  bouyer 	if (char_count(node, '/') < 2)
    946      1.1  bouyer  		return;
    947      1.1  bouyer 
    948      1.1  bouyer 	exists = xenbus_exists(NULL, node, "");
    949      1.1  bouyer 	if (!exists) {
    950      1.1  bouyer 		xenbus_cleanup_devices(node, &bus->bus);
    951      1.1  bouyer 		return;
    952      1.1  bouyer 	}
    953      1.1  bouyer 
    954      1.1  bouyer 	/* backend/<type>/... or device/<type>/... */
    955      1.1  bouyer 	p = strchr(node, '/') + 1;
    956      1.1  bouyer 	snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
    957      1.1  bouyer 	type[BUS_ID_SIZE-1] = '\0';
    958      1.1  bouyer 
    959      1.1  bouyer 	rootlen = strsep_len(node, '/', bus->levels);
    960      1.1  bouyer 	if (rootlen < 0)
    961      1.1  bouyer 		return;
    962      1.1  bouyer 	root = kasprintf("%.*s", rootlen, node);
    963      1.1  bouyer 	if (!root)
    964      1.1  bouyer 		return;
    965      1.1  bouyer 
    966      1.1  bouyer 	dev = xenbus_device_find(root, &bus->bus);
    967      1.1  bouyer 	if (!dev)
    968      1.1  bouyer 		xenbus_probe_node(bus, type, root);
    969      1.1  bouyer 	else
    970      1.1  bouyer 		put_device(&dev->dev);
    971      1.1  bouyer 
    972      1.2  bouyer 	free(root, M_DEVBUF);
    973      1.1  bouyer }
    974      1.2  bouyer #endif
    975      1.1  bouyer 
    976      1.2  bouyer static void
    977      1.2  bouyer frontend_changed(struct xenbus_watch *watch,
    978      1.1  bouyer 			     const char **vec, unsigned int len)
    979      1.1  bouyer {
    980      1.1  bouyer 	DPRINTK("");
    981  1.2.2.1    elad 	//printf("frontend_changed %s\n", vec[XS_WATCH_PATH]);
    982  1.2.2.1    elad 	xenbus_probe_devices(&xenbus_frontend);
    983      1.1  bouyer }
    984      1.1  bouyer 
    985      1.2  bouyer static void
    986      1.2  bouyer backend_changed(struct xenbus_watch *watch,
    987      1.1  bouyer 			    const char **vec, unsigned int len)
    988      1.1  bouyer {
    989      1.1  bouyer 	DPRINTK("");
    990      1.1  bouyer 
    991      1.2  bouyer 	printf("backend_changed %s\n", vec[XS_WATCH_PATH]);
    992      1.2  bouyer 	//dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
    993      1.1  bouyer }
    994      1.1  bouyer 
    995      1.2  bouyer 
    996      1.1  bouyer /* We watch for devices appearing and vanishing. */
    997  1.2.2.1    elad static struct xenbus_watch fe_watch;
    998      1.1  bouyer 
    999  1.2.2.1    elad static struct xenbus_watch be_watch;
   1000      1.1  bouyer 
   1001      1.2  bouyer #if 0
   1002      1.1  bouyer static int suspend_dev(struct device *dev, void *data)
   1003      1.1  bouyer {
   1004      1.1  bouyer 	int err = 0;
   1005      1.1  bouyer 	struct xenbus_driver *drv;
   1006      1.1  bouyer 	struct xenbus_device *xdev;
   1007      1.1  bouyer 
   1008      1.1  bouyer 	DPRINTK("");
   1009      1.1  bouyer 
   1010      1.1  bouyer 	if (dev->driver == NULL)
   1011      1.1  bouyer 		return 0;
   1012      1.1  bouyer 	drv = to_xenbus_driver(dev->driver);
   1013      1.1  bouyer 	xdev = container_of(dev, struct xenbus_device, dev);
   1014      1.1  bouyer 	if (drv->suspend)
   1015      1.1  bouyer 		err = drv->suspend(xdev);
   1016      1.1  bouyer 	if (err)
   1017      1.1  bouyer 		printk(KERN_WARNING
   1018      1.1  bouyer 		       "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
   1019      1.1  bouyer 	return 0;
   1020      1.1  bouyer }
   1021      1.1  bouyer 
   1022      1.2  bouyer static int
   1023      1.2  bouyer resume_dev(struct device *dev, void *data)
   1024      1.1  bouyer {
   1025      1.1  bouyer 	int err;
   1026      1.1  bouyer 	struct xenbus_driver *drv;
   1027      1.1  bouyer 	struct xenbus_device *xdev;
   1028      1.1  bouyer 
   1029      1.1  bouyer 	DPRINTK("");
   1030      1.1  bouyer 
   1031      1.1  bouyer 	if (dev->driver == NULL)
   1032      1.1  bouyer 		return 0;
   1033      1.1  bouyer 	drv = to_xenbus_driver(dev->driver);
   1034      1.1  bouyer 	xdev = container_of(dev, struct xenbus_device, dev);
   1035      1.1  bouyer 
   1036      1.1  bouyer 	err = talk_to_otherend(xdev);
   1037      1.1  bouyer 	if (err) {
   1038      1.1  bouyer 		printk(KERN_WARNING
   1039      1.1  bouyer 		       "xenbus: resume (talk_to_otherend) %s failed: %i\n",
   1040      1.1  bouyer 		       dev->bus_id, err);
   1041      1.1  bouyer 		return err;
   1042      1.1  bouyer 	}
   1043      1.1  bouyer 
   1044      1.1  bouyer 	if (drv->resume)
   1045      1.1  bouyer 		err = drv->resume(xdev);
   1046      1.1  bouyer 	if (err)
   1047      1.1  bouyer 		printk(KERN_WARNING
   1048      1.1  bouyer 		       "xenbus: resume %s failed: %i\n", dev->bus_id, err);
   1049      1.1  bouyer 	return err;
   1050      1.1  bouyer }
   1051      1.1  bouyer 
   1052      1.2  bouyer void
   1053      1.2  bouyer xenbus_suspend(void)
   1054      1.1  bouyer {
   1055      1.1  bouyer 	DPRINTK("");
   1056      1.1  bouyer 
   1057      1.1  bouyer 	bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
   1058      1.1  bouyer 	bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
   1059      1.1  bouyer 	xs_suspend();
   1060      1.1  bouyer }
   1061      1.1  bouyer 
   1062      1.2  bouyer void xenbus_resume(struct device *dev)
   1063      1.1  bouyer {
   1064      1.2  bouyer 	xb_init_comms(dev);
   1065      1.1  bouyer 	xs_resume();
   1066      1.1  bouyer 	bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
   1067      1.1  bouyer 	bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
   1068      1.1  bouyer }
   1069      1.2  bouyer #endif
   1070      1.1  bouyer 
   1071      1.1  bouyer 
   1072      1.1  bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
   1073      1.1  bouyer int xenstored_ready = 0;
   1074      1.1  bouyer 
   1075      1.1  bouyer 
   1076      1.2  bouyer #if 0
   1077      1.2  bouyer int
   1078      1.2  bouyer register_xenstore_notifier(struct notifier_block *nb)
   1079      1.1  bouyer {
   1080      1.1  bouyer 	int ret = 0;
   1081      1.1  bouyer 
   1082      1.1  bouyer 	if (xenstored_ready > 0)
   1083      1.1  bouyer 		ret = nb->notifier_call(nb, 0, NULL);
   1084      1.1  bouyer 	else
   1085      1.1  bouyer 		notifier_chain_register(&xenstore_chain, nb);
   1086      1.1  bouyer 
   1087      1.1  bouyer 	return ret;
   1088      1.1  bouyer }
   1089      1.1  bouyer 
   1090      1.1  bouyer void unregister_xenstore_notifier(struct notifier_block *nb)
   1091      1.1  bouyer {
   1092      1.1  bouyer 	notifier_chain_unregister(&xenstore_chain, nb);
   1093      1.1  bouyer }
   1094      1.2  bouyer #endif
   1095      1.1  bouyer 
   1096      1.1  bouyer 
   1097      1.1  bouyer 
   1098      1.2  bouyer void
   1099      1.2  bouyer xenbus_probe(void *unused)
   1100      1.1  bouyer {
   1101      1.2  bouyer 	KASSERT((xenstored_ready > 0));
   1102      1.1  bouyer 
   1103      1.1  bouyer 	/* Enumerate devices in xenstore. */
   1104      1.1  bouyer 	xenbus_probe_devices(&xenbus_frontend);
   1105      1.1  bouyer 	xenbus_probe_devices(&xenbus_backend);
   1106      1.1  bouyer 
   1107      1.1  bouyer 	/* Watch for changes. */
   1108  1.2.2.1    elad 	fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
   1109  1.2.2.1    elad 	strcpy(fe_watch.node, "device");
   1110  1.2.2.1    elad 	fe_watch.xbw_callback = frontend_changed;
   1111      1.1  bouyer 	register_xenbus_watch(&fe_watch);
   1112  1.2.2.1    elad 	be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
   1113  1.2.2.1    elad 	strcpy(be_watch.node, "backend");
   1114  1.2.2.1    elad 	be_watch.xbw_callback = backend_changed;
   1115      1.1  bouyer 	register_xenbus_watch(&be_watch);
   1116      1.1  bouyer 
   1117      1.1  bouyer 	/* Notify others that xenstore is up */
   1118      1.2  bouyer 	//notifier_call_chain(&xenstore_chain, 0, NULL);
   1119      1.1  bouyer }
   1120      1.1  bouyer 
   1121      1.2  bouyer static void
   1122      1.2  bouyer xenbus_probe_init(void *unused)
   1123      1.1  bouyer {
   1124      1.1  bouyer 	int err = 0, dom0;
   1125      1.1  bouyer 
   1126      1.1  bouyer 	DPRINTK("");
   1127      1.1  bouyer 
   1128  1.2.2.1    elad 	SLIST_INIT(&xenbus_device_list);
   1129      1.1  bouyer 
   1130      1.1  bouyer 	/*
   1131      1.1  bouyer 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
   1132      1.1  bouyer 	*/
   1133      1.2  bouyer 	dom0 = (xen_start_info.store_evtchn == 0);
   1134      1.1  bouyer 	if (dom0) {
   1135  1.2.2.1    elad #if defined(DOM0OPS)
   1136  1.2.2.1    elad 		vaddr_t page;
   1137  1.2.2.1    elad 		paddr_t ma;
   1138      1.1  bouyer 		evtchn_op_t op = { 0 };
   1139      1.1  bouyer 		int ret;
   1140      1.1  bouyer 
   1141      1.1  bouyer 		/* Allocate page. */
   1142  1.2.2.1    elad 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   1143  1.2.2.1    elad 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
   1144      1.1  bouyer 		if (!page)
   1145  1.2.2.1    elad 			panic("can't get xenstore page");
   1146      1.1  bouyer 
   1147  1.2.2.1    elad 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
   1148  1.2.2.1    elad 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
   1149  1.2.2.1    elad 		xenstore_interface = (void *)page;
   1150      1.1  bouyer 
   1151      1.1  bouyer 		/* Next allocate a local port which xenstored can bind to */
   1152      1.1  bouyer 		op.cmd = EVTCHNOP_alloc_unbound;
   1153      1.1  bouyer 		op.u.alloc_unbound.dom        = DOMID_SELF;
   1154      1.1  bouyer 		op.u.alloc_unbound.remote_dom = 0;
   1155      1.1  bouyer 
   1156      1.1  bouyer 		ret = HYPERVISOR_event_channel_op(&op);
   1157  1.2.2.1    elad 		if (ret)
   1158      1.2  bouyer 			panic("can't register xenstore event");
   1159  1.2.2.1    elad 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
   1160      1.1  bouyer 
   1161  1.2.2.1    elad 		/* And finally publish the above info in /kern/xen */
   1162  1.2.2.1    elad 		xenbus_kernfs_init();
   1163  1.2.2.1    elad #else /* DOM0OPS */
   1164  1.2.2.1    elad 		return ; /* can't get a working xenstore in this case */
   1165  1.2.2.1    elad #endif /* DOM0OPS */
   1166      1.1  bouyer 	}
   1167      1.1  bouyer 
   1168      1.1  bouyer 	/* Initialize the interface to xenstore. */
   1169      1.1  bouyer 	err = xs_init();
   1170      1.1  bouyer 	if (err) {
   1171      1.2  bouyer 		printf("XENBUS: Error initializing xenstore comms: %i\n", err);
   1172      1.2  bouyer 		kthread_exit(err);
   1173      1.1  bouyer 	}
   1174      1.1  bouyer 
   1175      1.1  bouyer 	if (!dom0) {
   1176      1.1  bouyer 		xenstored_ready = 1;
   1177      1.1  bouyer 		xenbus_probe(NULL);
   1178      1.1  bouyer 	}
   1179      1.1  bouyer 
   1180      1.2  bouyer 	DPRINTK("done");
   1181      1.2  bouyer 	config_pending_decr();
   1182      1.2  bouyer 	kthread_exit(0);
   1183      1.1  bouyer }
   1184      1.1  bouyer 
   1185      1.1  bouyer /*
   1186      1.1  bouyer  * Local variables:
   1187      1.1  bouyer  *  c-file-style: "linux"
   1188      1.1  bouyer  *  indent-tabs-mode: t
   1189      1.1  bouyer  *  c-indent-level: 8
   1190      1.1  bouyer  *  c-basic-offset: 8
   1191      1.1  bouyer  *  tab-width: 8
   1192      1.1  bouyer  * End:
   1193      1.1  bouyer  */
   1194