xref: /netbsd-src/sys/kern/subr_autoconf.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /* $NetBSD: subr_autoconf.c,v 1.57 2001/07/01 02:56:20 gmcgarry 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. All advertising materials mentioning features or use of this software
59  *    must display the following acknowledgement:
60  *	This product includes software developed by the University of
61  *	California, Berkeley and its contributors.
62  * 4. Neither the name of the University nor the names of its contributors
63  *    may be used to endorse or promote products derived from this software
64  *    without specific prior written permission.
65  *
66  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76  * SUCH DAMAGE.
77  *
78  * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp  (LBL)
79  *
80  *	@(#)subr_autoconf.c	8.3 (Berkeley) 5/17/94
81  */
82 
83 #include <sys/cdefs.h>
84 
85 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.57 2001/07/01 02:56:20 gmcgarry Exp $");
86 
87 #include <sys/param.h>
88 #include <sys/device.h>
89 #include <sys/malloc.h>
90 #include <sys/systm.h>
91 #include <sys/kernel.h>
92 #include <sys/errno.h>
93 #include <sys/proc.h>
94 #include <machine/limits.h>
95 
96 #include "opt_userconf.h"
97 #ifdef USERCONF
98 #include <sys/userconf.h>
99 #include <sys/reboot.h>
100 #endif
101 
102 /*
103  * Autoconfiguration subroutines.
104  */
105 
106 /*
107  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
108  * devices and drivers are found via these tables.
109  */
110 extern struct cfdata cfdata[];
111 extern short cfroots[];
112 
113 #define	ROOT ((struct device *)NULL)
114 
115 struct matchinfo {
116 	cfmatch_t fn;
117 	struct	device *parent;
118 	void	*aux;
119 	struct	cfdata *match;
120 	int	pri;
121 };
122 
123 static char *number(char *, int);
124 static void mapply(struct matchinfo *, struct cfdata *);
125 
126 struct deferred_config {
127 	TAILQ_ENTRY(deferred_config) dc_queue;
128 	struct device *dc_dev;
129 	void (*dc_func)(struct device *);
130 };
131 
132 TAILQ_HEAD(deferred_config_head, deferred_config);
133 
134 struct deferred_config_head deferred_config_queue;
135 struct deferred_config_head interrupt_config_queue;
136 
137 static void config_process_deferred(struct deferred_config_head *,
138 	struct device *);
139 
140 /* list of all devices */
141 struct devicelist alldevs;
142 
143 /* list of all events */
144 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
145 
146 __volatile int config_pending;		/* semaphore for mountroot */
147 
148 /*
149  * Configure the system's hardware.
150  */
151 void
152 configure(void)
153 {
154 
155 	TAILQ_INIT(&deferred_config_queue);
156 	TAILQ_INIT(&interrupt_config_queue);
157 	TAILQ_INIT(&alldevs);
158 
159 #ifdef USERCONF
160 	if (boothowto & RB_USERCONF)
161 		user_config();
162 #endif
163 
164 	/*
165 	 * Do the machine-dependent portion of autoconfiguration.  This
166 	 * sets the configuration machinery here in motion by "finding"
167 	 * the root bus.  When this function returns, we expect interrupts
168 	 * to be enabled.
169 	 */
170 	cpu_configure();
171 
172 	/*
173 	 * Now that we've found all the hardware, start the real time
174 	 * and statistics clocks.
175 	 */
176 	initclocks();
177 
178 	cold = 0;	/* clocks are running, we're warm now! */
179 
180 	/*
181 	 * Now callback to finish configuration for devices which want
182 	 * to do this once interrupts are enabled.
183 	 */
184 	config_process_deferred(&interrupt_config_queue, NULL);
185 }
186 
187 /*
188  * Apply the matching function and choose the best.  This is used
189  * a few times and we want to keep the code small.
190  */
191 static void
192 mapply(struct matchinfo *m, struct cfdata *cf)
193 {
194 	int pri;
195 
196 	if (m->fn != NULL)
197 		pri = (*m->fn)(m->parent, cf, m->aux);
198 	else {
199 	        if (cf->cf_attach->ca_match == NULL) {
200 			panic("mapply: no match function for '%s' device\n",
201 			    cf->cf_driver->cd_name);
202 		}
203 		pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
204 	}
205 	if (pri > m->pri) {
206 		m->match = cf;
207 		m->pri = pri;
208 	}
209 }
210 
211 /*
212  * Iterate over all potential children of some device, calling the given
213  * function (default being the child's match function) for each one.
214  * Nonzero returns are matches; the highest value returned is considered
215  * the best match.  Return the `found child' if we got a match, or NULL
216  * otherwise.  The `aux' pointer is simply passed on through.
217  *
218  * Note that this function is designed so that it can be used to apply
219  * an arbitrary function to all potential children (its return value
220  * can be ignored).
221  */
222 struct cfdata *
223 config_search(cfmatch_t fn, struct device *parent, void *aux)
224 {
225 	struct cfdata *cf;
226 	short *p;
227 	struct matchinfo m;
228 
229 	m.fn = fn;
230 	m.parent = parent;
231 	m.aux = aux;
232 	m.match = NULL;
233 	m.pri = 0;
234 	for (cf = cfdata; cf->cf_driver; cf++) {
235 		/*
236 		 * Skip cf if no longer eligible, otherwise scan through
237 		 * parents for one matching `parent', and try match function.
238 		 */
239 		if (cf->cf_fstate == FSTATE_FOUND)
240 			continue;
241 		for (p = cf->cf_parents; *p >= 0; p++)
242 			if (parent->dv_cfdata == &cfdata[*p])
243 				mapply(&m, cf);
244 	}
245 	return (m.match);
246 }
247 
248 /*
249  * Find the given root device.
250  * This is much like config_search, but there is no parent.
251  */
252 struct cfdata *
253 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
254 {
255 	struct cfdata *cf;
256 	short *p;
257 	struct matchinfo m;
258 
259 	m.fn = fn;
260 	m.parent = ROOT;
261 	m.aux = aux;
262 	m.match = NULL;
263 	m.pri = 0;
264 	/*
265 	 * Look at root entries for matching name.  We do not bother
266 	 * with found-state here since only one root should ever be
267 	 * searched (and it must be done first).
268 	 */
269 	for (p = cfroots; *p >= 0; p++) {
270 		cf = &cfdata[*p];
271 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
272 			mapply(&m, cf);
273 	}
274 	return (m.match);
275 }
276 
277 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
278 
279 /*
280  * The given `aux' argument describes a device that has been found
281  * on the given parent, but not necessarily configured.  Locate the
282  * configuration data for that device (using the submatch function
283  * provided, or using candidates' cd_match configuration driver
284  * functions) and attach it, and return true.  If the device was
285  * not configured, call the given `print' function and return 0.
286  */
287 struct device *
288 config_found_sm(struct device *parent, void *aux, cfprint_t print,
289     cfmatch_t submatch)
290 {
291 	struct cfdata *cf;
292 
293 	if ((cf = config_search(submatch, parent, aux)) != NULL)
294 		return (config_attach(parent, cf, aux, print));
295 	if (print)
296 		printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
297 	return (NULL);
298 }
299 
300 /*
301  * As above, but for root devices.
302  */
303 struct device *
304 config_rootfound(const char *rootname, void *aux)
305 {
306 	struct cfdata *cf;
307 
308 	if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
309 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
310 	printf("root device %s not configured\n", rootname);
311 	return (NULL);
312 }
313 
314 /* just like sprintf(buf, "%d") except that it works from the end */
315 static char *
316 number(char *ep, int n)
317 {
318 
319 	*--ep = 0;
320 	while (n >= 10) {
321 		*--ep = (n % 10) + '0';
322 		n /= 10;
323 	}
324 	*--ep = n + '0';
325 	return (ep);
326 }
327 
328 /*
329  * Attach a found device.  Allocates memory for device variables.
330  */
331 struct device *
332 config_attach(struct device *parent, struct cfdata *cf, void *aux,
333 	cfprint_t print)
334 {
335 	struct device *dev;
336 	struct cfdriver *cd;
337 	struct cfattach *ca;
338 	size_t lname, lunit;
339 	const char *xunit;
340 	int myunit;
341 	char num[10];
342 
343 	cd = cf->cf_driver;
344 	ca = cf->cf_attach;
345 	if (ca->ca_devsize < sizeof(struct device))
346 		panic("config_attach");
347 #ifndef __BROKEN_CONFIG_UNIT_USAGE
348 	if (cf->cf_fstate == FSTATE_STAR) {
349 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
350 			if (cd->cd_devs[myunit] == NULL)
351 				break;
352 		/*
353 		 * myunit is now the unit of the first NULL device pointer,
354 		 * or max(cd->cd_ndevs,cf->cf_unit).
355 		 */
356 	} else {
357 		myunit = cf->cf_unit;
358 #else /* __BROKEN_CONFIG_UNIT_USAGE */
359 	myunit = cf->cf_unit;
360 	if (cf->cf_fstate == FSTATE_STAR)
361 		cf->cf_unit++;
362 	else {
363 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
364 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
365 		cf->cf_fstate = FSTATE_FOUND;
366 	}
367 
368 	/* compute length of name and decimal expansion of unit number */
369 	lname = strlen(cd->cd_name);
370 	xunit = number(&num[sizeof(num)], myunit);
371 	lunit = &num[sizeof(num)] - xunit;
372 	if (lname + lunit >= sizeof(dev->dv_xname))
373 		panic("config_attach: device name too long");
374 
375 	/* get memory for all device vars */
376 	dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
377 	    cold ? M_NOWAIT : M_WAITOK);
378 	if (!dev)
379 	    panic("config_attach: memory allocation for device softc failed");
380 	memset(dev, 0, ca->ca_devsize);
381 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
382 	dev->dv_class = cd->cd_class;
383 	dev->dv_cfdata = cf;
384 	dev->dv_unit = myunit;
385 	memcpy(dev->dv_xname, cd->cd_name, lname);
386 	memcpy(dev->dv_xname + lname, xunit, lunit);
387 	dev->dv_parent = parent;
388 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
389 
390 	if (parent == ROOT)
391 		printf("%s (root)", dev->dv_xname);
392 	else {
393 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
394 		if (print)
395 			(void) (*print)(aux, NULL);
396 	}
397 
398 	/* put this device in the devices array */
399 	if (dev->dv_unit >= cd->cd_ndevs) {
400 		/*
401 		 * Need to expand the array.
402 		 */
403 		int old = cd->cd_ndevs, new;
404 		void **nsp;
405 
406 		if (old == 0)
407 			new = MINALLOCSIZE / sizeof(void *);
408 		else
409 			new = old * 2;
410 		while (new <= dev->dv_unit)
411 			new *= 2;
412 		cd->cd_ndevs = new;
413 		nsp = malloc(new * sizeof(void *), M_DEVBUF,
414 		    cold ? M_NOWAIT : M_WAITOK);
415 		if (nsp == 0)
416 			panic("config_attach: %sing dev array",
417 			    old != 0 ? "expand" : "creat");
418 		memset(nsp + old, 0, (new - old) * sizeof(void *));
419 		if (old != 0) {
420 			memcpy(nsp, cd->cd_devs, old * sizeof(void *));
421 			free(cd->cd_devs, M_DEVBUF);
422 		}
423 		cd->cd_devs = nsp;
424 	}
425 	if (cd->cd_devs[dev->dv_unit])
426 		panic("config_attach: duplicate %s", dev->dv_xname);
427 	cd->cd_devs[dev->dv_unit] = dev;
428 
429 	/*
430 	 * Before attaching, clobber any unfound devices that are
431 	 * otherwise identical.
432 	 */
433 #ifdef __BROKEN_CONFIG_UNIT_USAGE
434 	/* bump the unit number on all starred cfdata for this device. */
435 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
436 	for (cf = cfdata; cf->cf_driver; cf++)
437 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
438 			if (cf->cf_fstate == FSTATE_NOTFOUND)
439 				cf->cf_fstate = FSTATE_FOUND;
440 #ifdef __BROKEN_CONFIG_UNIT_USAGE
441 			if (cf->cf_fstate == FSTATE_STAR)
442 				cf->cf_unit++;
443 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
444 		}
445 #ifdef __HAVE_DEVICE_REGISTER
446 	device_register(dev, aux);
447 #endif
448 	(*ca->ca_attach)(parent, dev, aux);
449 	config_process_deferred(&deferred_config_queue, dev);
450 	return (dev);
451 }
452 
453 /*
454  * Detach a device.  Optionally forced (e.g. because of hardware
455  * removal) and quiet.  Returns zero if successful, non-zero
456  * (an error code) otherwise.
457  *
458  * Note that this code wants to be run from a process context, so
459  * that the detach can sleep to allow processes which have a device
460  * open to run and unwind their stacks.
461  */
462 int
463 config_detach(struct device *dev, int flags)
464 {
465 	struct cfdata *cf;
466 	struct cfattach *ca;
467 	struct cfdriver *cd;
468 #ifdef DIAGNOSTIC
469 	struct device *d;
470 #endif
471 	int rv = 0, i;
472 
473 	cf = dev->dv_cfdata;
474 #ifdef DIAGNOSTIC
475 	if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
476 		panic("config_detach: bad device fstate");
477 #endif
478 	ca = cf->cf_attach;
479 	cd = cf->cf_driver;
480 
481 	/*
482 	 * Ensure the device is deactivated.  If the device doesn't
483 	 * have an activation entry point, we allow DVF_ACTIVE to
484 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
485 	 * device is busy, and the detach fails.
486 	 */
487 	if (ca->ca_activate != NULL)
488 		rv = config_deactivate(dev);
489 
490 	/*
491 	 * Try to detach the device.  If that's not possible, then
492 	 * we either panic() (for the forced but failed case), or
493 	 * return an error.
494 	 */
495 	if (rv == 0) {
496 		if (ca->ca_detach != NULL)
497 			rv = (*ca->ca_detach)(dev, flags);
498 		else
499 			rv = EOPNOTSUPP;
500 	}
501 	if (rv != 0) {
502 		if ((flags & DETACH_FORCE) == 0)
503 			return (rv);
504 		else
505 			panic("config_detach: forced detach of %s failed (%d)",
506 			    dev->dv_xname, rv);
507 	}
508 
509 	/*
510 	 * The device has now been successfully detached.
511 	 */
512 
513 #ifdef DIAGNOSTIC
514 	/*
515 	 * Sanity: If you're successfully detached, you should have no
516 	 * children.  (Note that because children must be attached
517 	 * after parents, we only need to search the latter part of
518 	 * the list.)
519 	 */
520 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
521 	    d = TAILQ_NEXT(d, dv_list)) {
522 		if (d->dv_parent == dev) {
523 			printf("config_detach: detached device %s"
524 			    " has children %s\n", dev->dv_xname, d->dv_xname);
525 			panic("config_detach");
526 		}
527 	}
528 #endif
529 
530 	/*
531 	 * Mark cfdata to show that the unit can be reused, if possible.
532 	 */
533 #ifdef __BROKEN_CONFIG_UNIT_USAGE
534 	/*
535 	 * Note that we can only re-use a starred unit number if the unit
536 	 * being detached had the last assigned unit number.
537 	 */
538 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
539 	for (cf = cfdata; cf->cf_driver; cf++) {
540 		if (cf->cf_driver == cd) {
541 			if (cf->cf_fstate == FSTATE_FOUND &&
542 			    cf->cf_unit == dev->dv_unit)
543 				cf->cf_fstate = FSTATE_NOTFOUND;
544 #ifdef __BROKEN_CONFIG_UNIT_USAGE
545 			if (cf->cf_fstate == FSTATE_STAR &&
546 			    cf->cf_unit == dev->dv_unit + 1)
547 				cf->cf_unit--;
548 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
549 		}
550 	}
551 
552 	/*
553 	 * Unlink from device list.
554 	 */
555 	TAILQ_REMOVE(&alldevs, dev, dv_list);
556 
557 	/*
558 	 * Remove from cfdriver's array, tell the world, and free softc.
559 	 */
560 	cd->cd_devs[dev->dv_unit] = NULL;
561 	if ((flags & DETACH_QUIET) == 0)
562 		printf("%s detached\n", dev->dv_xname);
563 	free(dev, M_DEVBUF);
564 
565 	/*
566 	 * If the device now has no units in use, deallocate its softc array.
567 	 */
568 	for (i = 0; i < cd->cd_ndevs; i++)
569 		if (cd->cd_devs[i] != NULL)
570 			break;
571 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
572 		free(cd->cd_devs, M_DEVBUF);
573 		cd->cd_devs = NULL;
574 		cd->cd_ndevs = 0;
575 	}
576 
577 	/*
578 	 * Return success.
579 	 */
580 	return (0);
581 }
582 
583 int
584 config_activate(struct device *dev)
585 {
586 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
587 	int rv = 0, oflags = dev->dv_flags;
588 
589 	if (ca->ca_activate == NULL)
590 		return (EOPNOTSUPP);
591 
592 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
593 		dev->dv_flags |= DVF_ACTIVE;
594 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
595 		if (rv)
596 			dev->dv_flags = oflags;
597 	}
598 	return (rv);
599 }
600 
601 int
602 config_deactivate(struct device *dev)
603 {
604 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
605 	int rv = 0, oflags = dev->dv_flags;
606 
607 	if (ca->ca_activate == NULL)
608 		return (EOPNOTSUPP);
609 
610 	if (dev->dv_flags & DVF_ACTIVE) {
611 		dev->dv_flags &= ~DVF_ACTIVE;
612 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
613 		if (rv)
614 			dev->dv_flags = oflags;
615 	}
616 	return (rv);
617 }
618 
619 /*
620  * Defer the configuration of the specified device until all
621  * of its parent's devices have been attached.
622  */
623 void
624 config_defer(struct device *dev, void (*func)(struct device *))
625 {
626 	struct deferred_config *dc;
627 
628 	if (dev->dv_parent == NULL)
629 		panic("config_defer: can't defer config of a root device");
630 
631 #ifdef DIAGNOSTIC
632 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
633 	     dc = TAILQ_NEXT(dc, dc_queue)) {
634 		if (dc->dc_dev == dev)
635 			panic("config_defer: deferred twice");
636 	}
637 #endif
638 
639 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
640 	if (dc == NULL)
641 		panic("config_defer: unable to allocate callback");
642 
643 	dc->dc_dev = dev;
644 	dc->dc_func = func;
645 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
646 	config_pending_incr();
647 }
648 
649 /*
650  * Defer some autoconfiguration for a device until after interrupts
651  * are enabled.
652  */
653 void
654 config_interrupts(struct device *dev, void (*func)(struct device *))
655 {
656 	struct deferred_config *dc;
657 
658 	/*
659 	 * If interrupts are enabled, callback now.
660 	 */
661 	if (cold == 0) {
662 		(*func)(dev);
663 		return;
664 	}
665 
666 #ifdef DIAGNOSTIC
667 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
668 	     dc = TAILQ_NEXT(dc, dc_queue)) {
669 		if (dc->dc_dev == dev)
670 			panic("config_interrupts: deferred twice");
671 	}
672 #endif
673 
674 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
675 	if (dc == NULL)
676 		panic("config_interrupts: unable to allocate callback");
677 
678 	dc->dc_dev = dev;
679 	dc->dc_func = func;
680 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
681 	config_pending_incr();
682 }
683 
684 /*
685  * Process a deferred configuration queue.
686  */
687 static void
688 config_process_deferred(struct deferred_config_head *queue,
689     struct device *parent)
690 {
691 	struct deferred_config *dc, *ndc;
692 
693 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
694 		ndc = TAILQ_NEXT(dc, dc_queue);
695 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
696 			TAILQ_REMOVE(queue, dc, dc_queue);
697 			(*dc->dc_func)(dc->dc_dev);
698 			free(dc, M_DEVBUF);
699 			config_pending_decr();
700 		}
701 	}
702 }
703 
704 /*
705  * Manipulate the config_pending semaphore.
706  */
707 void
708 config_pending_incr(void)
709 {
710 
711 	config_pending++;
712 }
713 
714 void
715 config_pending_decr(void)
716 {
717 
718 #ifdef DIAGNOSTIC
719 	if (config_pending == 0)
720 		panic("config_pending_decr: config_pending == 0");
721 #endif
722 	config_pending--;
723 	if (config_pending == 0)
724 		wakeup((void *)&config_pending);
725 }
726 
727 /*
728  * Attach a statically-initialized event.  The type and string pointers
729  * are already set up.
730  */
731 void
732 evcnt_attach_static(struct evcnt *ev)
733 {
734 	int len;
735 
736 	len = strlen(ev->ev_group);
737 #ifdef DIAGNOSTIC
738 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
739 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
740 #endif
741 	ev->ev_grouplen = len;
742 
743 	len = strlen(ev->ev_name);
744 #ifdef DIAGNOSTIC
745 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
746 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
747 #endif
748 	ev->ev_namelen = len;
749 
750 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
751 }
752 
753 /*
754  * Attach a dynamically-initialized event.  Zero it, set up the type
755  * and string pointers and then act like it was statically initialized.
756  */
757 void
758 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
759     const char *group, const char *name)
760 {
761 
762 	memset(ev, 0, sizeof *ev);
763 	ev->ev_type = type;
764 	ev->ev_parent = parent;
765 	ev->ev_group = group;
766 	ev->ev_name = name;
767 	evcnt_attach_static(ev);
768 }
769 
770 /*
771  * Detach an event.
772  */
773 void
774 evcnt_detach(struct evcnt *ev)
775 {
776 
777 	TAILQ_REMOVE(&allevents, ev, ev_list);
778 }
779