Linux USB
 help / color / mirror / Atom feed
* [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI
@ 2026-12-05  8:33 Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
                   ` (14 more replies)
  0 siblings, 15 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

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 and NHI directly as a memory-mapped platform device described
via ACPI ("ACPI0015"). Furthermore, embedded and SoC-based platforms
often lack PCIe-attached host routers and do not require Intel ICM
support, making a mandatory PCI dependency and bundled PCIe code
undesirable.

This series:
1. Fixes an initialization race and error unwind issue in nhi_probe().
2. Refactors and encapsulates internal subsystem interfaces (interrupt
   handling, router device type checks, bus walks, and teardown).
3. Decouples PCIe NHI and Intel ICM support from the core framework into
   an opt-in module (thunderbolt_pci.ko).
4. Makes struct tb_nhi_ops optional for simple drivers.
5. Introduces a standalone platform NHI driver (thunderbolt_platform.ko).
6. Improves link training timeouts and disconnect handling under dynamic
   runtime conditions.

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

1. Standalone Core Bugfix (Patch 1)
   - Fixes initialization order and error unwind paths in nhi_probe().
     Initializes &nhi->lock and configures the DMA mask before enabling
     interrupts to prevent race conditions with early interrupts, and
     consolidates post-interrupt failure paths to invoke nhi_shutdown().

2. Subsystem Interface Encapsulation (Patches 2-8)
   In preparation for modularizing PCIe NHI support, several internal
   abstractions are cleaned up to avoid leaking core internals across
   module boundaries:
   - Patch 2 renames core ring and NHI symbols to use standard tb_/tb_ring_
     prefixes.
   - Patch 3 internalizes non-MSI-X interrupt request and work handling
     into nhi.c, allowing nhi_isr(), nhi_interrupt_work(), and
     nhi_disable_interrupts() to become static, and drops the
     .init_interrupts callback from struct tb_nhi_ops.
   - Patch 4 encapsulates router type checking within switch.c by moving
     tb_to_switch() out-of-line and introducing tb_switch_has_children(),
     keeping struct device_type tb_switch_type private to switch.c.
   - Patch 5 encapsulates the bus iteration for runtime resume completion
     into domain.c as tb_domain_complete_rpm(), keeping tb_bus_type
     private to domain.c.
   - Patch 6 consolidates domain and NHI teardown into tb_nhi_remove(),
     making nhi_shutdown() static and eliminating the need to export
     tb_domain_remove() and nhi_shutdown().
   - Patch 7 moves Intel ICM mailbox helpers (nhi_mailbox_cmd/mode) into
     icm.c as static functions.
   - Patch 8 decouples ICM probing from the NHI core by introducing an
     optional .select_cm() hook in struct tb_nhi_ops.

3. Modularizing PCIe NHI & Making Ops Optional (Patches 9-10)
   - Patch 9 drops the hard "depends on PCI" from CONFIG_USB4, adds
     CONFIG_USB4_PCI (defaulting to USB4) to build PCIe NHI and ICM as
     thunderbolt_pci.ko, and exports the minimal set of remaining APIs.
     Thanks to patches 2-8, internal structures like tb_bus_type and
     tb_switch_type do not need to be exported.
   - Patch 10 makes struct tb_nhi_ops completely optional by introducing
     the TB_NHI_HAS_OP() helper macro and dropping the mandatory
     "if (!nhi->ops)" check in tb_nhi_probe(), allowing platform drivers
     to operate without any ops table.

4. Platform NHI Driver (Patch 11)
   - Introduces CONFIG_USB4_PLATFORM (thunderbolt_platform.ko), adding
     support for memory-mapped USB4 NHIs via ACPI ("ACPI0015").
   - Supports an optional dedicated wakeup interrupt from an always-on
     power domain (registered via dev_pm_set_dedicated_wake_irq()),
     necessary for SoCs where the primary NHI power domain is gated
     during system sleep.

5. Link Training and Disconnect Improvements (Patches 12-14)
   - Patch 12 increases the port link training timeout in
     tb_wait_for_port() from 1s to 2s to accommodate Phase 5 equalization
     and retimer propagation delays across multi-retimer topologies
     during wakeups.
   - Patch 13 decouples TB_PORT_UNPLUGGED wait time from the overall 2s
     link-up timeout, capping it to 400ms so genuine disconnects are
     handled promptly.
   - Patch 14 drops an unnecessary WARN_ON() in tb_disconnect_pci() when
     userspace deauthorizes a router after a tunnel was already torn down.

Differences since v3
====================
- Re-ordered the nhi_probe() error handling and race condition fix to
  Patch 1 as a standalone bugfix with a Fixes: tag.
- Decomposed the previous monolithic modularization patch into discrete
  preparatory encapsulation patches (Patches 2-8):
  * Internalized primary interrupt request and work into nhi.c.
  * Encapsulated router type checks (tb_to_switch / tb_switch_has_children)
    in switch.c.
  * Encapsulated rpm_complete bus walk in domain.c.
  * Consolidated teardown into tb_nhi_remove().
  * Decoupled ICM mailbox helpers and probe logic.
- Avoided exporting tb_bus_type, tb_switch_type, tb_domain_remove, and
  nhi_shutdown in the PCIe modularization patch.
- Added TB_NHI_HAS_OP() and made struct tb_nhi_ops optional (Patch 10).
- In the platform driver (Patch 11):
  * Wrapped wake_irq in a local driver structure (struct tb_nhi_platform)
    instead of modifying include/linux/thunderbolt.h.
  * Corrected wake IRQ registration order by setting wakeup capability
    before dev_pm_set_dedicated_wake_irq().
  * Added cleanup for sysfs wakeup nodes on error and device removal.

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 (13):
  thunderbolt: Fix error handling and unwind paths in nhi_probe()
  thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_
  thunderbolt: Internalize non-MSI-X interrupt request and work in NHI
    core
  thunderbolt: Encapsulate router device type checks in switch.c
  thunderbolt: Move rpm_complete bus walk to domain.c
  thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()
  thunderbolt: Move ICM mailbox functions to icm.c
  thunderbolt: Decouple ICM probe from NHI core via .select_cm hook
  thunderbolt: Make nhi->ops optional
  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   |  37 +++++-
 drivers/thunderbolt/icm.c      |  76 ++++++++++---
 drivers/thunderbolt/nhi.c      | 200 +++++++++++++++------------------
 drivers/thunderbolt/nhi.h      |  20 ++--
 drivers/thunderbolt/pci.c      | 124 +++++++-------------
 drivers/thunderbolt/platform.c | 155 +++++++++++++++++++++++++
 drivers/thunderbolt/switch.c   |  63 +++++++++--
 drivers/thunderbolt/tb.c       |   4 +-
 drivers/thunderbolt/tb.h       |  12 +-
 drivers/thunderbolt/tunnel.c   |   1 +
 drivers/thunderbolt/xdomain.c  |   5 +
 include/linux/thunderbolt.h    |   2 +
 15 files changed, 490 insertions(+), 247 deletions(-)
 create mode 100644 drivers/thunderbolt/platform.c

-- 
2.45.2


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe()
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:50   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, 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
nhi->ops->init() or nhi_select_cm(), nhi_probe() immediately
returns without calling 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 nhi->ops->init_interrupts(). If a hardware interrupt fires
immediately upon registration, nhi_msi() 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
  nhi->ops->init_interrupts(). If DMA mask setup fails, exit early
  before interrupts are enabled.
- Handle post-interrupt error paths by nhi_shutdown().

Fixes: e241d98e04ef ("thunderbolt: Separate out common NHI bits")

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: no changes, but move the patch earlier in the series
v3: new patch
---
 drivers/thunderbolt/nhi.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index e99a3fcc4a29..d998d3caad75 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -1318,6 +1318,10 @@ int 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);
 
@@ -1331,6 +1335,8 @@ int nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->tx_rings || !nhi->rx_rings || !nhi->interrupt_mask)
 		return -ENOMEM;
 
+	spin_lock_init(&nhi->lock);
+
 	nhi_reset(nhi);
 
 	/* In case someone left them on. */
@@ -1340,24 +1346,22 @@ int 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");
 
@@ -1371,8 +1375,8 @@ int nhi_probe(struct tb_nhi *nhi)
 		 */
 		tb_domain_put(tb);
 		wait_for_completion(&nhi->domain_released);
-		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);
 
@@ -1384,6 +1388,10 @@ int nhi_probe(struct tb_nhi *nhi)
 	pm_runtime_put_autosuspend(dev);
 
 	return 0;
+
+err:
+	nhi_shutdown(nhi);
+	return res;
 }
 
 /*
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

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() 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).

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

- ring_msix       -> tb_ring_msix
- nhi_probe       -> tb_nhi_probe
- nhi_pm_ops      -> tb_nhi_pm_ops

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: Dropped renaming of nhi_msi and nhi_shutdown

v3: no changes
v2: new patch
---
 drivers/thunderbolt/nhi.c | 8 ++++----
 drivers/thunderbolt/nhi.h | 6 +++---
 drivers/thunderbolt/pci.c | 6 +++---
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index d998d3caad75..01311ad1f693 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -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);
@@ -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;
@@ -1399,7 +1399,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,  /*
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 88af9ab9fde7..594a38b4225d 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -33,10 +33,10 @@ 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);
+irqreturn_t tb_ring_msix(int irq, void *data);
+int tb_nhi_probe(struct tb_nhi *nhi);
 void nhi_shutdown(struct tb_nhi *nhi);
-extern const struct dev_pm_ops nhi_pm_ops;
+extern const struct dev_pm_ops tb_nhi_pm_ops;
 
 /**
  * struct tb_nhi_ops - NHI specific optional operations
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 4408229d8ac7..9cdfadf8b1b7 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -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)
@@ -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)
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:51   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, the primary interrupt request and work initialization are
handled inside the backend PCIe driver (pci.c), requiring nhi_msi(),
nhi_interrupt_work(), and nhi_disable_interrupts() to be exposed in nhi.h,
and requiring struct tb_nhi_ops to provide an .init_interrupts hook.

Since the interrupt work and ISR only interact with NHI core rings and
locks, they can be made entirely private to nhi.c. Backend drivers only
need to discover and provide the IRQ number in struct tb_nhi.

Move the non-MSI-X interrupt handling into the NHI core, drop the
.init_interrupts callback from struct tb_nhi_ops, and clean up the
declarations in nhi.h.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/nhi.c   | 31 ++++++++++++++++-------
 drivers/thunderbolt/nhi.h   |  6 -----
 drivers/thunderbolt/pci.c   | 50 ++++++++++++++-----------------------
 include/linux/thunderbolt.h |  2 ++
 4 files changed, 43 insertions(+), 46 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 01311ad1f693..16562a8cd4a7 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -177,7 +177,7 @@ static void ring_interrupt_active(struct tb_ring *ring, bool active)
  *
  * Use only during init and shutdown.
  */
-void nhi_disable_interrupts(struct tb_nhi *nhi)
+static void nhi_disable_interrupts(struct tb_nhi *nhi)
 {
 	int i = 0;
 	/* disable interrupts */
@@ -1022,7 +1022,7 @@ enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
 	return (enum nhi_fw_mode)val;
 }
 
-void nhi_interrupt_work(struct work_struct *work)
+static void nhi_interrupt_work(struct work_struct *work)
 {
 	struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
 	int value = 0; /* Suppress uninitialized usage warning. */
@@ -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)
+static irqreturn_t nhi_isr(int irq, void *data)
 {
 	struct tb_nhi *nhi = data;
 	schedule_work(&nhi->interrupt_work);
@@ -1232,6 +1232,15 @@ void nhi_shutdown(struct tb_nhi *nhi)
 	}
 	nhi_disable_interrupts(nhi);
 
+	/*
+	 * We have to release the irq before calling flush_work. Otherwise an
+	 * already executing IRQ handler could call schedule_work again.
+	 */
+	if (nhi->irq > 0) {
+		devm_free_irq(nhi->dev, nhi->irq, nhi);
+		flush_work(&nhi->interrupt_work);
+	}
+
 	if (nhi->ops->shutdown)
 		nhi->ops->shutdown(nhi);
 }
@@ -1315,9 +1324,6 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->ops)
 		return dev_err_probe(dev, -EINVAL, "NHI ops not set\n");
 
-	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");
@@ -1337,14 +1343,21 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 
 	spin_lock_init(&nhi->lock);
 
+	INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
+
 	nhi_reset(nhi);
 
 	/* In case someone left them on. */
 	nhi_disable_interrupts(nhi);
 
-	res = nhi->ops->init_interrupts(nhi);
-	if (res)
-		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
+	if (nhi->irq > 0) {
+		res = devm_request_irq(dev, nhi->irq, nhi_isr, IRQF_NO_SUSPEND,
+				       dev_name(dev), nhi);
+		if (res) {
+			nhi->irq = 0;
+			goto err;
+		}
+	}
 
 	if (nhi->ops->init) {
 		res = nhi->ops->init(nhi);
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 594a38b4225d..24a52f5f3fca 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -29,10 +29,6 @@ enum nhi_mailbox_cmd {
 
 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 nhi_msi(int irq, void *data);
 irqreturn_t tb_ring_msix(int irq, void *data);
 int tb_nhi_probe(struct tb_nhi *nhi);
 void nhi_shutdown(struct tb_nhi *nhi);
@@ -52,7 +48,6 @@ extern const struct dev_pm_ops tb_nhi_pm_ops;
  * @request_ring_irq: NHI specific interrupt retrieval hook
  * @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
  */
 struct tb_nhi_ops {
 	int (*init)(struct tb_nhi *nhi);
@@ -67,7 +62,6 @@ struct tb_nhi_ops {
 	int (*request_ring_irq)(struct tb_ring *ring, bool no_suspend);
 	void (*release_ring_irq)(struct tb_ring *ring);
 	bool (*is_present)(struct tb_nhi *nhi);
-	int (*init_interrupts)(struct tb_nhi *nhi);
 };
 
 /*
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 9cdfadf8b1b7..11543bcb5634 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -185,11 +185,11 @@ static bool nhi_pci_add_links(struct tb_nhi *nhi)
 	return ret;
 }
 
-static int nhi_pci_init_msi(struct tb_nhi *nhi)
+static int nhi_pci_init_msi(struct tb_nhi_pci *nhi_pci)
 {
-	struct tb_nhi_pci *nhi_pci = nhi_to_pci(nhi);
+	struct tb_nhi *nhi = &nhi_pci->nhi;
 	struct pci_dev *pdev = to_pci_dev(nhi->dev);
-	int res, irq, nvec;
+	int irq, nvec;
 
 	ida_init(&nhi_pci->msix_ida);
 
@@ -206,21 +206,21 @@ 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;
 
-		res = devm_request_irq(&pdev->dev, irq, nhi_msi,
-				       IRQF_NO_SUSPEND, "thunderbolt", nhi);
-		if (res)
-			return res;
+		nhi->irq = irq;
 	}
 
 	return 0;
 }
 
+static void nhi_pci_release_msi(struct tb_nhi_pci *nhi_pci)
+{
+	ida_destroy(&nhi_pci->msix_ida);
+}
+
 static bool nhi_pci_imr_valid(struct pci_dev *pdev)
 {
 	u8 val;
@@ -306,22 +306,6 @@ static void nhi_pci_ring_release_msix(struct tb_ring *ring)
 	ring->irq = 0;
 }
 
-static void nhi_pci_release_irq(struct tb_nhi *nhi)
-{
-	struct tb_nhi_pci *nhi_pci = nhi_to_pci(nhi);
-	struct pci_dev *pdev = to_pci_dev(nhi->dev);
-
-	/*
-	 * We have to release the irq before calling flush_work. Otherwise an
-	 * already executing IRQ handler could call schedule_work again.
-	 */
-	if (!pdev->msix_enabled) {
-		devm_free_irq(nhi->dev, pdev->irq, nhi);
-		flush_work(&nhi->interrupt_work);
-	}
-	ida_destroy(&nhi_pci->msix_ida);
-}
-
 static bool nhi_pci_is_present(struct tb_nhi *nhi)
 {
 	return pci_device_is_present(to_pci_dev(nhi->dev));
@@ -333,9 +317,7 @@ static const struct tb_nhi_ops pci_nhi_default_ops = {
 	.post_nvm_auth = nhi_pci_complete_dma_port,
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
-	.shutdown = nhi_pci_release_irq,
 	.is_present = nhi_pci_is_present,
-	.init_interrupts = nhi_pci_init_msi,
 };
 
 /* Ice Lake specific NHI operations */
@@ -535,8 +517,6 @@ static bool icl_nhi_add_links(struct tb_nhi *nhi)
 
 static void icl_nhi_shutdown(struct tb_nhi *nhi)
 {
-	nhi_pci_release_irq(nhi);
-
 	icl_nhi_force_power(nhi, false);
 }
 
@@ -553,7 +533,6 @@ static const struct tb_nhi_ops icl_nhi_ops = {
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
 	.is_present = nhi_pci_is_present,
-	.init_interrupts = nhi_pci_init_msi,
 };
 
 static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -588,7 +567,15 @@ static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	pci_set_master(pdev);
 
-	return tb_nhi_probe(&nhi_pci->nhi);
+	res = nhi_pci_init_msi(nhi_pci);
+	if (res)
+		return dev_err_probe(dev, res, "cannot initialize msi, aborting\n");
+
+	res = tb_nhi_probe(nhi);
+	if (res)
+		nhi_pci_release_msi(nhi_pci);
+
+	return res;
 }
 
 static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
@@ -611,6 +598,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);
+	nhi_pci_release_msi(nhi_to_pci(nhi));
 }
 
 static void nhi_pci_remove(struct pci_dev *pdev)
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 57502da29080..412fcc6adcf7 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -513,6 +513,7 @@ void tb_service_properties_changed(struct tb_service *svc);
  * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
  * @interrupt_work: Work scheduled to handle ring interrupt when no
  *		    MSI-X is used.
+ * @irq: Interrupt number of NHI when MSI-X is not used.
  * @hop_count: Number of rings (end point hops) supported by NHI.
  * @quirks: NHI specific quirks if any
  * @domain_released: Completed when domain has been fully released
@@ -533,6 +534,7 @@ struct tb_nhi {
 	bool going_away;
 	bool iommu_dma_protection;
 	struct work_struct interrupt_work;
+	int irq;
 	u32 hop_count;
 	unsigned long quirks;
 	struct completion domain_released;
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (2 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:41   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, tb_is_switch() and tb_to_switch() are inline functions in
tb.h that directly inspect dev->type == &tb_switch_type. In addition,
pci.c has an open-coded child router lookup in
icl_nhi_is_device_connected() that calls tb_is_switch().

This directly exposes the internal struct device_type tb_switch_type to
outside files. In preparation for modularizing PCIe NHI and ICM support
into a separate module, this would require exporting the internal
tb_switch_type structure to avoid modpost undefined symbol errors.

Encapsulate router type inspection entirely within switch.c:
- Move tb_to_switch() out-of-line into switch.c.
- Move the child router check from pci.c to switch.c as
  tb_switch_has_children().

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/pci.c    | 17 +----------------
 drivers/thunderbolt/switch.c | 33 +++++++++++++++++++++++++++++++++
 drivers/thunderbolt/tb.h     |  9 ++-------
 3 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 11543bcb5634..c1f9ed93c364 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -324,21 +324,6 @@ static const struct tb_nhi_ops pci_nhi_default_ops = {
 
 #define ICL_LC_MAILBOX_TIMEOUT	500 /* ms */
 
-static int check_for_device(struct device *dev, void *data)
-{
-	return tb_is_switch(dev);
-}
-
-static bool icl_nhi_is_device_connected(struct tb_nhi *nhi)
-{
-	struct tb *tb = dev_get_drvdata(nhi->dev);
-	int ret;
-
-	ret = device_for_each_child(&tb->root_switch->dev, NULL,
-				    check_for_device);
-	return ret > 0;
-}
-
 static int icl_nhi_force_power(struct tb_nhi *nhi, bool power)
 {
 	struct pci_dev *pdev = to_pci_dev(nhi->dev);
@@ -435,7 +420,7 @@ static int icl_nhi_suspend(struct tb_nhi *nhi)
 	struct tb *tb = dev_get_drvdata(nhi->dev);
 	int ret;
 
-	if (icl_nhi_is_device_connected(nhi))
+	if (tb_switch_has_children(tb->root_switch))
 		return 0;
 
 	if (tb_switch_is_icm(tb->root_switch)) {
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index d22d8db8f890..0388c20c574a 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -2397,6 +2397,20 @@ const struct device_type tb_switch_type = {
 	.pm = &tb_switch_pm_ops,
 };
 
+/**
+ * tb_to_switch() - Return a router pointer from a device
+ * @dev: Device to check
+ *
+ * Return: Pointer to &struct tb_switch if @dev is a router, %NULL otherwise.
+ */
+struct tb_switch *tb_to_switch(const struct device *dev)
+{
+	if (tb_is_switch(dev))
+		return container_of(dev, struct tb_switch, dev);
+
+	return NULL;
+}
+
 static int tb_switch_get_generation(struct tb_switch *sw)
 {
 	if (tb_switch_is_usb4(sw))
@@ -3904,6 +3918,25 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw,
 	return NULL;
 }
 
+static int check_for_device(struct device *dev, void *data)
+{
+	return tb_is_switch(dev);
+}
+
+/**
+ * tb_switch_has_children() - Check if router has any child routers connected
+ * @sw: Router to check
+ *
+ * Return: %true if there is at least one child router connected to @sw.
+ */
+bool tb_switch_has_children(struct tb_switch *sw)
+{
+	int ret;
+
+	ret = device_for_each_child(&sw->dev, NULL, check_for_device);
+	return ret > 0;
+}
+
 /*
  * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
  * device. For now used only for Titan Ridge.
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index e86845762e22..9158b8f3580f 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -862,10 +862,12 @@ int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
 void tb_sw_set_unplugged(struct tb_switch *sw);
 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
 				    enum tb_port_type type);
+bool tb_switch_has_children(struct tb_switch *sw);
 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
 					       u8 depth);
 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
+struct tb_switch *tb_to_switch(const struct device *dev);
 
 /**
  * tb_switch_for_each_port() - Iterate over each switch port
@@ -895,13 +897,6 @@ static inline bool tb_is_switch(const struct device *dev)
 	return dev->type == &tb_switch_type;
 }
 
-static inline struct tb_switch *tb_to_switch(const struct device *dev)
-{
-	if (tb_is_switch(dev))
-		return container_of(dev, struct tb_switch, dev);
-	return NULL;
-}
-
 static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
 {
 	return tb_to_switch(sw->dev.parent);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (3 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:50   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, remove_unplugged_switch() in icm.c traverses all devices on
the bus using bus_for_each_dev(&tb_bus_type, ...) to signal rpm_complete
before calling tb_switch_remove().

This creates a direct dependency on the internal struct bus_type
tb_bus_type in icm.c. When icm.c is modularized, this would require
exporting tb_bus_type to other modules, leaking subsystem core internals.

Encapsulate this bus traversal inside domain.c as tb_domain_complete_rpm().
This keeps tb_bus_type private to domain.c and eliminates the need to
export the bus structure.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/domain.c | 24 ++++++++++++++++++++++++
 drivers/thunderbolt/icm.c    | 17 +----------------
 drivers/thunderbolt/tb.h     |  1 +
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 24611f05b3cd..075e0f8af1e8 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -881,6 +881,30 @@ int tb_domain_unregister_unplugged_xdomains(struct tb *tb)
 	return ctx.n;
 }
 
+static int complete_rpm(struct device *dev, void *data)
+{
+	struct tb_switch *sw = tb_to_switch(dev);
+
+	if (sw && sw->tb == data)
+		complete(&sw->rpm_complete);
+
+	return 0;
+}
+
+/**
+ * tb_domain_complete_rpm() - Signal runtime resume completion for router and below
+ * @tb: Domain the router belongs to
+ * @sw: Router to complete runtime resume
+ *
+ * Signal @sw and routers below for rpm_complete because
+ * tb_switch_remove() calls pm_runtime_get_sync() that then waits for it.
+ */
+void tb_domain_complete_rpm(struct tb *tb, struct tb_switch *sw)
+{
+	complete_rpm(&sw->dev, tb);
+	bus_for_each_dev(&tb_bus_type, &sw->dev, tb, complete_rpm);
+}
+
 int tb_domain_init(void)
 {
 	int ret;
diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index a295a5e6cc9c..f2c387d226f0 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -2068,28 +2068,13 @@ static void icm_unplug_children(struct tb_switch *sw)
 	}
 }
 
-static int complete_rpm(struct device *dev, void *data)
-{
-	struct tb_switch *sw = tb_to_switch(dev);
-
-	if (sw && sw->tb == data)
-		complete(&sw->rpm_complete);
-	return 0;
-}
-
 static void remove_unplugged_switch(struct tb_switch *sw)
 {
 	struct device *parent = get_device(sw->dev.parent);
 
 	pm_runtime_get_sync(parent);
 
-	/*
-	 * Signal this and switches below for rpm_complete because
-	 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
-	 * for it.
-	 */
-	complete_rpm(&sw->dev, sw->tb);
-	bus_for_each_dev(&tb_bus_type, &sw->dev, sw->tb, complete_rpm);
+	tb_domain_complete_rpm(sw->tb, sw);
 	tb_switch_remove(sw);
 
 	pm_runtime_mark_last_busy(parent);
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 9158b8f3580f..d6c188922ca1 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -797,6 +797,7 @@ int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 				       int receive_path, int receive_ring);
 int tb_domain_disconnect_all_paths(struct tb *tb);
 int tb_domain_unregister_unplugged_xdomains(struct tb *tb);
+void tb_domain_complete_rpm(struct tb *tb, struct tb_switch *sw);
 
 static inline struct tb *tb_domain_get(struct tb *tb)
 {
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (4 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:50   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, the remove and shutdown paths in pci.c manually do several
core teardown steps which breaks abstraction symmetry with tb_nhi_probe()
and exposes internal teardown primitives (tb_domain_remove, nhi_shutdown)
across file and module boundaries.

Consolidate this teardown sequence into a symmetric tb_nhi_remove()
helper in nhi.c, and make nhi_shutdown() static to nhi.c. Backend bus
drivers (PCI and platform) only need to handle their bus-specific power
management before calling tb_nhi_remove().

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/nhi.c | 31 ++++++++++++++++++++++++++++++-
 drivers/thunderbolt/nhi.h |  2 +-
 drivers/thunderbolt/pci.c | 16 +++-------------
 3 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 16562a8cd4a7..d55e1f03b68a 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -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)
+static void nhi_shutdown(struct tb_nhi *nhi)
 {
 	int i;
 
@@ -1407,6 +1407,35 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	return res;
 }
 
+/**
+ * tb_nhi_remove() - Remove and release NHI instance and its domain
+ * @nhi: Pointer to the NHI structure
+ * @reset: Whether to force a host router reset (e.g. on shutdown/reboot)
+ *
+ * Common teardown sequence across all bus types. Stops and unregisters
+ * the domain, waits for domain release, and shuts down the NHI hardware.
+ */
+void tb_nhi_remove(struct tb_nhi *nhi, bool reset)
+{
+	struct device *dev = nhi->dev;
+	struct tb *tb = dev_get_drvdata(dev);
+
+	/*
+	 * On system shutdown/reboot force a host router reset so the
+	 * connection manager asserts DPR on connected Thunderbolt 3 devices
+	 * before the router tree is removed (see tb_stop()).
+	 */
+	if (reset)
+		nhi->host_reset = true;
+
+	pm_runtime_get_sync(dev);
+	pm_runtime_dont_use_autosuspend(dev);
+
+	tb_domain_remove(tb);
+	wait_for_completion(&nhi->domain_released);
+	nhi_shutdown(nhi);
+}
+
 /*
  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 24a52f5f3fca..9aa167321cfe 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -31,7 +31,7 @@ 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);
 irqreturn_t tb_ring_msix(int irq, void *data);
 int tb_nhi_probe(struct tb_nhi *nhi);
-void nhi_shutdown(struct tb_nhi *nhi);
+void tb_nhi_remove(struct tb_nhi *nhi, bool reset);
 extern const struct dev_pm_ops tb_nhi_pm_ops;
 
 /**
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index c1f9ed93c364..4b985e1c4363 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -568,21 +568,11 @@ static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
 	struct tb *tb = pci_get_drvdata(pdev);
 	struct tb_nhi *nhi = tb->nhi;
 
-	/*
-	 * On system shutdown/reboot force a host router reset so the
-	 * connection manager asserts DPR on connected Thunderbolt 3 devices
-	 * before the router tree is removed (see tb_stop()).
-	 */
-	if (reset)
-		nhi->host_reset = true;
-
-	pm_runtime_get_sync(&pdev->dev);
-	pm_runtime_dont_use_autosuspend(&pdev->dev);
+	/* Restore PCI device to forbid state */
 	pm_runtime_forbid(&pdev->dev);
 
-	tb_domain_remove(tb);
-	wait_for_completion(&nhi->domain_released);
-	nhi_shutdown(nhi);
+	tb_nhi_remove(nhi, reset);
+
 	nhi_pci_release_msi(nhi_to_pci(nhi));
 }
 
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (5 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	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.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: drop &nhi->interrupt_work init which moved into the patch 03/14 in this series
v3: fix a indent
v2: new patch
---
 drivers/thunderbolt/icm.c | 59 ++++++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/nhi.c | 60 ---------------------------------------
 drivers/thunderbolt/nhi.h |  2 --
 3 files changed, 59 insertions(+), 62 deletions(-)

diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index f2c387d226f0..b01638b9f51d 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -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)
 {
diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index d55e1f03b68a..0b13ddf6c99d 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -35,8 +35,6 @@
  */
 #define RING_E2E_RESERVED_HOPID	RING_FIRST_USABLE_HOPID
 
-#define NHI_MAILBOX_TIMEOUT	500 /* ms */
-
 static bool host_reset = true;
 module_param(host_reset, bool, 0444);
 MODULE_PARM_DESC(host_reset, "reset USB4 host router (default: true)");
@@ -964,64 +962,6 @@ int tb_ring_throttling(struct tb_ring *ring, unsigned int interval_nsec)
 }
 EXPORT_SYMBOL_GPL(tb_ring_throttling);
 
-/**
- * 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.
- */
-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(), NHI_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.
- */
-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 void nhi_interrupt_work(struct work_struct *work)
 {
 	struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 9aa167321cfe..e3f19e63f2ba 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -27,8 +27,6 @@ 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);
 irqreturn_t tb_ring_msix(int irq, void *data);
 int tb_nhi_probe(struct tb_nhi *nhi);
 void tb_nhi_remove(struct tb_nhi *nhi, bool reset);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (6 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Currently, nhi_select_cm() in nhi.c directly invokes icm_probe() when
operating in non-native ACPI mode. This tightly couples the generic NHI
core with the Intel Connection Manager (ICM), which is specific to PCIe
controllers.

To allow the NHI core to support non-PCIe platform devices (which only
use software CM) and to cleanly decouple ICM from the core framework,
introduce an optional .select_cm() callback to struct tb_nhi_ops.

Move the icm_probe() call into pci.c as nhi_pci_select_cm(). Non-PCIe
drivers can omit this hook to automatically use the standard software
connection manager.

Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch, split from the patch 3/8 of v3 series
---
 drivers/thunderbolt/nhi.c |  9 ++-------
 drivers/thunderbolt/nhi.h |  2 ++
 drivers/thunderbolt/pci.c | 12 ++++++++++++
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 0b13ddf6c99d..384493d80839 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -1225,13 +1225,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;
 	}
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index e3f19e63f2ba..837bbc87c63f 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -46,6 +46,7 @@ extern const struct dev_pm_ops tb_nhi_pm_ops;
  * @request_ring_irq: NHI specific interrupt retrieval hook
  * @release_ring_irq: NHI specific interrupt release hook
  * @is_present: Whether the device is currently present on the parent bus
+ * @select_cm: Optional hook to select a custom CM
  */
 struct tb_nhi_ops {
 	int (*init)(struct tb_nhi *nhi);
@@ -60,6 +61,7 @@ struct tb_nhi_ops {
 	int (*request_ring_irq)(struct tb_ring *ring, bool no_suspend);
 	void (*release_ring_irq)(struct tb_ring *ring);
 	bool (*is_present)(struct tb_nhi *nhi);
+	struct tb *(*select_cm)(struct tb_nhi *nhi);
 };
 
 /*
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 4b985e1c4363..379677362b40 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -311,6 +311,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,
@@ -318,6 +328,7 @@ static const struct tb_nhi_ops pci_nhi_default_ops = {
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
 	.is_present = nhi_pci_is_present,
+	.select_cm = nhi_pci_select_cm,
 };
 
 /* Ice Lake specific NHI operations */
@@ -518,6 +529,7 @@ static const struct tb_nhi_ops icl_nhi_ops = {
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
 	.is_present = nhi_pci_is_present,
+	.select_cm = nhi_pci_select_cm,
 };
 
 static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (7 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:58   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

From: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>

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. Symbol Exports:
   - Export necessary APIs required by pci and platform drivers.

Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4:
* split .select_cm() change into new patch 08/14 in the series
* drop tb_bus_type, tb_switch_type, tb_domain_remove and nhi_shutdown exports
* add tb_domain_complete_rpm, tb_to_switch, tb_switch_has_children and tb_nhi_remove exports

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  | 13 +++++++++++--
 drivers/thunderbolt/nhi.c     |  4 ++++
 drivers/thunderbolt/pci.c     | 27 ++-------------------------
 drivers/thunderbolt/switch.c  | 10 ++++++++++
 drivers/thunderbolt/tb.h      |  2 --
 drivers/thunderbolt/tunnel.c  |  1 +
 drivers/thunderbolt/xdomain.c |  5 +++++
 10 files changed, 54 insertions(+), 33 deletions(-)

diff --git a/drivers/thunderbolt/Kconfig b/drivers/thunderbolt/Kconfig
index 294b3227a545..80960555a364 100644
--- a/drivers/thunderbolt/Kconfig
+++ b/drivers/thunderbolt/Kconfig
@@ -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)
diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
index beb054c3126b..a563bf2b0345 100644
--- a/drivers/thunderbolt/Makefile
+++ b/drivers/thunderbolt/Makefile
@@ -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
diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
index 965988b18608..96717a0a94b1 100644
--- a/drivers/thunderbolt/ctl.c
+++ b/drivers/thunderbolt/ctl.c
@@ -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
diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 075e0f8af1e8..6c01768a2d54 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -421,6 +421,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
@@ -880,6 +881,7 @@ int tb_domain_unregister_unplugged_xdomains(struct tb *tb)
 
 	return ctx.n;
 }
+EXPORT_SYMBOL_GPL(tb_domain_unregister_unplugged_xdomains);
 
 static int complete_rpm(struct device *dev, void *data)
 {
@@ -904,8 +906,9 @@ void tb_domain_complete_rpm(struct tb *tb, struct tb_switch *sw)
 	complete_rpm(&sw->dev, tb);
 	bus_for_each_dev(&tb_bus_type, &sw->dev, tb, complete_rpm);
 }
+EXPORT_SYMBOL_GPL(tb_domain_complete_rpm);
 
-int tb_domain_init(void)
+static int __init tb_domain_init(void)
 {
 	int ret;
 
@@ -931,7 +934,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);
@@ -941,3 +944,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");
diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 384493d80839..a2d5a5b13b9b 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -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)
 {
@@ -1341,6 +1342,7 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	nhi_shutdown(nhi);
 	return res;
 }
+EXPORT_SYMBOL_GPL(tb_nhi_probe);
 
 /**
  * tb_nhi_remove() - Remove and release NHI instance and its domain
@@ -1370,6 +1372,7 @@ void tb_nhi_remove(struct tb_nhi *nhi, bool reset)
 	wait_for_completion(&nhi->domain_released);
 	nhi_shutdown(nhi);
 }
+EXPORT_SYMBOL_GPL(tb_nhi_remove);
 
 /*
  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
@@ -1392,3 +1395,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);
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 379677362b40..7e68831df449 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -690,7 +690,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 = {
@@ -701,27 +701,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);
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 0388c20c574a..63ffc48738c9 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -2410,6 +2410,7 @@ struct tb_switch *tb_to_switch(const struct device *dev)
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_to_switch);
 
 static int tb_switch_get_generation(struct tb_switch *sw)
 {
@@ -2585,6 +2586,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
@@ -2624,6 +2626,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
@@ -3452,6 +3455,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
@@ -3497,6 +3501,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
@@ -3842,6 +3847,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
@@ -3868,6 +3874,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
@@ -3897,6 +3904,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
@@ -3917,6 +3925,7 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw,
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(tb_switch_find_port);
 
 static int check_for_device(struct device *dev, void *data)
 {
@@ -3936,6 +3945,7 @@ bool tb_switch_has_children(struct tb_switch *sw)
 	ret = device_for_each_child(&sw->dev, NULL, check_for_device);
 	return ret > 0;
 }
+EXPORT_SYMBOL_GPL(tb_switch_has_children);
 
 /*
  * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index d6c188922ca1..5516e0340610 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -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);
 
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 2b911372001c..c92f181f181d 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -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)
 {
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index b187a60663f5..8ebe6cc18f60 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -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
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 10/14] thunderbolt: Make nhi->ops optional
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (8 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

Commit dd60fb487e55 ("thunderbolt: Require nhi->ops be valid")
previously added a mandatory sanity-check in tb_nhi_probe() because
ops->init_interrupts() was required for all drivers.

Now that non-MSI-X interrupt handling has been internalized into
the NHI core, simple platform NHI drivers that use native software
Connection Manager may not need to supply any custom callbacks,
leaving nhi->ops as NULL. So make struct tb_nhi_ops optional again.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/nhi.c    | 27 ++++++++++++---------------
 drivers/thunderbolt/nhi.h    |  2 ++
 drivers/thunderbolt/switch.c |  6 +++---
 3 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index a2d5a5b13b9b..d8acc934a318 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -669,7 +669,7 @@ static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
 	if (!ring->descriptors)
 		goto err_free_ring;
 
-	if (!(flags & RING_FLAG_NO_INTERRUPT) && nhi->ops->request_ring_irq) {
+	if (!(flags & RING_FLAG_NO_INTERRUPT) && TB_NHI_HAS_OP(nhi, request_ring_irq)) {
 		if (nhi->ops->request_ring_irq(ring, flags & RING_FLAG_NO_SUSPEND))
 			goto err_free_descs;
 	}
@@ -680,7 +680,7 @@ static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
 	return ring;
 
 err_release_msix:
-	if (nhi->ops->release_ring_irq)
+	if (TB_NHI_HAS_OP(nhi, release_ring_irq))
 		nhi->ops->release_ring_irq(ring);
 err_free_descs:
 	dma_free_coherent(ring->nhi->dev,
@@ -918,7 +918,7 @@ void tb_ring_free(struct tb_ring *ring)
 	}
 	spin_unlock_irq(&ring->nhi->lock);
 
-	if (nhi->ops->release_ring_irq)
+	if (TB_NHI_HAS_OP(nhi, release_ring_irq))
 		nhi->ops->release_ring_irq(ring);
 
 	dma_free_coherent(ring->nhi->dev,
@@ -1030,7 +1030,7 @@ static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
 	if (ret)
 		return ret;
 
-	if (nhi->ops->suspend_noirq) {
+	if (TB_NHI_HAS_OP(nhi, suspend_noirq)) {
 		ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
 		if (ret)
 			return ret;
@@ -1091,9 +1091,9 @@ static int nhi_resume_noirq(struct device *dev)
 	 * unplugged last device which causes the host controller to go
 	 * away on PCs.
 	 */
-	if ((nhi->ops->is_present && !nhi->ops->is_present(nhi))) {
+	if (TB_NHI_HAS_OP(nhi, is_present) && !nhi->ops->is_present(nhi)) {
 		nhi->going_away = true;
-	} else if (nhi->ops->resume_noirq) {
+	} else if (TB_NHI_HAS_OP(nhi, resume_noirq)) {
 		ret = nhi->ops->resume_noirq(nhi);
 		if (ret)
 			return ret;
@@ -1134,7 +1134,7 @@ static int nhi_runtime_suspend(struct device *dev)
 	if (ret)
 		return ret;
 
-	if (nhi->ops->runtime_suspend) {
+	if (TB_NHI_HAS_OP(nhi, runtime_suspend)) {
 		ret = nhi->ops->runtime_suspend(tb->nhi);
 		if (ret)
 			return ret;
@@ -1148,7 +1148,7 @@ static int nhi_runtime_resume(struct device *dev)
 	struct tb_nhi *nhi = tb->nhi;
 	int ret;
 
-	if (nhi->ops->runtime_resume) {
+	if (TB_NHI_HAS_OP(nhi, runtime_resume)) {
 		ret = nhi->ops->runtime_resume(nhi);
 		if (ret)
 			return ret;
@@ -1182,7 +1182,7 @@ static void nhi_shutdown(struct tb_nhi *nhi)
 		flush_work(&nhi->interrupt_work);
 	}
 
-	if (nhi->ops->shutdown)
+	if (TB_NHI_HAS_OP(nhi, shutdown))
 		nhi->ops->shutdown(nhi);
 }
 
@@ -1226,7 +1226,7 @@ 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() && nhi->ops->select_cm) {
+	if (!tb_acpi_is_native() && TB_NHI_HAS_OP(nhi, select_cm)) {
 		tb = nhi->ops->select_cm(nhi);
 		if (tb)
 			return tb;
@@ -1236,7 +1236,7 @@ static struct tb *nhi_select_cm(struct tb_nhi *nhi)
 	if (!tb)
 		return NULL;
 
-	if (nhi->ops->add_links)
+	if (TB_NHI_HAS_OP(nhi, add_links))
 		linked = nhi->ops->add_links(nhi);
 	if (!linked)
 		linked = tb_acpi_add_links(nhi);
@@ -1257,9 +1257,6 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	struct tb *tb;
 	int res;
 
-	if (!nhi->ops)
-		return dev_err_probe(dev, -EINVAL, "NHI ops not set\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");
@@ -1295,7 +1292,7 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 		}
 	}
 
-	if (nhi->ops->init) {
+	if (TB_NHI_HAS_OP(nhi, init)) {
 		res = nhi->ops->init(nhi);
 		if (res) {
 			dev_err_probe(dev, res, "NHI specific init failed\n");
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 837bbc87c63f..a858d57b4d19 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -64,6 +64,8 @@ struct tb_nhi_ops {
 	struct tb *(*select_cm)(struct tb_nhi *nhi);
 };
 
+#define TB_NHI_HAS_OP(nhi, op)	((nhi)->ops && (nhi)->ops->op)
+
 /*
  * PCI IDs used in this driver from Win Ridge forward. There is no
  * need for the PCI quirk anymore as we will use ICM also on Apple
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 63ffc48738c9..760da4f5ebec 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -254,7 +254,7 @@ static int nvm_authenticate(struct tb_switch *sw, bool auth_only)
 
 	sw->nvm->authenticating = true;
 	if (!tb_route(sw)) {
-		if (nhi->ops->pre_nvm_auth)
+		if (TB_NHI_HAS_OP(nhi, pre_nvm_auth))
 			nhi->ops->pre_nvm_auth(nhi);
 		ret = nvm_authenticate_host_dma_port(sw);
 	} else {
@@ -2819,7 +2819,7 @@ static int tb_switch_add_dma_port(struct tb_switch *sw)
 	nvm_get_auth_status(sw, &status);
 	if (status) {
 		if (!tb_route(sw)) {
-			if (nhi->ops->post_nvm_auth)
+			if (TB_NHI_HAS_OP(nhi, post_nvm_auth))
 				nhi->ops->post_nvm_auth(nhi);
 		}
 		return 0;
@@ -2836,7 +2836,7 @@ static int tb_switch_add_dma_port(struct tb_switch *sw)
 
 	/* Now we can allow root port to suspend again */
 	if (!tb_route(sw)) {
-		if (nhi->ops->post_nvm_auth)
+		if (TB_NHI_HAS_OP(nhi, post_nvm_auth))
 			nhi->ops->post_nvm_auth(nhi);
 	}
 
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 11/14] thunderbolt: Add platform NHI driver
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (9 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:56   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	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). Because the primary interrupt is handled
generically by the NHI core, no custom struct tb_nhi_ops
hooks are required.

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.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: changes:
* Drop nhi_platform_default_ops
* Add struct tb_nhi_platform
* Drop primary irq register and handle wake irq
* Teardown via tb_nhi_remove
* Drop pm_runtime_resume in nhi_suspend

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/platform.c | 155 +++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+)
 create mode 100644 drivers/thunderbolt/platform.c

diff --git a/drivers/thunderbolt/Kconfig b/drivers/thunderbolt/Kconfig
index 80960555a364..2cf1997d4508 100644
--- a/drivers/thunderbolt/Kconfig
+++ b/drivers/thunderbolt/Kconfig
@@ -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 for the USB4 and Thunderbolt host routers
+	  enumerated via ACPI (such as devices matching ACPI0015).
+
+	  To compile this driver as 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)
diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
index a563bf2b0345..334fa4ebdfc8 100644
--- a/drivers/thunderbolt/Makefile
+++ b/drivers/thunderbolt/Makefile
@@ -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
diff --git a/drivers/thunderbolt/platform.c b/drivers/thunderbolt/platform.c
new file mode 100644
index 000000000000..ce3bb65543c1
--- /dev/null
+++ b/drivers/thunderbolt/platform.c
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Thunderbolt driver - NHI driver (Platform bus specific implementation)
+ *
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ */
+
+#include <linux/acpi.h>
+#include <linux/interrupt.h>
+#include <linux/errno.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/pm_wakeirq.h>
+#include <linux/slab.h>
+
+#include "nhi.h"
+#include "tb.h"
+
+/**
+ * struct tb_nhi_platform - NHI device connected over platform bus
+ * @nhi: Thunderbolt native host interface.
+ * @wake_irq: Optional dedicated wakeup interrupt.
+ */
+struct tb_nhi_platform {
+	struct tb_nhi nhi;
+	int wake_irq;
+};
+
+static inline struct tb_nhi_platform *nhi_to_platform(struct tb_nhi *nhi)
+{
+	return container_of(nhi, struct tb_nhi_platform, nhi);
+}
+
+static int nhi_platform_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tb_nhi_platform *nhi_plat;
+	struct tb_nhi *nhi;
+	int ret;
+
+	nhi_plat = devm_kzalloc_obj(dev, *nhi_plat);
+	if (!nhi_plat)
+		return -ENOMEM;
+
+	nhi = &nhi_plat->nhi;
+	nhi->dev = dev;
+
+	nhi->iobase = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(nhi->iobase))
+		return PTR_ERR(nhi->iobase);
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		return ret;
+
+	nhi->irq = ret;
+
+	/*
+	 * Since the wake interrupt is implementation specific, make it
+	 * optional.
+	 */
+	ret = platform_get_irq_optional(pdev, 1);
+	if (ret < 0 && ret != -ENXIO)
+		return dev_err_probe(dev, ret, "failed to get wake IRQ\n");
+	if (ret > 0)
+		nhi_plat->wake_irq = ret;
+
+	pm_runtime_set_active(dev);
+	pm_runtime_get_noresume(dev);
+	pm_runtime_enable(dev);
+
+	/* Enable system wakeup capability */
+	device_set_wakeup_capable(dev, true);
+
+	if (nhi_plat->wake_irq) {
+		ret = dev_pm_set_dedicated_wake_irq(dev, nhi_plat->wake_irq);
+		if (ret) {
+			dev_err_probe(dev, ret,
+				      "failed to set wake interrupt %d\n",
+				      nhi_plat->wake_irq);
+			goto wake_irq_err;
+		}
+	}
+
+	ret = tb_nhi_probe(nhi);
+	if (ret)
+		goto probe_err;
+
+	device_enable_async_suspend(dev);
+
+	return 0;
+
+probe_err:
+	if (nhi_plat->wake_irq)
+		dev_pm_clear_wake_irq(dev);
+
+wake_irq_err:
+	device_set_wakeup_capable(dev, false);
+	pm_runtime_put_noidle(dev);
+	pm_runtime_disable(dev);
+	pm_runtime_set_suspended(dev);
+	return ret;
+}
+
+static void nhi_platform_do_remove(struct platform_device *pdev, bool reset)
+{
+	struct tb *tb = platform_get_drvdata(pdev);
+	struct device *dev = &pdev->dev;
+	struct tb_nhi *nhi = tb->nhi;
+	struct tb_nhi_platform *nhi_plat = nhi_to_platform(nhi);
+
+	tb_nhi_remove(nhi, reset);
+
+	if (nhi_plat->wake_irq)
+		dev_pm_clear_wake_irq(dev);
+
+	device_set_wakeup_capable(dev, false);
+	pm_runtime_put_noidle(dev);
+	pm_runtime_disable(dev);
+	pm_runtime_set_suspended(dev);
+}
+
+static void nhi_platform_remove(struct platform_device *pdev)
+{
+	nhi_platform_do_remove(pdev, false);
+}
+
+static void nhi_platform_shutdown(struct platform_device *pdev)
+{
+	nhi_platform_do_remove(pdev, true);
+}
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id nhi_platform_acpi_match[] = {
+	{ "ACPI0015", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, nhi_platform_acpi_match);
+#endif
+
+static struct platform_driver nhi_platform_driver = {
+	.probe = nhi_platform_probe,
+	.remove = nhi_platform_remove,
+	.shutdown = nhi_platform_shutdown,
+	.driver = {
+		.name = "thunderbolt",
+		.acpi_match_table = ACPI_PTR(nhi_platform_acpi_match),
+		.pm = &tb_nhi_pm_ops,
+	},
+};
+
+module_platform_driver(nhi_platform_driver);
+MODULE_ALIAS("platform:thunderbolt");
+MODULE_DESCRIPTION("Thunderbolt/USB4 platform host interface driver");
+MODULE_LICENSE("GPL");
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (10 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	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.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: no changes
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(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 760da4f5ebec..e0bbdc5fb2f6 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -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");
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (11 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:56   ` sashiko-bot
  2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun
  2026-12-05  8:58 ` [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI sashiko-bot
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	Chunfeng Yun

When a USB4 router wakes up due to a device disconnect, there is no need
to wait the full 2 seconds in TB_PORT_UNPLUGGED.

Although the USB4 specification does not define an overall timeout for
Phases 2-4 (before the port transitions from CLd/TB_PORT_UNPLUGGED to
Training/TB_PORT_CONNECTING), empirical testing across various devices
shows that Phases 2-4 typically take at most ~200ms after router resume.

Wait at most 400ms (2x margin) for the port to leave TB_PORT_UNPLUGGED;
if it remains in TB_PORT_UNPLUGGED after that, treat it as disconnected
and return early.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: modify commit message
v3: fix code style
v2: new patch
---
 drivers/thunderbolt/switch.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index e0bbdc5fb2f6..f25c358181dd 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -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");
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci()
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (12 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
@ 2026-12-05  8:33 ` Chunfeng Yun
  2026-12-05  8:55   ` sashiko-bot
  2026-12-05  8:58 ` [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI sashiko-bot
  14 siblings, 1 reply; 25+ messages in thread
From: Chunfeng Yun @ 2026-12-05  8:33 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Andreas Noever, Konrad Dybcio, Sven Peter, Yehezkel Bernat,
	Matthias Brugger, Greg Kroah-Hartman, AngeloGioacchino Del Regno,
	linux-kernel, linux-usb, linux-arm-kernel, linux-mediatek,
	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.

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

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: Add Fixes in commit message

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(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 57c11ba13459..5f674c305d43 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -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);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c
  2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
@ 2026-12-05  8:41   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:41 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] thunderbolt: tb_is_switch() and tb_switch_type remain exposed in
  tb.h
--

commit 8daee24827ddbf3d888482160f876b8924b99c84
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Encapsulate router device type checks in switch.c

This commit moves tb_to_switch() out-of-line into switch.c and replaces
icl_nhi_is_device_connected() in pci.c with a new tb_switch_has_children()
helper in switch.c.

> diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
> index 8ed373e94b652..5a5ba688ef32f 100644
> --- a/drivers/thunderbolt/tb.h
> +++ b/drivers/thunderbolt/tb.h

[ ... ]

> @@ -897,13 +899,6 @@ static inline bool tb_is_switch(const struct device *dev)
>  	return dev->type == &tb_switch_type;
>  }
>  
> -static inline struct tb_switch *tb_to_switch(const struct device *dev)
> -{
> -	if (tb_is_switch(dev))
> -		return container_of(dev, struct tb_switch, dev);
> -	return NULL;
> -}

