Skip to content

Commit

Permalink
PCI: Fail safely if we can't handle BARs larger than 4GB
Browse files Browse the repository at this point in the history
We can only handle BARs larger than 4GB if both dma_addr_t and
resource_size_t are 64 bits wide.  If dma_addr_t is 32 bits, we can't
represent all the bus addresses, and if resource_size_t is 32 bits, we
can't represent all the CPU addresses.

Previously we cleared res->flags (at "fail:") for resources that were too
large.  That means we think the BAR doesn't exist at all, which in turn
means that we could enable the device even though we can't keep track of
where the BAR is and we can't make sure it doesn't overlap something else.

This preserves the type flags (MEM/IO) so we can keep from enabling the
device.

Signed-off-by: Bjorn Helgaas <[email protected]>
  • Loading branch information
bjorn-helgaas committed May 23, 2014
1 parent c96ec95 commit 23b13bc
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions drivers/pci/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
struct resource *res, unsigned int pos)
{
u32 l, sz, mask;
u64 l64, sz64, mask64;
u16 orig_cmd;
struct pci_bus_region region, inverted_region;
bool bar_too_big = false, bar_disabled = false;
Expand Down Expand Up @@ -226,9 +227,9 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
}

if (res->flags & IORESOURCE_MEM_64) {
u64 l64 = l;
u64 sz64 = sz;
u64 mask64 = mask | (u64)~0 << 32;
l64 = l;
sz64 = sz;
mask64 = mask | (u64)~0 << 32;

pci_read_config_dword(dev, pos + 4, &l);
pci_write_config_dword(dev, pos + 4, ~0);
Expand All @@ -243,9 +244,13 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
if (!sz64)
goto fail;

if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
if ((sizeof(dma_addr_t) < 8 || sizeof(resource_size_t) < 8) &&
sz64 > 0x100000000ULL) {
res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
res->start = 0;
res->end = 0;
bar_too_big = true;
goto fail;
goto out;
}

if ((sizeof(resource_size_t) < 8) && l) {
Expand Down Expand Up @@ -303,7 +308,8 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
pci_write_config_word(dev, PCI_COMMAND, orig_cmd);

if (bar_too_big)
dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n", pos);
dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
pos, (unsigned long long) sz64);
if (res->flags && !bar_disabled)
dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);

Expand Down

0 comments on commit 23b13bc

Please sign in to comment.