Sun Nov 8 21:03:00 2015 UTC ()
Add the parent device (if present) to the device properties, making it
easier to identify devices in the tree with drvctl(8).


(joerg)
diff -r1.235 -r1.236 src/sys/kern/subr_autoconf.c

cvs diff -r1.235 -r1.236 src/sys/kern/subr_autoconf.c (switch to unified diff)

--- src/sys/kern/subr_autoconf.c 2015/04/13 16:46:33 1.235
+++ src/sys/kern/subr_autoconf.c 2015/11/08 21:03:00 1.236
@@ -1,2436 +1,2440 @@ @@ -1,2436 +1,2440 @@
1/* $NetBSD: subr_autoconf.c,v 1.235 2015/04/13 16:46:33 riastradh Exp $ */ 1/* $NetBSD: subr_autoconf.c,v 1.236 2015/11/08 21:03:00 joerg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996, 2000 Christopher G. Demetriou 4 * Copyright (c) 1996, 2000 Christopher G. Demetriou
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 16 * must display the following acknowledgement:
17 * This product includes software developed for the 17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for 18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD. 19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products 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. 21 * derived from this software without specific prior written permission.
22 * 22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 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 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * 33 *
34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )-- 34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
35 */ 35 */
36 36
37/* 37/*
38 * Copyright (c) 1992, 1993 38 * Copyright (c) 1992, 1993
39 * The Regents of the University of California. All rights reserved. 39 * The Regents of the University of California. All rights reserved.
40 * 40 *
41 * This software was developed by the Computer Systems Engineering group 41 * This software was developed by the Computer Systems Engineering group
42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
43 * contributed to Berkeley. 43 * contributed to Berkeley.
44 * 44 *
45 * All advertising materials mentioning features or use of this software 45 * All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement: 46 * must display the following acknowledgement:
47 * This product includes software developed by the University of 47 * This product includes software developed by the University of
48 * California, Lawrence Berkeley Laboratories. 48 * California, Lawrence Berkeley Laboratories.
49 * 49 *
50 * Redistribution and use in source and binary forms, with or without 50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions 51 * modification, are permitted provided that the following conditions
52 * are met: 52 * are met:
53 * 1. Redistributions of source code must retain the above copyright 53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer. 54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright 55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the 56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution. 57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors 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 59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission. 60 * without specific prior written permission.
61 * 61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE. 72 * SUCH DAMAGE.
73 * 73 *
74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL) 74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
75 * 75 *
76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94 76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
77 */ 77 */
78 78
79#include <sys/cdefs.h> 79#include <sys/cdefs.h>
80__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.235 2015/04/13 16:46:33 riastradh Exp $"); 80__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.236 2015/11/08 21:03:00 joerg Exp $");
81 81
82#ifdef _KERNEL_OPT 82#ifdef _KERNEL_OPT
83#include "opt_ddb.h" 83#include "opt_ddb.h"
84#include "drvctl.h" 84#include "drvctl.h"
85#endif 85#endif
86 86
87#include <sys/param.h> 87#include <sys/param.h>
88#include <sys/device.h> 88#include <sys/device.h>
89#include <sys/disklabel.h> 89#include <sys/disklabel.h>
90#include <sys/conf.h> 90#include <sys/conf.h>
91#include <sys/kauth.h> 91#include <sys/kauth.h>
92#include <sys/kmem.h> 92#include <sys/kmem.h>
93#include <sys/systm.h> 93#include <sys/systm.h>
94#include <sys/kernel.h> 94#include <sys/kernel.h>
95#include <sys/errno.h> 95#include <sys/errno.h>
96#include <sys/proc.h> 96#include <sys/proc.h>
97#include <sys/reboot.h> 97#include <sys/reboot.h>
98#include <sys/kthread.h> 98#include <sys/kthread.h>
99#include <sys/buf.h> 99#include <sys/buf.h>
100#include <sys/dirent.h> 100#include <sys/dirent.h>
101#include <sys/mount.h> 101#include <sys/mount.h>
102#include <sys/namei.h> 102#include <sys/namei.h>
103#include <sys/unistd.h> 103#include <sys/unistd.h>
104#include <sys/fcntl.h> 104#include <sys/fcntl.h>
105#include <sys/lockf.h> 105#include <sys/lockf.h>
106#include <sys/callout.h> 106#include <sys/callout.h>
107#include <sys/devmon.h> 107#include <sys/devmon.h>
108#include <sys/cpu.h> 108#include <sys/cpu.h>
109#include <sys/sysctl.h> 109#include <sys/sysctl.h>
110 110
111#include <sys/disk.h> 111#include <sys/disk.h>
112 112
113#include <sys/rndsource.h> 113#include <sys/rndsource.h>
114 114
115#include <machine/limits.h> 115#include <machine/limits.h>
116 116
117/* 117/*
118 * Autoconfiguration subroutines. 118 * Autoconfiguration subroutines.
119 */ 119 */
120 120
121/* 121/*
122 * Device autoconfiguration timings are mixed into the entropy pool. 122 * Device autoconfiguration timings are mixed into the entropy pool.
123 */ 123 */
124extern krndsource_t rnd_autoconf_source; 124extern krndsource_t rnd_autoconf_source;
125 125
126/* 126/*
127 * ioconf.c exports exactly two names: cfdata and cfroots. All system 127 * ioconf.c exports exactly two names: cfdata and cfroots. All system
128 * devices and drivers are found via these tables. 128 * devices and drivers are found via these tables.
129 */ 129 */
130extern struct cfdata cfdata[]; 130extern struct cfdata cfdata[];
131extern const short cfroots[]; 131extern const short cfroots[];
132 132
133/* 133/*
134 * List of all cfdriver structures. We use this to detect duplicates 134 * List of all cfdriver structures. We use this to detect duplicates
135 * when other cfdrivers are loaded. 135 * when other cfdrivers are loaded.
136 */ 136 */
137struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 137struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
138extern struct cfdriver * const cfdriver_list_initial[]; 138extern struct cfdriver * const cfdriver_list_initial[];
139 139
140/* 140/*
141 * Initial list of cfattach's. 141 * Initial list of cfattach's.
142 */ 142 */
143extern const struct cfattachinit cfattachinit[]; 143extern const struct cfattachinit cfattachinit[];
144 144
145/* 145/*
146 * List of cfdata tables. We always have one such list -- the one 146 * List of cfdata tables. We always have one such list -- the one
147 * built statically when the kernel was configured. 147 * built statically when the kernel was configured.
148 */ 148 */
149struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables); 149struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
150static struct cftable initcftable; 150static struct cftable initcftable;
151 151
152#define ROOT ((device_t)NULL) 152#define ROOT ((device_t)NULL)
153 153
154struct matchinfo { 154struct matchinfo {
155 cfsubmatch_t fn; 155 cfsubmatch_t fn;
156 device_t parent; 156 device_t parent;
157 const int *locs; 157 const int *locs;
158 void *aux; 158 void *aux;
159 struct cfdata *match; 159 struct cfdata *match;
160 int pri; 160 int pri;
161}; 161};
162 162
163struct alldevs_foray { 163struct alldevs_foray {
164 int af_s; 164 int af_s;
165 struct devicelist af_garbage; 165 struct devicelist af_garbage;
166}; 166};
167 167
168static char *number(char *, int); 168static char *number(char *, int);
169static void mapply(struct matchinfo *, cfdata_t); 169static void mapply(struct matchinfo *, cfdata_t);
170static device_t config_devalloc(const device_t, const cfdata_t, const int *); 170static device_t config_devalloc(const device_t, const cfdata_t, const int *);
171static void config_devdelete(device_t); 171static void config_devdelete(device_t);
172static void config_devunlink(device_t, struct devicelist *); 172static void config_devunlink(device_t, struct devicelist *);
173static void config_makeroom(int, struct cfdriver *); 173static void config_makeroom(int, struct cfdriver *);
174static void config_devlink(device_t); 174static void config_devlink(device_t);
175static void config_alldevs_unlock(int); 175static void config_alldevs_unlock(int);
176static int config_alldevs_lock(void); 176static int config_alldevs_lock(void);
177static void config_alldevs_enter(struct alldevs_foray *); 177static void config_alldevs_enter(struct alldevs_foray *);
178static void config_alldevs_exit(struct alldevs_foray *); 178static void config_alldevs_exit(struct alldevs_foray *);
179static void config_add_attrib_dict(device_t); 179static void config_add_attrib_dict(device_t);
180 180
181static void config_collect_garbage(struct devicelist *); 181static void config_collect_garbage(struct devicelist *);
182static void config_dump_garbage(struct devicelist *); 182static void config_dump_garbage(struct devicelist *);
183 183
184static void pmflock_debug(device_t, const char *, int); 184static void pmflock_debug(device_t, const char *, int);
185 185
186static device_t deviter_next1(deviter_t *); 186static device_t deviter_next1(deviter_t *);
187static void deviter_reinit(deviter_t *); 187static void deviter_reinit(deviter_t *);
188 188
189struct deferred_config { 189struct deferred_config {
190 TAILQ_ENTRY(deferred_config) dc_queue; 190 TAILQ_ENTRY(deferred_config) dc_queue;
191 device_t dc_dev; 191 device_t dc_dev;
192 void (*dc_func)(device_t); 192 void (*dc_func)(device_t);
193}; 193};
194 194
195TAILQ_HEAD(deferred_config_head, deferred_config); 195TAILQ_HEAD(deferred_config_head, deferred_config);
196 196
197struct deferred_config_head deferred_config_queue = 197struct deferred_config_head deferred_config_queue =
198 TAILQ_HEAD_INITIALIZER(deferred_config_queue); 198 TAILQ_HEAD_INITIALIZER(deferred_config_queue);
199struct deferred_config_head interrupt_config_queue = 199struct deferred_config_head interrupt_config_queue =
200 TAILQ_HEAD_INITIALIZER(interrupt_config_queue); 200 TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
201int interrupt_config_threads = 8; 201int interrupt_config_threads = 8;
202struct deferred_config_head mountroot_config_queue = 202struct deferred_config_head mountroot_config_queue =
203 TAILQ_HEAD_INITIALIZER(mountroot_config_queue); 203 TAILQ_HEAD_INITIALIZER(mountroot_config_queue);
204int mountroot_config_threads = 2; 204int mountroot_config_threads = 2;
205static lwp_t **mountroot_config_lwpids; 205static lwp_t **mountroot_config_lwpids;
206static size_t mountroot_config_lwpids_size; 206static size_t mountroot_config_lwpids_size;
207static bool root_is_mounted = false; 207static bool root_is_mounted = false;
208 208
209static void config_process_deferred(struct deferred_config_head *, device_t); 209static void config_process_deferred(struct deferred_config_head *, device_t);
210 210
211/* Hooks to finalize configuration once all real devices have been found. */ 211/* Hooks to finalize configuration once all real devices have been found. */
212struct finalize_hook { 212struct finalize_hook {
213 TAILQ_ENTRY(finalize_hook) f_list; 213 TAILQ_ENTRY(finalize_hook) f_list;
214 int (*f_func)(device_t); 214 int (*f_func)(device_t);
215 device_t f_dev; 215 device_t f_dev;
216}; 216};
217static TAILQ_HEAD(, finalize_hook) config_finalize_list = 217static TAILQ_HEAD(, finalize_hook) config_finalize_list =
218 TAILQ_HEAD_INITIALIZER(config_finalize_list); 218 TAILQ_HEAD_INITIALIZER(config_finalize_list);
219static int config_finalize_done; 219static int config_finalize_done;
220 220
221/* list of all devices */ 221/* list of all devices */
222static struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs); 222static struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
223static kmutex_t alldevs_mtx; 223static kmutex_t alldevs_mtx;
224static volatile bool alldevs_garbage = false; 224static volatile bool alldevs_garbage = false;
225static volatile devgen_t alldevs_gen = 1; 225static volatile devgen_t alldevs_gen = 1;
226static volatile int alldevs_nread = 0; 226static volatile int alldevs_nread = 0;
227static volatile int alldevs_nwrite = 0; 227static volatile int alldevs_nwrite = 0;
228 228
229static int config_pending; /* semaphore for mountroot */ 229static int config_pending; /* semaphore for mountroot */
230static kmutex_t config_misc_lock; 230static kmutex_t config_misc_lock;
231static kcondvar_t config_misc_cv; 231static kcondvar_t config_misc_cv;
232 232
233static bool detachall = false; 233static bool detachall = false;
234 234
235#define STREQ(s1, s2) \ 235#define STREQ(s1, s2) \
236 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 236 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
237 237
238static bool config_initialized = false; /* config_init() has been called. */ 238static bool config_initialized = false; /* config_init() has been called. */
239 239
240static int config_do_twiddle; 240static int config_do_twiddle;
241static callout_t config_twiddle_ch; 241static callout_t config_twiddle_ch;
242 242
243static void sysctl_detach_setup(struct sysctllog **); 243static void sysctl_detach_setup(struct sysctllog **);
244 244
245typedef int (*cfdriver_fn)(struct cfdriver *); 245typedef int (*cfdriver_fn)(struct cfdriver *);
246static int 246static int
247frob_cfdrivervec(struct cfdriver * const *cfdriverv, 247frob_cfdrivervec(struct cfdriver * const *cfdriverv,
248 cfdriver_fn drv_do, cfdriver_fn drv_undo, 248 cfdriver_fn drv_do, cfdriver_fn drv_undo,
249 const char *style, bool dopanic) 249 const char *style, bool dopanic)
250{ 250{
251 void (*pr)(const char *, ...) __printflike(1, 2) = 251 void (*pr)(const char *, ...) __printflike(1, 2) =
252 dopanic ? panic : printf; 252 dopanic ? panic : printf;
253 int i, error = 0, e2 __diagused; 253 int i, error = 0, e2 __diagused;
254 254
255 for (i = 0; cfdriverv[i] != NULL; i++) { 255 for (i = 0; cfdriverv[i] != NULL; i++) {
256 if ((error = drv_do(cfdriverv[i])) != 0) { 256 if ((error = drv_do(cfdriverv[i])) != 0) {
257 pr("configure: `%s' driver %s failed: %d", 257 pr("configure: `%s' driver %s failed: %d",
258 cfdriverv[i]->cd_name, style, error); 258 cfdriverv[i]->cd_name, style, error);
259 goto bad; 259 goto bad;
260 } 260 }
261 } 261 }
262 262
263 KASSERT(error == 0); 263 KASSERT(error == 0);
264 return 0; 264 return 0;
265 265
266 bad: 266 bad:
267 printf("\n"); 267 printf("\n");
268 for (i--; i >= 0; i--) { 268 for (i--; i >= 0; i--) {
269 e2 = drv_undo(cfdriverv[i]); 269 e2 = drv_undo(cfdriverv[i]);
270 KASSERT(e2 == 0); 270 KASSERT(e2 == 0);
271 } 271 }
272 272
273 return error; 273 return error;
274} 274}
275 275
276typedef int (*cfattach_fn)(const char *, struct cfattach *); 276typedef int (*cfattach_fn)(const char *, struct cfattach *);
277static int 277static int
278frob_cfattachvec(const struct cfattachinit *cfattachv, 278frob_cfattachvec(const struct cfattachinit *cfattachv,
279 cfattach_fn att_do, cfattach_fn att_undo, 279 cfattach_fn att_do, cfattach_fn att_undo,
280 const char *style, bool dopanic) 280 const char *style, bool dopanic)
281{ 281{
282 const struct cfattachinit *cfai = NULL; 282 const struct cfattachinit *cfai = NULL;
283 void (*pr)(const char *, ...) __printflike(1, 2) = 283 void (*pr)(const char *, ...) __printflike(1, 2) =
284 dopanic ? panic : printf; 284 dopanic ? panic : printf;
285 int j = 0, error = 0, e2 __diagused; 285 int j = 0, error = 0, e2 __diagused;
286 286
287 for (cfai = &cfattachv[0]; cfai->cfai_name != NULL; cfai++) { 287 for (cfai = &cfattachv[0]; cfai->cfai_name != NULL; cfai++) {
288 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 288 for (j = 0; cfai->cfai_list[j] != NULL; j++) {
289 if ((error = att_do(cfai->cfai_name, 289 if ((error = att_do(cfai->cfai_name,
290 cfai->cfai_list[j])) != 0) { 290 cfai->cfai_list[j])) != 0) {
291 pr("configure: attachment `%s' " 291 pr("configure: attachment `%s' "
292 "of `%s' driver %s failed: %d", 292 "of `%s' driver %s failed: %d",
293 cfai->cfai_list[j]->ca_name, 293 cfai->cfai_list[j]->ca_name,
294 cfai->cfai_name, style, error); 294 cfai->cfai_name, style, error);
295 goto bad; 295 goto bad;
296 } 296 }
297 } 297 }
298 } 298 }
299 299
300 KASSERT(error == 0); 300 KASSERT(error == 0);
301 return 0; 301 return 0;
302 302
303 bad: 303 bad:
304 /* 304 /*
305 * Rollback in reverse order. dunno if super-important, but 305 * Rollback in reverse order. dunno if super-important, but
306 * do that anyway. Although the code looks a little like 306 * do that anyway. Although the code looks a little like
307 * someone did a little integration (in the math sense). 307 * someone did a little integration (in the math sense).
308 */ 308 */
309 printf("\n"); 309 printf("\n");
310 if (cfai) { 310 if (cfai) {
311 bool last; 311 bool last;
312 312
313 for (last = false; last == false; ) { 313 for (last = false; last == false; ) {
314 if (cfai == &cfattachv[0]) 314 if (cfai == &cfattachv[0])
315 last = true; 315 last = true;
316 for (j--; j >= 0; j--) { 316 for (j--; j >= 0; j--) {
317 e2 = att_undo(cfai->cfai_name, 317 e2 = att_undo(cfai->cfai_name,
318 cfai->cfai_list[j]); 318 cfai->cfai_list[j]);
319 KASSERT(e2 == 0); 319 KASSERT(e2 == 0);
320 } 320 }
321 if (!last) { 321 if (!last) {
322 cfai--; 322 cfai--;
323 for (j = 0; cfai->cfai_list[j] != NULL; j++) 323 for (j = 0; cfai->cfai_list[j] != NULL; j++)
324 ; 324 ;
325 } 325 }
326 } 326 }
327 } 327 }
328 328
329 return error; 329 return error;
330} 330}
331 331
332/* 332/*
333 * Initialize the autoconfiguration data structures. Normally this 333 * Initialize the autoconfiguration data structures. Normally this
334 * is done by configure(), but some platforms need to do this very 334 * is done by configure(), but some platforms need to do this very
335 * early (to e.g. initialize the console). 335 * early (to e.g. initialize the console).
336 */ 336 */
337void 337void
338config_init(void) 338config_init(void)
339{ 339{
340 340
341 KASSERT(config_initialized == false); 341 KASSERT(config_initialized == false);
342 342
343 mutex_init(&alldevs_mtx, MUTEX_DEFAULT, IPL_VM); 343 mutex_init(&alldevs_mtx, MUTEX_DEFAULT, IPL_VM);
344 344
345 mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE); 345 mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE);
346 cv_init(&config_misc_cv, "cfgmisc"); 346 cv_init(&config_misc_cv, "cfgmisc");
347 347
348 callout_init(&config_twiddle_ch, CALLOUT_MPSAFE); 348 callout_init(&config_twiddle_ch, CALLOUT_MPSAFE);
349 349
350 frob_cfdrivervec(cfdriver_list_initial, 350 frob_cfdrivervec(cfdriver_list_initial,
351 config_cfdriver_attach, NULL, "bootstrap", true); 351 config_cfdriver_attach, NULL, "bootstrap", true);
352 frob_cfattachvec(cfattachinit, 352 frob_cfattachvec(cfattachinit,
353 config_cfattach_attach, NULL, "bootstrap", true); 353 config_cfattach_attach, NULL, "bootstrap", true);
354 354
355 initcftable.ct_cfdata = cfdata; 355 initcftable.ct_cfdata = cfdata;
356 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 356 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
357 357
358 config_initialized = true; 358 config_initialized = true;
359} 359}
360 360
361/* 361/*
362 * Init or fini drivers and attachments. Either all or none 362 * Init or fini drivers and attachments. Either all or none
363 * are processed (via rollback). It would be nice if this were 363 * are processed (via rollback). It would be nice if this were
364 * atomic to outside consumers, but with the current state of 364 * atomic to outside consumers, but with the current state of
365 * locking ... 365 * locking ...
366 */ 366 */
367int 367int
368config_init_component(struct cfdriver * const *cfdriverv, 368config_init_component(struct cfdriver * const *cfdriverv,
369 const struct cfattachinit *cfattachv, struct cfdata *cfdatav) 369 const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
370{ 370{
371 int error; 371 int error;
372 372
373 if ((error = frob_cfdrivervec(cfdriverv, 373 if ((error = frob_cfdrivervec(cfdriverv,
374 config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0) 374 config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0)
375 return error; 375 return error;
376 if ((error = frob_cfattachvec(cfattachv, 376 if ((error = frob_cfattachvec(cfattachv,
377 config_cfattach_attach, config_cfattach_detach, 377 config_cfattach_attach, config_cfattach_detach,
378 "init", false)) != 0) { 378 "init", false)) != 0) {
379 frob_cfdrivervec(cfdriverv, 379 frob_cfdrivervec(cfdriverv,
380 config_cfdriver_detach, NULL, "init rollback", true); 380 config_cfdriver_detach, NULL, "init rollback", true);
381 return error; 381 return error;
382 } 382 }
383 if ((error = config_cfdata_attach(cfdatav, 1)) != 0) { 383 if ((error = config_cfdata_attach(cfdatav, 1)) != 0) {
384 frob_cfattachvec(cfattachv, 384 frob_cfattachvec(cfattachv,
385 config_cfattach_detach, NULL, "init rollback", true); 385 config_cfattach_detach, NULL, "init rollback", true);
386 frob_cfdrivervec(cfdriverv, 386 frob_cfdrivervec(cfdriverv,
387 config_cfdriver_detach, NULL, "init rollback", true); 387 config_cfdriver_detach, NULL, "init rollback", true);
388 return error; 388 return error;
389 } 389 }
390 390
391 return 0; 391 return 0;
392} 392}
393 393
394int 394int
395config_fini_component(struct cfdriver * const *cfdriverv, 395config_fini_component(struct cfdriver * const *cfdriverv,
396 const struct cfattachinit *cfattachv, struct cfdata *cfdatav) 396 const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
397{ 397{
398 int error; 398 int error;
399 399
400 if ((error = config_cfdata_detach(cfdatav)) != 0) 400 if ((error = config_cfdata_detach(cfdatav)) != 0)
401 return error; 401 return error;
402 if ((error = frob_cfattachvec(cfattachv, 402 if ((error = frob_cfattachvec(cfattachv,
403 config_cfattach_detach, config_cfattach_attach, 403 config_cfattach_detach, config_cfattach_attach,
404 "fini", false)) != 0) { 404 "fini", false)) != 0) {
405 if (config_cfdata_attach(cfdatav, 0) != 0) 405 if (config_cfdata_attach(cfdatav, 0) != 0)
406 panic("config_cfdata fini rollback failed"); 406 panic("config_cfdata fini rollback failed");
407 return error; 407 return error;
408 } 408 }
409 if ((error = frob_cfdrivervec(cfdriverv, 409 if ((error = frob_cfdrivervec(cfdriverv,
410 config_cfdriver_detach, config_cfdriver_attach, 410 config_cfdriver_detach, config_cfdriver_attach,
411 "fini", false)) != 0) { 411 "fini", false)) != 0) {
412 frob_cfattachvec(cfattachv, 412 frob_cfattachvec(cfattachv,
413 config_cfattach_attach, NULL, "fini rollback", true); 413 config_cfattach_attach, NULL, "fini rollback", true);
414 if (config_cfdata_attach(cfdatav, 0) != 0) 414 if (config_cfdata_attach(cfdatav, 0) != 0)
415 panic("config_cfdata fini rollback failed"); 415 panic("config_cfdata fini rollback failed");
416 return error; 416 return error;
417 } 417 }
418 418
419 return 0; 419 return 0;
420} 420}
421 421
422void 422void
423config_init_mi(void) 423config_init_mi(void)
424{ 424{
425 425
426 if (!config_initialized) 426 if (!config_initialized)
427 config_init(); 427 config_init();
428 428
429 sysctl_detach_setup(NULL); 429 sysctl_detach_setup(NULL);
430} 430}
431 431
432void 432void
433config_deferred(device_t dev) 433config_deferred(device_t dev)
434{ 434{
435 config_process_deferred(&deferred_config_queue, dev); 435 config_process_deferred(&deferred_config_queue, dev);
436 config_process_deferred(&interrupt_config_queue, dev); 436 config_process_deferred(&interrupt_config_queue, dev);
437 config_process_deferred(&mountroot_config_queue, dev); 437 config_process_deferred(&mountroot_config_queue, dev);
438} 438}
439 439
440static void 440static void
441config_interrupts_thread(void *cookie) 441config_interrupts_thread(void *cookie)
442{ 442{
443 struct deferred_config *dc; 443 struct deferred_config *dc;
444 444
445 while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) { 445 while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) {
446 TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue); 446 TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue);
447 (*dc->dc_func)(dc->dc_dev); 447 (*dc->dc_func)(dc->dc_dev);
448 config_pending_decr(dc->dc_dev); 448 config_pending_decr(dc->dc_dev);
449 kmem_free(dc, sizeof(*dc)); 449 kmem_free(dc, sizeof(*dc));
450 } 450 }
451 kthread_exit(0); 451 kthread_exit(0);
452} 452}
453 453
454void 454void
455config_create_interruptthreads(void) 455config_create_interruptthreads(void)
456{ 456{
457 int i; 457 int i;
458 458
459 for (i = 0; i < interrupt_config_threads; i++) { 459 for (i = 0; i < interrupt_config_threads; i++) {
460 (void)kthread_create(PRI_NONE, 0, NULL, 460 (void)kthread_create(PRI_NONE, 0, NULL,
461 config_interrupts_thread, NULL, NULL, "configintr"); 461 config_interrupts_thread, NULL, NULL, "configintr");
462 } 462 }
463} 463}
464 464
465static void 465static void
466config_mountroot_thread(void *cookie) 466config_mountroot_thread(void *cookie)
467{ 467{
468 struct deferred_config *dc; 468 struct deferred_config *dc;
469 469
470 while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) { 470 while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) {
471 TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue); 471 TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue);
472 (*dc->dc_func)(dc->dc_dev); 472 (*dc->dc_func)(dc->dc_dev);
473 kmem_free(dc, sizeof(*dc)); 473 kmem_free(dc, sizeof(*dc));
474 } 474 }
475 kthread_exit(0); 475 kthread_exit(0);
476} 476}
477 477
478void 478void
479config_create_mountrootthreads(void) 479config_create_mountrootthreads(void)
480{ 480{
481 int i; 481 int i;
482 482
483 if (!root_is_mounted) 483 if (!root_is_mounted)
484 root_is_mounted = true; 484 root_is_mounted = true;
485 485
486 mountroot_config_lwpids_size = sizeof(mountroot_config_lwpids) * 486 mountroot_config_lwpids_size = sizeof(mountroot_config_lwpids) *
487 mountroot_config_threads; 487 mountroot_config_threads;
488 mountroot_config_lwpids = kmem_alloc(mountroot_config_lwpids_size, 488 mountroot_config_lwpids = kmem_alloc(mountroot_config_lwpids_size,
489 KM_NOSLEEP); 489 KM_NOSLEEP);
490 KASSERT(mountroot_config_lwpids); 490 KASSERT(mountroot_config_lwpids);
491 for (i = 0; i < mountroot_config_threads; i++) { 491 for (i = 0; i < mountroot_config_threads; i++) {
492 mountroot_config_lwpids[i] = 0; 492 mountroot_config_lwpids[i] = 0;
493 (void)kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 493 (void)kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
494 config_mountroot_thread, NULL, 494 config_mountroot_thread, NULL,
495 &mountroot_config_lwpids[i], 495 &mountroot_config_lwpids[i],
496 "configroot"); 496 "configroot");
497 } 497 }
498} 498}
499 499
500void 500void
501config_finalize_mountroot(void) 501config_finalize_mountroot(void)
502{ 502{
503 int i, error; 503 int i, error;
504 504
505 for (i = 0; i < mountroot_config_threads; i++) { 505 for (i = 0; i < mountroot_config_threads; i++) {
506 if (mountroot_config_lwpids[i] == 0) 506 if (mountroot_config_lwpids[i] == 0)
507 continue; 507 continue;
508 508
509 error = kthread_join(mountroot_config_lwpids[i]); 509 error = kthread_join(mountroot_config_lwpids[i]);
510 if (error) 510 if (error)
511 printf("%s: thread %x joined with error %d\n", 511 printf("%s: thread %x joined with error %d\n",
512 __func__, i, error); 512 __func__, i, error);
513 } 513 }
514 kmem_free(mountroot_config_lwpids, mountroot_config_lwpids_size); 514 kmem_free(mountroot_config_lwpids, mountroot_config_lwpids_size);
515} 515}
516 516
517/* 517/*
518 * Announce device attach/detach to userland listeners. 518 * Announce device attach/detach to userland listeners.
519 */ 519 */
520static void 520static void
521devmon_report_device(device_t dev, bool isattach) 521devmon_report_device(device_t dev, bool isattach)
522{ 522{
523#if NDRVCTL > 0 523#if NDRVCTL > 0
524 prop_dictionary_t ev; 524 prop_dictionary_t ev;
525 const char *parent; 525 const char *parent;
526 const char *what; 526 const char *what;
527 device_t pdev = device_parent(dev); 527 device_t pdev = device_parent(dev);
528 528
529 ev = prop_dictionary_create(); 529 ev = prop_dictionary_create();
530 if (ev == NULL) 530 if (ev == NULL)
531 return; 531 return;
532 532
533 what = (isattach ? "device-attach" : "device-detach"); 533 what = (isattach ? "device-attach" : "device-detach");
534 parent = (pdev == NULL ? "root" : device_xname(pdev)); 534 parent = (pdev == NULL ? "root" : device_xname(pdev));
535 if (!prop_dictionary_set_cstring(ev, "device", device_xname(dev)) || 535 if (!prop_dictionary_set_cstring(ev, "device", device_xname(dev)) ||
536 !prop_dictionary_set_cstring(ev, "parent", parent)) { 536 !prop_dictionary_set_cstring(ev, "parent", parent)) {
537 prop_object_release(ev); 537 prop_object_release(ev);
538 return; 538 return;
539 } 539 }
540 540
541 devmon_insert(what, ev); 541 devmon_insert(what, ev);
542#endif 542#endif
543} 543}
544 544
545/* 545/*
546 * Add a cfdriver to the system. 546 * Add a cfdriver to the system.
547 */ 547 */
548int 548int
549config_cfdriver_attach(struct cfdriver *cd) 549config_cfdriver_attach(struct cfdriver *cd)
550{ 550{
551 struct cfdriver *lcd; 551 struct cfdriver *lcd;
552 552
553 /* Make sure this driver isn't already in the system. */ 553 /* Make sure this driver isn't already in the system. */
554 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 554 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
555 if (STREQ(lcd->cd_name, cd->cd_name)) 555 if (STREQ(lcd->cd_name, cd->cd_name))
556 return EEXIST; 556 return EEXIST;
557 } 557 }
558 558
559 LIST_INIT(&cd->cd_attach); 559 LIST_INIT(&cd->cd_attach);
560 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 560 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
561 561
562 return 0; 562 return 0;
563} 563}
564 564
565/* 565/*
566 * Remove a cfdriver from the system. 566 * Remove a cfdriver from the system.
567 */ 567 */
568int 568int
569config_cfdriver_detach(struct cfdriver *cd) 569config_cfdriver_detach(struct cfdriver *cd)
570{ 570{
571 struct alldevs_foray af; 571 struct alldevs_foray af;
572 int i, rc = 0; 572 int i, rc = 0;
573 573
574 config_alldevs_enter(&af); 574 config_alldevs_enter(&af);
575 /* Make sure there are no active instances. */ 575 /* Make sure there are no active instances. */
576 for (i = 0; i < cd->cd_ndevs; i++) { 576 for (i = 0; i < cd->cd_ndevs; i++) {
577 if (cd->cd_devs[i] != NULL) { 577 if (cd->cd_devs[i] != NULL) {
578 rc = EBUSY; 578 rc = EBUSY;
579 break; 579 break;
580 } 580 }
581 } 581 }
582 config_alldevs_exit(&af); 582 config_alldevs_exit(&af);
583 583
584 if (rc != 0) 584 if (rc != 0)
585 return rc; 585 return rc;
586 586
587 /* ...and no attachments loaded. */ 587 /* ...and no attachments loaded. */
588 if (LIST_EMPTY(&cd->cd_attach) == 0) 588 if (LIST_EMPTY(&cd->cd_attach) == 0)
589 return EBUSY; 589 return EBUSY;
590 590
591 LIST_REMOVE(cd, cd_list); 591 LIST_REMOVE(cd, cd_list);
592 592
593 KASSERT(cd->cd_devs == NULL); 593 KASSERT(cd->cd_devs == NULL);
594 594
595 return 0; 595 return 0;
596} 596}
597 597
598/* 598/*
599 * Look up a cfdriver by name. 599 * Look up a cfdriver by name.
600 */ 600 */
601struct cfdriver * 601struct cfdriver *
602config_cfdriver_lookup(const char *name) 602config_cfdriver_lookup(const char *name)
603{ 603{
604 struct cfdriver *cd; 604 struct cfdriver *cd;
605 605
606 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 606 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
607 if (STREQ(cd->cd_name, name)) 607 if (STREQ(cd->cd_name, name))
608 return cd; 608 return cd;
609 } 609 }
610 610
611 return NULL; 611 return NULL;
612} 612}
613 613
614/* 614/*
615 * Add a cfattach to the specified driver. 615 * Add a cfattach to the specified driver.
616 */ 616 */
617int 617int
618config_cfattach_attach(const char *driver, struct cfattach *ca) 618config_cfattach_attach(const char *driver, struct cfattach *ca)
619{ 619{
620 struct cfattach *lca; 620 struct cfattach *lca;
621 struct cfdriver *cd; 621 struct cfdriver *cd;
622 622
623 cd = config_cfdriver_lookup(driver); 623 cd = config_cfdriver_lookup(driver);
624 if (cd == NULL) 624 if (cd == NULL)
625 return ESRCH; 625 return ESRCH;
626 626
627 /* Make sure this attachment isn't already on this driver. */ 627 /* Make sure this attachment isn't already on this driver. */
628 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 628 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
629 if (STREQ(lca->ca_name, ca->ca_name)) 629 if (STREQ(lca->ca_name, ca->ca_name))
630 return EEXIST; 630 return EEXIST;
631 } 631 }
632 632
633 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 633 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
634 634
635 return 0; 635 return 0;
636} 636}
637 637
638/* 638/*
639 * Remove a cfattach from the specified driver. 639 * Remove a cfattach from the specified driver.
640 */ 640 */
641int 641int
642config_cfattach_detach(const char *driver, struct cfattach *ca) 642config_cfattach_detach(const char *driver, struct cfattach *ca)
643{ 643{
644 struct alldevs_foray af; 644 struct alldevs_foray af;
645 struct cfdriver *cd; 645 struct cfdriver *cd;
646 device_t dev; 646 device_t dev;
647 int i, rc = 0; 647 int i, rc = 0;
648 648
649 cd = config_cfdriver_lookup(driver); 649 cd = config_cfdriver_lookup(driver);
650 if (cd == NULL) 650 if (cd == NULL)
651 return ESRCH; 651 return ESRCH;
652 652
653 config_alldevs_enter(&af); 653 config_alldevs_enter(&af);
654 /* Make sure there are no active instances. */ 654 /* Make sure there are no active instances. */
655 for (i = 0; i < cd->cd_ndevs; i++) { 655 for (i = 0; i < cd->cd_ndevs; i++) {
656 if ((dev = cd->cd_devs[i]) == NULL) 656 if ((dev = cd->cd_devs[i]) == NULL)
657 continue; 657 continue;
658 if (dev->dv_cfattach == ca) { 658 if (dev->dv_cfattach == ca) {
659 rc = EBUSY; 659 rc = EBUSY;
660 break; 660 break;
661 } 661 }
662 } 662 }
663 config_alldevs_exit(&af); 663 config_alldevs_exit(&af);
664 664
665 if (rc != 0) 665 if (rc != 0)
666 return rc; 666 return rc;
667 667
668 LIST_REMOVE(ca, ca_list); 668 LIST_REMOVE(ca, ca_list);
669 669
670 return 0; 670 return 0;
671} 671}
672 672
673/* 673/*
674 * Look up a cfattach by name. 674 * Look up a cfattach by name.
675 */ 675 */
676static struct cfattach * 676static struct cfattach *
677config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 677config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
678{ 678{
679 struct cfattach *ca; 679 struct cfattach *ca;
680 680
681 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 681 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
682 if (STREQ(ca->ca_name, atname)) 682 if (STREQ(ca->ca_name, atname))
683 return ca; 683 return ca;
684 } 684 }
685 685
686 return NULL; 686 return NULL;
687} 687}
688 688
689/* 689/*
690 * Look up a cfattach by driver/attachment name. 690 * Look up a cfattach by driver/attachment name.
691 */ 691 */
692struct cfattach * 692struct cfattach *
693config_cfattach_lookup(const char *name, const char *atname) 693config_cfattach_lookup(const char *name, const char *atname)
694{ 694{
695 struct cfdriver *cd; 695 struct cfdriver *cd;
696 696
697 cd = config_cfdriver_lookup(name); 697 cd = config_cfdriver_lookup(name);
698 if (cd == NULL) 698 if (cd == NULL)
699 return NULL; 699 return NULL;
700 700
701 return config_cfattach_lookup_cd(cd, atname); 701 return config_cfattach_lookup_cd(cd, atname);
702} 702}
703 703
704/* 704/*
705 * Apply the matching function and choose the best. This is used 705 * Apply the matching function and choose the best. This is used
706 * a few times and we want to keep the code small. 706 * a few times and we want to keep the code small.
707 */ 707 */
708static void 708static void
709mapply(struct matchinfo *m, cfdata_t cf) 709mapply(struct matchinfo *m, cfdata_t cf)
710{ 710{
711 int pri; 711 int pri;
712 712
713 if (m->fn != NULL) { 713 if (m->fn != NULL) {
714 pri = (*m->fn)(m->parent, cf, m->locs, m->aux); 714 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
715 } else { 715 } else {
716 pri = config_match(m->parent, cf, m->aux); 716 pri = config_match(m->parent, cf, m->aux);
717 } 717 }
718 if (pri > m->pri) { 718 if (pri > m->pri) {
719 m->match = cf; 719 m->match = cf;
720 m->pri = pri; 720 m->pri = pri;
721 } 721 }
722} 722}
723 723
724int 724int
725config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 725config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
726{ 726{
727 const struct cfiattrdata *ci; 727 const struct cfiattrdata *ci;
728 const struct cflocdesc *cl; 728 const struct cflocdesc *cl;
729 int nlocs, i; 729 int nlocs, i;
730 730
731 ci = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver); 731 ci = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
732 KASSERT(ci); 732 KASSERT(ci);
733 nlocs = ci->ci_loclen; 733 nlocs = ci->ci_loclen;
734 KASSERT(!nlocs || locs); 734 KASSERT(!nlocs || locs);
735 for (i = 0; i < nlocs; i++) { 735 for (i = 0; i < nlocs; i++) {
736 cl = &ci->ci_locdesc[i]; 736 cl = &ci->ci_locdesc[i];
737 if (cl->cld_defaultstr != NULL && 737 if (cl->cld_defaultstr != NULL &&
738 cf->cf_loc[i] == cl->cld_default) 738 cf->cf_loc[i] == cl->cld_default)
739 continue; 739 continue;
740 if (cf->cf_loc[i] == locs[i]) 740 if (cf->cf_loc[i] == locs[i])
741 continue; 741 continue;
742 return 0; 742 return 0;
743 } 743 }
744 744
745 return config_match(parent, cf, aux); 745 return config_match(parent, cf, aux);
746} 746}
747 747
748/* 748/*
749 * Helper function: check whether the driver supports the interface attribute 749 * Helper function: check whether the driver supports the interface attribute
750 * and return its descriptor structure. 750 * and return its descriptor structure.
751 */ 751 */
752static const struct cfiattrdata * 752static const struct cfiattrdata *
753cfdriver_get_iattr(const struct cfdriver *cd, const char *ia) 753cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
754{ 754{
755 const struct cfiattrdata * const *cpp; 755 const struct cfiattrdata * const *cpp;
756 756
757 if (cd->cd_attrs == NULL) 757 if (cd->cd_attrs == NULL)
758 return 0; 758 return 0;
759 759
760 for (cpp = cd->cd_attrs; *cpp; cpp++) { 760 for (cpp = cd->cd_attrs; *cpp; cpp++) {
761 if (STREQ((*cpp)->ci_name, ia)) { 761 if (STREQ((*cpp)->ci_name, ia)) {
762 /* Match. */ 762 /* Match. */
763 return *cpp; 763 return *cpp;
764 } 764 }
765 } 765 }
766 return 0; 766 return 0;
767} 767}
768 768
769/* 769/*
770 * Lookup an interface attribute description by name. 770 * Lookup an interface attribute description by name.
771 * If the driver is given, consider only its supported attributes. 771 * If the driver is given, consider only its supported attributes.
772 */ 772 */
773const struct cfiattrdata * 773const struct cfiattrdata *
774cfiattr_lookup(const char *name, const struct cfdriver *cd) 774cfiattr_lookup(const char *name, const struct cfdriver *cd)
775{ 775{
776 const struct cfdriver *d; 776 const struct cfdriver *d;
777 const struct cfiattrdata *ia; 777 const struct cfiattrdata *ia;
778 778
779 if (cd) 779 if (cd)
780 return cfdriver_get_iattr(cd, name); 780 return cfdriver_get_iattr(cd, name);
781 781
782 LIST_FOREACH(d, &allcfdrivers, cd_list) { 782 LIST_FOREACH(d, &allcfdrivers, cd_list) {
783 ia = cfdriver_get_iattr(d, name); 783 ia = cfdriver_get_iattr(d, name);
784 if (ia) 784 if (ia)
785 return ia; 785 return ia;
786 } 786 }
787 return 0; 787 return 0;
788} 788}
789 789
790/* 790/*
791 * Determine if `parent' is a potential parent for a device spec based 791 * Determine if `parent' is a potential parent for a device spec based
792 * on `cfp'. 792 * on `cfp'.
793 */ 793 */
794static int 794static int
795cfparent_match(const device_t parent, const struct cfparent *cfp) 795cfparent_match(const device_t parent, const struct cfparent *cfp)
796{ 796{
797 struct cfdriver *pcd; 797 struct cfdriver *pcd;
798 798
799 /* We don't match root nodes here. */ 799 /* We don't match root nodes here. */
800 if (cfp == NULL) 800 if (cfp == NULL)
801 return 0; 801 return 0;
802 802
803 pcd = parent->dv_cfdriver; 803 pcd = parent->dv_cfdriver;
804 KASSERT(pcd != NULL); 804 KASSERT(pcd != NULL);
805 805
806 /* 806 /*
807 * First, ensure this parent has the correct interface 807 * First, ensure this parent has the correct interface
808 * attribute. 808 * attribute.
809 */ 809 */
810 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr)) 810 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
811 return 0; 811 return 0;
812 812
813 /* 813 /*
814 * If no specific parent device instance was specified (i.e. 814 * If no specific parent device instance was specified (i.e.
815 * we're attaching to the attribute only), we're done! 815 * we're attaching to the attribute only), we're done!
816 */ 816 */
817 if (cfp->cfp_parent == NULL) 817 if (cfp->cfp_parent == NULL)
818 return 1; 818 return 1;
819 819
820 /* 820 /*
821 * Check the parent device's name. 821 * Check the parent device's name.
822 */ 822 */
823 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 823 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
824 return 0; /* not the same parent */ 824 return 0; /* not the same parent */
825 825
826 /* 826 /*
827 * Make sure the unit number matches. 827 * Make sure the unit number matches.
828 */ 828 */
829 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 829 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
830 cfp->cfp_unit == parent->dv_unit) 830 cfp->cfp_unit == parent->dv_unit)
831 return 1; 831 return 1;
832 832
833 /* Unit numbers don't match. */ 833 /* Unit numbers don't match. */
834 return 0; 834 return 0;
835} 835}
836 836
837/* 837/*
838 * Helper for config_cfdata_attach(): check all devices whether it could be 838 * Helper for config_cfdata_attach(): check all devices whether it could be
839 * parent any attachment in the config data table passed, and rescan. 839 * parent any attachment in the config data table passed, and rescan.
840 */ 840 */
841static void 841static void
842rescan_with_cfdata(const struct cfdata *cf) 842rescan_with_cfdata(const struct cfdata *cf)
843{ 843{
844 device_t d; 844 device_t d;
845 const struct cfdata *cf1; 845 const struct cfdata *cf1;
846 deviter_t di; 846 deviter_t di;
847  847
848 848
849 /* 849 /*
850 * "alldevs" is likely longer than a modules's cfdata, so make it 850 * "alldevs" is likely longer than a modules's cfdata, so make it
851 * the outer loop. 851 * the outer loop.
852 */ 852 */
853 for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) { 853 for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) {
854 854
855 if (!(d->dv_cfattach->ca_rescan)) 855 if (!(d->dv_cfattach->ca_rescan))
856 continue; 856 continue;
857 857
858 for (cf1 = cf; cf1->cf_name; cf1++) { 858 for (cf1 = cf; cf1->cf_name; cf1++) {
859 859
860 if (!cfparent_match(d, cf1->cf_pspec)) 860 if (!cfparent_match(d, cf1->cf_pspec))
861 continue; 861 continue;
862 862
863 (*d->dv_cfattach->ca_rescan)(d, 863 (*d->dv_cfattach->ca_rescan)(d,
864 cfdata_ifattr(cf1), cf1->cf_loc); 864 cfdata_ifattr(cf1), cf1->cf_loc);
865 865
866 config_deferred(d); 866 config_deferred(d);
867 } 867 }
868 } 868 }
869 deviter_release(&di); 869 deviter_release(&di);
870} 870}
871 871
872/* 872/*
873 * Attach a supplemental config data table and rescan potential 873 * Attach a supplemental config data table and rescan potential
874 * parent devices if required. 874 * parent devices if required.
875 */ 875 */
876int 876int
877config_cfdata_attach(cfdata_t cf, int scannow) 877config_cfdata_attach(cfdata_t cf, int scannow)
878{ 878{
879 struct cftable *ct; 879 struct cftable *ct;
880 880
881 ct = kmem_alloc(sizeof(*ct), KM_SLEEP); 881 ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
882 ct->ct_cfdata = cf; 882 ct->ct_cfdata = cf;
883 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list); 883 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
884 884
885 if (scannow) 885 if (scannow)
886 rescan_with_cfdata(cf); 886 rescan_with_cfdata(cf);
887 887
888 return 0; 888 return 0;
889} 889}
890 890
891/* 891/*
892 * Helper for config_cfdata_detach: check whether a device is 892 * Helper for config_cfdata_detach: check whether a device is
893 * found through any attachment in the config data table. 893 * found through any attachment in the config data table.
894 */ 894 */
895static int 895static int
896dev_in_cfdata(device_t d, cfdata_t cf) 896dev_in_cfdata(device_t d, cfdata_t cf)
897{ 897{
898 const struct cfdata *cf1; 898 const struct cfdata *cf1;
899 899
900 for (cf1 = cf; cf1->cf_name; cf1++) 900 for (cf1 = cf; cf1->cf_name; cf1++)
901 if (d->dv_cfdata == cf1) 901 if (d->dv_cfdata == cf1)
902 return 1; 902 return 1;
903 903
904 return 0; 904 return 0;
905} 905}
906 906
907/* 907/*
908 * Detach a supplemental config data table. Detach all devices found 908 * Detach a supplemental config data table. Detach all devices found
909 * through that table (and thus keeping references to it) before. 909 * through that table (and thus keeping references to it) before.
910 */ 910 */
911int 911int
912config_cfdata_detach(cfdata_t cf) 912config_cfdata_detach(cfdata_t cf)
913{ 913{
914 device_t d; 914 device_t d;
915 int error = 0; 915 int error = 0;
916 struct cftable *ct; 916 struct cftable *ct;
917 deviter_t di; 917 deviter_t di;
918 918
919 for (d = deviter_first(&di, DEVITER_F_RW); d != NULL; 919 for (d = deviter_first(&di, DEVITER_F_RW); d != NULL;
920 d = deviter_next(&di)) { 920 d = deviter_next(&di)) {
921 if (!dev_in_cfdata(d, cf)) 921 if (!dev_in_cfdata(d, cf))
922 continue; 922 continue;
923 if ((error = config_detach(d, 0)) != 0) 923 if ((error = config_detach(d, 0)) != 0)
924 break; 924 break;
925 } 925 }
926 deviter_release(&di); 926 deviter_release(&di);
927 if (error) { 927 if (error) {
928 aprint_error_dev(d, "unable to detach instance\n"); 928 aprint_error_dev(d, "unable to detach instance\n");
929 return error; 929 return error;
930 } 930 }
931 931
932 TAILQ_FOREACH(ct, &allcftables, ct_list) { 932 TAILQ_FOREACH(ct, &allcftables, ct_list) {
933 if (ct->ct_cfdata == cf) { 933 if (ct->ct_cfdata == cf) {
934 TAILQ_REMOVE(&allcftables, ct, ct_list); 934 TAILQ_REMOVE(&allcftables, ct, ct_list);
935 kmem_free(ct, sizeof(*ct)); 935 kmem_free(ct, sizeof(*ct));
936 return 0; 936 return 0;
937 } 937 }
938 } 938 }
939 939
940 /* not found -- shouldn't happen */ 940 /* not found -- shouldn't happen */
941 return EINVAL; 941 return EINVAL;
942} 942}
943 943
944/* 944/*
945 * Invoke the "match" routine for a cfdata entry on behalf of 945 * Invoke the "match" routine for a cfdata entry on behalf of
946 * an external caller, usually a "submatch" routine. 946 * an external caller, usually a "submatch" routine.
947 */ 947 */
948int 948int
949config_match(device_t parent, cfdata_t cf, void *aux) 949config_match(device_t parent, cfdata_t cf, void *aux)
950{ 950{
951 struct cfattach *ca; 951 struct cfattach *ca;
952 952
953 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 953 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
954 if (ca == NULL) { 954 if (ca == NULL) {
955 /* No attachment for this entry, oh well. */ 955 /* No attachment for this entry, oh well. */
956 return 0; 956 return 0;
957 } 957 }
958 958
959 return (*ca->ca_match)(parent, cf, aux); 959 return (*ca->ca_match)(parent, cf, aux);
960} 960}
961 961
962/* 962/*
963 * Iterate over all potential children of some device, calling the given 963 * Iterate over all potential children of some device, calling the given
964 * function (default being the child's match function) for each one. 964 * function (default being the child's match function) for each one.
965 * Nonzero returns are matches; the highest value returned is considered 965 * Nonzero returns are matches; the highest value returned is considered
966 * the best match. Return the `found child' if we got a match, or NULL 966 * the best match. Return the `found child' if we got a match, or NULL
967 * otherwise. The `aux' pointer is simply passed on through. 967 * otherwise. The `aux' pointer is simply passed on through.
968 * 968 *
969 * Note that this function is designed so that it can be used to apply 969 * Note that this function is designed so that it can be used to apply
970 * an arbitrary function to all potential children (its return value 970 * an arbitrary function to all potential children (its return value
971 * can be ignored). 971 * can be ignored).
972 */ 972 */
973cfdata_t 973cfdata_t
974config_search_loc(cfsubmatch_t fn, device_t parent, 974config_search_loc(cfsubmatch_t fn, device_t parent,
975 const char *ifattr, const int *locs, void *aux) 975 const char *ifattr, const int *locs, void *aux)
976{ 976{
977 struct cftable *ct; 977 struct cftable *ct;
978 cfdata_t cf; 978 cfdata_t cf;
979 struct matchinfo m; 979 struct matchinfo m;
980 980
981 KASSERT(config_initialized); 981 KASSERT(config_initialized);
982 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr)); 982 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
983 983
984 m.fn = fn; 984 m.fn = fn;
985 m.parent = parent; 985 m.parent = parent;
986 m.locs = locs; 986 m.locs = locs;
987 m.aux = aux; 987 m.aux = aux;
988 m.match = NULL; 988 m.match = NULL;
989 m.pri = 0; 989 m.pri = 0;
990 990
991 TAILQ_FOREACH(ct, &allcftables, ct_list) { 991 TAILQ_FOREACH(ct, &allcftables, ct_list) {
992 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 992 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
993 993
994 /* We don't match root nodes here. */ 994 /* We don't match root nodes here. */
995 if (!cf->cf_pspec) 995 if (!cf->cf_pspec)
996 continue; 996 continue;
997 997
998 /* 998 /*
999 * Skip cf if no longer eligible, otherwise scan 999 * Skip cf if no longer eligible, otherwise scan
1000 * through parents for one matching `parent', and 1000 * through parents for one matching `parent', and
1001 * try match function. 1001 * try match function.
1002 */ 1002 */
1003 if (cf->cf_fstate == FSTATE_FOUND) 1003 if (cf->cf_fstate == FSTATE_FOUND)
1004 continue; 1004 continue;
1005 if (cf->cf_fstate == FSTATE_DNOTFOUND || 1005 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
1006 cf->cf_fstate == FSTATE_DSTAR) 1006 cf->cf_fstate == FSTATE_DSTAR)
1007 continue; 1007 continue;
1008 1008
1009 /* 1009 /*
1010 * If an interface attribute was specified, 1010 * If an interface attribute was specified,
1011 * consider only children which attach to 1011 * consider only children which attach to
1012 * that attribute. 1012 * that attribute.
1013 */ 1013 */
1014 if (ifattr && !STREQ(ifattr, cfdata_ifattr(cf))) 1014 if (ifattr && !STREQ(ifattr, cfdata_ifattr(cf)))
1015 continue; 1015 continue;
1016 1016
1017 if (cfparent_match(parent, cf->cf_pspec)) 1017 if (cfparent_match(parent, cf->cf_pspec))
1018 mapply(&m, cf); 1018 mapply(&m, cf);
1019 } 1019 }
1020 } 1020 }
1021 return m.match; 1021 return m.match;
1022} 1022}
1023 1023
1024cfdata_t 1024cfdata_t
1025config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr, 1025config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
1026 void *aux) 1026 void *aux)
1027{ 1027{
1028 1028
1029 return config_search_loc(fn, parent, ifattr, NULL, aux); 1029 return config_search_loc(fn, parent, ifattr, NULL, aux);
1030} 1030}
1031 1031
1032/* 1032/*
1033 * Find the given root device. 1033 * Find the given root device.
1034 * This is much like config_search, but there is no parent. 1034 * This is much like config_search, but there is no parent.
1035 * Don't bother with multiple cfdata tables; the root node 1035 * Don't bother with multiple cfdata tables; the root node
1036 * must always be in the initial table. 1036 * must always be in the initial table.
1037 */ 1037 */
1038cfdata_t 1038cfdata_t
1039config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux) 1039config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
1040{ 1040{
1041 cfdata_t cf; 1041 cfdata_t cf;
1042 const short *p; 1042 const short *p;
1043 struct matchinfo m; 1043 struct matchinfo m;
1044 1044
1045 m.fn = fn; 1045 m.fn = fn;
1046 m.parent = ROOT; 1046 m.parent = ROOT;
1047 m.aux = aux; 1047 m.aux = aux;
1048 m.match = NULL; 1048 m.match = NULL;
1049 m.pri = 0; 1049 m.pri = 0;
1050 m.locs = 0; 1050 m.locs = 0;
1051 /* 1051 /*
1052 * Look at root entries for matching name. We do not bother 1052 * Look at root entries for matching name. We do not bother
1053 * with found-state here since only one root should ever be 1053 * with found-state here since only one root should ever be
1054 * searched (and it must be done first). 1054 * searched (and it must be done first).
1055 */ 1055 */
1056 for (p = cfroots; *p >= 0; p++) { 1056 for (p = cfroots; *p >= 0; p++) {
1057 cf = &cfdata[*p]; 1057 cf = &cfdata[*p];
1058 if (strcmp(cf->cf_name, rootname) == 0) 1058 if (strcmp(cf->cf_name, rootname) == 0)
1059 mapply(&m, cf); 1059 mapply(&m, cf);
1060 } 1060 }
1061 return m.match; 1061 return m.match;
1062} 1062}
1063 1063
1064static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 1064static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
1065 1065
1066/* 1066/*
1067 * The given `aux' argument describes a device that has been found 1067 * The given `aux' argument describes a device that has been found
1068 * on the given parent, but not necessarily configured. Locate the 1068 * on the given parent, but not necessarily configured. Locate the
1069 * configuration data for that device (using the submatch function 1069 * configuration data for that device (using the submatch function
1070 * provided, or using candidates' cd_match configuration driver 1070 * provided, or using candidates' cd_match configuration driver
1071 * functions) and attach it, and return its device_t. If the device was 1071 * functions) and attach it, and return its device_t. If the device was
1072 * not configured, call the given `print' function and return NULL. 1072 * not configured, call the given `print' function and return NULL.
1073 */ 1073 */
1074device_t 1074device_t
1075config_found_sm_loc(device_t parent, 1075config_found_sm_loc(device_t parent,
1076 const char *ifattr, const int *locs, void *aux, 1076 const char *ifattr, const int *locs, void *aux,
1077 cfprint_t print, cfsubmatch_t submatch) 1077 cfprint_t print, cfsubmatch_t submatch)
1078{ 1078{
1079 cfdata_t cf; 1079 cfdata_t cf;
1080 1080
1081 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux))) 1081 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
1082 return(config_attach_loc(parent, cf, locs, aux, print)); 1082 return(config_attach_loc(parent, cf, locs, aux, print));
1083 if (print) { 1083 if (print) {
1084 if (config_do_twiddle && cold) 1084 if (config_do_twiddle && cold)
1085 twiddle(); 1085 twiddle();
1086 aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]); 1086 aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]);
1087 } 1087 }
1088 1088
1089 /* 1089 /*
1090 * This has the effect of mixing in a single timestamp to the 1090 * This has the effect of mixing in a single timestamp to the
1091 * entropy pool. Experiments indicate the estimator will almost 1091 * entropy pool. Experiments indicate the estimator will almost
1092 * always attribute one bit of entropy to this sample; analysis 1092 * always attribute one bit of entropy to this sample; analysis
1093 * of device attach/detach timestamps on FreeBSD indicates 4 1093 * of device attach/detach timestamps on FreeBSD indicates 4
1094 * bits of entropy/sample so this seems appropriately conservative. 1094 * bits of entropy/sample so this seems appropriately conservative.
1095 */ 1095 */
1096 rnd_add_uint32(&rnd_autoconf_source, 0); 1096 rnd_add_uint32(&rnd_autoconf_source, 0);
1097 return NULL; 1097 return NULL;
1098} 1098}
1099 1099
1100device_t 1100device_t
1101config_found_ia(device_t parent, const char *ifattr, void *aux, 1101config_found_ia(device_t parent, const char *ifattr, void *aux,
1102 cfprint_t print) 1102 cfprint_t print)
1103{ 1103{
1104 1104
1105 return config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL); 1105 return config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL);
1106} 1106}
1107 1107
1108device_t 1108device_t
1109config_found(device_t parent, void *aux, cfprint_t print) 1109config_found(device_t parent, void *aux, cfprint_t print)
1110{ 1110{
1111 1111
1112 return config_found_sm_loc(parent, NULL, NULL, aux, print, NULL); 1112 return config_found_sm_loc(parent, NULL, NULL, aux, print, NULL);
1113} 1113}
1114 1114
1115/* 1115/*
1116 * As above, but for root devices. 1116 * As above, but for root devices.
1117 */ 1117 */
1118device_t 1118device_t
1119config_rootfound(const char *rootname, void *aux) 1119config_rootfound(const char *rootname, void *aux)
1120{ 1120{
1121 cfdata_t cf; 1121 cfdata_t cf;
1122 1122
1123 if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL) 1123 if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL)
1124 return config_attach(ROOT, cf, aux, NULL); 1124 return config_attach(ROOT, cf, aux, NULL);
1125 aprint_error("root device %s not configured\n", rootname); 1125 aprint_error("root device %s not configured\n", rootname);
1126 return NULL; 1126 return NULL;
1127} 1127}
1128 1128
1129/* just like sprintf(buf, "%d") except that it works from the end */ 1129/* just like sprintf(buf, "%d") except that it works from the end */
1130static char * 1130static char *
1131number(char *ep, int n) 1131number(char *ep, int n)
1132{ 1132{
1133 1133
1134 *--ep = 0; 1134 *--ep = 0;
1135 while (n >= 10) { 1135 while (n >= 10) {
1136 *--ep = (n % 10) + '0'; 1136 *--ep = (n % 10) + '0';
1137 n /= 10; 1137 n /= 10;
1138 } 1138 }
1139 *--ep = n + '0'; 1139 *--ep = n + '0';
1140 return ep; 1140 return ep;
1141} 1141}
1142 1142
1143/* 1143/*
1144 * Expand the size of the cd_devs array if necessary. 1144 * Expand the size of the cd_devs array if necessary.
1145 * 1145 *
1146 * The caller must hold alldevs_mtx. config_makeroom() may release and 1146 * The caller must hold alldevs_mtx. config_makeroom() may release and
1147 * re-acquire alldevs_mtx, so callers should re-check conditions such 1147 * re-acquire alldevs_mtx, so callers should re-check conditions such
1148 * as alldevs_nwrite == 0 and alldevs_nread == 0 when config_makeroom() 1148 * as alldevs_nwrite == 0 and alldevs_nread == 0 when config_makeroom()
1149 * returns. 1149 * returns.
1150 */ 1150 */
1151static void 1151static void
1152config_makeroom(int n, struct cfdriver *cd) 1152config_makeroom(int n, struct cfdriver *cd)
1153{ 1153{
1154 int ondevs, nndevs; 1154 int ondevs, nndevs;
1155 device_t *osp, *nsp; 1155 device_t *osp, *nsp;
1156 1156
1157 alldevs_nwrite++; 1157 alldevs_nwrite++;
1158 1158
1159 for (nndevs = MAX(4, cd->cd_ndevs); nndevs <= n; nndevs += nndevs) 1159 for (nndevs = MAX(4, cd->cd_ndevs); nndevs <= n; nndevs += nndevs)
1160 ; 1160 ;
1161 1161
1162 while (n >= cd->cd_ndevs) { 1162 while (n >= cd->cd_ndevs) {
1163 /* 1163 /*
1164 * Need to expand the array. 1164 * Need to expand the array.
1165 */ 1165 */
1166 ondevs = cd->cd_ndevs; 1166 ondevs = cd->cd_ndevs;
1167 osp = cd->cd_devs; 1167 osp = cd->cd_devs;
1168 1168
1169 /* Release alldevs_mtx around allocation, which may 1169 /* Release alldevs_mtx around allocation, which may
1170 * sleep. 1170 * sleep.
1171 */ 1171 */
1172 mutex_exit(&alldevs_mtx); 1172 mutex_exit(&alldevs_mtx);
1173 nsp = kmem_alloc(sizeof(device_t[nndevs]), KM_SLEEP); 1173 nsp = kmem_alloc(sizeof(device_t[nndevs]), KM_SLEEP);
1174 if (nsp == NULL) 1174 if (nsp == NULL)
1175 panic("%s: could not expand cd_devs", __func__); 1175 panic("%s: could not expand cd_devs", __func__);
1176 mutex_enter(&alldevs_mtx); 1176 mutex_enter(&alldevs_mtx);
1177 1177
1178 /* If another thread moved the array while we did 1178 /* If another thread moved the array while we did
1179 * not hold alldevs_mtx, try again. 1179 * not hold alldevs_mtx, try again.
1180 */ 1180 */
1181 if (cd->cd_devs != osp) { 1181 if (cd->cd_devs != osp) {
1182 mutex_exit(&alldevs_mtx); 1182 mutex_exit(&alldevs_mtx);
1183 kmem_free(nsp, sizeof(device_t[nndevs])); 1183 kmem_free(nsp, sizeof(device_t[nndevs]));
1184 mutex_enter(&alldevs_mtx); 1184 mutex_enter(&alldevs_mtx);
1185 continue; 1185 continue;
1186 } 1186 }
1187 1187
1188 memset(nsp + ondevs, 0, sizeof(device_t[nndevs - ondevs])); 1188 memset(nsp + ondevs, 0, sizeof(device_t[nndevs - ondevs]));
1189 if (ondevs != 0) 1189 if (ondevs != 0)
1190 memcpy(nsp, cd->cd_devs, sizeof(device_t[ondevs])); 1190 memcpy(nsp, cd->cd_devs, sizeof(device_t[ondevs]));
1191 1191
1192 cd->cd_ndevs = nndevs; 1192 cd->cd_ndevs = nndevs;
1193 cd->cd_devs = nsp; 1193 cd->cd_devs = nsp;
1194 if (ondevs != 0) { 1194 if (ondevs != 0) {
1195 mutex_exit(&alldevs_mtx); 1195 mutex_exit(&alldevs_mtx);
1196 kmem_free(osp, sizeof(device_t[ondevs])); 1196 kmem_free(osp, sizeof(device_t[ondevs]));
1197 mutex_enter(&alldevs_mtx); 1197 mutex_enter(&alldevs_mtx);
1198 } 1198 }
1199 } 1199 }
1200 alldevs_nwrite--; 1200 alldevs_nwrite--;
1201} 1201}
1202 1202
1203/* 1203/*
1204 * Put dev into the devices list. 1204 * Put dev into the devices list.
1205 */ 1205 */
1206static void 1206static void
1207config_devlink(device_t dev) 1207config_devlink(device_t dev)
1208{ 1208{
1209 int s; 1209 int s;
1210 1210
1211 s = config_alldevs_lock(); 1211 s = config_alldevs_lock();
1212 1212
1213 KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev); 1213 KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev);
1214 1214
1215 dev->dv_add_gen = alldevs_gen; 1215 dev->dv_add_gen = alldevs_gen;
1216 /* It is safe to add a device to the tail of the list while 1216 /* It is safe to add a device to the tail of the list while
1217 * readers and writers are in the list. 1217 * readers and writers are in the list.
1218 */ 1218 */
1219 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); 1219 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
1220 config_alldevs_unlock(s); 1220 config_alldevs_unlock(s);
1221} 1221}
1222 1222
1223static void 1223static void
1224config_devfree(device_t dev) 1224config_devfree(device_t dev)
1225{ 1225{
1226 int priv = (dev->dv_flags & DVF_PRIV_ALLOC); 1226 int priv = (dev->dv_flags & DVF_PRIV_ALLOC);
1227 1227
1228 if (dev->dv_cfattach->ca_devsize > 0) 1228 if (dev->dv_cfattach->ca_devsize > 0)
1229 kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize); 1229 kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize);
1230 if (priv) 1230 if (priv)
1231 kmem_free(dev, sizeof(*dev)); 1231 kmem_free(dev, sizeof(*dev));
1232} 1232}
1233 1233
1234/* 1234/*
1235 * Caller must hold alldevs_mtx. 1235 * Caller must hold alldevs_mtx.
1236 */ 1236 */
1237static void 1237static void
1238config_devunlink(device_t dev, struct devicelist *garbage) 1238config_devunlink(device_t dev, struct devicelist *garbage)
1239{ 1239{
1240 struct device_garbage *dg = &dev->dv_garbage; 1240 struct device_garbage *dg = &dev->dv_garbage;
1241 cfdriver_t cd = device_cfdriver(dev); 1241 cfdriver_t cd = device_cfdriver(dev);
1242 int i; 1242 int i;
1243 1243
1244 KASSERT(mutex_owned(&alldevs_mtx)); 1244 KASSERT(mutex_owned(&alldevs_mtx));
1245 1245
1246 /* Unlink from device list. Link to garbage list. */ 1246 /* Unlink from device list. Link to garbage list. */
1247 TAILQ_REMOVE(&alldevs, dev, dv_list); 1247 TAILQ_REMOVE(&alldevs, dev, dv_list);
1248 TAILQ_INSERT_TAIL(garbage, dev, dv_list); 1248 TAILQ_INSERT_TAIL(garbage, dev, dv_list);
1249 1249
1250 /* Remove from cfdriver's array. */ 1250 /* Remove from cfdriver's array. */
1251 cd->cd_devs[dev->dv_unit] = NULL; 1251 cd->cd_devs[dev->dv_unit] = NULL;
1252 1252
1253 /* 1253 /*
1254 * If the device now has no units in use, unlink its softc array. 1254 * If the device now has no units in use, unlink its softc array.
1255 */ 1255 */
1256 for (i = 0; i < cd->cd_ndevs; i++) { 1256 for (i = 0; i < cd->cd_ndevs; i++) {
1257 if (cd->cd_devs[i] != NULL) 1257 if (cd->cd_devs[i] != NULL)
1258 break; 1258 break;
1259 } 1259 }
1260 /* Nothing found. Unlink, now. Deallocate, later. */ 1260 /* Nothing found. Unlink, now. Deallocate, later. */
1261 if (i == cd->cd_ndevs) { 1261 if (i == cd->cd_ndevs) {
1262 dg->dg_ndevs = cd->cd_ndevs; 1262 dg->dg_ndevs = cd->cd_ndevs;
1263 dg->dg_devs = cd->cd_devs; 1263 dg->dg_devs = cd->cd_devs;
1264 cd->cd_devs = NULL; 1264 cd->cd_devs = NULL;
1265 cd->cd_ndevs = 0; 1265 cd->cd_ndevs = 0;
1266 } 1266 }
1267} 1267}
1268 1268
1269static void 1269static void
1270config_devdelete(device_t dev) 1270config_devdelete(device_t dev)
1271{ 1271{
1272 struct device_garbage *dg = &dev->dv_garbage; 1272 struct device_garbage *dg = &dev->dv_garbage;
1273 device_lock_t dvl = device_getlock(dev); 1273 device_lock_t dvl = device_getlock(dev);
1274 1274
1275 if (dg->dg_devs != NULL) 1275 if (dg->dg_devs != NULL)
1276 kmem_free(dg->dg_devs, sizeof(device_t[dg->dg_ndevs])); 1276 kmem_free(dg->dg_devs, sizeof(device_t[dg->dg_ndevs]));
1277 1277
1278 cv_destroy(&dvl->dvl_cv); 1278 cv_destroy(&dvl->dvl_cv);
1279 mutex_destroy(&dvl->dvl_mtx); 1279 mutex_destroy(&dvl->dvl_mtx);
1280 1280
1281 KASSERT(dev->dv_properties != NULL); 1281 KASSERT(dev->dv_properties != NULL);
1282 prop_object_release(dev->dv_properties); 1282 prop_object_release(dev->dv_properties);
1283 1283
1284 if (dev->dv_activity_handlers) 1284 if (dev->dv_activity_handlers)
1285 panic("%s with registered handlers", __func__); 1285 panic("%s with registered handlers", __func__);
1286 1286
1287 if (dev->dv_locators) { 1287 if (dev->dv_locators) {
1288 size_t amount = *--dev->dv_locators; 1288 size_t amount = *--dev->dv_locators;
1289 kmem_free(dev->dv_locators, amount); 1289 kmem_free(dev->dv_locators, amount);
1290 } 1290 }
1291 1291
1292 config_devfree(dev); 1292 config_devfree(dev);
1293} 1293}
1294 1294
1295static int 1295static int
1296config_unit_nextfree(cfdriver_t cd, cfdata_t cf) 1296config_unit_nextfree(cfdriver_t cd, cfdata_t cf)
1297{ 1297{
1298 int unit; 1298 int unit;
1299 1299
1300 if (cf->cf_fstate == FSTATE_STAR) { 1300 if (cf->cf_fstate == FSTATE_STAR) {
1301 for (unit = cf->cf_unit; unit < cd->cd_ndevs; unit++) 1301 for (unit = cf->cf_unit; unit < cd->cd_ndevs; unit++)
1302 if (cd->cd_devs[unit] == NULL) 1302 if (cd->cd_devs[unit] == NULL)
1303 break; 1303 break;
1304 /* 1304 /*
1305 * unit is now the unit of the first NULL device pointer, 1305 * unit is now the unit of the first NULL device pointer,
1306 * or max(cd->cd_ndevs,cf->cf_unit). 1306 * or max(cd->cd_ndevs,cf->cf_unit).
1307 */ 1307 */
1308 } else { 1308 } else {
1309 unit = cf->cf_unit; 1309 unit = cf->cf_unit;
1310 if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL) 1310 if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL)
1311 unit = -1; 1311 unit = -1;
1312 } 1312 }
1313 return unit; 1313 return unit;
1314} 1314}
1315 1315
1316static int 1316static int
1317config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf) 1317config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf)
1318{ 1318{
1319 struct alldevs_foray af; 1319 struct alldevs_foray af;
1320 int unit; 1320 int unit;
1321 1321
1322 config_alldevs_enter(&af); 1322 config_alldevs_enter(&af);
1323 for (;;) { 1323 for (;;) {
1324 unit = config_unit_nextfree(cd, cf); 1324 unit = config_unit_nextfree(cd, cf);
1325 if (unit == -1) 1325 if (unit == -1)
1326 break; 1326 break;
1327 if (unit < cd->cd_ndevs) { 1327 if (unit < cd->cd_ndevs) {
1328 cd->cd_devs[unit] = dev; 1328 cd->cd_devs[unit] = dev;
1329 dev->dv_unit = unit; 1329 dev->dv_unit = unit;
1330 break; 1330 break;
1331 } 1331 }
1332 config_makeroom(unit, cd); 1332 config_makeroom(unit, cd);
1333 } 1333 }
1334 config_alldevs_exit(&af); 1334 config_alldevs_exit(&af);
1335 1335
1336 return unit; 1336 return unit;
1337} 1337}
1338 1338
1339static device_t 1339static device_t
1340config_devalloc(const device_t parent, const cfdata_t cf, const int *locs) 1340config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
1341{ 1341{
1342 cfdriver_t cd; 1342 cfdriver_t cd;
1343 cfattach_t ca; 1343 cfattach_t ca;
1344 size_t lname, lunit; 1344 size_t lname, lunit;
1345 const char *xunit; 1345 const char *xunit;
1346 int myunit; 1346 int myunit;
1347 char num[10]; 1347 char num[10];
1348 device_t dev; 1348 device_t dev;
1349 void *dev_private; 1349 void *dev_private;
1350 const struct cfiattrdata *ia; 1350 const struct cfiattrdata *ia;
1351 device_lock_t dvl; 1351 device_lock_t dvl;
1352 1352
1353 cd = config_cfdriver_lookup(cf->cf_name); 1353 cd = config_cfdriver_lookup(cf->cf_name);
1354 if (cd == NULL) 1354 if (cd == NULL)
1355 return NULL; 1355 return NULL;
1356 1356
1357 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1357 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
1358 if (ca == NULL) 1358 if (ca == NULL)
1359 return NULL; 1359 return NULL;
1360 1360
1361 if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 && 1361 if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 &&
1362 ca->ca_devsize < sizeof(struct device)) 1362 ca->ca_devsize < sizeof(struct device))
1363 panic("config_devalloc: %s (%zu < %zu)", cf->cf_atname, 1363 panic("config_devalloc: %s (%zu < %zu)", cf->cf_atname,
1364 ca->ca_devsize, sizeof(struct device)); 1364 ca->ca_devsize, sizeof(struct device));
1365 1365
1366 /* get memory for all device vars */ 1366 /* get memory for all device vars */
1367 KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device)); 1367 KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device));
1368 if (ca->ca_devsize > 0) { 1368 if (ca->ca_devsize > 0) {
1369 dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP); 1369 dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP);
1370 if (dev_private == NULL) 1370 if (dev_private == NULL)
1371 panic("config_devalloc: memory allocation for device softc failed"); 1371 panic("config_devalloc: memory allocation for device softc failed");
1372 } else { 1372 } else {
1373 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC); 1373 KASSERT(ca->ca_flags & DVF_PRIV_ALLOC);
1374 dev_private = NULL; 1374 dev_private = NULL;
1375 } 1375 }
1376 1376
1377 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) { 1377 if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
1378 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 1378 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1379 } else { 1379 } else {
1380 dev = dev_private; 1380 dev = dev_private;
1381#ifdef DIAGNOSTIC 1381#ifdef DIAGNOSTIC
1382 printf("%s has not been converted to device_t\n", cd->cd_name); 1382 printf("%s has not been converted to device_t\n", cd->cd_name);
1383#endif 1383#endif
1384 } 1384 }
1385 if (dev == NULL) 1385 if (dev == NULL)
1386 panic("config_devalloc: memory allocation for device_t failed"); 1386 panic("config_devalloc: memory allocation for device_t failed");
1387 1387
1388 dev->dv_class = cd->cd_class; 1388 dev->dv_class = cd->cd_class;
1389 dev->dv_cfdata = cf; 1389 dev->dv_cfdata = cf;
1390 dev->dv_cfdriver = cd; 1390 dev->dv_cfdriver = cd;
1391 dev->dv_cfattach = ca; 1391 dev->dv_cfattach = ca;
1392 dev->dv_activity_count = 0; 1392 dev->dv_activity_count = 0;
1393 dev->dv_activity_handlers = NULL; 1393 dev->dv_activity_handlers = NULL;
1394 dev->dv_private = dev_private; 1394 dev->dv_private = dev_private;
1395 dev->dv_flags = ca->ca_flags; /* inherit flags from class */ 1395 dev->dv_flags = ca->ca_flags; /* inherit flags from class */
1396 1396
1397 myunit = config_unit_alloc(dev, cd, cf); 1397 myunit = config_unit_alloc(dev, cd, cf);
1398 if (myunit == -1) { 1398 if (myunit == -1) {
1399 config_devfree(dev); 1399 config_devfree(dev);
1400 return NULL; 1400 return NULL;
1401 } 1401 }
1402 1402
1403 /* compute length of name and decimal expansion of unit number */ 1403 /* compute length of name and decimal expansion of unit number */
1404 lname = strlen(cd->cd_name); 1404 lname = strlen(cd->cd_name);
1405 xunit = number(&num[sizeof(num)], myunit); 1405 xunit = number(&num[sizeof(num)], myunit);
1406 lunit = &num[sizeof(num)] - xunit; 1406 lunit = &num[sizeof(num)] - xunit;
1407 if (lname + lunit > sizeof(dev->dv_xname)) 1407 if (lname + lunit > sizeof(dev->dv_xname))
1408 panic("config_devalloc: device name too long"); 1408 panic("config_devalloc: device name too long");
1409 1409
1410 dvl = device_getlock(dev); 1410 dvl = device_getlock(dev);
1411 1411
1412 mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE); 1412 mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE);
1413 cv_init(&dvl->dvl_cv, "pmfsusp"); 1413 cv_init(&dvl->dvl_cv, "pmfsusp");
1414 1414
1415 memcpy(dev->dv_xname, cd->cd_name, lname); 1415 memcpy(dev->dv_xname, cd->cd_name, lname);
1416 memcpy(dev->dv_xname + lname, xunit, lunit); 1416 memcpy(dev->dv_xname + lname, xunit, lunit);
1417 dev->dv_parent = parent; 1417 dev->dv_parent = parent;
1418 if (parent != NULL) 1418 if (parent != NULL)
1419 dev->dv_depth = parent->dv_depth + 1; 1419 dev->dv_depth = parent->dv_depth + 1;
1420 else 1420 else
1421 dev->dv_depth = 0; 1421 dev->dv_depth = 0;
1422 dev->dv_flags |= DVF_ACTIVE; /* always initially active */ 1422 dev->dv_flags |= DVF_ACTIVE; /* always initially active */
1423 if (locs) { 1423 if (locs) {
1424 KASSERT(parent); /* no locators at root */ 1424 KASSERT(parent); /* no locators at root */
1425 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver); 1425 ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
1426 dev->dv_locators = 1426 dev->dv_locators =
1427 kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP); 1427 kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP);
1428 *dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]); 1428 *dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]);
1429 memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen])); 1429 memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen]));
1430 } 1430 }
1431 dev->dv_properties = prop_dictionary_create(); 1431 dev->dv_properties = prop_dictionary_create();
1432 KASSERT(dev->dv_properties != NULL); 1432 KASSERT(dev->dv_properties != NULL);
1433 1433
1434 prop_dictionary_set_cstring_nocopy(dev->dv_properties, 1434 prop_dictionary_set_cstring_nocopy(dev->dv_properties,
1435 "device-driver", dev->dv_cfdriver->cd_name); 1435 "device-driver", dev->dv_cfdriver->cd_name);
1436 prop_dictionary_set_uint16(dev->dv_properties, 1436 prop_dictionary_set_uint16(dev->dv_properties,
1437 "device-unit", dev->dv_unit); 1437 "device-unit", dev->dv_unit);
 1438 if (parent != NULL) {
 1439 prop_dictionary_set_cstring(dev->dv_properties,
 1440 "device-parent", device_xname(parent));
 1441 }
