1 /* $NetBSD: subr_autoconf.c,v 1.119 2007/07/20 22:15:47 tsutsui 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.119 2007/07/20 22:15:47 tsutsui Exp $"); 81 82 #include "opt_ddb.h" 83 84 #include <sys/param.h> 85 #include <sys/device.h> 86 #include <sys/disklabel.h> 87 #include <sys/conf.h> 88 #include <sys/kauth.h> 89 #include <sys/malloc.h> 90 #include <sys/systm.h> 91 #include <sys/kernel.h> 92 #include <sys/errno.h> 93 #include <sys/proc.h> 94 #include <sys/reboot.h> 95 96 #include <sys/buf.h> 97 #include <sys/dirent.h> 98 #include <sys/lock.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 106 #include <sys/disk.h> 107 108 #include <machine/limits.h> 109 110 #include "opt_userconf.h" 111 #ifdef USERCONF 112 #include <sys/userconf.h> 113 #endif 114 115 #ifdef __i386__ 116 #include "opt_splash.h" 117 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 118 #include <dev/splash/splash.h> 119 extern struct splash_progress *splash_progress_state; 120 #endif 121 #endif 122 123 /* 124 * Autoconfiguration subroutines. 125 */ 126 127 /* 128 * ioconf.c exports exactly two names: cfdata and cfroots. All system 129 * devices and drivers are found via these tables. 130 */ 131 extern struct cfdata cfdata[]; 132 extern const short cfroots[]; 133 134 /* 135 * List of all cfdriver structures. We use this to detect duplicates 136 * when other cfdrivers are loaded. 137 */ 138 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 139 extern struct cfdriver * const cfdriver_list_initial[]; 140 141 /* 142 * Initial list of cfattach's. 143 */ 144 extern const struct cfattachinit cfattachinit[]; 145 146 /* 147 * List of cfdata tables. We always have one such list -- the one 148 * built statically when the kernel was configured. 149 */ 150 struct cftablelist allcftables; 151 static struct cftable initcftable; 152 153 #define ROOT ((device_t)NULL) 154 155 struct matchinfo { 156 cfsubmatch_t fn; 157 struct device *parent; 158 const int *locs; 159 void *aux; 160 struct cfdata *match; 161 int pri; 162 }; 163 164 static char *number(char *, int); 165 static void mapply(struct matchinfo *, cfdata_t); 166 static device_t config_devalloc(const device_t, const cfdata_t, const int *); 167 static void config_devdealloc(device_t); 168 static void config_makeroom(int, struct cfdriver *); 169 static void config_devlink(device_t); 170 static void config_devunlink(device_t); 171 172 struct deferred_config { 173 TAILQ_ENTRY(deferred_config) dc_queue; 174 device_t dc_dev; 175 void (*dc_func)(device_t); 176 }; 177 178 TAILQ_HEAD(deferred_config_head, deferred_config); 179 180 struct deferred_config_head deferred_config_queue; 181 struct deferred_config_head interrupt_config_queue; 182 183 static void config_process_deferred(struct deferred_config_head *, device_t); 184 185 /* Hooks to finalize configuration once all real devices have been found. */ 186 struct finalize_hook { 187 TAILQ_ENTRY(finalize_hook) f_list; 188 int (*f_func)(device_t); 189 device_t f_dev; 190 }; 191 static TAILQ_HEAD(, finalize_hook) config_finalize_list; 192 static int config_finalize_done; 193 194 /* list of all devices */ 195 struct devicelist alldevs; 196 197 volatile int config_pending; /* semaphore for mountroot */ 198 199 #define STREQ(s1, s2) \ 200 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 201 202 static int config_initialized; /* config_init() has been called. */ 203 204 static int config_do_twiddle; 205 206 struct vnode * 207 opendisk(struct device *dv) 208 { 209 int bmajor, bminor; 210 struct vnode *tmpvn; 211 int error; 212 dev_t dev; 213 214 /* 215 * Lookup major number for disk block device. 216 */ 217 bmajor = devsw_name2blk(device_xname(dv), NULL, 0); 218 if (bmajor == -1) 219 return NULL; 220 221 bminor = minor(device_unit(dv)); 222 /* 223 * Fake a temporary vnode for the disk, open it, and read 224 * and hash the sectors. 225 */ 226 dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) : 227 MAKEDISKDEV(bmajor, bminor, RAW_PART); 228 if (bdevvp(dev, &tmpvn)) 229 panic("%s: can't alloc vnode for %s", __func__, 230 device_xname(dv)); 231 error = VOP_OPEN(tmpvn, FREAD, NOCRED, 0); 232 if (error) { 233 #ifndef DEBUG 234 /* 235 * Ignore errors caused by missing device, partition, 236 * or medium. 237 */ 238 if (error != ENXIO && error != ENODEV) 239 #endif 240 printf("%s: can't open dev %s (%d)\n", 241 __func__, device_xname(dv), error); 242 vput(tmpvn); 243 return NULL; 244 } 245 246 return tmpvn; 247 } 248 249 int 250 config_handle_wedges(struct device *dv, int par) 251 { 252 struct dkwedge_list wl; 253 struct dkwedge_info *wi; 254 struct vnode *vn; 255 char diskname[16]; 256 int i, error; 257 258 if ((vn = opendisk(dv)) == NULL) 259 return -1; 260 261 wl.dkwl_bufsize = sizeof(*wi) * 16; 262 wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK); 263 264 error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED, 0); 265 VOP_CLOSE(vn, FREAD, NOCRED, 0); 266 vput(vn); 267 if (error) { 268 #ifdef DEBUG_WEDGE 269 printf("%s: List wedges returned %d\n", 270 device_xname(dv), error); 271 #endif 272 free(wi, M_TEMP); 273 return -1; 274 } 275 276 #ifdef DEBUG_WEDGE 277 printf("%s: Returned %u(%u) wedges\n", device_xname(dv), 278 wl.dkwl_nwedges, wl.dkwl_ncopied); 279 #endif 280 snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv), 281 par + 'a'); 282 283 for (i = 0; i < wl.dkwl_ncopied; i++) { 284 #ifdef DEBUG_WEDGE 285 printf("%s: Looking for %s in %s\n", 286 device_xname(dv), diskname, wi[i].dkw_wname); 287 #endif 288 if (strcmp(wi[i].dkw_wname, diskname) == 0) 289 break; 290 } 291 292 if (i == wl.dkwl_ncopied) { 293 #ifdef DEBUG_WEDGE 294 printf("%s: Cannot find wedge with parent %s\n", 295 device_xname(dv), diskname); 296 #endif 297 free(wi, M_TEMP); 298 return -1; 299 } 300 301 #ifdef DEBUG_WEDGE 302 printf("%s: Setting boot wedge %s (%s) at %llu %llu\n", 303 device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname, 304 (unsigned long long)wi[i].dkw_offset, 305 (unsigned long long)wi[i].dkw_size); 306 #endif 307 dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size); 308 free(wi, M_TEMP); 309 return 0; 310 } 311 312 /* 313 * Initialize the autoconfiguration data structures. Normally this 314 * is done by configure(), but some platforms need to do this very 315 * early (to e.g. initialize the console). 316 */ 317 void 318 config_init(void) 319 { 320 const struct cfattachinit *cfai; 321 int i, j; 322 323 if (config_initialized) 324 return; 325 326 /* allcfdrivers is statically initialized. */ 327 for (i = 0; cfdriver_list_initial[i] != NULL; i++) { 328 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0) 329 panic("configure: duplicate `%s' drivers", 330 cfdriver_list_initial[i]->cd_name); 331 } 332 333 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) { 334 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 335 if (config_cfattach_attach(cfai->cfai_name, 336 cfai->cfai_list[j]) != 0) 337 panic("configure: duplicate `%s' attachment " 338 "of `%s' driver", 339 cfai->cfai_list[j]->ca_name, 340 cfai->cfai_name); 341 } 342 } 343 344 TAILQ_INIT(&allcftables); 345 initcftable.ct_cfdata = cfdata; 346 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 347 348 TAILQ_INIT(&deferred_config_queue); 349 TAILQ_INIT(&interrupt_config_queue); 350 TAILQ_INIT(&config_finalize_list); 351 TAILQ_INIT(&alldevs); 352 353 config_initialized = 1; 354 } 355 356 /* 357 * Configure the system's hardware. 358 */ 359 void 360 configure(void) 361 { 362 int errcnt; 363 364 /* Initialize data structures. */ 365 config_init(); 366 367 #ifdef USERCONF 368 if (boothowto & RB_USERCONF) 369 user_config(); 370 #endif 371 372 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 373 config_do_twiddle = 1; 374 printf_nolog("Detecting hardware..."); 375 } 376 377 /* 378 * Do the machine-dependent portion of autoconfiguration. This 379 * sets the configuration machinery here in motion by "finding" 380 * the root bus. When this function returns, we expect interrupts 381 * to be enabled. 382 */ 383 cpu_configure(); 384 385 /* Initialize callouts, part 2. */ 386 callout_startup2(); 387 388 /* 389 * Now that we've found all the hardware, start the real time 390 * and statistics clocks. 391 */ 392 initclocks(); 393 394 cold = 0; /* clocks are running, we're warm now! */ 395 396 /* 397 * Now callback to finish configuration for devices which want 398 * to do this once interrupts are enabled. 399 */ 400 config_process_deferred(&interrupt_config_queue, NULL); 401 402 errcnt = aprint_get_error_count(); 403 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 404 (boothowto & AB_VERBOSE) == 0) { 405 if (config_do_twiddle) { 406 config_do_twiddle = 0; 407 printf_nolog("done.\n"); 408 } 409 if (errcnt != 0) { 410 printf("WARNING: %d error%s while detecting hardware; " 411 "check system log.\n", errcnt, 412 errcnt == 1 ? "" : "s"); 413 } 414 } 415 } 416 417 /* 418 * Add a cfdriver to the system. 419 */ 420 int 421 config_cfdriver_attach(struct cfdriver *cd) 422 { 423 struct cfdriver *lcd; 424 425 /* Make sure this driver isn't already in the system. */ 426 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 427 if (STREQ(lcd->cd_name, cd->cd_name)) 428 return (EEXIST); 429 } 430 431 LIST_INIT(&cd->cd_attach); 432 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 433 434 return (0); 435 } 436 437 /* 438 * Remove a cfdriver from the system. 439 */ 440 int 441 config_cfdriver_detach(struct cfdriver *cd) 442 { 443 int i; 444 445 /* Make sure there are no active instances. */ 446 for (i = 0; i < cd->cd_ndevs; i++) { 447 if (cd->cd_devs[i] != NULL) 448 return (EBUSY); 449 } 450 451 /* ...and no attachments loaded. */ 452 if (LIST_EMPTY(&cd->cd_attach) == 0) 453 return (EBUSY); 454 455 LIST_REMOVE(cd, cd_list); 456 457 KASSERT(cd->cd_devs == NULL); 458 459 return (0); 460 } 461 462 /* 463 * Look up a cfdriver by name. 464 */ 465 struct cfdriver * 466 config_cfdriver_lookup(const char *name) 467 { 468 struct cfdriver *cd; 469 470 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 471 if (STREQ(cd->cd_name, name)) 472 return (cd); 473 } 474 475 return (NULL); 476 } 477 478 /* 479 * Add a cfattach to the specified driver. 480 */ 481 int 482 config_cfattach_attach(const char *driver, struct cfattach *ca) 483 { 484 struct cfattach *lca; 485 struct cfdriver *cd; 486 487 cd = config_cfdriver_lookup(driver); 488 if (cd == NULL) 489 return (ESRCH); 490 491 /* Make sure this attachment isn't already on this driver. */ 492 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 493 if (STREQ(lca->ca_name, ca->ca_name)) 494 return (EEXIST); 495 } 496 497 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 498 499 return (0); 500 } 501 502 /* 503 * Remove a cfattach from the specified driver. 504 */ 505 int 506 config_cfattach_detach(const char *driver, struct cfattach *ca) 507 { 508 struct cfdriver *cd; 509 device_t dev; 510 int i; 511 512 cd = config_cfdriver_lookup(driver); 513 if (cd == NULL) 514 return (ESRCH); 515 516 /* Make sure there are no active instances. */ 517 for (i = 0; i < cd->cd_ndevs; i++) { 518 if ((dev = cd->cd_devs[i]) == NULL) 519 continue; 520 if (dev->dv_cfattach == ca) 521 return (EBUSY); 522 } 523 524 LIST_REMOVE(ca, ca_list); 525 526 return (0); 527 } 528 529 /* 530 * Look up a cfattach by name. 531 */ 532 static struct cfattach * 533 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 534 { 535 struct cfattach *ca; 536 537 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 538 if (STREQ(ca->ca_name, atname)) 539 return (ca); 540 } 541 542 return (NULL); 543 } 544 545 /* 546 * Look up a cfattach by driver/attachment name. 547 */ 548 struct cfattach * 549 config_cfattach_lookup(const char *name, const char *atname) 550 { 551 struct cfdriver *cd; 552 553 cd = config_cfdriver_lookup(name); 554 if (cd == NULL) 555 return (NULL); 556 557 return (config_cfattach_lookup_cd(cd, atname)); 558 } 559 560 /* 561 * Apply the matching function and choose the best. This is used 562 * a few times and we want to keep the code small. 563 */ 564 static void 565 mapply(struct matchinfo *m, cfdata_t cf) 566 { 567 int pri; 568 569 if (m->fn != NULL) { 570 pri = (*m->fn)(m->parent, cf, m->locs, m->aux); 571 } else { 572 pri = config_match(m->parent, cf, m->aux); 573 } 574 if (pri > m->pri) { 575 m->match = cf; 576 m->pri = pri; 577 } 578 } 579 580 int 581 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 582 { 583 const struct cfiattrdata *ci; 584 const struct cflocdesc *cl; 585 int nlocs, i; 586 587 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver); 588 KASSERT(ci); 589 nlocs = ci->ci_loclen; 590 for (i = 0; i < nlocs; i++) { 591 cl = &ci->ci_locdesc[i]; 592 /* !cld_defaultstr means no default value */ 593 if ((!(cl->cld_defaultstr) 594 || (cf->cf_loc[i] != cl->cld_default)) 595 && cf->cf_loc[i] != locs[i]) 596 return (0); 597 } 598 599 return (config_match(parent, cf, aux)); 600 } 601 602 /* 603 * Helper function: check whether the driver supports the interface attribute 604 * and return its descriptor structure. 605 */ 606 static const struct cfiattrdata * 607 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia) 608 { 609 const struct cfiattrdata * const *cpp; 610 611 if (cd->cd_attrs == NULL) 612 return (0); 613 614 for (cpp = cd->cd_attrs; *cpp; cpp++) { 615 if (STREQ((*cpp)->ci_name, ia)) { 616 /* Match. */ 617 return (*cpp); 618 } 619 } 620 return (0); 621 } 622 623 /* 624 * Lookup an interface attribute description by name. 625 * If the driver is given, consider only its supported attributes. 626 */ 627 const struct cfiattrdata * 628 cfiattr_lookup(const char *name, const struct cfdriver *cd) 629 { 630 const struct cfdriver *d; 631 const struct cfiattrdata *ia; 632 633 if (cd) 634 return (cfdriver_get_iattr(cd, name)); 635 636 LIST_FOREACH(d, &allcfdrivers, cd_list) { 637 ia = cfdriver_get_iattr(d, name); 638 if (ia) 639 return (ia); 640 } 641 return (0); 642 } 643 644 /* 645 * Determine if `parent' is a potential parent for a device spec based 646 * on `cfp'. 647 */ 648 static int 649 cfparent_match(const device_t parent, const struct cfparent *cfp) 650 { 651 struct cfdriver *pcd; 652 653 /* We don't match root nodes here. */ 654 if (cfp == NULL) 655 return (0); 656 657 pcd = parent->dv_cfdriver; 658 KASSERT(pcd != NULL); 659 660 /* 661 * First, ensure this parent has the correct interface 662 * attribute. 663 */ 664 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr)) 665 return (0); 666 667 /* 668 * If no specific parent device instance was specified (i.e. 669 * we're attaching to the attribute only), we're done! 670 */ 671 if (cfp->cfp_parent == NULL) 672 return (1); 673 674 /* 675 * Check the parent device's name. 676 */ 677 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 678 return (0); /* not the same parent */ 679 680 /* 681 * Make sure the unit number matches. 682 */ 683 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 684 cfp->cfp_unit == parent->dv_unit) 685 return (1); 686 687 /* Unit numbers don't match. */ 688 return (0); 689 } 690 691 /* 692 * Helper for config_cfdata_attach(): check all devices whether it could be 693 * parent any attachment in the config data table passed, and rescan. 694 */ 695 static void 696 rescan_with_cfdata(const struct cfdata *cf) 697 { 698 device_t d; 699 const struct cfdata *cf1; 700 701 /* 702 * "alldevs" is likely longer than an LKM's cfdata, so make it 703 * the outer loop. 704 */ 705 TAILQ_FOREACH(d, &alldevs, dv_list) { 706 707 if (!(d->dv_cfattach->ca_rescan)) 708 continue; 709 710 for (cf1 = cf; cf1->cf_name; cf1++) { 711 712 if (!cfparent_match(d, cf1->cf_pspec)) 713 continue; 714 715 (*d->dv_cfattach->ca_rescan)(d, 716 cf1->cf_pspec->cfp_iattr, cf1->cf_loc); 717 } 718 } 719 } 720 721 /* 722 * Attach a supplemental config data table and rescan potential 723 * parent devices if required. 724 */ 725 int 726 config_cfdata_attach(cfdata_t cf, int scannow) 727 { 728 struct cftable *ct; 729 730 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK); 731 ct->ct_cfdata = cf; 732 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list); 733 734 if (scannow) 735 rescan_with_cfdata(cf); 736 737 return (0); 738 } 739 740 /* 741 * Helper for config_cfdata_detach: check whether a device is 742 * found through any attachment in the config data table. 743 */ 744 static int 745 dev_in_cfdata(const struct device *d, const struct cfdata *cf) 746 { 747 const struct cfdata *cf1; 748 749 for (cf1 = cf; cf1->cf_name; cf1++) 750 if (d->dv_cfdata == cf1) 751 return (1); 752 753 return (0); 754 } 755 756 /* 757 * Detach a supplemental config data table. Detach all devices found 758 * through that table (and thus keeping references to it) before. 759 */ 760 int 761 config_cfdata_detach(cfdata_t cf) 762 { 763 device_t d; 764 int error; 765 struct cftable *ct; 766 767 again: 768 TAILQ_FOREACH(d, &alldevs, dv_list) { 769 if (dev_in_cfdata(d, cf)) { 770 error = config_detach(d, 0); 771 if (error) { 772 aprint_error("%s: unable to detach instance\n", 773 d->dv_xname); 774 return (error); 775 } 776 goto again; 777 } 778 } 779 780 TAILQ_FOREACH(ct, &allcftables, ct_list) { 781 if (ct->ct_cfdata == cf) { 782 TAILQ_REMOVE(&allcftables, ct, ct_list); 783 free(ct, M_DEVBUF); 784 return (0); 785 } 786 } 787 788 /* not found -- shouldn't happen */ 789 return (EINVAL); 790 } 791 792 /* 793 * Invoke the "match" routine for a cfdata entry on behalf of 794 * an external caller, usually a "submatch" routine. 795 */ 796 int 797 config_match(device_t parent, cfdata_t cf, void *aux) 798 { 799 struct cfattach *ca; 800 801 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 802 if (ca == NULL) { 803 /* No attachment for this entry, oh well. */ 804 return (0); 805 } 806 807 return ((*ca->ca_match)(parent, cf, aux)); 808 } 809 810 /* 811 * Iterate over all potential children of some device, calling the given 812 * function (default being the child's match function) for each one. 813 * Nonzero returns are matches; the highest value returned is considered 814 * the best match. Return the `found child' if we got a match, or NULL 815 * otherwise. The `aux' pointer is simply passed on through. 816 * 817 * Note that this function is designed so that it can be used to apply 818 * an arbitrary function to all potential children (its return value 819 * can be ignored). 820 */ 821 cfdata_t 822 config_search_loc(cfsubmatch_t fn, device_t parent, 823 const char *ifattr, const int *locs, void *aux) 824 { 825 struct cftable *ct; 826 cfdata_t cf; 827 struct matchinfo m; 828 829 KASSERT(config_initialized); 830 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr)); 831 832 m.fn = fn; 833 m.parent = parent; 834 m.locs = locs; 835 m.aux = aux; 836 m.match = NULL; 837 m.pri = 0; 838 839 TAILQ_FOREACH(ct, &allcftables, ct_list) { 840 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 841 842 /* We don't match root nodes here. */ 843 if (!cf->cf_pspec) 844 continue; 845 846 /* 847 * Skip cf if no longer eligible, otherwise scan 848 * through parents for one matching `parent', and 849 * try match function. 850 */ 851 if (cf->cf_fstate == FSTATE_FOUND) 852 continue; 853 if (cf->cf_fstate == FSTATE_DNOTFOUND || 854 cf->cf_fstate == FSTATE_DSTAR) 855 continue; 856 857 /* 858 * If an interface attribute was specified, 859 * consider only children which attach to 860 * that attribute. 861 */ 862 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr)) 863 continue; 864 865 if (cfparent_match(parent, cf->cf_pspec)) 866 mapply(&m, cf); 867 } 868 } 869 return (m.match); 870 } 871 872 cfdata_t 873 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr, 874 void *aux) 875 { 876 877 return (config_search_loc(fn, parent, ifattr, NULL, aux)); 878 } 879 880 /* 881 * Find the given root device. 882 * This is much like config_search, but there is no parent. 883 * Don't bother with multiple cfdata tables; the root node 884 * must always be in the initial table. 885 */ 886 cfdata_t 887 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux) 888 { 889 cfdata_t cf; 890 const short *p; 891 struct matchinfo m; 892 893 m.fn = fn; 894 m.parent = ROOT; 895 m.aux = aux; 896 m.match = NULL; 897 m.pri = 0; 898 m.locs = 0; 899 /* 900 * Look at root entries for matching name. We do not bother 901 * with found-state here since only one root should ever be 902 * searched (and it must be done first). 903 */ 904 for (p = cfroots; *p >= 0; p++) { 905 cf = &cfdata[*p]; 906 if (strcmp(cf->cf_name, rootname) == 0) 907 mapply(&m, cf); 908 } 909 return (m.match); 910 } 911 912 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 913 914 /* 915 * The given `aux' argument describes a device that has been found 916 * on the given parent, but not necessarily configured. Locate the 917 * configuration data for that device (using the submatch function 918 * provided, or using candidates' cd_match configuration driver 919 * functions) and attach it, and return true. If the device was 920 * not configured, call the given `print' function and return 0. 921 */ 922 device_t 923 config_found_sm_loc(device_t parent, 924 const char *ifattr, const int *locs, void *aux, 925 cfprint_t print, cfsubmatch_t submatch) 926 { 927 cfdata_t cf; 928 929 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 930 if (splash_progress_state) 931 splash_progress_update(splash_progress_state); 932 #endif 933 934 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux))) 935 return(config_attach_loc(parent, cf, locs, aux, print)); 936 if (print) { 937 if (config_do_twiddle) 938 twiddle(); 939 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]); 940 } 941 942 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 943 if (splash_progress_state) 944 splash_progress_update(splash_progress_state); 945 #endif 946 947 return (NULL); 948 } 949 950 device_t 951 config_found_ia(device_t parent, const char *ifattr, void *aux, 952 cfprint_t print) 953 { 954 955 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL)); 956 } 957 958 device_t 959 config_found(device_t parent, void *aux, cfprint_t print) 960 { 961 962 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL)); 963 } 964 965 /* 966 * As above, but for root devices. 967 */ 968 device_t 969 config_rootfound(const char *rootname, void *aux) 970 { 971 cfdata_t cf; 972 973 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL) 974 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 975 aprint_error("root device %s not configured\n", rootname); 976 return (NULL); 977 } 978 979 /* just like sprintf(buf, "%d") except that it works from the end */ 980 static char * 981 number(char *ep, int n) 982 { 983 984 *--ep = 0; 985 while (n >= 10) { 986 *--ep = (n % 10) + '0'; 987 n /= 10; 988 } 989 *--ep = n + '0'; 990 return (ep); 991 } 992 993 /* 994 * Expand the size of the cd_devs array if necessary. 995 */ 996 static void 997 config_makeroom(int n, struct cfdriver *cd) 998 { 999 int old, new; 1000 void **nsp; 1001 1002 if (n < cd->cd_ndevs) 1003 return; 1004 1005 /* 1006 * Need to expand the array. 1007 */ 1008 old = cd->cd_ndevs; 1009 if (old == 0) 1010 new = 4; 1011 else 1012 new = old * 2; 1013 while (new <= n) 1014 new *= 2; 1015 cd->cd_ndevs = new; 1016 nsp = malloc(new * sizeof(void *), M_DEVBUF, 1017 cold ? M_NOWAIT : M_WAITOK); 1018 if (nsp == NULL) 1019 panic("config_attach: %sing dev array", 1020 old != 0 ? "expand" : "creat"); 1021 memset(nsp + old, 0, (new - old) * sizeof(void *)); 1022 if (old != 0) { 1023 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 1024 free(cd->cd_devs, M_DEVBUF); 1025 } 1026 cd->cd_devs = nsp; 1027 } 1028 1029 static void 1030 config_devlink(device_t dev) 1031 { 1032 struct cfdriver *cd = dev->dv_cfdriver; 1033 1034 /* put this device in the devices array */ 1035 config_makeroom(dev->dv_unit, cd); 1036 if (cd->cd_devs[dev->dv_unit]) 1037 panic("config_attach: duplicate %s", dev->dv_xname); 1038 cd->cd_devs[dev->dv_unit] = dev; 1039 1040 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 1041 } 1042 1043 static void 1044 config_devunlink(device_t dev) 1045 { 1046 struct cfdriver *cd = dev->dv_cfdriver; 1047 int i; 1048 1049 /* Unlink from device list. */ 1050 TAILQ_REMOVE(&alldevs, dev, dv_list); 1051 1052 /* Remove from cfdriver's array. */ 1053 cd->cd_devs[dev->dv_unit] = NULL; 1054 1055 /* 1056 * If the device now has no units in use, deallocate its softc array. 1057 */ 1058 for (i = 0; i < cd->cd_ndevs; i++) 1059 if (cd->cd_devs[i] != NULL) 1060 break; 1061 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 1062 free(cd->cd_devs, M_DEVBUF); 1063 cd->cd_devs = NULL; 1064 cd->cd_ndevs = 0; 1065 } 1066 } 1067 1068 static device_t 1069 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs) 1070 { 1071 struct cfdriver *cd; 1072 struct cfattach *ca; 1073 size_t lname, lunit; 1074 const char *xunit; 1075 int myunit; 1076 char num[10]; 1077 device_t dev; 1078 const struct cfiattrdata *ia; 1079 1080 cd = config_cfdriver_lookup(cf->cf_name); 1081 if (cd == NULL) 1082 return (NULL); 1083 1084 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1085 if (ca == NULL) 1086 return (NULL); 1087 1088 if (ca->ca_devsize < sizeof(struct device)) 1089 panic("config_devalloc"); 1090 1091 #ifndef __BROKEN_CONFIG_UNIT_USAGE 1092 if (cf->cf_fstate == FSTATE_STAR) { 1093 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 1094 if (cd->cd_devs[myunit] == NULL) 1095 break; 1096 /* 1097 * myunit is now the unit of the first NULL device pointer, 1098 * or max(cd->cd_ndevs,cf->cf_unit). 1099 */ 1100 } else { 1101 myunit = cf->cf_unit; 1102 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL) 1103 return (NULL); 1104 } 1105 #else 1106 myunit = cf->cf_unit; 1107 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */ 1108 1109 /* compute length of name and decimal expansion of unit number */ 1110 lname = strlen(cd->cd_name); 1111 xunit = number(&num[sizeof(num)], myunit); 1112 lunit = &num[sizeof(num)] - xunit; 1113 if (lname + lunit > sizeof(dev->dv_xname)) 1114 panic("config_devalloc: device name too long"); 1115 1116 /* get memory for all device vars */ 1117 dev = (device_t)malloc(ca->ca_devsize, M_DEVBUF, 1118 M_ZERO | (cold ? M_NOWAIT : M_WAITOK)); 1119 if (!dev) 1120 panic("config_devalloc: memory allocation for device softc failed"); 1121 dev->dv_class = cd->cd_class; 1122 dev->dv_cfdata = cf; 1123 dev->dv_cfdriver = cd; 1124 dev->dv_cfattach = ca; 1125 dev->dv_unit = myunit; 1126 memcpy(dev->dv_xname, cd->cd_name, lname); 1127 memcpy(dev->dv_xname + lname, xunit, lunit); 1128 dev->dv_parent = parent; 1129 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 1130 if (locs) { 1131 KASSERT(parent); /* no locators at root */ 1132 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr, 1133 parent->dv_cfdriver); 1134 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int), 1135 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1136 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int)); 1137 } 1138 dev->dv_properties = prop_dictionary_create(); 1139 KASSERT(dev->dv_properties != NULL); 1140 1141 return (dev); 1142 } 1143 1144 static void 1145 config_devdealloc(device_t dev) 1146 { 1147 1148 KASSERT(dev->dv_properties != NULL); 1149 prop_object_release(dev->dv_properties); 1150 1151 if (dev->dv_locators) 1152 free(dev->dv_locators, M_DEVBUF); 1153 1154 free(dev, M_DEVBUF); 1155 } 1156 1157 /* 1158 * Attach a found device. 1159 */ 1160 device_t 1161 config_attach_loc(device_t parent, cfdata_t cf, 1162 const int *locs, void *aux, cfprint_t print) 1163 { 1164 device_t dev; 1165 struct cftable *ct; 1166 const char *drvname; 1167 1168 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1169 if (splash_progress_state) 1170 splash_progress_update(splash_progress_state); 1171 #endif 1172 1173 dev = config_devalloc(parent, cf, locs); 1174 if (!dev) 1175 panic("config_attach: allocation of device softc failed"); 1176 1177 /* XXX redundant - see below? */ 1178 if (cf->cf_fstate != FSTATE_STAR) { 1179 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1180 cf->cf_fstate = FSTATE_FOUND; 1181 } 1182 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1183 else 1184 cf->cf_unit++; 1185 #endif 1186 1187 config_devlink(dev); 1188 1189 if (config_do_twiddle) 1190 twiddle(); 1191 else 1192 aprint_naive("Found "); 1193 /* 1194 * We want the next two printfs for normal, verbose, and quiet, 1195 * but not silent (in which case, we're twiddling, instead). 1196 */ 1197 if (parent == ROOT) { 1198 aprint_naive("%s (root)", dev->dv_xname); 1199 aprint_normal("%s (root)", dev->dv_xname); 1200 } else { 1201 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname); 1202 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname); 1203 if (print) 1204 (void) (*print)(aux, NULL); 1205 } 1206 1207 /* 1208 * Before attaching, clobber any unfound devices that are 1209 * otherwise identical. 1210 * XXX code above is redundant? 1211 */ 1212 drvname = dev->dv_cfdriver->cd_name; 1213 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1214 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1215 if (STREQ(cf->cf_name, drvname) && 1216 cf->cf_unit == dev->dv_unit) { 1217 if (cf->cf_fstate == FSTATE_NOTFOUND) 1218 cf->cf_fstate = FSTATE_FOUND; 1219 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1220 /* 1221 * Bump the unit number on all starred cfdata 1222 * entries for this device. 1223 */ 1224 if (cf->cf_fstate == FSTATE_STAR) 1225 cf->cf_unit++; 1226 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1227 } 1228 } 1229 } 1230 #ifdef __HAVE_DEVICE_REGISTER 1231 device_register(dev, aux); 1232 #endif 1233 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1234 if (splash_progress_state) 1235 splash_progress_update(splash_progress_state); 1236 #endif 1237 (*dev->dv_cfattach->ca_attach)(parent, dev, aux); 1238 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1239 if (splash_progress_state) 1240 splash_progress_update(splash_progress_state); 1241 #endif 1242 config_process_deferred(&deferred_config_queue, dev); 1243 return (dev); 1244 } 1245 1246 device_t 1247 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1248 { 1249 1250 return (config_attach_loc(parent, cf, NULL, aux, print)); 1251 } 1252 1253 /* 1254 * As above, but for pseudo-devices. Pseudo-devices attached in this 1255 * way are silently inserted into the device tree, and their children 1256 * attached. 1257 * 1258 * Note that because pseudo-devices are attached silently, any information 1259 * the attach routine wishes to print should be prefixed with the device 1260 * name by the attach routine. 1261 */ 1262 device_t 1263 config_attach_pseudo(cfdata_t cf) 1264 { 1265 device_t dev; 1266 1267 dev = config_devalloc(ROOT, cf, NULL); 1268 if (!dev) 1269 return (NULL); 1270 1271 /* XXX mark busy in cfdata */ 1272 1273 config_devlink(dev); 1274 1275 #if 0 /* XXXJRT not yet */ 1276 #ifdef __HAVE_DEVICE_REGISTER 1277 device_register(dev, NULL); /* like a root node */ 1278 #endif 1279 #endif 1280 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL); 1281 config_process_deferred(&deferred_config_queue, dev); 1282 return (dev); 1283 } 1284 1285 /* 1286 * Detach a device. Optionally forced (e.g. because of hardware 1287 * removal) and quiet. Returns zero if successful, non-zero 1288 * (an error code) otherwise. 1289 * 1290 * Note that this code wants to be run from a process context, so 1291 * that the detach can sleep to allow processes which have a device 1292 * open to run and unwind their stacks. 1293 */ 1294 int 1295 config_detach(device_t dev, int flags) 1296 { 1297 struct cftable *ct; 1298 cfdata_t cf; 1299 const struct cfattach *ca; 1300 struct cfdriver *cd; 1301 #ifdef DIAGNOSTIC 1302 device_t d; 1303 #endif 1304 int rv = 0; 1305 1306 #ifdef DIAGNOSTIC 1307 if (dev->dv_cfdata != NULL && 1308 dev->dv_cfdata->cf_fstate != FSTATE_FOUND && 1309 dev->dv_cfdata->cf_fstate != FSTATE_STAR) 1310 panic("config_detach: bad device fstate"); 1311 #endif 1312 cd = dev->dv_cfdriver; 1313 KASSERT(cd != NULL); 1314 1315 ca = dev->dv_cfattach; 1316 KASSERT(ca != NULL); 1317 1318 /* 1319 * Ensure the device is deactivated. If the device doesn't 1320 * have an activation entry point, we allow DVF_ACTIVE to 1321 * remain set. Otherwise, if DVF_ACTIVE is still set, the 1322 * device is busy, and the detach fails. 1323 */ 1324 if (ca->ca_activate != NULL) 1325 rv = config_deactivate(dev); 1326 1327 /* 1328 * Try to detach the device. If that's not possible, then 1329 * we either panic() (for the forced but failed case), or 1330 * return an error. 1331 */ 1332 if (rv == 0) { 1333 if (ca->ca_detach != NULL) 1334 rv = (*ca->ca_detach)(dev, flags); 1335 else 1336 rv = EOPNOTSUPP; 1337 } 1338 if (rv != 0) { 1339 if ((flags & DETACH_FORCE) == 0) 1340 return (rv); 1341 else 1342 panic("config_detach: forced detach of %s failed (%d)", 1343 dev->dv_xname, rv); 1344 } 1345 1346 /* 1347 * The device has now been successfully detached. 1348 */ 1349 1350 #ifdef DIAGNOSTIC 1351 /* 1352 * Sanity: If you're successfully detached, you should have no 1353 * children. (Note that because children must be attached 1354 * after parents, we only need to search the latter part of 1355 * the list.) 1356 */ 1357 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1358 d = TAILQ_NEXT(d, dv_list)) { 1359 if (d->dv_parent == dev) { 1360 printf("config_detach: detached device %s" 1361 " has children %s\n", dev->dv_xname, d->dv_xname); 1362 panic("config_detach"); 1363 } 1364 } 1365 #endif 1366 1367 /* notify the parent that the child is gone */ 1368 if (dev->dv_parent) { 1369 device_t p = dev->dv_parent; 1370 if (p->dv_cfattach->ca_childdetached) 1371 (*p->dv_cfattach->ca_childdetached)(p, dev); 1372 } 1373 1374 /* 1375 * Mark cfdata to show that the unit can be reused, if possible. 1376 */ 1377 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1378 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1379 if (STREQ(cf->cf_name, cd->cd_name)) { 1380 if (cf->cf_fstate == FSTATE_FOUND && 1381 cf->cf_unit == dev->dv_unit) 1382 cf->cf_fstate = FSTATE_NOTFOUND; 1383 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1384 /* 1385 * Note that we can only re-use a starred 1386 * unit number if the unit being detached 1387 * had the last assigned unit number. 1388 */ 1389 if (cf->cf_fstate == FSTATE_STAR && 1390 cf->cf_unit == dev->dv_unit + 1) 1391 cf->cf_unit--; 1392 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1393 } 1394 } 1395 } 1396 1397 config_devunlink(dev); 1398 1399 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1400 aprint_normal("%s detached\n", dev->dv_xname); 1401 1402 config_devdealloc(dev); 1403 1404 return (0); 1405 } 1406 1407 int 1408 config_activate(device_t dev) 1409 { 1410 const struct cfattach *ca = dev->dv_cfattach; 1411 int rv = 0, oflags = dev->dv_flags; 1412 1413 if (ca->ca_activate == NULL) 1414 return (EOPNOTSUPP); 1415 1416 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 1417 dev->dv_flags |= DVF_ACTIVE; 1418 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 1419 if (rv) 1420 dev->dv_flags = oflags; 1421 } 1422 return (rv); 1423 } 1424 1425 int 1426 config_deactivate(device_t dev) 1427 { 1428 const struct cfattach *ca = dev->dv_cfattach; 1429 int rv = 0, oflags = dev->dv_flags; 1430 1431 if (ca->ca_activate == NULL) 1432 return (EOPNOTSUPP); 1433 1434 if (dev->dv_flags & DVF_ACTIVE) { 1435 dev->dv_flags &= ~DVF_ACTIVE; 1436 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 1437 if (rv) 1438 dev->dv_flags = oflags; 1439 } 1440 return (rv); 1441 } 1442 1443 /* 1444 * Defer the configuration of the specified device until all 1445 * of its parent's devices have been attached. 1446 */ 1447 void 1448 config_defer(device_t dev, void (*func)(device_t)) 1449 { 1450 struct deferred_config *dc; 1451 1452 if (dev->dv_parent == NULL) 1453 panic("config_defer: can't defer config of a root device"); 1454 1455 #ifdef DIAGNOSTIC 1456 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 1457 dc = TAILQ_NEXT(dc, dc_queue)) { 1458 if (dc->dc_dev == dev) 1459 panic("config_defer: deferred twice"); 1460 } 1461 #endif 1462 1463 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1464 if (dc == NULL) 1465 panic("config_defer: unable to allocate callback"); 1466 1467 dc->dc_dev = dev; 1468 dc->dc_func = func; 1469 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1470 config_pending_incr(); 1471 } 1472 1473 /* 1474 * Defer some autoconfiguration for a device until after interrupts 1475 * are enabled. 1476 */ 1477 void 1478 config_interrupts(device_t dev, void (*func)(device_t)) 1479 { 1480 struct deferred_config *dc; 1481 1482 /* 1483 * If interrupts are enabled, callback now. 1484 */ 1485 if (cold == 0) { 1486 (*func)(dev); 1487 return; 1488 } 1489 1490 #ifdef DIAGNOSTIC 1491 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 1492 dc = TAILQ_NEXT(dc, dc_queue)) { 1493 if (dc->dc_dev == dev) 1494 panic("config_interrupts: deferred twice"); 1495 } 1496 #endif 1497 1498 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1499 if (dc == NULL) 1500 panic("config_interrupts: unable to allocate callback"); 1501 1502 dc->dc_dev = dev; 1503 dc->dc_func = func; 1504 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1505 config_pending_incr(); 1506 } 1507 1508 /* 1509 * Process a deferred configuration queue. 1510 */ 1511 static void 1512 config_process_deferred(struct deferred_config_head *queue, 1513 device_t parent) 1514 { 1515 struct deferred_config *dc, *ndc; 1516 1517 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 1518 ndc = TAILQ_NEXT(dc, dc_queue); 1519 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 1520 TAILQ_REMOVE(queue, dc, dc_queue); 1521 (*dc->dc_func)(dc->dc_dev); 1522 free(dc, M_DEVBUF); 1523 config_pending_decr(); 1524 } 1525 } 1526 } 1527 1528 /* 1529 * Manipulate the config_pending semaphore. 1530 */ 1531 void 1532 config_pending_incr(void) 1533 { 1534 1535 config_pending++; 1536 } 1537 1538 void 1539 config_pending_decr(void) 1540 { 1541 1542 #ifdef DIAGNOSTIC 1543 if (config_pending == 0) 1544 panic("config_pending_decr: config_pending == 0"); 1545 #endif 1546 config_pending--; 1547 if (config_pending == 0) 1548 wakeup(&config_pending); 1549 } 1550 1551 /* 1552 * Register a "finalization" routine. Finalization routines are 1553 * called iteratively once all real devices have been found during 1554 * autoconfiguration, for as long as any one finalizer has done 1555 * any work. 1556 */ 1557 int 1558 config_finalize_register(device_t dev, int (*fn)(device_t)) 1559 { 1560 struct finalize_hook *f; 1561 1562 /* 1563 * If finalization has already been done, invoke the 1564 * callback function now. 1565 */ 1566 if (config_finalize_done) { 1567 while ((*fn)(dev) != 0) 1568 /* loop */ ; 1569 } 1570 1571 /* Ensure this isn't already on the list. */ 1572 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 1573 if (f->f_func == fn && f->f_dev == dev) 1574 return (EEXIST); 1575 } 1576 1577 f = malloc(sizeof(*f), M_TEMP, M_WAITOK); 1578 f->f_func = fn; 1579 f->f_dev = dev; 1580 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 1581 1582 return (0); 1583 } 1584 1585 void 1586 config_finalize(void) 1587 { 1588 struct finalize_hook *f; 1589 int rv; 1590 1591 /* Run the hooks until none of them does any work. */ 1592 do { 1593 rv = 0; 1594 TAILQ_FOREACH(f, &config_finalize_list, f_list) 1595 rv |= (*f->f_func)(f->f_dev); 1596 } while (rv != 0); 1597 1598 config_finalize_done = 1; 1599 1600 /* Now free all the hooks. */ 1601 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 1602 TAILQ_REMOVE(&config_finalize_list, f, f_list); 1603 free(f, M_TEMP); 1604 } 1605 } 1606 1607 /* 1608 * device_lookup: 1609 * 1610 * Look up a device instance for a given driver. 1611 */ 1612 void * 1613 device_lookup(cfdriver_t cd, int unit) 1614 { 1615 1616 if (unit < 0 || unit >= cd->cd_ndevs) 1617 return (NULL); 1618 1619 return (cd->cd_devs[unit]); 1620 } 1621 1622 /* 1623 * Accessor functions for the device_t type. 1624 */ 1625 devclass_t 1626 device_class(device_t dev) 1627 { 1628 1629 return (dev->dv_class); 1630 } 1631 1632 cfdata_t 1633 device_cfdata(device_t dev) 1634 { 1635 1636 return (dev->dv_cfdata); 1637 } 1638 1639 cfdriver_t 1640 device_cfdriver(device_t dev) 1641 { 1642 1643 return (dev->dv_cfdriver); 1644 } 1645 1646 cfattach_t 1647 device_cfattach(device_t dev) 1648 { 1649 1650 return (dev->dv_cfattach); 1651 } 1652 1653 int 1654 device_unit(device_t dev) 1655 { 1656 1657 return (dev->dv_unit); 1658 } 1659 1660 const char * 1661 device_xname(device_t dev) 1662 { 1663 1664 return (dev->dv_xname); 1665 } 1666 1667 device_t 1668 device_parent(device_t dev) 1669 { 1670 1671 return (dev->dv_parent); 1672 } 1673 1674 bool 1675 device_is_active(device_t dev) 1676 { 1677 1678 return ((dev->dv_flags & DVF_ACTIVE) != 0); 1679 } 1680 1681 int 1682 device_locator(device_t dev, u_int locnum) 1683 { 1684 1685 KASSERT(dev->dv_locators != NULL); 1686 return (dev->dv_locators[locnum]); 1687 } 1688 1689 void * 1690 device_private(device_t dev) 1691 { 1692 1693 /* 1694 * For now, at least, "struct device" is the first thing in 1695 * the driver's private data. So, we just return ourselves. 1696 */ 1697 return (dev); 1698 } 1699 1700 prop_dictionary_t 1701 device_properties(device_t dev) 1702 { 1703 1704 return (dev->dv_properties); 1705 } 1706 1707 /* 1708 * device_is_a: 1709 * 1710 * Returns true if the device is an instance of the specified 1711 * driver. 1712 */ 1713 bool 1714 device_is_a(device_t dev, const char *dname) 1715 { 1716 1717 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0); 1718 } 1719