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
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #define DPRINTK(fmt, args...) \
34 printf("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
36 #define DPRINTK(fmt, args...) ((void)0)
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/cdefs.h>
47 #include <sys/eventhandler.h>
48 #include <sys/errno.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
53 #include <sys/systm.h>
54 #include <sys/syslog.h>
59 #include <machine/xen/xen-os.h>
60 #include <xen/hypervisor.h>
61 #include <machine/xen/xenbus.h>
62 #include <machine/stdarg.h>
64 #include <xen/evtchn.h>
65 #include <xen/xenbus/xenbus_comms.h>
67 #define BUG_ON PANIC_IF
68 #define semaphore sema
69 #define rw_semaphore sema
70 #define DEFINE_SPINLOCK(lock) struct mtx lock
71 #define DECLARE_MUTEX(lock) struct sema lock
73 #define list_del(head, ent) TAILQ_REMOVE(head, ent, list)
74 #define simple_strtoul strtoul
75 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
76 #define list_empty TAILQ_EMPTY
78 extern struct xendev_list_head xenbus_device_backend_list;
80 static int xenbus_uevent_backend(struct device *dev, char **envp,
81 int num_envp, char *buffer, int buffer_size);
83 static int xenbus_probe_backend(const char *type, const char *domid);
85 static int read_frontend_details(struct xenbus_device *xendev)
87 return read_otherend_details(xendev, "frontend-id", "frontend");
90 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
91 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
94 const char *devid, *type, *frontend;
97 type = strchr(nodename, '/');
101 typelen = strcspn(type, "/");
102 if (!typelen || type[typelen] != '/')
105 devid = strrchr(nodename, '/') + 1;
107 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
108 "frontend", NULL, &frontend,
112 if (strlen(frontend) == 0)
114 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
121 if (snprintf(bus_id, BUS_ID_SIZE,
122 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
127 static struct xen_bus_type xenbus_backend = {
129 .levels = 3, /* backend/type/<frontend>/<id> */
130 .get_bus_id = backend_bus_id,
131 .probe = xenbus_probe_backend,
132 .bus = &xenbus_device_backend_list,
137 .name = "xen-backend",
138 .match = xenbus_match,
139 .probe = xenbus_dev_probe,
140 .remove = xenbus_dev_remove,
141 // .shutdown = xenbus_dev_shutdown,
142 .uevent = xenbus_uevent_backend,
145 .bus_id = "xen-backend",
151 static int xenbus_uevent_backend(struct device *dev, char **envp,
152 int num_envp, char *buffer, int buffer_size)
154 struct xenbus_device *xdev;
155 struct xenbus_driver *drv;
164 xdev = to_xenbus_device(dev);
168 /* stuff we want to pass to /sbin/hotplug */
169 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
170 "XENBUS_TYPE=%s", xdev->devicetype);
172 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
173 "XENBUS_PATH=%s", xdev->nodename);
175 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
176 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
178 /* terminate, set to next free slot, shrink available space */
182 buffer = &buffer[length];
183 buffer_size -= length;
186 drv = to_xenbus_driver(dev->driver);
187 if (drv && drv->uevent)
188 return drv->uevent(xdev, envp, num_envp, buffer,
196 int xenbus_register_backend(struct xenbus_driver *drv)
198 drv->read_otherend_details = read_frontend_details;
200 return xenbus_register_driver_common(drv, &xenbus_backend);
203 /* backend/<typename>/<frontend-uuid>/<name> */
204 static int xenbus_probe_backend_unit(const char *dir,
211 nodename = kasprintf("%s/%s", dir, name);
215 DPRINTK("%s\n", nodename);
217 err = xenbus_probe_node(&xenbus_backend, type, nodename);
222 /* backend/<typename>/<frontend-domid> */
223 static int xenbus_probe_backend(const char *type, const char *domid)
228 unsigned int i, dir_n = 0;
232 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
236 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
242 for (i = 0; i < dir_n; i++) {
243 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
252 static void backend_changed(struct xenbus_watch *watch,
253 const char **vec, unsigned int len)
257 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
260 static struct xenbus_watch be_watch = {
262 .callback = backend_changed,
265 void xenbus_backend_suspend(int (*fn)(struct device *, void *))
268 if (!xenbus_backend.error)
269 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn);
272 void xenbus_backend_resume(int (*fn)(struct device *, void *))
275 if (!xenbus_backend.error)
276 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn);
279 void xenbus_backend_probe_and_watch(void)
281 xenbus_probe_devices(&xenbus_backend);
282 register_xenbus_watch(&be_watch);
286 void xenbus_backend_bus_register(void)
288 xenbus_backend.error = bus_register(&xenbus_backend.bus);
289 if (xenbus_backend.error)
291 "XENBUS: Error registering backend bus: %i\n",
292 xenbus_backend.error);
295 void xenbus_backend_device_register(void)
297 if (xenbus_backend.error)
300 xenbus_backend.error = device_register(&xenbus_backend.dev);
301 if (xenbus_backend.error) {
302 bus_unregister(&xenbus_backend.bus);
304 "XENBUS: Error registering backend device: %i\n",
305 xenbus_backend.error);