/*
	Copyright (c) Microsoft Corporation.  All rights reserved.
*/

#include "sdv_prefixer.h"




#define SP_RETURN_NOT_FOUND     0
#define SP_RETURN_FOUND         1
#define SP_RETURN_ERROR         2
#define SP_RETURN_BAD_CONFIG    3 
   

#define NULL 0

#define TIMER_EXPIRED_INDEX_BITS        6



#define ALL_SP_CALLBACKS  DriverEntry, \
                          fun_SP_HW_INITIALIZE, \
                          fun_SP_HW_BUILDIO, \
                          fun_SP_HW_STARTIO, \
                          fun_SP_HW_INTERRUPT, \
                          fun_SP_HW_TIMER_1, \
                          fun_SP_HW_TIMER_2, \
                          fun_SP_HW_TIMER_3, \
                          fun_SP_HW_TIMER_4, \
                          fun_SP_HW_TIMER_5, \
                          fun_SP_HW_TIMER_6, \
                          fun_SP_HW_DMA_STARTED, \
                          fun_SP_HW_FIND_ADAPTER, \
                          fun_SP_HW_RESET_BUS, \
                          fun_SP_HW_ADAPTER_STATE, \
                          fun_SP_HW_ADAPTER_CONTROL, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_1, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_2, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_3, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_4, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_5, \
                          fun_SP_HW_PASSIVE_INITIALIZE_ROUTINE_6, \
                          fun_SP_HW_DPC_ROUTINE_1, \
                          fun_SP_HW_DPC_ROUTINE_2, \
                          fun_SP_HW_DPC_ROUTINE_3, \
                          fun_SP_HW_DPC_ROUTINE_4, \
                          fun_SP_HW_DPC_ROUTINE_5, \
                          fun_SP_HW_DPC_ROUTINE_6, \
                          fun_SP_HW_FREE_ADAPTER_RESOURCES, \
                          fun_SP_HW_PROCESS_SERVICE_REQUEST, \
                          fun_SP_HW_COMPLETE_SERVICE_IRP, \
                          fun_SP_HW_INITIALIZE_TRACING, \
                          fun_SP_HW_CLEANUP_TRACING, \
                          fun_SP_VIRTUAL_HW_FIND_ADAPTER, \
                          fun_IO_COMPLETION_ROUTINE_1, \
                          fun_IO_COMPLETION_ROUTINE_2, \
                          fun_IO_COMPLETION_ROUTINE_3, \
                          fun_IO_COMPLETION_ROUTINE_4, \
                          fun_IO_COMPLETION_ROUTINE_5, \
                          fun_IO_COMPLETION_ROUTINE_6, \
                          fun_IO_COMPLETION_ROUTINE_7, \
                          fun_IO_COMPLETION_ROUTINE_8, \
                          fun_IO_COMPLETION_ROUTINE_9, \
                          fun_IO_COMPLETION_ROUTINE_10, \
                          fun_IO_COMPLETION_ROUTINE_11, \
                          fun_IO_COMPLETION_ROUTINE_12, \
                          fun_DRIVER_CANCEL_1, \
                          fun_DRIVER_CANCEL_2, \
                          fun_DRIVER_CANCEL_3, \
                          fun_DRIVER_CANCEL_4, \
                          fun_DRIVER_CANCEL_5, \
                          fun_DRIVER_CANCEL_6, \
                          fun_DRIVER_CANCEL_7, \
                          fun_HW_MESSAGE_SIGNALED_INTERRUPT_ROUTINE, \
                          fun_HW_STATE_CHANGE_1, \
                          fun_HW_STATE_CHANGE_2, \
                          fun_HW_STATE_CHANGE_3, \
                          fun_HW_STATE_CHANGE_4, \
                          fun_HW_STATE_CHANGE_5, \
                          fun_HW_STATE_CHANGE_6, \
                          fun_SP_HW_TIMER_EX_1, \
                          fun_SP_HW_TIMER_EX_2, \
                          fun_SP_HW_TIMER_EX_3, \
                          fun_SP_HW_TIMER_EX_4, \
                          fun_SP_HW_TIMER_EX_5, \
                          fun_SP_HW_TIMER_EX_6, \
                          fun_HW_WORKITEM_1, \
                          fun_HW_WORKITEM_2, \
                          fun_HW_WORKITEM_3, \
                          fun_HW_WORKITEM_4, \
                          fun_HW_WORKITEM_5, \
                          fun_HW_WORKITEM_6, \
                          fun_SP_POST_SCATTER_GATHER_EXECUTE, \
                          fun_SP_HW_PROCESS_SERVICE_REQUEST, \
                          fun_SP_HW_COMPLETE_SERVICE_IRP 



                          




