]> mj.ucw.cz Git - pciutils.git/blob - lmr/lmr.h
pcilmr: Add option to configure margining dwell time
[pciutils.git] / lmr / lmr.h
1 /*
2  *      The PCI Utilities -- Margining utility main header
3  *
4  *      Copyright (c) 2023-2024 KNS Group LLC (YADRO)
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #ifndef _LMR_H
12 #define _LMR_H
13
14 #include <stdbool.h>
15
16 #include "pciutils.h"
17
18 enum margin_hw { MARGIN_HW_DEFAULT, MARGIN_ICE_LAKE_RC };
19
20 // in ps
21 static const double margin_ui[] = { 62.5, 31.25 };
22
23 /* PCI Device wrapper for margining functions */
24 struct margin_dev {
25   struct pci_dev *dev;
26   int lmr_cap_addr;
27   u8 width;
28   u8 retimers_n;
29   u8 link_speed;
30
31   enum margin_hw hw;
32
33   /* Saved Device settings to restore after margining */
34   u8 aspm;
35   bool hasd; // Hardware Autonomous Speed Disable
36   bool hawd; // Hardware Autonomous Width Disable
37 };
38
39 /* Specification Revision 5.0 Table 8-11 */
40 struct margin_params {
41   bool ind_error_sampler;
42   bool sample_report_method;
43   bool ind_left_right_tim;
44   bool ind_up_down_volt;
45   bool volt_support;
46
47   u8 max_lanes;
48
49   u8 timing_steps;
50   u8 timing_offset;
51
52   u8 volt_steps;
53   u8 volt_offset;
54
55   u8 sample_rate_v;
56   u8 sample_rate_t;
57 };
58
59 /* Step Margin Execution Status - Step command response */
60 enum margin_step_exec_sts {
61   MARGIN_NAK = 0, // NAK/Set up for margin
62   MARGIN_LIM,     // Too many errors (device limit)
63   MARGIN_THR      // Test threshold has been reached
64 };
65
66 enum margin_dir { VOLT_UP = 0, VOLT_DOWN, TIM_LEFT, TIM_RIGHT };
67
68 /* Margining results of one lane of the receiver */
69 struct margin_res_lane {
70   u8 lane;
71   u8 steps[4];
72   enum margin_step_exec_sts statuses[4];
73 };
74
75 /* Reason not to run margining test on the Link/Receiver */
76 enum margin_test_status {
77   MARGIN_TEST_OK = 0,
78   MARGIN_TEST_READY_BIT,
79   MARGIN_TEST_CAPS,
80
81   // Couldn't run test
82   MARGIN_TEST_PREREQS,
83   MARGIN_TEST_ARGS_LANES,
84   MARGIN_TEST_ARGS_RECVS,
85   MARGIN_TEST_ASPM
86 };
87
88 /* All lanes Receiver results */
89 struct margin_results {
90   u8 recvn; // Receiver Number; from 1 to 6
91   struct margin_params params;
92   bool lane_reversal;
93   u8 link_speed;
94
95   enum margin_test_status test_status;
96
97   /* Used to convert steps to physical quantity.
98      Calculated from MaxOffset and NumSteps     */
99   double tim_coef; // from steps to % UI
100   double volt_coef;
101
102   bool tim_off_reported;
103   bool volt_off_reported;
104
105   u8 lanes_n;
106   struct margin_res_lane *lanes;
107 };
108
109 /* pcilmr arguments */
110
111 // Common args
112 struct margin_com_args {
113   u8 error_limit;    // [0; 63]
114   bool run_margin;   // Or print params only
115   u8 verbosity;      // 0 - basic;
116                      // 1 - add info about remaining time and lanes in progress during margining
117   u64 steps_utility; // For ETA logging
118   bool save_csv;
119   char *dir_for_csv;
120   u8 dwell_time;
121 };
122
123 struct margin_recv_args {
124   // Grading options
125   struct {
126     bool valid;
127     double criteria; // in ps/mV
128     bool one_side_is_whole;
129   } t, v;
130 };
131
132 struct margin_link_args {
133   struct margin_com_args *common;
134   u8 steps_t;        // 0 == use NumTimingSteps
135   u8 steps_v;        // 0 == use NumVoltageSteps
136   u8 parallel_lanes; // [1; MaxLanes + 1]
137   u8 recvs[6];       // Receivers Numbers
138   u8 recvs_n;        // 0 == margin all available receivers
139   struct margin_recv_args recv_args[6];
140   u8 lanes[32]; // Lanes to Margin
141   u8 lanes_n;   // 0 == margin all available lanes
142 };
143
144 struct margin_link {
145   struct margin_dev down_port;
146   struct margin_dev up_port;
147   struct margin_link_args args;
148 };
149
150 /* Receiver structure */
151 struct margin_recv {
152   struct margin_dev *dev;
153   u8 recvn; // Receiver Number; from 1 to 6
154   bool lane_reversal;
155   struct margin_params *params;
156
157   u8 parallel_lanes;
158   u8 error_limit;
159   u8 dwell_time;
160 };
161
162 struct margin_lanes_data {
163   struct margin_recv *recv;
164
165   struct margin_res_lane *results;
166   u8 *lanes_numbers;
167   u8 lanes_n;
168
169   bool ind;
170   enum margin_dir dir;
171
172   u8 steps_lane_done;
173   u8 steps_lane_total;
174   u64 *steps_utility;
175
176   u8 verbosity;
177 };
178
179 /* margin_args */
180
181 enum margin_mode { MARGIN, FULL, SCAN };
182
183 extern const char *usage;
184
185 struct margin_link *margin_parse_util_args(struct pci_access *pacc, int argc, char **argv,
186                                            enum margin_mode mode, u8 *links_n);
187
188 /* margin_hw */
189
190 bool margin_port_is_down(struct pci_dev *dev);
191
192 /* Results through down/up ports */
193 bool margin_find_pair(struct pci_access *pacc, struct pci_dev *dev, struct pci_dev **down_port,
194                       struct pci_dev **up_port);
195
196 /* Verify that devices form the link with 16 GT/s or 32 GT/s data rate */
197 bool margin_verify_link(struct pci_dev *down_port, struct pci_dev *up_port);
198
199 /* Check Margining Ready bit from Margining Port Status Register */
200 bool margin_check_ready_bit(struct pci_dev *dev);
201
202 /* Verify link and fill wrappers */
203 bool margin_fill_link(struct pci_dev *down_port, struct pci_dev *up_port,
204                       struct margin_link *wrappers);
205
206 /* Disable ASPM, set Hardware Autonomous Speed/Width Disable bits */
207 bool margin_prep_link(struct margin_link *link);
208
209 /* Restore ASPM, Hardware Autonomous Speed/Width settings */
210 void margin_restore_link(struct margin_link *link);
211
212 /* margin */
213
214 /* Fill margin_params without calling other functions */
215 bool margin_read_params(struct pci_access *pacc, struct pci_dev *dev, u8 recvn,
216                         struct margin_params *params);
217
218 enum margin_test_status margin_process_args(struct margin_link *link);
219
220 /* Awaits that links are prepared through process_args.
221    Returns number of margined Receivers through recvs_n */
222 struct margin_results *margin_test_link(struct margin_link *link, u8 *recvs_n);
223
224 void margin_free_results(struct margin_results *results, u8 results_n);
225
226 /* margin_log */
227
228 extern bool margin_global_logging;
229 extern bool margin_print_domain;
230
231 void margin_log(char *format, ...);
232
233 /* b:d.f -> b:d.f */
234 void margin_log_bdfs(struct pci_dev *down_port, struct pci_dev *up_port);
235 void margin_gen_bdfs(struct pci_dev *down_port, struct pci_dev *up_port, char *dest, size_t maxlen);
236
237 /* Print Link header (bdfs, width, speed) */
238 void margin_log_link(struct margin_link *link);
239
240 void margin_log_params(struct margin_params *params);
241
242 /* Print receiver number */
243 void margin_log_recvn(struct margin_recv *recv);
244
245 /* Print full info from Receiver struct */
246 void margin_log_receiver(struct margin_recv *recv);
247
248 /* Margining in progress log */
249 void margin_log_margining(struct margin_lanes_data arg);
250
251 void margin_log_hw_quirks(struct margin_recv *recv);
252
253 /* margin_results */
254
255 // Min values are taken from PCIe Base Spec Rev. 5.0 Section 8.4.2.
256 // Rec values are based on PCIe Arch PHY Test Spec Rev 5.0
257 // (Transmitter Electrical Compliance)
258
259 // values in ps
260 static const double margin_ew_min[] = { 18.75, 9.375 };
261 static const double margin_ew_rec[] = { 23.75, 10.1565 };
262
263 static const double margin_eh_min[] = { 15, 15 };
264 static const double margin_eh_rec[] = { 21, 19.75 };
265
266 void margin_results_print_brief(struct margin_results *results, u8 recvs_n,
267                                 struct margin_link_args *args);
268
269 void margin_results_save_csv(struct margin_results *results, u8 recvs_n, struct margin_link *link);
270
271 #endif