1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #define DPRINTK(fmt, ...) \
4 pr_debug("(%s:%d) " fmt "\n", \
5 __func__, __LINE__, ##__VA_ARGS__)
7 #include <linux/kernel.h>
9 #include <linux/string.h>
10 #include <linux/ctype.h>
11 #include <linux/fcntl.h>
13 #include <linux/proc_fs.h>
14 #include <linux/notifier.h>
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
18 #include <linux/module.h>
21 #include <asm/pgtable.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/xenbus.h>
24 #include <xen/events.h>
28 #include <xen/platform_pci.h>
30 #include "xenbus_comms.h"
31 #include "xenbus_probe.h"
34 static struct workqueue_struct
*xenbus_frontend_wq
;
36 /* device/<type>/<id> => <type>-<id> */
37 static int frontend_bus_id(char bus_id
[XEN_BUS_ID_SIZE
], const char *nodename
)
39 nodename
= strchr(nodename
, '/');
40 if (!nodename
|| strlen(nodename
+ 1) >= XEN_BUS_ID_SIZE
) {
41 pr_warn("bad frontend %s\n", nodename
);
45 strlcpy(bus_id
, nodename
+ 1, XEN_BUS_ID_SIZE
);
46 if (!strchr(bus_id
, '/')) {
47 pr_warn("bus_id %s no slash\n", bus_id
);
50 *strchr(bus_id
, '/') = '-';
54 /* device/<typename>/<name> */
55 static int xenbus_probe_frontend(struct xen_bus_type
*bus
, const char *type
,
61 /* ignore console/0 */
62 if (!strncmp(type
, "console", 7) && !strncmp(name
, "0", 1)) {
63 DPRINTK("Ignoring buggy device entry console/0");
67 nodename
= kasprintf(GFP_KERNEL
, "%s/%s/%s", bus
->root
, type
, name
);
71 DPRINTK("%s", nodename
);
73 err
= xenbus_probe_node(bus
, type
, nodename
);
78 static int xenbus_uevent_frontend(struct device
*_dev
,
79 struct kobj_uevent_env
*env
)
81 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
83 if (add_uevent_var(env
, "MODALIAS=xen:%s", dev
->devicetype
))
90 static void backend_changed(struct xenbus_watch
*watch
,
91 const char **vec
, unsigned int len
)
93 xenbus_otherend_changed(watch
, vec
, len
, 1);
96 static void xenbus_frontend_delayed_resume(struct work_struct
*w
)
98 struct xenbus_device
*xdev
= container_of(w
, struct xenbus_device
, work
);
100 xenbus_dev_resume(&xdev
->dev
);
103 static int xenbus_frontend_dev_resume(struct device
*dev
)
106 * If xenstored is running in this domain, we cannot access the backend
107 * state at the moment, so we need to defer xenbus_dev_resume
109 if (xen_store_domain_type
== XS_LOCAL
) {
110 struct xenbus_device
*xdev
= to_xenbus_device(dev
);
112 if (!xenbus_frontend_wq
) {
113 pr_err("%s: no workqueue to process delayed resume\n",
118 queue_work(xenbus_frontend_wq
, &xdev
->work
);
123 return xenbus_dev_resume(dev
);
126 static int xenbus_frontend_dev_probe(struct device
*dev
)
128 if (xen_store_domain_type
== XS_LOCAL
) {
129 struct xenbus_device
*xdev
= to_xenbus_device(dev
);
130 INIT_WORK(&xdev
->work
, xenbus_frontend_delayed_resume
);
133 return xenbus_dev_probe(dev
);
136 static const struct dev_pm_ops xenbus_pm_ops
= {
137 .suspend
= xenbus_dev_suspend
,
138 .resume
= xenbus_frontend_dev_resume
,
139 .freeze
= xenbus_dev_suspend
,
140 .thaw
= xenbus_dev_cancel
,
141 .restore
= xenbus_dev_resume
,
144 static struct xen_bus_type xenbus_frontend
= {
146 .levels
= 2, /* device/type/<id> */
147 .get_bus_id
= frontend_bus_id
,
148 .probe
= xenbus_probe_frontend
,
149 .otherend_changed
= backend_changed
,
152 .match
= xenbus_match
,
153 .uevent
= xenbus_uevent_frontend
,
154 .probe
= xenbus_frontend_dev_probe
,
155 .remove
= xenbus_dev_remove
,
156 .shutdown
= xenbus_dev_shutdown
,
157 .dev_groups
= xenbus_dev_groups
,
159 .pm
= &xenbus_pm_ops
,
163 static void frontend_changed(struct xenbus_watch
*watch
,
164 const char **vec
, unsigned int len
)
168 xenbus_dev_changed(vec
[XS_WATCH_PATH
], &xenbus_frontend
);
172 /* We watch for devices appearing and vanishing. */
173 static struct xenbus_watch fe_watch
= {
175 .callback
= frontend_changed
,
178 static int read_backend_details(struct xenbus_device
*xendev
)
180 return xenbus_read_otherend_details(xendev
, "backend-id", "backend");
183 static int is_device_connecting(struct device
*dev
, void *data
, bool ignore_nonessential
)
185 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
186 struct device_driver
*drv
= data
;
187 struct xenbus_driver
*xendrv
;
190 * A device with no driver will never connect. We care only about
191 * devices which should currently be in the process of connecting.
196 /* Is this search limited to a particular driver? */
197 if (drv
&& (dev
->driver
!= drv
))
200 if (ignore_nonessential
) {
201 /* With older QEMU, for PVonHVM guests the guest config files
202 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
203 * which is nonsensical as there is no PV FB (there can be
204 * a PVKB) running as HVM guest. */
206 if ((strncmp(xendev
->nodename
, "device/vkbd", 11) == 0))
209 if ((strncmp(xendev
->nodename
, "device/vfb", 10) == 0))
212 xendrv
= to_xenbus_driver(dev
->driver
);
213 return (xendev
->state
< XenbusStateConnected
||
214 (xendev
->state
== XenbusStateConnected
&&
215 xendrv
->is_ready
&& !xendrv
->is_ready(xendev
)));
217 static int essential_device_connecting(struct device
*dev
, void *data
)
219 return is_device_connecting(dev
, data
, true /* ignore PV[KBB+FB] */);
221 static int non_essential_device_connecting(struct device
*dev
, void *data
)
223 return is_device_connecting(dev
, data
, false);
226 static int exists_essential_connecting_device(struct device_driver
*drv
)
228 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
229 essential_device_connecting
);
231 static int exists_non_essential_connecting_device(struct device_driver
*drv
)
233 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
234 non_essential_device_connecting
);
237 static int print_device_status(struct device
*dev
, void *data
)
239 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
240 struct device_driver
*drv
= data
;
242 /* Is this operation limited to a particular driver? */
243 if (drv
&& (dev
->driver
!= drv
))
247 /* Information only: is this too noisy? */
248 pr_info("Device with no driver: %s\n", xendev
->nodename
);
249 } else if (xendev
->state
< XenbusStateConnected
) {
250 enum xenbus_state rstate
= XenbusStateUnknown
;
251 if (xendev
->otherend
)
252 rstate
= xenbus_read_driver_state(xendev
->otherend
);
253 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
254 xendev
->nodename
, xendev
->state
, rstate
);
260 /* We only wait for device setup after most initcalls have run. */
261 static int ready_to_wait_for_devices
;
263 static bool wait_loop(unsigned long start
, unsigned int max_delay
,
264 unsigned int *seconds_waited
)
266 if (time_after(jiffies
, start
+ (*seconds_waited
+5)*HZ
)) {
267 if (!*seconds_waited
)
268 pr_warn("Waiting for devices to initialise: ");
269 *seconds_waited
+= 5;
270 pr_cont("%us...", max_delay
- *seconds_waited
);
271 if (*seconds_waited
== max_delay
) {
277 schedule_timeout_interruptible(HZ
/10);
282 * On a 5-minute timeout, wait for all devices currently configured. We need
283 * to do this to guarantee that the filesystems and / or network devices
284 * needed for boot are available, before we can allow the boot to proceed.
286 * This needs to be on a late_initcall, to happen after the frontend device
287 * drivers have been initialised, but before the root fs is mounted.
289 * A possible improvement here would be to have the tools add a per-device
290 * flag to the store entry, indicating whether it is needed at boot time.
291 * This would allow people who knew what they were doing to accelerate their
292 * boot slightly, but of course needs tools or manual intervention to set up
293 * those flags correctly.
295 static void wait_for_devices(struct xenbus_driver
*xendrv
)
297 unsigned long start
= jiffies
;
298 struct device_driver
*drv
= xendrv
? &xendrv
->driver
: NULL
;
299 unsigned int seconds_waited
= 0;
301 if (!ready_to_wait_for_devices
|| !xen_domain())
304 while (exists_non_essential_connecting_device(drv
))
305 if (wait_loop(start
, 30, &seconds_waited
))
308 /* Skips PVKB and PVFB check.*/
309 while (exists_essential_connecting_device(drv
))
310 if (wait_loop(start
, 270, &seconds_waited
))
316 bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
317 print_device_status
);
320 int xenbus_register_frontend(struct xenbus_driver
*drv
)
324 drv
->read_otherend_details
= read_backend_details
;
326 ret
= xenbus_register_driver_common(drv
, &xenbus_frontend
);
330 /* If this driver is loaded as a module wait for devices to attach. */
331 wait_for_devices(drv
);
335 EXPORT_SYMBOL_GPL(xenbus_register_frontend
);
337 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq
);
338 static int backend_state
;
340 static void xenbus_reset_backend_state_changed(struct xenbus_watch
*w
,
341 const char **v
, unsigned int l
)
343 xenbus_scanf(XBT_NIL
, v
[XS_WATCH_PATH
], "", "%i", &backend_state
);
344 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
345 v
[XS_WATCH_PATH
], xenbus_strstate(backend_state
));
346 wake_up(&backend_state_wq
);
349 static void xenbus_reset_wait_for_backend(char *be
, int expected
)
352 timeout
= wait_event_interruptible_timeout(backend_state_wq
,
353 backend_state
== expected
, 5 * HZ
);
355 pr_info("backend %s timed out\n", be
);
359 * Reset frontend if it is in Connected or Closed state.
360 * Wait for backend to catch up.
361 * State Connected happens during kdump, Closed after kexec.
363 static void xenbus_reset_frontend(char *fe
, char *be
, int be_state
)
365 struct xenbus_watch be_watch
;
367 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
368 be
, xenbus_strstate(be_state
));
370 memset(&be_watch
, 0, sizeof(be_watch
));
371 be_watch
.node
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "%s/state", be
);
375 be_watch
.callback
= xenbus_reset_backend_state_changed
;
376 backend_state
= XenbusStateUnknown
;
378 pr_info("triggering reconnect on %s\n", be
);
379 register_xenbus_watch(&be_watch
);
381 /* fall through to forward backend to state XenbusStateInitialising */
383 case XenbusStateConnected
:
384 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosing
);
385 xenbus_reset_wait_for_backend(be
, XenbusStateClosing
);
387 case XenbusStateClosing
:
388 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosed
);
389 xenbus_reset_wait_for_backend(be
, XenbusStateClosed
);
391 case XenbusStateClosed
:
392 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateInitialising
);
393 xenbus_reset_wait_for_backend(be
, XenbusStateInitWait
);
396 unregister_xenbus_watch(&be_watch
);
397 pr_info("reconnect done on %s\n", be
);
398 kfree(be_watch
.node
);
401 static void xenbus_check_frontend(char *class, char *dev
)
403 int be_state
, fe_state
, err
;
404 char *backend
, *frontend
;
406 frontend
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "device/%s/%s", class, dev
);
410 err
= xenbus_scanf(XBT_NIL
, frontend
, "state", "%i", &fe_state
);
415 case XenbusStateConnected
:
416 case XenbusStateClosed
:
417 printk(KERN_DEBUG
"XENBUS: frontend %s %s\n",
418 frontend
, xenbus_strstate(fe_state
));
419 backend
= xenbus_read(XBT_NIL
, frontend
, "backend", NULL
);
420 if (!backend
|| IS_ERR(backend
))
422 err
= xenbus_scanf(XBT_NIL
, backend
, "state", "%i", &be_state
);
424 xenbus_reset_frontend(frontend
, backend
, be_state
);
434 static void xenbus_reset_state(void)
436 char **devclass
, **dev
;
437 int devclass_n
, dev_n
;
440 devclass
= xenbus_directory(XBT_NIL
, "device", "", &devclass_n
);
441 if (IS_ERR(devclass
))
444 for (i
= 0; i
< devclass_n
; i
++) {
445 dev
= xenbus_directory(XBT_NIL
, "device", devclass
[i
], &dev_n
);
448 for (j
= 0; j
< dev_n
; j
++)
449 xenbus_check_frontend(devclass
[i
], dev
[j
]);
455 static int frontend_probe_and_watch(struct notifier_block
*notifier
,
459 /* reset devices in Connected or Closed state */
460 if (xen_hvm_domain())
461 xenbus_reset_state();
462 /* Enumerate devices in xenstore and watch for changes. */
463 xenbus_probe_devices(&xenbus_frontend
);
464 register_xenbus_watch(&fe_watch
);
470 static int __init
xenbus_probe_frontend_init(void)
472 static struct notifier_block xenstore_notifier
= {
473 .notifier_call
= frontend_probe_and_watch
479 /* Register ourselves with the kernel bus subsystem */
480 err
= bus_register(&xenbus_frontend
.bus
);
484 register_xenstore_notifier(&xenstore_notifier
);
486 if (xen_store_domain_type
== XS_LOCAL
) {
487 xenbus_frontend_wq
= create_workqueue("xenbus_frontend");
488 if (!xenbus_frontend_wq
)
489 pr_warn("create xenbus frontend workqueue failed, S3 resume is likely to fail\n");
494 subsys_initcall(xenbus_probe_frontend_init
);
497 static int __init
boot_wait_for_devices(void)
499 if (!xen_has_pv_devices())
502 ready_to_wait_for_devices
= 1;
503 wait_for_devices(NULL
);
507 late_initcall(boot_wait_for_devices
);
510 MODULE_LICENSE("GPL");