Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / xen / xenbus / xenbus_probe.c
blob3479332113e998abfd311ffc079d548a342878c5
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>
48 #include <linux/slab.h>
50 #include <asm/page.h>
51 #include <asm/pgtable.h>
52 #include <asm/xen/hypervisor.h>
54 #include <xen/xen.h>
55 #include <xen/xenbus.h>
56 #include <xen/events.h>
57 #include <xen/page.h>
59 #include "xenbus_comms.h"
60 #include "xenbus_probe.h"
63 int xen_store_evtchn;
64 EXPORT_SYMBOL(xen_store_evtchn);
66 struct xenstore_domain_interface *xen_store_interface;
67 static unsigned long xen_store_mfn;
69 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
71 static void wait_for_devices(struct xenbus_driver *xendrv);
73 static int xenbus_probe_frontend(const char *type, const char *name);
75 static void xenbus_dev_shutdown(struct device *_dev);
77 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
78 static int xenbus_dev_resume(struct device *dev);
80 /* If something in array of ids matches this device, return it. */
81 static const struct xenbus_device_id *
82 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
84 for (; *arr->devicetype != '\0'; arr++) {
85 if (!strcmp(arr->devicetype, dev->devicetype))
86 return arr;
88 return NULL;
91 int xenbus_match(struct device *_dev, struct device_driver *_drv)
93 struct xenbus_driver *drv = to_xenbus_driver(_drv);
95 if (!drv->ids)
96 return 0;
98 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
101 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
103 struct xenbus_device *dev = to_xenbus_device(_dev);
105 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
106 return -ENOMEM;
108 return 0;
111 /* device/<type>/<id> => <type>-<id> */
112 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
114 nodename = strchr(nodename, '/');
115 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
116 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
117 return -EINVAL;
120 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
121 if (!strchr(bus_id, '/')) {
122 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
123 return -EINVAL;
125 *strchr(bus_id, '/') = '-';
126 return 0;
130 static void free_otherend_details(struct xenbus_device *dev)
132 kfree(dev->otherend);
133 dev->otherend = NULL;
137 static void free_otherend_watch(struct xenbus_device *dev)
139 if (dev->otherend_watch.node) {
140 unregister_xenbus_watch(&dev->otherend_watch);
141 kfree(dev->otherend_watch.node);
142 dev->otherend_watch.node = NULL;
147 int read_otherend_details(struct xenbus_device *xendev,
148 char *id_node, char *path_node)
150 int err = xenbus_gather(XBT_NIL, xendev->nodename,
151 id_node, "%i", &xendev->otherend_id,
152 path_node, NULL, &xendev->otherend,
153 NULL);
154 if (err) {
155 xenbus_dev_fatal(xendev, err,
156 "reading other end details from %s",
157 xendev->nodename);
158 return err;
160 if (strlen(xendev->otherend) == 0 ||
161 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
162 xenbus_dev_fatal(xendev, -ENOENT,
163 "unable to read other end from %s. "
164 "missing or inaccessible.",
165 xendev->nodename);
166 free_otherend_details(xendev);
167 return -ENOENT;
170 return 0;
174 static int read_backend_details(struct xenbus_device *xendev)
176 return read_otherend_details(xendev, "backend-id", "backend");
179 static struct device_attribute xenbus_dev_attrs[] = {
180 __ATTR_NULL
183 /* Bus type for frontend drivers. */
184 static struct xen_bus_type xenbus_frontend = {
185 .root = "device",
186 .levels = 2, /* device/type/<id> */
187 .get_bus_id = frontend_bus_id,
188 .probe = xenbus_probe_frontend,
189 .bus = {
190 .name = "xen",
191 .match = xenbus_match,
192 .uevent = xenbus_uevent,
193 .probe = xenbus_dev_probe,
194 .remove = xenbus_dev_remove,
195 .shutdown = xenbus_dev_shutdown,
196 .dev_attrs = xenbus_dev_attrs,
198 .suspend = xenbus_dev_suspend,
199 .resume = xenbus_dev_resume,
203 static void otherend_changed(struct xenbus_watch *watch,
204 const char **vec, unsigned int len)
206 struct xenbus_device *dev =
207 container_of(watch, struct xenbus_device, otherend_watch);
208 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
209 enum xenbus_state state;
211 /* Protect us against watches firing on old details when the otherend
212 details change, say immediately after a resume. */
213 if (!dev->otherend ||
214 strncmp(dev->otherend, vec[XS_WATCH_PATH],
215 strlen(dev->otherend))) {
216 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
217 vec[XS_WATCH_PATH]);
218 return;
221 state = xenbus_read_driver_state(dev->otherend);
223 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
224 state, xenbus_strstate(state), dev->otherend_watch.node,
225 vec[XS_WATCH_PATH]);
228 * Ignore xenbus transitions during shutdown. This prevents us doing
229 * work that can fail e.g., when the rootfs is gone.
231 if (system_state > SYSTEM_RUNNING) {
232 struct xen_bus_type *bus = bus;
233 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
234 /* If we're frontend, drive the state machine to Closed. */
235 /* This should cause the backend to release our resources. */
236 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
237 xenbus_frontend_closed(dev);
238 return;
241 if (drv->otherend_changed)
242 drv->otherend_changed(dev, state);
246 static int talk_to_otherend(struct xenbus_device *dev)
248 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
250 free_otherend_watch(dev);
251 free_otherend_details(dev);
253 return drv->read_otherend_details(dev);
257 static int watch_otherend(struct xenbus_device *dev)
259 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
260 "%s/%s", dev->otherend, "state");
264 int xenbus_dev_probe(struct device *_dev)
266 struct xenbus_device *dev = to_xenbus_device(_dev);
267 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
268 const struct xenbus_device_id *id;
269 int err;
271 DPRINTK("%s", dev->nodename);
273 if (!drv->probe) {
274 err = -ENODEV;
275 goto fail;
278 id = match_device(drv->ids, dev);
279 if (!id) {
280 err = -ENODEV;
281 goto fail;
284 err = talk_to_otherend(dev);
285 if (err) {
286 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
287 dev->nodename);
288 return err;
291 err = drv->probe(dev, id);
292 if (err)
293 goto fail;
295 err = watch_otherend(dev);
296 if (err) {
297 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
298 dev->nodename);
299 return err;
302 return 0;
303 fail:
304 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
305 xenbus_switch_state(dev, XenbusStateClosed);
306 return -ENODEV;
309 int xenbus_dev_remove(struct device *_dev)
311 struct xenbus_device *dev = to_xenbus_device(_dev);
312 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
314 DPRINTK("%s", dev->nodename);
316 free_otherend_watch(dev);
317 free_otherend_details(dev);
319 if (drv->remove)
320 drv->remove(dev);
322 xenbus_switch_state(dev, XenbusStateClosed);
323 return 0;
326 static void xenbus_dev_shutdown(struct device *_dev)
328 struct xenbus_device *dev = to_xenbus_device(_dev);
329 unsigned long timeout = 5*HZ;
331 DPRINTK("%s", dev->nodename);
333 get_device(&dev->dev);
334 if (dev->state != XenbusStateConnected) {
335 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
336 dev->nodename, xenbus_strstate(dev->state));
337 goto out;
339 xenbus_switch_state(dev, XenbusStateClosing);
340 timeout = wait_for_completion_timeout(&dev->down, timeout);
341 if (!timeout)
342 printk(KERN_INFO "%s: %s timeout closing device\n",
343 __func__, dev->nodename);
344 out:
345 put_device(&dev->dev);
348 int xenbus_register_driver_common(struct xenbus_driver *drv,
349 struct xen_bus_type *bus,
350 struct module *owner,
351 const char *mod_name)
353 drv->driver.name = drv->name;
354 drv->driver.bus = &bus->bus;
355 drv->driver.owner = owner;
356 drv->driver.mod_name = mod_name;
358 return driver_register(&drv->driver);
361 int __xenbus_register_frontend(struct xenbus_driver *drv,
362 struct module *owner, const char *mod_name)
364 int ret;
366 drv->read_otherend_details = read_backend_details;
368 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
369 owner, mod_name);
370 if (ret)
371 return ret;
373 /* If this driver is loaded as a module wait for devices to attach. */
374 wait_for_devices(drv);
376 return 0;
378 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
380 void xenbus_unregister_driver(struct xenbus_driver *drv)
382 driver_unregister(&drv->driver);
384 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
386 struct xb_find_info
388 struct xenbus_device *dev;
389 const char *nodename;
392 static int cmp_dev(struct device *dev, void *data)
394 struct xenbus_device *xendev = to_xenbus_device(dev);
395 struct xb_find_info *info = data;
397 if (!strcmp(xendev->nodename, info->nodename)) {
398 info->dev = xendev;
399 get_device(dev);
400 return 1;
402 return 0;
405 struct xenbus_device *xenbus_device_find(const char *nodename,
406 struct bus_type *bus)
408 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
410 bus_for_each_dev(bus, NULL, &info, cmp_dev);
411 return info.dev;
414 static int cleanup_dev(struct device *dev, void *data)
416 struct xenbus_device *xendev = to_xenbus_device(dev);
417 struct xb_find_info *info = data;
418 int len = strlen(info->nodename);
420 DPRINTK("%s", info->nodename);
422 /* Match the info->nodename path, or any subdirectory of that path. */
423 if (strncmp(xendev->nodename, info->nodename, len))
424 return 0;
426 /* If the node name is longer, ensure it really is a subdirectory. */
427 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
428 return 0;
430 info->dev = xendev;
431 get_device(dev);
432 return 1;
435 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
437 struct xb_find_info info = { .nodename = path };
439 do {
440 info.dev = NULL;
441 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
442 if (info.dev) {
443 device_unregister(&info.dev->dev);
444 put_device(&info.dev->dev);
446 } while (info.dev);
449 static void xenbus_dev_release(struct device *dev)
451 if (dev)
452 kfree(to_xenbus_device(dev));
455 static ssize_t xendev_show_nodename(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
460 static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
462 static ssize_t xendev_show_devtype(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
467 static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
469 static ssize_t xendev_show_modalias(struct device *dev,
470 struct device_attribute *attr, char *buf)
472 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
474 static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
476 int xenbus_probe_node(struct xen_bus_type *bus,
477 const char *type,
478 const char *nodename)
480 char devname[XEN_BUS_ID_SIZE];
481 int err;
482 struct xenbus_device *xendev;
483 size_t stringlen;
484 char *tmpstring;
486 enum xenbus_state state = xenbus_read_driver_state(nodename);
488 if (state != XenbusStateInitialising) {
489 /* Device is not new, so ignore it. This can happen if a
490 device is going away after switching to Closed. */
491 return 0;
494 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
495 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
496 if (!xendev)
497 return -ENOMEM;
499 xendev->state = XenbusStateInitialising;
501 /* Copy the strings into the extra space. */
503 tmpstring = (char *)(xendev + 1);
504 strcpy(tmpstring, nodename);
505 xendev->nodename = tmpstring;
507 tmpstring += strlen(tmpstring) + 1;
508 strcpy(tmpstring, type);
509 xendev->devicetype = tmpstring;
510 init_completion(&xendev->down);
512 xendev->dev.bus = &bus->bus;
513 xendev->dev.release = xenbus_dev_release;
515 err = bus->get_bus_id(devname, xendev->nodename);
516 if (err)
517 goto fail;
519 dev_set_name(&xendev->dev, devname);
521 /* Register with generic device framework. */
522 err = device_register(&xendev->dev);
523 if (err)
524 goto fail;
526 err = device_create_file(&xendev->dev, &dev_attr_nodename);
527 if (err)
528 goto fail_unregister;
530 err = device_create_file(&xendev->dev, &dev_attr_devtype);
531 if (err)
532 goto fail_remove_nodename;
534 err = device_create_file(&xendev->dev, &dev_attr_modalias);
535 if (err)
536 goto fail_remove_devtype;
538 return 0;
539 fail_remove_devtype:
540 device_remove_file(&xendev->dev, &dev_attr_devtype);
541 fail_remove_nodename:
542 device_remove_file(&xendev->dev, &dev_attr_nodename);
543 fail_unregister:
544 device_unregister(&xendev->dev);
545 fail:
546 kfree(xendev);
547 return err;
550 /* device/<typename>/<name> */
551 static int xenbus_probe_frontend(const char *type, const char *name)
553 char *nodename;
554 int err;
556 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
557 xenbus_frontend.root, type, name);
558 if (!nodename)
559 return -ENOMEM;
561 DPRINTK("%s", nodename);
563 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
564 kfree(nodename);
565 return err;
568 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
570 int err = 0;
571 char **dir;
572 unsigned int dir_n = 0;
573 int i;
575 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
576 if (IS_ERR(dir))
577 return PTR_ERR(dir);
579 for (i = 0; i < dir_n; i++) {
580 err = bus->probe(type, dir[i]);
581 if (err)
582 break;
584 kfree(dir);
585 return err;
588 int xenbus_probe_devices(struct xen_bus_type *bus)
590 int err = 0;
591 char **dir;
592 unsigned int i, dir_n;
594 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
595 if (IS_ERR(dir))
596 return PTR_ERR(dir);
598 for (i = 0; i < dir_n; i++) {
599 err = xenbus_probe_device_type(bus, dir[i]);
600 if (err)
601 break;
603 kfree(dir);
604 return err;
607 static unsigned int char_count(const char *str, char c)
609 unsigned int i, ret = 0;
611 for (i = 0; str[i]; i++)
612 if (str[i] == c)
613 ret++;
614 return ret;
617 static int strsep_len(const char *str, char c, unsigned int len)
619 unsigned int i;
621 for (i = 0; str[i]; i++)
622 if (str[i] == c) {
623 if (len == 0)
624 return i;
625 len--;
627 return (len == 0) ? i : -ERANGE;
630 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
632 int exists, rootlen;
633 struct xenbus_device *dev;
634 char type[XEN_BUS_ID_SIZE];
635 const char *p, *root;
637 if (char_count(node, '/') < 2)
638 return;
640 exists = xenbus_exists(XBT_NIL, node, "");
641 if (!exists) {
642 xenbus_cleanup_devices(node, &bus->bus);
643 return;
646 /* backend/<type>/... or device/<type>/... */
647 p = strchr(node, '/') + 1;
648 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
649 type[XEN_BUS_ID_SIZE-1] = '\0';
651 rootlen = strsep_len(node, '/', bus->levels);
652 if (rootlen < 0)
653 return;
654 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
655 if (!root)
656 return;
658 dev = xenbus_device_find(root, &bus->bus);
659 if (!dev)
660 xenbus_probe_node(bus, type, root);
661 else
662 put_device(&dev->dev);
664 kfree(root);
666 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
668 static void frontend_changed(struct xenbus_watch *watch,
669 const char **vec, unsigned int len)
671 DPRINTK("");
673 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
676 /* We watch for devices appearing and vanishing. */
677 static struct xenbus_watch fe_watch = {
678 .node = "device",
679 .callback = frontend_changed,
682 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
684 int err = 0;
685 struct xenbus_driver *drv;
686 struct xenbus_device *xdev;
688 DPRINTK("");
690 if (dev->driver == NULL)
691 return 0;
692 drv = to_xenbus_driver(dev->driver);
693 xdev = container_of(dev, struct xenbus_device, dev);
694 if (drv->suspend)
695 err = drv->suspend(xdev, state);
696 if (err)
697 printk(KERN_WARNING
698 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
699 return 0;
702 static int xenbus_dev_resume(struct device *dev)
704 int err;
705 struct xenbus_driver *drv;
706 struct xenbus_device *xdev;
708 DPRINTK("");
710 if (dev->driver == NULL)
711 return 0;
713 drv = to_xenbus_driver(dev->driver);
714 xdev = container_of(dev, struct xenbus_device, dev);
716 err = talk_to_otherend(xdev);
717 if (err) {
718 printk(KERN_WARNING
719 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
720 dev_name(dev), err);
721 return err;
724 xdev->state = XenbusStateInitialising;
726 if (drv->resume) {
727 err = drv->resume(xdev);
728 if (err) {
729 printk(KERN_WARNING
730 "xenbus: resume %s failed: %i\n",
731 dev_name(dev), err);
732 return err;
736 err = watch_otherend(xdev);
737 if (err) {
738 printk(KERN_WARNING
739 "xenbus_probe: resume (watch_otherend) %s failed: "
740 "%d.\n", dev_name(dev), err);
741 return err;
744 return 0;
747 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
748 int xenstored_ready = 0;
751 int register_xenstore_notifier(struct notifier_block *nb)
753 int ret = 0;
755 if (xenstored_ready > 0)
756 ret = nb->notifier_call(nb, 0, NULL);
757 else
758 blocking_notifier_chain_register(&xenstore_chain, nb);
760 return ret;
762 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
764 void unregister_xenstore_notifier(struct notifier_block *nb)
766 blocking_notifier_chain_unregister(&xenstore_chain, nb);
768 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
770 void xenbus_probe(struct work_struct *unused)
772 BUG_ON((xenstored_ready <= 0));
774 /* Enumerate devices in xenstore and watch for changes. */
775 xenbus_probe_devices(&xenbus_frontend);
776 register_xenbus_watch(&fe_watch);
777 xenbus_backend_probe_and_watch();
779 /* Notify others that xenstore is up */
780 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
783 static int __init xenbus_probe_init(void)
785 int err = 0;
787 DPRINTK("");
789 err = -ENODEV;
790 if (!xen_domain())
791 goto out_error;
793 /* Register ourselves with the kernel bus subsystem */
794 err = bus_register(&xenbus_frontend.bus);
795 if (err)
796 goto out_error;
798 err = xenbus_backend_bus_register();
799 if (err)
800 goto out_unreg_front;
803 * Domain0 doesn't have a store_evtchn or store_mfn yet.
805 if (xen_initial_domain()) {
806 /* dom0 not yet supported */
807 } else {
808 xenstored_ready = 1;
809 xen_store_evtchn = xen_start_info->store_evtchn;
810 xen_store_mfn = xen_start_info->store_mfn;
812 xen_store_interface = mfn_to_virt(xen_store_mfn);
814 /* Initialize the interface to xenstore. */
815 err = xs_init();
816 if (err) {
817 printk(KERN_WARNING
818 "XENBUS: Error initializing xenstore comms: %i\n", err);
819 goto out_unreg_back;
822 if (!xen_initial_domain())
823 xenbus_probe(NULL);
825 #ifdef CONFIG_XEN_COMPAT_XENFS
827 * Create xenfs mountpoint in /proc for compatibility with
828 * utilities that expect to find "xenbus" under "/proc/xen".
830 proc_mkdir("xen", NULL);
831 #endif
833 return 0;
835 out_unreg_back:
836 xenbus_backend_bus_unregister();
838 out_unreg_front:
839 bus_unregister(&xenbus_frontend.bus);
841 out_error:
842 return err;
845 postcore_initcall(xenbus_probe_init);
847 MODULE_LICENSE("GPL");
849 static int is_device_connecting(struct device *dev, void *data)
851 struct xenbus_device *xendev = to_xenbus_device(dev);
852 struct device_driver *drv = data;
853 struct xenbus_driver *xendrv;
856 * A device with no driver will never connect. We care only about
857 * devices which should currently be in the process of connecting.
859 if (!dev->driver)
860 return 0;
862 /* Is this search limited to a particular driver? */
863 if (drv && (dev->driver != drv))
864 return 0;
866 xendrv = to_xenbus_driver(dev->driver);
867 return (xendev->state < XenbusStateConnected ||
868 (xendev->state == XenbusStateConnected &&
869 xendrv->is_ready && !xendrv->is_ready(xendev)));
872 static int exists_connecting_device(struct device_driver *drv)
874 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
875 is_device_connecting);
878 static int print_device_status(struct device *dev, void *data)
880 struct xenbus_device *xendev = to_xenbus_device(dev);
881 struct device_driver *drv = data;
883 /* Is this operation limited to a particular driver? */
884 if (drv && (dev->driver != drv))
885 return 0;
887 if (!dev->driver) {
888 /* Information only: is this too noisy? */
889 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
890 xendev->nodename);
891 } else if (xendev->state < XenbusStateConnected) {
892 enum xenbus_state rstate = XenbusStateUnknown;
893 if (xendev->otherend)
894 rstate = xenbus_read_driver_state(xendev->otherend);
895 printk(KERN_WARNING "XENBUS: Timeout connecting "
896 "to device: %s (local state %d, remote state %d)\n",
897 xendev->nodename, xendev->state, rstate);
900 return 0;
903 /* We only wait for device setup after most initcalls have run. */
904 static int ready_to_wait_for_devices;
907 * On a 5-minute timeout, wait for all devices currently configured. We need
908 * to do this to guarantee that the filesystems and / or network devices
909 * needed for boot are available, before we can allow the boot to proceed.
911 * This needs to be on a late_initcall, to happen after the frontend device
912 * drivers have been initialised, but before the root fs is mounted.
914 * A possible improvement here would be to have the tools add a per-device
915 * flag to the store entry, indicating whether it is needed at boot time.
916 * This would allow people who knew what they were doing to accelerate their
917 * boot slightly, but of course needs tools or manual intervention to set up
918 * those flags correctly.
920 static void wait_for_devices(struct xenbus_driver *xendrv)
922 unsigned long start = jiffies;
923 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
924 unsigned int seconds_waited = 0;
926 if (!ready_to_wait_for_devices || !xen_domain())
927 return;
929 while (exists_connecting_device(drv)) {
930 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
931 if (!seconds_waited)
932 printk(KERN_WARNING "XENBUS: Waiting for "
933 "devices to initialise: ");
934 seconds_waited += 5;
935 printk("%us...", 300 - seconds_waited);
936 if (seconds_waited == 300)
937 break;
940 schedule_timeout_interruptible(HZ/10);
943 if (seconds_waited)
944 printk("\n");
946 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
947 print_device_status);
950 #ifndef MODULE
951 static int __init boot_wait_for_devices(void)
953 ready_to_wait_for_devices = 1;
954 wait_for_devices(NULL);
955 return 0;
958 late_initcall(boot_wait_for_devices);
959 #endif