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