[PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()
From: Chunfeng Yun
Date: Sat Dec 05 2026 - 03:34:57 EST
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@xxxxxxxxxxxx>
---
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