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