Skip to contentSkip to conversation

Fix bwctrl boot hang

PATCH Superseded Lukas Wunner 6 participants 15 messages Dec 15, 2024 – Dec 16, 2024 PCI

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix bwctrl boot hang

Here's my renewed proposal to fix the boot hang reported by Niklas when enabling the bandwidth controller on Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers.

@Niklas, could you re-test this?

I believe I've addressed all the feedback on v1, please let me know if I've missed anything.

Changes v1 -> v2:

* [PATCH 2/3] PCI: Honor Max Link Speed when determining supported speeds
* Use PCI_EXP_LNKCAP2_SLS_2_5GB as lowest bit in GENMASK() macro

    (Ilpo, Niklas).
  * Mention user-visible issues addressed by the patch in commit message
    (Bjorn).

* [PATCH 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined
* New patch to prevent invocation of malformed GENMASK(0, lowest) macro.

* [PATCH 3/3] PCI/bwctrl: Enable only if more than one speed is supported
* New patch to prevent the boot hang. This is a future-proof alternative to Niklas' patch.

Link to v1, prior discussion and Niklas' patch:

https://lore.kernel.org/r/e3386d62a766be6d0ef7138a001dabfe563cdff8.1733991971.git.lukas@wunner.de/
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/r/[email protected]/

Lukas Wunner (3):
PCI: Assume 2.5 GT/s if Max Link Speed is undefined
PCI: Honor Max Link Speed when determining supported speeds PCI/bwctrl: Enable only if more than one speed is supported

 drivers/pci/pci.c          | 13 +++++++++++--
 drivers/pci/pcie/portdrv.c |  4 +++-
 2 files changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 PCI: Assume 2.5 GT/s if Max Link Speed is undefined

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

Emit a message informing about the malformed field. Use KERN_INFO severity to minimize annoyance. This will help silicon validation engineers take note of the issue so that regular users hopefully never see it.

There is currently no known affected product, but a subsequent commit will honor the Max Link Speed field when determining supported speeds and depends on the field being well-formed. (It uses the Max Link Speed as highest bit in a GENMASK(highest, lowest) macro and if the field is zero, that would result in GENMASK(0, lowest).)

---
 drivers/pci/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/pci.c +7 -2
index 35dc9f249b86..ab0ef7b6c798 100644
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	u32 lnkcap2, lnkcap;
 	u8 speeds;
 
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}
+
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
 	 * Speeds Vector to allow using SLS Vector bit defines directly.
@@ -6244,8 +6251,6 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	if (speeds)
 		return speeds;
 
-	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-
 	/* Synthesize from the Max Link Speed field */
 	if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
 		speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH for-linus v2 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined

Quoted 40 lines · Lukas Wunner

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

Emit a message informing about the malformed field. Use KERN_INFO severity to minimize annoyance. This will help silicon validation engineers take note of the issue so that regular users hopefully never see it.

There is currently no known affected product, but a subsequent commit will honor the Max Link Speed field when determining supported speeds and depends on the field being well-formed. (It uses the Max Link Speed as highest bit in a GENMASK(highest, lowest) macro and if the field is zero, that would result in GENMASK(0, lowest).)

---
 drivers/pci/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/pci.c +7 -0
index 35dc9f249b86..ab0ef7b6c798 100644
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	u32 lnkcap2, lnkcap;
 	u8 speeds;
 
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}
+
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
 	 * Speeds Vector to allow using SLS Vector bit defines directly.

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6256

Quoted 8 lines
drivers/pci/pci.c +0 -2
@@ -6244,8 +6251,6 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	if (speeds)
 		return speeds;
 
-	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-
 	/* Synthesize from the Max Link Speed field */
 	if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
 		speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB;

I feel like this patch goes a bit against the idea of this being more future proof. Personally, I kind of expect that any future devices which may skip support for lower speeds would start with skipping 2.5 GT/s and a future PCIe spec might allow this.

