1 #define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
5 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/fcntl.h>
11 #include <linux/proc_fs.h>
12 #include <linux/notifier.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
16 #include <linux/module.h>
19 #include <asm/pgtable.h>
20 #include <asm/xen/hypervisor.h>
21 #include <xen/xenbus.h>
22 #include <xen/events.h>
26 #include <xen/platform_pci.h>
28 #include "xenbus_comms.h"
29 #include "xenbus_probe.h"
32 /* device/<type>/<id> => <type>-<id> */
33 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
35 nodename = strchr(nodename, '/');
36 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
37 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
41 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
42 if (!strchr(bus_id, '/')) {
43 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
46 *strchr(bus_id, '/') = '-';
50 /* device/<typename>/<name> */
51 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
57 /* ignore console/0 */
58 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
59 DPRINTK("Ignoring buggy device entry console/0");
63 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
67 DPRINTK("%s", nodename);
69 err = xenbus_probe_node(bus, type, nodename);
74 static int xenbus_uevent_frontend(struct device *_dev,
75 struct kobj_uevent_env *env)
77 struct xenbus_device *dev = to_xenbus_device(_dev);
79 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
86 static void backend_changed(struct xenbus_watch *watch,
87 const char **vec, unsigned int len)
89 xenbus_otherend_changed(watch, vec, len, 1);
92 static const struct dev_pm_ops xenbus_pm_ops = {
93 .suspend = xenbus_dev_suspend,
94 .resume = xenbus_dev_resume,
95 .freeze = xenbus_dev_suspend,
96 .thaw = xenbus_dev_cancel,
97 .restore = xenbus_dev_resume,
100 static struct xen_bus_type xenbus_frontend = {
102 .levels = 2, /* device/type/<id> */
103 .get_bus_id = frontend_bus_id,
104 .probe = xenbus_probe_frontend,
105 .otherend_changed = backend_changed,
108 .match = xenbus_match,
109 .uevent = xenbus_uevent_frontend,
110 .probe = xenbus_dev_probe,
111 .remove = xenbus_dev_remove,
112 .shutdown = xenbus_dev_shutdown,
113 .dev_attrs = xenbus_dev_attrs,
115 .pm = &xenbus_pm_ops,
119 static void frontend_changed(struct xenbus_watch *watch,
120 const char **vec, unsigned int len)
124 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
128 /* We watch for devices appearing and vanishing. */
129 static struct xenbus_watch fe_watch = {
131 .callback = frontend_changed,
134 static int read_backend_details(struct xenbus_device *xendev)
136 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
139 static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
141 struct xenbus_device *xendev = to_xenbus_device(dev);
142 struct device_driver *drv = data;
143 struct xenbus_driver *xendrv;
146 * A device with no driver will never connect. We care only about
147 * devices which should currently be in the process of connecting.
152 /* Is this search limited to a particular driver? */
153 if (drv && (dev->driver != drv))
156 if (ignore_nonessential) {
157 /* With older QEMU, for PVonHVM guests the guest config files
158 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
159 * which is nonsensical as there is no PV FB (there can be
160 * a PVKB) running as HVM guest. */
162 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
165 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
168 xendrv = to_xenbus_driver(dev->driver);
169 return (xendev->state < XenbusStateConnected ||
170 (xendev->state == XenbusStateConnected &&
171 xendrv->is_ready && !xendrv->is_ready(xendev)));
173 static int essential_device_connecting(struct device *dev, void *data)
175 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
177 static int non_essential_device_connecting(struct device *dev, void *data)
179 return is_device_connecting(dev, data, false);
182 static int exists_essential_connecting_device(struct device_driver *drv)
184 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
185 essential_device_connecting);
187 static int exists_non_essential_connecting_device(struct device_driver *drv)
189 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
190 non_essential_device_connecting);
193 static int print_device_status(struct device *dev, void *data)
195 struct xenbus_device *xendev = to_xenbus_device(dev);
196 struct device_driver *drv = data;
198 /* Is this operation limited to a particular driver? */
199 if (drv && (dev->driver != drv))
203 /* Information only: is this too noisy? */
204 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
206 } else if (xendev->state < XenbusStateConnected) {
207 enum xenbus_state rstate = XenbusStateUnknown;
208 if (xendev->otherend)
209 rstate = xenbus_read_driver_state(xendev->otherend);
210 printk(KERN_WARNING "XENBUS: Timeout connecting "
211 "to device: %s (local state %d, remote state %d)\n",
212 xendev->nodename, xendev->state, rstate);
218 /* We only wait for device setup after most initcalls have run. */
219 static int ready_to_wait_for_devices;
221 static bool wait_loop(unsigned long start, unsigned int max_delay,
222 unsigned int *seconds_waited)
224 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
225 if (!*seconds_waited)
226 printk(KERN_WARNING "XENBUS: Waiting for "
227 "devices to initialise: ");
228 *seconds_waited += 5;
229 printk("%us...", max_delay - *seconds_waited);
230 if (*seconds_waited == max_delay)
234 schedule_timeout_interruptible(HZ/10);
239 * On a 5-minute timeout, wait for all devices currently configured. We need
240 * to do this to guarantee that the filesystems and / or network devices
241 * needed for boot are available, before we can allow the boot to proceed.
243 * This needs to be on a late_initcall, to happen after the frontend device
244 * drivers have been initialised, but before the root fs is mounted.
246 * A possible improvement here would be to have the tools add a per-device
247 * flag to the store entry, indicating whether it is needed at boot time.
248 * This would allow people who knew what they were doing to accelerate their
249 * boot slightly, but of course needs tools or manual intervention to set up
250 * those flags correctly.
252 static void wait_for_devices(struct xenbus_driver *xendrv)
254 unsigned long start = jiffies;
255 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
256 unsigned int seconds_waited = 0;
258 if (!ready_to_wait_for_devices || !xen_domain())
261 while (exists_non_essential_connecting_device(drv))
262 if (wait_loop(start, 30, &seconds_waited))
265 /* Skips PVKB and PVFB check.*/
266 while (exists_essential_connecting_device(drv))
267 if (wait_loop(start, 270, &seconds_waited))
273 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
274 print_device_status);
277 int xenbus_register_frontend(struct xenbus_driver *drv)
281 drv->read_otherend_details = read_backend_details;
283 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
287 /* If this driver is loaded as a module wait for devices to attach. */
288 wait_for_devices(drv);
292 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
294 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
295 static int backend_state;
297 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
298 const char **v, unsigned int l)
300 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
301 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
302 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
303 wake_up(&backend_state_wq);
306 static void xenbus_reset_wait_for_backend(char *be, int expected)
309 timeout = wait_event_interruptible_timeout(backend_state_wq,
310 backend_state == expected, 5 * HZ);
312 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
316 * Reset frontend if it is in Connected or Closed state.
317 * Wait for backend to catch up.
318 * State Connected happens during kdump, Closed after kexec.
320 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
322 struct xenbus_watch be_watch;
324 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
325 be, xenbus_strstate(be_state));
327 memset(&be_watch, 0, sizeof(be_watch));
328 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
332 be_watch.callback = xenbus_reset_backend_state_changed;
333 backend_state = XenbusStateUnknown;
335 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
336 register_xenbus_watch(&be_watch);
338 /* fall through to forward backend to state XenbusStateInitialising */
340 case XenbusStateConnected:
341 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
342 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
344 case XenbusStateClosing:
345 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
346 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
348 case XenbusStateClosed:
349 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
350 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
353 unregister_xenbus_watch(&be_watch);
354 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
355 kfree(be_watch.node);
358 static void xenbus_check_frontend(char *class, char *dev)
360 int be_state, fe_state, err;
361 char *backend, *frontend;
363 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
367 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
372 case XenbusStateConnected:
373 case XenbusStateClosed:
374 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
375 frontend, xenbus_strstate(fe_state));
376 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
377 if (!backend || IS_ERR(backend))
379 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
381 xenbus_reset_frontend(frontend, backend, be_state);
391 static void xenbus_reset_state(void)
393 char **devclass, **dev;
394 int devclass_n, dev_n;
397 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
398 if (IS_ERR(devclass))
401 for (i = 0; i < devclass_n; i++) {
402 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
405 for (j = 0; j < dev_n; j++)
406 xenbus_check_frontend(devclass[i], dev[j]);
412 static int frontend_probe_and_watch(struct notifier_block *notifier,
416 /* reset devices in Connected or Closed state */
417 if (xen_hvm_domain())
418 xenbus_reset_state();
419 /* Enumerate devices in xenstore and watch for changes. */
420 xenbus_probe_devices(&xenbus_frontend);
421 register_xenbus_watch(&fe_watch);
427 static int __init xenbus_probe_frontend_init(void)
429 static struct notifier_block xenstore_notifier = {
430 .notifier_call = frontend_probe_and_watch
436 /* Register ourselves with the kernel bus subsystem */
437 err = bus_register(&xenbus_frontend.bus);
441 register_xenstore_notifier(&xenstore_notifier);
445 subsys_initcall(xenbus_probe_frontend_init);
448 static int __init boot_wait_for_devices(void)
450 if (xen_hvm_domain() && !xen_platform_pci_unplug)
453 ready_to_wait_for_devices = 1;
454 wait_for_devices(NULL);
458 late_initcall(boot_wait_for_devices);
461 MODULE_LICENSE("GPL");