blob: 611a231196757abdb040615acf2a80244360a6a2 [file] [log] [blame]
Joe Perches283c0972013-06-28 03:21:41 -07001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
3#define DPRINTK(fmt, ...) \
4 pr_debug("(%s:%d) " fmt "\n", \
5 __func__, __LINE__, ##__VA_ARGS__)
Ian Campbell2de06cc2009-02-09 12:05:51 -08006
7#include <linux/kernel.h>
8#include <linux/err.h>
9#include <linux/string.h>
10#include <linux/ctype.h>
11#include <linux/fcntl.h>
12#include <linux/mm.h>
13#include <linux/proc_fs.h>
14#include <linux/notifier.h>
15#include <linux/kthread.h>
16#include <linux/mutex.h>
17#include <linux/io.h>
Paul Gortmaker72ee5112011-07-03 16:20:57 -040018#include <linux/module.h>
Ian Campbell2de06cc2009-02-09 12:05:51 -080019
20#include <asm/page.h>
21#include <asm/pgtable.h>
22#include <asm/xen/hypervisor.h>
23#include <xen/xenbus.h>
24#include <xen/events.h>
25#include <xen/page.h>
Stefano Stabellini4d9310e2012-08-06 15:27:09 +010026#include <xen/xen.h>
Ian Campbell2de06cc2009-02-09 12:05:51 -080027
28#include <xen/platform_pci.h>
29
30#include "xenbus_comms.h"
31#include "xenbus_probe.h"
32
33
Aurelien Chartier2abb2742013-05-28 18:09:56 +010034
Ian Campbell2de06cc2009-02-09 12:05:51 -080035/* device/<type>/<id> => <type>-<id> */
36static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
37{
38 nodename = strchr(nodename, '/');
39 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
Joe Perches283c0972013-06-28 03:21:41 -070040 pr_warn("bad frontend %s\n", nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -080041 return -EINVAL;
42 }
43
44 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
45 if (!strchr(bus_id, '/')) {
Joe Perches283c0972013-06-28 03:21:41 -070046 pr_warn("bus_id %s no slash\n", bus_id);
Ian Campbell2de06cc2009-02-09 12:05:51 -080047 return -EINVAL;
48 }
49 *strchr(bus_id, '/') = '-';
50 return 0;
51}
52
53/* device/<typename>/<name> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000054static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
55 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080056{
57 char *nodename;
58 int err;
59
Stefano Stabellini42c46e62012-03-13 18:30:44 +000060 /* ignore console/0 */
61 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
62 DPRINTK("Ignoring buggy device entry console/0");
63 return 0;
64 }
65
Ian Campbell2de06cc2009-02-09 12:05:51 -080066 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
67 if (!nodename)
68 return -ENOMEM;
69
70 DPRINTK("%s", nodename);
71
72 err = xenbus_probe_node(bus, type, nodename);
73 kfree(nodename);
74 return err;
75}
76
Ian Campbell6bac7f92010-12-10 14:39:15 +000077static int xenbus_uevent_frontend(struct device *_dev,
78 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080079{
80 struct xenbus_device *dev = to_xenbus_device(_dev);
81
82 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
83 return -ENOMEM;
84
85 return 0;
86}
87
88
Ian Campbell2de06cc2009-02-09 12:05:51 -080089static void backend_changed(struct xenbus_watch *watch,
90 const char **vec, unsigned int len)
91{
92 xenbus_otherend_changed(watch, vec, len, 1);
93}
94
Aurelien Chartier2abb2742013-05-28 18:09:56 +010095static void xenbus_frontend_delayed_resume(struct work_struct *w)
96{
97 struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
98
99 xenbus_dev_resume(&xdev->dev);
100}
101
102static int xenbus_frontend_dev_resume(struct device *dev)
103{
104 /*
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
107 */
108 if (xen_store_domain_type == XS_LOCAL) {
109 struct xenbus_device *xdev = to_xenbus_device(dev);
110
Bhaktipriya Shridhar5ee405d2016-05-31 22:26:30 +0530111 schedule_work(&xdev->work);
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100112
113 return 0;
114 }
115
116 return xenbus_dev_resume(dev);
117}
118
Aurelien Chartierd7ead0c2013-07-09 14:29:35 +0100119static int xenbus_frontend_dev_probe(struct device *dev)
120{
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);
124 }
125
126 return xenbus_dev_probe(dev);
127}
128
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800129static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800130 .suspend = xenbus_dev_suspend,
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100131 .resume = xenbus_frontend_dev_resume,
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800132 .freeze = xenbus_dev_suspend,
133 .thaw = xenbus_dev_cancel,
134 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800135};
136
Ian Campbell2de06cc2009-02-09 12:05:51 -0800137static struct xen_bus_type xenbus_frontend = {
138 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000139 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -0800140 .get_bus_id = frontend_bus_id,
141 .probe = xenbus_probe_frontend,
142 .otherend_changed = backend_changed,
143 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000144 .name = "xen",
145 .match = xenbus_match,
146 .uevent = xenbus_uevent_frontend,
Aurelien Chartierd7ead0c2013-07-09 14:29:35 +0100147 .probe = xenbus_frontend_dev_probe,
Ian Campbell6bac7f92010-12-10 14:39:15 +0000148 .remove = xenbus_dev_remove,
149 .shutdown = xenbus_dev_shutdown,
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700150 .dev_groups = xenbus_dev_groups,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800151
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800152 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800153 },
154};
155
156static void frontend_changed(struct xenbus_watch *watch,
157 const char **vec, unsigned int len)
158{
159 DPRINTK("");
160
161 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
162}
163
164
165/* We watch for devices appearing and vanishing. */
166static struct xenbus_watch fe_watch = {
167 .node = "device",
168 .callback = frontend_changed,
169};
170
171static int read_backend_details(struct xenbus_device *xendev)
172{
173 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
174}
175
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400176static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800177{
178 struct xenbus_device *xendev = to_xenbus_device(dev);
179 struct device_driver *drv = data;
180 struct xenbus_driver *xendrv;
181
182 /*
183 * A device with no driver will never connect. We care only about
184 * devices which should currently be in the process of connecting.
185 */
186 if (!dev->driver)
187 return 0;
188
189 /* Is this search limited to a particular driver? */
190 if (drv && (dev->driver != drv))
191 return 0;
192
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400193 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. */
198
199 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
200 return 0;
201
202 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
203 return 0;
204 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800205 xendrv = to_xenbus_driver(dev->driver);
206 return (xendev->state < XenbusStateConnected ||
207 (xendev->state == XenbusStateConnected &&
208 xendrv->is_ready && !xendrv->is_ready(xendev)));
209}
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400210static int essential_device_connecting(struct device *dev, void *data)
211{
212 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
213}
214static int non_essential_device_connecting(struct device *dev, void *data)
215{
216 return is_device_connecting(dev, data, false);
217}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800218
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400219static int exists_essential_connecting_device(struct device_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800220{
221 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400222 essential_device_connecting);
223}
224static int exists_non_essential_connecting_device(struct device_driver *drv)
225{
226 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
227 non_essential_device_connecting);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800228}
229
230static int print_device_status(struct device *dev, void *data)
231{
232 struct xenbus_device *xendev = to_xenbus_device(dev);
233 struct device_driver *drv = data;
234
235 /* Is this operation limited to a particular driver? */
236 if (drv && (dev->driver != drv))
237 return 0;
238
239 if (!dev->driver) {
240 /* Information only: is this too noisy? */
Joe Perches283c0972013-06-28 03:21:41 -0700241 pr_info("Device with no driver: %s\n", xendev->nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800242 } else if (xendev->state < XenbusStateConnected) {
243 enum xenbus_state rstate = XenbusStateUnknown;
244 if (xendev->otherend)
245 rstate = xenbus_read_driver_state(xendev->otherend);
Joe Perches283c0972013-06-28 03:21:41 -0700246 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
247 xendev->nodename, xendev->state, rstate);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800248 }
249
250 return 0;
251}
252
253/* We only wait for device setup after most initcalls have run. */
254static int ready_to_wait_for_devices;
255
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400256static bool wait_loop(unsigned long start, unsigned int max_delay,
257 unsigned int *seconds_waited)
258{
259 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
260 if (!*seconds_waited)
Joe Perches283c0972013-06-28 03:21:41 -0700261 pr_warn("Waiting for devices to initialise: ");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400262 *seconds_waited += 5;
Joe Perches283c0972013-06-28 03:21:41 -0700263 pr_cont("%us...", max_delay - *seconds_waited);
264 if (*seconds_waited == max_delay) {
265 pr_cont("\n");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400266 return true;
Joe Perches283c0972013-06-28 03:21:41 -0700267 }
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400268 }
269
270 schedule_timeout_interruptible(HZ/10);
271
272 return false;
273}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800274/*
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.
278 *
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.
281 *
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.
287 */
288static void wait_for_devices(struct xenbus_driver *xendrv)
289{
290 unsigned long start = jiffies;
291 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
292 unsigned int seconds_waited = 0;
293
294 if (!ready_to_wait_for_devices || !xen_domain())
295 return;
296
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400297 while (exists_non_essential_connecting_device(drv))
298 if (wait_loop(start, 30, &seconds_waited))
299 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800300
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400301 /* Skips PVKB and PVFB check.*/
302 while (exists_essential_connecting_device(drv))
303 if (wait_loop(start, 270, &seconds_waited))
304 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800305
306 if (seconds_waited)
307 printk("\n");
308
309 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
310 print_device_status);
311}
312
David Vrabel95afae42014-09-08 17:30:41 +0100313int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
314 const char *mod_name)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800315{
316 int ret;
317
318 drv->read_otherend_details = read_backend_details;
319
David Vrabel95afae42014-09-08 17:30:41 +0100320 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
321 owner, mod_name);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800322 if (ret)
323 return ret;
324
325 /* If this driver is loaded as a module wait for devices to attach. */
326 wait_for_devices(drv);
327
328 return 0;
329}
David Vrabel95afae42014-09-08 17:30:41 +0100330EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800331
Olaf Hering116df6f2011-08-25 18:34:45 +0200332static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
333static int backend_state;
334
335static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
336 const char **v, unsigned int l)
337{
338 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
339 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
340 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
341 wake_up(&backend_state_wq);
342}
343
344static void xenbus_reset_wait_for_backend(char *be, int expected)
345{
346 long timeout;
347 timeout = wait_event_interruptible_timeout(backend_state_wq,
348 backend_state == expected, 5 * HZ);
349 if (timeout <= 0)
Joe Perches283c0972013-06-28 03:21:41 -0700350 pr_info("backend %s timed out\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200351}
352
353/*
354 * Reset frontend if it is in Connected or Closed state.
355 * Wait for backend to catch up.
356 * State Connected happens during kdump, Closed after kexec.
357 */
358static void xenbus_reset_frontend(char *fe, char *be, int be_state)
359{
360 struct xenbus_watch be_watch;
361
362 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
363 be, xenbus_strstate(be_state));
364
365 memset(&be_watch, 0, sizeof(be_watch));
366 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
367 if (!be_watch.node)
368 return;
369
370 be_watch.callback = xenbus_reset_backend_state_changed;
371 backend_state = XenbusStateUnknown;
372
Joe Perches283c0972013-06-28 03:21:41 -0700373 pr_info("triggering reconnect on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200374 register_xenbus_watch(&be_watch);
375
376 /* fall through to forward backend to state XenbusStateInitialising */
377 switch (be_state) {
378 case XenbusStateConnected:
379 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
380 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
381
382 case XenbusStateClosing:
383 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
384 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
385
386 case XenbusStateClosed:
387 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
388 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
389 }
390
391 unregister_xenbus_watch(&be_watch);
Joe Perches283c0972013-06-28 03:21:41 -0700392 pr_info("reconnect done on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200393 kfree(be_watch.node);
394}
395
396static void xenbus_check_frontend(char *class, char *dev)
397{
398 int be_state, fe_state, err;
399 char *backend, *frontend;
400
401 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
402 if (!frontend)
403 return;
404
405 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
406 if (err != 1)
407 goto out;
408
409 switch (fe_state) {
410 case XenbusStateConnected:
411 case XenbusStateClosed:
412 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
413 frontend, xenbus_strstate(fe_state));
414 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
415 if (!backend || IS_ERR(backend))
416 goto out;
417 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
418 if (err == 1)
419 xenbus_reset_frontend(frontend, backend, be_state);
420 kfree(backend);
421 break;
422 default:
423 break;
424 }
425out:
426 kfree(frontend);
427}
428
429static void xenbus_reset_state(void)
430{
431 char **devclass, **dev;
432 int devclass_n, dev_n;
433 int i, j;
434
435 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
436 if (IS_ERR(devclass))
437 return;
438
439 for (i = 0; i < devclass_n; i++) {
440 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
441 if (IS_ERR(dev))
442 continue;
443 for (j = 0; j < dev_n; j++)
444 xenbus_check_frontend(devclass[i], dev[j]);
445 kfree(dev);
446 }
447 kfree(devclass);
448}
449
Ian Campbelldf660252009-02-09 12:05:51 -0800450static int frontend_probe_and_watch(struct notifier_block *notifier,
451 unsigned long event,
452 void *data)
453{
Olaf Hering116df6f2011-08-25 18:34:45 +0200454 /* reset devices in Connected or Closed state */
455 if (xen_hvm_domain())
456 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800457 /* Enumerate devices in xenstore and watch for changes. */
458 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800459 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700460
Ian Campbelldf660252009-02-09 12:05:51 -0800461 return NOTIFY_DONE;
462}
463
464
Ian Campbell2de06cc2009-02-09 12:05:51 -0800465static int __init xenbus_probe_frontend_init(void)
466{
Ian Campbelldf660252009-02-09 12:05:51 -0800467 static struct notifier_block xenstore_notifier = {
468 .notifier_call = frontend_probe_and_watch
469 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800470 int err;
471
472 DPRINTK("");
473
474 /* Register ourselves with the kernel bus subsystem */
475 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700476 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800477 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800478
Ian Campbelldf660252009-02-09 12:05:51 -0800479 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800480
481 return 0;
482}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800483subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800484
485#ifndef MODULE
486static int __init boot_wait_for_devices(void)
487{
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -0500488 if (!xen_has_pv_devices())
Ian Campbell2de06cc2009-02-09 12:05:51 -0800489 return -ENODEV;
490
491 ready_to_wait_for_devices = 1;
492 wait_for_devices(NULL);
493 return 0;
494}
495
496late_initcall(boot_wait_for_devices);
497#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700498
499MODULE_LICENSE("GPL");