[Severity: Low]
Should tb_is_switch() and tb_switch_type also be encapsulated in switch.c?

The commit message states that router type inspection is encapsulated
entirely within switch.c, yet tb_is_switch() remains inline here and
tb_switch_type stays extern in tb.h and non-static in switch.c.

Any future tb_is_switch() caller in a separate module including tb.h
would hit a modpost undefined symbol error for tb_switch_type.

Comparing dev->type == &tb_switch_type directly in tb_to_switch() and
moving tb_is_switch() to switch.c (or calling tb_to_switch() from it)
would allow making tb_switch_type static and removing it from tb.h.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=4

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe()
  2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
@ 2026-12-05  8:50   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:50 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] thunderbolt: Fixes tag in nhi_probe() cites refactor commit
  e241d98e04ef
--

commit eee24f59504b2e86fb4bdbccff24c965b0e09126
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

> thunderbolt: Fix error handling and unwind paths in nhi_probe()
>
> Currently, 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
> nhi->ops->init() or nhi_select_cm(), nhi_probe() immediately
> returns without calling 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 nhi->ops->init_interrupts(). If a hardware interrupt fires
> immediately upon registration, nhi_msi() 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
>   nhi->ops->init_interrupts(). If DMA mask setup fails, exit early
>   before interrupts are enabled.
> - Handle post-interrupt error paths by nhi_shutdown().
>
> Fixes: e241d98e04ef ("thunderbolt: Separate out common NHI bits")
>
> Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>

