]> mj.ucw.cz Git - pciutils.git/blobdiff - lib/sysfs.c
Fix stripping in cross-compiling mode
[pciutils.git] / lib / sysfs.c
index fb6424105e84d927c03270546ee410979ed1ed60..ffa4f9d63d34916fe91945b0e83040049d95c564 100644 (file)
@@ -153,14 +153,17 @@ sysfs_get_resources(struct pci_dev *d)
 {
   struct pci_access *a = d->access;
   char namebuf[OBJNAMELEN], buf[256];
 {
   struct pci_access *a = d->access;
   char namebuf[OBJNAMELEN], buf[256];
+  struct { pciaddr_t flags, base_addr, size; } lines[10];
+  int have_bar_bases, have_rom_base, have_bridge_bases;
   FILE *file;
   int i;
 
   FILE *file;
   int i;
 
+  have_bar_bases = have_rom_base = have_bridge_bases = 0;
   sysfs_obj_name(d, "resource", namebuf);
   file = fopen(namebuf, "r");
   if (!file)
     a->error("Cannot open %s: %s", namebuf, strerror(errno));
   sysfs_obj_name(d, "resource", namebuf);
   file = fopen(namebuf, "r");
   if (!file)
     a->error("Cannot open %s: %s", namebuf, strerror(errno));
-  for (i = 0; i < 7; i++)
+  for (i = 0; i < 7+6+4+1; i++)
     {
       unsigned long long start, end, size, flags;
       if (!fgets(buf, sizeof(buf), file))
     {
       unsigned long long start, end, size, flags;
       if (!fgets(buf, sizeof(buf), file))
@@ -177,16 +180,55 @@ sysfs_get_resources(struct pci_dev *d)
          flags &= PCI_ADDR_FLAG_MASK;
          d->base_addr[i] = start | flags;
          d->size[i] = size;
          flags &= PCI_ADDR_FLAG_MASK;
          d->base_addr[i] = start | flags;
          d->size[i] = size;
+         have_bar_bases = 1;
        }
        }
-      else
+      else if (i == 6)
        {
          d->rom_flags = flags;
          flags &= PCI_ADDR_FLAG_MASK;
          d->rom_base_addr = start | flags;
          d->rom_size = size;
        {
          d->rom_flags = flags;
          flags &= PCI_ADDR_FLAG_MASK;
          d->rom_base_addr = start | flags;
          d->rom_size = size;
+         have_rom_base = 1;
        }
        }
+      else if (i < 7+6+4)
+        {
+          /*
+           * If kernel was compiled without CONFIG_PCI_IOV option then after
+           * the ROM line for configured bridge device (that which had set
+           * subordinary bus number to non-zero value) are four additional lines
+           * which describe resources behind bridge. For PCI-to-PCI bridges they
+           * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
+           * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
+           * there is no additional line after the ROM line. If kernel was
+           * compiled with CONFIG_PCI_IOV option then after the ROM line and
+           * before the first bridge resource line are six additional lines
+           * which describe IOV resources. Read all remaining lines in resource
+           * file and based on the number of remaining lines (0, 4, 6, 10) parse
+           * resources behind bridge.
+           */
+          lines[i-7].flags = flags;
+          lines[i-7].base_addr = start;
+          lines[i-7].size = size;
+        }
+    }
+  if (i == 7+4 || i == 7+6+4)
+    {
+      int offset = (i == 7+6+4) ? 6 : 0;
+      for (i = 0; i < 4; i++)
+        {
+          d->bridge_flags[i] = lines[offset+i].flags;
+          d->bridge_base_addr[i] = lines[offset+i].base_addr;
+          d->bridge_size[i] = lines[offset+i].size;
+        }
+      have_bridge_bases = 1;
     }
   fclose(file);
     }
   fclose(file);
+  if (!have_bar_bases)
+    clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
+  if (!have_rom_base)
+    clear_fill(d, PCI_FILL_ROM_BASE);
+  if (!have_bridge_bases)
+    clear_fill(d, PCI_FILL_BRIDGE_BASES);
 }
 
 static void sysfs_scan(struct pci_access *a)
 }
 
 static void sysfs_scan(struct pci_access *a)
@@ -288,10 +330,10 @@ sysfs_fill_slots(struct pci_access *a)
   closedir(dir);
 }
 
   closedir(dir);
 }
 