In that case with the above code we end up assuming 2.5 GT/s which won't work while the Supported Link Speeds Vector could contain supported speeds with the assumption that when in doubt software relies on that (PCIe r6.2 sec 7.5.3.18) and it might even be future spec conformant.

So I think instead of assuming 2.5 GT/s I was thinking of something like the diff below (on top of this series).

Thanks
Niklas

----

drivers/pci/pci.c +6 -8
index ef5c48bda012..cfb34fa96f81 100644
@@ -6223,14 +6223,11 @@ EXPORT_SYMBOL(pcie_bandwidth_available);
 u8 pcie_get_supported_speeds(struct pci_dev *dev)
 {
 	u32 lnkcap2, lnkcap;
-	u8 speeds;
+	u8 speeds, max_bits;
 
 	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
-		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
-		return PCI_EXP_LNKCAP2_SLS_2_5GB;
-	}
+	max_bits = lnkcap & PCI_EXP_LNKCAP_SLS;
 
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
@@ -6238,10 +6235,11 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	 */
 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
 	speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS;
-
 	/* Ignore speeds higher than Max Link Speed */
-	speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS,
-			  PCI_EXP_LNKCAP2_SLS_2_5GB);
+	if (max_bits)
+		speeds &= GENMASK(max_bits, PCI_EXP_LNKCAP2_SLS_2_5GB);
+	else
+		pci_info(dev, "Undefined Max Link Speed; relying on LnkCap2\n");
 
 	/* PCIe r3.0-compliant */
 	if (speeds)
#3 lore raw

Re: [PATCH for-linus v2 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined

Quoted 5 lines · Niklas Schnelle
Quoted 4 lines · Lukas Wunner

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

[...]

Quoted 30 lines
Quoted 6 lines
drivers/pci/pci.c +0 -0
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	u32 lnkcap2, lnkcap;
 	u8 speeds;
 

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6245

Quoted 10 lines
drivers/pci/pci.c +7 -0
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}
+
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
 	 * Speeds Vector to allow using SLS Vector bit defines directly.

I feel like this patch goes a bit against the idea of this being more future proof. Personally, I kind of expect that any future devices which may skip support for lower speeds would start with skipping 2.5 GT/s and a future PCIe spec might allow this.

In that case with the above code we end up assuming 2.5 GT/s which won't work while the Supported Link Speeds Vector could contain supported speeds with the assumption that when in doubt software relies on that (PCIe r6.2 sec 7.5.3.18) and it might even be future spec conformant.

So I think instead of assuming 2.5 GT/s I was thinking of something like the diff below (on top of this series).

[...]

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6242

Quoted 12 lines
drivers/pci/pci.c +4 -3
@@ -6238,10 +6235,11 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	 */
 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
 	speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS;
-
 	/* Ignore speeds higher than Max Link Speed */
-	speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS,
-			  PCI_EXP_LNKCAP2_SLS_2_5GB);
+	if (max_bits)
+		speeds &= GENMASK(max_bits, PCI_EXP_LNKCAP2_SLS_2_5GB);
+	else
+		pci_info(dev, "Undefined Max Link Speed; relying on LnkCap2\n");

I see. Right now assuming 2.5 GT/s is the most conservative approach. We may have to revisit this once the PCIe spec does allow gaps in the Supported Link Speeds. Then again, I'm not aware of any broken devices that actually *have* an undefined Max Link Speed, so this patch is a safety measure to avoid the GENMASK() inversion in patch [2/3].

Thanks,

Lukas

Reviewed-by on v2 1/3
#4 lore raw

