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