2 * The PCI Library -- Configuration Access via /sys/bus/pci
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * Copyright (c) 1997--2023 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
22 #include <sys/types.h>
27 sysfs_config(struct pci_access *a)
29 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
33 sysfs_name(struct pci_access *a)
35 return pci_get_param(a, "sysfs.path");
39 sysfs_detect(struct pci_access *a)
41 if (access(sysfs_name(a), R_OK))
43 a->debug("...cannot open %s", sysfs_name(a));
46 a->debug("...using %s", sysfs_name(a));
51 sysfs_init(struct pci_access *a)
58 sysfs_flush_cache(struct pci_access *a)
74 sysfs_cleanup(struct pci_access *a)
79 #define OBJNAMELEN 1024
81 sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
83 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s",
84 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object);
85 if (n < 0 || n >= OBJNAMELEN)
86 d->access->error("File name too long");
89 #define OBJBUFSIZE 1024
92 sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
94 struct pci_access *a = d->access;
96 char namebuf[OBJNAMELEN];
97 void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning);
99 sysfs_obj_name(d, object, namebuf);
100 fd = open(namebuf, O_RDONLY);
103 if (mandatory || errno != ENOENT)
104 warn("Cannot open %s: %s", namebuf, strerror(errno));
107 n = read(fd, buf, OBJBUFSIZE);
111 warn("Error reading %s: %s", namebuf, strerror(errno));
116 warn("Value in %s too long", namebuf);
124 sysfs_deref_link(struct pci_dev *d, char *link_name)
126 char path[2*OBJNAMELEN], rel_path[OBJNAMELEN];
128 sysfs_obj_name(d, link_name, path);
129 memset(rel_path, 0, sizeof(rel_path));
131 if (readlink(path, rel_path, sizeof(rel_path)) < 0)
134 sysfs_obj_name(d, "", path);
135 strcat(path, rel_path);
137 // Returns a pointer to malloc'ed memory
138 return realpath(path, NULL);
142 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
144 char buf[OBJBUFSIZE];
146 if (sysfs_get_string(d, object, buf, mandatory))
147 return strtol(buf, NULL, 0);
153 sysfs_get_resources(struct pci_dev *d)
155 struct pci_access *a = d->access;
156 char namebuf[OBJNAMELEN], buf[256];
157 struct { pciaddr_t flags, base_addr, size; } lines[10];
158 int have_bar_bases, have_rom_base, have_bridge_bases;
162 have_bar_bases = have_rom_base = have_bridge_bases = 0;
163 sysfs_obj_name(d, "resource", namebuf);
164 file = fopen(namebuf, "r");
166 a->error("Cannot open %s: %s", namebuf, strerror(errno));
167 for (i = 0; i < 7+6+4+1; i++)
169 unsigned long long start, end, size, flags;
170 if (!fgets(buf, sizeof(buf), file))
172 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
173 a->error("Syntax error in %s", namebuf);
175 size = end - start + 1;
181 flags &= PCI_ADDR_FLAG_MASK;
182 d->base_addr[i] = start | flags;
188 d->rom_flags = flags;
189 flags &= PCI_ADDR_FLAG_MASK;
190 d->rom_base_addr = start | flags;
197 * If kernel was compiled without CONFIG_PCI_IOV option then after
198 * the ROM line for configured bridge device (that which had set
199 * subordinary bus number to non-zero value) are four additional lines
200 * which describe resources behind bridge. For PCI-to-PCI bridges they
201 * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
202 * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
203 * there is no additional line after the ROM line. If kernel was
204 * compiled with CONFIG_PCI_IOV option then after the ROM line and
205 * before the first bridge resource line are six additional lines
206 * which describe IOV resources. Read all remaining lines in resource
207 * file and based on the number of remaining lines (0, 4, 6, 10) parse
208 * resources behind bridge.
210 lines[i-7].flags = flags;
211 lines[i-7].base_addr = start;
212 lines[i-7].size = size;
215 if (i == 7+4 || i == 7+6+4)
217 int offset = (i == 7+6+4) ? 6 : 0;
218 for (i = 0; i < 4; i++)
220 d->bridge_flags[i] = lines[offset+i].flags;
221 d->bridge_base_addr[i] = lines[offset+i].base_addr;
222 d->bridge_size[i] = lines[offset+i].size;
224 have_bridge_bases = 1;
228 clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
230 clear_fill(d, PCI_FILL_ROM_BASE);
231 if (!have_bridge_bases)
232 clear_fill(d, PCI_FILL_BRIDGE_BASES);
235 static void sysfs_scan(struct pci_access *a)
239 struct dirent *entry;
242 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
243 if (n < 0 || n >= (int) sizeof(dirname))
244 a->error("Directory name too long");
245 dir = opendir(dirname);
247 a->error("Cannot open %s", dirname);
248 while ((entry = readdir(dir)))
251 unsigned int dom, bus, dev, func;
253 /* ".", ".." or a special non-device perhaps */
254 if (entry->d_name[0] == '.')
257 d = pci_alloc_dev(a);
258 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
259 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
261 /* Ensure kernel provided domain that fits in a signed integer */
262 if (dom > 0x7fffffff)
263 a->error("sysfs_scan: Invalid domain %x", dom);
275 sysfs_fill_slots(struct pci_access *a)
279 struct dirent *entry;
282 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
283 if (n < 0 || n >= (int) sizeof(dirname))
284 a->error("Directory name too long");
285 dir = opendir(dirname);
289 while (entry = readdir(dir))
291 char namebuf[OBJNAMELEN], buf[16];
293 unsigned int dom, bus, dev;
297 /* ".", ".." or a special non-device perhaps */
298 if (entry->d_name[0] == '.')
301 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
302 if (n < 0 || n >= OBJNAMELEN)
303 a->error("File name too long");
304 file = fopen(namebuf, "r");
306 * Old versions of Linux had a fakephp which didn't have an 'address'
307 * file. There's no useful information to be gleaned from these
308 * devices, pretend they're not there.
313 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
316 * In some cases, the slot is not tied to a specific device before
317 * a card gets inserted. This happens for example on IBM pSeries
318 * and we need not warn about it.
321 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
325 for (d = a->devices; d; d = d->next)
326 if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
327 d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name);
335 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
337 int value, want_class, want_class_ext;
339 if (!d->access->buscentric)
342 * These fields can be read from the config registers, but we want to show
343 * the kernel's view, which has regions and IRQs remapped and other fields
344 * (most importantly classes) possibly fixed if the device is known broken.
346 if (want_fill(d, flags, PCI_FILL_IDENT))
348 d->vendor_id = sysfs_get_value(d, "vendor", 1);
349 d->device_id = sysfs_get_value(d, "device", 1);
351 want_class = want_fill(d, flags, PCI_FILL_CLASS);
352 want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
353 if (want_class || want_class_ext)
355 value = sysfs_get_value(d, "class", 1);
357 d->device_class = value >> 8;
360 d->prog_if = value & 0xff;
361 value = sysfs_get_value(d, "revision", 0);
363 value = pci_read_byte(d, PCI_REVISION_ID);
368 if (want_fill(d, flags, PCI_FILL_SUBSYS))
370 value = sysfs_get_value(d, "subsystem_vendor", 0);
373 d->subsys_vendor_id = value;
374 value = sysfs_get_value(d, "subsystem_device", 0);
376 d->subsys_id = value;
379 clear_fill(d, PCI_FILL_SUBSYS);
381 if (want_fill(d, flags, PCI_FILL_IRQ))
382 d->irq = sysfs_get_value(d, "irq", 1);
383 if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
384 sysfs_get_resources(d);
385 if (want_fill(d, flags, PCI_FILL_PARENT))
387 unsigned int domain, bus, dev, func;
388 char *path_abs, *path_canon, *name;
389 char path_rel[OBJNAMELEN];
390 struct pci_dev *parent;
392 /* Construct sysfs path for parent device */
393 sysfs_obj_name(d, "..", path_rel);
394 path_abs = realpath(path_rel, NULL);
395 name = path_abs ? strrchr(path_abs, '/') : NULL;
396 name = name ? name+1 : name;
399 if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
400 for (parent = d->access->devices; parent; parent = parent->next)
401 if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
406 /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
407 sysfs_obj_name(parent, ".", path_rel);
408 path_canon = realpath(path_rel, NULL);
409 if (!path_canon || strcmp(path_canon, path_abs) != 0)
419 clear_fill(d, PCI_FILL_PARENT);
426 if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
429 sysfs_fill_slots(d->access);
430 for (pd = d->access->devices; pd; pd = pd->next)
431 pd->known_fields |= PCI_FILL_PHYS_SLOT;
434 if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
436 char buf[OBJBUFSIZE];
437 if (sysfs_get_string(d, "modalias", buf, 0))
438 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
441 if (want_fill(d, flags, PCI_FILL_LABEL))
443 char buf[OBJBUFSIZE];
444 if (sysfs_get_string(d, "label", buf, 0))
445 d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
448 if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
449 d->numa_node = sysfs_get_value(d, "numa_node", 0);
451 if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
453 char *group_link = sysfs_deref_link(d, "iommu_group");
456 pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
461 if (want_fill(d, flags, PCI_FILL_DT_NODE))
463 char *node = sysfs_deref_link(d, "of_node");
466 pci_set_property(d, PCI_FILL_DT_NODE, node);
471 if (want_fill(d, flags, PCI_FILL_DRIVER))
473 char *driver_path = sysfs_deref_link(d, "driver");
476 char *driver = strrchr(driver_path, '/');
477 driver = driver ? driver+1 : driver_path;
478 pci_set_property(d, PCI_FILL_DRIVER, driver);
482 clear_fill(d, PCI_FILL_DRIVER);
485 pci_generic_fill_info(d, flags);
488 /* Intent of the sysfs_setup() caller */
491 SETUP_READ_CONFIG = 0,
492 SETUP_WRITE_CONFIG = 1,
497 sysfs_setup(struct pci_dev *d, int intent)
499 struct pci_access *a = d->access;
500 char namebuf[OBJNAMELEN];
502 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
504 sysfs_flush_cache(a);
508 if (intent == SETUP_READ_VPD)
512 sysfs_obj_name(d, "vpd", namebuf);
513 a->fd_vpd = open(namebuf, O_RDONLY);
514 /* No warning on error; vpd may be absent or accessible only to root */
521 sysfs_obj_name(d, "config", namebuf);
522 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
523 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
525 a->warning("Cannot open %s", namebuf);
530 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
532 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
537 res = pread(fd, buf, len, pos);
540 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
548 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
550 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
555 res = pwrite(fd, buf, len, pos);
558 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
563 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
569 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
571 int fd = sysfs_setup(d, SETUP_READ_VPD);
576 res = pread(fd, buf, len, pos);
579 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
587 static void sysfs_cleanup_dev(struct pci_dev *d)
589 struct pci_access *a = d->access;
591 if (a->cached_dev == d)
592 sysfs_flush_cache(a);
595 struct pci_methods pm_linux_sysfs = {
597 "The sys filesystem on Linux",