Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Talks to Xen Store to figure out what devices we have. |
| 3 | * |
| 4 | * Copyright (C) 2005 Rusty Russell, IBM Corporation |
| 5 | * Copyright (C) 2005 Mike Wray, Hewlett-Packard |
| 6 | * Copyright (C) 2005, 2006 XenSource Ltd |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License version 2 |
| 10 | * as published by the Free Software Foundation; or, when distributed |
| 11 | * separately from the Linux kernel or incorporated into other |
| 12 | * software packages, subject to the following license: |
| 13 | * |
| 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | * of this source file (the "Software"), to deal in the Software without |
| 16 | * restriction, including without limitation the rights to use, copy, modify, |
| 17 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 18 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 19 | * the following conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice shall be included in |
| 22 | * all copies or substantial portions of the Software. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 30 | * IN THE SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #define DPRINTK(fmt, args...) \ |
| 34 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ |
| 35 | __func__, __LINE__, ##args) |
| 36 | |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/ctype.h> |
| 41 | #include <linux/fcntl.h> |
| 42 | #include <linux/mm.h> |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 43 | #include <linux/proc_fs.h> |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 44 | #include <linux/notifier.h> |
| 45 | #include <linux/kthread.h> |
| 46 | #include <linux/mutex.h> |
| 47 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 48 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/page.h> |
| 51 | #include <asm/pgtable.h> |
| 52 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 53 | |
| 54 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 55 | #include <xen/xenbus.h> |
| 56 | #include <xen/events.h> |
| 57 | #include <xen/page.h> |
| 58 | |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 59 | #include <xen/platform_pci.h> |
Sheng Yang | bee6ab53 | 2010-05-14 12:39:33 +0100 | [diff] [blame] | 60 | #include <xen/hvm.h> |
| 61 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 62 | #include "xenbus_comms.h" |
| 63 | #include "xenbus_probe.h" |
| 64 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 65 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 66 | int xen_store_evtchn; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 67 | EXPORT_SYMBOL(xen_store_evtchn); |
| 68 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 69 | struct xenstore_domain_interface *xen_store_interface; |
| 70 | static unsigned long xen_store_mfn; |
| 71 | |
| 72 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); |
| 73 | |
| 74 | static void wait_for_devices(struct xenbus_driver *xendrv); |
| 75 | |
| 76 | static int xenbus_probe_frontend(const char *type, const char *name); |
| 77 | |
| 78 | static void xenbus_dev_shutdown(struct device *_dev); |
| 79 | |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 80 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state); |
| 81 | static int xenbus_dev_resume(struct device *dev); |
| 82 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 83 | /* If something in array of ids matches this device, return it. */ |
| 84 | static const struct xenbus_device_id * |
| 85 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) |
| 86 | { |
| 87 | for (; *arr->devicetype != '\0'; arr++) { |
| 88 | if (!strcmp(arr->devicetype, dev->devicetype)) |
| 89 | return arr; |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | int xenbus_match(struct device *_dev, struct device_driver *_drv) |
| 95 | { |
| 96 | struct xenbus_driver *drv = to_xenbus_driver(_drv); |
| 97 | |
| 98 | if (!drv->ids) |
| 99 | return 0; |
| 100 | |
| 101 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; |
| 102 | } |
| 103 | |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 104 | static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env) |
| 105 | { |
| 106 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 107 | |
| 108 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) |
| 109 | return -ENOMEM; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 114 | /* device/<type>/<id> => <type>-<id> */ |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 115 | static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 116 | { |
| 117 | nodename = strchr(nodename, '/'); |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 118 | if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 119 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 123 | strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 124 | if (!strchr(bus_id, '/')) { |
| 125 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | *strchr(bus_id, '/') = '-'; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static void free_otherend_details(struct xenbus_device *dev) |
| 134 | { |
| 135 | kfree(dev->otherend); |
| 136 | dev->otherend = NULL; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static void free_otherend_watch(struct xenbus_device *dev) |
| 141 | { |
| 142 | if (dev->otherend_watch.node) { |
| 143 | unregister_xenbus_watch(&dev->otherend_watch); |
| 144 | kfree(dev->otherend_watch.node); |
| 145 | dev->otherend_watch.node = NULL; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | int read_otherend_details(struct xenbus_device *xendev, |
| 151 | char *id_node, char *path_node) |
| 152 | { |
| 153 | int err = xenbus_gather(XBT_NIL, xendev->nodename, |
| 154 | id_node, "%i", &xendev->otherend_id, |
| 155 | path_node, NULL, &xendev->otherend, |
| 156 | NULL); |
| 157 | if (err) { |
| 158 | xenbus_dev_fatal(xendev, err, |
| 159 | "reading other end details from %s", |
| 160 | xendev->nodename); |
| 161 | return err; |
| 162 | } |
| 163 | if (strlen(xendev->otherend) == 0 || |
| 164 | !xenbus_exists(XBT_NIL, xendev->otherend, "")) { |
| 165 | xenbus_dev_fatal(xendev, -ENOENT, |
| 166 | "unable to read other end from %s. " |
| 167 | "missing or inaccessible.", |
| 168 | xendev->nodename); |
| 169 | free_otherend_details(xendev); |
| 170 | return -ENOENT; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static int read_backend_details(struct xenbus_device *xendev) |
| 178 | { |
| 179 | return read_otherend_details(xendev, "backend-id", "backend"); |
| 180 | } |
| 181 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 182 | static struct device_attribute xenbus_dev_attrs[] = { |
| 183 | __ATTR_NULL |
| 184 | }; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 185 | |
| 186 | /* Bus type for frontend drivers. */ |
| 187 | static struct xen_bus_type xenbus_frontend = { |
| 188 | .root = "device", |
| 189 | .levels = 2, /* device/type/<id> */ |
| 190 | .get_bus_id = frontend_bus_id, |
| 191 | .probe = xenbus_probe_frontend, |
| 192 | .bus = { |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 193 | .name = "xen", |
| 194 | .match = xenbus_match, |
| 195 | .uevent = xenbus_uevent, |
| 196 | .probe = xenbus_dev_probe, |
| 197 | .remove = xenbus_dev_remove, |
| 198 | .shutdown = xenbus_dev_shutdown, |
| 199 | .dev_attrs = xenbus_dev_attrs, |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 200 | |
| 201 | .suspend = xenbus_dev_suspend, |
| 202 | .resume = xenbus_dev_resume, |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 203 | }, |
| 204 | }; |
| 205 | |
| 206 | static void otherend_changed(struct xenbus_watch *watch, |
| 207 | const char **vec, unsigned int len) |
| 208 | { |
| 209 | struct xenbus_device *dev = |
| 210 | container_of(watch, struct xenbus_device, otherend_watch); |
| 211 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); |
| 212 | enum xenbus_state state; |
| 213 | |
| 214 | /* Protect us against watches firing on old details when the otherend |
| 215 | details change, say immediately after a resume. */ |
| 216 | if (!dev->otherend || |
| 217 | strncmp(dev->otherend, vec[XS_WATCH_PATH], |
| 218 | strlen(dev->otherend))) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 219 | dev_dbg(&dev->dev, "Ignoring watch at %s\n", |
| 220 | vec[XS_WATCH_PATH]); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | state = xenbus_read_driver_state(dev->otherend); |
| 225 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 226 | dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n", |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 227 | state, xenbus_strstate(state), dev->otherend_watch.node, |
| 228 | vec[XS_WATCH_PATH]); |
| 229 | |
| 230 | /* |
| 231 | * Ignore xenbus transitions during shutdown. This prevents us doing |
| 232 | * work that can fail e.g., when the rootfs is gone. |
| 233 | */ |
| 234 | if (system_state > SYSTEM_RUNNING) { |
| 235 | struct xen_bus_type *bus = bus; |
| 236 | bus = container_of(dev->dev.bus, struct xen_bus_type, bus); |
| 237 | /* If we're frontend, drive the state machine to Closed. */ |
| 238 | /* This should cause the backend to release our resources. */ |
| 239 | if ((bus == &xenbus_frontend) && (state == XenbusStateClosing)) |
| 240 | xenbus_frontend_closed(dev); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (drv->otherend_changed) |
| 245 | drv->otherend_changed(dev, state); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static int talk_to_otherend(struct xenbus_device *dev) |
| 250 | { |
| 251 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); |
| 252 | |
| 253 | free_otherend_watch(dev); |
| 254 | free_otherend_details(dev); |
| 255 | |
| 256 | return drv->read_otherend_details(dev); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static int watch_otherend(struct xenbus_device *dev) |
| 261 | { |
| 262 | return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed, |
| 263 | "%s/%s", dev->otherend, "state"); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | int xenbus_dev_probe(struct device *_dev) |
| 268 | { |
| 269 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 270 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); |
| 271 | const struct xenbus_device_id *id; |
| 272 | int err; |
| 273 | |
| 274 | DPRINTK("%s", dev->nodename); |
| 275 | |
| 276 | if (!drv->probe) { |
| 277 | err = -ENODEV; |
| 278 | goto fail; |
| 279 | } |
| 280 | |
| 281 | id = match_device(drv->ids, dev); |
| 282 | if (!id) { |
| 283 | err = -ENODEV; |
| 284 | goto fail; |
| 285 | } |
| 286 | |
| 287 | err = talk_to_otherend(dev); |
| 288 | if (err) { |
| 289 | dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n", |
| 290 | dev->nodename); |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | err = drv->probe(dev, id); |
| 295 | if (err) |
| 296 | goto fail; |
| 297 | |
| 298 | err = watch_otherend(dev); |
| 299 | if (err) { |
| 300 | dev_warn(&dev->dev, "watch_otherend on %s failed.\n", |
| 301 | dev->nodename); |
| 302 | return err; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | fail: |
| 307 | xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); |
| 308 | xenbus_switch_state(dev, XenbusStateClosed); |
| 309 | return -ENODEV; |
| 310 | } |
| 311 | |
| 312 | int xenbus_dev_remove(struct device *_dev) |
| 313 | { |
| 314 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 315 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); |
| 316 | |
| 317 | DPRINTK("%s", dev->nodename); |
| 318 | |
| 319 | free_otherend_watch(dev); |
| 320 | free_otherend_details(dev); |
| 321 | |
| 322 | if (drv->remove) |
| 323 | drv->remove(dev); |
| 324 | |
| 325 | xenbus_switch_state(dev, XenbusStateClosed); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void xenbus_dev_shutdown(struct device *_dev) |
| 330 | { |
| 331 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 332 | unsigned long timeout = 5*HZ; |
| 333 | |
| 334 | DPRINTK("%s", dev->nodename); |
| 335 | |
| 336 | get_device(&dev->dev); |
| 337 | if (dev->state != XenbusStateConnected) { |
| 338 | printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__, |
| 339 | dev->nodename, xenbus_strstate(dev->state)); |
| 340 | goto out; |
| 341 | } |
| 342 | xenbus_switch_state(dev, XenbusStateClosing); |
| 343 | timeout = wait_for_completion_timeout(&dev->down, timeout); |
| 344 | if (!timeout) |
| 345 | printk(KERN_INFO "%s: %s timeout closing device\n", |
| 346 | __func__, dev->nodename); |
| 347 | out: |
| 348 | put_device(&dev->dev); |
| 349 | } |
| 350 | |
| 351 | int xenbus_register_driver_common(struct xenbus_driver *drv, |
| 352 | struct xen_bus_type *bus, |
| 353 | struct module *owner, |
| 354 | const char *mod_name) |
| 355 | { |
| 356 | drv->driver.name = drv->name; |
| 357 | drv->driver.bus = &bus->bus; |
| 358 | drv->driver.owner = owner; |
| 359 | drv->driver.mod_name = mod_name; |
| 360 | |
| 361 | return driver_register(&drv->driver); |
| 362 | } |
| 363 | |
| 364 | int __xenbus_register_frontend(struct xenbus_driver *drv, |
| 365 | struct module *owner, const char *mod_name) |
| 366 | { |
| 367 | int ret; |
| 368 | |
| 369 | drv->read_otherend_details = read_backend_details; |
| 370 | |
| 371 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, |
| 372 | owner, mod_name); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | /* If this driver is loaded as a module wait for devices to attach. */ |
| 377 | wait_for_devices(drv); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); |
| 382 | |
| 383 | void xenbus_unregister_driver(struct xenbus_driver *drv) |
| 384 | { |
| 385 | driver_unregister(&drv->driver); |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(xenbus_unregister_driver); |
| 388 | |
| 389 | struct xb_find_info |
| 390 | { |
| 391 | struct xenbus_device *dev; |
| 392 | const char *nodename; |
| 393 | }; |
| 394 | |
| 395 | static int cmp_dev(struct device *dev, void *data) |
| 396 | { |
| 397 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 398 | struct xb_find_info *info = data; |
| 399 | |
| 400 | if (!strcmp(xendev->nodename, info->nodename)) { |
| 401 | info->dev = xendev; |
| 402 | get_device(dev); |
| 403 | return 1; |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | struct xenbus_device *xenbus_device_find(const char *nodename, |
| 409 | struct bus_type *bus) |
| 410 | { |
| 411 | struct xb_find_info info = { .dev = NULL, .nodename = nodename }; |
| 412 | |
| 413 | bus_for_each_dev(bus, NULL, &info, cmp_dev); |
| 414 | return info.dev; |
| 415 | } |
| 416 | |
| 417 | static int cleanup_dev(struct device *dev, void *data) |
| 418 | { |
| 419 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 420 | struct xb_find_info *info = data; |
| 421 | int len = strlen(info->nodename); |
| 422 | |
| 423 | DPRINTK("%s", info->nodename); |
| 424 | |
| 425 | /* Match the info->nodename path, or any subdirectory of that path. */ |
| 426 | if (strncmp(xendev->nodename, info->nodename, len)) |
| 427 | return 0; |
| 428 | |
| 429 | /* If the node name is longer, ensure it really is a subdirectory. */ |
| 430 | if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/')) |
| 431 | return 0; |
| 432 | |
| 433 | info->dev = xendev; |
| 434 | get_device(dev); |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | static void xenbus_cleanup_devices(const char *path, struct bus_type *bus) |
| 439 | { |
| 440 | struct xb_find_info info = { .nodename = path }; |
| 441 | |
| 442 | do { |
| 443 | info.dev = NULL; |
| 444 | bus_for_each_dev(bus, NULL, &info, cleanup_dev); |
| 445 | if (info.dev) { |
| 446 | device_unregister(&info.dev->dev); |
| 447 | put_device(&info.dev->dev); |
| 448 | } |
| 449 | } while (info.dev); |
| 450 | } |
| 451 | |
| 452 | static void xenbus_dev_release(struct device *dev) |
| 453 | { |
| 454 | if (dev) |
| 455 | kfree(to_xenbus_device(dev)); |
| 456 | } |
| 457 | |
| 458 | static ssize_t xendev_show_nodename(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
| 460 | { |
| 461 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename); |
| 462 | } |
Jeremy Fitzhardinge | db05fed | 2009-11-24 16:41:47 -0800 | [diff] [blame] | 463 | static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 464 | |
| 465 | static ssize_t xendev_show_devtype(struct device *dev, |
| 466 | struct device_attribute *attr, char *buf) |
| 467 | { |
| 468 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype); |
| 469 | } |
Jeremy Fitzhardinge | db05fed | 2009-11-24 16:41:47 -0800 | [diff] [blame] | 470 | static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 471 | |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 472 | static ssize_t xendev_show_modalias(struct device *dev, |
| 473 | struct device_attribute *attr, char *buf) |
| 474 | { |
| 475 | return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype); |
| 476 | } |
Jeremy Fitzhardinge | db05fed | 2009-11-24 16:41:47 -0800 | [diff] [blame] | 477 | static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 478 | |
| 479 | int xenbus_probe_node(struct xen_bus_type *bus, |
| 480 | const char *type, |
| 481 | const char *nodename) |
| 482 | { |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 483 | char devname[XEN_BUS_ID_SIZE]; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 484 | int err; |
| 485 | struct xenbus_device *xendev; |
| 486 | size_t stringlen; |
| 487 | char *tmpstring; |
| 488 | |
| 489 | enum xenbus_state state = xenbus_read_driver_state(nodename); |
| 490 | |
| 491 | if (state != XenbusStateInitialising) { |
| 492 | /* Device is not new, so ignore it. This can happen if a |
| 493 | device is going away after switching to Closed. */ |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | stringlen = strlen(nodename) + 1 + strlen(type) + 1; |
| 498 | xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL); |
| 499 | if (!xendev) |
| 500 | return -ENOMEM; |
| 501 | |
| 502 | xendev->state = XenbusStateInitialising; |
| 503 | |
| 504 | /* Copy the strings into the extra space. */ |
| 505 | |
| 506 | tmpstring = (char *)(xendev + 1); |
| 507 | strcpy(tmpstring, nodename); |
| 508 | xendev->nodename = tmpstring; |
| 509 | |
| 510 | tmpstring += strlen(tmpstring) + 1; |
| 511 | strcpy(tmpstring, type); |
| 512 | xendev->devicetype = tmpstring; |
| 513 | init_completion(&xendev->down); |
| 514 | |
| 515 | xendev->dev.bus = &bus->bus; |
| 516 | xendev->dev.release = xenbus_dev_release; |
| 517 | |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 518 | err = bus->get_bus_id(devname, xendev->nodename); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 519 | if (err) |
| 520 | goto fail; |
| 521 | |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 522 | dev_set_name(&xendev->dev, devname); |
| 523 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 524 | /* Register with generic device framework. */ |
| 525 | err = device_register(&xendev->dev); |
| 526 | if (err) |
| 527 | goto fail; |
| 528 | |
| 529 | err = device_create_file(&xendev->dev, &dev_attr_nodename); |
| 530 | if (err) |
| 531 | goto fail_unregister; |
| 532 | |
| 533 | err = device_create_file(&xendev->dev, &dev_attr_devtype); |
| 534 | if (err) |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 535 | goto fail_remove_nodename; |
| 536 | |
| 537 | err = device_create_file(&xendev->dev, &dev_attr_modalias); |
| 538 | if (err) |
| 539 | goto fail_remove_devtype; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 540 | |
| 541 | return 0; |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 542 | fail_remove_devtype: |
| 543 | device_remove_file(&xendev->dev, &dev_attr_devtype); |
| 544 | fail_remove_nodename: |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 545 | device_remove_file(&xendev->dev, &dev_attr_nodename); |
| 546 | fail_unregister: |
| 547 | device_unregister(&xendev->dev); |
| 548 | fail: |
| 549 | kfree(xendev); |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | /* device/<typename>/<name> */ |
| 554 | static int xenbus_probe_frontend(const char *type, const char *name) |
| 555 | { |
| 556 | char *nodename; |
| 557 | int err; |
| 558 | |
| 559 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", |
| 560 | xenbus_frontend.root, type, name); |
| 561 | if (!nodename) |
| 562 | return -ENOMEM; |
| 563 | |
| 564 | DPRINTK("%s", nodename); |
| 565 | |
| 566 | err = xenbus_probe_node(&xenbus_frontend, type, nodename); |
| 567 | kfree(nodename); |
| 568 | return err; |
| 569 | } |
| 570 | |
| 571 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) |
| 572 | { |
| 573 | int err = 0; |
| 574 | char **dir; |
| 575 | unsigned int dir_n = 0; |
| 576 | int i; |
| 577 | |
| 578 | dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); |
| 579 | if (IS_ERR(dir)) |
| 580 | return PTR_ERR(dir); |
| 581 | |
| 582 | for (i = 0; i < dir_n; i++) { |
| 583 | err = bus->probe(type, dir[i]); |
| 584 | if (err) |
| 585 | break; |
| 586 | } |
| 587 | kfree(dir); |
| 588 | return err; |
| 589 | } |
| 590 | |
| 591 | int xenbus_probe_devices(struct xen_bus_type *bus) |
| 592 | { |
| 593 | int err = 0; |
| 594 | char **dir; |
| 595 | unsigned int i, dir_n; |
| 596 | |
| 597 | dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); |
| 598 | if (IS_ERR(dir)) |
| 599 | return PTR_ERR(dir); |
| 600 | |
| 601 | for (i = 0; i < dir_n; i++) { |
| 602 | err = xenbus_probe_device_type(bus, dir[i]); |
| 603 | if (err) |
| 604 | break; |
| 605 | } |
| 606 | kfree(dir); |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | static unsigned int char_count(const char *str, char c) |
| 611 | { |
| 612 | unsigned int i, ret = 0; |
| 613 | |
| 614 | for (i = 0; str[i]; i++) |
| 615 | if (str[i] == c) |
| 616 | ret++; |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | static int strsep_len(const char *str, char c, unsigned int len) |
| 621 | { |
| 622 | unsigned int i; |
| 623 | |
| 624 | for (i = 0; str[i]; i++) |
| 625 | if (str[i] == c) { |
| 626 | if (len == 0) |
| 627 | return i; |
| 628 | len--; |
| 629 | } |
| 630 | return (len == 0) ? i : -ERANGE; |
| 631 | } |
| 632 | |
| 633 | void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) |
| 634 | { |
| 635 | int exists, rootlen; |
| 636 | struct xenbus_device *dev; |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 637 | char type[XEN_BUS_ID_SIZE]; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 638 | const char *p, *root; |
| 639 | |
| 640 | if (char_count(node, '/') < 2) |
| 641 | return; |
| 642 | |
| 643 | exists = xenbus_exists(XBT_NIL, node, ""); |
| 644 | if (!exists) { |
| 645 | xenbus_cleanup_devices(node, &bus->bus); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | /* backend/<type>/... or device/<type>/... */ |
| 650 | p = strchr(node, '/') + 1; |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 651 | snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p); |
| 652 | type[XEN_BUS_ID_SIZE-1] = '\0'; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 653 | |
| 654 | rootlen = strsep_len(node, '/', bus->levels); |
| 655 | if (rootlen < 0) |
| 656 | return; |
| 657 | root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node); |
| 658 | if (!root) |
| 659 | return; |
| 660 | |
| 661 | dev = xenbus_device_find(root, &bus->bus); |
| 662 | if (!dev) |
| 663 | xenbus_probe_node(bus, type, root); |
| 664 | else |
| 665 | put_device(&dev->dev); |
| 666 | |
| 667 | kfree(root); |
| 668 | } |
Jeremy Fitzhardinge | c6a960c | 2009-02-09 12:05:53 -0800 | [diff] [blame] | 669 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 670 | |
| 671 | static void frontend_changed(struct xenbus_watch *watch, |
| 672 | const char **vec, unsigned int len) |
| 673 | { |
| 674 | DPRINTK(""); |
| 675 | |
| 676 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); |
| 677 | } |
| 678 | |
| 679 | /* We watch for devices appearing and vanishing. */ |
| 680 | static struct xenbus_watch fe_watch = { |
| 681 | .node = "device", |
| 682 | .callback = frontend_changed, |
| 683 | }; |
| 684 | |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 685 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 686 | { |
| 687 | int err = 0; |
| 688 | struct xenbus_driver *drv; |
| 689 | struct xenbus_device *xdev; |
| 690 | |
| 691 | DPRINTK(""); |
| 692 | |
| 693 | if (dev->driver == NULL) |
| 694 | return 0; |
| 695 | drv = to_xenbus_driver(dev->driver); |
| 696 | xdev = container_of(dev, struct xenbus_device, dev); |
| 697 | if (drv->suspend) |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 698 | err = drv->suspend(xdev, state); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 699 | if (err) |
| 700 | printk(KERN_WARNING |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 701 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 705 | static int xenbus_dev_resume(struct device *dev) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 706 | { |
| 707 | int err; |
| 708 | struct xenbus_driver *drv; |
| 709 | struct xenbus_device *xdev; |
| 710 | |
| 711 | DPRINTK(""); |
| 712 | |
| 713 | if (dev->driver == NULL) |
| 714 | return 0; |
| 715 | |
| 716 | drv = to_xenbus_driver(dev->driver); |
| 717 | xdev = container_of(dev, struct xenbus_device, dev); |
| 718 | |
| 719 | err = talk_to_otherend(xdev); |
| 720 | if (err) { |
| 721 | printk(KERN_WARNING |
| 722 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 723 | dev_name(dev), err); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 724 | return err; |
| 725 | } |
| 726 | |
| 727 | xdev->state = XenbusStateInitialising; |
| 728 | |
| 729 | if (drv->resume) { |
| 730 | err = drv->resume(xdev); |
| 731 | if (err) { |
| 732 | printk(KERN_WARNING |
| 733 | "xenbus: resume %s failed: %i\n", |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 734 | dev_name(dev), err); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 735 | return err; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | err = watch_otherend(xdev); |
| 740 | if (err) { |
| 741 | printk(KERN_WARNING |
| 742 | "xenbus_probe: resume (watch_otherend) %s failed: " |
Kay Sievers | 2a678cc | 2009-01-06 10:44:34 -0800 | [diff] [blame] | 743 | "%d.\n", dev_name(dev), err); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 744 | return err; |
| 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 750 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
| 751 | int xenstored_ready = 0; |
| 752 | |
| 753 | |
| 754 | int register_xenstore_notifier(struct notifier_block *nb) |
| 755 | { |
| 756 | int ret = 0; |
| 757 | |
Stefano Stabellini | a947f0f | 2010-10-04 16:10:06 +0100 | [diff] [blame] | 758 | if (xenstored_ready > 0) |
| 759 | ret = nb->notifier_call(nb, 0, NULL); |
| 760 | else |
| 761 | blocking_notifier_chain_register(&xenstore_chain, nb); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | EXPORT_SYMBOL_GPL(register_xenstore_notifier); |
| 766 | |
| 767 | void unregister_xenstore_notifier(struct notifier_block *nb) |
| 768 | { |
| 769 | blocking_notifier_chain_unregister(&xenstore_chain, nb); |
| 770 | } |
| 771 | EXPORT_SYMBOL_GPL(unregister_xenstore_notifier); |
| 772 | |
| 773 | void xenbus_probe(struct work_struct *unused) |
| 774 | { |
Stefano Stabellini | a947f0f | 2010-10-04 16:10:06 +0100 | [diff] [blame] | 775 | xenstored_ready = 1; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 776 | |
| 777 | /* Enumerate devices in xenstore and watch for changes. */ |
| 778 | xenbus_probe_devices(&xenbus_frontend); |
| 779 | register_xenbus_watch(&fe_watch); |
| 780 | xenbus_backend_probe_and_watch(); |
| 781 | |
| 782 | /* Notify others that xenstore is up */ |
| 783 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); |
| 784 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 785 | EXPORT_SYMBOL_GPL(xenbus_probe); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 786 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 787 | static int __init xenbus_probe_initcall(void) |
| 788 | { |
| 789 | if (!xen_domain()) |
| 790 | return -ENODEV; |
| 791 | |
| 792 | if (xen_initial_domain() || xen_hvm_domain()) |
| 793 | return 0; |
| 794 | |
| 795 | xenbus_probe(NULL); |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | device_initcall(xenbus_probe_initcall); |
| 800 | |
| 801 | static int __init xenbus_init(void) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 802 | { |
| 803 | int err = 0; |
Juan Quintela | b37a56d | 2010-09-02 14:53:56 +0100 | [diff] [blame^] | 804 | unsigned long page = 0; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 805 | |
| 806 | DPRINTK(""); |
| 807 | |
| 808 | err = -ENODEV; |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 809 | if (!xen_domain()) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 810 | goto out_error; |
| 811 | |
| 812 | /* Register ourselves with the kernel bus subsystem */ |
| 813 | err = bus_register(&xenbus_frontend.bus); |
| 814 | if (err) |
| 815 | goto out_error; |
| 816 | |
| 817 | err = xenbus_backend_bus_register(); |
| 818 | if (err) |
| 819 | goto out_unreg_front; |
| 820 | |
| 821 | /* |
| 822 | * Domain0 doesn't have a store_evtchn or store_mfn yet. |
| 823 | */ |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 824 | if (xen_initial_domain()) { |
Juan Quintela | b37a56d | 2010-09-02 14:53:56 +0100 | [diff] [blame^] | 825 | struct evtchn_alloc_unbound alloc_unbound; |
| 826 | |
| 827 | /* Allocate Xenstore page */ |
| 828 | page = get_zeroed_page(GFP_KERNEL); |
| 829 | if (!page) |
| 830 | goto out_error; |
| 831 | |
| 832 | xen_store_mfn = xen_start_info->store_mfn = |
| 833 | pfn_to_mfn(virt_to_phys((void *)page) >> |
| 834 | PAGE_SHIFT); |
| 835 | |
| 836 | /* Next allocate a local port which xenstored can bind to */ |
| 837 | alloc_unbound.dom = DOMID_SELF; |
| 838 | alloc_unbound.remote_dom = 0; |
| 839 | |
| 840 | err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, |
| 841 | &alloc_unbound); |
| 842 | if (err == -ENOSYS) |
| 843 | goto out_error; |
| 844 | |
| 845 | BUG_ON(err); |
| 846 | xen_store_evtchn = xen_start_info->store_evtchn = |
| 847 | alloc_unbound.port; |
| 848 | |
| 849 | xen_store_interface = mfn_to_virt(xen_store_mfn); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 850 | } else { |
Sheng Yang | bee6ab53 | 2010-05-14 12:39:33 +0100 | [diff] [blame] | 851 | if (xen_hvm_domain()) { |
| 852 | uint64_t v = 0; |
| 853 | err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v); |
| 854 | if (err) |
| 855 | goto out_error; |
| 856 | xen_store_evtchn = (int)v; |
| 857 | err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v); |
| 858 | if (err) |
| 859 | goto out_error; |
| 860 | xen_store_mfn = (unsigned long)v; |
| 861 | xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE); |
| 862 | } else { |
| 863 | xen_store_evtchn = xen_start_info->store_evtchn; |
| 864 | xen_store_mfn = xen_start_info->store_mfn; |
| 865 | xen_store_interface = mfn_to_virt(xen_store_mfn); |
Stefano Stabellini | a947f0f | 2010-10-04 16:10:06 +0100 | [diff] [blame] | 866 | xenstored_ready = 1; |
Sheng Yang | bee6ab53 | 2010-05-14 12:39:33 +0100 | [diff] [blame] | 867 | } |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 868 | } |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 869 | |
| 870 | /* Initialize the interface to xenstore. */ |
| 871 | err = xs_init(); |
| 872 | if (err) { |
| 873 | printk(KERN_WARNING |
| 874 | "XENBUS: Error initializing xenstore comms: %i\n", err); |
| 875 | goto out_unreg_back; |
| 876 | } |
| 877 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 878 | #ifdef CONFIG_XEN_COMPAT_XENFS |
| 879 | /* |
| 880 | * Create xenfs mountpoint in /proc for compatibility with |
| 881 | * utilities that expect to find "xenbus" under "/proc/xen". |
| 882 | */ |
| 883 | proc_mkdir("xen", NULL); |
| 884 | #endif |
| 885 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 886 | return 0; |
| 887 | |
| 888 | out_unreg_back: |
| 889 | xenbus_backend_bus_unregister(); |
| 890 | |
| 891 | out_unreg_front: |
| 892 | bus_unregister(&xenbus_frontend.bus); |
| 893 | |
| 894 | out_error: |
Juan Quintela | b37a56d | 2010-09-02 14:53:56 +0100 | [diff] [blame^] | 895 | if (page != 0) |
| 896 | free_page(page); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 897 | return err; |
| 898 | } |
| 899 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 900 | postcore_initcall(xenbus_init); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 901 | |
| 902 | MODULE_LICENSE("GPL"); |
| 903 | |
Paolo Bonzini | c6e1971 | 2009-07-08 12:27:37 +0200 | [diff] [blame] | 904 | static int is_device_connecting(struct device *dev, void *data) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 905 | { |
| 906 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 907 | struct device_driver *drv = data; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 908 | struct xenbus_driver *xendrv; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * A device with no driver will never connect. We care only about |
| 912 | * devices which should currently be in the process of connecting. |
| 913 | */ |
| 914 | if (!dev->driver) |
| 915 | return 0; |
| 916 | |
| 917 | /* Is this search limited to a particular driver? */ |
| 918 | if (drv && (dev->driver != drv)) |
| 919 | return 0; |
| 920 | |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 921 | xendrv = to_xenbus_driver(dev->driver); |
Paolo Bonzini | c6e1971 | 2009-07-08 12:27:37 +0200 | [diff] [blame] | 922 | return (xendev->state < XenbusStateConnected || |
| 923 | (xendev->state == XenbusStateConnected && |
| 924 | xendrv->is_ready && !xendrv->is_ready(xendev))); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Paolo Bonzini | c6e1971 | 2009-07-08 12:27:37 +0200 | [diff] [blame] | 927 | static int exists_connecting_device(struct device_driver *drv) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 928 | { |
| 929 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
Paolo Bonzini | c6e1971 | 2009-07-08 12:27:37 +0200 | [diff] [blame] | 930 | is_device_connecting); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static int print_device_status(struct device *dev, void *data) |
| 934 | { |
| 935 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 936 | struct device_driver *drv = data; |
| 937 | |
| 938 | /* Is this operation limited to a particular driver? */ |
| 939 | if (drv && (dev->driver != drv)) |
| 940 | return 0; |
| 941 | |
| 942 | if (!dev->driver) { |
| 943 | /* Information only: is this too noisy? */ |
| 944 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", |
| 945 | xendev->nodename); |
Paolo Bonzini | f8dc330 | 2009-07-08 12:27:38 +0200 | [diff] [blame] | 946 | } else if (xendev->state < XenbusStateConnected) { |
| 947 | enum xenbus_state rstate = XenbusStateUnknown; |
| 948 | if (xendev->otherend) |
| 949 | rstate = xenbus_read_driver_state(xendev->otherend); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 950 | printk(KERN_WARNING "XENBUS: Timeout connecting " |
Paolo Bonzini | f8dc330 | 2009-07-08 12:27:38 +0200 | [diff] [blame] | 951 | "to device: %s (local state %d, remote state %d)\n", |
| 952 | xendev->nodename, xendev->state, rstate); |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | /* We only wait for device setup after most initcalls have run. */ |
| 959 | static int ready_to_wait_for_devices; |
| 960 | |
| 961 | /* |
Paolo Bonzini | ae78880 | 2009-07-08 12:27:39 +0200 | [diff] [blame] | 962 | * On a 5-minute timeout, wait for all devices currently configured. We need |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 963 | * to do this to guarantee that the filesystems and / or network devices |
| 964 | * needed for boot are available, before we can allow the boot to proceed. |
| 965 | * |
| 966 | * This needs to be on a late_initcall, to happen after the frontend device |
| 967 | * drivers have been initialised, but before the root fs is mounted. |
| 968 | * |
| 969 | * A possible improvement here would be to have the tools add a per-device |
| 970 | * flag to the store entry, indicating whether it is needed at boot time. |
| 971 | * This would allow people who knew what they were doing to accelerate their |
| 972 | * boot slightly, but of course needs tools or manual intervention to set up |
| 973 | * those flags correctly. |
| 974 | */ |
| 975 | static void wait_for_devices(struct xenbus_driver *xendrv) |
| 976 | { |
Paolo Bonzini | ae78880 | 2009-07-08 12:27:39 +0200 | [diff] [blame] | 977 | unsigned long start = jiffies; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 978 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; |
Paolo Bonzini | ae78880 | 2009-07-08 12:27:39 +0200 | [diff] [blame] | 979 | unsigned int seconds_waited = 0; |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 980 | |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 981 | if (!ready_to_wait_for_devices || !xen_domain()) |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 982 | return; |
| 983 | |
Paolo Bonzini | c6e1971 | 2009-07-08 12:27:37 +0200 | [diff] [blame] | 984 | while (exists_connecting_device(drv)) { |
Paolo Bonzini | ae78880 | 2009-07-08 12:27:39 +0200 | [diff] [blame] | 985 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { |
| 986 | if (!seconds_waited) |
| 987 | printk(KERN_WARNING "XENBUS: Waiting for " |
| 988 | "devices to initialise: "); |
| 989 | seconds_waited += 5; |
| 990 | printk("%us...", 300 - seconds_waited); |
| 991 | if (seconds_waited == 300) |
| 992 | break; |
| 993 | } |
| 994 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 995 | schedule_timeout_interruptible(HZ/10); |
| 996 | } |
| 997 | |
Paolo Bonzini | ae78880 | 2009-07-08 12:27:39 +0200 | [diff] [blame] | 998 | if (seconds_waited) |
| 999 | printk("\n"); |
| 1000 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1001 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 1002 | print_device_status); |
| 1003 | } |
| 1004 | |
| 1005 | #ifndef MODULE |
| 1006 | static int __init boot_wait_for_devices(void) |
| 1007 | { |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1008 | if (xen_hvm_domain() && !xen_platform_pci_unplug) |
| 1009 | return -ENODEV; |
| 1010 | |
Jeremy Fitzhardinge | 4bac07c | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1011 | ready_to_wait_for_devices = 1; |
| 1012 | wait_for_devices(NULL); |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | late_initcall(boot_wait_for_devices); |
| 1017 | #endif |