/******************************************************************************/
/*                                                                            */
/*  Copyright (C), 1995-2007, SanDisk IL Ltd. All rights reserved.            */
/*                                                                            */
/*  Redistribution and use in source and binary forms, with or without        */
/*  modification, are permitted provided that the following conditions are    */
/*  met:                                                                      */
/*  1. Redistributions of source code must retain the above copyright notice, */
/*     this list of conditions and the following disclaimer.                  */
/*  2. Redistributions in binary form must reproduce the above copyright      */
/*     notice, this list of conditions and the following disclaimer in the    */
/*     documentation and/or other materials provided with the distribution.   */
/*  3. Neither the name of SanDisk IL nor the names of its contributors may be*/
/*     used to endorse or promote products derived from this software without */
/*     specific prior written permission.                                     */
/*                                                                            */
/*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
/*  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR             */
/*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      */
/*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     */
/*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  */
/*  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    */
/*  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      */
/*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        */
/*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
/*                                                                            */
/*  NOTWITHSTANDING ANYTHING STATED TO THE CONTRARY, SANDISK'S TOTAL LIABILITY*/
/*  ARISING OUT OF OR RELATED TO THE SOFTWARE OR ANY SUPPORT SERVICES         */
/*  PROVIDED WITH RESPECT THERETO SHALL NOT EXCEED $100.                      */
/*                                                                            */
/*  The laws of the State of California, United States of America, exclusive  */
/*  of conflict-of-laws provisions, shall govern this license in all respects.*/
/*  The the federal or state courts of competent jurisdiction in the State    */ 
/*  of California shall have exclusive jurisdiction with respect to all       */
/*  actions commenced in relation to the software. Licensee agrees not        */ 
/*  to export or re-export, directly or indirectly any technical data         */ 
/*  acquired from SanDisk or any products utilizing such data in violation    */
/*  of applicable export laws and regulations.                                */
/*                                                                            */
/******************************************************************************/

/*
 * $Log:   V:/PVCSDB/DiskOnChip/archives/Testing/TrueFFS 6.3/Drop 2.5/3/common/flioctl.h-arc  $
 * 
 *    Rev 1.17   Oct 22 2007 11:34:52   einat.avidan
 * update copyrights header
 * 
 *    Rev 1.16   Jul 02 2007 11:43:46   einat.avidan
 * bug fix: SCR2966 : removed flags which are not in use 
 * FL_IPL_DOWNLOAD
 * FL_IPL_ALERNATE_MAP
 * FL_USING_BUFFERS
 * FL_IPL_NO_ADDRESS_SHIFT_MODE 
 * 
 *    Rev 1.15   Feb 28 2007 09:53:48   einat.avidan
 * No change
 * 
 *    Rev 1.14.1.1   Oct 29 2006 14:23:46   Yaniv.Iarovici
 * Update 'FL_IOCTL_PASS_THROUGH' structures.
 * 
 *    Rev 1.14.1.0   Oct 22 2006 14:53:58   yaniv.iarovici
 * Changed protectionType from FLByte to FLWord in flProtectionOutput stucture.
 * 
 *    Rev 1.14   Oct 09 2006 14:05:38   yaniv.iarovici
 * Removed #ifndef FL_DO_NOT_SUPPORT_H3
 * 
 *    Rev 1.13   Oct 05 2006 11:01:06   yaniv.iarovici
 * Fixed support for FL_IOCTL_PASS_THROUGH.
 * 
 *    Rev 1.12   Sep 11 2006 13:45:22   yaniv.iarovici
 * Legal header added
 * 
 *    Rev 1.11   Aug 16 2006 08:47:20   Yaniv.Iarovici
 * Encapsulate BDK related defines under '#ifdef BDK_ACCESS'.
 * 
 *    Rev 1.9   Aug 09 2006 16:52:50   Polina.Marimont
 * initial for DOC Driver 1.0
 */

#ifndef FLIOCTL_H
#define FLIOCTL_H

#include "flsystem.h"
#include "flstruct.h"
#include "_dochapi.h"


