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