Linux USB
 help / color / mirror / Atom feed
* [PATCH 00/18] thunderbolt: Add support for shared buffering
@ 2026-11-19 17:12 Mika Westerberg
  2026-11-19 17:12 ` [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal Mika Westerberg
                   ` (17 more replies)
  0 siblings, 18 replies; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

Hi all,

Currently all DMA tunnels (over XDomain) use dedicated flow control meaning
that each consumes fixed amount of buffers from a router ingress adapter.
This becomes problematic if the DMA tunnel is routed through a device
router because that may involve other tunnels like native ones that take
away their share of buffers. The current scheme is such that we allocate
full credits for USB3/PCIe, some for DMA and the rest goes for DisplayPort.
On Intel hardware this means the first DMA tunnel gets full buffers and the
second one gets one and after that we run out.

This series reworks the credit allocation so that there is still reserve
but a smaller one. For DP we allocate as much as there are DP INs on in the
host router, then minimum for USB3/PCIe/DMA. Everything else goes to common
pool that is first come first served.

Then we add support for shared buffering and lastly enable them for DMA
tunnels over intra-domain links. This allows more DMA tunnels to share a
link. The inter-domain link stays using the dedicated flow control.

The other patches before the credit work are needed because they fix a long
standing issue around suspend to disk. The topology is now reset on restore
so DP resources the restore kernel left behind are no longer leaked. That
would otherwise affect the credits too.

Gil Fine (2):
  thunderbolt: Add support for shared flow control
  thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links

Mika Westerberg (Intel) (16):
  thunderbolt: Avoid leaking of DMA tunnel after XDomain removal
  thunderbolt: Tear down DMA tunnels on every XDomain removal
  thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails
  thunderbolt: Call DP tunnel activation callback with tb->lock held
  thunderbolt: Re-apply Alpine/Titan Ridge flow on resume
  thunderbolt: Return -ESHUTDOWN when control channel is not running
  thunderbolt: Add usb4_port_hotplug_disable()
  thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host()
  thunderbolt: Add tb_port_state_is_up()
  thunderbolt: Split host router port reset into a separate function
  thunderbolt: Reset topology after suspend-to-disk
  thunderbolt: Drop tunnel discovery without HopID allocation
  thunderbolt: Always take domain lock when tb_tunnel_put() is called
  thunderbolt: Assign domain to the routers in KUnit tests
  thunderbolt: Rework tunnel credit allocation
  thunderbolt: Add KUnit tests for shared buffering

 drivers/thunderbolt/Makefile  |    2 +-
 drivers/thunderbolt/credits.c |  685 ++++++++++++++++++++
 drivers/thunderbolt/ctl.c     |    4 +-
 drivers/thunderbolt/debugfs.c |   43 ++
 drivers/thunderbolt/domain.c  |    5 +-
 drivers/thunderbolt/nhi.c     |   17 +-
 drivers/thunderbolt/path.c    |  101 +--
 drivers/thunderbolt/switch.c  |  374 ++++++++---
 drivers/thunderbolt/tb.c      |  201 +++---
 drivers/thunderbolt/tb.h      |  200 ++++--
 drivers/thunderbolt/tb_regs.h |    2 +
 drivers/thunderbolt/test.c    | 1106 +++++++++++++++++++++++++++++++--
 drivers/thunderbolt/tunnel.c  |  495 +++++----------
 drivers/thunderbolt/tunnel.h  |    7 +-
 drivers/thunderbolt/usb4.c    |   28 +-
 15 files changed, 2601 insertions(+), 669 deletions(-)
 create mode 100644 drivers/thunderbolt/credits.c

-- 
2.50.1


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

