xenbus_probe_frontend.c 12.8 KB
Newer Older
J
Joe Perches 已提交
1 2 3 4 5
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#define DPRINTK(fmt, ...)				\
	pr_debug("(%s:%d) " fmt "\n",			\
		 __func__, __LINE__, ##__VA_ARGS__)
I
Ian Campbell 已提交
6 7 8 9 10 11 12 13 14 15 16 17

#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/notifier.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
#include <linux/io.h>
18
#include <linux/module.h>
I
Ian Campbell 已提交
19 20 21 22 23 24 25

#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/xen/hypervisor.h>
#include <xen/xenbus.h>
#include <xen/events.h>
#include <xen/page.h>
S
Stefano Stabellini 已提交
26
#include <xen/xen.h>
I
Ian Campbell 已提交
27 28 29

#include <xen/platform_pci.h>

30
#include "xenbus.h"
I
Ian Campbell 已提交
31 32


33

I
Ian Campbell 已提交
34 35 36 37 38
/* device/<type>/<id> => <type>-<id> */
static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
{
	nodename = strchr(nodename, '/');
	if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
J
Joe Perches 已提交
39
		pr_warn("bad frontend %s\n", nodename);
I
Ian Campbell 已提交
40 41 42 43 44
		return -EINVAL;
	}

	strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
	if (!strchr(bus_id, '/')) {
J
Joe Perches 已提交
45
		pr_warn("bus_id %s no slash\n", bus_id);
I
Ian Campbell 已提交
46 47 48 49 50 51 52
		return -EINVAL;
	}
	*strchr(bus_id, '/') = '-';
	return 0;
}

/* device/<typename>/<name> */
53 54
static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
				 const char *name)
I
Ian Campbell 已提交
55 56 57 58
{
	char *nodename;
	int err;

59 60 61 62 63 64
	/* ignore console/0 */
	if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
		DPRINTK("Ignoring buggy device entry console/0");
		return 0;
	}

I
Ian Campbell 已提交
65 66 67 68 69 70 71 72 73 74 75
	nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
	if (!nodename)
		return -ENOMEM;

	DPRINTK("%s", nodename);

	err = xenbus_probe_node(bus, type, nodename);
	kfree(nodename);
	return err;
}

76 77
static int xenbus_uevent_frontend(struct device *_dev,
				  struct kobj_uevent_env *env)
I
Ian Campbell 已提交
78 79 80 81 82 83 84 85 86 87
{
	struct xenbus_device *dev = to_xenbus_device(_dev);

	if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
		return -ENOMEM;

	return 0;
}


I
Ian Campbell 已提交
88
static void backend_changed(struct xenbus_watch *watch,
89
			    const char *path, const char *token)
I
Ian Campbell 已提交
90
{
91
	xenbus_otherend_changed(watch, path, token, 1);
I
Ian Campbell 已提交
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void xenbus_frontend_delayed_resume(struct work_struct *w)
{
	struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);

	xenbus_dev_resume(&xdev->dev);
}

static int xenbus_frontend_dev_resume(struct device *dev)
{
	/*
	 * If xenstored is running in this domain, we cannot access the backend
	 * state at the moment, so we need to defer xenbus_dev_resume
	 */
	if (xen_store_domain_type == XS_LOCAL) {
		struct xenbus_device *xdev = to_xenbus_device(dev);

110
		schedule_work(&xdev->work);
111 112 113 114 115 116 117

		return 0;
	}

	return xenbus_dev_resume(dev);
}

118 119 120 121 122 123 124 125 126 127
static int xenbus_frontend_dev_probe(struct device *dev)
{
	if (xen_store_domain_type == XS_LOCAL) {
		struct xenbus_device *xdev = to_xenbus_device(dev);
		INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
	}

	return xenbus_dev_probe(dev);
}

K
Kazuhiro SUZUKI 已提交
128
static const struct dev_pm_ops xenbus_pm_ops = {
129
	.suspend	= xenbus_dev_suspend,
130
	.resume		= xenbus_frontend_dev_resume,
131 132 133
	.freeze		= xenbus_dev_suspend,
	.thaw		= xenbus_dev_cancel,
	.restore	= xenbus_dev_resume,
K
Kazuhiro SUZUKI 已提交
134 135
};

