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