blob: a2f709be50d2a0a1625d7ffe4b9007e0678458e9 [file] [log] [blame]
Jason M. Bills7ef5a552020-04-06 14:58:44 -07001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#include <ctype.h>
Jason M. Billsff44e542021-04-05 08:11:52 -070017#include <errno.h>
Jason M. Bills7ef5a552020-04-06 14:58:44 -070018#include <inttypes.h>
Jason M. Billsff44e542021-04-05 08:11:52 -070019#include <limits.h>
Jason M. Bills7ef5a552020-04-06 14:58:44 -070020#include <peci.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#ifndef ABS
26#define ABS(_v_) (((_v_) > 0) ? (_v_) : -(_v_))
27#endif
28
Jason M. Billsff44e542021-04-05 08:11:52 -070029#define CC_COUNT 256 // CC is a byte so only has 256 possible values
30
Jason M. Bills7ef5a552020-04-06 14:58:44 -070031extern EPECIStatus peci_GetDIB(uint8_t target, uint64_t* dib);
32
33void Usage(char* progname)
34{
35 printf("Usage:\n");
Jason M. Billsff44e542021-04-05 08:11:52 -070036 printf("%s [-h] [-v] [-a <addr>] [-s <size>] [-l <count>] <command> "
37 "[parameters]\n",
38 progname);
Jason M. Bills7ef5a552020-04-06 14:58:44 -070039 printf("Options:\n");
Jason M. Billsff44e542021-04-05 08:11:52 -070040 printf("\t%-12s%s\n", "-h", "Display this help information");
41 printf("\t%-12s%s\n", "-v",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070042 "Display additional information about the command");
Jason M. Billsff44e542021-04-05 08:11:52 -070043 printf("\t%-12s%s %lu\n", "-l <count>",
44 "Loop the command the given number of times. <count> is in the "
45 "range 1 to",
46 ULONG_MAX);
47 printf("\t%-12s%s\n", "-a <addr>",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070048 "Address of the target. Accepted values are 48-55 (0x30-0x37). "
49 "Default is 48 (0x30)");
Jason M. Billsff44e542021-04-05 08:11:52 -070050 printf("\t%-12s%s\n", "-s <size>",
Jason M. Bills7ef5a552020-04-06 14:58:44 -070051 "Size of data to read or write in bytes. Accepted values are 1, 2, "
52 "4, 8, and 16. Default is 4");
53 printf("Commands:\n");
54 printf("\t%-28s%s\n", "Ping", "Ping the target");
55 printf("\t%-28s%s\n", "GetTemp", "Get the temperature");
56 printf("\t%-28s%s\n", "GetDIB", "Get the DIB");
57 printf("\t%-28s%s\n", "RdPkgConfig",
58 "Read Package Config <Index Parameter>");
59 printf("\t%-28s%s\n", "WrPkgConfig",
60 "Write Package Config <Index Parameter Data>");
61 printf("\t%-28s%s\n", "RdIAMSR", "MSR Read <Thread Address>");
62 printf("\t%-28s%s\n", "RdPCIConfig", "PCI Read <Bus Dev Func [Reg]>");
63 printf("\t%-28s%s\n", "RdPCIConfigLocal",
64 "Local PCI Read <Bus Dev Func [Reg]>");
65 printf("\t%-28s%s\n", "WrPCIConfigLocal",
66 "Local PCI Write <Bus Dev Func Reg Data>");
67 printf("\t%-28s%s\n", "RdEndpointConfigPCILocal",
68 "Endpoint Local PCI Config Read <Seg Bus Dev Func Reg>");
69 printf("\t%-28s%s\n", "WrEndpointConfigPCILocal",
70 "Endpoint Local PCI Config Write <Seg Bus Dev Func Reg Data>");
71 printf("\t%-28s%s\n", "RdEndpointConfigPCI",
72 "Endpoint PCI Config Read <Seg Bus Dev Func Reg>");
73 printf("\t%-28s%s\n", "WrEndpointConfigPCI",
74 "Endpoint PCI Config Write <Seg Bus Dev Func Reg Data>");
75 printf("\t%-28s%s\n", "RdEndpointConfigMMIO",
76 "Endpoint MMIO Read <AType Bar Seg Bus Dev Func Reg>");
77 printf("\t%-28s%s\n", "WrEndpointConfigMMIO",
78 "Endpoint MMIO Write <AType Bar Seg Bus Dev Func Reg Data>");
79 printf("\t%-28s%s\n", "raw", "Raw PECI command in bytes");
80 printf("\n");
81}
82
Jason M. Billsff44e542021-04-05 08:11:52 -070083static void printLoopSummary(uint32_t* ccCounts)
84{
85 printf("Completion code counts:\n");
86 for (uint32_t i = 0; i < CC_COUNT; i++)
87 {
88 if (ccCounts[i])
89 {
90 printf(" 0x%02x: %d\n", i, ccCounts[i]);
91 }
92 }
93}
94
Jason M. Bills7ef5a552020-04-06 14:58:44 -070095int main(int argc, char* argv[])
96{
97 int c;
98 int i = 0;
99 char* cmd = NULL;
100 EPECIStatus ret;
101 uint8_t address = 0x30; // use default address of 48d
102 uint8_t u8Size = 4; // default to a DWORD
103 uint32_t u32PciReadVal = 0;
104 uint8_t u8Seg = 0;
105 uint8_t u8Bar = 0;
106 uint8_t u8AddrType = 0;
107 uint8_t u8PciBus = 0;
108 uint8_t u8PciDev = 0;
109 uint8_t u8PciFunc = 0;
110 uint16_t u16PciReg = 0;
111 uint64_t u64Offset = 0;
112 uint32_t u32PciWriteVal = 0;
113 uint64_t u64MmioWriteVal = 0;
114 uint8_t u8PkgIndex = 0;
115 uint16_t u16PkgParam = 0;
116 uint32_t u32PkgValue = 0;
117 uint8_t u8MsrThread = 0;
118 uint16_t u16MsrAddr = 0;
119 uint64_t u64MsrVal = 0;
120 short temperature;
121 uint64_t dib;
122 int index = 0;
123 uint8_t cc = 0;
124 bool verbose = false;
Jason M. Billsff44e542021-04-05 08:11:52 -0700125 bool looped = false;
126 uint32_t loops = 1;
127 uint32_t ccCounts[CC_COUNT] = {};
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700128
129 //
130 // Parse arguments.
131 //
Jason M. Billsff44e542021-04-05 08:11:52 -0700132 while (-1 != (c = getopt(argc, argv, "hvl:a:s:")))
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700133 {
134 switch (c)
135 {
136 case 'h':
137 Usage(argv[0]);
138 return 0;
139 break;
140
141 case 'v':
142 verbose = true;
143 break;
144
Jason M. Billsff44e542021-04-05 08:11:52 -0700145 case 'l':
146 looped = true;
147 errno = 0;
148 if (optarg != NULL)
149 loops = (uint32_t)strtoul(optarg, NULL, 0);
150 if (!loops || errno)
151 {
152 printf("ERROR: Invalid loop count\n");
153 if (errno)
154 perror("");
155 goto ErrorExit;
156 }
157 break;
158
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700159 case 'a':
160 if (optarg != NULL)
161 address = (uint8_t)strtoul(optarg, NULL, 0);
162 if (address < MIN_CLIENT_ADDR || address > MAX_CLIENT_ADDR)
163 {
164 printf("ERROR: Invalid address \"0x%x\"\n", address);
165 goto ErrorExit;
166 }
167
168 break;
169
170 case 's':
171 if (optarg != NULL)
172 u8Size = (uint8_t)strtoul(optarg, NULL, 0);
173 if (u8Size != 1 && u8Size != 2 && u8Size != 4 && u8Size != 8 &&
174 u8Size != 16)
175 {
176 printf("ERROR: Invalid size \"%d\"\n", u8Size);
177 goto ErrorExit;
178 }
179 break;
180
181 default:
182 printf("ERROR: Unrecognized option \"-%c\"\n", optopt);
183 goto ErrorExit;
184 break;
185 }
186 }
187
188 // Get the command from the first parameter
189 cmd = argv[optind++];
190 if (cmd == NULL)
191 {
192 Usage(argv[0]);
193 return 0;
194 }
195
196 // Allow any case
197 while (cmd[i])
198 {
199 cmd[i] = (char)tolower((int)cmd[i]);
200 i++;
201 }
202
203 //
204 // Execute the command
205 //
206 if (verbose)
207 {
208 printf("PECI target[0x%x]: ", address);
209 }
210 if (strcmp(cmd, "ping") == 0)
211 {
212 if (verbose)
213 {
214 printf("Pinging ... ");
215 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700216 while (loops--)
217 {
218 ret = peci_Ping(address);
219 if (verbose || loops == 0)
220 {
221 if (0 != ret)
222 {
223 printf("Failed\n");
224 }
225 else
226 {
227 printf("Succeeded\n");
228 }
229 }
230 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700231 }
232 else if (strcmp(cmd, "getdib") == 0)
233 {
234 if (verbose)
235 {
236 printf("GetDIB\n");
237 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700238 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700239 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700240 ret = peci_GetDIB(address, &dib);
241 if (verbose || loops == 0)
242 {
243 if (0 != ret)
244 {
245 printf("ERROR %d: Retrieving DIB failed\n", ret);
246 }
247 else
248 {
249 printf(" 0x%" PRIx64 "\n", dib);
250 }
251 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700252 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700253 }
254
255 else if (strcmp(cmd, "gettemp") == 0)
256 {
257 if (verbose)
258 {
259 printf("GetTemp\n");
260 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700261 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700262 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700263 ret = peci_GetTemp(address, &temperature);
264 if (verbose || loops == 0)
265 {
266 if (0 != ret)
267 {
268 printf("ERROR %d: Retrieving temperature failed\n", ret);
269 }
270 else
271 {
272 printf(" %04xh (%c%d.%02dC)\n",
273 (int)(unsigned int)(unsigned short)temperature,
274 (0 > temperature) ? '-' : '+',
275 (int)((unsigned int)ABS(temperature) / 64),
276 (int)(((unsigned int)ABS(temperature) % 64) * 100) /
277 64);
278 }
279 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700280 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700281 }
282
283 else if (strcmp(cmd, "rdpkgconfig") == 0)
284 {
285 index = argc;
286 switch (argc - optind)
287 {
288 case 2:
289 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
290 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
291 break;
292 default:
293 printf("ERROR: Unsupported arguments for Pkg Read\n");
294 goto ErrorExit;
295 break;
296 }
297 if (verbose)
298 {
299 printf("Pkg Read of Index %02x Param %04x\n", u8PkgIndex,
300 u16PkgParam);
301 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700302 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700303 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700304 ret = peci_RdPkgConfig(address, u8PkgIndex, u16PkgParam, u8Size,
305 (uint8_t*)&u32PkgValue, &cc);
306 ccCounts[cc]++;
307
308 if (verbose || loops == 0)
309 {
310 if (0 != ret)
311 {
312 printf("ERROR %d: command failed\n", ret);
313 printf(" cc:0x%02x\n", cc);
314 }
315 else
316 {
317 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
318 u32PkgValue);
319 }
320 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700321 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700322 if (looped)
323 {
324 printLoopSummary(ccCounts);
325 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700326 }
327 else if (strcmp(cmd, "wrpkgconfig") == 0)
328 {
329 index = argc;
330 switch (argc - optind)
331 {
332 case 3:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700333 u32PkgValue = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700334 u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
335 u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
336 break;
337 default:
338 printf("ERROR: Unsupported arguments for Pkg Write\n");
339 goto ErrorExit;
340 break;
341 }
342 if (verbose)
343 {
344 printf("Pkg Write of Index %02x Param %04x: 0x%0*x\n", u8PkgIndex,
345 u16PkgParam, u8Size * 2, u32PkgValue);
346 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700347 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700348 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700349 ret = peci_WrPkgConfig(address, u8PkgIndex, u16PkgParam,
350 u32PkgValue, u8Size, &cc);
351 ccCounts[cc]++;
352
353 if (verbose || loops == 0)
354 {
355 if (0 != ret)
356 {
357 printf("ERROR %d: command failed\n", ret);
358 }
359 printf(" cc:0x%02x\n", cc);
360 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700361 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700362 if (looped)
363 {
364 printLoopSummary(ccCounts);
365 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700366 }
367 else if (strcmp(cmd, "rdiamsr") == 0)
368 {
369 index = argc;
370 switch (argc - optind)
371 {
372 case 2:
373 u16MsrAddr = (uint16_t)strtoul(argv[--index], NULL, 0);
374 u8MsrThread = (uint8_t)strtoul(argv[--index], NULL, 0);
375 break;
376 default:
377 printf("ERROR: Unsupported arguments for MSR Read\n");
378 goto ErrorExit;
379 break;
380 }
381 if (verbose)
382 {
383 printf("MSR Read of Thread %02x MSR %04x\n", u8MsrThread,
384 u16MsrAddr);
385 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700386 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700387 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700388 ret =
389 peci_RdIAMSR(address, u8MsrThread, u16MsrAddr, &u64MsrVal, &cc);
390 ccCounts[cc]++;
391
392 if (verbose || loops == 0)
393 {
394 if (0 != ret)
395 {
396 printf("ERROR %d: command failed\n", ret);
397 printf(" cc:0x%02x\n", cc);
398 }
399 else
400 {
401 printf(" cc:0x%02x 0x%0*llx\n", cc, u8Size * 2,
402 (unsigned long long)u64MsrVal);
403 }
404 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700405 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700406 if (looped)
407 {
408 printLoopSummary(ccCounts);
409 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700410 }
411 else if (strcmp(cmd, "rdpciconfig") == 0)
412 {
413 index = argc;
414 switch (argc - optind)
415 {
416 case 4:
417 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
418 /* FALLTHROUGH */
419 case 3:
420 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
421 /* FALLTHROUGH */
422 case 2:
423 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
424 /* FALLTHROUGH */
425 case 1:
426 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
427 break;
428 default:
429 printf("ERROR: Unsupported arguments for PCI Read\n");
430 goto ErrorExit;
431 break;
432 }
433 if (verbose)
434 {
435 printf("PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus, u8PciDev,
436 u8PciFunc, u16PciReg);
437 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700438 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700439 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700440 ret = peci_RdPCIConfig(address, u8PciBus, u8PciDev, u8PciFunc,
441 u16PciReg, (uint8_t*)&u32PciReadVal, &cc);
442 ccCounts[cc]++;
443
444 if (verbose || loops == 0)
445 {
446 if (0 != ret)
447 {
448 printf("ERROR %d: command failed\n", ret);
449 printf(" cc:0x%02x\n", cc);
450 }
451 else
452 {
453 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
454 u32PciReadVal);
455 }
456 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700457 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700458 if (looped)
459 {
460 printLoopSummary(ccCounts);
461 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700462 }
463 else if (strcmp(cmd, "rdpciconfiglocal") == 0)
464 {
465 index = argc;
466 switch (argc - optind)
467 {
468 case 4:
469 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
470 /* FALLTHROUGH */
471 case 3:
472 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
473 /* FALLTHROUGH */
474 case 2:
475 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
476 /* FALLTHROUGH */
477 case 1:
478 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
479 break;
480 default:
481 printf("ERROR: Unsupported arguments for Local PCI Read\n");
482 goto ErrorExit;
483 break;
484 }
485 if (verbose)
486 {
487 printf("Local PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus,
488 u8PciDev, u8PciFunc, u16PciReg);
489 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700490 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700491 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700492 ret = peci_RdPCIConfigLocal(address, u8PciBus, u8PciDev, u8PciFunc,
493 u16PciReg, u8Size,
494 (uint8_t*)&u32PciReadVal, &cc);
495 ccCounts[cc]++;
496
497 if (verbose || loops == 0)
498 {
499 if (0 != ret)
500 {
501 printf("ERROR %d: command failed\n", ret);
502 printf(" cc:0x%02x\n", cc);
503 }
504 else
505 {
506 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
507 u32PciReadVal);
508 }
509 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700510 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700511 if (looped)
512 {
513 printLoopSummary(ccCounts);
514 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700515 }
516 else if (strcmp(cmd, "wrpciconfiglocal") == 0)
517 {
518 index = argc;
Jason M. Bills62cbc712020-05-07 14:07:49 -0700519 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700520 switch (argc - optind)
521 {
522 case 5:
523 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
524 /* FALLTHROUGH */
525 case 4:
526 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
527 /* FALLTHROUGH */
528 case 3:
529 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
530 /* FALLTHROUGH */
531 case 2:
532 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
533 break;
534 default:
535 printf("ERROR: Unsupported arguments for Local PCI Write\n");
536 goto ErrorExit;
537 break;
538 }
539 if (verbose)
540 {
541 printf("Local PCI Write of %02x:%02x:%02x Reg %02x: 0x%0*x\n",
542 u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
543 u32PciWriteVal);
544 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700545 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700546 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700547 ret = peci_WrPCIConfigLocal(address, u8PciBus, u8PciDev, u8PciFunc,
548 u16PciReg, u8Size, u32PciWriteVal, &cc);
549 ccCounts[cc]++;
550
551 if (verbose || loops == 0)
552 {
553 if (0 != ret)
554 {
555 printf("ERROR %d: command failed\n", ret);
556 }
557 printf(" cc:0x%02x\n", cc);
558 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700559 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700560 if (looped)
561 {
562 printLoopSummary(ccCounts);
563 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700564 }
565 else if (strcmp(cmd, "rdendpointconfigpcilocal") == 0)
566 {
567 index = argc;
568 switch (argc - optind)
569 {
570 case 5:
571 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
572 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
573 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
574 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
575 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
576 break;
577
578 default:
579 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
580 "Read\n");
581 goto ErrorExit;
582 }
583 if (verbose)
584 {
585 printf(
586 "Endpoint Local PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
587 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
588 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700589 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700590 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700591 ret = peci_RdEndPointConfigPciLocal(
592 address, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg,
593 u8Size, (uint8_t*)&u32PciReadVal, &cc);
594 ccCounts[cc]++;
595
596 if (verbose || loops == 0)
597 {
598 if (0 != ret)
599 {
600 printf("ERROR %d: command failed\n", ret);
601 printf(" cc:0x%02x\n", cc);
602 }
603 else
604 {
605 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
606 u32PciReadVal);
607 }
608 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700609 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700610 if (looped)
611 {
612 printLoopSummary(ccCounts);
613 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700614 }
615 else if (strcmp(cmd, "wrendpointconfigpcilocal") == 0)
616 {
617 index = argc;
618 switch (argc - optind)
619 {
620 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700621 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700622 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
623 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
624 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
625 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
626 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
627 break;
628
629 default:
630 printf("ERROR: Unsupported arguments for Endpoint Local PCI "
631 "Write\n");
632 goto ErrorExit;
633 }
634 if (verbose)
635 {
636 printf("Endpoint Local PCI Write of Seg:%02x %02x:%02x:%02x Reg "
637 "%02x: 0x%0*x\n",
638 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
639 u32PciWriteVal);
640 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700641 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700642 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700643 ret = peci_WrEndPointPCIConfigLocal(address, u8Seg, u8PciBus,
644 u8PciDev, u8PciFunc, u16PciReg,
645 u8Size, u32PciWriteVal, &cc);
646 ccCounts[cc]++;
647
648 if (verbose || loops == 0)
649 {
650 if (0 != ret)
651 {
652 printf("ERROR %d: command failed\n", ret);
653 }
654 printf(" cc:0x%02x\n", cc);
655 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700656 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700657 if (looped)
658 {
659 printLoopSummary(ccCounts);
660 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700661 }
662 else if (strcmp(cmd, "rdendpointconfigpci") == 0)
663 {
664 index = argc;
665 switch (argc - optind)
666 {
667 case 5:
668 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
669 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
670 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
671 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
672 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
673 break;
674
675 default:
676 printf("ERROR: Unsupported arguments for Endpoint PCI Read\n");
677 goto ErrorExit;
678 }
679 if (verbose)
680 {
681 printf("Endpoint PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
682 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
683 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700684 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700685 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700686 ret = peci_RdEndPointConfigPci(address, u8Seg, u8PciBus, u8PciDev,
687 u8PciFunc, u16PciReg, u8Size,
688 (uint8_t*)&u32PciReadVal, &cc);
689 ccCounts[cc]++;
690
691 if (verbose || loops == 0)
692 {
693 if (0 != ret)
694 {
695 printf("ERROR %d: command failed\n", ret);
696 printf(" cc:0x%02x\n", cc);
697 }
698 else
699 {
700 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
701 u32PciReadVal);
702 }
703 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700704 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700705 if (looped)
706 {
707 printLoopSummary(ccCounts);
708 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700709 }
710 else if (strcmp(cmd, "wrendpointconfigpci") == 0)
711 {
712 index = argc;
713 switch (argc - optind)
714 {
715 case 6:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700716 u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700717 u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
718 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
719 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
720 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
721 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
722 break;
723
724 default:
725 printf("ERROR: Unsupported arguments for Endpoint PCI Write\n");
726 goto ErrorExit;
727 }
728 if (verbose)
729 {
730 printf("Endpoint PCI Write of Seg:%02x %02x:%02x:%02x Reg %02x: "
731 "0x%0*x\n",
732 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
733 u32PciWriteVal);
734 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700735 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700736 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700737 ret = peci_WrEndPointPCIConfig(address, u8Seg, u8PciBus, u8PciDev,
738 u8PciFunc, u16PciReg, u8Size,
739 u32PciWriteVal, &cc);
740 ccCounts[cc]++;
741
742 if (verbose || loops == 0)
743 {
744 if (0 != ret)
745 {
746 printf("ERROR %d: command failed\n", ret);
747 }
748 printf(" cc:0x%02x\n", cc);
749 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700750 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700751 if (looped)
752 {
753 printLoopSummary(ccCounts);
754 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700755 }
756 else if (strcmp(cmd, "rdendpointconfigmmio") == 0)
757 {
758 index = argc;
759 switch (argc - optind)
760 {
761 case 7:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700762 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700763 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
764 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
765 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
766 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
767 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
768 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
769 break;
770
771 default:
772 printf("ERROR: Unsupported arguments for Endpoint MMIO Read\n");
773 goto ErrorExit;
774 }
775 if (verbose)
776 {
777 printf("Endpoint MMIO Read of Seg:%02x %02x:%02x:%02x AType:%02x "
778 "Bar:%02x Offset:0x%" PRIx64 "\n",
779 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
780 u64Offset);
781 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700782 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700783 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700784 ret = peci_RdEndPointConfigMmio(
785 address, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
786 u8AddrType, u64Offset, u8Size, (uint8_t*)&u32PciReadVal, &cc);
787 ccCounts[cc]++;
788
789 if (verbose || loops == 0)
790 {
791 if (0 != ret)
792 {
793 printf("ERROR %d: command failed\n", ret);
794 printf(" cc:0x%02x\n", cc);
795 }
796 else
797 {
798 printf(" cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
799 u32PciReadVal);
800 }
801 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700802 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700803 if (looped)
804 {
805 printLoopSummary(ccCounts);
806 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700807 }
808 else if (strcmp(cmd, "wrendpointconfigmmio") == 0)
809 {
810 index = argc;
811 switch (argc - optind)
812 {
813 case 8:
Jason M. Bills62cbc712020-05-07 14:07:49 -0700814 u64MmioWriteVal = (uint64_t)strtoull(argv[--index], NULL, 0);
815 u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700816 u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
817 u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
818 u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
819 u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
820 u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
821 u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
822 break;
823
824 default:
825 printf(
826 "ERROR: Unsupported arguments for Endpoint MMIO Write\n");
827 goto ErrorExit;
828 }
829 if (verbose)
830 {
831 printf("Endpoint MMIO Write of Seg:%02x %02x:%02x:%02x AType:%02x "
832 "Bar:%02x Offset:0x%" PRIx64 ": 0x%0*" PRIx64 "\n",
833 u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
834 u64Offset, u8Size * 2, u64MmioWriteVal);
835 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700836 while (loops--)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700837 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700838 ret = peci_WrEndPointConfigMmio(
839 address, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
840 u8AddrType, u64Offset, u8Size, u64MmioWriteVal, &cc);
841 ccCounts[cc]++;
842
843 if (verbose || loops == 0)
844 {
845 if (0 != ret)
846 {
847 printf("ERROR %d: command failed\n", ret);
848 }
849 printf(" cc:0x%02x\n", cc);
850 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700851 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700852 if (looped)
853 {
854 printLoopSummary(ccCounts);
855 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700856 }
857 else if (strcmp(cmd, "raw") == 0)
858 {
859 if ((argc - optind) < 3)
860 {
861 printf("ERROR: Unsupported arguments for raw command\n");
862 goto ErrorExit;
863 }
864
865 // Address is provided in the first byte of the PECI command
866 uint8_t rawAddr = (uint8_t)strtoul(argv[optind++], NULL, 0);
867 // Write length is provided in the second byte of the PECI command
868 uint8_t writeLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
869 // Read length is provided in the third byte of the PECI command
870 uint8_t readLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
871
Jason M. Billsc965e722020-07-06 15:49:14 -0700872 // remaining parameters should fit within write length
873 if ((argc - optind) > writeLength)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700874 {
875 printf("ERROR: Incorrect write length for raw command\n");
876 goto ErrorExit;
877 }
878 uint8_t* rawCmd = (uint8_t*)calloc(writeLength, sizeof(uint8_t));
879 if (rawCmd == NULL)
880 {
881 // calloc failed, abort the sequence
882 printf("Raw command memory allocation failed\n");
883 return 1;
884 }
Jason M. Billsc965e722020-07-06 15:49:14 -0700885 for (i = 0; i < (argc - optind); i++)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700886 {
887 rawCmd[i] = (uint8_t)strtoul(argv[i + optind], NULL, 0);
888 }
889 if (verbose)
890 {
891 printf("Raw command: %02x %02x %02x ", rawAddr, writeLength,
892 readLength);
893 for (i = 0; i < writeLength; i++)
894 {
895 printf("0x%02x ", rawCmd[i]);
896 }
897 printf("\n");
898 }
899
900 uint8_t* rawResp = (uint8_t*)calloc(readLength, sizeof(uint8_t));
901 if (rawResp == NULL)
902 {
903 // calloc failed, abort the sequence
904 printf("Raw command memory allocation failed\n");
905 free(rawCmd);
906 return 1;
907 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700908 while (loops--)
Jason M. Bills0e21dde2020-07-06 14:51:53 -0700909 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700910 ret = peci_raw(rawAddr, readLength, rawCmd, writeLength, rawResp,
911 readLength);
912 if (verbose)
913 {
914 printf(" ");
915 for (i = 0; i < readLength; i++)
916 {
917 printf("0x%02x ", rawResp[i]);
918 }
919 printf("\n");
920 }
921 ccCounts[rawResp[0]]++;
922 }
923 if (!verbose)
924 {
925 if (0 != ret)
926 {
927 printf("ERROR %d: command failed\n", ret);
928 }
929 printf(" ");
Jason M. Bills0e21dde2020-07-06 14:51:53 -0700930 for (i = 0; i < readLength; i++)
931 {
932 printf("0x%02x ", rawResp[i]);
933 }
934 printf("\n");
935 }
Jason M. Billsff44e542021-04-05 08:11:52 -0700936
937 if (looped)
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700938 {
Jason M. Billsff44e542021-04-05 08:11:52 -0700939 printLoopSummary(ccCounts);
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700940 }
Jason M. Bills7ef5a552020-04-06 14:58:44 -0700941
942 free(rawCmd);
943 free(rawResp);
944 }
945 else
946 {
947 printf("ERROR: Unrecognized command\n");
948 goto ErrorExit;
949 }
950
951 return 0;
952
953ErrorExit:
954 Usage(argv[0]);
955 return 1;
956}