/* In every call to flIOctl function, the irFlags field in the structure
IOreq should hold one of the following: */
typedef enum{FL_IOCTL_GET_INFO = FL_IOCTL_START,
  FL_IOCTL_DEFRAGMENT,
  FL_IOCTL_NO_LONGER_SUPPORTED_0,	/* FL_IOCTL_WRITE_PROTECT             */
  FL_IOCTL_MOUNT_VOLUME,
  FL_IOCTL_NO_LONGER_SUPPORTED_1,	/* FL_IOCTL_FORMAT_VOLUME             */
#ifdef BDK_ACCESS
  FL_IOCTL_BDK_OPERATION,
#else /*BDK_ACCESS*/
  FL_IOCTL_RESERVED1,
#endif /*BDK_ACCESS*/
  FL_IOCTL_DELETE_SECTORS,
  FL_IOCTL_READ_SECTORS,
  FL_IOCTL_WRITE_SECTORS,
  FL_IOCTL_NO_LONGER_SUPPORTED_2,	/* FL_IOCTL_FORMAT_PHYSICAL_DRIVE     */
  FL_IOCTL_NO_LONGER_SUPPORTED_3,	/* FL_IOCTL_FORMAT_LOGICAL_DRIVE      */
  FL_IOCTL_BDTL_HW_PROTECTION,
#ifdef BDK_ACCESS
  FL_IOCTL_BINARY_HW_PROTECTION,
#else /*BDK_ACCESS*/
  FL_IOCTL_RESERVED2,
#endif /*BDK_ACCESS*/
  FL_IOCTL_OTP,
  FL_IOCTL_CUSTOMER_ID,
  FL_IOCTL_UNIQUE_ID,
  FL_IOCTL_NUMBER_OF_PARTITIONS,
  FL_IOCTL_NO_LONGER_SUPPORTED_4,	/* FL_IOCTL_INQUIRE_CAPABILITIES      */ 
  FL_IOCTL_NO_LONGER_SUPPORTED_5,	/* FL_IOCTL_SET_ENVIRONMENT_VARIABLES */
  FL_IOCTL_NO_LONGER_SUPPORTED_6,	/* FL_IOCTL_PLACE_EXB_BY_BUFFER       */
  FL_IOCTL_NO_LONGER_SUPPORTED_7,	/* FL_IOCTL_WRITE_IPL                 */
  FL_IOCTL_DEEP_POWER_DOWN_MODE,
  FL_IOCTL_EXTENDED_ENVIRONMENT_VARIABLES, 
  FL_IOCTL_VERIFY_VOLUME,	 
  FL_IOCTL_SET_ACCESS_ROUTINE,
  FL_IOCTL_GET_ACCESS_ROUTINE,
  FL_IOCTL_NO_LONGER_SUPPORTED_8,	/* FL_IOCTL_EXTENDED_WRITE_IPL        */  
  FL_IOCTL_NO_LONGER_SUPPORTED_9,	/* FL_IOCTL_OPEN_TRANSACTION          */
  FL_IOCTL_NO_LONGER_SUPPORTED_10,	/* FL_IOCTL_COMMIT_TRANSACTION        */
  FL_IOCTL_NO_LONGER_SUPPORTED_11,	/* FL_IOCTL_IS_VOLUME_RUGGEDIZED      */
  FL_IOCTL_IS_QUICK_MOUNT_VALID,      
  FL_IOCTL_WRITE_QUICK_MOUNT_INFO,
  FL_IOCTL_CLEAR_QUICK_MOUNT_INFO,
  FL_IOCTL_NO_LONGER_SUPPORTED_12,	/* FL_IOCTL_COMPLETE_OPERATION        */
  FL_IOCTL_FLASH_FORMAT, /* Replacing FL_IOCTL_FORMAT_PHYSICAL_DRIVE       */
  FL_IOCTL_ERASE_BD,                  /* Added in TrueFFS 6.3.0            */
  FL_IOCTL_FLASH_UNFORMAT,            /* Added in TrueFFS 6.3.0            */
  FL_IOCTL_WRITE_IPL,                 /* Changed in TrueFFS 6.3.0          */
  FL_IOCTL_MCOTP,
  FL_IOCTL_PASS_THROUGH,
  FL_IOCTL_IPL_HW_PROTECTION
} flIOctlFunctionNo;


/* In every call to flIOctl function, the irData field in the structure
IOreq should point to the structure defined below. The fields
inputRecord and outputRecord should point to structures which are
specific to each IOctl function as defined in this file. */
typedef struct {
	void FAR1 *inputRecord;
	void FAR1 *outputRecord;
} flIOctlRecord;


