/*******************************************************************************
**+--------------------------------------------------------------------------+**
**|                            ****                                          |**
**|                            ****                                          |**
**|                            ******o***                                    |**
**|                      ********_///_****                                   |**
**|                      ***** /_//_/ ****                                   |**
**|                       ** ** (__/ ****                                    |**
**|                           *********                                      |**
**|                            ****                                          |**
**|                            ***                                           |**
**|                                                                          |**
**|         Copyright (c) 1998-2006 Texas Instruments Incorporated           |**
**|                        ALL RIGHTS RESERVED                               |**
**|                                                                          |**
**+--------------------------------------------------------------------------+**
*******************************************************************************/

/** \file   ddc_cpsar.h
    \brief  DDC CPSAR header file

    This file is the CPSAR DDC header file to be included by the CPSAR DDA
    driver files. This file is compliant to the PSP Framework 1.0 definitions
    and prototypes.

    \note When including this file from DDC source code, CPSAR_DDC must be
    defined.

    Documentation for #defines that may be included in makefiles:

    CPSAR_MULTIFRAGMENT - must be defined by the make file to support
      multifragment packets - single frag code not available yet
    CPSAR_DDC_DEBUG - should be defined by the makefile for compiling in debug
      statements
    CPSAR_DDC_GETSTATS - enables statistic counters

    Features not supported currently:
    1) Multipacket transmit and receive
    2) Tx interrupt disable mode

    @author     Greg Guyotte
 */

#ifndef __DDC_CPSAR_H__
#define __DDC_CPSAR_H__

#include "ddc_cpsarcfg.h"   /* DDC Configuration file for ready reference to some HW cfg macros */
#include "cslr_cpsar.h"     /* CSL Header file */
#include "pal_cppi4.h"      /* CPPI4 PAL interface header file */
#include "ddc_netdev.h"     /* DDC Net device header file. */

/**
 * \defgroup CPSAR_DDC_Interface    CPSAR DDC Interface
 *
 *  CPSAR DDC Layer Interface
 */
/*@{*/

/**
 *  \brief DDC CPSAR Error Codes
 *
 *  Refer to DDC Error codes description.
 */

/* Defining the macro CPSAR_INSTANCE_CODE to 0 so that it can be usable in DDA */
#define CPSAR_INSTANCE_CODE                     0 /* Arbitrary base */
#define CPSAR_ERROR_CODE                        ((DDC_ERROR | (CPSAR_INSTANCE_CODE << 16)) + DDC_NETDEV_ERROR_MAX)
#define CPSAR_ERROR_INFO                        (CPSAR_ERROR_CODE)
#define CPSAR_ERROR_WARNING                     (CPSAR_ERROR_CODE | 0x10000000)
#define CPSAR_ERROR_MINOR                       (CPSAR_ERROR_CODE | 0x20000000)
#define CPSAR_ERROR_MAJOR                       (CPSAR_ERROR_CODE | 0x30000000)
#define CPSAR_ERROR_CRITICAL                    (CPSAR_ERROR_CODE | 0x40000000)

/* CPSAR Success code */
#define CPSAR_SUCCESS                           PAL_SOK

