Skip to contentSkip to conversation

thunderbolt: Add support platform NHI driver

PATCH New Chunfeng Yun 3 participants 15 messages Nov 23 – Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover thunderbolt: Add support platform NHI driver

Historically, the USB4 / Thunderbolt subsystem has had a hard dependency on PCI, bundling the core framework, the PCIe-attached Native Host Interface (NHI) driver (pci.c), and the Intel Connection Manager (icm.c) into a single module (thunderbolt.ko).

Modern ARM64 SoCs (such as MediaTek MT8901 / MT8992) integrate the USB4 host router / NHI directly as an MMIO platform device rather than sitting on an external PCIe bus. Furthermore, embedded and SoC-based platforms often do not include PCIe-attached host routers or require Intel ICM support, making a mandatory PCI dependency and bundled PCIe code undesirable.

This series decouples PCIe NHI and Intel ICM support from the Thunderbolt core framework, introduces a generic platform NHI driver supporting ACPI bindings, and addresses probe error handling, link training timing, and disconnect warning issues observed during runtime PM and suspend/resume.

Overview of Changes
===================

1. Modularization & Namespace Cleanups (Patches 1-3)
- Patches 1 & 2 clean up internal interfaces before exporting. Core NHI functions are prefixed with standard tb_/tb_ring_ namespace prefixes. Intel ICM mailbox helpers (nhi_mailbox_cmd/mode) are moved into icm.c and made static, and interrupt work initialization is internalized in tb_nhi_probe().
- Patch 3 drops the hard "depends on PCI" from CONFIG_USB4, introduces CONFIG_USB4_PCI (defaulting to USB4) to build PCIe NHI and ICM as an opt-in module (thunderbolt_pci.ko), adds a .select_cm() hook in tb_nhi_ops to allow connection manager selection without core hardcoding, and exports symbols required by the modular drivers.

2. Core Probe Error Handling & Unwind Fix (Patch 4)
- Patch 4 fixes initialization ordering and error unwind paths in tb_nhi_probe(). Moves dma_set_mask_and_coherent() and spin_lock_init() before ops->init_interrupts() to avoid race conditions with early interrupts, and consolidates post-interrupt failure paths by invoking tb_nhi_shutdown() to ensure interrupt work is flushed, IRQs are masked, and controller power-off callbacks are executed.

3. Platform NHI Driver (Patch 5)
- Introduces CONFIG_USB4_PLATFORM (thunderbolt_platform.ko), adding support for memory-mapped USB4 NHIs via ACPI ("ACPI0015").
- Implements platform-specific tb_nhi_ops hooks and runtime PM.
- Adds support for an optional dedicated wakeup interrupt from an always-on power domain, necessary for SoCs where the primary NHI MAC/PHY power domain is gated during suspend.
- Calls pm_runtime_resume() in nhi_suspend() if the device is runtime-suspended, ensuring the controller is in an active state before system suspend (unlike the PCI bus, the platform bus does not automatically runtime-resume devices prior to system suspend).

4. Link Training and Disconnect Improvements (Patches 6-7)
- Patch 6 increases the port link training timeout in tb_wait_for_port() from 1s to 2s and updates the kernel-doc documentation. In the USB4 specification, Phase 5 (Link Equalization) alone is permitted up to 1000 ms. Factoring in Phases 2-4 (~200 ms) and retimer command transaction delays across dual-retimer topologies, physical link establishment can legitimately take 1.2s to 1.5s+, which previously led to spurious disconnects during wakeups.
- Patch 7 decouples the TB_PORT_UNPLUGGED wait time from the overall 2s link-up timeout. Under normal link training, the port enters TB_PORT_CONNECTING within ~200 ms. Capping TB_PORT_UNPLUGGED retries to 400 ms allows genuine device disconnect wakeups to be handled promptly without waiting the full 2s.

5. Drop WARN_ON in Disconnect Flow (Patch 8)
- Patch 8 drops WARN_ON(!up) and WARN_ON(!tunnel) checks in tb_disconnect_pci(). When userspace deauthorizes a router via sysfs (writing 0 to 'authorized'), missing upstream ports or tunnels can legitimately happen if a router was unplugged/lost during sleep or if tunnel activation previously failed. Removing WARN_ON() avoids false kernel splats and panics on panic_on_warn systems while cleanly returning -ENODEV.

Differences from Previous Versions
==================================

Changes in v3:
- Patch 3: Added 'default USB4' to CONFIG_USB4_PCI in Kconfig so existing configurations retain PCIe NHI support by default without config churn. Exported tb_xdomain_remove symbol.
- Patch 4: Added new patch to fix error handling and unwind paths in tb_nhi_probe(). Reordered DMA mask setup and spinlock initialization before interrupt setup, and ensured tb_nhi_shutdown() is invoked on all post-interrupt probe failure paths.
- Patch 5: Added 'depends on ACPI' to CONFIG_USB4_PLATFORM and focused platform device matching on ACPI0015. Added pm_runtime_resume() call in nhi_suspend() to handle platform bus runtime PM behavior during system suspend. Cleaner Wake IRQ Handling. Kernel Coding Cleanups.
- Patch 6: Updated kernel-doc comment in tb_wait_for_port() to reflect the new 2-second timeout value.
- Patch 7: Clarified kernel-doc comment phrasing ("router resume").
- Patch 8: Reworked from v2's sw->is_unplugged checks to directly drop the WARN_ON(!up) and WARN_ON(!tunnel) checks in tb_disconnect_pci(), as missing ports/tunnels are expected under dynamic disconnect conditions and do not indicate a kernel bug.

Changes in v2:
- Based on Konrad's patch, decoupled PCIe NHI and ICM into a separate module (thunderbolt_pci.ko, CONFIG_USB4_PCI) and removed the hard PCI dependency from CONFIG_USB4.
- Added preparatory patches to prefix core NHI functions with tb_/tb_ring_ and internalize ICM mailbox and interrupt work.
- Built platform driver as a separate module (thunderbolt_platform.ko, CONFIG_USB4_PLATFORM).
- Renamed plat.c to platform.c and updated driver symbols.
- Squashed optional dedicated wake IRQ support directly into the platform driver patch.
- Reduced link training timeout from 2.5s to 2.0s, and decoupled the TB_PORT_UNPLUGGED timeout to 400ms.

Testing
=======
This series has been tested on MediaTek ARM64 SoC platforms with an integrated platform USB4 NHI, verifying:
- Cold boot, device discovery, and tunnel allocation (PCIe, USB3).
- System/runtime suspend and wake-on-connect/disconnect/remote wakeup.
- Link training across wakeups with dual-retimer topologies.
- Concurrent sysfs deauthorization during disconnect events (verifying absence of WARN_ON splats).
- Modular builds with CONFIG_USB4=m, CONFIG_USB4_PCI=m, and CONFIG_USB4_PLATFORM=m, as well as built-in configurations.

Chunfeng Yun (7):
thunderbolt: Prefix NHI core functions with tb_ and tb_ring_
thunderbolt: Move ICM mailbox functions to icm.c and internalize
interrupt work
thunderbolt: Fix error handling and unwind paths in tb_nhi_probe()
thunderbolt: Add platform NHI driver
thunderbolt: Increase the lane adapter link-up timeout value
thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and
TB_PORT_UP
thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

Konrad Dybcio (1):
thunderbolt: Make PCIe NHI support opt-in

 drivers/thunderbolt/Kconfig    |  22 ++++-
 drivers/thunderbolt/Makefile   |  10 +-
 drivers/thunderbolt/ctl.c      |   6 +-
 drivers/thunderbolt/domain.c   |  14 ++-
 drivers/thunderbolt/icm.c      |  59 ++++++++++++
 drivers/thunderbolt/nhi.c      | 132 +++++++++----------------
 drivers/thunderbolt/nhi.h      |  16 ++--
 drivers/thunderbolt/pci.c      |  51 ++++------
 drivers/thunderbolt/platform.c | 170 +++++++++++++++++++++++++++++++++
 drivers/thunderbolt/switch.c   |  23 +++--
 drivers/thunderbolt/tb.c       |   4 +-
 drivers/thunderbolt/tb.h       |   2 -
 drivers/thunderbolt/tunnel.c   |   1 +
 drivers/thunderbolt/xdomain.c  |   5 +
 include/linux/thunderbolt.h    |   4 +
 15 files changed, 375 insertions(+), 144 deletions(-)
 create mode 100644 drivers/thunderbolt/platform.c
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 1/8 thunderbolt: Prefix NHI core functions with tb_ and tb_ring_

