Linux 2.6.34-rc3
[pohmelfs.git] / drivers / xen / xenbus / xenbus_probe.c
blob2f7aaa99dc47e5d26701308e761612a2cdcbec8e
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>
53 #include <xen/xen.h>
54 #include <xen/xenbus.h>
55 #include <xen/events.h>
56 #include <xen/page.h>
58 #include "xenbus_comms.h"
59 #include "xenbus_probe.h"
62 int xen_store_evtchn;
63 EXPORT_SYMBOL(xen_store_evtchn);
65 struct xenstore_domain_interface *xen_store_interface;
66 static unsigned long xen_store_mfn;
68 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
70 static void wait_for_devices(struct xenbus_driver *xendrv);
72 static int xenbus_probe_frontend(const char *type, const char *name);
74 static void xenbus_dev_shutdown(struct device *_dev);
76 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
77 static int xenbus_dev_resume(struct device *dev);
79 /* If something in array of ids matches this device, return it. */
80 static const struct xenbus_device_id *
81 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
83 for (; *arr->devicetype != '\0'; arr++) {
84 if (!strcmp(arr->devicetype, dev->devicetype))
85 return arr;
87 return NULL;
90 int xenbus_match(struct device *_dev, struct device_driver *_drv)
92 struct xenbus_driver *drv = to_xenbus_driver(_drv);
94 if (!drv->ids)
95 return 0;
97 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
100 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
102 struct xenbus_device *dev = to_xenbus_device(_dev);
104 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
105 return -ENOMEM;
107 return 0;
110 /* device/<type>/<id> => <type>-<id> */
111 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
113 nodename = strchr(nodename, '/');
114 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
115 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
116 return -EINVAL;
119 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
120 if (!strchr(bus_id, '/')) {
121 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
122 return -EINVAL;
124 *strchr(bus_id, '/') = '-';
125 return 0;
129 static void free_otherend_details(struct xenbus_device *dev)
131 kfree(dev->otherend);
132 dev->otherend = NULL;
136 static void free_otherend_watch(struct xenbus_device *dev)
138 if (dev->otherend_watch.node) {
139 unregister_xenbus_watch(&dev->otherend_watch);
140 kfree(dev->otherend_watch.node);
141 dev->otherend_watch.node = NULL;
146 int read_otherend_details(struct xenbus_device *xendev,
147 char *id_node, char *path_node)
149 int err = xenbus_gather(XBT_NIL, xendev->nodename,
150 id_node, "%i", &xendev->otherend_id,
151 path_node, NULL, &xendev->otherend,
152 NULL);
153 if (err) {
154 xenbus_dev_fatal(xendev, err,
155 "reading other end details from %s",
156 xendev->nodename);
157 return err;
159 if (strlen(xendev->otherend) == 0 ||
160 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
161 xenbus_dev_fatal(xendev, -ENOENT,
162 "unable to read other end from %s. "
163 "missing or inaccessible.",
164 xendev->nodename);
165 free_otherend_details(xendev);
166 return -ENOENT;
169 return 0;
173 static int read_backend_details(struct xenbus_device *xendev)
175 return read_otherend_details(xendev, "backend-id", "backend");
178 static struct device_attribute xenbus_dev_attrs[] = {
179 __ATTR_NULL
182 /* Bus type for frontend drivers. */
183 static struct xen_bus_type xenbus_frontend = {
184 .root = "device",
185 .levels = 2, /* device/type/<id> */
186 .get_bus_id = frontend_bus_id,
187 .probe = xenbus_probe_frontend,
188 .bus = {
189 .name = "xen",
190 .match = xenbus_match,
191 .uevent = xenbus_uevent,
192 .probe = xenbus_dev_probe,
193 .remove = xenbus_dev_remove,
194 .shutdown = xenbus_dev_shutdown,
195 .dev_attrs = xenbus_dev_attrs,
197 .suspend = xenbus_dev_suspend,
198 .resume = xenbus_dev_resume,
202 static void otherend_changed(struct xenbus_watch *watch,
203 const char **vec, unsigned int len)
205 struct xenbus_device *dev =
206 container_of(watch, struct xenbus_device, otherend_watch);
207 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
208 enum xenbus_state state;
210 /* Protect us against watches firing on old details when the otherend
211 details change, say immediately after a resume. */
212 if (!dev->otherend ||
213 strncmp(dev->otherend, vec[XS_WATCH_PATH],
214 strlen(dev->otherend))) {
215 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
216 vec[XS_WATCH_PATH]);
217 return;
220 state = xenbus_read_driver_state(dev->otherend);
222 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
223 state, xenbus_strstate(state), dev->otherend_watch.node,
224 vec[XS_WATCH_PATH]);
227 * Ignore xenbus transitions during shutdown. This prevents us doing
228 * work that can fail e.g., when the rootfs is gone.
230 if (system_state > SYSTEM_RUNNING) {
231 struct xen_bus_type *bus = bus;
232 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
233 /* If we're frontend, drive the state machine to Closed. */
234 /* This should cause the backend to release our resources. */
235 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
236 xenbus_frontend_closed(dev);
237 return;
240 if (drv->otherend_changed)
241 drv->otherend_changed(dev, state);
245 static int talk_to_otherend(struct xenbus_device *dev)
247 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
249 free_otherend_watch(dev);
250 free_otherend_details(dev);
252 return drv->read_otherend_details(dev);
256 static int watch_otherend(struct xenbus_device *dev)
258 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
259 "%s/%s", dev->otherend, "state");
263 int xenbus_dev_probe(struct device *_dev)
265 struct xenbus_device *dev = to_xenbus_device(_dev);
266 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
267 const struct xenbus_device_id *id;
268 int err;
270 DPRINTK("%s", dev->nodename);
272 if (!drv->probe) {
273 err = -ENODEV;
274 goto fail;
277 id = match_device(drv->ids, dev);
278 if (!id) {
279 err = -ENODEV;
280 goto fail;
283 err = talk_to_otherend(dev);
284 if (err) {
285 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
286 dev->nodename);
287 return err;
290 err = drv->probe(dev, id);
291 if (err)
292 goto fail;
294 err = watch_otherend(dev);
295 if (err) {
296 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
297 dev->nodename);
298 return err;
301 return 0;
302 fail:
303 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
304 xenbus_switch_state(dev, XenbusStateClosed);
305 return -ENODEV;
308 int xenbus_dev_remove(struct device *_dev)
310 struct xenbus_device *dev = to_xenbus_device(_dev);
311 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
313 DPRINTK("%s", dev->nodename);
315 free_otherend_watch(dev);
316 free_otherend_details(dev);
318 if (drv->remove)
319 drv->remove(dev);
321 xenbus_switch_state(dev, XenbusStateClosed);
322 return 0;
325 static void xenbus_dev_shutdown(struct device *_dev)
327 struct xenbus_device *dev = to_xenbus_device(_dev);
328 unsigned long timeout = 5*HZ;
330 DPRINTK("%s", dev->nodename);
332 get_device(&dev->dev);
333 if (dev->state != XenbusStateConnected) {
334 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
335 dev->nodename, xenbus_strstate(dev->state));
336 goto out;
338 xenbus_switch_state(dev, XenbusStateClosing);
339 timeout = wait_for_completion_timeout(&dev->down, timeout);
340 if (!timeout)
341 printk(KERN_INFO "%s: %s timeout closing device\n",
342 __func__, dev->nodename);
343 out:
344 put_device(&dev->dev);
347 int xenbus_register_driver_common(struct xenbus_driver *drv,
348 struct xen_bus_type *bus,
349 struct module *owner,
350 const char *mod_name)
352 drv->driver.name = drv->name;
353 drv->driver.bus = &bus->bus;
354 drv->driver.owner = owner;
355 drv->driver.mod_name = mod_name;
357 return driver_register(&drv->driver);
360 int __xenbus_register_frontend(struct xenbus_driver *drv,
361 struct module *owner, const char *mod_name)
363 int ret;
365 drv->read_otherend_details = read_backend_details;
367 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
368 owner, mod_name);
369 if (ret)
370 return ret;
372 /* If this driver is loaded as a module wait for devices to attach. */
373 wait_for_devices(drv);
375 return 0;
377 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
379 void xenbus_unregister_driver(struct xenbus_driver *drv)
381 driver_unregister(&drv->driver);
383 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
385 struct xb_find_info
387 struct xenbus_device *dev;
388 const char *nodename;
391 static int cmp_dev(struct device *dev, void *data)
393 struct xenbus_device *xendev = to_xenbus_device(dev);
394 struct xb_find_info *info = data;
396 if (!strcmp(xendev->nodename, info->nodename)) {
397 info->dev = xendev;
398 get_device(dev);
399 return 1;
401 return 0;
404 struct xenbus_device *xenbus_device_find(const char *nodename,
405 struct bus_type *bus)
407 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
409 bus_for_each_dev(bus, NULL, &info, cmp_dev);
410 return info.dev;
413 static int cleanup_dev(struct device *dev, void *data)
415 struct xenbus_device *xendev = to_xenbus_device(dev);
416 struct xb_find_info *info = data;
417 int len = strlen(info->nodename);
419 DPRINTK("%s", info->nodename);
421 /* Match the info->nodename path, or any subdirectory of that path. */
422 if (strncmp(xendev->nodename, info->nodename, len))
423 return 0;
425 /* If the node name is longer, ensure it really is a subdirectory. */
426 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
427 return 0;
429 info->dev = xendev;
430 get_device(dev);
431 return 1;
434 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
436 struct xb_find_info info = { .nodename = path };
438 do {
439 info.dev = NULL;
440 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
441 if (info.dev) {
442 device_unregister(&info.dev->dev);
443 put_device(&info.dev->dev);
445 } while (info.dev);
448 static void xenbus_dev_release(struct device *dev)
450 if (dev)
451 kfree(to_xenbus_device(dev));
454 static ssize_t xendev_show_nodename(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
459 static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
461 static ssize_t xendev_show_devtype(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
466 static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
468 static ssize_t xendev_show_modalias(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
473 static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
475 int xenbus_probe_node(struct xen_bus_type *bus,
476 const char *type,
477 const char *nodename)
479 char devname[XEN_BUS_ID_SIZE];
480 int err;
481 struct xenbus_device *xendev;
482 size_t stringlen;
483 char *tmpstring;
485 enum xenbus_state state = xenbus_read_driver_state(nodename);
487 if (state != XenbusStateInitialising) {
488 /* Device is not new, so ignore it. This can happen if a
489 device is going away after switching to Closed. */
490 return 0;
493 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
494 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
495 if (!xendev)
496 return -ENOMEM;
498 xendev->state = XenbusStateInitialising;
500 /* Copy the strings into the extra space. */
502 tmpstring = (char *)(xendev + 1);
503 strcpy(tmpstring, nodename);
504 xendev->nodename = tmpstring;
506 tmpstring += strlen(tmpstring) + 1;
507 strcpy(tmpstring, type);
508 xendev->devicetype = tmpstring;
509 init_completion(&xendev->down);
511 xendev->dev.bus = &bus->bus;
512 xendev->dev.release = xenbus_dev_release;
514 err = bus->get_bus_id(devname, xendev->nodename);
515 if (err)
516 goto fail;
518 dev_set_name(&xendev->dev, devname);
520 /* Register with generic device framework. */
521 err = device_register(&xendev->dev);
522 if (err)
523 goto fail;
525 err = device_create_file(&xendev->dev, &dev_attr_nodename);
526 if (err)
527 goto fail_unregister;
529 err = device_create_file(&xendev->dev, &dev_attr_devtype);
530 if (err)
531 goto fail_remove_nodename;
533 err = device_create_file(&xendev->dev, &dev_attr_modalias);
534 if (err)
535 goto fail_remove_devtype;
537 return 0;
538 fail_remove_devtype:
539 device_remove_file(&xendev->dev, &dev_attr_devtype);
540 fail_remove_nodename:
541 device_remove_file(&xendev->dev, &dev_attr_nodename);
542 fail_unregister:
543 device_unregister(&xendev->dev);
544 fail:
545 kfree(xendev);
546 return err;
549 /* device/<typename>/<name> */
550 static int xenbus_probe_frontend(const char *type, const char *name)
552 char *nodename;
553 int err;
555 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
556 xenbus_frontend.root, type, name);
557 if (!nodename)
558 return -ENOMEM;
560 DPRINTK("%s", nodename);
562 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
563 kfree(nodename);
564 return err;
567 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
569 int err = 0;
570 char **dir;
571 unsigned int dir_n = 0;
572 int i;
574 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
575 if (IS_ERR(dir))
576 return PTR_ERR(dir);
578 for (i = 0; i < dir_n; i++) {
579 err = bus->probe(type, dir[i]);
580 if (err)
581 break;
583 kfree(dir);
584 return err;
587 int xenbus_probe_devices(struct xen_bus_type *bus)
589 int err = 0;
590 char **dir;
591 unsigned int i, dir_n;
593 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
594 if (IS_ERR(dir))
595 return PTR_ERR(dir);
597 for (i = 0; i < dir_n; i++) {
598 err = xenbus_probe_device_type(bus, dir[i]);
599 if (err)
600 break;
602 kfree(dir);
603 return err;
606 static unsigned int char_count(const char *str, char c)
608 unsigned int i, ret = 0;
610 for (i = 0; str[i]; i++)
611 if (str[i] == c)
612 ret++;
613 return ret;
616 static int strsep_len(const char *str, char c, unsigned int len)
618 unsigned int i;
620 for (i = 0; str[i]; i++)
621 if (str[i] == c) {
622 if (len == 0)
623 return i;
624 len--;
626 return (len == 0) ? i : -ERANGE;
629 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
631 int exists, rootlen;
632 struct xenbus_device *dev;
633 char type[XEN_BUS_ID_SIZE];
634 const char *p, *root;
636 if (char_count(node, '/') < 2)
637 return;
639 exists = xenbus_exists(XBT_NIL, node, "");
640 if (!exists) {
641 xenbus_cleanup_devices(node, &bus->bus);
642 return;
645 /* backend/<type>/... or device/<type>/... */
646 p = strchr(node, '/') + 1;
647 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
648 type[XEN_BUS_ID_SIZE-1] = '\0';
650 rootlen = strsep_len(node, '/', bus->levels);
651 if (rootlen < 0)
652 return;
653 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
654 if (!root)
655 return;
657 dev = xenbus_device_find(root, &bus->bus);
658 if (!dev)
659 xenbus_probe_node(bus, type, root);
660 else
661 put_device(&dev->dev);
663 kfree(root);
665 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
667 static void frontend_changed(struct xenbus_watch *watch,
668 const char **vec, unsigned int len)
670 DPRINTK("");
672 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
675 /* We watch for devices appearing and vanishing. */
676 static struct xenbus_watch fe_watch = {
677 .node = "device",
678 .callback = frontend_changed,
681 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
683 int err = 0;
684 struct xenbus_driver *drv;
685 struct xenbus_device *xdev;
687 DPRINTK("");
689 if (dev->driver == NULL)
690 return 0;
691 drv = to_xenbus_driver(dev->driver);
692 xdev = container_of(dev, struct xenbus_device, dev);
693 if (drv->suspend)
694 err = drv->suspend(xdev, state);
695 if (err)
696 printk(KERN_WARNING
697 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
698 return 0;
701 static int xenbus_dev_resume(struct device *dev)
703 int err;
704 struct xenbus_driver *drv;
705 struct xenbus_device *xdev;
707 DPRINTK("");
709 if (dev->driver == NULL)
710 return 0;
712 drv = to_xenbus_driver(dev->driver);
713 xdev = container_of(dev, struct xenbus_device, dev);
715 err = talk_to_otherend(xdev);
716 if (err) {
717 printk(KERN_WARNING
718 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
719 dev_name(dev), err);
720 return err;
723 xdev->state = XenbusStateInitialising;
725 if (drv->resume) {
726 err = drv->resume(xdev);
727 if (err) {
728 printk(KERN_WARNING
729 "xenbus: resume %s failed: %i\n",
730 dev_name(dev), err);
731 return err;
735 err = watch_otherend(xdev);
736 if (err) {
737 printk(KERN_WARNING
738 "xenbus_probe: resume (watch_otherend) %s failed: "
739 "%d.\n", dev_name(dev), err);
740 return err;
743 return 0;
746 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
747 int xenstored_ready = 0;
750 int register_xenstore_notifier(struct notifier_block *nb)
752 int ret = 0;
754 if (xenstored_ready > 0)
755 ret = nb->notifier_call(nb, 0, NULL);
756 else
757 blocking_notifier_chain_register(&xenstore_chain, nb);
759 return ret;
761 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
763 void unregister_xenstore_notifier(struct notifier_block *nb)
765 blocking_notifier_chain_unregister(&xenstore_chain, nb);
767 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
769 void xenbus_probe(struct work_struct *unused)
771 BUG_ON((xenstored_ready <= 0));
773 /* Enumerate devices in xenstore and watch for changes. */
774 xenbus_probe_devices(&xenbus_frontend);
775 register_xenbus_watch(&fe_watch);
776 xenbus_backend_probe_and_watch();
778 /* Notify others that xenstore is up */
779 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
782 static int __init xenbus_probe_init(void)
784 int err = 0;
786 DPRINTK("");
788 err = -ENODEV;
789 if (!xen_domain())
790 goto out_error;
792 /* Register ourselves with the kernel bus subsystem */
793 err = bus_register(&xenbus_frontend.bus);
794 if (err)
795 goto out_error;
797 err = xenbus_backend_bus_register();
798 if (err)
799 goto out_unreg_front;
802 * Domain0 doesn't have a store_evtchn or store_mfn yet.
804 if (xen_initial_domain()) {
805 /* dom0 not yet supported */
806 } else {
807 xenstored_ready = 1;
808 xen_store_evtchn = xen_start_info->store_evtchn;
809 xen_store_mfn = xen_start_info->store_mfn;
811 xen_store_interface = mfn_to_virt(xen_store_mfn);
813 /* Initialize the interface to xenstore. */
814 err = xs_init();
815 if (err) {
816 printk(KERN_WARNING
817 "XENBUS: Error initializing xenstore comms: %i\n", err);
818 goto out_unreg_back;
821 if (!xen_initial_domain())
822 xenbus_probe(NULL);
824 #ifdef CONFIG_XEN_COMPAT_XENFS
826 * Create xenfs mountpoint in /proc for compatibility with
827 * utilities that expect to find "xenbus" under "/proc/xen".
829 proc_mkdir("xen", NULL);
830 #endif
832 return 0;
834 out_unreg_back:
835 xenbus_backend_bus_unregister();
837 out_unreg_front:
838 bus_unregister(&xenbus_frontend.bus);
840 out_error:
841 return err;
844 postcore_initcall(xenbus_probe_init);
846 MODULE_LICENSE("GPL");
848 static int is_device_connecting(struct device *dev, void *data)
850 struct xenbus_device *xendev = to_xenbus_device(dev);
851 struct device_driver *drv = data;
852 struct xenbus_driver *xendrv;
855 * A device with no driver will never connect. We care only about
856 * devices which should currently be in the process of connecting.
858 if (!dev->driver)
859 return 0;
861 /* Is this search limited to a particular driver? */
862 if (drv && (dev->driver != drv))
863 return 0;
865 xendrv = to_xenbus_driver(dev->driver);
866 return (xendev->state < XenbusStateConnected ||
867 (xendev->state == XenbusStateConnected &&
868 xendrv->is_ready && !xendrv->is_ready(xendev)));
871 static int exists_connecting_device(struct device_driver *drv)
873 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
874 is_device_connecting);
877 static int print_device_status(struct device *dev, void *data)
879 struct xenbus_device *xendev = to_xenbus_device(dev);
880 struct device_driver *drv = data;
882 /* Is this operation limited to a particular driver? */
883 if (drv && (dev->driver != drv))
884 return 0;
886 if (!dev->driver) {
887 /* Information only: is this too noisy? */
888 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
889 xendev->nodename);
890 } else if (xendev->state < XenbusStateConnected) {
891 enum xenbus_state rstate = XenbusStateUnknown;
892 if (xendev->otherend)
893 rstate = xenbus_read_driver_state(xendev->otherend);
894 printk(KERN_WARNING "XENBUS: Timeout connecting "
895 "to device: %s (local state %d, remote state %d)\n",
896 xendev->nodename, xendev->state, rstate);
899 return 0;
902 /* We only wait for device setup after most initcalls have run. */
903 static int ready_to_wait_for_devices;
906 * On a 5-minute timeout, wait for all devices currently configured. We need
907 * to do this to guarantee that the filesystems and / or network devices
908 * needed for boot are available, before we can allow the boot to proceed.
910 * This needs to be on a late_initcall, to happen after the frontend device
911 * drivers have been initialised, but before the root fs is mounted.
913 * A possible improvement here would be to have the tools add a per-device
914 * flag to the store entry, indicating whether it is needed at boot time.
915 * This would allow people who knew what they were doing to accelerate their
916 * boot slightly, but of course needs tools or manual intervention to set up
917 * those flags correctly.
919 static void wait_for_devices(struct xenbus_driver *xendrv)
921 unsigned long start = jiffies;
922 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
923 unsigned int seconds_waited = 0;
925 if (!ready_to_wait_for_devices || !xen_domain())
926 return;
928 while (exists_connecting_device(drv)) {
929 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
930 if (!seconds_waited)
931 printk(KERN_WARNING "XENBUS: Waiting for "
932 "devices to initialise: ");
933 seconds_waited += 5;
934 printk("%us...", 300 - seconds_waited);
935 if (seconds_waited == 300)
936 break;
939 schedule_timeout_interruptible(HZ/10);
942 if (seconds_waited)
943 printk("\n");
945 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
946 print_device_status);
949 #ifndef MODULE
950 static int __init boot_wait_for_devices(void)
952 ready_to_wait_for_devices = 1;
953 wait_for_devices(NULL);
954 return 0;
957 late_initcall(boot_wait_for_devices);
958 #endif