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