Skip to content

Commit f53ea40

Browse files
committed
thunderbolt: Correlate PCI devices with Thunderbolt ports
macOS correlates PCI devices with Thunderbolt ports and stores their device paths in each other's I/O Registry entry: IOThunderboltPort@7 { "PCI Device" = 4 "PCI Function" = 0 "PCI Path" = "IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB2@4" ... } DSB2@4 { "pcidebug" = "6:4:0(132:132)" "Thunderbolt Path" = "IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB0@0/IOPP/NHI0@0/AppleThunderboltHAL/AppleThunderboltNHIType1/IOThunderboltController/IOThunderboltPort@6/IOThunderboltSwitchType1/IOThunderboltPort@7" ... } Do the same by finding the Thunderbolt port corresponding to a PCI device from a bus notifier and storing a pointer to the PCI device in struct tb_port. On initial switch scan, fill in the pointers for already enumerated PCI devices. This achieves a unidirectional mapping from tb_port to pci_dev. If an inverse mapping is needed, it may be possible to use the sysdata pointer in struct pci_dev. To find the Thunderbolt port, the PCI slot numbers specified in the root switch's DROM need to be available. On Macs with Thunderbolt 1 that's not the case unless the kernel is booted by the EFI stub. Moreover the driver needs to know which tunnels have been established, which is not the case with ICM-controlled tunnel management, so the bus notifier is only registered if tunnel management is under OS control. Perhaps it is possible to retrieve a list of established tunnels from the ICM firmware, or if all else fails, follow the hop entries in a downstream port's config space to discover established tunnels. Correlation would then also work for ICM-controlled tunnel management. Ideas what we can do with correlation: * Represent the relationship between PCI devices and Thunderbolt ports with symlinks in sysfs. * Thunderbolt controllers up to revision 1 of Cactus Ridge 4C have to use INTx because MSI signaling is broken. This results in hotplug ports sharing interrupts with other devices and, when daisy-chaining multiple affected Thunderbolt controllers, can lead to extremely unbalanced interrupt usage. To avoid this we could prefer downstream ports for tunnel establishment which do not share interrupts (based on the nr_actions field of the correlated PCI device's irq_desc). * Alternatively, we could use non-working MSI signaling on affected controllers and synthesize an interrupt whenever a tunnel is established or goes down on unplug. The shared interrupts issue is grave. This is /proc/interrupts on a MacBookPro9,1 with a daisy-chain of two Light Ridge controllers and one Port Ridge (all with broken MSI signaling): 16: IO-APIC 16-fasteoi pciehp 17: IO-APIC 17-fasteoi pciehp, pciehp, pciehp, mmc0, snd_hda_intel, b43 18: IO-APIC 18-fasteoi pciehp, pciehp, i801_smbus 19: IO-APIC 19-fasteoi pciehp Signed-off-by: Lukas Wunner <lukas@wunner.de>
1 parent 58f16e7 commit f53ea40

File tree

5 files changed

+230
-14
lines changed

5 files changed

+230
-14
lines changed

drivers/thunderbolt/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
obj-${CONFIG_THUNDERBOLT} := thunderbolt.o
22
thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel_pci.o eeprom.o
3-
thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o
3+
thunderbolt-objs += adapter_pci.o domain.o dma_port.o icm.o property.o xdomain.o

drivers/thunderbolt/adapter_pci.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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_is_pci_adapter() - whether given PCI device is a Thunderbolt PCIe adapter
16+
* @pdev: PCI device
17+
*
18+
* For simplicity this function returns a false positive in the following 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_is_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 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 pci_dev *parent_pdev;
52+
struct tb_port *parent_port;
53+
struct tb_port *port;
54+
55+
if (!tb_is_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+
tb_sw_for_each_port(tb->root_switch, port)
61+
if (port->pci.devfn == pdev->devfn)
62+
return port;
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+
tb_sw_for_each_port(parent_port->sw, port)
83+
if (port->config.type == TB_TYPE_PCIE_DOWN &&
84+
port->pci.devfn == pdev->devfn)
85+
return port;
86+
return NULL;
87+
case TB_TYPE_PCIE_DOWN:
88+
/*
89+
* A PCIe downstream adapter is the parent of
90+
* a PCIe upstream adapter at the other end of a tunnel.
91+
*/
92+
list_for_each_entry(tunnel, &tcm->tunnel_list, list)
93+
if (tunnel->down_port == parent_port)
94+
return tunnel->up_port;
95+
return NULL;
96+
default:
97+
return NULL;
98+
}
99+
}
100+
101+
/**
102+
* tb_pci_notifier_call() - Thunderbolt PCI bus notifier
103+
* @nb: Notifier block embedded in struct tb_cm
104+
* @action: Notifier action
105+
* @data: PCI device
106+
*
107+
* On addition of PCI device @data, correlate it with a PCIe adapter on the
108+
* Thunderbolt bus and store a pointer to the PCI device in struct tb_port.
109+
* On deletion, reset the pointer to %NULL.
110+
*/
111+
int tb_pci_notifier_call(struct notifier_block *nb, unsigned long action,
112+
void *data)
113+
{
114+
struct tb_cm *tcm = container_of(nb, struct tb_cm, pci_notifier);
115+
struct tb *tb = tb_from_priv(tcm);
116+
struct device *dev = data;
117+
struct pci_dev *pdev = to_pci_dev(dev);
118+
struct tb_port *port;
119+
120+
if ((action != BUS_NOTIFY_ADD_DEVICE &&
121+
action != BUS_NOTIFY_DEL_DEVICE) || !tb_is_pci_adapter(pdev))
122+
return NOTIFY_DONE;
123+
124+
mutex_lock(&tb->lock);
125+
port = tb_pci_find_port(tb, pdev);
126+
if (!port)
127+
goto out;
128+
129+
switch (action) {
130+
case BUS_NOTIFY_ADD_DEVICE:
131+
port->pci.dev = pdev;
132+
tb_port_info(port, "correlates with %s\n", pci_name(pdev));
133+
break;
134+
case BUS_NOTIFY_DEL_DEVICE:
135+
port->pci.dev = NULL;
136+
tb_port_info(port, "no longer correlates with %s\n",
137+
pci_name(pdev));
138+
break;
139+
}
140+
out:
141+
mutex_unlock(&tb->lock);
142+
return NOTIFY_DONE;
143+
}
144+
145+
/**
146+
* tb_pci_correlate() - Correlate given PCI device with a Thunderbolt port
147+
* @pdev: PCI device
148+
* @data: Thunderbolt bus
149+
*
150+
* Correlate @pdev with a PCIe adapter on Thunderbolt bus @data and store a
151+
* pointer to the PCI device in struct tb_port. Intended to be used as a
152+
* pci_walk_bus() callback.
153+
*/
154+
int tb_pci_correlate(struct pci_dev *pdev, void *data)
155+
{
156+
struct tb *tb = data;
157+
struct tb_port *port;
158+
159+
port = tb_pci_find_port(tb, pdev);
160+
if (port) {
161+
port->pci.dev = pdev;
162+
tb_port_info(port, "correlates with %s\n", pci_name(pdev));
163+
}
164+
165+
return 0;
166+
}

