subr_autoconf.c revision 1.65 1 1.65 thorpej /* $NetBSD: subr_autoconf.c,v 1.65 2002/09/23 23:16:06 thorpej Exp $ */
2 1.53 cgd
3 1.53 cgd /*
4 1.53 cgd * Copyright (c) 1996, 2000 Christopher G. Demetriou
5 1.53 cgd * All rights reserved.
6 1.54 cgd *
7 1.53 cgd * Redistribution and use in source and binary forms, with or without
8 1.53 cgd * modification, are permitted provided that the following conditions
9 1.53 cgd * are met:
10 1.53 cgd * 1. Redistributions of source code must retain the above copyright
11 1.53 cgd * notice, this list of conditions and the following disclaimer.
12 1.53 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.53 cgd * notice, this list of conditions and the following disclaimer in the
14 1.53 cgd * documentation and/or other materials provided with the distribution.
15 1.53 cgd * 3. All advertising materials mentioning features or use of this software
16 1.53 cgd * must display the following acknowledgement:
17 1.54 cgd * This product includes software developed for the
18 1.54 cgd * NetBSD Project. See http://www.netbsd.org/ for
19 1.54 cgd * information about NetBSD.
20 1.53 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.54 cgd * derived from this software without specific prior written permission.
22 1.54 cgd *
23 1.53 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.53 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.53 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.53 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.53 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.53 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.53 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.53 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.53 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.53 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.54 cgd *
34 1.54 cgd * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
35 1.53 cgd */
36 1.9 cgd
37 1.1 glass /*
38 1.7 glass * Copyright (c) 1992, 1993
39 1.7 glass * The Regents of the University of California. All rights reserved.
40 1.1 glass *
41 1.1 glass * This software was developed by the Computer Systems Engineering group
42 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
43 1.1 glass * contributed to Berkeley.
44 1.1 glass *
45 1.1 glass * All advertising materials mentioning features or use of this software
46 1.1 glass * must display the following acknowledgement:
47 1.1 glass * This product includes software developed by the University of
48 1.1 glass * California, Lawrence Berkeley Laboratories.
49 1.1 glass *
50 1.7 glass * Redistribution and use in source and binary forms, with or without
51 1.7 glass * modification, are permitted provided that the following conditions
52 1.7 glass * are met:
53 1.7 glass * 1. Redistributions of source code must retain the above copyright
54 1.7 glass * notice, this list of conditions and the following disclaimer.
55 1.7 glass * 2. Redistributions in binary form must reproduce the above copyright
56 1.7 glass * notice, this list of conditions and the following disclaimer in the
57 1.7 glass * documentation and/or other materials provided with the distribution.
58 1.7 glass * 3. All advertising materials mentioning features or use of this software
59 1.7 glass * must display the following acknowledgement:
60 1.7 glass * This product includes software developed by the University of
61 1.7 glass * California, Berkeley and its contributors.
62 1.7 glass * 4. Neither the name of the University nor the names of its contributors
63 1.7 glass * may be used to endorse or promote products derived from this software
64 1.7 glass * without specific prior written permission.
65 1.1 glass *
66 1.7 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 1.7 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 1.7 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 1.7 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 1.7 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 1.7 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 1.7 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 1.7 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 1.7 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 1.7 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 1.7 glass * SUCH DAMAGE.
77 1.1 glass *
78 1.8 cgd * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
79 1.9 cgd *
80 1.28 fvdl * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
81 1.1 glass */
82 1.1 glass
83 1.51 cgd #include <sys/cdefs.h>
84 1.65 thorpej __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.65 2002/09/23 23:16:06 thorpej Exp $");
85 1.62 simonb
86 1.62 simonb #include "opt_ddb.h"
87 1.51 cgd
88 1.4 mycroft #include <sys/param.h>
89 1.4 mycroft #include <sys/device.h>
90 1.4 mycroft #include <sys/malloc.h>
91 1.17 christos #include <sys/systm.h>
92 1.43 thorpej #include <sys/kernel.h>
93 1.33 thorpej #include <sys/errno.h>
94 1.47 thorpej #include <sys/proc.h>
95 1.16 mycroft #include <machine/limits.h>
96 1.1 glass
97 1.57 gmcgarry #include "opt_userconf.h"
98 1.57 gmcgarry #ifdef USERCONF
99 1.57 gmcgarry #include <sys/userconf.h>
100 1.57 gmcgarry #include <sys/reboot.h>
101 1.57 gmcgarry #endif
102 1.57 gmcgarry
103 1.1 glass /*
104 1.1 glass * Autoconfiguration subroutines.
105 1.1 glass */
106 1.1 glass
107 1.1 glass /*
108 1.1 glass * ioconf.c exports exactly two names: cfdata and cfroots. All system
109 1.1 glass * devices and drivers are found via these tables.
110 1.1 glass */
111 1.1 glass extern struct cfdata cfdata[];
112 1.1 glass extern short cfroots[];
113 1.1 glass
114 1.65 thorpej /*
115 1.65 thorpej * List of cfdata tables. We always have one such list -- the one
116 1.65 thorpej * built statically when the kernel was configured.
117 1.65 thorpej */
118 1.65 thorpej struct cftablelist allcftables;
119 1.65 thorpej static struct cftable initcftable;
120 1.65 thorpej
121 1.1 glass #define ROOT ((struct device *)NULL)
122 1.1 glass
123 1.16 mycroft struct matchinfo {
124 1.16 mycroft cfmatch_t fn;
125 1.16 mycroft struct device *parent;
126 1.25 cgd void *aux;
127 1.25 cgd struct cfdata *match;
128 1.25 cgd int pri;
129 1.16 mycroft };
130 1.17 christos
131 1.51 cgd static char *number(char *, int);
132 1.51 cgd static void mapply(struct matchinfo *, struct cfdata *);
133 1.16 mycroft
134 1.29 thorpej struct deferred_config {
135 1.29 thorpej TAILQ_ENTRY(deferred_config) dc_queue;
136 1.29 thorpej struct device *dc_dev;
137 1.51 cgd void (*dc_func)(struct device *);
138 1.29 thorpej };
139 1.29 thorpej
140 1.42 thorpej TAILQ_HEAD(deferred_config_head, deferred_config);
141 1.29 thorpej
142 1.42 thorpej struct deferred_config_head deferred_config_queue;
143 1.42 thorpej struct deferred_config_head interrupt_config_queue;
144 1.42 thorpej
145 1.51 cgd static void config_process_deferred(struct deferred_config_head *,
146 1.51 cgd struct device *);
147 1.29 thorpej
148 1.56 thorpej /* list of all devices */
149 1.56 thorpej struct devicelist alldevs;
150 1.56 thorpej
151 1.56 thorpej /* list of all events */
152 1.56 thorpej struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
153 1.20 cgd
154 1.47 thorpej __volatile int config_pending; /* semaphore for mountroot */
155 1.47 thorpej
156 1.20 cgd /*
157 1.41 thorpej * Configure the system's hardware.
158 1.20 cgd */
159 1.20 cgd void
160 1.51 cgd configure(void)
161 1.20 cgd {
162 1.20 cgd
163 1.65 thorpej TAILQ_INIT(&allcftables);
164 1.65 thorpej
165 1.65 thorpej initcftable.ct_cfdata = cfdata;
166 1.65 thorpej TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
167 1.65 thorpej
168 1.29 thorpej TAILQ_INIT(&deferred_config_queue);
169 1.42 thorpej TAILQ_INIT(&interrupt_config_queue);
170 1.41 thorpej TAILQ_INIT(&alldevs);
171 1.57 gmcgarry
172 1.57 gmcgarry #ifdef USERCONF
173 1.57 gmcgarry if (boothowto & RB_USERCONF)
174 1.57 gmcgarry user_config();
175 1.57 gmcgarry #endif
176 1.41 thorpej
177 1.41 thorpej /*
178 1.41 thorpej * Do the machine-dependent portion of autoconfiguration. This
179 1.41 thorpej * sets the configuration machinery here in motion by "finding"
180 1.41 thorpej * the root bus. When this function returns, we expect interrupts
181 1.41 thorpej * to be enabled.
182 1.41 thorpej */
183 1.41 thorpej cpu_configure();
184 1.43 thorpej
185 1.43 thorpej /*
186 1.43 thorpej * Now that we've found all the hardware, start the real time
187 1.43 thorpej * and statistics clocks.
188 1.43 thorpej */
189 1.43 thorpej initclocks();
190 1.43 thorpej
191 1.43 thorpej cold = 0; /* clocks are running, we're warm now! */
192 1.42 thorpej
193 1.42 thorpej /*
194 1.42 thorpej * Now callback to finish configuration for devices which want
195 1.42 thorpej * to do this once interrupts are enabled.
196 1.42 thorpej */
197 1.42 thorpej config_process_deferred(&interrupt_config_queue, NULL);
198 1.20 cgd }
199 1.20 cgd
200 1.1 glass /*
201 1.1 glass * Apply the matching function and choose the best. This is used
202 1.1 glass * a few times and we want to keep the code small.
203 1.1 glass */
204 1.16 mycroft static void
205 1.51 cgd mapply(struct matchinfo *m, struct cfdata *cf)
206 1.1 glass {
207 1.50 augustss int pri;
208 1.1 glass
209 1.1 glass if (m->fn != NULL)
210 1.25 cgd pri = (*m->fn)(m->parent, cf, m->aux);
211 1.3 glass else {
212 1.19 thorpej if (cf->cf_attach->ca_match == NULL) {
213 1.16 mycroft panic("mapply: no match function for '%s' device\n",
214 1.12 mycroft cf->cf_driver->cd_name);
215 1.3 glass }
216 1.25 cgd pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
217 1.3 glass }
218 1.1 glass if (pri > m->pri) {
219 1.25 cgd m->match = cf;
220 1.1 glass m->pri = pri;
221 1.1 glass }
222 1.1 glass }
223 1.1 glass
224 1.1 glass /*
225 1.1 glass * Iterate over all potential children of some device, calling the given
226 1.1 glass * function (default being the child's match function) for each one.
227 1.1 glass * Nonzero returns are matches; the highest value returned is considered
228 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
229 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
230 1.1 glass *
231 1.1 glass * Note that this function is designed so that it can be used to apply
232 1.1 glass * an arbitrary function to all potential children (its return value
233 1.1 glass * can be ignored).
234 1.1 glass */
235 1.25 cgd struct cfdata *
236 1.51 cgd config_search(cfmatch_t fn, struct device *parent, void *aux)
237 1.1 glass {
238 1.65 thorpej struct cftable *ct;
239 1.50 augustss struct cfdata *cf;
240 1.50 augustss short *p;
241 1.1 glass struct matchinfo m;
242 1.1 glass
243 1.1 glass m.fn = fn;
244 1.1 glass m.parent = parent;
245 1.25 cgd m.aux = aux;
246 1.14 mycroft m.match = NULL;
247 1.1 glass m.pri = 0;
248 1.65 thorpej
249 1.65 thorpej TAILQ_FOREACH(ct, &allcftables, ct_list) {
250 1.65 thorpej for (cf = ct->ct_cfdata; cf->cf_driver; cf++) {
251 1.65 thorpej /*
252 1.65 thorpej * Skip cf if no longer eligible, otherwise scan
253 1.65 thorpej * through parents for one matching `parent', and
254 1.65 thorpej * try match function.
255 1.65 thorpej */
256 1.65 thorpej if (cf->cf_fstate == FSTATE_FOUND)
257 1.65 thorpej continue;
258 1.65 thorpej if (cf->cf_fstate == FSTATE_DNOTFOUND ||
259 1.65 thorpej cf->cf_fstate == FSTATE_DSTAR)
260 1.65 thorpej continue;
261 1.65 thorpej for (p = cf->cf_parents; *p >= 0; p++)
262 1.65 thorpej if (parent->dv_cfdata == &(ct->ct_cfdata)[*p])
263 1.65 thorpej mapply(&m, cf);
264 1.65 thorpej }
265 1.1 glass }
266 1.1 glass return (m.match);
267 1.1 glass }
268 1.1 glass
269 1.16 mycroft /*
270 1.1 glass * Find the given root device.
271 1.1 glass * This is much like config_search, but there is no parent.
272 1.65 thorpej * Don't bother with multiple cfdata tables; the root node
273 1.65 thorpej * must always be in the initial table.
274 1.1 glass */
275 1.25 cgd struct cfdata *
276 1.52 cgd config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
277 1.1 glass {
278 1.50 augustss struct cfdata *cf;
279 1.50 augustss short *p;
280 1.1 glass struct matchinfo m;
281 1.1 glass
282 1.1 glass m.fn = fn;
283 1.1 glass m.parent = ROOT;
284 1.25 cgd m.aux = aux;
285 1.14 mycroft m.match = NULL;
286 1.1 glass m.pri = 0;
287 1.1 glass /*
288 1.1 glass * Look at root entries for matching name. We do not bother
289 1.1 glass * with found-state here since only one root should ever be
290 1.1 glass * searched (and it must be done first).
291 1.1 glass */
292 1.1 glass for (p = cfroots; *p >= 0; p++) {
293 1.1 glass cf = &cfdata[*p];
294 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
295 1.16 mycroft mapply(&m, cf);
296 1.1 glass }
297 1.1 glass return (m.match);
298 1.1 glass }
299 1.1 glass
300 1.52 cgd static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
301 1.1 glass
302 1.1 glass /*
303 1.1 glass * The given `aux' argument describes a device that has been found
304 1.1 glass * on the given parent, but not necessarily configured. Locate the
305 1.18 cgd * configuration data for that device (using the submatch function
306 1.18 cgd * provided, or using candidates' cd_match configuration driver
307 1.18 cgd * functions) and attach it, and return true. If the device was
308 1.1 glass * not configured, call the given `print' function and return 0.
309 1.1 glass */
310 1.21 cgd struct device *
311 1.51 cgd config_found_sm(struct device *parent, void *aux, cfprint_t print,
312 1.51 cgd cfmatch_t submatch)
313 1.1 glass {
314 1.25 cgd struct cfdata *cf;
315 1.25 cgd
316 1.25 cgd if ((cf = config_search(submatch, parent, aux)) != NULL)
317 1.25 cgd return (config_attach(parent, cf, aux, print));
318 1.3 glass if (print)
319 1.55 sommerfe printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
320 1.21 cgd return (NULL);
321 1.1 glass }
322 1.1 glass
323 1.1 glass /*
324 1.1 glass * As above, but for root devices.
325 1.1 glass */
326 1.21 cgd struct device *
327 1.52 cgd config_rootfound(const char *rootname, void *aux)
328 1.1 glass {
329 1.25 cgd struct cfdata *cf;
330 1.25 cgd
331 1.25 cgd if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
332 1.25 cgd return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
333 1.24 christos printf("root device %s not configured\n", rootname);
334 1.21 cgd return (NULL);
335 1.1 glass }
336 1.1 glass
337 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
338 1.1 glass static char *
339 1.51 cgd number(char *ep, int n)
340 1.1 glass {
341 1.1 glass
342 1.1 glass *--ep = 0;
343 1.1 glass while (n >= 10) {
344 1.1 glass *--ep = (n % 10) + '0';
345 1.1 glass n /= 10;
346 1.1 glass }
347 1.1 glass *--ep = n + '0';
348 1.1 glass return (ep);
349 1.1 glass }
350 1.1 glass
351 1.1 glass /*
352 1.59 augustss * Expand the size of the cd_devs array if necessary.
353 1.59 augustss */
354 1.59 augustss void
355 1.59 augustss config_makeroom(int n, struct cfdriver *cd)
356 1.59 augustss {
357 1.59 augustss int old, new;
358 1.59 augustss void **nsp;
359 1.59 augustss
360 1.59 augustss if (n < cd->cd_ndevs)
361 1.59 augustss return;
362 1.59 augustss
363 1.59 augustss /*
364 1.59 augustss * Need to expand the array.
365 1.59 augustss */
366 1.59 augustss old = cd->cd_ndevs;
367 1.61 thorpej if (old == 0)
368 1.59 augustss new = MINALLOCSIZE / sizeof(void *);
369 1.59 augustss else
370 1.59 augustss new = old * 2;
371 1.59 augustss while (new <= n)
372 1.59 augustss new *= 2;
373 1.59 augustss cd->cd_ndevs = new;
374 1.59 augustss nsp = malloc(new * sizeof(void *), M_DEVBUF,
375 1.59 augustss cold ? M_NOWAIT : M_WAITOK);
376 1.60 augustss if (nsp == NULL)
377 1.59 augustss panic("config_attach: %sing dev array",
378 1.59 augustss old != 0 ? "expand" : "creat");
379 1.59 augustss memset(nsp + old, 0, (new - old) * sizeof(void *));
380 1.61 thorpej if (old != 0) {
381 1.59 augustss memcpy(nsp, cd->cd_devs, old * sizeof(void *));
382 1.59 augustss free(cd->cd_devs, M_DEVBUF);
383 1.59 augustss }
384 1.59 augustss cd->cd_devs = nsp;
385 1.59 augustss }
386 1.59 augustss
387 1.59 augustss /*
388 1.1 glass * Attach a found device. Allocates memory for device variables.
389 1.1 glass */
390 1.25 cgd struct device *
391 1.51 cgd config_attach(struct device *parent, struct cfdata *cf, void *aux,
392 1.51 cgd cfprint_t print)
393 1.25 cgd {
394 1.50 augustss struct device *dev;
395 1.65 thorpej struct cftable *ct;
396 1.50 augustss struct cfdriver *cd;
397 1.50 augustss struct cfattach *ca;
398 1.50 augustss size_t lname, lunit;
399 1.52 cgd const char *xunit;
400 1.25 cgd int myunit;
401 1.25 cgd char num[10];
402 1.25 cgd
403 1.25 cgd cd = cf->cf_driver;
404 1.25 cgd ca = cf->cf_attach;
405 1.25 cgd if (ca->ca_devsize < sizeof(struct device))
406 1.25 cgd panic("config_attach");
407 1.46 cgd #ifndef __BROKEN_CONFIG_UNIT_USAGE
408 1.45 cgd if (cf->cf_fstate == FSTATE_STAR) {
409 1.45 cgd for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
410 1.45 cgd if (cd->cd_devs[myunit] == NULL)
411 1.45 cgd break;
412 1.45 cgd /*
413 1.45 cgd * myunit is now the unit of the first NULL device pointer,
414 1.45 cgd * or max(cd->cd_ndevs,cf->cf_unit).
415 1.45 cgd */
416 1.45 cgd } else {
417 1.45 cgd myunit = cf->cf_unit;
418 1.46 cgd #else /* __BROKEN_CONFIG_UNIT_USAGE */
419 1.46 cgd myunit = cf->cf_unit;
420 1.46 cgd if (cf->cf_fstate == FSTATE_STAR)
421 1.46 cgd cf->cf_unit++;
422 1.46 cgd else {
423 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
424 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
425 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
426 1.25 cgd }
427 1.25 cgd
428 1.25 cgd /* compute length of name and decimal expansion of unit number */
429 1.25 cgd lname = strlen(cd->cd_name);
430 1.30 perry xunit = number(&num[sizeof(num)], myunit);
431 1.30 perry lunit = &num[sizeof(num)] - xunit;
432 1.64 drochner if (lname + lunit > sizeof(dev->dv_xname))
433 1.25 cgd panic("config_attach: device name too long");
434 1.25 cgd
435 1.25 cgd /* get memory for all device vars */
436 1.43 thorpej dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
437 1.43 thorpej cold ? M_NOWAIT : M_WAITOK);
438 1.25 cgd if (!dev)
439 1.25 cgd panic("config_attach: memory allocation for device softc failed");
440 1.31 perry memset(dev, 0, ca->ca_devsize);
441 1.25 cgd TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
442 1.25 cgd dev->dv_class = cd->cd_class;
443 1.25 cgd dev->dv_cfdata = cf;
444 1.25 cgd dev->dv_unit = myunit;
445 1.31 perry memcpy(dev->dv_xname, cd->cd_name, lname);
446 1.31 perry memcpy(dev->dv_xname + lname, xunit, lunit);
447 1.25 cgd dev->dv_parent = parent;
448 1.33 thorpej dev->dv_flags = DVF_ACTIVE; /* always initially active */
449 1.29 thorpej
450 1.25 cgd if (parent == ROOT)
451 1.25 cgd printf("%s (root)", dev->dv_xname);
452 1.25 cgd else {
453 1.25 cgd printf("%s at %s", dev->dv_xname, parent->dv_xname);
454 1.25 cgd if (print)
455 1.52 cgd (void) (*print)(aux, NULL);
456 1.25 cgd }
457 1.25 cgd
458 1.25 cgd /* put this device in the devices array */
459 1.59 augustss config_makeroom(dev->dv_unit, cd);
460 1.25 cgd if (cd->cd_devs[dev->dv_unit])
461 1.25 cgd panic("config_attach: duplicate %s", dev->dv_xname);
462 1.25 cgd cd->cd_devs[dev->dv_unit] = dev;
463 1.25 cgd
464 1.25 cgd /*
465 1.25 cgd * Before attaching, clobber any unfound devices that are
466 1.45 cgd * otherwise identical.
467 1.25 cgd */
468 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
469 1.46 cgd /* bump the unit number on all starred cfdata for this device. */
470 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
471 1.65 thorpej TAILQ_FOREACH(ct, &allcftables, ct_list) {
472 1.65 thorpej for (cf = ct->ct_cfdata; cf->cf_driver; cf++) {
473 1.65 thorpej if (cf->cf_driver == cd &&
474 1.65 thorpej cf->cf_unit == dev->dv_unit) {
475 1.65 thorpej if (cf->cf_fstate == FSTATE_NOTFOUND)
476 1.65 thorpej cf->cf_fstate = FSTATE_FOUND;
477 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
478 1.65 thorpej if (cf->cf_fstate == FSTATE_STAR)
479 1.65 thorpej cf->cf_unit++;
480 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
481 1.65 thorpej }
482 1.25 cgd }
483 1.65 thorpej }
484 1.49 danw #ifdef __HAVE_DEVICE_REGISTER
485 1.25 cgd device_register(dev, aux);
486 1.25 cgd #endif
487 1.25 cgd (*ca->ca_attach)(parent, dev, aux);
488 1.42 thorpej config_process_deferred(&deferred_config_queue, dev);
489 1.25 cgd return (dev);
490 1.25 cgd }
491 1.29 thorpej
492 1.29 thorpej /*
493 1.33 thorpej * Detach a device. Optionally forced (e.g. because of hardware
494 1.33 thorpej * removal) and quiet. Returns zero if successful, non-zero
495 1.33 thorpej * (an error code) otherwise.
496 1.33 thorpej *
497 1.33 thorpej * Note that this code wants to be run from a process context, so
498 1.33 thorpej * that the detach can sleep to allow processes which have a device
499 1.33 thorpej * open to run and unwind their stacks.
500 1.33 thorpej */
501 1.33 thorpej int
502 1.51 cgd config_detach(struct device *dev, int flags)
503 1.33 thorpej {
504 1.65 thorpej struct cftable *ct;
505 1.33 thorpej struct cfdata *cf;
506 1.33 thorpej struct cfattach *ca;
507 1.33 thorpej struct cfdriver *cd;
508 1.33 thorpej #ifdef DIAGNOSTIC
509 1.33 thorpej struct device *d;
510 1.33 thorpej #endif
511 1.36 thorpej int rv = 0, i;
512 1.33 thorpej
513 1.33 thorpej cf = dev->dv_cfdata;
514 1.33 thorpej #ifdef DIAGNOSTIC
515 1.33 thorpej if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
516 1.33 thorpej panic("config_detach: bad device fstate");
517 1.33 thorpej #endif
518 1.33 thorpej ca = cf->cf_attach;
519 1.33 thorpej cd = cf->cf_driver;
520 1.33 thorpej
521 1.33 thorpej /*
522 1.33 thorpej * Ensure the device is deactivated. If the device doesn't
523 1.33 thorpej * have an activation entry point, we allow DVF_ACTIVE to
524 1.33 thorpej * remain set. Otherwise, if DVF_ACTIVE is still set, the
525 1.33 thorpej * device is busy, and the detach fails.
526 1.33 thorpej */
527 1.35 thorpej if (ca->ca_activate != NULL)
528 1.35 thorpej rv = config_deactivate(dev);
529 1.33 thorpej
530 1.33 thorpej /*
531 1.33 thorpej * Try to detach the device. If that's not possible, then
532 1.33 thorpej * we either panic() (for the forced but failed case), or
533 1.33 thorpej * return an error.
534 1.33 thorpej */
535 1.33 thorpej if (rv == 0) {
536 1.33 thorpej if (ca->ca_detach != NULL)
537 1.33 thorpej rv = (*ca->ca_detach)(dev, flags);
538 1.33 thorpej else
539 1.33 thorpej rv = EOPNOTSUPP;
540 1.33 thorpej }
541 1.33 thorpej if (rv != 0) {
542 1.33 thorpej if ((flags & DETACH_FORCE) == 0)
543 1.33 thorpej return (rv);
544 1.33 thorpej else
545 1.33 thorpej panic("config_detach: forced detach of %s failed (%d)",
546 1.33 thorpej dev->dv_xname, rv);
547 1.33 thorpej }
548 1.33 thorpej
549 1.33 thorpej /*
550 1.33 thorpej * The device has now been successfully detached.
551 1.33 thorpej */
552 1.33 thorpej
553 1.33 thorpej #ifdef DIAGNOSTIC
554 1.33 thorpej /*
555 1.33 thorpej * Sanity: If you're successfully detached, you should have no
556 1.33 thorpej * children. (Note that because children must be attached
557 1.33 thorpej * after parents, we only need to search the latter part of
558 1.33 thorpej * the list.)
559 1.33 thorpej */
560 1.33 thorpej for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
561 1.48 enami d = TAILQ_NEXT(d, dv_list)) {
562 1.48 enami if (d->dv_parent == dev) {
563 1.48 enami printf("config_detach: detached device %s"
564 1.48 enami " has children %s\n", dev->dv_xname, d->dv_xname);
565 1.48 enami panic("config_detach");
566 1.48 enami }
567 1.33 thorpej }
568 1.33 thorpej #endif
569 1.33 thorpej
570 1.33 thorpej /*
571 1.33 thorpej * Mark cfdata to show that the unit can be reused, if possible.
572 1.33 thorpej */
573 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
574 1.46 cgd /*
575 1.46 cgd * Note that we can only re-use a starred unit number if the unit
576 1.46 cgd * being detached had the last assigned unit number.
577 1.46 cgd */
578 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
579 1.65 thorpej TAILQ_FOREACH(ct, &allcftables, ct_list) {
580 1.65 thorpej for (cf = ct->ct_cfdata; cf->cf_driver; cf++) {
581 1.65 thorpej if (cf->cf_driver == cd) {
582 1.65 thorpej if (cf->cf_fstate == FSTATE_FOUND &&
583 1.65 thorpej cf->cf_unit == dev->dv_unit)
584 1.65 thorpej cf->cf_fstate = FSTATE_NOTFOUND;
585 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
586 1.65 thorpej if (cf->cf_fstate == FSTATE_STAR &&
587 1.65 thorpej cf->cf_unit == dev->dv_unit + 1)
588 1.65 thorpej cf->cf_unit--;
589 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
590 1.65 thorpej }
591 1.33 thorpej }
592 1.33 thorpej }
593 1.33 thorpej
594 1.33 thorpej /*
595 1.33 thorpej * Unlink from device list.
596 1.33 thorpej */
597 1.33 thorpej TAILQ_REMOVE(&alldevs, dev, dv_list);
598 1.33 thorpej
599 1.33 thorpej /*
600 1.33 thorpej * Remove from cfdriver's array, tell the world, and free softc.
601 1.33 thorpej */
602 1.33 thorpej cd->cd_devs[dev->dv_unit] = NULL;
603 1.33 thorpej if ((flags & DETACH_QUIET) == 0)
604 1.33 thorpej printf("%s detached\n", dev->dv_xname);
605 1.33 thorpej free(dev, M_DEVBUF);
606 1.33 thorpej
607 1.33 thorpej /*
608 1.33 thorpej * If the device now has no units in use, deallocate its softc array.
609 1.33 thorpej */
610 1.33 thorpej for (i = 0; i < cd->cd_ndevs; i++)
611 1.33 thorpej if (cd->cd_devs[i] != NULL)
612 1.33 thorpej break;
613 1.33 thorpej if (i == cd->cd_ndevs) { /* nothing found; deallocate */
614 1.33 thorpej free(cd->cd_devs, M_DEVBUF);
615 1.33 thorpej cd->cd_devs = NULL;
616 1.33 thorpej cd->cd_ndevs = 0;
617 1.33 thorpej }
618 1.33 thorpej
619 1.33 thorpej /*
620 1.33 thorpej * Return success.
621 1.33 thorpej */
622 1.33 thorpej return (0);
623 1.33 thorpej }
624 1.33 thorpej
625 1.33 thorpej int
626 1.51 cgd config_activate(struct device *dev)
627 1.33 thorpej {
628 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
629 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
630 1.33 thorpej
631 1.33 thorpej if (ca->ca_activate == NULL)
632 1.33 thorpej return (EOPNOTSUPP);
633 1.33 thorpej
634 1.33 thorpej if ((dev->dv_flags & DVF_ACTIVE) == 0) {
635 1.33 thorpej dev->dv_flags |= DVF_ACTIVE;
636 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
637 1.34 thorpej if (rv)
638 1.34 thorpej dev->dv_flags = oflags;
639 1.33 thorpej }
640 1.33 thorpej return (rv);
641 1.33 thorpej }
642 1.33 thorpej
643 1.33 thorpej int
644 1.51 cgd config_deactivate(struct device *dev)
645 1.33 thorpej {
646 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
647 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
648 1.33 thorpej
649 1.33 thorpej if (ca->ca_activate == NULL)
650 1.33 thorpej return (EOPNOTSUPP);
651 1.33 thorpej
652 1.33 thorpej if (dev->dv_flags & DVF_ACTIVE) {
653 1.33 thorpej dev->dv_flags &= ~DVF_ACTIVE;
654 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
655 1.34 thorpej if (rv)
656 1.34 thorpej dev->dv_flags = oflags;
657 1.33 thorpej }
658 1.33 thorpej return (rv);
659 1.33 thorpej }
660 1.33 thorpej
661 1.33 thorpej /*
662 1.29 thorpej * Defer the configuration of the specified device until all
663 1.29 thorpej * of its parent's devices have been attached.
664 1.29 thorpej */
665 1.29 thorpej void
666 1.51 cgd config_defer(struct device *dev, void (*func)(struct device *))
667 1.29 thorpej {
668 1.29 thorpej struct deferred_config *dc;
669 1.29 thorpej
670 1.29 thorpej if (dev->dv_parent == NULL)
671 1.29 thorpej panic("config_defer: can't defer config of a root device");
672 1.29 thorpej
673 1.29 thorpej #ifdef DIAGNOSTIC
674 1.29 thorpej for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
675 1.29 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
676 1.29 thorpej if (dc->dc_dev == dev)
677 1.29 thorpej panic("config_defer: deferred twice");
678 1.29 thorpej }
679 1.29 thorpej #endif
680 1.29 thorpej
681 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
682 1.43 thorpej if (dc == NULL)
683 1.43 thorpej panic("config_defer: unable to allocate callback");
684 1.29 thorpej
685 1.29 thorpej dc->dc_dev = dev;
686 1.29 thorpej dc->dc_func = func;
687 1.29 thorpej TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
688 1.47 thorpej config_pending_incr();
689 1.29 thorpej }
690 1.29 thorpej
691 1.29 thorpej /*
692 1.42 thorpej * Defer some autoconfiguration for a device until after interrupts
693 1.42 thorpej * are enabled.
694 1.42 thorpej */
695 1.42 thorpej void
696 1.51 cgd config_interrupts(struct device *dev, void (*func)(struct device *))
697 1.42 thorpej {
698 1.42 thorpej struct deferred_config *dc;
699 1.42 thorpej
700 1.42 thorpej /*
701 1.42 thorpej * If interrupts are enabled, callback now.
702 1.42 thorpej */
703 1.43 thorpej if (cold == 0) {
704 1.42 thorpej (*func)(dev);
705 1.42 thorpej return;
706 1.42 thorpej }
707 1.42 thorpej
708 1.42 thorpej #ifdef DIAGNOSTIC
709 1.42 thorpej for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
710 1.42 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
711 1.42 thorpej if (dc->dc_dev == dev)
712 1.42 thorpej panic("config_interrupts: deferred twice");
713 1.42 thorpej }
714 1.42 thorpej #endif
715 1.42 thorpej
716 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
717 1.43 thorpej if (dc == NULL)
718 1.43 thorpej panic("config_interrupts: unable to allocate callback");
719 1.42 thorpej
720 1.42 thorpej dc->dc_dev = dev;
721 1.42 thorpej dc->dc_func = func;
722 1.42 thorpej TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
723 1.47 thorpej config_pending_incr();
724 1.42 thorpej }
725 1.42 thorpej
726 1.42 thorpej /*
727 1.42 thorpej * Process a deferred configuration queue.
728 1.29 thorpej */
729 1.29 thorpej static void
730 1.51 cgd config_process_deferred(struct deferred_config_head *queue,
731 1.51 cgd struct device *parent)
732 1.29 thorpej {
733 1.29 thorpej struct deferred_config *dc, *ndc;
734 1.29 thorpej
735 1.42 thorpej for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
736 1.29 thorpej ndc = TAILQ_NEXT(dc, dc_queue);
737 1.42 thorpej if (parent == NULL || dc->dc_dev->dv_parent == parent) {
738 1.42 thorpej TAILQ_REMOVE(queue, dc, dc_queue);
739 1.29 thorpej (*dc->dc_func)(dc->dc_dev);
740 1.29 thorpej free(dc, M_DEVBUF);
741 1.47 thorpej config_pending_decr();
742 1.29 thorpej }
743 1.29 thorpej }
744 1.47 thorpej }
745 1.47 thorpej
746 1.47 thorpej /*
747 1.47 thorpej * Manipulate the config_pending semaphore.
748 1.47 thorpej */
749 1.47 thorpej void
750 1.51 cgd config_pending_incr(void)
751 1.47 thorpej {
752 1.47 thorpej
753 1.47 thorpej config_pending++;
754 1.47 thorpej }
755 1.47 thorpej
756 1.47 thorpej void
757 1.51 cgd config_pending_decr(void)
758 1.47 thorpej {
759 1.47 thorpej
760 1.47 thorpej #ifdef DIAGNOSTIC
761 1.47 thorpej if (config_pending == 0)
762 1.47 thorpej panic("config_pending_decr: config_pending == 0");
763 1.47 thorpej #endif
764 1.47 thorpej config_pending--;
765 1.47 thorpej if (config_pending == 0)
766 1.47 thorpej wakeup((void *)&config_pending);
767 1.29 thorpej }
768 1.1 glass
769 1.1 glass /*
770 1.53 cgd * Attach a statically-initialized event. The type and string pointers
771 1.53 cgd * are already set up.
772 1.1 glass */
773 1.1 glass void
774 1.53 cgd evcnt_attach_static(struct evcnt *ev)
775 1.1 glass {
776 1.53 cgd int len;
777 1.1 glass
778 1.53 cgd len = strlen(ev->ev_group);
779 1.1 glass #ifdef DIAGNOSTIC
780 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
781 1.53 cgd panic("evcnt_attach_static: group length (%s)", ev->ev_group);
782 1.1 glass #endif
783 1.53 cgd ev->ev_grouplen = len;
784 1.53 cgd
785 1.53 cgd len = strlen(ev->ev_name);
786 1.53 cgd #ifdef DIAGNOSTIC
787 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
788 1.53 cgd panic("evcnt_attach_static: name length (%s)", ev->ev_name);
789 1.53 cgd #endif
790 1.53 cgd ev->ev_namelen = len;
791 1.53 cgd
792 1.20 cgd TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
793 1.53 cgd }
794 1.53 cgd
795 1.53 cgd /*
796 1.53 cgd * Attach a dynamically-initialized event. Zero it, set up the type
797 1.53 cgd * and string pointers and then act like it was statically initialized.
798 1.53 cgd */
799 1.53 cgd void
800 1.53 cgd evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
801 1.53 cgd const char *group, const char *name)
802 1.53 cgd {
803 1.53 cgd
804 1.53 cgd memset(ev, 0, sizeof *ev);
805 1.53 cgd ev->ev_type = type;
806 1.53 cgd ev->ev_parent = parent;
807 1.53 cgd ev->ev_group = group;
808 1.53 cgd ev->ev_name = name;
809 1.53 cgd evcnt_attach_static(ev);
810 1.33 thorpej }
811 1.33 thorpej
812 1.33 thorpej /*
813 1.33 thorpej * Detach an event.
814 1.33 thorpej */
815 1.33 thorpej void
816 1.51 cgd evcnt_detach(struct evcnt *ev)
817 1.33 thorpej {
818 1.33 thorpej
819 1.33 thorpej TAILQ_REMOVE(&allevents, ev, ev_list);
820 1.1 glass }
821 1.62 simonb
822 1.62 simonb #ifdef DDB
823 1.62 simonb void
824 1.62 simonb event_print(int full, void (*pr)(const char *, ...))
825 1.62 simonb {
826 1.62 simonb struct evcnt *evp;
827 1.62 simonb
828 1.62 simonb TAILQ_FOREACH(evp, &allevents, ev_list) {
829 1.62 simonb if (evp->ev_count == 0 && !full)
830 1.62 simonb continue;
831 1.62 simonb
832 1.62 simonb (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
833 1.62 simonb evp->ev_group, evp->ev_name, evp->ev_count);
834 1.62 simonb }
835 1.62 simonb }
836 1.62 simonb #endif /* DDB */
837