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