Merge branch 'kconfig' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[pandora-kernel.git] / drivers / xen / xenbus / xenbus_probe_frontend.c
1 #define DPRINTK(fmt, args...)                           \
2         pr_debug("xenbus_probe (%s:%d) " fmt ".\n",     \
3                  __func__, __LINE__, ##args)
4
5 #include <linux/kernel.h>
6 #include <linux/err.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/fcntl.h>
10 #include <linux/mm.h>
11 #include <linux/proc_fs.h>
12 #include <linux/notifier.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
15 #include <linux/io.h>
16
17 #include <asm/page.h>
18 #include <asm/pgtable.h>
19 #include <asm/xen/hypervisor.h>
20 #include <xen/xenbus.h>
21 #include <xen/events.h>
22 #include <xen/page.h>
23
24 #include <xen/platform_pci.h>
25
26 #include "xenbus_comms.h"
27 #include "xenbus_probe.h"
28
29
30 /* device/<type>/<id> => <type>-<id> */
31 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
32 {
33         nodename = strchr(nodename, '/');
34         if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
35                 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
36                 return -EINVAL;
37         }
38
39         strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
40         if (!strchr(bus_id, '/')) {
41                 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
42                 return -EINVAL;
43         }
44         *strchr(bus_id, '/') = '-';
45         return 0;
46 }
47
48 /* device/<typename>/<name> */
49 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
50                                  const char *name)
51 {
52         char *nodename;
53         int err;
54
55         nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
56         if (!nodename)
57                 return -ENOMEM;
58
59         DPRINTK("%s", nodename);
60
61         err = xenbus_probe_node(bus, type, nodename);
62         kfree(nodename);
63         return err;
64 }
65
66 static int xenbus_uevent_frontend(struct device *_dev,
67                                   struct kobj_uevent_env *env)
68 {
69         struct xenbus_device *dev = to_xenbus_device(_dev);
70
71         if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
72                 return -ENOMEM;
73
74         return 0;
75 }
76
77
78 static void backend_changed(struct xenbus_watch *watch,
79                             const char **vec, unsigned int len)
80 {
81         xenbus_otherend_changed(watch, vec, len, 1);
82 }
83
84 static const struct dev_pm_ops xenbus_pm_ops = {
85         .suspend        = xenbus_dev_suspend,
86         .resume         = xenbus_dev_resume,
87         .freeze         = xenbus_dev_suspend,
88         .thaw           = xenbus_dev_cancel,
89         .restore        = xenbus_dev_resume,
90 };
91
92 static struct xen_bus_type xenbus_frontend = {
93         .root = "device",
94         .levels = 2,            /* device/type/<id> */
95         .get_bus_id = frontend_bus_id,
96         .probe = xenbus_probe_frontend,
97         .otherend_changed = backend_changed,
98         .bus = {
99                 .name           = "xen",
100                 .match          = xenbus_match,
101                 .uevent         = xenbus_uevent_frontend,
102                 .probe          = xenbus_dev_probe,
103                 .remove         = xenbus_dev_remove,
104                 .shutdown       = xenbus_dev_shutdown,
105                 .dev_attrs      = xenbus_dev_attrs,
106
107                 .pm             = &xenbus_pm_ops,
108         },
109 };
110
111 static void frontend_changed(struct xenbus_watch *watch,
112                              const char **vec, unsigned int len)
113 {
114         DPRINTK("");
115
116         xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
117 }
118
119
120 /* We watch for devices appearing and vanishing. */
121 static struct xenbus_watch fe_watch = {
122         .node = "device",
123         .callback = frontend_changed,
124 };
125
126 static int read_backend_details(struct xenbus_device *xendev)
127 {
128         return xenbus_read_otherend_details(xendev, "backend-id", "backend");
129 }
130
131 static int is_device_connecting(struct device *dev, void *data)
132 {
133         struct xenbus_device *xendev = to_xenbus_device(dev);
134         struct device_driver *drv = data;
135         struct xenbus_driver *xendrv;
136
137         /*
138          * A device with no driver will never connect. We care only about
139          * devices which should currently be in the process of connecting.
140          */
141         if (!dev->driver)
142                 return 0;
143
144         /* Is this search limited to a particular driver? */
145         if (drv && (dev->driver != drv))
146                 return 0;
147
148         xendrv = to_xenbus_driver(dev->driver);
149         return (xendev->state < XenbusStateConnected ||
150                 (xendev->state == XenbusStateConnected &&
151                  xendrv->is_ready && !xendrv->is_ready(xendev)));
152 }
153
154 static int exists_connecting_device(struct device_driver *drv)
155 {
156         return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
157                                 is_device_connecting);
158 }
159
160 static int print_device_status(struct device *dev, void *data)
161 {
162         struct xenbus_device *xendev = to_xenbus_device(dev);
163         struct device_driver *drv = data;
164
165         /* Is this operation limited to a particular driver? */
166         if (drv && (dev->driver != drv))
167                 return 0;
168
169         if (!dev->driver) {
170                 /* Information only: is this too noisy? */
171                 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
172                        xendev->nodename);
173         } else if (xendev->state < XenbusStateConnected) {
174                 enum xenbus_state rstate = XenbusStateUnknown;
175                 if (xendev->otherend)
176                         rstate = xenbus_read_driver_state(xendev->otherend);
177                 printk(KERN_WARNING "XENBUS: Timeout connecting "
178                        "to device: %s (local state %d, remote state %d)\n",
179                        xendev->nodename, xendev->state, rstate);
180         }
181
182         return 0;
183 }
184
185 /* We only wait for device setup after most initcalls have run. */
186 static int ready_to_wait_for_devices;
187
188 /*
189  * On a 5-minute timeout, wait for all devices currently configured.  We need
190  * to do this to guarantee that the filesystems and / or network devices
191  * needed for boot are available, before we can allow the boot to proceed.
192  *
193  * This needs to be on a late_initcall, to happen after the frontend device
194  * drivers have been initialised, but before the root fs is mounted.
195  *
196  * A possible improvement here would be to have the tools add a per-device
197  * flag to the store entry, indicating whether it is needed at boot time.
198  * This would allow people who knew what they were doing to accelerate their
199  * boot slightly, but of course needs tools or manual intervention to set up
200  * those flags correctly.
201  */
202 static void wait_for_devices(struct xenbus_driver *xendrv)
203 {
204         unsigned long start = jiffies;
205         struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
206         unsigned int seconds_waited = 0;
207
208         if (!ready_to_wait_for_devices || !xen_domain())
209                 return;
210
211         while (exists_connecting_device(drv)) {
212                 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
213                         if (!seconds_waited)
214                                 printk(KERN_WARNING "XENBUS: Waiting for "
215                                        "devices to initialise: ");
216                         seconds_waited += 5;
217                         printk("%us...", 300 - seconds_waited);
218                         if (seconds_waited == 300)
219                                 break;
220                 }
221
222                 schedule_timeout_interruptible(HZ/10);
223         }
224
225         if (seconds_waited)
226                 printk("\n");
227
228         bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
229                          print_device_status);
230 }
231
232 int __xenbus_register_frontend(struct xenbus_driver *drv,
233                                struct module *owner, const char *mod_name)
234 {
235         int ret;
236
237         drv->read_otherend_details = read_backend_details;
238
239         ret = xenbus_register_driver_common(drv, &xenbus_frontend,
240                                             owner, mod_name);
241         if (ret)
242                 return ret;
243
244         /* If this driver is loaded as a module wait for devices to attach. */
245         wait_for_devices(drv);
246
247         return 0;
248 }
249 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
250
251 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
252 static int backend_state;
253
254 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
255                                         const char **v, unsigned int l)
256 {
257         xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
258         printk(KERN_DEBUG "XENBUS: backend %s %s\n",
259                         v[XS_WATCH_PATH], xenbus_strstate(backend_state));
260         wake_up(&backend_state_wq);
261 }
262
263 static void xenbus_reset_wait_for_backend(char *be, int expected)
264 {
265         long timeout;
266         timeout = wait_event_interruptible_timeout(backend_state_wq,
267                         backend_state == expected, 5 * HZ);
268         if (timeout <= 0)
269                 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
270 }
271
272 /*
273  * Reset frontend if it is in Connected or Closed state.
274  * Wait for backend to catch up.
275  * State Connected happens during kdump, Closed after kexec.
276  */
277 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
278 {
279         struct xenbus_watch be_watch;
280
281         printk(KERN_DEBUG "XENBUS: backend %s %s\n",
282                         be, xenbus_strstate(be_state));
283
284         memset(&be_watch, 0, sizeof(be_watch));
285         be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
286         if (!be_watch.node)
287                 return;
288
289         be_watch.callback = xenbus_reset_backend_state_changed;
290         backend_state = XenbusStateUnknown;
291
292         printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
293         register_xenbus_watch(&be_watch);
294
295         /* fall through to forward backend to state XenbusStateInitialising */
296         switch (be_state) {
297         case XenbusStateConnected:
298                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
299                 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
300
301         case XenbusStateClosing:
302                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
303                 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
304
305         case XenbusStateClosed:
306                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
307                 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
308         }
309
310         unregister_xenbus_watch(&be_watch);
311         printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
312         kfree(be_watch.node);
313 }
314
315 static void xenbus_check_frontend(char *class, char *dev)
316 {
317         int be_state, fe_state, err;
318         char *backend, *frontend;
319
320         frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
321         if (!frontend)
322                 return;
323
324         err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
325         if (err != 1)
326                 goto out;
327
328         switch (fe_state) {
329         case XenbusStateConnected:
330         case XenbusStateClosed:
331                 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
332                                 frontend, xenbus_strstate(fe_state));
333                 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
334                 if (!backend || IS_ERR(backend))
335                         goto out;
336                 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
337                 if (err == 1)
338                         xenbus_reset_frontend(frontend, backend, be_state);
339                 kfree(backend);
340                 break;
341         default:
342                 break;
343         }
344 out:
345         kfree(frontend);
346 }
347
348 static void xenbus_reset_state(void)
349 {
350         char **devclass, **dev;
351         int devclass_n, dev_n;
352         int i, j;
353
354         devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
355         if (IS_ERR(devclass))
356                 return;
357
358         for (i = 0; i < devclass_n; i++) {
359                 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
360                 if (IS_ERR(dev))
361                         continue;
362                 for (j = 0; j < dev_n; j++)
363                         xenbus_check_frontend(devclass[i], dev[j]);
364                 kfree(dev);
365         }
366         kfree(devclass);
367 }
368
369 static int frontend_probe_and_watch(struct notifier_block *notifier,
370                                    unsigned long event,
371                                    void *data)
372 {
373         /* reset devices in Connected or Closed state */
374         if (xen_hvm_domain())
375                 xenbus_reset_state();
376         /* Enumerate devices in xenstore and watch for changes. */
377         xenbus_probe_devices(&xenbus_frontend);
378         register_xenbus_watch(&fe_watch);
379
380         return NOTIFY_DONE;
381 }
382
383
384 static int __init xenbus_probe_frontend_init(void)
385 {
386         static struct notifier_block xenstore_notifier = {
387                 .notifier_call = frontend_probe_and_watch
388         };
389         int err;
390
391         DPRINTK("");
392
393         /* Register ourselves with the kernel bus subsystem */
394         err = bus_register(&xenbus_frontend.bus);
395         if (err)
396                 return err;
397
398         register_xenstore_notifier(&xenstore_notifier);
399
400         return 0;
401 }
402 subsys_initcall(xenbus_probe_frontend_init);
403
404 #ifndef MODULE
405 static int __init boot_wait_for_devices(void)
406 {
407         if (xen_hvm_domain() && !xen_platform_pci_unplug)
408                 return -ENODEV;
409
410         ready_to_wait_for_devices = 1;
411         wait_for_devices(NULL);
412         return 0;
413 }
414
415 late_initcall(boot_wait_for_devices);
416 #endif
417
418 MODULE_LICENSE("GPL");