1 /* $NetBSD: subr_autoconf.c,v 1.63 2002/04/15 05:30:12 gmcgarry 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.63 2002/04/15 05:30:12 gmcgarry 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 <machine/limits.h> 96 97 #include "opt_userconf.h" 98 #ifdef USERCONF 99 #include <sys/userconf.h> 100 #include <sys/reboot.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 short cfroots[]; 113 114 #define ROOT ((struct device *)NULL) 115 116 struct matchinfo { 117 cfmatch_t fn; 118 struct device *parent; 119 void *aux; 120 struct cfdata *match; 121 int pri; 122 }; 123 124 static char *number(char *, int); 125 static void mapply(struct matchinfo *, struct cfdata *); 126 127 struct deferred_config { 128 TAILQ_ENTRY(deferred_config) dc_queue; 129 struct device *dc_dev; 130 void (*dc_func)(struct device *); 131 }; 132 133 TAILQ_HEAD(deferred_config_head, deferred_config); 134 135 struct deferred_config_head deferred_config_queue; 136 struct deferred_config_head interrupt_config_queue; 137 138 static void config_process_deferred(struct deferred_config_head *, 139 struct device *); 140 141 /* list of all devices */ 142 struct devicelist alldevs; 143 144 /* list of all events */ 145 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents); 146 147 __volatile int config_pending; /* semaphore for mountroot */ 148 149 /* 150 * Configure the system's hardware. 151 */ 152 void 153 configure(void) 154 { 155 156 TAILQ_INIT(&deferred_config_queue); 157 TAILQ_INIT(&interrupt_config_queue); 158 TAILQ_INIT(&alldevs); 159 160 #ifdef USERCONF 161 if (boothowto & RB_USERCONF) 162 user_config(); 163 #endif 164 165 /* 166 * Do the machine-dependent portion of autoconfiguration. This 167 * sets the configuration machinery here in motion by "finding" 168 * the root bus. When this function returns, we expect interrupts 169 * to be enabled. 170 */ 171 cpu_configure(); 172 173 /* 174 * Now that we've found all the hardware, start the real time 175 * and statistics clocks. 176 */ 177 initclocks(); 178 179 cold = 0; /* clocks are running, we're warm now! */ 180 181 /* 182 * Now callback to finish configuration for devices which want 183 * to do this once interrupts are enabled. 184 */ 185 config_process_deferred(&interrupt_config_queue, NULL); 186 } 187 188 /* 189 * Apply the matching function and choose the best. This is used 190 * a few times and we want to keep the code small. 191 */ 192 static void 193 mapply(struct matchinfo *m, struct cfdata *cf) 194 { 195 int pri; 196 197 if (m->fn != NULL) 198 pri = (*m->fn)(m->parent, cf, m->aux); 199 else { 200 if (cf->cf_attach->ca_match == NULL) { 201 panic("mapply: no match function for '%s' device\n", 202 cf->cf_driver->cd_name); 203 } 204 pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux); 205 } 206 if (pri > m->pri) { 207 m->match = cf; 208 m->pri = pri; 209 } 210 } 211 212 /* 213 * Iterate over all potential children of some device, calling the given 214 * function (default being the child's match function) for each one. 215 * Nonzero returns are matches; the highest value returned is considered 216 * the best match. Return the `found child' if we got a match, or NULL 217 * otherwise. The `aux' pointer is simply passed on through. 218 * 219 * Note that this function is designed so that it can be used to apply 220 * an arbitrary function to all potential children (its return value 221 * can be ignored). 222 */ 223 struct cfdata * 224 config_search(cfmatch_t fn, struct device *parent, void *aux) 225 { 226 struct cfdata *cf; 227 short *p; 228 struct matchinfo m; 229 230 m.fn = fn; 231 m.parent = parent; 232 m.aux = aux; 233 m.match = NULL; 234 m.pri = 0; 235 for (cf = cfdata; cf->cf_driver; cf++) { 236 /* 237 * Skip cf if no longer eligible, otherwise scan through 238 * parents for one matching `parent', and try match function. 239 */ 240 if (cf->cf_fstate == FSTATE_FOUND) 241 continue; 242 if (cf->cf_fstate == FSTATE_DNOTFOUND || 243 cf->cf_fstate == FSTATE_DSTAR) 244 continue; 245 for (p = cf->cf_parents; *p >= 0; p++) 246 if (parent->dv_cfdata == &cfdata[*p]) 247 mapply(&m, cf); 248 } 249 return (m.match); 250 } 251 252 /* 253 * Find the given root device. 254 * This is much like config_search, but there is no parent. 255 */ 256 struct cfdata * 257 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux) 258 { 259 struct cfdata *cf; 260 short *p; 261 struct matchinfo m; 262 263 m.fn = fn; 264 m.parent = ROOT; 265 m.aux = aux; 266 m.match = NULL; 267 m.pri = 0; 268 /* 269 * Look at root entries for matching name. We do not bother 270 * with found-state here since only one root should ever be 271 * searched (and it must be done first). 272 */ 273 for (p = cfroots; *p >= 0; p++) { 274 cf = &cfdata[*p]; 275 if (strcmp(cf->cf_driver->cd_name, rootname) == 0) 276 mapply(&m, cf); 277 } 278 return (m.match); 279 } 280 281 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" }; 282 283 /* 284 * The given `aux' argument describes a device that has been found 285 * on the given parent, but not necessarily configured. Locate the 286 * configuration data for that device (using the submatch function 287 * provided, or using candidates' cd_match configuration driver 288 * functions) and attach it, and return true. If the device was 289 * not configured, call the given `print' function and return 0. 290 */ 291 struct device * 292 config_found_sm(struct device *parent, void *aux, cfprint_t print, 293 cfmatch_t submatch) 294 { 295 struct cfdata *cf; 296 297 if ((cf = config_search(submatch, parent, aux)) != NULL) 298 return (config_attach(parent, cf, aux, print)); 299 if (print) 300 printf("%s", msgs[(*print)(aux, parent->dv_xname)]); 301 return (NULL); 302 } 303 304 /* 305 * As above, but for root devices. 306 */ 307 struct device * 308 config_rootfound(const char *rootname, void *aux) 309 { 310 struct cfdata *cf; 311 312 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) 313 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 314 printf("root device %s not configured\n", rootname); 315 return (NULL); 316 } 317 318 /* just like sprintf(buf, "%d") except that it works from the end */ 319 static char * 320 number(char *ep, int n) 321 { 322 323 *--ep = 0; 324 while (n >= 10) { 325 *--ep = (n % 10) + '0'; 326 n /= 10; 327 } 328 *--ep = n + '0'; 329 return (ep); 330 } 331 332 /* 333 * Expand the size of the cd_devs array if necessary. 334 */ 335 void 336 config_makeroom(int n, struct cfdriver *cd) 337 { 338 int old, new; 339 void **nsp; 340 341 if (n < cd->cd_ndevs) 342 return; 343 344 /* 345 * Need to expand the array. 346 */ 347 old = cd->cd_ndevs; 348 if (old == 0) 349 new = MINALLOCSIZE / sizeof(void *); 350 else 351 new = old * 2; 352 while (new <= n) 353 new *= 2; 354 cd->cd_ndevs = new; 355 nsp = malloc(new * sizeof(void *), M_DEVBUF, 356 cold ? M_NOWAIT : M_WAITOK); 357 if (nsp == NULL) 358 panic("config_attach: %sing dev array", 359 old != 0 ? "expand" : "creat"); 360 memset(nsp + old, 0, (new - old) * sizeof(void *)); 361 if (old != 0) { 362 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 363 free(cd->cd_devs, M_DEVBUF); 364 } 365 cd->cd_devs = nsp; 366 } 367 368 /* 369 * Attach a found device. Allocates memory for device variables. 370 */ 371 struct device * 372 config_attach(struct device *parent, struct cfdata *cf, void *aux, 373 cfprint_t print) 374 { 375 struct device *dev; 376 struct cfdriver *cd; 377 struct cfattach *ca; 378 size_t lname, lunit; 379 const char *xunit; 380 int myunit; 381 char num[10]; 382 383 cd = cf->cf_driver; 384 ca = cf->cf_attach; 385 if (ca->ca_devsize < sizeof(struct device)) 386 panic("config_attach"); 387 #ifndef __BROKEN_CONFIG_UNIT_USAGE 388 if (cf->cf_fstate == FSTATE_STAR) { 389 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 390 if (cd->cd_devs[myunit] == NULL) 391 break; 392 /* 393 * myunit is now the unit of the first NULL device pointer, 394 * or max(cd->cd_ndevs,cf->cf_unit). 395 */ 396 } else { 397 myunit = cf->cf_unit; 398 #else /* __BROKEN_CONFIG_UNIT_USAGE */ 399 myunit = cf->cf_unit; 400 if (cf->cf_fstate == FSTATE_STAR) 401 cf->cf_unit++; 402 else { 403 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 404 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 405 cf->cf_fstate = FSTATE_FOUND; 406 } 407 408 /* compute length of name and decimal expansion of unit number */ 409 lname = strlen(cd->cd_name); 410 xunit = number(&num[sizeof(num)], myunit); 411 lunit = &num[sizeof(num)] - xunit; 412 if (lname + lunit >= sizeof(dev->dv_xname)) 413 panic("config_attach: device name too long"); 414 415 /* get memory for all device vars */ 416 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, 417 cold ? M_NOWAIT : M_WAITOK); 418 if (!dev) 419 panic("config_attach: memory allocation for device softc failed"); 420 memset(dev, 0, ca->ca_devsize); 421 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 422 dev->dv_class = cd->cd_class; 423 dev->dv_cfdata = cf; 424 dev->dv_unit = myunit; 425 memcpy(dev->dv_xname, cd->cd_name, lname); 426 memcpy(dev->dv_xname + lname, xunit, lunit); 427 dev->dv_parent = parent; 428 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 429 430 if (parent == ROOT) 431 printf("%s (root)", dev->dv_xname); 432 else { 433 printf("%s at %s", dev->dv_xname, parent->dv_xname); 434 if (print) 435 (void) (*print)(aux, NULL); 436 } 437 438 /* put this device in the devices array */ 439 config_makeroom(dev->dv_unit, cd); 440 if (cd->cd_devs[dev->dv_unit]) 441 panic("config_attach: duplicate %s", dev->dv_xname); 442 cd->cd_devs[dev->dv_unit] = dev; 443 444 /* 445 * Before attaching, clobber any unfound devices that are 446 * otherwise identical. 447 */ 448 #ifdef __BROKEN_CONFIG_UNIT_USAGE 449 /* bump the unit number on all starred cfdata for this device. */ 450 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 451 for (cf = cfdata; cf->cf_driver; cf++) 452 if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) { 453 if (cf->cf_fstate == FSTATE_NOTFOUND) 454 cf->cf_fstate = FSTATE_FOUND; 455 #ifdef __BROKEN_CONFIG_UNIT_USAGE 456 if (cf->cf_fstate == FSTATE_STAR) 457 cf->cf_unit++; 458 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 459 } 460 #ifdef __HAVE_DEVICE_REGISTER 461 device_register(dev, aux); 462 #endif 463 (*ca->ca_attach)(parent, dev, aux); 464 config_process_deferred(&deferred_config_queue, dev); 465 return (dev); 466 } 467 468 /* 469 * Detach a device. Optionally forced (e.g. because of hardware 470 * removal) and quiet. Returns zero if successful, non-zero 471 * (an error code) otherwise. 472 * 473 * Note that this code wants to be run from a process context, so 474 * that the detach can sleep to allow processes which have a device 475 * open to run and unwind their stacks. 476 */ 477 int 478 config_detach(struct device *dev, int flags) 479 { 480 struct cfdata *cf; 481 struct cfattach *ca; 482 struct cfdriver *cd; 483 #ifdef DIAGNOSTIC 484 struct device *d; 485 #endif 486 int rv = 0, i; 487 488 cf = dev->dv_cfdata; 489 #ifdef DIAGNOSTIC 490 if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR) 491 panic("config_detach: bad device fstate"); 492 #endif 493 ca = cf->cf_attach; 494 cd = cf->cf_driver; 495 496 /* 497 * Ensure the device is deactivated. If the device doesn't 498 * have an activation entry point, we allow DVF_ACTIVE to 499 * remain set. Otherwise, if DVF_ACTIVE is still set, the 500 * device is busy, and the detach fails. 501 */ 502 if (ca->ca_activate != NULL) 503 rv = config_deactivate(dev); 504 505 /* 506 * Try to detach the device. If that's not possible, then 507 * we either panic() (for the forced but failed case), or 508 * return an error. 509 */ 510 if (rv == 0) { 511 if (ca->ca_detach != NULL) 512 rv = (*ca->ca_detach)(dev, flags); 513 else 514 rv = EOPNOTSUPP; 515 } 516 if (rv != 0) { 517 if ((flags & DETACH_FORCE) == 0) 518 return (rv); 519 else 520 panic("config_detach: forced detach of %s failed (%d)", 521 dev->dv_xname, rv); 522 } 523 524 /* 525 * The device has now been successfully detached. 526 */ 527 528 #ifdef DIAGNOSTIC 529 /* 530 * Sanity: If you're successfully detached, you should have no 531 * children. (Note that because children must be attached 532 * after parents, we only need to search the latter part of 533 * the list.) 534 */ 535 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 536 d = TAILQ_NEXT(d, dv_list)) { 537 if (d->dv_parent == dev) { 538 printf("config_detach: detached device %s" 539 " has children %s\n", dev->dv_xname, d->dv_xname); 540 panic("config_detach"); 541 } 542 } 543 #endif 544 545 /* 546 * Mark cfdata to show that the unit can be reused, if possible. 547 */ 548 #ifdef __BROKEN_CONFIG_UNIT_USAGE 549 /* 550 * Note that we can only re-use a starred unit number if the unit 551 * being detached had the last assigned unit number. 552 */ 553 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 554 for (cf = cfdata; cf->cf_driver; cf++) { 555 if (cf->cf_driver == cd) { 556 if (cf->cf_fstate == FSTATE_FOUND && 557 cf->cf_unit == dev->dv_unit) 558 cf->cf_fstate = FSTATE_NOTFOUND; 559 #ifdef __BROKEN_CONFIG_UNIT_USAGE 560 if (cf->cf_fstate == FSTATE_STAR && 561 cf->cf_unit == dev->dv_unit + 1) 562 cf->cf_unit--; 563 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 564 } 565 } 566 567 /* 568 * Unlink from device list. 569 */ 570 TAILQ_REMOVE(&alldevs, dev, dv_list); 571 572 /* 573 * Remove from cfdriver's array, tell the world, and free softc. 574 */ 575 cd->cd_devs[dev->dv_unit] = NULL; 576 if ((flags & DETACH_QUIET) == 0) 577 printf("%s detached\n", dev->dv_xname); 578 free(dev, M_DEVBUF); 579 580 /* 581 * If the device now has no units in use, deallocate its softc array. 582 */ 583 for (i = 0; i < cd->cd_ndevs; i++) 584 if (cd->cd_devs[i] != NULL) 585 break; 586 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 587 free(cd->cd_devs, M_DEVBUF); 588 cd->cd_devs = NULL; 589 cd->cd_ndevs = 0; 590 } 591 592 /* 593 * Return success. 594 */ 595 return (0); 596 } 597 598 int 599 config_activate(struct device *dev) 600 { 601 struct cfattach *ca = dev->dv_cfdata->cf_attach; 602 int rv = 0, oflags = dev->dv_flags; 603 604 if (ca->ca_activate == NULL) 605 return (EOPNOTSUPP); 606 607 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 608 dev->dv_flags |= DVF_ACTIVE; 609 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 610 if (rv) 611 dev->dv_flags = oflags; 612 } 613 return (rv); 614 } 615 616 int 617 config_deactivate(struct device *dev) 618 { 619 struct cfattach *ca = dev->dv_cfdata->cf_attach; 620 int rv = 0, oflags = dev->dv_flags; 621 622 if (ca->ca_activate == NULL) 623 return (EOPNOTSUPP); 624 625 if (dev->dv_flags & DVF_ACTIVE) { 626 dev->dv_flags &= ~DVF_ACTIVE; 627 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 628 if (rv) 629 dev->dv_flags = oflags; 630 } 631 return (rv); 632 } 633 634 /* 635 * Defer the configuration of the specified device until all 636 * of its parent's devices have been attached. 637 */ 638 void 639 config_defer(struct device *dev, void (*func)(struct device *)) 640 { 641 struct deferred_config *dc; 642 643 if (dev->dv_parent == NULL) 644 panic("config_defer: can't defer config of a root device"); 645 646 #ifdef DIAGNOSTIC 647 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 648 dc = TAILQ_NEXT(dc, dc_queue)) { 649 if (dc->dc_dev == dev) 650 panic("config_defer: deferred twice"); 651 } 652 #endif 653 654 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 655 if (dc == NULL) 656 panic("config_defer: unable to allocate callback"); 657 658 dc->dc_dev = dev; 659 dc->dc_func = func; 660 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 661 config_pending_incr(); 662 } 663 664 /* 665 * Defer some autoconfiguration for a device until after interrupts 666 * are enabled. 667 */ 668 void 669 config_interrupts(struct device *dev, void (*func)(struct device *)) 670 { 671 struct deferred_config *dc; 672 673 /* 674 * If interrupts are enabled, callback now. 675 */ 676 if (cold == 0) { 677 (*func)(dev); 678 return; 679 } 680 681 #ifdef DIAGNOSTIC 682 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 683 dc = TAILQ_NEXT(dc, dc_queue)) { 684 if (dc->dc_dev == dev) 685 panic("config_interrupts: deferred twice"); 686 } 687 #endif 688 689 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 690 if (dc == NULL) 691 panic("config_interrupts: unable to allocate callback"); 692 693 dc->dc_dev = dev; 694 dc->dc_func = func; 695 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 696 config_pending_incr(); 697 } 698 699 /* 700 * Process a deferred configuration queue. 701 */ 702 static void 703 config_process_deferred(struct deferred_config_head *queue, 704 struct device *parent) 705 { 706 struct deferred_config *dc, *ndc; 707 708 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 709 ndc = TAILQ_NEXT(dc, dc_queue); 710 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 711 TAILQ_REMOVE(queue, dc, dc_queue); 712 (*dc->dc_func)(dc->dc_dev); 713 free(dc, M_DEVBUF); 714 config_pending_decr(); 715 } 716 } 717 } 718 719 /* 720 * Manipulate the config_pending semaphore. 721 */ 722 void 723 config_pending_incr(void) 724 { 725 726 config_pending++; 727 } 728 729 void 730 config_pending_decr(void) 731 { 732 733 #ifdef DIAGNOSTIC 734 if (config_pending == 0) 735 panic("config_pending_decr: config_pending == 0"); 736 #endif 737 config_pending--; 738 if (config_pending == 0) 739 wakeup((void *)&config_pending); 740 } 741 742 /* 743 * Attach a statically-initialized event. The type and string pointers 744 * are already set up. 745 */ 746 void 747 evcnt_attach_static(struct evcnt *ev) 748 { 749 int len; 750 751 len = strlen(ev->ev_group); 752 #ifdef DIAGNOSTIC 753 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 754 panic("evcnt_attach_static: group length (%s)", ev->ev_group); 755 #endif 756 ev->ev_grouplen = len; 757 758 len = strlen(ev->ev_name); 759 #ifdef DIAGNOSTIC 760 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 761 panic("evcnt_attach_static: name length (%s)", ev->ev_name); 762 #endif 763 ev->ev_namelen = len; 764 765 TAILQ_INSERT_TAIL(&allevents, ev, ev_list); 766 } 767 768 /* 769 * Attach a dynamically-initialized event. Zero it, set up the type 770 * and string pointers and then act like it was statically initialized. 771 */ 772 void 773 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent, 774 const char *group, const char *name) 775 { 776 777 memset(ev, 0, sizeof *ev); 778 ev->ev_type = type; 779 ev->ev_parent = parent; 780 ev->ev_group = group; 781 ev->ev_name = name; 782 evcnt_attach_static(ev); 783 } 784 785 /* 786 * Detach an event. 787 */ 788 void 789 evcnt_detach(struct evcnt *ev) 790 { 791 792 TAILQ_REMOVE(&allevents, ev, ev_list); 793 } 794 795 #ifdef DDB 796 void 797 event_print(int full, void (*pr)(const char *, ...)) 798 { 799 struct evcnt *evp; 800 801 TAILQ_FOREACH(evp, &allevents, ev_list) { 802 if (evp->ev_count == 0 && !full) 803 continue; 804 805 (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type, 806 evp->ev_group, evp->ev_name, evp->ev_count); 807 } 808 } 809 #endif /* DDB */ 810