2 * The PCI Library -- Configuration Access via /sys/bus/pci
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * Copyright (c) 1997--2008 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>
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);
112 warn("Error reading %s: %s", namebuf, strerror(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)
417 clear_fill(d, PCI_FILL_PARENT);
421 if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
424 sysfs_fill_slots(d->access);
425 for (pd = d->access->devices; pd; pd = pd->next)
426 pd->known_fields |= PCI_FILL_PHYS_SLOT;
429 if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
431 char buf[OBJBUFSIZE];
432 if (sysfs_get_string(d, "modalias", buf, 0))
433 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
436 if (want_fill(d, flags, PCI_FILL_LABEL))
438 char buf[OBJBUFSIZE];
439 if (sysfs_get_string(d, "label", buf, 0))
440 d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
443 if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
444 d->numa_node = sysfs_get_value(d, "numa_node", 0);
446 if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
448 char *group_link = sysfs_deref_link(d, "iommu_group");
451 pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
456 if (want_fill(d, flags, PCI_FILL_DT_NODE))
458 char *node = sysfs_deref_link(d, "of_node");
461 pci_set_property(d, PCI_FILL_DT_NODE, node);
466 if (want_fill(d, flags, PCI_FILL_DRIVER))
468 char *driver_path = sysfs_deref_link(d, "driver");
471 char *driver = strrchr(driver_path, '/');
472 driver = driver ? driver+1 : driver_path;
473 pci_set_property(d, PCI_FILL_DRIVER, driver);
477 clear_fill(d, PCI_FILL_DRIVER);
480 pci_generic_fill_info(d, flags);
483 /* Intent of the sysfs_setup() caller */
486 SETUP_READ_CONFIG = 0,
487 SETUP_WRITE_CONFIG = 1,
492 sysfs_setup(struct pci_dev *d, int intent)
494 struct pci_access *a = d->access;
495 char namebuf[OBJNAMELEN];
497 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
499 sysfs_flush_cache(a);
503 if (intent == SETUP_READ_VPD)
507 sysfs_obj_name(d, "vpd", namebuf);
508 a->fd_vpd = open(namebuf, O_RDONLY);
509 /* No warning on error; vpd may be absent or accessible only to root */
516 sysfs_obj_name(d, "config", namebuf);
517 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
518 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
520 a->warning("Cannot open %s", namebuf);
526 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
528 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
533 res = do_read(d, fd, buf, len, pos);
536 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
544 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
546 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
551 res = do_write(d, fd, buf, len, pos);
554 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
559 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
565 #ifdef PCI_HAVE_DO_READ
567 /* pread() is not available and do_read() only works for a single fd, so we
568 * cannot implement read_vpd properly. */
569 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
574 #else /* !PCI_HAVE_DO_READ */
576 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
578 int fd = sysfs_setup(d, SETUP_READ_VPD);
583 res = pread(fd, buf, len, pos);
586 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
594 #endif /* PCI_HAVE_DO_READ */
596 static void sysfs_cleanup_dev(struct pci_dev *d)
598 struct pci_access *a = d->access;
600 if (a->cached_dev == d)
601 sysfs_flush_cache(a);
604 struct pci_methods pm_linux_sysfs = {
606 "The sys filesystem on Linux",