1 /* $NetBSD: subr_autoconf.c,v 1.273 2020/08/01 11:18:26 jdolecek 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.273 2020/08/01 11:18:26 jdolecek Exp $"); 81 82 #ifdef _KERNEL_OPT 83 #include "opt_ddb.h" 84 #include "drvctl.h" 85 #endif 86 87 #include <sys/param.h> 88 #include <sys/device.h> 89 #include <sys/disklabel.h> 90 #include <sys/conf.h> 91 #include <sys/kauth.h> 92 #include <sys/kmem.h> 93 #include <sys/systm.h> 94 #include <sys/kernel.h> 95 #include <sys/errno.h> 96 #include <sys/proc.h> 97 #include <sys/reboot.h> 98 #include <sys/kthread.h> 99 #include <sys/buf.h> 100 #include <sys/dirent.h> 101 #include <sys/mount.h> 102 #include <sys/namei.h> 103 #include <sys/unistd.h> 104 #include <sys/fcntl.h> 105 #include <sys/lockf.h> 106 #include <sys/callout.h> 107 #include <sys/devmon.h> 108 #include <sys/cpu.h> 109 #include <sys/sysctl.h> 110 111 #include <sys/disk.h> 112 113 #include <sys/rndsource.h> 114 115 #include <machine/limits.h> 116 117 /* 118 * Autoconfiguration subroutines. 119 */ 120 121 /* 122 * Device autoconfiguration timings are mixed into the entropy pool. 123 */ 124 static krndsource_t rnd_autoconf_source; 125 126 /* 127 * ioconf.c exports exactly two names: cfdata and cfroots. All system 128 * devices and drivers are found via these tables. 129 */ 130 extern struct cfdata cfdata[]; 131 extern const short cfroots[]; 132 133 /* 134 * List of all cfdriver structures. We use this to detect duplicates 135 * when other cfdrivers are loaded. 136 */ 137 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 138 extern struct cfdriver * const cfdriver_list_initial[]; 139 140 /* 141 * Initial list of cfattach's. 142 */ 143 extern const struct cfattachinit cfattachinit[]; 144 145 /* 146 * List of cfdata tables. We always have one such list -- the one 147 * built statically when the kernel was configured. 148 */ 149 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables); 150 static struct cftable initcftable; 151 152 #define ROOT ((device_t)NULL) 153 154 struct matchinfo { 155 cfsubmatch_t fn; 156 device_t parent; 157 const int *locs; 158 void *aux; 159 struct cfdata *match; 160 int pri; 161 }; 162 163 struct alldevs_foray { 164 int af_s; 165 struct devicelist af_garbage; 166 }; 167 168 static char *number(char *, int); 169 static void mapply(struct matchinfo *, cfdata_t); 170 static device_t config_devalloc(const device_t, const cfdata_t, const int *); 171 static void config_devdelete(device_t); 172 static void config_devunlink(device_t, struct devicelist *); 173 static void config_makeroom(int, struct cfdriver *); 174 static void config_devlink(device_t); 175 static void config_alldevs_enter(struct alldevs_foray *); 176 static void config_alldevs_exit(struct alldevs_foray *); 177 static void config_add_attrib_dict(device_t); 178 179 static void config_collect_garbage(struct devicelist *); 180 static void config_dump_garbage(struct devicelist *); 181 182 static void pmflock_debug(device_t, const char *, int); 183 184 static device_t deviter_next1(deviter_t *); 185 static void deviter_reinit(deviter_t *); 186 187 struct deferred_config { 188 TAILQ_ENTRY(deferred_config) dc_queue; 189 device_t dc_dev; 190 void (*dc_func)(device_t); 191 }; 192 193 TAILQ_HEAD(deferred_config_head, deferred_config); 194 195 static struct deferred_config_head deferred_config_queue = 196 TAILQ_HEAD_INITIALIZER(deferred_config_queue); 197 static struct deferred_config_head interrupt_config_queue = 198 TAILQ_HEAD_INITIALIZER(interrupt_config_queue); 199 static int interrupt_config_threads = 8; 200 static struct deferred_config_head mountroot_config_queue = 201 TAILQ_HEAD_INITIALIZER(mountroot_config_queue); 202 static int mountroot_config_threads = 2; 203 static lwp_t **mountroot_config_lwpids; 204 static size_t mountroot_config_lwpids_size; 205 bool root_is_mounted = false; 206 207 static void config_process_deferred(struct deferred_config_head *, device_t); 208 209 /* Hooks to finalize configuration once all real devices have been found. */ 210 struct finalize_hook { 211 TAILQ_ENTRY(finalize_hook) f_list; 212 int (*f_func)(device_t); 213 device_t f_dev; 214 }; 215 static TAILQ_HEAD(, finalize_hook) config_finalize_list = 216 TAILQ_HEAD_INITIALIZER(config_finalize_list); 217 static int config_finalize_done; 218 219 /* list of all devices */ 220 static struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs); 221 static kmutex_t alldevs_lock __cacheline_aligned; 222 static devgen_t alldevs_gen = 1; 223 static int alldevs_nread = 0; 224 static int alldevs_nwrite = 0; 225 static bool alldevs_garbage = false; 226 227 static int config_pending; /* semaphore for mountroot */ 228 static kmutex_t config_misc_lock; 229 static kcondvar_t config_misc_cv; 230 231 static bool detachall = false; 232 233 #define STREQ(s1, s2) \ 234 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 235 236 static bool config_initialized = false; /* config_init() has been called. */ 237 238 static int config_do_twiddle; 239 static callout_t config_twiddle_ch; 240 241 static void sysctl_detach_setup(struct sysctllog **); 242 243 int no_devmon_insert(const char *, prop_dictionary_t); 244 int (*devmon_insert_vec)(const char *, prop_dictionary_t) = no_devmon_insert; 245 246 typedef int (*cfdriver_fn)(struct cfdriver *); 247 static int 248 frob_cfdrivervec(struct cfdriver * const *cfdriverv, 249 cfdriver_fn drv_do, cfdriver_fn drv_undo, 250 const char *style, bool dopanic) 251 { 252 void (*pr)(const char *, ...) __printflike(1, 2) = 253 dopanic ? panic : printf; 254 int i, error = 0, e2 __diagused; 255 256 for (i = 0; cfdriverv[i] != NULL; i++) { 257 if ((error = drv_do(cfdriverv[i])) != 0) { 258 pr("configure: `%s' driver %s failed: %d", 259 cfdriverv[i]->cd_name, style, error); 260 goto bad; 261 } 262 } 263 264 KASSERT(error == 0); 265 return 0; 266 267 bad: 268 printf("\n"); 269 for (i--; i >= 0; i--) { 270 e2 = drv_undo(cfdriverv[i]); 271 KASSERT(e2 == 0); 272 } 273 274 return error; 275 } 276 277 typedef int (*cfattach_fn)(const char *, struct cfattach *); 278 static int 279 frob_cfattachvec(const struct cfattachinit *cfattachv, 280 cfattach_fn att_do, cfattach_fn att_undo, 281 const char *style, bool dopanic) 282 { 283 const struct cfattachinit *cfai = NULL; 284 void (*pr)(const char *, ...) __printflike(1, 2) = 285 dopanic ? panic : printf; 286 int j = 0, error = 0, e2 __diagused; 287 288 for (cfai = &cfattachv[0]; cfai->cfai_name != NULL; cfai++) { 289 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 290 if ((error = att_do(cfai->cfai_name, 291 cfai->cfai_list[j])) != 0) { 292 pr("configure: attachment `%s' " 293 "of `%s' driver %s failed: %d", 294 cfai->cfai_list[j]->ca_name, 295 cfai->cfai_name, style, error); 296 goto bad; 297 } 298 } 299 } 300 301 KASSERT(error == 0); 302 return 0; 303 304 bad: 305 /* 306 * Rollback in reverse order. dunno if super-important, but 307 * do that anyway. Although the code looks a little like 308 * someone did a little integration (in the math sense). 309 */ 310 printf("\n"); 311 if (cfai) { 312 bool last; 313 314 for (last = false; last == false; ) { 315 if (cfai == &cfattachv[0]) 316 last = true; 317 for (j--; j >= 0; j--) { 318 e2 = att_undo(cfai->cfai_name, 319 cfai->cfai_list[j]); 320 KASSERT(e2 == 0); 321 } 322 if (!last) { 323 cfai--; 324 for (j = 0; cfai->cfai_list[j] != NULL; j++) 325 ; 326 } 327 } 328 } 329 330 return error; 331 } 332 333 /* 334 * Initialize the autoconfiguration data structures. Normally this 335 * is done by configure(), but some platforms need to do this very 336 * early (to e.g. initialize the console). 337 */ 338 void 339 config_init(void) 340 { 341 342 KASSERT(config_initialized == false); 343 344 mutex_init(&alldevs_lock, MUTEX_DEFAULT, IPL_VM); 345 346 mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE); 347 cv_init(&config_misc_cv, "cfgmisc"); 348 349 callout_init(&config_twiddle_ch, CALLOUT_MPSAFE); 350 351 frob_cfdrivervec(cfdriver_list_initial, 352 config_cfdriver_attach, NULL, "bootstrap", true); 353 frob_cfattachvec(cfattachinit, 354 config_cfattach_attach, NULL, "bootstrap", true); 355 356 initcftable.ct_cfdata = cfdata; 357 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 358 359 rnd_attach_source(&rnd_autoconf_source, "autoconf", RND_TYPE_UNKNOWN, 360 RND_FLAG_COLLECT_TIME); 361 362 config_initialized = true; 363 } 364 365 /* 366 * Init or fini drivers and attachments. Either all or none 367 * are processed (via rollback). It would be nice if this were 368 * atomic to outside consumers, but with the current state of 369 * locking ... 370 */ 371 int 372 config_init_component(struct cfdriver * const *cfdriverv, 373 const struct cfattachinit *cfattachv, struct cfdata *cfdatav) 374 { 375 int error; 376 377 if ((error = frob_cfdrivervec(cfdriverv, 378 config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0) 379 return error; 380 if ((error = frob_cfattachvec(cfattachv, 381 config_cfattach_attach, config_cfattach_detach, 382 "init", false)) != 0) { 383 frob_cfdrivervec(cfdriverv, 384 config_cfdriver_detach, NULL, "init rollback", true); 385 return error; 386 } 387 if ((error = config_cfdata_attach(cfdatav, 1)) != 0) { 388 frob_cfattachvec(cfattachv, 389 config_cfattach_detach, NULL, "init rollback", true); 390 frob_cfdrivervec(cfdriverv, 391 config_cfdriver_detach, NULL, "init rollback", true); 392 return error; 393 } 394 395 return 0; 396 } 397 398 int 399 config_fini_component(struct cfdriver * const *cfdriverv, 400 const struct cfattachinit *cfattachv, struct cfdata *cfdatav) 401 { 402 int error; 403 404 if ((error = config_cfdata_detach(cfdatav)) != 0) 405 return error; 406 if ((error = frob_cfattachvec(cfattachv, 407 config_cfattach_detach, config_cfattach_attach, 408 "fini", false)) != 0) { 409 if (config_cfdata_attach(cfdatav, 0) != 0) 410 panic("config_cfdata fini rollback failed"); 411 return error; 412 } 413 if ((error = frob_cfdrivervec(cfdriverv, 414 config_cfdriver_detach, config_cfdriver_attach, 415 "fini", false)) != 0) { 416 frob_cfattachvec(cfattachv, 417 config_cfattach_attach, NULL, "fini rollback", true); 418 if (config_cfdata_attach(cfdatav, 0) != 0) 419 panic("config_cfdata fini rollback failed"); 420 return error; 421 } 422 423 return 0; 424 } 425 426 void 427 config_init_mi(void) 428 { 429 430 if (!config_initialized) 431 config_init(); 432 433 sysctl_detach_setup(NULL); 434 } 435 436 void 437 config_deferred(device_t dev) 438 { 439 config_process_deferred(&deferred_config_queue, dev); 440 config_process_deferred(&interrupt_config_queue, dev); 441 config_process_deferred(&mountroot_config_queue, dev); 442 } 443 444 static void 445 config_interrupts_thread(void *cookie) 446 { 447 struct deferred_config *dc; 448 device_t dev; 449 450 mutex_enter(&config_misc_lock); 451 while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) { 452 TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue); 453 mutex_exit(&config_misc_lock); 454 455 dev = dc->dc_dev; 456 (*dc->dc_func)(dev); 457 if (!device_pmf_is_registered(dev)) 458 aprint_debug_dev(dev, 459 "WARNING: power management not supported\n"); 460 config_pending_decr(dev); 461 kmem_free(dc, sizeof(*dc)); 462 463 mutex_enter(&config_misc_lock); 464 dev->dv_flags &= ~DVF_ATTACH_INPROGRESS; 465 } 466 mutex_exit(&config_misc_lock); 467 468 kthread_exit(0); 469 } 470 471 void 472 config_create_interruptthreads(void) 473 { 474 int i; 475 476 for (i = 0; i < interrupt_config_threads; i++) { 477 (void)kthread_create(PRI_NONE, 0/*XXXSMP */, NULL, 478 config_interrupts_thread, NULL, NULL, "configintr"); 479 } 480 } 481 482 static void 483 config_mountroot_thread(void *cookie) 484 { 485 struct deferred_config *dc; 486 487 mutex_enter(&config_misc_lock); 488 while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) { 489 TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue); 490 mutex_exit(&config_misc_lock); 491 492 (*dc->dc_func)(dc->dc_dev); 493 kmem_free(dc, sizeof(*dc)); 494 495 mutex_enter(&config_misc_lock); 496 } 497 mutex_exit(&config_misc_lock); 498 499 kthread_exit(0); 500 } 501 502 void 503 config_create_mountrootthreads(void) 504 { 505 int i; 506 507 if (!root_is_mounted) 508 root_is_mounted = true; 509 510 mountroot_config_lwpids_size = sizeof(mountroot_config_lwpids) * 511 mountroot_config_threads; 512 mountroot_config_lwpids = kmem_alloc(mountroot_config_lwpids_size, 513 KM_NOSLEEP); 514 KASSERT(mountroot_config_lwpids); 515 for (i = 0; i < mountroot_config_threads; i++) { 516 mountroot_config_lwpids[i] = 0; 517 (void)kthread_create(PRI_NONE, KTHREAD_MUSTJOIN/* XXXSMP */, 518 NULL, config_mountroot_thread, NULL, 519 &mountroot_config_lwpids[i], 520 "configroot"); 521 } 522 } 523 524 void 525 config_finalize_mountroot(void) 526 { 527 int i, error; 528 529 for (i = 0; i < mountroot_config_threads; i++) { 530 if (mountroot_config_lwpids[i] == 0) 531 continue; 532 533 error = kthread_join(mountroot_config_lwpids[i]); 534 if (error) 535 printf("%s: thread %x joined with error %d\n", 536 __func__, i, error); 537 } 538 kmem_free(mountroot_config_lwpids, mountroot_config_lwpids_size); 539 } 540 541 /* 542 * Announce device attach/detach to userland listeners. 543 */ 544 545 int 546 no_devmon_insert(const char *name, prop_dictionary_t p) 547 { 548 549 return ENODEV; 550 } 551 552 static void 553 devmon_report_device(device_t dev, bool isattach) 554 { 555 prop_dictionary_t ev, dict = device_properties(dev); 556 const char *parent; 557 const char *what; 558 const char *where; 559 device_t pdev = device_parent(dev); 560 561 /* If currently no drvctl device, just return */ 562 if (devmon_insert_vec == no_devmon_insert) 563 return; 564 565 ev = prop_dictionary_create(); 566 if (ev == NULL) 567 return; 568 569 what = (isattach ? "device-attach" : "device-detach"); 570 parent = (pdev == NULL ? "root" : device_xname(pdev)); 571 if (prop_dictionary_get_string(dict, "location", &where)) { 572 prop_dictionary_set_string(ev, "location", where); 573 aprint_debug("ev: %s %s at %s in [%s]\n", 574 what, device_xname(dev), parent, where); 575 } 576 if (!prop_dictionary_set_string(ev, "device", device_xname(dev)) || 577 !prop_dictionary_set_string(ev, "parent", parent)) { 578 prop_object_release(ev); 579 return; 580 } 581 582 if ((*devmon_insert_vec)(what, ev) != 0) 583 prop_object_release(ev); 584 } 585 586 /* 587 * Add a cfdriver to the system. 588 */ 589 int 590 config_cfdriver_attach(struct cfdriver *cd) 591 { 592 struct cfdriver *lcd; 593 594 /* Make sure this driver isn't already in the system. */ 595 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 596 if (STREQ(lcd->cd_name, cd->cd_name)) 597 return EEXIST; 598 } 599 600 LIST_INIT(&cd->cd_attach); 601 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 602 603 return 0; 604 } 605 606 /* 607 * Remove a cfdriver from the system. 608 */ 609 int 610 config_cfdriver_detach(struct cfdriver *cd) 611 { 612 struct alldevs_foray af; 613 int i, rc = 0; 614 615 config_alldevs_enter(&af); 616 /* Make sure there are no active instances. */ 617 for (i = 0; i < cd->cd_ndevs; i++) { 618 if (cd->cd_devs[i] != NULL) { 619 rc = EBUSY; 620 break; 621 } 622 } 623 config_alldevs_exit(&af); 624 625 if (rc != 0) 626 return rc; 627 628 /* ...and no attachments loaded. */ 629 if (LIST_EMPTY(&cd->cd_attach) == 0) 630 return EBUSY; 631 632 LIST_REMOVE(cd, cd_list); 633 634 KASSERT(cd->cd_devs == NULL); 635 636 return 0; 637 } 638 639 /* 640 * Look up a cfdriver by name. 641 */ 642 struct cfdriver * 643 config_cfdriver_lookup(const char *name) 644 { 645 struct cfdriver *cd; 646 647 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 648 if (STREQ(cd->cd_name, name)) 649 return cd; 650 } 651 652 return NULL; 653 } 654 655 /* 656 * Add a cfattach to the specified driver. 657 */ 658 int 659 config_cfattach_attach(const char *driver, struct cfattach *ca) 660 { 661 struct cfattach *lca; 662 struct cfdriver *cd; 663 664 cd = config_cfdriver_lookup(driver); 665 if (cd == NULL) 666 return ESRCH; 667 668 /* Make sure this attachment isn't already on this driver. */ 669 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 670 if (STREQ(lca->ca_name, ca->ca_name)) 671 return EEXIST; 672 } 673 674 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 675 676 return 0; 677 } 678 679 /* 680 * Remove a cfattach from the specified driver. 681 */ 682 int 683 config_cfattach_detach(const char *driver, struct cfattach *ca) 684 { 685 struct alldevs_foray af; 686 struct cfdriver *cd; 687 device_t dev; 688 int i, rc = 0; 689 690 cd = config_cfdriver_lookup(driver); 691 if (cd == NULL) 692 return ESRCH; 693 694 config_alldevs_enter(&af); 695 /* Make sure there are no active instances. */ 696 for (i = 0; i < cd->cd_ndevs; i++) { 697 if ((dev = cd->cd_devs[i]) == NULL) 698 continue; 699 if (dev->dv_cfattach == ca) { 700 rc = EBUSY; 701 break; 702 } 703 } 704 config_alldevs_exit(&af); 705 706 if (rc != 0) 707 return rc; 708 709 LIST_REMOVE(ca, ca_list); 710 711 return 0; 712 } 713 714 /* 715 * Look up a cfattach by name. 716 */ 717 static struct cfattach * 718 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 719 { 720 struct cfattach *ca; 721 722 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 723 if (STREQ(ca->ca_name, atname)) 724 return ca; 725 } 726 727 return NULL; 728 } 729 730 /* 731 * Look up a cfattach by driver/attachment name. 732 */ 733 struct cfattach * 734 config_cfattach_lookup(const char *name, const char *atname) 735 { 736 struct cfdriver *cd; 737 738 cd = config_cfdriver_lookup(name); 739 if (cd == NULL) 740 return NULL; 741 742 return config_cfattach_lookup_cd(cd, atname); 743 } 744 745 /* 746 * Apply the matching function and choose the best. This is used 747 * a few times and we want to keep the code small. 748 */ 749 static void 750 mapply(struct matchinfo *m, cfdata_t cf) 751 { 752 int pri; 753 754 if (m->fn != NULL) { 755 pri = (*m->fn)(m->parent, cf, m->locs, m->aux); 756 } else { 757 pri = config_match(m->parent, cf, m->aux); 758 } 759 if (pri > m->pri) { 760 m->match = cf; 761 m->pri = pri; 762 } 763 } 764 765 int 766 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 767 { 768 const struct cfiattrdata *ci; 769 const struct cflocdesc *cl; 770 int nlocs, i; 771 772 ci = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver); 773 KASSERT(ci); 774 nlocs = ci->ci_loclen; 775 KASSERT(!nlocs || locs); 776 for (i = 0; i < nlocs; i++) { 777 cl = &ci->ci_locdesc[i]; 778 if (cl->cld_defaultstr != NULL && 779 cf->cf_loc[i] == cl->cld_default) 780 continue; 781 if (cf->cf_loc[i] == locs[i]) 782 continue; 783 return 0; 784 } 785 786 return config_match(parent, cf, aux); 787 } 788 789 /* 790 * Helper function: check whether the driver supports the interface attribute 791 * and return its descriptor structure. 792 */ 793 static const struct cfiattrdata * 794 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia) 795 { 796 const struct cfiattrdata * const *cpp; 797 798 if (cd->cd_attrs == NULL) 799 return 0; 800 801 for (cpp = cd->cd_attrs; *cpp; cpp++) { 802 if (STREQ((*cpp)->ci_name, ia)) { 803 /* Match. */ 804 return *cpp; 805 } 806 } 807 return 0; 808 } 809 810 /* 811 * Lookup an interface attribute description by name. 812 * If the driver is given, consider only its supported attributes. 813 */ 814 const struct cfiattrdata * 815 cfiattr_lookup(const char *name, const struct cfdriver *cd) 816 { 817 const struct cfdriver *d; 818 const struct cfiattrdata *ia; 819 820 if (cd) 821 return cfdriver_get_iattr(cd, name); 822 823 LIST_FOREACH(d, &allcfdrivers, cd_list) { 824 ia = cfdriver_get_iattr(d, name); 825 if (ia) 826 return ia; 827 } 828 return 0; 829 } 830 831 /* 832 * Determine if `parent' is a potential parent for a device spec based 833 * on `cfp'. 834 */ 835 static int 836 cfparent_match(const device_t parent, const struct cfparent *cfp) 837 { 838 struct cfdriver *pcd; 839 840 /* We don't match root nodes here. */ 841 if (cfp == NULL) 842 return 0; 843 844 pcd = parent->dv_cfdriver; 845 KASSERT(pcd != NULL); 846 847 /* 848 * First, ensure this parent has the correct interface 849 * attribute. 850 */ 851 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr)) 852 return 0; 853 854 /* 855 * If no specific parent device instance was specified (i.e. 856 * we're attaching to the attribute only), we're done! 857 */ 858 if (cfp->cfp_parent == NULL) 859 return 1; 860 861 /* 862 * Check the parent device's name. 863 */ 864 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 865 return 0; /* not the same parent */ 866 867 /* 868 * Make sure the unit number matches. 869 */ 870 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 871 cfp->cfp_unit == parent->dv_unit) 872 return 1; 873 874 /* Unit numbers don't match. */ 875 return 0; 876 } 877 878 /* 879 * Helper for config_cfdata_attach(): check all devices whether it could be 880 * parent any attachment in the config data table passed, and rescan. 881 */ 882 static void 883 rescan_with_cfdata(const struct cfdata *cf) 884 { 885 device_t d; 886 const struct cfdata *cf1; 887 deviter_t di; 888 889 890 /* 891 * "alldevs" is likely longer than a modules's cfdata, so make it 892 * the outer loop. 893 */ 894 for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) { 895 896 if (!(d->dv_cfattach->ca_rescan)) 897 continue; 898 899 for (cf1 = cf; cf1->cf_name; cf1++) { 900 901 if (!cfparent_match(d, cf1->cf_pspec)) 902 continue; 903 904 (*d->dv_cfattach->ca_rescan)(d, 905 cfdata_ifattr(cf1), cf1->cf_loc); 906 907 config_deferred(d); 908 } 909 } 910 deviter_release(&di); 911 } 912 913 /* 914 * Attach a supplemental config data table and rescan potential 915 * parent devices if required. 916 */ 917 int 918 config_cfdata_attach(cfdata_t cf, int scannow) 919 { 920 struct cftable *ct; 921 922 ct = kmem_alloc(sizeof(*ct), KM_SLEEP); 923 ct->ct_cfdata = cf; 924 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list); 925 926 if (scannow) 927 rescan_with_cfdata(cf); 928 929 return 0; 930 } 931 932 /* 933 * Helper for config_cfdata_detach: check whether a device is 934 * found through any attachment in the config data table. 935 */ 936 static int 937 dev_in_cfdata(device_t d, cfdata_t cf) 938 { 939 const struct cfdata *cf1; 940 941 for (cf1 = cf; cf1->cf_name; cf1++) 942 if (d->dv_cfdata == cf1) 943 return 1; 944 945 return 0; 946 } 947 948 /* 949 * Detach a supplemental config data table. Detach all devices found 950 * through that table (and thus keeping references to it) before. 951 */ 952 int 953 config_cfdata_detach(cfdata_t cf) 954 { 955 device_t d; 956 int error = 0; 957 struct cftable *ct; 958 deviter_t di; 959 960 for (d = deviter_first(&di, DEVITER_F_RW); d != NULL; 961 d = deviter_next(&di)) { 962 if (!dev_in_cfdata(d, cf)) 963 continue; 964 if ((error = config_detach(d, 0)) != 0) 965 break; 966 } 967 deviter_release(&di); 968 if (error) { 969 aprint_error_dev(d, "unable to detach instance\n"); 970 return error; 971 } 972 973 TAILQ_FOREACH(ct, &allcftables, ct_list) { 974 if (ct->ct_cfdata == cf) { 975 TAILQ_REMOVE(&allcftables, ct, ct_list); 976 kmem_free(ct, sizeof(*ct)); 977 return 0; 978 } 979 } 980 981 /* not found -- shouldn't happen */ 982 return EINVAL; 983 } 984 985 /* 986 * Invoke the "match" routine for a cfdata entry on behalf of 987 * an external caller, usually a "submatch" routine. 988 */ 989 int 990 config_match(device_t parent, cfdata_t cf, void *aux) 991 { 992 struct cfattach *ca; 993 994 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 995 if (ca == NULL) { 996 /* No attachment for this entry, oh well. */ 997 return 0; 998 } 999 1000 return (*ca->ca_match)(parent, cf, aux); 1001 } 1002 1003 /* 1004 * Iterate over all potential children of some device, calling the given 1005 * function (default being the child's match function) for each one. 1006 * Nonzero returns are matches; the highest value returned is considered 1007 * the best match. Return the `found child' if we got a match, or NULL 1008 * otherwise. The `aux' pointer is simply passed on through. 1009 * 1010 * Note that this function is designed so that it can be used to apply 1011 * an arbitrary function to all potential children (its return value 1012 * can be ignored). 1013 */ 1014 cfdata_t 1015 config_search_loc(cfsubmatch_t fn, device_t parent, 1016 const char *ifattr, const int *locs, void *aux) 1017 { 1018 struct cftable *ct; 1019 cfdata_t cf; 1020 struct matchinfo m; 1021 1022 KASSERT(config_initialized); 1023 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr)); 1024 1025 m.fn = fn; 1026 m.parent = parent; 1027 m.locs = locs; 1028 m.aux = aux; 1029 m.match = NULL; 1030 m.pri = 0; 1031 1032 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1033 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1034 1035 /* We don't match root nodes here. */ 1036 if (!cf->cf_pspec) 1037 continue; 1038 1039 /* 1040 * Skip cf if no longer eligible, otherwise scan 1041 * through parents for one matching `parent', and 1042 * try match function. 1043 */ 1044 if (cf->cf_fstate == FSTATE_FOUND) 1045 continue; 1046 if (cf->cf_fstate == FSTATE_DNOTFOUND || 1047 cf->cf_fstate == FSTATE_DSTAR) 1048 continue; 1049 1050 /* 1051 * If an interface attribute was specified, 1052 * consider only children which attach to 1053 * that attribute. 1054 */ 1055 if (ifattr && !STREQ(ifattr, cfdata_ifattr(cf))) 1056 continue; 1057 1058 if (cfparent_match(parent, cf->cf_pspec)) 1059 mapply(&m, cf); 1060 } 1061 } 1062 return m.match; 1063 } 1064 1065 cfdata_t 1066 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr, 1067 void *aux) 1068 { 1069 1070 return config_search_loc(fn, parent, ifattr, NULL, aux); 1071 } 1072 1073 /* 1074 * Find the given root device. 1075 * This is much like config_search, but there is no parent. 1076 * Don't bother with multiple cfdata tables; the root node 1077 * must always be in the initial table. 1078 */ 1079 cfdata_t 1080 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux) 1081 { 1082 cfdata_t cf; 1083 const short *p; 1084 struct matchinfo m; 1085 1086 m.fn = fn; 1087 m.parent = ROOT; 1088 m.aux = aux; 1089 m.match = NULL; 1090 m.pri = 0; 1091 m.locs = 0; 1092 /* 1093 * Look at root entries for matching name. We do not bother 1094 * with found-state here since only one root should ever be 1095 * searched (and it must be done first). 1096 */ 1097 for (p = cfroots; *p >= 0; p++) { 1098 cf = &cfdata[*p]; 1099 if (strcmp(cf->cf_name, rootname) == 0) 1100 mapply(&m, cf); 1101 } 1102 return m.match; 1103 } 1104 1105 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 1106 1107 /* 1108 * The given `aux' argument describes a device that has been found 1109 * on the given parent, but not necessarily configured. Locate the 1110 * configuration data for that device (using the submatch function 1111 * provided, or using candidates' cd_match configuration driver 1112 * functions) and attach it, and return its device_t. If the device was 1113 * not configured, call the given `print' function and return NULL. 1114 */ 1115 device_t 1116 config_found_sm_loc(device_t parent, 1117 const char *ifattr, const int *locs, void *aux, 1118 cfprint_t print, cfsubmatch_t submatch) 1119 { 1120 cfdata_t cf; 1121 1122 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux))) 1123 return(config_attach_loc(parent, cf, locs, aux, print)); 1124 if (print) { 1125 if (config_do_twiddle && cold) 1126 twiddle(); 1127 aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]); 1128 } 1129 1130 /* 1131 * This has the effect of mixing in a single timestamp to the 1132 * entropy pool. Experiments indicate the estimator will almost 1133 * always attribute one bit of entropy to this sample; analysis 1134 * of device attach/detach timestamps on FreeBSD indicates 4 1135 * bits of entropy/sample so this seems appropriately conservative. 1136 */ 1137 rnd_add_uint32(&rnd_autoconf_source, 0); 1138 return NULL; 1139 } 1140 1141 device_t 1142 config_found_ia(device_t parent, const char *ifattr, void *aux, 1143 cfprint_t print) 1144 { 1145 1146 return config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL); 1147 } 1148 1149 device_t 1150 config_found(device_t parent, void *aux, cfprint_t print) 1151 { 1152 1153 return config_found_sm_loc(parent, NULL, NULL, aux, print, NULL); 1154 } 1155 1156 /* 1157 * As above, but for root devices. 1158 */ 1159 device_t 1160 config_rootfound(const char *rootname, void *aux) 1161 { 1162 cfdata_t cf; 1163 1164 if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL) 1165 return config_attach(ROOT, cf, aux, NULL); 1166 aprint_error("root device %s not configured\n", rootname); 1167 return NULL; 1168 } 1169 1170 /* just like sprintf(buf, "%d") except that it works from the end */ 1171 static char * 1172 number(char *ep, int n) 1173 { 1174 1175 *--ep = 0; 1176 while (n >= 10) { 1177 *--ep = (n % 10) + '0'; 1178 n /= 10; 1179 } 1180 *--ep = n + '0'; 1181 return ep; 1182 } 1183 1184 /* 1185 * Expand the size of the cd_devs array if necessary. 1186 * 1187 * The caller must hold alldevs_lock. config_makeroom() may release and 1188 * re-acquire alldevs_lock, so callers should re-check conditions such 1189 * as alldevs_nwrite == 0 and alldevs_nread == 0 when config_makeroom() 1190 * returns. 1191 */ 1192 static void 1193 config_makeroom(int n, struct cfdriver *cd) 1194 { 1195 int ondevs, nndevs; 1196 device_t *osp, *nsp; 1197 1198 KASSERT(mutex_owned(&alldevs_lock)); 1199 alldevs_nwrite++; 1200 1201 for (nndevs = MAX(4, cd->cd_ndevs); nndevs <= n; nndevs += nndevs) 1202 ; 1203 1204 while (n >= cd->cd_ndevs) { 1205 /* 1206 * Need to expand the array. 1207 */ 1208 ondevs = cd->cd_ndevs; 1209 osp = cd->cd_devs; 1210 1211 /* 1212 * Release alldevs_lock around allocation, which may 1213 * sleep. 1214 */ 1215 mutex_exit(&alldevs_lock); 1216 nsp = kmem_alloc(sizeof(device_t) * nndevs, KM_SLEEP); 1217 mutex_enter(&alldevs_lock); 1218 1219 /* 1220 * If another thread moved the array while we did 1221 * not hold alldevs_lock, try again. 1222 */ 1223 if (cd->cd_devs != osp) { 1224 mutex_exit(&alldevs_lock); 1225 kmem_free(nsp, sizeof(device_t) * nndevs); 1226 mutex_enter(&alldevs_lock); 1227 continue; 1228 } 1229 1230 memset(nsp + ondevs, 0, sizeof(device_t) * (nndevs - ondevs)); 1231 if (ondevs != 0) 1232 memcpy(nsp, cd->cd_devs, sizeof(device_t) * ondevs); 1233 1234 cd->cd_ndevs = nndevs; 1235 cd->cd_devs = nsp; 1236 if (ondevs != 0) { 1237 mutex_exit(&alldevs_lock); 1238 kmem_free(osp, sizeof(device_t) * ondevs); 1239 mutex_enter(&alldevs_lock); 1240 } 1241 } 1242 KASSERT(mutex_owned(&alldevs_lock)); 1243 alldevs_nwrite--; 1244 } 1245 1246 /* 1247 * Put dev into the devices list. 1248 */ 1249 static void 1250 config_devlink(device_t dev) 1251 { 1252 1253 mutex_enter(&alldevs_lock); 1254 1255 KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev); 1256 1257 dev->dv_add_gen = alldevs_gen; 1258 /* It is safe to add a device to the tail of the list while 1259 * readers and writers are in the list. 1260 */ 1261 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); 1262 mutex_exit(&alldevs_lock); 1263 } 1264 1265 static void 1266 config_devfree(device_t dev) 1267 { 1268 KASSERT(dev->dv_flags & DVF_PRIV_ALLOC); 1269 1270 if (dev->dv_cfattach->ca_devsize > 0) 1271 kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize); 1272 kmem_free(dev, sizeof(*dev)); 1273 } 1274 1275 /* 1276 * Caller must hold alldevs_lock. 1277 */ 1278 static void 1279 config_devunlink(device_t dev, struct devicelist *garbage) 1280 { 1281 struct device_garbage *dg = &dev->dv_garbage; 1282 cfdriver_t cd = device_cfdriver(dev); 1283 int i; 1284 1285 KASSERT(mutex_owned(&alldevs_lock)); 1286 1287 /* Unlink from device list. Link to garbage list. */ 1288 TAILQ_REMOVE(&alldevs, dev, dv_list); 1289 TAILQ_INSERT_TAIL(garbage, dev, dv_list); 1290 1291 /* Remove from cfdriver's array. */ 1292 cd->cd_devs[dev->dv_unit] = NULL; 1293 1294 /* 1295 * If the device now has no units in use, unlink its softc array. 1296 */ 1297 for (i = 0; i < cd->cd_ndevs; i++) { 1298 if (cd->cd_devs[i] != NULL) 1299 break; 1300 } 1301 /* Nothing found. Unlink, now. Deallocate, later. */ 1302 if (i == cd->cd_ndevs) { 1303 dg->dg_ndevs = cd->cd_ndevs; 1304 dg->dg_devs = cd->cd_devs; 1305 cd->cd_devs = NULL; 1306 cd->cd_ndevs = 0; 1307 } 1308 } 1309 1310 static void 1311 config_devdelete(device_t dev) 1312 { 1313 struct device_garbage *dg = &dev->dv_garbage; 1314 device_lock_t dvl = device_getlock(dev); 1315 1316 if (dg->dg_devs != NULL) 1317 kmem_free(dg->dg_devs, sizeof(device_t) * dg->dg_ndevs); 1318 1319 cv_destroy(&dvl->dvl_cv); 1320 mutex_destroy(&dvl->dvl_mtx); 1321 1322 KASSERT(dev->dv_properties != NULL); 1323 prop_object_release(dev->dv_properties); 1324 1325 if (dev->dv_activity_handlers) 1326 panic("%s with registered handlers", __func__); 1327 1328 if (dev->dv_locators) { 1329 size_t amount = *--dev->dv_locators; 1330 kmem_free(dev->dv_locators, amount); 1331 } 1332 1333 config_devfree(dev); 1334 } 1335 1336 static int 1337 config_unit_nextfree(cfdriver_t cd, cfdata_t cf) 1338 { 1339 int unit; 1340 1341 if (cf->cf_fstate == FSTATE_STAR) { 1342 for (unit = cf->cf_unit; unit < cd->cd_ndevs; unit++) 1343 if (cd->cd_devs[unit] == NULL) 1344 break; 1345 /* 1346 * unit is now the unit of the first NULL device pointer, 1347 * or max(cd->cd_ndevs,cf->cf_unit). 1348 */ 1349 } else { 1350 unit = cf->cf_unit; 1351 if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL) 1352 unit = -1; 1353 } 1354 return unit; 1355 } 1356 1357 static int 1358 config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf) 1359 { 1360 struct alldevs_foray af; 1361 int unit; 1362 1363 config_alldevs_enter(&af); 1364 for (;;) { 1365 unit = config_unit_nextfree(cd, cf); 1366 if (unit == -1) 1367 break; 1368 if (unit < cd->cd_ndevs) { 1369 cd->cd_devs[unit] = dev; 1370 dev->dv_unit = unit; 1371 break; 1372 } 1373 config_makeroom(unit, cd); 1374 } 1375 config_alldevs_exit(&af); 1376 1377 return unit; 1378 } 1379 1380 static device_t 1381 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs) 1382 { 1383 cfdriver_t cd; 1384 cfattach_t ca; 1385 size_t lname, lunit; 1386 const char *xunit; 1387 int myunit; 1388 char num[10]; 1389 device_t dev; 1390 void *dev_private; 1391 const struct cfiattrdata *ia; 1392 device_lock_t dvl; 1393 1394 cd = config_cfdriver_lookup(cf->cf_name); 1395 if (cd == NULL) 1396 return NULL; 1397 1398 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1399 if (ca == NULL) 1400 return NULL; 1401 1402 /* get memory for all device vars */ 1403 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC); 1404 if (ca->ca_devsize > 0) { 1405 dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP); 1406 } else { 1407 dev_private = NULL; 1408 } 1409 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 1410 1411 dev->dv_class = cd->cd_class; 1412 dev->dv_cfdata = cf; 1413 dev->dv_cfdriver = cd; 1414 dev->dv_cfattach = ca; 1415 dev->dv_activity_count = 0; 1416 dev->dv_activity_handlers = NULL; 1417 dev->dv_private = dev_private; 1418 dev->dv_flags = ca->ca_flags; /* inherit flags from class */ 1419 1420 myunit = config_unit_alloc(dev, cd, cf); 1421 if (myunit == -1) { 1422 config_devfree(dev); 1423 return NULL; 1424 } 1425 1426 /* compute length of name and decimal expansion of unit number */ 1427 lname = strlen(cd->cd_name); 1428 xunit = number(&num[sizeof(num)], myunit); 1429 lunit = &num[sizeof(num)] - xunit; 1430 if (lname + lunit > sizeof(dev->dv_xname)) 1431 panic("config_devalloc: device name too long"); 1432 1433 dvl = device_getlock(dev); 1434 1435 mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE); 1436 cv_init(&dvl->dvl_cv, "pmfsusp"); 1437 1438 memcpy(dev->dv_xname, cd->cd_name, lname); 1439 memcpy(dev->dv_xname + lname, xunit, lunit); 1440 dev->dv_parent = parent; 1441 if (parent != NULL) 1442 dev->dv_depth = parent->dv_depth + 1; 1443 else 1444 dev->dv_depth = 0; 1445 dev->dv_flags |= DVF_ACTIVE; /* always initially active */ 1446 if (locs) { 1447 KASSERT(parent); /* no locators at root */ 1448 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver); 1449 dev->dv_locators = 1450 kmem_alloc(sizeof(int) * (ia->ci_loclen + 1), KM_SLEEP); 1451 *dev->dv_locators++ = sizeof(int) * (ia->ci_loclen + 1); 1452 memcpy(dev->dv_locators, locs, sizeof(int) * ia->ci_loclen); 1453 } 1454 dev->dv_properties = prop_dictionary_create(); 1455 KASSERT(dev->dv_properties != NULL); 1456 1457 prop_dictionary_set_string_nocopy(dev->dv_properties, 1458 "device-driver", dev->dv_cfdriver->cd_name); 1459 prop_dictionary_set_uint16(dev->dv_properties, 1460 "device-unit", dev->dv_unit); 1461 if (parent != NULL) { 1462 prop_dictionary_set_string(dev->dv_properties, 1463 "device-parent", device_xname(parent)); 1464 } 1465 1466 if (dev->dv_cfdriver->cd_attrs != NULL) 1467 config_add_attrib_dict(dev); 1468 1469 return dev; 1470 } 1471 1472 /* 1473 * Create an array of device attach attributes and add it 1474 * to the device's dv_properties dictionary. 1475 * 1476 * <key>interface-attributes</key> 1477 * <array> 1478 * <dict> 1479 * <key>attribute-name</key> 1480 * <string>foo</string> 1481 * <key>locators</key> 1482 * <array> 1483 * <dict> 1484 * <key>loc-name</key> 1485 * <string>foo-loc1</string> 1486 * </dict> 1487 * <dict> 1488 * <key>loc-name</key> 1489 * <string>foo-loc2</string> 1490 * <key>default</key> 1491 * <string>foo-loc2-default</string> 1492 * </dict> 1493 * ... 1494 * </array> 1495 * </dict> 1496 * ... 1497 * </array> 1498 */ 1499 1500 static void 1501 config_add_attrib_dict(device_t dev) 1502 { 1503 int i, j; 1504 const struct cfiattrdata *ci; 1505 prop_dictionary_t attr_dict, loc_dict; 1506 prop_array_t attr_array, loc_array; 1507 1508 if ((attr_array = prop_array_create()) == NULL) 1509 return; 1510 1511 for (i = 0; ; i++) { 1512 if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL) 1513 break; 1514 if ((attr_dict = prop_dictionary_create()) == NULL) 1515 break; 1516 prop_dictionary_set_string_nocopy(attr_dict, "attribute-name", 1517 ci->ci_name); 1518 1519 /* Create an array of the locator names and defaults */ 1520 1521 if (ci->ci_loclen != 0 && 1522 (loc_array = prop_array_create()) != NULL) { 1523 for (j = 0; j < ci->ci_loclen; j++) { 1524 loc_dict = prop_dictionary_create(); 1525 if (loc_dict == NULL) 1526 continue; 1527 prop_dictionary_set_string_nocopy(loc_dict, 1528 "loc-name", ci->ci_locdesc[j].cld_name); 1529 if (ci->ci_locdesc[j].cld_defaultstr != NULL) 1530 prop_dictionary_set_string_nocopy( 1531 loc_dict, "default", 1532 ci->ci_locdesc[j].cld_defaultstr); 1533 prop_array_set(loc_array, j, loc_dict); 1534 prop_object_release(loc_dict); 1535 } 1536 prop_dictionary_set_and_rel(attr_dict, "locators", 1537 loc_array); 1538 } 1539 prop_array_add(attr_array, attr_dict); 1540 prop_object_release(attr_dict); 1541 } 1542 if (i == 0) 1543 prop_object_release(attr_array); 1544 else 1545 prop_dictionary_set_and_rel(dev->dv_properties, 1546 "interface-attributes", attr_array); 1547 1548 return; 1549 } 1550 1551 /* 1552 * Attach a found device. 1553 */ 1554 device_t 1555 config_attach_loc(device_t parent, cfdata_t cf, 1556 const int *locs, void *aux, cfprint_t print) 1557 { 1558 device_t dev; 1559 struct cftable *ct; 1560 const char *drvname; 1561 1562 dev = config_devalloc(parent, cf, locs); 1563 if (!dev) 1564 panic("config_attach: allocation of device softc failed"); 1565 1566 /* XXX redundant - see below? */ 1567 if (cf->cf_fstate != FSTATE_STAR) { 1568 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1569 cf->cf_fstate = FSTATE_FOUND; 1570 } 1571 1572 config_devlink(dev); 1573 1574 if (config_do_twiddle && cold) 1575 twiddle(); 1576 else 1577 aprint_naive("Found "); 1578 /* 1579 * We want the next two printfs for normal, verbose, and quiet, 1580 * but not silent (in which case, we're twiddling, instead). 1581 */ 1582 if (parent == ROOT) { 1583 aprint_naive("%s (root)", device_xname(dev)); 1584 aprint_normal("%s (root)", device_xname(dev)); 1585 } else { 1586 aprint_naive("%s at %s", device_xname(dev), 1587 device_xname(parent)); 1588 aprint_normal("%s at %s", device_xname(dev), 1589 device_xname(parent)); 1590 if (print) 1591 (void) (*print)(aux, NULL); 1592 } 1593 1594 /* 1595 * Before attaching, clobber any unfound devices that are 1596 * otherwise identical. 1597 * XXX code above is redundant? 1598 */ 1599 drvname = dev->dv_cfdriver->cd_name; 1600 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1601 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1602 if (STREQ(cf->cf_name, drvname) && 1603 cf->cf_unit == dev->dv_unit) { 1604 if (cf->cf_fstate == FSTATE_NOTFOUND) 1605 cf->cf_fstate = FSTATE_FOUND; 1606 } 1607 } 1608 } 1609 device_register(dev, aux); 1610 1611 /* Let userland know */ 1612 devmon_report_device(dev, true); 1613 1614 (*dev->dv_cfattach->ca_attach)(parent, dev, aux); 1615 1616 if (((dev->dv_flags & DVF_ATTACH_INPROGRESS) == 0) 1617 && !device_pmf_is_registered(dev)) 1618 aprint_debug_dev(dev, 1619 "WARNING: power management not supported\n"); 1620 1621 config_process_deferred(&deferred_config_queue, dev); 1622 1623 device_register_post_config(dev, aux); 1624 return dev; 1625 } 1626 1627 device_t 1628 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1629 { 1630 1631 return config_attach_loc(parent, cf, NULL, aux, print); 1632 } 1633 1634 /* 1635 * As above, but for pseudo-devices. Pseudo-devices attached in this 1636 * way are silently inserted into the device tree, and their children 1637 * attached. 1638 * 1639 * Note that because pseudo-devices are attached silently, any information 1640 * the attach routine wishes to print should be prefixed with the device 1641 * name by the attach routine. 1642 */ 1643 device_t 1644 config_attach_pseudo(cfdata_t cf) 1645 { 1646 device_t dev; 1647 1648 dev = config_devalloc(ROOT, cf, NULL); 1649 if (!dev) 1650 return NULL; 1651 1652 /* XXX mark busy in cfdata */ 1653 1654 if (cf->cf_fstate != FSTATE_STAR) { 1655 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1656 cf->cf_fstate = FSTATE_FOUND; 1657 } 1658 1659 config_devlink(dev); 1660 1661 #if 0 /* XXXJRT not yet */ 1662 device_register(dev, NULL); /* like a root node */ 1663 #endif 1664 1665 /* Let userland know */ 1666 devmon_report_device(dev, true); 1667 1668 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL); 1669 1670 config_process_deferred(&deferred_config_queue, dev); 1671 return dev; 1672 } 1673 1674 /* 1675 * Caller must hold alldevs_lock. 1676 */ 1677 static void 1678 config_collect_garbage(struct devicelist *garbage) 1679 { 1680 device_t dv; 1681 1682 KASSERT(!cpu_intr_p()); 1683 KASSERT(!cpu_softintr_p()); 1684 KASSERT(mutex_owned(&alldevs_lock)); 1685 1686 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) { 1687 TAILQ_FOREACH(dv, &alldevs, dv_list) { 1688 if (dv->dv_del_gen != 0) 1689 break; 1690 } 1691 if (dv == NULL) { 1692 alldevs_garbage = false; 1693 break; 1694 } 1695 config_devunlink(dv, garbage); 1696 } 1697 KASSERT(mutex_owned(&alldevs_lock)); 1698 } 1699 1700 static void 1701 config_dump_garbage(struct devicelist *garbage) 1702 { 1703 device_t dv; 1704 1705 while ((dv = TAILQ_FIRST(garbage)) != NULL) { 1706 TAILQ_REMOVE(garbage, dv, dv_list); 1707 config_devdelete(dv); 1708 } 1709 } 1710 1711 /* 1712 * Detach a device. Optionally forced (e.g. because of hardware 1713 * removal) and quiet. Returns zero if successful, non-zero 1714 * (an error code) otherwise. 1715 * 1716 * Note that this code wants to be run from a process context, so 1717 * that the detach can sleep to allow processes which have a device 1718 * open to run and unwind their stacks. 1719 */ 1720 int 1721 config_detach(device_t dev, int flags) 1722 { 1723 struct alldevs_foray af; 1724 struct cftable *ct; 1725 cfdata_t cf; 1726 const struct cfattach *ca; 1727 struct cfdriver *cd; 1728 device_t d __diagused; 1729 int rv = 0; 1730 1731 cf = dev->dv_cfdata; 1732 KASSERTMSG((cf == NULL || cf->cf_fstate == FSTATE_FOUND || 1733 cf->cf_fstate == FSTATE_STAR), 1734 "config_detach: %s: bad device fstate: %d", 1735 device_xname(dev), cf ? cf->cf_fstate : -1); 1736 1737 cd = dev->dv_cfdriver; 1738 KASSERT(cd != NULL); 1739 1740 ca = dev->dv_cfattach; 1741 KASSERT(ca != NULL); 1742 1743 mutex_enter(&alldevs_lock); 1744 if (dev->dv_del_gen != 0) { 1745 mutex_exit(&alldevs_lock); 1746 #ifdef DIAGNOSTIC 1747 printf("%s: %s is already detached\n", __func__, 1748 device_xname(dev)); 1749 #endif /* DIAGNOSTIC */ 1750 return ENOENT; 1751 } 1752 alldevs_nwrite++; 1753 mutex_exit(&alldevs_lock); 1754 1755 if (!detachall && 1756 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN && 1757 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) { 1758 rv = EOPNOTSUPP; 1759 } else if (ca->ca_detach != NULL) { 1760 rv = (*ca->ca_detach)(dev, flags); 1761 } else 1762 rv = EOPNOTSUPP; 1763 1764 /* 1765 * If it was not possible to detach the device, then we either 1766 * panic() (for the forced but failed case), or return an error. 1767 * 1768 * If it was possible to detach the device, ensure that the 1769 * device is deactivated. 1770 */ 1771 if (rv == 0) 1772 dev->dv_flags &= ~DVF_ACTIVE; 1773 else if ((flags & DETACH_FORCE) == 0) 1774 goto out; 1775 else { 1776 panic("config_detach: forced detach of %s failed (%d)", 1777 device_xname(dev), rv); 1778 } 1779 1780 /* 1781 * The device has now been successfully detached. 1782 */ 1783 1784 /* Let userland know */ 1785 devmon_report_device(dev, false); 1786 1787 #ifdef DIAGNOSTIC 1788 /* 1789 * Sanity: If you're successfully detached, you should have no 1790 * children. (Note that because children must be attached 1791 * after parents, we only need to search the latter part of 1792 * the list.) 1793 */ 1794 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1795 d = TAILQ_NEXT(d, dv_list)) { 1796 if (d->dv_parent == dev && d->dv_del_gen == 0) { 1797 printf("config_detach: detached device %s" 1798 " has children %s\n", device_xname(dev), 1799 device_xname(d)); 1800 panic("config_detach"); 1801 } 1802 } 1803 #endif 1804 1805 /* notify the parent that the child is gone */ 1806 if (dev->dv_parent) { 1807 device_t p = dev->dv_parent; 1808 if (p->dv_cfattach->ca_childdetached) 1809 (*p->dv_cfattach->ca_childdetached)(p, dev); 1810 } 1811 1812 /* 1813 * Mark cfdata to show that the unit can be reused, if possible. 1814 */ 1815 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1816 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1817 if (STREQ(cf->cf_name, cd->cd_name)) { 1818 if (cf->cf_fstate == FSTATE_FOUND && 1819 cf->cf_unit == dev->dv_unit) 1820 cf->cf_fstate = FSTATE_NOTFOUND; 1821 } 1822 } 1823 } 1824 1825 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1826 aprint_normal_dev(dev, "detached\n"); 1827 1828 out: 1829 config_alldevs_enter(&af); 1830 KASSERT(alldevs_nwrite != 0); 1831 --alldevs_nwrite; 1832 if (rv == 0 && dev->dv_del_gen == 0) { 1833 if (alldevs_nwrite == 0 && alldevs_nread == 0) 1834 config_devunlink(dev, &af.af_garbage); 1835 else { 1836 dev->dv_del_gen = alldevs_gen; 1837 alldevs_garbage = true; 1838 } 1839 } 1840 config_alldevs_exit(&af); 1841 1842 return rv; 1843 } 1844 1845 int 1846 config_detach_children(device_t parent, int flags) 1847 { 1848 device_t dv; 1849 deviter_t di; 1850 int error = 0; 1851 1852 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL; 1853 dv = deviter_next(&di)) { 1854 if (device_parent(dv) != parent) 1855 continue; 1856 if ((error = config_detach(dv, flags)) != 0) 1857 break; 1858 } 1859 deviter_release(&di); 1860 return error; 1861 } 1862 1863 device_t 1864 shutdown_first(struct shutdown_state *s) 1865 { 1866 if (!s->initialized) { 1867 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST); 1868 s->initialized = true; 1869 } 1870 return shutdown_next(s); 1871 } 1872 1873 device_t 1874 shutdown_next(struct shutdown_state *s) 1875 { 1876 device_t dv; 1877 1878 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv)) 1879 ; 1880 1881 if (dv == NULL) 1882 s->initialized = false; 1883 1884 return dv; 1885 } 1886 1887 bool 1888 config_detach_all(int how) 1889 { 1890 static struct shutdown_state s; 1891 device_t curdev; 1892 bool progress = false; 1893 int flags; 1894 1895 if ((how & (RB_NOSYNC|RB_DUMP)) != 0) 1896 return false; 1897 1898 if ((how & RB_POWERDOWN) == RB_POWERDOWN) 1899 flags = DETACH_SHUTDOWN | DETACH_POWEROFF; 1900 else 1901 flags = DETACH_SHUTDOWN; 1902 1903 for (curdev = shutdown_first(&s); curdev != NULL; 1904 curdev = shutdown_next(&s)) { 1905 aprint_debug(" detaching %s, ", device_xname(curdev)); 1906 if (config_detach(curdev, flags) == 0) { 1907 progress = true; 1908 aprint_debug("success."); 1909 } else 1910 aprint_debug("failed."); 1911 } 1912 return progress; 1913 } 1914 1915 static bool 1916 device_is_ancestor_of(device_t ancestor, device_t descendant) 1917 { 1918 device_t dv; 1919 1920 for (dv = descendant; dv != NULL; dv = device_parent(dv)) { 1921 if (device_parent(dv) == ancestor) 1922 return true; 1923 } 1924 return false; 1925 } 1926 1927 int 1928 config_deactivate(device_t dev) 1929 { 1930 deviter_t di; 1931 const struct cfattach *ca; 1932 device_t descendant; 1933 int s, rv = 0, oflags; 1934 1935 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST); 1936 descendant != NULL; 1937 descendant = deviter_next(&di)) { 1938 if (dev != descendant && 1939 !device_is_ancestor_of(dev, descendant)) 1940 continue; 1941 1942 if ((descendant->dv_flags & DVF_ACTIVE) == 0) 1943 continue; 1944 1945 ca = descendant->dv_cfattach; 1946 oflags = descendant->dv_flags; 1947 1948 descendant->dv_flags &= ~DVF_ACTIVE; 1949 if (ca->ca_activate == NULL) 1950 continue; 1951 s = splhigh(); 1952 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE); 1953 splx(s); 1954 if (rv != 0) 1955 descendant->dv_flags = oflags; 1956 } 1957 deviter_release(&di); 1958 return rv; 1959 } 1960 1961 /* 1962 * Defer the configuration of the specified device until all 1963 * of its parent's devices have been attached. 1964 */ 1965 void 1966 config_defer(device_t dev, void (*func)(device_t)) 1967 { 1968 struct deferred_config *dc; 1969 1970 if (dev->dv_parent == NULL) 1971 panic("config_defer: can't defer config of a root device"); 1972 1973 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 1974 1975 config_pending_incr(dev); 1976 1977 mutex_enter(&config_misc_lock); 1978 #ifdef DIAGNOSTIC 1979 struct deferred_config *odc; 1980 TAILQ_FOREACH(odc, &deferred_config_queue, dc_queue) { 1981 if (odc->dc_dev == dev) 1982 panic("config_defer: deferred twice"); 1983 } 1984 #endif 1985 dc->dc_dev = dev; 1986 dc->dc_func = func; 1987 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1988 mutex_exit(&config_misc_lock); 1989 } 1990 1991 /* 1992 * Defer some autoconfiguration for a device until after interrupts 1993 * are enabled. 1994 */ 1995 void 1996 config_interrupts(device_t dev, void (*func)(device_t)) 1997 { 1998 struct deferred_config *dc; 1999 2000 /* 2001 * If interrupts are enabled, callback now. 2002 */ 2003 if (cold == 0) { 2004 (*func)(dev); 2005 return; 2006 } 2007 2008 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 2009 2010 config_pending_incr(dev); 2011 2012 mutex_enter(&config_misc_lock); 2013 #ifdef DIAGNOSTIC 2014 struct deferred_config *odc; 2015 TAILQ_FOREACH(odc, &interrupt_config_queue, dc_queue) { 2016 if (odc->dc_dev == dev) 2017 panic("config_interrupts: deferred twice"); 2018 } 2019 #endif 2020 dc->dc_dev = dev; 2021 dc->dc_func = func; 2022 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 2023 dev->dv_flags |= DVF_ATTACH_INPROGRESS; 2024 mutex_exit(&config_misc_lock); 2025 } 2026 2027 /* 2028 * Defer some autoconfiguration for a device until after root file system 2029 * is mounted (to load firmware etc). 2030 */ 2031 void 2032 config_mountroot(device_t dev, void (*func)(device_t)) 2033 { 2034 struct deferred_config *dc; 2035 2036 /* 2037 * If root file system is mounted, callback now. 2038 */ 2039 if (root_is_mounted) { 2040 (*func)(dev); 2041 return; 2042 } 2043 2044 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 2045 2046 mutex_enter(&config_misc_lock); 2047 #ifdef DIAGNOSTIC 2048 struct deferred_config *odc; 2049 TAILQ_FOREACH(odc, &mountroot_config_queue, dc_queue) { 2050 if (odc->dc_dev == dev) 2051 panic("%s: deferred twice", __func__); 2052 } 2053 #endif 2054 2055 dc->dc_dev = dev; 2056 dc->dc_func = func; 2057 TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue); 2058 mutex_exit(&config_misc_lock); 2059 } 2060 2061 /* 2062 * Process a deferred configuration queue. 2063 */ 2064 static void 2065 config_process_deferred(struct deferred_config_head *queue, device_t parent) 2066 { 2067 struct deferred_config *dc; 2068 2069 mutex_enter(&config_misc_lock); 2070 dc = TAILQ_FIRST(queue); 2071 while (dc) { 2072 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 2073 TAILQ_REMOVE(queue, dc, dc_queue); 2074 mutex_exit(&config_misc_lock); 2075 2076 (*dc->dc_func)(dc->dc_dev); 2077 config_pending_decr(dc->dc_dev); 2078 kmem_free(dc, sizeof(*dc)); 2079 2080 mutex_enter(&config_misc_lock); 2081 /* Restart, queue might have changed */ 2082 dc = TAILQ_FIRST(queue); 2083 } else { 2084 dc = TAILQ_NEXT(dc, dc_queue); 2085 } 2086 } 2087 mutex_exit(&config_misc_lock); 2088 } 2089 2090 /* 2091 * Manipulate the config_pending semaphore. 2092 */ 2093 void 2094 config_pending_incr(device_t dev) 2095 { 2096 2097 mutex_enter(&config_misc_lock); 2098 config_pending++; 2099 #ifdef DEBUG_AUTOCONF 2100 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending); 2101 #endif 2102 mutex_exit(&config_misc_lock); 2103 } 2104 2105 void 2106 config_pending_decr(device_t dev) 2107 { 2108 2109 KASSERT(0 < config_pending); 2110 mutex_enter(&config_misc_lock); 2111 config_pending--; 2112 #ifdef DEBUG_AUTOCONF 2113 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending); 2114 #endif 2115 if (config_pending == 0) 2116 cv_broadcast(&config_misc_cv); 2117 mutex_exit(&config_misc_lock); 2118 } 2119 2120 /* 2121 * Register a "finalization" routine. Finalization routines are 2122 * called iteratively once all real devices have been found during 2123 * autoconfiguration, for as long as any one finalizer has done 2124 * any work. 2125 */ 2126 int 2127 config_finalize_register(device_t dev, int (*fn)(device_t)) 2128 { 2129 struct finalize_hook *f; 2130 2131 /* 2132 * If finalization has already been done, invoke the 2133 * callback function now. 2134 */ 2135 if (config_finalize_done) { 2136 while ((*fn)(dev) != 0) 2137 /* loop */ ; 2138 return 0; 2139 } 2140 2141 /* Ensure this isn't already on the list. */ 2142 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 2143 if (f->f_func == fn && f->f_dev == dev) 2144 return EEXIST; 2145 } 2146 2147 f = kmem_alloc(sizeof(*f), KM_SLEEP); 2148 f->f_func = fn; 2149 f->f_dev = dev; 2150 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 2151 2152 return 0; 2153 } 2154 2155 void 2156 config_finalize(void) 2157 { 2158 struct finalize_hook *f; 2159 struct pdevinit *pdev; 2160 extern struct pdevinit pdevinit[]; 2161 int errcnt, rv; 2162 2163 /* 2164 * Now that device driver threads have been created, wait for 2165 * them to finish any deferred autoconfiguration. 2166 */ 2167 mutex_enter(&config_misc_lock); 2168 while (config_pending != 0) 2169 cv_wait(&config_misc_cv, &config_misc_lock); 2170 mutex_exit(&config_misc_lock); 2171 2172 KERNEL_LOCK(1, NULL); 2173 2174 /* Attach pseudo-devices. */ 2175 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 2176 (*pdev->pdev_attach)(pdev->pdev_count); 2177 2178 /* Run the hooks until none of them does any work. */ 2179 do { 2180 rv = 0; 2181 TAILQ_FOREACH(f, &config_finalize_list, f_list) 2182 rv |= (*f->f_func)(f->f_dev); 2183 } while (rv != 0); 2184 2185 config_finalize_done = 1; 2186 2187 /* Now free all the hooks. */ 2188 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 2189 TAILQ_REMOVE(&config_finalize_list, f, f_list); 2190 kmem_free(f, sizeof(*f)); 2191 } 2192 2193 KERNEL_UNLOCK_ONE(NULL); 2194 2195 errcnt = aprint_get_error_count(); 2196 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 2197 (boothowto & AB_VERBOSE) == 0) { 2198 mutex_enter(&config_misc_lock); 2199 if (config_do_twiddle) { 2200 config_do_twiddle = 0; 2201 printf_nolog(" done.\n"); 2202 } 2203 mutex_exit(&config_misc_lock); 2204 } 2205 if (errcnt != 0) { 2206 printf("WARNING: %d error%s while detecting hardware; " 2207 "check system log.\n", errcnt, 2208 errcnt == 1 ? "" : "s"); 2209 } 2210 } 2211 2212 void 2213 config_twiddle_init(void) 2214 { 2215 2216 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 2217 config_do_twiddle = 1; 2218 } 2219 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL); 2220 } 2221 2222 void 2223 config_twiddle_fn(void *cookie) 2224 { 2225 2226 mutex_enter(&config_misc_lock); 2227 if (config_do_twiddle) { 2228 twiddle(); 2229 callout_schedule(&config_twiddle_ch, mstohz(100)); 2230 } 2231 mutex_exit(&config_misc_lock); 2232 } 2233 2234 static void 2235 config_alldevs_enter(struct alldevs_foray *af) 2236 { 2237 TAILQ_INIT(&af->af_garbage); 2238 mutex_enter(&alldevs_lock); 2239 config_collect_garbage(&af->af_garbage); 2240 } 2241 2242 static void 2243 config_alldevs_exit(struct alldevs_foray *af) 2244 { 2245 mutex_exit(&alldevs_lock); 2246 config_dump_garbage(&af->af_garbage); 2247 } 2248 2249 /* 2250 * device_lookup: 2251 * 2252 * Look up a device instance for a given driver. 2253 */ 2254 device_t 2255 device_lookup(cfdriver_t cd, int unit) 2256 { 2257 device_t dv; 2258 2259 mutex_enter(&alldevs_lock); 2260 if (unit < 0 || unit >= cd->cd_ndevs) 2261 dv = NULL; 2262 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0) 2263 dv = NULL; 2264 mutex_exit(&alldevs_lock); 2265 2266 return dv; 2267 } 2268 2269 /* 2270 * device_lookup_private: 2271 * 2272 * Look up a softc instance for a given driver. 2273 */ 2274 void * 2275 device_lookup_private(cfdriver_t cd, int unit) 2276 { 2277 2278 return device_private(device_lookup(cd, unit)); 2279 } 2280 2281 /* 2282 * device_find_by_xname: 2283 * 2284 * Returns the device of the given name or NULL if it doesn't exist. 2285 */ 2286 device_t 2287 device_find_by_xname(const char *name) 2288 { 2289 device_t dv; 2290 deviter_t di; 2291 2292 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) { 2293 if (strcmp(device_xname(dv), name) == 0) 2294 break; 2295 } 2296 deviter_release(&di); 2297 2298 return dv; 2299 } 2300 2301 /* 2302 * device_find_by_driver_unit: 2303 * 2304 * Returns the device of the given driver name and unit or 2305 * NULL if it doesn't exist. 2306 */ 2307 device_t 2308 device_find_by_driver_unit(const char *name, int unit) 2309 { 2310 struct cfdriver *cd; 2311 2312 if ((cd = config_cfdriver_lookup(name)) == NULL) 2313 return NULL; 2314 return device_lookup(cd, unit); 2315 } 2316 2317 /* 2318 * device_compatible_match: 2319 * 2320 * Match a driver's "compatible" data against a device's 2321 * "compatible" strings. If a match is found, we return 2322 * a weighted match result, and optionally the matching 2323 * entry. 2324 */ 2325 int 2326 device_compatible_match(const char **device_compats, int ndevice_compats, 2327 const struct device_compatible_entry *driver_compats, 2328 const struct device_compatible_entry **matching_entryp) 2329 { 2330 const struct device_compatible_entry *dce = NULL; 2331 int i, match_weight; 2332 2333 if (ndevice_compats == 0 || device_compats == NULL || 2334 driver_compats == NULL) 2335 return 0; 2336 2337 /* 2338 * We take the first match because we start with the most-specific 2339 * device compatible string. 2340 */ 2341 for (i = 0, match_weight = ndevice_compats - 1; 2342 i < ndevice_compats; 2343 i++, match_weight--) { 2344 for (dce = driver_compats; dce->compat != NULL; dce++) { 2345 if (strcmp(dce->compat, device_compats[i]) == 0) { 2346 KASSERT(match_weight >= 0); 2347 if (matching_entryp) 2348 *matching_entryp = dce; 2349 return 1 + match_weight; 2350 } 2351 } 2352 } 2353 return 0; 2354 } 2355 2356 /* 2357 * Power management related functions. 2358 */ 2359 2360 bool 2361 device_pmf_is_registered(device_t dev) 2362 { 2363 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0; 2364 } 2365 2366 bool 2367 device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual) 2368 { 2369 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2370 return true; 2371 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2372 return false; 2373 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2374 dev->dv_driver_suspend != NULL && 2375 !(*dev->dv_driver_suspend)(dev, qual)) 2376 return false; 2377 2378 dev->dv_flags |= DVF_DRIVER_SUSPENDED; 2379 return true; 2380 } 2381 2382 bool 2383 device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual) 2384 { 2385 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 2386 return true; 2387 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 2388 return false; 2389 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2390 dev->dv_driver_resume != NULL && 2391 !(*dev->dv_driver_resume)(dev, qual)) 2392 return false; 2393 2394 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED; 2395 return true; 2396 } 2397 2398 bool 2399 device_pmf_driver_shutdown(device_t dev, int how) 2400 { 2401 2402 if (*dev->dv_driver_shutdown != NULL && 2403 !(*dev->dv_driver_shutdown)(dev, how)) 2404 return false; 2405 return true; 2406 } 2407 2408 bool 2409 device_pmf_driver_register(device_t dev, 2410 bool (*suspend)(device_t, const pmf_qual_t *), 2411 bool (*resume)(device_t, const pmf_qual_t *), 2412 bool (*shutdown)(device_t, int)) 2413 { 2414 dev->dv_driver_suspend = suspend; 2415 dev->dv_driver_resume = resume; 2416 dev->dv_driver_shutdown = shutdown; 2417 dev->dv_flags |= DVF_POWER_HANDLERS; 2418 return true; 2419 } 2420 2421 static const char * 2422 curlwp_name(void) 2423 { 2424 if (curlwp->l_name != NULL) 2425 return curlwp->l_name; 2426 else 2427 return curlwp->l_proc->p_comm; 2428 } 2429 2430 void 2431 device_pmf_driver_deregister(device_t dev) 2432 { 2433 device_lock_t dvl = device_getlock(dev); 2434 2435 dev->dv_driver_suspend = NULL; 2436 dev->dv_driver_resume = NULL; 2437 2438 mutex_enter(&dvl->dvl_mtx); 2439 dev->dv_flags &= ~DVF_POWER_HANDLERS; 2440 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) { 2441 /* Wake a thread that waits for the lock. That 2442 * thread will fail to acquire the lock, and then 2443 * it will wake the next thread that waits for the 2444 * lock, or else it will wake us. 2445 */ 2446 cv_signal(&dvl->dvl_cv); 2447 pmflock_debug(dev, __func__, __LINE__); 2448 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2449 pmflock_debug(dev, __func__, __LINE__); 2450 } 2451 mutex_exit(&dvl->dvl_mtx); 2452 } 2453 2454 bool 2455 device_pmf_driver_child_register(device_t dev) 2456 { 2457 device_t parent = device_parent(dev); 2458 2459 if (parent == NULL || parent->dv_driver_child_register == NULL) 2460 return true; 2461 return (*parent->dv_driver_child_register)(dev); 2462 } 2463 2464 void 2465 device_pmf_driver_set_child_register(device_t dev, 2466 bool (*child_register)(device_t)) 2467 { 2468 dev->dv_driver_child_register = child_register; 2469 } 2470 2471 static void 2472 pmflock_debug(device_t dev, const char *func, int line) 2473 { 2474 device_lock_t dvl = device_getlock(dev); 2475 2476 aprint_debug_dev(dev, 2477 "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n", func, line, 2478 curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait, dev->dv_flags); 2479 } 2480 2481 static bool 2482 device_pmf_lock1(device_t dev) 2483 { 2484 device_lock_t dvl = device_getlock(dev); 2485 2486 while (device_pmf_is_registered(dev) && 2487 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) { 2488 dvl->dvl_nwait++; 2489 pmflock_debug(dev, __func__, __LINE__); 2490 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2491 pmflock_debug(dev, __func__, __LINE__); 2492 dvl->dvl_nwait--; 2493 } 2494 if (!device_pmf_is_registered(dev)) { 2495 pmflock_debug(dev, __func__, __LINE__); 2496 /* We could not acquire the lock, but some other thread may 2497 * wait for it, also. Wake that thread. 2498 */ 2499 cv_signal(&dvl->dvl_cv); 2500 return false; 2501 } 2502 dvl->dvl_nlock++; 2503 dvl->dvl_holder = curlwp; 2504 pmflock_debug(dev, __func__, __LINE__); 2505 return true; 2506 } 2507 2508 bool 2509 device_pmf_lock(device_t dev) 2510 { 2511 bool rc; 2512 device_lock_t dvl = device_getlock(dev); 2513 2514 mutex_enter(&dvl->dvl_mtx); 2515 rc = device_pmf_lock1(dev); 2516 mutex_exit(&dvl->dvl_mtx); 2517 2518 return rc; 2519 } 2520 2521 void 2522 device_pmf_unlock(device_t dev) 2523 { 2524 device_lock_t dvl = device_getlock(dev); 2525 2526 KASSERT(dvl->dvl_nlock > 0); 2527 mutex_enter(&dvl->dvl_mtx); 2528 if (--dvl->dvl_nlock == 0) 2529 dvl->dvl_holder = NULL; 2530 cv_signal(&dvl->dvl_cv); 2531 pmflock_debug(dev, __func__, __LINE__); 2532 mutex_exit(&dvl->dvl_mtx); 2533 } 2534 2535 device_lock_t 2536 device_getlock(device_t dev) 2537 { 2538 return &dev->dv_lock; 2539 } 2540 2541 void * 2542 device_pmf_bus_private(device_t dev) 2543 { 2544 return dev->dv_bus_private; 2545 } 2546 2547 bool 2548 device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual) 2549 { 2550 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 2551 return true; 2552 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 || 2553 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 2554 return false; 2555 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS && 2556 dev->dv_bus_suspend != NULL && 2557 !(*dev->dv_bus_suspend)(dev, qual)) 2558 return false; 2559 2560 dev->dv_flags |= DVF_BUS_SUSPENDED; 2561 return true; 2562 } 2563 2564 bool 2565 device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual) 2566 { 2567 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0) 2568 return true; 2569 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS && 2570 dev->dv_bus_resume != NULL && 2571 !(*dev->dv_bus_resume)(dev, qual)) 2572 return false; 2573 2574 dev->dv_flags &= ~DVF_BUS_SUSPENDED; 2575 return true; 2576 } 2577 2578 bool 2579 device_pmf_bus_shutdown(device_t dev, int how) 2580 { 2581 2582 if (*dev->dv_bus_shutdown != NULL && 2583 !(*dev->dv_bus_shutdown)(dev, how)) 2584 return false; 2585 return true; 2586 } 2587 2588 void 2589 device_pmf_bus_register(device_t dev, void *priv, 2590 bool (*suspend)(device_t, const pmf_qual_t *), 2591 bool (*resume)(device_t, const pmf_qual_t *), 2592 bool (*shutdown)(device_t, int), void (*deregister)(device_t)) 2593 { 2594 dev->dv_bus_private = priv; 2595 dev->dv_bus_resume = resume; 2596 dev->dv_bus_suspend = suspend; 2597 dev->dv_bus_shutdown = shutdown; 2598 dev->dv_bus_deregister = deregister; 2599 } 2600 2601 void 2602 device_pmf_bus_deregister(device_t dev) 2603 { 2604 if (dev->dv_bus_deregister == NULL) 2605 return; 2606 (*dev->dv_bus_deregister)(dev); 2607 dev->dv_bus_private = NULL; 2608 dev->dv_bus_suspend = NULL; 2609 dev->dv_bus_resume = NULL; 2610 dev->dv_bus_deregister = NULL; 2611 } 2612 2613 void * 2614 device_pmf_class_private(device_t dev) 2615 { 2616 return dev->dv_class_private; 2617 } 2618 2619 bool 2620 device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual) 2621 { 2622 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0) 2623 return true; 2624 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS && 2625 dev->dv_class_suspend != NULL && 2626 !(*dev->dv_class_suspend)(dev, qual)) 2627 return false; 2628 2629 dev->dv_flags |= DVF_CLASS_SUSPENDED; 2630 return true; 2631 } 2632 2633 bool 2634 device_pmf_class_resume(device_t dev, const pmf_qual_t *qual) 2635 { 2636 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2637 return true; 2638 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 || 2639 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2640 return false; 2641 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS && 2642 dev->dv_class_resume != NULL && 2643 !(*dev->dv_class_resume)(dev, qual)) 2644 return false; 2645 2646 dev->dv_flags &= ~DVF_CLASS_SUSPENDED; 2647 return true; 2648 } 2649 2650 void 2651 device_pmf_class_register(device_t dev, void *priv, 2652 bool (*suspend)(device_t, const pmf_qual_t *), 2653 bool (*resume)(device_t, const pmf_qual_t *), 2654 void (*deregister)(device_t)) 2655 { 2656 dev->dv_class_private = priv; 2657 dev->dv_class_suspend = suspend; 2658 dev->dv_class_resume = resume; 2659 dev->dv_class_deregister = deregister; 2660 } 2661 2662 void 2663 device_pmf_class_deregister(device_t dev) 2664 { 2665 if (dev->dv_class_deregister == NULL) 2666 return; 2667 (*dev->dv_class_deregister)(dev); 2668 dev->dv_class_private = NULL; 2669 dev->dv_class_suspend = NULL; 2670 dev->dv_class_resume = NULL; 2671 dev->dv_class_deregister = NULL; 2672 } 2673 2674 bool 2675 device_active(device_t dev, devactive_t type) 2676 { 2677 size_t i; 2678 2679 if (dev->dv_activity_count == 0) 2680 return false; 2681 2682 for (i = 0; i < dev->dv_activity_count; ++i) { 2683 if (dev->dv_activity_handlers[i] == NULL) 2684 break; 2685 (*dev->dv_activity_handlers[i])(dev, type); 2686 } 2687 2688 return true; 2689 } 2690 2691 bool 2692 device_active_register(device_t dev, void (*handler)(device_t, devactive_t)) 2693 { 2694 void (**new_handlers)(device_t, devactive_t); 2695 void (**old_handlers)(device_t, devactive_t); 2696 size_t i, old_size, new_size; 2697 int s; 2698 2699 old_handlers = dev->dv_activity_handlers; 2700 old_size = dev->dv_activity_count; 2701 2702 KASSERT(old_size == 0 || old_handlers != NULL); 2703 2704 for (i = 0; i < old_size; ++i) { 2705 KASSERT(old_handlers[i] != handler); 2706 if (old_handlers[i] == NULL) { 2707 old_handlers[i] = handler; 2708 return true; 2709 } 2710 } 2711 2712 new_size = old_size + 4; 2713 new_handlers = kmem_alloc(sizeof(void *) * new_size, KM_SLEEP); 2714 2715 for (i = 0; i < old_size; ++i) 2716 new_handlers[i] = old_handlers[i]; 2717 new_handlers[old_size] = handler; 2718 for (i = old_size+1; i < new_size; ++i) 2719 new_handlers[i] = NULL; 2720 2721 s = splhigh(); 2722 dev->dv_activity_count = new_size; 2723 dev->dv_activity_handlers = new_handlers; 2724 splx(s); 2725 2726 if (old_size > 0) 2727 kmem_free(old_handlers, sizeof(void *) * old_size); 2728 2729 return true; 2730 } 2731 2732 void 2733 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t)) 2734 { 2735 void (**old_handlers)(device_t, devactive_t); 2736 size_t i, old_size; 2737 int s; 2738 2739 old_handlers = dev->dv_activity_handlers; 2740 old_size = dev->dv_activity_count; 2741 2742 for (i = 0; i < old_size; ++i) { 2743 if (old_handlers[i] == handler) 2744 break; 2745 if (old_handlers[i] == NULL) 2746 return; /* XXX panic? */ 2747 } 2748 2749 if (i == old_size) 2750 return; /* XXX panic? */ 2751 2752 for (; i < old_size - 1; ++i) { 2753 if ((old_handlers[i] = old_handlers[i + 1]) != NULL) 2754 continue; 2755 2756 if (i == 0) { 2757 s = splhigh(); 2758 dev->dv_activity_count = 0; 2759 dev->dv_activity_handlers = NULL; 2760 splx(s); 2761 kmem_free(old_handlers, sizeof(void *) * old_size); 2762 } 2763 return; 2764 } 2765 old_handlers[i] = NULL; 2766 } 2767 2768 /* Return true iff the device_t `dev' exists at generation `gen'. */ 2769 static bool 2770 device_exists_at(device_t dv, devgen_t gen) 2771 { 2772 return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) && 2773 dv->dv_add_gen <= gen; 2774 } 2775 2776 static bool 2777 deviter_visits(const deviter_t *di, device_t dv) 2778 { 2779 return device_exists_at(dv, di->di_gen); 2780 } 2781 2782 /* 2783 * Device Iteration 2784 * 2785 * deviter_t: a device iterator. Holds state for a "walk" visiting 2786 * each device_t's in the device tree. 2787 * 2788 * deviter_init(di, flags): initialize the device iterator `di' 2789 * to "walk" the device tree. deviter_next(di) will return 2790 * the first device_t in the device tree, or NULL if there are 2791 * no devices. 2792 * 2793 * `flags' is one or more of DEVITER_F_RW, indicating that the 2794 * caller intends to modify the device tree by calling 2795 * config_detach(9) on devices in the order that the iterator 2796 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices 2797 * nearest the "root" of the device tree to be returned, first; 2798 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from 2799 * the root of the device tree, first; and DEVITER_F_SHUTDOWN, 2800 * indicating both that deviter_init() should not respect any 2801 * locks on the device tree, and that deviter_next(di) may run 2802 * in more than one LWP before the walk has finished. 2803 * 2804 * Only one DEVITER_F_RW iterator may be in the device tree at 2805 * once. 2806 * 2807 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW. 2808 * 2809 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and 2810 * DEVITER_F_LEAVES_FIRST are used in combination. 2811 * 2812 * deviter_first(di, flags): initialize the device iterator `di' 2813 * and return the first device_t in the device tree, or NULL 2814 * if there are no devices. The statement 2815 * 2816 * dv = deviter_first(di); 2817 * 2818 * is shorthand for 2819 * 2820 * deviter_init(di); 2821 * dv = deviter_next(di); 2822 * 2823 * deviter_next(di): return the next device_t in the device tree, 2824 * or NULL if there are no more devices. deviter_next(di) 2825 * is undefined if `di' was not initialized with deviter_init() or 2826 * deviter_first(). 2827 * 2828 * deviter_release(di): stops iteration (subsequent calls to 2829 * deviter_next() will return NULL), releases any locks and 2830 * resources held by the device iterator. 2831 * 2832 * Device iteration does not return device_t's in any particular 2833 * order. An iterator will never return the same device_t twice. 2834 * Device iteration is guaranteed to complete---i.e., if deviter_next(di) 2835 * is called repeatedly on the same `di', it will eventually return 2836 * NULL. It is ok to attach/detach devices during device iteration. 2837 */ 2838 void 2839 deviter_init(deviter_t *di, deviter_flags_t flags) 2840 { 2841 device_t dv; 2842 2843 memset(di, 0, sizeof(*di)); 2844 2845 if ((flags & DEVITER_F_SHUTDOWN) != 0) 2846 flags |= DEVITER_F_RW; 2847 2848 mutex_enter(&alldevs_lock); 2849 if ((flags & DEVITER_F_RW) != 0) 2850 alldevs_nwrite++; 2851 else 2852 alldevs_nread++; 2853 di->di_gen = alldevs_gen++; 2854 di->di_flags = flags; 2855 2856 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2857 case DEVITER_F_LEAVES_FIRST: 2858 TAILQ_FOREACH(dv, &alldevs, dv_list) { 2859 if (!deviter_visits(di, dv)) 2860 continue; 2861 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth); 2862 } 2863 break; 2864 case DEVITER_F_ROOT_FIRST: 2865 TAILQ_FOREACH(dv, &alldevs, dv_list) { 2866 if (!deviter_visits(di, dv)) 2867 continue; 2868 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth); 2869 } 2870 break; 2871 default: 2872 break; 2873 } 2874 2875 deviter_reinit(di); 2876 mutex_exit(&alldevs_lock); 2877 } 2878 2879 static void 2880 deviter_reinit(deviter_t *di) 2881 { 2882 2883 KASSERT(mutex_owned(&alldevs_lock)); 2884 if ((di->di_flags & DEVITER_F_RW) != 0) 2885 di->di_prev = TAILQ_LAST(&alldevs, devicelist); 2886 else 2887 di->di_prev = TAILQ_FIRST(&alldevs); 2888 } 2889 2890 device_t 2891 deviter_first(deviter_t *di, deviter_flags_t flags) 2892 { 2893 2894 deviter_init(di, flags); 2895 return deviter_next(di); 2896 } 2897 2898 static device_t 2899 deviter_next2(deviter_t *di) 2900 { 2901 device_t dv; 2902 2903 KASSERT(mutex_owned(&alldevs_lock)); 2904 2905 dv = di->di_prev; 2906 2907 if (dv == NULL) 2908 return NULL; 2909 2910 if ((di->di_flags & DEVITER_F_RW) != 0) 2911 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list); 2912 else 2913 di->di_prev = TAILQ_NEXT(dv, dv_list); 2914 2915 return dv; 2916 } 2917 2918 static device_t 2919 deviter_next1(deviter_t *di) 2920 { 2921 device_t dv; 2922 2923 KASSERT(mutex_owned(&alldevs_lock)); 2924 2925 do { 2926 dv = deviter_next2(di); 2927 } while (dv != NULL && !deviter_visits(di, dv)); 2928 2929 return dv; 2930 } 2931 2932 device_t 2933 deviter_next(deviter_t *di) 2934 { 2935 device_t dv = NULL; 2936 2937 mutex_enter(&alldevs_lock); 2938 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2939 case 0: 2940 dv = deviter_next1(di); 2941 break; 2942 case DEVITER_F_LEAVES_FIRST: 2943 while (di->di_curdepth >= 0) { 2944 if ((dv = deviter_next1(di)) == NULL) { 2945 di->di_curdepth--; 2946 deviter_reinit(di); 2947 } else if (dv->dv_depth == di->di_curdepth) 2948 break; 2949 } 2950 break; 2951 case DEVITER_F_ROOT_FIRST: 2952 while (di->di_curdepth <= di->di_maxdepth) { 2953 if ((dv = deviter_next1(di)) == NULL) { 2954 di->di_curdepth++; 2955 deviter_reinit(di); 2956 } else if (dv->dv_depth == di->di_curdepth) 2957 break; 2958 } 2959 break; 2960 default: 2961 break; 2962 } 2963 mutex_exit(&alldevs_lock); 2964 2965 return dv; 2966 } 2967 2968 void 2969 deviter_release(deviter_t *di) 2970 { 2971 bool rw = (di->di_flags & DEVITER_F_RW) != 0; 2972 2973 mutex_enter(&alldevs_lock); 2974 if (rw) 2975 --alldevs_nwrite; 2976 else 2977 --alldevs_nread; 2978 /* XXX wake a garbage-collection thread */ 2979 mutex_exit(&alldevs_lock); 2980 } 2981 2982 const char * 2983 cfdata_ifattr(const struct cfdata *cf) 2984 { 2985 return cf->cf_pspec->cfp_iattr; 2986 } 2987 2988 bool 2989 ifattr_match(const char *snull, const char *t) 2990 { 2991 return (snull == NULL) || strcmp(snull, t) == 0; 2992 } 2993 2994 void 2995 null_childdetached(device_t self, device_t child) 2996 { 2997 /* do nothing */ 2998 } 2999 3000 static void 3001 sysctl_detach_setup(struct sysctllog **clog) 3002 { 3003 3004 sysctl_createv(clog, 0, NULL, NULL, 3005 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 3006 CTLTYPE_BOOL, "detachall", 3007 SYSCTL_DESCR("Detach all devices at shutdown"), 3008 NULL, 0, &detachall, 0, 3009 CTL_KERN, CTL_CREATE, CTL_EOL); 3010 } 3011