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