-static unsigned int
+static void
 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
 {
 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
 {
-  unsigned int done = 0;
+  int value, want_class, want_class_ext;
 
   if (!d->access->buscentric)
     {
 
   if (!d->access->buscentric)
     {
@@ -300,61 +342,106 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags)
        *  the kernel's view, which has regions and IRQs remapped and other fields
        *  (most importantly classes) possibly fixed if the device is known broken.
        */
        *  the kernel's view, which has regions and IRQs remapped and other fields
        *  (most importantly classes) possibly fixed if the device is known broken.
        */
-      if (flags & PCI_FILL_IDENT)
+      if (want_fill(d, flags, PCI_FILL_IDENT))
        {
          d->vendor_id = sysfs_get_value(d, "vendor", 1);
          d->device_id = sysfs_get_value(d, "device", 1);
        {
          d->vendor_id = sysfs_get_value(d, "vendor", 1);
          d->device_id = sysfs_get_value(d, "device", 1);
-         done |= PCI_FILL_IDENT;
        }
        }
-      if (flags & PCI_FILL_CLASS)
-       {
-         d->device_class = sysfs_get_value(d, "class", 1) >> 8;
-         done |= PCI_FILL_CLASS;
+      want_class = want_fill(d, flags, PCI_FILL_CLASS);
+      want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
+      if (want_class || want_class_ext)
+        {
+         value = sysfs_get_value(d, "class", 1);
+         if (want_class)
+           d->device_class = value >> 8;
+         if (want_class_ext)
+           {
+             d->prog_if = value & 0xff;
+             value = sysfs_get_value(d, "revision", 0);
+             if (value < 0)
+               value = pci_read_byte(d, PCI_REVISION_ID);
+             if (value >= 0)
+               d->rev_id = value;
+           }
        }
        }
-      if (flags & PCI_FILL_IRQ)
+      if (want_fill(d, flags, PCI_FILL_SUBSYS))
        {
        {
-         d->irq = sysfs_get_value(d, "irq", 1);
-         done |= PCI_FILL_IRQ;
+         value = sysfs_get_value(d, "subsystem_vendor", 0);
+         if (value >= 0)
+           {
+             d->subsys_vendor_id = value;
+             value = sysfs_get_value(d, "subsystem_device", 0);
+             if (value >= 0)
+               d->subsys_id = value;
+           }
+         else
+           clear_fill(d, PCI_FILL_SUBSYS);
        }
        }
-      if (flags & (PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS))
-       {
+      if (want_fill(d, flags, PCI_FILL_IRQ))
+         d->irq = sysfs_get_value(d, "irq", 1);
+      if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
          sysfs_get_resources(d);
          sysfs_get_resources(d);
-         done |= PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS;
+      if (want_fill(d, flags, PCI_FILL_PARENT))
+       {
+         unsigned int domain, bus, dev, func;
+         char *path_abs, *path_canon, *name;
+         char path_rel[OBJNAMELEN];
+         struct pci_dev *parent;
+
+         /* Construct sysfs path for parent device */
+         sysfs_obj_name(d, "..", path_rel);
+         path_abs = realpath(path_rel, NULL);
+         name = path_abs ? strrchr(path_abs, '/') : NULL;
+         name = name ? name+1 : name;
+         parent = NULL;
+
+         if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
+           for (parent = d->access->devices; parent; parent = parent->next)
+             if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
+               break;
+
+         if (parent)
+           {
+             /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
+             sysfs_obj_name(parent, ".", path_rel);
+             path_canon = realpath(path_rel, NULL);
+             if (!path_canon || strcmp(path_canon, path_abs) != 0)
+               parent = NULL;
+           }
+
+         if (parent)
+           d->parent = parent;
+         else
+           clear_fill(d, PCI_FILL_PARENT);
        }
     }
 
        }
     }
 
-  if (flags & PCI_FILL_PHYS_SLOT)
+  if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
     {
       struct pci_dev *pd;
       sysfs_fill_slots(d->access);
       for (pd = d->access->devices; pd; pd = pd->next)
        pd->known_fields |= PCI_FILL_PHYS_SLOT;
     {
       struct pci_dev *pd;
       sysfs_fill_slots(d->access);
       for (pd = d->access->devices; pd; pd = pd->next)
        pd->known_fields |= PCI_FILL_PHYS_SLOT;
-      done |= PCI_FILL_PHYS_SLOT;
     }
 
     }
 
-  if (flags & PCI_FILL_MODULE_ALIAS)
+  if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "modalias", buf, 0))
        d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "modalias", buf, 0))
        d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
-      done |= PCI_FILL_MODULE_ALIAS;
     }
 
     }
 
-  if (flags & PCI_FILL_LABEL)
+  if (want_fill(d, flags, PCI_FILL_LABEL))
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "label", buf, 0))
        d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "label", buf, 0))
        d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
-      done |= PCI_FILL_LABEL;
     }
 
     }
 
-  if (flags & PCI_FILL_NUMA_NODE)
-    {
-      d->numa_node = sysfs_get_value(d, "numa_node", 0);
-      done |= PCI_FILL_NUMA_NODE;
-    }
+  if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
+    d->numa_node = sysfs_get_value(d, "numa_node", 0);
 
 
-  if (flags & PCI_FILL_IOMMU_GROUP)
+  if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
     {
       char *group_link = sysfs_deref_link(d, "iommu_group");
       if (group_link)
     {
       char *group_link = sysfs_deref_link(d, "iommu_group");
       if (group_link)
@@ -362,10 +449,9 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags)
           pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
           free(group_link);
         }
           pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
           free(group_link);
         }
-      done |= PCI_FILL_IOMMU_GROUP;
     }
 
     }
 
-  if (flags & PCI_FILL_DT_NODE)
+  if (want_fill(d, flags, PCI_FILL_DT_NODE))
     {
       char *node = sysfs_deref_link(d, "of_node");
       if (node)
     {
       char *node = sysfs_deref_link(d, "of_node");
       if (node)
@@ -373,10 +459,23 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags)
          pci_set_property(d, PCI_FILL_DT_NODE, node);
          free(node);
        }
          pci_set_property(d, PCI_FILL_DT_NODE, node);
          free(node);
        }
-      done |= PCI_FILL_DT_NODE;
     }
 
     }
 
-  return done | pci_generic_fill_info(d, flags & ~done);
+  if (want_fill(d, flags, PCI_FILL_DRIVER))
+    {
+      char *driver_path = sysfs_deref_link(d, "driver");
+      if (driver_path)
+        {
+          char *driver = strrchr(driver_path, '/');
+          driver = driver ? driver+1 : driver_path;
+          pci_set_property(d, PCI_FILL_DRIVER, driver);
+          free(driver_path);
+        }
+      else
+        clear_fill(d, PCI_FILL_DRIVER);
+    }
+
+  pci_generic_fill_info(d, flags);
 }
 
 /* Intent of the sysfs_setup() caller */
 }
 
 /* Intent of the sysfs_setup() caller */