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