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> |
| 43 | #include <linux/notifier.h> |
| 44 | #include <linux/kthread.h> |
| 45 | #include <linux/mutex.h> |
| 46 | #include <linux/io.h> |
| 47 | |
| 48 | #include <asm/page.h> |
| 49 | #include <asm/pgtable.h> |
| 50 | #include <asm/xen/hypervisor.h> |
| 51 | #include <xen/xenbus.h> |
| 52 | #include <xen/events.h> |
| 53 | #include <xen/page.h> |
| 54 | |
| 55 | #include "xenbus_comms.h" |
| 56 | #include "xenbus_probe.h" |
| 57 | |
| 58 | int xen_store_evtchn; |
| 59 | struct xenstore_domain_interface *xen_store_interface; |
| 60 | static unsigned long xen_store_mfn; |
| 61 | |
| 62 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); |
| 63 | |
| 64 | static void wait_for_devices(struct xenbus_driver *xendrv); |
| 65 | |
| 66 | static int xenbus_probe_frontend(const char *type, const char *name); |
| 67 | |
| 68 | static void xenbus_dev_shutdown(struct device *_dev); |
| 69 | |
| 70 | /* If something in array of ids matches this device, return it. */ |
| 71 | static const struct xenbus_device_id * |
| 72 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) |
| 73 | { |
| 74 | for (; *arr->devicetype != '\0'; arr++) { |
| 75 | if (!strcmp(arr->devicetype, dev->devicetype)) |
| 76 | return arr; |
| 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | int xenbus_match(struct device *_dev, struct device_driver *_drv) |
| 82 | { |
| 83 | struct xenbus_driver *drv = to_xenbus_driver(_drv); |
| 84 | |
| 85 | if (!drv->ids) |
| 86 | return 0; |
| 87 | |
| 88 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; |
| 89 | } |
| 90 | |
| 91 | /* device/<type>/<id> => <type>-<id> */ |
| 92 | static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename) |
| 93 | { |
| 94 | nodename = strchr(nodename, '/'); |
| 95 | if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) { |
| 96 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | strlcpy(bus_id, nodename + 1, BUS_ID_SIZE); |
| 101 | if (!strchr(bus_id, '/')) { |
| 102 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | *strchr(bus_id, '/') = '-'; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void free_otherend_details(struct xenbus_device *dev) |
| 111 | { |
| 112 | kfree(dev->otherend); |
| 113 | dev->otherend = NULL; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static void free_otherend_watch(struct xenbus_device *dev) |
| 118 | { |
| 119 | if (dev->otherend_watch.node) { |
| 120 | unregister_xenbus_watch(&dev->otherend_watch); |
| 121 | kfree(dev->otherend_watch.node); |
| 122 | dev->otherend_watch.node = NULL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | int read_otherend_details(struct xenbus_device *xendev, |
| 128 | char *id_node, char *path_node) |
| 129 | { |
| 130 | int err = xenbus_gather(XBT_NIL, xendev->nodename, |
| 131 | id_node, "%i", &xendev->otherend_id, |
| 132 | path_node, NULL, &xendev->otherend, |
| 133 | NULL); |
| 134 | if (err) { |
| 135 | xenbus_dev_fatal(xendev, err, |
| 136 | "reading other end details from %s", |
| 137 | xendev->nodename); |
| 138 | return err; |
| 139 | } |
| 140 | if (strlen(xendev->otherend) == 0 || |
| 141 | !xenbus_exists(XBT_NIL, xendev->otherend, "")) { |
| 142 | xenbus_dev_fatal(xendev, -ENOENT, |
| 143 | "unable to read other end from %s. " |
| 144 | "missing or inaccessible.", |
| 145 | xendev->nodename); |
| 146 | free_otherend_details(xendev); |
| 147 | return -ENOENT; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static int read_backend_details(struct xenbus_device *xendev) |
| 155 | { |
| 156 | return read_otherend_details(xendev, "backend-id", "backend"); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Bus type for frontend drivers. */ |
| 161 | static struct xen_bus_type xenbus_frontend = { |
| 162 | .root = "device", |
| 163 | .levels = 2, /* device/type/<id> */ |
| 164 | .get_bus_id = frontend_bus_id, |
| 165 | .probe = xenbus_probe_frontend, |
| 166 | .bus = { |
| 167 | .name = "xen", |
| 168 | .match = xenbus_match, |
| 169 | .probe = xenbus_dev_probe, |
| 170 | .remove = xenbus_dev_remove, |
| 171 | .shutdown = xenbus_dev_shutdown, |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | static void otherend_changed(struct xenbus_watch *watch, |
| 176 | const char **vec, unsigned int len) |
| 177 | { |
| 178 | struct xenbus_device *dev = |
| 179 | container_of(watch, struct xenbus_device, otherend_watch); |
| 180 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); |
| 181 | enum xenbus_state state; |
| 182 | |
| 183 | /* Protect us against watches firing on old details when the otherend |
| 184 | details change, say immediately after a resume. */ |
| 185 | if (!dev->otherend || |
| 186 | strncmp(dev->otherend, vec[XS_WATCH_PATH], |
| 187 | strlen(dev->otherend))) { |
| 188 | dev_dbg(&dev->dev, "Ignoring watch at %s", vec[XS_WATCH_PATH]); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | state = xenbus_read_driver_state(dev->otherend); |
| 193 | |
| 194 | dev_dbg(&dev->dev, "state is %d, (%s), %s, %s", |
| 195 | state, xenbus_strstate(state), dev->otherend_watch.node, |
| 196 | vec[XS_WATCH_PATH]); |
| 197 | |
| 198 | /* |
| 199 | * Ignore xenbus transitions during shutdown. This prevents us doing |
| 200 | * work that can fail e.g., when the rootfs is gone. |
| 201 | */ |
| 202 | if (system_state > SYSTEM_RUNNING) { |
| 203 | struct xen_bus_type *bus = bus; |
| 204 | bus = container_of(dev->dev.bus, struct xen_bus_type, bus); |
| 205 | /* If we're frontend, drive the state machine to Closed. */ |
| 206 | /* This should cause the backend to release our resources. */ |
| 207 | if ((bus == &xenbus_frontend) && (state == XenbusStateClosing)) |
| 208 | xenbus_frontend_closed(dev); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (drv->otherend_changed) |
| 213 | drv->otherend_changed(dev, state); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static int talk_to_otherend(struct xenbus_device *dev) |
| 218 | { |
| 219 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); |
| 220 | |
| 221 | free_otherend_watch(dev); |
| 222 | free_otherend_details(dev); |
| 223 | |
| 224 | return drv->read_otherend_details(dev); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static int watch_otherend(struct xenbus_device *dev) |
| 229 | { |
| 230 | return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed, |
| 231 | "%s/%s", dev->otherend, "state"); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int xenbus_dev_probe(struct device *_dev) |
| 236 | { |
| 237 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 238 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); |
| 239 | const struct xenbus_device_id *id; |
| 240 | int err; |
| 241 | |
| 242 | DPRINTK("%s", dev->nodename); |
| 243 | |
| 244 | if (!drv->probe) { |
| 245 | err = -ENODEV; |
| 246 | goto fail; |
| 247 | } |
| 248 | |
| 249 | id = match_device(drv->ids, dev); |
| 250 | if (!id) { |
| 251 | err = -ENODEV; |
| 252 | goto fail; |
| 253 | } |
| 254 | |
| 255 | err = talk_to_otherend(dev); |
| 256 | if (err) { |
| 257 | dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n", |
| 258 | dev->nodename); |
| 259 | return err; |
| 260 | } |
| 261 | |
| 262 | err = drv->probe(dev, id); |
| 263 | if (err) |
| 264 | goto fail; |
| 265 | |
| 266 | err = watch_otherend(dev); |
| 267 | if (err) { |
| 268 | dev_warn(&dev->dev, "watch_otherend on %s failed.\n", |
| 269 | dev->nodename); |
| 270 | return err; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | fail: |
| 275 | xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); |
| 276 | xenbus_switch_state(dev, XenbusStateClosed); |
| 277 | return -ENODEV; |
| 278 | } |
| 279 | |
| 280 | int xenbus_dev_remove(struct device *_dev) |
| 281 | { |
| 282 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 283 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); |
| 284 | |
| 285 | DPRINTK("%s", dev->nodename); |
| 286 | |
| 287 | free_otherend_watch(dev); |
| 288 | free_otherend_details(dev); |
| 289 | |
| 290 | if (drv->remove) |
| 291 | drv->remove(dev); |
| 292 | |
| 293 | xenbus_switch_state(dev, XenbusStateClosed); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void xenbus_dev_shutdown(struct device *_dev) |
| 298 | { |
| 299 | struct xenbus_device *dev = to_xenbus_device(_dev); |
| 300 | unsigned long timeout = 5*HZ; |
| 301 | |
| 302 | DPRINTK("%s", dev->nodename); |
| 303 | |
| 304 | get_device(&dev->dev); |
| 305 | if (dev->state != XenbusStateConnected) { |
| 306 | printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__, |
| 307 | dev->nodename, xenbus_strstate(dev->state)); |
| 308 | goto out; |
| 309 | } |
| 310 | xenbus_switch_state(dev, XenbusStateClosing); |
| 311 | timeout = wait_for_completion_timeout(&dev->down, timeout); |
| 312 | if (!timeout) |
| 313 | printk(KERN_INFO "%s: %s timeout closing device\n", |
| 314 | __func__, dev->nodename); |
| 315 | out: |
| 316 | put_device(&dev->dev); |
| 317 | } |
| 318 | |
| 319 | int xenbus_register_driver_common(struct xenbus_driver *drv, |
| 320 | struct xen_bus_type *bus, |
| 321 | struct module *owner, |
| 322 | const char *mod_name) |
| 323 | { |
| 324 | drv->driver.name = drv->name; |
| 325 | drv->driver.bus = &bus->bus; |
| 326 | drv->driver.owner = owner; |
| 327 | drv->driver.mod_name = mod_name; |
| 328 | |
| 329 | return driver_register(&drv->driver); |
| 330 | } |
| 331 | |
| 332 | int __xenbus_register_frontend(struct xenbus_driver *drv, |
| 333 | struct module *owner, const char *mod_name) |
| 334 | { |
| 335 | int ret; |
| 336 | |
| 337 | drv->read_otherend_details = read_backend_details; |
| 338 | |
| 339 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, |
| 340 | owner, mod_name); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | /* If this driver is loaded as a module wait for devices to attach. */ |
| 345 | wait_for_devices(drv); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); |
| 350 | |
| 351 | void xenbus_unregister_driver(struct xenbus_driver *drv) |
| 352 | { |
| 353 | driver_unregister(&drv->driver); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(xenbus_unregister_driver); |
| 356 | |
| 357 | struct xb_find_info |
| 358 | { |
| 359 | struct xenbus_device *dev; |
| 360 | const char *nodename; |
| 361 | }; |
| 362 | |
| 363 | static int cmp_dev(struct device *dev, void *data) |
| 364 | { |
| 365 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 366 | struct xb_find_info *info = data; |
| 367 | |
| 368 | if (!strcmp(xendev->nodename, info->nodename)) { |
| 369 | info->dev = xendev; |
| 370 | get_device(dev); |
| 371 | return 1; |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | struct xenbus_device *xenbus_device_find(const char *nodename, |
| 377 | struct bus_type *bus) |
| 378 | { |
| 379 | struct xb_find_info info = { .dev = NULL, .nodename = nodename }; |
| 380 | |
| 381 | bus_for_each_dev(bus, NULL, &info, cmp_dev); |
| 382 | return info.dev; |
| 383 | } |
| 384 | |
| 385 | static int cleanup_dev(struct device *dev, void *data) |
| 386 | { |
| 387 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 388 | struct xb_find_info *info = data; |
| 389 | int len = strlen(info->nodename); |
| 390 | |
| 391 | DPRINTK("%s", info->nodename); |
| 392 | |
| 393 | /* Match the info->nodename path, or any subdirectory of that path. */ |
| 394 | if (strncmp(xendev->nodename, info->nodename, len)) |
| 395 | return 0; |
| 396 | |
| 397 | /* If the node name is longer, ensure it really is a subdirectory. */ |
| 398 | if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/')) |
| 399 | return 0; |
| 400 | |
| 401 | info->dev = xendev; |
| 402 | get_device(dev); |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | static void xenbus_cleanup_devices(const char *path, struct bus_type *bus) |
| 407 | { |
| 408 | struct xb_find_info info = { .nodename = path }; |
| 409 | |
| 410 | do { |
| 411 | info.dev = NULL; |
| 412 | bus_for_each_dev(bus, NULL, &info, cleanup_dev); |
| 413 | if (info.dev) { |
| 414 | device_unregister(&info.dev->dev); |
| 415 | put_device(&info.dev->dev); |
| 416 | } |
| 417 | } while (info.dev); |
| 418 | } |
| 419 | |
| 420 | static void xenbus_dev_release(struct device *dev) |
| 421 | { |
| 422 | if (dev) |
| 423 | kfree(to_xenbus_device(dev)); |
| 424 | } |
| 425 | |
| 426 | static ssize_t xendev_show_nodename(struct device *dev, |
| 427 | struct device_attribute *attr, char *buf) |
| 428 | { |
| 429 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename); |
| 430 | } |
| 431 | DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL); |
| 432 | |
| 433 | static ssize_t xendev_show_devtype(struct device *dev, |
| 434 | struct device_attribute *attr, char *buf) |
| 435 | { |
| 436 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype); |
| 437 | } |
| 438 | DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL); |
| 439 | |
| 440 | |
| 441 | int xenbus_probe_node(struct xen_bus_type *bus, |
| 442 | const char *type, |
| 443 | const char *nodename) |
| 444 | { |
| 445 | int err; |
| 446 | struct xenbus_device *xendev; |
| 447 | size_t stringlen; |
| 448 | char *tmpstring; |
| 449 | |
| 450 | enum xenbus_state state = xenbus_read_driver_state(nodename); |
| 451 | |
| 452 | if (state != XenbusStateInitialising) { |
| 453 | /* Device is not new, so ignore it. This can happen if a |
| 454 | device is going away after switching to Closed. */ |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | stringlen = strlen(nodename) + 1 + strlen(type) + 1; |
| 459 | xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL); |
| 460 | if (!xendev) |
| 461 | return -ENOMEM; |
| 462 | |
| 463 | xendev->state = XenbusStateInitialising; |
| 464 | |
| 465 | /* Copy the strings into the extra space. */ |
| 466 | |
| 467 | tmpstring = (char *)(xendev + 1); |
| 468 | strcpy(tmpstring, nodename); |
| 469 | xendev->nodename = tmpstring; |
| 470 | |
| 471 | tmpstring += strlen(tmpstring) + 1; |
| 472 | strcpy(tmpstring, type); |
| 473 | xendev->devicetype = tmpstring; |
| 474 | init_completion(&xendev->down); |
| 475 | |
| 476 | xendev->dev.bus = &bus->bus; |
| 477 | xendev->dev.release = xenbus_dev_release; |
| 478 | |
| 479 | err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename); |
| 480 | if (err) |
| 481 | goto fail; |
| 482 | |
| 483 | /* Register with generic device framework. */ |
| 484 | err = device_register(&xendev->dev); |
| 485 | if (err) |
| 486 | goto fail; |
| 487 | |
| 488 | err = device_create_file(&xendev->dev, &dev_attr_nodename); |
| 489 | if (err) |
| 490 | goto fail_unregister; |
| 491 | |
| 492 | err = device_create_file(&xendev->dev, &dev_attr_devtype); |
| 493 | if (err) |
| 494 | goto fail_remove_file; |
| 495 | |
| 496 | return 0; |
| 497 | fail_remove_file: |
| 498 | device_remove_file(&xendev->dev, &dev_attr_nodename); |
| 499 | fail_unregister: |
| 500 | device_unregister(&xendev->dev); |
| 501 | fail: |
| 502 | kfree(xendev); |
| 503 | return err; |
| 504 | } |
| 505 | |
| 506 | /* device/<typename>/<name> */ |
| 507 | static int xenbus_probe_frontend(const char *type, const char *name) |
| 508 | { |
| 509 | char *nodename; |
| 510 | int err; |
| 511 | |
| 512 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", |
| 513 | xenbus_frontend.root, type, name); |
| 514 | if (!nodename) |
| 515 | return -ENOMEM; |
| 516 | |
| 517 | DPRINTK("%s", nodename); |
| 518 | |
| 519 | err = xenbus_probe_node(&xenbus_frontend, type, nodename); |
| 520 | kfree(nodename); |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) |
| 525 | { |
| 526 | int err = 0; |
| 527 | char **dir; |
| 528 | unsigned int dir_n = 0; |
| 529 | int i; |
| 530 | |
| 531 | dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); |
| 532 | if (IS_ERR(dir)) |
| 533 | return PTR_ERR(dir); |
| 534 | |
| 535 | for (i = 0; i < dir_n; i++) { |
| 536 | err = bus->probe(type, dir[i]); |
| 537 | if (err) |
| 538 | break; |
| 539 | } |
| 540 | kfree(dir); |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | int xenbus_probe_devices(struct xen_bus_type *bus) |
| 545 | { |
| 546 | int err = 0; |
| 547 | char **dir; |
| 548 | unsigned int i, dir_n; |
| 549 | |
| 550 | dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); |
| 551 | if (IS_ERR(dir)) |
| 552 | return PTR_ERR(dir); |
| 553 | |
| 554 | for (i = 0; i < dir_n; i++) { |
| 555 | err = xenbus_probe_device_type(bus, dir[i]); |
| 556 | if (err) |
| 557 | break; |
| 558 | } |
| 559 | kfree(dir); |
| 560 | return err; |
| 561 | } |
| 562 | |
| 563 | static unsigned int char_count(const char *str, char c) |
| 564 | { |
| 565 | unsigned int i, ret = 0; |
| 566 | |
| 567 | for (i = 0; str[i]; i++) |
| 568 | if (str[i] == c) |
| 569 | ret++; |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | static int strsep_len(const char *str, char c, unsigned int len) |
| 574 | { |
| 575 | unsigned int i; |
| 576 | |
| 577 | for (i = 0; str[i]; i++) |
| 578 | if (str[i] == c) { |
| 579 | if (len == 0) |
| 580 | return i; |
| 581 | len--; |
| 582 | } |
| 583 | return (len == 0) ? i : -ERANGE; |
| 584 | } |
| 585 | |
| 586 | void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) |
| 587 | { |
| 588 | int exists, rootlen; |
| 589 | struct xenbus_device *dev; |
| 590 | char type[BUS_ID_SIZE]; |
| 591 | const char *p, *root; |
| 592 | |
| 593 | if (char_count(node, '/') < 2) |
| 594 | return; |
| 595 | |
| 596 | exists = xenbus_exists(XBT_NIL, node, ""); |
| 597 | if (!exists) { |
| 598 | xenbus_cleanup_devices(node, &bus->bus); |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | /* backend/<type>/... or device/<type>/... */ |
| 603 | p = strchr(node, '/') + 1; |
| 604 | snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p); |
| 605 | type[BUS_ID_SIZE-1] = '\0'; |
| 606 | |
| 607 | rootlen = strsep_len(node, '/', bus->levels); |
| 608 | if (rootlen < 0) |
| 609 | return; |
| 610 | root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node); |
| 611 | if (!root) |
| 612 | return; |
| 613 | |
| 614 | dev = xenbus_device_find(root, &bus->bus); |
| 615 | if (!dev) |
| 616 | xenbus_probe_node(bus, type, root); |
| 617 | else |
| 618 | put_device(&dev->dev); |
| 619 | |
| 620 | kfree(root); |
| 621 | } |
| 622 | |
| 623 | static void frontend_changed(struct xenbus_watch *watch, |
| 624 | const char **vec, unsigned int len) |
| 625 | { |
| 626 | DPRINTK(""); |
| 627 | |
| 628 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); |
| 629 | } |
| 630 | |
| 631 | /* We watch for devices appearing and vanishing. */ |
| 632 | static struct xenbus_watch fe_watch = { |
| 633 | .node = "device", |
| 634 | .callback = frontend_changed, |
| 635 | }; |
| 636 | |
| 637 | static int suspend_dev(struct device *dev, void *data) |
| 638 | { |
| 639 | int err = 0; |
| 640 | struct xenbus_driver *drv; |
| 641 | struct xenbus_device *xdev; |
| 642 | |
| 643 | DPRINTK(""); |
| 644 | |
| 645 | if (dev->driver == NULL) |
| 646 | return 0; |
| 647 | drv = to_xenbus_driver(dev->driver); |
| 648 | xdev = container_of(dev, struct xenbus_device, dev); |
| 649 | if (drv->suspend) |
| 650 | err = drv->suspend(xdev); |
| 651 | if (err) |
| 652 | printk(KERN_WARNING |
| 653 | "xenbus: suspend %s failed: %i\n", dev->bus_id, err); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int suspend_cancel_dev(struct device *dev, void *data) |
| 658 | { |
| 659 | int err = 0; |
| 660 | struct xenbus_driver *drv; |
| 661 | struct xenbus_device *xdev; |
| 662 | |
| 663 | DPRINTK(""); |
| 664 | |
| 665 | if (dev->driver == NULL) |
| 666 | return 0; |
| 667 | drv = to_xenbus_driver(dev->driver); |
| 668 | xdev = container_of(dev, struct xenbus_device, dev); |
| 669 | if (drv->suspend_cancel) |
| 670 | err = drv->suspend_cancel(xdev); |
| 671 | if (err) |
| 672 | printk(KERN_WARNING |
| 673 | "xenbus: suspend_cancel %s failed: %i\n", |
| 674 | dev->bus_id, err); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int resume_dev(struct device *dev, void *data) |
| 679 | { |
| 680 | int err; |
| 681 | struct xenbus_driver *drv; |
| 682 | struct xenbus_device *xdev; |
| 683 | |
| 684 | DPRINTK(""); |
| 685 | |
| 686 | if (dev->driver == NULL) |
| 687 | return 0; |
| 688 | |
| 689 | drv = to_xenbus_driver(dev->driver); |
| 690 | xdev = container_of(dev, struct xenbus_device, dev); |
| 691 | |
| 692 | err = talk_to_otherend(xdev); |
| 693 | if (err) { |
| 694 | printk(KERN_WARNING |
| 695 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", |
| 696 | dev->bus_id, err); |
| 697 | return err; |
| 698 | } |
| 699 | |
| 700 | xdev->state = XenbusStateInitialising; |
| 701 | |
| 702 | if (drv->resume) { |
| 703 | err = drv->resume(xdev); |
| 704 | if (err) { |
| 705 | printk(KERN_WARNING |
| 706 | "xenbus: resume %s failed: %i\n", |
| 707 | dev->bus_id, err); |
| 708 | return err; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | err = watch_otherend(xdev); |
| 713 | if (err) { |
| 714 | printk(KERN_WARNING |
| 715 | "xenbus_probe: resume (watch_otherend) %s failed: " |
| 716 | "%d.\n", dev->bus_id, err); |
| 717 | return err; |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | void xenbus_suspend(void) |
| 724 | { |
| 725 | DPRINTK(""); |
| 726 | |
| 727 | bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev); |
| 728 | xenbus_backend_suspend(suspend_dev); |
| 729 | xs_suspend(); |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(xenbus_suspend); |
| 732 | |
| 733 | void xenbus_resume(void) |
| 734 | { |
| 735 | xb_init_comms(); |
| 736 | xs_resume(); |
| 737 | bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev); |
| 738 | xenbus_backend_resume(resume_dev); |
| 739 | } |
| 740 | EXPORT_SYMBOL_GPL(xenbus_resume); |
| 741 | |
| 742 | void xenbus_suspend_cancel(void) |
| 743 | { |
| 744 | xs_suspend_cancel(); |
| 745 | bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev); |
| 746 | xenbus_backend_resume(suspend_cancel_dev); |
| 747 | } |
| 748 | EXPORT_SYMBOL_GPL(xenbus_suspend_cancel); |
| 749 | |
| 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 | |
| 758 | if (xenstored_ready > 0) |
| 759 | ret = nb->notifier_call(nb, 0, NULL); |
| 760 | else |
| 761 | blocking_notifier_chain_register(&xenstore_chain, nb); |
| 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 | { |
| 775 | BUG_ON((xenstored_ready <= 0)); |
| 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 | } |
| 785 | |
| 786 | static int __init xenbus_probe_init(void) |
| 787 | { |
| 788 | int err = 0; |
| 789 | |
| 790 | DPRINTK(""); |
| 791 | |
| 792 | err = -ENODEV; |
| 793 | if (!is_running_on_xen()) |
| 794 | goto out_error; |
| 795 | |
| 796 | /* Register ourselves with the kernel bus subsystem */ |
| 797 | err = bus_register(&xenbus_frontend.bus); |
| 798 | if (err) |
| 799 | goto out_error; |
| 800 | |
| 801 | err = xenbus_backend_bus_register(); |
| 802 | if (err) |
| 803 | goto out_unreg_front; |
| 804 | |
| 805 | /* |
| 806 | * Domain0 doesn't have a store_evtchn or store_mfn yet. |
| 807 | */ |
| 808 | if (is_initial_xendomain()) { |
| 809 | /* dom0 not yet supported */ |
| 810 | } else { |
| 811 | xenstored_ready = 1; |
| 812 | xen_store_evtchn = xen_start_info->store_evtchn; |
| 813 | xen_store_mfn = xen_start_info->store_mfn; |
| 814 | } |
| 815 | xen_store_interface = mfn_to_virt(xen_store_mfn); |
| 816 | |
| 817 | /* Initialize the interface to xenstore. */ |
| 818 | err = xs_init(); |
| 819 | if (err) { |
| 820 | printk(KERN_WARNING |
| 821 | "XENBUS: Error initializing xenstore comms: %i\n", err); |
| 822 | goto out_unreg_back; |
| 823 | } |
| 824 | |
| 825 | if (!is_initial_xendomain()) |
| 826 | xenbus_probe(NULL); |
| 827 | |
| 828 | return 0; |
| 829 | |
| 830 | out_unreg_back: |
| 831 | xenbus_backend_bus_unregister(); |
| 832 | |
| 833 | out_unreg_front: |
| 834 | bus_unregister(&xenbus_frontend.bus); |
| 835 | |
| 836 | out_error: |
| 837 | return err; |
| 838 | } |
| 839 | |
| 840 | postcore_initcall(xenbus_probe_init); |
| 841 | |
| 842 | MODULE_LICENSE("GPL"); |
| 843 | |
| 844 | static int is_disconnected_device(struct device *dev, void *data) |
| 845 | { |
| 846 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 847 | struct device_driver *drv = data; |
| 848 | |
| 849 | /* |
| 850 | * A device with no driver will never connect. We care only about |
| 851 | * devices which should currently be in the process of connecting. |
| 852 | */ |
| 853 | if (!dev->driver) |
| 854 | return 0; |
| 855 | |
| 856 | /* Is this search limited to a particular driver? */ |
| 857 | if (drv && (dev->driver != drv)) |
| 858 | return 0; |
| 859 | |
| 860 | return (xendev->state != XenbusStateConnected); |
| 861 | } |
| 862 | |
| 863 | static int exists_disconnected_device(struct device_driver *drv) |
| 864 | { |
| 865 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 866 | is_disconnected_device); |
| 867 | } |
| 868 | |
| 869 | static int print_device_status(struct device *dev, void *data) |
| 870 | { |
| 871 | struct xenbus_device *xendev = to_xenbus_device(dev); |
| 872 | struct device_driver *drv = data; |
| 873 | |
| 874 | /* Is this operation limited to a particular driver? */ |
| 875 | if (drv && (dev->driver != drv)) |
| 876 | return 0; |
| 877 | |
| 878 | if (!dev->driver) { |
| 879 | /* Information only: is this too noisy? */ |
| 880 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", |
| 881 | xendev->nodename); |
| 882 | } else if (xendev->state != XenbusStateConnected) { |
| 883 | printk(KERN_WARNING "XENBUS: Timeout connecting " |
| 884 | "to device: %s (state %d)\n", |
| 885 | xendev->nodename, xendev->state); |
| 886 | } |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /* We only wait for device setup after most initcalls have run. */ |
| 892 | static int ready_to_wait_for_devices; |
| 893 | |
| 894 | /* |
| 895 | * On a 10 second timeout, wait for all devices currently configured. We need |
| 896 | * to do this to guarantee that the filesystems and / or network devices |
| 897 | * needed for boot are available, before we can allow the boot to proceed. |
| 898 | * |
| 899 | * This needs to be on a late_initcall, to happen after the frontend device |
| 900 | * drivers have been initialised, but before the root fs is mounted. |
| 901 | * |
| 902 | * A possible improvement here would be to have the tools add a per-device |
| 903 | * flag to the store entry, indicating whether it is needed at boot time. |
| 904 | * This would allow people who knew what they were doing to accelerate their |
| 905 | * boot slightly, but of course needs tools or manual intervention to set up |
| 906 | * those flags correctly. |
| 907 | */ |
| 908 | static void wait_for_devices(struct xenbus_driver *xendrv) |
| 909 | { |
| 910 | unsigned long timeout = jiffies + 10*HZ; |
| 911 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; |
| 912 | |
| 913 | if (!ready_to_wait_for_devices || !is_running_on_xen()) |
| 914 | return; |
| 915 | |
| 916 | while (exists_disconnected_device(drv)) { |
| 917 | if (time_after(jiffies, timeout)) |
| 918 | break; |
| 919 | schedule_timeout_interruptible(HZ/10); |
| 920 | } |
| 921 | |
| 922 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
| 923 | print_device_status); |
| 924 | } |
| 925 | |
| 926 | #ifndef MODULE |
| 927 | static int __init boot_wait_for_devices(void) |
| 928 | { |
| 929 | ready_to_wait_for_devices = 1; |
| 930 | wait_for_devices(NULL); |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | late_initcall(boot_wait_for_devices); |
| 935 | #endif |