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