d->access->error("File name too long");
}
+#define OBJBUFSIZE 1024
+
static int
-sysfs_get_value(struct pci_dev *d, char *object)
+sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
{
struct pci_access *a = d->access;
int fd, n;
- char namebuf[OBJNAMELEN], buf[256];
+ char namebuf[OBJNAMELEN];
+ void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning);
sysfs_obj_name(d, object, namebuf);
fd = open(namebuf, O_RDONLY);
if (fd < 0)
- a->error("Cannot open %s: %s", namebuf, strerror(errno));
- n = read(fd, buf, sizeof(buf));
+ {
+ if (mandatory || errno != ENOENT)
+ warn("Cannot open %s: %s", namebuf, strerror(errno));
+ return 0;
+ }
+ n = read(fd, buf, OBJBUFSIZE);
close(fd);
if (n < 0)
- a->error("Error reading %s: %s", namebuf, strerror(errno));
- if (n >= (int) sizeof(buf))
- a->error("Value in %s too long", namebuf);
+ {
+ warn("Error reading %s: %s", namebuf, strerror(errno));
+ return 0;
+ }
+ if (n >= OBJBUFSIZE)
+ {
+ warn("Value in %s too long", namebuf);
+ return 0;
+ }
buf[n] = 0;
- return strtol(buf, NULL, 0);
+ return 1;
+}
+
+static int
+sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
+{
+ char buf[OBJBUFSIZE];
+
+ if (sysfs_get_string(d, object, buf, mandatory))
+ return strtol(buf, NULL, 0);
+ else
+ return -1;
}
static void
break;
if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
a->error("Syntax error in %s", namebuf);
- if (start)
+ if (end > start)
size = end - start + 1;
else
size = 0;
- flags &= PCI_ADDR_FLAG_MASK;
if (i < 6)
{
+ d->flags[i] = flags;
+ flags &= PCI_ADDR_FLAG_MASK;
d->base_addr[i] = start | flags;
d->size[i] = size;
}
else
{
+ d->rom_flags = flags;
+ flags &= PCI_ADDR_FLAG_MASK;
d->rom_base_addr = start | flags;
d->rom_size = size;
}
d = pci_alloc_dev(a);
if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
+
+ /* Ensure kernel provided domain that fits in a signed integer */
+ if (dom > 0x7fffffff)
+ a->error("sysfs_scan: Invalid domain %x", dom);
+
d->domain = dom;
d->bus = bus;
d->dev = dev;
if (!a->buscentric)
{
sysfs_get_resources(d);
- d->irq = sysfs_get_value(d, "irq");
+ d->irq = sysfs_get_value(d, "irq", 1);
/*
* We could read these faster from the config registers, but we want to give
* the kernel a chance to fix up ID's and especially classes of broken devices.
*/
- d->vendor_id = sysfs_get_value(d, "vendor");
- d->device_id = sysfs_get_value(d, "device");
- d->device_class = sysfs_get_value(d, "class") >> 8;
- d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
+ d->vendor_id = sysfs_get_value(d, "vendor", 1);
+ d->device_id = sysfs_get_value(d, "device", 1);
+ d->device_class = sysfs_get_value(d, "class", 1) >> 8;
+ d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS;
}
pci_link_dev(a, d);
}
char namebuf[OBJNAMELEN], buf[16];
FILE *file;
unsigned int dom, bus, dev;
+ int res = 0;
struct pci_dev *d;
/* ".", ".." or a special non-device perhaps */
if (n < 0 || n >= OBJNAMELEN)
a->error("File name too long");
file = fopen(namebuf, "r");
+ /*
+ * Old versions of Linux had a fakephp which didn't have an 'address'
+ * file. There's no useful information to be gleaned from these
+ * devices, pretend they're not there.
+ */
if (!file)
+ continue;
+
+ if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
{
- a->warning("sysfs_fill_slots: Cannot open %s: %s", namebuf, strerror(errno));
- continue;
+ /*
+ * In some cases, the slot is not tied to a specific device before
+ * a card gets inserted. This happens for example on IBM pSeries
+ * and we need not warn about it.
+ */
+ if (res != 2)
+ a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
}
-
- if (!fgets(buf, sizeof(buf), file) || sscanf(buf, "%x:%x:%x", &dom, &bus, &dev) < 3)
- a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
else
{
for (d = a->devices; d; d = d->next)
- if (dom == d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
- {
- d->phy_slot = pci_malloc(a, strlen(entry->d_name) + 1);
- strcpy(d->phy_slot, entry->d_name);
- break;
- }
+ if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
+ d->phy_slot = pci_strdup(a, entry->d_name);
}
fclose(file);
}
for (pd = d->access->devices; pd; pd = pd->next)
pd->known_fields |= PCI_FILL_PHYS_SLOT;
}
+
+ if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
+ {
+ char buf[OBJBUFSIZE];
+ if (sysfs_get_string(d, "modalias", buf, 0))
+ d->module_alias = pci_strdup(d->access, buf);
+ }
+
+ if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
+ {
+ char buf[OBJBUFSIZE];
+ if (sysfs_get_string(d, "label", buf, 0))
+ d->label = pci_strdup(d->access, buf);
+ }
+
+ if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE))
+ d->numa_node = sysfs_get_value(d, "numa_node", 0);
+
return pci_generic_fill_info(d, flags);
}