blob: 32417b5064fd71dc760d284ed992047babd28d9f [file] [log] [blame]
Ian Campbelldf660252009-02-09 12:05:51 -08001/******************************************************************************
2 * Talks to Xen Store to figure out what devices we have (backend half).
3 *
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.
8 *
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:
14 *
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:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
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
31 * IN THE SOFTWARE.
32 */
33
34#define DPRINTK(fmt, args...) \
35 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
36 __func__, __LINE__, ##args)
37
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>
43#include <linux/mm.h>
44#include <linux/notifier.h>
45
46#include <asm/page.h>
47#include <asm/pgtable.h>
48#include <asm/xen/hypervisor.h>
49#include <asm/hypervisor.h>
50#include <xen/xenbus.h>
Ian Campbelldf660252009-02-09 12:05:51 -080051#include <xen/features.h>
52
53#include "xenbus_comms.h"
54#include "xenbus_probe.h"
55
56/* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
57static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
58{
59 int domid, err;
60 const char *devid, *type, *frontend;
61 unsigned int typelen;
62
63 type = strchr(nodename, '/');
64 if (!type)
65 return -EINVAL;
66 type++;
67 typelen = strcspn(type, "/");
68 if (!typelen || type[typelen] != '/')
69 return -EINVAL;
70
71 devid = strrchr(nodename, '/') + 1;
72
73 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
74 "frontend", NULL, &frontend,
75 NULL);
76 if (err)
77 return err;
78 if (strlen(frontend) == 0)
79 err = -ERANGE;
80 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
81 err = -ENOENT;
82 kfree(frontend);
83
84 if (err)
85 return err;
86
Ian Campbell6bac7f92010-12-10 14:39:15 +000087 if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
88 typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
Ian Campbelldf660252009-02-09 12:05:51 -080089 return -ENOSPC;
90 return 0;
91}
92
93static int xenbus_uevent_backend(struct device *dev,
94 struct kobj_uevent_env *env)
95{
96 struct xenbus_device *xdev;
97 struct xenbus_driver *drv;
98 struct xen_bus_type *bus;
99
100 DPRINTK("");
101
102 if (dev == NULL)
103 return -ENODEV;
104
105 xdev = to_xenbus_device(dev);
106 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
Ian Campbelldf660252009-02-09 12:05:51 -0800107
Bastian Blank149bb2f2011-06-29 14:40:08 +0200108 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
109 return -ENOMEM;
110
Ian Campbelldf660252009-02-09 12:05:51 -0800111 /* stuff we want to pass to /sbin/hotplug */
112 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
113 return -ENOMEM;
114
115 if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
116 return -ENOMEM;
117
118 if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
119 return -ENOMEM;
120
121 if (dev->driver) {
122 drv = to_xenbus_driver(dev->driver);
123 if (drv && drv->uevent)
124 return drv->uevent(xdev, env);
125 }
126
127 return 0;
128}
129
130/* backend/<typename>/<frontend-uuid>/<name> */
131static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
132 const char *dir,
133 const char *type,
134 const char *name)
135{
136 char *nodename;
137 int err;
138
139 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
140 if (!nodename)
141 return -ENOMEM;
142
143 DPRINTK("%s\n", nodename);
144
145 err = xenbus_probe_node(bus, type, nodename);
146 kfree(nodename);
147 return err;
148}
149
150/* backend/<typename>/<frontend-domid> */
Ian Campbell6bac7f92010-12-10 14:39:15 +0000151static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
152 const char *domid)
Ian Campbelldf660252009-02-09 12:05:51 -0800153{
154 char *nodename;
155 int err = 0;
156 char **dir;
157 unsigned int i, dir_n = 0;
158
159 DPRINTK("");
160
161 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
162 if (!nodename)
163 return -ENOMEM;
164
165 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
166 if (IS_ERR(dir)) {
167 kfree(nodename);
168 return PTR_ERR(dir);
169 }
170
171 for (i = 0; i < dir_n; i++) {
172 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
173 if (err)
174 break;
175 }
176 kfree(dir);
177 kfree(nodename);
178 return err;
179}
180
181static void frontend_changed(struct xenbus_watch *watch,
182 const char **vec, unsigned int len)
183{
184 xenbus_otherend_changed(watch, vec, len, 0);
185}
186
Ian Campbelldf660252009-02-09 12:05:51 -0800187static struct xen_bus_type xenbus_backend = {
188 .root = "backend",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000189 .levels = 3, /* backend/type/<frontend>/<id> */
Ian Campbelldf660252009-02-09 12:05:51 -0800190 .get_bus_id = backend_bus_id,
191 .probe = xenbus_probe_backend,
192 .otherend_changed = frontend_changed,
193 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000194 .name = "xen-backend",
195 .match = xenbus_match,
196 .uevent = xenbus_uevent_backend,
197 .probe = xenbus_dev_probe,
198 .remove = xenbus_dev_remove,
199 .shutdown = xenbus_dev_shutdown,
Bastian Blankcc85e932011-06-29 14:39:26 +0200200 .dev_attrs = xenbus_dev_attrs,
Ian Campbelldf660252009-02-09 12:05:51 -0800201 },
202};
203
204static void backend_changed(struct xenbus_watch *watch,
205 const char **vec, unsigned int len)
206{
207 DPRINTK("");
208
209 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
210}
211
212static struct xenbus_watch be_watch = {
213 .node = "backend",
214 .callback = backend_changed,
215};
216
217static int read_frontend_details(struct xenbus_device *xendev)
218{
219 return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
220}
221
Ian Campbelldf660252009-02-09 12:05:51 -0800222int xenbus_dev_is_online(struct xenbus_device *dev)
223{
224 int rc, val;
225
226 rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
227 if (rc != 1)
228 val = 0; /* no online node present */
229
230 return val;
231}
232EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
233
234int __xenbus_register_backend(struct xenbus_driver *drv,
235 struct module *owner, const char *mod_name)
236{
237 drv->read_otherend_details = read_frontend_details;
238
239 return xenbus_register_driver_common(drv, &xenbus_backend,
240 owner, mod_name);
241}
242EXPORT_SYMBOL_GPL(__xenbus_register_backend);
243
244static int backend_probe_and_watch(struct notifier_block *notifier,
245 unsigned long event,
246 void *data)
247{
248 /* Enumerate devices in xenstore and watch for changes. */
249 xenbus_probe_devices(&xenbus_backend);
Ian Campbelldf660252009-02-09 12:05:51 -0800250 register_xenbus_watch(&be_watch);
Jeremy Fitzhardingeb5d33d02010-03-29 14:38:35 -0700251
Ian Campbelldf660252009-02-09 12:05:51 -0800252 return NOTIFY_DONE;
253}
254
255static int __init xenbus_probe_backend_init(void)
256{
257 static struct notifier_block xenstore_notifier = {
258 .notifier_call = backend_probe_and_watch
259 };
260 int err;
261
262 DPRINTK("");
263
264 /* Register ourselves with the kernel bus subsystem */
265 err = bus_register(&xenbus_backend.bus);
Jeremy Fitzhardingeb5d33d02010-03-29 14:38:35 -0700266 if (err)
Ian Campbelldf660252009-02-09 12:05:51 -0800267 return err;
Ian Campbelldf660252009-02-09 12:05:51 -0800268
269 register_xenstore_notifier(&xenstore_notifier);
270
271 return 0;
272}
Jeremy Fitzhardingec3676e82009-03-04 22:32:16 -0800273subsys_initcall(xenbus_probe_backend_init);