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