|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * PCIe adapters on a Thunderbolt switch serve as endpoints for PCI tunnels. |
| 4 | + * Each may be attached to an upstream or downstream port of the PCIe switch |
| 5 | + * integrated into a Thunderbolt controller. |
| 6 | + * |
| 7 | + * Copyright (C) 2018 Lukas Wunner <lukas@wunner.de> |
| 8 | + */ |
| 9 | + |
| 10 | +#include "tb.h" |
| 11 | +#include "tunnel_pci.h" |
| 12 | +#include "adapter_pci.h" |
| 13 | + |
| 14 | +/** |
| 15 | + * tb_pci_adapter() - whether a given PCI device is a Thunderbolt PCIe adapter |
| 16 | + * @pdev: PCI device |
| 17 | + * |
| 18 | + * To keep it simple, this function will return a false positive in a few cases |
| 19 | + * and callers need to make sure they can handle that: |
| 20 | + * * Upstream port on a host controller |
| 21 | + * * Downstream port to the XHCI on a host controller |
| 22 | + * * Downstream port on non-chainable endpoint controllers such as Port Ridge |
| 23 | + */ |
| 24 | +static bool tb_pci_adapter(struct pci_dev *pdev) |
| 25 | +{ |
| 26 | + /* downstream ports with devfn 0 are reserved for the NHI */ |
| 27 | + return pdev->is_thunderbolt && |
| 28 | + (pci_pcie_type(pdev) == PCI_EXP_TYPE_UPSTREAM || |
| 29 | + (pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM && |
| 30 | + pdev->devfn)); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * tb_pci_find_port() - locate Thunderbolt port for a given PCI device |
| 35 | + * @pdev: PCI device |
| 36 | + * |
| 37 | + * Walk up the PCI hierarchy from @pdev to discover the sequence of |
| 38 | + * PCIe upstream and downstream ports leading to the host controller. |
| 39 | + * Then walk down the Thunderbolt daisy-chain following the previously |
| 40 | + * discovered sequence along the tunnels we've established. |
| 41 | + * |
| 42 | + * Return the port corresponding to @pdev, or %NULL if none was found. |
| 43 | + * |
| 44 | + * This function needs to be called under the global Thunderbolt lock |
| 45 | + * to prevent tb_switch and tb_pci_tunnel structs from going away. |
| 46 | + */ |
| 47 | +static struct tb_port *tb_pci_find_port(struct tb *tb, struct pci_dev *pdev) |
| 48 | +{ |
| 49 | + struct tb_cm *tcm = tb_priv(tb); |
| 50 | + struct tb_pci_tunnel *tunnel; |
| 51 | + struct tb_port *parent_port; |
| 52 | + struct pci_dev *parent_pdev; |
| 53 | + int i; |
| 54 | + |
| 55 | + if (!tb_pci_adapter(pdev)) |
| 56 | + return NULL; |
| 57 | + |
| 58 | + /* base of the recursion: we've reached the host controller */ |
| 59 | + if (pdev->bus == tb->upstream->subordinate) { |
| 60 | + for (i = 1; i <= tb->root_switch->config.max_port_number; i++) |
| 61 | + if (tb->root_switch->ports[i].pci.devfn == pdev->devfn) |
| 62 | + return &tb->root_switch->ports[i]; |
| 63 | + |
| 64 | + return NULL; |
| 65 | + } |
| 66 | + |
| 67 | + /* recurse up the PCI hierarchy */ |
| 68 | + parent_pdev = pci_upstream_bridge(pdev); |
| 69 | + if (!parent_pdev) |
| 70 | + return NULL; |
| 71 | + |
| 72 | + parent_port = tb_pci_find_port(tb, parent_pdev); |
| 73 | + if (!parent_port) |
| 74 | + return NULL; |
| 75 | + |
| 76 | + switch (parent_port->config.type) { |
| 77 | + case TB_TYPE_PCIE_UP: |
| 78 | + /* |
| 79 | + * A PCIe upstream adapter is the parent of |
| 80 | + * a PCIe downstream adapter on the same switch. |
| 81 | + */ |
| 82 | + for (i = 1; i <= parent_port->sw->config.max_port_number; i++) { |
| 83 | + struct tb_port *port = &parent_port->sw->ports[i]; |
| 84 | + if (port->config.type == TB_TYPE_PCIE_DOWN && |
| 85 | + port->pci.devfn == pdev->devfn) |
| 86 | + return port; |
| 87 | + } |
| 88 | + return NULL; |
| 89 | + case TB_TYPE_PCIE_DOWN: |
| 90 | + /* |
| 91 | + * A PCIe downstream adapter is the parent of |
| 92 | + * a PCIe upstream adapter at the other end of a tunnel. |
| 93 | + */ |
| 94 | + list_for_each_entry(tunnel, &tcm->tunnel_list, list) |
| 95 | + if (tunnel->down_port == parent_port) |
| 96 | + return tunnel->up_port; |
| 97 | + return NULL; |
| 98 | + default: |
| 99 | + return NULL; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * tb_pci_notifier_call() - Thunderbolt PCI bus notifier |
| 105 | + * @nb: Notifier block embedded in struct tb_cm |
| 106 | + * @action: Notifier action |
| 107 | + * @data: PCI device |
| 108 | + * |
| 109 | + * On addition of PCI device @data, correlate it with a PCIe adapter on the |
| 110 | + * Thunderbolt bus and store a pointer to the PCI device in struct tb_port. |
| 111 | + * On deletion, reset the pointer to %NULL. |
| 112 | + */ |
| 113 | +int tb_pci_notifier_call(struct notifier_block *nb, unsigned long action, |
| 114 | + void *data) |
| 115 | +{ |
| 116 | + struct tb_cm *tcm = container_of(nb, struct tb_cm, pci_notifier); |
| 117 | + struct tb *tb = (void *)(tcm) - offsetof(struct tb, privdata); |
| 118 | + struct device *dev = data; |
| 119 | + struct pci_dev *pdev = to_pci_dev(dev); |
| 120 | + struct tb_port *port; |
| 121 | + |
| 122 | + if ((action != BUS_NOTIFY_ADD_DEVICE && |
| 123 | + action != BUS_NOTIFY_DEL_DEVICE) || !tb_pci_adapter(pdev)) |
| 124 | + return NOTIFY_DONE; |
| 125 | + |
| 126 | + mutex_lock(&tb->lock); |
| 127 | + port = tb_pci_find_port(tb, pdev); |
| 128 | + if (!port) |
| 129 | + goto out; |
| 130 | + |
| 131 | + switch (action) { |
| 132 | + case BUS_NOTIFY_ADD_DEVICE: |
| 133 | + port->pci.dev = pdev; |
| 134 | + tb_port_info(port, "correlates with %s\n", pci_name(pdev)); |
| 135 | + break; |
| 136 | + case BUS_NOTIFY_DEL_DEVICE: |
| 137 | + port->pci.dev = NULL; |
| 138 | + tb_port_info(port, "no longer correlates with %s\n", |
| 139 | + pci_name(pdev)); |
| 140 | + break; |
| 141 | + } |
| 142 | +out: |
| 143 | + mutex_unlock(&tb->lock); |
| 144 | + return NOTIFY_DONE; |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * tb_pci_correlate() - Correlate a PCI device with a Thunderbolt port |
| 149 | + * @pdev: PCI device |
| 150 | + * @data: Thunderbolt bus |
| 151 | + * |
| 152 | + * Correlate @pdev with a PCIe adapter on Thunderbolt bus @data and store a |
| 153 | + * pointer to the PCI device in struct tb_port. This function is intended |
| 154 | + * to be used as a pci_walk_bus() callback. |
| 155 | + */ |
| 156 | +int tb_pci_correlate(struct pci_dev *pdev, void *data) |
| 157 | +{ |
| 158 | + struct tb *tb = data; |
| 159 | + struct tb_port *port; |
| 160 | + |
| 161 | + port = tb_pci_find_port(tb, pdev); |
| 162 | + if (port) { |
| 163 | + port->pci.dev = pdev; |
| 164 | + tb_port_info(port, "correlates with %s\n", pci_name(pdev)); |
| 165 | + } |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
0 commit comments