Skip to contentSkip to conversation

thunderbolt: Offline on-board retimer NVM upgrade support

PATCH In review Mika Westerberg 3 participants 16 messages May 19, 2021 – Jun 1, 2021

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

cover thunderbolt: Offline on-board retimer NVM upgrade support

Hi all,

USB4 retimers are only accessible when the USB4 is up. However, sometimes it may be useful to be able to upgrade on-board retimers even if the link is not up. For instance if the user simply does not have any USB4 devices.

Making retimers accessible in "offline" mode requires some help from the platform firmware (ACPI in our case) to turn on power to the retimers and cycle them through different modes to get the sideband link up. This may also involve other firmwares such as Embedded Controller (as it is the case with recent Chromebooks).

This series adds support for "offline" retimer NVM upgrade so that it first exposes each USB4 port to the userspace. If the platform firmware provides a special _DSM-method (Device Specific Method) under the USB4 port ACPI description, we expose two attributes under the port that the userspace can use to put the port to offline mode and rescan for the retimers. Otherwise the NVM upgrade works the same way than with the online mode. We also add documentation to the admin-guide how this can be done.

In addition to this, at least Intel USB4 devices (and retimers) allow running NVM authenticate (upgrade) separately from write so we make it possible for the userspace to run the write and authenticate in two steps. This allows userspace to trigger the authentication at later time, like when the user logs out.

Mika Westerberg (4):
thunderbolt: Log the link as TBT instead of TBT3
thunderbolt: Add USB4 port devices
thunderbolt: Allow router NVM authenticate separately
thunderbolt: Check for NVM authentication status after the operation started

Rajmohan Mani (5):
thunderbolt: Add support for ACPI _DSM to power on/off retimers
thunderbolt: Add additional USB4 port operations for retimer access
thunderbolt: Add support for retimer NVM upgrade when there is no link
thunderbolt: Move nvm_write_ops to tb.h
thunderbolt: Add WRITE_ONLY and AUTHENTICATE_ONLY NVM operations for retimers

 .../ABI/testing/sysfs-bus-thunderbolt         |  38 ++-
 Documentation/admin-guide/thunderbolt.rst     |  29 ++
 drivers/thunderbolt/Makefile                  |   2 +-
 drivers/thunderbolt/acpi.c                    | 206 +++++++++++++
 drivers/thunderbolt/domain.c                  |   9 +-
 drivers/thunderbolt/retimer.c                 | 108 +++++--
 drivers/thunderbolt/sb_regs.h                 |   2 +
 drivers/thunderbolt/switch.c                  | 120 +++++---
 drivers/thunderbolt/tb.c                      |   4 +-
 drivers/thunderbolt/tb.h                      |  60 +++-
 drivers/thunderbolt/usb4.c                    | 153 +++++++++-
 drivers/thunderbolt/usb4_port.c               | 281 ++++++++++++++++++
 12 files changed, 930 insertions(+), 82 deletions(-)
 create mode 100644 drivers/thunderbolt/usb4_port.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/9 thunderbolt: Log the link as TBT instead of TBT3

The upstream port can be connected to any previous generation Thunderbolt port so logging as "TBT" is more accurate than "TBT3.

No functional changes.

---
 drivers/thunderbolt/usb4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thunderbolt/usb4.c +1 -1
index edab8ea63c0b..94cc25cc6388 100644
@@ -260,7 +260,7 @@ int usb4_switch_setup(struct tb_switch *sw)
 	parent = tb_switch_parent(sw);
 	downstream_port = tb_port_at(tb_route(sw), parent);
 	sw->link_usb4 = link_is_usb4(downstream_port);
-	tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3");
+	tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
 
 	xhci = val & ROUTER_CS_6_HCI;
 	tbt3 = !(val & ROUTER_CS_6_TNS);
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/9 thunderbolt: Add USB4 port devices

In reply to Mika Westerberg

Create devices for each USB4 port. This is needed when we add retimer access when there is no device connected but may be useful for other purposes too following what USB subsystem does. This exports a single attribute "link" that shows the type of the USB4 link (or "none" if there is no cable connected).

---
 .../ABI/testing/sysfs-bus-thunderbolt         |   7 ++
 drivers/thunderbolt/Makefile                  |   2 +-
 drivers/thunderbolt/retimer.c                 |  15 ++-
 drivers/thunderbolt/switch.c                  |  17 ++-
 drivers/thunderbolt/tb.h                      |  30 +++++
 drivers/thunderbolt/usb4.c                    |  54 +++++++++
 drivers/thunderbolt/usb4_port.c               | 112 ++++++++++++++++++
 7 files changed, 229 insertions(+), 8 deletions(-)
 create mode 100644 drivers/thunderbolt/usb4_port.c
Documentation/ABI/testing/sysfs-bus-thunderbolt +7 -0
index 05afeee05538..3537ba1ba892 100644
@@ -290,6 +290,13 @@ Contact:	[email protected]
 Description:	This contains XDomain service specific settings as
 		bitmask. Format: %x
 
+What:		/sys/bus/thunderbolt/devices/usb4_portX/link
+Date:		Sep 2021
+KernelVersion:	v5.14
+Contact:	Mika Westerberg <[email protected]>
+Description:	Returns the current link mode. Possible values are
+		"usb4", "tbt" and "none".
+
 What:		/sys/bus/thunderbolt/devices/<device>:<port>.<index>/device
 Date:		Oct 2020
 KernelVersion:	v5.9
drivers/thunderbolt/Makefile +1 -1
index 7aa48f6c41d9..da19d7987d00 100644
@@ -2,7 +2,7 @@
 obj-${CONFIG_USB4} := thunderbolt.o
 thunderbolt-objs := nhi.o nhi_ops.o ctl.o tb.o switch.o cap.o path.o tunnel.o eeprom.o
 thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o lc.o tmu.o usb4.o
-thunderbolt-objs += nvm.o retimer.o quirks.o
+thunderbolt-objs += usb4_port.o nvm.o retimer.o quirks.o
 
 thunderbolt-${CONFIG_ACPI} += acpi.o
 thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
