Linux USB
 help / color / mirror / Atom feed
From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: linux-usb@vger.kernel.org
Cc: Yehezkel Bernat <YehezkelShB@gmail.com>,
	Lukas Wunner <lukas@wunner.de>,
	Andreas Noever <andreas.noever@gmail.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	Mika Westerberg <westeri@kernel.org>
Subject: [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters
Date: Wed, 25 Nov 2026 16:26:18 +0100	[thread overview]
Message-ID: <20261125152623.456054-6-mika.westerberg@linux.intel.com> (raw)
In-Reply-To: <20261125152623.456054-1-mika.westerberg@linux.intel.com>

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

At the moment we only support DMA tunnels between two hosts so that it
ends up or starts from the host interface adapter. This is the typical
use-case. However, it is entirely possible to make a DMA tunnel from one
lane adapter to another so acting as "passthrough" instead of ending or
starting the traffic.

This modifies the existing DMA tunneling functions to support lane
adapter to lane adapter tunnels as well.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c     |  11 +--
 drivers/thunderbolt/test.c   | 150 ++++++++++++++++--------------
 drivers/thunderbolt/tunnel.c | 171 +++++++++++++++++++++--------------
 drivers/thunderbolt/tunnel.h |  14 +--
 4 files changed, 194 insertions(+), 152 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 10fca534ff78..c7a009a62411 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -2393,8 +2393,8 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	 */
 	tb_disable_clx(sw);
 
-	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
-				     transmit_ring, receive_path, receive_ring);
+	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, transmit_ring, receive_ring,
+				     dst_port, receive_path, transmit_path);
 	if (!tunnel) {
 		ret = -ENOMEM;
 		goto err_clx;
@@ -2437,11 +2437,10 @@ static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
 		if (!tb_tunnel_is_dma(tunnel))
 			continue;
-		if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
-			continue;
 
-		if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
-					receive_path, receive_ring))
+		if (tb_tunnel_match_dma(tunnel, nhi_port, transmit_ring,
+					receive_ring, dst_port, receive_path,
+					transmit_path))
 			tb_deactivate_and_free_tunnel(tunnel);
 	}
 
diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index 3fa9c4661541..0321408fe91c 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -1924,7 +1924,7 @@ static void tb_test_tunnel_dma(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &host->ports[1];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -1967,7 +1967,7 @@ static void tb_test_tunnel_dma_rx(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &host->ports[1];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, -1, -1, 15, 2);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, -1, 2, port, 15, -1);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2006,7 +2006,7 @@ static void tb_test_tunnel_dma_tx(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &host->ports[1];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 2, -1, -1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 2, -1, port, -1, 15);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2054,7 +2054,7 @@ static void tb_test_tunnel_dma_chain(struct kunit *test)
 
 	nhi = &host->ports[7];
 	port = &dev2->ports[3];
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2103,45 +2103,55 @@ static void tb_test_tunnel_dma_match(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &host->ports[1];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 1, 15, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 15, 15);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, 15, 1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 8, 1, 15, 1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, -1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, 1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, 1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 8, -1, 8, -1));
+#define MATCH(src, src_in, src_out, dst, dst_in, dst_out) \
+	tb_tunnel_match_dma(tunnel, src, src_in, src_out, dst, dst_in, dst_out)
+
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, 1, port, 15, 15));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, 1, 1, port, 15, 8));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 1, port, 15, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, 15));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, 15));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, 15, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 1, port, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(NULL, -1, -1, NULL, -1, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, -1, port, 8, 8));
+	/* The two ends can be given in either order */
+	KUNIT_ASSERT_TRUE(test, MATCH(port, 15, 15, nhi, 1, 1));
+	/* The tunnel does not end to this port */
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, -1, &host->ports[3], -1, -1));
 
 	tb_tunnel_put(tunnel);
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 1, -1, -1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, -1, port, -1, 15);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, -1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, 1, -1, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 1, 15, 1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, 15));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, 15));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, 1, 1, port, 15, 15));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 1, port, 15, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, 11, -1, port, -1, 15));
 
 	tb_tunnel_put(tunnel);
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, -1, -1, 15, 11);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, -1, 11, port, 15, -1);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 11));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, -1));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, 11));
-	KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 10, 11));
-	KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 11, port, 15, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, 15, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 11, port, -1, -1));
+	KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 1, port, 15, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 11, port, 10, -1));
+	KUNIT_ASSERT_FALSE(test, MATCH(nhi, 11, -1, port, -1, 15));
 
 	tb_tunnel_put(tunnel);
