Skip to contentSkip to conversation

pciutils: Add utility for Lane Margining

PATCH In review Nikita Proshkin 2 participants 18 messages Dec 27, 2023 – Feb 17, 2024

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

v2 cover pciutils: Add utility for Lane Margining

PCIe Base Spec Rev 4.0 introduced new extended capability mandatory for all ports - Lane Margining at the Receiver. This new feature allows to get an approximation of the Link eye margin diagram by four points. This information shows how resistant the Link is to external influences and can be useful for link debugging and presets tuning.
Previously, this information was only available using a hardware debugger.

Patch series consists of two parts:

* Patch for lspci to add Margining registers reading. There is not much information available without issuing any margining commands, but this info is useful anyway;
* New pcilmr utility.

Margining capability assumes the exchange of commands with the device, so its support was implemented as a separate utility pcilmr.
The pcilmr allows you to test Links and supports all the features provided by the Margining capability. The utility is written using a pcilib, it is divided into a library part and a main function with arguments parsing in the pciutils root dir.
Man page is provided as well.

Utility compilation and man page preparation are integrated into the pciutils Makefile, so run make is enough to start testing the utility (16/32 GT/s Link is required though).
Utility was written with Linux in mind and was tested only on this OS.

Example utility results on different systems you can see in gist:
https://gist.github.com/bombanya/f2b15263712757ffba1a11eea011c419

Patch series is also posted as PR on pciutils github:
https://github.com/pciutils/pciutils/pull/162

Changelog:

v2:
- Replace packed structures with bitfields for margining registers parsing with BIT() and MASK() macros;
- Hardware quirks are now based on ports PCI ID instead of /proc/cpuinfo;
- Refer to the PCIe Spec properly;
- Clean up the formatting;
- Move the entire internal interface of the utility into one common header;
- Use pcilib u8/16/... types instead of types from stdint.h;
- Use common.c functions such as die() and xmalloc() as Martin suggested;
- Change utility building to the variant with linking object files separately instead of building lmr as a library.

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

Nikita Proshkin (15):
pciutils-lspci: Fix unsynchronized caches in lspci struct device and
pci struct pci_dev
pciutils: Add constants for Lane Margining at the Receiver Extended
Capability
pciutils-lspci: Add Lane Margining support to the lspci
pciutils-pcilib: Add separate file for bit manipulation functions
pciutils-pcilmr: Add functions for device checking and preparations
before main margining processes
pciutils-pcilmr: Add margining process functions
pciutils-pcilmr: Add logging functions for margining
pciutils-pcilmr: Add function for default margining results log
pciutils-pcilmr: Add utility main function
pciutils-pcilmr: Add support for unique hardware quirks
pciutils-pcilmr: Add the ability to pass multiple links to the utility
pciutils-pcilmr: Add --scan mode to search for all LMR-capable Links
pciutils-pcilmr: Add option to save margining results in csv form
pciutils-pcilmr: Add handling of situations when device reports its
MaxOffset values equal to 0
pciutils-pcilmr: Add pcilmr man page

 Makefile             |  16 +-
 lib/bitops.h         |  39 +++
 lib/header.h         |   7 +
 lib/pci.h            |   1 +
 lmr/lmr.h            | 228 +++++++++++++++++
 lmr/margin.c         | 573 +++++++++++++++++++++++++++++++++++++++++++
 lmr/margin_hw.c      | 160 ++++++++++++
 lmr/margin_log.c     | 158 ++++++++++++
 lmr/margin_results.c | 283 +++++++++++++++++++++
 ls-ecaps.c           |  22 +-
 lspci.c              |   1 +
 lspci.h              |   6 -
 pcilmr.c             | 483 ++++++++++++++++++++++++++++++++++++
 pcilmr.man           | 182 ++++++++++++++
 14 files changed, 2149 insertions(+), 10 deletions(-)
 create mode 100644 lib/bitops.h
 create mode 100644 lmr/lmr.h
 create mode 100644 lmr/margin.c
 create mode 100644 lmr/margin_hw.c
 create mode 100644 lmr/margin_log.c
 create mode 100644 lmr/margin_results.c
 create mode 100644 pcilmr.c
 create mode 100644 pcilmr.man

--
2.34.1

#1 lore raw

PATCH v2 1/15 pciutils-lspci: Fix unsynchronized caches in lspci struct device and pci struct pci_dev

lspci initializes both caches for the device to the same memory block in its scan_device function. Latter calls to config_fetch function will realloc cache in struct device, but not in struct pci_dev leading to the invalid pointer in the latter. pci_dev cache is used by pci_read_* functions, what will lead to a possible use-after-free situations.

Example:

With patch:

ls-caps.c +1 -0
index a481b16..b454843 100644
@@ -1802,6 +1802,7 @@ show_caps(struct device *d, int where)
 	      break;
 	    case PCI_CAP_ID_EXP:
 	      type = cap_express(d, where, cap);
+        struct pci_cap* test = pci_find_cap(d->dev, PCI_CAP_ID_EXP, PCI_CAP_NORMAL);
 	      can_have_ext_caps = 1;
 	      break;
 	    case PCI_CAP_ID_MSIX:

valgrind run:
valgrind ./lspci -vvvs 7:0.0

...
==22835== Invalid read of size 2

