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