#define FILE_AUTOGENERATED_DEVICE_NAME  128
#define FILE_DEVICE_DISK_FILE_SYSTEM    8
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 20
#define FILE_DEVICE_SECURE_OPEN         256
#define FILE_DEVICE_TAPE_FILE_SYSTEM    32

#define IRP_MJ_CREATE 0
#define IRP_MJ_CREATE_NAMED_PIPE 1
#define IRP_MJ_CLOSE 2
#define IRP_MJ_READ 3
#define IRP_MJ_WRITE 4
#define IRP_MJ_DEVICE_CONTROL 14
#define IRP_MJ_INTERNAL_DEVICE_CONTROL 15
#define IRP_MJ_POWER 22


#define IRP_MN_REMOVE_DEVICE 2
#define IRP_MN_START_DEVICE 0
#define IRP_MN_SURPRISE_REMOVAL 23

#define IRP_MN_CANCEL_STOP_DEVICE 6
#define IRP_MN_CANCEL_REMOVE_DEVICE 3


#define IRP_MN_QUERY_STOP_DEVICE 5
#define IRP_MN_QUERY_REMOVE_DEVICE 1
#define IRP_MN_QUERY_DEVICE_RELATIONS 7
#define IRP_MN_QUERY_INTERFACE 8
#define IRP_MN_QUERY_CAPABILITIES 9
#define IRP_MN_QUERY_RESOURCES 10
#define IRP_MN_QUERY_RESOURCE_REQUIREMENTS 11
#define IRP_MN_QUERY_DEVICE_TEXT 12
#define IRP_MN_QUERY_ID 19
#define IRP_MN_QUERY_PNP_DEVICE_STATE 20
#define IRP_MN_QUERY_BUS_INFORMATION 21
#define IRP_MN_QUERY_LEGACY_BUS_INFORMATION 24

#define IO_TYPE_ADAPTER                 1
#define IO_TYPE_CONTROLLER              2
#define IO_TYPE_DEVICE                  3
#define IO_TYPE_DRIVER                  4
#define IO_TYPE_FILE                    5
#define IO_TYPE_IRP                     6
#define IO_TYPE_MASTER_ADAPTER          7
#define IO_TYPE_OPEN_PACKET             8
#define IO_TYPE_TIMER                   9
#define IO_TYPE_VPB                     10
#define IO_TYPE_ERROR_LOG               11
#define IO_TYPE_ERROR_MESSAGE           12
#define IO_TYPE_DEVICE_OBJECT_EXTENSION 13

#define NT_SUCCESS(Status) (Status >= 0)
#define NULL 0

#define PowerDeviceD3 4
#define PowerDeviceD2 3
#define PowerDeviceD1 2
#define PowerDeviceD0 1

#define STATUS_NOT_SUPPORTED 3221225659
#define STATUS_PENDING 259
#define STATUS_SUCCESS 0

#define IRP_MJ_PNP 27

#define WMIREG_ACTION_REGISTER      1
#define WMIREG_ACTION_DEREGISTER    2

#define KernelMode 0
#define UserMode 1
#define MaximumMode 2

#define BusRelations 0
#define EjectionRelations 1
#define PowerRelations 2
#define RemovalRelations 3
#define TargetDeviceRelation 4
#define SingleBusRelations 5


#define NonPagedPool 0
#define PagedPool 1
#define NonPagedPoolMustSucceed 2
#define DontUseThisType 3
#define NonPagedPoolCacheAligned 4
#define PagedPoolCacheAligned 5
#define NonPagedPoolCacheAlignedMustS 6


#define OBJ_KERNEL_HANDLE 512


