1 /* $NetBSD: subr_autoconf.c,v 1.204 2010/03/25 19:23:18 pooka 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.204 2010/03/25 19:23:18 pooka 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 kmem_free(nsp, sizeof(device_t[new])); 1122 continue; 1123 } 1124 1125 memset(nsp + old, 0, sizeof(device_t[new - old])); 1126 if (old != 0) 1127 memcpy(nsp, cd->cd_devs, sizeof(device_t[old])); 1128 1129 cd->cd_ndevs = new; 1130 cd->cd_devs = nsp; 1131 if (old != 0) 1132 kmem_free(osp, sizeof(device_t[old])); 1133 } 1134 alldevs_nwrite--; 1135 } 1136 1137 /* 1138 * Put dev into the devices list. 1139 */ 1140 static void 1141 config_devlink(device_t dev) 1142 { 1143 int s; 1144 1145 s = config_alldevs_lock(); 1146 1147 KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev); 1148 1149 dev->dv_add_gen = alldevs_gen; 1150 /* It is safe to add a device to the tail of the list while 1151 * readers and writers are in the list. 1152 */ 1153 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); 1154 config_alldevs_unlock(s); 1155 } 1156 1157 static void 1158 config_devfree(device_t dev) 1159 { 1160 int priv = (dev->dv_flags & DVF_PRIV_ALLOC); 1161 1162 if (dev->dv_cfattach->ca_devsize > 0) 1163 kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize); 1164 if (priv) 1165 kmem_free(dev, sizeof(*dev)); 1166 } 1167 1168 /* 1169 * Caller must hold alldevs_mtx. 1170 */ 1171 static void 1172 config_devunlink(device_t dev, struct devicelist *garbage) 1173 { 1174 struct device_garbage *dg = &dev->dv_garbage; 1175 cfdriver_t cd = device_cfdriver(dev); 1176 int i; 1177 1178 KASSERT(mutex_owned(&alldevs_mtx)); 1179 1180 /* Unlink from device list. Link to garbage list. */ 1181 TAILQ_REMOVE(&alldevs, dev, dv_list); 1182 TAILQ_INSERT_TAIL(garbage, dev, dv_list); 1183 1184 /* Remove from cfdriver's array. */ 1185 cd->cd_devs[dev->dv_unit] = NULL; 1186 1187 /* 1188 * If the device now has no units in use, unlink its softc array. 1189 */ 1190 for (i = 0; i < cd->cd_ndevs; i++) { 1191 if (cd->cd_devs[i] != NULL) 1192 break; 1193 } 1194 /* Nothing found. Unlink, now. Deallocate, later. */ 1195 if (i == cd->cd_ndevs) { 1196 dg->dg_ndevs = cd->cd_ndevs; 1197 dg->dg_devs = cd->cd_devs; 1198 cd->cd_devs = NULL; 1199 cd->cd_ndevs = 0; 1200 } 1201 } 1202 1203 static void 1204 config_devdelete(device_t dev) 1205 { 1206 struct device_garbage *dg = &dev->dv_garbage; 1207 device_lock_t dvl = device_getlock(dev); 1208 1209 if (dg->dg_devs != NULL) 1210 kmem_free(dg->dg_devs, sizeof(device_t[dg->dg_ndevs])); 1211 1212 cv_destroy(&dvl->dvl_cv); 1213 mutex_destroy(&dvl->dvl_mtx); 1214 1215 KASSERT(dev->dv_properties != NULL); 1216 prop_object_release(dev->dv_properties); 1217 1218 if (dev->dv_activity_handlers) 1219 panic("%s with registered handlers", __func__); 1220 1221 if (dev->dv_locators) { 1222 size_t amount = *--dev->dv_locators; 1223 kmem_free(dev->dv_locators, amount); 1224 } 1225 1226 config_devfree(dev); 1227 } 1228 1229 static int 1230 config_unit_nextfree(cfdriver_t cd, cfdata_t cf) 1231 { 1232 int unit; 1233 1234 if (cf->cf_fstate == FSTATE_STAR) { 1235 for (unit = cf->cf_unit; unit < cd->cd_ndevs; unit++) 1236 if (cd->cd_devs[unit] == NULL) 1237 break; 1238 /* 1239 * unit is now the unit of the first NULL device pointer, 1240 * or max(cd->cd_ndevs,cf->cf_unit). 1241 */ 1242 } else { 1243 unit = cf->cf_unit; 1244 if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL) 1245 unit = -1; 1246 } 1247 return unit; 1248 } 1249 1250 static int 1251 config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf) 1252 { 1253 struct alldevs_foray af; 1254 int unit; 1255 1256 config_alldevs_enter(&af); 1257 for (;;) { 1258 unit = config_unit_nextfree(cd, cf); 1259 if (unit == -1) 1260 break; 1261 if (unit < cd->cd_ndevs) { 1262 cd->cd_devs[unit] = dev; 1263 dev->dv_unit = unit; 1264 break; 1265 } 1266 config_makeroom(unit, cd); 1267 } 1268 config_alldevs_exit(&af); 1269 1270 return unit; 1271 } 1272 1273 static device_t 1274 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs) 1275 { 1276 cfdriver_t cd; 1277 cfattach_t ca; 1278 size_t lname, lunit; 1279 const char *xunit; 1280 int myunit; 1281 char num[10]; 1282 device_t dev; 1283 void *dev_private; 1284 const struct cfiattrdata *ia; 1285 device_lock_t dvl; 1286 1287 cd = config_cfdriver_lookup(cf->cf_name); 1288 if (cd == NULL) 1289 return NULL; 1290 1291 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1292 if (ca == NULL) 1293 return NULL; 1294 1295 if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 && 1296 ca->ca_devsize < sizeof(struct device)) 1297 panic("config_devalloc: %s", cf->cf_atname); 1298 1299 /* get memory for all device vars */ 1300 KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device)); 1301 if (ca->ca_devsize > 0) { 1302 dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP); 1303 if (dev_private == NULL) 1304 panic("config_devalloc: memory allocation for device softc failed"); 1305 } else { 1306 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC); 1307 dev_private = NULL; 1308 } 1309 1310 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) { 1311 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 1312 } else { 1313 dev = dev_private; 1314 } 1315 if (dev == NULL) 1316 panic("config_devalloc: memory allocation for device_t failed"); 1317 1318 dev->dv_class = cd->cd_class; 1319 dev->dv_cfdata = cf; 1320 dev->dv_cfdriver = cd; 1321 dev->dv_cfattach = ca; 1322 dev->dv_activity_count = 0; 1323 dev->dv_activity_handlers = NULL; 1324 dev->dv_private = dev_private; 1325 dev->dv_flags = ca->ca_flags; /* inherit flags from class */ 1326 1327 myunit = config_unit_alloc(dev, cd, cf); 1328 if (myunit == -1) { 1329 config_devfree(dev); 1330 return NULL; 1331 } 1332 1333 /* compute length of name and decimal expansion of unit number */ 1334 lname = strlen(cd->cd_name); 1335 xunit = number(&num[sizeof(num)], myunit); 1336 lunit = &num[sizeof(num)] - xunit; 1337 if (lname + lunit > sizeof(dev->dv_xname)) 1338 panic("config_devalloc: device name too long"); 1339 1340 dvl = device_getlock(dev); 1341 1342 mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE); 1343 cv_init(&dvl->dvl_cv, "pmfsusp"); 1344 1345 memcpy(dev->dv_xname, cd->cd_name, lname); 1346 memcpy(dev->dv_xname + lname, xunit, lunit); 1347 dev->dv_parent = parent; 1348 if (parent != NULL) 1349 dev->dv_depth = parent->dv_depth + 1; 1350 else 1351 dev->dv_depth = 0; 1352 dev->dv_flags |= DVF_ACTIVE; /* always initially active */ 1353 if (locs) { 1354 KASSERT(parent); /* no locators at root */ 1355 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver); 1356 dev->dv_locators = 1357 kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP); 1358 *dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]); 1359 memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen])); 1360 } 1361 dev->dv_properties = prop_dictionary_create(); 1362 KASSERT(dev->dv_properties != NULL); 1363 1364 prop_dictionary_set_cstring_nocopy(dev->dv_properties, 1365 "device-driver", dev->dv_cfdriver->cd_name); 1366 prop_dictionary_set_uint16(dev->dv_properties, 1367 "device-unit", dev->dv_unit); 1368 1369 return dev; 1370 } 1371 1372 /* 1373 * Attach a found device. 1374 */ 1375 device_t 1376 config_attach_loc(device_t parent, cfdata_t cf, 1377 const int *locs, void *aux, cfprint_t print) 1378 { 1379 device_t dev; 1380 struct cftable *ct; 1381 const char *drvname; 1382 1383 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1384 if (splash_progress_state) 1385 splash_progress_update(splash_progress_state); 1386 #endif 1387 1388 dev = config_devalloc(parent, cf, locs); 1389 if (!dev) 1390 panic("config_attach: allocation of device softc failed"); 1391 1392 /* XXX redundant - see below? */ 1393 if (cf->cf_fstate != FSTATE_STAR) { 1394 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1395 cf->cf_fstate = FSTATE_FOUND; 1396 } 1397 1398 config_devlink(dev); 1399 1400 if (config_do_twiddle && cold) 1401 twiddle(); 1402 else 1403 aprint_naive("Found "); 1404 /* 1405 * We want the next two printfs for normal, verbose, and quiet, 1406 * but not silent (in which case, we're twiddling, instead). 1407 */ 1408 if (parent == ROOT) { 1409 aprint_naive("%s (root)", device_xname(dev)); 1410 aprint_normal("%s (root)", device_xname(dev)); 1411 } else { 1412 aprint_naive("%s at %s", device_xname(dev), device_xname(parent)); 1413 aprint_normal("%s at %s", device_xname(dev), device_xname(parent)); 1414 if (print) 1415 (void) (*print)(aux, NULL); 1416 } 1417 1418 /* 1419 * Before attaching, clobber any unfound devices that are 1420 * otherwise identical. 1421 * XXX code above is redundant? 1422 */ 1423 drvname = dev->dv_cfdriver->cd_name; 1424 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1425 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1426 if (STREQ(cf->cf_name, drvname) && 1427 cf->cf_unit == dev->dv_unit) { 1428 if (cf->cf_fstate == FSTATE_NOTFOUND) 1429 cf->cf_fstate = FSTATE_FOUND; 1430 } 1431 } 1432 } 1433 #ifdef __HAVE_DEVICE_REGISTER 1434 device_register(dev, aux); 1435 #endif 1436 1437 /* Let userland know */ 1438 devmon_report_device(dev, true); 1439 1440 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1441 if (splash_progress_state) 1442 splash_progress_update(splash_progress_state); 1443 #endif 1444 (*dev->dv_cfattach->ca_attach)(parent, dev, aux); 1445 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1446 if (splash_progress_state) 1447 splash_progress_update(splash_progress_state); 1448 #endif 1449 1450 if (!device_pmf_is_registered(dev)) 1451 aprint_debug_dev(dev, "WARNING: power management not supported\n"); 1452 1453 config_process_deferred(&deferred_config_queue, dev); 1454 1455 #ifdef __HAVE_DEVICE_REGISTER_POSTCONFIG 1456 device_register_post_config(dev, aux); 1457 #endif 1458 return dev; 1459 } 1460 1461 device_t 1462 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1463 { 1464 1465 return config_attach_loc(parent, cf, NULL, aux, print); 1466 } 1467 1468 /* 1469 * As above, but for pseudo-devices. Pseudo-devices attached in this 1470 * way are silently inserted into the device tree, and their children 1471 * attached. 1472 * 1473 * Note that because pseudo-devices are attached silently, any information 1474 * the attach routine wishes to print should be prefixed with the device 1475 * name by the attach routine. 1476 */ 1477 device_t 1478 config_attach_pseudo(cfdata_t cf) 1479 { 1480 device_t dev; 1481 1482 dev = config_devalloc(ROOT, cf, NULL); 1483 if (!dev) 1484 return NULL; 1485 1486 /* XXX mark busy in cfdata */ 1487 1488 if (cf->cf_fstate != FSTATE_STAR) { 1489 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1490 cf->cf_fstate = FSTATE_FOUND; 1491 } 1492 1493 config_devlink(dev); 1494 1495 #if 0 /* XXXJRT not yet */ 1496 #ifdef __HAVE_DEVICE_REGISTER 1497 device_register(dev, NULL); /* like a root node */ 1498 #endif 1499 #endif 1500 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL); 1501 config_process_deferred(&deferred_config_queue, dev); 1502 return dev; 1503 } 1504 1505 /* 1506 * Caller must hold alldevs_mtx. 1507 */ 1508 static void 1509 config_collect_garbage(struct devicelist *garbage) 1510 { 1511 device_t dv; 1512 1513 KASSERT(!cpu_intr_p()); 1514 KASSERT(!cpu_softintr_p()); 1515 KASSERT(mutex_owned(&alldevs_mtx)); 1516 1517 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) { 1518 TAILQ_FOREACH(dv, &alldevs, dv_list) { 1519 if (dv->dv_del_gen != 0) 1520 break; 1521 } 1522 if (dv == NULL) { 1523 alldevs_garbage = false; 1524 break; 1525 } 1526 config_devunlink(dv, garbage); 1527 } 1528 KASSERT(mutex_owned(&alldevs_mtx)); 1529 } 1530 1531 static void 1532 config_dump_garbage(struct devicelist *garbage) 1533 { 1534 device_t dv; 1535 1536 while ((dv = TAILQ_FIRST(garbage)) != NULL) { 1537 TAILQ_REMOVE(garbage, dv, dv_list); 1538 config_devdelete(dv); 1539 } 1540 } 1541 1542 /* 1543 * Detach a device. Optionally forced (e.g. because of hardware 1544 * removal) and quiet. Returns zero if successful, non-zero 1545 * (an error code) otherwise. 1546 * 1547 * Note that this code wants to be run from a process context, so 1548 * that the detach can sleep to allow processes which have a device 1549 * open to run and unwind their stacks. 1550 */ 1551 int 1552 config_detach(device_t dev, int flags) 1553 { 1554 struct alldevs_foray af; 1555 struct cftable *ct; 1556 cfdata_t cf; 1557 const struct cfattach *ca; 1558 struct cfdriver *cd; 1559 #ifdef DIAGNOSTIC 1560 device_t d; 1561 #endif 1562 int rv = 0, s; 1563 1564 #ifdef DIAGNOSTIC 1565 cf = dev->dv_cfdata; 1566 if (cf != NULL && cf->cf_fstate != FSTATE_FOUND && 1567 cf->cf_fstate != FSTATE_STAR) 1568 panic("config_detach: %s: bad device fstate %d", 1569 device_xname(dev), cf ? cf->cf_fstate : -1); 1570 #endif 1571 cd = dev->dv_cfdriver; 1572 KASSERT(cd != NULL); 1573 1574 ca = dev->dv_cfattach; 1575 KASSERT(ca != NULL); 1576 1577 s = config_alldevs_lock(); 1578 if (dev->dv_del_gen != 0) { 1579 config_alldevs_unlock(s); 1580 #ifdef DIAGNOSTIC 1581 printf("%s: %s is already detached\n", __func__, 1582 device_xname(dev)); 1583 #endif /* DIAGNOSTIC */ 1584 return ENOENT; 1585 } 1586 alldevs_nwrite++; 1587 config_alldevs_unlock(s); 1588 1589 if (!detachall && 1590 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN && 1591 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) { 1592 rv = EOPNOTSUPP; 1593 } else if (ca->ca_detach != NULL) { 1594 rv = (*ca->ca_detach)(dev, flags); 1595 } else 1596 rv = EOPNOTSUPP; 1597 1598 /* 1599 * If it was not possible to detach the device, then we either 1600 * panic() (for the forced but failed case), or return an error. 1601 * 1602 * If it was possible to detach the device, ensure that the 1603 * device is deactivated. 1604 */ 1605 if (rv == 0) 1606 dev->dv_flags &= ~DVF_ACTIVE; 1607 else if ((flags & DETACH_FORCE) == 0) 1608 goto out; 1609 else { 1610 panic("config_detach: forced detach of %s failed (%d)", 1611 device_xname(dev), rv); 1612 } 1613 1614 /* 1615 * The device has now been successfully detached. 1616 */ 1617 1618 /* Let userland know */ 1619 devmon_report_device(dev, false); 1620 1621 #ifdef DIAGNOSTIC 1622 /* 1623 * Sanity: If you're successfully detached, you should have no 1624 * children. (Note that because children must be attached 1625 * after parents, we only need to search the latter part of 1626 * the list.) 1627 */ 1628 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1629 d = TAILQ_NEXT(d, dv_list)) { 1630 if (d->dv_parent == dev && d->dv_del_gen == 0) { 1631 printf("config_detach: detached device %s" 1632 " has children %s\n", device_xname(dev), device_xname(d)); 1633 panic("config_detach"); 1634 } 1635 } 1636 #endif 1637 1638 /* notify the parent that the child is gone */ 1639 if (dev->dv_parent) { 1640 device_t p = dev->dv_parent; 1641 if (p->dv_cfattach->ca_childdetached) 1642 (*p->dv_cfattach->ca_childdetached)(p, dev); 1643 } 1644 1645 /* 1646 * Mark cfdata to show that the unit can be reused, if possible. 1647 */ 1648 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1649 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1650 if (STREQ(cf->cf_name, cd->cd_name)) { 1651 if (cf->cf_fstate == FSTATE_FOUND && 1652 cf->cf_unit == dev->dv_unit) 1653 cf->cf_fstate = FSTATE_NOTFOUND; 1654 } 1655 } 1656 } 1657 1658 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1659 aprint_normal_dev(dev, "detached\n"); 1660 1661 out: 1662 config_alldevs_enter(&af); 1663 KASSERT(alldevs_nwrite != 0); 1664 --alldevs_nwrite; 1665 if (rv == 0 && dev->dv_del_gen == 0) 1666 config_devunlink(dev, &af.af_garbage); 1667 config_alldevs_exit(&af); 1668 1669 return rv; 1670 } 1671 1672 int 1673 config_detach_children(device_t parent, int flags) 1674 { 1675 device_t dv; 1676 deviter_t di; 1677 int error = 0; 1678 1679 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL; 1680 dv = deviter_next(&di)) { 1681 if (device_parent(dv) != parent) 1682 continue; 1683 if ((error = config_detach(dv, flags)) != 0) 1684 break; 1685 } 1686 deviter_release(&di); 1687 return error; 1688 } 1689 1690 device_t 1691 shutdown_first(struct shutdown_state *s) 1692 { 1693 if (!s->initialized) { 1694 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST); 1695 s->initialized = true; 1696 } 1697 return shutdown_next(s); 1698 } 1699 1700 device_t 1701 shutdown_next(struct shutdown_state *s) 1702 { 1703 device_t dv; 1704 1705 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv)) 1706 ; 1707 1708 if (dv == NULL) 1709 s->initialized = false; 1710 1711 return dv; 1712 } 1713 1714 bool 1715 config_detach_all(int how) 1716 { 1717 static struct shutdown_state s; 1718 device_t curdev; 1719 bool progress = false; 1720 1721 if ((how & RB_NOSYNC) != 0) 1722 return false; 1723 1724 for (curdev = shutdown_first(&s); curdev != NULL; 1725 curdev = shutdown_next(&s)) { 1726 aprint_debug(" detaching %s, ", device_xname(curdev)); 1727 if (config_detach(curdev, DETACH_SHUTDOWN) == 0) { 1728 progress = true; 1729 aprint_debug("success."); 1730 } else 1731 aprint_debug("failed."); 1732 } 1733 return progress; 1734 } 1735 1736 static bool 1737 device_is_ancestor_of(device_t ancestor, device_t descendant) 1738 { 1739 device_t dv; 1740 1741 for (dv = descendant; dv != NULL; dv = device_parent(dv)) { 1742 if (device_parent(dv) == ancestor) 1743 return true; 1744 } 1745 return false; 1746 } 1747 1748 int 1749 config_deactivate(device_t dev) 1750 { 1751 deviter_t di; 1752 const struct cfattach *ca; 1753 device_t descendant; 1754 int s, rv = 0, oflags; 1755 1756 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST); 1757 descendant != NULL; 1758 descendant = deviter_next(&di)) { 1759 if (dev != descendant && 1760 !device_is_ancestor_of(dev, descendant)) 1761 continue; 1762 1763 if ((descendant->dv_flags & DVF_ACTIVE) == 0) 1764 continue; 1765 1766 ca = descendant->dv_cfattach; 1767 oflags = descendant->dv_flags; 1768 1769 descendant->dv_flags &= ~DVF_ACTIVE; 1770 if (ca->ca_activate == NULL) 1771 continue; 1772 s = splhigh(); 1773 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE); 1774 splx(s); 1775 if (rv != 0) 1776 descendant->dv_flags = oflags; 1777 } 1778 deviter_release(&di); 1779 return rv; 1780 } 1781 1782 /* 1783 * Defer the configuration of the specified device until all 1784 * of its parent's devices have been attached. 1785 */ 1786 void 1787 config_defer(device_t dev, void (*func)(device_t)) 1788 { 1789 struct deferred_config *dc; 1790 1791 if (dev->dv_parent == NULL) 1792 panic("config_defer: can't defer config of a root device"); 1793 1794 #ifdef DIAGNOSTIC 1795 TAILQ_FOREACH(dc, &deferred_config_queue, dc_queue) { 1796 if (dc->dc_dev == dev) 1797 panic("config_defer: deferred twice"); 1798 } 1799 #endif 1800 1801 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 1802 if (dc == NULL) 1803 panic("config_defer: unable to allocate callback"); 1804 1805 dc->dc_dev = dev; 1806 dc->dc_func = func; 1807 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1808 config_pending_incr(); 1809 } 1810 1811 /* 1812 * Defer some autoconfiguration for a device until after interrupts 1813 * are enabled. 1814 */ 1815 void 1816 config_interrupts(device_t dev, void (*func)(device_t)) 1817 { 1818 struct deferred_config *dc; 1819 1820 /* 1821 * If interrupts are enabled, callback now. 1822 */ 1823 if (cold == 0) { 1824 (*func)(dev); 1825 return; 1826 } 1827 1828 #ifdef DIAGNOSTIC 1829 TAILQ_FOREACH(dc, &interrupt_config_queue, dc_queue) { 1830 if (dc->dc_dev == dev) 1831 panic("config_interrupts: deferred twice"); 1832 } 1833 #endif 1834 1835 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 1836 if (dc == NULL) 1837 panic("config_interrupts: unable to allocate callback"); 1838 1839 dc->dc_dev = dev; 1840 dc->dc_func = func; 1841 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1842 config_pending_incr(); 1843 } 1844 1845 /* 1846 * Process a deferred configuration queue. 1847 */ 1848 static void 1849 config_process_deferred(struct deferred_config_head *queue, 1850 device_t parent) 1851 { 1852 struct deferred_config *dc, *ndc; 1853 1854 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 1855 ndc = TAILQ_NEXT(dc, dc_queue); 1856 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 1857 TAILQ_REMOVE(queue, dc, dc_queue); 1858 (*dc->dc_func)(dc->dc_dev); 1859 kmem_free(dc, sizeof(*dc)); 1860 config_pending_decr(); 1861 } 1862 } 1863 } 1864 1865 /* 1866 * Manipulate the config_pending semaphore. 1867 */ 1868 void 1869 config_pending_incr(void) 1870 { 1871 1872 mutex_enter(&config_misc_lock); 1873 config_pending++; 1874 mutex_exit(&config_misc_lock); 1875 } 1876 1877 void 1878 config_pending_decr(void) 1879 { 1880 1881 #ifdef DIAGNOSTIC 1882 if (config_pending == 0) 1883 panic("config_pending_decr: config_pending == 0"); 1884 #endif 1885 mutex_enter(&config_misc_lock); 1886 config_pending--; 1887 if (config_pending == 0) 1888 cv_broadcast(&config_misc_cv); 1889 mutex_exit(&config_misc_lock); 1890 } 1891 1892 /* 1893 * Register a "finalization" routine. Finalization routines are 1894 * called iteratively once all real devices have been found during 1895 * autoconfiguration, for as long as any one finalizer has done 1896 * any work. 1897 */ 1898 int 1899 config_finalize_register(device_t dev, int (*fn)(device_t)) 1900 { 1901 struct finalize_hook *f; 1902 1903 /* 1904 * If finalization has already been done, invoke the 1905 * callback function now. 1906 */ 1907 if (config_finalize_done) { 1908 while ((*fn)(dev) != 0) 1909 /* loop */ ; 1910 } 1911 1912 /* Ensure this isn't already on the list. */ 1913 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 1914 if (f->f_func == fn && f->f_dev == dev) 1915 return EEXIST; 1916 } 1917 1918 f = kmem_alloc(sizeof(*f), KM_SLEEP); 1919 f->f_func = fn; 1920 f->f_dev = dev; 1921 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 1922 1923 return 0; 1924 } 1925 1926 void 1927 config_finalize(void) 1928 { 1929 struct finalize_hook *f; 1930 struct pdevinit *pdev; 1931 extern struct pdevinit pdevinit[]; 1932 int errcnt, rv; 1933 1934 /* 1935 * Now that device driver threads have been created, wait for 1936 * them to finish any deferred autoconfiguration. 1937 */ 1938 mutex_enter(&config_misc_lock); 1939 while (config_pending != 0) 1940 cv_wait(&config_misc_cv, &config_misc_lock); 1941 mutex_exit(&config_misc_lock); 1942 1943 KERNEL_LOCK(1, NULL); 1944 1945 /* Attach pseudo-devices. */ 1946 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 1947 (*pdev->pdev_attach)(pdev->pdev_count); 1948 1949 /* Run the hooks until none of them does any work. */ 1950 do { 1951 rv = 0; 1952 TAILQ_FOREACH(f, &config_finalize_list, f_list) 1953 rv |= (*f->f_func)(f->f_dev); 1954 } while (rv != 0); 1955 1956 config_finalize_done = 1; 1957 1958 /* Now free all the hooks. */ 1959 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 1960 TAILQ_REMOVE(&config_finalize_list, f, f_list); 1961 kmem_free(f, sizeof(*f)); 1962 } 1963 1964 KERNEL_UNLOCK_ONE(NULL); 1965 1966 errcnt = aprint_get_error_count(); 1967 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 1968 (boothowto & AB_VERBOSE) == 0) { 1969 mutex_enter(&config_misc_lock); 1970 if (config_do_twiddle) { 1971 config_do_twiddle = 0; 1972 printf_nolog(" done.\n"); 1973 } 1974 mutex_exit(&config_misc_lock); 1975 if (errcnt != 0) { 1976 printf("WARNING: %d error%s while detecting hardware; " 1977 "check system log.\n", errcnt, 1978 errcnt == 1 ? "" : "s"); 1979 } 1980 } 1981 } 1982 1983 void 1984 config_twiddle_init() 1985 { 1986 1987 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 1988 config_do_twiddle = 1; 1989 } 1990 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL); 1991 } 1992 1993 void 1994 config_twiddle_fn(void *cookie) 1995 { 1996 1997 mutex_enter(&config_misc_lock); 1998 if (config_do_twiddle) { 1999 twiddle(); 2000 callout_schedule(&config_twiddle_ch, mstohz(100)); 2001 } 2002 mutex_exit(&config_misc_lock); 2003 } 2004 2005 static int 2006 config_alldevs_lock(void) 2007 { 2008 int s; 2009 2010 s = splhigh(); 2011 mutex_enter(&alldevs_mtx); 2012 return s; 2013 } 2014 2015 static void 2016 config_alldevs_enter(struct alldevs_foray *af) 2017 { 2018 TAILQ_INIT(&af->af_garbage); 2019 af->af_s = config_alldevs_lock(); 2020 config_collect_garbage(&af->af_garbage); 2021 } 2022 2023 static void 2024 config_alldevs_exit(struct alldevs_foray *af) 2025 { 2026 config_alldevs_unlock(af->af_s); 2027 config_dump_garbage(&af->af_garbage); 2028 } 2029 2030 static void 2031 config_alldevs_unlock(int s) 2032 { 2033 mutex_exit(&alldevs_mtx); 2034 splx(s); 2035 } 2036 2037 /* 2038 * device_lookup: 2039 * 2040 * Look up a device instance for a given driver. 2041 */ 2042 device_t 2043 device_lookup(cfdriver_t cd, int unit) 2044 { 2045 device_t dv; 2046 int s; 2047 2048 s = config_alldevs_lock(); 2049 KASSERT(mutex_owned(&alldevs_mtx)); 2050 if (unit < 0 || unit >= cd->cd_ndevs) 2051 dv = NULL; 2052 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0) 2053 dv = NULL; 2054 config_alldevs_unlock(s); 2055 2056 return dv; 2057 } 2058 2059 /* 2060 * device_lookup_private: 2061 * 2062 * Look up a softc instance for a given driver. 2063 */ 2064 void * 2065 device_lookup_private(cfdriver_t cd, int unit) 2066 { 2067 2068 return device_private(device_lookup(cd, unit)); 2069 } 2070 2071 /* 2072 * device_find_by_xname: 2073 * 2074 * Returns the device of the given name or NULL if it doesn't exist. 2075 */ 2076 device_t 2077 device_find_by_xname(const char *name) 2078 { 2079 device_t dv; 2080 deviter_t di; 2081 2082 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) { 2083 if (strcmp(device_xname(dv), name) == 0) 2084 break; 2085 } 2086 deviter_release(&di); 2087 2088 return dv; 2089 } 2090 2091 /* 2092 * device_find_by_driver_unit: 2093 * 2094 * Returns the device of the given driver name and unit or 2095 * NULL if it doesn't exist. 2096 */ 2097 device_t 2098 device_find_by_driver_unit(const char *name, int unit) 2099 { 2100 struct cfdriver *cd; 2101 2102 if ((cd = config_cfdriver_lookup(name)) == NULL) 2103 return NULL; 2104 return device_lookup(cd, unit); 2105 } 2106 2107 /* 2108 * Power management related functions. 2109 */ 2110 2111 bool 2112 device_pmf_is_registered(device_t dev) 2113 { 2114 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0; 2115 } 2116 2117 bool 2118 device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual) 2119 { 2120 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2121 return true; 2122 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2123 return false; 2124 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2125 dev->dv_driver_suspend != NULL && 2126 !(*dev->dv_driver_suspend)(dev, qual)) 2127 return false; 2128 2129 dev->dv_flags |= DVF_DRIVER_SUSPENDED; 2130 return true; 2131 } 2132 2133 bool 2134 device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual) 2135 { 2136 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 2137 return true; 2138 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 2139 return false; 2140 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2141 dev->dv_driver_resume != NULL && 2142 !(*dev->dv_driver_resume)(dev, qual)) 2143 return false; 2144 2145 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED; 2146 return true; 2147 } 2148 2149 bool 2150 device_pmf_driver_shutdown(device_t dev, int how) 2151 { 2152 2153 if (*dev->dv_driver_shutdown != NULL && 2154 !(*dev->dv_driver_shutdown)(dev, how)) 2155 return false; 2156 return true; 2157 } 2158 2159 bool 2160 device_pmf_driver_register(device_t dev, 2161 bool (*suspend)(device_t, const pmf_qual_t *), 2162 bool (*resume)(device_t, const pmf_qual_t *), 2163 bool (*shutdown)(device_t, int)) 2164 { 2165 dev->dv_driver_suspend = suspend; 2166 dev->dv_driver_resume = resume; 2167 dev->dv_driver_shutdown = shutdown; 2168 dev->dv_flags |= DVF_POWER_HANDLERS; 2169 return true; 2170 } 2171 2172 static const char * 2173 curlwp_name(void) 2174 { 2175 if (curlwp->l_name != NULL) 2176 return curlwp->l_name; 2177 else 2178 return curlwp->l_proc->p_comm; 2179 } 2180 2181 void 2182 device_pmf_driver_deregister(device_t dev) 2183 { 2184 device_lock_t dvl = device_getlock(dev); 2185 2186 dev->dv_driver_suspend = NULL; 2187 dev->dv_driver_resume = NULL; 2188 2189 mutex_enter(&dvl->dvl_mtx); 2190 dev->dv_flags &= ~DVF_POWER_HANDLERS; 2191 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) { 2192 /* Wake a thread that waits for the lock. That 2193 * thread will fail to acquire the lock, and then 2194 * it will wake the next thread that waits for the 2195 * lock, or else it will wake us. 2196 */ 2197 cv_signal(&dvl->dvl_cv); 2198 pmflock_debug(dev, __func__, __LINE__); 2199 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2200 pmflock_debug(dev, __func__, __LINE__); 2201 } 2202 mutex_exit(&dvl->dvl_mtx); 2203 } 2204 2205 bool 2206 device_pmf_driver_child_register(device_t dev) 2207 { 2208 device_t parent = device_parent(dev); 2209 2210 if (parent == NULL || parent->dv_driver_child_register == NULL) 2211 return true; 2212 return (*parent->dv_driver_child_register)(dev); 2213 } 2214 2215 void 2216 device_pmf_driver_set_child_register(device_t dev, 2217 bool (*child_register)(device_t)) 2218 { 2219 dev->dv_driver_child_register = child_register; 2220 } 2221 2222 static void 2223 pmflock_debug(device_t dev, const char *func, int line) 2224 { 2225 device_lock_t dvl = device_getlock(dev); 2226 2227 aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n", 2228 func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait, 2229 dev->dv_flags); 2230 } 2231 2232 static bool 2233 device_pmf_lock1(device_t dev) 2234 { 2235 device_lock_t dvl = device_getlock(dev); 2236 2237 while (device_pmf_is_registered(dev) && 2238 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) { 2239 dvl->dvl_nwait++; 2240 pmflock_debug(dev, __func__, __LINE__); 2241 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2242 pmflock_debug(dev, __func__, __LINE__); 2243 dvl->dvl_nwait--; 2244 } 2245 if (!device_pmf_is_registered(dev)) { 2246 pmflock_debug(dev, __func__, __LINE__); 2247 /* We could not acquire the lock, but some other thread may 2248 * wait for it, also. Wake that thread. 2249 */ 2250 cv_signal(&dvl->dvl_cv); 2251 return false; 2252 } 2253 dvl->dvl_nlock++; 2254 dvl->dvl_holder = curlwp; 2255 pmflock_debug(dev, __func__, __LINE__); 2256 return true; 2257 } 2258 2259 bool 2260 device_pmf_lock(device_t dev) 2261 { 2262 bool rc; 2263 device_lock_t dvl = device_getlock(dev); 2264 2265 mutex_enter(&dvl->dvl_mtx); 2266 rc = device_pmf_lock1(dev); 2267 mutex_exit(&dvl->dvl_mtx); 2268 2269 return rc; 2270 } 2271 2272 void 2273 device_pmf_unlock(device_t dev) 2274 { 2275 device_lock_t dvl = device_getlock(dev); 2276 2277 KASSERT(dvl->dvl_nlock > 0); 2278 mutex_enter(&dvl->dvl_mtx); 2279 if (--dvl->dvl_nlock == 0) 2280 dvl->dvl_holder = NULL; 2281 cv_signal(&dvl->dvl_cv); 2282 pmflock_debug(dev, __func__, __LINE__); 2283 mutex_exit(&dvl->dvl_mtx); 2284 } 2285 2286 device_lock_t 2287 device_getlock(device_t dev) 2288 { 2289 return &dev->dv_lock; 2290 } 2291 2292 void * 2293 device_pmf_bus_private(device_t dev) 2294 { 2295 return dev->dv_bus_private; 2296 } 2297 2298 bool 2299 device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual) 2300 { 2301 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 2302 return true; 2303 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 || 2304 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 2305 return false; 2306 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS && 2307 dev->dv_bus_suspend != NULL && 2308 !(*dev->dv_bus_suspend)(dev, qual)) 2309 return false; 2310 2311 dev->dv_flags |= DVF_BUS_SUSPENDED; 2312 return true; 2313 } 2314 2315 bool 2316 device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual) 2317 { 2318 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0) 2319 return true; 2320 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS && 2321 dev->dv_bus_resume != NULL && 2322 !(*dev->dv_bus_resume)(dev, qual)) 2323 return false; 2324 2325 dev->dv_flags &= ~DVF_BUS_SUSPENDED; 2326 return true; 2327 } 2328 2329 bool 2330 device_pmf_bus_shutdown(device_t dev, int how) 2331 { 2332 2333 if (*dev->dv_bus_shutdown != NULL && 2334 !(*dev->dv_bus_shutdown)(dev, how)) 2335 return false; 2336 return true; 2337 } 2338 2339 void 2340 device_pmf_bus_register(device_t dev, void *priv, 2341 bool (*suspend)(device_t, const pmf_qual_t *), 2342 bool (*resume)(device_t, const pmf_qual_t *), 2343 bool (*shutdown)(device_t, int), void (*deregister)(device_t)) 2344 { 2345 dev->dv_bus_private = priv; 2346 dev->dv_bus_resume = resume; 2347 dev->dv_bus_suspend = suspend; 2348 dev->dv_bus_shutdown = shutdown; 2349 dev->dv_bus_deregister = deregister; 2350 } 2351 2352 void 2353 device_pmf_bus_deregister(device_t dev) 2354 { 2355 if (dev->dv_bus_deregister == NULL) 2356 return; 2357 (*dev->dv_bus_deregister)(dev); 2358 dev->dv_bus_private = NULL; 2359 dev->dv_bus_suspend = NULL; 2360 dev->dv_bus_resume = NULL; 2361 dev->dv_bus_deregister = NULL; 2362 } 2363 2364 void * 2365 device_pmf_class_private(device_t dev) 2366 { 2367 return dev->dv_class_private; 2368 } 2369 2370 bool 2371 device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual) 2372 { 2373 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0) 2374 return true; 2375 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS && 2376 dev->dv_class_suspend != NULL && 2377 !(*dev->dv_class_suspend)(dev, qual)) 2378 return false; 2379 2380 dev->dv_flags |= DVF_CLASS_SUSPENDED; 2381 return true; 2382 } 2383 2384 bool 2385 device_pmf_class_resume(device_t dev, const pmf_qual_t *qual) 2386 { 2387 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2388 return true; 2389 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 || 2390 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2391 return false; 2392 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS && 2393 dev->dv_class_resume != NULL && 2394 !(*dev->dv_class_resume)(dev, qual)) 2395 return false; 2396 2397 dev->dv_flags &= ~DVF_CLASS_SUSPENDED; 2398 return true; 2399 } 2400 2401 void 2402 device_pmf_class_register(device_t dev, void *priv, 2403 bool (*suspend)(device_t, const pmf_qual_t *), 2404 bool (*resume)(device_t, const pmf_qual_t *), 2405 void (*deregister)(device_t)) 2406 { 2407 dev->dv_class_private = priv; 2408 dev->dv_class_suspend = suspend; 2409 dev->dv_class_resume = resume; 2410 dev->dv_class_deregister = deregister; 2411 } 2412 2413 void 2414 device_pmf_class_deregister(device_t dev) 2415 { 2416 if (dev->dv_class_deregister == NULL) 2417 return; 2418 (*dev->dv_class_deregister)(dev); 2419 dev->dv_class_private = NULL; 2420 dev->dv_class_suspend = NULL; 2421 dev->dv_class_resume = NULL; 2422 dev->dv_class_deregister = NULL; 2423 } 2424 2425 bool 2426 device_active(device_t dev, devactive_t type) 2427 { 2428 size_t i; 2429 2430 if (dev->dv_activity_count == 0) 2431 return false; 2432 2433 for (i = 0; i < dev->dv_activity_count; ++i) { 2434 if (dev->dv_activity_handlers[i] == NULL) 2435 break; 2436 (*dev->dv_activity_handlers[i])(dev, type); 2437 } 2438 2439 return true; 2440 } 2441 2442 bool 2443 device_active_register(device_t dev, void (*handler)(device_t, devactive_t)) 2444 { 2445 void (**new_handlers)(device_t, devactive_t); 2446 void (**old_handlers)(device_t, devactive_t); 2447 size_t i, old_size, new_size; 2448 int s; 2449 2450 old_handlers = dev->dv_activity_handlers; 2451 old_size = dev->dv_activity_count; 2452 2453 for (i = 0; i < old_size; ++i) { 2454 KASSERT(old_handlers[i] != handler); 2455 if (old_handlers[i] == NULL) { 2456 old_handlers[i] = handler; 2457 return true; 2458 } 2459 } 2460 2461 new_size = old_size + 4; 2462 new_handlers = kmem_alloc(sizeof(void *[new_size]), KM_SLEEP); 2463 2464 memcpy(new_handlers, old_handlers, sizeof(void *[old_size])); 2465 new_handlers[old_size] = handler; 2466 memset(new_handlers + old_size + 1, 0, 2467 sizeof(int [new_size - (old_size+1)])); 2468 2469 s = splhigh(); 2470 dev->dv_activity_count = new_size; 2471 dev->dv_activity_handlers = new_handlers; 2472 splx(s); 2473 2474 if (old_handlers != NULL) 2475 kmem_free(old_handlers, sizeof(void * [old_size])); 2476 2477 return true; 2478 } 2479 2480 void 2481 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t)) 2482 { 2483 void (**old_handlers)(device_t, devactive_t); 2484 size_t i, old_size; 2485 int s; 2486 2487 old_handlers = dev->dv_activity_handlers; 2488 old_size = dev->dv_activity_count; 2489 2490 for (i = 0; i < old_size; ++i) { 2491 if (old_handlers[i] == handler) 2492 break; 2493 if (old_handlers[i] == NULL) 2494 return; /* XXX panic? */ 2495 } 2496 2497 if (i == old_size) 2498 return; /* XXX panic? */ 2499 2500 for (; i < old_size - 1; ++i) { 2501 if ((old_handlers[i] = old_handlers[i + 1]) != NULL) 2502 continue; 2503 2504 if (i == 0) { 2505 s = splhigh(); 2506 dev->dv_activity_count = 0; 2507 dev->dv_activity_handlers = NULL; 2508 splx(s); 2509 kmem_free(old_handlers, sizeof(void *[old_size])); 2510 } 2511 return; 2512 } 2513 old_handlers[i] = NULL; 2514 } 2515 2516 /* Return true iff the device_t `dev' exists at generation `gen'. */ 2517 static bool 2518 device_exists_at(device_t dv, devgen_t gen) 2519 { 2520 return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) && 2521 dv->dv_add_gen <= gen; 2522 } 2523 2524 static bool 2525 deviter_visits(const deviter_t *di, device_t dv) 2526 { 2527 return device_exists_at(dv, di->di_gen); 2528 } 2529 2530 /* 2531 * Device Iteration 2532 * 2533 * deviter_t: a device iterator. Holds state for a "walk" visiting 2534 * each device_t's in the device tree. 2535 * 2536 * deviter_init(di, flags): initialize the device iterator `di' 2537 * to "walk" the device tree. deviter_next(di) will return 2538 * the first device_t in the device tree, or NULL if there are 2539 * no devices. 2540 * 2541 * `flags' is one or more of DEVITER_F_RW, indicating that the 2542 * caller intends to modify the device tree by calling 2543 * config_detach(9) on devices in the order that the iterator 2544 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices 2545 * nearest the "root" of the device tree to be returned, first; 2546 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from 2547 * the root of the device tree, first; and DEVITER_F_SHUTDOWN, 2548 * indicating both that deviter_init() should not respect any 2549 * locks on the device tree, and that deviter_next(di) may run 2550 * in more than one LWP before the walk has finished. 2551 * 2552 * Only one DEVITER_F_RW iterator may be in the device tree at 2553 * once. 2554 * 2555 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW. 2556 * 2557 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and 2558 * DEVITER_F_LEAVES_FIRST are used in combination. 2559 * 2560 * deviter_first(di, flags): initialize the device iterator `di' 2561 * and return the first device_t in the device tree, or NULL 2562 * if there are no devices. The statement 2563 * 2564 * dv = deviter_first(di); 2565 * 2566 * is shorthand for 2567 * 2568 * deviter_init(di); 2569 * dv = deviter_next(di); 2570 * 2571 * deviter_next(di): return the next device_t in the device tree, 2572 * or NULL if there are no more devices. deviter_next(di) 2573 * is undefined if `di' was not initialized with deviter_init() or 2574 * deviter_first(). 2575 * 2576 * deviter_release(di): stops iteration (subsequent calls to 2577 * deviter_next() will return NULL), releases any locks and 2578 * resources held by the device iterator. 2579 * 2580 * Device iteration does not return device_t's in any particular 2581 * order. An iterator will never return the same device_t twice. 2582 * Device iteration is guaranteed to complete---i.e., if deviter_next(di) 2583 * is called repeatedly on the same `di', it will eventually return 2584 * NULL. It is ok to attach/detach devices during device iteration. 2585 */ 2586 void 2587 deviter_init(deviter_t *di, deviter_flags_t flags) 2588 { 2589 device_t dv; 2590 int s; 2591 2592 memset(di, 0, sizeof(*di)); 2593 2594 s = config_alldevs_lock(); 2595 if ((flags & DEVITER_F_SHUTDOWN) != 0) 2596 flags |= DEVITER_F_RW; 2597 2598 if ((flags & DEVITER_F_RW) != 0) 2599 alldevs_nwrite++; 2600 else 2601 alldevs_nread++; 2602 di->di_gen = alldevs_gen++; 2603 config_alldevs_unlock(s); 2604 2605 di->di_flags = flags; 2606 2607 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2608 case DEVITER_F_LEAVES_FIRST: 2609 TAILQ_FOREACH(dv, &alldevs, dv_list) { 2610 if (!deviter_visits(di, dv)) 2611 continue; 2612 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth); 2613 } 2614 break; 2615 case DEVITER_F_ROOT_FIRST: 2616 TAILQ_FOREACH(dv, &alldevs, dv_list) { 2617 if (!deviter_visits(di, dv)) 2618 continue; 2619 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth); 2620 } 2621 break; 2622 default: 2623 break; 2624 } 2625 2626 deviter_reinit(di); 2627 } 2628 2629 static void 2630 deviter_reinit(deviter_t *di) 2631 { 2632 if ((di->di_flags & DEVITER_F_RW) != 0) 2633 di->di_prev = TAILQ_LAST(&alldevs, devicelist); 2634 else 2635 di->di_prev = TAILQ_FIRST(&alldevs); 2636 } 2637 2638 device_t 2639 deviter_first(deviter_t *di, deviter_flags_t flags) 2640 { 2641 deviter_init(di, flags); 2642 return deviter_next(di); 2643 } 2644 2645 static device_t 2646 deviter_next2(deviter_t *di) 2647 { 2648 device_t dv; 2649 2650 dv = di->di_prev; 2651 2652 if (dv == NULL) 2653 return NULL; 2654 2655 if ((di->di_flags & DEVITER_F_RW) != 0) 2656 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list); 2657 else 2658 di->di_prev = TAILQ_NEXT(dv, dv_list); 2659 2660 return dv; 2661 } 2662 2663 static device_t 2664 deviter_next1(deviter_t *di) 2665 { 2666 device_t dv; 2667 2668 do { 2669 dv = deviter_next2(di); 2670 } while (dv != NULL && !deviter_visits(di, dv)); 2671 2672 return dv; 2673 } 2674 2675 device_t 2676 deviter_next(deviter_t *di) 2677 { 2678 device_t dv = NULL; 2679 2680 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) { 2681 case 0: 2682 return deviter_next1(di); 2683 case DEVITER_F_LEAVES_FIRST: 2684 while (di->di_curdepth >= 0) { 2685 if ((dv = deviter_next1(di)) == NULL) { 2686 di->di_curdepth--; 2687 deviter_reinit(di); 2688 } else if (dv->dv_depth == di->di_curdepth) 2689 break; 2690 } 2691 return dv; 2692 case DEVITER_F_ROOT_FIRST: 2693 while (di->di_curdepth <= di->di_maxdepth) { 2694 if ((dv = deviter_next1(di)) == NULL) { 2695 di->di_curdepth++; 2696 deviter_reinit(di); 2697 } else if (dv->dv_depth == di->di_curdepth) 2698 break; 2699 } 2700 return dv; 2701 default: 2702 return NULL; 2703 } 2704 } 2705 2706 void 2707 deviter_release(deviter_t *di) 2708 { 2709 bool rw = (di->di_flags & DEVITER_F_RW) != 0; 2710 int s; 2711 2712 s = config_alldevs_lock(); 2713 if (rw) 2714 --alldevs_nwrite; 2715 else 2716 --alldevs_nread; 2717 /* XXX wake a garbage-collection thread */ 2718 config_alldevs_unlock(s); 2719 } 2720 2721 const char * 2722 cfdata_ifattr(const struct cfdata *cf) 2723 { 2724 return cf->cf_pspec->cfp_iattr; 2725 } 2726 2727 bool 2728 ifattr_match(const char *snull, const char *t) 2729 { 2730 return (snull == NULL) || strcmp(snull, t) == 0; 2731 } 2732 2733 void 2734 null_childdetached(device_t self, device_t child) 2735 { 2736 /* do nothing */ 2737 } 2738 2739 static void 2740 sysctl_detach_setup(struct sysctllog **clog) 2741 { 2742 const struct sysctlnode *node = NULL; 2743 2744 sysctl_createv(clog, 0, NULL, &node, 2745 CTLFLAG_PERMANENT, 2746 CTLTYPE_NODE, "kern", NULL, 2747 NULL, 0, NULL, 0, 2748 CTL_KERN, CTL_EOL); 2749 2750 if (node == NULL) 2751 return; 2752 2753 sysctl_createv(clog, 0, &node, NULL, 2754 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 2755 CTLTYPE_INT, "detachall", 2756 SYSCTL_DESCR("Detach all devices at shutdown"), 2757 NULL, 0, &detachall, 0, 2758 CTL_CREATE, CTL_EOL); 2759 } 2760