Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.144
      1 /* $NetBSD: subr_autoconf.c,v 1.144 2008/04/14 18:07:51 ad 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.144 2008/04/14 18:07:51 ad Exp $");
     81 
     82 #include "opt_multiprocessor.h"
     83 #include "opt_ddb.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/device.h>
     87 #include <sys/disklabel.h>
     88 #include <sys/conf.h>
     89 #include <sys/kauth.h>
     90 #include <sys/malloc.h>
     91 #include <sys/systm.h>
     92 #include <sys/kernel.h>
     93 #include <sys/errno.h>
     94 #include <sys/proc.h>
     95 #include <sys/reboot.h>
     96 #include <sys/kthread.h>
     97 #include <sys/buf.h>
     98 #include <sys/dirent.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 #include <sys/mutex.h>
    107 #include <sys/condvar.h>
    108 
    109 #include <sys/disk.h>
    110 
    111 #include <machine/limits.h>
    112 
    113 #include "opt_userconf.h"
    114 #ifdef USERCONF
    115 #include <sys/userconf.h>
    116 #endif
    117 
    118 #ifdef __i386__
    119 #include "opt_splash.h"
    120 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    121 #include <dev/splash/splash.h>
    122 extern struct splash_progress *splash_progress_state;
    123 #endif
    124 #endif
    125 
    126 /*
    127  * Autoconfiguration subroutines.
    128  */
    129 
    130 typedef struct pmf_private {
    131 	int		pp_nwait;
    132 	int		pp_nlock;
    133 	lwp_t		*pp_holder;
    134 	kmutex_t	pp_mtx;
    135 	kcondvar_t	pp_cv;
    136 } pmf_private_t;
    137 
    138 /*
    139  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    140  * devices and drivers are found via these tables.
    141  */
    142 extern struct cfdata cfdata[];
    143 extern const short cfroots[];
    144 
    145 /*
    146  * List of all cfdriver structures.  We use this to detect duplicates
    147  * when other cfdrivers are loaded.
    148  */
    149 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
    150 extern struct cfdriver * const cfdriver_list_initial[];
    151 
    152 /*
    153  * Initial list of cfattach's.
    154  */
    155 extern const struct cfattachinit cfattachinit[];
    156 
    157 /*
    158  * List of cfdata tables.  We always have one such list -- the one
    159  * built statically when the kernel was configured.
    160  */
    161 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
    162 static struct cftable initcftable;
    163 
    164 #define	ROOT ((device_t)NULL)
    165 
    166 struct matchinfo {
    167 	cfsubmatch_t fn;
    168 	struct	device *parent;
    169 	const int *locs;
    170 	void	*aux;
    171 	struct	cfdata *match;
    172 	int	pri;
    173 };
    174 
    175 static char *number(char *, int);
    176 static void mapply(struct matchinfo *, cfdata_t);
    177 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
    178 static void config_devdealloc(device_t);
    179 static void config_makeroom(int, struct cfdriver *);
    180 static void config_devlink(device_t);
    181 static void config_devunlink(device_t);
    182 
    183 static void pmflock_debug(device_t, const char *, int);
    184 static void pmflock_debug_with_flags(device_t, const char *, int PMF_FN_PROTO);
    185 
    186 static device_t deviter_next1(deviter_t *);
    187 static void deviter_reinit(deviter_t *);
    188 
    189 struct deferred_config {
    190 	TAILQ_ENTRY(deferred_config) dc_queue;
    191 	device_t dc_dev;
    192 	void (*dc_func)(device_t);
    193 };
    194 
    195 TAILQ_HEAD(deferred_config_head, deferred_config);
    196 
    197 struct deferred_config_head deferred_config_queue =
    198 	TAILQ_HEAD_INITIALIZER(deferred_config_queue);
    199 struct deferred_config_head interrupt_config_queue =
    200 	TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
    201 int interrupt_config_threads = 8;
    202 
    203 static void config_process_deferred(struct deferred_config_head *, device_t);
    204 
    205 /* Hooks to finalize configuration once all real devices have been found. */
    206 struct finalize_hook {
    207 	TAILQ_ENTRY(finalize_hook) f_list;
    208 	int (*f_func)(device_t);
    209 	device_t f_dev;
    210 };
    211 static TAILQ_HEAD(, finalize_hook) config_finalize_list =
    212 	TAILQ_HEAD_INITIALIZER(config_finalize_list);
    213 static int config_finalize_done;
    214 
    215 /* list of all devices */
    216 struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
    217 kcondvar_t alldevs_cv;
    218 kmutex_t alldevs_mtx;
    219 static int alldevs_nread = 0;
    220 static int alldevs_nwrite = 0;
    221 static lwp_t *alldevs_writer = NULL;
    222 
    223 volatile int config_pending;		/* semaphore for mountroot */
    224 
    225 #define	STREQ(s1, s2)			\
    226 	(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
    227 
    228 static int config_initialized;		/* config_init() has been called. */
    229 
    230 static int config_do_twiddle;
    231 
    232 MALLOC_DEFINE(M_PMFPRIV, "pmfpriv", "device pmf private storage");
    233 
    234 struct vnode *
    235 opendisk(struct device *dv)
    236 {
    237 	int bmajor, bminor;
    238 	struct vnode *tmpvn;
    239 	int error;
    240 	dev_t dev;
    241 
    242 	/*
    243 	 * Lookup major number for disk block device.
    244 	 */
    245 	bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
    246 	if (bmajor == -1)
    247 		return NULL;
    248 
    249 	bminor = minor(device_unit(dv));
    250 	/*
    251 	 * Fake a temporary vnode for the disk, open it, and read
    252 	 * and hash the sectors.
    253 	 */
    254 	dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) :
    255 	    MAKEDISKDEV(bmajor, bminor, RAW_PART);
    256 	if (bdevvp(dev, &tmpvn))
    257 		panic("%s: can't alloc vnode for %s", __func__,
    258 		    device_xname(dv));
    259 	error = VOP_OPEN(tmpvn, FREAD, NOCRED);
    260 	if (error) {
    261 #ifndef DEBUG
    262 		/*
    263 		 * Ignore errors caused by missing device, partition,
    264 		 * or medium.
    265 		 */
    266 		if (error != ENXIO && error != ENODEV)
    267 #endif
    268 			printf("%s: can't open dev %s (%d)\n",
    269 			    __func__, device_xname(dv), error);
    270 		vput(tmpvn);
    271 		return NULL;
    272 	}
    273 
    274 	return tmpvn;
    275 }
    276 
    277 int
    278 config_handle_wedges(struct device *dv, int par)
    279 {
    280 	struct dkwedge_list wl;
    281 	struct dkwedge_info *wi;
    282 	struct vnode *vn;
    283 	char diskname[16];
    284 	int i, error;
    285 
    286 	if ((vn = opendisk(dv)) == NULL)
    287 		return -1;
    288 
    289 	wl.dkwl_bufsize = sizeof(*wi) * 16;
    290 	wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK);
    291 
    292 	error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED);
    293 	VOP_CLOSE(vn, FREAD, NOCRED);
    294 	vput(vn);
    295 	if (error) {
    296 #ifdef DEBUG_WEDGE
    297 		printf("%s: List wedges returned %d\n",
    298 		    device_xname(dv), error);
    299 #endif
    300 		free(wi, M_TEMP);
    301 		return -1;
    302 	}
    303 
    304 #ifdef DEBUG_WEDGE
    305 	printf("%s: Returned %u(%u) wedges\n", device_xname(dv),
    306 	    wl.dkwl_nwedges, wl.dkwl_ncopied);
    307 #endif
    308 	snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv),
    309 	    par + 'a');
    310 
    311 	for (i = 0; i < wl.dkwl_ncopied; i++) {
    312 #ifdef DEBUG_WEDGE
    313 		printf("%s: Looking for %s in %s\n",
    314 		    device_xname(dv), diskname, wi[i].dkw_wname);
    315 #endif
    316 		if (strcmp(wi[i].dkw_wname, diskname) == 0)
    317 			break;
    318 	}
    319 
    320 	if (i == wl.dkwl_ncopied) {
    321 #ifdef DEBUG_WEDGE
    322 		printf("%s: Cannot find wedge with parent %s\n",
    323 		    device_xname(dv), diskname);
    324 #endif
    325 		free(wi, M_TEMP);
    326 		return -1;
    327 	}
    328 
    329 #ifdef DEBUG_WEDGE
    330 	printf("%s: Setting boot wedge %s (%s) at %llu %llu\n",
    331 		device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname,
    332 		(unsigned long long)wi[i].dkw_offset,
    333 		(unsigned long long)wi[i].dkw_size);
    334 #endif
    335 	dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size);
    336 	free(wi, M_TEMP);
    337 	return 0;
    338 }
    339 
    340 /*
    341  * Initialize the autoconfiguration data structures.  Normally this
    342  * is done by configure(), but some platforms need to do this very
    343  * early (to e.g. initialize the console).
    344  */
    345 void
    346 config_init(void)
    347 {
    348 	const struct cfattachinit *cfai;
    349 	int i, j;
    350 
    351 	if (config_initialized)
    352 		return;
    353 
    354 	mutex_init(&alldevs_mtx, MUTEX_DEFAULT, IPL_NONE);
    355 	cv_init(&alldevs_cv, "alldevs");
    356 
    357 	/* allcfdrivers is statically initialized. */
    358 	for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
    359 		if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
    360 			panic("configure: duplicate `%s' drivers",
    361 			    cfdriver_list_initial[i]->cd_name);
    362 	}
    363 
    364 	for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
    365 		for (j = 0; cfai->cfai_list[j] != NULL; j++) {
    366 			if (config_cfattach_attach(cfai->cfai_name,
    367 						   cfai->cfai_list[j]) != 0)
    368 				panic("configure: duplicate `%s' attachment "
    369 				    "of `%s' driver",
    370 				    cfai->cfai_list[j]->ca_name,
    371 				    cfai->cfai_name);
    372 		}
    373 	}
    374 
    375 	initcftable.ct_cfdata = cfdata;
    376 	TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
    377 
    378 	config_initialized = 1;
    379 }
    380 
    381 void
    382 config_deferred(device_t dev)
    383 {
    384 	config_process_deferred(&deferred_config_queue, dev);
    385 	config_process_deferred(&interrupt_config_queue, dev);
    386 }
    387 
    388 static void
    389 config_interrupts_thread(void *cookie)
    390 {
    391 	struct deferred_config *dc;
    392 
    393 	while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) {
    394 		TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue);
    395 		(*dc->dc_func)(dc->dc_dev);
    396 		free(dc, M_DEVBUF);
    397 		config_pending_decr();
    398 	}
    399 	kthread_exit(0);
    400 }
    401 
    402 /*
    403  * Configure the system's hardware.
    404  */
    405 void
    406 configure(void)
    407 {
    408 	extern void ssp_init(void);
    409 	int i;
    410 
    411 	/* Initialize data structures. */
    412 	config_init();
    413 	pmf_init();
    414 
    415 #ifdef USERCONF
    416 	if (boothowto & RB_USERCONF)
    417 		user_config();
    418 #endif
    419 
    420 	if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
    421 		config_do_twiddle = 1;
    422 		printf_nolog("Detecting hardware...");
    423 	}
    424 
    425 	/*
    426 	 * Do the machine-dependent portion of autoconfiguration.  This
    427 	 * sets the configuration machinery here in motion by "finding"
    428 	 * the root bus.  When this function returns, we expect interrupts
    429 	 * to be enabled.
    430 	 */
    431 	cpu_configure();
    432 
    433 	/* Initialize SSP. */
    434 	ssp_init();
    435 
    436 	/* Initialize callouts, part 2. */
    437 	callout_startup2();
    438 
    439 	/*
    440 	 * Now that we've found all the hardware, start the real time
    441 	 * and statistics clocks.
    442 	 */
    443 	initclocks();
    444 
    445 	cold = 0;	/* clocks are running, we're warm now! */
    446 
    447 	/* Boot the secondary processors. */
    448 	mp_online = true;
    449 #if defined(MULTIPROCESSOR)
    450 	cpu_boot_secondary_processors();
    451 #endif
    452 
    453 	/* Setup the scheduler. */
    454 	sched_init();
    455 
    456 	/*
    457 	 * Create threads to call back and finish configuration for
    458 	 * devices that want interrupts enabled.
    459 	 */
    460 	for (i = 0; i < interrupt_config_threads; i++) {
    461 		(void)kthread_create(PRI_NONE, 0, NULL,
    462 		    config_interrupts_thread, NULL, NULL, "config");
    463 	}
    464 
    465 	/* Get the threads going and into any sleeps before continuing. */
    466 	yield();
    467 
    468 	/* Lock the kernel on behalf of lwp0. */
    469 	KERNEL_LOCK(1, NULL);
    470 }
    471 
    472 /*
    473  * Add a cfdriver to the system.
    474  */
    475 int
    476 config_cfdriver_attach(struct cfdriver *cd)
    477 {
    478 	struct cfdriver *lcd;
    479 
    480 	/* Make sure this driver isn't already in the system. */
    481 	LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
    482 		if (STREQ(lcd->cd_name, cd->cd_name))
    483 			return (EEXIST);
    484 	}
    485 
    486 	LIST_INIT(&cd->cd_attach);
    487 	LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
    488 
    489 	return (0);
    490 }
    491 
    492 /*
    493  * Remove a cfdriver from the system.
    494  */
    495 int
    496 config_cfdriver_detach(struct cfdriver *cd)
    497 {
    498 	int i;
    499 
    500 	/* Make sure there are no active instances. */
    501 	for (i = 0; i < cd->cd_ndevs; i++) {
    502 		if (cd->cd_devs[i] != NULL)
    503 			return (EBUSY);
    504 	}
    505 
    506 	/* ...and no attachments loaded. */
    507 	if (LIST_EMPTY(&cd->cd_attach) == 0)
    508 		return (EBUSY);
    509 
    510 	LIST_REMOVE(cd, cd_list);
    511 
    512 	KASSERT(cd->cd_devs == NULL);
    513 
    514 	return (0);
    515 }
    516 
    517 /*
    518  * Look up a cfdriver by name.
    519  */
    520 struct cfdriver *
    521 config_cfdriver_lookup(const char *name)
    522 {
    523 	struct cfdriver *cd;
    524 
    525 	LIST_FOREACH(cd, &allcfdrivers, cd_list) {
    526 		if (STREQ(cd->cd_name, name))
    527 			return (cd);
    528 	}
    529 
    530 	return (NULL);
    531 }
    532 
    533 /*
    534  * Add a cfattach to the specified driver.
    535  */
    536 int
    537 config_cfattach_attach(const char *driver, struct cfattach *ca)
    538 {
    539 	struct cfattach *lca;
    540 	struct cfdriver *cd;
    541 
    542 	cd = config_cfdriver_lookup(driver);
    543 	if (cd == NULL)
    544 		return (ESRCH);
    545 
    546 	/* Make sure this attachment isn't already on this driver. */
    547 	LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
    548 		if (STREQ(lca->ca_name, ca->ca_name))
    549 			return (EEXIST);
    550 	}
    551 
    552 	LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
    553 
    554 	return (0);
    555 }
    556 
    557 /*
    558  * Remove a cfattach from the specified driver.
    559  */
    560 int
    561 config_cfattach_detach(const char *driver, struct cfattach *ca)
    562 {
    563 	struct cfdriver *cd;
    564 	device_t dev;
    565 	int i;
    566 
    567 	cd = config_cfdriver_lookup(driver);
    568 	if (cd == NULL)
    569 		return (ESRCH);
    570 
    571 	/* Make sure there are no active instances. */
    572 	for (i = 0; i < cd->cd_ndevs; i++) {
    573 		if ((dev = cd->cd_devs[i]) == NULL)
    574 			continue;
    575 		if (dev->dv_cfattach == ca)
    576 			return (EBUSY);
    577 	}
    578 
    579 	LIST_REMOVE(ca, ca_list);
    580 
    581 	return (0);
    582 }
    583 
    584 /*
    585  * Look up a cfattach by name.
    586  */
    587 static struct cfattach *
    588 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
    589 {
    590 	struct cfattach *ca;
    591 
    592 	LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
    593 		if (STREQ(ca->ca_name, atname))
    594 			return (ca);
    595 	}
    596 
    597 	return (NULL);
    598 }
    599 
    600 /*
    601  * Look up a cfattach by driver/attachment name.
    602  */
    603 struct cfattach *
    604 config_cfattach_lookup(const char *name, const char *atname)
    605 {
    606 	struct cfdriver *cd;
    607 
    608 	cd = config_cfdriver_lookup(name);
    609 	if (cd == NULL)
    610 		return (NULL);
    611 
    612 	return (config_cfattach_lookup_cd(cd, atname));
    613 }
    614 
    615 /*
    616  * Apply the matching function and choose the best.  This is used
    617  * a few times and we want to keep the code small.
    618  */
    619 static void
    620 mapply(struct matchinfo *m, cfdata_t cf)
    621 {
    622 	int pri;
    623 
    624 	if (m->fn != NULL) {
    625 		pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
    626 	} else {
    627 		pri = config_match(m->parent, cf, m->aux);
    628 	}
    629 	if (pri > m->pri) {
    630 		m->match = cf;
    631 		m->pri = pri;
    632 	}
    633 }
    634 
    635 int
    636 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    637 {
    638 	const struct cfiattrdata *ci;
    639 	const struct cflocdesc *cl;
    640 	int nlocs, i;
    641 
    642 	ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
    643 	KASSERT(ci);
    644 	nlocs = ci->ci_loclen;
    645 	for (i = 0; i < nlocs; i++) {
    646 		cl = &ci->ci_locdesc[i];
    647 		/* !cld_defaultstr means no default value */
    648 		if ((!(cl->cld_defaultstr)
    649 		     || (cf->cf_loc[i] != cl->cld_default))
    650 		    && cf->cf_loc[i] != locs[i])
    651 			return (0);
    652 	}
    653 
    654 	return (config_match(parent, cf, aux));
    655 }
    656 
    657 /*
    658  * Helper function: check whether the driver supports the interface attribute
    659  * and return its descriptor structure.
    660  */
    661 static const struct cfiattrdata *
    662 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
    663 {
    664 	const struct cfiattrdata * const *cpp;
    665 
    666 	if (cd->cd_attrs == NULL)
    667 		return (0);
    668 
    669 	for (cpp = cd->cd_attrs; *cpp; cpp++) {
    670 		if (STREQ((*cpp)->ci_name, ia)) {
    671 			/* Match. */
    672 			return (*cpp);
    673 		}
    674 	}
    675 	return (0);
    676 }
    677 
    678 /*
    679  * Lookup an interface attribute description by name.
    680  * If the driver is given, consider only its supported attributes.
    681  */
    682 const struct cfiattrdata *
    683 cfiattr_lookup(const char *name, const struct cfdriver *cd)
    684 {
    685 	const struct cfdriver *d;
    686 	const struct cfiattrdata *ia;
    687 
    688 	if (cd)
    689 		return (cfdriver_get_iattr(cd, name));
    690 
    691 	LIST_FOREACH(d, &allcfdrivers, cd_list) {
    692 		ia = cfdriver_get_iattr(d, name);
    693 		if (ia)
    694 			return (ia);
    695 	}
    696 	return (0);
    697 }
    698 
    699 /*
    700  * Determine if `parent' is a potential parent for a device spec based
    701  * on `cfp'.
    702  */
    703 static int
    704 cfparent_match(const device_t parent, const struct cfparent *cfp)
    705 {
    706 	struct cfdriver *pcd;
    707 
    708 	/* We don't match root nodes here. */
    709 	if (cfp == NULL)
    710 		return (0);
    711 
    712 	pcd = parent->dv_cfdriver;
    713 	KASSERT(pcd != NULL);
    714 
    715 	/*
    716 	 * First, ensure this parent has the correct interface
    717 	 * attribute.
    718 	 */
    719 	if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
    720 		return (0);
    721 
    722 	/*
    723 	 * If no specific parent device instance was specified (i.e.
    724 	 * we're attaching to the attribute only), we're done!
    725 	 */
    726 	if (cfp->cfp_parent == NULL)
    727 		return (1);
    728 
    729 	/*
    730 	 * Check the parent device's name.
    731 	 */
    732 	if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
    733 		return (0);	/* not the same parent */
    734 
    735 	/*
    736 	 * Make sure the unit number matches.
    737 	 */
    738 	if (cfp->cfp_unit == DVUNIT_ANY ||	/* wildcard */
    739 	    cfp->cfp_unit == parent->dv_unit)
    740 		return (1);
    741 
    742 	/* Unit numbers don't match. */
    743 	return (0);
    744 }
    745 
    746 /*
    747  * Helper for config_cfdata_attach(): check all devices whether it could be
    748  * parent any attachment in the config data table passed, and rescan.
    749  */
    750 static void
    751 rescan_with_cfdata(const struct cfdata *cf)
    752 {
    753 	device_t d;
    754 	const struct cfdata *cf1;
    755 	deviter_t di;
    756 
    757 
    758 	/*
    759 	 * "alldevs" is likely longer than an LKM's cfdata, so make it
    760 	 * the outer loop.
    761 	 */
    762 	for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) {
    763 
    764 		if (!(d->dv_cfattach->ca_rescan))
    765 			continue;
    766 
    767 		for (cf1 = cf; cf1->cf_name; cf1++) {
    768 
    769 			if (!cfparent_match(d, cf1->cf_pspec))
    770 				continue;
    771 
    772 			(*d->dv_cfattach->ca_rescan)(d,
    773 				cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
    774 		}
    775 	}
    776 	deviter_release(&di);
    777 }
    778 
    779 /*
    780  * Attach a supplemental config data table and rescan potential
    781  * parent devices if required.
    782  */
    783 int
    784 config_cfdata_attach(cfdata_t cf, int scannow)
    785 {
    786 	struct cftable *ct;
    787 
    788 	ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
    789 	ct->ct_cfdata = cf;
    790 	TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
    791 
    792 	if (scannow)
    793 		rescan_with_cfdata(cf);
    794 
    795 	return (0);
    796 }
    797 
    798 /*
    799  * Helper for config_cfdata_detach: check whether a device is
    800  * found through any attachment in the config data table.
    801  */
    802 static int
    803 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
    804 {
    805 	const struct cfdata *cf1;
    806 
    807 	for (cf1 = cf; cf1->cf_name; cf1++)
    808 		if (d->dv_cfdata == cf1)
    809 			return (1);
    810 
    811 	return (0);
    812 }
    813 
    814 /*
    815  * Detach a supplemental config data table. Detach all devices found
    816  * through that table (and thus keeping references to it) before.
    817  */
    818 int
    819 config_cfdata_detach(cfdata_t cf)
    820 {
    821 	device_t d;
    822 	int error = 0;
    823 	struct cftable *ct;
    824 	deviter_t di;
    825 
    826 	for (d = deviter_first(&di, DEVITER_F_RW); d != NULL;
    827 	     d = deviter_next(&di)) {
    828 		if (!dev_in_cfdata(d, cf))
    829 			continue;
    830 		if ((error = config_detach(d, 0)) != 0)
    831 			break;
    832 	}
    833 	deviter_release(&di);
    834 	if (error) {
    835 		aprint_error_dev(d, "unable to detach instance\n");
    836 		return error;
    837 	}
    838 
    839 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    840 		if (ct->ct_cfdata == cf) {
    841 			TAILQ_REMOVE(&allcftables, ct, ct_list);
    842 			free(ct, M_DEVBUF);
    843 			return (0);
    844 		}
    845 	}
    846 
    847 	/* not found -- shouldn't happen */
    848 	return (EINVAL);
    849 }
    850 
    851 /*
    852  * Invoke the "match" routine for a cfdata entry on behalf of
    853  * an external caller, usually a "submatch" routine.
    854  */
    855 int
    856 config_match(device_t parent, cfdata_t cf, void *aux)
    857 {
    858 	struct cfattach *ca;
    859 
    860 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    861 	if (ca == NULL) {
    862 		/* No attachment for this entry, oh well. */
    863 		return (0);
    864 	}
    865 
    866 	return ((*ca->ca_match)(parent, cf, aux));
    867 }
    868 
    869 /*
    870  * Iterate over all potential children of some device, calling the given
    871  * function (default being the child's match function) for each one.
    872  * Nonzero returns are matches; the highest value returned is considered
    873  * the best match.  Return the `found child' if we got a match, or NULL
    874  * otherwise.  The `aux' pointer is simply passed on through.
    875  *
    876  * Note that this function is designed so that it can be used to apply
    877  * an arbitrary function to all potential children (its return value
    878  * can be ignored).
    879  */
    880 cfdata_t
    881 config_search_loc(cfsubmatch_t fn, device_t parent,
    882 		  const char *ifattr, const int *locs, void *aux)
    883 {
    884 	struct cftable *ct;
    885 	cfdata_t cf;
    886 	struct matchinfo m;
    887 
    888 	KASSERT(config_initialized);
    889 	KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
    890 
    891 	m.fn = fn;
    892 	m.parent = parent;
    893 	m.locs = locs;
    894 	m.aux = aux;
    895 	m.match = NULL;
    896 	m.pri = 0;
    897 
    898 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    899 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    900 
    901 			/* We don't match root nodes here. */
    902 			if (!cf->cf_pspec)
    903 				continue;
    904 
    905 			/*
    906 			 * Skip cf if no longer eligible, otherwise scan
    907 			 * through parents for one matching `parent', and
    908 			 * try match function.
    909 			 */
    910 			if (cf->cf_fstate == FSTATE_FOUND)
    911 				continue;
    912 			if (cf->cf_fstate == FSTATE_DNOTFOUND ||
    913 			    cf->cf_fstate == FSTATE_DSTAR)
    914 				continue;
    915 
    916 			/*
    917 			 * If an interface attribute was specified,
    918 			 * consider only children which attach to
    919 			 * that attribute.
    920 			 */
    921 			if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
    922 				continue;
    923 
    924 			if (cfparent_match(parent, cf->cf_pspec))
    925 				mapply(&m, cf);
    926 		}
    927 	}
    928 	return (m.match);
    929 }
    930 
    931 cfdata_t
    932 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
    933     void *aux)
    934 {
    935 
    936 	return (config_search_loc(fn, parent, ifattr, NULL, aux));
    937 }
    938 
    939 /*
    940  * Find the given root device.
    941  * This is much like config_search, but there is no parent.
    942  * Don't bother with multiple cfdata tables; the root node
    943  * must always be in the initial table.
    944  */
    945 cfdata_t
    946 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
    947 {
    948 	cfdata_t cf;
    949 	const short *p;
    950 	struct matchinfo m;
    951 
    952 	m.fn = fn;
    953 	m.parent = ROOT;
    954 	m.aux = aux;
    955 	m.match = NULL;
    956 	m.pri = 0;
    957 	m.locs = 0;
    958 	/*
    959 	 * Look at root entries for matching name.  We do not bother
    960 	 * with found-state here since only one root should ever be
    961 	 * searched (and it must be done first).
    962 	 */
    963 	for (p = cfroots; *p >= 0; p++) {
    964 		cf = &cfdata[*p];
    965 		if (strcmp(cf->cf_name, rootname) == 0)
    966 			mapply(&m, cf);
    967 	}
    968 	return (m.match);
    969 }
    970 
    971 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
    972 
    973 /*
    974  * The given `aux' argument describes a device that has been found
    975  * on the given parent, but not necessarily configured.  Locate the
    976  * configuration data for that device (using the submatch function
    977  * provided, or using candidates' cd_match configuration driver
    978  * functions) and attach it, and return true.  If the device was
    979  * not configured, call the given `print' function and return 0.
    980  */
    981 device_t
    982 config_found_sm_loc(device_t parent,
    983 		const char *ifattr, const int *locs, void *aux,
    984 		cfprint_t print, cfsubmatch_t submatch)
    985 {
    986 	cfdata_t cf;
    987 
    988 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    989 	if (splash_progress_state)
    990 		splash_progress_update(splash_progress_state);
    991 #endif
    992 
    993 	if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
    994 		return(config_attach_loc(parent, cf, locs, aux, print));
    995 	if (print) {
    996 		if (config_do_twiddle)
    997 			twiddle();
    998 		aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]);
    999 	}
   1000 
   1001 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1002 	if (splash_progress_state)
   1003 		splash_progress_update(splash_progress_state);
   1004 #endif
   1005 
   1006 	return (NULL);
   1007 }
   1008 
   1009 device_t
   1010 config_found_ia(device_t parent, const char *ifattr, void *aux,
   1011     cfprint_t print)
   1012 {
   1013 
   1014 	return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL));
   1015 }
   1016 
   1017 device_t
   1018 config_found(device_t parent, void *aux, cfprint_t print)
   1019 {
   1020 
   1021 	return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL));
   1022 }
   1023 
   1024 /*
   1025  * As above, but for root devices.
   1026  */
   1027 device_t
   1028 config_rootfound(const char *rootname, void *aux)
   1029 {
   1030 	cfdata_t cf;
   1031 
   1032 	if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
   1033 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
   1034 	aprint_error("root device %s not configured\n", rootname);
   1035 	return (NULL);
   1036 }
   1037 
   1038 /* just like sprintf(buf, "%d") except that it works from the end */
   1039 static char *
   1040 number(char *ep, int n)
   1041 {
   1042 
   1043 	*--ep = 0;
   1044 	while (n >= 10) {
   1045 		*--ep = (n % 10) + '0';
   1046 		n /= 10;
   1047 	}
   1048 	*--ep = n + '0';
   1049 	return (ep);
   1050 }
   1051 
   1052 /*
   1053  * Expand the size of the cd_devs array if necessary.
   1054  */
   1055 static void
   1056 config_makeroom(int n, struct cfdriver *cd)
   1057 {
   1058 	int old, new;
   1059 	void **nsp;
   1060 
   1061 	if (n < cd->cd_ndevs)
   1062 		return;
   1063 
   1064 	/*
   1065 	 * Need to expand the array.
   1066 	 */
   1067 	old = cd->cd_ndevs;
   1068 	if (old == 0)
   1069 		new = 4;
   1070 	else
   1071 		new = old * 2;
   1072 	while (new <= n)
   1073 		new *= 2;
   1074 	cd->cd_ndevs = new;
   1075 	nsp = malloc(new * sizeof(void *), M_DEVBUF,
   1076 	    cold ? M_NOWAIT : M_WAITOK);
   1077 	if (nsp == NULL)
   1078 		panic("config_attach: %sing dev array",
   1079 		    old != 0 ? "expand" : "creat");
   1080 	memset(nsp + old, 0, (new - old) * sizeof(void *));
   1081 	if (old != 0) {
   1082 		memcpy(nsp, cd->cd_devs, old * sizeof(void *));
   1083 		free(cd->cd_devs, M_DEVBUF);
   1084 	}
   1085 	cd->cd_devs = nsp;
   1086 }
   1087 
   1088 static void
   1089 config_devlink(device_t dev)
   1090 {
   1091 	struct cfdriver *cd = dev->dv_cfdriver;
   1092 
   1093 	/* put this device in the devices array */
   1094 	config_makeroom(dev->dv_unit, cd);
   1095 	if (cd->cd_devs[dev->dv_unit])
   1096 		panic("config_attach: duplicate %s", device_xname(dev));
   1097 	cd->cd_devs[dev->dv_unit] = dev;
   1098 
   1099 	/* It is safe to add a device to the tail of the list while
   1100 	 * readers are in the list, but not while a writer is in
   1101 	 * the list.  Wait for any writer to complete.
   1102 	 */
   1103 	mutex_enter(&alldevs_mtx);
   1104 	while (alldevs_nwrite != 0 && alldevs_writer != curlwp)
   1105 		cv_wait(&alldevs_cv, &alldevs_mtx);
   1106 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
   1107 	cv_signal(&alldevs_cv);
   1108 	mutex_exit(&alldevs_mtx);
   1109 }
   1110 
   1111 static void
   1112 config_devunlink(device_t dev)
   1113 {
   1114 	struct cfdriver *cd = dev->dv_cfdriver;
   1115 	int i;
   1116 
   1117 	/* Unlink from device list. */
   1118 	TAILQ_REMOVE(&alldevs, dev, dv_list);
   1119 
   1120 	/* Remove from cfdriver's array. */
   1121 	cd->cd_devs[dev->dv_unit] = NULL;
   1122 
   1123 	/*
   1124 	 * If the device now has no units in use, deallocate its softc array.
   1125 	 */
   1126 	for (i = 0; i < cd->cd_ndevs; i++)
   1127 		if (cd->cd_devs[i] != NULL)
   1128 			break;
   1129 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
   1130 		free(cd->cd_devs, M_DEVBUF);
   1131 		cd->cd_devs = NULL;
   1132 		cd->cd_ndevs = 0;
   1133 	}
   1134 }
   1135 
   1136 static device_t
   1137 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
   1138 {
   1139 	struct cfdriver *cd;
   1140 	struct cfattach *ca;
   1141 	size_t lname, lunit;
   1142 	const char *xunit;
   1143 	int myunit;
   1144 	char num[10];
   1145 	device_t dev;
   1146 	void *dev_private;
   1147 	const struct cfiattrdata *ia;
   1148 
   1149 	cd = config_cfdriver_lookup(cf->cf_name);
   1150 	if (cd == NULL)
   1151 		return (NULL);
   1152 
   1153 	ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
   1154 	if (ca == NULL)
   1155 		return (NULL);
   1156 
   1157 	if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 &&
   1158 	    ca->ca_devsize < sizeof(struct device))
   1159 		panic("config_devalloc: %s", cf->cf_atname);
   1160 
   1161 #ifndef __BROKEN_CONFIG_UNIT_USAGE
   1162 	if (cf->cf_fstate == FSTATE_STAR) {
   1163 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
   1164 			if (cd->cd_devs[myunit] == NULL)
   1165 				break;
   1166 		/*
   1167 		 * myunit is now the unit of the first NULL device pointer,
   1168 		 * or max(cd->cd_ndevs,cf->cf_unit).
   1169 		 */
   1170 	} else {
   1171 		myunit = cf->cf_unit;
   1172 		if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
   1173 			return (NULL);
   1174 	}
   1175 #else
   1176 	myunit = cf->cf_unit;
   1177 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
   1178 
   1179 	/* compute length of name and decimal expansion of unit number */
   1180 	lname = strlen(cd->cd_name);
   1181 	xunit = number(&num[sizeof(num)], myunit);
   1182 	lunit = &num[sizeof(num)] - xunit;
   1183 	if (lname + lunit > sizeof(dev->dv_xname))
   1184 		panic("config_devalloc: device name too long");
   1185 
   1186 	/* get memory for all device vars */
   1187 	KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device));
   1188 	if (ca->ca_devsize > 0) {
   1189 		dev_private = malloc(ca->ca_devsize, M_DEVBUF,
   1190 				     M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
   1191 		if (dev_private == NULL)
   1192 			panic("config_devalloc: memory allocation for device softc failed");
   1193 	} else {
   1194 		KASSERT(ca->ca_flags & DVF_PRIV_ALLOC);
   1195 		dev_private = NULL;
   1196 	}
   1197 
   1198 	if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
   1199 		dev = malloc(sizeof(struct device), M_DEVBUF,
   1200 			     M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
   1201 	} else {
   1202 		dev = dev_private;
   1203 	}
   1204 	if (dev == NULL)
   1205 		panic("config_devalloc: memory allocation for device_t failed");
   1206 
   1207 	dev->dv_class = cd->cd_class;
   1208 	dev->dv_cfdata = cf;
   1209 	dev->dv_cfdriver = cd;
   1210 	dev->dv_cfattach = ca;
   1211 	dev->dv_unit = myunit;
   1212 	dev->dv_activity_count = 0;
   1213 	dev->dv_activity_handlers = NULL;
   1214 	dev->dv_private = dev_private;
   1215 	memcpy(dev->dv_xname, cd->cd_name, lname);
   1216 	memcpy(dev->dv_xname + lname, xunit, lunit);
   1217 	dev->dv_parent = parent;
   1218 	if (parent != NULL)
   1219 		dev->dv_depth = parent->dv_depth + 1;
   1220 	else
   1221 		dev->dv_depth = 0;
   1222 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
   1223 	dev->dv_flags |= ca->ca_flags;	/* inherit flags from class */
   1224 	if (locs) {
   1225 		KASSERT(parent); /* no locators at root */
   1226 		ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
   1227 				    parent->dv_cfdriver);
   1228 		dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
   1229 					  M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1230 		memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
   1231 	}
   1232 	dev->dv_properties = prop_dictionary_create();
   1233 	KASSERT(dev->dv_properties != NULL);
   1234 
   1235 	return (dev);
   1236 }
   1237 
   1238 static void
   1239 config_devdealloc(device_t dev)
   1240 {
   1241 
   1242 	KASSERT(dev->dv_properties != NULL);
   1243 	prop_object_release(dev->dv_properties);
   1244 
   1245 	if (dev->dv_activity_handlers)
   1246 		panic("config_devdealloc with registered handlers");
   1247 
   1248 	if (dev->dv_locators)
   1249 		free(dev->dv_locators, M_DEVBUF);
   1250 
   1251 	if ((dev->dv_flags & DVF_PRIV_ALLOC) != 0 && dev->dv_private != NULL)
   1252 		free(dev->dv_private, M_DEVBUF);
   1253 
   1254 	free(dev, M_DEVBUF);
   1255 }
   1256 
   1257 /*
   1258  * Attach a found device.
   1259  */
   1260 device_t
   1261 config_attach_loc(device_t parent, cfdata_t cf,
   1262 	const int *locs, void *aux, cfprint_t print)
   1263 {
   1264 	device_t dev;
   1265 	struct cftable *ct;
   1266 	const char *drvname;
   1267 
   1268 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1269 	if (splash_progress_state)
   1270 		splash_progress_update(splash_progress_state);
   1271 #endif
   1272 
   1273 	dev = config_devalloc(parent, cf, locs);
   1274 	if (!dev)
   1275 		panic("config_attach: allocation of device softc failed");
   1276 
   1277 	/* XXX redundant - see below? */
   1278 	if (cf->cf_fstate != FSTATE_STAR) {
   1279 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
   1280 		cf->cf_fstate = FSTATE_FOUND;
   1281 	}
   1282 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1283 	  else
   1284 		cf->cf_unit++;
   1285 #endif
   1286 
   1287 	config_devlink(dev);
   1288 
   1289 	if (config_do_twiddle)
   1290 		twiddle();
   1291 	else
   1292 		aprint_naive("Found ");
   1293 	/*
   1294 	 * We want the next two printfs for normal, verbose, and quiet,
   1295 	 * but not silent (in which case, we're twiddling, instead).
   1296 	 */
   1297 	if (parent == ROOT) {
   1298 		aprint_naive("%s (root)", device_xname(dev));
   1299 		aprint_normal("%s (root)", device_xname(dev));
   1300 	} else {
   1301 		aprint_naive("%s at %s", device_xname(dev), device_xname(parent));
   1302 		aprint_normal("%s at %s", device_xname(dev), device_xname(parent));
   1303 		if (print)
   1304 			(void) (*print)(aux, NULL);
   1305 	}
   1306 
   1307 	/*
   1308 	 * Before attaching, clobber any unfound devices that are
   1309 	 * otherwise identical.
   1310 	 * XXX code above is redundant?
   1311 	 */
   1312 	drvname = dev->dv_cfdriver->cd_name;
   1313 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1314 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1315 			if (STREQ(cf->cf_name, drvname) &&
   1316 			    cf->cf_unit == dev->dv_unit) {
   1317 				if (cf->cf_fstate == FSTATE_NOTFOUND)
   1318 					cf->cf_fstate = FSTATE_FOUND;
   1319 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1320 				/*
   1321 				 * Bump the unit number on all starred cfdata
   1322 				 * entries for this device.
   1323 				 */
   1324 				if (cf->cf_fstate == FSTATE_STAR)
   1325 					cf->cf_unit++;
   1326 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1327 			}
   1328 		}
   1329 	}
   1330 #ifdef __HAVE_DEVICE_REGISTER
   1331 	device_register(dev, aux);
   1332 #endif
   1333 
   1334 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1335 	if (splash_progress_state)
   1336 		splash_progress_update(splash_progress_state);
   1337 #endif
   1338 	(*dev->dv_cfattach->ca_attach)(parent, dev, aux);
   1339 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1340 	if (splash_progress_state)
   1341 		splash_progress_update(splash_progress_state);
   1342 #endif
   1343 
   1344 	if (!device_pmf_is_registered(dev))
   1345 		aprint_debug_dev(dev, "WARNING: power management not supported\n");
   1346 
   1347 	config_process_deferred(&deferred_config_queue, dev);
   1348 	return (dev);
   1349 }
   1350 
   1351 device_t
   1352 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
   1353 {
   1354 
   1355 	return (config_attach_loc(parent, cf, NULL, aux, print));
   1356 }
   1357 
   1358 /*
   1359  * As above, but for pseudo-devices.  Pseudo-devices attached in this
   1360  * way are silently inserted into the device tree, and their children
   1361  * attached.
   1362  *
   1363  * Note that because pseudo-devices are attached silently, any information
   1364  * the attach routine wishes to print should be prefixed with the device
   1365  * name by the attach routine.
   1366  */
   1367 device_t
   1368 config_attach_pseudo(cfdata_t cf)
   1369 {
   1370 	device_t dev;
   1371 
   1372 	dev = config_devalloc(ROOT, cf, NULL);
   1373 	if (!dev)
   1374 		return (NULL);
   1375 
   1376 	/* XXX mark busy in cfdata */
   1377 
   1378 	config_devlink(dev);
   1379 
   1380 #if 0	/* XXXJRT not yet */
   1381 #ifdef __HAVE_DEVICE_REGISTER
   1382 	device_register(dev, NULL);	/* like a root node */
   1383 #endif
   1384 #endif
   1385 	(*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
   1386 	config_process_deferred(&deferred_config_queue, dev);
   1387 	return (dev);
   1388 }
   1389 
   1390 /*
   1391  * Detach a device.  Optionally forced (e.g. because of hardware
   1392  * removal) and quiet.  Returns zero if successful, non-zero
   1393  * (an error code) otherwise.
   1394  *
   1395  * Note that this code wants to be run from a process context, so
   1396  * that the detach can sleep to allow processes which have a device
   1397  * open to run and unwind their stacks.
   1398  */
   1399 int
   1400 config_detach(device_t dev, int flags)
   1401 {
   1402 	struct cftable *ct;
   1403 	cfdata_t cf;
   1404 	const struct cfattach *ca;
   1405 	struct cfdriver *cd;
   1406 #ifdef DIAGNOSTIC
   1407 	device_t d;
   1408 #endif
   1409 	int rv = 0;
   1410 
   1411 #ifdef DIAGNOSTIC
   1412 	if (dev->dv_cfdata != NULL &&
   1413 	    dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
   1414 	    dev->dv_cfdata->cf_fstate != FSTATE_STAR)
   1415 		panic("config_detach: bad device fstate");
   1416 #endif
   1417 	cd = dev->dv_cfdriver;
   1418 	KASSERT(cd != NULL);
   1419 
   1420 	ca = dev->dv_cfattach;
   1421 	KASSERT(ca != NULL);
   1422 
   1423 	KASSERT(curlwp != NULL);
   1424 	mutex_enter(&alldevs_mtx);
   1425 	if (alldevs_nwrite > 0 && alldevs_writer == NULL)
   1426 		;
   1427 	else while (alldevs_nread != 0 ||
   1428 	       (alldevs_nwrite != 0 && alldevs_writer != curlwp))
   1429 		cv_wait(&alldevs_cv, &alldevs_mtx);
   1430 	if (alldevs_nwrite++ == 0)
   1431 		alldevs_writer = curlwp;
   1432 	mutex_exit(&alldevs_mtx);
   1433 
   1434 	/*
   1435 	 * Ensure the device is deactivated.  If the device doesn't
   1436 	 * have an activation entry point, we allow DVF_ACTIVE to
   1437 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
   1438 	 * device is busy, and the detach fails.
   1439 	 */
   1440 	if (ca->ca_activate != NULL)
   1441 		rv = config_deactivate(dev);
   1442 
   1443 	/*
   1444 	 * Try to detach the device.  If that's not possible, then
   1445 	 * we either panic() (for the forced but failed case), or
   1446 	 * return an error.
   1447 	 */
   1448 	if (rv == 0) {
   1449 		if (ca->ca_detach != NULL)
   1450 			rv = (*ca->ca_detach)(dev, flags);
   1451 		else
   1452 			rv = EOPNOTSUPP;
   1453 	}
   1454 	if (rv != 0) {
   1455 		if ((flags & DETACH_FORCE) == 0)
   1456 			goto out;
   1457 		else
   1458 			panic("config_detach: forced detach of %s failed (%d)",
   1459 			    device_xname(dev), rv);
   1460 	}
   1461 
   1462 	/*
   1463 	 * The device has now been successfully detached.
   1464 	 */
   1465 
   1466 #ifdef DIAGNOSTIC
   1467 	/*
   1468 	 * Sanity: If you're successfully detached, you should have no
   1469 	 * children.  (Note that because children must be attached
   1470 	 * after parents, we only need to search the latter part of
   1471 	 * the list.)
   1472 	 */
   1473 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
   1474 	    d = TAILQ_NEXT(d, dv_list)) {
   1475 		if (d->dv_parent == dev) {
   1476 			printf("config_detach: detached device %s"
   1477 			    " has children %s\n", device_xname(dev), device_xname(d));
   1478 			panic("config_detach");
   1479 		}
   1480 	}
   1481 #endif
   1482 
   1483 	/* notify the parent that the child is gone */
   1484 	if (dev->dv_parent) {
   1485 		device_t p = dev->dv_parent;
   1486 		if (p->dv_cfattach->ca_childdetached)
   1487 			(*p->dv_cfattach->ca_childdetached)(p, dev);
   1488 	}
   1489 
   1490 	/*
   1491 	 * Mark cfdata to show that the unit can be reused, if possible.
   1492 	 */
   1493 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1494 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1495 			if (STREQ(cf->cf_name, cd->cd_name)) {
   1496 				if (cf->cf_fstate == FSTATE_FOUND &&
   1497 				    cf->cf_unit == dev->dv_unit)
   1498 					cf->cf_fstate = FSTATE_NOTFOUND;
   1499 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1500 				/*
   1501 				 * Note that we can only re-use a starred
   1502 				 * unit number if the unit being detached
   1503 				 * had the last assigned unit number.
   1504 				 */
   1505 				if (cf->cf_fstate == FSTATE_STAR &&
   1506 				    cf->cf_unit == dev->dv_unit + 1)
   1507 					cf->cf_unit--;
   1508 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1509 			}
   1510 		}
   1511 	}
   1512 
   1513 	config_devunlink(dev);
   1514 
   1515 	if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
   1516 		aprint_normal_dev(dev, "detached\n");
   1517 
   1518 	config_devdealloc(dev);
   1519 
   1520 out:
   1521 	mutex_enter(&alldevs_mtx);
   1522 	if (--alldevs_nwrite == 0)
   1523 		alldevs_writer = NULL;
   1524 	cv_signal(&alldevs_cv);
   1525 	mutex_exit(&alldevs_mtx);
   1526 	return rv;
   1527 }
   1528 
   1529 int
   1530 config_detach_children(device_t parent, int flags)
   1531 {
   1532 	device_t dv;
   1533 	deviter_t di;
   1534 	int error = 0;
   1535 
   1536 	for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
   1537 	     dv = deviter_next(&di)) {
   1538 		if (device_parent(dv) != parent)
   1539 			continue;
   1540 		if ((error = config_detach(dv, flags)) != 0)
   1541 			break;
   1542 	}
   1543 	deviter_release(&di);
   1544 	return error;
   1545 }
   1546 
   1547 int
   1548 config_activate(device_t dev)
   1549 {
   1550 	const struct cfattach *ca = dev->dv_cfattach;
   1551 	int rv = 0, oflags = dev->dv_flags;
   1552 
   1553 	if (ca->ca_activate == NULL)
   1554 		return (EOPNOTSUPP);
   1555 
   1556 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
   1557 		dev->dv_flags |= DVF_ACTIVE;
   1558 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
   1559 		if (rv)
   1560 			dev->dv_flags = oflags;
   1561 	}
   1562 	return (rv);
   1563 }
   1564 
   1565 int
   1566 config_deactivate(device_t dev)
   1567 {
   1568 	const struct cfattach *ca = dev->dv_cfattach;
   1569 	int rv = 0, oflags = dev->dv_flags;
   1570 
   1571 	if (ca->ca_activate == NULL)
   1572 		return (EOPNOTSUPP);
   1573 
   1574 	if (dev->dv_flags & DVF_ACTIVE) {
   1575 		dev->dv_flags &= ~DVF_ACTIVE;
   1576 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
   1577 		if (rv)
   1578 			dev->dv_flags = oflags;
   1579 	}
   1580 	return (rv);
   1581 }
   1582 
   1583 /*
   1584  * Defer the configuration of the specified device until all
   1585  * of its parent's devices have been attached.
   1586  */
   1587 void
   1588 config_defer(device_t dev, void (*func)(device_t))
   1589 {
   1590 	struct deferred_config *dc;
   1591 
   1592 	if (dev->dv_parent == NULL)
   1593 		panic("config_defer: can't defer config of a root device");
   1594 
   1595 #ifdef DIAGNOSTIC
   1596 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
   1597 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1598 		if (dc->dc_dev == dev)
   1599 			panic("config_defer: deferred twice");
   1600 	}
   1601 #endif
   1602 
   1603 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1604 	if (dc == NULL)
   1605 		panic("config_defer: unable to allocate callback");
   1606 
   1607 	dc->dc_dev = dev;
   1608 	dc->dc_func = func;
   1609 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
   1610 	config_pending_incr();
   1611 }
   1612 
   1613 /*
   1614  * Defer some autoconfiguration for a device until after interrupts
   1615  * are enabled.
   1616  */
   1617 void
   1618 config_interrupts(device_t dev, void (*func)(device_t))
   1619 {
   1620 	struct deferred_config *dc;
   1621 
   1622 	/*
   1623 	 * If interrupts are enabled, callback now.
   1624 	 */
   1625 	if (cold == 0) {
   1626 		(*func)(dev);
   1627 		return;
   1628 	}
   1629 
   1630 #ifdef DIAGNOSTIC
   1631 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
   1632 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1633 		if (dc->dc_dev == dev)
   1634 			panic("config_interrupts: deferred twice");
   1635 	}
   1636 #endif
   1637 
   1638 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1639 	if (dc == NULL)
   1640 		panic("config_interrupts: unable to allocate callback");
   1641 
   1642 	dc->dc_dev = dev;
   1643 	dc->dc_func = func;
   1644 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
   1645 	config_pending_incr();
   1646 }
   1647 
   1648 /*
   1649  * Process a deferred configuration queue.
   1650  */
   1651 static void
   1652 config_process_deferred(struct deferred_config_head *queue,
   1653     device_t parent)
   1654 {
   1655 	struct deferred_config *dc, *ndc;
   1656 
   1657 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
   1658 		ndc = TAILQ_NEXT(dc, dc_queue);
   1659 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
   1660 			TAILQ_REMOVE(queue, dc, dc_queue);
   1661 			(*dc->dc_func)(dc->dc_dev);
   1662 			free(dc, M_DEVBUF);
   1663 			config_pending_decr();
   1664 		}
   1665 	}
   1666 }
   1667 
   1668 /*
   1669  * Manipulate the config_pending semaphore.
   1670  */
   1671 void
   1672 config_pending_incr(void)
   1673 {
   1674 
   1675 	config_pending++;
   1676 }
   1677 
   1678 void
   1679 config_pending_decr(void)
   1680 {
   1681 
   1682 #ifdef DIAGNOSTIC
   1683 	if (config_pending == 0)
   1684 		panic("config_pending_decr: config_pending == 0");
   1685 #endif
   1686 	config_pending--;
   1687 	if (config_pending == 0)
   1688 		wakeup(&config_pending);
   1689 }
   1690 
   1691 /*
   1692  * Register a "finalization" routine.  Finalization routines are
   1693  * called iteratively once all real devices have been found during
   1694  * autoconfiguration, for as long as any one finalizer has done
   1695  * any work.
   1696  */
   1697 int
   1698 config_finalize_register(device_t dev, int (*fn)(device_t))
   1699 {
   1700 	struct finalize_hook *f;
   1701 
   1702 	/*
   1703 	 * If finalization has already been done, invoke the
   1704 	 * callback function now.
   1705 	 */
   1706 	if (config_finalize_done) {
   1707 		while ((*fn)(dev) != 0)
   1708 			/* loop */ ;
   1709 	}
   1710 
   1711 	/* Ensure this isn't already on the list. */
   1712 	TAILQ_FOREACH(f, &config_finalize_list, f_list) {
   1713 		if (f->f_func == fn && f->f_dev == dev)
   1714 			return (EEXIST);
   1715 	}
   1716 
   1717 	f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
   1718 	f->f_func = fn;
   1719 	f->f_dev = dev;
   1720 	TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
   1721 
   1722 	return (0);
   1723 }
   1724 
   1725 void
   1726 config_finalize(void)
   1727 {
   1728 	struct finalize_hook *f;
   1729 	struct pdevinit *pdev;
   1730 	extern struct pdevinit pdevinit[];
   1731 	int errcnt, rv;
   1732 
   1733 	/*
   1734 	 * Now that device driver threads have been created, wait for
   1735 	 * them to finish any deferred autoconfiguration.
   1736 	 */
   1737 	while (config_pending)
   1738 		(void) tsleep(&config_pending, PWAIT, "cfpend", hz);
   1739 
   1740 	/* Attach pseudo-devices. */
   1741 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
   1742 		(*pdev->pdev_attach)(pdev->pdev_count);
   1743 
   1744 	/* Run the hooks until none of them does any work. */
   1745 	do {
   1746 		rv = 0;
   1747 		TAILQ_FOREACH(f, &config_finalize_list, f_list)
   1748 			rv |= (*f->f_func)(f->f_dev);
   1749 	} while (rv != 0);
   1750 
   1751 	config_finalize_done = 1;
   1752 
   1753 	/* Now free all the hooks. */
   1754 	while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
   1755 		TAILQ_REMOVE(&config_finalize_list, f, f_list);
   1756 		free(f, M_TEMP);
   1757 	}
   1758 
   1759 	errcnt = aprint_get_error_count();
   1760 	if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
   1761 	    (boothowto & AB_VERBOSE) == 0) {
   1762 		if (config_do_twiddle) {
   1763 			config_do_twiddle = 0;
   1764 			printf_nolog("done.\n");
   1765 		}
   1766 		if (errcnt != 0) {
   1767 			printf("WARNING: %d error%s while detecting hardware; "
   1768 			    "check system log.\n", errcnt,
   1769 			    errcnt == 1 ? "" : "s");
   1770 		}
   1771 	}
   1772 }
   1773 
   1774 /*
   1775  * device_lookup:
   1776  *
   1777  *	Look up a device instance for a given driver.
   1778  */
   1779 void *
   1780 device_lookup(cfdriver_t cd, int unit)
   1781 {
   1782 
   1783 	if (unit < 0 || unit >= cd->cd_ndevs)
   1784 		return (NULL);
   1785 
   1786 	return (cd->cd_devs[unit]);
   1787 }
   1788 
   1789 /*
   1790  * device_lookup:
   1791  *
   1792  *	Look up a device instance for a given driver.
   1793  */
   1794 void *
   1795 device_lookup_private(cfdriver_t cd, int unit)
   1796 {
   1797 	device_t dv;
   1798 
   1799 	if (unit < 0 || unit >= cd->cd_ndevs)
   1800 		return NULL;
   1801 
   1802 	if ((dv = cd->cd_devs[unit]) == NULL)
   1803 		return NULL;
   1804 
   1805 	return dv->dv_private;
   1806 }
   1807 
   1808 /*
   1809  * Accessor functions for the device_t type.
   1810  */
   1811 devclass_t
   1812 device_class(device_t dev)
   1813 {
   1814 
   1815 	return (dev->dv_class);
   1816 }
   1817 
   1818 cfdata_t
   1819 device_cfdata(device_t dev)
   1820 {
   1821 
   1822 	return (dev->dv_cfdata);
   1823 }
   1824 
   1825 cfdriver_t
   1826 device_cfdriver(device_t dev)
   1827 {
   1828 
   1829 	return (dev->dv_cfdriver);
   1830 }
   1831 
   1832 cfattach_t
   1833 device_cfattach(device_t dev)
   1834 {
   1835 
   1836 	return (dev->dv_cfattach);
   1837 }
   1838 
   1839 int
   1840 device_unit(device_t dev)
   1841 {
   1842 
   1843 	return (dev->dv_unit);
   1844 }
   1845 
   1846 const char *
   1847 device_xname(device_t dev)
   1848 {
   1849 
   1850 	return (dev->dv_xname);
   1851 }
   1852 
   1853 device_t
   1854 device_parent(device_t dev)
   1855 {
   1856 
   1857 	return (dev->dv_parent);
   1858 }
   1859 
   1860 bool
   1861 device_is_active(device_t dev)
   1862 {
   1863 	int active_flags;
   1864 
   1865 	active_flags = DVF_ACTIVE;
   1866 	active_flags |= DVF_CLASS_SUSPENDED;
   1867 	active_flags |= DVF_DRIVER_SUSPENDED;
   1868 	active_flags |= DVF_BUS_SUSPENDED;
   1869 
   1870 	return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
   1871 }
   1872 
   1873 bool
   1874 device_is_enabled(device_t dev)
   1875 {
   1876 	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
   1877 }
   1878 
   1879 bool
   1880 device_has_power(device_t dev)
   1881 {
   1882 	int active_flags;
   1883 
   1884 	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
   1885 
   1886 	return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
   1887 }
   1888 
   1889 int
   1890 device_locator(device_t dev, u_int locnum)
   1891 {
   1892 
   1893 	KASSERT(dev->dv_locators != NULL);
   1894 	return (dev->dv_locators[locnum]);
   1895 }
   1896 
   1897 void *
   1898 device_private(device_t dev)
   1899 {
   1900 
   1901 	/*
   1902 	 * The reason why device_private(NULL) is allowed is to simplify the
   1903 	 * work of a lot of userspace request handlers (i.e., c/bdev
   1904 	 * handlers) which grab cfdriver_t->cd_units[n].
   1905 	 * It avoids having them test for it to be NULL and only then calling
   1906 	 * device_private.
   1907 	 */
   1908 	return dev == NULL ? NULL : dev->dv_private;
   1909 }
   1910 
   1911 prop_dictionary_t
   1912 device_properties(device_t dev)
   1913 {
   1914 
   1915 	return (dev->dv_properties);
   1916 }
   1917 
   1918 /*
   1919  * device_is_a:
   1920  *
   1921  *	Returns true if the device is an instance of the specified
   1922  *	driver.
   1923  */
   1924 bool
   1925 device_is_a(device_t dev, const char *dname)
   1926 {
   1927 
   1928 	return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0);
   1929 }
   1930 
   1931 /*
   1932  * device_find_by_xname:
   1933  *
   1934  *	Returns the device of the given name or NULL if it doesn't exist.
   1935  */
   1936 device_t
   1937 device_find_by_xname(const char *name)
   1938 {
   1939 	device_t dv;
   1940 	deviter_t di;
   1941 
   1942 	for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
   1943 		if (strcmp(device_xname(dv), name) == 0)
   1944 			break;
   1945 	}
   1946 	deviter_release(&di);
   1947 
   1948 	return dv;
   1949 }
   1950 
   1951 /*
   1952  * device_find_by_driver_unit:
   1953  *
   1954  *	Returns the device of the given driver name and unit or
   1955  *	NULL if it doesn't exist.
   1956  */
   1957 device_t
   1958 device_find_by_driver_unit(const char *name, int unit)
   1959 {
   1960 	struct cfdriver *cd;
   1961 
   1962 	if ((cd = config_cfdriver_lookup(name)) == NULL)
   1963 		return NULL;
   1964 	return device_lookup(cd, unit);
   1965 }
   1966 
   1967 /*
   1968  * Power management related functions.
   1969  */
   1970 
   1971 bool
   1972 device_pmf_is_registered(device_t dev)
   1973 {
   1974 	return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
   1975 }
   1976 
   1977 bool
   1978 device_pmf_driver_suspend(device_t dev PMF_FN_ARGS)
   1979 {
   1980 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   1981 		return true;
   1982 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   1983 		return false;
   1984 	if (*dev->dv_driver_suspend != NULL &&
   1985 	    !(*dev->dv_driver_suspend)(dev PMF_FN_CALL))
   1986 		return false;
   1987 
   1988 	dev->dv_flags |= DVF_DRIVER_SUSPENDED;
   1989 	return true;
   1990 }
   1991 
   1992 bool
   1993 device_pmf_driver_resume(device_t dev PMF_FN_ARGS)
   1994 {
   1995 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   1996 		return true;
   1997 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   1998 		return false;
   1999 	if ((flags & PMF_F_SELF) != 0 && !device_is_self_suspended(dev))
   2000 		return false;
   2001 	if (*dev->dv_driver_resume != NULL &&
   2002 	    !(*dev->dv_driver_resume)(dev PMF_FN_CALL))
   2003 		return false;
   2004 
   2005 	dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
   2006 	return true;
   2007 }
   2008 
   2009 bool
   2010 device_pmf_driver_shutdown(device_t dev, int how)
   2011 {
   2012 
   2013 	if (*dev->dv_driver_shutdown != NULL &&
   2014 	    !(*dev->dv_driver_shutdown)(dev, how))
   2015 		return false;
   2016 	return true;
   2017 }
   2018 
   2019 bool
   2020 device_pmf_driver_register(device_t dev,
   2021     bool (*suspend)(device_t PMF_FN_PROTO),
   2022     bool (*resume)(device_t PMF_FN_PROTO),
   2023     bool (*shutdown)(device_t, int))
   2024 {
   2025 	pmf_private_t *pp;
   2026 
   2027 	if ((pp = malloc(sizeof(*pp), M_PMFPRIV, M_NOWAIT|M_ZERO)) == NULL)
   2028 		return false;
   2029 	mutex_init(&pp->pp_mtx, MUTEX_DEFAULT, IPL_NONE);
   2030 	cv_init(&pp->pp_cv, "pmfsusp");
   2031 	dev->dv_pmf_private = pp;
   2032 
   2033 	dev->dv_driver_suspend = suspend;
   2034 	dev->dv_driver_resume = resume;
   2035 	dev->dv_driver_shutdown = shutdown;
   2036 	dev->dv_flags |= DVF_POWER_HANDLERS;
   2037 	return true;
   2038 }
   2039 
   2040 static const char *
   2041 curlwp_name(void)
   2042 {
   2043 	if (curlwp->l_name != NULL)
   2044 		return curlwp->l_name;
   2045 	else
   2046 		return curlwp->l_proc->p_comm;
   2047 }
   2048 
   2049 void
   2050 device_pmf_driver_deregister(device_t dev)
   2051 {
   2052 	pmf_private_t *pp = dev->dv_pmf_private;
   2053 
   2054 	dev->dv_driver_suspend = NULL;
   2055 	dev->dv_driver_resume = NULL;
   2056 
   2057 	dev->dv_pmf_private = NULL;
   2058 
   2059 	mutex_enter(&pp->pp_mtx);
   2060 	dev->dv_flags &= ~DVF_POWER_HANDLERS;
   2061 	while (pp->pp_nlock > 0 || pp->pp_nwait > 0) {
   2062 		/* Wake a thread that waits for the lock.  That
   2063 		 * thread will fail to acquire the lock, and then
   2064 		 * it will wake the next thread that waits for the
   2065 		 * lock, or else it will wake us.
   2066 		 */
   2067 		cv_signal(&pp->pp_cv);
   2068 		pmflock_debug(dev, __func__, __LINE__);
   2069 		cv_wait(&pp->pp_cv, &pp->pp_mtx);
   2070 		pmflock_debug(dev, __func__, __LINE__);
   2071 	}
   2072 	mutex_exit(&pp->pp_mtx);
   2073 
   2074 	cv_destroy(&pp->pp_cv);
   2075 	mutex_destroy(&pp->pp_mtx);
   2076 	free(pp, M_PMFPRIV);
   2077 }
   2078 
   2079 bool
   2080 device_pmf_driver_child_register(device_t dev)
   2081 {
   2082 	device_t parent = device_parent(dev);
   2083 
   2084 	if (parent == NULL || parent->dv_driver_child_register == NULL)
   2085 		return true;
   2086 	return (*parent->dv_driver_child_register)(dev);
   2087 }
   2088 
   2089 void
   2090 device_pmf_driver_set_child_register(device_t dev,
   2091     bool (*child_register)(device_t))
   2092 {
   2093 	dev->dv_driver_child_register = child_register;
   2094 }
   2095 
   2096 void
   2097 device_pmf_self_resume(device_t dev PMF_FN_ARGS)
   2098 {
   2099 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2100 	if ((dev->dv_flags & DVF_SELF_SUSPENDED) != 0)
   2101 		dev->dv_flags &= ~DVF_SELF_SUSPENDED;
   2102 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2103 }
   2104 
   2105 bool
   2106 device_is_self_suspended(device_t dev)
   2107 {
   2108 	return (dev->dv_flags & DVF_SELF_SUSPENDED) != 0;
   2109 }
   2110 
   2111 void
   2112 device_pmf_self_suspend(device_t dev PMF_FN_ARGS)
   2113 {
   2114 	bool self = (flags & PMF_F_SELF) != 0;
   2115 
   2116 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2117 
   2118 	if (!self)
   2119 		dev->dv_flags &= ~DVF_SELF_SUSPENDED;
   2120 	else if (device_is_active(dev))
   2121 		dev->dv_flags |= DVF_SELF_SUSPENDED;
   2122 
   2123 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2124 }
   2125 
   2126 static void
   2127 pmflock_debug(device_t dev, const char *func, int line)
   2128 {
   2129 	pmf_private_t *pp = device_pmf_private(dev);
   2130 
   2131 	aprint_debug_dev(dev, "%s.%d, %s pp_nlock %d pp_nwait %d dv_flags %x\n",
   2132 	    func, line, curlwp_name(), pp->pp_nlock, pp->pp_nwait,
   2133 	    dev->dv_flags);
   2134 }
   2135 
   2136 static void
   2137 pmflock_debug_with_flags(device_t dev, const char *func, int line PMF_FN_ARGS)
   2138 {
   2139 	pmf_private_t *pp = device_pmf_private(dev);
   2140 
   2141 	aprint_debug_dev(dev, "%s.%d, %s pp_nlock %d pp_nwait %d dv_flags %x "
   2142 	    "flags " PMF_FLAGS_FMT "\n", func, line, curlwp_name(),
   2143 	    pp->pp_nlock, pp->pp_nwait, dev->dv_flags PMF_FN_CALL);
   2144 }
   2145 
   2146 static bool
   2147 device_pmf_lock1(device_t dev PMF_FN_ARGS)
   2148 {
   2149 	pmf_private_t *pp = device_pmf_private(dev);
   2150 
   2151 	while (pp->pp_nlock > 0 && pp->pp_holder != curlwp &&
   2152 	       device_pmf_is_registered(dev)) {
   2153 		pp->pp_nwait++;
   2154 		pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2155 		cv_wait(&pp->pp_cv, &pp->pp_mtx);
   2156 		pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2157 		pp->pp_nwait--;
   2158 	}
   2159 	if (!device_pmf_is_registered(dev)) {
   2160 		pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2161 		/* We could not acquire the lock, but some other thread may
   2162 		 * wait for it, also.  Wake that thread.
   2163 		 */
   2164 		cv_signal(&pp->pp_cv);
   2165 		return false;
   2166 	}
   2167 	pp->pp_nlock++;
   2168 	pp->pp_holder = curlwp;
   2169 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2170 	return true;
   2171 }
   2172 
   2173 bool
   2174 device_pmf_lock(device_t dev PMF_FN_ARGS)
   2175 {
   2176 	bool rc;
   2177 	pmf_private_t *pp = device_pmf_private(dev);
   2178 
   2179 	mutex_enter(&pp->pp_mtx);
   2180 	rc = device_pmf_lock1(dev PMF_FN_CALL);
   2181 	mutex_exit(&pp->pp_mtx);
   2182 
   2183 	return rc;
   2184 }
   2185 
   2186 void
   2187 device_pmf_unlock(device_t dev PMF_FN_ARGS)
   2188 {
   2189 	pmf_private_t *pp = device_pmf_private(dev);
   2190 
   2191 	KASSERT(pp->pp_nlock > 0);
   2192 	mutex_enter(&pp->pp_mtx);
   2193 	if (--pp->pp_nlock == 0)
   2194 		pp->pp_holder = NULL;
   2195 	cv_signal(&pp->pp_cv);
   2196 	pmflock_debug_with_flags(dev, __func__, __LINE__ PMF_FN_CALL);
   2197 	mutex_exit(&pp->pp_mtx);
   2198 }
   2199 
   2200 void *
   2201 device_pmf_private(device_t dev)
   2202 {
   2203 	return dev->dv_pmf_private;
   2204 }
   2205 
   2206 void *
   2207 device_pmf_bus_private(device_t dev)
   2208 {
   2209 	return dev->dv_bus_private;
   2210 }
   2211 
   2212 bool
   2213 device_pmf_bus_suspend(device_t dev PMF_FN_ARGS)
   2214 {
   2215 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   2216 		return true;
   2217 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
   2218 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   2219 		return false;
   2220 	if (*dev->dv_bus_suspend != NULL &&
   2221 	    !(*dev->dv_bus_suspend)(dev PMF_FN_CALL))
   2222 		return false;
   2223 
   2224 	dev->dv_flags |= DVF_BUS_SUSPENDED;
   2225 	return true;
   2226 }
   2227 
   2228 bool
   2229 device_pmf_bus_resume(device_t dev PMF_FN_ARGS)
   2230 {
   2231 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
   2232 		return true;
   2233 	if ((flags & PMF_F_SELF) != 0 && !device_is_self_suspended(dev))
   2234 		return false;
   2235 	if (*dev->dv_bus_resume != NULL &&
   2236 	    !(*dev->dv_bus_resume)(dev PMF_FN_CALL))
   2237 		return false;
   2238 
   2239 	dev->dv_flags &= ~DVF_BUS_SUSPENDED;
   2240 	return true;
   2241 }
   2242 
   2243 bool
   2244 device_pmf_bus_shutdown(device_t dev, int how)
   2245 {
   2246 
   2247 	if (*dev->dv_bus_shutdown != NULL &&
   2248 	    !(*dev->dv_bus_shutdown)(dev, how))
   2249 		return false;
   2250 	return true;
   2251 }
   2252 
   2253 void
   2254 device_pmf_bus_register(device_t dev, void *priv,
   2255     bool (*suspend)(device_t PMF_FN_PROTO),
   2256     bool (*resume)(device_t PMF_FN_PROTO),
   2257     bool (*shutdown)(device_t, int), void (*deregister)(device_t))
   2258 {
   2259 	dev->dv_bus_private = priv;
   2260 	dev->dv_bus_resume = resume;
   2261 	dev->dv_bus_suspend = suspend;
   2262 	dev->dv_bus_shutdown = shutdown;
   2263 	dev->dv_bus_deregister = deregister;
   2264 }
   2265 
   2266 void
   2267 device_pmf_bus_deregister(device_t dev)
   2268 {
   2269 	if (dev->dv_bus_deregister == NULL)
   2270 		return;
   2271 	(*dev->dv_bus_deregister)(dev);
   2272 	dev->dv_bus_private = NULL;
   2273 	dev->dv_bus_suspend = NULL;
   2274 	dev->dv_bus_resume = NULL;
   2275 	dev->dv_bus_deregister = NULL;
   2276 }
   2277 
   2278 void *
   2279 device_pmf_class_private(device_t dev)
   2280 {
   2281 	return dev->dv_class_private;
   2282 }
   2283 
   2284 bool
   2285 device_pmf_class_suspend(device_t dev PMF_FN_ARGS)
   2286 {
   2287 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
   2288 		return true;
   2289 	if (*dev->dv_class_suspend != NULL &&
   2290 	    !(*dev->dv_class_suspend)(dev PMF_FN_CALL))
   2291 		return false;
   2292 
   2293 	dev->dv_flags |= DVF_CLASS_SUSPENDED;
   2294 	return true;
   2295 }
   2296 
   2297 bool
   2298 device_pmf_class_resume(device_t dev PMF_FN_ARGS)
   2299 {
   2300 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   2301 		return true;
   2302 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
   2303 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   2304 		return false;
   2305 	if (*dev->dv_class_resume != NULL &&
   2306 	    !(*dev->dv_class_resume)(dev PMF_FN_CALL))
   2307 		return false;
   2308 
   2309 	dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
   2310 	return true;
   2311 }
   2312 
   2313 void
   2314 device_pmf_class_register(device_t dev, void *priv,
   2315     bool (*suspend)(device_t PMF_FN_PROTO),
   2316     bool (*resume)(device_t PMF_FN_PROTO),
   2317     void (*deregister)(device_t))
   2318 {
   2319 	dev->dv_class_private = priv;
   2320 	dev->dv_class_suspend = suspend;
   2321 	dev->dv_class_resume = resume;
   2322 	dev->dv_class_deregister = deregister;
   2323 }
   2324 
   2325 void
   2326 device_pmf_class_deregister(device_t dev)
   2327 {
   2328 	if (dev->dv_class_deregister == NULL)
   2329 		return;
   2330 	(*dev->dv_class_deregister)(dev);
   2331 	dev->dv_class_private = NULL;
   2332 	dev->dv_class_suspend = NULL;
   2333 	dev->dv_class_resume = NULL;
   2334 	dev->dv_class_deregister = NULL;
   2335 }
   2336 
   2337 bool
   2338 device_active(device_t dev, devactive_t type)
   2339 {
   2340 	size_t i;
   2341 
   2342 	if (dev->dv_activity_count == 0)
   2343 		return false;
   2344 
   2345 	for (i = 0; i < dev->dv_activity_count; ++i)
   2346 		(*dev->dv_activity_handlers[i])(dev, type);
   2347 
   2348 	return true;
   2349 }
   2350 
   2351 bool
   2352 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
   2353 {
   2354 	void (**new_handlers)(device_t, devactive_t);
   2355 	void (**old_handlers)(device_t, devactive_t);
   2356 	size_t i, new_size;
   2357 	int s;
   2358 
   2359 	old_handlers = dev->dv_activity_handlers;
   2360 
   2361 	for (i = 0; i < dev->dv_activity_count; ++i) {
   2362 		if (old_handlers[i] == handler)
   2363 			panic("Double registering of idle handlers");
   2364 	}
   2365 
   2366 	new_size = dev->dv_activity_count + 1;
   2367 	new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, M_WAITOK);
   2368 
   2369 	memcpy(new_handlers, old_handlers,
   2370 	    sizeof(void *) * dev->dv_activity_count);
   2371 	new_handlers[new_size - 1] = handler;
   2372 
   2373 	s = splhigh();
   2374 	dev->dv_activity_count = new_size;
   2375 	dev->dv_activity_handlers = new_handlers;
   2376 	splx(s);
   2377 
   2378 	if (old_handlers != NULL)
   2379 		free(old_handlers, M_DEVBUF);
   2380 
   2381 	return true;
   2382 }
   2383 
   2384 void
   2385 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
   2386 {
   2387 	void (**new_handlers)(device_t, devactive_t);
   2388 	void (**old_handlers)(device_t, devactive_t);
   2389 	size_t i, new_size;
   2390 	int s;
   2391 
   2392 	old_handlers = dev->dv_activity_handlers;
   2393 
   2394 	for (i = 0; i < dev->dv_activity_count; ++i) {
   2395 		if (old_handlers[i] == handler)
   2396 			break;
   2397 	}
   2398 
   2399 	if (i == dev->dv_activity_count)
   2400 		return; /* XXX panic? */
   2401 
   2402 	new_size = dev->dv_activity_count - 1;
   2403 
   2404 	if (new_size == 0) {
   2405 		new_handlers = NULL;
   2406 	} else {
   2407 		new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF,
   2408 		    M_WAITOK);
   2409 		memcpy(new_handlers, old_handlers, sizeof(void *) * i);
   2410 		memcpy(new_handlers + i, old_handlers + i + 1,
   2411 		    sizeof(void *) * (new_size - i));
   2412 	}
   2413 
   2414 	s = splhigh();
   2415 	dev->dv_activity_count = new_size;
   2416 	dev->dv_activity_handlers = new_handlers;
   2417 	splx(s);
   2418 
   2419 	free(old_handlers, M_DEVBUF);
   2420 }
   2421 
   2422 /*
   2423  * Device Iteration
   2424  *
   2425  * deviter_t: a device iterator.  Holds state for a "walk" visiting
   2426  *     each device_t's in the device tree.
   2427  *
   2428  * deviter_init(di, flags): initialize the device iterator `di'
   2429  *     to "walk" the device tree.  deviter_next(di) will return
   2430  *     the first device_t in the device tree, or NULL if there are
   2431  *     no devices.
   2432  *
   2433  *     `flags' is one or more of DEVITER_F_RW, indicating that the
   2434  *     caller intends to modify the device tree by calling
   2435  *     config_detach(9) on devices in the order that the iterator
   2436  *     returns them; DEVITER_F_ROOT_FIRST, asking for the devices
   2437  *     nearest the "root" of the device tree to be returned, first;
   2438  *     DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
   2439  *     the root of the device tree, first; and DEVITER_F_SHUTDOWN,
   2440  *     indicating both that deviter_init() should not respect any
   2441  *     locks on the device tree, and that deviter_next(di) may run
   2442  *     in more than one LWP before the walk has finished.
   2443  *
   2444  *     Only one DEVITER_F_RW iterator may be in the device tree at
   2445  *     once.
   2446  *
   2447  *     DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
   2448  *
   2449  *     Results are undefined if the flags DEVITER_F_ROOT_FIRST and
   2450  *     DEVITER_F_LEAVES_FIRST are used in combination.
   2451  *
   2452  * deviter_first(di, flags): initialize the device iterator `di'
   2453  *     and return the first device_t in the device tree, or NULL
   2454  *     if there are no devices.  The statement
   2455  *
   2456  *         dv = deviter_first(di);
   2457  *
   2458  *     is shorthand for
   2459  *
   2460  *         deviter_init(di);
   2461  *         dv = deviter_next(di);
   2462  *
   2463  * deviter_next(di): return the next device_t in the device tree,
   2464  *     or NULL if there are no more devices.  deviter_next(di)
   2465  *     is undefined if `di' was not initialized with deviter_init() or
   2466  *     deviter_first().
   2467  *
   2468  * deviter_release(di): stops iteration (subsequent calls to
   2469  *     deviter_next() will return NULL), releases any locks and
   2470  *     resources held by the device iterator.
   2471  *
   2472  * Device iteration does not return device_t's in any particular
   2473  * order.  An iterator will never return the same device_t twice.
   2474  * Device iteration is guaranteed to complete---i.e., if deviter_next(di)
   2475  * is called repeatedly on the same `di', it will eventually return
   2476  * NULL.  It is ok to attach/detach devices during device iteration.
   2477  */
   2478 void
   2479 deviter_init(deviter_t *di, deviter_flags_t flags)
   2480 {
   2481 	device_t dv;
   2482 	bool rw;
   2483 
   2484 	mutex_enter(&alldevs_mtx);
   2485 	if ((flags & DEVITER_F_SHUTDOWN) != 0) {
   2486 		flags |= DEVITER_F_RW;
   2487 		alldevs_nwrite++;
   2488 		alldevs_writer = NULL;
   2489 		alldevs_nread = 0;
   2490 	} else {
   2491 		rw = (flags & DEVITER_F_RW) != 0;
   2492 
   2493 		if (alldevs_nwrite > 0 && alldevs_writer == NULL)
   2494 			;
   2495 		else while ((alldevs_nwrite != 0 && alldevs_writer != curlwp) ||
   2496 		       (rw && alldevs_nread != 0))
   2497 			cv_wait(&alldevs_cv, &alldevs_mtx);
   2498 
   2499 		if (rw) {
   2500 			if (alldevs_nwrite++ == 0)
   2501 				alldevs_writer = curlwp;
   2502 		} else
   2503 			alldevs_nread++;
   2504 	}
   2505 	mutex_exit(&alldevs_mtx);
   2506 
   2507 	memset(di, 0, sizeof(*di));
   2508 
   2509 	di->di_flags = flags;
   2510 
   2511 	switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
   2512 	case DEVITER_F_LEAVES_FIRST:
   2513 		TAILQ_FOREACH(dv, &alldevs, dv_list)
   2514 			di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
   2515 		break;
   2516 	case DEVITER_F_ROOT_FIRST:
   2517 		TAILQ_FOREACH(dv, &alldevs, dv_list)
   2518 			di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
   2519 		break;
   2520 	default:
   2521 		break;
   2522 	}
   2523 
   2524 	deviter_reinit(di);
   2525 }
   2526 
   2527 static void
   2528 deviter_reinit(deviter_t *di)
   2529 {
   2530 	if ((di->di_flags & DEVITER_F_RW) != 0)
   2531 		di->di_prev = TAILQ_LAST(&alldevs, devicelist);
   2532 	else
   2533 		di->di_prev = TAILQ_FIRST(&alldevs);
   2534 }
   2535 
   2536 device_t
   2537 deviter_first(deviter_t *di, deviter_flags_t flags)
   2538 {
   2539 	deviter_init(di, flags);
   2540 	return deviter_next(di);
   2541 }
   2542 
   2543 static device_t
   2544 deviter_next1(deviter_t *di)
   2545 {
   2546 	device_t dv;
   2547 
   2548 	dv = di->di_prev;
   2549 
   2550 	if (dv == NULL)
   2551 		;
   2552 	else if ((di->di_flags & DEVITER_F_RW) != 0)
   2553 		di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
   2554 	else
   2555 		di->di_prev = TAILQ_NEXT(dv, dv_list);
   2556 
   2557 	return dv;
   2558 }
   2559 
   2560 device_t
   2561 deviter_next(deviter_t *di)
   2562 {
   2563 	device_t dv = NULL;
   2564 
   2565 	switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
   2566 	case 0:
   2567 		return deviter_next1(di);
   2568 	case DEVITER_F_LEAVES_FIRST:
   2569 		while (di->di_curdepth >= 0) {
   2570 			if ((dv = deviter_next1(di)) == NULL) {
   2571 				di->di_curdepth--;
   2572 				deviter_reinit(di);
   2573 			} else if (dv->dv_depth == di->di_curdepth)
   2574 				break;
   2575 		}
   2576 		return dv;
   2577 	case DEVITER_F_ROOT_FIRST:
   2578 		while (di->di_curdepth <= di->di_maxdepth) {
   2579 			if ((dv = deviter_next1(di)) == NULL) {
   2580 				di->di_curdepth++;
   2581 				deviter_reinit(di);
   2582 			} else if (dv->dv_depth == di->di_curdepth)
   2583 				break;
   2584 		}
   2585 		return dv;
   2586 	default:
   2587 		return NULL;
   2588 	}
   2589 }
   2590 
   2591 void
   2592 deviter_release(deviter_t *di)
   2593 {
   2594 	bool rw = (di->di_flags & DEVITER_F_RW) != 0;
   2595 
   2596 	mutex_enter(&alldevs_mtx);
   2597 	if (alldevs_nwrite > 0 && alldevs_writer == NULL)
   2598 		--alldevs_nwrite;
   2599 	else {
   2600 
   2601 		if (rw) {
   2602 			if (--alldevs_nwrite == 0)
   2603 				alldevs_writer = NULL;
   2604 		} else
   2605 			--alldevs_nread;
   2606 
   2607 		cv_signal(&alldevs_cv);
   2608 	}
   2609 	mutex_exit(&alldevs_mtx);
   2610 }
   2611