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