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