I
Ian Campbell 已提交
136 137
static struct xen_bus_type xenbus_frontend = {
	.root = "device",
138
	.levels = 2,		/* device/type/<id> */
I
Ian Campbell 已提交
139 140 141 142
	.get_bus_id = frontend_bus_id,
	.probe = xenbus_probe_frontend,
	.otherend_changed = backend_changed,
	.bus = {
143 144 145
		.name		= "xen",
		.match		= xenbus_match,
		.uevent		= xenbus_uevent_frontend,
146
		.probe		= xenbus_frontend_dev_probe,
147 148
		.remove		= xenbus_dev_remove,
		.shutdown	= xenbus_dev_shutdown,
149
		.dev_groups	= xenbus_dev_groups,
150

K
Kazuhiro SUZUKI 已提交
151
		.pm		= &xenbus_pm_ops,
I
Ian Campbell 已提交
152 153 154 155
	},
};

static void frontend_changed(struct xenbus_watch *watch,
156
			     const char *path, const char *token)
I
Ian Campbell 已提交
157 158 159
{
	DPRINTK("");

160
	xenbus_dev_changed(path, &xenbus_frontend);
I
Ian Campbell 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
}


/* We watch for devices appearing and vanishing. */
static struct xenbus_watch fe_watch = {
	.node = "device",
	.callback = frontend_changed,
};

static int read_backend_details(struct xenbus_device *xendev)
{
	return xenbus_read_otherend_details(xendev, "backend-id", "backend");
}

175
static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
I
Ian Campbell 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
	struct xenbus_device *xendev = to_xenbus_device(dev);
	struct device_driver *drv = data;
	struct xenbus_driver *xendrv;

	/*
	 * A device with no driver will never connect. We care only about
	 * devices which should currently be in the process of connecting.
	 */
	if (!dev->driver)
		return 0;

	/* Is this search limited to a particular driver? */
	if (drv && (dev->driver != drv))
		return 0;

192 193 194 195 196 197 198 199 200 201 202 203
	if (ignore_nonessential) {
		/* With older QEMU, for PVonHVM guests the guest config files
		 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
		 * which is nonsensical as there is no PV FB (there can be
		 * a PVKB) running as HVM guest. */

		if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
			return 0;

		if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
			return 0;
	}
I
Ian Campbell 已提交
204 205 206 207 208
	xendrv = to_xenbus_driver(dev->driver);
	return (xendev->state < XenbusStateConnected ||
		(xendev->state == XenbusStateConnected &&
		 xendrv->is_ready && !xendrv->is_ready(xendev)));
}
209 210 211 212 213 214 215 216
static int essential_device_connecting(struct device *dev, void *data)
{
	return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
}
static int non_essential_device_connecting(struct device *dev, void *data)
{
	return is_device_connecting(dev, data, false);
}
I
Ian Campbell 已提交
217

218
static int exists_essential_connecting_device(struct device_driver *drv)
I
Ian Campbell 已提交
219 220
{
	return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
221 222 223 224 225 226
				essential_device_connecting);
}
static int exists_non_essential_connecting_device(struct device_driver *drv)
{
	return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
				non_essential_device_connecting);
I
Ian Campbell 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
}

static int print_device_status(struct device *dev, void *data)
{
	struct xenbus_device *xendev = to_xenbus_device(dev);
	struct device_driver *drv = data;

	/* Is this operation limited to a particular driver? */
	if (drv && (dev->driver != drv))
		return 0;

	if (!dev->driver) {
		/* Information only: is this too noisy? */
J
Joe Perches 已提交
240
		pr_info("Device with no driver: %s\n", xendev->nodename);
I
Ian Campbell 已提交
241 242 243 244
	} else if (xendev->state < XenbusStateConnected) {
		enum xenbus_state rstate = XenbusStateUnknown;
		if (xendev->otherend)
			rstate = xenbus_read_driver_state(xendev->otherend);
J
Joe Perches 已提交
245 246
		pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
			xendev->nodename, xendev->state, rstate);
I
Ian Campbell 已提交
247 248 249 250 251 252 253 254
	}

	return 0;
}