drivers/thunderbolt/retimer.c +10 -5
index c44fad2b9fbb..2e5188fb1150 100644
@@ -283,11 +283,13 @@ struct device_type tb_retimer_type = {
 
 static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
 {
+	struct usb4_port *usb4;
 	struct tb_retimer *rt;
 	u32 vendor, device;
 	int ret;
 
-	if (!port->cap_usb4)
+	usb4 = port->usb4;
+	if (!usb4)
 		return -EINVAL;
 
 	ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
@@ -331,7 +333,7 @@ static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
 	rt->port = port;
 	rt->tb = port->sw->tb;
 
-	rt->dev.parent = &port->sw->dev;
+	rt->dev.parent = &usb4->dev;
 	rt->dev.bus = &tb_bus_type;
 	rt->dev.type = &tb_retimer_type;
 	dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
@@ -389,7 +391,7 @@ static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
 	struct tb_retimer_lookup lookup = { .port = port, .index = index };
 	struct device *dev;
 
-	dev = device_find_child(&port->sw->dev, &lookup, retimer_match);
+	dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
 	if (dev)
 		return tb_to_retimer(dev);
 
@@ -479,7 +481,10 @@ static int remove_retimer(struct device *dev, void *data)
  */
 void tb_retimer_remove_all(struct tb_port *port)
 {
-	if (port->cap_usb4)
-		device_for_each_child_reverse(&port->sw->dev, port,
+	struct usb4_port *usb4;
+
+	usb4 = port->usb4;
+	if (usb4)
+		device_for_each_child_reverse(&usb4->dev, port,
 					      remove_retimer);
 }
drivers/thunderbolt/switch.c +15 -2
index e015dc93a916..7303c61a891a 100644
@@ -2741,11 +2741,16 @@ int tb_switch_add(struct tb_switch *sw)
 				 sw->device_name);
 	}
 
+	ret = usb4_switch_add_ports(sw);
+	if (ret) {
+		dev_err(&sw->dev, "failed to add USB4 ports\n");
+		goto err_del;
+	}
+
 	ret = tb_switch_nvm_add(sw);
 	if (ret) {
 		dev_err(&sw->dev, "failed to add NVM devices\n");
-		device_del(&sw->dev);
-		return ret;
+		goto err_ports;
 	}
 
 	/*
@@ -2766,6 +2771,13 @@ int tb_switch_add(struct tb_switch *sw)
 
 	tb_switch_debugfs_init(sw);
 	return 0;
+
+err_ports:
+	usb4_switch_remove_ports(sw);
+err_del:
+	device_del(&sw->dev);
+
+	return ret;
 }
 
 /**
@@ -2805,6 +2817,7 @@ void tb_switch_remove(struct tb_switch *sw)
 		tb_plug_events_active(sw, false);
 
 	tb_switch_nvm_remove(sw);
+	usb4_switch_remove_ports(sw);
 
 	if (tb_route(sw))
 		dev_info(&sw->dev, "device disconnected\n");
drivers/thunderbolt/tb.h +30 -0
index 89e38aeea52b..948e7601428f 100644
@@ -202,6 +202,7 @@ struct tb_switch {
  * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
  * @cap_adap: Offset of the adapter specific capability (%0 if not present)
  * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
+ * @usb4: Pointer to the USB4 port structure (only if @cap_usb4 is != %0)
  * @port: Port number on switch
  * @disabled: Disabled by eeprom or enabled but not implemented
  * @bonded: true if the port is bonded (two lanes combined as one)
@@ -228,6 +229,7 @@ struct tb_port {
 	int cap_tmu;
 	int cap_adap;
 	int cap_usb4;
+	struct usb4_port *usb4;
 	u8 port;
 	bool disabled;
 	bool bonded;
@@ -241,6 +243,16 @@ struct tb_port {
 	unsigned int dma_credits;
 };
 
+/**
+ * struct usb4_port - USB4 port device
+ * @dev: Device for the port
+ * @port: Pointer to the lane 0 adapter
+ */
+struct usb4_port {
+	struct device dev;
+	struct tb_port *port;
+};
+
 /**
  * tb_retimer: Thunderbolt retimer
  * @dev: Device for the retimer
@@ -645,6 +657,7 @@ struct tb *tb_probe(struct tb_nhi *nhi);
 extern struct device_type tb_domain_type;
 extern struct device_type tb_retimer_type;
 extern struct device_type tb_switch_type;
+extern struct device_type usb4_port_device_type;
 
 int tb_domain_init(void);
 void tb_domain_exit(void);
@@ -1038,6 +1051,8 @@ struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
 					  const struct tb_port *port);
 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
 					  const struct tb_port *port);
+int usb4_switch_add_ports(struct tb_switch *sw);
+void usb4_switch_remove_ports(struct tb_switch *sw);
 
 int usb4_port_unlock(struct tb_port *port);
 int usb4_port_configure(struct tb_port *port);
@@ -1070,6 +1085,21 @@ int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
 				     int *downstream_bw);
 
+static inline bool tb_is_usb4_port_device(const struct device *dev)
+{
+	return dev->type == &usb4_port_device_type;
+}
+
+static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
+{
+	if (tb_is_usb4_port_device(dev))
+		return container_of(dev, struct usb4_port, dev);
+	return NULL;
+}
+
+struct usb4_port *usb4_port_device_add(struct tb_port *port);
+void usb4_port_device_remove(struct usb4_port *usb4);
+
 /* Keep link controller awake during update */
 #define QUIRK_FORCE_POWER_LINK_CONTROLLER		BIT(0)
 
drivers/thunderbolt/usb4.c +54 -0

60 lines not on this page · raw message

drivers/thunderbolt/usb4_port.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH 2/9] thunderbolt: Add USB4 port devices

Quoted 5 lines · Mika Westerberg

Create devices for each USB4 port. This is needed when we add retimer access when there is no device connected but may be useful for other purposes too following what USB subsystem does. This exports a single attribute "link" that shows the type of the USB4 link (or "none" if there is no cable connected).

<snip>

Quoted 52 lines
drivers/thunderbolt/usb4_port.c new file +52 -0
+/*
+ * USB4 port device
+ *
+ * Copyright (C) 2021, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "tb.h"
+
+static ssize_t link_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
+	struct tb_port *port = usb4->port;
+	struct tb *tb = port->sw->tb;
+	const char *link;
+
+	if (mutex_lock_interruptible(&tb->lock))
+		return -ERESTARTSYS;
+
+	if (tb_is_upstream_port(port))
+		link = port->sw->link_usb4 ? "usb4" : "tbt";
+	else if (tb_port_has_remote(port))
+		link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
+	else
+		link = "none";
+
+	mutex_unlock(&tb->lock);
+
+	return sysfs_emit(buf, "%s\n", link);
+}
+static DEVICE_ATTR_RO(link);
+
+static struct attribute *common_attrs[] = {
+	&dev_attr_link.attr,
+	NULL
+};
+
+static const struct attribute_group common_group = {
+	.attrs = common_attrs,
+};
+
+static const struct attribute_group *usb4_port_device_groups[] = {
+	&common_group,
+	NULL
+};
+
+static void usb4_port_device_release(struct device *dev)
+{

drivers/thunderbolt/usb4_port.c line 63

Quoted 10 lines
drivers/thunderbolt/usb4_port.c new file +10 -0
+	struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
+
+	kfree(usb4);
+}
+
+struct device_type usb4_port_device_type = {
+	.name = "usb4_port",
+	.groups = usb4_port_device_groups,
+	.release = usb4_port_device_release,
+};

I noticed that in the next patch you add acpi_bus_type for these ports, but is that really necessary? Why not just:

int usb4_port_fwnode_match(struct tb_port *port, struct fwnode_handle *fwnode)

{
        if (is_acpi_device_node(fwnode))
                return acpi_device_adr(to_acpi_device_node(fwnode)) == port->port;

        return 0;
}

drivers/thunderbolt/usb4_port.c line 73

Quoted 9 lines
drivers/thunderbolt/usb4_port.c new file +9 -0
+/**
+ * usb4_port_device_add() - Add USB4 port device
+ * @port: Lane 0 adapter port to add the USB4 port
+ *
+ * Creates and registers a USB4 port device for @port. Returns the new
+ * USB4 port device pointer or ERR_PTR() in case of error.
+ */
+struct usb4_port *usb4_port_device_add(struct tb_port *port)
+{
        struct fwnode_handle *child;

drivers/thunderbolt/usb4_port.c line 84

Quoted 11 lines
drivers/thunderbolt/usb4_port.c new file +11 -0
+	struct usb4_port *usb4;
+	int ret;
+
+	usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
+	if (!usb4)
+		return ERR_PTR(-ENOMEM);
+
+	usb4->port = port;
+	usb4->dev.type = &usb4_port_device_type;
+	usb4->dev.parent = &port->sw->dev;
+	dev_set_name(&usb4->dev, "usb4_port%d", port->port);

and then here something like this (feel free to improve this part):

        device_for_each_child_node(&port->sw->dev, child) {
                if (usb4_port_fwnode_match(port, child)) {
                        usb4->dev.fwnode = child;
                        break;
                }
        }

Or maybe I'm missing something?

Quoted 5 lines
drivers/thunderbolt/usb4_port.c new file +5 -0
+	ret = device_register(&usb4->dev);
+	if (ret) {
+		put_device(&usb4->dev);
+		return ERR_PTR(ret);
+	}

drivers/thunderbolt/usb4_port.c line 100

Quoted 10 lines
drivers/thunderbolt/usb4_port.c new file +10 -0
+
+	pm_runtime_no_callbacks(&usb4->dev);
+	pm_runtime_set_active(&usb4->dev);
+	pm_runtime_enable(&usb4->dev);
+	pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
+	pm_runtime_mark_last_busy(&usb4->dev);
+	pm_runtime_use_autosuspend(&usb4->dev);
+
+	return usb4;
+}

