Skip to contentSkip to conversation

thunderbolt: Add debugfs support

PATCH In review Mika Westerberg 2 participants 14 messages Aug 26, 2020 – Sep 3, 2020

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

cover thunderbolt: Add debugfs support

Hi all,

This series adds debugfs support to the driver. This is useful when debugging different hardware/software issues as the developer can dump different config spaces of the router including adapter, path and counters config spaces. Each connected router is exposed in debugfs under thunderbolt directory, and the naming follows what we have in sysfs.

This also adds a capability to write certain registers but that needs to be enabled through Kconfig option, not supposed to be enabled by distros (or regular users).

The series is based on top of my "Power Management improvements" patches which can be viewed in the below link:

https://lore.kernel.org/linux-usb/[email protected]/

Gil Fine (2):
thunderbolt: Introduce tb_switch_is_tiger_lake()
thunderbolt: Add debugfs interface

Mika Westerberg (7):
thunderbolt: Move struct tb_cap_any to tb_regs.h
thunderbolt: Introduce tb_port_next_cap()
thunderbolt: Introduce tb_switch_next_cap()
thunderbolt: Introduce tb_port_is_nhi()
thunderbolt: Check for Intel vendor ID when identifying controller
thunderbolt: Introduce tb_switch_is_ice_lake()
thunderbolt: No need to warn in TB_CFG_ERROR_INVALID_CONFIG_SPACE

 drivers/thunderbolt/Kconfig   |  10 +
 drivers/thunderbolt/Makefile  |   1 +
 drivers/thunderbolt/cap.c     | 126 ++++--
 drivers/thunderbolt/ctl.c     |   5 +-
 drivers/thunderbolt/debugfs.c | 700 ++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/domain.c  |  13 +-
 drivers/thunderbolt/switch.c  |   5 +-
 drivers/thunderbolt/tb.h      | 109 ++++--
 drivers/thunderbolt/tb_regs.h |  18 +-
 9 files changed, 909 insertions(+), 78 deletions(-)
 create mode 100644 drivers/thunderbolt/debugfs.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/9 thunderbolt: Move struct tb_cap_any to tb_regs.h

This structure will be needed by the debugfs implementation so make it available outside of cap.c.

While there add kernel-doc comments to the structure.

---
 drivers/thunderbolt/cap.c     |  8 --------
 drivers/thunderbolt/tb_regs.h | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 8 deletions(-)
drivers/thunderbolt/cap.c +0 -8
index 19db6cdc5b70..1582e4ebac56 100644
@@ -15,14 +15,6 @@
 #define VSE_CAP_OFFSET_MAX	0xffff
 #define TMU_ACCESS_EN		BIT(20)
 
-struct tb_cap_any {
-	union {
-		struct tb_cap_basic basic;
-		struct tb_cap_extended_short extended_short;
-		struct tb_cap_extended_long extended_long;
-	};
-} __packed;
-
 static int tb_port_enable_tmu(struct tb_port *port, bool enable)
 {
 	struct tb_switch *sw = port->sw;
drivers/thunderbolt/tb_regs.h +14 -0
index 0431e415e3bc..c33751be0f56 100644
@@ -93,6 +93,20 @@ struct tb_cap_extended_long {
 	u16 length;
 } __packed;
 
+/**
+ * struct tb_cap_any - Structure capable of hold every capability
+ * @basic: Basic capability
+ * @extended_short: Vendor specific capability
+ * @extended_long: Vendor specific extended capability
+ */
+struct tb_cap_any {
+	union {
+		struct tb_cap_basic basic;
+		struct tb_cap_extended_short extended_short;
+		struct tb_cap_extended_long extended_long;
+	};
+} __packed;
+
 /* capabilities */
 
 struct tb_cap_link_controller {
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/9 thunderbolt: Introduce tb_port_next_cap()

In reply to Mika Westerberg

This function is useful for walking port config space (adapter) capability lists. Convert the tb_port_find_cap() to use this as well.

---
 drivers/thunderbolt/cap.c | 35 +++++++++++++++++++++++++++++++----
 drivers/thunderbolt/tb.h  |  1 +
 2 files changed, 32 insertions(+), 4 deletions(-)
drivers/thunderbolt/cap.c +31 -4
index 1582e4ebac56..c45b3a488412 100644
@@ -59,23 +59,50 @@ static void tb_port_dummy_read(struct tb_port *port)
 	}
 }
 
