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