/*
  IRQL

  The SDV Rules specifies IRQL requirements for various DDIs.

  The IRQLs are positive ordered numbers starting from 0.  The actual
  numbers are processer specific, on X86 ranging from 0 to 31 and on
  AMD64 and IA64 ranging from 0 to 15.  For all three platforms
  PASSIVE_LEVEL, APC_LEVEL and DISPATCH_LEVEL are respectively 0, 1
  and 2.

  SDV rules only distinguish between the concrete values
  PASSIVE_LEVEL, APC_LEVEL, DISPATCH_LEVEL and the predicate
  SDV_IRQL_ELEVATED_LEVEL for 'everything else' (3-31 on X86, 3-15 on
  AMD64 or IA64).  The SDV rules are unable to distinguish between
  DIRQL, CMC_LEVEL, HIGH_LEVEL, etc.  The OS header files provide the
  equivalent macro definitions except for the SDV_IRQL_ELEVATED_LEVEL
  predicate.

  The SDV OS Model declares the SDV_DIRQL macro for elevating to an
  arbitrary Device IRQL.  The definition of SDV_DIRQL and the
  definition of SDV_IRQL_ELEVATED_LEVEL implies that
  SDV_IRQL_ELEVATED_LEVEL(SDV_DIRQL)==TRUE.

  See the article "Scheduling, Thread Context, and IRQL" for more
  details.
*/

#define PASSIVE_LEVEL 0 
#define APC_LEVEL 1 
#define DISPATCH_LEVEL 2 

#define HIGH_LEVEL 31
#define POWER_LEVEL 30
#define IPI_LEVEL 29
#define PROFILE_LEVEL 27

#define SDV_IRQL_ELEVATED_LEVEL(irql) ((irql)!=PASSIVE_LEVEL && (irql)!=APC_LEVEL && (irql)!=DISPATCH_LEVEL)


#define STACKL Tail.Overlay.CurrentStackLocation


#define FILE_AUTOGENERATED_DEVICE_NAME  128
#define FILE_DEVICE_DISK_FILE_SYSTEM    8
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 20
#define FILE_DEVICE_SECURE_OPEN         256
#define FILE_DEVICE_TAPE_FILE_SYSTEM    32

#define IRP_MJ_CREATE 0
#define IRP_MJ_CREATE_NAMED_PIPE 1
#define IRP_MJ_CLOSE 2
#define IRP_MJ_READ 3
#define IRP_MJ_WRITE 4
#define IRP_MJ_DEVICE_CONTROL 14
#define IRP_MJ_INTERNAL_DEVICE_CONTROL 15
#define IRP_MJ_POWER 22


#define IRP_MN_REMOVE_DEVICE 2
#define IRP_MN_START_DEVICE 0
#define IRP_MN_SURPRISE_REMOVAL 23

#define IRP_MN_CANCEL_STOP_DEVICE 6
#define IRP_MN_CANCEL_REMOVE_DEVICE 3

#define IRP_MN_WAIT_WAKE         0
#define IRP_MN_QUERY_STOP_DEVICE 5
#define IRP_MN_QUERY_REMOVE_DEVICE 1
#define IRP_MN_QUERY_DEVICE_RELATIONS 7
#define IRP_MN_QUERY_INTERFACE 8
#define IRP_MN_QUERY_CAPABILITIES 9
#define IRP_MN_QUERY_RESOURCES 10
#define IRP_MN_QUERY_RESOURCE_REQUIREMENTS 11
#define IRP_MN_QUERY_DEVICE_TEXT 12
#define IRP_MN_QUERY_ID 19
#define IRP_MN_QUERY_PNP_DEVICE_STATE 20
#define IRP_MN_QUERY_BUS_INFORMATION 21
#define IRP_MN_QUERY_LEGACY_BUS_INFORMATION 24

#define IO_TYPE_ADAPTER                 1
#define IO_TYPE_CONTROLLER              2
#define IO_TYPE_DEVICE                  3
#define IO_TYPE_DRIVER                  4
#define IO_TYPE_FILE                    5
#define IO_TYPE_IRP                     6
#define IO_TYPE_MASTER_ADAPTER          7
#define IO_TYPE_OPEN_PACKET             8
#define IO_TYPE_TIMER                   9
#define IO_TYPE_VPB                     10
#define IO_TYPE_ERROR_LOG               11
#define IO_TYPE_ERROR_MESSAGE           12
#define IO_TYPE_DEVICE_OBJECT_EXTENSION 13