/* CPSAR Error codes */
#define CPSAR_ERR_DEV_ALREADY_INSTANTIATED(instID) (0x30000000 + DDC_ERROR + DDC_NETDEV_ERROR_MAX + ((instId) << 16) )
#define CPSAR_ERR_DEV_NOT_INSTANTIATED          (CPSAR_ERROR_MAJOR + 1)
#define CPSAR_INVALID_PARAM                     (CPSAR_ERROR_MAJOR + 2)
#define CPSAR_ERR_TX_CH_INVALID                 (CPSAR_ERROR_CRITICAL + 3)
#define CPSAR_ERR_TX_CH_ALREADY_INIT            (CPSAR_ERROR_MAJOR + 4)
#define CPSAR_ERR_TX_CH_ALREADY_CLOSED          (CPSAR_ERROR_MAJOR + 5)
#define CPSAR_ERR_TX_NO_LINK                    (CPSAR_ERROR_MAJOR + 6)
#define CPSAR_ERR_TX_OUT_OF_BD                  (CPSAR_ERROR_MAJOR + 7)
#define CPSAR_ERR_RX_CH_INVALID                 (CPSAR_ERROR_CRITICAL + 8)
#define CPSAR_ERR_RX_CH_ALREADY_INIT            (CPSAR_ERROR_MAJOR + 9)
#define CPSAR_ERR_RX_CH_ALREADY_CLOSED          (CPSAR_ERROR_MAJOR + 10)
#define CPSAR_ERR_RX_CH_NOT_OPEN                (CPSAR_ERROR_MAJOR + 11)
#define CPSAR_ERR_DEV_NOT_OPEN                  (CPSAR_ERROR_MAJOR + 12)
#define CPSAR_ERR_DEV_ALREADY_CLOSED            (CPSAR_ERROR_MAJOR + 13)
#define CPSAR_ERR_DEV_ALREADY_OPEN              (CPSAR_ERROR_MAJOR + 14)
#define CPSAR_ERR_FIRMWARE_TOO_LARGE            (CPSAR_ERROR_MAJOR + 15)
#define CPSAR_ERR_PDSP_LOAD_FAIL                (CPSAR_ERROR_CRITICAL + 16)
#define CPSAR_ERR_LUT_NOT_READY                 (CPSAR_ERROR_CRITICAL + 17)
#define CPSAR_INTERNAL_FAILURE                  (CPSAR_ERROR_MAJOR + 18)
#define CPSAR_ERR_TX_CH_ALREADY_OPENED          (CPSAR_ERROR_MAJOR + 19)
#define CPSAR_ERR_RX_CH_ALREADY_OPENED          (CPSAR_ERROR_MAJOR + 20)

/**
 *  \brief CPSAR DDC Ioctl's
 *
 */
#define DDC_NET_CPMAC_IOCTL_BASE                0       /* Arbitrary base */
#define CPSAR_DDC_IOCTL_GET_SWVER               DDC_IOCTL(DDC_NET_IOCTL_MIN, 2)
#define CPSAR_DDC_IOCTL_GET_HWVER               DDC_IOCTL(DDC_NET_IOCTL_MIN, 3)
#define CPSAR_DDC_IOCTL_SET_INITCFG             DDC_IOCTL(DDC_NET_IOCTL_MIN, 4)
#define CPSAR_DDC_IOCTL_GET_STATUS              DDC_IOCTL(DDC_NET_IOCTL_MIN, 5)
#define CPSAR_DDC_IOCTL_GET_STATISTICS          DDC_NET_IOCTL_GET_NET_STATS
#define CPSAR_DDC_IOCTL_CLR_STATISTICS          DDC_NET_IOCTL_CLR_NET_STATS

/**
 *  \brief CPSAR DDA Ioctl's
 *
 *  Called by the DDC layer, implemented by DDA layer in Control function
 */
#define DDA_NET_CPSAR_IOCTL_BASE                0    /* Arbitrary Base */
#define CPSAR_DDA_IOCTL_TEARDOWN_PEND           (DDA_NET_CPSAR_IOCTL_BASE + 1)

#if 0
/**
 *  \brief Net Channel State
 *
 *  State of the channel (initialized, about to be closed, closed etc
 */
typedef enum
{
    DDC_NET_CH_DIR_TX = 0,              /**< Transmit only */
    DDC_NET_CH_DIR_RX,                  /**< Receive only */
    DDC_NET_CH_DIR_BIDIRECTIONAL,       /**< Bidirectonaly - TX/RX  */
    DDC_NET_CH_DIR_UNDEFINED            /**< Not defined */
    /* ... more generic channel states */
} DDC_NetChDir;

/**
 *  \brief Net Channel State
 *
 *  State of the channel (initialized, about to be closed, closed etc
 */
typedef enum
{
    DDC_NET_CH_UNINITIALIZED = 0,       /**< Uninitialized state */
    DDC_NET_CH_INITIALIZED,             /**< Initialization complete, resources allocated */
    DDC_NET_CH_OPENED,                  /**< Channel in open state */
    DDC_NET_CH_CLOSE_IN_PROGRESS,       /**< Channel close/teardown in progress */
    DDC_NET_CH_CLOSED                   /**< Channel is closed, resources deallocated - require this ? */
    /* ... more generic channel states */
} DDC_NetChState;