* [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:32   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every " Mika Westerberg
                   ` (16 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

When XDomain is disconnected the service drivers remain bound until
tb_domain_unregister_unplugged_xdomains() runs outside of tb->lock, and
during that window a service driver can still enable a tunnel by calling
tb_xdomain_enable_paths(). This tunnel is also added to the tunnel list.
Then later when the service driver is unbound
tb_disconnect_xdomain_paths() checks for xd->is_unplugged and skips the
tear down leaking the tunnel.

Fix this by checking xd->is_unplugged and doing the adapter lookups
under tb->lock (tb->root_switch is NULL once domain is being removed) in
tb_approve_xdomain_paths() and tb_disconnect_xdomain_paths().

Fixes: a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without holding tb->lock")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 418ad0a61a5d..655bcc699410 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -2336,12 +2336,18 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	struct tb_switch *sw;
 	int ret;
 
+	mutex_lock(&tb->lock);
+
+	/* The XDomain is going away so no new tunnels allowed */
+	if (xd->is_unplugged) {
+		ret = -ENODEV;
+		goto err_unlock;
+	}
+
 	sw = tb_to_switch(xd->dev.parent);
 	dst_port = tb_port_at(xd->route, sw);
 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
 
-	mutex_lock(&tb->lock);
-
 	/*
 	 * When tunneling DMA paths the link should not enter CL states
 	 * so disable them now.
@@ -2370,6 +2376,7 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	tb_tunnel_put(tunnel);
 err_clx:
 	tb_enable_clx(sw);
+err_unlock:
 	mutex_unlock(&tb->lock);
 
 	return ret;
@@ -2411,13 +2418,12 @@ static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 				       int transmit_path, int transmit_ring,
 				       int receive_path, int receive_ring)
 {
-	if (!xd->is_unplugged) {
-		mutex_lock(&tb->lock);
+	mutex_lock(&tb->lock);
+	if (!xd->is_unplugged)
 		__tb_disconnect_xdomain_paths(tb, xd, transmit_path,
 					      transmit_ring, receive_path,
 					      receive_ring);
-		mutex_unlock(&tb->lock);
-	}
+	mutex_unlock(&tb->lock);
 	return 0;
 }
 
-- 
2.50.1


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

* [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every XDomain removal
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
  2026-11-19 17:12 ` [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:34   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails Mika Westerberg
                   ` (15 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Currently we only tear down DMA tunnels when the XDomain is unplugged
(and when service drivers explicitly do that) but not when the XDomain
is removed as part of resume or replaced by a router in tb_scan_port().
The tunnel then stays in the tunnel list until the driver is unloaded
(or the router it goes through is removed).

Fix this by always tearing down the tunnels when XDomain is removed.

Fixes: 7ea4cd6b2010 ("thunderbolt: Add support for XDomain connections")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c | 47 ++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 655bcc699410..680ed23db0bf 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -83,6 +83,9 @@ struct tb_hotplug_event {
 	int retry;
 };
 
+static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
+					  int transmit_path, int transmit_ring,
+					  int receive_path, int receive_ring);
 static void tb_scan_port(struct tb_port *port);
 static void tb_handle_hotplug(struct work_struct *work);
 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port,
@@ -456,6 +459,23 @@ static void tb_scan_xdomain(struct tb_port *port)
 	}
 }
 
+static void tb_remove_xdomain(struct tb_port *port)
+{
+	struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
+	struct tb *tb = xd->tb;
+
+	/*
+	 * Paths are torn down here so make tb_xdomain_disable_paths()
+	 * from the service drivers no-op.
+	 */
+	xd->is_unplugged = true;
+	tb_xdomain_remove(port->xdomain);
+	port->xdomain = NULL;
+	__tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
+	tb_port_unconfigure_xdomain(port);
+	tb_xdomain_put(xd);
+}
+
 /*
  * Returns the first inactive port on @sw.
  */
@@ -1352,11 +1372,8 @@ static void tb_scan_port(struct tb_port *port)
 	 * If there was previously another domain connected remove it
 	 * first.
 	 */
-	if (port->xdomain) {
-		tb_xdomain_remove(port->xdomain);
-		tb_port_unconfigure_xdomain(port);
-		port->xdomain = NULL;
-	}
+	if (port->xdomain)
+		tb_remove_xdomain(port);
 
 	/*
 	 * Do not send uevents until we have discovered all existing
@@ -2491,22 +2508,8 @@ static void tb_handle_hotplug(struct work_struct *work)
 			tb_recalc_estimated_bandwidth(tb);
 			tb_tunnel_dp(tb);
 		} else if (port->xdomain) {
-			struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
-
 			tb_port_dbg(port, "xdomain unplugged\n");
-			/*
-			 * Service drivers are unbound during
-			 * tb_xdomain_remove() so setting XDomain as
-			 * unplugged here prevents deadlock if they call
-			 * tb_xdomain_disable_paths(). We will tear down
-			 * all the tunnels below.
-			 */
-			xd->is_unplugged = true;
-			tb_xdomain_remove(xd);
-			port->xdomain = NULL;
-			__tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
-			tb_xdomain_put(xd);
-			tb_port_unconfigure_xdomain(port);
+			tb_remove_xdomain(port);
 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
 			tb_dp_resource_unavailable(tb, port, "adapter unplug");
 		} else if (!port->port) {
@@ -3167,9 +3170,7 @@ static void tb_free_unplugged_xdomains(struct tb_switch *sw)
 			continue;
 		if (port->xdomain && port->xdomain->is_unplugged) {
 			tb_retimer_remove_all(port);
-			tb_xdomain_remove(port->xdomain);
-			tb_port_unconfigure_xdomain(port);
-			port->xdomain = NULL;
+			tb_remove_xdomain(port);
 		} else if (port->remote) {
 			tb_free_unplugged_xdomains(port->remote->sw);
 		}
-- 
2.50.1


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

* [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
  2026-11-19 17:12 ` [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal Mika Westerberg
  2026-11-19 17:12 ` [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every " Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:26   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held Mika Westerberg
                   ` (14 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

If DPRX negotiation fails, for example the DP IN that was tried does not
have cable connected to the add-in-card, we call tb_dp_resource_unavailable()
and that then tries to pair the DP OUT with another DP IN. However, at
that point the existing tunnel may be still referenced so its HopIDs are
reserved and that causes the new tunnel activation to fail.

Instead schedule a worker that runs on the same work queue as the DPRX
work so the reference should be dropped at that point.

Fixes: 67600ccfc4f3 ("thunderbolt: Fix use-after-free in tb_dp_dprx_work")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 680ed23db0bf..acad8461011c 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -59,6 +59,7 @@ MODULE_PARM_DESC(asym_threshold,
  *		    after cfg has been paused.
  * @remove_work: Work used to remove any unplugged routers after
  *		 runtime resume
+ * @tunnel_dp_work: Work used to delay DP tunnel creation
  * @groups: Bandwidth groups used in this domain.
  */
 struct tb_cm {
@@ -66,6 +67,7 @@ struct tb_cm {
 	struct list_head dp_resources;
 	bool hotplug_active;
 	struct delayed_work remove_work;
+	struct delayed_work tunnel_dp_work;
 	struct tb_bandwidth_group groups[MAX_GROUPS];
 };
 
@@ -2128,6 +2130,23 @@ static void tb_tunnel_dp(struct tb *tb)
 		tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
 }
 
+static void tb_tunnel_dp_work(struct work_struct *work)
+{
+	struct tb_cm *tcm = container_of(work, struct tb_cm,
+					 tunnel_dp_work.work);
+	struct tb *tb = tcm_to_tb(tcm);
+
+	pm_runtime_get_sync(&tb->dev);
+
+	scoped_guard(mutex, &tb->lock) {
+		if (tcm->hotplug_active)
+			tb_tunnel_dp(tb);
+	}
+
+	pm_runtime_mark_last_busy(&tb->dev);
+	pm_runtime_put_autosuspend(&tb->dev);
+}
+
 static void tb_enter_redrive(struct tb_port *port)
 {
 	struct tb_switch *sw = port->sw;
@@ -2205,6 +2224,7 @@ static void tb_switch_exit_redrive(struct tb_switch *sw)
 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port,
 				       const char *reason)
 {
+	struct tb_cm *tcm = tb_priv(tb);
 	struct tb_port *in, *out;
 	struct tb_tunnel *tunnel;
 
@@ -2230,7 +2250,7 @@ static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port,
 	 * to create another tunnel.
 	 */
 	tb_recalc_estimated_bandwidth(tb);
-	tb_tunnel_dp(tb);
+	queue_delayed_work(tb->wq, &tcm->tunnel_dp_work, 0);
 }
 
 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
@@ -2966,6 +2986,7 @@ static void tb_stop(struct tb *tb)
 	struct tb_tunnel *n;
 
 	cancel_delayed_work(&tcm->remove_work);
+	cancel_delayed_work(&tcm->tunnel_dp_work);
 	/* tunnels are only present after everything has been initialized */
 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
 		/*
@@ -3361,6 +3382,7 @@ struct tb *tb_probe(struct tb_nhi *nhi)
 	INIT_LIST_HEAD(&tcm->tunnel_list);
 	INIT_LIST_HEAD(&tcm->dp_resources);
 	INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
+	INIT_DELAYED_WORK(&tcm->tunnel_dp_work, tb_tunnel_dp_work);
 	tb_init_bandwidth_groups(tcm);
 
 	tb_dbg(tb, "using software connection manager\n");
-- 
2.50.1


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

* [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (2 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:28   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume Mika Westerberg
                   ` (13 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

tb_dp_dprx_work() decides the outcome of the DPRX read under tb->lock
but then releases the lock and calls the callback which takes it again.
Between the two something else can grab the lock and change the domain
state, for example stop the control channel, and the callback then acts
on a decision that is no longer valid.

Fix this by calling the callback in the same tb->lock critical section
that decided the DPRX result.

Fixes: d6d458d42e1e ("thunderbolt: Handle DisplayPort tunnel activation asynchronously")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c     | 4 ----
 drivers/thunderbolt/tunnel.c | 4 ++--
 2 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index acad8461011c..3c4903adb687 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -1930,8 +1930,6 @@ static void tb_dp_tunnel_active(struct tb_tunnel *tunnel)
 	struct tb_port *out = tunnel->dst_port;
 	struct tb *tb = tunnel->tb;
 
-	mutex_lock(&tb->lock);
-
 	/*
 	 * If the DPRX read was canceled the tunnel is already being torn
 	 * down by whoever canceled it. Do not touch the adapters here
@@ -1939,7 +1937,6 @@ static void tb_dp_tunnel_active(struct tb_tunnel *tunnel)
 	 */
 	if (tunnel->dprx_canceled) {
 		tb_tunnel_dbg(tunnel, "DPRX read canceled, not activating\n");
-		mutex_unlock(&tb->lock);
 		return;
 	}
 
@@ -1996,7 +1993,6 @@ static void tb_dp_tunnel_active(struct tb_tunnel *tunnel)
 		tb_tunnel_warn(tunnel, "not active, tearing down\n");
 		tb_dp_resource_unavailable(tb, in, "DPRX negotiation failed");
 	}
-	mutex_unlock(&tb->lock);
 }
 
 static void tb_tunnel_one_dp(struct tb *tb, struct tb_port *in,
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 8145657835c5..90bc6e42d89b 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -1115,9 +1115,9 @@ static void tb_dp_dprx_work(struct work_struct *work)
 			tb_tunnel_set_active(tunnel, true);
 		}
 	}
+	tunnel->callback(tunnel);
 	mutex_unlock(&tb->lock);
 
-	tunnel->callback(tunnel);
 	tb_tunnel_put(tunnel);
 	tb_domain_put(tb);
 }
@@ -1703,7 +1703,7 @@ struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
  * once the tunnel has been fully activated. It can call
  * tb_tunnel_is_active() to check if activation was successful (or if it
  * returns %false there was some sort of issue). The @callback is called
- * without @tb->lock held.
+ * with @tb->lock held.
  *
  * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
  */
-- 
2.50.1


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

* [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (3 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:35   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running Mika Westerberg
                   ` (12 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Alpine Ridge and Titan Ridge both require special flow to get the xHCI
enabled, and CL0s working. We do that when the PCIe tunnel is created
but forgot to do that when we resume from system sleep. Fix this by
applying these after the PCIe tunnel has been re-activated.

Fixes: 30a4eca69b76 ("thunderbolt: Add internal xHCI connect flows for Thunderbolt 3 devices")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 3c4903adb687..6a11d686bdf1 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -2315,6 +2315,21 @@ static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
 	return 0;
 }
 
+static void tb_pci_post_activate(struct tb_tunnel *tunnel)
+{
+	struct tb_switch *sw = tunnel->dst_port->sw;
+
+	/*
+	 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
+	 * here.
+	 */
+	if (tb_switch_pcie_l1_enable(sw))
+		tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
+
+	if (tb_switch_xhci_connect(sw))
+		tb_sw_warn(sw, "failed to connect xHCI\n");
+}
+
 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
 {
 	struct tb_port *up, *down, *port;
@@ -2345,15 +2360,7 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
 		return -EIO;
 	}
 
-	/*
-	 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
-	 * here.
-	 */
-	if (tb_switch_pcie_l1_enable(sw))
-		tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
-
-	if (tb_switch_xhci_connect(sw))
-		tb_sw_warn(sw, "failed to connect xHCI\n");
+	tb_pci_post_activate(tunnel);
 
 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
 	return 0;
@@ -3239,6 +3246,8 @@ static int tb_resume_noirq(struct tb *tb)
 			usb3_delay = 0;
 		}
 		tb_tunnel_activate(tunnel);
+		if (tb_tunnel_is_pci(tunnel))
+			tb_pci_post_activate(tunnel);
 	}
 	if (!list_empty(&tcm->tunnel_list)) {
 		/*
-- 
2.50.1


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

* [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (4 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:25   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable() Mika Westerberg
                   ` (11 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Use -ESHUTDOWN like the ring layer does when the control channel is not
running, and drop the WARN(). On buses like Thunderbolt/USB4 user can
unplug a device any time so it is expected that control channel reads
and writes fail and the callers are supposed to handle that.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/ctl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
index 965988b18608..c8b07dd01bdf 100644
--- a/drivers/thunderbolt/ctl.c
+++ b/drivers/thunderbolt/ctl.c
@@ -140,7 +140,7 @@ static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
 	guard(mutex)(&ctl->request_queue_lock);
 	if (!ctl->running) {
 		tb_cfg_request_put(req);
-		return -ENOTCONN;
+		return -ESHUTDOWN;
 	}
 	req->ctl = ctl;
 	list_add_tail(&req->list, &ctl->request_queue);
@@ -1127,7 +1127,6 @@ int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
 		break;
 
 	default:
-		WARN(1, "tb_cfg_read: %d\n", res.err);
 		break;
 	}
 	return res.err;
@@ -1153,7 +1152,6 @@ int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
 		break;
 
 	default:
-		WARN(1, "tb_cfg_write: %d\n", res.err);
 		break;
 	}
 	return res.err;
-- 
2.50.1


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

* [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable()
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (5 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:22   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host() Mika Westerberg
                   ` (10 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

This is the opposite of usb4_port_hotplug_enable() and allows the
connection manager to mask hot plug events of a USB4 port. This is
needed in subsequent patches.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tb.h   |  1 +
 drivers/thunderbolt/usb4.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index e86845762e22..727e33992e41 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -1358,6 +1358,7 @@ void usb4_switch_remove_ports(struct tb_switch *sw);
 
 int usb4_port_unlock(struct tb_port *port);
 int usb4_port_hotplug_enable(struct tb_port *port);
+int usb4_port_hotplug_disable(struct tb_port *port);
 int usb4_port_reset(struct tb_port *port);
 int usb4_port_configure(struct tb_port *port);
 void usb4_port_unconfigure(struct tb_port *port);
diff --git a/drivers/thunderbolt/usb4.c b/drivers/thunderbolt/usb4.c
index c9b3931c45ed..519aeca66d6f 100644
--- a/drivers/thunderbolt/usb4.c
+++ b/drivers/thunderbolt/usb4.c
@@ -1164,6 +1164,27 @@ int usb4_port_hotplug_enable(struct tb_port *port)
 	return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
 }
 
+/**
+ * usb4_port_hotplug_disable() - Disables hotplug for a port
+ * @port: USB4 port to operate on
+ *
+ * The opposite of usb4_port_hotplug_enable().
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+int usb4_port_hotplug_disable(struct tb_port *port)
+{
+	int ret;
+	u32 val;
+
+	ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
+	if (ret)
+		return ret;
+
+	val |= ADP_CS_5_DHP;
+	return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
+}
+
 /**
  * usb4_port_reset() - Issue downstream port reset
  * @port: USB4 port to reset
-- 
2.50.1


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

* [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host()
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (6 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable() Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:25   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 09/18] thunderbolt: Add tb_port_state_is_up() Mika Westerberg
                   ` (9 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

We are going to call it from tb_switch_reset_host() in a subsequent
patch so move it there. No functional changes.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/switch.c | 80 ++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 40 deletions(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index d22d8db8f890..c748158a7044 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -1598,6 +1598,46 @@ static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
 	       regs->__unknown1, regs->__unknown4);
 }
 
+/*
+ * tb_plug_events_active() - enable/disable plug events on a switch
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+static int tb_plug_events_active(struct tb_switch *sw, bool active)
+{
+	u32 data;
+	int res;
+
+	if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
+		return 0;
+
+	res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
+	if (res)
+		return res;
+
+	if (active) {
+		data = data & 0xFFFFFF83;
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
+		case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
+		case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
+			break;
+		default:
+			/*
+			 * Skip Alpine Ridge, it needs to have vendor
+			 * specific USB hotplug event enabled for the
+			 * internal xHCI to work.
+			 */
+			if (!tb_switch_is_alpine_ridge(sw))
+				data |= TB_PLUG_EVENTS_USB_DISABLE;
+		}
+	} else {
+		data = data | 0x7c;
+	}
+	return tb_sw_write(sw, &data, TB_CFG_SWITCH,
+			   sw->cap_plug_events + 1, 1);
+}
+
 static int tb_switch_reset_host(struct tb_switch *sw)
 {
 	if (sw->generation > 1) {
@@ -1762,46 +1802,6 @@ int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
 	return -ETIMEDOUT;
 }
 
-/*
- * tb_plug_events_active() - enable/disable plug events on a switch
- *
- * Return: %0 on success, negative errno otherwise.
- */
-static int tb_plug_events_active(struct tb_switch *sw, bool active)
-{
-	u32 data;
-	int res;
-
-	if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
-		return 0;
-
-	res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
-	if (res)
-		return res;
-
-	if (active) {
-		data = data & 0xFFFFFF83;
-		switch (sw->config.device_id) {
-		case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
-		case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
-		case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
-			break;
-		default:
-			/*
-			 * Skip Alpine Ridge, it needs to have vendor
-			 * specific USB hotplug event enabled for the
-			 * internal xHCI to work.
-			 */
-			if (!tb_switch_is_alpine_ridge(sw))
-				data |= TB_PLUG_EVENTS_USB_DISABLE;
-		}
-	} else {
-		data = data | 0x7c;
-	}
-	return tb_sw_write(sw, &data, TB_CFG_SWITCH,
-			   sw->cap_plug_events + 1, 1);
-}
-
 static ssize_t authorized_show(struct device *dev,
 			       struct device_attribute *attr,
 			       char *buf)
-- 
2.50.1


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

* [PATCH 09/18] thunderbolt: Add tb_port_state_is_up()
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (7 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host() Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:22   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 10/18] thunderbolt: Split host router port reset into a separate function Mika Westerberg
                   ` (8 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Move the check whether the link of a lane adapter is up from
tb_wait_for_port() into a helper function. We are going to use it in
subsequent patches. No functional changes.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/switch.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index c748158a7044..26a4363f94f8 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -481,6 +481,20 @@ int tb_port_state(struct tb_port *port)
 	return phy.state;
 }
 
+static bool tb_port_state_is_up(enum tb_port_state state)
+{
+	switch (state) {
+	case TB_PORT_UP:
+	case TB_PORT_TX_CL0S:
+	case TB_PORT_RX_CL0S:
+	case TB_PORT_CL1:
+	case TB_PORT_CL2:
+		return true;
+	default:
+		return false;
+	}
+}
+
 /**
  * tb_wait_for_port() - wait for a port to become ready
  * @port: Port to wait
@@ -529,15 +543,12 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
 			tb_port_dbg(port, "is unplugged (state: 7)\n");
 			return 0;
 
-		case TB_PORT_UP:
-		case TB_PORT_TX_CL0S:
-		case TB_PORT_RX_CL0S:
-		case TB_PORT_CL1:
-		case TB_PORT_CL2:
-			tb_port_dbg(port, "is connected, link is up (state: %d)\n", state);
-			return 1;
-
 		default:
+			if (tb_port_state_is_up(state)) {
+				tb_port_dbg(port, "is connected, link is up (state: %d)\n",
+					    state);
+				return 1;
+			}
 			if (state < 0)
 				return state;
 
-- 
2.50.1


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

* [PATCH 10/18] thunderbolt: Split host router port reset into a separate function
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (8 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 09/18] thunderbolt: Add tb_port_state_is_up() Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:22   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk Mika Westerberg
                   ` (7 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Move the part of tb_switch_reset_host() that resets the host router
lane adapters and cleans up the protocol adapter path config spaces into
a separate function tb_switch_reset_host_ports(). This makes it easier
to extend tb_switch_reset_host() in subsequent patches. No functional
changes.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/switch.c | 88 ++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 39 deletions(-)

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 26a4363f94f8..85a2aa67cbec 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -1649,53 +1649,63 @@ static int tb_plug_events_active(struct tb_switch *sw, bool active)
 			   sw->cap_plug_events + 1, 1);
 }
 
-static int tb_switch_reset_host(struct tb_switch *sw)
+static int tb_switch_reset_host_ports(struct tb_switch *sw)
 {
-	if (sw->generation > 1) {
-		struct tb_port *port;
+	struct tb_port *port;
 
-		tb_switch_for_each_port(sw, port) {
-			int i, ret;
+	tb_switch_for_each_port(sw, port) {
+		int i, ret;
 
+		/*
+		 * For lane adapters we issue downstream port reset and
+		 * clear up path config spaces.
+		 *
+		 * For protocol adapters we disable the path and clear
+		 * path config space one by one (from 8 to Max Input
+		 * HopID of the adapter).
+		 */
+		if (tb_port_is_null(port) && !tb_is_upstream_port(port)) {
+			ret = tb_port_reset(port);
+			if (ret)
+				return ret;
 			/*
-			 * For lane adapters we issue downstream port
-			 * reset and clear up path config spaces.
-			 *
-			 * For protocol adapters we disable the path and
-			 * clear path config space one by one (from 8 to
-			 * Max Input HopID of the adapter).
+			 * USB4 Lane 1 adapters do not have accessible
+			 * path config space.
 			 */
-			if (tb_port_is_null(port) && !tb_is_upstream_port(port)) {
-				ret = tb_port_reset(port);
-				if (ret)
-					return ret;
-				/*
-				 * USB4 Lane 1 adapters do not have accessible
-				 * path config space.
-				 */
-				if (tb_switch_is_usb4(sw) && !port->usb4)
-					continue;
-			} else if (tb_port_is_usb3_down(port) ||
-				   tb_port_is_usb3_up(port)) {
-				tb_usb3_port_enable(port, false);
-			} else if (tb_port_is_dpin(port) ||
-				   tb_port_is_dpout(port)) {
-				tb_dp_port_enable(port, false);
-			} else if (tb_port_is_pcie_down(port) ||
-				   tb_port_is_pcie_up(port)) {
-				tb_pci_port_enable(port, false);
-			} else {
+			if (tb_switch_is_usb4(sw) && !port->usb4)
 				continue;
-			}
+		} else if (tb_port_is_usb3_down(port) ||
+			   tb_port_is_usb3_up(port)) {
+			tb_usb3_port_enable(port, false);
+		} else if (tb_port_is_dpin(port) || tb_port_is_dpout(port)) {
+			tb_dp_port_enable(port, false);
+		} else if (tb_port_is_pcie_down(port) ||
+			   tb_port_is_pcie_up(port)) {
+			tb_pci_port_enable(port, false);
+		} else {
+			continue;
+		}
 
-			/* Cleanup path config space of protocol adapter */
-			for (i = TB_PATH_MIN_HOPID;
-			     i <= port->config.max_in_hop_id; i++) {
-				ret = tb_path_deactivate_hop(port, i);
-				if (ret)
-					return ret;
-			}
+		/* Cleanup path config space of protocol adapter */
+		for (i = TB_PATH_MIN_HOPID;
+		     i <= port->config.max_in_hop_id; i++) {
+			ret = tb_path_deactivate_hop(port, i);
+			if (ret)
+				return ret;
 		}
+	}
+
+	return 0;
+}
+
+static int tb_switch_reset_host(struct tb_switch *sw)
+{
+	if (sw->generation > 1) {
+		int ret;
+
+		ret = tb_switch_reset_host_ports(sw);
+		if (ret)
+			return ret;
 	} else {
 		struct tb_cfg_result res;
 
-- 
2.50.1


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

* [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (9 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 10/18] thunderbolt: Split host router port reset into a separate function Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:44   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation Mika Westerberg
                   ` (6 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

The hibernation image is taken with all tunnels up, so after restore the
tunnels the restore kernel (or boot firmware) created may collide with
the resources the image still holds. Instead of discovering and tearing
those down, reset the host router on restore and re-create the tunnels
from the image. DP tunnels are dropped and re-established from plug
events just like we do on system suspend.

Links can take some time to retrain after reset so we wait for them to
retrain before resuming the connected routers (and we mask the hotplug
during that time). Because the plug events are masked we perform rescan
in complete phase to pick up devices replaced during hibernation (or S3
for older Apple devices).

DPRX polling is put on hold while the control channel is stopped so that
it does not pointlessly try to access the hardware during hibernation.

Cc: Rene Sapiens <rene.sapiens@linux.intel.com>
Cc: Pooja Katiyar <pooja.katiyar@intel.com>
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/domain.c |  5 ++-
 drivers/thunderbolt/nhi.c    | 17 ++++++--
 drivers/thunderbolt/switch.c | 76 ++++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/tb.c     | 64 ++++++++++++++++--------------
 drivers/thunderbolt/tb.h     |  4 +-
 drivers/thunderbolt/tunnel.c | 32 ++++++++++++---
 6 files changed, 155 insertions(+), 43 deletions(-)

diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 24611f05b3cd..f9951d4311cb 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -545,20 +545,21 @@ int tb_domain_suspend_noirq(struct tb *tb)
 /**
  * tb_domain_resume_noirq() - Resume a domain
  * @tb: Domain to resume
+ * @hibernate: Are we resuming from hibernation
  *
  * Re-starts the control channel, and resumes all devices connected to
  * the domain.
  *
  * Return: %0 on success, negative errno otherwise.
  */
-int tb_domain_resume_noirq(struct tb *tb)
+int tb_domain_resume_noirq(struct tb *tb, bool hibernate)
 {
 	int ret = 0;
 
 	mutex_lock(&tb->lock);
 	tb_ctl_start(tb->ctl);
 	if (tb->cm_ops->resume_noirq)
-		ret = tb->cm_ops->resume_noirq(tb);
+		ret = tb->cm_ops->resume_noirq(tb, hibernate);
 	mutex_unlock(&tb->lock);
 
 	return ret;
diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index f54ab2189cf4..cfb782f3233a 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -1139,9 +1139,8 @@ static int nhi_poweroff_noirq(struct device *dev)
 	return __nhi_suspend_noirq(dev, wakeup);
 }
 
-static int nhi_resume_noirq(struct device *dev)
+static int __nhi_resume_noirq(struct tb *tb, bool hibernate)
 {
-	struct tb *tb = dev_get_drvdata(dev);
 	struct tb_nhi *nhi = tb->nhi;
 	int ret;
 
@@ -1158,7 +1157,17 @@ static int nhi_resume_noirq(struct device *dev)
 			return ret;
 	}
 
-	return tb_domain_resume_noirq(tb);
+	return tb_domain_resume_noirq(tb, hibernate);
+}
+
+static int nhi_resume_noirq(struct device *dev)
+{
+	return __nhi_resume_noirq(dev_get_drvdata(dev), false);
+}
+
+static int nhi_restore_noirq(struct device *dev)
+{
+	return __nhi_resume_noirq(dev_get_drvdata(dev), true);
 }
 
 static int nhi_suspend(struct device *dev)
@@ -1396,7 +1405,7 @@ const struct dev_pm_ops nhi_pm_ops = {
 					    * pci-tunnels stay alive.
 					    */
 	.thaw_noirq = nhi_thaw_noirq,
-	.restore_noirq = nhi_resume_noirq,
+	.restore_noirq = nhi_restore_noirq,
 	.suspend = nhi_suspend,
 	.poweroff_noirq = nhi_poweroff_noirq,
 	.poweroff = nhi_suspend,
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 85a2aa67cbec..216681d2bb8f 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -21,6 +21,8 @@
 
 /* How long a hop is given to drain when a path is deactivated */
 #define TB_PORT_PENDING_TIMEOUT		500 /* ms */
+/* Maximum time we wait after link reset */
+#define TB_RESET_LINK_TIMEOUT		5000 /* ms */
 
 /* Switch NVM support */
 
@@ -495,6 +497,11 @@ static bool tb_port_state_is_up(enum tb_port_state state)
 	}
 }
 
+static inline bool tb_port_link_is_up(struct tb_port *port)
+{
+	return tb_port_state_is_up(tb_port_state(port));
+}
+
 /**
  * tb_wait_for_port() - wait for a port to become ready
  * @port: Port to wait
@@ -1649,6 +1656,15 @@ static int tb_plug_events_active(struct tb_switch *sw, bool active)
 			   sw->cap_plug_events + 1, 1);
 }
 
+/*
+ * USB4 ports whose link is expected to come back after host reset
+ * because something was connected before.
+ */
+static bool tb_port_should_reconnect(const struct tb_port *port)
+{
+	return tb_port_has_remote(port) || (port->xdomain && port->usb4);
+}
+
 static int tb_switch_reset_host_ports(struct tb_switch *sw)
 {
 	struct tb_port *port;
@@ -1698,12 +1714,63 @@ static int tb_switch_reset_host_ports(struct tb_switch *sw)
 	return 0;
 }
 
+static void tb_switch_wait_for_reconnect(struct tb_switch *sw)
+{
+	ktime_t timeout = ktime_add_ms(ktime_get(), TB_RESET_LINK_TIMEOUT);
+	struct tb_port *port;
+
+	tb_switch_for_each_port(sw, port) {
+		if (!tb_port_should_reconnect(port))
+			continue;
+
+		/*
+		 * It's fine to wait just for one port. The others train
+		 * in parallel.
+		 */
+		while (!tb_port_link_is_up(port)) {
+			if (ktime_after(ktime_get(), timeout)) {
+				tb_port_dbg(port, "link not up after reset\n");
+				break;
+			}
+			msleep(20);
+		}
+	}
+}
+
+static void tb_switch_hotplug_disable(struct tb_switch *sw)
+{
+	struct tb_port *port;
+
+	tb_plug_events_active(sw, false);
+	tb_switch_for_each_port(sw, port) {
+		if (!port->cap_usb4 || !tb_port_should_reconnect(port))
+			continue;
+		usb4_port_hotplug_disable(port);
+	}
+}
+
+static void tb_switch_hotplug_enable(struct tb_switch *sw)
+{
+	struct tb_port *port;
+
+	tb_switch_for_each_port(sw, port) {
+		if (!port->cap_usb4 || !tb_port_should_reconnect(port))
+			continue;
+		usb4_port_hotplug_enable(port);
+	}
+	tb_plug_events_active(sw, true);
+}
+
 static int tb_switch_reset_host(struct tb_switch *sw)
 {
 	if (sw->generation > 1) {
 		int ret;
 
+		tb_switch_hotplug_disable(sw);
 		ret = tb_switch_reset_host_ports(sw);
+		if (!ret)
+			tb_switch_wait_for_reconnect(sw);
+		tb_switch_hotplug_enable(sw);
 		if (ret)
 			return ret;
 	} else {
@@ -3618,6 +3685,15 @@ int tb_switch_resume(struct tb_switch *sw, bool runtime)
 	/* Disable wakes */
 	tb_switch_set_wake(sw, 0, true);
 
+	/*
+	 * Restore link back to dual if it was asymmetric. This will be
+	 * updated once DP tunnels are created.
+	 */
+	if (sw->link_width > TB_LINK_WIDTH_DUAL)
+		sw->link_width = TB_LINK_WIDTH_DUAL;
+
+	tb_switch_clx_init(sw);
+
 	err = tb_switch_tmu_init(sw);
 	if (err)
 		return err;
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 6a11d686bdf1..6c1ca4b5ffdd 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -57,6 +57,7 @@ MODULE_PARM_DESC(asym_threshold,
  *		    events and exit if this is not set (it needs to
  *		    acquire the lock one more time). Used to drain wq
  *		    after cfg has been paused.
+ * @rescan_on_complete: Topology rescan is needed in tb_complete()
  * @remove_work: Work used to remove any unplugged routers after
  *		 runtime resume
  * @tunnel_dp_work: Work used to delay DP tunnel creation
@@ -66,6 +67,7 @@ struct tb_cm {
 	struct list_head tunnel_list;
 	struct list_head dp_resources;
 	bool hotplug_active;
+	bool rescan_on_complete;
 	struct delayed_work remove_work;
 	struct delayed_work tunnel_dp_work;
 	struct tb_bandwidth_group groups[MAX_GROUPS];
@@ -3201,21 +3203,24 @@ static void tb_free_unplugged_xdomains(struct tb_switch *sw)
 	}
 }
 
-static int tb_resume_noirq(struct tb *tb)
+static int tb_resume_noirq(struct tb *tb, bool hibernate)
 {
 	struct tb_cm *tcm = tb_priv(tb);
 	struct tb_tunnel *tunnel, *n;
-	unsigned int usb3_delay = 0;
-	LIST_HEAD(tunnels);
+	bool usb3_delay = false;
 
 	tb_dbg(tb, "resuming...\n");
 
 	/*
-	 * For non-USB4 hosts (Apple systems) remove any PCIe devices
-	 * the firmware might have setup.
+	 * Resume from suspend-to-disk or non-USB4 Apple systems reset
+	 * the topology to get rid of the tunnels the restore kernel or
+	 * boot firmware created.
 	 */
-	if (!tb_switch_is_usb4(tb->root_switch))
+	if (hibernate || !tb_switch_is_usb4(tb->root_switch)) {
 		tb_switch_reset(tb->root_switch);
+		/* Plug events were masked over domain reset */
+		tcm->rescan_on_complete = true;
+	}
 
 	tb_switch_resume(tb->root_switch, false);
 	tb_free_invalid_tunnels(tb);
@@ -3223,27 +3228,23 @@ static int tb_resume_noirq(struct tb *tb)
 	tb_free_unplugged_xdomains(tb->root_switch);
 	tb_restore_children(tb->root_switch);
 
-	/*
-	 * If we get here from suspend to disk the boot firmware or the
-	 * restore kernel might have created tunnels of its own. Since
-	 * we cannot be sure they are usable for us we find and tear
-	 * them down.
-	 */
-	tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
-	list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
-		if (tb_tunnel_is_usb3(tunnel))
-			usb3_delay = 500;
-		tb_tunnel_deactivate(tunnel);
-		tb_tunnel_put(tunnel);
+	if (hibernate) {
+		tb_disconnect_and_release_dp(tb);
+		tb_add_dp_resources(tb->root_switch);
+		tb_switch_exit_redrive(tb->root_switch);
+		usb3_delay = true;
 	}
 
 	/* Re-create our tunnels now */
 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
-		/* USB3 requires delay before it can be re-activated */
-		if (tb_tunnel_is_usb3(tunnel)) {
-			msleep(usb3_delay);
-			/* Only need to do it once */
-			usb3_delay = 0;
+		/*
+		 * USB3 requires delay before it can be re-activated.
+		 * If we have it saved assume the restore kernel had it
+		 * as well.
+		 */
+		if (usb3_delay && tb_tunnel_is_usb3(tunnel)) {
+			msleep(500);
+			usb3_delay = false;
 		}
 		tb_tunnel_activate(tunnel);
 		if (tb_tunnel_is_pci(tunnel))
@@ -3283,14 +3284,19 @@ static int tb_thaw_noirq(struct tb *tb)
 
 static void tb_complete(struct tb *tb)
 {
+	struct tb_cm *tcm = tb_priv(tb);
+	bool rescan;
+
+	rescan = tb_domain_unregister_unplugged_xdomains(tb);
+
+	guard(mutex)(&tb->lock);
 	/*
-	 * Unregister unplugged XDomains and if there is a case where
-	 * another domain is swapped in place of unplugged XDomain we
-	 * need to run another rescan.
+	 * Rescan if an unplugged XDomain was replaced by another
+	 * domain, or after host router reset.
 	 */
-	if (tb_domain_unregister_unplugged_xdomains(tb)) {
-		scoped_guard(mutex, &tb->lock)
-			tb_scan_switch(tb->root_switch);
+	if (rescan || tcm->rescan_on_complete) {
+		tcm->rescan_on_complete = false;
+		tb_scan_switch(tb->root_switch);
 	}
 }
 
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 727e33992e41..7f47a5c3c0d3 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -513,7 +513,7 @@ struct tb_cm_ops {
 	void (*stop)(struct tb *tb);
 	void (*deinit)(struct tb *tb);
 	int (*suspend_noirq)(struct tb *tb);
-	int (*resume_noirq)(struct tb *tb);
+	int (*resume_noirq)(struct tb *tb, bool hibernate);
 	int (*suspend)(struct tb *tb);
 	int (*freeze_noirq)(struct tb *tb);
 	int (*thaw_noirq)(struct tb *tb);
@@ -777,7 +777,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize
 int tb_domain_add(struct tb *tb, bool reset);
 void tb_domain_remove(struct tb *tb);
 int tb_domain_suspend_noirq(struct tb *tb);
-int tb_domain_resume_noirq(struct tb *tb);
+int tb_domain_resume_noirq(struct tb *tb, bool hibernate);
 int tb_domain_suspend(struct tb *tb);
 int tb_domain_freeze_noirq(struct tb *tb);
 int tb_domain_thaw_noirq(struct tb *tb);
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 90bc6e42d89b..9d831a7a1ced 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -1090,6 +1090,24 @@ static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec)
 	return -ETIMEDOUT;
 }
 
+static int tb_dp_dprx_poll(struct tb_tunnel *tunnel)
+{
+	int ret;
+
+	ret = tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT);
+	switch (ret) {
+	case 0:
+		return 0;
+	case -ESHUTDOWN:
+		/* Control channel is frozen, ignore the timeout */
+		return -EINPROGRESS;
+	default:
+		if (ktime_before(ktime_get(), tunnel->dprx_timeout))
+			return -EINPROGRESS;
+		return ret;
+	}
+}
+
 static void tb_dp_dprx_work(struct work_struct *work)
 {
 	struct tb_tunnel *tunnel = container_of(work, typeof(*tunnel), dprx_work.work);
@@ -1103,17 +1121,19 @@ static void tb_dp_dprx_work(struct work_struct *work)
 	 */
 	mutex_lock(&tb->lock);
 	if (!tunnel->dprx_canceled) {
-		if (tb_dp_is_usb4(tunnel->src_port->sw) &&
-		    tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT)) {
-			if (ktime_before(ktime_get(), tunnel->dprx_timeout)) {
+		int ret = 0;
+
+		if (tb_dp_is_usb4(tunnel->src_port->sw)) {
+			ret = tb_dp_dprx_poll(tunnel);
+			if (ret == -EINPROGRESS) {
 				queue_delayed_work(tb->wq, &tunnel->dprx_work,
-						   msecs_to_jiffies(TB_DPRX_POLL_DELAY));
+					msecs_to_jiffies(TB_DPRX_POLL_DELAY));
 				mutex_unlock(&tb->lock);
 				return;
 			}
-		} else {
-			tb_tunnel_set_active(tunnel, true);
 		}
+		if (!ret)
+			tb_tunnel_set_active(tunnel, true);
 	}
 	tunnel->callback(tunnel);
 	mutex_unlock(&tb->lock);
-- 
2.50.1


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

* [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (10 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:26   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called Mika Westerberg
                   ` (5 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

The only user of tunnel discovery without HopID allocation was the
resume path from suspend-to-disk, which now resets the topology
instead. Remove the now unused alloc_hopid parameter and always
allocate HopIDs when discovering paths.

No functional changes.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/path.c   | 41 +++++++++++++++---------------------
 drivers/thunderbolt/tb.c     | 17 ++++++---------
 drivers/thunderbolt/tb.h     |  5 +----
 drivers/thunderbolt/tunnel.c | 26 +++++++++--------------
 drivers/thunderbolt/tunnel.h |  7 ++----
 5 files changed, 37 insertions(+), 59 deletions(-)

diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c
index 05249eed3f64..79c285dd29fb 100644
--- a/drivers/thunderbolt/path.c
+++ b/drivers/thunderbolt/path.c
@@ -85,12 +85,11 @@ static int tb_path_find_src_hopid(struct tb_port *src,
  * @dst_hopid: HopID to the @dst (%-1 if don't care)
  * @last: Last port is filled here if not %NULL
  * @name: Name of the path
- * @alloc_hopid: Allocate HopIDs for the ports
  *
  * Follows a path starting from @src and @src_hopid to the last output
- * port of the path. Allocates HopIDs for the visited ports (if
- * @alloc_hopid is true). Call tb_path_free() to release the path and
- * allocated HopIDs when the path is not needed anymore.
+ * port of the path. Allocates HopIDs for the visited ports. Call
+ * tb_path_free() to release the path and allocated HopIDs when the path
+ * is not needed anymore.
  *
  * Note function discovers also incomplete paths so caller should check
  * that the @dst port is the expected one. If it is not, the path can be
@@ -100,8 +99,7 @@ static int tb_path_find_src_hopid(struct tb_port *src,
  */
 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
 				 struct tb_port *dst, int dst_hopid,
-				 struct tb_port **last, const char *name,
-				 bool alloc_hopid)
+				 struct tb_port **last, const char *name)
 {
 	struct tb_port *out_port;
 	struct tb_regs_hop hop;
@@ -159,7 +157,6 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
 	path->name = name;
 	path->tb = src->sw->tb;
 	path->activated = true;
-	path->alloc_hopid = alloc_hopid;
 
 	tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n",
 	       path->name, tb_route(src->sw), src->port);
@@ -178,14 +175,13 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
 			goto err;
 		}
 
-		if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0)
+		if (tb_port_alloc_in_hopid(p, h, h) < 0)
 			goto err;
 
 		out_port = &sw->ports[hop.out_port];
 		next_hop = hop.next_hop;
 
-		if (alloc_hopid &&
-		    tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
+		if (tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
 			tb_port_release_in_hopid(p, h);
 			goto err;
 		}
@@ -261,7 +257,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
 		return NULL;
 
 	path->path_length = num_hops;
-	path->alloc_hopid = true;
 
 	in_hopid = src_hopid;
 	out_port = NULL;
@@ -344,19 +339,17 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
  */
 void tb_path_free(struct tb_path *path)
 {
-	if (path->alloc_hopid) {
-		int i;
-
-		for (i = 0; i < path->path_length; i++) {
-			const struct tb_path_hop *hop = &path->hops[i];
-
-			if (hop->in_port)
-				tb_port_release_in_hopid(hop->in_port,
-							 hop->in_hop_index);
-			if (hop->out_port)
-				tb_port_release_out_hopid(hop->out_port,
-							  hop->next_hop_index);
-		}
+	int i;
+
+	for (i = 0; i < path->path_length; i++) {
+		const struct tb_path_hop *hop = &path->hops[i];
+
+		if (hop->in_port)
+			tb_port_release_in_hopid(hop->in_port,
+						 hop->in_hop_index);
+		if (hop->out_port)
+			tb_port_release_out_hopid(hop->out_port,
+						  hop->next_hop_index);
 	}
 
 	kfree(path);
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 6c1ca4b5ffdd..10fca534ff78 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -382,8 +382,7 @@ static int tb_enable_tmu(struct tb_switch *sw)
 }
 
 static void tb_switch_discover_tunnels(struct tb_switch *sw,
-				       struct list_head *list,
-				       bool alloc_hopids)
+				       struct list_head *list)
 {
 	struct tb *tb = sw->tb;
 	struct tb_port *port;
@@ -393,17 +392,17 @@ static void tb_switch_discover_tunnels(struct tb_switch *sw,
 
 		switch (port->config.type) {
 		case TB_TYPE_DP_HDMI_IN:
-			tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids,
+			tunnel = tb_tunnel_discover_dp(tb, port,
 						       tb_dp_tunnel_active);
 			tb_increase_tmu_accuracy(tunnel);
 			break;
 
 		case TB_TYPE_PCIE_DOWN:
-			tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
+			tunnel = tb_tunnel_discover_pci(tb, port);
 			break;
 
 		case TB_TYPE_USB3_DOWN:
-			tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
+			tunnel = tb_tunnel_discover_usb3(tb, port);
 			break;
 
 		default:
@@ -415,10 +414,8 @@ static void tb_switch_discover_tunnels(struct tb_switch *sw,
 	}
 
 	tb_switch_for_each_port(sw, port) {
-		if (tb_port_has_remote(port)) {
-			tb_switch_discover_tunnels(port->remote->sw, list,
-						   alloc_hopids);
-		}
+		if (tb_port_has_remote(port))
+			tb_switch_discover_tunnels(port->remote->sw, list);
 	}
 }
 
@@ -1719,7 +1716,7 @@ static void tb_discover_tunnels(struct tb *tb)
 	struct tb_cm *tcm = tb_priv(tb);
 	struct tb_tunnel *tunnel;
 
-	tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
+	tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list);
 
 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
 		if (tb_tunnel_is_pci(tunnel)) {
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 7f47a5c3c0d3..c31ede3244ac 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -423,7 +423,6 @@ enum tb_path_port {
  * @clear_fc: Clear all flow control from the path config space entries
  *	      when deactivating this path
  * @path_length: How many hops the path uses
- * @alloc_hopid: Does this path consume port HopID
  * @hops: Path hops
  *
  * A path consists of a number of hops (see &struct tb_path_hop). To
@@ -444,7 +443,6 @@ struct tb_path {
 	bool activated;
 	bool clear_fc;
 	int path_length;
-	bool alloc_hopid;
 
 	struct tb_path_hop hops[] __counted_by(path_length);
 };
@@ -1194,8 +1192,7 @@ int tb_dp_port_enable(struct tb_port *port, bool enable);
 
 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
 				 struct tb_port *dst, int dst_hopid,
-				 struct tb_port **last, const char *name,
-				 bool alloc_hopid);
+				 struct tb_port **last, const char *name);
 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
 			      struct tb_port *dst, int dst_hopid, int link_nr,
 			      const char *name);
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 9d831a7a1ced..c4e624c94bb0 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -446,15 +446,13 @@ static int tb_pci_init_path(struct tb_path *path)
  * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
  * @tb: Pointer to the domain structure
  * @down: PCIe downstream adapter
- * @alloc_hopid: Allocate HopIDs from visited ports
  *
  * If @down adapter is active, follows the tunnel to the PCIe upstream
  * adapter and back.
  *
  * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
  */
-struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
-					 bool alloc_hopid)
+struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down)
 {
 	struct tb_tunnel *tunnel;
 	struct tb_path *path;
@@ -475,7 +473,7 @@ struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
 	 * case.
 	 */
 	path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
-				&tunnel->dst_port, "PCIe Up", alloc_hopid);
+				&tunnel->dst_port, "PCIe Up");
 	if (!path) {
 		/* Just disable the downstream port */
 		tb_pci_port_enable(down, false);
@@ -486,7 +484,7 @@ struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
 		goto err_free;
 
 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
-				"PCIe Down", alloc_hopid);
+				"PCIe Down");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_PCI_PATH_DOWN] = path;
@@ -1610,7 +1608,6 @@ static void tb_dp_dump(struct tb_tunnel *tunnel)
  * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
  * @tb: Pointer to the domain structure
  * @in: DP in adapter
- * @alloc_hopid: Allocate HopIDs from visited ports
  * @callback: Callback that is called when the DP tunnel is fully
  *	      activated (or there is an error)
  *
@@ -1621,7 +1618,6 @@ static void tb_dp_dump(struct tb_tunnel *tunnel)
  * Return: Pointer to &struct tb_tunnel or %NULL if no tunnel found.
  */
 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
-					bool alloc_hopid,
 					void (*callback)(struct tb_tunnel *))
 {
 	struct tb_tunnel *tunnel;
@@ -1650,7 +1646,7 @@ struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
 	INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work);
 
 	path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
-				&tunnel->dst_port, "Video", alloc_hopid);
+				&tunnel->dst_port, "Video");
 	if (!path) {
 		/* Just disable the DP IN port */
 		tb_dp_port_enable(in, false);
@@ -1660,15 +1656,15 @@ struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
 	if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false))
 		goto err_free;
 
