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