thanks,

Signature · 1 line
heikki
#4 lore raw

Re: [PATCH 2/9] thunderbolt: Add USB4 port devices

Quoted 109 lines · Heikki Krogerus
Quoted 5 lines · Mika Westerberg

Create devices for each USB4 port. This is needed when we add retimer access when there is no device connected but may be useful for other purposes too following what USB subsystem does. This exports a single attribute "link" that shows the type of the USB4 link (or "none" if there is no cable connected).

<snip>

Quoted 52 lines
drivers/thunderbolt/usb4_port.c new file +52 -0
+/*
+ * USB4 port device
+ *
+ * Copyright (C) 2021, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "tb.h"
+
+static ssize_t link_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
+	struct tb_port *port = usb4->port;
+	struct tb *tb = port->sw->tb;
+	const char *link;
+
+	if (mutex_lock_interruptible(&tb->lock))
+		return -ERESTARTSYS;
+
+	if (tb_is_upstream_port(port))
+		link = port->sw->link_usb4 ? "usb4" : "tbt";
+	else if (tb_port_has_remote(port))
+		link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
+	else
+		link = "none";
+
+	mutex_unlock(&tb->lock);
+
+	return sysfs_emit(buf, "%s\n", link);
+}
+static DEVICE_ATTR_RO(link);
+
+static struct attribute *common_attrs[] = {
+	&dev_attr_link.attr,
+	NULL
+};
+
+static const struct attribute_group common_group = {
+	.attrs = common_attrs,
+};
+
+static const struct attribute_group *usb4_port_device_groups[] = {
+	&common_group,
+	NULL
+};
+
+static void usb4_port_device_release(struct device *dev)
+{

drivers/thunderbolt/usb4_port.c line 63

Quoted 10 lines
drivers/thunderbolt/usb4_port.c new file +10 -0
+	struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
+
+	kfree(usb4);
+}
+
+struct device_type usb4_port_device_type = {
+	.name = "usb4_port",
+	.groups = usb4_port_device_groups,
+	.release = usb4_port_device_release,
+};

I noticed that in the next patch you add acpi_bus_type for these ports, but is that really necessary? Why not just:

int usb4_port_fwnode_match(struct tb_port *port, struct fwnode_handle *fwnode)

{
        if (is_acpi_device_node(fwnode))
                return acpi_device_adr(to_acpi_device_node(fwnode)) == port->port;

        return 0;
}

drivers/thunderbolt/usb4_port.c line 73

Quoted 9 lines
drivers/thunderbolt/usb4_port.c new file +9 -0
+/**
+ * usb4_port_device_add() - Add USB4 port device
+ * @port: Lane 0 adapter port to add the USB4 port
+ *
+ * Creates and registers a USB4 port device for @port. Returns the new
+ * USB4 port device pointer or ERR_PTR() in case of error.
+ */
+struct usb4_port *usb4_port_device_add(struct tb_port *port)
+{
        struct fwnode_handle *child;

drivers/thunderbolt/usb4_port.c line 84

Quoted 11 lines
drivers/thunderbolt/usb4_port.c new file +11 -0
+	struct usb4_port *usb4;
+	int ret;
+
+	usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
+	if (!usb4)
+		return ERR_PTR(-ENOMEM);
+
+	usb4->port = port;
+	usb4->dev.type = &usb4_port_device_type;
+	usb4->dev.parent = &port->sw->dev;
+	dev_set_name(&usb4->dev, "usb4_port%d", port->port);

and then here something like this (feel free to improve this part):

Quoted 8 lines
        device_for_each_child_node(&port->sw->dev, child) {
                if (usb4_port_fwnode_match(port, child)) {
                        usb4->dev.fwnode = child;
                        break;
                }
        }

Or maybe I'm missing something?

Ah, the node hierarchy is much more complex than I though.

Sorry for the noise.

thanks,

Signature · 1 line
heikki
#5 lore raw

Re: [PATCH 2/9] thunderbolt: Add USB4 port devices

In reply to Heikki Krogerus

Hi Heikki,

Quoted 75 lines · Heikki Krogerus
Quoted 5 lines · Mika Westerberg

Create devices for each USB4 port. This is needed when we add retimer access when there is no device connected but may be useful for other purposes too following what USB subsystem does. This exports a single attribute "link" that shows the type of the USB4 link (or "none" if there is no cable connected).

<snip>

Quoted 52 lines
drivers/thunderbolt/usb4_port.c new file +52 -0
+/*
+ * USB4 port device
+ *
+ * Copyright (C) 2021, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/pm_runtime.h>
+
+#include "tb.h"
+
+static ssize_t link_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
+	struct tb_port *port = usb4->port;
+	struct tb *tb = port->sw->tb;
+	const char *link;
+
+	if (mutex_lock_interruptible(&tb->lock))
+		return -ERESTARTSYS;
+
+	if (tb_is_upstream_port(port))
+		link = port->sw->link_usb4 ? "usb4" : "tbt";
+	else if (tb_port_has_remote(port))
+		link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
+	else
+		link = "none";
+
+	mutex_unlock(&tb->lock);
+
+	return sysfs_emit(buf, "%s\n", link);
+}
+static DEVICE_ATTR_RO(link);
+
+static struct attribute *common_attrs[] = {
+	&dev_attr_link.attr,
+	NULL
+};
+
+static const struct attribute_group common_group = {
+	.attrs = common_attrs,
+};
+
+static const struct attribute_group *usb4_port_device_groups[] = {
+	&common_group,
+	NULL
+};
+
+static void usb4_port_device_release(struct device *dev)
+{

drivers/thunderbolt/usb4_port.c line 63

Quoted 10 lines
drivers/thunderbolt/usb4_port.c new file +10 -0
+	struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
+
+	kfree(usb4);
+}
+
+struct device_type usb4_port_device_type = {
+	.name = "usb4_port",
+	.groups = usb4_port_device_groups,
+	.release = usb4_port_device_release,
+};

I noticed that in the next patch you add acpi_bus_type for these ports, but is that really necessary? Why not just:

Quoted 7 lines

int usb4_port_fwnode_match(struct tb_port *port, struct fwnode_handle *fwnode)

{
        if (is_acpi_device_node(fwnode))
                return acpi_device_adr(to_acpi_device_node(fwnode)) == port->port;

        return 0;
}

I have to say I might be missing some new additions to fwnode front but the acpi_bus_type is used to match the ACPI nodes to usb4_ports and also to routers. I see that this one may work for the former but not sure about the latter.

I guess we could do similar for routers too in switch.c.

However, I would like to keep ACPI specific code in acpi.c if possible but if this is the preferred way then no problem doing what you say :)

Quoted 55 lines

drivers/thunderbolt/usb4_port.c line 73

Quoted 9 lines
drivers/thunderbolt/usb4_port.c new file +9 -0
+/**
+ * usb4_port_device_add() - Add USB4 port device
+ * @port: Lane 0 adapter port to add the USB4 port
+ *
+ * Creates and registers a USB4 port device for @port. Returns the new
+ * USB4 port device pointer or ERR_PTR() in case of error.
+ */
+struct usb4_port *usb4_port_device_add(struct tb_port *port)
+{
        struct fwnode_handle *child;

drivers/thunderbolt/usb4_port.c line 84

Quoted 11 lines
drivers/thunderbolt/usb4_port.c new file +11 -0
+	struct usb4_port *usb4;
+	int ret;
+
+	usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
+	if (!usb4)
+		return ERR_PTR(-ENOMEM);
+
+	usb4->port = port;
+	usb4->dev.type = &usb4_port_device_type;
+	usb4->dev.parent = &port->sw->dev;
+	dev_set_name(&usb4->dev, "usb4_port%d", port->port);

and then here something like this (feel free to improve this part):

        device_for_each_child_node(&port->sw->dev, child) {
                if (usb4_port_fwnode_match(port, child)) {
                        usb4->dev.fwnode = child;
                        break;
                }
        }

Or maybe I'm missing something?

Quoted 5 lines
drivers/thunderbolt/usb4_port.c new file +5 -0
+	ret = device_register(&usb4->dev);
+	if (ret) {
+		put_device(&usb4->dev);
+		return ERR_PTR(ret);
+	}

drivers/thunderbolt/usb4_port.c line 100

Quoted 10 lines
drivers/thunderbolt/usb4_port.c new file +10 -0
+
+	pm_runtime_no_callbacks(&usb4->dev);
+	pm_runtime_set_active(&usb4->dev);
+	pm_runtime_enable(&usb4->dev);
+	pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
+	pm_runtime_mark_last_busy(&usb4->dev);
+	pm_runtime_use_autosuspend(&usb4->dev);
+
+	return usb4;
+}

thanks,

Signature · 1 line
heikki
#6 lore raw

Re: [PATCH 2/9] thunderbolt: Add USB4 port devices

Hi,

Quoted 8 lines · Mika Westerberg
Quoted 8 lines

I noticed that in the next patch you add acpi_bus_type for these ports, but is that really necessary? Why not just:

int usb4_port_fwnode_match(struct tb_port *port, struct fwnode_handle *fwnode)

{
        if (is_acpi_device_node(fwnode))
                return acpi_device_adr(to_acpi_device_node(fwnode)) == port->port;
Quoted 7 lines
Quoted 2 lines
        return 0;
}

I have to say I might be missing some new additions to fwnode front but the acpi_bus_type is used to match the ACPI nodes to usb4_ports and also to routers. I see that this one may work for the former but not sure about the latter.

Yeah, I noticed that afterwards.

Quoted 1 line

I guess we could do similar for routers too in switch.c.

Forget about it.

Quoted 2 lines

However, I would like to keep ACPI specific code in acpi.c if possible but if this is the preferred way then no problem doing what you say :)