/**
 *  \brief DDC_CpsarRxPktProcess
 *
 *  DDA calls the Rx packet processing function when there are pending Rx packets
 *  to be processed.  DDA typically calls this function in a thread context.
 *
 *  @param  hDDC        DDC Handle
 *  @param  channel     Channel number
 *  @param  pktsPending Not currently used.
 *  @param  pktArgs     Not currently used.
 *  @return Number of packets processed
 */
typedef int (*DDC_CpsarRxPktProcess) (
            CpsarDDCObj    *hDDC,
        int             channel,
            int             *pktsPending,
            void *             pktArgs);

/**
 *  \brief Network Data Token
 *
 *  Token associated with a network data buffer/fragment
 *  E.g.  Linux: skb, VxWorks: MBlk
 */
typedef Ptr DDC_NetDataToken;


/**
 *  \brief Network Buffer Object
 *
 *  Holds attributes of a buffer/fragment
 *  \note
 *  \n \b Send: Usually when the buffers are allocated by DDA, the Start of Packet token
 *  will be the handle to the whole packet. This token/handle should be good enough
 *  to free the packet or return to its pool. When the buffers are allocated by
 *  DDC, typically token for each buffer needs to be indicated (TxComplete) rather
 *  than only the Start of Packet token.
 *  \n \b Receive: For each buffer the token will be a handle to the buffer that can be
 *  used by the allocater (DDA or DDC) of the buffer to free it or return to a pool.
 */
typedef struct
{
    DDC_NetDataToken    bufToken;       /**< Buffer Token. */
    Char                *dataPtr;       /**< Pointer to data buffer */
    Int                 length;         /**< Buffer Length (number of bytes) */
} DDC_NetBufObj;


/**
 *  \brief Network Packet Object
 *
 *  Holds attributes of a network packet (NetBufObjs and packet size).
 */
typedef struct
{
    DDC_NetDataToken    pktToken;       /**< OS Data Token /may hold Tx/Rx chan id */
    DDC_NetBufObj       *bufList;       /**< Array of Network Buffer objects */
    Int                 numBufs;        /**< Number of Network Buffer objects */
    Int                 pktLength;      /**< Packet Length (number of bytes) */
} DDC_NetPktObj;

#endif

/**
 *  \brief CPSAR Channel Config Info
 *
 *  Common to both TX/RX.  Used to pass channel config info from DDA to DDC for
 *  CPSAR channels.  Note that the caller of ChOpen is required to fill the
 *  given CPPI4 channel structures with data.
 */
