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