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