+
+#undef MATCH
 }
 
 static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test)
@@ -2642,7 +2652,7 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &dev->ports[3];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
 
@@ -2712,7 +2722,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	 * Tunnel 6 fails initially then we release tunnel 1 and then
 	 * tunnel 6 gets full 14 for both sides.
 	 */
-	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel1);
 	KUNIT_ASSERT_EQ(test, tunnel1->npaths, (size_t)2);
 
@@ -2732,7 +2742,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
-	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel2);
 	KUNIT_ASSERT_EQ(test, tunnel2->npaths, (size_t)2);
 
@@ -2752,7 +2762,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel3);
 	KUNIT_ASSERT_EQ(test, tunnel3->npaths, (size_t)2);
 
@@ -2772,7 +2782,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel4);
 	KUNIT_ASSERT_EQ(test, tunnel4->npaths, (size_t)2);
 
@@ -2792,7 +2802,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 10U);
 
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel5);
 	KUNIT_ASSERT_EQ(test, tunnel5->npaths, (size_t)2);
 
@@ -2812,12 +2822,12 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
 	KUNIT_ASSERT_NULL(test, tunnel6);
 
 	tb_tunnel_put(tunnel1);
 
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel6);
 	KUNIT_ASSERT_EQ(test, tunnel6->npaths, (size_t)2);
 
@@ -2933,7 +2943,7 @@ static void tb_test_credit_alloc_dma_shared(struct kunit *test)
 	 * (port 3) is not part of the link so it uses dedicated
 	 * buffers.
 	 */
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel, 14, 14, 14, true);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 1);
@@ -2974,15 +2984,15 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
 	 * dev ingress port (3) is still using dedicated for the
 	 * inter-domain link (and runs out of credits the same way).
 	 */
-	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, true);
-	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, true);
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
 	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, true);
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 14, true);
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 14, true);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -2993,7 +3003,7 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
 	 * shared buffers are allocated. Releasing the failed tunnel
 	 * must not drop the users of the shared buffers.
 	 */
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
 	KUNIT_ASSERT_NULL(test, tunnel6);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -3004,7 +3014,7 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
 	tb_test_check_shared(test, &host->ports[1], 14, 4);
 	tb_test_check_shared(test, &dev->ports[1], 14, 4);
 
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
 	tb_test_check_dma_tunnel(test, tunnel6, 14, 14, 14, true);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -3027,17 +3037,17 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
 	tb_switch_disable_shared_buffering(host);
 	tb_switch_disable_shared_buffering(dev);
 
-	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
-	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
 	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
 	KUNIT_ASSERT_NULL(test, tunnel6);
 
 	tb_tunnel_put(tunnel5);
@@ -3071,11 +3081,11 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
 	 * with dedicated buffers. This leaves 52 - 3 * 14 = 10 credits
 	 * there.
 	 */
-	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
-	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
 	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
 
 	/*
@@ -3086,14 +3096,14 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
 	tb_switch_enable_shared_buffering(host);
 	tb_switch_enable_shared_buffering(dev);
 
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, true);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 1);
 	tb_test_check_shared(test, &dev->ports[1], 10, 1);
 
 	/* This one joins the existing shared buffers */
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 10, true);
 
 	tb_test_check_shared(test, &host->ports[1], 14, 2);
@@ -3117,9 +3127,9 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
 	tb_switch_disable_shared_buffering(host);
 	tb_switch_disable_shared_buffering(dev);
 
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
 
 	tb_tunnel_put(tunnel5);
@@ -3153,13 +3163,13 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
 	common = &up->credit_pool[USB4_CREDIT_POOL_COMMON];
 	dma = &up->credit_pool[USB4_CREDIT_POOL_DMA];
 
-	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
-	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
 	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
-	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
 	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
 
 	KUNIT_EXPECT_EQ(test, usb4_credit_pool_available(common), 0U);
@@ -3174,7 +3184,7 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
 	 * the shared buffers get the minimum from the DMA pool, same as
 	 * the dedicated buffers would.
 	 */
