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