1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __FUNCTION__, __LINE__, ##args)
37 #include <linux/kernel.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/ctype.h>
41 #include <linux/fcntl.h>
43 #include <linux/notifier.h>
44 #include <linux/kthread.h>
48 #include <asm/pgtable.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <xen/evtchn.h>
53 #include <xen/features.h>
55 #include "xenbus_comms.h"
57 extern struct mutex xenwatch_mutex;
59 static BLOCKING_NOTIFIER_HEAD(xenstore_notifier_list);
61 static void wait_for_devices(struct xenbus_driver *xendrv);
63 /* If something in array of ids matches this device, return it. */
64 static const struct xenbus_device_id *
65 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
67 for (; *arr->devicetype != '\0'; arr++) {
68 if (!strcmp(arr->devicetype, dev->devicetype))
74 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
76 struct xenbus_driver *drv = to_xenbus_driver(_drv);
81 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
88 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
89 int (*probe)(const char *type, const char *dir);
95 /* device/<type>/<id> => <type>-<id> */
96 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
98 nodename = strchr(nodename, '/');
99 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
100 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
104 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
105 if (!strchr(bus_id, '/')) {
106 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
109 *strchr(bus_id, '/') = '-';
114 static void free_otherend_details(struct xenbus_device *dev)
116 kfree(dev->otherend);
117 dev->otherend = NULL;
121 static void free_otherend_watch(struct xenbus_device *dev)
123 if (dev->otherend_watch.node) {
124 unregister_xenbus_watch(&dev->otherend_watch);
125 kfree(dev->otherend_watch.node);
126 dev->otherend_watch.node = NULL;
131 static int read_otherend_details(struct xenbus_device *xendev,
132 char *id_node, char *path_node)
134 int err = xenbus_gather(XBT_NIL, xendev->nodename,
135 id_node, "%i", &xendev->otherend_id,
136 path_node, NULL, &xendev->otherend,
139 xenbus_dev_fatal(xendev, err,
140 "reading other end details from %s",
144 if (strlen(xendev->otherend) == 0 ||
145 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
146 xenbus_dev_fatal(xendev, -ENOENT,
147 "unable to read other end from %s. "
148 "missing or inaccessible.",
150 free_otherend_details(xendev);
158 static int read_backend_details(struct xenbus_device *xendev)
160 return read_otherend_details(xendev, "backend-id", "backend");
164 static int read_frontend_details(struct xenbus_device *xendev)
166 return read_otherend_details(xendev, "frontend-id", "frontend");
170 /* Bus type for frontend drivers. */
171 static int xenbus_probe_frontend(const char *type, const char *name);
172 static struct xen_bus_type xenbus_frontend = {
174 .levels = 2, /* device/type/<id> */
175 .get_bus_id = frontend_bus_id,
176 .probe = xenbus_probe_frontend,
179 .match = xenbus_match,
186 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
187 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
190 const char *devid, *type, *frontend;
191 unsigned int typelen;
193 type = strchr(nodename, '/');
197 typelen = strcspn(type, "/");
198 if (!typelen || type[typelen] != '/')
201 devid = strrchr(nodename, '/') + 1;
203 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
204 "frontend", NULL, &frontend,
208 if (strlen(frontend) == 0)
210 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
218 if (snprintf(bus_id, BUS_ID_SIZE,
219 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
224 static int xenbus_uevent_backend(struct device *dev, char **envp,
225 int num_envp, char *buffer, int buffer_size);
226 static int xenbus_probe_backend(const char *type, const char *domid);
227 static struct xen_bus_type xenbus_backend = {
229 .levels = 3, /* backend/type/<frontend>/<id> */
230 .get_bus_id = backend_bus_id,
231 .probe = xenbus_probe_backend,
233 .name = "xen-backend",
234 .match = xenbus_match,
235 .uevent = xenbus_uevent_backend,
238 .bus_id = "xen-backend",
242 static int xenbus_uevent_backend(struct device *dev, char **envp,
243 int num_envp, char *buffer, int buffer_size)
245 struct xenbus_device *xdev;
246 struct xenbus_driver *drv;
255 xdev = to_xenbus_device(dev);
259 /* stuff we want to pass to /sbin/hotplug */
260 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
261 "XENBUS_TYPE=%s", xdev->devicetype);
263 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
264 "XENBUS_PATH=%s", xdev->nodename);
266 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
267 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
269 /* terminate, set to next free slot, shrink available space */
273 buffer = &buffer[length];
274 buffer_size -= length;
277 drv = to_xenbus_driver(dev->driver);
278 if (drv && drv->uevent)
279 return drv->uevent(xdev, envp, num_envp, buffer,
286 static void otherend_changed(struct xenbus_watch *watch,
287 const char **vec, unsigned int len)
289 struct xenbus_device *dev =
290 container_of(watch, struct xenbus_device, otherend_watch);
291 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
292 enum xenbus_state state;
294 /* Protect us against watches firing on old details when the otherend
295 details change, say immediately after a resume. */
296 if (!dev->otherend ||
297 strncmp(dev->otherend, vec[XS_WATCH_PATH],
298 strlen(dev->otherend))) {
299 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
303 state = xenbus_read_driver_state(dev->otherend);
305 DPRINTK("state is %d, %s, %s",
306 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
307 if (drv->otherend_changed)
308 drv->otherend_changed(dev, state);
312 static int talk_to_otherend(struct xenbus_device *dev)
314 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
316 free_otherend_watch(dev);
317 free_otherend_details(dev);
319 return drv->read_otherend_details(dev);
323 static int watch_otherend(struct xenbus_device *dev)
325 return xenbus_watch_path2(dev, dev->otherend, "state",
326 &dev->otherend_watch, otherend_changed);
330 static int xenbus_dev_probe(struct device *_dev)
332 struct xenbus_device *dev = to_xenbus_device(_dev);
333 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
334 const struct xenbus_device_id *id;
344 id = match_device(drv->ids, dev);
350 err = talk_to_otherend(dev);
353 "xenbus_probe: talk_to_otherend on %s failed.\n",
358 err = drv->probe(dev, id);
362 err = watch_otherend(dev);
365 "xenbus_probe: watch_otherend on %s failed.\n",
372 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
373 xenbus_switch_state(dev, XenbusStateClosed);
377 static int xenbus_dev_remove(struct device *_dev)
379 struct xenbus_device *dev = to_xenbus_device(_dev);
380 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
384 free_otherend_watch(dev);
385 free_otherend_details(dev);
390 xenbus_switch_state(dev, XenbusStateClosed);
394 static int xenbus_register_driver_common(struct xenbus_driver *drv,
395 struct xen_bus_type *bus)
399 drv->driver.name = drv->name;
400 drv->driver.bus = &bus->bus;
401 drv->driver.owner = drv->owner;
402 drv->driver.probe = xenbus_dev_probe;
403 drv->driver.remove = xenbus_dev_remove;
405 mutex_lock(&xenwatch_mutex);
406 ret = driver_register(&drv->driver);
407 mutex_unlock(&xenwatch_mutex);
411 int xenbus_register_frontend(struct xenbus_driver *drv)
415 drv->read_otherend_details = read_backend_details;
417 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
421 /* If this driver is loaded as a module wait for devices to attach. */
422 wait_for_devices(drv);
426 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
428 int xenbus_register_backend(struct xenbus_driver *drv)
430 drv->read_otherend_details = read_frontend_details;
432 return xenbus_register_driver_common(drv, &xenbus_backend);
434 EXPORT_SYMBOL_GPL(xenbus_register_backend);
436 void xenbus_unregister_driver(struct xenbus_driver *drv)
438 driver_unregister(&drv->driver);
440 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
444 struct xenbus_device *dev;
445 const char *nodename;
448 static int cmp_dev(struct device *dev, void *data)
450 struct xenbus_device *xendev = to_xenbus_device(dev);
451 struct xb_find_info *info = data;
453 if (!strcmp(xendev->nodename, info->nodename)) {
461 struct xenbus_device *xenbus_device_find(const char *nodename,
462 struct bus_type *bus)
464 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
466 bus_for_each_dev(bus, NULL, &info, cmp_dev);
470 static int cleanup_dev(struct device *dev, void *data)
472 struct xenbus_device *xendev = to_xenbus_device(dev);
473 struct xb_find_info *info = data;
474 int len = strlen(info->nodename);
476 DPRINTK("%s", info->nodename);
478 /* Match the info->nodename path, or any subdirectory of that path. */
479 if (strncmp(xendev->nodename, info->nodename, len))
482 /* If the node name is longer, ensure it really is a subdirectory. */
483 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
491 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
493 struct xb_find_info info = { .nodename = path };
497 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
499 device_unregister(&info.dev->dev);
500 put_device(&info.dev->dev);
505 static void xenbus_dev_release(struct device *dev)
508 kfree(to_xenbus_device(dev));
511 /* Simplified asprintf. */
512 char *kasprintf(const char *fmt, ...)
519 /* FIXME: vsnprintf has a bug, NULL should work */
520 len = vsnprintf(dummy, 0, fmt, ap);
523 p = kmalloc(len + 1, GFP_KERNEL);
527 vsprintf(p, fmt, ap);
532 static ssize_t xendev_show_nodename(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
537 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
539 static ssize_t xendev_show_devtype(struct device *dev,
540 struct device_attribute *attr, char *buf)
542 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
544 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
547 static int xenbus_probe_node(struct xen_bus_type *bus,
549 const char *nodename)
552 struct xenbus_device *xendev;
556 enum xenbus_state state = xenbus_read_driver_state(nodename);
558 if (state != XenbusStateInitialising) {
559 /* Device is not new, so ignore it. This can happen if a
560 device is going away after switching to Closed. */
564 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
565 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
569 /* Copy the strings into the extra space. */
571 tmpstring = (char *)(xendev + 1);
572 strcpy(tmpstring, nodename);
573 xendev->nodename = tmpstring;
575 tmpstring += strlen(tmpstring) + 1;
576 strcpy(tmpstring, type);
577 xendev->devicetype = tmpstring;
579 xendev->dev.parent = &bus->dev;
580 xendev->dev.bus = &bus->bus;
581 xendev->dev.release = xenbus_dev_release;
583 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
587 /* Register with generic device framework. */
588 err = device_register(&xendev->dev);
592 device_create_file(&xendev->dev, &dev_attr_nodename);
593 device_create_file(&xendev->dev, &dev_attr_devtype);
601 /* device/<typename>/<name> */
602 static int xenbus_probe_frontend(const char *type, const char *name)
607 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
611 DPRINTK("%s", nodename);
613 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
618 /* backend/<typename>/<frontend-uuid>/<name> */
619 static int xenbus_probe_backend_unit(const char *dir,
626 nodename = kasprintf("%s/%s", dir, name);
630 DPRINTK("%s\n", nodename);
632 err = xenbus_probe_node(&xenbus_backend, type, nodename);
637 /* backend/<typename>/<frontend-domid> */
638 static int xenbus_probe_backend(const char *type, const char *domid)
643 unsigned int i, dir_n = 0;
647 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
651 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
657 for (i = 0; i < dir_n; i++) {
658 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
667 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
671 unsigned int dir_n = 0;
674 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
678 for (i = 0; i < dir_n; i++) {
679 err = bus->probe(type, dir[i]);
687 static int xenbus_probe_devices(struct xen_bus_type *bus)
691 unsigned int i, dir_n;
693 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
697 for (i = 0; i < dir_n; i++) {
698 err = xenbus_probe_device_type(bus, dir[i]);
706 static unsigned int char_count(const char *str, char c)
708 unsigned int i, ret = 0;
710 for (i = 0; str[i]; i++)
716 static int strsep_len(const char *str, char c, unsigned int len)
720 for (i = 0; str[i]; i++)
726 return (len == 0) ? i : -ERANGE;
729 static void dev_changed(const char *node, struct xen_bus_type *bus)
732 struct xenbus_device *dev;
733 char type[BUS_ID_SIZE];
734 const char *p, *root;
736 if (char_count(node, '/') < 2)
739 exists = xenbus_exists(XBT_NIL, node, "");
741 xenbus_cleanup_devices(node, &bus->bus);
745 /* backend/<type>/... or device/<type>/... */
746 p = strchr(node, '/') + 1;
747 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
748 type[BUS_ID_SIZE-1] = '\0';
750 rootlen = strsep_len(node, '/', bus->levels);
753 root = kasprintf("%.*s", rootlen, node);
757 dev = xenbus_device_find(root, &bus->bus);
759 xenbus_probe_node(bus, type, root);
761 put_device(&dev->dev);
766 static void frontend_changed(struct xenbus_watch *watch,
767 const char **vec, unsigned int len)
771 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
774 static void backend_changed(struct xenbus_watch *watch,
775 const char **vec, unsigned int len)
779 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
782 /* We watch for devices appearing and vanishing. */
783 static struct xenbus_watch fe_watch = {
785 .callback = frontend_changed,
788 static struct xenbus_watch be_watch = {
790 .callback = backend_changed,
793 static int suspend_dev(struct device *dev, void *data)
796 struct xenbus_driver *drv;
797 struct xenbus_device *xdev;
801 if (dev->driver == NULL)
803 drv = to_xenbus_driver(dev->driver);
804 xdev = container_of(dev, struct xenbus_device, dev);
806 err = drv->suspend(xdev);
809 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
813 static int resume_dev(struct device *dev, void *data)
816 struct xenbus_driver *drv;
817 struct xenbus_device *xdev;
821 if (dev->driver == NULL)
824 drv = to_xenbus_driver(dev->driver);
825 xdev = container_of(dev, struct xenbus_device, dev);
827 err = talk_to_otherend(xdev);
830 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
835 xdev->state = XenbusStateInitialising;
838 err = drv->resume(xdev);
841 "xenbus: resume %s failed: %i\n",
847 err = watch_otherend(xdev);
850 "xenbus_probe: resume (watch_otherend) %s failed: "
851 "%d.\n", dev->bus_id, err);
858 void xenbus_suspend(void)
862 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
863 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
866 EXPORT_SYMBOL_GPL(xenbus_suspend);
868 void xenbus_resume(void)
872 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
873 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
875 EXPORT_SYMBOL_GPL(xenbus_resume);
878 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
879 int xenstored_ready = 0;
882 int register_xenstore_notifier(struct notifier_block *nb)
886 if (xenstored_ready > 0)
887 ret = nb->notifier_call(nb, 0, NULL);
889 blocking_notifier_chain_register(&xenstore_notifier_list, nb);
893 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
895 void unregister_xenstore_notifier(struct notifier_block *nb)
897 blocking_notifier_chain_unregister(&xenstore_notifier_list, nb);
899 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
902 void xenbus_probe(void *unused)
904 BUG_ON((xenstored_ready <= 0));
906 /* Enumerate devices in xenstore. */
907 xenbus_probe_devices(&xenbus_frontend);
908 xenbus_probe_devices(&xenbus_backend);
910 /* Watch for changes. */
911 register_xenbus_watch(&fe_watch);
912 register_xenbus_watch(&be_watch);
914 /* Notify others that xenstore is up */
915 blocking_notifier_call_chain(&xenstore_notifier_list, 0, NULL);
919 #ifdef CONFIG_PROC_FS
920 static struct file_operations xsd_kva_fops;
921 static struct proc_dir_entry *xsd_kva_intf;
922 static struct proc_dir_entry *xsd_port_intf;
924 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
926 size_t size = vma->vm_end - vma->vm_start;
928 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
931 if (remap_pfn_range(vma, vma->vm_start,
932 mfn_to_pfn(xen_start_info->store_mfn),
933 size, vma->vm_page_prot))
939 static int xsd_kva_read(char *page, char **start, off_t off,
940 int count, int *eof, void *data)
944 len = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
949 static int xsd_port_read(char *page, char **start, off_t off,
950 int count, int *eof, void *data)
954 len = sprintf(page, "%d", xen_start_info->store_evtchn);
961 static int __init xenbus_probe_init(void)
964 unsigned long page = 0;
968 if (!is_running_on_xen())
971 /* Register ourselves with the kernel bus subsystem */
972 bus_register(&xenbus_frontend.bus);
973 bus_register(&xenbus_backend.bus);
976 * Domain0 doesn't have a store_evtchn or store_mfn yet.
978 dom0 = (xen_start_info->store_evtchn == 0);
981 struct evtchn_alloc_unbound alloc_unbound;
984 page = get_zeroed_page(GFP_KERNEL);
988 xen_start_info->store_mfn =
989 pfn_to_mfn(virt_to_phys((void *)page) >>
992 /* Next allocate a local port which xenstored can bind to */
993 alloc_unbound.dom = DOMID_SELF;
994 alloc_unbound.remote_dom = 0;
996 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1001 xen_start_info->store_evtchn = alloc_unbound.port;
1003 #ifdef CONFIG_PROC_FS
1004 /* And finally publish the above info in /proc/xen */
1005 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1007 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1008 sizeof(xsd_kva_fops));
1009 xsd_kva_fops.mmap = xsd_kva_mmap;
1010 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1011 xsd_kva_intf->read_proc = xsd_kva_read;
1013 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1015 xsd_port_intf->read_proc = xsd_port_read;
1018 xenstored_ready = 1;
1020 /* Initialize the interface to xenstore. */
1024 "XENBUS: Error initializing xenstore comms: %i\n", err);
1028 /* Register ourselves with the kernel device subsystem */
1029 device_register(&xenbus_frontend.dev);
1030 device_register(&xenbus_backend.dev);
1042 * Do not unregister the xenbus front/backend buses here. The buses
1043 * must exist because front/backend drivers will use them when they are
1050 postcore_initcall(xenbus_probe_init);
1053 static int is_disconnected_device(struct device *dev, void *data)
1055 struct xenbus_device *xendev = to_xenbus_device(dev);
1056 struct device_driver *drv = data;
1059 * A device with no driver will never connect. We care only about
1060 * devices which should currently be in the process of connecting.
1065 /* Is this search limited to a particular driver? */
1066 if (drv && (dev->driver != drv))
1069 return (xendev->state != XenbusStateConnected);
1072 static int exists_disconnected_device(struct device_driver *drv)
1074 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1075 is_disconnected_device);
1078 static int print_device_status(struct device *dev, void *data)
1080 struct xenbus_device *xendev = to_xenbus_device(dev);
1081 struct device_driver *drv = data;
1083 /* Is this operation limited to a particular driver? */
1084 if (drv && (dev->driver != drv))
1088 /* Information only: is this too noisy? */
1089 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1091 } else if (xendev->state != XenbusStateConnected) {
1092 printk(KERN_WARNING "XENBUS: Timeout connecting "
1093 "to device: %s (state %d)\n",
1094 xendev->nodename, xendev->state);
1100 /* We only wait for device setup after most initcalls have run. */
1101 static int ready_to_wait_for_devices;
1104 * On a 10 second timeout, wait for all devices currently configured. We need
1105 * to do this to guarantee that the filesystems and / or network devices
1106 * needed for boot are available, before we can allow the boot to proceed.
1108 * This needs to be on a late_initcall, to happen after the frontend device
1109 * drivers have been initialised, but before the root fs is mounted.
1111 * A possible improvement here would be to have the tools add a per-device
1112 * flag to the store entry, indicating whether it is needed at boot time.
1113 * This would allow people who knew what they were doing to accelerate their
1114 * boot slightly, but of course needs tools or manual intervention to set up
1115 * those flags correctly.
1117 static void wait_for_devices(struct xenbus_driver *xendrv)
1119 unsigned long timeout = jiffies + 10*HZ;
1120 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1122 if (!ready_to_wait_for_devices || !is_running_on_xen())
1125 while (exists_disconnected_device(drv)) {
1126 if (time_after(jiffies, timeout))
1128 schedule_timeout_interruptible(HZ/10);
1131 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1132 print_device_status);
1135 static int __init boot_wait_for_devices(void)
1137 ready_to_wait_for_devices = 1;
1138 wait_for_devices(NULL);
1142 late_initcall(boot_wait_for_devices);