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