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 __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>
43 #include <linux/notifier.h>
44 #include <linux/kthread.h>
45 #include <linux/mutex.h>
49 #include <asm/pgtable.h>
50 #include <asm/xen/hypervisor.h>
51 #include <xen/xenbus.h>
52 #include <xen/events.h>
55 #include "xenbus_comms.h"
56 #include "xenbus_probe.h"
59 struct xenstore_domain_interface *xen_store_interface;
60 static unsigned long xen_store_mfn;
62 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
64 static void wait_for_devices(struct xenbus_driver *xendrv);
66 static int xenbus_probe_frontend(const char *type, const char *name);
68 static void xenbus_dev_shutdown(struct device *_dev);
70 /* If something in array of ids matches this device, return it. */
71 static const struct xenbus_device_id *
72 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
74 for (; *arr->devicetype != '\0'; arr++) {
75 if (!strcmp(arr->devicetype, dev->devicetype))
81 int xenbus_match(struct device *_dev, struct device_driver *_drv)
83 struct xenbus_driver *drv = to_xenbus_driver(_drv);
88 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
91 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
93 struct xenbus_device *dev = to_xenbus_device(_dev);
95 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
101 /* device/<type>/<id> => <type>-<id> */
102 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
104 nodename = strchr(nodename, '/');
105 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
106 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
110 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
111 if (!strchr(bus_id, '/')) {
112 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
115 *strchr(bus_id, '/') = '-';
120 static void free_otherend_details(struct xenbus_device *dev)
122 kfree(dev->otherend);
123 dev->otherend = NULL;
127 static void free_otherend_watch(struct xenbus_device *dev)
129 if (dev->otherend_watch.node) {
130 unregister_xenbus_watch(&dev->otherend_watch);
131 kfree(dev->otherend_watch.node);
132 dev->otherend_watch.node = NULL;
137 int read_otherend_details(struct xenbus_device *xendev,
138 char *id_node, char *path_node)
140 int err = xenbus_gather(XBT_NIL, xendev->nodename,
141 id_node, "%i", &xendev->otherend_id,
142 path_node, NULL, &xendev->otherend,
145 xenbus_dev_fatal(xendev, err,
146 "reading other end details from %s",
150 if (strlen(xendev->otherend) == 0 ||
151 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
152 xenbus_dev_fatal(xendev, -ENOENT,
153 "unable to read other end from %s. "
154 "missing or inaccessible.",
156 free_otherend_details(xendev);
164 static int read_backend_details(struct xenbus_device *xendev)
166 return read_otherend_details(xendev, "backend-id", "backend");
170 /* Bus type for frontend drivers. */
171 static struct xen_bus_type xenbus_frontend = {
173 .levels = 2, /* device/type/<id> */
174 .get_bus_id = frontend_bus_id,
175 .probe = xenbus_probe_frontend,
178 .match = xenbus_match,
179 .uevent = xenbus_uevent,
180 .probe = xenbus_dev_probe,
181 .remove = xenbus_dev_remove,
182 .shutdown = xenbus_dev_shutdown,
186 static void otherend_changed(struct xenbus_watch *watch,
187 const char **vec, unsigned int len)
189 struct xenbus_device *dev =
190 container_of(watch, struct xenbus_device, otherend_watch);
191 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
192 enum xenbus_state state;
194 /* Protect us against watches firing on old details when the otherend
195 details change, say immediately after a resume. */
196 if (!dev->otherend ||
197 strncmp(dev->otherend, vec[XS_WATCH_PATH],
198 strlen(dev->otherend))) {
199 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
204 state = xenbus_read_driver_state(dev->otherend);
206 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
207 state, xenbus_strstate(state), dev->otherend_watch.node,
211 * Ignore xenbus transitions during shutdown. This prevents us doing
212 * work that can fail e.g., when the rootfs is gone.
214 if (system_state > SYSTEM_RUNNING) {
215 struct xen_bus_type *bus = bus;
216 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
217 /* If we're frontend, drive the state machine to Closed. */
218 /* This should cause the backend to release our resources. */
219 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
220 xenbus_frontend_closed(dev);
224 if (drv->otherend_changed)
225 drv->otherend_changed(dev, state);
229 static int talk_to_otherend(struct xenbus_device *dev)
231 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
233 free_otherend_watch(dev);
234 free_otherend_details(dev);
236 return drv->read_otherend_details(dev);
240 static int watch_otherend(struct xenbus_device *dev)
242 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
243 "%s/%s", dev->otherend, "state");
247 int xenbus_dev_probe(struct device *_dev)
249 struct xenbus_device *dev = to_xenbus_device(_dev);
250 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
251 const struct xenbus_device_id *id;
254 DPRINTK("%s", dev->nodename);
261 id = match_device(drv->ids, dev);
267 err = talk_to_otherend(dev);
269 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
274 err = drv->probe(dev, id);
278 err = watch_otherend(dev);
280 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
287 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
288 xenbus_switch_state(dev, XenbusStateClosed);
292 int xenbus_dev_remove(struct device *_dev)
294 struct xenbus_device *dev = to_xenbus_device(_dev);
295 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
297 DPRINTK("%s", dev->nodename);
299 free_otherend_watch(dev);
300 free_otherend_details(dev);
305 xenbus_switch_state(dev, XenbusStateClosed);
309 static void xenbus_dev_shutdown(struct device *_dev)
311 struct xenbus_device *dev = to_xenbus_device(_dev);
312 unsigned long timeout = 5*HZ;
314 DPRINTK("%s", dev->nodename);
316 get_device(&dev->dev);
317 if (dev->state != XenbusStateConnected) {
318 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
319 dev->nodename, xenbus_strstate(dev->state));
322 xenbus_switch_state(dev, XenbusStateClosing);
323 timeout = wait_for_completion_timeout(&dev->down, timeout);
325 printk(KERN_INFO "%s: %s timeout closing device\n",
326 __func__, dev->nodename);
328 put_device(&dev->dev);
331 int xenbus_register_driver_common(struct xenbus_driver *drv,
332 struct xen_bus_type *bus,
333 struct module *owner,
334 const char *mod_name)
336 drv->driver.name = drv->name;
337 drv->driver.bus = &bus->bus;
338 drv->driver.owner = owner;
339 drv->driver.mod_name = mod_name;
341 return driver_register(&drv->driver);
344 int __xenbus_register_frontend(struct xenbus_driver *drv,
345 struct module *owner, const char *mod_name)
349 drv->read_otherend_details = read_backend_details;
351 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
356 /* If this driver is loaded as a module wait for devices to attach. */
357 wait_for_devices(drv);
361 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
363 void xenbus_unregister_driver(struct xenbus_driver *drv)
365 driver_unregister(&drv->driver);
367 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
371 struct xenbus_device *dev;
372 const char *nodename;
375 static int cmp_dev(struct device *dev, void *data)
377 struct xenbus_device *xendev = to_xenbus_device(dev);
378 struct xb_find_info *info = data;
380 if (!strcmp(xendev->nodename, info->nodename)) {
388 struct xenbus_device *xenbus_device_find(const char *nodename,
389 struct bus_type *bus)
391 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
393 bus_for_each_dev(bus, NULL, &info, cmp_dev);
397 static int cleanup_dev(struct device *dev, void *data)
399 struct xenbus_device *xendev = to_xenbus_device(dev);
400 struct xb_find_info *info = data;
401 int len = strlen(info->nodename);
403 DPRINTK("%s", info->nodename);
405 /* Match the info->nodename path, or any subdirectory of that path. */
406 if (strncmp(xendev->nodename, info->nodename, len))
409 /* If the node name is longer, ensure it really is a subdirectory. */
410 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
418 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
420 struct xb_find_info info = { .nodename = path };
424 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
426 device_unregister(&info.dev->dev);
427 put_device(&info.dev->dev);
432 static void xenbus_dev_release(struct device *dev)
435 kfree(to_xenbus_device(dev));
438 static ssize_t xendev_show_nodename(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
443 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
445 static ssize_t xendev_show_devtype(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
450 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
452 static ssize_t xendev_show_modalias(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
457 DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
459 int xenbus_probe_node(struct xen_bus_type *bus,
461 const char *nodename)
464 struct xenbus_device *xendev;
468 enum xenbus_state state = xenbus_read_driver_state(nodename);
470 if (state != XenbusStateInitialising) {
471 /* Device is not new, so ignore it. This can happen if a
472 device is going away after switching to Closed. */
476 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
477 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
481 xendev->state = XenbusStateInitialising;
483 /* Copy the strings into the extra space. */
485 tmpstring = (char *)(xendev + 1);
486 strcpy(tmpstring, nodename);
487 xendev->nodename = tmpstring;
489 tmpstring += strlen(tmpstring) + 1;
490 strcpy(tmpstring, type);
491 xendev->devicetype = tmpstring;
492 init_completion(&xendev->down);
494 xendev->dev.bus = &bus->bus;
495 xendev->dev.release = xenbus_dev_release;
497 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
501 /* Register with generic device framework. */
502 err = device_register(&xendev->dev);
506 err = device_create_file(&xendev->dev, &dev_attr_nodename);
508 goto fail_unregister;
510 err = device_create_file(&xendev->dev, &dev_attr_devtype);
512 goto fail_remove_nodename;
514 err = device_create_file(&xendev->dev, &dev_attr_modalias);
516 goto fail_remove_devtype;
520 device_remove_file(&xendev->dev, &dev_attr_devtype);
521 fail_remove_nodename:
522 device_remove_file(&xendev->dev, &dev_attr_nodename);
524 device_unregister(&xendev->dev);
530 /* device/<typename>/<name> */
531 static int xenbus_probe_frontend(const char *type, const char *name)
536 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
537 xenbus_frontend.root, type, name);
541 DPRINTK("%s", nodename);
543 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
548 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
552 unsigned int dir_n = 0;
555 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
559 for (i = 0; i < dir_n; i++) {
560 err = bus->probe(type, dir[i]);
568 int xenbus_probe_devices(struct xen_bus_type *bus)
572 unsigned int i, dir_n;
574 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
578 for (i = 0; i < dir_n; i++) {
579 err = xenbus_probe_device_type(bus, dir[i]);
587 static unsigned int char_count(const char *str, char c)
589 unsigned int i, ret = 0;
591 for (i = 0; str[i]; i++)
597 static int strsep_len(const char *str, char c, unsigned int len)
601 for (i = 0; str[i]; i++)
607 return (len == 0) ? i : -ERANGE;
610 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
613 struct xenbus_device *dev;
614 char type[BUS_ID_SIZE];
615 const char *p, *root;
617 if (char_count(node, '/') < 2)
620 exists = xenbus_exists(XBT_NIL, node, "");
622 xenbus_cleanup_devices(node, &bus->bus);
626 /* backend/<type>/... or device/<type>/... */
627 p = strchr(node, '/') + 1;
628 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
629 type[BUS_ID_SIZE-1] = '\0';
631 rootlen = strsep_len(node, '/', bus->levels);
634 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
638 dev = xenbus_device_find(root, &bus->bus);
640 xenbus_probe_node(bus, type, root);
642 put_device(&dev->dev);
647 static void frontend_changed(struct xenbus_watch *watch,
648 const char **vec, unsigned int len)
652 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
655 /* We watch for devices appearing and vanishing. */
656 static struct xenbus_watch fe_watch = {
658 .callback = frontend_changed,
661 static int suspend_dev(struct device *dev, void *data)
664 struct xenbus_driver *drv;
665 struct xenbus_device *xdev;
669 if (dev->driver == NULL)
671 drv = to_xenbus_driver(dev->driver);
672 xdev = container_of(dev, struct xenbus_device, dev);
674 err = drv->suspend(xdev);
677 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
681 static int suspend_cancel_dev(struct device *dev, void *data)
684 struct xenbus_driver *drv;
685 struct xenbus_device *xdev;
689 if (dev->driver == NULL)
691 drv = to_xenbus_driver(dev->driver);
692 xdev = container_of(dev, struct xenbus_device, dev);
693 if (drv->suspend_cancel)
694 err = drv->suspend_cancel(xdev);
697 "xenbus: suspend_cancel %s failed: %i\n",
702 static int resume_dev(struct device *dev, void *data)
705 struct xenbus_driver *drv;
706 struct xenbus_device *xdev;
710 if (dev->driver == NULL)
713 drv = to_xenbus_driver(dev->driver);
714 xdev = container_of(dev, struct xenbus_device, dev);
716 err = talk_to_otherend(xdev);
719 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
724 xdev->state = XenbusStateInitialising;
727 err = drv->resume(xdev);
730 "xenbus: resume %s failed: %i\n",
736 err = watch_otherend(xdev);
739 "xenbus_probe: resume (watch_otherend) %s failed: "
740 "%d.\n", dev->bus_id, err);
747 void xenbus_suspend(void)
751 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
752 xenbus_backend_suspend(suspend_dev);
755 EXPORT_SYMBOL_GPL(xenbus_suspend);
757 void xenbus_resume(void)
761 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
762 xenbus_backend_resume(resume_dev);
764 EXPORT_SYMBOL_GPL(xenbus_resume);
766 void xenbus_suspend_cancel(void)
769 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
770 xenbus_backend_resume(suspend_cancel_dev);
772 EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
774 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
775 int xenstored_ready = 0;
778 int register_xenstore_notifier(struct notifier_block *nb)
782 if (xenstored_ready > 0)
783 ret = nb->notifier_call(nb, 0, NULL);
785 blocking_notifier_chain_register(&xenstore_chain, nb);
789 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
791 void unregister_xenstore_notifier(struct notifier_block *nb)
793 blocking_notifier_chain_unregister(&xenstore_chain, nb);
795 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
797 void xenbus_probe(struct work_struct *unused)
799 BUG_ON((xenstored_ready <= 0));
801 /* Enumerate devices in xenstore and watch for changes. */
802 xenbus_probe_devices(&xenbus_frontend);
803 register_xenbus_watch(&fe_watch);
804 xenbus_backend_probe_and_watch();
806 /* Notify others that xenstore is up */
807 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
810 static int __init xenbus_probe_init(void)
820 /* Register ourselves with the kernel bus subsystem */
821 err = bus_register(&xenbus_frontend.bus);
825 err = xenbus_backend_bus_register();
827 goto out_unreg_front;
830 * Domain0 doesn't have a store_evtchn or store_mfn yet.
832 if (xen_initial_domain()) {
833 /* dom0 not yet supported */
836 xen_store_evtchn = xen_start_info->store_evtchn;
837 xen_store_mfn = xen_start_info->store_mfn;
839 xen_store_interface = mfn_to_virt(xen_store_mfn);
841 /* Initialize the interface to xenstore. */
845 "XENBUS: Error initializing xenstore comms: %i\n", err);
849 if (!xen_initial_domain())
855 xenbus_backend_bus_unregister();
858 bus_unregister(&xenbus_frontend.bus);
864 postcore_initcall(xenbus_probe_init);
866 MODULE_LICENSE("GPL");
868 static int is_disconnected_device(struct device *dev, void *data)
870 struct xenbus_device *xendev = to_xenbus_device(dev);
871 struct device_driver *drv = data;
872 struct xenbus_driver *xendrv;
875 * A device with no driver will never connect. We care only about
876 * devices which should currently be in the process of connecting.
881 /* Is this search limited to a particular driver? */
882 if (drv && (dev->driver != drv))
885 xendrv = to_xenbus_driver(dev->driver);
886 return (xendev->state != XenbusStateConnected ||
887 (xendrv->is_ready && !xendrv->is_ready(xendev)));
890 static int exists_disconnected_device(struct device_driver *drv)
892 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
893 is_disconnected_device);
896 static int print_device_status(struct device *dev, void *data)
898 struct xenbus_device *xendev = to_xenbus_device(dev);
899 struct device_driver *drv = data;
901 /* Is this operation limited to a particular driver? */
902 if (drv && (dev->driver != drv))
906 /* Information only: is this too noisy? */
907 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
909 } else if (xendev->state != XenbusStateConnected) {
910 printk(KERN_WARNING "XENBUS: Timeout connecting "
911 "to device: %s (state %d)\n",
912 xendev->nodename, xendev->state);
918 /* We only wait for device setup after most initcalls have run. */
919 static int ready_to_wait_for_devices;
922 * On a 10 second timeout, wait for all devices currently configured. We need
923 * to do this to guarantee that the filesystems and / or network devices
924 * needed for boot are available, before we can allow the boot to proceed.
926 * This needs to be on a late_initcall, to happen after the frontend device
927 * drivers have been initialised, but before the root fs is mounted.
929 * A possible improvement here would be to have the tools add a per-device
930 * flag to the store entry, indicating whether it is needed at boot time.
931 * This would allow people who knew what they were doing to accelerate their
932 * boot slightly, but of course needs tools or manual intervention to set up
933 * those flags correctly.
935 static void wait_for_devices(struct xenbus_driver *xendrv)
937 unsigned long timeout = jiffies + 10*HZ;
938 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
940 if (!ready_to_wait_for_devices || !xen_domain())
943 while (exists_disconnected_device(drv)) {
944 if (time_after(jiffies, timeout))
946 schedule_timeout_interruptible(HZ/10);
949 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
950 print_device_status);
954 static int __init boot_wait_for_devices(void)
956 ready_to_wait_for_devices = 1;
957 wait_for_devices(NULL);
961 late_initcall(boot_wait_for_devices);