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