Well, that's actually the thing. The matching is the only ACPI (or any firmware type) specific thing that we need when assigning the firmware nodes to the device. Note also that the above function works already even with CONFIG_ACPI=n. That's why I though that there is no reason not to just make this firmware node type agnostic from the start.

But since your acpi_bus_type handles also the routers, and also since the node hierarchy with the separate UFP and DFP ports and everything did not look straightforward, it's probable easier to just use that.

So sorry again for the noise.

Quoted 35 lines
Quoted 35 lines

drivers/thunderbolt/usb4_port.c line 73

Quoted 9 lines
drivers/thunderbolt/usb4_port.c new file +9 -0
+/**
+ * usb4_port_device_add() - Add USB4 port device
+ * @port: Lane 0 adapter port to add the USB4 port
+ *
+ * Creates and registers a USB4 port device for @port. Returns the new
+ * USB4 port device pointer or ERR_PTR() in case of error.
+ */
+struct usb4_port *usb4_port_device_add(struct tb_port *port)
+{
        struct fwnode_handle *child;

drivers/thunderbolt/usb4_port.c line 84

Quoted 11 lines
drivers/thunderbolt/usb4_port.c new file +11 -0
+	struct usb4_port *usb4;
+	int ret;
+
+	usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
+	if (!usb4)
+		return ERR_PTR(-ENOMEM);
+
+	usb4->port = port;
+	usb4->dev.type = &usb4_port_device_type;
+	usb4->dev.parent = &port->sw->dev;
+	dev_set_name(&usb4->dev, "usb4_port%d", port->port);

and then here something like this (feel free to improve this part):

        device_for_each_child_node(&port->sw->dev, child) {
                if (usb4_port_fwnode_match(port, child)) {
                        usb4->dev.fwnode = child;
                        break;
                }
        }

Or maybe I'm missing something?

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
drivers/thunderbolt/usb4_port.c new file +7 -0
+	ret = device_register(&usb4->dev);
+	if (ret) {
+		put_device(&usb4->dev);
+		return ERR_PTR(ret);
+	}
+
+	pm_runtime_no_callbacks(&usb4->dev);
Quoted 8 lines

drivers/thunderbolt/usb4_port.c line 100

Quoted 8 lines
drivers/thunderbolt/usb4_port.c new file +8 -0
+	pm_runtime_set_active(&usb4->dev);
+	pm_runtime_enable(&usb4->dev);
+	pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
+	pm_runtime_mark_last_busy(&usb4->dev);
+	pm_runtime_use_autosuspend(&usb4->dev);
+
+	return usb4;
+}

thanks,

Signature · 1 line
heikki
#7 lore raw

PATCH 3/9 thunderbolt: Add support for ACPI _DSM to power on/off retimers

In reply to Mika Westerberg

From: Rajmohan Mani <[email protected]>

Typically retimers can be accessed only when the USB4 link is up (e.g there is a cable connected). However, sometimes it is useful to be able to access retimers even if there is nothing connected to the USB4 port. For instance we may still want to be able to upgrade the retimer NVM firmware even if the user does not have any USB4 devices. This is something that USB4 spec leaves to implementers.

In case of ACPI based systems, we can support this by providing a special _DSM method under each USB4 port. This _DSM can be used to turn on power to on-board retimers (and cycle it through different modes so that the sideband becomes usable).

This patch adds support for this _DSM and makes the functionality available to the rest of the driver through tb_acpi_power_[on|off]_retimers().

---
 drivers/thunderbolt/acpi.c   | 206 +++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/domain.c |   9 +-
 drivers/thunderbolt/tb.h     |  13 +++
 3 files changed, 225 insertions(+), 3 deletions(-)
drivers/thunderbolt/acpi.c +206 -0
index 35fa17f7e599..b67e72d5644b 100644
@@ -180,3 +180,209 @@ bool tb_acpi_is_xdomain_allowed(void)
 		return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
 	return true;
 }
