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.
20 #include <sys/types.h>
26 sysfs_config(struct pci_access *a)
28 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
32 sysfs_name(struct pci_access *a)
34 return pci_get_param(a, "sysfs.path");
38 sysfs_detect(struct pci_access *a)
40 if (access(sysfs_name(a), R_OK))
42 a->debug("...cannot open %s", sysfs_name(a));
45 a->debug("...using %s", sysfs_name(a));
50 sysfs_init(struct pci_access *a)
57 sysfs_flush_cache(struct pci_access *a)
73 sysfs_cleanup(struct pci_access *a)
78 #define OBJNAMELEN 1024
80 sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
82 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s",
83 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object);
84 if (n < 0 || n >= OBJNAMELEN)
85 d->access->error("File name too long");
88 #define OBJBUFSIZE 1024
91 sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
93 struct pci_access *a = d->access;
95 char namebuf[OBJNAMELEN];
96 void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning);
98 sysfs_obj_name(d, object, namebuf);
99 fd = open(namebuf, O_RDONLY);
102 if (mandatory || errno != ENOENT)
103 warn("Cannot open %s: %s", namebuf, strerror(errno));
106 n = read(fd, buf, OBJBUFSIZE);
110 warn("Error reading %s: %s", namebuf, strerror(errno));
115 warn("Value in %s too long", namebuf);
123 sysfs_deref_link(struct pci_dev *d, char *link_name)
125 char path[2*OBJNAMELEN], rel_path[OBJNAMELEN];
127 sysfs_obj_name(d, link_name, path);
128 memset(rel_path, 0, sizeof(rel_path));
130 if (readlink(path, rel_path, sizeof(rel_path)) < 0)
133 sysfs_obj_name(d, "", path);
134 strcat(path, rel_path);
136 // Returns a pointer to malloc'ed memory
137 return realpath(path, NULL);
141 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
143 char buf[OBJBUFSIZE];
145 if (sysfs_get_string(d, object, buf, mandatory))
146 return strtol(buf, NULL, 0);
152 sysfs_get_resources(struct pci_dev *d)
154 struct pci_access *a = d->access;
155 char namebuf[OBJNAMELEN], buf[256];
156 struct { pciaddr_t flags, base_addr, size; } lines[10];
157 int have_bar_bases, have_rom_base, have_bridge_bases;
161 have_bar_bases = have_rom_base = have_bridge_bases = 0;
162 sysfs_obj_name(d, "resource", namebuf);
163 file = fopen(namebuf, "r");
165 a->error("Cannot open %s: %s", namebuf, strerror(errno));
166 for (i = 0; i < 7+6+4+1; i++)
168 unsigned long long start, end, size, flags;
169 if (!fgets(buf, sizeof(buf), file))
171 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
172 a->error("Syntax error in %s", namebuf);
174 size = end - start + 1;
180 flags &= PCI_ADDR_FLAG_MASK;
181 d->base_addr[i] = start | flags;
187 d->rom_flags = flags;
188 flags &= PCI_ADDR_FLAG_MASK;
189 d->rom_base_addr = start | flags;
196 * If kernel was compiled without CONFIG_PCI_IOV option then after
197 * the ROM line for configured bridge device (that which had set
198 * subordinary bus number to non-zero value) are four additional lines
199 * which describe resources behind bridge. For PCI-to-PCI bridges they
200 * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
201 * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
202 * there is no additional line after the ROM line. If kernel was
203 * compiled with CONFIG_PCI_IOV option then after the ROM line and
204 * before the first bridge resource line are six additional lines
205 * which describe IOV resources. Read all remaining lines in resource
206 * file and based on the number of remaining lines (0, 4, 6, 10) parse
207 * resources behind bridge.
209 lines[i-7].flags = flags;
210 lines[i-7].base_addr = start;
211 lines[i-7].size = size;
214 if (i == 7+4 || i == 7+6+4)
216 int offset = (i == 7+6+4) ? 6 : 0;
217 for (i = 0; i < 4; i++)
219 d->bridge_flags[i] = lines[offset+i].flags;
220 d->bridge_base_addr[i] = lines[offset+i].base_addr;
221 d->bridge_size[i] = lines[offset+i].size;
223 have_bridge_bases = 1;
227 clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
229 clear_fill(d, PCI_FILL_ROM_BASE);
230 if (!have_bridge_bases)
231 clear_fill(d, PCI_FILL_BRIDGE_BASES);
234 static void sysfs_scan(struct pci_access *a)
238 struct dirent *entry;
241 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
242 if (n < 0 || n >= (int) sizeof(dirname))
243 a->error("Directory name too long");
244 dir = opendir(dirname);
246 a->error("Cannot open %s", dirname);
247 while ((entry = readdir(dir)))
250 unsigned int dom, bus, dev, func;
252 /* ".", ".." or a special non-device perhaps */
253 if (entry->d_name[0] == '.')
256 d = pci_alloc_dev(a);
257 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
258 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
260 /* Ensure kernel provided domain that fits in a signed integer */
261 if (dom > 0x7fffffff)
262 a->error("sysfs_scan: Invalid domain %x", dom);
274 sysfs_fill_slots(struct pci_access *a)
278 struct dirent *entry;
281 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
282 if (n < 0 || n >= (int) sizeof(dirname))
283 a->error("Directory name too long");
284 dir = opendir(dirname);
288 while (entry = readdir(dir))
290 char namebuf[OBJNAMELEN], buf[16];
292 unsigned int dom, bus, dev;
296 /* ".", ".." or a special non-device perhaps */
297 if (entry->d_name[0] == '.')
300 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
301 if (n < 0 || n >= OBJNAMELEN)
302 a->error("File name too long");
303 file = fopen(namebuf, "r");
305 * Old versions of Linux had a fakephp which didn't have an 'address'
306 * file. There's no useful information to be gleaned from these
307 * devices, pretend they're not there.
312 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
315 * In some cases, the slot is not tied to a specific device before
316 * a card gets inserted. This happens for example on IBM pSeries
317 * and we need not warn about it.
320 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
324 for (d = a->devices; d; d = d->next)
325 if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
326 d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name);
334 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
336 int value, want_class, want_class_ext;
338 if (!d->access->buscentric)
341 * These fields can be read from the config registers, but we want to show
342 * the kernel's view, which has regions and IRQs remapped and other fields
343 * (most importantly classes) possibly fixed if the device is known broken.
345 if (want_fill(d, flags, PCI_FILL_IDENT))
347 d->vendor_id = sysfs_get_value(d, "vendor", 1);
348 d->device_id = sysfs_get_value(d, "device", 1);
350 want_class = want_fill(d, flags, PCI_FILL_CLASS);
351 want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
352 if (want_class || want_class_ext)
354 value = sysfs_get_value(d, "class", 1);
356 d->device_class = value >> 8;
359 d->prog_if = value & 0xff;
360 value = sysfs_get_value(d, "revision", 0);
362 value = pci_read_byte(d, PCI_REVISION_ID);
367 if (want_fill(d, flags, PCI_FILL_SUBSYS))
369 value = sysfs_get_value(d, "subsystem_vendor", 0);
372 d->subsys_vendor_id = value;
373 value = sysfs_get_value(d, "subsystem_device", 0);
375 d->subsys_id = value;
378 clear_fill(d, PCI_FILL_SUBSYS);
380 if (want_fill(d, flags, PCI_FILL_IRQ))
381 d->irq = sysfs_get_value(d, "irq", 1);
382 if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
383 sysfs_get_resources(d);
384 if (want_fill(d, flags, PCI_FILL_PARENT))
386 unsigned int domain, bus, dev, func;
387 char *path_abs, *path_canon, *name;
388 char path_rel[OBJNAMELEN];
389 struct pci_dev *parent;
391 /* Construct sysfs path for parent device */
392 sysfs_obj_name(d, "..", path_rel);
393 path_abs = realpath(path_rel, NULL);
394 name = path_abs ? strrchr(path_abs, '/') : NULL;
395 name = name ? name+1 : name;
398 if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
399 for (parent = d->access->devices; parent; parent = parent->next)
400 if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
405 /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
406 sysfs_obj_name(parent, ".", path_rel);
407 path_canon = realpath(path_rel, NULL);
408 if (!path_canon || strcmp(path_canon, path_abs) != 0)
415 clear_fill(d, PCI_FILL_PARENT);
419 if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
422 sysfs_fill_slots(d->access);
423 for (pd = d->access->devices; pd; pd = pd->next)
424 pd->known_fields |= PCI_FILL_PHYS_SLOT;
427 if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
429 char buf[OBJBUFSIZE];
430 if (sysfs_get_string(d, "modalias", buf, 0))
431 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
434 if (want_fill(d, flags, PCI_FILL_LABEL))
436 char buf[OBJBUFSIZE];
437 if (sysfs_get_string(d, "label", buf, 0))
438 d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
441 if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
442 d->numa_node = sysfs_get_value(d, "numa_node", 0);
444 if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
446 char *group_link = sysfs_deref_link(d, "iommu_group");
449 pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
454 if (want_fill(d, flags, PCI_FILL_DT_NODE))
456 char *node = sysfs_deref_link(d, "of_node");
459 pci_set_property(d, PCI_FILL_DT_NODE, node);
464 pci_generic_fill_info(d, flags);
467 /* Intent of the sysfs_setup() caller */
470 SETUP_READ_CONFIG = 0,
471 SETUP_WRITE_CONFIG = 1,
476 sysfs_setup(struct pci_dev *d, int intent)
478 struct pci_access *a = d->access;
479 char namebuf[OBJNAMELEN];
481 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
483 sysfs_flush_cache(a);
487 if (intent == SETUP_READ_VPD)
491 sysfs_obj_name(d, "vpd", namebuf);
492 a->fd_vpd = open(namebuf, O_RDONLY);
493 /* No warning on error; vpd may be absent or accessible only to root */
500 sysfs_obj_name(d, "config", namebuf);
501 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
502 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
504 a->warning("Cannot open %s", namebuf);
510 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
512 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
517 res = do_read(d, fd, buf, len, pos);
520 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
528 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
530 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
535 res = do_write(d, fd, buf, len, pos);
538 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
543 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
549 #ifdef PCI_HAVE_DO_READ
551 /* pread() is not available and do_read() only works for a single fd, so we
552 * cannot implement read_vpd properly. */
553 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
558 #else /* !PCI_HAVE_DO_READ */
560 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
562 int fd = sysfs_setup(d, SETUP_READ_VPD);
567 res = pread(fd, buf, len, pos);
570 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
578 #endif /* PCI_HAVE_DO_READ */
580 static void sysfs_cleanup_dev(struct pci_dev *d)
582 struct pci_access *a = d->access;
584 if (a->cached_dev == d)
585 sysfs_flush_cache(a);
588 struct pci_methods pm_linux_sysfs = {
590 "The sys filesystem on Linux",