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);
108 int read_errno = errno;
112 warn("Error reading %s: %s", namebuf, strerror(read_errno));
117 warn("Value in %s too long", namebuf);
125 sysfs_deref_link(struct pci_dev *d, char *link_name)
127 char path[2*OBJNAMELEN], rel_path[OBJNAMELEN];
129 sysfs_obj_name(d, link_name, path);
130 memset(rel_path, 0, sizeof(rel_path));
132 if (readlink(path, rel_path, sizeof(rel_path)) < 0)
135 sysfs_obj_name(d, "", path);
136 strcat(path, rel_path);
138 // Returns a pointer to malloc'ed memory
139 return realpath(path, NULL);
143 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
145 char buf[OBJBUFSIZE];
147 if (sysfs_get_string(d, object, buf, mandatory))
148 return strtol(buf, NULL, 0);
154 sysfs_get_resources(struct pci_dev *d)
156 struct pci_access *a = d->access;
157 char namebuf[OBJNAMELEN], buf[256];
158 struct { pciaddr_t flags, base_addr, size; } lines[10];
159 int have_bar_bases, have_rom_base, have_bridge_bases;
163 have_bar_bases = have_rom_base = have_bridge_bases = 0;
164 sysfs_obj_name(d, "resource", namebuf);
165 file = fopen(namebuf, "r");
167 a->error("Cannot open %s: %s", namebuf, strerror(errno));
168 for (i = 0; i < 7+6+4+1; i++)
170 unsigned long long start, end, size, flags;
171 if (!fgets(buf, sizeof(buf), file))
173 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
174 a->error("Syntax error in %s", namebuf);
176 size = end - start + 1;
182 flags &= PCI_ADDR_FLAG_MASK;
183 d->base_addr[i] = start | flags;
189 d->rom_flags = flags;
190 flags &= PCI_ADDR_FLAG_MASK;
191 d->rom_base_addr = start | flags;
198 * If kernel was compiled without CONFIG_PCI_IOV option then after
199 * the ROM line for configured bridge device (that which had set
200 * subordinary bus number to non-zero value) are four additional lines
201 * which describe resources behind bridge. For PCI-to-PCI bridges they
202 * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
203 * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
204 * there is no additional line after the ROM line. If kernel was
205 * compiled with CONFIG_PCI_IOV option then after the ROM line and
206 * before the first bridge resource line are six additional lines
207 * which describe IOV resources. Read all remaining lines in resource
208 * file and based on the number of remaining lines (0, 4, 6, 10) parse
209 * resources behind bridge.
211 lines[i-7].flags = flags;
212 lines[i-7].base_addr = start;
213 lines[i-7].size = size;
216 if (i == 7+4 || i == 7+6+4)
218 int offset = (i == 7+6+4) ? 6 : 0;
219 for (i = 0; i < 4; i++)
221 d->bridge_flags[i] = lines[offset+i].flags;
222 d->bridge_base_addr[i] = lines[offset+i].base_addr;
223 d->bridge_size[i] = lines[offset+i].size;
225 have_bridge_bases = 1;
229 clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
231 clear_fill(d, PCI_FILL_ROM_BASE);
232 if (!have_bridge_bases)
233 clear_fill(d, PCI_FILL_BRIDGE_BASES);
236 static void sysfs_scan(struct pci_access *a)
240 struct dirent *entry;
243 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
244 if (n < 0 || n >= (int) sizeof(dirname))
245 a->error("Directory name too long");
246 dir = opendir(dirname);
248 a->error("Cannot open %s", dirname);
249 while ((entry = readdir(dir)))
252 unsigned int dom, bus, dev, func;
254 /* ".", ".." or a special non-device perhaps */
255 if (entry->d_name[0] == '.')
258 d = pci_alloc_dev(a);
259 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
260 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
262 /* Ensure kernel provided domain that fits in a signed integer */
263 if (dom > 0x7fffffff)
264 a->error("sysfs_scan: Invalid domain %x", dom);
276 sysfs_fill_slots(struct pci_access *a)
280 struct dirent *entry;
283 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
284 if (n < 0 || n >= (int) sizeof(dirname))
285 a->error("Directory name too long");
286 dir = opendir(dirname);
290 while (entry = readdir(dir))
292 char namebuf[OBJNAMELEN], buf[16];
294 unsigned int dom, bus, dev;
298 /* ".", ".." or a special non-device perhaps */
299 if (entry->d_name[0] == '.')
302 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
303 if (n < 0 || n >= OBJNAMELEN)
304 a->error("File name too long");
305 file = fopen(namebuf, "r");
307 * Old versions of Linux had a fakephp which didn't have an 'address'
308 * file. There's no useful information to be gleaned from these
309 * devices, pretend they're not there.
314 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
317 * In some cases, the slot is not tied to a specific device before
318 * a card gets inserted. This happens for example on IBM pSeries
319 * and we need not warn about it.
322 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
326 for (d = a->devices; d; d = d->next)
327 if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
328 d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name);
336 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
338 int value, want_class, want_class_ext;
340 if (!d->access->buscentric)
343 * These fields can be read from the config registers, but we want to show
344 * the kernel's view, which has regions and IRQs remapped and other fields
345 * (most importantly classes) possibly fixed if the device is known broken.
347 if (want_fill(d, flags, PCI_FILL_IDENT))
349 d->vendor_id = sysfs_get_value(d, "vendor", 1);
350 d->device_id = sysfs_get_value(d, "device", 1);
352 want_class = want_fill(d, flags, PCI_FILL_CLASS);
353 want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
354 if (want_class || want_class_ext)
356 value = sysfs_get_value(d, "class", 1);
358 d->device_class = value >> 8;
361 d->prog_if = value & 0xff;
362 value = sysfs_get_value(d, "revision", 0);
364 value = pci_read_byte(d, PCI_REVISION_ID);
369 if (want_fill(d, flags, PCI_FILL_SUBSYS))
371 value = sysfs_get_value(d, "subsystem_vendor", 0);
374 d->subsys_vendor_id = value;
375 value = sysfs_get_value(d, "subsystem_device", 0);
377 d->subsys_id = value;
380 clear_fill(d, PCI_FILL_SUBSYS);
382 if (want_fill(d, flags, PCI_FILL_IRQ))
383 d->irq = sysfs_get_value(d, "irq", 1);
384 if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
385 sysfs_get_resources(d);
386 if (want_fill(d, flags, PCI_FILL_PARENT))
388 unsigned int domain, bus, dev, func;
389 char *path_abs, *path_canon, *name;
390 char path_rel[OBJNAMELEN];
391 struct pci_dev *parent;
393 /* Construct sysfs path for parent device */
394 sysfs_obj_name(d, "..", path_rel);
395 path_abs = realpath(path_rel, NULL);
396 name = path_abs ? strrchr(path_abs, '/') : NULL;
397 name = name ? name+1 : name;
400 if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
401 for (parent = d->access->devices; parent; parent = parent->next)
402 if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
407 /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
408 sysfs_obj_name(parent, ".", path_rel);
409 path_canon = realpath(path_rel, NULL);
410 if (!path_canon || strcmp(path_canon, path_abs) != 0)
420 clear_fill(d, PCI_FILL_PARENT);
427 if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
430 sysfs_fill_slots(d->access);
431 for (pd = d->access->devices; pd; pd = pd->next)
432 pd->known_fields |= PCI_FILL_PHYS_SLOT;
435 if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
437 char buf[OBJBUFSIZE];
438 if (sysfs_get_string(d, "modalias", buf, 0))
439 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
442 if (want_fill(d, flags, PCI_FILL_LABEL))
444 char buf[OBJBUFSIZE];
445 if (sysfs_get_string(d, "label", buf, 0))
446 d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
449 if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
450 d->numa_node = sysfs_get_value(d, "numa_node", 0);
452 if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
454 char *group_link = sysfs_deref_link(d, "iommu_group");
457 pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
462 if (want_fill(d, flags, PCI_FILL_DT_NODE))
464 char *node = sysfs_deref_link(d, "of_node");
467 pci_set_property(d, PCI_FILL_DT_NODE, node);
472 if (want_fill(d, flags, PCI_FILL_DRIVER))
474 char *driver_path = sysfs_deref_link(d, "driver");
477 char *driver = strrchr(driver_path, '/');
478 driver = driver ? driver+1 : driver_path;
479 pci_set_property(d, PCI_FILL_DRIVER, driver);
483 clear_fill(d, PCI_FILL_DRIVER);
486 pci_generic_fill_info(d, flags);
489 /* Intent of the sysfs_setup() caller */
492 SETUP_READ_CONFIG = 0,
493 SETUP_WRITE_CONFIG = 1,
498 sysfs_setup(struct pci_dev *d, int intent)
500 struct pci_access *a = d->access;
501 char namebuf[OBJNAMELEN];
503 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
505 sysfs_flush_cache(a);
509 if (intent == SETUP_READ_VPD)
513 sysfs_obj_name(d, "vpd", namebuf);
514 a->fd_vpd = open(namebuf, O_RDONLY);
515 /* No warning on error; vpd may be absent or accessible only to root */
522 sysfs_obj_name(d, "config", namebuf);
523 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
524 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
526 a->warning("Cannot open %s", namebuf);
531 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
533 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
538 res = pread(fd, buf, len, pos);
541 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
549 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
551 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
556 res = pwrite(fd, buf, len, pos);
559 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
564 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
570 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
572 int fd = sysfs_setup(d, SETUP_READ_VPD);
577 res = pread(fd, buf, len, pos);
580 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
588 static void sysfs_cleanup_dev(struct pci_dev *d)
590 struct pci_access *a = d->access;
592 if (a->cached_dev == d)
593 sysfs_flush_cache(a);
596 struct pci_methods pm_linux_sysfs = {
597 .name = "linux-sysfs",
598 .help = "The sys filesystem on Linux",
599 .config = sysfs_config,
600 .detect = sysfs_detect,
602 .cleanup = sysfs_cleanup,
604 .fill_info = sysfs_fill_info,
606 .write = sysfs_write,
607 .read_vpd = sysfs_read_vpd,
608 .cleanup_dev = sysfs_cleanup_dev,