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