1 #define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
5 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/fcntl.h>
11 #include <linux/proc_fs.h>
12 #include <linux/notifier.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
18 #include <asm/pgtable.h>
19 #include <asm/xen/hypervisor.h>
20 #include <xen/xenbus.h>
21 #include <xen/events.h>
24 #include <xen/platform_pci.h>
26 #include "xenbus_comms.h"
27 #include "xenbus_probe.h"
30 /* device/<type>/<id> => <type>-<id> */
31 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
33 nodename = strchr(nodename, '/');
34 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
35 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
39 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
40 if (!strchr(bus_id, '/')) {
41 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
44 *strchr(bus_id, '/') = '-';
48 /* device/<typename>/<name> */
49 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
55 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
59 DPRINTK("%s", nodename);
61 err = xenbus_probe_node(bus, type, nodename);
66 static int xenbus_uevent_frontend(struct device *_dev,
67 struct kobj_uevent_env *env)
69 struct xenbus_device *dev = to_xenbus_device(_dev);
71 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
78 static void backend_changed(struct xenbus_watch *watch,
79 const char **vec, unsigned int len)
81 xenbus_otherend_changed(watch, vec, len, 1);
84 static struct device_attribute xenbus_frontend_dev_attrs[] = {
88 static const struct dev_pm_ops xenbus_pm_ops = {
89 .suspend = xenbus_dev_suspend,
90 .resume = xenbus_dev_resume,
91 .freeze = xenbus_dev_suspend,
92 .thaw = xenbus_dev_cancel,
93 .restore = xenbus_dev_resume,
96 static struct xen_bus_type xenbus_frontend = {
98 .levels = 2, /* device/type/<id> */
99 .get_bus_id = frontend_bus_id,
100 .probe = xenbus_probe_frontend,
101 .otherend_changed = backend_changed,
104 .match = xenbus_match,
105 .uevent = xenbus_uevent_frontend,
106 .probe = xenbus_dev_probe,
107 .remove = xenbus_dev_remove,
108 .shutdown = xenbus_dev_shutdown,
109 .dev_attrs = xenbus_frontend_dev_attrs,
111 .pm = &xenbus_pm_ops,
115 static void frontend_changed(struct xenbus_watch *watch,
116 const char **vec, unsigned int len)
120 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
124 /* We watch for devices appearing and vanishing. */
125 static struct xenbus_watch fe_watch = {
127 .callback = frontend_changed,
130 static int read_backend_details(struct xenbus_device *xendev)
132 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
135 static int is_device_connecting(struct device *dev, void *data)
137 struct xenbus_device *xendev = to_xenbus_device(dev);
138 struct device_driver *drv = data;
139 struct xenbus_driver *xendrv;
142 * A device with no driver will never connect. We care only about
143 * devices which should currently be in the process of connecting.
148 /* Is this search limited to a particular driver? */
149 if (drv && (dev->driver != drv))
152 xendrv = to_xenbus_driver(dev->driver);
153 return (xendev->state < XenbusStateConnected ||
154 (xendev->state == XenbusStateConnected &&
155 xendrv->is_ready && !xendrv->is_ready(xendev)));
158 static int exists_connecting_device(struct device_driver *drv)
160 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
161 is_device_connecting);
164 static int print_device_status(struct device *dev, void *data)
166 struct xenbus_device *xendev = to_xenbus_device(dev);
167 struct device_driver *drv = data;
169 /* Is this operation limited to a particular driver? */
170 if (drv && (dev->driver != drv))
174 /* Information only: is this too noisy? */
175 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
177 } else if (xendev->state < XenbusStateConnected) {
178 enum xenbus_state rstate = XenbusStateUnknown;
179 if (xendev->otherend)
180 rstate = xenbus_read_driver_state(xendev->otherend);
181 printk(KERN_WARNING "XENBUS: Timeout connecting "
182 "to device: %s (local state %d, remote state %d)\n",
183 xendev->nodename, xendev->state, rstate);
189 /* We only wait for device setup after most initcalls have run. */
190 static int ready_to_wait_for_devices;
193 * On a 5-minute timeout, wait for all devices currently configured. We need
194 * to do this to guarantee that the filesystems and / or network devices
195 * needed for boot are available, before we can allow the boot to proceed.
197 * This needs to be on a late_initcall, to happen after the frontend device
198 * drivers have been initialised, but before the root fs is mounted.
200 * A possible improvement here would be to have the tools add a per-device
201 * flag to the store entry, indicating whether it is needed at boot time.
202 * This would allow people who knew what they were doing to accelerate their
203 * boot slightly, but of course needs tools or manual intervention to set up
204 * those flags correctly.
206 static void wait_for_devices(struct xenbus_driver *xendrv)
208 unsigned long start = jiffies;
209 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
210 unsigned int seconds_waited = 0;
212 if (!ready_to_wait_for_devices || !xen_domain())
215 while (exists_connecting_device(drv)) {
216 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
218 printk(KERN_WARNING "XENBUS: Waiting for "
219 "devices to initialise: ");
221 printk("%us...", 300 - seconds_waited);
222 if (seconds_waited == 300)
226 schedule_timeout_interruptible(HZ/10);
232 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
233 print_device_status);
236 int __xenbus_register_frontend(struct xenbus_driver *drv,
237 struct module *owner, const char *mod_name)
241 drv->read_otherend_details = read_backend_details;
243 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
248 /* If this driver is loaded as a module wait for devices to attach. */
249 wait_for_devices(drv);
253 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
255 static int frontend_probe_and_watch(struct notifier_block *notifier,
259 /* Enumerate devices in xenstore and watch for changes. */
260 xenbus_probe_devices(&xenbus_frontend);
261 register_xenbus_watch(&fe_watch);
267 static int __init xenbus_probe_frontend_init(void)
269 static struct notifier_block xenstore_notifier = {
270 .notifier_call = frontend_probe_and_watch
276 /* Register ourselves with the kernel bus subsystem */
277 err = bus_register(&xenbus_frontend.bus);
281 register_xenstore_notifier(&xenstore_notifier);
285 subsys_initcall(xenbus_probe_frontend_init);
288 static int __init boot_wait_for_devices(void)
290 if (xen_hvm_domain() && !xen_platform_pci_unplug)
293 ready_to_wait_for_devices = 1;
294 wait_for_devices(NULL);
298 late_initcall(boot_wait_for_devices);
301 MODULE_LICENSE("GPL");