+/**
+ * tb_port_next_cap() - Return next capability in the linked list
+ * @port: Port to find the capability for
+ * @offset: Previous capability offset (%0 for start)
+ *
+ * Returns dword offset of the next capability in port config space
+ * capability list and returns it. Passing %0 returns the first entry in
+ * the capability list. If no next capability is found returns %0. In case
+ * of failure returns negative errno.
+ */
+int tb_port_next_cap(struct tb_port *port, unsigned int offset)
+{
+	struct tb_cap_any header;
+	int ret;
+
+	if (!offset)
+		return port->config.first_cap_offset;
+
+	ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
+	if (ret)
+		return ret;
+
+	return header.basic.next;
+}
+
 static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
 {
-	u32 offset = 1;
+	int offset = 0;
 
 	do {
 		struct tb_cap_any header;
 		int ret;
 
+		offset = tb_port_next_cap(port, offset);
+		if (offset < 0)
+			return offset;
+
 		ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
 		if (ret)
 			return ret;
 
 		if (header.basic.cap == cap)
 			return offset;
-
-		offset = header.basic.next;
-	} while (offset);
+	} while (offset > 0);
 
 	return -ENOENT;
 }
drivers/thunderbolt/tb.h +1 -0
index 7754c690addc..54e8fad78bee 100644
@@ -823,6 +823,7 @@ int tb_port_get_link_speed(struct tb_port *port);
 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
+int tb_port_next_cap(struct tb_port *port, unsigned int offset);
 bool tb_port_is_enabled(struct tb_port *port);
 
 bool tb_usb3_port_is_enabled(struct tb_port *port);
Signature · 1 line
2.28.0
#3 lore raw

PATCH 3/9 thunderbolt: Introduce tb_switch_next_cap()

In reply to Mika Westerberg

This is similar to tb_port_next_cap() but instead allows walking capability list of a switch (router). Convert tb_switch_find_cap() and tb_switch_find_vse_cap() to use this as well.

---
 drivers/thunderbolt/cap.c | 83 +++++++++++++++++++++++++--------------
 drivers/thunderbolt/tb.h  |  1 +
 2 files changed, 54 insertions(+), 30 deletions(-)
drivers/thunderbolt/cap.c +53 -30
index c45b3a488412..9af1b855e43b 100644
@@ -132,6 +132,40 @@ int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
 	return ret;
 }
 
+/**
+ * tb_switch_next_cap() - Return next capability in the linked list
+ * @sw: Switch to find the capability for
+ * @offset: Previous capability offset (%0 for start)
+ *
+ * Finds dword offset of the next capability in router config space
+ * capability list and returns it. Passing %0 returns the first entry in
+ * the capability list. If no next capability is found returns %0. In case
+ * of failure returns negative errno.
+ */
+int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
+{
+	struct tb_cap_any header;
+	int ret;
+
+	if (!offset)
+		return sw->config.first_cap_offset;
+
+	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
+	if (ret)
+		return ret;
+
+	if (header.basic.cap == TB_SWITCH_CAP_VSE) {
+		if (!header.extended_short.length)
+			ret = header.extended_long.next;
+		else
+			ret = header.extended_short.next;
+	} else {
+		ret = header.basic.next;
+	}
+
+	return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
+}
+
 /**
  * tb_switch_find_cap() - Find switch capability
  * @sw Switch to find the capability for
@@ -143,21 +177,23 @@ int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  */
 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
 {
-	int offset = sw->config.first_cap_offset;
+	int offset = 0;
 
-	while (offset > 0 && offset < CAP_OFFSET_MAX) {
+	do {
 		struct tb_cap_any header;
 		int ret;
 
+		offset = tb_switch_next_cap(sw, offset);
+		if (offset < 0)
+			return offset;
+
 		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
 		if (ret)
 			return ret;
 
 		if (header.basic.cap == cap)
 			return offset;
-
-		offset = header.basic.next;
-	}
+	} while (offset);
 
 	return -ENOENT;
 }
