1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __FUNCTION__, __LINE__, ##args)
37 #include <linux/kernel.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/ctype.h>
41 #include <linux/fcntl.h>
43 #include <linux/notifier.h>
44 #include <linux/kthread.h>
48 #include <asm/maddr.h>
49 #include <asm/pgtable.h>
50 #include <asm/hypervisor.h>
51 #include <xen/xenbus.h>
52 #include <xen/xen_proc.h>
53 #include <xen/evtchn.h>
54 #include <xen/features.h>
57 #include "xenbus_comms.h"
60 struct xenstore_domain_interface *xen_store_interface;
61 static unsigned long xen_store_mfn;
63 extern struct mutex xenwatch_mutex;
65 static BLOCKING_NOTIFIER_HEAD(xenstore_notifier_list);
67 static void wait_for_devices(struct xenbus_driver *xendrv);
69 static int xenbus_probe_frontend(const char *type, const char *name);
70 static int xenbus_uevent_backend(struct device *dev, char **envp,
71 int num_envp, char *buffer, int buffer_size);
72 static int xenbus_probe_backend(const char *type, const char *domid);
74 static int xenbus_dev_probe(struct device *_dev);
75 static int xenbus_dev_remove(struct device *_dev);
76 static void xenbus_dev_shutdown(struct device *_dev);
78 /* If something in array of ids matches this device, return it. */
79 static const struct xenbus_device_id *
80 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
82 for (; *arr->devicetype != '\0'; arr++) {
83 if (!strcmp(arr->devicetype, dev->devicetype))
89 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
91 struct xenbus_driver *drv = to_xenbus_driver(_drv);
96 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
103 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
104 int (*probe)(const char *type, const char *dir);
110 /* device/<type>/<id> => <type>-<id> */
111 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
113 nodename = strchr(nodename, '/');
114 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
115 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
119 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
120 if (!strchr(bus_id, '/')) {
121 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
124 *strchr(bus_id, '/') = '-';
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 static 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,
154 xenbus_dev_fatal(xendev, err,
155 "reading other end details from %s",
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.",
165 free_otherend_details(xendev);
173 static int read_backend_details(struct xenbus_device *xendev)
175 return read_otherend_details(xendev, "backend-id", "backend");
179 static int read_frontend_details(struct xenbus_device *xendev)
181 return read_otherend_details(xendev, "frontend-id", "frontend");
185 /* Bus type for frontend drivers. */
186 static struct xen_bus_type xenbus_frontend = {
188 .levels = 2, /* device/type/<id> */
189 .get_bus_id = frontend_bus_id,
190 .probe = xenbus_probe_frontend,
193 .match = xenbus_match,
194 .probe = xenbus_dev_probe,
195 .remove = xenbus_dev_remove,
196 .shutdown = xenbus_dev_shutdown,
203 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
204 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
207 const char *devid, *type, *frontend;
208 unsigned int typelen;
210 type = strchr(nodename, '/');
214 typelen = strcspn(type, "/");
215 if (!typelen || type[typelen] != '/')
218 devid = strrchr(nodename, '/') + 1;
220 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
221 "frontend", NULL, &frontend,
225 if (strlen(frontend) == 0)
227 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
235 if (snprintf(bus_id, BUS_ID_SIZE,
236 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
241 static struct xen_bus_type xenbus_backend = {
243 .levels = 3, /* backend/type/<frontend>/<id> */
244 .get_bus_id = backend_bus_id,
245 .probe = xenbus_probe_backend,
247 .name = "xen-backend",
248 .match = xenbus_match,
249 .probe = xenbus_dev_probe,
250 .remove = xenbus_dev_remove,
251 // .shutdown = xenbus_dev_shutdown,
252 .uevent = xenbus_uevent_backend,
255 .bus_id = "xen-backend",
259 static int xenbus_uevent_backend(struct device *dev, char **envp,
260 int num_envp, char *buffer, int buffer_size)
262 struct xenbus_device *xdev;
263 struct xenbus_driver *drv;
272 xdev = to_xenbus_device(dev);
276 /* stuff we want to pass to /sbin/hotplug */
277 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
278 "XENBUS_TYPE=%s", xdev->devicetype);
280 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
281 "XENBUS_PATH=%s", xdev->nodename);
283 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
284 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
286 /* terminate, set to next free slot, shrink available space */
290 buffer = &buffer[length];
291 buffer_size -= length;
294 drv = to_xenbus_driver(dev->driver);
295 if (drv && drv->uevent)
296 return drv->uevent(xdev, envp, num_envp, buffer,
303 static void otherend_changed(struct xenbus_watch *watch,
304 const char **vec, unsigned int len)
306 struct xenbus_device *dev =
307 container_of(watch, struct xenbus_device, otherend_watch);
308 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
309 enum xenbus_state state;
311 /* Protect us against watches firing on old details when the otherend
312 details change, say immediately after a resume. */
313 if (!dev->otherend ||
314 strncmp(dev->otherend, vec[XS_WATCH_PATH],
315 strlen(dev->otherend))) {
316 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
320 state = xenbus_read_driver_state(dev->otherend);
322 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
323 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
326 * Ignore xenbus transitions during shutdown. This prevents us doing
327 * work that can fail e.g., when the rootfs is gone.
329 if (system_state > SYSTEM_RUNNING) {
330 struct xen_bus_type *bus = bus;
331 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
332 /* If we're frontend, drive the state machine to Closed. */
333 /* This should cause the backend to release our resources. */
334 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
335 xenbus_frontend_closed(dev);
339 if (drv->otherend_changed)
340 drv->otherend_changed(dev, state);
344 static int talk_to_otherend(struct xenbus_device *dev)
346 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
348 free_otherend_watch(dev);
349 free_otherend_details(dev);
351 return drv->read_otherend_details(dev);
355 static int watch_otherend(struct xenbus_device *dev)
357 return xenbus_watch_path2(dev, dev->otherend, "state",
358 &dev->otherend_watch, otherend_changed);
362 static int xenbus_dev_probe(struct device *_dev)
364 struct xenbus_device *dev = to_xenbus_device(_dev);
365 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
366 const struct xenbus_device_id *id;
369 DPRINTK("%s", dev->nodename);
376 id = match_device(drv->ids, dev);
382 err = talk_to_otherend(dev);
385 "xenbus_probe: talk_to_otherend on %s failed.\n",
390 err = drv->probe(dev, id);
394 err = watch_otherend(dev);
397 "xenbus_probe: watch_otherend on %s failed.\n",
404 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
405 xenbus_switch_state(dev, XenbusStateClosed);
409 static int xenbus_dev_remove(struct device *_dev)
411 struct xenbus_device *dev = to_xenbus_device(_dev);
412 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
414 DPRINTK("%s", dev->nodename);
416 free_otherend_watch(dev);
417 free_otherend_details(dev);
422 xenbus_switch_state(dev, XenbusStateClosed);
426 static void xenbus_dev_shutdown(struct device *_dev)
428 struct xenbus_device *dev = to_xenbus_device(_dev);
429 unsigned long timeout = 5*HZ;
431 DPRINTK("%s", dev->nodename);
433 get_device(&dev->dev);
434 if (dev->state != XenbusStateConnected) {
435 printk("%s: %s: %s != Connected, skipping\n", __FUNCTION__,
436 dev->nodename, xenbus_strstate(dev->state));
439 xenbus_switch_state(dev, XenbusStateClosing);
440 timeout = wait_for_completion_timeout(&dev->down, timeout);
442 printk("%s: %s timeout closing device\n", __FUNCTION__, dev->nodename);
444 put_device(&dev->dev);
447 static int xenbus_register_driver_common(struct xenbus_driver *drv,
448 struct xen_bus_type *bus)
452 drv->driver.name = drv->name;
453 drv->driver.bus = &bus->bus;
454 drv->driver.owner = drv->owner;
456 mutex_lock(&xenwatch_mutex);
457 ret = driver_register(&drv->driver);
458 mutex_unlock(&xenwatch_mutex);
462 int xenbus_register_frontend(struct xenbus_driver *drv)
466 drv->read_otherend_details = read_backend_details;
468 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
472 /* If this driver is loaded as a module wait for devices to attach. */
473 wait_for_devices(drv);
477 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
479 int xenbus_register_backend(struct xenbus_driver *drv)
481 drv->read_otherend_details = read_frontend_details;
483 return xenbus_register_driver_common(drv, &xenbus_backend);
485 EXPORT_SYMBOL_GPL(xenbus_register_backend);
487 void xenbus_unregister_driver(struct xenbus_driver *drv)
489 driver_unregister(&drv->driver);
491 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
495 struct xenbus_device *dev;
496 const char *nodename;
499 static int cmp_dev(struct device *dev, void *data)
501 struct xenbus_device *xendev = to_xenbus_device(dev);
502 struct xb_find_info *info = data;
504 if (!strcmp(xendev->nodename, info->nodename)) {
512 struct xenbus_device *xenbus_device_find(const char *nodename,
513 struct bus_type *bus)
515 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
517 bus_for_each_dev(bus, NULL, &info, cmp_dev);
521 static int cleanup_dev(struct device *dev, void *data)
523 struct xenbus_device *xendev = to_xenbus_device(dev);
524 struct xb_find_info *info = data;
525 int len = strlen(info->nodename);
527 DPRINTK("%s", info->nodename);
529 /* Match the info->nodename path, or any subdirectory of that path. */
530 if (strncmp(xendev->nodename, info->nodename, len))
533 /* If the node name is longer, ensure it really is a subdirectory. */
534 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
542 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
544 struct xb_find_info info = { .nodename = path };
548 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
550 device_unregister(&info.dev->dev);
551 put_device(&info.dev->dev);
556 static void xenbus_dev_release(struct device *dev)
559 kfree(to_xenbus_device(dev));
562 static ssize_t xendev_show_nodename(struct device *dev,
563 struct device_attribute *attr, char *buf)
565 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
567 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
569 static ssize_t xendev_show_devtype(struct device *dev,
570 struct device_attribute *attr, char *buf)
572 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
574 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
577 static int xenbus_probe_node(struct xen_bus_type *bus,
579 const char *nodename)
582 struct xenbus_device *xendev;
586 enum xenbus_state state = xenbus_read_driver_state(nodename);
588 if (state != XenbusStateInitialising) {
589 /* Device is not new, so ignore it. This can happen if a
590 device is going away after switching to Closed. */
594 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
595 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
599 /* Copy the strings into the extra space. */
601 tmpstring = (char *)(xendev + 1);
602 strcpy(tmpstring, nodename);
603 xendev->nodename = tmpstring;
605 tmpstring += strlen(tmpstring) + 1;
606 strcpy(tmpstring, type);
607 xendev->devicetype = tmpstring;
608 init_completion(&xendev->down);
610 xendev->dev.parent = &bus->dev;
611 xendev->dev.bus = &bus->bus;
612 xendev->dev.release = xenbus_dev_release;
614 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
618 /* Register with generic device framework. */
619 err = device_register(&xendev->dev);
623 device_create_file(&xendev->dev, &dev_attr_nodename);
624 device_create_file(&xendev->dev, &dev_attr_devtype);
632 /* device/<typename>/<name> */
633 static int xenbus_probe_frontend(const char *type, const char *name)
638 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name);
642 DPRINTK("%s", nodename);
644 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
649 /* backend/<typename>/<frontend-uuid>/<name> */
650 static int xenbus_probe_backend_unit(const char *dir,
657 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
661 DPRINTK("%s\n", nodename);
663 err = xenbus_probe_node(&xenbus_backend, type, nodename);
668 /* backend/<typename>/<frontend-domid> */
669 static int xenbus_probe_backend(const char *type, const char *domid)
674 unsigned int i, dir_n = 0;
678 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_backend.root, type, domid);
682 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
688 for (i = 0; i < dir_n; i++) {
689 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
698 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
702 unsigned int dir_n = 0;
705 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
709 for (i = 0; i < dir_n; i++) {
710 err = bus->probe(type, dir[i]);
718 static int xenbus_probe_devices(struct xen_bus_type *bus)
722 unsigned int i, dir_n;
724 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
728 for (i = 0; i < dir_n; i++) {
729 err = xenbus_probe_device_type(bus, dir[i]);
737 static unsigned int char_count(const char *str, char c)
739 unsigned int i, ret = 0;
741 for (i = 0; str[i]; i++)
747 static int strsep_len(const char *str, char c, unsigned int len)
751 for (i = 0; str[i]; i++)
757 return (len == 0) ? i : -ERANGE;
760 static void dev_changed(const char *node, struct xen_bus_type *bus)
763 struct xenbus_device *dev;
764 char type[BUS_ID_SIZE];
765 const char *p, *root;
767 if (char_count(node, '/') < 2)
770 exists = xenbus_exists(XBT_NIL, node, "");
772 xenbus_cleanup_devices(node, &bus->bus);
776 /* backend/<type>/... or device/<type>/... */
777 p = strchr(node, '/') + 1;
778 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
779 type[BUS_ID_SIZE-1] = '\0';
781 rootlen = strsep_len(node, '/', bus->levels);
784 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
788 dev = xenbus_device_find(root, &bus->bus);
790 xenbus_probe_node(bus, type, root);
792 put_device(&dev->dev);
797 static void frontend_changed(struct xenbus_watch *watch,
798 const char **vec, unsigned int len)
802 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
805 static void backend_changed(struct xenbus_watch *watch,
806 const char **vec, unsigned int len)
810 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
813 /* We watch for devices appearing and vanishing. */
814 static struct xenbus_watch fe_watch = {
816 .callback = frontend_changed,
819 static struct xenbus_watch be_watch = {
821 .callback = backend_changed,
824 static int suspend_dev(struct device *dev, void *data)
827 struct xenbus_driver *drv;
828 struct xenbus_device *xdev;
832 if (dev->driver == NULL)
834 drv = to_xenbus_driver(dev->driver);
835 xdev = container_of(dev, struct xenbus_device, dev);
837 err = drv->suspend(xdev);
840 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
844 static int resume_dev(struct device *dev, void *data)
847 struct xenbus_driver *drv;
848 struct xenbus_device *xdev;
852 if (dev->driver == NULL)
855 drv = to_xenbus_driver(dev->driver);
856 xdev = container_of(dev, struct xenbus_device, dev);
858 err = talk_to_otherend(xdev);
861 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
866 xdev->state = XenbusStateInitialising;
869 err = drv->resume(xdev);
872 "xenbus: resume %s failed: %i\n",
878 err = watch_otherend(xdev);
881 "xenbus_probe: resume (watch_otherend) %s failed: "
882 "%d.\n", dev->bus_id, err);
889 void xenbus_suspend(void)
893 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
894 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
897 EXPORT_SYMBOL_GPL(xenbus_suspend);
899 void xenbus_resume(void)
903 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
904 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
906 EXPORT_SYMBOL_GPL(xenbus_resume);
909 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
910 int xenstored_ready = 0;
913 int register_xenstore_notifier(struct notifier_block *nb)
917 if (xenstored_ready > 0)
918 ret = nb->notifier_call(nb, 0, NULL);
920 blocking_notifier_chain_register(&xenstore_notifier_list, nb);
924 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
926 void unregister_xenstore_notifier(struct notifier_block *nb)
928 blocking_notifier_chain_unregister(&xenstore_notifier_list, nb);
930 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
933 void xenbus_probe(void *unused)
935 BUG_ON((xenstored_ready <= 0));
937 /* Enumerate devices in xenstore. */
938 xenbus_probe_devices(&xenbus_frontend);
939 xenbus_probe_devices(&xenbus_backend);
941 /* Watch for changes. */
942 register_xenbus_watch(&fe_watch);
943 register_xenbus_watch(&be_watch);
945 /* Notify others that xenstore is up */
946 blocking_notifier_call_chain(&xenstore_notifier_list, 0, NULL);
950 #ifdef CONFIG_PROC_FS
951 static struct file_operations xsd_kva_fops;
952 static struct proc_dir_entry *xsd_kva_intf;
953 static struct proc_dir_entry *xsd_port_intf;
955 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
957 size_t size = vma->vm_end - vma->vm_start;
959 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
962 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
963 size, vma->vm_page_prot))
969 static int xsd_kva_read(char *page, char **start, off_t off,
970 int count, int *eof, void *data)
974 len = sprintf(page, "0x%p", xen_store_interface);
979 static int xsd_port_read(char *page, char **start, off_t off,
980 int count, int *eof, void *data)
984 len = sprintf(page, "%d", xen_store_evtchn);
990 static int __init xenbus_probe_init(void)
993 unsigned long page = 0;
997 if (!is_running_on_xen())
1000 /* Register ourselves with the kernel bus subsystem */
1001 bus_register(&xenbus_frontend.bus);
1002 bus_register(&xenbus_backend.bus);
1005 * Domain0 doesn't have a store_evtchn or store_mfn yet.
1007 if (is_initial_xendomain()) {
1008 struct evtchn_alloc_unbound alloc_unbound;
1010 /* Allocate page. */
1011 page = get_zeroed_page(GFP_KERNEL);
1015 xen_store_mfn = xen_start_info->store_mfn =
1016 pfn_to_mfn(virt_to_phys((void *)page) >>
1019 /* Next allocate a local port which xenstored can bind to */
1020 alloc_unbound.dom = DOMID_SELF;
1021 alloc_unbound.remote_dom = 0;
1023 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1028 xen_store_evtchn = xen_start_info->store_evtchn =
1031 #ifdef CONFIG_PROC_FS
1032 /* And finally publish the above info in /proc/xen */
1033 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1035 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1036 sizeof(xsd_kva_fops));
1037 xsd_kva_fops.mmap = xsd_kva_mmap;
1038 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1039 xsd_kva_intf->read_proc = xsd_kva_read;
1041 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1043 xsd_port_intf->read_proc = xsd_port_read;
1045 xen_store_interface = mfn_to_virt(xen_store_mfn);
1047 xenstored_ready = 1;
1049 xen_store_evtchn = xen_start_info->store_evtchn;
1050 xen_store_mfn = xen_start_info->store_mfn;
1051 xen_store_interface = mfn_to_virt(xen_store_mfn);
1053 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1054 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1055 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1063 /* Initialize the interface to xenstore. */
1067 "XENBUS: Error initializing xenstore comms: %i\n", err);
1071 /* Register ourselves with the kernel device subsystem */
1072 device_register(&xenbus_frontend.dev);
1073 device_register(&xenbus_backend.dev);
1075 if (!is_initial_xendomain())
1085 * Do not unregister the xenbus front/backend buses here. The buses
1086 * must exist because front/backend drivers will use them when they are
1093 postcore_initcall(xenbus_probe_init);
1095 MODULE_LICENSE("Dual BSD/GPL");
1098 static int is_disconnected_device(struct device *dev, void *data)
1100 struct xenbus_device *xendev = to_xenbus_device(dev);
1101 struct device_driver *drv = data;
1104 * A device with no driver will never connect. We care only about
1105 * devices which should currently be in the process of connecting.
1110 /* Is this search limited to a particular driver? */
1111 if (drv && (dev->driver != drv))
1114 return (xendev->state != XenbusStateConnected);
1117 static int exists_disconnected_device(struct device_driver *drv)
1119 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1120 is_disconnected_device);
1123 static int print_device_status(struct device *dev, void *data)
1125 struct xenbus_device *xendev = to_xenbus_device(dev);
1126 struct device_driver *drv = data;
1128 /* Is this operation limited to a particular driver? */
1129 if (drv && (dev->driver != drv))
1133 /* Information only: is this too noisy? */
1134 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1136 } else if (xendev->state != XenbusStateConnected) {
1137 printk(KERN_WARNING "XENBUS: Timeout connecting "
1138 "to device: %s (state %d)\n",
1139 xendev->nodename, xendev->state);
1145 /* We only wait for device setup after most initcalls have run. */
1146 static int ready_to_wait_for_devices;
1149 * On a 10 second timeout, wait for all devices currently configured. We need
1150 * to do this to guarantee that the filesystems and / or network devices
1151 * needed for boot are available, before we can allow the boot to proceed.
1153 * This needs to be on a late_initcall, to happen after the frontend device
1154 * drivers have been initialised, but before the root fs is mounted.
1156 * A possible improvement here would be to have the tools add a per-device
1157 * flag to the store entry, indicating whether it is needed at boot time.
1158 * This would allow people who knew what they were doing to accelerate their
1159 * boot slightly, but of course needs tools or manual intervention to set up
1160 * those flags correctly.
1162 static void wait_for_devices(struct xenbus_driver *xendrv)
1164 unsigned long timeout = jiffies + 10*HZ;
1165 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1167 if (!ready_to_wait_for_devices || !is_running_on_xen())
1170 while (exists_disconnected_device(drv)) {
1171 if (time_after(jiffies, timeout))
1173 schedule_timeout_interruptible(HZ/10);
1176 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1177 print_device_status);
1181 static int __init boot_wait_for_devices(void)
1183 ready_to_wait_for_devices = 1;
1184 wait_for_devices(NULL);
1188 late_initcall(boot_wait_for_devices);