drivers/thunderbolt/adapter_pci.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#ifndef ADAPTER_PCI_H_
11+
#define ADAPTER_PCI_H_
12+
13+
#include <linux/notifier.h>
14+
15+
int tb_pci_notifier_call(struct notifier_block *nb, unsigned long action,
16+
void *data);
17+
int tb_pci_correlate(struct pci_dev *pdev, void *data);
18+
19+
#endif

drivers/thunderbolt/tb.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@
1313
#include "tb.h"
1414
#include "tb_regs.h"
1515
#include "tunnel_pci.h"
16-
17-
/**
18-
* struct tb_cm - Simple Thunderbolt connection manager
19-
* @tunnel_list: List of active tunnels
20-
* @hotplug_active: tb_handle_hotplug will stop progressing plug
21-
* events and exit if this is not set (it needs to
22-
* acquire the lock one more time). Used to drain wq
23-
* after cfg has been paused.
24-
*/
25-
struct tb_cm {
26-
struct list_head tunnel_list;
27-
bool hotplug_active;
28-
};
16+
#include "adapter_pci.h"
2917

3018
/* enumeration & hot plug handling */
3119

@@ -355,6 +343,7 @@ static void tb_stop(struct tb *tb)
355343
struct tb_pci_tunnel *tunnel;
356344
struct tb_pci_tunnel *n;
357345

