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];
97 sysfs_obj_name(d, object, namebuf);
98 fd = open(namebuf, O_RDONLY);
102 a->error("Cannot open %s: %s", namebuf, strerror(errno));
105 n = read(fd, buf, OBJBUFSIZE);
108 a->error("Error reading %s: %s", namebuf, strerror(errno));
110 a->error("Value in %s too long", namebuf);
116 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
118 char buf[OBJBUFSIZE];
120 if (sysfs_get_string(d, object, buf, mandatory))
121 return strtol(buf, NULL, 0);
127 sysfs_get_resources(struct pci_dev *d)
129 struct pci_access *a = d->access;
130 char namebuf[OBJNAMELEN], buf[256];
134 sysfs_obj_name(d, "resource", namebuf);
135 file = fopen(namebuf, "r");
137 a->error("Cannot open %s: %s", namebuf, strerror(errno));
138 for (i = 0; i < 7; i++)
140 unsigned long long start, end, size, flags;
141 if (!fgets(buf, sizeof(buf), file))
143 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
144 a->error("Syntax error in %s", namebuf);
146 size = end - start + 1;
149 flags &= PCI_ADDR_FLAG_MASK;
152 d->base_addr[i] = start | flags;
157 d->rom_base_addr = start | flags;
164 static void sysfs_scan(struct pci_access *a)
168 struct dirent *entry;
171 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
172 if (n < 0 || n >= (int) sizeof(dirname))
173 a->error("Directory name too long");
174 dir = opendir(dirname);
176 a->error("Cannot open %s", dirname);
177 while ((entry = readdir(dir)))
180 unsigned int dom, bus, dev, func;
182 /* ".", ".." or a special non-device perhaps */
183 if (entry->d_name[0] == '.')
186 d = pci_alloc_dev(a);
187 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
188 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
195 sysfs_get_resources(d);
196 d->irq = sysfs_get_value(d, "irq", 1);
198 * We could read these faster from the config registers, but we want to give
199 * the kernel a chance to fix up ID's and especially classes of broken devices.
201 d->vendor_id = sysfs_get_value(d, "vendor", 1);
202 d->device_id = sysfs_get_value(d, "device", 1);
203 d->device_class = sysfs_get_value(d, "class", 1) >> 8;
204 d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
212 sysfs_fill_slots(struct pci_access *a)
216 struct dirent *entry;
219 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
220 if (n < 0 || n >= (int) sizeof(dirname))
221 a->error("Directory name too long");
222 dir = opendir(dirname);
226 while (entry = readdir(dir))
228 char namebuf[OBJNAMELEN], buf[16];
230 unsigned int dom, bus, dev;
234 /* ".", ".." or a special non-device perhaps */
235 if (entry->d_name[0] == '.')
238 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
239 if (n < 0 || n >= OBJNAMELEN)
240 a->error("File name too long");
241 file = fopen(namebuf, "r");
243 * Old versions of Linux had a fakephp which didn't have an 'address'
244 * file. There's no useful information to be gleaned from these
245 * devices, pretend they're not there.
250 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
253 * In some cases, the slot is not tied to a specific device before
254 * a card gets inserted. This happens for example on IBM pSeries
255 * and we need not warn about it.
258 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
262 for (d = a->devices; d; d = d->next)
263 if (dom == d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
264 d->phy_slot = pci_strdup(a, entry->d_name);
272 sysfs_fill_info(struct pci_dev *d, int flags)
274 if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
277 sysfs_fill_slots(d->access);
278 for (pd = d->access->devices; pd; pd = pd->next)
279 pd->known_fields |= PCI_FILL_PHYS_SLOT;
282 if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
284 char buf[OBJBUFSIZE];
285 if (sysfs_get_string(d, "modalias", buf, 0))
286 d->module_alias = pci_strdup(d->access, buf);
289 if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
291 char buf[OBJBUFSIZE];
292 if (sysfs_get_string(d, "label", buf, 0))
293 d->label = pci_strdup(d->access, buf);
296 if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE))
297 d->numa_node = sysfs_get_value(d, "numa_node", 0);
299 return pci_generic_fill_info(d, flags);
302 /* Intent of the sysfs_setup() caller */
305 SETUP_READ_CONFIG = 0,
306 SETUP_WRITE_CONFIG = 1,
311 sysfs_setup(struct pci_dev *d, int intent)
313 struct pci_access *a = d->access;
314 char namebuf[OBJNAMELEN];
316 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
318 sysfs_flush_cache(a);
322 if (intent == SETUP_READ_VPD)
326 sysfs_obj_name(d, "vpd", namebuf);
327 a->fd_vpd = open(namebuf, O_RDONLY);
328 /* No warning on error; vpd may be absent or accessible only to root */
335 sysfs_obj_name(d, "config", namebuf);
336 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
337 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
339 a->warning("Cannot open %s", namebuf);
345 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
347 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
352 res = do_read(d, fd, buf, len, pos);
355 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
363 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
365 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
370 res = do_write(d, fd, buf, len, pos);
373 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
378 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
384 #ifdef PCI_HAVE_DO_READ
386 /* pread() is not available and do_read() only works for a single fd, so we
387 * cannot implement read_vpd properly. */
388 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
393 #else /* !PCI_HAVE_DO_READ */
395 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
397 int fd = sysfs_setup(d, SETUP_READ_VPD);
402 res = pread(fd, buf, len, pos);
405 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
413 #endif /* PCI_HAVE_DO_READ */
415 static void sysfs_cleanup_dev(struct pci_dev *d)
417 struct pci_access *a = d->access;
419 if (a->cached_dev == d)
420 sysfs_flush_cache(a);
423 struct pci_methods pm_linux_sysfs = {
425 "The sys filesystem on Linux",