/*
Copyright (C) 2008, 2009  GE Intelligent Platforms, Inc
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef __EGASIC_DEF_H__
#define __EGASIC_DEF_H__

/* Configuration Functions */
void egInitAsic (reginfo_t *regdb, int mode_type);
void egAsicInitFreePageTable (void);
void egAsicSpiProgramCalendar (int mode_type);
int egAsicRegisterCheckDefault (void);

/* DPG DPV related commands */
int egAsicDPGPacketSizeMode(int mode, int minsize, int maxsize);
int egAsicDPGCrcAppend (int mode, unsigned int crc_val);
int egAsicDPGVlanTagInsert (int mode, unsigned int vlan_tag);
int egAsicDPGSAAppend (int mode, unsigned int sa_lsb, unsigned int sa_msb);
int egAsicDPGDAAppend (int mode, unsigned int da_lsb, unsigned int da_msb);
int egAsicDPGPreambleAppend (int mode, unsigned int pre_lsb, unsigned int pre_msb);
int egAsicDPGNumPackets (int num_pkts);
int egAsicDPGGapSize (int mode, int size);
int egAsicDPGUserDataErrBitSet (int mode);
int egAsicDPGSetDataPattern (int mode, unsigned int patt_val);
int egAsicStartDPG (int port_num, int cfgDpgSettings);
void egAsicDPGSetDefault (void);

/* MDIO related functions */
#define MDIO_START_OF_FRAME 0x01
#define MDIO_WRITE_OPERATION 0x01
#define MDIO_READ_OPERATION 0x02
int egAsicMdioRead (unsigned int portAddr, unsigned int regAddr, unsigned int *regVal);
int egAsicMdioWrite (int portAddr, unsigned int regAddr, unsigned short regVal);

/* Two WIRE Interface related functions */
int egAsicTwoWireSerialWrite (int portNum, int mod_addr, int reg_addr, unsigned int data);
int egAsicTwoWireSerialRead (int portNum, int mod_addr, int reg_addr, unsigned int *data);

/* CPU Packet Insertion/Reception Functions */
int egAsicCpuPacketRecv (reginfo_t *regdb, int port, unsigned char *packet, int *length);
void egAsicCpuPacketSend (reginfo_t *regdb, unsigned char *pkt, int size, int port);

/* Memory Read Write Functions */
int egAsicReadInternalMemory (int addr, unsigned char *data);
int egAsicWriteInternalMemory (int addr, unsigned char *data);
int egAsicReadTxMemory (int portNum, int addr, unsigned char *data);
int egAsicWriteTxMemory (int portNum, int addr, unsigned char *data);

/* Used for registers with instances */
#define STAT_INST_UPDATE(regname, to) regGet(regdb, regname, port, to)
#define STAT_UPDATE(regname, to) regGet(regdb, regname, 0, to)

/* Eagle Statistic structures */
typedef struct egRxMacStats {
    unsigned int vLANTagRcvd;
    unsigned int pauseTime;
    unsigned int octetsRcvdOKMSB;
    unsigned int octetsRcvdOKLSB;
    unsigned int numOfBadOctetsMSB;
    unsigned int numOfBadOctetsLSB;
    unsigned int numOfBadFrms;
    unsigned int numOfCRCErrors;
    unsigned int numOfCRCErrorsGT1518;
    unsigned int numOfCRCErrorsGTMax;
    unsigned int numOfBcastFrms;
    unsigned int numOfMcastFrms;
    unsigned int frmsOf64Octets;
    unsigned int frmsOf65to127Octets;
    unsigned int frmsOf128to255Octets;
    unsigned int frmsOf256to511Octets;
    unsigned int frmsOf512to1023Octets;
    unsigned int frmsOf1024to1518Octets;
    unsigned int oversizeFrms;
    unsigned int undersizeFrms;
    unsigned int numOfFragments;
    unsigned int numOfLongErrors;
    unsigned int numOfJabbers;
    unsigned int fRcvdOK;
    unsigned int fTooLongErrors;
    unsigned int ctrlFRcvd;
    unsigned int unsuppOpcodeRcvd;
    unsigned int goodVLANFRcvd;
    unsigned int badSAFRcvd;
    unsigned int dataError;
    unsigned int uCASTDrop;
    unsigned int filDrop;
} egRxMacStats;