typedef struct
{
  int             chNum;              /**< DDC_NetChInfo: Channel number */
  DDC_NetChDir    chDir;              /**< DDC_NetChInfo: Channel direction */
  DDC_NetChState  chState;            /**< DDC_NetChInfo: Channel state */
  int             numTxBD;            /**< Number of Tx BD */

  /* SAR Channel Parameters */
  int Priority;       /**< Priority bin this channel will be scheduled within. */
  int PktType;        /**< 0=AAL5,1=Null AAL,2=OAM,3=Transparent,4=AAL2. */
  int DaMask;         /**< Specifies whether credit issuance is paused when Tx data not available. */
  /* Tx VC State */
  int TxVc_CellRate;  /**< Tx rate, set as clock ticks between transmissions (SCR for VBR, CBR for CBR). */
  int TxVc_QosType;   /**< 0=CBR,1=VBR,2=UBR,3=UBRmcr. */
  int TxVc_Mbs;       /**< Min Burst Size in cells.*/
  int TxVc_Pcr;       /**< Peak Cell Rate for VBR in clock ticks between transmissions. */
  int TxVc_OamTc;     /**< TC Path to transmit OAM cells for TX connection (0,1). */
  int TxVc_VpOffset;  /**< Offset to the OAM VP state table. */
  /* Rx VC State */
  int RxVc_OamCh;     /**< Ch to terminate rx'd OAM cells to be forwarded to the host. */
  int RxVc_OamToHost; /**< 0=do not pass, 1=pass. */
  int RxVc_OamTc;     /**< TC Path to transmit OAM cells for RX connection (0,1). */
  int RxVc_VpOffset;  /**< Offset to the OAM VP state table. */
  /* Tx VP State */
  int TxVp_OamTc;     /**< TC Path to transmit OAM cells for TX VP connection (0,1). */
  /* Rx VP State  */
  int RxVp_OamCh;     /**< Ch to terminate rx'd OAM cells to be forwarded to the host. */
  int RxVp_OamToHost; /**< 0=do not pass, 1=pass. */
  int RxVp_OamTc;     /**< TC Path to transmit OAM cells for RX VP connection (0,1). */
  int RxVp_OamVcList; /**< Indicates all VC channels associated with this VP channel (one-hot encoded). */

  int CpcsUU;         /**< AAL5 trailer field */
  unsigned int AtmHeader;   /**< Atm header to be used on Tx packets */

  unsigned int    TxServiceMax;
  unsigned int    RxServiceMax;
  unsigned int    TxNumBuffers;
  unsigned int    RxNumBuffers;
  unsigned int    RxBufSize;
  Cppi4TxChInfo   cppi4TxChInfo;        /**< CPPI4 Tx Ch Parameters */
  Cppi4RxChInfo   cppi4RxChInfo;        /**< CPPI4 Rx Ch Parameters */
} CpsarChInfo;

/**
 *  \brief CPSAR Init Configuration
 *
 *  Configuration information provided to DDC layer during initialization.
 *  DDA gets the config information from the OS/PAL layer and passes the relevant
 *  config to the DDC during initialization. The config info can come from various
 *  sources - static compiled in info, boot time (ENV, Flash) info etc.
 */
typedef struct
{
  unsigned int      instId;             /**< DDC Init Cfg: Instance number */
  unsigned int      numTxChannels;      /**< DDCNet Init Cfg: Number of Tx Channels to be supported */
  unsigned int      numRxChannels;      /**< DDCNet Init Cfg: Number of Rx Channels to be supported */
  unsigned int      cpsarBusFrequency;  /**< Bus frequency at which this module is operating */
  unsigned int      cpuFrequency;       /**< CPU frequency */
  unsigned int      baseAddress;        /**< CPSAR peripheral device's register overlay address */
  unsigned int      rxMaxFrags;         /**< Maximum number of rx fragments to support - must be at least 1 */
  unsigned int      scratchpadAddress;  /**< Address of scratchpad registers */
  unsigned int      useLoopFirmware;    /**< 1 = use loopback firmware */
  unsigned int      *loopFirmware;       /**< Address of loopback firmware */
  unsigned int      loopFirmwareSize;   /**< Size of loopback firmware */
  unsigned int      *firmware;           /**< Address of regular firmware */
  unsigned int      firmwareSize;       /**< Size of regular firmware */
  unsigned int      oamLbTimeout;       /**< OAM loopback timeout, in msecs */
  unsigned int      uniNni;             /**< 0 = NNI, 1 = UNI */
  unsigned int      oamMode;            /**< 0 = host OAM, 1 = firmware OAM */
  unsigned int      deviceCPID[4];      /**< OAM Connection Point Identifier */
  unsigned int      lbSourceLLID[4];    /**< OAM Loopback Location Identifier */
  Cppi4InitConfig   cppi4InitCfg;   /**< CPPI4 PAL Initialization Config */
} CpsarInitConfig;

/*
 *  The CPDST DDC object internals are hidden from the upper layers. Hence the
 *  internal data structure for CpdstDDCObj is in the DDC internal header and the
 *  upper layers interpret DDC_Handle as a void *.  All DDC files must be compiled
 *  with the switch "CPSAR_DDC" so that the DDC can interpret a DDC_Handle as
 *  a data structure.
 */
#ifndef CPSAR_DDC
typedef DDC_Handle *CpsarDDCObj;
#endif

/********************** CPSAR DDC External API Documentation *******************/


