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