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