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