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