#define NT_SUCCESS(Status) (Status >= 0)
#define STATUS_INSUFFICIENT_RESOURCES 3221225626
#define NULL 0

#define PowerDeviceD3 4
#define PowerDeviceD2 3
#define PowerDeviceD1 2
#define PowerDeviceD0 1

#define STATUS_NOT_SUPPORTED 3221225659
#define STATUS_PENDING 259
#define STATUS_SUCCESS 0

#define IRP_MJ_PNP 27

#define WMIREG_ACTION_REGISTER      1
#define WMIREG_ACTION_DEREGISTER    2

#define KernelMode 0
#define UserMode 1
#define MaximumMode 2

#define BusRelations 0
#define EjectionRelations 1
#define PowerRelations 2
#define RemovalRelations 3
#define TargetDeviceRelation 4
#define SingleBusRelations 5


#define NonPagedPool 0
#define PagedPool 1
#define NonPagedPoolMustSucceed 2
#define DontUseThisType 3
#define NonPagedPoolCacheAligned 4
#define PagedPoolCacheAligned 5
#define NonPagedPoolCacheAlignedMustS 6


#define OBJ_KERNEL_HANDLE 512


/*
  IRQL

  The SDV Rules specifies IRQL requirements for various DDIs.

  The IRQLs are positive ordered numbers starting from 0.  The actual
  numbers are processer specific, on X86 ranging from 0 to 31 and on
  AMD64 and IA64 ranging from 0 to 15.  For all three platforms
  PASSIVE_LEVEL, APC_LEVEL and DISPATCH_LEVEL are respectively 0, 1
  and 2.

  SDV rules only distinguish between the concrete values
  PASSIVE_LEVEL, APC_LEVEL, DISPATCH_LEVEL and the predicate
  SDV_IRQL_ELEVATED_LEVEL for 'everything else' (3-31 on X86, 3-15 on
  AMD64 or IA64).  The SDV rules are unable to distinguish between
  DIRQL, CMC_LEVEL, HIGH_LEVEL, etc.  The OS header files provide the
  equivalent macro definitions except for the SDV_IRQL_ELEVATED_LEVEL
  predicate.

  The SDV OS Model declares the SDV_DIRQL macro for elevating to an
  arbitrary Device IRQL.  The definition of SDV_DIRQL and the
  definition of SDV_IRQL_ELEVATED_LEVEL implies that
  SDV_IRQL_ELEVATED_LEVEL(SDV_DIRQL)==TRUE.

  See the article "Scheduling, Thread Context, and IRQL" for more
  details.
*/

#define PASSIVE_LEVEL 0 
#define APC_LEVEL 1 
#define DISPATCH_LEVEL 2 

#define SDV_IRQL_ELEVATED_LEVEL(irql) ((irql)!=PASSIVE_LEVEL && (irql)!=APC_LEVEL && (irql)!=DISPATCH_LEVEL)

#define SDV_IRQL_PREVIOUS_EQUAL(irql) (irql == sdv_irql_previous)



#define STACKL Tail.Overlay.CurrentStackLocation