Re: [PATCH for-linus v2 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined

In reply to Lukas Wunner
Quoted 10 lines · On Sun, 15 Dec 2024 11:20:51 +0100 Lukas Wunner <[email protected]> wrote:

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

Emit a message informing about the malformed field. Use KERN_INFO severity to minimize annoyance. This will help silicon validation engineers take note of the issue so that regular users hopefully never see it.

Quoted 7 lines

There is currently no known affected product, but a subsequent commit will honor the Max Link Speed field when determining supported speeds and depends on the field being well-formed. (It uses the Max Link Speed as highest bit in a GENMASK(highest, lowest) macro and if the field is zero, that would result in GENMASK(0, lowest).)

Seems like this is the best we can do for this (hopefully) theoretical hardware bug.

Quoted 31 lines
---
 drivers/pci/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/pci.c +7 -2
index 35dc9f249b86..ab0ef7b6c798 100644
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	u32 lnkcap2, lnkcap;
 	u8 speeds;
 
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}
+
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
 	 * Speeds Vector to allow using SLS Vector bit defines directly.
@@ -6244,8 +6251,6 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	if (speeds)
 		return speeds;
 
-	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-
 	/* Synthesize from the Max Link Speed field */
 	if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
 		speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB;
#5 lore raw

Re: [PATCH for-linus v2 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined

In reply to Lukas Wunner
Quoted 26 lines · Lukas Wunner

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

Emit a message informing about the malformed field. Use KERN_INFO severity to minimize annoyance. This will help silicon validation engineers take note of the issue so that regular users hopefully never see it.

There is currently no known affected product, but a subsequent commit will honor the Max Link Speed field when determining supported speeds and depends on the field being well-formed. (It uses the Max Link Speed as highest bit in a GENMASK(highest, lowest) macro and if the field is zero, that would result in GENMASK(0, lowest).)

---
 drivers/pci/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/pci.c +0 -0
index 35dc9f249b86..ab0ef7b6c798 100644

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6241

Quoted 9 lines
drivers/pci/pci.c +6 -0
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	u32 lnkcap2, lnkcap;
 	u8 speeds;
 
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}

