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"
35 /* device/<type>/<id> => <type>-<id> */
36 static int frontend_bus_id(char bus_id
[XEN_BUS_ID_SIZE
], const char *nodename
)
38 nodename
= strchr(nodename
, '/');
39 if (!nodename
|| strlen(nodename
+ 1) >= XEN_BUS_ID_SIZE
) {
40 pr_warn("bad frontend %s\n", nodename
);
44 strlcpy(bus_id
, nodename
+ 1, XEN_BUS_ID_SIZE
);
45 if (!strchr(bus_id
, '/')) {
46 pr_warn("bus_id %s no slash\n", bus_id
);
49 *strchr(bus_id
, '/') = '-';
53 /* device/<typename>/<name> */
54 static int xenbus_probe_frontend(struct xen_bus_type
*bus
, const char *type
,
60 /* ignore console/0 */
61 if (!strncmp(type
, "console", 7) && !strncmp(name
, "0", 1)) {
62 DPRINTK("Ignoring buggy device entry console/0");
66 nodename
= kasprintf(GFP_KERNEL
, "%s/%s/%s", bus
->root
, type
, name
);
70 DPRINTK("%s", nodename
);
72 err
= xenbus_probe_node(bus
, type
, nodename
);
77 static int xenbus_uevent_frontend(struct device
*_dev
,
78 struct kobj_uevent_env
*env
)
80 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
82 if (add_uevent_var(env
, "MODALIAS=xen:%s", dev
->devicetype
))
89 static void backend_changed(struct xenbus_watch
*watch
,
90 const char **vec
, unsigned int len
)
92 xenbus_otherend_changed(watch
, vec
, len
, 1);
95 static void xenbus_frontend_delayed_resume(struct work_struct
*w
)
97 struct xenbus_device
*xdev
= container_of(w
, struct xenbus_device
, work
);
99 xenbus_dev_resume(&xdev
->dev
);
102 static int xenbus_frontend_dev_resume(struct device
*dev
)
105 * If xenstored is running in this domain, we cannot access the backend
106 * state at the moment, so we need to defer xenbus_dev_resume
108 if (xen_store_domain_type
== XS_LOCAL
) {
109 struct xenbus_device
*xdev
= to_xenbus_device(dev
);
111 schedule_work(&xdev
->work
);
116 return xenbus_dev_resume(dev
);
119 static int xenbus_frontend_dev_probe(struct device
*dev
)
121 if (xen_store_domain_type
== XS_LOCAL
) {
122 struct xenbus_device
*xdev
= to_xenbus_device(dev
);
123 INIT_WORK(&xdev
->work
, xenbus_frontend_delayed_resume
);
126 return xenbus_dev_probe(dev
);
129 static const struct dev_pm_ops xenbus_pm_ops
= {
130 .suspend
= xenbus_dev_suspend
,
131 .resume
= xenbus_frontend_dev_resume
,
132 .freeze
= xenbus_dev_suspend
,
133 .thaw
= xenbus_dev_cancel
,
134 .restore
= xenbus_dev_resume
,
137 static struct xen_bus_type xenbus_frontend
= {
139 .levels
= 2, /* device/type/<id> */
140 .get_bus_id
= frontend_bus_id
,
141 .probe
= xenbus_probe_frontend
,
142 .otherend_changed
= backend_changed
,
145 .match
= xenbus_match
,
146 .uevent
= xenbus_uevent_frontend
,
147 .probe
= xenbus_frontend_dev_probe
,
148 .remove
= xenbus_dev_remove
,
149 .shutdown
= xenbus_dev_shutdown
,
150 .dev_groups
= xenbus_dev_groups
,
152 .pm
= &xenbus_pm_ops
,
156 static void frontend_changed(struct xenbus_watch
*watch
,
157 const char **vec
, unsigned int len
)
161 xenbus_dev_changed(vec
[XS_WATCH_PATH
], &xenbus_frontend
);
165 /* We watch for devices appearing and vanishing. */
166 static struct xenbus_watch fe_watch
= {
168 .callback
= frontend_changed
,
171 static int read_backend_details(struct xenbus_device
*xendev
)
173 return xenbus_read_otherend_details(xendev
, "backend-id", "backend");
176 static int is_device_connecting(struct device
*dev
, void *data
, bool ignore_nonessential
)
178 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
179 struct device_driver
*drv
= data
;
180 struct xenbus_driver
*xendrv
;
183 * A device with no driver will never connect. We care only about
184 * devices which should currently be in the process of connecting.
189 /* Is this search limited to a particular driver? */
190 if (drv
&& (dev
->driver
!= drv
))
193 if (ignore_nonessential
) {
194 /* With older QEMU, for PVonHVM guests the guest config files
195 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
196 * which is nonsensical as there is no PV FB (there can be
197 * a PVKB) running as HVM guest. */
199 if ((strncmp(xendev
->nodename
, "device/vkbd", 11) == 0))
202 if ((strncmp(xendev
->nodename
, "device/vfb", 10) == 0))
205 xendrv
= to_xenbus_driver(dev
->driver
);
206 return (xendev
->state
< XenbusStateConnected
||
207 (xendev
->state
== XenbusStateConnected
&&
208 xendrv
->is_ready
&& !xendrv
->is_ready(xendev
)));
210 static int essential_device_connecting(struct device
*dev
, void *data
)
212 return is_device_connecting(dev
, data
, true /* ignore PV[KBB+FB] */);
214 static int non_essential_device_connecting(struct device
*dev
, void *data
)
216 return is_device_connecting(dev
, data
, false);
219 static int exists_essential_connecting_device(struct device_driver
*drv
)
221 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
222 essential_device_connecting
);
224 static int exists_non_essential_connecting_device(struct device_driver
*drv
)
226 return bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
227 non_essential_device_connecting
);
230 static int print_device_status(struct device
*dev
, void *data
)
232 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
233 struct device_driver
*drv
= data
;
235 /* Is this operation limited to a particular driver? */
236 if (drv
&& (dev
->driver
!= drv
))
240 /* Information only: is this too noisy? */
241 pr_info("Device with no driver: %s\n", xendev
->nodename
);
242 } else if (xendev
->state
< XenbusStateConnected
) {
243 enum xenbus_state rstate
= XenbusStateUnknown
;
244 if (xendev
->otherend
)
245 rstate
= xenbus_read_driver_state(xendev
->otherend
);
246 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
247 xendev
->nodename
, xendev
->state
, rstate
);
253 /* We only wait for device setup after most initcalls have run. */
254 static int ready_to_wait_for_devices
;
256 static bool wait_loop(unsigned long start
, unsigned int max_delay
,
257 unsigned int *seconds_waited
)
259 if (time_after(jiffies
, start
+ (*seconds_waited
+5)*HZ
)) {
260 if (!*seconds_waited
)
261 pr_warn("Waiting for devices to initialise: ");
262 *seconds_waited
+= 5;
263 pr_cont("%us...", max_delay
- *seconds_waited
);
264 if (*seconds_waited
== max_delay
) {
270 schedule_timeout_interruptible(HZ
/10);
275 * On a 5-minute timeout, wait for all devices currently configured. We need
276 * to do this to guarantee that the filesystems and / or network devices
277 * needed for boot are available, before we can allow the boot to proceed.
279 * This needs to be on a late_initcall, to happen after the frontend device
280 * drivers have been initialised, but before the root fs is mounted.
282 * A possible improvement here would be to have the tools add a per-device
283 * flag to the store entry, indicating whether it is needed at boot time.
284 * This would allow people who knew what they were doing to accelerate their
285 * boot slightly, but of course needs tools or manual intervention to set up
286 * those flags correctly.
288 static void wait_for_devices(struct xenbus_driver
*xendrv
)
290 unsigned long start
= jiffies
;
291 struct device_driver
*drv
= xendrv
? &xendrv
->driver
: NULL
;
292 unsigned int seconds_waited
= 0;
294 if (!ready_to_wait_for_devices
|| !xen_domain())
297 while (exists_non_essential_connecting_device(drv
))
298 if (wait_loop(start
, 30, &seconds_waited
))
301 /* Skips PVKB and PVFB check.*/
302 while (exists_essential_connecting_device(drv
))
303 if (wait_loop(start
, 270, &seconds_waited
))
309 bus_for_each_dev(&xenbus_frontend
.bus
, NULL
, drv
,
310 print_device_status
);
313 int __xenbus_register_frontend(struct xenbus_driver
*drv
, struct module
*owner
,
314 const char *mod_name
)
318 drv
->read_otherend_details
= read_backend_details
;
320 ret
= xenbus_register_driver_common(drv
, &xenbus_frontend
,
325 /* If this driver is loaded as a module wait for devices to attach. */
326 wait_for_devices(drv
);
330 EXPORT_SYMBOL_GPL(__xenbus_register_frontend
);
332 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq
);
333 static int backend_state
;
335 static void xenbus_reset_backend_state_changed(struct xenbus_watch
*w
,
336 const char **v
, unsigned int l
)
338 if (xenbus_scanf(XBT_NIL
, v
[XS_WATCH_PATH
], "", "%i",
339 &backend_state
) != 1)
340 backend_state
= XenbusStateUnknown
;
341 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
342 v
[XS_WATCH_PATH
], xenbus_strstate(backend_state
));
343 wake_up(&backend_state_wq
);
346 static void xenbus_reset_wait_for_backend(char *be
, int expected
)
349 timeout
= wait_event_interruptible_timeout(backend_state_wq
,
350 backend_state
== expected
, 5 * HZ
);
352 pr_info("backend %s timed out\n", be
);
356 * Reset frontend if it is in Connected or Closed state.
357 * Wait for backend to catch up.
358 * State Connected happens during kdump, Closed after kexec.
360 static void xenbus_reset_frontend(char *fe
, char *be
, int be_state
)
362 struct xenbus_watch be_watch
;
364 printk(KERN_DEBUG
"XENBUS: backend %s %s\n",
365 be
, xenbus_strstate(be_state
));
367 memset(&be_watch
, 0, sizeof(be_watch
));
368 be_watch
.node
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "%s/state", be
);
372 be_watch
.callback
= xenbus_reset_backend_state_changed
;
373 backend_state
= XenbusStateUnknown
;
375 pr_info("triggering reconnect on %s\n", be
);
376 register_xenbus_watch(&be_watch
);
378 /* fall through to forward backend to state XenbusStateInitialising */
380 case XenbusStateConnected
:
381 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosing
);
382 xenbus_reset_wait_for_backend(be
, XenbusStateClosing
);
384 case XenbusStateClosing
:
385 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateClosed
);
386 xenbus_reset_wait_for_backend(be
, XenbusStateClosed
);
388 case XenbusStateClosed
:
389 xenbus_printf(XBT_NIL
, fe
, "state", "%d", XenbusStateInitialising
);
390 xenbus_reset_wait_for_backend(be
, XenbusStateInitWait
);
393 unregister_xenbus_watch(&be_watch
);
394 pr_info("reconnect done on %s\n", be
);
395 kfree(be_watch
.node
);
398 static void xenbus_check_frontend(char *class, char *dev
)
400 int be_state
, fe_state
, err
;
401 char *backend
, *frontend
;
403 frontend
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "device/%s/%s", class, dev
);
407 err
= xenbus_scanf(XBT_NIL
, frontend
, "state", "%i", &fe_state
);
412 case XenbusStateConnected
:
413 case XenbusStateClosed
:
414 printk(KERN_DEBUG
"XENBUS: frontend %s %s\n",
415 frontend
, xenbus_strstate(fe_state
));
416 backend
= xenbus_read(XBT_NIL
, frontend
, "backend", NULL
);
417 if (!backend
|| IS_ERR(backend
))
419 err
= xenbus_scanf(XBT_NIL
, backend
, "state", "%i", &be_state
);
421 xenbus_reset_frontend(frontend
, backend
, be_state
);
431 static void xenbus_reset_state(void)
433 char **devclass
, **dev
;
434 int devclass_n
, dev_n
;
437 devclass
= xenbus_directory(XBT_NIL
, "device", "", &devclass_n
);
438 if (IS_ERR(devclass
))
441 for (i
= 0; i
< devclass_n
; i
++) {
442 dev
= xenbus_directory(XBT_NIL
, "device", devclass
[i
], &dev_n
);
445 for (j
= 0; j
< dev_n
; j
++)
446 xenbus_check_frontend(devclass
[i
], dev
[j
]);
452 static int frontend_probe_and_watch(struct notifier_block
*notifier
,
456 /* reset devices in Connected or Closed state */
457 if (xen_hvm_domain())
458 xenbus_reset_state();
459 /* Enumerate devices in xenstore and watch for changes. */
460 xenbus_probe_devices(&xenbus_frontend
);
461 register_xenbus_watch(&fe_watch
);
467 static int __init
xenbus_probe_frontend_init(void)
469 static struct notifier_block xenstore_notifier
= {
470 .notifier_call
= frontend_probe_and_watch
476 /* Register ourselves with the kernel bus subsystem */
477 err
= bus_register(&xenbus_frontend
.bus
);
481 register_xenstore_notifier(&xenstore_notifier
);
485 subsys_initcall(xenbus_probe_frontend_init
);
488 static int __init
boot_wait_for_devices(void)
490 if (!xen_has_pv_devices())
493 ready_to_wait_for_devices
= 1;
494 wait_for_devices(NULL
);
498 late_initcall(boot_wait_for_devices
);
501 MODULE_LICENSE("GPL");