#define	FILE_DEVICE_8042_PORT	39
#define	FILE_DEVICE_ACPI	50
#define	FILE_DEVICE_BATTERY	41
#define	FILE_DEVICE_BEEP	1
#define	FILE_DEVICE_BUS_EXTENDER	42
#define	FILE_DEVICE_CD_ROM	2
#define	FILE_DEVICE_CD_ROM_FILE_SYSTEM	3
#define	FILE_DEVICE_CHANGER	48
#define	FILE_DEVICE_CONTROLLER	4
#define	FILE_DEVICE_DATALINK	5
#define	FILE_DEVICE_DFS	6
#define	FILE_DEVICE_DFS_FILE_SYSTEM	53
#define	FILE_DEVICE_DFS_VOLUME	54
#define	FILE_DEVICE_DISK	7
#define	FILE_DEVICE_DISK_FILE_SYSTEM	8
#define	FILE_DEVICE_DVD	51
#define	FILE_DEVICE_FILE_SYSTEM	9
#define	FILE_DEVICE_FIPS	58
#define	FILE_DEVICE_FULLSCREEN_VIDEO	52
#define	FILE_DEVICE_INPORT_PORT	10
#define	FILE_DEVICE_KEYBOARD	11
#define	FILE_DEVICE_KS	47
#define	FILE_DEVICE_KSEC	57
#define	FILE_DEVICE_MAILSLOT	12
#define	FILE_DEVICE_MASS_STORAGE	45
#define	FILE_DEVICE_MIDI_IN	13
#define	FILE_DEVICE_MIDI_OUT	14
#define	FILE_DEVICE_MODEM	43
#define	FILE_DEVICE_MOUSE	15
#define	FILE_DEVICE_MULTI_UNC_PROVIDER	16
#define	FILE_DEVICE_NAMED_PIPE	17
#define	FILE_DEVICE_NETWORK	18
#define	FILE_DEVICE_NETWORK_BROWSER	19
#define	FILE_DEVICE_NETWORK_FILE_SYSTEM	20
#define	FILE_DEVICE_NETWORK_REDIRECTOR	40
#define	FILE_DEVICE_NULL	21
#define	FILE_DEVICE_PARALLEL_PORT	22
#define	FILE_DEVICE_PHYSICAL_NETCARD	23
#define	FILE_DEVICE_PRINTER	24
#define	FILE_DEVICE_SCANNER	25
#define	FILE_DEVICE_SCREEN	28
#define	FILE_DEVICE_SERENUM	55
#define	FILE_DEVICE_SERIAL_MOUSE_PORT	26
#define	FILE_DEVICE_SERIAL_PORT	27
#define	FILE_DEVICE_SMARTCARD	49
#define	FILE_DEVICE_SMB	46
#define	FILE_DEVICE_SOUND	29
#define	FILE_DEVICE_STREAMS	30
#define	FILE_DEVICE_TAPE	31
#define	FILE_DEVICE_TAPE_FILE_SYSTEM	32
#define	FILE_DEVICE_TERMSRV	57
#define	FILE_DEVICE_TRANSPORT	33
#define	FILE_DEVICE_UNKNOWN	34
#define	FILE_DEVICE_VDM	44
#define	FILE_DEVICE_VIDEO	35
#define	FILE_DEVICE_VIRTUAL_DISK	36
#define	FILE_DEVICE_WAVE_IN	37
#define	FILE_DEVICE_WAVE_OUT	38

/*
t-anseyf
SCSI notification types from %SLAM%\WDK\inc\ddk\storport.h
*/

#define ScsiAdapterControlSuccess		0
#define ScsiAdapterControlUnsuccessful	1

/*
StorPortNotification notification types:
*/

#define RequestComplete				0
#define NextRequest					1
#define NextLuRequest				2
#define ResetDetected				3
#define _obsolete1    				4
#define _obsolete2					5
#define RequestTimerCall			6 
#define BusChangeDetected			7
#define WMIEvent					8
#define WMIReregister				9
#define LinkUp						10
#define LinkDown					11
#define QueryTickCount				12
#define BufferOverrunDetected		13
#define TraceNotification			14
#define GetExtendedFunctionTable	15

#define EnablePassiveInitialization 4096
#define InitializeDpc				4097
#define IssueDpc					4098
#define AcquireSpinLock				4099
#define ReleaseSpinLock				4100
#define StateChangeDetectedCall		4101
#define IoTargetRequestServiceTime  4102

