Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 1 | #define DPRINTK(fmt, args...) \ |
| 2 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ |
| 3 | __func__, __LINE__, ##args) |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/ctype.h> |
| 9 | #include <linux/fcntl.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/proc_fs.h> |
| 12 | #include <linux/notifier.h> |
| 13 | #include <linux/kthread.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/io.h> |
Paul Gortmaker | 72ee511 | 2011-07-03 16:20:57 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 17 | |
| 18 | #include <asm/page.h> |
| 19 | #include <asm/pgtable.h> |
| 20 | #include <asm/xen/hypervisor.h> |
| 21 | #include <xen/xenbus.h> |
| 22 | #include <xen/events.h> |
| 23 | #include <xen/page.h> |
| 24 | |
| 25 | #include <xen/platform_pci.h> |
| 26 | |
| 27 | #include "xenbus_comms.h" |
| 28 | #include "xenbus_probe.h" |
| 29 | |
| 30 | |
| 31 | /* device/<type>/<id> => <type>-<id> */ |
| 32 | static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) |
| 33 | { |
| 34 | nodename = strchr(nodename, '/'); |
| 35 | if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { |
| 36 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); |
| 37 | return -EINVAL; |
| 38 | } |
| 39 | |
| 40 | strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); |
| 41 | if (!strchr(bus_id, '/')) { |
| 42 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); |
| 43 | return -EINVAL; |
| 44 | } |
| 45 | *strchr(bus_id, '/') = '-'; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | /* device/<typename>/<name> */ |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 50 | static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, |
| 51 | const char *name) |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 52 | { |
| 53 | char *nodename; |
| 54 | int err; |
| 55 | |
Stefano Stabellini | 42c46e6 | 2012-03-13 18:30:44 +0000 | [diff] [blame] | 56 | /* ignore console/0 */ |
| 57 | if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) { |
| 58 | DPRINTK("Ignoring buggy device entry console/0"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 62 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name); |
| 63 | if (!nodename) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | DPRINTK("%s", nodename); |
| 67 | |
| 68 | err = xenbus_probe_node(bus, type, nodename); |
| 69 | kfree(nodename); |
| 70 | return err; |
| 71 | } |
| 72 | |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 73 | static int xenbus_uevent_frontend(struct device *_dev, |
| 74 | struct kobj_uevent_env *env) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 75 | { |
| 76 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 77 | |
| 78 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 85 | static void backend_changed(struct xenbus_watch *watch, |
| 86 | const char **vec, unsigned int len) |
| 87 | { |
| 88 | xenbus_otherend_changed(watch, vec, len, 1); |
| 89 | } |
| 90 | |
Kazuhiro SUZUKI | c7853ae | 2011-02-18 14:43:07 -0800 | [diff] [blame] | 91 | static const struct dev_pm_ops xenbus_pm_ops = { |
Shriram Rajagopalan | b3e96c0 | 2011-02-22 14:59:06 -0800 | [diff] [blame] | 92 | .suspend = xenbus_dev_suspend, |
| 93 | .resume = xenbus_dev_resume, |
| 94 | .freeze = xenbus_dev_suspend, |
| 95 | .thaw = xenbus_dev_cancel, |
| 96 | .restore = xenbus_dev_resume, |
Kazuhiro SUZUKI | c7853ae | 2011-02-18 14:43:07 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 99 | static struct xen_bus_type xenbus_frontend = { |
| 100 | .root = "device", |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 101 | .levels = 2, /* device/type/<id> */ |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 102 | .get_bus_id = frontend_bus_id, |
| 103 | .probe = xenbus_probe_frontend, |
| 104 | .otherend_changed = backend_changed, |
| 105 | .bus = { |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 106 | .name = "xen", |
| 107 | .match = xenbus_match, |
| 108 | .uevent = xenbus_uevent_frontend, |
| 109 | .probe = xenbus_dev_probe, |
| 110 | .remove = xenbus_dev_remove, |
| 111 | .shutdown = xenbus_dev_shutdown, |
Bastian Blank | cc85e93 | 2011-06-29 14:39:26 +0200 | [diff] [blame] | 112 | .dev_attrs = xenbus_dev_attrs, |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 113 | |
Kazuhiro SUZUKI | c7853ae | 2011-02-18 14:43:07 -0800 | [diff] [blame] | 114 | .pm = &xenbus_pm_ops, |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 115 | }, |
| 116 | }; |
| 117 | |
| 118 | static void frontend_changed(struct xenbus_watch *watch, |
| 119 | const char **vec, unsigned int len) |
| 120 | { |
| 121 | DPRINTK(""); |
| 122 | |
| 123 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* We watch for devices appearing and vanishing. */ |
| 128 | static struct xenbus_watch fe_watch = { |
| 129 | .node = "device", |
| 130 | .callback = frontend_changed, |
| 131 | }; |
| 132 | |
| 133 | static int read_backend_details(struct xenbus_device *xendev) |
| 134 | { |
| 135 | return xenbus_read_otherend_details(xendev, "backend-id", "backend"); |
| 136 | } |
| 137 | |
| 138 | static int is_device_connecting(struct device *dev, void *data) |
| 139 | { |
| 140 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 141 | struct device_driver *drv = data; |
| 142 | struct xenbus_driver *xendrv; |
| 143 | |
| 144 | /* |
| 145 | * A device with no driver will never connect. We care only about |
| 146 | * devices which should currently be in the process of connecting. |
| 147 | */ |
| 148 | if (!dev->driver) |
| 149 | return 0; |
| 150 | |
| 151 | /* Is this search limited to a particular driver? */ |
| 152 | if (drv && (dev->driver != drv)) |
| 153 | return 0; |
| 154 | |
| 155 | xendrv = to_xenbus_driver(dev->driver); |
| 156 | return (xendev->state < XenbusStateConnected || |
| 157 | (xendev->state == XenbusStateConnected && |
| 158 | xendrv->is_ready && !xendrv->is_ready(xendev))); |
| 159 | } |
| 160 | |
| 161 | static int exists_connecting_device(struct device_driver *drv) |
| 162 | { |
| 163 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 164 | is_device_connecting); |
| 165 | } |
| 166 | |
| 167 | static int print_device_status(struct device *dev, void *data) |
| 168 | { |
| 169 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 170 | struct device_driver *drv = data; |
| 171 | |
| 172 | /* Is this operation limited to a particular driver? */ |
| 173 | if (drv && (dev->driver != drv)) |
| 174 | return 0; |
| 175 | |
| 176 | if (!dev->driver) { |
| 177 | /* Information only: is this too noisy? */ |
| 178 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", |
| 179 | xendev->nodename); |
| 180 | } else if (xendev->state < XenbusStateConnected) { |
| 181 | enum xenbus_state rstate = XenbusStateUnknown; |
| 182 | if (xendev->otherend) |
| 183 | rstate = xenbus_read_driver_state(xendev->otherend); |
| 184 | printk(KERN_WARNING "XENBUS: Timeout connecting " |
| 185 | "to device: %s (local state %d, remote state %d)\n", |
| 186 | xendev->nodename, xendev->state, rstate); |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* We only wait for device setup after most initcalls have run. */ |
| 193 | static int ready_to_wait_for_devices; |
| 194 | |
| 195 | /* |
| 196 | * On a 5-minute timeout, wait for all devices currently configured. We need |
| 197 | * to do this to guarantee that the filesystems and / or network devices |
| 198 | * needed for boot are available, before we can allow the boot to proceed. |
| 199 | * |
| 200 | * This needs to be on a late_initcall, to happen after the frontend device |
| 201 | * drivers have been initialised, but before the root fs is mounted. |
| 202 | * |
| 203 | * A possible improvement here would be to have the tools add a per-device |
| 204 | * flag to the store entry, indicating whether it is needed at boot time. |
| 205 | * This would allow people who knew what they were doing to accelerate their |
| 206 | * boot slightly, but of course needs tools or manual intervention to set up |
| 207 | * those flags correctly. |
| 208 | */ |
| 209 | static void wait_for_devices(struct xenbus_driver *xendrv) |
| 210 | { |
| 211 | unsigned long start = jiffies; |
| 212 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; |
| 213 | unsigned int seconds_waited = 0; |
| 214 | |
| 215 | if (!ready_to_wait_for_devices || !xen_domain()) |
| 216 | return; |
| 217 | |
| 218 | while (exists_connecting_device(drv)) { |
| 219 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { |
| 220 | if (!seconds_waited) |
| 221 | printk(KERN_WARNING "XENBUS: Waiting for " |
| 222 | "devices to initialise: "); |
| 223 | seconds_waited += 5; |
| 224 | printk("%us...", 300 - seconds_waited); |
| 225 | if (seconds_waited == 300) |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | schedule_timeout_interruptible(HZ/10); |
| 230 | } |
| 231 | |
| 232 | if (seconds_waited) |
| 233 | printk("\n"); |
| 234 | |
| 235 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 236 | print_device_status); |
| 237 | } |
| 238 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 239 | int xenbus_register_frontend(struct xenbus_driver *drv) |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 240 | { |
| 241 | int ret; |
| 242 | |
| 243 | drv->read_otherend_details = read_backend_details; |
| 244 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 245 | ret = xenbus_register_driver_common(drv, &xenbus_frontend); |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | /* If this driver is loaded as a module wait for devices to attach. */ |
| 250 | wait_for_devices(drv); |
| 251 | |
| 252 | return 0; |
| 253 | } |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(xenbus_register_frontend); |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 255 | |
Olaf Hering | 116df6f | 2011-08-25 18:34:45 +0200 | [diff] [blame] | 256 | static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq); |
| 257 | static int backend_state; |
| 258 | |
| 259 | static void xenbus_reset_backend_state_changed(struct xenbus_watch *w, |
| 260 | const char **v, unsigned int l) |
| 261 | { |
| 262 | xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state); |
| 263 | printk(KERN_DEBUG "XENBUS: backend %s %s\n", |
| 264 | v[XS_WATCH_PATH], xenbus_strstate(backend_state)); |
| 265 | wake_up(&backend_state_wq); |
| 266 | } |
| 267 | |
| 268 | static void xenbus_reset_wait_for_backend(char *be, int expected) |
| 269 | { |
| 270 | long timeout; |
| 271 | timeout = wait_event_interruptible_timeout(backend_state_wq, |
| 272 | backend_state == expected, 5 * HZ); |
| 273 | if (timeout <= 0) |
| 274 | printk(KERN_INFO "XENBUS: backend %s timed out.\n", be); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Reset frontend if it is in Connected or Closed state. |
| 279 | * Wait for backend to catch up. |
| 280 | * State Connected happens during kdump, Closed after kexec. |
| 281 | */ |
| 282 | static void xenbus_reset_frontend(char *fe, char *be, int be_state) |
| 283 | { |
| 284 | struct xenbus_watch be_watch; |
| 285 | |
| 286 | printk(KERN_DEBUG "XENBUS: backend %s %s\n", |
| 287 | be, xenbus_strstate(be_state)); |
| 288 | |
| 289 | memset(&be_watch, 0, sizeof(be_watch)); |
| 290 | be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be); |
| 291 | if (!be_watch.node) |
| 292 | return; |
| 293 | |
| 294 | be_watch.callback = xenbus_reset_backend_state_changed; |
| 295 | backend_state = XenbusStateUnknown; |
| 296 | |
| 297 | printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be); |
| 298 | register_xenbus_watch(&be_watch); |
| 299 | |
| 300 | /* fall through to forward backend to state XenbusStateInitialising */ |
| 301 | switch (be_state) { |
| 302 | case XenbusStateConnected: |
| 303 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing); |
| 304 | xenbus_reset_wait_for_backend(be, XenbusStateClosing); |
| 305 | |
| 306 | case XenbusStateClosing: |
| 307 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed); |
| 308 | xenbus_reset_wait_for_backend(be, XenbusStateClosed); |
| 309 | |
| 310 | case XenbusStateClosed: |
| 311 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising); |
| 312 | xenbus_reset_wait_for_backend(be, XenbusStateInitWait); |
| 313 | } |
| 314 | |
| 315 | unregister_xenbus_watch(&be_watch); |
| 316 | printk(KERN_INFO "XENBUS: reconnect done on %s\n", be); |
| 317 | kfree(be_watch.node); |
| 318 | } |
| 319 | |
| 320 | static void xenbus_check_frontend(char *class, char *dev) |
| 321 | { |
| 322 | int be_state, fe_state, err; |
| 323 | char *backend, *frontend; |
| 324 | |
| 325 | frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev); |
| 326 | if (!frontend) |
| 327 | return; |
| 328 | |
| 329 | err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state); |
| 330 | if (err != 1) |
| 331 | goto out; |
| 332 | |
| 333 | switch (fe_state) { |
| 334 | case XenbusStateConnected: |
| 335 | case XenbusStateClosed: |
| 336 | printk(KERN_DEBUG "XENBUS: frontend %s %s\n", |
| 337 | frontend, xenbus_strstate(fe_state)); |
| 338 | backend = xenbus_read(XBT_NIL, frontend, "backend", NULL); |
| 339 | if (!backend || IS_ERR(backend)) |
| 340 | goto out; |
| 341 | err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state); |
| 342 | if (err == 1) |
| 343 | xenbus_reset_frontend(frontend, backend, be_state); |
| 344 | kfree(backend); |
| 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | out: |
| 350 | kfree(frontend); |
| 351 | } |
| 352 | |
| 353 | static void xenbus_reset_state(void) |
| 354 | { |
| 355 | char **devclass, **dev; |
| 356 | int devclass_n, dev_n; |
| 357 | int i, j; |
| 358 | |
| 359 | devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n); |
| 360 | if (IS_ERR(devclass)) |
| 361 | return; |
| 362 | |
| 363 | for (i = 0; i < devclass_n; i++) { |
| 364 | dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n); |
| 365 | if (IS_ERR(dev)) |
| 366 | continue; |
| 367 | for (j = 0; j < dev_n; j++) |
| 368 | xenbus_check_frontend(devclass[i], dev[j]); |
| 369 | kfree(dev); |
| 370 | } |
| 371 | kfree(devclass); |
| 372 | } |
| 373 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 374 | static int frontend_probe_and_watch(struct notifier_block *notifier, |
| 375 | unsigned long event, |
| 376 | void *data) |
| 377 | { |
Olaf Hering | 116df6f | 2011-08-25 18:34:45 +0200 | [diff] [blame] | 378 | /* reset devices in Connected or Closed state */ |
| 379 | if (xen_hvm_domain()) |
| 380 | xenbus_reset_state(); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 381 | /* Enumerate devices in xenstore and watch for changes. */ |
| 382 | xenbus_probe_devices(&xenbus_frontend); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 383 | register_xenbus_watch(&fe_watch); |
Jeremy Fitzhardinge | 0ff4fdf | 2010-03-29 14:38:54 -0700 | [diff] [blame] | 384 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 385 | return NOTIFY_DONE; |
| 386 | } |
| 387 | |
| 388 | |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 389 | static int __init xenbus_probe_frontend_init(void) |
| 390 | { |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 391 | static struct notifier_block xenstore_notifier = { |
| 392 | .notifier_call = frontend_probe_and_watch |
| 393 | }; |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 394 | int err; |
| 395 | |
| 396 | DPRINTK(""); |
| 397 | |
| 398 | /* Register ourselves with the kernel bus subsystem */ |
| 399 | err = bus_register(&xenbus_frontend.bus); |
Jeremy Fitzhardinge | 0ff4fdf | 2010-03-29 14:38:54 -0700 | [diff] [blame] | 400 | if (err) |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 401 | return err; |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 402 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 403 | register_xenstore_notifier(&xenstore_notifier); |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |
Jeremy Fitzhardinge | 806f546 | 2009-03-04 22:31:45 -0800 | [diff] [blame] | 407 | subsys_initcall(xenbus_probe_frontend_init); |
Ian Campbell | 2de06cc | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 408 | |
| 409 | #ifndef MODULE |
| 410 | static int __init boot_wait_for_devices(void) |
| 411 | { |
| 412 | if (xen_hvm_domain() && !xen_platform_pci_unplug) |
| 413 | return -ENODEV; |
| 414 | |
| 415 | ready_to_wait_for_devices = 1; |
| 416 | wait_for_devices(NULL); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | late_initcall(boot_wait_for_devices); |
| 421 | #endif |
Jeremy Fitzhardinge | 1b31a14 | 2009-03-27 16:29:44 -0700 | [diff] [blame] | 422 | |
| 423 | MODULE_LICENSE("GPL"); |