@@ -174,37 +210,24 @@ int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
  */
 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
 {
-	struct tb_cap_any header;
-	int offset;
-
-	offset = tb_switch_find_cap(sw, TB_SWITCH_CAP_VSE);
-	if (offset < 0)
-		return offset;
+	int offset = 0;
 
-	while (offset > 0 && offset < VSE_CAP_OFFSET_MAX) {
+	do {
+		struct tb_cap_any header;
 		int ret;
 
-		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
+		offset = tb_switch_next_cap(sw, offset);
+		if (offset < 0)
+			return offset;
+
+		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
 		if (ret)
 			return ret;
 
-		/*
-		 * Extended vendor specific capabilities come in two
-		 * flavors: short and long. The latter is used when
-		 * offset is over 0xff.
-		 */
-		if (offset >= CAP_OFFSET_MAX) {
-			if (header.extended_long.vsec_id == vsec)
-				return offset;
-			offset = header.extended_long.next;
-		} else {
-			if (header.extended_short.vsec_id == vsec)
-				return offset;
-			if (!header.extended_short.length)
-				return -ENOENT;
-			offset = header.extended_short.next;
-		}
-	}
+		if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
+		    header.extended_short.vsec_id == vsec)
+			return offset;
+	} while (offset);
 
 	return -ENOENT;
 }
drivers/thunderbolt/tb.h +1 -0
index 54e8fad78bee..a1d5de53a349 100644
@@ -822,6 +822,7 @@ int tb_port_get_link_speed(struct tb_port *port);
 
 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
+int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
 bool tb_port_is_enabled(struct tb_port *port);
Signature · 1 line
2.28.0
#4 lore raw

PATCH v2 3/9 thunderbolt: Introduce tb_switch_next_cap()

This is similar to tb_port_next_cap() but instead allows walking capability list of a switch (router). Convert tb_switch_find_cap() and tb_switch_find_vse_cap() to use this as well.

---
Hi all,

Just sending this one as the rest of the series is unchanged. I noticed when testing with older devices, the Port Ridge controller (found on Apple Thunderbolt ethernet/firewire dongles) does not terminate the router capability list correctly so this version checks for the two supported capability IDs and terminates the walk if an unknown ID is found.

Also added Greg's tag.

 drivers/thunderbolt/cap.c | 93 ++++++++++++++++++++++++++-------------
 drivers/thunderbolt/tb.h  |  1 +
 2 files changed, 64 insertions(+), 30 deletions(-)
drivers/thunderbolt/cap.c +63 -30
index c45b3a488412..6f571e912cf2 100644
@@ -132,6 +132,50 @@ int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
 	return ret;
 }
 
+/**
+ * tb_switch_next_cap() - Return next capability in the linked list
+ * @sw: Switch to find the capability for
+ * @offset: Previous capability offset (%0 for start)
+ *
+ * Finds dword offset of the next capability in router config space
+ * capability list and returns it. Passing %0 returns the first entry in
+ * the capability list. If no next capability is found returns %0. In case
+ * of failure returns negative errno.
+ */
+int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
+{
+	struct tb_cap_any header;
+	int ret;
+
+	if (!offset)
+		return sw->config.first_cap_offset;
+
+	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
+	if (ret)
+		return ret;
+
+	switch (header.basic.cap) {
+	case TB_SWITCH_CAP_TMU:
+		ret = header.basic.next;
+		break;
+
+	case TB_SWITCH_CAP_VSE:
+		if (!header.extended_short.length)
+			ret = header.extended_long.next;
+		else
+			ret = header.extended_short.next;
+		break;
+
+	default:
+		tb_sw_dbg(sw, "unknown capability %#x at %#x\n",
+			  header.basic.cap, offset);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
+}
+
 /**
  * tb_switch_find_cap() - Find switch capability
  * @sw Switch to find the capability for
@@ -143,21 +187,23 @@ int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  */
 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
 {
-	int offset = sw->config.first_cap_offset;
+	int offset = 0;
 
-	while (offset > 0 && offset < CAP_OFFSET_MAX) {
+	do {
 		struct tb_cap_any header;
 		int ret;
 
+		offset = tb_switch_next_cap(sw, offset);
+		if (offset < 0)
+			return offset;
+
 		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
 		if (ret)
 			return ret;
 
 		if (header.basic.cap == cap)
 			return offset;
-
-		offset = header.basic.next;
-	}
+	} while (offset);
 
 	return -ENOENT;
 }
@@ -174,37 +220,24 @@ int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
  */
 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
 {
-	struct tb_cap_any header;
-	int offset;
-
-	offset = tb_switch_find_cap(sw, TB_SWITCH_CAP_VSE);
-	if (offset < 0)
-		return offset;
+	int offset = 0;
 
-	while (offset > 0 && offset < VSE_CAP_OFFSET_MAX) {
+	do {
+		struct tb_cap_any header;
 		int ret;
 
-		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
+		offset = tb_switch_next_cap(sw, offset);
+		if (offset < 0)
+			return offset;
+
+		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
 		if (ret)
 			return ret;
 
-		/*
-		 * Extended vendor specific capabilities come in two
-		 * flavors: short and long. The latter is used when
-		 * offset is over 0xff.
-		 */
-		if (offset >= CAP_OFFSET_MAX) {
-			if (header.extended_long.vsec_id == vsec)
-				return offset;
-			offset = header.extended_long.next;
-		} else {
-			if (header.extended_short.vsec_id == vsec)
-				return offset;
-			if (!header.extended_short.length)
-				return -ENOENT;
-			offset = header.extended_short.next;
-		}
-	}
+		if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
+		    header.extended_short.vsec_id == vsec)
+			return offset;
+	} while (offset);
 
 	return -ENOENT;
 }
