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)
14 #define MAXIMUM_AHCI_PORT_COUNT 25
15 #define MAXIMUM_QUEUE_BUFFER_SIZE 255
16 #define MAXIMUM_TRANSFER_LENGTH (128*1024) // 128 KB
19 #define AHCI_Global_HBA_CONTROL_HR (1 << 0)
20 #define AHCI_Global_HBA_CONTROL_IE (1 << 1)
21 #define AHCI_Global_HBA_CONTROL_MRSM (1 << 2)
22 #define AHCI_Global_HBA_CONTROL_AE (1 << 31)
23 #define AHCI_Global_HBA_CAP_S64A (1 << 31)
26 #define ATA_FUNCTION_ATA_COMMAND 0x100
27 #define ATA_FUNCTION_ATA_IDENTIFY 0x101
30 #define ATA_FUNCTION_ATAPI_COMMAND 0x200
33 #define ATA_FLAGS_DATA_IN (1 << 1)
34 #define ATA_FLAGS_DATA_OUT (1 << 2)
36 #define IsAtaCommand(AtaFunction) (AtaFunction & ATA_FUNCTION_ATA_COMMAND)
37 #define IsAtapiCommand(AtaFunction) (AtaFunction & ATA_FUNCTION_ATAPI_COMMAND)
38 #define IsDataTransferNeeded(SrbExtension) (SrbExtension->Flags & (ATA_FLAGS_DATA_IN | ATA_FLAGS_DATA_OUT))
39 #define IsAdapterCAPS64(CAP) (CAP & AHCI_Global_HBA_CAP_S64A)
41 // 3.1.1 NCS = CAP[12:08] -> Align
42 #define AHCI_Global_Port_CAP_NCS(x) (((x) & 0xF00) >> 8)
44 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
46 #define DebugPrint(format, ...) StorPortDebugPrint(0, format, __VA_ARGS__)
49 //////////////////////////////////////////////////////////////
50 // ---- Support Structures --- //
51 //////////////////////////////////////////////////////////////
54 typedef union _AHCI_INTERRUPT_STATUS
58 ULONG DHRS
:1; //Device to Host Register FIS Interrupt
59 ULONG PSS
:1; //PIO Setup FIS Interrupt
60 ULONG DSS
:1; //DMA Setup FIS Interrupt
61 ULONG SDBS
:1; //Set Device Bits Interrupt
62 ULONG UFS
:1; //Unknown FIS Interrupt
63 ULONG DPS
:1; //Descriptor Processed
64 ULONG PCS
:1; //Port Connect Change Status
65 ULONG DMPS
:1; //Device Mechanical Presence Status (DMPS)
67 ULONG PRCS
:1; //PhyRdy Change Status
68 ULONG IPMS
:1; //Incorrect Port Multiplier Status
69 ULONG OFS
:1; //Overflow Status
71 ULONG INFS
:1; //Interface Non-fatal Error Status
72 ULONG IFS
:1; //Interface Fatal Error Status
73 ULONG HBDS
:1; //Host Bus Data Error Status
74 ULONG HBFS
:1; //Host Bus Fatal Error Status
75 ULONG TFES
:1; //Task File Error Status
76 ULONG CPDS
:1; //Cold Port Detect Status
80 } AHCI_INTERRUPT_STATUS
;
82 typedef struct _AHCI_FIS_DMA_SETUP
84 ULONG ULONG0_1
; // FIS_TYPE_DMA_SETUP
87 // Data transfer direction, 1 - device to host
89 // Auto-activate. Specifies if DMA Activate FIS is needed
90 UCHAR Reserved
[2]; // Reserved
91 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.
93 ULONG Reserved2
; // More reserved
94 ULONG DmaBufferOffset
; // Byte offset into buffer. First 2 bits must be 0
95 ULONG TranferCount
; // Number of bytes to transfer. Bit 0 must be 0
96 ULONG Reserved3
; // Reserved
99 typedef struct _AHCI_PIO_SETUP_FIS
114 UCHAR SectorNumb_Exp
;
120 UCHAR SectorCount_Exp
;
124 USHORT TransferCount
;
126 } AHCI_PIO_SETUP_FIS
;
128 typedef struct _AHCI_D2H_REGISTER_FIS
148 UCHAR SectorCount_Exp
;
152 } AHCI_D2H_REGISTER_FIS
;
154 typedef struct _AHCI_SET_DEVICE_BITS_FIS
171 } AHCI_SET_DEVICE_BITS_FIS
;
173 typedef struct _AHCI_QUEUE
175 PVOID Buffer
[MAXIMUM_QUEUE_BUFFER_SIZE
]; // because Storahci hold Srb queue of 255 size
178 } AHCI_QUEUE
, *PAHCI_QUEUE
;
180 //////////////////////////////////////////////////////////////
181 // --------------------------- //
182 //////////////////////////////////////////////////////////////
184 typedef union _AHCI_COMMAND_HEADER_DESCRIPTION
188 ULONG CFL
:5; // Command FIS Length
189 ULONG A
:1; // IsATAPI
191 ULONG P
:1; // Prefetchable
195 ULONG C
:1; //Clear Busy upon R_OK
196 ULONG DW0_Reserved
:1;
197 ULONG PMP
:4; //Port Multiplier Port
199 ULONG PRDTL
:16; //Physical Region Descriptor Table Length
203 } AHCI_COMMAND_HEADER_DESCRIPTION
;
205 // 4.2.2 Command Header
206 typedef struct _AHCI_COMMAND_HEADER
208 AHCI_COMMAND_HEADER_DESCRIPTION DI
; // DW 0
211 ULONG CTBA_U0
; // DW 3
212 ULONG Reserved
[4]; // DW 4-7
213 } AHCI_COMMAND_HEADER
, *PAHCI_COMMAND_HEADER
;
216 typedef struct _AHCI_RECEIVED_FIS
218 struct _AHCI_FIS_DMA_SETUP DmaSetupFIS
; // 0x00 -- DMA Setup FIS
219 ULONG pad0
; // 4 BYTE padding
220 struct _AHCI_PIO_SETUP_FIS PioSetupFIS
; // 0x20 -- PIO Setup FIS
221 ULONG pad1
[3]; // 12 BYTE padding
222 struct _AHCI_D2H_REGISTER_FIS RegisterFIS
; // 0x40 -- Register – Device to Host FIS
223 ULONG pad2
; // 4 BYTE padding
224 struct _AHCI_SET_DEVICE_BITS_FIS SetDeviceFIS
; // 0x58 -- Set Device Bit FIS
225 ULONG UnknowFIS
[16]; // 0x60 -- Unknown FIS
226 ULONG Reserved
[24]; // 0xA0 -- Reserved
227 } AHCI_RECEIVED_FIS
, *PAHCI_RECEIVED_FIS
;
229 // Holds Port Information
230 typedef struct _AHCI_PORT
232 ULONG CLB
; // 0x00, command list base address, 1K-byte aligned
233 ULONG CLBU
; // 0x04, command list base address upper 32 bits
234 ULONG FB
; // 0x08, FIS base address, 256-byte aligned
235 ULONG FBU
; // 0x0C, FIS base address upper 32 bits
236 ULONG IS
; // 0x10, interrupt status
237 ULONG IE
; // 0x14, interrupt enable
238 ULONG CMD
; // 0x18, command and status
239 ULONG RSV0
; // 0x1C, Reserved
240 ULONG TFD
; // 0x20, task file data
241 ULONG SIG
; // 0x24, signature
242 ULONG SSTS
; // 0x28, SATA status (SCR0:SStatus)
243 ULONG SCTL
; // 0x2C, SATA control (SCR2:SControl)
244 ULONG SERR
; // 0x30, SATA error (SCR1:SError)
245 ULONG SACT
; // 0x34, SATA active (SCR3:SActive)
246 ULONG CI
; // 0x38, command issue
247 ULONG SNTF
; // 0x3C, SATA notification (SCR4:SNotification)
248 ULONG FBS
; // 0x40, FIS-based switch control
249 ULONG RSV1
[11]; // 0x44 ~ 0x6F, Reserved
250 ULONG Vendor
[4]; // 0x70 ~ 0x7F, vendor specific
251 } AHCI_PORT
, *PAHCI_PORT
;
253 typedef struct _AHCI_MEMORY_REGISTERS
255 // 0x00 - 0x2B, Generic Host Control
256 ULONG CAP
; // 0x00, Host capability
257 ULONG GHC
; // 0x04, Global host control
258 ULONG IS
; // 0x08, Interrupt status
259 ULONG PI
; // 0x0C, Port implemented
260 ULONG VS
; // 0x10, Version
261 ULONG CCC_CTL
; // 0x14, Command completion coalescing control
262 ULONG CCC_PTS
; // 0x18, Command completion coalescing ports
263 ULONG EM_LOC
; // 0x1C, Enclosure management location
264 ULONG EM_CTL
; // 0x20, Enclosure management control
265 ULONG CAP2
; // 0x24, Host capabilities extended
266 ULONG BOHC
; // 0x28, BIOS/OS handoff control and status
267 ULONG Reserved
[0xA0-0x2C]; // 0x2C - 0x9F, Reserved
268 ULONG VendorSpecific
[0x100-0xA0]; // 0xA0 - 0xFF, Vendor specific registers
269 AHCI_PORT PortList
[MAXIMUM_AHCI_PORT_COUNT
];
271 } AHCI_MEMORY_REGISTERS
, *PAHCI_MEMORY_REGISTERS
;
273 // Holds information for each attached attached port to a given adapter.
274 typedef struct _AHCI_PORT_EXTENSION
277 ULONG OccupiedSlots
; // slots to which we have already assigned task
279 PAHCI_PORT Port
; // AHCI Port Infomation
281 PAHCI_RECEIVED_FIS ReceivedFIS
;
282 PAHCI_COMMAND_HEADER CommandList
;
283 STOR_DEVICE_POWER_STATE DevicePowerState
; // Device Power State
284 struct _AHCI_ADAPTER_EXTENSION
* AdapterExtension
; // Port's Adapter Information
285 } AHCI_PORT_EXTENSION
, *PAHCI_PORT_EXTENSION
;
287 // Holds Adapter Information
288 typedef struct _AHCI_ADAPTER_EXTENSION
290 ULONG SystemIoBusNumber
;
292 ULONG AhciBaseAddress
;
293 PULONG IS
;// Interrupt Status, In case of MSIM == `1`
294 ULONG PortImplemented
;// bit-mapping of ports which are implemented
304 ULONG LastInterruptPort
;
305 ULONG CurrentCommandSlot
;
307 PVOID NonCachedExtension
;// holds virtual address to noncached buffer allocated for Port Extension
311 // Message per port or shared port?
312 ULONG MessagePerPort
: 1;
314 ULONG Reserved
: 30; // not in use -- maintain 4 byte alignment
317 PAHCI_MEMORY_REGISTERS ABAR_Address
;
318 AHCI_PORT_EXTENSION PortExtension
[MAXIMUM_AHCI_PORT_COUNT
];
319 } AHCI_ADAPTER_EXTENSION
, *PAHCI_ADAPTER_EXTENSION
;
321 typedef struct _ATA_REGISTER
327 typedef struct _AHCI_SRB_EXTENSION
334 } AHCI_SRB_EXTENSION
, *PAHCI_SRB_EXTENSION
;
336 //////////////////////////////////////////////////////////////
338 //////////////////////////////////////////////////////////////
342 __in PAHCI_ADAPTER_EXTENSION AdapterExtension
349 __in ULONG BufferSize
355 __in PAHCI_ADAPTER_EXTENSION AdapterExtension
,
360 DeviceInquiryRequest (
361 __in PAHCI_ADAPTER_EXTENSION AdapterExtension
,
362 __in PSCSI_REQUEST_BLOCK Srb
,
369 __inout PAHCI_QUEUE Queue
,
376 __inout PAHCI_QUEUE Queue