Skip to contentSkip to conversation

thunderbolt: Add support for shared buffering

PATCH In review Mika Westerberg 2 participants 39 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover thunderbolt: Add support for shared buffering

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
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/18 thunderbolt: Avoid leaking of DMA tunnel after XDomain removal

From: "Mika Westerberg (Intel)" <[email protected]>

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")
---
 drivers/thunderbolt/tb.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/thunderbolt/tb.c +12 -6
index 418ad0a61a5d..655bcc699410 100644
@@ -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;
 }
 
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH 01/18] thunderbolt: Avoid leaking of DMA tunnel after XDomain removal

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…

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) <[email protected]>

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.

Quoted 5 lines
drivers/thunderbolt/tb.c +0 -0
index 418ad0a61a5de..655bcc699410e 100644

drivers/thunderbolt/tb.c static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, · line 2345

Quoted 10 lines
drivers/thunderbolt/tb.c +7 -0
@@ -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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/18 thunderbolt: Tear down DMA tunnels on every XDomain removal

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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")
---
 drivers/thunderbolt/tb.c | 47 ++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 23 deletions(-)
drivers/thunderbolt/tb.c +24 -23
index 655bcc699410..680ed23db0bf 100644
@@ -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);
 		}
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH 02/18] thunderbolt: Tear down DMA tunnels on every XDomain removal

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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).

Quoted 4 lines

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

Fixes 7ea4cd6b2010 ("thunderbolt: Add support for XDomain connections")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH 3/18 thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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")
---
 drivers/thunderbolt/tb.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/thunderbolt/tb.c +23 -1
index 680ed23db0bf..acad8461011c 100644
@@ -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");
Signature · 1 line
2.50.1
#6 lore raw

Re: [PATCH 03/18] thunderbolt: Delay re-pairing DP IN/OUT if DPRX negotiation fails

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Quoted 5 lines

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")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

PATCH 4/18 thunderbolt: Call DP tunnel activation callback with tb->lock held

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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")
---
 drivers/thunderbolt/tb.c     | 4 ----
 drivers/thunderbolt/tunnel.c | 4 ++--
 2 files changed, 2 insertions(+), 6 deletions(-)
drivers/thunderbolt/tb.c +0 -4
index acad8461011c..3c4903adb687 100644
@@ -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,
drivers/thunderbolt/tunnel.c +2 -2
index 8145657835c5..90bc6e42d89b 100644
@@ -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.
  */
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH 04/18] thunderbolt: Call DP tunnel activation callback with tb->lock held

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Quoted 5 lines

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")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

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

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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")
---
 drivers/thunderbolt/tb.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