drivers/thunderbolt/tb.h +1 -0
index 786c313ce97c..cbd18cad9bcd 100644
@@ -828,6 +828,7 @@ int tb_port_get_link_speed(struct tb_port *port);
 
 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
+int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
 bool tb_port_is_enabled(struct tb_port *port);
Signature · 1 line
2.28.0
#5 lore raw

PATCH 4/9 thunderbolt: Introduce tb_port_is_nhi()

In reply to Mika Westerberg

This is useful if one needs to check if adapter (port) is the host interface (NHI). Make tb_port_alloc_hopid() take advantage of this.

---
 drivers/thunderbolt/switch.c | 2 +-
 drivers/thunderbolt/tb.h     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
drivers/thunderbolt/switch.c +1 -1
index f646bad88309..d7164843cf8b 100644
@@ -789,7 +789,7 @@ static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
 	 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are
 	 * reserved.
 	 */
-	if (port->config.type != TB_TYPE_NHI && min_hopid < TB_PATH_MIN_HOPID)
+	if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
 		min_hopid = TB_PATH_MIN_HOPID;
 
 	if (max_hopid < 0 || max_hopid > port_max_hopid)
drivers/thunderbolt/tb.h +5 -0
index a1d5de53a349..6aee18b4f53d 100644
@@ -464,6 +464,11 @@ static inline bool tb_port_is_null(const struct tb_port *port)
 	return port && port->port && port->config.type == TB_TYPE_PORT;
 }
 
+static inline bool tb_port_is_nhi(const struct tb_port *port)
+{
+	return port && port->config.type == TB_TYPE_NHI;
+}
+
 static inline bool tb_port_is_pcie_down(const struct tb_port *port)
 {
 	return port && port->config.type == TB_TYPE_PCIE_DOWN;
Signature · 1 line
2.28.0
#6 lore raw

PATCH 5/9 thunderbolt: Check for Intel vendor ID when identifying controller

In reply to Mika Westerberg

With USB4 there will be other vendors so make sure the current checks for different Intel controllers will not accidentally match those.

---
 drivers/thunderbolt/tb.h | 64 ++++++++++++++++++++++------------------
 1 file changed, 35 insertions(+), 29 deletions(-)
drivers/thunderbolt/tb.h +35 -29
index 6aee18b4f53d..664a861e8e9f 100644
@@ -697,59 +697,65 @@ static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
 
 static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
 {
-	return sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
+	return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
+	       sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
 }
 
 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
 {
-	return sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
+	return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
+	       sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
 }
 
 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
 {
-	switch (sw->config.device_id) {
-	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
-	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
-		return true;
-	default:
-		return false;
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
+		case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
+			return true;
+		}
 	}
+	return false;
 }
 
 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
 {
-	switch (sw->config.device_id) {
-	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
-		return true;
-	default:
-		return false;
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
+			return true;
+		}
 	}
+	return false;
 }
 
 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
 {
-	switch (sw->config.device_id) {
-	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
-		return true;
-	default:
-		return false;
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
+			return true;
+		}
 	}
+	return false;
 }
 
 static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
 {
-	switch (sw->config.device_id) {
-	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
-	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
-		return true;
-	default:
-		return false;
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
+		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
+			return true;
+		}
 	}
