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