static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
 {
        unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
-       struct resource *devr = &dev->resource[idx];
+       struct resource *devr = &dev->resource[idx], *busr;
 
        if (!dev->bus)
                return 0;
-       for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
-               struct resource *busr = dev->bus->resource[i];
 
+       pci_bus_for_each_resource(dev->bus, busr, i) {
                if (!busr || ((busr->flags ^ devr->flags) & type_mask))
                        continue;
                if ((devr->start) && (devr->start >= busr->start) &&
 
 static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
 {
        unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
-       struct resource *devr = &dev->resource[idx];
+       struct resource *devr = &dev->resource[idx], *busr;
 
        if (dev->bus) {
-               for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-                       struct resource *busr = dev->bus->resource[i];
-
+               pci_bus_for_each_resource(dev->bus, busr, i) {
                        if (!busr || (busr->flags ^ devr->flags) & type_mask)
                                continue;
 
 
 
        struct pci_dev *dev = bus->self;
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
-               if ((res = bus->resource[i]) == NULL)
-                       continue;
-               if (!res->flags)
+       pci_bus_for_each_resource(bus, res, i) {
+               if (!res || !res->flags)
                        continue;
                if (i >= 3 && bus->self->transparent)
                        continue;
        pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
                 pci_domain_nr(bus), bus->number);
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
-               if ((res = bus->resource[i]) == NULL || !res->flags
-                   || res->start > res->end || res->parent)
+       pci_bus_for_each_resource(bus, res, i) {
+               if (!res || !res->flags || res->start > res->end || res->parent)
                        continue;
                if (bus->parent == NULL)
                        pr = (res->flags & IORESOURCE_IO) ?
 
        int i;
        u8 *dummy;
        struct pci_bus *bus = dev->bus;
+       struct resource *res;
        resource_size_t end = 0;
 
        for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCES+3; i++) {
                        end = pci_resource_end(dev, i);
        }
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               if ((bus->resource[i]) &&
-                       (bus->resource[i]->flags & IORESOURCE_MEM)) {
-                       if (bus->resource[i]->end == end)
-                               dummy = ioremap(bus->resource[i]->start, 0x4);
+       pci_bus_for_each_resource(bus, res, i) {
+               if (res && res->flags & IORESOURCE_MEM) {
+                       if (res->end == end)
+                               dummy = ioremap(res->start, 0x4);
                        else
-                               dummy = ioremap(bus->resource[i]->end - 3, 0x4);
+                               dummy = ioremap(res->end - 3, 0x4);
                        if (dummy) {
                                in_8(dummy);
                                iounmap(dummy);
 
                void *alignf_data)
 {
        int i, ret = -ENOMEM;
+       struct resource *r;
        resource_size_t max = -1;
 
        type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
        if (!(res->flags & IORESOURCE_MEM_64))
                max = PCIBIOS_MAX_MEM_32;
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               struct resource *r = bus->resource[i];
+       pci_bus_for_each_resource(bus, r, i) {
                if (!r)
                        continue;
 
 
        bus = pdev->subordinate;
 
        out += sprintf(buf, "Free resources: memory\n");
-       for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
-               res = bus->resource[index];
+       pci_bus_for_each_resource(bus, res, index) {
                if (res && (res->flags & IORESOURCE_MEM) &&
                                !(res->flags & IORESOURCE_PREFETCH)) {
                        out += sprintf(out, "start = %8.8llx, "
                }
        }
        out += sprintf(out, "Free resources: prefetchable memory\n");
-       for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
-               res = bus->resource[index];
+       pci_bus_for_each_resource(bus, res, index) {
                if (res && (res->flags & IORESOURCE_MEM) &&
                               (res->flags & IORESOURCE_PREFETCH)) {
                        out += sprintf(out, "start = %8.8llx, "
                }
        }
        out += sprintf(out, "Free resources: IO\n");
-       for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
-               res = bus->resource[index];
+       pci_bus_for_each_resource(bus, res, index) {
                if (res && (res->flags & IORESOURCE_IO)) {
                        out += sprintf(out, "start = %8.8llx, "
                                        "length = %8.8llx\n",
 
 {
        const struct pci_bus *bus = dev->bus;
        int i;
-       struct resource *best = NULL;
+       struct resource *best = NULL, *r;
 
-       for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               struct resource *r = bus->resource[i];
+       pci_bus_for_each_resource(bus, r, i) {
                if (!r)
                        continue;
                if (res->start && !(res->start >= r->start && res->end <= r->end))
 
        unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
                                  IORESOURCE_PREFETCH;
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               r = bus->resource[i];
+       pci_bus_for_each_resource(bus, r, i) {
                if (r == &ioport_resource || r == &iomem_resource)
                        continue;
                if (r && (r->flags & type_mask) == type && !r->parent)
 
 static void pci_bus_dump_res(struct pci_bus *bus)
 {
-        int i;
-
-        for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-                struct resource *res = bus->resource[i];
+       struct resource *res;
+       int i;
 
+       pci_bus_for_each_resource(bus, res, i) {
                if (!res || !res->end || !res->flags)
                         continue;
 
 
                return -EINVAL;
 #endif
 
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               res = s->cb_dev->bus->resource[i];
+       pci_bus_for_each_resource(s->cb_dev->bus, res, i) {
                if (!res)
                        continue;
 
 
 static int yenta_search_res(struct yenta_socket *socket, struct resource *res,
                            u32 min)
 {
+       struct resource *root;
        int i;
-       for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
-               struct resource *root = socket->dev->bus->resource[i];
+
+       pci_bus_for_each_resource(socket->dev->bus, root, i) {
                if (!root)
                        continue;
 
 
 void pci_release_selected_regions(struct pci_dev *, int);
 
 /* drivers/pci/bus.c */
+#define pci_bus_for_each_resource(bus, res, i)                         \
+       for (i = 0; res = bus->resource[i], i < PCI_BUS_NUM_RESOURCES; i++)
+
 int __must_check pci_bus_alloc_resource(struct pci_bus *bus,
                        struct resource *res, resource_size_t size,
                        resource_size_t align, resource_size_t min,