typedef struct rxDrpCtrlStats {
    unsigned int oVLANCtrlRdData;
    unsigned int oVLANCtrlRdDataPro;
    unsigned int oVLANIDRdData;
    unsigned int hashRdData;
    unsigned int dSCPAssignData;
    unsigned int tdmAbortedPkts[24];
    unsigned int tdmDroppedPkts[24];
    unsigned int rxDefaultDroppedFrms[24];
    unsigned int pedBCastFrms[24];
    unsigned int rxMPLSDroppedFrms[24];
    unsigned int rxVLANEtherDropFrms[24];
    unsigned int rxVLANControlDropFrms[24];
    unsigned int rxMACDropFrms[24];
    unsigned int rxDSCPIPDropFrms[24];
    unsigned int rxIllegalTypeLenFrms[24];
    unsigned int greenFrames[24];
    unsigned int redYellowFrames[24];
    unsigned int controlEOFs;
} rxDrpCtrlStats;

typedef struct rxWredStats {
    unsigned int eopCount[24];
    unsigned int wREDDropQ_0[24];
    unsigned int wREDDropQ_1[24];
    unsigned int wREDDropQ_2[24];
    unsigned int wREDDropQ_3[24];
    unsigned int bPDUDropQ_0[24];
    unsigned int bPDUDropQ_1[24];
    unsigned int bPDUDropQ_2[24];
    unsigned int bPDUDropQ_3[24];
    unsigned int tailDropQ_0[24];
    unsigned int tailDropQ_1[24];
    unsigned int tailDropQ_2[24];
    unsigned int tailDropQ_3[24];
} rxWredStats;

typedef struct rxLine2CpuStats {
    unsigned int nearlyFull;
    unsigned int nearlyEmpty;
    unsigned int tailDropCount;
    unsigned int pktCnt;
    unsigned int writePointer;
    unsigned int readPointer;
    unsigned int commitPointer;
    unsigned int lnDataReg_0;
    unsigned int lnDataReg_1;
    unsigned int lnDataReg_2;
    unsigned int lnDataReg_3;
    unsigned int lnDataReg_4;
    unsigned int lnDataReg_5;
    unsigned int lnDataReg_6;
    unsigned int lnDataReg_7;
    unsigned int lnDataReg_8;
    unsigned int lnDataRegVld;
} rxLine2CpuStats;

typedef struct rxEnqStats {
    unsigned int egrPktCnt;
    unsigned int perQueueDropPktCnt[96];
} rxEnqStats;

typedef struct rxQueueMgrPageStats {
    unsigned int maxPageCountRd[96];
    unsigned int rsvdPageCountRd[96];
    unsigned int usedPageCountRd[96];
    unsigned int queueFillLevel[96];
} rxQueueMgrPageStats;

typedef struct rxQueueMgrPktStats {
    unsigned int qmQueueNotEmpty0;
    unsigned int qmQueueNotEmpty1;
    unsigned int qmQueueNotEmpty2;
    unsigned int rsvdPoolPagePntr;
    unsigned int freePoolPagePntr;
    unsigned int memoryFullCount[24];
} rxQueueMgrPktStats;

typedef struct rxIntMemCtrlStats {
    unsigned int readData_0;
    unsigned int readData_1;
    unsigned int readData_2;
    unsigned int readData_3;
    unsigned int readData_4;
    unsigned int readData_5;
    unsigned int readData_6;
    unsigned int readData_7;
    unsigned int currReadAddress;
    unsigned int currWriteAddress;
} rxIntMemCtrlStats;

typedef struct rxDeqStats {
    unsigned int eOFCount;
    unsigned int pktCount[96];
} rxDeqStats;

typedef struct rxSpiStats {
    unsigned int minMdrrCreditCount[24];
    unsigned int maxMdrrCreditCount[24];
    /* unsigned int minMdrrCreditCount;
    unsigned int maxMdrrCreditCount; */
    unsigned int miscSPIHM;
    unsigned int ampleUsePipeEmpty;
    unsigned int sopCount;
    unsigned int eop1Count;
    unsigned int eop2Count;
    unsigned int eopACount;
    unsigned int trainingCount;
    unsigned int burstFifoUFlow;
    unsigned int outputFifoUFlow;
    unsigned int ampleUseSopCnt;
    unsigned int dip2ErrCount;
    unsigned int losStatus;
    unsigned int shdwCalendarAct;
} rxSpiStats;

