Skip to content

Commit c25dc82

Browse files
Alex Williamsonbjorn-helgaas
authored andcommitted
PCI: Add DMA alias iterator
In a mixed PCI/PCI-X/PCIe topology, bridges can take ownership of transactions, replacing the original requester ID with their own. Sometimes we just want to know the resulting device or resulting alias; other times we want each step in the chain. This iterator allows either usage. When an endpoint is connected via an unbroken chain of PCIe switches and root ports, it has no alias and its requester ID is visible to the root bus. When PCI/X get in the way, we pick up aliases for bridges. The reason why we potentially care about each step in the path is because of PCI-X. PCI-X has the concept of a requester ID, but bridges may or may not take ownership of various types of transactions. We therefore leave it to the consumer of this function to prune out what they don't care about rather than attempt to flatten the alias ourselves. Tested-by: George Spelvin <linux@horizon.com> Tested-by: Pat Erley <pat-lkml@erley.org> Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent c9eaa44 commit c25dc82

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

drivers/pci/search.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,76 @@
1717
DECLARE_RWSEM(pci_bus_sem);
1818
EXPORT_SYMBOL_GPL(pci_bus_sem);
1919

20+
/*
21+
* pci_for_each_dma_alias - Iterate over DMA aliases for a device
22+
* @pdev: starting downstream device
23+
* @fn: function to call for each alias
24+
* @data: opaque data to pass to @fn
25+
*
26+
* Starting @pdev, walk up the bus calling @fn for each possible alias
27+
* of @pdev at the root bus.
28+
*/
29+
int pci_for_each_dma_alias(struct pci_dev *pdev,
30+
int (*fn)(struct pci_dev *pdev,
31+
u16 alias, void *data), void *data)
32+
{
33+
struct pci_bus *bus;
34+
int ret;
35+
36+
ret = fn(pdev, PCI_DEVID(pdev->bus->number, pdev->devfn), data);
37+
if (ret)
38+
return ret;
39+
40+
for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
41+
struct pci_dev *tmp;
42+
43+
/* Skip virtual buses */
44+
if (!bus->self)
45+
continue;
46+
47+
tmp = bus->self;
48+
49+
/*
50+
* PCIe-to-PCI/X bridges alias transactions from downstream
51+
* devices using the subordinate bus number (PCI Express to
52+
* PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
53+
* where the upstream bus is PCI/X we alias to the bridge
54+
* (there are various conditions in the previous reference
55+
* where the bridge may take ownership of transactions, even
56+
* when the secondary interface is PCI-X).
57+
*/
58+
if (pci_is_pcie(tmp)) {
59+
switch (pci_pcie_type(tmp)) {
60+
case PCI_EXP_TYPE_ROOT_PORT:
61+
case PCI_EXP_TYPE_UPSTREAM:
62+
case PCI_EXP_TYPE_DOWNSTREAM:
63+
continue;
64+
case PCI_EXP_TYPE_PCI_BRIDGE:
65+
ret = fn(tmp,
66+
PCI_DEVID(tmp->subordinate->number,
67+
PCI_DEVFN(0, 0)), data);
68+
if (ret)
69+
return ret;
70+
continue;
71+
case PCI_EXP_TYPE_PCIE_BRIDGE:
72+
ret = fn(tmp,
73+
PCI_DEVID(tmp->bus->number,
74+
tmp->devfn), data);
75+
if (ret)
76+
return ret;
77+
continue;
78+
}
79+
} else {
80+
ret = fn(tmp, PCI_DEVID(tmp->bus->number, tmp->devfn),
81+
data);
82+
if (ret)
83+
return ret;
84+
}
85+
}
86+
87+
return ret;
88+
}
89+
2090
/*
2191
* find the upstream PCIe-to-PCI bridge of a PCI device
2292
* if the device is PCIE, return NULL

include/linux/pci.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,10 @@ static inline struct eeh_dev *pci_dev_to_eeh_dev(struct pci_dev *pdev)
17951795
}
17961796
#endif
17971797

1798+
int pci_for_each_dma_alias(struct pci_dev *pdev,
1799+
int (*fn)(struct pci_dev *pdev,
1800+
u16 alias, void *data), void *data);
1801+
17981802
/**
17991803
* pci_find_upstream_pcie_bridge - find upstream PCIe-to-PCI bridge of a device
18001804
* @pdev: the PCI device

0 commit comments

Comments
 (0)