1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have (backend half).
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
7 * Copyright (C) 2007 Solarflare Communications, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define DPRINTK(fmt, args...) \
35 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
36 __func__, __LINE__, ##args)
38 #include <linux/kernel.h>
39 #include <linux/err.h>
40 #include <linux/string.h>
41 #include <linux/ctype.h>
42 #include <linux/fcntl.h>
44 #include <linux/notifier.h>
45 #include <linux/export.h>
48 #include <asm/pgtable.h>
49 #include <asm/xen/hypervisor.h>
50 #include <asm/hypervisor.h>
51 #include <xen/xenbus.h>
52 #include <xen/features.h>
54 #include "xenbus_comms.h"
55 #include "xenbus_probe.h"
57 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
58 static int backend_bus_id(char bus_id
[XEN_BUS_ID_SIZE
], const char *nodename
)
61 const char *devid
, *type
, *frontend
;
64 type
= strchr(nodename
, '/');
68 typelen
= strcspn(type
, "/");
69 if (!typelen
|| type
[typelen
] != '/')
72 devid
= strrchr(nodename
, '/') + 1;
74 err
= xenbus_gather(XBT_NIL
, nodename
, "frontend-id", "%i", &domid
,
75 "frontend", NULL
, &frontend
,
79 if (strlen(frontend
) == 0)
81 if (!err
&& !xenbus_exists(XBT_NIL
, frontend
, ""))
88 if (snprintf(bus_id
, XEN_BUS_ID_SIZE
, "%.*s-%i-%s",
89 typelen
, type
, domid
, devid
) >= XEN_BUS_ID_SIZE
)
94 static int xenbus_uevent_backend(struct device
*dev
,
95 struct kobj_uevent_env
*env
)
97 struct xenbus_device
*xdev
;
98 struct xenbus_driver
*drv
;
99 struct xen_bus_type
*bus
;
106 xdev
= to_xenbus_device(dev
);
107 bus
= container_of(xdev
->dev
.bus
, struct xen_bus_type
, bus
);
109 if (add_uevent_var(env
, "MODALIAS=xen-backend:%s", xdev
->devicetype
))
112 /* stuff we want to pass to /sbin/hotplug */
113 if (add_uevent_var(env
, "XENBUS_TYPE=%s", xdev
->devicetype
))
116 if (add_uevent_var(env
, "XENBUS_PATH=%s", xdev
->nodename
))
119 if (add_uevent_var(env
, "XENBUS_BASE_PATH=%s", bus
->root
))
123 drv
= to_xenbus_driver(dev
->driver
);
124 if (drv
&& drv
->uevent
)
125 return drv
->uevent(xdev
, env
);
131 /* backend/<typename>/<frontend-uuid>/<name> */
132 static int xenbus_probe_backend_unit(struct xen_bus_type
*bus
,
140 nodename
= kasprintf(GFP_KERNEL
, "%s/%s", dir
, name
);
144 DPRINTK("%s\n", nodename
);
146 err
= xenbus_probe_node(bus
, type
, nodename
);
151 /* backend/<typename>/<frontend-domid> */
152 static int xenbus_probe_backend(struct xen_bus_type
*bus
, const char *type
,
158 unsigned int i
, dir_n
= 0;
162 nodename
= kasprintf(GFP_KERNEL
, "%s/%s/%s", bus
->root
, type
, domid
);
166 dir
= xenbus_directory(XBT_NIL
, nodename
, "", &dir_n
);
172 for (i
= 0; i
< dir_n
; i
++) {
173 err
= xenbus_probe_backend_unit(bus
, nodename
, type
, dir
[i
]);
182 static void frontend_changed(struct xenbus_watch
*watch
,
183 const char **vec
, unsigned int len
)
185 xenbus_otherend_changed(watch
, vec
, len
, 0);
188 static struct xen_bus_type xenbus_backend
= {
190 .levels
= 3, /* backend/type/<frontend>/<id> */
191 .get_bus_id
= backend_bus_id
,
192 .probe
= xenbus_probe_backend
,
193 .otherend_changed
= frontend_changed
,
195 .name
= "xen-backend",
196 .match
= xenbus_match
,
197 .uevent
= xenbus_uevent_backend
,
198 .probe
= xenbus_dev_probe
,
199 .remove
= xenbus_dev_remove
,
200 .shutdown
= xenbus_dev_shutdown
,
201 .dev_attrs
= xenbus_dev_attrs
,
205 static void backend_changed(struct xenbus_watch
*watch
,
206 const char **vec
, unsigned int len
)
210 xenbus_dev_changed(vec
[XS_WATCH_PATH
], &xenbus_backend
);
213 static struct xenbus_watch be_watch
= {
215 .callback
= backend_changed
,
218 static int read_frontend_details(struct xenbus_device
*xendev
)
220 return xenbus_read_otherend_details(xendev
, "frontend-id", "frontend");
223 int xenbus_dev_is_online(struct xenbus_device
*dev
)
227 rc
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "online", "%d", &val
);
229 val
= 0; /* no online node present */
233 EXPORT_SYMBOL_GPL(xenbus_dev_is_online
);
235 int xenbus_register_backend(struct xenbus_driver
*drv
)
237 drv
->read_otherend_details
= read_frontend_details
;
239 return xenbus_register_driver_common(drv
, &xenbus_backend
);
241 EXPORT_SYMBOL_GPL(xenbus_register_backend
);
243 static int backend_probe_and_watch(struct notifier_block
*notifier
,
247 /* Enumerate devices in xenstore and watch for changes. */
248 xenbus_probe_devices(&xenbus_backend
);
249 register_xenbus_watch(&be_watch
);
254 static int __init
xenbus_probe_backend_init(void)
256 static struct notifier_block xenstore_notifier
= {
257 .notifier_call
= backend_probe_and_watch
263 /* Register ourselves with the kernel bus subsystem */
264 err
= bus_register(&xenbus_backend
.bus
);
268 register_xenstore_notifier(&xenstore_notifier
);
272 subsys_initcall(xenbus_probe_backend_init
);