+	return false;
 }
 
 /**
Signature · 1 line
2.28.0
#7 lore raw

PATCH 6/9 thunderbolt: Introduce tb_switch_is_ice_lake()

In reply to Mika Westerberg

This is needed to differentiate Ice Lake from other controllers.

---
 drivers/thunderbolt/tb.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/thunderbolt/tb.h +12 -0
index 664a861e8e9f..1d5ee4c0de1c 100644
@@ -758,6 +758,18 @@ static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
 	return false;
 }
 
+static inline bool tb_switch_is_ice_lake(const struct tb_switch *sw)
+{
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_ICL_NHI0:
+		case PCI_DEVICE_ID_INTEL_ICL_NHI1:
+			return true;
+		}
+	}
+	return false;
+}
+
 /**
  * tb_switch_is_usb4() - Is the switch USB4 compliant
  * @sw: Switch to check
Signature · 1 line
2.28.0
#8 lore raw

PATCH 7/9 thunderbolt: Introduce tb_switch_is_tiger_lake()

In reply to Mika Westerberg

From: Gil Fine <[email protected]>

This is needed to differentiate Tiger Lake from other controllers.

---
 drivers/thunderbolt/tb.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/thunderbolt/tb.h +12 -0
index 1d5ee4c0de1c..3035258b3afa 100644
@@ -770,6 +770,18 @@ static inline bool tb_switch_is_ice_lake(const struct tb_switch *sw)
 	return false;
 }
 
+static inline bool tb_switch_is_tiger_lake(const struct tb_switch *sw)
+{
+	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
+		switch (sw->config.device_id) {
+		case PCI_DEVICE_ID_INTEL_TGL_NHI0:
+		case PCI_DEVICE_ID_INTEL_TGL_NHI1:
+			return true;
+		}
+	}
+	return false;
+}
+
 /**
  * tb_switch_is_usb4() - Is the switch USB4 compliant
  * @sw: Switch to check
Signature · 1 line
2.28.0
#9 lore raw

PATCH 8/9 thunderbolt: No need to warn in TB_CFG_ERROR_INVALID_CONFIG_SPACE

In reply to Mika Westerberg

This may be returned for example when accessing some of the vendor specific capabilities. It is not fatal by any means so instead of WARN() just log it as debug level. The caller gets error back anyway and is expected to handle it accordingly.

---
 drivers/thunderbolt/ctl.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/thunderbolt/ctl.c +2 -3
index f77ceae5c7d7..38e5e296fe3f 100644
@@ -266,9 +266,8 @@ static void tb_cfg_print_error(struct tb_ctl *ctl,
 		 * Invalid cfg_space/offset/length combination in
 		 * cfg_read/cfg_write.
 		 */