In preparation for modularizing PCIe NHI support and allowing PCIe and platform NHI drivers to be built as separate modules atop the Thunderbolt core framework, several NHI functions and structures will need to be exported.

Currently, several of these functions lack standard subsystem prefixes, risking namespace pollution or collisions across the flat kernel symbol namespace:

- ring_msix() lacks any subsystem prefix and diverges from all other exported ring functions in the driver, which use the "tb_ring_" prefix.
- nhi_probe(), nhi_shutdown(), and nhi_pm_ops lack the standard "tb_" prefix used across the Thunderbolt public interface (such as struct tb_nhi and struct tb_nhi_ops).
- nhi_msi() is named after PCI MSI, even though the ISR simply schedules the interrupt work and can serve as a generic ISR for non-PCIe platform controllers as well.

Rename these functions and symbols to conform to Thunderbolt subsystem naming conventions before exporting them:

- ring_msix       -> tb_ring_msix
- nhi_msi         -> tb_nhi_isr
- nhi_probe       -> tb_nhi_probe
- nhi_shutdown    -> tb_nhi_shutdown
- nhi_pm_ops      -> tb_nhi_pm_ops

---
v3: no changes

---
 drivers/thunderbolt/nhi.c | 14 +++++++-------
 drivers/thunderbolt/nhi.h | 10 +++++-----
 drivers/thunderbolt/pci.c | 10 +++++-----
 3 files changed, 17 insertions(+), 17 deletions(-)
drivers/thunderbolt/nhi.c +7 -7
index e99a3fcc4a29..c574c336f25b 100644
@@ -535,7 +535,7 @@ static void ring_clear_msix(const struct tb_ring *ring)
 			  4 * (ring->nhi->hop_count / 32));
 }
 
-irqreturn_t ring_msix(int irq, void *data)
+irqreturn_t tb_ring_msix(int irq, void *data)
 {
 	struct tb_ring *ring = data;
 
@@ -935,7 +935,7 @@ void tb_ring_free(struct tb_ring *ring)
 
 	/*
 	 * ring->work can no longer be scheduled (it is scheduled only
-	 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
+	 * by nhi_interrupt_work, ring_stop and tb_ring_msix). Wait for it
 	 * to finish before freeing the ring.
 	 */
 	flush_work(&ring->work);
@@ -1072,7 +1072,7 @@ void nhi_interrupt_work(struct work_struct *work)
 	spin_unlock_irq(&nhi->lock);
 }
 
-irqreturn_t nhi_msi(int irq, void *data)
+irqreturn_t tb_nhi_isr(int irq, void *data)
 {
 	struct tb_nhi *nhi = data;
 	schedule_work(&nhi->interrupt_work);
@@ -1216,7 +1216,7 @@ static int nhi_runtime_resume(struct device *dev)
 	return tb_domain_runtime_resume(tb);
 }
 
-void nhi_shutdown(struct tb_nhi *nhi)
+void tb_nhi_shutdown(struct tb_nhi *nhi)
 {
 	int i;
 
@@ -1306,7 +1306,7 @@ static struct tb *nhi_select_cm(struct tb_nhi *nhi)
 	return tb;
 }
 
-int nhi_probe(struct tb_nhi *nhi)
+int tb_nhi_probe(struct tb_nhi *nhi)
 {
 	struct device *dev = nhi->dev;
 	struct tb *tb;
@@ -1371,7 +1371,7 @@ int nhi_probe(struct tb_nhi *nhi)
 		 */
 		tb_domain_put(tb);
 		wait_for_completion(&nhi->domain_released);
-		nhi_shutdown(nhi);
+		tb_nhi_shutdown(nhi);
 		return dev_err_probe(dev, res, "failed to add domain\n");
 	}
 	dev_set_drvdata(dev, tb);
@@ -1391,7 +1391,7 @@ int nhi_probe(struct tb_nhi *nhi)
  * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  * resume_noirq until we are done.
  */
