blob: 6ed8a9df4472edc36b91b70ca3a0f463f237108d [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 +010034static struct workqueue_struct *xenbus_frontend_wq;
35
Ian Campbell2de06cc2009-02-09 12:05:51 -080036/* device/<type>/<id> => <type>-<id> */
37static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
38{
39 nodename = strchr(nodename, '/');
40 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
Joe Perches283c0972013-06-28 03:21:41 -070041 pr_warn("bad frontend %s\n", nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -080042 return -EINVAL;
43 }
44
45 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
46 if (!strchr(bus_id, '/')) {
Joe Perches283c0972013-06-28 03:21:41 -070047 pr_warn("bus_id %s no slash\n", bus_id);
Ian Campbell2de06cc2009-02-09 12:05:51 -080048 return -EINVAL;
49 }
50 *strchr(bus_id, '/') = '-';
51 return 0;
52}
53
54/* device/<typename>/<name> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000055static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
56 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080057{
58 char *nodename;
59 int err;
60
Stefano Stabellini42c46e62012-03-13 18:30:44 +000061 /* ignore console/0 */
62 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
63 DPRINTK("Ignoring buggy device entry console/0");
64 return 0;
65 }
66
Ian Campbell2de06cc2009-02-09 12:05:51 -080067 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
68 if (!nodename)
69 return -ENOMEM;
70
71 DPRINTK("%s", nodename);
72
73 err = xenbus_probe_node(bus, type, nodename);
74 kfree(nodename);
75 return err;
76}
77
Ian Campbell6bac7f92010-12-10 14:39:15 +000078static int xenbus_uevent_frontend(struct device *_dev,
79 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080080{
81 struct xenbus_device *dev = to_xenbus_device(_dev);
82
83 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
84 return -ENOMEM;
85
86 return 0;
87}
88
89
Ian Campbell2de06cc2009-02-09 12:05:51 -080090static void backend_changed(struct xenbus_watch *watch,
91 const char **vec, unsigned int len)
92{
93 xenbus_otherend_changed(watch, vec, len, 1);
94}
95
Aurelien Chartier2abb2742013-05-28 18:09:56 +010096static void xenbus_frontend_delayed_resume(struct work_struct *w)
97{
98 struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
99
100 xenbus_dev_resume(&xdev->dev);
101}
102
103static int xenbus_frontend_dev_resume(struct device *dev)
104{
105 /*
106 * If xenstored is running in this domain, we cannot access the backend
107 * state at the moment, so we need to defer xenbus_dev_resume
108 */
109 if (xen_store_domain_type == XS_LOCAL) {
110 struct xenbus_device *xdev = to_xenbus_device(dev);
111
112 if (!xenbus_frontend_wq) {
113 pr_err("%s: no workqueue to process delayed resume\n",
114 xdev->nodename);
115 return -EFAULT;
116 }
117
118 INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
119 queue_work(xenbus_frontend_wq, &xdev->work);
120
121 return 0;
122 }
123
124 return xenbus_dev_resume(dev);
125}
126
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800127static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800128 .suspend = xenbus_dev_suspend,
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100129 .resume = xenbus_frontend_dev_resume,
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -0800130 .freeze = xenbus_dev_suspend,
131 .thaw = xenbus_dev_cancel,
132 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800133};
134
Ian Campbell2de06cc2009-02-09 12:05:51 -0800135static struct xen_bus_type xenbus_frontend = {
136 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000137 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -0800138 .get_bus_id = frontend_bus_id,
139 .probe = xenbus_probe_frontend,
140 .otherend_changed = backend_changed,
141 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000142 .name = "xen",
143 .match = xenbus_match,
144 .uevent = xenbus_uevent_frontend,
145 .probe = xenbus_dev_probe,
146 .remove = xenbus_dev_remove,
147 .shutdown = xenbus_dev_shutdown,
Bastian Blankcc85e932011-06-29 14:39:26 +0200148 .dev_attrs = xenbus_dev_attrs,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800149
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800150 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800151 },
152};
153
154static void frontend_changed(struct xenbus_watch *watch,
155 const char **vec, unsigned int len)
156{
157 DPRINTK("");
158
159 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
160}
161
162
163/* We watch for devices appearing and vanishing. */
164static struct xenbus_watch fe_watch = {
165 .node = "device",
166 .callback = frontend_changed,
167};
168
169static int read_backend_details(struct xenbus_device *xendev)
170{
171 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
172}
173
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400174static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800175{
176 struct xenbus_device *xendev = to_xenbus_device(dev);
177 struct device_driver *drv = data;
178 struct xenbus_driver *xendrv;
179
180 /*
181 * A device with no driver will never connect. We care only about
182 * devices which should currently be in the process of connecting.
183 */
184 if (!dev->driver)
185 return 0;
186
187 /* Is this search limited to a particular driver? */
188 if (drv && (dev->driver != drv))
189 return 0;
190
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400191 if (ignore_nonessential) {
192 /* With older QEMU, for PVonHVM guests the guest config files
193 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
194 * which is nonsensical as there is no PV FB (there can be
195 * a PVKB) running as HVM guest. */
196
197 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
198 return 0;
199
200 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
201 return 0;
202 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800203 xendrv = to_xenbus_driver(dev->driver);
204 return (xendev->state < XenbusStateConnected ||
205 (xendev->state == XenbusStateConnected &&
206 xendrv->is_ready && !xendrv->is_ready(xendev)));
207}
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400208static int essential_device_connecting(struct device *dev, void *data)
209{
210 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
211}
212static int non_essential_device_connecting(struct device *dev, void *data)
213{
214 return is_device_connecting(dev, data, false);
215}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800216
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400217static int exists_essential_connecting_device(struct device_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800218{
219 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400220 essential_device_connecting);
221}
222static int exists_non_essential_connecting_device(struct device_driver *drv)
223{
224 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
225 non_essential_device_connecting);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800226}
227
228static int print_device_status(struct device *dev, void *data)
229{
230 struct xenbus_device *xendev = to_xenbus_device(dev);
231 struct device_driver *drv = data;
232
233 /* Is this operation limited to a particular driver? */
234 if (drv && (dev->driver != drv))
235 return 0;
236
237 if (!dev->driver) {
238 /* Information only: is this too noisy? */
Joe Perches283c0972013-06-28 03:21:41 -0700239 pr_info("Device with no driver: %s\n", xendev->nodename);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800240 } else if (xendev->state < XenbusStateConnected) {
241 enum xenbus_state rstate = XenbusStateUnknown;
242 if (xendev->otherend)
243 rstate = xenbus_read_driver_state(xendev->otherend);
Joe Perches283c0972013-06-28 03:21:41 -0700244 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
245 xendev->nodename, xendev->state, rstate);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800246 }
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)
Joe Perches283c0972013-06-28 03:21:41 -0700259 pr_warn("Waiting for devices to initialise: ");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400260 *seconds_waited += 5;
Joe Perches283c0972013-06-28 03:21:41 -0700261 pr_cont("%us...", max_delay - *seconds_waited);
262 if (*seconds_waited == max_delay) {
263 pr_cont("\n");
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400264 return true;
Joe Perches283c0972013-06-28 03:21:41 -0700265 }
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400266 }
267
268 schedule_timeout_interruptible(HZ/10);
269
270 return false;
271}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800272/*
273 * On a 5-minute timeout, wait for all devices currently configured. We need
274 * to do this to guarantee that the filesystems and / or network devices
275 * needed for boot are available, before we can allow the boot to proceed.
276 *
277 * This needs to be on a late_initcall, to happen after the frontend device
278 * drivers have been initialised, but before the root fs is mounted.
279 *
280 * A possible improvement here would be to have the tools add a per-device
281 * flag to the store entry, indicating whether it is needed at boot time.
282 * This would allow people who knew what they were doing to accelerate their
283 * boot slightly, but of course needs tools or manual intervention to set up
284 * those flags correctly.
285 */
286static void wait_for_devices(struct xenbus_driver *xendrv)
287{
288 unsigned long start = jiffies;
289 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
290 unsigned int seconds_waited = 0;
291
292 if (!ready_to_wait_for_devices || !xen_domain())
293 return;
294
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400295 while (exists_non_essential_connecting_device(drv))
296 if (wait_loop(start, 30, &seconds_waited))
297 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800298
Konrad Rzeszutek Wilk30666162012-04-17 22:21:38 -0400299 /* Skips PVKB and PVFB check.*/
300 while (exists_essential_connecting_device(drv))
301 if (wait_loop(start, 270, &seconds_waited))
302 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800303
304 if (seconds_waited)
305 printk("\n");
306
307 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
308 print_device_status);
309}
310
Jan Beulich73db1442011-12-22 09:08:13 +0000311int xenbus_register_frontend(struct xenbus_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800312{
313 int ret;
314
315 drv->read_otherend_details = read_backend_details;
316
Jan Beulich73db1442011-12-22 09:08:13 +0000317 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800318 if (ret)
319 return ret;
320
321 /* If this driver is loaded as a module wait for devices to attach. */
322 wait_for_devices(drv);
323
324 return 0;
325}
Jan Beulich73db1442011-12-22 09:08:13 +0000326EXPORT_SYMBOL_GPL(xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800327
Olaf Hering116df6f2011-08-25 18:34:45 +0200328static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
329static int backend_state;
330
331static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
332 const char **v, unsigned int l)
333{
334 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
335 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
336 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
337 wake_up(&backend_state_wq);
338}
339
340static void xenbus_reset_wait_for_backend(char *be, int expected)
341{
342 long timeout;
343 timeout = wait_event_interruptible_timeout(backend_state_wq,
344 backend_state == expected, 5 * HZ);
345 if (timeout <= 0)
Joe Perches283c0972013-06-28 03:21:41 -0700346 pr_info("backend %s timed out\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200347}
348
349/*
350 * Reset frontend if it is in Connected or Closed state.
351 * Wait for backend to catch up.
352 * State Connected happens during kdump, Closed after kexec.
353 */
354static void xenbus_reset_frontend(char *fe, char *be, int be_state)
355{
356 struct xenbus_watch be_watch;
357
358 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
359 be, xenbus_strstate(be_state));
360
361 memset(&be_watch, 0, sizeof(be_watch));
362 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
363 if (!be_watch.node)
364 return;
365
366 be_watch.callback = xenbus_reset_backend_state_changed;
367 backend_state = XenbusStateUnknown;
368
Joe Perches283c0972013-06-28 03:21:41 -0700369 pr_info("triggering reconnect on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200370 register_xenbus_watch(&be_watch);
371
372 /* fall through to forward backend to state XenbusStateInitialising */
373 switch (be_state) {
374 case XenbusStateConnected:
375 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
376 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
377
378 case XenbusStateClosing:
379 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
380 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
381
382 case XenbusStateClosed:
383 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
384 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
385 }
386
387 unregister_xenbus_watch(&be_watch);
Joe Perches283c0972013-06-28 03:21:41 -0700388 pr_info("reconnect done on %s\n", be);
Olaf Hering116df6f2011-08-25 18:34:45 +0200389 kfree(be_watch.node);
390}
391
392static void xenbus_check_frontend(char *class, char *dev)
393{
394 int be_state, fe_state, err;
395 char *backend, *frontend;
396
397 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
398 if (!frontend)
399 return;
400
401 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
402 if (err != 1)
403 goto out;
404
405 switch (fe_state) {
406 case XenbusStateConnected:
407 case XenbusStateClosed:
408 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
409 frontend, xenbus_strstate(fe_state));
410 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
411 if (!backend || IS_ERR(backend))
412 goto out;
413 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
414 if (err == 1)
415 xenbus_reset_frontend(frontend, backend, be_state);
416 kfree(backend);
417 break;
418 default:
419 break;
420 }
421out:
422 kfree(frontend);
423}
424
425static void xenbus_reset_state(void)
426{
427 char **devclass, **dev;
428 int devclass_n, dev_n;
429 int i, j;
430
431 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
432 if (IS_ERR(devclass))
433 return;
434
435 for (i = 0; i < devclass_n; i++) {
436 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
437 if (IS_ERR(dev))
438 continue;
439 for (j = 0; j < dev_n; j++)
440 xenbus_check_frontend(devclass[i], dev[j]);
441 kfree(dev);
442 }
443 kfree(devclass);
444}
445
Ian Campbelldf660252009-02-09 12:05:51 -0800446static int frontend_probe_and_watch(struct notifier_block *notifier,
447 unsigned long event,
448 void *data)
449{
Olaf Hering116df6f2011-08-25 18:34:45 +0200450 /* reset devices in Connected or Closed state */
451 if (xen_hvm_domain())
452 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800453 /* Enumerate devices in xenstore and watch for changes. */
454 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800455 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700456
Ian Campbelldf660252009-02-09 12:05:51 -0800457 return NOTIFY_DONE;
458}
459
460
Ian Campbell2de06cc2009-02-09 12:05:51 -0800461static int __init xenbus_probe_frontend_init(void)
462{
Ian Campbelldf660252009-02-09 12:05:51 -0800463 static struct notifier_block xenstore_notifier = {
464 .notifier_call = frontend_probe_and_watch
465 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800466 int err;
467
468 DPRINTK("");
469
470 /* Register ourselves with the kernel bus subsystem */
471 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700472 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800473 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800474
Ian Campbelldf660252009-02-09 12:05:51 -0800475 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800476
Aurelien Chartier2abb2742013-05-28 18:09:56 +0100477 xenbus_frontend_wq = create_workqueue("xenbus_frontend");
478
Ian Campbell2de06cc2009-02-09 12:05:51 -0800479 return 0;
480}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800481subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800482
483#ifndef MODULE
484static int __init boot_wait_for_devices(void)
485{
486 if (xen_hvm_domain() && !xen_platform_pci_unplug)
487 return -ENODEV;
488
489 ready_to_wait_for_devices = 1;
490 wait_for_devices(NULL);
491 return 0;
492}
493
494late_initcall(boot_wait_for_devices);
495#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700496
497MODULE_LICENSE("GPL");