subr_autoconf.c revision 1.227 1 /* $NetBSD: subr_autoconf.c,v 1.227 2013/06/28 15:33:40 christos 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.227 2013/06/28 15:33:40 christos 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;
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 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();
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();
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 kmem_free(dc, sizeof(*dc));
1994 config_pending_decr();
1995 }
1996 }
1997 }
1998
1999 /*
2000 * Manipulate the config_pending semaphore.
2001 */
2002 void
2003 config_pending_incr(void)
2004 {
2005
2006 mutex_enter(&config_misc_lock);
2007 config_pending++;
2008 mutex_exit(&config_misc_lock);
2009 }
2010
2011 void
2012 config_pending_decr(void)
2013 {
2014
2015 #ifdef DIAGNOSTIC
2016 if (config_pending == 0)
2017 panic("config_pending_decr: config_pending == 0");
2018 #endif
2019 mutex_enter(&config_misc_lock);
2020 config_pending--;
2021 if (config_pending == 0)
2022 cv_broadcast(&config_misc_cv);
2023 mutex_exit(&config_misc_lock);
2024 }
2025
2026 /*
2027 * Register a "finalization" routine. Finalization routines are
2028 * called iteratively once all real devices have been found during
2029 * autoconfiguration, for as long as any one finalizer has done
2030 * any work.
2031 */
2032 int
2033 config_finalize_register(device_t dev, int (*fn)(device_t))
2034 {
2035 struct finalize_hook *f;
2036
2037 /*
2038 * If finalization has already been done, invoke the
2039 * callback function now.
2040 */
2041 if (config_finalize_done) {
2042 while ((*fn)(dev) != 0)
2043 /* loop */ ;
2044 }
2045
2046 /* Ensure this isn't already on the list. */
2047 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
2048 if (f->f_func == fn && f->f_dev == dev)
2049 return EEXIST;
2050 }
2051
2052 f = kmem_alloc(sizeof(*f), KM_SLEEP);
2053 f->f_func = fn;
2054 f->f_dev = dev;
2055 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
2056
2057 return 0;
2058 }
2059
2060 void
2061 config_finalize(void)
2062 {
2063 struct finalize_hook *f;
2064 struct pdevinit *pdev;
2065 extern struct pdevinit pdevinit[];
2066 int errcnt, rv;
2067
2068 /*
2069 * Now that device driver threads have been created, wait for
2070 * them to finish any deferred autoconfiguration.
2071 */
2072 mutex_enter(&config_misc_lock);
2073 while (config_pending != 0)
2074 cv_wait(&config_misc_cv, &config_misc_lock);
2075 mutex_exit(&config_misc_lock);
2076
2077 KERNEL_LOCK(1, NULL);
2078
2079 /* Attach pseudo-devices. */
2080 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
2081 (*pdev->pdev_attach)(pdev->pdev_count);
2082
2083 /* Run the hooks until none of them does any work. */
2084 do {
2085 rv = 0;
2086 TAILQ_FOREACH(f, &config_finalize_list, f_list)
2087 rv |= (*f->f_func)(f->f_dev);
2088 } while (rv != 0);
2089
2090 config_finalize_done = 1;
2091
2092 /* Now free all the hooks. */
2093 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
2094 TAILQ_REMOVE(&config_finalize_list, f, f_list);
2095 kmem_free(f, sizeof(*f));
2096 }
2097
2098 KERNEL_UNLOCK_ONE(NULL);
2099
2100 errcnt = aprint_get_error_count();
2101 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
2102 (boothowto & AB_VERBOSE) == 0) {
2103 mutex_enter(&config_misc_lock);
2104 if (config_do_twiddle) {
2105 config_do_twiddle = 0;
2106 printf_nolog(" done.\n");
2107 }
2108 mutex_exit(&config_misc_lock);
2109 if (errcnt != 0) {
2110 printf("WARNING: %d error%s while detecting hardware; "
2111 "check system log.\n", errcnt,
2112 errcnt == 1 ? "" : "s");
2113 }
2114 }
2115 }
2116
2117 void
2118 config_twiddle_init(void)
2119 {
2120
2121 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
2122 config_do_twiddle = 1;
2123 }
2124 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
2125 }
2126
2127 void
2128 config_twiddle_fn(void *cookie)
2129 {
2130
2131 mutex_enter(&config_misc_lock);
2132 if (config_do_twiddle) {
2133 twiddle();
2134 callout_schedule(&config_twiddle_ch, mstohz(100));
2135 }
2136 mutex_exit(&config_misc_lock);
2137 }
2138
2139 static int
2140 config_alldevs_lock(void)
2141 {
2142 mutex_enter(&alldevs_mtx);
2143 return 0;
2144 }
2145
2146 static void
2147 config_alldevs_enter(struct alldevs_foray *af)
2148 {
2149 TAILQ_INIT(&af->af_garbage);
2150 af->af_s = config_alldevs_lock();
2151 config_collect_garbage(&af->af_garbage);
2152 }
2153
2154 static void
2155 config_alldevs_exit(struct alldevs_foray *af)
2156 {
2157 config_alldevs_unlock(af->af_s);
2158 config_dump_garbage(&af->af_garbage);
2159 }
2160
2161 /*ARGSUSED*/
2162 static void
2163 config_alldevs_unlock(int s)
2164 {
2165 mutex_exit(&alldevs_mtx);
2166 }
2167
2168 /*
2169 * device_lookup:
2170 *
2171 * Look up a device instance for a given driver.
2172 */
2173 device_t
2174 device_lookup(cfdriver_t cd, int unit)
2175 {
2176 device_t dv;
2177 int s;
2178
2179 s = config_alldevs_lock();
2180 KASSERT(mutex_owned(&alldevs_mtx));
2181 if (unit < 0 || unit >= cd->cd_ndevs)
2182 dv = NULL;
2183 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
2184 dv = NULL;
2185 config_alldevs_unlock(s);
2186
2187 return dv;
2188 }
2189
2190 /*
2191 * device_lookup_private:
2192 *
2193 * Look up a softc instance for a given driver.
2194 */
2195 void *
2196 device_lookup_private(cfdriver_t cd, int unit)
2197 {
2198
2199 return device_private(device_lookup(cd, unit));
2200 }
2201
2202 /*
2203 * device_find_by_xname:
2204 *
2205 * Returns the device of the given name or NULL if it doesn't exist.
2206 */
2207 device_t
2208 device_find_by_xname(const char *name)
2209 {
2210 device_t dv;
2211 deviter_t di;
2212
2213 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
2214 if (strcmp(device_xname(dv), name) == 0)
2215 break;
2216 }
2217 deviter_release(&di);
2218
2219 return dv;
2220 }
2221
2222 /*
2223 * device_find_by_driver_unit:
2224 *
2225 * Returns the device of the given driver name and unit or
2226 * NULL if it doesn't exist.
2227 */
2228 device_t
2229 device_find_by_driver_unit(const char *name, int unit)
2230 {
2231 struct cfdriver *cd;
2232
2233 if ((cd = config_cfdriver_lookup(name)) == NULL)
2234 return NULL;
2235 return device_lookup(cd, unit);
2236 }
2237
2238 /*
2239 * Power management related functions.
2240 */
2241
2242 bool
2243 device_pmf_is_registered(device_t dev)
2244 {
2245 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
2246 }
2247
2248 bool
2249 device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual)
2250 {
2251 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2252 return true;
2253 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2254 return false;
2255 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2256 dev->dv_driver_suspend != NULL &&
2257 !(*dev->dv_driver_suspend)(dev, qual))
2258 return false;
2259
2260 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
2261 return true;
2262 }
2263
2264 bool
2265 device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual)
2266 {
2267 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2268 return true;
2269 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2270 return false;
2271 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2272 dev->dv_driver_resume != NULL &&
2273 !(*dev->dv_driver_resume)(dev, qual))
2274 return false;
2275
2276 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
2277 return true;
2278 }
2279
2280 bool
2281 device_pmf_driver_shutdown(device_t dev, int how)
2282 {
2283
2284 if (*dev->dv_driver_shutdown != NULL &&
2285 !(*dev->dv_driver_shutdown)(dev, how))
2286 return false;
2287 return true;
2288 }
2289
2290 bool
2291 device_pmf_driver_register(device_t dev,
2292 bool (*suspend)(device_t, const pmf_qual_t *),
2293 bool (*resume)(device_t, const pmf_qual_t *),
2294 bool (*shutdown)(device_t, int))
2295 {
2296 dev->dv_driver_suspend = suspend;
2297 dev->dv_driver_resume = resume;
2298 dev->dv_driver_shutdown = shutdown;
2299 dev->dv_flags |= DVF_POWER_HANDLERS;
2300 return true;
2301 }
2302
2303 static const char *
2304 curlwp_name(void)
2305 {
2306 if (curlwp->l_name != NULL)
2307 return curlwp->l_name;
2308 else
2309 return curlwp->l_proc->p_comm;
2310 }
2311
2312 void
2313 device_pmf_driver_deregister(device_t dev)
2314 {
2315 device_lock_t dvl = device_getlock(dev);
2316
2317 dev->dv_driver_suspend = NULL;
2318 dev->dv_driver_resume = NULL;
2319
2320 mutex_enter(&dvl->dvl_mtx);
2321 dev->dv_flags &= ~DVF_POWER_HANDLERS;
2322 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
2323 /* Wake a thread that waits for the lock. That
2324 * thread will fail to acquire the lock, and then
2325 * it will wake the next thread that waits for the
2326 * lock, or else it will wake us.
2327 */
2328 cv_signal(&dvl->dvl_cv);
2329 pmflock_debug(dev, __func__, __LINE__);
2330 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2331 pmflock_debug(dev, __func__, __LINE__);
2332 }
2333 mutex_exit(&dvl->dvl_mtx);
2334 }
2335
2336 bool
2337 device_pmf_driver_child_register(device_t dev)
2338 {
2339 device_t parent = device_parent(dev);
2340
2341 if (parent == NULL || parent->dv_driver_child_register == NULL)
2342 return true;
2343 return (*parent->dv_driver_child_register)(dev);
2344 }
2345
2346 void
2347 device_pmf_driver_set_child_register(device_t dev,
2348 bool (*child_register)(device_t))
2349 {
2350 dev->dv_driver_child_register = child_register;
2351 }
2352
2353 static void
2354 pmflock_debug(device_t dev, const char *func, int line)
2355 {
2356 device_lock_t dvl = device_getlock(dev);
2357
2358 aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n",
2359 func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait,
2360 dev->dv_flags);
2361 }
2362
2363 static bool
2364 device_pmf_lock1(device_t dev)
2365 {
2366 device_lock_t dvl = device_getlock(dev);
2367
2368 while (device_pmf_is_registered(dev) &&
2369 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
2370 dvl->dvl_nwait++;
2371 pmflock_debug(dev, __func__, __LINE__);
2372 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2373 pmflock_debug(dev, __func__, __LINE__);
2374 dvl->dvl_nwait--;
2375 }
2376 if (!device_pmf_is_registered(dev)) {
2377 pmflock_debug(dev, __func__, __LINE__);
2378 /* We could not acquire the lock, but some other thread may
2379 * wait for it, also. Wake that thread.
2380 */
2381 cv_signal(&dvl->dvl_cv);
2382 return false;
2383 }
2384 dvl->dvl_nlock++;
2385 dvl->dvl_holder = curlwp;
2386 pmflock_debug(dev, __func__, __LINE__);
2387 return true;
2388 }
2389
2390 bool
2391 device_pmf_lock(device_t dev)
2392 {
2393 bool rc;
2394 device_lock_t dvl = device_getlock(dev);
2395
2396 mutex_enter(&dvl->dvl_mtx);
2397 rc = device_pmf_lock1(dev);
2398 mutex_exit(&dvl->dvl_mtx);
2399
2400 return rc;
2401 }
2402
2403 void
2404 device_pmf_unlock(device_t dev)
2405 {
2406 device_lock_t dvl = device_getlock(dev);
2407
2408 KASSERT(dvl->dvl_nlock > 0);
2409 mutex_enter(&dvl->dvl_mtx);
2410 if (--dvl->dvl_nlock == 0)
2411 dvl->dvl_holder = NULL;
2412 cv_signal(&dvl->dvl_cv);
2413 pmflock_debug(dev, __func__, __LINE__);
2414 mutex_exit(&dvl->dvl_mtx);
2415 }
2416
2417 device_lock_t
2418 device_getlock(device_t dev)
2419 {
2420 return &dev->dv_lock;
2421 }
2422
2423 void *
2424 device_pmf_bus_private(device_t dev)
2425 {
2426 return dev->dv_bus_private;
2427 }
2428
2429 bool
2430 device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual)
2431 {
2432 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2433 return true;
2434 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
2435 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2436 return false;
2437 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2438 dev->dv_bus_suspend != NULL &&
2439 !(*dev->dv_bus_suspend)(dev, qual))
2440 return false;
2441
2442 dev->dv_flags |= DVF_BUS_SUSPENDED;
2443 return true;
2444 }
2445
2446 bool
2447 device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual)
2448 {
2449 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
2450 return true;
2451 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
2452 dev->dv_bus_resume != NULL &&
2453 !(*dev->dv_bus_resume)(dev, qual))
2454 return false;
2455
2456 dev->dv_flags &= ~DVF_BUS_SUSPENDED;
2457 return true;
2458 }
2459
2460 bool
2461 device_pmf_bus_shutdown(device_t dev, int how)
2462 {
2463
2464 if (*dev->dv_bus_shutdown != NULL &&
2465 !(*dev->dv_bus_shutdown)(dev, how))
2466 return false;
2467 return true;
2468 }
2469
2470 void
2471 device_pmf_bus_register(device_t dev, void *priv,
2472 bool (*suspend)(device_t, const pmf_qual_t *),
2473 bool (*resume)(device_t, const pmf_qual_t *),
2474 bool (*shutdown)(device_t, int), void (*deregister)(device_t))
2475 {
2476 dev->dv_bus_private = priv;
2477 dev->dv_bus_resume = resume;
2478 dev->dv_bus_suspend = suspend;
2479 dev->dv_bus_shutdown = shutdown;
2480 dev->dv_bus_deregister = deregister;
2481 }
2482
2483 void
2484 device_pmf_bus_deregister(device_t dev)
2485 {
2486 if (dev->dv_bus_deregister == NULL)
2487 return;
2488 (*dev->dv_bus_deregister)(dev);
2489 dev->dv_bus_private = NULL;
2490 dev->dv_bus_suspend = NULL;
2491 dev->dv_bus_resume = NULL;
2492 dev->dv_bus_deregister = NULL;
2493 }
2494
2495 void *
2496 device_pmf_class_private(device_t dev)
2497 {
2498 return dev->dv_class_private;
2499 }
2500
2501 bool
2502 device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual)
2503 {
2504 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
2505 return true;
2506 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2507 dev->dv_class_suspend != NULL &&
2508 !(*dev->dv_class_suspend)(dev, qual))
2509 return false;
2510
2511 dev->dv_flags |= DVF_CLASS_SUSPENDED;
2512 return true;
2513 }
2514
2515 bool
2516 device_pmf_class_resume(device_t dev, const pmf_qual_t *qual)
2517 {
2518 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2519 return true;
2520 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
2521 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2522 return false;
2523 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
2524 dev->dv_class_resume != NULL &&
2525 !(*dev->dv_class_resume)(dev, qual))
2526 return false;
2527
2528 dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
2529 return true;
2530 }
2531
2532 void
2533 device_pmf_class_register(device_t dev, void *priv,
2534 bool (*suspend)(device_t, const pmf_qual_t *),
2535 bool (*resume)(device_t, const pmf_qual_t *),
2536 void (*deregister)(device_t))
2537 {
2538 dev->dv_class_private = priv;
2539 dev->dv_class_suspend = suspend;
2540 dev->dv_class_resume = resume;
2541 dev->dv_class_deregister = deregister;
2542 }
2543
2544 void
2545 device_pmf_class_deregister(device_t dev)
2546 {
2547 if (dev->dv_class_deregister == NULL)
2548 return;
2549 (*dev->dv_class_deregister)(dev);
2550 dev->dv_class_private = NULL;
2551 dev->dv_class_suspend = NULL;
2552 dev->dv_class_resume = NULL;
2553 dev->dv_class_deregister = NULL;
2554 }
2555
2556 bool
2557 device_active(device_t dev, devactive_t type)
2558 {
2559 size_t i;
2560
2561 if (dev->dv_activity_count == 0)
2562 return false;
2563
2564 for (i = 0; i < dev->dv_activity_count; ++i) {
2565 if (dev->dv_activity_handlers[i] == NULL)
2566 break;
2567 (*dev->dv_activity_handlers[i])(dev, type);
2568 }
2569
2570 return true;
2571 }
2572
2573 bool
2574 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
2575 {
2576 void (**new_handlers)(device_t, devactive_t);
2577 void (**old_handlers)(device_t, devactive_t);
2578 size_t i, old_size, new_size;
2579 int s;
2580
2581 old_handlers = dev->dv_activity_handlers;
2582 old_size = dev->dv_activity_count;
2583
2584 for (i = 0; i < old_size; ++i) {
2585 KASSERT(old_handlers[i] != handler);
2586 if (old_handlers[i] == NULL) {
2587 old_handlers[i] = handler;
2588 return true;
2589 }
2590 }
2591
2592 new_size = old_size + 4;
2593 new_handlers = kmem_alloc(sizeof(void *[new_size]), KM_SLEEP);
2594
2595 memcpy(new_handlers, old_handlers, sizeof(void *[old_size]));
2596 new_handlers[old_size] = handler;
2597 memset(new_handlers + old_size + 1, 0,
2598 sizeof(int [new_size - (old_size+1)]));
2599
2600 s = splhigh();
2601 dev->dv_activity_count = new_size;
2602 dev->dv_activity_handlers = new_handlers;
2603 splx(s);
2604
2605 if (old_handlers != NULL)
2606 kmem_free(old_handlers, sizeof(void * [old_size]));
2607
2608 return true;
2609 }
2610
2611 void
2612 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
2613 {
2614 void (**old_handlers)(device_t, devactive_t);
2615 size_t i, old_size;
2616 int s;
2617
2618 old_handlers = dev->dv_activity_handlers;
2619 old_size = dev->dv_activity_count;
2620
2621 for (i = 0; i < old_size; ++i) {
2622 if (old_handlers[i] == handler)
2623 break;
2624 if (old_handlers[i] == NULL)
2625 return; /* XXX panic? */
2626 }
2627
2628 if (i == old_size)
2629 return; /* XXX panic? */
2630
2631 for (; i < old_size - 1; ++i) {
2632 if ((old_handlers[i] = old_handlers[i + 1]) != NULL)
2633 continue;
2634
2635 if (i == 0) {
2636 s = splhigh();
2637 dev->dv_activity_count = 0;
2638 dev->dv_activity_handlers = NULL;
2639 splx(s);
2640 kmem_free(old_handlers, sizeof(void *[old_size]));
2641 }
2642 return;
2643 }
2644 old_handlers[i] = NULL;
2645 }
2646
2647 /* Return true iff the device_t `dev' exists at generation `gen'. */
2648 static bool
2649 device_exists_at(device_t dv, devgen_t gen)
2650 {
2651 return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) &&
2652 dv->dv_add_gen <= gen;
2653 }
2654
2655 static bool
2656 deviter_visits(const deviter_t *di, device_t dv)
2657 {
2658 return device_exists_at(dv, di->di_gen);
2659 }
2660
2661 /*
2662 * Device Iteration
2663 *
2664 * deviter_t: a device iterator. Holds state for a "walk" visiting
2665 * each device_t's in the device tree.
2666 *
2667 * deviter_init(di, flags): initialize the device iterator `di'
2668 * to "walk" the device tree. deviter_next(di) will return
2669 * the first device_t in the device tree, or NULL if there are
2670 * no devices.
2671 *
2672 * `flags' is one or more of DEVITER_F_RW, indicating that the
2673 * caller intends to modify the device tree by calling
2674 * config_detach(9) on devices in the order that the iterator
2675 * returns them; DEVITER_F_ROOT_FIRST, asking for the devices
2676 * nearest the "root" of the device tree to be returned, first;
2677 * DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
2678 * the root of the device tree, first; and DEVITER_F_SHUTDOWN,
2679 * indicating both that deviter_init() should not respect any
2680 * locks on the device tree, and that deviter_next(di) may run
2681 * in more than one LWP before the walk has finished.
2682 *
2683 * Only one DEVITER_F_RW iterator may be in the device tree at
2684 * once.
2685 *
2686 * DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
2687 *
2688 * Results are undefined if the flags DEVITER_F_ROOT_FIRST and
2689 * DEVITER_F_LEAVES_FIRST are used in combination.
2690 *
2691 * deviter_first(di, flags): initialize the device iterator `di'
2692 * and return the first device_t in the device tree, or NULL
2693 * if there are no devices. The statement
2694 *
2695 * dv = deviter_first(di);
2696 *
2697 * is shorthand for
2698 *
2699 * deviter_init(di);
2700 * dv = deviter_next(di);
2701 *
2702 * deviter_next(di): return the next device_t in the device tree,
2703 * or NULL if there are no more devices. deviter_next(di)
2704 * is undefined if `di' was not initialized with deviter_init() or
2705 * deviter_first().
2706 *
2707 * deviter_release(di): stops iteration (subsequent calls to
2708 * deviter_next() will return NULL), releases any locks and
2709 * resources held by the device iterator.
2710 *
2711 * Device iteration does not return device_t's in any particular
2712 * order. An iterator will never return the same device_t twice.
2713 * Device iteration is guaranteed to complete---i.e., if deviter_next(di)
2714 * is called repeatedly on the same `di', it will eventually return
2715 * NULL. It is ok to attach/detach devices during device iteration.
2716 */
2717 void
2718 deviter_init(deviter_t *di, deviter_flags_t flags)
2719 {
2720 device_t dv;
2721 int s;
2722
2723 memset(di, 0, sizeof(*di));
2724
2725 s = config_alldevs_lock();
2726 if ((flags & DEVITER_F_SHUTDOWN) != 0)
2727 flags |= DEVITER_F_RW;
2728
2729 if ((flags & DEVITER_F_RW) != 0)
2730 alldevs_nwrite++;
2731 else
2732 alldevs_nread++;
2733 di->di_gen = alldevs_gen++;
2734 config_alldevs_unlock(s);
2735
2736 di->di_flags = flags;
2737
2738 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2739 case DEVITER_F_LEAVES_FIRST:
2740 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2741 if (!deviter_visits(di, dv))
2742 continue;
2743 di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
2744 }
2745 break;
2746 case DEVITER_F_ROOT_FIRST:
2747 TAILQ_FOREACH(dv, &alldevs, dv_list) {
2748 if (!deviter_visits(di, dv))
2749 continue;
2750 di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
2751 }
2752 break;
2753 default:
2754 break;
2755 }
2756
2757 deviter_reinit(di);
2758 }
2759
2760 static void
2761 deviter_reinit(deviter_t *di)
2762 {
2763 if ((di->di_flags & DEVITER_F_RW) != 0)
2764 di->di_prev = TAILQ_LAST(&alldevs, devicelist);
2765 else
2766 di->di_prev = TAILQ_FIRST(&alldevs);
2767 }
2768
2769 device_t
2770 deviter_first(deviter_t *di, deviter_flags_t flags)
2771 {
2772 deviter_init(di, flags);
2773 return deviter_next(di);
2774 }
2775
2776 static device_t
2777 deviter_next2(deviter_t *di)
2778 {
2779 device_t dv;
2780
2781 dv = di->di_prev;
2782
2783 if (dv == NULL)
2784 return NULL;
2785
2786 if ((di->di_flags & DEVITER_F_RW) != 0)
2787 di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
2788 else
2789 di->di_prev = TAILQ_NEXT(dv, dv_list);
2790
2791 return dv;
2792 }
2793
2794 static device_t
2795 deviter_next1(deviter_t *di)
2796 {
2797 device_t dv;
2798
2799 do {
2800 dv = deviter_next2(di);
2801 } while (dv != NULL && !deviter_visits(di, dv));
2802
2803 return dv;
2804 }
2805
2806 device_t
2807 deviter_next(deviter_t *di)
2808 {
2809 device_t dv = NULL;
2810
2811 switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
2812 case 0:
2813 return deviter_next1(di);
2814 case DEVITER_F_LEAVES_FIRST:
2815 while (di->di_curdepth >= 0) {
2816 if ((dv = deviter_next1(di)) == NULL) {
2817 di->di_curdepth--;
2818 deviter_reinit(di);
2819 } else if (dv->dv_depth == di->di_curdepth)
2820 break;
2821 }
2822 return dv;
2823 case DEVITER_F_ROOT_FIRST:
2824 while (di->di_curdepth <= di->di_maxdepth) {
2825 if ((dv = deviter_next1(di)) == NULL) {
2826 di->di_curdepth++;
2827 deviter_reinit(di);
2828 } else if (dv->dv_depth == di->di_curdepth)
2829 break;
2830 }
2831 return dv;
2832 default:
2833 return NULL;
2834 }
2835 }
2836
2837 void
2838 deviter_release(deviter_t *di)
2839 {
2840 bool rw = (di->di_flags & DEVITER_F_RW) != 0;
2841 int s;
2842
2843 s = config_alldevs_lock();
2844 if (rw)
2845 --alldevs_nwrite;
2846 else
2847 --alldevs_nread;
2848 /* XXX wake a garbage-collection thread */
2849 config_alldevs_unlock(s);
2850 }
2851
2852 const char *
2853 cfdata_ifattr(const struct cfdata *cf)
2854 {
2855 return cf->cf_pspec->cfp_iattr;
2856 }
2857
2858 bool
2859 ifattr_match(const char *snull, const char *t)
2860 {
2861 return (snull == NULL) || strcmp(snull, t) == 0;
2862 }
2863
2864 void
2865 null_childdetached(device_t self, device_t child)
2866 {
2867 /* do nothing */
2868 }
2869
2870 static void
2871 sysctl_detach_setup(struct sysctllog **clog)
2872 {
2873 const struct sysctlnode *node = NULL;
2874
2875 sysctl_createv(clog, 0, NULL, &node,
2876 CTLFLAG_PERMANENT,
2877 CTLTYPE_NODE, "kern", NULL,
2878 NULL, 0, NULL, 0,
2879 CTL_KERN, CTL_EOL);
2880
2881 if (node == NULL)
2882 return;
2883
2884 sysctl_createv(clog, 0, &node, NULL,
2885 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2886 CTLTYPE_BOOL, "detachall",
2887 SYSCTL_DESCR("Detach all devices at shutdown"),
2888 NULL, 0, &detachall, 0,
2889 CTL_CREATE, CTL_EOL);
2890 }
2891