typedef struct egTxMacStats {
    unsigned int goodByteCountMSB;
    unsigned int goodByteCountLSB;
    unsigned int errorByteCountMSB;
    unsigned int errorByteCountLSB;
    unsigned int goodFrames;
    unsigned int bCASTFrames;
    unsigned int mCASTFrames;
    unsigned int controlFrames;
    unsigned int vLANFrames;
    unsigned int pauseFrames;
    unsigned int frames64B;
    unsigned int frames65to127B;
    unsigned int frames128to255B;
    unsigned int frames256to511B;
    unsigned int frames512to1023B;
    unsigned int frames1024to1518B;
    unsigned int over1518BFrames;
    unsigned int erroredFrames;
    unsigned int filterDropFrames;
    unsigned int tooLongFrames;
    unsigned int droppedFrames;
    unsigned int fIFOUnderRun;
    unsigned int cRCErrorCountLT64;
    unsigned int cRCErrorCount;
    unsigned int cRCErrorCountGT1518;
    unsigned int cRCErrorCountGTMax;
} egTxMacStats;

typedef struct txMemMgrStats {
    /* UCAST Related Stats */
    unsigned int ucastTailDropCountReg[24];
    unsigned int ucastFifoPtrsReg[24];
    unsigned int ucastFifoNotEmptyReg;

    /* MCAST Related Stats */
    unsigned int mcastTailDropCountReg[24];
    unsigned int mcastFrmsOutCntReg[24];
    unsigned int mcastFifoPtrsReg[24];
    unsigned int mcastFifoNotEmptyReg;
    unsigned int mismatchFrameDropCntReg;
    unsigned int framesSentReg;
} txMemMgrStats;

typedef struct txCpu2LineStats {
    unsigned int fifoFull;
    unsigned int fifoEmpty;
    unsigned int tailDropPktCnt;
} txCpu2LineStats;

typedef struct txSpiStats {
    unsigned int addrMapMemRdData_0;
    unsigned int addrMapMemRdData_1;
    unsigned int addrMapMemRdData_2;
    unsigned int addrMapMemRdData_3;
    unsigned int oneLinePktDrop;
    unsigned int trainingCount;
    unsigned int sopCount;
    unsigned int eopCount;
    unsigned int extAbortEopCount;
    unsigned int intAbortEopCount;
    unsigned int dip4ErrCount;
    unsigned int protViolCount;
    unsigned int fifoOverFlow;
    unsigned int dPGStatus;
    unsigned int dPVByteErrCnt0;
    unsigned int dPVByteErrCnt1;
    unsigned int dPVByteErrCnt2;
    unsigned int dPVByteErrCnt3;
    unsigned int dPVByteErrCnt4;
    unsigned int dPVByteErrCnt5;
    unsigned int dPVByteErrCnt6;
    unsigned int dPVByteErrCnt7;
    unsigned int dPVGoodPktCnt;
    unsigned int dPVSyncOnCnt;
    unsigned int dPVEopCnt;
    unsigned int dPGEopCnt;
    unsigned int ampleUseDDS2;
} txSpiStats;

/* Statistc related Functions */
void egAsicRxMacStats (reginfo_t *regdb, int port, egRxMacStats *stat);
void egAsicRxDrpCtrlStats (reginfo_t *regdb, rxDrpCtrlStats *stat);
void egAsicRxWredStats (reginfo_t *regdb, rxWredStats *stat);
void egAsicRxLine2CpuStats (reginfo_t *regdb, rxLine2CpuStats *stat);
void egAsicRxEnqStats (reginfo_t *regdb, rxEnqStats *stat);
void egAsicRxQueueMgrPageStats (reginfo_t *regdb, rxQueueMgrPageStats *stat);
void egAsicRxQueueMgrPktStats (reginfo_t *regdb, rxQueueMgrPktStats *stat);
void egAsicRxIntMemCtrlStats (reginfo_t *regdb, rxIntMemCtrlStats *stat);
void egAsicRxDeqStats (reginfo_t *regdb, rxDeqStats *stat);
void egAsicRxSpiStats (reginfo_t *regdb, rxSpiStats *stat);
void egAsicTxMacStats (reginfo_t *regdb,  int port, egTxMacStats *stat);
void egAsicTxMemMgrStats (reginfo_t *regdb, txMemMgrStats *stat);
void egAsicTxCpu2LineStats (reginfo_t *regdb, txCpu2LineStats *stat);
void egAsicTxSpiStats (reginfo_t *regdb, txSpiStats *stat);

/* Statistic Display Functions */
void egAsicPrintRxMacStats (int port);
void egAsicPrintRxDrpCtrlStats (void);
void egAsicPrintRxWredStats (void);
void egAsicPrintRxLine2CpuStats (void);
void egAsicPrintRxEnqStats (void);
void egAsicPrintRxQueueMgrPageStats (void);
void egAsicPrintRxQueueMgrPktStats (void);
void egAsicPrintRxIntMemCtrlStats (void);
void egAsicPrintRxDeqStats (void);
void egAsicPrintRxSpiStats (void);
void egAsicPrintTxMacStats (int port);
void egAsicPrintTxMemMgrStats (void);
void egAsicPrintTxCpu2LineStats (void);
void egAsicPrintTxSpiStats (void);
int egAsicPrintAllStats (char *file_name);
void egAsicClearAllStats(void);

/* Default Check Functions */
void egAsicCheckCommonSlaveDefaults (void);
void egAsicCheckLedDefaults (void);
void egAsicCheckMdioSlaveDefaults (void);
void egAsicCheckPhyMdioDefaults (void);
void egAsicCheckRxDequeuerDefaults (void);
void egAsicCheckRxDropControlDefaults (void);
void egAsicCheckRxEnqueuerDefaults (void);
void egAsicCheckRxInternalMemoryDefaults (void);
void egAsicCheckRxLine2CPUDefaults (void);
void egAsicCheckRxMacDefaults (void);
void egAsicCheckRxQueueManagerDefaults (void);
void egAsicCheckRxSpiDefaults (void);
void egAsicCheckRxWredDefaults (void);
void egAsicCheckTxCpu2LineDefaults (void);
void egAsicCheckTxMacDefaults (void);
void egAsicCheckTxMemoryManagerDefaults (void);
void egAsicCheckTxSpiDefaults (void);
void egAsicInitForDefaultCheck (void);
void egCheckDefVal (int regId, unsigned int defVal);

/* Register RW Functions */
void egAsicCommonRW (void);
void egAsicMdioRW (void);
void egAsicLedRW (void);
void egAsicPhyPcsRW (void);
void egAsicRxMacRW (void);
void egAsicDrpCtrlRW (void);
void egAsicRxWredRW (void);
void egAsicRxLine2CpuRW (void);
void egAsicRxMemEnqRW (void);
void egAsicRxMemDatabaseMgrRW (void);
void egAsicRxIntMemoryRW (void);
void egAsicRxDeqRW (void);
void egAsicRxSpiRW (void);
void egAsicTxMacRW (void);
void egAsicTxMemUCastRW (void);
void egAsicTxMemMCastRW (void);
void egAsicTxMemMgrCpuRW (void);
void egAsicTxCpu2LineRW (void);
void egAsicTxSpiRW (void);

/* Misc Functions */
int egAsicConfigHashFilter (int hashMode);
int egAsicConfigDAFilter (void);
int egAsicProgramMCastRegisters (void);
int egAsicConfigTypeFilter (void);
void egAsicConfigTxMem16K (void);
void egAsicWriteSpiDeskewReg (unsigned int addr, unsigned int data);
void egAsicReadSpiDeskewReg (unsigned int addr, unsigned int *data);
void egAsicConfigFiberPort (int portNum);
int egAsicFindFiberModule (int portNumber);
void egAsicConfigTxFlowControl (int portNum, int pauseMode);
void egAsicConfigGigRxMAC (int portNum, int mtuSizeMode);
void egAsicConfigBCastFilter (int portNum, int percentage);
void egAsic8021PBitConfiguration (void);
void egAsicConfigDSCP (void);
void egAsicConfigWRED (void);
void egAsicLEDActivityConfig (int type);
void egAsicLEDStatusConfig (int type);
void egAsicConfigRxSPIRateLimiter (int portNum, unsigned long long minBW, unsigned long long maxBW, unsigned long long windowSize, int maxBurstSize);
void egAsicPortEnableStrict3Mode (int portNum);
void egAsicPortDisableStrict3Mode (int portNum);
int egAsicPerformAutoNegFiber (int portNum);
int egAsicPerformAutoNegSGMII (int portNum, int speed);
void egAsicSGMIISpeedConfig (int portNum, int speed);
void programMACSpeed ( int portNum, int macSpeed);

