/*
// Copyright (c) 2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <peci.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef ABS
#define ABS(_v_) (((_v_) > 0) ? (_v_) : -(_v_))
#endif

#define CC_COUNT 256 // CC is a byte so only has 256 possible values

extern EPECIStatus peci_GetDIB(uint8_t target, uint64_t* dib);

double getTimeDifference(const struct timespec begin)
{
    double timeDiff = 0.0;
    struct timespec end;
    long seconds = 0;
    long nanoseconds = 0;

    clock_gettime(CLOCK_REALTIME, &end);
    seconds = end.tv_sec - begin.tv_sec;
    nanoseconds = end.tv_nsec - begin.tv_nsec;
    timeDiff = (double)seconds + (double)nanoseconds * 1e-9;

    return timeDiff;
}

void Usage(char* progname)
{
    printf("Usage:\n");
    printf("%s [-h] [-v] [-t] [-a <addr>] [-i <domain id>] [-s <size>] [-l "
           "<count>] "
           "<command> [parameters]\n",
           progname);
    printf("Options:\n");
    printf("\t%-12s%s\n", "-h", "Display this help information");
    printf("\t%-12s%s\n", "-v",
           "Display additional information about the command");
    printf("\t%-12s%s\n", "-t", "Measure request-to-response time");
    printf("\t%-12s%s %lu\n", "-l <count>",
           "Loop the command the given number of times. <count> is in the "
           "range 1 to",
           ULONG_MAX);
    printf("\t%-12s%s\n", "-a <addr>",
           "Address of the target. Accepted values are 48-55 (0x30-0x37). "
           "Default is 48 (0x30)");
    printf("\t%-12s%s\n", "-i <domain id>",
           "Domain ID of the target. Accepted values are 0-127. Default is 0");
    printf("\t%-12s%s\n", "-s <size>",
           "Size of data to read or write in bytes. Accepted values are 1, 2, "
           "4, 8, and 16. Default is 4");
    printf("\t%-12s%s\n", "-d",
           "Set PECI device name, for example \"-d /dev/peci-0\"");
    printf("Commands:\n");
    printf("\t%-28s%s\n", "Ping", "Ping the target");
    printf("\t%-28s%s\n", "GetTemp", "Get the temperature");
    printf("\t%-28s%s\n", "GetDIB", "Get the DIB");
    printf("\t%-28s%s\n", "RdPkgConfig",
           "Read Package Config <Index Parameter>");
    printf("\t%-28s%s\n", "WrPkgConfig",
           "Write Package Config <Index Parameter Data>");
    printf("\t%-28s%s\n", "RdIAMSR", "MSR Read <Thread Address>");
    printf("\t%-28s%s\n", "RdPCIConfig", "PCI Read <Bus Dev Func [Reg]>");
    printf("\t%-28s%s\n", "RdPCIConfigLocal",
           "Local PCI Read <Bus Dev Func [Reg]>");
    printf("\t%-28s%s\n", "WrPCIConfigLocal",
           "Local PCI Write <Bus Dev Func Reg Data>");
    printf("\t%-28s%s\n", "RdEndpointConfigPCILocal",
           "Endpoint Local PCI Config Read <Seg Bus Dev Func Reg>");
    printf("\t%-28s%s\n", "WrEndpointConfigPCILocal",
           "Endpoint Local PCI Config Write <Seg Bus Dev Func Reg Data>");
    printf("\t%-28s%s\n", "RdEndpointConfigPCI",
           "Endpoint PCI Config Read <Seg Bus Dev Func Reg>");
    printf("\t%-28s%s\n", "WrEndpointConfigPCI",
           "Endpoint PCI Config Write <Seg Bus Dev Func Reg Data>");
    printf("\t%-28s%s\n", "RdEndpointConfigMMIO",
           "Endpoint MMIO Read <AType Bar Seg Bus Dev Func Reg>");
    printf("\t%-28s%s\n", "WrEndpointConfigMMIO",
           "Endpoint MMIO Write <AType Bar Seg Bus Dev Func Reg Data>");
    printf("\t%-28s%s\n", "raw", "Raw PECI command in bytes");
    printf("\n");
}

static void printLoopSummary(uint32_t* ccCounts)
{
    printf("Completion code counts:\n");
    for (uint32_t i = 0; i < CC_COUNT; i++)
    {
        if (ccCounts[i])
        {
            printf("   0x%02x: %d\n", i, ccCounts[i]);
        }
    }
}

int main(int argc, char* argv[])
{
    int c;
    int i = 0;
    char* cmd = NULL;
    EPECIStatus ret;
    uint8_t address = 0x30; // use default address of 48d
    uint8_t domainId = 0;   // use default domain ID of 0
    uint8_t u8Size = 4;     // default to a DWORD
    uint32_t u32PciReadVal = 0;
    uint8_t u8Seg = 0;
    uint8_t u8Bar = 0;
    uint8_t u8AddrType = 0;
    uint8_t u8PciBus = 0;
    uint8_t u8PciDev = 0;
    uint8_t u8PciFunc = 0;
    uint16_t u16PciReg = 0;
    uint64_t u64Offset = 0;
    uint32_t u32PciWriteVal = 0;
    uint64_t u64MmioWriteVal = 0;
    uint8_t u8PkgIndex = 0;
    uint16_t u16PkgParam = 0;
    uint32_t u32PkgValue = 0;
    uint8_t u8MsrThread = 0;
    uint16_t u16MsrAddr = 0;
    uint64_t u64MsrVal = 0;
    short temperature;
    uint64_t dib;
    int index = 0;
    uint8_t cc = 0;
    bool measureTime = false;
    bool verbose = false;
    bool looped = false;
    uint32_t loops = 1;
    uint32_t loopCount = 1;
    uint32_t ccCounts[CC_COUNT] = {0};
    struct timespec begin;
    double timeSpent = 0.0;
    double totalTimeSpent = 0.0;

    //
    // Parse arguments.
    //
    while (-1 != (c = getopt(argc, argv, "hvtl:a:i:s:d:")))
    {
        switch (c)
        {
            case 'h':
                Usage(argv[0]);
                return 0;
                break;

            case 'v':
                verbose = true;
                break;

            case 't':
                measureTime = true;
                break;

            case 'l':
                looped = true;
                errno = 0;
                if (optarg != NULL)
                    loops = (uint32_t)strtoul(optarg, NULL, 0);
                if (!loops || errno)
                {
                    printf("ERROR: Invalid loop count\n");
                    if (errno)
                        perror("");
                    goto ErrorExit;
                }
                loopCount =
                    loops; // Preserve a copy for average time measurement
                break;

            case 'a':
                if (optarg != NULL)
                    address = (uint8_t)strtoul(optarg, NULL, 0);
                if (address < MIN_CLIENT_ADDR || address > MAX_CLIENT_ADDR)
                {
                    printf("ERROR: Invalid address \"0x%x\"\n", address);
                    goto ErrorExit;
                }

                break;

            case 'i':
                if (optarg != NULL)
                    domainId = (uint8_t)strtoul(optarg, NULL, 0);
                if (domainId >= 128) // Domain ID is only 7 bits
                {
                    printf("ERROR: Invalid domain ID \"%d\"\n", domainId);
                    goto ErrorExit;
                }
                break;

            case 's':
                if (optarg != NULL)
                    u8Size = (uint8_t)strtoul(optarg, NULL, 0);
                if (u8Size != 1 && u8Size != 2 && u8Size != 4 && u8Size != 8 &&
                    u8Size != 16)
                {
                    printf("ERROR: Invalid size \"%d\"\n", u8Size);
                    goto ErrorExit;
                }
                break;

            case 'd':
                peci_SetDevName(optarg);
                break;

            default:
                printf("ERROR: Unrecognized option \"-%c\"\n", optopt);
                goto ErrorExit;
                break;
        }
    }

    // Get the command from the first parameter
    cmd = argv[optind++];
    if (cmd == NULL)
    {
        Usage(argv[0]);
        return 0;
    }

    // Allow any case
    while (cmd[i])
    {
        cmd[i] = (char)tolower((int)cmd[i]);
        i++;
    }

    //
    // Execute the command
    //
    if (verbose)
    {
        printf("PECI target[0x%x]: ", address);
    }

    if (measureTime)
    {
        if (verbose && (loopCount > 1))
        {
            printf("Warning: Request-response time measurement with verbose "
                   "mode can affect the time between consecutive commands in "
                   "looped mode!\n");
        }
    }

    if (strcmp(cmd, "ping") == 0)
    {
        if (verbose)
        {
            printf("Pinging ... ");
        }
        while (loops--)
        {
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_Ping(address);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("Failed\n");
                }
                else
                {
                    printf("Succeeded\n");
                }
            }
        }
    }
    else if (strcmp(cmd, "getdib") == 0)
    {
        if (verbose)
        {
            printf("GetDIB\n");
        }
        while (loops--)
        {
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_GetDIB(address, &dib);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: Retrieving DIB failed\n", ret);
                }
                else
                {
                    printf("   0x%" PRIx64 "\n", dib);
                }
            }
        }
    }

    else if (strcmp(cmd, "gettemp") == 0)
    {
        if (verbose)
        {
            printf("GetTemp\n");
        }
        while (loops--)
        {
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_GetTemp(address, &temperature);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: Retrieving temperature failed\n", ret);
                }
                else
                {
                    printf("   %04xh (%c%d.%02dC)\n",
                           (int)(unsigned int)(unsigned short)temperature,
                           (0 > temperature) ? '-' : '+',
                           (int)((unsigned int)ABS(temperature) / 64),
                           (int)(((unsigned int)ABS(temperature) % 64) * 100) /
                               64);
                }
            }
        }
    }

    else if (strcmp(cmd, "rdpkgconfig") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 2:
                u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for Pkg Read\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("Pkg Read of Index %02x Param %04x\n", u8PkgIndex,
                   u16PkgParam);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdPkgConfig_dom(address, domainId, u8PkgIndex,
                                       u16PkgParam, u8Size,
                                       (uint8_t*)&u32PkgValue, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PkgValue);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "wrpkgconfig") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 3:
                u32PkgValue = (uint32_t)strtoul(argv[--index], NULL, 0);
                u16PkgParam = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PkgIndex = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for Pkg Write\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("Pkg Write of Index %02x Param %04x: 0x%0*x\n", u8PkgIndex,
                   u16PkgParam, u8Size * 2, u32PkgValue);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_WrPkgConfig_dom(address, domainId, u8PkgIndex,
                                       u16PkgParam, u32PkgValue, u8Size, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                }
                printf("   cc:0x%02x\n", cc);
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdiamsr") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 2:
                u16MsrAddr = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8MsrThread = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for MSR Read\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("MSR Read of Thread %02x MSR %04x\n", u8MsrThread,
                   u16MsrAddr);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdIAMSR_dom(address, domainId, u8MsrThread, u16MsrAddr,
                                   &u64MsrVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*llx\n", cc, u8Size * 2,
                           (unsigned long long)u64MsrVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdpciconfig") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 4:
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 3:
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 2:
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 1:
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for PCI Read\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus, u8PciDev,
                   u8PciFunc, u16PciReg);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdPCIConfig_dom(address, domainId, u8PciBus, u8PciDev,
                                       u8PciFunc, u16PciReg,
                                       (uint8_t*)&u32PciReadVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PciReadVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdpciconfiglocal") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 4:
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 3:
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 2:
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 1:
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for Local PCI Read\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("Local PCI Read of %02x:%02x:%02x Reg %02x\n", u8PciBus,
                   u8PciDev, u8PciFunc, u16PciReg);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdPCIConfigLocal_dom(
                address, domainId, u8PciBus, u8PciDev, u8PciFunc, u16PciReg,
                u8Size, (uint8_t*)&u32PciReadVal, &cc);
            ccCounts[cc]++;
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PciReadVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "wrpciconfiglocal") == 0)
    {
        index = argc;
        u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
        switch (argc - optind)
        {
            case 5:
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 4:
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 3:
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                /* FALLTHROUGH */
            case 2:
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;
            default:
                printf("ERROR: Unsupported arguments for Local PCI Write\n");
                goto ErrorExit;
                break;
        }
        if (verbose)
        {
            printf("Local PCI Write of %02x:%02x:%02x Reg %02x: 0x%0*x\n",
                   u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
                   u32PciWriteVal);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_WrPCIConfigLocal_dom(address, domainId, u8PciBus,
                                            u8PciDev, u8PciFunc, u16PciReg,
                                            u8Size, u32PciWriteVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                }
                printf("   cc:0x%02x\n", cc);
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdendpointconfigpcilocal") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 5:
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf("ERROR: Unsupported arguments for Endpoint Local PCI "
                       "Read\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf(
                "Endpoint Local PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
                u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdEndPointConfigPciLocal_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
                u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PciReadVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "wrendpointconfigpcilocal") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 6:
                u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf("ERROR: Unsupported arguments for Endpoint Local PCI "
                       "Write\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf("Endpoint Local PCI Write of Seg:%02x %02x:%02x:%02x Reg "
                   "%02x: 0x%0*x\n",
                   u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
                   u32PciWriteVal);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_WrEndPointPCIConfigLocal_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
                u16PciReg, u8Size, u32PciWriteVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                }
                printf("   cc:0x%02x\n", cc);
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdendpointconfigpci") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 5:
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf("ERROR: Unsupported arguments for Endpoint PCI Read\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf("Endpoint PCI Read of Seg:%02x %02x:%02x:%02x Reg %02x\n",
                   u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdEndPointConfigPci_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
                u16PciReg, u8Size, (uint8_t*)&u32PciReadVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PciReadVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "wrendpointconfigpci") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 6:
                u32PciWriteVal = (uint32_t)strtoul(argv[--index], NULL, 0);
                u16PciReg = (uint16_t)strtoul(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf("ERROR: Unsupported arguments for Endpoint PCI Write\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf("Endpoint PCI Write of Seg:%02x %02x:%02x:%02x Reg %02x: "
                   "0x%0*x\n",
                   u8Seg, u8PciBus, u8PciDev, u8PciFunc, u16PciReg, u8Size * 2,
                   u32PciWriteVal);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_WrEndPointPCIConfig_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc,
                u16PciReg, u8Size, u32PciWriteVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                }
                printf("   cc:0x%02x\n", cc);
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "rdendpointconfigmmio") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 7:
                u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf("ERROR: Unsupported arguments for Endpoint MMIO Read\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf("Endpoint MMIO Read of Seg:%02x %02x:%02x:%02x AType:%02x "
                   "Bar:%02x Offset:0x%" PRIx64 "\n",
                   u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
                   u64Offset);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_RdEndPointConfigMmio_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
                u8AddrType, u64Offset, u8Size, (uint8_t*)&u32PciReadVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                    printf("   cc:0x%02x\n", cc);
                }
                else
                {
                    printf("   cc:0x%02x 0x%0*x\n", cc, u8Size * 2,
                           u32PciReadVal);
                }
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "wrendpointconfigmmio") == 0)
    {
        index = argc;
        switch (argc - optind)
        {
            case 8:
                u64MmioWriteVal = (uint64_t)strtoull(argv[--index], NULL, 0);
                u64Offset = (uint64_t)strtoull(argv[--index], NULL, 0);
                u8PciFunc = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciDev = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8PciBus = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Seg = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8Bar = (uint8_t)strtoul(argv[--index], NULL, 0);
                u8AddrType = (uint8_t)strtoul(argv[--index], NULL, 0);
                break;

            default:
                printf(
                    "ERROR: Unsupported arguments for Endpoint MMIO Write\n");
                goto ErrorExit;
        }
        if (verbose)
        {
            printf("Endpoint MMIO Write of Seg:%02x %02x:%02x:%02x AType:%02x "
                   "Bar:%02x Offset:0x%" PRIx64 ": 0x%0*" PRIx64 "\n",
                   u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8AddrType, u8Bar,
                   u64Offset, u8Size * 2, u64MmioWriteVal);
        }
        while (loops--)
        {
            cc = 0; // reset the cc for each loop
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_WrEndPointConfigMmio_dom(
                address, domainId, u8Seg, u8PciBus, u8PciDev, u8PciFunc, u8Bar,
                u8AddrType, u64Offset, u8Size, u64MmioWriteVal, &cc);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            ccCounts[cc]++;

            if (verbose || loops == 0)
            {
                if (0 != ret)
                {
                    printf("ERROR %d: command failed\n", ret);
                }
                printf("   cc:0x%02x\n", cc);
            }
        }
        if (looped)
        {
            printLoopSummary(ccCounts);
        }
    }
    else if (strcmp(cmd, "raw") == 0)
    {
        if ((argc - optind) < 3)
        {
            printf("ERROR: Unsupported arguments for raw command\n");
            goto ErrorExit;
        }

        // Address is provided in the first byte of the PECI command
        uint8_t rawAddr = (uint8_t)strtoul(argv[optind++], NULL, 0);
        // Write length is provided in the second byte of the PECI command
        uint8_t writeLength = (uint8_t)strtoul(argv[optind++], NULL, 0);
        // Read length is provided in the third byte of the PECI command
        uint8_t readLength = (uint8_t)strtoul(argv[optind++], NULL, 0);

        // remaining parameters should fit within write length
        if ((argc - optind) > writeLength)
        {
            printf("ERROR: Incorrect write length for raw command\n");
            goto ErrorExit;
        }
        uint8_t* rawCmd = (uint8_t*)calloc(writeLength, sizeof(uint8_t));
        if (rawCmd == NULL)
        {
            // calloc failed, abort the sequence
            printf("Raw command memory allocation failed\n");
            return 1;
        }
        for (i = 0; i < (argc - optind); i++)
        {
            rawCmd[i] = (uint8_t)strtoul(argv[i + optind], NULL, 0);
        }
        if (verbose)
        {
            printf("Raw command: %02x %02x %02x ", rawAddr, writeLength,
                   readLength);
            for (i = 0; i < writeLength; i++)
            {
                printf("0x%02x ", rawCmd[i]);
            }
            printf("\n");
        }

        uint8_t* rawResp = (uint8_t*)calloc(readLength, sizeof(uint8_t));
        if (rawResp == NULL)
        {
            // calloc failed, abort the sequence
            printf("Raw command memory allocation failed\n");
            free(rawCmd);
            return 1;
        }
        while (loops--)
        {
            clock_gettime(CLOCK_REALTIME, &begin);
            ret = peci_raw(rawAddr, readLength, rawCmd, writeLength, rawResp,
                           readLength);
            timeSpent = getTimeDifference(begin);
            if (verbose && measureTime)
            {
                printf("\nTime taken in iteration %d = %lf s\n",
                       (loopCount - loops), timeSpent);
            }
            totalTimeSpent += timeSpent;

            if (verbose)
            {
                printf("   ");
                for (i = 0; i < readLength; i++)
                {
                    printf("0x%02x ", rawResp[i]);
                }
                printf("\n");
            }
            ccCounts[rawResp[0]]++;
        }
        if (!verbose)
        {
            if (0 != ret)
            {
                printf("ERROR %d: command failed\n", ret);
            }
            printf("   ");
            for (i = 0; i < readLength; i++)
            {
                printf("0x%02x ", rawResp[i]);
            }
            printf("\n");
        }

        if (looped)
        {
            printLoopSummary(ccCounts);
        }

        free(rawCmd);
        free(rawResp);
    }
    else
    {
        printf("ERROR: Unrecognized command\n");
        goto ErrorExit;
    }

    if (measureTime)
    {
        printf("Total time taken = %lf seconds\n", totalTimeSpent);
        if (loopCount > 1)
        {
            printf("Average time taken per command = %lf seconds\n",
                   totalTimeSpent / loopCount);
        }
    }

    return 0;

ErrorExit:
    Usage(argv[0]);
    return 1;
}
