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 /* detach parent before deleting children, if any */ 1481 if ((error = device_detach(child)) != 0) 1482 return (error); 1483 1484 /* remove children second */ 1485 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1486 error = device_delete_child(child, grandchild); 1487 if (error) 1488 return (error); 1489 } 1490 1491 if (child->devclass) 1492 devclass_delete_device(child->devclass, child); 1493 if (child->parent) 1494 BUS_CHILD_DELETED(dev, child); 1495 TAILQ_REMOVE(&dev->children, child, link); 1496 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1497 kobj_delete((kobj_t) child, M_BUS); 1498 1499 bus_data_generation_update(); 1500 return (0); 1501 } 1502 1503 /** 1504 * @brief Delete all children devices of the given device, if any. 1505 * 1506 * This function deletes all children devices of the given device, if 1507 * any, using the device_delete_child() function for each device it 1508 * finds. If a child device cannot be deleted, this function will 1509 * return an error code. 1510 * 1511 * @param dev the parent device 1512 * 1513 * @retval 0 success 1514 * @retval non-zero a device would not detach 1515 */ 1516 int 1517 device_delete_children(device_t dev) 1518 { 1519 device_t child; 1520 int error; 1521 1522 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1523 1524 error = 0; 1525 1526 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1527 error = device_delete_child(dev, child); 1528 if (error) { 1529 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1530 break; 1531 } 1532 } 1533 return (error); 1534 } 1535 1536 /** 1537 * @brief Find a device given a unit number 1538 * 1539 * This is similar to devclass_get_devices() but only searches for 1540 * devices which have @p dev as a parent. 1541 * 1542 * @param dev the parent device to search 1543 * @param unit the unit number to search for. If the unit is 1544 * @c DEVICE_UNIT_ANY, return the first child of @p dev 1545 * which has name @p classname (that is, the one with the 1546 * lowest unit.) 1547 * 1548 * @returns the device with the given unit number or @c 1549 * NULL if there is no such device 1550 */ 1551 device_t 1552 device_find_child(device_t dev, const char *classname, int unit) 1553 { 1554 devclass_t dc; 1555 device_t child; 1556 1557 dc = devclass_find(classname); 1558 if (!dc) 1559 return (NULL); 1560 1561 if (unit != DEVICE_UNIT_ANY) { 1562 child = devclass_get_device(dc, unit); 1563 if (child && child->parent == dev) 1564 return (child); 1565 } else { 1566 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1567 child = devclass_get_device(dc, unit); 1568 if (child && child->parent == dev) 1569 return (child); 1570 } 1571 } 1572 return (NULL); 1573 } 1574 1575 /** 1576 * @internal 1577 */ 1578 static driverlink_t 1579 first_matching_driver(devclass_t dc, device_t dev) 1580 { 1581 if (dev->devclass) 1582 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1583 return (TAILQ_FIRST(&dc->drivers)); 1584 } 1585 1586 /** 1587 * @internal 1588 */ 1589 static driverlink_t 1590 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1591 { 1592 if (dev->devclass) { 1593 driverlink_t dl; 1594 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1595 if (!strcmp(dev->devclass->name, dl->driver->name)) 1596 return (dl); 1597 return (NULL); 1598 } 1599 return (TAILQ_NEXT(last, link)); 1600 } 1601 1602 /** 1603 * @internal 1604 */ 1605 int 1606 device_probe_child(device_t dev, device_t child) 1607 { 1608 devclass_t dc; 1609 driverlink_t best = NULL; 1610 driverlink_t dl; 1611 int result, pri = 0; 1612 /* We should preserve the devclass (or lack of) set by the bus. */ 1613 int hasclass = (child->devclass != NULL); 1614 1615 bus_topo_assert(); 1616 1617 dc = dev->devclass; 1618 if (!dc) 1619 panic("device_probe_child: parent device has no devclass"); 1620 1621 /* 1622 * If the state is already probed, then return. 1623 */ 1624 if (child->state == DS_ALIVE) 1625 return (0); 1626 1627 for (; dc; dc = dc->parent) { 1628 for (dl = first_matching_driver(dc, child); 1629 dl; 1630 dl = next_matching_driver(dc, child, dl)) { 1631 /* If this driver's pass is too high, then ignore it. */ 1632 if (dl->pass > bus_current_pass) 1633 continue; 1634 1635 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1636 result = device_set_driver(child, dl->driver); 1637 if (result == ENOMEM) 1638 return (result); 1639 else if (result != 0) 1640 continue; 1641 if (!hasclass) { 1642 if (device_set_devclass(child, 1643 dl->driver->name) != 0) { 1644 char const * devname = 1645 device_get_name(child); 1646 if (devname == NULL) 1647 devname = "(unknown)"; 1648 printf("driver bug: Unable to set " 1649 "devclass (class: %s " 1650 "devname: %s)\n", 1651 dl->driver->name, 1652 devname); 1653 (void)device_set_driver(child, NULL); 1654 continue; 1655 } 1656 } 1657 1658 /* Fetch any flags for the device before probing. */ 1659 resource_int_value(dl->driver->name, child->unit, 1660 "flags", &child->devflags); 1661 1662 result = DEVICE_PROBE(child); 1663 1664 /* 1665 * If probe returns 0, this is the driver that wins this 1666 * device. 1667 */ 1668 if (result == 0) { 1669 best = dl; 1670 pri = 0; 1671 goto exact_match; /* C doesn't have break 2 */ 1672 } 1673 1674 /* Reset flags and devclass before the next probe. */ 1675 child->devflags = 0; 1676 if (!hasclass) 1677 (void)device_set_devclass(child, NULL); 1678 1679 /* 1680 * Reset DF_QUIET in case this driver doesn't 1681 * end up as the best driver. 1682 */ 1683 device_verbose(child); 1684 1685 /* 1686 * Probes that return BUS_PROBE_NOWILDCARD or lower 1687 * only match on devices whose driver was explicitly 1688 * specified. 1689 */ 1690 if (result <= BUS_PROBE_NOWILDCARD && 1691 !(child->flags & DF_FIXEDCLASS)) { 1692 result = ENXIO; 1693 } 1694 1695 /* 1696 * The driver returned an error so it 1697 * certainly doesn't match. 1698 */ 1699 if (result > 0) { 1700 (void)device_set_driver(child, NULL); 1701 continue; 1702 } 1703 1704 /* 1705 * A priority lower than SUCCESS, remember the 1706 * best matching driver. Initialise the value 1707 * of pri for the first match. 1708 */ 1709 if (best == NULL || result > pri) { 1710 best = dl; 1711 pri = result; 1712 continue; 1713 } 1714 } 1715 } 1716 1717 if (best == NULL) 1718 return (ENXIO); 1719 1720 /* 1721 * If we found a driver, change state and initialise the devclass. 1722 * Set the winning driver, devclass, and flags. 1723 */ 1724 result = device_set_driver(child, best->driver); 1725 if (result != 0) 1726 return (result); 1727 if (!child->devclass) { 1728 result = device_set_devclass(child, best->driver->name); 1729 if (result != 0) { 1730 (void)device_set_driver(child, NULL); 1731 return (result); 1732 } 1733 } 1734 resource_int_value(best->driver->name, child->unit, 1735 "flags", &child->devflags); 1736 1737 /* 1738 * A bit bogus. Call the probe method again to make sure that we have 1739 * the right description for the device. 1740 */ 1741 result = DEVICE_PROBE(child); 1742 if (result > 0) { 1743 if (!hasclass) 1744 (void)device_set_devclass(child, NULL); 1745 (void)device_set_driver(child, NULL); 1746 return (result); 1747 } 1748 1749 exact_match: 1750 child->state = DS_ALIVE; 1751 bus_data_generation_update(); 1752 return (0); 1753 } 1754 1755 /** 1756 * @brief Return the parent of a device 1757 */ 1758 device_t 1759 device_get_parent(device_t dev) 1760 { 1761 return (dev->parent); 1762 } 1763 1764 /** 1765 * @brief Get a list of children of a device 1766 * 1767 * An array containing a list of all the children of the given device 1768 * is allocated and returned in @p *devlistp. The number of devices 1769 * in the array is returned in @p *devcountp. The caller should free 1770 * the array using @c free(p, M_TEMP). 1771 * 1772 * @param dev the device to examine 1773 * @param devlistp points at location for array pointer return 1774 * value 1775 * @param devcountp points at location for array size return value 1776 * 1777 * @retval 0 success 1778 * @retval ENOMEM the array allocation failed 1779 */ 1780 int 1781 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1782 { 1783 int count; 1784 device_t child; 1785 device_t *list; 1786 1787 count = 0; 1788 TAILQ_FOREACH(child, &dev->children, link) { 1789 count++; 1790 } 1791 if (count == 0) { 1792 *devlistp = NULL; 1793 *devcountp = 0; 1794 return (0); 1795 } 1796 1797 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1798 if (!list) 1799 return (ENOMEM); 1800 1801 count = 0; 1802 TAILQ_FOREACH(child, &dev->children, link) { 1803 list[count] = child; 1804 count++; 1805 } 1806 1807 *devlistp = list; 1808 *devcountp = count; 1809 1810 return (0); 1811 } 1812 1813 /** 1814 * @brief Return the current driver for the device or @c NULL if there 1815 * is no driver currently attached 1816 */ 1817 driver_t * 1818 device_get_driver(device_t dev) 1819 { 1820 return (dev->driver); 1821 } 1822 1823 /** 1824 * @brief Return the current devclass for the device or @c NULL if 1825 * there is none. 1826 */ 1827 devclass_t 1828 device_get_devclass(device_t dev) 1829 { 1830 return (dev->devclass); 1831 } 1832 1833 /** 1834 * @brief Return the name of the device's devclass or @c NULL if there 1835 * is none. 1836 */ 1837 const char * 1838 device_get_name(device_t dev) 1839 { 1840 if (dev != NULL && dev->devclass) 1841 return (devclass_get_name(dev->devclass)); 1842 return (NULL); 1843 } 1844 1845 /** 1846 * @brief Return a string containing the device's devclass name 1847 * followed by an ascii representation of the device's unit number 1848 * (e.g. @c "foo2"). 1849 */ 1850 const char * 1851 device_get_nameunit(device_t dev) 1852 { 1853 return (dev->nameunit); 1854 } 1855 1856 /** 1857 * @brief Return the device's unit number. 1858 */ 1859 int 1860 device_get_unit(device_t dev) 1861 { 1862 return (dev->unit); 1863 } 1864 1865 /** 1866 * @brief Return the device's description string 1867 */ 1868 const char * 1869 device_get_desc(device_t dev) 1870 { 1871 return (dev->desc); 1872 } 1873 1874 /** 1875 * @brief Return the device's flags 1876 */ 1877 uint32_t 1878 device_get_flags(device_t dev) 1879 { 1880 return (dev->devflags); 1881 } 1882 1883 struct sysctl_ctx_list * 1884 device_get_sysctl_ctx(device_t dev) 1885 { 1886 return (&dev->sysctl_ctx); 1887 } 1888 1889 struct sysctl_oid * 1890 device_get_sysctl_tree(device_t dev) 1891 { 1892 return (dev->sysctl_tree); 1893 } 1894 1895 /** 1896 * @brief Print the name of the device followed by a colon and a space 1897 * 1898 * @returns the number of characters printed 1899 */ 1900 int 1901 device_print_prettyname(device_t dev) 1902 { 1903 const char *name = device_get_name(dev); 1904 1905 if (name == NULL) 1906 return (printf("unknown: ")); 1907 return (printf("%s%d: ", name, device_get_unit(dev))); 1908 } 1909 1910 /** 1911 * @brief Print the name of the device followed by a colon, a space 1912 * and the result of calling vprintf() with the value of @p fmt and 1913 * the following arguments. 1914 * 1915 * @returns the number of characters printed 1916 */ 1917 int 1918 device_printf(device_t dev, const char * fmt, ...) 1919 { 1920 char buf[128]; 1921 struct sbuf sb; 1922 const char *name; 1923 va_list ap; 1924 size_t retval; 1925 1926 retval = 0; 1927 1928 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 1929 sbuf_set_drain(&sb, sbuf_printf_drain, &retval); 1930 1931 name = device_get_name(dev); 1932 1933 if (name == NULL) 1934 sbuf_cat(&sb, "unknown: "); 1935 else 1936 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev)); 1937 1938 va_start(ap, fmt); 1939 sbuf_vprintf(&sb, fmt, ap); 1940 va_end(ap); 1941 1942 sbuf_finish(&sb); 1943 sbuf_delete(&sb); 1944 1945 return (retval); 1946 } 1947 1948 /** 1949 * @brief Print the name of the device followed by a colon, a space 1950 * and the result of calling log() with the value of @p fmt and 1951 * the following arguments. 1952 * 1953 * @returns the number of characters printed 1954 */ 1955 int 1956 device_log(device_t dev, int pri, const char * fmt, ...) 1957 { 1958 char buf[128]; 1959 struct sbuf sb; 1960 const char *name; 1961 va_list ap; 1962 size_t retval; 1963 1964 retval = 0; 1965 1966 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 1967 1968 name = device_get_name(dev); 1969 1970 if (name == NULL) 1971 sbuf_cat(&sb, "unknown: "); 1972 else 1973 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev)); 1974 1975 va_start(ap, fmt); 1976 sbuf_vprintf(&sb, fmt, ap); 1977 va_end(ap); 1978 1979 sbuf_finish(&sb); 1980 1981 log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb)); 1982 retval = sbuf_len(&sb); 1983 1984 sbuf_delete(&sb); 1985 1986 return (retval); 1987 } 1988 1989 /** 1990 * @internal 1991 */ 1992 static void 1993 device_set_desc_internal(device_t dev, const char *desc, bool allocated) 1994 { 1995 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1996 free(dev->desc, M_BUS); 1997 dev->flags &= ~DF_DESCMALLOCED; 1998 dev->desc = NULL; 1999 } 2000 2001 if (allocated && desc) 2002 dev->flags |= DF_DESCMALLOCED; 2003 dev->desc = __DECONST(char *, desc); 2004 2005 bus_data_generation_update(); 2006 } 2007 2008 /** 2009 * @brief Set the device's description 2010 * 2011 * The value of @c desc should be a string constant that will not 2012 * change (at least until the description is changed in a subsequent 2013 * call to device_set_desc() or device_set_desc_copy()). 2014 */ 2015 void 2016 device_set_desc(device_t dev, const char *desc) 2017 { 2018 device_set_desc_internal(dev, desc, false); 2019 } 2020 2021 /** 2022 * @brief Set the device's description 2023 * 2024 * A printf-like version of device_set_desc(). 2025 */ 2026 void 2027 device_set_descf(device_t dev, const char *fmt, ...) 2028 { 2029 va_list ap; 2030 char *buf = NULL; 2031 2032 va_start(ap, fmt); 2033 vasprintf(&buf, M_BUS, fmt, ap); 2034 va_end(ap); 2035 device_set_desc_internal(dev, buf, true); 2036 } 2037 2038 /** 2039 * @brief Set the device's description 2040 * 2041 * The string pointed to by @c desc is copied. Use this function if 2042 * the device description is generated, (e.g. with sprintf()). 2043 */ 2044 void 2045 device_set_desc_copy(device_t dev, const char *desc) 2046 { 2047 char *buf; 2048 2049 buf = strdup_flags(desc, M_BUS, M_NOWAIT); 2050 device_set_desc_internal(dev, buf, true); 2051 } 2052 2053 /** 2054 * @brief Set the device's flags 2055 */ 2056 void 2057 device_set_flags(device_t dev, uint32_t flags) 2058 { 2059 dev->devflags = flags; 2060 } 2061 2062 /** 2063 * @brief Return the device's softc field 2064 * 2065 * The softc is allocated and zeroed when a driver is attached, based 2066 * on the size field of the driver. 2067 */ 2068 void * 2069 device_get_softc(device_t dev) 2070 { 2071 return (dev->softc); 2072 } 2073 2074 /** 2075 * @brief Set the device's softc field 2076 * 2077 * Most drivers do not need to use this since the softc is allocated 2078 * automatically when the driver is attached. 2079 */ 2080 void 2081 device_set_softc(device_t dev, void *softc) 2082 { 2083 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2084 free(dev->softc, M_BUS_SC); 2085 dev->softc = softc; 2086 if (dev->softc) 2087 dev->flags |= DF_EXTERNALSOFTC; 2088 else 2089 dev->flags &= ~DF_EXTERNALSOFTC; 2090 } 2091 2092 /** 2093 * @brief Free claimed softc 2094 * 2095 * Most drivers do not need to use this since the softc is freed 2096 * automatically when the driver is detached. 2097 */ 2098 void 2099 device_free_softc(void *softc) 2100 { 2101 free(softc, M_BUS_SC); 2102 } 2103 2104 /** 2105 * @brief Claim softc 2106 * 2107 * This function can be used to let the driver free the automatically 2108 * allocated softc using "device_free_softc()". This function is 2109 * useful when the driver is refcounting the softc and the softc 2110 * cannot be freed when the "device_detach" method is called. 2111 */ 2112 void 2113 device_claim_softc(device_t dev) 2114 { 2115 if (dev->softc) 2116 dev->flags |= DF_EXTERNALSOFTC; 2117 else 2118 dev->flags &= ~DF_EXTERNALSOFTC; 2119 } 2120 2121 /** 2122 * @brief Get the device's ivars field 2123 * 2124 * The ivars field is used by the parent device to store per-device 2125 * state (e.g. the physical location of the device or a list of 2126 * resources). 2127 */ 2128 void * 2129 device_get_ivars(device_t dev) 2130 { 2131 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2132 return (dev->ivars); 2133 } 2134 2135 /** 2136 * @brief Set the device's ivars field 2137 */ 2138 void 2139 device_set_ivars(device_t dev, void * ivars) 2140 { 2141 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2142 dev->ivars = ivars; 2143 } 2144 2145 /** 2146 * @brief Return the device's state 2147 */ 2148 device_state_t 2149 device_get_state(device_t dev) 2150 { 2151 return (dev->state); 2152 } 2153 2154 /** 2155 * @brief Set the DF_ENABLED flag for the device 2156 */ 2157 void 2158 device_enable(device_t dev) 2159 { 2160 dev->flags |= DF_ENABLED; 2161 } 2162 2163 /** 2164 * @brief Clear the DF_ENABLED flag for the device 2165 */ 2166 void 2167 device_disable(device_t dev) 2168 { 2169 dev->flags &= ~DF_ENABLED; 2170 } 2171 2172 /** 2173 * @brief Increment the busy counter for the device 2174 */ 2175 void 2176 device_busy(device_t dev) 2177 { 2178 2179 /* 2180 * Mark the device as busy, recursively up the tree if this busy count 2181 * goes 0->1. 2182 */ 2183 if (refcount_acquire(&dev->busy) == 0 && dev->parent != NULL) 2184 device_busy(dev->parent); 2185 } 2186 2187 /** 2188 * @brief Decrement the busy counter for the device 2189 */ 2190 void 2191 device_unbusy(device_t dev) 2192 { 2193 2194 /* 2195 * Mark the device as unbsy, recursively if this is the last busy count. 2196 */ 2197 if (refcount_release(&dev->busy) && dev->parent != NULL) 2198 device_unbusy(dev->parent); 2199 } 2200 2201 /** 2202 * @brief Set the DF_QUIET flag for the device 2203 */ 2204 void 2205 device_quiet(device_t dev) 2206 { 2207 dev->flags |= DF_QUIET; 2208 } 2209 2210 /** 2211 * @brief Set the DF_QUIET_CHILDREN flag for the device 2212 */ 2213 void 2214 device_quiet_children(device_t dev) 2215 { 2216 dev->flags |= DF_QUIET_CHILDREN; 2217 } 2218 2219 /** 2220 * @brief Clear the DF_QUIET flag for the device 2221 */ 2222 void 2223 device_verbose(device_t dev) 2224 { 2225 dev->flags &= ~DF_QUIET; 2226 } 2227 2228 ssize_t 2229 device_get_property(device_t dev, const char *prop, void *val, size_t sz, 2230 device_property_type_t type) 2231 { 2232 device_t bus = device_get_parent(dev); 2233 2234 switch (type) { 2235 case DEVICE_PROP_ANY: 2236 case DEVICE_PROP_BUFFER: 2237 case DEVICE_PROP_HANDLE: /* Size checks done in implementation. */ 2238 break; 2239 case DEVICE_PROP_UINT32: 2240 if (sz % 4 != 0) 2241 return (-1); 2242 break; 2243 case DEVICE_PROP_UINT64: 2244 if (sz % 8 != 0) 2245 return (-1); 2246 break; 2247 default: 2248 return (-1); 2249 } 2250 2251 return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type)); 2252 } 2253 2254 bool 2255 device_has_property(device_t dev, const char *prop) 2256 { 2257 return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0); 2258 } 2259 2260 /** 2261 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device 2262 */ 2263 int 2264 device_has_quiet_children(device_t dev) 2265 { 2266 return ((dev->flags & DF_QUIET_CHILDREN) != 0); 2267 } 2268 2269 /** 2270 * @brief Return non-zero if the DF_QUIET flag is set on the device 2271 */ 2272 int 2273 device_is_quiet(device_t dev) 2274 { 2275 return ((dev->flags & DF_QUIET) != 0); 2276 } 2277 2278 /** 2279 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2280 */ 2281 int 2282 device_is_enabled(device_t dev) 2283 { 2284 return ((dev->flags & DF_ENABLED) != 0); 2285 } 2286 2287 /** 2288 * @brief Return non-zero if the device was successfully probed 2289 */ 2290 int 2291 device_is_alive(device_t dev) 2292 { 2293 return (dev->state >= DS_ALIVE); 2294 } 2295 2296 /** 2297 * @brief Return non-zero if the device currently has a driver 2298 * attached to it 2299 */ 2300 int 2301 device_is_attached(device_t dev) 2302 { 2303 return (dev->state >= DS_ATTACHED); 2304 } 2305 2306 /** 2307 * @brief Return non-zero if the device is currently suspended. 2308 */ 2309 int 2310 device_is_suspended(device_t dev) 2311 { 2312 return ((dev->flags & DF_SUSPENDED) != 0); 2313 } 2314 2315 /** 2316 * @brief Set the devclass of a device 2317 * @see devclass_add_device(). 2318 */ 2319 int 2320 device_set_devclass(device_t dev, const char *classname) 2321 { 2322 devclass_t dc; 2323 int error; 2324 2325 if (!classname) { 2326 if (dev->devclass) 2327 devclass_delete_device(dev->devclass, dev); 2328 return (0); 2329 } 2330 2331 if (dev->devclass) { 2332 printf("device_set_devclass: device class already set\n"); 2333 return (EINVAL); 2334 } 2335 2336 dc = devclass_find_internal(classname, NULL, TRUE); 2337 if (!dc) 2338 return (ENOMEM); 2339 2340 error = devclass_add_device(dc, dev); 2341 2342 bus_data_generation_update(); 2343 return (error); 2344 } 2345 2346 /** 2347 * @brief Set the devclass of a device and mark the devclass fixed. 2348 * @see device_set_devclass() 2349 */ 2350 int 2351 device_set_devclass_fixed(device_t dev, const char *classname) 2352 { 2353 int error; 2354 2355 if (classname == NULL) 2356 return (EINVAL); 2357 2358 error = device_set_devclass(dev, classname); 2359 if (error) 2360 return (error); 2361 dev->flags |= DF_FIXEDCLASS; 2362 return (0); 2363 } 2364 2365 /** 2366 * @brief Query the device to determine if it's of a fixed devclass 2367 * @see device_set_devclass_fixed() 2368 */ 2369 bool 2370 device_is_devclass_fixed(device_t dev) 2371 { 2372 return ((dev->flags & DF_FIXEDCLASS) != 0); 2373 } 2374 2375 /** 2376 * @brief Set the driver of a device 2377 * 2378 * @retval 0 success 2379 * @retval EBUSY the device already has a driver attached 2380 * @retval ENOMEM a memory allocation failure occurred 2381 */ 2382 int 2383 device_set_driver(device_t dev, driver_t *driver) 2384 { 2385 int domain; 2386 struct domainset *policy; 2387 2388 if (dev->state >= DS_ATTACHED) 2389 return (EBUSY); 2390 2391 if (dev->driver == driver) 2392 return (0); 2393 2394 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2395 free(dev->softc, M_BUS_SC); 2396 dev->softc = NULL; 2397 } 2398 device_set_desc(dev, NULL); 2399 kobj_delete((kobj_t) dev, NULL); 2400 dev->driver = driver; 2401 if (driver) { 2402 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2403 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2404 if (bus_get_domain(dev, &domain) == 0) 2405 policy = DOMAINSET_PREF(domain); 2406 else 2407 policy = DOMAINSET_RR(); 2408 dev->softc = malloc_domainset(driver->size, M_BUS_SC, 2409 policy, M_NOWAIT | M_ZERO); 2410 if (!dev->softc) { 2411 kobj_delete((kobj_t) dev, NULL); 2412 kobj_init((kobj_t) dev, &null_class); 2413 dev->driver = NULL; 2414 return (ENOMEM); 2415 } 2416 } 2417 } else { 2418 kobj_init((kobj_t) dev, &null_class); 2419 } 2420 2421 bus_data_generation_update(); 2422 return (0); 2423 } 2424 2425 /** 2426 * @brief Probe a device, and return this status. 2427 * 2428 * This function is the core of the device autoconfiguration 2429 * system. Its purpose is to select a suitable driver for a device and 2430 * then call that driver to initialise the hardware appropriately. The 2431 * driver is selected by calling the DEVICE_PROBE() method of a set of 2432 * candidate drivers and then choosing the driver which returned the 2433 * best value. This driver is then attached to the device using 2434 * device_attach(). 2435 * 2436 * The set of suitable drivers is taken from the list of drivers in 2437 * the parent device's devclass. If the device was originally created 2438 * with a specific class name (see device_add_child()), only drivers 2439 * with that name are probed, otherwise all drivers in the devclass 2440 * are probed. If no drivers return successful probe values in the 2441 * parent devclass, the search continues in the parent of that 2442 * devclass (see devclass_get_parent()) if any. 2443 * 2444 * @param dev the device to initialise 2445 * 2446 * @retval 0 success 2447 * @retval ENXIO no driver was found 2448 * @retval ENOMEM memory allocation failure 2449 * @retval non-zero some other unix error code 2450 * @retval -1 Device already attached 2451 */ 2452 int 2453 device_probe(device_t dev) 2454 { 2455 int error; 2456 2457 bus_topo_assert(); 2458 2459 if (dev->state >= DS_ALIVE) 2460 return (-1); 2461 2462 if (!(dev->flags & DF_ENABLED)) { 2463 if (bootverbose && device_get_name(dev) != NULL) { 2464 device_print_prettyname(dev); 2465 printf("not probed (disabled)\n"); 2466 } 2467 return (-1); 2468 } 2469 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2470 if (bus_current_pass == BUS_PASS_DEFAULT && 2471 !(dev->flags & DF_DONENOMATCH)) { 2472 device_handle_nomatch(dev); 2473 } 2474 return (error); 2475 } 2476 return (0); 2477 } 2478 2479 /** 2480 * @brief Probe a device and attach a driver if possible 2481 * 2482 * calls device_probe() and attaches if that was successful. 2483 */ 2484 int 2485 device_probe_and_attach(device_t dev) 2486 { 2487 int error; 2488 2489 bus_topo_assert(); 2490 2491 error = device_probe(dev); 2492 if (error == -1) 2493 return (0); 2494 else if (error != 0) 2495 return (error); 2496 2497 CURVNET_SET_QUIET(vnet0); 2498 error = device_attach(dev); 2499 CURVNET_RESTORE(); 2500 return error; 2501 } 2502 2503 /** 2504 * @brief Attach a device driver to a device 2505 * 2506 * This function is a wrapper around the DEVICE_ATTACH() driver 2507 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2508 * device's sysctl tree, optionally prints a description of the device 2509 * and queues a notification event for user-based device management 2510 * services. 2511 * 2512 * Normally this function is only called internally from 2513 * device_probe_and_attach(). 2514 * 2515 * @param dev the device to initialise 2516 * 2517 * @retval 0 success 2518 * @retval ENXIO no driver was found 2519 * @retval ENOMEM memory allocation failure 2520 * @retval non-zero some other unix error code 2521 */ 2522 int 2523 device_attach(device_t dev) 2524 { 2525 uint64_t attachtime; 2526 uint16_t attachentropy; 2527 int error; 2528 2529 if (resource_disabled(dev->driver->name, dev->unit)) { 2530 device_disable(dev); 2531 if (bootverbose) 2532 device_printf(dev, "disabled via hints entry\n"); 2533 return (ENXIO); 2534 } 2535 2536 device_sysctl_init(dev); 2537 if (!device_is_quiet(dev)) 2538 device_print_child(dev->parent, dev); 2539 attachtime = get_cyclecount(); 2540 dev->state = DS_ATTACHING; 2541 if ((error = DEVICE_ATTACH(dev)) != 0) { 2542 printf("device_attach: %s%d attach returned %d\n", 2543 dev->driver->name, dev->unit, error); 2544 if (disable_failed_devs) { 2545 /* 2546 * When the user has asked to disable failed devices, we 2547 * directly disable the device, but leave it in the 2548 * attaching state. It will not try to probe/attach the 2549 * device further. This leaves the device numbering 2550 * intact for other similar devices in the system. It 2551 * can be removed from this state with devctl. 2552 */ 2553 device_disable(dev); 2554 } else { 2555 /* 2556 * Otherwise, when attach fails, tear down the state 2557 * around that so we can retry when, for example, new 2558 * drivers are loaded. 2559 */ 2560 if (!(dev->flags & DF_FIXEDCLASS)) 2561 devclass_delete_device(dev->devclass, dev); 2562 (void)device_set_driver(dev, NULL); 2563 device_sysctl_fini(dev); 2564 KASSERT(dev->busy == 0, ("attach failed but busy")); 2565 dev->state = DS_NOTPRESENT; 2566 } 2567 return (error); 2568 } 2569 dev->flags |= DF_ATTACHED_ONCE; 2570 /* 2571 * We only need the low bits of this time, but ranges from tens to thousands 2572 * have been seen, so keep 2 bytes' worth. 2573 */ 2574 attachentropy = (uint16_t)(get_cyclecount() - attachtime); 2575 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH); 2576 device_sysctl_update(dev); 2577 dev->state = DS_ATTACHED; 2578 dev->flags &= ~DF_DONENOMATCH; 2579 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev); 2580 return (0); 2581 } 2582 2583 /** 2584 * @brief Detach a driver from a device 2585 * 2586 * This function is a wrapper around the DEVICE_DETACH() driver 2587 * method. If the call to DEVICE_DETACH() succeeds, it calls 2588 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2589 * notification event for user-based device management services and 2590 * cleans up the device's sysctl tree. 2591 * 2592 * @param dev the device to un-initialise 2593 * 2594 * @retval 0 success 2595 * @retval ENXIO no driver was found 2596 * @retval ENOMEM memory allocation failure 2597 * @retval non-zero some other unix error code 2598 */ 2599 int 2600 device_detach(device_t dev) 2601 { 2602 int error; 2603 2604 bus_topo_assert(); 2605 2606 PDEBUG(("%s", DEVICENAME(dev))); 2607 if (dev->busy > 0) 2608 return (EBUSY); 2609 if (dev->state == DS_ATTACHING) { 2610 device_printf(dev, "device in attaching state! Deferring detach.\n"); 2611 return (EBUSY); 2612 } 2613 if (dev->state != DS_ATTACHED) 2614 return (0); 2615 2616 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN); 2617 if ((error = DEVICE_DETACH(dev)) != 0) { 2618 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2619 EVHDEV_DETACH_FAILED); 2620 return (error); 2621 } else { 2622 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2623 EVHDEV_DETACH_COMPLETE); 2624 } 2625 if (!device_is_quiet(dev)) 2626 device_printf(dev, "detached\n"); 2627 if (dev->parent) 2628 BUS_CHILD_DETACHED(dev->parent, dev); 2629 2630 if (!(dev->flags & DF_FIXEDCLASS)) 2631 devclass_delete_device(dev->devclass, dev); 2632 2633 device_verbose(dev); 2634 dev->state = DS_NOTPRESENT; 2635 (void)device_set_driver(dev, NULL); 2636 device_sysctl_fini(dev); 2637 2638 return (0); 2639 } 2640 2641 /** 2642 * @brief Tells a driver to quiesce itself. 2643 * 2644 * This function is a wrapper around the DEVICE_QUIESCE() driver 2645 * method. If the call to DEVICE_QUIESCE() succeeds. 2646 * 2647 * @param dev the device to quiesce 2648 * 2649 * @retval 0 success 2650 * @retval ENXIO no driver was found 2651 * @retval ENOMEM memory allocation failure 2652 * @retval non-zero some other unix error code 2653 */ 2654 int 2655 device_quiesce(device_t dev) 2656 { 2657 PDEBUG(("%s", DEVICENAME(dev))); 2658 if (dev->busy > 0) 2659 return (EBUSY); 2660 if (dev->state != DS_ATTACHED) 2661 return (0); 2662 2663 return (DEVICE_QUIESCE(dev)); 2664 } 2665 2666 /** 2667 * @brief Notify a device of system shutdown 2668 * 2669 * This function calls the DEVICE_SHUTDOWN() driver method if the 2670 * device currently has an attached driver. 2671 * 2672 * @returns the value returned by DEVICE_SHUTDOWN() 2673 */ 2674 int 2675 device_shutdown(device_t dev) 2676 { 2677 if (dev->state < DS_ATTACHED) 2678 return (0); 2679 return (DEVICE_SHUTDOWN(dev)); 2680 } 2681 2682 /** 2683 * @brief Set the unit number of a device 2684 * 2685 * This function can be used to override the unit number used for a 2686 * device (e.g. to wire a device to a pre-configured unit number). 2687 */ 2688 int 2689 device_set_unit(device_t dev, int unit) 2690 { 2691 devclass_t dc; 2692 int err; 2693 2694 if (unit == dev->unit) 2695 return (0); 2696 dc = device_get_devclass(dev); 2697 if (unit < dc->maxunit && dc->devices[unit]) 2698 return (EBUSY); 2699 err = devclass_delete_device(dc, dev); 2700 if (err) 2701 return (err); 2702 dev->unit = unit; 2703 err = devclass_add_device(dc, dev); 2704 if (err) 2705 return (err); 2706 2707 bus_data_generation_update(); 2708 return (0); 2709 } 2710 2711 /*======================================*/ 2712 /* 2713 * Some useful method implementations to make life easier for bus drivers. 2714 */ 2715 2716 /** 2717 * @brief Initialize a resource mapping request 2718 * 2719 * This is the internal implementation of the public API 2720 * resource_init_map_request. Callers may be using a different layout 2721 * of struct resource_map_request than the kernel, so callers pass in 2722 * the size of the structure they are using to identify the structure 2723 * layout. 2724 */ 2725 void 2726 resource_init_map_request_impl(struct resource_map_request *args, size_t sz) 2727 { 2728 bzero(args, sz); 2729 args->size = sz; 2730 args->memattr = VM_MEMATTR_DEVICE; 2731 } 2732 2733 /** 2734 * @brief Validate a resource mapping request 2735 * 2736 * Translate a device driver's mapping request (@p in) to a struct 2737 * resource_map_request using the current structure layout (@p out). 2738 * In addition, validate the offset and length from the mapping 2739 * request against the bounds of the resource @p r. If the offset or 2740 * length are invalid, fail with EINVAL. If the offset and length are 2741 * valid, the absolute starting address of the requested mapping is 2742 * returned in @p startp and the length of the requested mapping is 2743 * returned in @p lengthp. 2744 */ 2745 int 2746 resource_validate_map_request(struct resource *r, 2747 struct resource_map_request *in, struct resource_map_request *out, 2748 rman_res_t *startp, rman_res_t *lengthp) 2749 { 2750 rman_res_t end, length, start; 2751 2752 /* 2753 * This assumes that any callers of this function are compiled 2754 * into the kernel and use the same version of the structure 2755 * as this file. 2756 */ 2757 MPASS(out->size == sizeof(struct resource_map_request)); 2758 2759 if (in != NULL) 2760 bcopy(in, out, imin(in->size, out->size)); 2761 start = rman_get_start(r) + out->offset; 2762 if (out->length == 0) 2763 length = rman_get_size(r); 2764 else 2765 length = out->length; 2766 end = start + length - 1; 2767 if (start > rman_get_end(r) || start < rman_get_start(r)) 2768 return (EINVAL); 2769 if (end > rman_get_end(r) || end < start) 2770 return (EINVAL); 2771 *lengthp = length; 2772 *startp = start; 2773 return (0); 2774 } 2775 2776 /** 2777 * @brief Initialise a resource list. 2778 * 2779 * @param rl the resource list to initialise 2780 */ 2781 void 2782 resource_list_init(struct resource_list *rl) 2783 { 2784 STAILQ_INIT(rl); 2785 } 2786 2787 /** 2788 * @brief Reclaim memory used by a resource list. 2789 * 2790 * This function frees the memory for all resource entries on the list 2791 * (if any). 2792 * 2793 * @param rl the resource list to free 2794 */ 2795 void 2796 resource_list_free(struct resource_list *rl) 2797 { 2798 struct resource_list_entry *rle; 2799 2800 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2801 if (rle->res) 2802 panic("resource_list_free: resource entry is busy"); 2803 STAILQ_REMOVE_HEAD(rl, link); 2804 free(rle, M_BUS); 2805 } 2806 } 2807 2808 /** 2809 * @brief Add a resource entry. 2810 * 2811 * This function adds a resource entry using the given @p type, @p 2812 * start, @p end and @p count values. A rid value is chosen by 2813 * searching sequentially for the first unused rid starting at zero. 2814 * 2815 * @param rl the resource list to edit 2816 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2817 * @param start the start address of the resource 2818 * @param end the end address of the resource 2819 * @param count XXX end-start+1 2820 */ 2821 int 2822 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start, 2823 rman_res_t end, rman_res_t count) 2824 { 2825 int rid; 2826 2827 rid = 0; 2828 while (resource_list_find(rl, type, rid) != NULL) 2829 rid++; 2830 resource_list_add(rl, type, rid, start, end, count); 2831 return (rid); 2832 } 2833 2834 /** 2835 * @brief Add or modify a resource entry. 2836 * 2837 * If an existing entry exists with the same type and rid, it will be 2838 * modified using the given values of @p start, @p end and @p 2839 * count. If no entry exists, a new one will be created using the 2840 * given values. The resource list entry that matches is then returned. 2841 * 2842 * @param rl the resource list to edit 2843 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2844 * @param rid the resource identifier 2845 * @param start the start address of the resource 2846 * @param end the end address of the resource 2847 * @param count XXX end-start+1 2848 */ 2849 struct resource_list_entry * 2850 resource_list_add(struct resource_list *rl, int type, int rid, 2851 rman_res_t start, rman_res_t end, rman_res_t count) 2852 { 2853 struct resource_list_entry *rle; 2854 2855 rle = resource_list_find(rl, type, rid); 2856 if (!rle) { 2857 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 2858 M_NOWAIT); 2859 if (!rle) 2860 panic("resource_list_add: can't record entry"); 2861 STAILQ_INSERT_TAIL(rl, rle, link); 2862 rle->type = type; 2863 rle->rid = rid; 2864 rle->res = NULL; 2865 rle->flags = 0; 2866 } 2867 2868 if (rle->res) 2869 panic("resource_list_add: resource entry is busy"); 2870 2871 rle->start = start; 2872 rle->end = end; 2873 rle->count = count; 2874 return (rle); 2875 } 2876 2877 /** 2878 * @brief Determine if a resource entry is busy. 2879 * 2880 * Returns true if a resource entry is busy meaning that it has an 2881 * associated resource that is not an unallocated "reserved" resource. 2882 * 2883 * @param rl the resource list to search 2884 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2885 * @param rid the resource identifier 2886 * 2887 * @returns Non-zero if the entry is busy, zero otherwise. 2888 */ 2889 int 2890 resource_list_busy(struct resource_list *rl, int type, int rid) 2891 { 2892 struct resource_list_entry *rle; 2893 2894 rle = resource_list_find(rl, type, rid); 2895 if (rle == NULL || rle->res == NULL) 2896 return (0); 2897 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) { 2898 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE), 2899 ("reserved resource is active")); 2900 return (0); 2901 } 2902 return (1); 2903 } 2904 2905 /** 2906 * @brief Determine if a resource entry is reserved. 2907 * 2908 * Returns true if a resource entry is reserved meaning that it has an 2909 * associated "reserved" resource. The resource can either be 2910 * allocated or unallocated. 2911 * 2912 * @param rl the resource list to search 2913 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2914 * @param rid the resource identifier 2915 * 2916 * @returns Non-zero if the entry is reserved, zero otherwise. 2917 */ 2918 int 2919 resource_list_reserved(struct resource_list *rl, int type, int rid) 2920 { 2921 struct resource_list_entry *rle; 2922 2923 rle = resource_list_find(rl, type, rid); 2924 if (rle != NULL && rle->flags & RLE_RESERVED) 2925 return (1); 2926 return (0); 2927 } 2928 2929 /** 2930 * @brief Find a resource entry by type and rid. 2931 * 2932 * @param rl the resource list to search 2933 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2934 * @param rid the resource identifier 2935 * 2936 * @returns the resource entry pointer or NULL if there is no such 2937 * entry. 2938 */ 2939 struct resource_list_entry * 2940 resource_list_find(struct resource_list *rl, int type, int rid) 2941 { 2942 struct resource_list_entry *rle; 2943 2944 STAILQ_FOREACH(rle, rl, link) { 2945 if (rle->type == type && rle->rid == rid) 2946 return (rle); 2947 } 2948 return (NULL); 2949 } 2950 2951 /** 2952 * @brief Delete a resource entry. 2953 * 2954 * @param rl the resource list to edit 2955 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2956 * @param rid the resource identifier 2957 */ 2958 void 2959 resource_list_delete(struct resource_list *rl, int type, int rid) 2960 { 2961 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2962 2963 if (rle) { 2964 if (rle->res != NULL) 2965 panic("resource_list_delete: resource has not been released"); 2966 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 2967 free(rle, M_BUS); 2968 } 2969 } 2970 2971 /** 2972 * @brief Allocate a reserved resource 2973 * 2974 * This can be used by buses to force the allocation of resources 2975 * that are always active in the system even if they are not allocated 2976 * by a driver (e.g. PCI BARs). This function is usually called when 2977 * adding a new child to the bus. The resource is allocated from the 2978 * parent bus when it is reserved. The resource list entry is marked 2979 * with RLE_RESERVED to note that it is a reserved resource. 2980 * 2981 * Subsequent attempts to allocate the resource with 2982 * resource_list_alloc() will succeed the first time and will set 2983 * RLE_ALLOCATED to note that it has been allocated. When a reserved 2984 * resource that has been allocated is released with 2985 * resource_list_release() the resource RLE_ALLOCATED is cleared, but 2986 * the actual resource remains allocated. The resource can be released to 2987 * the parent bus by calling resource_list_unreserve(). 2988 * 2989 * @param rl the resource list to allocate from 2990 * @param bus the parent device of @p child 2991 * @param child the device for which the resource is being reserved 2992 * @param type the type of resource to allocate 2993 * @param rid a pointer to the resource identifier 2994 * @param start hint at the start of the resource range - pass 2995 * @c 0 for any start address 2996 * @param end hint at the end of the resource range - pass 2997 * @c ~0 for any end address 2998 * @param count hint at the size of range required - pass @c 1 2999 * for any size 3000 * @param flags any extra flags to control the resource 3001 * allocation - see @c RF_XXX flags in 3002 * <sys/rman.h> for details 3003 * 3004 * @returns the resource which was allocated or @c NULL if no 3005 * resource could be allocated 3006 */ 3007 struct resource * 3008 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, 3009 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3010 { 3011 struct resource_list_entry *rle = NULL; 3012 int passthrough = (device_get_parent(child) != bus); 3013 struct resource *r; 3014 3015 if (passthrough) 3016 panic( 3017 "resource_list_reserve() should only be called for direct children"); 3018 if (flags & RF_ACTIVE) 3019 panic( 3020 "resource_list_reserve() should only reserve inactive resources"); 3021 3022 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 3023 flags); 3024 if (r != NULL) { 3025 rle = resource_list_find(rl, type, *rid); 3026 rle->flags |= RLE_RESERVED; 3027 } 3028 return (r); 3029 } 3030 3031 /** 3032 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 3033 * 3034 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 3035 * and passing the allocation up to the parent of @p bus. This assumes 3036 * that the first entry of @c device_get_ivars(child) is a struct 3037 * resource_list. This also handles 'passthrough' allocations where a 3038 * child is a remote descendant of bus by passing the allocation up to 3039 * the parent of bus. 3040 * 3041 * Typically, a bus driver would store a list of child resources 3042 * somewhere in the child device's ivars (see device_get_ivars()) and 3043 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 3044 * then call resource_list_alloc() to perform the allocation. 3045 * 3046 * @param rl the resource list to allocate from 3047 * @param bus the parent device of @p child 3048 * @param child the device which is requesting an allocation 3049 * @param type the type of resource to allocate 3050 * @param rid a pointer to the resource identifier 3051 * @param start hint at the start of the resource range - pass 3052 * @c 0 for any start address 3053 * @param end hint at the end of the resource range - pass 3054 * @c ~0 for any end address 3055 * @param count hint at the size of range required - pass @c 1 3056 * for any size 3057 * @param flags any extra flags to control the resource 3058 * allocation - see @c RF_XXX flags in 3059 * <sys/rman.h> for details 3060 * 3061 * @returns the resource which was allocated or @c NULL if no 3062 * resource could be allocated 3063 */ 3064 struct resource * 3065 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 3066 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3067 { 3068 struct resource_list_entry *rle = NULL; 3069 int passthrough = (device_get_parent(child) != bus); 3070 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 3071 3072 if (passthrough) { 3073 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3074 type, rid, start, end, count, flags)); 3075 } 3076 3077 rle = resource_list_find(rl, type, *rid); 3078 3079 if (!rle) 3080 return (NULL); /* no resource of that type/rid */ 3081 3082 if (rle->res) { 3083 if (rle->flags & RLE_RESERVED) { 3084 if (rle->flags & RLE_ALLOCATED) 3085 return (NULL); 3086 if ((flags & RF_ACTIVE) && 3087 bus_activate_resource(child, type, *rid, 3088 rle->res) != 0) 3089 return (NULL); 3090 rle->flags |= RLE_ALLOCATED; 3091 return (rle->res); 3092 } 3093 device_printf(bus, 3094 "resource entry %#x type %d for child %s is busy\n", *rid, 3095 type, device_get_nameunit(child)); 3096 return (NULL); 3097 } 3098 3099 if (isdefault) { 3100 start = rle->start; 3101 count = ulmax(count, rle->count); 3102 end = ulmax(rle->end, start + count - 1); 3103 } 3104 3105 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3106 type, rid, start, end, count, flags); 3107 3108 /* 3109 * Record the new range. 3110 */ 3111 if (rle->res) { 3112 rle->start = rman_get_start(rle->res); 3113 rle->end = rman_get_end(rle->res); 3114 rle->count = count; 3115 } 3116 3117 return (rle->res); 3118 } 3119 3120 /** 3121 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 3122 * 3123 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 3124 * used with resource_list_alloc(). 3125 * 3126 * @param rl the resource list which was allocated from 3127 * @param bus the parent device of @p child 3128 * @param child the device which is requesting a release 3129 * @param res the resource to release 3130 * 3131 * @retval 0 success 3132 * @retval non-zero a standard unix error code indicating what 3133 * error condition prevented the operation 3134 */ 3135 int 3136 resource_list_release(struct resource_list *rl, device_t bus, device_t child, 3137 struct resource *res) 3138 { 3139 struct resource_list_entry *rle = NULL; 3140 int passthrough = (device_get_parent(child) != bus); 3141 int error; 3142 3143 if (passthrough) { 3144 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3145 res)); 3146 } 3147 3148 rle = resource_list_find(rl, rman_get_type(res), rman_get_rid(res)); 3149 3150 if (!rle) 3151 panic("resource_list_release: can't find resource"); 3152 if (!rle->res) 3153 panic("resource_list_release: resource entry is not busy"); 3154 if (rle->flags & RLE_RESERVED) { 3155 if (rle->flags & RLE_ALLOCATED) { 3156 if (rman_get_flags(res) & RF_ACTIVE) { 3157 error = bus_deactivate_resource(child, res); 3158 if (error) 3159 return (error); 3160 } 3161 rle->flags &= ~RLE_ALLOCATED; 3162 return (0); 3163 } 3164 return (EINVAL); 3165 } 3166 3167 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, res); 3168 if (error) 3169 return (error); 3170 3171 rle->res = NULL; 3172 return (0); 3173 } 3174 3175 /** 3176 * @brief Release all active resources of a given type 3177 * 3178 * Release all active resources of a specified type. This is intended 3179 * to be used to cleanup resources leaked by a driver after detach or 3180 * a failed attach. 3181 * 3182 * @param rl the resource list which was allocated from 3183 * @param bus the parent device of @p child 3184 * @param child the device whose active resources are being released 3185 * @param type the type of resources to release 3186 * 3187 * @retval 0 success 3188 * @retval EBUSY at least one resource was active 3189 */ 3190 int 3191 resource_list_release_active(struct resource_list *rl, device_t bus, 3192 device_t child, int type) 3193 { 3194 struct resource_list_entry *rle; 3195 int error, retval; 3196 3197 retval = 0; 3198 STAILQ_FOREACH(rle, rl, link) { 3199 if (rle->type != type) 3200 continue; 3201 if (rle->res == NULL) 3202 continue; 3203 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == 3204 RLE_RESERVED) 3205 continue; 3206 retval = EBUSY; 3207 error = resource_list_release(rl, bus, child, rle->res); 3208 if (error != 0) 3209 device_printf(bus, 3210 "Failed to release active resource: %d\n", error); 3211 } 3212 return (retval); 3213 } 3214 3215 /** 3216 * @brief Fully release a reserved resource 3217 * 3218 * Fully releases a resource reserved via resource_list_reserve(). 3219 * 3220 * @param rl the resource list which was allocated from 3221 * @param bus the parent device of @p child 3222 * @param child the device whose reserved resource is being released 3223 * @param type the type of resource to release 3224 * @param rid the resource identifier 3225 * @param res the resource to release 3226 * 3227 * @retval 0 success 3228 * @retval non-zero a standard unix error code indicating what 3229 * error condition prevented the operation 3230 */ 3231 int 3232 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, 3233 int type, int rid) 3234 { 3235 struct resource_list_entry *rle = NULL; 3236 int passthrough = (device_get_parent(child) != bus); 3237 3238 if (passthrough) 3239 panic( 3240 "resource_list_unreserve() should only be called for direct children"); 3241 3242 rle = resource_list_find(rl, type, rid); 3243 3244 if (!rle) 3245 panic("resource_list_unreserve: can't find resource"); 3246 if (!(rle->flags & RLE_RESERVED)) 3247 return (EINVAL); 3248 if (rle->flags & RLE_ALLOCATED) 3249 return (EBUSY); 3250 rle->flags &= ~RLE_RESERVED; 3251 return (resource_list_release(rl, bus, child, rle->res)); 3252 } 3253 3254 /** 3255 * @brief Print a description of resources in a resource list 3256 * 3257 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 3258 * The name is printed if at least one resource of the given type is available. 3259 * The format is used to print resource start and end. 3260 * 3261 * @param rl the resource list to print 3262 * @param name the name of @p type, e.g. @c "memory" 3263 * @param type type type of resource entry to print 3264 * @param format printf(9) format string to print resource 3265 * start and end values 3266 * 3267 * @returns the number of characters printed 3268 */ 3269 int 3270 resource_list_print_type(struct resource_list *rl, const char *name, int type, 3271 const char *format) 3272 { 3273 struct resource_list_entry *rle; 3274 int printed, retval; 3275 3276 printed = 0; 3277 retval = 0; 3278 /* Yes, this is kinda cheating */ 3279 STAILQ_FOREACH(rle, rl, link) { 3280 if (rle->type == type) { 3281 if (printed == 0) 3282 retval += printf(" %s ", name); 3283 else 3284 retval += printf(","); 3285 printed++; 3286 retval += printf(format, rle->start); 3287 if (rle->count > 1) { 3288 retval += printf("-"); 3289 retval += printf(format, rle->start + 3290 rle->count - 1); 3291 } 3292 } 3293 } 3294 return (retval); 3295 } 3296 3297 /** 3298 * @brief Releases all the resources in a list. 3299 * 3300 * @param rl The resource list to purge. 3301 * 3302 * @returns nothing 3303 */ 3304 void 3305 resource_list_purge(struct resource_list *rl) 3306 { 3307 struct resource_list_entry *rle; 3308 3309 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3310 if (rle->res) 3311 bus_release_resource(rman_get_device(rle->res), 3312 rle->type, rle->rid, rle->res); 3313 STAILQ_REMOVE_HEAD(rl, link); 3314 free(rle, M_BUS); 3315 } 3316 } 3317 3318 device_t 3319 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 3320 { 3321 return (device_add_child_ordered(dev, order, name, unit)); 3322 } 3323 3324 /** 3325 * @brief Helper function for implementing DEVICE_PROBE() 3326 * 3327 * This function can be used to help implement the DEVICE_PROBE() for 3328 * a bus (i.e. a device which has other devices attached to it). It 3329 * calls the DEVICE_IDENTIFY() method of each driver in the device's 3330 * devclass. 3331 */ 3332 int 3333 bus_generic_probe(device_t dev) 3334 { 3335 devclass_t dc = dev->devclass; 3336 driverlink_t dl; 3337 3338 TAILQ_FOREACH(dl, &dc->drivers, link) { 3339 /* 3340 * If this driver's pass is too high, then ignore it. 3341 * For most drivers in the default pass, this will 3342 * never be true. For early-pass drivers they will 3343 * only call the identify routines of eligible drivers 3344 * when this routine is called. Drivers for later 3345 * passes should have their identify routines called 3346 * on early-pass buses during BUS_NEW_PASS(). 3347 */ 3348 if (dl->pass > bus_current_pass) 3349 continue; 3350 DEVICE_IDENTIFY(dl->driver, dev); 3351 } 3352 3353 return (0); 3354 } 3355 3356 /** 3357 * @brief Helper function for implementing DEVICE_ATTACH() 3358 * 3359 * This function can be used to help implement the DEVICE_ATTACH() for 3360 * a bus. It calls device_probe_and_attach() for each of the device's 3361 * children. 3362 */ 3363 int 3364 bus_generic_attach(device_t dev) 3365 { 3366 device_t child; 3367 3368 TAILQ_FOREACH(child, &dev->children, link) { 3369 device_probe_and_attach(child); 3370 } 3371 3372 return (0); 3373 } 3374 3375 /** 3376 * @brief Helper function for delaying attaching children 3377 * 3378 * Many buses can't run transactions on the bus which children need to probe and 3379 * attach until after interrupts and/or timers are running. This function 3380 * delays their attach until interrupts and timers are enabled. 3381 */ 3382 int 3383 bus_delayed_attach_children(device_t dev) 3384 { 3385 /* Probe and attach the bus children when interrupts are available */ 3386 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 3387 3388 return (0); 3389 } 3390 3391 /** 3392 * @brief Helper function for implementing DEVICE_DETACH() 3393 * 3394 * This function can be used to help implement the DEVICE_DETACH() for 3395 * a bus. It calls device_detach() for each of the device's 3396 * children. 3397 */ 3398 int 3399 bus_generic_detach(device_t dev) 3400 { 3401 device_t child; 3402 int error; 3403 3404 if (dev->state != DS_ATTACHED) 3405 return (EBUSY); 3406 3407 /* 3408 * Detach children in the reverse order. 3409 * See bus_generic_suspend for details. 3410 */ 3411 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3412 if ((error = device_detach(child)) != 0) 3413 return (error); 3414 } 3415 3416 return (0); 3417 } 3418 3419 /** 3420 * @brief Helper function for implementing DEVICE_SHUTDOWN() 3421 * 3422 * This function can be used to help implement the DEVICE_SHUTDOWN() 3423 * for a bus. It calls device_shutdown() for each of the device's 3424 * children. 3425 */ 3426 int 3427 bus_generic_shutdown(device_t dev) 3428 { 3429 device_t child; 3430 3431 /* 3432 * Shut down children in the reverse order. 3433 * See bus_generic_suspend for details. 3434 */ 3435 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3436 device_shutdown(child); 3437 } 3438 3439 return (0); 3440 } 3441 3442 /** 3443 * @brief Default function for suspending a child device. 3444 * 3445 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD(). 3446 */ 3447 int 3448 bus_generic_suspend_child(device_t dev, device_t child) 3449 { 3450 int error; 3451 3452 error = DEVICE_SUSPEND(child); 3453 3454 if (error == 0) { 3455 child->flags |= DF_SUSPENDED; 3456 } else { 3457 printf("DEVICE_SUSPEND(%s) failed: %d\n", 3458 device_get_nameunit(child), error); 3459 } 3460 3461 return (error); 3462 } 3463 3464 /** 3465 * @brief Default function for resuming a child device. 3466 * 3467 * This function is to be used by a bus's DEVICE_RESUME_CHILD(). 3468 */ 3469 int 3470 bus_generic_resume_child(device_t dev, device_t child) 3471 { 3472 DEVICE_RESUME(child); 3473 child->flags &= ~DF_SUSPENDED; 3474 3475 return (0); 3476 } 3477 3478 /** 3479 * @brief Helper function for implementing DEVICE_SUSPEND() 3480 * 3481 * This function can be used to help implement the DEVICE_SUSPEND() 3482 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 3483 * children. If any call to DEVICE_SUSPEND() fails, the suspend 3484 * operation is aborted and any devices which were suspended are 3485 * resumed immediately by calling their DEVICE_RESUME() methods. 3486 */ 3487 int 3488 bus_generic_suspend(device_t dev) 3489 { 3490 int error; 3491 device_t child; 3492 3493 /* 3494 * Suspend children in the reverse order. 3495 * For most buses all children are equal, so the order does not matter. 3496 * Other buses, such as acpi, carefully order their child devices to 3497 * express implicit dependencies between them. For such buses it is 3498 * safer to bring down devices in the reverse order. 3499 */ 3500 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3501 error = BUS_SUSPEND_CHILD(dev, child); 3502 if (error != 0) { 3503 child = TAILQ_NEXT(child, link); 3504 if (child != NULL) { 3505 TAILQ_FOREACH_FROM(child, &dev->children, link) 3506 BUS_RESUME_CHILD(dev, child); 3507 } 3508 return (error); 3509 } 3510 } 3511 return (0); 3512 } 3513 3514 /** 3515 * @brief Helper function for implementing DEVICE_RESUME() 3516 * 3517 * This function can be used to help implement the DEVICE_RESUME() for 3518 * a bus. It calls DEVICE_RESUME() on each of the device's children. 3519 */ 3520 int 3521 bus_generic_resume(device_t dev) 3522 { 3523 device_t child; 3524 3525 TAILQ_FOREACH(child, &dev->children, link) { 3526 BUS_RESUME_CHILD(dev, child); 3527 /* if resume fails, there's nothing we can usefully do... */ 3528 } 3529 return (0); 3530 } 3531 3532 /** 3533 * @brief Helper function for implementing BUS_RESET_POST 3534 * 3535 * Bus can use this function to implement common operations of 3536 * re-attaching or resuming the children after the bus itself was 3537 * reset, and after restoring bus-unique state of children. 3538 * 3539 * @param dev The bus 3540 * #param flags DEVF_RESET_* 3541 */ 3542 int 3543 bus_helper_reset_post(device_t dev, int flags) 3544 { 3545 device_t child; 3546 int error, error1; 3547 3548 error = 0; 3549 TAILQ_FOREACH(child, &dev->children,link) { 3550 BUS_RESET_POST(dev, child); 3551 error1 = (flags & DEVF_RESET_DETACH) != 0 ? 3552 device_probe_and_attach(child) : 3553 BUS_RESUME_CHILD(dev, child); 3554 if (error == 0 && error1 != 0) 3555 error = error1; 3556 } 3557 return (error); 3558 } 3559 3560 static void 3561 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags) 3562 { 3563 child = TAILQ_NEXT(child, link); 3564 if (child == NULL) 3565 return; 3566 TAILQ_FOREACH_FROM(child, &dev->children,link) { 3567 BUS_RESET_POST(dev, child); 3568 if ((flags & DEVF_RESET_DETACH) != 0) 3569 device_probe_and_attach(child); 3570 else 3571 BUS_RESUME_CHILD(dev, child); 3572 } 3573 } 3574 3575 /** 3576 * @brief Helper function for implementing BUS_RESET_PREPARE 3577 * 3578 * Bus can use this function to implement common operations of 3579 * detaching or suspending the children before the bus itself is 3580 * reset, and then save bus-unique state of children that must 3581 * persists around reset. 3582 * 3583 * @param dev The bus 3584 * #param flags DEVF_RESET_* 3585 */ 3586 int 3587 bus_helper_reset_prepare(device_t dev, int flags) 3588 { 3589 device_t child; 3590 int error; 3591 3592 if (dev->state != DS_ATTACHED) 3593 return (EBUSY); 3594 3595 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3596 if ((flags & DEVF_RESET_DETACH) != 0) { 3597 error = device_get_state(child) == DS_ATTACHED ? 3598 device_detach(child) : 0; 3599 } else { 3600 error = BUS_SUSPEND_CHILD(dev, child); 3601 } 3602 if (error == 0) { 3603 error = BUS_RESET_PREPARE(dev, child); 3604 if (error != 0) { 3605 if ((flags & DEVF_RESET_DETACH) != 0) 3606 device_probe_and_attach(child); 3607 else 3608 BUS_RESUME_CHILD(dev, child); 3609 } 3610 } 3611 if (error != 0) { 3612 bus_helper_reset_prepare_rollback(dev, child, flags); 3613 return (error); 3614 } 3615 } 3616 return (0); 3617 } 3618 3619 /** 3620 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3621 * 3622 * This function prints the first part of the ascii representation of 3623 * @p child, including its name, unit and description (if any - see 3624 * device_set_desc()). 3625 * 3626 * @returns the number of characters printed 3627 */ 3628 int 3629 bus_print_child_header(device_t dev, device_t child) 3630 { 3631 int retval = 0; 3632 3633 if (device_get_desc(child)) { 3634 retval += device_printf(child, "<%s>", device_get_desc(child)); 3635 } else { 3636 retval += printf("%s", device_get_nameunit(child)); 3637 } 3638 3639 return (retval); 3640 } 3641 3642 /** 3643 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3644 * 3645 * This function prints the last part of the ascii representation of 3646 * @p child, which consists of the string @c " on " followed by the 3647 * name and unit of the @p dev. 3648 * 3649 * @returns the number of characters printed 3650 */ 3651 int 3652 bus_print_child_footer(device_t dev, device_t child) 3653 { 3654 return (printf(" on %s\n", device_get_nameunit(dev))); 3655 } 3656 3657 /** 3658 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3659 * 3660 * This function prints out the VM domain for the given device. 3661 * 3662 * @returns the number of characters printed 3663 */ 3664 int 3665 bus_print_child_domain(device_t dev, device_t child) 3666 { 3667 int domain; 3668 3669 /* No domain? Don't print anything */ 3670 if (BUS_GET_DOMAIN(dev, child, &domain) != 0) 3671 return (0); 3672 3673 return (printf(" numa-domain %d", domain)); 3674 } 3675 3676 /** 3677 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3678 * 3679 * This function simply calls bus_print_child_header() followed by 3680 * bus_print_child_footer(). 3681 * 3682 * @returns the number of characters printed 3683 */ 3684 int 3685 bus_generic_print_child(device_t dev, device_t child) 3686 { 3687 int retval = 0; 3688 3689 retval += bus_print_child_header(dev, child); 3690 retval += bus_print_child_domain(dev, child); 3691 retval += bus_print_child_footer(dev, child); 3692 3693 return (retval); 3694 } 3695 3696 /** 3697 * @brief Stub function for implementing BUS_READ_IVAR(). 3698 * 3699 * @returns ENOENT 3700 */ 3701 int 3702 bus_generic_read_ivar(device_t dev, device_t child, int index, 3703 uintptr_t * result) 3704 { 3705 return (ENOENT); 3706 } 3707 3708 /** 3709 * @brief Stub function for implementing BUS_WRITE_IVAR(). 3710 * 3711 * @returns ENOENT 3712 */ 3713 int 3714 bus_generic_write_ivar(device_t dev, device_t child, int index, 3715 uintptr_t value) 3716 { 3717 return (ENOENT); 3718 } 3719 3720 /** 3721 * @brief Helper function for implementing BUS_GET_PROPERTY(). 3722 * 3723 * This simply calls the BUS_GET_PROPERTY of the parent of dev, 3724 * until a non-default implementation is found. 3725 */ 3726 ssize_t 3727 bus_generic_get_property(device_t dev, device_t child, const char *propname, 3728 void *propvalue, size_t size, device_property_type_t type) 3729 { 3730 if (device_get_parent(dev) != NULL) 3731 return (BUS_GET_PROPERTY(device_get_parent(dev), child, 3732 propname, propvalue, size, type)); 3733 3734 return (-1); 3735 } 3736 3737 /** 3738 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 3739 * 3740 * @returns NULL 3741 */ 3742 struct resource_list * 3743 bus_generic_get_resource_list(device_t dev, device_t child) 3744 { 3745 return (NULL); 3746 } 3747 3748 /** 3749 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 3750 * 3751 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 3752 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 3753 * and then calls device_probe_and_attach() for each unattached child. 3754 */ 3755 void 3756 bus_generic_driver_added(device_t dev, driver_t *driver) 3757 { 3758 device_t child; 3759 3760 DEVICE_IDENTIFY(driver, dev); 3761 TAILQ_FOREACH(child, &dev->children, link) { 3762 if (child->state == DS_NOTPRESENT) 3763 device_probe_and_attach(child); 3764 } 3765 } 3766 3767 /** 3768 * @brief Helper function for implementing BUS_NEW_PASS(). 3769 * 3770 * This implementing of BUS_NEW_PASS() first calls the identify 3771 * routines for any drivers that probe at the current pass. Then it 3772 * walks the list of devices for this bus. If a device is already 3773 * attached, then it calls BUS_NEW_PASS() on that device. If the 3774 * device is not already attached, it attempts to attach a driver to 3775 * it. 3776 */ 3777 void 3778 bus_generic_new_pass(device_t dev) 3779 { 3780 driverlink_t dl; 3781 devclass_t dc; 3782 device_t child; 3783 3784 dc = dev->devclass; 3785 TAILQ_FOREACH(dl, &dc->drivers, link) { 3786 if (dl->pass == bus_current_pass) 3787 DEVICE_IDENTIFY(dl->driver, dev); 3788 } 3789 TAILQ_FOREACH(child, &dev->children, link) { 3790 if (child->state >= DS_ATTACHED) 3791 BUS_NEW_PASS(child); 3792 else if (child->state == DS_NOTPRESENT) 3793 device_probe_and_attach(child); 3794 } 3795 } 3796 3797 /** 3798 * @brief Helper function for implementing BUS_SETUP_INTR(). 3799 * 3800 * This simple implementation of BUS_SETUP_INTR() simply calls the 3801 * BUS_SETUP_INTR() method of the parent of @p dev. 3802 */ 3803 int 3804 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3805 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 3806 void **cookiep) 3807 { 3808 /* Propagate up the bus hierarchy until someone handles it. */ 3809 if (dev->parent) 3810 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3811 filter, intr, arg, cookiep)); 3812 return (EINVAL); 3813 } 3814 3815 /** 3816 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3817 * 3818 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3819 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3820 */ 3821 int 3822 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3823 void *cookie) 3824 { 3825 /* Propagate up the bus hierarchy until someone handles it. */ 3826 if (dev->parent) 3827 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3828 return (EINVAL); 3829 } 3830 3831 /** 3832 * @brief Helper function for implementing BUS_SUSPEND_INTR(). 3833 * 3834 * This simple implementation of BUS_SUSPEND_INTR() simply calls the 3835 * BUS_SUSPEND_INTR() method of the parent of @p dev. 3836 */ 3837 int 3838 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq) 3839 { 3840 /* Propagate up the bus hierarchy until someone handles it. */ 3841 if (dev->parent) 3842 return (BUS_SUSPEND_INTR(dev->parent, child, irq)); 3843 return (EINVAL); 3844 } 3845 3846 /** 3847 * @brief Helper function for implementing BUS_RESUME_INTR(). 3848 * 3849 * This simple implementation of BUS_RESUME_INTR() simply calls the 3850 * BUS_RESUME_INTR() method of the parent of @p dev. 3851 */ 3852 int 3853 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq) 3854 { 3855 /* Propagate up the bus hierarchy until someone handles it. */ 3856 if (dev->parent) 3857 return (BUS_RESUME_INTR(dev->parent, child, irq)); 3858 return (EINVAL); 3859 } 3860 3861 /** 3862 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 3863 * 3864 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the 3865 * BUS_ADJUST_RESOURCE() method of the parent of @p dev. 3866 */ 3867 int 3868 bus_generic_adjust_resource(device_t dev, device_t child, struct resource *r, 3869 rman_res_t start, rman_res_t end) 3870 { 3871 /* Propagate up the bus hierarchy until someone handles it. */ 3872 if (dev->parent) 3873 return (BUS_ADJUST_RESOURCE(dev->parent, child, r, start, end)); 3874 return (EINVAL); 3875 } 3876 3877 /* 3878 * @brief Helper function for implementing BUS_TRANSLATE_RESOURCE(). 3879 * 3880 * This simple implementation of BUS_TRANSLATE_RESOURCE() simply calls the 3881 * BUS_TRANSLATE_RESOURCE() method of the parent of @p dev. If there is no 3882 * parent, no translation happens. 3883 */ 3884 int 3885 bus_generic_translate_resource(device_t dev, int type, rman_res_t start, 3886 rman_res_t *newstart) 3887 { 3888 if (dev->parent) 3889 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, 3890 newstart)); 3891 *newstart = start; 3892 return (0); 3893 } 3894 3895 /** 3896 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3897 * 3898 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3899 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3900 */ 3901 struct resource * 3902 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3903 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3904 { 3905 /* Propagate up the bus hierarchy until someone handles it. */ 3906 if (dev->parent) 3907 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3908 start, end, count, flags)); 3909 return (NULL); 3910 } 3911 3912 /** 3913 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3914 * 3915 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3916 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3917 */ 3918 int 3919 bus_generic_release_resource(device_t dev, device_t child, struct resource *r) 3920 { 3921 /* Propagate up the bus hierarchy until someone handles it. */ 3922 if (dev->parent) 3923 return (BUS_RELEASE_RESOURCE(dev->parent, child, r)); 3924 return (EINVAL); 3925 } 3926 3927 /** 3928 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3929 * 3930 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3931 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3932 */ 3933 int 3934 bus_generic_activate_resource(device_t dev, device_t child, struct resource *r) 3935 { 3936 /* Propagate up the bus hierarchy until someone handles it. */ 3937 if (dev->parent) 3938 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, r)); 3939 return (EINVAL); 3940 } 3941 3942 /** 3943 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3944 * 3945 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3946 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3947 */ 3948 int 3949 bus_generic_deactivate_resource(device_t dev, device_t child, 3950 struct resource *r) 3951 { 3952 /* Propagate up the bus hierarchy until someone handles it. */ 3953 if (dev->parent) 3954 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, r)); 3955 return (EINVAL); 3956 } 3957 3958 /** 3959 * @brief Helper function for implementing BUS_MAP_RESOURCE(). 3960 * 3961 * This simple implementation of BUS_MAP_RESOURCE() simply calls the 3962 * BUS_MAP_RESOURCE() method of the parent of @p dev. 3963 */ 3964 int 3965 bus_generic_map_resource(device_t dev, device_t child, struct resource *r, 3966 struct resource_map_request *args, struct resource_map *map) 3967 { 3968 /* Propagate up the bus hierarchy until someone handles it. */ 3969 if (dev->parent) 3970 return (BUS_MAP_RESOURCE(dev->parent, child, r, args, map)); 3971 return (EINVAL); 3972 } 3973 3974 /** 3975 * @brief Helper function for implementing BUS_UNMAP_RESOURCE(). 3976 * 3977 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the 3978 * BUS_UNMAP_RESOURCE() method of the parent of @p dev. 3979 */ 3980 int 3981 bus_generic_unmap_resource(device_t dev, device_t child, struct resource *r, 3982 struct resource_map *map) 3983 { 3984 /* Propagate up the bus hierarchy until someone handles it. */ 3985 if (dev->parent) 3986 return (BUS_UNMAP_RESOURCE(dev->parent, child, r, map)); 3987 return (EINVAL); 3988 } 3989 3990 /** 3991 * @brief Helper function for implementing BUS_BIND_INTR(). 3992 * 3993 * This simple implementation of BUS_BIND_INTR() simply calls the 3994 * BUS_BIND_INTR() method of the parent of @p dev. 3995 */ 3996 int 3997 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, 3998 int cpu) 3999 { 4000 /* Propagate up the bus hierarchy until someone handles it. */ 4001 if (dev->parent) 4002 return (BUS_BIND_INTR(dev->parent, child, irq, cpu)); 4003 return (EINVAL); 4004 } 4005 4006 /** 4007 * @brief Helper function for implementing BUS_CONFIG_INTR(). 4008 * 4009 * This simple implementation of BUS_CONFIG_INTR() simply calls the 4010 * BUS_CONFIG_INTR() method of the parent of @p dev. 4011 */ 4012 int 4013 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 4014 enum intr_polarity pol) 4015 { 4016 /* Propagate up the bus hierarchy until someone handles it. */ 4017 if (dev->parent) 4018 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 4019 return (EINVAL); 4020 } 4021 4022 /** 4023 * @brief Helper function for implementing BUS_DESCRIBE_INTR(). 4024 * 4025 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the 4026 * BUS_DESCRIBE_INTR() method of the parent of @p dev. 4027 */ 4028 int 4029 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, 4030 void *cookie, const char *descr) 4031 { 4032 /* Propagate up the bus hierarchy until someone handles it. */ 4033 if (dev->parent) 4034 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie, 4035 descr)); 4036 return (EINVAL); 4037 } 4038 4039 /** 4040 * @brief Helper function for implementing BUS_GET_CPUS(). 4041 * 4042 * This simple implementation of BUS_GET_CPUS() simply calls the 4043 * BUS_GET_CPUS() method of the parent of @p dev. 4044 */ 4045 int 4046 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op, 4047 size_t setsize, cpuset_t *cpuset) 4048 { 4049 /* Propagate up the bus hierarchy until someone handles it. */ 4050 if (dev->parent != NULL) 4051 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset)); 4052 return (EINVAL); 4053 } 4054 4055 /** 4056 * @brief Helper function for implementing BUS_GET_DMA_TAG(). 4057 * 4058 * This simple implementation of BUS_GET_DMA_TAG() simply calls the 4059 * BUS_GET_DMA_TAG() method of the parent of @p dev. 4060 */ 4061 bus_dma_tag_t 4062 bus_generic_get_dma_tag(device_t dev, device_t child) 4063 { 4064 /* Propagate up the bus hierarchy until someone handles it. */ 4065 if (dev->parent != NULL) 4066 return (BUS_GET_DMA_TAG(dev->parent, child)); 4067 return (NULL); 4068 } 4069 4070 /** 4071 * @brief Helper function for implementing BUS_GET_BUS_TAG(). 4072 * 4073 * This simple implementation of BUS_GET_BUS_TAG() simply calls the 4074 * BUS_GET_BUS_TAG() method of the parent of @p dev. 4075 */ 4076 bus_space_tag_t 4077 bus_generic_get_bus_tag(device_t dev, device_t child) 4078 { 4079 /* Propagate up the bus hierarchy until someone handles it. */ 4080 if (dev->parent != NULL) 4081 return (BUS_GET_BUS_TAG(dev->parent, child)); 4082 return ((bus_space_tag_t)0); 4083 } 4084 4085 /** 4086 * @brief Helper function for implementing BUS_GET_RESOURCE(). 4087 * 4088 * This implementation of BUS_GET_RESOURCE() uses the 4089 * resource_list_find() function to do most of the work. It calls 4090 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4091 * search. 4092 */ 4093 int 4094 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 4095 rman_res_t *startp, rman_res_t *countp) 4096 { 4097 struct resource_list * rl = NULL; 4098 struct resource_list_entry * rle = NULL; 4099 4100 rl = BUS_GET_RESOURCE_LIST(dev, child); 4101 if (!rl) 4102 return (EINVAL); 4103 4104 rle = resource_list_find(rl, type, rid); 4105 if (!rle) 4106 return (ENOENT); 4107 4108 if (startp) 4109 *startp = rle->start; 4110 if (countp) 4111 *countp = rle->count; 4112 4113 return (0); 4114 } 4115 4116 /** 4117 * @brief Helper function for implementing BUS_SET_RESOURCE(). 4118 * 4119 * This implementation of BUS_SET_RESOURCE() uses the 4120 * resource_list_add() function to do most of the work. It calls 4121 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4122 * edit. 4123 */ 4124 int 4125 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 4126 rman_res_t start, rman_res_t count) 4127 { 4128 struct resource_list * rl = NULL; 4129 4130 rl = BUS_GET_RESOURCE_LIST(dev, child); 4131 if (!rl) 4132 return (EINVAL); 4133 4134 resource_list_add(rl, type, rid, start, (start + count - 1), count); 4135 4136 return (0); 4137 } 4138 4139 /** 4140 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 4141 * 4142 * This implementation of BUS_DELETE_RESOURCE() uses the 4143 * resource_list_delete() function to do most of the work. It calls 4144 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4145 * edit. 4146 */ 4147 void 4148 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 4149 { 4150 struct resource_list * rl = NULL; 4151 4152 rl = BUS_GET_RESOURCE_LIST(dev, child); 4153 if (!rl) 4154 return; 4155 4156 resource_list_delete(rl, type, rid); 4157 4158 return; 4159 } 4160 4161 /** 4162 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4163 * 4164 * This implementation of BUS_RELEASE_RESOURCE() uses the 4165 * resource_list_release() function to do most of the work. It calls 4166 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4167 */ 4168 int 4169 bus_generic_rl_release_resource(device_t dev, device_t child, 4170 struct resource *r) 4171 { 4172 struct resource_list * rl = NULL; 4173 4174 if (device_get_parent(child) != dev) 4175 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, r)); 4176 4177 rl = BUS_GET_RESOURCE_LIST(dev, child); 4178 if (!rl) 4179 return (EINVAL); 4180 4181 return (resource_list_release(rl, dev, child, r)); 4182 } 4183 4184 /** 4185 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4186 * 4187 * This implementation of BUS_ALLOC_RESOURCE() uses the 4188 * resource_list_alloc() function to do most of the work. It calls 4189 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4190 */ 4191 struct resource * 4192 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 4193 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4194 { 4195 struct resource_list * rl = NULL; 4196 4197 if (device_get_parent(child) != dev) 4198 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 4199 type, rid, start, end, count, flags)); 4200 4201 rl = BUS_GET_RESOURCE_LIST(dev, child); 4202 if (!rl) 4203 return (NULL); 4204 4205 return (resource_list_alloc(rl, dev, child, type, rid, 4206 start, end, count, flags)); 4207 } 4208 4209 /** 4210 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4211 * 4212 * This implementation of BUS_ALLOC_RESOURCE() allocates a 4213 * resource from a resource manager. It uses BUS_GET_RMAN() 4214 * to obtain the resource manager. 4215 */ 4216 struct resource * 4217 bus_generic_rman_alloc_resource(device_t dev, device_t child, int type, 4218 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4219 { 4220 struct resource *r; 4221 struct rman *rm; 4222 4223 rm = BUS_GET_RMAN(dev, type, flags); 4224 if (rm == NULL) 4225 return (NULL); 4226 4227 r = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 4228 child); 4229 if (r == NULL) 4230 return (NULL); 4231 rman_set_rid(r, *rid); 4232 rman_set_type(r, type); 4233 4234 if (flags & RF_ACTIVE) { 4235 if (bus_activate_resource(child, type, *rid, r) != 0) { 4236 rman_release_resource(r); 4237 return (NULL); 4238 } 4239 } 4240 4241 return (r); 4242 } 4243 4244 /** 4245 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 4246 * 4247 * This implementation of BUS_ADJUST_RESOURCE() adjusts resources only 4248 * if they were allocated from the resource manager returned by 4249 * BUS_GET_RMAN(). 4250 */ 4251 int 4252 bus_generic_rman_adjust_resource(device_t dev, device_t child, 4253 struct resource *r, rman_res_t start, rman_res_t end) 4254 { 4255 struct rman *rm; 4256 4257 rm = BUS_GET_RMAN(dev, rman_get_type(r), rman_get_flags(r)); 4258 if (rm == NULL) 4259 return (ENXIO); 4260 if (!rman_is_region_manager(r, rm)) 4261 return (EINVAL); 4262 return (rman_adjust_resource(r, start, end)); 4263 } 4264 4265 /** 4266 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4267 * 4268 * This implementation of BUS_RELEASE_RESOURCE() releases resources 4269 * allocated by bus_generic_rman_alloc_resource. 4270 */ 4271 int 4272 bus_generic_rman_release_resource(device_t dev, device_t child, 4273 struct resource *r) 4274 { 4275 #ifdef INVARIANTS 4276 struct rman *rm; 4277 #endif 4278 int error; 4279 4280 #ifdef INVARIANTS 4281 rm = BUS_GET_RMAN(dev, rman_get_type(r), rman_get_flags(r)); 4282 KASSERT(rman_is_region_manager(r, rm), 4283 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4284 #endif 4285 4286 if (rman_get_flags(r) & RF_ACTIVE) { 4287 error = bus_deactivate_resource(child, r); 4288 if (error != 0) 4289 return (error); 4290 } 4291 return (rman_release_resource(r)); 4292 } 4293 4294 /** 4295 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 4296 * 4297 * This implementation of BUS_ACTIVATE_RESOURCE() activates resources 4298 * allocated by bus_generic_rman_alloc_resource. 4299 */ 4300 int 4301 bus_generic_rman_activate_resource(device_t dev, device_t child, 4302 struct resource *r) 4303 { 4304 struct resource_map map; 4305 #ifdef INVARIANTS 4306 struct rman *rm; 4307 #endif 4308 int error, type; 4309 4310 type = rman_get_type(r); 4311 #ifdef INVARIANTS 4312 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r)); 4313 KASSERT(rman_is_region_manager(r, rm), 4314 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4315 #endif 4316 4317 error = rman_activate_resource(r); 4318 if (error != 0) 4319 return (error); 4320 4321 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 4322 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 4323 error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map); 4324 if (error != 0) { 4325 rman_deactivate_resource(r); 4326 return (error); 4327 } 4328 4329 rman_set_mapping(r, &map); 4330 } 4331 return (0); 4332 } 4333 4334 /** 4335 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 4336 * 4337 * This implementation of BUS_DEACTIVATE_RESOURCE() deactivates 4338 * resources allocated by bus_generic_rman_alloc_resource. 4339 */ 4340 int 4341 bus_generic_rman_deactivate_resource(device_t dev, device_t child, 4342 struct resource *r) 4343 { 4344 struct resource_map map; 4345 #ifdef INVARIANTS 4346 struct rman *rm; 4347 #endif 4348 int error, type; 4349 4350 type = rman_get_type(r); 4351 #ifdef INVARIANTS 4352 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r)); 4353 KASSERT(rman_is_region_manager(r, rm), 4354 ("%s: rman %p doesn't match for resource %p", __func__, rm, r)); 4355 #endif 4356 4357 error = rman_deactivate_resource(r); 4358 if (error != 0) 4359 return (error); 4360 4361 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 4362 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 4363 rman_get_mapping(r, &map); 4364 BUS_UNMAP_RESOURCE(dev, child, r, &map); 4365 } 4366 return (0); 4367 } 4368 4369 /** 4370 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 4371 * 4372 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 4373 * BUS_CHILD_PRESENT() method of the parent of @p dev. 4374 */ 4375 int 4376 bus_generic_child_present(device_t dev, device_t child) 4377 { 4378 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 4379 } 4380 4381 /** 4382 * @brief Helper function for implementing BUS_GET_DOMAIN(). 4383 * 4384 * This simple implementation of BUS_GET_DOMAIN() calls the 4385 * BUS_GET_DOMAIN() method of the parent of @p dev. If @p dev 4386 * does not have a parent, the function fails with ENOENT. 4387 */ 4388 int 4389 bus_generic_get_domain(device_t dev, device_t child, int *domain) 4390 { 4391 if (dev->parent) 4392 return (BUS_GET_DOMAIN(dev->parent, dev, domain)); 4393 4394 return (ENOENT); 4395 } 4396 4397 /** 4398 * @brief Helper function to implement normal BUS_GET_DEVICE_PATH() 4399 * 4400 * This function knows how to (a) pass the request up the tree if there's 4401 * a parent and (b) Knows how to supply a FreeBSD locator. 4402 * 4403 * @param bus bus in the walk up the tree 4404 * @param child leaf node to print information about 4405 * @param locator BUS_LOCATOR_xxx string for locator 4406 * @param sb Buffer to print information into 4407 */ 4408 int 4409 bus_generic_get_device_path(device_t bus, device_t child, const char *locator, 4410 struct sbuf *sb) 4411 { 4412 int rv = 0; 4413 device_t parent; 4414 4415 /* 4416 * We don't recurse on ACPI since either we know the handle for the 4417 * device or we don't. And if we're in the generic routine, we don't 4418 * have a ACPI override. All other locators build up a path by having 4419 * their parents create a path and then adding the path element for this 4420 * node. That's why we recurse with parent, bus rather than the typical 4421 * parent, child: each spot in the tree is independent of what our child 4422 * will do with this path. 4423 */ 4424 parent = device_get_parent(bus); 4425 if (parent != NULL && strcmp(locator, BUS_LOCATOR_ACPI) != 0) { 4426 rv = BUS_GET_DEVICE_PATH(parent, bus, locator, sb); 4427 } 4428 if (strcmp(locator, BUS_LOCATOR_FREEBSD) == 0) { 4429 if (rv == 0) { 4430 sbuf_printf(sb, "/%s", device_get_nameunit(child)); 4431 } 4432 return (rv); 4433 } 4434 /* 4435 * Don't know what to do. So assume we do nothing. Not sure that's 4436 * the right thing, but keeps us from having a big list here. 4437 */ 4438 return (0); 4439 } 4440 4441 4442 /** 4443 * @brief Helper function for implementing BUS_RESCAN(). 4444 * 4445 * This null implementation of BUS_RESCAN() always fails to indicate 4446 * the bus does not support rescanning. 4447 */ 4448 int 4449 bus_null_rescan(device_t dev) 4450 { 4451 return (ENODEV); 4452 } 4453 4454 /* 4455 * Some convenience functions to make it easier for drivers to use the 4456 * resource-management functions. All these really do is hide the 4457 * indirection through the parent's method table, making for slightly 4458 * less-wordy code. In the future, it might make sense for this code 4459 * to maintain some sort of a list of resources allocated by each device. 4460 */ 4461 4462 int 4463 bus_alloc_resources(device_t dev, struct resource_spec *rs, 4464 struct resource **res) 4465 { 4466 int i; 4467 4468 for (i = 0; rs[i].type != -1; i++) 4469 res[i] = NULL; 4470 for (i = 0; rs[i].type != -1; i++) { 4471 res[i] = bus_alloc_resource_any(dev, 4472 rs[i].type, &rs[i].rid, rs[i].flags); 4473 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 4474 bus_release_resources(dev, rs, res); 4475 return (ENXIO); 4476 } 4477 } 4478 return (0); 4479 } 4480 4481 void 4482 bus_release_resources(device_t dev, const struct resource_spec *rs, 4483 struct resource **res) 4484 { 4485 int i; 4486 4487 for (i = 0; rs[i].type != -1; i++) 4488 if (res[i] != NULL) { 4489 bus_release_resource( 4490 dev, rs[i].type, rs[i].rid, res[i]); 4491 res[i] = NULL; 4492 } 4493 } 4494 4495 /** 4496 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 4497 * 4498 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 4499 * parent of @p dev. 4500 */ 4501 struct resource * 4502 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, 4503 rman_res_t end, rman_res_t count, u_int flags) 4504 { 4505 struct resource *res; 4506 4507 if (dev->parent == NULL) 4508 return (NULL); 4509 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 4510 count, flags); 4511 return (res); 4512 } 4513 4514 /** 4515 * @brief Wrapper function for BUS_ADJUST_RESOURCE(). 4516 * 4517 * This function simply calls the BUS_ADJUST_RESOURCE() method of the 4518 * parent of @p dev. 4519 */ 4520 int 4521 bus_adjust_resource(device_t dev, struct resource *r, rman_res_t start, 4522 rman_res_t end) 4523 { 4524 if (dev->parent == NULL) 4525 return (EINVAL); 4526 return (BUS_ADJUST_RESOURCE(dev->parent, dev, r, start, end)); 4527 } 4528 4529 int 4530 bus_adjust_resource_old(device_t dev, int type __unused, struct resource *r, 4531 rman_res_t start, rman_res_t end) 4532 { 4533 return (bus_adjust_resource(dev, r, start, end)); 4534 } 4535 4536 /** 4537 * @brief Wrapper function for BUS_TRANSLATE_RESOURCE(). 4538 * 4539 * This function simply calls the BUS_TRANSLATE_RESOURCE() method of the 4540 * parent of @p dev. 4541 */ 4542 int 4543 bus_translate_resource(device_t dev, int type, rman_res_t start, 4544 rman_res_t *newstart) 4545 { 4546 if (dev->parent == NULL) 4547 return (EINVAL); 4548 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, newstart)); 4549 } 4550 4551 /** 4552 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 4553 * 4554 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 4555 * parent of @p dev. 4556 */ 4557 int 4558 bus_activate_resource(device_t dev, struct resource *r) 4559 { 4560 if (dev->parent == NULL) 4561 return (EINVAL); 4562 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, r)); 4563 } 4564 4565 int 4566 bus_activate_resource_old(device_t dev, int type, int rid, struct resource *r) 4567 { 4568 return (bus_activate_resource(dev, r)); 4569 } 4570 4571 /** 4572 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 4573 * 4574 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 4575 * parent of @p dev. 4576 */ 4577 int 4578 bus_deactivate_resource(device_t dev, struct resource *r) 4579 { 4580 if (dev->parent == NULL) 4581 return (EINVAL); 4582 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, r)); 4583 } 4584 4585 int 4586 bus_deactivate_resource_old(device_t dev, int type, int rid, struct resource *r) 4587 { 4588 return (bus_deactivate_resource(dev, r)); 4589 } 4590 4591 /** 4592 * @brief Wrapper function for BUS_MAP_RESOURCE(). 4593 * 4594 * This function simply calls the BUS_MAP_RESOURCE() method of the 4595 * parent of @p dev. 4596 */ 4597 int 4598 bus_map_resource(device_t dev, struct resource *r, 4599 struct resource_map_request *args, struct resource_map *map) 4600 { 4601 if (dev->parent == NULL) 4602 return (EINVAL); 4603 return (BUS_MAP_RESOURCE(dev->parent, dev, r, args, map)); 4604 } 4605 4606 int 4607 bus_map_resource_old(device_t dev, int type, struct resource *r, 4608 struct resource_map_request *args, struct resource_map *map) 4609 { 4610 return (bus_map_resource(dev, r, args, map)); 4611 } 4612 4613 /** 4614 * @brief Wrapper function for BUS_UNMAP_RESOURCE(). 4615 * 4616 * This function simply calls the BUS_UNMAP_RESOURCE() method of the 4617 * parent of @p dev. 4618 */ 4619 int 4620 bus_unmap_resource(device_t dev, struct resource *r, struct resource_map *map) 4621 { 4622 if (dev->parent == NULL) 4623 return (EINVAL); 4624 return (BUS_UNMAP_RESOURCE(dev->parent, dev, r, map)); 4625 } 4626 4627 int 4628 bus_unmap_resource_old(device_t dev, int type, struct resource *r, 4629 struct resource_map *map) 4630 { 4631 return (bus_unmap_resource(dev, r, map)); 4632 } 4633 4634 /** 4635 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 4636 * 4637 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 4638 * parent of @p dev. 4639 */ 4640 int 4641 bus_release_resource(device_t dev, struct resource *r) 4642 { 4643 int rv; 4644 4645 if (dev->parent == NULL) 4646 return (EINVAL); 4647 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, r); 4648 return (rv); 4649 } 4650 4651 int 4652 bus_release_resource_old(device_t dev, int type, int rid, struct resource *r) 4653 { 4654 return (bus_release_resource(dev, r)); 4655 } 4656 4657 /** 4658 * @brief Wrapper function for BUS_SETUP_INTR(). 4659 * 4660 * This function simply calls the BUS_SETUP_INTR() method of the 4661 * parent of @p dev. 4662 */ 4663 int 4664 bus_setup_intr(device_t dev, struct resource *r, int flags, 4665 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) 4666 { 4667 int error; 4668 4669 if (dev->parent == NULL) 4670 return (EINVAL); 4671 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler, 4672 arg, cookiep); 4673 if (error != 0) 4674 return (error); 4675 if (handler != NULL && !(flags & INTR_MPSAFE)) 4676 device_printf(dev, "[GIANT-LOCKED]\n"); 4677 return (0); 4678 } 4679 4680 /** 4681 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 4682 * 4683 * This function simply calls the BUS_TEARDOWN_INTR() method of the 4684 * parent of @p dev. 4685 */ 4686 int 4687 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 4688 { 4689 if (dev->parent == NULL) 4690 return (EINVAL); 4691 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 4692 } 4693 4694 /** 4695 * @brief Wrapper function for BUS_SUSPEND_INTR(). 4696 * 4697 * This function simply calls the BUS_SUSPEND_INTR() method of the 4698 * parent of @p dev. 4699 */ 4700 int 4701 bus_suspend_intr(device_t dev, struct resource *r) 4702 { 4703 if (dev->parent == NULL) 4704 return (EINVAL); 4705 return (BUS_SUSPEND_INTR(dev->parent, dev, r)); 4706 } 4707 4708 /** 4709 * @brief Wrapper function for BUS_RESUME_INTR(). 4710 * 4711 * This function simply calls the BUS_RESUME_INTR() method of the 4712 * parent of @p dev. 4713 */ 4714 int 4715 bus_resume_intr(device_t dev, struct resource *r) 4716 { 4717 if (dev->parent == NULL) 4718 return (EINVAL); 4719 return (BUS_RESUME_INTR(dev->parent, dev, r)); 4720 } 4721 4722 /** 4723 * @brief Wrapper function for BUS_BIND_INTR(). 4724 * 4725 * This function simply calls the BUS_BIND_INTR() method of the 4726 * parent of @p dev. 4727 */ 4728 int 4729 bus_bind_intr(device_t dev, struct resource *r, int cpu) 4730 { 4731 if (dev->parent == NULL) 4732 return (EINVAL); 4733 return (BUS_BIND_INTR(dev->parent, dev, r, cpu)); 4734 } 4735 4736 /** 4737 * @brief Wrapper function for BUS_DESCRIBE_INTR(). 4738 * 4739 * This function first formats the requested description into a 4740 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of 4741 * the parent of @p dev. 4742 */ 4743 int 4744 bus_describe_intr(device_t dev, struct resource *irq, void *cookie, 4745 const char *fmt, ...) 4746 { 4747 va_list ap; 4748 char descr[MAXCOMLEN + 1]; 4749 4750 if (dev->parent == NULL) 4751 return (EINVAL); 4752 va_start(ap, fmt); 4753 vsnprintf(descr, sizeof(descr), fmt, ap); 4754 va_end(ap); 4755 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr)); 4756 } 4757 4758 /** 4759 * @brief Wrapper function for BUS_SET_RESOURCE(). 4760 * 4761 * This function simply calls the BUS_SET_RESOURCE() method of the 4762 * parent of @p dev. 4763 */ 4764 int 4765 bus_set_resource(device_t dev, int type, int rid, 4766 rman_res_t start, rman_res_t count) 4767 { 4768 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 4769 start, count)); 4770 } 4771 4772 /** 4773 * @brief Wrapper function for BUS_GET_RESOURCE(). 4774 * 4775 * This function simply calls the BUS_GET_RESOURCE() method of the 4776 * parent of @p dev. 4777 */ 4778 int 4779 bus_get_resource(device_t dev, int type, int rid, 4780 rman_res_t *startp, rman_res_t *countp) 4781 { 4782 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4783 startp, countp)); 4784 } 4785 4786 /** 4787 * @brief Wrapper function for BUS_GET_RESOURCE(). 4788 * 4789 * This function simply calls the BUS_GET_RESOURCE() method of the 4790 * parent of @p dev and returns the start value. 4791 */ 4792 rman_res_t 4793 bus_get_resource_start(device_t dev, int type, int rid) 4794 { 4795 rman_res_t start; 4796 rman_res_t count; 4797 int error; 4798 4799 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4800 &start, &count); 4801 if (error) 4802 return (0); 4803 return (start); 4804 } 4805 4806 /** 4807 * @brief Wrapper function for BUS_GET_RESOURCE(). 4808 * 4809 * This function simply calls the BUS_GET_RESOURCE() method of the 4810 * parent of @p dev and returns the count value. 4811 */ 4812 rman_res_t 4813 bus_get_resource_count(device_t dev, int type, int rid) 4814 { 4815 rman_res_t start; 4816 rman_res_t count; 4817 int error; 4818 4819 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4820 &start, &count); 4821 if (error) 4822 return (0); 4823 return (count); 4824 } 4825 4826 /** 4827 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 4828 * 4829 * This function simply calls the BUS_DELETE_RESOURCE() method of the 4830 * parent of @p dev. 4831 */ 4832 void 4833 bus_delete_resource(device_t dev, int type, int rid) 4834 { 4835 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 4836 } 4837 4838 /** 4839 * @brief Wrapper function for BUS_CHILD_PRESENT(). 4840 * 4841 * This function simply calls the BUS_CHILD_PRESENT() method of the 4842 * parent of @p dev. 4843 */ 4844 int 4845 bus_child_present(device_t child) 4846 { 4847 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 4848 } 4849 4850 /** 4851 * @brief Wrapper function for BUS_CHILD_PNPINFO(). 4852 * 4853 * This function simply calls the BUS_CHILD_PNPINFO() method of the parent of @p 4854 * dev. 4855 */ 4856 int 4857 bus_child_pnpinfo(device_t child, struct sbuf *sb) 4858 { 4859 device_t parent; 4860 4861 parent = device_get_parent(child); 4862 if (parent == NULL) 4863 return (0); 4864 return (BUS_CHILD_PNPINFO(parent, child, sb)); 4865 } 4866 4867 /** 4868 * @brief Generic implementation that does nothing for bus_child_pnpinfo 4869 * 4870 * This function has the right signature and returns 0 since the sbuf is passed 4871 * to us to append to. 4872 */ 4873 int 4874 bus_generic_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb) 4875 { 4876 return (0); 4877 } 4878 4879 /** 4880 * @brief Wrapper function for BUS_CHILD_LOCATION(). 4881 * 4882 * This function simply calls the BUS_CHILD_LOCATION() method of the parent of 4883 * @p dev. 4884 */ 4885 int 4886 bus_child_location(device_t child, struct sbuf *sb) 4887 { 4888 device_t parent; 4889 4890 parent = device_get_parent(child); 4891 if (parent == NULL) 4892 return (0); 4893 return (BUS_CHILD_LOCATION(parent, child, sb)); 4894 } 4895 4896 /** 4897 * @brief Generic implementation that does nothing for bus_child_location 4898 * 4899 * This function has the right signature and returns 0 since the sbuf is passed 4900 * to us to append to. 4901 */ 4902 int 4903 bus_generic_child_location(device_t dev, device_t child, struct sbuf *sb) 4904 { 4905 return (0); 4906 } 4907 4908 /** 4909 * @brief Wrapper function for BUS_GET_CPUS(). 4910 * 4911 * This function simply calls the BUS_GET_CPUS() method of the 4912 * parent of @p dev. 4913 */ 4914 int 4915 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset) 4916 { 4917 device_t parent; 4918 4919 parent = device_get_parent(dev); 4920 if (parent == NULL) 4921 return (EINVAL); 4922 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset)); 4923 } 4924 4925 /** 4926 * @brief Wrapper function for BUS_GET_DMA_TAG(). 4927 * 4928 * This function simply calls the BUS_GET_DMA_TAG() method of the 4929 * parent of @p dev. 4930 */ 4931 bus_dma_tag_t 4932 bus_get_dma_tag(device_t dev) 4933 { 4934 device_t parent; 4935 4936 parent = device_get_parent(dev); 4937 if (parent == NULL) 4938 return (NULL); 4939 return (BUS_GET_DMA_TAG(parent, dev)); 4940 } 4941 4942 /** 4943 * @brief Wrapper function for BUS_GET_BUS_TAG(). 4944 * 4945 * This function simply calls the BUS_GET_BUS_TAG() method of the 4946 * parent of @p dev. 4947 */ 4948 bus_space_tag_t 4949 bus_get_bus_tag(device_t dev) 4950 { 4951 device_t parent; 4952 4953 parent = device_get_parent(dev); 4954 if (parent == NULL) 4955 return ((bus_space_tag_t)0); 4956 return (BUS_GET_BUS_TAG(parent, dev)); 4957 } 4958 4959 /** 4960 * @brief Wrapper function for BUS_GET_DOMAIN(). 4961 * 4962 * This function simply calls the BUS_GET_DOMAIN() method of the 4963 * parent of @p dev. 4964 */ 4965 int 4966 bus_get_domain(device_t dev, int *domain) 4967 { 4968 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain)); 4969 } 4970 4971 /* Resume all devices and then notify userland that we're up again. */ 4972 static int 4973 root_resume(device_t dev) 4974 { 4975 int error; 4976 4977 error = bus_generic_resume(dev); 4978 if (error == 0) { 4979 devctl_notify("kernel", "power", "resume", NULL); 4980 } 4981 return (error); 4982 } 4983 4984 static int 4985 root_print_child(device_t dev, device_t child) 4986 { 4987 int retval = 0; 4988 4989 retval += bus_print_child_header(dev, child); 4990 retval += printf("\n"); 4991 4992 return (retval); 4993 } 4994 4995 static int 4996 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 4997 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 4998 { 4999 /* 5000 * If an interrupt mapping gets to here something bad has happened. 5001 */ 5002 panic("root_setup_intr"); 5003 } 5004 5005 /* 5006 * If we get here, assume that the device is permanent and really is 5007 * present in the system. Removable bus drivers are expected to intercept 5008 * this call long before it gets here. We return -1 so that drivers that 5009 * really care can check vs -1 or some ERRNO returned higher in the food 5010 * chain. 5011 */ 5012 static int 5013 root_child_present(device_t dev, device_t child) 5014 { 5015 return (-1); 5016 } 5017 5018 static int 5019 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize, 5020 cpuset_t *cpuset) 5021 { 5022 switch (op) { 5023 case INTR_CPUS: 5024 /* Default to returning the set of all CPUs. */ 5025 if (setsize != sizeof(cpuset_t)) 5026 return (EINVAL); 5027 *cpuset = all_cpus; 5028 return (0); 5029 default: 5030 return (EINVAL); 5031 } 5032 } 5033 5034 static kobj_method_t root_methods[] = { 5035 /* Device interface */ 5036 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 5037 KOBJMETHOD(device_suspend, bus_generic_suspend), 5038 KOBJMETHOD(device_resume, root_resume), 5039 5040 /* Bus interface */ 5041 KOBJMETHOD(bus_print_child, root_print_child), 5042 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 5043 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 5044 KOBJMETHOD(bus_setup_intr, root_setup_intr), 5045 KOBJMETHOD(bus_child_present, root_child_present), 5046 KOBJMETHOD(bus_get_cpus, root_get_cpus), 5047 5048 KOBJMETHOD_END 5049 }; 5050 5051 static driver_t root_driver = { 5052 "root", 5053 root_methods, 5054 1, /* no softc */ 5055 }; 5056 5057 device_t root_bus; 5058 devclass_t root_devclass; 5059 5060 static int 5061 root_bus_module_handler(module_t mod, int what, void* arg) 5062 { 5063 switch (what) { 5064 case MOD_LOAD: 5065 TAILQ_INIT(&bus_data_devices); 5066 kobj_class_compile((kobj_class_t) &root_driver); 5067 root_bus = make_device(NULL, "root", 0); 5068 root_bus->desc = "System root bus"; 5069 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 5070 root_bus->driver = &root_driver; 5071 root_bus->state = DS_ATTACHED; 5072 root_devclass = devclass_find_internal("root", NULL, FALSE); 5073 devctl2_init(); 5074 return (0); 5075 5076 case MOD_SHUTDOWN: 5077 device_shutdown(root_bus); 5078 return (0); 5079 default: 5080 return (EOPNOTSUPP); 5081 } 5082 5083 return (0); 5084 } 5085 5086 static moduledata_t root_bus_mod = { 5087 "rootbus", 5088 root_bus_module_handler, 5089 NULL 5090 }; 5091 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 5092 5093 /** 5094 * @brief Automatically configure devices 5095 * 5096 * This function begins the autoconfiguration process by calling 5097 * device_probe_and_attach() for each child of the @c root0 device. 5098 */ 5099 void 5100 root_bus_configure(void) 5101 { 5102 PDEBUG((".")); 5103 5104 /* Eventually this will be split up, but this is sufficient for now. */ 5105 bus_set_pass(BUS_PASS_DEFAULT); 5106 } 5107 5108 /** 5109 * @brief Module handler for registering device drivers 5110 * 5111 * This module handler is used to automatically register device 5112 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 5113 * devclass_add_driver() for the driver described by the 5114 * driver_module_data structure pointed to by @p arg 5115 */ 5116 int 5117 driver_module_handler(module_t mod, int what, void *arg) 5118 { 5119 struct driver_module_data *dmd; 5120 devclass_t bus_devclass; 5121 kobj_class_t driver; 5122 int error, pass; 5123 5124 dmd = (struct driver_module_data *)arg; 5125 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 5126 error = 0; 5127 5128 switch (what) { 5129 case MOD_LOAD: 5130 if (dmd->dmd_chainevh) 5131 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5132 5133 pass = dmd->dmd_pass; 5134 driver = dmd->dmd_driver; 5135 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 5136 DRIVERNAME(driver), dmd->dmd_busname, pass)); 5137 error = devclass_add_driver(bus_devclass, driver, pass, 5138 dmd->dmd_devclass); 5139 break; 5140 5141 case MOD_UNLOAD: 5142 PDEBUG(("Unloading module: driver %s from bus %s", 5143 DRIVERNAME(dmd->dmd_driver), 5144 dmd->dmd_busname)); 5145 error = devclass_delete_driver(bus_devclass, 5146 dmd->dmd_driver); 5147 5148 if (!error && dmd->dmd_chainevh) 5149 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5150 break; 5151 case MOD_QUIESCE: 5152 PDEBUG(("Quiesce module: driver %s from bus %s", 5153 DRIVERNAME(dmd->dmd_driver), 5154 dmd->dmd_busname)); 5155 error = devclass_quiesce_driver(bus_devclass, 5156 dmd->dmd_driver); 5157 5158 if (!error && dmd->dmd_chainevh) 5159 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5160 break; 5161 default: 5162 error = EOPNOTSUPP; 5163 break; 5164 } 5165 5166 return (error); 5167 } 5168 5169 /** 5170 * @brief Enumerate all hinted devices for this bus. 5171 * 5172 * Walks through the hints for this bus and calls the bus_hinted_child 5173 * routine for each one it fines. It searches first for the specific 5174 * bus that's being probed for hinted children (eg isa0), and then for 5175 * generic children (eg isa). 5176 * 5177 * @param dev bus device to enumerate 5178 */ 5179 void 5180 bus_enumerate_hinted_children(device_t bus) 5181 { 5182 int i; 5183 const char *dname, *busname; 5184 int dunit; 5185 5186 /* 5187 * enumerate all devices on the specific bus 5188 */ 5189 busname = device_get_nameunit(bus); 5190 i = 0; 5191 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5192 BUS_HINTED_CHILD(bus, dname, dunit); 5193 5194 /* 5195 * and all the generic ones. 5196 */ 5197 busname = device_get_name(bus); 5198 i = 0; 5199 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5200 BUS_HINTED_CHILD(bus, dname, dunit); 5201 } 5202 5203 #ifdef BUS_DEBUG 5204 5205 /* the _short versions avoid iteration by not calling anything that prints 5206 * more than oneliners. I love oneliners. 5207 */ 5208 5209 static void 5210 print_device_short(device_t dev, int indent) 5211 { 5212 if (!dev) 5213 return; 5214 5215 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 5216 dev->unit, dev->desc, 5217 (dev->parent? "":"no "), 5218 (TAILQ_EMPTY(&dev->children)? "no ":""), 5219 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 5220 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 5221 (dev->flags&DF_WILDCARD? "wildcard,":""), 5222 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 5223 (dev->flags&DF_SUSPENDED? "suspended,":""), 5224 (dev->ivars? "":"no "), 5225 (dev->softc? "":"no "), 5226 dev->busy)); 5227 } 5228 5229 static void 5230 print_device(device_t dev, int indent) 5231 { 5232 if (!dev) 5233 return; 5234 5235 print_device_short(dev, indent); 5236 5237 indentprintf(("Parent:\n")); 5238 print_device_short(dev->parent, indent+1); 5239 indentprintf(("Driver:\n")); 5240 print_driver_short(dev->driver, indent+1); 5241 indentprintf(("Devclass:\n")); 5242 print_devclass_short(dev->devclass, indent+1); 5243 } 5244 5245 void 5246 print_device_tree_short(device_t dev, int indent) 5247 /* print the device and all its children (indented) */ 5248 { 5249 device_t child; 5250 5251 if (!dev) 5252 return; 5253 5254 print_device_short(dev, indent); 5255 5256 TAILQ_FOREACH(child, &dev->children, link) { 5257 print_device_tree_short(child, indent+1); 5258 } 5259 } 5260 5261 void 5262 print_device_tree(device_t dev, int indent) 5263 /* print the device and all its children (indented) */ 5264 { 5265 device_t child; 5266 5267 if (!dev) 5268 return; 5269 5270 print_device(dev, indent); 5271 5272 TAILQ_FOREACH(child, &dev->children, link) { 5273 print_device_tree(child, indent+1); 5274 } 5275 } 5276 5277 static void 5278 print_driver_short(driver_t *driver, int indent) 5279 { 5280 if (!driver) 5281 return; 5282 5283 indentprintf(("driver %s: softc size = %zd\n", 5284 driver->name, driver->size)); 5285 } 5286 5287 static void 5288 print_driver(driver_t *driver, int indent) 5289 { 5290 if (!driver) 5291 return; 5292 5293 print_driver_short(driver, indent); 5294 } 5295 5296 static void 5297 print_driver_list(driver_list_t drivers, int indent) 5298 { 5299 driverlink_t driver; 5300 5301 TAILQ_FOREACH(driver, &drivers, link) { 5302 print_driver(driver->driver, indent); 5303 } 5304 } 5305 5306 static void 5307 print_devclass_short(devclass_t dc, int indent) 5308 { 5309 if ( !dc ) 5310 return; 5311 5312 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 5313 } 5314 5315 static void 5316 print_devclass(devclass_t dc, int indent) 5317 { 5318 int i; 5319 5320 if ( !dc ) 5321 return; 5322 5323 print_devclass_short(dc, indent); 5324 indentprintf(("Drivers:\n")); 5325 print_driver_list(dc->drivers, indent+1); 5326 5327 indentprintf(("Devices:\n")); 5328 for (i = 0; i < dc->maxunit; i++) 5329 if (dc->devices[i]) 5330 print_device(dc->devices[i], indent+1); 5331 } 5332 5333 void 5334 print_devclass_list_short(void) 5335 { 5336 devclass_t dc; 5337 5338 printf("Short listing of devclasses, drivers & devices:\n"); 5339 TAILQ_FOREACH(dc, &devclasses, link) { 5340 print_devclass_short(dc, 0); 5341 } 5342 } 5343 5344 void 5345 print_devclass_list(void) 5346 { 5347 devclass_t dc; 5348 5349 printf("Full listing of devclasses, drivers & devices:\n"); 5350 TAILQ_FOREACH(dc, &devclasses, link) { 5351 print_devclass(dc, 0); 5352 } 5353 } 5354 5355 #endif 5356 5357 /* 5358 * User-space access to the device tree. 5359 * 5360 * We implement a small set of nodes: 5361 * 5362 * hw.bus Single integer read method to obtain the 5363 * current generation count. 5364 * hw.bus.devices Reads the entire device tree in flat space. 5365 * hw.bus.rman Resource manager interface 5366 * 5367 * We might like to add the ability to scan devclasses and/or drivers to 5368 * determine what else is currently loaded/available. 5369 */ 5370 5371 static int 5372 sysctl_bus_info(SYSCTL_HANDLER_ARGS) 5373 { 5374 struct u_businfo ubus; 5375 5376 ubus.ub_version = BUS_USER_VERSION; 5377 ubus.ub_generation = bus_data_generation; 5378 5379 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 5380 } 5381 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD | 5382 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo", 5383 "bus-related data"); 5384 5385 static int 5386 sysctl_devices(SYSCTL_HANDLER_ARGS) 5387 { 5388 struct sbuf sb; 5389 int *name = (int *)arg1; 5390 u_int namelen = arg2; 5391 int index; 5392 device_t dev; 5393 struct u_device *udev; 5394 int error; 5395 5396 if (namelen != 2) 5397 return (EINVAL); 5398 5399 if (bus_data_generation_check(name[0])) 5400 return (EINVAL); 5401 5402 index = name[1]; 5403 5404 /* 5405 * Scan the list of devices, looking for the requested index. 5406 */ 5407 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5408 if (index-- == 0) 5409 break; 5410 } 5411 if (dev == NULL) 5412 return (ENOENT); 5413 5414 /* 5415 * Populate the return item, careful not to overflow the buffer. 5416 */ 5417 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO); 5418 udev->dv_handle = (uintptr_t)dev; 5419 udev->dv_parent = (uintptr_t)dev->parent; 5420 udev->dv_devflags = dev->devflags; 5421 udev->dv_flags = dev->flags; 5422 udev->dv_state = dev->state; 5423 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN); 5424 if (dev->nameunit != NULL) 5425 sbuf_cat(&sb, dev->nameunit); 5426 sbuf_putc(&sb, '\0'); 5427 if (dev->desc != NULL) 5428 sbuf_cat(&sb, dev->desc); 5429 sbuf_putc(&sb, '\0'); 5430 if (dev->driver != NULL) 5431 sbuf_cat(&sb, dev->driver->name); 5432 sbuf_putc(&sb, '\0'); 5433 bus_child_pnpinfo(dev, &sb); 5434 sbuf_putc(&sb, '\0'); 5435 bus_child_location(dev, &sb); 5436 sbuf_putc(&sb, '\0'); 5437 error = sbuf_finish(&sb); 5438 if (error == 0) 5439 error = SYSCTL_OUT(req, udev, sizeof(*udev)); 5440 sbuf_delete(&sb); 5441 free(udev, M_BUS); 5442 return (error); 5443 } 5444 5445 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, 5446 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices, 5447 "system device tree"); 5448 5449 int 5450 bus_data_generation_check(int generation) 5451 { 5452 if (generation != bus_data_generation) 5453 return (1); 5454 5455 /* XXX generate optimised lists here? */ 5456 return (0); 5457 } 5458 5459 void 5460 bus_data_generation_update(void) 5461 { 5462 atomic_add_int(&bus_data_generation, 1); 5463 } 5464 5465 int 5466 bus_free_resource(device_t dev, int type, struct resource *r) 5467 { 5468 if (r == NULL) 5469 return (0); 5470 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 5471 } 5472 5473 device_t 5474 device_lookup_by_name(const char *name) 5475 { 5476 device_t dev; 5477 5478 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5479 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0) 5480 return (dev); 5481 } 5482 return (NULL); 5483 } 5484 5485 /* 5486 * /dev/devctl2 implementation. The existing /dev/devctl device has 5487 * implicit semantics on open, so it could not be reused for this. 5488 * Another option would be to call this /dev/bus? 5489 */ 5490 static int 5491 find_device(struct devreq *req, device_t *devp) 5492 { 5493 device_t dev; 5494 5495 /* 5496 * First, ensure that the name is nul terminated. 5497 */ 5498 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL) 5499 return (EINVAL); 5500 5501 /* 5502 * Second, try to find an attached device whose name matches 5503 * 'name'. 5504 */ 5505 dev = device_lookup_by_name(req->dr_name); 5506 if (dev != NULL) { 5507 *devp = dev; 5508 return (0); 5509 } 5510 5511 /* Finally, give device enumerators a chance. */ 5512 dev = NULL; 5513 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev); 5514 if (dev == NULL) 5515 return (ENOENT); 5516 *devp = dev; 5517 return (0); 5518 } 5519 5520 static bool 5521 driver_exists(device_t bus, const char *driver) 5522 { 5523 devclass_t dc; 5524 5525 for (dc = bus->devclass; dc != NULL; dc = dc->parent) { 5526 if (devclass_find_driver_internal(dc, driver) != NULL) 5527 return (true); 5528 } 5529 return (false); 5530 } 5531 5532 static void 5533 device_gen_nomatch(device_t dev) 5534 { 5535 device_t child; 5536 5537 if (dev->flags & DF_NEEDNOMATCH && 5538 dev->state == DS_NOTPRESENT) { 5539 device_handle_nomatch(dev); 5540 } 5541 dev->flags &= ~DF_NEEDNOMATCH; 5542 TAILQ_FOREACH(child, &dev->children, link) { 5543 device_gen_nomatch(child); 5544 } 5545 } 5546 5547 static void 5548 device_do_deferred_actions(void) 5549 { 5550 devclass_t dc; 5551 driverlink_t dl; 5552 5553 /* 5554 * Walk through the devclasses to find all the drivers we've tagged as 5555 * deferred during the freeze and call the driver added routines. They 5556 * have already been added to the lists in the background, so the driver 5557 * added routines that trigger a probe will have all the right bidders 5558 * for the probe auction. 5559 */ 5560 TAILQ_FOREACH(dc, &devclasses, link) { 5561 TAILQ_FOREACH(dl, &dc->drivers, link) { 5562 if (dl->flags & DL_DEFERRED_PROBE) { 5563 devclass_driver_added(dc, dl->driver); 5564 dl->flags &= ~DL_DEFERRED_PROBE; 5565 } 5566 } 5567 } 5568 5569 /* 5570 * We also defer no-match events during a freeze. Walk the tree and 5571 * generate all the pent-up events that are still relevant. 5572 */ 5573 device_gen_nomatch(root_bus); 5574 bus_data_generation_update(); 5575 } 5576 5577 static int 5578 device_get_path(device_t dev, const char *locator, struct sbuf *sb) 5579 { 5580 device_t parent; 5581 int error; 5582 5583 KASSERT(sb != NULL, ("sb is NULL")); 5584 parent = device_get_parent(dev); 5585 if (parent == NULL) { 5586 error = sbuf_putc(sb, '/'); 5587 } else { 5588 error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb); 5589 if (error == 0) { 5590 error = sbuf_error(sb); 5591 if (error == 0 && sbuf_len(sb) <= 1) 5592 error = EIO; 5593 } 5594 } 5595 sbuf_finish(sb); 5596 return (error); 5597 } 5598 5599 static int 5600 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, 5601 struct thread *td) 5602 { 5603 struct devreq *req; 5604 device_t dev; 5605 int error, old; 5606 5607 /* Locate the device to control. */ 5608 bus_topo_lock(); 5609 req = (struct devreq *)data; 5610 switch (cmd) { 5611 case DEV_ATTACH: 5612 case DEV_DETACH: 5613 case DEV_ENABLE: 5614 case DEV_DISABLE: 5615 case DEV_SUSPEND: 5616 case DEV_RESUME: 5617 case DEV_SET_DRIVER: 5618 case DEV_CLEAR_DRIVER: 5619 case DEV_RESCAN: 5620 case DEV_DELETE: 5621 case DEV_RESET: 5622 error = priv_check(td, PRIV_DRIVER); 5623 if (error == 0) 5624 error = find_device(req, &dev); 5625 break; 5626 case DEV_FREEZE: 5627 case DEV_THAW: 5628 error = priv_check(td, PRIV_DRIVER); 5629 break; 5630 case DEV_GET_PATH: 5631 error = find_device(req, &dev); 5632 break; 5633 default: 5634 error = ENOTTY; 5635 break; 5636 } 5637 if (error) { 5638 bus_topo_unlock(); 5639 return (error); 5640 } 5641 5642 /* Perform the requested operation. */ 5643 switch (cmd) { 5644 case DEV_ATTACH: 5645 if (device_is_attached(dev)) 5646 error = EBUSY; 5647 else if (!device_is_enabled(dev)) 5648 error = ENXIO; 5649 else 5650 error = device_probe_and_attach(dev); 5651 break; 5652 case DEV_DETACH: 5653 if (!device_is_attached(dev)) { 5654 error = ENXIO; 5655 break; 5656 } 5657 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5658 error = device_quiesce(dev); 5659 if (error) 5660 break; 5661 } 5662 error = device_detach(dev); 5663 break; 5664 case DEV_ENABLE: 5665 if (device_is_enabled(dev)) { 5666 error = EBUSY; 5667 break; 5668 } 5669 5670 /* 5671 * If the device has been probed but not attached (e.g. 5672 * when it has been disabled by a loader hint), just 5673 * attach the device rather than doing a full probe. 5674 */ 5675 device_enable(dev); 5676 if (device_is_alive(dev)) { 5677 /* 5678 * If the device was disabled via a hint, clear 5679 * the hint. 5680 */ 5681 if (resource_disabled(dev->driver->name, dev->unit)) 5682 resource_unset_value(dev->driver->name, 5683 dev->unit, "disabled"); 5684 error = device_attach(dev); 5685 } else 5686 error = device_probe_and_attach(dev); 5687 break; 5688 case DEV_DISABLE: 5689 if (!device_is_enabled(dev)) { 5690 error = ENXIO; 5691 break; 5692 } 5693 5694 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5695 error = device_quiesce(dev); 5696 if (error) 5697 break; 5698 } 5699 5700 /* 5701 * Force DF_FIXEDCLASS on around detach to preserve 5702 * the existing name. 5703 */ 5704 old = dev->flags; 5705 dev->flags |= DF_FIXEDCLASS; 5706 error = device_detach(dev); 5707 if (!(old & DF_FIXEDCLASS)) 5708 dev->flags &= ~DF_FIXEDCLASS; 5709 if (error == 0) 5710 device_disable(dev); 5711 break; 5712 case DEV_SUSPEND: 5713 if (device_is_suspended(dev)) { 5714 error = EBUSY; 5715 break; 5716 } 5717 if (device_get_parent(dev) == NULL) { 5718 error = EINVAL; 5719 break; 5720 } 5721 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev); 5722 break; 5723 case DEV_RESUME: 5724 if (!device_is_suspended(dev)) { 5725 error = EINVAL; 5726 break; 5727 } 5728 if (device_get_parent(dev) == NULL) { 5729 error = EINVAL; 5730 break; 5731 } 5732 error = BUS_RESUME_CHILD(device_get_parent(dev), dev); 5733 break; 5734 case DEV_SET_DRIVER: { 5735 devclass_t dc; 5736 char driver[128]; 5737 5738 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL); 5739 if (error) 5740 break; 5741 if (driver[0] == '\0') { 5742 error = EINVAL; 5743 break; 5744 } 5745 if (dev->devclass != NULL && 5746 strcmp(driver, dev->devclass->name) == 0) 5747 /* XXX: Could possibly force DF_FIXEDCLASS on? */ 5748 break; 5749 5750 /* 5751 * Scan drivers for this device's bus looking for at 5752 * least one matching driver. 5753 */ 5754 if (dev->parent == NULL) { 5755 error = EINVAL; 5756 break; 5757 } 5758 if (!driver_exists(dev->parent, driver)) { 5759 error = ENOENT; 5760 break; 5761 } 5762 dc = devclass_create(driver); 5763 if (dc == NULL) { 5764 error = ENOMEM; 5765 break; 5766 } 5767 5768 /* Detach device if necessary. */ 5769 if (device_is_attached(dev)) { 5770 if (req->dr_flags & DEVF_SET_DRIVER_DETACH) 5771 error = device_detach(dev); 5772 else 5773 error = EBUSY; 5774 if (error) 5775 break; 5776 } 5777 5778 /* Clear any previously-fixed device class and unit. */ 5779 if (dev->flags & DF_FIXEDCLASS) 5780 devclass_delete_device(dev->devclass, dev); 5781 dev->flags |= DF_WILDCARD; 5782 dev->unit = DEVICE_UNIT_ANY; 5783 5784 /* Force the new device class. */ 5785 error = devclass_add_device(dc, dev); 5786 if (error) 5787 break; 5788 dev->flags |= DF_FIXEDCLASS; 5789 error = device_probe_and_attach(dev); 5790 break; 5791 } 5792 case DEV_CLEAR_DRIVER: 5793 if (!(dev->flags & DF_FIXEDCLASS)) { 5794 error = 0; 5795 break; 5796 } 5797 if (device_is_attached(dev)) { 5798 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH) 5799 error = device_detach(dev); 5800 else 5801 error = EBUSY; 5802 if (error) 5803 break; 5804 } 5805 5806 dev->flags &= ~DF_FIXEDCLASS; 5807 dev->flags |= DF_WILDCARD; 5808 devclass_delete_device(dev->devclass, dev); 5809 error = device_probe_and_attach(dev); 5810 break; 5811 case DEV_RESCAN: 5812 if (!device_is_attached(dev)) { 5813 error = ENXIO; 5814 break; 5815 } 5816 error = BUS_RESCAN(dev); 5817 break; 5818 case DEV_DELETE: { 5819 device_t parent; 5820 5821 parent = device_get_parent(dev); 5822 if (parent == NULL) { 5823 error = EINVAL; 5824 break; 5825 } 5826 if (!(req->dr_flags & DEVF_FORCE_DELETE)) { 5827 if (bus_child_present(dev) != 0) { 5828 error = EBUSY; 5829 break; 5830 } 5831 } 5832 5833 error = device_delete_child(parent, dev); 5834 break; 5835 } 5836 case DEV_FREEZE: 5837 if (device_frozen) 5838 error = EBUSY; 5839 else 5840 device_frozen = true; 5841 break; 5842 case DEV_THAW: 5843 if (!device_frozen) 5844 error = EBUSY; 5845 else { 5846 device_do_deferred_actions(); 5847 device_frozen = false; 5848 } 5849 break; 5850 case DEV_RESET: 5851 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) { 5852 error = EINVAL; 5853 break; 5854 } 5855 if (device_get_parent(dev) == NULL) { 5856 error = EINVAL; 5857 break; 5858 } 5859 error = BUS_RESET_CHILD(device_get_parent(dev), dev, 5860 req->dr_flags); 5861 break; 5862 case DEV_GET_PATH: { 5863 struct sbuf *sb; 5864 char locator[64]; 5865 ssize_t len; 5866 5867 error = copyinstr(req->dr_buffer.buffer, locator, 5868 sizeof(locator), NULL); 5869 if (error != 0) 5870 break; 5871 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | 5872 SBUF_INCLUDENUL /* | SBUF_WAITOK */); 5873 error = device_get_path(dev, locator, sb); 5874 if (error == 0) { 5875 len = sbuf_len(sb); 5876 if (req->dr_buffer.length < len) { 5877 error = ENAMETOOLONG; 5878 } else { 5879 error = copyout(sbuf_data(sb), 5880 req->dr_buffer.buffer, len); 5881 } 5882 req->dr_buffer.length = len; 5883 } 5884 sbuf_delete(sb); 5885 break; 5886 } 5887 } 5888 bus_topo_unlock(); 5889 return (error); 5890 } 5891 5892 static struct cdevsw devctl2_cdevsw = { 5893 .d_version = D_VERSION, 5894 .d_ioctl = devctl2_ioctl, 5895 .d_name = "devctl2", 5896 }; 5897 5898 static void 5899 devctl2_init(void) 5900 { 5901 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL, 5902 UID_ROOT, GID_WHEEL, 0644, "devctl2"); 5903 } 5904 5905 /* 5906 * For maintaining device 'at' location info to avoid recomputing it 5907 */ 5908 struct device_location_node { 5909 const char *dln_locator; 5910 const char *dln_path; 5911 TAILQ_ENTRY(device_location_node) dln_link; 5912 }; 5913 typedef TAILQ_HEAD(device_location_list, device_location_node) device_location_list_t; 5914 5915 struct device_location_cache { 5916 device_location_list_t dlc_list; 5917 }; 5918 5919 5920 /* 5921 * Location cache for wired devices. 5922 */ 5923 device_location_cache_t * 5924 dev_wired_cache_init(void) 5925 { 5926 device_location_cache_t *dcp; 5927 5928 dcp = malloc(sizeof(*dcp), M_BUS, M_WAITOK | M_ZERO); 5929 TAILQ_INIT(&dcp->dlc_list); 5930 5931 return (dcp); 5932 } 5933 5934 void 5935 dev_wired_cache_fini(device_location_cache_t *dcp) 5936 { 5937 struct device_location_node *dln, *tdln; 5938 5939 TAILQ_FOREACH_SAFE(dln, &dcp->dlc_list, dln_link, tdln) { 5940 free(dln, M_BUS); 5941 } 5942 free(dcp, M_BUS); 5943 } 5944 5945 static struct device_location_node * 5946 dev_wired_cache_lookup(device_location_cache_t *dcp, const char *locator) 5947 { 5948 struct device_location_node *dln; 5949 5950 TAILQ_FOREACH(dln, &dcp->dlc_list, dln_link) { 5951 if (strcmp(locator, dln->dln_locator) == 0) 5952 return (dln); 5953 } 5954 5955 return (NULL); 5956 } 5957 5958 static struct device_location_node * 5959 dev_wired_cache_add(device_location_cache_t *dcp, const char *locator, const char *path) 5960 { 5961 struct device_location_node *dln; 5962 size_t loclen, pathlen; 5963 5964 loclen = strlen(locator) + 1; 5965 pathlen = strlen(path) + 1; 5966 dln = malloc(sizeof(*dln) + loclen + pathlen, M_BUS, M_WAITOK | M_ZERO); 5967 dln->dln_locator = (char *)(dln + 1); 5968 memcpy(__DECONST(char *, dln->dln_locator), locator, loclen); 5969 dln->dln_path = dln->dln_locator + loclen; 5970 memcpy(__DECONST(char *, dln->dln_path), path, pathlen); 5971 TAILQ_INSERT_HEAD(&dcp->dlc_list, dln, dln_link); 5972 5973 return (dln); 5974 } 5975 5976 bool 5977 dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, 5978 const char *at) 5979 { 5980 struct sbuf *sb; 5981 const char *cp; 5982 char locator[32]; 5983 int error, len; 5984 struct device_location_node *res; 5985 5986 cp = strchr(at, ':'); 5987 if (cp == NULL) 5988 return (false); 5989 len = cp - at; 5990 if (len > sizeof(locator) - 1) /* Skip too long locator */ 5991 return (false); 5992 memcpy(locator, at, len); 5993 locator[len] = '\0'; 5994 cp++; 5995 5996 error = 0; 5997 /* maybe cache this inside device_t and look that up, but not yet */ 5998 res = dev_wired_cache_lookup(dcp, locator); 5999 if (res == NULL) { 6000 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | 6001 SBUF_INCLUDENUL | SBUF_NOWAIT); 6002 if (sb != NULL) { 6003 error = device_get_path(dev, locator, sb); 6004 if (error == 0) { 6005 res = dev_wired_cache_add(dcp, locator, 6006 sbuf_data(sb)); 6007 } 6008 sbuf_delete(sb); 6009 } 6010 } 6011 if (error != 0 || res == NULL || res->dln_path == NULL) 6012 return (false); 6013 6014 return (strcmp(res->dln_path, cp) == 0); 6015 } 6016 6017 /* 6018 * APIs to manage deprecation and obsolescence. 6019 */ 6020 static int obsolete_panic = 0; 6021 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0, 6022 "Panic when obsolete features are used (0 = never, 1 = if obsolete, " 6023 "2 = if deprecated)"); 6024 6025 static void 6026 gone_panic(int major, int running, const char *msg) 6027 { 6028 switch (obsolete_panic) 6029 { 6030 case 0: 6031 return; 6032 case 1: 6033 if (running < major) 6034 return; 6035 /* FALLTHROUGH */ 6036 default: 6037 panic("%s", msg); 6038 } 6039 } 6040 6041 void 6042 _gone_in(int major, const char *msg) 6043 { 6044 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 6045 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 6046 printf("Obsolete code will be removed soon: %s\n", msg); 6047 else 6048 printf("Deprecated code (to be removed in FreeBSD %d): %s\n", 6049 major, msg); 6050 } 6051 6052 void 6053 _gone_in_dev(device_t dev, int major, const char *msg) 6054 { 6055 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 6056 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 6057 device_printf(dev, 6058 "Obsolete code will be removed soon: %s\n", msg); 6059 else 6060 device_printf(dev, 6061 "Deprecated code (to be removed in FreeBSD %d): %s\n", 6062 major, msg); 6063 } 6064 6065 #ifdef DDB 6066 DB_SHOW_COMMAND(device, db_show_device) 6067 { 6068 device_t dev; 6069 6070 if (!have_addr) 6071 return; 6072 6073 dev = (device_t)addr; 6074 6075 db_printf("name: %s\n", device_get_nameunit(dev)); 6076 db_printf(" driver: %s\n", DRIVERNAME(dev->driver)); 6077 db_printf(" class: %s\n", DEVCLANAME(dev->devclass)); 6078 db_printf(" addr: %p\n", dev); 6079 db_printf(" parent: %p\n", dev->parent); 6080 db_printf(" softc: %p\n", dev->softc); 6081 db_printf(" ivars: %p\n", dev->ivars); 6082 } 6083 6084 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices) 6085 { 6086 device_t dev; 6087 6088 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 6089 db_show_device((db_expr_t)dev, true, count, modif); 6090 } 6091 } 6092 #endif 6093