346+
bus_unregister_notifier(&pci_bus_type, &tcm->pci_notifier);
358347
/* tunnels are only present after everything has been initialized */
359348
list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
360349
tb_pci_deactivate(tunnel);
@@ -395,6 +384,11 @@ static int tb_start(struct tb *tb)
395384

396385
/* Full scan to discover devices added before the driver was loaded. */
397386
tb_scan_switch(tb->root_switch);
387+
388+
/* Correlate PCI devices with Thunderbolt ports */
389+
bus_register_notifier(&pci_bus_type, &tcm->pci_notifier);
390+
pci_walk_bus(tb->upstream->subordinate, tb_pci_correlate, tb);
391+
398392
tb_activate_pcie_devices(tb);
399393

400394
/* Allow tb_handle_hotplug to progress events */
@@ -469,6 +463,7 @@ struct tb *tb_probe(struct tb_nhi *nhi)
469463

470464
tcm = tb_priv(tb);
471465
INIT_LIST_HEAD(&tcm->tunnel_list);
466+
tcm->pci_notifier.notifier_call = tb_pci_notifier_call;
472467

473468
return tb;
474469
}

drivers/thunderbolt/tb.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
#include "ctl.h"
1818
#include "dma_port.h"
1919

20+
/**
21+
* struct tb_cm - Native Thunderbolt connection manager
22+
* @pci_notifier: Notifier to correlate PCI devices with Thunderbolt ports
23+
* @tunnel_list: List of active tunnels
24+
* @hotplug_active: tb_handle_hotplug() will stop processing plug events and
25+
* exit if this is not set (it needs to acquire the lock one
26+
* more time). Used to drain wq after cfg has been paused.
27+
*/
28+
struct tb_cm {
29+
struct notifier_block pci_notifier;
30+
struct list_head tunnel_list;
31+
unsigned int hotplug_active:1;
32+
};
33+
2034
/**
2135
* struct tb_switch_nvm - Structure holding switch NVM information
2236
* @major: Major version number of the active NVM portion
@@ -128,6 +142,9 @@ struct tb_switch {
128142
* @pci: Data specific to PCIe adapters
129143
* @pci.devfn: PCI slot/function according to DROM
130144
* @pci.unknown: Unknown data in DROM (to be reverse-engineered)
145+
* @pci.dev: PCI device of corresponding PCIe upstream or downstream port
146+
* (%NULL if not found or if removed by PCI core). To access,
147+
* acquire Thunderbolt lock, call pci_dev_get(), release the lock.
131148
*/
132149
struct tb_port {
133150
struct tb_regs_port_header config;
@@ -143,6 +160,7 @@ struct tb_port {
143160
struct {
144161
u8 devfn;
145162
u8 unknown[9];
163+
struct pci_dev *dev;
146164
} pci;
147165
};
148166
};
@@ -250,6 +268,11 @@ static inline void *tb_priv(struct tb *tb)
250268
return (void *)tb->privdata;
251269
}
252270

271+
static inline struct tb *tb_from_priv(void *priv)
272+
{
273+
return priv - offsetof(struct tb, privdata);
274+
}
275+
253276
#define TB_AUTOSUSPEND_DELAY 15000 /* ms */
254277

255278
/* helper functions & macros */
@@ -332,6 +355,19 @@ static inline int tb_port_write(struct tb_port *port, const void *buffer,
332355
length);
333356
}
334357

358+
/**
359+
* tb_sw_for_each_port() - iterate over ports on a switch
360+
* @switch: pointer to struct tb_switch over whose ports shall be iterated
361+
* @port: pointer to struct tb_port which shall be used as the iterator
362+
*
363+
* Excludes port 0, which is the switch itself and therefore irrelevant for
364+
* most iterations.
365+
*/
366+
#define tb_sw_for_each_port(switch, port) \
367+
for (port = &(switch)->ports[1]; \
368+
port <= &(switch)->ports[(switch)->config.max_port_number]; \
369+
port++)
370+
335371
#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
336372
#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
337373
#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)

0 commit comments

Comments
 (0)