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