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