2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * An interface to the FreeBSD kernel's bus/device information interface.
36 * This interface is implemented with the
42 * sysctls. The interface is not meant for general user application
45 * Device information is obtained by scanning a linear list of all devices
46 * maintained by the kernel. The actual device structure pointers are
47 * handed out as opaque handles in order to allow reconstruction of the
48 * logical toplogy in user space.
50 * Resource information is obtained by scanning the kernel's resource
51 * managers and fetching their contents. Ownership of resources is
52 * tracked using the device's structure pointer again as a handle.
54 * In order to ensure coherency of the library's picture of the kernel,
55 * a generation count is maintained by the kernel. The initial generation
56 * count is obtained (along with the interface version) from the hw.bus
57 * sysctl, and must be passed in with every request. If the generation
58 * number supplied by the library does not match the kernel's current
59 * generation number, the request is failed and the library must discard
60 * the data it has received and rescan.
62 * The information obtained from the kernel is exported to consumers of
63 * this library through a variety of interfaces.
66 #include <sys/param.h>
67 #include <sys/types.h>
68 #include <sys/sysctl.h>
75 #include "devinfo_var.h"
77 static int devinfo_init_devices(int generation);
78 static int devinfo_init_resources(int generation);
80 TAILQ_HEAD(,devinfo_i_dev) devinfo_dev;
81 TAILQ_HEAD(,devinfo_i_rman) devinfo_rman;
82 TAILQ_HEAD(,devinfo_i_res) devinfo_res;
84 static int devinfo_initted = 0;
85 static int devinfo_generation = 0;
88 # define debug(...) do { \
89 fprintf(stderr, "%s:", __func__); \
90 fprintf(stderr, __VA_ARGS__); \
91 fprintf(stderr, "\n"); \
98 * Initialise our local database with the contents of the kernel's
104 struct u_businfo ubus;
108 if (!devinfo_initted) {
109 TAILQ_INIT(&devinfo_dev);
110 TAILQ_INIT(&devinfo_rman);
111 TAILQ_INIT(&devinfo_res);
115 * Get the generation count and interface version, verify that we
116 * are compatible with the kernel.
118 for (retries = 0; retries < 10; retries++) {
119 debug("get interface version");
120 ub_size = sizeof(ubus);
121 if (sysctlbyname("hw.bus.info", &ubus,
122 &ub_size, NULL, 0) != 0) {
123 warn("sysctlbyname(\"hw.bus.info\", ...) failed");
126 if ((ub_size != sizeof(ubus)) ||
127 (ubus.ub_version != BUS_USER_VERSION)) {
128 warnx("kernel bus interface version mismatch: kernel %d expected %d",
129 ubus.ub_version, BUS_USER_VERSION);
132 debug("generation count is %d", ubus.ub_generation);
135 * Don't rescan if the generation count hasn't changed.
137 if (ubus.ub_generation == devinfo_generation)
141 * Generation count changed, rescan
145 devinfo_generation = 0;
147 if ((error = devinfo_init_devices(ubus.ub_generation)) != 0) {
153 if ((error = devinfo_init_resources(ubus.ub_generation)) != 0) {
160 devinfo_generation = ubus.ub_generation;
163 debug("scan failed after %d retries", retries);
169 devinfo_init_devices(int generation)
171 struct u_device udev;
172 struct devinfo_i_dev *dd;
176 int oid[CTL_MAXNAME + 12];
182 * Find the OID for the rman interface node.
183 * This is just the usual evil, undocumented sysctl juju.
187 oidlen = sizeof(oid);
188 name = "hw.bus.devices";
189 error = sysctl(name2oid, 2, oid, &oidlen, name, strlen(name));
191 warnx("can't find hw.bus.devices sysctl node");
194 oidlen /= sizeof(int);
195 if (oidlen > CTL_MAXNAME) {
196 warnx("hw.bus.devices oid is too large");
199 oid[oidlen++] = generation;
205 * Stop after a fairly insane number to avoid death in the case
206 * of kernel corruption.
208 for (dev_idx = 0; dev_idx < 10000; dev_idx++) {
211 * Get the device information.
213 oid[dev_ptr] = dev_idx;
215 error = sysctl(oid, oidlen, &udev, &rlen, NULL, 0);
217 if (errno == ENOENT) /* end of list */
219 if (errno != EINVAL) /* gen count skip, restart */
220 warn("sysctl hw.bus.devices.%d", dev_idx);
223 if (rlen != sizeof(udev)) {
224 warnx("sysctl returned wrong data %zd bytes instead of %zd",
228 if ((dd = malloc(sizeof(*dd))) == NULL)
230 dd->dd_dev.dd_handle = udev.dv_handle;
231 dd->dd_dev.dd_parent = udev.dv_parent;
232 snprintf(dd->dd_name, sizeof(dd->dd_name), "%s", udev.dv_name);
233 dd->dd_dev.dd_name = &dd->dd_name[0];
234 snprintf(dd->dd_desc, sizeof(dd->dd_desc), "%s", udev.dv_desc);
235 dd->dd_dev.dd_desc = &dd->dd_desc[0];
236 snprintf(dd->dd_drivername, sizeof(dd->dd_drivername), "%s",
238 dd->dd_dev.dd_drivername = &dd->dd_drivername[0];
239 snprintf(dd->dd_pnpinfo, sizeof(dd->dd_pnpinfo), "%s",
241 dd->dd_dev.dd_pnpinfo = &dd->dd_pnpinfo[0];
242 snprintf(dd->dd_location, sizeof(dd->dd_location), "%s",
244 dd->dd_dev.dd_location = &dd->dd_location[0];
245 dd->dd_dev.dd_devflags = udev.dv_devflags;
246 dd->dd_dev.dd_flags = udev.dv_flags;
247 dd->dd_dev.dd_state = udev.dv_state;
248 TAILQ_INSERT_TAIL(&devinfo_dev, dd, dd_link);
250 debug("fetched %d devices", dev_idx);
255 devinfo_init_resources(int generation)
258 struct devinfo_i_rman *dm;
259 struct u_resource ures;
260 struct devinfo_i_res *dr;
261 int rman_idx, res_idx;
262 int rman_ptr, res_ptr;
264 int oid[CTL_MAXNAME + 12];
270 * Find the OID for the rman interface node.
271 * This is just the usual evil, undocumented sysctl juju.
275 oidlen = sizeof(oid);
276 name = "hw.bus.rman";
277 error = sysctl(name2oid, 2, oid, &oidlen, name, strlen(name));
279 warnx("can't find hw.bus.rman sysctl node");
282 oidlen /= sizeof(int);
283 if (oidlen > CTL_MAXNAME) {
284 warnx("hw.bus.rman oid is too large");
287 oid[oidlen++] = generation;
292 * Scan resource managers.
294 * Stop after a fairly insane number to avoid death in the case
295 * of kernel corruption.
297 for (rman_idx = 0; rman_idx < 255; rman_idx++) {
300 * Get the resource manager information.
302 oid[rman_ptr] = rman_idx;
304 rlen = sizeof(urman);
305 error = sysctl(oid, oidlen, &urman, &rlen, NULL, 0);
307 if (errno == ENOENT) /* end of list */
309 if (errno != EINVAL) /* gen count skip, restart */
310 warn("sysctl hw.bus.rman.%d", rman_idx);
313 if ((dm = malloc(sizeof(*dm))) == NULL)
315 dm->dm_rman.dm_handle = urman.rm_handle;
316 dm->dm_rman.dm_start = urman.rm_start;
317 dm->dm_rman.dm_size = urman.rm_size;
318 snprintf(dm->dm_desc, DEVINFO_STRLEN, "%s", urman.rm_descr);
319 dm->dm_rman.dm_desc = &dm->dm_desc[0];
320 TAILQ_INSERT_TAIL(&devinfo_rman, dm, dm_link);
323 * Scan resources on this resource manager.
325 * Stop after a fairly insane number to avoid death in the case
326 * of kernel corruption.
328 for (res_idx = 0; res_idx < 1000; res_idx++) {
330 * Get the resource information.
332 oid[res_ptr] = res_idx;
334 error = sysctl(oid, oidlen, &ures, &rlen, NULL, 0);
336 if (errno == ENOENT) /* end of list */
338 if (errno != EINVAL) /* gen count skip */
339 warn("sysctl hw.bus.rman.%d.%d",
343 if ((dr = malloc(sizeof(*dr))) == NULL)
345 dr->dr_res.dr_handle = ures.r_handle;
346 dr->dr_res.dr_rman = ures.r_parent;
347 dr->dr_res.dr_device = ures.r_device;
348 dr->dr_res.dr_start = ures.r_start;
349 dr->dr_res.dr_size = ures.r_size;
350 TAILQ_INSERT_TAIL(&devinfo_res, dr, dr_link);
352 debug("fetched %d resources", res_idx);
354 debug("scanned %d resource managers", rman_idx);
359 * Free the list contents.
364 struct devinfo_i_dev *dd;
365 struct devinfo_i_rman *dm;
366 struct devinfo_i_res *dr;
368 while ((dd = TAILQ_FIRST(&devinfo_dev)) != NULL) {
369 TAILQ_REMOVE(&devinfo_dev, dd, dd_link);
372 while ((dm = TAILQ_FIRST(&devinfo_rman)) != NULL) {
373 TAILQ_REMOVE(&devinfo_rman, dm, dm_link);
376 while ((dr = TAILQ_FIRST(&devinfo_res)) != NULL) {
377 TAILQ_REMOVE(&devinfo_res, dr, dr_link);
381 devinfo_generation = 0;
385 * Find a device by its handle.
388 devinfo_handle_to_device(devinfo_handle_t handle)
390 struct devinfo_i_dev *dd;
393 * Find the root device, whose parent is NULL
395 if (handle == DEVINFO_ROOT_DEVICE) {
396 TAILQ_FOREACH(dd, &devinfo_dev, dd_link)
397 if (dd->dd_dev.dd_parent == DEVINFO_ROOT_DEVICE)
403 * Scan for the device
405 TAILQ_FOREACH(dd, &devinfo_dev, dd_link)
406 if (dd->dd_dev.dd_handle == handle)
412 * Find a resource by its handle.
415 devinfo_handle_to_resource(devinfo_handle_t handle)
417 struct devinfo_i_res *dr;
419 TAILQ_FOREACH(dr, &devinfo_res, dr_link)
420 if (dr->dr_res.dr_handle == handle)
426 * Find a resource manager by its handle.
428 struct devinfo_rman *
429 devinfo_handle_to_rman(devinfo_handle_t handle)
431 struct devinfo_i_rman *dm;
433 TAILQ_FOREACH(dm, &devinfo_rman, dm_link)
434 if (dm->dm_rman.dm_handle == handle)
435 return(&dm->dm_rman);
440 * Iterate over the children of a device, calling (fn) on each. If
441 * (fn) returns nonzero, abort the scan and return.
444 devinfo_foreach_device_child(struct devinfo_dev *parent,
445 int (* fn)(struct devinfo_dev *child, void *arg),
448 struct devinfo_i_dev *dd;
451 TAILQ_FOREACH(dd, &devinfo_dev, dd_link)
452 if (dd->dd_dev.dd_parent == parent->dd_handle)
453 if ((error = fn(&dd->dd_dev, arg)) != 0)
459 * Iterate over all the resources owned by a device, calling (fn) on each.
460 * If (fn) returns nonzero, abort the scan and return.
463 devinfo_foreach_device_resource(struct devinfo_dev *dev,
464 int (* fn)(struct devinfo_dev *dev, struct devinfo_res *res, void *arg),
467 struct devinfo_i_res *dr;
470 TAILQ_FOREACH(dr, &devinfo_res, dr_link)
471 if (dr->dr_res.dr_device == dev->dd_handle)
472 if ((error = fn(dev, &dr->dr_res, arg)) != 0)
478 * Iterate over all the resources owned by a resource manager, calling (fn)
479 * on each. If (fn) returns nonzero, abort the scan and return.
482 devinfo_foreach_rman_resource(struct devinfo_rman *rman,
483 int (* fn)(struct devinfo_res *res, void *arg),
486 struct devinfo_i_res *dr;
489 TAILQ_FOREACH(dr, &devinfo_res, dr_link)
490 if (dr->dr_res.dr_rman == rman->dm_handle)
491 if ((error = fn(&dr->dr_res, arg)) != 0)
497 * Iterate over all the resource managers, calling (fn) on each. If (fn)
498 * returns nonzero, abort the scan and return.
501 devinfo_foreach_rman(int (* fn)(struct devinfo_rman *rman, void *arg),
504 struct devinfo_i_rman *dm;
507 TAILQ_FOREACH(dm, &devinfo_rman, dm_link)
508 if ((error = fn(&dm->dm_rman, arg)) != 0)