subr_autoconf.c revision 1.266 1 /* $NetBSD: subr_autoconf.c,v 1.266 2020/02/20 21:14:23 jdolecek 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.266 2020/02/20 21:14:23 jdolecek Exp $");
81
82 #ifdef _KERNEL_OPT
83 #include "opt_ddb.h"
84 #include "drvctl.h"
85 #endif
86
87 #include <sys/param.h>
88 #include <sys/device.h>
89 #include <sys/disklabel.h>
90 #include <sys/conf.h>
91 #include <sys/kauth.h>
92 #include <sys/kmem.h>
93 #include <sys/systm.h>
94 #include <sys/kernel.h>
95 #include <sys/errno.h>
96 #include <sys/proc.h>
97 #include <sys/reboot.h>
98 #include <sys/kthread.h>
99 #include <sys/buf.h>
100 #include <sys/dirent.h>
101 #include <sys/mount.h>
102 #include <sys/namei.h>
103 #include <sys/unistd.h>
104 #include <sys/fcntl.h>
105 #include <sys/lockf.h>
106 #include <sys/callout.h>
107 #include <sys/devmon.h>
108 #include <sys/cpu.h>
109 #include <sys/sysctl.h>
110
111 #include <sys/disk.h>
112
113 #include <sys/rndsource.h>
114
115 #include <machine/limits.h>
116
117 /*
118 * Autoconfiguration subroutines.
119 */
120
121 /*
122 * Device autoconfiguration timings are mixed into the entropy pool.
123 */
124 extern krndsource_t rnd_autoconf_source;
125
126 /*
127 * ioconf.c exports exactly two names: cfdata and cfroots. All system
128 * devices and drivers are found via these tables.
129 */
130 extern struct cfdata cfdata[];
131 extern const short cfroots[];
132
133 /*
134 * List of all cfdriver structures. We use this to detect duplicates
135 * when other cfdrivers are loaded.
136 */
137 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
138 extern struct cfdriver * const cfdriver_list_initial[];
139
140 /*
141 * Initial list of cfattach's.
142 */
143 extern const struct cfattachinit cfattachinit[];
144
145 /*
146 * List of cfdata tables. We always have one such list -- the one
147 * built statically when the kernel was configured.
148 */
149 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
150 static struct cftable initcftable;
151
152 #define ROOT ((device_t)NULL)
153
154 struct matchinfo {
155 cfsubmatch_t fn;
156 device_t parent;
157 const int *locs;
158 void *aux;
159 struct cfdata *match;
160 int pri;
161 };
162
163 struct alldevs_foray {
164 int af_s;
165 struct devicelist af_garbage;
166 };
167
168 static char *number(char *, int);
169 static void mapply(struct matchinfo *, cfdata_t);
170 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
171 static void config_devdelete(device_t);
172 static void config_devunlink(device_t, struct devicelist *);
173 static void config_makeroom(int, struct cfdriver *);
174 static void config_devlink(device_t);
175 static void config_alldevs_enter(struct alldevs_foray *);
176 static void config_alldevs_exit(struct alldevs_foray *);
177 static void config_add_attrib_dict(device_t);
178
179 static void config_collect_garbage(struct devicelist *);
180 static void config_dump_garbage(struct devicelist *);
181
182 static void pmflock_debug(device_t, const char *, int);
183
184 static device_t deviter_next1(deviter_t *);
185 static void deviter_reinit(deviter_t *);
186
187 struct deferred_config {
188 TAILQ_ENTRY(deferred_config) dc_queue;
189 device_t dc_dev;
190 void (*dc_func)(device_t);
191 };
192
193 TAILQ_HEAD(deferred_config_head, deferred_config);
194
195 static struct deferred_config_head deferred_config_queue =
196 TAILQ_HEAD_INITIALIZER(deferred_config_queue);
197 static struct deferred_config_head interrupt_config_queue =
198 TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
199 static int interrupt_config_threads = 8;
200 static struct deferred_config_head mountroot_config_queue =
201 TAILQ_HEAD_INITIALIZER(mountroot_config_queue);
202 static int mountroot_config_threads = 2;
203 static lwp_t **mountroot_config_lwpids;
204 static size_t mountroot_config_lwpids_size;
205 bool root_is_mounted = false;
206
207 static void config_process_deferred(struct deferred_config_head *, device_t);
208
209 /* Hooks to finalize configuration once all real devices have been found. */
210 struct finalize_hook {
211 TAILQ_ENTRY(finalize_hook) f_list;
212 int (*f_func)(device_t);
213 device_t f_dev;
214 };
215 static TAILQ_HEAD(, finalize_hook) config_finalize_list =
216 TAILQ_HEAD_INITIALIZER(config_finalize_list);
217 static int config_finalize_done;
218
219 /* list of all devices */
220 static struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
221 static kmutex_t alldevs_lock __cacheline_aligned;
222 static devgen_t alldevs_gen = 1;
223 static int alldevs_nread = 0;
224 static int alldevs_nwrite = 0;
225 static bool alldevs_garbage = false;
226
227 static int config_pending; /* semaphore for mountroot */
228 static kmutex_t config_misc_lock;
229 static kcondvar_t config_misc_cv;
230
231 static bool detachall = false;
232
233 #define STREQ(s1, s2) \
234 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
235
236 static bool config_initialized = false; /* config_init() has been called. */
237
238 static int config_do_twiddle;
239 static callout_t config_twiddle_ch;
240
241 static void sysctl_detach_setup(struct sysctllog **);
242
243 int no_devmon_insert(const char *, prop_dictionary_t);
244 int (*devmon_insert_vec)(const char *, prop_dictionary_t) = no_devmon_insert;
245
246 typedef int (*cfdriver_fn)(struct cfdriver *);
247 static int
248 frob_cfdrivervec(struct cfdriver * const *cfdriverv,
249 cfdriver_fn drv_do, cfdriver_fn drv_undo,
250 const char *style, bool dopanic)
251 {
252 void (*pr)(const char *, ...) __printflike(1, 2) =
253 dopanic ? panic : printf;
254 int i, error = 0, e2 __diagused;
255
256 for (i = 0; cfdriverv[i] != NULL; i++) {
257 if ((error = drv_do(cfdriverv[i])) != 0) {
258 pr("configure: `%s' driver %s failed: %d",
259 cfdriverv[i]->cd_name, style, error);
260 goto bad;
261 }
262 }
263
264 KASSERT(error == 0);
265 return 0;
266
267 bad:
268 printf("\n");
269 for (i--; i >= 0; i--) {
270 e2 = drv_undo(cfdriverv[i]);
271 KASSERT(e2 == 0);
272 }
273
274 return error;
275 }
276
277 typedef int (*cfattach_fn)(const char *, struct cfattach *);
278 static int
279 frob_cfattachvec(const struct cfattachinit *cfattachv,
280 cfattach_fn att_do, cfattach_fn att_undo,
281 const char *style, bool dopanic)
282 {
283 const struct cfattachinit *cfai = NULL;
284 void (*pr)(const char *, ...) __printflike(1, 2) =
285 dopanic ? panic : printf;
286 int j = 0, error = 0, e2 __diagused;
287
288 for (cfai = &cfattachv[0]; cfai->cfai_name != NULL; cfai++) {
289 for (j = 0; cfai->cfai_list[j] != NULL; j++) {
290 if ((error = att_do(cfai->cfai_name,
291 cfai->cfai_list[j])) != 0) {
292 pr("configure: attachment `%s' "
293 "of `%s' driver %s failed: %d",
294 cfai->cfai_list[j]->ca_name,
295 cfai->cfai_name, style, error);
296 goto bad;
297 }
298 }
299 }
300
301 KASSERT(error == 0);
302 return 0;
303
304 bad:
305 /*
306 * Rollback in reverse order. dunno if super-important, but
307 * do that anyway. Although the code looks a little like
308 * someone did a little integration (in the math sense).
309 */
310 printf("\n");
311 if (cfai) {
312 bool last;
313
314 for (last = false; last == false; ) {
315 if (cfai == &cfattachv[0])
316 last = true;
317 for (j--; j >= 0; j--) {
318 e2 = att_undo(cfai->cfai_name,
319 cfai->cfai_list[j]);
320 KASSERT(e2 == 0);
321 }
322 if (!last) {
323 cfai--;
324 for (j = 0; cfai->cfai_list[j] != NULL; j++)
325 ;
326 }
327 }
328 }
329
330 return error;
331 }
332
333 /*
334 * Initialize the autoconfiguration data structures. Normally this
335 * is done by configure(), but some platforms need to do this very
336 * early (to e.g. initialize the console).
337 */
338 void
339 config_init(void)
340 {
341
342 KASSERT(config_initialized == false);
343
344 mutex_init(&alldevs_lock, MUTEX_DEFAULT, IPL_VM);
345
346 mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE);
347 cv_init(&config_misc_cv, "cfgmisc");
348
349 callout_init(&config_twiddle_ch, CALLOUT_MPSAFE);
350
351 frob_cfdrivervec(cfdriver_list_initial,
352 config_cfdriver_attach, NULL, "bootstrap", true);
353 frob_cfattachvec(cfattachinit,
354 config_cfattach_attach, NULL, "bootstrap", true);
355
356 initcftable.ct_cfdata = cfdata;
357 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
358
359 config_initialized = true;
360 }
361
362 /*
363 * Init or fini drivers and attachments. Either all or none
364 * are processed (via rollback). It would be nice if this were
365 * atomic to outside consumers, but with the current state of
366 * locking ...
367 */
368 int
369 config_init_component(struct cfdriver * const *cfdriverv,
370 const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
371 {
372 int error;
373
374 if ((error = frob_cfdrivervec(cfdriverv,
375 config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0)
376 return error;
377 if ((error = frob_cfattachvec(cfattachv,
378 config_cfattach_attach, config_cfattach_detach,
379 "init", false)) != 0) {
380 frob_cfdrivervec(cfdriverv,
381 config_cfdriver_detach, NULL, "init rollback", true);
382 return error;
383 }
384 if ((error = config_cfdata_attach(cfdatav, 1)) != 0) {
385 frob_cfattachvec(cfattachv,
386 config_cfattach_detach, NULL, "init rollback", true);
387 frob_cfdrivervec(cfdriverv,
388 config_cfdriver_detach, NULL, "init rollback", true);
389 return error;
390 }
391
392 return 0;
393 }
394
395 int
396 config_fini_component(struct cfdriver * const *cfdriverv,
397 const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
398 {
399 int error;
400
401 if ((error = config_cfdata_detach(cfdatav)) != 0)
402 return error;
403 if ((error = frob_cfattachvec(cfattachv,
404 config_cfattach_detach, config_cfattach_attach,
405 "fini", false)) != 0) {
406 if (config_cfdata_attach(cfdatav, 0) != 0)
407 panic("config_cfdata fini rollback failed");
408 return error;
409 }
410 if ((error = frob_cfdrivervec(cfdriverv,
411 config_cfdriver_detach, config_cfdriver_attach,
412 "fini", false)) != 0) {
413 frob_cfattachvec(cfattachv,
414 config_cfattach_attach, NULL, "fini rollback", true);
415 if (config_cfdata_attach(cfdatav, 0) != 0)
416 panic("config_cfdata fini rollback failed");
417 return error;
418 }
419
420 return 0;
421 }
422
423 void
424 config_init_mi(void)
425 {
426
427 if (!config_initialized)
428 config_init();
429
430 sysctl_detach_setup(NULL);
431 }
432
433 void
434 config_deferred(device_t dev)
435 {
436 config_process_deferred(&deferred_config_queue, dev);
437 config_process_deferred(&interrupt_config_queue, dev);
438 config_process_deferred(&mountroot_config_queue, dev);
439 }
440
441 static void
442 config_interrupts_thread(void *cookie)
443 {
444 struct deferred_config *dc;
445
446 mutex_enter(&config_misc_lock);
447 while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) {
448 TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue);
449 mutex_exit(&config_misc_lock);
450
451 (*dc->dc_func)(dc->dc_dev);
452 if (!device_pmf_is_registered(dc->dc_dev))
453 aprint_debug_dev(dc->dc_dev,
454 "WARNING: power management not supported\n");
455 config_pending_decr(dc->dc_dev);
456 kmem_free(dc, sizeof(*dc));
457
458 mutex_enter(&config_misc_lock);
459 dc->dc_dev->dv_flags &= ~DVF_ATTACH_INPROGRESS;
460 }
461 mutex_exit(&config_misc_lock);
462
463 kthread_exit(0);
464 }
465
466 void
467 config_create_interruptthreads(void)
468 {
469 int i;
470
471 for (i = 0; i < interrupt_config_threads; i++) {
472 (void)kthread_create(PRI_NONE, 0/*XXXSMP */, NULL,
473 config_interrupts_thread, NULL, NULL, "configintr");
474 }
475 }
476
477 static void
478 config_mountroot_thread(void *cookie)
479 {
480 struct deferred_config *dc;
481
482 mutex_enter(&config_misc_lock);
483 while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) {
484 TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue);
485 mutex_exit(&config_misc_lock);
486
487 (*dc->dc_func)(dc->dc_dev);
488 kmem_free(dc, sizeof(*dc));
489
490 mutex_enter(&config_misc_lock);
491 }
492 mutex_exit(&config_misc_lock);
493
494 kthread_exit(0);
495 }
496
497 void
498 config_create_mountrootthreads(void)
499 {
500 int i;
501
502 if (!root_is_mounted)
503 root_is_mounted = true;
504
505 mountroot_config_lwpids_size = sizeof(mountroot_config_lwpids) *
506 mountroot_config_threads;
507 mountroot_config_lwpids = kmem_alloc(mountroot_config_lwpids_size,
508 KM_NOSLEEP);
509 KASSERT(mountroot_config_lwpids);
510 for (i = 0; i < mountroot_config_threads; i++) {
511 mountroot_config_lwpids[i] = 0;
512 (void)kthread_create(PRI_NONE, KTHREAD_MUSTJOIN/* XXXSMP */,
513 NULL, config_mountroot_thread, NULL,
514 &mountroot_config_lwpids[i],
515 "configroot");
516 }
517 }
518
519 void
520 config_finalize_mountroot(void)
521 {
522 int i, error;
523
524 for (i = 0; i < mountroot_config_threads; i++) {
525 if (mountroot_config_lwpids[i] == 0)
526 continue;
527
528 error = kthread_join(mountroot_config_lwpids[i]);
529 if (error)
530 printf("%s: thread %x joined with error %d\n",
531 __func__, i, error);
532 }
533 kmem_free(mountroot_config_lwpids, mountroot_config_lwpids_size);
534 }
535
536 /*
537 * Announce device attach/detach to userland listeners.
538 */
539
540 int
541 no_devmon_insert(const char *name, prop_dictionary_t p)
542 {
543
544 return ENODEV;
545 }
546
547 static void
548 devmon_report_device(device_t dev, bool isattach)
549 {
550 prop_dictionary_t ev;
551 const char *parent;
552 const char *what;
553 device_t pdev = device_parent(dev);
554
555 /* If currently no drvctl device, just return */
556 if (devmon_insert_vec == no_devmon_insert)
557 return;
558
559 ev = prop_dictionary_create();
560 if (ev == NULL)
561 return;
562
563 what = (isattach ? "device-attach" : "device-detach");
564 parent = (pdev == NULL ? "root" : device_xname(pdev));
565 if (!prop_dictionary_set_cstring(ev, "device", device_xname(dev)) ||
566 !prop_dictionary_set_cstring(ev, "parent", parent)) {
567 prop_object_release(ev);
568 return;
569 }
570
571 if ((*devmon_insert_vec)(what, ev) != 0)
572 prop_object_release(ev);
573 }
574
575 /*
576 * Add a cfdriver to the system.
577 */
578 int
579 config_cfdriver_attach(struct cfdriver *cd)
580 {
581 struct cfdriver *lcd;
582
583 /* Make sure this driver isn't already in the system. */
584 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
585 if (STREQ(lcd->cd_name, cd->cd_name))
586 return EEXIST;
587 }
588
589 LIST_INIT(&cd->cd_attach);
590 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
591
592 return 0;
593 }
594
595 /*
596 * Remove a cfdriver from the system.
597 */
598 int
599 config_cfdriver_detach(struct cfdriver *cd)
600 {
601 struct alldevs_foray af;
602 int i, rc = 0;
603
604 config_alldevs_enter(&af);
605 /* Make sure there are no active instances. */
606 for (i = 0; i < cd->cd_ndevs; i++) {
607 if (cd->cd_devs[i] != NULL) {
608 rc = EBUSY;
609 break;
610 }
611 }
612 config_alldevs_exit(&af);
613
614 if (rc != 0)
615 return rc;
616
617 /* ...and no attachments loaded. */
618 if (LIST_EMPTY(&cd->cd_attach) == 0)
619 return EBUSY;
620
621 LIST_REMOVE(cd, cd_list);
622
623 KASSERT(cd->cd_devs == NULL);
624
625 return 0;
626 }
627
628 /*
629 * Look up a cfdriver by name.
630 */
631 struct cfdriver *
632 config_cfdriver_lookup(const char *name)
633 {
634 struct cfdriver *cd;
635
636 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
637 if (STREQ(cd->cd_name, name))
638 return cd;
639 }
640
641 return NULL;
642 }
643
644 /*
645 * Add a cfattach to the specified driver.
646 */
647 int
648 config_cfattach_attach(const char *driver, struct cfattach *ca)
649 {
650 struct cfattach *lca;
651 struct cfdriver *cd;
652
653 cd = config_cfdriver_lookup(driver);
654 if (cd == NULL)
655 return ESRCH;
656
657 /* Make sure this attachment isn't already on this driver. */
658 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
659 if (STREQ(lca->ca_name, ca->ca_name))
660 return EEXIST;
661 }
662
663 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
664
665 return 0;
666 }
667
668 /*
669 * Remove a cfattach from the specified driver.
670 */
671 int
672 config_cfattach_detach(const char *driver, struct cfattach *ca)
673 {
674 struct alldevs_foray af;
675 struct cfdriver *cd;
676 device_t dev;
677 int i, rc = 0;
678
679 cd = config_cfdriver_lookup(driver);
680 if (cd == NULL)
681 return ESRCH;
682
683 config_alldevs_enter(&af);
684 /* Make sure there are no active instances. */
685 for (i = 0; i < cd->cd_ndevs; i++) {
686 if ((dev = cd->cd_devs[i]) == NULL)
687 continue;
688 if (dev->dv_cfattach == ca) {
689 rc = EBUSY;
690 break;
691 }
692 }
693 config_alldevs_exit(&af);
694
695 if (rc != 0)
696 return rc;
697
698 LIST_REMOVE(ca, ca_list);
699
700 return 0;
701 }
702
703 /*
704 * Look up a cfattach by name.
705 */
706 static struct cfattach *
707 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
708 {
709 struct cfattach *ca;
710
711 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
712 if (STREQ(ca->ca_name, atname))
713 return ca;
714 }
715
716 return NULL;
717 }
718
719 /*
720 * Look up a cfattach by driver/attachment name.
721 */
722 struct cfattach *
723 config_cfattach_lookup(const char *name, const char *atname)
724 {
725 struct cfdriver *cd;
726
727 cd = config_cfdriver_lookup(name);
728 if (cd == NULL)
729 return NULL;
730
731 return config_cfattach_lookup_cd(cd, atname);
732 }
733
734 /*
735 * Apply the matching function and choose the best. This is used
736 * a few times and we want to keep the code small.
737 */
738 static void
739 mapply(struct matchinfo *m, cfdata_t cf)
740 {
741 int pri;
742
743 if (m->fn != NULL) {
744 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
745 } else {
746 pri = config_match(m->parent, cf, m->aux);
747 }
748 if (pri > m->pri) {
749 m->match = cf;
750 m->pri = pri;
751 }
752 }
753
754 int
755 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
756 {
757 const struct cfiattrdata *ci;
758 const struct cflocdesc *cl;
759 int nlocs, i;
760
761 ci = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
762 KASSERT(ci);
763 nlocs = ci->ci_loclen;
764 KASSERT(!nlocs || locs);
765 for (i = 0; i < nlocs; i++) {
766 cl = &ci->ci_locdesc[i];
767 if (cl->cld_defaultstr != NULL &&
768 cf->cf_loc[i] == cl->cld_default)
769 continue;
770 if (cf->cf_loc[i] == locs[i])
771 continue;
772 return 0;
773 }
774
775 return config_match(parent, cf, aux);
776 }
777
778 /*
779 * Helper function: check whether the driver supports the interface attribute
780 * and return its descriptor structure.
781 */
782 static const struct cfiattrdata *
783 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
784 {
785 const struct cfiattrdata * const *cpp;
786
787 if (cd->cd_attrs == NULL)
788 return 0;
789
790 for (cpp = cd->cd_attrs; *cpp; cpp++) {
791 if (STREQ((*cpp)->ci_name, ia)) {
792 /* Match. */
793 return *cpp;
794 }
795 }
796 return 0;
797 }
798
799 /*
800 * Lookup an interface attribute description by name.
801 * If the driver is given, consider only its supported attributes.
802 */
803 const struct cfiattrdata *
804 cfiattr_lookup(const char *name, const struct cfdriver *cd)
805 {
806 const struct cfdriver *d;
807 const struct cfiattrdata *ia;
808
809 if (cd)
810 return cfdriver_get_iattr(cd, name);
811
812 LIST_FOREACH(d, &allcfdrivers, cd_list) {
813 ia = cfdriver_get_iattr(d, name);
814 if (ia)
815 return ia;
816 }
817 return 0;
818 }
819
820 /*
821 * Determine if `parent' is a potential parent for a device spec based
822 * on `cfp'.
823 */
824 static int
825 cfparent_match(const device_t parent, const struct cfparent *cfp)
826 {
827 struct cfdriver *pcd;
828
829 /* We don't match root nodes here. */
830 if (cfp == NULL)
831 return 0;
832
833 pcd = parent->dv_cfdriver;
834 KASSERT(pcd != NULL);
835
836 /*
837 * First, ensure this parent has the correct interface
838 * attribute.
839 */
840 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
841 return 0;
842
843 /*
844 * If no specific parent device instance was specified (i.e.
845 * we're attaching to the attribute only), we're done!
846 */
847 if (cfp->cfp_parent == NULL)
848 return 1;
849
850 /*
851 * Check the parent device's name.
852 */
853 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
854 return 0; /* not the same parent */
855
856 /*
857 * Make sure the unit number matches.
858 */
859 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
860 cfp->cfp_unit == parent->dv_unit)
861 return 1;
862
863 /* Unit numbers don't match. */
864 return 0;
865 }
866
867 /*
868 * Helper for config_cfdata_attach(): check all devices whether it could be
869 * parent any attachment in the config data table passed, and rescan.
870 */
871 static void
872 rescan_with_cfdata(const struct cfdata *cf)
873 {
874 device_t d;
875 const struct cfdata *cf1;
876 deviter_t di;
877
878
879 /*
880 * "alldevs" is likely longer than a modules's cfdata, so make it
881 * the outer loop.
882 */
883 for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) {
884
885 if (!(d->dv_cfattach->ca_rescan))
886 continue;
887
888 for (cf1 = cf; cf1->cf_name; cf1++) {
889
890 if (!cfparent_match(d, cf1->cf_pspec))
891 continue;
892
893 (*d->dv_cfattach->ca_rescan)(d,
894 cfdata_ifattr(cf1), cf1->cf_loc);
895
896 config_deferred(d);
897 }
898 }
899 deviter_release(&di);
900 }
901
902 /*
903 * Attach a supplemental config data table and rescan potential
904 * parent devices if required.
905 */
906 int
907 config_cfdata_attach(cfdata_t cf, int scannow)
908 {
909 struct cftable *ct;
910
911 ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
912 ct->ct_cfdata = cf;
913 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
914
915 if (scannow)
916 rescan_with_cfdata(cf);
917
918 return 0;
919 }
920
921 /*
922 * Helper for config_cfdata_detach: check whether a device is
923 * found through any attachment in the config data table.
924 */
925 static int
926 dev_in_cfdata(device_t d, cfdata_t cf)
927 {
928 const struct cfdata *cf1;
929
930 for (cf1 = cf; cf1->cf_name; cf1++)
931 if (d->dv_cfdata == cf1)
932 return 1;
933
934 return 0;
935 }
936
937 /*
938 * Detach a supplemental config data table. Detach all devices found
939 * through that table (and thus keeping references to it) before.
940 */
941 int
942 config_cfdata_detach(cfdata_t cf)
943 {
944 device_t d;
945 int error = 0;
946 struct cftable *ct;
947 deviter_t di;
948
949 for (d = deviter_first(&di, DEVITER_F_RW); d != NULL;
950 d = deviter_next(&di)) {
951 if (!dev_in_cfdata(d, cf))
952 continue;
953 if ((error = config_detach(d, 0)) != 0)
954 break;
955 }
956 deviter_release(&di);
957 if (error) {
958 aprint_error_dev(d, "unable to detach instance\n");
959 return error;
960 }
961
962 TAILQ_FOREACH(ct, &allcftables, ct_list) {
963 if (ct->ct_cfdata == cf) {
964 TAILQ_REMOVE(&allcftables, ct, ct_list);
965 kmem_free(ct, sizeof(*ct));
966 return 0;
967 }
968 }
969
970 /* not found -- shouldn't happen */
971 return EINVAL;
972 }
973
974 /*
975 * Invoke the "match" routine for a cfdata entry on behalf of
976 * an external caller, usually a "submatch" routine.
977 */
978 int
979 config_match(device_t parent, cfdata_t cf, void *aux)
980 {
981 struct cfattach *ca;
982
983 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
984 if (ca == NULL) {
985 /* No attachment for this entry, oh well. */
986 return 0;
987 }
988
989 return (*ca->ca_match)(parent, cf, aux);
990 }
991
992 /*
993 * Iterate over all potential children of some device, calling the given
994 * function (default being the child's match function) for each one.
995 * Nonzero returns are matches; the highest value returned is considered
996 * the best match. Return the `found child' if we got a match, or NULL
997 * otherwise. The `aux' pointer is simply passed on through.
998 *
999 * Note that this function is designed so that it can be used to apply
1000 * an arbitrary function to all potential children (its return value
1001 * can be ignored).
1002 */
1003 cfdata_t
1004 config_search_loc(cfsubmatch_t fn, device_t parent,
1005 const char *ifattr, const int *locs, void *aux)
1006 {
1007 struct cftable *ct;
1008 cfdata_t cf;
1009 struct matchinfo m;
1010
1011 KASSERT(config_initialized);
1012 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
1013
1014 m.fn = fn;
1015 m.parent = parent;
1016 m.locs = locs;
1017 m.aux = aux;
1018 m.match = NULL;
1019 m.pri = 0;
1020
1021 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1022 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1023
1024 /* We don't match root nodes here. */
1025 if (!cf->cf_pspec)
1026 continue;
1027
1028 /*
1029 * Skip cf if no longer eligible, otherwise scan
1030 * through parents for one matching `parent', and
1031 * try match function.
1032 */
1033 if (cf->cf_fstate == FSTATE_FOUND)
1034 continue;
1035 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
1036 cf->cf_fstate == FSTATE_DSTAR)
1037 continue;
1038
1039 /*
1040 * If an interface attribute was specified,
1041 * consider only children which attach to
1042 * that attribute.
1043 */
1044 if (ifattr && !STREQ(ifattr, cfdata_ifattr(cf)))
1045 continue;
1046
1047 if (cfparent_match(parent, cf->cf_pspec))
1048 mapply(&m, cf);
1049 }
1050 }
1051 return m.match;
1052 }
1053
1054 cfdata_t
1055 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
1056 void *aux)
1057 {
1058
1059 return config_search_loc(fn, parent, ifattr, NULL, aux);
1060 }
1061
1062 /*
1063 * Find the given root device.
1064 * This is much like config_search, but there is no parent.
1065 * Don't bother with multiple cfdata tables; the root node
1066 * must always be in the initial table.
1067 */
1068 cfdata_t
1069 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
1070 {
1071 cfdata_t cf;
1072 const short *p;
1073 struct matchinfo m;
1074
1075 m.fn = fn;
1076 m.parent = ROOT;
1077 m.aux = aux;
1078 m.match = NULL;
1079 m.pri = 0;
1080 m.locs = 0;
1081 /*
1082 * Look at root entries for matching name. We do not bother
1083 * with found-state here since only one root should ever be
1084 * searched (and it must be done first).
1085 */
1086 for (p = cfroots; *p >= 0; p++) {
1087 cf = &cfdata[*p];
1088 if (strcmp(cf->cf_name, rootname) == 0)
1089 mapply(&m, cf);
1090 }
1091 return m.match;
1092 }
1093
1094 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
1095
1096 /*
1097 * The given `aux' argument describes a device that has been found
1098 * on the given parent, but not necessarily configured. Locate the
1099 * configuration data for that device (using the submatch function
1100 * provided, or using candidates' cd_match configuration driver
1101 * functions) and attach it, and return its device_t. If the device was
1102 * not configured, call the given `print' function and return NULL.
1103 */
1104 device_t
1105 config_found_sm_loc(device_t parent,
1106 const char *ifattr, const int *locs, void *aux,
1107 cfprint_t print, cfsubmatch_t submatch)
1108 {
1109 cfdata_t cf;
1110
1111 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
1112 return(config_attach_loc(parent, cf, locs, aux, print));
1113 if (print) {
1114 if (config_do_twiddle && cold)
1115 twiddle();
1116 aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]);
1117 }
1118
1119 /*
1120 * This has the effect of mixing in a single timestamp to the
1121 * entropy pool. Experiments indicate the estimator will almost
1122 * always attribute one bit of entropy to this sample; analysis
1123 * of device attach/detach timestamps on FreeBSD indicates 4
1124 * bits of entropy/sample so this seems appropriately conservative.
1125 */
1126 rnd_add_uint32(&rnd_autoconf_source, 0);
1127 return NULL;
1128 }
1129
1130 device_t
1131 config_found_ia(device_t parent, const char *ifattr, void *aux,
1132 cfprint_t print)
1133 {
1134
1135 return config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL);
1136 }
1137
1138 device_t
1139 config_found(device_t parent, void *aux, cfprint_t print)
1140 {
1141
1142 return config_found_sm_loc(parent, NULL, NULL, aux, print, NULL);
1143 }
1144
1145 /*
1146 * As above, but for root devices.
1147 */
1148 device_t
1149 config_rootfound(const char *rootname, void *aux)
1150 {
1151 cfdata_t cf;
1152
1153 if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL)
1154 return config_attach(ROOT, cf, aux, NULL);
1155 aprint_error("root device %s not configured\n", rootname);
1156 return NULL;
1157 }
1158
1159 /* just like sprintf(buf, "%d") except that it works from the end */
1160 static char *
1161 number(char *ep, int n)
1162 {
1163
1164 *--ep = 0;
1165 while (n >= 10) {
1166 *--ep = (n % 10) + '0';
1167 n /= 10;
1168 }
1169 *--ep = n + '0';
1170 return ep;
1171 }
1172
1173 /*
1174 * Expand the size of the cd_devs array if necessary.
1175 *
1176 * The caller must hold alldevs_lock. config_makeroom() may release and
1177 * re-acquire alldevs_lock, so callers should re-check conditions such
1178 * as alldevs_nwrite == 0 and alldevs_nread == 0 when config_makeroom()
1179 * returns.
1180 */
1181 static void
1182 config_makeroom(int n, struct cfdriver *cd)
1183 {
1184 int ondevs, nndevs;
1185 device_t *osp, *nsp;
1186
1187 KASSERT(mutex_owned(&alldevs_lock));
1188 alldevs_nwrite++;
1189
1190 for (nndevs = MAX(4, cd->cd_ndevs); nndevs <= n; nndevs += nndevs)
1191 ;
1192
1193 while (n >= cd->cd_ndevs) {
1194 /*
1195 * Need to expand the array.
1196 */
1197 ondevs = cd->cd_ndevs;
1198 osp = cd->cd_devs;
1199
1200 /*
1201 * Release alldevs_lock around allocation, which may
1202 * sleep.
1203 */
1204 mutex_exit(&alldevs_lock);
1205 nsp = kmem_alloc(sizeof(device_t[nndevs]), KM_SLEEP);
1206 mutex_enter(&alldevs_lock);
1207
1208 /*
1209 * If another thread moved the array while we did
1210 * not hold alldevs_lock, try again.
1211 */
1212 if (cd->cd_devs != osp) {
1213 mutex_exit(&alldevs_lock);
1214 kmem_free(nsp, sizeof(device_t[nndevs]));
1215 mutex_enter(&alldevs_lock);
1216 continue;
1217 }
1218
1219 memset(nsp + ondevs, 0, sizeof(device_t[nndevs - ondevs]));
1220 if (ondevs != 0)
1221 memcpy(nsp, cd->cd_devs, sizeof(device_t[ondevs]));
1222
1223 cd->cd_ndevs = nndevs;
1224 cd->cd_devs = nsp;
1225 if (ondevs != 0) {
1226 mutex_exit(&alldevs_lock);
1227 kmem_free(osp, sizeof(device_t[ondevs]));
1228 mutex_enter(&alldevs_lock);
1229 }
1230 }
1231 KASSERT(mutex_owned(&alldevs_lock));
1232 alldevs_nwrite--;
1233 }
1234
1235 /*
1236 * Put dev into the devices list.
1237 */
1238 static void
1239 config_devlink(device_t dev)
1240 {
1241
1242 mutex_enter(&alldevs_lock);
1243
1244 KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev);
1245
1246 dev->dv_add_gen = alldevs_gen;
1247 /* It is safe to add a device to the tail of the list while
1248 * readers and writers are in the list.
1249 */
1250 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
1251 mutex_exit(&alldevs_lock);
1252 }
1253
1254 static void
1255 config_devfree(device_t dev)
1256 {
1257 int priv = (dev->dv_flags & DVF_PRIV_ALLOC);
1258
1259 if (dev->dv_cfattach->ca_devsize > 0)
1260 kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize);
1261 if (priv)
1262 kmem_free(dev, sizeof(*dev));
1263 }
1264
1265 /*
1266 * Caller must hold alldevs_lock.
1267 */
1268 static void
1269 config_devunlink(device_t dev, struct devicelist *garbage)
1270 {
1271 struct device_garbage *dg = &dev->dv_garbage;
1272 cfdriver_t cd = device_cfdriver(dev);
1273 int i;
1274
1275 KASSERT(mutex_owned(&alldevs_lock));
1276
1277 /* Unlink from device list. Link to garbage list. */
1278 TAILQ_REMOVE(&alldevs, dev, dv_list);
1279 TAILQ_INSERT_TAIL(garbage, dev, dv_list);
1280
1281 /* Remove from cfdriver's array. */
1282 cd->cd_devs[dev->dv_unit] = NULL;
1283
1284 /*
1285 * If the device now has no units in use, unlink its softc array.
1286 */
1287 for (i = 0; i < cd->cd_ndevs; i++) {
1288 if (cd->cd_devs[i] != NULL)
1289 break;
1290 }
1291 /* Nothing found. Unlink, now. Deallocate, later. */
1292 if (i == cd->cd_ndevs) {
1293 dg->dg_ndevs = cd->cd_ndevs;
1294 dg->dg_devs = cd->cd_devs;
1295 cd->cd_devs = NULL;
1296 cd->cd_ndevs = 0;
1297 }
1298 }
1299
1300 static void
1301 config_devdelete(device_t dev)
1302 {
1303 struct device_garbage *dg = &dev->dv_garbage;
1304 device_lock_t dvl = device_getlock(dev);
1305
1306 if (dg->dg_devs != NULL)
1307 kmem_free(dg->dg_devs, sizeof(device_t[dg->dg_ndevs]));
1308
1309 cv_destroy(&dvl->dvl_cv);
1310 mutex_destroy(&dvl->dvl_mtx);
1311
1312 KASSERT(dev->dv_properties != NULL);
1313 prop_object_release(dev->dv_properties);
1314
1315 if (dev->dv_activity_handlers)
1316 panic("%s with registered handlers", __func__);
1317
1318 if (dev->dv_locators) {
1319 size_t amount = *--dev->dv_locators;
1320 kmem_free(dev->dv_locators, amount);
1321 }
1322
1323 config_devfree(dev);
1324 }
1325
1326 static int
1327 config_unit_nextfree(cfdriver_t cd, cfdata_t cf)
1328 {
1329 int unit;
1330
1331 if (cf->cf_fstate == FSTATE_STAR) {
1332 for (unit = cf->cf_unit; unit < cd->cd_ndevs; unit++)
1333 if (cd->cd_devs[unit] == NULL)
1334 break;
1335 /*
1336 * unit is now the unit of the first NULL device pointer,
1337 * or max(cd->cd_ndevs,cf->cf_unit).
1338 */
1339 } else {
1340 unit = cf->cf_unit;
1341 if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL)
1342 unit = -1;
1343 }
1344 return unit;
1345 }
1346
1347 static int
1348 config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf)
1349 {
1350 struct alldevs_foray af;
1351 int unit;
1352
1353 config_alldevs_enter(&af);
1354 for (;;) {
1355 unit = config_unit_nextfree(cd, cf);
1356 if (unit == -1)
1357 break;
1358 if (unit < cd->cd_ndevs) {
1359 cd->cd_devs[unit] = dev;
1360 dev->dv_unit = unit;
1361 break;
1362 }
1363 config_makeroom(unit, cd);
1364 }
1365 config_alldevs_exit(&af);
1366
1367 return unit;
1368 }
1369
1370 static device_t
1371 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
1372 {
1373 cfdriver_t cd;
1374 cfattach_t ca;
1375 size_t lname, lunit;
1376 const char *xunit;
1377 int myunit;
1378 char num[10];
1379 device_t dev;
1380 void *dev_private;
1381 const struct cfiattrdata *ia;
1382 device_lock_t dvl;
1383
1384 cd = config_cfdriver_lookup(cf->cf_name);
1385 if (cd == NULL)
1386 return NULL;
1387
1388 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
1389 if (ca == NULL)
1390 return NULL;
1391
1392 /* get memory for all device vars */
1393 KASSERTMSG((ca->ca_flags & DVF_PRIV_ALLOC)
1394 || ca->ca_devsize >= sizeof(struct device),
1395 "%s: %s (%zu < %zu)", __func__, cf->cf_atname, ca->ca_devsize,
1396 sizeof(struct device));
1397 if (ca->ca_devsize > 0) {
1398 dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP);
1399 } else {
1400 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC);
1401 dev_private = NULL;
1402 }
1403
1404 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
1405 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1406 } else {
1407 dev = dev_private;
1408 #ifdef DIAGNOSTIC
1409 printf("%s has not been converted to device_t\n", cd->cd_name);
1410 #endif
1411 KASSERT(dev != NULL);
1412 }
1413 dev->dv_class = cd->cd_class;
1414 dev->dv_cfdata = cf;
1415 dev->dv_cfdriver = cd;
1416 dev->dv_cfattach = ca;
1417 dev->dv_activity_count = 0;
1418 dev->dv_activity_handlers = NULL;
1419 dev->dv_private = dev_private;
1420 dev->dv_flags = ca->ca_flags; /* inherit flags from class */
1421
1422 myunit = config_unit_alloc(dev, cd, cf);
1423 if (myunit == -1) {
1424 config_devfree(dev);
1425 return NULL;
1426 }
1427
1428 /* compute length of name and decimal expansion of unit number */
1429 lname = strlen(cd->cd_name);
1430 xunit = number(&num[sizeof(num)], myunit);
1431 lunit = &num[sizeof(num)] - xunit;
1432 if (lname + lunit > sizeof(dev->dv_xname))
1433 panic("config_devalloc: device name too long");
1434
1435 dvl = device_getlock(dev);
1436
1437 mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE);
1438 cv_init(&dvl->dvl_cv, "pmfsusp");
1439
1440 memcpy(dev->dv_xname, cd->cd_name, lname);
1441 memcpy(dev->dv_xname + lname, xunit, lunit);
1442 dev->dv_parent = parent;
1443 if (parent != NULL)
1444 dev->dv_depth = parent->dv_depth + 1;
1445 else
1446 dev->dv_depth = 0;
1447 dev->dv_flags |= DVF_ACTIVE; /* always initially active */
1448 if (locs) {
1449 KASSERT(parent); /* no locators at root */
1450 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
1451 dev->dv_locators =
1452 kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP);
1453 *dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]);
1454 memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen]));
1455 }
1456 dev->dv_properties = prop_dictionary_create();
1457 KASSERT(dev->dv_properties != NULL);
1458
1459 prop_dictionary_set_cstring_nocopy(dev->dv_properties,
1460 "device-driver", dev->dv_cfdriver->cd_name);
1461 prop_dictionary_set_uint16(dev->dv_properties,
1462 "device-unit", dev->dv_unit);
1463 if (parent != NULL) {
1464 prop_dictionary_set_cstring(dev->dv_properties,
1465 "device-parent", device_xname(parent));
1466 }
1467
1468 if (dev->dv_cfdriver->cd_attrs != NULL)
1469 config_add_attrib_dict(dev);
1470
1471 return dev;
1472 }
1473
1474 /*
1475 * Create an array of device attach attributes and add it
1476 * to the device's dv_properties dictionary.
1477 *
1478 * <key>interface-attributes</key>
1479 * <array>
1480 * <dict>
1481 * <key>attribute-name</key>
1482 * <string>foo</string>
1483 * <key>locators</key>
1484 * <array>
1485 * <dict>
1486 * <key>loc-name</key>
1487 * <string>foo-loc1</string>
1488 * </dict>
1489 * <dict>
1490 * <key>loc-name</key>
1491 * <string>foo-loc2</string>
1492 * <key>default</key>
1493 * <string>foo-loc2-default</string>
1494 * </dict>
1495 * ...
1496 * </array>
1497 * </dict>
1498 * ...
1499 * </array>
1500 */
1501
1502 static void
1503 config_add_attrib_dict(device_t dev)
1504 {
1505 int i, j;
1506 const struct cfiattrdata *ci;
1507 prop_dictionary_t attr_dict, loc_dict;
1508 prop_array_t attr_array, loc_array;
1509
1510 if ((attr_array = prop_array_create()) == NULL)
1511 return;
1512
1513 for (i = 0; ; i++) {
1514 if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL)
1515 break;
1516 if ((attr_dict = prop_dictionary_create()) == NULL)
1517 break;
1518 prop_dictionary_set_cstring_nocopy(attr_dict, "attribute-name",
1519 ci->ci_name);
1520
1521 /* Create an array of the locator names and defaults */
1522
1523 if (ci->ci_loclen != 0 &&
1524 (loc_array = prop_array_create()) != NULL) {
1525 for (j = 0; j < ci->ci_loclen; j++) {
1526 loc_dict = prop_dictionary_create();
1527 if (loc_dict == NULL)
1528 continue;
1529 prop_dictionary_set_cstring_nocopy(loc_dict,
1530 "loc-name", ci->ci_locdesc[j].cld_name);
1531 if (ci->ci_locdesc[j].cld_defaultstr != NULL)
1532 prop_dictionary_set_cstring_nocopy(
1533 loc_dict, "default",
1534 ci->ci_locdesc[j].cld_defaultstr);
1535 prop_array_set(loc_array, j, loc_dict);
1536 prop_object_release(loc_dict);
1537 }
1538 prop_dictionary_set_and_rel(attr_dict, "locators",
1539 loc_array);
1540 }
1541 prop_array_add(attr_array, attr_dict);
1542 prop_object_release(attr_dict);
1543 }
1544 if (i == 0)
1545 prop_object_release(attr_array);
1546 else
1547 prop_dictionary_set_and_rel(dev->dv_properties,
1548 "interface-attributes", attr_array);
1549
1550 return;
1551 }
1552
1553 /*
1554 * Attach a found device.
1555 */
1556 device_t
1557 config_attach_loc(device_t parent, cfdata_t cf,
1558 const int *locs, void *aux, cfprint_t print)
1559 {
1560 device_t dev;
1561 struct cftable *ct;
1562 const char *drvname;
1563
1564 dev = config_devalloc(parent, cf, locs);
1565 if (!dev)
1566 panic("config_attach: allocation of device softc failed");
1567
1568 /* XXX redundant - see below? */
1569 if (cf->cf_fstate != FSTATE_STAR) {
1570 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1571 cf->cf_fstate = FSTATE_FOUND;
1572 }
1573
1574 config_devlink(dev);
1575
1576 if (config_do_twiddle && cold)
1577 twiddle();
1578 else
1579 aprint_naive("Found ");
1580 /*
1581 * We want the next two printfs for normal, verbose, and quiet,
1582 * but not silent (in which case, we're twiddling, instead).
1583 */
1584 if (parent == ROOT) {
1585 aprint_naive("%s (root)", device_xname(dev));
1586 aprint_normal("%s (root)", device_xname(dev));
1587 } else {
1588 aprint_naive("%s at %s", device_xname(dev),
1589 device_xname(parent));
1590 aprint_normal("%s at %s", device_xname(dev),
1591 device_xname(parent));
1592 if (print)
1593 (void) (*print)(aux, NULL);
1594 }
1595
1596 /*
1597 * Before attaching, clobber any unfound devices that are
1598 * otherwise identical.
1599 * XXX code above is redundant?
1600 */
1601 drvname = dev->dv_cfdriver->cd_name;
1602 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1603 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1604 if (STREQ(cf->cf_name, drvname) &&
1605 cf->cf_unit == dev->dv_unit) {
1606 if (cf->cf_fstate == FSTATE_NOTFOUND)
1607 cf->cf_fstate = FSTATE_FOUND;
1608 }
1609 }
1610 }
1611 device_register(dev, aux);
1612
1613 /* Let userland know */
1614 devmon_report_device(dev, true);
1615
1616 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1617
1618 if (((dev->dv_flags & DVF_ATTACH_INPROGRESS) == 0)
1619 && !device_pmf_is_registered(dev))
1620 aprint_debug_dev(dev,
1621 "WARNING: power management not supported\n");
1622
1623 config_process_deferred(&deferred_config_queue, dev);
1624
1625 device_register_post_config(dev, aux);
1626 return dev;
1627 }
1628
1629 device_t
1630 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1631 {
1632
1633 return config_attach_loc(parent, cf, NULL, aux, print);
1634 }
1635
1636 /*
1637 * As above, but for pseudo-devices. Pseudo-devices attached in this
1638 * way are silently inserted into the device tree, and their children
1639 * attached.
1640 *
1641 * Note that because pseudo-devices are attached silently, any information
1642 * the attach routine wishes to print should be prefixed with the device
1643 * name by the attach routine.
1644 */
1645 device_t
1646 config_attach_pseudo(cfdata_t cf)
1647 {
1648 device_t dev;
1649
1650 dev = config_devalloc(ROOT, cf, NULL);
1651 if (!dev)
1652 return NULL;
1653
1654 /* XXX mark busy in cfdata */
1655
1656 if (cf->cf_fstate != FSTATE_STAR) {
1657 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1658 cf->cf_fstate = FSTATE_FOUND;
1659 }
1660
1661 config_devlink(dev);
1662
1663 #if 0 /* XXXJRT not yet */
1664 device_register(dev, NULL); /* like a root node */
1665 #endif
1666
1667 /* Let userland know */
1668 devmon_report_device(dev, true);
1669
1670 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1671
1672 config_process_deferred(&deferred_config_queue, dev);
1673 return dev;
1674 }
1675
1676 /*
1677 * Caller must hold alldevs_lock.
1678 */
1679 static void
1680 config_collect_garbage(struct devicelist *garbage)
1681 {
1682 device_t dv;
1683
1684 KASSERT(!cpu_intr_p());
1685 KASSERT(!cpu_softintr_p());
1686 KASSERT(mutex_owned(&alldevs_lock));
1687
1688 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) {
1689 TAILQ_FOREACH(dv, &alldevs, dv_list) {
1690 if (dv->dv_del_gen != 0)
1691 break;
1692 }
1693 if (dv == NULL) {
1694 alldevs_garbage = false;
1695 break;
1696 }
1697 config_devunlink(dv, garbage);
1698 }
1699 KASSERT(mutex_owned(&alldevs_lock));
1700 }
1701
1702 static void
1703 config_dump_garbage(struct devicelist *garbage)
1704 {
1705 device_t dv;
1706
1707 while ((dv = TAILQ_FIRST(garbage)) != NULL) {
1708 TAILQ_REMOVE(garbage, dv, dv_list);
1709 config_devdelete(dv);
1710 }
1711 }
1712
1713 /*
1714 * Detach a device. Optionally forced (e.g. because of hardware
1715 * removal) and quiet. Returns zero if successful, non-zero
1716 * (an error code) otherwise.
1717 *
1718 * Note that this code wants to be run from a process context, so
1719 * that the detach can sleep to allow processes which have a device
1720 * open to run and unwind their stacks.
1721 */
1722 int
1723 config_detach(device_t dev, int flags)
1724 {
1725 struct alldevs_foray af;
1726 struct cftable *ct;
1727 cfdata_t cf;
1728 const struct cfattach *ca;
1729 struct cfdriver *cd;
1730 device_t d __diagused;
1731 int rv = 0;
1732
1733 cf = dev->dv_cfdata;
1734 KASSERTMSG((cf == NULL || cf->cf_fstate == FSTATE_FOUND ||
1735 cf->cf_fstate == FSTATE_STAR),
1736 "config_detach: %s: bad device fstate: %d",
1737 device_xname(dev), cf ? cf->cf_fstate : -1);
1738
1739 cd = dev->dv_cfdriver;
1740 KASSERT(cd != NULL);
1741
1742 ca = dev->dv_cfattach;
1743 KASSERT(ca != NULL);
1744
1745 mutex_enter(&alldevs_lock);
1746 if (dev->dv_del_gen != 0) {
1747 mutex_exit(&alldevs_lock);
1748 #ifdef DIAGNOSTIC
1749 printf("%s: %s is already detached\n", __func__,
1750 device_xname(dev));
1751 #endif /* DIAGNOSTIC */
1752 return ENOENT;
1753 }
1754 alldevs_nwrite++;
1755 mutex_exit(&alldevs_lock);
1756
1757 if (!detachall &&
1758 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN &&
1759 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) {
1760 rv = EOPNOTSUPP;
1761 } else if (ca->ca_detach != NULL) {
1762 rv = (*ca->ca_detach)(dev, flags);
1763 } else
1764 rv = EOPNOTSUPP;
1765
1766 /*
1767 * If it was not possible to detach the device, then we either
1768 * panic() (for the forced but failed case), or return an error.
1769 *
1770 * If it was possible to detach the device, ensure that the
1771 * device is deactivated.
1772 */
1773 if (rv == 0)
1774 dev->dv_flags &= ~DVF_ACTIVE;
1775 else if ((flags & DETACH_FORCE) == 0)
1776 goto out;
1777 else {
1778 panic("config_detach: forced detach of %s failed (%d)",
1779 device_xname(dev), rv);
1780 }
1781
1782 /*
1783 * The device has now been successfully detached.
1784 */
1785
1786 /* Let userland know */
1787 devmon_report_device(dev, false);
1788
1789 #ifdef DIAGNOSTIC
1790 /*
1791 * Sanity: If you're successfully detached, you should have no
1792 * children. (Note that because children must be attached
1793 * after parents, we only need to search the latter part of
1794 * the list.)
1795 */
1796 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1797 d = TAILQ_NEXT(d, dv_list)) {
1798 if (d->dv_parent == dev && d->dv_del_gen == 0) {
1799 printf("config_detach: detached device %s"
1800 " has children %s\n", device_xname(dev),
1801 device_xname(d));
1802 panic("config_detach");
1803 }
1804 }
1805 #endif
1806
1807 /* notify the parent that the child is gone */
1808 if (dev->dv_parent) {
1809 device_t p = dev->dv_parent;
1810 if (p->dv_cfattach->ca_childdetached)
1811 (*p->dv_cfattach->ca_childdetached)(p, dev);
1812 }
1813
1814 /*
1815 * Mark cfdata to show that the unit can be reused, if possible.
1816 */
1817 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1818 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1819 if (STREQ(cf->cf_name, cd->cd_name)) {
1820 if (cf->cf_fstate == FSTATE_FOUND &&
1821 cf->cf_unit == dev->dv_unit)
1822 cf->cf_fstate = FSTATE_NOTFOUND;
1823 }
1824 }
1825 }
1826
1827 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1828 aprint_normal_dev(dev, "detached\n");
1829
1830 out:
1831 config_alldevs_enter(&af);
1832 KASSERT(alldevs_nwrite != 0);
1833 --alldevs_nwrite;
1834 if (rv == 0 && dev->dv_del_gen == 0) {
1835 if (alldevs_nwrite == 0 && alldevs_nread == 0)
1836 config_devunlink(dev, &af.af_garbage);
1837 else {
1838 dev->dv_del_gen = alldevs_gen;
1839 alldevs_garbage = true;
1840 }
1841 }
1842 config_alldevs_exit(&af);
1843
1844 return rv;
1845 }
1846
1847 int
1848 config_detach_children(device_t parent, int flags)
1849 {
1850 device_t dv;
1851 deviter_t di;
1852 int error = 0;
1853
1854 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
1855 dv = deviter_next(&di)) {
1856 if (device_parent(dv) != parent)
1857 continue;
1858 if ((error = config_detach(dv, flags)) != 0)
1859 break;
1860 }
1861 deviter_release(&di);
1862 return error;
1863 }
1864
1865 device_t
1866 shutdown_first(struct shutdown_state *s)
1867 {
1868 if (!s->initialized) {
1869 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
1870 s->initialized = true;
1871 }
1872 return shutdown_next(s);
1873 }
1874
1875 device_t
1876 shutdown_next(struct shutdown_state *s)
1877 {
1878 device_t dv;
1879
1880 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
1881 ;
1882
1883 if (dv == NULL)
1884 s->initialized = false;
1885
1886 return dv;
1887 }
1888
1889 bool
1890 config_detach_all(int how)
1891 {
1892 static struct shutdown_state s;
1893 device_t curdev;
1894 bool progress = false;
1895 int flags;
1896
1897 if ((how & (RB_NOSYNC|RB_DUMP)) != 0)
1898 return false;
1899
1900 if ((how & RB_POWERDOWN) == RB_POWERDOWN)
1901 flags = DETACH_SHUTDOWN | DETACH_POWEROFF;
1902 else
1903 flags = DETACH_SHUTDOWN;
1904
1905 for (curdev = shutdown_first(&s); curdev != NULL;
1906 curdev = shutdown_next(&s)) {
1907 aprint_debug(" detaching %s, ", device_xname(curdev));
1908 if (config_detach(curdev, flags) == 0) {
1909 progress = true;
1910 aprint_debug("success.");
1911 } else
1912 aprint_debug("failed.");
1913 }
1914 return progress;
1915 }
1916
1917 static bool
1918 device_is_ancestor_of(device_t ancestor, device_t descendant)
1919 {
1920 device_t dv;
1921
1922 for (dv = descendant; dv != NULL; dv = device_parent(dv)) {
1923 if (device_parent(dv) == ancestor)
1924 return true;
1925 }
1926 return false;
1927 }
1928
1929 int
1930 config_deactivate(device_t dev)
1931 {
1932 deviter_t di;
1933 const struct cfattach *ca;
1934 device_t descendant;
1935 int s, rv = 0, oflags;
1936
1937 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST);
1938 descendant != NULL;
1939 descendant = deviter_next(&di)) {
1940 if (dev != descendant &&
1941 !device_is_ancestor_of(dev, descendant))
1942 continue;
1943
1944 if ((descendant->dv_flags & DVF_ACTIVE) == 0)
1945 continue;
1946
1947 ca = descendant->dv_cfattach;
1948 oflags = descendant->dv_flags;
1949
1950 descendant->dv_flags &= ~DVF_ACTIVE;
1951 if (ca->ca_activate == NULL)
1952 continue;
1953 s = splhigh();
1954 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE);
1955 splx(s);
1956 if (rv != 0)
1957 descendant->dv_flags = oflags;
1958 }
1959 deviter_release(&di);
1960 return rv;
1961 }
1962
1963 /*
1964 * Defer the configuration of the specified device until all
1965 * of its parent's devices have been attached.
1966 */
1967 void
1968 config_defer(device_t dev, void (*func)(device_t))
1969 {
1970 struct deferred_config *dc;
1971
1972 if (dev->dv_parent == NULL)
1973 panic("config_defer: can't defer config of a root device");
1974
1975 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1976
1977 config_pending_incr(dev);
1978
1979 mutex_enter(&config_misc_lock);
1980 #ifdef DIAGNOSTIC
1981 struct deferred_config *odc;
1982 TAILQ_FOREACH(odc, &deferred_config_queue, dc_queue) {
1983 if (odc->dc_dev == dev)
1984 panic("config_defer: deferred twice");
1985 }
1986 #endif
1987 dc->dc_dev = dev;
1988 dc->dc_func = func;
1989 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1990 mutex_exit(&config_misc_lock);
1991 }
1992
1993 /*
1994 * Defer some autoconfiguration for a device until after interrupts
1995 * are enabled.
1996 */
1997 void
1998 config_interrupts(device_t dev, void (*func)(device_t))
1999 {
2000 struct deferred_config *dc;
2001
2002 /*
2003 * If interrupts are enabled, callback now.
2004 */
2005 if (cold == 0) {
2006 (*func)(dev);
2007 return;
2008 }
2009
2010 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
2011
2012 config_pending_incr(dev);
2013
2014 mutex_enter(&config_misc_lock);
2015 #ifdef DIAGNOSTIC
2016 struct deferred_config *odc;
2017 TAILQ_FOREACH(odc, &interrupt_config_queue, dc_queue) {
2018 if (odc->dc_dev == dev)
2019 panic("config_interrupts: deferred twice");
2020 }
2021 #endif
2022 dc->dc_dev = dev;
2023 dc->dc_func = func;
2024 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
2025 dev->dv_flags |= DVF_ATTACH_INPROGRESS;
2026 mutex_exit(&config_misc_lock);
2027 }
2028
2029 /*
2030 * Defer some autoconfiguration for a device until after root file system
2031 * is mounted (to load firmware etc).
2032 */
2033 void
2034 config_mountroot(device_t dev, void (*func)(device_t))
2035 {
2036 struct deferred_config *dc;
2037
2038 /*
2039 * If root file system is mounted, callback now.
2040 */
2041 if (root_is_mounted) {
2042 (*func)(dev);
2043 return;
2044 }
2045
2046 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
2047
2048 mutex_enter(&config_misc_lock);
2049 #ifdef DIAGNOSTIC
2050 struct deferred_config *odc;
2051 TAILQ_FOREACH(odc, &mountroot_config_queue, dc_queue) {
2052 if (odc->dc_dev == dev)
2053 panic("%s: deferred twice", __func__);
2054 }
2055 #endif
2056
2057 dc->dc_dev = dev;
2058 dc->dc_func = func;
2059 TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue);
2060 mutex_exit(&config_misc_lock);
2061 }
2062
2063 /*
2064 * Process a deferred configuration queue.
2065 */
2066 static void
2067 config_process_deferred(struct deferred_config_head *queue, device_t parent)
2068 {
2069 struct deferred_config *dc;
2070
2071 mutex_enter(&config_misc_lock);
2072 dc = TAILQ_FIRST(queue);
2073 while (dc) {
2074 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
2075 TAILQ_REMOVE(queue, dc, dc_queue);
2076 mutex_exit(&config_misc_lock);
2077
2078 (*dc->dc_func)(dc->dc_dev);
2079 config_pending_decr(dc->dc_dev);
2080 kmem_free(dc, sizeof(*dc));
2081
2082 mutex_enter(&config_misc_lock);
2083 /* Restart, queue might have changed */
2084 dc = TAILQ_FIRST(queue);
2085 } else {
2086 dc = TAILQ_NEXT(dc, dc_queue);
2087 }
2088 }
2089 mutex_exit(&config_misc_lock);
2090 }
2091
2092 /*
2093 * Manipulate the config_pending semaphore.
2094 */
2095 void
2096 config_pending_incr(device_t dev)
2097 {
2098
2099 mutex_enter(&config_misc_lock);
2100 config_pending++;
2101 #ifdef DEBUG_AUTOCONF
2102 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending);
2103 #endif
2104 mutex_exit(&config_misc_lock);
2105 }
2106
2107 void
2108 config_pending_decr(device_t dev)
2109 {
2110
2111 KASSERT(0 < config_pending);
2112 mutex_enter(&config_misc_lock);
2113 config_pending--;
2114 #ifdef DEBUG_AUTOCONF
2115 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending);
2116 #endif
2117 if (config_pending == 0)
2118 cv_broadcast(&config_misc_cv);
2119 mutex_exit(&config_misc_lock);
2120 }
2121
2122 /*
2123 * Register a "finalization" routine. Finalization routines are
2124 * called iteratively once all real devices have been found during
2125 * autoconfiguration, for as long as any one finalizer has done
2126 * any work.
2127 */
2128 int
2129 config_finalize_register(device_t dev, int (*fn)(device_t))
2130 {
2131 struct finalize_hook *f;
2132
2133 /*
2134 * If finalization has already been done, invoke the
2135 * callback function now.
2136 */
2137 if (config_finalize_done) {
2138 while ((*fn)(dev) != 0)
2139 /* loop */ ;
2140 return 0;
2141 }
2142
2143 /* Ensure this isn't already on the list. */
2144 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
2145 if (f->f_func == fn && f->f_dev == dev)
2146 return EEXIST;
2147 }
2148
2149 f = kmem_alloc(sizeof(*f), KM_SLEEP);
2150 f->f_func = fn;
2151 f->f_dev = dev;
2152 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
2153
2154 return 0;
2155 }
2156
2157 void
2158 config_finalize(void)
2159 {
2160 struct finalize_hook *f;
2161 struct pdevinit *pdev;
2162 extern struct pdevinit pdevinit[];
2163 int errcnt, rv;
2164
2165 /*
2166 * Now that device driver threads have been created, wait for
2167 * them to finish any deferred autoconfiguration.
2168 */
2169 mutex_enter(&config_misc_lock);
2170 while (config_pending != 0)
2171 cv_wait(&config_misc_cv, &config_misc_lock);
2172 mutex_exit(&config_misc_lock);
2173
2174 KERNEL_LOCK(1, NULL);
2175
2176 /* Attach pseudo-devices. */
2177 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
2178 (*pdev->pdev_attach)(pdev->pdev_count);
2179
2180 /* Run the hooks until none of them does any work. */
2181 do {
2182 rv = 0;
2183 TAILQ_FOREACH(f, &config_finalize_list, f_list)
2184 rv |= (*f->f_func)(f->f_dev);
2185 } while (rv != 0);
2186
2187 config_finalize_done = 1;
2188
2189 /* Now free all the hooks. */
2190 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
2191 TAILQ_REMOVE(&config_finalize_list, f, f_list);
2192 kmem_free(f, sizeof(*f));
2193 }
2194
2195 KERNEL_UNLOCK_ONE(NULL);
2196
2197 errcnt = aprint_get_error_count();
2198 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
2199 (boothowto & AB_VERBOSE) == 0) {
2200 mutex_enter(&config_misc_lock);
2201 if (config_do_twiddle) {
2202 config_do_twiddle = 0;
2203 printf_nolog(" done.\n");
2204 }
2205 mutex_exit(&config_misc_lock);
2206 }
2207 if (errcnt != 0) {
2208 printf("WARNING: %d error%s while detecting hardware; "
2209 "check system log.\n", errcnt,
2210 errcnt == 1 ? "" : "s");
2211 }
2212 }
2213
2214 void
2215 config_twiddle_init(void)
2216 {
2217
2218 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
2219 config_do_twiddle = 1;
2220 }
2221 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
2222 }
2223
2224 void
2225 config_twiddle_fn(void *cookie)
2226 {
2227
2228 mutex_enter(&config_misc_lock);
2229 if (config_do_twiddle) {
2230 twiddle();
2231 callout_schedule(&config_twiddle_ch, mstohz(100));
2232 }
2233 mutex_exit(&config_misc_lock);
2234 }
2235
2236 static void
2237 config_alldevs_enter(struct alldevs_foray *af)
2238 {
2239 TAILQ_INIT(&af->af_garbage);
2240 mutex_enter(&alldevs_lock);
2241 config_collect_garbage(&af->af_garbage);
2242 }
2243
2244 static void
2245 config_alldevs_exit(struct alldevs_foray *af)
2246 {
2247 mutex_exit(&alldevs_lock);
2248 config_dump_garbage(&af->af_garbage);
2249 }
2250
2251 /*
2252 * device_lookup:
2253 *
2254 * Look up a device instance for a given driver.
2255 */
2256 device_t
2257 device_lookup(cfdriver_t cd, int unit)
2258 {
2259 device_t dv;
2260
2261 mutex_enter(&alldevs_lock);
2262 if (unit < 0 || unit >= cd->cd_ndevs)
2263 dv = NULL;
2264 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
2265 dv = NULL;
2266 mutex_exit(&alldevs_lock);
2267
2268 return dv;
2269 }
2270
2271 /*
2272 * device_lookup_private:
2273 *
2274 * Look up a softc instance for a given driver.
2275 */
2276 void *
2277 device_lookup_private(cfdriver_t cd, int unit)
2278 {
2279
2280 return device_private(device_lookup(cd, unit));
2281 }
2282
2283 /*
2284 * device_find_by_xname:
2285 *
2286 * Returns the device of the given name or NULL if it doesn't exist.
2287 */
2288 device_t
2289 device_find_by_xname(const char *name)
2290 {
2291 device_t dv;
2292 deviter_t di;
2293
2294 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
2295 if (strcmp(device_xname(dv), name) == 0)
2296 break;
2297 }
2298 deviter_release(&di);
2299
2300 return dv;
2301 }
2302
2303 /*
2304 * device_find_by_driver_unit:
2305 *
2306 * Returns the device of the given driver name and unit or
2307 * NULL if it doesn't exist.
2308 */
2309 device_t
2310 device_find_by_driver_unit(const char *name, int unit)
2311 {
2312 struct cfdriver *cd;
2313
2314 if ((cd = config_cfdriver_lookup(name)) == NULL)
2315 return NULL;
2316 return device_lookup(cd, unit);
2317 }
2318
2319 /*
2320 * device_compatible_match:
2321 *
2322 * Match a driver's "compatible" data against a device's
2323 * "compatible" strings. If a match is found, we return
2324 * a weighted match result, and optionally the matching
2325 * entry.
2326 */
2327 int
2328 device_compatible_match(const char **device_compats, int ndevice_compats,
2329 const struct device_compatible_entry *driver_compats,
2330 const struct device_compatible_entry **matching_entryp)
2331 {
2332 const struct device_compatible_entry *dce = NULL;
2333 int i, match_weight;
2334
2335 if (ndevice_compats == 0 || device_compats == NULL ||
2336 driver_compats == NULL)
2337 return 0;
2338
2339 /*
2340 * We take the first match because we start with the most-specific
2341 * device compatible string.
2342 */
2343 for (i = 0, match_weight = ndevice_compats - 1;
2344 i < ndevice_compats;
2345 i++, match_weight--) {
2346 for (dce = driver_compats; dce->compat != NULL; dce++) {
2347 if (strcmp(dce->compat, device_compats[i]) == 0) {
2348 KASSERT(match_weight >= 0);
2349 if (matching_entryp)
2350 *matching_entryp = dce;
2351 return 1 + match_weight;
2352 }
2353 }
2354 }
2355 return 0;
2356 }
2357
2358 /*
2359 * Power management related functions.
2360 */
2361
2362 bool
2363 device_pmf_is_registered(device_t dev)
2364 {
2365 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
2366 }
2367
2368 bool
2369 device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual)
2370 {
2371 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2372 return true;
2373 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2374 return false;
2375 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2376 dev->dv_driver_suspend != NULL &&
2377 !(*dev->dv_driver_suspend)(dev, qual))
2378 return false;
2379
2380 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
2381 return true;
2382 }
2383
2384 bool
2385 device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual)
2386 {
2387 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2388 return true;
2389 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2390 return false;
2391 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2392 dev->dv_driver_resume != NULL &&
2393 !(*dev->dv_driver_resume)(dev, qual))
2394 return false;
2395
2396 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
2397 return true;
2398 }
2399
2400 bool
2401 device_pmf_driver_shutdown(device_t dev, int how)
2402 {
2403
2404 if (*dev->dv_driver_shutdown != NULL &&
2405 !(*dev->dv_driver_shutdown)(dev, how))
2406 return false;
2407 return true;
2408 }
2409
2410 bool
2411 device_pmf_driver_register(device_t dev,
2412 bool (*suspend)(device_t, const pmf_qual_t *),
2413 bool (*resume)(device_t, const pmf_qual_t *),
2414 bool (*shutdown)(device_t, int))
2415 {
2416 dev->dv_driver_suspend = suspend;
2417 dev->dv_driver_resume = resume;
2418 dev->dv_driver_shutdown = shutdown;
2419 dev->dv_flags |= DVF_POWER_HANDLERS;
2420 return true;
2421 }
2422
2423 static const char *
2424 curlwp_name(void)
2425 {
2426 if (curlwp->l_name != NULL)
2427 return curlwp->l_name;
2428 else
2429 return curlwp->l_proc->p_comm;
2430 }
2431
2432 void
2433 device_pmf_driver_deregister(device_t dev)
2434 {
2435 device_lock_t dvl = device_getlock(dev);
2436
2437 dev->dv_driver_suspend = NULL;
2438 dev->dv_driver_resume = NULL;
2439
2440 mutex_enter(&dvl->dvl_mtx);
2441 dev->dv_flags &= ~DVF_POWER_HANDLERS;
2442 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
2443 /* Wake a thread that waits for the lock. That
2444 * thread will fail to acquire the lock, and then
2445 * it will wake the next thread that waits for the
2446 * lock, or else it will wake us.
2447 */
2448 cv_signal(&dvl->dvl_cv);
2449 pmflock_debug(dev, __func__, __LINE__);
2450 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2451 pmflock_debug(dev, __func__, __LINE__);
2452 }
2453 mutex_exit(&dvl->dvl_mtx);
2454 }
2455
2456 bool
2457 device_pmf_driver_child_register(device_t dev)
2458 {
2459 device_t parent = device_parent(dev);
2460
2461 if (parent == NULL || parent->dv_driver_child_register == NULL)
2462 return true;
2463 return (*parent->dv_driver_child_register)(dev);
2464 }
2465
2466 void
2467 device_pmf_driver_set_child_register(device_t dev,
2468 bool (*child_register)(device_t))
2469 {
2470 dev->dv_driver_child_register = child_register;
2471 }
2472
2473 static void
2474 pmflock_debug(device_t dev, const char *func, int line)
2475 {
2476 device_lock_t dvl = device_getlock(dev);
2477
2478 aprint_debug_dev(dev,
2479 "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n", func, line,
2480 curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait, dev->dv_flags);
2481 }
2482
2483 static bool
2484 device_pmf_lock1(device_t dev)
2485 {
2486 device_lock_t dvl = device_getlock(dev);
2487
2488 while (device_pmf_is_registered(dev) &&
2489 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
2490 dvl->dvl_nwait++;
2491 pmflock_debug(dev, __func__, __LINE__);
2492 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2493 pmflock_debug(dev, __func__, __LINE__);
2494 dvl->dvl_nwait--;
2495 }
2496 if (!device_pmf_is_registered(dev)) {
2497 pmflock_debug(dev, __func__, __LINE__);
2498 /* We could not acquire the lock, but some other thread may
2499 * wait for it, also. Wake that thread.
2500 */
2501 cv_signal(&dvl->dvl_cv);
2502 return false;
2503 }
2504 dvl->dvl_nlock++;
2505 dvl->dvl_holder = curlwp;
2506 pmflock_debug(dev, __func__, __LINE__);
2507 return true;
2508 }
2509
2510 bool
2511 device_pmf_lock(device_t dev)
2512 {
2513 bool rc;
2514 device_lock_t dvl = device_getlock(dev);
2515
2516 mutex_enter(&dvl->dvl_mtx);
2517 rc = device_pmf_lock1(dev);
2518 mutex_exit(&dvl->dvl_mtx);
2519
2520 return rc;
2521 }
2522
2523 void
2524 device_pmf_unlock(device_t dev)
2525 {
2526 device_lock_t dvl = device_getlock(dev);
2527
2528 KASSERT(dvl->dvl_nlock > 0);
2529 mutex_enter(&dvl->dvl_mtx);
2530 if (--dvl->dvl_nlock == 0)
2531 dvl->dvl_holder = NULL;
2532 cv_signal(&dvl->dvl_cv);
2533 pmflock_debug(dev, __func__, __LINE__);
2534 mutex_exit(&dvl->dvl_mtx);
2535 }
2536
2537 device_lock_t
2538 device_getlock(device_t dev)
2539 {
2540 return &dev->dv_lock;
2541 }
2542
2543 void *
2544 device_pmf_bus_private(device_t dev)
2545 {
2546 return dev->dv_bus_private;
2547 }
2548
2549 bool
2550 device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual)
2551 {
2552 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2553 return true;
2554 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
2555 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2556 return false;
2557 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2558 dev->dv_bus_suspend != NULL &&
2559 !(*dev->dv_bus_suspend)(dev, qual))
2560 return false;
2561
2562 dev->dv_flags |= DVF_BUS_SUSPENDED;
2563 return true;
2564 }
2565
2566 bool
2567 device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual)
2568 {
2569 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
2570 return true;
2571 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2572 dev->dv_bus_resume != NULL &&
2573 !(*dev->dv_bus_resume)(dev, qual))
2574 return false;
2575
2576 dev->dv_flags &= ~DVF_BUS_SUSPENDED;
2577 return true;
2578 }
2579
2580 bool
2581 device_pmf_bus_shutdown(device_t dev, int how)
2582 {
2583
2584 if (*dev->dv_bus_shutdown != NULL &&
2585 !(*dev->dv_bus_shutdown)(dev, how))
2586 return false;
2587 return true;
2588 }
2589
2590 void
2591 device_pmf_bus_register(device_t dev, void *priv,
2592 bool (*suspend)(device_t, const pmf_qual_t *),
2593 bool (*resume)(device_t, const pmf_qual_t *),
2594 bool (*shutdown)(device_t, int), void (*deregister)(device_t))
2595 {
2596 dev->dv_bus_private = priv;
2597 dev->dv_bus_resume = resume;
2598 dev->dv_bus_suspend = suspend;
2599 dev->dv_bus_shutdown = shutdown;
2600 dev->dv_bus_deregister = deregister;
2601 }
2602
2603 void
2604 device_pmf_bus_deregister(device_t dev)
2605 {
2606 if (dev->dv_bus_deregister == NULL)
2607 return;
2608 (*dev->dv_bus_deregister)(dev);
2609 dev->dv_bus_private = NULL;
2610 dev->dv_bus_suspend = NULL;
2611 dev->dv_bus_resume = NULL;
2612 dev->dv_bus_deregister = NULL;
2613 }
2614
2615 void *
2616 device_pmf_class_private(device_t dev)
2617 {
2618 return dev->dv_class_private;
2619 }
2620
2621 bool
2622 device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual)
2623 {
2624 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
2625 return true;
2626 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2627 dev->dv_class_suspend != NULL &&
2628 !(*dev->dv_class_suspend)(dev, qual))
2629 return false;
2630
2631 dev->dv_flags |= DVF_CLASS_SUSPENDED;
2632 return true;
2633 }
2634
2635 bool
2636 device_pmf_class_resume(device_t dev, const pmf_qual_t *qual)
2637 {
2638 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2639 return true;
2640 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
2641 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2642 return false;
2643 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2644 dev->dv_class_resume != NULL &&
2645 !(*dev->dv_class_resume)(dev, qual))
2646 return false;
2647
2648 dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
2649 return true;
2650 }
2651
2652 void
2653 device_pmf_class_register(device_t dev, void *priv,
2654 bool (*suspend)(device_t, const pmf_qual_t *),
2655 bool (*resume)(device_t, const pmf_qual_t *),
2656 void (*deregister)(device_t))
2657 {
2658 dev->dv_class_private = priv;
2659 dev->dv_class_suspend = suspend;
2660 dev->dv_class_resume = resume;
2661 dev->dv_class_deregister = deregister;
2662 }
2663
2664 void
2665 device_pmf_class_deregister(device_t dev)
2666 {
2667 if (dev->dv_class_deregister == NULL)
2668 return;
2669 (*dev->dv_class_deregister)(dev);
2670 dev->dv_class_private = NULL;
2671 dev->dv_class_suspend = NULL;
2672 dev->dv_class_resume = NULL;
2673 dev->dv_class_deregister = NULL;
2674 }
2675
2676 bool
2677 device_active(device_t dev, devactive_t type)
2678 {
2679 size_t i;
2680
2681 if (dev->dv_activity_count == 0)
2682 return false;
2683
2684 for (i = 0; i < dev->dv_activity_count; ++i) {
2685 if (dev->dv_activity_handlers[i] == NULL)
2686 break;
2687 (*dev->dv_activity_handlers[i])(dev, type);
2688 }
2689
2690 return true;
2691 }
2692
2693 bool
2694 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
2695 {
2696 void (**new_handlers)(device_t, devactive_t);
2697 void (**old_handlers)(device_t, devactive_t);
2698 size_t i, old_size, new_size;
2699 int s;
2700
2701 old_handlers = dev->dv_activity_handlers;
2702 old_size = dev->dv_activity_count;
2703
2704 KASSERT(old_size == 0 || old_handlers != NULL);
2705
2706 for (i = 0; i < old_size; ++i) {
2707 KASSERT(old_handlers[i] != handler);
2708 if (old_handlers[i] == NULL) {
2709 old_handlers[i] = handler;
2710 return true;
2711 }
2712 }
2713
2714 new_size = old_size + 4;
2715 new_handlers = kmem_alloc(sizeof(void *[new_size]), KM_SLEEP);
2716
2717 for (i = 0; i < old_size; ++i)
2718 new_handlers[i] = old_handlers[i];
2719 new_handlers[old_size] = handler;
2720 for (i = old_size+1; i < new_size; ++i)
2721 new_handlers[i] = NULL;
2722
2723 s = splhigh();
2724 dev->dv_activity_count = new_size;
2725 dev->dv_activity_handlers = new_handlers;
2726 splx(s);
2727
2728 if (old_size > 0)
2729 kmem_free(old_handlers, sizeof(void * [old_size]));
2730
2731 return true;
2732 }
2733
2734 void
2735 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
2736 {
2737 void (**old_handlers)(device_t, devactive_t);
2738 size_t i, old_size;
2739 int s;
2740
2741 old_handlers = dev->dv_activity_handlers;
2742 old_size = dev->dv_activity_count;
2743
2744 for (i = 0; i < old_size; ++i) {
2745 if (old_handlers[i] == handler)
2746 break;
2747 if (old_handlers[i] == NULL)
2748 return; /* XXX panic? */
2749 }
2750
2751 if (i == old_size)
2752 return; /* XXX panic? */
2753
2754 for (; i < old_size - 1; ++i) {
2755 if ((old_handlers[i] = old_handlers[i + 1]) != NULL)
2756 continue;
2757
2758 if (i == 0) {
2759 s = splhigh();
2760 dev->dv_activity_count = 0;
2761 dev->dv_activity_handlers = NULL;
2762 splx(s);
2763 kmem_free(old_handlers, sizeof(void *[old_size]));
2764 }
2765 return;
2766 }
2767 old_handlers[i] = NULL;
2768 }
2769
2770 /* Return true iff the device_t `dev' exists at generation `gen'. */
2771 static bool
2772 device_exists_at(device_t dv, devgen_t gen)
2773 {
2774 return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) &&
2775 dv->dv_add_gen <= gen;
2776 }
2777
2778 static bool
2779 deviter_visits(const deviter_t *di, device_t dv)
2780 {
2781 return device_exists_at(dv, di->di_gen);
2782 }
2783
2784 /*
2785 * Device Iteration
2786 *
2787 * deviter_t: a device iterator. Holds state for a "walk" visiting
2788 * each device_t's in the device tree.
2789 *
2790 * deviter_init(di, flags): initialize the device iterator `di'
2791 * to "walk" the device tree. deviter_next(di) will return
2792 * the first device_t in the device tree, or NULL if there are
2793 * no devices.
2794 *
2795 * `flags' is one or more of DEVITER_F_RW, indicating that the
2796 * caller intends to modify the device tree by calling
2797 * config_detach(9) on devices in the order that the iterator
2798 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices
2799 * nearest the "root" of the device tree to be returned, first;
2800 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
2801 * the root of the device tree, first; and DEVITER_F_SHUTDOWN,
2802 * indicating both that deviter_init() should not respect any
2803 * locks on the device tree, and that deviter_next(di) may run
2804 * in more than one LWP before the walk has finished.
2805 *
2806 * Only one DEVITER_F_RW iterator may be in the device tree at
2807 * once.
2808 *
2809 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
2810 *
2811 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and
2812 * DEVITER_F_LEAVES_FIRST are used in combination.
2813 *
2814 * deviter_first(di, flags): initialize the device iterator `di'
2815 * and return the first device_t in the device tree, or NULL
2816 * if there are no devices. The statement
2817 *
2818 * dv = deviter_first(di);
2819 *
2820 * is shorthand for
2821 *
2822 * deviter_init(di);
2823 * dv = deviter_next(di);
2824 *
2825 * deviter_next(di): return the next device_t in the device tree,
2826 * or NULL if there are no more devices. deviter_next(di)
2827 * is undefined if `di' was not initialized with deviter_init() or
2828 * deviter_first().
2829 *
2830 * deviter_release(di): stops iteration (subsequent calls to
2831 * deviter_next() will return NULL), releases any locks and
2832 * resources held by the device iterator.
2833 *
2834 * Device iteration does not return device_t's in any particular
2835 * order. An iterator will never return the same device_t twice.
2836 * Device iteration is guaranteed to complete---i.e., if deviter_next(di)
2837 * is called repeatedly on the same `di', it will eventually return
2838 * NULL. It is ok to attach/detach devices during device iteration.
2839 */
2840 void
2841 deviter_init(deviter_t *di, deviter_flags_t flags)
2842 {
2843 device_t dv;
2844
2845 memset(di, 0, sizeof(*di));
2846
2847 if ((flags & DEVITER_F_SHUTDOWN) != 0)
2848 flags |= DEVITER_F_RW;
2849
2850 mutex_enter(&alldevs_lock);
2851 if ((flags & DEVITER_F_RW) != 0)
2852 alldevs_nwrite++;
2853 else
2854 alldevs_nread++;
2855 di->di_gen = alldevs_gen++;
2856 di->di_flags = flags;
2857
2858 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2859 case DEVITER_F_LEAVES_FIRST:
2860 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2861 if (!deviter_visits(di, dv))
2862 continue;
2863 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
2864 }
2865 break;
2866 case DEVITER_F_ROOT_FIRST:
2867 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2868 if (!deviter_visits(di, dv))
2869 continue;
2870 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
2871 }
2872 break;
2873 default:
2874 break;
2875 }
2876
2877 deviter_reinit(di);
2878 mutex_exit(&alldevs_lock);
2879 }
2880
2881 static void
2882 deviter_reinit(deviter_t *di)
2883 {
2884
2885 KASSERT(mutex_owned(&alldevs_lock));
2886 if ((di->di_flags & DEVITER_F_RW) != 0)
2887 di->di_prev = TAILQ_LAST(&alldevs, devicelist);
2888 else
2889 di->di_prev = TAILQ_FIRST(&alldevs);
2890 }
2891
2892 device_t
2893 deviter_first(deviter_t *di, deviter_flags_t flags)
2894 {
2895
2896 deviter_init(di, flags);
2897 return deviter_next(di);
2898 }
2899
2900 static device_t
2901 deviter_next2(deviter_t *di)
2902 {
2903 device_t dv;
2904
2905 KASSERT(mutex_owned(&alldevs_lock));
2906
2907 dv = di->di_prev;
2908
2909 if (dv == NULL)
2910 return NULL;
2911
2912 if ((di->di_flags & DEVITER_F_RW) != 0)
2913 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
2914 else
2915 di->di_prev = TAILQ_NEXT(dv, dv_list);
2916
2917 return dv;
2918 }
2919
2920 static device_t
2921 deviter_next1(deviter_t *di)
2922 {
2923 device_t dv;
2924
2925 KASSERT(mutex_owned(&alldevs_lock));
2926
2927 do {
2928 dv = deviter_next2(di);
2929 } while (dv != NULL && !deviter_visits(di, dv));
2930
2931 return dv;
2932 }
2933
2934 device_t
2935 deviter_next(deviter_t *di)
2936 {
2937 device_t dv = NULL;
2938
2939 mutex_enter(&alldevs_lock);
2940 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2941 case 0:
2942 dv = deviter_next1(di);
2943 break;
2944 case DEVITER_F_LEAVES_FIRST:
2945 while (di->di_curdepth >= 0) {
2946 if ((dv = deviter_next1(di)) == NULL) {
2947 di->di_curdepth--;
2948 deviter_reinit(di);
2949 } else if (dv->dv_depth == di->di_curdepth)
2950 break;
2951 }
2952 break;
2953 case DEVITER_F_ROOT_FIRST:
2954 while (di->di_curdepth <= di->di_maxdepth) {
2955 if ((dv = deviter_next1(di)) == NULL) {
2956 di->di_curdepth++;
2957 deviter_reinit(di);
2958 } else if (dv->dv_depth == di->di_curdepth)
2959 break;
2960 }
2961 break;
2962 default:
2963 break;
2964 }
2965 mutex_exit(&alldevs_lock);
2966
2967 return dv;
2968 }
2969
2970 void
2971 deviter_release(deviter_t *di)
2972 {
2973 bool rw = (di->di_flags & DEVITER_F_RW) != 0;
2974
2975 mutex_enter(&alldevs_lock);
2976 if (rw)
2977 --alldevs_nwrite;
2978 else
2979 --alldevs_nread;
2980 /* XXX wake a garbage-collection thread */
2981 mutex_exit(&alldevs_lock);
2982 }
2983
2984 const char *
2985 cfdata_ifattr(const struct cfdata *cf)
2986 {
2987 return cf->cf_pspec->cfp_iattr;
2988 }
2989
2990 bool
2991 ifattr_match(const char *snull, const char *t)
2992 {
2993 return (snull == NULL) || strcmp(snull, t) == 0;
2994 }
2995
2996 void
2997 null_childdetached(device_t self, device_t child)
2998 {
2999 /* do nothing */
3000 }
3001
3002 static void
3003 sysctl_detach_setup(struct sysctllog **clog)
3004 {
3005
3006 sysctl_createv(clog, 0, NULL, NULL,
3007 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
3008 CTLTYPE_BOOL, "detachall",
3009 SYSCTL_DESCR("Detach all devices at shutdown"),
3010 NULL, 0, &detachall, 0,
3011 CTL_KERN, CTL_CREATE, CTL_EOL);
3012 }
3013