subr_autoconf.c revision 1.75 1 /* $NetBSD: subr_autoconf.c,v 1.75 2002/10/01 18:11:58 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.75 2002/10/01 18:11:58 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 all cfdriver structures. We use this to detect duplicates
116 * when other cfdrivers are loaded.
117 */
118 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
119 extern struct cfdriver * const cfdriver_list_initial[];
120
121 /*
122 * List of cfdata tables. We always have one such list -- the one
123 * built statically when the kernel was configured.
124 */
125 struct cftablelist allcftables;
126 static struct cftable initcftable;
127
128 #define ROOT ((struct device *)NULL)
129
130 struct matchinfo {
131 cfmatch_t fn;
132 struct device *parent;
133 void *aux;
134 struct cfdata *match;
135 int pri;
136 };
137
138 static char *number(char *, int);
139 static void mapply(struct matchinfo *, struct cfdata *);
140
141 struct deferred_config {
142 TAILQ_ENTRY(deferred_config) dc_queue;
143 struct device *dc_dev;
144 void (*dc_func)(struct device *);
145 };
146
147 TAILQ_HEAD(deferred_config_head, deferred_config);
148
149 struct deferred_config_head deferred_config_queue;
150 struct deferred_config_head interrupt_config_queue;
151
152 static void config_process_deferred(struct deferred_config_head *,
153 struct device *);
154
155 /* Hooks to finalize configuration once all real devices have been found. */
156 struct finalize_hook {
157 TAILQ_ENTRY(finalize_hook) f_list;
158 int (*f_func)(struct device *);
159 struct device *f_dev;
160 };
161 static TAILQ_HEAD(, finalize_hook) config_finalize_list;
162 static int config_finalize_done;
163
164 /* list of all devices */
165 struct devicelist alldevs;
166
167 /* list of all events */
168 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
169
170 __volatile int config_pending; /* semaphore for mountroot */
171
172 #define STREQ(s1, s2) \
173 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
174
175 static int config_initialized; /* config_init() has been called. */
176
177 /*
178 * Initialize the autoconfiguration data structures. Normally this
179 * is done by configure(), but some platforms need to do this very
180 * early (to e.g. initialize the console).
181 */
182 void
183 config_init(void)
184 {
185 int i;
186
187 if (config_initialized)
188 return;
189
190 /* allcfdrivers is statically initialized. */
191 for (i = 0; cfdriver_list_initial[i] != NULL; i++)
192 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
193 panic("configure: duplicate `%s' drivers",
194 cfdriver_list_initial[i]->cd_name);
195
196 TAILQ_INIT(&allcftables);
197 initcftable.ct_cfdata = cfdata;
198 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
199
200 TAILQ_INIT(&deferred_config_queue);
201 TAILQ_INIT(&interrupt_config_queue);
202 TAILQ_INIT(&config_finalize_list);
203 TAILQ_INIT(&alldevs);
204
205 config_initialized = 1;
206 }
207
208 /*
209 * Configure the system's hardware.
210 */
211 void
212 configure(void)
213 {
214
215 /* Initialize data structures. */
216 config_init();
217
218 #ifdef USERCONF
219 if (boothowto & RB_USERCONF)
220 user_config();
221 #endif
222
223 /*
224 * Do the machine-dependent portion of autoconfiguration. This
225 * sets the configuration machinery here in motion by "finding"
226 * the root bus. When this function returns, we expect interrupts
227 * to be enabled.
228 */
229 cpu_configure();
230
231 /*
232 * Now that we've found all the hardware, start the real time
233 * and statistics clocks.
234 */
235 initclocks();
236
237 cold = 0; /* clocks are running, we're warm now! */
238
239 /*
240 * Now callback to finish configuration for devices which want
241 * to do this once interrupts are enabled.
242 */
243 config_process_deferred(&interrupt_config_queue, NULL);
244 }
245
246 /*
247 * Add a cfdriver to the system.
248 */
249 int
250 config_cfdriver_attach(struct cfdriver *cd)
251 {
252 struct cfdriver *lcd;
253
254 /* Make sure this driver isn't already in the system. */
255 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
256 if (STREQ(lcd->cd_name, cd->cd_name))
257 return (EEXIST);
258 }
259
260 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
261
262 return (0);
263 }
264
265 /*
266 * Remove a cfdriver from the system.
267 */
268 int
269 config_cfdriver_detach(struct cfdriver *cd)
270 {
271 int i;
272
273 /* Make sure there are no active instances. */
274 for (i = 0; i < cd->cd_ndevs; i++) {
275 if (cd->cd_devs[i] != NULL)
276 return (EBUSY);
277 }
278
279 LIST_REMOVE(cd, cd_list);
280
281 KASSERT(cd->cd_devs == NULL);
282
283 return (0);
284 }
285
286 /*
287 * Look up a cfdriver by name.
288 */
289 static struct cfdriver *
290 config_cfdriver_lookup(const char *name)
291 {
292 struct cfdriver *cd;
293
294 /*
295 * It is sometimes necessary to use the autoconfiguration
296 * framework quite early (e.g. to initialize the console).
297 * We support this by noticing an empty cfdriver list and
298 * searching the initial static list instead.
299 */
300 if (LIST_EMPTY(&allcfdrivers)) {
301 int i;
302
303 for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
304 if (STREQ(cfdriver_list_initial[i]->cd_name, name))
305 return (cfdriver_list_initial[i]);
306 }
307 }
308
309 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
310 if (STREQ(cd->cd_name, name))
311 return (cd);
312 }
313
314 return (NULL);
315 }
316
317 /*
318 * Apply the matching function and choose the best. This is used
319 * a few times and we want to keep the code small.
320 */
321 static void
322 mapply(struct matchinfo *m, struct cfdata *cf)
323 {
324 int pri;
325
326 if (m->fn != NULL)
327 pri = (*m->fn)(m->parent, cf, m->aux);
328 else {
329 if (cf->cf_attach->ca_match == NULL) {
330 panic("mapply: no match function for '%s' device",
331 cf->cf_name);
332 }
333 pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
334 }
335 if (pri > m->pri) {
336 m->match = cf;
337 m->pri = pri;
338 }
339 }
340
341 /*
342 * Determine if `parent' is a potential parent for a device spec based
343 * on `cfp'.
344 */
345 static int
346 cfparent_match(struct device *parent, const struct cfparent *cfp)
347 {
348 struct cfdriver *pcd;
349 const char * const *cpp;
350 const char *cp;
351
352 /* We don't match root nodes here. */
353 if (cfp == NULL)
354 return (0);
355
356 pcd = config_cfdriver_lookup(parent->dv_cfdata->cf_name);
357 KASSERT(pcd != NULL);
358
359 /*
360 * First, ensure this parent has the correct interface
361 * attribute.
362 */
363 if (pcd->cd_attrs == NULL)
364 return (0); /* no interface attributes -> no children */
365 for (cpp = pcd->cd_attrs; (cp = *cpp) != NULL; cpp++) {
366 if (STREQ(cp, cfp->cfp_iattr)) {
367 /* Match. */
368 break;
369 }
370 }
371 if (cp == NULL)
372 return (0); /* doesn't carry the req'd attribute */
373
374 /*
375 * If no specific parent device instance was specified (i.e.
376 * we're attaching to the attribute only), we're done!
377 */
378 if (cfp->cfp_parent == NULL)
379 return (1);
380
381 /*
382 * Check the parent device's name.
383 */
384 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
385 return (0); /* not the same parent */
386
387 /*
388 * Make sure the unit number matches.
389 */
390 if (cfp->cfp_unit == -1 || /* wildcard */
391 cfp->cfp_unit == parent->dv_unit)
392 return (1);
393
394 /* Unit numbers don't match. */
395 return (0);
396 }
397
398 /*
399 * Invoke the "match" routine for a cfdata entry on behalf of
400 * an external caller, usually a "submatch" routine.
401 */
402 int
403 config_match(struct device *parent, struct cfdata *cf, void *aux)
404 {
405
406 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
407 }
408
409 /*
410 * Iterate over all potential children of some device, calling the given
411 * function (default being the child's match function) for each one.
412 * Nonzero returns are matches; the highest value returned is considered
413 * the best match. Return the `found child' if we got a match, or NULL
414 * otherwise. The `aux' pointer is simply passed on through.
415 *
416 * Note that this function is designed so that it can be used to apply
417 * an arbitrary function to all potential children (its return value
418 * can be ignored).
419 */
420 struct cfdata *
421 config_search(cfmatch_t fn, struct device *parent, void *aux)
422 {
423 struct cftable *ct;
424 struct cfdata *cf;
425 struct matchinfo m;
426
427 KASSERT(config_initialized);
428
429 m.fn = fn;
430 m.parent = parent;
431 m.aux = aux;
432 m.match = NULL;
433 m.pri = 0;
434
435 TAILQ_FOREACH(ct, &allcftables, ct_list) {
436 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
437 /*
438 * Skip cf if no longer eligible, otherwise scan
439 * through parents for one matching `parent', and
440 * try match function.
441 */
442 if (cf->cf_fstate == FSTATE_FOUND)
443 continue;
444 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
445 cf->cf_fstate == FSTATE_DSTAR)
446 continue;
447 if (cfparent_match(parent, cf->cf_pspec))
448 mapply(&m, cf);
449 }
450 }
451 return (m.match);
452 }
453
454 /*
455 * Find the given root device.
456 * This is much like config_search, but there is no parent.
457 * Don't bother with multiple cfdata tables; the root node
458 * must always be in the initial table.
459 */
460 struct cfdata *
461 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
462 {
463 struct cfdata *cf;
464 short *p;
465 struct matchinfo m;
466
467 m.fn = fn;
468 m.parent = ROOT;
469 m.aux = aux;
470 m.match = NULL;
471 m.pri = 0;
472 /*
473 * Look at root entries for matching name. We do not bother
474 * with found-state here since only one root should ever be
475 * searched (and it must be done first).
476 */
477 for (p = cfroots; *p >= 0; p++) {
478 cf = &cfdata[*p];
479 if (strcmp(cf->cf_name, rootname) == 0)
480 mapply(&m, cf);
481 }
482 return (m.match);
483 }
484
485 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
486
487 /*
488 * The given `aux' argument describes a device that has been found
489 * on the given parent, but not necessarily configured. Locate the
490 * configuration data for that device (using the submatch function
491 * provided, or using candidates' cd_match configuration driver
492 * functions) and attach it, and return true. If the device was
493 * not configured, call the given `print' function and return 0.
494 */
495 struct device *
496 config_found_sm(struct device *parent, void *aux, cfprint_t print,
497 cfmatch_t submatch)
498 {
499 struct cfdata *cf;
500
501 if ((cf = config_search(submatch, parent, aux)) != NULL)
502 return (config_attach(parent, cf, aux, print));
503 if (print)
504 printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
505 return (NULL);
506 }
507
508 /*
509 * As above, but for root devices.
510 */
511 struct device *
512 config_rootfound(const char *rootname, void *aux)
513 {
514 struct cfdata *cf;
515
516 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
517 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
518 printf("root device %s not configured\n", rootname);
519 return (NULL);
520 }
521
522 /* just like sprintf(buf, "%d") except that it works from the end */
523 static char *
524 number(char *ep, int n)
525 {
526
527 *--ep = 0;
528 while (n >= 10) {
529 *--ep = (n % 10) + '0';
530 n /= 10;
531 }
532 *--ep = n + '0';
533 return (ep);
534 }
535
536 /*
537 * Expand the size of the cd_devs array if necessary.
538 */
539 void
540 config_makeroom(int n, struct cfdriver *cd)
541 {
542 int old, new;
543 void **nsp;
544
545 if (n < cd->cd_ndevs)
546 return;
547
548 /*
549 * Need to expand the array.
550 */
551 old = cd->cd_ndevs;
552 if (old == 0)
553 new = MINALLOCSIZE / sizeof(void *);
554 else
555 new = old * 2;
556 while (new <= n)
557 new *= 2;
558 cd->cd_ndevs = new;
559 nsp = malloc(new * sizeof(void *), M_DEVBUF,
560 cold ? M_NOWAIT : M_WAITOK);
561 if (nsp == NULL)
562 panic("config_attach: %sing dev array",
563 old != 0 ? "expand" : "creat");
564 memset(nsp + old, 0, (new - old) * sizeof(void *));
565 if (old != 0) {
566 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
567 free(cd->cd_devs, M_DEVBUF);
568 }
569 cd->cd_devs = nsp;
570 }
571
572 /*
573 * Attach a found device. Allocates memory for device variables.
574 */
575 struct device *
576 config_attach(struct device *parent, struct cfdata *cf, void *aux,
577 cfprint_t print)
578 {
579 struct device *dev;
580 struct cftable *ct;
581 struct cfdriver *cd;
582 const struct cfattach *ca;
583 size_t lname, lunit;
584 const char *xunit;
585 int myunit;
586 char num[10];
587
588 cd = config_cfdriver_lookup(cf->cf_name);
589 KASSERT(cd != NULL);
590 ca = cf->cf_attach;
591 if (ca->ca_devsize < sizeof(struct device))
592 panic("config_attach");
593
594 #ifndef __BROKEN_CONFIG_UNIT_USAGE
595 if (cf->cf_fstate == FSTATE_STAR) {
596 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
597 if (cd->cd_devs[myunit] == NULL)
598 break;
599 /*
600 * myunit is now the unit of the first NULL device pointer,
601 * or max(cd->cd_ndevs,cf->cf_unit).
602 */
603 } else {
604 myunit = cf->cf_unit;
605 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
606 cf->cf_fstate = FSTATE_FOUND;
607 }
608 #else
609 myunit = cf->cf_unit;
610 if (cf->cf_fstate == FSTATE_STAR)
611 cf->cf_unit++;
612 else {
613 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
614 cf->cf_fstate = FSTATE_FOUND;
615 }
616 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
617
618 /* compute length of name and decimal expansion of unit number */
619 lname = strlen(cd->cd_name);
620 xunit = number(&num[sizeof(num)], myunit);
621 lunit = &num[sizeof(num)] - xunit;
622 if (lname + lunit > sizeof(dev->dv_xname))
623 panic("config_attach: device name too long");
624
625 /* get memory for all device vars */
626 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
627 cold ? M_NOWAIT : M_WAITOK);
628 if (!dev)
629 panic("config_attach: memory allocation for device softc failed");
630 memset(dev, 0, ca->ca_devsize);
631 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
632 dev->dv_class = cd->cd_class;
633 dev->dv_cfdata = cf;
634 dev->dv_unit = myunit;
635 memcpy(dev->dv_xname, cd->cd_name, lname);
636 memcpy(dev->dv_xname + lname, xunit, lunit);
637 dev->dv_parent = parent;
638 dev->dv_flags = DVF_ACTIVE; /* always initially active */
639
640 if (parent == ROOT)
641 printf("%s (root)", dev->dv_xname);
642 else {
643 printf("%s at %s", dev->dv_xname, parent->dv_xname);
644 if (print)
645 (void) (*print)(aux, NULL);
646 }
647
648 /* put this device in the devices array */
649 config_makeroom(dev->dv_unit, cd);
650 if (cd->cd_devs[dev->dv_unit])
651 panic("config_attach: duplicate %s", dev->dv_xname);
652 cd->cd_devs[dev->dv_unit] = dev;
653
654 /*
655 * Before attaching, clobber any unfound devices that are
656 * otherwise identical.
657 */
658 TAILQ_FOREACH(ct, &allcftables, ct_list) {
659 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
660 if (STREQ(cf->cf_name, cd->cd_name) &&
661 cf->cf_unit == dev->dv_unit) {
662 if (cf->cf_fstate == FSTATE_NOTFOUND)
663 cf->cf_fstate = FSTATE_FOUND;
664 #ifdef __BROKEN_CONFIG_UNIT_USAGE
665 /*
666 * Bump the unit number on all starred cfdata
667 * entries for this device.
668 */
669 if (cf->cf_fstate == FSTATE_STAR)
670 cf->cf_unit++;
671 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
672 }
673 }
674 }
675 #ifdef __HAVE_DEVICE_REGISTER
676 device_register(dev, aux);
677 #endif
678 (*ca->ca_attach)(parent, dev, aux);
679 config_process_deferred(&deferred_config_queue, dev);
680 return (dev);
681 }
682
683 /*
684 * Detach a device. Optionally forced (e.g. because of hardware
685 * removal) and quiet. Returns zero if successful, non-zero
686 * (an error code) otherwise.
687 *
688 * Note that this code wants to be run from a process context, so
689 * that the detach can sleep to allow processes which have a device
690 * open to run and unwind their stacks.
691 */
692 int
693 config_detach(struct device *dev, int flags)
694 {
695 struct cftable *ct;
696 struct cfdata *cf;
697 const struct cfattach *ca;
698 struct cfdriver *cd;
699 #ifdef DIAGNOSTIC
700 struct device *d;
701 #endif
702 int rv = 0, i;
703
704 cf = dev->dv_cfdata;
705 #ifdef DIAGNOSTIC
706 if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
707 panic("config_detach: bad device fstate");
708 #endif
709 cd = config_cfdriver_lookup(cf->cf_name);
710 KASSERT(cd != NULL);
711 ca = cf->cf_attach;
712
713 /*
714 * Ensure the device is deactivated. If the device doesn't
715 * have an activation entry point, we allow DVF_ACTIVE to
716 * remain set. Otherwise, if DVF_ACTIVE is still set, the
717 * device is busy, and the detach fails.
718 */
719 if (ca->ca_activate != NULL)
720 rv = config_deactivate(dev);
721
722 /*
723 * Try to detach the device. If that's not possible, then
724 * we either panic() (for the forced but failed case), or
725 * return an error.
726 */
727 if (rv == 0) {
728 if (ca->ca_detach != NULL)
729 rv = (*ca->ca_detach)(dev, flags);
730 else
731 rv = EOPNOTSUPP;
732 }
733 if (rv != 0) {
734 if ((flags & DETACH_FORCE) == 0)
735 return (rv);
736 else
737 panic("config_detach: forced detach of %s failed (%d)",
738 dev->dv_xname, rv);
739 }
740
741 /*
742 * The device has now been successfully detached.
743 */
744
745 #ifdef DIAGNOSTIC
746 /*
747 * Sanity: If you're successfully detached, you should have no
748 * children. (Note that because children must be attached
749 * after parents, we only need to search the latter part of
750 * the list.)
751 */
752 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
753 d = TAILQ_NEXT(d, dv_list)) {
754 if (d->dv_parent == dev) {
755 printf("config_detach: detached device %s"
756 " has children %s\n", dev->dv_xname, d->dv_xname);
757 panic("config_detach");
758 }
759 }
760 #endif
761
762 /*
763 * Mark cfdata to show that the unit can be reused, if possible.
764 */
765 TAILQ_FOREACH(ct, &allcftables, ct_list) {
766 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
767 if (STREQ(cf->cf_name, cd->cd_name)) {
768 if (cf->cf_fstate == FSTATE_FOUND &&
769 cf->cf_unit == dev->dv_unit)
770 cf->cf_fstate = FSTATE_NOTFOUND;
771 #ifdef __BROKEN_CONFIG_UNIT_USAGE
772 /*
773 * Note that we can only re-use a starred
774 * unit number if the unit being detached
775 * had the last assigned unit number.
776 */
777 if (cf->cf_fstate == FSTATE_STAR &&
778 cf->cf_unit == dev->dv_unit + 1)
779 cf->cf_unit--;
780 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
781 }
782 }
783 }
784
785 /*
786 * Unlink from device list.
787 */
788 TAILQ_REMOVE(&alldevs, dev, dv_list);
789
790 /*
791 * Remove from cfdriver's array, tell the world, and free softc.
792 */
793 cd->cd_devs[dev->dv_unit] = NULL;
794 if ((flags & DETACH_QUIET) == 0)
795 printf("%s detached\n", dev->dv_xname);
796 free(dev, M_DEVBUF);
797
798 /*
799 * If the device now has no units in use, deallocate its softc array.
800 */
801 for (i = 0; i < cd->cd_ndevs; i++)
802 if (cd->cd_devs[i] != NULL)
803 break;
804 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
805 free(cd->cd_devs, M_DEVBUF);
806 cd->cd_devs = NULL;
807 cd->cd_ndevs = 0;
808 }
809
810 /*
811 * Return success.
812 */
813 return (0);
814 }
815
816 int
817 config_activate(struct device *dev)
818 {
819 const struct cfattach *ca = dev->dv_cfdata->cf_attach;
820 int rv = 0, oflags = dev->dv_flags;
821
822 if (ca->ca_activate == NULL)
823 return (EOPNOTSUPP);
824
825 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
826 dev->dv_flags |= DVF_ACTIVE;
827 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
828 if (rv)
829 dev->dv_flags = oflags;
830 }
831 return (rv);
832 }
833
834 int
835 config_deactivate(struct device *dev)
836 {
837 const struct cfattach *ca = dev->dv_cfdata->cf_attach;
838 int rv = 0, oflags = dev->dv_flags;
839
840 if (ca->ca_activate == NULL)
841 return (EOPNOTSUPP);
842
843 if (dev->dv_flags & DVF_ACTIVE) {
844 dev->dv_flags &= ~DVF_ACTIVE;
845 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
846 if (rv)
847 dev->dv_flags = oflags;
848 }
849 return (rv);
850 }
851
852 /*
853 * Defer the configuration of the specified device until all
854 * of its parent's devices have been attached.
855 */
856 void
857 config_defer(struct device *dev, void (*func)(struct device *))
858 {
859 struct deferred_config *dc;
860
861 if (dev->dv_parent == NULL)
862 panic("config_defer: can't defer config of a root device");
863
864 #ifdef DIAGNOSTIC
865 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
866 dc = TAILQ_NEXT(dc, dc_queue)) {
867 if (dc->dc_dev == dev)
868 panic("config_defer: deferred twice");
869 }
870 #endif
871
872 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
873 if (dc == NULL)
874 panic("config_defer: unable to allocate callback");
875
876 dc->dc_dev = dev;
877 dc->dc_func = func;
878 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
879 config_pending_incr();
880 }
881
882 /*
883 * Defer some autoconfiguration for a device until after interrupts
884 * are enabled.
885 */
886 void
887 config_interrupts(struct device *dev, void (*func)(struct device *))
888 {
889 struct deferred_config *dc;
890
891 /*
892 * If interrupts are enabled, callback now.
893 */
894 if (cold == 0) {
895 (*func)(dev);
896 return;
897 }
898
899 #ifdef DIAGNOSTIC
900 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
901 dc = TAILQ_NEXT(dc, dc_queue)) {
902 if (dc->dc_dev == dev)
903 panic("config_interrupts: deferred twice");
904 }
905 #endif
906
907 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
908 if (dc == NULL)
909 panic("config_interrupts: unable to allocate callback");
910
911 dc->dc_dev = dev;
912 dc->dc_func = func;
913 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
914 config_pending_incr();
915 }
916
917 /*
918 * Process a deferred configuration queue.
919 */
920 static void
921 config_process_deferred(struct deferred_config_head *queue,
922 struct device *parent)
923 {
924 struct deferred_config *dc, *ndc;
925
926 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
927 ndc = TAILQ_NEXT(dc, dc_queue);
928 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
929 TAILQ_REMOVE(queue, dc, dc_queue);
930 (*dc->dc_func)(dc->dc_dev);
931 free(dc, M_DEVBUF);
932 config_pending_decr();
933 }
934 }
935 }
936
937 /*
938 * Manipulate the config_pending semaphore.
939 */
940 void
941 config_pending_incr(void)
942 {
943
944 config_pending++;
945 }
946
947 void
948 config_pending_decr(void)
949 {
950
951 #ifdef DIAGNOSTIC
952 if (config_pending == 0)
953 panic("config_pending_decr: config_pending == 0");
954 #endif
955 config_pending--;
956 if (config_pending == 0)
957 wakeup((void *)&config_pending);
958 }
959
960 /*
961 * Register a "finalization" routine. Finalization routines are
962 * called iteratively once all real devices have been found during
963 * autoconfiguration, for as long as any one finalizer has done
964 * any work.
965 */
966 int
967 config_finalize_register(struct device *dev, int (*fn)(struct device *))
968 {
969 struct finalize_hook *f;
970
971 /*
972 * If finalization has already been done, invoke the
973 * callback function now.
974 */
975 if (config_finalize_done) {
976 while ((*fn)(dev) != 0)
977 /* loop */ ;
978 }
979
980 /* Ensure this isn't already on the list. */
981 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
982 if (f->f_func == fn && f->f_dev == dev)
983 return (EEXIST);
984 }
985
986 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
987 f->f_func = fn;
988 f->f_dev = dev;
989 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
990
991 return (0);
992 }
993
994 void
995 config_finalize(void)
996 {
997 struct finalize_hook *f;
998 int rv;
999
1000 /* Run the hooks until none of them does any work. */
1001 do {
1002 rv = 0;
1003 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1004 rv |= (*f->f_func)(f->f_dev);
1005 } while (rv != 0);
1006
1007 config_finalize_done = 1;
1008
1009 /* Now free all the hooks. */
1010 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1011 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1012 free(f, M_TEMP);
1013 }
1014 }
1015
1016 /*
1017 * Attach a statically-initialized event. The type and string pointers
1018 * are already set up.
1019 */
1020 void
1021 evcnt_attach_static(struct evcnt *ev)
1022 {
1023 int len;
1024
1025 len = strlen(ev->ev_group);
1026 #ifdef DIAGNOSTIC
1027 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
1028 panic("evcnt_attach_static: group length (%s)", ev->ev_group);
1029 #endif
1030 ev->ev_grouplen = len;
1031
1032 len = strlen(ev->ev_name);
1033 #ifdef DIAGNOSTIC
1034 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
1035 panic("evcnt_attach_static: name length (%s)", ev->ev_name);
1036 #endif
1037 ev->ev_namelen = len;
1038
1039 TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
1040 }
1041
1042 /*
1043 * Attach a dynamically-initialized event. Zero it, set up the type
1044 * and string pointers and then act like it was statically initialized.
1045 */
1046 void
1047 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
1048 const char *group, const char *name)
1049 {
1050
1051 memset(ev, 0, sizeof *ev);
1052 ev->ev_type = type;
1053 ev->ev_parent = parent;
1054 ev->ev_group = group;
1055 ev->ev_name = name;
1056 evcnt_attach_static(ev);
1057 }
1058
1059 /*
1060 * Detach an event.
1061 */
1062 void
1063 evcnt_detach(struct evcnt *ev)
1064 {
1065
1066 TAILQ_REMOVE(&allevents, ev, ev_list);
1067 }
1068
1069 #ifdef DDB
1070 void
1071 event_print(int full, void (*pr)(const char *, ...))
1072 {
1073 struct evcnt *evp;
1074
1075 TAILQ_FOREACH(evp, &allevents, ev_list) {
1076 if (evp->ev_count == 0 && !full)
1077 continue;
1078
1079 (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
1080 evp->ev_group, evp->ev_name, evp->ev_count);
1081 }
1082 }
1083 #endif /* DDB */
1084