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