After some more thinking, I realized this is probably not a good idea (I know you got it from me :-(). IIRC, this function is also called for RCiEPs and they do not have to implement these registers. I saw supported_speeds were 0 for many devices, for most to be more precise, while developing bwctrl (none of those impacted bwctrl).

If I'm correct, that print out should trigger many times on a simple boot test so it should be easy to confirm.

Signature · 1 line
 i.
Quoted 14 lines
drivers/pci/pci.c +1 -2
+
 	/*
 	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
 	 * Speeds Vector to allow using SLS Vector bit defines directly.
@@ -6244,8 +6251,6 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	if (speeds)
 		return speeds;
 
-	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-
 	/* Synthesize from the Max Link Speed field */
 	if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
 		speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB;
#6 lore raw

Re: [PATCH for-linus v2 1/3] PCI: Assume 2.5 GT/s if Max Link Speed is undefined

In reply to Lukas Wunner
Quoted 26 lines · Lukas Wunner

Broken PCIe devices may not set any of the bits in the Link Capabilities Register's "Max Link Speed" field. Assume 2.5 GT/s in such a case, which is the lowest possible PCIe speed. It must be supported by every device per PCIe r6.2 sec 8.2.1.

Emit a message informing about the malformed field. Use KERN_INFO severity to minimize annoyance. This will help silicon validation engineers take note of the issue so that regular users hopefully never see it.

There is currently no known affected product, but a subsequent commit will honor the Max Link Speed field when determining supported speeds and depends on the field being well-formed. (It uses the Max Link Speed as highest bit in a GENMASK(highest, lowest) macro and if the field is zero, that would result in GENMASK(0, lowest).)

---
  drivers/pci/pci.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/pci.c +0 -0
index 35dc9f249b86..ab0ef7b6c798 100644

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6239

Quoted 7 lines
drivers/pci/pci.c +4 -0
@@ -6233,6 +6233,13 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
  	u32 lnkcap2, lnkcap;
  	u8 speeds;
 
+	/* A device must support 2.5 GT/s (PCIe r6.2 sec 8.2.1) */
+	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
+	if (!(lnkcap & PCI_EXP_LNKCAP_SLS)) {
+		pci_info(dev, "Undefined Max Link Speed; assume 2.5 GT/s\n");

As it's just theoretical, shouldn't it be noisier? I'm thinking at least pci_warn(). Otherwise if this goes in and stays at pci_info() it's going to be a lot easier to miss.

Whereas at least messages that are warn or err get a more thorough look at during hardware bring up.

Quoted 15 lines
drivers/pci/pci.c +3 -2
+		return PCI_EXP_LNKCAP2_SLS_2_5GB;
+	}
+
  	/*
  	 * Speeds retain the reserved 0 at LSB before PCIe Supported Link
  	 * Speeds Vector to allow using SLS Vector bit defines directly.
@@ -6244,8 +6251,6 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
  	if (speeds)
  		return speeds;
 
-	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
-
  	/* Synthesize from the Max Link Speed field */
  	if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
  		speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB;
#7 lore raw

PATCH v2 2/3 PCI: Honor Max Link Speed when determining supported speeds

In reply to Lukas Wunner

The Supported Link Speeds Vector in the Link Capabilities 2 Register indicates the *supported* link speeds. The Max Link Speed field in the Link Capabilities Register indicates the *maximum* of those speeds.

pcie_get_supported_speeds() neglects to honor the Max Link Speed field and will thus incorrectly deem higher speeds as supported. Fix it.

One user-visible issue addressed here is an incorrect value in the sysfs attribute "max_link_speed".

But the main motivation is a boot hang reported by Niklas: Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers supports 2.5-8 GT/s speeds, but indicate 2.5 GT/s as maximum. Ilpo recalls seeing this on more devices. It can be explained by the controller's Downstream Ports supporting 8 GT/s if an Endpoint is attached, but limiting to 2.5 GT/s if the port interfaces to a PCIe Adapter, in accordance with USB4 v2 sec 11.2.1:

"This section defines the functionality of an Internal PCIe Port that
interfaces to a PCIe Adapter. [...]
The Logical sub-block shall update the PCIe configuration registers with the following characteristics: [...]
Max Link Speed field in the Link Capabilities Register set to 0001b (data rate of 2.5 GT/s only).
Note: These settings do not represent actual throughput. Throughput is implementation specific and based on the USB4 Fabric performance."

The present commit is not sufficient on its own to fix Niklas' boot hang, but it is a prerequisite.

Fixes d2bd39c0456b ("PCI: Store all PCIe Supported Link Speeds")
Cc Ilpo Järvinen <[email protected]>
---
 drivers/pci/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/pci/pci.c +4 -0
index ab0ef7b6c798..9f672399e688 100644
@@ -6247,6 +6247,10 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
 	speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS;
 
+	/* Ignore speeds higher than Max Link Speed */
+	speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS,
+			  PCI_EXP_LNKCAP2_SLS_2_5GB);
+
 	/* PCIe r3.0-compliant */
 	if (speeds)
 		return speeds;
Signature · 1 line
2.43.0
Tested-by on v2 2/3
#8 lore raw

Re: [PATCH for-linus v2 2/3] PCI: Honor Max Link Speed when determining supported speeds

Quoted 9 lines · Lukas Wunner

The Supported Link Speeds Vector in the Link Capabilities 2 Register indicates the *supported* link speeds. The Max Link Speed field in the Link Capabilities Register indicates the *maximum* of those speeds.

pcie_get_supported_speeds() neglects to honor the Max Link Speed field and will thus incorrectly deem higher speeds as supported. Fix it.

One user-visible issue addressed here is an incorrect value in the sysfs attribute "max_link_speed".

Can confirm that I saw this effect on my affected Intel JHL7540 "Titan Ridge 2018" Thunderbolt controller.

Feel free to add:

Reviewed-by on v2 2/3
#9 lore raw