-		tb_ctl_WARN(ctl,
-			"CFG_ERROR(%llx:%x): Invalid config space or offset\n",
-			res->response_route, res->response_port);
+		tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
+			   res->response_route, res->response_port);
 		return;
 	case TB_CFG_ERROR_NO_SUCH_PORT:
 		/*
Signature · 1 line
2.28.0
#10 lore raw

PATCH 9/9 thunderbolt: Add debugfs interface

In reply to Mika Westerberg

From: Gil Fine <[email protected]>

This adds debugfs interface that can be used for debugging possible issues in hardware/software. It exposes router and adapter config spaces through files like this:

/sys/kernel/debug/thunderbolt/<DEVICE>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/path /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/counters /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/path /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/counters
...

The "regs" is either the router or port configuration space register dump. The "path" is the port path configuration space and "counters" is the optional counters configuration space.

These files contains one register per line so it should be easy to use normal filtering tools to find the registers of interest if needed.

The router and adapter regs file becomes writable when CONFIG_USB4_DEBUGFS_WRITE is enabled (which is not supposed to be done in production systems) and in this case the developer can write "offset value" lines there to modify the hardware directly. For convenience this also supports the long format the read side produces (but ignores the additional fields). The counters file can be written even when CONFIG_USB4_DEBUGFS_WRITE is not enabled and it is only used to clear the counter values.

---
 drivers/thunderbolt/Kconfig   |  10 +
 drivers/thunderbolt/Makefile  |   1 +
 drivers/thunderbolt/debugfs.c | 700 ++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/domain.c  |  13 +-
 drivers/thunderbolt/switch.c  |   3 +
 drivers/thunderbolt/tb.h      |  14 +
 drivers/thunderbolt/tb_regs.h |   4 +-
 7 files changed, 742 insertions(+), 3 deletions(-)
 create mode 100644 drivers/thunderbolt/debugfs.c
drivers/thunderbolt/Kconfig +10 -0
index 354e61c0f2e5..2257c22f8ab3 100644
@@ -16,6 +16,16 @@ menuconfig USB4
 	  To compile this driver a module, choose M here. The module will be
 	  called thunderbolt.
 
+config USB4_DEBUGFS_WRITE
+	bool "Enable write by debugfs to configuration spaces (DANGEROUS)"
+	depends on USB4
+	help
+	  Enables writing to device configuration registers through
+	  debugfs interface.
+
+	  Only enable this if you know what you are doing! Never enable
+	  this for production systems or distro kernels.
+
 config USB4_KUNIT_TEST
 	bool "KUnit tests"
 	depends on KUNIT=y
drivers/thunderbolt/Makefile +1 -0
index 754a529aa132..61d5dff445b6 100644
@@ -5,5 +5,6 @@ thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o lc.o tmu.o us
 thunderbolt-objs += nvm.o retimer.o quirks.o
 
 thunderbolt-${CONFIG_ACPI} += acpi.o
+thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
 
 obj-${CONFIG_USB4_KUNIT_TEST} += test.o
drivers/thunderbolt/debugfs.c new file +700 -0

700 lines not on this page · raw message

drivers/thunderbolt/domain.c +11 -2
index 7ca6a2b34ddc..753f98c25a3b 100644
@@ -800,12 +800,20 @@ int tb_domain_init(void)
 {
 	int ret;
 
+	tb_debugfs_init();
 	ret = tb_xdomain_init();
 	if (ret)
-		return ret;
+		goto err_debugfs;
 	ret = bus_register(&tb_bus_type);
 	if (ret)
-		tb_xdomain_exit();
+		goto err_xdomain;
+
+	return 0;
+
+err_xdomain:
+	tb_xdomain_exit();
+err_debugfs:
+	tb_debugfs_exit();
 
 	return ret;
 }
@@ -816,4 +824,5 @@ void tb_domain_exit(void)
 	ida_destroy(&tb_domain_ida);
 	tb_nvm_exit();
 	tb_xdomain_exit();
+	tb_debugfs_exit();
 }
drivers/thunderbolt/switch.c +3 -0
index d7164843cf8b..6b246945429f 100644
@@ -2517,6 +2517,7 @@ int tb_switch_add(struct tb_switch *sw)
 		pm_request_autosuspend(&sw->dev);
 	}
 
+	tb_switch_debugfs_init(sw);
 	return 0;
 }
 
@@ -2532,6 +2533,8 @@ void tb_switch_remove(struct tb_switch *sw)
 {
 	struct tb_port *port;
 
+	tb_switch_debugfs_remove(sw);
+
 	if (sw->rpm) {
 		pm_runtime_get_sync(&sw->dev);
 		pm_runtime_disable(&sw->dev);
drivers/thunderbolt/tb.h +14 -0
index 3035258b3afa..450f9cf16005 100644
@@ -125,6 +125,7 @@ struct tb_switch_tmu {
  * @rpm: The switch supports runtime PM
  * @authorized: Whether the switch is authorized by user or policy
  * @security_level: Switch supported security level
+ * @debugfs_dir: Pointer to the debugfs structure
  * @key: Contains the key used to challenge the device or %NULL if not
  *	 supported. Size of the key is %TB_SWITCH_KEY_SIZE.
  * @connection_id: Connection ID used with ICM messaging
@@ -166,6 +167,7 @@ struct tb_switch {
 	bool rpm;
 	unsigned int authorized;
 	enum tb_security_level security_level;
+	struct dentry *debugfs_dir;
 	u8 *key;
 	u8 connection_id;
 	u8 connection_key;
@@ -1010,4 +1012,16 @@ void tb_acpi_add_links(struct tb_nhi *nhi);
 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
 #endif
 
+#ifdef CONFIG_DEBUG_FS
+void tb_debugfs_init(void);
+void tb_debugfs_exit(void);
+void tb_switch_debugfs_init(struct tb_switch *sw);
+void tb_switch_debugfs_remove(struct tb_switch *sw);
+#else
+static inline void tb_debugfs_init(void) { }
+static inline void tb_debugfs_exit(void) { }
+static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
+static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
+#endif
+
 #endif
drivers/thunderbolt/tb_regs.h +3 -1
index c33751be0f56..e7d9529822fa 100644
@@ -39,6 +39,7 @@ enum tb_switch_vse_cap {
 
 enum tb_port_cap {
 	TB_PORT_CAP_PHY			= 0x01,
+	TB_PORT_CAP_POWER		= 0x02,
 	TB_PORT_CAP_TIME1		= 0x03,
 	TB_PORT_CAP_ADAP		= 0x04,
 	TB_PORT_CAP_VSE			= 0x05,
@@ -252,7 +253,8 @@ struct tb_regs_port_header {
 	/* DWORD 1 */
 	u32 first_cap_offset:8;
 	u32 max_counters:11;
