1 /* $NetBSD: subr_autoconf.c,v 1.137 2008/03/05 15:37:55 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 2000 Christopher G. Demetriou 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the 18 * NetBSD Project. See http://www.NetBSD.org/ for 19 * information about NetBSD. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )-- 35 */ 36 37 /* 38 * Copyright (c) 1992, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * This software was developed by the Computer Systems Engineering group 42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 43 * contributed to Berkeley. 44 * 45 * All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Lawrence Berkeley Laboratories. 49 * 50 * Redistribution and use in source and binary forms, with or without 51 * modification, are permitted provided that the following conditions 52 * are met: 53 * 1. Redistributions of source code must retain the above copyright 54 * notice, this list of conditions and the following disclaimer. 55 * 2. Redistributions in binary form must reproduce the above copyright 56 * notice, this list of conditions and the following disclaimer in the 57 * documentation and/or other materials provided with the distribution. 58 * 3. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL) 75 * 76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.137 2008/03/05 15:37:55 dyoung Exp $"); 81 82 #include "opt_multiprocessor.h" 83 #include "opt_ddb.h" 84 85 #include <sys/param.h> 86 #include <sys/device.h> 87 #include <sys/disklabel.h> 88 #include <sys/conf.h> 89 #include <sys/kauth.h> 90 #include <sys/malloc.h> 91 #include <sys/systm.h> 92 #include <sys/kernel.h> 93 #include <sys/errno.h> 94 #include <sys/proc.h> 95 #include <sys/reboot.h> 96 97 #include <sys/buf.h> 98 #include <sys/dirent.h> 99 #include <sys/vnode.h> 100 #include <sys/mount.h> 101 #include <sys/namei.h> 102 #include <sys/unistd.h> 103 #include <sys/fcntl.h> 104 #include <sys/lockf.h> 105 #include <sys/callout.h> 106 #include <sys/mutex.h> 107 #include <sys/condvar.h> 108 109 #include <sys/disk.h> 110 111 #include <machine/limits.h> 112 113 #include "opt_userconf.h" 114 #ifdef USERCONF 115 #include <sys/userconf.h> 116 #endif 117 118 #ifdef __i386__ 119 #include "opt_splash.h" 120 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 121 #include <dev/splash/splash.h> 122 extern struct splash_progress *splash_progress_state; 123 #endif 124 #endif 125 126 /* 127 * Autoconfiguration subroutines. 128 */ 129 130 /* 131 * ioconf.c exports exactly two names: cfdata and cfroots. All system 132 * devices and drivers are found via these tables. 133 */ 134 extern struct cfdata cfdata[]; 135 extern const short cfroots[]; 136 137 /* 138 * List of all cfdriver structures. We use this to detect duplicates 139 * when other cfdrivers are loaded. 140 */ 141 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 142 extern struct cfdriver * const cfdriver_list_initial[]; 143 144 /* 145 * Initial list of cfattach's. 146 */ 147 extern const struct cfattachinit cfattachinit[]; 148 149 /* 150 * List of cfdata tables. We always have one such list -- the one 151 * built statically when the kernel was configured. 152 */ 153 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables); 154 static struct cftable initcftable; 155 156 #define ROOT ((device_t)NULL) 157 158 struct matchinfo { 159 cfsubmatch_t fn; 160 struct device *parent; 161 const int *locs; 162 void *aux; 163 struct cfdata *match; 164 int pri; 165 }; 166 167 static char *number(char *, int); 168 static void mapply(struct matchinfo *, cfdata_t); 169 static device_t config_devalloc(const device_t, const cfdata_t, const int *); 170 static void config_devdealloc(device_t); 171 static void config_makeroom(int, struct cfdriver *); 172 static void config_devlink(device_t); 173 static void config_devunlink(device_t); 174 175 static device_t deviter_next1(deviter_t *); 176 static void deviter_reinit(deviter_t *); 177 178 struct deferred_config { 179 TAILQ_ENTRY(deferred_config) dc_queue; 180 device_t dc_dev; 181 void (*dc_func)(device_t); 182 }; 183 184 TAILQ_HEAD(deferred_config_head, deferred_config); 185 186 struct deferred_config_head deferred_config_queue = 187 TAILQ_HEAD_INITIALIZER(deferred_config_queue); 188 struct deferred_config_head interrupt_config_queue = 189 TAILQ_HEAD_INITIALIZER(interrupt_config_queue); 190 191 static void config_process_deferred(struct deferred_config_head *, device_t); 192 193 /* Hooks to finalize configuration once all real devices have been found. */ 194 struct finalize_hook { 195 TAILQ_ENTRY(finalize_hook) f_list; 196 int (*f_func)(device_t); 197 device_t f_dev; 198 }; 199 static TAILQ_HEAD(, finalize_hook) config_finalize_list = 200 TAILQ_HEAD_INITIALIZER(config_finalize_list); 201 static int config_finalize_done; 202 203 /* list of all devices */ 204 struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs); 205 kcondvar_t alldevs_cv; 206 kmutex_t alldevs_mtx; 207 static int alldevs_nread = 0; 208 static int alldevs_nwrite = 0; 209 static lwp_t *alldevs_writer = NULL; 210 211 volatile int config_pending; /* semaphore for mountroot */ 212 213 #define STREQ(s1, s2) \ 214 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 215 216 static int config_initialized; /* config_init() has been called. */ 217 218 static int config_do_twiddle; 219 220 struct vnode * 221 opendisk(struct device *dv) 222 { 223 int bmajor, bminor; 224 struct vnode *tmpvn; 225 int error; 226 dev_t dev; 227 228 /* 229 * Lookup major number for disk block device. 230 */ 231 bmajor = devsw_name2blk(device_xname(dv), NULL, 0); 232 if (bmajor == -1) 233 return NULL; 234 235 bminor = minor(device_unit(dv)); 236 /* 237 * Fake a temporary vnode for the disk, open it, and read 238 * and hash the sectors. 239 */ 240 dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) : 241 MAKEDISKDEV(bmajor, bminor, RAW_PART); 242 if (bdevvp(dev, &tmpvn)) 243 panic("%s: can't alloc vnode for %s", __func__, 244 device_xname(dv)); 245 error = VOP_OPEN(tmpvn, FREAD, NOCRED); 246 if (error) { 247 #ifndef DEBUG 248 /* 249 * Ignore errors caused by missing device, partition, 250 * or medium. 251 */ 252 if (error != ENXIO && error != ENODEV) 253 #endif 254 printf("%s: can't open dev %s (%d)\n", 255 __func__, device_xname(dv), error); 256 vput(tmpvn); 257 return NULL; 258 } 259 260 return tmpvn; 261 } 262 263 int 264 config_handle_wedges(struct device *dv, int par) 265 { 266 struct dkwedge_list wl; 267 struct dkwedge_info *wi; 268 struct vnode *vn; 269 char diskname[16]; 270 int i, error; 271 272 if ((vn = opendisk(dv)) == NULL) 273 return -1; 274 275 wl.dkwl_bufsize = sizeof(*wi) * 16; 276 wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK); 277 278 error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED); 279 VOP_CLOSE(vn, FREAD, NOCRED); 280 vput(vn); 281 if (error) { 282 #ifdef DEBUG_WEDGE 283 printf("%s: List wedges returned %d\n", 284 device_xname(dv), error); 285 #endif 286 free(wi, M_TEMP); 287 return -1; 288 } 289 290 #ifdef DEBUG_WEDGE 291 printf("%s: Returned %u(%u) wedges\n", device_xname(dv), 292 wl.dkwl_nwedges, wl.dkwl_ncopied); 293 #endif 294 snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv), 295 par + 'a'); 296 297 for (i = 0; i < wl.dkwl_ncopied; i++) { 298 #ifdef DEBUG_WEDGE 299 printf("%s: Looking for %s in %s\n", 300 device_xname(dv), diskname, wi[i].dkw_wname); 301 #endif 302 if (strcmp(wi[i].dkw_wname, diskname) == 0) 303 break; 304 } 305 306 if (i == wl.dkwl_ncopied) { 307 #ifdef DEBUG_WEDGE 308 printf("%s: Cannot find wedge with parent %s\n", 309 device_xname(dv), diskname); 310 #endif 311 free(wi, M_TEMP); 312 return -1; 313 } 314 315 #ifdef DEBUG_WEDGE 316 printf("%s: Setting boot wedge %s (%s) at %llu %llu\n", 317 device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname, 318 (unsigned long long)wi[i].dkw_offset, 319 (unsigned long long)wi[i].dkw_size); 320 #endif 321 dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size); 322 free(wi, M_TEMP); 323 return 0; 324 } 325 326 /* 327 * Initialize the autoconfiguration data structures. Normally this 328 * is done by configure(), but some platforms need to do this very 329 * early (to e.g. initialize the console). 330 */ 331 void 332 config_init(void) 333 { 334 const struct cfattachinit *cfai; 335 int i, j; 336 337 if (config_initialized) 338 return; 339 340 mutex_init(&alldevs_mtx, MUTEX_DEFAULT, IPL_NONE); 341 cv_init(&alldevs_cv, "alldevs"); 342 343 /* allcfdrivers is statically initialized. */ 344 for (i = 0; cfdriver_list_initial[i] != NULL; i++) { 345 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0) 346 panic("configure: duplicate `%s' drivers", 347 cfdriver_list_initial[i]->cd_name); 348 } 349 350 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) { 351 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 352 if (config_cfattach_attach(cfai->cfai_name, 353 cfai->cfai_list[j]) != 0) 354 panic("configure: duplicate `%s' attachment " 355 "of `%s' driver", 356 cfai->cfai_list[j]->ca_name, 357 cfai->cfai_name); 358 } 359 } 360 361 initcftable.ct_cfdata = cfdata; 362 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 363 364 config_initialized = 1; 365 } 366 367 void 368 config_deferred(device_t dev) 369 { 370 config_process_deferred(&deferred_config_queue, dev); 371 config_process_deferred(&interrupt_config_queue, dev); 372 } 373 374 /* 375 * Configure the system's hardware. 376 */ 377 void 378 configure(void) 379 { 380 int errcnt; 381 382 /* Initialize data structures. */ 383 config_init(); 384 pmf_init(); 385 386 #ifdef USERCONF 387 if (boothowto & RB_USERCONF) 388 user_config(); 389 #endif 390 391 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 392 config_do_twiddle = 1; 393 printf_nolog("Detecting hardware..."); 394 } 395 396 /* 397 * Do the machine-dependent portion of autoconfiguration. This 398 * sets the configuration machinery here in motion by "finding" 399 * the root bus. When this function returns, we expect interrupts 400 * to be enabled. 401 */ 402 cpu_configure(); 403 404 /* Initialize callouts, part 2. */ 405 callout_startup2(); 406 407 /* 408 * Now that we've found all the hardware, start the real time 409 * and statistics clocks. 410 */ 411 initclocks(); 412 413 cold = 0; /* clocks are running, we're warm now! */ 414 415 /* Boot the secondary processors. */ 416 mp_online = true; 417 #if defined(MULTIPROCESSOR) 418 cpu_boot_secondary_processors(); 419 #endif 420 421 /* 422 * Now callback to finish configuration for devices which want 423 * to do this once interrupts are enabled. 424 */ 425 config_process_deferred(&interrupt_config_queue, NULL); 426 427 errcnt = aprint_get_error_count(); 428 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 429 (boothowto & AB_VERBOSE) == 0) { 430 if (config_do_twiddle) { 431 config_do_twiddle = 0; 432 printf_nolog("done.\n"); 433 } 434 if (errcnt != 0) { 435 printf("WARNING: %d error%s while detecting hardware; " 436 "check system log.\n", errcnt, 437 errcnt == 1 ? "" : "s"); 438 } 439 } 440 } 441 442 /* 443 * Add a cfdriver to the system. 444 */ 445 int 446 config_cfdriver_attach(struct cfdriver *cd) 447 { 448 struct cfdriver *lcd; 449 450 /* Make sure this driver isn't already in the system. */ 451 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 452 if (STREQ(lcd->cd_name, cd->cd_name)) 453 return (EEXIST); 454 } 455 456 LIST_INIT(&cd->cd_attach); 457 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 458 459 return (0); 460 } 461 462 /* 463 * Remove a cfdriver from the system. 464 */ 465 int 466 config_cfdriver_detach(struct cfdriver *cd) 467 { 468 int i; 469 470 /* Make sure there are no active instances. */ 471 for (i = 0; i < cd->cd_ndevs; i++) { 472 if (cd->cd_devs[i] != NULL) 473 return (EBUSY); 474 } 475 476 /* ...and no attachments loaded. */ 477 if (LIST_EMPTY(&cd->cd_attach) == 0) 478 return (EBUSY); 479 480 LIST_REMOVE(cd, cd_list); 481 482 KASSERT(cd->cd_devs == NULL); 483 484 return (0); 485 } 486 487 /* 488 * Look up a cfdriver by name. 489 */ 490 struct cfdriver * 491 config_cfdriver_lookup(const char *name) 492 { 493 struct cfdriver *cd; 494 495 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 496 if (STREQ(cd->cd_name, name)) 497 return (cd); 498 } 499 500 return (NULL); 501 } 502 503 /* 504 * Add a cfattach to the specified driver. 505 */ 506 int 507 config_cfattach_attach(const char *driver, struct cfattach *ca) 508 { 509 struct cfattach *lca; 510 struct cfdriver *cd; 511 512 cd = config_cfdriver_lookup(driver); 513 if (cd == NULL) 514 return (ESRCH); 515 516 /* Make sure this attachment isn't already on this driver. */ 517 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 518 if (STREQ(lca->ca_name, ca->ca_name)) 519 return (EEXIST); 520 } 521 522 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 523 524 return (0); 525 } 526 527 /* 528 * Remove a cfattach from the specified driver. 529 */ 530 int 531 config_cfattach_detach(const char *driver, struct cfattach *ca) 532 { 533 struct cfdriver *cd; 534 device_t dev; 535 int i; 536 537 cd = config_cfdriver_lookup(driver); 538 if (cd == NULL) 539 return (ESRCH); 540 541 /* Make sure there are no active instances. */ 542 for (i = 0; i < cd->cd_ndevs; i++) { 543 if ((dev = cd->cd_devs[i]) == NULL) 544 continue; 545 if (dev->dv_cfattach == ca) 546 return (EBUSY); 547 } 548 549 LIST_REMOVE(ca, ca_list); 550 551 return (0); 552 } 553 554 /* 555 * Look up a cfattach by name. 556 */ 557 static struct cfattach * 558 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 559 { 560 struct cfattach *ca; 561 562 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 563 if (STREQ(ca->ca_name, atname)) 564 return (ca); 565 } 566 567 return (NULL); 568 } 569 570 /* 571 * Look up a cfattach by driver/attachment name. 572 */ 573 struct cfattach * 574 config_cfattach_lookup(const char *name, const char *atname) 575 { 576 struct cfdriver *cd; 577 578 cd = config_cfdriver_lookup(name); 579 if (cd == NULL) 580 return (NULL); 581 582 return (config_cfattach_lookup_cd(cd, atname)); 583 } 584 585 /* 586 * Apply the matching function and choose the best. This is used 587 * a few times and we want to keep the code small. 588 */ 589 static void 590 mapply(struct matchinfo *m, cfdata_t cf) 591 { 592 int pri; 593 594 if (m->fn != NULL) { 595 pri = (*m->fn)(m->parent, cf, m->locs, m->aux); 596 } else { 597 pri = config_match(m->parent, cf, m->aux); 598 } 599 if (pri > m->pri) { 600 m->match = cf; 601 m->pri = pri; 602 } 603 } 604 605 int 606 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 607 { 608 const struct cfiattrdata *ci; 609 const struct cflocdesc *cl; 610 int nlocs, i; 611 612 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver); 613 KASSERT(ci); 614 nlocs = ci->ci_loclen; 615 for (i = 0; i < nlocs; i++) { 616 cl = &ci->ci_locdesc[i]; 617 /* !cld_defaultstr means no default value */ 618 if ((!(cl->cld_defaultstr) 619 || (cf->cf_loc[i] != cl->cld_default)) 620 && cf->cf_loc[i] != locs[i]) 621 return (0); 622 } 623 624 return (config_match(parent, cf, aux)); 625 } 626 627 /* 628 * Helper function: check whether the driver supports the interface attribute 629 * and return its descriptor structure. 630 */ 631 static const struct cfiattrdata * 632 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia) 633 { 634 const struct cfiattrdata * const *cpp; 635 636 if (cd->cd_attrs == NULL) 637 return (0); 638 639 for (cpp = cd->cd_attrs; *cpp; cpp++) { 640 if (STREQ((*cpp)->ci_name, ia)) { 641 /* Match. */ 642 return (*cpp); 643 } 644 } 645 return (0); 646 } 647 648 /* 649 * Lookup an interface attribute description by name. 650 * If the driver is given, consider only its supported attributes. 651 */ 652 const struct cfiattrdata * 653 cfiattr_lookup(const char *name, const struct cfdriver *cd) 654 { 655 const struct cfdriver *d; 656 const struct cfiattrdata *ia; 657 658 if (cd) 659 return (cfdriver_get_iattr(cd, name)); 660 661 LIST_FOREACH(d, &allcfdrivers, cd_list) { 662 ia = cfdriver_get_iattr(d, name); 663 if (ia) 664 return (ia); 665 } 666 return (0); 667 } 668 669 /* 670 * Determine if `parent' is a potential parent for a device spec based 671 * on `cfp'. 672 */ 673 static int 674 cfparent_match(const device_t parent, const struct cfparent *cfp) 675 { 676 struct cfdriver *pcd; 677 678 /* We don't match root nodes here. */ 679 if (cfp == NULL) 680 return (0); 681 682 pcd = parent->dv_cfdriver; 683 KASSERT(pcd != NULL); 684 685 /* 686 * First, ensure this parent has the correct interface 687 * attribute. 688 */ 689 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr)) 690 return (0); 691 692 /* 693 * If no specific parent device instance was specified (i.e. 694 * we're attaching to the attribute only), we're done! 695 */ 696 if (cfp->cfp_parent == NULL) 697 return (1); 698 699 /* 700 * Check the parent device's name. 701 */ 702 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 703 return (0); /* not the same parent */ 704 705 /* 706 * Make sure the unit number matches. 707 */ 708 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 709 cfp->cfp_unit == parent->dv_unit) 710 return (1); 711 712 /* Unit numbers don't match. */ 713 return (0); 714 } 715 716 /* 717 * Helper for config_cfdata_attach(): check all devices whether it could be 718 * parent any attachment in the config data table passed, and rescan. 719 */ 720 static void 721 rescan_with_cfdata(const struct cfdata *cf) 722 { 723 device_t d; 724 const struct cfdata *cf1; 725 deviter_t di; 726 727 728 /* 729 * "alldevs" is likely longer than an LKM's cfdata, so make it 730 * the outer loop. 731 */ 732 for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) { 733 734 if (!(d->dv_cfattach->ca_rescan)) 735 continue; 736 737 for (cf1 = cf; cf1->cf_name; cf1++) { 738 739 if (!cfparent_match(d, cf1->cf_pspec)) 740 continue; 741 742 (*d->dv_cfattach->ca_rescan)(d, 743 cf1->cf_pspec->cfp_iattr, cf1->cf_loc); 744 } 745 } 746 deviter_release(&di); 747 } 748 749 /* 750 * Attach a supplemental config data table and rescan potential 751 * parent devices if required. 752 */ 753 int 754 config_cfdata_attach(cfdata_t cf, int scannow) 755 { 756 struct cftable *ct; 757 758 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK); 759 ct->ct_cfdata = cf; 760 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list); 761 762 if (scannow) 763 rescan_with_cfdata(cf); 764 765 return (0); 766 } 767 768 /* 769 * Helper for config_cfdata_detach: check whether a device is 770 * found through any attachment in the config data table. 771 */ 772 static int 773 dev_in_cfdata(const struct device *d, const struct cfdata *cf) 774 { 775 const struct cfdata *cf1; 776 777 for (cf1 = cf; cf1->cf_name; cf1++) 778 if (d->dv_cfdata == cf1) 779 return (1); 780 781 return (0); 782 } 783 784 /* 785 * Detach a supplemental config data table. Detach all devices found 786 * through that table (and thus keeping references to it) before. 787 */ 788 int 789 config_cfdata_detach(cfdata_t cf) 790 { 791 device_t d; 792 int error = 0; 793 struct cftable *ct; 794 deviter_t di; 795 796 for (d = deviter_first(&di, DEVITER_F_RW); d != NULL; 797 d = deviter_next(&di)) { 798 if (!dev_in_cfdata(d, cf)) 799 continue; 800 if ((error = config_detach(d, 0)) != 0) 801 break; 802 } 803 deviter_release(&di); 804 if (error) { 805 aprint_error_dev(d, "unable to detach instance\n"); 806 return error; 807 } 808 809 TAILQ_FOREACH(ct, &allcftables, ct_list) { 810 if (ct->ct_cfdata == cf) { 811 TAILQ_REMOVE(&allcftables, ct, ct_list); 812 free(ct, M_DEVBUF); 813 return (0); 814 } 815 } 816 817 /* not found -- shouldn't happen */ 818 return (EINVAL); 819 } 820 821 /* 822 * Invoke the "match" routine for a cfdata entry on behalf of 823 * an external caller, usually a "submatch" routine. 824 */ 825 int 826 config_match(device_t parent, cfdata_t cf, void *aux) 827 { 828 struct cfattach *ca; 829 830 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 831 if (ca == NULL) { 832 /* No attachment for this entry, oh well. */ 833 return (0); 834 } 835 836 return ((*ca->ca_match)(parent, cf, aux)); 837 } 838 839 /* 840 * Iterate over all potential children of some device, calling the given 841 * function (default being the child's match function) for each one. 842 * Nonzero returns are matches; the highest value returned is considered 843 * the best match. Return the `found child' if we got a match, or NULL 844 * otherwise. The `aux' pointer is simply passed on through. 845 * 846 * Note that this function is designed so that it can be used to apply 847 * an arbitrary function to all potential children (its return value 848 * can be ignored). 849 */ 850 cfdata_t 851 config_search_loc(cfsubmatch_t fn, device_t parent, 852 const char *ifattr, const int *locs, void *aux) 853 { 854 struct cftable *ct; 855 cfdata_t cf; 856 struct matchinfo m; 857 858 KASSERT(config_initialized); 859 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr)); 860 861 m.fn = fn; 862 m.parent = parent; 863 m.locs = locs; 864 m.aux = aux; 865 m.match = NULL; 866 m.pri = 0; 867 868 TAILQ_FOREACH(ct, &allcftables, ct_list) { 869 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 870 871 /* We don't match root nodes here. */ 872 if (!cf->cf_pspec) 873 continue; 874 875 /* 876 * Skip cf if no longer eligible, otherwise scan 877 * through parents for one matching `parent', and 878 * try match function. 879 */ 880 if (cf->cf_fstate == FSTATE_FOUND) 881 continue; 882 if (cf->cf_fstate == FSTATE_DNOTFOUND || 883 cf->cf_fstate == FSTATE_DSTAR) 884 continue; 885 886 /* 887 * If an interface attribute was specified, 888 * consider only children which attach to 889 * that attribute. 890 */ 891 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr)) 892 continue; 893 894 if (cfparent_match(parent, cf->cf_pspec)) 895 mapply(&m, cf); 896 } 897 } 898 return (m.match); 899 } 900 901 cfdata_t 902 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr, 903 void *aux) 904 { 905 906 return (config_search_loc(fn, parent, ifattr, NULL, aux)); 907 } 908 909 /* 910 * Find the given root device. 911 * This is much like config_search, but there is no parent. 912 * Don't bother with multiple cfdata tables; the root node 913 * must always be in the initial table. 914 */ 915 cfdata_t 916 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux) 917 { 918 cfdata_t cf; 919 const short *p; 920 struct matchinfo m; 921 922 m.fn = fn; 923 m.parent = ROOT; 924 m.aux = aux; 925 m.match = NULL; 926 m.pri = 0; 927 m.locs = 0; 928 /* 929 * Look at root entries for matching name. We do not bother 930 * with found-state here since only one root should ever be 931 * searched (and it must be done first). 932 */ 933 for (p = cfroots; *p >= 0; p++) { 934 cf = &cfdata[*p]; 935 if (strcmp(cf->cf_name, rootname) == 0) 936 mapply(&m, cf); 937 } 938 return (m.match); 939 } 940 941 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 942 943 /* 944 * The given `aux' argument describes a device that has been found 945 * on the given parent, but not necessarily configured. Locate the 946 * configuration data for that device (using the submatch function 947 * provided, or using candidates' cd_match configuration driver 948 * functions) and attach it, and return true. If the device was 949 * not configured, call the given `print' function and return 0. 950 */ 951 device_t 952 config_found_sm_loc(device_t parent, 953 const char *ifattr, const int *locs, void *aux, 954 cfprint_t print, cfsubmatch_t submatch) 955 { 956 cfdata_t cf; 957 958 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 959 if (splash_progress_state) 960 splash_progress_update(splash_progress_state); 961 #endif 962 963 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux))) 964 return(config_attach_loc(parent, cf, locs, aux, print)); 965 if (print) { 966 if (config_do_twiddle) 967 twiddle(); 968 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]); 969 } 970 971 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 972 if (splash_progress_state) 973 splash_progress_update(splash_progress_state); 974 #endif 975 976 return (NULL); 977 } 978 979 device_t 980 config_found_ia(device_t parent, const char *ifattr, void *aux, 981 cfprint_t print) 982 { 983 984 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL)); 985 } 986 987 device_t 988 config_found(device_t parent, void *aux, cfprint_t print) 989 { 990 991 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL)); 992 } 993 994 /* 995 * As above, but for root devices. 996 */ 997 device_t 998 config_rootfound(const char *rootname, void *aux) 999 { 1000 cfdata_t cf; 1001 1002 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL) 1003 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 1004 aprint_error("root device %s not configured\n", rootname); 1005 return (NULL); 1006 } 1007 1008 /* just like sprintf(buf, "%d") except that it works from the end */ 1009 static char * 1010 number(char *ep, int n) 1011 { 1012 1013 *--ep = 0; 1014 while (n >= 10) { 1015 *--ep = (n % 10) + '0'; 1016 n /= 10; 1017 } 1018 *--ep = n + '0'; 1019 return (ep); 1020 } 1021 1022 /* 1023 * Expand the size of the cd_devs array if necessary. 1024 */ 1025 static void 1026 config_makeroom(int n, struct cfdriver *cd) 1027 { 1028 int old, new; 1029 void **nsp; 1030 1031 if (n < cd->cd_ndevs) 1032 return; 1033 1034 /* 1035 * Need to expand the array. 1036 */ 1037 old = cd->cd_ndevs; 1038 if (old == 0) 1039 new = 4; 1040 else 1041 new = old * 2; 1042 while (new <= n) 1043 new *= 2; 1044 cd->cd_ndevs = new; 1045 nsp = malloc(new * sizeof(void *), M_DEVBUF, 1046 cold ? M_NOWAIT : M_WAITOK); 1047 if (nsp == NULL) 1048 panic("config_attach: %sing dev array", 1049 old != 0 ? "expand" : "creat"); 1050 memset(nsp + old, 0, (new - old) * sizeof(void *)); 1051 if (old != 0) { 1052 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 1053 free(cd->cd_devs, M_DEVBUF); 1054 } 1055 cd->cd_devs = nsp; 1056 } 1057 1058 static void 1059 config_devlink(device_t dev) 1060 { 1061 struct cfdriver *cd = dev->dv_cfdriver; 1062 1063 /* put this device in the devices array */ 1064 config_makeroom(dev->dv_unit, cd); 1065 if (cd->cd_devs[dev->dv_unit]) 1066 panic("config_attach: duplicate %s", dev->dv_xname); 1067 cd->cd_devs[dev->dv_unit] = dev; 1068 1069 /* It is safe to add a device to the tail of the list while 1070 * readers are in the list, but not while a writer is in 1071 * the list. Wait for any writer to complete. 1072 */ 1073 mutex_enter(&alldevs_mtx); 1074 while (alldevs_nwrite != 0 && alldevs_writer != curlwp) 1075 cv_wait(&alldevs_cv, &alldevs_mtx); 1076 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 1077 cv_signal(&alldevs_cv); 1078 mutex_exit(&alldevs_mtx); 1079 } 1080 1081 static void 1082 config_devunlink(device_t dev) 1083 { 1084 struct cfdriver *cd = dev->dv_cfdriver; 1085 int i; 1086 1087 /* Unlink from device list. */ 1088 TAILQ_REMOVE(&alldevs, dev, dv_list); 1089 1090 /* Remove from cfdriver's array. */ 1091 cd->cd_devs[dev->dv_unit] = NULL; 1092 1093 /* 1094 * If the device now has no units in use, deallocate its softc array. 1095 */ 1096 for (i = 0; i < cd->cd_ndevs; i++) 1097 if (cd->cd_devs[i] != NULL) 1098 break; 1099 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 1100 free(cd->cd_devs, M_DEVBUF); 1101 cd->cd_devs = NULL; 1102 cd->cd_ndevs = 0; 1103 } 1104 } 1105 1106 static device_t 1107 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs) 1108 { 1109 struct cfdriver *cd; 1110 struct cfattach *ca; 1111 size_t lname, lunit; 1112 const char *xunit; 1113 int myunit; 1114 char num[10]; 1115 device_t dev; 1116 void *dev_private; 1117 const struct cfiattrdata *ia; 1118 1119 cd = config_cfdriver_lookup(cf->cf_name); 1120 if (cd == NULL) 1121 return (NULL); 1122 1123 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1124 if (ca == NULL) 1125 return (NULL); 1126 1127 if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 && 1128 ca->ca_devsize < sizeof(struct device)) 1129 panic("config_devalloc"); 1130 1131 #ifndef __BROKEN_CONFIG_UNIT_USAGE 1132 if (cf->cf_fstate == FSTATE_STAR) { 1133 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 1134 if (cd->cd_devs[myunit] == NULL) 1135 break; 1136 /* 1137 * myunit is now the unit of the first NULL device pointer, 1138 * or max(cd->cd_ndevs,cf->cf_unit). 1139 */ 1140 } else { 1141 myunit = cf->cf_unit; 1142 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL) 1143 return (NULL); 1144 } 1145 #else 1146 myunit = cf->cf_unit; 1147 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */ 1148 1149 /* compute length of name and decimal expansion of unit number */ 1150 lname = strlen(cd->cd_name); 1151 xunit = number(&num[sizeof(num)], myunit); 1152 lunit = &num[sizeof(num)] - xunit; 1153 if (lname + lunit > sizeof(dev->dv_xname)) 1154 panic("config_devalloc: device name too long"); 1155 1156 /* get memory for all device vars */ 1157 KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device)); 1158 if (ca->ca_devsize > 0) { 1159 dev_private = malloc(ca->ca_devsize, M_DEVBUF, 1160 M_ZERO | (cold ? M_NOWAIT : M_WAITOK)); 1161 if (dev_private == NULL) 1162 panic("config_devalloc: memory allocation for device softc failed"); 1163 } else { 1164 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC); 1165 dev_private = NULL; 1166 } 1167 1168 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) { 1169 dev = malloc(sizeof(struct device), M_DEVBUF, 1170 M_ZERO | (cold ? M_NOWAIT : M_WAITOK)); 1171 } else { 1172 dev = dev_private; 1173 } 1174 if (dev == NULL) 1175 panic("config_devalloc: memory allocation for device_t failed"); 1176 1177 dev->dv_class = cd->cd_class; 1178 dev->dv_cfdata = cf; 1179 dev->dv_cfdriver = cd; 1180 dev->dv_cfattach = ca; 1181 dev->dv_unit = myunit; 1182 dev->dv_activity_count = 0; 1183 dev->dv_activity_handlers = NULL; 1184 dev->dv_private = dev_private; 1185 memcpy(dev->dv_xname, cd->cd_name, lname); 1186 memcpy(dev->dv_xname + lname, xunit, lunit); 1187 dev->dv_parent = parent; 1188 if (parent != NULL) 1189 dev->dv_depth = parent->dv_depth + 1; 1190 else 1191 dev->dv_depth = 0; 1192 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 1193 dev->dv_flags |= ca->ca_flags; /* inherit flags from class */ 1194 if (locs) { 1195 KASSERT(parent); /* no locators at root */ 1196 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr, 1197 parent->dv_cfdriver); 1198 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int), 1199 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1200 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int)); 1201 } 1202 dev->dv_properties = prop_dictionary_create(); 1203 KASSERT(dev->dv_properties != NULL); 1204 1205 return (dev); 1206 } 1207 1208 static void 1209 config_devdealloc(device_t dev) 1210 { 1211 1212 KASSERT(dev->dv_properties != NULL); 1213 prop_object_release(dev->dv_properties); 1214 1215 if (dev->dv_activity_handlers) 1216 panic("config_devdealloc with registered handlers"); 1217 1218 if (dev->dv_locators) 1219 free(dev->dv_locators, M_DEVBUF); 1220 1221 if ((dev->dv_flags & DVF_PRIV_ALLOC) != 0) 1222 free(dev->dv_private, M_DEVBUF); 1223 1224 free(dev, M_DEVBUF); 1225 } 1226 1227 /* 1228 * Attach a found device. 1229 */ 1230 device_t 1231 config_attach_loc(device_t parent, cfdata_t cf, 1232 const int *locs, void *aux, cfprint_t print) 1233 { 1234 device_t dev; 1235 struct cftable *ct; 1236 const char *drvname; 1237 1238 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1239 if (splash_progress_state) 1240 splash_progress_update(splash_progress_state); 1241 #endif 1242 1243 dev = config_devalloc(parent, cf, locs); 1244 if (!dev) 1245 panic("config_attach: allocation of device softc failed"); 1246 1247 /* XXX redundant - see below? */ 1248 if (cf->cf_fstate != FSTATE_STAR) { 1249 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1250 cf->cf_fstate = FSTATE_FOUND; 1251 } 1252 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1253 else 1254 cf->cf_unit++; 1255 #endif 1256 1257 config_devlink(dev); 1258 1259 if (config_do_twiddle) 1260 twiddle(); 1261 else 1262 aprint_naive("Found "); 1263 /* 1264 * We want the next two printfs for normal, verbose, and quiet, 1265 * but not silent (in which case, we're twiddling, instead). 1266 */ 1267 if (parent == ROOT) { 1268 aprint_naive("%s (root)", dev->dv_xname); 1269 aprint_normal("%s (root)", dev->dv_xname); 1270 } else { 1271 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname); 1272 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname); 1273 if (print) 1274 (void) (*print)(aux, NULL); 1275 } 1276 1277 /* 1278 * Before attaching, clobber any unfound devices that are 1279 * otherwise identical. 1280 * XXX code above is redundant? 1281 */ 1282 drvname = dev->dv_cfdriver->cd_name; 1283 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1284 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1285 if (STREQ(cf->cf_name, drvname) && 1286 cf->cf_unit == dev->dv_unit) { 1287 if (cf->cf_fstate == FSTATE_NOTFOUND) 1288 cf->cf_fstate = FSTATE_FOUND; 1289 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1290 /* 1291 * Bump the unit number on all starred cfdata 1292 * entries for this device. 1293 */ 1294 if (cf->cf_fstate == FSTATE_STAR) 1295 cf->cf_unit++; 1296 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1297 } 1298 } 1299 } 1300 #ifdef __HAVE_DEVICE_REGISTER 1301 device_register(dev, aux); 1302 #endif 1303 1304 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1305 if (splash_progress_state) 1306 splash_progress_update(splash_progress_state); 1307 #endif 1308 (*dev->dv_cfattach->ca_attach)(parent, dev, aux); 1309 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1310 if (splash_progress_state) 1311 splash_progress_update(splash_progress_state); 1312 #endif 1313 1314 if (!device_pmf_is_registered(dev)) 1315 aprint_debug_dev(dev, "WARNING: power management not supported\n"); 1316 1317 config_process_deferred(&deferred_config_queue, dev); 1318 return (dev); 1319 } 1320 1321 device_t 1322 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1323 { 1324 1325 return (config_attach_loc(parent, cf, NULL, aux, print)); 1326 } 1327 1328 /* 1329 * As above, but for pseudo-devices. Pseudo-devices attached in this 1330 * way are silently inserted into the device tree, and their children 1331 * attached. 1332 * 1333 * Note that because pseudo-devices are attached silently, any information 1334 * the attach routine wishes to print should be prefixed with the device 1335 * name by the attach routine. 1336 */ 1337 device_t 1338 config_attach_pseudo(cfdata_t cf) 1339 { 1340 device_t dev; 1341 1342 dev = config_devalloc(ROOT, cf, NULL); 1343 if (!dev) 1344 return (NULL); 1345 1346 /* XXX mark busy in cfdata */ 1347 1348 config_devlink(dev); 1349 1350 #if 0 /* XXXJRT not yet */ 1351 #ifdef __HAVE_DEVICE_REGISTER 1352 device_register(dev, NULL); /* like a root node */ 1353 #endif 1354 #endif 1355 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL); 1356 config_process_deferred(&deferred_config_queue, dev); 1357 return (dev); 1358 } 1359 1360 /* 1361 * Detach a device. Optionally forced (e.g. because of hardware 1362 * removal) and quiet. Returns zero if successful, non-zero 1363 * (an error code) otherwise. 1364 * 1365 * Note that this code wants to be run from a process context, so 1366 * that the detach can sleep to allow processes which have a device 1367 * open to run and unwind their stacks. 1368 */ 1369 int 1370 config_detach(device_t dev, int flags) 1371 { 1372 struct cftable *ct; 1373 cfdata_t cf; 1374 const struct cfattach *ca; 1375 struct cfdriver *cd; 1376 #ifdef DIAGNOSTIC 1377 device_t d; 1378 #endif 1379 int rv = 0; 1380 1381 #ifdef DIAGNOSTIC 1382 if (dev->dv_cfdata != NULL && 1383 dev->dv_cfdata->cf_fstate != FSTATE_FOUND && 1384 dev->dv_cfdata->cf_fstate != FSTATE_STAR) 1385 panic("config_detach: bad device fstate"); 1386 #endif 1387 cd = dev->dv_cfdriver; 1388 KASSERT(cd != NULL); 1389 1390 ca = dev->dv_cfattach; 1391 KASSERT(ca != NULL); 1392 1393 KASSERT(curlwp != NULL); 1394 mutex_enter(&alldevs_mtx); 1395 if (alldevs_nwrite > 0 && alldevs_writer == NULL) 1396 ; 1397 else while (alldevs_nread != 0 || 1398 (alldevs_nwrite != 0 && alldevs_writer != curlwp)) 1399 cv_wait(&alldevs_cv, &alldevs_mtx); 1400 if (alldevs_nwrite++ == 0) 1401 alldevs_writer = curlwp; 1402 mutex_exit(&alldevs_mtx); 1403 1404 /* 1405 * Ensure the device is deactivated. If the device doesn't 1406 * have an activation entry point, we allow DVF_ACTIVE to 1407 * remain set. Otherwise, if DVF_ACTIVE is still set, the 1408 * device is busy, and the detach fails. 1409 */ 1410 if (ca->ca_activate != NULL) 1411 rv = config_deactivate(dev); 1412 1413 /* 1414 * Try to detach the device. If that's not possible, then 1415 * we either panic() (for the forced but failed case), or 1416 * return an error. 1417 */ 1418 if (rv == 0) { 1419 if (ca->ca_detach != NULL) 1420 rv = (*ca->ca_detach)(dev, flags); 1421 else 1422 rv = EOPNOTSUPP; 1423 } 1424 if (rv != 0) { 1425 if ((flags & DETACH_FORCE) == 0) 1426 goto out; 1427 else 1428 panic("config_detach: forced detach of %s failed (%d)", 1429 dev->dv_xname, rv); 1430 } 1431 1432 /* 1433 * The device has now been successfully detached. 1434 */ 1435 1436 #ifdef DIAGNOSTIC 1437 /* 1438 * Sanity: If you're successfully detached, you should have no 1439 * children. (Note that because children must be attached 1440 * after parents, we only need to search the latter part of 1441 * the list.) 1442 */ 1443 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1444 d = TAILQ_NEXT(d, dv_list)) { 1445 if (d->dv_parent == dev) { 1446 printf("config_detach: detached device %s" 1447 " has children %s\n", dev->dv_xname, d->dv_xname); 1448 panic("config_detach"); 1449 } 1450 } 1451 #endif 1452 1453 /* notify the parent that the child is gone */ 1454 if (dev->dv_parent) { 1455 device_t p = dev->dv_parent; 1456 if (p->dv_cfattach->ca_childdetached) 1457 (*p->dv_cfattach->ca_childdetached)(p, dev); 1458 } 1459 1460 /* 1461 * Mark cfdata to show that the unit can be reused, if possible. 1462 */ 1463 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1464 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1465 if (STREQ(cf->cf_name, cd->cd_name)) { 1466 if (cf->cf_fstate == FSTATE_FOUND && 1467 cf->cf_unit == dev->dv_unit) 1468 cf->cf_fstate = FSTATE_NOTFOUND; 1469 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1470 /* 1471 * Note that we can only re-use a starred 1472 * unit number if the unit being detached 1473 * had the last assigned unit number. 1474 */ 1475 if (cf->cf_fstate == FSTATE_STAR && 1476 cf->cf_unit == dev->dv_unit + 1) 1477 cf->cf_unit--; 1478 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1479 } 1480 } 1481 } 1482 1483 config_devunlink(dev); 1484 1485 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1486 aprint_normal_dev(dev, "detached\n"); 1487 1488 config_devdealloc(dev); 1489 1490 out: 1491 mutex_enter(&alldevs_mtx); 1492 if (--alldevs_nwrite == 0) 1493 alldevs_writer = NULL; 1494 cv_signal(&alldevs_cv); 1495 mutex_exit(&alldevs_mtx); 1496 return rv; 1497 } 1498 1499 int 1500 config_detach_children(device_t parent, int flags) 1501 { 1502 device_t dv; 1503 deviter_t di; 1504 int error = 0; 1505 1506 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL; 1507 dv = deviter_next(&di)) { 1508 if (device_parent(dv) != parent) 1509 continue; 1510 if ((error = config_detach(dv, flags)) != 0) 1511 break; 1512 } 1513 deviter_release(&di); 1514 return error; 1515 } 1516 1517 int 1518 config_activate(device_t dev) 1519 { 1520 const struct cfattach *ca = dev->dv_cfattach; 1521 int rv = 0, oflags = dev->dv_flags; 1522 1523 if (ca->ca_activate == NULL) 1524 return (EOPNOTSUPP); 1525 1526 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 1527 dev->dv_flags |= DVF_ACTIVE; 1528 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 1529 if (rv) 1530 dev->dv_flags = oflags; 1531 } 1532 return (rv); 1533 } 1534 1535 int 1536 config_deactivate(device_t dev) 1537 { 1538 const struct cfattach *ca = dev->dv_cfattach; 1539 int rv = 0, oflags = dev->dv_flags; 1540 1541 if (ca->ca_activate == NULL) 1542 return (EOPNOTSUPP); 1543 1544 if (dev->dv_flags & DVF_ACTIVE) { 1545 dev->dv_flags &= ~DVF_ACTIVE; 1546 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 1547 if (rv) 1548 dev->dv_flags = oflags; 1549 } 1550 return (rv); 1551 } 1552 1553 /* 1554 * Defer the configuration of the specified device until all 1555 * of its parent's devices have been attached. 1556 */ 1557 void 1558 config_defer(device_t dev, void (*func)(device_t)) 1559 { 1560 struct deferred_config *dc; 1561 1562 if (dev->dv_parent == NULL) 1563 panic("config_defer: can't defer config of a root device"); 1564 1565 #ifdef DIAGNOSTIC 1566 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 1567 dc = TAILQ_NEXT(dc, dc_queue)) { 1568 if (dc->dc_dev == dev) 1569 panic("config_defer: deferred twice"); 1570 } 1571 #endif 1572 1573 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1574 if (dc == NULL) 1575 panic("config_defer: unable to allocate callback"); 1576 1577 dc->dc_dev = dev; 1578 dc->dc_func = func; 1579 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1580 config_pending_incr(); 1581 } 1582 1583 /* 1584 * Defer some autoconfiguration for a device until after interrupts 1585 * are enabled. 1586 */ 1587 void 1588 config_interrupts(device_t dev, void (*func)(device_t)) 1589 { 1590 struct deferred_config *dc; 1591 1592 /* 1593 * If interrupts are enabled, callback now. 1594 */ 1595 if (cold == 0) { 1596 (*func)(dev); 1597 return; 1598 } 1599 1600 #ifdef DIAGNOSTIC 1601 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 1602 dc = TAILQ_NEXT(dc, dc_queue)) { 1603 if (dc->dc_dev == dev) 1604 panic("config_interrupts: deferred twice"); 1605 } 1606 #endif 1607 1608 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1609 if (dc == NULL) 1610 panic("config_interrupts: unable to allocate callback"); 1611 1612 dc->dc_dev = dev; 1613 dc->dc_func = func; 1614 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1615 config_pending_incr(); 1616 } 1617 1618 /* 1619 * Process a deferred configuration queue. 1620 */ 1621 static void 1622 config_process_deferred(struct deferred_config_head *queue, 1623 device_t parent) 1624 { 1625 struct deferred_config *dc, *ndc; 1626 1627 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 1628 ndc = TAILQ_NEXT(dc, dc_queue); 1629 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 1630 TAILQ_REMOVE(queue, dc, dc_queue); 1631 (*dc->dc_func)(dc->dc_dev); 1632 free(dc, M_DEVBUF); 1633 config_pending_decr(); 1634 } 1635 } 1636 } 1637 1638 /* 1639 * Manipulate the config_pending semaphore. 1640 */ 1641 void 1642 config_pending_incr(void) 1643 { 1644 1645 config_pending++; 1646 } 1647 1648 void 1649 config_pending_decr(void) 1650 { 1651 1652 #ifdef DIAGNOSTIC 1653 if (config_pending == 0) 1654 panic("config_pending_decr: config_pending == 0"); 1655 #endif 1656 config_pending--; 1657 if (config_pending == 0) 1658 wakeup(&config_pending); 1659 } 1660 1661 /* 1662 * Register a "finalization" routine. Finalization routines are 1663 * called iteratively once all real devices have been found during 1664 * autoconfiguration, for as long as any one finalizer has done 1665 * any work. 1666 */ 1667 int 1668 config_finalize_register(device_t dev, int (*fn)(device_t)) 1669 { 1670 struct finalize_hook *f; 1671 1672 /* 1673 * If finalization has already been done, invoke the 1674 * callback function now. 1675 */ 1676 if (config_finalize_done) { 1677 while ((*fn)(dev) != 0) 1678 /* loop */ ; 1679 } 1680 1681 /* Ensure this isn't already on the list. */ 1682 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 1683 if (f->f_func == fn && f->f_dev == dev) 1684 return (EEXIST); 1685 } 1686 1687 f = malloc(sizeof(*f), M_TEMP, M_WAITOK); 1688 f->f_func = fn; 1689 f->f_dev = dev; 1690 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 1691 1692 return (0); 1693 } 1694 1695 void 1696 config_finalize(void) 1697 { 1698 struct finalize_hook *f; 1699 int rv; 1700 1701 /* Run the hooks until none of them does any work. */ 1702 do { 1703 rv = 0; 1704 TAILQ_FOREACH(f, &config_finalize_list, f_list) 1705 rv |= (*f->f_func)(f->f_dev); 1706 } while (rv != 0); 1707 1708 config_finalize_done = 1; 1709 1710 /* Now free all the hooks. */ 1711 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 1712 TAILQ_REMOVE(&config_finalize_list, f, f_list); 1713 free(f, M_TEMP); 1714 } 1715 } 1716 1717 /* 1718 * device_lookup: 1719 * 1720 * Look up a device instance for a given driver. 1721 */ 1722 void * 1723 device_lookup(cfdriver_t cd, int unit) 1724 { 1725 1726 if (unit < 0 || unit >= cd->cd_ndevs) 1727 return (NULL); 1728 1729 return (cd->cd_devs[unit]); 1730 } 1731 1732 /* 1733 * Accessor functions for the device_t type. 1734 */ 1735 devclass_t 1736 device_class(device_t dev) 1737 { 1738 1739 return (dev->dv_class); 1740 } 1741 1742 cfdata_t 1743 device_cfdata(device_t dev) 1744 { 1745 1746 return (dev->dv_cfdata); 1747 } 1748 1749 cfdriver_t 1750 device_cfdriver(device_t dev) 1751 { 1752 1753 return (dev->dv_cfdriver); 1754 } 1755 1756 cfattach_t 1757 device_cfattach(device_t dev) 1758 { 1759 1760 return (dev->dv_cfattach); 1761 } 1762 1763 int 1764 device_unit(device_t dev) 1765 { 1766 1767 return (dev->dv_unit); 1768 } 1769 1770 const char * 1771 device_xname(device_t dev) 1772 { 1773 1774 return (dev->dv_xname); 1775 } 1776 1777 device_t 1778 device_parent(device_t dev) 1779 { 1780 1781 return (dev->dv_parent); 1782 } 1783 1784 bool 1785 device_is_active(device_t dev) 1786 { 1787 int active_flags; 1788 1789 active_flags = DVF_ACTIVE; 1790 active_flags |= DVF_CLASS_SUSPENDED; 1791 active_flags |= DVF_DRIVER_SUSPENDED; 1792 active_flags |= DVF_BUS_SUSPENDED; 1793 1794 return ((dev->dv_flags & active_flags) == DVF_ACTIVE); 1795 } 1796 1797 bool 1798 device_is_enabled(device_t dev) 1799 { 1800 return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE; 1801 } 1802 1803 bool 1804 device_has_power(device_t dev) 1805 { 1806 int active_flags; 1807 1808 active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED; 1809 1810 return ((dev->dv_flags & active_flags) == DVF_ACTIVE); 1811 } 1812 1813 int 1814 device_locator(device_t dev, u_int locnum) 1815 { 1816 1817 KASSERT(dev->dv_locators != NULL); 1818 return (dev->dv_locators[locnum]); 1819 } 1820 1821 void * 1822 device_private(device_t dev) 1823 { 1824 1825 /* 1826 * The reason why device_private(NULL) is allowed is to simplify the 1827 * work of a lot of userspace request handlers (i.e., c/bdev 1828 * handlers) which grab cfdriver_t->cd_units[n]. 1829 * It avoids having them test for it to be NULL and only then calling 1830 * device_private. 1831 */ 1832 return dev == NULL ? NULL : dev->dv_private; 1833 } 1834 1835 prop_dictionary_t 1836 device_properties(device_t dev) 1837 { 1838 1839 return (dev->dv_properties); 1840 } 1841 1842 /* 1843 * device_is_a: 1844 * 1845 * Returns true if the device is an instance of the specified 1846 * driver. 1847 */ 1848 bool 1849 device_is_a(device_t dev, const char *dname) 1850 { 1851 1852 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0); 1853 } 1854 1855 /* 1856 * device_find_by_xname: 1857 * 1858 * Returns the device of the given name or NULL if it doesn't exist. 1859 */ 1860 device_t 1861 device_find_by_xname(const char *name) 1862 { 1863 device_t dv; 1864 deviter_t di; 1865 1866 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) { 1867 if (strcmp(device_xname(dv), name) == 0) 1868 break; 1869 } 1870 deviter_release(&di); 1871 1872 return dv; 1873 } 1874 1875 /* 1876 * device_find_by_driver_unit: 1877 * 1878 * Returns the device of the given driver name and unit or 1879 * NULL if it doesn't exist. 1880 */ 1881 device_t 1882 device_find_by_driver_unit(const char *name, int unit) 1883 { 1884 struct cfdriver *cd; 1885 1886 if ((cd = config_cfdriver_lookup(name)) == NULL) 1887 return NULL; 1888 return device_lookup(cd, unit); 1889 } 1890 1891 /* 1892 * Power management related functions. 1893 */ 1894 1895 bool 1896 device_pmf_is_registered(device_t dev) 1897 { 1898 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0; 1899 } 1900 1901 bool 1902 device_pmf_driver_suspend(device_t dev PMF_FN_ARGS) 1903 { 1904 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 1905 return true; 1906 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 1907 return false; 1908 if (*dev->dv_driver_suspend != NULL && 1909 !(*dev->dv_driver_suspend)(dev PMF_FN_CALL)) 1910 return false; 1911 1912 dev->dv_flags |= DVF_DRIVER_SUSPENDED; 1913 return true; 1914 } 1915 1916 bool 1917 device_pmf_driver_resume(device_t dev PMF_FN_ARGS) 1918 { 1919 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 1920 return true; 1921 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 1922 return false; 1923 if (*dev->dv_driver_resume != NULL && 1924 !(*dev->dv_driver_resume)(dev PMF_FN_CALL)) 1925 return false; 1926 1927 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED; 1928 return true; 1929 } 1930 1931 bool 1932 device_pmf_driver_shutdown(device_t dev, int how) 1933 { 1934 1935 if (*dev->dv_driver_shutdown != NULL && 1936 !(*dev->dv_driver_shutdown)(dev, how)) 1937 return false; 1938 return true; 1939 } 1940 1941 bool 1942 device_pmf_driver_register(device_t dev, 1943 bool (*suspend)(device_t PMF_FN_PROTO), 1944 bool (*resume)(device_t PMF_FN_PROTO), 1945 bool (*shutdown)(device_t, int)) 1946 { 1947 dev->dv_driver_suspend = suspend; 1948 dev->dv_driver_resume = resume; 1949 dev->dv_driver_shutdown = shutdown; 1950 dev->dv_flags |= DVF_POWER_HANDLERS; 1951 return true; 1952 } 1953 1954 void 1955 device_pmf_driver_deregister(device_t dev) 1956 { 1957 dev->dv_driver_suspend = NULL; 1958 dev->dv_driver_resume = NULL; 1959 dev->dv_flags &= ~DVF_POWER_HANDLERS; 1960 } 1961 1962 bool 1963 device_pmf_driver_child_register(device_t dev) 1964 { 1965 device_t parent = device_parent(dev); 1966 1967 if (parent == NULL || parent->dv_driver_child_register == NULL) 1968 return true; 1969 return (*parent->dv_driver_child_register)(dev); 1970 } 1971 1972 void 1973 device_pmf_driver_set_child_register(device_t dev, 1974 bool (*child_register)(device_t)) 1975 { 1976 dev->dv_driver_child_register = child_register; 1977 } 1978 1979 void * 1980 device_pmf_bus_private(device_t dev) 1981 { 1982 return dev->dv_bus_private; 1983 } 1984 1985 bool 1986 device_pmf_bus_suspend(device_t dev PMF_FN_ARGS) 1987 { 1988 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 1989 return true; 1990 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 || 1991 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 1992 return false; 1993 if (*dev->dv_bus_suspend != NULL && 1994 !(*dev->dv_bus_suspend)(dev PMF_FN_CALL)) 1995 return false; 1996 1997 dev->dv_flags |= DVF_BUS_SUSPENDED; 1998 return true; 1999 } 2000 2001 bool 2002 device_pmf_bus_resume(device_t dev PMF_FN_ARGS) 2003 { 2004 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0) 2005 return true; 2006 if (*dev->dv_bus_resume != NULL && 2007 !(*dev->dv_bus_resume)(dev PMF_FN_CALL)) 2008 return false; 2009 2010 dev->dv_flags &= ~DVF_BUS_SUSPENDED; 2011 return true; 2012 } 2013 2014 bool 2015 device_pmf_bus_shutdown(device_t dev, int how) 2016 { 2017 2018 if (*dev->dv_bus_shutdown != NULL && 2019 !(*dev->dv_bus_shutdown)(dev, how)) 2020 return false; 2021 return true; 2022 } 2023 2024 void 2025 device_pmf_bus_register(device_t dev, void *priv, 2026 bool (*suspend)(device_t PMF_FN_PROTO), 2027 bool (*resume)(device_t PMF_FN_PROTO), 2028 bool (*shutdown)(device_t, int), void (*deregister)(device_t)) 2029 { 2030 dev->dv_bus_private = priv; 2031 dev->dv_bus_resume = resume; 2032 dev->dv_bus_suspend = suspend; 2033 dev->dv_bus_shutdown = shutdown; 2034 dev->dv_bus_deregister = deregister; 2035 } 2036 2037 void 2038 device_pmf_bus_deregister(device_t dev) 2039 { 2040 if (dev->dv_bus_deregister == NULL) 2041 return; 2042 (*dev->dv_bus_deregister)(dev); 2043 dev->dv_bus_private = NULL; 2044 dev->dv_bus_suspend = NULL; 2045 dev->dv_bus_resume = NULL; 2046 dev->dv_bus_deregister = NULL; 2047 } 2048 2049 void * 2050 device_pmf_class_private(device_t dev) 2051 { 2052 return dev->dv_class_private; 2053 } 2054 2055 bool 2056 device_pmf_class_suspend(device_t dev PMF_FN_ARGS) 2057 { 2058 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0) 2059 return true; 2060 if (*dev->dv_class_suspend != NULL && 2061 !(*dev->dv_class_suspend)(dev PMF_FN_CALL)) 2062 return false; 2063 2064 dev->dv_flags |= DVF_CLASS_SUSPENDED; 2065 return true; 2066 } 2067 2068 bool 2069 device_pmf_class_resume(device_t dev PMF_FN_ARGS) 2070 { 2071 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2072 return true; 2073 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 || 2074 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2075 return false; 2076 if (*dev->dv_class_resume != NULL && 2077 !(*dev->dv_class_resume)(dev PMF_FN_CALL)) 2078 return false; 2079 2080 dev->dv_flags &= ~DVF_CLASS_SUSPENDED; 2081 return true; 2082 } 2083 2084 void 2085 device_pmf_class_register(device_t dev, void *priv, 2086 bool (*suspend)(device_t PMF_FN_PROTO), 2087 bool (*resume)(device_t PMF_FN_PROTO), 2088 void (*deregister)(device_t)) 2089 { 2090 dev->dv_class_private = priv; 2091 dev->dv_class_suspend = suspend; 2092 dev->dv_class_resume = resume; 2093 dev->dv_class_deregister = deregister; 2094 } 2095 2096 void 2097 device_pmf_class_deregister(device_t dev) 2098 { 2099 if (dev->dv_class_deregister == NULL) 2100 return; 2101 (*dev->dv_class_deregister)(dev); 2102 dev->dv_class_private = NULL; 2103 dev->dv_class_suspend = NULL; 2104 dev->dv_class_resume = NULL; 2105 dev->dv_class_deregister = NULL; 2106 } 2107 2108 bool 2109 device_active(device_t dev, devactive_t type) 2110 { 2111 size_t i; 2112 2113 if (dev->dv_activity_count == 0) 2114 return false; 2115 2116 for (i = 0; i < dev->dv_activity_count; ++i) 2117 (*dev->dv_activity_handlers[i])(dev, type); 2118 2119 return true; 2120 } 2121 2122 bool 2123 device_active_register(device_t dev, void (*handler)(device_t, devactive_t)) 2124 { 2125 void (**new_handlers)(device_t, devactive_t); 2126 void (**old_handlers)(device_t, devactive_t); 2127 size_t i, new_size; 2128 int s; 2129 2130 old_handlers = dev->dv_activity_handlers; 2131 2132 for (i = 0; i < dev->dv_activity_count; ++i) { 2133 if (old_handlers[i] == handler) 2134 panic("Double registering of idle handlers"); 2135 } 2136 2137 new_size = dev->dv_activity_count + 1; 2138 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, M_WAITOK); 2139 2140 memcpy(new_handlers, old_handlers, 2141 sizeof(void *) * dev->dv_activity_count); 2142 new_handlers[new_size - 1] = handler; 2143 2144 s = splhigh(); 2145 dev->dv_activity_count = new_size; 2146 dev->dv_activity_handlers = new_handlers; 2147 splx(s); 2148 2149 if (old_handlers != NULL) 2150 free(old_handlers, M_DEVBUF); 2151 2152 return true; 2153 } 2154 2155 void 2156 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t)) 2157 { 2158 void (**new_handlers)(device_t, devactive_t); 2159 void (**old_handlers)(device_t, devactive_t); 2160 size_t i, new_size; 2161 int s; 2162 2163 old_handlers = dev->dv_activity_handlers; 2164 2165 for (i = 0; i < dev->dv_activity_count; ++i) { 2166 if (old_handlers[i] == handler) 2167 break; 2168 } 2169 2170 if (i == dev->dv_activity_count) 2171 return; /* XXX panic? */ 2172 2173 new_size = dev->dv_activity_count - 1; 2174 2175 if (new_size == 0) { 2176 new_handlers = NULL; 2177 } else { 2178 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, 2179 M_WAITOK); 2180 memcpy(new_handlers, old_handlers, sizeof(void *) * i); 2181 memcpy(new_handlers + i, old_handlers + i + 1, 2182 sizeof(void *) * (new_size - i)); 2183 } 2184 2185 s = splhigh(); 2186 dev->dv_activity_count = new_size; 2187 dev->dv_activity_handlers = new_handlers; 2188 splx(s); 2189 2190 free(old_handlers, M_DEVBUF); 2191 } 2192 2193 /* 2194 * Device Iteration 2195 * 2196 * deviter_t: a device iterator. Holds state for a "walk" visiting 2197 * each device_t's in the device tree. 2198 * 2199 * deviter_init(di, flags): initialize the device iterator `di' 2200 * to "walk" the device tree. deviter_next(di) will return 2201 * the first device_t in the device tree, or NULL if there are 2202 * no devices. 2203 * 2204 * `flags' is one or more of DEVITER_F_RW, indicating that the 2205 * caller intends to modify the device tree by calling 2206 * config_detach(9) on devices in the order that the iterator 2207 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices 2208 * nearest the "root" of the device tree to be returned, first; 2209 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from 2210 * the root of the device tree, first; and DEVITER_F_SHUTDOWN, 2211 * indicating both that deviter_init() should not respect any 2212 * locks on the device tree, and that deviter_next(di) may run 2213 * in more than one LWP before the walk has finished. 2214 * 2215 * Only one DEVITER_F_RW iterator may be in the device tree at 2216 * once. 2217 * 2218 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW. 2219 * 2220 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and 2221 * DEVITER_F_LEAVES_FIRST are used in combination. 2222 * 2223 * deviter_first(di, flags): initialize the device iterator `di' 2224 * and return the first device_t in the device tree, or NULL 2225 * if there are no devices. The statement 2226 * 2227 * dv = deviter_first(di); 2228 * 2229 * is shorthand for 2230 * 2231 * deviter_init(di); 2232 * dv = deviter_next(di); 2233 * 2234 * deviter_next(di): return the next device_t in the device tree, 2235 * or NULL if there are no more devices. deviter_next(di) 2236 * is undefined if `di' was not initialized with deviter_init() or 2237 * deviter_first(). 2238 * 2239 * deviter_release(di): stops iteration (subsequent calls to 2240 * deviter_next() will return NULL), releases any locks and 2241 * resources held by the device iterator. 2242 * 2243 * Device iteration does not return device_t's in any particular 2244 * order. An iterator will never return the same device_t twice. 2245 * Device iteration is guaranteed to complete---i.e., if deviter_next(di) 2246 * is called repeatedly on the same `di', it will eventually return 2247 * NULL. It is ok to attach/detach devices during device iteration. 2248 */ 2249 void 2250 deviter_init(deviter_t *di, deviter_flags_t flags) 2251 { 2252 device_t dv; 2253 bool rw; 2254 2255 mutex_enter(&alldevs_mtx); 2256 if ((flags & DEVITER_F_SHUTDOWN) != 0) { 2257 flags |= DEVITER_F_RW; 2258 alldevs_nwrite++; 2259 alldevs_writer = NULL; 2260 alldevs_nread = 0; 2261 } else { 2262 rw = (flags & DEVITER_F_RW) != 0; 2263 2264 if (alldevs_nwrite > 0 && alldevs_writer == NULL) 2265 ; 2266 else while ((alldevs_nwrite != 0 && alldevs_writer != curlwp) || 2267 (rw && alldevs_nread != 0)) 2268 cv_wait(&alldevs_cv, &alldevs_mtx); 2269 2270 if (rw) { 2271 if (alldevs_nwrite++ == 0) 2272 alldevs_writer = curlwp; 2273 } else 2274 alldevs_nread++; 2275 } 2276 mutex_exit(&alldevs_mtx); 2277 2278 memset(di, 0, sizeof(*di)); 2279 2280 di->di_flags = flags; 2281 2282 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2283 case DEVITER_F_LEAVES_FIRST: 2284 TAILQ_FOREACH(dv, &alldevs, dv_list) 2285 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth); 2286 break; 2287 case DEVITER_F_ROOT_FIRST: 2288 TAILQ_FOREACH(dv, &alldevs, dv_list) 2289 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth); 2290 break; 2291 default: 2292 break; 2293 } 2294 2295 deviter_reinit(di); 2296 } 2297 2298 static void 2299 deviter_reinit(deviter_t *di) 2300 { 2301 if ((di->di_flags & DEVITER_F_RW) != 0) 2302 di->di_prev = TAILQ_LAST(&alldevs, devicelist); 2303 else 2304 di->di_prev = TAILQ_FIRST(&alldevs); 2305 } 2306 2307 device_t 2308 deviter_first(deviter_t *di, deviter_flags_t flags) 2309 { 2310 deviter_init(di, flags); 2311 return deviter_next(di); 2312 } 2313 2314 static device_t 2315 deviter_next1(deviter_t *di) 2316 { 2317 device_t dv; 2318 2319 dv = di->di_prev; 2320 2321 if (dv == NULL) 2322 ; 2323 else if ((di->di_flags & DEVITER_F_RW) != 0) 2324 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list); 2325 else 2326 di->di_prev = TAILQ_NEXT(dv, dv_list); 2327 2328 return dv; 2329 } 2330 2331 device_t 2332 deviter_next(deviter_t *di) 2333 { 2334 device_t dv = NULL; 2335 2336 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2337 case 0: 2338 return deviter_next1(di); 2339 case DEVITER_F_LEAVES_FIRST: 2340 while (di->di_curdepth >= 0) { 2341 if ((dv = deviter_next1(di)) == NULL) { 2342 di->di_curdepth--; 2343 deviter_reinit(di); 2344 } else if (dv->dv_depth == di->di_curdepth) 2345 break; 2346 } 2347 return dv; 2348 case DEVITER_F_ROOT_FIRST: 2349 while (di->di_curdepth <= di->di_maxdepth) { 2350 if ((dv = deviter_next1(di)) == NULL) { 2351 di->di_curdepth++; 2352 deviter_reinit(di); 2353 } else if (dv->dv_depth == di->di_curdepth) 2354 break; 2355 } 2356 return dv; 2357 default: 2358 return NULL; 2359 } 2360 } 2361 2362 void 2363 deviter_release(deviter_t *di) 2364 { 2365 bool rw = (di->di_flags & DEVITER_F_RW) != 0; 2366 2367 mutex_enter(&alldevs_mtx); 2368 if (alldevs_nwrite > 0 && alldevs_writer == NULL) 2369 --alldevs_nwrite; 2370 else { 2371 2372 if (rw) { 2373 if (--alldevs_nwrite == 0) 2374 alldevs_writer = NULL; 2375 } else 2376 --alldevs_nread; 2377 2378 cv_signal(&alldevs_cv); 2379 } 2380 mutex_exit(&alldevs_mtx); 2381 } 2382