==22835==    at 0x11A90A: pci_read_word (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x11EBEC: pci_scan_caps (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x11AC00: pci_fill_info_v38 (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x11ED73: pci_find_cap (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x1126FA: show_caps (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10E860: show_device (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10BFA3: main (in /home/merlin/git/pciutils/lspci)
==22835==  Address 0x5249276 is 6 bytes inside a block of size 64 free'd
==22835==    at 0x4E0A13B: realloc (vg_replace_malloc.c:1649)
==22835==    by 0x119BCC: xrealloc (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10CD2C: config_fetch (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x110DAA: show_caps (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10E860: show_device (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10BFA3: main (in /home/merlin/git/pciutils/lspci)
==22835==  Block was alloc'd at
==22835==    at 0x4E050B5: malloc (vg_replace_malloc.c:431)
==22835==    by 0x119B9C: xmalloc (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10CE80: scan_device (in /home/merlin/git/pciutils/lspci)
==22835==    by 0x10BF0F: main (in /home/merlin/git/pciutils/lspci)

...

---
 lspci.c | 1 +
 1 file changed, 1 insertion(+)
lspci.c +1 -0
index 9452cd3..071cc11 100644
@@ -107,6 +107,7 @@ config_fetch(struct device *d, unsigned int pos, unsigned int len)
       d->config = xrealloc(d->config, d->config_bufsize);
       d->present = xrealloc(d->present, d->config_bufsize);
       memset(d->present + orig_size, 0, d->config_bufsize - orig_size);
+      pci_setup_cache(d->dev, d->config, d->dev->cache_len);
     }
   result = pci_read_block(d->dev, pos, d->config + pos, len);
   if (result)
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/15 pciutils: Add constants for Lane Margining at the Receiver Extended Capability

In reply to Nikita Proshkin
---
 lib/header.h | 7 +++++++
 1 file changed, 7 insertions(+)
lib/header.h +7 -0
index 2bace93..2141013 100644
@@ -1414,6 +1414,13 @@
 #define  PCI_DOE_STS_ERROR		0x4	/* DOE Error */
 #define  PCI_DOE_STS_OBJECT_READY	0x80000000 /* Data Object Ready */
 
+/* Lane Margining at the Receiver Extended Capability */
+#define PCI_LMR_CAPS			0x4 /* Margining Port Capabilities Register */
+#define PCI_LMR_CAPS_DRVR		0x1 /* Margining uses Driver Software */
+#define PCI_LMR_PORT_STS		0x6 /* Margining Port Status Register */
+#define PCI_LMR_PORT_STS_READY		0x1 /* Margining Ready */
+#define PCI_LMR_PORT_STS_SOFT_READY	0x2 /* Margining Software Ready */
+
 /*
  * The PCI interface treats multi-function devices as independent
  * devices.  The slot/function address of each device is encoded
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/15 pciutils-lspci: Add Lane Margining support to the lspci

In reply to Nikita Proshkin

Gather all the info available without writing to the config space. Without any commands margining capability exposes only 3 status bits to read through Margining Port Capabilities and Margining Port Status registers. It makes sense to show them anyway. For example, Margining Ready bit indicates whether the device is actually ready for the margining process.

---
 ls-ecaps.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
ls-ecaps.c +21 -1
index 6d2e7b0..9810044 100644
@@ -691,6 +691,26 @@ cap_rcec(struct device *d, int where)
     printf("\t\tAssociatedBusNumbers: %02x-%02x\n", nextbusn, lastbusn );
 }
 
+static void
+cap_lmr(struct device *d, int where)
+{
+  printf("Lane Margining at the Receiver\n");
+
+  if (verbose < 2)
+    return;
+
+  if (!config_fetch(d, where, 8))
+    return;
+
+  u16 port_caps = get_conf_word(d, where + PCI_LMR_CAPS);
+  u16 port_status = get_conf_word(d, where + PCI_LMR_PORT_STS);
+
+  printf("\t\tPortCap: Uses Driver%c\n", FLAG(port_caps, PCI_LMR_CAPS_DRVR));
+  printf("\t\tPortSta: MargReady%c MargSoftReady%c\n",
+         FLAG(port_status, PCI_LMR_PORT_STS_READY),
+         FLAG(port_status, PCI_LMR_PORT_STS_SOFT_READY));
+}
+
 static void
 cxl_range(u64 base, u64 size, int n)
 {
@@ -1610,7 +1630,7 @@ show_ext_caps(struct device *d, int type)
 	    printf("Physical Layer 16.0 GT/s <?>\n");
 	    break;
 	  case PCI_EXT_CAP_ID_LMR:
-	    printf("Lane Margining at the Receiver <?>\n");
+	    cap_lmr(d, where);
 	    break;
 	  case PCI_EXT_CAP_ID_HIER_ID:
 	    printf("Hierarchy ID <?>\n");
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/15 pciutils-pcilib: Add separate file for bit manipulation functions

In reply to Nikita Proshkin

Move several macros from lspci and add some more for operations with bit masks.

---
 Makefile     |  2 +-
 lib/bitops.h | 39 +++++++++++++++++++++++++++++++++++++++
 lib/pci.h    |  1 +
 lspci.h      |  6 ------
 4 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 lib/bitops.h
Makefile +1 -1
index 228cb56..52538e8 100644
@@ -62,7 +62,7 @@ LIBNAME=libpci
 
 -include lib/config.mk
 
-PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h
+PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h lib/bitops.h
 PCIINC_INS=lib/config.h lib/header.h lib/pci.h lib/types.h
 
 export
lib/bitops.h new file +39 -0
new file mode 100644
index 0000000..029741e
@@ -0,0 +1,39 @@
+/*
+ *	The PCI Utilities -- Decode bits and bit fields
+ *
+ *	Copyright (c) 2023 Martin Mares <[email protected]>
+ *	Copyright (c) 2023 KNS Group LLC (YADRO)
+ *
+ *	Can be freely distributed and used under the terms of the GNU GPL v2+.
+ *
+ *	SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _BITOPS_H
+#define _BITOPS_H
+
+#ifndef _PCI_LIB_H
+#error Import only from pci.h
+#endif
+
+/* Useful macros for decoding of bits and bit fields */
+
+#define FLAG(x, y) ((x & y) ? '+' : '-')
+
+// Generate mask
+
+#define BIT(at) ((u64)1 << (at))
+// Boundaries inclusive
+#define MASK(h, l)   ((((u64)1 << ((h) + 1)) - 1) & ~(((u64)1 << (l)) - 1))
+
+// Get/set from register
+
+#define BITS(x, at, width)      (((x) >> (at)) & ((1 << (width)) - 1))
+#define GET_REG_MASK(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
+#define SET_REG_MASK(reg, mask, val)                                                               \
+  (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
+
+#define TABLE(tab, x, buf)                                                                         \
+  ((x) < sizeof(tab) / sizeof((tab)[0]) ? (tab)[x] : (sprintf((buf), "??%d", (x)), (buf)))
+
+#endif
lib/pci.h +1 -0
index 2322bf7..6cc46b3 100644
@@ -17,6 +17,7 @@
 
 #include "header.h"
 #include "types.h"
+#include "bitops.h"
 
 #define PCI_LIB_VERSION 0x030a00
 
lspci.h +0 -6
index c5a9ec7..4d711a5 100644
@@ -58,12 +58,6 @@ u32 get_conf_long(struct device *d, unsigned int pos);
 word get_conf_word(struct device *d, unsigned int pos);
 byte get_conf_byte(struct device *d, unsigned int pos);
 
-/* Useful macros for decoding of bits and bit fields */
-
-#define FLAG(x,y) ((x & y) ? '+' : '-')
-#define BITS(x,at,width) (((x) >> (at)) & ((1 << (width)) - 1))
-#define TABLE(tab,x,buf) ((x) < sizeof(tab)/sizeof((tab)[0]) ? (tab)[x] : (sprintf((buf), "??%d", (x)), (buf)))
-
 /* ls-vpd.c */
 
 void cap_vpd(struct device *d);
Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 5/15 pciutils-pcilmr: Add functions for device checking and preparations before main margining processes

In reply to Nikita Proshkin

Follow the checklist from PCIe Base Spec Rev 5.0 section 4.2.13.3 "Receiver Margin Testing Requirements":
* Verify the Link is at 16 GT/s or higher data rate, in DO PM state;
* Verify that Margining Ready bit of the device is set;
* Disable the ASPM and Autonomous Speed/Width features for the duration of the test.

Also verify that Upstream Port of the Link is Function 0 of a Device, according to spec, only it must implement margining registers.

---
 lmr/lmr.h       |  55 +++++++++++++++++++
 lmr/margin_hw.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)
 create mode 100644 lmr/lmr.h
 create mode 100644 lmr/margin_hw.c
lmr/lmr.h new file +55 -0
new file mode 100644
index 0000000..67fe0b0
@@ -0,0 +1,55 @@
+/*
+ *	The PCI Utilities -- Margining utility main header
+ *
+ *	Copyright (c) 2023 KNS Group LLC (YADRO)
+ *
+ *	Can be freely distributed and used under the terms of the GNU GPL v2+.
+ *
+ *	SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _LMR_H
+#define _LMR_H
+
+#include <stdbool.h>
+
+#include "pciutils.h"
+
+/* PCI Device wrapper for margining functions */
+struct margin_dev {
+  struct pci_dev *dev;
+  int lmr_cap_addr;
+  u8 width;
+  u8 retimers_n;
+  u8 link_speed;
+
+  /* Saved Device settings to restore after margining */
+  u8 aspm;
+  bool hasd; // Hardware Autonomous Speed Disable
+  bool hawd; // Hardware Autonomous Width Disable
+};
+
+struct margin_link {
+  struct margin_dev down_port;
+  struct margin_dev up_port;
+};
+
+/* margin_hw */
+
+/* Verify that devices form the link with 16 GT/s or 32 GT/s data rate */
+bool margin_verify_link(struct pci_dev *down_port, struct pci_dev *up_port);
+
+/* Check Margining Ready bit from Margining Port Status Register */
+bool margin_check_ready_bit(struct pci_dev *dev);
+
+/* Verify link and fill wrappers */
+bool margin_fill_link(struct pci_dev *down_port, struct pci_dev *up_port,
+                      struct margin_link *wrappers);
+
+/* Disable ASPM, set Hardware Autonomous Speed/Width Disable bits */
+bool margin_prep_link(struct margin_link *link);
+
+/* Restore ASPM, Hardware Autonomous Speed/Width settings */
+void margin_restore_link(struct margin_link *link);
+
+#endif
lmr/margin_hw.c new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 6/15 pciutils-pcilmr: Add margining process functions

In reply to Nikita Proshkin

* Implement the margining flow as described in the section "Example Software Flow for Lane Margining at Receiver"
of the PCIe Base Spec Rev 5.0;
* Implement margining commands formation and response parsing according to the PCIe Base Spec Rev 5.0 table 4-26;
* Use Receiver margining parameters as described in the
PCIe Base Spec Rev 5.0 table 8-11;
* Support lane reversal and simultaneous margining of several link lanes.

---
 lmr/lmr.h    | 129 ++++++++++++
 lmr/margin.c | 539 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 668 insertions(+)
 create mode 100644 lmr/margin.c
lmr/lmr.h +129 -0
index 67fe0b0..fa627a3 100644
@@ -15,6 +15,8 @@
 
 #include "pciutils.h"
 
+#define MARGIN_STEP_MS 1000
+
 /* PCI Device wrapper for margining functions */
 struct margin_dev {
   struct pci_dev *dev;
@@ -34,6 +36,118 @@ struct margin_link {
   struct margin_dev up_port;
 };
 
+/* Specification Revision 5.0 Table 8-11 */
+struct margin_params {
+  bool ind_error_sampler;
+  bool sample_report_method;
+  bool ind_left_right_tim;
+  bool ind_up_down_volt;
+  bool volt_support;
+
+  u8 max_lanes;
+
+  u8 timing_steps;
+  u8 timing_offset;
+
+  u8 volt_steps;
+  u8 volt_offset;
+
+  u8 sample_rate_v;
+  u8 sample_rate_t;
+};
+
+/* Step Margin Execution Status - Step command response */
+enum margin_step_exec_sts {
+  MARGIN_NAK = 0, // NAK/Set up for margin
+  MARGIN_LIM,     // Too many errors (device limit)
+  MARGIN_THR      // Test threshold has been reached
+};
+
+enum margin_dir { VOLT_UP = 0, VOLT_DOWN, TIM_LEFT, TIM_RIGHT };
+
+/* Margining results of one lane of the receiver */
+struct margin_res_lane {
+  u8 lane;
+  u8 steps[4];
+  enum margin_step_exec_sts statuses[4];
+};
+
+/* Reason not to run margining test on the Link/Receiver */
+enum margin_test_status {
+  MARGIN_TEST_OK = 0,
+  MARGIN_TEST_READY_BIT,
+  MARGIN_TEST_CAPS,
+
+  // Couldn't run test
+  MARGIN_TEST_PREREQS,
+  MARGIN_TEST_ARGS_LANES,
+  MARGIN_TEST_ARGS_RECVS,
+  MARGIN_TEST_ASPM
+};
+
+/* All lanes Receiver results */
+struct margin_results {
+  u8 recvn; // Receiver Number
+  struct margin_params params;
+  bool lane_reversal;
+  u8 link_speed;
+
+  enum margin_test_status test_status;
+
+  /* Used to convert steps to physical quantity.
+     Calculated from MaxOffset and NumSteps     */
+  double tim_coef;
+  double volt_coef;
+
+  u8 lanes_n;
+  struct margin_res_lane *lanes;
+};
+
+/* pcilmr arguments */
+struct margin_args {
+  u8 steps_t;        // 0 == use NumTimingSteps
+  u8 steps_v;        // 0 == use NumVoltageSteps
+  u8 parallel_lanes; // [1; MaxLanes + 1]
+  u8 error_limit;    // [0; 63]
+  u8 recvs[6];       // Receivers Numbers
+  u8 recvs_n;        // 0 == margin all available receivers
+  u8 lanes[32];      // Lanes to Margin
+  u8 lanes_n;        // 0 == margin all available lanes
+  bool run_margin;   // Or print params only
+  u8 verbosity;      // 0 - basic;
+                     // 1 - add info about remaining time and lanes in progress during margining
+
+  u64 *steps_utility; // For ETA logging
+};
+
+/* Receiver structure */
+struct margin_recv {
+  struct margin_dev *dev;
+  u8 recvn; // Receiver Number
+  bool lane_reversal;
+  struct margin_params *params;
+
+  u8 parallel_lanes;
+  u8 error_limit;
+};
+
+struct margin_lanes_data {
+  struct margin_recv *recv;
+
+  struct margin_res_lane *results;
+  u8 *lanes_numbers;
+  u8 lanes_n;
+
+  bool ind;
+  enum margin_dir dir;
+
+  u8 steps_lane_done;
+  u8 steps_lane_total;
+  u64 *steps_utility;
+
+  u8 verbosity;
+};
+
 /* margin_hw */
 
 /* Verify that devices form the link with 16 GT/s or 32 GT/s data rate */
@@ -52,4 +166,19 @@ bool margin_prep_link(struct margin_link *link);
 /* Restore ASPM, Hardware Autonomous Speed/Width settings */
 void margin_restore_link(struct margin_link *link);
 
+/* margin */
+
+/* Fill margin_params without calling other functions */
+bool margin_read_params(struct pci_access *pacc, struct pci_dev *dev, u8 recvn,
+                        struct margin_params *params);
+
+enum margin_test_status margin_process_args(struct margin_dev *dev, struct margin_args *args);
+
+/* Awaits that args are prepared through process_args.
+   Returns number of margined Receivers through recvs_n */
+struct margin_results *margin_test_link(struct margin_link *link, struct margin_args *args,
+                                        u8 *recvs_n);
+
+void margin_free_results(struct margin_results *results, u8 results_n);
+
 #endif
lmr/margin.c new file +539 -0

539 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 7/15 pciutils-pcilmr: Add logging functions for margining

In reply to Nikita Proshkin

* Implement option to turn on/off logging for margining;
* Support systems with several PCI domains;
* margin_log_margining function prints margining in progress log using one line messages for each Receiver in the form:
"Margining - <direction> - Lanes [<current simultaneous lanes>] - ETA:
<current direction-lanes margining remaining time> Steps: <current margining steps done> Total ETA: <utility run total remaining time>".

---
 lmr/lmr.h        |  24 +++++++++
 lmr/margin.c     |  13 +++++
 lmr/margin_log.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 172 insertions(+)
 create mode 100644 lmr/margin_log.c
lmr/lmr.h +24 -0
index fa627a3..c3a5039 100644
@@ -181,4 +181,28 @@ struct margin_results *margin_test_link(struct margin_link *link, struct margin_
 
 void margin_free_results(struct margin_results *results, u8 results_n);
 
+/* margin_log */
+
+extern bool margin_global_logging;
+extern bool margin_print_domain;
+
+void margin_log(char *format, ...);
+
+/* b:d.f -> b:d.f */
+void margin_log_bdfs(struct pci_dev *down_port, struct pci_dev *up_port);
+
+/* Print Link header (bdfs, width, speed) */
+void margin_log_link(struct margin_link *link);
+
+void margin_log_params(struct margin_params *params);
+
+/* Print receiver number */
+void margin_log_recvn(struct margin_recv *recv);
+
+/* Print full info from Receiver struct */
+void margin_log_receiver(struct margin_recv *recv);
+
+/* Margining in progress log */
+void margin_log_margining(struct margin_lanes_data arg);
+
 #endif
lmr/margin.c +13 -0
index 57bef88..1f1fa2f 100644
@@ -256,6 +256,7 @@ margin_test_lanes(struct margin_lanes_data arg)
         }
 
       arg.steps_lane_done = steps_done;
+      margin_log_margining(arg);
     }
 
   for (int i = 0; i < arg.lanes_n; i++)
@@ -286,9 +287,11 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
 
   results->recvn = recvn;
   results->lanes_n = lanes_n;
+  margin_log_recvn(&recv);
 
   if (!margin_check_ready_bit(dev->dev))
     {
+      margin_log("\nMargining Ready bit is Clear.\n");
       results->test_status = MARGIN_TEST_READY_BIT;
       return false;
     }
@@ -298,6 +301,7 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
       recv.lane_reversal = true;
       if (!read_params_internal(dev, recvn, recv.lane_reversal, &params))
         {
+          margin_log("\nError during caps reading.\n");
           results->test_status = MARGIN_TEST_CAPS;
           return false;
         }
@@ -315,6 +319,8 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
   results->link_speed = dev->link_speed;
   results->test_status = MARGIN_TEST_OK;
 
+  margin_log_receiver(&recv);
+
   results->lanes = xmalloc(sizeof(struct margin_res_lane) * lanes_n);
   for (int i = 0; i < lanes_n; i++)
     {
@@ -324,6 +330,8 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
 
   if (args->run_margin)
     {
+      if (args->verbosity > 0)
+        margin_log("\n");
       struct margin_lanes_data lanes_data
         = { .recv = &recv, .verbosity = args->verbosity, .steps_utility = args->steps_utility };
 
@@ -363,6 +371,8 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
             }
           lanes_done += use_lanes;
         }
+      if (args->verbosity > 0)
+        margin_log("\n");
       if (recv.lane_reversal)
         {
           for (int i = 0; i < lanes_n; i++)
@@ -504,11 +514,14 @@ margin_test_link(struct margin_link *link, struct margin_args *args, u8 *recvs_n
   u8 receivers_n = status ? args->recvs_n : 1;
   u8 *receivers = args->recvs;
 
+  margin_log_link(link);
+
   struct margin_results *results = xmalloc(sizeof(*results) * receivers_n);
 
   if (!status)
     {
       results[0].test_status = MARGIN_TEST_ASPM;
+      margin_log("\nCouldn't disable ASPM on the given Link.\n");
     }
 
   if (status)
lmr/margin_log.c new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 8/15 pciutils-pcilmr: Add function for default margining results log

In reply to Nikita Proshkin

Lanes are rated according to the minimum/recommended values. The minimum values are taken from PCIe Base Spec Rev 5.0 section 8.4.4. 30% UI recommended value for timing is taken from NVIDIA presentation "PCIe 4.0 Mass Electrical Margins Data Collection".

Receiver lanes are called 'Weird' if all results of all receiver lanes are equal to the spec minimum value.

---
 lmr/lmr.h            |   8 +++
 lmr/margin_results.c | 159 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 167 insertions(+)
 create mode 100644 lmr/margin_results.c
lmr/lmr.h +8 -0
index c3a5039..d35b8ae 100644
@@ -17,6 +17,10 @@
 
 #define MARGIN_STEP_MS 1000
 
+#define MARGIN_TIM_MIN       20
+#define MARGIN_TIM_RECOMMEND 30
+#define MARGIN_VOLT_MIN      50
+
 /* PCI Device wrapper for margining functions */
 struct margin_dev {
   struct pci_dev *dev;
@@ -205,4 +209,8 @@ void margin_log_receiver(struct margin_recv *recv);
 /* Margining in progress log */
 void margin_log_margining(struct margin_lanes_data arg);
 
+/* margin_results */
+
+void margin_results_print_brief(struct margin_results *results, u8 recvs_n);
+
 #endif
lmr/margin_results.c new file +159 -0

159 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 9/15 pciutils-pcilmr: Add utility main function

In reply to Nikita Proshkin
---
 Makefile |  14 ++-
 pcilmr.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 306 insertions(+), 2 deletions(-)
 create mode 100644 pcilmr.c
Makefile +12 -2
index 52538e8..ee8e8ee 100644
@@ -65,9 +65,13 @@ LIBNAME=libpci
 PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h lib/bitops.h
 PCIINC_INS=lib/config.h lib/header.h lib/pci.h lib/types.h
 
+LMR=margin_hw.o margin.o margin_log.o margin_results.o
+LMROBJS=$(addprefix lmr/,$(LMR))
+LMRINC=lmr/lmr.h
+
 export
 
-all: lib/$(PCIIMPLIB) lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lspci.8 setpci.8 pcilib.7 pci.ids.5 update-pciids update-pciids.8 $(PCI_IDS)
+all: lib/$(PCIIMPLIB) lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lspci.8 setpci.8 pcilib.7 pci.ids.5 update-pciids update-pciids.8 $(PCI_IDS) pcilmr
 
 lib/$(PCIIMPLIB): $(PCIINC) force
 	$(MAKE) -C lib all
@@ -110,6 +114,12 @@ update-pciids: update-pciids.sh
 example$(EXEEXT): example.o lib/$(PCIIMPLIB)
 example.o: example.c $(PCIINC)
 
+$(LMROBJS) pcilmr.o: CFLAGS+=-I .
+$(LMROBJS): %.o: %.c $(LMRINC) $(PCIINC) pciutils.h
+
+pcilmr: pcilmr.o lib/$(PCIIMPLIB) $(LMROBJS) $(COMMON)
+pcilmr.o: pcilmr.c $(LMRINC) $(PCIINC) pciutils.h
+
 %$(EXEEXT): %.o
 	$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
 
@@ -141,7 +151,7 @@ TAGS:
 
 clean:
 	rm -f `find . -name "*~" -o -name "*.[oa]" -o -name "\#*\#" -o -name TAGS -o -name core -o -name "*.orig"`
-	rm -f update-pciids lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lib/config.* *.[578] pci.ids.gz lib/*.pc lib/*.so lib/*.so.* lib/*.dll lib/*.def lib/dllrsrc.rc *-rsrc.rc tags
+	rm -f update-pciids lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lib/config.* *.[578] pci.ids.gz lib/*.pc lib/*.so lib/*.so.* lib/*.dll lib/*.def lib/dllrsrc.rc *-rsrc.rc tags pcilmr
 	rm -rf maint/dist
 
 distclean: clean
pcilmr.c new file +294 -0

294 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 10/15 pciutils-pcilmr: Add support for unique hardware quirks

In reply to Nikita Proshkin

Make it possible to change receiver margining parameters depending on current hardware specificity.

In our tests Intel Ice Lake CPUs RC ports reported MaxVoltageOffset = 50 (RxA), which led to results several times bigger than the results of the hardware debugger.
Looks like in Intel Sapphire Rapids this was fixed, these CPU RC ports report MaxVoltageOffset = 12 (RxA). To solve the problem it was decided to hardcode Volt Offset to 12 (120 mV) for Ice Lake RC ports.

In the case of margining a specific link, only information about Downstream and Upstream ports should be sufficient to decide whether to use quirks, so the feature was implemented based on a list of devices (vendor - device - revision triples), whose problems are known.

Back to Ice Lake ports, according to Integrators List on the pci-sig site, the list of possible RC ports of Ice Lake Xeon's includes at least three more options (with ids 347B/C/D) besides the one used in this commit, but we don't have such processors to check the relevance of the MaxVoltageOffset problem for these ports.

---
 lmr/lmr.h        |  6 ++++++
 lmr/margin.c     | 16 ++++++++++++++++
 lmr/margin_hw.c  | 24 +++++++++++++++++++++++-
 lmr/margin_log.c | 16 ++++++++++++++++
 4 files changed, 61 insertions(+), 1 deletion(-)
lmr/lmr.h +6 -0
index d35b8ae..bb188fc 100644
@@ -21,6 +21,8 @@
 #define MARGIN_TIM_RECOMMEND 30
 #define MARGIN_VOLT_MIN      50
 
+enum margin_hw { MARGIN_HW_DEFAULT, MARGIN_ICE_LAKE_RC };
+
 /* PCI Device wrapper for margining functions */
 struct margin_dev {
   struct pci_dev *dev;
@@ -29,6 +31,8 @@ struct margin_dev {
   u8 retimers_n;
   u8 link_speed;
 
+  enum margin_hw hw;
+
   /* Saved Device settings to restore after margining */
   u8 aspm;
   bool hasd; // Hardware Autonomous Speed Disable
@@ -209,6 +213,8 @@ void margin_log_receiver(struct margin_recv *recv);
 /* Margining in progress log */
 void margin_log_margining(struct margin_lanes_data arg);
 
+void margin_log_hw_quirks(struct margin_recv *recv);
+
 /* margin_results */
 
 void margin_results_print_brief(struct margin_results *results, u8 recvs_n);
lmr/margin.c +16 -0
index 1f1fa2f..cc142fa 100644
@@ -127,6 +127,20 @@ margin_report_cmd(struct margin_dev *dev, u8 lane, margin_cmd cmd, margin_cmd *r
          && margin_set_cmd(dev, lane, NO_COMMAND);
 }
 
+static void
+margin_apply_hw_quirks(struct margin_recv *recv)
+{
+  switch (recv->dev->hw)
+    {
+      case MARGIN_ICE_LAKE_RC:
+        if (recv->recvn == 1)
+          recv->params->volt_offset = 12;
+        break;
+      default:
+        break;
+    }
+}
+
 static bool
 read_params_internal(struct margin_dev *dev, u8 recvn, bool lane_reversal,
                      struct margin_params *params)
@@ -311,6 +325,8 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
 
   if (recv.parallel_lanes > params.max_lanes + 1)
     recv.parallel_lanes = params.max_lanes + 1;
+  margin_apply_hw_quirks(&recv);
+  margin_log_hw_quirks(&recv);
 
   results->tim_coef = (double)params.timing_offset / (double)params.timing_steps;
   results->volt_coef = (double)params.volt_offset / (double)params.volt_steps * 10.0;
lmr/margin_hw.c +23 -1
index c000132..fc427c8 100644
@@ -10,6 +10,27 @@
 
 #include "lmr.h"
 
+static u16 special_hw[][4] =
+  // Vendor ID, Device ID, Revision ID, margin_hw
+  { { 0x8086, 0x347A, 0x4, MARGIN_ICE_LAKE_RC }, 
+    { 0xFFFF, 0, 0, MARGIN_HW_DEFAULT } 
+  };
+
+static enum margin_hw
+detect_unique_hw(struct pci_dev *dev)
+{
+  u16 vendor = pci_read_word(dev, PCI_VENDOR_ID);
+  u16 device = pci_read_word(dev, PCI_DEVICE_ID);
+  u8 revision = pci_read_byte(dev, PCI_REVISION_ID);
+
+  for (int i = 0; special_hw[i][0] != 0xFFFF; i++)
+    {
+      if (vendor == special_hw[i][0] && device == special_hw[i][1] && revision == special_hw[i][2])
+        return special_hw[i][3];
+    }
+  return MARGIN_HW_DEFAULT;
+}
+
 bool
 margin_verify_link(struct pci_dev *down_port, struct pci_dev *up_port)
 {
@@ -56,7 +77,8 @@ fill_dev_wrapper(struct pci_dev *dev)
         .retimers_n
         = (!!(pci_read_word(dev, cap->addr + PCI_EXP_LNKSTA2) & PCI_EXP_LINKSTA2_RETIMER))
           + (!!(pci_read_word(dev, cap->addr + PCI_EXP_LNKSTA2) & PCI_EXP_LINKSTA2_2RETIMERS)),
-        .link_speed = (pci_read_word(dev, cap->addr + PCI_EXP_LNKSTA) & PCI_EXP_LNKSTA_SPEED) };
+        .link_speed = (pci_read_word(dev, cap->addr + PCI_EXP_LNKSTA) & PCI_EXP_LNKSTA_SPEED),
+        .hw = detect_unique_hw(dev) };
   return res;
 }
 
lmr/margin_log.c +16 -0
index 6833d1a..a93e07b 100644
@@ -133,3 +133,19 @@ margin_log_margining(struct margin_lanes_data arg)
       fflush(stdout);
     }
 }
+
+void
+margin_log_hw_quirks(struct margin_recv *recv)
+{
+  switch (recv->dev->hw)
+    {
+      case MARGIN_ICE_LAKE_RC:
+        if (recv->recvn == 1)
+          margin_log("\nRx(A) is Intel Ice Lake RC port.\n"
+                     "Applying next quirks for margining process:\n"
+                     "  - Set MaxVoltageOffset to 12 (120 mV).\n");
+        break;
+      default:
+        break;
+    }
+}
Signature · 1 line
2.34.1
#11 lore raw

PATCH v2 11/15 pciutils-pcilmr: Add the ability to pass multiple links to the utility

In reply to Nikita Proshkin

* Add support for different utility modes;
* Make the default (now --margin) mode capable to accept several components and run test for all of them;
* Add --full mode for sequential start of the test on all ready links in the system;
* The complication of the main function is due to the need to pre-read the parameters of the devices before starting the tests in order to calculate Total ETA of the utility.

---
 pcilmr.c | 243 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 189 insertions(+), 54 deletions(-)
pcilmr.c +189 -54
index 3634c97..3c2f250 100644
@@ -17,11 +17,17 @@
 
 const char program_name[] = "pcilmr";
 
+enum mode { MARGIN, FULL };
+
 static const char usage_msg[]
   = "Usage:\n"
-    "pcilmr [<margining options>] <downstream component>\n\n"
+    "pcilmr [--margin] [<margining options>] <downstream component> ...\n"
+    "pcilmr --full [<margining options>]\n"
     "Device Specifier:\n"
     "<device/component>:\t[<domain>:]<bus>:<dev>.<func>\n\n"
+    "Modes:\n"
+    "--margin\t\tMargin selected Links\n"
+    "--full\t\t\tMargin all ready for testing Links in the system (one by one)\n"
     "Margining options:\n\n"
     "Margining Test settings:\n"
     "-c\t\t\tPrint Device Lane Margining Capabilities only. Do not run margining.\n"
@@ -100,27 +106,59 @@ parse_csv_arg(char *arg, u8 *vals)
   return cnt;
 }
 
+static u8
+find_ready_links(struct pci_access *pacc, struct pci_dev **down_ports, struct pci_dev **up_ports,
+                 bool cnt_only)
+{
+  u8 cnt = 0;
+  for (struct pci_dev *up = pacc->devices; up; up = up->next)
+    {
+      if (pci_find_cap(up, PCI_EXT_CAP_ID_LMR, PCI_CAP_EXTENDED))
+        {
+          struct pci_dev *down = find_down_port_for_up(pacc, up);
+
+          if (down && margin_verify_link(down, up)
+              && (margin_check_ready_bit(down) || margin_check_ready_bit(up)))
+            {
+              if (!cnt_only)
+                {
+                  up_ports[cnt] = up;
+                  down_ports[cnt] = down;
+                }
+              cnt++;
+            }
+        }
+    }
+  return cnt;
+}
+
 int
 main(int argc, char **argv)
 {
   struct pci_access *pacc;
 
-  struct pci_dev *up_port;
-  struct pci_dev *down_port;
+  struct pci_dev **up_ports;
+  struct pci_dev **down_ports;
+  u8 ports_n = 0;
 
-  struct margin_link link;
+  struct margin_link *links;
+  bool *checks_status_ports;
 
   bool status = true;
+  enum mode mode;
 
-  struct margin_results *results;
-  u8 results_n;
+  /* each link has several receivers -> several results */
+  struct margin_results **results;
+  u8 *results_n;
 
-  struct margin_args args;
+  struct margin_args *args;
 
   u8 steps_t_arg = 0;
   u8 steps_v_arg = 0;
   u8 parallel_lanes_arg = 1;
   u8 error_limit = 4;
+  u8 lanes_arg[32];
+  u8 recvs_arg[6];
 
   u8 lanes_n = 0;
   u8 recvs_n = 0;
@@ -145,7 +183,29 @@ main(int argc, char **argv)
 
   margin_global_logging = true;
 
+  struct option long_options[]
+    = { { .name = "margin", .has_arg = no_argument, .flag = NULL, .val = 0 },
+        { .name = "full", .has_arg = no_argument, .flag = NULL, .val = 1 },
+        { 0, 0, 0, 0 } };
+
   int c;
+  c = getopt_long(argc, argv, ":", long_options, NULL);
+
+  switch (c)
+    {
+      case -1: /* no options (strings like component are possible) */
+        /* FALLTHROUGH */
+      case 0:
+        mode = MARGIN;
+        break;
+      case 1:
+        mode = FULL;
+        break;
+      default: /* unknown option symbol */
+        mode = MARGIN;
+        optind--;
+        break;
+    }
 
   while ((c = getopt(argc, argv, ":r:e:l:cp:t:v:VT")) != -1)
     {
@@ -170,20 +230,22 @@ main(int argc, char **argv)
             run_margin = false;
             break;
           case 'l':
-            lanes_n = parse_csv_arg(optarg, args.lanes);
+            lanes_n = parse_csv_arg(optarg, lanes_arg);
             break;
           case 'e':
             error_limit = atoi(optarg);
             break;
           case 'r':
-            recvs_n = parse_csv_arg(optarg, args.recvs);
+            recvs_n = parse_csv_arg(optarg, recvs_arg);
             break;
           default:
             die("Invalid arguments\n\n%s", usage_msg);
         }
     }
 
-  if (optind != argc - 1)
+  if (mode == FULL && optind != argc)
+    status = false;
+  if (mode == MARGIN && optind == argc)
     status = false;
   if (!status && argc > 1)
     die("Invalid arguments\n\n%s", usage_msg);
@@ -193,59 +255,91 @@ main(int argc, char **argv)
       exit(0);
     }
 
-  up_port = dev_for_filter(pacc, argv[argc - 1]);
+  if (mode == FULL)
+    {
+      ports_n = find_ready_links(pacc, NULL, NULL, true);
+      if (ports_n == 0)
+        {
+          die("Links not found or you don't have enough privileges.\n");
+        }
+      else
+        {
+          up_ports = xmalloc(ports_n * sizeof(*up_ports));
+          down_ports = xmalloc(ports_n * sizeof(*down_ports));
+          find_ready_links(pacc, down_ports, up_ports, false);
+        }
+    }
+  else if (mode == MARGIN)
+    {
+      ports_n = argc - optind;
+      up_ports = xmalloc(ports_n * sizeof(*up_ports));
+      down_ports = xmalloc(ports_n * sizeof(*down_ports));
 
-  down_port = find_down_port_for_up(pacc, up_port);
-  if (!down_port)
-    die("Cannot find Upstream Component for the specified device: %s\n", argv[argc - 1]);
+      u8 cnt = 0;
+      while (optind != argc)
+        {
+          up_ports[cnt] = dev_for_filter(pacc, argv[optind]);
+          down_ports[cnt] = find_down_port_for_up(pacc, up_ports[cnt]);
+          if (!down_ports[cnt])
+            die("Cannot find Upstream Component for the specified device: %s\n", argv[optind]);
+          cnt++;
+          optind++;
+        }
+    }
+  else
+    die("Bug in the args parsing!\n");
 
-  if (!pci_find_cap(up_port, PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
+  if (!pci_find_cap(up_ports[0], PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
     die("Looks like you don't have enough privileges to access "
         "Device Configuration Space.\nTry to run utility as root.\n");
 
-  if (!margin_fill_link(down_port, up_port, &link))
-    {
-      printf("Link ");
-      margin_log_bdfs(down_port, up_port);
-      printf(" is not ready for margining.\n"
-             "Link data rate must be 16 GT/s or 32 GT/s.\n"
-             "Downstream Component must be at D0 PM state.\n");
-      status = false;
-    }
+  results = xmalloc(ports_n * sizeof(*results));
+  results_n = xmalloc(ports_n * sizeof(*results_n));
+  links = xmalloc(ports_n * sizeof(*links));
+  checks_status_ports = xmalloc(ports_n * sizeof(*checks_status_ports));
+  args = xmalloc(ports_n * sizeof(*args));
 
-  if (status)
+  for (int i = 0; i < ports_n; i++)
     {
-      args.error_limit = error_limit;
-      args.lanes_n = lanes_n;
-      args.recvs_n = recvs_n;
-      args.steps_t = steps_t_arg;
-      args.steps_v = steps_v_arg;
-      args.parallel_lanes = parallel_lanes_arg;
-      args.run_margin = run_margin;
-      args.verbosity = 1;
-      args.steps_utility = &total_steps;
+      args[i].error_limit = error_limit;
+      args[i].parallel_lanes = parallel_lanes_arg;
+      args[i].run_margin = run_margin;
+      args[i].verbosity = 1;
+      args[i].steps_t = steps_t_arg;
+      args[i].steps_v = steps_v_arg;
+      for (int j = 0; j < recvs_n; j++)
+        args[i].recvs[j] = recvs_arg[j];
+      args[i].recvs_n = recvs_n;
+      for (int j = 0; j < lanes_n; j++)
+        args[i].lanes[j] = lanes_arg[j];
+      args[i].lanes_n = lanes_n;
+      args[i].steps_utility = &total_steps;
 
       enum margin_test_status args_status;
 
-      if ((args_status = margin_process_args(&link.down_port, &args)) != MARGIN_TEST_OK)
+      if (!margin_fill_link(down_ports[i], up_ports[i], &links[i]))
         {
-          status = false;
-          margin_log_link(&link);
-          if (args_status == MARGIN_TEST_ARGS_RECVS)
-            margin_log("\nInvalid RecNums specified.\n");
-          else if (args_status == MARGIN_TEST_ARGS_LANES)
-            margin_log("\nInvalid lanes specified.\n");
+          checks_status_ports[i] = false;
+          results[i] = xmalloc(sizeof(*results[i]));
+          results[i]->test_status = MARGIN_TEST_PREREQS;
+          continue;
         }
-    }
 
-  if (status)
-    {
+      if ((args_status = margin_process_args(&links[i].down_port, &args[i])) != MARGIN_TEST_OK)
+        {
+          checks_status_ports[i] = false;
+          results[i] = xmalloc(sizeof(*results[i]));
+          results[i]->test_status = args_status;
+          continue;
+        }
+
+      checks_status_ports[i] = true;
       struct margin_params params;
 
-      for (int i = 0; i < args.recvs_n; i++)
+      for (int j = 0; j < args[i].recvs_n; j++)
         {
-          if (margin_read_params(pacc, args.recvs[i] == 6 ? up_port : down_port, args.recvs[i],
-                                 &params))
+          if (margin_read_params(pacc, args[i].recvs[j] == 6 ? up_ports[i] : down_ports[i],
+                                 args[i].recvs[j], &params))
             {
               u8 steps_t = steps_t_arg ? steps_t_arg : params.timing_steps;
               u8 steps_v = steps_v_arg ? steps_v_arg : params.volt_steps;
@@ -253,7 +347,7 @@ main(int argc, char **argv)
                                                                              parallel_lanes_arg;
 
               u8 step_multiplier
-                = args.lanes_n / parallel_recv + ((args.lanes_n % parallel_recv) > 0);
+                = args[i].lanes_n / parallel_recv + ((args[i].lanes_n % parallel_recv) > 0);
 
               total_steps += steps_t * step_multiplier;
               if (params.ind_left_right_tim)
@@ -266,13 +360,40 @@ main(int argc, char **argv)
                 }
             }
         }
+    }
 
-      results = margin_test_link(&link, &args, &results_n);
+  for (int i = 0; i < ports_n; i++)
+    {
+      if (checks_status_ports[i])
+        results[i] = margin_test_link(&links[i], &args[i], &results_n[i]);
+      else
+        {
+          results_n[i] = 1;
+          if (results[i]->test_status == MARGIN_TEST_PREREQS)
+            {
+              printf("Link ");
+              margin_log_bdfs(down_ports[i], up_ports[i]);
+              printf(" is not ready for margining.\n"
+                     "Link data rate must be 16 GT/s or 32 GT/s.\n"
+                     "Downstream Component must be at D0 PM state.\n");
+            }
+          else if (results[i]->test_status == MARGIN_TEST_ARGS_RECVS)
+            {
+              margin_log_link(&links[i]);
+              printf("\nInvalid RecNums specified.\n");
+            }
+          else if (results[i]->test_status == MARGIN_TEST_ARGS_LANES)
+            {
+              margin_log_link(&links[i]);
+              printf("\nInvalid lanes specified.\n");
+            }
+        }
+      printf("\n----\n\n");
     }
 
-  if (status && run_margin)
+  if (run_margin)
     {
-      printf("\nResults:\n");
+      printf("Results:\n");
       printf("\nPass/fail criteria:\nTiming:\n");
       printf("Minimum Offset (spec): %d %% UI\nRecommended Offset: %d %% UI\n", MARGIN_TIM_MIN,
              MARGIN_TIM_RECOMMEND);
@@ -283,11 +404,25 @@ main(int argc, char **argv)
       printf("THR -\tThe set (using the utility options) \n\tstep threshold has been reached\n\n");
       printf("Notations:\nst - steps\n\n");
 
-      margin_results_print_brief(results, results_n);
+      for (int i = 0; i < ports_n; i++)
+        {
+          printf("Link ");
+          margin_log_bdfs(down_ports[i], up_ports[i]);
+          printf(":\n\n");
+          margin_results_print_brief(results[i], results_n[i]);
+          printf("\n");
+        }
     }
 
-  if (status)
-    margin_free_results(results, results_n);
+  for (int i = 0; i < ports_n; i++)
+    margin_free_results(results[i], results_n[i]);
+  free(results_n);
+  free(results);
+  free(up_ports);
+  free(down_ports);
+  free(links);
+  free(checks_status_ports);
+  free(args);
 
   pci_cleanup(pacc);
   return 0;
Signature · 1 line
2.34.1
#12 lore raw

PATCH v2 12/15 pciutils-pcilmr: Add --scan mode to search for all LMR-capable Links

In reply to Nikita Proshkin
---
 pcilmr.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
pcilmr.c +41 -2
index 3c2f250..43e791d 100644
@@ -17,17 +17,19 @@
 
 const char program_name[] = "pcilmr";
 
-enum mode { MARGIN, FULL };
+enum mode { MARGIN, FULL, SCAN };
 
 static const char usage_msg[]
   = "Usage:\n"
     "pcilmr [--margin] [<margining options>] <downstream component> ...\n"
     "pcilmr --full [<margining options>]\n"
+    "pcilmr --scan\n\n"
     "Device Specifier:\n"
     "<device/component>:\t[<domain>:]<bus>:<dev>.<func>\n\n"
     "Modes:\n"
     "--margin\t\tMargin selected Links\n"
     "--full\t\t\tMargin all ready for testing Links in the system (one by one)\n"
+    "--scan\t\t\tScan for Links available for margining\n\n"
     "Margining options:\n\n"
     "Margining Test settings:\n"
     "-c\t\t\tPrint Device Lane Margining Capabilities only. Do not run margining.\n"
@@ -106,6 +108,36 @@ parse_csv_arg(char *arg, u8 *vals)
   return cnt;
 }
 
+static void
+scan_links(struct pci_access *pacc, bool only_ready)
+{
+  if (only_ready)
+    printf("Links ready for margining:\n");
+  else
+    printf("Links with Lane Margining at the Receiver capabilities:\n");
+  bool flag = true;
+  for (struct pci_dev *up = pacc->devices; up; up = up->next)
+    {
+      if (pci_find_cap(up, PCI_EXT_CAP_ID_LMR, PCI_CAP_EXTENDED))
+        {
+          struct pci_dev *down = find_down_port_for_up(pacc, up);
+
+          if (down && margin_verify_link(down, up))
+            {
+              margin_log_bdfs(down, up);
+              if (!only_ready && (margin_check_ready_bit(down) || margin_check_ready_bit(up)))
+                printf(" - Ready");
+              printf("\n");
+              flag = false;
+            }
+        }
+    }
+  if (flag)
+    printf("Links not found or you don't have enough privileges.\n");
+  pci_cleanup(pacc);
+  exit(0);
+}
+
 static u8
 find_ready_links(struct pci_access *pacc, struct pci_dev **down_ports, struct pci_dev **up_ports,
                  bool cnt_only)
@@ -185,7 +217,8 @@ main(int argc, char **argv)
 
   struct option long_options[]
     = { { .name = "margin", .has_arg = no_argument, .flag = NULL, .val = 0 },
-        { .name = "full", .has_arg = no_argument, .flag = NULL, .val = 1 },
+        { .name = "scan", .has_arg = no_argument, .flag = NULL, .val = 1 },
+        { .name = "full", .has_arg = no_argument, .flag = NULL, .val = 2 },
         { 0, 0, 0, 0 } };
 
   int c;
@@ -199,6 +232,12 @@ main(int argc, char **argv)
         mode = MARGIN;
         break;
       case 1:
+        mode = SCAN;
+        if (optind == argc)
+          scan_links(pacc, false);
+        optind--;
+        break;
+      case 2:
         mode = FULL;
         break;
       default: /* unknown option symbol */
Signature · 1 line
2.34.1
#13 lore raw

PATCH v2 13/15 pciutils-pcilmr: Add option to save margining results in csv form

In reply to Nikita Proshkin
---
 lmr/lmr.h            |   3 ++
 lmr/margin_results.c | 111 +++++++++++++++++++++++++++++++++++++++++++
 pcilmr.c             |  18 ++++++-
 3 files changed, 130 insertions(+), 2 deletions(-)
lmr/lmr.h +3 -0
index bb188fc..c9dcd69 100644
@@ -219,4 +219,7 @@ void margin_log_hw_quirks(struct margin_recv *recv);
 
 void margin_results_print_brief(struct margin_results *results, u8 recvs_n);
 
+void margin_results_save_csv(struct margin_results *results, u8 recvs_n, char *dir,
+                             struct pci_dev *up_port);
+
 #endif
lmr/margin_results.c +111 -0

114 lines not on this page · raw message

pcilmr.c +16 -2
index 43e791d..eb5d947 100644
@@ -52,7 +52,12 @@ static const char usage_msg[]
     "-v <steps>\t\tSpecify maximum number of steps for Voltage Margining.\n"
     "Use only one of -T/-t options at the same time (same for -V/-v).\n"
     "Without these options utility will use MaxSteps from Device\n"
-    "capabilities as test limit.\n\n";
+    "capabilities as test limit.\n\n"
+    "Margining Log settings:\n"
+    "-o <directory>\t\tSave margining results in csv form into the\n"
+    "\t\t\tspecified directory. Utility will generate file with the\n"
+    "\t\t\tname in form of 'lmr_<downstream component>_Rx#_<timestamp>.csv'\n"
+    "\t\t\tfor each successfully tested receiver.\n";
 
 static struct pci_dev *
 dev_for_filter(struct pci_access *pacc, char *filter)
@@ -197,6 +202,9 @@ main(int argc, char **argv)
 
   bool run_margin = true;
 
+  char *dir_for_csv = NULL;
+  bool save_csv = false;
+
   u64 total_steps = 0;
 
   pacc = pci_alloc();
@@ -246,7 +254,7 @@ main(int argc, char **argv)
         break;
     }
 
-  while ((c = getopt(argc, argv, ":r:e:l:cp:t:v:VT")) != -1)
+  while ((c = getopt(argc, argv, ":r:e:l:cp:t:v:VTo:")) != -1)
     {
       switch (c)
         {
@@ -277,6 +285,10 @@ main(int argc, char **argv)
           case 'r':
             recvs_n = parse_csv_arg(optarg, recvs_arg);
             break;
+          case 'o':
+            dir_for_csv = optarg;
+            save_csv = true;
+            break;
           default:
             die("Invalid arguments\n\n%s", usage_msg);
         }
@@ -449,6 +461,8 @@ main(int argc, char **argv)
           margin_log_bdfs(down_ports[i], up_ports[i]);
           printf(":\n\n");
           margin_results_print_brief(results[i], results_n[i]);
+          if (save_csv)
+            margin_results_save_csv(results[i], results_n[i], dir_for_csv, up_ports[i]);
           printf("\n");
         }
     }
Signature · 1 line
2.34.1
#14 lore raw

PATCH v2 14/15 pciutils-pcilmr: Add handling of situations when device reports its MaxOffset values equal to 0

In reply to Nikita Proshkin

According to spec, for the MaxTimingOffset and MaxVoltageOffset parameters 'A 0 value may be reported if the vendor chooses not to report the offset'.

Use max possible Offset value in such situations and report to the user.

---
 lmr/lmr.h            |  3 +++
 lmr/margin.c         |  9 +++++++--
 lmr/margin_log.c     |  7 +++++++
 lmr/margin_results.c | 13 +++++++++++++
 4 files changed, 30 insertions(+), 2 deletions(-)
lmr/lmr.h +3 -0
index c9dcd69..7375c33 100644
@@ -107,6 +107,9 @@ struct margin_results {
   double tim_coef;
   double volt_coef;
 
+  bool tim_off_reported;
+  bool volt_off_reported;
+
   u8 lanes_n;
   struct margin_res_lane *lanes;
 };
lmr/margin.c +7 -2
index cc142fa..0b14747 100644
@@ -328,8 +328,13 @@ margin_test_receiver(struct margin_dev *dev, u8 recvn, struct margin_args *args,
   margin_apply_hw_quirks(&recv);
   margin_log_hw_quirks(&recv);
 
-  results->tim_coef = (double)params.timing_offset / (double)params.timing_steps;
-  results->volt_coef = (double)params.volt_offset / (double)params.volt_steps * 10.0;
+  results->tim_off_reported = params.timing_offset != 0;
+  results->volt_off_reported = params.volt_offset != 0;
+  double tim_offset = results->tim_off_reported ? (double)params.timing_offset : 50.0;
+  double volt_offset = results->volt_off_reported ? (double)params.volt_offset : 50.0;
+
+  results->tim_coef = tim_offset / (double)params.timing_steps;
+  results->volt_coef = volt_offset / (double)params.volt_steps * 10.0;
 
   results->lane_reversal = recv.lane_reversal;
   results->link_speed = dev->link_speed;
lmr/margin_log.c +7 -0
index a93e07b..b3c4bd5 100644
@@ -86,6 +86,13 @@ margin_log_receiver(struct margin_recv *recv)
       margin_log("\nWarning: device uses Lane Reversal.\n");
       margin_log("However, utility uses logical lane numbers in arguments and for logging.\n");
     }
+
+  if (recv->params->timing_offset == 0)
+    margin_log("\nWarning: Vendor chose not to report the Max Timing Offset.\n"
+               "Utility will use its max possible value - 50 (50%% UI).\n");
+  if (recv->params->volt_support && recv->params->volt_offset == 0)
+    margin_log("\nWarning: Vendor chose not to report the Max Voltage Offset.\n"
+               "Utility will use its max possible value - 50 (500 mV).\n");
 }
 
 void
lmr/margin_results.c +13 -0
index aca3ab7..6d6ed29 100644
@@ -105,6 +105,19 @@ margin_results_print_brief(struct margin_results *results, u8 recvs_n)
       if (res->lane_reversal)
         printf("Rx(%X) - Lane Reversal\n", 10 + res->recvn - 1);
 
+      if (!res->tim_off_reported)
+        printf("Rx(%X) - Attention: Vendor chose not to report the Max Timing Offset.\n"
+               "Utility used its max possible value (50%% UI) for calculations of %% UI and ps.\n"
+               "Keep in mind that for timing results of this receiver only steps values are "
+               "reliable.\n\n",
+               10 + res->recvn - 1);
+      if (params.volt_support && !res->volt_off_reported)
+        printf("Rx(%X) - Attention: Vendor chose not to report the Max Voltage Offset.\n"
+               "Utility used its max possible value (500 mV) for calculations of mV.\n"
+               "Keep in mind that for voltage results of this receiver only steps values are "
+               "reliable.\n\n",
+               10 + res->recvn - 1);
+
       if (check_recv_weird(res, MARGIN_TIM_MIN, MARGIN_VOLT_MIN))
         lane_rating = WEIRD;
       else
Signature · 1 line
2.34.1
#15 lore raw

PATCH v2 15/15 pciutils-pcilmr: Add pcilmr man page

In reply to Nikita Proshkin
---
 Makefile   |   2 +-
 pcilmr.c   |   3 +-
 pcilmr.man | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 pcilmr.man
Makefile +1 -1
index ee8e8ee..cb002cb 100644
@@ -71,7 +71,7 @@ LMRINC=lmr/lmr.h
 
 export
 
-all: lib/$(PCIIMPLIB) lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lspci.8 setpci.8 pcilib.7 pci.ids.5 update-pciids update-pciids.8 $(PCI_IDS) pcilmr
+all: lib/$(PCIIMPLIB) lspci$(EXEEXT) setpci$(EXEEXT) example$(EXEEXT) lspci.8 setpci.8 pcilib.7 pci.ids.5 update-pciids update-pciids.8 $(PCI_IDS) pcilmr pcilmr.8
 
 lib/$(PCIIMPLIB): $(PCIINC) force
 	$(MAKE) -C lib all
pcilmr.c +2 -1
index eb5d947..bab2a07 100644
@@ -20,7 +20,8 @@ const char program_name[] = "pcilmr";
 enum mode { MARGIN, FULL, SCAN };
 
 static const char usage_msg[]
-  = "Usage:\n"
+  = "! Utility requires preliminary preparation of the system. Refer to the pcilmr man page !\n\n"
+    "Usage:\n"
     "pcilmr [--margin] [<margining options>] <downstream component> ...\n"
     "pcilmr --full [<margining options>]\n"
     "pcilmr --scan\n\n"
pcilmr.man new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw
In reply to Nikita Proshkin
Quoted 31 lines · On Wed, 27 Dec 2023 14:44:49 +0500 Nikita Proshkin <[email protected]> wrote:

PCIe Base Spec Rev 4.0 introduced new extended capability mandatory for all ports - Lane Margining at the Receiver. This new feature allows to get an approximation of the Link eye margin diagram by four points. This information shows how resistant the Link is to external influences and can be useful for link debugging and presets tuning.
Previously, this information was only available using a hardware debugger.

Patch series consists of two parts:

* Patch for lspci to add Margining registers reading. There is not much information available without issuing any margining commands, but this info is useful anyway;
* New pcilmr utility.

Patch series is also posted as PR on pciutils github:
https://github.com/pciutils/pciutils/pull/162

Changelog:

v2:
- Replace packed structures with bitfields for margining registers parsing with BIT() and MASK() macros;
- Hardware quirks are now based on ports PCI ID instead of /proc/cpuinfo;
- Refer to the PCIe Spec properly;
- Clean up the formatting;
- Move the entire internal interface of the utility into one common header;
- Use pcilib u8/16/... types instead of types from stdint.h;
- Use common.c functions such as die() and xmalloc() as Martin suggested;
- Change utility building to the variant with linking object files separately instead of building lmr as a library.

Quoted 2 lines

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

Gently pinging Martin.

Review v2 patches, please.
I would like to hear any requests/suggestions on them. Thanks.

Nikita Proshkin