[Severity: Low]
Does the Fixes tag point to the right commit?  Commit e241d98e04ef
("thunderbolt: Separate out common NHI bits") only refactored PCI helpers
out of nhi.c and preserved the existing probe order and error paths, so
citing it keeps stable-tree automation from finding older kernels.

These bugs were introduced by earlier commits:

  - 16603153666d ("thunderbolt: Add initial cactus ridge NHI support")
    for initializing nhi->lock after IRQ registration (converted to a
    spinlock in 59120e06101d ("thunderbolt: Use spinlock in NHI
    serialization"))
  - 9d3cce0b6136 ("thunderbolt: Introduce thunderbolt bus and connection
    manager") for returning without nhi_shutdown() when connection manager
    selection fails
  - dba3caf62103 ("thunderbolt: Use 64-bit DMA mask if supported by the
    platform") for calling dma_set_mask_and_coherent() after interrupt
    setup without nhi_shutdown() on failure
  - 3cdb9446a117 ("thunderbolt: Add support for Intel Ice Lake") for
    returning without nhi_shutdown() when nhi->ops->init() fails

Should the Fixes tag reference the introducing commit(s) instead?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=1

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c
  2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
@ 2026-12-05  8:50   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:50 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] thunderbolt: commit message wrongly claims tb_bus_type is
  private to domain.c
- [Low] thunderbolt: tb_domain_complete_rpm() leaks ICM internals into
  domain core
--

commit b67cb4b97423964ddf1ba9a2c60a06a557299bdc
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Move rpm_complete bus walk to domain.c

This commit moves the rpm_complete bus walk from icm.c into a new
tb_domain_complete_rpm() helper in domain.c so icm.c no longer references
tb_bus_type directly.

> Encapsulate this bus traversal inside domain.c as tb_domain_complete_rpm().
> This keeps tb_bus_type private to domain.c and eliminates the need to
> export the bus structure.

[Severity: Low]
Is tb_bus_type actually private to domain.c?  It remains a non-static
global declared extern in include/linux/thunderbolt.h and used directly
by retimer.c, switch.c, and xdomain.c.  The commit message should say
this keeps tb_bus_type internal to the thunderbolt core module instead.

[ ... ]

> diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
> index 24611f05b3cda..075e0f8af1e8f 100644
> --- a/drivers/thunderbolt/domain.c
> +++ b/drivers/thunderbolt/domain.c
> @@ -881,6 +881,30 @@ int tb_domain_unregister_unplugged_xdomains(struct tb *tb)
>  	return ctx.n;
>  }
>  
> +static int complete_rpm(struct device *dev, void *data)
> +{
> +	struct tb_switch *sw = tb_to_switch(dev);
> +
> +	if (sw && sw->tb == data)
> +		complete(&sw->rpm_complete);
> +
> +	return 0;
> +}
> +
> +/**
> + * tb_domain_complete_rpm() - Signal runtime resume completion for router and below
> + * @tb: Domain the router belongs to
> + * @sw: Router to complete runtime resume
> + *
> + * Signal @sw and routers below for rpm_complete because
> + * tb_switch_remove() calls pm_runtime_get_sync() that then waits for it.
> + */
> +void tb_domain_complete_rpm(struct tb *tb, struct tb_switch *sw)
> +{
> +	complete_rpm(&sw->dev, tb);
> +	bus_for_each_dev(&tb_bus_type, &sw->dev, tb, complete_rpm);
> +}

[Severity: Low]
Does tb_domain_complete_rpm() belong in domain.c when sw->rpm_complete is
only initialized in icm.c alloc_switch() and used by icm.c?  Moving it
here (and later exporting it in commit "thunderbolt: Make PCIe NHI
support opt-in") leaks icm.c internals into the generic domain core.

The tb parameter is also redundant with sw->tb, and the kerneldoc omits
that sw and subsequent switches on the bus must be non-root switches
with initialized rpm_complete.

Walking the subtree locally in icm.c via tb_switch_for_each_port() and
tb_port_has_remote() (matching icm_unplug_children() and
tb_switch_remove()) drops the tb_bus_type dependency without adding a
domain helper or signaling unrelated switches registered after sw.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=5

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()
  2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
@ 2026-12-05  8:50   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:50 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] thunderbolt: split pm_runtime_allow() and undocumented
  tb_nhi_remove() PM
--

commit 836a017990ff5baedafc35035fd228fa66590e97
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()

This commit moves the common domain and NHI teardown steps from
nhi_pci_do_remove() into a new tb_nhi_remove() helper in nhi.c and makes
nhi_shutdown() static.

> diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
> index 41a6ec03c4e74..cf83ce54fca40 100644
> --- a/drivers/thunderbolt/nhi.c
> +++ b/drivers/thunderbolt/nhi.c
[ ... ]
> @@ -1404,6 +1404,35 @@ int tb_nhi_probe(struct tb_nhi *nhi)
>  	return res;
>  }
>  
> +/**
> + * tb_nhi_remove() - Remove and release NHI instance and its domain
> + * @nhi: Pointer to the NHI structure
> + * @reset: Whether to force a host router reset (e.g. on shutdown/reboot)
> + *
> + * Common teardown sequence across all bus types. Stops and unregisters
> + * the domain, waits for domain release, and shuts down the NHI hardware.
> + */
> +void tb_nhi_remove(struct tb_nhi *nhi, bool reset)
> +{
[ ... ]
> +	pm_runtime_get_sync(dev);
> +	pm_runtime_dont_use_autosuspend(dev);
> +
> +	tb_domain_remove(tb);
> +	wait_for_completion(&nhi->domain_released);
> +	nhi_shutdown(nhi);
> +}
[ ... ]
> diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
> index 96280942e7083..9e48946c1d6bd 100644
> --- a/drivers/thunderbolt/pci.c
> +++ b/drivers/thunderbolt/pci.c
> @@ -568,21 +568,11 @@ static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
>  	struct tb *tb = pci_get_drvdata(pdev);
>  	struct tb_nhi *nhi = tb->nhi;
>  
> -	/*
> -	 * On system shutdown/reboot force a host router reset so the
> -	 * connection manager asserts DPR on connected Thunderbolt 3 devices
> -	 * before the router tree is removed (see tb_stop()).
> -	 */
> -	if (reset)
> -		nhi->host_reset = true;
> -
> -	pm_runtime_get_sync(&pdev->dev);
> -	pm_runtime_dont_use_autosuspend(&pdev->dev);
> +	/* Restore PCI device to forbid state */
>  	pm_runtime_forbid(&pdev->dev);
>  
> -	tb_domain_remove(tb);
> -	wait_for_completion(&nhi->domain_released);
> -	nhi_shutdown(nhi);
> +	tb_nhi_remove(nhi, reset);
> +
>  	nhi_pci_release_msi(nhi_to_pci(nhi));
>  }

