2 * PROJECT: ReactOS Kernel
3 * LICENSE: GNU GPLv2 only as published by the Free Software Foundation
4 * PURPOSE: To Implement AHCI Miniport driver targeting storport NT 5.2
5 * PROGRAMMERS: Aman Priyadarshi (aman.eureka@gmail.com)
11 #define AHCI_POOL_TAG 'ahci'
12 #define MAXIMUM_AHCI_PORT_COUNT 12
15 #define AHCI_Global_HBA_CONTROL_HR (0x1<<0)
16 #define AHCI_Global_HBA_CONTROL_IE (0x1<<1)
17 #define AHCI_Global_HBA_CONTROL_MRSM (0x1<<2)
18 #define AHCI_Global_HBA_CONTROL_AE (0x1<<31)
20 //////////////////////////////////////////////////////////////
21 // ---- Support Structures --- //
22 //////////////////////////////////////////////////////////////
24 typedef struct _AHCI_FIS_DMA_SETUP
26 ULONG ULONG0_1
; // FIS_TYPE_DMA_SETUP
29 // Data transfer direction, 1 - device to host
31 // Auto-activate. Specifies if DMA Activate FIS is needed
32 UCHAR Reserved
[2]; // Reserved
33 ULONG DmaBufferLow
; // DMA Buffer Identifier. Used to Identify DMA buffer in host memory. SATA Spec says host specific and not in Spec. Trying AHCI spec might work.
35 ULONG Reserved2
; //More reserved
36 ULONG DmaBufferOffset
; //Byte offset into buffer. First 2 bits must be 0
37 ULONG TranferCount
; //Number of bytes to transfer. Bit 0 must be 0
38 ULONG Reserved3
; //Reserved
41 typedef struct _AHCI_PIO_SETUP_FIS
45 UCHAR D
:1; // 1 is write (device to host)
62 UCHAR SectorCount_Exp
;
70 typedef struct _AHCI_D2H_REGISTER_FIS
90 UCHAR SectorCount_Exp
;
94 } AHCI_D2H_REGISTER_FIS
;
96 typedef struct _AHCI_SET_DEVICE_BITS_FIS
113 } AHCI_SET_DEVICE_BITS_FIS
;
115 //////////////////////////////////////////////////////////////
116 // --------------------------- //
117 //////////////////////////////////////////////////////////////
119 // 4.2.2 Command Header
120 typedef struct _AHCI_COMMAND_HEADER
122 ULONG HEADER_DESCRIPTION
; // DW 0
125 ULONG CTBA_U0
; // DW 3
126 ULONG Reserved
[4]; // DW 4-7
127 } AHCI_COMMAND_HEADER
, *PAHCI_COMMAND_HEADER
;
130 typedef struct _AHCI_RECEIVED_FIS
132 struct _AHCI_FIS_DMA_SETUP DmaSetupFIS
; // 0x00 -- DMA Setup FIS
133 ULONG pad0
; // 4 BYTE padding
134 struct _AHCI_PIO_SETUP_FIS PioSetupFIS
; // 0x20 -- PIO Setup FIS
135 ULONG pad1
[3]; // 12 BYTE padding
136 struct _AHCI_D2H_REGISTER_FIS RegisterFIS
; // 0x40 -- Register – Device to Host FIS
137 ULONG pad2
; // 4 BYTE padding
138 struct _AHCI_SET_DEVICE_BITS_FIS SetDeviceFIS
; // 0x58 -- Set Device Bit FIS
139 ULONG UnknowFIS
[16]; // 0x60 -- Unknown FIS
140 ULONG Reserved
[24]; // 0xA0 -- Reserved
141 } AHCI_RECEIVED_FIS
, *PAHCI_RECEIVED_FIS
;
143 // Holds Port Information
144 typedef struct _AHCI_PORT
146 ULONG CLB
; // 0x00, command list base address, 1K-byte aligned
147 ULONG CLBU
; // 0x04, command list base address upper 32 bits
148 ULONG FB
; // 0x08, FIS base address, 256-byte aligned
149 ULONG FBU
; // 0x0C, FIS base address upper 32 bits
150 ULONG IS
; // 0x10, interrupt status
151 ULONG IE
; // 0x14, interrupt enable
152 ULONG CMD
; // 0x18, command and status
153 ULONG RSV0
; // 0x1C, Reserved
154 ULONG TFD
; // 0x20, task file data
155 ULONG SIG
; // 0x24, signature
156 ULONG SSTS
; // 0x28, SATA status (SCR0:SStatus)
157 ULONG SCTL
; // 0x2C, SATA control (SCR2:SControl)
158 ULONG SERR
; // 0x30, SATA error (SCR1:SError)
159 ULONG SACT
; // 0x34, SATA active (SCR3:SActive)
160 ULONG CI
; // 0x38, command issue
161 ULONG SNTF
; // 0x3C, SATA notification (SCR4:SNotification)
162 ULONG FBS
; // 0x40, FIS-based switch control
163 ULONG RSV1
[11]; // 0x44 ~ 0x6F, Reserved
164 ULONG Vendor
[4]; // 0x70 ~ 0x7F, vendor specific
165 } AHCI_PORT
, *PAHCI_PORT
;
167 typedef struct _AHCI_MEMORY_REGISTERS
169 // 0x00 - 0x2B, Generic Host Control
170 ULONG CAP
; // 0x00, Host capability
171 ULONG GHC
; // 0x04, Global host control
172 ULONG IS
; // 0x08, Interrupt status
173 ULONG PI
; // 0x0C, Port implemented
174 ULONG VS
; // 0x10, Version
175 ULONG CCC_CTL
; // 0x14, Command completion coalescing control
176 ULONG CCC_PTS
; // 0x18, Command completion coalescing ports
177 ULONG EM_LOC
; // 0x1C, Enclosure management location
178 ULONG EM_CTL
; // 0x20, Enclosure management control
179 ULONG CAP2
; // 0x24, Host capabilities extended
180 ULONG BOHC
; // 0x28, BIOS/OS handoff control and status
181 ULONG Reserved
[0xA0-0x2C]; // 0x2C - 0x9F, Reserved
182 ULONG VendorSpecific
[0x100-0xA0]; // 0xA0 - 0xFF, Vendor specific registers
183 AHCI_PORT PortList
[MAXIMUM_AHCI_PORT_COUNT
];
185 } AHCI_MEMORY_REGISTERS
, *PAHCI_MEMORY_REGISTERS
;
187 // Holds information for each attached attached port to a given adapter.
188 typedef struct _AHCI_PORT_EXTENSION
192 PAHCI_PORT Port
; // AHCI Port Infomation
193 PAHCI_RECEIVED_FIS ReceivedFIS
;
194 PAHCI_COMMAND_HEADER CommandList
;
195 STOR_DEVICE_POWER_STATE DevicePowerState
; // Device Power State
196 struct _AHCI_ADAPTER_EXTENSION
* AdapterExtension
; // Port's Adapter Information
197 } AHCI_PORT_EXTENSION
, *PAHCI_PORT_EXTENSION
;
199 // Holds Adapter Information
200 typedef struct _AHCI_ADAPTER_EXTENSION
202 ULONG SystemIoBusNumber
;
204 ULONG AhciBaseAddress
;
205 PULONG IS
;// Interrupt Status, In case of MSIM == `1`
206 ULONG PortImplemented
;// bit-mapping of ports which are implemented
215 ULONG LastInterruptPort
;
217 PVOID NonCachedExtension
;// holds virtual address to noncached buffer allocated for Port Extension
221 // Message per port or shared port?
222 ULONG MessagePerPort
: 1;
224 ULONG Reserved
: 30; // not in use -- maintain 4 byte alignment
227 PAHCI_MEMORY_REGISTERS ABAR_Address
;
228 AHCI_PORT_EXTENSION PortExtension
[MAXIMUM_AHCI_PORT_COUNT
];
229 } AHCI_ADAPTER_EXTENSION
, *PAHCI_ADAPTER_EXTENSION
;
231 typedef struct _AHCI_SRB_EXTENSION
234 } AHCI_SRB_EXTENSION
;
236 //////////////////////////////////////////////////////////////
238 //////////////////////////////////////////////////////////////
240 BOOLEAN
AhciAdapterReset(
241 __in PAHCI_ADAPTER_EXTENSION adapterExtension
247 __in ULONG bufferSize
252 __in PAHCI_ADAPTER_EXTENSION adapterExtension
,
256 ULONG
DeviceInquiryRequest(
257 __in PAHCI_ADAPTER_EXTENSION adapterExtension
,
258 __in PSCSI_REQUEST_BLOCK Srb
,