+
+/* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */
+static const guid_t retimer_dsm_guid =
+	GUID_INIT(0xe0053122, 0x795b, 0x4122,
+		  0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
+
+#define RETIMER_DSM_QUERY_ONLINE_STATE	1
+#define RETIMER_DSM_SET_ONLINE_STATE	2
+
+static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
+{
+	struct usb4_port *usb4 = port->usb4;
+	union acpi_object argv4[2];
+	struct acpi_device *adev;
+	union acpi_object *obj;
+	int ret;
+
+	if (!usb4->can_offline)
+		return 0;
+
+	adev = ACPI_COMPANION(&usb4->dev);
+	if (WARN_ON(!adev))
+		return 0;
+
+	/* Check if we are already powered on (and in correct mode) */
+	obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
+				      RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
+				      ACPI_TYPE_INTEGER);
+	if (!obj) {
+		tb_port_warn(port, "ACPI: query online _DSM failed\n");
+		return -EIO;
+	}
+
+	ret = obj->integer.value;
+	ACPI_FREE(obj);
+
+	if (power == ret)
+		return 0;
+
+	tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
+		    power ? "on" : "off");
+
+	argv4[0].type = ACPI_TYPE_PACKAGE;
+	argv4[0].package.count = 1;
+	argv4[0].package.elements = &argv4[1];
+	argv4[1].integer.type = ACPI_TYPE_INTEGER;
+	argv4[1].integer.value = power;
+
+	obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
+				      RETIMER_DSM_SET_ONLINE_STATE, argv4,
+				      ACPI_TYPE_INTEGER);
+	if (!obj) {
+		tb_port_warn(port,
+			     "ACPI: set online state _DSM evaluation failed\n");
+		return -EIO;
+	}
+
+	ret = obj->integer.value;
+	ACPI_FREE(obj);
+
+	if (ret >= 0) {
+		if (power)
+			return ret == 1 ? 0 : -EBUSY;
+		return 0;
+	}
+
+	tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
+	return -EIO;
+}
+
+/**
+ * tb_acpi_power_on_retimers() - Call platform to power on retimers
+ * @port: USB4 port
+ *
+ * Calls platform to turn on power to all retimers behind this USB4
+ * port. After this function returns successfully the caller can
+ * continue with the normal retimer flows (as specified in the USB4
+ * spec). Note if this returns %-EBUSY it means the type-C port is in
+ * non-USB4/TBT mode (there is non-USB4/TBT device connected).
+ *
+ * This should only be called if the USB4/TBT link is not up.
+ *
+ * Returns %0 on success.
+ */
+int tb_acpi_power_on_retimers(struct tb_port *port)
+{
+	return tb_acpi_retimer_set_power(port, true);
+}
+
+/**
+ * tb_acpi_power_off_retimers() - Call platform to power off retimers
+ * @port: USB4 port
+ *
+ * This is the opposite of tb_acpi_power_on_retimers(). After returning
+ * successfully the normal operations with the @port can continue.
+ *
+ * Returns %0 on success.
+ */
+int tb_acpi_power_off_retimers(struct tb_port *port)
+{
+	return tb_acpi_retimer_set_power(port, false);
+}
+
+static bool tb_acpi_bus_match(struct device *dev)
+{
+	return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
+}
+
+static struct acpi_device *tb_acpi_find_port(struct acpi_device *adev,
+					     const struct tb_port *port)
+{
+	struct acpi_device *port_adev;
+
+	if (!adev)
+		return NULL;
+
+	/*
+	 * Device routers exists under the downstream facing USB4 port
+	 * of the parent router. Their _ADR is always 0.
+	 */
+	list_for_each_entry(port_adev, &adev->children, node) {
+		if (acpi_device_adr(port_adev) == port->port)
+			return port_adev;
+	}
+
+	return NULL;
+}
+
+static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
+{
+	struct acpi_device *adev = NULL;
+	struct tb_switch *parent_sw;
+
+	parent_sw = tb_switch_parent(sw);
+	if (parent_sw) {
+		struct tb_port *port = tb_port_at(tb_route(sw), parent_sw);
+		struct acpi_device *port_adev;
+
+		port_adev = tb_acpi_find_port(ACPI_COMPANION(&parent_sw->dev), port);
+		if (port_adev)
+			adev = acpi_find_child_device(port_adev, 0, false);
+	} else {
+		struct tb_nhi *nhi = sw->tb->nhi;
+		struct acpi_device *parent_adev;
+
+		parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
+		if (parent_adev)
+			adev = acpi_find_child_device(parent_adev, 0, false);
+	}
+
+	return adev;
+}
+
+static struct acpi_device *tb_acpi_find_companion(struct device *dev)
+{
+	/*
+	 * The Thunderbolt/USB4 hierarchy looks like following:
+	 *
+	 * Device (NHI)
+	 *   Device (HR)		// Host router _ADR == 0
+	 *      Device (DFP0)		// Downstream port _ADR == lane 0 adapter
+	 *        Device (DR)		// Device router _ADR == 0
+	 *          Device (UFP)	// Upstream port _ADR == lane 0 adapter
+	 *      Device (DFP1)		// Downstream port _ADR == lane 0 adapter number
+	 *
+	 * At the moment we bind the host router to the corresponding
+	 * Linux device.
+	 */
+	if (tb_is_switch(dev))
+		return tb_acpi_switch_find_companion(tb_to_switch(dev));
+	else if (tb_is_usb4_port_device(dev))
+		return tb_acpi_find_port(ACPI_COMPANION(dev->parent),
+					 tb_to_usb4_port_device(dev)->port);
+	return NULL;
+}
+
+static void tb_acpi_setup(struct device *dev)
+{
+	struct acpi_device *adev = ACPI_COMPANION(dev);
+	struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
+
+	if (!adev || !usb4)
+		return;
+
+	if (acpi_check_dsm(adev->handle, &retimer_dsm_guid, 1,
+			   BIT(RETIMER_DSM_QUERY_ONLINE_STATE) |
+			   BIT(RETIMER_DSM_SET_ONLINE_STATE)))
+		usb4->can_offline = true;
+}
+
+static struct acpi_bus_type tb_acpi_bus = {
+	.name = "thunderbolt",
+	.match = tb_acpi_bus_match,
+	.find_companion = tb_acpi_find_companion,
+	.setup = tb_acpi_setup,
+};
+
+int tb_acpi_init(void)
+{
+	return register_acpi_bus_type(&tb_acpi_bus);
+}
+
+void tb_acpi_exit(void)
+{
+	unregister_acpi_bus_type(&tb_acpi_bus);
+}
drivers/thunderbolt/domain.c +6 -3
index 98f4056f89ff..a062befcb3b2 100644
@@ -881,11 +881,12 @@ int tb_domain_init(void)
 	int ret;
 
 	tb_test_init();
-
 	tb_debugfs_init();
+	tb_acpi_init();
+
 	ret = tb_xdomain_init();
 	if (ret)
-		goto err_debugfs;
+		goto err_acpi;
 	ret = bus_register(&tb_bus_type);
 	if (ret)
 		goto err_xdomain;
@@ -894,7 +895,8 @@ int tb_domain_init(void)
 
 err_xdomain:
 	tb_xdomain_exit();
-err_debugfs:
+err_acpi:
+	tb_acpi_exit();
 	tb_debugfs_exit();
 	tb_test_exit();
 
@@ -907,6 +909,7 @@ void tb_domain_exit(void)
 	ida_destroy(&tb_domain_ida);
 	tb_nvm_exit();
 	tb_xdomain_exit();
+	tb_acpi_exit();
 	tb_debugfs_exit();
 	tb_test_exit();
 }