Re: [PATCH for-linus v2 2/3] PCI: Honor Max Link Speed when determining supported speeds

In reply to Lukas Wunner
Quoted 27 lines · On Sun, 15 Dec 2024 11:20:52 +0100 Lukas Wunner <[email protected]> wrote:

The Supported Link Speeds Vector in the Link Capabilities 2 Register indicates the *supported* link speeds. The Max Link Speed field in the Link Capabilities Register indicates the *maximum* of those speeds.

pcie_get_supported_speeds() neglects to honor the Max Link Speed field and will thus incorrectly deem higher speeds as supported. Fix it.

One user-visible issue addressed here is an incorrect value in the sysfs attribute "max_link_speed".

But the main motivation is a boot hang reported by Niklas: Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers supports 2.5-8 GT/s speeds, but indicate 2.5 GT/s as maximum. Ilpo recalls seeing this on more devices. It can be explained by the controller's Downstream Ports supporting 8 GT/s if an Endpoint is attached, but limiting to 2.5 GT/s if the port interfaces to a PCIe Adapter, in accordance with USB4 v2 sec 11.2.1:

"This section defines the functionality of an Internal PCIe Port that
interfaces to a PCIe Adapter. [...]
The Logical sub-block shall update the PCIe configuration registers with the following characteristics: [...]
Max Link Speed field in the Link Capabilities Register set to 0001b (data rate of 2.5 GT/s only).
Note: These settings do not represent actual throughput. Throughput is implementation specific and based on the USB4 Fabric performance."

Quoted 8 lines

The present commit is not sufficient on its own to fix Niklas' boot hang, but it is a prerequisite.

Fixes d2bd39c0456b ("PCI: Store all PCIe Supported Link Speeds")
Cc Ilpo Järvinen <[email protected]>

Makes sense as a hardening step, even without the oddity of thunderbolt.

#10 lore raw

Re: [PATCH for-linus v2 2/3] PCI: Honor Max Link Speed when determining supported speeds

In reply to Lukas Wunner
Quoted 44 lines · Lukas Wunner

The Supported Link Speeds Vector in the Link Capabilities 2 Register indicates the *supported* link speeds. The Max Link Speed field in the Link Capabilities Register indicates the *maximum* of those speeds.

pcie_get_supported_speeds() neglects to honor the Max Link Speed field and will thus incorrectly deem higher speeds as supported. Fix it.

One user-visible issue addressed here is an incorrect value in the sysfs attribute "max_link_speed".

But the main motivation is a boot hang reported by Niklas: Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers supports 2.5-8 GT/s speeds, but indicate 2.5 GT/s as maximum. Ilpo recalls seeing this on more devices. It can be explained by the controller's Downstream Ports supporting 8 GT/s if an Endpoint is attached, but limiting to 2.5 GT/s if the port interfaces to a PCIe Adapter, in accordance with USB4 v2 sec 11.2.1:

"This section defines the functionality of an Internal PCIe Port that
interfaces to a PCIe Adapter. [...]
The Logical sub-block shall update the PCIe configuration registers with the following characteristics: [...]
Max Link Speed field in the Link Capabilities Register set to 0001b (data rate of 2.5 GT/s only).
Note: These settings do not represent actual throughput. Throughput is implementation specific and based on the USB4 Fabric performance."

The present commit is not sufficient on its own to fix Niklas' boot hang, but it is a prerequisite.

Fixes d2bd39c0456b ("PCI: Store all PCIe Supported Link Speeds")
Cc Ilpo Järvinen <[email protected]>
---
 drivers/pci/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/pci/pci.c +0 -0
index ab0ef7b6c798..9f672399e688 100644

drivers/pci/pci.c u8 pcie_get_supported_speeds(struct pci_dev *dev) · line 6252

