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