-	u32 __unknown1:5;
+	u32 counters_support:1;
+	u32 __unknown1:4;
 	u32 revision:8;
 	/* DWORD 2 */
 	enum tb_port_type type:24;
Signature · 1 line
2.28.0
maintainer Reviewed-by on v1 9/9
#11 lore raw

Re: [PATCH 9/9] thunderbolt: Add debugfs interface

Quoted 122 lines · Mika Westerberg

From: Gil Fine <[email protected]>

This adds debugfs interface that can be used for debugging possible issues in hardware/software. It exposes router and adapter config spaces through files like this:

/sys/kernel/debug/thunderbolt/<DEVICE>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/path /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT1>/counters /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/regs /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/path /sys/kernel/debug/thunderbolt/<DEVICE>/<PORT2>/counters
...

The "regs" is either the router or port configuration space register dump. The "path" is the port path configuration space and "counters" is the optional counters configuration space.

These files contains one register per line so it should be easy to use normal filtering tools to find the registers of interest if needed.

The router and adapter regs file becomes writable when CONFIG_USB4_DEBUGFS_WRITE is enabled (which is not supposed to be done in production systems) and in this case the developer can write "offset value" lines there to modify the hardware directly. For convenience this also supports the long format the read side produces (but ignores the additional fields). The counters file can be written even when CONFIG_USB4_DEBUGFS_WRITE is not enabled and it is only used to clear the counter values.

---
 drivers/thunderbolt/Kconfig   |  10 +
 drivers/thunderbolt/Makefile  |   1 +
 drivers/thunderbolt/debugfs.c | 700 ++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/domain.c  |  13 +-
 drivers/thunderbolt/switch.c  |   3 +
 drivers/thunderbolt/tb.h      |  14 +
 drivers/thunderbolt/tb_regs.h |   4 +-
 7 files changed, 742 insertions(+), 3 deletions(-)
 create mode 100644 drivers/thunderbolt/debugfs.c
drivers/thunderbolt/Kconfig +10 -0
index 354e61c0f2e5..2257c22f8ab3 100644
@@ -16,6 +16,16 @@ menuconfig USB4
 	  To compile this driver a module, choose M here. The module will be
 	  called thunderbolt.
 
+config USB4_DEBUGFS_WRITE
+	bool "Enable write by debugfs to configuration spaces (DANGEROUS)"
+	depends on USB4
+	help
+	  Enables writing to device configuration registers through
+	  debugfs interface.
+
+	  Only enable this if you know what you are doing! Never enable
+	  this for production systems or distro kernels.
+
 config USB4_KUNIT_TEST
 	bool "KUnit tests"
 	depends on KUNIT=y
drivers/thunderbolt/Makefile +1 -0
index 754a529aa132..61d5dff445b6 100644
@@ -5,5 +5,6 @@ thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o lc.o tmu.o us
 thunderbolt-objs += nvm.o retimer.o quirks.o
 
 thunderbolt-${CONFIG_ACPI} += acpi.o
+thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
 
 obj-${CONFIG_USB4_KUNIT_TEST} += test.o