[Severity: Medium]
Should pm_runtime_allow() also move into nhi_pci_probe() now that
pm_runtime_forbid() stays in nhi_pci_do_remove()?

PCI devices start forbidden in pci_pm_init(), while non-PCI devices start
allowed.  Leaving pm_runtime_allow(dev) in tb_nhi_probe() silently
overrides userspace power/control=on on platform NHI devices (commit
"thunderbolt: Add platform NHI driver"), allowing runtime suspend.

Also, tb_nhi_remove() takes an unbalanced pm_runtime_get_sync(dev), so
callers must keep runtime PM enabled on entry and drop the reference
afterwards.  That contract is undocumented in kernel-doc and contradicts
the commit message saying backends only handle PM before tb_nhi_remove().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=6

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core
  2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
@ 2026-12-05  8:51   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:51 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] thunderbolt: tb_nhi_probe() calls nhi->ops->shutdown() on IRQ
  request failure
--

commit b4233662a9b39ef9b7a70df478f45341b9bd4eb0
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core

This commit moves non-MSI-X interrupt request, ISR, and work handling from
the PCI backend into the NHI core, drops the .init_interrupts callback from
struct tb_nhi_ops, and stores the non-MSI-X IRQ number in struct tb_nhi.

> diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
> index 0e21e7b97371e..41a6ec03c4e74 100644
> --- a/drivers/thunderbolt/nhi.c
> +++ b/drivers/thunderbolt/nhi.c

