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