Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_probe_backend.c
1 /******************************************************************************
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/version.h>
40 #include <linux/err.h>
41 #include <linux/string.h>
42 #include <linux/ctype.h>
43 #include <linux/fcntl.h>
44 #include <linux/mm.h>
45 #include <linux/slab.h>
46 #include <linux/notifier.h>
47 #include <linux/export.h>
48
49 #include <asm/page.h>
50 #include <asm/pgtable.h>
51 #ifndef CONFIG_XEN
52 #include <asm/xen/hypervisor.h>
53 #endif
54 #include <asm/hypervisor.h>
55 #include <xen/xenbus.h>
56 #ifdef CONFIG_XEN
57 #include <xen/xen_proc.h>
58 #include <xen/evtchn.h>
59 #endif
60 #include <xen/features.h>
61
62 #include "xenbus_comms.h"
63 #include "xenbus_probe.h"
64
65 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
66 #include <xen/platform-compat.h>
67 #endif
68
69 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
70 static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
71 {
72         int domid, err;
73         const char *devid, *type, *frontend;
74         unsigned int typelen;
75
76         type = strchr(nodename, '/');
77         if (!type)
78                 return -EINVAL;
79         type++;
80         typelen = strcspn(type, "/");
81         if (!typelen || type[typelen] != '/')
82                 return -EINVAL;
83
84         devid = strrchr(nodename, '/') + 1;
85
86         err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
87                             "frontend", NULL, &frontend,
88                             NULL);
89         if (err)
90                 return err;
91         if (strlen(frontend) == 0)
92                 err = -ERANGE;
93         if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
94                 err = -ENOENT;
95         kfree(frontend);
96
97         if (err)
98                 return err;
99
100         if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
101                      typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
102                 return -ENOSPC;
103         return 0;
104 }
105
106 static int xenbus_uevent_backend(struct device *dev,
107                                  struct kobj_uevent_env *env)
108 {
109         struct xenbus_device *xdev;
110         struct xenbus_driver *drv;
111         struct xen_bus_type *bus;
112
113         DPRINTK("");
114
115         if (dev == NULL)
116                 return -ENODEV;
117
118         xdev = to_xenbus_device(dev);
119         bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
120
121         if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
122                 return -ENOMEM;
123
124         /* stuff we want to pass to /sbin/hotplug */
125         if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
126                 return -ENOMEM;
127
128         if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
129                 return -ENOMEM;
130
131         if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
132                 return -ENOMEM;
133
134         if (dev->driver) {
135                 drv = to_xenbus_driver(dev->driver);
136                 if (drv && drv->uevent)
137                         return drv->uevent(xdev, env);
138         }
139
140         return 0;
141 }
142
143 /* backend/<typename>/<frontend-uuid>/<name> */
144 static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
145                                      const char *dir,
146                                      const char *type,
147                                      const char *name)
148 {
149         char *nodename;
150         int err;
151
152         nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
153         if (!nodename)
154                 return -ENOMEM;
155
156         DPRINTK("%s\n", nodename);
157
158         err = xenbus_probe_node(bus, type, nodename);
159         kfree(nodename);
160         return err;
161 }
162
163 /* backend/<typename>/<frontend-domid> */
164 static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
165                                 const char *domid)
166 {
167         char *nodename;
168         int err = 0;
169         char **dir;
170         unsigned int i, dir_n = 0;
171
172         DPRINTK("");
173
174         nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
175         if (!nodename)
176                 return -ENOMEM;
177
178         dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
179         if (IS_ERR(dir)) {
180                 kfree(nodename);
181                 return PTR_ERR(dir);
182         }
183
184         for (i = 0; i < dir_n; i++) {
185                 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
186                 if (err)
187                         break;
188         }
189         kfree(dir);
190         kfree(nodename);
191         return err;
192 }
193
194 #ifndef CONFIG_XEN
195 static void frontend_changed(struct xenbus_watch *watch,
196                             const char **vec, unsigned int len)
197 {
198         xenbus_otherend_changed(watch, vec, len, 0);
199 }
200 #endif
201
202 static struct xen_bus_type xenbus_backend = {
203         .root = "backend",
204         .levels = 3,            /* backend/type/<frontend>/<id> */
205         .get_bus_id = backend_bus_id,
206         .probe = xenbus_probe_backend,
207 #ifndef CONFIG_XEN
208         .otherend_changed = frontend_changed,
209 #else
210         .dev = {
211                 .init_name = "xen-backend",
212         },
213 #endif
214         .error = -ENODEV,
215         .bus = {
216                 .name           = "xen-backend",
217                 .match          = xenbus_match,
218                 .uevent         = xenbus_uevent_backend,
219                 .probe          = xenbus_dev_probe,
220                 .remove         = xenbus_dev_remove,
221 #ifdef CONFIG_XEN
222                 .shutdown       = xenbus_dev_shutdown,
223 #endif
224                 .dev_attrs      = xenbus_dev_attrs,
225         },
226 };
227
228 static void backend_changed(struct xenbus_watch *watch,
229                             const char **vec, unsigned int len)
230 {
231         DPRINTK("");
232
233         xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
234 }
235
236 static struct xenbus_watch be_watch = {
237         .node = "backend",
238         .callback = backend_changed,
239 };
240
241 static int read_frontend_details(struct xenbus_device *xendev)
242 {
243         return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
244 }
245
246 #ifndef CONFIG_XEN
247 int xenbus_dev_is_online(struct xenbus_device *dev)
248 {
249         int rc, val;
250
251         rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
252         if (rc != 1)
253                 val = 0; /* no online node present */
254
255         return val;
256 }
257 EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
258 #endif
259
260 int xenbus_register_backend(struct xenbus_driver *drv)
261 {
262         drv->read_otherend_details = read_frontend_details;
263
264         return xenbus_register_driver_common(drv, &xenbus_backend);
265 }
266 EXPORT_SYMBOL_GPL(xenbus_register_backend);
267
268 #if defined(CONFIG_XEN) && defined(CONFIG_PM_SLEEP)
269 void xenbus_backend_suspend(int (*fn)(struct device *, void *))
270 {
271         DPRINTK("");
272         if (!xenbus_backend.error)
273                 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn);
274 }
275
276 void xenbus_backend_resume(int (*fn)(struct device *, void *))
277 {
278         DPRINTK("");
279         if (!xenbus_backend.error)
280                 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn);
281 }
282 #endif
283
284 #ifndef CONFIG_XEN
285 static int backend_probe_and_watch(struct notifier_block *notifier,
286                                    unsigned long event,
287                                    void *data)
288 #else
289 void xenbus_backend_probe_and_watch(void)
290 #endif
291 {
292         /* Enumerate devices in xenstore and watch for changes. */
293         xenbus_probe_devices(&xenbus_backend);
294         register_xenbus_watch(&be_watch);
295
296 #ifndef CONFIG_XEN
297         return NOTIFY_DONE;
298 #endif
299 }
300
301 #ifndef CONFIG_XEN
302
303 static int __init xenbus_probe_backend_init(void)
304 {
305         static struct notifier_block xenstore_notifier = {
306                 .notifier_call = backend_probe_and_watch
307         };
308         int err;
309
310         DPRINTK("");
311
312         /* Register ourselves with the kernel bus subsystem */
313         err = bus_register(&xenbus_backend.bus);
314         if (err)
315                 return err;
316
317         register_xenstore_notifier(&xenstore_notifier);
318
319         return 0;
320 }
321 subsys_initcall(xenbus_probe_backend_init);
322
323 #else
324
325 void __init xenbus_backend_bus_register(void)
326 {
327         xenbus_backend.error = bus_register(&xenbus_backend.bus);
328         if (xenbus_backend.error)
329                 pr_warning("XENBUS: Error registering backend bus: %i\n",
330                            xenbus_backend.error);
331 }
332
333 void __init xenbus_backend_device_register(void)
334 {
335         if (xenbus_backend.error)
336                 return;
337
338         xenbus_backend.error = device_register(&xenbus_backend.dev);
339         if (xenbus_backend.error) {
340                 bus_unregister(&xenbus_backend.bus);
341                 pr_warning("XENBUS: Error registering backend device: %i\n",
342                            xenbus_backend.error);
343         }
344 }
345
346 int xenbus_for_each_backend(void *arg, int (*fn)(struct device *, void *))
347 {
348         return bus_for_each_dev(&xenbus_backend.bus, NULL, arg, fn);
349 }
350 EXPORT_SYMBOL_GPL(xenbus_for_each_backend);
351
352 #endif