/*
 * Copyright (c)  2018-present Facebook. All Rights Reserved.
 *
 * 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.
 */

#pragma once
#include "sdrutils.hpp"

#define SEL_JSON_DATA_FILE "/var/log/fbSelRaw.json"
#define KEY_SEL_COUNT "SelCount"
#define KEY_SEL_ENTRY_RAW "SelEntry"
#define KEY_ADD_TIME "AddTimeStamp"
#define KEY_ERASE_TIME "EraseTimeStamp"
#define KEY_FREE_SPACE "FreeSpace"
#define KEY_OPER_SUPP "OperationalSupport"
#define KEY_SEL_VER "SELVersion"
#define BIT(value, index) ((value >> index) & 1)

static constexpr uint8_t ipmiSdrVersion = 0x51;

#pragma pack(push, 1)

struct GetSDRReq
{
    uint16_t reservationID;
    uint16_t recordID;
    uint8_t offset;
    uint8_t bytesToRead;
};

struct GetFRUAreaResp
{
    uint8_t inventorySizeLSB;
    uint8_t inventorySizeMSB;
    uint8_t accessType;
};

struct GetFRUAreaReq
{
    uint8_t fruDeviceID;
    uint16_t fruInventoryOffset;
    uint8_t countToRead;
};

struct WriteFRUDataReq
{
    uint8_t fruDeviceID;
    uint16_t fruInventoryOffset;
    uint8_t data[];
};

#pragma pack(pop)

enum class GetFRUAreaAccessType : uint8_t
{
    byte = 0x0,
    words = 0x1
};

enum class IPMINetfnStorageCmds : ipmi_cmd_t
{
    ipmiCmdGetFRUInvAreaInfo = 0x10,
    ipmiCmdReadFRUData = 0x11,
    ipmiCmdWriteFRUData = 0x12,
    ipmiCmdGetRepositoryInfo = 0x20,
    ipmiCmdGetSDRAllocationInfo = 0x21,
    ipmiCmdReserveSDR = 0x22,
    ipmiCmdGetSDR = 0x23,
    ipmiCmdGetSELInfo = 0x40,
    ipmiCmdReserveSEL = 0x42,
    ipmiCmdGetSELEntry = 0x43,
    ipmiCmdAddSEL = 0x44,
    ipmiCmdDeleteSEL = 0x46,
    ipmiCmdClearSEL = 0x47,
    ipmiCmdGetSELTime = 0x48,
    ipmiCmdSetSELTime = 0x49,
};

#pragma pack(push, 1)
struct FRUHeader
{
    uint8_t commonHeaderFormat;
    uint8_t internalOffset;
    uint8_t chassisOffset;
    uint8_t boardOffset;
    uint8_t productOffset;
    uint8_t multiRecordOffset;
    uint8_t pad;
    uint8_t checksum;
};
#pragma pack(pop)

namespace fb_oem::ipmi::sel
{

static constexpr auto selVersion = 0x51;
static constexpr auto invalidTimeStamp = 0xFFFFFFFF;
/* Spec indicates that more than 64kB is free */
static constexpr auto freeSpace = 0xFFFF;
static constexpr uint8_t selOperationSupport = 0x02;

static constexpr auto firstEntry = 0x0000;
static constexpr auto lastEntry = 0xFFFF;
static constexpr auto entireRecord = 0xFF;
static constexpr auto selRecordSize = 16;

static constexpr auto stdErr = "Standard";
static constexpr auto oemTSErr = "OEM timestamped";
static constexpr auto fbUniSELErr = "Facebook Unified SEL";
static constexpr auto oemNTSErr = "OEM non-timestamped";
static constexpr auto unknownErr = "Unknown";

static constexpr uint8_t stdErrType = 0x02;
static constexpr uint8_t oemTSErrTypeMin = 0xC0;
static constexpr uint8_t oemTSErrTypeMax = 0xDF;
static constexpr uint8_t oemNTSErrTypeMin = 0xE0;
static constexpr uint8_t fbUniErrType = 0xFB;
static constexpr uint8_t oemNTSErrTypeMax = 0xFF;

static constexpr uint8_t unifiedPcieErr = 0;
static constexpr uint8_t unifiedMemErr = 1;

/* event sensor name in processing SEL */
static constexpr uint8_t memoryEccError = 0x63;
static constexpr uint8_t memoryErrLogDIS = 0x87;

/** @struct GetSELInfoData
 *
 * IPMI response payload data for Get SEL Info request
 */
struct GetSELInfoData
{
    uint8_t selVersion;
    uint16_t entries;
    uint16_t freeSpace;
    uint32_t addTimeStamp;
    uint32_t eraseTimeStamp;
    uint8_t operationSupport;
};

/** @struct GetSELEntryRequest
 *
 *  IPMI payload for Get SEL Entry command request.
 */
struct GetSELEntryRequest
{
    uint16_t reservID; //!< Reservation ID.
    uint16_t recordID; //!< SEL Record ID.
    uint8_t offset;    //!< Offset into record.
    uint8_t readLen;   //!< Bytes to read.
} __attribute__((packed));

/** @struct GetSELEntryResponse
 *
 *  IPMI payload for Get SEL Entry command response.
 */
struct GetSELEntryResponse
{
    uint16_t nextRecordID;  //!< Next RecordID.
    uint8_t recordData[16]; //!< Record Data.
} __attribute__((packed));

/** @struct AddSELEntryRequest
 *
 *  IPMI payload for ADD SEL Entry command request.
 */
struct StdSELEntry
{
    uint16_t recordID;        //!< Record ID.
    uint8_t recordType;       //!< Record Type.
    uint32_t timeStamp;       //!< Timestamp.
    uint16_t generatorID;     //!< Generator ID.
    uint8_t eventMsgRevision; //!< Event Message Revision.
    uint8_t sensorType;       //!< Sensor Type.
    uint8_t sensorNum;        //!< Sensor Number.
    uint8_t eventType;        //!< Event Dir | Event Type.
    uint8_t eventData1;       //!< Event Data 1.
    uint8_t eventData2;       //!< Event Data 2.
    uint8_t eventData3;       //!< Event Data 3.
} __attribute__((packed));

struct TsOemSELEntry
{
    uint16_t recordID;  //!< Record ID.
    uint8_t recordType; //!< Record Type.
    uint32_t timeStamp; //!< Timestamp.
    uint8_t mfrId[3];   //!< Manufacturer Id.
    uint8_t oemData[6]; //!< OEM Data.
} __attribute__((packed));

struct NtsOemSELEntry
{
    uint16_t recordID;   //!< Record ID.
    uint8_t recordType;  //!< Record Type.
    uint8_t oemData[13]; //!< OEM Data.
} __attribute__((packed));

static constexpr auto initiateErase = 0xAA;
static constexpr auto getEraseStatus = 0x00;
static constexpr auto eraseComplete = 0x01;

} // namespace fb_oem::ipmi::sel