-	path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
-				alloc_hopid);
+	path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL,
+				"AUX TX");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
 	tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false);
 
 	path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
-				&port, "AUX RX", alloc_hopid);
+				&port, "AUX RX");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_DP_AUX_PATH_IN] = path;
@@ -2237,15 +2233,13 @@ static void tb_usb3_init_path(struct tb_path *path)
  * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
  * @tb: Pointer to the domain structure
  * @down: USB3 downstream adapter
- * @alloc_hopid: Allocate HopIDs from visited ports
  *
  * If @down adapter is active, follows the tunnel to the USB3 upstream
  * adapter and back.
  *
  * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
  */
-struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
-					  bool alloc_hopid)
+struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down)
 {
 	struct tb_tunnel *tunnel;
 	struct tb_path *path;
@@ -2266,7 +2260,7 @@ struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
 	 * case.
 	 */
 	path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
-				&tunnel->dst_port, "USB3 Down", alloc_hopid);
+				&tunnel->dst_port, "USB3 Down");
 	if (!path) {
 		/* Just disable the downstream port */
 		tb_usb3_port_enable(down, false);
@@ -2276,7 +2270,7 @@ struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
 	tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
 
 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
-				"USB3 Up", alloc_hopid);
+				"USB3 Up");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_USB3_PATH_UP] = path;
diff --git a/drivers/thunderbolt/tunnel.h b/drivers/thunderbolt/tunnel.h
index 28f49f7e9f56..35d026c5bc96 100644
--- a/drivers/thunderbolt/tunnel.h
+++ b/drivers/thunderbolt/tunnel.h
@@ -108,14 +108,12 @@ struct tb_tunnel {
 	struct tb_path *paths[] __counted_by(npaths);
 };
 
-struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
-					 bool alloc_hopid);
+struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down);
 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
 				      struct tb_port *down);
 bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
 			    int *reserved_down);
 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
-					bool alloc_hopid,
 					void (*callback)(struct tb_tunnel *));
 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
 				     struct tb_port *out, int link_nr,
@@ -127,8 +125,7 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 				      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_discover_usb3(struct tb *tb, struct tb_port *down,
-					  bool alloc_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,
 				       int max_down);
-- 
2.50.1


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