/**
 *  \brief DDC_CpsarTxPktProcess
 *
 *  DDA calls the packet packet processing function when the device interrupt is
 *  pending.  DDA typically calls this function in a thread context.
 *
 *  @param  hDDC        DDC Handle
 *  @param  channel     Interrupt channel number
 *  @param  pktsPending Not currently used.
 *  @param  pktArgs     Not currently used.
 *  @return Number of packets processed
 */
#if 0
typedef int (*DDC_CpsarTxPktProcess) (
            CpsarDDCObj    *hDDC,
        int             channel,
            int             *pktsPending,
            void *             pktArgs);

#endif

/**
 * \brief DDA Callback Interface
 *
 * Callback functions provided by the DDA layer to be called by DDC.
 */
#if 0
    //int DDA_atm_receive (void *os_dev, int ch, unsigned int packet_size, void *os_receive_info, void *data);
    int DDA_cpsarNetRx(void* hDDA, DDC_NetPktObj *pkt, void * rxArgs, void * arg);

    //void DDA_free_buffer(OS_RECEIVEINFO *OSRecieveInfo, void *memptr);
    int DDA_cpsarNetFreeRxBuf(void* hDDA, void * buffer, DDC_NetDataToken dataToken, unsigned int channel, void * freeArgs);


    //void * DDA_malloc_rxbuffer(unsigned int size, void *mem_base, unsigned int mem_range, void *osSetup,HAL_RECEIVEINFO *hal_info, OS_RECEIVEINFO **os_receive_info, OS_DEVICE *os_dev);
    void *DDA_cpsarNetAllocRxBuf(void* hDDA, int bufSize, DDC_NetDataToken *dataToken, unsigned int channel, void * allocArgs);

    //int  DDA_send_complete(OS_SENDINFO *OsSendInfo);
    int DDA_cpsarNetTxComplete(CpsarDevice* hDDA, DDC_NetDataToken *netDataTokens, int numTokens, unsigned int channel);

    //int DDC_sar_control(void *dev_info, const char *key, const char *action, void *value);
    int DDA_cpsarControlCb(CpsarDevice* hDDA, int cmd, void * cmdArg, void * param);

    /* Debug printf and error functions */
    extern int DDA_Printf(const char *format, ...);
    extern int DDA_ErrLog(const char *format, ...);
#endif


/**
 *  \brief DDC_cpsarGetVersionInfo
 *
 *  This function returns the version information of the DDC implementation for
 *  CPSAR device. The version number is returned in the pointer parameter passed
 *  to the function and the string is returned as the return value of the
 *  function.
 *
 *  @param  swVer       Placeholder for returning Version Id (major/minor
 *                      version).  Major Version Id (MSB 16 bits), minor version
 *                      Id (LSB 16 bits)
 *  @return Returns version string (array of char's stored in DDC)
 */
String DDC_cpsarGetVersionInfo(unsigned int *swVer);

/**
 *  \brief DDC_cpsarCreateInstance
 *
 *  CPSAR Instance creation function. DDA/DDC Handle and interface pointer
 *  exchange happens here.
 *
 *  @param  instId          Instance identifier
 *  @param  hDDA            Handle to the DDA layer
 *  @param  hDDA            Handle to the DDA layer
 *  @param  hDDACbIf        Handle to the DDA Callback interface structure
 *  @param  hDDC            Placeholder for Handle to the DDC layer
 *  @param  hDDCIf          Placeholder for Handle to the DDC interface structure
 *  @param  param           Not used in this implementation. For future use.
 *  @return Success or failure (PAL success/error code)
 */
#if 0
int DDC_cpsarCreateInstance (unsigned int instId,
                    DDA_Handle hDDA,
                    //CpsarDDACbIf *hDDACbIf,
                    DDC_Handle **hDDC,
                    void * param );
#endif
#ifdef CPSAR_DDC_GETSTATS
//extern void cpsarDDCStats(CpsarDDCObj *hDDC, unsigned char ch, unsigned char txQueue, unsigned char rxQueue);
#endif

/*@}*/

#endif /* __DDC_CPSAR_H__ */