drivers/thunderbolt/tb.h +13 -0
index 948e7601428f..c5704f495afa 100644
@@ -247,10 +247,13 @@ struct tb_port {
  * struct usb4_port - USB4 port device
  * @dev: Device for the port
  * @port: Pointer to the lane 0 adapter
+ * @can_offline: Does the port have necessary platform support to moved
+ *		 it into offline mode and back
  */
 struct usb4_port {
 	struct device dev;
 	struct tb_port *port;
+	bool can_offline;
 };
 
 /**
@@ -1113,6 +1116,11 @@ bool tb_acpi_may_tunnel_usb3(void);
 bool tb_acpi_may_tunnel_dp(void);
 bool tb_acpi_may_tunnel_pcie(void);
 bool tb_acpi_is_xdomain_allowed(void);
+
+int tb_acpi_init(void);
+void tb_acpi_exit(void);
+int tb_acpi_power_on_retimers(struct tb_port *port);
+int tb_acpi_power_off_retimers(struct tb_port *port);
 #else
 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
 
@@ -1121,6 +1129,11 @@ static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
 static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
+
+static inline int tb_acpi_init(void) { return 0; }
+static inline void tb_acpi_exit(void) { }
+static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; }
+static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; }
 #endif
 
 #ifdef CONFIG_DEBUG_FS
Signature · 1 line
2.30.2
#8 lore raw

PATCH 4/9 thunderbolt: Add additional USB4 port operations for retimer access

In reply to Mika Westerberg

From: Rajmohan Mani <[email protected]>

When accessing retimers when there is no cable connected we are going to need additional USB4 port operations. First the port needs to be put into offline mode, and then the sideband channel transactions must be enabled on the SBTX line. This adds support for these operations.

---
 drivers/thunderbolt/sb_regs.h |  2 +
 drivers/thunderbolt/tb.h      |  3 ++
 drivers/thunderbolt/usb4.c    | 69 +++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
drivers/thunderbolt/sb_regs.h +2 -0
index 9dafd696612f..bda889ff3bda 100644
@@ -17,7 +17,9 @@
 enum usb4_sb_opcode {
 	USB4_SB_OPCODE_ERR = 0x20525245,			/* "ERR " */
 	USB4_SB_OPCODE_ONS = 0x444d4321,			/* "!CMD" */
+	USB4_SB_OPCODE_ROUTER_OFFLINE = 0x4e45534c,		/* "LSEN" */
 	USB4_SB_OPCODE_ENUMERATE_RETIMERS = 0x4d554e45,		/* "ENUM" */
+	USB4_SB_OPCODE_SET_INBOUND_SBTX = 0x5055534c,		/* "LSUP" */
 	USB4_SB_OPCODE_QUERY_LAST_RETIMER = 0x5453414c,		/* "LAST" */
 	USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE = 0x53534e47,	/* "GNSS" */
 	USB4_SB_OPCODE_NVM_SET_OFFSET = 0x53504f42,		/* "BOPS" */
drivers/thunderbolt/tb.h +3 -0
index c5704f495afa..936518adca74 100644
@@ -1062,8 +1062,11 @@ int usb4_port_configure(struct tb_port *port);
 void usb4_port_unconfigure(struct tb_port *port);
 int usb4_port_configure_xdomain(struct tb_port *port);
 void usb4_port_unconfigure_xdomain(struct tb_port *port);
+int usb4_port_router_offline(struct tb_port *port);
+int usb4_port_router_online(struct tb_port *port);
 int usb4_port_enumerate_retimers(struct tb_port *port);
 
+int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index);
 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
 			   u8 size);
 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
drivers/thunderbolt/usb4.c +69 -0

81 lines not on this page · raw message

Signature · 1 line
2.30.2
#9 lore raw

PATCH 5/9 thunderbolt: Add support for retimer NVM upgrade when there is no link

In reply to Mika Westerberg

From: Rajmohan Mani <[email protected]>

With help from platform firmware (ACPI) it is possible to power on retimers even when there is no USB4 link (e.g nothing is connected to the USB4 ports). This allows us to bring the USB4 sideband up so that we can access retimers and upgrade their NVM firmware.

If the platform has support for this, we expose two additional attributes under USB4 ports: offline and rescan. These can be used to bring the port offline, rescan for the retimers and put the port online again. The retimer NVM upgrade itself works the same way than with cable connected.

---
 .../ABI/testing/sysfs-bus-thunderbolt         |  26 +++
 Documentation/admin-guide/thunderbolt.rst     |  29 +++
 drivers/thunderbolt/retimer.c                 |  24 ++-
 drivers/thunderbolt/switch.c                  |  48 +++--
 drivers/thunderbolt/tb.c                      |   4 +-
 drivers/thunderbolt/tb.h                      |   5 +-
 drivers/thunderbolt/usb4_port.c               | 169 ++++++++++++++++++
 7 files changed, 277 insertions(+), 28 deletions(-)
Documentation/ABI/testing/sysfs-bus-thunderbolt +26 -0
index 3537ba1ba892..f6743dc33aac 100644
@@ -297,6 +297,32 @@ Contact:	Mika Westerberg <[email protected]>
 Description:	Returns the current link mode. Possible values are
 		"usb4", "tbt" and "none".
 
+What:		/sys/bus/thunderbolt/devices/usb4_portX/offline
+Date:		Sep 2021
+KernelVersion:	v5.14
+Contact:	Rajmohan Mani <[email protected]>
+Description:	Writing 1 to this attribute puts the USB4 port into
+		offline mode. Only allowed when there is nothing
+		connected to the port (link attribute returns "none").
+		Once the port is in offline mode it does not receive any
+		hotplug events. This is used to update NVM firmware of
+		on-board retimers. Writing 0 puts the port back to
+		online mode.
+
+		This attribute is only visible if the platform supports
+		powering on retimers when there is no cable connected.
+
+What:		/sys/bus/thunderbolt/devices/usb4_portX/rescan
+Date:		Sep 2021
+KernelVersion:	v5.14
+Contact:	Rajmohan Mani <[email protected]>
+Description:	When the USB4 port is in offline mode writing 1 to this
+		attribute forces rescan of the sideband for on-board
+		retimers. Each retimer appear under the USB4 port as if
+		the USB4 link was up. These retimers act in the same way
+		as if the cable was connected so upgrading their NVM
+		firmware can be done the usual way.
+
 What:		/sys/bus/thunderbolt/devices/<device>:<port>.<index>/device
 Date:		Oct 2020
 KernelVersion:	v5.9
Documentation/admin-guide/thunderbolt.rst +29 -0
index f18e881373c4..2ed79f41a411 100644
@@ -256,6 +256,35 @@ Note names of the NVMem devices ``nvm_activeN`` and ``nvm_non_activeN``
 depend on the order they are registered in the NVMem subsystem. N in
 the name is the identifier added by the NVMem subsystem.
 
+Upgrading on-board retimer NVM when there is no cable connected
+---------------------------------------------------------------
+If the platform supports, it may be possible to upgrade the retimer NVM
+firmware even when there is nothing connected to the USB4
+ports. When this is the case the ``usb4_portX`` devices have two special
+attributes: ``offline`` and ``rescan``. The way to upgrade the firmware
+is to first put the USB4 port into offline mode::
+
+  # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
+
+This step makes sure the port does not respond to any hotplug events,
+and also ensures the retimers are powered on. The next step is to scan
+for the retimers::
+
+  # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
+
+This enumerates and adds the on-board retimers. Now retimer NVM can be
+upgraded in the same way than with cable connected (see previous
+section). However, the retimer is not disconnected as we are offline
+mode) so after writing ``1`` to ``nvm_authenticate`` one should wait for
+5 or more seconds before running rescan again::
+
+  # echo 1 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/rescan
+
+This point if everything went fine, the port can be put back to
+functional state again::
+
+  # echo 0 > /sys/bus/thunderbolt/devices/0-0/usb4_port1/offline
+
 Upgrading NVM when host controller is in safe mode
 --------------------------------------------------
 If the existing NVM is not properly authenticated (or is missing) the
drivers/thunderbolt/retimer.c +15 -9
index 2e5188fb1150..05af0feefe84 100644
@@ -401,19 +401,18 @@ static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
 /**
  * tb_retimer_scan() - Scan for on-board retimers under port
  * @port: USB4 port to scan
+ * @add: If true also registers found retimers
  *
- * Tries to enumerate on-board retimers connected to @port. Found
- * retimers are registered as children of @port. Does not scan for cable
- * retimers for now.
+ * Brings the sideband into a state where retimers can be accessed.
+ * Then Tries to enumerate on-board retimers connected to @port. Found
+ * retimers are registered as children of @port if @add is set.  Does
+ * not scan for cable retimers for now.
  */
