xen: wait up to 5 minutes for device connetion
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / xenbus / xenbus_probe.c
blob649fcdf114b7aab926cfbe4c3c348441d0c58ee0
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
30 * IN THE SOFTWARE.
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __func__, __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>
42 #include <linux/mm.h>
43 #include <linux/proc_fs.h>
44 #include <linux/notifier.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
47 #include <linux/io.h>
49 #include <asm/page.h>
50 #include <asm/pgtable.h>
51 #include <asm/xen/hypervisor.h>
52 #include <xen/xenbus.h>
53 #include <xen/events.h>
54 #include <xen/page.h>
56 #include "xenbus_comms.h"
57 #include "xenbus_probe.h"
60 int xen_store_evtchn;
61 EXPORT_SYMBOL(xen_store_evtchn);
63 struct xenstore_domain_interface *xen_store_interface;
64 static unsigned long xen_store_mfn;
66 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
68 static void wait_for_devices(struct xenbus_driver *xendrv);
70 static int xenbus_probe_frontend(const char *type, const char *name);
72 static void xenbus_dev_shutdown(struct device *_dev);
74 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
75 static int xenbus_dev_resume(struct device *dev);
77 /* If something in array of ids matches this device, return it. */
78 static const struct xenbus_device_id *
79 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
81 for (; *arr->devicetype != '\0'; arr++) {
82 if (!strcmp(arr->devicetype, dev->devicetype))
83 return arr;
85 return NULL;
88 int xenbus_match(struct device *_dev, struct device_driver *_drv)
90 struct xenbus_driver *drv = to_xenbus_driver(_drv);
92 if (!drv->ids)
93 return 0;
95 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
98 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
100 struct xenbus_device *dev = to_xenbus_device(_dev);
102 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
103 return -ENOMEM;
105 return 0;
108 /* device/<type>/<id> => <type>-<id> */
109 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
111 nodename = strchr(nodename, '/');
112 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
113 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
114 return -EINVAL;
117 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
118 if (!strchr(bus_id, '/')) {
119 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
120 return -EINVAL;
122 *strchr(bus_id, '/') = '-';
123 return 0;
127 static void free_otherend_details(struct xenbus_device *dev)
129 kfree(dev->otherend);
130 dev->otherend = NULL;
134 static void free_otherend_watch(struct xenbus_device *dev)
136 if (dev->otherend_watch.node) {
137 unregister_xenbus_watch(&dev->otherend_watch);
138 kfree(dev->otherend_watch.node);
139 dev->otherend_watch.node = NULL;
144 int read_otherend_details(struct xenbus_device *xendev,
145 char *id_node, char *path_node)
147 int err = xenbus_gather(XBT_NIL, xendev->nodename,
148 id_node, "%i", &xendev->otherend_id,
149 path_node, NULL, &xendev->otherend,
150 NULL);
151 if (err) {
152 xenbus_dev_fatal(xendev, err,
153 "reading other end details from %s",
154 xendev->nodename);
155 return err;
157 if (strlen(xendev->otherend) == 0 ||
158 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
159 xenbus_dev_fatal(xendev, -ENOENT,
160 "unable to read other end from %s. "
161 "missing or inaccessible.",
162 xendev->nodename);
163 free_otherend_details(xendev);
164 return -ENOENT;
167 return 0;
171 static int read_backend_details(struct xenbus_device *xendev)
173 return read_otherend_details(xendev, "backend-id", "backend");
176 static struct device_attribute xenbus_dev_attrs[] = {
177 __ATTR_NULL
180 /* Bus type for frontend drivers. */
181 static struct xen_bus_type xenbus_frontend = {
182 .root = "device",
183 .levels = 2, /* device/type/<id> */
184 .get_bus_id = frontend_bus_id,
185 .probe = xenbus_probe_frontend,
186 .bus = {
187 .name = "xen",
188 .match = xenbus_match,
189 .uevent = xenbus_uevent,
190 .probe = xenbus_dev_probe,
191 .remove = xenbus_dev_remove,
192 .shutdown = xenbus_dev_shutdown,
193 .dev_attrs = xenbus_dev_attrs,
195 .suspend = xenbus_dev_suspend,
196 .resume = xenbus_dev_resume,
200 static void otherend_changed(struct xenbus_watch *watch,
201 const char **vec, unsigned int len)
203 struct xenbus_device *dev =
204 container_of(watch, struct xenbus_device, otherend_watch);
205 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
206 enum xenbus_state state;
208 /* Protect us against watches firing on old details when the otherend
209 details change, say immediately after a resume. */
210 if (!dev->otherend ||
211 strncmp(dev->otherend, vec[XS_WATCH_PATH],
212 strlen(dev->otherend))) {
213 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
214 vec[XS_WATCH_PATH]);
215 return;
218 state = xenbus_read_driver_state(dev->otherend);
220 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
221 state, xenbus_strstate(state), dev->otherend_watch.node,
222 vec[XS_WATCH_PATH]);
225 * Ignore xenbus transitions during shutdown. This prevents us doing
226 * work that can fail e.g., when the rootfs is gone.
228 if (system_state > SYSTEM_RUNNING) {
229 struct xen_bus_type *bus = bus;
230 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
231 /* If we're frontend, drive the state machine to Closed. */
232 /* This should cause the backend to release our resources. */
233 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
234 xenbus_frontend_closed(dev);
235 return;
238 if (drv->otherend_changed)
239 drv->otherend_changed(dev, state);
243 static int talk_to_otherend(struct xenbus_device *dev)
245 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
247 free_otherend_watch(dev);
248 free_otherend_details(dev);
250 return drv->read_otherend_details(dev);
254 static int watch_otherend(struct xenbus_device *dev)
256 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
257 "%s/%s", dev->otherend, "state");
261 int xenbus_dev_probe(struct device *_dev)
263 struct xenbus_device *dev = to_xenbus_device(_dev);
264 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
265 const struct xenbus_device_id *id;
266 int err;
268 DPRINTK("%s", dev->nodename);
270 if (!drv->probe) {
271 err = -ENODEV;
272 goto fail;
275 id = match_device(drv->ids, dev);
276 if (!id) {
277 err = -ENODEV;
278 goto fail;
281 err = talk_to_otherend(dev);
282 if (err) {
283 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
284 dev->nodename);
285 return err;
288 err = drv->probe(dev, id);
289 if (err)
290 goto fail;
292 err = watch_otherend(dev);
293 if (err) {
294 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
295 dev->nodename);
296 return err;
299 return 0;
300 fail:
301 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
302 xenbus_switch_state(dev, XenbusStateClosed);
303 return -ENODEV;
306 int xenbus_dev_remove(struct device *_dev)
308 struct xenbus_device *dev = to_xenbus_device(_dev);
309 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
311 DPRINTK("%s", dev->nodename);
313 free_otherend_watch(dev);
314 free_otherend_details(dev);
316 if (drv->remove)
317 drv->remove(dev);
319 xenbus_switch_state(dev, XenbusStateClosed);
320 return 0;
323 static void xenbus_dev_shutdown(struct device *_dev)
325 struct xenbus_device *dev = to_xenbus_device(_dev);
326 unsigned long timeout = 5*HZ;
328 DPRINTK("%s", dev->nodename);
330 get_device(&dev->dev);
331 if (dev->state != XenbusStateConnected) {
332 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
333 dev->nodename, xenbus_strstate(dev->state));
334 goto out;
336 xenbus_switch_state(dev, XenbusStateClosing);
337 timeout = wait_for_completion_timeout(&dev->down, timeout);
338 if (!timeout)
339 printk(KERN_INFO "%s: %s timeout closing device\n",
340 __func__, dev->nodename);
341 out:
342 put_device(&dev->dev);
345 int xenbus_register_driver_common(struct xenbus_driver *drv,
346 struct xen_bus_type *bus,
347 struct module *owner,
348 const char *mod_name)
350 drv->driver.name = drv->name;
351 drv->driver.bus = &bus->bus;
352 drv->driver.owner = owner;
353 drv->driver.mod_name = mod_name;
355 return driver_register(&drv->driver);
358 int __xenbus_register_frontend(struct xenbus_driver *drv,
359 struct module *owner, const char *mod_name)
361 int ret;
363 drv->read_otherend_details = read_backend_details;
365 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
366 owner, mod_name);
367 if (ret)
368 return ret;
370 /* If this driver is loaded as a module wait for devices to attach. */
371 wait_for_devices(drv);
373 return 0;
375 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
377 void xenbus_unregister_driver(struct xenbus_driver *drv)
379 driver_unregister(&drv->driver);
381 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
383 struct xb_find_info
385 struct xenbus_device *dev;
386 const char *nodename;
389 static int cmp_dev(struct device *dev, void *data)
391 struct xenbus_device *xendev = to_xenbus_device(dev);
392 struct xb_find_info *info = data;
394 if (!strcmp(xendev->nodename, info->nodename)) {
395 info->dev = xendev;
396 get_device(dev);
397 return 1;
399 return 0;
402 struct xenbus_device *xenbus_device_find(const char *nodename,
403 struct bus_type *bus)
405 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
407 bus_for_each_dev(bus, NULL, &info, cmp_dev);
408 return info.dev;
411 static int cleanup_dev(struct device *dev, void *data)
413 struct xenbus_device *xendev = to_xenbus_device(dev);
414 struct xb_find_info *info = data;
415 int len = strlen(info->nodename);
417 DPRINTK("%s", info->nodename);
419 /* Match the info->nodename path, or any subdirectory of that path. */
420 if (strncmp(xendev->nodename, info->nodename, len))
421 return 0;
423 /* If the node name is longer, ensure it really is a subdirectory. */
424 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
425 return 0;
427 info->dev = xendev;
428 get_device(dev);
429 return 1;
432 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
434 struct xb_find_info info = { .nodename = path };
436 do {
437 info.dev = NULL;
438 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
439 if (info.dev) {
440 device_unregister(&info.dev->dev);
441 put_device(&info.dev->dev);
443 } while (info.dev);
446 static void xenbus_dev_release(struct device *dev)
448 if (dev)
449 kfree(to_xenbus_device(dev));
452 static ssize_t xendev_show_nodename(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
457 static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
459 static ssize_t xendev_show_devtype(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
464 static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
466 static ssize_t xendev_show_modalias(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
471 static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
473 int xenbus_probe_node(struct xen_bus_type *bus,
474 const char *type,
475 const char *nodename)
477 char devname[XEN_BUS_ID_SIZE];
478 int err;
479 struct xenbus_device *xendev;
480 size_t stringlen;
481 char *tmpstring;
483 enum xenbus_state state = xenbus_read_driver_state(nodename);
485 if (state != XenbusStateInitialising) {
486 /* Device is not new, so ignore it. This can happen if a
487 device is going away after switching to Closed. */
488 return 0;
491 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
492 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
493 if (!xendev)
494 return -ENOMEM;
496 xendev->state = XenbusStateInitialising;
498 /* Copy the strings into the extra space. */
500 tmpstring = (char *)(xendev + 1);
501 strcpy(tmpstring, nodename);
502 xendev->nodename = tmpstring;
504 tmpstring += strlen(tmpstring) + 1;
505 strcpy(tmpstring, type);
506 xendev->devicetype = tmpstring;
507 init_completion(&xendev->down);
509 xendev->dev.bus = &bus->bus;
510 xendev->dev.release = xenbus_dev_release;
512 err = bus->get_bus_id(devname, xendev->nodename);
513 if (err)
514 goto fail;
516 dev_set_name(&xendev->dev, devname);
518 /* Register with generic device framework. */
519 err = device_register(&xendev->dev);
520 if (err)
521 goto fail;
523 err = device_create_file(&xendev->dev, &dev_attr_nodename);
524 if (err)
525 goto fail_unregister;
527 err = device_create_file(&xendev->dev, &dev_attr_devtype);
528 if (err)
529 goto fail_remove_nodename;
531 err = device_create_file(&xendev->dev, &dev_attr_modalias);
532 if (err)
533 goto fail_remove_devtype;
535 return 0;
536 fail_remove_devtype:
537 device_remove_file(&xendev->dev, &dev_attr_devtype);
538 fail_remove_nodename:
539 device_remove_file(&xendev->dev, &dev_attr_nodename);
540 fail_unregister:
541 device_unregister(&xendev->dev);
542 fail:
543 kfree(xendev);
544 return err;
547 /* device/<typename>/<name> */
548 static int xenbus_probe_frontend(const char *type, const char *name)
550 char *nodename;
551 int err;
553 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
554 xenbus_frontend.root, type, name);
555 if (!nodename)
556 return -ENOMEM;
558 DPRINTK("%s", nodename);
560 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
561 kfree(nodename);
562 return err;
565 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
567 int err = 0;
568 char **dir;
569 unsigned int dir_n = 0;
570 int i;
572 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
573 if (IS_ERR(dir))
574 return PTR_ERR(dir);
576 for (i = 0; i < dir_n; i++) {
577 err = bus->probe(type, dir[i]);
578 if (err)
579 break;
581 kfree(dir);
582 return err;
585 int xenbus_probe_devices(struct xen_bus_type *bus)
587 int err = 0;
588 char **dir;
589 unsigned int i, dir_n;
591 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
592 if (IS_ERR(dir))
593 return PTR_ERR(dir);
595 for (i = 0; i < dir_n; i++) {
596 err = xenbus_probe_device_type(bus, dir[i]);
597 if (err)
598 break;
600 kfree(dir);
601 return err;
604 static unsigned int char_count(const char *str, char c)
606 unsigned int i, ret = 0;
608 for (i = 0; str[i]; i++)
609 if (str[i] == c)
610 ret++;
611 return ret;
614 static int strsep_len(const char *str, char c, unsigned int len)
616 unsigned int i;
618 for (i = 0; str[i]; i++)
619 if (str[i] == c) {
620 if (len == 0)
621 return i;
622 len--;
624 return (len == 0) ? i : -ERANGE;
627 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
629 int exists, rootlen;
630 struct xenbus_device *dev;
631 char type[XEN_BUS_ID_SIZE];
632 const char *p, *root;
634 if (char_count(node, '/') < 2)
635 return;
637 exists = xenbus_exists(XBT_NIL, node, "");
638 if (!exists) {
639 xenbus_cleanup_devices(node, &bus->bus);
640 return;
643 /* backend/<type>/... or device/<type>/... */
644 p = strchr(node, '/') + 1;
645 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
646 type[XEN_BUS_ID_SIZE-1] = '\0';
648 rootlen = strsep_len(node, '/', bus->levels);
649 if (rootlen < 0)
650 return;
651 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
652 if (!root)
653 return;
655 dev = xenbus_device_find(root, &bus->bus);
656 if (!dev)
657 xenbus_probe_node(bus, type, root);
658 else
659 put_device(&dev->dev);
661 kfree(root);
663 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
665 static void frontend_changed(struct xenbus_watch *watch,
666 const char **vec, unsigned int len)
668 DPRINTK("");
670 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
673 /* We watch for devices appearing and vanishing. */
674 static struct xenbus_watch fe_watch = {
675 .node = "device",
676 .callback = frontend_changed,
679 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
681 int err = 0;
682 struct xenbus_driver *drv;
683 struct xenbus_device *xdev;
685 DPRINTK("");
687 if (dev->driver == NULL)
688 return 0;
689 drv = to_xenbus_driver(dev->driver);
690 xdev = container_of(dev, struct xenbus_device, dev);
691 if (drv->suspend)
692 err = drv->suspend(xdev, state);
693 if (err)
694 printk(KERN_WARNING
695 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
696 return 0;
699 static int xenbus_dev_resume(struct device *dev)
701 int err;
702 struct xenbus_driver *drv;
703 struct xenbus_device *xdev;
705 DPRINTK("");
707 if (dev->driver == NULL)
708 return 0;
710 drv = to_xenbus_driver(dev->driver);
711 xdev = container_of(dev, struct xenbus_device, dev);
713 err = talk_to_otherend(xdev);
714 if (err) {
715 printk(KERN_WARNING
716 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
717 dev_name(dev), err);
718 return err;
721 xdev->state = XenbusStateInitialising;
723 if (drv->resume) {
724 err = drv->resume(xdev);
725 if (err) {
726 printk(KERN_WARNING
727 "xenbus: resume %s failed: %i\n",
728 dev_name(dev), err);
729 return err;
733 err = watch_otherend(xdev);
734 if (err) {
735 printk(KERN_WARNING
736 "xenbus_probe: resume (watch_otherend) %s failed: "
737 "%d.\n", dev_name(dev), err);
738 return err;
741 return 0;
744 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
745 int xenstored_ready = 0;
748 int register_xenstore_notifier(struct notifier_block *nb)
750 int ret = 0;
752 if (xenstored_ready > 0)
753 ret = nb->notifier_call(nb, 0, NULL);
754 else
755 blocking_notifier_chain_register(&xenstore_chain, nb);
757 return ret;
759 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
761 void unregister_xenstore_notifier(struct notifier_block *nb)
763 blocking_notifier_chain_unregister(&xenstore_chain, nb);
765 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
767 void xenbus_probe(struct work_struct *unused)
769 BUG_ON((xenstored_ready <= 0));
771 /* Enumerate devices in xenstore and watch for changes. */
772 xenbus_probe_devices(&xenbus_frontend);
773 register_xenbus_watch(&fe_watch);
774 xenbus_backend_probe_and_watch();
776 /* Notify others that xenstore is up */
777 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
780 static int __init xenbus_probe_init(void)
782 int err = 0;
784 DPRINTK("");
786 err = -ENODEV;
787 if (!xen_domain())
788 goto out_error;
790 /* Register ourselves with the kernel bus subsystem */
791 err = bus_register(&xenbus_frontend.bus);
792 if (err)
793 goto out_error;
795 err = xenbus_backend_bus_register();
796 if (err)
797 goto out_unreg_front;
800 * Domain0 doesn't have a store_evtchn or store_mfn yet.
802 if (xen_initial_domain()) {
803 /* dom0 not yet supported */
804 } else {
805 xenstored_ready = 1;
806 xen_store_evtchn = xen_start_info->store_evtchn;
807 xen_store_mfn = xen_start_info->store_mfn;
809 xen_store_interface = mfn_to_virt(xen_store_mfn);
811 /* Initialize the interface to xenstore. */
812 err = xs_init();
813 if (err) {
814 printk(KERN_WARNING
815 "XENBUS: Error initializing xenstore comms: %i\n", err);
816 goto out_unreg_back;
819 if (!xen_initial_domain())
820 xenbus_probe(NULL);
822 #ifdef CONFIG_XEN_COMPAT_XENFS
824 * Create xenfs mountpoint in /proc for compatibility with
825 * utilities that expect to find "xenbus" under "/proc/xen".
827 proc_mkdir("xen", NULL);
828 #endif
830 return 0;
832 out_unreg_back:
833 xenbus_backend_bus_unregister();
835 out_unreg_front:
836 bus_unregister(&xenbus_frontend.bus);
838 out_error:
839 return err;
842 postcore_initcall(xenbus_probe_init);
844 MODULE_LICENSE("GPL");
846 static int is_device_connecting(struct device *dev, void *data)
848 struct xenbus_device *xendev = to_xenbus_device(dev);
849 struct device_driver *drv = data;
850 struct xenbus_driver *xendrv;
853 * A device with no driver will never connect. We care only about
854 * devices which should currently be in the process of connecting.
856 if (!dev->driver)
857 return 0;
859 /* Is this search limited to a particular driver? */
860 if (drv && (dev->driver != drv))
861 return 0;
863 xendrv = to_xenbus_driver(dev->driver);
864 return (xendev->state < XenbusStateConnected ||
865 (xendev->state == XenbusStateConnected &&
866 xendrv->is_ready && !xendrv->is_ready(xendev)));
869 static int exists_connecting_device(struct device_driver *drv)
871 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
872 is_device_connecting);
875 static int print_device_status(struct device *dev, void *data)
877 struct xenbus_device *xendev = to_xenbus_device(dev);
878 struct device_driver *drv = data;
880 /* Is this operation limited to a particular driver? */
881 if (drv && (dev->driver != drv))
882 return 0;
884 if (!dev->driver) {
885 /* Information only: is this too noisy? */
886 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
887 xendev->nodename);
888 } else if (xendev->state < XenbusStateConnected) {
889 enum xenbus_state rstate = XenbusStateUnknown;
890 if (xendev->otherend)
891 rstate = xenbus_read_driver_state(xendev->otherend);
892 printk(KERN_WARNING "XENBUS: Timeout connecting "
893 "to device: %s (local state %d, remote state %d)\n",
894 xendev->nodename, xendev->state, rstate);
897 return 0;
900 /* We only wait for device setup after most initcalls have run. */
901 static int ready_to_wait_for_devices;
904 * On a 5-minute timeout, wait for all devices currently configured. We need
905 * to do this to guarantee that the filesystems and / or network devices
906 * needed for boot are available, before we can allow the boot to proceed.
908 * This needs to be on a late_initcall, to happen after the frontend device
909 * drivers have been initialised, but before the root fs is mounted.
911 * A possible improvement here would be to have the tools add a per-device
912 * flag to the store entry, indicating whether it is needed at boot time.
913 * This would allow people who knew what they were doing to accelerate their
914 * boot slightly, but of course needs tools or manual intervention to set up
915 * those flags correctly.
917 static void wait_for_devices(struct xenbus_driver *xendrv)
919 unsigned long start = jiffies;
920 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
921 unsigned int seconds_waited = 0;
923 if (!ready_to_wait_for_devices || !xen_domain())
924 return;
926 while (exists_connecting_device(drv)) {
927 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
928 if (!seconds_waited)
929 printk(KERN_WARNING "XENBUS: Waiting for "
930 "devices to initialise: ");
931 seconds_waited += 5;
932 printk("%us...", 300 - seconds_waited);
933 if (seconds_waited == 300)
934 break;
937 schedule_timeout_interruptible(HZ/10);
940 if (seconds_waited)
941 printk("\n");
943 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
944 print_device_status);
947 #ifndef MODULE
948 static int __init boot_wait_for_devices(void)
950 ready_to_wait_for_devices = 1;
951 wait_for_devices(NULL);
952 return 0;
955 late_initcall(boot_wait_for_devices);
956 #endif