Quoted 6 lines
drivers/pci/pci.c +3 -0
@@ -6247,6 +6247,10 @@ u8 pcie_get_supported_speeds(struct pci_dev *dev)
 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
 	speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS;
 
+	/* Ignore speeds higher than Max Link Speed */
+	speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS,
+			  PCI_EXP_LNKCAP2_SLS_2_5GB);

You pass a value instead of bit position to GENMASK() which is not correct way to use GENMASK(). You need to do either:

			  ilog2(PCI_EXP_LNKCAP2_SLS_2_5GB)
or
			  __ffs(PCI_EXP_LNKCAP2_SLS) 

(Technically, also __ffs(PCI_EXP_LNKCAP2_SLS_2_5GB) would work).

+ Please check the correct header is included depending on which you pick.

Quoted 5 lines
drivers/pci/pci.c +1 -0
+
 	/* PCIe r3.0-compliant */
 	if (speeds)
 		return speeds;
Signature · 1 line
 i.
#11 lore raw

PATCH v2 3/3 PCI/bwctrl: Enable only if more than one speed is supported

In reply to Lukas Wunner

If a PCIe port only supports a single speed, enabling bandwidth control is pointless: There's no need to monitor autonomous speed changes, nor can the speed be changed.

Not enabling it saves a small amount of memory and compute resources, but also fixes a boot hang reported by Niklas: It occurs when enabling bandwidth control on Downstream Ports of Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers. The ports only support 2.5 GT/s in accordance with USB4 v2 sec 11.2.1, so the present commit works around the issue.

PCIe r6.2 sec 8.2.1 prescribes that:

"A device must support 2.5 GT/s and is not permitted to skip support
for any data rates between 2.5 GT/s and the highest supported rate."

Consequently, bandwidth control is currently only disabled if a port doesn't support higher speeds than 2.5 GT/s. However the Implementation Note in PCIe r6.2 sec 7.5.3.18 cautions:

"It is strongly encouraged that software primarily utilize the
Supported Link Speeds Vector instead of the Max Link Speed field, so that software can determine the exact set of supported speeds on current and future hardware. This can avoid software being confused if a future specification defines Links that do not require support for all slower speeds."

In other words, future revisions of the PCIe Base Spec may allow gaps in the Supported Link Speeds Vector. To be future-proof, don't just check whether speeds above 2.5 GT/s are supported, but rather check whether *more than one* speed is supported.

Fixes 665745f27487 ("PCI/bwctrl: Re-add BW notification portdrv as PCIe BW controller")
Cc Ilpo Järvinen <[email protected]>
---
 drivers/pci/pcie/portdrv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/portdrv.c +3 -1
index 5e10306b6308..02e73099bad0 100644
@@ -265,12 +265,14 @@ static int get_port_device_capability(struct pci_dev *dev)
 	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
 		services |= PCIE_PORT_SERVICE_DPC;
 
+	/* Enable bandwidth control if more than one speed is supported. */
 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
 	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
 		u32 linkcap;
 
 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
-		if (linkcap & PCI_EXP_LNKCAP_LBNC)
+		if (linkcap & PCI_EXP_LNKCAP_LBNC &&
+		    hweight8(dev->supported_speeds) > 1)
 			services |= PCIE_PORT_SERVICE_BWCTRL;
 	}
 
Signature · 1 line
2.43.0
Tested-by on v2 3/3
#12 lore raw

Re: [PATCH for-linus v2 3/3] PCI/bwctrl: Enable only if more than one speed is supported

Quoted 50 lines · Lukas Wunner

If a PCIe port only supports a single speed, enabling bandwidth control is pointless: There's no need to monitor autonomous speed changes, nor can the speed be changed.

Not enabling it saves a small amount of memory and compute resources, but also fixes a boot hang reported by Niklas: It occurs when enabling bandwidth control on Downstream Ports of Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers. The ports only support 2.5 GT/s in accordance with USB4 v2 sec 11.2.1, so the present commit works around the issue.

