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