mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chunfeng Yun <chunfeng.yun@mediatek.com>
To: Mika Westerberg <westeri@kernel.org>
Cc: Andreas Noever <andreas.noever@gmail.com>,
	Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>,
	Sven Peter <sven@kernel.org>,
	Yehezkel Bernat <YehezkelShB@gmail.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	<linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>
Subject: [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c
Date: Sat, 5 Dec 2026 16:33:29 +0800	[thread overview]
Message-ID: <20261205083342.4009278-5-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <20261205083342.4009278-1-chunfeng.yun@mediatek.com>

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


  parent reply	other threads:[~2026-12-05  8:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [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 [this message]
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 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
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: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: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:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205083342.4009278-5-chunfeng.yun@mediatek.com \
    --to=chunfeng.yun@mediatek.com \
    --cc=YehezkelShB@gmail.com \
    --cc=andreas.noever@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=matthias.bgg@gmail.com \
    --cc=sven@kernel.org \
    --cc=westeri@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®