PCIe r6.2 sec 8.2.1 prescribes that:

"A device must support 2.5 GT/s and is not permitted to skip support
for any data rates between 2.5 GT/s and the highest supported rate."

Consequently, bandwidth control is currently only disabled if a port doesn't support higher speeds than 2.5 GT/s. However the Implementation Note in PCIe r6.2 sec 7.5.3.18 cautions:

"It is strongly encouraged that software primarily utilize the
Supported Link Speeds Vector instead of the Max Link Speed field, so that software can determine the exact set of supported speeds on current and future hardware. This can avoid software being confused if a future specification defines Links that do not require support for all slower speeds."

In other words, future revisions of the PCIe Base Spec may allow gaps in the Supported Link Speeds Vector. To be future-proof, don't just check whether speeds above 2.5 GT/s are supported, but rather check whether *more than one* speed is supported.

Fixes 665745f27487 ("PCI/bwctrl: Re-add BW notification portdrv as PCIe BW controller")
Cc Ilpo Järvinen <[email protected]>
---
 drivers/pci/pcie/portdrv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/portdrv.c +1 -0
index 5e10306b6308..02e73099bad0 100644
@@ -265,12 +265,14 @@ static int get_port_device_capability(struct pci_dev *dev)
 	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
 		services |= PCIE_PORT_SERVICE_DPC;
 
+	/* Enable bandwidth control if more than one speed is supported. */
 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||

drivers/pci/pcie/portdrv.c static int get_port_device_capability(struct pci_dev *dev) · line 278

Quoted 10 lines
drivers/pci/pcie/portdrv.c +2 -1
 	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
 		u32 linkcap;
 
 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
-		if (linkcap & PCI_EXP_LNKCAP_LBNC)
+		if (linkcap & PCI_EXP_LNKCAP_LBNC &&
+		    hweight8(dev->supported_speeds) > 1)
 			services |= PCIE_PORT_SERVICE_BWCTRL;
 	}
 

I can confirm that this in combination with the two other patches fixes my problem. I'm still a little unsure if we want to go with a more minimal patch for v6.13-rc to take more time to figure out the correct handling in patch 1, but I think medium term this is the right overall approach.

Either way, feel free to add:

Reviewed-by on v2 3/3
#13 lore raw

Re: [PATCH for-linus v2 3/3] PCI/bwctrl: Enable only if more than one speed is supported

In reply to Lukas Wunner
Quoted 26 lines · On Sun, 15 Dec 2024 11:20:53 +0100 Lukas Wunner <[email protected]> wrote:

If a PCIe port only supports a single speed, enabling bandwidth control is pointless: There's no need to monitor autonomous speed changes, nor can the speed be changed.

Not enabling it saves a small amount of memory and compute resources, but also fixes a boot hang reported by Niklas: It occurs when enabling bandwidth control on Downstream Ports of Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers. The ports only support 2.5 GT/s in accordance with USB4 v2 sec 11.2.1, so the present commit works around the issue.

PCIe r6.2 sec 8.2.1 prescribes that:

"A device must support 2.5 GT/s and is not permitted to skip support
for any data rates between 2.5 GT/s and the highest supported rate."

Consequently, bandwidth control is currently only disabled if a port doesn't support higher speeds than 2.5 GT/s. However the Implementation Note in PCIe r6.2 sec 7.5.3.18 cautions:

"It is strongly encouraged that software primarily utilize the
Supported Link Speeds Vector instead of the Max Link Speed field, so that software can determine the exact set of supported speeds on current and future hardware. This can avoid software being confused if a future specification defines Links that do not require support for all slower speeds."

Quoted 4 lines

In other words, future revisions of the PCIe Base Spec may allow gaps in the Supported Link Speeds Vector. To be future-proof, don't just check whether speeds above 2.5 GT/s are supported, but rather check whether *more than one* speed is supported.