-const struct dev_pm_ops nhi_pm_ops = {
+const struct dev_pm_ops tb_nhi_pm_ops = {
 	.suspend_noirq = nhi_suspend_noirq,
 	.resume_noirq = nhi_resume_noirq,
 	.freeze_noirq = nhi_freeze_noirq,  /*
drivers/thunderbolt/nhi.h +5 -5
index 88af9ab9fde7..a36c62c49a5c 100644
@@ -32,11 +32,11 @@ enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi);
 void nhi_enable_int_throttling(struct tb_nhi *nhi);
 void nhi_disable_interrupts(struct tb_nhi *nhi);
 void nhi_interrupt_work(struct work_struct *work);
-irqreturn_t nhi_msi(int irq, void *data);
-irqreturn_t ring_msix(int irq, void *data);
-int nhi_probe(struct tb_nhi *nhi);
-void nhi_shutdown(struct tb_nhi *nhi);
-extern const struct dev_pm_ops nhi_pm_ops;
+irqreturn_t tb_nhi_isr(int irq, void *data);
+irqreturn_t tb_ring_msix(int irq, void *data);
+int tb_nhi_probe(struct tb_nhi *nhi);
+void tb_nhi_shutdown(struct tb_nhi *nhi);
+extern const struct dev_pm_ops tb_nhi_pm_ops;
 
 /**
  * struct tb_nhi_ops - NHI specific optional operations
drivers/thunderbolt/pci.c +5 -5
index 4408229d8ac7..2f0abf4d27a3 100644
@@ -212,7 +212,7 @@ static int nhi_pci_init_msi(struct tb_nhi *nhi)
 		if (irq < 0)
 			return irq;
 
-		res = devm_request_irq(&pdev->dev, irq, nhi_msi,
+		res = devm_request_irq(&pdev->dev, irq, tb_nhi_isr,
 				       IRQF_NO_SUSPEND, "thunderbolt", nhi);
 		if (res)
 			return res;
@@ -281,7 +281,7 @@ static int nhi_pci_ring_request_msix(struct tb_ring *ring, bool no_suspend)
 	ring->irq = ret;
 
 	irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
-	ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
+	ret = request_irq(ring->irq, tb_ring_msix, irqflags, "thunderbolt", ring);
 	if (ret)
 		goto err_ida_remove;
 
@@ -588,7 +588,7 @@ static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	pci_set_master(pdev);
 
-	return nhi_probe(&nhi_pci->nhi);
+	return tb_nhi_probe(&nhi_pci->nhi);
 }
 
 static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
@@ -610,7 +610,7 @@ static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
 
 	tb_domain_remove(tb);
 	wait_for_completion(&nhi->domain_released);
-	nhi_shutdown(nhi);
+	tb_nhi_shutdown(nhi);
 }
 
 static void nhi_pci_remove(struct pci_dev *pdev)
@@ -724,7 +724,7 @@ static struct pci_driver nhi_driver = {
 	.probe = nhi_pci_probe,
 	.remove = nhi_pci_remove,
 	.shutdown = nhi_pci_shutdown,
-	.driver.pm = &nhi_pm_ops,
+	.driver.pm = &tb_nhi_pm_ops,
 };
 
 static int __init nhi_init(void)
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH v3 1/8] thunderbolt: Prefix NHI core functions with tb_ and tb_ring_

Sashiko has finished reviewing this 8-patch series:

Sashiko has finished reviewing this 8-patch series:

- Patches 1-4, 6, and 7: no issues found.
- Patch 5 ("thunderbolt: Add platform NHI driver"): 1 potential issue (1 Low)
- Patch 8 ("thunderbolt: Drop WARN_ON() in tb_disconnect_pci()"): 1 potential issue (1 Low)

Detailed reviews have been sent as separate replies to patches 5 and 8.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
#3 lore raw

PATCH v3 2/8 thunderbolt: Move ICM mailbox functions to icm.c and internalize interrupt work

In reply to Chunfeng Yun

In preparation for modularizing PCIe NHI and ICM support as a separate driver, clean up internal helper functions so that they do not need to be exported:

nhi_mailbox_cmd() and nhi_mailbox_mode() are only used by the Intel firmware Connection Manager (icm.c) to exchange mailbox commands with firmware running on Intel PCIe NHIs. The Thunderbolt core framework, software connection manager, and non-PCIe platform controllers do not use this mailbox interface. Move both functions into icm.c and mark them static.

nhi_interrupt_work was previously initialized in pci.c within nhi_pci_init_msi(), requiring it to be visible across files. Move INIT_WORK(&nhi->interrupt_work, ...) into tb_nhi_probe() so that the workqueue item is initialized by the NHI core and nhi_interrupt_work() can be marked static in nhi.c.

---
v3: fix a indent

---
 drivers/thunderbolt/icm.c | 59 ++++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/nhi.c | 64 ++-------------------------------------
 drivers/thunderbolt/nhi.h |  3 --
 drivers/thunderbolt/pci.c |  2 --
 4 files changed, 62 insertions(+), 66 deletions(-)
drivers/thunderbolt/icm.c +59 -0
index a295a5e6cc9c..736f551cd567 100644
@@ -45,6 +45,7 @@
 #define ICM_RETRIES			3
 #define ICM_APPROVE_TIMEOUT		10000	/* ms */
 #define ICM_MAX_LINK			4
+#define ICM_MAILBOX_TIMEOUT		500 /* ms */
 
 static bool start_icm;
 module_param(start_icm, bool, 0444);
@@ -269,6 +270,64 @@ static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
 	return pci2cio_wait_completion(icm, 5000);
 }
 
+/**
+ * nhi_mailbox_cmd() - Send a command through NHI mailbox
+ * @nhi: Pointer to the NHI structure
+ * @cmd: Command to send
+ * @data: Data to be send with the command
+ *
+ * Sends mailbox command to the firmware running on NHI.
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+static int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
+{
+	ktime_t timeout;
+	u32 val;
+
+	iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
+
+	val = ioread32(nhi->iobase + REG_INMAIL_CMD);
+	val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
+	val |= REG_INMAIL_OP_REQUEST | cmd;
+	iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
+
+	timeout = ktime_add_ms(ktime_get(), ICM_MAILBOX_TIMEOUT);
+	do {
+		val = ioread32(nhi->iobase + REG_INMAIL_CMD);
+		if (!(val & REG_INMAIL_OP_REQUEST))
+			break;
+		usleep_range(10, 20);
+	} while (ktime_before(ktime_get(), timeout));
+
+	if (val & REG_INMAIL_OP_REQUEST)
+		return -ETIMEDOUT;
+	if (val & REG_INMAIL_ERROR)
+		return -EIO;
+
+	return 0;
+}
+
+/**
+ * nhi_mailbox_mode() - Return current firmware operation mode
+ * @nhi: Pointer to the NHI structure
+ *
+ * The function reads current firmware operation mode using NHI mailbox
+ * registers and returns it to the caller.
+ *
+ * Return: &enum nhi_fw_mode.
+ */
+static enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
+{
+	u32 val;
+
+	val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
+	val &= REG_OUTMAIL_CMD_OPMODE_MASK;
+	val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
+
+	return (enum nhi_fw_mode)val;
+}
+
 static bool icm_match(const struct tb_cfg_request *req,
 		      const struct ctl_pkg *pkg)
 {
drivers/thunderbolt/nhi.c +3 -61

82 lines not on this page · raw message

drivers/thunderbolt/nhi.h +0 -3
index a36c62c49a5c..3dd6ace6150d 100644
@@ -27,11 +27,8 @@ enum nhi_mailbox_cmd {
 	NHI_MAILBOX_ALLOW_ALL_DEVS = 0x23,
 };
 
-int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data);
-enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi);
 void nhi_enable_int_throttling(struct tb_nhi *nhi);
 void nhi_disable_interrupts(struct tb_nhi *nhi);
-void nhi_interrupt_work(struct work_struct *work);
 irqreturn_t tb_nhi_isr(int irq, void *data);
 irqreturn_t tb_ring_msix(int irq, void *data);
 int tb_nhi_probe(struct tb_nhi *nhi);
drivers/thunderbolt/pci.c +0 -2
index 2f0abf4d27a3..275389903090 100644
@@ -206,8 +206,6 @@ static int nhi_pci_init_msi(struct tb_nhi *nhi)
 		if (nvec < 0)
 			return nvec;
 
-		INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
-
 		irq = pci_irq_vector(pdev, 0);
 		if (irq < 0)
 			return irq;
Signature · 1 line
2.45.2
#4 lore raw

PATCH v3 3/8 thunderbolt: Make PCIe NHI support opt-in

In reply to Chunfeng Yun

From: Konrad Dybcio <[email protected]>

Currently, the USB4/Thunderbolt subsystem directly depends on PCI, tightly bundling the core framework with the PCIe-attached Native Host Interface (NHI) driver (pci.c) and the Intel Connection Manager (icm.c) into a single module (thunderbolt.o).

With support for platform/SoC-attached USB4 host controllers coming in (such as on ARM64 systems), the core USB4 framework should no longer have a hard dependency on PCI. Furthermore, embedded or SoC systems without PCIe-attached host routers should be able to omit PCIe NHI support to reduce kernel footprint.

Decouple the PCIe NHI and ICM drivers from the core framework:

1. Kconfig & Makefile:
- Remove "depends on PCI" from CONFIG_USB4.
- Introduce CONFIG_USB4_PCI to build PCIe NHI and ICM support as a separate module (thunderbolt_pci.ko).

2. Driver Registration:
- Make the core thunderbolt module standalone by converting tb_domain_init/exit() to postcore_initcall() and module_exit().
- Register the PCIe driver in thunderbolt_pci via module_pci_driver().

3. Connection Manager Selection:
- Add an optional .select_cm() hook to struct tb_nhi_ops so that the PCIe driver can invoke icm_probe() for legacy/firmware-CM controllers without hardcoding ICM calls into the core nhi.c.

4. Symbol Exports:
- Export necessary APIs required by pci and platform drivers.

---
v3:
Added 'default USB4' to CONFIG_USB4_PCI in Kconfig; Exported tb_xdomain_remove symbol immediately after its definition;

v2:
cherry-pick the patch of Konrad to fix the issues of this series, and also do some changes:

* Renamed CONFIG_USB4_PCIE and thunderbolt_pcie.ko to CONFIG_USB4_PCI and thunderbolt_pci.ko.
* Updated exported NHI symbols to standard tb_ / tb_ring_ prefixes (e.g., ring_msix → tb_ring_msix, nhi_msi → tb_nhi_isr).
* Eliminated exports for nhi_mailbox_* and nhi_interrupt_work by keeping them internal/static (handled by preparatory patches in v2).
* Added missing trailing semicolon to module_pci_driver(nhi_driver);.
* Expanded the commit message;
* Fix cherry-pick conflict caused by .reset_interface();

---
 drivers/thunderbolt/Kconfig   | 12 ++++++++++-
 drivers/thunderbolt/Makefile  |  7 +++++--
 drivers/thunderbolt/ctl.c     |  6 +++++-
 drivers/thunderbolt/domain.c  | 14 +++++++++++--
 drivers/thunderbolt/nhi.c     | 14 ++++++-------
 drivers/thunderbolt/nhi.h     |  2 ++
 drivers/thunderbolt/pci.c     | 39 +++++++++++++----------------------
 drivers/thunderbolt/switch.c  |  9 ++++++++
 drivers/thunderbolt/tb.h      |  2 --
 drivers/thunderbolt/tunnel.c  |  1 +
 drivers/thunderbolt/xdomain.c |  5 +++++
 11 files changed, 71 insertions(+), 40 deletions(-)
