From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=210.61.82.184 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796459652; b=TXnHRo6IhZz2ch3wPH4lCMcUwCYWwsERUc4cQ4gSSPIlLBUHZmSmwi3IzjIswkKBFcZc U97QHEIQyhjyWioJOQLmeYj36IIPxEye+VGhp1oLe6I4vvxkfnfZDfakbLTg/ZIOqxh0u tGP/2AFQkHMjitsmUpK43zsQuWAS8PMM4Oq9ysRvgDxlJrrXJvLazg+OzrA0Avu5dPDx8 hjkPu5lfdg1//rP+vZ7nzHS8KTfkdSAyiMh46HGTRe328AFb2yZBroglzkKHkwrPGi2uV w6rwa87RiT3Nmruu5/8+yuIy8UkYb0pMz1LvzN0l7vQKOY8nYOuaq/CexVYStVuv5cA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796459652; h=DMARC-Filter:X-UUID:DKIM-Signature:X-CID-P-RULE:X-CID-O-INFO: X-CID-META:X-CID-BVR:X-CID-BAS:X-CID-FACTOR:X-CID-RHF:X-UUID:Received: Received:Received:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding: Content-Type:X-MTK; bh=bEi19mVsFnwOyGGuHtF3FxQueGl36jfirvLs8kWVllU=; b=wXTbGD8sID8byOmclVNXCL33IdUa1AnHtwo9yQsAfpTjhmTI3qND5UzpBc0WlDWhkLtS C5NFQXOZyCxoRcwcY39xR5mPixZDUB7aYOezMdIUlOJ7Sx2NBn/wBNowfMi8DGcAWIJze W7cX4T1N9DO7VHe8AtJFYQWS5KK79XtP/fnE848f9Ss0zkorhVcDZqbkyD9Osa+gHeFU4 hsBPKorHvEwKh+MzlvacEO8aWkc7mm8kwU6i/btfLtnQt9lO0wHE8A296sVKaf8+mBuf4 OnH0js+V/BadDB0KYlAWp0+dAXbiZ2p6PiwXavKsVC3RA8yRvUDfCj//j2o6n+sQBZg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=S5YuN0pT; arc=none smtp.remote-ip=210.61.82.184 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A961606A32F; Sat, 5 Dec 2026 08:34:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A961606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=S5YuN0pT X-UUID: 9f897600f08611f1a7aecdb2598be72c-20261205 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=bEi19mVsFnwOyGGuHtF3FxQueGl36jfirvLs8kWVllU=; b=S5YuN0pT+wtyLvIZbKlcJazHdp/zUnrKYjGU9OGditwGzlzui8BBWcLlewMz+ff57dW7XlZuCae9EhkErRMMroTyksM9W9AGJqZqMyjaPsOWK9reJwGXk9yb1+RrlxXCNe/tXNRHhNgcJtwP6GeGherVFSLPidvXmg42ZOFKRzY=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.22,REQID:337c48fe-ac98-4d04-9203-109282ce8a96,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:d36851b,CLOUDID:7663c631-f289-4656-bb36-27baf4489f11,B ulkID:nil,BulkQuantity:0,SF:81|82|102|836|865|888|898,TC:-5,Content:0|15|5 0|99,EDM:-3,IP:nil,URL:0,File:130,RT:0,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0 ,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 9f897600f08611f1a7aecdb2598be72c-20261205 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 941347386; Sat, 05 Dec 2026 16:34:01 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.56; Sat, 5 Dec 2026 16:34:00 +0800 Received: from gcnsap21.gcn.mediatek.inc (10.17.81.22) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.2562.56 via Frontend Transport; Sat, 5 Dec 2026 16:33:59 +0800 From: Chunfeng Yun To: Mika Westerberg CC: Andreas Noever , Konrad Dybcio , Sven Peter , Yehezkel Bernat , Matthias Brugger , Greg Kroah-Hartman , AngeloGioacchino Del Regno , , , , , Chunfeng Yun Subject: [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Date: Sat, 5 Dec 2026 16:33:29 +0800 Message-ID: <20261205083342.4009278-5-chunfeng.yun@mediatek.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261205083342.4009278-1-chunfeng.yun@mediatek.com> References: <20261205083342.4009278-1-chunfeng.yun@mediatek.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N 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 --- 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