#define SRB_FUNCTION_EXECUTE_SCSI           0
#define SRB_FUNCTION_CLAIM_DEVICE           1
#define SRB_FUNCTION_IO_CONTROL             2
#define SRB_FUNCTION_RECEIVE_EVENT          3
#define SRB_FUNCTION_RELEASE_QUEUE          4
#define SRB_FUNCTION_ATTACH_DEVICE          5
#define SRB_FUNCTION_RELEASE_DEVICE         6
#define SRB_FUNCTION_SHUTDOWN               7
#define SRB_FUNCTION_FLUSH                  8
#define SRB_FUNCTION_ABORT_COMMAND          16
#define SRB_FUNCTION_RELEASE_RECOVERY       17
#define SRB_FUNCTION_RESET_BUS              18
#define SRB_FUNCTION_RESET_DEVICE           19
#define SRB_FUNCTION_TERMINATE_IO           20
#define SRB_FUNCTION_FLUSH_QUEUE            21
#define SRB_FUNCTION_REMOVE_DEVICE          22
#define SRB_FUNCTION_WMI                    23
#define SRB_FUNCTION_LOCK_QUEUE             24
#define SRB_FUNCTION_UNLOCK_QUEUE           25
#define SRB_FUNCTION_RESET_LOGICAL_UNIT     32
#define SRB_FUNCTION_SET_LINK_TIMEOUT       33
#define SRB_FUNCTION_LINK_TIMEOUT_OCCURRED  34
#define SRB_FUNCTION_LINK_TIMEOUT_COMPLETE  35
#define SRB_FUNCTION_POWER                  36
#define SRB_FUNCTION_PNP                    37
#define SRB_FUNCTION_DUMP_POINTERS          38

#define SRB_STATUS_PENDING                  0
#define SRB_STATUS_SUCCESS                  1
#define SRB_STATUS_ABORTED                  2
#define SRB_STATUS_ABORT_FAILED             3
#define SRB_STATUS_ERROR                    4
#define SRB_STATUS_BUSY                     5
#define SRB_STATUS_INVALID_REQUEST          6
#define SRB_STATUS_INVALID_PATH_ID          7
#define SRB_STATUS_NO_DEVICE                8
#define SRB_STATUS_TIMEOUT                  9
#define SRB_STATUS_SELECTION_TIMEOUT        10
#define SRB_STATUS_COMMAND_TIMEOUT          11
#define SRB_STATUS_MESSAGE_REJECTED         13
#define SRB_STATUS_BUS_RESET                14
#define SRB_STATUS_PARITY_ERROR             15
#define SRB_STATUS_REQUEST_SENSE_FAILED     16
#define SRB_STATUS_NO_HBA                   17
#define SRB_STATUS_DATA_OVERRUN             18
#define SRB_STATUS_UNEXPECTED_BUS_FREE      19
#define SRB_STATUS_PHASE_SEQUENCE_FAILURE   20
#define SRB_STATUS_BAD_SRB_BLOCK_LENGTH     21
#define SRB_STATUS_REQUEST_FLUSHED          22
#define SRB_STATUS_INVALID_LUN              32
#define SRB_STATUS_INVALID_TARGET_ID        33
#define SRB_STATUS_BAD_FUNCTION             34
#define SRB_STATUS_ERROR_RECOVERY           35
#define SRB_STATUS_NOT_POWERED              36
#define SRB_STATUS_LINK_DOWN                37

#define STOR_STATUS_SUCCESS                     0
#define STOR_STATUS_UNSUCCESSFUL                3238002689
#define STOR_STATUS_NOT_IMPLEMENTED             3238002690
#define STOR_STATUS_INSUFFICIENT_RESOURCES      3238002691
#define STOR_STATUS_BUFFER_TOO_SMALL            3238002692
#define STOR_STATUS_ACCESS_DENIED               3238002693
#define STOR_STATUS_INVALID_PARAMETER           3238002694
#define STOR_STATUS_INVALID_DEVICE_REQUEST      3238002695
#define STOR_STATUS_INVALID_IRQL                3238002696
#define STOR_STATUS_INVALID_DEVICE_STATE        3238002697
#define STOR_STATUS_INVALID_BUFFER_SIZE         3238002698
#define STOR_STATUS_UNSUPPORTED_VERSION         3238002699

#define InterruptSupportNone 0
#define InterruptSynchronizeAll 1
#define InterruptSynchronizePerMessage 2

#define StorSynchronizeHalfDuplex 0
#define StorSynchronizeFullDuplex 1

#define DpcLock			1
#define StartIoLock		2
#define InterruptLock	3


#define ScsiQuerySupportedControlTypes  0
#define ScsiStopAdapter 1
#define ScsiRestartAdapter 2
#define ScsiSetBootConfig 3
#define ScsiSetRunningConfig 4
#define ScsiAdapterControlMax 5
#define MakeAdapterControlTypeSizeOfUlong 6