drivers/thunderbolt/Kconfig +11 -1
index 294b3227a545..80960555a364 100644
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 menuconfig USB4
 	tristate "Unified support for USB4 and Thunderbolt"
-	depends on PCI
 	select APPLE_PROPERTIES if EFI_STUB && X86
 	select CRC32
 	select CRYPTO_LIB_SHA256
@@ -18,6 +17,17 @@ menuconfig USB4
 
 if USB4
 
+config USB4_PCI
+	tristate "Support for PCIe-attached USB4 and Thunderbolt controllers"
+	depends on PCI
+	default USB4
+	help
+	  Say Y here to add support for the USB4 and Thunderbolt host
+	  routers attached over the PCIe bus, commonly found on x86 PCs.
+
+	  To compile this driver a module, choose M here. The module will be
+	  called thunderbolt_pci.
+
 config USB4_CONFIGFS
 	def_tristate USB4
 	depends on CONFIGFS_FS && !(USB4=y && CONFIGFS_FS=m)
drivers/thunderbolt/Makefile +5 -2
index beb054c3126b..a563bf2b0345 100644
@@ -1,10 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-only
 ccflags-y := -I$(src)
 obj-${CONFIG_USB4} := thunderbolt.o
-thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o pci.o path.o tunnel.o eeprom.o
-thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o lc.o tmu.o usb4.o
+thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel.o eeprom.o
+thunderbolt-objs += domain.o dma_port.o property.o xdomain.o lc.o tmu.o usb4.o
 thunderbolt-objs += usb4_port.o nvm.o retimer.o quirks.o clx.o
 
+obj-${CONFIG_USB4_PCI} += thunderbolt_pci.o
+thunderbolt_pci-y := pci.o icm.o
+
 thunderbolt-${CONFIG_ACPI} += acpi.o
 thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
 thunderbolt-$(CONFIG_USB4_CONFIGFS) += configfs.o
drivers/thunderbolt/ctl.c +5 -1
index 965988b18608..96717a0a94b1 100644
@@ -9,7 +9,6 @@
 #include <linux/crc32.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
-#include <linux/pci.h>
 #include <linux/dmapool.h>
 #include <linux/workqueue.h>
 
@@ -96,6 +95,7 @@ struct tb_cfg_request *tb_cfg_request_alloc(void)
 
 	return req;
 }
+EXPORT_SYMBOL_GPL(tb_cfg_request_alloc);
 
 /**
  * tb_cfg_request_get() - Increase refcount of a request
@@ -128,6 +128,7 @@ void tb_cfg_request_put(struct tb_cfg_request *req)
 	kref_put(&req->kref, tb_cfg_request_destroy);
 	mutex_unlock(&tb_cfg_request_lock);
 }
+EXPORT_SYMBOL_GPL(tb_cfg_request_put);
 
 static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
 				  struct tb_cfg_request *req)
@@ -575,6 +576,7 @@ int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
 	tb_cfg_request_dequeue(req);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(tb_cfg_request);
 
 /**
  * tb_cfg_request_cancel() - Cancel a control request
@@ -634,6 +636,7 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
 
 	return req->result;
 }
+EXPORT_SYMBOL_GPL(tb_cfg_request_sync);
 
 /* public interface, alloc/start/stop/free */
 
@@ -1009,6 +1012,7 @@ struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
 		memcpy(buffer, &reply.data, 4 * length);
 	return res;
 }
+EXPORT_SYMBOL_GPL(tb_cfg_read_raw);
 
 /**
  * tb_cfg_write_raw() - write from buffer into config space
drivers/thunderbolt/domain.c +12 -2
index 24611f05b3cd..114ddd5be25b 100644
@@ -313,6 +313,7 @@ const struct bus_type tb_bus_type = {
 	.remove = tb_service_remove,
 	.shutdown = tb_service_shutdown,
 };
+EXPORT_SYMBOL_GPL(tb_bus_type);
 
 static void tb_domain_release(struct device *dev)
 {
@@ -421,6 +422,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_domain_alloc);
 
 /**
  * tb_domain_add() - Add domain to the system
@@ -514,6 +516,7 @@ void tb_domain_remove(struct tb *tb)
 
 	device_unregister(&tb->dev);
 }
+EXPORT_SYMBOL_GPL(tb_domain_remove);
 
 /**
  * tb_domain_suspend_noirq() - Suspend a domain
@@ -880,8 +883,9 @@ int tb_domain_unregister_unplugged_xdomains(struct tb *tb)
 
 	return ctx.n;
 }
+EXPORT_SYMBOL_GPL(tb_domain_unregister_unplugged_xdomains);
 
-int tb_domain_init(void)
+static int __init tb_domain_init(void)
 {
 	int ret;
 
@@ -907,7 +911,7 @@ int tb_domain_init(void)
 	return ret;
 }
 
-void tb_domain_exit(void)
+static void __exit tb_domain_exit(void)
 {
 	bus_unregister(&tb_bus_type);
 	ida_destroy(&tb_domain_ida);
@@ -917,3 +921,9 @@ void tb_domain_exit(void)
 	tb_debugfs_exit();
 	tb_configfs_exit();
 }
+
+postcore_initcall(tb_domain_init);
+module_exit(tb_domain_exit);
+
+MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
+MODULE_LICENSE("GPL");
drivers/thunderbolt/nhi.c +7 -7
index ce02f96187b0..69783e81dd3b 100644
@@ -546,6 +546,7 @@ irqreturn_t tb_ring_msix(int irq, void *data)
 
 	return IRQ_HANDLED;
 }
+EXPORT_SYMBOL_GPL(tb_ring_msix);
 
 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
 {
@@ -1018,6 +1019,7 @@ irqreturn_t tb_nhi_isr(int irq, void *data)
 	schedule_work(&nhi->interrupt_work);
 	return IRQ_HANDLED;
 }
+EXPORT_SYMBOL_GPL(tb_nhi_isr);
 
 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
 {
@@ -1175,6 +1177,7 @@ void tb_nhi_shutdown(struct tb_nhi *nhi)
 	if (nhi->ops->shutdown)
 		nhi->ops->shutdown(nhi);
 }
+EXPORT_SYMBOL_GPL(tb_nhi_shutdown);
 
 static void nhi_reset(struct tb_nhi *nhi)
 {
@@ -1216,13 +1219,8 @@ static struct tb *nhi_select_cm(struct tb_nhi *nhi)
 	 * USB4 case is simple. If we got control of any of the
 	 * capabilities, we use software CM.
 	 */
