subr_autoconf.c revision 1.223.2.3 1 /* $NetBSD: subr_autoconf.c,v 1.223.2.3 2013/02/25 00:29:52 tls 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.223.2.3 2013/02/25 00:29:52 tls 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 = 0, error = 0, e2;
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;
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 kmem_free(dc, sizeof(*dc));
440 config_pending_decr();
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 dev->dv_maxphys = parent->dv_maxphys;
1378 } else {
1379 dev->dv_depth = 0;
1380 dev->dv_maxphys = MACHINE_MAXPHYS;
1381 }
1382 aprint_debug_dev(dev, "dv_maxphys = %d", dev->dv_maxphys);
1383 dev->dv_flags |= DVF_ACTIVE; /* always initially active */
1384 if (locs) {
1385 KASSERT(parent); /* no locators at root */
1386 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
1387 dev->dv_locators =
1388 kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP);
1389 *dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]);
1390 memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen]));
1391 }
1392 dev->dv_properties = prop_dictionary_create();
1393 KASSERT(dev->dv_properties != NULL);
1394
1395 prop_dictionary_set_cstring_nocopy(dev->dv_properties,
1396 "device-driver", dev->dv_cfdriver->cd_name);
1397 prop_dictionary_set_uint16(dev->dv_properties,
1398 "device-unit", dev->dv_unit);
1399
1400 if (dev->dv_cfdriver->cd_attrs != NULL)
1401 config_add_attrib_dict(dev);
1402
1403 return dev;
1404 }
1405
1406 /*
1407 * Create an array of device attach attributes and add it
1408 * to the device's dv_properties dictionary.
1409 *
1410 * <key>interface-attributes</key>
1411 * <array>
1412 * <dict>
1413 * <key>attribute-name</key>
1414 * <string>foo</string>
1415 * <key>locators</key>
1416 * <array>
1417 * <dict>
1418 * <key>loc-name</key>
1419 * <string>foo-loc1</string>
1420 * </dict>
1421 * <dict>
1422 * <key>loc-name</key>
1423 * <string>foo-loc2</string>
1424 * <key>default</key>
1425 * <string>foo-loc2-default</string>
1426 * </dict>
1427 * ...
1428 * </array>
1429 * </dict>
1430 * ...
1431 * </array>
1432 */
1433
1434 static void
1435 config_add_attrib_dict(device_t dev)
1436 {
1437 int i, j;
1438 const struct cfiattrdata *ci;
1439 prop_dictionary_t attr_dict, loc_dict;
1440 prop_array_t attr_array, loc_array;
1441
1442 if ((attr_array = prop_array_create()) == NULL)
1443 return;
1444
1445 for (i = 0; ; i++) {
1446 if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL)
1447 break;
1448 if ((attr_dict = prop_dictionary_create()) == NULL)
1449 break;
1450 prop_dictionary_set_cstring_nocopy(attr_dict, "attribute-name",
1451 ci->ci_name);
1452
1453 /* Create an array of the locator names and defaults */
1454
1455 if (ci->ci_loclen != 0 &&
1456 (loc_array = prop_array_create()) != NULL) {
1457 for (j = 0; j < ci->ci_loclen; j++) {
1458 loc_dict = prop_dictionary_create();
1459 if (loc_dict == NULL)
1460 continue;
1461 prop_dictionary_set_cstring_nocopy(loc_dict,
1462 "loc-name", ci->ci_locdesc[j].cld_name);
1463 if (ci->ci_locdesc[j].cld_defaultstr != NULL)
1464 prop_dictionary_set_cstring_nocopy(
1465 loc_dict, "default",
1466 ci->ci_locdesc[j].cld_defaultstr);
1467 prop_array_set(loc_array, j, loc_dict);
1468 prop_object_release(loc_dict);
1469 }
1470 prop_dictionary_set_and_rel(attr_dict, "locators",
1471 loc_array);
1472 }
1473 prop_array_add(attr_array, attr_dict);
1474 prop_object_release(attr_dict);
1475 }
1476 if (i == 0)
1477 prop_object_release(attr_array);
1478 else
1479 prop_dictionary_set_and_rel(dev->dv_properties,
1480 "interface-attributes", attr_array);
1481
1482 return;
1483 }
1484
1485 /*
1486 * Attach a found device.
1487 */
1488 device_t
1489 config_attach_loc(device_t parent, cfdata_t cf,
1490 const int *locs, void *aux, cfprint_t print)
1491 {
1492 device_t dev;
1493 struct cftable *ct;
1494 const char *drvname;
1495
1496 dev = config_devalloc(parent, cf, locs);
1497 if (!dev)
1498 panic("config_attach: allocation of device softc failed");
1499
1500 /* XXX redundant - see below? */
1501 if (cf->cf_fstate != FSTATE_STAR) {
1502 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1503 cf->cf_fstate = FSTATE_FOUND;
1504 }
1505
1506 config_devlink(dev);
1507
1508 if (config_do_twiddle && cold)
1509 twiddle();
1510 else
1511 aprint_naive("Found ");
1512 /*
1513 * We want the next two printfs for normal, verbose, and quiet,
1514 * but not silent (in which case, we're twiddling, instead).
1515 */
1516 if (parent == ROOT) {
1517 aprint_naive("%s (root)", device_xname(dev));
1518 aprint_normal("%s (root)", device_xname(dev));
1519 } else {
1520 aprint_naive("%s at %s", device_xname(dev), device_xname(parent));
1521 aprint_normal("%s at %s", device_xname(dev), device_xname(parent));
1522 if (print)
1523 (void) (*print)(aux, NULL);
1524 }
1525
1526 /*
1527 * Before attaching, clobber any unfound devices that are
1528 * otherwise identical.
1529 * XXX code above is redundant?
1530 */
1531 drvname = dev->dv_cfdriver->cd_name;
1532 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1533 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1534 if (STREQ(cf->cf_name, drvname) &&
1535 cf->cf_unit == dev->dv_unit) {
1536 if (cf->cf_fstate == FSTATE_NOTFOUND)
1537 cf->cf_fstate = FSTATE_FOUND;
1538 }
1539 }
1540 }
1541 device_register(dev, aux);
1542
1543 /* Let userland know */
1544 devmon_report_device(dev, true);
1545
1546 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1547
1548 if (!device_pmf_is_registered(dev))
1549 aprint_debug_dev(dev, "WARNING: power management not supported\n");
1550
1551 config_process_deferred(&deferred_config_queue, dev);
1552
1553 device_register_post_config(dev, aux);
1554 return dev;
1555 }
1556
1557 device_t
1558 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1559 {
1560
1561 return config_attach_loc(parent, cf, NULL, aux, print);
1562 }
1563
1564 /*
1565 * As above, but for pseudo-devices. Pseudo-devices attached in this
1566 * way are silently inserted into the device tree, and their children
1567 * attached.
1568 *
1569 * Note that because pseudo-devices are attached silently, any information
1570 * the attach routine wishes to print should be prefixed with the device
1571 * name by the attach routine.
1572 */
1573 device_t
1574 config_attach_pseudo(cfdata_t cf)
1575 {
1576 device_t dev;
1577
1578 dev = config_devalloc(ROOT, cf, NULL);
1579 if (!dev)
1580 return NULL;
1581
1582 /* XXX mark busy in cfdata */
1583
1584 if (cf->cf_fstate != FSTATE_STAR) {
1585 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1586 cf->cf_fstate = FSTATE_FOUND;
1587 }
1588
1589 config_devlink(dev);
1590
1591 #if 0 /* XXXJRT not yet */
1592 device_register(dev, NULL); /* like a root node */
1593 #endif
1594
1595 /* Let userland know */
1596 devmon_report_device(dev, true);
1597
1598 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1599
1600 config_process_deferred(&deferred_config_queue, dev);
1601 return dev;
1602 }
1603
1604 /*
1605 * Caller must hold alldevs_mtx.
1606 */
1607 static void
1608 config_collect_garbage(struct devicelist *garbage)
1609 {
1610 device_t dv;
1611
1612 KASSERT(!cpu_intr_p());
1613 KASSERT(!cpu_softintr_p());
1614 KASSERT(mutex_owned(&alldevs_mtx));
1615
1616 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) {
1617 TAILQ_FOREACH(dv, &alldevs, dv_list) {
1618 if (dv->dv_del_gen != 0)
1619 break;
1620 }
1621 if (dv == NULL) {
1622 alldevs_garbage = false;
1623 break;
1624 }
1625 config_devunlink(dv, garbage);
1626 }
1627 KASSERT(mutex_owned(&alldevs_mtx));
1628 }
1629
1630 static void
1631 config_dump_garbage(struct devicelist *garbage)
1632 {
1633 device_t dv;
1634
1635 while ((dv = TAILQ_FIRST(garbage)) != NULL) {
1636 TAILQ_REMOVE(garbage, dv, dv_list);
1637 config_devdelete(dv);
1638 }
1639 }
1640
1641 /*
1642 * Detach a device. Optionally forced (e.g. because of hardware
1643 * removal) and quiet. Returns zero if successful, non-zero
1644 * (an error code) otherwise.
1645 *
1646 * Note that this code wants to be run from a process context, so
1647 * that the detach can sleep to allow processes which have a device
1648 * open to run and unwind their stacks.
1649 */
1650 int
1651 config_detach(device_t dev, int flags)
1652 {
1653 struct alldevs_foray af;
1654 struct cftable *ct;
1655 cfdata_t cf;
1656 const struct cfattach *ca;
1657 struct cfdriver *cd;
1658 #ifdef DIAGNOSTIC
1659 device_t d;
1660 #endif
1661 int rv = 0, s;
1662
1663 #ifdef DIAGNOSTIC
1664 cf = dev->dv_cfdata;
1665 if (cf != NULL && cf->cf_fstate != FSTATE_FOUND &&
1666 cf->cf_fstate != FSTATE_STAR)
1667 panic("config_detach: %s: bad device fstate %d",
1668 device_xname(dev), cf ? cf->cf_fstate : -1);
1669 #endif
1670 cd = dev->dv_cfdriver;
1671 KASSERT(cd != NULL);
1672
1673 ca = dev->dv_cfattach;
1674 KASSERT(ca != NULL);
1675
1676 s = config_alldevs_lock();
1677 if (dev->dv_del_gen != 0) {
1678 config_alldevs_unlock(s);
1679 #ifdef DIAGNOSTIC
1680 printf("%s: %s is already detached\n", __func__,
1681 device_xname(dev));
1682 #endif /* DIAGNOSTIC */
1683 return ENOENT;
1684 }
1685 alldevs_nwrite++;
1686 config_alldevs_unlock(s);
1687
1688 if (!detachall &&
1689 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN &&
1690 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) {
1691 rv = EOPNOTSUPP;
1692 } else if (ca->ca_detach != NULL) {
1693 rv = (*ca->ca_detach)(dev, flags);
1694 } else
1695 rv = EOPNOTSUPP;
1696
1697 /*
1698 * If it was not possible to detach the device, then we either
1699 * panic() (for the forced but failed case), or return an error.
1700 *
1701 * If it was possible to detach the device, ensure that the
1702 * device is deactivated.
1703 */
1704 if (rv == 0)
1705 dev->dv_flags &= ~DVF_ACTIVE;
1706 else if ((flags & DETACH_FORCE) == 0)
1707 goto out;
1708 else {
1709 panic("config_detach: forced detach of %s failed (%d)",
1710 device_xname(dev), rv);
1711 }
1712
1713 /*
1714 * The device has now been successfully detached.
1715 */
1716
1717 /* Let userland know */
1718 devmon_report_device(dev, false);
1719
1720 #ifdef DIAGNOSTIC
1721 /*
1722 * Sanity: If you're successfully detached, you should have no
1723 * children. (Note that because children must be attached
1724 * after parents, we only need to search the latter part of
1725 * the list.)
1726 */
1727 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1728 d = TAILQ_NEXT(d, dv_list)) {
1729 if (d->dv_parent == dev && d->dv_del_gen == 0) {
1730 printf("config_detach: detached device %s"
1731 " has children %s\n", device_xname(dev), device_xname(d));
1732 panic("config_detach");
1733 }
1734 }
1735 #endif
1736
1737 /* notify the parent that the child is gone */
1738 if (dev->dv_parent) {
1739 device_t p = dev->dv_parent;
1740 if (p->dv_cfattach->ca_childdetached)
1741 (*p->dv_cfattach->ca_childdetached)(p, dev);
1742 }
1743
1744 /*
1745 * Mark cfdata to show that the unit can be reused, if possible.
1746 */
1747 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1748 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1749 if (STREQ(cf->cf_name, cd->cd_name)) {
1750 if (cf->cf_fstate == FSTATE_FOUND &&
1751 cf->cf_unit == dev->dv_unit)
1752 cf->cf_fstate = FSTATE_NOTFOUND;
1753 }
1754 }
1755 }
1756
1757 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1758 aprint_normal_dev(dev, "detached\n");
1759
1760 out:
1761 config_alldevs_enter(&af);
1762 KASSERT(alldevs_nwrite != 0);
1763 --alldevs_nwrite;
1764 if (rv == 0 && dev->dv_del_gen == 0) {
1765 if (alldevs_nwrite == 0 && alldevs_nread == 0)
1766 config_devunlink(dev, &af.af_garbage);
1767 else {
1768 dev->dv_del_gen = alldevs_gen;
1769 alldevs_garbage = true;
1770 }
1771 }
1772 config_alldevs_exit(&af);
1773
1774 return rv;
1775 }
1776
1777 int
1778 config_detach_children(device_t parent, int flags)
1779 {
1780 device_t dv;
1781 deviter_t di;
1782 int error = 0;
1783
1784 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
1785 dv = deviter_next(&di)) {
1786 if (device_parent(dv) != parent)
1787 continue;
1788 if ((error = config_detach(dv, flags)) != 0)
1789 break;
1790 }
1791 deviter_release(&di);
1792 return error;
1793 }
1794
1795 device_t
1796 shutdown_first(struct shutdown_state *s)
1797 {
1798 if (!s->initialized) {
1799 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
1800 s->initialized = true;
1801 }
1802 return shutdown_next(s);
1803 }
1804
1805 device_t
1806 shutdown_next(struct shutdown_state *s)
1807 {
1808 device_t dv;
1809
1810 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
1811 ;
1812
1813 if (dv == NULL)
1814 s->initialized = false;
1815
1816 return dv;
1817 }
1818
1819 bool
1820 config_detach_all(int how)
1821 {
1822 static struct shutdown_state s;
1823 device_t curdev;
1824 bool progress = false;
1825
1826 if ((how & RB_NOSYNC) != 0)
1827 return false;
1828
1829 for (curdev = shutdown_first(&s); curdev != NULL;
1830 curdev = shutdown_next(&s)) {
1831 aprint_debug(" detaching %s, ", device_xname(curdev));
1832 if (config_detach(curdev, DETACH_SHUTDOWN) == 0) {
1833 progress = true;
1834 aprint_debug("success.");
1835 } else
1836 aprint_debug("failed.");
1837 }
1838 return progress;
1839 }
1840
1841 static bool
1842 device_is_ancestor_of(device_t ancestor, device_t descendant)
1843 {
1844 device_t dv;
1845
1846 for (dv = descendant; dv != NULL; dv = device_parent(dv)) {
1847 if (device_parent(dv) == ancestor)
1848 return true;
1849 }
1850 return false;
1851 }
1852
1853 int
1854 config_deactivate(device_t dev)
1855 {
1856 deviter_t di;
1857 const struct cfattach *ca;
1858 device_t descendant;
1859 int s, rv = 0, oflags;
1860
1861 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST);
1862 descendant != NULL;
1863 descendant = deviter_next(&di)) {
1864 if (dev != descendant &&
1865 !device_is_ancestor_of(dev, descendant))
1866 continue;
1867
1868 if ((descendant->dv_flags & DVF_ACTIVE) == 0)
1869 continue;
1870
1871 ca = descendant->dv_cfattach;
1872 oflags = descendant->dv_flags;
1873
1874 descendant->dv_flags &= ~DVF_ACTIVE;
1875 if (ca->ca_activate == NULL)
1876 continue;
1877 s = splhigh();
1878 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE);
1879 splx(s);
1880 if (rv != 0)
1881 descendant->dv_flags = oflags;
1882 }
1883 deviter_release(&di);
1884 return rv;
1885 }
1886
1887 /*
1888 * Defer the configuration of the specified device until all
1889 * of its parent's devices have been attached.
1890 */
1891 void
1892 config_defer(device_t dev, void (*func)(device_t))
1893 {
1894 struct deferred_config *dc;
1895
1896 if (dev->dv_parent == NULL)
1897 panic("config_defer: can't defer config of a root device");
1898
1899 #ifdef DIAGNOSTIC
1900 TAILQ_FOREACH(dc, &deferred_config_queue, dc_queue) {
1901 if (dc->dc_dev == dev)
1902 panic("config_defer: deferred twice");
1903 }
1904 #endif
1905
1906 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1907 if (dc == NULL)
1908 panic("config_defer: unable to allocate callback");
1909
1910 dc->dc_dev = dev;
1911 dc->dc_func = func;
1912 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1913 config_pending_incr();
1914 }
1915
1916 /*
1917 * Defer some autoconfiguration for a device until after interrupts
1918 * are enabled.
1919 */
1920 void
1921 config_interrupts(device_t dev, void (*func)(device_t))
1922 {
1923 struct deferred_config *dc;
1924
1925 /*
1926 * If interrupts are enabled, callback now.
1927 */
1928 if (cold == 0) {
1929 (*func)(dev);
1930 return;
1931 }
1932
1933 #ifdef DIAGNOSTIC
1934 TAILQ_FOREACH(dc, &interrupt_config_queue, dc_queue) {
1935 if (dc->dc_dev == dev)
1936 panic("config_interrupts: deferred twice");
1937 }
1938 #endif
1939
1940 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1941 if (dc == NULL)
1942 panic("config_interrupts: unable to allocate callback");
1943
1944 dc->dc_dev = dev;
1945 dc->dc_func = func;
1946 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1947 config_pending_incr();
1948 }
1949
1950 /*
1951 * Defer some autoconfiguration for a device until after root file system
1952 * is mounted (to load firmware etc).
1953 */
1954 void
1955 config_mountroot(device_t dev, void (*func)(device_t))
1956 {
1957 struct deferred_config *dc;
1958
1959 /*
1960 * If root file system is mounted, callback now.
1961 */
1962 if (root_is_mounted) {
1963 (*func)(dev);
1964 return;
1965 }
1966
1967 #ifdef DIAGNOSTIC
1968 TAILQ_FOREACH(dc, &mountroot_config_queue, dc_queue) {
1969 if (dc->dc_dev == dev)
1970 panic("%s: deferred twice", __func__);
1971 }
1972 #endif
1973
1974 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1975 if (dc == NULL)
1976 panic("%s: unable to allocate callback", __func__);
1977
1978 dc->dc_dev = dev;
1979 dc->dc_func = func;
1980 TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue);
1981 }
1982
1983 /*
1984 * Process a deferred configuration queue.
1985 */
1986 static void
1987 config_process_deferred(struct deferred_config_head *queue,
1988 device_t parent)
1989 {
1990 struct deferred_config *dc, *ndc;
1991
1992 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1993 ndc = TAILQ_NEXT(dc, dc_queue);
1994 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1995 TAILQ_REMOVE(queue, dc, dc_queue);
1996 (*dc->dc_func)(dc->dc_dev);
1997 kmem_free(dc, sizeof(*dc));
1998 config_pending_decr();
1999 }
2000 }
2001 }
2002
2003 /*
2004 * Manipulate the config_pending semaphore.
2005 */
2006 void
2007 config_pending_incr(void)
2008 {
2009
2010 mutex_enter(&config_misc_lock);
2011 config_pending++;
2012 mutex_exit(&config_misc_lock);
2013 }
2014
2015 void
2016 config_pending_decr(void)
2017 {
2018
2019 #ifdef DIAGNOSTIC
2020 if (config_pending == 0)
2021 panic("config_pending_decr: config_pending == 0");
2022 #endif
2023 mutex_enter(&config_misc_lock);
2024 config_pending--;
2025 if (config_pending == 0)
2026 cv_broadcast(&config_misc_cv);
2027 mutex_exit(&config_misc_lock);
2028 }
2029
2030 /*
2031 * Register a "finalization" routine. Finalization routines are
2032 * called iteratively once all real devices have been found during
2033 * autoconfiguration, for as long as any one finalizer has done
2034 * any work.
2035 */
2036 int
2037 config_finalize_register(device_t dev, int (*fn)(device_t))
2038 {
2039 struct finalize_hook *f;
2040
2041 /*
2042 * If finalization has already been done, invoke the
2043 * callback function now.
2044 */
2045 if (config_finalize_done) {
2046 while ((*fn)(dev) != 0)
2047 /* loop */ ;
2048 }
2049
2050 /* Ensure this isn't already on the list. */
2051 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
2052 if (f->f_func == fn && f->f_dev == dev)
2053 return EEXIST;
2054 }
2055
2056 f = kmem_alloc(sizeof(*f), KM_SLEEP);
2057 f->f_func = fn;
2058 f->f_dev = dev;
2059 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
2060
2061 return 0;
2062 }
2063
2064 void
2065 config_finalize(void)
2066 {
2067 struct finalize_hook *f;
2068 struct pdevinit *pdev;
2069 extern struct pdevinit pdevinit[];
2070 int errcnt, rv;
2071
2072 /*
2073 * Now that device driver threads have been created, wait for
2074 * them to finish any deferred autoconfiguration.
2075 */
2076 mutex_enter(&config_misc_lock);
2077 while (config_pending != 0)
2078 cv_wait(&config_misc_cv, &config_misc_lock);
2079 mutex_exit(&config_misc_lock);
2080
2081 KERNEL_LOCK(1, NULL);
2082
2083 /* Attach pseudo-devices. */
2084 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
2085 (*pdev->pdev_attach)(pdev->pdev_count);
2086
2087 /* Run the hooks until none of them does any work. */
2088 do {
2089 rv = 0;
2090 TAILQ_FOREACH(f, &config_finalize_list, f_list)
2091 rv |= (*f->f_func)(f->f_dev);
2092 } while (rv != 0);
2093
2094 config_finalize_done = 1;
2095
2096 /* Now free all the hooks. */
2097 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
2098 TAILQ_REMOVE(&config_finalize_list, f, f_list);
2099 kmem_free(f, sizeof(*f));
2100 }
2101
2102 KERNEL_UNLOCK_ONE(NULL);
2103
2104 errcnt = aprint_get_error_count();
2105 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
2106 (boothowto & AB_VERBOSE) == 0) {
2107 mutex_enter(&config_misc_lock);
2108 if (config_do_twiddle) {
2109 config_do_twiddle = 0;
2110 printf_nolog(" done.\n");
2111 }
2112 mutex_exit(&config_misc_lock);
2113 if (errcnt != 0) {
2114 printf("WARNING: %d error%s while detecting hardware; "
2115 "check system log.\n", errcnt,
2116 errcnt == 1 ? "" : "s");
2117 }
2118 }
2119 }
2120
2121 void
2122 config_twiddle_init(void)
2123 {
2124
2125 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
2126 config_do_twiddle = 1;
2127 }
2128 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
2129 }
2130
2131 void
2132 config_twiddle_fn(void *cookie)
2133 {
2134
2135 mutex_enter(&config_misc_lock);
2136 if (config_do_twiddle) {
2137 twiddle();
2138 callout_schedule(&config_twiddle_ch, mstohz(100));
2139 }
2140 mutex_exit(&config_misc_lock);
2141 }
2142
2143 static int
2144 config_alldevs_lock(void)
2145 {
2146 mutex_enter(&alldevs_mtx);
2147 return 0;
2148 }
2149
2150 static void
2151 config_alldevs_enter(struct alldevs_foray *af)
2152 {
2153 TAILQ_INIT(&af->af_garbage);
2154 af->af_s = config_alldevs_lock();
2155 config_collect_garbage(&af->af_garbage);
2156 }
2157
2158 static void
2159 config_alldevs_exit(struct alldevs_foray *af)
2160 {
2161 config_alldevs_unlock(af->af_s);
2162 config_dump_garbage(&af->af_garbage);
2163 }
2164
2165 /*ARGSUSED*/
2166 static void
2167 config_alldevs_unlock(int s)
2168 {
2169 mutex_exit(&alldevs_mtx);
2170 }
2171
2172 /*
2173 * device_lookup:
2174 *
2175 * Look up a device instance for a given driver.
2176 */
2177 device_t
2178 device_lookup(cfdriver_t cd, int unit)
2179 {
2180 device_t dv;
2181 int s;
2182
2183 s = config_alldevs_lock();
2184 KASSERT(mutex_owned(&alldevs_mtx));
2185 if (unit < 0 || unit >= cd->cd_ndevs)
2186 dv = NULL;
2187 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
2188 dv = NULL;
2189 config_alldevs_unlock(s);
2190
2191 return dv;
2192 }
2193
2194 /*
2195 * device_lookup_private:
2196 *
2197 * Look up a softc instance for a given driver.
2198 */
2199 void *
2200 device_lookup_private(cfdriver_t cd, int unit)
2201 {
2202
2203 return device_private(device_lookup(cd, unit));
2204 }
2205
2206 /*
2207 * device_find_by_xname:
2208 *
2209 * Returns the device of the given name or NULL if it doesn't exist.
2210 */
2211 device_t
2212 device_find_by_xname(const char *name)
2213 {
2214 device_t dv;
2215 deviter_t di;
2216
2217 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
2218 if (strcmp(device_xname(dv), name) == 0)
2219 break;
2220 }
2221 deviter_release(&di);
2222
2223 return dv;
2224 }
2225
2226 /*
2227 * device_find_by_driver_unit:
2228 *
2229 * Returns the device of the given driver name and unit or
2230 * NULL if it doesn't exist.
2231 */
2232 device_t
2233 device_find_by_driver_unit(const char *name, int unit)
2234 {
2235 struct cfdriver *cd;
2236
2237 if ((cd = config_cfdriver_lookup(name)) == NULL)
2238 return NULL;
2239 return device_lookup(cd, unit);
2240 }
2241
2242 /*
2243 * Power management related functions.
2244 */
2245
2246 bool
2247 device_pmf_is_registered(device_t dev)
2248 {
2249 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
2250 }
2251
2252 bool
2253 device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual)
2254 {
2255 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2256 return true;
2257 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2258 return false;
2259 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2260 dev->dv_driver_suspend != NULL &&
2261 !(*dev->dv_driver_suspend)(dev, qual))
2262 return false;
2263
2264 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
2265 return true;
2266 }
2267
2268 bool
2269 device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual)
2270 {
2271 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2272 return true;
2273 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2274 return false;
2275 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2276 dev->dv_driver_resume != NULL &&
2277 !(*dev->dv_driver_resume)(dev, qual))
2278 return false;
2279
2280 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
2281 return true;
2282 }
2283
2284 bool
2285 device_pmf_driver_shutdown(device_t dev, int how)
2286 {
2287
2288 if (*dev->dv_driver_shutdown != NULL &&
2289 !(*dev->dv_driver_shutdown)(dev, how))
2290 return false;
2291 return true;
2292 }
2293
2294 bool
2295 device_pmf_driver_register(device_t dev,
2296 bool (*suspend)(device_t, const pmf_qual_t *),
2297 bool (*resume)(device_t, const pmf_qual_t *),
2298 bool (*shutdown)(device_t, int))
2299 {
2300 dev->dv_driver_suspend = suspend;
2301 dev->dv_driver_resume = resume;
2302 dev->dv_driver_shutdown = shutdown;
2303 dev->dv_flags |= DVF_POWER_HANDLERS;
2304 return true;
2305 }
2306
2307 static const char *
2308 curlwp_name(void)
2309 {
2310 if (curlwp->l_name != NULL)
2311 return curlwp->l_name;
2312 else
2313 return curlwp->l_proc->p_comm;
2314 }
2315
2316 void
2317 device_pmf_driver_deregister(device_t dev)
2318 {
2319 device_lock_t dvl = device_getlock(dev);
2320
2321 dev->dv_driver_suspend = NULL;
2322 dev->dv_driver_resume = NULL;
2323
2324 mutex_enter(&dvl->dvl_mtx);
2325 dev->dv_flags &= ~DVF_POWER_HANDLERS;
2326 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
2327 /* Wake a thread that waits for the lock. That
2328 * thread will fail to acquire the lock, and then
2329 * it will wake the next thread that waits for the
2330 * lock, or else it will wake us.
2331 */
2332 cv_signal(&dvl->dvl_cv);
2333 pmflock_debug(dev, __func__, __LINE__);
2334 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2335 pmflock_debug(dev, __func__, __LINE__);
2336 }
2337 mutex_exit(&dvl->dvl_mtx);
2338 }
2339
2340 bool
2341 device_pmf_driver_child_register(device_t dev)
2342 {
2343 device_t parent = device_parent(dev);
2344
2345 if (parent == NULL || parent->dv_driver_child_register == NULL)
2346 return true;
2347 return (*parent->dv_driver_child_register)(dev);
2348 }
2349
2350 void
2351 device_pmf_driver_set_child_register(device_t dev,
2352 bool (*child_register)(device_t))
2353 {
2354 dev->dv_driver_child_register = child_register;
2355 }
2356
2357 static void
2358 pmflock_debug(device_t dev, const char *func, int line)
2359 {
2360 device_lock_t dvl = device_getlock(dev);
2361
2362 aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n",
2363 func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait,
2364 dev->dv_flags);
2365 }
2366
2367 static bool
2368 device_pmf_lock1(device_t dev)
2369 {
2370 device_lock_t dvl = device_getlock(dev);
2371
2372 while (device_pmf_is_registered(dev) &&
2373 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
2374 dvl->dvl_nwait++;
2375 pmflock_debug(dev, __func__, __LINE__);
2376 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2377 pmflock_debug(dev, __func__, __LINE__);
2378 dvl->dvl_nwait--;
2379 }
2380 if (!device_pmf_is_registered(dev)) {
2381 pmflock_debug(dev, __func__, __LINE__);
2382 /* We could not acquire the lock, but some other thread may
2383 * wait for it, also. Wake that thread.
2384 */
2385 cv_signal(&dvl->dvl_cv);
2386 return false;
2387 }
2388 dvl->dvl_nlock++;
2389 dvl->dvl_holder = curlwp;
2390 pmflock_debug(dev, __func__, __LINE__);
2391 return true;
2392 }
2393
2394 bool
2395 device_pmf_lock(device_t dev)
2396 {
2397 bool rc;
2398 device_lock_t dvl = device_getlock(dev);
2399
2400 mutex_enter(&dvl->dvl_mtx);
2401 rc = device_pmf_lock1(dev);
2402 mutex_exit(&dvl->dvl_mtx);
2403
2404 return rc;
2405 }
2406
2407 void
2408 device_pmf_unlock(device_t dev)
2409 {
2410 device_lock_t dvl = device_getlock(dev);
2411
2412 KASSERT(dvl->dvl_nlock > 0);
2413 mutex_enter(&dvl->dvl_mtx);
2414 if (--dvl->dvl_nlock == 0)
2415 dvl->dvl_holder = NULL;
2416 cv_signal(&dvl->dvl_cv);
2417 pmflock_debug(dev, __func__, __LINE__);
2418 mutex_exit(&dvl->dvl_mtx);
2419 }
2420
2421 device_lock_t
2422 device_getlock(device_t dev)
2423 {
2424 return &dev->dv_lock;
2425 }
2426
2427 void *
2428 device_pmf_bus_private(device_t dev)
2429 {
2430 return dev->dv_bus_private;
2431 }
2432
2433 bool
2434 device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual)
2435 {
2436 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2437 return true;
2438 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
2439 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2440 return false;
2441 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2442 dev->dv_bus_suspend != NULL &&
2443 !(*dev->dv_bus_suspend)(dev, qual))
2444 return false;
2445
2446 dev->dv_flags |= DVF_BUS_SUSPENDED;
2447 return true;
2448 }
2449
2450 bool
2451 device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual)
2452 {
2453 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
2454 return true;
2455 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2456 dev->dv_bus_resume != NULL &&
2457 !(*dev->dv_bus_resume)(dev, qual))
2458 return false;
2459
2460 dev->dv_flags &= ~DVF_BUS_SUSPENDED;
2461 return true;
2462 }
2463
2464 bool
2465 device_pmf_bus_shutdown(device_t dev, int how)
2466 {
2467
2468 if (*dev->dv_bus_shutdown != NULL &&
2469 !(*dev->dv_bus_shutdown)(dev, how))
2470 return false;
2471 return true;
2472 }
2473
2474 void
2475 device_pmf_bus_register(device_t dev, void *priv,
2476 bool (*suspend)(device_t, const pmf_qual_t *),
2477 bool (*resume)(device_t, const pmf_qual_t *),
2478 bool (*shutdown)(device_t, int), void (*deregister)(device_t))
2479 {
2480 dev->dv_bus_private = priv;
2481 dev->dv_bus_resume = resume;
2482 dev->dv_bus_suspend = suspend;
2483 dev->dv_bus_shutdown = shutdown;
2484 dev->dv_bus_deregister = deregister;
2485 }
2486
2487 void
2488 device_pmf_bus_deregister(device_t dev)
2489 {
2490 if (dev->dv_bus_deregister == NULL)
2491 return;
2492 (*dev->dv_bus_deregister)(dev);
2493 dev->dv_bus_private = NULL;
2494 dev->dv_bus_suspend = NULL;
2495 dev->dv_bus_resume = NULL;
2496 dev->dv_bus_deregister = NULL;
2497 }
2498
2499 void *
2500 device_pmf_class_private(device_t dev)
2501 {
2502 return dev->dv_class_private;
2503 }
2504
2505 bool
2506 device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual)
2507 {
2508 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
2509 return true;
2510 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2511 dev->dv_class_suspend != NULL &&
2512 !(*dev->dv_class_suspend)(dev, qual))
2513 return false;
2514
2515 dev->dv_flags |= DVF_CLASS_SUSPENDED;
2516 return true;
2517 }
2518
2519 bool
2520 device_pmf_class_resume(device_t dev, const pmf_qual_t *qual)
2521 {
2522 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2523 return true;
2524 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
2525 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2526 return false;
2527 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2528 dev->dv_class_resume != NULL &&
2529 !(*dev->dv_class_resume)(dev, qual))
2530 return false;
2531
2532 dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
2533 return true;
2534 }
2535
2536 void
2537 device_pmf_class_register(device_t dev, void *priv,
2538 bool (*suspend)(device_t, const pmf_qual_t *),
2539 bool (*resume)(device_t, const pmf_qual_t *),
2540 void (*deregister)(device_t))
2541 {
2542 dev->dv_class_private = priv;
2543 dev->dv_class_suspend = suspend;
2544 dev->dv_class_resume = resume;
2545 dev->dv_class_deregister = deregister;
2546 }
2547
2548 void
2549 device_pmf_class_deregister(device_t dev)
2550 {
2551 if (dev->dv_class_deregister == NULL)
2552 return;
2553 (*dev->dv_class_deregister)(dev);
2554 dev->dv_class_private = NULL;
2555 dev->dv_class_suspend = NULL;
2556 dev->dv_class_resume = NULL;
2557 dev->dv_class_deregister = NULL;
2558 }
2559
2560 bool
2561 device_active(device_t dev, devactive_t type)
2562 {
2563 size_t i;
2564
2565 if (dev->dv_activity_count == 0)
2566 return false;
2567
2568 for (i = 0; i < dev->dv_activity_count; ++i) {
2569 if (dev->dv_activity_handlers[i] == NULL)
2570 break;
2571 (*dev->dv_activity_handlers[i])(dev, type);
2572 }
2573
2574 return true;
2575 }
2576
2577 bool
2578 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
2579 {
2580 void (**new_handlers)(device_t, devactive_t);
2581 void (**old_handlers)(device_t, devactive_t);
2582 size_t i, old_size, new_size;
2583 int s;
2584
2585 old_handlers = dev->dv_activity_handlers;
2586 old_size = dev->dv_activity_count;
2587
2588 for (i = 0; i < old_size; ++i) {
2589 KASSERT(old_handlers[i] != handler);
2590 if (old_handlers[i] == NULL) {
2591 old_handlers[i] = handler;
2592 return true;
2593 }
2594 }
2595
2596 new_size = old_size + 4;
2597 new_handlers = kmem_alloc(sizeof(void *[new_size]), KM_SLEEP);
2598
2599 memcpy(new_handlers, old_handlers, sizeof(void *[old_size]));
2600 new_handlers[old_size] = handler;
2601 memset(new_handlers + old_size + 1, 0,
2602 sizeof(int [new_size - (old_size+1)]));
2603
2604 s = splhigh();
2605 dev->dv_activity_count = new_size;
2606 dev->dv_activity_handlers = new_handlers;
2607 splx(s);
2608
2609 if (old_handlers != NULL)
2610 kmem_free(old_handlers, sizeof(void * [old_size]));
2611
2612 return true;
2613 }
2614
2615 void
2616 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
2617 {
2618 void (**old_handlers)(device_t, devactive_t);
2619 size_t i, old_size;
2620 int s;
2621
2622 old_handlers = dev->dv_activity_handlers;
2623 old_size = dev->dv_activity_count;
2624
2625 for (i = 0; i < old_size; ++i) {
2626 if (old_handlers[i] == handler)
2627 break;
2628 if (old_handlers[i] == NULL)
2629 return; /* XXX panic? */
2630 }
2631
2632 if (i == old_size)
2633 return; /* XXX panic? */
2634
2635 for (; i < old_size - 1; ++i) {
2636 if ((old_handlers[i] = old_handlers[i + 1]) != NULL)
2637 continue;
2638
2639 if (i == 0) {
2640 s = splhigh();
2641 dev->dv_activity_count = 0;
2642 dev->dv_activity_handlers = NULL;
2643 splx(s);
2644 kmem_free(old_handlers, sizeof(void *[old_size]));
2645 }
2646 return;
2647 }
2648 old_handlers[i] = NULL;
2649 }
2650
2651 /* Return true iff the device_t `dev' exists at generation `gen'. */
2652 static bool
2653 device_exists_at(device_t dv, devgen_t gen)
2654 {
2655 return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) &&
2656 dv->dv_add_gen <= gen;
2657 }
2658
2659 static bool
2660 deviter_visits(const deviter_t *di, device_t dv)
2661 {
2662 return device_exists_at(dv, di->di_gen);
2663 }
2664
2665 /*
2666 * Device Iteration
2667 *
2668 * deviter_t: a device iterator. Holds state for a "walk" visiting
2669 * each device_t's in the device tree.
2670 *
2671 * deviter_init(di, flags): initialize the device iterator `di'
2672 * to "walk" the device tree. deviter_next(di) will return
2673 * the first device_t in the device tree, or NULL if there are
2674 * no devices.
2675 *
2676 * `flags' is one or more of DEVITER_F_RW, indicating that the
2677 * caller intends to modify the device tree by calling
2678 * config_detach(9) on devices in the order that the iterator
2679 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices
2680 * nearest the "root" of the device tree to be returned, first;
2681 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
2682 * the root of the device tree, first; and DEVITER_F_SHUTDOWN,
2683 * indicating both that deviter_init() should not respect any
2684 * locks on the device tree, and that deviter_next(di) may run
2685 * in more than one LWP before the walk has finished.
2686 *
2687 * Only one DEVITER_F_RW iterator may be in the device tree at
2688 * once.
2689 *
2690 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
2691 *
2692 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and
2693 * DEVITER_F_LEAVES_FIRST are used in combination.
2694 *
2695 * deviter_first(di, flags): initialize the device iterator `di'
2696 * and return the first device_t in the device tree, or NULL
2697 * if there are no devices. The statement
2698 *
2699 * dv = deviter_first(di);
2700 *
2701 * is shorthand for
2702 *
2703 * deviter_init(di);
2704 * dv = deviter_next(di);
2705 *
2706 * deviter_next(di): return the next device_t in the device tree,
2707 * or NULL if there are no more devices. deviter_next(di)
2708 * is undefined if `di' was not initialized with deviter_init() or
2709 * deviter_first().
2710 *
2711 * deviter_release(di): stops iteration (subsequent calls to
2712 * deviter_next() will return NULL), releases any locks and
2713 * resources held by the device iterator.
2714 *
2715 * Device iteration does not return device_t's in any particular
2716 * order. An iterator will never return the same device_t twice.
2717 * Device iteration is guaranteed to complete---i.e., if deviter_next(di)
2718 * is called repeatedly on the same `di', it will eventually return
2719 * NULL. It is ok to attach/detach devices during device iteration.
2720 */
2721 void
2722 deviter_init(deviter_t *di, deviter_flags_t flags)
2723 {
2724 device_t dv;
2725 int s;
2726
2727 memset(di, 0, sizeof(*di));
2728
2729 s = config_alldevs_lock();
2730 if ((flags & DEVITER_F_SHUTDOWN) != 0)
2731 flags |= DEVITER_F_RW;
2732
2733 if ((flags & DEVITER_F_RW) != 0)
2734 alldevs_nwrite++;
2735 else
2736 alldevs_nread++;
2737 di->di_gen = alldevs_gen++;
2738 config_alldevs_unlock(s);
2739
2740 di->di_flags = flags;
2741
2742 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2743 case DEVITER_F_LEAVES_FIRST:
2744 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2745 if (!deviter_visits(di, dv))
2746 continue;
2747 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
2748 }
2749 break;
2750 case DEVITER_F_ROOT_FIRST:
2751 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2752 if (!deviter_visits(di, dv))
2753 continue;
2754 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
2755 }
2756 break;
2757 default:
2758 break;
2759 }
2760
2761 deviter_reinit(di);
2762 }
2763
2764 static void
2765 deviter_reinit(deviter_t *di)
2766 {
2767 if ((di->di_flags & DEVITER_F_RW) != 0)
2768 di->di_prev = TAILQ_LAST(&alldevs, devicelist);
2769 else
2770 di->di_prev = TAILQ_FIRST(&alldevs);
2771 }
2772
2773 device_t
2774 deviter_first(deviter_t *di, deviter_flags_t flags)
2775 {
2776 deviter_init(di, flags);
2777 return deviter_next(di);
2778 }
2779
2780 static device_t
2781 deviter_next2(deviter_t *di)
2782 {
2783 device_t dv;
2784
2785 dv = di->di_prev;
2786
2787 if (dv == NULL)
2788 return NULL;
2789
2790 if ((di->di_flags & DEVITER_F_RW) != 0)
2791 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
2792 else
2793 di->di_prev = TAILQ_NEXT(dv, dv_list);
2794
2795 return dv;
2796 }
2797
2798 static device_t
2799 deviter_next1(deviter_t *di)
2800 {
2801 device_t dv;
2802
2803 do {
2804 dv = deviter_next2(di);
2805 } while (dv != NULL && !deviter_visits(di, dv));
2806
2807 return dv;
2808 }
2809
2810 device_t
2811 deviter_next(deviter_t *di)
2812 {
2813 device_t dv = NULL;
2814
2815 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2816 case 0:
2817 return deviter_next1(di);
2818 case DEVITER_F_LEAVES_FIRST:
2819 while (di->di_curdepth >= 0) {
2820 if ((dv = deviter_next1(di)) == NULL) {
2821 di->di_curdepth--;
2822 deviter_reinit(di);
2823 } else if (dv->dv_depth == di->di_curdepth)
2824 break;
2825 }
2826 return dv;
2827 case DEVITER_F_ROOT_FIRST:
2828 while (di->di_curdepth <= di->di_maxdepth) {
2829 if ((dv = deviter_next1(di)) == NULL) {
2830 di->di_curdepth++;
2831 deviter_reinit(di);
2832 } else if (dv->dv_depth == di->di_curdepth)
2833 break;
2834 }
2835 return dv;
2836 default:
2837 return NULL;
2838 }
2839 }
2840
2841 void
2842 deviter_release(deviter_t *di)
2843 {
2844 bool rw = (di->di_flags & DEVITER_F_RW) != 0;
2845 int s;
2846
2847 s = config_alldevs_lock();
2848 if (rw)
2849 --alldevs_nwrite;
2850 else
2851 --alldevs_nread;
2852 /* XXX wake a garbage-collection thread */
2853 config_alldevs_unlock(s);
2854 }
2855
2856 const char *
2857 cfdata_ifattr(const struct cfdata *cf)
2858 {
2859 return cf->cf_pspec->cfp_iattr;
2860 }
2861
2862 bool
2863 ifattr_match(const char *snull, const char *t)
2864 {
2865 return (snull == NULL) || strcmp(snull, t) == 0;
2866 }
2867
2868 void
2869 null_childdetached(device_t self, device_t child)
2870 {
2871 /* do nothing */
2872 }
2873
2874 static void
2875 sysctl_detach_setup(struct sysctllog **clog)
2876 {
2877 const struct sysctlnode *node = NULL;
2878
2879 sysctl_createv(clog, 0, NULL, &node,
2880 CTLFLAG_PERMANENT,
2881 CTLTYPE_NODE, "kern", NULL,
2882 NULL, 0, NULL, 0,
2883 CTL_KERN, CTL_EOL);
2884
2885 if (node == NULL)
2886 return;
2887
2888 sysctl_createv(clog, 0, &node, NULL,
2889 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2890 CTLTYPE_BOOL, "detachall",
2891 SYSCTL_DESCR("Detach all devices at shutdown"),
2892 NULL, 0, &detachall, 0,
2893 CTL_CREATE, CTL_EOL);
2894 }
2895