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