-	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
 	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, true);
 	tb_test_check_shared(test, up, 1, 1);
 	KUNIT_EXPECT_PTR_EQ(test, up->shared_pool, dma);
@@ -3182,7 +3192,7 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 1U);
 
 	/* Port 3 has run out so this one goes through port 5 instead */
-	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, &dev->ports[5], 8, 6, 8, 6);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, &dev->ports[5], 8, 8);
 	tb_test_check_dma_tunnel(test, tunnel6, 14, 14, 1, true);
 	tb_test_check_shared(test, up, 1, 2);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel6->paths[1]->hops[1].credit_pool, dma);
@@ -3237,7 +3247,7 @@ static void tb_test_credit_alloc_dma_shared_mixed(struct kunit *test)
 	nhi = &host->ports[7];
 	port = &dev2->ports[3];
 
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
 
@@ -3297,7 +3307,7 @@ static void tb_test_credit_alloc_dma_shared_no_credit_allocation(struct kunit *t
 	 * host ingress (RX path) can use shared buffers. The device
 	 * ingress uses the fixed dedicated credits.
 	 */
-	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel);
 	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
 
@@ -3517,7 +3527,7 @@ static struct tb_tunnel *TB_TEST_DMA_TUNNEL1(struct kunit *test,
 
 	nhi = &host->ports[7];
 	port = &dev->ports[3];
-	dma_tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	dma_tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
 	KUNIT_ASSERT_NOT_NULL(test, dma_tunnel1);
 	KUNIT_ASSERT_EQ(test, dma_tunnel1->npaths, (size_t)2);
 
@@ -3547,7 +3557,7 @@ static struct tb_tunnel *TB_TEST_DMA_TUNNEL2(struct kunit *test,
 
 	nhi = &host->ports[7];
 	port = &dev->ports[3];
-	dma_tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	dma_tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
 	KUNIT_ASSERT_NOT_NULL(test, dma_tunnel2);
 	KUNIT_ASSERT_EQ(test, dma_tunnel2->npaths, (size_t)2);
 
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 3f2b6f8eb243..b36fed0b52d4 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -1733,36 +1733,54 @@ static int tb_dma_init_path(struct tb_path *path)
 	return 0;
 }
 
+static const char *
+tb_dma_path_name(const struct tb_port *src, const struct tb_port *dst)
+{
+	if (tb_port_is_nhi(src))
+		return "DMA TX";
+	if (tb_port_is_nhi(dst))
+		return "DMA RX";
+	return "DMA";
+}
+
 /**
- * tb_tunnel_alloc_dma() - allocate a DMA tunnel
+ * tb_tunnel_alloc_dma() - Allocate a DMA tunnel
  * @tb: Pointer to the domain structure
- * @nhi: Host controller port
- * @dst: Destination null port which the other domain is connected to
- * @transmit_path: HopID used for transmitting packets
- * @transmit_ring: NHI ring number used to send packets towards the
- *		   other domain. Set to %-1 if TX path is not needed.
- * @receive_path: HopID used for receiving packets
- * @receive_ring: NHI ring number used to receive packets from the
- *		  other domain. Set to %-1 if RX path is not needed.
+ * @src: First end of the tunnel
+ * @src_in_hopid: HopID the packets flowing towards @dst enter @src with. Set
+ *		  to %-1 if the path from @src to @dst is not needed.
+ * @src_out_hopid: HopID the packets flowing towards @src leave with. Set to
+ *		   %-1 if the path from @dst to @src is not needed.
+ * @dst: Second end of the tunnel
+ * @dst_in_hopid: HopID the packets flowing towards @src enter @dst with. Set
+ *		  to %-1 if the path from @dst to @src is not needed.
+ * @dst_out_hopid: HopID the packets flowing towards @dst leave with. Set to
+ *		   %-1 if the path from @src to @dst is not needed.
+ *
+ * Allocates a DMA tunnel between host interface and lane adapter, lane
+ * adapter and host interface or lane adapter and lane adapter.
  *
  * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
  */
-struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
-				      struct tb_port *dst, int transmit_path,
-				      int transmit_ring, int receive_path,
-				      int receive_ring)
+struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *src,
+				      int src_in_hopid, int src_out_hopid,
+				      struct tb_port *dst, int dst_in_hopid,
+				      int dst_out_hopid)
 {
+	bool src_to_dst, dst_to_src;
 	struct tb_tunnel *tunnel;
 	size_t npaths = 0, i = 0;
 	struct tb_path *path;
 
-	/* Ring 0 is reserved for control channel */
-	if (WARN_ON(!receive_ring || !transmit_ring))
+	if (WARN_ON(src == dst))
 		return NULL;
 
-	if (receive_ring > 0)
+	src_to_dst = src_in_hopid > 0 && dst_out_hopid > 0;
+	dst_to_src = dst_in_hopid > 0 && src_out_hopid > 0;
+
+	if (src_to_dst)
 		npaths++;
-	if (transmit_ring > 0)
+	if (dst_to_src)
 		npaths++;
 
 	if (WARN_ON(!npaths))
@@ -1772,29 +1790,31 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 	if (!tunnel)
 		return NULL;
 
-	tunnel->src_port = nhi;
+	tunnel->src_port = src;
 	tunnel->dst_port = dst;
 
-	if (receive_ring > 0) {
-		path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
-				     "DMA RX");
+	if (dst_to_src) {
+		path = tb_path_alloc(tb, dst, dst_in_hopid, src, src_out_hopid,
+				     0, tb_dma_path_name(dst, src));
 		if (!path)
 			goto err_free;
 		tunnel->paths[i++] = path;
 		if (tb_dma_init_path(path)) {
-			tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
+			tb_tunnel_dbg(tunnel, "not enough buffers for %s path\n",
+				      path->name);
 			goto err_free;
 		}
 	}
 
-	if (transmit_ring > 0) {
-		path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
-				     "DMA TX");
+	if (src_to_dst) {
+		path = tb_path_alloc(tb, src, src_in_hopid, dst, dst_out_hopid,
+				     0, tb_dma_path_name(src, dst));
 		if (!path)
 			goto err_free;
 		tunnel->paths[i++] = path;
 		if (tb_dma_init_path(path)) {
-			tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
+			tb_tunnel_dbg(tunnel, "not enough buffers for %s path\n",
+				      path->name);
 			goto err_free;
 		}
 	}
@@ -1806,67 +1826,78 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 	return NULL;
 }
 