-	if (!tb_acpi_is_native()) {
-		/*
-		 * Either firmware based CM is running (we did not get
-		 * control from the firmware) or this is pre-USB4 PC so
-		 * try first firmware CM and then fallback to software CM.
-		 */
-		tb = icm_probe(nhi);
+	if (!tb_acpi_is_native() && nhi->ops->select_cm) {
+		tb = nhi->ops->select_cm(nhi);
 		if (tb)
 			return tb;
 	}
@@ -1327,6 +1325,7 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(tb_nhi_probe);
 
 /*
  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
@@ -1349,3 +1348,4 @@ const struct dev_pm_ops tb_nhi_pm_ops = {
 	.runtime_suspend = nhi_runtime_suspend,
 	.runtime_resume = nhi_runtime_resume,
 };
+EXPORT_SYMBOL_GPL(tb_nhi_pm_ops);
drivers/thunderbolt/nhi.h +2 -0
index 3dd6ace6150d..65f9daf36068 100644
@@ -50,6 +50,7 @@ extern const struct dev_pm_ops tb_nhi_pm_ops;
  * @release_ring_irq: NHI specific interrupt release hook
  * @is_present: Whether the device is currently present on the parent bus
  * @init_interrupts: NHI specific interrupt initialization hook
+ * @select_cm: optional hook to select a custom CM
  */
 struct tb_nhi_ops {
 	int (*init)(struct tb_nhi *nhi);
@@ -65,6 +66,7 @@ struct tb_nhi_ops {
 	void (*release_ring_irq)(struct tb_ring *ring);
 	bool (*is_present)(struct tb_nhi *nhi);
 	int (*init_interrupts)(struct tb_nhi *nhi);
+	struct tb * (*select_cm)(struct tb_nhi *nhi);
 };
 
 /*
drivers/thunderbolt/pci.c +14 -25
index 275389903090..9379c598a221 100644
@@ -325,6 +325,16 @@ static bool nhi_pci_is_present(struct tb_nhi *nhi)
 	return pci_device_is_present(to_pci_dev(nhi->dev));
 }
 
+static struct tb *nhi_pci_select_cm(struct tb_nhi *nhi)
+{
+	/*
+	 * Either the firmware-based CM is running (we did not get
+	 * control from the firmware) or this is a pre-USB4 PC, so
+	 * first try using the firmware CM and then fallback to software CM.
+	 */
+	return icm_probe(nhi);
+}
+
 static const struct tb_nhi_ops pci_nhi_default_ops = {
 	.add_links = nhi_pci_add_links,
 	.pre_nvm_auth = nhi_pci_start_dma_port,
@@ -334,6 +344,7 @@ static const struct tb_nhi_ops pci_nhi_default_ops = {
 	.shutdown = nhi_pci_release_irq,
 	.is_present = nhi_pci_is_present,
 	.init_interrupts = nhi_pci_init_msi,
+	.select_cm = nhi_pci_select_cm,
 };
 
 /* Ice Lake specific NHI operations */
@@ -552,6 +563,7 @@ static const struct tb_nhi_ops icl_nhi_ops = {
 	.release_ring_irq = nhi_pci_ring_release_msix,
 	.is_present = nhi_pci_is_present,
 	.init_interrupts = nhi_pci_init_msi,
+	.select_cm = nhi_pci_select_cm,
 };
 
 static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -713,7 +725,7 @@ static struct pci_device_id nhi_ids[] = {
 };
 
 MODULE_DEVICE_TABLE(pci, nhi_ids);
-MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
+MODULE_DESCRIPTION("PCIe-attached Thunderbolt/USB4 host interface driver");
 MODULE_LICENSE("GPL");
 
 static struct pci_driver nhi_driver = {
@@ -724,27 +736,4 @@ static struct pci_driver nhi_driver = {
 	.shutdown = nhi_pci_shutdown,
 	.driver.pm = &tb_nhi_pm_ops,
 };
-
-static int __init nhi_init(void)
-{
-	int ret;
-
-	ret = tb_domain_init();
-	if (ret)
-		return ret;
-
-	ret = pci_register_driver(&nhi_driver);
-	if (ret)
-		tb_domain_exit();
-
-	return ret;
-}
-
-static void __exit nhi_unload(void)
-{
-	pci_unregister_driver(&nhi_driver);
-	tb_domain_exit();
-}
-
-rootfs_initcall(nhi_init);
-module_exit(nhi_unload);
+module_pci_driver(nhi_driver);
drivers/thunderbolt/switch.c +9 -0
index d22d8db8f890..7ff3da79da76 100644
@@ -2396,6 +2396,7 @@ const struct device_type tb_switch_type = {
 	.uevent = tb_switch_uevent,
 	.pm = &tb_switch_pm_ops,
 };
+EXPORT_SYMBOL_GPL(tb_switch_type);
 
 static int tb_switch_get_generation(struct tb_switch *sw)
 {
@@ -2571,6 +2572,7 @@ struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
 
 	return ERR_PTR(ret);
 }
+EXPORT_SYMBOL_GPL(tb_switch_alloc);
 
 /**
  * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
@@ -2610,6 +2612,7 @@ tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
 
 	return sw;
 }
+EXPORT_SYMBOL_GPL(tb_switch_alloc_safe_mode);
 
 /**
  * tb_switch_configure() - Uploads configuration to the switch
@@ -3438,6 +3441,7 @@ int tb_switch_add(struct tb_switch *sw)
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(tb_switch_add);
 
 /**
  * tb_switch_remove() - Remove and release a switch
@@ -3483,6 +3487,7 @@ void tb_switch_remove(struct tb_switch *sw)
 		dev_info(&sw->dev, "device disconnected\n");
 	device_unregister(&sw->dev);
 }
+EXPORT_SYMBOL_GPL(tb_switch_remove);
 
 /**
  * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
@@ -3828,6 +3833,7 @@ struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_switch_find_by_link_depth);
 
 /**
  * tb_switch_find_by_uuid() - Find switch by UUID
@@ -3854,6 +3860,7 @@ struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_switch_find_by_uuid);
 
 /**
  * tb_switch_find_by_route() - Find switch by route string
@@ -3883,6 +3890,7 @@ struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_switch_find_by_route);
 
 /**
  * tb_switch_find_port() - return the first port of @type on @sw or NULL
@@ -3903,6 +3911,7 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw,
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_switch_find_port);
 
 /*
  * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
drivers/thunderbolt/tb.h +0 -2
index e86845762e22..6dfdc47e05d2 100644
@@ -768,8 +768,6 @@ extern const struct device_type tb_retimer_type;
 extern const struct device_type tb_switch_type;
 extern const struct device_type usb4_port_device_type;
 
-int tb_domain_init(void);
-void tb_domain_exit(void);
 int tb_xdomain_init(void);
 void tb_xdomain_exit(void);
 
drivers/thunderbolt/tunnel.c +1 -0
index 2b911372001c..c92f181f181d 100644
@@ -274,6 +274,7 @@ void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event,
 	kfree(envp[1]);
 	kfree(envp[0]);
 }
+EXPORT_SYMBOL_GPL(tb_tunnel_event);
 
 static inline void tb_tunnel_set_active(struct tb_tunnel *tunnel, bool active)
 {
drivers/thunderbolt/xdomain.c +5 -0
index b187a60663f5..8ebe6cc18f60 100644
@@ -86,6 +86,7 @@ bool tb_is_xdomain_enabled(void)
 {
 	return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed();
 }
+EXPORT_SYMBOL_GPL(tb_is_xdomain_enabled);
 
 static bool tb_xdomain_match(const struct tb_cfg_request *req,
 			     const struct ctl_pkg *pkg)
@@ -2196,6 +2197,7 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_xdomain_alloc);
 
 /**
  * tb_xdomain_add() - Add XDomain to the bus
@@ -2211,6 +2213,7 @@ void tb_xdomain_add(struct tb_xdomain *xd)
 	/* Start exchanging properties with the other host */
 	start_handshake(xd);
 }
+EXPORT_SYMBOL_GPL(tb_xdomain_add);
 
 static int unregister_service(struct device *dev, void *data)
 {
@@ -2252,6 +2255,7 @@ void tb_xdomain_remove(struct tb_xdomain *xd)
 		put_device(&xd->dev);
 	}
 }
+EXPORT_SYMBOL_GPL(tb_xdomain_remove);
 
 /**
  * tb_xdomain_unregister() - Unregister XDomain
@@ -2593,6 +2597,7 @@ struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
 	xd = switch_find_xdomain(tb->root_switch, &lookup);
 	return tb_xdomain_get(xd);
 }
+EXPORT_SYMBOL_GPL(tb_xdomain_find_by_link_depth);
 
 /**
  * tb_xdomain_find_by_route() - Find an XDomain by route string
Signature · 1 line
2.45.2
#5 lore raw

PATCH v3 4/8 thunderbolt: Fix error handling and unwind paths in tb_nhi_probe()

In reply to Chunfeng Yun

Currently, tb_nhi_probe() has several issues in its error unwind paths:

1. Incomplete cleanup on failure:
If an error occurs after interrupts are initialized, such as in dma_set_mask_and_coherent(), tb_nhi_ops.init(), or nhi_select_cm(), tb_nhi_probe() immediately returns without calling tb_nhi_shutdown(). This leaves resources uncleaned:
- Hardware interrupt registers remain unmasked.
- Any scheduled nhi->interrupt_work is not flushed before the driver core frees the tb_nhi structure, risking a use-after-free.
- For Ice Lake PCIe controllers, icl_nhi_ops.init powers on the controller, but icl_nhi_shutdown() is never called to turn force-power back off, leaking power.

2. Fragile initialization order:
dma_set_mask_and_coherent() and spin_lock_init(&nhi->lock) are called after tb_nhi_ops.init_interrupts(). If a hardware interrupt fires immediately upon registration, tb_nhi_isr() schedules interrupt_work, which acquires &nhi->lock before it is initialized. Furthermore, if DMA mask configuration fails, interrupts have already been allocated and must be torn down.

Improve the probe sequence and error handling:
- Move dma_set_mask_and_coherent() and spin_lock_init() before tb_nhi_ops.init_interrupts(). If DMA mask setup fails, exit early without needing any interrupt teardown.
- Consolidate post-interrupt error paths by tb_nhi_shutdown(nhi).

---
new patch

---
 drivers/thunderbolt/nhi.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/thunderbolt/nhi.c +21 -13
index 69783e81dd3b..f687b37aac91 100644
@@ -1256,6 +1256,10 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->ops->init_interrupts)
 		return dev_err_probe(dev, -EINVAL, "missing required NHI ops\n");
 
+	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
+	if (res)
+		return dev_err_probe(dev, res, "failed to set DMA mask\n");
+
 	nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff;
 	dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
 
@@ -1269,6 +1273,8 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->tx_rings || !nhi->rx_rings || !nhi->interrupt_mask)
 		return -ENOMEM;
 
+	spin_lock_init(&nhi->lock);
+
 	INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
 
 	nhi_reset(nhi);
@@ -1280,24 +1286,22 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	if (res)
 		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
 
-	spin_lock_init(&nhi->lock);
-
-	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
-	if (res)
-		return dev_err_probe(dev, res, "failed to set DMA mask\n");
-
 	if (nhi->ops->init) {
 		res = nhi->ops->init(nhi);
-		if (res)
-			return dev_err_probe(dev, res, "NHI specific init failed\n");
+		if (res) {
+			dev_err_probe(dev, res, "NHI specific init failed\n");
+			goto err;
+		}
 	}
 
 	init_completion(&nhi->domain_released);
 
 	tb = nhi_select_cm(nhi);
-	if (!tb)
-		return dev_err_probe(dev, -ENODEV,
-			"failed to determine connection manager, aborting\n");
+	if (!tb) {
+		res = dev_err_probe(dev, -ENODEV,
+				    "failed to determine connection manager, aborting\n");
+		goto err;
+	}
 
 	dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
 
@@ -1311,8 +1315,8 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 		 */
 		tb_domain_put(tb);
 		wait_for_completion(&nhi->domain_released);
-		tb_nhi_shutdown(nhi);
-		return dev_err_probe(dev, res, "failed to add domain\n");
+		dev_err_probe(dev, res, "failed to add domain\n");
+		goto err;
 	}
 	dev_set_drvdata(dev, tb);
 
