2 * The PCI Utilities -- Margining utility main function
4 * Copyright (c) 2023-2024 KNS Group LLC (YADRO)
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
17 const char program_name[] = "pcilmr";
20 scan_links(struct pci_access *pacc, bool only_ready)
23 printf("Links ready for margining:\n");
25 printf("Links with Lane Margining at the Receiver capabilities:\n");
27 for (struct pci_dev *p = pacc->devices; p; p = p->next)
29 if (pci_find_cap(p, PCI_EXT_CAP_ID_LMR, PCI_CAP_EXTENDED) && margin_port_is_down(p))
31 struct pci_dev *down = NULL;
32 struct pci_dev *up = NULL;
33 margin_find_pair(pacc, p, &down, &up);
35 if (down && margin_verify_link(down, up))
37 margin_log_bdfs(down, up);
38 if (!only_ready && (margin_check_ready_bit(down) || margin_check_ready_bit(up)))
46 printf("Links not found or you don't have enough privileges.\n");
52 main(int argc, char **argv)
54 struct pci_access *pacc;
57 struct margin_link *links;
58 bool *checks_status_ports;
60 enum margin_mode mode;
62 /* each link has several receivers -> several results */
63 struct margin_results **results;
70 margin_print_domain = false;
71 for (struct pci_dev *dev = pacc->devices; dev; dev = dev->next)
75 margin_print_domain = true;
80 margin_global_logging = true;
82 struct option long_options[]
83 = { { .name = "margin", .has_arg = no_argument, .flag = NULL, .val = 0 },
84 { .name = "scan", .has_arg = no_argument, .flag = NULL, .val = 1 },
85 { .name = "full", .has_arg = no_argument, .flag = NULL, .val = 2 },
90 c = getopt_long(argc, argv, "+", long_options, NULL);
94 case -1: /* no options (strings like component are possible) */
102 scan_links(pacc, false);
104 die("Invalid arguments\n\n%s", usage);
109 default: /* unknown option symbol */
117 links = margin_parse_util_args(pacc, argc, argv, mode, &links_n);
118 struct margin_com_args *com_args = links[0].args.common;
120 results = xmalloc(links_n * sizeof(*results));
121 results_n = xmalloc(links_n * sizeof(*results_n));
122 checks_status_ports = xmalloc(links_n * sizeof(*checks_status_ports));
124 for (int i = 0; i < links_n; i++)
126 enum margin_test_status args_status;
128 if ((args_status = margin_process_args(&links[i])) != MARGIN_TEST_OK)
130 checks_status_ports[i] = false;
131 results[i] = xmalloc(sizeof(*results[i]));
132 results[i]->test_status = args_status;
136 checks_status_ports[i] = true;
137 struct margin_params params;
138 struct margin_link_args *link_args = &links[i].args;
140 for (int j = 0; j < link_args->recvs_n; j++)
142 if (margin_read_params(
143 pacc, link_args->recvs[j] == 6 ? links[i].up_port.dev : links[i].down_port.dev,
144 link_args->recvs[j], ¶ms))
146 u8 steps_t = link_args->steps_t ? link_args->steps_t : params.timing_steps;
147 u8 steps_v = link_args->steps_v ? link_args->steps_v : params.volt_steps;
148 u8 parallel_recv = link_args->parallel_lanes > params.max_lanes + 1 ?
149 params.max_lanes + 1 :
150 link_args->parallel_lanes;
153 = link_args->lanes_n / parallel_recv + ((link_args->lanes_n % parallel_recv) > 0);
155 com_args->steps_utility += steps_t * step_multiplier;
156 if (params.ind_left_right_tim)
157 com_args->steps_utility += steps_t * step_multiplier;
158 if (params.volt_support)
160 com_args->steps_utility += steps_v * step_multiplier;
161 if (params.ind_up_down_volt)
162 com_args->steps_utility += steps_v * step_multiplier;
168 for (int i = 0; i < links_n; i++)
170 if (checks_status_ports[i])
171 results[i] = margin_test_link(&links[i], &results_n[i]);
175 if (results[i]->test_status == MARGIN_TEST_ARGS_RECVS)
177 margin_log_link(&links[i]);
178 printf("\nInvalid RecNums specified.\n");
180 else if (results[i]->test_status == MARGIN_TEST_ARGS_LANES)
182 margin_log_link(&links[i]);
183 printf("\nInvalid lanes specified.\n");
186 printf("\n----\n\n");
189 if (com_args->run_margin)
191 printf("Results:\n");
193 "Margining statuses:\nLIM -\tErrorCount exceeded Error Count Limit (found device limit)\n");
194 printf("NAK -\tDevice didn't execute last command, \n\tso result may be less reliable\n");
195 printf("THR -\tThe set (using the utility options) \n\tstep threshold has been reached\n\n");
196 printf("Notations:\nst - steps\n\n");
198 for (int i = 0; i < links_n; i++)
201 margin_log_bdfs(links[i].down_port.dev, links[i].up_port.dev);
203 margin_results_print_brief(results[i], results_n[i], &links[i].args);
204 if (com_args->save_csv)
205 margin_results_save_csv(results[i], results_n[i], &links[i]);
210 for (int i = 0; i < links_n; i++)
211 margin_free_results(results[i], results_n[i]);
216 free(checks_status_ports);