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