-/**
- * tb_tunnel_match_dma() - Match DMA tunnel
- * @tunnel: Tunnel to match
- * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
- * @transmit_ring: NHI ring number used to send packets towards the
- *		   other domain. Pass %-1 to ignore.
- * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
- * @receive_ring: NHI ring number used to receive packets from the
- *		  other domain. Pass %-1 to ignore.
- *
- * This function can be used to match specific DMA tunnel, if there are
- * multiple DMA tunnels going through the same XDomain connection.
- *
- * Return: %true if there is a match, %false otherwise.
- */
-bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
-			 int transmit_ring, int receive_path, int receive_ring)
+static void tb_dma_end_hopids(const struct tb_tunnel *tunnel,
+			      const struct tb_port *port,
+			      int *in_hopid, int *out_hopid)
 {
-	const struct tb_path *tx_path = NULL, *rx_path = NULL;
 	int i;
 
-	if (!receive_ring || !transmit_ring)
-		return false;
+	*in_hopid = *out_hopid = -1;
 
 	for (i = 0; i < tunnel->npaths; i++) {
 		const struct tb_path *path = tunnel->paths[i];
+		const struct tb_path_hop *first, *last;
 
 		if (!path)
 			continue;
 
-		if (tb_port_is_nhi(path->hops[0].in_port))
-			tx_path = path;
-		else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
-			rx_path = path;
-	}
+		first = &path->hops[0];
+		last = &path->hops[path->path_length - 1];
 
-	if (transmit_ring > 0 || transmit_path > 0) {
-		if (!tx_path)
-			return false;
-		if (transmit_ring > 0 &&
-		    (tx_path->hops[0].in_hop_index != transmit_ring))
-			return false;
-		if (transmit_path > 0 &&
-		    (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
-			return false;
+		if (first->in_port == port)
+			*in_hopid = first->in_hop_index;
+		else if (last->out_port == port)
+			*out_hopid = last->next_hop_index;
 	}
+}
 
-	if (receive_ring > 0 || receive_path > 0) {
-		if (!rx_path)
-			return false;
-		if (receive_path > 0 &&
-		    (rx_path->hops[0].in_hop_index != receive_path))
-			return false;
-		if (receive_ring > 0 &&
-		    (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
-			return false;
-	}
+static bool tb_dma_match_end(const struct tb_tunnel *tunnel,
+			     const struct tb_port *port, int in_hopid,
+			     int out_hopid)
+{
+	int tunnel_in_hopid, tunnel_out_hopid;
+
+	if (!port)
+		return true;
+
+	tb_dma_end_hopids(tunnel, port, &tunnel_in_hopid, &tunnel_out_hopid);
+
+	/* The tunnel must end to the adapter in the first place */
+	if (tunnel_in_hopid < 0 && tunnel_out_hopid < 0)
+		return false;
+
+	if (in_hopid > 0 && in_hopid != tunnel_in_hopid)
+		return false;
+	if (out_hopid > 0 && out_hopid != tunnel_out_hopid)
+		return false;
 
 	return true;
 }
 
+/**
+ * tb_tunnel_match_dma() - Match DMA tunnel
+ * @tunnel: Tunnel to match
+ * @src: First end of the tunnel. Pass %NULL to ignore.
+ * @src_in_hopid: HopID the packets entering @src use. Pass %-1 to ignore.
+ * @src_out_hopid: HopID the packets leaving @src use. Pass %-1 to ignore.
+ * @dst: Second end of the tunnel. Pass %NULL to ignore.
+ * @dst_in_hopid: HopID the packets entering @dst use. Pass %-1 to ignore.
+ * @dst_out_hopid: HopID the packets leaving @dst use. Pass %-1 to ignore.
+ *
+ * This function can be used to match specific DMA tunnel, if there are
+ * multiple DMA tunnels going through the same XDomain connection.
+ *
+ * Return: %true if there is a match, %false otherwise.
+ */
+bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel,
+			 const struct tb_port *src, int src_in_hopid,
+			 int src_out_hopid, const struct tb_port *dst,
+			 int dst_in_hopid, int dst_out_hopid)
+{
+	return tb_dma_match_end(tunnel, src, src_in_hopid, src_out_hopid) &&
+	       tb_dma_match_end(tunnel, dst, dst_in_hopid, dst_out_hopid);
+}
+
 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
 {
 	int ret, up_max_rate, down_max_rate;
diff --git a/drivers/thunderbolt/tunnel.h b/drivers/thunderbolt/tunnel.h
index 35d026c5bc96..5dedf742b14d 100644
--- a/drivers/thunderbolt/tunnel.h
+++ b/drivers/thunderbolt/tunnel.h
@@ -119,12 +119,14 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
 				     struct tb_port *out, int link_nr,
 				     int max_up, int max_down,
 				     void (*callback)(struct tb_tunnel *));
-struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
-				      struct tb_port *dst, int transmit_path,
-				      int transmit_ring, int receive_path,
-				      int receive_ring);
-bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
-			 int transmit_ring, int receive_path, int receive_ring);
+struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *src,
+				      int src_in_hopid, int src_out_hopid,
+				      struct tb_port *dst, int dst_in_hopid,
+				      int dst_out_hopid);
+bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel,
+			 const struct tb_port *src, int src_in_hopid,
+			 int src_out_hopid, const struct tb_port *dst,
+			 int dst_in_hopid, int dst_out_hopid);
 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down);
 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
 				       struct tb_port *down, int max_up,
-- 
2.50.1


  parent reply	other threads:[~2026-11-25 15:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
2026-11-25 15:26 ` [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed Mika Westerberg
2026-11-25 15:26 ` [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs Mika Westerberg
2026-11-25 15:26 ` [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly Mika Westerberg
2026-11-25 15:26 ` Mika Westerberg [this message]
2026-11-25 15:26 ` [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels Mika Westerberg
2026-11-25 15:26 ` [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths() Mika Westerberg
2026-11-25 15:26 ` [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 9/9] Documentation: thunderbolt: Document " Mika Westerberg

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=20261125152623.456054-6-mika.westerberg@linux.intel.com \
    --to=mika.westerberg@linux.intel.com \
    --cc=YehezkelShB@gmail.com \
    --cc=andreas.noever@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=lukas@wunner.de \
    --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