@@ -1324,6 +1328,10 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	pm_runtime_put_autosuspend(dev);
 
 	return 0;
+
+err:
+	tb_nhi_shutdown(nhi);
+	return res;
 }
 EXPORT_SYMBOL_GPL(tb_nhi_probe);
 
Signature · 1 line
2.45.2
#6 lore raw

PATCH v3 5/8 thunderbolt: Add platform NHI driver

In reply to Chunfeng Yun

Historically, the Thunderbolt/USB4 Native Host Interface (NHI) controller has been exposed as a PCI device handled by pci.c. However, some SoCs (such as MediaTek mt8901/mt8992) integrate the NHI as a memory-mapped platform device described via ACPI.

To support platform-attached NHI controllers:

Introduce platform.c to register a platform driver matching "ACPI0015" (ACPI), implement platform tb_nhi_ops hooks for PM, interrupts related;

Support an optional dedicated wake interrupt: on integrated SoCs, the MAC/PHY reside in a power domain whose clocks and power can be gated during sleep, preventing the primary NHI interrupt from acting as a wakeup source; so add an optional dedicated wakeup interrupt which comes from the always-on power domain; it is implementation specific.

Unlike the PCI bus core (pci_pm_suspend()), the platform bus does not automatically runtime-resume a runtime-suspended device prior to calling system suspend callbacks. Call pm_runtime_resume() in nhi_suspend() to ensure the controller are in an active state before nhi_suspend_noirq() stops them.

Clean up the unused nhi_enable_int_throttling() prototype.

---
v3 changes:
* Added 'depends on ACPI' to CONFIG_USB4_PLATFORM.
* Drop OF matching.
* Runtime PM Fix in Suspend.
* Cleaner Wake IRQ Handling.
* Kernel Coding Cleanups.

v2 changes:
* Added CONFIG_USB4_PLATFORM to build the platform driver as a separate module (thunderbolt_platform.ko), instead of statically embedding it into thunderbolt.o.
* Renamed plat.c to platform.c, renamed internal functions from nhi_plat_* to nhi_platform_*, and updated the Device Tree compatible string from "thunderbolt-platform" to "usb4-nhi-platform".
* Removed domain lifecycle changes (domain.c) and PCI driver cleanups (pci.c) from this patch, handled by previous patches.
* Squashed the optional dedicated wake IRQ support (previously a separate patch in v1) directly into this patch.
* Replaced the custom local ISR with the shared tb_nhi_isr(), adopted the renamed tb_nhi_probe() / tb_nhi_shutdown() / tb_nhi_pm_ops interfaces, and added -EPROBE_DEFER handling for the wake IRQ.

---
 drivers/thunderbolt/Kconfig    |  10 ++
 drivers/thunderbolt/Makefile   |   3 +
 drivers/thunderbolt/nhi.c      |   8 ++
 drivers/thunderbolt/nhi.h      |   1 -
 drivers/thunderbolt/platform.c | 170 +++++++++++++++++++++++++++++++++
 include/linux/thunderbolt.h    |   4 +
 6 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 drivers/thunderbolt/platform.c
drivers/thunderbolt/Kconfig +10 -0
index 80960555a364..ff4b1d4a0c19 100644
@@ -28,6 +28,16 @@ config USB4_PCI
 	  To compile this driver a module, choose M here. The module will be
 	  called thunderbolt_pci.
 
+config USB4_PLATFORM
+	tristate "Support for platform USB4 and Thunderbolt controllers"
+	depends on ACPI
+	help
+	  Say Y here to add support the USB4 and Thunderbolt host routers
+	  enumerated via ACPI (such as devices matching ACPI0015).
+
+	  To compile this driver a module, choose M here. The module will be
+	  called thunderbolt_platform.
+
 config USB4_CONFIGFS
 	def_tristate USB4
 	depends on CONFIGFS_FS && !(USB4=y && CONFIGFS_FS=m)
drivers/thunderbolt/Makefile +3 -0
index a563bf2b0345..334fa4ebdfc8 100644
@@ -8,6 +8,9 @@ thunderbolt-objs += usb4_port.o nvm.o retimer.o quirks.o clx.o
 obj-${CONFIG_USB4_PCI} += thunderbolt_pci.o
 thunderbolt_pci-y := pci.o icm.o
 
+obj-${CONFIG_USB4_PLATFORM} += thunderbolt_platform.o
+thunderbolt_platform-y := platform.o
+
 thunderbolt-${CONFIG_ACPI} += acpi.o
 thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
 thunderbolt-$(CONFIG_USB4_CONFIGFS) += configfs.o
