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/proc_fs.h>
44 #include <linux/notifier.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
50 #include <asm/pgtable.h>
51 #include <asm/xen/hypervisor.h>
52 #include <xen/xenbus.h>
53 #include <xen/events.h>
56 #include "xenbus_comms.h"
57 #include "xenbus_probe.h"
61 EXPORT_SYMBOL(xen_store_evtchn);
63 struct xenstore_domain_interface *xen_store_interface;
64 static unsigned long xen_store_mfn;
66 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
68 static void wait_for_devices(struct xenbus_driver *xendrv);
70 static int xenbus_probe_frontend(const char *type, const char *name);
72 static void xenbus_dev_shutdown(struct device *_dev);
74 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
75 static int xenbus_dev_resume(struct device *dev);
77 /* If something in array of ids matches this device, return it. */
78 static const struct xenbus_device_id *
79 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
81 for (; *arr->devicetype != '\0'; arr++) {
82 if (!strcmp(arr->devicetype, dev->devicetype))
88 int xenbus_match(struct device *_dev, struct device_driver *_drv)
90 struct xenbus_driver *drv = to_xenbus_driver(_drv);
95 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
98 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
100 struct xenbus_device *dev = to_xenbus_device(_dev);
102 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
108 /* device/<type>/<id> => <type>-<id> */
109 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
111 nodename = strchr(nodename, '/');
112 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
113 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
117 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
118 if (!strchr(bus_id, '/')) {
119 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
122 *strchr(bus_id, '/') = '-';
127 static void free_otherend_details(struct xenbus_device *dev)
129 kfree(dev->otherend);
130 dev->otherend = NULL;
134 static void free_otherend_watch(struct xenbus_device *dev)
136 if (dev->otherend_watch.node) {
137 unregister_xenbus_watch(&dev->otherend_watch);
138 kfree(dev->otherend_watch.node);
139 dev->otherend_watch.node = NULL;
144 int read_otherend_details(struct xenbus_device *xendev,
145 char *id_node, char *path_node)
147 int err = xenbus_gather(XBT_NIL, xendev->nodename,
148 id_node, "%i", &xendev->otherend_id,
149 path_node, NULL, &xendev->otherend,
152 xenbus_dev_fatal(xendev, err,
153 "reading other end details from %s",
157 if (strlen(xendev->otherend) == 0 ||
158 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
159 xenbus_dev_fatal(xendev, -ENOENT,
160 "unable to read other end from %s. "
161 "missing or inaccessible.",
163 free_otherend_details(xendev);
171 static int read_backend_details(struct xenbus_device *xendev)
173 return read_otherend_details(xendev, "backend-id", "backend");
176 static struct device_attribute xenbus_dev_attrs[] = {
180 /* Bus type for frontend drivers. */
181 static struct xen_bus_type xenbus_frontend = {
183 .levels = 2, /* device/type/<id> */
184 .get_bus_id = frontend_bus_id,
185 .probe = xenbus_probe_frontend,
188 .match = xenbus_match,
189 .uevent = xenbus_uevent,
190 .probe = xenbus_dev_probe,
191 .remove = xenbus_dev_remove,
192 .shutdown = xenbus_dev_shutdown,
193 .dev_attrs = xenbus_dev_attrs,
195 .suspend = xenbus_dev_suspend,
196 .resume = xenbus_dev_resume,
200 static void otherend_changed(struct xenbus_watch *watch,
201 const char **vec, unsigned int len)
203 struct xenbus_device *dev =
204 container_of(watch, struct xenbus_device, otherend_watch);
205 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
206 enum xenbus_state state;
208 /* Protect us against watches firing on old details when the otherend
209 details change, say immediately after a resume. */
210 if (!dev->otherend ||
211 strncmp(dev->otherend, vec[XS_WATCH_PATH],
212 strlen(dev->otherend))) {
213 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
218 state = xenbus_read_driver_state(dev->otherend);
220 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
221 state, xenbus_strstate(state), dev->otherend_watch.node,
225 * Ignore xenbus transitions during shutdown. This prevents us doing
226 * work that can fail e.g., when the rootfs is gone.
228 if (system_state > SYSTEM_RUNNING) {
229 struct xen_bus_type *bus = bus;
230 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
231 /* If we're frontend, drive the state machine to Closed. */
232 /* This should cause the backend to release our resources. */
233 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
234 xenbus_frontend_closed(dev);
238 if (drv->otherend_changed)
239 drv->otherend_changed(dev, state);
243 static int talk_to_otherend(struct xenbus_device *dev)
245 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
247 free_otherend_watch(dev);
248 free_otherend_details(dev);
250 return drv->read_otherend_details(dev);
254 static int watch_otherend(struct xenbus_device *dev)
256 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
257 "%s/%s", dev->otherend, "state");
261 int xenbus_dev_probe(struct device *_dev)
263 struct xenbus_device *dev = to_xenbus_device(_dev);
264 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
265 const struct xenbus_device_id *id;
268 DPRINTK("%s", dev->nodename);
275 id = match_device(drv->ids, dev);
281 err = talk_to_otherend(dev);
283 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
288 err = drv->probe(dev, id);
292 err = watch_otherend(dev);
294 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
301 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
302 xenbus_switch_state(dev, XenbusStateClosed);
306 int xenbus_dev_remove(struct device *_dev)
308 struct xenbus_device *dev = to_xenbus_device(_dev);
309 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
311 DPRINTK("%s", dev->nodename);
313 free_otherend_watch(dev);
314 free_otherend_details(dev);
319 xenbus_switch_state(dev, XenbusStateClosed);
323 static void xenbus_dev_shutdown(struct device *_dev)
325 struct xenbus_device *dev = to_xenbus_device(_dev);
326 unsigned long timeout = 5*HZ;
328 DPRINTK("%s", dev->nodename);
330 get_device(&dev->dev);
331 if (dev->state != XenbusStateConnected) {
332 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
333 dev->nodename, xenbus_strstate(dev->state));
336 xenbus_switch_state(dev, XenbusStateClosing);
337 timeout = wait_for_completion_timeout(&dev->down, timeout);
339 printk(KERN_INFO "%s: %s timeout closing device\n",
340 __func__, dev->nodename);
342 put_device(&dev->dev);
345 int xenbus_register_driver_common(struct xenbus_driver *drv,
346 struct xen_bus_type *bus,
347 struct module *owner,
348 const char *mod_name)
350 drv->driver.name = drv->name;
351 drv->driver.bus = &bus->bus;
352 drv->driver.owner = owner;
353 drv->driver.mod_name = mod_name;
355 return driver_register(&drv->driver);
358 int __xenbus_register_frontend(struct xenbus_driver *drv,
359 struct module *owner, const char *mod_name)
363 drv->read_otherend_details = read_backend_details;
365 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
370 /* If this driver is loaded as a module wait for devices to attach. */
371 wait_for_devices(drv);
375 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
377 void xenbus_unregister_driver(struct xenbus_driver *drv)
379 driver_unregister(&drv->driver);
381 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
385 struct xenbus_device *dev;
386 const char *nodename;
389 static int cmp_dev(struct device *dev, void *data)
391 struct xenbus_device *xendev = to_xenbus_device(dev);
392 struct xb_find_info *info = data;
394 if (!strcmp(xendev->nodename, info->nodename)) {
402 struct xenbus_device *xenbus_device_find(const char *nodename,
403 struct bus_type *bus)
405 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
407 bus_for_each_dev(bus, NULL, &info, cmp_dev);
411 static int cleanup_dev(struct device *dev, void *data)
413 struct xenbus_device *xendev = to_xenbus_device(dev);
414 struct xb_find_info *info = data;
415 int len = strlen(info->nodename);
417 DPRINTK("%s", info->nodename);
419 /* Match the info->nodename path, or any subdirectory of that path. */
420 if (strncmp(xendev->nodename, info->nodename, len))
423 /* If the node name is longer, ensure it really is a subdirectory. */
424 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
432 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
434 struct xb_find_info info = { .nodename = path };
438 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
440 device_unregister(&info.dev->dev);
441 put_device(&info.dev->dev);
446 static void xenbus_dev_release(struct device *dev)
449 kfree(to_xenbus_device(dev));
452 static ssize_t xendev_show_nodename(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
457 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
459 static ssize_t xendev_show_devtype(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
464 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
466 static ssize_t xendev_show_modalias(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
471 DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
473 int xenbus_probe_node(struct xen_bus_type *bus,
475 const char *nodename)
477 char devname[XEN_BUS_ID_SIZE];
479 struct xenbus_device *xendev;
483 enum xenbus_state state = xenbus_read_driver_state(nodename);
485 if (state != XenbusStateInitialising) {
486 /* Device is not new, so ignore it. This can happen if a
487 device is going away after switching to Closed. */
491 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
492 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
496 xendev->state = XenbusStateInitialising;
498 /* Copy the strings into the extra space. */
500 tmpstring = (char *)(xendev + 1);
501 strcpy(tmpstring, nodename);
502 xendev->nodename = tmpstring;
504 tmpstring += strlen(tmpstring) + 1;
505 strcpy(tmpstring, type);
506 xendev->devicetype = tmpstring;
507 init_completion(&xendev->down);
509 xendev->dev.bus = &bus->bus;
510 xendev->dev.release = xenbus_dev_release;
512 err = bus->get_bus_id(devname, xendev->nodename);
516 dev_set_name(&xendev->dev, devname);
518 /* Register with generic device framework. */
519 err = device_register(&xendev->dev);
523 err = device_create_file(&xendev->dev, &dev_attr_nodename);
525 goto fail_unregister;
527 err = device_create_file(&xendev->dev, &dev_attr_devtype);
529 goto fail_remove_nodename;
531 err = device_create_file(&xendev->dev, &dev_attr_modalias);
533 goto fail_remove_devtype;
537 device_remove_file(&xendev->dev, &dev_attr_devtype);
538 fail_remove_nodename:
539 device_remove_file(&xendev->dev, &dev_attr_nodename);
541 device_unregister(&xendev->dev);
547 /* device/<typename>/<name> */
548 static int xenbus_probe_frontend(const char *type, const char *name)
553 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
554 xenbus_frontend.root, type, name);
558 DPRINTK("%s", nodename);
560 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
565 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
569 unsigned int dir_n = 0;
572 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
576 for (i = 0; i < dir_n; i++) {
577 err = bus->probe(type, dir[i]);
585 int xenbus_probe_devices(struct xen_bus_type *bus)
589 unsigned int i, dir_n;
591 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
595 for (i = 0; i < dir_n; i++) {
596 err = xenbus_probe_device_type(bus, dir[i]);
604 static unsigned int char_count(const char *str, char c)
606 unsigned int i, ret = 0;
608 for (i = 0; str[i]; i++)
614 static int strsep_len(const char *str, char c, unsigned int len)
618 for (i = 0; str[i]; i++)
624 return (len == 0) ? i : -ERANGE;
627 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
630 struct xenbus_device *dev;
631 char type[XEN_BUS_ID_SIZE];
632 const char *p, *root;
634 if (char_count(node, '/') < 2)
637 exists = xenbus_exists(XBT_NIL, node, "");
639 xenbus_cleanup_devices(node, &bus->bus);
643 /* backend/<type>/... or device/<type>/... */
644 p = strchr(node, '/') + 1;
645 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
646 type[XEN_BUS_ID_SIZE-1] = '\0';
648 rootlen = strsep_len(node, '/', bus->levels);
651 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
655 dev = xenbus_device_find(root, &bus->bus);
657 xenbus_probe_node(bus, type, root);
659 put_device(&dev->dev);
663 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
665 static void frontend_changed(struct xenbus_watch *watch,
666 const char **vec, unsigned int len)
670 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
673 /* We watch for devices appearing and vanishing. */
674 static struct xenbus_watch fe_watch = {
676 .callback = frontend_changed,
679 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
682 struct xenbus_driver *drv;
683 struct xenbus_device *xdev;
687 if (dev->driver == NULL)
689 drv = to_xenbus_driver(dev->driver);
690 xdev = container_of(dev, struct xenbus_device, dev);
692 err = drv->suspend(xdev, state);
695 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
699 static int xenbus_dev_resume(struct device *dev)
702 struct xenbus_driver *drv;
703 struct xenbus_device *xdev;
707 if (dev->driver == NULL)
710 drv = to_xenbus_driver(dev->driver);
711 xdev = container_of(dev, struct xenbus_device, dev);
713 err = talk_to_otherend(xdev);
716 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
721 xdev->state = XenbusStateInitialising;
724 err = drv->resume(xdev);
727 "xenbus: resume %s failed: %i\n",
733 err = watch_otherend(xdev);
736 "xenbus_probe: resume (watch_otherend) %s failed: "
737 "%d.\n", dev_name(dev), err);
744 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
745 int xenstored_ready = 0;
748 int register_xenstore_notifier(struct notifier_block *nb)
752 if (xenstored_ready > 0)
753 ret = nb->notifier_call(nb, 0, NULL);
755 blocking_notifier_chain_register(&xenstore_chain, nb);
759 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
761 void unregister_xenstore_notifier(struct notifier_block *nb)
763 blocking_notifier_chain_unregister(&xenstore_chain, nb);
765 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
767 void xenbus_probe(struct work_struct *unused)
769 BUG_ON((xenstored_ready <= 0));
771 /* Enumerate devices in xenstore and watch for changes. */
772 xenbus_probe_devices(&xenbus_frontend);
773 register_xenbus_watch(&fe_watch);
774 xenbus_backend_probe_and_watch();
776 /* Notify others that xenstore is up */
777 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
780 static int __init xenbus_probe_init(void)
790 /* Register ourselves with the kernel bus subsystem */
791 err = bus_register(&xenbus_frontend.bus);
795 err = xenbus_backend_bus_register();
797 goto out_unreg_front;
800 * Domain0 doesn't have a store_evtchn or store_mfn yet.
802 if (xen_initial_domain()) {
803 /* dom0 not yet supported */
806 xen_store_evtchn = xen_start_info->store_evtchn;
807 xen_store_mfn = xen_start_info->store_mfn;
809 xen_store_interface = mfn_to_virt(xen_store_mfn);
811 /* Initialize the interface to xenstore. */
815 "XENBUS: Error initializing xenstore comms: %i\n", err);
819 if (!xen_initial_domain())
822 #ifdef CONFIG_XEN_COMPAT_XENFS
824 * Create xenfs mountpoint in /proc for compatibility with
825 * utilities that expect to find "xenbus" under "/proc/xen".
827 proc_mkdir("xen", NULL);
833 xenbus_backend_bus_unregister();
836 bus_unregister(&xenbus_frontend.bus);
842 postcore_initcall(xenbus_probe_init);
844 MODULE_LICENSE("GPL");
846 static int is_disconnected_device(struct device *dev, void *data)
848 struct xenbus_device *xendev = to_xenbus_device(dev);
849 struct device_driver *drv = data;
850 struct xenbus_driver *xendrv;
853 * A device with no driver will never connect. We care only about
854 * devices which should currently be in the process of connecting.
859 /* Is this search limited to a particular driver? */
860 if (drv && (dev->driver != drv))
863 xendrv = to_xenbus_driver(dev->driver);
864 return (xendev->state != XenbusStateConnected ||
865 (xendrv->is_ready && !xendrv->is_ready(xendev)));
868 static int exists_disconnected_device(struct device_driver *drv)
870 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
871 is_disconnected_device);
874 static int print_device_status(struct device *dev, void *data)
876 struct xenbus_device *xendev = to_xenbus_device(dev);
877 struct device_driver *drv = data;
879 /* Is this operation limited to a particular driver? */
880 if (drv && (dev->driver != drv))
884 /* Information only: is this too noisy? */
885 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
887 } else if (xendev->state != XenbusStateConnected) {
888 printk(KERN_WARNING "XENBUS: Timeout connecting "
889 "to device: %s (state %d)\n",
890 xendev->nodename, xendev->state);
896 /* We only wait for device setup after most initcalls have run. */
897 static int ready_to_wait_for_devices;
900 * On a 10 second timeout, wait for all devices currently configured. We need
901 * to do this to guarantee that the filesystems and / or network devices
902 * needed for boot are available, before we can allow the boot to proceed.
904 * This needs to be on a late_initcall, to happen after the frontend device
905 * drivers have been initialised, but before the root fs is mounted.
907 * A possible improvement here would be to have the tools add a per-device
908 * flag to the store entry, indicating whether it is needed at boot time.
909 * This would allow people who knew what they were doing to accelerate their
910 * boot slightly, but of course needs tools or manual intervention to set up
911 * those flags correctly.
913 static void wait_for_devices(struct xenbus_driver *xendrv)
915 unsigned long timeout = jiffies + 10*HZ;
916 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
918 if (!ready_to_wait_for_devices || !xen_domain())
921 while (exists_disconnected_device(drv)) {
922 if (time_after(jiffies, timeout))
924 schedule_timeout_interruptible(HZ/10);
927 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
928 print_device_status);
932 static int __init boot_wait_for_devices(void)
934 ready_to_wait_for_devices = 1;
935 wait_for_devices(NULL);
939 late_initcall(boot_wait_for_devices);