Sun May 24 12:27:50 2009 UTC ()
Bus scans can make it appear as if the system has paused, so
twiddle constantly while config_interrupts() jobs are running.


(ad)
diff -r1.175 -r1.176 src/sys/kern/subr_autoconf.c

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

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