drivers/thunderbolt/nhi.c +8 -0
index f687b37aac91..e083493c50e0 100644
@@ -1107,6 +1107,14 @@ static int nhi_suspend(struct device *dev)
 {
 	struct tb *tb = dev_get_drvdata(dev);
 
+	/*
+	 * Unlike the PCI bus, the platform bus does not automatically
+	 * runtime-resume the device before invoking system suspend callbacks.
+	 * Ensure the device is runtime-active before suspending the domain.
+	 */
+	if (pm_runtime_suspended(dev))
+		pm_runtime_resume(dev);
+
 	return tb_domain_suspend(tb);
 }
 
drivers/thunderbolt/nhi.h +0 -1
index 65f9daf36068..ef5d24d81251 100644
@@ -27,7 +27,6 @@ enum nhi_mailbox_cmd {
 	NHI_MAILBOX_ALLOW_ALL_DEVS = 0x23,
 };
 
-void nhi_enable_int_throttling(struct tb_nhi *nhi);
 void nhi_disable_interrupts(struct tb_nhi *nhi);
 irqreturn_t tb_nhi_isr(int irq, void *data);
 irqreturn_t tb_ring_msix(int irq, void *data);
drivers/thunderbolt/platform.c new file +170 -0

170 lines not on this page · raw message

include/linux/thunderbolt.h +4 -0
index 57502da29080..72497acac254 100644
@@ -511,6 +511,8 @@ void tb_service_properties_changed(struct tb_service *svc);
  * @going_away: The host controller device is about to disappear so when
  *		this flag is set, avoid touching the hardware anymore.
  * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
+ * @irq: interrupt number of NHI.
+ * @wake_irq: optional dedicated wakeup interrupt.
  * @interrupt_work: Work scheduled to handle ring interrupt when no
  *		    MSI-X is used.
  * @hop_count: Number of rings (end point hops) supported by NHI.
@@ -532,6 +534,8 @@ struct tb_nhi {
 	u32 *interrupt_mask;
 	bool going_away;
 	bool iommu_dma_protection;
+	int irq;
+	int wake_irq;
 	struct work_struct interrupt_work;
 	u32 hop_count;
 	unsigned long quirks;
Signature · 1 line
2.45.2
#7 lore raw

Re: [PATCH v3 5/8] thunderbolt: Add platform NHI driver

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] thunderbolt: invalid SPDX license identifier GPL in platform.c --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: invalid SPDX license identifier GPL in platform.c --

commit 69e07d737f3ccaa08c0332e46cb3614ab51f29be
Author: Chunfeng Yun <[email protected]>

thunderbolt: Add platform NHI driver

This commit adds a platform driver for Thunderbolt/USB4 Native Host Interface controllers enumerated via ACPI (ACPI0015), along with support for an optional dedicated wakeup interrupt.

drivers/thunderbolt/platform.c line 1

Quoted 7 lines
drivers/thunderbolt/platform.c new file +1 -0
new file mode 100644
index 0000000000000..05dd858ac8342
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL

[Severity: Low]
Should this SPDX-License-Identifier be GPL-2.0-only (or GPL-2.0) instead of GPL? Bare "GPL" is a MODULE_LICENSE() string rather than a valid SPDX license identifier in LICENSES/preferred/GPL-2.0, so scripts/spdxcheck.py and scripts/checkpatch.pl will reject it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#8 lore raw

Re: [PATCH v3 5/8] thunderbolt: Add platform NHI driver

Quoted 25 lines · On 一, 2026-11-23 at 11:54 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: invalid SPDX license identifier GPL in platform.c
--

commit 69e07d737f3ccaa08c0332e46cb3614ab51f29be
Author: Chunfeng Yun <[email protected]>

thunderbolt: Add platform NHI driver

This commit adds a platform driver for Thunderbolt/USB4 Native Host Interface controllers enumerated via ACPI (ACPI0015), along with support
for an optional dedicated wakeup interrupt.

drivers/thunderbolt/platform.c line 1

Quoted 8 lines
drivers/thunderbolt/platform.c new file +1 -0
new file mode 100644
index 0000000000000..05dd858ac8342
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL
Quoted 9 lines

[Severity: Low]
Should this SPDX-License-Identifier be GPL-2.0-only (or GPL-2.0) instead
of GPL?  Bare "GPL" is a MODULE_LICENSE() string rather than a valid SPDX
license identifier in LICENSES/preferred/GPL-2.0, so scripts/spdxcheck.py
and scripts/checkpatch.pl will reject it.

Hi Mika,

do you prefer to use which one? the checkpatch.pl prefer GPL according to the patch:
bf7fbeeae6db module: Cure the MODULE_LICENSE "GPL" vs. "GPL v2" bogosity

Thanks

maintainer
#9 lore raw

Re: [PATCH v3 5/8] thunderbolt: Add platform NHI driver

Hi,

Quoted 35 lines · Chunfeng Yun (云春峰)
Quoted 25 lines · On 一, 2026-11-23 at 11:54 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: invalid SPDX license identifier GPL in platform.c
--

commit 69e07d737f3ccaa08c0332e46cb3614ab51f29be
Author: Chunfeng Yun <[email protected]>

thunderbolt: Add platform NHI driver

This commit adds a platform driver for Thunderbolt/USB4 Native Host Interface controllers enumerated via ACPI (ACPI0015), along with support
for an optional dedicated wakeup interrupt.

drivers/thunderbolt/platform.c line 1

Quoted 8 lines
drivers/thunderbolt/platform.c new file +1 -0
new file mode 100644
index 0000000000000..05dd858ac8342
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL
Quoted 9 lines

[Severity: Low]
Should this SPDX-License-Identifier be GPL-2.0-only (or GPL-2.0) instead
of GPL?  Bare "GPL" is a MODULE_LICENSE() string rather than a valid SPDX
license identifier in LICENSES/preferred/GPL-2.0, so scripts/spdxcheck.py
and scripts/checkpatch.pl will reject it.

Quoted 6 lines

Hi Mika,

do you prefer to use which one? the checkpatch.pl prefer GPL according to the patch:
bf7fbeeae6db module: Cure the MODULE_LICENSE "GPL" vs. "GPL v2" bogosity

I prefer what we have now:

// SPDX-License-Identifier: GPL-2.0

#10 lore raw

PATCH v3 6/8 thunderbolt: Increase the lane adapter link-up timeout value

In reply to Chunfeng Yun

when plug into a USB3 device into USB4 hub's downstream facing port to trigger USB4 driver wakeup, encounter the lane link up timeout issue, then cause USB3 hub disconnect:

thunderbolt NVDA8100:00: 0:1: failed to reach state TB_PORT_UP. Ignoring port... thunderbolt NVDA8100:00: 0:1: lost during suspend, disconnecting thunderbolt NVDA8100:00: 0:4 <-> 1:16 (USB3): deactivating thunderbolt NVDA8100:00: deactivating USB3 Down path from 0:4 to 1:16 thunderbolt NVDA8100:00: deactivating USB3 Up path from 1:16 to 0:4 thunderbolt NVDA8100:00: 0: TMU: mode change off -> bi-directional, HiFi requested thunderbolt NVDA8100:00: 0: TMU: mode set to: bi-directional, HiFi thunderbolt NVDA8100:00: 0:5: DP IN resource available after hotplug thunderbolt NVDA8100:00: looking for DP IN <-> DP OUT pairs:
...
thunderbolt 0-0:1.1: retimer disconnected
thunderbolt 0-1:1.1: retimer disconnected
thunderbolt 0-1: device disconnected
usb 2-1: USB disconnect, device number 5

Completes link training at ~1.48s (with two retimer) and transitions to TB_PORT_UP after change the timeout to 2s;

In the USB4 spec, Phase 5 (Link Equalization) alone is allowed up to 1000 ms. When add tPhase2~4 (200 ms), and retimer transaction delays (tCmdResponse = 50 ms), the physical link is allowed by spec to take 1200 ms to 1500 ms+ to complete initialization.