-int tb_retimer_scan(struct tb_port *port)
+int tb_retimer_scan(struct tb_port *port, bool add)
 {
 	u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
 	int ret, i, last_idx = 0;
 
-	if (!port->cap_usb4)
-		return 0;
-
 	/*
 	 * Send broadcast RT to make sure retimer indices facing this
 	 * port are set.
@@ -422,6 +421,13 @@ int tb_retimer_scan(struct tb_port *port)
 	if (ret)
 		return ret;
 
+	/*
+	 * Enable sideband channel for each retimer. We can do this
+	 * regardless whether there is device connected or not.
+	 */
+	for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
+		usb4_port_retimer_set_inbound_sbtx(port, i);
+
 	/*
 	 * Before doing anything else, read the authentication status.
 	 * If the retimer has it set, store it for the new retimer
@@ -453,10 +459,10 @@ int tb_retimer_scan(struct tb_port *port)
 		rt = tb_port_find_retimer(port, i);
 		if (rt) {
 			put_device(&rt->dev);
-		} else {
+		} else if (add) {
 			ret = tb_retimer_add(port, i, status[i]);
 			if (ret && ret != -EOPNOTSUPP)
-				return ret;
+				break;
 		}
 	}
 
drivers/thunderbolt/switch.c +32 -16

67 lines not on this page · raw message

drivers/thunderbolt/tb.c +2 -2
index 7e6dc2b03bed..bc6d568dbb89 100644
@@ -595,7 +595,7 @@ static void tb_scan_port(struct tb_port *port)
 		return;
 	}
 
-	tb_retimer_scan(port);
+	tb_retimer_scan(port, true);
 
 	sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
 			     tb_downstream_route(port));
@@ -662,7 +662,7 @@ static void tb_scan_port(struct tb_port *port)
 		tb_sw_warn(sw, "failed to enable TMU\n");
 
 	/* Scan upstream retimers */
-	tb_retimer_scan(upstream_port);
+	tb_retimer_scan(upstream_port, true);
 
 	/*
 	 * Create USB 3.x tunnels only when the switch is plugged to the
drivers/thunderbolt/tb.h +4 -1
index 936518adca74..341e8443a22d 100644
@@ -249,11 +249,13 @@ struct tb_port {
  * @port: Pointer to the lane 0 adapter
  * @can_offline: Does the port have necessary platform support to moved
  *		 it into offline mode and back
+ * @offline: The port is currently in offline mode
  */
 struct usb4_port {
 	struct device dev;
 	struct tb_port *port;
 	bool can_offline;
+	bool offline;
 };
 
 /**
@@ -1017,7 +1019,7 @@ void tb_xdomain_remove(struct tb_xdomain *xd);
 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
 						 u8 depth);
 
-int tb_retimer_scan(struct tb_port *port);
+int tb_retimer_scan(struct tb_port *port, bool add);
 void tb_retimer_remove_all(struct tb_port *port);
 
 static inline bool tb_is_retimer(const struct device *dev)
@@ -1105,6 +1107,7 @@ static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
 
 struct usb4_port *usb4_port_device_add(struct tb_port *port);
 void usb4_port_device_remove(struct usb4_port *usb4);
+int usb4_port_device_resume(struct usb4_port *usb4);
 
 /* Keep link controller awake during update */
 #define QUIRK_FORCE_POWER_LINK_CONTROLLER		BIT(0)
drivers/thunderbolt/usb4_port.c +169 -0

180 lines not on this page · raw message

Signature · 1 line
2.30.2
#10 lore raw

PATCH 6/9 thunderbolt: Move nvm_write_ops to tb.h

In reply to Mika Westerberg

From: Rajmohan Mani <[email protected]>

Currently these write ops are used for updating router firmware images only. Moving to tb.h helps the retimers also to use the same ops.

Also add tb_ prefix to the enum while there.

---
 drivers/thunderbolt/switch.c | 5 -----
 drivers/thunderbolt/tb.h     | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/thunderbolt/switch.c +0 -5
index dae59919e2bf..bf4821d3bbab 100644
@@ -26,11 +26,6 @@ struct nvm_auth_status {
 	u32 status;
 };
 
-enum nvm_write_ops {
-	WRITE_AND_AUTHENTICATE = 1,
-	WRITE_ONLY = 2,
-};
-
 /*
  * Hold NVM authentication failure status per switch This information
  * needs to stay around even when the switch gets power cycled so we
drivers/thunderbolt/tb.h +5 -0
index 341e8443a22d..863d80ad44ab 100644
@@ -58,6 +58,11 @@ struct tb_nvm {
 	bool flushed;
 };
 
+enum tb_nvm_write_ops {
+	WRITE_AND_AUTHENTICATE = 1,
+	WRITE_ONLY = 2,
+};
+
 #define TB_SWITCH_KEY_SIZE		32
 #define TB_SWITCH_MAX_DEPTH		6
 #define USB4_SWITCH_MAX_DEPTH		5
Signature · 1 line
2.30.2
#11 lore raw

PATCH 7/9 thunderbolt: Allow router NVM authenticate separately

In reply to Mika Westerberg

It may be useful if the actual NVM authentication can be delayed to be run later, for instance when the user logs out. For this reason add a new NVM operation (AUHENTICATE_ONLY) that just triggers the authentication procedure over whatever was written to the NVM storage.

This is not supported with Thunderbolt 1-3 devices, though.

---
 .../ABI/testing/sysfs-bus-thunderbolt         |  5 +-
 drivers/thunderbolt/switch.c                  | 50 ++++++++++++-------
 drivers/thunderbolt/tb.h                      |  2 +
 drivers/thunderbolt/usb4.c                    | 13 ++++-
 4 files changed, 49 insertions(+), 21 deletions(-)
Documentation/ABI/testing/sysfs-bus-thunderbolt +4 -1
index f6743dc33aac..da580b504c87 100644
@@ -213,12 +213,15 @@ Description:	When new NVM image is written to the non-active NVM
 		restarted with the new NVM firmware. If the image
 		verification fails an error code is returned instead.
 
-		This file will accept writing values "1" or "2"
+		This file will accept writing values "1", "2" or "3".
 
 		- Writing "1" will flush the image to the storage
 		  area and authenticate the image in one action.
 		- Writing "2" will run some basic validation on the image
 		  and flush it to the storage area.
+		- Writing "3" will authenticate the image that is
+		  currently written in the storage area. This is only
+		  supported with USB4 devices.
 
 		When read holds status of the last authentication
 		operation if an error occurred during the process. This
drivers/thunderbolt/switch.c +32 -18

75 lines not on this page · raw message

drivers/thunderbolt/tb.h +2 -0
index 863d80ad44ab..53f6bb85b178 100644
@@ -61,6 +61,7 @@ struct tb_nvm {
 enum tb_nvm_write_ops {
 	WRITE_AND_AUTHENTICATE = 1,
 	WRITE_ONLY = 2,
+	AUTHENTICATE_ONLY = 3,
 };
 
 #define TB_SWITCH_KEY_SIZE		32
@@ -1049,6 +1050,7 @@ int usb4_switch_set_sleep(struct tb_switch *sw);
 int usb4_switch_nvm_sector_size(struct tb_switch *sw);
 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
 			 size_t size);
+int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address);
 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
 			  const void *buf, size_t size);
 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
drivers/thunderbolt/usb4.c +11 -2
index 8af96dbaa7a7..76d7335aa440 100644
@@ -547,8 +547,17 @@ int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
 				usb4_switch_nvm_read_block, sw);
 }
 
-static int usb4_switch_nvm_set_offset(struct tb_switch *sw,
-				      unsigned int address)
+/**
+ * usb4_switch_nvm_set_offset() - Set NVM write offset
+ * @sw: USB4 router
+ * @address: Start offset
+ *
+ * Explicitly sets NVM write offset. Normally when writing to NVM this
+ * is done automatically by usb4_switch_nvm_write().
+ *
+ * Returns %0 in success and negative errno if there was a failure.
+ */
+int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address)
 {
 	u32 metadata, dwaddress;
 	u8 status = 0;
Signature · 1 line
2.30.2
#12 lore raw

PATCH 8/9 thunderbolt: Add WRITE_ONLY and AUTHENTICATE_ONLY NVM operations for retimers

In reply to Mika Westerberg

From: Rajmohan Mani <[email protected]>

The same way we support these two operations for USB4 routers we can extend the retimer NVM operations to support retimers also.

---
 .../ABI/testing/sysfs-bus-thunderbolt         |  2 +-
 drivers/thunderbolt/retimer.c                 | 51 ++++++++++++++-----
 drivers/thunderbolt/tb.h                      |  2 +
 drivers/thunderbolt/usb4.c                    | 15 +++++-
 4 files changed, 53 insertions(+), 17 deletions(-)
Documentation/ABI/testing/sysfs-bus-thunderbolt +1 -1
index da580b504c87..95c21d6c9a84 100644
@@ -221,7 +221,7 @@ Description:	When new NVM image is written to the non-active NVM
 		  and flush it to the storage area.
 		- Writing "3" will authenticate the image that is
 		  currently written in the storage area. This is only
-		  supported with USB4 devices.
+		  supported with USB4 devices and retimers.
 
 		When read holds status of the last authentication
 		operation if an error occurred during the process. This
drivers/thunderbolt/retimer.c +37 -14

82 lines not on this page · raw message

drivers/thunderbolt/tb.h +2 -0
index 53f6bb85b178..725104c83e3d 100644
@@ -1082,6 +1082,8 @@ int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
 			    const void *buf, u8 size);
 int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