/* General output record that returns only status. */
typedef struct {
	FLStatus status;
} flOutputStatusRecord;

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

TFFS_DLL_API FLStatus NAMING_CONVENTION bdIOctl(IOreq FAR2 *);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#ifndef flIOctl
#define flIOctl bdIOctl
#endif


/* Input and output records for the different IOCTL functions: */
/* =========================================================== */


/*************************************************************************/
/* Get disk information (FL_IOCTL_GET_INFO) */
/* Input record: NULL */
/* Output record: */
typedef struct 
{
    VolumeInfoRecord info;  
	FLStatus status;
} flDiskInfoOutput;
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Defragment volume (FL_IOCTL_DEFRAGMENT) */
/* Input record: */
typedef struct {
	FLSDword requiredNoOfSectors;   /* Minimum number of sectors to make */
	/* available.                        */ 
} flDefragInput;
/* Outout record: */
typedef struct {
	FLSDword actualNoOfSectors;     /* Actual number of sectors available */
	FLStatus status;
} flDefragOutput;

/*************************************************************************/
/* Mount volume (FL_IOCTL_MOUNT_VOLUME) */
/* Input record: */
typedef struct {
	FLByte  type;        /*  type of operation: FL_MOUNT\FL_DISMOUNT */
} flMountInput;
#define FL_MOUNT          0
#define FL_DISMOUNT        1
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Delete logical sectors (FL_IOCTL_DELETE_SECTORS) */
/* Input record: */
typedef struct {
	FLSDword firstSector;                /* First logical sector to delete */
	FLSDword numberOfSectors;                /* Number of sectors to delete */
} flDeleteSectorsInput;
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Format flash device (FL_IOCTL_FLASH_FORMAT) */
/* Input record: */
typedef struct {
	FLDword dwFormatFlags;  /* type of format as defined in blockdev.h             */
	FLSDword sdwNoOfKeeps;
	FormatParams3 * fpPtr;     /* Format parameters structure (defined in flformat.h) */
} flFlashFormatInput;
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Erase existing disk partition (FL_IOCTL_ERASE_BD) */
/* Input record: */
typedef struct {
	FLDword dwFlags;  /* Reserved - Must be set to 0 */
} flEraseBDInput;
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Format flash device (FL_IOCTL_FLASH_UNFORMAT) */
/* Input record: */
typedef struct {
	FLDword dwFlags;  /* Reserved - Must be set to 0 */
} flFlashUnformatInput;
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* BDK and BDTL protection operations: (FL_IOCTL_BINARY_HW_PROTECTION) */
/*                                     (FL_IOCTL_BDTL_HW_PROTECTION)   */
/*   insert key \ remove key \ identify \ change key \                 */
/*   change protection type \ change lock status                       */
/* Input record: */
typedef struct {
    FLByte protectionType;            /* Protection attributes. The following        */
                                      /*attributes can be ored: PROTECTABLE,         */
                                      /*READ_PROTECTED, WRITE_PROTECTED, LOCK_ENABLED*/
                                      /*CHANGEABLE_PROTECTION, OTW_PROTECTED,        */
    FLByte key[8];                    /* The new key to the change Key call          */
    FLByte type;                      /* Operation type. available values are:       */
                                      /*PROTECTION_INSERT_KEY, PROTECTION_REMOVE_KEY,*/
                                      /*PROTECTION_GET_TYPE, PROTECTION_DISABLE_LOCK,*/
                                      /*PROTECTION_ENABLE_LOCK, PROTECTION_CHANGE_KEY,*/
                                      /*PROTECTION_CHANGE_TYPE                       */
} flProtectionInput;
#define PROTECTION_INSERT_KEY   0
#define PROTECTION_REMOVE_KEY   1
#define PROTECTION_GET_TYPE     2
#define PROTECTION_DISABLE_LOCK 3
#define PROTECTION_ENABLE_LOCK  4
#define PROTECTION_CHANGE_KEY   5
#define PROTECTION_CHANGE_TYPE  6
#define PROTECTION_APPLY_STICKY 7
/* Output record: */
typedef struct {
   FLWord protectionType;             /* Protection attributes. The following        */
                                      /*attributes can be ored: PROTECTABLE,         */ 
                                      /*READ_PROTECTED, WRITE_PROTECTED, LOCK_ENABLED*/
                                      /*LOCK_ASSERTED, KEY_INSERTED,                 */
                                      /*CHANGEABLE_PROTECTION, OTW_PROTECTED,        */
                                      /*STICKY_LOCK_ASSERTED                         */

   FLStatus status;
} flProtectionOutput;

