* [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling
@ 2026-11-25 15:26 Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
Hi all,
This series makes it possible to "couple" streams through host or device
router lane adapters. For example we can have topology like this:
host1 <-USB4/TB cable-> host2 <-USB4/TB cable-> host3
You can already forward stream from host1 to host3 through host2 using dd
or similar but we can also make direct tunnel through the host2 host
router. This does not consume CPU cycles of the host2. Same can be done if
there is a device router where the hosts are connected. It will take the
shortest path.
The first patches are fixes that the rest of the series needs. The usage
steps are in the last patch.
This applies on top of my "shared buffer" series here:
https://lore.kernel.org/linux-usb/20261119171232.1616316-1-mika.westerberg@linux.intel.com/
Mika Westerberg (Intel) (9):
thunderbolt: Add and remove XDomain services outside of the state work
thunderbolt: Unregister XDomains when the domain is removed
thunderbolt: stream: Keep better track of the HopIDs
thunderbolt: stream: Validate the ConfigFS group name more strictly
thunderbolt: Allow DMA tunnel also between two lane adapters
thunderbolt: Add KUnit tests for lane to lane DMA tunnels
thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths()
thunderbolt: stream: Introduce USB4STREAM coupling
Documentation: thunderbolt: Document USB4STREAM coupling
.../ABI/testing/configfs-thunderbolt_stream | 82 ++
Documentation/admin-guide/thunderbolt.rst | 119 +++
drivers/thunderbolt/domain.c | 104 +-
drivers/thunderbolt/icm.c | 49 +-
drivers/thunderbolt/stream.c | 971 +++++++++++++++++-
drivers/thunderbolt/tb.c | 150 +--
drivers/thunderbolt/tb.h | 28 +-
drivers/thunderbolt/test.c | 558 ++++++++--
drivers/thunderbolt/tunnel.c | 171 +--
drivers/thunderbolt/tunnel.h | 14 +-
drivers/thunderbolt/xdomain.c | 163 ++-
include/linux/thunderbolt.h | 14 +
12 files changed, 2108 insertions(+), 315 deletions(-)
--
2.50.1
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed Mika Westerberg
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
The XDomain services are registered and unregistered from the state
work, which means the service driver probe and remove callbacks are
called from there too. The state work is cancelled synchronously in
tb_xdomain_remove() which is called with tb->lock held, so if a service
driver calls anything that takes tb->lock, such as
tb_xdomain_disable_paths() they end up waiting for each other.
Fix this by moving the service enumeration into a separate work that is
only waited for in tb_xdomain_unregister(), which is called without
tb->lock.
Fixes: a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without holding tb->lock")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/xdomain.c | 53 ++++++++++++++++++++++++++---------
include/linux/thunderbolt.h | 5 ++++
2 files changed, 44 insertions(+), 14 deletions(-)
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index e5a962537920..55e17f4e35dc 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -1164,15 +1164,14 @@ static void __unregister_service(struct device *dev)
static int remove_missing_service(struct device *dev, void *data)
{
- struct tb_xdomain *xd = data;
+ struct tb_property_dir *dir = data;
struct tb_service *svc;
svc = tb_to_service(dev);
if (!svc)
return 0;
- if (!tb_property_find(xd->remote_properties, svc->key,
- TB_PROPERTY_TYPE_DIRECTORY))
+ if (!tb_property_find(dir, svc->key, TB_PROPERTY_TYPE_DIRECTORY))
__unregister_service(dev);
return 0;
@@ -1223,7 +1222,8 @@ static int populate_service(struct tb_service *svc,
return 0;
}
-static void enumerate_services(struct tb_xdomain *xd)
+static void enumerate_services(struct tb_xdomain *xd,
+ struct tb_property_dir *dir)
{
struct tb_service *svc;
struct tb_property *p;
@@ -1234,10 +1234,10 @@ static void enumerate_services(struct tb_xdomain *xd)
* First remove all services that are not available anymore in
* the updated property block.
*/
- device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
+ device_for_each_child_reverse(&xd->dev, dir, remove_missing_service);
/* Then re-enumerate properties creating new services as we go */
- tb_property_for_each(xd->remote_properties, p) {
+ tb_property_for_each(dir, p) {
if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
continue;
@@ -1544,7 +1544,6 @@ static int tb_xdomain_get_properties(struct tb_xdomain *xd)
{
struct tb_property_dir *dir;
struct tb *tb = xd->tb;
- bool update = false;
u32 *block = NULL;
u32 gen = 0;
int ret;
@@ -1570,7 +1569,8 @@ static int tb_xdomain_get_properties(struct tb_xdomain *xd)
mutex_lock(&xd->lock);
/* Only accept newer generation properties */
- if (xd->remote_properties && gen <= xd->remote_property_block_gen) {
+ if (xd->remote_properties && gen <= xd->remote_property_block_gen &&
+ !xd->remote_properties_stale) {
ret = 0;
goto err_free_block;
}
@@ -1589,13 +1589,12 @@ static int tb_xdomain_get_properties(struct tb_xdomain *xd)
}
/* Release the existing one */
- if (xd->remote_properties) {
+ if (xd->remote_properties)
tb_property_free_dir(xd->remote_properties);
- update = true;
- }
xd->remote_properties = dir;
xd->remote_property_block_gen = gen;
+ xd->remote_properties_stale = false;
tb_xdomain_update_link_attributes(xd);
@@ -1608,7 +1607,7 @@ static int tb_xdomain_get_properties(struct tb_xdomain *xd)
* bus and let userspace know about it. If the device is already
* registered, we notify the userspace that it has changed.
*/
- if (!update) {
+ if (!device_is_registered(&xd->dev)) {
/*
* Now disable lane 1 if bonding was not enabled. Do
* this only if bonding was possible at the beginning
@@ -1643,7 +1642,7 @@ static int tb_xdomain_get_properties(struct tb_xdomain *xd)
kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
}
- enumerate_services(xd);
+ queue_work(system_long_wq, &xd->services_work);
return 0;
err_free_dir:
@@ -1842,6 +1841,24 @@ static void tb_xdomain_state_work(struct work_struct *work)
msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
}
+static void tb_xdomain_services_work(struct work_struct *work)
+{
+ struct tb_xdomain *xd = container_of(work, typeof(*xd), services_work);
+ struct tb_property_dir *dir;
+
+ /* The state work may replace the remote properties meanwhile */
+ scoped_guard(mutex, &xd->lock) {
+ if (xd->removing)
+ return;
+ dir = tb_property_copy_dir(xd->remote_properties);
+ if (!dir)
+ return;
+ }
+
+ enumerate_services(xd, dir);
+ tb_property_free_dir(dir);
+}
+
static void tb_xdomain_properties_changed(struct work_struct *work)
{
struct tb_xdomain *xd = container_of(work, typeof(*xd),
@@ -2040,7 +2057,13 @@ static void tb_xdomain_release(struct device *dev)
static int __maybe_unused tb_xdomain_suspend(struct device *dev)
{
- stop_handshake(tb_to_xdomain(dev));
+ struct tb_xdomain *xd = tb_to_xdomain(dev);
+
+ stop_handshake(xd);
+ if (cancel_work_sync(&xd->services_work)) {
+ guard(mutex)(&xd->lock);
+ xd->remote_properties_stale = true;
+ }
return 0;
}
@@ -2151,6 +2174,7 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
INIT_DELAYED_WORK(&xd->state_work, tb_xdomain_state_work);
INIT_DELAYED_WORK(&xd->properties_changed_work,
tb_xdomain_properties_changed);
+ INIT_WORK(&xd->services_work, tb_xdomain_services_work);
atomic_set(&xd->ntunnels, 0);
xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
@@ -2265,6 +2289,7 @@ void tb_xdomain_unregister(struct tb_xdomain *xd)
{
lockdep_assert_not_held(&xd->tb->lock);
+ cancel_work_sync(&xd->services_work);
device_for_each_child_reverse(&xd->dev, xd, unregister_service);
dev_info(&xd->dev, "host disconnected\n");
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 57502da29080..48ae996a779c 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -226,6 +226,8 @@ enum tb_link_width {
* @local_property_block_len: Length of the @local_property_block in dwords
* @remote_properties: Properties exported by the remote domain
* @remote_property_block_gen: Generation of @remote_properties
+ * @remote_properties_stale: Accept the next properties even if
+ * @remote_property_block_gen has not changed
* @state: Next XDomain discovery state to run
* @state_work: Work used to run the next state
* @state_retries: Number of retries remain for the state
@@ -233,6 +235,7 @@ enum tb_link_width {
* our properties have changed
* @properties_changed_retries: Number of times left to send properties
* changed notification
+ * @services_work: Work used to add and remove the services
* @bonding_possible: True if lane bonding is possible on local side
* @target_link_width: Target link width from the remote host
* @ntunnels: Keeps track of how many tunnels go through this XDomain
@@ -275,11 +278,13 @@ struct tb_xdomain {
u32 local_property_block_len;
struct tb_property_dir *remote_properties;
u32 remote_property_block_gen;
+ bool remote_properties_stale;
int state;
struct delayed_work state_work;
int state_retries;
struct delayed_work properties_changed_work;
int properties_changed_retries;
+ struct work_struct services_work;
bool bonding_possible;
u8 target_link_width;
atomic_t ntunnels;
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs Mika Westerberg
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
tb_xdomain_remove() only stops the handshake and leaves the XDomain
registered to the bus. Unregistering happens separately without tb->lock
by tb_domain_unregister_unplugged_xdomains() but this is never called
when the domain itself is removed. This may cause adding XDomains later
on fail like:
sysfs: cannot create duplicate filename '/bus/thunderbolt/devices/0-1'
thunderbolt 0-1: failed to add XDomain device
Fix this by first unbinding the service drivers in tb_domain_remove(),
and then unregistering the XDomains once tb->lock has been released and
the workqueue has been flushed (the work items unregister the unplugged
XDomains too, so this avoids doing it twice).
Fixes: a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without holding tb->lock")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/domain.c | 34 ++++++++++++++++++++++++++++++++++
drivers/thunderbolt/xdomain.c | 2 +-
include/linux/thunderbolt.h | 3 +++
3 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index f9951d4311cb..61666e1a9b39 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -78,6 +78,9 @@ static int tb_service_probe(struct device *dev)
struct tb_service *svc = tb_to_service(dev);
struct tb_service_driver *driver;
+ if (READ_ONCE(tb_service_parent(svc)->tb->removing))
+ return -ENODEV;
+
driver = container_of(dev->driver, struct tb_service_driver, driver);
return driver->probe(svc);
@@ -491,6 +494,29 @@ int tb_domain_add(struct tb *tb, bool reset)
return ret;
}
+static int unbind_service(struct device *dev, void *data)
+{
+ if (tb_is_service(dev))
+ device_release_driver(dev);
+ return 0;
+}
+
+static int unbind_services(struct device *dev, void *data)
+{
+ struct tb_xdomain *xd;
+ const struct tb *tb = data;
+
+ xd = tb_to_xdomain(dev);
+ if (xd && xd->tb == tb) {
+ scoped_guard(mutex, &xd->lock)
+ xd->removing = true;
+ cancel_work_sync(&xd->services_work);
+ device_for_each_child_reverse(&xd->dev, NULL, unbind_service);
+ }
+
+ return 0;
+}
+
/**
* tb_domain_remove() - Removes and releases a domain
* @tb: Domain to remove
@@ -500,6 +526,13 @@ int tb_domain_add(struct tb *tb, bool reset)
*/
void tb_domain_remove(struct tb *tb)
{
+ WRITE_ONCE(tb->removing, true);
+ /*
+ * Unbind service drivers now so they can do their cleanup with
+ * the working control channel.
+ */
+ bus_for_each_dev(&tb_bus_type, NULL, tb, unbind_services);
+
mutex_lock(&tb->lock);
if (tb->cm_ops->stop)
tb->cm_ops->stop(tb);
@@ -508,6 +541,7 @@ void tb_domain_remove(struct tb *tb)
mutex_unlock(&tb->lock);
flush_workqueue(tb->wq);
+ tb_domain_unregister_unplugged_xdomains(tb);
if (tb->cm_ops->deinit)
tb->cm_ops->deinit(tb);
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index 55e17f4e35dc..fceaa177b3c1 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -1848,7 +1848,7 @@ static void tb_xdomain_services_work(struct work_struct *work)
/* The state work may replace the remote properties meanwhile */
scoped_guard(mutex, &xd->lock) {
- if (xd->removing)
+ if (READ_ONCE(xd->tb->removing) || xd->removing)
return;
dir = tb_property_copy_dir(xd->remote_properties);
if (!dir)
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 48ae996a779c..03f315934e44 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -78,6 +78,8 @@ enum tb_security_level {
* @index: Linux assigned domain number
* @security_level: Current security level
* @nboot_acl: Number of boot ACLs the domain supports
+ * @removing: Domain is about to be removed, no new XDomain services are
+ * allowed to be registered
* @privdata: Private connection manager specific data
*/
struct tb {
@@ -91,6 +93,7 @@ struct tb {
int index;
enum tb_security_level security_level;
size_t nboot_acl;
+ bool removing;
unsigned long privdata[];
};
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
2026-11-25 15:26 ` [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly Mika Westerberg
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
Currently if a HopID is already taken and we try to allocate that with
previous HopID already set like this (HopID 10 is already allocated)
# echo 9 > in_hopid
# echo 10 > in_hopid
In the second echo we release 9 but since allocation of 10 fails we
return -EBUSY and leave 9 as ->in_hopid. If at this point the stream is
removed with rmdir we release 9 again which results WARN() from
ida_free().
Make this work better so that we keep track of what we actually have
allocated and keep the configured value as separate.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/stream.c | 77 +++++++++++++++++++++++++-----------
1 file changed, 53 insertions(+), 24 deletions(-)
diff --git a/drivers/thunderbolt/stream.c b/drivers/thunderbolt/stream.c
index c9e1987a6924..2b73f585ccff 100644
--- a/drivers/thunderbolt/stream.c
+++ b/drivers/thunderbolt/stream.c
@@ -128,6 +128,8 @@ struct tbstream_ring {
* @index: Unique identifier for the character device
* @in_hopid: In HopID
* @out_hopid: Out HopID
+ * @in_hopid_alloc: Allocated @in_hopid, %0 if not allocated
+ * @out_hopid_alloc: Allocated @out_hopid, %0 if not allocated
* @ring_size: Size of the rings
* @throttling: Interrupt throttling rate in ns
* @busy_poll: Instead of interrupts, busy poll the rings
@@ -145,6 +147,11 @@ struct tbstream_ring {
* @close_received is used on write side to notify the writer that the
* other side sent CLOSE. @closed on the other hand is used on the read
* side to make read(2) return EOF to the caller.
+ *
+ * @in_hopid and @out_hopid are the values configured by the user and
+ * stay (except %-1 which will be replaced by an automatically allocated
+ * HopID). @in_hopid_alloc and @out_hopid_alloc are the currently
+ * allocated values based on those.
*/
struct tbstream_dev {
struct config_group group;
@@ -154,6 +161,8 @@ struct tbstream_dev {
int index;
int in_hopid;
int out_hopid;
+ int in_hopid_alloc;
+ int out_hopid_alloc;
unsigned int ring_size;
unsigned int throttling;
bool busy_poll;
@@ -308,10 +317,10 @@ static void tbstream_dev_release(struct kref *kref)
if (sdev->stream) {
struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
- if (sdev->out_hopid > 0)
- tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
- if (sdev->in_hopid > 0)
- tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+ if (sdev->out_hopid_alloc)
+ tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+ if (sdev->in_hopid_alloc)
+ tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
tbstream_put(sdev->stream);
}
@@ -344,7 +353,7 @@ static inline int tbstream_dev_valid(const struct tbstream_dev *sdev)
if (!tbstream_valid(stream))
return -ENXIO;
- if (sdev->in_hopid <= 0 || sdev->out_hopid <= 0)
+ if (!sdev->in_hopid_alloc || !sdev->out_hopid_alloc)
return -EINVAL;
return 0;
}
@@ -660,10 +669,10 @@ static int tbstream_dev_start(struct tbstream_dev *sdev)
}
sdev->rx_ring.ring = ring;
- ret = tb_xdomain_enable_paths(xd, sdev->out_hopid,
- sdev->tx_ring.ring->hop,
- sdev->in_hopid,
- sdev->rx_ring.ring->hop);
+ ret = tb_xdomain_enable_paths(xd, sdev->out_hopid_alloc,
+ sdev->tx_ring.ring->hop,
+ sdev->in_hopid_alloc,
+ sdev->rx_ring.ring->hop);
if (ret)
goto err_free_rx;
@@ -736,9 +745,9 @@ static void tbstream_dev_stop(struct tbstream_dev *sdev)
xd = tbstream_dev_xdomain(sdev);
if (xd) {
- tb_xdomain_disable_paths(xd, sdev->out_hopid,
+ tb_xdomain_disable_paths(xd, sdev->out_hopid_alloc,
sdev->tx_ring.ring->hop,
- sdev->in_hopid,
+ sdev->in_hopid_alloc,
sdev->rx_ring.ring->hop);
}
@@ -1331,7 +1340,8 @@ static int tbstream_dev_update_properties(struct tbstream_dev *sdev)
ret = service_update_properties(stream->svc,
config_item_name(&sdev->group.cg_item),
- sdev->in_hopid, sdev->out_hopid);
+ sdev->in_hopid_alloc,
+ sdev->out_hopid_alloc);
if (!ret)
tb_service_properties_changed(stream->svc);
@@ -1344,9 +1354,15 @@ static int tbstream_dev_alloc_in_hopid(struct tbstream_dev *sdev, int hopid)
struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
int ret;
- if (sdev->in_hopid > 0 && sdev->in_hopid != hopid)
- tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+ /* Already allocated */
+ if (hopid > 0 && hopid == sdev->in_hopid_alloc)
+ return 0;
+ if (hopid < 0 && sdev->in_hopid_alloc)
+ return 0;
if (!hopid) {
+ if (sdev->in_hopid_alloc)
+ tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+ sdev->in_hopid_alloc = 0;
sdev->in_hopid = hopid;
return 0;
}
@@ -1361,7 +1377,9 @@ static int tbstream_dev_alloc_in_hopid(struct tbstream_dev *sdev, int hopid)
tb_xdomain_release_in_hopid(xd, ret);
return -EBUSY;
}
- sdev->in_hopid = ret;
+ if (sdev->in_hopid_alloc)
+ tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+ sdev->in_hopid_alloc = sdev->in_hopid = ret;
return 0;
}
@@ -1370,9 +1388,14 @@ static int tbstream_dev_alloc_out_hopid(struct tbstream_dev *sdev, int hopid)
struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
int ret;
- if (sdev->out_hopid > 0 && sdev->out_hopid != hopid)
- tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
+ if (hopid > 0 && hopid == sdev->out_hopid_alloc)
+ return 0;
+ if (hopid < 0 && sdev->out_hopid_alloc)
+ return 0;
if (!hopid) {
+ if (sdev->out_hopid_alloc)
+ tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+ sdev->out_hopid_alloc = 0;
sdev->out_hopid = hopid;
return 0;
}
@@ -1383,7 +1406,9 @@ static int tbstream_dev_alloc_out_hopid(struct tbstream_dev *sdev, int hopid)
tb_xdomain_release_out_hopid(xd, ret);
return -EBUSY;
}
- sdev->out_hopid = ret;
+ if (sdev->out_hopid_alloc)
+ tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+ sdev->out_hopid_alloc = sdev->out_hopid = ret;
return 0;
}
@@ -1597,8 +1622,8 @@ tbstream_dev_attach_stream(struct tbstream_dev *sdev, struct tbstream_group *sg)
tbstream_dev_alloc_out_hopid(sdev, sdev->out_hopid);
}
- service_update_properties(stream->svc, name, sdev->in_hopid,
- sdev->out_hopid);
+ service_update_properties(stream->svc, name, sdev->in_hopid_alloc,
+ sdev->out_hopid_alloc);
tb_service_properties_changed(stream->svc);
/* Notify any openerers that the stream is now attached */
@@ -1617,10 +1642,14 @@ static void tbstream_dev_detach_stream(struct tbstream_dev *sdev)
return;
sdev->stream = NULL;
xd = tb_service_parent(stream->svc);
- if (sdev->out_hopid > 0)
- tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
- if (sdev->in_hopid > 0)
- tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+ if (sdev->out_hopid_alloc) {
+ tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+ sdev->out_hopid_alloc = 0;
+ }
+ if (sdev->in_hopid_alloc) {
+ tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+ sdev->in_hopid_alloc = 0;
+ }
}
service_update_properties(stream->svc, name, 0, 0);
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (2 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters Mika Westerberg
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
The ConfigFS group names must match the Thunderbolt service names so
passing something like "0-1.0a" will not match any service but it is
accepted at the moment. Make this more strict. We are going to need
this also later in the series when we introduce USB4STREAM coupling.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/stream.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/drivers/thunderbolt/stream.c b/drivers/thunderbolt/stream.c
index 2b73f585ccff..2661f563b99c 100644
--- a/drivers/thunderbolt/stream.c
+++ b/drivers/thunderbolt/stream.c
@@ -1763,16 +1763,24 @@ static const struct config_item_type tbstream_dev_group_type = {
.ct_item_ops = &tbstream_item_ops,
};
+static bool tbstream_valid_service_name(const char *name)
+{
+ unsigned int domain, index;
+ int len = 0;
+ u64 route;
+
+ if (sscanf(name, "%u-%llx.%u%n", &domain, &route, &index, &len) != 3)
+ return false;
+ return len == strlen(name);
+}
+
static struct config_group *
tbstream_make_group(struct config_group *group, const char *name)
{
struct tbstream_group *sg;
struct tbstream *stream;
- int domain, index;
- u64 route;
- /* Make sure the format is correct */
- if (sscanf(name, "%u-%llx.%u", &domain, &route, &index) != 3)
+ if (!tbstream_valid_service_name(name))
return ERR_PTR(-EINVAL);
sg = kzalloc_obj(*sg);
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (3 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels Mika Westerberg
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
At the moment we only support DMA tunnels between two hosts so that it
ends up or starts from the host interface adapter. This is the typical
use-case. However, it is entirely possible to make a DMA tunnel from one
lane adapter to another so acting as "passthrough" instead of ending or
starting the traffic.
This modifies the existing DMA tunneling functions to support lane
adapter to lane adapter tunnels as well.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/tb.c | 11 +--
drivers/thunderbolt/test.c | 150 ++++++++++++++++--------------
drivers/thunderbolt/tunnel.c | 171 +++++++++++++++++++++--------------
drivers/thunderbolt/tunnel.h | 14 +--
4 files changed, 194 insertions(+), 152 deletions(-)
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 10fca534ff78..c7a009a62411 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -2393,8 +2393,8 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
*/
tb_disable_clx(sw);
- tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
- transmit_ring, receive_path, receive_ring);
+ tunnel = tb_tunnel_alloc_dma(tb, nhi_port, transmit_ring, receive_ring,
+ dst_port, receive_path, transmit_path);
if (!tunnel) {
ret = -ENOMEM;
goto err_clx;
@@ -2437,11 +2437,10 @@ static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
if (!tb_tunnel_is_dma(tunnel))
continue;
- if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
- continue;
- if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
- receive_path, receive_ring))
+ if (tb_tunnel_match_dma(tunnel, nhi_port, transmit_ring,
+ receive_ring, dst_port, receive_path,
+ transmit_path))
tb_deactivate_and_free_tunnel(tunnel);
}
diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index 3fa9c4661541..0321408fe91c 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -1924,7 +1924,7 @@ static void tb_test_tunnel_dma(struct kunit *test)
nhi = &host->ports[7];
port = &host->ports[1];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -1967,7 +1967,7 @@ static void tb_test_tunnel_dma_rx(struct kunit *test)
nhi = &host->ports[7];
port = &host->ports[1];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, -1, -1, 15, 2);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, -1, 2, port, 15, -1);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2006,7 +2006,7 @@ static void tb_test_tunnel_dma_tx(struct kunit *test)
nhi = &host->ports[7];
port = &host->ports[1];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 2, -1, -1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 2, -1, port, -1, 15);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2054,7 +2054,7 @@ static void tb_test_tunnel_dma_chain(struct kunit *test)
nhi = &host->ports[7];
port = &dev2->ports[3];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, nhi);
@@ -2103,45 +2103,55 @@ static void tb_test_tunnel_dma_match(struct kunit *test)
nhi = &host->ports[7];
port = &host->ports[1];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 1, 15, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 15, 15);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, 15, 1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 8, 1, 15, 1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, -1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, 1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, 1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 8, -1, 8, -1));
+#define MATCH(src, src_in, src_out, dst, dst_in, dst_out) \
+ tb_tunnel_match_dma(tunnel, src, src_in, src_out, dst, dst_in, dst_out)
+
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, 1, port, 15, 15));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, 1, 1, port, 15, 8));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 1, port, 15, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, 15));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, 15));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, 15, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 1, port, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(NULL, -1, -1, NULL, -1, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, -1, port, 8, 8));
+ /* The two ends can be given in either order */
+ KUNIT_ASSERT_TRUE(test, MATCH(port, 15, 15, nhi, 1, 1));
+ /* The tunnel does not end to this port */
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, -1, &host->ports[3], -1, -1));
tb_tunnel_put(tunnel);
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 15, 1, -1, -1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, -1, port, -1, 15);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, 1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, 15, -1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, 1, -1, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 1, 15, 1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, 15));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, 15));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, 1, -1, port, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, 1, 1, port, 15, 15));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 1, port, 15, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, 11, -1, port, -1, 15));
tb_tunnel_put(tunnel);
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, -1, -1, 15, 11);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, -1, 11, port, 15, -1);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 11));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, -1));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, 11));
- KUNIT_ASSERT_TRUE(test, tb_tunnel_match_dma(tunnel, -1, -1, -1, -1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 15, 1));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, -1, -1, 10, 11));
- KUNIT_ASSERT_FALSE(test, tb_tunnel_match_dma(tunnel, 15, 11, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 11, port, 15, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, 15, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, 11, port, -1, -1));
+ KUNIT_ASSERT_TRUE(test, MATCH(nhi, -1, -1, port, -1, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 1, port, 15, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, -1, 11, port, 10, -1));
+ KUNIT_ASSERT_FALSE(test, MATCH(nhi, 11, -1, port, -1, 15));
tb_tunnel_put(tunnel);
+
+#undef MATCH
}
static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test)
@@ -2642,7 +2652,7 @@ static void tb_test_credit_alloc_dma(struct kunit *test)
nhi = &host->ports[7];
port = &dev->ports[3];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
@@ -2712,7 +2722,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
* Tunnel 6 fails initially then we release tunnel 1 and then
* tunnel 6 gets full 14 for both sides.
*/
- tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel1);
KUNIT_ASSERT_EQ(test, tunnel1->npaths, (size_t)2);
@@ -2732,7 +2742,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
- tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
KUNIT_ASSERT_NOT_NULL(test, tunnel2);
KUNIT_ASSERT_EQ(test, tunnel2->npaths, (size_t)2);
@@ -2752,7 +2762,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
- tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
KUNIT_ASSERT_NOT_NULL(test, tunnel3);
KUNIT_ASSERT_EQ(test, tunnel3->npaths, (size_t)2);
@@ -2772,7 +2782,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
KUNIT_ASSERT_NOT_NULL(test, tunnel4);
KUNIT_ASSERT_EQ(test, tunnel4->npaths, (size_t)2);
@@ -2792,7 +2802,7 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 10U);
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
KUNIT_ASSERT_NOT_NULL(test, tunnel5);
KUNIT_ASSERT_EQ(test, tunnel5->npaths, (size_t)2);
@@ -2812,12 +2822,12 @@ static void tb_test_credit_alloc_dma_multiple(struct kunit *test)
KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 1U);
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
KUNIT_ASSERT_NULL(test, tunnel6);
tb_tunnel_put(tunnel1);
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
KUNIT_ASSERT_NOT_NULL(test, tunnel6);
KUNIT_ASSERT_EQ(test, tunnel6->npaths, (size_t)2);
@@ -2933,7 +2943,7 @@ static void tb_test_credit_alloc_dma_shared(struct kunit *test)
* (port 3) is not part of the link so it uses dedicated
* buffers.
*/
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
tb_test_check_dma_tunnel(test, tunnel, 14, 14, 14, true);
tb_test_check_shared(test, &host->ports[1], 14, 1);
@@ -2974,15 +2984,15 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
* dev ingress port (3) is still using dedicated for the
* inter-domain link (and runs out of credits the same way).
*/
- tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, true);
- tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, true);
- tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, true);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 14, true);
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 14, true);
tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -2993,7 +3003,7 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
* shared buffers are allocated. Releasing the failed tunnel
* must not drop the users of the shared buffers.
*/
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
KUNIT_ASSERT_NULL(test, tunnel6);
tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -3004,7 +3014,7 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
tb_test_check_shared(test, &host->ports[1], 14, 4);
tb_test_check_shared(test, &dev->ports[1], 14, 4);
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
tb_test_check_dma_tunnel(test, tunnel6, 14, 14, 14, true);
tb_test_check_shared(test, &host->ports[1], 14, 5);
@@ -3027,17 +3037,17 @@ static void tb_test_credit_alloc_dma_multiple_shared(struct kunit *test)
tb_switch_disable_shared_buffering(host);
tb_switch_disable_shared_buffering(dev);
- tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
- tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
- tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, port, 13, 6, 13, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, port, 13, 13);
KUNIT_ASSERT_NULL(test, tunnel6);
tb_tunnel_put(tunnel5);
@@ -3071,11 +3081,11 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
* with dedicated buffers. This leaves 52 - 3 * 14 = 10 credits
* there.
*/
- tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
- tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
- tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
/*
@@ -3086,14 +3096,14 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
tb_switch_enable_shared_buffering(host);
tb_switch_enable_shared_buffering(dev);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, true);
tb_test_check_shared(test, &host->ports[1], 14, 1);
tb_test_check_shared(test, &dev->ports[1], 10, 1);
/* This one joins the existing shared buffers */
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 10, true);
tb_test_check_shared(test, &host->ports[1], 14, 2);
@@ -3117,9 +3127,9 @@ static void tb_test_credit_alloc_dma_shared_partial(struct kunit *test)
tb_switch_disable_shared_buffering(host);
tb_switch_disable_shared_buffering(dev);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, false);
tb_tunnel_put(tunnel5);
@@ -3153,13 +3163,13 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
common = &up->credit_pool[USB4_CREDIT_POOL_COMMON];
dma = &up->credit_pool[USB4_CREDIT_POOL_DMA];
- tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
tb_test_check_dma_tunnel(test, tunnel1, 14, 14, 14, false);
- tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
tb_test_check_dma_tunnel(test, tunnel2, 14, 14, 14, false);
- tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, port, 10, 3, 10, 3);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, nhi, 3, 3, port, 10, 10);
tb_test_check_dma_tunnel(test, tunnel3, 14, 14, 14, false);
- tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, port, 11, 4, 11, 4);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, nhi, 4, 4, port, 11, 11);
tb_test_check_dma_tunnel(test, tunnel4, 4, 14, 10, false);
KUNIT_EXPECT_EQ(test, usb4_credit_pool_available(common), 0U);
@@ -3174,7 +3184,7 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
* the shared buffers get the minimum from the DMA pool, same as
* the dedicated buffers would.
*/
- tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, port, 12, 5, 12, 5);
+ tunnel5 = tb_tunnel_alloc_dma(NULL, nhi, 5, 5, port, 12, 12);
tb_test_check_dma_tunnel(test, tunnel5, 1, 14, 1, true);
tb_test_check_shared(test, up, 1, 1);
KUNIT_EXPECT_PTR_EQ(test, up->shared_pool, dma);
@@ -3182,7 +3192,7 @@ static void tb_test_credit_alloc_dma_shared_reserved(struct kunit *test)
KUNIT_EXPECT_EQ(test, usb4_credit_pool_used(dma), 1U);
/* Port 3 has run out so this one goes through port 5 instead */
- tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, &dev->ports[5], 8, 6, 8, 6);
+ tunnel6 = tb_tunnel_alloc_dma(NULL, nhi, 6, 6, &dev->ports[5], 8, 8);
tb_test_check_dma_tunnel(test, tunnel6, 14, 14, 1, true);
tb_test_check_shared(test, up, 1, 2);
KUNIT_EXPECT_PTR_EQ(test, tunnel6->paths[1]->hops[1].credit_pool, dma);
@@ -3237,7 +3247,7 @@ static void tb_test_credit_alloc_dma_shared_mixed(struct kunit *test)
nhi = &host->ports[7];
port = &dev2->ports[3];
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
@@ -3297,7 +3307,7 @@ static void tb_test_credit_alloc_dma_shared_no_credit_allocation(struct kunit *t
* host ingress (RX path) can use shared buffers. The device
* ingress uses the fixed dedicated credits.
*/
- tunnel = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ tunnel = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, tunnel);
KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
@@ -3517,7 +3527,7 @@ static struct tb_tunnel *TB_TEST_DMA_TUNNEL1(struct kunit *test,
nhi = &host->ports[7];
port = &dev->ports[3];
- dma_tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, port, 8, 1, 8, 1);
+ dma_tunnel1 = tb_tunnel_alloc_dma(NULL, nhi, 1, 1, port, 8, 8);
KUNIT_ASSERT_NOT_NULL(test, dma_tunnel1);
KUNIT_ASSERT_EQ(test, dma_tunnel1->npaths, (size_t)2);
@@ -3547,7 +3557,7 @@ static struct tb_tunnel *TB_TEST_DMA_TUNNEL2(struct kunit *test,
nhi = &host->ports[7];
port = &dev->ports[3];
- dma_tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, port, 9, 2, 9, 2);
+ dma_tunnel2 = tb_tunnel_alloc_dma(NULL, nhi, 2, 2, port, 9, 9);
KUNIT_ASSERT_NOT_NULL(test, dma_tunnel2);
KUNIT_ASSERT_EQ(test, dma_tunnel2->npaths, (size_t)2);
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 3f2b6f8eb243..b36fed0b52d4 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -1733,36 +1733,54 @@ static int tb_dma_init_path(struct tb_path *path)
return 0;
}
+static const char *
+tb_dma_path_name(const struct tb_port *src, const struct tb_port *dst)
+{
+ if (tb_port_is_nhi(src))
+ return "DMA TX";
+ if (tb_port_is_nhi(dst))
+ return "DMA RX";
+ return "DMA";
+}
+
/**
- * tb_tunnel_alloc_dma() - allocate a DMA tunnel
+ * tb_tunnel_alloc_dma() - Allocate a DMA tunnel
* @tb: Pointer to the domain structure
- * @nhi: Host controller port
- * @dst: Destination null port which the other domain is connected to
- * @transmit_path: HopID used for transmitting packets
- * @transmit_ring: NHI ring number used to send packets towards the
- * other domain. Set to %-1 if TX path is not needed.
- * @receive_path: HopID used for receiving packets
- * @receive_ring: NHI ring number used to receive packets from the
- * other domain. Set to %-1 if RX path is not needed.
+ * @src: First end of the tunnel
+ * @src_in_hopid: HopID the packets flowing towards @dst enter @src with. Set
+ * to %-1 if the path from @src to @dst is not needed.
+ * @src_out_hopid: HopID the packets flowing towards @src leave with. Set to
+ * %-1 if the path from @dst to @src is not needed.
+ * @dst: Second end of the tunnel
+ * @dst_in_hopid: HopID the packets flowing towards @src enter @dst with. Set
+ * to %-1 if the path from @dst to @src is not needed.
+ * @dst_out_hopid: HopID the packets flowing towards @dst leave with. Set to
+ * %-1 if the path from @src to @dst is not needed.
+ *
+ * Allocates a DMA tunnel between host interface and lane adapter, lane
+ * adapter and host interface or lane adapter and lane adapter.
*
* Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
*/
-struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
- struct tb_port *dst, int transmit_path,
- int transmit_ring, int receive_path,
- int receive_ring)
+struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *src,
+ int src_in_hopid, int src_out_hopid,
+ struct tb_port *dst, int dst_in_hopid,
+ int dst_out_hopid)
{
+ bool src_to_dst, dst_to_src;
struct tb_tunnel *tunnel;
size_t npaths = 0, i = 0;
struct tb_path *path;
- /* Ring 0 is reserved for control channel */
- if (WARN_ON(!receive_ring || !transmit_ring))
+ if (WARN_ON(src == dst))
return NULL;
- if (receive_ring > 0)
+ src_to_dst = src_in_hopid > 0 && dst_out_hopid > 0;
+ dst_to_src = dst_in_hopid > 0 && src_out_hopid > 0;
+
+ if (src_to_dst)
npaths++;
- if (transmit_ring > 0)
+ if (dst_to_src)
npaths++;
if (WARN_ON(!npaths))
@@ -1772,29 +1790,31 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
if (!tunnel)
return NULL;
- tunnel->src_port = nhi;
+ tunnel->src_port = src;
tunnel->dst_port = dst;
- if (receive_ring > 0) {
- path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
- "DMA RX");
+ if (dst_to_src) {
+ path = tb_path_alloc(tb, dst, dst_in_hopid, src, src_out_hopid,
+ 0, tb_dma_path_name(dst, src));
if (!path)
goto err_free;
tunnel->paths[i++] = path;
if (tb_dma_init_path(path)) {
- tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
+ tb_tunnel_dbg(tunnel, "not enough buffers for %s path\n",
+ path->name);
goto err_free;
}
}
- if (transmit_ring > 0) {
- path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
- "DMA TX");
+ if (src_to_dst) {
+ path = tb_path_alloc(tb, src, src_in_hopid, dst, dst_out_hopid,
+ 0, tb_dma_path_name(src, dst));
if (!path)
goto err_free;
tunnel->paths[i++] = path;
if (tb_dma_init_path(path)) {
- tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
+ tb_tunnel_dbg(tunnel, "not enough buffers for %s path\n",
+ path->name);
goto err_free;
}
}
@@ -1806,67 +1826,78 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
return NULL;
}
-/**
- * tb_tunnel_match_dma() - Match DMA tunnel
- * @tunnel: Tunnel to match
- * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
- * @transmit_ring: NHI ring number used to send packets towards the
- * other domain. Pass %-1 to ignore.
- * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
- * @receive_ring: NHI ring number used to receive packets from the
- * other domain. Pass %-1 to ignore.
- *
- * This function can be used to match specific DMA tunnel, if there are
- * multiple DMA tunnels going through the same XDomain connection.
- *
- * Return: %true if there is a match, %false otherwise.
- */
-bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
- int transmit_ring, int receive_path, int receive_ring)
+static void tb_dma_end_hopids(const struct tb_tunnel *tunnel,
+ const struct tb_port *port,
+ int *in_hopid, int *out_hopid)
{
- const struct tb_path *tx_path = NULL, *rx_path = NULL;
int i;
- if (!receive_ring || !transmit_ring)
- return false;
+ *in_hopid = *out_hopid = -1;
for (i = 0; i < tunnel->npaths; i++) {
const struct tb_path *path = tunnel->paths[i];
+ const struct tb_path_hop *first, *last;
if (!path)
continue;
- if (tb_port_is_nhi(path->hops[0].in_port))
- tx_path = path;
- else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
- rx_path = path;
- }
+ first = &path->hops[0];
+ last = &path->hops[path->path_length - 1];
- if (transmit_ring > 0 || transmit_path > 0) {
- if (!tx_path)
- return false;
- if (transmit_ring > 0 &&
- (tx_path->hops[0].in_hop_index != transmit_ring))
- return false;
- if (transmit_path > 0 &&
- (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
- return false;
+ if (first->in_port == port)
+ *in_hopid = first->in_hop_index;
+ else if (last->out_port == port)
+ *out_hopid = last->next_hop_index;
}
+}
- if (receive_ring > 0 || receive_path > 0) {
- if (!rx_path)
- return false;
- if (receive_path > 0 &&
- (rx_path->hops[0].in_hop_index != receive_path))
- return false;
- if (receive_ring > 0 &&
- (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
- return false;
- }
+static bool tb_dma_match_end(const struct tb_tunnel *tunnel,
+ const struct tb_port *port, int in_hopid,
+ int out_hopid)
+{
+ int tunnel_in_hopid, tunnel_out_hopid;
+
+ if (!port)
+ return true;
+
+ tb_dma_end_hopids(tunnel, port, &tunnel_in_hopid, &tunnel_out_hopid);
+
+ /* The tunnel must end to the adapter in the first place */
+ if (tunnel_in_hopid < 0 && tunnel_out_hopid < 0)
+ return false;
+
+ if (in_hopid > 0 && in_hopid != tunnel_in_hopid)
+ return false;
+ if (out_hopid > 0 && out_hopid != tunnel_out_hopid)
+ return false;
return true;
}
+/**
+ * tb_tunnel_match_dma() - Match DMA tunnel
+ * @tunnel: Tunnel to match
+ * @src: First end of the tunnel. Pass %NULL to ignore.
+ * @src_in_hopid: HopID the packets entering @src use. Pass %-1 to ignore.
+ * @src_out_hopid: HopID the packets leaving @src use. Pass %-1 to ignore.
+ * @dst: Second end of the tunnel. Pass %NULL to ignore.
+ * @dst_in_hopid: HopID the packets entering @dst use. Pass %-1 to ignore.
+ * @dst_out_hopid: HopID the packets leaving @dst use. Pass %-1 to ignore.
+ *
+ * This function can be used to match specific DMA tunnel, if there are
+ * multiple DMA tunnels going through the same XDomain connection.
+ *
+ * Return: %true if there is a match, %false otherwise.
+ */
+bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel,
+ const struct tb_port *src, int src_in_hopid,
+ int src_out_hopid, const struct tb_port *dst,
+ int dst_in_hopid, int dst_out_hopid)
+{
+ return tb_dma_match_end(tunnel, src, src_in_hopid, src_out_hopid) &&
+ tb_dma_match_end(tunnel, dst, dst_in_hopid, dst_out_hopid);
+}
+
static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
{
int ret, up_max_rate, down_max_rate;
diff --git a/drivers/thunderbolt/tunnel.h b/drivers/thunderbolt/tunnel.h
index 35d026c5bc96..5dedf742b14d 100644
--- a/drivers/thunderbolt/tunnel.h
+++ b/drivers/thunderbolt/tunnel.h
@@ -119,12 +119,14 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
struct tb_port *out, int link_nr,
int max_up, int max_down,
void (*callback)(struct tb_tunnel *));
-struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
- struct tb_port *dst, int transmit_path,
- int transmit_ring, int receive_path,
- int receive_ring);
-bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
- int transmit_ring, int receive_path, int receive_ring);
+struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *src,
+ int src_in_hopid, int src_out_hopid,
+ struct tb_port *dst, int dst_in_hopid,
+ int dst_out_hopid);
+bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel,
+ const struct tb_port *src, int src_in_hopid,
+ int src_out_hopid, const struct tb_port *dst,
+ int dst_in_hopid, int dst_out_hopid);
struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down);
struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
struct tb_port *down, int max_up,
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (4 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths() Mika Westerberg
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
This adds KUnit tests for the lane to lane DMA tunnels we just started
to support.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/test.c | 408 +++++++++++++++++++++++++++++++++++++
1 file changed, 408 insertions(+)
diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c
index 0321408fe91c..b36bbe8bd7c1 100644
--- a/drivers/thunderbolt/test.c
+++ b/drivers/thunderbolt/test.c
@@ -2154,6 +2154,239 @@ static void tb_test_tunnel_dma_match(struct kunit *test)
#undef MATCH
}
+static void tb_test_tunnel_dma_lane2lane(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port;
+ struct tb_tunnel *tunnel;
+ struct tb_switch *host;
+
+ /*
+ * Create DMA tunnel between two hosts that are connected to lane
+ * adapters 1 and 3 of this host.
+ *
+ * [Host 2] [Host 3]
+ * ............ Domain border ............
+ * | |
+ * 1 v In HopID 8 -> Out HopID 8 |
+ * 1 ^ Out HopID 8 <- In HopID 8 v 3
+ * [Host 1]
+ */
+ host = alloc_host(test);
+ src_port = &host->ports[1];
+ dst_port = &host->ports[3];
+
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, 8, dst_port, 8, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_EXPECT_EQ(test, tunnel->type, TB_TUNNEL_DMA);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, src_port);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->dst_port, dst_port);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, 2);
+ /* Path from the second host to the first one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 8);
+ KUNIT_EXPECT_TRUE(test, tunnel->paths[0]->hops[0].ingress_fc_enable);
+ KUNIT_EXPECT_TRUE(test, tunnel->paths[0]->hops[0].egress_fc_enable);
+ /* Path from the first host to the second one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].out_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].next_hop_index, 8);
+ KUNIT_EXPECT_TRUE(test, tunnel->paths[1]->hops[0].ingress_fc_enable);
+ KUNIT_EXPECT_TRUE(test, tunnel->paths[1]->hops[0].egress_fc_enable);
+
+ tb_tunnel_put(tunnel);
+}
+
+static void tb_test_tunnel_dma_lane2lane_one_way(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port;
+ struct tb_tunnel *tunnel;
+ struct tb_switch *host;
+
+ /*
+ * Create uni-directional DMA tunnels between two hosts that are
+ * connected to lane adapters 1 and 3 of this host.
+ *
+ * [Host 2] [Host 3]
+ * ............ Domain border ............
+ * | |
+ * 1 v In HopID 8 -> Out HopID 8 v 3
+ * [Host 1]
+ */
+ host = alloc_host(test);
+ src_port = &host->ports[1];
+ dst_port = &host->ports[3];
+
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, -1, dst_port, -1, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, 1);
+ KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 8);
+
+ tb_tunnel_put(tunnel);
+
+ /* Now the other way around */
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, -1, 8, dst_port, 8, -1);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, 1);
+ KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 8);
+
+ tb_tunnel_put(tunnel);
+}
+
+static void tb_test_tunnel_dma_lane2lane_chain(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port;
+ struct tb_switch *host, *dev;
+ struct tb_tunnel *tunnel;
+
+ /*
+ * Create DMA tunnel between a host connected to lane adapter 1 of
+ * this host and another one that is behind a device router.
+ *
+ * [Host 2]
+ * ............ Domain border
+ * |
+ * 1 | In and Out HopID 8
+ * [Host 1]
+ * 3 \
+ * 1 \
+ * [Device #1]
+ * 3 | In and Out HopID 8
+ * v
+ * ............ Domain border
+ * |
+ * [Host 3]
+ */
+ host = alloc_host(test);
+ dev = alloc_dev_default(test, host, 0x3, true);
+
+ src_port = &host->ports[1];
+ dst_port = &dev->ports[3];
+
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, 8, dst_port, 8, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, 2);
+ /* Path from the second host to the first one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 2);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port,
+ &dev->ports[1]);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[1].in_port,
+ &host->ports[3]);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[1].out_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[1].next_hop_index, 8);
+ /* Path from the first host to the second one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 2);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].out_port,
+ &host->ports[3]);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[1].in_port,
+ &dev->ports[1]);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[1].out_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[1].next_hop_index, 8);
+
+ tb_tunnel_put(tunnel);
+}
+
+static void tb_test_tunnel_dma_lane2lane_hub(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port;
+ struct tb_switch *host, *hub;
+ struct tb_tunnel *tunnel;
+
+ /*
+ * Create DMA tunnel between two hosts that are both connected to a
+ * hub below this host. Now the path goes directly from one downstream
+ * port of the hub to the other one and does not reach this host at
+ * all.
+ *
+ * [Host 1]
+ * 1 |
+ * 1 |
+ * [Hub #1]
+ * 3 v In HopID 8 -> Out HopID 8 v 5
+ * 3 ^ Out HopID 8 <- In HopID 8 ^ 5
+ * ............ Domain border ............
+ * | |
+ * [Host 2] [Host 3]
+ */
+ host = alloc_host(test);
+ hub = alloc_dev_default(test, host, 0x1, true);
+
+ src_port = &hub->ports[3];
+ dst_port = &hub->ports[5];
+
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, 8, dst_port, 8, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, 2);
+ /* Path from the second host to the first one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].out_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[0]->hops[0].next_hop_index, 8);
+ /* Path from the first host to the second one */
+ KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 1);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, src_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].in_hop_index, 8);
+ KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].out_port, dst_port);
+ KUNIT_EXPECT_EQ(test, tunnel->paths[1]->hops[0].next_hop_index, 8);
+
+ tb_tunnel_put(tunnel);
+}
+
+static void tb_test_tunnel_dma_lane2lane_match(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port, *nhi;
+ struct tb_tunnel *tunnel;
+ struct tb_switch *host;
+
+ host = alloc_host(test);
+ nhi = &host->ports[7];
+ src_port = &host->ports[1];
+ dst_port = &host->ports[3];
+
+ /*
+ * Use a different HopID for each end and direction, which is what
+ * happens when there are already other tunnels going through, so
+ * that mixing them up gets caught below.
+ */
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, 9, dst_port, 10, 11);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+
+#define MATCH(src, src_in, src_out, dst, dst_in, dst_out) \
+ tb_tunnel_match_dma(tunnel, src, src_in, src_out, dst, dst_in, dst_out)
+
+ KUNIT_EXPECT_TRUE(test, MATCH(src_port, 8, 9, dst_port, 10, 11));
+ KUNIT_EXPECT_TRUE(test, MATCH(src_port, 8, -1, NULL, -1, -1));
+ KUNIT_EXPECT_TRUE(test, MATCH(dst_port, 10, -1, NULL, -1, -1));
+ KUNIT_EXPECT_TRUE(test, MATCH(src_port, -1, -1, dst_port, -1, -1));
+ /* The HopIDs of the two ends cannot be mixed up */
+ KUNIT_EXPECT_FALSE(test, MATCH(src_port, 10, 11, dst_port, 8, 9));
+ KUNIT_EXPECT_FALSE(test, MATCH(src_port, 9, -1, NULL, -1, -1));
+ KUNIT_EXPECT_FALSE(test, MATCH(dst_port, -1, 10, NULL, -1, -1));
+ /* Neither end is at the host interface */
+ KUNIT_EXPECT_FALSE(test, MATCH(nhi, -1, -1, NULL, -1, -1));
+
+ tb_tunnel_put(tunnel);
+
+#undef MATCH
+}
+
static void tb_test_credit_alloc_legacy_not_bonded(struct kunit *test)
{
struct tb_switch *host, *dev;
@@ -3382,6 +3615,174 @@ static void tb_test_credit_alloc_shared_hop(struct kunit *test)
usb4_switch_free_credit_pools(host);
}
+/* Same as what tb_port_configure_xdomain() does for the credit pools */
+static void tb_test_connect_xdomain(struct kunit *test, struct tb_port *port)
+{
+ port->xdomain = kunit_kzalloc(test, sizeof(*port->xdomain), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, port->xdomain);
+ usb4_port_update_credit_pools(port);
+}
+
+static unsigned int tb_test_dma_credits_used(const struct tb_port *port)
+{
+ return usb4_credit_pool_used(&port->credit_pool[USB4_CREDIT_POOL_COMMON]);
+}
+
+static void tb_test_credit_alloc_dma_lane2lane(struct kunit *test)
+{
+ struct tb_port *src_port, *dst_port;
+ struct tb_switch *host, *dev;
+ struct tb_tunnel *tunnel;
+ struct tb_path *path;
+
+ host = alloc_host_usb4(test);
+ dev = alloc_dev_usb4(test, host, 0x3, true);
+
+ KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+ KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(dev), 0);
+
+ src_port = &host->ports[1];
+ dst_port = &dev->ports[3];
+
+ tb_test_connect_xdomain(test, src_port);
+ tb_test_connect_xdomain(test, dst_port);
+
+ tunnel = tb_tunnel_alloc_dma(NULL, src_port, 8, 8, dst_port, 8, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel);
+ KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)2);
+
+ /*
+ * Both directions are treated the same way. The first hop is the
+ * lane adapter facing the remote host so all its buffers are
+ * available for DMA, the rest are allocated from the intermediate
+ * hops. Both paths end to a lane adapter so flow control is
+ * enabled on egress of every hop.
+ */
+ path = tunnel->paths[0];
+ 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, 14U);
+ KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+ KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+ path = tunnel->paths[1];
+ KUNIT_ASSERT_EQ(test, path->path_length, 2);
+ 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, 14U);
+ KUNIT_EXPECT_EQ(test, path->hops[1].nfc_credits, 0U);
+ KUNIT_EXPECT_EQ(test, path->hops[1].initial_credits, 14U);
+
+ tb_tunnel_put(tunnel);
+
+ usb4_switch_free_credit_pools(dev);
+ usb4_switch_free_credit_pools(host);
+}
+
+static void tb_test_credit_alloc_dma_lane2lane_multiple(struct kunit *test)
+{
+ struct tb_tunnel *tunnel1, *tunnel2, *tunnel3, *tunnel4, *tunnel5;
+ struct tb_port *src_port, *dst_port;
+ struct tb_tunnel *tunnel6;
+ struct tb_switch *host;
+ struct tb_path *path;
+
+ host = alloc_host_usb4(test);
+
+ KUNIT_ASSERT_EQ(test, usb4_switch_alloc_credit_pools(host), 0);
+
+ src_port = &host->ports[1];
+ dst_port = &host->ports[3];
+
+ tb_test_connect_xdomain(test, src_port);
+ tb_test_connect_xdomain(test, dst_port);
+
+ /*
+ * Couple the two hosts connected to lane adapters 1 and 3 several
+ * times, each coupling using different HopIDs.
+ *
+ * Neither lane adapter tunnels other protocols so all their
+ * buffers except the ones reserved for control traffic stay in
+ * the common pool:
+ *
+ * 60 - 2 = 58
+ *
+ * Each coupling takes 14 of those from both adapters, so the first
+ * four get what they asked for, the fifth gets the remaining 2 and
+ * after that we run out of buffers.
+ */
+ tunnel1 = tb_tunnel_alloc_dma(NULL, src_port, 8, 8, dst_port, 8, 8);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel1);
+ KUNIT_ASSERT_EQ(test, tunnel1->npaths, (size_t)2);
+
+ path = tunnel1->paths[0];
+ KUNIT_ASSERT_EQ(test, path->path_length, 1);
+ KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+
+ path = tunnel1->paths[1];
+ KUNIT_ASSERT_EQ(test, path->path_length, 1);
+ KUNIT_EXPECT_EQ(test, path->hops[0].nfc_credits, 0U);
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+
+ tunnel2 = tb_tunnel_alloc_dma(NULL, src_port, 9, 9, dst_port, 9, 9);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel2);
+ tunnel3 = tb_tunnel_alloc_dma(NULL, src_port, 10, 10, dst_port, 10, 10);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel3);
+ tunnel4 = tb_tunnel_alloc_dma(NULL, src_port, 11, 11, dst_port, 11, 11);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel4);
+
+ path = tunnel4->paths[0];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+ path = tunnel4->paths[1];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+
+ /* Both adapters have now handed out 4 * 14 = 56 of their 58 */
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(src_port), 56U);
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(dst_port), 56U);
+
+ /* The fifth one has to make do with what is left */
+ tunnel5 = tb_tunnel_alloc_dma(NULL, src_port, 12, 12, dst_port, 12, 12);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel5);
+
+ path = tunnel5->paths[0];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 2U);
+ path = tunnel5->paths[1];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 2U);
+
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(src_port), 58U);
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(dst_port), 58U);
+
+ /* And the sixth one no longer fits */
+ tunnel6 = tb_tunnel_alloc_dma(NULL, src_port, 13, 13, dst_port, 13, 13);
+ KUNIT_ASSERT_NULL(test, tunnel6);
+
+ /* Releasing the first one makes room for another */
+ tb_tunnel_put(tunnel1);
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(src_port), 44U);
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(dst_port), 44U);
+
+ tunnel1 = tb_tunnel_alloc_dma(NULL, src_port, 13, 13, dst_port, 13, 13);
+ KUNIT_ASSERT_NOT_NULL(test, tunnel1);
+
+ path = tunnel1->paths[0];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+ path = tunnel1->paths[1];
+ KUNIT_EXPECT_EQ(test, path->hops[0].initial_credits, 14U);
+
+ tb_tunnel_put(tunnel5);
+ tb_tunnel_put(tunnel4);
+ tb_tunnel_put(tunnel3);
+ tb_tunnel_put(tunnel2);
+ tb_tunnel_put(tunnel1);
+
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(src_port), 0U);
+ KUNIT_EXPECT_EQ(test, tb_test_dma_credits_used(dst_port), 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)
{
@@ -4147,6 +4548,11 @@ static struct kunit_case tb_test_cases[] = {
KUNIT_CASE(tb_test_tunnel_dma_tx),
KUNIT_CASE(tb_test_tunnel_dma_chain),
KUNIT_CASE(tb_test_tunnel_dma_match),
+ KUNIT_CASE(tb_test_tunnel_dma_lane2lane),
+ KUNIT_CASE(tb_test_tunnel_dma_lane2lane_one_way),
+ KUNIT_CASE(tb_test_tunnel_dma_lane2lane_chain),
+ KUNIT_CASE(tb_test_tunnel_dma_lane2lane_hub),
+ KUNIT_CASE(tb_test_tunnel_dma_lane2lane_match),
KUNIT_CASE(tb_test_credit_alloc_legacy_not_bonded),
KUNIT_CASE(tb_test_credit_alloc_legacy_bonded),
KUNIT_CASE(tb_test_credit_alloc_pcie),
@@ -4166,6 +4572,8 @@ static struct kunit_case tb_test_cases[] = {
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_dma_lane2lane),
+ KUNIT_CASE(tb_test_credit_alloc_dma_lane2lane_multiple),
KUNIT_CASE(tb_test_credit_alloc_all),
KUNIT_CASE(tb_test_property_parse),
KUNIT_CASE(tb_test_property_format),
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths()
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (5 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 9/9] Documentation: thunderbolt: Document " Mika Westerberg
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
A DMA tunnel does not need to end at the host interface. Both ends can
be lane adapters as well, in which case the router forwards the packets
from one remote host to the other. Generalize the DMA path handling to
cover this, and provide two new API functions: tb_xdomains_enable_paths()
and tb_xdomains_disable_paths() that can be used to create DMA tunnels
between two XDomains.
While there replace the only place where -ENOTSUPP is returned over
-EOPNOTSUPP.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
drivers/thunderbolt/domain.c | 70 +++++++++-------
drivers/thunderbolt/icm.c | 49 +++++++----
drivers/thunderbolt/tb.c | 149 +++++++++++++++++++++-------------
drivers/thunderbolt/tb.h | 28 ++++---
drivers/thunderbolt/xdomain.c | 110 +++++++++++++++++++++++--
include/linux/thunderbolt.h | 6 ++
6 files changed, 291 insertions(+), 121 deletions(-)
diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 61666e1a9b39..d4094036837e 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -797,59 +797,69 @@ int tb_domain_disconnect_pcie_paths(struct tb *tb)
/**
* tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
* @tb: Domain enabling the DMA paths
- * @xd: XDomain DMA paths are created to
- * @transmit_path: HopID we are using to send out packets
- * @transmit_ring: DMA ring used to send out packets
- * @receive_path: HopID the other end is using to send packets to us
- * @receive_ring: DMA ring used to receive packets from @receive_path
+ * @xd_a: XDomain DMA paths are created to
+ * @a_in_hopid: HopID @xd_a is using to send packets to us
+ * @a_out_hopid: HopID we are using to send packets to @xd_a
+ * @xd_b: The other end of the DMA paths or %NULL if it is the host
+ * interface. In the latter case @b_in_hopid and @b_out_hopid are
+ * the DMA ring numbers used to send and receive the packets.
+ * @b_in_hopid: HopID @xd_b is using to send packets to us
+ * @b_out_hopid: HopID we are using to send packets to @xd_b
*
- * Calls connection manager specific method to enable DMA paths to the
- * XDomain in question.
+ * Calls connection manager specific method to enable DMA paths between the
+ * two ends. Passing %-1 as HopID of a direction leaves that direction out.
*
* Return:
* * %0 - On success.
- * * %-ENOTSUPP - If the connection manager implementation does not support
- * XDomains.
+ * * %-EOPNOTSUPP - If the connection manager implementation does not support
+ * XDomains.
* * Negative errno - An error occurred.
*/
-int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
{
if (!tb->cm_ops->approve_xdomain_paths)
- return -ENOTSUPP;
+ return -EOPNOTSUPP;
- return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
- transmit_ring, receive_path, receive_ring);
+ return tb->cm_ops->approve_xdomain_paths(tb, xd_a, a_in_hopid,
+ a_out_hopid, xd_b, b_in_hopid,
+ b_out_hopid);
}
/**
* tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
* @tb: Domain disabling the DMA paths
- * @xd: XDomain whose DMA paths are disconnected
- * @transmit_path: HopID we are using to send out packets
- * @transmit_ring: DMA ring used to send out packets
- * @receive_path: HopID the other end is using to send packets to us
- * @receive_ring: DMA ring used to receive packets from @receive_path
+ * @xd_a: XDomain whose DMA paths are disconnected
+ * @a_in_hopid: HopID @xd_a is using to send packets to us
+ * @a_out_hopid: HopID we are using to send packets to @xd_a
+ * @xd_b: The other end of the DMA paths or %NULL if it is the host
+ * interface. In the latter case @b_in_hopid and @b_out_hopid are
+ * the DMA ring numbers.
+ * @b_in_hopid: HopID @xd_b is using to send packets to us
+ * @b_out_hopid: HopID we are using to send packets to @xd_b
*
- * Calls connection manager specific method to disconnect DMA paths to
- * the XDomain in question.
+ * Calls connection manager specific method to disconnect the DMA paths
+ * previously established by tb_domain_approve_xdomain_paths(). Passing %-1
+ * as any of the HopIDs means don't care.
*
* Return:
* * %0 - On success.
- * * %-ENOTSUPP - If the connection manager implementation does not support
- * XDomains.
+ * * %-EOPNOTSUPP - If the connection manager implementation does not support
+ * XDomains.
* * Negative errno - An error occurred.
*/
-int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
{
if (!tb->cm_ops->disconnect_xdomain_paths)
- return -ENOTSUPP;
+ return -EOPNOTSUPP;
- return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
- transmit_ring, receive_path, receive_ring);
+ return tb->cm_ops->disconnect_xdomain_paths(tb, xd_a, a_in_hopid,
+ a_out_hopid, xd_b, b_in_hopid, b_out_hopid);
}
static int disconnect_xdomain(struct device *dev, void *data)
diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index a295a5e6cc9c..3237ac54b684 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -580,13 +580,18 @@ static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
}
static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
{
struct icm_fr_pkg_approve_xdomain_response reply;
struct icm_fr_pkg_approve_xdomain request;
int ret;
+ /* The firmware can only tunnel to the host interface */
+ if (xd_b)
+ return -EOPNOTSUPP;
+
if (atomic_read(&xd->ntunnels) >= 1) {
tb_warn(tb, "only one tunnel is supported by the firmware\n");
return -EOPNOTSUPP;
@@ -597,10 +602,10 @@ static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
- request.transmit_path = transmit_path;
- request.transmit_ring = transmit_ring;
- request.receive_path = receive_path;
- request.receive_ring = receive_ring;
+ request.transmit_path = a_out_hopid;
+ request.transmit_ring = b_in_hopid;
+ request.receive_path = a_in_hopid;
+ request.receive_ring = b_out_hopid;
memset(&reply, 0, sizeof(reply));
ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
@@ -616,12 +621,16 @@ static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
}
static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid)
{
u8 phy_port;
u8 cmd;
+ if (xd_b)
+ return -EOPNOTSUPP;
+
phy_port = tb_phy_port_from_link(xd->link);
if (phy_port == 0)
cmd = NHI_MAILBOX_DISCONNECT_PA;
@@ -1156,13 +1165,17 @@ static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
}
static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
{
struct icm_tr_pkg_approve_xdomain_response reply;
struct icm_tr_pkg_approve_xdomain request;
int ret;
+ if (xd_b)
+ return -EOPNOTSUPP;
+
if (atomic_read(&xd->ntunnels) >= 1) {
tb_warn(tb, "only one tunnel is supported by the firmware\n");
return -EOPNOTSUPP;
@@ -1172,10 +1185,10 @@ static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
request.hdr.code = ICM_APPROVE_XDOMAIN;
request.route_hi = upper_32_bits(xd->route);
request.route_lo = lower_32_bits(xd->route);
- request.transmit_path = transmit_path;
- request.transmit_ring = transmit_ring;
- request.receive_path = receive_path;
- request.receive_ring = receive_ring;
+ request.transmit_path = a_out_hopid;
+ request.transmit_ring = b_in_hopid;
+ request.receive_path = a_in_hopid;
+ request.receive_ring = b_out_hopid;
memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
memset(&reply, 0, sizeof(reply));
@@ -1218,11 +1231,15 @@ static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
}
static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid)
{
int ret;
+ if (xd_b)
+ return -EOPNOTSUPP;
+
ret = icm_tr_xdomain_tear_down(tb, xd, 1);
if (ret)
return ret;
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index c7a009a62411..4db25062b5e5 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -87,9 +87,10 @@ 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_disconnect_dma_paths(struct tb *tb, struct tb_port *port_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_port *port_b, int b_in_hopid,
+ int b_out_hopid);
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,
@@ -472,7 +473,7 @@ static void tb_remove_xdomain(struct tb_port *port)
xd->is_unplugged = true;
tb_xdomain_remove(port->xdomain);
port->xdomain = NULL;
- __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
+ __tb_disconnect_dma_paths(tb, port, -1, -1, NULL, -1, -1);
tb_port_unconfigure_xdomain(port);
tb_xdomain_put(xd);
}
@@ -2365,44 +2366,97 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
return 0;
}
-static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring)
+/*
+ * Tears down the DMA tunnels matching the two ends. Called with @tb->lock
+ * held. See tb_tunnel_match_dma() for the wildcards.
+ */
+static void __tb_disconnect_dma_paths(struct tb *tb, struct tb_port *port_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_port *port_b, int b_in_hopid,
+ int b_out_hopid)
{
struct tb_cm *tcm = tb_priv(tb);
- struct tb_port *nhi_port, *dst_port;
+ struct tb_tunnel *tunnel, *n;
+
+ list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
+ struct tb_switch *sw_a, *sw_b;
+
+ if (!tb_tunnel_is_dma(tunnel))
+ continue;
+ if (!tb_tunnel_match_dma(tunnel, port_a, a_in_hopid, a_out_hopid,
+ port_b, b_in_hopid, b_out_hopid))
+ continue;
+
+ sw_a = tunnel->src_port->sw;
+ sw_b = tunnel->dst_port->sw;
+ tb_deactivate_and_free_tunnel(tunnel);
+
+ /*
+ * Try to re-enable CL states now, it is OK if this
+ * fails because we may still have another DMA tunnel
+ * active through the same router USB4 port.
+ */
+ tb_enable_clx(sw_a);
+ tb_enable_clx(sw_b);
+ }
+}
+
+/*
+ * Returns the adapter where the DMA paths of @xd end. Passing %NULL means
+ * the host interface.
+ */
+static struct tb_port *tb_xdomain_port(struct tb *tb, struct tb_xdomain *xd)
+{
+ if (!xd)
+ return tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
+ return tb_xdomain_downstream_port(xd);
+}
+
+static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
+{
+ struct tb_cm *tcm = tb_priv(tb);
+ struct tb_port *port_a, *port_b;
struct tb_tunnel *tunnel;
- struct tb_switch *sw;
int ret;
mutex_lock(&tb->lock);
- /* The XDomain is going away so no new tunnels allowed */
- if (xd->is_unplugged) {
+ /* The remote host or hosts need to be still connected */
+ if (xd_a->is_unplugged || (xd_b && xd_b->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);
+ port_a = tb_xdomain_port(tb, xd_a);
+ port_b = tb_xdomain_port(tb, xd_b);
/*
- * When tunneling DMA paths the link should not enter CL states
- * so disable them now.
+ * When tunneling DMA paths the links should not enter CL states
+ * so disable them now. This is a no-op for the host router.
*/
- tb_disable_clx(sw);
+ tb_disable_clx(port_a->sw);
+ tb_disable_clx(port_b->sw);
- tunnel = tb_tunnel_alloc_dma(tb, nhi_port, transmit_ring, receive_ring,
- dst_port, receive_path, transmit_path);
+ /*
+ * Keep the host interface as the first end when it is one of the
+ * two, the same way as with host-to-host tunnels.
+ */
+ if (xd_b)
+ tunnel = tb_tunnel_alloc_dma(tb, port_a, a_in_hopid, a_out_hopid,
+ port_b, b_in_hopid, b_out_hopid);
+ else
+ tunnel = tb_tunnel_alloc_dma(tb, port_b, b_in_hopid, b_out_hopid,
+ port_a, a_in_hopid, a_out_hopid);
if (!tunnel) {
ret = -ENOMEM;
goto err_clx;
}
if (tb_tunnel_activate(tunnel)) {
- tb_port_info(nhi_port,
- "DMA tunnel activation failed, aborting\n");
+ tb_tunnel_info(tunnel, "tunnel activation failed, aborting\n");
ret = -EIO;
goto err_free;
}
@@ -2414,53 +2468,32 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
err_free:
tb_tunnel_put(tunnel);
err_clx:
- tb_enable_clx(sw);
+ tb_enable_clx(port_b->sw);
+ tb_enable_clx(port_a->sw);
err_unlock:
mutex_unlock(&tb->lock);
return ret;
}
-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 int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid)
{
- struct tb_cm *tcm = tb_priv(tb);
- struct tb_port *nhi_port, *dst_port;
- struct tb_tunnel *tunnel, *n;
- struct tb_switch *sw;
-
- 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);
-
- list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
- if (!tb_tunnel_is_dma(tunnel))
- continue;
-
- if (tb_tunnel_match_dma(tunnel, nhi_port, transmit_ring,
- receive_ring, dst_port, receive_path,
- transmit_path))
- tb_deactivate_and_free_tunnel(tunnel);
- }
-
+ mutex_lock(&tb->lock);
/*
- * Try to re-enable CL states now, it is OK if this fails
- * because we may still have another DMA tunnel active through
- * the same host router USB4 downstream port.
+ * If either end is unplugged the tunnels are already torn down by
+ * tb_remove_xdomain() so there is nothing left to do here.
*/
- tb_enable_clx(sw);
-}
+ if (!xd_a->is_unplugged && !(xd_b && xd_b->is_unplugged)) {
+ struct tb_port *port_a, *port_b;
-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)
-{
- mutex_lock(&tb->lock);
- if (!xd->is_unplugged)
- __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
- transmit_ring, receive_path,
- receive_ring);
+ port_a = tb_xdomain_port(tb, xd_a);
+ port_b = tb_xdomain_port(tb, xd_b);
+ __tb_disconnect_dma_paths(tb, port_a, a_in_hopid, a_out_hopid,
+ port_b, b_in_hopid, b_out_hopid);
+ }
mutex_unlock(&tb->lock);
return 0;
}
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 6da3492fd322..3126610a3602 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -639,12 +639,14 @@ struct tb_cm_ops {
int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
const u8 *challenge, u8 *response);
int (*disconnect_pcie_paths)(struct tb *tb);
- int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring);
- int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring);
+ int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid);
+ int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid);
int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
u8 *status, const void *tx_data, size_t tx_data_len,
void *rx_data, size_t rx_data_len);
@@ -896,12 +898,14 @@ int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
int tb_domain_disconnect_pcie_paths(struct tb *tb);
-int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring);
-int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
- int transmit_path, int transmit_ring,
- int receive_path, int receive_ring);
+int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid);
+int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd_a,
+ int a_in_hopid, int a_out_hopid,
+ struct tb_xdomain *xd_b, int b_in_hopid,
+ int b_out_hopid);
int tb_domain_disconnect_all_paths(struct tb *tb);
int tb_domain_unregister_unplugged_xdomains(struct tb *tb);
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index fceaa177b3c1..b9f8ac31bb20 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -2474,8 +2474,8 @@ int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
{
int ret;
- ret = tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path,
- transmit_ring, receive_path,
+ ret = tb_domain_approve_xdomain_paths(xd->tb, xd, receive_path,
+ transmit_path, NULL, transmit_ring,
receive_ring);
if (ret)
return ret;
@@ -2505,9 +2505,9 @@ int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
{
int ret;
- ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path,
- transmit_ring, receive_path,
- receive_ring);
+ ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd, receive_path,
+ transmit_path, NULL,
+ transmit_ring, receive_ring);
if (ret)
return ret;
atomic_dec(&xd->ntunnels);
@@ -2515,6 +2515,106 @@ int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
}
EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
+static int
+validate_xdomains(const struct tb_xdomain *xd_a, const struct tb_xdomain *xd_b)
+{
+ if (!xd_a || !xd_b)
+ return -EINVAL;
+ if (xd_a == xd_b)
+ return -EINVAL;
+ /* Must belong to the same domain */
+ if (xd_a->tb != xd_b->tb)
+ return -EINVAL;
+ return 0;
+}
+
+/**
+ * tb_xdomains_enable_paths() - Enable DMA paths between two XDomains
+ * @xd_a: First XDomain connection
+ * @a_in_hopid: HopID @xd_a is using to send packets to us
+ * @a_out_hopid: HopID we are using to send packets to @xd_a
+ * @xd_b: Second XDomain connection
+ * @b_in_hopid: HopID @xd_b is using to send packets to us
+ * @b_out_hopid: HopID we are using to send packets to @xd_b
+ *
+ * Like tb_xdomain_enable_paths() but instead of ending at the host
+ * interface, the DMA paths forward packets from one XDomain to the
+ * other through this domain. If only one direction is needed, pass %-1
+ * for both HopIDs of the other one.
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+int tb_xdomains_enable_paths(struct tb_xdomain *xd_a, int a_in_hopid,
+ int a_out_hopid, struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid)
+{
+ bool a_to_b, b_to_a;
+ int ret;
+
+ ret = validate_xdomains(xd_a, xd_b);
+ if (ret)
+ return ret;
+
+ a_to_b = a_in_hopid > 0 && b_out_hopid > 0;
+ b_to_a = b_in_hopid > 0 && a_out_hopid > 0;
+
+ /* A direction needs the HopIDs of both of its ends */
+ if (!a_to_b && (a_in_hopid > 0 || b_out_hopid > 0))
+ return -EINVAL;
+ if (!b_to_a && (b_in_hopid > 0 || a_out_hopid > 0))
+ return -EINVAL;
+ /* And at least one of the two directions is needed */
+ if (!a_to_b && !b_to_a)
+ return -EINVAL;
+
+ ret = tb_domain_approve_xdomain_paths(xd_a->tb, xd_a, a_in_hopid,
+ a_out_hopid, xd_b, b_in_hopid,
+ b_out_hopid);
+ if (ret)
+ return ret;
+
+ atomic_inc(&xd_a->ntunnels);
+ atomic_inc(&xd_b->ntunnels);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(tb_xdomains_enable_paths);
+
+/**
+ * tb_xdomains_disable_paths() - Disable DMA paths between two XDomains
+ * @xd_a: First XDomain connection
+ * @a_in_hopid: HopID @xd_a is using to send packets to us
+ * @a_out_hopid: HopID we are using to send packets to @xd_a
+ * @xd_b: Second XDomain connection
+ * @b_in_hopid: HopID @xd_b is using to send packets to us
+ * @b_out_hopid: HopID we are using to send packets to @xd_b
+ *
+ * This does the opposite of tb_xdomains_enable_paths(). The callers should
+ * pass the same values here as they did when the paths were enabled.
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+int tb_xdomains_disable_paths(struct tb_xdomain *xd_a, int a_in_hopid,
+ int a_out_hopid, struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid)
+{
+ int ret;
+
+ ret = validate_xdomains(xd_a, xd_b);
+ if (ret)
+ return ret;
+
+ ret = tb_domain_disconnect_xdomain_paths(xd_a->tb, xd_a, a_in_hopid,
+ a_out_hopid, xd_b, b_in_hopid,
+ b_out_hopid);
+ if (ret)
+ return ret;
+
+ atomic_dec(&xd_a->ntunnels);
+ atomic_dec(&xd_b->ntunnels);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(tb_xdomains_disable_paths);
+
struct tb_xdomain_lookup {
const uuid_t *uuid;
u8 link;
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 03f315934e44..a19d6fede0bd 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -307,6 +307,12 @@ int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
int transmit_ring, int receive_path,
int receive_ring);
+int tb_xdomains_enable_paths(struct tb_xdomain *xd_a, int a_in_hopid,
+ int a_out_hopid, struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid);
+int tb_xdomains_disable_paths(struct tb_xdomain *xd_a, int a_in_hopid,
+ int a_out_hopid, struct tb_xdomain *xd_b,
+ int b_in_hopid, int b_out_hopid);
static inline int tb_xdomain_disable_all_paths(struct tb_xdomain *xd)
{
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (6 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths() Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
2026-11-25 15:26 ` [PATCH 9/9] Documentation: thunderbolt: Document " Mika Westerberg
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
When there are multiple hosts involved like host1 <-> host2 <-> host3,
and there is need to forward one stream to another through the middle
host (host2), the simplest way is to run something like:
# dd if=/dev/tbstream0 of=/dev/tbstream1
However, that involves copying the frames to the userspace and back, and
this is just for one direction (in case of tool such as dd, a custom
application may do this in both directions simultaneously).
We can avoid all this by making a tunnel directly between the leaf
hosts. This does not involve the middle host CPU at all on the data
path. This is called USB4STREAM coupling. It is configured on the middle
host via ConfigFS as before but it will not result a /dev/tbstreamX to
be created:
# cd /sys/kernel/config/thunderbolt/stream/coupling
# mkdir -p 0-1.0_0-3.0/data
# cd 0-1.0_0-3.0/data
# echo -1 > 0-1.0/in_hopid
# echo -1 > 0-1.0/out_hopid
# echo -1 > 0-3.0/in_hopid
# echo -1 > 0-3.0/out_hopid
Once the XDomains are connected this will create a DMA tunnel directly
between XDomains 0-1 and 0-3 using the HopIDs configured above. Then the
hosts at both ends can use their stream exactly as before.
To get rid of the coupling run rmdir similarly to regular streams:
# cd /sys/kernel/config/thunderbolt/stream/coupling
# rmdir -p 0-1.0_0-3.0/data
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
.../ABI/testing/configfs-thunderbolt_stream | 82 ++
drivers/thunderbolt/stream.c | 888 +++++++++++++++++-
2 files changed, 960 insertions(+), 10 deletions(-)
diff --git a/Documentation/ABI/testing/configfs-thunderbolt_stream b/Documentation/ABI/testing/configfs-thunderbolt_stream
index cbecb3d8db50..d4af0596b602 100644
--- a/Documentation/ABI/testing/configfs-thunderbolt_stream
+++ b/Documentation/ABI/testing/configfs-thunderbolt_stream
@@ -96,3 +96,85 @@ Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Interrupt throttling rate in ns. Lower values can give
better latency. The default is 8192 ns.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ Fixed directory for coupled streams. If this host is
+ connected to two other hosts, for example A <-> B <-> C
+ where B is this host, then B can forward streams between A
+ and C through the routers in its domain.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling/<pair>
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ Configuration group for a pair of stream services that are
+ coupled together. The <pair> name consists of the two
+ service names separated by "_", for instance
+ "0-1.0_0-3.0". The two must be different and belong to the
+ same domain. The order does not matter and the same two
+ services can be paired only once.
+
+ A service can be part of several pairs, so streams of one
+ host can be forwarded to more than one other host.
+
+ Like with the non-coupled streams the group can be created
+ before either of the two hosts is connected. The couplings
+ below it are established once both of them show up.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling/<pair>/$name
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ Creates a new coupled stream with $name. The stream is
+ established once in_hopid and out_hopid of both sides
+ have been filled in. If the two hosts announce the
+ HopIDs of a stream with this name they are filled in
+ automatically. Likewise the HopIDs of each side are
+ announced to the host on that side, so it is enough to
+ configure this end.
+
+ There will be no /dev/tbstreamX device for coupled
+ streams.
+
+ Removing the directory tears the tunnel down and this is
+ also the only way to get rid of a coupling.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling/<pair>/$name/<xdomain>.<service>
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ Holds the HopIDs of one side of the coupling. Both sides are
+ always present, named after the two stream services of
+ <pair>.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling/<pair>/$name/<xdomain>.<service>/in_hopid
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ In HopID the host on this side of the coupling uses to send
+ packets to this host. It needs to match the out_hopid that
+ host has configured for the stream, and if that host
+ announces it in its XDomain properties it is picked up from
+ there. You can pass -1 for automatic allocation.
+
+ Once both HopIDs are filled in for both services the
+ tunnel will be established. Writing 0 to any of the
+ attributes will tear it down.
+
+What: /sys/kernel/config/thunderbolt/stream/coupling/<pair>/$name/<xdomain>.<service>/out_hopid
+Date: Feb 2027
+KernelVersion: v7.5
+Contact: Mika Westerberg <westeri@kernel.org>
+Description:
+ Out HopID this host uses to send packets to the host on
+ this side of the coupling. It needs to match the in_hopid
+ that host has configured for the stream. Otherwise this
+ behaves like in_hopid above.
diff --git a/drivers/thunderbolt/stream.c b/drivers/thunderbolt/stream.c
index 2661f563b99c..fb6d25694c20 100644
--- a/drivers/thunderbolt/stream.c
+++ b/drivers/thunderbolt/stream.c
@@ -197,6 +197,83 @@ struct tbstream_group {
struct list_head dev_list;
};
+/**
+ * struct tbstream_coupling_host - One of the two hosts of a coupling group
+ * @name: Name of the stream service connecting to the host
+ * @stream: The stream if the host is connected, %NULL otherwise
+ */
+struct tbstream_coupling_host {
+ const char *name;
+ struct tbstream *stream;
+};
+
+/**
+ * struct tbstream_coupling_side - One side of a coupling
+ * @group: ConfigFS group, named after the service of that side
+ * @coupling: Coupling this side belongs to
+ * @host: Host this side is facing
+ * @in_hopid: HopID the host on this side uses to send packets to us
+ * @out_hopid: HopID we use to send packets to the host on this side
+ * @in_hopid_alloc: Allocated @in_hopid, %0 if not allocated
+ * @out_hopid_alloc: Allocated @out_hopid, %0 if not allocated
+ */
+struct tbstream_coupling_side {
+ struct config_group group;
+ struct tbstream_coupling *coupling;
+ const struct tbstream_coupling_host *host;
+ int in_hopid;
+ int out_hopid;
+ int in_hopid_alloc;
+ int out_hopid_alloc;
+};
+
+/**
+ * struct tbstream_coupling - Coupled stream between two other hosts
+ * @group: ConfigFS group for the coupling, named after the stream
+ * @cg: Coupling group this coupling belongs to
+ * @side_a: Side facing &tbstream_coupling_group.host_a
+ * @side_b: Side facing &tbstream_coupling_group.host_b
+ * @enabled: The tunnel has been established
+ * @list: Couplings are linked through this in @cg->coupling_list
+ *
+ * This is the ConfigFS directory for a single stream that is forwarded
+ * between the two hosts of @cg. Protected by @cg->lock.
+ */
+struct tbstream_coupling {
+ struct config_group group;
+ struct tbstream_coupling_group *cg;
+ struct tbstream_coupling_side side_a;
+ struct tbstream_coupling_side side_b;
+ bool enabled;
+ struct list_head list;
+};
+
+/**
+ * struct tbstream_coupling_group - ConfigFS group for a pair of connections
+ * @group: ConfigFS group
+ * @host_a: Host named by the first half of the group name
+ * @host_b: Host named by the second half of the group name
+ * @lock: Lock protecting this structure and the couplings below it. May be
+ * held while taking &tb.lock so must not be taken the other way
+ * around.
+ * @coupling_list: List of couplings
+ * @list: Coupling groups are linked through this in @tbstream_coupling_list
+ *
+ * This is the ConfigFS directory for a pair of connections to two other
+ * hosts. There can be several &struct tbstream_coupling linked through
+ * @coupling_list. When a host is not connected the configuration below
+ * @coupling_list is kept until it comes back. Reference count managed
+ * through @group.
+ */
+struct tbstream_coupling_group {
+ struct config_group group;
+ struct tbstream_coupling_host host_a;
+ struct tbstream_coupling_host host_b;
+ struct mutex lock;
+ struct list_head coupling_list;
+ struct list_head list;
+};
+
/**
* struct tbstream - Stream service private data
* @kref: Reference count
@@ -217,6 +294,13 @@ static DEFINE_IDA(tbstream_indices);
static DEFINE_MUTEX(tbstream_lock);
static LIST_HEAD(tbstream_list);
+/*
+ * Protects tbstream_coupling_list. If you need to take both
+ * tbstream_lock and this one, take this first.
+ */
+static DEFINE_MUTEX(tbstream_coupling_lock);
+static LIST_HEAD(tbstream_coupling_list);
+
/* Serializes tbstream_get()/put() */
static DEFINE_MUTEX(tbstream_kref_lock);
@@ -1667,6 +1751,41 @@ to_tbstream_group(struct config_group *group)
return container_of(group, struct tbstream_group, group);
}
+static bool
+tbstream_coupling_group_has_service(const struct tbstream_coupling_group *cg,
+ const char *name)
+{
+ return !strcmp(cg->host_a.name, name) || !strcmp(cg->host_b.name, name);
+}
+
+/* tbstream_coupling_lock must be held */
+static bool
+tbstream_coupling_service_has_name(const char *svc_name, const char *name,
+ const struct tbstream_coupling_group *skip)
+{
+ struct tbstream_coupling_group *tmp;
+
+ list_for_each_entry(tmp, &tbstream_coupling_list, list) {
+ struct tbstream_coupling *coupling;
+
+ if (tmp == skip)
+ continue;
+ if (!tbstream_coupling_group_has_service(tmp, svc_name))
+ continue;
+
+ guard(mutex)(&tmp->lock);
+
+ list_for_each_entry(coupling, &tmp->coupling_list, list) {
+ struct config_item *item = &coupling->group.cg_item;
+
+ if (!strcmp(config_item_name(item), name))
+ return true;
+ }
+ }
+
+ return false;
+}
+
static struct config_group *
tbstream_dev_make_group(struct config_group *group, const char *name)
{
@@ -1681,6 +1800,14 @@ tbstream_dev_make_group(struct config_group *group, const char *name)
if (strlen(name) > TB_PROPERTY_KEY_SIZE)
return ERR_PTR(-ENAMETOOLONG);
+ /* Both would announce the same property directory */
+ scoped_guard(mutex, &tbstream_coupling_lock) {
+ const char *svc_name = config_item_name(&group->cg_item);
+
+ if (tbstream_coupling_service_has_name(svc_name, name, NULL))
+ return ERR_PTR(-EEXIST);
+ }
+
sdev = kzalloc_obj(*sdev);
if (!sdev)
return ERR_PTR(-ENOMEM);
@@ -1763,15 +1890,21 @@ static const struct config_item_type tbstream_dev_group_type = {
.ct_item_ops = &tbstream_item_ops,
};
-static bool tbstream_valid_service_name(const char *name)
+static bool tbstream_valid_service_name(const char *name,
+ unsigned int *domain)
{
- unsigned int domain, index;
+ unsigned int d, index;
int len = 0;
u64 route;
- if (sscanf(name, "%u-%llx.%u%n", &domain, &route, &index, &len) != 3)
+ if (sscanf(name, "%u-%llx.%u%n", &d, &route, &index, &len) != 3)
+ return false;
+ if (len != strlen(name))
return false;
- return len == strlen(name);
+
+ if (domain)
+ *domain = d;
+ return true;
}
static struct config_group *
@@ -1780,7 +1913,7 @@ tbstream_make_group(struct config_group *group, const char *name)
struct tbstream_group *sg;
struct tbstream *stream;
- if (!tbstream_valid_service_name(name))
+ if (!tbstream_valid_service_name(name, NULL))
return ERR_PTR(-EINVAL);
sg = kzalloc_obj(*sg);
@@ -1833,6 +1966,732 @@ static struct tbstream_group *tbstream_group_find(struct tbstream *stream)
return to_tbstream_group(to_config_group(item));
}
+static inline struct tbstream_coupling_side *
+tbstream_coupling_side_from_item(struct config_item *item)
+{
+ struct config_group *group = to_config_group(item);
+
+ return container_of(group, struct tbstream_coupling_side, group);
+}
+
+/* cg->lock must be held */
+static struct tb_xdomain *
+tbstream_coupling_side_xdomain(struct tbstream_coupling_side *side)
+{
+ struct tbstream *stream = side->host->stream;
+
+ return stream ? tb_service_parent(stream->svc) : NULL;
+}
+
+/* cg->lock must be held */
+static void
+tbstream_coupling_side_update_properties(const struct tbstream_coupling_side *side,
+ int in_hopid, int out_hopid)
+{
+ struct tbstream *stream = side->host->stream;
+ const char *name = config_item_name(&side->coupling->group.cg_item);
+
+ if (!stream)
+ return;
+
+ if (!service_update_properties(stream->svc, name, in_hopid, out_hopid))
+ tb_service_properties_changed(stream->svc);
+}
+
+/* cg->lock must be held */
+static bool
+tbstream_coupling_side_ready(const struct tbstream_coupling_side *side)
+{
+ return side->host->stream && side->in_hopid_alloc &&
+ side->out_hopid_alloc;
+}
+
+/* cg->lock must be held */
+static bool tbstream_coupling_ready(const struct tbstream_coupling *coupling)
+{
+ return tbstream_coupling_side_ready(&coupling->side_a) &&
+ tbstream_coupling_side_ready(&coupling->side_b);
+}
+
+/* cg->lock must be held */
+static int tbstream_coupling_enable(struct tbstream_coupling *coupling)
+{
+ struct tbstream_coupling_side *a = &coupling->side_a;
+ struct tbstream_coupling_side *b = &coupling->side_b;
+ int ret;
+
+ if (coupling->enabled)
+ return 0;
+ if (!tbstream_coupling_ready(coupling))
+ return 0;
+
+ ret = tb_xdomains_enable_paths(tbstream_coupling_side_xdomain(a),
+ a->in_hopid_alloc, a->out_hopid_alloc,
+ tbstream_coupling_side_xdomain(b),
+ b->in_hopid_alloc, b->out_hopid_alloc);
+ if (ret)
+ return ret;
+
+ coupling->enabled = true;
+ return 0;
+}
+
+/* cg->lock must be held */
+static void tbstream_coupling_disable(struct tbstream_coupling *coupling)
+{
+ struct tbstream_coupling_side *a = &coupling->side_a;
+ struct tbstream_coupling_side *b = &coupling->side_b;
+
+ if (!coupling->enabled)
+ return;
+
+ tb_xdomains_disable_paths(tbstream_coupling_side_xdomain(a),
+ a->in_hopid_alloc, a->out_hopid_alloc,
+ tbstream_coupling_side_xdomain(b),
+ b->in_hopid_alloc, b->out_hopid_alloc);
+ coupling->enabled = false;
+}
+
+/* cg->lock must be held */
+static int
+tbstream_coupling_side_alloc_in_hopid(struct tbstream_coupling_side *side,
+ int hopid)
+{
+ struct tb_xdomain *xd = tbstream_coupling_side_xdomain(side);
+ int ret;
+
+ /* Already allocated */
+ if (hopid > 0 && hopid == side->in_hopid_alloc)
+ return 0;
+ if (hopid < 0 && side->in_hopid_alloc)
+ return 0;
+ if (!hopid) {
+ if (side->in_hopid_alloc)
+ tb_xdomain_release_in_hopid(xd, side->in_hopid_alloc);
+ side->in_hopid_alloc = 0;
+ side->in_hopid = hopid;
+ return 0;
+ }
+ ret = tb_xdomain_alloc_in_hopid(xd, hopid);
+ if (ret < 0)
+ return ret;
+ if (hopid > 0 && hopid != ret) {
+ tb_xdomain_release_in_hopid(xd, ret);
+ return -EBUSY;
+ }
+ if (side->in_hopid_alloc)
+ tb_xdomain_release_in_hopid(xd, side->in_hopid_alloc);
+ side->in_hopid_alloc = side->in_hopid = ret;
+ return 0;
+}
+
+/* cg->lock must be held */
+static int
+tbstream_coupling_side_alloc_out_hopid(struct tbstream_coupling_side *side,
+ int hopid)
+{
+ struct tb_xdomain *xd = tbstream_coupling_side_xdomain(side);
+ int ret;
+
+ if (hopid > 0 && hopid == side->out_hopid_alloc)
+ return 0;
+ if (hopid < 0 && side->out_hopid_alloc)
+ return 0;
+ if (!hopid) {
+ if (side->out_hopid_alloc)
+ tb_xdomain_release_out_hopid(xd, side->out_hopid_alloc);
+ side->out_hopid_alloc = 0;
+ side->out_hopid = hopid;
+ return 0;
+ }
+ ret = tb_xdomain_alloc_out_hopid(xd, hopid);
+ if (ret < 0)
+ return ret;
+ if (hopid > 0 && hopid != ret) {
+ tb_xdomain_release_out_hopid(xd, ret);
+ return -EBUSY;
+ }
+ if (side->out_hopid_alloc)
+ tb_xdomain_release_out_hopid(xd, side->out_hopid_alloc);
+ side->out_hopid_alloc = side->out_hopid = ret;
+ return 0;
+}
+
+static ssize_t
+tbstream_coupling_side_store_hopid(struct tbstream_coupling_side *side,
+ const char *buf, size_t count, bool in,
+ int *hopidp)
+{
+ struct tbstream_coupling *coupling = side->coupling;
+ const char *pair = config_item_name(&coupling->cg->group.cg_item);
+ const char *name = config_item_name(&coupling->group.cg_item);
+ int ret, hopid;
+
+ ret = kstrtoint(buf, 0, &hopid);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&coupling->cg->lock);
+
+ /*
+ * The HopIDs of a running coupling cannot be changed but
+ * writing 0 tears it down.
+ */
+ if (coupling->enabled) {
+ if (hopid)
+ return -EBUSY;
+ tbstream_coupling_disable(coupling);
+ }
+
+ /* The XDomain is not present yet so just store the HopID */
+ if (!tbstream_coupling_side_xdomain(side)) {
+ *hopidp = hopid;
+ return count;
+ }
+
+ if (in)
+ ret = tbstream_coupling_side_alloc_in_hopid(side, hopid);
+ else
+ ret = tbstream_coupling_side_alloc_out_hopid(side, hopid);
+ if (ret)
+ return ret;
+
+ ret = tbstream_coupling_enable(coupling);
+ if (ret) {
+ pr_warn("%s/%s: failed to enable coupling\n", pair, name);
+ /* Keep the HopIDs allocated so that next write retries */
+ }
+
+ tbstream_coupling_side_update_properties(side, side->in_hopid_alloc,
+ side->out_hopid_alloc);
+ return ret ? ret : count;
+}
+
+static ssize_t
+tbstream_coupling_side_in_hopid_show(struct config_item *item, char *buf)
+{
+ struct tbstream_coupling_side *side =
+ tbstream_coupling_side_from_item(item);
+
+ guard(mutex)(&side->coupling->cg->lock);
+ return sysfs_emit(buf, "%d\n", side->in_hopid);
+}
+
+static ssize_t
+tbstream_coupling_side_in_hopid_store(struct config_item *item,
+ const char *buf, size_t count)
+{
+ struct tbstream_coupling_side *side =
+ tbstream_coupling_side_from_item(item);
+
+ return tbstream_coupling_side_store_hopid(side, buf, count, true,
+ &side->in_hopid);
+}
+CONFIGFS_ATTR(tbstream_coupling_side_, in_hopid);
+
+static ssize_t
+tbstream_coupling_side_out_hopid_show(struct config_item *item, char *buf)
+{
+ struct tbstream_coupling_side *side =
+ tbstream_coupling_side_from_item(item);
+
+ guard(mutex)(&side->coupling->cg->lock);
+ return sysfs_emit(buf, "%d\n", side->out_hopid);
+}
+
+static ssize_t
+tbstream_coupling_side_out_hopid_store(struct config_item *item,
+ const char *buf, size_t count)
+{
+ struct tbstream_coupling_side *side =
+ tbstream_coupling_side_from_item(item);
+
+ return tbstream_coupling_side_store_hopid(side, buf, count, false,
+ &side->out_hopid);
+}
+CONFIGFS_ATTR(tbstream_coupling_side_, out_hopid);
+
+static struct configfs_attribute *tbstream_coupling_side_attrs[] = {
+ &tbstream_coupling_side_attr_in_hopid,
+ &tbstream_coupling_side_attr_out_hopid,
+ NULL,
+};
+
+static const struct config_item_type tbstream_coupling_side_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_attrs = tbstream_coupling_side_attrs,
+};
+
+/* cg->lock must be held */
+static void
+tbstream_coupling_side_release_hopids(struct tbstream_coupling_side *side)
+{
+ struct tb_xdomain *xd;
+
+ xd = tbstream_coupling_side_xdomain(side);
+ if (!xd)
+ return;
+
+ tbstream_coupling_side_update_properties(side, 0, 0);
+
+ if (side->out_hopid_alloc) {
+ tb_xdomain_release_out_hopid(xd, side->out_hopid_alloc);
+ side->out_hopid_alloc = 0;
+ }
+ if (side->in_hopid_alloc) {
+ tb_xdomain_release_in_hopid(xd, side->in_hopid_alloc);
+ side->in_hopid_alloc = 0;
+ }
+}
+
+static void tbstream_coupling_item_release(struct config_item *item)
+{
+ struct config_group *g = to_config_group(item);
+ struct tbstream_coupling *coupling = container_of(g,
+ struct tbstream_coupling, group);
+
+ configfs_remove_default_groups(&coupling->group);
+ kfree(coupling);
+}
+
+static struct configfs_item_operations tbstream_coupling_item_ops = {
+ .release = tbstream_coupling_item_release,
+};
+
+static const struct config_item_type tbstream_coupling_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_item_ops = &tbstream_coupling_item_ops,
+};
+
+static inline struct tbstream_coupling_group *
+to_tbstream_coupling_group(struct config_group *group)
+{
+ return container_of(group, struct tbstream_coupling_group, group);
+}
+
+/* coupling->cg->lock must be held */
+static void tbstream_coupling_attach_side(struct tbstream_coupling_side *side)
+{
+ struct tbstream_coupling *coupling = side->coupling;
+ const char *pair = config_item_name(&coupling->cg->group.cg_item);
+ const char *name = config_item_name(&coupling->group.cg_item);
+ struct tb_service *svc = side->host->stream->svc;
+ int ret;
+
+ /*
+ * If the user did not configure this side, see if the host on
+ * it announces the HopIDs of this stream and use those.
+ */
+ if (side->in_hopid <= 0 && side->out_hopid <= 0)
+ service_get_hopids(svc, name, &side->in_hopid,
+ &side->out_hopid);
+
+ /*
+ * The HopIDs were released when the host went away so allocate
+ * them again using the configured values.
+ */
+ if (side->in_hopid) {
+ ret = tbstream_coupling_side_alloc_in_hopid(side, side->in_hopid);
+ if (ret) {
+ pr_warn("%s/%s: failed to allocate in HopID %d\n",
+ pair, name, side->in_hopid);
+ return;
+ }
+ }
+ if (side->out_hopid) {
+ ret = tbstream_coupling_side_alloc_out_hopid(side, side->out_hopid);
+ if (ret) {
+ pr_warn("%s/%s: failed to allocate out HopID %d\n",
+ pair, name, side->out_hopid);
+ return;
+ }
+ }
+
+ /*
+ * Announce the HopIDs to the host as well in case user
+ * configured them.
+ */
+ tbstream_coupling_side_update_properties(side, side->in_hopid_alloc,
+ side->out_hopid_alloc);
+
+ if (tbstream_coupling_enable(coupling))
+ pr_warn("%s/%s: failed to establish coupling\n", pair, name);
+}
+
+/* tbstream_group.cg_subsys->su_mutex must be held */
+static bool tbstream_dev_service_has_name(const char *svc_name,
+ const char *name)
+{
+ struct tbstream_group *sg;
+ struct config_item *item;
+ bool found = false;
+
+ item = config_group_find_item(&tbstream_group, svc_name);
+ if (!item)
+ return false;
+
+ sg = to_tbstream_group(to_config_group(item));
+ scoped_guard(mutex, &sg->lock) {
+ struct tbstream_dev *sdev;
+
+ list_for_each_entry(sdev, &sg->dev_list, list) {
+ struct config_item *sitem = &sdev->group.cg_item;
+
+ if (!strcmp(config_item_name(sitem), name)) {
+ found = true;
+ break;
+ }
+ }
+ }
+
+ config_item_put(item);
+ return found;
+}
+
+/* tbstream_coupling_lock must be held */
+static bool
+tbstream_coupling_host_has_name(const struct tbstream_coupling_group *cg,
+ const struct tbstream_coupling_host *host,
+ const char *name)
+{
+ return tbstream_coupling_service_has_name(host->name, name, cg) ||
+ tbstream_dev_service_has_name(host->name, name);
+}
+
+/* tbstream_coupling_lock must be held */
+static bool
+tbstream_coupling_name_taken(const struct tbstream_coupling_group *cg,
+ const char *name)
+{
+ return tbstream_coupling_host_has_name(cg, &cg->host_a, name) ||
+ tbstream_coupling_host_has_name(cg, &cg->host_b, name);
+}
+
+static void tbstream_coupling_init_side(struct tbstream_coupling *coupling,
+ struct tbstream_coupling_side *side,
+ const struct tbstream_coupling_host *host)
+{
+ side->coupling = coupling;
+ side->host = host;
+ config_group_init_type_name(&side->group, host->name,
+ &tbstream_coupling_side_type);
+ configfs_add_default_group(&side->group, &coupling->group);
+}
+
+static struct config_group *
+tbstream_coupling_make_group(struct config_group *group, const char *name)
+{
+ struct tbstream_coupling_group *cg = to_tbstream_coupling_group(group);
+ struct tbstream_coupling *coupling;
+
+ /* The name is used as a property directory name as well */
+ if (strlen(name) > TB_PROPERTY_KEY_SIZE)
+ return ERR_PTR(-ENAMETOOLONG);
+
+ guard(mutex)(&tbstream_coupling_lock);
+
+ if (tbstream_coupling_name_taken(cg, name))
+ return ERR_PTR(-EEXIST);
+
+ coupling = kzalloc_obj(*coupling, GFP_KERNEL);
+ if (!coupling)
+ return ERR_PTR(-ENOMEM);
+
+ coupling->cg = cg;
+ INIT_LIST_HEAD(&coupling->list);
+ config_group_init_type_name(&coupling->group, name,
+ &tbstream_coupling_type);
+
+ /*
+ * Both sides are always present as sub-directories named after
+ * the services they belong to.
+ */
+ tbstream_coupling_init_side(coupling, &coupling->side_a, &cg->host_a);
+ tbstream_coupling_init_side(coupling, &coupling->side_b, &cg->host_b);
+
+ scoped_guard(mutex, &cg->lock) {
+ list_add_tail(&coupling->list, &cg->coupling_list);
+ /*
+ * If the host is already connected see if we can attach
+ * coupling to it now.
+ */
+ if (cg->host_a.stream)
+ tbstream_coupling_attach_side(&coupling->side_a);
+ if (cg->host_b.stream)
+ tbstream_coupling_attach_side(&coupling->side_b);
+ }
+
+ return &coupling->group;
+}
+
+static void tbstream_coupling_detach(struct tbstream_coupling *coupling)
+{
+ tbstream_coupling_disable(coupling);
+ tbstream_coupling_side_release_hopids(&coupling->side_b);
+ tbstream_coupling_side_release_hopids(&coupling->side_a);
+}
+
+static void tbstream_coupling_drop_item(struct config_group *group,
+ struct config_item *item)
+{
+ struct tbstream_coupling_group *cg = to_tbstream_coupling_group(group);
+ struct config_group *g = to_config_group(item);
+ struct tbstream_coupling *coupling =
+ container_of(g, struct tbstream_coupling, group);
+
+ scoped_guard(mutex, &cg->lock) {
+ tbstream_coupling_detach(coupling);
+ list_del(&coupling->list);
+ }
+
+ config_item_put(item);
+}
+
+static struct configfs_group_operations tbstream_coupling_group_ops = {
+ .make_group = tbstream_coupling_make_group,
+ .drop_item = tbstream_coupling_drop_item,
+};
+
+static void tbstream_coupling_group_item_release(struct config_item *item)
+{
+ struct tbstream_coupling_group *cg =
+ to_tbstream_coupling_group(to_config_group(item));
+
+ tbstream_put(cg->host_b.stream);
+ tbstream_put(cg->host_a.stream);
+ kfree(cg->host_b.name);
+ kfree(cg->host_a.name);
+ kfree(cg);
+}
+
+static struct configfs_item_operations tbstream_coupling_group_item_ops = {
+ .release = tbstream_coupling_group_item_release,
+};
+
+static const struct config_item_type tbstream_coupling_group_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_group_ops = &tbstream_coupling_group_ops,
+ .ct_item_ops = &tbstream_coupling_group_item_ops,
+};
+
+/*
+ * The coupling group name is "<service>_<service>", for example
+ * "0-1.0_0-3.0". Splits it into the names of @cg->host_a and
+ * @cg->host_b and validates both halves.
+ */
+static int tbstream_coupling_split_name(struct tbstream_coupling_group *cg,
+ const char *name)
+{
+ unsigned int domain_a, domain_b;
+ const char *sep;
+
+ sep = strchr(name, '_');
+ if (!sep)
+ return -EINVAL;
+
+ cg->host_a.name = kmemdup_nul(name, sep - name, GFP_KERNEL);
+ cg->host_b.name = kstrdup(sep + 1, GFP_KERNEL);
+ if (!cg->host_a.name || !cg->host_b.name)
+ return -ENOMEM;
+
+ if (!tbstream_valid_service_name(cg->host_a.name, &domain_a) ||
+ !tbstream_valid_service_name(cg->host_b.name, &domain_b))
+ return -EINVAL;
+
+ /* A host cannot be coupled to itself */
+ if (!strcmp(cg->host_a.name, cg->host_b.name))
+ return -EINVAL;
+
+ /* The path is programmed through this domain so both need to be here */
+ if (domain_a != domain_b)
+ return -EINVAL;
+
+ return 0;
+}
+
+/* tbstream_coupling_lock must be held */
+static bool
+tbstream_coupling_pair_exists(const struct tbstream_coupling_group *cg)
+{
+ const struct tbstream_coupling_group *tmp;
+
+ list_for_each_entry(tmp, &tbstream_coupling_list, list) {
+ if (!strcmp(tmp->host_a.name, cg->host_b.name) &&
+ !strcmp(tmp->host_b.name, cg->host_a.name))
+ return true;
+ }
+
+ return false;
+}
+
+static struct tbstream_coupling_host *
+tbstream_coupling_find_host(struct tbstream_coupling_group *cg,
+ const char *svc_name)
+{
+ if (sysfs_streq(cg->host_a.name, svc_name))
+ return &cg->host_a;
+ if (sysfs_streq(cg->host_b.name, svc_name))
+ return &cg->host_b;
+ return NULL;
+}
+
+static int tbstream_coupling_group_add(struct tbstream_coupling_group *cg,
+ const char *name)
+{
+ int ret;
+
+ ret = tbstream_coupling_split_name(cg, name);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&tbstream_coupling_lock);
+
+ /* The same two services can be paired only once */
+ if (tbstream_coupling_pair_exists(cg))
+ return -EEXIST;
+
+ /* Attach the streams that are already around */
+ scoped_guard(mutex, &tbstream_lock) {
+ struct tbstream *stream;
+
+ list_for_each_entry(stream, &tbstream_list, list) {
+ struct tbstream_coupling_host *host;
+
+ host = tbstream_coupling_find_host(cg,
+ dev_name(&stream->svc->dev));
+ if (host && !host->stream)
+ host->stream = tbstream_get(stream);
+ }
+ }
+
+ config_group_init_type_name(&cg->group, name,
+ &tbstream_coupling_group_type);
+ list_add_tail(&cg->list, &tbstream_coupling_list);
+ return 0;
+}
+
+static struct config_group *
+tbstream_coupling_group_make_group(struct config_group *group, const char *name)
+{
+ struct tbstream_coupling_group *cg;
+ int ret;
+
+ cg = kzalloc_obj(*cg, GFP_KERNEL);
+ if (!cg)
+ return ERR_PTR(-ENOMEM);
+
+ mutex_init(&cg->lock);
+ INIT_LIST_HEAD(&cg->coupling_list);
+ INIT_LIST_HEAD(&cg->list);
+
+ ret = tbstream_coupling_group_add(cg, name);
+ if (ret) {
+ kfree(cg->host_b.name);
+ kfree(cg->host_a.name);
+ kfree(cg);
+ return ERR_PTR(ret);
+ }
+
+ return &cg->group;
+}
+
+static void tbstream_coupling_group_drop_item(struct config_group *group,
+ struct config_item *item)
+{
+ struct tbstream_coupling_group *cg =
+ to_tbstream_coupling_group(to_config_group(item));
+
+ scoped_guard(mutex, &tbstream_coupling_lock)
+ list_del(&cg->list);
+
+ config_item_put(item);
+}
+
+static struct configfs_group_operations tbstream_coupling_dir_ops = {
+ .make_group = tbstream_coupling_group_make_group,
+ .drop_item = tbstream_coupling_group_drop_item,
+};
+
+static const struct config_item_type tbstream_coupling_dir_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_group_ops = &tbstream_coupling_dir_ops,
+};
+
+static struct config_group tbstream_coupling_dir;
+
+static struct tbstream_coupling_side *
+tbstream_coupling_side(struct tbstream_coupling *coupling,
+ const struct tbstream_coupling_host *host)
+{
+ if (host == &coupling->cg->host_a)
+ return &coupling->side_a;
+ return &coupling->side_b;
+}
+
+/* tbstream_coupling_lock must be held */
+static void tbstream_coupling_attach_stream(struct tbstream *stream)
+{
+ const char *name = dev_name(&stream->svc->dev);
+ struct tbstream_coupling_group *cg;
+
+ list_for_each_entry(cg, &tbstream_coupling_list, list) {
+ struct tbstream_coupling_host *host;
+ struct tbstream_coupling *coupling;
+
+ guard(mutex)(&cg->lock);
+
+ host = tbstream_coupling_find_host(cg, name);
+ if (!host)
+ continue;
+ if (WARN_ON(host->stream))
+ continue;
+
+ host->stream = tbstream_get(stream);
+
+ list_for_each_entry(coupling, &cg->coupling_list, list) {
+ struct tbstream_coupling_side *side;
+
+ side = tbstream_coupling_side(coupling, host);
+ tbstream_coupling_attach_side(side);
+ }
+ }
+}
+
+/* cg->lock must be held */
+static void
+tbstream_coupling_group_detach_host(struct tbstream_coupling_group *cg,
+ struct tbstream_coupling_host *host)
+{
+ struct tbstream_coupling *coupling;
+
+ list_for_each_entry_reverse(coupling, &cg->coupling_list, list) {
+ struct tbstream_coupling_side *side;
+
+ tbstream_coupling_disable(coupling);
+ /* Release the ones for the host that is gone */
+ side = tbstream_coupling_side(coupling, host);
+ tbstream_coupling_side_release_hopids(side);
+ }
+
+ tbstream_put(host->stream);
+ host->stream = NULL;
+}
+
+/* tbstream_coupling_lock must be held */
+static void tbstream_coupling_detach_stream(struct tbstream *stream)
+{
+ struct tbstream_coupling_group *cg;
+
+ list_for_each_entry(cg, &tbstream_coupling_list, list) {
+ guard(mutex)(&cg->lock);
+
+ if (cg->host_a.stream == stream)
+ tbstream_coupling_group_detach_host(cg, &cg->host_a);
+ else if (cg->host_b.stream == stream)
+ tbstream_coupling_group_detach_host(cg, &cg->host_b);
+ }
+}
+
static void tbstream_group_attach_stream(struct tbstream *stream)
{
struct tbstream_group *sg;
@@ -1898,9 +2757,11 @@ static int tbstream_probe(struct tb_service *svc)
stream->svc = tb_service_get(svc);
INIT_LIST_HEAD(&stream->list);
- scoped_guard(mutex, &tbstream_lock)
- list_add_tail(&stream->list, &tbstream_list);
-
+ scoped_guard(mutex, &tbstream_coupling_lock) {
+ scoped_guard(mutex, &tbstream_lock)
+ list_add_tail(&stream->list, &tbstream_list);
+ tbstream_coupling_attach_stream(stream);
+ }
tbstream_group_attach_stream(stream);
tb_service_set_drvdata(svc, stream);
return 0;
@@ -1910,9 +2771,12 @@ static void tbstream_remove(struct tb_service *svc)
{
struct tbstream *stream = tb_service_get_drvdata(svc);
+ scoped_guard(mutex, &tbstream_coupling_lock) {
+ tbstream_coupling_detach_stream(stream);
+ scoped_guard(mutex, &tbstream_lock)
+ list_del(&stream->list);
+ }
tbstream_group_detach_stream(stream);
- scoped_guard(mutex, &tbstream_lock)
- list_del(&stream->list);
tbstream_put(stream);
}
@@ -2008,6 +2872,10 @@ static int __init tbstream_init(void)
goto err_free_dir;
config_group_init(&tbstream_group);
+ config_group_init_type_name(&tbstream_coupling_dir, "coupling",
+ &tbstream_coupling_dir_type);
+ configfs_add_default_group(&tbstream_coupling_dir, &tbstream_group);
+
ret = tb_configfs_register_group(&tbstream_group);
if (ret)
goto err_unregister_dir;
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 9/9] Documentation: thunderbolt: Document USB4STREAM coupling
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
` (7 preceding siblings ...)
2026-11-25 15:26 ` [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling Mika Westerberg
@ 2026-11-25 15:26 ` Mika Westerberg
8 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2026-11-25 15:26 UTC (permalink / raw)
To: linux-usb
Cc: Yehezkel Bernat, Lukas Wunner, Andreas Noever, Mika Westerberg,
Mika Westerberg
From: "Mika Westerberg (Intel)" <westeri@kernel.org>
This adds a section to the admin guide describing how USB4STREAM
coupling could be used.
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
Documentation/admin-guide/thunderbolt.rst | 119 ++++++++++++++++++++++
1 file changed, 119 insertions(+)
diff --git a/Documentation/admin-guide/thunderbolt.rst b/Documentation/admin-guide/thunderbolt.rst
index ff25fe853706..3b71feaa1efa 100644
--- a/Documentation/admin-guide/thunderbolt.rst
+++ b/Documentation/admin-guide/thunderbolt.rst
@@ -434,6 +434,125 @@ Then you have ``/dev/tbstream0`` for ``control`` and ``/dev/tbstream1``
for ``data``. Before you can use them you need to configure them as
shown above for the one stream case.
+Coupling a stream between two other hosts
+-----------------------------------------
+If a host is connected to two other hosts, say
+``host1 <-> host2 <-> host3`` where ``host2`` is the one running Linux,
+then ``host2`` can forward streams between ``host1`` and ``host3``
+through the routers of its domain. The two hosts do not need to be
+connected directly to ``host2``, there can be device routers such as
+USB4 hubs in between.
+
+This can already be done in userspace like::
+
+ host2 # dd if=/dev/tbstream0 of=/dev/tbstream1
+
+but that copies every frame through the memory of ``host2`` and needs
+another process for the other direction.
+
+With coupling the frames go through a tunnel directly between ``host1``
+and ``host3`` through the shortest path so there is no need for a
+separate application that copies the frames. Coupling only works with
+the software connection manager as ``host2``.
+
+Start from ``host2``, where ``host1`` is ``0-1.0`` and ``host3`` is
+``0-3.0``. The coupling is created under ``coupling`` in a directory
+named after the two services, and it has a sub-directory for each of
+them::
+
+ host2 # cd /sys/kernel/config/thunderbolt/stream/coupling
+ host2 # mkdir -p 0-1.0_0-3.0/data
+ host2 # cd 0-1.0_0-3.0/data
+ host2 # echo -1 > 0-1.0/in_hopid
+ host2 # echo -1 > 0-1.0/out_hopid
+ host2 # echo -1 > 0-3.0/in_hopid
+ host2 # echo -1 > 0-3.0/out_hopid
+
+Like elsewhere in the driver ``in_hopid`` is the HopID the host on that
+side sends with and ``out_hopid`` the one used when sending towards it.
+Using ``-1`` picks the next available one::
+
+ host2 # grep -H . 0-1.0/*_hopid 0-3.0/*_hopid
+ 0-1.0/in_hopid:8
+ 0-1.0/out_hopid:8
+ 0-3.0/in_hopid:8
+ 0-3.0/out_hopid:8
+
+HopIDs are allocated per adapter and per direction, so each adapter has
+one for the packets coming in and another for the ones going out (more
+details can be found from USB4 spec). In the above example there were no
+other tunnels through the lane adapters so the first available HopID in
+either direction is ``8``.
+
+Each host announces its streams to the other side of the link which
+allows performing auto-configuration on that side using the same name
+for the stream.
+
+Now ``host1`` and ``host3`` only need the stream created because the
+HopIDs are picked up from what ``host2`` announces, reversed so that the
+``in_hopid`` of one end matches the ``out_hopid`` of the other::
+
+ host1 # cd /sys/kernel/config/thunderbolt/stream
+ host1 # mkdir -p 0-1.0/data
+
+ host3 # cd /sys/kernel/config/thunderbolt/stream
+ host3 # mkdir -p 0-1.0/data
+
+Note that ``host2`` is ``0-1.0`` for both of them, the service names are
+relative to the domain of each host. The other way around works just as
+well: if the streams on ``host1`` and ``host3`` are configured first,
+creating the two directories on ``host2`` is enough and the HopIDs of
+both of its sides are picked up from them. It is also possible to
+configure HopIDs manually.
+
+There is no ``/dev/tbstreamX`` node on ``host2`` because the frames do
+not go through it. Once all four HopIDs are in place and both hosts are
+connected the tunnel is established and ``host1`` and ``host3`` can talk
+to each other as if there was a single cable between them. It can be
+inspected with ``tbtunnels`` from tbtools.
+
+The tunnel only covers the routers that are actually between the two
+hosts and it turns at the closest common router of the two, which is not
+necessarily the one of ``host2``. If both hosts are connected to the
+same hub below ``host2``::
+
+ [host2]
+ |
+ [Hub]
+ / \
+ [host1] [host3]
+
+then the tunnel goes only through the hub, not involving the host router
+on ``host2`` at all. ``host2`` still needs to set it up because it
+is the connection manager of that domain.
+
+After this the ``/dev/tbstreamX`` on ``host1`` and ``host3`` can be used
+like a regular stream described in the previous section, with the
+difference explained below. The hosts ``host1`` and ``host3`` do not
+need to support coupling themselves and can be even using firmware
+connection manager.
+
+Each hop of a path keeps track of the credits it has sent and received,
+and this state is only reset when the path is set up (see the USB4 spec
+for details). For a coupled stream the path is split between three
+hosts: ``host1`` and ``host3`` set up their part when the stream is
+opened and tear it down when it is closed, while the part ``host2`` set
+up stays in place. It is therefore recommended that the whole tunnel is
+torn down and recreated before a new transfer takes place::
+
+ host2 # cd /sys/kernel/config/thunderbolt/stream/coupling
+ host2 # rmdir 0-1.0_0-3.0/data
+ host2 # mkdir 0-1.0_0-3.0/data
+
+This assumes both sides announce their HopIDs so the coupling can be
+re-created based on those. Otherwise HopIDs need to be filled in as
+well.
+
+The coupling stays until it is removed as below::
+
+ host2 # cd /sys/kernel/config/thunderbolt/stream/coupling
+ host2 # rmdir -p 0-1.0_0-3.0/data
+
Forcing power
-------------
Many OEMs include a method that can be used to force the power of a
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-25 15:41 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
2026-11-25 15:26 ` [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed Mika Westerberg
2026-11-25 15:26 ` [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs Mika Westerberg
2026-11-25 15:26 ` [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly Mika Westerberg
2026-11-25 15:26 ` [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters Mika Westerberg
2026-11-25 15:26 ` [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels Mika Westerberg
2026-11-25 15:26 ` [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths() Mika Westerberg
2026-11-25 15:26 ` [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 9/9] Documentation: thunderbolt: Document " Mika Westerberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox