Skip to contentSkip to conversation

PCI: Add Lane Margining at the Receiver debugfs interface

PATCH Superseded Priyank Rathod 3 participants 6 messages Oct 6 – Oct 7

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover PCI: Add Lane Margining at the Receiver debugfs interface

Lane Margining at the Receiver lets software move the sampling point of a receiver in time or in voltage while the link stays up, and read back the errors the receiver sees, to find out how much margin a link running at 16.0 GT/s or faster has. pcilmr in pciutils does this today by writing the capability registers directly from user space.

This series adds a debugfs interface for it. The kernel sets the link up for margining and puts it back afterwards:

- ASPM is turned off for the session with the existing ASPM API, Hardware Autonomous Width/Speed Disable are set on both ends, and both ends are kept runtime resumed. All of this is undone when the session ends or either end of the link is removed.
- Before each margining command, the link is checked against the state the session started in.
- User space selects the receiver and the steps; the kernel does not run sweeps or interpret results.

Patch 1 adds the interface and its documentation, patch 2 a kselftest.

The only PCI core changes are the init/exit hooks in probe.c and remove.c and their prototypes in drivers/pci/pci.h. aspm.c, pci.c and include/linux/pci.h are not changed.

---
Changes in v6:
- Add a kref on struct pci_lmr_port and use pci_lmr_end_sessions() in pci_lmr_reboot_notify() so reboot/kexec waits for in-flight commands
outside pci_lmr_mutex (preserving the port->lock -> pci_lmr_mutex lock
order) instead of skipping ports on mutex_trylock() failure.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Clarify in pcie_lmr.sh why the sysfs 'link/' check targets receiver 6
(and rename 'up' to 'child'): in PCIe terminology the Upstream Port
(receiver 6) is the child device below the Downstream Port, and in drivers/pci/pcie/aspm.c aspm_ctrl_attrs_are_visible() looks up the
link via pcie_aspm_get_link(pdev) -> pci_upstream_bridge(pdev)->link_state,
so '/sys/bus/pci/devices/<dev>/link/' is attached to the child device
below the Downstream Port, not to the Downstream Port itself.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Drop the exported pcie_get_link_endpoints(); the partner lookup is
private to margin.c (Ilpo).
- Drop pci_aspm_inhibit() and all aspm.c changes. Use
pcie_aspm_enabled(), pci_disable_link_state() and pci_force_enable_link_state(), call neither when no ASPM state is
enabled, and restore Clock PM when CLKREQ# was enabled. ASPM Control
is checked on both ends before each command. The remaining
differences after a session are documented.
- Remove the MSampleMultipleReceivers capability bit.
- One receiver per session, selected with a port-level 'receiver'
file. A session starts with receiver 1 on a Downstream Port and 6
below it, as pcilmr does; receiver 0 is not used for margining.
- Add a per-lane 'status' file with the step response and error count,
and a 'port' file with the Margining Port bits. Stop writing
Margining Port Status.
- Locking: take both device locks with pci_dev_trylock() only while a
session starts or stops, require both ends to be added, and make no
runtime PM calls under the LMR locks.
- End sessions from pci_lmr_exit() for either end of the link.
- Commands fail with -EIO after a system suspend.
- Move debugfs to pcie_lmr_<device> at the debugfs root.
- Keep per-device state in margin.c instead of struct pci_dev.
- Split the selftest into its own patch, rename it pcie_lmr, use KTAP,
and only start a session on the port named by PCIE_LMR_DEV.
- Refer to registers by name instead of specification section numbers.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Priyank Rathod (2):
PCI/LMR: Add Lane Margining at the Receiver debugfs interface selftests/pcie_lmr: Add tests for the Lane Margining debugfs interface

 Documentation/PCI/index.rst                  |    1 +
 Documentation/PCI/pcie-lmr.rst               |  162 +++
 MAINTAINERS                                  |    8 +
 drivers/pci/pci.h                            |    8 +
 drivers/pci/pcie/Kconfig                     |   11 +
 drivers/pci/pcie/Makefile                    |    1 +
 drivers/pci/pcie/margin.c                    | 1609 ++++++++++++++++++++++++++
 drivers/pci/probe.c                          |    1 +
 drivers/pci/remove.c                         |    1 +
 include/uapi/linux/pci_regs.h                |   18 +
 tools/testing/selftests/Makefile             |    1 +
 tools/testing/selftests/pcie_lmr/Makefile    |    3 +
 tools/testing/selftests/pcie_lmr/pcie_lmr.sh |  311 +++++
 13 files changed, 2135 insertions(+)

---
base-commit: 498d9f0561247549701c2af9ea1220a7138eacac
change-id: 20260831-pcie-link-endpoints-978e100d5d06

Best regards,

Signature · 1 line
Priyank Rathod <[email protected]>
#1 lore raw

PATCH v6 1/2 PCI/LMR: Add Lane Margining at the Receiver debugfs interface

Lane Margining at the Receiver moves the sampling point of a receiver away from its normal setting, in time or in voltage, while the link stays up, and reports the errors the receiver sees. It is available on links running at 16.0 GT/s or faster, through the Lane Margining at the Receiver Extended Capability.

Add a debugfs directory, pcie_lmr_<device>, for each Downstream Port and each Function 0 below a Downstream Port that has the capability.

Writing 1 to 'enable' starts a margining session on the link. ASPM is turned off with pci_disable_link_state(), Hardware Autonomous Width Disable and Hardware Autonomous Speed Disable are set on both ends, both ends are kept runtime resumed, and the port's Margining Ready bit is awaited. Writing 0, or removing either end of the link, ends the session and undoes these steps. Only one session can be active on a link.

During a session, 'receiver' selects the receiver, laneN/margin_timing and laneN/margin_voltage issue Step Margin commands, laneN/caps shows the parameters the receiver reports, and laneN/status shows the response to the last step command, including the error count. Before each command, the link is checked against the state the session started in: speed, width, Margining Ready, ASPM still off, and no system suspend since.

The kernel does not run margining sweeps or interpret the results; that is left to user space.

---
 Documentation/PCI/index.rst    |    1 +
 Documentation/PCI/pcie-lmr.rst |  162 ++++
 MAINTAINERS                    |    7 +
 drivers/pci/pci.h              |    8 +
 drivers/pci/pcie/Kconfig       |   11 +
 drivers/pci/pcie/Makefile      |    1 +
 drivers/pci/pcie/margin.c      | 1609 ++++++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c            |    1 +
 drivers/pci/remove.c           |    1 +
 include/uapi/linux/pci_regs.h  |   18 +
 10 files changed, 1819 insertions(+)
Documentation/PCI/index.rst +1 -0
index 5d720d2a415e..9170c98cbf3f 100644
@@ -20,3 +20,4 @@ PCI Bus Subsystem
    controller/index
    boot-interrupts
    tph
+   pcie-lmr
Documentation/PCI/pcie-lmr.rst new file +162 -0

162 lines not on this page · raw message

MAINTAINERS +7 -0
index 8df569bbf3a9..cc0fdf2ff7e9 100644
@@ -21292,6 +21292,13 @@ F:	Documentation/devicetree/bindings/pci/qcom,sa8255p-pcie-ep.yaml
 F:	drivers/pci/controller/dwc/pcie-qcom-common.c
 F:	drivers/pci/controller/dwc/pcie-qcom-ep.c
 
+PCIE LANE MARGINING AT THE RECEIVER (LMR)
+M:	Priyank Rathod <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/PCI/pcie-lmr.rst
+F:	drivers/pci/pcie/margin.c
+
 PCMCIA SUBSYSTEM
 M:	Dominik Brodowski <[email protected]>
 S:	Odd Fixes
drivers/pci/pci.h +8 -0
index a002fe1a3f4c..363f065c8af2 100644
@@ -772,6 +772,14 @@ static inline void pci_tsm_init(struct pci_dev *pdev) { }
 static inline void pci_tsm_destroy(struct pci_dev *pdev) { }
 #endif
 
+#ifdef CONFIG_PCIE_LMR
+void pci_lmr_init(struct pci_dev *dev);
+void pci_lmr_exit(struct pci_dev *dev);
+#else
+static inline void pci_lmr_init(struct pci_dev *dev) { }
+static inline void pci_lmr_exit(struct pci_dev *dev) { }
+#endif
+
 /**
  * pci_dev_set_io_state - Set the new error state if possible.
  *
drivers/pci/pcie/Kconfig +11 -0
index 207c2deae35f..b307460fa97c 100644
@@ -146,3 +146,14 @@ config PCIE_EDR
 	  the PCI Firmware Specification r3.2.  Enable this if you want to
 	  support hybrid DPC model which uses both firmware and OS to
 	  implement DPC.
+
+config PCIE_LMR
+	bool "PCI Express Lane Margining at the Receiver support"
+	depends on PCIEASPM && DEBUG_FS
+	help
+	  This option exposes the Lane Margining at the Receiver Extended
+	  Capability in debugfs (see Documentation/PCI/pcie-lmr.rst), so
+	  that user space can measure the timing and voltage margin of
+	  links running at 16.0 GT/s or faster.
+
+	  If unsure, say N.
drivers/pci/pcie/Makefile +1 -0
index b0b43a18c304..f7216f6a3bac 100644
@@ -14,3 +14,4 @@ obj-$(CONFIG_PCIE_PME)		+= pme.o
 obj-$(CONFIG_PCIE_DPC)		+= dpc.o
 obj-$(CONFIG_PCIE_PTM)		+= ptm.o
 obj-$(CONFIG_PCIE_EDR)		+= edr.o
+obj-$(CONFIG_PCIE_LMR)		+= margin.o
drivers/pci/pcie/margin.c new file +1609 -0

1,609 lines not on this page · raw message

drivers/pci/probe.c +1 -0
index 721daf5c5184..62c1db3a1d32 100644
@@ -2679,6 +2679,7 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	pci_rebar_init(dev);		/* Resizable BAR */
 	pci_dev3_init(dev);		/* Device 3 capabilities */
 	pci_ide_init(dev);		/* Link Integrity and Data Encryption */
+	pci_lmr_init(dev);		/* Lane Margining at the Receiver */
 
 	pcie_report_downtraining(dev);
 	pci_init_reset_methods(dev);
drivers/pci/remove.c +1 -0
index e711ac1d4e38..c858386beff3 100644
@@ -37,6 +37,7 @@ static void pci_destroy_dev(struct pci_dev *dev)
 	platform_pci_remove_wake(dev);
 	pci_doe_sysfs_teardown(dev);
 	pci_npem_remove(dev);
+	pci_lmr_exit(dev);
 
 	/*
 	 * While device is in D0 drop the device from TSM link operations
include/uapi/linux/pci_regs.h +18 -0
index facaa324bd86..47daa1cebe0e 100644
@@ -711,6 +711,8 @@
 #define  PCI_EXP_LNKCTL2_TX_MARGIN	0x0380 /* Transmit Margin */
 #define  PCI_EXP_LNKCTL2_HASD		0x0020 /* HW Autonomous Speed Disable */
 #define PCI_EXP_LNKSTA2		0x32	/* Link Status 2 */
+#define  PCI_EXP_LNKSTA2_RETIMER	0x0040 /* Retimer Presence Detected */
+#define  PCI_EXP_LNKSTA2_2RETIMERS	0x0080 /* Two Retimers Presence Detected */
 #define  PCI_EXP_LNKSTA2_FLIT		0x0400 /* Flit Mode Status */
 #define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2	0x34	/* end of v2 EPs w/ link */
 #define PCI_EXP_SLTCAP2		0x34	/* Slot Capabilities 2 */
@@ -757,6 +759,7 @@
 #define PCI_EXT_CAP_ID_VF_REBAR 0x24	/* VF Resizable BAR */
 #define PCI_EXT_CAP_ID_DLF	0x25	/* Data Link Feature */
 #define PCI_EXT_CAP_ID_PL_16GT	0x26	/* Physical Layer 16.0 GT/s */
+#define PCI_EXT_CAP_ID_LMR	0x27	/* Lane Margining at the Receiver */
 #define PCI_EXT_CAP_ID_NPEM	0x29	/* Native PCIe Enclosure Management */
 #define PCI_EXT_CAP_ID_PL_32GT  0x2A    /* Physical Layer 32.0 GT/s */
 #define PCI_EXT_CAP_ID_DOE	0x2E	/* Data Object Exchange */
@@ -1181,6 +1184,21 @@
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK		0x000000F0
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT	4
 
+/* Lane Margining at the Receiver */
+#define PCI_LMR_PORT_CAP		0x04	/* Port Capabilities */
+#define  PCI_LMR_PORT_CAP_USES_SW_READY	0x0001	/* Uses Driver Software */
+#define PCI_LMR_PORT_STS		0x06	/* Port Status */
+#define  PCI_LMR_PORT_STS_MARGIN_READY	0x0001	/* Margining Ready */
+#define  PCI_LMR_PORT_STS_SW_READY	0x0002	/* Margining Software Ready */
+#define PCI_LMR_LANE_CTRL		0x08	/* Lane Control, lane 0 */
+#define PCI_LMR_LANE_STS		0x0a	/* Lane Status, lane 0 */
+#define PCI_LMR_LANE_STRIDE		4	/* Lane N at +4*N */
+/* Fields of both Margining Lane Control and Margining Lane Status */
+#define  PCI_LMR_LANE_RX		0x0007	/* Receiver Number */
+#define  PCI_LMR_LANE_TYPE		0x0038	/* Margin Type */
+#define  PCI_LMR_LANE_USAGE		0x0040	/* Usage Model */
+#define  PCI_LMR_LANE_PAYLOAD		0xff00	/* Margin Payload */
+
 /* Physical Layer 32.0 GT/s */
 #define PCI_PL_32GT_LE_CTRL	0x20	/* Lane Equalization Control Register */
 
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#2 lore raw

Re: [PATCH v6 1/2] PCI/LMR: Add Lane Margining at the Receiver debugfs interface

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] pci/lmr: pci_lmr_no_cmd() fails due to U16_MAX strict match against Usage Model bit - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] pci/lmr: pci_lmr_no_cmd() fails due to U16_MAX strict match against Usage Model bit
- [Low] pci/lmr: Format string mismatch passing promoted int to %lu specifier --

commit 0a41b591425afa70cf540096078e9a59a614786c
Author: Priyank Rathod <[email protected]>

PCI/LMR: Add Lane Margining at the Receiver debugfs interface

Lane Margining at the Receiver moves the sampling point of a receiver away from its normal setting, in time or in voltage, while the link stays up, and reports the errors the receiver sees.

Quoted 4 lines
drivers/pci/pcie/margin.c new file +0 -0
new file mode 100644

[ ... ]

drivers/pci/pcie/margin.c line 283

Quoted 5 lines
drivers/pci/pcie/margin.c new file +5 -0
+static int pci_lmr_no_cmd(struct pci_lmr_port *port, unsigned int lane)
+{
+	u16 cmd = pci_lmr_word(0, LMR_TYPE_NO_CMD, LMR_NO_CMD);
+
+	return pci_lmr_write_wait(port, lane, cmd, U16_MAX, NULL);

[Severity: Medium]
Will this wait timeout unconditionally?

Looking at pci_lmr_write_wait() in drivers/pci/pcie/margin.c, it uses the U16_MAX mask to wait for the lane status to exactly match cmd. However, the command encodes bit 6 (Usage Model) as 0, while hardware that supports margining will always set this read-only bit to 1, as defined in include/uapi/linux/pci_regs.h:

#define PCI_LMR_LANE_USAGE 0x0040 /* Usage Model */

This seems like it would cause the exact match condition to permanently fail, leading to a timeout for all margining operations.

Quoted 1 line

+}

