1 /* 2 * Copyright 2007, Marcus Overhagen. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _AHCI_DEFS_H 6 #define _AHCI_DEFS_H 7 8 #include <bus/PCI.h> 9 #include <bus/SCSI.h> 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 #define AHCI_DEVICE_MODULE_NAME "busses/scsi/ahci/device_v1" 16 #define AHCI_SIM_MODULE_NAME "busses/scsi/ahci/sim/v1" 17 18 // RW1 = Write 1 to set bit (writing 0 is ignored) 19 // RWC = Write 1 to clear bit (writing 0 is ignored) 20 21 22 enum { 23 CAP_S64A = (1 << 31), // Supports 64-bit Addressing 24 CAP_SNCQ = (1 << 30), // Supports Native Command Queuing 25 CAP_SSNTF = (1 << 29), // Supports SNotification Register 26 CAP_SMPS = (1 << 28), // Supports Mechanical Presence Switch 27 CAP_SSS = (1 << 27), // Supports Staggered Spin-up 28 CAP_SALP = (1 << 26), // Supports Aggressive Link Power Management 29 CAP_SAL = (1 << 25), // Supports Activity LED 30 CAP_SCLO = (1 << 24), // Supports Command List Override 31 CAP_ISS_MASK = 0xf, // Interface Speed Support 32 CAP_ISS_SHIFT = 20, 33 CAP_SNZO = (1 << 19), // Supports Non-Zero DMA Offsets 34 CAP_SAM = (1 << 18), // Supports AHCI mode only 35 CAP_SPM = (1 << 17), // Supports Port Multiplier 36 CAP_FBSS = (1 << 16), // FIS-based Switching Supported 37 CAP_PMD = (1 << 15), // PIO Multiple DRQ Block 38 CAP_SSC = (1 << 14), // Slumber State Capable 39 CAP_PSC = (1 << 13), // Partial State Capable 40 CAP_NCS_MASK = 0x1f, // Number of Command Slots (zero-based number) 41 CAP_NCS_SHIFT = 8, 42 CAP_CCCS = (1 << 7), // Command Completion Coalescing Supported 43 CAP_EMS = (1 << 6), // Enclosure Management Supported 44 CAP_SXS = (1 << 5), // Supports External SATA 45 CAP_NP_MASK = 0x1f, // Number of Ports (zero-based number) 46 CAP_NP_SHIFT = 0, 47 }; 48 49 50 enum { 51 GHC_AE = (1 << 31), // AHCI Enable 52 GHC_MRSM = (1 << 2), // MSI Revert to Single Message 53 GHC_IE = (1 << 1), // Interrupt Enable 54 GHC_HR = (1 << 0), // HBA Reset **RW1** 55 }; 56 57 58 enum { 59 INT_CPD = (1 << 31), // Cold Port Detect Status/Enable 60 INT_TFE = (1 << 30), // Task File Error Status/Enable 61 INT_HBF = (1 << 29), // Host Bus Fatal Error Status/Enable 62 INT_HBD = (1 << 28), // Host Bus Data Error Status/Enable 63 INT_IF = (1 << 27), // Interface Fatal Error Status/Enable 64 INT_INF = (1 << 26), // Interface Non-fatal Error Status/Enable 65 INT_OF = (1 << 24), // Overflow Status/Enable 66 INT_IPM = (1 << 23), // Incorrect Port Multiplier Status/Enable 67 INT_PRC = (1 << 22), // PhyRdy Change Status/Enable 68 INT_DMP = (1 << 7), // Device Mechanical Presence Status/Enable 69 INT_PC = (1 << 6), // Port Change Interrupt Status/Enable 70 INT_DP = (1 << 5), // Descriptor Processed Interrupt/Enable 71 INT_UF = (1 << 4), // Unknown FIS Interrupt/Enable 72 INT_SDB = (1 << 3), // Set Device Bits Interrupt/Enable 73 INT_DS = (1 << 2), // DMA Setup FIS Interrupt/Enable 74 INT_PS = (1 << 1), // PIO Setup FIS Interrupt/Enable 75 INT_DHR = (1 << 0), // Device to Host Register FIS Interrupt/Enable 76 }; 77 78 79 typedef struct { 80 uint32 clb; // Command List Base Address (alignment 1024 byte) 81 uint32 clbu; // Command List Base Address Upper 32-Bits 82 uint32 fb; // FIS Base Address (alignment 256 byte) 83 uint32 fbu; // FIS Base Address Upper 32-Bits 84 uint32 is; // Interrupt Status **RWC** 85 uint32 ie; // Interrupt Enable 86 uint32 cmd; // Command and Status 87 uint32 res1; // Reserved 88 uint32 tfd; // Task File Data 89 uint32 sig; // Signature 90 uint32 ssts; // Serial ATA Status (SCR0: SStatus) 91 uint32 sctl; // Serial ATA Control (SCR2: SControl) 92 uint32 serr; // Serial ATA Error (SCR1: SError) **RWC** 93 uint32 sact; // Serial ATA Active (SCR3: SActive) **RW1** 94 uint32 ci; // Command Issue **RW1** 95 uint32 sntf; // SNotification 96 uint32 res2; // Reserved for FIS-based Switching Definition 97 uint32 res[11]; // Reserved 98 uint32 vendor[4]; // Vendor Specific 99 } _PACKED ahci_port; 100 101 102 enum { 103 PORT_CMD_ICC_ACTIVE = (1 << 28), // Interface Communication control 104 PORT_CMD_ICC_SLUMBER = (6 << 28), // Interface Communication control 105 PORT_CMD_ICC_MASK = (0xf<<28), // Interface Communication control 106 PORT_CMD_ATAPI = (1 << 24), // Device is ATAPI 107 PORT_CMD_CR = (1 << 15), // Command List Running (DMA active) 108 PORT_CMD_FR = (1 << 14), // FIS Receive Running 109 PORT_CMD_FER = (1 << 4), // FIS Receive Enable 110 PORT_CMD_CLO = (1 << 3), // Command List Override 111 PORT_CMD_POD = (1 << 2), // Power On Device 112 PORT_CMD_SUD = (1 << 1), // Spin-up Device 113 PORT_CMD_ST = (1 << 0), // Start DMA 114 }; 115 116 117 enum { 118 PORT_INT_CPD = (1 << 31), // Cold Presence Detect Status/Enable 119 PORT_INT_TFE = (1 << 30), // Task File Error Status/Enable 120 PORT_INT_HBF = (1 << 29), // Host Bus Fatal Error Status/Enable 121 PORT_INT_HBD = (1 << 28), // Host Bus Data Error Status/Enable 122 PORT_INT_IF = (1 << 27), // Interface Fatal Error Status/Enable 123 PORT_INT_INF = (1 << 26), // Interface Non-fatal Error Status/Enable 124 PORT_INT_OF = (1 << 24), // Overflow Status/Enable 125 PORT_INT_IPM = (1 << 23), // Incorrect Port Multiplier Status/Enable 126 PORT_INT_PRCE = (1 << 22), // PhyRdy Change Status/Enable 127 PORT_INT_DI = (1 << 7), // Device Interlock Status/Enable 128 PORT_INT_PC = (1 << 6), // Port Change Status/Enable 129 PORT_INT_DP = (1 << 5), // Descriptor Processed Interrupt 130 PORT_INT_UF = (1 << 4), // Unknown FIS Interrupt 131 PORT_INT_SDB = (1 << 3), // Set Device Bits FIS Interrupt 132 PORT_INT_DS = (1 << 2), // DMA Setup FIS Interrupt 133 PORT_INT_PS = (1 << 1), // PIO Setup FIS Interrupt 134 PORT_INT_DHR = (1 << 0), // Device to Host Register FIS Interrupt 135 }; 136 137 #define PORT_INT_FATAL (PORT_INT_HBF | PORT_INT_IF | PORT_INT_IPM | PORT_INT_UF) 138 #define PORT_INT_ERROR (PORT_INT_TFE | PORT_INT_HBD) 139 #define PORT_INT_MASK (PORT_INT_FATAL | PORT_INT_ERROR | PORT_INT_DP |\ 140 PORT_INT_SDB | PORT_INT_DS | PORT_INT_PS | PORT_INT_DHR) 141 142 enum { 143 ATA_BSY = 0x80, 144 ATA_DRQ = 0x08, 145 ATA_ERR = 0x01, 146 }; 147 148 149 typedef struct { 150 uint32 cap; // Host Capabilities 151 uint32 ghc; // Global Host Control 152 uint32 is; // Interrupt Status 153 uint32 pi; // Ports Implemented 154 uint32 vs; // Version 155 uint32 ccc_ctl; // Command Completion Coalescing Control 156 uint32 ccc_ports; // Command Completion Coalsecing Ports 157 uint32 em_loc; // Enclosure Management Location 158 uint32 em_ctl; // Enclosure Management Control 159 uint32 res[31]; // Reserved 160 uint32 vendor[24]; // Vendor Specific registers 161 ahci_port port[32]; 162 } _PACKED ahci_hba; 163 164 165 typedef struct { 166 uint8 dsfis[0x1c]; // DMA Setup FIS 167 uint8 res1[0x04]; 168 uint8 psfis[0x14]; // PIO Setup FIS 169 uint8 res2[0x0c]; 170 uint8 rfis[0x14]; // D2H Register FIS 171 uint8 res3[0x04]; 172 uint8 sdbfis[0x08]; // Set Device Bits FIS 173 uint8 ufis[0x40]; // Unknown FIS 174 uint8 res4[0x60]; 175 } _PACKED fis; 176 177 178 typedef struct { 179 union { 180 struct { 181 uint16 cfl : 5; // command FIS length 182 uint16 a : 1; // ATAPI 183 uint16 w : 1; // Write 184 uint16 p : 1; // Prefetchable 185 uint16 r : 1; // Reset 186 uint16 b : 1; // Build In Self Test 187 uint16 c : 1; // Clear Busy upon R_OK 188 uint16 : 1; 189 uint16 pmp : 4; // Port Multiplier Port 190 uint16 prdtl; // physical region description table length; 191 } _PACKED; 192 uint32 prdtl_flags_cfl; 193 } _PACKED; 194 uint32 prdbc; // PRD Byte Count 195 uint32 ctba; // command table desciptor base address (alignment 128 byte) 196 uint32 ctbau; // command table desciptor base address upper 197 uint8 res1[0x10]; 198 } _PACKED command_list_entry; 199 200 #define COMMAND_LIST_ENTRY_COUNT 32 201 202 203 // Command FIS layout - Host to Device (H2D) 204 // 0 - FIS Type (0x27) 205 // 1 - C bit (0x80) 206 // 2 - Command 207 // 3 - Features 208 // 4 - Sector Number (LBA Low, bits 0-7) 209 // 5 - Cylinder Low (LBA Mid, bits 8-15) 210 // 6 - Cylinder High (LBA High, bits 16-23) 211 // 7 - Device / Head (for 28-bit LBA commands, bits 24-27) 212 // 8 - Sector Number expanded (LBA Low-previous, bits 24-31) 213 // 9 - Cylinder Low expanded (LBA Mid-previous, bits 32-39) 214 // 10 - Cylinder High expanded (LBA High-previous, bits 40-47) 215 // 11 - Features expanded 216 // 12 - Sector Count (Sector count, bits 0-7) 217 // 13 - Sector Count expanded (Sector count, bits 8-15) 218 // 14 - Reserved (0) 219 // 15 - Control 220 // 16 - Reserved (0) 221 // 17 - Reserved (0) 222 // 18 - Reserved (0) 223 // 19 - Reserved (0) 224 typedef struct { 225 uint8 cfis[0x40]; // command FIS 226 uint8 acmd[0x20]; // ATAPI command 227 uint8 res[0x20]; // reserved 228 } _PACKED command_table; 229 230 231 typedef struct { 232 uint32 dba; // Data Base Address (2-byte aligned) 233 uint32 dbau; // Data Base Address Upper 234 uint32 res; 235 uint32 dbc; // Bytecount (0-based, even, max 4MB) 236 #define DBC_I 0x80000000 /* Interrupt on completition */ 237 } _PACKED prd; 238 239 #define PRD_TABLE_ENTRY_COUNT 168 240 #define PRD_MAX_DATA_LENGTH 0x400000 /* 4 MB */ 241 242 243 typedef struct { 244 uint16 vendor; 245 uint16 device; 246 const char *name; 247 uint32 flags; 248 } device_info; 249 250 status_t get_device_info(uint16 vendorID, uint16 deviceID, const char **name, uint32 *flags); 251 252 253 extern scsi_sim_interface gAHCISimInterface; 254 extern device_manager_info *gDeviceManager; 255 extern pci_device_module_info *gPCI; 256 extern scsi_for_sim_interface *gSCSI; 257 258 259 #define MAX_SECTOR_LBA_28 ((1ull << 28) - 1) 260 #define MAX_SECTOR_LBA_48 ((1ull << 48) - 1) 261 262 263 #define LO32(val) ((uint32)(val)) 264 #define HI32(val) (((uint64)(val)) >> 32) 265 #define ASSERT(expr) if (expr) {} else panic(#expr) 266 267 #define PCI_VENDOR_INTEL 0x8086 268 #define PCI_VENDOR_JMICRON 0x197b 269 #define PCI_JMICRON_CONTROLLER_CONTROL_1 0x40 270 271 #ifdef __cplusplus 272 } 273 #endif 274 275 #ifdef __cplusplus 276 277 template <class T> 278 int count_bits_set(T value) 279 { 280 int count = 0; 281 for (T mask = 1; mask; mask <<= 1) 282 if (value & mask) 283 count++; 284 return count; 285 } 286 287 inline 288 status_t 289 wait_until_set(volatile uint32 *reg, uint32 bits, bigtime_t timeout) 290 { 291 int trys = (timeout + 9999) / 10000; 292 while (trys--) { 293 if (((*reg) & bits) == bits) 294 return B_OK; 295 snooze(10000); 296 } 297 return B_TIMED_OUT; 298 } 299 300 inline 301 status_t 302 wait_until_clear(volatile uint32 *reg, uint32 bits, bigtime_t timeout) 303 { 304 int trys = (timeout + 9999) / 10000; 305 while (trys--) { 306 if (((*reg) & bits) == 0) 307 return B_OK; 308 snooze(10000); 309 } 310 return B_TIMED_OUT; 311 } 312 313 #endif /* __cplusplus */ 314 315 #endif /* _AHCI_DEFS_H */ 316