drivers/thunderbolt/tb.c +18 -9
index 3c4903adb687..6a11d686bdf1 100644
@@ -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)) {
 		/*
Signature · 1 line
2.50.1
#10 lore raw

Re: [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume

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() --

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) <[email protected]>

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().

Quoted 22 lines
drivers/thunderbolt/tb.c +14 -0
index 3c4903adb687d..6a11d686bdf14 100644
@@ -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");
+}

[ ... ]

drivers/thunderbolt/tb.c static int tb_resume_noirq(struct tb *tb) · line 3251

Quoted 7 lines
drivers/thunderbolt/tb.c +2 -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);
 	}

[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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#11 lore raw

Re: [PATCH 05/18] thunderbolt: Re-apply Alpine/Titan Ridge flow on resume

Hi,

Quoted 40 lines · On Thu, Nov 19, 2026 at 05:35:54PM +0000, [email protected] wrote:

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) <[email protected]>

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().

Quoted 22 lines
drivers/thunderbolt/tb.c +14 -0
index 3c4903adb687d..6a11d686bdf14 100644
@@ -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");
+}

[ ... ]

Quoted 11 lines

drivers/thunderbolt/tb.c static int tb_resume_noirq(struct tb *tb) · line 3251

Quoted 7 lines
drivers/thunderbolt/tb.c +2 -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);
 	}

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

No actually. This is only needed for suspend to disk where we reset the topology (but there is no harm calling it unconditionally in tb_resume_noirq(()). LC keeps this over RTD3.

#12 lore raw

PATCH 6/18 thunderbolt: Return -ESHUTDOWN when control channel is not running

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/ctl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/thunderbolt/ctl.c +1 -3
index 965988b18608..c8b07dd01bdf 100644
@@ -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;
Signature · 1 line
2.50.1
#13 lore raw

Re: [PATCH 06/18] thunderbolt: Return -ESHUTDOWN when control channel is not running

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#14 lore raw

PATCH 7/18 thunderbolt: Add usb4_port_hotplug_disable()

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/tb.h   |  1 +
 drivers/thunderbolt/usb4.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
drivers/thunderbolt/tb.h +1 -0
index e86845762e22..727e33992e41 100644
@@ -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);
drivers/thunderbolt/usb4.c +21 -0
index c9b3931c45ed..519aeca66d6f 100644
@@ -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
Signature · 1 line
2.50.1
#15 lore raw

Re: [PATCH 07/18] thunderbolt: Add usb4_port_hotplug_disable()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#16 lore raw

PATCH 8/18 thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host()

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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

---
 drivers/thunderbolt/switch.c | 80 ++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 40 deletions(-)
drivers/thunderbolt/switch.c +40 -40
index d22d8db8f890..c748158a7044 100644
@@ -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)
Signature · 1 line
2.50.1
#17 lore raw

Re: [PATCH 08/18] thunderbolt: Move tb_plug_events_active() before tb_switch_reset_host()

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#18 lore raw

PATCH 9/18 thunderbolt: Add tb_port_state_is_up()

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/switch.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/thunderbolt/switch.c +19 -8
index c748158a7044..26a4363f94f8 100644
@@ -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;
 
Signature · 1 line
2.50.1
#19 lore raw

Re: [PATCH 09/18] thunderbolt: Add tb_port_state_is_up()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#20 lore raw

PATCH 10/18 thunderbolt: Split host router port reset into a separate function

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/switch.c | 88 ++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 39 deletions(-)
drivers/thunderbolt/switch.c +49 -39
index 26a4363f94f8..85a2aa67cbec 100644
@@ -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;
 
Signature · 1 line
2.50.1
#21 lore raw

Re: [PATCH 10/18] thunderbolt: Split host router port reset into a separate function

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#22 lore raw

PATCH 11/18 thunderbolt: Reset topology after suspend-to-disk

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 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(-)
drivers/thunderbolt/domain.c +3 -2
index 24611f05b3cd..f9951d4311cb 100644
@@ -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;
drivers/thunderbolt/nhi.c +13 -4
index f54ab2189cf4..cfb782f3233a 100644
@@ -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,
drivers/thunderbolt/switch.c +76 -0

112 lines not on this page · raw message

drivers/thunderbolt/tb.c +35 -29

109 lines not on this page · raw message

drivers/thunderbolt/tb.h +2 -2
index 727e33992e41..7f47a5c3c0d3 100644
@@ -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);
drivers/thunderbolt/tunnel.c +26 -6
index 90bc6e42d89b..9d831a7a1ced 100644
@@ -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);
Signature · 1 line
2.50.1
#23 lore raw

Re: [PATCH 11/18] thunderbolt: Reset topology after suspend-to-disk

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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).

Quoted 6 lines

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#24 lore raw

PATCH 12/18 thunderbolt: Drop tunnel discovery without HopID allocation

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 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(-)
drivers/thunderbolt/path.c +17 -24
index 05249eed3f64..79c285dd29fb 100644
@@ -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);
drivers/thunderbolt/tb.c +7 -10
index 6c1ca4b5ffdd..10fca534ff78 100644
@@ -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)) {
drivers/thunderbolt/tb.h +1 -4
index 7f47a5c3c0d3..c31ede3244ac 100644
@@ -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);
drivers/thunderbolt/tunnel.c +10 -16
index 9d831a7a1ced..c4e624c94bb0 100644
@@ -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;
drivers/thunderbolt/tunnel.h +2 -5
index 28f49f7e9f56..35d026c5bc96 100644
@@ -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);
Signature · 1 line
2.50.1
#25 lore raw

Re: [PATCH 12/18] thunderbolt: Drop tunnel discovery without HopID allocation

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#26 lore raw

PATCH 13/18 thunderbolt: Always take domain lock when tb_tunnel_put() is called

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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().

---
 drivers/thunderbolt/tunnel.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/thunderbolt/tunnel.c +8 -8
index c4e624c94bb0..af10c67997be 100644
@@ -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);
 }
 
Signature · 1 line
2.50.1
#27 lore raw

Re: [PATCH 13/18] thunderbolt: Always take domain lock when tb_tunnel_put() is called

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Quoted 4 lines

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#28 lore raw

PATCH 14/18 thunderbolt: Assign domain to the routers in KUnit tests

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/thunderbolt/test.c +23 -0
index a1f9dbb66940..9c6da22cd100 100644
@@ -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);
Signature · 1 line
2.50.1
#29 lore raw

Re: [PATCH 14/18] thunderbolt: Assign domain to the routers in KUnit tests

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#30 lore raw

PATCH 15/18 thunderbolt: Rework tunnel credit allocation

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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).

---
 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
drivers/thunderbolt/Makefile +1 -1
index beb054c3126b..86226d5ebd7b 100644
@@ -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
drivers/thunderbolt/credits.c new file +585 -0

585 lines not on this page · raw message

drivers/thunderbolt/debugfs.c +29 -0
index 0d9f41a0f2cd..eac6d564bf18 100644
@@ -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);
drivers/thunderbolt/path.c +2 -2
index 79c285dd29fb..9b18704f8010 100644
@@ -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;
 		}
drivers/thunderbolt/switch.c +17 -5
index 216681d2bb8f..a4b96950f3e6 100644
@@ -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;
drivers/thunderbolt/tb.h +135 -10

208 lines not on this page · raw message

drivers/thunderbolt/test.c +425 -60

803 lines not on this page · raw message

drivers/thunderbolt/tunnel.c +90 -310

608 lines not on this page · raw message

drivers/thunderbolt/usb4.c +6 -1
index 519aeca66d6f..8e71ca6967b6 100644
@@ -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,
Signature · 1 line
2.50.1
#31 lore raw

Re: [PATCH 15/18] thunderbolt: Rework tunnel credit allocation

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() --

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) <[email protected]>

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.

Quoted 22 lines
drivers/thunderbolt/debugfs.c +14 -0
index 0d9f41a0f2cdc..eac6d564bf185 100644
@@ -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));
+	}
+}

drivers/thunderbolt/debugfs.c static int port_sb_regs_show(struct seq_file *s, void *not_used) · line 2514

Quoted 10 lines
drivers/thunderbolt/debugfs.c +10 -0
+
+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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#32 lore raw

Re: [PATCH 15/18] thunderbolt: Rework tunnel credit allocation

Hi,

Quoted 43 lines · On Thu, Nov 19, 2026 at 05:43:22PM +0000, [email protected] wrote:

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) <[email protected]>

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.

Quoted 28 lines
drivers/thunderbolt/debugfs.c +20 -0
index 0d9f41a0f2cdc..eac6d564bf185 100644
@@ -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)
Quoted 8 lines

drivers/thunderbolt/debugfs.c static int port_sb_regs_show(struct seq_file *s, void *not_used) · line 2514

Quoted 4 lines
drivers/thunderbolt/debugfs.c +4 -0
+		credits_show(port, s);
+
+	return 0;
+}

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

Yes it can but that's intentional. This is just for debugging so the always accurate numbers don't matter.

#33 lore raw

PATCH 16/18 thunderbolt: Add support for shared flow control

In reply to Mika Westerberg

From: Gil Fine <[email protected]>

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.

---
 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(-)
drivers/thunderbolt/credits.c +116 -16
index 84954778b916..58b9fdec19be 100644
@@ -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;
drivers/thunderbolt/debugfs.c +14 -0
index eac6d564bf18..5e9e087ae237 100644
@@ -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)
drivers/thunderbolt/path.c +45 -11

119 lines not on this page · raw message

drivers/thunderbolt/switch.c +78 -5

111 lines not on this page · raw message

drivers/thunderbolt/tb.h +24 -21

102 lines not on this page · raw message

drivers/thunderbolt/tb_regs.h +2 -0
index 92d893634d2b..ca0f18798d95 100644
@@ -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)
drivers/thunderbolt/tunnel.c +0 -10
index ffadd30bff7b..2ed3cb57d8a9 100644
@@ -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;
Signature · 1 line
2.50.1
#34 lore raw

Re: [PATCH 16/18] thunderbolt: Add support for shared flow control

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: Gil Fine <[email protected]>

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.

Quoted 6 lines

This adds support for both methods.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#35 lore raw

PATCH 17/18 thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links

In reply to Mika Westerberg

From: Gil Fine <[email protected]>

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.

---
 drivers/thunderbolt/tunnel.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
drivers/thunderbolt/tunnel.c +30 -1
index 2ed3cb57d8a9..3f2b6f8eb243 100644
@@ -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;
 	}
Signature · 1 line
2.50.1
#36 lore raw

Re: [PATCH 17/18] thunderbolt: Use restricted shared flow control for DMA tunnels on intra-domain links

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: Gil Fine <[email protected]>

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]
Quoted 6 lines

[ ... ]

This allows more efficient use of credits on the link.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#37 lore raw

PATCH 18/18 thunderbolt: Add KUnit tests for shared buffering

In reply to Mika Westerberg

From: "Mika Westerberg (Intel)" <[email protected]>

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.

---
 drivers/thunderbolt/test.c | 598 +++++++++++++++++++++++++++++++++++++
 1 file changed, 598 insertions(+)
drivers/thunderbolt/test.c +598 -0
index d6679bcc4ac3..3fa9c4661541 100644
@@ -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),
Signature · 1 line
2.50.1
#38 lore raw

Re: [PATCH 18/18] thunderbolt: Add KUnit tests for shared buffering

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

From: "Mika Westerberg (Intel)" <[email protected]>

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18