/*****************************************************************************/
/*                                                                           */
/* COMPONENT   : Rainbow				                                     */
/* MODULE      : Virtual Flash Layer                                         */
/* NAME    	   : VFL types definition header                                 */
/* FILE        : VFLTypes.h	                                                 */
/* PURPOSE 	   : This header defines Data types which are shared             */
/*               by all VFL submodules                                       */
/*                                                                           */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/*          COPYRIGHT 2003-2005 SAMSUNG ELECTRONICS CO., LTD.                */
/*                          ALL RIGHTS RESERVED                              */
/*                                                                           */
/*   Permission is hereby granted to licensees of Samsung Electronics        */
/*   Co., Ltd. products to use or abstract this computer program for the     */
/*   sole purpose of implementing a product based on Samsung                 */
/*   Electronics Co., Ltd. products. No other rights to reproduce, use,      */
/*   or disseminate this computer program, whether in part or in whole,      */
/*   are granted.                                                            */
/*                                                                           */
/*   Samsung Electronics Co., Ltd. makes no representation or warranties     */
/*   with respect to the performance of this computer program, and           */
/*   specifically disclaims any responsibility for any damages,              */
/*   special or consequential, connected with the use of this program.       */
/*                                                                           */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* REVISION HISTORY                                                          */
/*                                                                           */
/*   04-AUG-2003 [HM Jung]		: first writing								 */
/*   14-JUL-2005 [Jaesung Jung] : reorganize code                            */
/*	 06-SEP-2005 [Jaesung Jung] : fix from code inspection					 */
/*   24-JAN-2006 [Yangsup Lee ] : support un-pair bad block management       */
/*   31-MAR-2006 [Yangsup Lee ] : support ftl meta block wear leveling       */
/*                                                                           */
/*****************************************************************************/
#ifndef _VFL_TYPES_H_
#define _VFL_TYPES_H_

#include "WMRConfig.h"

typedef struct
{
    UInt16 wPagesPerBlock;             /* the count of pages per block		 */
    UInt8 bRefreshThreshold;          // number of bit flips beyond which we want to rewrite the page
    UInt16 wNumOfCEs;
    UInt16 wBlocksPerCS;               /* the count of blocks per CS		 */
    UInt16 wBlocksPerBank;
    UInt16 wBlocksPerDie;
    UInt16 wDieStride;
    UInt16 wBlockStride;
    UInt16 wUserBlocksPerCS;

    // Info generated by CalcGlobal
    UInt16 wPagesPerSuBlk;             /* the count of pages per virtual block */
    UInt16 wBytesPerPage;              /* bytes per page (main)			 */

    UInt32 dwValidMetaPerLogicalPage;
    UInt32 dwTotalMetaPerLogicalPage;
    UInt32 dwLogicalPageSize;

    UInt16 wNumOfBank;                 /* the number of banks				 */
    UInt16 wNumOfBanksPerCS;

    UInt16 dwVFLAreaStart;             /* the block number where VFL area starts */

} VSVFLWMRDeviceInfo;

/*****************************************************************************/
/* Global variables	redefinitions (WMRLayout)					             */
/*****************************************************************************/
#define     VFL_AREA_START          (stVSVFLWMRDeviceInfo.dwVFLAreaStart)
#define     VFL_FIRST_BLK_TO_SEARCH_CXT     VFL_AREA_START
#define     VFL_LAST_BLK_TO_SEARCH_CXT      (VFL_AREA_START + 199)

#define     VFL_INFO_SECTION_START  (VFL_AREA_START)
#define     VFL_INFO_SECTION_SIZE   (4)

#define     FIL_ECC_REFRESH_TRESHOLD (stVSVFLWMRDeviceInfo.bRefreshThreshold)

#define     VFL_RESERVED_SECTION_SIZE_CS        (BLOCKS_PER_CS - (_GetNumOfSuBlks() * BANKS_PER_CS))
#define     VFL_RESERVED_SECTION_SIZE_BANK      (BLOCKS_PER_BANK - _GetNumOfSuBlks())

#define     BBT_SIZE_PER_CS         ((BLOCKS_PER_CS / 8) + (BLOCKS_PER_CS % 8 ? 1 : 0))
#if (defined(VFL_SMALL_ADDRESS_BUFFER) && VFL_SMALL_ADDRESS_BUFFER)
#define     VSVFL_MAX_BANKS         (4)
#else
#define     VSVFL_MAX_PHYS_CS       (16)
#define     VSVFL_MAX_BANKS         (VSVFL_MAX_PHYS_CS * AND_MAX_BANKS_PER_CS)        /* the total maximum number of bank		 */
#endif
#define     VFL_SCRUB_LIST_SIZE     (20)
#define     FTL_FORMAT_STRUCT_SIZE  (16)        // packed
/*****************************************************************************/
/* VFL context status(confirm) mark definition                               */
/*****************************************************************************/
#define     PAGE_VALID                  (0x00)

typedef struct
{
    UInt8 abNewFTLType[4];
    UInt8 abNewNumOfFTLSuBlk[2];
    UInt8 abReserved[9];
    UInt8 bFormatFTL; // 0 - false, 1 - format...
} FTLFormatType;