drivers/thunderbolt/debugfs.c new file +40 -0
new file mode 100644
index 000000000000..fdfe6e4afbfe
@@ -0,0 +1,700 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Debugfs interface
+ *
+ * Copyright (C) 2020, Intel Corporation
+ * Authors: Gil Fine <[email protected]>
+ *	    Mika Westerberg <[email protected]>
+ */
+
+#include <linux/debugfs.h>
+#include <linux/pm_runtime.h>
+
+#include "tb.h"
+
+#define PORT_CAP_PCIE_LEN	1
+#define PORT_CAP_POWER_LEN	2
+#define PORT_CAP_LANE_LEN	3
+#define PORT_CAP_USB3_LEN	5
+#define PORT_CAP_DP_LEN		8
+#define PORT_CAP_TMU_LEN	8
+#define PORT_CAP_BASIC_LEN	9
+#define PORT_CAP_USB4_LEN	20
+
+#define SWITCH_CAP_TMU_LEN	26
+#define SWITCH_CAP_BASIC_LEN	27
+
+#define PATH_LEN		2
+
+#define COUNTER_SET_LEN		3
+
+#define DEBUGFS_ATTR(__space, __write)					\
+static int __space ## _open(struct inode *inode, struct file *file)	\
+{									\
+	return single_open(file, __space ## _show, inode->i_private);	\
+}									\
+									\
+static const struct file_operations __space ## _fops = {		\
+	.owner = THIS_MODULE,						\
+	.open = __space ## _open,					\
+	.release = single_release,					\

drivers/thunderbolt/debugfs.c line 50

Quoted 10 lines
drivers/thunderbolt/debugfs.c new file +10 -0
+	.read  = seq_read,						\
+	.write = __write,						\
+	.llseek = seq_lseek,						\
+}
+
+#define DEBUGFS_ATTR_RO(__space)					\
+	DEBUGFS_ATTR(__space, NULL)
+
+#define DEBUGFS_ATTR_RW(__space)					\
+	DEBUGFS_ATTR(__space, __space ## _write)

We do have DEFINE_SIMPLE_ATTRIBUTE() and DEFINE_DEBUGFS_ATTRIBUTE, do those work here instead of your custom macro?

Other than that, this series looks fine to me:

#12 lore raw

Re: [PATCH 9/9] thunderbolt: Add debugfs interface

Hi Greg,

Quoted 15 lines · Greg Kroah-Hartman
Quoted 15 lines
drivers/thunderbolt/debugfs.c new file +15 -0
+#define DEBUGFS_ATTR(__space, __write)					\
+static int __space ## _open(struct inode *inode, struct file *file)	\
+{									\
+	return single_open(file, __space ## _show, inode->i_private);	\
+}									\
+									\
+static const struct file_operations __space ## _fops = {		\
+	.owner = THIS_MODULE,						\
+	.open = __space ## _open,					\
+	.release = single_release,					\
+	.read  = seq_read,						\
+	.write = __write,						\
+	.llseek = seq_lseek,						\
+}
+
Quoted 8 lines

drivers/thunderbolt/debugfs.c line 50

Quoted 5 lines
drivers/thunderbolt/debugfs.c new file +5 -0
+#define DEBUGFS_ATTR_RO(__space)					\
+	DEBUGFS_ATTR(__space, NULL)
+
+#define DEBUGFS_ATTR_RW(__space)					\
+	DEBUGFS_ATTR(__space, __space ## _write)

We do have DEFINE_SIMPLE_ATTRIBUTE() and DEFINE_DEBUGFS_ATTRIBUTE, do those work here instead of your custom macro?

Unfortunately they do not work as they want u64 for the write side but we actually need to parse (offset, value) pairs here when write is enabled.

Quoted 3 lines

Other than that, this series looks fine to me:

Thanks!

#13 lore raw
In reply to Mika Westerberg
Quoted 21 lines · Mika Westerberg

Hi all,

This series adds debugfs support to the driver. This is useful when debugging different hardware/software issues as the developer can dump different config spaces of the router including adapter, path and counters config spaces. Each connected router is exposed in debugfs under thunderbolt directory, and the naming follows what we have in sysfs.

This also adds a capability to write certain registers but that needs to be enabled through Kconfig option, not supposed to be enabled by distros (or regular users).

The series is based on top of my "Power Management improvements" patches which can be viewed in the below link:

https://lore.kernel.org/linux-usb/[email protected]/

Gil Fine (2):
thunderbolt: Introduce tb_switch_is_tiger_lake()
thunderbolt: Add debugfs interface

Quoted 8 lines

Mika Westerberg (7):
thunderbolt: Move struct tb_cap_any to tb_regs.h
thunderbolt: Introduce tb_port_next_cap()
thunderbolt: Introduce tb_switch_next_cap()
thunderbolt: Introduce tb_port_is_nhi()
thunderbolt: Check for Intel vendor ID when identifying controller
thunderbolt: Introduce tb_switch_is_ice_lake()
thunderbolt: No need to warn in TB_CFG_ERROR_INVALID_CONFIG_SPACE

All applied to thunderbolt.git/next.