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