[ ... ]

> @@ -1334,14 +1340,21 @@ int tb_nhi_probe(struct tb_nhi *nhi)
>  
>  	spin_lock_init(&nhi->lock);
>  
> +	INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
> +
>  	nhi_reset(nhi);
>  
>  	/* In case someone left them on. */
>  	nhi_disable_interrupts(nhi);
>  
> -	res = nhi->ops->init_interrupts(nhi);
> -	if (res)
> -		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
> +	if (nhi->irq > 0) {
> +		res = devm_request_irq(dev, nhi->irq, nhi_isr, IRQF_NO_SUSPEND,
> +				       dev_name(dev), nhi);
> +		if (res) {
> +			nhi->irq = 0;
> +			goto err;
> +		}
> +	}
>  
>  	if (nhi->ops->init) {
>  		res = nhi->ops->init(nhi);

[Severity: Medium]
Does jumping to err here invoke nhi->ops->shutdown() before
nhi->ops->init() has run?

The err label calls nhi_shutdown(), which unconditionally invokes
nhi->ops->shutdown().  On Ice Lake and newer controllers (icl_nhi_ops),
icl_nhi_shutdown() clears VS_CAP_22_FORCE_POWER even though .init never
ran, breaking the .init/.shutdown pairing for NHI backends.

Since interrupts are already disabled and no rings or backend state exist
yet, returning the error directly avoids calling nhi_shutdown().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=3

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci()
  2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun
@ 2026-12-05  8:55   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:55 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] thunderbolt: commit message misstates tunnel activation failure
  path