* [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (11 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:34   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests Mika Westerberg
                   ` (4 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

When tb_tunnel_put() is called with last reference it will call
tb_tunnel_destroy() to release the memory. In addition to this it also
touches adapter states (releasing the HopIDs and dma_credits). All other
users do this under the domain lock except tb_dp_dprx_work() which drops
the reference without it. Fix that here.

We can drop tb_tunnel_lock too because all the existing users have a
valid reference already when they call kref_get()/put().

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tunnel.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index c4e624c94bb0..af10c67997be 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -112,9 +112,6 @@ static const char * const tb_event_names[] = {
 	[TB_TUNNEL_NO_BANDWIDTH] = "insufficient bandwidth",
 };
 
-/* Synchronizes kref_get()/put() of struct tb_tunnel */
-static DEFINE_MUTEX(tb_tunnel_lock);
-
 static inline unsigned int tb_usable_credits(const struct tb_port *port)
 {
 	return port->total_credits - port->ctl_credits;
@@ -200,9 +197,7 @@ static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
 
 static void tb_tunnel_get(struct tb_tunnel *tunnel)
 {
-	mutex_lock(&tb_tunnel_lock);
 	kref_get(&tunnel->kref);
-	mutex_unlock(&tb_tunnel_lock);
 }
 
 static void tb_tunnel_destroy(struct kref *kref)
@@ -221,11 +216,16 @@ static void tb_tunnel_destroy(struct kref *kref)
 	kfree(tunnel);
 }
 
+/**
+ * tb_tunnel_put() - Drop tunnel reference
+ * @tunnel: Tunnel to release
+ *
+ * Drops tunnel reference and if it is last one releases the object.
+ * Must be called with tb->lock held.
+ */
 void tb_tunnel_put(struct tb_tunnel *tunnel)
 {
-	mutex_lock(&tb_tunnel_lock);
 	kref_put(&tunnel->kref, tb_tunnel_destroy);
-	mutex_unlock(&tb_tunnel_lock);
 }
 
 /**
@@ -1134,9 +1134,9 @@ static void tb_dp_dprx_work(struct work_struct *work)
 			tb_tunnel_set_active(tunnel, true);
 	}
 	tunnel->callback(tunnel);
+	tb_tunnel_put(tunnel);
 	mutex_unlock(&tb->lock);
 
-	tb_tunnel_put(tunnel);
 	tb_domain_put(tb);
 }
 
-- 
2.50.1


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

* [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (12 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:26   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 15/18] thunderbolt: Rework tunnel credit allocation Mika Westerberg
                   ` (3 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

The tests allocate routers without a domain (struct tb) but some of the
tunneling parameters, such as the buffer allocation, need to be read
from the host router which is reached through the domain. For this
reason allocate a dummy domain structure, and fill in the host router
pointer accordingly.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index a1f9dbb66940..9c6da22cd100 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -90,6 +90,24 @@ static struct tb_switch *alloc_switch(struct kunit *test, u64 route,
 	return sw;
 }
 
+/*
+ * Allocates a domain for the host router. The routers of the domain need
+ * to be able to reach the host router as some of the parameters, such as
+ * the buffer allocation, are read from it.
+ */
+static int alloc_domain(struct kunit *test, struct tb_switch *host)
+{
+	struct tb *tb;
+
+	tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL);
+	if (!tb)
+		return -ENOMEM;
+
+	tb->root_switch = host;
+	host->tb = tb;
+	return 0;
+}
+
 static struct tb_switch *alloc_host(struct kunit *test)
 {
 	struct tb_switch *sw;
@@ -98,6 +116,9 @@ static struct tb_switch *alloc_host(struct kunit *test)
 	if (!sw)
 		return NULL;
 
+	if (alloc_domain(test, sw))
+		return NULL;
+
 	sw->config.vendor_id = 0x8086;
 	sw->config.device_id = 0x9a1b;
 
@@ -333,6 +354,8 @@ static struct tb_switch *alloc_dev_default(struct kunit *test,
 	if (!parent)
 		return sw;
 
+	sw->tb = parent->tb;
+
 	/* Link them */
 	upstream_port = tb_upstream_port(sw);
 	port = tb_port_at(route, parent);
-- 
2.50.1


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

* [PATCH 15/18] thunderbolt: Rework tunnel credit allocation
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (13 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:43   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 16/18] thunderbolt: Add support for shared flow control Mika Westerberg
                   ` (2 subsequent siblings)
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

Currently the credit allocation code is hard to understand and does not
support on-demand allocation at all. What it does is pretty much
allocate full credits for PCIe/USB3, some for DMA and the rest goes for
DisplayPort tunnels. For instance if a host only has 2 DP IN adapters
it does not make sense to reserve credits for more than that.

This reworks the credit allocation so that it still reserves for the DP
and PCIe/USB3 but instead it:

 - Reserves the amount of DP tunnels possible from the host.
 - Reserves minimum but working amount for PCIe/USB3.
 - Reserves minimum for DMA.

Everything else is first come first served basis. For example if user
later plugs in a device router with a DP IN, it can still get the
credits from the "common" pool even if all host router DP tunnels are
already up. This also simplifies the call sites.

Because we change how credits are allocated we need to update the test
cases as well.

While we move the dma_credits parameter to credits.c allow it to
increase credits too (currently it only allowed to decrease them).

Co-developed-by: Gil Fine <gil.fine@linux.intel.com>
Signed-off-by: Gil Fine <gil.fine@linux.intel.com>
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/Makefile  |   2 +-
 drivers/thunderbolt/credits.c | 585 ++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/debugfs.c |  29 ++
 drivers/thunderbolt/path.c    |   4 +-
 drivers/thunderbolt/switch.c  |  22 +-
 drivers/thunderbolt/tb.h      | 145 ++++++++-
 drivers/thunderbolt/test.c    | 485 ++++++++++++++++++++++++----
 drivers/thunderbolt/tunnel.c  | 400 ++++++-----------------
 drivers/thunderbolt/usb4.c    |   7 +-
 9 files changed, 1290 insertions(+), 389 deletions(-)
 create mode 100644 drivers/thunderbolt/credits.c

diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
index beb054c3126b..86226d5ebd7b 100644
--- a/drivers/thunderbolt/Makefile
+++ b/drivers/thunderbolt/Makefile
@@ -3,7 +3,7 @@ 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 += usb4_port.o nvm.o retimer.o quirks.o clx.o
+thunderbolt-objs += usb4_port.o nvm.o retimer.o quirks.o clx.o credits.o
 
 thunderbolt-${CONFIG_ACPI} += acpi.o
 thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
diff --git a/drivers/thunderbolt/credits.c b/drivers/thunderbolt/credits.c
new file mode 100644
index 000000000000..84954778b916
--- /dev/null
+++ b/drivers/thunderbolt/credits.c
@@ -0,0 +1,585 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Credit allocation support
+ *
+ * Copyright (C) 2026, Intel Corporation
+ * Authors: Gil Fine <gil.fine@linux.intel.com>
+ *	    Mika Westerberg <westeri@kernel.org>
+ */
+
+#include "tb.h"
+
+#define MIN_PCIE_CREDITS	6
+/*
+ * According to CM guide minimum amount of buffers for 1.5 Gbps can be
+ * calculated as:
+ *
+ *   Number of buffers = 1.5 * 10^9 * 2 * 10^-6 / (256 * 8)
+ *
+ * That gives us the minimum of 2 buffers.
+ */
+#define MIN_USB3_CREDITS	2
+#define MIN_DMA_CREDITS		1
+
+/* Default credits if host router does not provide them */
+#define DEFAULT_DMA_CREDITS	14
+/* Path Credits Allocated is 7-bits */
+#define MAX_PATH_CREDITS	127
+
+/**
+ * struct credit_policy - Policy for credit allocation
+ * @pool: Which pool to use
+ * @common_first: Try first from the common pool
+ * @single_user: At most one tunnel of this type goes through a lane
+ *		 adapter (only meaningful with @common_first)
+ *
+ * If @common_first is true then allocations are first tried from
+ * %USB4_CREDIT_POOL_COMMON and if there is not enough room then the
+ * reserved pool is tried. From the reserved pool the minimum is taken.
+ *
+ * If it is false then maximum allocation is done from the reserved pool
+ * first and if there is no room the same allocation is tried from the
+ * %USB4_CREDIT_POOL_COMMON.
+ *
+ * If @single_user is true and the allocation was done from
+ * %USB4_CREDIT_POOL_COMMON, the reserved pool is returned to the
+ * common pool until the credits are released.
+ */
+struct credit_policy {
+	enum usb4_credit_pool_type pool;
+	bool common_first;
+	bool single_user;
+};
+
+static const struct credit_policy credit_policy[] = {
+	[TB_CREDITS_DP_MAIN] = {
+		.pool = USB4_CREDIT_POOL_DP,
+	},
+	[TB_CREDITS_DP_AUX] = {
+		.pool = USB4_CREDIT_POOL_DP,
+	},
+	[TB_CREDITS_PCI] = {
+		.pool = USB4_CREDIT_POOL_PCI,
+		.common_first = true,
+		.single_user = true,
+	},
+	[TB_CREDITS_USB3] = {
+		.pool = USB4_CREDIT_POOL_USB3,
+		.common_first = true,
+		.single_user = true,
+	},
+	[TB_CREDITS_DMA] = {
+		.pool = USB4_CREDIT_POOL_DMA,
+		.common_first = true,
+	},
+};
+
+static const char * const pool_names[] = {
+	[USB4_CREDIT_POOL_COMMON] = "common",
+	[USB4_CREDIT_POOL_DP] = "DP",
+	[USB4_CREDIT_POOL_PCI] = "PCIe",
+	[USB4_CREDIT_POOL_USB3] = "USB3",
+	[USB4_CREDIT_POOL_DMA] = "DMA",
+};
+
+static unsigned int dma_credits;
+module_param(dma_credits, uint, 0444);
+MODULE_PARM_DESC(dma_credits,
+		 "specify custom credits for DMA tunnels, 0 uses host baMaxHI (default: 0)");
+
+/* Protects lane adapter credit pools */
+static DEFINE_MUTEX(credits_lock);
+
+static void usb4_credit_pool_init(struct usb4_credit_pool *pool, const char *name)
+{
+	pool->name = name;
+	pool->total = 0;
+	pool->used = 0;
+}
+
+static void usb4_credit_pool_set_total(struct usb4_credit_pool *pool,
+				       unsigned int credits)
+{
+	pool->total = credits;
+}
+
+static int usb4_credit_pool_alloc(struct usb4_credit_pool *pool,
+				  unsigned int min, unsigned int max)
+{
+	if (WARN_ON(min > max))
+		return -EINVAL;
+
+	max = min(max, usb4_credit_pool_available(pool));
+	if (max < min)
+		return -ENOBUFS;
+
+	pool->used += max;
+	return max;
+}
+
+static void usb4_credit_pool_free(struct usb4_credit_pool *pool,
+				  unsigned int credits)
+{
+	if (WARN_ON(pool->used < credits))
+		return;
+	pool->used -= credits;
+}
+
+static void
+usb4_port_remove_credit_pool(struct tb_port *port, unsigned int type)
+{
+	unsigned int credits;
+
+	WARN_ON(port->credit_pool[type].used);
+
+	credits = usb4_credit_pool_total(&port->credit_pool[type]);
+	usb4_credit_pool_set_total(&port->credit_pool[type], 0);
+	/* Release the credits to the common pool */
+	usb4_credit_pool_free(&port->credit_pool[USB4_CREDIT_POOL_COMMON],
+			      credits);
+}
+
+static void usb4_port_remove_credit_pools(struct tb_port *port)
+{
+	int i;
+
+	if (!port->credit_pool)
+		return;
+
+	for (i = USB4_CREDIT_NUM_POOLS - 1; i > 0; i--)
+		usb4_port_remove_credit_pool(port, i);
+}
+
+static unsigned int usb4_port_dp_credits(const struct tb_port *port)
+{
+	const struct tb_switch *sw = port->sw;
+	unsigned int credits = 0, dp_streams = 0;
+	struct tb_port *p;
+
+	if (!tb_acpi_may_tunnel_dp())
+		return 0;
+
+	tb_switch_for_each_port(sw->tb->root_switch, p) {
+		if (tb_port_is_dpin(p))
+			dp_streams++;
+	}
+
+	/* Only the device router upstream port carries DP main */
+	if (sw->min_dp_main_credits > 0 && tb_is_upstream_port(port))
+		credits = dp_streams * sw->min_dp_main_credits;
+	if (sw->min_dp_aux_credits > 0)
+		credits += dp_streams * sw->min_dp_aux_credits;
+
+	return credits;
+}
+
+static unsigned int usb4_port_pool_min_credits(const struct tb_port *port,
+					       enum usb4_credit_pool_type type)
+{
+	const struct tb_switch *sw = port->sw;
+
+	switch (type) {
+	case USB4_CREDIT_POOL_DP:
+		return usb4_port_dp_credits(port);
+	case USB4_CREDIT_POOL_PCI:
+		if (tb_acpi_may_tunnel_pcie() && sw->max_pcie_credits > 0)
+			return min(MIN_PCIE_CREDITS, sw->max_pcie_credits);
+		break;
+	case USB4_CREDIT_POOL_USB3:
+		if (tb_acpi_may_tunnel_usb3() && sw->max_usb3_credits > 0)
+			return min(MIN_USB3_CREDITS, sw->max_usb3_credits);
+		break;
+	case USB4_CREDIT_POOL_DMA:
+		if (tb_acpi_is_xdomain_allowed())
+			return MIN_DMA_CREDITS;
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static void usb4_port_reserve_credits(struct tb_port *port, unsigned int type)
+{
+	struct usb4_credit_pool *common =
+			&port->credit_pool[USB4_CREDIT_POOL_COMMON];
+	unsigned int credits = usb4_port_pool_min_credits(port, type);
+
+	if (WARN_ON(port->credit_pool[type].total))
+		return;
+
+	if (credits > usb4_credit_pool_available(common)) {
+		/*
+		 * If we cannot get the minimum there is no point
+		 * wasting those credits as the allocation would fail
+		 * anyway.
+		 */
+		if (type != USB4_CREDIT_POOL_DP)
+			return;
+		credits = usb4_credit_pool_available(common);
+	}
+
+	usb4_credit_pool_alloc(common, credits, credits);
+	usb4_credit_pool_set_total(&port->credit_pool[type], credits);
+}
+
+/*
+ * Reserves credits from the lane adapter common credit pool for the
+ * protocol tunnels, that's DP, USB3, PCIe and DMA (depending on what
+ * tunneling is allowed).
+ *
+ * What is left in the common pool is available as first come first
+ * served.
+ */
+static void usb4_port_init_credit_pools(struct tb_port *port)
+{
+	struct usb4_credit_pool *pool;
+	int i;
+
+	if (!port->credit_pool)
+		return;
+
+	for (i = 0; i < USB4_CREDIT_NUM_POOLS; i++)
+		usb4_credit_pool_init(&port->credit_pool[i], pool_names[i]);
+
+	/* Happens to lane 1 adapter when lanes are bonded */
+	if (!port->total_credits)
+		return;
+
+	/* Initialize the common pool */
+	pool = &port->credit_pool[USB4_CREDIT_POOL_COMMON];
+	usb4_credit_pool_set_total(pool, port->total_credits -
+				   port->ctl_credits);
+
+	/*
+	 * When XDomain is connected then there cannot be any other
+	 * tunnels through the ingress adapter than DMA so all credits
+	 * are available.
+	 */
+	if (port->xdomain)
+		return;
+
+	/* The order sets the priority when there are not enough credits */
+	usb4_port_reserve_credits(port, USB4_CREDIT_POOL_DP);
+	usb4_port_reserve_credits(port, USB4_CREDIT_POOL_PCI);
+	usb4_port_reserve_credits(port, USB4_CREDIT_POOL_USB3);
+	usb4_port_reserve_credits(port, USB4_CREDIT_POOL_DMA);
+}
+
+static bool usb4_port_credit_pools_empty(const struct tb_port *port)
+{
+	unsigned int reserved = 0;
+	int i;
+
+	for (i = 1; i < USB4_CREDIT_NUM_POOLS; i++) {
+		if (port->credit_pool[i].used)
+			return false;
+		reserved += usb4_credit_pool_total(&port->credit_pool[i]);
+	}
+
+	return port->credit_pool[USB4_CREDIT_POOL_COMMON].used == reserved;
+}
+
+/**
+ * usb4_switch_free_credit_pools() - Free router credit pools
+ * @sw: USB4 router
+ *
+ * Releases credit pools allocated for each USB4 port of this router.
+ */
+void usb4_switch_free_credit_pools(struct tb_switch *sw)
+{
+	struct tb_port *port;
+
+	if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
+		return;
+
+	tb_switch_for_each_port(sw, port) {
+		if (!port->credit_pool)
+			continue;
+
+		usb4_port_remove_credit_pools(port);
+		WARN_ON(port->credit_pool[USB4_CREDIT_POOL_COMMON].used);
+		kfree(port->credit_pool);
+		port->credit_pool = NULL;
+	}
+}
+
+/**
+ * usb4_switch_alloc_credit_pools() - Allocate credit pools for this router
+ * @sw: USB4 router
+ *
+ * Allocates credit pools for each USB4 port of this router.
+ *
+ * Return: %0 in case of success, negative errno in case of failure.
+ */
+int usb4_switch_alloc_credit_pools(struct tb_switch *sw)
+{
+	struct tb_port *port;
+	int ret;
+
+	if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
+		return 0;
+	if (!sw->credit_allocation)
+		return 0;
+
+	/* Initialize credit pools for lane adapters */
+	tb_switch_for_each_port(sw, port) {
+		if (!tb_port_is_null(port))
+			continue;
+
+		port->credit_pool = kzalloc_objs(*port->credit_pool,
+						 USB4_CREDIT_NUM_POOLS,
+						 GFP_KERNEL);
+		if (!port->credit_pool) {
+			ret = -ENOMEM;
+			goto err_remove;
+		}
+
+		usb4_port_init_credit_pools(port);
+	}
+
+	return 0;
+
+err_remove:
+	tb_sw_warn(sw, "credits: failed to allocate credit pools\n");
+	usb4_switch_free_credit_pools(sw);
+	sw->credit_allocation = false;
+
+	return ret;
+}
+
+static unsigned int tb_switch_dma_credits(const struct tb_switch *sw)
+{
+	unsigned int credits;
+
+	if (dma_credits)
+		credits = dma_credits;
+	else if (sw->tb->root_switch->max_dma_credits)
+		credits = sw->tb->root_switch->max_dma_credits;
+	else
+		credits = DEFAULT_DMA_CREDITS;
+	return min(credits, MAX_PATH_CREDITS);
+}
+
+static int usb4_port_alloc_credits(struct tb_port *port,
+				   const struct credit_policy *policy,
+				   unsigned int min, unsigned int max,
+				   struct usb4_credit_pool **ppool)
+{
+	enum usb4_credit_pool_type primary, secondary, resolved;
+	int ret;
+
+	guard(mutex)(&credits_lock);
+
+	if (policy->common_first) {
+		primary = USB4_CREDIT_POOL_COMMON;
+		secondary = policy->pool;
+	} else {
+		primary = policy->pool;
+		secondary = USB4_CREDIT_POOL_COMMON;
+	}
+
+	ret = usb4_credit_pool_alloc(&port->credit_pool[primary], min, max);
+	if (ret < 0) {
+		if (ret != -ENOBUFS)
+			return ret;
+		/* Only take the minimum out of the pool */
+		if (secondary != USB4_CREDIT_POOL_COMMON)
+			max = min;
+		ret = usb4_credit_pool_alloc(&port->credit_pool[secondary], min,
+					     max);
+		if (ret < 0)
+			return ret;
+		resolved = secondary;
+	} else {
+		resolved = primary;
+	}
+
+	*ppool = &port->credit_pool[resolved];
+
+	/* Single user took from common, so return the minimum back there */
+	if (policy->single_user &&
+	    *ppool == &port->credit_pool[USB4_CREDIT_POOL_COMMON])
+		usb4_port_remove_credit_pool(port, policy->pool);
+
+	tb_port_dbg(port, "credits: allocated %s: %u\n",
+		    usb4_credit_pool_name(*ppool), ret);
+
+	return ret;
+}
+
+static void usb4_port_release_credits(struct tb_port *port,
+				      unsigned int credits,
+				      struct usb4_credit_pool *pool,
+				      const struct credit_policy *policy)
+{
+	guard(mutex)(&credits_lock);
+
+	usb4_credit_pool_free(pool, credits);
+	/* The minimum went to common at allocation, reserve it again */
+	if (policy->single_user &&
+	    pool == &port->credit_pool[USB4_CREDIT_POOL_COMMON])
+		usb4_port_reserve_credits(port, policy->pool);
+}
+
+/**
+ * usb4_port_update_credit_pools() - Updates lane adapter credit pools
+ * @port: Lane adapter
+ *
+ * Updates the lane adapter credit pools based on the available credits
+ * on the hardware.
+ */
+void usb4_port_update_credit_pools(struct tb_port *port)
+{
+	guard(mutex)(&credits_lock);
+
+	if (!port->credit_pool)
+		return;
+
+	if (!usb4_port_credit_pools_empty(port))
+		return;
+
+	usb4_port_remove_credit_pools(port);
+	usb4_port_init_credit_pools(port);
+}
+
+/**
+ * tb_hop_alloc_credits() - Allocate credits for path segment
+ * @hop: Path segment
+ * @type: Credit type to allocate
+ * @flags: Flags specifying the flow control
+ *
+ * Tries to allocate credits from the ingress lane adapter using the
+ * buffer allocation information the router provides (or fixed credits
+ * in case of pre-USB4 router). Fills in @hop->initial_credits or
+ * @hop->nfc_credits with the allocation.
+ *
+ * Call tb_hop_release_credits() to release the allocated credits back
+ * to the adapter credit pool.
+ *
+ * Return: %0 in case of success, negative errno in case of error.
+ */
+int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
+			 unsigned int flags)
+{
+	struct tb_port *port = hop->in_port;
+	struct tb_switch *sw = port->sw;
+	bool dedicated;
+	int ret;
+
+	/* Only DP main link is supposed to use flow control disabled */
+	if (WARN_ON(flags && type == TB_CREDITS_DP_MAIN))
+		return -EINVAL;
+
+	dedicated = flags & TB_HOP_IFC;
+
+	if (tb_switch_is_usb4(sw) && !tb_port_is_null(port)) {
+		/*
+		 * USB4 protocol adapter path credits allocated is
+		 * vendor defined.
+		 */
+		ret = 0;
+	} else if (tb_port_use_credit_allocation(port)) {
+		unsigned int min, max;
+
+		switch (type) {
+		case TB_CREDITS_DP_MAIN:
+			min = sw->min_dp_main_credits;
+			max = sw->min_dp_main_credits;
+			break;
+		case TB_CREDITS_DP_AUX:
+			min = sw->min_dp_aux_credits;
+			max = sw->min_dp_aux_credits;
+			break;
+		case TB_CREDITS_PCI:
+			max = sw->max_pcie_credits;
+			min = min(MIN_PCIE_CREDITS, max);
+			break;
+		case TB_CREDITS_USB3:
+			max = sw->max_usb3_credits;
+			min = min(MIN_USB3_CREDITS, max);
+			break;
+		case TB_CREDITS_DMA:
+			max = tb_switch_dma_credits(sw);
+			min = min(MIN_DMA_CREDITS, max);
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		if (!max)
+			return -ENOBUFS;
+
+		ret = usb4_port_alloc_credits(port, &credit_policy[type], min,
+					      max, &hop->credit_pool);
+		if (ret < 0)
+			return ret;
+		hop->credit_type = type;
+	} else {
+		switch (type) {
+		case TB_CREDITS_DP_MAIN:
+			ret = min(port->total_credits - 2, 12U);
+			break;
+		case TB_CREDITS_DP_AUX:
+			ret = 1;
+			break;
+		case TB_CREDITS_PCI:
+		case TB_CREDITS_USB3:
+			if (tb_port_is_null(port))
+				ret = port->bonded ? 32 : 16;
+			else
+				ret = 7;
+			break;
+		case TB_CREDITS_DMA:
+			if (tb_port_is_null(port))
+				ret = port->bonded ? 14 : 6;
+			else
+				ret = min(port->total_credits,
+					  tb_switch_dma_credits(sw));
+			break;
+		default:
+			return -EINVAL;
+		}
+	}
+
+	if (dedicated) {
+		hop->initial_credits = ret;
+		hop->ingress_fc_enable = true;
+	}
+	if (flags & TB_HOP_EFC)
+		hop->egress_fc_enable = true;
+	if (!flags)
+		hop->nfc_credits = ret;
+
+	return 0;
+}
+
+/**
+ * tb_hop_release_credits() - Release credits allocated for path segment
+ * @hop: Path segment
+ *
+ * Releases the previously allocated credits back to the lane adapter's
+ * credit pools.
+ */
+void tb_hop_release_credits(struct tb_path_hop *hop)
+{
+	if (hop->credit_pool) {
+		unsigned int credits;
+
+		if (hop->nfc_credits)
+			credits = hop->nfc_credits;
+		else
+			credits = hop->initial_credits;
+
+		usb4_port_release_credits(hop->in_port, credits,
+					  hop->credit_pool,
+					  &credit_policy[hop->credit_type]);
+	}
+
+	hop->ingress_fc_enable = false;
+	hop->egress_fc_enable = false;
+	hop->credit_pool = NULL;
+	hop->initial_credits = 0;
+	hop->nfc_credits = 0;
+}
diff --git a/drivers/thunderbolt/debugfs.c b/drivers/thunderbolt/debugfs.c
index 0d9f41a0f2cd..eac6d564bf18 100644
--- a/drivers/thunderbolt/debugfs.c
+++ b/drivers/thunderbolt/debugfs.c
@@ -2488,6 +2488,32 @@ static int port_sb_regs_show(struct seq_file *s, void *not_used)
 }
 DEBUGFS_ATTR_RW(port_sb_regs);
 
+static void credits_show(const struct tb_port *port, struct seq_file *s)
+{
+	int i;
+
+	seq_puts(s, "# pool used total\n");
+
+	for (i = 0; i < USB4_CREDIT_NUM_POOLS; i++) {
+		const struct usb4_credit_pool *pool = &port->credit_pool[i];
+
+		seq_printf(s, "%s %u %u\n", usb4_credit_pool_name(pool),
+			   usb4_credit_pool_used(pool),
+			   usb4_credit_pool_total(pool));
+	}
+}
+
+static int port_credits_show(struct seq_file *s, void *not_used)
+{
+	const struct tb_port *port = s->private;
+
+	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->sw->tb->lock)
+		credits_show(port, s);
+
+	return 0;
+}
+DEBUGFS_ATTR_RO(port_credits);
+
 /**
  * tb_switch_debugfs_init() - Add debugfs entries for router
  * @sw: Pointer to the router
@@ -2527,6 +2553,9 @@ void tb_switch_debugfs_init(struct tb_switch *sw)
 		if (port->usb4)
 			debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
 					    port, &port_sb_regs_fops);
+		if (port->credit_pool)
+			debugfs_create_file("credits", 0400, debugfs_dir,
+					    port, &port_credits_fops);
 	}
 
 	margining_switch_init(sw);
diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c
index 79c285dd29fb..9b18704f8010 100644
--- a/drivers/thunderbolt/path.c
+++ b/drivers/thunderbolt/path.c
@@ -547,7 +547,7 @@ int tb_path_activate(struct tb_path *path)
 		hop.drop_packages = path->drop_packages;
 		hop.counter = path->hops[i].in_counter_index;
 		hop.counter_enable = path->hops[i].in_counter_index != -1;
-		hop.egress_fc = path->egress_fc_enable & out_mask;
+		hop.egress_fc = path->hops[i].egress_fc_enable;
 		hop.egress_shared_buffer = path->egress_shared_buffer & out_mask;
 		/*
 		 * Protocol adapters IFC and ISE bits, and Path Credits
@@ -557,7 +557,7 @@ int tb_path_activate(struct tb_path *path)
 		if (tb_port_is_null(path->hops[i].in_port) ||
 		    !tb_switch_is_usb4(path->hops[i].in_port->sw)) {
 			hop.initial_credits = path->hops[i].initial_credits;
-			hop.ingress_fc = path->ingress_fc_enable & in_mask;
+			hop.ingress_fc = path->hops[i].ingress_fc_enable;
 			hop.ingress_shared_buffer =
 				path->ingress_shared_buffer & in_mask;
 		}
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 216681d2bb8f..a4b96950f3e6 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -1271,26 +1271,33 @@ int tb_port_wait_for_link_width(struct tb_port *port, unsigned int width,
 
 static int tb_port_do_update_credits(struct tb_port *port)
 {
-	u32 nfc_credits;
+	u32 nfc_credits, total;
 	int ret;
 
 	ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1);
 	if (ret)
 		return ret;
 
-	if (nfc_credits != port->config.nfc_credits) {
-		u32 total;
+	port->config.nfc_credits = nfc_credits;
 
+	/*
+	 * When the link is bonded the lane 0 holds the total buffers
+	 * (lane 1 total buffers is vendor defined) so reflect that
+	 * here.
+	 */
+	if (port->bonded && port->link_nr)
+		total = 0;
+	else
 		total = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
 			ADP_CS_4_TOTAL_BUFFERS_SHIFT;
 
+	if (total != port->total_credits) {
 		tb_port_dbg(port, "total credits changed %u -> %u\n",
 			    port->total_credits, total);
-
-		port->config.nfc_credits = nfc_credits;
 		port->total_credits = total;
 	}
 
+	usb4_port_update_credit_pools(port);
 	return 0;
 }
 
@@ -2400,6 +2407,8 @@ static void tb_switch_release(struct device *dev)
 
 	dma_port_free(sw->dma_port);
 
+	usb4_switch_free_credit_pools(sw);
+
 	tb_switch_for_each_port(sw, port) {
 		ida_destroy(&port->in_hopids);
 		ida_destroy(&port->out_hopids);
@@ -3488,6 +3497,8 @@ int tb_switch_add(struct tb_switch *sw)
 				 sw->device_name);
 	}
 
+	usb4_switch_alloc_credit_pools(sw);
+
 	ret = usb4_switch_add_ports(sw);
 	if (ret) {
 		dev_err(&sw->dev, "failed to add USB4 ports\n");
@@ -3522,6 +3533,7 @@ int tb_switch_add(struct tb_switch *sw)
 err_ports:
 	usb4_switch_remove_ports(sw);
 err_del:
+	usb4_switch_free_credit_pools(sw);
 	device_del(&sw->dev);
 
 	return ret;
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index c31ede3244ac..25d3966521a8 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -243,6 +243,113 @@ struct tb_bandwidth_group {
 	struct delayed_work release_work;
 };
 
+/**
+ * enum usb4_credit_pool_type - Types of credit pools
+ * @USB4_CREDIT_POOL_COMMON: Contains credits usable for on-demand allocation
+ *			     after reserved below have been taken away from it
+ * @USB4_CREDIT_POOL_DP: Credits reserved for DP tunneling
+ * @USB4_CREDIT_POOL_PCI: Credits reserved for PCIe tunneling
+ * @USB4_CREDIT_POOL_USB3: Credits reserved for USB3 tunneling
+ * @USB4_CREDIT_POOL_DMA: Credits reserved for DMA tunnels
+ * @USB4_CREDIT_NUM_POOLS: Number of credit pools
+ *
+ * Each lane adapter has its own set of pools. They are accounting only,
+ * the hardware does not partition the buffers in any way. The pools
+ * make sure each tunnel type can always get at least a working amount
+ * of credits::
+ *
+ *   0 +------------+
+ *     |   control  | port->ctl_credits, not part of any pool.
+ *     +------------+
+ *     |   COMMON   | On-demand allocation, first come first served. This
+ *     |            | is what is left after the reservations below have
+ *     |            | been carved out.
+ *     |            |
+ *     +------------+
+ *     |     DP     | DP AUX for each DP IN adapter of the host router
+ *     |            | plus DP main link on the upstream adapter of a
+ *     |            | device router. DP takes from here first and from
+ *     |            | %USB4_CREDIT_POOL_COMMON when this runs out.
+ *     +------------+
+ *     |    PCIe    | Minimum to get a PCIe tunnel working. It takes from
+ *     |            | %USB4_CREDIT_POOL_COMMON first and only the minimum
+ *     |            | from here when that runs out. A lane adapter carries
+ *     |            | at most one PCIe tunnel, so while that tunnel uses
+ *     |            | the common pool, this reserve is returned there
+ *     |            | until the tunnel is released.
+ *     +------------+
+ *     |    USB3    | Same as %USB4_CREDIT_POOL_PCI but for USB3.
+ *     +------------+
+ *     |    DMA     | Minimum for DMA tunnels. Taken from
+ *     |            | %USB4_CREDIT_POOL_COMMON first, but this reserve
+ *     |            | is kept all the time.
+ *     +------------+ port->total_credits
+ *
+ * If an XDomain is connected to the adapter only DMA can go through, so
+ * all the credits stay in %USB4_CREDIT_POOL_COMMON. Lane 1 adapter of a
+ * bonded link has no credits.
+ */
+enum usb4_credit_pool_type {
+	USB4_CREDIT_POOL_COMMON,
+	USB4_CREDIT_POOL_DP,
+	USB4_CREDIT_POOL_PCI,
+	USB4_CREDIT_POOL_USB3,
+	USB4_CREDIT_POOL_DMA,
+	USB4_CREDIT_NUM_POOLS,
+};
+
+/**
+ * struct usb4_credit_pool - Credit pool accounting
+ * @name: Name of the pool
+ * @total: Total number of credits in this pool
+ * @used: Number of credits used from @total
+ */
+struct usb4_credit_pool {
+	const char *name;
+	unsigned int total;
+	unsigned int used;
+};
+
+static inline unsigned int
+usb4_credit_pool_used(const struct usb4_credit_pool *pool)
+{
+	return pool->used;
+}
+
+static inline unsigned int
+usb4_credit_pool_available(const struct usb4_credit_pool *pool)
+{
+	return pool->total - pool->used;
+}
+
+static inline unsigned int
+usb4_credit_pool_total(const struct usb4_credit_pool *pool)
+{
+	return pool->total;
+}
+
+static inline const char *
+usb4_credit_pool_name(const struct usb4_credit_pool *pool)
+{
+	return pool->name;
+}
+
+/**
+ * enum tb_credit_type - Type of credits to allocate
+ * @TB_CREDITS_DP_MAIN: DP main link credits (video stream)
+ * @TB_CREDITS_DP_AUX: DP AUX credits (control)
+ * @TB_CREDITS_PCI: Credits for PCIe path
+ * @TB_CREDITS_USB3: Credits for USB3 path
+ * @TB_CREDITS_DMA: Credits for host-to-host path
+ */
+enum tb_credit_type {
+	TB_CREDITS_DP_MAIN,
+	TB_CREDITS_DP_AUX,
+	TB_CREDITS_PCI,
+	TB_CREDITS_USB3,
+	TB_CREDITS_DMA,
+};
+
 /**
  * struct tb_port - a thunderbolt port, part of a tb_switch
  * @config: Cached port configuration read from registers
@@ -265,8 +372,7 @@ struct tb_bandwidth_group {
  * @list: Used to link ports to DP resources list
  * @total_credits: Total number of buffers available for this port
  * @ctl_credits: Buffers reserved for control path
- * @dma_credits: Number of credits allocated for DMA tunneling for all
- *		 DMA paths through this port.
+ * @credit_pool: Pool of credits used for tunneling (USB4 only)
  * @group: Bandwidth allocation group the adapter is assigned to. Only
  *	   used for DP IN adapters for now.
  * @group_list: The adapter is linked to the group's list of ports through this
@@ -299,7 +405,7 @@ struct tb_port {
 	struct list_head list;
 	unsigned int total_credits;
 	unsigned int ctl_credits;
-	unsigned int dma_credits;
+	struct usb4_credit_pool *credit_pool;
 	struct tb_bandwidth_group *group;
 	struct list_head group_list;
 	unsigned int max_bw;
@@ -368,6 +474,11 @@ struct tb_retimer {
  *		     the path
  * @nfc_credits: Number of non-flow controlled buffers allocated for the
  *		 @in_port.
+ * @ingress_fc_enable: Enable flow control in ingress adapter
+ * @egress_fc_enable: Enable flow control in egress adapter
+ * @credit_pool: Pointer to the credit pool used to hold
+ *		 @initial_credits or @nfc_credits (%NULL if not used)
+ * @credit_type: Type of credits allocated (valid when @credit_pool is set)
  * @pm_support: Set path PM packet support bit to 1 (for USB4 v2 routers)
  *
  * Hop configuration is always done on the IN port of a switch.
@@ -389,6 +500,10 @@ struct tb_path_hop {
 	int next_hop_index;
 	unsigned int initial_credits;
 	unsigned int nfc_credits;
+	bool ingress_fc_enable;
+	bool egress_fc_enable;
+	struct usb4_credit_pool *credit_pool;
+	enum tb_credit_type credit_type;
 	bool pm_support;
 };
 
@@ -414,8 +529,6 @@ enum tb_path_port {
  * @name: Name of the path (used for debugging)
  * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
  * @egress_shared_buffer: Shared buffering used for egress ports on the path
- * @ingress_fc_enable: Flow control for ingress ports on the path
- * @egress_fc_enable: Flow control for egress ports on the path
  * @priority: Priority group if the path
  * @weight: Weight of the path inside the priority group
  * @drop_packages: Drop packages from queue tail or head
@@ -434,8 +547,6 @@ struct tb_path {
 	const char *name;
 	enum tb_path_port ingress_shared_buffer;
 	enum tb_path_port egress_shared_buffer;
-	enum tb_path_port ingress_fc_enable;
-	enum tb_path_port egress_fc_enable;
 
 	unsigned int priority:3;
 	int weight:4;
@@ -1208,12 +1319,23 @@ bool tb_path_port_on_path(const struct tb_path *path,
  * tb_path_for_each_hop() - Iterate over each hop on path
  * @path: Path whose hops to iterate
  * @hop: Hop used as iterator
+ * @index: Hop index
  *
  * Iterates over each hop on path.
  */
-#define tb_path_for_each_hop(path, hop)					\
-	for ((hop) = &(path)->hops[0];					\
-	     (hop) <= &(path)->hops[(path)->path_length - 1]; (hop)++)
+#define tb_path_for_each_hop(path, hop, index)				\
+	for ((index) = 0, (hop) = &(path)->hops[0];			\
+	     (index) < (path)->path_length;				\
+	     (index)++, (hop)++)
+
+/* Use flow control in ingress adapter */
+#define TB_HOP_IFC		BIT(0)
+/* Use flow control in egress adapter */
+#define TB_HOP_EFC		BIT(1)
+
+int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
+			 unsigned int flags);
+void tb_hop_release_credits(struct tb_path_hop *hop);
 
 int tb_drom_read(struct tb_switch *sw);
 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
@@ -1343,6 +1465,8 @@ int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
 int usb4_switch_credits_init(struct tb_switch *sw);
+int usb4_switch_alloc_credit_pools(struct tb_switch *sw);
+void usb4_switch_free_credit_pools(struct tb_switch *sw);
 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
@@ -1365,6 +1489,7 @@ int usb4_port_router_offline(struct tb_port *port);
 int usb4_port_router_online(struct tb_port *port);
 int usb4_port_enumerate_retimers(struct tb_port *port);
 bool usb4_port_clx_supported(struct tb_port *port);
+void usb4_port_update_credit_pools(struct tb_port *port);
 
 bool usb4_port_asym_supported(struct tb_port *port);
 int usb4_port_asym_set_link_width(struct tb_port *port, enum tb_link_width width);
diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index 9c6da22cd100..d6679bcc4ac3 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -69,8 +69,8 @@ static struct tb_switch *alloc_switch(struct kunit *test, u64 route,
 	sw->config.depth = tb_route_length(route);
 	sw->config.route_hi = upper_32_bits(route);
 	sw->config.route_lo = lower_32_bits(route);
-	sw->config.enabled = 0;
 	sw->config.max_port_number = max_port_number;
+	sw->config.enabled = true;
 
 	size = (sw->config.max_port_number + 1) * sizeof(*sw->ports);
 	sw->ports = kunit_kzalloc(test, size, GFP_KERNEL);
@@ -201,6 +201,8 @@ static struct tb_switch *alloc_host_usb4(struct kunit *test)
 	if (!sw)
 		return NULL;
 
+	sw->config.thunderbolt_version = 0x20;
+
 	sw->generation = 4;
 	sw->credit_allocation = true;
 	sw->max_usb3_credits = 32;
@@ -220,6 +222,8 @@ static struct tb_switch *alloc_host_br(struct kunit *test)
 	if (!sw)
 		return NULL;
 
+	sw->config.thunderbolt_version = 0x40;
+
 	sw->ports[10].config.type = TB_TYPE_DP_HDMI_IN;
 	sw->ports[10].config.max_in_hop_id = 9;
 	sw->ports[10].config.max_out_hop_id = 9;
@@ -432,13 +436,14 @@ static struct tb_switch *alloc_dev_without_dp(struct kunit *test,
 	for (i = 18; i <= 19; i++)
 		sw->ports[i].disabled = true;
 
+	sw->config.thunderbolt_version = 0x40;
+
 	sw->generation = 4;
 	sw->credit_allocation = true;
 	sw->max_usb3_credits = 109;
 	sw->min_dp_aux_credits = 0;
 	sw->min_dp_main_credits = 0;
 	sw->max_pcie_credits = 30;
-	sw->max_dma_credits = 1;
 
 	return sw;
 }
@@ -453,13 +458,14 @@ static struct tb_switch *alloc_dev_usb4(struct kunit *test,
 	if (!sw)
 		return NULL;
 
+	sw->config.thunderbolt_version = 0x20;
+
 	sw->generation = 4;
 	sw->credit_allocation = true;
 	sw->max_usb3_credits = 14;
 	sw->min_dp_aux_credits = 1;
 	sw->min_dp_main_credits = 18;
 	sw->max_pcie_credits = 32;
-	sw->max_dma_credits = 14;
 
 	return sw;
 }
@@ -1675,11 +1681,18 @@ static void tb_test_tunnel_3dp(struct kunit *test)
 	 *         [Device #5]
 	 */
 	host = alloc_host_br(test);
-	dev1 = alloc_dev_default(test, host, 0x3, true);
-	dev2 = alloc_dev_default(test, dev1, 0x303, true);
-	dev3 = alloc_dev_default(test, dev1, 0x503, true);
-	dev4 = alloc_dev_default(test, dev1, 0x703, true);
-	dev5 = alloc_dev_default(test, dev3, 0x50503, true);
+	dev1 = alloc_dev_usb4(test, host, 0x3, true);
+	dev2 = alloc_dev_usb4(test, dev1, 0x303, true);
+	dev3 = alloc_dev_usb4(test, dev1, 0x503, true);
+	dev4 = alloc_dev_usb4(test, dev1, 0x703, true);
+	dev5 = alloc_dev_usb4(test, dev3, 0x50503, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev1), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev2), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev3), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev4), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev5), 0);
 
 	in1 = &host->ports[5];
 	in2 = &host->ports[6];
@@ -1719,6 +1732,13 @@ static void tb_test_tunnel_3dp(struct kunit *test)
 	tb_tunnel_put(tunnel3);
 	tb_tunnel_put(tunnel2);
 	tb_tunnel_put(tunnel1);
+
+	usb4_switch_free_credit_pools(dev5);
+	usb4_switch_free_credit_pools(dev4);
+	usb4_switch_free_credit_pools(dev3);
+	usb4_switch_free_credit_pools(dev2);
+	usb4_switch_free_credit_pools(dev1);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_tunnel_usb3(struct kunit *test)
@@ -1738,9 +1758,13 @@ static void tb_test_tunnel_usb3(struct kunit *test)
 	 *           \ 1
 	 *         [Device #2]
 	 */
-	host = alloc_host(test);
-	dev1 = alloc_dev_default(test, host, 0x1, true);
-	dev2 = alloc_dev_default(test, dev1, 0x701, true);
+	host = alloc_host_usb4(test);
+	dev1 = alloc_dev_usb4(test, host, 0x1, true);
+	dev2 = alloc_dev_usb4(test, dev1, 0x701, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev1), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev2), 0);
 
 	down = &host->ports[12];
 	up = &dev1->ports[16];
@@ -1774,6 +1798,10 @@ static void tb_test_tunnel_usb3(struct kunit *test)
 
 	tb_tunnel_put(tunnel2);
 	tb_tunnel_put(tunnel1);
+
+	usb4_switch_free_credit_pools(dev2);
+	usb4_switch_free_credit_pools(dev1);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_tunnel_port_on_path(struct kunit *test)
@@ -2153,6 +2181,9 @@ static void tb_test_credit_alloc_pcie(struct kunit *test)
 	host = alloc_host_usb4(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	down = &host->ports[8];
 	up = &dev->ports[9];
 	tunnel = tb_tunnel_alloc_pci(NULL, up, down);
@@ -2162,18 +2193,185 @@ static void tb_test_credit_alloc_pcie(struct kunit *test)
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U);
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 64U);
 
 	tb_tunnel_put(tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static unsigned int tb_test_common_used(const struct tb_port *port)
+{
+	return usb4_credit_pool_used(&port->credit_pool[USB4_CREDIT_POOL_COMMON]);
+}
+
+static void tb_test_check_reserve(struct kunit *test,
+				  const struct tb_port *port,
+				  enum usb4_credit_pool_type type,
+				  unsigned int total, unsigned int common_used)
+{
+	const struct usb4_credit_pool *pool = &port->credit_pool[type];
+
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_total(pool), total);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(pool), 0U);
+	KUNIT_EXPECT_EQ(test, tb_test_common_used(port), common_used);
+}
+
+static void tb_test_credit_alloc_pcie_reserve(struct kunit *test)
+{
+	unsigned int host_used, dev_used;
+	struct tb_port *host_port, *dev_port;
+	struct tb_switch *host, *dev;
+	struct tb_tunnel *tunnel;
+	int i;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	host_port = &host->ports[1];
+	dev_port = &dev->ports[1];
+	host_used = tb_test_common_used(host_port);
+	dev_used = tb_test_common_used(dev_port);
+
+	tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_PCI, 6,
+			      host_used);
+	tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_PCI, 6,
+			      dev_used);
+
+	/* The second round makes sure the release put everything back */
+	for (i = 0; i < 2; i++) {
+		tunnel = tb_tunnel_alloc_pci(NULL, &dev->ports[9],
+					     &host->ports[8]);
+		KUNIT_ASSERT_NOT_NULL(test, tunnel);
+		KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+		KUNIT_ASSERT_PTR_EQ(test, tunnel->paths[0]->hops[1].in_port,
+				    dev_port);
+		KUNIT_ASSERT_PTR_EQ(test, tunnel->paths[1]->hops[1].in_port,
+				    host_port);
+
+		/*
+		 * Both sides got their credits from the common pool so
+		 * the PCIe reserves were returned there.
+		 */
+		tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_PCI, 0,
+				      host_used - 6 + 64);
+		tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_PCI, 0,
+				      dev_used - 6 + 32);
+
+		tb_tunnel_put(tunnel);
+
+		tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_PCI, 6,
+				      host_used);
+		tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_PCI, 6,
+				      dev_used);
+	}
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_pcie_from_reserve(struct kunit *test)
+{
+	struct usb4_credit_pool *common, *pci;
+	struct tb_switch *host, *dev;
+	struct tb_tunnel *tunnel;
+	unsigned int common_used;
+	struct tb_path_hop *hop;
+	struct tb_port *port;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	/*
+	 * Leave only enough buffers on the device upstream adapter for
+	 * the control path and the reservations:
+	 *
+	 *   2 + 2 * (1 + 18) + 2 + 6 + 1 = 49
+	 */
+	port = &dev->ports[1];
+	port->total_credits = 49;
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	common = &port->credit_pool[USB4_CREDIT_POOL_COMMON];
+	pci = &port->credit_pool[USB4_CREDIT_POOL_PCI];
+	KUNIT_ASSERT_EQ(test, usb4_credit_pool_available(common), 0U);
+	KUNIT_ASSERT_EQ(test, usb4_credit_pool_total(pci), 6U);
+	common_used = usb4_credit_pool_used(common);
+
+	tunnel = tb_tunnel_alloc_pci(NULL, &dev->ports[9], &host->ports[8]);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel);
+	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+
+	/* The minimum comes from the reserve and the reserve stays */
+	hop = &tunnel->paths[0]->hops[1];
+	KUNIT_ASSERT_PTR_EQ(test, hop->in_port, port);
+	KUNIT_EXPECT_PTR_EQ(test, hop->credit_pool, pci);
+	KUNIT_EXPECT_EQ(test, hop->initial_credits, 6U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_total(pci), 6U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(pci), 6U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(common), common_used);
+
+	tb_tunnel_put(tunnel);
+
+	tb_test_check_reserve(test, port, USB4_CREDIT_POOL_PCI, 6, common_used);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_reserve_below_min(struct kunit *test)
+{
+	struct usb4_credit_pool *common;
+	struct tb_switch *host, *dev;
+	struct tb_port *port;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	/*
+	 * Leave 3 buffers after the control path and DP. That is less
+	 * than the PCIe minimum but enough for USB3 and DMA:
+	 *
+	 *   2 + 2 * (1 + 18) + 3 = 43
+	 */
+	port = &dev->ports[1];
+	port->total_credits = 43;
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	common = &port->credit_pool[USB4_CREDIT_POOL_COMMON];
+	KUNIT_EXPECT_EQ(test,
+		usb4_credit_pool_total(&port->credit_pool[USB4_CREDIT_POOL_DP]),
+		38U);
+	KUNIT_EXPECT_EQ(test,
+		usb4_credit_pool_total(&port->credit_pool[USB4_CREDIT_POOL_PCI]),
+		0U);
+	KUNIT_EXPECT_EQ(test,
+		usb4_credit_pool_total(&port->credit_pool[USB4_CREDIT_POOL_USB3]),
+		2U);
+	KUNIT_EXPECT_EQ(test,
+		usb4_credit_pool_total(&port->credit_pool[USB4_CREDIT_POOL_DMA]),
+		1U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_available(common), 0U);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_credit_alloc_without_dp(struct kunit *test)
@@ -2189,18 +2387,19 @@ static void tb_test_credit_alloc_without_dp(struct kunit *test)
 	/*
 	 * The device has no DP therefore baMinDPmain = baMinDPaux = 0
 	 *
-	 * Create PCIe path with buffers less than baMaxPCIe.
-	 *
 	 * For a device with buffers configurations:
 	 * baMaxUSB3 = 109
 	 * baMinDPaux = 0
 	 * baMinDPmain = 0
 	 * baMaxPCIe = 30
-	 * baMaxHI = 1
-	 * Remaining Buffers = Total - (CP + DP) = 120 - (2 + 0) = 118
-	 * PCIe Credits = Max(6, Min(baMaxPCIe, Remaining Buffers - baMaxUSB3)
-	 *		= Max(6, Min(30, 9) = 9
+	 * Remaining Buffers = Total - (CP + DP + Reserve)
+	 * Remaining Buffers = 120 - (2 + (2 * 0) + 2 + 6 + 1) = 109
+	 * PCIe Credits = Min(Remaining Buffers, baMaxPCIe)
+	 * PCIe Credits = Min(109, 30) = 30
 	 */
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	down = &host->ports[8];
 	up = &dev->ports[9];
 	tunnel = tb_tunnel_alloc_pci(NULL, up, down);
@@ -2211,19 +2410,22 @@ static void tb_test_credit_alloc_without_dp(struct kunit *test)
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 9U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 30U);
 
 	/* PCIe upstream path */
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 64U);
 
 	tb_tunnel_put(tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_credit_alloc_dp(struct kunit *test)
@@ -2236,6 +2438,9 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	host = alloc_host_usb4(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	in = &host->ports[5];
 	out = &dev->ports[14];
 
@@ -2247,7 +2452,7 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	/* Video (main) path */
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
-	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 12U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 18U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 0U);
@@ -2256,7 +2461,7 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
@@ -2264,11 +2469,14 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	path = tunnel->paths[2];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
 	tb_tunnel_put(tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_credit_alloc_usb3(struct kunit *test)
@@ -2281,6 +2489,9 @@ static void tb_test_credit_alloc_usb3(struct kunit *test)
 	host = alloc_host_usb4(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	down = &host->ports[12];
 	up = &dev->ports[16];
 	tunnel = tb_tunnel_alloc_usb3(NULL, up, down, 0, 0);
@@ -2290,18 +2501,77 @@ static void tb_test_credit_alloc_usb3(struct kunit *test)
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U);
 
 	tb_tunnel_put(tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_usb3_reserve(struct kunit *test)
+{
+	unsigned int host_used, dev_used;
+	struct tb_port *host_port, *dev_port;
+	struct tb_switch *host, *dev;
+	struct tb_tunnel *tunnel;
+	int i;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	host_port = &host->ports[1];
+	dev_port = &dev->ports[1];
+	host_used = tb_test_common_used(host_port);
+	dev_used = tb_test_common_used(dev_port);
+
+	tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_USB3, 2,
+			      host_used);
+	tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_USB3, 2,
+			      dev_used);
+
+	/* The second round makes sure the release put everything back */
+	for (i = 0; i < 2; i++) {
+		tunnel = tb_tunnel_alloc_usb3(NULL, &dev->ports[16],
+					      &host->ports[12], 0, 0);
+		KUNIT_ASSERT_NOT_NULL(test, tunnel);
+		KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+		KUNIT_ASSERT_PTR_EQ(test, tunnel->paths[0]->hops[1].in_port,
+				    dev_port);
+		KUNIT_ASSERT_PTR_EQ(test, tunnel->paths[1]->hops[1].in_port,
+				    host_port);
+
+		/*
+		 * Both sides got their credits from the common pool so
+		 * the USB3 reserves were returned there.
+		 */
+		tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_USB3, 0,
+				      host_used - 2 + 32);
+		tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_USB3, 0,
+				      dev_used - 2 + 14);
+
+		tb_tunnel_put(tunnel);
+
+		tb_test_check_reserve(test, host_port, USB4_CREDIT_POOL_USB3, 2,
+				      host_used);
+		tb_test_check_reserve(test, dev_port, USB4_CREDIT_POOL_USB3, 2,
+				      dev_used);
+	}
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_credit_alloc_dma(struct kunit *test)
@@ -2314,6 +2584,9 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
 	host = alloc_host_usb4(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	nhi = &host->ports[7];
 	port = &dev->ports[3];
 
@@ -2338,38 +2611,52 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	tb_tunnel_put(tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 {
-	struct tb_tunnel *tunnel1, *tunnel2, *tunnel3;
+	struct tb_tunnel *tunnel1, *tunnel2, *tunnel3, *tunnel4;
+	struct tb_tunnel *tunnel5, *tunnel6;
 	struct tb_switch *host, *dev;
 	struct tb_port *nhi, *port;
 	struct tb_path *path;
 
-	host = alloc_host_usb4(test);
+	host = alloc_host_br(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	nhi = &host->ports[7];
 	port = &dev->ports[3];
 
 	/*
-	 * Create three DMA tunnels through the same ports. With the
-	 * default buffers we should be able to create two and the last
-	 * one fails.
+	 * Create six DMA tunnels through the same ports. With the
+	 * default reserved buffers we should be able to create five and
+	 * the last one fails.
 	 *
 	 * For default host we have following buffers for DMA:
 	 *
-	 *   120 - (2 + 2 * (1 + 0) + 32 + 64 + spare) = 20
+	 *   120 - (2 + 3 * (1 + 0) + 2 + 6 + 1) = 106
 	 *
 	 * For device we have following:
 	 *
-	 *  120 - (2 + 2 * (1 + 18) + 14 + 32 + spare) = 34
+	 *  120 - (2 + 3 * (1 + 18) + 2 + 6 + 1) = 52
+	 *
+	 * For the first 3 tunnels we get the full 14 credits both
+	 * directions.
 	 *
-	 * spare = 14 + 1 = 15
+	 * For the tunnel 4 we get 10 for the device ingres and 14 for
+	 * the host ingress.
 	 *
-	 * So on host the first tunnel gets 14 and the second gets the
-	 * remaining 1 and then we run out of buffers.
+	 * For the tunnel 5 we get 1 for the device ingres and 14 for
+	 * the host ingress.
+	 *
+	 * 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);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel1);
@@ -2398,26 +2685,18 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	path = tunnel2->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
-
-	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
-	KUNIT_ASSERT_NULL(test, tunnel3);
-
-	/*
-	 * Release the first DMA tunnel. That should make 14 buffers
-	 * available for the next tunnel.
-	 */
-	tb_tunnel_put(tunnel1);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
 	KUNIT_ASSERT_NOT_NULL(test, tunnel3);
+	KUNIT_ASSERT_EQ(test, tunnel3->npaths, (size_t)2);
 
 	path = tunnel3->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
@@ -2433,8 +2712,73 @@ 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);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel4);
+	KUNIT_ASSERT_EQ(test, tunnel4->npaths, (size_t)2);
+
+	path = tunnel4->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 4U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+	path = tunnel4->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
+	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);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel5);
+	KUNIT_ASSERT_EQ(test, tunnel5->npaths, (size_t)2);
+
+	path = tunnel5->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+	path = tunnel5->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
+	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);
+	KUNIT_ASSERT_NULL(test, tunnel6);
+
+	tb_tunnel_put(tunnel1);
+
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel6);
+	KUNIT_ASSERT_EQ(test, tunnel6->npaths, (size_t)2);
+
+	path = tunnel6->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+	path = tunnel6->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+	tb_tunnel_put(tunnel6);
+	tb_tunnel_put(tunnel5);
+	tb_tunnel_put(tunnel4);
 	tb_tunnel_put(tunnel3);
 	tb_tunnel_put(tunnel2);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static struct tb_tunnel *TB_TEST_PCIE_TUNNEL(struct kunit *test,
@@ -2453,14 +2797,14 @@ static struct tb_tunnel *TB_TEST_PCIE_TUNNEL(struct kunit *test,
 	path = pcie_tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U);
 
 	path = pcie_tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 64U);
 
@@ -2483,7 +2827,7 @@ static struct tb_tunnel *TB_TEST_DP_TUNNEL1(struct kunit *test,
 
 	path = dp_tunnel1->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
-	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 12U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 18U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 0U);
@@ -2491,14 +2835,14 @@ static struct tb_tunnel *TB_TEST_DP_TUNNEL1(struct kunit *test,
 	path = dp_tunnel1->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
 	path = dp_tunnel1->paths[2];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
@@ -2521,7 +2865,7 @@ static struct tb_tunnel *TB_TEST_DP_TUNNEL2(struct kunit *test,
 
 	path = dp_tunnel2->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
-	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 12U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 18U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 0U);
@@ -2529,14 +2873,14 @@ static struct tb_tunnel *TB_TEST_DP_TUNNEL2(struct kunit *test,
 	path = dp_tunnel2->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
 	path = dp_tunnel2->paths[2];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
 
@@ -2559,14 +2903,14 @@ static struct tb_tunnel *TB_TEST_USB3_TUNNEL(struct kunit *test,
 	path = usb3_tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	path = usb3_tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
+	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 32U);
 
@@ -2621,14 +2965,14 @@ static struct tb_tunnel *TB_TEST_DMA_TUNNEL2(struct kunit *test,
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 5U);
 
 	path = dma_tunnel2->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
-	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
+	KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
 
 	return dma_tunnel2;
 }
@@ -2648,10 +2992,24 @@ static void tb_test_credit_alloc_all(struct kunit *test)
 	host = alloc_host_usb4(test);
 	dev = alloc_dev_usb4(test, host, 0x1, true);
 
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
 	pcie_tunnel = TB_TEST_PCIE_TUNNEL(test, host, dev);
 	dp_tunnel1 = TB_TEST_DP_TUNNEL1(test, host, dev);
 	dp_tunnel2 = TB_TEST_DP_TUNNEL2(test, host, dev);
 	usb3_tunnel = TB_TEST_USB3_TUNNEL(test, host, dev);
+	/*
+	 * PCIe and USB3 Gen X got their credits from the common pool so
+	 * their minimum (6 + 2) is back there. After all the above we
+	 * get for DMA:
+	 *
+	 *   120 - (2 + 2 * (1 + 0) + 1) - (32 + 64) = 19
+	 *
+	 * For device we have following:
+	 *
+	 *  120 - (2 + 2 * (1 + 18) + 1) - (14 + 32) = 33
+	 */
 	dma_tunnel1 = TB_TEST_DMA_TUNNEL1(test, host, dev);
 	dma_tunnel2 = TB_TEST_DMA_TUNNEL2(test, host, dev);
 
@@ -2661,6 +3019,9 @@ static void tb_test_credit_alloc_all(struct kunit *test)
 	tb_tunnel_put(dp_tunnel2);
 	tb_tunnel_put(dp_tunnel1);
 	tb_tunnel_put(pcie_tunnel);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
 }
 
 static const u32 root_directory[] = {
@@ -3188,6 +3549,10 @@ static struct kunit_case tb_test_cases[] = {
 	KUNIT_CASE(tb_test_credit_alloc_legacy_not_bonded),
 	KUNIT_CASE(tb_test_credit_alloc_legacy_bonded),
 	KUNIT_CASE(tb_test_credit_alloc_pcie),
+	KUNIT_CASE(tb_test_credit_alloc_pcie_reserve),
+	KUNIT_CASE(tb_test_credit_alloc_pcie_from_reserve),
+	KUNIT_CASE(tb_test_credit_alloc_reserve_below_min),
+	KUNIT_CASE(tb_test_credit_alloc_usb3_reserve),
 	KUNIT_CASE(tb_test_credit_alloc_without_dp),
 	KUNIT_CASE(tb_test_credit_alloc_dp),
 	KUNIT_CASE(tb_test_credit_alloc_usb3),
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index af10c67997be..ffadd30bff7b 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -49,19 +49,6 @@
 #define TB_DP_AUX_PRIORITY		2
 #define TB_DP_AUX_WEIGHT		1
 
-/* struct tb_regs_hop::initial_credits is 7 bits wide */
-#define TB_MAX_CREDITS			127
-
-/* Minimum number of credits needed for PCIe path */
-#define TB_MIN_PCIE_CREDITS		6U
-/*
- * Number of credits we try to allocate for each DMA path if not limited
- * by the host router baMaxHI.
- */
-#define TB_DMA_CREDITS			14
-/* Minimum number of credits for DMA path */
-#define TB_MIN_DMA_CREDITS		1
-
 #define TB_DMA_PRIORITY			5
 #define TB_DMA_WEIGHT			1
 
@@ -92,11 +79,6 @@ MODULE_PARM_DESC(dprx_timeout,
 		 "DPRX capability read timeout in ms, -1 waits forever (default: "
 		 __stringify(TB_DPRX_TIMEOUT) ")");
 
-static unsigned int dma_credits = TB_DMA_CREDITS;
-module_param(dma_credits, uint, 0444);
-MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: "
-		 __stringify(TB_DMA_CREDITS) ")");
-
 static bool bw_alloc_mode = true;
 module_param(bw_alloc_mode, bool, 0444);
 MODULE_PARM_DESC(bw_alloc_mode,
@@ -112,60 +94,6 @@ static const char * const tb_event_names[] = {
 	[TB_TUNNEL_NO_BANDWIDTH] = "insufficient bandwidth",
 };
 
-static inline unsigned int tb_usable_credits(const struct tb_port *port)
-{
-	return port->total_credits - port->ctl_credits;
-}
-
-/**
- * tb_available_credits() - Available credits for PCIe and DMA
- * @port: Lane adapter to check
- * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
- *		    streams possible through this lane adapter
- *
- * Return: Number of available credits.
- */
-static unsigned int tb_available_credits(const struct tb_port *port,
-					 size_t *max_dp_streams)
-{
-	const struct tb_switch *sw = port->sw;
-	int credits, usb3, pcie, spare;
-	size_t ndp;
-
-	usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
-	pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
-
-	if (tb_acpi_is_xdomain_allowed()) {
-		spare = min_not_zero(sw->max_dma_credits, dma_credits);
-		/* Add some credits for potential second DMA tunnel */
-		spare += TB_MIN_DMA_CREDITS;
-	} else {
-		spare = 0;
-	}
-
-	credits = tb_usable_credits(port);
-	if (tb_acpi_may_tunnel_dp()) {
-		/*
-		 * Maximum number of DP streams possible through the
-		 * lane adapter.
-		 */
-		if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
-			ndp = (credits - (usb3 + pcie + spare)) /
-			      (sw->min_dp_aux_credits + sw->min_dp_main_credits);
-		else
-			ndp = 0;
-	} else {
-		ndp = 0;
-	}
-	credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
-	credits -= usb3;
-
-	if (max_dp_streams)
-		*max_dp_streams = ndp;
-
-	return credits > 0 ? credits : 0;
-}
-
 static void tb_init_pm_support(struct tb_path_hop *hop)
 {
 	struct tb_port *out_port = hop->out_port;
@@ -200,11 +128,34 @@ static void tb_tunnel_get(struct tb_tunnel *tunnel)
 	kref_get(&tunnel->kref);
 }
 
+static void tb_path_release_credits(struct tb_path *path)
+{
+	struct tb_path_hop *hop;
+	int i;
+
+	tb_path_for_each_hop(path, hop, i)
+		tb_hop_release_credits(hop);
+}
+
+static void tb_tunnel_release_credits(struct tb_tunnel *tunnel)
+{
+	int i;
+
+	for (i = 0; i < tunnel->npaths; i++) {
+		if (!tunnel->paths[i])
+			continue;
+		tb_path_release_credits(tunnel->paths[i]);
+	}
+}
+
 static void tb_tunnel_destroy(struct kref *kref)
 {
 	struct tb_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
 	int i;
 
+	/* Automatically release credits allocated for the tunnel */
+	tb_tunnel_release_credits(tunnel);
+
 	if (tunnel->destroy)
 		tunnel->destroy(tunnel);
 
@@ -392,49 +343,25 @@ static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
 	return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate);
 }
 
-static int tb_pci_init_credits(struct tb_path_hop *hop)
-{
-	struct tb_port *port = hop->in_port;
-	struct tb_switch *sw = port->sw;
-	unsigned int credits;
-
-	if (tb_port_use_credit_allocation(port)) {
-		unsigned int available;
-
-		available = tb_available_credits(port, NULL);
-		credits = min(sw->max_pcie_credits, available);
-
-		if (credits < TB_MIN_PCIE_CREDITS)
-			return -ENOSPC;
-
-		credits = max(TB_MIN_PCIE_CREDITS, credits);
-	} else {
-		if (tb_port_is_null(port))
-			credits = port->bonded ? 32 : 16;
-		else
-			credits = 7;
-	}
-
-	hop->initial_credits = credits;
-	return 0;
-}
-
 static int tb_pci_init_path(struct tb_path *path)
 {
 	struct tb_path_hop *hop;
+	int i;
 
-	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
 	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_fc_enable = TB_PATH_ALL;
 	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_PCI_PRIORITY;
 	path->weight = TB_PCI_WEIGHT;
 	path->drop_packages = 0;
 
-	tb_path_for_each_hop(path, hop) {
+	tb_path_for_each_hop(path, hop, i) {
+		unsigned int flags = TB_HOP_IFC;
 		int ret;
 
-		ret = tb_pci_init_credits(hop);
+		if (i < path->path_length - 1)
+			flags |= TB_HOP_EFC;
+
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_PCI, flags);
 		if (ret)
 			return ret;
 	}
@@ -481,7 +408,7 @@ struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down)
 	}
 	tunnel->paths[TB_PCI_PATH_UP] = path;
 	if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
-		goto err_free;
+		goto err_deactivate;
 
 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
 				"PCIe Down");
@@ -1481,59 +1408,28 @@ static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
 	return 0;
 }
 
-static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
-{
-	struct tb_port *port = hop->in_port;
-	struct tb_switch *sw = port->sw;
-
-	if (tb_port_use_credit_allocation(port))
-		hop->initial_credits = sw->min_dp_aux_credits;
-	else
-		hop->initial_credits = 1;
-}
-
-static void tb_dp_init_aux_path(struct tb_path *path, bool pm_support)
+static int tb_dp_init_aux_path(struct tb_path *path, bool pm_support)
 {
 	struct tb_path_hop *hop;
+	int i;
 
-	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
 	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_fc_enable = TB_PATH_ALL;
 	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DP_AUX_PRIORITY;
 	path->weight = TB_DP_AUX_WEIGHT;
 
-	tb_path_for_each_hop(path, hop) {
-		tb_dp_init_aux_credits(hop);
-		if (pm_support)
-			tb_init_pm_support(hop);
-	}
-}
-
-static int tb_dp_init_video_credits(struct tb_path_hop *hop)
-{
-	struct tb_port *port = hop->in_port;
-	struct tb_switch *sw = port->sw;
-
-	if (tb_port_use_credit_allocation(port)) {
-		unsigned int nfc_credits;
-		size_t max_dp_streams;
+	tb_path_for_each_hop(path, hop, i) {
+		unsigned int flags = TB_HOP_IFC;
+		int ret;
 
-		tb_available_credits(port, &max_dp_streams);
-		/*
-		 * Read the number of currently allocated NFC credits
-		 * from the lane adapter. Since we only use them for DP
-		 * tunneling we can use that to figure out how many DP
-		 * tunnels already go through the lane adapter.
-		 */
-		nfc_credits = port->config.nfc_credits &
-				ADP_CS_4_NFC_BUFFERS_MASK;
-		if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
-			return -ENOSPC;
+		if (i < path->path_length - 1)
+			flags |= TB_HOP_EFC;
 
-		hop->nfc_credits = sw->min_dp_main_credits;
-	} else {
-		hop->nfc_credits = min(port->total_credits - 2, 12U);
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_DP_AUX, flags);
+		if (ret)
+			return ret;
+		if (pm_support)
+			tb_init_pm_support(hop);
 	}
 
 	return 0;
@@ -1542,18 +1438,17 @@ static int tb_dp_init_video_credits(struct tb_path_hop *hop)
 static int tb_dp_init_video_path(struct tb_path *path, bool pm_support)
 {
 	struct tb_path_hop *hop;
+	int i;
 
-	path->egress_fc_enable = TB_PATH_NONE;
 	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_fc_enable = TB_PATH_NONE;
 	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DP_VIDEO_PRIORITY;
 	path->weight = TB_DP_VIDEO_WEIGHT;
 
-	tb_path_for_each_hop(path, hop) {
+	tb_path_for_each_hop(path, hop, i) {
 		int ret;
 
-		ret = tb_dp_init_video_credits(hop);
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_DP_MAIN, 0);
 		if (ret)
 			return ret;
 		if (pm_support)
@@ -1654,21 +1549,23 @@ struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
 	}
 	tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
 	if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false))
-		goto err_free;
+		goto err_deactivate;
 
 	path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL,
 				"AUX TX");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
-	tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false);
+	if (tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false))
+		goto err_deactivate;
 
 	path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
 				&port, "AUX RX");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_DP_AUX_PATH_IN] = path;
-	tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false);
+	if (tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false))
+		goto err_deactivate;
 
 	/* Validate that the tunnel is complete */
 	if (!tb_port_is_dpout(tunnel->dst_port)) {
@@ -1761,22 +1658,25 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
 			     link_nr, "Video");
 	if (!path)
 		goto err_free;
-	tb_dp_init_video_path(path, pm_support);
 	paths[TB_DP_VIDEO_PATH_OUT] = path;
+	if (tb_dp_init_video_path(path, pm_support))
+		goto err_free;
 
 	path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
 			     TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
 	if (!path)
 		goto err_free;
-	tb_dp_init_aux_path(path, pm_support);
 	paths[TB_DP_AUX_PATH_OUT] = path;
+	if (tb_dp_init_aux_path(path, pm_support))
+		goto err_free;
 
 	path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
 			     TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
 	if (!path)
 		goto err_free;
-	tb_dp_init_aux_path(path, pm_support);
 	paths[TB_DP_AUX_PATH_IN] = path;
+	if (tb_dp_init_aux_path(path, pm_support))
+		goto err_free;
 
 	return tunnel;
 
@@ -1785,102 +1685,26 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
 	return NULL;
 }
 
-static unsigned int tb_dma_available_credits(const struct tb_port *port)
-{
-	const struct tb_switch *sw = port->sw;
-	int credits;
-
-	credits = tb_available_credits(port, NULL);
-	if (tb_acpi_may_tunnel_pcie())
-		credits -= sw->max_pcie_credits;
-	credits -= port->dma_credits;
-
-	return credits > 0 ? credits : 0;
-}
-
-static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
-{
-	struct tb_port *port = hop->in_port;
-
-	if (tb_port_use_credit_allocation(port)) {
-		unsigned int available = tb_dma_available_credits(port);
-
-		/*
-		 * Need to have at least TB_MIN_DMA_CREDITS, otherwise
-		 * DMA path cannot be established.
-		 */
-		if (available < TB_MIN_DMA_CREDITS)
-			return -ENOSPC;
-
-		credits = min(credits, available);
-
-		tb_port_dbg(port, "reserving %u credits for DMA path\n",
-			    credits);
-
-		port->dma_credits += credits;
-	} else {
-		if (tb_port_is_null(port))
-			credits = port->bonded ? 14 : 6;
-		else
-			credits = min(port->total_credits, credits);
-	}
-
-	hop->initial_credits = credits;
-	return 0;
-}
-
-/* Path from lane adapter to NHI */
-static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
+static int tb_dma_init_path(struct tb_path *path)
 {
 	struct tb_path_hop *hop;
-	unsigned int i, tmp;
+	int i;
 
-	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
-	path->ingress_fc_enable = TB_PATH_ALL;
 	path->egress_shared_buffer = TB_PATH_NONE;
 	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DMA_PRIORITY;
 	path->weight = TB_DMA_WEIGHT;
 	path->clear_fc = true;
 
-	/*
-	 * First lane adapter is the one connected to the remote host.
-	 * We don't tunnel other traffic over this link so we can use
-	 * all the credits (except the ones reserved for control traffic).
-	 */
-	hop = &path->hops[0];
-	tmp = min(tb_usable_credits(hop->in_port), credits);
-	hop->initial_credits = tmp;
-	hop->in_port->dma_credits += tmp;
-
-	for (i = 1; i < path->path_length; i++) {
+	tb_path_for_each_hop(path, hop, i) {
+		unsigned int flags = TB_HOP_IFC;
 		int ret;
 
-		ret = tb_dma_reserve_credits(&path->hops[i], credits);
-		if (ret)
-			return ret;
-	}
+		/* No EFC towards the host interface */
+		if (!tb_port_is_nhi(hop->out_port))
+			flags |= TB_HOP_EFC;
 
-	return 0;
-}
-
-/* Path from NHI to lane adapter */
-static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
-{
-	struct tb_path_hop *hop;
-
-	path->egress_fc_enable = TB_PATH_ALL;
-	path->ingress_fc_enable = TB_PATH_ALL;
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
-	path->priority = TB_DMA_PRIORITY;
-	path->weight = TB_DMA_WEIGHT;
-	path->clear_fc = true;
-
-	tb_path_for_each_hop(path, hop) {
-		int ret;
-
-		ret = tb_dma_reserve_credits(hop, credits);
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_DMA, flags);
 		if (ret)
 			return ret;
 	}
@@ -1888,37 +1712,6 @@ static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
 	return 0;
 }
 
-static void tb_dma_release_credits(struct tb_path_hop *hop)
-{
-	struct tb_port *port = hop->in_port;
-
-	if (tb_port_use_credit_allocation(port)) {
-		port->dma_credits -= hop->initial_credits;
-
-		tb_port_dbg(port, "released %u DMA path credits\n",
-			    hop->initial_credits);
-	}
-}
-
-static void tb_dma_destroy_path(struct tb_path *path)
-{
-	struct tb_path_hop *hop;
-
-	tb_path_for_each_hop(path, hop)
-		tb_dma_release_credits(hop);
-}
-
-static void tb_dma_destroy(struct tb_tunnel *tunnel)
-{
-	int i;
-
-	for (i = 0; i < tunnel->npaths; i++) {
-		if (!tunnel->paths[i])
-			continue;
-		tb_dma_destroy_path(tunnel->paths[i]);
-	}
-}
-
 /**
  * tb_tunnel_alloc_dma() - allocate a DMA tunnel
  * @tb: Pointer to the domain structure
@@ -1941,7 +1734,6 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 	struct tb_tunnel *tunnel;
 	size_t npaths = 0, i = 0;
 	struct tb_path *path;
-	unsigned int credits;
 
 	/* Ring 0 is reserved for control channel */
 	if (WARN_ON(!receive_ring || !transmit_ring))
@@ -1961,14 +1753,6 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 
 	tunnel->src_port = nhi;
 	tunnel->dst_port = dst;
-	tunnel->destroy = tb_dma_destroy;
-
-	credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits);
-	if (credits > TB_MAX_CREDITS) {
-		tb_tunnel_dbg(tunnel, "%u credits do not fit a hop, using %u\n",
-			      credits, TB_MAX_CREDITS);
-		credits = TB_MAX_CREDITS;
-	}
 
 	if (receive_ring > 0) {
 		path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
@@ -1976,7 +1760,7 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 		if (!path)
 			goto err_free;
 		tunnel->paths[i++] = path;
-		if (tb_dma_init_rx_path(path, credits)) {
+		if (tb_dma_init_path(path)) {
 			tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
 			goto err_free;
 		}
@@ -1988,7 +1772,7 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
 		if (!path)
 			goto err_free;
 		tunnel->paths[i++] = path;
-		if (tb_dma_init_tx_path(path, credits)) {
+		if (tb_dma_init_path(path)) {
 			tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
 			goto err_free;
 		}
@@ -2195,38 +1979,30 @@ static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
 		      tunnel->allocated_up, tunnel->allocated_down);
 }
 
-static void tb_usb3_init_credits(struct tb_path_hop *hop)
-{
-	struct tb_port *port = hop->in_port;
-	struct tb_switch *sw = port->sw;
-	unsigned int credits;
-
-	if (tb_port_use_credit_allocation(port)) {
-		credits = sw->max_usb3_credits;
-	} else {
-		if (tb_port_is_null(port))
-			credits = port->bonded ? 32 : 16;
-		else
-			credits = 7;
-	}
-
-	hop->initial_credits = credits;
-}
-
-static void tb_usb3_init_path(struct tb_path *path)
+static int tb_usb3_init_path(struct tb_path *path)
 {
 	struct tb_path_hop *hop;
+	int i;
 
-	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
 	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_fc_enable = TB_PATH_ALL;
 	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_USB3_PRIORITY;
 	path->weight = TB_USB3_WEIGHT;
 	path->drop_packages = 0;
 
-	tb_path_for_each_hop(path, hop)
-		tb_usb3_init_credits(hop);
+	tb_path_for_each_hop(path, hop, i) {
+		unsigned int flags = TB_HOP_IFC;
+		int ret;
+
+		if (i < path->path_length - 1)
+			flags |= TB_HOP_EFC;
+
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_USB3, flags);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 /**
@@ -2267,14 +2043,16 @@ struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down)
 		goto err_free;
 	}
 	tunnel->paths[TB_USB3_PATH_DOWN] = path;
-	tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
+	if (tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]))
+		goto err_deactivate;
 
 	path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
 				"USB3 Up");
 	if (!path)
 		goto err_deactivate;
 	tunnel->paths[TB_USB3_PATH_UP] = path;
-	tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
+	if (tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]))
+		goto err_deactivate;
 
 	/* Validate that the tunnel is complete */
 	if (!tb_port_is_usb3_up(tunnel->dst_port)) {
@@ -2381,15 +2159,17 @@ struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
 			     "USB3 Down");
 	if (!path)
 		goto err_free;
-	tb_usb3_init_path(path);
 	tunnel->paths[TB_USB3_PATH_DOWN] = path;
+	if (tb_usb3_init_path(path))
+		goto err_free;
 
 	path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
 			     "USB3 Up");
 	if (!path)
 		goto err_free;
-	tb_usb3_init_path(path);
 	tunnel->paths[TB_USB3_PATH_UP] = path;
+	if (tb_usb3_init_path(path))
+		goto err_free;
 
 	if (!tb_route(down->sw)) {
 		tunnel->allocated_up = min(max_rate, max_up);
diff --git a/drivers/thunderbolt/usb4.c b/drivers/thunderbolt/usb4.c
index 519aeca66d6f..8e71ca6967b6 100644
--- a/drivers/thunderbolt/usb4.c
+++ b/drivers/thunderbolt/usb4.c
@@ -1308,8 +1308,12 @@ static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
  */
 int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
 {
+	int ret;
+
 	xd->link_usb4 = link_is_usb4(port);
-	return usb4_set_xdomain_configured(port, true);
+	ret = usb4_set_xdomain_configured(port, true);
+	tb_port_update_credits(port);
+	return ret;
 }
 
 /**
@@ -1321,6 +1325,7 @@ int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
 void usb4_port_unconfigure_xdomain(struct tb_port *port)
 {
 	usb4_set_xdomain_configured(port, false);
+	tb_port_update_credits(port);
 }
 
 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
-- 
2.50.1


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

* [PATCH 16/18] thunderbolt: Add support for shared flow control
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (14 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 15/18] thunderbolt: Rework tunnel credit allocation Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:34   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links Mika Westerberg
  2026-11-19 17:12 ` [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering Mika Westerberg
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

From: Gil Fine <gil.fine@linux.intel.com>

It is possible to make several paths share the ingress adapter buffers.
There are two possible configurations: shared flow control and
restricted shared flow control where the former shares all the
configured buffers and the latter shares specified amount of the
configured buffers for the paths going through an ingress adapter.

This adds support for both methods.

While there fix the rollback of NFC credits to start from correct index.

Signed-off-by: Gil Fine <gil.fine@linux.intel.com>
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/credits.c | 132 +++++++++++++++++++++++++++++-----
 drivers/thunderbolt/debugfs.c |  14 ++++
 drivers/thunderbolt/path.c    |  56 ++++++++++++---
 drivers/thunderbolt/switch.c  |  83 +++++++++++++++++++--
 drivers/thunderbolt/tb.h      |  45 ++++++------
 drivers/thunderbolt/tb_regs.h |   2 +
 drivers/thunderbolt/tunnel.c  |  10 ---
 7 files changed, 279 insertions(+), 63 deletions(-)

diff --git a/drivers/thunderbolt/credits.c b/drivers/thunderbolt/credits.c
index 84954778b916..58b9fdec19be 100644
--- a/drivers/thunderbolt/credits.c
+++ b/drivers/thunderbolt/credits.c
@@ -32,6 +32,7 @@
  * @common_first: Try first from the common pool
  * @single_user: At most one tunnel of this type goes through a lane
  *		 adapter (only meaningful with @common_first)
+ * @shared: Allow shared buffering to be used
  *
  * If @common_first is true then allocations are first tried from
  * %USB4_CREDIT_POOL_COMMON and if there is not enough room then the
@@ -49,6 +50,7 @@ struct credit_policy {
 	enum usb4_credit_pool_type pool;
 	bool common_first;
 	bool single_user;
+	bool shared;
 };
 
 static const struct credit_policy credit_policy[] = {
@@ -71,6 +73,7 @@ static const struct credit_policy credit_policy[] = {
 	[TB_CREDITS_DMA] = {
 		.pool = USB4_CREDIT_POOL_DMA,
 		.common_first = true,
+		.shared = true,
 	},
 };
 
@@ -362,16 +365,14 @@ static unsigned int tb_switch_dma_credits(const struct tb_switch *sw)
 	return min(credits, MAX_PATH_CREDITS);
 }
 
-static int usb4_port_alloc_credits(struct tb_port *port,
-				   const struct credit_policy *policy,
-				   unsigned int min, unsigned int max,
-				   struct usb4_credit_pool **ppool)
+static int __usb4_port_alloc_credits(struct tb_port *port,
+				     const struct credit_policy *policy,
+				     unsigned int min, unsigned int max,
+				     struct usb4_credit_pool **ppool)
 {
 	enum usb4_credit_pool_type primary, secondary, resolved;
 	int ret;
 
-	guard(mutex)(&credits_lock);
-
 	if (policy->common_first) {
 		primary = USB4_CREDIT_POOL_COMMON;
 		secondary = policy->pool;
@@ -397,6 +398,58 @@ static int usb4_port_alloc_credits(struct tb_port *port,
 	}
 
 	*ppool = &port->credit_pool[resolved];
+	return ret;
+}
+
+static inline unsigned int
+usb4_port_total_shared_credits(const struct tb_port *port)
+{
+	/* For now only DMA uses shared credits so LCA == PCA */
+	return tb_switch_dma_credits(port->sw);
+}
+
+static int usb4_port_alloc_shared_credits(struct tb_port *port,
+					  const struct credit_policy *policy,
+					  unsigned int min, unsigned int max,
+					  struct usb4_credit_pool **ppool)
+{
+	int ret;
+
+	if (WARN_ON(!policy->shared))
+		return -EINVAL;
+
+	if (!port->shared_users) {
+		unsigned int lca = usb4_port_total_shared_credits(port);
+
+		ret = __usb4_port_alloc_credits(port, policy, min, lca, ppool);
+		if (ret < 0)
+			return ret;
+		port->shared_pool = *ppool;
+		port->shared_credits = ret;
+	} else if (min > port->shared_credits) {
+		return -ENOBUFS;
+	}
+
+	port->shared_users++;
+	ret = min(max, port->shared_credits);
+	*ppool = port->shared_pool;
+
+	tb_port_dbg(port, "credits: allocated shared: %u users: %u\n", ret,
+		    port->shared_users);
+
+	return ret;
+}
+
+static int usb4_port_alloc_dedicated_credits(struct tb_port *port,
+					     const struct credit_policy *policy,
+					     unsigned int min, unsigned int max,
+					     struct usb4_credit_pool **ppool)
+{
+	int ret;
+
+	ret = __usb4_port_alloc_credits(port, policy, min, max, ppool);
+	if (ret < 0)
+		return ret;
 
 	/* Single user took from common, so return the minimum back there */
 	if (policy->single_user &&
@@ -409,18 +462,52 @@ static int usb4_port_alloc_credits(struct tb_port *port,
 	return ret;
 }
 
+static int usb4_port_alloc_credits(struct tb_port *port,
+				   const struct credit_policy *policy,
+				   unsigned int min, unsigned int max,
+				   bool shared, struct usb4_credit_pool **ppool)
+{
+	guard(mutex)(&credits_lock);
+
+	if (shared)
+		return usb4_port_alloc_shared_credits(port, policy, min, max,
+						      ppool);
+	return usb4_port_alloc_dedicated_credits(port, policy, min, max, ppool);
+}
+
 static void usb4_port_release_credits(struct tb_port *port,
-				      unsigned int credits,
+				      unsigned int credits, bool shared,
 				      struct usb4_credit_pool *pool,
 				      const struct credit_policy *policy)
 {
 	guard(mutex)(&credits_lock);
 
-	usb4_credit_pool_free(pool, credits);
-	/* The minimum went to common at allocation, reserve it again */
-	if (policy->single_user &&
-	    pool == &port->credit_pool[USB4_CREDIT_POOL_COMMON])
-		usb4_port_reserve_credits(port, policy->pool);
+	if (shared) {
+		const struct usb4_credit_pool *shared_pool = port->shared_pool;
+		unsigned int tmp = port->shared_credits;
+
+		if (WARN_ON(!port->shared_users))
+			return;
+
+		if (!--port->shared_users) {
+			usb4_credit_pool_free(port->shared_pool,
+					      port->shared_credits);
+			port->shared_pool = NULL;
+			port->shared_credits = 0;
+		}
+		tb_port_dbg(port,
+			    "credits: released shared %s: %u users: %u\n",
+			    usb4_credit_pool_name(shared_pool), tmp,
+			    port->shared_users);
+	} else {
+		usb4_credit_pool_free(pool, credits);
+		/* The minimum went to common at allocation, reserve it again */
+		if (policy->single_user &&
+		    pool == &port->credit_pool[USB4_CREDIT_POOL_COMMON])
+			usb4_port_reserve_credits(port, policy->pool);
+		tb_port_dbg(port, "credits: released %s: %u\n",
+			    usb4_credit_pool_name(pool), credits);
+	}
 }
 
 /**
@@ -465,7 +552,7 @@ int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
 {
 	struct tb_port *port = hop->in_port;
 	struct tb_switch *sw = port->sw;
-	bool dedicated;
+	bool dedicated, shared;
 	int ret;
 
 	/* Only DP main link is supposed to use flow control disabled */
@@ -473,12 +560,15 @@ int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
 		return -EINVAL;
 
 	dedicated = flags & TB_HOP_IFC;
+	shared = flags & TB_HOP_ISE;
 
 	if (tb_switch_is_usb4(sw) && !tb_port_is_null(port)) {
 		/*
-		 * USB4 protocol adapter path credits allocated is
-		 * vendor defined.
+		 * USB4 protocol adapter path credits allocated and ISE
+		 * bit are vendor defined.
 		 */
+		if (WARN_ON(shared))
+			return -EINVAL;
 		ret = 0;
 	} else if (tb_port_use_credit_allocation(port)) {
 		unsigned int min, max;
@@ -512,11 +602,14 @@ int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
 			return -ENOBUFS;
 
 		ret = usb4_port_alloc_credits(port, &credit_policy[type], min,
-					      max, &hop->credit_pool);
+					      max, shared, &hop->credit_pool);
 		if (ret < 0)
 			return ret;
 		hop->credit_type = type;
 	} else {
+		if (WARN_ON(shared))
+			return -EINVAL;
+
 		switch (type) {
 		case TB_CREDITS_DP_MAIN:
 			ret = min(port->total_credits - 2, 12U);
@@ -547,8 +640,12 @@ int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
 		hop->initial_credits = ret;
 		hop->ingress_fc_enable = true;
 	}
+	if (shared)
+		hop->ingress_shared_buffer = true;
 	if (flags & TB_HOP_EFC)
 		hop->egress_fc_enable = true;
+	if (flags & TB_HOP_ESE)
+		hop->egress_shared_buffer = true;
 	if (!flags)
 		hop->nfc_credits = ret;
 
@@ -573,12 +670,15 @@ void tb_hop_release_credits(struct tb_path_hop *hop)
 			credits = hop->initial_credits;
 
 		usb4_port_release_credits(hop->in_port, credits,
+					  hop->ingress_shared_buffer,
 					  hop->credit_pool,
 					  &credit_policy[hop->credit_type]);
 	}
 
 	hop->ingress_fc_enable = false;
 	hop->egress_fc_enable = false;
+	hop->ingress_shared_buffer = false;
+	hop->egress_shared_buffer = false;
 	hop->credit_pool = NULL;
 	hop->initial_credits = 0;
 	hop->nfc_credits = 0;
diff --git a/drivers/thunderbolt/debugfs.c b/drivers/thunderbolt/debugfs.c
index eac6d564bf18..5e9e087ae237 100644
--- a/drivers/thunderbolt/debugfs.c
+++ b/drivers/thunderbolt/debugfs.c
@@ -2501,6 +2501,20 @@ static void credits_show(const struct tb_port *port, struct seq_file *s)
 			   usb4_credit_pool_used(pool),
 			   usb4_credit_pool_total(pool));
 	}
+
+	if (port->shared_buffering) {
+		const char *name;
+
+		if (port->shared_pool)
+			name = usb4_credit_pool_name(port->shared_pool);
+		else
+			name = "-";
+
+		seq_puts(s, "# shared pool credits users paths\n");
+		seq_printf(s, "shared %s %u %u %u\n", name,
+			   port->shared_credits, port->shared_users,
+			   port->shared_paths);
+	}
 }
 
 static int port_credits_show(struct seq_file *s, void *not_used)
diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c
index 9b18704f8010..89d3046498ce 100644
--- a/drivers/thunderbolt/path.c
+++ b/drivers/thunderbolt/path.c
@@ -368,6 +368,25 @@ static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
 	}
 }
 
+static void __tb_path_deallocate_shared(struct tb_path *path, int first_hop)
+{
+	int i;
+
+	for (i = first_hop; i < path->path_length; i++) {
+		int ret;
+
+		if (!path->hops[i].ingress_shared_buffer)
+			continue;
+
+		ret = tb_port_enable_shared_buffering(path->hops[i].in_port,
+						      false);
+		if (ret)
+			tb_port_warn(path->hops[i].in_port,
+				     "shared credits deallocation failed for hop %d\n",
+				     i);
+	}
+}
+
 static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
 				    bool clear_fc)
 {
@@ -469,6 +488,7 @@ void tb_path_deactivate(struct tb_path *path)
 	       tb_route(path->hops[path->path_length - 1].out_port->sw),
 	       path->hops[path->path_length - 1].out_port->port);
 	__tb_path_deactivate_hops(path, 0);
+	__tb_path_deallocate_shared(path, 0);
 	__tb_path_deallocate_nfc(path, 0);
 	path->activated = false;
 }