/* Interrupt definitions */
#define PHY_INTR          BIT_15
#define MAC_INTR          BIT_13
#define RXSPI_INTR        BIT_12
#define TXSPI_INTR        BIT_11
#define RXQUEUE_MGR_INTR  BIT_10
#define RXDEQUEUE_INTR    BIT_9
#define RXENQ_INTR        BIT_8
#define RXINT_MEM_INTR    BIT_7
#define TXMEM_MGR_INTR    BIT_6
#define TXCPU2LINE_INTR   BIT_5
#define RXLINE2CPU_INTR   BIT_4
#define RXDROPCTRL_INTR   BIT_3
#define EG_MDIO_INTR         BIT_2
#define EG_COMMON_INTR       BIT_0

/* Interrupt Routines */
struct egAsicInterrupts_ {
    struct commonIntr_ {
        unsigned int total;
        unsigned int pllLockInterrupt;
        unsigned int clkMonInterrupt;
        unsigned int sfpModTxFaultInterrupt;
        unsigned int twoWireMasterInterrupt;
        unsigned int regTimeoutInterrupt;
    } commonIntr;

    struct mdioIntr_ {
        unsigned int total;
        unsigned int macInterrupt;
        unsigned int mdioInterrupt;
        unsigned int rxInterrupt;
    } mdioIntr;

    /* For all 24 ports */
    struct rxMacIntr_ {
        unsigned int rxMacIntr;
    } rxMacIntr[24];

    struct drpCtrlIntr_ {
        unsigned int total;
        unsigned int cpuEccSingleErrorInterrupt;
        unsigned int cpuEccMultiErrorInterrupt;
        unsigned int ctrlEccSingleErrorInterrupt;
        unsigned int ctrlEccMultiErrorInterrupt;
    } drpCtrlIntr;

    struct rxline2cpuIntr_ {
        unsigned int total;
        unsigned int cpuLnDataPresentInterrupt;
        unsigned int cpuOverflowInterrupt;
        unsigned int cpuEmptyInterrupt;
        unsigned int cpuUnderrunInterrupt;
        unsigned int cpuEccSingleErrorInterrupt;
        unsigned int cpuEccDoubleErrorInterrupt;
    } rxline2cpuIntr;

    struct rxQueueMgrIntr_ {
        unsigned int total;
        unsigned int totalRsvdPageCountInterrupt;
        unsigned int pageFifoOverflowInterrupt;
        unsigned int globalFullnessInterrupt;
    } rxQueueMgrIntr;

    struct rxIntMemIntr_ {
        unsigned int total;
        unsigned int eccSingleErrorInterrupt;
        unsigned int eccDoubleErrorInterrupt;
    } rxIntMemIntr;

    struct rxSpiIntr_ {
        unsigned int spiIntr;
    } rxSpiIntr;

    /* For all 24 ports */
    struct txMacIntr_ {
        unsigned int macIntr;
    } txMacIntr[24];

    struct txMemCpuIntr_ {
        unsigned int total;
        unsigned int eccErrorInterruptReg0;
        unsigned int eccErrorInterruptReg1;
        unsigned int eccErrorInterruptReg2;
        unsigned int eccErrorInterruptReg3;
        unsigned int eccErrorInterruptReg4;
        unsigned int eccErrorInterruptReg5;
        unsigned int eccErrorInterruptReg6;
        unsigned int eccErrorInterruptReg7;
        unsigned int eccErrorInterruptReg8;
        unsigned int eccErrorInterruptReg9;
        unsigned int eccErrorInterruptReg10;
        unsigned int eccErrorInterruptReg11;
        unsigned int eccErrorInterruptReg12;
        unsigned int eccErrorInterruptReg13;
        unsigned int eccErrorInterruptReg14;
        unsigned int eccErrorInterruptReg15;
        unsigned int eccErrorInterruptReg16;
        unsigned int eccErrorInterruptReg17;
        unsigned int eccErrorInterruptReg18;
        unsigned int eccErrorInterruptReg19;
        unsigned int eccErrorInterruptReg20;
        unsigned int eccErrorInterruptReg21;
        unsigned int eccErrorInterruptReg22;
        unsigned int eccErrorInterruptReg23;
    } txMemCpuIntr;

    struct txCpuToLineIntr_ {
        unsigned int total;
        unsigned int fifoOverflowInterrupt;
        unsigned int eccSingleErrorInterrupt;
        unsigned int eccDoubleErrorInterrupt;
    } txCpuToLineIntr;

    struct txSpiIntr_ {
        unsigned int spiIntr;
    } txSpiIntr;
};

void egAsicInterruptHandler (reginfo_t *regdb);

#endif /* __EGASIC_DEF_H__ */
