blob: 3159a37d966d57755b6bd6d0d2a19379309c7e6e [file] [log] [blame]
Ian Campbell2de06cc2009-02-09 12:05:51 -08001#define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
4
5#include <linux/kernel.h>
6#include <linux/err.h>
7#include <linux/string.h>
8#include <linux/ctype.h>
9#include <linux/fcntl.h>
10#include <linux/mm.h>
11#include <linux/proc_fs.h>
12#include <linux/notifier.h>
13#include <linux/kthread.h>
14#include <linux/mutex.h>
15#include <linux/io.h>
Paul Gortmaker72ee5112011-07-03 16:20:57 -040016#include <linux/module.h>
Ian Campbell2de06cc2009-02-09 12:05:51 -080017
18#include <asm/page.h>
19#include <asm/pgtable.h>
20#include <asm/xen/hypervisor.h>
21#include <xen/xenbus.h>
22#include <xen/events.h>
23#include <xen/page.h>
Stefano Stabellini4d9310e2012-08-06 15:27:09 +010024#include <xen/xen.h>
Ian Campbell2de06cc2009-02-09 12:05:51 -080025
26#include <xen/platform_pci.h>
27
28#include "xenbus_comms.h"
29#include "xenbus_probe.h"
30
31
32/* device/<type>/<id> => <type>-<id> */
33static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
34{
35 nodename = strchr(nodename, '/');
36 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
37 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
38 return -EINVAL;
39 }
40
41 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
42 if (!strchr(bus_id, '/')) {
43 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
44 return -EINVAL;
45 }
46 *strchr(bus_id, '/') = '-';
47 return 0;
48}
49
50/* device/<typename>/<name> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000051static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
52 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080053{
54 char *nodename;
55 int err;
56
Stefano Stabellini42c46e62012-03-13 18:30:44 +000057 /* ignore console/0 */
58 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
59 DPRINTK("Ignoring buggy device entry console/0");
60 return 0;
61 }
62
Ian Campbell2de06cc2009-02-09 12:05:51 -080063 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
64 if (!nodename)
65 return -ENOMEM;
66
67 DPRINTK("%s", nodename);
68
69 err = xenbus_probe_node(bus, type, nodename);
70 kfree(nodename);
71 return err;
72}
73
Ian Campbell6bac7f92010-12-10 14:39:15 +000074static int xenbus_uevent_frontend(struct device *_dev,
75 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080076{
77 struct xenbus_device *dev = to_xenbus_device(_dev);
78
79 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
80 return -ENOMEM;
81
82 return 0;
83}
84
85
Ian Campbell2de06cc2009-02-09 12:05:51 -080086static void backend_changed(struct xenbus_watch *watch,
87 const char **vec, unsigned int len)
88{
89 xenbus_otherend_changed(watch, vec, len, 1);
90}
91
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080092static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -080093 .suspend = xenbus_dev_suspend,
94 .resume = xenbus_dev_resume,
95 .freeze = xenbus_dev_suspend,
96 .thaw = xenbus_dev_cancel,
97 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080098};
99
Ian Campbell2de06cc2009-02-09 12:05:51 -0800100static struct xen_bus_type xenbus_frontend = {
101 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000102 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -0800103 .get_bus_id = frontend_bus_id,
104 .probe = xenbus_probe_frontend,
105 .otherend_changed = backend_changed,
106 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000107 .name = "xen",
108 .match = xenbus_match,
109 .uevent = xenbus_uevent_frontend,
110 .probe = xenbus_dev_probe,
111 .remove = xenbus_dev_remove,
112 .shutdown = xenbus_dev_shutdown,
Bastian Blankcc85e932011-06-29 14:39:26 +0200113 .dev_attrs = xenbus_dev_attrs,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800114
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800115 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800116 },
117};
118
119static void frontend_changed(struct xenbus_watch *watch,
120 const char **vec, unsigned int len)
121{
122 DPRINTK("");
123
124 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
125}
126
127
128/* We watch for devices appearing and vanishing. */
129static struct xenbus_watch fe_watch = {
130 .node = "device",
131 .callback = frontend_changed,
132};
133
134static int read_backend_details(struct xenbus_device *xendev)
135{
136 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
137}
138
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400139static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800140{
141 struct xenbus_device *xendev = to_xenbus_device(dev);
142 struct device_driver *drv = data;
143 struct xenbus_driver *xendrv;
144
145 /*
146 * A device with no driver will never connect. We care only about
147 * devices which should currently be in the process of connecting.
148 */
149 if (!dev->driver)
150 return 0;
151
152 /* Is this search limited to a particular driver? */
153 if (drv && (dev->driver != drv))
154 return 0;
155
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400156 if (ignore_nonessential) {
157 /* With older QEMU, for PVonHVM guests the guest config files
158 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
159 * which is nonsensical as there is no PV FB (there can be
160 * a PVKB) running as HVM guest. */
161
162 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
163 return 0;
164
165 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
166 return 0;
167 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800168 xendrv = to_xenbus_driver(dev->driver);
169 return (xendev->state < XenbusStateConnected ||
170 (xendev->state == XenbusStateConnected &&
171 xendrv->is_ready && !xendrv->is_ready(xendev)));
172}
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400173static int essential_device_connecting(struct device *dev, void *data)
174{
175 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
176}
177static int non_essential_device_connecting(struct device *dev, void *data)
178{
179 return is_device_connecting(dev, data, false);
180}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800181
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400182static int exists_essential_connecting_device(struct device_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800183{
184 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400185 essential_device_connecting);
186}
187static int exists_non_essential_connecting_device(struct device_driver *drv)
188{
189 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
190 non_essential_device_connecting);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800191}
192
193static int print_device_status(struct device *dev, void *data)
194{
195 struct xenbus_device *xendev = to_xenbus_device(dev);
196 struct device_driver *drv = data;
197
198 /* Is this operation limited to a particular driver? */
199 if (drv && (dev->driver != drv))
200 return 0;
201
202 if (!dev->driver) {
203 /* Information only: is this too noisy? */
204 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
205 xendev->nodename);
206 } else if (xendev->state < XenbusStateConnected) {
207 enum xenbus_state rstate = XenbusStateUnknown;
208 if (xendev->otherend)
209 rstate = xenbus_read_driver_state(xendev->otherend);
210 printk(KERN_WARNING "XENBUS: Timeout connecting "
211 "to device: %s (local state %d, remote state %d)\n",
212 xendev->nodename, xendev->state, rstate);
213 }
214
215 return 0;
216}
217
218/* We only wait for device setup after most initcalls have run. */
219static int ready_to_wait_for_devices;
220
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400221static bool wait_loop(unsigned long start, unsigned int max_delay,
222 unsigned int *seconds_waited)
223{
224 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
225 if (!*seconds_waited)
226 printk(KERN_WARNING "XENBUS: Waiting for "
227 "devices to initialise: ");
228 *seconds_waited += 5;
229 printk("%us...", max_delay - *seconds_waited);
230 if (*seconds_waited == max_delay)
231 return true;
232 }
233
234 schedule_timeout_interruptible(HZ/10);
235
236 return false;
237}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800238/*
239 * On a 5-minute timeout, wait for all devices currently configured. We need
240 * to do this to guarantee that the filesystems and / or network devices
241 * needed for boot are available, before we can allow the boot to proceed.
242 *
243 * This needs to be on a late_initcall, to happen after the frontend device
244 * drivers have been initialised, but before the root fs is mounted.
245 *
246 * A possible improvement here would be to have the tools add a per-device
247 * flag to the store entry, indicating whether it is needed at boot time.
248 * This would allow people who knew what they were doing to accelerate their
249 * boot slightly, but of course needs tools or manual intervention to set up
250 * those flags correctly.
251 */
252static void wait_for_devices(struct xenbus_driver *xendrv)
253{
254 unsigned long start = jiffies;
255 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
256 unsigned int seconds_waited = 0;
257
258 if (!ready_to_wait_for_devices || !xen_domain())
259 return;
260
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400261 while (exists_non_essential_connecting_device(drv))
262 if (wait_loop(start, 30, &seconds_waited))
263 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800264
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400265 /* Skips PVKB and PVFB check.*/
266 while (exists_essential_connecting_device(drv))
267 if (wait_loop(start, 270, &seconds_waited))
268 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800269
270 if (seconds_waited)
271 printk("\n");
272
273 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
274 print_device_status);
275}
276
Jan Beulich73db1442011-12-22 09:08:13 +0000277int xenbus_register_frontend(struct xenbus_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800278{
279 int ret;
280
281 drv->read_otherend_details = read_backend_details;
282
Jan Beulich73db1442011-12-22 09:08:13 +0000283 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800284 if (ret)
285 return ret;
286
287 /* If this driver is loaded as a module wait for devices to attach. */
288 wait_for_devices(drv);
289
290 return 0;
291}
Jan Beulich73db1442011-12-22 09:08:13 +0000292EXPORT_SYMBOL_GPL(xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800293
Olaf Hering116df6f2011-08-25 18:34:45 +0200294static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
295static int backend_state;
296
297static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
298 const char **v, unsigned int l)
299{
300 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
301 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
302 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
303 wake_up(&backend_state_wq);
304}
305
306static void xenbus_reset_wait_for_backend(char *be, int expected)
307{
308 long timeout;
309 timeout = wait_event_interruptible_timeout(backend_state_wq,
310 backend_state == expected, 5 * HZ);
311 if (timeout <= 0)
312 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
313}
314
315/*
316 * Reset frontend if it is in Connected or Closed state.
317 * Wait for backend to catch up.
318 * State Connected happens during kdump, Closed after kexec.
319 */
320static void xenbus_reset_frontend(char *fe, char *be, int be_state)
321{
322 struct xenbus_watch be_watch;
323
324 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
325 be, xenbus_strstate(be_state));
326
327 memset(&be_watch, 0, sizeof(be_watch));
328 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
329 if (!be_watch.node)
330 return;
331
332 be_watch.callback = xenbus_reset_backend_state_changed;
333 backend_state = XenbusStateUnknown;
334
335 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
336 register_xenbus_watch(&be_watch);
337
338 /* fall through to forward backend to state XenbusStateInitialising */
339 switch (be_state) {
340 case XenbusStateConnected:
341 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
342 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
343
344 case XenbusStateClosing:
345 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
346 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
347
348 case XenbusStateClosed:
349 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
350 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
351 }
352
353 unregister_xenbus_watch(&be_watch);
354 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
355 kfree(be_watch.node);
356}
357
358static void xenbus_check_frontend(char *class, char *dev)
359{
360 int be_state, fe_state, err;
361 char *backend, *frontend;
362
363 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
364 if (!frontend)
365 return;
366
367 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
368 if (err != 1)
369 goto out;
370
371 switch (fe_state) {
372 case XenbusStateConnected:
373 case XenbusStateClosed:
374 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
375 frontend, xenbus_strstate(fe_state));
376 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
377 if (!backend || IS_ERR(backend))
378 goto out;
379 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
380 if (err == 1)
381 xenbus_reset_frontend(frontend, backend, be_state);
382 kfree(backend);
383 break;
384 default:
385 break;
386 }
387out:
388 kfree(frontend);
389}
390
391static void xenbus_reset_state(void)
392{
393 char **devclass, **dev;
394 int devclass_n, dev_n;
395 int i, j;
396
397 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
398 if (IS_ERR(devclass))
399 return;
400
401 for (i = 0; i < devclass_n; i++) {
402 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
403 if (IS_ERR(dev))
404 continue;
405 for (j = 0; j < dev_n; j++)
406 xenbus_check_frontend(devclass[i], dev[j]);
407 kfree(dev);
408 }
409 kfree(devclass);
410}
411
Ian Campbelldf660252009-02-09 12:05:51 -0800412static int frontend_probe_and_watch(struct notifier_block *notifier,
413 unsigned long event,
414 void *data)
415{
Olaf Hering116df6f2011-08-25 18:34:45 +0200416 /* reset devices in Connected or Closed state */
417 if (xen_hvm_domain())
418 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800419 /* Enumerate devices in xenstore and watch for changes. */
420 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800421 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700422
Ian Campbelldf660252009-02-09 12:05:51 -0800423 return NOTIFY_DONE;
424}
425
426
Ian Campbell2de06cc2009-02-09 12:05:51 -0800427static int __init xenbus_probe_frontend_init(void)
428{
Ian Campbelldf660252009-02-09 12:05:51 -0800429 static struct notifier_block xenstore_notifier = {
430 .notifier_call = frontend_probe_and_watch
431 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800432 int err;
433
434 DPRINTK("");
435
436 /* Register ourselves with the kernel bus subsystem */
437 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700438 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800439 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800440
Ian Campbelldf660252009-02-09 12:05:51 -0800441 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800442
443 return 0;
444}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800445subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800446
447#ifndef MODULE
448static int __init boot_wait_for_devices(void)
449{
450 if (xen_hvm_domain() && !xen_platform_pci_unplug)
451 return -ENODEV;
452
453 ready_to_wait_for_devices = 1;
454 wait_for_devices(NULL);
455 return 0;
456}
457
458late_initcall(boot_wait_for_devices);
459#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700460
461MODULE_LICENSE("GPL");