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>
16 #include <linux/module.h>
19 #include <asm/pgtable.h>
20 #include <asm/xen/hypervisor.h>
21 #include <xen/xenbus.h>
22 #include <xen/events.h>
25 #include <xen/platform_pci.h>
27 #include "xenbus_comms.h"
28 #include "xenbus_probe.h"
31 /* device/<type>/<id> => <type>-<id> */
32 static int frontend_bus_id(char bus_id
[XEN_BUS_ID_SIZE
], const char *nodename
)
34 nodename
= strchr(nodename
, '/');
35 if (!nodename
|| strlen(nodename
+ 1) >= XEN_BUS_ID_SIZE
) {
36 printk(KERN_WARNING
"XENBUS: bad frontend %s\n", nodename
);
40 strlcpy(bus_id
, nodename
+ 1, XEN_BUS_ID_SIZE
);
41 if (!strchr(bus_id
, '/')) {
42 printk(KERN_WARNING
"XENBUS: bus_id %s no slash\n", bus_id
);
45 *strchr(bus_id
, '/') = '-';
49 /* device/<typename>/<name> */
50 static int xenbus_probe_frontend(struct xen_bus_type
*bus
, const char *type
,
56 /* ignore console/0 */
57 if (!strncmp(type
, "console", 7) && !strncmp(name
, "0", 1)) {
58 DPRINTK("Ignoring buggy device entry console/0");
62 nodename
= kasprintf(GFP_KERNEL
, "%s/%s/%s", bus
->root
, type
, name
);
66 DPRINTK("%s", nodename
);
68 err
= xenbus_probe_node(bus
, type
, nodename
);
73 static int xenbus_uevent_frontend(struct device
*_dev
,
74 struct kobj_uevent_env
*env
)
76 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
78 if (add_uevent_var(env
, "MODALIAS=xen:%s", dev
->devicetype
))
85 static void backend_changed(struct xenbus_watch
*watch
,
86 const char **vec
, unsigned int len
)
88 xenbus_otherend_changed(watch
, vec
, len
, 1);
91 static const struct dev_pm_ops xenbus_pm_ops
= {
92 .suspend
= xenbus_dev_suspend
,
93 .resume
= xenbus_dev_resume
,
94 .freeze
= xenbus_dev_suspend
,
95 .thaw
= xenbus_dev_cancel
,
96 .restore
= xenbus_dev_resume
,
99 static struct xen_bus_type xenbus_frontend
= {
101 .levels
= 2, /* device/type/<id> */
102 .get_bus_id
= frontend_bus_id
,
103 .probe
= xenbus_probe_frontend
,
104 .otherend_changed
= backend_changed
,
107 .match
= xenbus_match
,
108 .uevent
= xenbus_uevent_frontend
,
109 .probe
= xenbus_dev_probe
,
110 .remove
= xenbus_dev_remove
,
111 .shutdown
= xenbus_dev_shutdown
,
112 .dev_attrs
= xenbus_dev_attrs
,
114 .pm
= &xenbus_pm_ops
,
118 static void frontend_changed(struct xenbus_watch
*watch
,
119 const char **vec
, unsigned int len
)
123 xenbus_dev_changed(vec
[XS_WATCH_PATH
], &xenbus_frontend
);
127 /* We watch for devices appearing and vanishing. */
128 static struct xenbus_watch fe_watch
= {
130 .callback
= frontend_changed
,
133 static int read_backend_details(struct xenbus_device
*xendev
)
135 return xenbus_read_otherend_details(xendev
, "backend-id", "backend");
138 static int is_device_connecting(struct device
*dev
, void *data
, bool ignore_nonessential
)
140 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
141 struct device_driver
*drv
= data
;
142 struct xenbus_driver
*xendrv
;
145 * A device with no driver will never connect. We care only about
146 * devices which should currently be in the process of connecting.
151 /* Is this search limited to a particular driver? */
152 if (drv
&& (dev
->driver
!= drv
))
155 if (ignore_nonessential
) {
156 /* With older QEMU, for PVonHVM guests the guest config files
157 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
158 * which is nonsensical as there is no PV FB (there can be
159 * a PVKB) running as HVM guest. */
161 if ((strncmp(xendev
->nodename
, "device/vkbd", 11) == 0))
164 if ((strncmp(xendev
->nodename
, "device/vfb", 10) == 0))
167 xendrv
= to_xenbus_driver(dev
->driver
);
168 return (xendev
->state
< XenbusStateConnected
||
169 (xendev
->state
== XenbusStateConnected
&&
170 xendrv
->is_ready
&& !xendrv
->is_ready(xendev
)));
172 static int essential_device_connecting(struct device
*dev
, void *data
)
174 return is_device_connecting(dev
, data
, true /* ignore PV[KBB+FB] */);
176 static int non_essential_device_connecting(struct device
*dev
, void *data
)
178 return is_device_connecting(dev
, data
, false);
181 static int exists_essential_connecting_device(struct device_driver
*drv
)
183 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
184 essential_device_connecting
);
186 static int exists_non_essential_connecting_device(struct device_driver
*drv
)
188 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
189 non_essential_device_connecting
);
192 static int print_device_status(struct device
*dev
, void *data
)
194 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
195 struct device_driver
*drv
= data
;
197 /* Is this operation limited to a particular driver? */
198 if (drv
&& (dev
->driver
!= drv
))
202 /* Information only: is this too noisy? */
203 printk(KERN_INFO
"XENBUS: Device with no driver: %s\n",
205 } else if (xendev
->state
< XenbusStateConnected
) {
206 enum xenbus_state rstate
= XenbusStateUnknown
;
207 if (xendev
->otherend
)
208 rstate
= xenbus_read_driver_state(xendev
->otherend
);
209 printk(KERN_WARNING
"XENBUS: Timeout connecting "
210 "to device: %s (local state %d, remote state %d)\n",
211 xendev
->nodename
, xendev
->state
, rstate
);
217 /* We only wait for device setup after most initcalls have run. */
218 static int ready_to_wait_for_devices
;
220 static bool wait_loop(unsigned long start
, unsigned int max_delay
,
221 unsigned int *seconds_waited
)
223 if (time_after(jiffies
, start
+ (*seconds_waited
+5)*HZ
)) {
224 if (!*seconds_waited
)
225 printk(KERN_WARNING
"XENBUS: Waiting for "
226 "devices to initialise: ");
227 *seconds_waited
+= 5;
228 printk("%us...", max_delay
- *seconds_waited
);
229 if (*seconds_waited
== max_delay
)
233 schedule_timeout_interruptible(HZ
/10);
238 * On a 5-minute timeout, wait for all devices currently configured. We need
239 * to do this to guarantee that the filesystems and / or network devices
240 * needed for boot are available, before we can allow the boot to proceed.
242 * This needs to be on a late_initcall, to happen after the frontend device
243 * drivers have been initialised, but before the root fs is mounted.
245 * A possible improvement here would be to have the tools add a per-device
246 * flag to the store entry, indicating whether it is needed at boot time.
247 * This would allow people who knew what they were doing to accelerate their
248 * boot slightly, but of course needs tools or manual intervention to set up
249 * those flags correctly.
251 static void wait_for_devices(struct xenbus_driver
*xendrv
)
253 unsigned long start
= jiffies
;
254 struct device_driver
*drv
= xendrv
? &xendrv
->driver
: NULL
;
255 unsigned int seconds_waited
= 0;
257 if (!ready_to_wait_for_devices
|| !xen_domain())
260 while (exists_non_essential_connecting_device(drv
))
261 if (wait_loop(start
, 30, &seconds_waited
))
264 /* Skips PVKB and PVFB check.*/
265 while (exists_essential_connecting_device(drv
))
266 if (wait_loop(start
, 270, &seconds_waited
))
272 bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
273 print_device_status
);
276 int xenbus_register_frontend(struct xenbus_driver
*drv
)
280 drv
->read_otherend_details
= read_backend_details
;
282 ret
= xenbus_register_driver_common(drv
, &xenbus_frontend
);
286 /* If this driver is loaded as a module wait for devices to attach. */
287 wait_for_devices(drv
);
291 EXPORT_SYMBOL_GPL(xenbus_register_frontend
);
293 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq
);
294 static int backend_state
;
296 static void xenbus_reset_backend_state_changed(struct xenbus_watch
*w
,
297 const char **v
, unsigned int l
)
299 xenbus_scanf(XBT_NIL
, v
[XS_WATCH_PATH
], "", "%i", &backend_state
);
300 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
301 v
[XS_WATCH_PATH
], xenbus_strstate(backend_state
));
302 wake_up(&backend_state_wq
);
305 static void xenbus_reset_wait_for_backend(char *be
, int expected
)
308 timeout
= wait_event_interruptible_timeout(backend_state_wq
,
309 backend_state
== expected
, 5 * HZ
);
311 printk(KERN_INFO
"XENBUS: backend %s timed out.\n", be
);
315 * Reset frontend if it is in Connected or Closed state.
316 * Wait for backend to catch up.
317 * State Connected happens during kdump, Closed after kexec.
319 static void xenbus_reset_frontend(char *fe
, char *be
, int be_state
)
321 struct xenbus_watch be_watch
;
323 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
324 be
, xenbus_strstate(be_state
));
326 memset(&be_watch
, 0, sizeof(be_watch
));
327 be_watch
.node
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "%s/state", be
);
331 be_watch
.callback
= xenbus_reset_backend_state_changed
;
332 backend_state
= XenbusStateUnknown
;
334 printk(KERN_INFO
"XENBUS: triggering reconnect on %s\n", be
);
335 register_xenbus_watch(&be_watch
);
337 /* fall through to forward backend to state XenbusStateInitialising */
339 case XenbusStateConnected
:
340 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosing
);
341 xenbus_reset_wait_for_backend(be
, XenbusStateClosing
);
343 case XenbusStateClosing
:
344 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosed
);
345 xenbus_reset_wait_for_backend(be
, XenbusStateClosed
);
347 case XenbusStateClosed
:
348 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateInitialising
);
349 xenbus_reset_wait_for_backend(be
, XenbusStateInitWait
);
352 unregister_xenbus_watch(&be_watch
);
353 printk(KERN_INFO
"XENBUS: reconnect done on %s\n", be
);
354 kfree(be_watch
.node
);
357 static void xenbus_check_frontend(char *class, char *dev
)
359 int be_state
, fe_state
, err
;
360 char *backend
, *frontend
;
362 frontend
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "device/%s/%s", class, dev
);
366 err
= xenbus_scanf(XBT_NIL
, frontend
, "state", "%i", &fe_state
);
371 case XenbusStateConnected
:
372 case XenbusStateClosed
:
373 printk(KERN_DEBUG
"XENBUS: frontend %s %s\n",
374 frontend
, xenbus_strstate(fe_state
));
375 backend
= xenbus_read(XBT_NIL
, frontend
, "backend", NULL
);
376 if (!backend
|| IS_ERR(backend
))
378 err
= xenbus_scanf(XBT_NIL
, backend
, "state", "%i", &be_state
);
380 xenbus_reset_frontend(frontend
, backend
, be_state
);
390 static void xenbus_reset_state(void)
392 char **devclass
, **dev
;
393 int devclass_n
, dev_n
;
396 devclass
= xenbus_directory(XBT_NIL
, "device", "", &devclass_n
);
397 if (IS_ERR(devclass
))
400 for (i
= 0; i
< devclass_n
; i
++) {
401 dev
= xenbus_directory(XBT_NIL
, "device", devclass
[i
], &dev_n
);
404 for (j
= 0; j
< dev_n
; j
++)
405 xenbus_check_frontend(devclass
[i
], dev
[j
]);
411 static int frontend_probe_and_watch(struct notifier_block
*notifier
,
415 /* reset devices in Connected or Closed state */
416 if (xen_hvm_domain())
417 xenbus_reset_state();
418 /* Enumerate devices in xenstore and watch for changes. */
419 xenbus_probe_devices(&xenbus_frontend
);
420 register_xenbus_watch(&fe_watch
);
426 static int __init
xenbus_probe_frontend_init(void)
428 static struct notifier_block xenstore_notifier
= {
429 .notifier_call
= frontend_probe_and_watch
435 /* Register ourselves with the kernel bus subsystem */
436 err
= bus_register(&xenbus_frontend
.bus
);
440 register_xenstore_notifier(&xenstore_notifier
);
444 subsys_initcall(xenbus_probe_frontend_init
);
447 static int __init
boot_wait_for_devices(void)
449 if (xen_hvm_domain() && !xen_platform_pci_unplug
)
452 ready_to_wait_for_devices
= 1;
453 wait_for_devices(NULL
);
457 late_initcall(boot_wait_for_devices
);
460 MODULE_LICENSE("GPL");