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