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>
54 #include <xen/xenbus.h>
55 #include <xen/events.h>
58 #include "xenbus_comms.h"
59 #include "xenbus_probe.h"
63 EXPORT_SYMBOL(xen_store_evtchn);
65 struct xenstore_domain_interface *xen_store_interface;
66 static unsigned long xen_store_mfn;
68 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
70 static void wait_for_devices(struct xenbus_driver *xendrv);
72 static int xenbus_probe_frontend(const char *type, const char *name);
74 static void xenbus_dev_shutdown(struct device *_dev);
76 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
77 static int xenbus_dev_resume(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 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;
100 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
102 struct xenbus_device *dev = to_xenbus_device(_dev);
104 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
110 /* device/<type>/<id> => <type>-<id> */
111 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
113 nodename = strchr(nodename, '/');
114 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
115 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
119 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
120 if (!strchr(bus_id, '/')) {
121 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
124 *strchr(bus_id, '/') = '-';
129 static void free_otherend_details(struct xenbus_device *dev)
131 kfree(dev->otherend);
132 dev->otherend = NULL;
136 static void free_otherend_watch(struct xenbus_device *dev)
138 if (dev->otherend_watch.node) {
139 unregister_xenbus_watch(&dev->otherend_watch);
140 kfree(dev->otherend_watch.node);
141 dev->otherend_watch.node = NULL;
146 int read_otherend_details(struct xenbus_device *xendev,
147 char *id_node, char *path_node)
149 int err = xenbus_gather(XBT_NIL, xendev->nodename,
150 id_node, "%i", &xendev->otherend_id,
151 path_node, NULL, &xendev->otherend,
154 xenbus_dev_fatal(xendev, err,
155 "reading other end details from %s",
159 if (strlen(xendev->otherend) == 0 ||
160 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
161 xenbus_dev_fatal(xendev, -ENOENT,
162 "unable to read other end from %s. "
163 "missing or inaccessible.",
165 free_otherend_details(xendev);
173 static int read_backend_details(struct xenbus_device *xendev)
175 return read_otherend_details(xendev, "backend-id", "backend");
178 static struct device_attribute xenbus_dev_attrs[] = {
182 /* Bus type for frontend drivers. */
183 static struct xen_bus_type xenbus_frontend = {
185 .levels = 2, /* device/type/<id> */
186 .get_bus_id = frontend_bus_id,
187 .probe = xenbus_probe_frontend,
190 .match = xenbus_match,
191 .uevent = xenbus_uevent,
192 .probe = xenbus_dev_probe,
193 .remove = xenbus_dev_remove,
194 .shutdown = xenbus_dev_shutdown,
195 .dev_attrs = xenbus_dev_attrs,
197 .suspend = xenbus_dev_suspend,
198 .resume = xenbus_dev_resume,
202 static void otherend_changed(struct xenbus_watch *watch,
203 const char **vec, unsigned int len)
205 struct xenbus_device *dev =
206 container_of(watch, struct xenbus_device, otherend_watch);
207 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
208 enum xenbus_state state;
210 /* Protect us against watches firing on old details when the otherend
211 details change, say immediately after a resume. */
212 if (!dev->otherend ||
213 strncmp(dev->otherend, vec[XS_WATCH_PATH],
214 strlen(dev->otherend))) {
215 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
220 state = xenbus_read_driver_state(dev->otherend);
222 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
223 state, xenbus_strstate(state), dev->otherend_watch.node,
227 * Ignore xenbus transitions during shutdown. This prevents us doing
228 * work that can fail e.g., when the rootfs is gone.
230 if (system_state > SYSTEM_RUNNING) {
231 struct xen_bus_type *bus = bus;
232 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
233 /* If we're frontend, drive the state machine to Closed. */
234 /* This should cause the backend to release our resources. */
235 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
236 xenbus_frontend_closed(dev);
240 if (drv->otherend_changed)
241 drv->otherend_changed(dev, state);
245 static int talk_to_otherend(struct xenbus_device *dev)
247 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
249 free_otherend_watch(dev);
250 free_otherend_details(dev);
252 return drv->read_otherend_details(dev);
256 static int watch_otherend(struct xenbus_device *dev)
258 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
259 "%s/%s", dev->otherend, "state");
263 int xenbus_dev_probe(struct device *_dev)
265 struct xenbus_device *dev = to_xenbus_device(_dev);
266 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
267 const struct xenbus_device_id *id;
270 DPRINTK("%s", dev->nodename);
277 id = match_device(drv->ids, dev);
283 err = talk_to_otherend(dev);
285 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
290 err = drv->probe(dev, id);
294 err = watch_otherend(dev);
296 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
303 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
304 xenbus_switch_state(dev, XenbusStateClosed);
308 int xenbus_dev_remove(struct device *_dev)
310 struct xenbus_device *dev = to_xenbus_device(_dev);
311 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
313 DPRINTK("%s", dev->nodename);
315 free_otherend_watch(dev);
316 free_otherend_details(dev);
321 xenbus_switch_state(dev, XenbusStateClosed);
325 static void xenbus_dev_shutdown(struct device *_dev)
327 struct xenbus_device *dev = to_xenbus_device(_dev);
328 unsigned long timeout = 5*HZ;
330 DPRINTK("%s", dev->nodename);
332 get_device(&dev->dev);
333 if (dev->state != XenbusStateConnected) {
334 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
335 dev->nodename, xenbus_strstate(dev->state));
338 xenbus_switch_state(dev, XenbusStateClosing);
339 timeout = wait_for_completion_timeout(&dev->down, timeout);
341 printk(KERN_INFO "%s: %s timeout closing device\n",
342 __func__, dev->nodename);
344 put_device(&dev->dev);
347 int xenbus_register_driver_common(struct xenbus_driver *drv,
348 struct xen_bus_type *bus,
349 struct module *owner,
350 const char *mod_name)
352 drv->driver.name = drv->name;
353 drv->driver.bus = &bus->bus;
354 drv->driver.owner = owner;
355 drv->driver.mod_name = mod_name;
357 return driver_register(&drv->driver);
360 int __xenbus_register_frontend(struct xenbus_driver *drv,
361 struct module *owner, const char *mod_name)
365 drv->read_otherend_details = read_backend_details;
367 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
372 /* If this driver is loaded as a module wait for devices to attach. */
373 wait_for_devices(drv);
377 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
379 void xenbus_unregister_driver(struct xenbus_driver *drv)
381 driver_unregister(&drv->driver);
383 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
387 struct xenbus_device *dev;
388 const char *nodename;
391 static int cmp_dev(struct device *dev, void *data)
393 struct xenbus_device *xendev = to_xenbus_device(dev);
394 struct xb_find_info *info = data;
396 if (!strcmp(xendev->nodename, info->nodename)) {
404 struct xenbus_device *xenbus_device_find(const char *nodename,
405 struct bus_type *bus)
407 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
409 bus_for_each_dev(bus, NULL, &info, cmp_dev);
413 static int cleanup_dev(struct device *dev, void *data)
415 struct xenbus_device *xendev = to_xenbus_device(dev);
416 struct xb_find_info *info = data;
417 int len = strlen(info->nodename);
419 DPRINTK("%s", info->nodename);
421 /* Match the info->nodename path, or any subdirectory of that path. */
422 if (strncmp(xendev->nodename, info->nodename, len))
425 /* If the node name is longer, ensure it really is a subdirectory. */
426 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
434 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
436 struct xb_find_info info = { .nodename = path };
440 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
442 device_unregister(&info.dev->dev);
443 put_device(&info.dev->dev);
448 static void xenbus_dev_release(struct device *dev)
451 kfree(to_xenbus_device(dev));
454 static ssize_t xendev_show_nodename(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
459 static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
461 static ssize_t xendev_show_devtype(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
466 static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
468 static ssize_t xendev_show_modalias(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
473 static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
475 int xenbus_probe_node(struct xen_bus_type *bus,
477 const char *nodename)
479 char devname[XEN_BUS_ID_SIZE];
481 struct xenbus_device *xendev;
485 enum xenbus_state state = xenbus_read_driver_state(nodename);
487 if (state != XenbusStateInitialising) {
488 /* Device is not new, so ignore it. This can happen if a
489 device is going away after switching to Closed. */
493 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
494 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
498 xendev->state = XenbusStateInitialising;
500 /* Copy the strings into the extra space. */
502 tmpstring = (char *)(xendev + 1);
503 strcpy(tmpstring, nodename);
504 xendev->nodename = tmpstring;
506 tmpstring += strlen(tmpstring) + 1;
507 strcpy(tmpstring, type);
508 xendev->devicetype = tmpstring;
509 init_completion(&xendev->down);
511 xendev->dev.bus = &bus->bus;
512 xendev->dev.release = xenbus_dev_release;
514 err = bus->get_bus_id(devname, xendev->nodename);
518 dev_set_name(&xendev->dev, devname);
520 /* Register with generic device framework. */
521 err = device_register(&xendev->dev);
525 err = device_create_file(&xendev->dev, &dev_attr_nodename);
527 goto fail_unregister;
529 err = device_create_file(&xendev->dev, &dev_attr_devtype);
531 goto fail_remove_nodename;
533 err = device_create_file(&xendev->dev, &dev_attr_modalias);
535 goto fail_remove_devtype;
539 device_remove_file(&xendev->dev, &dev_attr_devtype);
540 fail_remove_nodename:
541 device_remove_file(&xendev->dev, &dev_attr_nodename);
543 device_unregister(&xendev->dev);
549 /* device/<typename>/<name> */
550 static int xenbus_probe_frontend(const char *type, const char *name)
555 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
556 xenbus_frontend.root, type, name);
560 DPRINTK("%s", nodename);
562 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
567 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
571 unsigned int dir_n = 0;
574 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
578 for (i = 0; i < dir_n; i++) {
579 err = bus->probe(type, dir[i]);
587 int xenbus_probe_devices(struct xen_bus_type *bus)
591 unsigned int i, dir_n;
593 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
597 for (i = 0; i < dir_n; i++) {
598 err = xenbus_probe_device_type(bus, dir[i]);
606 static unsigned int char_count(const char *str, char c)
608 unsigned int i, ret = 0;
610 for (i = 0; str[i]; i++)
616 static int strsep_len(const char *str, char c, unsigned int len)
620 for (i = 0; str[i]; i++)
626 return (len == 0) ? i : -ERANGE;
629 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
632 struct xenbus_device *dev;
633 char type[XEN_BUS_ID_SIZE];
634 const char *p, *root;
636 if (char_count(node, '/') < 2)
639 exists = xenbus_exists(XBT_NIL, node, "");
641 xenbus_cleanup_devices(node, &bus->bus);
645 /* backend/<type>/... or device/<type>/... */
646 p = strchr(node, '/') + 1;
647 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
648 type[XEN_BUS_ID_SIZE-1] = '\0';
650 rootlen = strsep_len(node, '/', bus->levels);
653 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
657 dev = xenbus_device_find(root, &bus->bus);
659 xenbus_probe_node(bus, type, root);
661 put_device(&dev->dev);
665 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
667 static void frontend_changed(struct xenbus_watch *watch,
668 const char **vec, unsigned int len)
672 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
675 /* We watch for devices appearing and vanishing. */
676 static struct xenbus_watch fe_watch = {
678 .callback = frontend_changed,
681 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
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);
694 err = drv->suspend(xdev, state);
697 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
701 static int xenbus_dev_resume(struct device *dev)
704 struct xenbus_driver *drv;
705 struct xenbus_device *xdev;
709 if (dev->driver == NULL)
712 drv = to_xenbus_driver(dev->driver);
713 xdev = container_of(dev, struct xenbus_device, dev);
715 err = talk_to_otherend(xdev);
718 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
723 xdev->state = XenbusStateInitialising;
726 err = drv->resume(xdev);
729 "xenbus: resume %s failed: %i\n",
735 err = watch_otherend(xdev);
738 "xenbus_probe: resume (watch_otherend) %s failed: "
739 "%d.\n", dev_name(dev), err);
746 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
747 int xenstored_ready = 0;
750 int register_xenstore_notifier(struct notifier_block *nb)
754 if (xenstored_ready > 0)
755 ret = nb->notifier_call(nb, 0, NULL);
757 blocking_notifier_chain_register(&xenstore_chain, nb);
761 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
763 void unregister_xenstore_notifier(struct notifier_block *nb)
765 blocking_notifier_chain_unregister(&xenstore_chain, nb);
767 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
769 void xenbus_probe(struct work_struct *unused)
771 BUG_ON((xenstored_ready <= 0));
773 /* Enumerate devices in xenstore and watch for changes. */
774 xenbus_probe_devices(&xenbus_frontend);
775 register_xenbus_watch(&fe_watch);
776 xenbus_backend_probe_and_watch();
778 /* Notify others that xenstore is up */
779 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
782 static int __init xenbus_probe_init(void)
792 /* Register ourselves with the kernel bus subsystem */
793 err = bus_register(&xenbus_frontend.bus);
797 err = xenbus_backend_bus_register();
799 goto out_unreg_front;
802 * Domain0 doesn't have a store_evtchn or store_mfn yet.
804 if (xen_initial_domain()) {
805 /* dom0 not yet supported */
808 xen_store_evtchn = xen_start_info->store_evtchn;
809 xen_store_mfn = xen_start_info->store_mfn;
811 xen_store_interface = mfn_to_virt(xen_store_mfn);
813 /* Initialize the interface to xenstore. */
817 "XENBUS: Error initializing xenstore comms: %i\n", err);
821 if (!xen_initial_domain())
824 #ifdef CONFIG_XEN_COMPAT_XENFS
826 * Create xenfs mountpoint in /proc for compatibility with
827 * utilities that expect to find "xenbus" under "/proc/xen".
829 proc_mkdir("xen", NULL);
835 xenbus_backend_bus_unregister();
838 bus_unregister(&xenbus_frontend.bus);
844 postcore_initcall(xenbus_probe_init);
846 MODULE_LICENSE("GPL");
848 static int is_device_connecting(struct device *dev, void *data)
850 struct xenbus_device *xendev = to_xenbus_device(dev);
851 struct device_driver *drv = data;
852 struct xenbus_driver *xendrv;
855 * A device with no driver will never connect. We care only about
856 * devices which should currently be in the process of connecting.
861 /* Is this search limited to a particular driver? */
862 if (drv && (dev->driver != drv))
865 xendrv = to_xenbus_driver(dev->driver);
866 return (xendev->state < XenbusStateConnected ||
867 (xendev->state == XenbusStateConnected &&
868 xendrv->is_ready && !xendrv->is_ready(xendev)));
871 static int exists_connecting_device(struct device_driver *drv)
873 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
874 is_device_connecting);
877 static int print_device_status(struct device *dev, void *data)
879 struct xenbus_device *xendev = to_xenbus_device(dev);
880 struct device_driver *drv = data;
882 /* Is this operation limited to a particular driver? */
883 if (drv && (dev->driver != drv))
887 /* Information only: is this too noisy? */
888 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
890 } else if (xendev->state < XenbusStateConnected) {
891 enum xenbus_state rstate = XenbusStateUnknown;
892 if (xendev->otherend)
893 rstate = xenbus_read_driver_state(xendev->otherend);
894 printk(KERN_WARNING "XENBUS: Timeout connecting "
895 "to device: %s (local state %d, remote state %d)\n",
896 xendev->nodename, xendev->state, rstate);
902 /* We only wait for device setup after most initcalls have run. */
903 static int ready_to_wait_for_devices;
906 * On a 5-minute timeout, wait for all devices currently configured. We need
907 * to do this to guarantee that the filesystems and / or network devices
908 * needed for boot are available, before we can allow the boot to proceed.
910 * This needs to be on a late_initcall, to happen after the frontend device
911 * drivers have been initialised, but before the root fs is mounted.
913 * A possible improvement here would be to have the tools add a per-device
914 * flag to the store entry, indicating whether it is needed at boot time.
915 * This would allow people who knew what they were doing to accelerate their
916 * boot slightly, but of course needs tools or manual intervention to set up
917 * those flags correctly.
919 static void wait_for_devices(struct xenbus_driver *xendrv)
921 unsigned long start = jiffies;
922 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
923 unsigned int seconds_waited = 0;
925 if (!ready_to_wait_for_devices || !xen_domain())
928 while (exists_connecting_device(drv)) {
929 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
931 printk(KERN_WARNING "XENBUS: Waiting for "
932 "devices to initialise: ");
934 printk("%us...", 300 - seconds_waited);
935 if (seconds_waited == 300)
939 schedule_timeout_interruptible(HZ/10);
945 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
946 print_device_status);
950 static int __init boot_wait_for_devices(void)
952 ready_to_wait_for_devices = 1;
953 wait_for_devices(NULL);
957 late_initcall(boot_wait_for_devices);