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