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