+int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
+				     unsigned int address);
 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
 				unsigned int address, const void *buf,
 				size_t size);
drivers/thunderbolt/usb4.c +13 -2
index 76d7335aa440..ceddbe7e9f93 100644
@@ -1513,8 +1513,19 @@ int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
 	return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
 }
 
-static int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
-					    unsigned int address)
+/**
+ * usb4_port_retimer_nvm_set_offset() - Set NVM write offset
+ * @port: USB4 port
+ * @index: Retimer index
+ * @address: Start offset
+ *
+ * Exlicitly sets NVM write offset. Normally when writing to NVM this is
+ * done automatically by usb4_port_retimer_nvm_write().
+ *
+ * Returns %0 in success and negative errno if there was a failure.
+ */
+int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
+				     unsigned int address)
 {
 	u32 metadata, dwaddress;
 	int ret;
Signature · 1 line
2.30.2
#13 lore raw

PATCH 9/9 thunderbolt: Check for NVM authentication status after the operation started

In reply to Mika Westerberg

If the NVM authentication fails immediately, like if the firmware detects that the image is not valid for some reason, better to read the status once and if set to non-zero fail the operation accordingly.

---
 drivers/thunderbolt/retimer.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/thunderbolt/retimer.c +19 -1
index 3aa790aa6500..722694052f4a 100644
@@ -151,6 +151,7 @@ static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
 
 static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
 {
+	u32 status;
 	int ret;
 
 	if (auth_only) {
@@ -159,7 +160,24 @@ static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
 			return ret;
 	}
 
-	return usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
+	ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
+	if (ret)
+		return ret;
+
+	usleep_range(100, 150);
+
+	/*
+	 * Check the status now if we still can access the retimer. It
+	 * is expected that the below fails.
+	 */
+	ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
+							&status);
+	if (!ret) {
+		rt->auth_status = status;
+		return status ? -EINVAL : 0;
+	}
+
+	return 0;
 }
 
 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
Signature · 1 line
2.30.2
maintainer Reviewed-by on the v1 series
#14 lore raw
In reply to Mika Westerberg
Quoted 32 lines · Mika Westerberg

Hi all,

USB4 retimers are only accessible when the USB4 is up. However, sometimes it may be useful to be able to upgrade on-board retimers even if the link is not up. For instance if the user simply does not have any USB4 devices.

Making retimers accessible in "offline" mode requires some help from the platform firmware (ACPI in our case) to turn on power to the retimers and cycle them through different modes to get the sideband link up. This may also involve other firmwares such as Embedded Controller (as it is the case with recent Chromebooks).

This series adds support for "offline" retimer NVM upgrade so that it first exposes each USB4 port to the userspace. If the platform firmware provides a special _DSM-method (Device Specific Method) under the USB4 port ACPI description, we expose two attributes under the port that the userspace can use to put the port to offline mode and rescan for the retimers. Otherwise the NVM upgrade works the same way than with the online mode. We also add documentation to the admin-guide how this can be done.

In addition to this, at least Intel USB4 devices (and retimers) allow running NVM authenticate (upgrade) separately from write so we make it possible for the userspace to run the write and authenticate in two steps. This allows userspace to trigger the authentication at later time, like when the user logs out.

Mika Westerberg (4):
thunderbolt: Log the link as TBT instead of TBT3
thunderbolt: Add USB4 port devices
thunderbolt: Allow router NVM authenticate separately
thunderbolt: Check for NVM authentication status after the operation started

Quoted 6 lines

Rajmohan Mani (5):
thunderbolt: Add support for ACPI _DSM to power on/off retimers
thunderbolt: Add additional USB4 port operations for retimer access
thunderbolt: Add support for retimer NVM upgrade when there is no link
thunderbolt: Move nvm_write_ops to tb.h
thunderbolt: Add WRITE_ONLY and AUTHENTICATE_ONLY NVM operations for retimers

Looks good:

#15 lore raw
Quoted 40 lines · Greg Kroah-Hartman
Quoted 32 lines · Mika Westerberg

Hi all,

USB4 retimers are only accessible when the USB4 is up. However, sometimes it may be useful to be able to upgrade on-board retimers even if the link is not up. For instance if the user simply does not have any USB4 devices.

Making retimers accessible in "offline" mode requires some help from the platform firmware (ACPI in our case) to turn on power to the retimers and cycle them through different modes to get the sideband link up. This may also involve other firmwares such as Embedded Controller (as it is the case with recent Chromebooks).

This series adds support for "offline" retimer NVM upgrade so that it first exposes each USB4 port to the userspace. If the platform firmware provides a special _DSM-method (Device Specific Method) under the USB4 port ACPI description, we expose two attributes under the port that the userspace can use to put the port to offline mode and rescan for the retimers. Otherwise the NVM upgrade works the same way than with the online mode. We also add documentation to the admin-guide how this can be done.

In addition to this, at least Intel USB4 devices (and retimers) allow running NVM authenticate (upgrade) separately from write so we make it possible for the userspace to run the write and authenticate in two steps. This allows userspace to trigger the authentication at later time, like when the user logs out.

Mika Westerberg (4):
thunderbolt: Log the link as TBT instead of TBT3
thunderbolt: Add USB4 port devices
thunderbolt: Allow router NVM authenticate separately
thunderbolt: Check for NVM authentication status after the operation started

Quoted 6 lines

Rajmohan Mani (5):
thunderbolt: Add support for ACPI _DSM to power on/off retimers
thunderbolt: Add additional USB4 port operations for retimer access
thunderbolt: Add support for retimer NVM upgrade when there is no link
thunderbolt: Move nvm_write_ops to tb.h
thunderbolt: Add WRITE_ONLY and AUTHENTICATE_ONLY NVM operations for retimers

Quoted 3 lines

Looks good:

Thanks!

All applied to thunderbolt.git/next.