@@ -485,7 +505,6 @@ void tb_path_deactivate(struct tb_path *path)
 int tb_path_activate(struct tb_path *path)
 {
 	int i, res;
-	enum tb_path_port out_mask, in_mask;
 	if (path->activated) {
 		tb_WARN(path->tb, "trying to activate already activated path\n");
 		return -EINVAL;
@@ -513,11 +532,23 @@ int tb_path_activate(struct tb_path *path)
 		res = tb_port_add_nfc_credits(path->hops[i].in_port,
 					      path->hops[i].nfc_credits);
 		if (res) {
-			__tb_path_deallocate_nfc(path, i);
+			__tb_path_deallocate_nfc(path, i + 1);
 			goto err;
 		}
 	}
 
+	/* Add shared credits */
+	for (i = path->path_length - 1; i >= 0; i--) {
+		if (!path->hops[i].ingress_shared_buffer)
+			continue;
+		res = tb_port_enable_shared_buffering(path->hops[i].in_port,
+						      true);
+		if (res) {
+			__tb_path_deallocate_shared(path, i + 1);
+			goto err_nfc;
+		}
+	}
+
 	/* Activate hops. */
 	for (i = 0; i < path->path_length; i++) {
 		struct tb_regs_hop hop = { 0 };
@@ -529,8 +560,10 @@ int tb_path_activate(struct tb_path *path)
 		/* Needed for USB4 routers, read path config space before write */
 		res = tb_port_read(path->hops[i].in_port, &hop, TB_CFG_HOPS,
 				   2 * path->hops[i].in_hop_index, 2);
-		if (res)
-			goto err;
+		if (res) {
+			__tb_path_deactivate_hops(path, 0);
+			goto err_shared;
+		}
 
 		hop.next_hop = path->hops[i].next_hop_index;
 		hop.out_port = path->hops[i].out_port->port;
@@ -538,9 +571,6 @@ int tb_path_activate(struct tb_path *path)
 		hop.unknown1 = 0;
 		hop.enable = 1;
 
-		out_mask = (i == path->path_length - 1) ?
-				TB_PATH_DESTINATION : TB_PATH_INTERNAL;
-		in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
 		hop.weight = path->weight;
 		hop.unknown2 = 0;
 		hop.priority = path->priority;
@@ -548,7 +578,7 @@ int tb_path_activate(struct tb_path *path)
 		hop.counter = path->hops[i].in_counter_index;
 		hop.counter_enable = path->hops[i].in_counter_index != -1;
 		hop.egress_fc = path->hops[i].egress_fc_enable;
-		hop.egress_shared_buffer = path->egress_shared_buffer & out_mask;
+		hop.egress_shared_buffer = path->hops[i].egress_shared_buffer;
 		/*
 		 * Protocol adapters IFC and ISE bits, and Path Credits
 		 * Allocated are vendor defined in the USB4 spec so we
@@ -559,7 +589,7 @@ int tb_path_activate(struct tb_path *path)
 			hop.initial_credits = path->hops[i].initial_credits;
 			hop.ingress_fc = path->hops[i].ingress_fc_enable;
 			hop.ingress_shared_buffer =
-				path->ingress_shared_buffer & in_mask;
+				path->hops[i].ingress_shared_buffer;
 		}
 
 		hop.unknown3 = 0;
@@ -570,13 +600,17 @@ int tb_path_activate(struct tb_path *path)
 				    2 * path->hops[i].in_hop_index, 2);
 		if (res) {
 			__tb_path_deactivate_hops(path, 0);
-			__tb_path_deallocate_nfc(path, 0);
-			goto err;
+			goto err_shared;
 		}
 	}
 	path->activated = true;
 	tb_dbg(path->tb, "%s path activation complete\n", path->name);
 	return 0;
+
+err_shared:
+	__tb_path_deallocate_shared(path, 0);
+err_nfc:
+	__tb_path_deallocate_nfc(path, 0);
 err:
 	tb_warn(path->tb, "%s path activation failed: %d\n", path->name, res);
 	return res;
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index a4b96950f3e6..8899fea713ac 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -459,6 +459,8 @@ static void tb_dump_port(struct tb *tb, const struct tb_port *port)
 	tb_dbg(tb, "  NFC Credits: %#x\n", regs->nfc_credits);
 	tb_dbg(tb, "  Credits (total/control): %u/%u\n", port->total_credits,
 	       port->ctl_credits);
+	tb_dbg(tb, "  Shared buffering: %s\n",
+	       str_yes_no(port->shared_buffering));
 }
 
 /**
@@ -587,7 +589,8 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  */
 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
 {
-	u32 nfc_credits;
+	u32 nfc_credits, val;
+	int ret;
 
 	if (credits == 0 || port->sw->is_unplugged)
 		return 0;
@@ -608,11 +611,78 @@ int tb_port_add_nfc_credits(struct tb_port *port, int credits)
 	tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
 		    port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
 
-	port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
-	port->config.nfc_credits |= nfc_credits;
+	val = port->config.nfc_credits & ~ADP_CS_4_NFC_BUFFERS_MASK;
+	val |= nfc_credits;
+
+	ret = tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
+	if (ret)
+		return ret;
+
+	port->config.nfc_credits = val;
+	return 0;
+}
+
+static int tb_port_set_shared_credits(struct tb_port *port,
+				      unsigned int credits)
+{
+	u32 val, lca;
+	int ret;
+
+	ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
+	if (ret)
+		return ret;
+
+	lca = FIELD_GET(ADP_CS_5_LCA_MASK, val);
+	if (lca == credits)
+		return 0;
+
+	val &= ~ADP_CS_5_LCA_MASK;
+	val |= FIELD_PREP(ADP_CS_5_LCA_MASK, credits);
+
+	return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
+}
 
-	return tb_port_write(port, &port->config.nfc_credits,
-			     TB_CFG_PORT, ADP_CS_4, 1);
+/**
+ * tb_port_enable_shared_buffering() - Enable/disable shared buffering
+ * @port: Ingress lane adapter to enable shared buffering
+ * @enable: Enable or disable shared buffering
+ *
+ * Enables/disables shared buffering for an ingress lane adapter. The
+ * credits should be set in @port->shared_credits before this is called.
+ * Can be called several times, the actual enable/disable happens only
+ * on first/last caller.
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+int tb_port_enable_shared_buffering(struct tb_port *port, bool enable)
+{
+	int ret;
+
+	if (port->sw->is_unplugged)
+		return 0;
+
+	if (enable) {
+		if (WARN_ON(!port->shared_credits))
+			return -EINVAL;
+		ret = tb_port_set_shared_credits(port, port->shared_credits);
+		if (ret) {
+			tb_port_warn(port, "failed to set shared buffers\n");
+			return ret;
+		}
+		port->shared_paths++;
+	} else {
+		if (WARN_ON(!port->shared_paths))
+			return -EINVAL;
+		if (--port->shared_paths)
+			return 0;
+		ret = tb_port_set_shared_credits(port, 0);
+		if (ret) {
+			tb_port_warn(port, "failed to clear shared buffers\n");
+			return ret;
+		}
+	}
+
+	return 0;
 }
 
 /**
@@ -770,9 +840,12 @@ static int tb_init_port(struct tb_port *port)
 		 */
 		if (port->cap_usb4) {
 			struct tb_regs_hop hop;
+			u32 val;
 
 			if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2))
 				port->ctl_credits = hop.initial_credits;
+			if (!tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_3, 1))
+				port->shared_buffering = !!(val & ADP_CS_3_SBC);
 		}
 		if (!port->ctl_credits)
 			port->ctl_credits = 2;
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 25d3966521a8..6da3492fd322 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -285,6 +285,10 @@ struct tb_bandwidth_group {
  *     |            | is kept all the time.
  *     +------------+ port->total_credits
  *
+ * Shared buffers follow the same rule as DMA. They are carved out of
+ * %USB4_CREDIT_POOL_COMMON if there is room, otherwise the minimum from
+ * the corresponding reserved pool.
+ *
  * If an XDomain is connected to the adapter only DMA can go through, so
  * all the credits stay in %USB4_CREDIT_POOL_COMMON. Lane 1 adapter of a
  * bonded link has no credits.
@@ -373,6 +377,12 @@ enum tb_credit_type {
  * @total_credits: Total number of buffers available for this port
  * @ctl_credits: Buffers reserved for control path
  * @credit_pool: Pool of credits used for tunneling (USB4 only)
+ * @shared_buffering: Lane adapter supports shared buffering
+ * @shared_paths: Number of paths going through this adapter using
+ *		  shared buffering
+ * @shared_users: Number of paths that have allocated shared buffers
+ * @shared_credits: Number of allocated shared buffers
+ * @shared_pool: Credit pool where shared credits are allocated from (USB4 only)
  * @group: Bandwidth allocation group the adapter is assigned to. Only
  *	   used for DP IN adapters for now.
  * @group_list: The adapter is linked to the group's list of ports through this
@@ -406,6 +416,11 @@ struct tb_port {
 	unsigned int total_credits;
 	unsigned int ctl_credits;
 	struct usb4_credit_pool *credit_pool;
+	bool shared_buffering;
+	unsigned int shared_paths;
+	unsigned int shared_users;
+	unsigned int shared_credits;
+	struct usb4_credit_pool *shared_pool;
 	struct tb_bandwidth_group *group;
 	struct list_head group_list;
 	unsigned int max_bw;
@@ -476,6 +491,8 @@ struct tb_retimer {
  *		 @in_port.
  * @ingress_fc_enable: Enable flow control in ingress adapter
  * @egress_fc_enable: Enable flow control in egress adapter
+ * @ingress_shared_buffer: Enable shared buffering for ingress adapter
+ * @egress_shared_buffer: Enable shared buffering for egress adapter
  * @credit_pool: Pointer to the credit pool used to hold
  *		 @initial_credits or @nfc_credits (%NULL if not used)
  * @credit_type: Type of credits allocated (valid when @credit_pool is set)
@@ -502,33 +519,17 @@ struct tb_path_hop {
 	unsigned int nfc_credits;
 	bool ingress_fc_enable;
 	bool egress_fc_enable;
+	bool ingress_shared_buffer;
+	bool egress_shared_buffer;
 	struct usb4_credit_pool *credit_pool;
 	enum tb_credit_type credit_type;
 	bool pm_support;
 };
 
-/**
- * enum tb_path_port - path options mask
- * @TB_PATH_NONE: Do not activate on any hop on path
- * @TB_PATH_SOURCE: Activate on the first hop (out of src)
- * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
- * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
- * @TB_PATH_ALL: Activate on all hops on the path
- */
-enum tb_path_port {
-	TB_PATH_NONE = 0,
-	TB_PATH_SOURCE = 1,
-	TB_PATH_INTERNAL = 2,
-	TB_PATH_DESTINATION = 4,
-	TB_PATH_ALL = 7,
-};
-
 /**
  * struct tb_path - a unidirectional path between two ports
  * @tb: Pointer to the domain structure
  * @name: Name of the path (used for debugging)
- * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
- * @egress_shared_buffer: Shared buffering used for egress ports on the path
  * @priority: Priority group if the path
  * @weight: Weight of the path inside the priority group
  * @drop_packages: Drop packages from queue tail or head
@@ -545,9 +546,6 @@ enum tb_path_port {
 struct tb_path {
 	struct tb *tb;
 	const char *name;
-	enum tb_path_port ingress_shared_buffer;
-	enum tb_path_port egress_shared_buffer;
-
 	unsigned int priority:3;
 	int weight:4;
 	bool drop_packages;
@@ -1211,6 +1209,7 @@ void tb_switch_xhci_disconnect(struct tb_switch *sw);
 int tb_port_state(struct tb_port *port);
 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
 int tb_port_add_nfc_credits(struct tb_port *port, int credits);
+int tb_port_enable_shared_buffering(struct tb_port *port, bool enable);
 int tb_port_clear_counter(struct tb_port *port, int counter);
 int tb_port_unlock(struct tb_port *port);
 int tb_port_enable(struct tb_port *port);
@@ -1332,6 +1331,10 @@ bool tb_path_port_on_path(const struct tb_path *path,
 #define TB_HOP_IFC		BIT(0)
 /* Use flow control in egress adapter */
 #define TB_HOP_EFC		BIT(1)
+/* Enable shared buffering in ingress adapter */
+#define TB_HOP_ISE		BIT(2)
+/* Enable shared buffering in egress adapter */
+#define TB_HOP_ESE		BIT(3)
 
 int tb_hop_alloc_credits(struct tb_path_hop *hop, enum tb_credit_type type,
 			 unsigned int flags);
diff --git a/drivers/thunderbolt/tb_regs.h b/drivers/thunderbolt/tb_regs.h
index 92d893634d2b..ca0f18798d95 100644
--- a/drivers/thunderbolt/tb_regs.h
+++ b/drivers/thunderbolt/tb_regs.h
@@ -311,6 +311,8 @@ struct tb_regs_port_header {
 } __packed;
 
 /* Basic adapter configuration registers */
+#define ADP_CS_3				0x03
+#define ADP_CS_3_SBC				BIT(31)
 #define ADP_CS_4				0x04
 #define ADP_CS_4_NFC_BUFFERS_MASK		GENMASK(9, 0)
 #define ADP_CS_4_TOTAL_BUFFERS_MASK		GENMASK(29, 20)
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index ffadd30bff7b..2ed3cb57d8a9 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -348,8 +348,6 @@ static int tb_pci_init_path(struct tb_path *path)
 	struct tb_path_hop *hop;
 	int i;
 
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_PCI_PRIORITY;
 	path->weight = TB_PCI_WEIGHT;
 	path->drop_packages = 0;
@@ -1413,8 +1411,6 @@ static int tb_dp_init_aux_path(struct tb_path *path, bool pm_support)
 	struct tb_path_hop *hop;
 	int i;
 
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DP_AUX_PRIORITY;
 	path->weight = TB_DP_AUX_WEIGHT;
 
@@ -1440,8 +1436,6 @@ static int tb_dp_init_video_path(struct tb_path *path, bool pm_support)
 	struct tb_path_hop *hop;
 	int i;
 
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DP_VIDEO_PRIORITY;
 	path->weight = TB_DP_VIDEO_WEIGHT;
 
@@ -1690,8 +1684,6 @@ static int tb_dma_init_path(struct tb_path *path)
 	struct tb_path_hop *hop;
 	int i;
 
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_DMA_PRIORITY;
 	path->weight = TB_DMA_WEIGHT;
 	path->clear_fc = true;
@@ -1984,8 +1976,6 @@ static int tb_usb3_init_path(struct tb_path *path)
 	struct tb_path_hop *hop;
 	int i;
 
-	path->egress_shared_buffer = TB_PATH_NONE;
-	path->ingress_shared_buffer = TB_PATH_NONE;
 	path->priority = TB_USB3_PRIORITY;
 	path->weight = TB_USB3_WEIGHT;
 	path->drop_packages = 0;
-- 
2.50.1


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

* [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (15 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 16/18] thunderbolt: Add support for shared flow control Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:32   ` sashiko-bot
  2026-11-19 17:12 ` [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering Mika Westerberg
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

From: Gil Fine <gil.fine@linux.intel.com>

Currently all DMA tunnels use the dedicated flow control scheme, which
allocates a separate pool of credits for every DMA tunnel on each link
it crosses.

Take for example the following topology, where the Host1 <-> Hub link is
an intra-domain link and the links from the Hub to Host2, Host3 and
Host4 are inter-domain links carrying DMA tunnels:

          --- Domain 1 ---
          [Host1: USB4 v1]
                 |
                 |
         [Hub:   USB4 v2]
         /       |        \
        /        |         \
   --Domain2-- --Domain3-- --Domain4--
       |         |            |
   [Host2]     [Host3]      [Host4]
   USB4v1      USB4v1       USB4v1

When several DMA tunnels share the same intra-domain link (Host1 <-> Hub)
and that link supports shared flow control, they can use shared
buffering instead, so there is no need to allocate a separate pool of
credits for each additional DMA tunnel.

Change the flow control from dedicated to restricted shared on
intra-domain links when both ends of the link support it. For links
where shared flow control is not supported, and for inter-domain links,
keep the existing dedicated flow control.

This allows more efficient use of credits on the link.

Signed-off-by: Gil Fine <gil.fine@linux.intel.com>
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/tunnel.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 2ed3cb57d8a9..3f2b6f8eb243 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -1679,6 +1679,34 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
 	return NULL;
 }
 
+static inline bool tb_port_shared_buffering_possible(const struct tb_port *port)
+{
+	if (!tb_port_is_null(port) || !port->remote)
+		return false;
+	if (!tb_port_use_credit_allocation(port))
+		return false;
+
+	/* Both ends of the link must support it */
+	return port->shared_buffering && port->remote->shared_buffering;
+}
+
+static unsigned int tb_hop_shared_flags(const struct tb_path *path, int i)
+{
+	const struct tb_path_hop *hop = &path->hops[i];
+	unsigned int flags = 0;
+
+	if (tb_port_shared_buffering_possible(hop->in_port))
+		flags |= TB_HOP_ISE;
+	if (i < path->path_length - 1) {
+		const struct tb_path_hop *next = &path->hops[i + 1];
+
+		if (tb_port_shared_buffering_possible(next->in_port))
+			flags |= TB_HOP_ESE;
+	}
+
+	return flags;
+}
+
 static int tb_dma_init_path(struct tb_path *path)
 {
 	struct tb_path_hop *hop;
@@ -1696,7 +1724,8 @@ static int tb_dma_init_path(struct tb_path *path)
 		if (!tb_port_is_nhi(hop->out_port))
 			flags |= TB_HOP_EFC;
 
-		ret = tb_hop_alloc_credits(hop, TB_CREDITS_DMA, flags);
+		ret = tb_hop_alloc_credits(hop, TB_CREDITS_DMA, flags |
+					   tb_hop_shared_flags(path, i));
 		if (ret)
 			return ret;
 	}
-- 
2.50.1


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

* [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering
  2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
                   ` (16 preceding siblings ...)
  2026-11-19 17:12 ` [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links Mika Westerberg
@ 2026-11-19 17:12 ` Mika Westerberg
  2026-11-19 17:28   ` sashiko-bot
  17 siblings, 1 reply; 37+ messages in thread
From: Mika Westerberg @ 2026-11-19 17:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Gil Fine,
	Rene Sapiens, Pooja Katiyar, Mika Westerberg

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

This adds various test cases to check the shared buffering support.

While there, check the per-hop flow control settings in the existing
credit allocation and tunnel test cases too. Egress flow control
follows the ingress flow control of the next hop except on the last hop
where it depends on the protocol, so make sure that is covered as well.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/test.c | 598 +++++++++++++++++++++++++++++++++++++
 1 file changed, 598 insertions(+)

diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index d6679bcc4ac3..3fa9c4661541 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -1381,6 +1381,30 @@ static void tb_test_path_mixed_chain_reverse(struct kunit *test)
 	tb_path_free(path);
 }
 
+static void tb_test_check_path_fc(struct kunit *test,
+				  const struct tb_path *path, bool last_efc)
+{
+	int i;
+
+	for (i = 0; i < path->path_length; i++) {
+		bool efc = i < path->path_length - 1 ? true : last_efc;
+
+		KUNIT_EXPECT_TRUE(test, path->hops[i].ingress_fc_enable);
+		KUNIT_EXPECT_EQ(test, path->hops[i].egress_fc_enable, efc);
+	}
+}
+
+static void tb_test_check_path_nfc(struct kunit *test,
+				   const struct tb_path *path)
+{
+	int i;
+
+	for (i = 0; i < path->path_length; i++) {
+		KUNIT_EXPECT_FALSE(test, path->hops[i].ingress_fc_enable);
+		KUNIT_EXPECT_FALSE(test, path->hops[i].egress_fc_enable);
+	}
+}
+
 static void tb_test_tunnel_pcie(struct kunit *test)
 {
 	struct tb_switch *host, *dev1, *dev2;
@@ -1523,6 +1547,10 @@ static void tb_test_tunnel_dp_chain(struct kunit *test)
 	KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 3);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[2].out_port, in);
+	tb_test_check_path_nfc(test, tunnel->paths[0]);
+	tb_test_check_path_fc(test, tunnel->paths[1], false);
+	tb_test_check_path_fc(test, tunnel->paths[2], false);
+
 	tb_tunnel_put(tunnel);
 }
 
@@ -1655,6 +1683,10 @@ static void tb_test_tunnel_dp_max_length(struct kunit *test)
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[6].out_port,
 			    &host->ports[1]);
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[12].out_port, in);
+	tb_test_check_path_nfc(test, tunnel->paths[0]);
+	tb_test_check_path_fc(test, tunnel->paths[1], false);
+	tb_test_check_path_fc(test, tunnel->paths[2], false);
+
 	tb_tunnel_put(tunnel);
 }
 
@@ -1948,6 +1980,8 @@ static void tb_test_tunnel_dma_rx(struct kunit *test)
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, nhi);
 	KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 2);
 
