Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI: Make pci_bus_for_each_resource() index optional

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>

authored by

Andy Shevchenko and committed by
Bjorn Helgaas
02992064 ceb928be

+32 -22
+3 -4
drivers/pci/bus.c
··· 161 161 void *alignf_data, 162 162 struct pci_bus_region *region) 163 163 { 164 - int i, ret; 165 164 struct resource *r, avail; 166 165 resource_size_t max; 166 + int ret; 167 167 168 168 type_mask |= IORESOURCE_TYPE_BITS; 169 169 170 - pci_bus_for_each_resource(bus, r, i) { 170 + pci_bus_for_each_resource(bus, r) { 171 171 resource_size_t min_used = min; 172 172 173 173 if (!r) ··· 268 268 struct resource *res = &dev->resource[idx]; 269 269 struct resource orig_res = *res; 270 270 struct resource *r; 271 - int i; 272 271 273 - pci_bus_for_each_resource(bus, r, i) { 272 + pci_bus_for_each_resource(bus, r) { 274 273 resource_size_t start, end; 275 274 276 275 if (!r)
+4 -4
drivers/pci/hotplug/shpchp_sysfs.c
··· 24 24 static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char *buf) 25 25 { 26 26 struct pci_dev *pdev; 27 - int index, busnr; 28 27 struct resource *res; 29 28 struct pci_bus *bus; 30 29 size_t len = 0; 30 + int busnr; 31 31 32 32 pdev = to_pci_dev(dev); 33 33 bus = pdev->subordinate; 34 34 35 35 len += sysfs_emit_at(buf, len, "Free resources: memory\n"); 36 - pci_bus_for_each_resource(bus, res, index) { 36 + pci_bus_for_each_resource(bus, res) { 37 37 if (res && (res->flags & IORESOURCE_MEM) && 38 38 !(res->flags & IORESOURCE_PREFETCH)) { 39 39 len += sysfs_emit_at(buf, len, ··· 43 43 } 44 44 } 45 45 len += sysfs_emit_at(buf, len, "Free resources: prefetchable memory\n"); 46 - pci_bus_for_each_resource(bus, res, index) { 46 + pci_bus_for_each_resource(bus, res) { 47 47 if (res && (res->flags & IORESOURCE_MEM) && 48 48 (res->flags & IORESOURCE_PREFETCH)) { 49 49 len += sysfs_emit_at(buf, len, ··· 53 53 } 54 54 } 55 55 len += sysfs_emit_at(buf, len, "Free resources: IO\n"); 56 - pci_bus_for_each_resource(bus, res, index) { 56 + pci_bus_for_each_resource(bus, res) { 57 57 if (res && (res->flags & IORESOURCE_IO)) { 58 58 len += sysfs_emit_at(buf, len, 59 59 "start = %8.8llx, length = %8.8llx\n",
+1 -2
drivers/pci/pci.c
··· 779 779 { 780 780 const struct pci_bus *bus = dev->bus; 781 781 struct resource *r; 782 - int i; 783 782 784 - pci_bus_for_each_resource(bus, r, i) { 783 + pci_bus_for_each_resource(bus, r) { 785 784 if (!r) 786 785 continue; 787 786 if (resource_contains(r, res)) {
+1 -1
drivers/pci/probe.c
··· 533 533 pci_read_bridge_mmio_pref(child); 534 534 535 535 if (dev->transparent) { 536 - pci_bus_for_each_resource(child->parent, res, i) { 536 + pci_bus_for_each_resource(child->parent, res) { 537 537 if (res && res->flags) { 538 538 pci_bus_add_resource(child, res, 539 539 PCI_SUBTRACTIVE_DECODE);
+4 -6
drivers/pci/setup-bus.c
··· 770 770 unsigned long type) 771 771 { 772 772 struct resource *r, *r_assigned = NULL; 773 - int i; 774 773 775 - pci_bus_for_each_resource(bus, r, i) { 774 + pci_bus_for_each_resource(bus, r) { 776 775 if (r == &ioport_resource || r == &iomem_resource) 777 776 continue; 778 777 if (r && (r->flags & type_mask) == type && !r->parent) ··· 1203 1204 additional_mmio_pref_size = 0; 1204 1205 struct resource *pref; 1205 1206 struct pci_host_bridge *host; 1206 - int hdr_type, i, ret; 1207 + int hdr_type, ret; 1207 1208 1208 1209 list_for_each_entry(dev, &bus->devices, bus_list) { 1209 1210 struct pci_bus *b = dev->subordinate; ··· 1227 1228 host = to_pci_host_bridge(bus->bridge); 1228 1229 if (!host->size_windows) 1229 1230 return; 1230 - pci_bus_for_each_resource(bus, pref, i) 1231 + pci_bus_for_each_resource(bus, pref) 1231 1232 if (pref && (pref->flags & IORESOURCE_PREFETCH)) 1232 1233 break; 1233 1234 hdr_type = -1; /* Intentionally invalid - not a PCI device. */ ··· 1332 1333 1333 1334 static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) 1334 1335 { 1335 - int i; 1336 1336 struct resource *parent_r; 1337 1337 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | 1338 1338 IORESOURCE_PREFETCH; 1339 1339 1340 - pci_bus_for_each_resource(b, parent_r, i) { 1340 + pci_bus_for_each_resource(b, parent_r) { 1341 1341 if (!parent_r) 1342 1342 continue; 1343 1343
+19 -5
include/linux/pci.h
··· 1444 1444 /* Temporary until new and working PCI SBR API in place */ 1445 1445 int pci_bridge_secondary_bus_reset(struct pci_dev *dev); 1446 1446 1447 + #define __pci_bus_for_each_res0(bus, res, ...) \ 1448 + for (unsigned int __b = 0; \ 1449 + (res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \ 1450 + __b++) 1451 + 1452 + #define __pci_bus_for_each_res1(bus, res, __b) \ 1453 + for (__b = 0; \ 1454 + (res = pci_bus_resource_n(bus, __b)) || __b < PCI_BRIDGE_RESOURCE_NUM; \ 1455 + __b++) 1456 + 1447 1457 /** 1448 1458 * pci_bus_for_each_resource - iterate over PCI bus resources 1449 1459 * @bus: the PCI bus 1450 1460 * @res: pointer to the current resource 1451 - * @i: index of the current resource 1461 + * @...: optional index of the current resource 1452 1462 * 1453 1463 * Iterate over PCI bus resources. The first part is to go over PCI bus 1454 1464 * resource array, which has at most the %PCI_BRIDGE_RESOURCE_NUM entries. ··· 1471 1461 * struct resource *res; 1472 1462 * unsigned int i; 1473 1463 * 1464 + * // With optional index 1474 1465 * pci_bus_for_each_resource(bus, res, i) 1475 1466 * pr_info("PCI bus resource[%u]: %pR\n", i, res); 1467 + * 1468 + * // Without index 1469 + * pci_bus_for_each_resource(bus, res) 1470 + * _do_something_(res); 1476 1471 */ 1477 - #define pci_bus_for_each_resource(bus, res, i) \ 1478 - for (i = 0; \ 1479 - (res = pci_bus_resource_n(bus, i)) || i < PCI_BRIDGE_RESOURCE_NUM; \ 1480 - i++) 1472 + #define pci_bus_for_each_resource(bus, res, ...) \ 1473 + CONCATENATE(__pci_bus_for_each_res, COUNT_ARGS(__VA_ARGS__)) \ 1474 + (bus, res, __VA_ARGS__) 1481 1475 1482 1476 int __must_check pci_bus_alloc_resource(struct pci_bus *bus, 1483 1477 struct resource *res, resource_size_t size,