1 /* $NetBSD: subr_autoconf.c,v 1.85 2003/04/29 00:56:52 thorpej 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. All advertising materials mentioning features or use of this software 59 * must display the following acknowledgement: 60 * This product includes software developed by the University of 61 * California, Berkeley and its contributors. 62 * 4. Neither the name of the University nor the names of its contributors 63 * may be used to endorse or promote products derived from this software 64 * without specific prior written permission. 65 * 66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 76 * SUCH DAMAGE. 77 * 78 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL) 79 * 80 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94 81 */ 82 83 #include <sys/cdefs.h> 84 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.85 2003/04/29 00:56:52 thorpej Exp $"); 85 86 #include "opt_ddb.h" 87 88 #include <sys/param.h> 89 #include <sys/device.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 <machine/limits.h> 97 98 #include "opt_userconf.h" 99 #ifdef USERCONF 100 #include <sys/userconf.h> 101 #endif 102 103 /* 104 * Autoconfiguration subroutines. 105 */ 106 107 /* 108 * ioconf.c exports exactly two names: cfdata and cfroots. All system 109 * devices and drivers are found via these tables. 110 */ 111 extern struct cfdata cfdata[]; 112 extern const short cfroots[]; 113 114 /* 115 * List of all cfdriver structures. We use this to detect duplicates 116 * when other cfdrivers are loaded. 117 */ 118 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 119 extern struct cfdriver * const cfdriver_list_initial[]; 120 121 /* 122 * Initial list of cfattach's. 123 */ 124 extern const struct cfattachinit cfattachinit[]; 125 126 /* 127 * List of cfdata tables. We always have one such list -- the one 128 * built statically when the kernel was configured. 129 */ 130 struct cftablelist allcftables; 131 static struct cftable initcftable; 132 133 #define ROOT ((struct device *)NULL) 134 135 struct matchinfo { 136 cfmatch_t fn; 137 struct device *parent; 138 void *aux; 139 struct cfdata *match; 140 int pri; 141 }; 142 143 static char *number(char *, int); 144 static void mapply(struct matchinfo *, struct cfdata *); 145 146 struct deferred_config { 147 TAILQ_ENTRY(deferred_config) dc_queue; 148 struct device *dc_dev; 149 void (*dc_func)(struct device *); 150 }; 151 152 TAILQ_HEAD(deferred_config_head, deferred_config); 153 154 struct deferred_config_head deferred_config_queue; 155 struct deferred_config_head interrupt_config_queue; 156 157 static void config_process_deferred(struct deferred_config_head *, 158 struct device *); 159 160 /* Hooks to finalize configuration once all real devices have been found. */ 161 struct finalize_hook { 162 TAILQ_ENTRY(finalize_hook) f_list; 163 int (*f_func)(struct device *); 164 struct device *f_dev; 165 }; 166 static TAILQ_HEAD(, finalize_hook) config_finalize_list; 167 static int config_finalize_done; 168 169 /* list of all devices */ 170 struct devicelist alldevs; 171 172 /* list of all events */ 173 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents); 174 175 __volatile int config_pending; /* semaphore for mountroot */ 176 177 #define STREQ(s1, s2) \ 178 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 179 180 static int config_initialized; /* config_init() has been called. */ 181 182 static int config_do_twiddle; 183 184 /* 185 * Initialize the autoconfiguration data structures. Normally this 186 * is done by configure(), but some platforms need to do this very 187 * early (to e.g. initialize the console). 188 */ 189 void 190 config_init(void) 191 { 192 const struct cfattachinit *cfai; 193 int i, j; 194 195 if (config_initialized) 196 return; 197 198 /* allcfdrivers is statically initialized. */ 199 for (i = 0; cfdriver_list_initial[i] != NULL; i++) { 200 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0) 201 panic("configure: duplicate `%s' drivers", 202 cfdriver_list_initial[i]->cd_name); 203 } 204 205 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) { 206 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 207 if (config_cfattach_attach(cfai->cfai_name, 208 cfai->cfai_list[j]) != 0) 209 panic("configure: duplicate `%s' attachment " 210 "of `%s' driver", 211 cfai->cfai_list[j]->ca_name, 212 cfai->cfai_name); 213 } 214 } 215 216 TAILQ_INIT(&allcftables); 217 initcftable.ct_cfdata = cfdata; 218 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 219 220 TAILQ_INIT(&deferred_config_queue); 221 TAILQ_INIT(&interrupt_config_queue); 222 TAILQ_INIT(&config_finalize_list); 223 TAILQ_INIT(&alldevs); 224 225 config_initialized = 1; 226 } 227 228 /* 229 * Configure the system's hardware. 230 */ 231 void 232 configure(void) 233 { 234 int errcnt; 235 236 /* Initialize data structures. */ 237 config_init(); 238 239 #ifdef USERCONF 240 if (boothowto & RB_USERCONF) 241 user_config(); 242 #endif 243 244 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 245 config_do_twiddle = 1; 246 printf_nolog("Detecting hardware..."); 247 } 248 249 /* 250 * Do the machine-dependent portion of autoconfiguration. This 251 * sets the configuration machinery here in motion by "finding" 252 * the root bus. When this function returns, we expect interrupts 253 * to be enabled. 254 */ 255 cpu_configure(); 256 257 /* 258 * Now that we've found all the hardware, start the real time 259 * and statistics clocks. 260 */ 261 initclocks(); 262 263 cold = 0; /* clocks are running, we're warm now! */ 264 265 /* 266 * Now callback to finish configuration for devices which want 267 * to do this once interrupts are enabled. 268 */ 269 config_process_deferred(&interrupt_config_queue, NULL); 270 271 errcnt = aprint_get_error_count(); 272 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 273 (boothowto & AB_VERBOSE) == 0) { 274 if (config_do_twiddle) { 275 config_do_twiddle = 0; 276 printf_nolog("done.\n"); 277 } 278 if (errcnt != 0) { 279 printf("WARNING: %d error%s while detecting hardware; " 280 "check system log.\n", errcnt, 281 errcnt == 1 ? "" : "s"); 282 } 283 } 284 } 285 286 /* 287 * Add a cfdriver to the system. 288 */ 289 int 290 config_cfdriver_attach(struct cfdriver *cd) 291 { 292 struct cfdriver *lcd; 293 294 /* Make sure this driver isn't already in the system. */ 295 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 296 if (STREQ(lcd->cd_name, cd->cd_name)) 297 return (EEXIST); 298 } 299 300 LIST_INIT(&cd->cd_attach); 301 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 302 303 return (0); 304 } 305 306 /* 307 * Remove a cfdriver from the system. 308 */ 309 int 310 config_cfdriver_detach(struct cfdriver *cd) 311 { 312 int i; 313 314 /* Make sure there are no active instances. */ 315 for (i = 0; i < cd->cd_ndevs; i++) { 316 if (cd->cd_devs[i] != NULL) 317 return (EBUSY); 318 } 319 320 /* ...and no attachments loaded. */ 321 if (LIST_EMPTY(&cd->cd_attach) == 0) 322 return (EBUSY); 323 324 LIST_REMOVE(cd, cd_list); 325 326 KASSERT(cd->cd_devs == NULL); 327 328 return (0); 329 } 330 331 /* 332 * Look up a cfdriver by name. 333 */ 334 struct cfdriver * 335 config_cfdriver_lookup(const char *name) 336 { 337 struct cfdriver *cd; 338 339 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 340 if (STREQ(cd->cd_name, name)) 341 return (cd); 342 } 343 344 return (NULL); 345 } 346 347 /* 348 * Add a cfattach to the specified driver. 349 */ 350 int 351 config_cfattach_attach(const char *driver, struct cfattach *ca) 352 { 353 struct cfattach *lca; 354 struct cfdriver *cd; 355 356 cd = config_cfdriver_lookup(driver); 357 if (cd == NULL) 358 return (ESRCH); 359 360 /* Make sure this attachment isn't already on this driver. */ 361 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 362 if (STREQ(lca->ca_name, ca->ca_name)) 363 return (EEXIST); 364 } 365 366 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 367 368 return (0); 369 } 370 371 /* 372 * Remove a cfattach from the specified driver. 373 */ 374 int 375 config_cfattach_detach(const char *driver, struct cfattach *ca) 376 { 377 struct cfdriver *cd; 378 struct device *dev; 379 int i; 380 381 cd = config_cfdriver_lookup(driver); 382 if (cd == NULL) 383 return (ESRCH); 384 385 /* Make sure there are no active instances. */ 386 for (i = 0; i < cd->cd_ndevs; i++) { 387 if ((dev = cd->cd_devs[i]) == NULL) 388 continue; 389 if (dev->dv_cfattach == ca) 390 return (EBUSY); 391 } 392 393 LIST_REMOVE(ca, ca_list); 394 395 return (0); 396 } 397 398 /* 399 * Look up a cfattach by name. 400 */ 401 static struct cfattach * 402 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 403 { 404 struct cfattach *ca; 405 406 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 407 if (STREQ(ca->ca_name, atname)) 408 return (ca); 409 } 410 411 return (NULL); 412 } 413 414 /* 415 * Look up a cfattach by driver/attachment name. 416 */ 417 struct cfattach * 418 config_cfattach_lookup(const char *name, const char *atname) 419 { 420 struct cfdriver *cd; 421 422 cd = config_cfdriver_lookup(name); 423 if (cd == NULL) 424 return (NULL); 425 426 return (config_cfattach_lookup_cd(cd, atname)); 427 } 428 429 /* 430 * Apply the matching function and choose the best. This is used 431 * a few times and we want to keep the code small. 432 */ 433 static void 434 mapply(struct matchinfo *m, struct cfdata *cf) 435 { 436 int pri; 437 438 if (m->fn != NULL) 439 pri = (*m->fn)(m->parent, cf, m->aux); 440 else { 441 struct cfattach *ca; 442 443 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 444 if (ca == NULL) { 445 /* No attachment for this entry, oh well. */ 446 return; 447 } 448 if (ca->ca_match == NULL) { 449 panic("mapply: no match function for '%s' attachment " 450 "of '%s'", cf->cf_atname, cf->cf_name); 451 } 452 pri = (*ca->ca_match)(m->parent, cf, m->aux); 453 } 454 if (pri > m->pri) { 455 m->match = cf; 456 m->pri = pri; 457 } 458 } 459 460 /* 461 * Determine if `parent' is a potential parent for a device spec based 462 * on `cfp'. 463 */ 464 static int 465 cfparent_match(struct device *parent, const struct cfparent *cfp) 466 { 467 struct cfdriver *pcd; 468 const char * const *cpp; 469 const char *cp; 470 471 /* We don't match root nodes here. */ 472 if (cfp == NULL) 473 return (0); 474 475 pcd = parent->dv_cfdriver; 476 KASSERT(pcd != NULL); 477 478 /* 479 * First, ensure this parent has the correct interface 480 * attribute. 481 */ 482 if (pcd->cd_attrs == NULL) 483 return (0); /* no interface attributes -> no children */ 484 for (cpp = pcd->cd_attrs; (cp = *cpp) != NULL; cpp++) { 485 if (STREQ(cp, cfp->cfp_iattr)) { 486 /* Match. */ 487 break; 488 } 489 } 490 if (cp == NULL) 491 return (0); /* doesn't carry the req'd attribute */ 492 493 /* 494 * If no specific parent device instance was specified (i.e. 495 * we're attaching to the attribute only), we're done! 496 */ 497 if (cfp->cfp_parent == NULL) 498 return (1); 499 500 /* 501 * Check the parent device's name. 502 */ 503 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 504 return (0); /* not the same parent */ 505 506 /* 507 * Make sure the unit number matches. 508 */ 509 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 510 cfp->cfp_unit == parent->dv_unit) 511 return (1); 512 513 /* Unit numbers don't match. */ 514 return (0); 515 } 516 517 /* 518 * Invoke the "match" routine for a cfdata entry on behalf of 519 * an external caller, usually a "submatch" routine. 520 */ 521 int 522 config_match(struct device *parent, struct cfdata *cf, void *aux) 523 { 524 struct cfattach *ca; 525 526 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 527 if (ca == NULL) { 528 /* No attachment for this entry, oh well. */ 529 return (0); 530 } 531 532 return ((*ca->ca_match)(parent, cf, aux)); 533 } 534 535 /* 536 * Iterate over all potential children of some device, calling the given 537 * function (default being the child's match function) for each one. 538 * Nonzero returns are matches; the highest value returned is considered 539 * the best match. Return the `found child' if we got a match, or NULL 540 * otherwise. The `aux' pointer is simply passed on through. 541 * 542 * Note that this function is designed so that it can be used to apply 543 * an arbitrary function to all potential children (its return value 544 * can be ignored). 545 */ 546 struct cfdata * 547 config_search(cfmatch_t fn, struct device *parent, void *aux) 548 { 549 struct cftable *ct; 550 struct cfdata *cf; 551 struct matchinfo m; 552 553 KASSERT(config_initialized); 554 555 m.fn = fn; 556 m.parent = parent; 557 m.aux = aux; 558 m.match = NULL; 559 m.pri = 0; 560 561 TAILQ_FOREACH(ct, &allcftables, ct_list) { 562 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 563 /* 564 * Skip cf if no longer eligible, otherwise scan 565 * through parents for one matching `parent', and 566 * try match function. 567 */ 568 if (cf->cf_fstate == FSTATE_FOUND) 569 continue; 570 if (cf->cf_fstate == FSTATE_DNOTFOUND || 571 cf->cf_fstate == FSTATE_DSTAR) 572 continue; 573 if (cfparent_match(parent, cf->cf_pspec)) 574 mapply(&m, cf); 575 } 576 } 577 return (m.match); 578 } 579 580 /* 581 * Find the given root device. 582 * This is much like config_search, but there is no parent. 583 * Don't bother with multiple cfdata tables; the root node 584 * must always be in the initial table. 585 */ 586 struct cfdata * 587 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux) 588 { 589 struct cfdata *cf; 590 const short *p; 591 struct matchinfo m; 592 593 m.fn = fn; 594 m.parent = ROOT; 595 m.aux = aux; 596 m.match = NULL; 597 m.pri = 0; 598 /* 599 * Look at root entries for matching name. We do not bother 600 * with found-state here since only one root should ever be 601 * searched (and it must be done first). 602 */ 603 for (p = cfroots; *p >= 0; p++) { 604 cf = &cfdata[*p]; 605 if (strcmp(cf->cf_name, rootname) == 0) 606 mapply(&m, cf); 607 } 608 return (m.match); 609 } 610 611 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 612 613 /* 614 * The given `aux' argument describes a device that has been found 615 * on the given parent, but not necessarily configured. Locate the 616 * configuration data for that device (using the submatch function 617 * provided, or using candidates' cd_match configuration driver 618 * functions) and attach it, and return true. If the device was 619 * not configured, call the given `print' function and return 0. 620 */ 621 struct device * 622 config_found_sm(struct device *parent, void *aux, cfprint_t print, 623 cfmatch_t submatch) 624 { 625 struct cfdata *cf; 626 627 if ((cf = config_search(submatch, parent, aux)) != NULL) 628 return (config_attach(parent, cf, aux, print)); 629 if (print) { 630 if (config_do_twiddle) 631 twiddle(); 632 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]); 633 } 634 return (NULL); 635 } 636 637 /* 638 * As above, but for root devices. 639 */ 640 struct device * 641 config_rootfound(const char *rootname, void *aux) 642 { 643 struct cfdata *cf; 644 645 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) 646 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 647 aprint_error("root device %s not configured\n", rootname); 648 return (NULL); 649 } 650 651 /* just like sprintf(buf, "%d") except that it works from the end */ 652 static char * 653 number(char *ep, int n) 654 { 655 656 *--ep = 0; 657 while (n >= 10) { 658 *--ep = (n % 10) + '0'; 659 n /= 10; 660 } 661 *--ep = n + '0'; 662 return (ep); 663 } 664 665 /* 666 * Expand the size of the cd_devs array if necessary. 667 */ 668 void 669 config_makeroom(int n, struct cfdriver *cd) 670 { 671 int old, new; 672 void **nsp; 673 674 if (n < cd->cd_ndevs) 675 return; 676 677 /* 678 * Need to expand the array. 679 */ 680 old = cd->cd_ndevs; 681 if (old == 0) 682 new = MINALLOCSIZE / sizeof(void *); 683 else 684 new = old * 2; 685 while (new <= n) 686 new *= 2; 687 cd->cd_ndevs = new; 688 nsp = malloc(new * sizeof(void *), M_DEVBUF, 689 cold ? M_NOWAIT : M_WAITOK); 690 if (nsp == NULL) 691 panic("config_attach: %sing dev array", 692 old != 0 ? "expand" : "creat"); 693 memset(nsp + old, 0, (new - old) * sizeof(void *)); 694 if (old != 0) { 695 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 696 free(cd->cd_devs, M_DEVBUF); 697 } 698 cd->cd_devs = nsp; 699 } 700 701 /* 702 * Attach a found device. Allocates memory for device variables. 703 */ 704 struct device * 705 config_attach(struct device *parent, struct cfdata *cf, void *aux, 706 cfprint_t print) 707 { 708 struct device *dev; 709 struct cftable *ct; 710 struct cfdriver *cd; 711 struct cfattach *ca; 712 size_t lname, lunit; 713 const char *xunit; 714 int myunit; 715 char num[10]; 716 717 cd = config_cfdriver_lookup(cf->cf_name); 718 KASSERT(cd != NULL); 719 720 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 721 KASSERT(ca != NULL); 722 723 if (ca->ca_devsize < sizeof(struct device)) 724 panic("config_attach"); 725 726 #ifndef __BROKEN_CONFIG_UNIT_USAGE 727 if (cf->cf_fstate == FSTATE_STAR) { 728 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 729 if (cd->cd_devs[myunit] == NULL) 730 break; 731 /* 732 * myunit is now the unit of the first NULL device pointer, 733 * or max(cd->cd_ndevs,cf->cf_unit). 734 */ 735 } else { 736 myunit = cf->cf_unit; 737 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 738 cf->cf_fstate = FSTATE_FOUND; 739 } 740 #else 741 myunit = cf->cf_unit; 742 if (cf->cf_fstate == FSTATE_STAR) 743 cf->cf_unit++; 744 else { 745 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 746 cf->cf_fstate = FSTATE_FOUND; 747 } 748 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */ 749 750 /* compute length of name and decimal expansion of unit number */ 751 lname = strlen(cd->cd_name); 752 xunit = number(&num[sizeof(num)], myunit); 753 lunit = &num[sizeof(num)] - xunit; 754 if (lname + lunit > sizeof(dev->dv_xname)) 755 panic("config_attach: device name too long"); 756 757 /* get memory for all device vars */ 758 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, 759 cold ? M_NOWAIT : M_WAITOK); 760 if (!dev) 761 panic("config_attach: memory allocation for device softc failed"); 762 memset(dev, 0, ca->ca_devsize); 763 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 764 dev->dv_class = cd->cd_class; 765 dev->dv_cfdata = cf; 766 dev->dv_cfdriver = cd; 767 dev->dv_cfattach = ca; 768 dev->dv_unit = myunit; 769 memcpy(dev->dv_xname, cd->cd_name, lname); 770 memcpy(dev->dv_xname + lname, xunit, lunit); 771 dev->dv_parent = parent; 772 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 773 774 if (config_do_twiddle) 775 twiddle(); 776 else 777 aprint_naive("Found "); 778 /* 779 * We want the next two printfs for normal, verbose, and quiet, 780 * but not silent (in which case, we're twiddling, instead). 781 */ 782 if (parent == ROOT) { 783 aprint_naive("%s (root)", dev->dv_xname); 784 aprint_normal("%s (root)", dev->dv_xname); 785 } else { 786 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname); 787 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname); 788 if (print) 789 (void) (*print)(aux, NULL); 790 } 791 792 /* put this device in the devices array */ 793 config_makeroom(dev->dv_unit, cd); 794 if (cd->cd_devs[dev->dv_unit]) 795 panic("config_attach: duplicate %s", dev->dv_xname); 796 cd->cd_devs[dev->dv_unit] = dev; 797 798 /* 799 * Before attaching, clobber any unfound devices that are 800 * otherwise identical. 801 */ 802 TAILQ_FOREACH(ct, &allcftables, ct_list) { 803 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 804 if (STREQ(cf->cf_name, cd->cd_name) && 805 cf->cf_unit == dev->dv_unit) { 806 if (cf->cf_fstate == FSTATE_NOTFOUND) 807 cf->cf_fstate = FSTATE_FOUND; 808 #ifdef __BROKEN_CONFIG_UNIT_USAGE 809 /* 810 * Bump the unit number on all starred cfdata 811 * entries for this device. 812 */ 813 if (cf->cf_fstate == FSTATE_STAR) 814 cf->cf_unit++; 815 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 816 } 817 } 818 } 819 #ifdef __HAVE_DEVICE_REGISTER 820 device_register(dev, aux); 821 #endif 822 (*ca->ca_attach)(parent, dev, aux); 823 config_process_deferred(&deferred_config_queue, dev); 824 return (dev); 825 } 826 827 /* 828 * As above, but for pseudo-devices. Pseudo-devices attached in this 829 * way are silently inserted into the device tree, and their children 830 * attached. 831 * 832 * Note that because pseudo-devices are attached silently, any information 833 * the attach routine wishes to print should be prefixed with the device 834 * name by the attach routine. 835 */ 836 struct device * 837 config_attach_pseudo(const char *name, int unit) 838 { 839 struct device *dev; 840 struct cfdriver *cd; 841 struct cfattach *ca; 842 size_t lname, lunit; 843 const char *xunit; 844 int myunit; 845 char num[10]; 846 847 cd = config_cfdriver_lookup(name); 848 if (cd == NULL) 849 return (NULL); 850 851 ca = config_cfattach_lookup_cd(cd, name); 852 if (ca == NULL) 853 return (NULL); 854 855 if (ca->ca_devsize < sizeof(struct device)) 856 panic("config_attach_pseudo"); 857 858 if (unit == DVUNIT_ANY) { 859 for (myunit = 0; myunit < cd->cd_ndevs; myunit++) 860 if (cd->cd_devs[myunit] == NULL) 861 break; 862 /* 863 * myunit is now the unit of the first NULL device pointer. 864 */ 865 } else { 866 myunit = unit; 867 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL) 868 return (NULL); 869 } 870 871 /* compute length of name and decimal expansion of unit number */ 872 lname = strlen(cd->cd_name); 873 xunit = number(&num[sizeof(num)], myunit); 874 lunit = &num[sizeof(num)] - xunit; 875 if (lname + lunit > sizeof(dev->dv_xname)) 876 panic("config_attach_pseudo: device name too long"); 877 878 /* get memory for all device vars */ 879 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, 880 cold ? M_NOWAIT : M_WAITOK); 881 if (!dev) 882 panic("config_attach_pseudo: memory allocation for device " 883 "softc failed"); 884 memset(dev, 0, ca->ca_devsize); 885 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 886 dev->dv_class = cd->cd_class; 887 dev->dv_cfdata = NULL; 888 dev->dv_cfdriver = cd; 889 dev->dv_cfattach = ca; 890 dev->dv_unit = myunit; 891 memcpy(dev->dv_xname, cd->cd_name, lname); 892 memcpy(dev->dv_xname + lname, xunit, lunit); 893 dev->dv_parent = ROOT; 894 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 895 896 /* put this device in the devices array */ 897 config_makeroom(dev->dv_unit, cd); 898 if (cd->cd_devs[dev->dv_unit]) 899 panic("config_attach_pseudo: duplicate %s", dev->dv_xname); 900 cd->cd_devs[dev->dv_unit] = dev; 901 902 #if 0 /* XXXJRT not yet */ 903 #ifdef __HAVE_DEVICE_REGISTER 904 device_register(dev, NULL); /* like a root node */ 905 #endif 906 #endif 907 (*ca->ca_attach)(ROOT, dev, NULL); 908 config_process_deferred(&deferred_config_queue, dev); 909 return (dev); 910 } 911 912 /* 913 * Detach a device. Optionally forced (e.g. because of hardware 914 * removal) and quiet. Returns zero if successful, non-zero 915 * (an error code) otherwise. 916 * 917 * Note that this code wants to be run from a process context, so 918 * that the detach can sleep to allow processes which have a device 919 * open to run and unwind their stacks. 920 */ 921 int 922 config_detach(struct device *dev, int flags) 923 { 924 struct cftable *ct; 925 struct cfdata *cf; 926 const struct cfattach *ca; 927 struct cfdriver *cd; 928 #ifdef DIAGNOSTIC 929 struct device *d; 930 #endif 931 int rv = 0, i; 932 933 #ifdef DIAGNOSTIC 934 if (dev->dv_cfdata != NULL && 935 dev->dv_cfdata->cf_fstate != FSTATE_FOUND && 936 dev->dv_cfdata->cf_fstate != FSTATE_STAR) 937 panic("config_detach: bad device fstate"); 938 #endif 939 cd = dev->dv_cfdriver; 940 KASSERT(cd != NULL); 941 942 ca = dev->dv_cfattach; 943 KASSERT(ca != NULL); 944 945 /* 946 * Ensure the device is deactivated. If the device doesn't 947 * have an activation entry point, we allow DVF_ACTIVE to 948 * remain set. Otherwise, if DVF_ACTIVE is still set, the 949 * device is busy, and the detach fails. 950 */ 951 if (ca->ca_activate != NULL) 952 rv = config_deactivate(dev); 953 954 /* 955 * Try to detach the device. If that's not possible, then 956 * we either panic() (for the forced but failed case), or 957 * return an error. 958 */ 959 if (rv == 0) { 960 if (ca->ca_detach != NULL) 961 rv = (*ca->ca_detach)(dev, flags); 962 else 963 rv = EOPNOTSUPP; 964 } 965 if (rv != 0) { 966 if ((flags & DETACH_FORCE) == 0) 967 return (rv); 968 else 969 panic("config_detach: forced detach of %s failed (%d)", 970 dev->dv_xname, rv); 971 } 972 973 /* 974 * The device has now been successfully detached. 975 */ 976 977 #ifdef DIAGNOSTIC 978 /* 979 * Sanity: If you're successfully detached, you should have no 980 * children. (Note that because children must be attached 981 * after parents, we only need to search the latter part of 982 * the list.) 983 */ 984 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 985 d = TAILQ_NEXT(d, dv_list)) { 986 if (d->dv_parent == dev) { 987 printf("config_detach: detached device %s" 988 " has children %s\n", dev->dv_xname, d->dv_xname); 989 panic("config_detach"); 990 } 991 } 992 #endif 993 994 /* 995 * Mark cfdata to show that the unit can be reused, if possible. 996 */ 997 TAILQ_FOREACH(ct, &allcftables, ct_list) { 998 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 999 if (STREQ(cf->cf_name, cd->cd_name)) { 1000 if (cf->cf_fstate == FSTATE_FOUND && 1001 cf->cf_unit == dev->dv_unit) 1002 cf->cf_fstate = FSTATE_NOTFOUND; 1003 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1004 /* 1005 * Note that we can only re-use a starred 1006 * unit number if the unit being detached 1007 * had the last assigned unit number. 1008 */ 1009 if (cf->cf_fstate == FSTATE_STAR && 1010 cf->cf_unit == dev->dv_unit + 1) 1011 cf->cf_unit--; 1012 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1013 } 1014 } 1015 } 1016 1017 /* 1018 * Unlink from device list. 1019 */ 1020 TAILQ_REMOVE(&alldevs, dev, dv_list); 1021 1022 /* 1023 * Remove from cfdriver's array, tell the world (unless it was 1024 * a pseudo-device), and free softc. 1025 */ 1026 cd->cd_devs[dev->dv_unit] = NULL; 1027 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1028 aprint_normal("%s detached\n", dev->dv_xname); 1029 free(dev, M_DEVBUF); 1030 1031 /* 1032 * If the device now has no units in use, deallocate its softc array. 1033 */ 1034 for (i = 0; i < cd->cd_ndevs; i++) 1035 if (cd->cd_devs[i] != NULL) 1036 break; 1037 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 1038 free(cd->cd_devs, M_DEVBUF); 1039 cd->cd_devs = NULL; 1040 cd->cd_ndevs = 0; 1041 } 1042 1043 /* 1044 * Return success. 1045 */ 1046 return (0); 1047 } 1048 1049 int 1050 config_activate(struct device *dev) 1051 { 1052 const struct cfattach *ca = dev->dv_cfattach; 1053 int rv = 0, oflags = dev->dv_flags; 1054 1055 if (ca->ca_activate == NULL) 1056 return (EOPNOTSUPP); 1057 1058 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 1059 dev->dv_flags |= DVF_ACTIVE; 1060 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 1061 if (rv) 1062 dev->dv_flags = oflags; 1063 } 1064 return (rv); 1065 } 1066 1067 int 1068 config_deactivate(struct device *dev) 1069 { 1070 const struct cfattach *ca = dev->dv_cfattach; 1071 int rv = 0, oflags = dev->dv_flags; 1072 1073 if (ca->ca_activate == NULL) 1074 return (EOPNOTSUPP); 1075 1076 if (dev->dv_flags & DVF_ACTIVE) { 1077 dev->dv_flags &= ~DVF_ACTIVE; 1078 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 1079 if (rv) 1080 dev->dv_flags = oflags; 1081 } 1082 return (rv); 1083 } 1084 1085 /* 1086 * Defer the configuration of the specified device until all 1087 * of its parent's devices have been attached. 1088 */ 1089 void 1090 config_defer(struct device *dev, void (*func)(struct device *)) 1091 { 1092 struct deferred_config *dc; 1093 1094 if (dev->dv_parent == NULL) 1095 panic("config_defer: can't defer config of a root device"); 1096 1097 #ifdef DIAGNOSTIC 1098 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 1099 dc = TAILQ_NEXT(dc, dc_queue)) { 1100 if (dc->dc_dev == dev) 1101 panic("config_defer: deferred twice"); 1102 } 1103 #endif 1104 1105 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1106 if (dc == NULL) 1107 panic("config_defer: unable to allocate callback"); 1108 1109 dc->dc_dev = dev; 1110 dc->dc_func = func; 1111 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1112 config_pending_incr(); 1113 } 1114 1115 /* 1116 * Defer some autoconfiguration for a device until after interrupts 1117 * are enabled. 1118 */ 1119 void 1120 config_interrupts(struct device *dev, void (*func)(struct device *)) 1121 { 1122 struct deferred_config *dc; 1123 1124 /* 1125 * If interrupts are enabled, callback now. 1126 */ 1127 if (cold == 0) { 1128 (*func)(dev); 1129 return; 1130 } 1131 1132 #ifdef DIAGNOSTIC 1133 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 1134 dc = TAILQ_NEXT(dc, dc_queue)) { 1135 if (dc->dc_dev == dev) 1136 panic("config_interrupts: deferred twice"); 1137 } 1138 #endif 1139 1140 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1141 if (dc == NULL) 1142 panic("config_interrupts: unable to allocate callback"); 1143 1144 dc->dc_dev = dev; 1145 dc->dc_func = func; 1146 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1147 config_pending_incr(); 1148 } 1149 1150 /* 1151 * Process a deferred configuration queue. 1152 */ 1153 static void 1154 config_process_deferred(struct deferred_config_head *queue, 1155 struct device *parent) 1156 { 1157 struct deferred_config *dc, *ndc; 1158 1159 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 1160 ndc = TAILQ_NEXT(dc, dc_queue); 1161 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 1162 TAILQ_REMOVE(queue, dc, dc_queue); 1163 (*dc->dc_func)(dc->dc_dev); 1164 free(dc, M_DEVBUF); 1165 config_pending_decr(); 1166 } 1167 } 1168 } 1169 1170 /* 1171 * Manipulate the config_pending semaphore. 1172 */ 1173 void 1174 config_pending_incr(void) 1175 { 1176 1177 config_pending++; 1178 } 1179 1180 void 1181 config_pending_decr(void) 1182 { 1183 1184 #ifdef DIAGNOSTIC 1185 if (config_pending == 0) 1186 panic("config_pending_decr: config_pending == 0"); 1187 #endif 1188 config_pending--; 1189 if (config_pending == 0) 1190 wakeup((void *)&config_pending); 1191 } 1192 1193 /* 1194 * Register a "finalization" routine. Finalization routines are 1195 * called iteratively once all real devices have been found during 1196 * autoconfiguration, for as long as any one finalizer has done 1197 * any work. 1198 */ 1199 int 1200 config_finalize_register(struct device *dev, int (*fn)(struct device *)) 1201 { 1202 struct finalize_hook *f; 1203 1204 /* 1205 * If finalization has already been done, invoke the 1206 * callback function now. 1207 */ 1208 if (config_finalize_done) { 1209 while ((*fn)(dev) != 0) 1210 /* loop */ ; 1211 } 1212 1213 /* Ensure this isn't already on the list. */ 1214 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 1215 if (f->f_func == fn && f->f_dev == dev) 1216 return (EEXIST); 1217 } 1218 1219 f = malloc(sizeof(*f), M_TEMP, M_WAITOK); 1220 f->f_func = fn; 1221 f->f_dev = dev; 1222 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 1223 1224 return (0); 1225 } 1226 1227 void 1228 config_finalize(void) 1229 { 1230 struct finalize_hook *f; 1231 int rv; 1232 1233 /* Run the hooks until none of them does any work. */ 1234 do { 1235 rv = 0; 1236 TAILQ_FOREACH(f, &config_finalize_list, f_list) 1237 rv |= (*f->f_func)(f->f_dev); 1238 } while (rv != 0); 1239 1240 config_finalize_done = 1; 1241 1242 /* Now free all the hooks. */ 1243 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 1244 TAILQ_REMOVE(&config_finalize_list, f, f_list); 1245 free(f, M_TEMP); 1246 } 1247 } 1248 1249 /* 1250 * We need a dummy object to stuff into the evcnt link set to 1251 * ensure that there always is at least one object in the set. 1252 */ 1253 static struct evcnt dummy_static_evcnt; 1254 __link_set_add_bss(evcnts, dummy_static_evcnt); 1255 1256 /* 1257 * Initialize event counters. This does the attach procedure for 1258 * each of the static event counters in the "evcnts" link set. 1259 */ 1260 void 1261 evcnt_init(void) 1262 { 1263 __link_set_decl(evcnts, struct evcnt); 1264 struct evcnt * const *evp; 1265 1266 __link_set_foreach(evp, evcnts) { 1267 if (*evp == &dummy_static_evcnt) 1268 continue; 1269 evcnt_attach_static(*evp); 1270 } 1271 } 1272 1273 /* 1274 * Attach a statically-initialized event. The type and string pointers 1275 * are already set up. 1276 */ 1277 void 1278 evcnt_attach_static(struct evcnt *ev) 1279 { 1280 int len; 1281 1282 len = strlen(ev->ev_group); 1283 #ifdef DIAGNOSTIC 1284 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 1285 panic("evcnt_attach_static: group length (%s)", ev->ev_group); 1286 #endif 1287 ev->ev_grouplen = len; 1288 1289 len = strlen(ev->ev_name); 1290 #ifdef DIAGNOSTIC 1291 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 1292 panic("evcnt_attach_static: name length (%s)", ev->ev_name); 1293 #endif 1294 ev->ev_namelen = len; 1295 1296 TAILQ_INSERT_TAIL(&allevents, ev, ev_list); 1297 } 1298 1299 /* 1300 * Attach a dynamically-initialized event. Zero it, set up the type 1301 * and string pointers and then act like it was statically initialized. 1302 */ 1303 void 1304 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent, 1305 const char *group, const char *name) 1306 { 1307 1308 memset(ev, 0, sizeof *ev); 1309 ev->ev_type = type; 1310 ev->ev_parent = parent; 1311 ev->ev_group = group; 1312 ev->ev_name = name; 1313 evcnt_attach_static(ev); 1314 } 1315 1316 /* 1317 * Detach an event. 1318 */ 1319 void 1320 evcnt_detach(struct evcnt *ev) 1321 { 1322 1323 TAILQ_REMOVE(&allevents, ev, ev_list); 1324 } 1325 1326 #ifdef DDB 1327 void 1328 event_print(int full, void (*pr)(const char *, ...)) 1329 { 1330 struct evcnt *evp; 1331 1332 TAILQ_FOREACH(evp, &allevents, ev_list) { 1333 if (evp->ev_count == 0 && !full) 1334 continue; 1335 1336 (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type, 1337 evp->ev_group, evp->ev_name, evp->ev_count); 1338 } 1339 } 1340 #endif /* DDB */ 1341