+	tb_test_check_path_fc(test, tunnel->paths[0], false);
+
 	tb_tunnel_put(tunnel);
 }
 
@@ -1985,6 +2019,8 @@ static void tb_test_tunnel_dma_tx(struct kunit *test)
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, port);
 	KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 15);
 
+	tb_test_check_path_fc(test, tunnel->paths[0], true);
+
 	tb_tunnel_put(tunnel);
 }
 
@@ -2051,6 +2087,9 @@ static void tb_test_tunnel_dma_chain(struct kunit *test)
 	KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[2].out_port, port);
 	KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[2].next_hop_index, 8);
 
+	tb_test_check_path_fc(test, tunnel->paths[0], false);
+	tb_test_check_path_fc(test, tunnel->paths[1], true);
+
 	tb_tunnel_put(tunnel);
 }
 
@@ -2123,6 +2162,7 @@ static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test)
 
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2130,6 +2170,7 @@ static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test)
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2156,6 +2197,7 @@ static void tb_test_credit_alloc_legacy_bonded(struct kunit *test)
 
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2163,6 +2205,7 @@ static void tb_test_credit_alloc_legacy_bonded(struct kunit *test)
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 7U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2192,6 +2235,7 @@ static void tb_test_credit_alloc_pcie(struct kunit *test)
 
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2199,6 +2243,7 @@ static void tb_test_credit_alloc_pcie(struct kunit *test)
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2409,6 +2454,7 @@ static void tb_test_credit_alloc_without_dp(struct kunit *test)
 	/* PCIe downstream path */
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2417,6 +2463,7 @@ static void tb_test_credit_alloc_without_dp(struct kunit *test)
 	/* PCIe upstream path */
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2452,6 +2499,7 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	/* Video (main) path */
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_nfc(test, path);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 18U);
@@ -2460,6 +2508,7 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	/* AUX TX */
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2468,6 +2517,7 @@ static void tb_test_credit_alloc_dp(struct kunit *test)
 	/* AUX RX */
 	path = tunnel->paths[2];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2500,6 +2550,7 @@ static void tb_test_credit_alloc_usb3(struct kunit *test)
 
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2507,6 +2558,7 @@ static void tb_test_credit_alloc_usb3(struct kunit *test)
 
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2597,6 +2649,7 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
 	/* DMA RX */
 	path = tunnel->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2605,6 +2658,7 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
 	/* DMA TX */
 	path = tunnel->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2664,6 +2718,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel1->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2671,6 +2726,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel1->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2682,6 +2738,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel2->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2689,6 +2746,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel2->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2700,6 +2758,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel3->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2707,6 +2766,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel3->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2718,6 +2778,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel4->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 4U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2725,6 +2786,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel4->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2736,6 +2798,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel5->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 1U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2743,6 +2806,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel5->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2759,6 +2823,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel6->paths[0];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, false);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2766,6 +2831,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 
 	path = tunnel6->paths[1];
 	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_path_fc(test, path, true);
 	KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 0U);
 	KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