/*************************************************************************/
/* One Time Programing operations: (FL_IOCTL_OTP */
/*   OTP size \ OTP read \ OTP write and lock    */
/* Input record: */
typedef struct {
	FLDword       length;           /* Length to read\write\size                  */
	FLDword       usedSize;         /* The written size of the area \ Area offset */
	FLByte        lockedFlag;       /* The area condition LOCKED_OTP (flflash.h)  */
	FLByte        FAR1* buffer;     /* pointer to user buffer                     */
	FLWord        type;             /* defined bellow                             */
} flOtpInput;
#define OTP_SIZE        1
#define OTP_READ        2
#define OTP_WRITE_LOCK  3
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* One Time Programing operations: (FL_IOCTL_MCOTP */
/*   OTP size \ OTP read \ OTP write and lock    */
/* Input record: */
typedef struct {
	FLWord        wPageNo ;         /* Page to write                              */
	FLDword       length;           /* Length to read\write\size                  */
	FLDword       usedSize;         /* The written size of the area \ Area offset */
	FLByte        lockedFlag;       /* The area condition LOCKED_OTP (flflash.h)  */
	FLByte        FAR1* buffer;     /* pointer to user buffer                     */
	FLWord        type;             /* defined bellow                             */
} flMCOtpInput;

#define MCOTP_SIZE        1
#define MCOTP_READ        2
#define MCOTP_WRITE_LOCK        3
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* Read customer ID (FL_IOCTL_CUSTOMER_ID) */
/* Input record: NULL */
/* Output record: */
typedef struct {
	FLByte  id[4];
	FLStatus status;
} flCustomerIdOutput;

/*************************************************************************/
/* Read unique ID (FL_IOCTL_UNIQUE_ID) */
/* Input record: NULL */
/* Output record: */
typedef struct {
	FLByte  id[16];
	FLStatus status;
} flUniqueIdOutput;

/*************************************************************************/
/* Read unique ID (FL_IOCTL_NUMBER_OF_PARTITIONS) */
/* Input record: NULL */
/* Output record: */
typedef struct {
	FLByte  noOfPartitions;
	FLStatus status;
} flCountPartitionsOutput;

/*************************************************************************/
/* Place the device into and out of the power down mode (FL_IOCTL_DEEP_POWER_DOWN_MODE) */

/* Input record:  */
typedef struct {
	FLByte  state; /* DEEP_POWER_DOWN - low power consumption           */
	               /* EXIT_DEEP_POWER_DOWN - regular power consumption  */
} flPowerDownInput;
/* Output record: flOutputStatusRecord */

#ifdef BDK_ACCESS
/*************************************************************************/
/* BDK operations read\write\erase\create (FL_IOCTL_BDK_OPERATION) */
/* Input record: */
typedef struct {
    FLByte type;                      /* type of operation. Available are:           */
                                      /*BDK_INIT_READ, BDK_READ, BDK_INIT_WRITE,     */
                                      /*BDK_WRITE, BDK_ERASE, BDK_CREATE,            */
                                      /*   BDK_GET_INFO                              */
    BDKStruct bdkStruct;              /* parameters for BDK operations               */
} flBDKOperationInput;
#define BDK_INIT_READ   0
#define BDK_READ        1
#define BDK_INIT_WRITE  2
#define BDK_WRITE       3
#define BDK_ERASE       4
#define BDK_CREATE      5
#define BDK_GET_INFO    6
/* Output record: flOutputStatusRecord */
#endif /*BDK_ACCESS*/

/******************************************************************************/
/* Set environment variables values (FL_IOCTL_EXTENDED_ENVIRONMENT_VARIABLES) */
/* Input record:  */
typedef struct {
	FLEnvVars      varName;  /* Enum describing the variable */
	FLDword          varValue; /* New variable value           */
	FLDword          flags;    /* FL_APPLY_TO_ALL    - All socket and partitions */
	                           /* FL_APPLY_TO_SOCKET - All socket and partitions */
	                           /* FL_APPLY_TO_VOLUME - All socket and partitions */
} flExtendedEnvVarsInput;
#define FL_APPLY_TO_ALL    1
#define FL_APPLY_TO_SOCKET 2
#define FL_APPLY_TO_VOLUME 3
/* Output record: */
typedef struct {
	FLDword    prevValue;   /* The previous value of the variable */
	FLStatus status;
} flExtendedEnvVarsOutput;

/******************************************************************************/
/* Write IPL area for docPlus family (FL_IOCTL_WRITE_IPL) */
/* Input record:  */
typedef struct {
	FLByte   FAR1* bBufPtr; /* IPL data buffer           */
    FLDword dwFlags;           /* IPL flags. Available are: FL_IPL_MODE_NORMAL, */
                               /* FL_DOC_IPL_PAGED_RAM_MODE,FL_IPL_VIRTUAL_RAM_MODE*/
							   /* FL_IPL_128K_WINDOW_MODE, FL_IPL_SWAP_BYTES_MODE*/
	FLSDword sdwLength;     /* IPL length                */
	FLSDword sdwOffset;     /* IPL offset                */
} flIPLInput;
/* Output record: flOutputStatusRecord */

/******************************************************************************/
/* Check quick mount validity (FL_IOCTL_IS_QUICK_MOUNT_VALID) */
/* Input record: NULL */
/* Output record: */
typedef struct {
	FLByte                mode; /* Either FL_ON for valid and FL_OFF for not */
	FLStatus              status;
} flGetQuickMountOutput;

/******************************************************************************/
/* Write quick mount data (FL_IOCTL_WRITE_QUICK_MOUNT_INFO) */
/* Input record: NULL */
/* Output record: flOutputStatusRecord */

/******************************************************************************/
/* Clear quick mount data (FL_IOCTL_CLEAR_QUICK_MOUNT_INFO) */
/* Input record: NULL */
/* Output record: flOutputStatusRecord */

/*************************************************************************/
/* read & write logical sectors (FL_IOCTL_READ_SECTORS & FL_IOCTL_WRITE_SECTORS) */
/* Input record: */
typedef struct {
	FLSDword firstSector;                  /* first logical sector */
	FLSDword numberOfSectors;         /* Number of sectors to read\write */
	FLByte  FAR1 *buf;               /* Data to read\write */
} flReadWriteInput;
/* Output record: */
typedef struct {
	FLSDword numberOfSectors;         /* Actual Number of sectors read\written */
	FLStatus status;
} flReadWriteOutput;

/******************************************************************************/
/* Check partition for power failures symptoms (FL_IOCTL_VERIFY_VOLUME) */
/* Input record: NULL */
typedef struct {
	FLDword flags;   /* Must be set to 0 */    
} flVerifyVolumeInput;
/* Output record: */
typedef struct {
	void FAR1* callBack;   /* Must be set to null */  
	FLStatus status;
} flVerifyVolumeOutput;

/******************************************************************************/
/* Plant user defined memory access routines (FL_IOCTL_SET_ACCESS_ROUTINE) */
/* Input record:  */
typedef struct {
	FLAccessStruct FAR1 * structPtr; /* Pointer memory access routine struct */
} flSetMemoryAccessInput;
/* Output record: flOutputStatusRecord */

/******************************************************************************/
/* Plant user defined memory access routines (FL_IOCTL_GET_ACCESS_ROUTINE) */
/* Input record: NULL */
/* Output record: */
typedef struct {
	FLAccessStruct FAR1 * structPtr; /* Pointer memory access routine struct */
	FLStatus              status;
} flGetMemoryAccessOutput;

/******************************************************************************/
/* Perform ATA command using the pass-through mechanism (FL_IOCTL_PASS_THROUGH) */
/* Input record: */
typedef struct {
	DOCH_PassThru_Op	passThruOP;		/*0 - No Data(CTRL), 1 - Data IN, 2 - Data OUT*/
	DOCH_PassThru_Int   useInterrupt;	/*0 - Do NOT use interrupt, 1- USE interrupt*/
	FLSDword			multSectorFrame;/*DRQ size exponent (power of 2)*/
	DOCH_Registers*		in_regs;		/*ATA input registers*/
	FLSDword			secNum;			/*Number of sectors in user buffer*/
	void*				userBuff;		/* User buffer (for read\write) */
} flAtaPassthroughInput;
/* Output record: */
typedef struct {
	DOCH_Registers*		out_regs;		/*ATA input registers*/
	FLStatus			status;
} flAtaPassthroughOutput;


#endif /* FLIOCTL_H */
