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
7 * Copyright (C) 2007 Solarflare Communications, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define DPRINTK(fmt, args...) \
35 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
36 __FUNCTION__, __LINE__, ##args)
38 #include <linux/kernel.h>
39 #include <linux/version.h>
40 #include <linux/err.h>
41 #include <linux/string.h>
42 #include <linux/ctype.h>
43 #include <linux/fcntl.h>
45 #include <linux/sched.h>
46 #include <linux/proc_fs.h>
47 #include <linux/notifier.h>
48 #include <linux/mutex.h>
50 #include <linux/slab.h>
51 #include <linux/module.h>
54 #include <asm/pgtable.h>
55 #if defined(CONFIG_XEN) || defined(MODULE)
56 #include <asm/hypervisor.h>
57 #include <xen/xenbus.h>
58 #include <xen/xen_proc.h>
59 #include <xen/evtchn.h>
60 #include <xen/features.h>
61 #include <xen/gnttab.h>
63 #define PARAVIRT_EXPORT_SYMBOL(sym) __typeof__(sym) sym
65 #include <asm/xen/hypervisor.h>
68 #include <xen/xenbus.h>
69 #include <xen/events.h>
72 #define PARAVIRT_EXPORT_SYMBOL EXPORT_SYMBOL_GPL
79 #include "xenbus_comms.h"
80 #include "xenbus_probe.h"
82 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
83 #include <xen/platform-compat.h>
87 EXPORT_SYMBOL_GPL(xen_store_evtchn);
89 struct xenstore_domain_interface *xen_store_interface;
90 EXPORT_SYMBOL_GPL(xen_store_interface);
92 static unsigned long xen_store_mfn;
94 extern struct mutex xenwatch_mutex;
97 #ifdef CONFIG_XEN_UNPRIVILEGED_GUEST
100 BLOCKING_NOTIFIER_HEAD(xenstore_chain);
102 #if defined(CONFIG_XEN) || defined(MODULE)
103 static void wait_for_devices(struct xenbus_driver *xendrv);
106 /* If something in array of ids matches this device, return it. */
107 static const struct xenbus_device_id *
108 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
110 for (; *arr->devicetype != '\0'; arr++) {
111 if (!strcmp(arr->devicetype, dev->devicetype))
117 int xenbus_match(struct device *_dev, struct device_driver *_drv)
119 struct xenbus_driver *drv = to_xenbus_driver(_drv);
124 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
126 PARAVIRT_EXPORT_SYMBOL(xenbus_match);
129 static void free_otherend_details(struct xenbus_device *dev)
131 kfree(dev->otherend);
132 dev->otherend = NULL;
136 static void free_otherend_watch(struct xenbus_device *dev)
138 if (dev->otherend_watch.node) {
139 unregister_xenbus_watch(&dev->otherend_watch);
140 kfree(dev->otherend_watch.node);
141 dev->otherend_watch.node = NULL;
146 int xenbus_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);
171 PARAVIRT_EXPORT_SYMBOL(xenbus_read_otherend_details);
173 #if defined(CONFIG_XEN) || defined(MODULE)
175 static int read_backend_details(struct xenbus_device *xendev)
177 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
180 static void otherend_changed(struct xenbus_watch *watch,
181 const char **vec, unsigned int len)
182 #else /* !CONFIG_XEN && !MODULE */
183 void xenbus_otherend_changed(struct xenbus_watch *watch,
184 const char **vec, unsigned int len,
185 int ignore_on_shutdown)
186 #endif /* CONFIG_XEN || MODULE */
188 struct xenbus_device *dev =
189 container_of(watch, struct xenbus_device, otherend_watch);
190 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
191 enum xenbus_state state;
193 /* Protect us against watches firing on old details when the otherend
194 details change, say immediately after a resume. */
195 if (!dev->otherend ||
196 strncmp(dev->otherend, vec[XS_WATCH_PATH],
197 strlen(dev->otherend))) {
198 dev_dbg(&dev->dev, "Ignoring watch at %s", vec[XS_WATCH_PATH]);
202 state = xenbus_read_driver_state(dev->otherend);
204 dev_dbg(&dev->dev, "state is %d (%s), %s, %s",
205 state, xenbus_strstate(state), dev->otherend_watch.node,
208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
210 * Ignore xenbus transitions during shutdown. This prevents us doing
211 * work that can fail e.g., when the rootfs is gone.
213 if (system_state > SYSTEM_RUNNING) {
214 /* If we're frontend, drive the state machine to Closed. */
215 /* This should cause the backend to release our resources. */
216 # if defined(CONFIG_XEN) || defined(MODULE)
217 const struct xen_bus_type *bus =
218 container_of(dev->dev.bus, struct xen_bus_type, bus);
219 int ignore_on_shutdown = (bus->levels == 2);
222 if (ignore_on_shutdown && (state == XenbusStateClosing))
223 xenbus_frontend_closed(dev);
228 if (drv->otherend_changed)
229 drv->otherend_changed(dev, state);
231 PARAVIRT_EXPORT_SYMBOL(xenbus_otherend_changed);
234 static int talk_to_otherend(struct xenbus_device *dev)
236 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
238 free_otherend_watch(dev);
239 free_otherend_details(dev);
241 return drv->read_otherend_details(dev);
246 static int watch_otherend(struct xenbus_device *dev)
248 #if defined(CONFIG_XEN) || defined(MODULE)
249 return xenbus_watch_path2(dev, dev->otherend, "state",
250 &dev->otherend_watch, otherend_changed);
252 struct xen_bus_type *bus =
253 container_of(dev->dev.bus, struct xen_bus_type, bus);
255 return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
256 bus->otherend_changed,
257 "%s/%s", dev->otherend, "state");
262 int xenbus_dev_probe(struct device *_dev)
264 struct xenbus_device *dev = to_xenbus_device(_dev);
265 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
266 const struct xenbus_device_id *id;
269 DPRINTK("%s", dev->nodename);
276 id = match_device(drv->ids, dev);
282 err = talk_to_otherend(dev);
285 "xenbus_probe: talk_to_otherend on %s failed.\n",
290 err = drv->probe(dev, id);
294 err = watch_otherend(dev);
297 "xenbus_probe: watch_otherend on %s failed.\n",
304 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
305 xenbus_switch_state(dev, XenbusStateClosed);
306 #if defined(CONFIG_XEN) || defined(MODULE)
312 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_probe);
314 int xenbus_dev_remove(struct device *_dev)
316 struct xenbus_device *dev = to_xenbus_device(_dev);
317 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
319 DPRINTK("%s", dev->nodename);
321 free_otherend_watch(dev);
322 free_otherend_details(dev);
327 xenbus_switch_state(dev, XenbusStateClosed);
330 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_remove);
332 void xenbus_dev_shutdown(struct device *_dev)
334 struct xenbus_device *dev = to_xenbus_device(_dev);
335 unsigned long timeout = 5*HZ;
337 DPRINTK("%s", dev->nodename);
339 /* Commented out since xenstored stubdom is now minios based not linux based
340 #define XENSTORE_DOMAIN_SHARES_THIS_KERNEL
342 #ifndef XENSTORE_DOMAIN_SHARES_THIS_KERNEL
343 if (is_initial_xendomain())
347 get_device(&dev->dev);
348 if (dev->state != XenbusStateConnected) {
349 dev_info(&dev->dev, "%s: %s: %s != Connected, skipping\n", __FUNCTION__,
350 dev->nodename, xenbus_strstate(dev->state));
353 xenbus_switch_state(dev, XenbusStateClosing);
355 if (!strcmp(dev->devicetype, "vfb"))
358 timeout = wait_for_completion_timeout(&dev->down, timeout);
360 dev_info(&dev->dev, "%s: %s timeout closing device\n",
361 __FUNCTION__, dev->nodename);
363 put_device(&dev->dev);
365 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_shutdown);
367 int xenbus_register_driver_common(struct xenbus_driver *drv,
368 struct xen_bus_type *bus)
375 drv->driver.bus = &bus->bus;
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
377 drv->driver.probe = xenbus_dev_probe;
378 drv->driver.remove = xenbus_dev_remove;
379 drv->driver.shutdown = xenbus_dev_shutdown;
382 mutex_lock(&xenwatch_mutex);
383 ret = driver_register(&drv->driver);
384 mutex_unlock(&xenwatch_mutex);
387 PARAVIRT_EXPORT_SYMBOL(xenbus_register_driver_common);
389 void xenbus_unregister_driver(struct xenbus_driver *drv)
391 driver_unregister(&drv->driver);
393 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
395 struct xb_find_info {
396 struct xenbus_device *dev;
397 const char *nodename;
400 static int cmp_dev(struct device *dev, void *data)
402 struct xenbus_device *xendev = to_xenbus_device(dev);
403 struct xb_find_info *info = data;
405 if (!strcmp(xendev->nodename, info->nodename)) {
413 struct xenbus_device *xenbus_device_find(const char *nodename,
414 struct bus_type *bus)
416 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
418 bus_for_each_dev(bus, NULL, &info, cmp_dev);
422 static int cleanup_dev(struct device *dev, void *data)
424 struct xenbus_device *xendev = to_xenbus_device(dev);
425 struct xb_find_info *info = data;
426 int len = strlen(info->nodename);
428 DPRINTK("%s", info->nodename);
430 /* Match the info->nodename path, or any subdirectory of that path. */
431 if (strncmp(xendev->nodename, info->nodename, len))
434 /* If the node name is longer, ensure it really is a subdirectory. */
435 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
443 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
445 struct xb_find_info info = { .nodename = path };
449 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
451 device_unregister(&info.dev->dev);
452 put_device(&info.dev->dev);
457 static void xenbus_dev_release(struct device *dev)
460 kfree(to_xenbus_device(dev));
463 static ssize_t nodename_show(struct device *dev,
464 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
465 struct device_attribute *attr,
469 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
472 static ssize_t devtype_show(struct device *dev,
473 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
474 struct device_attribute *attr,
478 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
481 static ssize_t modalias_show(struct device *dev,
482 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
483 struct device_attribute *attr,
487 return sprintf(buf, "%s:%s\n", dev->bus->name,
488 to_xenbus_device(dev)->devicetype);
491 struct device_attribute xenbus_dev_attrs[] = {
497 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_attrs);
499 int xenbus_probe_node(struct xen_bus_type *bus,
501 const char *nodename)
504 struct xenbus_device *xendev;
508 enum xenbus_state state = xenbus_read_driver_state(nodename);
513 if (state != XenbusStateInitialising) {
514 /* Device is not new, so ignore it. This can happen if a
515 device is going away after switching to Closed. */
519 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
520 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
524 xendev->state = XenbusStateInitialising;
526 /* Copy the strings into the extra space. */
528 tmpstring = (char *)(xendev + 1);
529 strcpy(tmpstring, nodename);
530 xendev->nodename = tmpstring;
532 tmpstring += strlen(tmpstring) + 1;
533 strcpy(tmpstring, type);
534 xendev->devicetype = tmpstring;
535 init_completion(&xendev->down);
537 #if defined(CONFIG_XEN) || defined(MODULE)
538 xendev->dev.parent = &bus->dev;
540 xendev->dev.bus = &bus->bus;
541 xendev->dev.release = xenbus_dev_release;
543 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
545 char devname[XEN_BUS_ID_SIZE];
547 err = bus->get_bus_id(devname, xendev->nodename);
549 dev_set_name(&xendev->dev, devname);
552 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
557 /* Register with generic device framework. */
558 err = device_register(&xendev->dev);
567 PARAVIRT_EXPORT_SYMBOL(xenbus_probe_node);
569 #if defined(CONFIG_XEN) || defined(MODULE)
571 /* device/<type>/<id> => <type>-<id> */
572 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
574 nodename = strchr(nodename, '/');
575 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
576 pr_warning("XENBUS: bad frontend %s\n", nodename);
580 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
581 if (!strchr(bus_id, '/')) {
582 pr_warning("XENBUS: bus_id %s no slash\n", bus_id);
585 *strchr(bus_id, '/') = '-';
589 /* device/<typename>/<name> */
590 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
596 if (!strcmp(type, "console"))
599 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
603 DPRINTK("%s", nodename);
605 err = xenbus_probe_node(bus, type, nodename);
610 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
611 static int xenbus_uevent_frontend(struct device *dev, struct kobj_uevent_env *env)
613 struct xenbus_device *xdev;
617 xdev = to_xenbus_device(dev);
621 /* stuff we want to pass to /sbin/hotplug */
622 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype) ||
623 add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename) ||
624 add_uevent_var(env, "MODALIAS=xen:%s", xdev->devicetype))
631 /* Bus type for frontend drivers. */
632 static struct xen_bus_type xenbus_frontend = {
634 .levels = 2, /* device/type/<id> */
635 .get_bus_id = frontend_bus_id,
636 .probe = xenbus_probe_frontend,
640 .match = xenbus_match,
641 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
642 .probe = xenbus_dev_probe,
643 .remove = xenbus_dev_remove,
644 .shutdown = xenbus_dev_shutdown,
645 .uevent = xenbus_uevent_frontend,
647 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
648 .dev_attrs = xenbus_dev_attrs,
656 int xenbus_register_frontend(struct xenbus_driver *drv)
660 drv->read_otherend_details = read_backend_details;
662 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
666 /* If this driver is loaded as a module wait for devices to attach. */
667 wait_for_devices(drv);
671 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
675 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
679 unsigned int dir_n = 0;
682 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
686 for (i = 0; i < dir_n; i++) {
687 err = bus->probe(bus, type, dir[i]);
696 int xenbus_probe_devices(struct xen_bus_type *bus)
700 unsigned int i, dir_n;
705 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
709 for (i = 0; i < dir_n; i++) {
710 err = xenbus_probe_device_type(bus, dir[i]);
718 PARAVIRT_EXPORT_SYMBOL(xenbus_probe_devices);
720 static unsigned int char_count(const char *str, char c)
722 unsigned int i, ret = 0;
724 for (i = 0; str[i]; i++)
730 static int strsep_len(const char *str, char c, unsigned int len)
734 for (i = 0; str[i]; i++)
740 return (len == 0) ? i : -ERANGE;
743 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
746 struct xenbus_device *dev;
747 char type[XEN_BUS_ID_SIZE];
748 const char *p, *root;
750 if (bus->error || char_count(node, '/') < 2)
753 exists = xenbus_exists(XBT_NIL, node, "");
755 xenbus_cleanup_devices(node, &bus->bus);
759 /* backend/<type>/... or device/<type>/... */
760 p = strchr(node, '/') + 1;
761 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
762 type[XEN_BUS_ID_SIZE-1] = '\0';
764 rootlen = strsep_len(node, '/', bus->levels);
767 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
771 dev = xenbus_device_find(root, &bus->bus);
773 xenbus_probe_node(bus, type, root);
775 put_device(&dev->dev);
779 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_changed);
781 #if defined(CONFIG_XEN) || defined(MODULE)
782 static void frontend_changed(struct xenbus_watch *watch,
783 const char **vec, unsigned int len)
787 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
790 /* We watch for devices appearing and vanishing. */
791 static struct xenbus_watch fe_watch = {
793 .callback = frontend_changed,
796 static int suspend_dev(struct device *dev, void *data)
798 int xenbus_dev_suspend(struct device *dev)
802 struct xenbus_driver *drv;
803 struct xenbus_device *xdev
804 = container_of(dev, struct xenbus_device, dev);
806 DPRINTK("%s", xdev->nodename);
808 if (dev->driver == NULL)
810 drv = to_xenbus_driver(dev->driver);
812 err = drv->suspend(xdev);
814 pr_warning("xenbus: suspend %s failed: %i\n",
818 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_suspend);
820 #if defined(CONFIG_XEN) || defined(MODULE)
821 static int suspend_cancel_dev(struct device *dev, void *data)
824 struct xenbus_driver *drv;
825 struct xenbus_device *xdev;
829 if (dev->driver == NULL)
831 drv = to_xenbus_driver(dev->driver);
832 xdev = container_of(dev, struct xenbus_device, dev);
833 if (drv->suspend_cancel)
834 err = drv->suspend_cancel(xdev);
836 pr_warning("xenbus: suspend_cancel %s failed: %i\n",
841 static int resume_dev(struct device *dev, void *data)
843 int xenbus_dev_resume(struct device *dev)
847 struct xenbus_driver *drv;
848 struct xenbus_device *xdev
849 = container_of(dev, struct xenbus_device, dev);
851 DPRINTK("%s", xdev->nodename);
853 if (dev->driver == NULL)
855 drv = to_xenbus_driver(dev->driver);
856 err = talk_to_otherend(xdev);
858 pr_warning("xenbus: resume (talk_to_otherend) %s failed: %i\n",
863 xdev->state = XenbusStateInitialising;
866 err = drv->resume(xdev);
868 pr_warning("xenbus: resume %s failed: %i\n",
874 err = watch_otherend(xdev);
876 pr_warning("xenbus_probe: resume (watch_otherend) %s failed:"
877 " %d\n", dev_name(dev), err);
883 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_resume);
885 #if !defined(CONFIG_XEN) && !defined(MODULE)
886 int xenbus_dev_cancel(struct device *dev)
892 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_cancel);
894 void xenbus_suspend(void)
898 if (!xenbus_frontend.error)
899 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
900 xenbus_backend_suspend(suspend_dev);
904 void xenbus_resume(void)
908 if (!xenbus_frontend.error)
909 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
910 xenbus_backend_resume(resume_dev);
913 void xenbus_suspend_cancel(void)
916 if (!xenbus_frontend.error)
917 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
918 xenbus_backend_resume(suspend_cancel_dev);
922 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
923 atomic_t xenbus_xsd_state = ATOMIC_INIT(XENBUS_XSD_UNCOMMITTED);
930 register_xenstore_notifier(struct notifier_block *nb)
934 if (is_xenstored_ready())
935 ret = nb->notifier_call(nb, 0, NULL);
937 blocking_notifier_chain_register(&xenstore_chain, nb);
942 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
944 void unregister_xenstore_notifier(struct notifier_block *nb)
946 blocking_notifier_chain_unregister(&xenstore_chain, nb);
948 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
952 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
953 static int backend_state;
955 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
956 const char **v, unsigned int l)
958 if (xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state) != 1)
959 backend_state = XenbusStateUnknown;
960 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
961 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
962 wake_up(&backend_state_wq);
965 static void xenbus_reset_wait_for_backend(char *be, int expected)
968 timeout = wait_event_interruptible_timeout(backend_state_wq,
969 backend_state == expected, 5 * HZ);
971 pr_info("XENBUS: backend %s timed out.\n", be);
975 * Reset frontend if it is in Connected or Closed state.
976 * Wait for backend to catch up.
977 * State Connected happens during kdump, Closed after kexec.
979 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
981 struct xenbus_watch be_watch;
983 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
984 be, xenbus_strstate(be_state));
986 memset(&be_watch, 0, sizeof(be_watch));
987 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
991 be_watch.callback = xenbus_reset_backend_state_changed;
992 backend_state = XenbusStateUnknown;
994 pr_info("XENBUS: triggering reconnect on %s\n", be);
995 register_xenbus_watch(&be_watch);
997 /* fall through to forward backend to state XenbusStateInitialising */
999 case XenbusStateConnected:
1000 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
1001 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
1003 case XenbusStateClosing:
1004 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
1005 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
1007 case XenbusStateClosed:
1008 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
1009 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
1012 unregister_xenbus_watch(&be_watch);
1013 pr_info("XENBUS: reconnect done on %s\n", be);
1014 kfree(be_watch.node);
1017 static void xenbus_check_frontend(char *class, char *dev)
1019 int be_state, fe_state, err;
1020 char *backend, *frontend;
1022 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
1026 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
1031 case XenbusStateConnected:
1032 case XenbusStateClosed:
1033 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
1034 frontend, xenbus_strstate(fe_state));
1035 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
1036 if (!backend || IS_ERR(backend))
1038 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
1040 xenbus_reset_frontend(frontend, backend, be_state);
1050 static void xenbus_reset_state(void)
1052 char **devclass, **dev;
1053 int devclass_n, dev_n;
1056 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
1057 if (IS_ERR(devclass))
1060 for (i = 0; i < devclass_n; i++) {
1061 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
1064 for (j = 0; j < dev_n; j++)
1065 xenbus_check_frontend(devclass[i], dev[j]);
1073 #if defined(CONFIG_XEN_UNPRIVILEGED_GUEST)
1075 #elif defined(MODULE)
1078 xenbus_probe(struct work_struct *unused)
1080 BUG_ON(!is_xenstored_ready());
1083 /* reset devices in Connected or Closed state */
1084 xenbus_reset_state();
1087 #if defined(CONFIG_XEN) || defined(MODULE)
1088 /* Enumerate devices in xenstore and watch for changes. */
1089 xenbus_probe_devices(&xenbus_frontend);
1090 register_xenbus_watch(&fe_watch);
1091 xenbus_backend_probe_and_watch();
1094 /* Notify others that xenstore is up */
1095 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
1097 PARAVIRT_EXPORT_SYMBOL(xenbus_probe);
1099 #if !defined(CONFIG_XEN) && !defined(MODULE)
1100 static int __init xenbus_probe_initcall(void)
1105 if (xen_initial_domain() || xen_hvm_domain())
1112 device_initcall(xenbus_probe_initcall);
1115 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
1116 #ifdef CONFIG_PROC_FS
1117 static struct file_operations xsd_kva_fops;
1118 static struct proc_dir_entry *xsd_kva_intf;
1119 static struct proc_dir_entry *xsd_port_intf;
1121 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
1123 size_t size = vma->vm_end - vma->vm_start;
1127 old = atomic_cmpxchg(&xenbus_xsd_state,
1128 XENBUS_XSD_UNCOMMITTED,
1129 XENBUS_XSD_LOCAL_INIT);
1131 case XENBUS_XSD_UNCOMMITTED:
1132 rc = xb_init_comms();
1137 case XENBUS_XSD_FOREIGN_INIT:
1138 case XENBUS_XSD_FOREIGN_READY:
1141 case XENBUS_XSD_LOCAL_INIT:
1142 case XENBUS_XSD_LOCAL_READY:
1147 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
1150 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
1151 size, vma->vm_page_prot))
1157 static int xsd_kva_read(char *page, char **start, off_t off,
1158 int count, int *eof, void *data)
1162 len = sprintf(page, "0x%p", xen_store_interface);
1167 static int xsd_port_read(char *page, char **start, off_t off,
1168 int count, int *eof, void *data)
1172 len = sprintf(page, "%d", xen_store_evtchn);
1178 #ifdef CONFIG_XEN_XENBUS_DEV
1179 int xenbus_conn(domid_t remote_dom, grant_ref_t *grant_ref,
1180 evtchn_port_t *local_port)
1182 struct evtchn_alloc_unbound alloc_unbound;
1185 BUG_ON(atomic_read(&xenbus_xsd_state) != XENBUS_XSD_FOREIGN_INIT);
1186 BUG_ON(!is_initial_xendomain());
1188 remove_xen_proc_entry("xsd_kva");
1189 remove_xen_proc_entry("xsd_port");
1191 rc = close_evtchn(xen_store_evtchn);
1195 alloc_unbound.dom = DOMID_SELF;
1196 alloc_unbound.remote_dom = remote_dom;
1197 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1201 *local_port = xen_store_evtchn = alloc_unbound.port;
1203 /* keep the old page (xen_store_mfn, xen_store_interface) */
1204 rc = gnttab_grant_foreign_access(remote_dom, xen_store_mfn,
1210 rc = xb_init_comms();
1217 rc2 = close_evtchn(xen_store_evtchn);
1219 pr_warning("XENBUS: Error freeing xenstore event channel:"
1222 xen_store_evtchn = -1;
1226 #endif /* CONFIG_XEN_PRIVILEGED_GUEST */
1228 /* Set up event channel for xenstored which is run as a local process
1229 * (this is normally used only in dom0)
1231 static int __init xenstored_local_init(void)
1234 unsigned long page = 0;
1235 struct evtchn_alloc_unbound alloc_unbound;
1237 /* Allocate Xenstore page */
1238 page = get_zeroed_page(GFP_KERNEL);
1242 xen_store_mfn = xen_start_info->store_mfn =
1243 pfn_to_mfn(virt_to_phys((void *)page) >>
1246 /* Next allocate a local port which xenstored can bind to */
1247 alloc_unbound.dom = DOMID_SELF;
1248 alloc_unbound.remote_dom = DOMID_SELF;
1250 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1256 xen_store_evtchn = xen_start_info->store_evtchn =
1278 if (!is_running_on_xen())
1281 #if defined(CONFIG_XEN) || defined(MODULE)
1282 /* Register ourselves with the kernel bus subsystem */
1283 xenbus_frontend.error = bus_register(&xenbus_frontend.bus);
1284 if (xenbus_frontend.error)
1285 pr_warning("XENBUS: Error registering frontend bus: %i\n",
1286 xenbus_frontend.error);
1287 xenbus_backend_bus_register();
1290 * Domain0 doesn't have a store_evtchn or store_mfn yet.
1292 if (is_initial_xendomain()) {
1293 err = xenstored_local_init();
1297 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
1298 /* And finally publish the above info in /proc/xen */
1299 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1301 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1302 sizeof(xsd_kva_fops));
1303 xsd_kva_fops.mmap = xsd_kva_mmap;
1304 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1305 xsd_kva_intf->read_proc = xsd_kva_read;
1307 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1309 xsd_port_intf->read_proc = xsd_port_read;
1311 xen_store_interface = mfn_to_virt(xen_store_mfn);
1316 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
1319 xen_store_evtchn = (int)v;
1320 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
1323 xen_store_mfn = (unsigned long)v;
1324 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1328 xen_store_evtchn = xen_start_info->store_evtchn;
1329 xen_store_mfn = xen_start_info->store_mfn;
1330 xen_store_interface = mfn_to_virt(xen_store_mfn);
1332 atomic_set(&xenbus_xsd_state, XENBUS_XSD_FOREIGN_READY);
1334 /* Initialize the shared memory rings to talk to xenstored */
1335 err = xb_init_comms();
1341 #else /* !defined(CONFIG_XEN) && !defined(MODULE) */
1342 xenbus_ring_ops_init();
1344 if (xen_hvm_domain()) {
1346 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
1349 xen_store_evtchn = (int)v;
1350 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
1353 xen_store_mfn = (unsigned long)v;
1354 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
1356 xen_store_evtchn = xen_start_info->store_evtchn;
1357 xen_store_mfn = xen_start_info->store_mfn;
1358 if (xen_store_evtchn)
1359 atomic_set(&xenbus_xsd_state, XENBUS_XSD_FOREIGN_READY);
1361 err = xenstored_local_init();
1365 xen_store_interface = mfn_to_virt(xen_store_mfn);
1369 /* Initialize the interface to xenstore. */
1372 pr_warning("XENBUS: Error initializing xenstore comms: %i\n",
1377 #if defined(CONFIG_XEN) || defined(MODULE)
1378 /* Register ourselves with the kernel device subsystem */
1379 if (!xenbus_frontend.error) {
1380 xenbus_frontend.error = device_register(&xenbus_frontend.dev);
1381 if (xenbus_frontend.error) {
1382 bus_unregister(&xenbus_frontend.bus);
1383 pr_warning("XENBUS: Error registering frontend device:"
1384 " %d\n", xenbus_frontend.error);
1387 xenbus_backend_device_register();
1389 if (!is_initial_xendomain())
1393 #if defined(CONFIG_XEN_COMPAT_XENFS) && !defined(MODULE)
1395 * Create xenfs mountpoint in /proc for compatibility with
1396 * utilities that expect to find "xenbus" under "/proc/xen".
1398 proc_mkdir("xen", NULL);
1405 * Do not unregister the xenbus front/backend buses here. The buses
1406 * must exist because front/backend drivers will use them when they are
1413 postcore_initcall(xenbus_init);
1415 MODULE_LICENSE("Dual BSD/GPL");
1417 MODULE_LICENSE("GPL");
1421 #if defined(CONFIG_XEN) || defined(MODULE)
1423 static int is_device_connecting(struct device *dev, void *data)
1425 struct xenbus_device *xendev = to_xenbus_device(dev);
1426 struct device_driver *drv = data;
1427 struct xenbus_driver *xendrv;
1430 * A device with no driver will never connect. We care only about
1431 * devices which should currently be in the process of connecting.
1436 /* Is this search limited to a particular driver? */
1437 if (drv && (dev->driver != drv))
1440 xendrv = to_xenbus_driver(dev->driver);
1441 return (xendev->state < XenbusStateConnected ||
1442 (xendev->state == XenbusStateConnected &&
1443 xendrv->is_ready && !xendrv->is_ready(xendev)));
1446 static int exists_connecting_device(struct device_driver *drv)
1448 if (xenbus_frontend.error)
1449 return xenbus_frontend.error;
1450 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1451 is_device_connecting);
1454 static int print_device_status(struct device *dev, void *data)
1456 struct xenbus_device *xendev = to_xenbus_device(dev);
1457 struct device_driver *drv = data;
1458 struct xenbus_driver *xendrv;
1460 /* Is this operation limited to a particular driver? */
1461 if (drv && (dev->driver != drv))
1465 /* Information only: is this too noisy? */
1466 pr_info("XENBUS: Device with no driver: %s\n",
1471 if (xendev->state < XenbusStateConnected) {
1472 enum xenbus_state rstate = XenbusStateUnknown;
1473 if (xendev->otherend)
1474 rstate = xenbus_read_driver_state(xendev->otherend);
1475 pr_warning("XENBUS: Timeout connecting to device: %s"
1476 " (local state %d, remote state %d)\n",
1477 xendev->nodename, xendev->state, rstate);
1480 xendrv = to_xenbus_driver(dev->driver);
1481 if (xendrv->is_ready && !xendrv->is_ready(xendev))
1482 pr_warning("XENBUS: Device not ready: %s\n",
1488 /* We only wait for device setup after most initcalls have run. */
1489 static int ready_to_wait_for_devices;
1492 * On a 5-minute timeout, wait for all devices currently configured. We need
1493 * to do this to guarantee that the filesystems and / or network devices
1494 * needed for boot are available, before we can allow the boot to proceed.
1496 * This needs to be on a late_initcall, to happen after the frontend device
1497 * drivers have been initialised, but before the root fs is mounted.
1499 * A possible improvement here would be to have the tools add a per-device
1500 * flag to the store entry, indicating whether it is needed at boot time.
1501 * This would allow people who knew what they were doing to accelerate their
1502 * boot slightly, but of course needs tools or manual intervention to set up
1503 * those flags correctly.
1505 static void wait_for_devices(struct xenbus_driver *xendrv)
1507 unsigned long start = jiffies;
1508 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1509 unsigned int seconds_waited = 0;
1511 if (!ready_to_wait_for_devices || !is_running_on_xen())
1514 while (exists_connecting_device(drv)) {
1515 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
1516 if (!seconds_waited)
1517 pr_warning("XENBUS: Waiting for "
1518 "devices to initialise: ");
1519 seconds_waited += 5;
1520 printk("%us...", 300 - seconds_waited);
1521 if (seconds_waited == 300)
1525 schedule_timeout_interruptible(HZ/10);
1531 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1532 print_device_status);
1536 static int __init boot_wait_for_devices(void)
1538 #if !defined(CONFIG_XEN) && !defined(MODULE)
1539 if (xen_hvm_domain() && !xen_platform_pci_unplug)
1543 if (!xenbus_frontend.error) {
1544 ready_to_wait_for_devices = 1;
1545 wait_for_devices(NULL);
1550 late_initcall(boot_wait_for_devices);
1553 int xenbus_for_each_frontend(void *arg, int (*fn)(struct device *, void *))
1555 return bus_for_each_dev(&xenbus_frontend.bus, NULL, arg, fn);
1557 EXPORT_SYMBOL_GPL(xenbus_for_each_frontend);
1559 #endif /* CONFIG_XEN || MODULE */