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