2 * The PCI Library -- Configuration Access via /sys/bus/pci
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * Copyright (c) 1997--2024 Martin Mares <mj@ucw.cz>
7 * Can be freely distributed and used under the terms of the GNU GPL v2+.
9 * SPDX-License-Identifier: GPL-2.0-or-later
23 #include <sys/types.h>
28 sysfs_config(struct pci_access *a)
30 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
34 sysfs_name(struct pci_access *a)
36 return pci_get_param(a, "sysfs.path");
40 sysfs_detect(struct pci_access *a)
42 if (access(sysfs_name(a), R_OK))
44 a->debug("...cannot open %s", sysfs_name(a));
47 a->debug("...using %s", sysfs_name(a));
52 sysfs_init(struct pci_access *a)
59 sysfs_flush_cache(struct pci_access *a)
75 sysfs_cleanup(struct pci_access *a)
80 #define OBJNAMELEN 1024
82 sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
84 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s",
85 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object);
86 if (n < 0 || n >= OBJNAMELEN)
87 d->access->error("File name too long");
90 #define OBJBUFSIZE 1024
93 sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
95 struct pci_access *a = d->access;
97 char namebuf[OBJNAMELEN];
98 void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning);
100 sysfs_obj_name(d, object, namebuf);
101 fd = open(namebuf, O_RDONLY);
104 if (mandatory || errno != ENOENT)
105 warn("Cannot open %s: %s", namebuf, strerror(errno));
108 n = read(fd, buf, OBJBUFSIZE);
109 int read_errno = errno;
113 warn("Error reading %s: %s", namebuf, strerror(read_errno));
118 warn("Value in %s too long", namebuf);
126 sysfs_deref_link(struct pci_dev *d, char *link_name)
128 char path[2*OBJNAMELEN], rel_path[OBJNAMELEN];
130 sysfs_obj_name(d, link_name, path);
131 memset(rel_path, 0, sizeof(rel_path));
133 if (readlink(path, rel_path, sizeof(rel_path)) < 0)
136 sysfs_obj_name(d, "", path);
137 strcat(path, rel_path);
139 // Returns a pointer to malloc'ed memory
140 return realpath(path, NULL);
144 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
146 char buf[OBJBUFSIZE];
148 if (sysfs_get_string(d, object, buf, mandatory))
149 return strtol(buf, NULL, 0);
155 sysfs_get_resources(struct pci_dev *d)
157 struct pci_access *a = d->access;
158 char namebuf[OBJNAMELEN], buf[256];
159 struct { pciaddr_t flags, base_addr, size; } lines[10];
160 int have_bar_bases, have_rom_base, have_bridge_bases;
164 have_bar_bases = have_rom_base = have_bridge_bases = 0;
165 sysfs_obj_name(d, "resource", namebuf);
166 file = fopen(namebuf, "r");
168 a->error("Cannot open %s: %s", namebuf, strerror(errno));
169 for (i = 0; i < 7+6+4+1; i++)
171 unsigned long long start, end, size, flags;
172 if (!fgets(buf, sizeof(buf), file))
174 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
175 a->error("Syntax error in %s", namebuf);
177 size = end - start + 1;
183 flags &= PCI_ADDR_FLAG_MASK;
184 d->base_addr[i] = start | flags;
190 d->rom_flags = flags;
191 flags &= PCI_ADDR_FLAG_MASK;
192 d->rom_base_addr = start | flags;
199 * If kernel was compiled without CONFIG_PCI_IOV option then after
200 * the ROM line for configured bridge device (that which had set
201 * subordinary bus number to non-zero value) are four additional lines
202 * which describe resources behind bridge. For PCI-to-PCI bridges they
203 * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
204 * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
205 * there is no additional line after the ROM line. If kernel was
206 * compiled with CONFIG_PCI_IOV option then after the ROM line and
207 * before the first bridge resource line are six additional lines
208 * which describe IOV resources. Read all remaining lines in resource
209 * file and based on the number of remaining lines (0, 4, 6, 10) parse
210 * resources behind bridge.
212 lines[i-7].flags = flags;
213 lines[i-7].base_addr = start;
214 lines[i-7].size = size;
217 if (i == 7+4 || i == 7+6+4)
219 int offset = (i == 7+6+4) ? 6 : 0;
220 for (i = 0; i < 4; i++)
222 d->bridge_flags[i] = lines[offset+i].flags;
223 d->bridge_base_addr[i] = lines[offset+i].base_addr;
224 d->bridge_size[i] = lines[offset+i].size;
226 have_bridge_bases = 1;
230 clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
232 clear_fill(d, PCI_FILL_ROM_BASE);
233 if (!have_bridge_bases)
234 clear_fill(d, PCI_FILL_BRIDGE_BASES);
237 static void sysfs_scan(struct pci_access *a)
241 struct dirent *entry;
244 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
245 if (n < 0 || n >= (int) sizeof(dirname))
246 a->error("Directory name too long");
247 dir = opendir(dirname);
249 a->error("Cannot open %s", dirname);
250 while ((entry = readdir(dir)))
253 unsigned int dom, bus, dev, func;
255 /* ".", ".." or a special non-device perhaps */
256 if (entry->d_name[0] == '.')
259 d = pci_alloc_dev(a);
260 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
261 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
263 /* Ensure kernel provided domain that fits in a signed integer */
264 if (dom > 0x7fffffff)
265 a->error("sysfs_scan: Invalid domain %x", dom);
277 sysfs_fill_slots(struct pci_access *a)
281 struct dirent *entry;
284 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
285 if (n < 0 || n >= (int) sizeof(dirname))
286 a->error("Directory name too long");
287 dir = opendir(dirname);
291 while (entry = readdir(dir))
293 char namebuf[OBJNAMELEN], buf[16];
295 unsigned int dom, bus, dev;
299 /* ".", ".." or a special non-device perhaps */
300 if (entry->d_name[0] == '.')
303 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
304 if (n < 0 || n >= OBJNAMELEN)
305 a->error("File name too long");
306 file = fopen(namebuf, "r");
308 * Old versions of Linux had a fakephp which didn't have an 'address'
309 * file. There's no useful information to be gleaned from these
310 * devices, pretend they're not there.
315 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
318 * In some cases, the slot is not tied to a specific device before
319 * a card gets inserted. This happens for example on IBM pSeries
320 * and we need not warn about it.
323 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
327 for (d = a->devices; d; d = d->next)
328 if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
329 d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name);
337 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
339 int value, want_class, want_class_ext;
341 if (!d->access->buscentric)
344 * These fields can be read from the config registers, but we want to show
345 * the kernel's view, which has regions and IRQs remapped and other fields
346 * (most importantly classes) possibly fixed if the device is known broken.
348 if (want_fill(d, flags, PCI_FILL_IDENT))
350 d->vendor_id = sysfs_get_value(d, "vendor", 1);
351 d->device_id = sysfs_get_value(d, "device", 1);
353 want_class = want_fill(d, flags, PCI_FILL_CLASS);
354 want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
355 if (want_class || want_class_ext)
357 value = sysfs_get_value(d, "class", 1);
359 d->device_class = value >> 8;
362 d->prog_if = value & 0xff;
363 value = sysfs_get_value(d, "revision", 0);
365 value = pci_read_byte(d, PCI_REVISION_ID);
370 if (want_fill(d, flags, PCI_FILL_SUBSYS))
372 value = sysfs_get_value(d, "subsystem_vendor", 0);
375 d->subsys_vendor_id = value;
376 value = sysfs_get_value(d, "subsystem_device", 0);
378 d->subsys_id = value;
381 clear_fill(d, PCI_FILL_SUBSYS);
383 if (want_fill(d, flags, PCI_FILL_IRQ))
384 d->irq = sysfs_get_value(d, "irq", 1);
385 if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
386 sysfs_get_resources(d);
387 if (want_fill(d, flags, PCI_FILL_PARENT))
389 unsigned int domain, bus, dev, func;
390 char *path_abs, *path_canon, *name;
391 char path_rel[OBJNAMELEN];
392 struct pci_dev *parent;
394 /* Construct sysfs path for parent device */
395 sysfs_obj_name(d, "..", path_rel);
396 path_abs = realpath(path_rel, NULL);
397 name = path_abs ? strrchr(path_abs, '/') : NULL;
398 name = name ? name+1 : name;
401 if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
402 for (parent = d->access->devices; parent; parent = parent->next)
403 if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
408 /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
409 sysfs_obj_name(parent, ".", path_rel);
410 path_canon = realpath(path_rel, NULL);
411 if (!path_canon || strcmp(path_canon, path_abs) != 0)
421 clear_fill(d, PCI_FILL_PARENT);
428 if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
431 sysfs_fill_slots(d->access);
432 for (pd = d->access->devices; pd; pd = pd->next)
433 pd->known_fields |= PCI_FILL_PHYS_SLOT;
436 if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
438 char buf[OBJBUFSIZE];
439 if (sysfs_get_string(d, "modalias", buf, 0))
440 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
443 if (want_fill(d, flags, PCI_FILL_LABEL))
445 char buf[OBJBUFSIZE];
446 if (sysfs_get_string(d, "label", buf, 0))
447 d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
450 if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
451 d->numa_node = sysfs_get_value(d, "numa_node", 0);
453 if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
455 char *group_link = sysfs_deref_link(d, "iommu_group");
458 pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
463 if (want_fill(d, flags, PCI_FILL_DT_NODE))
465 char *node = sysfs_deref_link(d, "of_node");
468 pci_set_property(d, PCI_FILL_DT_NODE, node);
473 if (want_fill(d, flags, PCI_FILL_DRIVER))
475 char *driver_path = sysfs_deref_link(d, "driver");
478 char *driver = strrchr(driver_path, '/');
479 driver = driver ? driver+1 : driver_path;
480 pci_set_property(d, PCI_FILL_DRIVER, driver);
484 clear_fill(d, PCI_FILL_DRIVER);
487 if (want_fill(d, flags, PCI_FILL_RCD_LNK))
489 char buf[OBJBUFSIZE];
490 if (sysfs_get_string(d, "rcd_link_cap", buf, 0))
491 d->rcd_link_cap = strtoul(buf, NULL, 16);
492 if (sysfs_get_string(d, "rcd_link_ctrl", buf, 0))
493 d->rcd_link_ctrl = strtoul(buf, NULL, 16);
494 if (sysfs_get_string(d, "rcd_link_status", buf, 0))
495 d->rcd_link_status = strtoul(buf, NULL, 16);
498 pci_generic_fill_info(d, flags);
501 /* Intent of the sysfs_setup() caller */
504 SETUP_READ_CONFIG = 0,
505 SETUP_WRITE_CONFIG = 1,
510 sysfs_setup(struct pci_dev *d, int intent)
512 struct pci_access *a = d->access;
513 char namebuf[OBJNAMELEN];
515 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
517 sysfs_flush_cache(a);
521 if (intent == SETUP_READ_VPD)
525 sysfs_obj_name(d, "vpd", namebuf);
526 a->fd_vpd = open(namebuf, O_RDONLY);
527 /* No warning on error; vpd may be absent or accessible only to root */
534 sysfs_obj_name(d, "config", namebuf);
535 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
536 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
538 a->warning("Cannot open %s", namebuf);
543 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
545 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
550 res = pread(fd, buf, len, pos);
553 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
561 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
563 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
568 res = pwrite(fd, buf, len, pos);
571 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
576 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
582 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
584 int fd = sysfs_setup(d, SETUP_READ_VPD);
589 res = pread(fd, buf, len, pos);
592 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
600 static void sysfs_cleanup_dev(struct pci_dev *d)
602 struct pci_access *a = d->access;
604 if (a->cached_dev == d)
605 sysfs_flush_cache(a);
608 struct pci_methods pm_linux_sysfs = {
609 .name = "linux-sysfs",
610 .help = "The sys filesystem on Linux",
611 .config = sysfs_config,
612 .detect = sysfs_detect,
614 .cleanup = sysfs_cleanup,
616 .fill_info = sysfs_fill_info,
618 .write = sysfs_write,
619 .read_vpd = sysfs_read_vpd,
620 .cleanup_dev = sysfs_cleanup_dev,