blob: 07896f4b273652cb0fadac3f4404990c002a53ef [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
Juergen Gross332f7912017-02-09 14:39:56 +010030#include "xenbus.h"
Ian Campbell2de06cc2009-02-09 12:05:51 -080031
32
Aurelien Chartier2abb2742013-05-28 18:09:56 +010033
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) {
Joe Perches283c0972013-06-28 03:21:41 -070039 pr_warn("bad frontend %s\n", nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -080040 return -EINVAL;
41 }
42
43 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
44 if (!strchr(bus_id, '/')) {
Joe Perches283c0972013-06-28 03:21:41 -070045 pr_warn("bus_id %s no slash\n", bus_id);
Ian Campbell2de06cc2009-02-09 12:05:51 -080046 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,
Juergen Gross5584ea22017-02-09 14:39:57 +010089 const char *path, const char *token)
Ian Campbell2de06cc2009-02-09 12:05:51 -080090{
Juergen Gross5584ea22017-02-09 14:39:57 +010091 xenbus_otherend_changed(watch, path, token, 1);
Ian Campbell2de06cc2009-02-09 12:05:51 -080092}
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
Bhaktipriya Shridhar5ee405d2016-05-31 22:26:30 +0530110 schedule_work(&xdev->work);
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100111
112 return 0;
113 }
114
115 return xenbus_dev_resume(dev);
116}
117
Aurelien Chartierd7ead0c2013-07-09 14:29:35 +0100118static int xenbus_frontend_dev_probe(struct device *dev)
119{
120 if (xen_store_domain_type == XS_LOCAL) {
121 struct xenbus_device *xdev = to_xenbus_device(dev);
122 INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
123 }
124
125 return xenbus_dev_probe(dev);
126}
127
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800128static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800129 .suspend = xenbus_dev_suspend,
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100130 .resume = xenbus_frontend_dev_resume,
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800131 .freeze = xenbus_dev_suspend,
132 .thaw = xenbus_dev_cancel,
133 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800134};
135
Ian Campbell2de06cc2009-02-09 12:05:51 -0800136static struct xen_bus_type xenbus_frontend = {
137 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000138 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -0800139 .get_bus_id = frontend_bus_id,
140 .probe = xenbus_probe_frontend,
141 .otherend_changed = backend_changed,
142 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000143 .name = "xen",
144 .match = xenbus_match,
145 .uevent = xenbus_uevent_frontend,
Aurelien Chartierd7ead0c2013-07-09 14:29:35 +0100146 .probe = xenbus_frontend_dev_probe,
Ian Campbell6bac7f92010-12-10 14:39:15 +0000147 .remove = xenbus_dev_remove,
148 .shutdown = xenbus_dev_shutdown,
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700149 .dev_groups = xenbus_dev_groups,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800150
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800151 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800152 },
153};
154
155static void frontend_changed(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100156 const char *path, const char *token)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800157{
158 DPRINTK("");
159
Juergen Gross5584ea22017-02-09 14:39:57 +0100160 xenbus_dev_changed(path, &xenbus_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800161}
162
163
164/* We watch for devices appearing and vanishing. */
165static struct xenbus_watch fe_watch = {
166 .node = "device",
167 .callback = frontend_changed,
168};
169
170static int read_backend_details(struct xenbus_device *xendev)
171{
172 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
173}
174
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400175static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800176{
177 struct xenbus_device *xendev = to_xenbus_device(dev);
178 struct device_driver *drv = data;
179 struct xenbus_driver *xendrv;
180
181 /*
182 * A device with no driver will never connect. We care only about
183 * devices which should currently be in the process of connecting.
184 */
185 if (!dev->driver)
186 return 0;
187
188 /* Is this search limited to a particular driver? */
189 if (drv && (dev->driver != drv))
190 return 0;
191
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400192 if (ignore_nonessential) {
193 /* With older QEMU, for PVonHVM guests the guest config files
194 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
195 * which is nonsensical as there is no PV FB (there can be
196 * a PVKB) running as HVM guest. */
197
198 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
199 return 0;
200
201 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
202 return 0;
203 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800204 xendrv = to_xenbus_driver(dev->driver);
205 return (xendev->state < XenbusStateConnected ||
206 (xendev->state == XenbusStateConnected &&
207 xendrv->is_ready && !xendrv->is_ready(xendev)));
208}
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400209static int essential_device_connecting(struct device *dev, void *data)
210{
211 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
212}
213static int non_essential_device_connecting(struct device *dev, void *data)
214{
215 return is_device_connecting(dev, data, false);
216}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800217
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400218static int exists_essential_connecting_device(struct device_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800219{
220 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400221 essential_device_connecting);
222}
223static int exists_non_essential_connecting_device(struct device_driver *drv)
224{
225 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
226 non_essential_device_connecting);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800227}
228
229static int print_device_status(struct device *dev, void *data)
230{
231 struct xenbus_device *xendev = to_xenbus_device(dev);
232 struct device_driver *drv = data;
233
234 /* Is this operation limited to a particular driver? */
235 if (drv && (dev->driver != drv))
236 return 0;
237
238 if (!dev->driver) {
239 /* Information only: is this too noisy? */
Joe Perches283c0972013-06-28 03:21:41 -0700240 pr_info("Device with no driver: %s\n", xendev->nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800241 } else if (xendev->state < XenbusStateConnected) {
242 enum xenbus_state rstate = XenbusStateUnknown;
243 if (xendev->otherend)
244 rstate = xenbus_read_driver_state(xendev->otherend);
Joe Perches283c0972013-06-28 03:21:41 -0700245 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
246 xendev->nodename, xendev->state, rstate);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800247 }
248
249 return 0;
250}
251
252/* We only wait for device setup after most initcalls have run. */
253static int ready_to_wait_for_devices;
254
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400255static bool wait_loop(unsigned long start, unsigned int max_delay,
256 unsigned int *seconds_waited)
257{
258 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
259 if (!*seconds_waited)
Joe Perches283c0972013-06-28 03:21:41 -0700260 pr_warn("Waiting for devices to initialise: ");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400261 *seconds_waited += 5;
Joe Perches283c0972013-06-28 03:21:41 -0700262 pr_cont("%us...", max_delay - *seconds_waited);
263 if (*seconds_waited == max_delay) {
264 pr_cont("\n");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400265 return true;
Joe Perches283c0972013-06-28 03:21:41 -0700266 }
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400267 }
268
269 schedule_timeout_interruptible(HZ/10);
270
271 return false;
272}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800273/*
274 * On a 5-minute timeout, wait for all devices currently configured. We need
275 * to do this to guarantee that the filesystems and / or network devices
276 * needed for boot are available, before we can allow the boot to proceed.
277 *
278 * This needs to be on a late_initcall, to happen after the frontend device
279 * drivers have been initialised, but before the root fs is mounted.
280 *
281 * A possible improvement here would be to have the tools add a per-device
282 * flag to the store entry, indicating whether it is needed at boot time.
283 * This would allow people who knew what they were doing to accelerate their
284 * boot slightly, but of course needs tools or manual intervention to set up
285 * those flags correctly.
286 */
287static void wait_for_devices(struct xenbus_driver *xendrv)
288{
289 unsigned long start = jiffies;
290 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
291 unsigned int seconds_waited = 0;
292
293 if (!ready_to_wait_for_devices || !xen_domain())
294 return;
295
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400296 while (exists_non_essential_connecting_device(drv))
297 if (wait_loop(start, 30, &seconds_waited))
298 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800299
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400300 /* Skips PVKB and PVFB check.*/
301 while (exists_essential_connecting_device(drv))
302 if (wait_loop(start, 270, &seconds_waited))
303 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800304
305 if (seconds_waited)
306 printk("\n");
307
308 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
309 print_device_status);
310}
311
David Vrabel95afae42014-09-08 17:30:41 +0100312int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
313 const char *mod_name)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800314{
315 int ret;
316
317 drv->read_otherend_details = read_backend_details;
318
David Vrabel95afae42014-09-08 17:30:41 +0100319 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
320 owner, mod_name);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800321 if (ret)
322 return ret;
323
324 /* If this driver is loaded as a module wait for devices to attach. */
325 wait_for_devices(drv);
326
327 return 0;
328}
David Vrabel95afae42014-09-08 17:30:41 +0100329EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800330
Olaf Hering116df6f2011-08-25 18:34:45 +0200331static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
332static int backend_state;
333
334static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
Juergen Gross5584ea22017-02-09 14:39:57 +0100335 const char *path, const char *token)
Olaf Hering116df6f2011-08-25 18:34:45 +0200336{
Juergen Gross5584ea22017-02-09 14:39:57 +0100337 if (xenbus_scanf(XBT_NIL, path, "", "%i",
Jan Beulichc251f152016-10-24 09:05:18 -0600338 &backend_state) != 1)
339 backend_state = XenbusStateUnknown;
Olaf Hering116df6f2011-08-25 18:34:45 +0200340 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
Juergen Gross5584ea22017-02-09 14:39:57 +0100341 path, xenbus_strstate(backend_state));
Olaf Hering116df6f2011-08-25 18:34:45 +0200342 wake_up(&backend_state_wq);
343}
344
345static void xenbus_reset_wait_for_backend(char *be, int expected)
346{
347 long timeout;
348 timeout = wait_event_interruptible_timeout(backend_state_wq,
349 backend_state == expected, 5 * HZ);
350 if (timeout <= 0)
Joe Perches283c0972013-06-28 03:21:41 -0700351 pr_info("backend %s timed out\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200352}
353
354/*
355 * Reset frontend if it is in Connected or Closed state.
356 * Wait for backend to catch up.
357 * State Connected happens during kdump, Closed after kexec.
358 */
359static void xenbus_reset_frontend(char *fe, char *be, int be_state)
360{
361 struct xenbus_watch be_watch;
362
363 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
364 be, xenbus_strstate(be_state));
365
366 memset(&be_watch, 0, sizeof(be_watch));
367 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
368 if (!be_watch.node)
369 return;
370
371 be_watch.callback = xenbus_reset_backend_state_changed;
372 backend_state = XenbusStateUnknown;
373
Joe Perches283c0972013-06-28 03:21:41 -0700374 pr_info("triggering reconnect on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200375 register_xenbus_watch(&be_watch);
376
377 /* fall through to forward backend to state XenbusStateInitialising */
378 switch (be_state) {
379 case XenbusStateConnected:
380 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
381 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
Gustavo A. R. Silva5fa916f2017-11-02 13:41:07 -0500382 /* fall through */
Olaf Hering116df6f2011-08-25 18:34:45 +0200383
384 case XenbusStateClosing:
385 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
386 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
Gustavo A. R. Silva5fa916f2017-11-02 13:41:07 -0500387 /* fall through */
Olaf Hering116df6f2011-08-25 18:34:45 +0200388
389 case XenbusStateClosed:
390 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
391 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
392 }
393
394 unregister_xenbus_watch(&be_watch);
Joe Perches283c0972013-06-28 03:21:41 -0700395 pr_info("reconnect done on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200396 kfree(be_watch.node);
397}
398
399static void xenbus_check_frontend(char *class, char *dev)
400{
401 int be_state, fe_state, err;
402 char *backend, *frontend;
403
404 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
405 if (!frontend)
406 return;
407
408 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
409 if (err != 1)
410 goto out;
411
412 switch (fe_state) {
413 case XenbusStateConnected:
414 case XenbusStateClosed:
415 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
416 frontend, xenbus_strstate(fe_state));
417 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
418 if (!backend || IS_ERR(backend))
419 goto out;
420 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
421 if (err == 1)
422 xenbus_reset_frontend(frontend, backend, be_state);
423 kfree(backend);
424 break;
425 default:
426 break;
427 }
428out:
429 kfree(frontend);
430}
431
432static void xenbus_reset_state(void)
433{
434 char **devclass, **dev;
435 int devclass_n, dev_n;
436 int i, j;
437
438 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
439 if (IS_ERR(devclass))
440 return;
441
442 for (i = 0; i < devclass_n; i++) {
443 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
444 if (IS_ERR(dev))
445 continue;
446 for (j = 0; j < dev_n; j++)
447 xenbus_check_frontend(devclass[i], dev[j]);
448 kfree(dev);
449 }
450 kfree(devclass);
451}
452
Ian Campbelldf660252009-02-09 12:05:51 -0800453static int frontend_probe_and_watch(struct notifier_block *notifier,
454 unsigned long event,
455 void *data)
456{
Olaf Hering116df6f2011-08-25 18:34:45 +0200457 /* reset devices in Connected or Closed state */
458 if (xen_hvm_domain())
459 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800460 /* Enumerate devices in xenstore and watch for changes. */
461 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800462 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700463
Ian Campbelldf660252009-02-09 12:05:51 -0800464 return NOTIFY_DONE;
465}
466
467
Ian Campbell2de06cc2009-02-09 12:05:51 -0800468static int __init xenbus_probe_frontend_init(void)
469{
Ian Campbelldf660252009-02-09 12:05:51 -0800470 static struct notifier_block xenstore_notifier = {
471 .notifier_call = frontend_probe_and_watch
472 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800473 int err;
474
475 DPRINTK("");
476
477 /* Register ourselves with the kernel bus subsystem */
478 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700479 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800480 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800481
Ian Campbelldf660252009-02-09 12:05:51 -0800482 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800483
484 return 0;
485}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800486subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800487
488#ifndef MODULE
489static int __init boot_wait_for_devices(void)
490{
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -0500491 if (!xen_has_pv_devices())
Ian Campbell2de06cc2009-02-09 12:05:51 -0800492 return -ENODEV;
493
494 ready_to_wait_for_devices = 1;
495 wait_for_devices(NULL);
496 return 0;
497}
498
499late_initcall(boot_wait_for_devices);
500#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700501
502MODULE_LICENSE("GPL");