/*****************************************************************************/
/* Data structure for storing the VFL context definition					 */
/*****************************************************************************/
/* NOTICE !!!														*/
/* this structure is used directly to load VFL context by WMR_MEMCPY*/
/* so the byte pad of this structure must be 0		   				*/
typedef struct
{
    UInt32 dwGlobalCxtAge;             /* age for FTL meta information search  */
    UInt32 dwCxtAge;                   /* context age 0xFFFFFFFF --> 0x0 */
    UInt32 dwFTLType;                  /* FTL identifier */

    UInt16 wCxtLocation;               /* current context block location  (Physical) */
    UInt16 wNextCxtPOffset;            /* current context page offset information */

    /* this data is used for summary */
    UInt16 wNumOfInitBadBlk;           /* the number of initial bad blocks - used for VFL format */
    UInt16 wNumOfWriteFail;         /* the number of failed write operations*/
    UInt16 wNumOfEraseFail;            /* the number of failed erase operations */

    /* bad blocks management table & good block pointer */
    UInt16 awReplacementIdx[AND_MAX_BANKS_PER_CS];           /* index to the last bad block updated in the awBadMapTable per bank
                                                                    (some might be bad blocks) the array is divided to banks */
    UInt16 awBadMapTable[WMR_MAX_RESERVED_SIZE]; /* remapping table of bad blocks - the UInt 16 value set here is the virtual block (vfl address space) that is being replaced */

    /* bad blocks management table within VFL info area */
    UInt16 awInfoBlk[VFL_INFO_SECTION_SIZE];       /* physical block addresses where FTL Cxt information is stored */

    UInt16 wNumOfFTLSuBlk;             /* the number of super blocks allocated for the FTL - default id to give all to FTL */
    UInt16 wNumOfVFLSuBlk;             /* the total number of available super blocks */
    UInt16 awFTLCxtVbn[FTL_CXT_SECTION_SIZE];  /* page address (FTL virtual addressing space) of FTL Cxt */
    UInt16 wScrubIdx;                              /* Index for the scrub list */
    UInt16 awScrubList[VFL_SCRUB_LIST_SIZE];       /* scrub list */
    UInt8 abFTLFormat[FTL_FORMAT_STRUCT_SIZE];
    UInt8 abVSFormtType[4];
} VFLCxt;

/* VFLMeta size is 2048 bytes(4 sector) */
#define VFL_META_VERSION 0x00000002
typedef struct _VFLMeta
{
    VFLCxt stVFLCxt;
    UInt8 abReserved[2048 - ((3 * sizeof(UInt32)) + sizeof(VFLCxt))];
    UInt32 dwVersion;
    UInt32 dwCheckSum;
    UInt32 dwXorSum;
} VFLMeta;

/*****************************************************************************/
/* Data structure for VFL context spare area								 */
/*****************************************************************************/
/* spare layout for SLC & MLC							*/
typedef struct
{
    UInt32 dwCxtAge;               /* context age 0xFFFFFFFF --> 0x0   */
    UInt32 dwReserved;             /* reserved			 		     */
    UInt8 cStatusMark;            /* status (confirm) mark - currently not used for anything */
    UInt8 bSpareType;             /* spare type */
    /* reserved for main ECC */
} VFLSpare;

#define VFL_SPARE_TYPE_CXT  0x80

/*****************************************************************************/
/* Asynchronous operation management structure & enum definition			 */
/*****************************************************************************/

/* Device Information Structures and defines */

#define BBT_SIZE_PER_CS                         ((BLOCKS_PER_CS / 8) + (BLOCKS_PER_CS % 8 ? 1 : 0))

#define WMR_NUM_OF_ERASE_TRIALS_FOR_INDEX_BLOCKS (4)

#define VFL_BLOCK_INDEX_MASK                        (0xFFFF)
#define VFL_INVALID_INFO_INDEX                      (0xFFFF)
#define VFL_BAD_MAP_TABLE_AVAILABLE_MARK            (0xFFF0)
#define VFL_BAD_MAP_TABLE_STRESS_FAIL_MARK          (0xFFFD)
#define VFL_BAD_MAP_TABLE_BORROW_MARK               (0xFFFE)
#define VFL_BAD_MAP_TABLE_BAD_MARK                  (0xFFFF)

#ifdef AND_COLLECT_STATISTICS
typedef struct
{
    UInt64 ddwPagesWrittenCnt;
    UInt64 ddwPagesReadCnt;
    UInt64 ddwBlocksErasedCnt;
    UInt64 ddwSingleWriteCallCnt;
    UInt64 ddwSingleReadCallCnt;
    UInt64 ddwSequetialReadCallCnt;
    UInt64 ddwScatteredReadCallCnt;
    UInt64 ddwMultipleWriteCallCnt;
    UInt64 ddwEraseCallCnt;
    UInt64 ddwEraseTimeoutFailCnt;
    UInt64 ddwBurnInCode;
} VFLStatistics;

#define VFL_STATISTICS_DESCREPTION { \
        "ddwPagesWrittenCnt", \
        "ddwPagesReadCnt", \
        "ddwBlocksErasedCnt", \
        "ddwSingleWriteCallCnt", \
        "ddwSingleReadCallCnt", \
        "ddwSequetialReadCallCnt", \
        "ddwScatteredReadCallCnt", \
        "ddwMultipleWriteCallCnt", \
        "ddwEraseCallCnt", \
        "ddwEraseTimeoutFailCnt", \
        "ddwBurnInCode", \
}
#endif /* AND_COLLECT_STATISTICS */

#endif /* _VFL_TYPES_H_ */
