blob: a7e25073de19bc3b70e95b2a8697792f62312b63 [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
Aurelien Chartier2abb2742013-05-28 18:09:56 +010032static struct workqueue_struct *xenbus_frontend_wq;
33
Ian Campbell2de06cc2009-02-09 12:05:51 -080034/* device/<type>/<id> => <type>-<id> */
35static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
36{
37 nodename = strchr(nodename, '/');
38 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
39 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
40 return -EINVAL;
41 }
42
43 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
44 if (!strchr(bus_id, '/')) {
45 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
46 return -EINVAL;
47 }
48 *strchr(bus_id, '/') = '-';
49 return 0;
50}
51
52/* device/<typename>/<name> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000053static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
54 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080055{
56 char *nodename;
57 int err;
58
Stefano Stabellini42c46e62012-03-13 18:30:44 +000059 /* ignore console/0 */
60 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
61 DPRINTK("Ignoring buggy device entry console/0");
62 return 0;
63 }
64
Ian Campbell2de06cc2009-02-09 12:05:51 -080065 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
66 if (!nodename)
67 return -ENOMEM;
68
69 DPRINTK("%s", nodename);
70
71 err = xenbus_probe_node(bus, type, nodename);
72 kfree(nodename);
73 return err;
74}
75
Ian Campbell6bac7f92010-12-10 14:39:15 +000076static int xenbus_uevent_frontend(struct device *_dev,
77 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080078{
79 struct xenbus_device *dev = to_xenbus_device(_dev);
80
81 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
82 return -ENOMEM;
83
84 return 0;
85}
86
87
Ian Campbell2de06cc2009-02-09 12:05:51 -080088static void backend_changed(struct xenbus_watch *watch,
89 const char **vec, unsigned int len)
90{
91 xenbus_otherend_changed(watch, vec, len, 1);
92}
93
Aurelien Chartier2abb2742013-05-28 18:09:56 +010094static void xenbus_frontend_delayed_resume(struct work_struct *w)
95{
96 struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
97
98 xenbus_dev_resume(&xdev->dev);
99}
100
101static int xenbus_frontend_dev_resume(struct device *dev)
102{
103 /*
104 * If xenstored is running in this domain, we cannot access the backend
105 * state at the moment, so we need to defer xenbus_dev_resume
106 */
107 if (xen_store_domain_type == XS_LOCAL) {
108 struct xenbus_device *xdev = to_xenbus_device(dev);
109
110 if (!xenbus_frontend_wq) {
111 pr_err("%s: no workqueue to process delayed resume\n",
112 xdev->nodename);
113 return -EFAULT;
114 }
115
116 INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
117 queue_work(xenbus_frontend_wq, &xdev->work);
118
119 return 0;
120 }
121
122 return xenbus_dev_resume(dev);
123}
124
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800125static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800126 .suspend = xenbus_dev_suspend,
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100127 .resume = xenbus_frontend_dev_resume,
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800128 .freeze = xenbus_dev_suspend,
129 .thaw = xenbus_dev_cancel,
130 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800131};
132
Ian Campbell2de06cc2009-02-09 12:05:51 -0800133static struct xen_bus_type xenbus_frontend = {
134 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000135 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -0800136 .get_bus_id = frontend_bus_id,
137 .probe = xenbus_probe_frontend,
138 .otherend_changed = backend_changed,
139 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000140 .name = "xen",
141 .match = xenbus_match,
142 .uevent = xenbus_uevent_frontend,
143 .probe = xenbus_dev_probe,
144 .remove = xenbus_dev_remove,
145 .shutdown = xenbus_dev_shutdown,
Bastian Blankcc85e932011-06-29 14:39:26 +0200146 .dev_attrs = xenbus_dev_attrs,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800147
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800148 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800149 },
150};
151
152static void frontend_changed(struct xenbus_watch *watch,
153 const char **vec, unsigned int len)
154{
155 DPRINTK("");
156
157 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
158}
159
160
161/* We watch for devices appearing and vanishing. */
162static struct xenbus_watch fe_watch = {
163 .node = "device",
164 .callback = frontend_changed,
165};
166
167static int read_backend_details(struct xenbus_device *xendev)
168{
169 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
170}
171
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400172static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800173{
174 struct xenbus_device *xendev = to_xenbus_device(dev);
175 struct device_driver *drv = data;
176 struct xenbus_driver *xendrv;
177
178 /*
179 * A device with no driver will never connect. We care only about
180 * devices which should currently be in the process of connecting.
181 */
182 if (!dev->driver)
183 return 0;
184
185 /* Is this search limited to a particular driver? */
186 if (drv && (dev->driver != drv))
187 return 0;
188
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400189 if (ignore_nonessential) {
190 /* With older QEMU, for PVonHVM guests the guest config files
191 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
192 * which is nonsensical as there is no PV FB (there can be
193 * a PVKB) running as HVM guest. */
194
195 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
196 return 0;
197
198 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
199 return 0;
200 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800201 xendrv = to_xenbus_driver(dev->driver);
202 return (xendev->state < XenbusStateConnected ||
203 (xendev->state == XenbusStateConnected &&
204 xendrv->is_ready && !xendrv->is_ready(xendev)));
205}
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400206static int essential_device_connecting(struct device *dev, void *data)
207{
208 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
209}
210static int non_essential_device_connecting(struct device *dev, void *data)
211{
212 return is_device_connecting(dev, data, false);
213}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800214
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400215static int exists_essential_connecting_device(struct device_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800216{
217 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400218 essential_device_connecting);
219}
220static int exists_non_essential_connecting_device(struct device_driver *drv)
221{
222 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
223 non_essential_device_connecting);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800224}
225
226static int print_device_status(struct device *dev, void *data)
227{
228 struct xenbus_device *xendev = to_xenbus_device(dev);
229 struct device_driver *drv = data;
230
231 /* Is this operation limited to a particular driver? */
232 if (drv && (dev->driver != drv))
233 return 0;
234
235 if (!dev->driver) {
236 /* Information only: is this too noisy? */
237 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
238 xendev->nodename);
239 } else if (xendev->state < XenbusStateConnected) {
240 enum xenbus_state rstate = XenbusStateUnknown;
241 if (xendev->otherend)
242 rstate = xenbus_read_driver_state(xendev->otherend);
243 printk(KERN_WARNING "XENBUS: Timeout connecting "
244 "to device: %s (local state %d, remote state %d)\n",
245 xendev->nodename, xendev->state, rstate);
246 }
247
248 return 0;
249}
250
251/* We only wait for device setup after most initcalls have run. */
252static int ready_to_wait_for_devices;
253
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400254static bool wait_loop(unsigned long start, unsigned int max_delay,
255 unsigned int *seconds_waited)
256{
257 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
258 if (!*seconds_waited)
259 printk(KERN_WARNING "XENBUS: Waiting for "
260 "devices to initialise: ");
261 *seconds_waited += 5;
262 printk("%us...", max_delay - *seconds_waited);
263 if (*seconds_waited == max_delay)
264 return true;
265 }
266
267 schedule_timeout_interruptible(HZ/10);
268
269 return false;
270}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800271/*
272 * On a 5-minute timeout, wait for all devices currently configured. We need
273 * to do this to guarantee that the filesystems and / or network devices
274 * needed for boot are available, before we can allow the boot to proceed.
275 *
276 * This needs to be on a late_initcall, to happen after the frontend device
277 * drivers have been initialised, but before the root fs is mounted.
278 *
279 * A possible improvement here would be to have the tools add a per-device
280 * flag to the store entry, indicating whether it is needed at boot time.
281 * This would allow people who knew what they were doing to accelerate their
282 * boot slightly, but of course needs tools or manual intervention to set up
283 * those flags correctly.
284 */
285static void wait_for_devices(struct xenbus_driver *xendrv)
286{
287 unsigned long start = jiffies;
288 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
289 unsigned int seconds_waited = 0;
290
291 if (!ready_to_wait_for_devices || !xen_domain())
292 return;
293
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400294 while (exists_non_essential_connecting_device(drv))
295 if (wait_loop(start, 30, &seconds_waited))
296 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800297
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400298 /* Skips PVKB and PVFB check.*/
299 while (exists_essential_connecting_device(drv))
300 if (wait_loop(start, 270, &seconds_waited))
301 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800302
303 if (seconds_waited)
304 printk("\n");
305
306 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
307 print_device_status);
308}
309
Jan Beulich73db1442011-12-22 09:08:13 +0000310int xenbus_register_frontend(struct xenbus_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800311{
312 int ret;
313
314 drv->read_otherend_details = read_backend_details;
315
Jan Beulich73db1442011-12-22 09:08:13 +0000316 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800317 if (ret)
318 return ret;
319
320 /* If this driver is loaded as a module wait for devices to attach. */
321 wait_for_devices(drv);
322
323 return 0;
324}
Jan Beulich73db1442011-12-22 09:08:13 +0000325EXPORT_SYMBOL_GPL(xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800326
Olaf Hering116df6f2011-08-25 18:34:45 +0200327static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
328static int backend_state;
329
330static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
331 const char **v, unsigned int l)
332{
333 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
334 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
335 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
336 wake_up(&backend_state_wq);
337}
338
339static void xenbus_reset_wait_for_backend(char *be, int expected)
340{
341 long timeout;
342 timeout = wait_event_interruptible_timeout(backend_state_wq,
343 backend_state == expected, 5 * HZ);
344 if (timeout <= 0)
345 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
346}
347
348/*
349 * Reset frontend if it is in Connected or Closed state.
350 * Wait for backend to catch up.
351 * State Connected happens during kdump, Closed after kexec.
352 */
353static void xenbus_reset_frontend(char *fe, char *be, int be_state)
354{
355 struct xenbus_watch be_watch;
356
357 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
358 be, xenbus_strstate(be_state));
359
360 memset(&be_watch, 0, sizeof(be_watch));
361 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
362 if (!be_watch.node)
363 return;
364
365 be_watch.callback = xenbus_reset_backend_state_changed;
366 backend_state = XenbusStateUnknown;
367
368 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
369 register_xenbus_watch(&be_watch);
370
371 /* fall through to forward backend to state XenbusStateInitialising */
372 switch (be_state) {
373 case XenbusStateConnected:
374 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
375 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
376
377 case XenbusStateClosing:
378 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
379 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
380
381 case XenbusStateClosed:
382 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
383 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
384 }
385
386 unregister_xenbus_watch(&be_watch);
387 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
388 kfree(be_watch.node);
389}
390
391static void xenbus_check_frontend(char *class, char *dev)
392{
393 int be_state, fe_state, err;
394 char *backend, *frontend;
395
396 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
397 if (!frontend)
398 return;
399
400 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
401 if (err != 1)
402 goto out;
403
404 switch (fe_state) {
405 case XenbusStateConnected:
406 case XenbusStateClosed:
407 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
408 frontend, xenbus_strstate(fe_state));
409 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
410 if (!backend || IS_ERR(backend))
411 goto out;
412 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
413 if (err == 1)
414 xenbus_reset_frontend(frontend, backend, be_state);
415 kfree(backend);
416 break;
417 default:
418 break;
419 }
420out:
421 kfree(frontend);
422}
423
424static void xenbus_reset_state(void)
425{
426 char **devclass, **dev;
427 int devclass_n, dev_n;
428 int i, j;
429
430 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
431 if (IS_ERR(devclass))
432 return;
433
434 for (i = 0; i < devclass_n; i++) {
435 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
436 if (IS_ERR(dev))
437 continue;
438 for (j = 0; j < dev_n; j++)
439 xenbus_check_frontend(devclass[i], dev[j]);
440 kfree(dev);
441 }
442 kfree(devclass);
443}
444
Ian Campbelldf660252009-02-09 12:05:51 -0800445static int frontend_probe_and_watch(struct notifier_block *notifier,
446 unsigned long event,
447 void *data)
448{
Olaf Hering116df6f2011-08-25 18:34:45 +0200449 /* reset devices in Connected or Closed state */
450 if (xen_hvm_domain())
451 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800452 /* Enumerate devices in xenstore and watch for changes. */
453 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800454 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700455
Ian Campbelldf660252009-02-09 12:05:51 -0800456 return NOTIFY_DONE;
457}
458
459
Ian Campbell2de06cc2009-02-09 12:05:51 -0800460static int __init xenbus_probe_frontend_init(void)
461{
Ian Campbelldf660252009-02-09 12:05:51 -0800462 static struct notifier_block xenstore_notifier = {
463 .notifier_call = frontend_probe_and_watch
464 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800465 int err;
466
467 DPRINTK("");
468
469 /* Register ourselves with the kernel bus subsystem */
470 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700471 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800472 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800473
Ian Campbelldf660252009-02-09 12:05:51 -0800474 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800475
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100476 xenbus_frontend_wq = create_workqueue("xenbus_frontend");
477
Ian Campbell2de06cc2009-02-09 12:05:51 -0800478 return 0;
479}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800480subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800481
482#ifndef MODULE
483static int __init boot_wait_for_devices(void)
484{
485 if (xen_hvm_domain() && !xen_platform_pci_unplug)
486 return -ENODEV;
487
488 ready_to_wait_for_devices = 1;
489 wait_for_devices(NULL);
490 return 0;
491}
492
493late_initcall(boot_wait_for_devices);
494#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700495
496MODULE_LICENSE("GPL");