It has long felt like the need for the very low speeds will become optional eventually, though the challenges of getting a backwards compatibility change into the specification may make that take a while. Hence I agree this approach makes sense.

Quoted 30 lines
Fixes 665745f27487 ("PCI/bwctrl: Re-add BW notification portdrv as PCIe BW controller")
Cc Ilpo Järvinen <[email protected]>
---
 drivers/pci/pcie/portdrv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/portdrv.c +3 -1
index 5e10306b6308..02e73099bad0 100644
@@ -265,12 +265,14 @@ static int get_port_device_capability(struct pci_dev *dev)
 	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
 		services |= PCIE_PORT_SERVICE_DPC;
 
+	/* Enable bandwidth control if more than one speed is supported. */
 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
 	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
 		u32 linkcap;
 
 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
-		if (linkcap & PCI_EXP_LNKCAP_LBNC)
+		if (linkcap & PCI_EXP_LNKCAP_LBNC &&
+		    hweight8(dev->supported_speeds) > 1)
 			services |= PCIE_PORT_SERVICE_BWCTRL;
 	}
 
Reviewed-by
#14 lore raw

Re: [PATCH for-linus v2 3/3] PCI/bwctrl: Enable only if more than one speed is supported

Quoted 34 lines · Jonathan Cameron
Quoted 26 lines · On Sun, 15 Dec 2024 11:20:53 +0100 Lukas Wunner <[email protected]> wrote:

If a PCIe port only supports a single speed, enabling bandwidth control is pointless: There's no need to monitor autonomous speed changes, nor can the speed be changed.

Not enabling it saves a small amount of memory and compute resources, but also fixes a boot hang reported by Niklas: It occurs when enabling bandwidth control on Downstream Ports of Intel JHL7540 "Titan Ridge 2018" Thunderbolt controllers. The ports only support 2.5 GT/s in accordance with USB4 v2 sec 11.2.1, so the present commit works around the issue.

PCIe r6.2 sec 8.2.1 prescribes that:

"A device must support 2.5 GT/s and is not permitted to skip support
for any data rates between 2.5 GT/s and the highest supported rate."

Consequently, bandwidth control is currently only disabled if a port doesn't support higher speeds than 2.5 GT/s. However the Implementation Note in PCIe r6.2 sec 7.5.3.18 cautions:

"It is strongly encouraged that software primarily utilize the
Supported Link Speeds Vector instead of the Max Link Speed field, so that software can determine the exact set of supported speeds on current and future hardware. This can avoid software being confused if a future specification defines Links that do not require support for all slower speeds."

Quoted 4 lines

In other words, future revisions of the PCIe Base Spec may allow gaps in the Supported Link Speeds Vector. To be future-proof, don't just check whether speeds above 2.5 GT/s are supported, but rather check whether *more than one* speed is supported.

Quoted 6 lines

It has long felt like the need for the very low speeds will become optional eventually, though the challenges of getting a backwards compatibility change into the specification may make that take a while. Hence I agree this approach makes sense.

Quoted 32 lines
Quoted 31 lines
Fixes 665745f27487 ("PCI/bwctrl: Re-add BW notification portdrv as PCIe BW controller")
Cc Ilpo Järvinen <[email protected]>
---
  drivers/pci/pcie/portdrv.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/portdrv.c +3 -1
index 5e10306b6308..02e73099bad0 100644
@@ -265,12 +265,14 @@ static int get_port_device_capability(struct pci_dev *dev)
  	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
  		services |= PCIE_PORT_SERVICE_DPC;
 
+	/* Enable bandwidth control if more than one speed is supported. */
  	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
  	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
  		u32 linkcap;
 
  		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
-		if (linkcap & PCI_EXP_LNKCAP_LBNC)
+		if (linkcap & PCI_EXP_LNKCAP_LBNC &&
+		    hweight8(dev->supported_speeds) > 1)
  			services |= PCIE_PORT_SERVICE_BWCTRL;
  	}