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