/* We only wait for device setup after most initcalls have run. */
static int ready_to_wait_for_devices;

255 256 257 258 259
static bool wait_loop(unsigned long start, unsigned int max_delay,
		     unsigned int *seconds_waited)
{
	if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
		if (!*seconds_waited)
J
Joe Perches 已提交
260
			pr_warn("Waiting for devices to initialise: ");
261
		*seconds_waited += 5;
J
Joe Perches 已提交
262 263 264
		pr_cont("%us...", max_delay - *seconds_waited);
		if (*seconds_waited == max_delay) {
			pr_cont("\n");
265
			return true;
J
Joe Perches 已提交
266
		}
267 268 269 270 271 272
	}

	schedule_timeout_interruptible(HZ/10);

	return false;
}
I
Ian Campbell 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/*
 * On a 5-minute timeout, wait for all devices currently configured.  We need
 * to do this to guarantee that the filesystems and / or network devices
 * needed for boot are available, before we can allow the boot to proceed.
 *
 * This needs to be on a late_initcall, to happen after the frontend device
 * drivers have been initialised, but before the root fs is mounted.
 *
 * A possible improvement here would be to have the tools add a per-device
 * flag to the store entry, indicating whether it is needed at boot time.
 * This would allow people who knew what they were doing to accelerate their
 * boot slightly, but of course needs tools or manual intervention to set up
 * those flags correctly.
 */
static void wait_for_devices(struct xenbus_driver *xendrv)
{
	unsigned long start = jiffies;
	struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
	unsigned int seconds_waited = 0;

	if (!ready_to_wait_for_devices || !xen_domain())
		return;

296 297 298 299 300 301 302 303
	while (exists_non_essential_connecting_device(drv))
		if (wait_loop(start, 30, &seconds_waited))
			break;

	/* Skips PVKB and PVFB check.*/
	while (exists_essential_connecting_device(drv))
		if (wait_loop(start, 270, &seconds_waited))
			break;
I
Ian Campbell 已提交
304 305 306 307 308 309 310 311

	if (seconds_waited)
		printk("\n");

	bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
			 print_device_status);
}

312 313
int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
			       const char *mod_name)
I
Ian Campbell 已提交
314 315 316 317 318
{
	int ret;

	drv->read_otherend_details = read_backend_details;

319 320
	ret = xenbus_register_driver_common(drv, &xenbus_frontend,
					    owner, mod_name);
I
Ian Campbell 已提交
321 322 323 324 325 326 327 328
	if (ret)
		return ret;

	/* If this driver is loaded as a module wait for devices to attach. */
	wait_for_devices(drv);

	return 0;
}
329
EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
I
Ian Campbell 已提交
330

331 332 333 334
static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
static int backend_state;

static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
335
					const char *path, const char *token)
336
{
337
	if (xenbus_scanf(XBT_NIL, path, "", "%i",
338 339
			 &backend_state) != 1)
		backend_state = XenbusStateUnknown;
340
	printk(KERN_DEBUG "XENBUS: backend %s %s\n",
341
	       path, xenbus_strstate(backend_state));
342 343 344 345 346 347 348 349 350
	wake_up(&backend_state_wq);
}

static void xenbus_reset_wait_for_backend(char *be, int expected)
{
	long timeout;
	timeout = wait_event_interruptible_timeout(backend_state_wq,
			backend_state == expected, 5 * HZ);
	if (timeout <= 0)
J
Joe Perches 已提交
351
		pr_info("backend %s timed out\n", be);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
}

/*
 * Reset frontend if it is in Connected or Closed state.
 * Wait for backend to catch up.
 * State Connected happens during kdump, Closed after kexec.
 */