[ ... ]

Quoted 2 lines
drivers/pci/pcie/margin.c new file +2 -0
+static int status_show(struct seq_file *s, void *unused)
+{

[ ... ]

drivers/pci/pcie/margin.c line 1467

Quoted 4 lines
drivers/pci/pcie/margin.c new file +4 -0
+	seq_printf(s, "%s %s %lu\n",
+		   type == LMR_TYPE_TIMING ? "timing" : "voltage",
+		   pci_lmr_step_status[FIELD_GET(LMR_STEP_STATUS, payload)],
+		   FIELD_GET(LMR_STEP_ERR_COUNT, payload));

[Severity: Low]
Does this seq_printf() call result in undefined behavior on 64-bit architectures?

In status_show() within drivers/pci/pcie/margin.c, the payload variable is a u8, and FIELD_GET() evaluates to a u8, which the varargs of seq_printf() promote to a 32-bit int. However, the format specifier %lu demands an 8-byte unsigned long. Reading 8 bytes for a 4-byte argument can produce garbage output or undefined behavior.

Quoted 3 lines
drivers/pci/pcie/margin.c new file +3 -0
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v6 2/2 selftests/pcie_lmr: Add tests for the Lane Margining debugfs interface

In reply to Priyank Rathod

Add a KTAP test for the Lane Margining at the Receiver debugfs interface.

By default it only runs checks that do not start a margining session, on every pcie_lmr_* directory: the files exist, malformed input is rejected, and commands fail with ENOTCONN without a session.

If PCIE_LMR_DEV names a port, it also starts a session on that link, checks the receiver and step bounds the receiver reports, and checks that Link Control and Link Control 2 of both ends are the same after the session as before it.

---
 MAINTAINERS                                  |   1 +
 tools/testing/selftests/Makefile             |   1 +
 tools/testing/selftests/pcie_lmr/Makefile    |   3 +
 tools/testing/selftests/pcie_lmr/pcie_lmr.sh | 311 +++++++++++++++++++++++++++
 4 files changed, 316 insertions(+)
MAINTAINERS +1 -0
index cc0fdf2ff7e9..fc8c362c5d22 100644
@@ -21298,6 +21298,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/PCI/pcie-lmr.rst
 F:	drivers/pci/pcie/margin.c
+F:	tools/testing/selftests/pcie_lmr/
 
 PCMCIA SUBSYSTEM
 M:	Dominik Brodowski <[email protected]>
tools/testing/selftests/Makefile +1 -0
index 2d960626750e..c92465d3d0b5 100644
@@ -93,6 +93,7 @@ TARGETS += net/tcp_ao
 TARGETS += nolibc
 TARGETS += pci_endpoint
 TARGETS += pcie_bwctrl
+TARGETS += pcie_lmr
 TARGETS += perf_events
 TARGETS += pidfd
 TARGETS += pid_namespace
tools/testing/selftests/pcie_lmr/Makefile new file +3 -0
new file mode 100644
index 000000000000..101c90e7ee9e
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+TEST_PROGS = pcie_lmr.sh
+include ../lib.mk
tools/testing/selftests/pcie_lmr/pcie_lmr.sh new file +311 -0

311 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#4 lore raw

Re: [PATCH v6 2/2] selftests/pcie_lmr: Add tests for the Lane Margining debugfs interface

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Add a KTAP test for the Lane Margining at the Receiver debugfs interface.

By default it only runs checks that do not start a margining session, on every pcie_lmr_* directory: the files exist, malformed input is rejected, and commands fail with ENOTCONN without a session.

Quoted 6 lines

If PCIE_LMR_DEV names a port, it also starts a session on that link, checks the receiver and step bounds the receiver reports, and checks that Link Control and Link Control 2 of both ends are the same after the session as before it.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw
In reply to Priyank Rathod
Quoted 24 lines · Priyank Rathod

Lane Margining at the Receiver lets software move the sampling point of a receiver in time or in voltage while the link stays up, and read back the errors the receiver sees, to find out how much margin a link running at 16.0 GT/s or faster has. pcilmr in pciutils does this today by writing the capability registers directly from user space.

This series adds a debugfs interface for it. The kernel sets the link up for margining and puts it back afterwards:

- ASPM is turned off for the session with the existing ASPM API, Hardware Autonomous Width/Speed Disable are set on both ends, and both ends are kept runtime resumed. All of this is undone when the session ends or either end of the link is removed.
- Before each margining command, the link is checked against the state the session started in.
- User space selects the receiver and the steps; the kernel does not run sweeps or interpret results.

Patch 1 adds the interface and its documentation, patch 2 a kselftest.

The only PCI core changes are the init/exit hooks in probe.c and remove.c and their prototypes in drivers/pci/pci.h. aspm.c, pci.c and include/linux/pci.h are not changed.

Quoted 8 lines

---
Changes in v6:
- Add a kref on struct pci_lmr_port and use pci_lmr_end_sessions() in pci_lmr_reboot_notify() so reboot/kexec waits for in-flight commands
outside pci_lmr_mutex (preserving the port->lock -> pci_lmr_mutex lock
order) instead of skipping ports on mutex_trylock() failure.
- Link to v5: https://lore.kernel.org/r/[email protected]

These reposts (v4, v5, and v6 within four hours) are a little bit too fast. The pace suggests this isn't fully baked yet, and it's too much for reviewers to assimilate.

Quoted 37 lines

Changes in v5:
- Clarify in pcie_lmr.sh why the sysfs 'link/' check targets receiver 6
(and rename 'up' to 'child'): in PCIe terminology the Upstream Port
(receiver 6) is the child device below the Downstream Port, and in drivers/pci/pcie/aspm.c aspm_ctrl_attrs_are_visible() looks up the
link via pcie_aspm_get_link(pdev) -> pci_upstream_bridge(pdev)->link_state,
so '/sys/bus/pci/devices/<dev>/link/' is attached to the child device
below the Downstream Port, not to the Downstream Port itself.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Drop the exported pcie_get_link_endpoints(); the partner lookup is private to margin.c (Ilpo).
- Drop pci_aspm_inhibit() and all aspm.c changes. Use pcie_aspm_enabled(), pci_disable_link_state() and pci_force_enable_link_state(), call neither when no ASPM state is enabled, and restore Clock PM when CLKREQ# was enabled. ASPM Control is checked on both ends before each command. The remaining differences after a session are documented.
- Remove the MSampleMultipleReceivers capability bit.
- One receiver per session, selected with a port-level 'receiver' file. A session starts with receiver 1 on a Downstream Port and 6 below it, as pcilmr does; receiver 0 is not used for margining.
- Add a per-lane 'status' file with the step response and error count, and a 'port' file with the Margining Port bits. Stop writing Margining Port Status.
- Locking: take both device locks with pci_dev_trylock() only while a session starts or stops, require both ends to be added, and make no runtime PM calls under the LMR locks.
- End sessions from pci_lmr_exit() for either end of the link.
- Commands fail with -EIO after a system suspend.
- Move debugfs to pcie_lmr_<device> at the debugfs root.
- Keep per-device state in margin.c instead of struct pci_dev.
- Split the selftest into its own patch, rename it pcie_lmr, use KTAP, and only start a session on the port named by PCIE_LMR_DEV.
- Refer to registers by name instead of specification section numbers.
...