---
v3: update commit message and kernel-doc
v2: change timeout from 2.5s to 2s

---
 drivers/thunderbolt/switch.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/thunderbolt/switch.c +3 -3
index 7ff3da79da76..97e3fd8cdc0c 100644
@@ -486,7 +486,7 @@ int tb_port_state(struct tb_port *port)
  * @port: Port to wait
  * @wait_if_unplugged: Wait also when port is unplugged
  *
- * Wait up to 1 second for a port to reach state TB_PORT_UP. If
+ * Wait up to 2 seconds for a port to reach state TB_PORT_UP. If
  * wait_if_unplugged is set then we also wait if the port is in state
  * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
  * switch resume). Otherwise we only wait if a device is registered but the link
@@ -494,13 +494,13 @@ int tb_port_state(struct tb_port *port)
  *
  * Return:
  * * %0 - If the port is not connected or failed to reach
- *   state %TB_PORT_UP within one second.
+ *   state %TB_PORT_UP within two seconds.
  * * %1 - If the port is connected and in state %TB_PORT_UP.
  * * Negative errno - An error occurred.
  */
 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 {
-	int retries = 10;
+	int retries = 20;
 	int state;
 	if (!port->cap_phy) {
 		tb_port_WARN(port, "does not have PHY\n");
Signature · 1 line
2.45.2
#11 lore raw

PATCH v3 7/8 thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP

In reply to Chunfeng Yun

When USB4 is woke up by device disconnect, no need wait for 2s, usually the phase2~4 takes at most about 200ms, and enters to TB_PORT_CONNECTING, if it is still in TB_PORT_UNPLUGGED after 400ms, treat it as disconnected.

---
v3: fix code style

---
 drivers/thunderbolt/switch.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thunderbolt/switch.c +5 -3
index 97e3fd8cdc0c..0da5b7ee4c29 100644
@@ -489,8 +489,9 @@ int tb_port_state(struct tb_port *port)
  * Wait up to 2 seconds for a port to reach state TB_PORT_UP. If
  * wait_if_unplugged is set then we also wait if the port is in state
  * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
- * switch resume). Otherwise we only wait if a device is registered but the link
- * has not yet been established.
+ * router resume, Wait at most 400 ms for link to start connecting,
+ * if timeout, means the device is disconnected). Otherwise we only wait
+ * if a device is registered but the link has not yet been established.
  *
  * Return:
  * * %0 - If the port is not connected or failed to reach
@@ -500,6 +501,7 @@ int tb_port_state(struct tb_port *port)
  */
 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 {
+	int unplugged_retries = 4;
 	int retries = 20;
 	int state;
 	if (!port->cap_phy) {
@@ -519,7 +521,7 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 			return 0;
 
 		case TB_PORT_UNPLUGGED:
-			if (wait_if_unplugged) {
+			if (wait_if_unplugged && unplugged_retries--) {
 				/* used during resume */
 				tb_port_dbg(port,
 					    "is unplugged (state: 7), retrying...\n");
Signature · 1 line
2.45.2
#12 lore raw

PATCH v3 8/8 thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

In reply to Chunfeng Yun

tb_disconnect_pci() uses WARN_ON() if the upstream PCIe adapter port or the associated PCIe tunnel cannot be found. This triggers a full kernel stack trace in dmesg.

However, tb_disconnect_pci() is invoked when userspace deauthorizes a router via sysfs (writing 0 to 'authorized'). Under dynamic runtime conditions, an upstream port or active tunnel may not exist. For example:
- If a USB4 router fails to resume or is unplugged during system/link sleep, the disconnect flow may tear down and free the PCIe tunnel before userspace deauthorization runs.
- If PCIe tunnel activation previously failed (e.g., due to link training or Type-C handshake hiccups), the tunnel was never established in the first place.

it seems unnecessary due to it does not indicate a kernel bug. Furthermore, it causes unnecessary kernel panics on systems booted with panic_on_warn.

Since -ENODEV is already returned to notify callers, simply drop the WARN_ON() checks while keeping the -ENODEV return.

---
v3:
* modify commit message.
* Reworked sw->is_unplugged checks to directly drop the WARN_ON(!up) and WARN_ON(!tunnel) checks in tb_disconnect_pci()

v2: no changes

---
 drivers/thunderbolt/tb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/thunderbolt/tb.c +2 -2
index 57c11ba13459..5f674c305d43 100644
@@ -2267,11 +2267,11 @@ static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
 	struct tb_port *up;
 
 	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
-	if (WARN_ON(!up))
+	if (!up)
 		return -ENODEV;
 
 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
-	if (WARN_ON(!tunnel))
+	if (!tunnel)
 		return -ENODEV;
 
 	tb_switch_xhci_disconnect(sw);
Signature · 1 line
2.45.2
#13 lore raw

Re: [PATCH v3 8/8] thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] thunderbolt: missing Fixes trailer for tb_disconnect_pci() WARN_ON() fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: missing Fixes trailer for tb_disconnect_pci() WARN_ON() fix
--

commit f6a685f6d3f79343c1ba3c57db3636a5afc5f46e
Author: Chunfeng Yun <[email protected]>

Quoted 17 lines

thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

tb_disconnect_pci() uses WARN_ON() if the upstream PCIe adapter port or the associated PCIe tunnel cannot be found. This triggers a full kernel stack trace in dmesg.

However, tb_disconnect_pci() is invoked when userspace deauthorizes a router via sysfs (writing 0 to 'authorized'). Under dynamic runtime conditions, an upstream port or active tunnel may not exist. For example:
- If a USB4 router fails to resume or is unplugged during system/link sleep, the disconnect flow may tear down and free the PCIe tunnel before userspace deauthorization runs.
- If PCIe tunnel activation previously failed (e.g., due to link training or Type-C handshake hiccups), the tunnel was never established in the first place.

Quoted 8 lines

it seems unnecessary due to it does not indicate a kernel bug. Furthermore, it causes unnecessary kernel panics on systems booted with panic_on_warn.

Since -ENODEV is already returned to notify callers, simply drop the WARN_ON() checks while keeping the -ENODEV return.

[Severity: Low]
This fixes spurious warnings in tb_disconnect_pci() when deauthorizing a router without an upstream PCIe port or active tunnel. Should this include:

Fixes 3da88be24997 ("thunderbolt: Add support for de-authorizing devices")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#14 lore raw

Re: [PATCH v3 8/8] thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

Quoted 39 lines · On 一, 2026-11-23 at 11:42 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: missing Fixes trailer for tb_disconnect_pci() WARN_ON() fix
--

commit f6a685f6d3f79343c1ba3c57db3636a5afc5f46e
Author: Chunfeng Yun <[email protected]>

Quoted 24 lines

thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

tb_disconnect_pci() uses WARN_ON() if the upstream PCIe adapter port
or the associated PCIe tunnel cannot be found. This triggers a full kernel stack trace in dmesg.

However, tb_disconnect_pci() is invoked when userspace deauthorizes a router via sysfs (writing 0 to 'authorized'). Under dynamic runtime
conditions, an upstream port or active tunnel may not exist. For example:
- If a USB4 router fails to resume or is unplugged during system/link
  sleep, the disconnect flow may tear down and free the PCIe tunnel before userspace deauthorization runs.
- If PCIe tunnel activation previously failed (e.g., due to link training or Type-C handshake hiccups), the tunnel was never established in the first place.

it seems unnecessary due to it does not indicate a kernel bug. Furthermore, it causes unnecessary kernel panics on systems booted with panic_on_warn.

Quoted 5 lines

Since -ENODEV is already returned to notify callers, simply drop the
WARN_ON() checks while keeping the -ENODEV return.

Quoted 9 lines

[Severity: Low]
This fixes spurious warnings in tb_disconnect_pci() when deauthorizing
a router without an upstream PCIe port or active tunnel.  Should this include:

Fixes 3da88be24997 ("thunderbolt: Add support for de-authorizing

devices")

will add it in commit message

Thanks