subr_autoconf.c revision 1.118 1 /* $NetBSD: subr_autoconf.c,v 1.118 2007/06/24 01:43:35 dyoung 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. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
75 *
76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.118 2007/06/24 01:43:35 dyoung Exp $");
81
82 #include "opt_ddb.h"
83
84 #include <sys/param.h>
85 #include <sys/device.h>
86 #include <sys/disklabel.h>
87 #include <sys/conf.h>
88 #include <sys/kauth.h>
89 #include <sys/malloc.h>
90 #include <sys/systm.h>
91 #include <sys/kernel.h>
92 #include <sys/errno.h>
93 #include <sys/proc.h>
94 #include <sys/reboot.h>
95
96 #include <sys/buf.h>
97 #include <sys/dirent.h>
98 #include <sys/lock.h>
99 #include <sys/vnode.h>
100 #include <sys/mount.h>
101 #include <sys/namei.h>
102 #include <sys/unistd.h>
103 #include <sys/fcntl.h>
104 #include <sys/lockf.h>
105
106 #include <sys/disk.h>
107
108 #include <machine/limits.h>
109
110 #include "opt_userconf.h"
111 #ifdef USERCONF
112 #include <sys/userconf.h>
113 #endif
114
115 #ifdef __i386__
116 #include "opt_splash.h"
117 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
118 #include <dev/splash/splash.h>
119 extern struct splash_progress *splash_progress_state;
120 #endif
121 #endif
122
123 /*
124 * Autoconfiguration subroutines.
125 */
126
127 /*
128 * ioconf.c exports exactly two names: cfdata and cfroots. All system
129 * devices and drivers are found via these tables.
130 */
131 extern struct cfdata cfdata[];
132 extern const short cfroots[];
133
134 /*
135 * List of all cfdriver structures. We use this to detect duplicates
136 * when other cfdrivers are loaded.
137 */
138 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
139 extern struct cfdriver * const cfdriver_list_initial[];
140
141 /*
142 * Initial list of cfattach's.
143 */
144 extern const struct cfattachinit cfattachinit[];
145
146 /*
147 * List of cfdata tables. We always have one such list -- the one
148 * built statically when the kernel was configured.
149 */
150 struct cftablelist allcftables;
151 static struct cftable initcftable;
152
153 #define ROOT ((device_t)NULL)
154
155 struct matchinfo {
156 cfsubmatch_t fn;
157 struct device *parent;
158 const int *locs;
159 void *aux;
160 struct cfdata *match;
161 int pri;
162 };
163
164 static char *number(char *, int);
165 static void mapply(struct matchinfo *, cfdata_t);
166 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
167 static void config_devdealloc(device_t);
168 static void config_makeroom(int, struct cfdriver *);
169 static void config_devlink(device_t);
170 static void config_devunlink(device_t);
171
172 struct deferred_config {
173 TAILQ_ENTRY(deferred_config) dc_queue;
174 device_t dc_dev;
175 void (*dc_func)(device_t);
176 };
177
178 TAILQ_HEAD(deferred_config_head, deferred_config);
179
180 struct deferred_config_head deferred_config_queue;
181 struct deferred_config_head interrupt_config_queue;
182
183 static void config_process_deferred(struct deferred_config_head *, device_t);
184
185 /* Hooks to finalize configuration once all real devices have been found. */
186 struct finalize_hook {
187 TAILQ_ENTRY(finalize_hook) f_list;
188 int (*f_func)(device_t);
189 device_t f_dev;
190 };
191 static TAILQ_HEAD(, finalize_hook) config_finalize_list;
192 static int config_finalize_done;
193
194 /* list of all devices */
195 struct devicelist alldevs;
196
197 volatile int config_pending; /* semaphore for mountroot */
198
199 #define STREQ(s1, s2) \
200 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
201
202 static int config_initialized; /* config_init() has been called. */
203
204 static int config_do_twiddle;
205
206 struct vnode *
207 opendisk(struct device *dv)
208 {
209 int bmajor, bminor;
210 struct vnode *tmpvn;
211 int error;
212 dev_t dev;
213
214 /*
215 * Lookup major number for disk block device.
216 */
217 bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
218 if (bmajor == -1)
219 return NULL;
220
221 bminor = minor(device_unit(dv));
222 /*
223 * Fake a temporary vnode for the disk, open it, and read
224 * and hash the sectors.
225 */
226 dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) :
227 MAKEDISKDEV(bmajor, bminor, RAW_PART);
228 if (bdevvp(dev, &tmpvn))
229 panic("%s: can't alloc vnode for %s", __func__,
230 device_xname(dv));
231 error = VOP_OPEN(tmpvn, FREAD, NOCRED, 0);
232 if (error) {
233 #ifndef DEBUG
234 /*
235 * Ignore errors caused by missing device, partition,
236 * or medium.
237 */
238 if (error != ENXIO && error != ENODEV)
239 #endif
240 printf("%s: can't open dev %s (%d)\n",
241 __func__, device_xname(dv), error);
242 vput(tmpvn);
243 return NULL;
244 }
245
246 return tmpvn;
247 }
248
249 int
250 config_handle_wedges(struct device *dv, int par)
251 {
252 struct dkwedge_list wl;
253 struct dkwedge_info *wi;
254 struct vnode *vn;
255 char diskname[16];
256 int i, error;
257
258 if ((vn = opendisk(dv)) == NULL)
259 return -1;
260
261 wl.dkwl_bufsize = sizeof(*wi) * 16;
262 wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK);
263
264 error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED, 0);
265 VOP_CLOSE(vn, FREAD, NOCRED, 0);
266 vput(vn);
267 if (error) {
268 #ifdef DEBUG_WEDGE
269 printf("%s: List wedges returned %d\n",
270 device_xname(dv), error);
271 #endif
272 free(wi, M_TEMP);
273 return -1;
274 }
275
276 #ifdef DEBUG_WEDGE
277 printf("%s: Returned %u(%u) wedges\n", device_xname(dv),
278 wl.dkwl_nwedges, wl.dkwl_ncopied);
279 #endif
280 snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv),
281 par + 'a');
282
283 for (i = 0; i < wl.dkwl_ncopied; i++) {
284 #ifdef DEBUG_WEDGE
285 printf("%s: Looking for %s in %s\n",
286 device_xname(dv), diskname, wi[i].dkw_wname);
287 #endif
288 if (strcmp(wi[i].dkw_wname, diskname) == 0)
289 break;
290 }
291
292 if (i == wl.dkwl_ncopied) {
293 #ifdef DEBUG_WEDGE
294 printf("%s: Cannot find wedge with parent %s\n",
295 device_xname(dv), diskname);
296 #endif
297 free(wi, M_TEMP);
298 return -1;
299 }
300
301 #ifdef DEBUG_WEDGE
302 printf("%s: Setting boot wedge %s (%s) at %llu %llu\n",
303 device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname,
304 (unsigned long long)wi[i].dkw_offset,
305 (unsigned long long)wi[i].dkw_size);
306 #endif
307 dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size);
308 free(wi, M_TEMP);
309 return 0;
310 }
311
312 /*
313 * Initialize the autoconfiguration data structures. Normally this
314 * is done by configure(), but some platforms need to do this very
315 * early (to e.g. initialize the console).
316 */
317 void
318 config_init(void)
319 {
320 const struct cfattachinit *cfai;
321 int i, j;
322
323 if (config_initialized)
324 return;
325
326 /* allcfdrivers is statically initialized. */
327 for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
328 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
329 panic("configure: duplicate `%s' drivers",
330 cfdriver_list_initial[i]->cd_name);
331 }
332
333 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
334 for (j = 0; cfai->cfai_list[j] != NULL; j++) {
335 if (config_cfattach_attach(cfai->cfai_name,
336 cfai->cfai_list[j]) != 0)
337 panic("configure: duplicate `%s' attachment "
338 "of `%s' driver",
339 cfai->cfai_list[j]->ca_name,
340 cfai->cfai_name);
341 }
342 }
343
344 TAILQ_INIT(&allcftables);
345 initcftable.ct_cfdata = cfdata;
346 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
347
348 TAILQ_INIT(&deferred_config_queue);
349 TAILQ_INIT(&interrupt_config_queue);
350 TAILQ_INIT(&config_finalize_list);
351 TAILQ_INIT(&alldevs);
352
353 config_initialized = 1;
354 }
355
356 /*
357 * Configure the system's hardware.
358 */
359 void
360 configure(void)
361 {
362 int errcnt;
363
364 /* Initialize data structures. */
365 config_init();
366
367 #ifdef USERCONF
368 if (boothowto & RB_USERCONF)
369 user_config();
370 #endif
371
372 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
373 config_do_twiddle = 1;
374 printf_nolog("Detecting hardware...");
375 }
376
377 /*
378 * Do the machine-dependent portion of autoconfiguration. This
379 * sets the configuration machinery here in motion by "finding"
380 * the root bus. When this function returns, we expect interrupts
381 * to be enabled.
382 */
383 cpu_configure();
384
385 /*
386 * Now that we've found all the hardware, start the real time
387 * and statistics clocks.
388 */
389 initclocks();
390
391 cold = 0; /* clocks are running, we're warm now! */
392
393 /*
394 * Now callback to finish configuration for devices which want
395 * to do this once interrupts are enabled.
396 */
397 config_process_deferred(&interrupt_config_queue, NULL);
398
399 errcnt = aprint_get_error_count();
400 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
401 (boothowto & AB_VERBOSE) == 0) {
402 if (config_do_twiddle) {
403 config_do_twiddle = 0;
404 printf_nolog("done.\n");
405 }
406 if (errcnt != 0) {
407 printf("WARNING: %d error%s while detecting hardware; "
408 "check system log.\n", errcnt,
409 errcnt == 1 ? "" : "s");
410 }
411 }
412 }
413
414 /*
415 * Add a cfdriver to the system.
416 */
417 int
418 config_cfdriver_attach(struct cfdriver *cd)
419 {
420 struct cfdriver *lcd;
421
422 /* Make sure this driver isn't already in the system. */
423 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
424 if (STREQ(lcd->cd_name, cd->cd_name))
425 return (EEXIST);
426 }
427
428 LIST_INIT(&cd->cd_attach);
429 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
430
431 return (0);
432 }
433
434 /*
435 * Remove a cfdriver from the system.
436 */
437 int
438 config_cfdriver_detach(struct cfdriver *cd)
439 {
440 int i;
441
442 /* Make sure there are no active instances. */
443 for (i = 0; i < cd->cd_ndevs; i++) {
444 if (cd->cd_devs[i] != NULL)
445 return (EBUSY);
446 }
447
448 /* ...and no attachments loaded. */
449 if (LIST_EMPTY(&cd->cd_attach) == 0)
450 return (EBUSY);
451
452 LIST_REMOVE(cd, cd_list);
453
454 KASSERT(cd->cd_devs == NULL);
455
456 return (0);
457 }
458
459 /*
460 * Look up a cfdriver by name.
461 */
462 struct cfdriver *
463 config_cfdriver_lookup(const char *name)
464 {
465 struct cfdriver *cd;
466
467 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
468 if (STREQ(cd->cd_name, name))
469 return (cd);
470 }
471
472 return (NULL);
473 }
474
475 /*
476 * Add a cfattach to the specified driver.
477 */
478 int
479 config_cfattach_attach(const char *driver, struct cfattach *ca)
480 {
481 struct cfattach *lca;
482 struct cfdriver *cd;
483
484 cd = config_cfdriver_lookup(driver);
485 if (cd == NULL)
486 return (ESRCH);
487
488 /* Make sure this attachment isn't already on this driver. */
489 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
490 if (STREQ(lca->ca_name, ca->ca_name))
491 return (EEXIST);
492 }
493
494 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
495
496 return (0);
497 }
498
499 /*
500 * Remove a cfattach from the specified driver.
501 */
502 int
503 config_cfattach_detach(const char *driver, struct cfattach *ca)
504 {
505 struct cfdriver *cd;
506 device_t dev;
507 int i;
508
509 cd = config_cfdriver_lookup(driver);
510 if (cd == NULL)
511 return (ESRCH);
512
513 /* Make sure there are no active instances. */
514 for (i = 0; i < cd->cd_ndevs; i++) {
515 if ((dev = cd->cd_devs[i]) == NULL)
516 continue;
517 if (dev->dv_cfattach == ca)
518 return (EBUSY);
519 }
520
521 LIST_REMOVE(ca, ca_list);
522
523 return (0);
524 }
525
526 /*
527 * Look up a cfattach by name.
528 */
529 static struct cfattach *
530 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
531 {
532 struct cfattach *ca;
533
534 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
535 if (STREQ(ca->ca_name, atname))
536 return (ca);
537 }
538
539 return (NULL);
540 }
541
542 /*
543 * Look up a cfattach by driver/attachment name.
544 */
545 struct cfattach *
546 config_cfattach_lookup(const char *name, const char *atname)
547 {
548 struct cfdriver *cd;
549
550 cd = config_cfdriver_lookup(name);
551 if (cd == NULL)
552 return (NULL);
553
554 return (config_cfattach_lookup_cd(cd, atname));
555 }
556
557 /*
558 * Apply the matching function and choose the best. This is used
559 * a few times and we want to keep the code small.
560 */
561 static void
562 mapply(struct matchinfo *m, cfdata_t cf)
563 {
564 int pri;
565
566 if (m->fn != NULL) {
567 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
568 } else {
569 pri = config_match(m->parent, cf, m->aux);
570 }
571 if (pri > m->pri) {
572 m->match = cf;
573 m->pri = pri;
574 }
575 }
576
577 int
578 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
579 {
580 const struct cfiattrdata *ci;
581 const struct cflocdesc *cl;
582 int nlocs, i;
583
584 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
585 KASSERT(ci);
586 nlocs = ci->ci_loclen;
587 for (i = 0; i < nlocs; i++) {
588 cl = &ci->ci_locdesc[i];
589 /* !cld_defaultstr means no default value */
590 if ((!(cl->cld_defaultstr)
591 || (cf->cf_loc[i] != cl->cld_default))
592 && cf->cf_loc[i] != locs[i])
593 return (0);
594 }
595
596 return (config_match(parent, cf, aux));
597 }
598
599 /*
600 * Helper function: check whether the driver supports the interface attribute
601 * and return its descriptor structure.
602 */
603 static const struct cfiattrdata *
604 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
605 {
606 const struct cfiattrdata * const *cpp;
607
608 if (cd->cd_attrs == NULL)
609 return (0);
610
611 for (cpp = cd->cd_attrs; *cpp; cpp++) {
612 if (STREQ((*cpp)->ci_name, ia)) {
613 /* Match. */
614 return (*cpp);
615 }
616 }
617 return (0);
618 }
619
620 /*
621 * Lookup an interface attribute description by name.
622 * If the driver is given, consider only its supported attributes.
623 */
624 const struct cfiattrdata *
625 cfiattr_lookup(const char *name, const struct cfdriver *cd)
626 {
627 const struct cfdriver *d;
628 const struct cfiattrdata *ia;
629
630 if (cd)
631 return (cfdriver_get_iattr(cd, name));
632
633 LIST_FOREACH(d, &allcfdrivers, cd_list) {
634 ia = cfdriver_get_iattr(d, name);
635 if (ia)
636 return (ia);
637 }
638 return (0);
639 }
640
641 /*
642 * Determine if `parent' is a potential parent for a device spec based
643 * on `cfp'.
644 */
645 static int
646 cfparent_match(const device_t parent, const struct cfparent *cfp)
647 {
648 struct cfdriver *pcd;
649
650 /* We don't match root nodes here. */
651 if (cfp == NULL)
652 return (0);
653
654 pcd = parent->dv_cfdriver;
655 KASSERT(pcd != NULL);
656
657 /*
658 * First, ensure this parent has the correct interface
659 * attribute.
660 */
661 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
662 return (0);
663
664 /*
665 * If no specific parent device instance was specified (i.e.
666 * we're attaching to the attribute only), we're done!
667 */
668 if (cfp->cfp_parent == NULL)
669 return (1);
670
671 /*
672 * Check the parent device's name.
673 */
674 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
675 return (0); /* not the same parent */
676
677 /*
678 * Make sure the unit number matches.
679 */
680 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
681 cfp->cfp_unit == parent->dv_unit)
682 return (1);
683
684 /* Unit numbers don't match. */
685 return (0);
686 }
687
688 /*
689 * Helper for config_cfdata_attach(): check all devices whether it could be
690 * parent any attachment in the config data table passed, and rescan.
691 */
692 static void
693 rescan_with_cfdata(const struct cfdata *cf)
694 {
695 device_t d;
696 const struct cfdata *cf1;
697
698 /*
699 * "alldevs" is likely longer than an LKM's cfdata, so make it
700 * the outer loop.
701 */
702 TAILQ_FOREACH(d, &alldevs, dv_list) {
703
704 if (!(d->dv_cfattach->ca_rescan))
705 continue;
706
707 for (cf1 = cf; cf1->cf_name; cf1++) {
708
709 if (!cfparent_match(d, cf1->cf_pspec))
710 continue;
711
712 (*d->dv_cfattach->ca_rescan)(d,
713 cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
714 }
715 }
716 }
717
718 /*
719 * Attach a supplemental config data table and rescan potential
720 * parent devices if required.
721 */
722 int
723 config_cfdata_attach(cfdata_t cf, int scannow)
724 {
725 struct cftable *ct;
726
727 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
728 ct->ct_cfdata = cf;
729 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
730
731 if (scannow)
732 rescan_with_cfdata(cf);
733
734 return (0);
735 }
736
737 /*
738 * Helper for config_cfdata_detach: check whether a device is
739 * found through any attachment in the config data table.
740 */
741 static int
742 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
743 {
744 const struct cfdata *cf1;
745
746 for (cf1 = cf; cf1->cf_name; cf1++)
747 if (d->dv_cfdata == cf1)
748 return (1);
749
750 return (0);
751 }
752
753 /*
754 * Detach a supplemental config data table. Detach all devices found
755 * through that table (and thus keeping references to it) before.
756 */
757 int
758 config_cfdata_detach(cfdata_t cf)
759 {
760 device_t d;
761 int error;
762 struct cftable *ct;
763
764 again:
765 TAILQ_FOREACH(d, &alldevs, dv_list) {
766 if (dev_in_cfdata(d, cf)) {
767 error = config_detach(d, 0);
768 if (error) {
769 aprint_error("%s: unable to detach instance\n",
770 d->dv_xname);
771 return (error);
772 }
773 goto again;
774 }
775 }
776
777 TAILQ_FOREACH(ct, &allcftables, ct_list) {
778 if (ct->ct_cfdata == cf) {
779 TAILQ_REMOVE(&allcftables, ct, ct_list);
780 free(ct, M_DEVBUF);
781 return (0);
782 }
783 }
784
785 /* not found -- shouldn't happen */
786 return (EINVAL);
787 }
788
789 /*
790 * Invoke the "match" routine for a cfdata entry on behalf of
791 * an external caller, usually a "submatch" routine.
792 */
793 int
794 config_match(device_t parent, cfdata_t cf, void *aux)
795 {
796 struct cfattach *ca;
797
798 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
799 if (ca == NULL) {
800 /* No attachment for this entry, oh well. */
801 return (0);
802 }
803
804 return ((*ca->ca_match)(parent, cf, aux));
805 }
806
807 /*
808 * Iterate over all potential children of some device, calling the given
809 * function (default being the child's match function) for each one.
810 * Nonzero returns are matches; the highest value returned is considered
811 * the best match. Return the `found child' if we got a match, or NULL
812 * otherwise. The `aux' pointer is simply passed on through.
813 *
814 * Note that this function is designed so that it can be used to apply
815 * an arbitrary function to all potential children (its return value
816 * can be ignored).
817 */
818 cfdata_t
819 config_search_loc(cfsubmatch_t fn, device_t parent,
820 const char *ifattr, const int *locs, void *aux)
821 {
822 struct cftable *ct;
823 cfdata_t cf;
824 struct matchinfo m;
825
826 KASSERT(config_initialized);
827 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
828
829 m.fn = fn;
830 m.parent = parent;
831 m.locs = locs;
832 m.aux = aux;
833 m.match = NULL;
834 m.pri = 0;
835
836 TAILQ_FOREACH(ct, &allcftables, ct_list) {
837 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
838
839 /* We don't match root nodes here. */
840 if (!cf->cf_pspec)
841 continue;
842
843 /*
844 * Skip cf if no longer eligible, otherwise scan
845 * through parents for one matching `parent', and
846 * try match function.
847 */
848 if (cf->cf_fstate == FSTATE_FOUND)
849 continue;
850 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
851 cf->cf_fstate == FSTATE_DSTAR)
852 continue;
853
854 /*
855 * If an interface attribute was specified,
856 * consider only children which attach to
857 * that attribute.
858 */
859 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
860 continue;
861
862 if (cfparent_match(parent, cf->cf_pspec))
863 mapply(&m, cf);
864 }
865 }
866 return (m.match);
867 }
868
869 cfdata_t
870 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
871 void *aux)
872 {
873
874 return (config_search_loc(fn, parent, ifattr, NULL, aux));
875 }
876
877 /*
878 * Find the given root device.
879 * This is much like config_search, but there is no parent.
880 * Don't bother with multiple cfdata tables; the root node
881 * must always be in the initial table.
882 */
883 cfdata_t
884 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
885 {
886 cfdata_t cf;
887 const short *p;
888 struct matchinfo m;
889
890 m.fn = fn;
891 m.parent = ROOT;
892 m.aux = aux;
893 m.match = NULL;
894 m.pri = 0;
895 m.locs = 0;
896 /*
897 * Look at root entries for matching name. We do not bother
898 * with found-state here since only one root should ever be
899 * searched (and it must be done first).
900 */
901 for (p = cfroots; *p >= 0; p++) {
902 cf = &cfdata[*p];
903 if (strcmp(cf->cf_name, rootname) == 0)
904 mapply(&m, cf);
905 }
906 return (m.match);
907 }
908
909 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
910
911 /*
912 * The given `aux' argument describes a device that has been found
913 * on the given parent, but not necessarily configured. Locate the
914 * configuration data for that device (using the submatch function
915 * provided, or using candidates' cd_match configuration driver
916 * functions) and attach it, and return true. If the device was
917 * not configured, call the given `print' function and return 0.
918 */
919 device_t
920 config_found_sm_loc(device_t parent,
921 const char *ifattr, const int *locs, void *aux,
922 cfprint_t print, cfsubmatch_t submatch)
923 {
924 cfdata_t cf;
925
926 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
927 if (splash_progress_state)
928 splash_progress_update(splash_progress_state);
929 #endif
930
931 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
932 return(config_attach_loc(parent, cf, locs, aux, print));
933 if (print) {
934 if (config_do_twiddle)
935 twiddle();
936 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
937 }
938
939 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
940 if (splash_progress_state)
941 splash_progress_update(splash_progress_state);
942 #endif
943
944 return (NULL);
945 }
946
947 device_t
948 config_found_ia(device_t parent, const char *ifattr, void *aux,
949 cfprint_t print)
950 {
951
952 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL));
953 }
954
955 device_t
956 config_found(device_t parent, void *aux, cfprint_t print)
957 {
958
959 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL));
960 }
961
962 /*
963 * As above, but for root devices.
964 */
965 device_t
966 config_rootfound(const char *rootname, void *aux)
967 {
968 cfdata_t cf;
969
970 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
971 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
972 aprint_error("root device %s not configured\n", rootname);
973 return (NULL);
974 }
975
976 /* just like sprintf(buf, "%d") except that it works from the end */
977 static char *
978 number(char *ep, int n)
979 {
980
981 *--ep = 0;
982 while (n >= 10) {
983 *--ep = (n % 10) + '0';
984 n /= 10;
985 }
986 *--ep = n + '0';
987 return (ep);
988 }
989
990 /*
991 * Expand the size of the cd_devs array if necessary.
992 */
993 static void
994 config_makeroom(int n, struct cfdriver *cd)
995 {
996 int old, new;
997 void **nsp;
998
999 if (n < cd->cd_ndevs)
1000 return;
1001
1002 /*
1003 * Need to expand the array.
1004 */
1005 old = cd->cd_ndevs;
1006 if (old == 0)
1007 new = 4;
1008 else
1009 new = old * 2;
1010 while (new <= n)
1011 new *= 2;
1012 cd->cd_ndevs = new;
1013 nsp = malloc(new * sizeof(void *), M_DEVBUF,
1014 cold ? M_NOWAIT : M_WAITOK);
1015 if (nsp == NULL)
1016 panic("config_attach: %sing dev array",
1017 old != 0 ? "expand" : "creat");
1018 memset(nsp + old, 0, (new - old) * sizeof(void *));
1019 if (old != 0) {
1020 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
1021 free(cd->cd_devs, M_DEVBUF);
1022 }
1023 cd->cd_devs = nsp;
1024 }
1025
1026 static void
1027 config_devlink(device_t dev)
1028 {
1029 struct cfdriver *cd = dev->dv_cfdriver;
1030
1031 /* put this device in the devices array */
1032 config_makeroom(dev->dv_unit, cd);
1033 if (cd->cd_devs[dev->dv_unit])
1034 panic("config_attach: duplicate %s", dev->dv_xname);
1035 cd->cd_devs[dev->dv_unit] = dev;
1036
1037 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
1038 }
1039
1040 static void
1041 config_devunlink(device_t dev)
1042 {
1043 struct cfdriver *cd = dev->dv_cfdriver;
1044 int i;
1045
1046 /* Unlink from device list. */
1047 TAILQ_REMOVE(&alldevs, dev, dv_list);
1048
1049 /* Remove from cfdriver's array. */
1050 cd->cd_devs[dev->dv_unit] = NULL;
1051
1052 /*
1053 * If the device now has no units in use, deallocate its softc array.
1054 */
1055 for (i = 0; i < cd->cd_ndevs; i++)
1056 if (cd->cd_devs[i] != NULL)
1057 break;
1058 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
1059 free(cd->cd_devs, M_DEVBUF);
1060 cd->cd_devs = NULL;
1061 cd->cd_ndevs = 0;
1062 }
1063 }
1064
1065 static device_t
1066 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
1067 {
1068 struct cfdriver *cd;
1069 struct cfattach *ca;
1070 size_t lname, lunit;
1071 const char *xunit;
1072 int myunit;
1073 char num[10];
1074 device_t dev;
1075 const struct cfiattrdata *ia;
1076
1077 cd = config_cfdriver_lookup(cf->cf_name);
1078 if (cd == NULL)
1079 return (NULL);
1080
1081 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
1082 if (ca == NULL)
1083 return (NULL);
1084
1085 if (ca->ca_devsize < sizeof(struct device))
1086 panic("config_devalloc");
1087
1088 #ifndef __BROKEN_CONFIG_UNIT_USAGE
1089 if (cf->cf_fstate == FSTATE_STAR) {
1090 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
1091 if (cd->cd_devs[myunit] == NULL)
1092 break;
1093 /*
1094 * myunit is now the unit of the first NULL device pointer,
1095 * or max(cd->cd_ndevs,cf->cf_unit).
1096 */
1097 } else {
1098 myunit = cf->cf_unit;
1099 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
1100 return (NULL);
1101 }
1102 #else
1103 myunit = cf->cf_unit;
1104 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
1105
1106 /* compute length of name and decimal expansion of unit number */
1107 lname = strlen(cd->cd_name);
1108 xunit = number(&num[sizeof(num)], myunit);
1109 lunit = &num[sizeof(num)] - xunit;
1110 if (lname + lunit > sizeof(dev->dv_xname))
1111 panic("config_devalloc: device name too long");
1112
1113 /* get memory for all device vars */
1114 dev = (device_t)malloc(ca->ca_devsize, M_DEVBUF,
1115 M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
1116 if (!dev)
1117 panic("config_devalloc: memory allocation for device softc failed");
1118 dev->dv_class = cd->cd_class;
1119 dev->dv_cfdata = cf;
1120 dev->dv_cfdriver = cd;
1121 dev->dv_cfattach = ca;
1122 dev->dv_unit = myunit;
1123 memcpy(dev->dv_xname, cd->cd_name, lname);
1124 memcpy(dev->dv_xname + lname, xunit, lunit);
1125 dev->dv_parent = parent;
1126 dev->dv_flags = DVF_ACTIVE; /* always initially active */
1127 if (locs) {
1128 KASSERT(parent); /* no locators at root */
1129 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
1130 parent->dv_cfdriver);
1131 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
1132 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1133 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
1134 }
1135 dev->dv_properties = prop_dictionary_create();
1136 KASSERT(dev->dv_properties != NULL);
1137
1138 return (dev);
1139 }
1140
1141 static void
1142 config_devdealloc(device_t dev)
1143 {
1144
1145 KASSERT(dev->dv_properties != NULL);
1146 prop_object_release(dev->dv_properties);
1147
1148 if (dev->dv_locators)
1149 free(dev->dv_locators, M_DEVBUF);
1150
1151 free(dev, M_DEVBUF);
1152 }
1153
1154 /*
1155 * Attach a found device.
1156 */
1157 device_t
1158 config_attach_loc(device_t parent, cfdata_t cf,
1159 const int *locs, void *aux, cfprint_t print)
1160 {
1161 device_t dev;
1162 struct cftable *ct;
1163 const char *drvname;
1164
1165 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1166 if (splash_progress_state)
1167 splash_progress_update(splash_progress_state);
1168 #endif
1169
1170 dev = config_devalloc(parent, cf, locs);
1171 if (!dev)
1172 panic("config_attach: allocation of device softc failed");
1173
1174 /* XXX redundant - see below? */
1175 if (cf->cf_fstate != FSTATE_STAR) {
1176 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1177 cf->cf_fstate = FSTATE_FOUND;
1178 }
1179 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1180 else
1181 cf->cf_unit++;
1182 #endif
1183
1184 config_devlink(dev);
1185
1186 if (config_do_twiddle)
1187 twiddle();
1188 else
1189 aprint_naive("Found ");
1190 /*
1191 * We want the next two printfs for normal, verbose, and quiet,
1192 * but not silent (in which case, we're twiddling, instead).
1193 */
1194 if (parent == ROOT) {
1195 aprint_naive("%s (root)", dev->dv_xname);
1196 aprint_normal("%s (root)", dev->dv_xname);
1197 } else {
1198 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
1199 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
1200 if (print)
1201 (void) (*print)(aux, NULL);
1202 }
1203
1204 /*
1205 * Before attaching, clobber any unfound devices that are
1206 * otherwise identical.
1207 * XXX code above is redundant?
1208 */
1209 drvname = dev->dv_cfdriver->cd_name;
1210 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1211 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1212 if (STREQ(cf->cf_name, drvname) &&
1213 cf->cf_unit == dev->dv_unit) {
1214 if (cf->cf_fstate == FSTATE_NOTFOUND)
1215 cf->cf_fstate = FSTATE_FOUND;
1216 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1217 /*
1218 * Bump the unit number on all starred cfdata
1219 * entries for this device.
1220 */
1221 if (cf->cf_fstate == FSTATE_STAR)
1222 cf->cf_unit++;
1223 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1224 }
1225 }
1226 }
1227 #ifdef __HAVE_DEVICE_REGISTER
1228 device_register(dev, aux);
1229 #endif
1230 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1231 if (splash_progress_state)
1232 splash_progress_update(splash_progress_state);
1233 #endif
1234 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1235 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1236 if (splash_progress_state)
1237 splash_progress_update(splash_progress_state);
1238 #endif
1239 config_process_deferred(&deferred_config_queue, dev);
1240 return (dev);
1241 }
1242
1243 device_t
1244 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1245 {
1246
1247 return (config_attach_loc(parent, cf, NULL, aux, print));
1248 }
1249
1250 /*
1251 * As above, but for pseudo-devices. Pseudo-devices attached in this
1252 * way are silently inserted into the device tree, and their children
1253 * attached.
1254 *
1255 * Note that because pseudo-devices are attached silently, any information
1256 * the attach routine wishes to print should be prefixed with the device
1257 * name by the attach routine.
1258 */
1259 device_t
1260 config_attach_pseudo(cfdata_t cf)
1261 {
1262 device_t dev;
1263
1264 dev = config_devalloc(ROOT, cf, NULL);
1265 if (!dev)
1266 return (NULL);
1267
1268 /* XXX mark busy in cfdata */
1269
1270 config_devlink(dev);
1271
1272 #if 0 /* XXXJRT not yet */
1273 #ifdef __HAVE_DEVICE_REGISTER
1274 device_register(dev, NULL); /* like a root node */
1275 #endif
1276 #endif
1277 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1278 config_process_deferred(&deferred_config_queue, dev);
1279 return (dev);
1280 }
1281
1282 /*
1283 * Detach a device. Optionally forced (e.g. because of hardware
1284 * removal) and quiet. Returns zero if successful, non-zero
1285 * (an error code) otherwise.
1286 *
1287 * Note that this code wants to be run from a process context, so
1288 * that the detach can sleep to allow processes which have a device
1289 * open to run and unwind their stacks.
1290 */
1291 int
1292 config_detach(device_t dev, int flags)
1293 {
1294 struct cftable *ct;
1295 cfdata_t cf;
1296 const struct cfattach *ca;
1297 struct cfdriver *cd;
1298 #ifdef DIAGNOSTIC
1299 device_t d;
1300 #endif
1301 int rv = 0;
1302
1303 #ifdef DIAGNOSTIC
1304 if (dev->dv_cfdata != NULL &&
1305 dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
1306 dev->dv_cfdata->cf_fstate != FSTATE_STAR)
1307 panic("config_detach: bad device fstate");
1308 #endif
1309 cd = dev->dv_cfdriver;
1310 KASSERT(cd != NULL);
1311
1312 ca = dev->dv_cfattach;
1313 KASSERT(ca != NULL);
1314
1315 /*
1316 * Ensure the device is deactivated. If the device doesn't
1317 * have an activation entry point, we allow DVF_ACTIVE to
1318 * remain set. Otherwise, if DVF_ACTIVE is still set, the
1319 * device is busy, and the detach fails.
1320 */
1321 if (ca->ca_activate != NULL)
1322 rv = config_deactivate(dev);
1323
1324 /*
1325 * Try to detach the device. If that's not possible, then
1326 * we either panic() (for the forced but failed case), or
1327 * return an error.
1328 */
1329 if (rv == 0) {
1330 if (ca->ca_detach != NULL)
1331 rv = (*ca->ca_detach)(dev, flags);
1332 else
1333 rv = EOPNOTSUPP;
1334 }
1335 if (rv != 0) {
1336 if ((flags & DETACH_FORCE) == 0)
1337 return (rv);
1338 else
1339 panic("config_detach: forced detach of %s failed (%d)",
1340 dev->dv_xname, rv);
1341 }
1342
1343 /*
1344 * The device has now been successfully detached.
1345 */
1346
1347 #ifdef DIAGNOSTIC
1348 /*
1349 * Sanity: If you're successfully detached, you should have no
1350 * children. (Note that because children must be attached
1351 * after parents, we only need to search the latter part of
1352 * the list.)
1353 */
1354 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1355 d = TAILQ_NEXT(d, dv_list)) {
1356 if (d->dv_parent == dev) {
1357 printf("config_detach: detached device %s"
1358 " has children %s\n", dev->dv_xname, d->dv_xname);
1359 panic("config_detach");
1360 }
1361 }
1362 #endif
1363
1364 /* notify the parent that the child is gone */
1365 if (dev->dv_parent) {
1366 device_t p = dev->dv_parent;
1367 if (p->dv_cfattach->ca_childdetached)
1368 (*p->dv_cfattach->ca_childdetached)(p, dev);
1369 }
1370
1371 /*
1372 * Mark cfdata to show that the unit can be reused, if possible.
1373 */
1374 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1375 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1376 if (STREQ(cf->cf_name, cd->cd_name)) {
1377 if (cf->cf_fstate == FSTATE_FOUND &&
1378 cf->cf_unit == dev->dv_unit)
1379 cf->cf_fstate = FSTATE_NOTFOUND;
1380 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1381 /*
1382 * Note that we can only re-use a starred
1383 * unit number if the unit being detached
1384 * had the last assigned unit number.
1385 */
1386 if (cf->cf_fstate == FSTATE_STAR &&
1387 cf->cf_unit == dev->dv_unit + 1)
1388 cf->cf_unit--;
1389 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1390 }
1391 }
1392 }
1393
1394 config_devunlink(dev);
1395
1396 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1397 aprint_normal("%s detached\n", dev->dv_xname);
1398
1399 config_devdealloc(dev);
1400
1401 return (0);
1402 }
1403
1404 int
1405 config_activate(device_t dev)
1406 {
1407 const struct cfattach *ca = dev->dv_cfattach;
1408 int rv = 0, oflags = dev->dv_flags;
1409
1410 if (ca->ca_activate == NULL)
1411 return (EOPNOTSUPP);
1412
1413 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
1414 dev->dv_flags |= DVF_ACTIVE;
1415 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
1416 if (rv)
1417 dev->dv_flags = oflags;
1418 }
1419 return (rv);
1420 }
1421
1422 int
1423 config_deactivate(device_t dev)
1424 {
1425 const struct cfattach *ca = dev->dv_cfattach;
1426 int rv = 0, oflags = dev->dv_flags;
1427
1428 if (ca->ca_activate == NULL)
1429 return (EOPNOTSUPP);
1430
1431 if (dev->dv_flags & DVF_ACTIVE) {
1432 dev->dv_flags &= ~DVF_ACTIVE;
1433 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
1434 if (rv)
1435 dev->dv_flags = oflags;
1436 }
1437 return (rv);
1438 }
1439
1440 /*
1441 * Defer the configuration of the specified device until all
1442 * of its parent's devices have been attached.
1443 */
1444 void
1445 config_defer(device_t dev, void (*func)(device_t))
1446 {
1447 struct deferred_config *dc;
1448
1449 if (dev->dv_parent == NULL)
1450 panic("config_defer: can't defer config of a root device");
1451
1452 #ifdef DIAGNOSTIC
1453 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
1454 dc = TAILQ_NEXT(dc, dc_queue)) {
1455 if (dc->dc_dev == dev)
1456 panic("config_defer: deferred twice");
1457 }
1458 #endif
1459
1460 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1461 if (dc == NULL)
1462 panic("config_defer: unable to allocate callback");
1463
1464 dc->dc_dev = dev;
1465 dc->dc_func = func;
1466 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1467 config_pending_incr();
1468 }
1469
1470 /*
1471 * Defer some autoconfiguration for a device until after interrupts
1472 * are enabled.
1473 */
1474 void
1475 config_interrupts(device_t dev, void (*func)(device_t))
1476 {
1477 struct deferred_config *dc;
1478
1479 /*
1480 * If interrupts are enabled, callback now.
1481 */
1482 if (cold == 0) {
1483 (*func)(dev);
1484 return;
1485 }
1486
1487 #ifdef DIAGNOSTIC
1488 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
1489 dc = TAILQ_NEXT(dc, dc_queue)) {
1490 if (dc->dc_dev == dev)
1491 panic("config_interrupts: deferred twice");
1492 }
1493 #endif
1494
1495 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1496 if (dc == NULL)
1497 panic("config_interrupts: unable to allocate callback");
1498
1499 dc->dc_dev = dev;
1500 dc->dc_func = func;
1501 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1502 config_pending_incr();
1503 }
1504
1505 /*
1506 * Process a deferred configuration queue.
1507 */
1508 static void
1509 config_process_deferred(struct deferred_config_head *queue,
1510 device_t parent)
1511 {
1512 struct deferred_config *dc, *ndc;
1513
1514 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1515 ndc = TAILQ_NEXT(dc, dc_queue);
1516 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1517 TAILQ_REMOVE(queue, dc, dc_queue);
1518 (*dc->dc_func)(dc->dc_dev);
1519 free(dc, M_DEVBUF);
1520 config_pending_decr();
1521 }
1522 }
1523 }
1524
1525 /*
1526 * Manipulate the config_pending semaphore.
1527 */
1528 void
1529 config_pending_incr(void)
1530 {
1531
1532 config_pending++;
1533 }
1534
1535 void
1536 config_pending_decr(void)
1537 {
1538
1539 #ifdef DIAGNOSTIC
1540 if (config_pending == 0)
1541 panic("config_pending_decr: config_pending == 0");
1542 #endif
1543 config_pending--;
1544 if (config_pending == 0)
1545 wakeup(&config_pending);
1546 }
1547
1548 /*
1549 * Register a "finalization" routine. Finalization routines are
1550 * called iteratively once all real devices have been found during
1551 * autoconfiguration, for as long as any one finalizer has done
1552 * any work.
1553 */
1554 int
1555 config_finalize_register(device_t dev, int (*fn)(device_t))
1556 {
1557 struct finalize_hook *f;
1558
1559 /*
1560 * If finalization has already been done, invoke the
1561 * callback function now.
1562 */
1563 if (config_finalize_done) {
1564 while ((*fn)(dev) != 0)
1565 /* loop */ ;
1566 }
1567
1568 /* Ensure this isn't already on the list. */
1569 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
1570 if (f->f_func == fn && f->f_dev == dev)
1571 return (EEXIST);
1572 }
1573
1574 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
1575 f->f_func = fn;
1576 f->f_dev = dev;
1577 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
1578
1579 return (0);
1580 }
1581
1582 void
1583 config_finalize(void)
1584 {
1585 struct finalize_hook *f;
1586 int rv;
1587
1588 /* Run the hooks until none of them does any work. */
1589 do {
1590 rv = 0;
1591 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1592 rv |= (*f->f_func)(f->f_dev);
1593 } while (rv != 0);
1594
1595 config_finalize_done = 1;
1596
1597 /* Now free all the hooks. */
1598 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1599 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1600 free(f, M_TEMP);
1601 }
1602 }
1603
1604 /*
1605 * device_lookup:
1606 *
1607 * Look up a device instance for a given driver.
1608 */
1609 void *
1610 device_lookup(cfdriver_t cd, int unit)
1611 {
1612
1613 if (unit < 0 || unit >= cd->cd_ndevs)
1614 return (NULL);
1615
1616 return (cd->cd_devs[unit]);
1617 }
1618
1619 /*
1620 * Accessor functions for the device_t type.
1621 */
1622 devclass_t
1623 device_class(device_t dev)
1624 {
1625
1626 return (dev->dv_class);
1627 }
1628
1629 cfdata_t
1630 device_cfdata(device_t dev)
1631 {
1632
1633 return (dev->dv_cfdata);
1634 }
1635
1636 cfdriver_t
1637 device_cfdriver(device_t dev)
1638 {
1639
1640 return (dev->dv_cfdriver);
1641 }
1642
1643 cfattach_t
1644 device_cfattach(device_t dev)
1645 {
1646
1647 return (dev->dv_cfattach);
1648 }
1649
1650 int
1651 device_unit(device_t dev)
1652 {
1653
1654 return (dev->dv_unit);
1655 }
1656
1657 const char *
1658 device_xname(device_t dev)
1659 {
1660
1661 return (dev->dv_xname);
1662 }
1663
1664 device_t
1665 device_parent(device_t dev)
1666 {
1667
1668 return (dev->dv_parent);
1669 }
1670
1671 bool
1672 device_is_active(device_t dev)
1673 {
1674
1675 return ((dev->dv_flags & DVF_ACTIVE) != 0);
1676 }
1677
1678 int
1679 device_locator(device_t dev, u_int locnum)
1680 {
1681
1682 KASSERT(dev->dv_locators != NULL);
1683 return (dev->dv_locators[locnum]);
1684 }
1685
1686 void *
1687 device_private(device_t dev)
1688 {
1689
1690 /*
1691 * For now, at least, "struct device" is the first thing in
1692 * the driver's private data. So, we just return ourselves.
1693 */
1694 return (dev);
1695 }
1696
1697 prop_dictionary_t
1698 device_properties(device_t dev)
1699 {
1700
1701 return (dev->dv_properties);
1702 }
1703
1704 /*
1705 * device_is_a:
1706 *
1707 * Returns true if the device is an instance of the specified
1708 * driver.
1709 */
1710 bool
1711 device_is_a(device_t dev, const char *dname)
1712 {
1713
1714 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0);
1715 }
1716