--

commit 16fea81347e049131d20cafc2ed656d8608d6787
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

This commit removes the WARN_ON() checks for a missing upstream PCIe port
or PCIe tunnel in tb_disconnect_pci(), keeping the -ENODEV return.

> - 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.

[Severity: Low]
Is this example accurate?

When tb_tunnel_activate() fails in tb_tunnel_pci(), it returns -EIO, so
tb_switch_set_authorized() leaves sw->authorized at 0.  Deauthorization
then returns early in tb_switch_set_authorized() and disapprove_switch()
skips the switch, so tb_disconnect_pci() is not reached.

Instead, an authorized switch can reach tb_disconnect_pci() without a PCIe
tunnel when tb_tunnel_pci() finds no upstream or downstream PCIe port and
returns 0, setting sw->authorized to 1 without creating a tunnel.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=14

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP
  2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
@ 2026-12-05  8:56   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:56 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] thunderbolt: tb_wait_for_port() cuts TB_PORT_UNPLUGGED
  timeout to 400ms
--

commit 05565fb12a80e9ccebefeee4587f7154d13fd01e
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP

This commit caps the time tb_wait_for_port() spends waiting in
TB_PORT_UNPLUGGED at 400 ms instead of using the full 2 second retry
budget.

> diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
> index e0bbdc5fb2f66..f25c358181dd3 100644
> --- a/drivers/thunderbolt/switch.c
> +++ b/drivers/thunderbolt/switch.c

