xref: /freebsd/sys/kern/subr_bus.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997,1998,2003 Doug Rabson
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bus.h"
33 #include "opt_ddb.h"
34 
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/domainset.h>
38 #include <sys/eventhandler.h>
39 #include <sys/filio.h>
40 #include <sys/lock.h>
41 #include <sys/kernel.h>
42 #include <sys/kobj.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/poll.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/condvar.h>
51 #include <sys/queue.h>
52 #include <machine/bus.h>
53 #include <sys/random.h>
54 #include <sys/rman.h>
55 #include <sys/sbuf.h>
56 #include <sys/selinfo.h>
57 #include <sys/signalvar.h>
58 #include <sys/smp.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/uio.h>
62 #include <sys/bus.h>
63 #include <sys/cpuset.h>
64 
65 #include <net/vnet.h>
66 
67 #include <machine/cpu.h>
68 #include <machine/stdarg.h>
69 
70 #include <vm/uma.h>
71 #include <vm/vm.h>
72 
73 #include <ddb/ddb.h>
74 
75 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
76     NULL);
77 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
78     NULL);
79 
80 /*
81  * Used to attach drivers to devclasses.
82  */
83 typedef struct driverlink *driverlink_t;
84 struct driverlink {
85 	kobj_class_t	driver;
86 	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
87 	int		pass;
88 	int		flags;
89 #define DL_DEFERRED_PROBE	1	/* Probe deferred on this */
90 	TAILQ_ENTRY(driverlink) passlink;
91 };
92 
93 /*
94  * Forward declarations
95  */
96 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
97 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
98 typedef TAILQ_HEAD(device_list, device) device_list_t;
99 
100 struct devclass {
101 	TAILQ_ENTRY(devclass) link;
102 	devclass_t	parent;		/* parent in devclass hierarchy */
103 	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
104 	char		*name;
105 	device_t	*devices;	/* array of devices indexed by unit */
106 	int		maxunit;	/* size of devices array */
107 	int		flags;
108 #define DC_HAS_CHILDREN		1
109 
110 	struct sysctl_ctx_list sysctl_ctx;
111 	struct sysctl_oid *sysctl_tree;
112 };
113 
114 /**
115  * @brief Implementation of device.
116  */
117 struct device {
118 	/*
119 	 * A device is a kernel object. The first field must be the
120 	 * current ops table for the object.
121 	 */
122 	KOBJ_FIELDS;
123 
124 	/*
125 	 * Device hierarchy.
126 	 */
127 	TAILQ_ENTRY(device)	link;	/**< list of devices in parent */
128 	TAILQ_ENTRY(device)	devlink; /**< global device list membership */
129 	device_t	parent;		/**< parent of this device  */
130 	device_list_t	children;	/**< list of child devices */
131 
132 	/*
133 	 * Details of this device.
134 	 */
135 	driver_t	*driver;	/**< current driver */
136 	devclass_t	devclass;	/**< current device class */
137 	int		unit;		/**< current unit number */
138 	char*		nameunit;	/**< name+unit e.g. foodev0 */
139 	char*		desc;		/**< driver specific description */
140 	int		busy;		/**< count of calls to device_busy() */
141 	device_state_t	state;		/**< current device state  */
142 	uint32_t	devflags;	/**< api level flags for device_get_flags() */
143 	u_int		flags;		/**< internal device flags  */
144 	u_int	order;			/**< order from device_add_child_ordered() */
145 	void	*ivars;			/**< instance variables  */
146 	void	*softc;			/**< current driver's variables  */
147 
148 	struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables  */
149 	struct sysctl_oid *sysctl_tree;	/**< state for sysctl variables */
150 };
151 
152 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
153 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
154 
155 EVENTHANDLER_LIST_DEFINE(device_attach);
156 EVENTHANDLER_LIST_DEFINE(device_detach);
157 EVENTHANDLER_LIST_DEFINE(dev_lookup);
158 
159 static void devctl2_init(void);
160 static bool device_frozen;
161 
162 #define DRIVERNAME(d)	((d)? d->name : "no driver")
163 #define DEVCLANAME(d)	((d)? d->name : "no devclass")
164 
165 #ifdef BUS_DEBUG
166 
167 static int bus_debug = 1;
168 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
169     "Bus debug level");
170 
171 #define PDEBUG(a)	if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
172 #define DEVICENAME(d)	((d)? device_get_name(d): "no device")
173 
174 /**
175  * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
176  * prevent syslog from deleting initial spaces
177  */
178 #define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
179 
180 static void print_device_short(device_t dev, int indent);
181 static void print_device(device_t dev, int indent);
182 void print_device_tree_short(device_t dev, int indent);
183 void print_device_tree(device_t dev, int indent);
184 static void print_driver_short(driver_t *driver, int indent);
185 static void print_driver(driver_t *driver, int indent);
186 static void print_driver_list(driver_list_t drivers, int indent);
187 static void print_devclass_short(devclass_t dc, int indent);
188 static void print_devclass(devclass_t dc, int indent);
189 void print_devclass_list_short(void);
190 void print_devclass_list(void);
191 
192 #else
193 /* Make the compiler ignore the function calls */
194 #define PDEBUG(a)			/* nop */
195 #define DEVICENAME(d)			/* nop */
196 
197 #define print_device_short(d,i)		/* nop */
198 #define print_device(d,i)		/* nop */
199 #define print_device_tree_short(d,i)	/* nop */
200 #define print_device_tree(d,i)		/* nop */
201 #define print_driver_short(d,i)		/* nop */
202 #define print_driver(d,i)		/* nop */
203 #define print_driver_list(d,i)		/* nop */
204 #define print_devclass_short(d,i)	/* nop */
205 #define print_devclass(d,i)		/* nop */
206 #define print_devclass_list_short()	/* nop */
207 #define print_devclass_list()		/* nop */
208 #endif
209 
210 /*
211  * dev sysctl tree
212  */
213 
214 enum {
215 	DEVCLASS_SYSCTL_PARENT,
216 };
217 
218 static int
219 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
220 {
221 	devclass_t dc = (devclass_t)arg1;
222 	const char *value;
223 
224 	switch (arg2) {
225 	case DEVCLASS_SYSCTL_PARENT:
226 		value = dc->parent ? dc->parent->name : "";
227 		break;
228 	default:
229 		return (EINVAL);
230 	}
231 	return (SYSCTL_OUT_STR(req, value));
232 }
233 
234 static void
235 devclass_sysctl_init(devclass_t dc)
236 {
237 	if (dc->sysctl_tree != NULL)
238 		return;
239 	sysctl_ctx_init(&dc->sysctl_ctx);
240 	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
241 	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
242 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
243 	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
244 	    OID_AUTO, "%parent",
245 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
246 	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
247 	    "parent class");
248 }
249 
250 enum {
251 	DEVICE_SYSCTL_DESC,
252 	DEVICE_SYSCTL_DRIVER,
253 	DEVICE_SYSCTL_LOCATION,
254 	DEVICE_SYSCTL_PNPINFO,
255 	DEVICE_SYSCTL_PARENT,
256 };
257 
258 static int
259 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
260 {
261 	device_t dev = (device_t)arg1;
262 	const char *value;
263 	char *buf;
264 	int error;
265 
266 	buf = NULL;
267 	switch (arg2) {
268 	case DEVICE_SYSCTL_DESC:
269 		value = dev->desc ? dev->desc : "";
270 		break;
271 	case DEVICE_SYSCTL_DRIVER:
272 		value = dev->driver ? dev->driver->name : "";
273 		break;
274 	case DEVICE_SYSCTL_LOCATION:
275 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
276 		bus_child_location_str(dev, buf, 1024);
277 		break;
278 	case DEVICE_SYSCTL_PNPINFO:
279 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
280 		bus_child_pnpinfo_str(dev, buf, 1024);
281 		break;
282 	case DEVICE_SYSCTL_PARENT:
283 		value = dev->parent ? dev->parent->nameunit : "";
284 		break;
285 	default:
286 		return (EINVAL);
287 	}
288 	error = SYSCTL_OUT_STR(req, value);
289 	if (buf != NULL)
290 		free(buf, M_BUS);
291 	return (error);
292 }
293 
294 static void
295 device_sysctl_init(device_t dev)
296 {
297 	devclass_t dc = dev->devclass;
298 	int domain;
299 
300 	if (dev->sysctl_tree != NULL)
301 		return;
302 	devclass_sysctl_init(dc);
303 	sysctl_ctx_init(&dev->sysctl_ctx);
304 	dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
305 	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
306 	    dev->nameunit + strlen(dc->name),
307 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
308 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
309 	    OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
310 	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
311 	    "device description");
312 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
313 	    OID_AUTO, "%driver",
314 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
315 	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
316 	    "device driver name");
317 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
318 	    OID_AUTO, "%location",
319 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
320 	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
321 	    "device location relative to parent");
322 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
323 	    OID_AUTO, "%pnpinfo",
324 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
325 	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
326 	    "device identification");
327 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
328 	    OID_AUTO, "%parent",
329 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
330 	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
331 	    "parent device");
332 	if (bus_get_domain(dev, &domain) == 0)
333 		SYSCTL_ADD_INT(&dev->sysctl_ctx,
334 		    SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
335 		    CTLFLAG_RD, NULL, domain, "NUMA domain");
336 }
337 
338 static void
339 device_sysctl_update(device_t dev)
340 {
341 	devclass_t dc = dev->devclass;
342 
343 	if (dev->sysctl_tree == NULL)
344 		return;
345 	sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
346 }
347 
348 static void
349 device_sysctl_fini(device_t dev)
350 {
351 	if (dev->sysctl_tree == NULL)
352 		return;
353 	sysctl_ctx_free(&dev->sysctl_ctx);
354 	dev->sysctl_tree = NULL;
355 }
356 
357 /*
358  * /dev/devctl implementation
359  */
360 
361 /*
362  * This design allows only one reader for /dev/devctl.  This is not desirable
363  * in the long run, but will get a lot of hair out of this implementation.
364  * Maybe we should make this device a clonable device.
365  *
366  * Also note: we specifically do not attach a device to the device_t tree
367  * to avoid potential chicken and egg problems.  One could argue that all
368  * of this belongs to the root node.
369  */
370 
371 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
372 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
373 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
374 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
375     CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
376 
377 static d_open_t		devopen;
378 static d_close_t	devclose;
379 static d_read_t		devread;
380 static d_ioctl_t	devioctl;
381 static d_poll_t		devpoll;
382 static d_kqfilter_t	devkqfilter;
383 
384 static struct cdevsw dev_cdevsw = {
385 	.d_version =	D_VERSION,
386 	.d_open =	devopen,
387 	.d_close =	devclose,
388 	.d_read =	devread,
389 	.d_ioctl =	devioctl,
390 	.d_poll =	devpoll,
391 	.d_kqfilter =	devkqfilter,
392 	.d_name =	"devctl",
393 };
394 
395 struct dev_event_info {
396 	char *dei_data;
397 	STAILQ_ENTRY(dev_event_info) dei_link;
398 };
399 
400 STAILQ_HEAD(devq, dev_event_info);
401 
402 static struct dev_softc {
403 	int		inuse;
404 	int		nonblock;
405 	int		queued;
406 	int		async;
407 	struct mtx	mtx;
408 	struct cv	cv;
409 	struct selinfo	sel;
410 	struct devq	devq;
411 	struct sigio	*sigio;
412 } devsoftc;
413 
414 static void	filt_devctl_detach(struct knote *kn);
415 static int	filt_devctl_read(struct knote *kn, long hint);
416 
417 struct filterops devctl_rfiltops = {
418 	.f_isfd = 1,
419 	.f_detach = filt_devctl_detach,
420 	.f_event = filt_devctl_read,
421 };
422 
423 static struct cdev *devctl_dev;
424 
425 static void
426 devinit(void)
427 {
428 	devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
429 	    UID_ROOT, GID_WHEEL, 0600, "devctl");
430 	mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
431 	cv_init(&devsoftc.cv, "dev cv");
432 	STAILQ_INIT(&devsoftc.devq);
433 	knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
434 	devctl2_init();
435 }
436 
437 static int
438 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
439 {
440 	mtx_lock(&devsoftc.mtx);
441 	if (devsoftc.inuse) {
442 		mtx_unlock(&devsoftc.mtx);
443 		return (EBUSY);
444 	}
445 	/* move to init */
446 	devsoftc.inuse = 1;
447 	mtx_unlock(&devsoftc.mtx);
448 	return (0);
449 }
450 
451 static int
452 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
453 {
454 	mtx_lock(&devsoftc.mtx);
455 	devsoftc.inuse = 0;
456 	devsoftc.nonblock = 0;
457 	devsoftc.async = 0;
458 	cv_broadcast(&devsoftc.cv);
459 	funsetown(&devsoftc.sigio);
460 	mtx_unlock(&devsoftc.mtx);
461 	return (0);
462 }
463 
464 /*
465  * The read channel for this device is used to report changes to
466  * userland in realtime.  We are required to free the data as well as
467  * the n1 object because we allocate them separately.  Also note that
468  * we return one record at a time.  If you try to read this device a
469  * character at a time, you will lose the rest of the data.  Listening
470  * programs are expected to cope.
471  */
472 static int
473 devread(struct cdev *dev, struct uio *uio, int ioflag)
474 {
475 	struct dev_event_info *n1;
476 	int rv;
477 
478 	mtx_lock(&devsoftc.mtx);
479 	while (STAILQ_EMPTY(&devsoftc.devq)) {
480 		if (devsoftc.nonblock) {
481 			mtx_unlock(&devsoftc.mtx);
482 			return (EAGAIN);
483 		}
484 		rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
485 		if (rv) {
486 			/*
487 			 * Need to translate ERESTART to EINTR here? -- jake
488 			 */
489 			mtx_unlock(&devsoftc.mtx);
490 			return (rv);
491 		}
492 	}
493 	n1 = STAILQ_FIRST(&devsoftc.devq);
494 	STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
495 	devsoftc.queued--;
496 	mtx_unlock(&devsoftc.mtx);
497 	rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
498 	free(n1->dei_data, M_BUS);
499 	free(n1, M_BUS);
500 	return (rv);
501 }
502 
503 static	int
504 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
505 {
506 	switch (cmd) {
507 	case FIONBIO:
508 		if (*(int*)data)
509 			devsoftc.nonblock = 1;
510 		else
511 			devsoftc.nonblock = 0;
512 		return (0);
513 	case FIOASYNC:
514 		if (*(int*)data)
515 			devsoftc.async = 1;
516 		else
517 			devsoftc.async = 0;
518 		return (0);
519 	case FIOSETOWN:
520 		return fsetown(*(int *)data, &devsoftc.sigio);
521 	case FIOGETOWN:
522 		*(int *)data = fgetown(&devsoftc.sigio);
523 		return (0);
524 
525 		/* (un)Support for other fcntl() calls. */
526 	case FIOCLEX:
527 	case FIONCLEX:
528 	case FIONREAD:
529 	default:
530 		break;
531 	}
532 	return (ENOTTY);
533 }
534 
535 static	int
536 devpoll(struct cdev *dev, int events, struct thread *td)
537 {
538 	int	revents = 0;
539 
540 	mtx_lock(&devsoftc.mtx);
541 	if (events & (POLLIN | POLLRDNORM)) {
542 		if (!STAILQ_EMPTY(&devsoftc.devq))
543 			revents = events & (POLLIN | POLLRDNORM);
544 		else
545 			selrecord(td, &devsoftc.sel);
546 	}
547 	mtx_unlock(&devsoftc.mtx);
548 
549 	return (revents);
550 }
551 
552 static int
553 devkqfilter(struct cdev *dev, struct knote *kn)
554 {
555 	int error;
556 
557 	if (kn->kn_filter == EVFILT_READ) {
558 		kn->kn_fop = &devctl_rfiltops;
559 		knlist_add(&devsoftc.sel.si_note, kn, 0);
560 		error = 0;
561 	} else
562 		error = EINVAL;
563 	return (error);
564 }
565 
566 static void
567 filt_devctl_detach(struct knote *kn)
568 {
569 	knlist_remove(&devsoftc.sel.si_note, kn, 0);
570 }
571 
572 static int
573 filt_devctl_read(struct knote *kn, long hint)
574 {
575 	kn->kn_data = devsoftc.queued;
576 	return (kn->kn_data != 0);
577 }
578 
579 /**
580  * @brief Return whether the userland process is running
581  */
582 boolean_t
583 devctl_process_running(void)
584 {
585 	return (devsoftc.inuse == 1);
586 }
587 
588 /**
589  * @brief Queue data to be read from the devctl device
590  *
591  * Generic interface to queue data to the devctl device.  It is
592  * assumed that @p data is properly formatted.  It is further assumed
593  * that @p data is allocated using the M_BUS malloc type.
594  */
595 static void
596 devctl_queue_data_f(char *data, int flags)
597 {
598 	struct dev_event_info *n1 = NULL, *n2 = NULL;
599 
600 	if (strlen(data) == 0)
601 		goto out;
602 	if (devctl_queue_length == 0)
603 		goto out;
604 	n1 = malloc(sizeof(*n1), M_BUS, flags);
605 	if (n1 == NULL)
606 		goto out;
607 	n1->dei_data = data;
608 	mtx_lock(&devsoftc.mtx);
609 	if (devctl_queue_length == 0) {
610 		mtx_unlock(&devsoftc.mtx);
611 		free(n1->dei_data, M_BUS);
612 		free(n1, M_BUS);
613 		return;
614 	}
615 	/* Leave at least one spot in the queue... */
616 	while (devsoftc.queued > devctl_queue_length - 1) {
617 		n2 = STAILQ_FIRST(&devsoftc.devq);
618 		STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
619 		free(n2->dei_data, M_BUS);
620 		free(n2, M_BUS);
621 		devsoftc.queued--;
622 	}
623 	STAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
624 	devsoftc.queued++;
625 	cv_broadcast(&devsoftc.cv);
626 	KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
627 	mtx_unlock(&devsoftc.mtx);
628 	selwakeup(&devsoftc.sel);
629 	if (devsoftc.async && devsoftc.sigio != NULL)
630 		pgsigio(&devsoftc.sigio, SIGIO, 0);
631 	return;
632 out:
633 	/*
634 	 * We have to free data on all error paths since the caller
635 	 * assumes it will be free'd when this item is dequeued.
636 	 */
637 	free(data, M_BUS);
638 	return;
639 }
640 
641 static void
642 devctl_queue_data(char *data)
643 {
644 	devctl_queue_data_f(data, M_NOWAIT);
645 }
646 
647 /**
648  * @brief Send a 'notification' to userland, using standard ways
649  */
650 void
651 devctl_notify_f(const char *system, const char *subsystem, const char *type,
652     const char *data, int flags)
653 {
654 	int len = 0;
655 	char *msg;
656 
657 	if (system == NULL)
658 		return;		/* BOGUS!  Must specify system. */
659 	if (subsystem == NULL)
660 		return;		/* BOGUS!  Must specify subsystem. */
661 	if (type == NULL)
662 		return;		/* BOGUS!  Must specify type. */
663 	len += strlen(" system=") + strlen(system);
664 	len += strlen(" subsystem=") + strlen(subsystem);
665 	len += strlen(" type=") + strlen(type);
666 	/* add in the data message plus newline. */
667 	if (data != NULL)
668 		len += strlen(data);
669 	len += 3;	/* '!', '\n', and NUL */
670 	msg = malloc(len, M_BUS, flags);
671 	if (msg == NULL)
672 		return;		/* Drop it on the floor */
673 	if (data != NULL)
674 		snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
675 		    system, subsystem, type, data);
676 	else
677 		snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
678 		    system, subsystem, type);
679 	devctl_queue_data_f(msg, flags);
680 }
681 
682 void
683 devctl_notify(const char *system, const char *subsystem, const char *type,
684     const char *data)
685 {
686 	devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
687 }
688 
689 /*
690  * Common routine that tries to make sending messages as easy as possible.
691  * We allocate memory for the data, copy strings into that, but do not
692  * free it unless there's an error.  The dequeue part of the driver should
693  * free the data.  We don't send data when the device is disabled.  We do
694  * send data, even when we have no listeners, because we wish to avoid
695  * races relating to startup and restart of listening applications.
696  *
697  * devaddq is designed to string together the type of event, with the
698  * object of that event, plus the plug and play info and location info
699  * for that event.  This is likely most useful for devices, but less
700  * useful for other consumers of this interface.  Those should use
701  * the devctl_queue_data() interface instead.
702  */
703 static void
704 devaddq(const char *type, const char *what, device_t dev)
705 {
706 	char *data = NULL;
707 	char *loc = NULL;
708 	char *pnp = NULL;
709 	const char *parstr;
710 
711 	if (!devctl_queue_length)/* Rare race, but lost races safely discard */
712 		return;
713 	data = malloc(1024, M_BUS, M_NOWAIT);
714 	if (data == NULL)
715 		goto bad;
716 
717 	/* get the bus specific location of this device */
718 	loc = malloc(1024, M_BUS, M_NOWAIT);
719 	if (loc == NULL)
720 		goto bad;
721 	*loc = '\0';
722 	bus_child_location_str(dev, loc, 1024);
723 
724 	/* Get the bus specific pnp info of this device */
725 	pnp = malloc(1024, M_BUS, M_NOWAIT);
726 	if (pnp == NULL)
727 		goto bad;
728 	*pnp = '\0';
729 	bus_child_pnpinfo_str(dev, pnp, 1024);
730 
731 	/* Get the parent of this device, or / if high enough in the tree. */
732 	if (device_get_parent(dev) == NULL)
733 		parstr = ".";	/* Or '/' ? */
734 	else
735 		parstr = device_get_nameunit(device_get_parent(dev));
736 	/* String it all together. */
737 	snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
738 	  parstr);
739 	free(loc, M_BUS);
740 	free(pnp, M_BUS);
741 	devctl_queue_data(data);
742 	return;
743 bad:
744 	free(pnp, M_BUS);
745 	free(loc, M_BUS);
746 	free(data, M_BUS);
747 	return;
748 }
749 
750 /*
751  * A device was added to the tree.  We are called just after it successfully
752  * attaches (that is, probe and attach success for this device).  No call
753  * is made if a device is merely parented into the tree.  See devnomatch
754  * if probe fails.  If attach fails, no notification is sent (but maybe
755  * we should have a different message for this).
756  */
757 static void
758 devadded(device_t dev)
759 {
760 	devaddq("+", device_get_nameunit(dev), dev);
761 }
762 
763 /*
764  * A device was removed from the tree.  We are called just before this
765  * happens.
766  */
767 static void
768 devremoved(device_t dev)
769 {
770 	devaddq("-", device_get_nameunit(dev), dev);
771 }
772 
773 /*
774  * Called when there's no match for this device.  This is only called
775  * the first time that no match happens, so we don't keep getting this
776  * message.  Should that prove to be undesirable, we can change it.
777  * This is called when all drivers that can attach to a given bus
778  * decline to accept this device.  Other errors may not be detected.
779  */
780 static void
781 devnomatch(device_t dev)
782 {
783 	devaddq("?", "", dev);
784 }
785 
786 static int
787 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
788 {
789 	struct dev_event_info *n1;
790 	int q, error;
791 
792 	q = devctl_queue_length;
793 	error = sysctl_handle_int(oidp, &q, 0, req);
794 	if (error || !req->newptr)
795 		return (error);
796 	if (q < 0)
797 		return (EINVAL);
798 	if (mtx_initialized(&devsoftc.mtx))
799 		mtx_lock(&devsoftc.mtx);
800 	devctl_queue_length = q;
801 	while (devsoftc.queued > devctl_queue_length) {
802 		n1 = STAILQ_FIRST(&devsoftc.devq);
803 		STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
804 		free(n1->dei_data, M_BUS);
805 		free(n1, M_BUS);
806 		devsoftc.queued--;
807 	}
808 	if (mtx_initialized(&devsoftc.mtx))
809 		mtx_unlock(&devsoftc.mtx);
810 	return (0);
811 }
812 
813 /**
814  * @brief safely quotes strings that might have double quotes in them.
815  *
816  * The devctl protocol relies on quoted strings having matching quotes.
817  * This routine quotes any internal quotes so the resulting string
818  * is safe to pass to snprintf to construct, for example pnp info strings.
819  *
820  * @param sb	sbuf to place the characters into
821  * @param src	Original buffer.
822  */
823 void
824 devctl_safe_quote_sb(struct sbuf *sb, const char *src)
825 {
826 	while (*src != '\0') {
827 		if (*src == '"' || *src == '\\')
828 			sbuf_putc(sb, '\\');
829 		sbuf_putc(sb, *src++);
830 	}
831 }
832 
833 /* End of /dev/devctl code */
834 
835 static TAILQ_HEAD(,device)	bus_data_devices;
836 static int bus_data_generation = 1;
837 
838 static kobj_method_t null_methods[] = {
839 	KOBJMETHOD_END
840 };
841 
842 DEFINE_CLASS(null, null_methods, 0);
843 
844 /*
845  * Bus pass implementation
846  */
847 
848 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
849 int bus_current_pass = BUS_PASS_ROOT;
850 
851 /**
852  * @internal
853  * @brief Register the pass level of a new driver attachment
854  *
855  * Register a new driver attachment's pass level.  If no driver
856  * attachment with the same pass level has been added, then @p new
857  * will be added to the global passes list.
858  *
859  * @param new		the new driver attachment
860  */
861 static void
862 driver_register_pass(struct driverlink *new)
863 {
864 	struct driverlink *dl;
865 
866 	/* We only consider pass numbers during boot. */
867 	if (bus_current_pass == BUS_PASS_DEFAULT)
868 		return;
869 
870 	/*
871 	 * Walk the passes list.  If we already know about this pass
872 	 * then there is nothing to do.  If we don't, then insert this
873 	 * driver link into the list.
874 	 */
875 	TAILQ_FOREACH(dl, &passes, passlink) {
876 		if (dl->pass < new->pass)
877 			continue;
878 		if (dl->pass == new->pass)
879 			return;
880 		TAILQ_INSERT_BEFORE(dl, new, passlink);
881 		return;
882 	}
883 	TAILQ_INSERT_TAIL(&passes, new, passlink);
884 }
885 
886 /**
887  * @brief Raise the current bus pass
888  *
889  * Raise the current bus pass level to @p pass.  Call the BUS_NEW_PASS()
890  * method on the root bus to kick off a new device tree scan for each
891  * new pass level that has at least one driver.
892  */
893 void
894 bus_set_pass(int pass)
895 {
896 	struct driverlink *dl;
897 
898 	if (bus_current_pass > pass)
899 		panic("Attempt to lower bus pass level");
900 
901 	TAILQ_FOREACH(dl, &passes, passlink) {
902 		/* Skip pass values below the current pass level. */
903 		if (dl->pass <= bus_current_pass)
904 			continue;
905 
906 		/*
907 		 * Bail once we hit a driver with a pass level that is
908 		 * too high.
909 		 */
910 		if (dl->pass > pass)
911 			break;
912 
913 		/*
914 		 * Raise the pass level to the next level and rescan
915 		 * the tree.
916 		 */
917 		bus_current_pass = dl->pass;
918 		BUS_NEW_PASS(root_bus);
919 	}
920 
921 	/*
922 	 * If there isn't a driver registered for the requested pass,
923 	 * then bus_current_pass might still be less than 'pass'.  Set
924 	 * it to 'pass' in that case.
925 	 */
926 	if (bus_current_pass < pass)
927 		bus_current_pass = pass;
928 	KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
929 }
930 
931 /*
932  * Devclass implementation
933  */
934 
935 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
936 
937 /**
938  * @internal
939  * @brief Find or create a device class
940  *
941  * If a device class with the name @p classname exists, return it,
942  * otherwise if @p create is non-zero create and return a new device
943  * class.
944  *
945  * If @p parentname is non-NULL, the parent of the devclass is set to
946  * the devclass of that name.
947  *
948  * @param classname	the devclass name to find or create
949  * @param parentname	the parent devclass name or @c NULL
950  * @param create	non-zero to create a devclass
951  */
952 static devclass_t
953 devclass_find_internal(const char *classname, const char *parentname,
954 		       int create)
955 {
956 	devclass_t dc;
957 
958 	PDEBUG(("looking for %s", classname));
959 	if (!classname)
960 		return (NULL);
961 
962 	TAILQ_FOREACH(dc, &devclasses, link) {
963 		if (!strcmp(dc->name, classname))
964 			break;
965 	}
966 
967 	if (create && !dc) {
968 		PDEBUG(("creating %s", classname));
969 		dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
970 		    M_BUS, M_NOWAIT | M_ZERO);
971 		if (!dc)
972 			return (NULL);
973 		dc->parent = NULL;
974 		dc->name = (char*) (dc + 1);
975 		strcpy(dc->name, classname);
976 		TAILQ_INIT(&dc->drivers);
977 		TAILQ_INSERT_TAIL(&devclasses, dc, link);
978 
979 		bus_data_generation_update();
980 	}
981 
982 	/*
983 	 * If a parent class is specified, then set that as our parent so
984 	 * that this devclass will support drivers for the parent class as
985 	 * well.  If the parent class has the same name don't do this though
986 	 * as it creates a cycle that can trigger an infinite loop in
987 	 * device_probe_child() if a device exists for which there is no
988 	 * suitable driver.
989 	 */
990 	if (parentname && dc && !dc->parent &&
991 	    strcmp(classname, parentname) != 0) {
992 		dc->parent = devclass_find_internal(parentname, NULL, TRUE);
993 		dc->parent->flags |= DC_HAS_CHILDREN;
994 	}
995 
996 	return (dc);
997 }
998 
999 /**
1000  * @brief Create a device class
1001  *
1002  * If a device class with the name @p classname exists, return it,
1003  * otherwise create and return a new device class.
1004  *
1005  * @param classname	the devclass name to find or create
1006  */
1007 devclass_t
1008 devclass_create(const char *classname)
1009 {
1010 	return (devclass_find_internal(classname, NULL, TRUE));
1011 }
1012 
1013 /**
1014  * @brief Find a device class
1015  *
1016  * If a device class with the name @p classname exists, return it,
1017  * otherwise return @c NULL.
1018  *
1019  * @param classname	the devclass name to find
1020  */
1021 devclass_t
1022 devclass_find(const char *classname)
1023 {
1024 	return (devclass_find_internal(classname, NULL, FALSE));
1025 }
1026 
1027 /**
1028  * @brief Register that a device driver has been added to a devclass
1029  *
1030  * Register that a device driver has been added to a devclass.  This
1031  * is called by devclass_add_driver to accomplish the recursive
1032  * notification of all the children classes of dc, as well as dc.
1033  * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1034  * the devclass.
1035  *
1036  * We do a full search here of the devclass list at each iteration
1037  * level to save storing children-lists in the devclass structure.  If
1038  * we ever move beyond a few dozen devices doing this, we may need to
1039  * reevaluate...
1040  *
1041  * @param dc		the devclass to edit
1042  * @param driver	the driver that was just added
1043  */
1044 static void
1045 devclass_driver_added(devclass_t dc, driver_t *driver)
1046 {
1047 	devclass_t parent;
1048 	int i;
1049 
1050 	/*
1051 	 * Call BUS_DRIVER_ADDED for any existing buses in this class.
1052 	 */
1053 	for (i = 0; i < dc->maxunit; i++)
1054 		if (dc->devices[i] && device_is_attached(dc->devices[i]))
1055 			BUS_DRIVER_ADDED(dc->devices[i], driver);
1056 
1057 	/*
1058 	 * Walk through the children classes.  Since we only keep a
1059 	 * single parent pointer around, we walk the entire list of
1060 	 * devclasses looking for children.  We set the
1061 	 * DC_HAS_CHILDREN flag when a child devclass is created on
1062 	 * the parent, so we only walk the list for those devclasses
1063 	 * that have children.
1064 	 */
1065 	if (!(dc->flags & DC_HAS_CHILDREN))
1066 		return;
1067 	parent = dc;
1068 	TAILQ_FOREACH(dc, &devclasses, link) {
1069 		if (dc->parent == parent)
1070 			devclass_driver_added(dc, driver);
1071 	}
1072 }
1073 
1074 /**
1075  * @brief Add a device driver to a device class
1076  *
1077  * Add a device driver to a devclass. This is normally called
1078  * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1079  * all devices in the devclass will be called to allow them to attempt
1080  * to re-probe any unmatched children.
1081  *
1082  * @param dc		the devclass to edit
1083  * @param driver	the driver to register
1084  */
1085 int
1086 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1087 {
1088 	driverlink_t dl;
1089 	const char *parentname;
1090 
1091 	PDEBUG(("%s", DRIVERNAME(driver)));
1092 
1093 	/* Don't allow invalid pass values. */
1094 	if (pass <= BUS_PASS_ROOT)
1095 		return (EINVAL);
1096 
1097 	dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1098 	if (!dl)
1099 		return (ENOMEM);
1100 
1101 	/*
1102 	 * Compile the driver's methods. Also increase the reference count
1103 	 * so that the class doesn't get freed when the last instance
1104 	 * goes. This means we can safely use static methods and avoids a
1105 	 * double-free in devclass_delete_driver.
1106 	 */
1107 	kobj_class_compile((kobj_class_t) driver);
1108 
1109 	/*
1110 	 * If the driver has any base classes, make the
1111 	 * devclass inherit from the devclass of the driver's
1112 	 * first base class. This will allow the system to
1113 	 * search for drivers in both devclasses for children
1114 	 * of a device using this driver.
1115 	 */
1116 	if (driver->baseclasses)
1117 		parentname = driver->baseclasses[0]->name;
1118 	else
1119 		parentname = NULL;
1120 	*dcp = devclass_find_internal(driver->name, parentname, TRUE);
1121 
1122 	dl->driver = driver;
1123 	TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1124 	driver->refs++;		/* XXX: kobj_mtx */
1125 	dl->pass = pass;
1126 	driver_register_pass(dl);
1127 
1128 	if (device_frozen) {
1129 		dl->flags |= DL_DEFERRED_PROBE;
1130 	} else {
1131 		devclass_driver_added(dc, driver);
1132 	}
1133 	bus_data_generation_update();
1134 	return (0);
1135 }
1136 
1137 /**
1138  * @brief Register that a device driver has been deleted from a devclass
1139  *
1140  * Register that a device driver has been removed from a devclass.
1141  * This is called by devclass_delete_driver to accomplish the
1142  * recursive notification of all the children classes of busclass, as
1143  * well as busclass.  Each layer will attempt to detach the driver
1144  * from any devices that are children of the bus's devclass.  The function
1145  * will return an error if a device fails to detach.
1146  *
1147  * We do a full search here of the devclass list at each iteration
1148  * level to save storing children-lists in the devclass structure.  If
1149  * we ever move beyond a few dozen devices doing this, we may need to
1150  * reevaluate...
1151  *
1152  * @param busclass	the devclass of the parent bus
1153  * @param dc		the devclass of the driver being deleted
1154  * @param driver	the driver being deleted
1155  */
1156 static int
1157 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1158 {
1159 	devclass_t parent;
1160 	device_t dev;
1161 	int error, i;
1162 
1163 	/*
1164 	 * Disassociate from any devices.  We iterate through all the
1165 	 * devices in the devclass of the driver and detach any which are
1166 	 * using the driver and which have a parent in the devclass which
1167 	 * we are deleting from.
1168 	 *
1169 	 * Note that since a driver can be in multiple devclasses, we
1170 	 * should not detach devices which are not children of devices in
1171 	 * the affected devclass.
1172 	 *
1173 	 * If we're frozen, we don't generate NOMATCH events. Mark to
1174 	 * generate later.
1175 	 */
1176 	for (i = 0; i < dc->maxunit; i++) {
1177 		if (dc->devices[i]) {
1178 			dev = dc->devices[i];
1179 			if (dev->driver == driver && dev->parent &&
1180 			    dev->parent->devclass == busclass) {
1181 				if ((error = device_detach(dev)) != 0)
1182 					return (error);
1183 				if (device_frozen) {
1184 					dev->flags &= ~DF_DONENOMATCH;
1185 					dev->flags |= DF_NEEDNOMATCH;
1186 				} else {
1187 					BUS_PROBE_NOMATCH(dev->parent, dev);
1188 					devnomatch(dev);
1189 					dev->flags |= DF_DONENOMATCH;
1190 				}
1191 			}
1192 		}
1193 	}
1194 
1195 	/*
1196 	 * Walk through the children classes.  Since we only keep a
1197 	 * single parent pointer around, we walk the entire list of
1198 	 * devclasses looking for children.  We set the
1199 	 * DC_HAS_CHILDREN flag when a child devclass is created on
1200 	 * the parent, so we only walk the list for those devclasses
1201 	 * that have children.
1202 	 */
1203 	if (!(busclass->flags & DC_HAS_CHILDREN))
1204 		return (0);
1205 	parent = busclass;
1206 	TAILQ_FOREACH(busclass, &devclasses, link) {
1207 		if (busclass->parent == parent) {
1208 			error = devclass_driver_deleted(busclass, dc, driver);
1209 			if (error)
1210 				return (error);
1211 		}
1212 	}
1213 	return (0);
1214 }
1215 
1216 /**
1217  * @brief Delete a device driver from a device class
1218  *
1219  * Delete a device driver from a devclass. This is normally called
1220  * automatically by DRIVER_MODULE().
1221  *
1222  * If the driver is currently attached to any devices,
1223  * devclass_delete_driver() will first attempt to detach from each
1224  * device. If one of the detach calls fails, the driver will not be
1225  * deleted.
1226  *
1227  * @param dc		the devclass to edit
1228  * @param driver	the driver to unregister
1229  */
1230 int
1231 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1232 {
1233 	devclass_t dc = devclass_find(driver->name);
1234 	driverlink_t dl;
1235 	int error;
1236 
1237 	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1238 
1239 	if (!dc)
1240 		return (0);
1241 
1242 	/*
1243 	 * Find the link structure in the bus' list of drivers.
1244 	 */
1245 	TAILQ_FOREACH(dl, &busclass->drivers, link) {
1246 		if (dl->driver == driver)
1247 			break;
1248 	}
1249 
1250 	if (!dl) {
1251 		PDEBUG(("%s not found in %s list", driver->name,
1252 		    busclass->name));
1253 		return (ENOENT);
1254 	}
1255 
1256 	error = devclass_driver_deleted(busclass, dc, driver);
1257 	if (error != 0)
1258 		return (error);
1259 
1260 	TAILQ_REMOVE(&busclass->drivers, dl, link);
1261 	free(dl, M_BUS);
1262 
1263 	/* XXX: kobj_mtx */
1264 	driver->refs--;
1265 	if (driver->refs == 0)
1266 		kobj_class_free((kobj_class_t) driver);
1267 
1268 	bus_data_generation_update();
1269 	return (0);
1270 }
1271 
1272 /**
1273  * @brief Quiesces a set of device drivers from a device class
1274  *
1275  * Quiesce a device driver from a devclass. This is normally called
1276  * automatically by DRIVER_MODULE().
1277  *
1278  * If the driver is currently attached to any devices,
1279  * devclass_quiesece_driver() will first attempt to quiesce each
1280  * device.
1281  *
1282  * @param dc		the devclass to edit
1283  * @param driver	the driver to unregister
1284  */
1285 static int
1286 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1287 {
1288 	devclass_t dc = devclass_find(driver->name);
1289 	driverlink_t dl;
1290 	device_t dev;
1291 	int i;
1292 	int error;
1293 
1294 	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1295 
1296 	if (!dc)
1297 		return (0);
1298 
1299 	/*
1300 	 * Find the link structure in the bus' list of drivers.
1301 	 */
1302 	TAILQ_FOREACH(dl, &busclass->drivers, link) {
1303 		if (dl->driver == driver)
1304 			break;
1305 	}
1306 
1307 	if (!dl) {
1308 		PDEBUG(("%s not found in %s list", driver->name,
1309 		    busclass->name));
1310 		return (ENOENT);
1311 	}
1312 
1313 	/*
1314 	 * Quiesce all devices.  We iterate through all the devices in
1315 	 * the devclass of the driver and quiesce any which are using
1316 	 * the driver and which have a parent in the devclass which we
1317 	 * are quiescing.
1318 	 *
1319 	 * Note that since a driver can be in multiple devclasses, we
1320 	 * should not quiesce devices which are not children of
1321 	 * devices in the affected devclass.
1322 	 */
1323 	for (i = 0; i < dc->maxunit; i++) {
1324 		if (dc->devices[i]) {
1325 			dev = dc->devices[i];
1326 			if (dev->driver == driver && dev->parent &&
1327 			    dev->parent->devclass == busclass) {
1328 				if ((error = device_quiesce(dev)) != 0)
1329 					return (error);
1330 			}
1331 		}
1332 	}
1333 
1334 	return (0);
1335 }
1336 
1337 /**
1338  * @internal
1339  */
1340 static driverlink_t
1341 devclass_find_driver_internal(devclass_t dc, const char *classname)
1342 {
1343 	driverlink_t dl;
1344 
1345 	PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1346 
1347 	TAILQ_FOREACH(dl, &dc->drivers, link) {
1348 		if (!strcmp(dl->driver->name, classname))
1349 			return (dl);
1350 	}
1351 
1352 	PDEBUG(("not found"));
1353 	return (NULL);
1354 }
1355 
1356 /**
1357  * @brief Return the name of the devclass
1358  */
1359 const char *
1360 devclass_get_name(devclass_t dc)
1361 {
1362 	return (dc->name);
1363 }
1364 
1365 /**
1366  * @brief Find a device given a unit number
1367  *
1368  * @param dc		the devclass to search
1369  * @param unit		the unit number to search for
1370  *
1371  * @returns		the device with the given unit number or @c
1372  *			NULL if there is no such device
1373  */
1374 device_t
1375 devclass_get_device(devclass_t dc, int unit)
1376 {
1377 	if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1378 		return (NULL);
1379 	return (dc->devices[unit]);
1380 }
1381 
1382 /**
1383  * @brief Find the softc field of a device given a unit number
1384  *
1385  * @param dc		the devclass to search
1386  * @param unit		the unit number to search for
1387  *
1388  * @returns		the softc field of the device with the given
1389  *			unit number or @c NULL if there is no such
1390  *			device
1391  */
1392 void *
1393 devclass_get_softc(devclass_t dc, int unit)
1394 {
1395 	device_t dev;
1396 
1397 	dev = devclass_get_device(dc, unit);
1398 	if (!dev)
1399 		return (NULL);
1400 
1401 	return (device_get_softc(dev));
1402 }
1403 
1404 /**
1405  * @brief Get a list of devices in the devclass
1406  *
1407  * An array containing a list of all the devices in the given devclass
1408  * is allocated and returned in @p *devlistp. The number of devices
1409  * in the array is returned in @p *devcountp. The caller should free
1410  * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1411  *
1412  * @param dc		the devclass to examine
1413  * @param devlistp	points at location for array pointer return
1414  *			value
1415  * @param devcountp	points at location for array size return value
1416  *
1417  * @retval 0		success
1418  * @retval ENOMEM	the array allocation failed
1419  */
1420 int
1421 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1422 {
1423 	int count, i;
1424 	device_t *list;
1425 
1426 	count = devclass_get_count(dc);
1427 	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1428 	if (!list)
1429 		return (ENOMEM);
1430 
1431 	count = 0;
1432 	for (i = 0; i < dc->maxunit; i++) {
1433 		if (dc->devices[i]) {
1434 			list[count] = dc->devices[i];
1435 			count++;
1436 		}
1437 	}
1438 
1439 	*devlistp = list;
1440 	*devcountp = count;
1441 
1442 	return (0);
1443 }
1444 
1445 /**
1446  * @brief Get a list of drivers in the devclass
1447  *
1448  * An array containing a list of pointers to all the drivers in the
1449  * given devclass is allocated and returned in @p *listp.  The number
1450  * of drivers in the array is returned in @p *countp. The caller should
1451  * free the array using @c free(p, M_TEMP).
1452  *
1453  * @param dc		the devclass to examine
1454  * @param listp		gives location for array pointer return value
1455  * @param countp	gives location for number of array elements
1456  *			return value
1457  *
1458  * @retval 0		success
1459  * @retval ENOMEM	the array allocation failed
1460  */
1461 int
1462 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1463 {
1464 	driverlink_t dl;
1465 	driver_t **list;
1466 	int count;
1467 
1468 	count = 0;
1469 	TAILQ_FOREACH(dl, &dc->drivers, link)
1470 		count++;
1471 	list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1472 	if (list == NULL)
1473 		return (ENOMEM);
1474 
1475 	count = 0;
1476 	TAILQ_FOREACH(dl, &dc->drivers, link) {
1477 		list[count] = dl->driver;
1478 		count++;
1479 	}
1480 	*listp = list;
1481 	*countp = count;
1482 
1483 	return (0);
1484 }
1485 
1486 /**
1487  * @brief Get the number of devices in a devclass
1488  *
1489  * @param dc		the devclass to examine
1490  */
1491 int
1492 devclass_get_count(devclass_t dc)
1493 {
1494 	int count, i;
1495 
1496 	count = 0;
1497 	for (i = 0; i < dc->maxunit; i++)
1498 		if (dc->devices[i])
1499 			count++;
1500 	return (count);
1501 }
1502 
1503 /**
1504  * @brief Get the maximum unit number used in a devclass
1505  *
1506  * Note that this is one greater than the highest currently-allocated
1507  * unit.  If a null devclass_t is passed in, -1 is returned to indicate
1508  * that not even the devclass has been allocated yet.
1509  *
1510  * @param dc		the devclass to examine
1511  */
1512 int
1513 devclass_get_maxunit(devclass_t dc)
1514 {
1515 	if (dc == NULL)
1516 		return (-1);
1517 	return (dc->maxunit);
1518 }
1519 
1520 /**
1521  * @brief Find a free unit number in a devclass
1522  *
1523  * This function searches for the first unused unit number greater
1524  * that or equal to @p unit.
1525  *
1526  * @param dc		the devclass to examine
1527  * @param unit		the first unit number to check
1528  */
1529 int
1530 devclass_find_free_unit(devclass_t dc, int unit)
1531 {
1532 	if (dc == NULL)
1533 		return (unit);
1534 	while (unit < dc->maxunit && dc->devices[unit] != NULL)
1535 		unit++;
1536 	return (unit);
1537 }
1538 
1539 /**
1540  * @brief Set the parent of a devclass
1541  *
1542  * The parent class is normally initialised automatically by
1543  * DRIVER_MODULE().
1544  *
1545  * @param dc		the devclass to edit
1546  * @param pdc		the new parent devclass
1547  */
1548 void
1549 devclass_set_parent(devclass_t dc, devclass_t pdc)
1550 {
1551 	dc->parent = pdc;
1552 }
1553 
1554 /**
1555  * @brief Get the parent of a devclass
1556  *
1557  * @param dc		the devclass to examine
1558  */
1559 devclass_t
1560 devclass_get_parent(devclass_t dc)
1561 {
1562 	return (dc->parent);
1563 }
1564 
1565 struct sysctl_ctx_list *
1566 devclass_get_sysctl_ctx(devclass_t dc)
1567 {
1568 	return (&dc->sysctl_ctx);
1569 }
1570 
1571 struct sysctl_oid *
1572 devclass_get_sysctl_tree(devclass_t dc)
1573 {
1574 	return (dc->sysctl_tree);
1575 }
1576 
1577 /**
1578  * @internal
1579  * @brief Allocate a unit number
1580  *
1581  * On entry, @p *unitp is the desired unit number (or @c -1 if any
1582  * will do). The allocated unit number is returned in @p *unitp.
1583 
1584  * @param dc		the devclass to allocate from
1585  * @param unitp		points at the location for the allocated unit
1586  *			number
1587  *
1588  * @retval 0		success
1589  * @retval EEXIST	the requested unit number is already allocated
1590  * @retval ENOMEM	memory allocation failure
1591  */
1592 static int
1593 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1594 {
1595 	const char *s;
1596 	int unit = *unitp;
1597 
1598 	PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1599 
1600 	/* Ask the parent bus if it wants to wire this device. */
1601 	if (unit == -1)
1602 		BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1603 		    &unit);
1604 
1605 	/* If we were given a wired unit number, check for existing device */
1606 	/* XXX imp XXX */
1607 	if (unit != -1) {
1608 		if (unit >= 0 && unit < dc->maxunit &&
1609 		    dc->devices[unit] != NULL) {
1610 			if (bootverbose)
1611 				printf("%s: %s%d already exists; skipping it\n",
1612 				    dc->name, dc->name, *unitp);
1613 			return (EEXIST);
1614 		}
1615 	} else {
1616 		/* Unwired device, find the next available slot for it */
1617 		unit = 0;
1618 		for (unit = 0;; unit++) {
1619 			/* If there is an "at" hint for a unit then skip it. */
1620 			if (resource_string_value(dc->name, unit, "at", &s) ==
1621 			    0)
1622 				continue;
1623 
1624 			/* If this device slot is already in use, skip it. */
1625 			if (unit < dc->maxunit && dc->devices[unit] != NULL)
1626 				continue;
1627 
1628 			break;
1629 		}
1630 	}
1631 
1632 	/*
1633 	 * We've selected a unit beyond the length of the table, so let's
1634 	 * extend the table to make room for all units up to and including
1635 	 * this one.
1636 	 */
1637 	if (unit >= dc->maxunit) {
1638 		device_t *newlist, *oldlist;
1639 		int newsize;
1640 
1641 		oldlist = dc->devices;
1642 		newsize = roundup((unit + 1),
1643 		    MAX(1, MINALLOCSIZE / sizeof(device_t)));
1644 		newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1645 		if (!newlist)
1646 			return (ENOMEM);
1647 		if (oldlist != NULL)
1648 			bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1649 		bzero(newlist + dc->maxunit,
1650 		    sizeof(device_t) * (newsize - dc->maxunit));
1651 		dc->devices = newlist;
1652 		dc->maxunit = newsize;
1653 		if (oldlist != NULL)
1654 			free(oldlist, M_BUS);
1655 	}
1656 	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1657 
1658 	*unitp = unit;
1659 	return (0);
1660 }
1661 
1662 /**
1663  * @internal
1664  * @brief Add a device to a devclass
1665  *
1666  * A unit number is allocated for the device (using the device's
1667  * preferred unit number if any) and the device is registered in the
1668  * devclass. This allows the device to be looked up by its unit
1669  * number, e.g. by decoding a dev_t minor number.
1670  *
1671  * @param dc		the devclass to add to
1672  * @param dev		the device to add
1673  *
1674  * @retval 0		success
1675  * @retval EEXIST	the requested unit number is already allocated
1676  * @retval ENOMEM	memory allocation failure
1677  */
1678 static int
1679 devclass_add_device(devclass_t dc, device_t dev)
1680 {
1681 	int buflen, error;
1682 
1683 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1684 
1685 	buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1686 	if (buflen < 0)
1687 		return (ENOMEM);
1688 	dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1689 	if (!dev->nameunit)
1690 		return (ENOMEM);
1691 
1692 	if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1693 		free(dev->nameunit, M_BUS);
1694 		dev->nameunit = NULL;
1695 		return (error);
1696 	}
1697 	dc->devices[dev->unit] = dev;
1698 	dev->devclass = dc;
1699 	snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1700 
1701 	return (0);
1702 }
1703 
1704 /**
1705  * @internal
1706  * @brief Delete a device from a devclass
1707  *
1708  * The device is removed from the devclass's device list and its unit
1709  * number is freed.
1710 
1711  * @param dc		the devclass to delete from
1712  * @param dev		the device to delete
1713  *
1714  * @retval 0		success
1715  */
1716 static int
1717 devclass_delete_device(devclass_t dc, device_t dev)
1718 {
1719 	if (!dc || !dev)
1720 		return (0);
1721 
1722 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1723 
1724 	if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1725 		panic("devclass_delete_device: inconsistent device class");
1726 	dc->devices[dev->unit] = NULL;
1727 	if (dev->flags & DF_WILDCARD)
1728 		dev->unit = -1;
1729 	dev->devclass = NULL;
1730 	free(dev->nameunit, M_BUS);
1731 	dev->nameunit = NULL;
1732 
1733 	return (0);
1734 }
1735 
1736 /**
1737  * @internal
1738  * @brief Make a new device and add it as a child of @p parent
1739  *
1740  * @param parent	the parent of the new device
1741  * @param name		the devclass name of the new device or @c NULL
1742  *			to leave the devclass unspecified
1743  * @parem unit		the unit number of the new device of @c -1 to
1744  *			leave the unit number unspecified
1745  *
1746  * @returns the new device
1747  */
1748 static device_t
1749 make_device(device_t parent, const char *name, int unit)
1750 {
1751 	device_t dev;
1752 	devclass_t dc;
1753 
1754 	PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1755 
1756 	if (name) {
1757 		dc = devclass_find_internal(name, NULL, TRUE);
1758 		if (!dc) {
1759 			printf("make_device: can't find device class %s\n",
1760 			    name);
1761 			return (NULL);
1762 		}
1763 	} else {
1764 		dc = NULL;
1765 	}
1766 
1767 	dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1768 	if (!dev)
1769 		return (NULL);
1770 
1771 	dev->parent = parent;
1772 	TAILQ_INIT(&dev->children);
1773 	kobj_init((kobj_t) dev, &null_class);
1774 	dev->driver = NULL;
1775 	dev->devclass = NULL;
1776 	dev->unit = unit;
1777 	dev->nameunit = NULL;
1778 	dev->desc = NULL;
1779 	dev->busy = 0;
1780 	dev->devflags = 0;
1781 	dev->flags = DF_ENABLED;
1782 	dev->order = 0;
1783 	if (unit == -1)
1784 		dev->flags |= DF_WILDCARD;
1785 	if (name) {
1786 		dev->flags |= DF_FIXEDCLASS;
1787 		if (devclass_add_device(dc, dev)) {
1788 			kobj_delete((kobj_t) dev, M_BUS);
1789 			return (NULL);
1790 		}
1791 	}
1792 	if (parent != NULL && device_has_quiet_children(parent))
1793 		dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1794 	dev->ivars = NULL;
1795 	dev->softc = NULL;
1796 
1797 	dev->state = DS_NOTPRESENT;
1798 
1799 	TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1800 	bus_data_generation_update();
1801 
1802 	return (dev);
1803 }
1804 
1805 /**
1806  * @internal
1807  * @brief Print a description of a device.
1808  */
1809 static int
1810 device_print_child(device_t dev, device_t child)
1811 {
1812 	int retval = 0;
1813 
1814 	if (device_is_alive(child))
1815 		retval += BUS_PRINT_CHILD(dev, child);
1816 	else
1817 		retval += device_printf(child, " not found\n");
1818 
1819 	return (retval);
1820 }
1821 
1822 /**
1823  * @brief Create a new device
1824  *
1825  * This creates a new device and adds it as a child of an existing
1826  * parent device. The new device will be added after the last existing
1827  * child with order zero.
1828  *
1829  * @param dev		the device which will be the parent of the
1830  *			new child device
1831  * @param name		devclass name for new device or @c NULL if not
1832  *			specified
1833  * @param unit		unit number for new device or @c -1 if not
1834  *			specified
1835  *
1836  * @returns		the new device
1837  */
1838 device_t
1839 device_add_child(device_t dev, const char *name, int unit)
1840 {
1841 	return (device_add_child_ordered(dev, 0, name, unit));
1842 }
1843 
1844 /**
1845  * @brief Create a new device
1846  *
1847  * This creates a new device and adds it as a child of an existing
1848  * parent device. The new device will be added after the last existing
1849  * child with the same order.
1850  *
1851  * @param dev		the device which will be the parent of the
1852  *			new child device
1853  * @param order		a value which is used to partially sort the
1854  *			children of @p dev - devices created using
1855  *			lower values of @p order appear first in @p
1856  *			dev's list of children
1857  * @param name		devclass name for new device or @c NULL if not
1858  *			specified
1859  * @param unit		unit number for new device or @c -1 if not
1860  *			specified
1861  *
1862  * @returns		the new device
1863  */
1864 device_t
1865 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1866 {
1867 	device_t child;
1868 	device_t place;
1869 
1870 	PDEBUG(("%s at %s with order %u as unit %d",
1871 	    name, DEVICENAME(dev), order, unit));
1872 	KASSERT(name != NULL || unit == -1,
1873 	    ("child device with wildcard name and specific unit number"));
1874 
1875 	child = make_device(dev, name, unit);
1876 	if (child == NULL)
1877 		return (child);
1878 	child->order = order;
1879 
1880 	TAILQ_FOREACH(place, &dev->children, link) {
1881 		if (place->order > order)
1882 			break;
1883 	}
1884 
1885 	if (place) {
1886 		/*
1887 		 * The device 'place' is the first device whose order is
1888 		 * greater than the new child.
1889 		 */
1890 		TAILQ_INSERT_BEFORE(place, child, link);
1891 	} else {
1892 		/*
1893 		 * The new child's order is greater or equal to the order of
1894 		 * any existing device. Add the child to the tail of the list.
1895 		 */
1896 		TAILQ_INSERT_TAIL(&dev->children, child, link);
1897 	}
1898 
1899 	bus_data_generation_update();
1900 	return (child);
1901 }
1902 
1903 /**
1904  * @brief Delete a device
1905  *
1906  * This function deletes a device along with all of its children. If
1907  * the device currently has a driver attached to it, the device is
1908  * detached first using device_detach().
1909  *
1910  * @param dev		the parent device
1911  * @param child		the device to delete
1912  *
1913  * @retval 0		success
1914  * @retval non-zero	a unit error code describing the error
1915  */
1916 int
1917 device_delete_child(device_t dev, device_t child)
1918 {
1919 	int error;
1920 	device_t grandchild;
1921 
1922 	PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1923 
1924 	/* detach parent before deleting children, if any */
1925 	if ((error = device_detach(child)) != 0)
1926 		return (error);
1927 
1928 	/* remove children second */
1929 	while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1930 		error = device_delete_child(child, grandchild);
1931 		if (error)
1932 			return (error);
1933 	}
1934 
1935 	if (child->devclass)
1936 		devclass_delete_device(child->devclass, child);
1937 	if (child->parent)
1938 		BUS_CHILD_DELETED(dev, child);
1939 	TAILQ_REMOVE(&dev->children, child, link);
1940 	TAILQ_REMOVE(&bus_data_devices, child, devlink);
1941 	kobj_delete((kobj_t) child, M_BUS);
1942 
1943 	bus_data_generation_update();
1944 	return (0);
1945 }
1946 
1947 /**
1948  * @brief Delete all children devices of the given device, if any.
1949  *
1950  * This function deletes all children devices of the given device, if
1951  * any, using the device_delete_child() function for each device it
1952  * finds. If a child device cannot be deleted, this function will
1953  * return an error code.
1954  *
1955  * @param dev		the parent device
1956  *
1957  * @retval 0		success
1958  * @retval non-zero	a device would not detach
1959  */
1960 int
1961 device_delete_children(device_t dev)
1962 {
1963 	device_t child;
1964 	int error;
1965 
1966 	PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1967 
1968 	error = 0;
1969 
1970 	while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1971 		error = device_delete_child(dev, child);
1972 		if (error) {
1973 			PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1974 			break;
1975 		}
1976 	}
1977 	return (error);
1978 }
1979 
1980 /**
1981  * @brief Find a device given a unit number
1982  *
1983  * This is similar to devclass_get_devices() but only searches for
1984  * devices which have @p dev as a parent.
1985  *
1986  * @param dev		the parent device to search
1987  * @param unit		the unit number to search for.  If the unit is -1,
1988  *			return the first child of @p dev which has name
1989  *			@p classname (that is, the one with the lowest unit.)
1990  *
1991  * @returns		the device with the given unit number or @c
1992  *			NULL if there is no such device
1993  */
1994 device_t
1995 device_find_child(device_t dev, const char *classname, int unit)
1996 {
1997 	devclass_t dc;
1998 	device_t child;
1999 
2000 	dc = devclass_find(classname);
2001 	if (!dc)
2002 		return (NULL);
2003 
2004 	if (unit != -1) {
2005 		child = devclass_get_device(dc, unit);
2006 		if (child && child->parent == dev)
2007 			return (child);
2008 	} else {
2009 		for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2010 			child = devclass_get_device(dc, unit);
2011 			if (child && child->parent == dev)
2012 				return (child);
2013 		}
2014 	}
2015 	return (NULL);
2016 }
2017 
2018 /**
2019  * @internal
2020  */
2021 static driverlink_t
2022 first_matching_driver(devclass_t dc, device_t dev)
2023 {
2024 	if (dev->devclass)
2025 		return (devclass_find_driver_internal(dc, dev->devclass->name));
2026 	return (TAILQ_FIRST(&dc->drivers));
2027 }
2028 
2029 /**
2030  * @internal
2031  */
2032 static driverlink_t
2033 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2034 {
2035 	if (dev->devclass) {
2036 		driverlink_t dl;
2037 		for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2038 			if (!strcmp(dev->devclass->name, dl->driver->name))
2039 				return (dl);
2040 		return (NULL);
2041 	}
2042 	return (TAILQ_NEXT(last, link));
2043 }
2044 
2045 /**
2046  * @internal
2047  */
2048 int
2049 device_probe_child(device_t dev, device_t child)
2050 {
2051 	devclass_t dc;
2052 	driverlink_t best = NULL;
2053 	driverlink_t dl;
2054 	int result, pri = 0;
2055 	int hasclass = (child->devclass != NULL);
2056 
2057 	GIANT_REQUIRED;
2058 
2059 	dc = dev->devclass;
2060 	if (!dc)
2061 		panic("device_probe_child: parent device has no devclass");
2062 
2063 	/*
2064 	 * If the state is already probed, then return.  However, don't
2065 	 * return if we can rebid this object.
2066 	 */
2067 	if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2068 		return (0);
2069 
2070 	for (; dc; dc = dc->parent) {
2071 		for (dl = first_matching_driver(dc, child);
2072 		     dl;
2073 		     dl = next_matching_driver(dc, child, dl)) {
2074 			/* If this driver's pass is too high, then ignore it. */
2075 			if (dl->pass > bus_current_pass)
2076 				continue;
2077 
2078 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2079 			result = device_set_driver(child, dl->driver);
2080 			if (result == ENOMEM)
2081 				return (result);
2082 			else if (result != 0)
2083 				continue;
2084 			if (!hasclass) {
2085 				if (device_set_devclass(child,
2086 				    dl->driver->name) != 0) {
2087 					char const * devname =
2088 					    device_get_name(child);
2089 					if (devname == NULL)
2090 						devname = "(unknown)";
2091 					printf("driver bug: Unable to set "
2092 					    "devclass (class: %s "
2093 					    "devname: %s)\n",
2094 					    dl->driver->name,
2095 					    devname);
2096 					(void)device_set_driver(child, NULL);
2097 					continue;
2098 				}
2099 			}
2100 
2101 			/* Fetch any flags for the device before probing. */
2102 			resource_int_value(dl->driver->name, child->unit,
2103 			    "flags", &child->devflags);
2104 
2105 			result = DEVICE_PROBE(child);
2106 
2107 			/* Reset flags and devclass before the next probe. */
2108 			child->devflags = 0;
2109 			if (!hasclass)
2110 				(void)device_set_devclass(child, NULL);
2111 
2112 			/*
2113 			 * If the driver returns SUCCESS, there can be
2114 			 * no higher match for this device.
2115 			 */
2116 			if (result == 0) {
2117 				best = dl;
2118 				pri = 0;
2119 				break;
2120 			}
2121 
2122 			/*
2123 			 * Reset DF_QUIET in case this driver doesn't
2124 			 * end up as the best driver.
2125 			 */
2126 			device_verbose(child);
2127 
2128 			/*
2129 			 * Probes that return BUS_PROBE_NOWILDCARD or lower
2130 			 * only match on devices whose driver was explicitly
2131 			 * specified.
2132 			 */
2133 			if (result <= BUS_PROBE_NOWILDCARD &&
2134 			    !(child->flags & DF_FIXEDCLASS)) {
2135 				result = ENXIO;
2136 			}
2137 
2138 			/*
2139 			 * The driver returned an error so it
2140 			 * certainly doesn't match.
2141 			 */
2142 			if (result > 0) {
2143 				(void)device_set_driver(child, NULL);
2144 				continue;
2145 			}
2146 
2147 			/*
2148 			 * A priority lower than SUCCESS, remember the
2149 			 * best matching driver. Initialise the value
2150 			 * of pri for the first match.
2151 			 */
2152 			if (best == NULL || result > pri) {
2153 				best = dl;
2154 				pri = result;
2155 				continue;
2156 			}
2157 		}
2158 		/*
2159 		 * If we have an unambiguous match in this devclass,
2160 		 * don't look in the parent.
2161 		 */
2162 		if (best && pri == 0)
2163 			break;
2164 	}
2165 
2166 	/*
2167 	 * If we found a driver, change state and initialise the devclass.
2168 	 */
2169 	/* XXX What happens if we rebid and got no best? */
2170 	if (best) {
2171 		/*
2172 		 * If this device was attached, and we were asked to
2173 		 * rescan, and it is a different driver, then we have
2174 		 * to detach the old driver and reattach this new one.
2175 		 * Note, we don't have to check for DF_REBID here
2176 		 * because if the state is > DS_ALIVE, we know it must
2177 		 * be.
2178 		 *
2179 		 * This assumes that all DF_REBID drivers can have
2180 		 * their probe routine called at any time and that
2181 		 * they are idempotent as well as completely benign in
2182 		 * normal operations.
2183 		 *
2184 		 * We also have to make sure that the detach
2185 		 * succeeded, otherwise we fail the operation (or
2186 		 * maybe it should just fail silently?  I'm torn).
2187 		 */
2188 		if (child->state > DS_ALIVE && best->driver != child->driver)
2189 			if ((result = device_detach(dev)) != 0)
2190 				return (result);
2191 
2192 		/* Set the winning driver, devclass, and flags. */
2193 		if (!child->devclass) {
2194 			result = device_set_devclass(child, best->driver->name);
2195 			if (result != 0)
2196 				return (result);
2197 		}
2198 		result = device_set_driver(child, best->driver);
2199 		if (result != 0)
2200 			return (result);
2201 		resource_int_value(best->driver->name, child->unit,
2202 		    "flags", &child->devflags);
2203 
2204 		if (pri < 0) {
2205 			/*
2206 			 * A bit bogus. Call the probe method again to make
2207 			 * sure that we have the right description.
2208 			 */
2209 			DEVICE_PROBE(child);
2210 #if 0
2211 			child->flags |= DF_REBID;
2212 #endif
2213 		} else
2214 			child->flags &= ~DF_REBID;
2215 		child->state = DS_ALIVE;
2216 
2217 		bus_data_generation_update();
2218 		return (0);
2219 	}
2220 
2221 	return (ENXIO);
2222 }
2223 
2224 /**
2225  * @brief Return the parent of a device
2226  */
2227 device_t
2228 device_get_parent(device_t dev)
2229 {
2230 	return (dev->parent);
2231 }
2232 
2233 /**
2234  * @brief Get a list of children of a device
2235  *
2236  * An array containing a list of all the children of the given device
2237  * is allocated and returned in @p *devlistp. The number of devices
2238  * in the array is returned in @p *devcountp. The caller should free
2239  * the array using @c free(p, M_TEMP).
2240  *
2241  * @param dev		the device to examine
2242  * @param devlistp	points at location for array pointer return
2243  *			value
2244  * @param devcountp	points at location for array size return value
2245  *
2246  * @retval 0		success
2247  * @retval ENOMEM	the array allocation failed
2248  */
2249 int
2250 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2251 {
2252 	int count;
2253 	device_t child;
2254 	device_t *list;
2255 
2256 	count = 0;
2257 	TAILQ_FOREACH(child, &dev->children, link) {
2258 		count++;
2259 	}
2260 	if (count == 0) {
2261 		*devlistp = NULL;
2262 		*devcountp = 0;
2263 		return (0);
2264 	}
2265 
2266 	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2267 	if (!list)
2268 		return (ENOMEM);
2269 
2270 	count = 0;
2271 	TAILQ_FOREACH(child, &dev->children, link) {
2272 		list[count] = child;
2273 		count++;
2274 	}
2275 
2276 	*devlistp = list;
2277 	*devcountp = count;
2278 
2279 	return (0);
2280 }
2281 
2282 /**
2283  * @brief Return the current driver for the device or @c NULL if there
2284  * is no driver currently attached
2285  */
2286 driver_t *
2287 device_get_driver(device_t dev)
2288 {
2289 	return (dev->driver);
2290 }
2291 
2292 /**
2293  * @brief Return the current devclass for the device or @c NULL if
2294  * there is none.
2295  */
2296 devclass_t
2297 device_get_devclass(device_t dev)
2298 {
2299 	return (dev->devclass);
2300 }
2301 
2302 /**
2303  * @brief Return the name of the device's devclass or @c NULL if there
2304  * is none.
2305  */
2306 const char *
2307 device_get_name(device_t dev)
2308 {
2309 	if (dev != NULL && dev->devclass)
2310 		return (devclass_get_name(dev->devclass));
2311 	return (NULL);
2312 }
2313 
2314 /**
2315  * @brief Return a string containing the device's devclass name
2316  * followed by an ascii representation of the device's unit number
2317  * (e.g. @c "foo2").
2318  */
2319 const char *
2320 device_get_nameunit(device_t dev)
2321 {
2322 	return (dev->nameunit);
2323 }
2324 
2325 /**
2326  * @brief Return the device's unit number.
2327  */
2328 int
2329 device_get_unit(device_t dev)
2330 {
2331 	return (dev->unit);
2332 }
2333 
2334 /**
2335  * @brief Return the device's description string
2336  */
2337 const char *
2338 device_get_desc(device_t dev)
2339 {
2340 	return (dev->desc);
2341 }
2342 
2343 /**
2344  * @brief Return the device's flags
2345  */
2346 uint32_t
2347 device_get_flags(device_t dev)
2348 {
2349 	return (dev->devflags);
2350 }
2351 
2352 struct sysctl_ctx_list *
2353 device_get_sysctl_ctx(device_t dev)
2354 {
2355 	return (&dev->sysctl_ctx);
2356 }
2357 
2358 struct sysctl_oid *
2359 device_get_sysctl_tree(device_t dev)
2360 {
2361 	return (dev->sysctl_tree);
2362 }
2363 
2364 /**
2365  * @brief Print the name of the device followed by a colon and a space
2366  *
2367  * @returns the number of characters printed
2368  */
2369 int
2370 device_print_prettyname(device_t dev)
2371 {
2372 	const char *name = device_get_name(dev);
2373 
2374 	if (name == NULL)
2375 		return (printf("unknown: "));
2376 	return (printf("%s%d: ", name, device_get_unit(dev)));
2377 }
2378 
2379 /**
2380  * @brief Print the name of the device followed by a colon, a space
2381  * and the result of calling vprintf() with the value of @p fmt and
2382  * the following arguments.
2383  *
2384  * @returns the number of characters printed
2385  */
2386 int
2387 device_printf(device_t dev, const char * fmt, ...)
2388 {
2389 	char buf[128];
2390 	struct sbuf sb;
2391 	const char *name;
2392 	va_list ap;
2393 	size_t retval;
2394 
2395 	retval = 0;
2396 
2397 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2398 	sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
2399 
2400 	name = device_get_name(dev);
2401 
2402 	if (name == NULL)
2403 		sbuf_cat(&sb, "unknown: ");
2404 	else
2405 		sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2406 
2407 	va_start(ap, fmt);
2408 	sbuf_vprintf(&sb, fmt, ap);
2409 	va_end(ap);
2410 
2411 	sbuf_finish(&sb);
2412 	sbuf_delete(&sb);
2413 
2414 	return (retval);
2415 }
2416 
2417 /**
2418  * @internal
2419  */
2420 static void
2421 device_set_desc_internal(device_t dev, const char* desc, int copy)
2422 {
2423 	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2424 		free(dev->desc, M_BUS);
2425 		dev->flags &= ~DF_DESCMALLOCED;
2426 		dev->desc = NULL;
2427 	}
2428 
2429 	if (copy && desc) {
2430 		dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2431 		if (dev->desc) {
2432 			strcpy(dev->desc, desc);
2433 			dev->flags |= DF_DESCMALLOCED;
2434 		}
2435 	} else {
2436 		/* Avoid a -Wcast-qual warning */
2437 		dev->desc = (char *)(uintptr_t) desc;
2438 	}
2439 
2440 	bus_data_generation_update();
2441 }
2442 
2443 /**
2444  * @brief Set the device's description
2445  *
2446  * The value of @c desc should be a string constant that will not
2447  * change (at least until the description is changed in a subsequent
2448  * call to device_set_desc() or device_set_desc_copy()).
2449  */
2450 void
2451 device_set_desc(device_t dev, const char* desc)
2452 {
2453 	device_set_desc_internal(dev, desc, FALSE);
2454 }
2455 
2456 /**
2457  * @brief Set the device's description
2458  *
2459  * The string pointed to by @c desc is copied. Use this function if
2460  * the device description is generated, (e.g. with sprintf()).
2461  */
2462 void
2463 device_set_desc_copy(device_t dev, const char* desc)
2464 {
2465 	device_set_desc_internal(dev, desc, TRUE);
2466 }
2467 
2468 /**
2469  * @brief Set the device's flags
2470  */
2471 void
2472 device_set_flags(device_t dev, uint32_t flags)
2473 {
2474 	dev->devflags = flags;
2475 }
2476 
2477 /**
2478  * @brief Return the device's softc field
2479  *
2480  * The softc is allocated and zeroed when a driver is attached, based
2481  * on the size field of the driver.
2482  */
2483 void *
2484 device_get_softc(device_t dev)
2485 {
2486 	return (dev->softc);
2487 }
2488 
2489 /**
2490  * @brief Set the device's softc field
2491  *
2492  * Most drivers do not need to use this since the softc is allocated
2493  * automatically when the driver is attached.
2494  */
2495 void
2496 device_set_softc(device_t dev, void *softc)
2497 {
2498 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2499 		free(dev->softc, M_BUS_SC);
2500 	dev->softc = softc;
2501 	if (dev->softc)
2502 		dev->flags |= DF_EXTERNALSOFTC;
2503 	else
2504 		dev->flags &= ~DF_EXTERNALSOFTC;
2505 }
2506 
2507 /**
2508  * @brief Free claimed softc
2509  *
2510  * Most drivers do not need to use this since the softc is freed
2511  * automatically when the driver is detached.
2512  */
2513 void
2514 device_free_softc(void *softc)
2515 {
2516 	free(softc, M_BUS_SC);
2517 }
2518 
2519 /**
2520  * @brief Claim softc
2521  *
2522  * This function can be used to let the driver free the automatically
2523  * allocated softc using "device_free_softc()". This function is
2524  * useful when the driver is refcounting the softc and the softc
2525  * cannot be freed when the "device_detach" method is called.
2526  */
2527 void
2528 device_claim_softc(device_t dev)
2529 {
2530 	if (dev->softc)
2531 		dev->flags |= DF_EXTERNALSOFTC;
2532 	else
2533 		dev->flags &= ~DF_EXTERNALSOFTC;
2534 }
2535 
2536 /**
2537  * @brief Get the device's ivars field
2538  *
2539  * The ivars field is used by the parent device to store per-device
2540  * state (e.g. the physical location of the device or a list of
2541  * resources).
2542  */
2543 void *
2544 device_get_ivars(device_t dev)
2545 {
2546 	KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2547 	return (dev->ivars);
2548 }
2549 
2550 /**
2551  * @brief Set the device's ivars field
2552  */
2553 void
2554 device_set_ivars(device_t dev, void * ivars)
2555 {
2556 	KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2557 	dev->ivars = ivars;
2558 }
2559 
2560 /**
2561  * @brief Return the device's state
2562  */
2563 device_state_t
2564 device_get_state(device_t dev)
2565 {
2566 	return (dev->state);
2567 }
2568 
2569 /**
2570  * @brief Set the DF_ENABLED flag for the device
2571  */
2572 void
2573 device_enable(device_t dev)
2574 {
2575 	dev->flags |= DF_ENABLED;
2576 }
2577 
2578 /**
2579  * @brief Clear the DF_ENABLED flag for the device
2580  */
2581 void
2582 device_disable(device_t dev)
2583 {
2584 	dev->flags &= ~DF_ENABLED;
2585 }
2586 
2587 /**
2588  * @brief Increment the busy counter for the device
2589  */
2590 void
2591 device_busy(device_t dev)
2592 {
2593 	if (dev->state < DS_ATTACHING)
2594 		panic("device_busy: called for unattached device");
2595 	if (dev->busy == 0 && dev->parent)
2596 		device_busy(dev->parent);
2597 	dev->busy++;
2598 	if (dev->state == DS_ATTACHED)
2599 		dev->state = DS_BUSY;
2600 }
2601 
2602 /**
2603  * @brief Decrement the busy counter for the device
2604  */
2605 void
2606 device_unbusy(device_t dev)
2607 {
2608 	if (dev->busy != 0 && dev->state != DS_BUSY &&
2609 	    dev->state != DS_ATTACHING)
2610 		panic("device_unbusy: called for non-busy device %s",
2611 		    device_get_nameunit(dev));
2612 	dev->busy--;
2613 	if (dev->busy == 0) {
2614 		if (dev->parent)
2615 			device_unbusy(dev->parent);
2616 		if (dev->state == DS_BUSY)
2617 			dev->state = DS_ATTACHED;
2618 	}
2619 }
2620 
2621 /**
2622  * @brief Set the DF_QUIET flag for the device
2623  */
2624 void
2625 device_quiet(device_t dev)
2626 {
2627 	dev->flags |= DF_QUIET;
2628 }
2629 
2630 /**
2631  * @brief Set the DF_QUIET_CHILDREN flag for the device
2632  */
2633 void
2634 device_quiet_children(device_t dev)
2635 {
2636 	dev->flags |= DF_QUIET_CHILDREN;
2637 }
2638 
2639 /**
2640  * @brief Clear the DF_QUIET flag for the device
2641  */
2642 void
2643 device_verbose(device_t dev)
2644 {
2645 	dev->flags &= ~DF_QUIET;
2646 }
2647 
2648 /**
2649  * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2650  */
2651 int
2652 device_has_quiet_children(device_t dev)
2653 {
2654 	return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2655 }
2656 
2657 /**
2658  * @brief Return non-zero if the DF_QUIET flag is set on the device
2659  */
2660 int
2661 device_is_quiet(device_t dev)
2662 {
2663 	return ((dev->flags & DF_QUIET) != 0);
2664 }
2665 
2666 /**
2667  * @brief Return non-zero if the DF_ENABLED flag is set on the device
2668  */
2669 int
2670 device_is_enabled(device_t dev)
2671 {
2672 	return ((dev->flags & DF_ENABLED) != 0);
2673 }
2674 
2675 /**
2676  * @brief Return non-zero if the device was successfully probed
2677  */
2678 int
2679 device_is_alive(device_t dev)
2680 {
2681 	return (dev->state >= DS_ALIVE);
2682 }
2683 
2684 /**
2685  * @brief Return non-zero if the device currently has a driver
2686  * attached to it
2687  */
2688 int
2689 device_is_attached(device_t dev)
2690 {
2691 	return (dev->state >= DS_ATTACHED);
2692 }
2693 
2694 /**
2695  * @brief Return non-zero if the device is currently suspended.
2696  */
2697 int
2698 device_is_suspended(device_t dev)
2699 {
2700 	return ((dev->flags & DF_SUSPENDED) != 0);
2701 }
2702 
2703 /**
2704  * @brief Set the devclass of a device
2705  * @see devclass_add_device().
2706  */
2707 int
2708 device_set_devclass(device_t dev, const char *classname)
2709 {
2710 	devclass_t dc;
2711 	int error;
2712 
2713 	if (!classname) {
2714 		if (dev->devclass)
2715 			devclass_delete_device(dev->devclass, dev);
2716 		return (0);
2717 	}
2718 
2719 	if (dev->devclass) {
2720 		printf("device_set_devclass: device class already set\n");
2721 		return (EINVAL);
2722 	}
2723 
2724 	dc = devclass_find_internal(classname, NULL, TRUE);
2725 	if (!dc)
2726 		return (ENOMEM);
2727 
2728 	error = devclass_add_device(dc, dev);
2729 
2730 	bus_data_generation_update();
2731 	return (error);
2732 }
2733 
2734 /**
2735  * @brief Set the devclass of a device and mark the devclass fixed.
2736  * @see device_set_devclass()
2737  */
2738 int
2739 device_set_devclass_fixed(device_t dev, const char *classname)
2740 {
2741 	int error;
2742 
2743 	if (classname == NULL)
2744 		return (EINVAL);
2745 
2746 	error = device_set_devclass(dev, classname);
2747 	if (error)
2748 		return (error);
2749 	dev->flags |= DF_FIXEDCLASS;
2750 	return (0);
2751 }
2752 
2753 /**
2754  * @brief Query the device to determine if it's of a fixed devclass
2755  * @see device_set_devclass_fixed()
2756  */
2757 bool
2758 device_is_devclass_fixed(device_t dev)
2759 {
2760 	return ((dev->flags & DF_FIXEDCLASS) != 0);
2761 }
2762 
2763 /**
2764  * @brief Set the driver of a device
2765  *
2766  * @retval 0		success
2767  * @retval EBUSY	the device already has a driver attached
2768  * @retval ENOMEM	a memory allocation failure occurred
2769  */
2770 int
2771 device_set_driver(device_t dev, driver_t *driver)
2772 {
2773 	int domain;
2774 	struct domainset *policy;
2775 
2776 	if (dev->state >= DS_ATTACHED)
2777 		return (EBUSY);
2778 
2779 	if (dev->driver == driver)
2780 		return (0);
2781 
2782 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2783 		free(dev->softc, M_BUS_SC);
2784 		dev->softc = NULL;
2785 	}
2786 	device_set_desc(dev, NULL);
2787 	kobj_delete((kobj_t) dev, NULL);
2788 	dev->driver = driver;
2789 	if (driver) {
2790 		kobj_init((kobj_t) dev, (kobj_class_t) driver);
2791 		if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2792 			if (bus_get_domain(dev, &domain) == 0)
2793 				policy = DOMAINSET_PREF(domain);
2794 			else
2795 				policy = DOMAINSET_RR();
2796 			dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2797 			    policy, M_NOWAIT | M_ZERO);
2798 			if (!dev->softc) {
2799 				kobj_delete((kobj_t) dev, NULL);
2800 				kobj_init((kobj_t) dev, &null_class);
2801 				dev->driver = NULL;
2802 				return (ENOMEM);
2803 			}
2804 		}
2805 	} else {
2806 		kobj_init((kobj_t) dev, &null_class);
2807 	}
2808 
2809 	bus_data_generation_update();
2810 	return (0);
2811 }
2812 
2813 /**
2814  * @brief Probe a device, and return this status.
2815  *
2816  * This function is the core of the device autoconfiguration
2817  * system. Its purpose is to select a suitable driver for a device and
2818  * then call that driver to initialise the hardware appropriately. The
2819  * driver is selected by calling the DEVICE_PROBE() method of a set of
2820  * candidate drivers and then choosing the driver which returned the
2821  * best value. This driver is then attached to the device using
2822  * device_attach().
2823  *
2824  * The set of suitable drivers is taken from the list of drivers in
2825  * the parent device's devclass. If the device was originally created
2826  * with a specific class name (see device_add_child()), only drivers
2827  * with that name are probed, otherwise all drivers in the devclass
2828  * are probed. If no drivers return successful probe values in the
2829  * parent devclass, the search continues in the parent of that
2830  * devclass (see devclass_get_parent()) if any.
2831  *
2832  * @param dev		the device to initialise
2833  *
2834  * @retval 0		success
2835  * @retval ENXIO	no driver was found
2836  * @retval ENOMEM	memory allocation failure
2837  * @retval non-zero	some other unix error code
2838  * @retval -1		Device already attached
2839  */
2840 int
2841 device_probe(device_t dev)
2842 {
2843 	int error;
2844 
2845 	GIANT_REQUIRED;
2846 
2847 	if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2848 		return (-1);
2849 
2850 	if (!(dev->flags & DF_ENABLED)) {
2851 		if (bootverbose && device_get_name(dev) != NULL) {
2852 			device_print_prettyname(dev);
2853 			printf("not probed (disabled)\n");
2854 		}
2855 		return (-1);
2856 	}
2857 	if ((error = device_probe_child(dev->parent, dev)) != 0) {
2858 		if (bus_current_pass == BUS_PASS_DEFAULT &&
2859 		    !(dev->flags & DF_DONENOMATCH)) {
2860 			BUS_PROBE_NOMATCH(dev->parent, dev);
2861 			devnomatch(dev);
2862 			dev->flags |= DF_DONENOMATCH;
2863 		}
2864 		return (error);
2865 	}
2866 	return (0);
2867 }
2868 
2869 /**
2870  * @brief Probe a device and attach a driver if possible
2871  *
2872  * calls device_probe() and attaches if that was successful.
2873  */
2874 int
2875 device_probe_and_attach(device_t dev)
2876 {
2877 	int error;
2878 
2879 	GIANT_REQUIRED;
2880 
2881 	error = device_probe(dev);
2882 	if (error == -1)
2883 		return (0);
2884 	else if (error != 0)
2885 		return (error);
2886 
2887 	CURVNET_SET_QUIET(vnet0);
2888 	error = device_attach(dev);
2889 	CURVNET_RESTORE();
2890 	return error;
2891 }
2892 
2893 /**
2894  * @brief Attach a device driver to a device
2895  *
2896  * This function is a wrapper around the DEVICE_ATTACH() driver
2897  * method. In addition to calling DEVICE_ATTACH(), it initialises the
2898  * device's sysctl tree, optionally prints a description of the device
2899  * and queues a notification event for user-based device management
2900  * services.
2901  *
2902  * Normally this function is only called internally from
2903  * device_probe_and_attach().
2904  *
2905  * @param dev		the device to initialise
2906  *
2907  * @retval 0		success
2908  * @retval ENXIO	no driver was found
2909  * @retval ENOMEM	memory allocation failure
2910  * @retval non-zero	some other unix error code
2911  */
2912 int
2913 device_attach(device_t dev)
2914 {
2915 	uint64_t attachtime;
2916 	uint16_t attachentropy;
2917 	int error;
2918 
2919 	if (resource_disabled(dev->driver->name, dev->unit)) {
2920 		device_disable(dev);
2921 		if (bootverbose)
2922 			 device_printf(dev, "disabled via hints entry\n");
2923 		return (ENXIO);
2924 	}
2925 
2926 	device_sysctl_init(dev);
2927 	if (!device_is_quiet(dev))
2928 		device_print_child(dev->parent, dev);
2929 	attachtime = get_cyclecount();
2930 	dev->state = DS_ATTACHING;
2931 	if ((error = DEVICE_ATTACH(dev)) != 0) {
2932 		printf("device_attach: %s%d attach returned %d\n",
2933 		    dev->driver->name, dev->unit, error);
2934 		if (!(dev->flags & DF_FIXEDCLASS))
2935 			devclass_delete_device(dev->devclass, dev);
2936 		(void)device_set_driver(dev, NULL);
2937 		device_sysctl_fini(dev);
2938 		KASSERT(dev->busy == 0, ("attach failed but busy"));
2939 		dev->state = DS_NOTPRESENT;
2940 		return (error);
2941 	}
2942 	dev->flags |= DF_ATTACHED_ONCE;
2943 	/* We only need the low bits of this time, but ranges from tens to thousands
2944 	 * have been seen, so keep 2 bytes' worth.
2945 	 */
2946 	attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2947 	random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2948 	device_sysctl_update(dev);
2949 	if (dev->busy)
2950 		dev->state = DS_BUSY;
2951 	else
2952 		dev->state = DS_ATTACHED;
2953 	dev->flags &= ~DF_DONENOMATCH;
2954 	EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2955 	devadded(dev);
2956 	return (0);
2957 }
2958 
2959 /**
2960  * @brief Detach a driver from a device
2961  *
2962  * This function is a wrapper around the DEVICE_DETACH() driver
2963  * method. If the call to DEVICE_DETACH() succeeds, it calls
2964  * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2965  * notification event for user-based device management services and
2966  * cleans up the device's sysctl tree.
2967  *
2968  * @param dev		the device to un-initialise
2969  *
2970  * @retval 0		success
2971  * @retval ENXIO	no driver was found
2972  * @retval ENOMEM	memory allocation failure
2973  * @retval non-zero	some other unix error code
2974  */
2975 int
2976 device_detach(device_t dev)
2977 {
2978 	int error;
2979 
2980 	GIANT_REQUIRED;
2981 
2982 	PDEBUG(("%s", DEVICENAME(dev)));
2983 	if (dev->state == DS_BUSY)
2984 		return (EBUSY);
2985 	if (dev->state == DS_ATTACHING) {
2986 		device_printf(dev, "device in attaching state! Deferring detach.\n");
2987 		return (EBUSY);
2988 	}
2989 	if (dev->state != DS_ATTACHED)
2990 		return (0);
2991 
2992 	EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2993 	if ((error = DEVICE_DETACH(dev)) != 0) {
2994 		EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2995 		    EVHDEV_DETACH_FAILED);
2996 		return (error);
2997 	} else {
2998 		EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2999 		    EVHDEV_DETACH_COMPLETE);
3000 	}
3001 	devremoved(dev);
3002 	if (!device_is_quiet(dev))
3003 		device_printf(dev, "detached\n");
3004 	if (dev->parent)
3005 		BUS_CHILD_DETACHED(dev->parent, dev);
3006 
3007 	if (!(dev->flags & DF_FIXEDCLASS))
3008 		devclass_delete_device(dev->devclass, dev);
3009 
3010 	device_verbose(dev);
3011 	dev->state = DS_NOTPRESENT;
3012 	(void)device_set_driver(dev, NULL);
3013 	device_sysctl_fini(dev);
3014 
3015 	return (0);
3016 }
3017 
3018 /**
3019  * @brief Tells a driver to quiesce itself.
3020  *
3021  * This function is a wrapper around the DEVICE_QUIESCE() driver
3022  * method. If the call to DEVICE_QUIESCE() succeeds.
3023  *
3024  * @param dev		the device to quiesce
3025  *
3026  * @retval 0		success
3027  * @retval ENXIO	no driver was found
3028  * @retval ENOMEM	memory allocation failure
3029  * @retval non-zero	some other unix error code
3030  */
3031 int
3032 device_quiesce(device_t dev)
3033 {
3034 	PDEBUG(("%s", DEVICENAME(dev)));
3035 	if (dev->state == DS_BUSY)
3036 		return (EBUSY);
3037 	if (dev->state != DS_ATTACHED)
3038 		return (0);
3039 
3040 	return (DEVICE_QUIESCE(dev));
3041 }
3042 
3043 /**
3044  * @brief Notify a device of system shutdown
3045  *
3046  * This function calls the DEVICE_SHUTDOWN() driver method if the
3047  * device currently has an attached driver.
3048  *
3049  * @returns the value returned by DEVICE_SHUTDOWN()
3050  */
3051 int
3052 device_shutdown(device_t dev)
3053 {
3054 	if (dev->state < DS_ATTACHED)
3055 		return (0);
3056 	return (DEVICE_SHUTDOWN(dev));
3057 }
3058 
3059 /**
3060  * @brief Set the unit number of a device
3061  *
3062  * This function can be used to override the unit number used for a
3063  * device (e.g. to wire a device to a pre-configured unit number).
3064  */
3065 int
3066 device_set_unit(device_t dev, int unit)
3067 {
3068 	devclass_t dc;
3069 	int err;
3070 
3071 	dc = device_get_devclass(dev);
3072 	if (unit < dc->maxunit && dc->devices[unit])
3073 		return (EBUSY);
3074 	err = devclass_delete_device(dc, dev);
3075 	if (err)
3076 		return (err);
3077 	dev->unit = unit;
3078 	err = devclass_add_device(dc, dev);
3079 	if (err)
3080 		return (err);
3081 
3082 	bus_data_generation_update();
3083 	return (0);
3084 }
3085 
3086 /*======================================*/
3087 /*
3088  * Some useful method implementations to make life easier for bus drivers.
3089  */
3090 
3091 void
3092 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
3093 {
3094 	bzero(args, sz);
3095 	args->size = sz;
3096 	args->memattr = VM_MEMATTR_UNCACHEABLE;
3097 }
3098 
3099 /**
3100  * @brief Initialise a resource list.
3101  *
3102  * @param rl		the resource list to initialise
3103  */
3104 void
3105 resource_list_init(struct resource_list *rl)
3106 {
3107 	STAILQ_INIT(rl);
3108 }
3109 
3110 /**
3111  * @brief Reclaim memory used by a resource list.
3112  *
3113  * This function frees the memory for all resource entries on the list
3114  * (if any).
3115  *
3116  * @param rl		the resource list to free
3117  */
3118 void
3119 resource_list_free(struct resource_list *rl)
3120 {
3121 	struct resource_list_entry *rle;
3122 
3123 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3124 		if (rle->res)
3125 			panic("resource_list_free: resource entry is busy");
3126 		STAILQ_REMOVE_HEAD(rl, link);
3127 		free(rle, M_BUS);
3128 	}
3129 }
3130 
3131 /**
3132  * @brief Add a resource entry.
3133  *
3134  * This function adds a resource entry using the given @p type, @p
3135  * start, @p end and @p count values. A rid value is chosen by
3136  * searching sequentially for the first unused rid starting at zero.
3137  *
3138  * @param rl		the resource list to edit
3139  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3140  * @param start		the start address of the resource
3141  * @param end		the end address of the resource
3142  * @param count		XXX end-start+1
3143  */
3144 int
3145 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3146     rman_res_t end, rman_res_t count)
3147 {
3148 	int rid;
3149 
3150 	rid = 0;
3151 	while (resource_list_find(rl, type, rid) != NULL)
3152 		rid++;
3153 	resource_list_add(rl, type, rid, start, end, count);
3154 	return (rid);
3155 }
3156 
3157 /**
3158  * @brief Add or modify a resource entry.
3159  *
3160  * If an existing entry exists with the same type and rid, it will be
3161  * modified using the given values of @p start, @p end and @p
3162  * count. If no entry exists, a new one will be created using the
3163  * given values.  The resource list entry that matches is then returned.
3164  *
3165  * @param rl		the resource list to edit
3166  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3167  * @param rid		the resource identifier
3168  * @param start		the start address of the resource
3169  * @param end		the end address of the resource
3170  * @param count		XXX end-start+1
3171  */
3172 struct resource_list_entry *
3173 resource_list_add(struct resource_list *rl, int type, int rid,
3174     rman_res_t start, rman_res_t end, rman_res_t count)
3175 {
3176 	struct resource_list_entry *rle;
3177 
3178 	rle = resource_list_find(rl, type, rid);
3179 	if (!rle) {
3180 		rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3181 		    M_NOWAIT);
3182 		if (!rle)
3183 			panic("resource_list_add: can't record entry");
3184 		STAILQ_INSERT_TAIL(rl, rle, link);
3185 		rle->type = type;
3186 		rle->rid = rid;
3187 		rle->res = NULL;
3188 		rle->flags = 0;
3189 	}
3190 
3191 	if (rle->res)
3192 		panic("resource_list_add: resource entry is busy");
3193 
3194 	rle->start = start;
3195 	rle->end = end;
3196 	rle->count = count;
3197 	return (rle);
3198 }
3199 
3200 /**
3201  * @brief Determine if a resource entry is busy.
3202  *
3203  * Returns true if a resource entry is busy meaning that it has an
3204  * associated resource that is not an unallocated "reserved" resource.
3205  *
3206  * @param rl		the resource list to search
3207  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3208  * @param rid		the resource identifier
3209  *
3210  * @returns Non-zero if the entry is busy, zero otherwise.
3211  */
3212 int
3213 resource_list_busy(struct resource_list *rl, int type, int rid)
3214 {
3215 	struct resource_list_entry *rle;
3216 
3217 	rle = resource_list_find(rl, type, rid);
3218 	if (rle == NULL || rle->res == NULL)
3219 		return (0);
3220 	if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3221 		KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3222 		    ("reserved resource is active"));
3223 		return (0);
3224 	}
3225 	return (1);
3226 }
3227 
3228 /**
3229  * @brief Determine if a resource entry is reserved.
3230  *
3231  * Returns true if a resource entry is reserved meaning that it has an
3232  * associated "reserved" resource.  The resource can either be
3233  * allocated or unallocated.
3234  *
3235  * @param rl		the resource list to search
3236  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3237  * @param rid		the resource identifier
3238  *
3239  * @returns Non-zero if the entry is reserved, zero otherwise.
3240  */
3241 int
3242 resource_list_reserved(struct resource_list *rl, int type, int rid)
3243 {
3244 	struct resource_list_entry *rle;
3245 
3246 	rle = resource_list_find(rl, type, rid);
3247 	if (rle != NULL && rle->flags & RLE_RESERVED)
3248 		return (1);
3249 	return (0);
3250 }
3251 
3252 /**
3253  * @brief Find a resource entry by type and rid.
3254  *
3255  * @param rl		the resource list to search
3256  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3257  * @param rid		the resource identifier
3258  *
3259  * @returns the resource entry pointer or NULL if there is no such
3260  * entry.
3261  */
3262 struct resource_list_entry *
3263 resource_list_find(struct resource_list *rl, int type, int rid)
3264 {
3265 	struct resource_list_entry *rle;
3266 
3267 	STAILQ_FOREACH(rle, rl, link) {
3268 		if (rle->type == type && rle->rid == rid)
3269 			return (rle);
3270 	}
3271 	return (NULL);
3272 }
3273 
3274 /**
3275  * @brief Delete a resource entry.
3276  *
3277  * @param rl		the resource list to edit
3278  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3279  * @param rid		the resource identifier
3280  */
3281 void
3282 resource_list_delete(struct resource_list *rl, int type, int rid)
3283 {
3284 	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3285 
3286 	if (rle) {
3287 		if (rle->res != NULL)
3288 			panic("resource_list_delete: resource has not been released");
3289 		STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3290 		free(rle, M_BUS);
3291 	}
3292 }
3293 
3294 /**
3295  * @brief Allocate a reserved resource
3296  *
3297  * This can be used by buses to force the allocation of resources
3298  * that are always active in the system even if they are not allocated
3299  * by a driver (e.g. PCI BARs).  This function is usually called when
3300  * adding a new child to the bus.  The resource is allocated from the
3301  * parent bus when it is reserved.  The resource list entry is marked
3302  * with RLE_RESERVED to note that it is a reserved resource.
3303  *
3304  * Subsequent attempts to allocate the resource with
3305  * resource_list_alloc() will succeed the first time and will set
3306  * RLE_ALLOCATED to note that it has been allocated.  When a reserved
3307  * resource that has been allocated is released with
3308  * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3309  * the actual resource remains allocated.  The resource can be released to
3310  * the parent bus by calling resource_list_unreserve().
3311  *
3312  * @param rl		the resource list to allocate from
3313  * @param bus		the parent device of @p child
3314  * @param child		the device for which the resource is being reserved
3315  * @param type		the type of resource to allocate
3316  * @param rid		a pointer to the resource identifier
3317  * @param start		hint at the start of the resource range - pass
3318  *			@c 0 for any start address
3319  * @param end		hint at the end of the resource range - pass
3320  *			@c ~0 for any end address
3321  * @param count		hint at the size of range required - pass @c 1
3322  *			for any size
3323  * @param flags		any extra flags to control the resource
3324  *			allocation - see @c RF_XXX flags in
3325  *			<sys/rman.h> for details
3326  *
3327  * @returns		the resource which was allocated or @c NULL if no
3328  *			resource could be allocated
3329  */
3330 struct resource *
3331 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3332     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3333 {
3334 	struct resource_list_entry *rle = NULL;
3335 	int passthrough = (device_get_parent(child) != bus);
3336 	struct resource *r;
3337 
3338 	if (passthrough)
3339 		panic(
3340     "resource_list_reserve() should only be called for direct children");
3341 	if (flags & RF_ACTIVE)
3342 		panic(
3343     "resource_list_reserve() should only reserve inactive resources");
3344 
3345 	r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3346 	    flags);
3347 	if (r != NULL) {
3348 		rle = resource_list_find(rl, type, *rid);
3349 		rle->flags |= RLE_RESERVED;
3350 	}
3351 	return (r);
3352 }
3353 
3354 /**
3355  * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3356  *
3357  * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3358  * and passing the allocation up to the parent of @p bus. This assumes
3359  * that the first entry of @c device_get_ivars(child) is a struct
3360  * resource_list. This also handles 'passthrough' allocations where a
3361  * child is a remote descendant of bus by passing the allocation up to
3362  * the parent of bus.
3363  *
3364  * Typically, a bus driver would store a list of child resources
3365  * somewhere in the child device's ivars (see device_get_ivars()) and
3366  * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3367  * then call resource_list_alloc() to perform the allocation.
3368  *
3369  * @param rl		the resource list to allocate from
3370  * @param bus		the parent device of @p child
3371  * @param child		the device which is requesting an allocation
3372  * @param type		the type of resource to allocate
3373  * @param rid		a pointer to the resource identifier
3374  * @param start		hint at the start of the resource range - pass
3375  *			@c 0 for any start address
3376  * @param end		hint at the end of the resource range - pass
3377  *			@c ~0 for any end address
3378  * @param count		hint at the size of range required - pass @c 1
3379  *			for any size
3380  * @param flags		any extra flags to control the resource
3381  *			allocation - see @c RF_XXX flags in
3382  *			<sys/rman.h> for details
3383  *
3384  * @returns		the resource which was allocated or @c NULL if no
3385  *			resource could be allocated
3386  */
3387 struct resource *
3388 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3389     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3390 {
3391 	struct resource_list_entry *rle = NULL;
3392 	int passthrough = (device_get_parent(child) != bus);
3393 	int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3394 
3395 	if (passthrough) {
3396 		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3397 		    type, rid, start, end, count, flags));
3398 	}
3399 
3400 	rle = resource_list_find(rl, type, *rid);
3401 
3402 	if (!rle)
3403 		return (NULL);		/* no resource of that type/rid */
3404 
3405 	if (rle->res) {
3406 		if (rle->flags & RLE_RESERVED) {
3407 			if (rle->flags & RLE_ALLOCATED)
3408 				return (NULL);
3409 			if ((flags & RF_ACTIVE) &&
3410 			    bus_activate_resource(child, type, *rid,
3411 			    rle->res) != 0)
3412 				return (NULL);
3413 			rle->flags |= RLE_ALLOCATED;
3414 			return (rle->res);
3415 		}
3416 		device_printf(bus,
3417 		    "resource entry %#x type %d for child %s is busy\n", *rid,
3418 		    type, device_get_nameunit(child));
3419 		return (NULL);
3420 	}
3421 
3422 	if (isdefault) {
3423 		start = rle->start;
3424 		count = ulmax(count, rle->count);
3425 		end = ulmax(rle->end, start + count - 1);
3426 	}
3427 
3428 	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3429 	    type, rid, start, end, count, flags);
3430 
3431 	/*
3432 	 * Record the new range.
3433 	 */
3434 	if (rle->res) {
3435 		rle->start = rman_get_start(rle->res);
3436 		rle->end = rman_get_end(rle->res);
3437 		rle->count = count;
3438 	}
3439 
3440 	return (rle->res);
3441 }
3442 
3443 /**
3444  * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3445  *
3446  * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3447  * used with resource_list_alloc().
3448  *
3449  * @param rl		the resource list which was allocated from
3450  * @param bus		the parent device of @p child
3451  * @param child		the device which is requesting a release
3452  * @param type		the type of resource to release
3453  * @param rid		the resource identifier
3454  * @param res		the resource to release
3455  *
3456  * @retval 0		success
3457  * @retval non-zero	a standard unix error code indicating what
3458  *			error condition prevented the operation
3459  */
3460 int
3461 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3462     int type, int rid, struct resource *res)
3463 {
3464 	struct resource_list_entry *rle = NULL;
3465 	int passthrough = (device_get_parent(child) != bus);
3466 	int error;
3467 
3468 	if (passthrough) {
3469 		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3470 		    type, rid, res));
3471 	}
3472 
3473 	rle = resource_list_find(rl, type, rid);
3474 
3475 	if (!rle)
3476 		panic("resource_list_release: can't find resource");
3477 	if (!rle->res)
3478 		panic("resource_list_release: resource entry is not busy");
3479 	if (rle->flags & RLE_RESERVED) {
3480 		if (rle->flags & RLE_ALLOCATED) {
3481 			if (rman_get_flags(res) & RF_ACTIVE) {
3482 				error = bus_deactivate_resource(child, type,
3483 				    rid, res);
3484 				if (error)
3485 					return (error);
3486 			}
3487 			rle->flags &= ~RLE_ALLOCATED;
3488 			return (0);
3489 		}
3490 		return (EINVAL);
3491 	}
3492 
3493 	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3494 	    type, rid, res);
3495 	if (error)
3496 		return (error);
3497 
3498 	rle->res = NULL;
3499 	return (0);
3500 }
3501 
3502 /**
3503  * @brief Release all active resources of a given type
3504  *
3505  * Release all active resources of a specified type.  This is intended
3506  * to be used to cleanup resources leaked by a driver after detach or
3507  * a failed attach.
3508  *
3509  * @param rl		the resource list which was allocated from
3510  * @param bus		the parent device of @p child
3511  * @param child		the device whose active resources are being released
3512  * @param type		the type of resources to release
3513  *
3514  * @retval 0		success
3515  * @retval EBUSY	at least one resource was active
3516  */
3517 int
3518 resource_list_release_active(struct resource_list *rl, device_t bus,
3519     device_t child, int type)
3520 {
3521 	struct resource_list_entry *rle;
3522 	int error, retval;
3523 
3524 	retval = 0;
3525 	STAILQ_FOREACH(rle, rl, link) {
3526 		if (rle->type != type)
3527 			continue;
3528 		if (rle->res == NULL)
3529 			continue;
3530 		if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3531 		    RLE_RESERVED)
3532 			continue;
3533 		retval = EBUSY;
3534 		error = resource_list_release(rl, bus, child, type,
3535 		    rman_get_rid(rle->res), rle->res);
3536 		if (error != 0)
3537 			device_printf(bus,
3538 			    "Failed to release active resource: %d\n", error);
3539 	}
3540 	return (retval);
3541 }
3542 
3543 /**
3544  * @brief Fully release a reserved resource
3545  *
3546  * Fully releases a resource reserved via resource_list_reserve().
3547  *
3548  * @param rl		the resource list which was allocated from
3549  * @param bus		the parent device of @p child
3550  * @param child		the device whose reserved resource is being released
3551  * @param type		the type of resource to release
3552  * @param rid		the resource identifier
3553  * @param res		the resource to release
3554  *
3555  * @retval 0		success
3556  * @retval non-zero	a standard unix error code indicating what
3557  *			error condition prevented the operation
3558  */
3559 int
3560 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3561     int type, int rid)
3562 {
3563 	struct resource_list_entry *rle = NULL;
3564 	int passthrough = (device_get_parent(child) != bus);
3565 
3566 	if (passthrough)
3567 		panic(
3568     "resource_list_unreserve() should only be called for direct children");
3569 
3570 	rle = resource_list_find(rl, type, rid);
3571 
3572 	if (!rle)
3573 		panic("resource_list_unreserve: can't find resource");
3574 	if (!(rle->flags & RLE_RESERVED))
3575 		return (EINVAL);
3576 	if (rle->flags & RLE_ALLOCATED)
3577 		return (EBUSY);
3578 	rle->flags &= ~RLE_RESERVED;
3579 	return (resource_list_release(rl, bus, child, type, rid, rle->res));
3580 }
3581 
3582 /**
3583  * @brief Print a description of resources in a resource list
3584  *
3585  * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3586  * The name is printed if at least one resource of the given type is available.
3587  * The format is used to print resource start and end.
3588  *
3589  * @param rl		the resource list to print
3590  * @param name		the name of @p type, e.g. @c "memory"
3591  * @param type		type type of resource entry to print
3592  * @param format	printf(9) format string to print resource
3593  *			start and end values
3594  *
3595  * @returns		the number of characters printed
3596  */
3597 int
3598 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3599     const char *format)
3600 {
3601 	struct resource_list_entry *rle;
3602 	int printed, retval;
3603 
3604 	printed = 0;
3605 	retval = 0;
3606 	/* Yes, this is kinda cheating */
3607 	STAILQ_FOREACH(rle, rl, link) {
3608 		if (rle->type == type) {
3609 			if (printed == 0)
3610 				retval += printf(" %s ", name);
3611 			else
3612 				retval += printf(",");
3613 			printed++;
3614 			retval += printf(format, rle->start);
3615 			if (rle->count > 1) {
3616 				retval += printf("-");
3617 				retval += printf(format, rle->start +
3618 						 rle->count - 1);
3619 			}
3620 		}
3621 	}
3622 	return (retval);
3623 }
3624 
3625 /**
3626  * @brief Releases all the resources in a list.
3627  *
3628  * @param rl		The resource list to purge.
3629  *
3630  * @returns		nothing
3631  */
3632 void
3633 resource_list_purge(struct resource_list *rl)
3634 {
3635 	struct resource_list_entry *rle;
3636 
3637 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3638 		if (rle->res)
3639 			bus_release_resource(rman_get_device(rle->res),
3640 			    rle->type, rle->rid, rle->res);
3641 		STAILQ_REMOVE_HEAD(rl, link);
3642 		free(rle, M_BUS);
3643 	}
3644 }
3645 
3646 device_t
3647 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3648 {
3649 	return (device_add_child_ordered(dev, order, name, unit));
3650 }
3651 
3652 /**
3653  * @brief Helper function for implementing DEVICE_PROBE()
3654  *
3655  * This function can be used to help implement the DEVICE_PROBE() for
3656  * a bus (i.e. a device which has other devices attached to it). It
3657  * calls the DEVICE_IDENTIFY() method of each driver in the device's
3658  * devclass.
3659  */
3660 int
3661 bus_generic_probe(device_t dev)
3662 {
3663 	devclass_t dc = dev->devclass;
3664 	driverlink_t dl;
3665 
3666 	TAILQ_FOREACH(dl, &dc->drivers, link) {
3667 		/*
3668 		 * If this driver's pass is too high, then ignore it.
3669 		 * For most drivers in the default pass, this will
3670 		 * never be true.  For early-pass drivers they will
3671 		 * only call the identify routines of eligible drivers
3672 		 * when this routine is called.  Drivers for later
3673 		 * passes should have their identify routines called
3674 		 * on early-pass buses during BUS_NEW_PASS().
3675 		 */
3676 		if (dl->pass > bus_current_pass)
3677 			continue;
3678 		DEVICE_IDENTIFY(dl->driver, dev);
3679 	}
3680 
3681 	return (0);
3682 }
3683 
3684 /**
3685  * @brief Helper function for implementing DEVICE_ATTACH()
3686  *
3687  * This function can be used to help implement the DEVICE_ATTACH() for
3688  * a bus. It calls device_probe_and_attach() for each of the device's
3689  * children.
3690  */
3691 int
3692 bus_generic_attach(device_t dev)
3693 {
3694 	device_t child;
3695 
3696 	TAILQ_FOREACH(child, &dev->children, link) {
3697 		device_probe_and_attach(child);
3698 	}
3699 
3700 	return (0);
3701 }
3702 
3703 /**
3704  * @brief Helper function for delaying attaching children
3705  *
3706  * Many buses can't run transactions on the bus which children need to probe and
3707  * attach until after interrupts and/or timers are running.  This function
3708  * delays their attach until interrupts and timers are enabled.
3709  */
3710 int
3711 bus_delayed_attach_children(device_t dev)
3712 {
3713 	/* Probe and attach the bus children when interrupts are available */
3714 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3715 
3716 	return (0);
3717 }
3718 
3719 /**
3720  * @brief Helper function for implementing DEVICE_DETACH()
3721  *
3722  * This function can be used to help implement the DEVICE_DETACH() for
3723  * a bus. It calls device_detach() for each of the device's
3724  * children.
3725  */
3726 int
3727 bus_generic_detach(device_t dev)
3728 {
3729 	device_t child;
3730 	int error;
3731 
3732 	if (dev->state != DS_ATTACHED)
3733 		return (EBUSY);
3734 
3735 	/*
3736 	 * Detach children in the reverse order.
3737 	 * See bus_generic_suspend for details.
3738 	 */
3739 	TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3740 		if ((error = device_detach(child)) != 0)
3741 			return (error);
3742 	}
3743 
3744 	return (0);
3745 }
3746 
3747 /**
3748  * @brief Helper function for implementing DEVICE_SHUTDOWN()
3749  *
3750  * This function can be used to help implement the DEVICE_SHUTDOWN()
3751  * for a bus. It calls device_shutdown() for each of the device's
3752  * children.
3753  */
3754 int
3755 bus_generic_shutdown(device_t dev)
3756 {
3757 	device_t child;
3758 
3759 	/*
3760 	 * Shut down children in the reverse order.
3761 	 * See bus_generic_suspend for details.
3762 	 */
3763 	TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3764 		device_shutdown(child);
3765 	}
3766 
3767 	return (0);
3768 }
3769 
3770 /**
3771  * @brief Default function for suspending a child device.
3772  *
3773  * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3774  */
3775 int
3776 bus_generic_suspend_child(device_t dev, device_t child)
3777 {
3778 	int	error;
3779 
3780 	error = DEVICE_SUSPEND(child);
3781 
3782 	if (error == 0)
3783 		child->flags |= DF_SUSPENDED;
3784 
3785 	return (error);
3786 }
3787 
3788 /**
3789  * @brief Default function for resuming a child device.
3790  *
3791  * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3792  */
3793 int
3794 bus_generic_resume_child(device_t dev, device_t child)
3795 {
3796 	DEVICE_RESUME(child);
3797 	child->flags &= ~DF_SUSPENDED;
3798 
3799 	return (0);
3800 }
3801 
3802 /**
3803  * @brief Helper function for implementing DEVICE_SUSPEND()
3804  *
3805  * This function can be used to help implement the DEVICE_SUSPEND()
3806  * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3807  * children. If any call to DEVICE_SUSPEND() fails, the suspend
3808  * operation is aborted and any devices which were suspended are
3809  * resumed immediately by calling their DEVICE_RESUME() methods.
3810  */
3811 int
3812 bus_generic_suspend(device_t dev)
3813 {
3814 	int		error;
3815 	device_t	child;
3816 
3817 	/*
3818 	 * Suspend children in the reverse order.
3819 	 * For most buses all children are equal, so the order does not matter.
3820 	 * Other buses, such as acpi, carefully order their child devices to
3821 	 * express implicit dependencies between them.  For such buses it is
3822 	 * safer to bring down devices in the reverse order.
3823 	 */
3824 	TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3825 		error = BUS_SUSPEND_CHILD(dev, child);
3826 		if (error != 0) {
3827 			child = TAILQ_NEXT(child, link);
3828 			if (child != NULL) {
3829 				TAILQ_FOREACH_FROM(child, &dev->children, link)
3830 					BUS_RESUME_CHILD(dev, child);
3831 			}
3832 			return (error);
3833 		}
3834 	}
3835 	return (0);
3836 }
3837 
3838 /**
3839  * @brief Helper function for implementing DEVICE_RESUME()
3840  *
3841  * This function can be used to help implement the DEVICE_RESUME() for
3842  * a bus. It calls DEVICE_RESUME() on each of the device's children.
3843  */
3844 int
3845 bus_generic_resume(device_t dev)
3846 {
3847 	device_t	child;
3848 
3849 	TAILQ_FOREACH(child, &dev->children, link) {
3850 		BUS_RESUME_CHILD(dev, child);
3851 		/* if resume fails, there's nothing we can usefully do... */
3852 	}
3853 	return (0);
3854 }
3855 
3856 /**
3857  * @brief Helper function for implementing BUS_RESET_POST
3858  *
3859  * Bus can use this function to implement common operations of
3860  * re-attaching or resuming the children after the bus itself was
3861  * reset, and after restoring bus-unique state of children.
3862  *
3863  * @param dev	The bus
3864  * #param flags	DEVF_RESET_*
3865  */
3866 int
3867 bus_helper_reset_post(device_t dev, int flags)
3868 {
3869 	device_t child;
3870 	int error, error1;
3871 
3872 	error = 0;
3873 	TAILQ_FOREACH(child, &dev->children,link) {
3874 		BUS_RESET_POST(dev, child);
3875 		error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3876 		    device_probe_and_attach(child) :
3877 		    BUS_RESUME_CHILD(dev, child);
3878 		if (error == 0 && error1 != 0)
3879 			error = error1;
3880 	}
3881 	return (error);
3882 }
3883 
3884 static void
3885 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3886 {
3887 	child = TAILQ_NEXT(child, link);
3888 	if (child == NULL)
3889 		return;
3890 	TAILQ_FOREACH_FROM(child, &dev->children,link) {
3891 		BUS_RESET_POST(dev, child);
3892 		if ((flags & DEVF_RESET_DETACH) != 0)
3893 			device_probe_and_attach(child);
3894 		else
3895 			BUS_RESUME_CHILD(dev, child);
3896 	}
3897 }
3898 
3899 /**
3900  * @brief Helper function for implementing BUS_RESET_PREPARE
3901  *
3902  * Bus can use this function to implement common operations of
3903  * detaching or suspending the children before the bus itself is
3904  * reset, and then save bus-unique state of children that must
3905  * persists around reset.
3906  *
3907  * @param dev	The bus
3908  * #param flags	DEVF_RESET_*
3909  */
3910 int
3911 bus_helper_reset_prepare(device_t dev, int flags)
3912 {
3913 	device_t child;
3914 	int error;
3915 
3916 	if (dev->state != DS_ATTACHED)
3917 		return (EBUSY);
3918 
3919 	TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3920 		if ((flags & DEVF_RESET_DETACH) != 0) {
3921 			error = device_get_state(child) == DS_ATTACHED ?
3922 			    device_detach(child) : 0;
3923 		} else {
3924 			error = BUS_SUSPEND_CHILD(dev, child);
3925 		}
3926 		if (error == 0) {
3927 			error = BUS_RESET_PREPARE(dev, child);
3928 			if (error != 0) {
3929 				if ((flags & DEVF_RESET_DETACH) != 0)
3930 					device_probe_and_attach(child);
3931 				else
3932 					BUS_RESUME_CHILD(dev, child);
3933 			}
3934 		}
3935 		if (error != 0) {
3936 			bus_helper_reset_prepare_rollback(dev, child, flags);
3937 			return (error);
3938 		}
3939 	}
3940 	return (0);
3941 }
3942 
3943 /**
3944  * @brief Helper function for implementing BUS_PRINT_CHILD().
3945  *
3946  * This function prints the first part of the ascii representation of
3947  * @p child, including its name, unit and description (if any - see
3948  * device_set_desc()).
3949  *
3950  * @returns the number of characters printed
3951  */
3952 int
3953 bus_print_child_header(device_t dev, device_t child)
3954 {
3955 	int	retval = 0;
3956 
3957 	if (device_get_desc(child)) {
3958 		retval += device_printf(child, "<%s>", device_get_desc(child));
3959 	} else {
3960 		retval += printf("%s", device_get_nameunit(child));
3961 	}
3962 
3963 	return (retval);
3964 }
3965 
3966 /**
3967  * @brief Helper function for implementing BUS_PRINT_CHILD().
3968  *
3969  * This function prints the last part of the ascii representation of
3970  * @p child, which consists of the string @c " on " followed by the
3971  * name and unit of the @p dev.
3972  *
3973  * @returns the number of characters printed
3974  */
3975 int
3976 bus_print_child_footer(device_t dev, device_t child)
3977 {
3978 	return (printf(" on %s\n", device_get_nameunit(dev)));
3979 }
3980 
3981 /**
3982  * @brief Helper function for implementing BUS_PRINT_CHILD().
3983  *
3984  * This function prints out the VM domain for the given device.
3985  *
3986  * @returns the number of characters printed
3987  */
3988 int
3989 bus_print_child_domain(device_t dev, device_t child)
3990 {
3991 	int domain;
3992 
3993 	/* No domain? Don't print anything */
3994 	if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3995 		return (0);
3996 
3997 	return (printf(" numa-domain %d", domain));
3998 }
3999 
4000 /**
4001  * @brief Helper function for implementing BUS_PRINT_CHILD().
4002  *
4003  * This function simply calls bus_print_child_header() followed by
4004  * bus_print_child_footer().
4005  *
4006  * @returns the number of characters printed
4007  */
4008 int
4009 bus_generic_print_child(device_t dev, device_t child)
4010 {
4011 	int	retval = 0;
4012 
4013 	retval += bus_print_child_header(dev, child);
4014 	retval += bus_print_child_domain(dev, child);
4015 	retval += bus_print_child_footer(dev, child);
4016 
4017 	return (retval);
4018 }
4019 
4020 /**
4021  * @brief Stub function for implementing BUS_READ_IVAR().
4022  *
4023  * @returns ENOENT
4024  */
4025 int
4026 bus_generic_read_ivar(device_t dev, device_t child, int index,
4027     uintptr_t * result)
4028 {
4029 	return (ENOENT);
4030 }
4031 
4032 /**
4033  * @brief Stub function for implementing BUS_WRITE_IVAR().
4034  *
4035  * @returns ENOENT
4036  */
4037 int
4038 bus_generic_write_ivar(device_t dev, device_t child, int index,
4039     uintptr_t value)
4040 {
4041 	return (ENOENT);
4042 }
4043 
4044 /**
4045  * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
4046  *
4047  * @returns NULL
4048  */
4049 struct resource_list *
4050 bus_generic_get_resource_list(device_t dev, device_t child)
4051 {
4052 	return (NULL);
4053 }
4054 
4055 /**
4056  * @brief Helper function for implementing BUS_DRIVER_ADDED().
4057  *
4058  * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
4059  * DEVICE_IDENTIFY() method to allow it to add new children to the bus
4060  * and then calls device_probe_and_attach() for each unattached child.
4061  */
4062 void
4063 bus_generic_driver_added(device_t dev, driver_t *driver)
4064 {
4065 	device_t child;
4066 
4067 	DEVICE_IDENTIFY(driver, dev);
4068 	TAILQ_FOREACH(child, &dev->children, link) {
4069 		if (child->state == DS_NOTPRESENT ||
4070 		    (child->flags & DF_REBID))
4071 			device_probe_and_attach(child);
4072 	}
4073 }
4074 
4075 /**
4076  * @brief Helper function for implementing BUS_NEW_PASS().
4077  *
4078  * This implementing of BUS_NEW_PASS() first calls the identify
4079  * routines for any drivers that probe at the current pass.  Then it
4080  * walks the list of devices for this bus.  If a device is already
4081  * attached, then it calls BUS_NEW_PASS() on that device.  If the
4082  * device is not already attached, it attempts to attach a driver to
4083  * it.
4084  */
4085 void
4086 bus_generic_new_pass(device_t dev)
4087 {
4088 	driverlink_t dl;
4089 	devclass_t dc;
4090 	device_t child;
4091 
4092 	dc = dev->devclass;
4093 	TAILQ_FOREACH(dl, &dc->drivers, link) {
4094 		if (dl->pass == bus_current_pass)
4095 			DEVICE_IDENTIFY(dl->driver, dev);
4096 	}
4097 	TAILQ_FOREACH(child, &dev->children, link) {
4098 		if (child->state >= DS_ATTACHED)
4099 			BUS_NEW_PASS(child);
4100 		else if (child->state == DS_NOTPRESENT)
4101 			device_probe_and_attach(child);
4102 	}
4103 }
4104 
4105 /**
4106  * @brief Helper function for implementing BUS_SETUP_INTR().
4107  *
4108  * This simple implementation of BUS_SETUP_INTR() simply calls the
4109  * BUS_SETUP_INTR() method of the parent of @p dev.
4110  */
4111 int
4112 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
4113     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
4114     void **cookiep)
4115 {
4116 	/* Propagate up the bus hierarchy until someone handles it. */
4117 	if (dev->parent)
4118 		return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
4119 		    filter, intr, arg, cookiep));
4120 	return (EINVAL);
4121 }
4122 
4123 /**
4124  * @brief Helper function for implementing BUS_TEARDOWN_INTR().
4125  *
4126  * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
4127  * BUS_TEARDOWN_INTR() method of the parent of @p dev.
4128  */
4129 int
4130 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
4131     void *cookie)
4132 {
4133 	/* Propagate up the bus hierarchy until someone handles it. */
4134 	if (dev->parent)
4135 		return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
4136 	return (EINVAL);
4137 }
4138 
4139 /**
4140  * @brief Helper function for implementing BUS_SUSPEND_INTR().
4141  *
4142  * This simple implementation of BUS_SUSPEND_INTR() simply calls the
4143  * BUS_SUSPEND_INTR() method of the parent of @p dev.
4144  */
4145 int
4146 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
4147 {
4148 	/* Propagate up the bus hierarchy until someone handles it. */
4149 	if (dev->parent)
4150 		return (BUS_SUSPEND_INTR(dev->parent, child, irq));
4151 	return (EINVAL);
4152 }
4153 
4154 /**
4155  * @brief Helper function for implementing BUS_RESUME_INTR().
4156  *
4157  * This simple implementation of BUS_RESUME_INTR() simply calls the
4158  * BUS_RESUME_INTR() method of the parent of @p dev.
4159  */
4160 int
4161 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
4162 {
4163 	/* Propagate up the bus hierarchy until someone handles it. */
4164 	if (dev->parent)
4165 		return (BUS_RESUME_INTR(dev->parent, child, irq));
4166 	return (EINVAL);
4167 }
4168 
4169 /**
4170  * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4171  *
4172  * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
4173  * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
4174  */
4175 int
4176 bus_generic_adjust_resource(device_t dev, device_t child, int type,
4177     struct resource *r, rman_res_t start, rman_res_t end)
4178 {
4179 	/* Propagate up the bus hierarchy until someone handles it. */
4180 	if (dev->parent)
4181 		return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
4182 		    end));
4183 	return (EINVAL);
4184 }
4185 
4186 /**
4187  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4188  *
4189  * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
4190  * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4191  */
4192 struct resource *
4193 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4194     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4195 {
4196 	/* Propagate up the bus hierarchy until someone handles it. */
4197 	if (dev->parent)
4198 		return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4199 		    start, end, count, flags));
4200 	return (NULL);
4201 }
4202 
4203 /**
4204  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4205  *
4206  * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4207  * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4208  */
4209 int
4210 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4211     struct resource *r)
4212 {
4213 	/* Propagate up the bus hierarchy until someone handles it. */
4214 	if (dev->parent)
4215 		return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4216 		    r));
4217 	return (EINVAL);
4218 }
4219 
4220 /**
4221  * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4222  *
4223  * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4224  * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4225  */
4226 int
4227 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4228     struct resource *r)
4229 {
4230 	/* Propagate up the bus hierarchy until someone handles it. */
4231 	if (dev->parent)
4232 		return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4233 		    r));
4234 	return (EINVAL);
4235 }
4236 
4237 /**
4238  * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4239  *
4240  * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4241  * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4242  */
4243 int
4244 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4245     int rid, struct resource *r)
4246 {
4247 	/* Propagate up the bus hierarchy until someone handles it. */
4248 	if (dev->parent)
4249 		return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4250 		    r));
4251 	return (EINVAL);
4252 }
4253 
4254 /**
4255  * @brief Helper function for implementing BUS_MAP_RESOURCE().
4256  *
4257  * This simple implementation of BUS_MAP_RESOURCE() simply calls the
4258  * BUS_MAP_RESOURCE() method of the parent of @p dev.
4259  */
4260 int
4261 bus_generic_map_resource(device_t dev, device_t child, int type,
4262     struct resource *r, struct resource_map_request *args,
4263     struct resource_map *map)
4264 {
4265 	/* Propagate up the bus hierarchy until someone handles it. */
4266 	if (dev->parent)
4267 		return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
4268 		    map));
4269 	return (EINVAL);
4270 }
4271 
4272 /**
4273  * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
4274  *
4275  * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
4276  * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
4277  */
4278 int
4279 bus_generic_unmap_resource(device_t dev, device_t child, int type,
4280     struct resource *r, struct resource_map *map)
4281 {
4282 	/* Propagate up the bus hierarchy until someone handles it. */
4283 	if (dev->parent)
4284 		return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
4285 	return (EINVAL);
4286 }
4287 
4288 /**
4289  * @brief Helper function for implementing BUS_BIND_INTR().
4290  *
4291  * This simple implementation of BUS_BIND_INTR() simply calls the
4292  * BUS_BIND_INTR() method of the parent of @p dev.
4293  */
4294 int
4295 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4296     int cpu)
4297 {
4298 	/* Propagate up the bus hierarchy until someone handles it. */
4299 	if (dev->parent)
4300 		return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4301 	return (EINVAL);
4302 }
4303 
4304 /**
4305  * @brief Helper function for implementing BUS_CONFIG_INTR().
4306  *
4307  * This simple implementation of BUS_CONFIG_INTR() simply calls the
4308  * BUS_CONFIG_INTR() method of the parent of @p dev.
4309  */
4310 int
4311 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4312     enum intr_polarity pol)
4313 {
4314 	/* Propagate up the bus hierarchy until someone handles it. */
4315 	if (dev->parent)
4316 		return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4317 	return (EINVAL);
4318 }
4319 
4320 /**
4321  * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4322  *
4323  * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4324  * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4325  */
4326 int
4327 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4328     void *cookie, const char *descr)
4329 {
4330 	/* Propagate up the bus hierarchy until someone handles it. */
4331 	if (dev->parent)
4332 		return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4333 		    descr));
4334 	return (EINVAL);
4335 }
4336 
4337 /**
4338  * @brief Helper function for implementing BUS_GET_CPUS().
4339  *
4340  * This simple implementation of BUS_GET_CPUS() simply calls the
4341  * BUS_GET_CPUS() method of the parent of @p dev.
4342  */
4343 int
4344 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4345     size_t setsize, cpuset_t *cpuset)
4346 {
4347 	/* Propagate up the bus hierarchy until someone handles it. */
4348 	if (dev->parent != NULL)
4349 		return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4350 	return (EINVAL);
4351 }
4352 
4353 /**
4354  * @brief Helper function for implementing BUS_GET_DMA_TAG().
4355  *
4356  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4357  * BUS_GET_DMA_TAG() method of the parent of @p dev.
4358  */
4359 bus_dma_tag_t
4360 bus_generic_get_dma_tag(device_t dev, device_t child)
4361 {
4362 	/* Propagate up the bus hierarchy until someone handles it. */
4363 	if (dev->parent != NULL)
4364 		return (BUS_GET_DMA_TAG(dev->parent, child));
4365 	return (NULL);
4366 }
4367 
4368 /**
4369  * @brief Helper function for implementing BUS_GET_BUS_TAG().
4370  *
4371  * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4372  * BUS_GET_BUS_TAG() method of the parent of @p dev.
4373  */
4374 bus_space_tag_t
4375 bus_generic_get_bus_tag(device_t dev, device_t child)
4376 {
4377 	/* Propagate up the bus hierarchy until someone handles it. */
4378 	if (dev->parent != NULL)
4379 		return (BUS_GET_BUS_TAG(dev->parent, child));
4380 	return ((bus_space_tag_t)0);
4381 }
4382 
4383 /**
4384  * @brief Helper function for implementing BUS_GET_RESOURCE().
4385  *
4386  * This implementation of BUS_GET_RESOURCE() uses the
4387  * resource_list_find() function to do most of the work. It calls
4388  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4389  * search.
4390  */
4391 int
4392 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4393     rman_res_t *startp, rman_res_t *countp)
4394 {
4395 	struct resource_list *		rl = NULL;
4396 	struct resource_list_entry *	rle = NULL;
4397 
4398 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4399 	if (!rl)
4400 		return (EINVAL);
4401 
4402 	rle = resource_list_find(rl, type, rid);
4403 	if (!rle)
4404 		return (ENOENT);
4405 
4406 	if (startp)
4407 		*startp = rle->start;
4408 	if (countp)
4409 		*countp = rle->count;
4410 
4411 	return (0);
4412 }
4413 
4414 /**
4415  * @brief Helper function for implementing BUS_SET_RESOURCE().
4416  *
4417  * This implementation of BUS_SET_RESOURCE() uses the
4418  * resource_list_add() function to do most of the work. It calls
4419  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4420  * edit.
4421  */
4422 int
4423 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4424     rman_res_t start, rman_res_t count)
4425 {
4426 	struct resource_list *		rl = NULL;
4427 
4428 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4429 	if (!rl)
4430 		return (EINVAL);
4431 
4432 	resource_list_add(rl, type, rid, start, (start + count - 1), count);
4433 
4434 	return (0);
4435 }
4436 
4437 /**
4438  * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4439  *
4440  * This implementation of BUS_DELETE_RESOURCE() uses the
4441  * resource_list_delete() function to do most of the work. It calls
4442  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4443  * edit.
4444  */
4445 void
4446 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4447 {
4448 	struct resource_list *		rl = NULL;
4449 
4450 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4451 	if (!rl)
4452 		return;
4453 
4454 	resource_list_delete(rl, type, rid);
4455 
4456 	return;
4457 }
4458 
4459 /**
4460  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4461  *
4462  * This implementation of BUS_RELEASE_RESOURCE() uses the
4463  * resource_list_release() function to do most of the work. It calls
4464  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4465  */
4466 int
4467 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4468     int rid, struct resource *r)
4469 {
4470 	struct resource_list *		rl = NULL;
4471 
4472 	if (device_get_parent(child) != dev)
4473 		return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4474 		    type, rid, r));
4475 
4476 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4477 	if (!rl)
4478 		return (EINVAL);
4479 
4480 	return (resource_list_release(rl, dev, child, type, rid, r));
4481 }
4482 
4483 /**
4484  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4485  *
4486  * This implementation of BUS_ALLOC_RESOURCE() uses the
4487  * resource_list_alloc() function to do most of the work. It calls
4488  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4489  */
4490 struct resource *
4491 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4492     int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4493 {
4494 	struct resource_list *		rl = NULL;
4495 
4496 	if (device_get_parent(child) != dev)
4497 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4498 		    type, rid, start, end, count, flags));
4499 
4500 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4501 	if (!rl)
4502 		return (NULL);
4503 
4504 	return (resource_list_alloc(rl, dev, child, type, rid,
4505 	    start, end, count, flags));
4506 }
4507 
4508 /**
4509  * @brief Helper function for implementing BUS_CHILD_PRESENT().
4510  *
4511  * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4512  * BUS_CHILD_PRESENT() method of the parent of @p dev.
4513  */
4514 int
4515 bus_generic_child_present(device_t dev, device_t child)
4516 {
4517 	return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4518 }
4519 
4520 int
4521 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4522 {
4523 	if (dev->parent)
4524 		return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4525 
4526 	return (ENOENT);
4527 }
4528 
4529 /**
4530  * @brief Helper function for implementing BUS_RESCAN().
4531  *
4532  * This null implementation of BUS_RESCAN() always fails to indicate
4533  * the bus does not support rescanning.
4534  */
4535 int
4536 bus_null_rescan(device_t dev)
4537 {
4538 	return (ENXIO);
4539 }
4540 
4541 /*
4542  * Some convenience functions to make it easier for drivers to use the
4543  * resource-management functions.  All these really do is hide the
4544  * indirection through the parent's method table, making for slightly
4545  * less-wordy code.  In the future, it might make sense for this code
4546  * to maintain some sort of a list of resources allocated by each device.
4547  */
4548 
4549 int
4550 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4551     struct resource **res)
4552 {
4553 	int i;
4554 
4555 	for (i = 0; rs[i].type != -1; i++)
4556 		res[i] = NULL;
4557 	for (i = 0; rs[i].type != -1; i++) {
4558 		res[i] = bus_alloc_resource_any(dev,
4559 		    rs[i].type, &rs[i].rid, rs[i].flags);
4560 		if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4561 			bus_release_resources(dev, rs, res);
4562 			return (ENXIO);
4563 		}
4564 	}
4565 	return (0);
4566 }
4567 
4568 void
4569 bus_release_resources(device_t dev, const struct resource_spec *rs,
4570     struct resource **res)
4571 {
4572 	int i;
4573 
4574 	for (i = 0; rs[i].type != -1; i++)
4575 		if (res[i] != NULL) {
4576 			bus_release_resource(
4577 			    dev, rs[i].type, rs[i].rid, res[i]);
4578 			res[i] = NULL;
4579 		}
4580 }
4581 
4582 /**
4583  * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4584  *
4585  * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4586  * parent of @p dev.
4587  */
4588 struct resource *
4589 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4590     rman_res_t end, rman_res_t count, u_int flags)
4591 {
4592 	struct resource *res;
4593 
4594 	if (dev->parent == NULL)
4595 		return (NULL);
4596 	res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4597 	    count, flags);
4598 	return (res);
4599 }
4600 
4601 /**
4602  * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4603  *
4604  * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4605  * parent of @p dev.
4606  */
4607 int
4608 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4609     rman_res_t end)
4610 {
4611 	if (dev->parent == NULL)
4612 		return (EINVAL);
4613 	return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4614 }
4615 
4616 /**
4617  * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4618  *
4619  * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4620  * parent of @p dev.
4621  */
4622 int
4623 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4624 {
4625 	if (dev->parent == NULL)
4626 		return (EINVAL);
4627 	return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4628 }
4629 
4630 /**
4631  * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4632  *
4633  * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4634  * parent of @p dev.
4635  */
4636 int
4637 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4638 {
4639 	if (dev->parent == NULL)
4640 		return (EINVAL);
4641 	return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4642 }
4643 
4644 /**
4645  * @brief Wrapper function for BUS_MAP_RESOURCE().
4646  *
4647  * This function simply calls the BUS_MAP_RESOURCE() method of the
4648  * parent of @p dev.
4649  */
4650 int
4651 bus_map_resource(device_t dev, int type, struct resource *r,
4652     struct resource_map_request *args, struct resource_map *map)
4653 {
4654 	if (dev->parent == NULL)
4655 		return (EINVAL);
4656 	return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4657 }
4658 
4659 /**
4660  * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4661  *
4662  * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4663  * parent of @p dev.
4664  */
4665 int
4666 bus_unmap_resource(device_t dev, int type, struct resource *r,
4667     struct resource_map *map)
4668 {
4669 	if (dev->parent == NULL)
4670 		return (EINVAL);
4671 	return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4672 }
4673 
4674 /**
4675  * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4676  *
4677  * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4678  * parent of @p dev.
4679  */
4680 int
4681 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4682 {
4683 	int rv;
4684 
4685 	if (dev->parent == NULL)
4686 		return (EINVAL);
4687 	rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4688 	return (rv);
4689 }
4690 
4691 /**
4692  * @brief Wrapper function for BUS_SETUP_INTR().
4693  *
4694  * This function simply calls the BUS_SETUP_INTR() method of the
4695  * parent of @p dev.
4696  */
4697 int
4698 bus_setup_intr(device_t dev, struct resource *r, int flags,
4699     driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4700 {
4701 	int error;
4702 
4703 	if (dev->parent == NULL)
4704 		return (EINVAL);
4705 	error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4706 	    arg, cookiep);
4707 	if (error != 0)
4708 		return (error);
4709 	if (handler != NULL && !(flags & INTR_MPSAFE))
4710 		device_printf(dev, "[GIANT-LOCKED]\n");
4711 	return (0);
4712 }
4713 
4714 /**
4715  * @brief Wrapper function for BUS_TEARDOWN_INTR().
4716  *
4717  * This function simply calls the BUS_TEARDOWN_INTR() method of the
4718  * parent of @p dev.
4719  */
4720 int
4721 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4722 {
4723 	if (dev->parent == NULL)
4724 		return (EINVAL);
4725 	return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4726 }
4727 
4728 /**
4729  * @brief Wrapper function for BUS_SUSPEND_INTR().
4730  *
4731  * This function simply calls the BUS_SUSPEND_INTR() method of the
4732  * parent of @p dev.
4733  */
4734 int
4735 bus_suspend_intr(device_t dev, struct resource *r)
4736 {
4737 	if (dev->parent == NULL)
4738 		return (EINVAL);
4739 	return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4740 }
4741 
4742 /**
4743  * @brief Wrapper function for BUS_RESUME_INTR().
4744  *
4745  * This function simply calls the BUS_RESUME_INTR() method of the
4746  * parent of @p dev.
4747  */
4748 int
4749 bus_resume_intr(device_t dev, struct resource *r)
4750 {
4751 	if (dev->parent == NULL)
4752 		return (EINVAL);
4753 	return (BUS_RESUME_INTR(dev->parent, dev, r));
4754 }
4755 
4756 /**
4757  * @brief Wrapper function for BUS_BIND_INTR().
4758  *
4759  * This function simply calls the BUS_BIND_INTR() method of the
4760  * parent of @p dev.
4761  */
4762 int
4763 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4764 {
4765 	if (dev->parent == NULL)
4766 		return (EINVAL);
4767 	return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4768 }
4769 
4770 /**
4771  * @brief Wrapper function for BUS_DESCRIBE_INTR().
4772  *
4773  * This function first formats the requested description into a
4774  * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4775  * the parent of @p dev.
4776  */
4777 int
4778 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4779     const char *fmt, ...)
4780 {
4781 	va_list ap;
4782 	char descr[MAXCOMLEN + 1];
4783 
4784 	if (dev->parent == NULL)
4785 		return (EINVAL);
4786 	va_start(ap, fmt);
4787 	vsnprintf(descr, sizeof(descr), fmt, ap);
4788 	va_end(ap);
4789 	return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4790 }
4791 
4792 /**
4793  * @brief Wrapper function for BUS_SET_RESOURCE().
4794  *
4795  * This function simply calls the BUS_SET_RESOURCE() method of the
4796  * parent of @p dev.
4797  */
4798 int
4799 bus_set_resource(device_t dev, int type, int rid,
4800     rman_res_t start, rman_res_t count)
4801 {
4802 	return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4803 	    start, count));
4804 }
4805 
4806 /**
4807  * @brief Wrapper function for BUS_GET_RESOURCE().
4808  *
4809  * This function simply calls the BUS_GET_RESOURCE() method of the
4810  * parent of @p dev.
4811  */
4812 int
4813 bus_get_resource(device_t dev, int type, int rid,
4814     rman_res_t *startp, rman_res_t *countp)
4815 {
4816 	return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4817 	    startp, countp));
4818 }
4819 
4820 /**
4821  * @brief Wrapper function for BUS_GET_RESOURCE().
4822  *
4823  * This function simply calls the BUS_GET_RESOURCE() method of the
4824  * parent of @p dev and returns the start value.
4825  */
4826 rman_res_t
4827 bus_get_resource_start(device_t dev, int type, int rid)
4828 {
4829 	rman_res_t start;
4830 	rman_res_t count;
4831 	int error;
4832 
4833 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4834 	    &start, &count);
4835 	if (error)
4836 		return (0);
4837 	return (start);
4838 }
4839 
4840 /**
4841  * @brief Wrapper function for BUS_GET_RESOURCE().
4842  *
4843  * This function simply calls the BUS_GET_RESOURCE() method of the
4844  * parent of @p dev and returns the count value.
4845  */
4846 rman_res_t
4847 bus_get_resource_count(device_t dev, int type, int rid)
4848 {
4849 	rman_res_t start;
4850 	rman_res_t count;
4851 	int error;
4852 
4853 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4854 	    &start, &count);
4855 	if (error)
4856 		return (0);
4857 	return (count);
4858 }
4859 
4860 /**
4861  * @brief Wrapper function for BUS_DELETE_RESOURCE().
4862  *
4863  * This function simply calls the BUS_DELETE_RESOURCE() method of the
4864  * parent of @p dev.
4865  */
4866 void
4867 bus_delete_resource(device_t dev, int type, int rid)
4868 {
4869 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4870 }
4871 
4872 /**
4873  * @brief Wrapper function for BUS_CHILD_PRESENT().
4874  *
4875  * This function simply calls the BUS_CHILD_PRESENT() method of the
4876  * parent of @p dev.
4877  */
4878 int
4879 bus_child_present(device_t child)
4880 {
4881 	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4882 }
4883 
4884 /**
4885  * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4886  *
4887  * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4888  * parent of @p dev.
4889  */
4890 int
4891 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4892 {
4893 	device_t parent;
4894 
4895 	parent = device_get_parent(child);
4896 	if (parent == NULL) {
4897 		*buf = '\0';
4898 		return (0);
4899 	}
4900 	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4901 }
4902 
4903 /**
4904  * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4905  *
4906  * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4907  * parent of @p dev.
4908  */
4909 int
4910 bus_child_location_str(device_t child, char *buf, size_t buflen)
4911 {
4912 	device_t parent;
4913 
4914 	parent = device_get_parent(child);
4915 	if (parent == NULL) {
4916 		*buf = '\0';
4917 		return (0);
4918 	}
4919 	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4920 }
4921 
4922 /**
4923  * @brief Wrapper function for BUS_GET_CPUS().
4924  *
4925  * This function simply calls the BUS_GET_CPUS() method of the
4926  * parent of @p dev.
4927  */
4928 int
4929 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
4930 {
4931 	device_t parent;
4932 
4933 	parent = device_get_parent(dev);
4934 	if (parent == NULL)
4935 		return (EINVAL);
4936 	return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
4937 }
4938 
4939 /**
4940  * @brief Wrapper function for BUS_GET_DMA_TAG().
4941  *
4942  * This function simply calls the BUS_GET_DMA_TAG() method of the
4943  * parent of @p dev.
4944  */
4945 bus_dma_tag_t
4946 bus_get_dma_tag(device_t dev)
4947 {
4948 	device_t parent;
4949 
4950 	parent = device_get_parent(dev);
4951 	if (parent == NULL)
4952 		return (NULL);
4953 	return (BUS_GET_DMA_TAG(parent, dev));
4954 }
4955 
4956 /**
4957  * @brief Wrapper function for BUS_GET_BUS_TAG().
4958  *
4959  * This function simply calls the BUS_GET_BUS_TAG() method of the
4960  * parent of @p dev.
4961  */
4962 bus_space_tag_t
4963 bus_get_bus_tag(device_t dev)
4964 {
4965 	device_t parent;
4966 
4967 	parent = device_get_parent(dev);
4968 	if (parent == NULL)
4969 		return ((bus_space_tag_t)0);
4970 	return (BUS_GET_BUS_TAG(parent, dev));
4971 }
4972 
4973 /**
4974  * @brief Wrapper function for BUS_GET_DOMAIN().
4975  *
4976  * This function simply calls the BUS_GET_DOMAIN() method of the
4977  * parent of @p dev.
4978  */
4979 int
4980 bus_get_domain(device_t dev, int *domain)
4981 {
4982 	return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
4983 }
4984 
4985 /* Resume all devices and then notify userland that we're up again. */
4986 static int
4987 root_resume(device_t dev)
4988 {
4989 	int error;
4990 
4991 	error = bus_generic_resume(dev);
4992 	if (error == 0) {
4993 		devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
4994 		devctl_notify("kernel", "power", "resume", NULL);
4995 	}
4996 	return (error);
4997 }
4998 
4999 static int
5000 root_print_child(device_t dev, device_t child)
5001 {
5002 	int	retval = 0;
5003 
5004 	retval += bus_print_child_header(dev, child);
5005 	retval += printf("\n");
5006 
5007 	return (retval);
5008 }
5009 
5010 static int
5011 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
5012     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
5013 {
5014 	/*
5015 	 * If an interrupt mapping gets to here something bad has happened.
5016 	 */
5017 	panic("root_setup_intr");
5018 }
5019 
5020 /*
5021  * If we get here, assume that the device is permanent and really is
5022  * present in the system.  Removable bus drivers are expected to intercept
5023  * this call long before it gets here.  We return -1 so that drivers that
5024  * really care can check vs -1 or some ERRNO returned higher in the food
5025  * chain.
5026  */
5027 static int
5028 root_child_present(device_t dev, device_t child)
5029 {
5030 	return (-1);
5031 }
5032 
5033 static int
5034 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
5035     cpuset_t *cpuset)
5036 {
5037 	switch (op) {
5038 	case INTR_CPUS:
5039 		/* Default to returning the set of all CPUs. */
5040 		if (setsize != sizeof(cpuset_t))
5041 			return (EINVAL);
5042 		*cpuset = all_cpus;
5043 		return (0);
5044 	default:
5045 		return (EINVAL);
5046 	}
5047 }
5048 
5049 static kobj_method_t root_methods[] = {
5050 	/* Device interface */
5051 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
5052 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
5053 	KOBJMETHOD(device_resume,	root_resume),
5054 
5055 	/* Bus interface */
5056 	KOBJMETHOD(bus_print_child,	root_print_child),
5057 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
5058 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
5059 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
5060 	KOBJMETHOD(bus_child_present,	root_child_present),
5061 	KOBJMETHOD(bus_get_cpus,	root_get_cpus),
5062 
5063 	KOBJMETHOD_END
5064 };
5065 
5066 static driver_t root_driver = {
5067 	"root",
5068 	root_methods,
5069 	1,			/* no softc */
5070 };
5071 
5072 device_t	root_bus;
5073 devclass_t	root_devclass;
5074 
5075 static int
5076 root_bus_module_handler(module_t mod, int what, void* arg)
5077 {
5078 	switch (what) {
5079 	case MOD_LOAD:
5080 		TAILQ_INIT(&bus_data_devices);
5081 		kobj_class_compile((kobj_class_t) &root_driver);
5082 		root_bus = make_device(NULL, "root", 0);
5083 		root_bus->desc = "System root bus";
5084 		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5085 		root_bus->driver = &root_driver;
5086 		root_bus->state = DS_ATTACHED;
5087 		root_devclass = devclass_find_internal("root", NULL, FALSE);
5088 		devinit();
5089 		return (0);
5090 
5091 	case MOD_SHUTDOWN:
5092 		device_shutdown(root_bus);
5093 		return (0);
5094 	default:
5095 		return (EOPNOTSUPP);
5096 	}
5097 
5098 	return (0);
5099 }
5100 
5101 static moduledata_t root_bus_mod = {
5102 	"rootbus",
5103 	root_bus_module_handler,
5104 	NULL
5105 };
5106 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5107 
5108 /**
5109  * @brief Automatically configure devices
5110  *
5111  * This function begins the autoconfiguration process by calling
5112  * device_probe_and_attach() for each child of the @c root0 device.
5113  */
5114 void
5115 root_bus_configure(void)
5116 {
5117 	PDEBUG(("."));
5118 
5119 	/* Eventually this will be split up, but this is sufficient for now. */
5120 	bus_set_pass(BUS_PASS_DEFAULT);
5121 }
5122 
5123 /**
5124  * @brief Module handler for registering device drivers
5125  *
5126  * This module handler is used to automatically register device
5127  * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5128  * devclass_add_driver() for the driver described by the
5129  * driver_module_data structure pointed to by @p arg
5130  */
5131 int
5132 driver_module_handler(module_t mod, int what, void *arg)
5133 {
5134 	struct driver_module_data *dmd;
5135 	devclass_t bus_devclass;
5136 	kobj_class_t driver;
5137 	int error, pass;
5138 
5139 	dmd = (struct driver_module_data *)arg;
5140 	bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5141 	error = 0;
5142 
5143 	switch (what) {
5144 	case MOD_LOAD:
5145 		if (dmd->dmd_chainevh)
5146 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5147 
5148 		pass = dmd->dmd_pass;
5149 		driver = dmd->dmd_driver;
5150 		PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5151 		    DRIVERNAME(driver), dmd->dmd_busname, pass));
5152 		error = devclass_add_driver(bus_devclass, driver, pass,
5153 		    dmd->dmd_devclass);
5154 		break;
5155 
5156 	case MOD_UNLOAD:
5157 		PDEBUG(("Unloading module: driver %s from bus %s",
5158 		    DRIVERNAME(dmd->dmd_driver),
5159 		    dmd->dmd_busname));
5160 		error = devclass_delete_driver(bus_devclass,
5161 		    dmd->dmd_driver);
5162 
5163 		if (!error && dmd->dmd_chainevh)
5164 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5165 		break;
5166 	case MOD_QUIESCE:
5167 		PDEBUG(("Quiesce module: driver %s from bus %s",
5168 		    DRIVERNAME(dmd->dmd_driver),
5169 		    dmd->dmd_busname));
5170 		error = devclass_quiesce_driver(bus_devclass,
5171 		    dmd->dmd_driver);
5172 
5173 		if (!error && dmd->dmd_chainevh)
5174 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5175 		break;
5176 	default:
5177 		error = EOPNOTSUPP;
5178 		break;
5179 	}
5180 
5181 	return (error);
5182 }
5183 
5184 /**
5185  * @brief Enumerate all hinted devices for this bus.
5186  *
5187  * Walks through the hints for this bus and calls the bus_hinted_child
5188  * routine for each one it fines.  It searches first for the specific
5189  * bus that's being probed for hinted children (eg isa0), and then for
5190  * generic children (eg isa).
5191  *
5192  * @param	dev	bus device to enumerate
5193  */
5194 void
5195 bus_enumerate_hinted_children(device_t bus)
5196 {
5197 	int i;
5198 	const char *dname, *busname;
5199 	int dunit;
5200 
5201 	/*
5202 	 * enumerate all devices on the specific bus
5203 	 */
5204 	busname = device_get_nameunit(bus);
5205 	i = 0;
5206 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5207 		BUS_HINTED_CHILD(bus, dname, dunit);
5208 
5209 	/*
5210 	 * and all the generic ones.
5211 	 */
5212 	busname = device_get_name(bus);
5213 	i = 0;
5214 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5215 		BUS_HINTED_CHILD(bus, dname, dunit);
5216 }
5217 
5218 #ifdef BUS_DEBUG
5219 
5220 /* the _short versions avoid iteration by not calling anything that prints
5221  * more than oneliners. I love oneliners.
5222  */
5223 
5224 static void
5225 print_device_short(device_t dev, int indent)
5226 {
5227 	if (!dev)
5228 		return;
5229 
5230 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5231 	    dev->unit, dev->desc,
5232 	    (dev->parent? "":"no "),
5233 	    (TAILQ_EMPTY(&dev->children)? "no ":""),
5234 	    (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5235 	    (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5236 	    (dev->flags&DF_WILDCARD? "wildcard,":""),
5237 	    (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5238 	    (dev->flags&DF_REBID? "rebiddable,":""),
5239 	    (dev->flags&DF_SUSPENDED? "suspended,":""),
5240 	    (dev->ivars? "":"no "),
5241 	    (dev->softc? "":"no "),
5242 	    dev->busy));
5243 }
5244 
5245 static void
5246 print_device(device_t dev, int indent)
5247 {
5248 	if (!dev)
5249 		return;
5250 
5251 	print_device_short(dev, indent);
5252 
5253 	indentprintf(("Parent:\n"));
5254 	print_device_short(dev->parent, indent+1);
5255 	indentprintf(("Driver:\n"));
5256 	print_driver_short(dev->driver, indent+1);
5257 	indentprintf(("Devclass:\n"));
5258 	print_devclass_short(dev->devclass, indent+1);
5259 }
5260 
5261 void
5262 print_device_tree_short(device_t dev, int indent)
5263 /* print the device and all its children (indented) */
5264 {
5265 	device_t child;
5266 
5267 	if (!dev)
5268 		return;
5269 
5270 	print_device_short(dev, indent);
5271 
5272 	TAILQ_FOREACH(child, &dev->children, link) {
5273 		print_device_tree_short(child, indent+1);
5274 	}
5275 }
5276 
5277 void
5278 print_device_tree(device_t dev, int indent)
5279 /* print the device and all its children (indented) */
5280 {
5281 	device_t child;
5282 
5283 	if (!dev)
5284 		return;
5285 
5286 	print_device(dev, indent);
5287 
5288 	TAILQ_FOREACH(child, &dev->children, link) {
5289 		print_device_tree(child, indent+1);
5290 	}
5291 }
5292 
5293 static void
5294 print_driver_short(driver_t *driver, int indent)
5295 {
5296 	if (!driver)
5297 		return;
5298 
5299 	indentprintf(("driver %s: softc size = %zd\n",
5300 	    driver->name, driver->size));
5301 }
5302 
5303 static void
5304 print_driver(driver_t *driver, int indent)
5305 {
5306 	if (!driver)
5307 		return;
5308 
5309 	print_driver_short(driver, indent);
5310 }
5311 
5312 static void
5313 print_driver_list(driver_list_t drivers, int indent)
5314 {
5315 	driverlink_t driver;
5316 
5317 	TAILQ_FOREACH(driver, &drivers, link) {
5318 		print_driver(driver->driver, indent);
5319 	}
5320 }
5321 
5322 static void
5323 print_devclass_short(devclass_t dc, int indent)
5324 {
5325 	if ( !dc )
5326 		return;
5327 
5328 	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5329 }
5330 
5331 static void
5332 print_devclass(devclass_t dc, int indent)
5333 {
5334 	int i;
5335 
5336 	if ( !dc )
5337 		return;
5338 
5339 	print_devclass_short(dc, indent);
5340 	indentprintf(("Drivers:\n"));
5341 	print_driver_list(dc->drivers, indent+1);
5342 
5343 	indentprintf(("Devices:\n"));
5344 	for (i = 0; i < dc->maxunit; i++)
5345 		if (dc->devices[i])
5346 			print_device(dc->devices[i], indent+1);
5347 }
5348 
5349 void
5350 print_devclass_list_short(void)
5351 {
5352 	devclass_t dc;
5353 
5354 	printf("Short listing of devclasses, drivers & devices:\n");
5355 	TAILQ_FOREACH(dc, &devclasses, link) {
5356 		print_devclass_short(dc, 0);
5357 	}
5358 }
5359 
5360 void
5361 print_devclass_list(void)
5362 {
5363 	devclass_t dc;
5364 
5365 	printf("Full listing of devclasses, drivers & devices:\n");
5366 	TAILQ_FOREACH(dc, &devclasses, link) {
5367 		print_devclass(dc, 0);
5368 	}
5369 }
5370 
5371 #endif
5372 
5373 /*
5374  * User-space access to the device tree.
5375  *
5376  * We implement a small set of nodes:
5377  *
5378  * hw.bus			Single integer read method to obtain the
5379  *				current generation count.
5380  * hw.bus.devices		Reads the entire device tree in flat space.
5381  * hw.bus.rman			Resource manager interface
5382  *
5383  * We might like to add the ability to scan devclasses and/or drivers to
5384  * determine what else is currently loaded/available.
5385  */
5386 
5387 static int
5388 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5389 {
5390 	struct u_businfo	ubus;
5391 
5392 	ubus.ub_version = BUS_USER_VERSION;
5393 	ubus.ub_generation = bus_data_generation;
5394 
5395 	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5396 }
5397 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5398     CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5399     "bus-related data");
5400 
5401 static int
5402 sysctl_devices(SYSCTL_HANDLER_ARGS)
5403 {
5404 	int			*name = (int *)arg1;
5405 	u_int			namelen = arg2;
5406 	int			index;
5407 	device_t		dev;
5408 	struct u_device		*udev;
5409 	int			error;
5410 	char			*walker, *ep;
5411 
5412 	if (namelen != 2)
5413 		return (EINVAL);
5414 
5415 	if (bus_data_generation_check(name[0]))
5416 		return (EINVAL);
5417 
5418 	index = name[1];
5419 
5420 	/*
5421 	 * Scan the list of devices, looking for the requested index.
5422 	 */
5423 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5424 		if (index-- == 0)
5425 			break;
5426 	}
5427 	if (dev == NULL)
5428 		return (ENOENT);
5429 
5430 	/*
5431 	 * Populate the return item, careful not to overflow the buffer.
5432 	 */
5433 	udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5434 	if (udev == NULL)
5435 		return (ENOMEM);
5436 	udev->dv_handle = (uintptr_t)dev;
5437 	udev->dv_parent = (uintptr_t)dev->parent;
5438 	udev->dv_devflags = dev->devflags;
5439 	udev->dv_flags = dev->flags;
5440 	udev->dv_state = dev->state;
5441 	walker = udev->dv_fields;
5442 	ep = walker + sizeof(udev->dv_fields);
5443 #define CP(src)						\
5444 	if ((src) == NULL)				\
5445 		*walker++ = '\0';			\
5446 	else {						\
5447 		strlcpy(walker, (src), ep - walker);	\
5448 		walker += strlen(walker) + 1;		\
5449 	}						\
5450 	if (walker >= ep)				\
5451 		break;
5452 
5453 	do {
5454 		CP(dev->nameunit);
5455 		CP(dev->desc);
5456 		CP(dev->driver != NULL ? dev->driver->name : NULL);
5457 		bus_child_pnpinfo_str(dev, walker, ep - walker);
5458 		walker += strlen(walker) + 1;
5459 		if (walker >= ep)
5460 			break;
5461 		bus_child_location_str(dev, walker, ep - walker);
5462 		walker += strlen(walker) + 1;
5463 		if (walker >= ep)
5464 			break;
5465 		*walker++ = '\0';
5466 	} while (0);
5467 #undef CP
5468 	error = SYSCTL_OUT(req, udev, sizeof(*udev));
5469 	free(udev, M_BUS);
5470 	return (error);
5471 }
5472 
5473 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5474     CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5475     "system device tree");
5476 
5477 int
5478 bus_data_generation_check(int generation)
5479 {
5480 	if (generation != bus_data_generation)
5481 		return (1);
5482 
5483 	/* XXX generate optimised lists here? */
5484 	return (0);
5485 }
5486 
5487 void
5488 bus_data_generation_update(void)
5489 {
5490 	atomic_add_int(&bus_data_generation, 1);
5491 }
5492 
5493 int
5494 bus_free_resource(device_t dev, int type, struct resource *r)
5495 {
5496 	if (r == NULL)
5497 		return (0);
5498 	return (bus_release_resource(dev, type, rman_get_rid(r), r));
5499 }
5500 
5501 device_t
5502 device_lookup_by_name(const char *name)
5503 {
5504 	device_t dev;
5505 
5506 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5507 		if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5508 			return (dev);
5509 	}
5510 	return (NULL);
5511 }
5512 
5513 /*
5514  * /dev/devctl2 implementation.  The existing /dev/devctl device has
5515  * implicit semantics on open, so it could not be reused for this.
5516  * Another option would be to call this /dev/bus?
5517  */
5518 static int
5519 find_device(struct devreq *req, device_t *devp)
5520 {
5521 	device_t dev;
5522 
5523 	/*
5524 	 * First, ensure that the name is nul terminated.
5525 	 */
5526 	if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5527 		return (EINVAL);
5528 
5529 	/*
5530 	 * Second, try to find an attached device whose name matches
5531 	 * 'name'.
5532 	 */
5533 	dev = device_lookup_by_name(req->dr_name);
5534 	if (dev != NULL) {
5535 		*devp = dev;
5536 		return (0);
5537 	}
5538 
5539 	/* Finally, give device enumerators a chance. */
5540 	dev = NULL;
5541 	EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5542 	if (dev == NULL)
5543 		return (ENOENT);
5544 	*devp = dev;
5545 	return (0);
5546 }
5547 
5548 static bool
5549 driver_exists(device_t bus, const char *driver)
5550 {
5551 	devclass_t dc;
5552 
5553 	for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5554 		if (devclass_find_driver_internal(dc, driver) != NULL)
5555 			return (true);
5556 	}
5557 	return (false);
5558 }
5559 
5560 static void
5561 device_gen_nomatch(device_t dev)
5562 {
5563 	device_t child;
5564 
5565 	if (dev->flags & DF_NEEDNOMATCH &&
5566 	    dev->state == DS_NOTPRESENT) {
5567 		BUS_PROBE_NOMATCH(dev->parent, dev);
5568 		devnomatch(dev);
5569 		dev->flags |= DF_DONENOMATCH;
5570 	}
5571 	dev->flags &= ~DF_NEEDNOMATCH;
5572 	TAILQ_FOREACH(child, &dev->children, link) {
5573 		device_gen_nomatch(child);
5574 	}
5575 }
5576 
5577 static void
5578 device_do_deferred_actions(void)
5579 {
5580 	devclass_t dc;
5581 	driverlink_t dl;
5582 
5583 	/*
5584 	 * Walk through the devclasses to find all the drivers we've tagged as
5585 	 * deferred during the freeze and call the driver added routines. They
5586 	 * have already been added to the lists in the background, so the driver
5587 	 * added routines that trigger a probe will have all the right bidders
5588 	 * for the probe auction.
5589 	 */
5590 	TAILQ_FOREACH(dc, &devclasses, link) {
5591 		TAILQ_FOREACH(dl, &dc->drivers, link) {
5592 			if (dl->flags & DL_DEFERRED_PROBE) {
5593 				devclass_driver_added(dc, dl->driver);
5594 				dl->flags &= ~DL_DEFERRED_PROBE;
5595 			}
5596 		}
5597 	}
5598 
5599 	/*
5600 	 * We also defer no-match events during a freeze. Walk the tree and
5601 	 * generate all the pent-up events that are still relevant.
5602 	 */
5603 	device_gen_nomatch(root_bus);
5604 	bus_data_generation_update();
5605 }
5606 
5607 static int
5608 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5609     struct thread *td)
5610 {
5611 	struct devreq *req;
5612 	device_t dev;
5613 	int error, old;
5614 
5615 	/* Locate the device to control. */
5616 	mtx_lock(&Giant);
5617 	req = (struct devreq *)data;
5618 	switch (cmd) {
5619 	case DEV_ATTACH:
5620 	case DEV_DETACH:
5621 	case DEV_ENABLE:
5622 	case DEV_DISABLE:
5623 	case DEV_SUSPEND:
5624 	case DEV_RESUME:
5625 	case DEV_SET_DRIVER:
5626 	case DEV_CLEAR_DRIVER:
5627 	case DEV_RESCAN:
5628 	case DEV_DELETE:
5629 	case DEV_RESET:
5630 		error = priv_check(td, PRIV_DRIVER);
5631 		if (error == 0)
5632 			error = find_device(req, &dev);
5633 		break;
5634 	case DEV_FREEZE:
5635 	case DEV_THAW:
5636 		error = priv_check(td, PRIV_DRIVER);
5637 		break;
5638 	default:
5639 		error = ENOTTY;
5640 		break;
5641 	}
5642 	if (error) {
5643 		mtx_unlock(&Giant);
5644 		return (error);
5645 	}
5646 
5647 	/* Perform the requested operation. */
5648 	switch (cmd) {
5649 	case DEV_ATTACH:
5650 		if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5651 			error = EBUSY;
5652 		else if (!device_is_enabled(dev))
5653 			error = ENXIO;
5654 		else
5655 			error = device_probe_and_attach(dev);
5656 		break;
5657 	case DEV_DETACH:
5658 		if (!device_is_attached(dev)) {
5659 			error = ENXIO;
5660 			break;
5661 		}
5662 		if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5663 			error = device_quiesce(dev);
5664 			if (error)
5665 				break;
5666 		}
5667 		error = device_detach(dev);
5668 		break;
5669 	case DEV_ENABLE:
5670 		if (device_is_enabled(dev)) {
5671 			error = EBUSY;
5672 			break;
5673 		}
5674 
5675 		/*
5676 		 * If the device has been probed but not attached (e.g.
5677 		 * when it has been disabled by a loader hint), just
5678 		 * attach the device rather than doing a full probe.
5679 		 */
5680 		device_enable(dev);
5681 		if (device_is_alive(dev)) {
5682 			/*
5683 			 * If the device was disabled via a hint, clear
5684 			 * the hint.
5685 			 */
5686 			if (resource_disabled(dev->driver->name, dev->unit))
5687 				resource_unset_value(dev->driver->name,
5688 				    dev->unit, "disabled");
5689 			error = device_attach(dev);
5690 		} else
5691 			error = device_probe_and_attach(dev);
5692 		break;
5693 	case DEV_DISABLE:
5694 		if (!device_is_enabled(dev)) {
5695 			error = ENXIO;
5696 			break;
5697 		}
5698 
5699 		if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5700 			error = device_quiesce(dev);
5701 			if (error)
5702 				break;
5703 		}
5704 
5705 		/*
5706 		 * Force DF_FIXEDCLASS on around detach to preserve
5707 		 * the existing name.
5708 		 */
5709 		old = dev->flags;
5710 		dev->flags |= DF_FIXEDCLASS;
5711 		error = device_detach(dev);
5712 		if (!(old & DF_FIXEDCLASS))
5713 			dev->flags &= ~DF_FIXEDCLASS;
5714 		if (error == 0)
5715 			device_disable(dev);
5716 		break;
5717 	case DEV_SUSPEND:
5718 		if (device_is_suspended(dev)) {
5719 			error = EBUSY;
5720 			break;
5721 		}
5722 		if (device_get_parent(dev) == NULL) {
5723 			error = EINVAL;
5724 			break;
5725 		}
5726 		error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5727 		break;
5728 	case DEV_RESUME:
5729 		if (!device_is_suspended(dev)) {
5730 			error = EINVAL;
5731 			break;
5732 		}
5733 		if (device_get_parent(dev) == NULL) {
5734 			error = EINVAL;
5735 			break;
5736 		}
5737 		error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5738 		break;
5739 	case DEV_SET_DRIVER: {
5740 		devclass_t dc;
5741 		char driver[128];
5742 
5743 		error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5744 		if (error)
5745 			break;
5746 		if (driver[0] == '\0') {
5747 			error = EINVAL;
5748 			break;
5749 		}
5750 		if (dev->devclass != NULL &&
5751 		    strcmp(driver, dev->devclass->name) == 0)
5752 			/* XXX: Could possibly force DF_FIXEDCLASS on? */
5753 			break;
5754 
5755 		/*
5756 		 * Scan drivers for this device's bus looking for at
5757 		 * least one matching driver.
5758 		 */
5759 		if (dev->parent == NULL) {
5760 			error = EINVAL;
5761 			break;
5762 		}
5763 		if (!driver_exists(dev->parent, driver)) {
5764 			error = ENOENT;
5765 			break;
5766 		}
5767 		dc = devclass_create(driver);
5768 		if (dc == NULL) {
5769 			error = ENOMEM;
5770 			break;
5771 		}
5772 
5773 		/* Detach device if necessary. */
5774 		if (device_is_attached(dev)) {
5775 			if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5776 				error = device_detach(dev);
5777 			else
5778 				error = EBUSY;
5779 			if (error)
5780 				break;
5781 		}
5782 
5783 		/* Clear any previously-fixed device class and unit. */
5784 		if (dev->flags & DF_FIXEDCLASS)
5785 			devclass_delete_device(dev->devclass, dev);
5786 		dev->flags |= DF_WILDCARD;
5787 		dev->unit = -1;
5788 
5789 		/* Force the new device class. */
5790 		error = devclass_add_device(dc, dev);
5791 		if (error)
5792 			break;
5793 		dev->flags |= DF_FIXEDCLASS;
5794 		error = device_probe_and_attach(dev);
5795 		break;
5796 	}
5797 	case DEV_CLEAR_DRIVER:
5798 		if (!(dev->flags & DF_FIXEDCLASS)) {
5799 			error = 0;
5800 			break;
5801 		}
5802 		if (device_is_attached(dev)) {
5803 			if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5804 				error = device_detach(dev);
5805 			else
5806 				error = EBUSY;
5807 			if (error)
5808 				break;
5809 		}
5810 
5811 		dev->flags &= ~DF_FIXEDCLASS;
5812 		dev->flags |= DF_WILDCARD;
5813 		devclass_delete_device(dev->devclass, dev);
5814 		error = device_probe_and_attach(dev);
5815 		break;
5816 	case DEV_RESCAN:
5817 		if (!device_is_attached(dev)) {
5818 			error = ENXIO;
5819 			break;
5820 		}
5821 		error = BUS_RESCAN(dev);
5822 		break;
5823 	case DEV_DELETE: {
5824 		device_t parent;
5825 
5826 		parent = device_get_parent(dev);
5827 		if (parent == NULL) {
5828 			error = EINVAL;
5829 			break;
5830 		}
5831 		if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5832 			if (bus_child_present(dev) != 0) {
5833 				error = EBUSY;
5834 				break;
5835 			}
5836 		}
5837 
5838 		error = device_delete_child(parent, dev);
5839 		break;
5840 	}
5841 	case DEV_FREEZE:
5842 		if (device_frozen)
5843 			error = EBUSY;
5844 		else
5845 			device_frozen = true;
5846 		break;
5847 	case DEV_THAW:
5848 		if (!device_frozen)
5849 			error = EBUSY;
5850 		else {
5851 			device_do_deferred_actions();
5852 			device_frozen = false;
5853 		}
5854 		break;
5855 	case DEV_RESET:
5856 		if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5857 			error = EINVAL;
5858 			break;
5859 		}
5860 		error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5861 		    req->dr_flags);
5862 		break;
5863 	}
5864 	mtx_unlock(&Giant);
5865 	return (error);
5866 }
5867 
5868 static struct cdevsw devctl2_cdevsw = {
5869 	.d_version =	D_VERSION,
5870 	.d_ioctl =	devctl2_ioctl,
5871 	.d_name =	"devctl2",
5872 };
5873 
5874 static void
5875 devctl2_init(void)
5876 {
5877 	make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5878 	    UID_ROOT, GID_WHEEL, 0600, "devctl2");
5879 }
5880 
5881 /*
5882  * APIs to manage deprecation and obsolescence.
5883  */
5884 static int obsolete_panic = 0;
5885 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5886     "Panic when obsolete features are used (0 = never, 1 = if osbolete, "
5887     "2 = if deprecated)");
5888 
5889 static void
5890 gone_panic(int major, int running, const char *msg)
5891 {
5892 	switch (obsolete_panic)
5893 	{
5894 	case 0:
5895 		return;
5896 	case 1:
5897 		if (running < major)
5898 			return;
5899 		/* FALLTHROUGH */
5900 	default:
5901 		panic("%s", msg);
5902 	}
5903 }
5904 
5905 void
5906 _gone_in(int major, const char *msg)
5907 {
5908 	gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5909 	if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5910 		printf("Obsolete code will be removed soon: %s\n", msg);
5911 	else
5912 		printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
5913 		    major, msg);
5914 }
5915 
5916 void
5917 _gone_in_dev(device_t dev, int major, const char *msg)
5918 {
5919 	gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5920 	if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5921 		device_printf(dev,
5922 		    "Obsolete code will be removed soon: %s\n", msg);
5923 	else
5924 		device_printf(dev,
5925 		    "Deprecated code (to be removed in FreeBSD %d): %s\n",
5926 		    major, msg);
5927 }
5928 
5929 #ifdef DDB
5930 DB_SHOW_COMMAND(device, db_show_device)
5931 {
5932 	device_t dev;
5933 
5934 	if (!have_addr)
5935 		return;
5936 
5937 	dev = (device_t)addr;
5938 
5939 	db_printf("name:    %s\n", device_get_nameunit(dev));
5940 	db_printf("  driver:  %s\n", DRIVERNAME(dev->driver));
5941 	db_printf("  class:   %s\n", DEVCLANAME(dev->devclass));
5942 	db_printf("  addr:    %p\n", dev);
5943 	db_printf("  parent:  %p\n", dev->parent);
5944 	db_printf("  softc:   %p\n", dev->softc);
5945 	db_printf("  ivars:   %p\n", dev->ivars);
5946 }
5947 
5948 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
5949 {
5950 	device_t dev;
5951 
5952 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5953 		db_show_device((db_expr_t)dev, true, count, modif);
5954 	}
5955 }
5956 #endif
5957