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