| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 3 |
|
| 4 |
#define DPRINTK(fmt, ...) \ |
| 5 |
pr_debug("(%s:%d) " fmt "\n", \ |
| 6 |
__func__, __LINE__, ##__VA_ARGS__) |
| 7 |
|
| 8 |
#include <linux/kernel.h> |
| 9 |
#include <linux/err.h> |
| 10 |
#include <linux/string.h> |
| 11 |
#include <linux/ctype.h> |
| 12 |
#include <linux/fcntl.h> |
| 13 |
#include <linux/mm.h> |
| 14 |
#include <linux/proc_fs.h> |
| 15 |
#include <linux/notifier.h> |
| 16 |
#include <linux/kthread.h> |
| 17 |
#include <linux/mutex.h> |
| 18 |
#include <linux/io.h> |
| 19 |
#include <linux/module.h> |
| 20 |
|
| 21 |
#include <asm/page.h> |
| 22 |
#include <asm/xen/hypervisor.h> |
| 23 |
#include <xen/xenbus.h> |
| 24 |
#include <xen/events.h> |
| 25 |
#include <xen/page.h> |
| 26 |
#include <xen/xen.h> |
| 27 |
|
| 28 |
#include <xen/platform_pci.h> |
| 29 |
|
| 30 |
#include "xenbus.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/* device/<type>/<id> => <type>-<id> */ |
| 35 |
static 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 |
pr_warn("bad frontend %s\n", nodename); |
| 40 |
return -EINVAL; |
| 41 |
} |
| 42 |
|
| 43 |
strscpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); |
| 44 |
if (!strchr(bus_id, '/')) { |
| 45 |
pr_warn("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> */ |
| 53 |
static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, |
| 54 |
const char *name) |
| 55 |
{ |
| 56 |
char *nodename; |
| 57 |
int err; |
| 58 |
|
| 59 |
/* 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 |
|
| 65 |
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 |
|
| 76 |
static int xenbus_uevent_frontend(const struct device *_dev, |
| 77 |
struct kobj_uevent_env *env) |
| 78 |
{ |
| 79 |
const 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 |
|
| 88 |
static void backend_changed(struct xenbus_watch *watch, |
| 89 |
const char *path, const char *token) |
| 90 |
{ |
| 91 |
xenbus_otherend_changed(watch, path, token, 1); |
| 92 |
} |
| 93 |
|
| 94 |
static void xenbus_frontend_delayed_restore(struct work_struct *w) |
| 95 |
{ |
| 96 |
struct xenbus_device *xdev = container_of(w, struct xenbus_device, work); |
| 97 |
|
| 98 |
xenbus_dev_restore(&xdev->dev); |
| 99 |
} |
| 100 |
|
| 101 |
static int xenbus_frontend_dev_restore(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 |
schedule_work(&xdev->work); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
return xenbus_dev_restore(dev); |
| 116 |
} |
| 117 |
|
| 118 |
static 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_restore); |
| 123 |
} |
| 124 |
|
| 125 |
return xenbus_dev_probe(dev); |
| 126 |
} |
| 127 |
|
| 128 |
static void xenbus_frontend_dev_shutdown(struct device *_dev) |
| 129 |
{ |
| 130 |
struct xenbus_device *dev = to_xenbus_device(_dev); |
| 131 |
unsigned long timeout = 5*HZ; |
| 132 |
|
| 133 |
DPRINTK("%s", dev->nodename); |
| 134 |
|
| 135 |
get_device(&dev->dev); |
| 136 |
if (dev->state != XenbusStateConnected) { |
| 137 |
pr_info("%s: %s: %s != Connected, skipping\n", |
| 138 |
__func__, dev->nodename, xenbus_strstate(dev->state)); |
| 139 |
goto out; |
| 140 |
} |
| 141 |
xenbus_switch_state(dev, XenbusStateClosing); |
| 142 |
timeout = wait_for_completion_timeout(&dev->down, timeout); |
| 143 |
if (!timeout) |
| 144 |
pr_info("%s: %s timeout closing device\n", |
| 145 |
__func__, dev->nodename); |
| 146 |
out: |
| 147 |
put_device(&dev->dev); |
| 148 |
} |
| 149 |
|
| 150 |
static const struct dev_pm_ops xenbus_pm_ops = { |
| 151 |
.freeze = xenbus_dev_freeze, |
| 152 |
.thaw = xenbus_dev_thaw, |
| 153 |
.restore = xenbus_frontend_dev_restore, |
| 154 |
}; |
| 155 |
|
| 156 |
static struct xen_bus_type xenbus_frontend = { |
| 157 |
.root = "device", |
| 158 |
.levels = 2, /* device/type/<id> */ |
| 159 |
.get_bus_id = frontend_bus_id, |
| 160 |
.probe = xenbus_probe_frontend, |
| 161 |
.otherend_changed = backend_changed, |
| 162 |
.bus = { |
| 163 |
.name = "xen", |
| 164 |
.match = xenbus_match, |
| 165 |
.uevent = xenbus_uevent_frontend, |
| 166 |
.probe = xenbus_frontend_dev_probe, |
| 167 |
.remove = xenbus_dev_remove, |
| 168 |
.shutdown = xenbus_frontend_dev_shutdown, |
| 169 |
.dev_groups = xenbus_dev_groups, |
| 170 |
|
| 171 |
.pm = &xenbus_pm_ops, |
| 172 |
}, |
| 173 |
}; |
| 174 |
|
| 175 |
static void frontend_changed(struct xenbus_watch *watch, |
| 176 |
const char *path, const char *token) |
| 177 |
{ |
| 178 |
DPRINTK(""); |
| 179 |
|
| 180 |
xenbus_dev_changed(path, &xenbus_frontend); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/* We watch for devices appearing and vanishing. */ |
| 185 |
static struct xenbus_watch fe_watch = { |
| 186 |
.node = "device", |
| 187 |
.callback = frontend_changed, |
| 188 |
}; |
| 189 |
|
| 190 |
static int read_backend_details(struct xenbus_device *xendev) |
| 191 |
{ |
| 192 |
return xenbus_read_otherend_details(xendev, "backend-id", "backend"); |
| 193 |
} |
| 194 |
|
| 195 |
static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential) |
| 196 |
{ |
| 197 |
struct xenbus_device *xendev = to_xenbus_device(dev); |
| 198 |
struct device_driver *drv = data; |
| 199 |
struct xenbus_driver *xendrv; |
| 200 |
|
| 201 |
/* |
| 202 |
* A device with no driver will never connect. We care only about |
| 203 |
* devices which should currently be in the process of connecting. |
| 204 |
*/ |
| 205 |
if (!dev->driver) |
| 206 |
return 0; |
| 207 |
|
| 208 |
/* Is this search limited to a particular driver? */ |
| 209 |
if (drv && (dev->driver != drv)) |
| 210 |
return 0; |
| 211 |
|
| 212 |
xendrv = to_xenbus_driver(dev->driver); |
| 213 |
|
| 214 |
if (ignore_nonessential && xendrv->not_essential) |
| 215 |
return 0; |
| 216 |
|
| 217 |
return (xendev->state < XenbusStateConnected || |
| 218 |
(xendev->state == XenbusStateConnected && |
| 219 |
xendrv->is_ready && !xendrv->is_ready(xendev))); |
| 220 |
} |
| 221 |
static int essential_device_connecting(struct device *dev, void *data) |
| 222 |
{ |
| 223 |
return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */); |
| 224 |
} |
| 225 |
static int non_essential_device_connecting(struct device *dev, void *data) |
| 226 |
{ |
| 227 |
return is_device_connecting(dev, data, false); |
| 228 |
} |
| 229 |
|
| 230 |
static int exists_essential_connecting_device(struct device_driver *drv) |
| 231 |
{ |
| 232 |
return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 233 |
essential_device_connecting); |
| 234 |
} |
| 235 |
static int exists_non_essential_connecting_device(struct device_driver *drv) |
| 236 |
{ |
| 237 |
return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 238 |
non_essential_device_connecting); |
| 239 |
} |
| 240 |
|
| 241 |
static int print_device_status(struct device *dev, void *data) |
| 242 |
{ |
| 243 |
struct xenbus_device *xendev = to_xenbus_device(dev); |
| 244 |
struct device_driver *drv = data; |
| 245 |
|
| 246 |
/* Is this operation limited to a particular driver? */ |
| 247 |
if (drv && (dev->driver != drv)) |
| 248 |
return 0; |
| 249 |
|
| 250 |
if (!dev->driver) { |
| 251 |
/* Information only: is this too noisy? */ |
| 252 |
pr_info("Device with no driver: %s\n", xendev->nodename); |
| 253 |
} else if (xendev->state < XenbusStateConnected) { |
| 254 |
enum xenbus_state rstate = XenbusStateUnknown; |
| 255 |
if (xendev->otherend) |
| 256 |
rstate = xenbus_read_driver_state(xendev, xendev->otherend); |
| 257 |
pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n", |
| 258 |
xendev->nodename, xendev->state, rstate); |
| 259 |
} |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
/* We only wait for device setup after most initcalls have run. */ |
| 265 |
static int ready_to_wait_for_devices; |
| 266 |
|
| 267 |
static bool wait_loop(unsigned long start, unsigned int max_delay, |
| 268 |
unsigned int *seconds_waited) |
| 269 |
{ |
| 270 |
if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) { |
| 271 |
if (!*seconds_waited) |
| 272 |
pr_warn("Waiting for devices to initialise: "); |
| 273 |
*seconds_waited += 5; |
| 274 |
pr_cont("%us...", max_delay - *seconds_waited); |
| 275 |
if (*seconds_waited == max_delay) { |
| 276 |
pr_cont("\n"); |
| 277 |
return true; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
schedule_timeout_interruptible(HZ/10); |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
/* |
| 286 |
* On a 5-minute timeout, wait for all devices currently configured. We need |
| 287 |
* to do this to guarantee that the filesystems and / or network devices |
| 288 |
* needed for boot are available, before we can allow the boot to proceed. |
| 289 |
* |
| 290 |
* This needs to be on a late_initcall, to happen after the frontend device |
| 291 |
* drivers have been initialised, but before the root fs is mounted. |
| 292 |
* |
| 293 |
* A possible improvement here would be to have the tools add a per-device |
| 294 |
* flag to the store entry, indicating whether it is needed at boot time. |
| 295 |
* This would allow people who knew what they were doing to accelerate their |
| 296 |
* boot slightly, but of course needs tools or manual intervention to set up |
| 297 |
* those flags correctly. |
| 298 |
*/ |
| 299 |
static void wait_for_devices(struct xenbus_driver *xendrv) |
| 300 |
{ |
| 301 |
unsigned long start = jiffies; |
| 302 |
struct device_driver *drv = xendrv ? &xendrv->driver : NULL; |
| 303 |
unsigned int seconds_waited = 0; |
| 304 |
|
| 305 |
if (!ready_to_wait_for_devices || !xen_domain()) |
| 306 |
return; |
| 307 |
|
| 308 |
while (exists_non_essential_connecting_device(drv)) |
| 309 |
if (wait_loop(start, 30, &seconds_waited)) |
| 310 |
break; |
| 311 |
|
| 312 |
/* Skips PVKB and PVFB check.*/ |
| 313 |
while (exists_essential_connecting_device(drv)) |
| 314 |
if (wait_loop(start, 270, &seconds_waited)) |
| 315 |
break; |
| 316 |
|
| 317 |
if (seconds_waited) |
| 318 |
printk("\n"); |
| 319 |
|
| 320 |
bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 321 |
print_device_status); |
| 322 |
} |
| 323 |
|
| 324 |
int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner, |
| 325 |
const char *mod_name) |
| 326 |
{ |
| 327 |
int ret; |
| 328 |
|
| 329 |
drv->read_otherend_details = read_backend_details; |
| 330 |
|
| 331 |
ret = xenbus_register_driver_common(drv, &xenbus_frontend, |
| 332 |
owner, mod_name); |
| 333 |
if (ret) |
| 334 |
return ret; |
| 335 |
|
| 336 |
/* If this driver is loaded as a module wait for devices to attach. */ |
| 337 |
wait_for_devices(drv); |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
EXPORT_SYMBOL_GPL(__xenbus_register_frontend); |
| 342 |
|
| 343 |
static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq); |
| 344 |
static int backend_state; |
| 345 |
|
| 346 |
static void xenbus_reset_backend_state_changed(struct xenbus_watch *w, |
| 347 |
const char *path, const char *token) |
| 348 |
{ |
| 349 |
if (xenbus_scanf(XBT_NIL, path, "", "%i", |
| 350 |
&backend_state) != 1) |
| 351 |
backend_state = XenbusStateUnknown; |
| 352 |
printk(KERN_DEBUG "XENBUS: backend %s %s\n", |
| 353 |
path, xenbus_strstate(backend_state)); |
| 354 |
wake_up(&backend_state_wq); |
| 355 |
} |
| 356 |
|
| 357 |
static void xenbus_reset_wait_for_backend(char *be, int expected) |
| 358 |
{ |
| 359 |
long timeout; |
| 360 |
timeout = wait_event_interruptible_timeout(backend_state_wq, |
| 361 |
backend_state == expected, 5 * HZ); |
| 362 |
if (timeout <= 0) |
| 363 |
pr_info("backend %s timed out\n", be); |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* Reset frontend if it is in Connected or Closed state. |
| 368 |
* Wait for backend to catch up. |
| 369 |
* State Connected happens during kdump, Closed after kexec. |
| 370 |
*/ |
| 371 |
static void xenbus_reset_frontend(char *fe, char *be, int be_state) |
| 372 |
{ |
| 373 |
struct xenbus_watch be_watch; |
| 374 |
|
| 375 |
printk(KERN_DEBUG "XENBUS: backend %s %s\n", |
| 376 |
be, xenbus_strstate(be_state)); |
| 377 |
|
| 378 |
memset(&be_watch, 0, sizeof(be_watch)); |
| 379 |
be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be); |
| 380 |
if (!be_watch.node) |
| 381 |
return; |
| 382 |
|
| 383 |
be_watch.callback = xenbus_reset_backend_state_changed; |
| 384 |
backend_state = XenbusStateUnknown; |
| 385 |
|
| 386 |
pr_info("triggering reconnect on %s\n", be); |
| 387 |
register_xenbus_watch(&be_watch); |
| 388 |
|
| 389 |
/* fall through to forward backend to state XenbusStateInitialising */ |
| 390 |
switch (be_state) { |
| 391 |
case XenbusStateConnected: |
| 392 |
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing); |
| 393 |
xenbus_reset_wait_for_backend(be, XenbusStateClosing); |
| 394 |
fallthrough; |
| 395 |
|
| 396 |
case XenbusStateClosing: |
| 397 |
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed); |
| 398 |
xenbus_reset_wait_for_backend(be, XenbusStateClosed); |
| 399 |
fallthrough; |
| 400 |
|
| 401 |
case XenbusStateClosed: |
| 402 |
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising); |
| 403 |
xenbus_reset_wait_for_backend(be, XenbusStateInitWait); |
| 404 |
} |
| 405 |
|
| 406 |
unregister_xenbus_watch(&be_watch); |
| 407 |
pr_info("reconnect done on %s\n", be); |
| 408 |
kfree(be_watch.node); |
| 409 |
} |
| 410 |
|
| 411 |
static void xenbus_check_frontend(char *class, char *dev) |
| 412 |
{ |
| 413 |
int be_state, fe_state, err; |
| 414 |
char *backend, *frontend; |
| 415 |
|
| 416 |
frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev); |
| 417 |
if (!frontend) |
| 418 |
return; |
| 419 |
|
| 420 |
err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state); |
| 421 |
if (err != 1) |
| 422 |
goto out; |
| 423 |
|
| 424 |
switch (fe_state) { |
| 425 |
case XenbusStateConnected: |
| 426 |
case XenbusStateClosed: |
| 427 |
printk(KERN_DEBUG "XENBUS: frontend %s %s\n", |
| 428 |
frontend, xenbus_strstate(fe_state)); |
| 429 |
backend = xenbus_read(XBT_NIL, frontend, "backend", NULL); |
| 430 |
if (IS_ERR_OR_NULL(backend)) |
| 431 |
goto out; |
| 432 |
err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state); |
| 433 |
if (err == 1) |
| 434 |
xenbus_reset_frontend(frontend, backend, be_state); |
| 435 |
kfree(backend); |
| 436 |
break; |
| 437 |
default: |
| 438 |
break; |
| 439 |
} |
| 440 |
out: |
| 441 |
kfree(frontend); |
| 442 |
} |
| 443 |
|
| 444 |
static void xenbus_reset_state(void) |
| 445 |
{ |
| 446 |
char **devclass, **dev; |
| 447 |
int devclass_n, dev_n; |
| 448 |
int i, j; |
| 449 |
|
| 450 |
devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n); |
| 451 |
if (IS_ERR(devclass)) |
| 452 |
return; |
| 453 |
|
| 454 |
for (i = 0; i < devclass_n; i++) { |
| 455 |
dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n); |
| 456 |
if (IS_ERR(dev)) |
| 457 |
continue; |
| 458 |
for (j = 0; j < dev_n; j++) |
| 459 |
xenbus_check_frontend(devclass[i], dev[j]); |
| 460 |
kfree(dev); |
| 461 |
} |
| 462 |
kfree(devclass); |
| 463 |
} |
| 464 |
|
| 465 |
static int frontend_probe_and_watch(struct notifier_block *notifier, |
| 466 |
unsigned long event, |
| 467 |
void *data) |
| 468 |
{ |
| 469 |
/* reset devices in Connected or Closed state */ |
| 470 |
if (xen_hvm_domain()) |
| 471 |
xenbus_reset_state(); |
| 472 |
/* Enumerate devices in xenstore and watch for changes. */ |
| 473 |
xenbus_probe_devices(&xenbus_frontend); |
| 474 |
register_xenbus_watch(&fe_watch); |
| 475 |
|
| 476 |
return NOTIFY_DONE; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
static int __init xenbus_probe_frontend_init(void) |
| 481 |
{ |
| 482 |
static struct notifier_block xenstore_notifier = { |
| 483 |
.notifier_call = frontend_probe_and_watch |
| 484 |
}; |
| 485 |
int err; |
| 486 |
|
| 487 |
DPRINTK(""); |
| 488 |
|
| 489 |
/* Register ourselves with the kernel bus subsystem */ |
| 490 |
err = bus_register(&xenbus_frontend.bus); |
| 491 |
if (err) |
| 492 |
return err; |
| 493 |
|
| 494 |
register_xenstore_notifier(&xenstore_notifier); |
| 495 |
|
| 496 |
return 0; |
| 497 |
} |
| 498 |
subsys_initcall(xenbus_probe_frontend_init); |
| 499 |
|
| 500 |
#ifndef MODULE |
| 501 |
static int __init boot_wait_for_devices(void) |
| 502 |
{ |
| 503 |
if (!xen_has_pv_devices()) |
| 504 |
return -ENODEV; |
| 505 |
|
| 506 |
ready_to_wait_for_devices = 1; |
| 507 |
wait_for_devices(NULL); |
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
late_initcall(boot_wait_for_devices); |
| 512 |
#endif |
| 513 |
|
| 514 |
MODULE_DESCRIPTION("Xen PV-device frontend support"); |
| 515 |
MODULE_LICENSE("GPL"); |