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