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