1 /****************************************************************************** 2 * Copyright (C) 2010 Spectra Logic Corporation 3 * Copyright (C) 2008 Doug Rabson 4 * Copyright (C) 2005 Rusty Russell, IBM Corporation 5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard 6 * Copyright (C) 2005 XenSource Ltd 7 * 8 * This file may be distributed separately from the Linux kernel, or 9 * incorporated into other software packages, subject to the following license: 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a copy 12 * of this source file (the "Software"), to deal in the Software without 13 * restriction, including without limitation the rights to use, copy, modify, 14 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 15 * and to permit persons to whom the Software is furnished to do so, subject to 16 * the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included in 19 * all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 27 * IN THE SOFTWARE. 28 */ 29 30 /** 31 * \file xenbusb.c 32 * 33 * \brief Shared support functions for managing the NewBus buses that contain 34 * Xen front and back end device instances. 35 * 36 * The NewBus implementation of XenBus attaches a xenbusb_front and xenbusb_back 37 * child bus to the xenstore device. This strategy allows the small differences 38 * in the handling of XenBus operations for front and back devices to be handled 39 * as overrides in xenbusb_front/back.c. Front and back specific device 40 * classes are also provided so device drivers can register for the devices they 41 * can handle without the need to filter within their probe routines. The 42 * net result is a device hierarchy that might look like this: 43 * 44 * xenstore0/ 45 * xenbusb_front0/ 46 * xn0 47 * xbd0 48 * xbd1 49 * xenbusb_back0/ 50 * xbbd0 51 * xnb0 52 * xnb1 53 */ 54 55 #include <sys/param.h> 56 #include <sys/bus.h> 57 #include <sys/kernel.h> 58 #include <sys/lock.h> 59 #include <sys/malloc.h> 60 #include <sys/module.h> 61 #include <sys/sbuf.h> 62 #include <sys/stdarg.h> 63 #include <sys/sysctl.h> 64 #include <sys/syslog.h> 65 #include <sys/systm.h> 66 #include <sys/sx.h> 67 #include <sys/taskqueue.h> 68 69 #include <xen/xen-os.h> 70 #include <xen/gnttab.h> 71 #include <xen/xenstore/xenstorevar.h> 72 #include <xen/xenbus/xenbusb.h> 73 #include <xen/xenbus/xenbusvar.h> 74 75 /*------------------------- Private Functions --------------------------------*/ 76 /** 77 * \brief Deallocate XenBus device instance variables. 78 * 79 * \param ivars The instance variable block to free. 80 */ 81 static void 82 xenbusb_free_child_ivars(struct xenbus_device_ivars *ivars) 83 { 84 if (ivars->xd_otherend_watch.node != NULL) { 85 xs_unregister_watch(&ivars->xd_otherend_watch); 86 free(ivars->xd_otherend_watch.node, M_XENBUS); 87 ivars->xd_otherend_watch.node = NULL; 88 } 89 90 if (ivars->xd_local_watch.node != NULL) { 91 xs_unregister_watch(&ivars->xd_local_watch); 92 ivars->xd_local_watch.node = NULL; 93 } 94 95 if (ivars->xd_node != NULL) { 96 free(ivars->xd_node, M_XENBUS); 97 ivars->xd_node = NULL; 98 } 99 ivars->xd_node_len = 0; 100 101 if (ivars->xd_type != NULL) { 102 free(ivars->xd_type, M_XENBUS); 103 ivars->xd_type = NULL; 104 } 105 106 if (ivars->xd_otherend_path != NULL) { 107 free(ivars->xd_otherend_path, M_XENBUS); 108 ivars->xd_otherend_path = NULL; 109 } 110 ivars->xd_otherend_path_len = 0; 111 112 free(ivars, M_XENBUS); 113 } 114 115 /** 116 * XenBus watch callback registered against the "state" XenStore 117 * node of the other-end of a split device connection. 118 * 119 * This callback is invoked whenever the state of a device instance's 120 * peer changes. 121 * 122 * \param watch The xs_watch object used to register this callback 123 * function. 124 * \param vec An array of pointers to NUL terminated strings containing 125 * watch event data. The vector should be indexed via the 126 * xs_watch_type enum in xs_wire.h. 127 * \param vec_size The number of elements in vec. 128 */ 129 static void 130 xenbusb_otherend_watch_cb(struct xs_watch *watch, const char **vec, 131 unsigned int vec_size __unused) 132 { 133 struct xenbus_device_ivars *ivars; 134 device_t child; 135 device_t bus; 136 const char *path; 137 enum xenbus_state newstate; 138 139 ivars = (struct xenbus_device_ivars *)watch->callback_data; 140 child = ivars->xd_dev; 141 bus = device_get_parent(child); 142 143 path = vec[XS_WATCH_PATH]; 144 if (ivars->xd_otherend_path == NULL 145 || strncmp(ivars->xd_otherend_path, path, ivars->xd_otherend_path_len)) 146 return; 147 148 newstate = xenbus_read_driver_state(ivars->xd_otherend_path); 149 XENBUSB_OTHEREND_CHANGED(bus, child, newstate); 150 } 151 152 /** 153 * XenBus watch callback registered against the XenStore sub-tree 154 * represnting the local half of a split device connection. 155 * 156 * This callback is invoked whenever any XenStore data in the subtree 157 * is modified, either by us or another privledged domain. 158 * 159 * \param watch The xs_watch object used to register this callback 160 * function. 161 * \param vec An array of pointers to NUL terminated strings containing 162 * watch event data. The vector should be indexed via the 163 * xs_watch_type enum in xs_wire.h. 164 * \param vec_size The number of elements in vec. 165 * 166 */ 167 static void 168 xenbusb_local_watch_cb(struct xs_watch *watch, const char **vec, 169 unsigned int vec_size __unused) 170 { 171 struct xenbus_device_ivars *ivars; 172 device_t child; 173 device_t bus; 174 const char *path; 175 176 ivars = (struct xenbus_device_ivars *)watch->callback_data; 177 child = ivars->xd_dev; 178 bus = device_get_parent(child); 179 180 path = vec[XS_WATCH_PATH]; 181 if (ivars->xd_node == NULL 182 || strncmp(ivars->xd_node, path, ivars->xd_node_len)) 183 return; 184 185 XENBUSB_LOCALEND_CHANGED(bus, child, &path[ivars->xd_node_len]); 186 } 187 188 /** 189 * Search our internal record of configured devices (not the XenStore) 190 * to determine if the XenBus device indicated by \a node is known to 191 * the system. 192 * 193 * \param dev The XenBus bus instance to search for device children. 194 * \param node The XenStore node path for the device to find. 195 * 196 * \return The device_t of the found device if any, or NULL. 197 * 198 * \note device_t is a pointer type, so it can be compared against 199 * NULL for validity. 200 */ 201 static device_t 202 xenbusb_device_exists(device_t dev, const char *node) 203 { 204 device_t *kids; 205 device_t result; 206 struct xenbus_device_ivars *ivars; 207 int i, count; 208 209 if (device_get_children(dev, &kids, &count)) 210 return (FALSE); 211 212 result = NULL; 213 for (i = 0; i < count; i++) { 214 ivars = device_get_ivars(kids[i]); 215 if (!strcmp(ivars->xd_node, node)) { 216 result = kids[i]; 217 break; 218 } 219 } 220 free(kids, M_TEMP); 221 222 return (result); 223 } 224 225 static void 226 xenbusb_delete_child(device_t dev, device_t child) 227 { 228 struct xenbus_device_ivars *ivars; 229 230 ivars = device_get_ivars(child); 231 232 /* 233 * We no longer care about the otherend of the 234 * connection. Cancel the watches now so that we 235 * don't try to handle an event for a partially 236 * detached child. 237 */ 238 if (ivars->xd_otherend_watch.node != NULL) 239 xs_unregister_watch(&ivars->xd_otherend_watch); 240 if (ivars->xd_local_watch.node != NULL) 241 xs_unregister_watch(&ivars->xd_local_watch); 242 243 device_delete_child(dev, child); 244 xenbusb_free_child_ivars(ivars); 245 } 246 247 /** 248 * \param dev The NewBus device representing this XenBus bus. 249 * \param child The NewBus device representing a child of dev%'s XenBus bus. 250 */ 251 static void 252 xenbusb_verify_device(device_t dev, device_t child) 253 { 254 if (xs_exists(XST_NIL, xenbus_get_node(child), "state") == 0) { 255 /* 256 * Device tree has been removed from Xenbus. 257 * Tear down the device. 258 */ 259 xenbusb_delete_child(dev, child); 260 } 261 } 262 263 /** 264 * \brief Enumerate the devices on a XenBus bus and register them with 265 * the NewBus device tree. 266 * 267 * xenbusb_enumerate_bus() will create entries (in state DS_NOTPRESENT) 268 * for nodes that appear in the XenStore, but will not invoke probe/attach 269 * operations on drivers. Probe/Attach processing must be separately 270 * performed via an invocation of xenbusb_probe_children(). This is usually 271 * done via the xbs_probe_children task. 272 * 273 * \param xbs XenBus Bus device softc of the owner of the bus to enumerate. 274 * 275 * \return On success, 0. Otherwise an errno value indicating the 276 * type of failure. 277 */ 278 static int 279 xenbusb_enumerate_bus(struct xenbusb_softc *xbs) 280 { 281 const char **types; 282 u_int type_idx; 283 u_int type_count; 284 int error; 285 286 error = xs_directory(XST_NIL, xbs->xbs_node, "", &type_count, &types); 287 if (error) 288 return (error); 289 290 for (type_idx = 0; type_idx < type_count; type_idx++) 291 XENBUSB_ENUMERATE_TYPE(xbs->xbs_dev, types[type_idx]); 292 293 free(types, M_XENSTORE); 294 295 return (0); 296 } 297 298 /** 299 * Handler for all generic XenBus device systcl nodes. 300 */ 301 static int 302 xenbusb_device_sysctl_handler(SYSCTL_HANDLER_ARGS) 303 { 304 device_t dev; 305 const char *value; 306 307 dev = (device_t)arg1; 308 switch (arg2) { 309 case XENBUS_IVAR_NODE: 310 value = xenbus_get_node(dev); 311 break; 312 case XENBUS_IVAR_TYPE: 313 value = xenbus_get_type(dev); 314 break; 315 case XENBUS_IVAR_STATE: 316 value = xenbus_strstate(xenbus_get_state(dev)); 317 break; 318 case XENBUS_IVAR_OTHEREND_ID: 319 return (sysctl_handle_int(oidp, NULL, 320 xenbus_get_otherend_id(dev), 321 req)); 322 /* NOTREACHED */ 323 case XENBUS_IVAR_OTHEREND_PATH: 324 value = xenbus_get_otherend_path(dev); 325 break; 326 default: 327 return (EINVAL); 328 } 329 return (SYSCTL_OUT_STR(req, value)); 330 } 331 332 /** 333 * Create read-only systcl nodes for xenbusb device ivar data. 334 * 335 * \param dev The XenBus device instance to register with sysctl. 336 */ 337 static void 338 xenbusb_device_sysctl_init(device_t dev) 339 { 340 struct sysctl_ctx_list *ctx; 341 struct sysctl_oid *tree; 342 343 ctx = device_get_sysctl_ctx(dev); 344 tree = device_get_sysctl_tree(dev); 345 346 SYSCTL_ADD_PROC(ctx, 347 SYSCTL_CHILDREN(tree), 348 OID_AUTO, 349 "xenstore_path", 350 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 351 dev, 352 XENBUS_IVAR_NODE, 353 xenbusb_device_sysctl_handler, 354 "A", 355 "XenStore path to device"); 356 357 SYSCTL_ADD_PROC(ctx, 358 SYSCTL_CHILDREN(tree), 359 OID_AUTO, 360 "xenbus_dev_type", 361 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 362 dev, 363 XENBUS_IVAR_TYPE, 364 xenbusb_device_sysctl_handler, 365 "A", 366 "XenBus device type"); 367 368 SYSCTL_ADD_PROC(ctx, 369 SYSCTL_CHILDREN(tree), 370 OID_AUTO, 371 "xenbus_connection_state", 372 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 373 dev, 374 XENBUS_IVAR_STATE, 375 xenbusb_device_sysctl_handler, 376 "A", 377 "XenBus state of peer connection"); 378 379 SYSCTL_ADD_PROC(ctx, 380 SYSCTL_CHILDREN(tree), 381 OID_AUTO, 382 "xenbus_peer_domid", 383 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 384 dev, 385 XENBUS_IVAR_OTHEREND_ID, 386 xenbusb_device_sysctl_handler, 387 "I", 388 "Xen domain ID of peer"); 389 390 SYSCTL_ADD_PROC(ctx, 391 SYSCTL_CHILDREN(tree), 392 OID_AUTO, 393 "xenstore_peer_path", 394 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 395 dev, 396 XENBUS_IVAR_OTHEREND_PATH, 397 xenbusb_device_sysctl_handler, 398 "A", 399 "XenStore path to peer device"); 400 } 401 402 /** 403 * \brief Decrement the number of XenBus child devices in the 404 * connecting state by one and release the xbs_attch_ch 405 * interrupt configuration hook if the connecting count 406 * drops to zero. 407 * 408 * \param xbs XenBus Bus device softc of the owner of the bus to enumerate. 409 */ 410 static void 411 xenbusb_release_confighook(struct xenbusb_softc *xbs) 412 { 413 mtx_lock(&xbs->xbs_lock); 414 KASSERT(xbs->xbs_connecting_children > 0, 415 ("Connecting device count error\n")); 416 xbs->xbs_connecting_children--; 417 if (xbs->xbs_connecting_children == 0 418 && (xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) { 419 xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE; 420 mtx_unlock(&xbs->xbs_lock); 421 config_intrhook_disestablish(&xbs->xbs_attach_ch); 422 } else { 423 mtx_unlock(&xbs->xbs_lock); 424 } 425 } 426 427 /** 428 * \brief Verify the existence of attached device instances and perform 429 * probe/attach processing for newly arrived devices. 430 * 431 * \param dev The NewBus device representing this XenBus bus. 432 * 433 * \return On success, 0. Otherwise an errno value indicating the 434 * type of failure. 435 */ 436 static int 437 xenbusb_probe_children(device_t dev) 438 { 439 device_t *kids; 440 struct xenbus_device_ivars *ivars; 441 int i, count, error; 442 443 if (device_get_children(dev, &kids, &count) == 0) { 444 for (i = 0; i < count; i++) { 445 if (device_get_state(kids[i]) != DS_NOTPRESENT) { 446 /* 447 * We already know about this one. 448 * Make sure it's still here. 449 */ 450 xenbusb_verify_device(dev, kids[i]); 451 continue; 452 } 453 454 error = device_probe_and_attach(kids[i]); 455 if (error == ENXIO) { 456 struct xenbusb_softc *xbs; 457 458 /* 459 * We don't have a PV driver for this device. 460 * However, an emulated device we do support 461 * may share this backend. Hide the node from 462 * XenBus until the next rescan, but leave it's 463 * state unchanged so we don't inadvertently 464 * prevent attachment of any emulated device. 465 */ 466 xenbusb_delete_child(dev, kids[i]); 467 468 /* 469 * Since the XenStore state of this device 470 * still indicates a pending attach, manually 471 * release it's hold on the boot process. 472 */ 473 xbs = device_get_softc(dev); 474 xenbusb_release_confighook(xbs); 475 476 continue; 477 } else if (error) { 478 /* 479 * Transition device to the closed state 480 * so the world knows that attachment will 481 * not occur. 482 */ 483 xenbus_set_state(kids[i], XenbusStateClosed); 484 485 /* 486 * Remove our record of this device. 487 * So long as it remains in the closed 488 * state in the XenStore, we will not find 489 * it again. The state will only change 490 * if the control domain actively reconfigures 491 * this device. 492 */ 493 xenbusb_delete_child(dev, kids[i]); 494 495 continue; 496 } 497 /* 498 * Augment default newbus provided dynamic sysctl 499 * variables with the standard ivar contents of 500 * XenBus devices. 501 */ 502 xenbusb_device_sysctl_init(kids[i]); 503 504 /* 505 * Now that we have a driver managing this device 506 * that can receive otherend state change events, 507 * hook up a watch for them. 508 */ 509 ivars = device_get_ivars(kids[i]); 510 xs_register_watch(&ivars->xd_otherend_watch); 511 xs_register_watch(&ivars->xd_local_watch); 512 } 513 free(kids, M_TEMP); 514 } 515 516 return (0); 517 } 518 519 /** 520 * \brief Task callback function to perform XenBus probe operations 521 * from a known safe context. 522 * 523 * \param arg The NewBus device_t representing the bus instance to 524 * on which to perform probe processing. 525 * \param pending The number of times this task was queued before it could 526 * be run. 527 */ 528 static void 529 xenbusb_probe_children_cb(void *arg, int pending __unused) 530 { 531 device_t dev = (device_t)arg; 532 533 bus_topo_lock(); 534 xenbusb_probe_children(dev); 535 bus_topo_unlock(); 536 } 537 538 /** 539 * \brief XenStore watch callback for the root node of the XenStore 540 * subtree representing a XenBus. 541 * 542 * This callback performs, or delegates to the xbs_probe_children task, 543 * all processing necessary to handle dynmaic device arrival and departure 544 * events from a XenBus. 545 * 546 * \param watch The XenStore watch object associated with this callback. 547 * \param vec The XenStore watch event data. 548 * \param len The number of fields in the event data stream. 549 */ 550 static void 551 xenbusb_devices_changed(struct xs_watch *watch, const char **vec, 552 unsigned int len) 553 { 554 struct xenbusb_softc *xbs; 555 device_t dev; 556 char *node; 557 char *type; 558 char *id; 559 char *p; 560 u_int component; 561 562 xbs = (struct xenbusb_softc *)watch->callback_data; 563 dev = xbs->xbs_dev; 564 565 if (len <= XS_WATCH_PATH) { 566 device_printf(dev, "xenbusb_devices_changed: " 567 "Short Event Data.\n"); 568 return; 569 } 570 571 node = strdup(vec[XS_WATCH_PATH], M_XENBUS); 572 p = strchr(node, '/'); 573 if (p == NULL) 574 goto out; 575 *p = 0; 576 type = p + 1; 577 578 p = strchr(type, '/'); 579 if (p == NULL) 580 goto out; 581 *p++ = 0; 582 583 /* 584 * Extract the device ID. A device ID has one or more path 585 * components separated by the '/' character. 586 * 587 * e.g. "<frontend vm id>/<frontend dev id>" for backend devices. 588 */ 589 id = p; 590 for (component = 0; component < xbs->xbs_id_components; component++) { 591 p = strchr(p, '/'); 592 if (p == NULL) 593 break; 594 p++; 595 } 596 if (p != NULL) 597 *p = 0; 598 599 if (*id != 0 && component >= xbs->xbs_id_components - 1) { 600 xenbusb_add_device(xbs->xbs_dev, type, id); 601 taskqueue_enqueue(taskqueue_thread, &xbs->xbs_probe_children); 602 } 603 out: 604 free(node, M_XENBUS); 605 } 606 607 /** 608 * \brief Interrupt configuration hook callback associated with xbs_attch_ch. 609 * 610 * Since interrupts are always functional at the time of XenBus configuration, 611 * there is nothing to be done when the callback occurs. This hook is only 612 * registered to hold up boot processing while XenBus devices come online. 613 * 614 * \param arg Unused configuration hook callback argument. 615 */ 616 static void 617 xenbusb_nop_confighook_cb(void *arg __unused) 618 { 619 } 620 621 /*--------------------------- Public Functions -------------------------------*/ 622 /*--------- API comments for these methods can be found in xenbusb.h ---------*/ 623 void 624 xenbusb_identify(driver_t *driver, device_t parent) 625 { 626 /* 627 * A single instance of each bus type for which we have a driver 628 * is always present in a system operating under Xen. 629 */ 630 BUS_ADD_CHILD(parent, 0, driver->name, 0); 631 } 632 633 int 634 xenbusb_add_device(device_t dev, const char *type, const char *id) 635 { 636 struct xenbusb_softc *xbs; 637 struct sbuf *devpath_sbuf; 638 char *devpath; 639 struct xenbus_device_ivars *ivars; 640 int error; 641 642 xbs = device_get_softc(dev); 643 devpath_sbuf = sbuf_new_auto(); 644 sbuf_printf(devpath_sbuf, "%s/%s/%s", xbs->xbs_node, type, id); 645 sbuf_finish(devpath_sbuf); 646 devpath = sbuf_data(devpath_sbuf); 647 648 ivars = malloc(sizeof(*ivars), M_XENBUS, M_ZERO|M_WAITOK); 649 error = ENXIO; 650 651 if (xs_exists(XST_NIL, devpath, "") != 0) { 652 device_t child; 653 enum xenbus_state state; 654 char *statepath; 655 656 child = xenbusb_device_exists(dev, devpath); 657 if (child != NULL) { 658 /* 659 * We are already tracking this node 660 */ 661 error = 0; 662 goto out; 663 } 664 665 state = xenbus_read_driver_state(devpath); 666 if (state != XenbusStateInitialising) { 667 /* 668 * Device is not new, so ignore it. This can 669 * happen if a device is going away after 670 * switching to Closed. 671 */ 672 printf("xenbusb_add_device: Device %s ignored. " 673 "State %d\n", devpath, state); 674 error = 0; 675 goto out; 676 } 677 678 sx_init(&ivars->xd_lock, "xdlock"); 679 ivars->xd_flags = XDF_CONNECTING; 680 ivars->xd_node = strdup(devpath, M_XENBUS); 681 ivars->xd_node_len = strlen(devpath); 682 ivars->xd_type = strdup(type, M_XENBUS); 683 ivars->xd_state = XenbusStateInitialising; 684 685 error = XENBUSB_GET_OTHEREND_NODE(dev, ivars); 686 if (error) { 687 printf("xenbus_update_device: %s no otherend id\n", 688 devpath); 689 goto out; 690 } 691 692 statepath = malloc(ivars->xd_otherend_path_len 693 + strlen("/state") + 1, M_XENBUS, M_WAITOK); 694 sprintf(statepath, "%s/state", ivars->xd_otherend_path); 695 ivars->xd_otherend_watch.node = statepath; 696 ivars->xd_otherend_watch.callback = xenbusb_otherend_watch_cb; 697 ivars->xd_otherend_watch.callback_data = (uintptr_t)ivars; 698 /* 699 * Other end state node watch, limit to one pending event 700 * to prevent frontends from queuing too many events that 701 * could cause resource starvation. 702 */ 703 ivars->xd_otherend_watch.max_pending = 1; 704 705 ivars->xd_local_watch.node = ivars->xd_node; 706 ivars->xd_local_watch.callback = xenbusb_local_watch_cb; 707 ivars->xd_local_watch.callback_data = (uintptr_t)ivars; 708 /* 709 * Watch our local path, only writable by us or a privileged 710 * domain, no need to limit. 711 */ 712 ivars->xd_local_watch.max_pending = 0; 713 714 mtx_lock(&xbs->xbs_lock); 715 xbs->xbs_connecting_children++; 716 mtx_unlock(&xbs->xbs_lock); 717 718 child = device_add_child(dev, NULL, DEVICE_UNIT_ANY); 719 ivars->xd_dev = child; 720 device_set_ivars(child, ivars); 721 } 722 723 out: 724 sbuf_delete(devpath_sbuf); 725 if (error != 0) 726 xenbusb_free_child_ivars(ivars); 727 728 return (error); 729 } 730 731 int 732 xenbusb_attach(device_t dev, char *bus_node, u_int id_components) 733 { 734 struct xenbusb_softc *xbs; 735 736 xbs = device_get_softc(dev); 737 mtx_init(&xbs->xbs_lock, "xenbusb softc lock", NULL, MTX_DEF); 738 xbs->xbs_node = bus_node; 739 xbs->xbs_id_components = id_components; 740 xbs->xbs_dev = dev; 741 742 /* 743 * Since XenBus buses are attached to the XenStore, and 744 * the XenStore does not probe children until after interrupt 745 * services are available, this config hook is used solely 746 * to ensure that the remainder of the boot process (e.g. 747 * mount root) is deferred until child devices are adequately 748 * probed. We unblock the boot process as soon as the 749 * connecting child count in our softc goes to 0. 750 */ 751 xbs->xbs_attach_ch.ich_func = xenbusb_nop_confighook_cb; 752 xbs->xbs_attach_ch.ich_arg = dev; 753 config_intrhook_establish(&xbs->xbs_attach_ch); 754 xbs->xbs_flags |= XBS_ATTACH_CH_ACTIVE; 755 xbs->xbs_connecting_children = 1; 756 757 /* 758 * The subtree for this bus type may not yet exist 759 * causing initial enumeration to fail. We still 760 * want to return success from our attach though 761 * so that we are ready to handle devices for this 762 * bus when they are dynamically attached to us 763 * by a Xen management action. 764 */ 765 (void)xenbusb_enumerate_bus(xbs); 766 xenbusb_probe_children(dev); 767 768 xbs->xbs_device_watch.node = bus_node; 769 xbs->xbs_device_watch.callback = xenbusb_devices_changed; 770 xbs->xbs_device_watch.callback_data = (uintptr_t)xbs; 771 /* 772 * Allow for unlimited pending watches, as those are local paths 773 * either controlled by the guest or only writable by privileged 774 * domains. 775 */ 776 xbs->xbs_device_watch.max_pending = 0; 777 778 TASK_INIT(&xbs->xbs_probe_children, 0, xenbusb_probe_children_cb, dev); 779 780 xs_register_watch(&xbs->xbs_device_watch); 781 782 xenbusb_release_confighook(xbs); 783 784 return (0); 785 } 786 787 int 788 xenbusb_resume(device_t dev) 789 { 790 device_t *kids; 791 struct xenbus_device_ivars *ivars; 792 int i, count, error; 793 char *statepath; 794 795 /* 796 * We must re-examine each device and find the new path for 797 * its backend. 798 */ 799 if (device_get_children(dev, &kids, &count) == 0) { 800 for (i = 0; i < count; i++) { 801 if (device_get_state(kids[i]) == DS_NOTPRESENT) 802 continue; 803 804 if (xen_suspend_cancelled) { 805 DEVICE_RESUME(kids[i]); 806 continue; 807 } 808 809 ivars = device_get_ivars(kids[i]); 810 811 xs_unregister_watch(&ivars->xd_otherend_watch); 812 xenbus_set_state(kids[i], XenbusStateInitialising); 813 814 /* 815 * Find the new backend details and 816 * re-register our watch. 817 */ 818 error = XENBUSB_GET_OTHEREND_NODE(dev, ivars); 819 if (error) 820 return (error); 821 822 statepath = malloc(ivars->xd_otherend_path_len 823 + strlen("/state") + 1, M_XENBUS, M_WAITOK); 824 sprintf(statepath, "%s/state", ivars->xd_otherend_path); 825 826 free(ivars->xd_otherend_watch.node, M_XENBUS); 827 ivars->xd_otherend_watch.node = statepath; 828 829 DEVICE_RESUME(kids[i]); 830 831 xs_register_watch(&ivars->xd_otherend_watch); 832 #if 0 833 /* 834 * Can't do this yet since we are running in 835 * the xenwatch thread and if we sleep here, 836 * we will stop delivering watch notifications 837 * and the device will never come back online. 838 */ 839 sx_xlock(&ivars->xd_lock); 840 while (ivars->xd_state != XenbusStateClosed 841 && ivars->xd_state != XenbusStateConnected) 842 sx_sleep(&ivars->xd_state, &ivars->xd_lock, 843 0, "xdresume", 0); 844 sx_xunlock(&ivars->xd_lock); 845 #endif 846 } 847 free(kids, M_TEMP); 848 } 849 850 return (0); 851 } 852 853 int 854 xenbusb_print_child(device_t dev, device_t child) 855 { 856 struct xenbus_device_ivars *ivars = device_get_ivars(child); 857 int retval = 0; 858 859 retval += bus_print_child_header(dev, child); 860 retval += printf(" at %s", ivars->xd_node); 861 retval += bus_print_child_footer(dev, child); 862 863 return (retval); 864 } 865 866 int 867 xenbusb_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 868 { 869 struct xenbus_device_ivars *ivars = device_get_ivars(child); 870 871 switch (index) { 872 case XENBUS_IVAR_NODE: 873 *result = (uintptr_t) ivars->xd_node; 874 return (0); 875 876 case XENBUS_IVAR_TYPE: 877 *result = (uintptr_t) ivars->xd_type; 878 return (0); 879 880 case XENBUS_IVAR_STATE: 881 *result = (uintptr_t) ivars->xd_state; 882 return (0); 883 884 case XENBUS_IVAR_OTHEREND_ID: 885 *result = (uintptr_t) ivars->xd_otherend_id; 886 return (0); 887 888 case XENBUS_IVAR_OTHEREND_PATH: 889 *result = (uintptr_t) ivars->xd_otherend_path; 890 return (0); 891 } 892 893 return (ENOENT); 894 } 895 896 int 897 xenbusb_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 898 { 899 struct xenbus_device_ivars *ivars = device_get_ivars(child); 900 enum xenbus_state newstate; 901 int currstate; 902 903 switch (index) { 904 case XENBUS_IVAR_STATE: 905 { 906 int error; 907 struct xs_transaction xst; 908 909 newstate = (enum xenbus_state)value; 910 sx_xlock(&ivars->xd_lock); 911 if (ivars->xd_state == newstate) { 912 error = 0; 913 goto out; 914 } 915 916 do { 917 error = xs_transaction_start(&xst); 918 if (error != 0) 919 goto out; 920 921 do { 922 error = xs_scanf(xst, ivars->xd_node, "state", 923 NULL, "%d", &currstate); 924 } while (error == EAGAIN); 925 if (error) 926 goto out; 927 928 do { 929 error = xs_printf(xst, ivars->xd_node, "state", 930 "%d", newstate); 931 } while (error == EAGAIN); 932 if (error) { 933 /* 934 * Avoid looping through xenbus_dev_fatal() 935 * which calls xenbus_write_ivar to set the 936 * state to closing. 937 */ 938 if (newstate != XenbusStateClosing) 939 xenbus_dev_fatal(dev, error, 940 "writing new state"); 941 goto out; 942 } 943 } while (xs_transaction_end(xst, 0)); 944 ivars->xd_state = newstate; 945 946 if ((ivars->xd_flags & XDF_CONNECTING) != 0 && 947 (newstate == XenbusStateClosed || 948 newstate == XenbusStateConnected)) { 949 struct xenbusb_softc *xbs; 950 951 ivars->xd_flags &= ~XDF_CONNECTING; 952 xbs = device_get_softc(dev); 953 xenbusb_release_confighook(xbs); 954 } 955 956 wakeup(&ivars->xd_state); 957 out: 958 if (error != 0) 959 xs_transaction_end(xst, 1); 960 sx_xunlock(&ivars->xd_lock); 961 /* 962 * Shallow ENOENT errors, as returning an error here will 963 * trigger a panic. ENOENT is fine to ignore, because it means 964 * the toolstack has removed the state node as part of 965 * destroying the device, and so we have to shut down the 966 * device without recreating it or else the node would be 967 * leaked. 968 */ 969 return (error == ENOENT ? 0 : error); 970 } 971 972 case XENBUS_IVAR_NODE: 973 case XENBUS_IVAR_TYPE: 974 case XENBUS_IVAR_OTHEREND_ID: 975 case XENBUS_IVAR_OTHEREND_PATH: 976 /* 977 * These variables are read-only. 978 */ 979 return (EINVAL); 980 } 981 982 return (ENOENT); 983 } 984 985 void 986 xenbusb_otherend_changed(device_t bus, device_t child, enum xenbus_state state) 987 { 988 XENBUS_OTHEREND_CHANGED(child, state); 989 } 990 991 void 992 xenbusb_localend_changed(device_t bus, device_t child, const char *path) 993 { 994 995 if (strcmp(path, "/state") != 0) { 996 struct xenbus_device_ivars *ivars; 997 998 ivars = device_get_ivars(child); 999 sx_xlock(&ivars->xd_lock); 1000 ivars->xd_state = xenbus_read_driver_state(ivars->xd_node); 1001 sx_xunlock(&ivars->xd_lock); 1002 } 1003 XENBUS_LOCALEND_CHANGED(child, path); 1004 } 1005