x86: add cache descriptors for Intel Core i7
[linux-2.6/mini2440.git] / drivers / xen / xenbus / xenbus_probe.c
blob773d1cf2328334b9c62cc52fdf90c7107df02778
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 /* If something in array of ids matches this device, return it. */
75 static const struct xenbus_device_id *
76 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
78 for (; *arr->devicetype != '\0'; arr++) {
79 if (!strcmp(arr->devicetype, dev->devicetype))
80 return arr;
82 return NULL;
85 int xenbus_match(struct device *_dev, struct device_driver *_drv)
87 struct xenbus_driver *drv = to_xenbus_driver(_drv);
89 if (!drv->ids)
90 return 0;
92 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
95 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
97 struct xenbus_device *dev = to_xenbus_device(_dev);
99 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
100 return -ENOMEM;
102 return 0;
105 /* device/<type>/<id> => <type>-<id> */
106 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
108 nodename = strchr(nodename, '/');
109 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
110 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
111 return -EINVAL;
114 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
115 if (!strchr(bus_id, '/')) {
116 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
117 return -EINVAL;
119 *strchr(bus_id, '/') = '-';
120 return 0;
124 static void free_otherend_details(struct xenbus_device *dev)
126 kfree(dev->otherend);
127 dev->otherend = NULL;
131 static void free_otherend_watch(struct xenbus_device *dev)
133 if (dev->otherend_watch.node) {
134 unregister_xenbus_watch(&dev->otherend_watch);
135 kfree(dev->otherend_watch.node);
136 dev->otherend_watch.node = NULL;
141 int read_otherend_details(struct xenbus_device *xendev,
142 char *id_node, char *path_node)
144 int err = xenbus_gather(XBT_NIL, xendev->nodename,
145 id_node, "%i", &xendev->otherend_id,
146 path_node, NULL, &xendev->otherend,
147 NULL);
148 if (err) {
149 xenbus_dev_fatal(xendev, err,
150 "reading other end details from %s",
151 xendev->nodename);
152 return err;
154 if (strlen(xendev->otherend) == 0 ||
155 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
156 xenbus_dev_fatal(xendev, -ENOENT,
157 "unable to read other end from %s. "
158 "missing or inaccessible.",
159 xendev->nodename);
160 free_otherend_details(xendev);
161 return -ENOENT;
164 return 0;
168 static int read_backend_details(struct xenbus_device *xendev)
170 return read_otherend_details(xendev, "backend-id", "backend");
173 static struct device_attribute xenbus_dev_attrs[] = {
174 __ATTR_NULL
177 /* Bus type for frontend drivers. */
178 static struct xen_bus_type xenbus_frontend = {
179 .root = "device",
180 .levels = 2, /* device/type/<id> */
181 .get_bus_id = frontend_bus_id,
182 .probe = xenbus_probe_frontend,
183 .bus = {
184 .name = "xen",
185 .match = xenbus_match,
186 .uevent = xenbus_uevent,
187 .probe = xenbus_dev_probe,
188 .remove = xenbus_dev_remove,
189 .shutdown = xenbus_dev_shutdown,
190 .dev_attrs = xenbus_dev_attrs,
194 static void otherend_changed(struct xenbus_watch *watch,
195 const char **vec, unsigned int len)
197 struct xenbus_device *dev =
198 container_of(watch, struct xenbus_device, otherend_watch);
199 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
200 enum xenbus_state state;
202 /* Protect us against watches firing on old details when the otherend
203 details change, say immediately after a resume. */
204 if (!dev->otherend ||
205 strncmp(dev->otherend, vec[XS_WATCH_PATH],
206 strlen(dev->otherend))) {
207 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
208 vec[XS_WATCH_PATH]);
209 return;
212 state = xenbus_read_driver_state(dev->otherend);
214 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
215 state, xenbus_strstate(state), dev->otherend_watch.node,
216 vec[XS_WATCH_PATH]);
219 * Ignore xenbus transitions during shutdown. This prevents us doing
220 * work that can fail e.g., when the rootfs is gone.
222 if (system_state > SYSTEM_RUNNING) {
223 struct xen_bus_type *bus = bus;
224 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
225 /* If we're frontend, drive the state machine to Closed. */
226 /* This should cause the backend to release our resources. */
227 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
228 xenbus_frontend_closed(dev);
229 return;
232 if (drv->otherend_changed)
233 drv->otherend_changed(dev, state);
237 static int talk_to_otherend(struct xenbus_device *dev)
239 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
241 free_otherend_watch(dev);
242 free_otherend_details(dev);
244 return drv->read_otherend_details(dev);
248 static int watch_otherend(struct xenbus_device *dev)
250 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
251 "%s/%s", dev->otherend, "state");
255 int xenbus_dev_probe(struct device *_dev)
257 struct xenbus_device *dev = to_xenbus_device(_dev);
258 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
259 const struct xenbus_device_id *id;
260 int err;
262 DPRINTK("%s", dev->nodename);
264 if (!drv->probe) {
265 err = -ENODEV;
266 goto fail;
269 id = match_device(drv->ids, dev);
270 if (!id) {
271 err = -ENODEV;
272 goto fail;
275 err = talk_to_otherend(dev);
276 if (err) {
277 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
278 dev->nodename);
279 return err;
282 err = drv->probe(dev, id);
283 if (err)
284 goto fail;
286 err = watch_otherend(dev);
287 if (err) {
288 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
289 dev->nodename);
290 return err;
293 return 0;
294 fail:
295 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
296 xenbus_switch_state(dev, XenbusStateClosed);
297 return -ENODEV;
300 int xenbus_dev_remove(struct device *_dev)
302 struct xenbus_device *dev = to_xenbus_device(_dev);
303 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
305 DPRINTK("%s", dev->nodename);
307 free_otherend_watch(dev);
308 free_otherend_details(dev);
310 if (drv->remove)
311 drv->remove(dev);
313 xenbus_switch_state(dev, XenbusStateClosed);
314 return 0;
317 static void xenbus_dev_shutdown(struct device *_dev)
319 struct xenbus_device *dev = to_xenbus_device(_dev);
320 unsigned long timeout = 5*HZ;
322 DPRINTK("%s", dev->nodename);
324 get_device(&dev->dev);
325 if (dev->state != XenbusStateConnected) {
326 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
327 dev->nodename, xenbus_strstate(dev->state));
328 goto out;
330 xenbus_switch_state(dev, XenbusStateClosing);
331 timeout = wait_for_completion_timeout(&dev->down, timeout);
332 if (!timeout)
333 printk(KERN_INFO "%s: %s timeout closing device\n",
334 __func__, dev->nodename);
335 out:
336 put_device(&dev->dev);
339 int xenbus_register_driver_common(struct xenbus_driver *drv,
340 struct xen_bus_type *bus,
341 struct module *owner,
342 const char *mod_name)
344 drv->driver.name = drv->name;
345 drv->driver.bus = &bus->bus;
346 drv->driver.owner = owner;
347 drv->driver.mod_name = mod_name;
349 return driver_register(&drv->driver);
352 int __xenbus_register_frontend(struct xenbus_driver *drv,
353 struct module *owner, const char *mod_name)
355 int ret;
357 drv->read_otherend_details = read_backend_details;
359 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
360 owner, mod_name);
361 if (ret)
362 return ret;
364 /* If this driver is loaded as a module wait for devices to attach. */
365 wait_for_devices(drv);
367 return 0;
369 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
371 void xenbus_unregister_driver(struct xenbus_driver *drv)
373 driver_unregister(&drv->driver);
375 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
377 struct xb_find_info
379 struct xenbus_device *dev;
380 const char *nodename;
383 static int cmp_dev(struct device *dev, void *data)
385 struct xenbus_device *xendev = to_xenbus_device(dev);
386 struct xb_find_info *info = data;
388 if (!strcmp(xendev->nodename, info->nodename)) {
389 info->dev = xendev;
390 get_device(dev);
391 return 1;
393 return 0;
396 struct xenbus_device *xenbus_device_find(const char *nodename,
397 struct bus_type *bus)
399 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
401 bus_for_each_dev(bus, NULL, &info, cmp_dev);
402 return info.dev;
405 static int cleanup_dev(struct device *dev, void *data)
407 struct xenbus_device *xendev = to_xenbus_device(dev);
408 struct xb_find_info *info = data;
409 int len = strlen(info->nodename);
411 DPRINTK("%s", info->nodename);
413 /* Match the info->nodename path, or any subdirectory of that path. */
414 if (strncmp(xendev->nodename, info->nodename, len))
415 return 0;
417 /* If the node name is longer, ensure it really is a subdirectory. */
418 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
419 return 0;
421 info->dev = xendev;
422 get_device(dev);
423 return 1;
426 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
428 struct xb_find_info info = { .nodename = path };
430 do {
431 info.dev = NULL;
432 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
433 if (info.dev) {
434 device_unregister(&info.dev->dev);
435 put_device(&info.dev->dev);
437 } while (info.dev);
440 static void xenbus_dev_release(struct device *dev)
442 if (dev)
443 kfree(to_xenbus_device(dev));
446 static ssize_t xendev_show_nodename(struct device *dev,
447 struct device_attribute *attr, char *buf)
449 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
451 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
453 static ssize_t xendev_show_devtype(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
458 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
460 static ssize_t xendev_show_modalias(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
465 DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
467 int xenbus_probe_node(struct xen_bus_type *bus,
468 const char *type,
469 const char *nodename)
471 char devname[XEN_BUS_ID_SIZE];
472 int err;
473 struct xenbus_device *xendev;
474 size_t stringlen;
475 char *tmpstring;
477 enum xenbus_state state = xenbus_read_driver_state(nodename);
479 if (state != XenbusStateInitialising) {
480 /* Device is not new, so ignore it. This can happen if a
481 device is going away after switching to Closed. */
482 return 0;
485 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
486 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
487 if (!xendev)
488 return -ENOMEM;
490 xendev->state = XenbusStateInitialising;
492 /* Copy the strings into the extra space. */
494 tmpstring = (char *)(xendev + 1);
495 strcpy(tmpstring, nodename);
496 xendev->nodename = tmpstring;
498 tmpstring += strlen(tmpstring) + 1;
499 strcpy(tmpstring, type);
500 xendev->devicetype = tmpstring;
501 init_completion(&xendev->down);
503 xendev->dev.bus = &bus->bus;
504 xendev->dev.release = xenbus_dev_release;
506 err = bus->get_bus_id(devname, xendev->nodename);
507 if (err)
508 goto fail;
510 dev_set_name(&xendev->dev, devname);
512 /* Register with generic device framework. */
513 err = device_register(&xendev->dev);
514 if (err)
515 goto fail;
517 err = device_create_file(&xendev->dev, &dev_attr_nodename);
518 if (err)
519 goto fail_unregister;
521 err = device_create_file(&xendev->dev, &dev_attr_devtype);
522 if (err)
523 goto fail_remove_nodename;
525 err = device_create_file(&xendev->dev, &dev_attr_modalias);
526 if (err)
527 goto fail_remove_devtype;
529 return 0;
530 fail_remove_devtype:
531 device_remove_file(&xendev->dev, &dev_attr_devtype);
532 fail_remove_nodename:
533 device_remove_file(&xendev->dev, &dev_attr_nodename);
534 fail_unregister:
535 device_unregister(&xendev->dev);
536 fail:
537 kfree(xendev);
538 return err;
541 /* device/<typename>/<name> */
542 static int xenbus_probe_frontend(const char *type, const char *name)
544 char *nodename;
545 int err;
547 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
548 xenbus_frontend.root, type, name);
549 if (!nodename)
550 return -ENOMEM;
552 DPRINTK("%s", nodename);
554 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
555 kfree(nodename);
556 return err;
559 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
561 int err = 0;
562 char **dir;
563 unsigned int dir_n = 0;
564 int i;
566 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
567 if (IS_ERR(dir))
568 return PTR_ERR(dir);
570 for (i = 0; i < dir_n; i++) {
571 err = bus->probe(type, dir[i]);
572 if (err)
573 break;
575 kfree(dir);
576 return err;
579 int xenbus_probe_devices(struct xen_bus_type *bus)
581 int err = 0;
582 char **dir;
583 unsigned int i, dir_n;
585 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
586 if (IS_ERR(dir))
587 return PTR_ERR(dir);
589 for (i = 0; i < dir_n; i++) {
590 err = xenbus_probe_device_type(bus, dir[i]);
591 if (err)
592 break;
594 kfree(dir);
595 return err;
598 static unsigned int char_count(const char *str, char c)
600 unsigned int i, ret = 0;
602 for (i = 0; str[i]; i++)
603 if (str[i] == c)
604 ret++;
605 return ret;
608 static int strsep_len(const char *str, char c, unsigned int len)
610 unsigned int i;
612 for (i = 0; str[i]; i++)
613 if (str[i] == c) {
614 if (len == 0)
615 return i;
616 len--;
618 return (len == 0) ? i : -ERANGE;
621 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
623 int exists, rootlen;
624 struct xenbus_device *dev;
625 char type[XEN_BUS_ID_SIZE];
626 const char *p, *root;
628 if (char_count(node, '/') < 2)
629 return;
631 exists = xenbus_exists(XBT_NIL, node, "");
632 if (!exists) {
633 xenbus_cleanup_devices(node, &bus->bus);
634 return;
637 /* backend/<type>/... or device/<type>/... */
638 p = strchr(node, '/') + 1;
639 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
640 type[XEN_BUS_ID_SIZE-1] = '\0';
642 rootlen = strsep_len(node, '/', bus->levels);
643 if (rootlen < 0)
644 return;
645 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
646 if (!root)
647 return;
649 dev = xenbus_device_find(root, &bus->bus);
650 if (!dev)
651 xenbus_probe_node(bus, type, root);
652 else
653 put_device(&dev->dev);
655 kfree(root);
658 static void frontend_changed(struct xenbus_watch *watch,
659 const char **vec, unsigned int len)
661 DPRINTK("");
663 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
666 /* We watch for devices appearing and vanishing. */
667 static struct xenbus_watch fe_watch = {
668 .node = "device",
669 .callback = frontend_changed,
672 static int suspend_dev(struct device *dev, void *data)
674 int err = 0;
675 struct xenbus_driver *drv;
676 struct xenbus_device *xdev;
678 DPRINTK("");
680 if (dev->driver == NULL)
681 return 0;
682 drv = to_xenbus_driver(dev->driver);
683 xdev = container_of(dev, struct xenbus_device, dev);
684 if (drv->suspend)
685 err = drv->suspend(xdev);
686 if (err)
687 printk(KERN_WARNING
688 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
689 return 0;
692 static int suspend_cancel_dev(struct device *dev, void *data)
694 int err = 0;
695 struct xenbus_driver *drv;
696 struct xenbus_device *xdev;
698 DPRINTK("");
700 if (dev->driver == NULL)
701 return 0;
702 drv = to_xenbus_driver(dev->driver);
703 xdev = container_of(dev, struct xenbus_device, dev);
704 if (drv->suspend_cancel)
705 err = drv->suspend_cancel(xdev);
706 if (err)
707 printk(KERN_WARNING
708 "xenbus: suspend_cancel %s failed: %i\n",
709 dev_name(dev), err);
710 return 0;
713 static int resume_dev(struct device *dev, void *data)
715 int err;
716 struct xenbus_driver *drv;
717 struct xenbus_device *xdev;
719 DPRINTK("");
721 if (dev->driver == NULL)
722 return 0;
724 drv = to_xenbus_driver(dev->driver);
725 xdev = container_of(dev, struct xenbus_device, dev);
727 err = talk_to_otherend(xdev);
728 if (err) {
729 printk(KERN_WARNING
730 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
731 dev_name(dev), err);
732 return err;
735 xdev->state = XenbusStateInitialising;
737 if (drv->resume) {
738 err = drv->resume(xdev);
739 if (err) {
740 printk(KERN_WARNING
741 "xenbus: resume %s failed: %i\n",
742 dev_name(dev), err);
743 return err;
747 err = watch_otherend(xdev);
748 if (err) {
749 printk(KERN_WARNING
750 "xenbus_probe: resume (watch_otherend) %s failed: "
751 "%d.\n", dev_name(dev), err);
752 return err;
755 return 0;
758 void xenbus_suspend(void)
760 DPRINTK("");
762 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
763 xenbus_backend_suspend(suspend_dev);
764 xs_suspend();
766 EXPORT_SYMBOL_GPL(xenbus_suspend);
768 void xenbus_resume(void)
770 xb_init_comms();
771 xs_resume();
772 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
773 xenbus_backend_resume(resume_dev);
775 EXPORT_SYMBOL_GPL(xenbus_resume);
777 void xenbus_suspend_cancel(void)
779 xs_suspend_cancel();
780 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
781 xenbus_backend_resume(suspend_cancel_dev);
783 EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
785 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
786 int xenstored_ready = 0;
789 int register_xenstore_notifier(struct notifier_block *nb)
791 int ret = 0;
793 if (xenstored_ready > 0)
794 ret = nb->notifier_call(nb, 0, NULL);
795 else
796 blocking_notifier_chain_register(&xenstore_chain, nb);
798 return ret;
800 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
802 void unregister_xenstore_notifier(struct notifier_block *nb)
804 blocking_notifier_chain_unregister(&xenstore_chain, nb);
806 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
808 void xenbus_probe(struct work_struct *unused)
810 BUG_ON((xenstored_ready <= 0));
812 /* Enumerate devices in xenstore and watch for changes. */
813 xenbus_probe_devices(&xenbus_frontend);
814 register_xenbus_watch(&fe_watch);
815 xenbus_backend_probe_and_watch();
817 /* Notify others that xenstore is up */
818 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
821 static int __init xenbus_probe_init(void)
823 int err = 0;
825 DPRINTK("");
827 err = -ENODEV;
828 if (!xen_domain())
829 goto out_error;
831 /* Register ourselves with the kernel bus subsystem */
832 err = bus_register(&xenbus_frontend.bus);
833 if (err)
834 goto out_error;
836 err = xenbus_backend_bus_register();
837 if (err)
838 goto out_unreg_front;
841 * Domain0 doesn't have a store_evtchn or store_mfn yet.
843 if (xen_initial_domain()) {
844 /* dom0 not yet supported */
845 } else {
846 xenstored_ready = 1;
847 xen_store_evtchn = xen_start_info->store_evtchn;
848 xen_store_mfn = xen_start_info->store_mfn;
850 xen_store_interface = mfn_to_virt(xen_store_mfn);
852 /* Initialize the interface to xenstore. */
853 err = xs_init();
854 if (err) {
855 printk(KERN_WARNING
856 "XENBUS: Error initializing xenstore comms: %i\n", err);
857 goto out_unreg_back;
860 if (!xen_initial_domain())
861 xenbus_probe(NULL);
863 #ifdef CONFIG_XEN_COMPAT_XENFS
865 * Create xenfs mountpoint in /proc for compatibility with
866 * utilities that expect to find "xenbus" under "/proc/xen".
868 proc_mkdir("xen", NULL);
869 #endif
871 return 0;
873 out_unreg_back:
874 xenbus_backend_bus_unregister();
876 out_unreg_front:
877 bus_unregister(&xenbus_frontend.bus);
879 out_error:
880 return err;
883 postcore_initcall(xenbus_probe_init);
885 MODULE_LICENSE("GPL");
887 static int is_disconnected_device(struct device *dev, void *data)
889 struct xenbus_device *xendev = to_xenbus_device(dev);
890 struct device_driver *drv = data;
891 struct xenbus_driver *xendrv;
894 * A device with no driver will never connect. We care only about
895 * devices which should currently be in the process of connecting.
897 if (!dev->driver)
898 return 0;
900 /* Is this search limited to a particular driver? */
901 if (drv && (dev->driver != drv))
902 return 0;
904 xendrv = to_xenbus_driver(dev->driver);
905 return (xendev->state != XenbusStateConnected ||
906 (xendrv->is_ready && !xendrv->is_ready(xendev)));
909 static int exists_disconnected_device(struct device_driver *drv)
911 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
912 is_disconnected_device);
915 static int print_device_status(struct device *dev, void *data)
917 struct xenbus_device *xendev = to_xenbus_device(dev);
918 struct device_driver *drv = data;
920 /* Is this operation limited to a particular driver? */
921 if (drv && (dev->driver != drv))
922 return 0;
924 if (!dev->driver) {
925 /* Information only: is this too noisy? */
926 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
927 xendev->nodename);
928 } else if (xendev->state != XenbusStateConnected) {
929 printk(KERN_WARNING "XENBUS: Timeout connecting "
930 "to device: %s (state %d)\n",
931 xendev->nodename, xendev->state);
934 return 0;
937 /* We only wait for device setup after most initcalls have run. */
938 static int ready_to_wait_for_devices;
941 * On a 10 second timeout, wait for all devices currently configured. We need
942 * to do this to guarantee that the filesystems and / or network devices
943 * needed for boot are available, before we can allow the boot to proceed.
945 * This needs to be on a late_initcall, to happen after the frontend device
946 * drivers have been initialised, but before the root fs is mounted.
948 * A possible improvement here would be to have the tools add a per-device
949 * flag to the store entry, indicating whether it is needed at boot time.
950 * This would allow people who knew what they were doing to accelerate their
951 * boot slightly, but of course needs tools or manual intervention to set up
952 * those flags correctly.
954 static void wait_for_devices(struct xenbus_driver *xendrv)
956 unsigned long timeout = jiffies + 10*HZ;
957 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
959 if (!ready_to_wait_for_devices || !xen_domain())
960 return;
962 while (exists_disconnected_device(drv)) {
963 if (time_after(jiffies, timeout))
964 break;
965 schedule_timeout_interruptible(HZ/10);
968 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
969 print_device_status);
972 #ifndef MODULE
973 static int __init boot_wait_for_devices(void)
975 ready_to_wait_for_devices = 1;
976 wait_for_devices(NULL);
977 return 0;
980 late_initcall(boot_wait_for_devices);
981 #endif