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 <asm/hypercall.h>
52 #include <xen/xenbus.h>
53 #include <xen/xen_proc.h>
54 #include <xen/evtchn.h>
55 #include <xen/features.h>
58 #include "xenbus_comms.h"
61 struct xenstore_domain_interface *xen_store_interface;
62 static unsigned long xen_store_mfn;
64 extern struct mutex xenwatch_mutex;
66 static BLOCKING_NOTIFIER_HEAD(xenstore_notifier_list);
68 static void wait_for_devices(struct xenbus_driver *xendrv);
70 static int xenbus_probe_frontend(const char *type, const char *name);
71 static int xenbus_uevent_backend(struct device *dev, char **envp,
72 int num_envp, char *buffer, int buffer_size);
73 static int xenbus_probe_backend(const char *type, const char *domid);
75 static int xenbus_dev_probe(struct device *_dev);
76 static int xenbus_dev_remove(struct device *_dev);
77 static void xenbus_dev_shutdown(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))
90 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
92 struct xenbus_driver *drv = to_xenbus_driver(_drv);
97 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
104 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
105 int (*probe)(const char *type, const char *dir);
111 /* device/<type>/<id> => <type>-<id> */
112 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
114 nodename = strchr(nodename, '/');
115 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
116 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
120 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
121 if (!strchr(bus_id, '/')) {
122 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
125 *strchr(bus_id, '/') = '-';
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 static 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,
155 xenbus_dev_fatal(xendev, err,
156 "reading other end details from %s",
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.",
166 free_otherend_details(xendev);
174 static int read_backend_details(struct xenbus_device *xendev)
176 return read_otherend_details(xendev, "backend-id", "backend");
180 static int read_frontend_details(struct xenbus_device *xendev)
182 return read_otherend_details(xendev, "frontend-id", "frontend");
186 /* Bus type for frontend drivers. */
187 static struct xen_bus_type xenbus_frontend = {
189 .levels = 2, /* device/type/<id> */
190 .get_bus_id = frontend_bus_id,
191 .probe = xenbus_probe_frontend,
194 .match = xenbus_match,
195 .probe = xenbus_dev_probe,
196 .remove = xenbus_dev_remove,
197 .shutdown = xenbus_dev_shutdown,
204 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
205 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
208 const char *devid, *type, *frontend;
209 unsigned int typelen;
211 type = strchr(nodename, '/');
215 typelen = strcspn(type, "/");
216 if (!typelen || type[typelen] != '/')
219 devid = strrchr(nodename, '/') + 1;
221 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
222 "frontend", NULL, &frontend,
226 if (strlen(frontend) == 0)
228 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
236 if (snprintf(bus_id, BUS_ID_SIZE,
237 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
242 static struct xen_bus_type xenbus_backend = {
244 .levels = 3, /* backend/type/<frontend>/<id> */
245 .get_bus_id = backend_bus_id,
246 .probe = xenbus_probe_backend,
248 .name = "xen-backend",
249 .match = xenbus_match,
250 .probe = xenbus_dev_probe,
251 .remove = xenbus_dev_remove,
252 // .shutdown = xenbus_dev_shutdown,
253 .uevent = xenbus_uevent_backend,
256 .bus_id = "xen-backend",
260 static int xenbus_uevent_backend(struct device *dev, char **envp,
261 int num_envp, char *buffer, int buffer_size)
263 struct xenbus_device *xdev;
264 struct xenbus_driver *drv;
273 xdev = to_xenbus_device(dev);
277 /* stuff we want to pass to /sbin/hotplug */
278 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
279 "XENBUS_TYPE=%s", xdev->devicetype);
281 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
282 "XENBUS_PATH=%s", xdev->nodename);
284 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
285 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
287 /* terminate, set to next free slot, shrink available space */
291 buffer = &buffer[length];
292 buffer_size -= length;
295 drv = to_xenbus_driver(dev->driver);
296 if (drv && drv->uevent)
297 return drv->uevent(xdev, envp, num_envp, buffer,
304 static void otherend_changed(struct xenbus_watch *watch,
305 const char **vec, unsigned int len)
307 struct xenbus_device *dev =
308 container_of(watch, struct xenbus_device, otherend_watch);
309 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
310 enum xenbus_state state;
312 /* Protect us against watches firing on old details when the otherend
313 details change, say immediately after a resume. */
314 if (!dev->otherend ||
315 strncmp(dev->otherend, vec[XS_WATCH_PATH],
316 strlen(dev->otherend))) {
317 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
321 state = xenbus_read_driver_state(dev->otherend);
323 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
324 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
327 * Ignore xenbus transitions during shutdown. This prevents us doing
328 * work that can fail e.g., when the rootfs is gone.
330 if (system_state > SYSTEM_RUNNING) {
331 struct xen_bus_type *bus = bus;
332 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
333 /* If we're frontend, drive the state machine to Closed. */
334 /* This should cause the backend to release our resources. */
335 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
336 xenbus_frontend_closed(dev);
340 if (drv->otherend_changed)
341 drv->otherend_changed(dev, state);
345 static int talk_to_otherend(struct xenbus_device *dev)
347 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
349 free_otherend_watch(dev);
350 free_otherend_details(dev);
352 return drv->read_otherend_details(dev);
356 static int watch_otherend(struct xenbus_device *dev)
358 return xenbus_watch_path2(dev, dev->otherend, "state",
359 &dev->otherend_watch, otherend_changed);
363 static int xenbus_dev_probe(struct device *_dev)
365 struct xenbus_device *dev = to_xenbus_device(_dev);
366 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
367 const struct xenbus_device_id *id;
370 DPRINTK("%s", dev->nodename);
377 id = match_device(drv->ids, dev);
383 err = talk_to_otherend(dev);
386 "xenbus_probe: talk_to_otherend on %s failed.\n",
391 err = drv->probe(dev, id);
395 err = watch_otherend(dev);
398 "xenbus_probe: watch_otherend on %s failed.\n",
405 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
406 xenbus_switch_state(dev, XenbusStateClosed);
410 static int xenbus_dev_remove(struct device *_dev)
412 struct xenbus_device *dev = to_xenbus_device(_dev);
413 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
415 DPRINTK("%s", dev->nodename);
417 free_otherend_watch(dev);
418 free_otherend_details(dev);
423 xenbus_switch_state(dev, XenbusStateClosed);
427 static void xenbus_dev_shutdown(struct device *_dev)
429 struct xenbus_device *dev = to_xenbus_device(_dev);
430 unsigned long timeout = 5*HZ;
432 DPRINTK("%s", dev->nodename);
434 get_device(&dev->dev);
435 if (dev->state != XenbusStateConnected) {
436 printk("%s: %s: %s != Connected, skipping\n", __FUNCTION__,
437 dev->nodename, xenbus_strstate(dev->state));
440 xenbus_switch_state(dev, XenbusStateClosing);
441 timeout = wait_for_completion_timeout(&dev->down, timeout);
443 printk("%s: %s timeout closing device\n", __FUNCTION__, dev->nodename);
445 put_device(&dev->dev);
448 static int xenbus_register_driver_common(struct xenbus_driver *drv,
449 struct xen_bus_type *bus)
453 drv->driver.name = drv->name;
454 drv->driver.bus = &bus->bus;
455 drv->driver.owner = drv->owner;
457 mutex_lock(&xenwatch_mutex);
458 ret = driver_register(&drv->driver);
459 mutex_unlock(&xenwatch_mutex);
463 int xenbus_register_frontend(struct xenbus_driver *drv)
467 drv->read_otherend_details = read_backend_details;
469 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
473 /* If this driver is loaded as a module wait for devices to attach. */
474 wait_for_devices(drv);
478 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
480 int xenbus_register_backend(struct xenbus_driver *drv)
482 drv->read_otherend_details = read_frontend_details;
484 return xenbus_register_driver_common(drv, &xenbus_backend);
486 EXPORT_SYMBOL_GPL(xenbus_register_backend);
488 void xenbus_unregister_driver(struct xenbus_driver *drv)
490 driver_unregister(&drv->driver);
492 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
496 struct xenbus_device *dev;
497 const char *nodename;
500 static int cmp_dev(struct device *dev, void *data)
502 struct xenbus_device *xendev = to_xenbus_device(dev);
503 struct xb_find_info *info = data;
505 if (!strcmp(xendev->nodename, info->nodename)) {
513 struct xenbus_device *xenbus_device_find(const char *nodename,
514 struct bus_type *bus)
516 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
518 bus_for_each_dev(bus, NULL, &info, cmp_dev);
522 static int cleanup_dev(struct device *dev, void *data)
524 struct xenbus_device *xendev = to_xenbus_device(dev);
525 struct xb_find_info *info = data;
526 int len = strlen(info->nodename);
528 DPRINTK("%s", info->nodename);
530 /* Match the info->nodename path, or any subdirectory of that path. */
531 if (strncmp(xendev->nodename, info->nodename, len))
534 /* If the node name is longer, ensure it really is a subdirectory. */
535 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
543 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
545 struct xb_find_info info = { .nodename = path };
549 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
551 device_unregister(&info.dev->dev);
552 put_device(&info.dev->dev);
557 static void xenbus_dev_release(struct device *dev)
560 kfree(to_xenbus_device(dev));
563 static ssize_t xendev_show_nodename(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
568 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
570 static ssize_t xendev_show_devtype(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
575 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
578 static int xenbus_probe_node(struct xen_bus_type *bus,
580 const char *nodename)
583 struct xenbus_device *xendev;
587 enum xenbus_state state = xenbus_read_driver_state(nodename);
589 if (state != XenbusStateInitialising) {
590 /* Device is not new, so ignore it. This can happen if a
591 device is going away after switching to Closed. */
595 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
596 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
600 /* Copy the strings into the extra space. */
602 tmpstring = (char *)(xendev + 1);
603 strcpy(tmpstring, nodename);
604 xendev->nodename = tmpstring;
606 tmpstring += strlen(tmpstring) + 1;
607 strcpy(tmpstring, type);
608 xendev->devicetype = tmpstring;
609 init_completion(&xendev->down);
611 xendev->dev.parent = &bus->dev;
612 xendev->dev.bus = &bus->bus;
613 xendev->dev.release = xenbus_dev_release;
615 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
619 /* Register with generic device framework. */
620 err = device_register(&xendev->dev);
624 device_create_file(&xendev->dev, &dev_attr_nodename);
625 device_create_file(&xendev->dev, &dev_attr_devtype);
633 /* device/<typename>/<name> */
634 static int xenbus_probe_frontend(const char *type, const char *name)
639 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name);
643 DPRINTK("%s", nodename);
645 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
650 /* backend/<typename>/<frontend-uuid>/<name> */
651 static int xenbus_probe_backend_unit(const char *dir,
658 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
662 DPRINTK("%s\n", nodename);
664 err = xenbus_probe_node(&xenbus_backend, type, nodename);
669 /* backend/<typename>/<frontend-domid> */
670 static int xenbus_probe_backend(const char *type, const char *domid)
675 unsigned int i, dir_n = 0;
679 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_backend.root, type, domid);
683 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
689 for (i = 0; i < dir_n; i++) {
690 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
699 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
703 unsigned int dir_n = 0;
706 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
710 for (i = 0; i < dir_n; i++) {
711 err = bus->probe(type, dir[i]);
719 static int xenbus_probe_devices(struct xen_bus_type *bus)
723 unsigned int i, dir_n;
725 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
729 for (i = 0; i < dir_n; i++) {
730 err = xenbus_probe_device_type(bus, dir[i]);
738 static unsigned int char_count(const char *str, char c)
740 unsigned int i, ret = 0;
742 for (i = 0; str[i]; i++)
748 static int strsep_len(const char *str, char c, unsigned int len)
752 for (i = 0; str[i]; i++)
758 return (len == 0) ? i : -ERANGE;
761 static void dev_changed(const char *node, struct xen_bus_type *bus)
764 struct xenbus_device *dev;
765 char type[BUS_ID_SIZE];
766 const char *p, *root;
768 if (char_count(node, '/') < 2)
771 exists = xenbus_exists(XBT_NIL, node, "");
773 xenbus_cleanup_devices(node, &bus->bus);
777 /* backend/<type>/... or device/<type>/... */
778 p = strchr(node, '/') + 1;
779 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
780 type[BUS_ID_SIZE-1] = '\0';
782 rootlen = strsep_len(node, '/', bus->levels);
785 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
789 dev = xenbus_device_find(root, &bus->bus);
791 xenbus_probe_node(bus, type, root);
793 put_device(&dev->dev);
798 static void frontend_changed(struct xenbus_watch *watch,
799 const char **vec, unsigned int len)
803 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
806 static void backend_changed(struct xenbus_watch *watch,
807 const char **vec, unsigned int len)
811 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
814 /* We watch for devices appearing and vanishing. */
815 static struct xenbus_watch fe_watch = {
817 .callback = frontend_changed,
820 static struct xenbus_watch be_watch = {
822 .callback = backend_changed,
825 static int suspend_dev(struct device *dev, void *data)
828 struct xenbus_driver *drv;
829 struct xenbus_device *xdev;
833 if (dev->driver == NULL)
835 drv = to_xenbus_driver(dev->driver);
836 xdev = container_of(dev, struct xenbus_device, dev);
838 err = drv->suspend(xdev);
841 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
845 static int resume_dev(struct device *dev, void *data)
848 struct xenbus_driver *drv;
849 struct xenbus_device *xdev;
853 if (dev->driver == NULL)
856 drv = to_xenbus_driver(dev->driver);
857 xdev = container_of(dev, struct xenbus_device, dev);
859 err = talk_to_otherend(xdev);
862 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
867 xdev->state = XenbusStateInitialising;
870 err = drv->resume(xdev);
873 "xenbus: resume %s failed: %i\n",
879 err = watch_otherend(xdev);
882 "xenbus_probe: resume (watch_otherend) %s failed: "
883 "%d.\n", dev->bus_id, err);
890 void xenbus_suspend(void)
894 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
895 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
898 EXPORT_SYMBOL_GPL(xenbus_suspend);
900 void xenbus_resume(void)
904 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
905 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
907 EXPORT_SYMBOL_GPL(xenbus_resume);
910 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
911 int xenstored_ready = 0;
914 int register_xenstore_notifier(struct notifier_block *nb)
918 if (xenstored_ready > 0)
919 ret = nb->notifier_call(nb, 0, NULL);
921 blocking_notifier_chain_register(&xenstore_notifier_list, nb);
925 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
927 void unregister_xenstore_notifier(struct notifier_block *nb)
929 blocking_notifier_chain_unregister(&xenstore_notifier_list, nb);
931 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
934 void xenbus_probe(void *unused)
936 BUG_ON((xenstored_ready <= 0));
938 /* Enumerate devices in xenstore. */
939 xenbus_probe_devices(&xenbus_frontend);
940 xenbus_probe_devices(&xenbus_backend);
942 /* Watch for changes. */
943 register_xenbus_watch(&fe_watch);
944 register_xenbus_watch(&be_watch);
946 /* Notify others that xenstore is up */
947 blocking_notifier_call_chain(&xenstore_notifier_list, 0, NULL);
951 #ifdef CONFIG_PROC_FS
952 static struct file_operations xsd_kva_fops;
953 static struct proc_dir_entry *xsd_kva_intf;
954 static struct proc_dir_entry *xsd_port_intf;
956 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
958 size_t size = vma->vm_end - vma->vm_start;
960 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
963 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
964 size, vma->vm_page_prot))
970 static int xsd_kva_read(char *page, char **start, off_t off,
971 int count, int *eof, void *data)
975 len = sprintf(page, "0x%p", xen_store_interface);
980 static int xsd_port_read(char *page, char **start, off_t off,
981 int count, int *eof, void *data)
985 len = sprintf(page, "%d", xen_store_evtchn);
991 static int __init xenbus_probe_init(void)
994 unsigned long page = 0;
998 if (!is_running_on_xen())
1001 /* Register ourselves with the kernel bus subsystem */
1002 bus_register(&xenbus_frontend.bus);
1003 bus_register(&xenbus_backend.bus);
1006 * Domain0 doesn't have a store_evtchn or store_mfn yet.
1008 if (is_initial_xendomain()) {
1009 struct evtchn_alloc_unbound alloc_unbound;
1011 /* Allocate page. */
1012 page = get_zeroed_page(GFP_KERNEL);
1016 xen_store_mfn = xen_start_info->store_mfn =
1017 pfn_to_mfn(virt_to_phys((void *)page) >>
1020 /* Next allocate a local port which xenstored can bind to */
1021 alloc_unbound.dom = DOMID_SELF;
1022 alloc_unbound.remote_dom = 0;
1024 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1029 xen_store_evtchn = xen_start_info->store_evtchn =
1032 #ifdef CONFIG_PROC_FS
1033 /* And finally publish the above info in /proc/xen */
1034 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1036 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1037 sizeof(xsd_kva_fops));
1038 xsd_kva_fops.mmap = xsd_kva_mmap;
1039 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1040 xsd_kva_intf->read_proc = xsd_kva_read;
1042 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1044 xsd_port_intf->read_proc = xsd_port_read;
1046 xen_store_interface = mfn_to_virt(xen_store_mfn);
1048 xenstored_ready = 1;
1050 xen_store_evtchn = xen_start_info->store_evtchn;
1051 xen_store_mfn = xen_start_info->store_mfn;
1052 xen_store_interface = mfn_to_virt(xen_store_mfn);
1054 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1055 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1056 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1064 /* Initialize the interface to xenstore. */
1068 "XENBUS: Error initializing xenstore comms: %i\n", err);
1072 /* Register ourselves with the kernel device subsystem */
1073 device_register(&xenbus_frontend.dev);
1074 device_register(&xenbus_backend.dev);
1076 if (!is_initial_xendomain())
1086 * Do not unregister the xenbus front/backend buses here. The buses
1087 * must exist because front/backend drivers will use them when they are
1094 postcore_initcall(xenbus_probe_init);
1096 MODULE_LICENSE("Dual BSD/GPL");
1099 static int is_disconnected_device(struct device *dev, void *data)
1101 struct xenbus_device *xendev = to_xenbus_device(dev);
1102 struct device_driver *drv = data;
1105 * A device with no driver will never connect. We care only about
1106 * devices which should currently be in the process of connecting.
1111 /* Is this search limited to a particular driver? */
1112 if (drv && (dev->driver != drv))
1115 return (xendev->state != XenbusStateConnected);
1118 static int exists_disconnected_device(struct device_driver *drv)
1120 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1121 is_disconnected_device);
1124 static int print_device_status(struct device *dev, void *data)
1126 struct xenbus_device *xendev = to_xenbus_device(dev);
1127 struct device_driver *drv = data;
1129 /* Is this operation limited to a particular driver? */
1130 if (drv && (dev->driver != drv))
1134 /* Information only: is this too noisy? */
1135 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1137 } else if (xendev->state != XenbusStateConnected) {
1138 printk(KERN_WARNING "XENBUS: Timeout connecting "
1139 "to device: %s (state %d)\n",
1140 xendev->nodename, xendev->state);
1146 /* We only wait for device setup after most initcalls have run. */
1147 static int ready_to_wait_for_devices;
1150 * On a 10 second timeout, wait for all devices currently configured. We need
1151 * to do this to guarantee that the filesystems and / or network devices
1152 * needed for boot are available, before we can allow the boot to proceed.
1154 * This needs to be on a late_initcall, to happen after the frontend device
1155 * drivers have been initialised, but before the root fs is mounted.
1157 * A possible improvement here would be to have the tools add a per-device
1158 * flag to the store entry, indicating whether it is needed at boot time.
1159 * This would allow people who knew what they were doing to accelerate their
1160 * boot slightly, but of course needs tools or manual intervention to set up
1161 * those flags correctly.
1163 static void wait_for_devices(struct xenbus_driver *xendrv)
1165 unsigned long timeout = jiffies + 10*HZ;
1166 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1168 if (!ready_to_wait_for_devices || !is_running_on_xen())
1171 while (exists_disconnected_device(drv)) {
1172 if (time_after(jiffies, timeout))
1174 schedule_timeout_interruptible(HZ/10);
1177 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1178 print_device_status);
1182 static int __init boot_wait_for_devices(void)
1184 ready_to_wait_for_devices = 1;
1185 wait_for_devices(NULL);
1189 late_initcall(boot_wait_for_devices);