@@ -2781,6 +2847,531 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
 	usb4_switch_free_credit_pools(host);
 }
 
+static void tb_switch_enable_shared_buffering(struct tb_switch *sw)
+{
+	struct tb_port *port;
+
+	tb_switch_for_each_port(sw, port) {
+		if (tb_port_is_null(port))
+			port->shared_buffering = true;
+	}
+}
+
+static void tb_switch_disable_shared_buffering(struct tb_switch *sw)
+{
+	struct tb_port *port;
+
+	tb_switch_for_each_port(sw, port)
+		port->shared_buffering = false;
+}
+
+static void tb_test_check_hop(struct kunit *test, const struct tb_path_hop *hop,
+			      unsigned int initial_credits, bool ingress_shared,
+			      bool egress_shared)
+{
+	KUNIT_EXPECT_EQ(test, hop->nfc_credits, 0U);
+	KUNIT_EXPECT_EQ(test, hop->initial_credits, initial_credits);
+	KUNIT_EXPECT_EQ(test, hop->ingress_shared_buffer, ingress_shared);
+	KUNIT_EXPECT_EQ(test, hop->egress_shared_buffer, egress_shared);
+}
+
+static void tb_test_check_shared(struct kunit *test, const struct tb_port *port,
+				 unsigned int shared_credits,
+				 unsigned int shared_users)
+{
+	KUNIT_EXPECT_EQ(test, port->shared_credits, shared_credits);
+	KUNIT_EXPECT_EQ(test, port->shared_users, shared_users);
+}
+
+static void tb_test_check_dma_tunnel(struct kunit *test,
+				     const struct tb_tunnel *tunnel,
+				     unsigned int ingress_dev,
+				     unsigned int ingress_host,
+				     unsigned int egress_dev, bool shared)
+{
+	const struct tb_path *path;
+
+	KUNIT_ASSERT_NOT_NULL(test, tunnel);
+	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+
+	/* DMA RX */
+	path = tunnel->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_hop(test, &path->hops[0], ingress_dev, false, shared);
+	tb_test_check_hop(test, &path->hops[1], ingress_host, shared, false);
+	tb_test_check_path_fc(test, path, false);
+
+	/* DMA TX */
+	path = tunnel->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_hop(test, &path->hops[0], 0, false, shared);
+	tb_test_check_hop(test, &path->hops[1], egress_dev, shared, false);
+	tb_test_check_path_fc(test, path, true);
+}
+
+static void tb_test_credit_alloc_dma_shared(struct kunit *test)
+{
+	struct tb_switch *host, *dev;
+	struct tb_port *nhi, *port;
+	struct tb_tunnel *tunnel;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	nhi = &host->ports[7];
+	port = &dev->ports[3];
+
+	/*
+	 * The link between the host and the device uses shared
+	 * buffering in both directions. The device ingress lane adapter
+	 * (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);
+	tb_test_check_dma_tunnel(test, tunnel, 14, 14, 14, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev->ports[3], 0, 0);
+
+	tb_tunnel_put(tunnel);
+
+	tb_test_check_shared(test, &host->ports[1], 0, 0);
+	tb_test_check_shared(test, &dev->ports[1], 0, 0);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
+{
+	struct tb_tunnel *tunnel1, *tunnel2, *tunnel3, *tunnel4;
+	struct tb_tunnel *tunnel5, *tunnel6;
+	struct tb_switch *host, *dev;
+	struct tb_port *nhi, *port;
+
+	host = alloc_host_br(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	nhi = &host->ports[7];
+	port = &dev->ports[3];
+
+	/*
+	 * Same as tb_test_credit_alloc_dma_multiple() but intra-domain
+	 * links use shared buffering so they get the full credits. The
+	 * 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);
+	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, true);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, true);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, true);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 14, true);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 14, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 5);
+	tb_test_check_shared(test, &dev->ports[1], 14, 5);
+
+	/*
+	 * This fails at the first hop of the RX path, before any of the
+	 * 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);
+	KUNIT_ASSERT_NULL(test, tunnel6);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 5);
+	tb_test_check_shared(test, &dev->ports[1], 14, 5);
+
+	tb_tunnel_put(tunnel1);
+
+	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);
+	tb_test_check_dma_tunnel(test, tunnel6, 14, 14, 14, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 5);
+	tb_test_check_shared(test, &dev->ports[1], 14, 5);
+
+	tb_tunnel_put(tunnel6);
+	tb_tunnel_put(tunnel5);
+	tb_tunnel_put(tunnel4);
+	tb_tunnel_put(tunnel3);
+	tb_tunnel_put(tunnel2);
+
+	tb_test_check_shared(test, &host->ports[1], 0, 0);
+	tb_test_check_shared(test, &dev->ports[1], 0, 0);
+
+	/*
+	 * All the shared buffers must have been returned to the common
+	 * pool so with dedicated buffering we should get the same
+	 * allocation as in tb_test_credit_alloc_dma_multiple().
+	 */
+	tb_switch_disable_shared_buffering(host);
+	tb_switch_disable_shared_buffering(dev);
+
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
+	tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+	KUNIT_ASSERT_NULL(test, tunnel6);
+
+	tb_tunnel_put(tunnel5);
+	tb_tunnel_put(tunnel4);
+	tb_tunnel_put(tunnel3);
+	tb_tunnel_put(tunnel2);
+	tb_tunnel_put(tunnel1);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
+{
+	struct tb_tunnel *tunnel1, *tunnel2, *tunnel3, *tunnel4;
+	struct tb_tunnel *tunnel5;
+	struct tb_switch *host, *dev;
+	struct tb_port *nhi, *port;
+
+	host = alloc_host_br(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	nhi = &host->ports[7];
+	port = &dev->ports[3];
+
+	/*
+	 * First take most of the device upstream port common pool
+	 * with dedicated buffers. This leaves 52 - 3 * 14 = 10 credits
+	 * there.
+	 */
+	tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
+
+	/*
+	 * Now enable shared buffering. The host side still has plenty
+	 * so it gets 14 shared buffers but the device side only gets
+	 * the remaining 10 and the path cannot use more than that.
+	 */
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev);
+
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	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);
+	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 10, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 2);
+	tb_test_check_shared(test, &dev->ports[1], 10, 2);
+
+	tb_tunnel_put(tunnel4);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev->ports[1], 10, 1);
+
+	tb_tunnel_put(tunnel5);
+
+	tb_test_check_shared(test, &host->ports[1], 0, 0);
+	tb_test_check_shared(test, &dev->ports[1], 0, 0);
+
+	/*
+	 * The 10 credits must be back in the device common pool so the
+	 * dedicated allocation continues as in
+	 * tb_test_credit_alloc_dma_multiple().
+	 */
+	tb_switch_disable_shared_buffering(host);
+	tb_switch_disable_shared_buffering(dev);
+
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
+	tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+	tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
+
+	tb_tunnel_put(tunnel5);
+	tb_tunnel_put(tunnel4);
+	tb_tunnel_put(tunnel3);
+	tb_tunnel_put(tunnel2);
+	tb_tunnel_put(tunnel1);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
+{
+	struct tb_tunnel *tunnel1, *tunnel2, *tunnel3, *tunnel4;
+	struct tb_tunnel *tunnel5, *tunnel6;
+	struct usb4_credit_pool *common, *dma;
+	struct tb_port *nhi, *port, *up;
+	struct tb_switch *host, *dev;
+	unsigned int common_used;
+
+	host = alloc_host_br(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	nhi = &host->ports[7];
+	port = &dev->ports[3];
+	up = &dev->ports[1];
+	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);
+	tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
+	tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+	tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
+	tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+	tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
+	tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+	tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
+
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_available(common), 0U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 0U);
+	common_used = usb4_credit_pool_used(common);
+
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev);
+
+	/*
+	 * The common pool of the device upstream port is exhausted so
+	 * 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);
+	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);
+	KUNIT_EXPECT_PTR_EQ(test, tunnel5->paths[1]->hops[1].credit_pool, dma);
+	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);
+	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);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 1U);
+
+	/*
+	 * Release the tunnel that allocated the shared buffers first so
+	 * that the joiner returns them.
+	 */
+	tb_tunnel_put(tunnel5);
+	tb_test_check_shared(test, up, 1, 1);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 1U);
+
+	tb_tunnel_put(tunnel6);
+	tb_test_check_shared(test, up, 0, 0);
+	KUNIT_EXPECT_NULL(test, up->shared_pool);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 0U);
+	KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(common), common_used);
+
+	tb_tunnel_put(tunnel4);
+	tb_tunnel_put(tunnel3);
+	tb_tunnel_put(tunnel2);
+	tb_tunnel_put(tunnel1);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_shared_mixed(struct kunit *test)
+{
+	struct tb_switch *host, *dev1, *dev2;
+	struct tb_port *nhi, *port;
+	struct tb_tunnel *tunnel;
+	struct tb_path *path;
+
+	/*
+	 * Host <-> Dev1 link supports shared buffering but Dev1 <-> Dev2
+	 * does not because Dev2 lacks support so that link must use
+	 * dedicated buffers.
+	 */
+	host = alloc_host_usb4(test);
+	dev1 = alloc_dev_usb4(test, host, 0x1, true);
+	dev2 = alloc_dev_usb4(test, dev1, 0x301, true);
+
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev1);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev1), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev2), 0);
+
+	nhi = &host->ports[7];
+	port = &dev2->ports[3];
+
+	tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel);
+	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+
+	/* DMA RX */
+	path = tunnel->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 3);
+	tb_test_check_hop(test, &path->hops[0], 14, false, false);
+	tb_test_check_hop(test, &path->hops[1], 14, false, true);
+	tb_test_check_hop(test, &path->hops[2], 14, true, false);
+	tb_test_check_path_fc(test, path, false);
+
+	/* DMA TX */
+	path = tunnel->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 3);
+	tb_test_check_hop(test, &path->hops[0], 0, false, true);
+	tb_test_check_hop(test, &path->hops[1], 14, true, false);
+	tb_test_check_hop(test, &path->hops[2], 14, false, false);
+	tb_test_check_path_fc(test, path, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev1->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev1->ports[3], 0, 0);
+	tb_test_check_shared(test, &dev2->ports[1], 0, 0);
+
+	tb_tunnel_put(tunnel);
+
+	tb_test_check_shared(test, &host->ports[1], 0, 0);
+	tb_test_check_shared(test, &dev1->ports[1], 0, 0);
+
+	usb4_switch_free_credit_pools(dev2);
+	usb4_switch_free_credit_pools(dev1);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_shared_no_credit_allocation(struct kunit *test)
+{
+	struct tb_switch *host, *dev;
+	struct tb_port *nhi, *port;
+	struct tb_tunnel *tunnel;
+	struct tb_path *path;
+
+	host = alloc_host_usb4(test);
+	dev = alloc_dev_usb4(test, host, 0x1, true);
+	dev->credit_allocation = false;
+
+	tb_switch_enable_shared_buffering(host);
+	tb_switch_enable_shared_buffering(dev);
+
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+	nhi = &host->ports[7];
+	port = &dev->ports[3];
+
+	/*
+	 * Shared buffering needs the credit allocation so only the
+	 * 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);
+	KUNIT_ASSERT_NOT_NULL(test, tunnel);
+	KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+
+	/* DMA RX */
+	path = tunnel->paths[0];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_hop(test, &path->hops[0], 6, false, true);
+	tb_test_check_hop(test, &path->hops[1], 14, true, false);
+	tb_test_check_path_fc(test, path, false);
+
+	/* DMA TX */
+	path = tunnel->paths[1];
+	KUNIT_ASSERT_EQ(test, path->path_length, 2);
+	tb_test_check_hop(test, &path->hops[0], 0, false, false);
+	tb_test_check_hop(test, &path->hops[1], 14, false, false);
+	tb_test_check_path_fc(test, path, true);
+
+	tb_test_check_shared(test, &host->ports[1], 14, 1);
+	tb_test_check_shared(test, &dev->ports[1], 0, 0);
+
+	tb_tunnel_put(tunnel);
+
+	tb_test_check_shared(test, &host->ports[1], 0, 0);
+
+	usb4_switch_free_credit_pools(dev);
+	usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_shared_hop(struct kunit *test)
+{
+	struct tb_path_hop hop1 = {}, hop2 = {}, hop3 = {};
+	struct tb_switch *host;
+	struct tb_port *port;
+
+	host = alloc_host_usb4(test);
+	KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+
+	port = &host->ports[1];
+
+	/* Shared without dedicated buffers (PCA is not used) */
+	hop1.in_port = port;
+	KUNIT_ASSERT_EQ(test, tb_hop_alloc_credits(&hop1, TB_CREDITS_DMA,
+						   TB_HOP_ISE), 0);
+	KUNIT_EXPECT_EQ(test, hop1.initial_credits, 0U);
+	KUNIT_EXPECT_EQ(test, hop1.nfc_credits, 0U);
+	KUNIT_EXPECT_FALSE(test, hop1.ingress_fc_enable);
+	KUNIT_EXPECT_TRUE(test, hop1.ingress_shared_buffer);
+	tb_test_check_shared(test, port, 14, 1);
+
+	/* Restricted shared joins the same shared buffers */
+	hop2.in_port = port;
+	KUNIT_ASSERT_EQ(test, tb_hop_alloc_credits(&hop2, TB_CREDITS_DMA,
+						   TB_HOP_IFC | TB_HOP_ISE), 0);
+	KUNIT_EXPECT_EQ(test, hop2.initial_credits, 14U);
+	KUNIT_EXPECT_EQ(test, hop2.nfc_credits, 0U);
+	KUNIT_EXPECT_TRUE(test, hop2.ingress_fc_enable);
+	KUNIT_EXPECT_TRUE(test, hop2.ingress_shared_buffer);
+	tb_test_check_shared(test, port, 14, 2);
+
+	/* Releasing hop that was never allocated is a no-op */
+	hop3.in_port = port;
+	hop3.ingress_shared_buffer = true;
+	tb_hop_release_credits(&hop3);
+	tb_test_check_shared(test, port, 14, 2);
+
+	tb_hop_release_credits(&hop1);
+	tb_test_check_shared(test, port, 14, 1);
+	tb_hop_release_credits(&hop2);
+	tb_test_check_shared(test, port, 0, 0);
+	KUNIT_EXPECT_EQ(test, hop2.initial_credits, 0U);
+
+	usb4_switch_free_credit_pools(host);
+}
+
 static struct tb_tunnel *TB_TEST_PCIE_TUNNEL(struct kunit *test,
 			struct tb_switch *host, struct tb_switch *dev)
 {
@@ -3558,6 +4149,13 @@ static struct kunit_case tb_test_cases[] = {
 	KUNIT_CASE(tb_test_credit_alloc_usb3),
 	KUNIT_CASE(tb_test_credit_alloc_dma),
 	KUNIT_CASE(tb_test_credit_alloc_dma_multiple),
+	KUNIT_CASE(tb_test_credit_alloc_dma_shared),
+	KUNIT_CASE(tb_test_credit_alloc_dma_multiple_shared),
+	KUNIT_CASE(tb_test_credit_alloc_dma_shared_partial),
+	KUNIT_CASE(tb_test_credit_alloc_dma_shared_reserved),
+	KUNIT_CASE(tb_test_credit_alloc_dma_shared_mixed),
+	KUNIT_CASE(tb_test_credit_alloc_dma_shared_no_credit_allocation),
+	KUNIT_CASE(tb_test_credit_alloc_shared_hop),
 	KUNIT_CASE(tb_test_credit_alloc_all),
 	KUNIT_CASE(tb_test_property_parse),
 	KUNIT_CASE(tb_test_property_format),
-- 
2.50.1


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

* Re: [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable()
  2026-11-19 17:12 ` [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable() Mika Westerberg
@ 2026-11-19 17:22   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:22 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> This is the opposite of usb4_port_hotplug_enable() and allows the
> connection manager to mask hot plug events of a USB4 port. This is
> needed in subsequent patches.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=7


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

* Re: [PATCH 10/18] thunderbolt: Split host router port reset into a separate function
  2026-11-19 17:12 ` [PATCH 10/18] thunderbolt: Split host router port reset into a separate function Mika Westerberg
@ 2026-11-19 17:22   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:22 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> Move the part of tb_switch_reset_host() that resets the host router
> lane adapters and cleans up the protocol adapter path config spaces into
> a separate function tb_switch_reset_host_ports(). This makes it easier
> to extend tb_switch_reset_host() in subsequent patches. No functional
> changes.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=10


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

* Re: [PATCH 09/18] thunderbolt: Add tb_port_state_is_up()
  2026-11-19 17:12 ` [PATCH 09/18] thunderbolt: Add tb_port_state_is_up() Mika Westerberg
@ 2026-11-19 17:22   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:22 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> Move the check whether the link of a lane adapter is up from
> tb_wait_for_port() into a helper function. We are going to use it in
> subsequent patches. No functional changes.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=9


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

* Re: [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running
  2026-11-19 17:12 ` [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running Mika Westerberg
@ 2026-11-19 17:25   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:25 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> Use -ESHUTDOWN like the ring layer does when the control channel is not
> running, and drop the WARN(). On buses like Thunderbolt/USB4 user can
> unplug a device any time so it is expected that control channel reads
> and writes fail and the callers are supposed to handle that.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=6


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

* Re: [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host()
  2026-11-19 17:12 ` [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host() Mika Westerberg
@ 2026-11-19 17:25   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:25 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> We are going to call it from tb_switch_reset_host() in a subsequent
> patch so move it there. No functional changes.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=8


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

* Re: [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation
  2026-11-19 17:12 ` [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation Mika Westerberg
@ 2026-11-19 17:26   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:26 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> The only user of tunnel discovery without HopID allocation was the
> resume path from suspend-to-disk, which now resets the topology
> instead. Remove the now unused alloc_hopid parameter and always
> allocate HopIDs when discovering paths.
> 
> No functional changes.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=12


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

* Re: [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails
  2026-11-19 17:12 ` [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails Mika Westerberg
@ 2026-11-19 17:26   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:26 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> If DPRX negotiation fails, for example the DP IN that was tried does not
> have cable connected to the add-in-card, we call tb_dp_resource_unavailable()
> and that then tries to pair the DP OUT with another DP IN. However, at
> that point the existing tunnel may be still referenced so its HopIDs are
> reserved and that causes the new tunnel activation to fail.
> 
> Instead schedule a worker that runs on the same work queue as the DPRX
> work so the reference should be dropped at that point.
> 
> Fixes: 67600ccfc4f3 ("thunderbolt: Fix use-after-free in tb_dp_dprx_work")
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=3


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

* Re: [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests
  2026-11-19 17:12 ` [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests Mika Westerberg
@ 2026-11-19 17:26   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:26 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> The tests allocate routers without a domain (struct tb) but some of the
> tunneling parameters, such as the buffer allocation, need to be read
> from the host router which is reached through the domain. For this
> reason allocate a dummy domain structure, and fill in the host router
> pointer accordingly.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=14


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

* Re: [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held
  2026-11-19 17:12 ` [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held Mika Westerberg
@ 2026-11-19 17:28   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:28 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> tb_dp_dprx_work() decides the outcome of the DPRX read under tb->lock
> but then releases the lock and calls the callback which takes it again.
> Between the two something else can grab the lock and change the domain
> state, for example stop the control channel, and the callback then acts
> on a decision that is no longer valid.
> 
> Fix this by calling the callback in the same tb->lock critical section
> that decided the DPRX result.
> 
> Fixes: d6d458d42e1e ("thunderbolt: Handle DisplayPort tunnel activation asynchronously")
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=4


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

* Re: [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering
  2026-11-19 17:12 ` [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering Mika Westerberg
@ 2026-11-19 17:28   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:28 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> This adds various test cases to check the shared buffering support.
> 
> While there, check the per-hop flow control settings in the existing
> credit allocation and tunnel test cases too. Egress flow control
> follows the ingress flow control of the next hop except on the last hop
> where it depends on the protocol, so make sure that is covered as well.
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=18


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

* Re: [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal
  2026-11-19 17:12 ` [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal Mika Westerberg
@ 2026-11-19 17:32   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:32 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] thunderbolt: use-after-free of unplugged xdomain via
  tb_xdomain_resume() [1]
--

commit 2656e124696e0ac2d72fefe13421cb4187bc49b6
Author: Mika Westerberg (Intel) <westeri@kernel.org>

thunderbolt: Avoid leaking of DMA tunnel after XDomain removal

This commit moves the xd->is_unplugged checks and adapter lookups under
tb->lock in tb_approve_xdomain_paths() and tb_disconnect_xdomain_paths()
to prevent creating or leaking DMA tunnels while an XDomain is being
unregistered.

> diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
> index 418ad0a61a5de..655bcc699410e 100644
> --- a/drivers/thunderbolt/tb.c
> +++ b/drivers/thunderbolt/tb.c
> @@ -2336,12 +2336,18 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
>  	struct tb_switch *sw;
>  	int ret;
>  
> +	mutex_lock(&tb->lock);
> +
> +	/* The XDomain is going away so no new tunnels allowed */
> +	if (xd->is_unplugged) {
> +		ret = -ENODEV;
> +		goto err_unlock;
> +	}

[Severity: High]
This problem wasn't introduced by this patch, but can tb_xdomain_resume()
trigger a use-after-free on an unplugged XDomain [1]?

Commit a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without
holding tb->lock") deferred unregistering unplugged XDomains from
tb_resume_noirq() to tb_complete().  Because xd->dev remains registered
during the intervening dpm_resume phase, tb_xdomain_resume() runs without
checking xd->removing or xd->is_unplugged and restarts xd->state_work
after tb_xdomain_remove() has already stopped it:

CPU0                                    kworker
tb_resume_noirq()
  ...
  tb_xdomain_remove(xd)
    xd->removing = true
    stop_handshake(xd)

tb_xdomain_resume(&xd->dev)
  start_handshake(xd)
    queue_delayed_work(..., &xd->state_work, ...)

tb_complete()
  ...
  tb_xdomain_unregister(xd)
    device_unregister(&xd->dev)
      tb_xdomain_release(&xd->dev)
        kfree(xd)
                                        tb_xdomain_state_work()
                                          xd->...  <-- use-after-free

[1] https://sashiko.dev/#/bug/linux-e4eef44b-8ad5-49c0-ac52-81edebd4fd36

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=1

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

* Re: [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links
  2026-11-19 17:12 ` [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links Mika Westerberg
@ 2026-11-19 17:32   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:32 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: Gil Fine <gil.fine@linux.intel.com>
> 
> Currently all DMA tunnels use the dedicated flow control scheme, which
> allocates a separate pool of credits for every DMA tunnel on each link
> it crosses.
> 
> Take for example the following topology, where the Host1 <-> Hub link is
> an intra-domain link and the links from the Hub to Host2, Host3 and
> Host4 are inter-domain links carrying DMA tunnels:
> 
>           --- Domain 1 ---
>           [Host1: USB4 v1]
>                  |
>                  |
>          [Hub:   USB4 v2]
> [ ... ]
> 
> This allows more efficient use of credits on the link.
> 
> Signed-off-by: Gil Fine <gil.fine@linux.intel.com>
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=17


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

* Re: [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called
  2026-11-19 17:12 ` [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called Mika Westerberg
@ 2026-11-19 17:34   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:34 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> When tb_tunnel_put() is called with last reference it will call
> tb_tunnel_destroy() to release the memory. In addition to this it also
> touches adapter states (releasing the HopIDs and dma_credits). All other
> users do this under the domain lock except tb_dp_dprx_work() which drops
> the reference without it. Fix that here.
> 
> We can drop tb_tunnel_lock too because all the existing users have a
> valid reference already when they call kref_get()/put().
> 
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=13


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

* Re: [PATCH 16/18] thunderbolt: Add support for shared flow control
  2026-11-19 17:12 ` [PATCH 16/18] thunderbolt: Add support for shared flow control Mika Westerberg
@ 2026-11-19 17:34   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:34 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: Gil Fine <gil.fine@linux.intel.com>
> 
> It is possible to make several paths share the ingress adapter buffers.
> There are two possible configurations: shared flow control and
> restricted shared flow control where the former shares all the
> configured buffers and the latter shares specified amount of the
> configured buffers for the paths going through an ingress adapter.
> 
> This adds support for both methods.
> 
> While there fix the rollback of NFC credits to start from correct index.
> 
> Signed-off-by: Gil Fine <gil.fine@linux.intel.com>
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=16


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

* Re: [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every XDomain removal
  2026-11-19 17:12 ` [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every " Mika Westerberg
@ 2026-11-19 17:34   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:34 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> Currently we only tear down DMA tunnels when the XDomain is unplugged
> (and when service drivers explicitly do that) but not when the XDomain
> is removed as part of resume or replaced by a router in tb_scan_port().
> The tunnel then stays in the tunnel list until the driver is unloaded
> (or the router it goes through is removed).
> 
> Fix this by always tearing down the tunnels when XDomain is removed.
> 
> Fixes: 7ea4cd6b2010 ("thunderbolt: Add support for XDomain connections")
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=2


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

* Re: [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume
  2026-11-19 17:12 ` [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume Mika Westerberg
@ 2026-11-19 17:35   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:35 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] thunderbolt: missing tb_pci_post_activate() in
  tb_runtime_resume()
--

commit 087ef2024705f5c81e4d876d241a868e52e4e3b5
Author: Mika Westerberg (Intel) <westeri@kernel.org>

thunderbolt: Re-apply Alpine/Titan Ridge flow on resume

This commit extracts the PCIe L1 enable and xHCI connect steps into
tb_pci_post_activate() and calls it when re-activating PCIe tunnels in
tb_resume_noirq().

> diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
> index 3c4903adb687d..6a11d686bdf14 100644
> --- a/drivers/thunderbolt/tb.c
> +++ b/drivers/thunderbolt/tb.c
> @@ -2315,6 +2315,21 @@ static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
>  	return 0;
>  }
>  
> +static void tb_pci_post_activate(struct tb_tunnel *tunnel)
> +{
> +	struct tb_switch *sw = tunnel->dst_port->sw;
> +
> +	/*
> +	 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
> +	 * here.
> +	 */
> +	if (tb_switch_pcie_l1_enable(sw))
> +		tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
> +
> +	if (tb_switch_xhci_connect(sw))
> +		tb_sw_warn(sw, "failed to connect xHCI\n");
> +}

[ ... ]

> @@ -3239,6 +3246,8 @@ static int tb_resume_noirq(struct tb *tb)
>  			usb3_delay = 0;
>  		}
>  		tb_tunnel_activate(tunnel);
> +		if (tb_tunnel_is_pci(tunnel))
> +			tb_pci_post_activate(tunnel);
>  	}

[Severity: Medium]
Should tb_runtime_resume() also call tb_pci_post_activate() when it
re-activates PCIe tunnels?

drivers/thunderbolt/tb.c:tb_runtime_resume() {
	...
	tb_switch_resume(tb->root_switch, true);
	tb_free_invalid_tunnels(tb);
	tb_restore_children(tb->root_switch);
	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
		tb_tunnel_activate(tunnel);
	...
}

Generation 3 routers use runtime PM (tb_runtime_suspend() puts the routers
into sleep via tb_switch_suspend()), and when the NHI is already
runtime-suspended at the start of system sleep, nhi_complete() resumes the
domain through pm_runtime_resume() -> tb_runtime_resume() instead of
tb_resume_noirq().  Without tb_pci_post_activate() there, Titan Ridge PCIe
L1 and Alpine/Titan Ridge internal xHCI remain unconfigured after runtime
resume.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=5

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

* Re: [PATCH 15/18] thunderbolt: Rework tunnel credit allocation
  2026-11-19 17:12 ` [PATCH 15/18] thunderbolt: Rework tunnel credit allocation Mika Westerberg
@ 2026-11-19 17:43   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:43 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] thunderbolt: data race on port->credit_pool in
  port_credits_show()
--

commit c28526c3bb35bc796324ed492685bd1fa66796c3
Author: Mika Westerberg (Intel) <westeri@kernel.org>

thunderbolt: Rework tunnel credit allocation

This commit reworks lane adapter tunnel credit allocation to use per-port
credit pools with minimum protocol reservations and on-demand allocation
from a common pool, and exposes pool usage in debugfs.

> diff --git a/drivers/thunderbolt/debugfs.c b/drivers/thunderbolt/debugfs.c
> index 0d9f41a0f2cdc..eac6d564bf185 100644
> --- a/drivers/thunderbolt/debugfs.c
> +++ b/drivers/thunderbolt/debugfs.c
> @@ -2488,6 +2488,32 @@ static int port_sb_regs_show(struct seq_file *s, void *not_used)
>  }
>  DEBUGFS_ATTR_RW(port_sb_regs);
>  
> +static void credits_show(const struct tb_port *port, struct seq_file *s)
> +{
> +	int i;
> +
> +	seq_puts(s, "# pool used total\n");
> +
> +	for (i = 0; i < USB4_CREDIT_NUM_POOLS; i++) {
> +		const struct usb4_credit_pool *pool = &port->credit_pool[i];
> +
> +		seq_printf(s, "%s %u %u\n", usb4_credit_pool_name(pool),
> +			   usb4_credit_pool_used(pool),
> +			   usb4_credit_pool_total(pool));
> +	}
> +}
> +
> +static int port_credits_show(struct seq_file *s, void *not_used)
> +{
> +	const struct tb_port *port = s->private;
> +
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->sw->tb->lock)
> +		credits_show(port, s);
> +
> +	return 0;
> +}

[Severity: Low]
Can port_credits_show() race with usb4_port_update_credit_pools() on
port->credit_pool?

usb4_port_update_credit_pools() protects port->credit_pool updates with
credits_lock, and can be reached from XDomain lane bonding without
holding tb->lock:

CPU0                                    CPU1 (kworker)
port_credits_show()                     tb_xdomain_state_work()
  lock(&port->sw->tb->lock)               tb_xdomain_lane_bonding_enable()
  credits_show()                            tb_port_update_credits()
                                              ...
                                              usb4_port_update_credit_pools()
                                                lock(&credits_lock)
                                                usb4_port_remove_credit_pools()
    usb4_credit_pool_used(pool)
    usb4_credit_pool_total(pool)  <-- data race
                                                usb4_port_init_credit_pools()

A debugfs reader can observe transient zeroed pool totals between
usb4_port_remove_credit_pools() and usb4_port_init_credit_pools(), or
trigger a KCSAN report. Acquiring credits_lock when reading
port->credit_pool would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=15

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

* Re: [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk
  2026-11-19 17:12 ` [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk Mika Westerberg
@ 2026-11-19 17:44   ` sashiko-bot
  0 siblings, 0 replies; 37+ messages in thread
From: sashiko-bot @ 2026-11-19 17:44 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb

> From: "Mika Westerberg (Intel)" <westeri@kernel.org>
> 
> The hibernation image is taken with all tunnels up, so after restore the
> tunnels the restore kernel (or boot firmware) created may collide with
> the resources the image still holds. Instead of discovering and tearing
> those down, reset the host router on restore and re-create the tunnels
> from the image. DP tunnels are dropped and re-established from plug
> events just like we do on system suspend.
> 
> Links can take some time to retrain after reset so we wait for them to
> retrain before resuming the connected routers (and we mask the hotplug
> during that time). Because the plug events are masked we perform rescan
> in complete phase to pick up devices replaced during hibernation (or S3
> for older Apple devices).
> 
> DPRX polling is put on hold while the control channel is stopped so that
> it does not pointlessly try to access the hardware during hibernation.
> 
> Cc: Rene Sapiens <rene.sapiens@linux.intel.com>
> Cc: Pooja Katiyar <pooja.katiyar@intel.com>
> Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119171232.1616316-1-mika.westerberg@linux.intel.com?part=11


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

end of thread, other threads:[~2026-11-19 17:44 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 17:12 [PATCH 00/18] thunderbolt: Add support for shared buffering Mika Westerberg
2026-11-19 17:12 ` [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal Mika Westerberg
2026-11-19 17:32   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every " Mika Westerberg
2026-11-19 17:34   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails Mika Westerberg
2026-11-19 17:26   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held Mika Westerberg
2026-11-19 17:28   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume Mika Westerberg
2026-11-19 17:35   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running Mika Westerberg
2026-11-19 17:25   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable() Mika Westerberg
2026-11-19 17:22   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host() Mika Westerberg
2026-11-19 17:25   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 09/18] thunderbolt: Add tb_port_state_is_up() Mika Westerberg
2026-11-19 17:22   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 10/18] thunderbolt: Split host router port reset into a separate function Mika Westerberg
2026-11-19 17:22   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk Mika Westerberg
2026-11-19 17:44   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation Mika Westerberg
2026-11-19 17:26   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called Mika Westerberg
2026-11-19 17:34   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests Mika Westerberg
2026-11-19 17:26   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 15/18] thunderbolt: Rework tunnel credit allocation Mika Westerberg
2026-11-19 17:43   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 16/18] thunderbolt: Add support for shared flow control Mika Westerberg
2026-11-19 17:34   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links Mika Westerberg
2026-11-19 17:32   ` sashiko-bot
2026-11-19 17:12 ` [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering Mika Westerberg
2026-11-19 17:28   ` sashiko-bot

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