subr_autoconf.c revision 1.127 1 /* $NetBSD: subr_autoconf.c,v 1.127 2008/01/04 21:18:12 ad 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.127 2008/01/04 21:18:12 ad Exp $");
81
82 #include "opt_multiprocessor.h"
83 #include "opt_ddb.h"
84
85 #include <sys/param.h>
86 #include <sys/device.h>
87 #include <sys/disklabel.h>
88 #include <sys/conf.h>
89 #include <sys/kauth.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
97 #include <sys/buf.h>
98 #include <sys/dirent.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 #include <sys/callout.h>
106
107 #include <sys/disk.h>
108
109 #include <machine/limits.h>
110
111 #include "opt_userconf.h"
112 #ifdef USERCONF
113 #include <sys/userconf.h>
114 #endif
115
116 #ifdef __i386__
117 #include "opt_splash.h"
118 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
119 #include <dev/splash/splash.h>
120 extern struct splash_progress *splash_progress_state;
121 #endif
122 #endif
123
124 /*
125 * Autoconfiguration subroutines.
126 */
127
128 /*
129 * ioconf.c exports exactly two names: cfdata and cfroots. All system
130 * devices and drivers are found via these tables.
131 */
132 extern struct cfdata cfdata[];
133 extern const short cfroots[];
134
135 /*
136 * List of all cfdriver structures. We use this to detect duplicates
137 * when other cfdrivers are loaded.
138 */
139 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
140 extern struct cfdriver * const cfdriver_list_initial[];
141
142 /*
143 * Initial list of cfattach's.
144 */
145 extern const struct cfattachinit cfattachinit[];
146
147 /*
148 * List of cfdata tables. We always have one such list -- the one
149 * built statically when the kernel was configured.
150 */
151 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
152 static struct cftable initcftable;
153
154 #define ROOT ((device_t)NULL)
155
156 struct matchinfo {
157 cfsubmatch_t fn;
158 struct device *parent;
159 const int *locs;
160 void *aux;
161 struct cfdata *match;
162 int pri;
163 };
164
165 static char *number(char *, int);
166 static void mapply(struct matchinfo *, cfdata_t);
167 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
168 static void config_devdealloc(device_t);
169 static void config_makeroom(int, struct cfdriver *);
170 static void config_devlink(device_t);
171 static void config_devunlink(device_t);
172
173 struct deferred_config {
174 TAILQ_ENTRY(deferred_config) dc_queue;
175 device_t dc_dev;
176 void (*dc_func)(device_t);
177 };
178
179 TAILQ_HEAD(deferred_config_head, deferred_config);
180
181 struct deferred_config_head deferred_config_queue =
182 TAILQ_HEAD_INITIALIZER(deferred_config_queue);
183 struct deferred_config_head interrupt_config_queue =
184 TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
185
186 static void config_process_deferred(struct deferred_config_head *, device_t);
187
188 /* Hooks to finalize configuration once all real devices have been found. */
189 struct finalize_hook {
190 TAILQ_ENTRY(finalize_hook) f_list;
191 int (*f_func)(device_t);
192 device_t f_dev;
193 };
194 static TAILQ_HEAD(, finalize_hook) config_finalize_list =
195 TAILQ_HEAD_INITIALIZER(config_finalize_list);
196 static int config_finalize_done;
197
198 /* list of all devices */
199 struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
200
201 volatile int config_pending; /* semaphore for mountroot */
202
203 #define STREQ(s1, s2) \
204 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
205
206 static int config_initialized; /* config_init() has been called. */
207
208 static int config_do_twiddle;
209
210 struct vnode *
211 opendisk(struct device *dv)
212 {
213 int bmajor, bminor;
214 struct vnode *tmpvn;
215 int error;
216 dev_t dev;
217
218 /*
219 * Lookup major number for disk block device.
220 */
221 bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
222 if (bmajor == -1)
223 return NULL;
224
225 bminor = minor(device_unit(dv));
226 /*
227 * Fake a temporary vnode for the disk, open it, and read
228 * and hash the sectors.
229 */
230 dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) :
231 MAKEDISKDEV(bmajor, bminor, RAW_PART);
232 if (bdevvp(dev, &tmpvn))
233 panic("%s: can't alloc vnode for %s", __func__,
234 device_xname(dv));
235 error = VOP_OPEN(tmpvn, FREAD, NOCRED);
236 if (error) {
237 #ifndef DEBUG
238 /*
239 * Ignore errors caused by missing device, partition,
240 * or medium.
241 */
242 if (error != ENXIO && error != ENODEV)
243 #endif
244 printf("%s: can't open dev %s (%d)\n",
245 __func__, device_xname(dv), error);
246 vput(tmpvn);
247 return NULL;
248 }
249
250 return tmpvn;
251 }
252
253 int
254 config_handle_wedges(struct device *dv, int par)
255 {
256 struct dkwedge_list wl;
257 struct dkwedge_info *wi;
258 struct vnode *vn;
259 char diskname[16];
260 int i, error;
261
262 if ((vn = opendisk(dv)) == NULL)
263 return -1;
264
265 wl.dkwl_bufsize = sizeof(*wi) * 16;
266 wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK);
267
268 error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED);
269 VOP_CLOSE(vn, FREAD, NOCRED);
270 vput(vn);
271 if (error) {
272 #ifdef DEBUG_WEDGE
273 printf("%s: List wedges returned %d\n",
274 device_xname(dv), error);
275 #endif
276 free(wi, M_TEMP);
277 return -1;
278 }
279
280 #ifdef DEBUG_WEDGE
281 printf("%s: Returned %u(%u) wedges\n", device_xname(dv),
282 wl.dkwl_nwedges, wl.dkwl_ncopied);
283 #endif
284 snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv),
285 par + 'a');
286
287 for (i = 0; i < wl.dkwl_ncopied; i++) {
288 #ifdef DEBUG_WEDGE
289 printf("%s: Looking for %s in %s\n",
290 device_xname(dv), diskname, wi[i].dkw_wname);
291 #endif
292 if (strcmp(wi[i].dkw_wname, diskname) == 0)
293 break;
294 }
295
296 if (i == wl.dkwl_ncopied) {
297 #ifdef DEBUG_WEDGE
298 printf("%s: Cannot find wedge with parent %s\n",
299 device_xname(dv), diskname);
300 #endif
301 free(wi, M_TEMP);
302 return -1;
303 }
304
305 #ifdef DEBUG_WEDGE
306 printf("%s: Setting boot wedge %s (%s) at %llu %llu\n",
307 device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname,
308 (unsigned long long)wi[i].dkw_offset,
309 (unsigned long long)wi[i].dkw_size);
310 #endif
311 dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size);
312 free(wi, M_TEMP);
313 return 0;
314 }
315
316 /*
317 * Initialize the autoconfiguration data structures. Normally this
318 * is done by configure(), but some platforms need to do this very
319 * early (to e.g. initialize the console).
320 */
321 void
322 config_init(void)
323 {
324 const struct cfattachinit *cfai;
325 int i, j;
326
327 if (config_initialized)
328 return;
329
330 /* allcfdrivers is statically initialized. */
331 for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
332 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
333 panic("configure: duplicate `%s' drivers",
334 cfdriver_list_initial[i]->cd_name);
335 }
336
337 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
338 for (j = 0; cfai->cfai_list[j] != NULL; j++) {
339 if (config_cfattach_attach(cfai->cfai_name,
340 cfai->cfai_list[j]) != 0)
341 panic("configure: duplicate `%s' attachment "
342 "of `%s' driver",
343 cfai->cfai_list[j]->ca_name,
344 cfai->cfai_name);
345 }
346 }
347
348 initcftable.ct_cfdata = cfdata;
349 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
350
351 config_initialized = 1;
352 }
353
354 void
355 config_deferred(device_t dev)
356 {
357 config_process_deferred(&deferred_config_queue, dev);
358 config_process_deferred(&interrupt_config_queue, dev);
359 }
360
361 /*
362 * Configure the system's hardware.
363 */
364 void
365 configure(void)
366 {
367 int errcnt;
368
369 /* Initialize data structures. */
370 config_init();
371 pmf_init();
372
373 #ifdef USERCONF
374 if (boothowto & RB_USERCONF)
375 user_config();
376 #endif
377
378 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
379 config_do_twiddle = 1;
380 printf_nolog("Detecting hardware...");
381 }
382
383 /*
384 * Do the machine-dependent portion of autoconfiguration. This
385 * sets the configuration machinery here in motion by "finding"
386 * the root bus. When this function returns, we expect interrupts
387 * to be enabled.
388 */
389 cpu_configure();
390
391 /* Initialize callouts, part 2. */
392 callout_startup2();
393
394 /*
395 * Now that we've found all the hardware, start the real time
396 * and statistics clocks.
397 */
398 initclocks();
399
400 cold = 0; /* clocks are running, we're warm now! */
401
402 #if defined(MULTIPROCESSOR)
403 /* Boot the secondary processors. */
404 cpu_boot_secondary_processors();
405 #endif
406
407 /*
408 * Now callback to finish configuration for devices which want
409 * to do this once interrupts are enabled.
410 */
411 config_process_deferred(&interrupt_config_queue, NULL);
412
413 errcnt = aprint_get_error_count();
414 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
415 (boothowto & AB_VERBOSE) == 0) {
416 if (config_do_twiddle) {
417 config_do_twiddle = 0;
418 printf_nolog("done.\n");
419 }
420 if (errcnt != 0) {
421 printf("WARNING: %d error%s while detecting hardware; "
422 "check system log.\n", errcnt,
423 errcnt == 1 ? "" : "s");
424 }
425 }
426 }
427
428 /*
429 * Add a cfdriver to the system.
430 */
431 int
432 config_cfdriver_attach(struct cfdriver *cd)
433 {
434 struct cfdriver *lcd;
435
436 /* Make sure this driver isn't already in the system. */
437 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
438 if (STREQ(lcd->cd_name, cd->cd_name))
439 return (EEXIST);
440 }
441
442 LIST_INIT(&cd->cd_attach);
443 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
444
445 return (0);
446 }
447
448 /*
449 * Remove a cfdriver from the system.
450 */
451 int
452 config_cfdriver_detach(struct cfdriver *cd)
453 {
454 int i;
455
456 /* Make sure there are no active instances. */
457 for (i = 0; i < cd->cd_ndevs; i++) {
458 if (cd->cd_devs[i] != NULL)
459 return (EBUSY);
460 }
461
462 /* ...and no attachments loaded. */
463 if (LIST_EMPTY(&cd->cd_attach) == 0)
464 return (EBUSY);
465
466 LIST_REMOVE(cd, cd_list);
467
468 KASSERT(cd->cd_devs == NULL);
469
470 return (0);
471 }
472
473 /*
474 * Look up a cfdriver by name.
475 */
476 struct cfdriver *
477 config_cfdriver_lookup(const char *name)
478 {
479 struct cfdriver *cd;
480
481 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
482 if (STREQ(cd->cd_name, name))
483 return (cd);
484 }
485
486 return (NULL);
487 }
488
489 /*
490 * Add a cfattach to the specified driver.
491 */
492 int
493 config_cfattach_attach(const char *driver, struct cfattach *ca)
494 {
495 struct cfattach *lca;
496 struct cfdriver *cd;
497
498 cd = config_cfdriver_lookup(driver);
499 if (cd == NULL)
500 return (ESRCH);
501
502 /* Make sure this attachment isn't already on this driver. */
503 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
504 if (STREQ(lca->ca_name, ca->ca_name))
505 return (EEXIST);
506 }
507
508 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
509
510 return (0);
511 }
512
513 /*
514 * Remove a cfattach from the specified driver.
515 */
516 int
517 config_cfattach_detach(const char *driver, struct cfattach *ca)
518 {
519 struct cfdriver *cd;
520 device_t dev;
521 int i;
522
523 cd = config_cfdriver_lookup(driver);
524 if (cd == NULL)
525 return (ESRCH);
526
527 /* Make sure there are no active instances. */
528 for (i = 0; i < cd->cd_ndevs; i++) {
529 if ((dev = cd->cd_devs[i]) == NULL)
530 continue;
531 if (dev->dv_cfattach == ca)
532 return (EBUSY);
533 }
534
535 LIST_REMOVE(ca, ca_list);
536
537 return (0);
538 }
539
540 /*
541 * Look up a cfattach by name.
542 */
543 static struct cfattach *
544 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
545 {
546 struct cfattach *ca;
547
548 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
549 if (STREQ(ca->ca_name, atname))
550 return (ca);
551 }
552
553 return (NULL);
554 }
555
556 /*
557 * Look up a cfattach by driver/attachment name.
558 */
559 struct cfattach *
560 config_cfattach_lookup(const char *name, const char *atname)
561 {
562 struct cfdriver *cd;
563
564 cd = config_cfdriver_lookup(name);
565 if (cd == NULL)
566 return (NULL);
567
568 return (config_cfattach_lookup_cd(cd, atname));
569 }
570
571 /*
572 * Apply the matching function and choose the best. This is used
573 * a few times and we want to keep the code small.
574 */
575 static void
576 mapply(struct matchinfo *m, cfdata_t cf)
577 {
578 int pri;
579
580 if (m->fn != NULL) {
581 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
582 } else {
583 pri = config_match(m->parent, cf, m->aux);
584 }
585 if (pri > m->pri) {
586 m->match = cf;
587 m->pri = pri;
588 }
589 }
590
591 int
592 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
593 {
594 const struct cfiattrdata *ci;
595 const struct cflocdesc *cl;
596 int nlocs, i;
597
598 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
599 KASSERT(ci);
600 nlocs = ci->ci_loclen;
601 for (i = 0; i < nlocs; i++) {
602 cl = &ci->ci_locdesc[i];
603 /* !cld_defaultstr means no default value */
604 if ((!(cl->cld_defaultstr)
605 || (cf->cf_loc[i] != cl->cld_default))
606 && cf->cf_loc[i] != locs[i])
607 return (0);
608 }
609
610 return (config_match(parent, cf, aux));
611 }
612
613 /*
614 * Helper function: check whether the driver supports the interface attribute
615 * and return its descriptor structure.
616 */
617 static const struct cfiattrdata *
618 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
619 {
620 const struct cfiattrdata * const *cpp;
621
622 if (cd->cd_attrs == NULL)
623 return (0);
624
625 for (cpp = cd->cd_attrs; *cpp; cpp++) {
626 if (STREQ((*cpp)->ci_name, ia)) {
627 /* Match. */
628 return (*cpp);
629 }
630 }
631 return (0);
632 }
633
634 /*
635 * Lookup an interface attribute description by name.
636 * If the driver is given, consider only its supported attributes.
637 */
638 const struct cfiattrdata *
639 cfiattr_lookup(const char *name, const struct cfdriver *cd)
640 {
641 const struct cfdriver *d;
642 const struct cfiattrdata *ia;
643
644 if (cd)
645 return (cfdriver_get_iattr(cd, name));
646
647 LIST_FOREACH(d, &allcfdrivers, cd_list) {
648 ia = cfdriver_get_iattr(d, name);
649 if (ia)
650 return (ia);
651 }
652 return (0);
653 }
654
655 /*
656 * Determine if `parent' is a potential parent for a device spec based
657 * on `cfp'.
658 */
659 static int
660 cfparent_match(const device_t parent, const struct cfparent *cfp)
661 {
662 struct cfdriver *pcd;
663
664 /* We don't match root nodes here. */
665 if (cfp == NULL)
666 return (0);
667
668 pcd = parent->dv_cfdriver;
669 KASSERT(pcd != NULL);
670
671 /*
672 * First, ensure this parent has the correct interface
673 * attribute.
674 */
675 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
676 return (0);
677
678 /*
679 * If no specific parent device instance was specified (i.e.
680 * we're attaching to the attribute only), we're done!
681 */
682 if (cfp->cfp_parent == NULL)
683 return (1);
684
685 /*
686 * Check the parent device's name.
687 */
688 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
689 return (0); /* not the same parent */
690
691 /*
692 * Make sure the unit number matches.
693 */
694 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
695 cfp->cfp_unit == parent->dv_unit)
696 return (1);
697
698 /* Unit numbers don't match. */
699 return (0);
700 }
701
702 /*
703 * Helper for config_cfdata_attach(): check all devices whether it could be
704 * parent any attachment in the config data table passed, and rescan.
705 */
706 static void
707 rescan_with_cfdata(const struct cfdata *cf)
708 {
709 device_t d;
710 const struct cfdata *cf1;
711
712 /*
713 * "alldevs" is likely longer than an LKM's cfdata, so make it
714 * the outer loop.
715 */
716 TAILQ_FOREACH(d, &alldevs, dv_list) {
717
718 if (!(d->dv_cfattach->ca_rescan))
719 continue;
720
721 for (cf1 = cf; cf1->cf_name; cf1++) {
722
723 if (!cfparent_match(d, cf1->cf_pspec))
724 continue;
725
726 (*d->dv_cfattach->ca_rescan)(d,
727 cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
728 }
729 }
730 }
731
732 /*
733 * Attach a supplemental config data table and rescan potential
734 * parent devices if required.
735 */
736 int
737 config_cfdata_attach(cfdata_t cf, int scannow)
738 {
739 struct cftable *ct;
740
741 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
742 ct->ct_cfdata = cf;
743 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
744
745 if (scannow)
746 rescan_with_cfdata(cf);
747
748 return (0);
749 }
750
751 /*
752 * Helper for config_cfdata_detach: check whether a device is
753 * found through any attachment in the config data table.
754 */
755 static int
756 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
757 {
758 const struct cfdata *cf1;
759
760 for (cf1 = cf; cf1->cf_name; cf1++)
761 if (d->dv_cfdata == cf1)
762 return (1);
763
764 return (0);
765 }
766
767 /*
768 * Detach a supplemental config data table. Detach all devices found
769 * through that table (and thus keeping references to it) before.
770 */
771 int
772 config_cfdata_detach(cfdata_t cf)
773 {
774 device_t d;
775 int error;
776 struct cftable *ct;
777
778 again:
779 TAILQ_FOREACH(d, &alldevs, dv_list) {
780 if (dev_in_cfdata(d, cf)) {
781 error = config_detach(d, 0);
782 if (error) {
783 aprint_error("%s: unable to detach instance\n",
784 d->dv_xname);
785 return (error);
786 }
787 goto again;
788 }
789 }
790
791 TAILQ_FOREACH(ct, &allcftables, ct_list) {
792 if (ct->ct_cfdata == cf) {
793 TAILQ_REMOVE(&allcftables, ct, ct_list);
794 free(ct, M_DEVBUF);
795 return (0);
796 }
797 }
798
799 /* not found -- shouldn't happen */
800 return (EINVAL);
801 }
802
803 /*
804 * Invoke the "match" routine for a cfdata entry on behalf of
805 * an external caller, usually a "submatch" routine.
806 */
807 int
808 config_match(device_t parent, cfdata_t cf, void *aux)
809 {
810 struct cfattach *ca;
811
812 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
813 if (ca == NULL) {
814 /* No attachment for this entry, oh well. */
815 return (0);
816 }
817
818 return ((*ca->ca_match)(parent, cf, aux));
819 }
820
821 /*
822 * Iterate over all potential children of some device, calling the given
823 * function (default being the child's match function) for each one.
824 * Nonzero returns are matches; the highest value returned is considered
825 * the best match. Return the `found child' if we got a match, or NULL
826 * otherwise. The `aux' pointer is simply passed on through.
827 *
828 * Note that this function is designed so that it can be used to apply
829 * an arbitrary function to all potential children (its return value
830 * can be ignored).
831 */
832 cfdata_t
833 config_search_loc(cfsubmatch_t fn, device_t parent,
834 const char *ifattr, const int *locs, void *aux)
835 {
836 struct cftable *ct;
837 cfdata_t cf;
838 struct matchinfo m;
839
840 KASSERT(config_initialized);
841 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
842
843 m.fn = fn;
844 m.parent = parent;
845 m.locs = locs;
846 m.aux = aux;
847 m.match = NULL;
848 m.pri = 0;
849
850 TAILQ_FOREACH(ct, &allcftables, ct_list) {
851 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
852
853 /* We don't match root nodes here. */
854 if (!cf->cf_pspec)
855 continue;
856
857 /*
858 * Skip cf if no longer eligible, otherwise scan
859 * through parents for one matching `parent', and
860 * try match function.
861 */
862 if (cf->cf_fstate == FSTATE_FOUND)
863 continue;
864 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
865 cf->cf_fstate == FSTATE_DSTAR)
866 continue;
867
868 /*
869 * If an interface attribute was specified,
870 * consider only children which attach to
871 * that attribute.
872 */
873 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
874 continue;
875
876 if (cfparent_match(parent, cf->cf_pspec))
877 mapply(&m, cf);
878 }
879 }
880 return (m.match);
881 }
882
883 cfdata_t
884 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
885 void *aux)
886 {
887
888 return (config_search_loc(fn, parent, ifattr, NULL, aux));
889 }
890
891 /*
892 * Find the given root device.
893 * This is much like config_search, but there is no parent.
894 * Don't bother with multiple cfdata tables; the root node
895 * must always be in the initial table.
896 */
897 cfdata_t
898 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
899 {
900 cfdata_t cf;
901 const short *p;
902 struct matchinfo m;
903
904 m.fn = fn;
905 m.parent = ROOT;
906 m.aux = aux;
907 m.match = NULL;
908 m.pri = 0;
909 m.locs = 0;
910 /*
911 * Look at root entries for matching name. We do not bother
912 * with found-state here since only one root should ever be
913 * searched (and it must be done first).
914 */
915 for (p = cfroots; *p >= 0; p++) {
916 cf = &cfdata[*p];
917 if (strcmp(cf->cf_name, rootname) == 0)
918 mapply(&m, cf);
919 }
920 return (m.match);
921 }
922
923 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
924
925 /*
926 * The given `aux' argument describes a device that has been found
927 * on the given parent, but not necessarily configured. Locate the
928 * configuration data for that device (using the submatch function
929 * provided, or using candidates' cd_match configuration driver
930 * functions) and attach it, and return true. If the device was
931 * not configured, call the given `print' function and return 0.
932 */
933 device_t
934 config_found_sm_loc(device_t parent,
935 const char *ifattr, const int *locs, void *aux,
936 cfprint_t print, cfsubmatch_t submatch)
937 {
938 cfdata_t cf;
939
940 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
941 if (splash_progress_state)
942 splash_progress_update(splash_progress_state);
943 #endif
944
945 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
946 return(config_attach_loc(parent, cf, locs, aux, print));
947 if (print) {
948 if (config_do_twiddle)
949 twiddle();
950 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
951 }
952
953 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
954 if (splash_progress_state)
955 splash_progress_update(splash_progress_state);
956 #endif
957
958 return (NULL);
959 }
960
961 device_t
962 config_found_ia(device_t parent, const char *ifattr, void *aux,
963 cfprint_t print)
964 {
965
966 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL));
967 }
968
969 device_t
970 config_found(device_t parent, void *aux, cfprint_t print)
971 {
972
973 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL));
974 }
975
976 /*
977 * As above, but for root devices.
978 */
979 device_t
980 config_rootfound(const char *rootname, void *aux)
981 {
982 cfdata_t cf;
983
984 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
985 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
986 aprint_error("root device %s not configured\n", rootname);
987 return (NULL);
988 }
989
990 /* just like sprintf(buf, "%d") except that it works from the end */
991 static char *
992 number(char *ep, int n)
993 {
994
995 *--ep = 0;
996 while (n >= 10) {
997 *--ep = (n % 10) + '0';
998 n /= 10;
999 }
1000 *--ep = n + '0';
1001 return (ep);
1002 }
1003
1004 /*
1005 * Expand the size of the cd_devs array if necessary.
1006 */
1007 static void
1008 config_makeroom(int n, struct cfdriver *cd)
1009 {
1010 int old, new;
1011 void **nsp;
1012
1013 if (n < cd->cd_ndevs)
1014 return;
1015
1016 /*
1017 * Need to expand the array.
1018 */
1019 old = cd->cd_ndevs;
1020 if (old == 0)
1021 new = 4;
1022 else
1023 new = old * 2;
1024 while (new <= n)
1025 new *= 2;
1026 cd->cd_ndevs = new;
1027 nsp = malloc(new * sizeof(void *), M_DEVBUF,
1028 cold ? M_NOWAIT : M_WAITOK);
1029 if (nsp == NULL)
1030 panic("config_attach: %sing dev array",
1031 old != 0 ? "expand" : "creat");
1032 memset(nsp + old, 0, (new - old) * sizeof(void *));
1033 if (old != 0) {
1034 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
1035 free(cd->cd_devs, M_DEVBUF);
1036 }
1037 cd->cd_devs = nsp;
1038 }
1039
1040 static void
1041 config_devlink(device_t dev)
1042 {
1043 struct cfdriver *cd = dev->dv_cfdriver;
1044
1045 /* put this device in the devices array */
1046 config_makeroom(dev->dv_unit, cd);
1047 if (cd->cd_devs[dev->dv_unit])
1048 panic("config_attach: duplicate %s", dev->dv_xname);
1049 cd->cd_devs[dev->dv_unit] = dev;
1050
1051 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
1052 }
1053
1054 static void
1055 config_devunlink(device_t dev)
1056 {
1057 struct cfdriver *cd = dev->dv_cfdriver;
1058 int i;
1059
1060 /* Unlink from device list. */
1061 TAILQ_REMOVE(&alldevs, dev, dv_list);
1062
1063 /* Remove from cfdriver's array. */
1064 cd->cd_devs[dev->dv_unit] = NULL;
1065
1066 /*
1067 * If the device now has no units in use, deallocate its softc array.
1068 */
1069 for (i = 0; i < cd->cd_ndevs; i++)
1070 if (cd->cd_devs[i] != NULL)
1071 break;
1072 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
1073 free(cd->cd_devs, M_DEVBUF);
1074 cd->cd_devs = NULL;
1075 cd->cd_ndevs = 0;
1076 }
1077 }
1078
1079 static device_t
1080 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
1081 {
1082 struct cfdriver *cd;
1083 struct cfattach *ca;
1084 size_t lname, lunit;
1085 const char *xunit;
1086 int myunit;
1087 char num[10];
1088 device_t dev;
1089 void *dev_private;
1090 const struct cfiattrdata *ia;
1091
1092 cd = config_cfdriver_lookup(cf->cf_name);
1093 if (cd == NULL)
1094 return (NULL);
1095
1096 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
1097 if (ca == NULL)
1098 return (NULL);
1099
1100 if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 &&
1101 ca->ca_devsize < sizeof(struct device))
1102 panic("config_devalloc");
1103
1104 #ifndef __BROKEN_CONFIG_UNIT_USAGE
1105 if (cf->cf_fstate == FSTATE_STAR) {
1106 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
1107 if (cd->cd_devs[myunit] == NULL)
1108 break;
1109 /*
1110 * myunit is now the unit of the first NULL device pointer,
1111 * or max(cd->cd_ndevs,cf->cf_unit).
1112 */
1113 } else {
1114 myunit = cf->cf_unit;
1115 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
1116 return (NULL);
1117 }
1118 #else
1119 myunit = cf->cf_unit;
1120 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
1121
1122 /* compute length of name and decimal expansion of unit number */
1123 lname = strlen(cd->cd_name);
1124 xunit = number(&num[sizeof(num)], myunit);
1125 lunit = &num[sizeof(num)] - xunit;
1126 if (lname + lunit > sizeof(dev->dv_xname))
1127 panic("config_devalloc: device name too long");
1128
1129 /* get memory for all device vars */
1130 dev_private = malloc(ca->ca_devsize, M_DEVBUF,
1131 M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
1132 if (dev_private == NULL)
1133 panic("config_devalloc: memory allocation for device softc failed");
1134
1135 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
1136 dev = malloc(sizeof(struct device), M_DEVBUF,
1137 M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
1138 } else {
1139 dev = dev_private;
1140 }
1141 if (dev == NULL)
1142 panic("config_devalloc: memory allocation for device_t failed");
1143
1144 dev->dv_class = cd->cd_class;
1145 dev->dv_cfdata = cf;
1146 dev->dv_cfdriver = cd;
1147 dev->dv_cfattach = ca;
1148 dev->dv_unit = myunit;
1149 dev->dv_activity_count = 0;
1150 dev->dv_activity_handlers = NULL;
1151 dev->dv_private = dev_private;
1152 memcpy(dev->dv_xname, cd->cd_name, lname);
1153 memcpy(dev->dv_xname + lname, xunit, lunit);
1154 dev->dv_parent = parent;
1155 if (parent != NULL)
1156 dev->dv_depth = parent->dv_depth + 1;
1157 else
1158 dev->dv_depth = 0;
1159 dev->dv_flags = DVF_ACTIVE; /* always initially active */
1160 dev->dv_flags |= ca->ca_flags; /* inherit flags from class */
1161 if (locs) {
1162 KASSERT(parent); /* no locators at root */
1163 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
1164 parent->dv_cfdriver);
1165 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
1166 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1167 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
1168 }
1169 dev->dv_properties = prop_dictionary_create();
1170 KASSERT(dev->dv_properties != NULL);
1171
1172 return (dev);
1173 }
1174
1175 static void
1176 config_devdealloc(device_t dev)
1177 {
1178
1179 KASSERT(dev->dv_properties != NULL);
1180 prop_object_release(dev->dv_properties);
1181
1182 if (dev->dv_activity_handlers)
1183 panic("config_devdealloc with registered handlers");
1184
1185 if (dev->dv_locators)
1186 free(dev->dv_locators, M_DEVBUF);
1187
1188 if ((dev->dv_flags & DVF_PRIV_ALLOC) != 0)
1189 free(dev->dv_private, M_DEVBUF);
1190
1191 free(dev, M_DEVBUF);
1192 }
1193
1194 /*
1195 * Attach a found device.
1196 */
1197 device_t
1198 config_attach_loc(device_t parent, cfdata_t cf,
1199 const int *locs, void *aux, cfprint_t print)
1200 {
1201 device_t dev;
1202 struct cftable *ct;
1203 const char *drvname;
1204
1205 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1206 if (splash_progress_state)
1207 splash_progress_update(splash_progress_state);
1208 #endif
1209
1210 dev = config_devalloc(parent, cf, locs);
1211 if (!dev)
1212 panic("config_attach: allocation of device softc failed");
1213
1214 /* XXX redundant - see below? */
1215 if (cf->cf_fstate != FSTATE_STAR) {
1216 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1217 cf->cf_fstate = FSTATE_FOUND;
1218 }
1219 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1220 else
1221 cf->cf_unit++;
1222 #endif
1223
1224 config_devlink(dev);
1225
1226 if (config_do_twiddle)
1227 twiddle();
1228 else
1229 aprint_naive("Found ");
1230 /*
1231 * We want the next two printfs for normal, verbose, and quiet,
1232 * but not silent (in which case, we're twiddling, instead).
1233 */
1234 if (parent == ROOT) {
1235 aprint_naive("%s (root)", dev->dv_xname);
1236 aprint_normal("%s (root)", dev->dv_xname);
1237 } else {
1238 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
1239 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
1240 if (print)
1241 (void) (*print)(aux, NULL);
1242 }
1243
1244 /*
1245 * Before attaching, clobber any unfound devices that are
1246 * otherwise identical.
1247 * XXX code above is redundant?
1248 */
1249 drvname = dev->dv_cfdriver->cd_name;
1250 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1251 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1252 if (STREQ(cf->cf_name, drvname) &&
1253 cf->cf_unit == dev->dv_unit) {
1254 if (cf->cf_fstate == FSTATE_NOTFOUND)
1255 cf->cf_fstate = FSTATE_FOUND;
1256 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1257 /*
1258 * Bump the unit number on all starred cfdata
1259 * entries for this device.
1260 */
1261 if (cf->cf_fstate == FSTATE_STAR)
1262 cf->cf_unit++;
1263 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1264 }
1265 }
1266 }
1267 #ifdef __HAVE_DEVICE_REGISTER
1268 device_register(dev, aux);
1269 #endif
1270
1271 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1272 if (splash_progress_state)
1273 splash_progress_update(splash_progress_state);
1274 #endif
1275 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1276 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1277 if (splash_progress_state)
1278 splash_progress_update(splash_progress_state);
1279 #endif
1280
1281 if (!device_pmf_is_registered(dev))
1282 aprint_debug_dev(dev, "WARNING: power management not supported\n");
1283
1284 config_process_deferred(&deferred_config_queue, dev);
1285 return (dev);
1286 }
1287
1288 device_t
1289 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1290 {
1291
1292 return (config_attach_loc(parent, cf, NULL, aux, print));
1293 }
1294
1295 /*
1296 * As above, but for pseudo-devices. Pseudo-devices attached in this
1297 * way are silently inserted into the device tree, and their children
1298 * attached.
1299 *
1300 * Note that because pseudo-devices are attached silently, any information
1301 * the attach routine wishes to print should be prefixed with the device
1302 * name by the attach routine.
1303 */
1304 device_t
1305 config_attach_pseudo(cfdata_t cf)
1306 {
1307 device_t dev;
1308
1309 dev = config_devalloc(ROOT, cf, NULL);
1310 if (!dev)
1311 return (NULL);
1312
1313 /* XXX mark busy in cfdata */
1314
1315 config_devlink(dev);
1316
1317 #if 0 /* XXXJRT not yet */
1318 #ifdef __HAVE_DEVICE_REGISTER
1319 device_register(dev, NULL); /* like a root node */
1320 #endif
1321 #endif
1322 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1323 config_process_deferred(&deferred_config_queue, dev);
1324 return (dev);
1325 }
1326
1327 /*
1328 * Detach a device. Optionally forced (e.g. because of hardware
1329 * removal) and quiet. Returns zero if successful, non-zero
1330 * (an error code) otherwise.
1331 *
1332 * Note that this code wants to be run from a process context, so
1333 * that the detach can sleep to allow processes which have a device
1334 * open to run and unwind their stacks.
1335 */
1336 int
1337 config_detach(device_t dev, int flags)
1338 {
1339 struct cftable *ct;
1340 cfdata_t cf;
1341 const struct cfattach *ca;
1342 struct cfdriver *cd;
1343 #ifdef DIAGNOSTIC
1344 device_t d;
1345 #endif
1346 int rv = 0;
1347
1348 #ifdef DIAGNOSTIC
1349 if (dev->dv_cfdata != NULL &&
1350 dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
1351 dev->dv_cfdata->cf_fstate != FSTATE_STAR)
1352 panic("config_detach: bad device fstate");
1353 #endif
1354 cd = dev->dv_cfdriver;
1355 KASSERT(cd != NULL);
1356
1357 ca = dev->dv_cfattach;
1358 KASSERT(ca != NULL);
1359
1360 /*
1361 * Ensure the device is deactivated. If the device doesn't
1362 * have an activation entry point, we allow DVF_ACTIVE to
1363 * remain set. Otherwise, if DVF_ACTIVE is still set, the
1364 * device is busy, and the detach fails.
1365 */
1366 if (ca->ca_activate != NULL)
1367 rv = config_deactivate(dev);
1368
1369 /*
1370 * Try to detach the device. If that's not possible, then
1371 * we either panic() (for the forced but failed case), or
1372 * return an error.
1373 */
1374 if (rv == 0) {
1375 if (ca->ca_detach != NULL)
1376 rv = (*ca->ca_detach)(dev, flags);
1377 else
1378 rv = EOPNOTSUPP;
1379 }
1380 if (rv != 0) {
1381 if ((flags & DETACH_FORCE) == 0)
1382 return (rv);
1383 else
1384 panic("config_detach: forced detach of %s failed (%d)",
1385 dev->dv_xname, rv);
1386 }
1387
1388 /*
1389 * The device has now been successfully detached.
1390 */
1391
1392 #ifdef DIAGNOSTIC
1393 /*
1394 * Sanity: If you're successfully detached, you should have no
1395 * children. (Note that because children must be attached
1396 * after parents, we only need to search the latter part of
1397 * the list.)
1398 */
1399 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1400 d = TAILQ_NEXT(d, dv_list)) {
1401 if (d->dv_parent == dev) {
1402 printf("config_detach: detached device %s"
1403 " has children %s\n", dev->dv_xname, d->dv_xname);
1404 panic("config_detach");
1405 }
1406 }
1407 #endif
1408
1409 /* notify the parent that the child is gone */
1410 if (dev->dv_parent) {
1411 device_t p = dev->dv_parent;
1412 if (p->dv_cfattach->ca_childdetached)
1413 (*p->dv_cfattach->ca_childdetached)(p, dev);
1414 }
1415
1416 /*
1417 * Mark cfdata to show that the unit can be reused, if possible.
1418 */
1419 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1420 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1421 if (STREQ(cf->cf_name, cd->cd_name)) {
1422 if (cf->cf_fstate == FSTATE_FOUND &&
1423 cf->cf_unit == dev->dv_unit)
1424 cf->cf_fstate = FSTATE_NOTFOUND;
1425 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1426 /*
1427 * Note that we can only re-use a starred
1428 * unit number if the unit being detached
1429 * had the last assigned unit number.
1430 */
1431 if (cf->cf_fstate == FSTATE_STAR &&
1432 cf->cf_unit == dev->dv_unit + 1)
1433 cf->cf_unit--;
1434 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1435 }
1436 }
1437 }
1438
1439 config_devunlink(dev);
1440
1441 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1442 aprint_normal("%s detached\n", dev->dv_xname);
1443
1444 config_devdealloc(dev);
1445
1446 return (0);
1447 }
1448
1449 struct config_detach_arg {
1450 int a_flags;
1451 int a_error;
1452 };
1453
1454 static bool
1455 config_detach_helper(device_t child, void *arg)
1456 {
1457 struct config_detach_arg *a = arg;
1458
1459 return (a->a_error = config_detach(child, a->a_flags)) == 0;
1460 }
1461
1462 int
1463 config_detach_children(device_t parent, int flags)
1464 {
1465 struct config_detach_arg a = {.a_flags = flags, .a_error = 0};
1466
1467 device_foreach_child(parent, config_detach_helper, &a);
1468 return a.a_error;
1469 }
1470
1471 int
1472 config_activate(device_t dev)
1473 {
1474 const struct cfattach *ca = dev->dv_cfattach;
1475 int rv = 0, oflags = dev->dv_flags;
1476
1477 if (ca->ca_activate == NULL)
1478 return (EOPNOTSUPP);
1479
1480 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
1481 dev->dv_flags |= DVF_ACTIVE;
1482 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
1483 if (rv)
1484 dev->dv_flags = oflags;
1485 }
1486 return (rv);
1487 }
1488
1489 int
1490 config_deactivate(device_t dev)
1491 {
1492 const struct cfattach *ca = dev->dv_cfattach;
1493 int rv = 0, oflags = dev->dv_flags;
1494
1495 if (ca->ca_activate == NULL)
1496 return (EOPNOTSUPP);
1497
1498 if (dev->dv_flags & DVF_ACTIVE) {
1499 dev->dv_flags &= ~DVF_ACTIVE;
1500 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
1501 if (rv)
1502 dev->dv_flags = oflags;
1503 }
1504 return (rv);
1505 }
1506
1507 /*
1508 * Defer the configuration of the specified device until all
1509 * of its parent's devices have been attached.
1510 */
1511 void
1512 config_defer(device_t dev, void (*func)(device_t))
1513 {
1514 struct deferred_config *dc;
1515
1516 if (dev->dv_parent == NULL)
1517 panic("config_defer: can't defer config of a root device");
1518
1519 #ifdef DIAGNOSTIC
1520 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
1521 dc = TAILQ_NEXT(dc, dc_queue)) {
1522 if (dc->dc_dev == dev)
1523 panic("config_defer: deferred twice");
1524 }
1525 #endif
1526
1527 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1528 if (dc == NULL)
1529 panic("config_defer: unable to allocate callback");
1530
1531 dc->dc_dev = dev;
1532 dc->dc_func = func;
1533 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1534 config_pending_incr();
1535 }
1536
1537 /*
1538 * Defer some autoconfiguration for a device until after interrupts
1539 * are enabled.
1540 */
1541 void
1542 config_interrupts(device_t dev, void (*func)(device_t))
1543 {
1544 struct deferred_config *dc;
1545
1546 /*
1547 * If interrupts are enabled, callback now.
1548 */
1549 if (cold == 0) {
1550 (*func)(dev);
1551 return;
1552 }
1553
1554 #ifdef DIAGNOSTIC
1555 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
1556 dc = TAILQ_NEXT(dc, dc_queue)) {
1557 if (dc->dc_dev == dev)
1558 panic("config_interrupts: deferred twice");
1559 }
1560 #endif
1561
1562 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1563 if (dc == NULL)
1564 panic("config_interrupts: unable to allocate callback");
1565
1566 dc->dc_dev = dev;
1567 dc->dc_func = func;
1568 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1569 config_pending_incr();
1570 }
1571
1572 /*
1573 * Process a deferred configuration queue.
1574 */
1575 static void
1576 config_process_deferred(struct deferred_config_head *queue,
1577 device_t parent)
1578 {
1579 struct deferred_config *dc, *ndc;
1580
1581 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1582 ndc = TAILQ_NEXT(dc, dc_queue);
1583 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1584 TAILQ_REMOVE(queue, dc, dc_queue);
1585 (*dc->dc_func)(dc->dc_dev);
1586 free(dc, M_DEVBUF);
1587 config_pending_decr();
1588 }
1589 }
1590 }
1591
1592 /*
1593 * Manipulate the config_pending semaphore.
1594 */
1595 void
1596 config_pending_incr(void)
1597 {
1598
1599 config_pending++;
1600 }
1601
1602 void
1603 config_pending_decr(void)
1604 {
1605
1606 #ifdef DIAGNOSTIC
1607 if (config_pending == 0)
1608 panic("config_pending_decr: config_pending == 0");
1609 #endif
1610 config_pending--;
1611 if (config_pending == 0)
1612 wakeup(&config_pending);
1613 }
1614
1615 /*
1616 * Register a "finalization" routine. Finalization routines are
1617 * called iteratively once all real devices have been found during
1618 * autoconfiguration, for as long as any one finalizer has done
1619 * any work.
1620 */
1621 int
1622 config_finalize_register(device_t dev, int (*fn)(device_t))
1623 {
1624 struct finalize_hook *f;
1625
1626 /*
1627 * If finalization has already been done, invoke the
1628 * callback function now.
1629 */
1630 if (config_finalize_done) {
1631 while ((*fn)(dev) != 0)
1632 /* loop */ ;
1633 }
1634
1635 /* Ensure this isn't already on the list. */
1636 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
1637 if (f->f_func == fn && f->f_dev == dev)
1638 return (EEXIST);
1639 }
1640
1641 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
1642 f->f_func = fn;
1643 f->f_dev = dev;
1644 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
1645
1646 return (0);
1647 }
1648
1649 void
1650 config_finalize(void)
1651 {
1652 struct finalize_hook *f;
1653 int rv;
1654
1655 /* Run the hooks until none of them does any work. */
1656 do {
1657 rv = 0;
1658 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1659 rv |= (*f->f_func)(f->f_dev);
1660 } while (rv != 0);
1661
1662 config_finalize_done = 1;
1663
1664 /* Now free all the hooks. */
1665 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1666 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1667 free(f, M_TEMP);
1668 }
1669 }
1670
1671 /*
1672 * device_lookup:
1673 *
1674 * Look up a device instance for a given driver.
1675 */
1676 void *
1677 device_lookup(cfdriver_t cd, int unit)
1678 {
1679
1680 if (unit < 0 || unit >= cd->cd_ndevs)
1681 return (NULL);
1682
1683 return (cd->cd_devs[unit]);
1684 }
1685
1686 /*
1687 * Accessor functions for the device_t type.
1688 */
1689 devclass_t
1690 device_class(device_t dev)
1691 {
1692
1693 return (dev->dv_class);
1694 }
1695
1696 cfdata_t
1697 device_cfdata(device_t dev)
1698 {
1699
1700 return (dev->dv_cfdata);
1701 }
1702
1703 cfdriver_t
1704 device_cfdriver(device_t dev)
1705 {
1706
1707 return (dev->dv_cfdriver);
1708 }
1709
1710 cfattach_t
1711 device_cfattach(device_t dev)
1712 {
1713
1714 return (dev->dv_cfattach);
1715 }
1716
1717 int
1718 device_unit(device_t dev)
1719 {
1720
1721 return (dev->dv_unit);
1722 }
1723
1724 const char *
1725 device_xname(device_t dev)
1726 {
1727
1728 return (dev->dv_xname);
1729 }
1730
1731 device_t
1732 device_parent(device_t dev)
1733 {
1734
1735 return (dev->dv_parent);
1736 }
1737
1738 bool
1739 device_foreach_child(device_t parent, bool (*func)(device_t, void *), void *arg)
1740 {
1741 device_t curdev;
1742
1743 TAILQ_FOREACH(curdev, &alldevs, dv_list) {
1744 if (device_parent(curdev) != parent)
1745 continue;
1746 if (!(*func)(curdev, arg))
1747 return false;
1748 }
1749 return true;
1750 }
1751
1752 bool
1753 device_is_active(device_t dev)
1754 {
1755 int active_flags;
1756
1757 active_flags = DVF_ACTIVE;
1758 active_flags |= DVF_CLASS_SUSPENDED;
1759 active_flags |= DVF_DRIVER_SUSPENDED;
1760 active_flags |= DVF_BUS_SUSPENDED;
1761
1762 return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
1763 }
1764
1765 bool
1766 device_is_enabled(device_t dev)
1767 {
1768 return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
1769 }
1770
1771 bool
1772 device_has_power(device_t dev)
1773 {
1774 int active_flags;
1775
1776 active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
1777
1778 return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
1779 }
1780
1781 int
1782 device_locator(device_t dev, u_int locnum)
1783 {
1784
1785 KASSERT(dev->dv_locators != NULL);
1786 return (dev->dv_locators[locnum]);
1787 }
1788
1789 void *
1790 device_private(device_t dev)
1791 {
1792
1793 return (dev->dv_private);
1794 }
1795
1796 prop_dictionary_t
1797 device_properties(device_t dev)
1798 {
1799
1800 return (dev->dv_properties);
1801 }
1802
1803 /*
1804 * device_is_a:
1805 *
1806 * Returns true if the device is an instance of the specified
1807 * driver.
1808 */
1809 bool
1810 device_is_a(device_t dev, const char *dname)
1811 {
1812
1813 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0);
1814 }
1815
1816 /*
1817 * Power management related functions.
1818 */
1819
1820 bool
1821 device_pmf_is_registered(device_t dev)
1822 {
1823 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
1824 }
1825
1826 bool
1827 device_pmf_driver_suspend(device_t dev)
1828 {
1829 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
1830 return true;
1831 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
1832 return false;
1833 if (*dev->dv_driver_suspend != NULL &&
1834 !(*dev->dv_driver_suspend)(dev))
1835 return false;
1836
1837 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
1838 return true;
1839 }
1840
1841 bool
1842 device_pmf_driver_resume(device_t dev)
1843 {
1844 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
1845 return true;
1846 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
1847 return false;
1848 if (*dev->dv_driver_resume != NULL &&
1849 !(*dev->dv_driver_resume)(dev))
1850 return false;
1851
1852 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
1853 return true;
1854 }
1855
1856 void
1857 device_pmf_driver_register(device_t dev,
1858 bool (*suspend)(device_t), bool (*resume)(device_t))
1859 {
1860 dev->dv_driver_suspend = suspend;
1861 dev->dv_driver_resume = resume;
1862 dev->dv_flags |= DVF_POWER_HANDLERS;
1863 }
1864
1865 void
1866 device_pmf_driver_deregister(device_t dev)
1867 {
1868 dev->dv_driver_suspend = NULL;
1869 dev->dv_driver_resume = NULL;
1870 dev->dv_flags &= ~DVF_POWER_HANDLERS;
1871 }
1872
1873 bool
1874 device_pmf_driver_child_register(device_t dev)
1875 {
1876 device_t parent = device_parent(dev);
1877
1878 if (parent == NULL || parent->dv_driver_child_register == NULL)
1879 return true;
1880 return (*parent->dv_driver_child_register)(dev);
1881 }
1882
1883 void
1884 device_pmf_driver_set_child_register(device_t dev,
1885 bool (*child_register)(device_t))
1886 {
1887 dev->dv_driver_child_register = child_register;
1888 }
1889
1890 void *
1891 device_pmf_bus_private(device_t dev)
1892 {
1893 return dev->dv_bus_private;
1894 }
1895
1896 bool
1897 device_pmf_bus_suspend(device_t dev)
1898 {
1899 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
1900 return true;
1901 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
1902 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
1903 return false;
1904 if (*dev->dv_bus_suspend != NULL &&
1905 !(*dev->dv_bus_suspend)(dev))
1906 return false;
1907
1908 dev->dv_flags |= DVF_BUS_SUSPENDED;
1909 return true;
1910 }
1911
1912 bool
1913 device_pmf_bus_resume(device_t dev)
1914 {
1915 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
1916 return true;
1917 if (*dev->dv_bus_resume != NULL &&
1918 !(*dev->dv_bus_resume)(dev))
1919 return false;
1920
1921 dev->dv_flags &= ~DVF_BUS_SUSPENDED;
1922 return true;
1923 }
1924
1925 void
1926 device_pmf_bus_register(device_t dev, void *priv,
1927 bool (*suspend)(device_t), bool (*resume)(device_t),
1928 void (*deregister)(device_t))
1929 {
1930 dev->dv_bus_private = priv;
1931 dev->dv_bus_resume = resume;
1932 dev->dv_bus_suspend = suspend;
1933 dev->dv_bus_deregister = deregister;
1934 }
1935
1936 void
1937 device_pmf_bus_deregister(device_t dev)
1938 {
1939 if (dev->dv_bus_deregister == NULL)
1940 return;
1941 (*dev->dv_bus_deregister)(dev);
1942 dev->dv_bus_private = NULL;
1943 dev->dv_bus_suspend = NULL;
1944 dev->dv_bus_resume = NULL;
1945 dev->dv_bus_deregister = NULL;
1946 }
1947
1948 void *
1949 device_pmf_class_private(device_t dev)
1950 {
1951 return dev->dv_class_private;
1952 }
1953
1954 bool
1955 device_pmf_class_suspend(device_t dev)
1956 {
1957 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
1958 return true;
1959 if (*dev->dv_class_suspend != NULL &&
1960 !(*dev->dv_class_suspend)(dev))
1961 return false;
1962
1963 dev->dv_flags |= DVF_CLASS_SUSPENDED;
1964 return true;
1965 }
1966
1967 bool
1968 device_pmf_class_resume(device_t dev)
1969 {
1970 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
1971 return true;
1972 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
1973 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
1974 return false;
1975 if (*dev->dv_class_resume != NULL &&
1976 !(*dev->dv_class_resume)(dev))
1977 return false;
1978
1979 dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
1980 return true;
1981 }
1982
1983 void
1984 device_pmf_class_register(device_t dev, void *priv,
1985 bool (*suspend)(device_t), bool (*resume)(device_t),
1986 void (*deregister)(device_t))
1987 {
1988 dev->dv_class_private = priv;
1989 dev->dv_class_suspend = suspend;
1990 dev->dv_class_resume = resume;
1991 dev->dv_class_deregister = deregister;
1992 }
1993
1994 void
1995 device_pmf_class_deregister(device_t dev)
1996 {
1997 if (dev->dv_class_deregister == NULL)
1998 return;
1999 (*dev->dv_class_deregister)(dev);
2000 dev->dv_class_private = NULL;
2001 dev->dv_class_suspend = NULL;
2002 dev->dv_class_resume = NULL;
2003 dev->dv_class_deregister = NULL;
2004 }
2005
2006 bool
2007 device_active(device_t dev, devactive_t type)
2008 {
2009 size_t i;
2010
2011 if (dev->dv_activity_count == 0)
2012 return false;
2013
2014 for (i = 0; i < dev->dv_activity_count; ++i)
2015 (*dev->dv_activity_handlers[i])(dev, type);
2016
2017 return true;
2018 }
2019
2020 bool
2021 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
2022 {
2023 void (**new_handlers)(device_t, devactive_t);
2024 void (**old_handlers)(device_t, devactive_t);
2025 size_t i, new_size;
2026 int s;
2027
2028 old_handlers = dev->dv_activity_handlers;
2029
2030 for (i = 0; i < dev->dv_activity_count; ++i) {
2031 if (old_handlers[i] == handler)
2032 panic("Double registering of idle handlers");
2033 }
2034
2035 new_size = dev->dv_activity_count + 1;
2036 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, M_WAITOK);
2037
2038 memcpy(new_handlers, old_handlers,
2039 sizeof(void *) * dev->dv_activity_count);
2040 new_handlers[new_size - 1] = handler;
2041
2042 s = splhigh();
2043 dev->dv_activity_count = new_size;
2044 dev->dv_activity_handlers = new_handlers;
2045 splx(s);
2046
2047 if (old_handlers != NULL)
2048 free(old_handlers, M_DEVBUF);
2049
2050 return true;
2051 }
2052
2053 void
2054 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
2055 {
2056 void (**new_handlers)(device_t, devactive_t);
2057 void (**old_handlers)(device_t, devactive_t);
2058 size_t i, new_size;
2059 int s;
2060
2061 old_handlers = dev->dv_activity_handlers;
2062
2063 for (i = 0; i < dev->dv_activity_count; ++i) {
2064 if (old_handlers[i] == handler)
2065 break;
2066 }
2067
2068 if (i == dev->dv_activity_count)
2069 return; /* XXX panic? */
2070
2071 new_size = dev->dv_activity_count - 1;
2072
2073 if (new_size == 0) {
2074 new_handlers = NULL;
2075 } else {
2076 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF,
2077 M_WAITOK);
2078 memcpy(new_handlers, old_handlers, sizeof(void *) * i);
2079 memcpy(new_handlers + i, old_handlers + i + 1,
2080 sizeof(void *) * (new_size - i));
2081 }
2082
2083 s = splhigh();
2084 dev->dv_activity_count = new_size;
2085 dev->dv_activity_handlers = new_handlers;
2086 splx(s);
2087
2088 free(old_handlers, M_DEVBUF);
2089 }
2090