Skip to content

Commit

Permalink
PCI: Add PCI_STD_NUM_BARS for the number of standard BARs
Browse files Browse the repository at this point in the history
Code that iterates over all standard PCI BARs typically uses
PCI_STD_RESOURCE_END.  However, that requires the unusual test
"i <= PCI_STD_RESOURCE_END" rather than something the typical
"i < PCI_STD_NUM_BARS".

Add a definition for PCI_STD_NUM_BARS and change loops to use the more
idiomatic C style to help avoid fencepost errors.

Link: https://lore.kernel.org/r/[email protected]
Link: https://lore.kernel.org/r/[email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Denis Efremov <[email protected]>
Signed-off-by: Bjorn Helgaas <[email protected]>
Acked-by: Sebastian Ott <[email protected]>			# arch/s390/
Acked-by: Bartlomiej Zolnierkiewicz <[email protected]>	# video/fbdev/
Acked-by: Gustavo Pimentel <[email protected]>	# pci/controller/dwc/
Acked-by: Jack Wang <[email protected]>		# scsi/pm8001/
Acked-by: Martin K. Petersen <[email protected]>	# scsi/pm8001/
Acked-by: Ulf Hansson <[email protected]>			# memstick/
  • Loading branch information
evdenis authored and bjorn-helgaas committed Oct 14, 2019
1 parent 7608158 commit c9c13ba
Show file tree
Hide file tree
Showing 46 changed files with 110 additions and 113 deletions.
8 changes: 4 additions & 4 deletions arch/alpha/kernel/pci-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ static int pci_mmap_resource(struct kobject *kobj,
struct pci_bus_region bar;
int i;

for (i = 0; i < PCI_ROM_RESOURCE; i++)
for (i = 0; i < PCI_STD_NUM_BARS; i++)
if (res == &pdev->resource[i])
break;
if (i >= PCI_ROM_RESOURCE)
if (i >= PCI_STD_NUM_BARS)
return -ENODEV;

if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
Expand Down Expand Up @@ -115,7 +115,7 @@ void pci_remove_resource_files(struct pci_dev *pdev)
{
int i;

for (i = 0; i < PCI_ROM_RESOURCE; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
struct bin_attribute *res_attr;

res_attr = pdev->res_attr[i];
Expand Down Expand Up @@ -232,7 +232,7 @@ int pci_create_resource_files(struct pci_dev *pdev)
int retval;

/* Expose the PCI resources from this device as files */
for (i = 0; i < PCI_ROM_RESOURCE; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {

/* skip empty resources */
if (!pci_resource_len(pdev, i))
Expand Down
5 changes: 1 addition & 4 deletions arch/s390/include/asm/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#ifndef __ASM_S390_PCI_H
#define __ASM_S390_PCI_H

/* must be set before including pci_clp.h */
#define PCI_BAR_COUNT 6

#include <linux/pci.h>
#include <linux/mutex.h>
#include <linux/iommu.h>
Expand Down Expand Up @@ -138,7 +135,7 @@ struct zpci_dev {

char res_name[16];
bool mio_capable;
struct zpci_bar_struct bars[PCI_BAR_COUNT];
struct zpci_bar_struct bars[PCI_STD_NUM_BARS];

u64 start_dma; /* Start of available DMA addresses */
u64 end_dma; /* End of available DMA addresses */
Expand Down
6 changes: 3 additions & 3 deletions arch/s390/include/asm/pci_clp.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct mio_info {
struct {
u64 wb;
u64 wt;
} addr[PCI_BAR_COUNT];
} addr[PCI_STD_NUM_BARS];
u32 reserved[6];
} __packed;

Expand All @@ -98,9 +98,9 @@ struct clp_rsp_query_pci {
u16 util_str_avail : 1; /* utility string available? */
u16 pfgid : 8; /* pci function group id */
u32 fid; /* pci function id */
u8 bar_size[PCI_BAR_COUNT];
u8 bar_size[PCI_STD_NUM_BARS];
u16 pchid;
__le32 bar[PCI_BAR_COUNT];
__le32 bar[PCI_STD_NUM_BARS];
u8 pfip[CLP_PFIP_NR_SEGMENTS]; /* pci function internal path */
u32 : 16;
u8 fmb_len;
Expand Down
16 changes: 8 additions & 8 deletions arch/s390/pci/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
static DEFINE_SPINLOCK(zpci_domain_lock);

#define ZPCI_IOMAP_ENTRIES \
min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \
min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2), \
ZPCI_IOMAP_MAX_ENTRIES)

static DEFINE_SPINLOCK(zpci_iomap_lock);
Expand Down Expand Up @@ -294,7 +294,7 @@ static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
unsigned long offset, unsigned long max)
{
if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
return NULL;

if (static_branch_likely(&have_mio))
Expand Down Expand Up @@ -324,7 +324,7 @@ static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
unsigned long offset, unsigned long max)
{
if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
return NULL;

if (static_branch_likely(&have_mio))
Expand Down Expand Up @@ -416,7 +416,7 @@ static void zpci_map_resources(struct pci_dev *pdev)
resource_size_t len;
int i;

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
len = pci_resource_len(pdev, i);
if (!len)
continue;
Expand Down Expand Up @@ -451,7 +451,7 @@ static void zpci_unmap_resources(struct pci_dev *pdev)
if (zpci_use_mio(zdev))
return;

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
len = pci_resource_len(pdev, i);
if (!len)
continue;
Expand Down Expand Up @@ -514,7 +514,7 @@ static int zpci_setup_bus_resources(struct zpci_dev *zdev,
snprintf(zdev->res_name, sizeof(zdev->res_name),
"PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (!zdev->bars[i].size)
continue;
entry = zpci_alloc_iomap(zdev);
Expand Down Expand Up @@ -551,7 +551,7 @@ static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
{
int i;

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (!zdev->bars[i].size || !zdev->bars[i].res)
continue;

Expand All @@ -573,7 +573,7 @@ int pcibios_add_device(struct pci_dev *pdev)
pdev->dev.dma_ops = &s390_pci_dma_ops;
zpci_map_resources(pdev);

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
res = &pdev->resource[i];
if (res->parent || !res->flags)
continue;
Expand Down
6 changes: 3 additions & 3 deletions arch/s390/pci/pci_clp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static int clp_store_query_pci_fn(struct zpci_dev *zdev,
{
int i;

for (i = 0; i < PCI_BAR_COUNT; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
zdev->bars[i].val = le32_to_cpu(response->bar[i]);
zdev->bars[i].size = response->bar_size[i];
}
Expand All @@ -164,8 +164,8 @@ static int clp_store_query_pci_fn(struct zpci_dev *zdev,
sizeof(zdev->util_str));
}
zdev->mio_capable = response->mio_addr_avail;
for (i = 0; i < PCI_BAR_COUNT; i++) {
if (!(response->mio.valid & (1 << (PCI_BAR_COUNT - i - 1))))
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (!(response->mio.valid & (1 << (PCI_STD_NUM_BARS - i - 1))))
continue;

zdev->bars[i].mio_wb = (void __iomem *) response->mio.addr[i].wb;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/pci/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void pcibios_fixup_device_resources(struct pci_dev *dev)
* resource so the kernel doesn't attempt to assign
* it later on in pci_assign_unassigned_resources
*/
for (bar = 0; bar <= PCI_STD_RESOURCE_END; bar++) {
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
bar_r = &dev->resource[bar];
if (bar_r->start == 0 && bar_r->end != 0) {
bar_r->flags = 0;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/pci/intel_mid_pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static void pci_fixed_bar_fixup(struct pci_dev *dev)
PCI_DEVFN(2, 2) == dev->devfn)
return;

for (i = 0; i < PCI_ROM_RESOURCE; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
pci_read_config_dword(dev, offset + 8 + (i * 4), &size);
dev->resource[i].end = dev->resource[i].start + size - 1;
dev->resource[i].flags |= IORESOURCE_PCI_FIXED;
Expand Down
2 changes: 1 addition & 1 deletion drivers/ata/pata_atp867x.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
#ifdef ATP867X_DEBUG
atp867x_check_res(pdev);

for (i = 0; i < PCI_ROM_RESOURCE; i++)
for (i = 0; i < PCI_STD_NUM_BARS; i++)
printk(KERN_DEBUG "ATP867X: iomap[%d]=0x%llx\n", i,
(unsigned long long)(host->iomap[i]));
#endif
Expand Down
2 changes: 1 addition & 1 deletion drivers/ata/sata_nv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,7 @@ static int nv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
// Make sure this is a SATA controller by counting the number of bars
// (NVIDIA SATA controllers will always have six bars). Otherwise,
// it's an IDE controller and we ignore it.
for (bar = 0; bar < 6; bar++)
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
if (pci_resource_start(pdev, bar) == 0)
return -ENODEV;

Expand Down
2 changes: 1 addition & 1 deletion drivers/memstick/host/jmb38x_ms.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ static int jmb38x_ms_count_slots(struct pci_dev *pdev)
{
int cnt, rc = 0;

for (cnt = 0; cnt < PCI_ROM_RESOURCE; ++cnt) {
for (cnt = 0; cnt < PCI_STD_NUM_BARS; ++cnt) {
if (!(IORESOURCE_MEM & pci_resource_flags(pdev, cnt)))
break;

Expand Down
8 changes: 4 additions & 4 deletions drivers/misc/pci_endpoint_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ enum pci_barno {
struct pci_endpoint_test {
struct pci_dev *pdev;
void __iomem *base;
void __iomem *bar[6];
void __iomem *bar[PCI_STD_NUM_BARS];
struct completion irq_raised;
int last_irq;
int num_irqs;
Expand Down Expand Up @@ -687,7 +687,7 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
if (!pci_endpoint_test_request_irq(test))
goto err_disable_irq;

for (bar = BAR_0; bar <= BAR_5; bar++) {
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
base = pci_ioremap_bar(pdev, bar);
if (!base) {
Expand Down Expand Up @@ -740,7 +740,7 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
ida_simple_remove(&pci_endpoint_test_ida, id);

err_iounmap:
for (bar = BAR_0; bar <= BAR_5; bar++) {
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
if (test->bar[bar])
pci_iounmap(pdev, test->bar[bar]);
}
Expand Down Expand Up @@ -771,7 +771,7 @@ static void pci_endpoint_test_remove(struct pci_dev *pdev)
misc_deregister(&test->miscdev);
kfree(misc_device->name);
ida_simple_remove(&pci_endpoint_test_ida, id);
for (bar = BAR_0; bar <= BAR_5; bar++) {
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
if (test->bar[bar])
pci_iounmap(pdev, test->bar[bar]);
}
Expand Down
1 change: 0 additions & 1 deletion drivers/net/ethernet/intel/e1000/e1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

#define BAR_0 0
#define BAR_1 1
#define BAR_5 5

#define INTEL_E1000_ETHERNET_DEVICE(device_id) {\
PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/intel/e1000/e1000_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
goto err_ioremap;

if (adapter->need_ioport) {
for (i = BAR_1; i <= BAR_5; i++) {
for (i = BAR_1; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pdev, i) == 0)
continue;
if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Expand Down
1 change: 0 additions & 1 deletion drivers/net/ethernet/intel/ixgb/ixgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

#define BAR_0 0
#define BAR_1 1
#define BAR_5 5

struct ixgb_adapter;
#include "ixgb_hw.h"
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/intel/ixgb/ixgb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ ixgb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
goto err_ioremap;
}

for (i = BAR_1; i <= BAR_5; i++) {
for (i = BAR_1; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pdev, i) == 0)
continue;
if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/ethernet/stmicro/stmmac/stmmac_pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ static int stmmac_pci_probe(struct pci_dev *pdev,
}

/* Get the base address of device */
for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pdev, i) == 0)
continue;
ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
Expand Down Expand Up @@ -532,7 +532,7 @@ static void stmmac_pci_remove(struct pci_dev *pdev)
if (priv->plat->stmmac_clk)
clk_unregister_fixed_rate(priv->plat->stmmac_clk);

for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pdev, i) == 0)
continue;
pcim_iounmap_regions(pdev, BIT(i));
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/synopsys/dwc-xlgmac-pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int xlgmac_probe(struct pci_dev *pcidev, const struct pci_device_id *id)
return ret;
}

for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pcidev, i) == 0)
continue;
ret = pcim_iomap_regions(pcidev, BIT(i), XLGMAC_DRV_NAME);
Expand Down
2 changes: 1 addition & 1 deletion drivers/pci/controller/dwc/pci-dra7xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
enum pci_barno bar;

for (bar = BAR_0; bar <= BAR_5; bar++)
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
dw_pcie_ep_reset_bar(pci, bar);

dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
Expand Down
2 changes: 1 addition & 1 deletion drivers/pci/controller/dwc/pci-layerscape-ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void ls_pcie_ep_init(struct dw_pcie_ep *ep)
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
enum pci_barno bar;

for (bar = BAR_0; bar <= BAR_5; bar++)
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
dw_pcie_ep_reset_bar(pci, bar);
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/pci/controller/dwc/pcie-artpec6.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static void artpec6_pcie_ep_init(struct dw_pcie_ep *ep)
artpec6_pcie_wait_for_phy(artpec6_pcie);
artpec6_pcie_set_nfts(artpec6_pcie);

for (bar = BAR_0; bar <= BAR_5; bar++)
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
dw_pcie_ep_reset_bar(pci, bar);
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/pci/controller/dwc/pcie-designware-plat.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void dw_plat_pcie_ep_init(struct dw_pcie_ep *ep)
struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
enum pci_barno bar;

for (bar = BAR_0; bar <= BAR_5; bar++)
for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
dw_pcie_ep_reset_bar(pci, bar);
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/pci/controller/dwc/pcie-designware.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ struct dw_pcie_ep {
phys_addr_t phys_base;
size_t addr_size;
size_t page_size;
u8 bar_to_atu[6];
u8 bar_to_atu[PCI_STD_NUM_BARS];
phys_addr_t *outbound_addr;
unsigned long *ib_window_map;
unsigned long *ob_window_map;
Expand Down
10 changes: 5 additions & 5 deletions drivers/pci/controller/pci-hyperv.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ struct pci_bus_relations {
struct pci_q_res_req_response {
struct vmpacket_descriptor hdr;
s32 status; /* negative values are failures */
u32 probed_bar[6];
u32 probed_bar[PCI_STD_NUM_BARS];
} __packed;

struct pci_set_power {
Expand Down Expand Up @@ -539,7 +539,7 @@ struct hv_pci_dev {
* What would be observed if one wrote 0xFFFFFFFF to a BAR and then
* read it back, for each of the BAR offsets within config space.
*/
u32 probed_bar[6];
u32 probed_bar[PCI_STD_NUM_BARS];
};

struct hv_pci_compl {
Expand Down Expand Up @@ -1610,7 +1610,7 @@ static void survey_child_resources(struct hv_pcibus_device *hbus)
* so it's sufficient to just add them up without tracking alignment.
*/
list_for_each_entry(hpdev, &hbus->children, list_entry) {
for (i = 0; i < 6; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
dev_err(&hbus->hdev->device,
"There's an I/O BAR in this list!\n");
Expand Down Expand Up @@ -1684,7 +1684,7 @@ static void prepopulate_bars(struct hv_pcibus_device *hbus)
/* Pick addresses for the BARs. */
do {
list_for_each_entry(hpdev, &hbus->children, list_entry) {
for (i = 0; i < 6; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
bar_val = hpdev->probed_bar[i];
if (bar_val == 0)
continue;
Expand Down Expand Up @@ -1841,7 +1841,7 @@ static void q_resource_requirements(void *context, struct pci_response *resp,
"query resource requirements failed: %x\n",
resp->status);
} else {
for (i = 0; i < 6; i++) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
completion->hpdev->probed_bar[i] =
q_res_req->probed_bar[i];
}
Expand Down
Loading

0 comments on commit c9c13ba

Please sign in to comment.