PCI: Make pci_bus_for_each_resource() index optional
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 4 Apr 2023 15:45:25 +0000 (10:45 -0500)
committerBjorn Helgaas <bhelgaas@google.com>
Wed, 5 Apr 2023 20:10:09 +0000 (15:10 -0500)
Refactor pci_bus_for_each_resource() in the same way as
pci_dev_for_each_resource(). This allows the index to be hidden inside the
implementation so the caller can omit it when it's not used otherwise.

No functional changes intended.

Link: https://lore.kernel.org/r/20230330162434.35055-6-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Krzysztof Wilczyński <kw@linux.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
drivers/pci/bus.c
drivers/pci/hotplug/shpchp_sysfs.c
drivers/pci/pci.c
drivers/pci/probe.c
drivers/pci/setup-bus.c
include/linux/pci.h

index 83ae838ceb5f0a3ea93750ea51568eb83bd91764..f37f23574ad8115ac185d7b63a231aea4bdc1994 100644 (file)
@@ -161,13 +161,13 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
                void *alignf_data,
                struct pci_bus_region *region)
 {
-       int i, ret;
        struct resource *r, avail;
        resource_size_t max;
+       int ret;
 
        type_mask |= IORESOURCE_TYPE_BITS;
 
-       pci_bus_for_each_resource(bus, r, i) {
+       pci_bus_for_each_resource(bus, r) {
                resource_size_t min_used = min;
 
                if (!r)
@@ -268,9 +268,8 @@ bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
        struct resource *res = &dev->resource[idx];
        struct resource orig_res = *res;
        struct resource *r;
-       int i;
 
-       pci_bus_for_each_resource(bus, r, i) {
+       pci_bus_for_each_resource(bus, r) {
                resource_size_t start, end;
 
                if (!r)
index 64beed7a26bed140bd974337856a16a50aac25b0..01d47a42da045a156bd871afee974e09c657fe62 100644 (file)
 static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
 {
        struct pci_dev *pdev;
-       int index, busnr;
        struct resource *res;
        struct pci_bus *bus;
        size_t len = 0;
+       int busnr;
 
        pdev = to_pci_dev(dev);
        bus = pdev->subordinate;
 
        len += sysfs_emit_at(buf, len, "Free resources: memory\n");
-       pci_bus_for_each_resource(bus, res, index) {
+       pci_bus_for_each_resource(bus, res) {
                if (res && (res->flags & IORESOURCE_MEM) &&
                                !(res->flags & IORESOURCE_PREFETCH)) {
                        len += sysfs_emit_at(buf, len,
@@ -43,7 +43,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char
                }
        }
        len += sysfs_emit_at(buf, len, "Free resources: prefetchable memory\n");
-       pci_bus_for_each_resource(bus, res, index) {
+       pci_bus_for_each_resource(bus, res) {
                if (res && (res->flags & IORESOURCE_MEM) &&
                               (res->flags & IORESOURCE_PREFETCH)) {
                        len += sysfs_emit_at(buf, len,
@@ -53,7 +53,7 @@ static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char
                }
        }
        len += sysfs_emit_at(buf, len, "Free resources: IO\n");
-       pci_bus_for_each_resource(bus, res, index) {
+       pci_bus_for_each_resource(bus, res) {
                if (res && (res->flags & IORESOURCE_IO)) {
                        len += sysfs_emit_at(buf, len,
                                             "start = %8.8llx, length = %8.8llx\n",
index 7a67611dc5f48f1b4cdc89c154bf4a4ce01ba483..99299f1299c481d07d5aa96cb4111fb430731f69 100644 (file)
@@ -779,9 +779,8 @@ struct resource *pci_find_parent_resource(const struct pci_dev *dev,
 {
        const struct pci_bus *bus = dev->bus;
        struct resource *r;
-       int i;
 
-       pci_bus_for_each_resource(bus, r, i) {
+       pci_bus_for_each_resource(bus, r) {
                if (!r)
                        continue;
                if (resource_contains(r, res)) {
index a3f68b6ba6ac2db4500e5f5f416b972e479746d9..f8191750f6b7348c618379bfff0bca729fb0ad63 100644 (file)
@@ -533,7 +533,7 @@ void pci_read_bridge_bases(struct pci_bus *child)
        pci_read_bridge_mmio_pref(child);
 
        if (dev->transparent) {
-               pci_bus_for_each_resource(child->parent, res, i) {
+               pci_bus_for_each_resource(child->parent, res) {
                        if (res && res->flags) {
                                pci_bus_add_resource(child, res,
                                                     PCI_SUBTRACTIVE_DECODE);
index 027b985dd1ee38927b1f152e90a7fd6a8459caae..fdeb121e91756ef452c20e70a660178dfad7cbbd 100644 (file)
@@ -770,9 +770,8 @@ static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
                                                  unsigned long type)
 {
        struct resource *r, *r_assigned = NULL;
-       int i;
 
-       pci_bus_for_each_resource(bus, r, i) {
+       pci_bus_for_each_resource(bus, r) {
                if (r == &ioport_resource || r == &iomem_resource)
                        continue;
                if (r && (r->flags & type_mask) == type && !r->parent)
@@ -1204,7 +1203,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
                        additional_mmio_pref_size = 0;
        struct resource *pref;
        struct pci_host_bridge *host;
-       int hdr_type, i, ret;
+       int hdr_type, ret;
 
        list_for_each_entry(dev, &bus->devices, bus_list) {
                struct pci_bus *b = dev->subordinate;
@@ -1228,7 +1227,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
                host = to_pci_host_bridge(bus->bridge);
                if (!host->size_windows)
                        return;
-               pci_bus_for_each_resource(bus, pref, i)
+               pci_bus_for_each_resource(bus, pref)
                        if (pref && (pref->flags & IORESOURCE_PREFETCH))
                                break;
                hdr_type = -1;  /* Intentionally invalid - not a PCI device. */
@@ -1333,12 +1332,11 @@ EXPORT_SYMBOL(pci_bus_size_bridges);
 
 static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
 {
-       int i;
        struct resource *parent_r;
        unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
                             IORESOURCE_PREFETCH;
 
-       pci_bus_for_each_resource(b, parent_r, i) {
+       pci_bus_for_each_resource(b, parent_r) {
                if (!parent_r)
                        continue;
 
index db3155562320291a524878dd9a6cbfb102795960..c048e70d42b9972beb8898df9f58477aa11a13cd 100644 (file)
@@ -1444,11 +1444,21 @@ int devm_request_pci_bus_resources(struct device *dev,
 /* Temporary until new and working PCI SBR API in place */
 int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
 
+#define __pci_bus_for_each_res0(bus, res, ...)                         \
+       for (unsigned int __b = 0;                                      \
+            (res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \
+            __b++)
+
+#define __pci_bus_for_each_res1(bus, res, __b)                         \
+       for (__b = 0;                                                   \
+            (res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \
+            __b++)
+
 /**
  * pci_bus_for_each_resource - iterate over PCI bus resources
  * @bus: the PCI bus
  * @res: pointer to the current resource
- * @i: index of the current resource
+ * @...: optional index of the current resource
  *
  * Iterate over PCI bus resources. The first part is to go over PCI bus
  * resource array, which has at most the %PCI_BRIDGE_RESOURCE_NUM entries.
@@ -1461,13 +1471,17 @@ int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
  *     struct resource *res;
  *     unsigned int i;
  *
+ *     // With optional index
  *     pci_bus_for_each_resource(bus, res, i)
  *             pr_info("PCI bus resource[%u]: %pR\n", i, res);
+ *
+ *     // Without index
+ *     pci_bus_for_each_resource(bus, res)
+ *             _do_something_(res);
  */
-#define pci_bus_for_each_resource(bus, res, i)                         \
-       for (i = 0;                                                     \
-           (res = pci_bus_resource_n(bus, i)) || i < PCI_BRIDGE_RESOURCE_NUM; \
-            i++)
+#define pci_bus_for_each_resource(bus, res, ...)                       \
+       CONCATENATE(__pci_bus_for_each_res, COUNT_ARGS(__VA_ARGS__))    \
+                   (bus, res, __VA_ARGS__)
 
 int __must_check pci_bus_alloc_resource(struct pci_bus *bus,
                        struct resource *res, resource_size_t size,