1438 1442
1439 if (dev->dv_cfdriver->cd_attrs != NULL) 1443 if (dev->dv_cfdriver->cd_attrs != NULL)
1440 config_add_attrib_dict(dev); 1444 config_add_attrib_dict(dev);
1441 1445
1442 return dev; 1446 return dev;
1443} 1447}
1444 1448
1445/* 1449/*
1446 * Create an array of device attach attributes and add it 1450 * Create an array of device attach attributes and add it
1447 * to the device's dv_properties dictionary. 1451 * to the device's dv_properties dictionary.
1448 * 1452 *
1449 * <key>interface-attributes</key> 1453 * <key>interface-attributes</key>
1450 * <array> 1454 * <array>
1451 * <dict> 1455 * <dict>
1452 * <key>attribute-name</key> 1456 * <key>attribute-name</key>
1453 * <string>foo</string> 1457 * <string>foo</string>
1454 * <key>locators</key> 1458 * <key>locators</key>
1455 * <array> 1459 * <array>
1456 * <dict> 1460 * <dict>
1457 * <key>loc-name</key> 1461 * <key>loc-name</key>
1458 * <string>foo-loc1</string> 1462 * <string>foo-loc1</string>
1459 * </dict> 1463 * </dict>
1460 * <dict> 1464 * <dict>
1461 * <key>loc-name</key> 1465 * <key>loc-name</key>
1462 * <string>foo-loc2</string> 1466 * <string>foo-loc2</string>
1463 * <key>default</key> 1467 * <key>default</key>
1464 * <string>foo-loc2-default</string> 1468 * <string>foo-loc2-default</string>
1465 * </dict> 1469 * </dict>
1466 * ... 1470 * ...
1467 * </array> 1471 * </array>
1468 * </dict> 1472 * </dict>
1469 * ... 1473 * ...
1470 * </array> 1474 * </array>
1471 */ 1475 */
1472 1476
1473static void 1477static void
1474config_add_attrib_dict(device_t dev) 1478config_add_attrib_dict(device_t dev)
1475{ 1479{
1476 int i, j; 1480 int i, j;
1477 const struct cfiattrdata *ci; 1481 const struct cfiattrdata *ci;
1478 prop_dictionary_t attr_dict, loc_dict; 1482 prop_dictionary_t attr_dict, loc_dict;
1479 prop_array_t attr_array, loc_array; 1483 prop_array_t attr_array, loc_array;
1480 1484
1481 if ((attr_array = prop_array_create()) == NULL) 1485 if ((attr_array = prop_array_create()) == NULL)
1482 return; 1486 return;
1483 1487
1484 for (i = 0; ; i++) { 1488 for (i = 0; ; i++) {
1485 if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL) 1489 if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL)
1486 break; 1490 break;
1487 if ((attr_dict = prop_dictionary_create()) == NULL) 1491 if ((attr_dict = prop_dictionary_create()) == NULL)
1488 break; 1492 break;
1489 prop_dictionary_set_cstring_nocopy(attr_dict, "attribute-name", 1493 prop_dictionary_set_cstring_nocopy(attr_dict, "attribute-name",
1490 ci->ci_name); 1494 ci->ci_name);
1491 1495
1492 /* Create an array of the locator names and defaults */ 1496 /* Create an array of the locator names and defaults */
1493 1497
1494 if (ci->ci_loclen != 0 && 1498 if (ci->ci_loclen != 0 &&
1495 (loc_array = prop_array_create()) != NULL) { 1499 (loc_array = prop_array_create()) != NULL) {
1496 for (j = 0; j < ci->ci_loclen; j++) { 1500 for (j = 0; j < ci->ci_loclen; j++) {
1497 loc_dict = prop_dictionary_create(); 1501 loc_dict = prop_dictionary_create();
1498 if (loc_dict == NULL) 1502 if (loc_dict == NULL)
1499 continue; 1503 continue;
1500 prop_dictionary_set_cstring_nocopy(loc_dict, 1504 prop_dictionary_set_cstring_nocopy(loc_dict,
1501 "loc-name", ci->ci_locdesc[j].cld_name); 1505 "loc-name", ci->ci_locdesc[j].cld_name);
1502 if (ci->ci_locdesc[j].cld_defaultstr != NULL) 1506 if (ci->ci_locdesc[j].cld_defaultstr != NULL)
1503 prop_dictionary_set_cstring_nocopy( 1507 prop_dictionary_set_cstring_nocopy(
1504 loc_dict, "default", 1508 loc_dict, "default",
1505 ci->ci_locdesc[j].cld_defaultstr); 1509 ci->ci_locdesc[j].cld_defaultstr);
1506 prop_array_set(loc_array, j, loc_dict); 1510 prop_array_set(loc_array, j, loc_dict);
1507 prop_object_release(loc_dict); 1511 prop_object_release(loc_dict);
1508 } 1512 }
1509 prop_dictionary_set_and_rel(attr_dict, "locators", 1513 prop_dictionary_set_and_rel(attr_dict, "locators",
1510 loc_array); 1514 loc_array);
1511 } 1515 }
1512 prop_array_add(attr_array, attr_dict); 1516 prop_array_add(attr_array, attr_dict);
1513 prop_object_release(attr_dict); 1517 prop_object_release(attr_dict);
1514 } 1518 }
1515 if (i == 0) 1519 if (i == 0)
1516 prop_object_release(attr_array); 1520 prop_object_release(attr_array);
1517 else 1521 else
1518 prop_dictionary_set_and_rel(dev->dv_properties, 1522 prop_dictionary_set_and_rel(dev->dv_properties,
1519 "interface-attributes", attr_array); 1523 "interface-attributes", attr_array);
1520 1524
1521 return; 1525 return;
1522} 1526}
1523 1527
1524/* 1528/*
1525 * Attach a found device. 1529 * Attach a found device.
1526 */ 1530 */
1527device_t 1531device_t
1528config_attach_loc(device_t parent, cfdata_t cf, 1532config_attach_loc(device_t parent, cfdata_t cf,
1529 const int *locs, void *aux, cfprint_t print) 1533 const int *locs, void *aux, cfprint_t print)
1530{ 1534{
1531 device_t dev; 1535 device_t dev;
1532 struct cftable *ct; 1536 struct cftable *ct;
1533 const char *drvname; 1537 const char *drvname;
1534 1538
1535 dev = config_devalloc(parent, cf, locs); 1539 dev = config_devalloc(parent, cf, locs);
1536 if (!dev) 1540 if (!dev)
1537 panic("config_attach: allocation of device softc failed"); 1541 panic("config_attach: allocation of device softc failed");
1538 1542
1539 /* XXX redundant - see below? */ 1543 /* XXX redundant - see below? */
1540 if (cf->cf_fstate != FSTATE_STAR) { 1544 if (cf->cf_fstate != FSTATE_STAR) {
1541 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1545 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1542 cf->cf_fstate = FSTATE_FOUND; 1546 cf->cf_fstate = FSTATE_FOUND;
1543 } 1547 }
1544 1548
1545 config_devlink(dev); 1549 config_devlink(dev);
1546 1550
1547 if (config_do_twiddle && cold) 1551 if (config_do_twiddle && cold)
1548 twiddle(); 1552 twiddle();
1549 else 1553 else
1550 aprint_naive("Found "); 1554 aprint_naive("Found ");
1551 /* 1555 /*
1552 * We want the next two printfs for normal, verbose, and quiet, 1556 * We want the next two printfs for normal, verbose, and quiet,
1553 * but not silent (in which case, we're twiddling, instead). 1557 * but not silent (in which case, we're twiddling, instead).
1554 */ 1558 */
1555 if (parent == ROOT) { 1559 if (parent == ROOT) {
1556 aprint_naive("%s (root)", device_xname(dev)); 1560 aprint_naive("%s (root)", device_xname(dev));
1557 aprint_normal("%s (root)", device_xname(dev)); 1561 aprint_normal("%s (root)", device_xname(dev));
1558 } else { 1562 } else {
1559 aprint_naive("%s at %s", device_xname(dev), device_xname(parent)); 1563 aprint_naive("%s at %s", device_xname(dev), device_xname(parent));
1560 aprint_normal("%s at %s", device_xname(dev), device_xname(parent)); 1564 aprint_normal("%s at %s", device_xname(dev), device_xname(parent));
1561 if (print) 1565 if (print)
1562 (void) (*print)(aux, NULL); 1566 (void) (*print)(aux, NULL);
1563 } 1567 }
1564 1568
1565 /* 1569 /*
1566 * Before attaching, clobber any unfound devices that are 1570 * Before attaching, clobber any unfound devices that are
1567 * otherwise identical. 1571 * otherwise identical.
1568 * XXX code above is redundant? 1572 * XXX code above is redundant?
1569 */ 1573 */
1570 drvname = dev->dv_cfdriver->cd_name; 1574 drvname = dev->dv_cfdriver->cd_name;
1571 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1575 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1572 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1576 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1573 if (STREQ(cf->cf_name, drvname) && 1577 if (STREQ(cf->cf_name, drvname) &&
1574 cf->cf_unit == dev->dv_unit) { 1578 cf->cf_unit == dev->dv_unit) {
1575 if (cf->cf_fstate == FSTATE_NOTFOUND) 1579 if (cf->cf_fstate == FSTATE_NOTFOUND)
1576 cf->cf_fstate = FSTATE_FOUND; 1580 cf->cf_fstate = FSTATE_FOUND;
1577 } 1581 }
1578 } 1582 }
1579 } 1583 }
1580 device_register(dev, aux); 1584 device_register(dev, aux);
1581 1585
1582 /* Let userland know */ 1586 /* Let userland know */
1583 devmon_report_device(dev, true); 1587 devmon_report_device(dev, true);
1584 1588
1585 (*dev->dv_cfattach->ca_attach)(parent, dev, aux); 1589 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1586 1590
1587 if (!device_pmf_is_registered(dev)) 1591 if (!device_pmf_is_registered(dev))
1588 aprint_debug_dev(dev, "WARNING: power management not supported\n"); 1592 aprint_debug_dev(dev, "WARNING: power management not supported\n");
1589 1593
1590 config_process_deferred(&deferred_config_queue, dev); 1594 config_process_deferred(&deferred_config_queue, dev);
1591 1595
1592 device_register_post_config(dev, aux); 1596 device_register_post_config(dev, aux);
1593 return dev; 1597 return dev;
1594} 1598}
1595 1599
1596device_t 1600device_t
1597config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1601config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1598{ 1602{
1599 1603
1600 return config_attach_loc(parent, cf, NULL, aux, print); 1604 return config_attach_loc(parent, cf, NULL, aux, print);
1601} 1605}
1602 1606
1603/* 1607/*
1604 * As above, but for pseudo-devices. Pseudo-devices attached in this 1608 * As above, but for pseudo-devices. Pseudo-devices attached in this
1605 * way are silently inserted into the device tree, and their children 1609 * way are silently inserted into the device tree, and their children
1606 * attached. 1610 * attached.
1607 * 1611 *
1608 * Note that because pseudo-devices are attached silently, any information 1612 * Note that because pseudo-devices are attached silently, any information
1609 * the attach routine wishes to print should be prefixed with the device 1613 * the attach routine wishes to print should be prefixed with the device
1610 * name by the attach routine. 1614 * name by the attach routine.
1611 */ 1615 */
1612device_t 1616device_t
1613config_attach_pseudo(cfdata_t cf) 1617config_attach_pseudo(cfdata_t cf)
1614{ 1618{
1615 device_t dev; 1619 device_t dev;
1616 1620
1617 dev = config_devalloc(ROOT, cf, NULL); 1621 dev = config_devalloc(ROOT, cf, NULL);
1618 if (!dev) 1622 if (!dev)
1619 return NULL; 1623 return NULL;
1620 1624
1621 /* XXX mark busy in cfdata */ 1625 /* XXX mark busy in cfdata */
1622 1626
1623 if (cf->cf_fstate != FSTATE_STAR) { 1627 if (cf->cf_fstate != FSTATE_STAR) {
1624 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 1628 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1625 cf->cf_fstate = FSTATE_FOUND; 1629 cf->cf_fstate = FSTATE_FOUND;
1626 } 1630 }
1627 1631
1628 config_devlink(dev); 1632 config_devlink(dev);
1629 1633
1630#if 0 /* XXXJRT not yet */ 1634#if 0 /* XXXJRT not yet */
1631 device_register(dev, NULL); /* like a root node */ 1635 device_register(dev, NULL); /* like a root node */
1632#endif 1636#endif
1633 1637
1634 /* Let userland know */ 1638 /* Let userland know */
1635 devmon_report_device(dev, true); 1639 devmon_report_device(dev, true);
1636 1640
1637 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL); 1641 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1638 1642
1639 config_process_deferred(&deferred_config_queue, dev); 1643 config_process_deferred(&deferred_config_queue, dev);
1640 return dev; 1644 return dev;
1641} 1645}
1642 1646
1643/* 1647/*
1644 * Caller must hold alldevs_mtx. 1648 * Caller must hold alldevs_mtx.
1645 */ 1649 */
1646static void 1650static void
1647config_collect_garbage(struct devicelist *garbage) 1651config_collect_garbage(struct devicelist *garbage)
1648{ 1652{
1649 device_t dv; 1653 device_t dv;
1650 1654
1651 KASSERT(!cpu_intr_p()); 1655 KASSERT(!cpu_intr_p());
1652 KASSERT(!cpu_softintr_p()); 1656 KASSERT(!cpu_softintr_p());
1653 KASSERT(mutex_owned(&alldevs_mtx)); 1657 KASSERT(mutex_owned(&alldevs_mtx));
1654 1658
1655 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) { 1659 while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) {
1656 TAILQ_FOREACH(dv, &alldevs, dv_list) { 1660 TAILQ_FOREACH(dv, &alldevs, dv_list) {
1657 if (dv->dv_del_gen != 0) 1661 if (dv->dv_del_gen != 0)
1658 break; 1662 break;
1659 } 1663 }
1660 if (dv == NULL) { 1664 if (dv == NULL) {
1661 alldevs_garbage = false; 1665 alldevs_garbage = false;
1662 break; 1666 break;
1663 } 1667 }
1664 config_devunlink(dv, garbage); 1668 config_devunlink(dv, garbage);
1665 } 1669 }
1666 KASSERT(mutex_owned(&alldevs_mtx)); 1670 KASSERT(mutex_owned(&alldevs_mtx));
1667} 1671}
1668 1672
1669static void 1673static void
1670config_dump_garbage(struct devicelist *garbage) 1674config_dump_garbage(struct devicelist *garbage)
1671{ 1675{
1672 device_t dv; 1676 device_t dv;
1673 1677
1674 while ((dv = TAILQ_FIRST(garbage)) != NULL) { 1678 while ((dv = TAILQ_FIRST(garbage)) != NULL) {
1675 TAILQ_REMOVE(garbage, dv, dv_list); 1679 TAILQ_REMOVE(garbage, dv, dv_list);
1676 config_devdelete(dv); 1680 config_devdelete(dv);
1677 } 1681 }
1678} 1682}
1679 1683
1680/* 1684/*
1681 * Detach a device. Optionally forced (e.g. because of hardware 1685 * Detach a device. Optionally forced (e.g. because of hardware
1682 * removal) and quiet. Returns zero if successful, non-zero 1686 * removal) and quiet. Returns zero if successful, non-zero
1683 * (an error code) otherwise. 1687 * (an error code) otherwise.
1684 * 1688 *
1685 * Note that this code wants to be run from a process context, so 1689 * Note that this code wants to be run from a process context, so
1686 * that the detach can sleep to allow processes which have a device 1690 * that the detach can sleep to allow processes which have a device
1687 * open to run and unwind their stacks. 1691 * open to run and unwind their stacks.
1688 */ 1692 */
1689int 1693int
1690config_detach(device_t dev, int flags) 1694config_detach(device_t dev, int flags)
1691{ 1695{
1692 struct alldevs_foray af; 1696 struct alldevs_foray af;
1693 struct cftable *ct; 1697 struct cftable *ct;
1694 cfdata_t cf; 1698 cfdata_t cf;
1695 const struct cfattach *ca; 1699 const struct cfattach *ca;
1696 struct cfdriver *cd; 1700 struct cfdriver *cd;
1697#ifdef DIAGNOSTIC 1701#ifdef DIAGNOSTIC
1698 device_t d; 1702 device_t d;
1699#endif 1703#endif
1700 int rv = 0, s; 1704 int rv = 0, s;
1701 1705
1702#ifdef DIAGNOSTIC 1706#ifdef DIAGNOSTIC
1703 cf = dev->dv_cfdata; 1707 cf = dev->dv_cfdata;
1704 if (cf != NULL && cf->cf_fstate != FSTATE_FOUND && 1708 if (cf != NULL && cf->cf_fstate != FSTATE_FOUND &&
1705 cf->cf_fstate != FSTATE_STAR) 1709 cf->cf_fstate != FSTATE_STAR)
1706 panic("config_detach: %s: bad device fstate %d", 1710 panic("config_detach: %s: bad device fstate %d",
1707 device_xname(dev), cf ? cf->cf_fstate : -1); 1711 device_xname(dev), cf ? cf->cf_fstate : -1);
1708#endif 1712#endif
1709 cd = dev->dv_cfdriver; 1713 cd = dev->dv_cfdriver;
1710 KASSERT(cd != NULL); 1714 KASSERT(cd != NULL);
1711 1715
1712 ca = dev->dv_cfattach; 1716 ca = dev->dv_cfattach;
1713 KASSERT(ca != NULL); 1717 KASSERT(ca != NULL);
1714 1718
1715 s = config_alldevs_lock(); 1719 s = config_alldevs_lock();
1716 if (dev->dv_del_gen != 0) { 1720 if (dev->dv_del_gen != 0) {
1717 config_alldevs_unlock(s); 1721 config_alldevs_unlock(s);
1718#ifdef DIAGNOSTIC 1722#ifdef DIAGNOSTIC
1719 printf("%s: %s is already detached\n", __func__, 1723 printf("%s: %s is already detached\n", __func__,
1720 device_xname(dev)); 1724 device_xname(dev));
1721#endif /* DIAGNOSTIC */ 1725#endif /* DIAGNOSTIC */
1722 return ENOENT; 1726 return ENOENT;
1723 } 1727 }
1724 alldevs_nwrite++; 1728 alldevs_nwrite++;
1725 config_alldevs_unlock(s); 1729 config_alldevs_unlock(s);
1726 1730
1727 if (!detachall && 1731 if (!detachall &&
1728 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN && 1732 (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN &&
1729 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) { 1733 (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) {
1730 rv = EOPNOTSUPP; 1734 rv = EOPNOTSUPP;
1731 } else if (ca->ca_detach != NULL) { 1735 } else if (ca->ca_detach != NULL) {
1732 rv = (*ca->ca_detach)(dev, flags); 1736 rv = (*ca->ca_detach)(dev, flags);
1733 } else 1737 } else
1734 rv = EOPNOTSUPP; 1738 rv = EOPNOTSUPP;
1735 1739
1736 /* 1740 /*
1737 * If it was not possible to detach the device, then we either 1741 * If it was not possible to detach the device, then we either
1738 * panic() (for the forced but failed case), or return an error. 1742 * panic() (for the forced but failed case), or return an error.
1739 * 1743 *
1740 * If it was possible to detach the device, ensure that the 1744 * If it was possible to detach the device, ensure that the
1741 * device is deactivated. 1745 * device is deactivated.
1742 */ 1746 */
1743 if (rv == 0) 1747 if (rv == 0)
1744 dev->dv_flags &= ~DVF_ACTIVE; 1748 dev->dv_flags &= ~DVF_ACTIVE;
1745 else if ((flags & DETACH_FORCE) == 0) 1749 else if ((flags & DETACH_FORCE) == 0)
1746 goto out; 1750 goto out;
1747 else { 1751 else {
1748 panic("config_detach: forced detach of %s failed (%d)", 1752 panic("config_detach: forced detach of %s failed (%d)",
1749 device_xname(dev), rv); 1753 device_xname(dev), rv);
1750 } 1754 }
1751 1755
1752 /* 1756 /*
1753 * The device has now been successfully detached. 1757 * The device has now been successfully detached.
1754 */ 1758 */
1755 1759
1756 /* Let userland know */ 1760 /* Let userland know */
1757 devmon_report_device(dev, false); 1761 devmon_report_device(dev, false);
1758 1762
1759#ifdef DIAGNOSTIC 1763#ifdef DIAGNOSTIC
1760 /* 1764 /*
1761 * Sanity: If you're successfully detached, you should have no 1765 * Sanity: If you're successfully detached, you should have no
1762 * children. (Note that because children must be attached 1766 * children. (Note that because children must be attached
1763 * after parents, we only need to search the latter part of 1767 * after parents, we only need to search the latter part of
1764 * the list.) 1768 * the list.)
1765 */ 1769 */
1766 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1770 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1767 d = TAILQ_NEXT(d, dv_list)) { 1771 d = TAILQ_NEXT(d, dv_list)) {
1768 if (d->dv_parent == dev && d->dv_del_gen == 0) { 1772 if (d->dv_parent == dev && d->dv_del_gen == 0) {
1769 printf("config_detach: detached device %s" 1773 printf("config_detach: detached device %s"
1770 " has children %s\n", device_xname(dev), device_xname(d)); 1774 " has children %s\n", device_xname(dev), device_xname(d));
1771 panic("config_detach"); 1775 panic("config_detach");
1772 } 1776 }
1773 } 1777 }
1774#endif 1778#endif
1775 1779
1776 /* notify the parent that the child is gone */ 1780 /* notify the parent that the child is gone */
1777 if (dev->dv_parent) { 1781 if (dev->dv_parent) {
1778 device_t p = dev->dv_parent; 1782 device_t p = dev->dv_parent;
1779 if (p->dv_cfattach->ca_childdetached) 1783 if (p->dv_cfattach->ca_childdetached)
1780 (*p->dv_cfattach->ca_childdetached)(p, dev); 1784 (*p->dv_cfattach->ca_childdetached)(p, dev);
1781 } 1785 }
1782 1786
1783 /* 1787 /*
1784 * Mark cfdata to show that the unit can be reused, if possible. 1788 * Mark cfdata to show that the unit can be reused, if possible.
1785 */ 1789 */
1786 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1790 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1787 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1791 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1788 if (STREQ(cf->cf_name, cd->cd_name)) { 1792 if (STREQ(cf->cf_name, cd->cd_name)) {
1789 if (cf->cf_fstate == FSTATE_FOUND && 1793 if (cf->cf_fstate == FSTATE_FOUND &&
1790 cf->cf_unit == dev->dv_unit) 1794 cf->cf_unit == dev->dv_unit)
1791 cf->cf_fstate = FSTATE_NOTFOUND; 1795 cf->cf_fstate = FSTATE_NOTFOUND;
1792 } 1796 }
1793 } 1797 }
1794 } 1798 }
1795 1799
1796 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1800 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1797 aprint_normal_dev(dev, "detached\n"); 1801 aprint_normal_dev(dev, "detached\n");
1798 1802
1799out: 1803out:
1800 config_alldevs_enter(&af); 1804 config_alldevs_enter(&af);
1801 KASSERT(alldevs_nwrite != 0); 1805 KASSERT(alldevs_nwrite != 0);
1802 --alldevs_nwrite; 1806 --alldevs_nwrite;
1803 if (rv == 0 && dev->dv_del_gen == 0) { 1807 if (rv == 0 && dev->dv_del_gen == 0) {
1804 if (alldevs_nwrite == 0 && alldevs_nread == 0) 1808 if (alldevs_nwrite == 0 && alldevs_nread == 0)
1805 config_devunlink(dev, &af.af_garbage); 1809 config_devunlink(dev, &af.af_garbage);
1806 else { 1810 else {
1807 dev->dv_del_gen = alldevs_gen; 1811 dev->dv_del_gen = alldevs_gen;
1808 alldevs_garbage = true; 1812 alldevs_garbage = true;
1809 } 1813 }
1810 } 1814 }
1811 config_alldevs_exit(&af); 1815 config_alldevs_exit(&af);
1812 1816
1813 return rv; 1817 return rv;
1814} 1818}
1815 1819
1816int 1820int
1817config_detach_children(device_t parent, int flags) 1821config_detach_children(device_t parent, int flags)
1818{ 1822{
1819 device_t dv; 1823 device_t dv;
1820 deviter_t di; 1824 deviter_t di;
1821 int error = 0; 1825 int error = 0;
1822 1826
1823 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL; 1827 for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
1824 dv = deviter_next(&di)) { 1828 dv = deviter_next(&di)) {
1825 if (device_parent(dv) != parent) 1829 if (device_parent(dv) != parent)
1826 continue; 1830 continue;
1827 if ((error = config_detach(dv, flags)) != 0) 1831 if ((error = config_detach(dv, flags)) != 0)
1828 break; 1832 break;
1829 } 1833 }
1830 deviter_release(&di); 1834 deviter_release(&di);
1831 return error; 1835 return error;
1832} 1836}
1833 1837
1834device_t 1838device_t
1835shutdown_first(struct shutdown_state *s) 1839shutdown_first(struct shutdown_state *s)
1836{ 1840{
1837 if (!s->initialized) { 1841 if (!s->initialized) {
1838 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST); 1842 deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
1839 s->initialized = true; 1843 s->initialized = true;
1840 } 1844 }
1841 return shutdown_next(s); 1845 return shutdown_next(s);
1842} 1846}
1843 1847
1844device_t 1848device_t
1845shutdown_next(struct shutdown_state *s) 1849shutdown_next(struct shutdown_state *s)
1846{ 1850{
1847 device_t dv; 1851 device_t dv;
1848 1852
1849 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv)) 1853 while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
1850 ; 1854 ;
1851 1855
1852 if (dv == NULL) 1856 if (dv == NULL)
1853 s->initialized = false; 1857 s->initialized = false;
1854 1858
1855 return dv; 1859 return dv;
1856} 1860}
1857 1861
1858bool 1862bool
1859config_detach_all(int how) 1863config_detach_all(int how)
1860{ 1864{
1861 static struct shutdown_state s; 1865 static struct shutdown_state s;
1862 device_t curdev; 1866 device_t curdev;
1863 bool progress = false; 1867 bool progress = false;
1864 1868
1865 if ((how & RB_NOSYNC) != 0) 1869 if ((how & RB_NOSYNC) != 0)
1866 return false; 1870 return false;
1867 1871
1868 for (curdev = shutdown_first(&s); curdev != NULL; 1872 for (curdev = shutdown_first(&s); curdev != NULL;
1869 curdev = shutdown_next(&s)) { 1873 curdev = shutdown_next(&s)) {
1870 aprint_debug(" detaching %s, ", device_xname(curdev)); 1874 aprint_debug(" detaching %s, ", device_xname(curdev));
1871 if (config_detach(curdev, DETACH_SHUTDOWN) == 0) { 1875 if (config_detach(curdev, DETACH_SHUTDOWN) == 0) {
1872 progress = true; 1876 progress = true;
1873 aprint_debug("success."); 1877 aprint_debug("success.");
1874 } else 1878 } else
1875 aprint_debug("failed."); 1879 aprint_debug("failed.");
1876 } 1880 }
1877 return progress; 1881 return progress;
1878} 1882}
1879 1883
1880static bool 1884static bool
1881device_is_ancestor_of(device_t ancestor, device_t descendant) 1885device_is_ancestor_of(device_t ancestor, device_t descendant)
1882{ 1886{
1883 device_t dv; 1887 device_t dv;
1884 1888
1885 for (dv = descendant; dv != NULL; dv = device_parent(dv)) { 1889 for (dv = descendant; dv != NULL; dv = device_parent(dv)) {
1886 if (device_parent(dv) == ancestor) 1890 if (device_parent(dv) == ancestor)
1887 return true; 1891 return true;
1888 } 1892 }
1889 return false; 1893 return false;
1890} 1894}
1891 1895
1892int 1896int
1893config_deactivate(device_t dev) 1897config_deactivate(device_t dev)
1894{ 1898{
1895 deviter_t di; 1899 deviter_t di;
1896 const struct cfattach *ca; 1900 const struct cfattach *ca;
1897 device_t descendant; 1901 device_t descendant;
1898 int s, rv = 0, oflags; 1902 int s, rv = 0, oflags;
1899 1903
1900 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST); 1904 for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST);
1901 descendant != NULL; 1905 descendant != NULL;
1902 descendant = deviter_next(&di)) { 1906 descendant = deviter_next(&di)) {
1903 if (dev != descendant && 1907 if (dev != descendant &&
1904 !device_is_ancestor_of(dev, descendant)) 1908 !device_is_ancestor_of(dev, descendant))
1905 continue; 1909 continue;
1906 1910
1907 if ((descendant->dv_flags & DVF_ACTIVE) == 0) 1911 if ((descendant->dv_flags & DVF_ACTIVE) == 0)
1908 continue; 1912 continue;
1909 1913
1910 ca = descendant->dv_cfattach; 1914 ca = descendant->dv_cfattach;
1911 oflags = descendant->dv_flags; 1915 oflags = descendant->dv_flags;
1912 1916
1913 descendant->dv_flags &= ~DVF_ACTIVE; 1917 descendant->dv_flags &= ~DVF_ACTIVE;
1914 if (ca->ca_activate == NULL) 1918 if (ca->ca_activate == NULL)
1915 continue; 1919 continue;
1916 s = splhigh(); 1920 s = splhigh();
1917 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE); 1921 rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE);
1918 splx(s); 1922 splx(s);
1919 if (rv != 0) 1923 if (rv != 0)
1920 descendant->dv_flags = oflags; 1924 descendant->dv_flags = oflags;
1921 } 1925 }
1922 deviter_release(&di); 1926 deviter_release(&di);
1923 return rv; 1927 return rv;
1924} 1928}
1925 1929
1926/* 1930/*
1927 * Defer the configuration of the specified device until all 1931 * Defer the configuration of the specified device until all
1928 * of its parent's devices have been attached. 1932 * of its parent's devices have been attached.
1929 */ 1933 */
1930void 1934void
1931config_defer(device_t dev, void (*func)(device_t)) 1935config_defer(device_t dev, void (*func)(device_t))
1932{ 1936{
1933 struct deferred_config *dc; 1937 struct deferred_config *dc;
1934 1938
1935 if (dev->dv_parent == NULL) 1939 if (dev->dv_parent == NULL)
1936 panic("config_defer: can't defer config of a root device"); 1940 panic("config_defer: can't defer config of a root device");
1937 1941
1938#ifdef DIAGNOSTIC 1942#ifdef DIAGNOSTIC
1939 TAILQ_FOREACH(dc, &deferred_config_queue, dc_queue) { 1943 TAILQ_FOREACH(dc, &deferred_config_queue, dc_queue) {
1940 if (dc->dc_dev == dev) 1944 if (dc->dc_dev == dev)
1941 panic("config_defer: deferred twice"); 1945 panic("config_defer: deferred twice");
1942 } 1946 }
1943#endif 1947#endif
1944 1948
1945 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 1949 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1946 if (dc == NULL) 1950 if (dc == NULL)
1947 panic("config_defer: unable to allocate callback"); 1951 panic("config_defer: unable to allocate callback");
1948 1952
1949 dc->dc_dev = dev; 1953 dc->dc_dev = dev;
1950 dc->dc_func = func; 1954 dc->dc_func = func;
1951 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1955 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1952 config_pending_incr(dev); 1956 config_pending_incr(dev);
1953} 1957}
1954 1958
1955/* 1959/*
1956 * Defer some autoconfiguration for a device until after interrupts 1960 * Defer some autoconfiguration for a device until after interrupts
1957 * are enabled. 1961 * are enabled.
1958 */ 1962 */
1959void 1963void
1960config_interrupts(device_t dev, void (*func)(device_t)) 1964config_interrupts(device_t dev, void (*func)(device_t))
1961{ 1965{
1962 struct deferred_config *dc; 1966 struct deferred_config *dc;
1963 1967
1964 /* 1968 /*
1965 * If interrupts are enabled, callback now. 1969 * If interrupts are enabled, callback now.
1966 */ 1970 */
1967 if (cold == 0) { 1971 if (cold == 0) {
1968 (*func)(dev); 1972 (*func)(dev);
1969 return; 1973 return;
1970 } 1974 }
1971 1975
1972#ifdef DIAGNOSTIC 1976#ifdef DIAGNOSTIC
1973 TAILQ_FOREACH(dc, &interrupt_config_queue, dc_queue) { 1977 TAILQ_FOREACH(dc, &interrupt_config_queue, dc_queue) {
1974 if (dc->dc_dev == dev) 1978 if (dc->dc_dev == dev)
1975 panic("config_interrupts: deferred twice"); 1979 panic("config_interrupts: deferred twice");
1976 } 1980 }
1977#endif 1981#endif
1978 1982
1979 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 1983 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
1980 if (dc == NULL) 1984 if (dc == NULL)
1981 panic("config_interrupts: unable to allocate callback"); 1985 panic("config_interrupts: unable to allocate callback");
1982 1986
1983 dc->dc_dev = dev; 1987 dc->dc_dev = dev;
1984 dc->dc_func = func; 1988 dc->dc_func = func;
1985 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1989 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1986 config_pending_incr(dev); 1990 config_pending_incr(dev);
1987} 1991}
1988 1992
1989/* 1993/*
1990 * Defer some autoconfiguration for a device until after root file system 1994 * Defer some autoconfiguration for a device until after root file system
1991 * is mounted (to load firmware etc). 1995 * is mounted (to load firmware etc).
1992 */ 1996 */
1993void 1997void
1994config_mountroot(device_t dev, void (*func)(device_t)) 1998config_mountroot(device_t dev, void (*func)(device_t))
1995{ 1999{
1996 struct deferred_config *dc; 2000 struct deferred_config *dc;
1997 2001
1998 /* 2002 /*
1999 * If root file system is mounted, callback now. 2003 * If root file system is mounted, callback now.
2000 */ 2004 */
2001 if (root_is_mounted) { 2005 if (root_is_mounted) {
2002 (*func)(dev); 2006 (*func)(dev);
2003 return; 2007 return;
2004 } 2008 }
2005 2009
2006#ifdef DIAGNOSTIC 2010#ifdef DIAGNOSTIC
2007 TAILQ_FOREACH(dc, &mountroot_config_queue, dc_queue) { 2011 TAILQ_FOREACH(dc, &mountroot_config_queue, dc_queue) {
2008 if (dc->dc_dev == dev) 2012 if (dc->dc_dev == dev)
2009 panic("%s: deferred twice", __func__); 2013 panic("%s: deferred twice", __func__);
2010 } 2014 }
2011#endif 2015#endif
2012 2016
2013 dc = kmem_alloc(sizeof(*dc), KM_SLEEP); 2017 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
2014 if (dc == NULL) 2018 if (dc == NULL)
2015 panic("%s: unable to allocate callback", __func__); 2019 panic("%s: unable to allocate callback", __func__);
2016 2020
2017 dc->dc_dev = dev; 2021 dc->dc_dev = dev;
2018 dc->dc_func = func; 2022 dc->dc_func = func;
2019 TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue); 2023 TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue);
2020} 2024}
2021 2025
2022/* 2026/*
2023 * Process a deferred configuration queue. 2027 * Process a deferred configuration queue.
2024 */ 2028 */
2025static void 2029static void
2026config_process_deferred(struct deferred_config_head *queue, 2030config_process_deferred(struct deferred_config_head *queue,
2027 device_t parent) 2031 device_t parent)
2028{ 2032{
2029 struct deferred_config *dc, *ndc; 2033 struct deferred_config *dc, *ndc;
2030 2034
2031 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 2035 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
2032 ndc = TAILQ_NEXT(dc, dc_queue); 2036 ndc = TAILQ_NEXT(dc, dc_queue);
2033 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 2037 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
2034 TAILQ_REMOVE(queue, dc, dc_queue); 2038 TAILQ_REMOVE(queue, dc, dc_queue);
2035 (*dc->dc_func)(dc->dc_dev); 2039 (*dc->dc_func)(dc->dc_dev);
2036 config_pending_decr(dc->dc_dev); 2040 config_pending_decr(dc->dc_dev);
2037 kmem_free(dc, sizeof(*dc)); 2041 kmem_free(dc, sizeof(*dc));
2038 } 2042 }
2039 } 2043 }
2040} 2044}
2041 2045
2042/* 2046/*
2043 * Manipulate the config_pending semaphore. 2047 * Manipulate the config_pending semaphore.
2044 */ 2048 */
2045void 2049void
2046config_pending_incr(device_t dev) 2050config_pending_incr(device_t dev)
2047{ 2051{
2048 2052
2049 mutex_enter(&config_misc_lock); 2053 mutex_enter(&config_misc_lock);
2050 config_pending++; 2054 config_pending++;
2051#ifdef DEBUG_AUTOCONF 2055#ifdef DEBUG_AUTOCONF
2052 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending); 2056 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending);
2053#endif 2057#endif
2054 mutex_exit(&config_misc_lock); 2058 mutex_exit(&config_misc_lock);
2055} 2059}
2056 2060
2057void 2061void
2058config_pending_decr(device_t dev) 2062config_pending_decr(device_t dev)
2059{ 2063{
2060 2064
2061#ifdef DIAGNOSTIC 2065#ifdef DIAGNOSTIC
2062 if (config_pending == 0) 2066 if (config_pending == 0)
2063 panic("config_pending_decr: config_pending == 0"); 2067 panic("config_pending_decr: config_pending == 0");
2064#endif 2068#endif
2065 mutex_enter(&config_misc_lock); 2069 mutex_enter(&config_misc_lock);
2066 config_pending--; 2070 config_pending--;
2067#ifdef DEBUG_AUTOCONF 2071#ifdef DEBUG_AUTOCONF
2068 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending); 2072 printf("%s: %s %d\n", __func__, device_xname(dev), config_pending);
2069#endif 2073#endif
2070 if (config_pending == 0) 2074 if (config_pending == 0)
2071 cv_broadcast(&config_misc_cv); 2075 cv_broadcast(&config_misc_cv);
2072 mutex_exit(&config_misc_lock); 2076 mutex_exit(&config_misc_lock);
2073} 2077}
2074 2078
2075/* 2079/*
2076 * Register a "finalization" routine. Finalization routines are 2080 * Register a "finalization" routine. Finalization routines are
2077 * called iteratively once all real devices have been found during 2081 * called iteratively once all real devices have been found during
2078 * autoconfiguration, for as long as any one finalizer has done 2082 * autoconfiguration, for as long as any one finalizer has done
2079 * any work. 2083 * any work.
2080 */ 2084 */
2081int 2085int
2082config_finalize_register(device_t dev, int (*fn)(device_t)) 2086config_finalize_register(device_t dev, int (*fn)(device_t))
2083{ 2087{
2084 struct finalize_hook *f; 2088 struct finalize_hook *f;
2085 2089
2086 /* 2090 /*
2087 * If finalization has already been done, invoke the 2091 * If finalization has already been done, invoke the
2088 * callback function now. 2092 * callback function now.
2089 */ 2093 */
2090 if (config_finalize_done) { 2094 if (config_finalize_done) {
2091 while ((*fn)(dev) != 0) 2095 while ((*fn)(dev) != 0)
2092 /* loop */ ; 2096 /* loop */ ;
2093 } 2097 }
2094 2098
2095 /* Ensure this isn't already on the list. */ 2099 /* Ensure this isn't already on the list. */
2096 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 2100 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
2097 if (f->f_func == fn && f->f_dev == dev) 2101 if (f->f_func == fn && f->f_dev == dev)
2098 return EEXIST; 2102 return EEXIST;
2099 } 2103 }
2100 2104
2101 f = kmem_alloc(sizeof(*f), KM_SLEEP); 2105 f = kmem_alloc(sizeof(*f), KM_SLEEP);
2102 f->f_func = fn; 2106 f->f_func = fn;
2103 f->f_dev = dev; 2107 f->f_dev = dev;
2104 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 2108 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
2105 2109
2106 return 0; 2110 return 0;
2107} 2111}
2108 2112
2109void 2113void
2110config_finalize(void) 2114config_finalize(void)
2111{ 2115{
2112 struct finalize_hook *f; 2116 struct finalize_hook *f;
2113 struct pdevinit *pdev; 2117 struct pdevinit *pdev;
2114 extern struct pdevinit pdevinit[]; 2118 extern struct pdevinit pdevinit[];
2115 int errcnt, rv; 2119 int errcnt, rv;
2116 2120
2117 /* 2121 /*
2118 * Now that device driver threads have been created, wait for 2122 * Now that device driver threads have been created, wait for
2119 * them to finish any deferred autoconfiguration. 2123 * them to finish any deferred autoconfiguration.
2120 */ 2124 */
2121 mutex_enter(&config_misc_lock); 2125 mutex_enter(&config_misc_lock);
2122 while (config_pending != 0) 2126 while (config_pending != 0)
2123 cv_wait(&config_misc_cv, &config_misc_lock); 2127 cv_wait(&config_misc_cv, &config_misc_lock);
2124 mutex_exit(&config_misc_lock); 2128 mutex_exit(&config_misc_lock);
2125 2129
2126 KERNEL_LOCK(1, NULL); 2130 KERNEL_LOCK(1, NULL);
2127 2131
2128 /* Attach pseudo-devices. */ 2132 /* Attach pseudo-devices. */
2129 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 2133 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
2130 (*pdev->pdev_attach)(pdev->pdev_count); 2134 (*pdev->pdev_attach)(pdev->pdev_count);
2131 2135
2132 /* Run the hooks until none of them does any work. */ 2136 /* Run the hooks until none of them does any work. */
2133 do { 2137 do {
2134 rv = 0; 2138 rv = 0;
2135 TAILQ_FOREACH(f, &config_finalize_list, f_list) 2139 TAILQ_FOREACH(f, &config_finalize_list, f_list)
2136 rv |= (*f->f_func)(f->f_dev); 2140 rv |= (*f->f_func)(f->f_dev);
2137 } while (rv != 0); 2141 } while (rv != 0);
2138 2142
2139 config_finalize_done = 1; 2143 config_finalize_done = 1;
2140 2144
2141 /* Now free all the hooks. */ 2145 /* Now free all the hooks. */
2142 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 2146 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
2143 TAILQ_REMOVE(&config_finalize_list, f, f_list); 2147 TAILQ_REMOVE(&config_finalize_list, f, f_list);
2144 kmem_free(f, sizeof(*f)); 2148 kmem_free(f, sizeof(*f));
2145 } 2149 }
2146 2150
2147 KERNEL_UNLOCK_ONE(NULL); 2151 KERNEL_UNLOCK_ONE(NULL);
2148 2152
2149 errcnt = aprint_get_error_count(); 2153 errcnt = aprint_get_error_count();
2150 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 2154 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
2151 (boothowto & AB_VERBOSE) == 0) { 2155 (boothowto & AB_VERBOSE) == 0) {
2152 mutex_enter(&config_misc_lock); 2156 mutex_enter(&config_misc_lock);
2153 if (config_do_twiddle) { 2157 if (config_do_twiddle) {
2154 config_do_twiddle = 0; 2158 config_do_twiddle = 0;
2155 printf_nolog(" done.\n"); 2159 printf_nolog(" done.\n");
2156 } 2160 }
2157 mutex_exit(&config_misc_lock); 2161 mutex_exit(&config_misc_lock);
2158 if (errcnt != 0) { 2162 if (errcnt != 0) {
2159 printf("WARNING: %d error%s while detecting hardware; " 2163 printf("WARNING: %d error%s while detecting hardware; "
2160 "check system log.\n", errcnt, 2164 "check system log.\n", errcnt,
2161 errcnt == 1 ? "" : "s"); 2165 errcnt == 1 ? "" : "s");
2162 } 2166 }
2163 } 2167 }
2164} 2168}
2165 2169
2166void 2170void
2167config_twiddle_init(void) 2171config_twiddle_init(void)
2168{ 2172{
2169 2173
2170 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 2174 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
2171 config_do_twiddle = 1; 2175 config_do_twiddle = 1;
2172 } 2176 }
2173 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL); 2177 callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
2174} 2178}
2175 2179
2176void 2180void
2177config_twiddle_fn(void *cookie) 2181config_twiddle_fn(void *cookie)
2178{ 2182{
2179 2183
2180 mutex_enter(&config_misc_lock); 2184 mutex_enter(&config_misc_lock);
2181 if (config_do_twiddle) { 2185 if (config_do_twiddle) {
2182 twiddle(); 2186 twiddle();
2183 callout_schedule(&config_twiddle_ch, mstohz(100)); 2187 callout_schedule(&config_twiddle_ch, mstohz(100));
2184 } 2188 }
2185 mutex_exit(&config_misc_lock); 2189 mutex_exit(&config_misc_lock);
2186} 2190}
2187 2191
2188static int 2192static int
2189config_alldevs_lock(void) 2193config_alldevs_lock(void)
2190{ 2194{
2191 mutex_enter(&alldevs_mtx); 2195 mutex_enter(&alldevs_mtx);
2192 return 0; 2196 return 0;
2193} 2197}
2194 2198
2195static void 2199static void
2196config_alldevs_enter(struct alldevs_foray *af) 2200config_alldevs_enter(struct alldevs_foray *af)
2197{ 2201{
2198 TAILQ_INIT(&af->af_garbage); 2202 TAILQ_INIT(&af->af_garbage);
2199 af->af_s = config_alldevs_lock(); 2203 af->af_s = config_alldevs_lock();
2200 config_collect_garbage(&af->af_garbage); 2204 config_collect_garbage(&af->af_garbage);
2201}  2205}
2202 2206
2203static void 2207static void
2204config_alldevs_exit(struct alldevs_foray *af) 2208config_alldevs_exit(struct alldevs_foray *af)
2205{ 2209{
2206 config_alldevs_unlock(af->af_s); 2210 config_alldevs_unlock(af->af_s);
2207 config_dump_garbage(&af->af_garbage); 2211 config_dump_garbage(&af->af_garbage);
2208} 2212}
2209 2213
2210/*ARGSUSED*/ 2214/*ARGSUSED*/
2211static void 2215static void
2212config_alldevs_unlock(int s) 2216config_alldevs_unlock(int s)
2213{ 2217{
2214 mutex_exit(&alldevs_mtx); 2218 mutex_exit(&alldevs_mtx);
2215} 2219}
2216 2220
2217/* 2221/*
2218 * device_lookup: 2222 * device_lookup:
2219 * 2223 *
2220 * Look up a device instance for a given driver. 2224 * Look up a device instance for a given driver.
2221 */ 2225 */
2222device_t 2226device_t
2223device_lookup(cfdriver_t cd, int unit) 2227device_lookup(cfdriver_t cd, int unit)
2224{ 2228{
2225 device_t dv; 2229 device_t dv;
2226 int s; 2230 int s;
2227 2231
2228 s = config_alldevs_lock(); 2232 s = config_alldevs_lock();
2229 KASSERT(mutex_owned(&alldevs_mtx)); 2233 KASSERT(mutex_owned(&alldevs_mtx));
2230 if (unit < 0 || unit >= cd->cd_ndevs) 2234 if (unit < 0 || unit >= cd->cd_ndevs)
2231 dv = NULL; 2235 dv = NULL;
2232 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0) 2236 else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
2233 dv = NULL; 2237 dv = NULL;
2234 config_alldevs_unlock(s); 2238 config_alldevs_unlock(s);
2235 2239
2236 return dv; 2240 return dv;
2237} 2241}
2238 2242
2239/* 2243/*
2240 * device_lookup_private: 2244 * device_lookup_private:
2241 * 2245 *
2242 * Look up a softc instance for a given driver. 2246 * Look up a softc instance for a given driver.
2243 */ 2247 */
2244void * 2248void *
2245device_lookup_private(cfdriver_t cd, int unit) 2249device_lookup_private(cfdriver_t cd, int unit)
2246{ 2250{
2247 2251
2248 return device_private(device_lookup(cd, unit)); 2252 return device_private(device_lookup(cd, unit));
2249} 2253}
2250 2254
2251/* 2255/*
2252 * device_find_by_xname: 2256 * device_find_by_xname:
2253 * 2257 *
2254 * Returns the device of the given name or NULL if it doesn't exist. 2258 * Returns the device of the given name or NULL if it doesn't exist.
2255 */ 2259 */
2256device_t 2260device_t
2257device_find_by_xname(const char *name) 2261device_find_by_xname(const char *name)
2258{ 2262{
2259 device_t dv; 2263 device_t dv;
2260 deviter_t di; 2264 deviter_t di;
2261 2265
2262 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) { 2266 for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
2263 if (strcmp(device_xname(dv), name) == 0) 2267 if (strcmp(device_xname(dv), name) == 0)
2264 break; 2268 break;
2265 } 2269 }
2266 deviter_release(&di); 2270 deviter_release(&di);
2267 2271
2268 return dv; 2272 return dv;
2269} 2273}
2270 2274
2271/* 2275/*
2272 * device_find_by_driver_unit: 2276 * device_find_by_driver_unit:
2273 * 2277 *
2274 * Returns the device of the given driver name and unit or 2278 * Returns the device of the given driver name and unit or
2275 * NULL if it doesn't exist. 2279 * NULL if it doesn't exist.
2276 */ 2280 */
2277device_t 2281device_t
2278device_find_by_driver_unit(const char *name, int unit) 2282device_find_by_driver_unit(const char *name, int unit)
2279{ 2283{
2280 struct cfdriver *cd; 2284 struct cfdriver *cd;
2281 2285
2282 if ((cd = config_cfdriver_lookup(name)) == NULL) 2286 if ((cd = config_cfdriver_lookup(name)) == NULL)
2283 return NULL; 2287 return NULL;
2284 return device_lookup(cd, unit); 2288 return device_lookup(cd, unit);
2285} 2289}
2286 2290
2287/* 2291/*
2288 * Power management related functions. 2292 * Power management related functions.
2289 */ 2293 */
2290 2294
2291bool 2295bool
2292device_pmf_is_registered(device_t dev) 2296device_pmf_is_registered(device_t dev)
2293{ 2297{
2294 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0; 2298 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
2295} 2299}
2296 2300
2297bool 2301bool
2298device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual) 2302device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual)
2299{ 2303{
2300 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0) 2304 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
2301 return true; 2305 return true;
2302 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0) 2306 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
2303 return false; 2307 return false;
2304 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2308 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2305 dev->dv_driver_suspend != NULL && 2309 dev->dv_driver_suspend != NULL &&
2306 !(*dev->dv_driver_suspend)(dev, qual)) 2310 !(*dev->dv_driver_suspend)(dev, qual))
2307 return false; 2311 return false;
2308 2312
2309 dev->dv_flags |= DVF_DRIVER_SUSPENDED; 2313 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
2310 return true; 2314 return true;
2311} 2315}
2312 2316
2313bool 2317bool
2314device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual) 2318device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual)
2315{ 2319{
2316 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0) 2320 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
2317 return true; 2321 return true;
2318 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0) 2322 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
2319 return false; 2323 return false;
2320 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER && 2324 if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
2321 dev->dv_driver_resume != NULL && 2325 dev->dv_driver_resume != NULL &&
2322 !(*dev->dv_driver_resume)(dev, qual)) 2326 !(*dev->dv_driver_resume)(dev, qual))
2323 return false; 2327 return false;
2324 2328
2325 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED; 2329 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
2326 return true; 2330 return true;
2327} 2331}
2328 2332
2329bool 2333bool
2330device_pmf_driver_shutdown(device_t dev, int how) 2334device_pmf_driver_shutdown(device_t dev, int how)
2331{ 2335{
2332 2336
2333 if (*dev->dv_driver_shutdown != NULL && 2337 if (*dev->dv_driver_shutdown != NULL &&
2334 !(*dev->dv_driver_shutdown)(dev, how)) 2338 !(*dev->dv_driver_shutdown)(dev, how))
2335 return false; 2339 return false;
2336 return true; 2340 return true;
2337} 2341}
2338 2342
2339bool 2343bool
2340device_pmf_driver_register(device_t dev, 2344device_pmf_driver_register(device_t dev,
2341 bool (*suspend)(device_t, const pmf_qual_t *), 2345 bool (*suspend)(device_t, const pmf_qual_t *),
2342 bool (*resume)(device_t, const pmf_qual_t *), 2346 bool (*resume)(device_t, const pmf_qual_t *),
2343 bool (*shutdown)(device_t, int)) 2347 bool (*shutdown)(device_t, int))
2344{ 2348{
2345 dev->dv_driver_suspend = suspend; 2349 dev->dv_driver_suspend = suspend;
2346 dev->dv_driver_resume = resume; 2350 dev->dv_driver_resume = resume;
2347 dev->dv_driver_shutdown = shutdown; 2351 dev->dv_driver_shutdown = shutdown;
2348 dev->dv_flags |= DVF_POWER_HANDLERS; 2352 dev->dv_flags |= DVF_POWER_HANDLERS;
2349 return true; 2353 return true;
2350} 2354}
2351 2355
2352static const char * 2356static const char *
2353curlwp_name(void) 2357curlwp_name(void)
2354{ 2358{
2355 if (curlwp->l_name != NULL) 2359 if (curlwp->l_name != NULL)
2356 return curlwp->l_name; 2360 return curlwp->l_name;
2357 else 2361 else
2358 return curlwp->l_proc->p_comm; 2362 return curlwp->l_proc->p_comm;
2359} 2363}
2360 2364
2361void 2365void
2362device_pmf_driver_deregister(device_t dev) 2366device_pmf_driver_deregister(device_t dev)
2363{ 2367{
2364 device_lock_t dvl = device_getlock(dev); 2368 device_lock_t dvl = device_getlock(dev);
2365 2369
2366 dev->dv_driver_suspend = NULL; 2370 dev->dv_driver_suspend = NULL;
2367 dev->dv_driver_resume = NULL; 2371 dev->dv_driver_resume = NULL;
2368 2372
2369 mutex_enter(&dvl->dvl_mtx); 2373 mutex_enter(&dvl->dvl_mtx);
2370 dev->dv_flags &= ~DVF_POWER_HANDLERS; 2374 dev->dv_flags &= ~DVF_POWER_HANDLERS;
2371 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) { 2375 while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
2372 /* Wake a thread that waits for the lock. That 2376 /* Wake a thread that waits for the lock. That
2373 * thread will fail to acquire the lock, and then 2377 * thread will fail to acquire the lock, and then
2374 * it will wake the next thread that waits for the 2378 * it will wake the next thread that waits for the
2375 * lock, or else it will wake us. 2379 * lock, or else it will wake us.
2376 */ 2380 */
2377 cv_signal(&dvl->dvl_cv); 2381 cv_signal(&dvl->dvl_cv);
2378 pmflock_debug(dev, __func__, __LINE__); 2382 pmflock_debug(dev, __func__, __LINE__);
2379 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2383 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2380 pmflock_debug(dev, __func__, __LINE__); 2384 pmflock_debug(dev, __func__, __LINE__);
2381 } 2385 }
2382 mutex_exit(&dvl->dvl_mtx); 2386 mutex_exit(&dvl->dvl_mtx);
2383} 2387}
2384 2388
2385bool 2389bool
2386device_pmf_driver_child_register(device_t dev) 2390device_pmf_driver_child_register(device_t dev)
2387{ 2391{
2388 device_t parent = device_parent(dev); 2392 device_t parent = device_parent(dev);
2389 2393
2390 if (parent == NULL || parent->dv_driver_child_register == NULL) 2394 if (parent == NULL || parent->dv_driver_child_register == NULL)
2391 return true; 2395 return true;
2392 return (*parent->dv_driver_child_register)(dev); 2396 return (*parent->dv_driver_child_register)(dev);
2393} 2397}
2394 2398
2395void 2399void
2396device_pmf_driver_set_child_register(device_t dev, 2400device_pmf_driver_set_child_register(device_t dev,
2397 bool (*child_register)(device_t)) 2401 bool (*child_register)(device_t))
2398{ 2402{
2399 dev->dv_driver_child_register = child_register; 2403 dev->dv_driver_child_register = child_register;
2400} 2404}
2401 2405
2402static void 2406static void
2403pmflock_debug(device_t dev, const char *func, int line) 2407pmflock_debug(device_t dev, const char *func, int line)
2404{ 2408{
2405 device_lock_t dvl = device_getlock(dev); 2409 device_lock_t dvl = device_getlock(dev);
2406 2410
2407 aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n", 2411 aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n",
2408 func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait, 2412 func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait,
2409 dev->dv_flags); 2413 dev->dv_flags);
2410} 2414}
2411 2415
2412static bool 2416static bool
2413device_pmf_lock1(device_t dev) 2417device_pmf_lock1(device_t dev)
2414{ 2418{
2415 device_lock_t dvl = device_getlock(dev); 2419 device_lock_t dvl = device_getlock(dev);
2416 2420
2417 while (device_pmf_is_registered(dev) && 2421 while (device_pmf_is_registered(dev) &&
2418 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) { 2422 dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
2419 dvl->dvl_nwait++; 2423 dvl->dvl_nwait++;
2420 pmflock_debug(dev, __func__, __LINE__); 2424 pmflock_debug(dev, __func__, __LINE__);
2421 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx); 2425 cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
2422 pmflock_debug(dev, __func__, __LINE__); 2426 pmflock_debug(dev, __func__, __LINE__);
2423 dvl->dvl_nwait--; 2427 dvl->dvl_nwait--;
2424 } 2428 }
2425 if (!device_pmf_is_registered(dev)) { 2429 if (!device_pmf_is_registered(dev)) {
2426 pmflock_debug(dev, __func__, __LINE__); 2430 pmflock_debug(dev, __func__, __LINE__);
2427 /* We could not acquire the lock, but some other thread may 2431 /* We could not acquire the lock, but some other thread may
2428 * wait for it, also. Wake that thread. 2432 * wait for it, also. Wake that thread.
2429 */ 2433 */
2430 cv_signal(&dvl->dvl_cv); 2434 cv_signal(&dvl->dvl_cv);
2431 return false; 2435 return false;
2432 } 2436 }
2433 dvl->dvl_nlock++; 2437 dvl->dvl_nlock++;
2434 dvl->dvl_holder = curlwp; 2438 dvl->dvl_holder = curlwp;
2435 pmflock_debug(dev, __func__, __LINE__); 2439 pmflock_debug(dev, __func__, __LINE__);
2436 return true; 2440 return true;