[ ... ]

> @@ -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");

[Severity: Medium]
Can cutting unplugged_retries to 4 (400 ms) regress resume on slower
hardware that relied on the previous 1000 ms timeout?

The 400 ms limit is based on empirical USB4 timing, but
tb_wait_for_port(port, true) is also used on legacy Thunderbolt 1/2/3
resume paths (after tb_switch_reset() in tb_resume_noirq() and lane
init in tb_port_resume()) and in tb_xdomain_lane_bonding_enable().

If a port stays in TB_PORT_UNPLUGGED between 400 ms and 1000 ms,
tb_wait_for_port() now returns 0 early, causing tb_switch_resume() to
mark the downstream subtree unplugged and tear down its active tunnels.

Setting unplugged_retries to 10 would decouple TB_PORT_UNPLUGGED from
the new 2 second TB_PORT_UP timeout without shrinking the existing
1000 ms window, or a shorter timeout could be restricted to USB4
disconnect wakes via PORT_CS_18_WODS.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=13

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 11/14] thunderbolt: Add platform NHI driver
  2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
@ 2026-12-05  8:56   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:56 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] thunderbolt: tb_nhi_pm_ops noirq callbacks invert ACPI platform
  PM ordering
- [High] thunderbolt: nhi_isr() causes interrupt storm on
  level-triggered platform IRQs
- [Medium] thunderbolt: nhi_platform_do_remove() leaks wakeup_source on
  teardown
- [Medium] thunderbolt: pm_runtime_put_noidle() before
  pm_runtime_disable() races drvdata
- [Low] thunderbolt: nhi_platform_driver assigns .driver.pm without
  pm_ptr()
--

commit b442c5ebfee8bab338c63df36d4eb9041e54f694
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Add platform NHI driver

This commit adds a platform driver for ACPI-enumerated Thunderbolt/USB4
NHI controllers matching ACPI0015, including optional dedicated wake IRQ
support.

> diff --git a/drivers/thunderbolt/platform.c b/drivers/thunderbolt/platform.c
> new file mode 100644
> index 0000000000000..ce3bb65543c11
> --- /dev/null
> +++ b/drivers/thunderbolt/platform.c
> @@ -0,0 +1,155 @@

[ ... ]

> +	ret = platform_get_irq(pdev, 0);
> +	if (ret < 0)
> +		return ret;
> +
> +	nhi->irq = ret;

[Severity: High]
Can this cause an interrupt storm on level-triggered platform IRQs?