static void xenbus_reset_frontend(char *fe, char *be, int be_state)
{
	struct xenbus_watch be_watch;

	printk(KERN_DEBUG "XENBUS: backend %s %s\n",
			be, xenbus_strstate(be_state));

	memset(&be_watch, 0, sizeof(be_watch));
	be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
	if (!be_watch.node)
		return;

	be_watch.callback = xenbus_reset_backend_state_changed;
	backend_state = XenbusStateUnknown;

J
Joe Perches 已提交
374
	pr_info("triggering reconnect on %s\n", be);
375 376 377 378 379 380 381
	register_xenbus_watch(&be_watch);

	/* fall through to forward backend to state XenbusStateInitialising */
	switch (be_state) {
	case XenbusStateConnected:
		xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
		xenbus_reset_wait_for_backend(be, XenbusStateClosing);
382
		/* fall through */
383 384 385 386

	case XenbusStateClosing:
		xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
		xenbus_reset_wait_for_backend(be, XenbusStateClosed);
387
		/* fall through */
388 389 390 391 392 393 394

	case XenbusStateClosed:
		xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
		xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
	}

	unregister_xenbus_watch(&be_watch);
J
Joe Perches 已提交
395
	pr_info("reconnect done on %s\n", be);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	kfree(be_watch.node);
}

static void xenbus_check_frontend(char *class, char *dev)
{
	int be_state, fe_state, err;
	char *backend, *frontend;

	frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
	if (!frontend)
		return;

	err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
	if (err != 1)
		goto out;

	switch (fe_state) {
	case XenbusStateConnected:
	case XenbusStateClosed:
		printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
				frontend, xenbus_strstate(fe_state));
		backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
		if (!backend || IS_ERR(backend))
			goto out;
		err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
		if (err == 1)
			xenbus_reset_frontend(frontend, backend, be_state);
		kfree(backend);
		break;
	default:
		break;
	}
out:
	kfree(frontend);
}

static void xenbus_reset_state(void)
{
	char **devclass, **dev;
	int devclass_n, dev_n;
	int i, j;

	devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
	if (IS_ERR(devclass))
		return;

	for (i = 0; i < devclass_n; i++) {
		dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
		if (IS_ERR(dev))
			continue;
		for (j = 0; j < dev_n; j++)
			xenbus_check_frontend(devclass[i], dev[j]);
		kfree(dev);
	}
	kfree(devclass);
}

I
Ian Campbell 已提交
453 454 455 456
static int frontend_probe_and_watch(struct notifier_block *notifier,
				   unsigned long event,
				   void *data)
{
457 458 459
	/* reset devices in Connected or Closed state */
	if (xen_hvm_domain())
		xenbus_reset_state();
I
Ian Campbell 已提交
460 461 462
	/* Enumerate devices in xenstore and watch for changes. */
	xenbus_probe_devices(&xenbus_frontend);
	register_xenbus_watch(&fe_watch);
463

I
Ian Campbell 已提交
464 465 466 467
	return NOTIFY_DONE;
}


I
Ian Campbell 已提交
468 469
static int __init xenbus_probe_frontend_init(void)
{
I
Ian Campbell 已提交
470 471 472
	static struct notifier_block xenstore_notifier = {
		.notifier_call = frontend_probe_and_watch
	};
I
Ian Campbell 已提交
473 474 475 476 477 478
	int err;

	DPRINTK("");

	/* Register ourselves with the kernel bus subsystem */
	err = bus_register(&xenbus_frontend.bus);
479
	if (err)
I
Ian Campbell 已提交
480 481
		return err;

I
Ian Campbell 已提交
482
	register_xenstore_notifier(&xenstore_notifier);
I
Ian Campbell 已提交
483 484 485

	return 0;
}
486
subsys_initcall(xenbus_probe_frontend_init);
I
Ian Campbell 已提交
487 488 489 490

#ifndef MODULE
static int __init boot_wait_for_devices(void)
{
491
	if (!xen_has_pv_devices())
I
Ian Campbell 已提交
492 493 494 495 496 497 498 499 500
		return -ENODEV;

	ready_to_wait_for_devices = 1;
	wait_for_devices(NULL);
	return 0;
}

late_initcall(boot_wait_for_devices);
#endif
501 502

MODULE_LICENSE("GPL");