1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1997,1998,2003 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include "opt_bus.h" 31 #include "opt_ddb.h" 32 33 #include <sys/param.h> 34 #include <sys/conf.h> 35 #include <sys/domainset.h> 36 #include <sys/eventhandler.h> 37 #include <sys/lock.h> 38 #include <sys/kernel.h> 39 #include <sys/limits.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/priv.h> 44 #include <machine/bus.h> 45 #include <sys/random.h> 46 #include <sys/refcount.h> 47 #include <sys/rman.h> 48 #include <sys/sbuf.h> 49 #include <sys/smp.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/bus.h> 53 #include <sys/cpuset.h> 54 55 #include <net/vnet.h> 56 57 #include <machine/cpu.h> 58 #include <machine/stdarg.h> 59 60 #include <vm/uma.h> 61 #include <vm/vm.h> 62 63 #include <ddb/ddb.h> 64 65 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 66 NULL); 67 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 68 NULL); 69 70 static bool disable_failed_devs = false; 71 SYSCTL_BOOL(_hw_bus, OID_AUTO, disable_failed_devices, CTLFLAG_RWTUN, &disable_failed_devs, 72 0, "Do not retry attaching devices that return an error from DEVICE_ATTACH the first time"); 73 74 /* 75 * Used to attach drivers to devclasses. 76 */ 77 typedef struct driverlink *driverlink_t; 78 struct driverlink { 79 kobj_class_t driver; 80 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 81 int pass; 82 int flags; 83 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */ 84 TAILQ_ENTRY(driverlink) passlink; 85 }; 86 87 /* 88 * Forward declarations 89 */ 90 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 91 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 92 typedef TAILQ_HEAD(device_list, _device) device_list_t; 93 94 struct devclass { 95 TAILQ_ENTRY(devclass) link; 96 devclass_t parent; /* parent in devclass hierarchy */ 97 driver_list_t drivers; /* bus devclasses store drivers for bus */ 98 char *name; 99 device_t *devices; /* array of devices indexed by unit */ 100 int maxunit; /* size of devices array */ 101 int flags; 102 #define DC_HAS_CHILDREN 1 103 104 struct sysctl_ctx_list sysctl_ctx; 105 struct sysctl_oid *sysctl_tree; 106 }; 107 108 /** 109 * @brief Implementation of _device. 110 * 111 * The structure is named "_device" instead of "device" to avoid type confusion 112 * caused by other subsystems defining a (struct device). 113 */ 114 struct _device { 115 /* 116 * A device is a kernel object. The first field must be the 117 * current ops table for the object. 118 */ 119 KOBJ_FIELDS; 120 121 /* 122 * Device hierarchy. 123 */ 124 TAILQ_ENTRY(_device) link; /**< list of devices in parent */ 125 TAILQ_ENTRY(_device) devlink; /**< global device list membership */ 126 device_t parent; /**< parent of this device */ 127 device_list_t children; /**< list of child devices */ 128 129 /* 130 * Details of this device. 131 */ 132 driver_t *driver; /**< current driver */ 133 devclass_t devclass; /**< current device class */ 134 int unit; /**< current unit number */ 135 char* nameunit; /**< name+unit e.g. foodev0 */ 136 char* desc; /**< driver specific description */ 137 u_int busy; /**< count of calls to device_busy() */ 138 device_state_t state; /**< current device state */ 139 uint32_t devflags; /**< api level flags for device_get_flags() */ 140 u_int flags; /**< internal device flags */ 141 u_int order; /**< order from device_add_child_ordered() */ 142 void *ivars; /**< instance variables */ 143 void *softc; /**< current driver's variables */ 144 145 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ 146 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ 147 }; 148 149 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 150 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 151 152 EVENTHANDLER_LIST_DEFINE(device_attach); 153 EVENTHANDLER_LIST_DEFINE(device_detach); 154 EVENTHANDLER_LIST_DEFINE(device_nomatch); 155 EVENTHANDLER_LIST_DEFINE(dev_lookup); 156 157 static void devctl2_init(void); 158 static bool device_frozen; 159 160 #define DRIVERNAME(d) ((d)? d->name : "no driver") 161 #define DEVCLANAME(d) ((d)? d->name : "no devclass") 162 163 #ifdef BUS_DEBUG 164 165 static int bus_debug = 1; 166 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0, 167 "Bus debug level"); 168 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 169 #define DEVICENAME(d) ((d)? device_get_name(d): "no device") 170 171 /** 172 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 173 * prevent syslog from deleting initial spaces 174 */ 175 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 176 177 static void print_device_short(device_t dev, int indent); 178 static void print_device(device_t dev, int indent); 179 void print_device_tree_short(device_t dev, int indent); 180 void print_device_tree(device_t dev, int indent); 181 static void print_driver_short(driver_t *driver, int indent); 182 static void print_driver(driver_t *driver, int indent); 183 static void print_driver_list(driver_list_t drivers, int indent); 184 static void print_devclass_short(devclass_t dc, int indent); 185 static void print_devclass(devclass_t dc, int indent); 186 void print_devclass_list_short(void); 187 void print_devclass_list(void); 188 189 #else 190 /* Make the compiler ignore the function calls */ 191 #define PDEBUG(a) /* nop */ 192 #define DEVICENAME(d) /* nop */ 193 194 #define print_device_short(d,i) /* nop */ 195 #define print_device(d,i) /* nop */ 196 #define print_device_tree_short(d,i) /* nop */ 197 #define print_device_tree(d,i) /* nop */ 198 #define print_driver_short(d,i) /* nop */ 199 #define print_driver(d,i) /* nop */ 200 #define print_driver_list(d,i) /* nop */ 201 #define print_devclass_short(d,i) /* nop */ 202 #define print_devclass(d,i) /* nop */ 203 #define print_devclass_list_short() /* nop */ 204 #define print_devclass_list() /* nop */ 205 #endif 206 207 /* 208 * dev sysctl tree 209 */ 210 211 enum { 212 DEVCLASS_SYSCTL_PARENT, 213 }; 214 215 static int 216 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS) 217 { 218 devclass_t dc = (devclass_t)arg1; 219 const char *value; 220 221 switch (arg2) { 222 case DEVCLASS_SYSCTL_PARENT: 223 value = dc->parent ? dc->parent->name : ""; 224 break; 225 default: 226 return (EINVAL); 227 } 228 return (SYSCTL_OUT_STR(req, value)); 229 } 230 231 static void 232 devclass_sysctl_init(devclass_t dc) 233 { 234 if (dc->sysctl_tree != NULL) 235 return; 236 sysctl_ctx_init(&dc->sysctl_ctx); 237 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx, 238 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name, 239 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 240 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree), 241 OID_AUTO, "%parent", 242 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 243 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A", 244 "parent class"); 245 } 246 247 enum { 248 DEVICE_SYSCTL_DESC, 249 DEVICE_SYSCTL_DRIVER, 250 DEVICE_SYSCTL_LOCATION, 251 DEVICE_SYSCTL_PNPINFO, 252 DEVICE_SYSCTL_PARENT, 253 }; 254 255 static int 256 device_sysctl_handler(SYSCTL_HANDLER_ARGS) 257 { 258 struct sbuf sb; 259 device_t dev = (device_t)arg1; 260 int error; 261 262 sbuf_new_for_sysctl(&sb, NULL, 1024, req); 263 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 264 bus_topo_lock(); 265 switch (arg2) { 266 case DEVICE_SYSCTL_DESC: 267 sbuf_cat(&sb, dev->desc ? dev->desc : ""); 268 break; 269 case DEVICE_SYSCTL_DRIVER: 270 sbuf_cat(&sb, dev->driver ? dev->driver->name : ""); 271 break; 272 case DEVICE_SYSCTL_LOCATION: 273 bus_child_location(dev, &sb); 274 break; 275 case DEVICE_SYSCTL_PNPINFO: 276 bus_child_pnpinfo(dev, &sb); 277 break; 278 case DEVICE_SYSCTL_PARENT: 279 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : ""); 280 break; 281 default: 282 error = EINVAL; 283 goto out; 284 } 285 error = sbuf_finish(&sb); 286 out: 287 bus_topo_unlock(); 288 sbuf_delete(&sb); 289 return (error); 290 } 291 292 static void 293 device_sysctl_init(device_t dev) 294 { 295 devclass_t dc = dev->devclass; 296 int domain; 297 298 if (dev->sysctl_tree != NULL) 299 return; 300 devclass_sysctl_init(dc); 301 sysctl_ctx_init(&dev->sysctl_ctx); 302 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx, 303 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO, 304 dev->nameunit + strlen(dc->name), 305 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index"); 306 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 307 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 308 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 309 "device description"); 310 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 311 OID_AUTO, "%driver", 312 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 313 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 314 "device driver name"); 315 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 316 OID_AUTO, "%location", 317 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 318 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 319 "device location relative to parent"); 320 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 321 OID_AUTO, "%pnpinfo", 322 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 323 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 324 "device identification"); 325 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 326 OID_AUTO, "%parent", 327 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 328 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A", 329 "parent device"); 330 if (bus_get_domain(dev, &domain) == 0) 331 SYSCTL_ADD_INT(&dev->sysctl_ctx, 332 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain", 333 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, domain, "NUMA domain"); 334 } 335 336 static void 337 device_sysctl_update(device_t dev) 338 { 339 devclass_t dc = dev->devclass; 340 341 if (dev->sysctl_tree == NULL) 342 return; 343 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name)); 344 } 345 346 static void 347 device_sysctl_fini(device_t dev) 348 { 349 if (dev->sysctl_tree == NULL) 350 return; 351 sysctl_ctx_free(&dev->sysctl_ctx); 352 dev->sysctl_tree = NULL; 353 } 354 355 static struct device_list bus_data_devices; 356 static int bus_data_generation = 1; 357 358 static kobj_method_t null_methods[] = { 359 KOBJMETHOD_END 360 }; 361 362 DEFINE_CLASS(null, null_methods, 0); 363 364 void 365 bus_topo_assert(void) 366 { 367 368 GIANT_REQUIRED; 369 } 370 371 struct mtx * 372 bus_topo_mtx(void) 373 { 374 375 return (&Giant); 376 } 377 378 void 379 bus_topo_lock(void) 380 { 381 382 mtx_lock(bus_topo_mtx()); 383 } 384 385 void 386 bus_topo_unlock(void) 387 { 388 389 mtx_unlock(bus_topo_mtx()); 390 } 391 392 /* 393 * Bus pass implementation 394 */ 395 396 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes); 397 int bus_current_pass = BUS_PASS_ROOT; 398 399 /** 400 * @internal 401 * @brief Register the pass level of a new driver attachment 402 * 403 * Register a new driver attachment's pass level. If no driver 404 * attachment with the same pass level has been added, then @p new 405 * will be added to the global passes list. 406 * 407 * @param new the new driver attachment 408 */ 409 static void 410 driver_register_pass(struct driverlink *new) 411 { 412 struct driverlink *dl; 413 414 /* We only consider pass numbers during boot. */ 415 if (bus_current_pass == BUS_PASS_DEFAULT) 416 return; 417 418 /* 419 * Walk the passes list. If we already know about this pass 420 * then there is nothing to do. If we don't, then insert this 421 * driver link into the list. 422 */ 423 TAILQ_FOREACH(dl, &passes, passlink) { 424 if (dl->pass < new->pass) 425 continue; 426 if (dl->pass == new->pass) 427 return; 428 TAILQ_INSERT_BEFORE(dl, new, passlink); 429 return; 430 } 431 TAILQ_INSERT_TAIL(&passes, new, passlink); 432 } 433 434 /** 435 * @brief Raise the current bus pass 436 * 437 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS() 438 * method on the root bus to kick off a new device tree scan for each 439 * new pass level that has at least one driver. 440 */ 441 void 442 bus_set_pass(int pass) 443 { 444 struct driverlink *dl; 445 446 if (bus_current_pass > pass) 447 panic("Attempt to lower bus pass level"); 448 449 TAILQ_FOREACH(dl, &passes, passlink) { 450 /* Skip pass values below the current pass level. */ 451 if (dl->pass <= bus_current_pass) 452 continue; 453 454 /* 455 * Bail once we hit a driver with a pass level that is 456 * too high. 457 */ 458 if (dl->pass > pass) 459 break; 460 461 /* 462 * Raise the pass level to the next level and rescan 463 * the tree. 464 */ 465 bus_current_pass = dl->pass; 466 BUS_NEW_PASS(root_bus); 467 } 468 469 /* 470 * If there isn't a driver registered for the requested pass, 471 * then bus_current_pass might still be less than 'pass'. Set 472 * it to 'pass' in that case. 473 */ 474 if (bus_current_pass < pass) 475 bus_current_pass = pass; 476 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level")); 477 } 478 479 /* 480 * Devclass implementation 481 */ 482 483 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 484 485 /** 486 * @internal 487 * @brief Find or create a device class 488 * 489 * If a device class with the name @p classname exists, return it, 490 * otherwise if @p create is non-zero create and return a new device 491 * class. 492 * 493 * If @p parentname is non-NULL, the parent of the devclass is set to 494 * the devclass of that name. 495 * 496 * @param classname the devclass name to find or create 497 * @param parentname the parent devclass name or @c NULL 498 * @param create non-zero to create a devclass 499 */ 500 static devclass_t 501 devclass_find_internal(const char *classname, const char *parentname, 502 int create) 503 { 504 devclass_t dc; 505 506 PDEBUG(("looking for %s", classname)); 507 if (!classname) 508 return (NULL); 509 510 TAILQ_FOREACH(dc, &devclasses, link) { 511 if (!strcmp(dc->name, classname)) 512 break; 513 } 514 515 if (create && !dc) { 516 PDEBUG(("creating %s", classname)); 517 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 518 M_BUS, M_NOWAIT | M_ZERO); 519 if (!dc) 520 return (NULL); 521 dc->parent = NULL; 522 dc->name = (char*) (dc + 1); 523 strcpy(dc->name, classname); 524 TAILQ_INIT(&dc->drivers); 525 TAILQ_INSERT_TAIL(&devclasses, dc, link); 526 527 bus_data_generation_update(); 528 } 529 530 /* 531 * If a parent class is specified, then set that as our parent so 532 * that this devclass will support drivers for the parent class as 533 * well. If the parent class has the same name don't do this though 534 * as it creates a cycle that can trigger an infinite loop in 535 * device_probe_child() if a device exists for which there is no 536 * suitable driver. 537 */ 538 if (parentname && dc && !dc->parent && 539 strcmp(classname, parentname) != 0) { 540 dc->parent = devclass_find_internal(parentname, NULL, TRUE); 541 dc->parent->flags |= DC_HAS_CHILDREN; 542 } 543 544 return (dc); 545 } 546 547 /** 548 * @brief Create a device class 549 * 550 * If a device class with the name @p classname exists, return it, 551 * otherwise create and return a new device class. 552 * 553 * @param classname the devclass name to find or create 554 */ 555 devclass_t 556 devclass_create(const char *classname) 557 { 558 return (devclass_find_internal(classname, NULL, TRUE)); 559 } 560 561 /** 562 * @brief Find a device class 563 * 564 * If a device class with the name @p classname exists, return it, 565 * otherwise return @c NULL. 566 * 567 * @param classname the devclass name to find 568 */ 569 devclass_t 570 devclass_find(const char *classname) 571 { 572 return (devclass_find_internal(classname, NULL, FALSE)); 573 } 574 575 /** 576 * @brief Register that a device driver has been added to a devclass 577 * 578 * Register that a device driver has been added to a devclass. This 579 * is called by devclass_add_driver to accomplish the recursive 580 * notification of all the children classes of dc, as well as dc. 581 * Each layer will have BUS_DRIVER_ADDED() called for all instances of 582 * the devclass. 583 * 584 * We do a full search here of the devclass list at each iteration 585 * level to save storing children-lists in the devclass structure. If 586 * we ever move beyond a few dozen devices doing this, we may need to 587 * reevaluate... 588 * 589 * @param dc the devclass to edit 590 * @param driver the driver that was just added 591 */ 592 static void 593 devclass_driver_added(devclass_t dc, driver_t *driver) 594 { 595 devclass_t parent; 596 int i; 597 598 /* 599 * Call BUS_DRIVER_ADDED for any existing buses in this class. 600 */ 601 for (i = 0; i < dc->maxunit; i++) 602 if (dc->devices[i] && device_is_attached(dc->devices[i])) 603 BUS_DRIVER_ADDED(dc->devices[i], driver); 604 605 /* 606 * Walk through the children classes. Since we only keep a 607 * single parent pointer around, we walk the entire list of 608 * devclasses looking for children. We set the 609 * DC_HAS_CHILDREN flag when a child devclass is created on 610 * the parent, so we only walk the list for those devclasses 611 * that have children. 612 */ 613 if (!(dc->flags & DC_HAS_CHILDREN)) 614 return; 615 parent = dc; 616 TAILQ_FOREACH(dc, &devclasses, link) { 617 if (dc->parent == parent) 618 devclass_driver_added(dc, driver); 619 } 620 } 621 622 static void 623 device_handle_nomatch(device_t dev) 624 { 625 BUS_PROBE_NOMATCH(dev->parent, dev); 626 EVENTHANDLER_DIRECT_INVOKE(device_nomatch, dev); 627 dev->flags |= DF_DONENOMATCH; 628 } 629 630 /** 631 * @brief Add a device driver to a device class 632 * 633 * Add a device driver to a devclass. This is normally called 634 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 635 * all devices in the devclass will be called to allow them to attempt 636 * to re-probe any unmatched children. 637 * 638 * @param dc the devclass to edit 639 * @param driver the driver to register 640 */ 641 int 642 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp) 643 { 644 driverlink_t dl; 645 devclass_t child_dc; 646 const char *parentname; 647 648 PDEBUG(("%s", DRIVERNAME(driver))); 649 650 /* Don't allow invalid pass values. */ 651 if (pass <= BUS_PASS_ROOT) 652 return (EINVAL); 653 654 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 655 if (!dl) 656 return (ENOMEM); 657 658 /* 659 * Compile the driver's methods. Also increase the reference count 660 * so that the class doesn't get freed when the last instance 661 * goes. This means we can safely use static methods and avoids a 662 * double-free in devclass_delete_driver. 663 */ 664 kobj_class_compile((kobj_class_t) driver); 665 666 /* 667 * If the driver has any base classes, make the 668 * devclass inherit from the devclass of the driver's 669 * first base class. This will allow the system to 670 * search for drivers in both devclasses for children 671 * of a device using this driver. 672 */ 673 if (driver->baseclasses) 674 parentname = driver->baseclasses[0]->name; 675 else 676 parentname = NULL; 677 child_dc = devclass_find_internal(driver->name, parentname, TRUE); 678 if (dcp != NULL) 679 *dcp = child_dc; 680 681 dl->driver = driver; 682 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 683 driver->refs++; /* XXX: kobj_mtx */ 684 dl->pass = pass; 685 driver_register_pass(dl); 686 687 if (device_frozen) { 688 dl->flags |= DL_DEFERRED_PROBE; 689 } else { 690 devclass_driver_added(dc, driver); 691 } 692 bus_data_generation_update(); 693 return (0); 694 } 695 696 /** 697 * @brief Register that a device driver has been deleted from a devclass 698 * 699 * Register that a device driver has been removed from a devclass. 700 * This is called by devclass_delete_driver to accomplish the 701 * recursive notification of all the children classes of busclass, as 702 * well as busclass. Each layer will attempt to detach the driver 703 * from any devices that are children of the bus's devclass. The function 704 * will return an error if a device fails to detach. 705 * 706 * We do a full search here of the devclass list at each iteration 707 * level to save storing children-lists in the devclass structure. If 708 * we ever move beyond a few dozen devices doing this, we may need to 709 * reevaluate... 710 * 711 * @param busclass the devclass of the parent bus 712 * @param dc the devclass of the driver being deleted 713 * @param driver the driver being deleted 714 */ 715 static int 716 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver) 717 { 718 devclass_t parent; 719 device_t dev; 720 int error, i; 721 722 /* 723 * Disassociate from any devices. We iterate through all the 724 * devices in the devclass of the driver and detach any which are 725 * using the driver and which have a parent in the devclass which 726 * we are deleting from. 727 * 728 * Note that since a driver can be in multiple devclasses, we 729 * should not detach devices which are not children of devices in 730 * the affected devclass. 731 * 732 * If we're frozen, we don't generate NOMATCH events. Mark to 733 * generate later. 734 */ 735 for (i = 0; i < dc->maxunit; i++) { 736 if (dc->devices[i]) { 737 dev = dc->devices[i]; 738 if (dev->driver == driver && dev->parent && 739 dev->parent->devclass == busclass) { 740 if ((error = device_detach(dev)) != 0) 741 return (error); 742 if (device_frozen) { 743 dev->flags &= ~DF_DONENOMATCH; 744 dev->flags |= DF_NEEDNOMATCH; 745 } else { 746 device_handle_nomatch(dev); 747 } 748 } 749 } 750 } 751 752 /* 753 * Walk through the children classes. Since we only keep a 754 * single parent pointer around, we walk the entire list of 755 * devclasses looking for children. We set the 756 * DC_HAS_CHILDREN flag when a child devclass is created on 757 * the parent, so we only walk the list for those devclasses 758 * that have children. 759 */ 760 if (!(busclass->flags & DC_HAS_CHILDREN)) 761 return (0); 762 parent = busclass; 763 TAILQ_FOREACH(busclass, &devclasses, link) { 764 if (busclass->parent == parent) { 765 error = devclass_driver_deleted(busclass, dc, driver); 766 if (error) 767 return (error); 768 } 769 } 770 return (0); 771 } 772 773 /** 774 * @brief Delete a device driver from a device class 775 * 776 * Delete a device driver from a devclass. This is normally called 777 * automatically by DRIVER_MODULE(). 778 * 779 * If the driver is currently attached to any devices, 780 * devclass_delete_driver() will first attempt to detach from each 781 * device. If one of the detach calls fails, the driver will not be 782 * deleted. 783 * 784 * @param dc the devclass to edit 785 * @param driver the driver to unregister 786 */ 787 int 788 devclass_delete_driver(devclass_t busclass, driver_t *driver) 789 { 790 devclass_t dc = devclass_find(driver->name); 791 driverlink_t dl; 792 int error; 793 794 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 795 796 if (!dc) 797 return (0); 798 799 /* 800 * Find the link structure in the bus' list of drivers. 801 */ 802 TAILQ_FOREACH(dl, &busclass->drivers, link) { 803 if (dl->driver == driver) 804 break; 805 } 806 807 if (!dl) { 808 PDEBUG(("%s not found in %s list", driver->name, 809 busclass->name)); 810 return (ENOENT); 811 } 812 813 error = devclass_driver_deleted(busclass, dc, driver); 814 if (error != 0) 815 return (error); 816 817 TAILQ_REMOVE(&busclass->drivers, dl, link); 818 free(dl, M_BUS); 819 820 /* XXX: kobj_mtx */ 821 driver->refs--; 822 if (driver->refs == 0) 823 kobj_class_free((kobj_class_t) driver); 824 825 bus_data_generation_update(); 826 return (0); 827 } 828 829 /** 830 * @brief Quiesces a set of device drivers from a device class 831 * 832 * Quiesce a device driver from a devclass. This is normally called 833 * automatically by DRIVER_MODULE(). 834 * 835 * If the driver is currently attached to any devices, 836 * devclass_quiesece_driver() will first attempt to quiesce each 837 * device. 838 * 839 * @param dc the devclass to edit 840 * @param driver the driver to unregister 841 */ 842 static int 843 devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 844 { 845 devclass_t dc = devclass_find(driver->name); 846 driverlink_t dl; 847 device_t dev; 848 int i; 849 int error; 850 851 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 852 853 if (!dc) 854 return (0); 855 856 /* 857 * Find the link structure in the bus' list of drivers. 858 */ 859 TAILQ_FOREACH(dl, &busclass->drivers, link) { 860 if (dl->driver == driver) 861 break; 862 } 863 864 if (!dl) { 865 PDEBUG(("%s not found in %s list", driver->name, 866 busclass->name)); 867 return (ENOENT); 868 } 869 870 /* 871 * Quiesce all devices. We iterate through all the devices in 872 * the devclass of the driver and quiesce any which are using 873 * the driver and which have a parent in the devclass which we 874 * are quiescing. 875 * 876 * Note that since a driver can be in multiple devclasses, we 877 * should not quiesce devices which are not children of 878 * devices in the affected devclass. 879 */ 880 for (i = 0; i < dc->maxunit; i++) { 881 if (dc->devices[i]) { 882 dev = dc->devices[i]; 883 if (dev->driver == driver && dev->parent && 884 dev->parent->devclass == busclass) { 885 if ((error = device_quiesce(dev)) != 0) 886 return (error); 887 } 888 } 889 } 890 891 return (0); 892 } 893 894 /** 895 * @internal 896 */ 897 static driverlink_t 898 devclass_find_driver_internal(devclass_t dc, const char *classname) 899 { 900 driverlink_t dl; 901 902 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 903 904 TAILQ_FOREACH(dl, &dc->drivers, link) { 905 if (!strcmp(dl->driver->name, classname)) 906 return (dl); 907 } 908 909 PDEBUG(("not found")); 910 return (NULL); 911 } 912 913 /** 914 * @brief Return the name of the devclass 915 */ 916 const char * 917 devclass_get_name(devclass_t dc) 918 { 919 return (dc->name); 920 } 921 922 /** 923 * @brief Find a device given a unit number 924 * 925 * @param dc the devclass to search 926 * @param unit the unit number to search for 927 * 928 * @returns the device with the given unit number or @c 929 * NULL if there is no such device 930 */ 931 device_t 932 devclass_get_device(devclass_t dc, int unit) 933 { 934 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 935 return (NULL); 936 return (dc->devices[unit]); 937 } 938 939 /** 940 * @brief Find the softc field of a device given a unit number 941 * 942 * @param dc the devclass to search 943 * @param unit the unit number to search for 944 * 945 * @returns the softc field of the device with the given 946 * unit number or @c NULL if there is no such 947 * device 948 */ 949 void * 950 devclass_get_softc(devclass_t dc, int unit) 951 { 952 device_t dev; 953 954 dev = devclass_get_device(dc, unit); 955 if (!dev) 956 return (NULL); 957 958 return (device_get_softc(dev)); 959 } 960 961 /** 962 * @brief Get a list of devices in the devclass 963 * 964 * An array containing a list of all the devices in the given devclass 965 * is allocated and returned in @p *devlistp. The number of devices 966 * in the array is returned in @p *devcountp. The caller should free 967 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0. 968 * 969 * @param dc the devclass to examine 970 * @param devlistp points at location for array pointer return 971 * value 972 * @param devcountp points at location for array size return value 973 * 974 * @retval 0 success 975 * @retval ENOMEM the array allocation failed 976 */ 977 int 978 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 979 { 980 int count, i; 981 device_t *list; 982 983 count = devclass_get_count(dc); 984 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 985 if (!list) 986 return (ENOMEM); 987 988 count = 0; 989 for (i = 0; i < dc->maxunit; i++) { 990 if (dc->devices[i]) { 991 list[count] = dc->devices[i]; 992 count++; 993 } 994 } 995 996 *devlistp = list; 997 *devcountp = count; 998 999 return (0); 1000 } 1001 1002 /** 1003 * @brief Get a list of drivers in the devclass 1004 * 1005 * An array containing a list of pointers to all the drivers in the 1006 * given devclass is allocated and returned in @p *listp. The number 1007 * of drivers in the array is returned in @p *countp. The caller should 1008 * free the array using @c free(p, M_TEMP). 1009 * 1010 * @param dc the devclass to examine 1011 * @param listp gives location for array pointer return value 1012 * @param countp gives location for number of array elements 1013 * return value 1014 * 1015 * @retval 0 success 1016 * @retval ENOMEM the array allocation failed 1017 */ 1018 int 1019 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 1020 { 1021 driverlink_t dl; 1022 driver_t **list; 1023 int count; 1024 1025 count = 0; 1026 TAILQ_FOREACH(dl, &dc->drivers, link) 1027 count++; 1028 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 1029 if (list == NULL) 1030 return (ENOMEM); 1031 1032 count = 0; 1033 TAILQ_FOREACH(dl, &dc->drivers, link) { 1034 list[count] = dl->driver; 1035 count++; 1036 } 1037 *listp = list; 1038 *countp = count; 1039 1040 return (0); 1041 } 1042 1043 /** 1044 * @brief Get the number of devices in a devclass 1045 * 1046 * @param dc the devclass to examine 1047 */ 1048 int 1049 devclass_get_count(devclass_t dc) 1050 { 1051 int count, i; 1052 1053 count = 0; 1054 for (i = 0; i < dc->maxunit; i++) 1055 if (dc->devices[i]) 1056 count++; 1057 return (count); 1058 } 1059 1060 /** 1061 * @brief Get the maximum unit number used in a devclass 1062 * 1063 * Note that this is one greater than the highest currently-allocated unit. If 1064 * @p dc is NULL, @c -1 is returned to indicate that not even the devclass has 1065 * been allocated yet. 1066 * 1067 * @param dc the devclass to examine 1068 */ 1069 int 1070 devclass_get_maxunit(devclass_t dc) 1071 { 1072 if (dc == NULL) 1073 return (-1); 1074 return (dc->maxunit); 1075 } 1076 1077 /** 1078 * @brief Find a free unit number in a devclass 1079 * 1080 * This function searches for the first unused unit number greater 1081 * that or equal to @p unit. 1082 * 1083 * @param dc the devclass to examine 1084 * @param unit the first unit number to check 1085 */ 1086 int 1087 devclass_find_free_unit(devclass_t dc, int unit) 1088 { 1089 if (dc == NULL) 1090 return (unit); 1091 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1092 unit++; 1093 return (unit); 1094 } 1095 1096 /** 1097 * @brief Set the parent of a devclass 1098 * 1099 * The parent class is normally initialised automatically by 1100 * DRIVER_MODULE(). 1101 * 1102 * @param dc the devclass to edit 1103 * @param pdc the new parent devclass 1104 */ 1105 void 1106 devclass_set_parent(devclass_t dc, devclass_t pdc) 1107 { 1108 dc->parent = pdc; 1109 } 1110 1111 /** 1112 * @brief Get the parent of a devclass 1113 * 1114 * @param dc the devclass to examine 1115 */ 1116 devclass_t 1117 devclass_get_parent(devclass_t dc) 1118 { 1119 return (dc->parent); 1120 } 1121 1122 struct sysctl_ctx_list * 1123 devclass_get_sysctl_ctx(devclass_t dc) 1124 { 1125 return (&dc->sysctl_ctx); 1126 } 1127 1128 struct sysctl_oid * 1129 devclass_get_sysctl_tree(devclass_t dc) 1130 { 1131 return (dc->sysctl_tree); 1132 } 1133 1134 /** 1135 * @internal 1136 * @brief Allocate a unit number 1137 * 1138 * On entry, @p *unitp is the desired unit number (or @c DEVICE_UNIT_ANY if any 1139 * will do). The allocated unit number is returned in @p *unitp. 1140 * 1141 * @param dc the devclass to allocate from 1142 * @param unitp points at the location for the allocated unit 1143 * number 1144 * 1145 * @retval 0 success 1146 * @retval EEXIST the requested unit number is already allocated 1147 * @retval ENOMEM memory allocation failure 1148 */ 1149 static int 1150 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) 1151 { 1152 const char *s; 1153 int unit = *unitp; 1154 1155 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1156 1157 /* Ask the parent bus if it wants to wire this device. */ 1158 if (unit == DEVICE_UNIT_ANY) 1159 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, 1160 &unit); 1161 1162 /* If we were given a wired unit number, check for existing device */ 1163 if (unit != DEVICE_UNIT_ANY) { 1164 if (unit >= 0 && unit < dc->maxunit && 1165 dc->devices[unit] != NULL) { 1166 if (bootverbose) 1167 printf("%s: %s%d already exists; skipping it\n", 1168 dc->name, dc->name, *unitp); 1169 return (EEXIST); 1170 } 1171 } else { 1172 /* Unwired device, find the next available slot for it */ 1173 unit = 0; 1174 for (unit = 0;; unit++) { 1175 /* If this device slot is already in use, skip it. */ 1176 if (unit < dc->maxunit && dc->devices[unit] != NULL) 1177 continue; 1178 1179 /* If there is an "at" hint for a unit then skip it. */ 1180 if (resource_string_value(dc->name, unit, "at", &s) == 1181 0) 1182 continue; 1183 1184 break; 1185 } 1186 } 1187 1188 /* 1189 * We've selected a unit beyond the length of the table, so let's 1190 * extend the table to make room for all units up to and including 1191 * this one. 1192 */ 1193 if (unit >= dc->maxunit) { 1194 device_t *newlist, *oldlist; 1195 int newsize; 1196 1197 oldlist = dc->devices; 1198 newsize = roundup((unit + 1), 1199 MAX(1, MINALLOCSIZE / sizeof(device_t))); 1200 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1201 if (!newlist) 1202 return (ENOMEM); 1203 if (oldlist != NULL) 1204 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit); 1205 bzero(newlist + dc->maxunit, 1206 sizeof(device_t) * (newsize - dc->maxunit)); 1207 dc->devices = newlist; 1208 dc->maxunit = newsize; 1209 if (oldlist != NULL) 1210 free(oldlist, M_BUS); 1211 } 1212 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1213 1214 *unitp = unit; 1215 return (0); 1216 } 1217 1218 /** 1219 * @internal 1220 * @brief Add a device to a devclass 1221 * 1222 * A unit number is allocated for the device (using the device's 1223 * preferred unit number if any) and the device is registered in the 1224 * devclass. This allows the device to be looked up by its unit 1225 * number, e.g. by decoding a dev_t minor number. 1226 * 1227 * @param dc the devclass to add to 1228 * @param dev the device to add 1229 * 1230 * @retval 0 success 1231 * @retval EEXIST the requested unit number is already allocated 1232 * @retval ENOMEM memory allocation failure 1233 */ 1234 static int 1235 devclass_add_device(devclass_t dc, device_t dev) 1236 { 1237 int buflen, error; 1238 1239 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1240 1241 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); 1242 if (buflen < 0) 1243 return (ENOMEM); 1244 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1245 if (!dev->nameunit) 1246 return (ENOMEM); 1247 1248 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { 1249 free(dev->nameunit, M_BUS); 1250 dev->nameunit = NULL; 1251 return (error); 1252 } 1253 dc->devices[dev->unit] = dev; 1254 dev->devclass = dc; 1255 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1256 1257 return (0); 1258 } 1259 1260 /** 1261 * @internal 1262 * @brief Delete a device from a devclass 1263 * 1264 * The device is removed from the devclass's device list and its unit 1265 * number is freed. 1266 1267 * @param dc the devclass to delete from 1268 * @param dev the device to delete 1269 * 1270 * @retval 0 success 1271 */ 1272 static int 1273 devclass_delete_device(devclass_t dc, device_t dev) 1274 { 1275 if (!dc || !dev) 1276 return (0); 1277 1278 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1279 1280 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1281 panic("devclass_delete_device: inconsistent device class"); 1282 dc->devices[dev->unit] = NULL; 1283 if (dev->flags & DF_WILDCARD) 1284 dev->unit = DEVICE_UNIT_ANY; 1285 dev->devclass = NULL; 1286 free(dev->nameunit, M_BUS); 1287 dev->nameunit = NULL; 1288 1289 return (0); 1290 } 1291 1292 /** 1293 * @internal 1294 * @brief Make a new device and add it as a child of @p parent 1295 * 1296 * @param parent the parent of the new device 1297 * @param name the devclass name of the new device or @c NULL 1298 * to leave the devclass unspecified 1299 * @parem unit the unit number of the new device of @c DEVICE_UNIT_ANY 1300 * to leave the unit number unspecified 1301 * 1302 * @returns the new device 1303 */ 1304 static device_t 1305 make_device(device_t parent, const char *name, int unit) 1306 { 1307 device_t dev; 1308 devclass_t dc; 1309 1310 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1311 1312 if (name) { 1313 dc = devclass_find_internal(name, NULL, TRUE); 1314 if (!dc) { 1315 printf("make_device: can't find device class %s\n", 1316 name); 1317 return (NULL); 1318 } 1319 } else { 1320 dc = NULL; 1321 } 1322 1323 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO); 1324 if (!dev) 1325 return (NULL); 1326 1327 dev->parent = parent; 1328 TAILQ_INIT(&dev->children); 1329 kobj_init((kobj_t) dev, &null_class); 1330 dev->driver = NULL; 1331 dev->devclass = NULL; 1332 dev->unit = unit; 1333 dev->nameunit = NULL; 1334 dev->desc = NULL; 1335 dev->busy = 0; 1336 dev->devflags = 0; 1337 dev->flags = DF_ENABLED; 1338 dev->order = 0; 1339 if (unit == DEVICE_UNIT_ANY) 1340 dev->flags |= DF_WILDCARD; 1341 if (name) { 1342 dev->flags |= DF_FIXEDCLASS; 1343 if (devclass_add_device(dc, dev)) { 1344 kobj_delete((kobj_t) dev, M_BUS); 1345 return (NULL); 1346 } 1347 } 1348 if (parent != NULL && device_has_quiet_children(parent)) 1349 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN; 1350 dev->ivars = NULL; 1351 dev->softc = NULL; 1352 1353 dev->state = DS_NOTPRESENT; 1354 1355 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1356 bus_data_generation_update(); 1357 1358 return (dev); 1359 } 1360 1361 /** 1362 * @internal 1363 * @brief Print a description of a device. 1364 */ 1365 static int 1366 device_print_child(device_t dev, device_t child) 1367 { 1368 int retval = 0; 1369 1370 if (device_is_alive(child)) 1371 retval += BUS_PRINT_CHILD(dev, child); 1372 else 1373 retval += device_printf(child, " not found\n"); 1374 1375 return (retval); 1376 } 1377 1378 /** 1379 * @brief Create a new device 1380 * 1381 * This creates a new device and adds it as a child of an existing 1382 * parent device. The new device will be added after the last existing 1383 * child with order zero. 1384 * 1385 * @param dev the device which will be the parent of the 1386 * new child device 1387 * @param name devclass name for new device or @c NULL if not 1388 * specified 1389 * @param unit unit number for new device or @c DEVICE_UNIT_ANY if not 1390 * specified 1391 * 1392 * @returns the new device 1393 */ 1394 device_t 1395 device_add_child(device_t dev, const char *name, int unit) 1396 { 1397 return (device_add_child_ordered(dev, 0, name, unit)); 1398 } 1399 1400 /** 1401 * @brief Create a new device 1402 * 1403 * This creates a new device and adds it as a child of an existing 1404 * parent device. The new device will be added after the last existing 1405 * child with the same order. 1406 * 1407 * @param dev the device which will be the parent of the 1408 * new child device 1409 * @param order a value which is used to partially sort the 1410 * children of @p dev - devices created using 1411 * lower values of @p order appear first in @p 1412 * dev's list of children 1413 * @param name devclass name for new device or @c NULL if not 1414 * specified 1415 * @param unit unit number for new device or @c DEVICE_UNIT_ANY if not 1416 * specified 1417 * 1418 * @returns the new device 1419 */ 1420 device_t 1421 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit) 1422 { 1423 device_t child; 1424 device_t place; 1425 1426 PDEBUG(("%s at %s with order %u as unit %d", 1427 name, DEVICENAME(dev), order, unit)); 1428 KASSERT(name != NULL || unit == DEVICE_UNIT_ANY, 1429 ("child device with wildcard name and specific unit number")); 1430 1431 child = make_device(dev, name, unit); 1432 if (child == NULL) 1433 return (child); 1434 child->order = order; 1435 1436 TAILQ_FOREACH(place, &dev->children, link) { 1437 if (place->order > order) 1438 break; 1439 } 1440 1441 if (place) { 1442 /* 1443 * The device 'place' is the first device whose order is 1444 * greater than the new child. 1445 */ 1446 TAILQ_INSERT_BEFORE(place, child, link); 1447 } else { 1448 /* 1449 * The new child's order is greater or equal to the order of 1450 * any existing device. Add the child to the tail of the list. 1451 */ 1452 TAILQ_INSERT_TAIL(&dev->children, child, link); 1453 } 1454 1455 bus_data_generation_update(); 1456 return (child); 1457 } 1458 1459 /** 1460 * @brief Delete a device 1461 * 1462 * This function deletes a device along with all of its children. If 1463 * the device currently has a driver attached to it, the device is 1464 * detached first using device_detach(). 1465 * 1466 * @param dev the parent device 1467 * @param child the device to delete 1468 * 1469 * @retval 0 success 1470 * @retval non-zero a unit error code describing the error 1471 */ 1472 int 1473 device_delete_child(device_t dev, device_t child) 1474 { 1475 int error; 1476 device_t grandchild; 1477 1478 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1479 1480 /* 1481 * Detach child. Ideally this cleans up any grandchild 1482 * devices. 1483 */ 1484 if ((error = device_detach(child)) != 0) 1485 return (error); 1486 1487 /* Delete any grandchildren left after detach. */ 1488 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1489 error = device_delete_child(child, grandchild); 1490 if (error) 1491 return (error); 1492 } 1493 1494 if (child->devclass) 1495 devclass_delete_device(child->devclass, child); 1496 if (child->parent) 1497 BUS_CHILD_DELETED(dev, child); 1498 TAILQ_REMOVE(&dev->children, child, link); 1499 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1500 kobj_delete((kobj_t) child, M_BUS); 1501 1502 bus_data_generation_update(); 1503 return (0); 1504 } 1505 1506 /** 1507 * @brief Delete all children devices of the given device, if any. 1508 * 1509 * This function deletes all children devices of the given device, if 1510 * any, using the device_delete_child() function for each device it 1511 * finds. If a child device cannot be deleted, this function will 1512 * return an error code. 1513 * 1514 * @param dev the parent device 1515 * 1516 * @retval 0 success 1517 * @retval non-zero a device would not detach 1518 */ 1519 int 1520 device_delete_children(device_t dev) 1521 { 1522 device_t child; 1523 int error; 1524 1525 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1526 1527 error = 0; 1528 1529 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1530 error = device_delete_child(dev, child); 1531 if (error) { 1532 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1533 break; 1534 } 1535 } 1536 return (error); 1537 } 1538 1539 /** 1540 * @brief Find a device given a unit number 1541 * 1542 * This is similar to devclass_get_devices() but only searches for 1543 * devices which have @p dev as a parent. 1544 * 1545 * @param dev the parent device to search 1546 * @param unit the unit number to search for. If the unit is 1547 * @c DEVICE_UNIT_ANY, return the first child of @p dev 1548 * which has name @p classname (that is, the one with the 1549 * lowest unit.) 1550 * 1551 * @returns the device with the given unit number or @c 1552 * NULL if there is no such device 1553 */ 1554 device_t 1555 device_find_child(device_t dev, const char *classname, int unit) 1556 { 1557 devclass_t dc; 1558 device_t child; 1559 1560 dc = devclass_find(classname); 1561 if (!dc) 1562 return (NULL); 1563 1564 if (unit != DEVICE_UNIT_ANY) { 1565 child = devclass_get_device(dc, unit); 1566 if (child && child->parent == dev) 1567 return (child); 1568 } else { 1569 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1570 child = devclass_get_device(dc, unit); 1571 if (child && child->parent == dev) 1572 return (child); 1573 } 1574 } 1575 return (NULL); 1576 } 1577 1578 /** 1579 * @internal 1580 */ 1581 static driverlink_t 1582 first_matching_driver(devclass_t dc, device_t dev) 1583 { 1584 if (dev->devclass) 1585 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1586 return (TAILQ_FIRST(&dc->drivers)); 1587 } 1588 1589 /** 1590 * @internal 1591 */ 1592 static driverlink_t 1593 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1594 { 1595 if (dev->devclass) { 1596 driverlink_t dl; 1597 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1598 if (!strcmp(dev->devclass->name, dl->driver->name)) 1599 return (dl); 1600 return (NULL); 1601 } 1602 return (TAILQ_NEXT(last, link)); 1603 } 1604 1605 /** 1606 * @internal 1607 */ 1608 int 1609 device_probe_child(device_t dev, device_t child) 1610 { 1611 devclass_t dc; 1612 driverlink_t best = NULL; 1613 driverlink_t dl; 1614 int result, pri = 0; 1615 /* We should preserve the devclass (or lack of) set by the bus. */ 1616 int hasclass = (child->devclass != NULL); 1617 1618 bus_topo_assert(); 1619 1620 dc = dev->devclass; 1621 if (!dc) 1622 panic("device_probe_child: parent device has no devclass"); 1623 1624 /* 1625 * If the state is already probed, then return. 1626 */ 1627 if (child->state == DS_ALIVE) 1628 return (0); 1629 1630 for (; dc; dc = dc->parent) { 1631 for (dl = first_matching_driver(dc, child); 1632 dl; 1633 dl = next_matching_driver(dc, child, dl)) { 1634 /* If this driver's pass is too high, then ignore it. */ 1635 if (dl->pass > bus_current_pass) 1636 continue; 1637 1638 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1639 result = device_set_driver(child, dl->driver); 1640 if (result == ENOMEM) 1641 return (result); 1642 else if (result != 0) 1643 continue; 1644 if (!hasclass) { 1645 if (device_set_devclass(child, 1646 dl->driver->name) != 0) { 1647 char const * devname = 1648 device_get_name(child); 1649 if (devname == NULL) 1650 devname = "(unknown)"; 1651 printf("driver bug: Unable to set " 1652 "devclass (class: %s " 1653 "devname: %s)\n", 1654 dl->driver->name, 1655 devname); 1656 (void)device_set_driver(child, NULL); 1657 continue; 1658 } 1659 } 1660 1661 /* Fetch any flags for the device before probing. */ 1662 resource_int_value(dl->driver->name, child->unit, 1663 "flags", &child->devflags); 1664 1665 result = DEVICE_PROBE(child); 1666 1667 /* 1668 * If probe returns 0, this is the driver that wins this 1669 * device. 1670 */ 1671 if (result == 0) { 1672 best = dl; 1673 pri = 0; 1674 goto exact_match; /* C doesn't have break 2 */ 1675 } 1676 1677 /* Reset flags and devclass before the next probe. */ 1678 child->devflags = 0; 1679 if (!hasclass) 1680 (void)device_set_devclass(child, NULL); 1681 1682 /* 1683 * Reset DF_QUIET in case this driver doesn't 1684 * end up as the best driver. 1685 */ 1686 device_verbose(child); 1687 1688 /* 1689 * Probes that return BUS_PROBE_NOWILDCARD or lower 1690 * only match on devices whose driver was explicitly 1691 * specified. 1692 */ 1693 if (result <= BUS_PROBE_NOWILDCARD && 1694 !(child->flags & DF_FIXEDCLASS)) { 1695 result = ENXIO; 1696 } 1697 1698 /* 1699 * The driver returned an error so it 1700 * certainly doesn't match. 1701 */ 1702 if (result > 0) { 1703 (void)device_set_driver(child, NULL); 1704 continue; 1705 } 1706 1707 /* 1708 * A priority lower than SUCCESS, remember the 1709 * best matching driver. Initialise the value 1710 * of pri for the first match. 1711 */ 1712 if (best == NULL || result > pri) { 1713 best = dl; 1714 pri = result; 1715 continue; 1716 } 1717 } 1718 } 1719 1720 if (best == NULL) 1721 return (ENXIO); 1722 1723 /* 1724 * If we found a driver, change state and initialise the devclass. 1725 * Set the winning driver, devclass, and flags. 1726 */ 1727 result = device_set_driver(child, best->driver); 1728 if (result != 0) 1729 return (result); 1730 if (!child->devclass) { 1731 result = device_set_devclass(child, best->driver->name); 1732 if (result != 0) { 1733 (void)device_set_driver(child, NULL); 1734 return (result); 1735 } 1736 } 1737 resource_int_value(best->driver->name, child->unit, 1738 "flags", &child->devflags); 1739 1740 /* 1741 * A bit bogus. Call the probe method again to make sure that we have 1742 * the right description for the device. 1743 */ 1744 result = DEVICE_PROBE(child); 1745 if (result > 0) { 1746 if (!hasclass) 1747 (void)device_set_devclass(child, NULL); 1748 (void)device_set_driver(child, NULL); 1749 return (result); 1750 } 1751 1752 exact_match: 1753 child->state = DS_ALIVE; 1754 bus_data_generation_update(); 1755 return (0); 1756 } 1757 1758 /** 1759 * @brief Return the parent of a device 1760 */ 1761 device_t 1762 device_get_parent(device_t dev) 1763 { 1764 return (dev->parent); 1765 } 1766 1767 /** 1768 * @brief Get a list of children of a device 1769 * 1770 * An array containing a list of all the children of the given device 1771 * is allocated and returned in @p *devlistp. The number of devices 1772 * in the array is returned in @p *devcountp. The caller should free 1773 * the array using @c free(p, M_TEMP). 1774 * 1775 * @param dev the device to examine 1776 * @param devlistp points at location for array pointer return 1777 * value 1778 * @param devcountp points at location for array size return value 1779 * 1780 * @retval 0 success 1781 * @retval ENOMEM the array allocation failed 1782 */ 1783 int 1784 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1785 { 1786 int count; 1787 device_t child; 1788 device_t *list; 1789 1790 count = 0; 1791 TAILQ_FOREACH(child, &dev->children, link) { 1792 count++; 1793 } 1794 if (count == 0) { 1795 *devlistp = NULL; 1796 *devcountp = 0; 1797 return (0); 1798 } 1799 1800 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1801 if (!list) 1802 return (ENOMEM); 1803 1804 count = 0; 1805 TAILQ_FOREACH(child, &dev->children, link) { 1806 list[count] = child; 1807 count++; 1808 } 1809 1810 *devlistp = list; 1811 *devcountp = count; 1812 1813 return (0); 1814 } 1815 1816 /** 1817 * @brief Return the current driver for the device or @c NULL if there 1818 * is no driver currently attached 1819 */ 1820 driver_t * 1821 device_get_driver(device_t dev) 1822 { 1823 return (dev->driver); 1824 } 1825 1826 /** 1827 * @brief Return the current devclass for the device or @c NULL if 1828 * there is none. 1829 */ 1830 devclass_t 1831 device_get_devclass(device_t dev) 1832 { 1833 return (dev->devclass); 1834 } 1835 1836 /** 1837 * @brief Return the name of the device's devclass or @c NULL if there 1838 * is none. 1839 */ 1840 const char * 1841 device_get_name(device_t dev) 1842 { 1843 if (dev != NULL && dev->devclass) 1844 return (devclass_get_name(dev->devclass)); 1845 return (NULL); 1846 } 1847 1848 /** 1849 * @brief Return a string containing the device's devclass name 1850 * followed by an ascii representation of the device's unit number 1851 * (e.g. @c "foo2"). 1852 */ 1853 const char * 1854 device_get_nameunit(device_t dev) 1855 { 1856 return (dev->nameunit); 1857 } 1858 1859 /** 1860 * @brief Return the device's unit number. 1861 */ 1862 int 1863 device_get_unit(device_t dev) 1864 { 1865 return (dev->unit); 1866 } 1867 1868 /** 1869 * @brief Return the device's description string 1870 */ 1871 const char * 1872 device_get_desc(device_t dev) 1873 { 1874 return (dev->desc); 1875 } 1876 1877 /** 1878 * @brief Return the device's flags 1879 */ 1880 uint32_t 1881 device_get_flags(device_t dev) 1882 { 1883 return (dev->devflags); 1884 } 1885 1886 struct sysctl_ctx_list * 1887 device_get_sysctl_ctx(device_t dev) 1888 { 1889 return (&dev->sysctl_ctx); 1890 } 1891 1892 struct sysctl_oid * 1893 device_get_sysctl_tree(device_t dev) 1894 { 1895 return (dev->sysctl_tree); 1896 } 1897 1898 /** 1899 * @brief Print the name of the device followed by a colon and a space 1900 * 1901 * @returns the number of characters printed 1902 */ 1903 int 1904 device_print_prettyname(device_t dev) 1905 { 1906 const char *name = device_get_name(dev); 1907 1908 if (name == NULL) 1909 return (printf("unknown: ")); 1910 return (printf("%s%d: ", name, device_get_unit(dev))); 1911 } 1912 1913 /** 1914 * @brief Print the name of the device followed by a colon, a space 1915 * and the result of calling vprintf() with the value of @p fmt and 1916 * the following arguments. 1917 * 1918 * @returns the number of characters printed 1919 */ 1920 int 1921 device_printf(device_t dev, const char * fmt, ...) 1922 { 1923 char buf[128]; 1924 struct sbuf sb; 1925 const char *name; 1926 va_list ap; 1927 size_t retval; 1928 1929 retval = 0; 1930 1931 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 1932 sbuf_set_drain(&sb, sbuf_printf_drain, &retval); 1933 1934 name = device_get_name(dev); 1935 1936 if (name == NULL) 1937 sbuf_cat(&sb, "unknown: "); 1938 else 1939 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev)); 1940 1941 va_start(ap, fmt); 1942 sbuf_vprintf(&sb, fmt, ap); 1943 va_end(ap); 1944 1945 sbuf_finish(&sb); 1946 sbuf_delete(&sb); 1947 1948 return (retval); 1949 } 1950 1951 /** 1952 * @brief Print the name of the device followed by a colon, a space 1953 * and the result of calling log() with the value of @p fmt and 1954 * the following arguments. 1955 * 1956 * @returns the number of characters printed 1957 */ 1958 int 1959 device_log(device_t dev, int pri, const char * fmt, ...) 1960 { 1961 char buf[128]; 1962 struct sbuf sb; 1963 const char *name; 1964 va_list ap; 1965 size_t retval; 1966 1967 retval = 0; 1968 1969 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 1970 1971 name = device_get_name(dev); 1972 1973 if (name == NULL) 1974 sbuf_cat(&sb, "unknown: "); 1975 else 1976 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev)); 1977 1978 va_start(ap, fmt); 1979 sbuf_vprintf(&sb, fmt, ap); 1980 va_end(ap); 1981 1982 sbuf_finish(&sb); 1983 1984 log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb)); 1985 retval = sbuf_len(&sb); 1986 1987 sbuf_delete(&sb); 1988 1989 return (retval); 1990 } 1991 1992 /** 1993 * @internal 1994 */ 1995 static void 1996 device_set_desc_internal(device_t dev, const char *desc, bool allocated) 1997 { 1998 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1999 free(dev->desc, M_BUS); 2000 dev->flags &= ~DF_DESCMALLOCED; 2001 dev->desc = NULL; 2002 } 2003 2004 if (allocated && desc) 2005 dev->flags |= DF_DESCMALLOCED; 2006 dev->desc = __DECONST(char *, desc); 2007 2008 bus_data_generation_update(); 2009 } 2010 2011 /** 2012 * @brief Set the device's description 2013 * 2014 * The value of @c desc should be a string constant that will not 2015 * change (at least until the description is changed in a subsequent 2016 * call to device_set_desc() or device_set_desc_copy()). 2017 */ 2018 void 2019 device_set_desc(device_t dev, const char *desc) 2020 { 2021 device_set_desc_internal(dev, desc, false); 2022 } 2023 2024 /** 2025 * @brief Set the device's description 2026 * 2027 * A printf-like version of device_set_desc(). 2028 */ 2029 void 2030 device_set_descf(device_t dev, const char *fmt, ...) 2031 { 2032 va_list ap; 2033 char *buf = NULL; 2034 2035 va_start(ap, fmt); 2036 vasprintf(&buf, M_BUS, fmt, ap); 2037 va_end(ap); 2038 device_set_desc_internal(dev, buf, true); 2039 } 2040 2041 /** 2042 * @brief Set the device's description 2043 * 2044 * The string pointed to by @c desc is copied. Use this function if 2045 * the device description is generated, (e.g. with sprintf()). 2046 */ 2047 void 2048 device_set_desc_copy(device_t dev, const char *desc) 2049 { 2050 char *buf; 2051 2052 buf = strdup_flags(desc, M_BUS, M_NOWAIT); 2053 device_set_desc_internal(dev, buf, true); 2054 } 2055 2056 /** 2057 * @brief Set the device's flags 2058 */ 2059 void 2060 device_set_flags(device_t dev, uint32_t flags) 2061 { 2062 dev->devflags = flags; 2063 } 2064 2065 /** 2066 * @brief Return the device's softc field 2067 * 2068 * The softc is allocated and zeroed when a driver is attached, based 2069 * on the size field of the driver. 2070 */ 2071 void * 2072 device_get_softc(device_t dev) 2073 { 2074 return (dev->softc); 2075 } 2076 2077 /** 2078 * @brief Set the device's softc field 2079 * 2080 * Most drivers do not need to use this since the softc is allocated 2081 * automatically when the driver is attached. 2082 */ 2083 void 2084 device_set_softc(device_t dev, void *softc) 2085 { 2086 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2087 free(dev->softc, M_BUS_SC); 2088 dev->softc = softc; 2089 if (dev->softc) 2090 dev->flags |= DF_EXTERNALSOFTC; 2091 else 2092 dev->flags &= ~DF_EXTERNALSOFTC; 2093 } 2094 2095 /** 2096 * @brief Free claimed softc 2097 * 2098 * Most drivers do not need to use this since the softc is freed 2099 * automatically when the driver is detached. 2100 */ 2101 void 2102 device_free_softc(void *softc) 2103 { 2104 free(softc, M_BUS_SC); 2105 } 2106 2107 /** 2108 * @brief Claim softc 2109 * 2110 * This function can be used to let the driver free the automatically 2111 * allocated softc using "device_free_softc()". This function is 2112 * useful when the driver is refcounting the softc and the softc 2113 * cannot be freed when the "device_detach" method is called. 2114 */ 2115 void 2116 device_claim_softc(device_t dev) 2117 { 2118 if (dev->softc) 2119 dev->flags |= DF_EXTERNALSOFTC; 2120 else 2121 dev->flags &= ~DF_EXTERNALSOFTC; 2122 } 2123 2124 /** 2125 * @brief Get the device's ivars field 2126 * 2127 * The ivars field is used by the parent device to store per-device 2128 * state (e.g. the physical location of the device or a list of 2129 * resources). 2130 */ 2131 void * 2132 device_get_ivars(device_t dev) 2133 { 2134 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2135 return (dev->ivars); 2136 } 2137 2138 /** 2139 * @brief Set the device's ivars field 2140 */ 2141 void 2142 device_set_ivars(device_t dev, void * ivars) 2143 { 2144 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2145 dev->ivars = ivars; 2146 } 2147 2148 /** 2149 * @brief Return the device's state 2150 */ 2151 device_state_t 2152 device_get_state(device_t dev) 2153 { 2154 return (dev->state); 2155 } 2156 2157 /** 2158 * @brief Set the DF_ENABLED flag for the device 2159 */ 2160 void 2161 device_enable(device_t dev) 2162 { 2163 dev->flags |= DF_ENABLED; 2164 } 2165 2166 /** 2167 * @brief Clear the DF_ENABLED flag for the device 2168 */ 2169 void 2170 device_disable(device_t dev) 2171 { 2172 dev->flags &= ~DF_ENABLED; 2173 } 2174 2175 /** 2176 * @brief Increment the busy counter for the device 2177 */ 2178 void 2179 device_busy(device_t dev) 2180 { 2181 2182 /* 2183 * Mark the device as busy, recursively up the tree if this busy count 2184 * goes 0->1. 2185 */ 2186 if (refcount_acquire(&dev->busy) == 0 && dev->parent != NULL) 2187 device_busy(dev->parent); 2188 } 2189 2190 /** 2191 * @brief Decrement the busy counter for the device 2192 */ 2193 void 2194 device_unbusy(device_t dev) 2195 { 2196 2197 /* 2198 * Mark the device as unbsy, recursively if this is the last busy count. 2199 */ 2200 if (refcount_release(&dev->busy) && dev->parent != NULL) 2201 device_unbusy(dev->parent); 2202 } 2203 2204 /** 2205 * @brief Set the DF_QUIET flag for the device 2206 */ 2207 void 2208 device_quiet(device_t dev) 2209 { 2210 dev->flags |= DF_QUIET; 2211 } 2212 2213 /** 2214 * @brief Set the DF_QUIET_CHILDREN flag for the device 2215 */ 2216 void 2217 device_quiet_children(device_t dev) 2218 { 2219 dev->flags |= DF_QUIET_CHILDREN; 2220 } 2221 2222 /** 2223 * @brief Clear the DF_QUIET flag for the device 2224 */ 2225 void 2226 device_verbose(device_t dev) 2227 { 2228 dev->flags &= ~DF_QUIET; 2229 } 2230 2231 ssize_t 2232 device_get_property(device_t dev, const char *prop, void *val, size_t sz, 2233 device_property_type_t type) 2234 { 2235 device_t bus = device_get_parent(dev); 2236 2237 switch (type) { 2238 case DEVICE_PROP_ANY: 2239 case DEVICE_PROP_BUFFER: 2240 case DEVICE_PROP_HANDLE: /* Size checks done in implementation. */ 2241 break; 2242 case DEVICE_PROP_UINT32: 2243 if (sz % 4 != 0) 2244 return (-1); 2245 break; 2246 case DEVICE_PROP_UINT64: 2247 if (sz % 8 != 0) 2248 return (-1); 2249 break; 2250 default: 2251 return (-1); 2252 } 2253 2254 return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type)); 2255 } 2256 2257 bool 2258 device_has_property(device_t dev, const char *prop) 2259 { 2260 return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0); 2261 } 2262 2263 /** 2264 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device 2265 */ 2266 int 2267 device_has_quiet_children(device_t dev) 2268 { 2269 return ((dev->flags & DF_QUIET_CHILDREN) != 0); 2270 } 2271 2272 /** 2273 * @brief Return non-zero if the DF_QUIET flag is set on the device 2274 */ 2275 int 2276 device_is_quiet(device_t dev) 2277 { 2278 return ((dev->flags & DF_QUIET) != 0); 2279 } 2280 2281 /** 2282 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2283 */ 2284 int 2285 device_is_enabled(device_t dev) 2286 { 2287 return ((dev->flags & DF_ENABLED) != 0); 2288 } 2289 2290 /** 2291 * @brief Return non-zero if the device was successfully probed 2292 */ 2293 int 2294 device_is_alive(device_t dev) 2295 { 2296 return (dev->state >= DS_ALIVE); 2297 } 2298 2299 /** 2300 * @brief Return non-zero if the device currently has a driver 2301 * attached to it 2302 */ 2303 int 2304 device_is_attached(device_t dev) 2305 { 2306 return (dev->state >= DS_ATTACHED); 2307 } 2308 2309 /** 2310 * @brief Return non-zero if the device is currently suspended. 2311 */ 2312 int 2313 device_is_suspended(device_t dev) 2314 { 2315 return ((dev->flags & DF_SUSPENDED) != 0); 2316 } 2317 2318 /** 2319 * @brief Set the devclass of a device 2320 * @see devclass_add_device(). 2321 */ 2322 int 2323 device_set_devclass(device_t dev, const char *classname) 2324 { 2325 devclass_t dc; 2326 int error; 2327 2328 if (!classname) { 2329 if (dev->devclass) 2330 devclass_delete_device(dev->devclass, dev); 2331 return (0); 2332 } 2333 2334 if (dev->devclass) { 2335 printf("device_set_devclass: device class already set\n"); 2336 return (EINVAL); 2337 } 2338 2339 dc = devclass_find_internal(classname, NULL, TRUE); 2340 if (!dc) 2341 return (ENOMEM); 2342 2343 error = devclass_add_device(dc, dev); 2344 2345 bus_data_generation_update(); 2346 return (error); 2347 } 2348 2349 /** 2350 * @brief Set the devclass of a device and mark the devclass fixed. 2351 * @see device_set_devclass() 2352 */ 2353 int 2354 device_set_devclass_fixed(device_t dev, const char *classname) 2355 { 2356 int error; 2357 2358 if (classname == NULL) 2359 return (EINVAL); 2360 2361 error = device_set_devclass(dev, classname); 2362 if (error) 2363 return (error); 2364 dev->flags |= DF_FIXEDCLASS; 2365 return (0); 2366 } 2367 2368 /** 2369 * @brief Query the device to determine if it's of a fixed devclass 2370 * @see device_set_devclass_fixed() 2371 */ 2372 bool 2373 device_is_devclass_fixed(device_t dev) 2374 { 2375 return ((dev->flags & DF_FIXEDCLASS) != 0); 2376 } 2377 2378 /** 2379 * @brief Set the driver of a device 2380 * 2381 * @retval 0 success 2382 * @retval EBUSY the device already has a driver attached 2383 * @retval ENOMEM a memory allocation failure occurred 2384 */ 2385 int 2386 device_set_driver(device_t dev, driver_t *driver) 2387 { 2388 int domain; 2389 struct domainset *policy; 2390 2391 if (dev->state >= DS_ATTACHED) 2392 return (EBUSY); 2393 2394 if (dev->driver == driver) 2395 return (0); 2396 2397 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2398 free(dev->softc, M_BUS_SC); 2399 dev->softc = NULL; 2400 } 2401 device_set_desc(dev, NULL); 2402 kobj_delete((kobj_t) dev, NULL); 2403 dev->driver = driver; 2404 if (driver) { 2405 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2406 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2407 if (bus_get_domain(dev, &domain) == 0) 2408 policy = DOMAINSET_PREF(domain); 2409 else 2410 policy = DOMAINSET_RR(); 2411 dev->softc = malloc_domainset(driver->size, M_BUS_SC, 2412 policy, M_NOWAIT | M_ZERO); 2413 if (!dev->softc) { 2414 kobj_delete((kobj_t) dev, NULL); 2415 kobj_init((kobj_t) dev, &null_class); 2416 dev->driver = NULL; 2417 return (ENOMEM); 2418 } 2419 } 2420 } else { 2421 kobj_init((kobj_t) dev, &null_class); 2422 } 2423 2424 bus_data_generation_update(); 2425 return (0); 2426 } 2427 2428 /** 2429 * @brief Probe a device, and return this status. 2430 * 2431 * This function is the core of the device autoconfiguration 2432 * system. Its purpose is to select a suitable driver for a device and 2433 * then call that driver to initialise the hardware appropriately. The 2434 * driver is selected by calling the DEVICE_PROBE() method of a set of 2435 * candidate drivers and then choosing the driver which returned the 2436 * best value. This driver is then attached to the device using 2437 * device_attach(). 2438 * 2439 * The set of suitable drivers is taken from the list of drivers in 2440 * the parent device's devclass. If the device was originally created 2441 * with a specific class name (see device_add_child()), only drivers 2442 * with that name are probed, otherwise all drivers in the devclass 2443 * are probed. If no drivers return successful probe values in the 2444 * parent devclass, the search continues in the parent of that 2445 * devclass (see devclass_get_parent()) if any. 2446 * 2447 * @param dev the device to initialise 2448 * 2449 * @retval 0 success 2450 * @retval ENXIO no driver was found 2451 * @retval ENOMEM memory allocation failure 2452 * @retval non-zero some other unix error code 2453 * @retval -1 Device already attached 2454 */ 2455 int 2456 device_probe(device_t dev) 2457 { 2458 int error; 2459 2460 bus_topo_assert(); 2461 2462 if (dev->state >= DS_ALIVE) 2463 return (-1); 2464 2465 if (!(dev->flags & DF_ENABLED)) { 2466 if (bootverbose && device_get_name(dev) != NULL) { 2467 device_print_prettyname(dev); 2468 printf("not probed (disabled)\n"); 2469 } 2470 return (-1); 2471 } 2472 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2473 if (bus_current_pass == BUS_PASS_DEFAULT && 2474 !(dev->flags & DF_DONENOMATCH)) { 2475 device_handle_nomatch(dev); 2476 } 2477 return (error); 2478 } 2479 return (0); 2480 } 2481 2482 /** 2483 * @brief Probe a device and attach a driver if possible 2484 * 2485 * calls device_probe() and attaches if that was successful. 2486 */ 2487 int 2488 device_probe_and_attach(device_t dev) 2489 { 2490 int error; 2491 2492 bus_topo_assert(); 2493 2494 error = device_probe(dev); 2495 if (error == -1) 2496 return (0); 2497 else if (error != 0) 2498 return (error); 2499 2500 CURVNET_SET_QUIET(vnet0); 2501 error = device_attach(dev); 2502 CURVNET_RESTORE(); 2503 return error; 2504 } 2505 2506 /** 2507 * @brief Attach a device driver to a device 2508 * 2509 * This function is a wrapper around the DEVICE_ATTACH() driver 2510 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2511 * device's sysctl tree, optionally prints a description of the device 2512 * and queues a notification event for user-based device management 2513 * services. 2514 * 2515 * Normally this function is only called internally from 2516 * device_probe_and_attach(). 2517 * 2518 * @param dev the device to initialise 2519 * 2520 * @retval 0 success 2521 * @retval ENXIO no driver was found 2522 * @retval ENOMEM memory allocation failure 2523 * @retval non-zero some other unix error code 2524 */ 2525 int 2526 device_attach(device_t dev) 2527 { 2528 uint64_t attachtime; 2529 uint16_t attachentropy; 2530 int error; 2531 2532 if (resource_disabled(dev->driver->name, dev->unit)) { 2533 device_disable(dev); 2534 if (bootverbose) 2535 device_printf(dev, "disabled via hints entry\n"); 2536 return (ENXIO); 2537 } 2538 2539 device_sysctl_init(dev); 2540 if (!device_is_quiet(dev)) 2541 device_print_child(dev->parent, dev); 2542 attachtime = get_cyclecount(); 2543 dev->state = DS_ATTACHING; 2544 if ((error = DEVICE_ATTACH(dev)) != 0) { 2545 printf("device_attach: %s%d attach returned %d\n", 2546 dev->driver->name, dev->unit, error); 2547 BUS_CHILD_DETACHED(dev->parent, dev); 2548 if (disable_failed_devs) { 2549 /* 2550 * When the user has asked to disable failed devices, we 2551 * directly disable the device, but leave it in the 2552 * attaching state. It will not try to probe/attach the 2553 * device further. This leaves the device numbering 2554 * intact for other similar devices in the system. It 2555 * can be removed from this state with devctl. 2556 */ 2557 device_disable(dev); 2558 } else { 2559 /* 2560 * Otherwise, when attach fails, tear down the state 2561 * around that so we can retry when, for example, new 2562 * drivers are loaded. 2563 */ 2564 if (!(dev->flags & DF_FIXEDCLASS)) 2565 devclass_delete_device(dev->devclass, dev); 2566 (void)device_set_driver(dev, NULL); 2567 device_sysctl_fini(dev); 2568 KASSERT(dev->busy == 0, ("attach failed but busy")); 2569 dev->state = DS_NOTPRESENT; 2570 } 2571 return (error); 2572 } 2573 dev->flags |= DF_ATTACHED_ONCE; 2574 /* 2575 * We only need the low bits of this time, but ranges from tens to thousands 2576 * have been seen, so keep 2 bytes' worth. 2577 */ 2578 attachentropy = (uint16_t)(get_cyclecount() - attachtime); 2579 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH); 2580 device_sysctl_update(dev); 2581 dev->state = DS_ATTACHED; 2582 dev->flags &= ~DF_DONENOMATCH; 2583 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev); 2584 return (0); 2585 } 2586 2587 /** 2588 * @brief Detach a driver from a device 2589 * 2590 * This function is a wrapper around the DEVICE_DETACH() driver 2591 * method. If the call to DEVICE_DETACH() succeeds, it calls 2592 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2593 * notification event for user-based device management services and 2594 * cleans up the device's sysctl tree. 2595 * 2596 * @param dev the device to un-initialise 2597 * 2598 * @retval 0 success 2599 * @retval ENXIO no driver was found 2600 * @retval ENOMEM memory allocation failure 2601 * @retval non-zero some other unix error code 2602 */ 2603 int 2604 device_detach(device_t dev) 2605 { 2606 int error; 2607 2608 bus_topo_assert(); 2609 2610 PDEBUG(("%s", DEVICENAME(dev))); 2611 if (dev->busy > 0) 2612 return (EBUSY); 2613 if (dev->state == DS_ATTACHING) { 2614 device_printf(dev, "device in attaching state! Deferring detach.\n"); 2615 return (EBUSY); 2616 } 2617 if (dev->state != DS_ATTACHED) 2618 return (0); 2619 2620 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN); 2621 if ((error = DEVICE_DETACH(dev)) != 0) { 2622 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2623 EVHDEV_DETACH_FAILED); 2624 return (error); 2625 } else { 2626 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2627 EVHDEV_DETACH_COMPLETE); 2628 } 2629 if (!device_is_quiet(dev)) 2630 device_printf(dev, "detached\n"); 2631 if (dev->parent) 2632 BUS_CHILD_DETACHED(dev->parent, dev); 2633 2634 if (!(dev->flags & DF_FIXEDCLASS)) 2635 devclass_delete_device(dev->devclass, dev); 2636 2637 device_verbose(dev); 2638 dev->state = DS_NOTPRESENT; 2639 (void)device_set_driver(dev, NULL); 2640 device_sysctl_fini(dev); 2641 2642 return (0); 2643 } 2644 2645 /** 2646 * @brief Tells a driver to quiesce itself. 2647 * 2648 * This function is a wrapper around the DEVICE_QUIESCE() driver 2649 * method. If the call to DEVICE_QUIESCE() succeeds. 2650 * 2651 * @param dev the device to quiesce 2652 * 2653 * @retval 0 success 2654 * @retval ENXIO no driver was found 2655 * @retval ENOMEM memory allocation failure 2656 * @retval non-zero some other unix error code 2657 */ 2658 int 2659 device_quiesce(device_t dev) 2660 { 2661 PDEBUG(("%s", DEVICENAME(dev))); 2662 if (dev->busy > 0) 2663 return (EBUSY); 2664 if (dev->state != DS_ATTACHED) 2665 return (0); 2666 2667 return (DEVICE_QUIESCE(dev)); 2668 } 2669 2670 /** 2671 * @brief Notify a device of system shutdown 2672 * 2673 * This function calls the DEVICE_SHUTDOWN() driver method if the 2674 * device currently has an attached driver. 2675 * 2676 * @returns the value returned by DEVICE_SHUTDOWN() 2677 */ 2678 int 2679 device_shutdown(device_t dev) 2680 { 2681 if (dev->state < DS_ATTACHED) 2682 return (0); 2683 return (DEVICE_SHUTDOWN(dev)); 2684 } 2685 2686 /** 2687 * @brief Set the unit number of a device 2688 * 2689 * This function can be used to override the unit number used for a 2690 * device (e.g. to wire a device to a pre-configured unit number). 2691 */ 2692 int 2693 device_set_unit(device_t dev, int unit) 2694 { 2695 devclass_t dc; 2696 int err; 2697 2698 if (unit == dev->unit) 2699 return (0); 2700 dc = device_get_devclass(dev); 2701 if (unit < dc->maxunit && dc->devices[unit]) 2702 return (EBUSY); 2703 err = devclass_delete_device(dc, dev); 2704 if (err) 2705 return (err); 2706 dev->unit = unit; 2707 err = devclass_add_device(dc, dev); 2708 if (err) 2709 return (err); 2710 2711 bus_data_generation_update(); 2712 return (0); 2713 } 2714 2715 /*======================================*/ 2716 /* 2717 * Some useful method implementations to make life easier for bus drivers. 2718 */ 2719 2720 /** 2721 * @brief Initialize a resource mapping request 2722 * 2723 * This is the internal implementation of the public API 2724 * resource_init_map_request. Callers may be using a different layout 2725 * of struct resource_map_request than the kernel, so callers pass in 2726 * the size of the structure they are using to identify the structure 2727 * layout. 2728 */ 2729 void 2730 resource_init_map_request_impl(struct resource_map_request *args, size_t sz) 2731 { 2732 bzero(args, sz); 2733 args->size = sz; 2734 args->memattr = VM_MEMATTR_DEVICE; 2735 } 2736 2737 /** 2738 * @brief Validate a resource mapping request 2739 * 2740 * Translate a device driver's mapping request (@p in) to a struct 2741 * resource_map_request using the current structure layout (@p out). 2742 * In addition, validate the offset and length from the mapping 2743 * request against the bounds of the resource @p r. If the offset or 2744 * length are invalid, fail with EINVAL. If the offset and length are 2745 * valid, the absolute starting address of the requested mapping is 2746 * returned in @p startp and the length of the requested mapping is 2747 * returned in @p lengthp. 2748 */ 2749 int 2750 resource_validate_map_request(struct resource *r, 2751 struct resource_map_request *in, struct resource_map_request *out, 2752 rman_res_t *startp, rman_res_t *lengthp) 2753 { 2754 rman_res_t end, length, start; 2755 2756 /* 2757 * This assumes that any callers of this function are compiled 2758 * into the kernel and use the same version of the structure 2759 * as this file. 2760 */ 2761 MPASS(out->size == sizeof(struct resource_map_request)); 2762 2763 if (in != NULL) 2764 bcopy(in, out, imin(in->size, out->size)); 2765 start = rman_get_start(r) + out->offset; 2766 if (out->length == 0) 2767 length = rman_get_size(r); 2768 else 2769 length = out->length; 2770 end = start + length - 1; 2771 if (start > rman_get_end(r) || start < rman_get_start(r)) 2772 return (EINVAL); 2773 if (end > rman_get_end(r) || end < start) 2774 return (EINVAL); 2775 *lengthp = length; 2776 *startp = start; 2777 return (0); 2778 } 2779 2780 /** 2781 * @brief Initialise a resource list. 2782 * 2783 * @param rl the resource list to initialise 2784 */ 2785 void 2786 resource_list_init(struct resource_list *rl) 2787 { 2788 STAILQ_INIT(rl); 2789 } 2790 2791 /** 2792 * @brief Reclaim memory used by a resource list. 2793 * 2794 * This function frees the memory for all resource entries on the list 2795 * (if any). 2796 * 2797 * @param rl the resource list to free 2798 */ 2799 void 2800 resource_list_free(struct resource_list *rl) 2801 { 2802 struct resource_list_entry *rle; 2803 2804 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2805 if (rle->res) 2806 panic("resource_list_free: resource entry is busy"); 2807 STAILQ_REMOVE_HEAD(rl, link); 2808 free(rle, M_BUS); 2809 } 2810 } 2811 2812 /** 2813 * @brief Add a resource entry. 2814 * 2815 * This function adds a resource entry using the given @p type, @p 2816 * start, @p end and @p count values. A rid value is chosen by 2817 * searching sequentially for the first unused rid starting at zero. 2818 * 2819 * @param rl the resource list to edit 2820 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2821 * @param start the start address of the resource 2822 * @param end the end address of the resource 2823 * @param count XXX end-start+1 2824 */ 2825 int 2826 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start, 2827 rman_res_t end, rman_res_t count) 2828 { 2829 int rid; 2830 2831 rid = 0; 2832 while (resource_list_find(rl, type, rid) != NULL) 2833 rid++; 2834 resource_list_add(rl, type, rid, start, end, count); 2835 return (rid); 2836 } 2837 2838 /** 2839 * @brief Add or modify a resource entry. 2840 * 2841 * If an existing entry exists with the same type and rid, it will be 2842 * modified using the given values of @p start, @p end and @p 2843 * count. If no entry exists, a new one will be created using the 2844 * given values. The resource list entry that matches is then returned. 2845 * 2846 * @param rl the resource list to edit 2847 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2848 * @param rid the resource identifier 2849 * @param start the start address of the resource 2850 * @param end the end address of the resource 2851 * @param count XXX end-start+1 2852 */ 2853 struct resource_list_entry * 2854 resource_list_add(struct resource_list *rl, int type, int rid, 2855 rman_res_t start, rman_res_t end, rman_res_t count) 2856 { 2857 struct resource_list_entry *rle; 2858 2859 rle = resource_list_find(rl, type, rid); 2860 if (!rle) { 2861 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 2862 M_NOWAIT); 2863 if (!rle) 2864 panic("resource_list_add: can't record entry"); 2865 STAILQ_INSERT_TAIL(rl, rle, link); 2866 rle->type = type; 2867 rle->rid = rid; 2868 rle->res = NULL; 2869 rle->flags = 0; 2870 } 2871 2872 if (rle->res) 2873 panic("resource_list_add: resource entry is busy"); 2874 2875 rle->start = start; 2876 rle->end = end; 2877 rle->count = count; 2878 return (rle); 2879 } 2880 2881 /** 2882 * @brief Determine if a resource entry is busy. 2883 * 2884 * Returns true if a resource entry is busy meaning that it has an 2885 * associated resource that is not an unallocated "reserved" resource. 2886 * 2887 * @param rl the resource list to search 2888 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2889 * @param rid the resource identifier 2890 * 2891 * @returns Non-zero if the entry is busy, zero otherwise. 2892 */ 2893 int 2894 resource_list_busy(struct resource_list *rl, int type, int rid) 2895 { 2896 struct resource_list_entry *rle; 2897 2898 rle = resource_list_find(rl, type, rid); 2899 if (rle == NULL || rle->res == NULL) 2900 return (0); 2901 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) { 2902 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE), 2903 ("reserved resource is active")); 2904 return (0); 2905 } 2906 return (1); 2907 } 2908 2909 /** 2910 * @brief Determine if a resource entry is reserved. 2911 * 2912 * Returns true if a resource entry is reserved meaning that it has an 2913 * associated "reserved" resource. The resource can either be 2914 * allocated or unallocated. 2915 * 2916 * @param rl the resource list to search 2917 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2918 * @param rid the resource identifier 2919 * 2920 * @returns Non-zero if the entry is reserved, zero otherwise. 2921 */ 2922 int 2923 resource_list_reserved(struct resource_list *rl, int type, int rid) 2924 { 2925 struct resource_list_entry *rle; 2926 2927 rle = resource_list_find(rl, type, rid); 2928 if (rle != NULL && rle->flags & RLE_RESERVED) 2929 return (1); 2930 return (0); 2931 } 2932 2933 /** 2934 * @brief Find a resource entry by type and rid. 2935 * 2936 * @param rl the resource list to search 2937 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2938 * @param rid the resource identifier 2939 * 2940 * @returns the resource entry pointer or NULL if there is no such 2941 * entry. 2942 */ 2943 struct resource_list_entry * 2944 resource_list_find(struct resource_list *rl, int type, int rid) 2945 { 2946 struct resource_list_entry *rle; 2947 2948 STAILQ_FOREACH(rle, rl, link) { 2949 if (rle->type == type && rle->rid == rid) 2950 return (rle); 2951 } 2952 return (NULL); 2953 } 2954 2955 /** 2956 * @brief Delete a resource entry. 2957 * 2958 * @param rl the resource list to edit 2959 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2960 * @param rid the resource identifier 2961 */ 2962 void 2963 resource_list_delete(struct resource_list *rl, int type, int rid) 2964 { 2965 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2966 2967 if (rle) { 2968 if (rle->res != NULL) 2969 panic("resource_list_delete: resource has not been released"); 2970 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 2971 free(rle, M_BUS); 2972 } 2973 } 2974 2975 /** 2976 * @brief Allocate a reserved resource 2977 * 2978 * This can be used by buses to force the allocation of resources 2979 * that are always active in the system even if they are not allocated 2980 * by a driver (e.g. PCI BARs). This function is usually called when 2981 * adding a new child to the bus. The resource is allocated from the 2982 * parent bus when it is reserved. The resource list entry is marked 2983 * with RLE_RESERVED to note that it is a reserved resource. 2984 * 2985 * Subsequent attempts to allocate the resource with 2986 * resource_list_alloc() will succeed the first time and will set 2987 * RLE_ALLOCATED to note that it has been allocated. When a reserved 2988 * resource that has been allocated is released with 2989 * resource_list_release() the resource RLE_ALLOCATED is cleared, but 2990 * the actual resource remains allocated. The resource can be released to 2991 * the parent bus by calling resource_list_unreserve(). 2992 * 2993 * @param rl the resource list to allocate from 2994 * @param bus the parent device of @p child 2995 * @param child the device for which the resource is being reserved 2996 * @param type the type of resource to allocate 2997 * @param rid a pointer to the resource identifier 2998 * @param start hint at the start of the resource range - pass 2999 * @c 0 for any start address 3000 * @param end hint at the end of the resource range - pass 3001 * @c ~0 for any end address 3002 * @param count hint at the size of range required - pass @c 1 3003 * for any size 3004 * @param flags any extra flags to control the resource 3005 * allocation - see @c RF_XXX flags in 3006 * <sys/rman.h> for details 3007 * 3008 * @returns the resource which was allocated or @c NULL if no 3009 * resource could be allocated 3010 */ 3011 struct resource * 3012 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, 3013 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3014 { 3015 struct resource_list_entry *rle = NULL; 3016 int passthrough = (device_get_parent(child) != bus); 3017 struct resource *r; 3018 3019 if (passthrough) 3020 panic( 3021 "resource_list_reserve() should only be called for direct children"); 3022 if (flags & RF_ACTIVE) 3023 panic( 3024 "resource_list_reserve() should only reserve inactive resources"); 3025 3026 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 3027 flags); 3028 if (r != NULL) { 3029 rle = resource_list_find(rl, type, *rid); 3030 rle->flags |= RLE_RESERVED; 3031 } 3032 return (r); 3033 } 3034 3035 /** 3036 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 3037 * 3038 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 3039 * and passing the allocation up to the parent of @p bus. This assumes 3040 * that the first entry of @c device_get_ivars(child) is a struct 3041 * resource_list. This also handles 'passthrough' allocations where a 3042 * child is a remote descendant of bus by passing the allocation up to 3043 * the parent of bus. 3044 * 3045 * Typically, a bus driver would store a list of child resources 3046 * somewhere in the child device's ivars (see device_get_ivars()) and 3047 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 3048 * then call resource_list_alloc() to perform the allocation. 3049 * 3050 * @param rl the resource list to allocate from 3051 * @param bus the parent device of @p child 3052 * @param child the device which is requesting an allocation 3053 * @param type the type of resource to allocate 3054 * @param rid a pointer to the resource identifier 3055 * @param start hint at the start of the resource range - pass 3056 * @c 0 for any start address 3057 * @param end hint at the end of the resource range - pass 3058 * @c ~0 for any end address 3059 * @param count hint at the size of range required - pass @c 1 3060 * for any size 3061 * @param flags any extra flags to control the resource 3062 * allocation - see @c RF_XXX flags in 3063 * <sys/rman.h> for details 3064 * 3065 * @returns the resource which was allocated or @c NULL if no 3066 * resource could be allocated 3067 */ 3068 struct resource * 3069 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 3070 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3071 { 3072 struct resource_list_entry *rle = NULL; 3073 int passthrough = (device_get_parent(child) != bus); 3074 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 3075 3076 if (passthrough) { 3077 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3078 type, rid, start, end, count, flags)); 3079 } 3080 3081 rle = resource_list_find(rl, type, *rid); 3082 3083 if (!rle) 3084 return (NULL); /* no resource of that type/rid */ 3085 3086 if (rle->res) { 3087 if (rle->flags & RLE_RESERVED) { 3088 if (rle->flags & RLE_ALLOCATED) 3089 return (NULL); 3090 if ((flags & RF_ACTIVE) && 3091 bus_activate_resource(child, type, *rid, 3092 rle->res) != 0) 3093 return (NULL); 3094 rle->flags |= RLE_ALLOCATED; 3095 return (rle->res); 3096 } 3097 device_printf(bus, 3098 "resource entry %#x type %d for child %s is busy\n", *rid, 3099 type, device_get_nameunit(child)); 3100 return (NULL); 3101 } 3102 3103 if (isdefault) { 3104 start = rle->start; 3105 count = ulmax(count, rle->count); 3106 end = ulmax(rle->end, start + count - 1); 3107 } 3108 3109 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3110 type, rid, start, end, count, flags); 3111 3112 /* 3113 * Record the new range. 3114 */ 3115 if (rle->res) { 3116 rle->start = rman_get_start(rle->res); 3117 rle->end = rman_get_end(rle->res); 3118 rle->count = count; 3119 } 3120 3121 return (rle->res); 3122 } 3123 3124 /** 3125 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 3126 * 3127 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 3128 * used with resource_list_alloc(). 3129 * 3130 * @param rl the resource list which was allocated from 3131 * @param bus the parent device of @p child 3132 * @param child the device which is requesting a release 3133 * @param res the resource to release 3134 * 3135 * @retval 0 success 3136 * @retval non-zero a standard unix error code indicating what 3137 * error condition prevented the operation 3138 */ 3139 int 3140 resource_list_release(struct resource_list *rl, device_t bus, device_t child, 3141 struct resource *res) 3142 { 3143 struct resource_list_entry *rle = NULL; 3144 int passthrough = (device_get_parent(child) != bus); 3145 int error; 3146 3147 if (passthrough) { 3148 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3149 res)); 3150 } 3151 3152 rle = resource_list_find(rl, rman_get_type(res), rman_get_rid(res)); 3153 3154 if (!rle) 3155 panic("resource_list_release: can't find resource"); 3156 if (!rle->res) 3157 panic("resource_list_release: resource entry is not busy"); 3158 if (rle->flags & RLE_RESERVED) { 3159 if (rle->flags & RLE_ALLOCATED) { 3160 if (rman_get_flags(res) & RF_ACTIVE) { 3161 error = bus_deactivate_resource(child, res); 3162 if (error) 3163 return (error); 3164 } 3165 rle->flags &= ~RLE_ALLOCATED; 3166 return (0); 3167 } 3168 return (EINVAL); 3169 } 3170 3171 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, res); 3172 if (error) 3173 return (error); 3174 3175 rle->res = NULL; 3176 return (0); 3177 } 3178 3179 /** 3180 * @brief Release all active resources of a given type 3181 * 3182 * Release all active resources of a specified type. This is intended 3183 * to be used to cleanup resources leaked by a driver after detach or 3184 * a failed attach. 3185 * 3186 * @param rl the resource list which was allocated from 3187 * @param bus the parent device of @p child 3188 * @param child the device whose active resources are being released 3189 * @param type the type of resources to release 3190 * 3191 * @retval 0 success 3192 * @retval EBUSY at least one resource was active 3193 */ 3194 int 3195 resource_list_release_active(struct resource_list *rl, device_t bus, 3196 device_t child, int type) 3197 { 3198 struct resource_list_entry *rle; 3199 int error, retval; 3200 3201 retval = 0; 3202 STAILQ_FOREACH(rle, rl, link) { 3203 if (rle->type != type) 3204 continue; 3205 if (rle->res == NULL) 3206 continue; 3207 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == 3208 RLE_RESERVED) 3209 continue; 3210 retval = EBUSY; 3211 error = resource_list_release(rl, bus, child, rle->res); 3212 if (error != 0) 3213 device_printf(bus, 3214 "Failed to release active resource: %d\n", error); 3215 } 3216 return (retval); 3217 } 3218 3219 /** 3220 * @brief Fully release a reserved resource 3221 * 3222 * Fully releases a resource reserved via resource_list_reserve(). 3223 * 3224 * @param rl the resource list which was allocated from 3225 * @param bus the parent device of @p child 3226 * @param child the device whose reserved resource is being released 3227 * @param type the type of resource to release 3228 * @param rid the resource identifier 3229 * @param res the resource to release 3230 * 3231 * @retval 0 success 3232 * @retval non-zero a standard unix error code indicating what 3233 * error condition prevented the operation 3234 */ 3235 int 3236 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, 3237 int type, int rid) 3238 { 3239 struct resource_list_entry *rle = NULL; 3240 int passthrough = (device_get_parent(child) != bus); 3241 3242 if (passthrough) 3243 panic( 3244 "resource_list_unreserve() should only be called for direct children"); 3245 3246 rle = resource_list_find(rl, type, rid); 3247 3248 if (!rle) 3249 panic("resource_list_unreserve: can't find resource"); 3250 if (!(rle->flags & RLE_RESERVED)) 3251 return (EINVAL); 3252 if (rle->flags & RLE_ALLOCATED) 3253 return (EBUSY); 3254 rle->flags &= ~RLE_RESERVED; 3255 return (resource_list_release(rl, bus, child, rle->res)); 3256 } 3257 3258 /** 3259 * @brief Print a description of resources in a resource list 3260 * 3261 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 3262 * The name is printed if at least one resource of the given type is available. 3263 * The format is used to print resource start and end. 3264 * 3265 * @param rl the resource list to print 3266 * @param name the name of @p type, e.g. @c "memory" 3267 * @param type type type of resource entry to print 3268 * @param format printf(9) format string to print resource 3269 * start and end values 3270 * 3271 * @returns the number of characters printed 3272 */ 3273 int 3274 resource_list_print_type(struct resource_list *rl, const char *name, int type, 3275 const char *format) 3276 { 3277 struct resource_list_entry *rle; 3278 int printed, retval; 3279 3280 printed = 0; 3281 retval = 0; 3282 /* Yes, this is kinda cheating */ 3283 STAILQ_FOREACH(rle, rl, link) { 3284 if (rle->type == type) { 3285 if (printed == 0) 3286 retval += printf(" %s ", name); 3287 else 3288 retval += printf(","); 3289 printed++; 3290 retval += printf(format, rle->start); 3291 if (rle->count > 1) { 3292 retval += printf("-"); 3293 retval += printf(format, rle->start + 3294 rle->count - 1); 3295 } 3296 } 3297 } 3298 return (retval); 3299 } 3300 3301 /** 3302 * @brief Releases all the resources in a list. 3303 * 3304 * @param rl The resource list to purge. 3305 * 3306 * @returns nothing 3307 */ 3308 void 3309 resource_list_purge(struct resource_list *rl) 3310 { 3311 struct resource_list_entry *rle; 3312 3313 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3314 if (rle->res) 3315 bus_release_resource(rman_get_device(rle->res), 3316 rle->type, rle->rid, rle->res); 3317 STAILQ_REMOVE_HEAD(rl, link); 3318 free(rle, M_BUS); 3319 } 3320 } 3321 3322 device_t 3323 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 3324 { 3325 return (device_add_child_ordered(dev, order, name, unit)); 3326 } 3327 3328 /** 3329 * @brief Helper function for implementing DEVICE_PROBE() 3330 * 3331 * This function can be used to help implement the DEVICE_PROBE() for 3332 * a bus (i.e. a device which has other devices attached to it). It 3333 * calls the DEVICE_IDENTIFY() method of each driver in the device's 3334 * devclass. 3335 */ 3336 int 3337 bus_generic_probe(device_t dev) 3338 { 3339 devclass_t dc = dev->devclass; 3340 driverlink_t dl; 3341 3342 TAILQ_FOREACH(dl, &dc->drivers, link) { 3343 /* 3344 * If this driver's pass is too high, then ignore it. 3345 * For most drivers in the default pass, this will 3346 * never be true. For early-pass drivers they will 3347 * only call the identify routines of eligible drivers 3348 * when this routine is called. Drivers for later 3349 * passes should have their identify routines called 3350 * on early-pass buses during BUS_NEW_PASS(). 3351 */ 3352 if (dl->pass > bus_current_pass) 3353 continue; 3354 DEVICE_IDENTIFY(dl->driver, dev); 3355 } 3356 3357 return (0); 3358 } 3359 3360 /** 3361 * @brief Helper function for implementing DEVICE_ATTACH() 3362 * 3363 * This function can be used to help implement the DEVICE_ATTACH() for 3364 * a bus. It calls device_probe_and_attach() for each of the device's 3365 * children. 3366 */ 3367 int 3368 bus_generic_attach(device_t dev) 3369 { 3370 device_t child; 3371 3372 TAILQ_FOREACH(child, &dev->children, link) { 3373 device_probe_and_attach(child); 3374 } 3375 3376 return (0); 3377 } 3378 3379 /** 3380 * @brief Helper function for delaying attaching children 3381 * 3382 * Many buses can't run transactions on the bus which children need to probe and 3383 * attach until after interrupts and/or timers are running. This function 3384 * delays their attach until interrupts and timers are enabled. 3385 */ 3386 int 3387 bus_delayed_attach_children(device_t dev) 3388 { 3389 /* Probe and attach the bus children when interrupts are available */ 3390 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 3391 3392 return (0); 3393 } 3394 3395 /** 3396 * @brief Helper function for implementing DEVICE_DETACH() 3397 * 3398 * This function can be used to help implement the DEVICE_DETACH() for 3399 * a bus. It calls device_detach() for each of the device's 3400 * children. 3401 */ 3402 int 3403 bus_generic_detach(device_t dev) 3404 { 3405 device_t child; 3406 int error; 3407 3408 /* 3409 * Detach children in the reverse order. 3410 * See bus_generic_suspend for details. 3411 */ 3412 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3413 if ((error = device_detach(child)) != 0) 3414 return (error); 3415 } 3416 3417 return (0); 3418 } 3419 3420 /** 3421 * @brief Helper function for implementing DEVICE_SHUTDOWN() 3422 * 3423 * This function can be used to help implement the DEVICE_SHUTDOWN() 3424 * for a bus. It calls device_shutdown() for each of the device's 3425 * children. 3426 */ 3427 int 3428 bus_generic_shutdown(device_t dev) 3429 { 3430 device_t child; 3431 3432 /* 3433 * Shut down children in the reverse order. 3434 * See bus_generic_suspend for details. 3435 */ 3436 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3437 device_shutdown(child); 3438 } 3439 3440 return (0); 3441 } 3442 3443 /** 3444 * @brief Default function for suspending a child device. 3445 * 3446 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD(). 3447 */ 3448 int 3449 bus_generic_suspend_child(device_t dev, device_t child) 3450 { 3451 int error; 3452 3453 error = DEVICE_SUSPEND(child); 3454 3455 if (error == 0) { 3456 child->flags |= DF_SUSPENDED; 3457 } else { 3458 printf("DEVICE_SUSPEND(%s) failed: %d\n", 3459 device_get_nameunit(child), error); 3460 } 3461 3462 return (error); 3463 } 3464 3465 /** 3466 * @brief Default function for resuming a child device. 3467 * 3468 * This function is to be used by a bus's DEVICE_RESUME_CHILD(). 3469 */ 3470 int 3471 bus_generic_resume_child(device_t dev, device_t child) 3472 { 3473 DEVICE_RESUME(child); 3474 child->flags &= ~DF_SUSPENDED; 3475 3476 return (0); 3477 } 3478 3479 /** 3480 * @brief Helper function for implementing DEVICE_SUSPEND() 3481 * 3482 * This function can be used to help implement the DEVICE_SUSPEND() 3483 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 3484 * children. If any call to DEVICE_SUSPEND() fails, the suspend 3485 * operation is aborted and any devices which were suspended are 3486 * resumed immediately by calling their DEVICE_RESUME() methods. 3487 */ 3488 int 3489 bus_generic_suspend(device_t dev) 3490 { 3491 int error; 3492 device_t child; 3493 3494 /* 3495 * Suspend children in the reverse order. 3496 * For most buses all children are equal, so the order does not matter. 3497 * Other buses, such as acpi, carefully order their child devices to 3498 * express implicit dependencies between them. For such buses it is 3499 * safer to bring down devices in the reverse order. 3500 */ 3501 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3502 error = BUS_SUSPEND_CHILD(dev, child); 3503 if (error != 0) { 3504 child = TAILQ_NEXT(child, link); 3505 if (child != NULL) { 3506 TAILQ_FOREACH_FROM(child, &dev->children, link) 3507 BUS_RESUME_CHILD(dev, child); 3508 } 3509 return (error); 3510 } 3511 } 3512 return (0); 3513 } 3514 3515 /** 3516 * @brief Helper function for implementing DEVICE_RESUME() 3517 * 3518 * This function can be used to help implement the DEVICE_RESUME() for 3519 * a bus. It calls DEVICE_RESUME() on each of the device's children. 3520 */ 3521 int 3522 bus_generic_resume(device_t dev) 3523 { 3524 device_t child; 3525 3526 TAILQ_FOREACH(child, &dev->children, link) { 3527 BUS_RESUME_CHILD(dev, child); 3528 /* if resume fails, there's nothing we can usefully do... */ 3529 } 3530 return (0); 3531 } 3532 3533 /** 3534 * @brief Helper function for implementing BUS_RESET_POST 3535 * 3536 * Bus can use this function to implement common operations of 3537 * re-attaching or resuming the children after the bus itself was 3538 * reset, and after restoring bus-unique state of children. 3539 * 3540 * @param dev The bus 3541 * #param flags DEVF_RESET_* 3542 */ 3543 int 3544 bus_helper_reset_post(device_t dev, int flags) 3545 { 3546 device_t child; 3547 int error, error1; 3548 3549 error = 0; 3550 TAILQ_FOREACH(child, &dev->children,link) { 3551 BUS_RESET_POST(dev, child); 3552 error1 = (flags & DEVF_RESET_DETACH) != 0 ? 3553 device_probe_and_attach(child) : 3554 BUS_RESUME_CHILD(dev, child); 3555 if (error == 0 && error1 != 0) 3556 error = error1; 3557 } 3558 return (error); 3559 } 3560 3561 static void 3562 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags) 3563 { 3564 child = TAILQ_NEXT(child, link); 3565 if (child == NULL) 3566 return; 3567 TAILQ_FOREACH_FROM(child, &dev->children,link) { 3568 BUS_RESET_POST(dev, child); 3569 if ((flags & DEVF_RESET_DETACH) != 0) 3570 device_probe_and_attach(child); 3571 else 3572 BUS_RESUME_CHILD(dev, child); 3573 } 3574 } 3575 3576 /** 3577 * @brief Helper function for implementing BUS_RESET_PREPARE 3578 * 3579 * Bus can use this function to implement common operations of 3580 * detaching or suspending the children before the bus itself is 3581 * reset, and then save bus-unique state of children that must 3582 * persists around reset. 3583 * 3584 * @param dev The bus 3585 * #param flags DEVF_RESET_* 3586 */ 3587 int 3588 bus_helper_reset_prepare(device_t dev, int flags) 3589 { 3590 device_t child; 3591 int error; 3592 3593 if (dev->state != DS_ATTACHED) 3594 return (EBUSY); 3595 3596 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3597 if ((flags & DEVF_RESET_DETACH) != 0) { 3598 error = device_get_state(child) == DS_ATTACHED ? 3599 device_detach(child) : 0; 3600 } else { 3601 error = BUS_SUSPEND_CHILD(dev, child); 3602 } 3603 if (error == 0) { 3604 error = BUS_RESET_PREPARE(dev, child); 3605 if (error != 0) { 3606 if ((flags & DEVF_RESET_DETACH) != 0) 3607 device_probe_and_attach(child); 3608 else 3609 BUS_RESUME_CHILD(dev, child); 3610 } 3611 } 3612 if (error != 0) { 3613 bus_helper_reset_prepare_rollback(dev, child, flags); 3614 return (error); 3615 } 3616 } 3617 return (0); 3618 } 3619 3620 /** 3621 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3622 * 3623 * This function prints the first part of the ascii representation of 3624 * @p child, including its name, unit and description (if any - see 3625 * device_set_desc()). 3626 * 3627 * @returns the number of characters printed 3628 */ 3629 int 3630 bus_print_child_header(device_t dev, device_t child) 3631 { 3632 int retval = 0; 3633 3634 if (device_get_desc(child)) { 3635 retval += device_printf(child, "<%s>", device_get_desc(child)); 3636 } else { 3637 retval += printf("%s", device_get_nameunit(child)); 3638 } 3639 3640 return (retval); 3641 } 3642 3643 /** 3644 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3645 * 3646 * This function prints the last part of the ascii representation of 3647 * @p child, which consists of the string @c " on " followed by the 3648 * name and unit of the @p dev. 3649 * 3650 * @returns the number of characters printed 3651 */ 3652 int 3653 bus_print_child_footer(device_t dev, device_t child) 3654 { 3655 return (printf(" on %s\n", device_get_nameunit(dev))); 3656 } 3657 3658 /** 3659 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3660 * 3661 * This function prints out the VM domain for the given device. 3662 * 3663 * @returns the number of characters printed 3664 */ 3665 int 3666 bus_print_child_domain(device_t dev, device_t child) 3667 { 3668 int domain; 3669 3670 /* No domain? Don't print anything */ 3671 if (BUS_GET_DOMAIN(dev, child, &domain) != 0) 3672 return (0); 3673 3674 return (printf(" numa-domain %d", domain)); 3675 } 3676 3677 /** 3678 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3679 * 3680 * This function simply calls bus_print_child_header() followed by 3681 * bus_print_child_footer(). 3682 * 3683 * @returns the number of characters printed 3684 */ 3685 int 3686 bus_generic_print_child(device_t dev, device_t child) 3687 { 3688 int retval = 0; 3689 3690 retval += bus_print_child_header(dev, child); 3691 retval += bus_print_child_domain(dev, child); 3692 retval += bus_print_child_footer(dev, child); 3693 3694 return (retval); 3695 } 3696 3697 /** 3698 * @brief Stub function for implementing BUS_READ_IVAR(). 3699 * 3700 * @returns ENOENT 3701 */ 3702 int 3703 bus_generic_read_ivar(device_t dev, device_t child, int index, 3704 uintptr_t * result) 3705 { 3706 return (ENOENT); 3707 } 3708 3709 /** 3710 * @brief Stub function for implementing BUS_WRITE_IVAR(). 3711 * 3712 * @returns ENOENT 3713 */ 3714 int 3715 bus_generic_write_ivar(device_t dev, device_t child, int index, 3716 uintptr_t value) 3717 { 3718 return (ENOENT); 3719 } 3720 3721 /** 3722 * @brief Helper function for implementing BUS_GET_PROPERTY(). 3723 * 3724 * This simply calls the BUS_GET_PROPERTY of the parent of dev, 3725 * until a non-default implementation is found. 3726 */ 3727 ssize_t 3728 bus_generic_get_property(device_t dev, device_t child, const char *propname, 3729 void *propvalue, size_t size, device_property_type_t type) 3730 { 3731 if (device_get_parent(dev) != NULL) 3732 return (BUS_GET_PROPERTY(device_get_parent(dev), child, 3733 propname, propvalue, size, type)); 3734 3735 return (-1); 3736 } 3737 3738 /** 3739 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 3740 * 3741 * @returns NULL 3742 */ 3743 struct resource_list * 3744 bus_generic_get_resource_list(device_t dev, device_t child) 3745 { 3746 return (NULL); 3747 } 3748 3749 /** 3750 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 3751 * 3752 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 3753 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 3754 * and then calls device_probe_and_attach() for each unattached child. 3755 */ 3756 void 3757 bus_generic_driver_added(device_t dev, driver_t *driver) 3758 { 3759 device_t child; 3760 3761 DEVICE_IDENTIFY(driver, dev); 3762 TAILQ_FOREACH(child, &dev->children, link) { 3763 if (child->state == DS_NOTPRESENT) 3764 device_probe_and_attach(child); 3765 } 3766 } 3767 3768 /** 3769 * @brief Helper function for implementing BUS_NEW_PASS(). 3770 * 3771 * This implementing of BUS_NEW_PASS() first calls the identify 3772 * routines for any drivers that probe at the current pass. Then it 3773 * walks the list of devices for this bus. If a device is already 3774 * attached, then it calls BUS_NEW_PASS() on that device. If the 3775 * device is not already attached, it attempts to attach a driver to 3776 * it. 3777 */ 3778 void 3779 bus_generic_new_pass(device_t dev) 3780 { 3781 driverlink_t dl; 3782 devclass_t dc; 3783 device_t child; 3784 3785 dc = dev->devclass; 3786 TAILQ_FOREACH(dl, &dc->drivers, link) { 3787 if (dl->pass == bus_current_pass) 3788 DEVICE_IDENTIFY(dl->driver, dev); 3789 } 3790 TAILQ_FOREACH(child, &dev->children, link) { 3791 if (child->state >= DS_ATTACHED) 3792 BUS_NEW_PASS(child); 3793 else if (child->state == DS_NOTPRESENT) 3794 device_probe_and_attach(child); 3795 } 3796 } 3797 3798 /** 3799 * @brief Helper function for implementing BUS_SETUP_INTR(). 3800 * 3801 * This simple implementation of BUS_SETUP_INTR() simply calls the 3802 * BUS_SETUP_INTR() method of the parent of @p dev. 3803 */ 3804 int 3805 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3806 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 3807 void **cookiep) 3808 { 3809 /* Propagate up the bus hierarchy until someone handles it. */ 3810 if (dev->parent) 3811 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3812 filter, intr, arg, cookiep)); 3813 return (EINVAL); 3814 } 3815 3816 /** 3817 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3818 * 3819 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3820 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3821 */ 3822 int 3823 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3824 void *cookie) 3825 { 3826 /* Propagate up the bus hierarchy until someone handles it. */ 3827 if (dev->parent) 3828 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3829 return (EINVAL); 3830 } 3831 3832 /** 3833 * @brief Helper function for implementing BUS_SUSPEND_INTR(). 3834 * 3835 * This simple implementation of BUS_SUSPEND_INTR() simply calls the 3836 * BUS_SUSPEND_INTR() method of the parent of @p dev. 3837 */ 3838 int 3839 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq) 3840 { 3841 /* Propagate up the bus hierarchy until someone handles it. */ 3842 if (dev->parent) 3843 return (BUS_SUSPEND_INTR(dev->parent, child, irq)); 3844 return (EINVAL); 3845 } 3846 3847 /** 3848 * @brief Helper function for implementing BUS_RESUME_INTR(). 3849 * 3850 * This simple implementation of BUS_RESUME_INTR() simply calls the 3851 * BUS_RESUME_INTR() method of the parent of @p dev. 3852 */ 3853 int 3854 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq) 3855 { 3856 /* Propagate up the bus hierarchy until someone handles it. */ 3857 if (dev->parent) 3858 return (BUS_RESUME_INTR(dev->parent, child, irq)); 3859 return (EINVAL); 3860 } 3861 3862 /** 3863 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 3864 * 3865 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the 3866 * BUS_ADJUST_RESOURCE() method of the parent of @p dev. 3867 */ 3868 int 3869 bus_generic_adjust_resource(device_t dev, device_t child, struct resource *r, 3870 rman_res_t start, rman_res_t end) 3871 { 3872 /* Propagate up the bus hierarchy until someone handles it. */ 3873 if (dev->parent) 3874 return (BUS_ADJUST_RESOURCE(dev->parent, child, r, start, end)); 3875 return (EINVAL); 3876 } 3877 3878 /* 3879 * @brief Helper function for implementing BUS_TRANSLATE_RESOURCE(). 3880 * 3881 * This simple implementation of BUS_TRANSLATE_RESOURCE() simply calls the 3882 * BUS_TRANSLATE_RESOURCE() method of the parent of @p dev. If there is no 3883 * parent, no translation happens. 3884 */ 3885 int 3886 bus_generic_translate_resource(device_t dev, int type, rman_res_t start, 3887 rman_res_t *newstart) 3888 { 3889 if (dev->parent) 3890 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, 3891 newstart)); 3892 *newstart = start; 3893 return (0); 3894 } 3895 3896 /** 3897 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3898 * 3899 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3900 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3901 */ 3902 struct resource * 3903 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3904 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3905 { 3906 /* Propagate up the bus hierarchy until someone handles it. */ 3907 if (dev->parent) 3908 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3909 start, end, count, flags)); 3910 return (NULL); 3911 } 3912 3913 /** 3914 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3915 * 3916 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3917 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3918 */ 3919 int 3920 bus_generic_release_resource(device_t dev, device_t child, struct resource *r) 3921 { 3922 /* Propagate up the bus hierarchy until someone handles it. */ 3923 if (dev->parent) 3924 return (BUS_RELEASE_RESOURCE(dev->parent, child, r)); 3925 return (EINVAL); 3926 } 3927 3928 /** 3929 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3930 * 3931 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3932 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3933 */ 3934 int 3935 bus_generic_activate_resource(device_t dev, device_t child, struct resource *r) 3936 { 3937 /* Propagate up the bus hierarchy until someone handles it. */ 3938 if (dev->parent) 3939 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, r)); 3940 return (EINVAL); 3941 } 3942 3943 /** 3944 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3945 * 3946 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3947 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3948 */ 3949 int 3950 bus_generic_deactivate_resource(device_t dev, device_t child, 3951 struct resource *r) 3952 { 3953 /* Propagate up the bus hierarchy until someone handles it. */ 3954 if (dev->parent) 3955 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, r)); 3956 return (EINVAL); 3957 } 3958 3959 /** 3960 * @brief Helper function for implementing BUS_MAP_RESOURCE(). 3961 * 3962 * This simple implementation of BUS_MAP_RESOURCE() simply calls the 3963 * BUS_MAP_RESOURCE() method of the parent of @p dev. 3964 */ 3965 int 3966 bus_generic_map_resource(device_t dev, device_t child, struct resource *r, 3967 struct resource_map_request *args, struct resource_map *map) 3968 { 3969 /* Propagate up the bus hierarchy until someone handles it. */ 3970 if (dev->parent) 3971 return (BUS_MAP_RESOURCE(dev->parent, child, r, args, map)); 3972 return (EINVAL); 3973 } 3974 3975 /** 3976 * @brief Helper function for implementing BUS_UNMAP_RESOURCE(). 3977 * 3978 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the 3979 * BUS_UNMAP_RESOURCE() method of the parent of @p dev. 3980 */ 3981 int 3982 bus_generic_unmap_resource(device_t dev, device_t child, struct resource *r, 3983 struct resource_map *map) 3984 { 3985 /* Propagate up the bus hierarchy until someone handles it. */ 3986 if (dev->parent) 3987 return (BUS_UNMAP_RESOURCE(dev->parent, child, r, map)); 3988 return (EINVAL); 3989 } 3990 3991 /** 3992 * @brief Helper function for implementing BUS_BIND_INTR(). 3993 * 3994 * This simple implementation of BUS_BIND_INTR() simply calls the 3995 * BUS_BIND_INTR() method of the parent of @p dev. 3996 */ 3997 int 3998 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, 3999 int cpu) 4000 { 4001 /* Propagate up the bus hierarchy until someone handles it. */ 4002 if (dev->parent) 4003 return (BUS_BIND_INTR(dev->parent, child, irq, cpu)); 4004 return (EINVAL); 4005 } 4006 4007 /** 4008 * @brief Helper function for implementing BUS_CONFIG_INTR(). 4009 * 4010 * This simple implementation of BUS_CONFIG_INTR() simply calls the 4011 * BUS_CONFIG_INTR() method of the parent of @p dev. 4012 */ 4013 int 4014 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 4015 enum intr_polarity pol) 4016 { 4017 /* Propagate up the bus hierarchy until someone handles it. */ 4018 if (dev->parent) 4019 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 4020 return (EINVAL); 4021 } 4022 4023 /** 4024 * @brief Helper function for implementing BUS_DESCRIBE_INTR(). 4025 * 4026 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the 4027 * BUS_DESCRIBE_INTR() method of the parent of @p dev. 4028 */ 4029 int 4030 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, 4031 void *cookie, const char *descr) 4032 { 4033 /* Propagate up the bus hierarchy until someone handles it. */ 4034 if (dev->parent) 4035 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie, 4036 descr)); 4037 return (EINVAL); 4038 } 4039 4040 /** 4041 * @brief Helper function for implementing BUS_GET_CPUS(). 4042 * 4043 * This simple implementation of BUS_GET_CPUS() simply calls the 4044 * BUS_GET_CPUS() method of the parent of @p dev. 4045 */ 4046 int 4047 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op, 4048 size_t setsize, cpuset_t *cpuset) 4049 { 4050 /* Propagate up the bus hierarchy until someone handles it. */ 4051 if (dev->parent != NULL) 4052 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset)); 4053 return (EINVAL); 4054 } 4055 4056 /** 4057 * @brief Helper function for implementing BUS_GET_DMA_TAG(). 4058 * 4059 * This simple implementation of BUS_GET_DMA_TAG() simply calls the 4060 * BUS_GET_DMA_TAG() method of the parent of @p dev. 4061 */ 4062 bus_dma_tag_t 4063 bus_generic_get_dma_tag(device_t dev, device_t child) 4064 { 4065 /* Propagate up the bus hierarchy until someone handles it. */ 4066 if (dev->parent != NULL) 4067 return (BUS_GET_DMA_TAG(dev->parent, child)); 4068 return (NULL); 4069 } 4070 4071 /** 4072 * @brief Helper function for implementing BUS_GET_BUS_TAG(). 4073 * 4074 * This simple implementation of BUS_GET_BUS_TAG() simply calls the 4075 * BUS_GET_BUS_TAG() method of the parent of @p dev. 4076 */ 4077 bus_space_tag_t 4078 bus_generic_get_bus_tag(device_t dev, device_t child) 4079 { 4080 /* Propagate up the bus hierarchy until someone handles it. */ 4081 if (dev->parent != NULL) 4082 return (BUS_GET_BUS_TAG(dev->parent, child)); 4083 return ((bus_space_tag_t)0); 4084 } 4085 4086 /** 4087 * @brief Helper function for implementing BUS_GET_RESOURCE(). 4088 * 4089 * This implementation of BUS_GET_RESOURCE() uses the 4090 * resource_list_find() function to do most of the work. It calls 4091 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4092 * search. 4093 */ 4094 int 4095 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 4096 rman_res_t *startp, rman_res_t *countp) 4097 { 4098 struct resource_list * rl = NULL; 4099 struct resource_list_entry * rle = NULL; 4100 4101 rl = BUS_GET_RESOURCE_LIST(dev, child); 4102 if (!rl) 4103 return (EINVAL); 4104 4105 rle = resource_list_find(rl, type, rid); 4106 if (!rle) 4107 return (ENOENT); 4108 4109 if (startp) 4110 *startp = rle->start; 4111 if (countp) 4112 *countp = rle->count; 4113 4114 return (0); 4115 } 4116 4117 /** 4118 * @brief Helper function for implementing BUS_SET_RESOURCE(). 4119 * 4120 * This implementation of BUS_SET_RESOURCE() uses the 4121 * resource_list_add() function to do most of the work. It calls 4122 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4123 * edit. 4124 */ 4125 int 4126 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 4127 rman_res_t start, rman_res_t count) 4128 { 4129 struct resource_list * rl = NULL; 4130 4131 rl = BUS_GET_RESOURCE_LIST(dev, child); 4132 if (!rl) 4133 return (EINVAL); 4134 4135 resource_list_add(rl, type, rid, start, (start + count - 1), count); 4136 4137 return (0); 4138 } 4139 4140 /** 4141 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 4142 * 4143 * This implementation of BUS_DELETE_RESOURCE() uses the 4144 * resource_list_delete() function to do most of the work. It calls 4145 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4146 * edit. 4147 */ 4148 void 4149 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 4150 { 4151 struct resource_list * rl = NULL; 4152 4153 rl = BUS_GET_RESOURCE_LIST(dev, child); 4154 if (!rl) 4155 return; 4156 4157 resource_list_delete(rl, type, rid); 4158 4159 return; 4160 } 4161 4162 /** 4163 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4164 * 4165 * This implementation of BUS_RELEASE_RESOURCE() uses the 4166 * resource_list_release() function to do most of the work. It calls 4167 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4168 */ 4169 int 4170 bus_generic_rl_release_resource(device_t dev, device_t child, 4171 struct resource *r) 4172 { 4173 struct resource_list * rl = NULL; 4174 4175 if (device_get_parent(child) != dev) 4176 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, r)); 4177 4178 rl = BUS_GET_RESOURCE_LIST(dev, child); 4179 if (!rl) 4180 return (EINVAL); 4181 4182 return (resource_list_release(rl, dev, child, r)); 4183 } 4184 4185 /** 4186 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4187 * 4188 * This implementation of BUS_ALLOC_RESOURCE() uses the 4189 * resource_list_alloc() function to do most of the work. It calls 4190 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4191 */ 4192 struct resource * 4193 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 4194 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4195 { 4196 struct resource_list * rl = NULL; 4197 4198 if (device_get_parent(child) != dev) 4199 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 4200 type, rid, start, end, count, flags)); 4201 4202 rl = BUS_GET_RESOURCE_LIST(dev, child); 4203 if (!rl) 4204 return (NULL); 4205 4206 return (resource_list_alloc(rl, dev, child, type, rid, 4207 start, end, count, flags)); 4208 } 4209 4210 /** 4211 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4212 * 4213 * This implementation of BUS_ALLOC_RESOURCE() allocates a 4214 * resource from a resource manager. It uses BUS_GET_RMAN() 4215 * to obtain the resource manager. 4216 */ 4217 struct resource * 4218 bus_generic_rman_alloc_resource(device_t dev, device_t child, int type, 4219 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4220 { 4221 struct resource *r; 4222 struct rman *rm; 4223 4224 rm = BUS_GET_RMAN(dev, type, flags); 4225 if (rm == NULL) 4226 return (NULL); 4227 4228 r = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 4229 child); 4230 if (r == NULL) 4231 return (NULL); 4232 rman_set_rid(r, *rid); 4233 rman_set_type(r, type); 4234 4235 if (flags & RF_ACTIVE) { 4236 if (bus_activate_resource(child, type, *rid, r) != 0) { 4237 rman_release_resource(r); 4238 return (NULL); 4239 } 4240 } 4241 4242 return (r); 4243 } 4244 4245 /** 4246 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 4247 * 4248 * This implementation of BUS_ADJUST_RESOURCE() adjusts resources only 4249 * if they were allocated from the resource manager returned by 4250 * BUS_GET_RMAN(). 4251 */ 4252 int 4253 bus_generic_rman_adjust_resource(device_t dev, device_t child, 4254 struct resource *r, rman_res_t start, rman_res_t end) 4255 { 4256 struct rman *rm; 4257 4258 rm = BUS_GET_RMAN(dev, rman_get_type(r), rman_get_flags(r)); 4259 if (rm == NULL) 4260 return (ENXIO); 4261 if (!rman_is_region_manager(r, rm)) 4262 return (EINVAL); 4263 return (rman_adjust_resource(r, start, end)); 4264 } 4265 4266 /** 4267 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4268 * 4269 * This implementation of BUS_RELEASE_RESOURCE() releases resources 4270 * allocated by bus_generic_rman_alloc_resource. 4271 */ 4272 int 4273 bus_generic_rman_release_resource(device_t dev, device_t child, 4274 struct resource *r) 4275 { 4276 #ifdef INVARIANTS 4277 struct rman *rm; 4278 #endif 4279 int error; 4280 4281 #ifdef INVARIANTS 4282 rm = BUS_GET_RMAN(dev, rman_get_type(r), rman_get_flags(r)); 4283 KASSERT(rman_is_region_manager(r, rm), 4284 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4285 #endif 4286 4287 if (rman_get_flags(r) & RF_ACTIVE) { 4288 error = bus_deactivate_resource(child, r); 4289 if (error != 0) 4290 return (error); 4291 } 4292 return (rman_release_resource(r)); 4293 } 4294 4295 /** 4296 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 4297 * 4298 * This implementation of BUS_ACTIVATE_RESOURCE() activates resources 4299 * allocated by bus_generic_rman_alloc_resource. 4300 */ 4301 int 4302 bus_generic_rman_activate_resource(device_t dev, device_t child, 4303 struct resource *r) 4304 { 4305 struct resource_map map; 4306 #ifdef INVARIANTS 4307 struct rman *rm; 4308 #endif 4309 int error, type; 4310 4311 type = rman_get_type(r); 4312 #ifdef INVARIANTS 4313 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r)); 4314 KASSERT(rman_is_region_manager(r, rm), 4315 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4316 #endif 4317 4318 error = rman_activate_resource(r); 4319 if (error != 0) 4320 return (error); 4321 4322 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 4323 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 4324 error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map); 4325 if (error != 0) { 4326 rman_deactivate_resource(r); 4327 return (error); 4328 } 4329 4330 rman_set_mapping(r, &map); 4331 } 4332 return (0); 4333 } 4334 4335 /** 4336 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 4337 * 4338 * This implementation of BUS_DEACTIVATE_RESOURCE() deactivates 4339 * resources allocated by bus_generic_rman_alloc_resource. 4340 */ 4341 int 4342 bus_generic_rman_deactivate_resource(device_t dev, device_t child, 4343 struct resource *r) 4344 { 4345 struct resource_map map; 4346 #ifdef INVARIANTS 4347 struct rman *rm; 4348 #endif 4349 int error, type; 4350 4351 type = rman_get_type(r); 4352 #ifdef INVARIANTS 4353 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r)); 4354 KASSERT(rman_is_region_manager(r, rm), 4355 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4356 #endif 4357 4358 error = rman_deactivate_resource(r); 4359 if (error != 0) 4360 return (error); 4361 4362 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 4363 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 4364 rman_get_mapping(r, &map); 4365 BUS_UNMAP_RESOURCE(dev, child, r, &map); 4366 } 4367 return (0); 4368 } 4369 4370 /** 4371 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 4372 * 4373 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 4374 * BUS_CHILD_PRESENT() method of the parent of @p dev. 4375 */ 4376 int 4377 bus_generic_child_present(device_t dev, device_t child) 4378 { 4379 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 4380 } 4381 4382 /** 4383 * @brief Helper function for implementing BUS_GET_DOMAIN(). 4384 * 4385 * This simple implementation of BUS_GET_DOMAIN() calls the 4386 * BUS_GET_DOMAIN() method of the parent of @p dev. If @p dev 4387 * does not have a parent, the function fails with ENOENT. 4388 */ 4389 int 4390 bus_generic_get_domain(device_t dev, device_t child, int *domain) 4391 { 4392 if (dev->parent) 4393 return (BUS_GET_DOMAIN(dev->parent, dev, domain)); 4394 4395 return (ENOENT); 4396 } 4397 4398 /** 4399 * @brief Helper function to implement normal BUS_GET_DEVICE_PATH() 4400 * 4401 * This function knows how to (a) pass the request up the tree if there's 4402 * a parent and (b) Knows how to supply a FreeBSD locator. 4403 * 4404 * @param bus bus in the walk up the tree 4405 * @param child leaf node to print information about 4406 * @param locator BUS_LOCATOR_xxx string for locator 4407 * @param sb Buffer to print information into 4408 */ 4409 int 4410 bus_generic_get_device_path(device_t bus, device_t child, const char *locator, 4411 struct sbuf *sb) 4412 { 4413 int rv = 0; 4414 device_t parent; 4415 4416 /* 4417 * We don't recurse on ACPI since either we know the handle for the 4418 * device or we don't. And if we're in the generic routine, we don't 4419 * have a ACPI override. All other locators build up a path by having 4420 * their parents create a path and then adding the path element for this 4421 * node. That's why we recurse with parent, bus rather than the typical 4422 * parent, child: each spot in the tree is independent of what our child 4423 * will do with this path. 4424 */ 4425 parent = device_get_parent(bus); 4426 if (parent != NULL && strcmp(locator, BUS_LOCATOR_ACPI) != 0) { 4427 rv = BUS_GET_DEVICE_PATH(parent, bus, locator, sb); 4428 } 4429 if (strcmp(locator, BUS_LOCATOR_FREEBSD) == 0) { 4430 if (rv == 0) { 4431 sbuf_printf(sb, "/%s", device_get_nameunit(child)); 4432 } 4433 return (rv); 4434 } 4435 /* 4436 * Don't know what to do. So assume we do nothing. Not sure that's 4437 * the right thing, but keeps us from having a big list here. 4438 */ 4439 return (0); 4440 } 4441 4442 4443 /** 4444 * @brief Helper function for implementing BUS_RESCAN(). 4445 * 4446 * This null implementation of BUS_RESCAN() always fails to indicate 4447 * the bus does not support rescanning. 4448 */ 4449 int 4450 bus_null_rescan(device_t dev) 4451 { 4452 return (ENODEV); 4453 } 4454 4455 /* 4456 * Some convenience functions to make it easier for drivers to use the 4457 * resource-management functions. All these really do is hide the 4458 * indirection through the parent's method table, making for slightly 4459 * less-wordy code. In the future, it might make sense for this code 4460 * to maintain some sort of a list of resources allocated by each device. 4461 */ 4462 4463 int 4464 bus_alloc_resources(device_t dev, struct resource_spec *rs, 4465 struct resource **res) 4466 { 4467 int i; 4468 4469 for (i = 0; rs[i].type != -1; i++) 4470 res[i] = NULL; 4471 for (i = 0; rs[i].type != -1; i++) { 4472 res[i] = bus_alloc_resource_any(dev, 4473 rs[i].type, &rs[i].rid, rs[i].flags); 4474 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 4475 bus_release_resources(dev, rs, res); 4476 return (ENXIO); 4477 } 4478 } 4479 return (0); 4480 } 4481 4482 void 4483 bus_release_resources(device_t dev, const struct resource_spec *rs, 4484 struct resource **res) 4485 { 4486 int i; 4487 4488 for (i = 0; rs[i].type != -1; i++) 4489 if (res[i] != NULL) { 4490 bus_release_resource( 4491 dev, rs[i].type, rs[i].rid, res[i]); 4492 res[i] = NULL; 4493 } 4494 } 4495 4496 /** 4497 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 4498 * 4499 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 4500 * parent of @p dev. 4501 */ 4502 struct resource * 4503 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, 4504 rman_res_t end, rman_res_t count, u_int flags) 4505 { 4506 struct resource *res; 4507 4508 if (dev->parent == NULL) 4509 return (NULL); 4510 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 4511 count, flags); 4512 return (res); 4513 } 4514 4515 /** 4516 * @brief Wrapper function for BUS_ADJUST_RESOURCE(). 4517 * 4518 * This function simply calls the BUS_ADJUST_RESOURCE() method of the 4519 * parent of @p dev. 4520 */ 4521 int 4522 bus_adjust_resource(device_t dev, struct resource *r, rman_res_t start, 4523 rman_res_t end) 4524 { 4525 if (dev->parent == NULL) 4526 return (EINVAL); 4527 return (BUS_ADJUST_RESOURCE(dev->parent, dev, r, start, end)); 4528 } 4529 4530 int 4531 bus_adjust_resource_old(device_t dev, int type __unused, struct resource *r, 4532 rman_res_t start, rman_res_t end) 4533 { 4534 return (bus_adjust_resource(dev, r, start, end)); 4535 } 4536 4537 /** 4538 * @brief Wrapper function for BUS_TRANSLATE_RESOURCE(). 4539 * 4540 * This function simply calls the BUS_TRANSLATE_RESOURCE() method of the 4541 * parent of @p dev. 4542 */ 4543 int 4544 bus_translate_resource(device_t dev, int type, rman_res_t start, 4545 rman_res_t *newstart) 4546 { 4547 if (dev->parent == NULL) 4548 return (EINVAL); 4549 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, newstart)); 4550 } 4551 4552 /** 4553 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 4554 * 4555 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 4556 * parent of @p dev. 4557 */ 4558 int 4559 bus_activate_resource(device_t dev, struct resource *r) 4560 { 4561 if (dev->parent == NULL) 4562 return (EINVAL); 4563 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, r)); 4564 } 4565 4566 int 4567 bus_activate_resource_old(device_t dev, int type, int rid, struct resource *r) 4568 { 4569 return (bus_activate_resource(dev, r)); 4570 } 4571 4572 /** 4573 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 4574 * 4575 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 4576 * parent of @p dev. 4577 */ 4578 int 4579 bus_deactivate_resource(device_t dev, struct resource *r) 4580 { 4581 if (dev->parent == NULL) 4582 return (EINVAL); 4583 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, r)); 4584 } 4585 4586 int 4587 bus_deactivate_resource_old(device_t dev, int type, int rid, struct resource *r) 4588 { 4589 return (bus_deactivate_resource(dev, r)); 4590 } 4591 4592 /** 4593 * @brief Wrapper function for BUS_MAP_RESOURCE(). 4594 * 4595 * This function simply calls the BUS_MAP_RESOURCE() method of the 4596 * parent of @p dev. 4597 */ 4598 int 4599 bus_map_resource(device_t dev, struct resource *r, 4600 struct resource_map_request *args, struct resource_map *map) 4601 { 4602 if (dev->parent == NULL) 4603 return (EINVAL); 4604 return (BUS_MAP_RESOURCE(dev->parent, dev, r, args, map)); 4605 } 4606 4607 int 4608 bus_map_resource_old(device_t dev, int type, struct resource *r, 4609 struct resource_map_request *args, struct resource_map *map) 4610 { 4611 return (bus_map_resource(dev, r, args, map)); 4612 } 4613 4614 /** 4615 * @brief Wrapper function for BUS_UNMAP_RESOURCE(). 4616 * 4617 * This function simply calls the BUS_UNMAP_RESOURCE() method of the 4618 * parent of @p dev. 4619 */ 4620 int 4621 bus_unmap_resource(device_t dev, struct resource *r, struct resource_map *map) 4622 { 4623 if (dev->parent == NULL) 4624 return (EINVAL); 4625 return (BUS_UNMAP_RESOURCE(dev->parent, dev, r, map)); 4626 } 4627 4628 int 4629 bus_unmap_resource_old(device_t dev, int type, struct resource *r, 4630 struct resource_map *map) 4631 { 4632 return (bus_unmap_resource(dev, r, map)); 4633 } 4634 4635 /** 4636 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 4637 * 4638 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 4639 * parent of @p dev. 4640 */ 4641 int 4642 bus_release_resource(device_t dev, struct resource *r) 4643 { 4644 int rv; 4645 4646 if (dev->parent == NULL) 4647 return (EINVAL); 4648 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, r); 4649 return (rv); 4650 } 4651 4652 int 4653 bus_release_resource_old(device_t dev, int type, int rid, struct resource *r) 4654 { 4655 return (bus_release_resource(dev, r)); 4656 } 4657 4658 /** 4659 * @brief Wrapper function for BUS_SETUP_INTR(). 4660 * 4661 * This function simply calls the BUS_SETUP_INTR() method of the 4662 * parent of @p dev. 4663 */ 4664 int 4665 bus_setup_intr(device_t dev, struct resource *r, int flags, 4666 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) 4667 { 4668 int error; 4669 4670 if (dev->parent == NULL) 4671 return (EINVAL); 4672 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler, 4673 arg, cookiep); 4674 if (error != 0) 4675 return (error); 4676 if (handler != NULL && !(flags & INTR_MPSAFE)) 4677 device_printf(dev, "[GIANT-LOCKED]\n"); 4678 return (0); 4679 } 4680 4681 /** 4682 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 4683 * 4684 * This function simply calls the BUS_TEARDOWN_INTR() method of the 4685 * parent of @p dev. 4686 */ 4687 int 4688 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 4689 { 4690 if (dev->parent == NULL) 4691 return (EINVAL); 4692 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 4693 } 4694 4695 /** 4696 * @brief Wrapper function for BUS_SUSPEND_INTR(). 4697 * 4698 * This function simply calls the BUS_SUSPEND_INTR() method of the 4699 * parent of @p dev. 4700 */ 4701 int 4702 bus_suspend_intr(device_t dev, struct resource *r) 4703 { 4704 if (dev->parent == NULL) 4705 return (EINVAL); 4706 return (BUS_SUSPEND_INTR(dev->parent, dev, r)); 4707 } 4708 4709 /** 4710 * @brief Wrapper function for BUS_RESUME_INTR(). 4711 * 4712 * This function simply calls the BUS_RESUME_INTR() method of the 4713 * parent of @p dev. 4714 */ 4715 int 4716 bus_resume_intr(device_t dev, struct resource *r) 4717 { 4718 if (dev->parent == NULL) 4719 return (EINVAL); 4720 return (BUS_RESUME_INTR(dev->parent, dev, r)); 4721 } 4722 4723 /** 4724 * @brief Wrapper function for BUS_BIND_INTR(). 4725 * 4726 * This function simply calls the BUS_BIND_INTR() method of the 4727 * parent of @p dev. 4728 */ 4729 int 4730 bus_bind_intr(device_t dev, struct resource *r, int cpu) 4731 { 4732 if (dev->parent == NULL) 4733 return (EINVAL); 4734 return (BUS_BIND_INTR(dev->parent, dev, r, cpu)); 4735 } 4736 4737 /** 4738 * @brief Wrapper function for BUS_DESCRIBE_INTR(). 4739 * 4740 * This function first formats the requested description into a 4741 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of 4742 * the parent of @p dev. 4743 */ 4744 int 4745 bus_describe_intr(device_t dev, struct resource *irq, void *cookie, 4746 const char *fmt, ...) 4747 { 4748 va_list ap; 4749 char descr[MAXCOMLEN + 1]; 4750 4751 if (dev->parent == NULL) 4752 return (EINVAL); 4753 va_start(ap, fmt); 4754 vsnprintf(descr, sizeof(descr), fmt, ap); 4755 va_end(ap); 4756 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr)); 4757 } 4758 4759 /** 4760 * @brief Wrapper function for BUS_SET_RESOURCE(). 4761 * 4762 * This function simply calls the BUS_SET_RESOURCE() method of the 4763 * parent of @p dev. 4764 */ 4765 int 4766 bus_set_resource(device_t dev, int type, int rid, 4767 rman_res_t start, rman_res_t count) 4768 { 4769 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 4770 start, count)); 4771 } 4772 4773 /** 4774 * @brief Wrapper function for BUS_GET_RESOURCE(). 4775 * 4776 * This function simply calls the BUS_GET_RESOURCE() method of the 4777 * parent of @p dev. 4778 */ 4779 int 4780 bus_get_resource(device_t dev, int type, int rid, 4781 rman_res_t *startp, rman_res_t *countp) 4782 { 4783 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4784 startp, countp)); 4785 } 4786 4787 /** 4788 * @brief Wrapper function for BUS_GET_RESOURCE(). 4789 * 4790 * This function simply calls the BUS_GET_RESOURCE() method of the 4791 * parent of @p dev and returns the start value. 4792 */ 4793 rman_res_t 4794 bus_get_resource_start(device_t dev, int type, int rid) 4795 { 4796 rman_res_t start; 4797 rman_res_t count; 4798 int error; 4799 4800 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4801 &start, &count); 4802 if (error) 4803 return (0); 4804 return (start); 4805 } 4806 4807 /** 4808 * @brief Wrapper function for BUS_GET_RESOURCE(). 4809 * 4810 * This function simply calls the BUS_GET_RESOURCE() method of the 4811 * parent of @p dev and returns the count value. 4812 */ 4813 rman_res_t 4814 bus_get_resource_count(device_t dev, int type, int rid) 4815 { 4816 rman_res_t start; 4817 rman_res_t count; 4818 int error; 4819 4820 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4821 &start, &count); 4822 if (error) 4823 return (0); 4824 return (count); 4825 } 4826 4827 /** 4828 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 4829 * 4830 * This function simply calls the BUS_DELETE_RESOURCE() method of the 4831 * parent of @p dev. 4832 */ 4833 void 4834 bus_delete_resource(device_t dev, int type, int rid) 4835 { 4836 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 4837 } 4838 4839 /** 4840 * @brief Wrapper function for BUS_CHILD_PRESENT(). 4841 * 4842 * This function simply calls the BUS_CHILD_PRESENT() method of the 4843 * parent of @p dev. 4844 */ 4845 int 4846 bus_child_present(device_t child) 4847 { 4848 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 4849 } 4850 4851 /** 4852 * @brief Wrapper function for BUS_CHILD_PNPINFO(). 4853 * 4854 * This function simply calls the BUS_CHILD_PNPINFO() method of the parent of @p 4855 * dev. 4856 */ 4857 int 4858 bus_child_pnpinfo(device_t child, struct sbuf *sb) 4859 { 4860 device_t parent; 4861 4862 parent = device_get_parent(child); 4863 if (parent == NULL) 4864 return (0); 4865 return (BUS_CHILD_PNPINFO(parent, child, sb)); 4866 } 4867 4868 /** 4869 * @brief Generic implementation that does nothing for bus_child_pnpinfo 4870 * 4871 * This function has the right signature and returns 0 since the sbuf is passed 4872 * to us to append to. 4873 */ 4874 int 4875 bus_generic_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb) 4876 { 4877 return (0); 4878 } 4879 4880 /** 4881 * @brief Wrapper function for BUS_CHILD_LOCATION(). 4882 * 4883 * This function simply calls the BUS_CHILD_LOCATION() method of the parent of 4884 * @p dev. 4885 */ 4886 int 4887 bus_child_location(device_t child, struct sbuf *sb) 4888 { 4889 device_t parent; 4890 4891 parent = device_get_parent(child); 4892 if (parent == NULL) 4893 return (0); 4894 return (BUS_CHILD_LOCATION(parent, child, sb)); 4895 } 4896 4897 /** 4898 * @brief Generic implementation that does nothing for bus_child_location 4899 * 4900 * This function has the right signature and returns 0 since the sbuf is passed 4901 * to us to append to. 4902 */ 4903 int 4904 bus_generic_child_location(device_t dev, device_t child, struct sbuf *sb) 4905 { 4906 return (0); 4907 } 4908 4909 /** 4910 * @brief Wrapper function for BUS_GET_CPUS(). 4911 * 4912 * This function simply calls the BUS_GET_CPUS() method of the 4913 * parent of @p dev. 4914 */ 4915 int 4916 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset) 4917 { 4918 device_t parent; 4919 4920 parent = device_get_parent(dev); 4921 if (parent == NULL) 4922 return (EINVAL); 4923 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset)); 4924 } 4925 4926 /** 4927 * @brief Wrapper function for BUS_GET_DMA_TAG(). 4928 * 4929 * This function simply calls the BUS_GET_DMA_TAG() method of the 4930 * parent of @p dev. 4931 */ 4932 bus_dma_tag_t 4933 bus_get_dma_tag(device_t dev) 4934 { 4935 device_t parent; 4936 4937 parent = device_get_parent(dev); 4938 if (parent == NULL) 4939 return (NULL); 4940 return (BUS_GET_DMA_TAG(parent, dev)); 4941 } 4942 4943 /** 4944 * @brief Wrapper function for BUS_GET_BUS_TAG(). 4945 * 4946 * This function simply calls the BUS_GET_BUS_TAG() method of the 4947 * parent of @p dev. 4948 */ 4949 bus_space_tag_t 4950 bus_get_bus_tag(device_t dev) 4951 { 4952 device_t parent; 4953 4954 parent = device_get_parent(dev); 4955 if (parent == NULL) 4956 return ((bus_space_tag_t)0); 4957 return (BUS_GET_BUS_TAG(parent, dev)); 4958 } 4959 4960 /** 4961 * @brief Wrapper function for BUS_GET_DOMAIN(). 4962 * 4963 * This function simply calls the BUS_GET_DOMAIN() method of the 4964 * parent of @p dev. 4965 */ 4966 int 4967 bus_get_domain(device_t dev, int *domain) 4968 { 4969 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain)); 4970 } 4971 4972 /* Resume all devices and then notify userland that we're up again. */ 4973 static int 4974 root_resume(device_t dev) 4975 { 4976 int error; 4977 4978 error = bus_generic_resume(dev); 4979 if (error == 0) { 4980 devctl_notify("kernel", "power", "resume", NULL); 4981 } 4982 return (error); 4983 } 4984 4985 static int 4986 root_print_child(device_t dev, device_t child) 4987 { 4988 int retval = 0; 4989 4990 retval += bus_print_child_header(dev, child); 4991 retval += printf("\n"); 4992 4993 return (retval); 4994 } 4995 4996 static int 4997 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 4998 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 4999 { 5000 /* 5001 * If an interrupt mapping gets to here something bad has happened. 5002 */ 5003 panic("root_setup_intr"); 5004 } 5005 5006 /* 5007 * If we get here, assume that the device is permanent and really is 5008 * present in the system. Removable bus drivers are expected to intercept 5009 * this call long before it gets here. We return -1 so that drivers that 5010 * really care can check vs -1 or some ERRNO returned higher in the food 5011 * chain. 5012 */ 5013 static int 5014 root_child_present(device_t dev, device_t child) 5015 { 5016 return (-1); 5017 } 5018 5019 static int 5020 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize, 5021 cpuset_t *cpuset) 5022 { 5023 switch (op) { 5024 case INTR_CPUS: 5025 /* Default to returning the set of all CPUs. */ 5026 if (setsize != sizeof(cpuset_t)) 5027 return (EINVAL); 5028 *cpuset = all_cpus; 5029 return (0); 5030 default: 5031 return (EINVAL); 5032 } 5033 } 5034 5035 static kobj_method_t root_methods[] = { 5036 /* Device interface */ 5037 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 5038 KOBJMETHOD(device_suspend, bus_generic_suspend), 5039 KOBJMETHOD(device_resume, root_resume), 5040 5041 /* Bus interface */ 5042 KOBJMETHOD(bus_print_child, root_print_child), 5043 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 5044 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 5045 KOBJMETHOD(bus_setup_intr, root_setup_intr), 5046 KOBJMETHOD(bus_child_present, root_child_present), 5047 KOBJMETHOD(bus_get_cpus, root_get_cpus), 5048 5049 KOBJMETHOD_END 5050 }; 5051 5052 static driver_t root_driver = { 5053 "root", 5054 root_methods, 5055 1, /* no softc */ 5056 }; 5057 5058 device_t root_bus; 5059 devclass_t root_devclass; 5060 5061 static int 5062 root_bus_module_handler(module_t mod, int what, void* arg) 5063 { 5064 switch (what) { 5065 case MOD_LOAD: 5066 TAILQ_INIT(&bus_data_devices); 5067 kobj_class_compile((kobj_class_t) &root_driver); 5068 root_bus = make_device(NULL, "root", 0); 5069 root_bus->desc = "System root bus"; 5070 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 5071 root_bus->driver = &root_driver; 5072 root_bus->state = DS_ATTACHED; 5073 root_devclass = devclass_find_internal("root", NULL, FALSE); 5074 devctl2_init(); 5075 return (0); 5076 5077 case MOD_SHUTDOWN: 5078 device_shutdown(root_bus); 5079 return (0); 5080 default: 5081 return (EOPNOTSUPP); 5082 } 5083 5084 return (0); 5085 } 5086 5087 static moduledata_t root_bus_mod = { 5088 "rootbus", 5089 root_bus_module_handler, 5090 NULL 5091 }; 5092 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 5093 5094 /** 5095 * @brief Automatically configure devices 5096 * 5097 * This function begins the autoconfiguration process by calling 5098 * device_probe_and_attach() for each child of the @c root0 device. 5099 */ 5100 void 5101 root_bus_configure(void) 5102 { 5103 PDEBUG((".")); 5104 5105 /* Eventually this will be split up, but this is sufficient for now. */ 5106 bus_set_pass(BUS_PASS_DEFAULT); 5107 } 5108 5109 /** 5110 * @brief Module handler for registering device drivers 5111 * 5112 * This module handler is used to automatically register device 5113 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 5114 * devclass_add_driver() for the driver described by the 5115 * driver_module_data structure pointed to by @p arg 5116 */ 5117 int 5118 driver_module_handler(module_t mod, int what, void *arg) 5119 { 5120 struct driver_module_data *dmd; 5121 devclass_t bus_devclass; 5122 kobj_class_t driver; 5123 int error, pass; 5124 5125 dmd = (struct driver_module_data *)arg; 5126 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 5127 error = 0; 5128 5129 switch (what) { 5130 case MOD_LOAD: 5131 if (dmd->dmd_chainevh) 5132 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5133 5134 pass = dmd->dmd_pass; 5135 driver = dmd->dmd_driver; 5136 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 5137 DRIVERNAME(driver), dmd->dmd_busname, pass)); 5138 error = devclass_add_driver(bus_devclass, driver, pass, 5139 dmd->dmd_devclass); 5140 break; 5141 5142 case MOD_UNLOAD: 5143 PDEBUG(("Unloading module: driver %s from bus %s", 5144 DRIVERNAME(dmd->dmd_driver), 5145 dmd->dmd_busname)); 5146 error = devclass_delete_driver(bus_devclass, 5147 dmd->dmd_driver); 5148 5149 if (!error && dmd->dmd_chainevh) 5150 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5151 break; 5152 case MOD_QUIESCE: 5153 PDEBUG(("Quiesce module: driver %s from bus %s", 5154 DRIVERNAME(dmd->dmd_driver), 5155 dmd->dmd_busname)); 5156 error = devclass_quiesce_driver(bus_devclass, 5157 dmd->dmd_driver); 5158 5159 if (!error && dmd->dmd_chainevh) 5160 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5161 break; 5162 default: 5163 error = EOPNOTSUPP; 5164 break; 5165 } 5166 5167 return (error); 5168 } 5169 5170 /** 5171 * @brief Enumerate all hinted devices for this bus. 5172 * 5173 * Walks through the hints for this bus and calls the bus_hinted_child 5174 * routine for each one it fines. It searches first for the specific 5175 * bus that's being probed for hinted children (eg isa0), and then for 5176 * generic children (eg isa). 5177 * 5178 * @param dev bus device to enumerate 5179 */ 5180 void 5181 bus_enumerate_hinted_children(device_t bus) 5182 { 5183 int i; 5184 const char *dname, *busname; 5185 int dunit; 5186 5187 /* 5188 * enumerate all devices on the specific bus 5189 */ 5190 busname = device_get_nameunit(bus); 5191 i = 0; 5192 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5193 BUS_HINTED_CHILD(bus, dname, dunit); 5194 5195 /* 5196 * and all the generic ones. 5197 */ 5198 busname = device_get_name(bus); 5199 i = 0; 5200 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5201 BUS_HINTED_CHILD(bus, dname, dunit); 5202 } 5203 5204 #ifdef BUS_DEBUG 5205 5206 /* the _short versions avoid iteration by not calling anything that prints 5207 * more than oneliners. I love oneliners. 5208 */ 5209 5210 static void 5211 print_device_short(device_t dev, int indent) 5212 { 5213 if (!dev) 5214 return; 5215 5216 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 5217 dev->unit, dev->desc, 5218 (dev->parent? "":"no "), 5219 (TAILQ_EMPTY(&dev->children)? "no ":""), 5220 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 5221 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 5222 (dev->flags&DF_WILDCARD? "wildcard,":""), 5223 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 5224 (dev->flags&DF_SUSPENDED? "suspended,":""), 5225 (dev->ivars? "":"no "), 5226 (dev->softc? "":"no "), 5227 dev->busy)); 5228 } 5229 5230 static void 5231 print_device(device_t dev, int indent) 5232 { 5233 if (!dev) 5234 return; 5235 5236 print_device_short(dev, indent); 5237 5238 indentprintf(("Parent:\n")); 5239 print_device_short(dev->parent, indent+1); 5240 indentprintf(("Driver:\n")); 5241 print_driver_short(dev->driver, indent+1); 5242 indentprintf(("Devclass:\n")); 5243 print_devclass_short(dev->devclass, indent+1); 5244 } 5245 5246 void 5247 print_device_tree_short(device_t dev, int indent) 5248 /* print the device and all its children (indented) */ 5249 { 5250 device_t child; 5251 5252 if (!dev) 5253 return; 5254 5255 print_device_short(dev, indent); 5256 5257 TAILQ_FOREACH(child, &dev->children, link) { 5258 print_device_tree_short(child, indent+1); 5259 } 5260 } 5261 5262 void 5263 print_device_tree(device_t dev, int indent) 5264 /* print the device and all its children (indented) */ 5265 { 5266 device_t child; 5267 5268 if (!dev) 5269 return; 5270 5271 print_device(dev, indent); 5272 5273 TAILQ_FOREACH(child, &dev->children, link) { 5274 print_device_tree(child, indent+1); 5275 } 5276 } 5277 5278 static void 5279 print_driver_short(driver_t *driver, int indent) 5280 { 5281 if (!driver) 5282 return; 5283 5284 indentprintf(("driver %s: softc size = %zd\n", 5285 driver->name, driver->size)); 5286 } 5287 5288 static void 5289 print_driver(driver_t *driver, int indent) 5290 { 5291 if (!driver) 5292 return; 5293 5294 print_driver_short(driver, indent); 5295 } 5296 5297 static void 5298 print_driver_list(driver_list_t drivers, int indent) 5299 { 5300 driverlink_t driver; 5301 5302 TAILQ_FOREACH(driver, &drivers, link) { 5303 print_driver(driver->driver, indent); 5304 } 5305 } 5306 5307 static void 5308 print_devclass_short(devclass_t dc, int indent) 5309 { 5310 if ( !dc ) 5311 return; 5312 5313 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 5314 } 5315 5316 static void 5317 print_devclass(devclass_t dc, int indent) 5318 { 5319 int i; 5320 5321 if ( !dc ) 5322 return; 5323 5324 print_devclass_short(dc, indent); 5325 indentprintf(("Drivers:\n")); 5326 print_driver_list(dc->drivers, indent+1); 5327 5328 indentprintf(("Devices:\n")); 5329 for (i = 0; i < dc->maxunit; i++) 5330 if (dc->devices[i]) 5331 print_device(dc->devices[i], indent+1); 5332 } 5333 5334 void 5335 print_devclass_list_short(void) 5336 { 5337 devclass_t dc; 5338 5339 printf("Short listing of devclasses, drivers & devices:\n"); 5340 TAILQ_FOREACH(dc, &devclasses, link) { 5341 print_devclass_short(dc, 0); 5342 } 5343 } 5344 5345 void 5346 print_devclass_list(void) 5347 { 5348 devclass_t dc; 5349 5350 printf("Full listing of devclasses, drivers & devices:\n"); 5351 TAILQ_FOREACH(dc, &devclasses, link) { 5352 print_devclass(dc, 0); 5353 } 5354 } 5355 5356 #endif 5357 5358 /* 5359 * User-space access to the device tree. 5360 * 5361 * We implement a small set of nodes: 5362 * 5363 * hw.bus Single integer read method to obtain the 5364 * current generation count. 5365 * hw.bus.devices Reads the entire device tree in flat space. 5366 * hw.bus.rman Resource manager interface 5367 * 5368 * We might like to add the ability to scan devclasses and/or drivers to 5369 * determine what else is currently loaded/available. 5370 */ 5371 5372 static int 5373 sysctl_bus_info(SYSCTL_HANDLER_ARGS) 5374 { 5375 struct u_businfo ubus; 5376 5377 ubus.ub_version = BUS_USER_VERSION; 5378 ubus.ub_generation = bus_data_generation; 5379 5380 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 5381 } 5382 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD | 5383 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo", 5384 "bus-related data"); 5385 5386 static int 5387 sysctl_devices(SYSCTL_HANDLER_ARGS) 5388 { 5389 struct sbuf sb; 5390 int *name = (int *)arg1; 5391 u_int namelen = arg2; 5392 int index; 5393 device_t dev; 5394 struct u_device *udev; 5395 int error; 5396 5397 if (namelen != 2) 5398 return (EINVAL); 5399 5400 if (bus_data_generation_check(name[0])) 5401 return (EINVAL); 5402 5403 index = name[1]; 5404 5405 /* 5406 * Scan the list of devices, looking for the requested index. 5407 */ 5408 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5409 if (index-- == 0) 5410 break; 5411 } 5412 if (dev == NULL) 5413 return (ENOENT); 5414 5415 /* 5416 * Populate the return item, careful not to overflow the buffer. 5417 */ 5418 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO); 5419 udev->dv_handle = (uintptr_t)dev; 5420 udev->dv_parent = (uintptr_t)dev->parent; 5421 udev->dv_devflags = dev->devflags; 5422 udev->dv_flags = dev->flags; 5423 udev->dv_state = dev->state; 5424 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN); 5425 if (dev->nameunit != NULL) 5426 sbuf_cat(&sb, dev->nameunit); 5427 sbuf_putc(&sb, '\0'); 5428 if (dev->desc != NULL) 5429 sbuf_cat(&sb, dev->desc); 5430 sbuf_putc(&sb, '\0'); 5431 if (dev->driver != NULL) 5432 sbuf_cat(&sb, dev->driver->name); 5433 sbuf_putc(&sb, '\0'); 5434 bus_child_pnpinfo(dev, &sb); 5435 sbuf_putc(&sb, '\0'); 5436 bus_child_location(dev, &sb); 5437 sbuf_putc(&sb, '\0'); 5438 error = sbuf_finish(&sb); 5439 if (error == 0) 5440 error = SYSCTL_OUT(req, udev, sizeof(*udev)); 5441 sbuf_delete(&sb); 5442 free(udev, M_BUS); 5443 return (error); 5444 } 5445 5446 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, 5447 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices, 5448 "system device tree"); 5449 5450 int 5451 bus_data_generation_check(int generation) 5452 { 5453 if (generation != bus_data_generation) 5454 return (1); 5455 5456 /* XXX generate optimised lists here? */ 5457 return (0); 5458 } 5459 5460 void 5461 bus_data_generation_update(void) 5462 { 5463 atomic_add_int(&bus_data_generation, 1); 5464 } 5465 5466 int 5467 bus_free_resource(device_t dev, int type, struct resource *r) 5468 { 5469 if (r == NULL) 5470 return (0); 5471 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 5472 } 5473 5474 device_t 5475 device_lookup_by_name(const char *name) 5476 { 5477 device_t dev; 5478 5479 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5480 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0) 5481 return (dev); 5482 } 5483 return (NULL); 5484 } 5485 5486 /* 5487 * /dev/devctl2 implementation. The existing /dev/devctl device has 5488 * implicit semantics on open, so it could not be reused for this. 5489 * Another option would be to call this /dev/bus? 5490 */ 5491 static int 5492 find_device(struct devreq *req, device_t *devp) 5493 { 5494 device_t dev; 5495 5496 /* 5497 * First, ensure that the name is nul terminated. 5498 */ 5499 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL) 5500 return (EINVAL); 5501 5502 /* 5503 * Second, try to find an attached device whose name matches 5504 * 'name'. 5505 */ 5506 dev = device_lookup_by_name(req->dr_name); 5507 if (dev != NULL) { 5508 *devp = dev; 5509 return (0); 5510 } 5511 5512 /* Finally, give device enumerators a chance. */ 5513 dev = NULL; 5514 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev); 5515 if (dev == NULL) 5516 return (ENOENT); 5517 *devp = dev; 5518 return (0); 5519 } 5520 5521 static bool 5522 driver_exists(device_t bus, const char *driver) 5523 { 5524 devclass_t dc; 5525 5526 for (dc = bus->devclass; dc != NULL; dc = dc->parent) { 5527 if (devclass_find_driver_internal(dc, driver) != NULL) 5528 return (true); 5529 } 5530 return (false); 5531 } 5532 5533 static void 5534 device_gen_nomatch(device_t dev) 5535 { 5536 device_t child; 5537 5538 if (dev->flags & DF_NEEDNOMATCH && 5539 dev->state == DS_NOTPRESENT) { 5540 device_handle_nomatch(dev); 5541 } 5542 dev->flags &= ~DF_NEEDNOMATCH; 5543 TAILQ_FOREACH(child, &dev->children, link) { 5544 device_gen_nomatch(child); 5545 } 5546 } 5547 5548 static void 5549 device_do_deferred_actions(void) 5550 { 5551 devclass_t dc; 5552 driverlink_t dl; 5553 5554 /* 5555 * Walk through the devclasses to find all the drivers we've tagged as 5556 * deferred during the freeze and call the driver added routines. They 5557 * have already been added to the lists in the background, so the driver 5558 * added routines that trigger a probe will have all the right bidders 5559 * for the probe auction. 5560 */ 5561 TAILQ_FOREACH(dc, &devclasses, link) { 5562 TAILQ_FOREACH(dl, &dc->drivers, link) { 5563 if (dl->flags & DL_DEFERRED_PROBE) { 5564 devclass_driver_added(dc, dl->driver); 5565 dl->flags &= ~DL_DEFERRED_PROBE; 5566 } 5567 } 5568 } 5569 5570 /* 5571 * We also defer no-match events during a freeze. Walk the tree and 5572 * generate all the pent-up events that are still relevant. 5573 */ 5574 device_gen_nomatch(root_bus); 5575 bus_data_generation_update(); 5576 } 5577 5578 static int 5579 device_get_path(device_t dev, const char *locator, struct sbuf *sb) 5580 { 5581 device_t parent; 5582 int error; 5583 5584 KASSERT(sb != NULL, ("sb is NULL")); 5585 parent = device_get_parent(dev); 5586 if (parent == NULL) { 5587 error = sbuf_putc(sb, '/'); 5588 } else { 5589 error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb); 5590 if (error == 0) { 5591 error = sbuf_error(sb); 5592 if (error == 0 && sbuf_len(sb) <= 1) 5593 error = EIO; 5594 } 5595 } 5596 sbuf_finish(sb); 5597 return (error); 5598 } 5599 5600 static int 5601 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, 5602 struct thread *td) 5603 { 5604 struct devreq *req; 5605 device_t dev; 5606 int error, old; 5607 5608 /* Locate the device to control. */ 5609 bus_topo_lock(); 5610 req = (struct devreq *)data; 5611 switch (cmd) { 5612 case DEV_ATTACH: 5613 case DEV_DETACH: 5614 case DEV_ENABLE: 5615 case DEV_DISABLE: 5616 case DEV_SUSPEND: 5617 case DEV_RESUME: 5618 case DEV_SET_DRIVER: 5619 case DEV_CLEAR_DRIVER: 5620 case DEV_RESCAN: 5621 case DEV_DELETE: 5622 case DEV_RESET: 5623 error = priv_check(td, PRIV_DRIVER); 5624 if (error == 0) 5625 error = find_device(req, &dev); 5626 break; 5627 case DEV_FREEZE: 5628 case DEV_THAW: 5629 error = priv_check(td, PRIV_DRIVER); 5630 break; 5631 case DEV_GET_PATH: 5632 error = find_device(req, &dev); 5633 break; 5634 default: 5635 error = ENOTTY; 5636 break; 5637 } 5638 if (error) { 5639 bus_topo_unlock(); 5640 return (error); 5641 } 5642 5643 /* Perform the requested operation. */ 5644 switch (cmd) { 5645 case DEV_ATTACH: 5646 if (device_is_attached(dev)) 5647 error = EBUSY; 5648 else if (!device_is_enabled(dev)) 5649 error = ENXIO; 5650 else 5651 error = device_probe_and_attach(dev); 5652 break; 5653 case DEV_DETACH: 5654 if (!device_is_attached(dev)) { 5655 error = ENXIO; 5656 break; 5657 } 5658 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5659 error = device_quiesce(dev); 5660 if (error) 5661 break; 5662 } 5663 error = device_detach(dev); 5664 break; 5665 case DEV_ENABLE: 5666 if (device_is_enabled(dev)) { 5667 error = EBUSY; 5668 break; 5669 } 5670 5671 /* 5672 * If the device has been probed but not attached (e.g. 5673 * when it has been disabled by a loader hint), just 5674 * attach the device rather than doing a full probe. 5675 */ 5676 device_enable(dev); 5677 if (device_is_alive(dev)) { 5678 /* 5679 * If the device was disabled via a hint, clear 5680 * the hint. 5681 */ 5682 if (resource_disabled(dev->driver->name, dev->unit)) 5683 resource_unset_value(dev->driver->name, 5684 dev->unit, "disabled"); 5685 error = device_attach(dev); 5686 } else 5687 error = device_probe_and_attach(dev); 5688 break; 5689 case DEV_DISABLE: 5690 if (!device_is_enabled(dev)) { 5691 error = ENXIO; 5692 break; 5693 } 5694 5695 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5696 error = device_quiesce(dev); 5697 if (error) 5698 break; 5699 } 5700 5701 /* 5702 * Force DF_FIXEDCLASS on around detach to preserve 5703 * the existing name. 5704 */ 5705 old = dev->flags; 5706 dev->flags |= DF_FIXEDCLASS; 5707 error = device_detach(dev); 5708 if (!(old & DF_FIXEDCLASS)) 5709 dev->flags &= ~DF_FIXEDCLASS; 5710 if (error == 0) 5711 device_disable(dev); 5712 break; 5713 case DEV_SUSPEND: 5714 if (device_is_suspended(dev)) { 5715 error = EBUSY; 5716 break; 5717 } 5718 if (device_get_parent(dev) == NULL) { 5719 error = EINVAL; 5720 break; 5721 } 5722 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev); 5723 break; 5724 case DEV_RESUME: 5725 if (!device_is_suspended(dev)) { 5726 error = EINVAL; 5727 break; 5728 } 5729 if (device_get_parent(dev) == NULL) { 5730 error = EINVAL; 5731 break; 5732 } 5733 error = BUS_RESUME_CHILD(device_get_parent(dev), dev); 5734 break; 5735 case DEV_SET_DRIVER: { 5736 devclass_t dc; 5737 char driver[128]; 5738 5739 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL); 5740 if (error) 5741 break; 5742 if (driver[0] == '\0') { 5743 error = EINVAL; 5744 break; 5745 } 5746 if (dev->devclass != NULL && 5747 strcmp(driver, dev->devclass->name) == 0) 5748 /* XXX: Could possibly force DF_FIXEDCLASS on? */ 5749 break; 5750 5751 /* 5752 * Scan drivers for this device's bus looking for at 5753 * least one matching driver. 5754 */ 5755 if (dev->parent == NULL) { 5756 error = EINVAL; 5757 break; 5758 } 5759 if (!driver_exists(dev->parent, driver)) { 5760 error = ENOENT; 5761 break; 5762 } 5763 dc = devclass_create(driver); 5764 if (dc == NULL) { 5765 error = ENOMEM; 5766 break; 5767 } 5768 5769 /* Detach device if necessary. */ 5770 if (device_is_attached(dev)) { 5771 if (req->dr_flags & DEVF_SET_DRIVER_DETACH) 5772 error = device_detach(dev); 5773 else 5774 error = EBUSY; 5775 if (error) 5776 break; 5777 } 5778 5779 /* Clear any previously-fixed device class and unit. */ 5780 if (dev->flags & DF_FIXEDCLASS) 5781 devclass_delete_device(dev->devclass, dev); 5782 dev->flags |= DF_WILDCARD; 5783 dev->unit = DEVICE_UNIT_ANY; 5784 5785 /* Force the new device class. */ 5786 error = devclass_add_device(dc, dev); 5787 if (error) 5788 break; 5789 dev->flags |= DF_FIXEDCLASS; 5790 error = device_probe_and_attach(dev); 5791 break; 5792 } 5793 case DEV_CLEAR_DRIVER: 5794 if (!(dev->flags & DF_FIXEDCLASS)) { 5795 error = 0; 5796 break; 5797 } 5798 if (device_is_attached(dev)) { 5799 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH) 5800 error = device_detach(dev); 5801 else 5802 error = EBUSY; 5803 if (error) 5804 break; 5805 } 5806 5807 dev->flags &= ~DF_FIXEDCLASS; 5808 dev->flags |= DF_WILDCARD; 5809 devclass_delete_device(dev->devclass, dev); 5810 error = device_probe_and_attach(dev); 5811 break; 5812 case DEV_RESCAN: 5813 if (!device_is_attached(dev)) { 5814 error = ENXIO; 5815 break; 5816 } 5817 error = BUS_RESCAN(dev); 5818 break; 5819 case DEV_DELETE: { 5820 device_t parent; 5821 5822 parent = device_get_parent(dev); 5823 if (parent == NULL) { 5824 error = EINVAL; 5825 break; 5826 } 5827 if (!(req->dr_flags & DEVF_FORCE_DELETE)) { 5828 if (bus_child_present(dev) != 0) { 5829 error = EBUSY; 5830 break; 5831 } 5832 } 5833 5834 error = device_delete_child(parent, dev); 5835 break; 5836 } 5837 case DEV_FREEZE: 5838 if (device_frozen) 5839 error = EBUSY; 5840 else 5841 device_frozen = true; 5842 break; 5843 case DEV_THAW: 5844 if (!device_frozen) 5845 error = EBUSY; 5846 else { 5847 device_do_deferred_actions(); 5848 device_frozen = false; 5849 } 5850 break; 5851 case DEV_RESET: 5852 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) { 5853 error = EINVAL; 5854 break; 5855 } 5856 if (device_get_parent(dev) == NULL) { 5857 error = EINVAL; 5858 break; 5859 } 5860 error = BUS_RESET_CHILD(device_get_parent(dev), dev, 5861 req->dr_flags); 5862 break; 5863 case DEV_GET_PATH: { 5864 struct sbuf *sb; 5865 char locator[64]; 5866 ssize_t len; 5867 5868 error = copyinstr(req->dr_buffer.buffer, locator, 5869 sizeof(locator), NULL); 5870 if (error != 0) 5871 break; 5872 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | 5873 SBUF_INCLUDENUL /* | SBUF_WAITOK */); 5874 error = device_get_path(dev, locator, sb); 5875 if (error == 0) { 5876 len = sbuf_len(sb); 5877 if (req->dr_buffer.length < len) { 5878 error = ENAMETOOLONG; 5879 } else { 5880 error = copyout(sbuf_data(sb), 5881 req->dr_buffer.buffer, len); 5882 } 5883 req->dr_buffer.length = len; 5884 } 5885 sbuf_delete(sb); 5886 break; 5887 } 5888 } 5889 bus_topo_unlock(); 5890 return (error); 5891 } 5892 5893 static struct cdevsw devctl2_cdevsw = { 5894 .d_version = D_VERSION, 5895 .d_ioctl = devctl2_ioctl, 5896 .d_name = "devctl2", 5897 }; 5898 5899 static void 5900 devctl2_init(void) 5901 { 5902 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL, 5903 UID_ROOT, GID_WHEEL, 0644, "devctl2"); 5904 } 5905 5906 /* 5907 * For maintaining device 'at' location info to avoid recomputing it 5908 */ 5909 struct device_location_node { 5910 const char *dln_locator; 5911 const char *dln_path; 5912 TAILQ_ENTRY(device_location_node) dln_link; 5913 }; 5914 typedef TAILQ_HEAD(device_location_list, device_location_node) device_location_list_t; 5915 5916 struct device_location_cache { 5917 device_location_list_t dlc_list; 5918 }; 5919 5920 5921 /* 5922 * Location cache for wired devices. 5923 */ 5924 device_location_cache_t * 5925 dev_wired_cache_init(void) 5926 { 5927 device_location_cache_t *dcp; 5928 5929 dcp = malloc(sizeof(*dcp), M_BUS, M_WAITOK | M_ZERO); 5930 TAILQ_INIT(&dcp->dlc_list); 5931 5932 return (dcp); 5933 } 5934 5935 void 5936 dev_wired_cache_fini(device_location_cache_t *dcp) 5937 { 5938 struct device_location_node *dln, *tdln; 5939 5940 TAILQ_FOREACH_SAFE(dln, &dcp->dlc_list, dln_link, tdln) { 5941 free(dln, M_BUS); 5942 } 5943 free(dcp, M_BUS); 5944 } 5945 5946 static struct device_location_node * 5947 dev_wired_cache_lookup(device_location_cache_t *dcp, const char *locator) 5948 { 5949 struct device_location_node *dln; 5950 5951 TAILQ_FOREACH(dln, &dcp->dlc_list, dln_link) { 5952 if (strcmp(locator, dln->dln_locator) == 0) 5953 return (dln); 5954 } 5955 5956 return (NULL); 5957 } 5958 5959 static struct device_location_node * 5960 dev_wired_cache_add(device_location_cache_t *dcp, const char *locator, const char *path) 5961 { 5962 struct device_location_node *dln; 5963 size_t loclen, pathlen; 5964 5965 loclen = strlen(locator) + 1; 5966 pathlen = strlen(path) + 1; 5967 dln = malloc(sizeof(*dln) + loclen + pathlen, M_BUS, M_WAITOK | M_ZERO); 5968 dln->dln_locator = (char *)(dln + 1); 5969 memcpy(__DECONST(char *, dln->dln_locator), locator, loclen); 5970 dln->dln_path = dln->dln_locator + loclen; 5971 memcpy(__DECONST(char *, dln->dln_path), path, pathlen); 5972 TAILQ_INSERT_HEAD(&dcp->dlc_list, dln, dln_link); 5973 5974 return (dln); 5975 } 5976 5977 bool 5978 dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, 5979 const char *at) 5980 { 5981 struct sbuf *sb; 5982 const char *cp; 5983 char locator[32]; 5984 int error, len; 5985 struct device_location_node *res; 5986 5987 cp = strchr(at, ':'); 5988 if (cp == NULL) 5989 return (false); 5990 len = cp - at; 5991 if (len > sizeof(locator) - 1) /* Skip too long locator */ 5992 return (false); 5993 memcpy(locator, at, len); 5994 locator[len] = '\0'; 5995 cp++; 5996 5997 error = 0; 5998 /* maybe cache this inside device_t and look that up, but not yet */ 5999 res = dev_wired_cache_lookup(dcp, locator); 6000 if (res == NULL) { 6001 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | 6002 SBUF_INCLUDENUL | SBUF_NOWAIT); 6003 if (sb != NULL) { 6004 error = device_get_path(dev, locator, sb); 6005 if (error == 0) { 6006 res = dev_wired_cache_add(dcp, locator, 6007 sbuf_data(sb)); 6008 } 6009 sbuf_delete(sb); 6010 } 6011 } 6012 if (error != 0 || res == NULL || res->dln_path == NULL) 6013 return (false); 6014 6015 return (strcmp(res->dln_path, cp) == 0); 6016 } 6017 6018 /* 6019 * APIs to manage deprecation and obsolescence. 6020 */ 6021 static int obsolete_panic = 0; 6022 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0, 6023 "Panic when obsolete features are used (0 = never, 1 = if obsolete, " 6024 "2 = if deprecated)"); 6025 6026 static void 6027 gone_panic(int major, int running, const char *msg) 6028 { 6029 switch (obsolete_panic) 6030 { 6031 case 0: 6032 return; 6033 case 1: 6034 if (running < major) 6035 return; 6036 /* FALLTHROUGH */ 6037 default: 6038 panic("%s", msg); 6039 } 6040 } 6041 6042 void 6043 _gone_in(int major, const char *msg) 6044 { 6045 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 6046 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 6047 printf("Obsolete code will be removed soon: %s\n", msg); 6048 else 6049 printf("Deprecated code (to be removed in FreeBSD %d): %s\n", 6050 major, msg); 6051 } 6052 6053 void 6054 _gone_in_dev(device_t dev, int major, const char *msg) 6055 { 6056 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 6057 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 6058 device_printf(dev, 6059 "Obsolete code will be removed soon: %s\n", msg); 6060 else 6061 device_printf(dev, 6062 "Deprecated code (to be removed in FreeBSD %d): %s\n", 6063 major, msg); 6064 } 6065 6066 #ifdef DDB 6067 DB_SHOW_COMMAND(device, db_show_device) 6068 { 6069 device_t dev; 6070 6071 if (!have_addr) 6072 return; 6073 6074 dev = (device_t)addr; 6075 6076 db_printf("name: %s\n", device_get_nameunit(dev)); 6077 db_printf(" driver: %s\n", DRIVERNAME(dev->driver)); 6078 db_printf(" class: %s\n", DEVCLANAME(dev->devclass)); 6079 db_printf(" addr: %p\n", dev); 6080 db_printf(" parent: %p\n", dev->parent); 6081 db_printf(" softc: %p\n", dev->softc); 6082 db_printf(" ivars: %p\n", dev->ivars); 6083 } 6084 6085 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices) 6086 { 6087 device_t dev; 6088 6089 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 6090 db_show_device((db_expr_t)dev, true, count, modif); 6091 } 6092 } 6093 #endif 6094