tb_nhi_probe() requests nhi->irq via devm_request_irq() with nhi_isr(),
which only calls schedule_work(&nhi->interrupt_work) and returns
IRQ_HANDLED without masking or acknowledging the interrupt.
REG_RING_NOTIFY_BASE is only read later in nhi_interrupt_work().

Previously nhi->irq came from PCI MSI (edge-triggered), whereas wired
platform IRQs on ACPI SoCs are commonly level-triggered.  When a
level-triggered interrupt fires, returning IRQ_HANDLED with the line
still asserted causes the controller to re-deliver the IRQ immediately,
starving nhi_interrupt_work() and livelocking the CPU.  Masking or
acknowledging the interrupt in hardirq context, or using
devm_request_threaded_irq() with IRQF_ONESHOT, would avoid this.

[ ... ]

> +static void nhi_platform_do_remove(struct platform_device *pdev, bool reset)
> +{
> +	struct tb *tb = platform_get_drvdata(pdev);
> +	struct device *dev = &pdev->dev;
> +	struct tb_nhi *nhi = tb->nhi;
> +	struct tb_nhi_platform *nhi_plat = nhi_to_platform(nhi);
> +
> +	tb_nhi_remove(nhi, reset);
> +
> +	if (nhi_plat->wake_irq)
> +		dev_pm_clear_wake_irq(dev);
> +
> +	device_set_wakeup_capable(dev, false);

[Severity: Medium]
Does this leak the wakeup_source registered by tb_nhi_probe()?

tb_nhi_probe() calls device_wakeup_enable(dev), which allocates and
registers dev->power.wakeup (along with its child wakeupN sysfs device),
while tb_nhi_remove() does not call device_wakeup_disable(dev).
device_set_wakeup_capable(dev, false) clears dev->power.can_wakeup
without detaching dev->power.wakeup.

Because device_wakeup_disable() returns early when !dev->power.can_wakeup,
even device_pm_remove() cannot free the wakeup_source later, and a
subsequent re-probe fails in device_wakeup_enable() with -EEXIST.
Calling device_init_wakeup(dev, false) instead avoids the leak.

> +	pm_runtime_put_noidle(dev);
> +	pm_runtime_disable(dev);
> +	pm_runtime_set_suspended(dev);
> +}

[Severity: Medium]
Can calling pm_runtime_put_noidle() before pm_runtime_disable() race with
nhi_runtime_suspend() and cause a use-after-free on tb?

CPU0                            CPU1
nhi_platform_do_remove()
  tb_nhi_remove()
    tb_domain_remove(tb)
    ...
    kfree(tb)
  pm_runtime_put_noidle(dev)
                                pm_suspend_timer_fn()
                                  nhi_runtime_suspend(dev)
                                    tb = dev_get_drvdata(dev)
                                    tb->nhi  <-- use-after-free
  pm_runtime_disable(dev)

tb_nhi_remove() waits for tb_domain_release() to free tb, but
dev->driver_data still points to it until device_unbind_cleanup().
Dropping usage_count to zero while runtime PM is still enabled lets an
armed autosuspend timer (or a stateless USB3 consumer device link)
invoke nhi_runtime_suspend() on freed memory.

The same ordering in nhi_platform_probe() under wake_irq_err can trigger
a NULL pointer dereference in nhi_runtime_suspend() while
dev_get_drvdata(dev) is NULL.  Calling pm_runtime_disable(dev) before
pm_runtime_put_noidle(dev) closes both windows.

[ ... ]

> +static struct platform_driver nhi_platform_driver = {
> +	.probe = nhi_platform_probe,
> +	.remove = nhi_platform_remove,
> +	.shutdown = nhi_platform_shutdown,
> +	.driver = {
> +		.name = "thunderbolt",
> +		.acpi_match_table = ACPI_PTR(nhi_platform_acpi_match),
> +		.pm = &tb_nhi_pm_ops,
> +	},
> +};

[Severity: High]
Does using tb_nhi_pm_ops invert the power-state ordering under
acpi_general_pm_domain?

When platform_probe() binds an ACPI platform device,
dev_pm_domain_attach() attaches acpi_general_pm_domain.  Unlike PCI PM,
acpi_general_pm_domain transitions the device to low power in
acpi_subsys_suspend_late() (and acpi_subsys_poweroff_late()), before
tb_nhi_pm_ops.suspend_noirq (nhi_suspend_noirq()) runs.

On resume, acpi_subsys_resume_noirq() runs nhi_resume_noirq() while the
device is still powered off, and because tb_nhi_pm_ops.resume_early is
NULL, acpi_subsys_resume_early() postpones acpi_dev_resume() until
acpi_subsys_resume() (with the same inversion in hibernation
restore_noirq vs acpi_subsys_restore_early()).

On SoCs whose ACPI power states gate the NHI clocks or power domain,
nhi_suspend_noirq() and nhi_resume_noirq() will perform MMIO register
accesses and control-channel DMA while the controller is powered off,
causing MMIO timeouts, failed tunnel recovery, or bus faults and hangs.

[Severity: Low]
This isn't a bug, but .pm should be wrapped with pm_ptr(&tb_nhi_pm_ops)
so the PM ops pointer evaluates to NULL and can be discarded when
CONFIG_PM is disabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=11

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in
  2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
@ 2026-12-05  8:58   ` sashiko-bot
  0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:58 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] thunderbolt: moving PCI IDs to thunderbolt_pci breaks blacklist
  and initramfs
- [Medium] thunderbolt: moving icm.o to thunderbolt_pci renames
  start_icm parameter
- [Low] thunderbolt: missing linux/export.h in four files adding
  EXPORT_SYMBOL_GPL()
--

commit e026bb092855a37bc1fed58283abd5123fa15e50
Author: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>

thunderbolt: Make PCIe NHI support opt-in

This commit decouples the PCIe NHI and ICM drivers from the Thunderbolt/USB4
core module into a separate thunderbolt_pci module under CONFIG_USB4_PCI,
and exports the core symbols needed by separate host controller drivers.

> diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
> index beb054c3126b1..a563bf2b03455 100644
> --- a/drivers/thunderbolt/Makefile
> +++ b/drivers/thunderbolt/Makefile
> @@ -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

[Severity: Medium]
Does moving icm.o into thunderbolt_pci rename the existing start_icm module
parameter?

icm.c defines module_param(start_icm, bool, 0444), previously exposed as
thunderbolt.start_icm.  With the new module name, thunderbolt.start_icm=1
on the kernel command line is silently ignored, and "options thunderbolt
start_icm=1" in /etc/modprobe.d/ causes modprobe thunderbolt to fail.

> diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
> index 965988b18608f..96717a0a94b13 100644
> --- a/drivers/thunderbolt/ctl.c
> +++ b/drivers/thunderbolt/ctl.c
> @@ -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);

[Severity: Low]
Should this file include <linux/export.h> for EXPORT_SYMBOL_GPL()?

ctl.c, domain.c, switch.c, and tunnel.c all newly add EXPORT_SYMBOL_GPL()
without directly including <linux/export.h>, which triggers W=2 build
warnings from scripts/misc-check.

[ ... ]

> diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
> index a7c92083f820b..850c301bc87cb 100644
> --- a/drivers/thunderbolt/pci.c
> +++ b/drivers/thunderbolt/pci.c
> @@ -690,7 +690,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");

[Severity: High]
Does moving MODULE_DEVICE_TABLE(pci, nhi_ids) to thunderbolt_pci break
existing module blacklists and initramfs configurations?

kmod blacklist rules only suppress modalias matching for the named module,
not loading it as a symbol dependency.  On systems with "blacklist
thunderbolt" in /etc/modprobe.d/ (for DMA attack mitigation), udev will now
match PCIe NHI devices against thunderbolt_pci and load both modules anyway.

In addition, initramfs setups or scripts that explicitly include or modprobe
thunderbolt will no longer pull in thunderbolt_pci, leaving PCIe controllers
unbound.  Keeping the PCIe driver named thunderbolt and naming the extracted
core module thunderbolt_core would preserve existing configurations.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=9

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI
  2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
                   ` (13 preceding siblings ...)
  2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun
@ 2026-12-05  8:58 ` sashiko-bot
  14 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-12-05  8:58 UTC (permalink / raw)
  To: Chunfeng Yun; +Cc: linux-usb

Sashiko has finished reviewing this 14-patch series:

- Patches 2, 7, 8, 10, and 12: no issues found.
- Patch 1 ("thunderbolt: Fix error handling and unwind paths in..."): 1
  potential issue (1 Low)
- Patch 3 ("thunderbolt: Internalize non-MSI-X interrupt request..."): 1
  potential issue (1 Medium)
- Patch 4 ("thunderbolt: Encapsulate router device type checks i..."): 1
  potential issue (1 Low)
- Patch 5 ("thunderbolt: Move rpm_complete bus walk to domain.c"): 2
  potential issues (2 Low)
- Patch 6 ("thunderbolt: Consolidate domain and NHI teardown int..."): 1
  potential issue (1 Medium)
- Patch 9 ("thunderbolt: Make PCIe NHI support opt-in"): 3 potential
  issues (1 High, 1 Medium, 1 Low)
- Patch 11 ("thunderbolt: Add platform NHI driver"): 5 potential issues
  (2 High, 2 Medium, 1 Low)
- Patch 13 ("thunderbolt: Decouple the waiting time of TB_PORT_UN..."):
  1 potential issue (1 Medium)
- Patch 14 ("thunderbolt: Drop WARN_ON() in tb_disconnect_pci()"): 1
  potential issue (1 Low)

Detailed reviews have been sent as separate replies to patches 1, 3-6,
9, 11, 13, and 14.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2026-12-05  8:58 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
2026-12-05  8:51   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
2026-12-05  8:41   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
2026-12-05  8:58   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
2026-12-05  8:56   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
2026-12-05  8:56   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun
2026-12-05  8:55   ` sashiko-bot
2026-12-05  8:58 ` [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox