| 1 | #ifndef LIBQOS_AHCI_H |
| 2 | #define LIBQOS_AHCI_H |
| 3 | |
| 4 | /* |
| 5 | * AHCI qtest library functions and definitions |
| 6 | * |
| 7 | * Copyright (c) 2014 John Snow <jsnow@redhat.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include "libqos.h" |
| 29 | #include "pci.h" |
| 30 | #include "malloc-pc.h" |
| 31 | |
| 32 | /*** Supplementary PCI Config Space IDs & Masks ***/ |
| 33 | #define PCI_DEVICE_ID_INTEL_Q35_AHCI (0x2922) |
| 34 | #define PCI_MSI_FLAGS_RESERVED (0xFF00) |
| 35 | #define PCI_PM_CTRL_RESERVED (0xFC) |
| 36 | #define PCI_BCC(REG32) ((REG32) >> 24) |
| 37 | #define PCI_PI(REG32) (((REG32) >> 8) & 0xFF) |
| 38 | #define PCI_SCC(REG32) (((REG32) >> 16) & 0xFF) |
| 39 | |
| 40 | /*** Recognized AHCI Device Types ***/ |
| 41 | #define AHCI_INTEL_ICH9 (PCI_DEVICE_ID_INTEL_Q35_AHCI << 16 | \ |
| 42 | PCI_VENDOR_ID_INTEL) |
| 43 | |
| 44 | /*** AHCI/HBA Register Offsets and Bitmasks ***/ |
| 45 | #define AHCI_CAP (0) |
| 46 | #define AHCI_CAP_NP (0x1F) |
| 47 | #define AHCI_CAP_SXS (0x20) |
| 48 | #define AHCI_CAP_EMS (0x40) |
| 49 | #define AHCI_CAP_CCCS (0x80) |
| 50 | #define AHCI_CAP_NCS (0x1F00) |
| 51 | #define AHCI_CAP_PSC (0x2000) |
| 52 | #define AHCI_CAP_SSC (0x4000) |
| 53 | #define AHCI_CAP_PMD (0x8000) |
| 54 | #define AHCI_CAP_FBSS (0x10000) |
| 55 | #define AHCI_CAP_SPM (0x20000) |
| 56 | #define AHCI_CAP_SAM (0x40000) |
| 57 | #define AHCI_CAP_RESERVED (0x80000) |
| 58 | #define AHCI_CAP_ISS (0xF00000) |
| 59 | #define AHCI_CAP_SCLO (0x1000000) |
| 60 | #define AHCI_CAP_SAL (0x2000000) |
| 61 | #define AHCI_CAP_SALP (0x4000000) |
| 62 | #define AHCI_CAP_SSS (0x8000000) |
| 63 | #define AHCI_CAP_SMPS (0x10000000) |
| 64 | #define AHCI_CAP_SSNTF (0x20000000) |
| 65 | #define AHCI_CAP_SNCQ (0x40000000) |
| 66 | #define AHCI_CAP_S64A (0x80000000) |
| 67 | |
| 68 | #define AHCI_GHC (1) |
| 69 | #define AHCI_GHC_HR (0x01) |
| 70 | #define AHCI_GHC_IE (0x02) |
| 71 | #define AHCI_GHC_MRSM (0x04) |
| 72 | #define AHCI_GHC_RESERVED (0x7FFFFFF8) |
| 73 | #define AHCI_GHC_AE (0x80000000) |
| 74 | |
| 75 | #define AHCI_IS (2) |
| 76 | #define AHCI_PI (3) |
| 77 | #define AHCI_VS (4) |
| 78 | |
| 79 | #define AHCI_CCCCTL (5) |
| 80 | #define AHCI_CCCCTL_EN (0x01) |
| 81 | #define AHCI_CCCCTL_RESERVED (0x06) |
| 82 | #define AHCI_CCCCTL_CC (0xFF00) |
| 83 | #define AHCI_CCCCTL_TV (0xFFFF0000) |
| 84 | |
| 85 | #define AHCI_CCCPORTS (6) |
| 86 | #define AHCI_EMLOC (7) |
| 87 | |
| 88 | #define AHCI_EMCTL (8) |
| 89 | #define AHCI_EMCTL_STSMR (0x01) |
| 90 | #define AHCI_EMCTL_CTLTM (0x100) |
| 91 | #define AHCI_EMCTL_CTLRST (0x200) |
| 92 | #define AHCI_EMCTL_RESERVED (0xF0F0FCFE) |
| 93 | |
| 94 | #define AHCI_CAP2 (9) |
| 95 | #define AHCI_CAP2_BOH (0x01) |
| 96 | #define AHCI_CAP2_NVMP (0x02) |
| 97 | #define AHCI_CAP2_APST (0x04) |
| 98 | #define AHCI_CAP2_RESERVED (0xFFFFFFF8) |
| 99 | |
| 100 | #define AHCI_BOHC (10) |
| 101 | #define AHCI_RESERVED (11) |
| 102 | #define AHCI_NVMHCI (24) |
| 103 | #define AHCI_VENDOR (40) |
| 104 | #define AHCI_PORTS (64) |
| 105 | |
| 106 | /*** Port Memory Offsets & Bitmasks ***/ |
| 107 | #define AHCI_PX_CLB (0) |
| 108 | #define AHCI_PX_CLB_RESERVED (0x1FF) |
| 109 | |
| 110 | #define AHCI_PX_CLBU (1) |
| 111 | |
| 112 | #define AHCI_PX_FB (2) |
| 113 | #define AHCI_PX_FB_RESERVED (0xFF) |
| 114 | |
| 115 | #define AHCI_PX_FBU (3) |
| 116 | |
| 117 | #define AHCI_PX_IS (4) |
| 118 | #define AHCI_PX_IS_DHRS (0x1) |
| 119 | #define AHCI_PX_IS_PSS (0x2) |
| 120 | #define AHCI_PX_IS_DSS (0x4) |
| 121 | #define AHCI_PX_IS_SDBS (0x8) |
| 122 | #define AHCI_PX_IS_UFS (0x10) |
| 123 | #define AHCI_PX_IS_DPS (0x20) |
| 124 | #define AHCI_PX_IS_PCS (0x40) |
| 125 | #define AHCI_PX_IS_DMPS (0x80) |
| 126 | #define AHCI_PX_IS_RESERVED (0x23FFF00) |
| 127 | #define AHCI_PX_IS_PRCS (0x400000) |
| 128 | #define AHCI_PX_IS_IPMS (0x800000) |
| 129 | #define AHCI_PX_IS_OFS (0x1000000) |
| 130 | #define AHCI_PX_IS_INFS (0x4000000) |
| 131 | #define AHCI_PX_IS_IFS (0x8000000) |
| 132 | #define AHCI_PX_IS_HBDS (0x10000000) |
| 133 | #define AHCI_PX_IS_HBFS (0x20000000) |
| 134 | #define AHCI_PX_IS_TFES (0x40000000) |
| 135 | #define AHCI_PX_IS_CPDS (0x80000000) |
| 136 | |
| 137 | #define AHCI_PX_IE (5) |
| 138 | #define AHCI_PX_IE_DHRE (0x1) |
| 139 | #define AHCI_PX_IE_PSE (0x2) |
| 140 | #define AHCI_PX_IE_DSE (0x4) |
| 141 | #define AHCI_PX_IE_SDBE (0x8) |
| 142 | #define AHCI_PX_IE_UFE (0x10) |
| 143 | #define AHCI_PX_IE_DPE (0x20) |
| 144 | #define AHCI_PX_IE_PCE (0x40) |
| 145 | #define AHCI_PX_IE_DMPE (0x80) |
| 146 | #define AHCI_PX_IE_RESERVED (0x23FFF00) |
| 147 | #define AHCI_PX_IE_PRCE (0x400000) |
| 148 | #define AHCI_PX_IE_IPME (0x800000) |
| 149 | #define AHCI_PX_IE_OFE (0x1000000) |
| 150 | #define AHCI_PX_IE_INFE (0x4000000) |
| 151 | #define AHCI_PX_IE_IFE (0x8000000) |
| 152 | #define AHCI_PX_IE_HBDE (0x10000000) |
| 153 | #define AHCI_PX_IE_HBFE (0x20000000) |
| 154 | #define AHCI_PX_IE_TFEE (0x40000000) |
| 155 | #define AHCI_PX_IE_CPDE (0x80000000) |
| 156 | |
| 157 | #define AHCI_PX_CMD (6) |
| 158 | #define AHCI_PX_CMD_ST (0x1) |
| 159 | #define AHCI_PX_CMD_SUD (0x2) |
| 160 | #define AHCI_PX_CMD_POD (0x4) |
| 161 | #define AHCI_PX_CMD_CLO (0x8) |
| 162 | #define AHCI_PX_CMD_FRE (0x10) |
| 163 | #define AHCI_PX_CMD_RESERVED (0xE0) |
| 164 | #define AHCI_PX_CMD_CCS (0x1F00) |
| 165 | #define AHCI_PX_CMD_MPSS (0x2000) |
| 166 | #define AHCI_PX_CMD_FR (0x4000) |
| 167 | #define AHCI_PX_CMD_CR (0x8000) |
| 168 | #define AHCI_PX_CMD_CPS (0x10000) |
| 169 | #define AHCI_PX_CMD_PMA (0x20000) |
| 170 | #define AHCI_PX_CMD_HPCP (0x40000) |
| 171 | #define AHCI_PX_CMD_MPSP (0x80000) |
| 172 | #define AHCI_PX_CMD_CPD (0x100000) |
| 173 | #define AHCI_PX_CMD_ESP (0x200000) |
| 174 | #define AHCI_PX_CMD_FBSCP (0x400000) |
| 175 | #define AHCI_PX_CMD_APSTE (0x800000) |
| 176 | #define AHCI_PX_CMD_ATAPI (0x1000000) |
| 177 | #define AHCI_PX_CMD_DLAE (0x2000000) |
| 178 | #define AHCI_PX_CMD_ALPE (0x4000000) |
| 179 | #define AHCI_PX_CMD_ASP (0x8000000) |
| 180 | #define AHCI_PX_CMD_ICC (0xF0000000) |
| 181 | |
| 182 | #define AHCI_PX_RES1 (7) |
| 183 | |
| 184 | #define AHCI_PX_TFD (8) |
| 185 | #define AHCI_PX_TFD_STS (0xFF) |
| 186 | #define AHCI_PX_TFD_STS_ERR (0x01) |
| 187 | #define AHCI_PX_TFD_STS_CS1 (0x06) |
| 188 | #define AHCI_PX_TFD_STS_DRQ (0x08) |
| 189 | #define AHCI_PX_TFD_STS_CS2 (0x70) |
| 190 | #define AHCI_PX_TFD_STS_BSY (0x80) |
| 191 | #define AHCI_PX_TFD_ERR (0xFF00) |
| 192 | #define AHCI_PX_TFD_RESERVED (0xFFFF0000) |
| 193 | |
| 194 | #define AHCI_PX_SIG (9) |
| 195 | #define AHCI_PX_SIG_SECTOR_COUNT (0xFF) |
| 196 | #define AHCI_PX_SIG_LBA_LOW (0xFF00) |
| 197 | #define AHCI_PX_SIG_LBA_MID (0xFF0000) |
| 198 | #define AHCI_PX_SIG_LBA_HIGH (0xFF000000) |
| 199 | |
| 200 | #define AHCI_PX_SSTS (10) |
| 201 | #define AHCI_PX_SSTS_DET (0x0F) |
| 202 | #define AHCI_PX_SSTS_SPD (0xF0) |
| 203 | #define AHCI_PX_SSTS_IPM (0xF00) |
| 204 | #define AHCI_PX_SSTS_RESERVED (0xFFFFF000) |
| 205 | #define SSTS_DET_NO_DEVICE (0x00) |
| 206 | #define SSTS_DET_PRESENT (0x01) |
| 207 | #define SSTS_DET_ESTABLISHED (0x03) |
| 208 | #define SSTS_DET_OFFLINE (0x04) |
| 209 | |
| 210 | #define AHCI_PX_SCTL (11) |
| 211 | |
| 212 | #define AHCI_PX_SERR (12) |
| 213 | #define AHCI_PX_SERR_ERR (0xFFFF) |
| 214 | #define AHCI_PX_SERR_DIAG (0xFFFF0000) |
| 215 | #define AHCI_PX_SERR_DIAG_X (0x04000000) |
| 216 | |
| 217 | #define AHCI_PX_SACT (13) |
| 218 | #define AHCI_PX_CI (14) |
| 219 | #define AHCI_PX_SNTF (15) |
| 220 | |
| 221 | #define AHCI_PX_FBS (16) |
| 222 | #define AHCI_PX_FBS_EN (0x1) |
| 223 | #define AHCI_PX_FBS_DEC (0x2) |
| 224 | #define AHCI_PX_FBS_SDE (0x4) |
| 225 | #define AHCI_PX_FBS_DEV (0xF00) |
| 226 | #define AHCI_PX_FBS_ADO (0xF000) |
| 227 | #define AHCI_PX_FBS_DWE (0xF0000) |
| 228 | #define AHCI_PX_FBS_RESERVED (0xFFF000F8) |
| 229 | |
| 230 | #define AHCI_PX_RES2 (17) |
| 231 | #define AHCI_PX_VS (28) |
| 232 | |
| 233 | #define HBA_DATA_REGION_SIZE (256) |
| 234 | #define HBA_PORT_DATA_SIZE (128) |
| 235 | #define HBA_PORT_NUM_REG (HBA_PORT_DATA_SIZE/4) |
| 236 | |
| 237 | #define AHCI_VERSION_0_95 (0x00000905) |
| 238 | #define AHCI_VERSION_1_0 (0x00010000) |
| 239 | #define AHCI_VERSION_1_1 (0x00010100) |
| 240 | #define AHCI_VERSION_1_2 (0x00010200) |
| 241 | #define AHCI_VERSION_1_3 (0x00010300) |
| 242 | |
| 243 | #define AHCI_SECTOR_SIZE (512) |
| 244 | #define ATAPI_SECTOR_SIZE (2048) |
| 245 | #define ATAPI_RAW_SECTOR_SIZE (2352) |
| 246 | |
| 247 | #define AHCI_SIGNATURE_CDROM (0xeb140101) |
| 248 | #define AHCI_SIGNATURE_DISK (0x00000101) |
| 249 | |
| 250 | /* FIS types */ |
| 251 | enum { |
| 252 | REG_H2D_FIS = 0x27, |
| 253 | REG_D2H_FIS = 0x34, |
| 254 | DMA_ACTIVATE_FIS = 0x39, |
| 255 | DMA_SETUP_FIS = 0x41, |
| 256 | DATA_FIS = 0x46, |
| 257 | BIST_ACTIVATE_FIS = 0x58, |
| 258 | PIO_SETUP_FIS = 0x5F, |
| 259 | SDB_FIS = 0xA1 |
| 260 | }; |
| 261 | |
| 262 | /* FIS flags */ |
| 263 | #define REG_H2D_FIS_CMD 0x80 |
| 264 | |
| 265 | /* ATA Commands */ |
| 266 | enum { |
| 267 | /* DMA */ |
| 268 | CMD_READ_DMA = 0xC8, |
| 269 | CMD_READ_DMA_EXT = 0x25, |
| 270 | CMD_WRITE_DMA = 0xCA, |
| 271 | CMD_WRITE_DMA_EXT = 0x35, |
| 272 | /* PIO */ |
| 273 | CMD_READ_PIO = 0x20, |
| 274 | CMD_READ_PIO_EXT = 0x24, |
| 275 | CMD_WRITE_PIO = 0x30, |
| 276 | CMD_WRITE_PIO_EXT = 0x34, |
| 277 | /* Misc */ |
| 278 | CMD_READ_MAX = 0xF8, |
| 279 | CMD_READ_MAX_EXT = 0x27, |
| 280 | CMD_FLUSH_CACHE = 0xE7, |
| 281 | CMD_INIT_DP = 0x91, /* INITIALIZE DEVICE PARAMETERS */ |
| 282 | CMD_IDENTIFY = 0xEC, |
| 283 | CMD_PACKET = 0xA0, |
| 284 | CMD_PACKET_ID = 0xA1, |
| 285 | /* NCQ */ |
| 286 | READ_FPDMA_QUEUED = 0x60, |
| 287 | WRITE_FPDMA_QUEUED = 0x61, |
| 288 | }; |
| 289 | |
| 290 | /* ATAPI Commands */ |
| 291 | enum { |
| 292 | CMD_ATAPI_TEST_UNIT_READY = 0x00, |
| 293 | CMD_ATAPI_REQUEST_SENSE = 0x03, |
| 294 | CMD_ATAPI_START_STOP_UNIT = 0x1b, |
| 295 | CMD_ATAPI_READ_10 = 0x28, |
| 296 | CMD_ATAPI_READ_CD = 0xbe, |
| 297 | }; |
| 298 | |
| 299 | enum { |
| 300 | SENSE_NO_SENSE = 0x00, |
| 301 | SENSE_NOT_READY = 0x02, |
| 302 | SENSE_UNIT_ATTENTION = 0x06, |
| 303 | }; |
| 304 | |
| 305 | enum { |
| 306 | ASC_MEDIUM_MAY_HAVE_CHANGED = 0x28, |
| 307 | ASC_MEDIUM_NOT_PRESENT = 0x3a, |
| 308 | }; |
| 309 | |
| 310 | /* AHCI Command Header Flags & Masks*/ |
| 311 | #define CMDH_CFL (0x1F) |
| 312 | #define CMDH_ATAPI (0x20) |
| 313 | #define CMDH_WRITE (0x40) |
| 314 | #define CMDH_PREFETCH (0x80) |
| 315 | #define CMDH_RESET (0x100) |
| 316 | #define CMDH_BIST (0x200) |
| 317 | #define CMDH_CLR_BSY (0x400) |
| 318 | #define CMDH_RES (0x800) |
| 319 | #define CMDH_PMP (0xF000) |
| 320 | |
| 321 | /* ATA device register masks */ |
| 322 | #define ATA_DEVICE_MAGIC 0xA0 /* used in ata1-3 */ |
| 323 | #define ATA_DEVICE_LBA 0x40 |
| 324 | #define NCQ_DEVICE_MAGIC 0x40 /* for ncq device registers */ |
| 325 | #define ATA_DEVICE_DRIVE 0x10 |
| 326 | #define ATA_DEVICE_HEAD 0x0F |
| 327 | |
| 328 | /* ATA error register bits */ |
| 329 | #define ATA_ERR_ABRT 0x04 |
| 330 | |
| 331 | /*** Structures ***/ |
| 332 | |
| 333 | typedef struct AHCIPortQState { |
| 334 | uint64_t fb; |
| 335 | uint64_t clb; |
| 336 | uint64_t ctba[32]; |
| 337 | uint16_t prdtl[32]; |
| 338 | uint8_t next; /** Next Command Slot to Use **/ |
| 339 | } AHCIPortQState; |
| 340 | |
| 341 | typedef struct AHCIQState { |
| 342 | QOSState *parent; |
| 343 | QPCIDevice *dev; |
| 344 | QPCIBar hba_bar; |
| 345 | uint64_t barsize; |
| 346 | uint32_t fingerprint; |
| 347 | uint32_t cap; |
| 348 | uint32_t cap2; |
| 349 | AHCIPortQState port[32]; |
| 350 | bool enabled; |
| 351 | } AHCIQState; |
| 352 | |
| 353 | /** |
| 354 | * Generic FIS structure. |
| 355 | */ |
| 356 | typedef struct FIS { |
| 357 | uint8_t fis_type; |
| 358 | uint8_t flags; |
| 359 | char data[]; |
| 360 | } __attribute__((__packed__)) FIS; |
| 361 | |
| 362 | /** |
| 363 | * Register device-to-host FIS structure. |
| 364 | */ |
| 365 | typedef struct RegD2HFIS { |
| 366 | /* DW0 */ |
| 367 | uint8_t fis_type; |
| 368 | uint8_t flags; |
| 369 | uint8_t status; |
| 370 | uint8_t error; |
| 371 | /* DW1 */ |
| 372 | uint8_t lba_lo[3]; |
| 373 | uint8_t device; |
| 374 | /* DW2 */ |
| 375 | uint8_t lba_hi[3]; |
| 376 | uint8_t res0; |
| 377 | /* DW3 */ |
| 378 | uint16_t count; |
| 379 | uint16_t res1; |
| 380 | /* DW4 */ |
| 381 | uint32_t res2; |
| 382 | } __attribute__((__packed__)) RegD2HFIS; |
| 383 | |
| 384 | /** |
| 385 | * Register device-to-host FIS structure; |
| 386 | * PIO Setup variety. |
| 387 | */ |
| 388 | typedef struct PIOSetupFIS { |
| 389 | /* DW0 */ |
| 390 | uint8_t fis_type; |
| 391 | uint8_t flags; |
| 392 | uint8_t status; |
| 393 | uint8_t error; |
| 394 | /* DW1 */ |
| 395 | uint8_t lba_lo[3]; |
| 396 | uint8_t device; |
| 397 | /* DW2 */ |
| 398 | uint8_t lba_hi[3]; |
| 399 | uint8_t res0; |
| 400 | /* DW3 */ |
| 401 | uint16_t count; |
| 402 | uint8_t res1; |
| 403 | uint8_t e_status; |
| 404 | /* DW4 */ |
| 405 | uint16_t tx_count; |
| 406 | uint16_t res2; |
| 407 | } __attribute__((__packed__)) PIOSetupFIS; |
| 408 | |
| 409 | /** |
| 410 | * Register host-to-device FIS structure. |
| 411 | */ |
| 412 | typedef struct RegH2DFIS { |
| 413 | /* DW0 */ |
| 414 | uint8_t fis_type; |
| 415 | uint8_t flags; |
| 416 | uint8_t command; |
| 417 | uint8_t feature_low; |
| 418 | /* DW1 */ |
| 419 | uint8_t lba_lo[3]; |
| 420 | uint8_t device; |
| 421 | /* DW2 */ |
| 422 | uint8_t lba_hi[3]; |
| 423 | uint8_t feature_high; |
| 424 | /* DW3 */ |
| 425 | uint16_t count; |
| 426 | uint8_t icc; |
| 427 | uint8_t control; |
| 428 | /* DW4 */ |
| 429 | uint8_t aux[4]; |
| 430 | } __attribute__((__packed__)) RegH2DFIS; |
| 431 | |
| 432 | /** |
| 433 | * Register host-to-device FIS structure, for NCQ commands. |
| 434 | * Actually just a RegH2DFIS, but with fields repurposed. |
| 435 | * Repurposed fields are annotated below. |
| 436 | */ |
| 437 | typedef struct NCQFIS { |
| 438 | /* DW0 */ |
| 439 | uint8_t fis_type; |
| 440 | uint8_t flags; |
| 441 | uint8_t command; |
| 442 | uint8_t sector_low; /* H2D: Feature 7:0 */ |
| 443 | /* DW1 */ |
| 444 | uint8_t lba_lo[3]; |
| 445 | uint8_t device; |
| 446 | /* DW2 */ |
| 447 | uint8_t lba_hi[3]; |
| 448 | uint8_t sector_hi; /* H2D: Feature 15:8 */ |
| 449 | /* DW3 */ |
| 450 | uint8_t tag; /* H2D: Count 0:7 */ |
| 451 | uint8_t prio; /* H2D: Count 15:8 */ |
| 452 | uint8_t icc; |
| 453 | uint8_t control; |
| 454 | /* DW4 */ |
| 455 | uint8_t aux[4]; |
| 456 | } __attribute__((__packed__)) NCQFIS; |
| 457 | |
| 458 | /** |
| 459 | * Command List entry structure. |
| 460 | * The command list contains between 1-32 of these structures. |
| 461 | */ |
| 462 | typedef struct AHCICommandHeader { |
| 463 | uint16_t flags; /* Cmd-Fis-Len, PMP#, and flags. */ |
| 464 | uint16_t prdtl; /* Phys Region Desc. Table Length */ |
| 465 | uint32_t prdbc; /* Phys Region Desc. Byte Count */ |
| 466 | uint64_t ctba; /* Command Table Descriptor Base Address */ |
| 467 | uint32_t res[4]; |
| 468 | } __attribute__((__packed__)) AHCICommandHeader; |
| 469 | |
| 470 | /** |
| 471 | * Physical Region Descriptor; pointed to by the Command List Header, |
| 472 | * struct ahci_command. |
| 473 | */ |
| 474 | typedef struct PRD { |
| 475 | uint64_t dba; /* Data Base Address */ |
| 476 | uint32_t res; /* Reserved */ |
| 477 | uint32_t dbc; /* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */ |
| 478 | } __attribute__((__packed__)) PRD; |
| 479 | |
| 480 | /* Opaque, defined within ahci.c */ |
| 481 | typedef struct AHCICommand AHCICommand; |
| 482 | |
| 483 | /* Options to ahci_exec */ |
| 484 | typedef struct AHCIOpts { |
| 485 | size_t size; /* Size of transfer */ |
| 486 | unsigned prd_size; /* Size per-each PRD */ |
| 487 | bool set_bcl; /* Override the default BCL of ATAPI_SECTOR_SIZE */ |
| 488 | unsigned bcl; /* Byte Count Limit, for ATAPI PIO */ |
| 489 | uint64_t lba; /* Starting LBA offset */ |
| 490 | uint64_t buffer; /* Pointer to source or destination guest buffer */ |
| 491 | bool atapi; /* ATAPI command? */ |
| 492 | bool atapi_dma; /* Use DMA for ATAPI? */ |
| 493 | bool atapi_raw; /* READ CD returning 2352-byte raw sectors */ |
| 494 | bool error; |
| 495 | int (*pre_cb)(AHCIQState*, AHCICommand*, const struct AHCIOpts *); |
| 496 | int (*mid_cb)(AHCIQState*, AHCICommand*, const struct AHCIOpts *); |
| 497 | int (*post_cb)(AHCIQState*, AHCICommand*, const struct AHCIOpts *); |
| 498 | void *opaque; |
| 499 | } AHCIOpts; |
| 500 | |
| 501 | /*** Macro Utilities ***/ |
| 502 | #define BITANY(data, mask) (((data) & (mask)) != 0) |
| 503 | #define BITSET(data, mask) (((data) & (mask)) == (mask)) |
| 504 | #define BITCLR(data, mask) (((data) & (mask)) == 0) |
| 505 | #define ASSERT_BIT_SET(data, mask) g_assert_cmphex((data) & (mask), ==, (mask)) |
| 506 | #define ASSERT_BIT_CLEAR(data, mask) g_assert_cmphex((data) & (mask), ==, 0) |
| 507 | |
| 508 | /* For calculating how big the PRD table needs to be: */ |
| 509 | #define CMD_TBL_SIZ(n) ((0x80 + ((n) * sizeof(PRD)) + 0x7F) & ~0x7F) |
| 510 | |
| 511 | /* Helpers for reading/writing AHCI HBA register values */ |
| 512 | |
| 513 | static inline uint32_t ahci_mread(AHCIQState *ahci, size_t offset) |
| 514 | { |
| 515 | return qpci_io_readl(ahci->dev, ahci->hba_bar, offset); |
| 516 | } |
| 517 | |
| 518 | static inline void ahci_mwrite(AHCIQState *ahci, size_t offset, uint32_t value) |
| 519 | { |
| 520 | qpci_io_writel(ahci->dev, ahci->hba_bar, offset, value); |
| 521 | } |
| 522 | |
| 523 | static inline uint32_t ahci_rreg(AHCIQState *ahci, uint32_t reg_num) |
| 524 | { |
| 525 | return ahci_mread(ahci, 4 * reg_num); |
| 526 | } |
| 527 | |
| 528 | static inline void ahci_wreg(AHCIQState *ahci, uint32_t reg_num, uint32_t value) |
| 529 | { |
| 530 | ahci_mwrite(ahci, 4 * reg_num, value); |
| 531 | } |
| 532 | |
| 533 | static inline void ahci_set(AHCIQState *ahci, uint32_t reg_num, uint32_t mask) |
| 534 | { |
| 535 | ahci_wreg(ahci, reg_num, ahci_rreg(ahci, reg_num) | mask); |
| 536 | } |
| 537 | |
| 538 | static inline void ahci_clr(AHCIQState *ahci, uint32_t reg_num, uint32_t mask) |
| 539 | { |
| 540 | ahci_wreg(ahci, reg_num, ahci_rreg(ahci, reg_num) & ~mask); |
| 541 | } |
| 542 | |
| 543 | static inline size_t ahci_px_offset(uint8_t port, uint32_t reg_num) |
| 544 | { |
| 545 | return AHCI_PORTS + (HBA_PORT_NUM_REG * port) + reg_num; |
| 546 | } |
| 547 | |
| 548 | static inline uint32_t ahci_px_rreg(AHCIQState *ahci, uint8_t port, |
| 549 | uint32_t reg_num) |
| 550 | { |
| 551 | return ahci_rreg(ahci, ahci_px_offset(port, reg_num)); |
| 552 | } |
| 553 | |
| 554 | static inline void ahci_px_wreg(AHCIQState *ahci, uint8_t port, |
| 555 | uint32_t reg_num, uint32_t value) |
| 556 | { |
| 557 | ahci_wreg(ahci, ahci_px_offset(port, reg_num), value); |
| 558 | } |
| 559 | |
| 560 | static inline void ahci_px_set(AHCIQState *ahci, uint8_t port, |
| 561 | uint32_t reg_num, uint32_t mask) |
| 562 | { |
| 563 | ahci_px_wreg(ahci, port, reg_num, |
| 564 | ahci_px_rreg(ahci, port, reg_num) | mask); |
| 565 | } |
| 566 | |
| 567 | static inline void ahci_px_clr(AHCIQState *ahci, uint8_t port, |
| 568 | uint32_t reg_num, uint32_t mask) |
| 569 | { |
| 570 | ahci_px_wreg(ahci, port, reg_num, |
| 571 | ahci_px_rreg(ahci, port, reg_num) & ~mask); |
| 572 | } |
| 573 | |
| 574 | /*** Prototypes ***/ |
| 575 | uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes); |
| 576 | void ahci_free(AHCIQState *ahci, uint64_t addr); |
| 577 | void ahci_clean_mem(AHCIQState *ahci); |
| 578 | |
| 579 | /* Device management */ |
| 580 | QPCIDevice *get_ahci_device(QTestState *qts, uint32_t *fingerprint); |
| 581 | void free_ahci_device(QPCIDevice *dev); |
| 582 | void ahci_pci_enable(AHCIQState *ahci); |
| 583 | void start_ahci_device(AHCIQState *ahci); |
| 584 | void ahci_hba_enable(AHCIQState *ahci); |
| 585 | |
| 586 | /* Port Management */ |
| 587 | unsigned ahci_port_select(AHCIQState *ahci); |
| 588 | void ahci_port_clear(AHCIQState *ahci, uint8_t port); |
| 589 | |
| 590 | /* Command header / table management */ |
| 591 | unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t port); |
| 592 | void ahci_get_command_header(AHCIQState *ahci, uint8_t port, |
| 593 | uint8_t slot, AHCICommandHeader *cmd); |
| 594 | void ahci_set_command_header(AHCIQState *ahci, uint8_t port, |
| 595 | uint8_t slot, AHCICommandHeader *cmd); |
| 596 | void ahci_destroy_command(AHCIQState *ahci, uint8_t port, uint8_t slot); |
| 597 | |
| 598 | /* AHCI sanity check routines */ |
| 599 | void ahci_port_check_error(AHCIQState *ahci, AHCICommand *cmd); |
| 600 | void ahci_port_check_interrupts(AHCIQState *ahci, AHCICommand *cmd); |
| 601 | void ahci_port_check_nonbusy(AHCIQState *ahci, AHCICommand *cmd); |
| 602 | void ahci_port_check_d2h_sanity(AHCIQState *ahci, uint8_t port, uint8_t slot); |
| 603 | void ahci_port_check_pio_sanity(AHCIQState *ahci, AHCICommand *cmd); |
| 604 | void ahci_port_check_cmd_sanity(AHCIQState *ahci, AHCICommand *cmd); |
| 605 | |
| 606 | /* Misc */ |
| 607 | bool is_atapi(AHCIQState *ahci, uint8_t port); |
| 608 | |
| 609 | /* Command: Macro level execution */ |
| 610 | void ahci_guest_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd, |
| 611 | uint64_t gbuffer, size_t size, uint64_t sector); |
| 612 | AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd, |
| 613 | uint64_t gbuffer, size_t size, uint64_t sector); |
| 614 | void ahci_guest_io_resume(AHCIQState *ahci, AHCICommand *cmd); |
| 615 | void ahci_io(AHCIQState *ahci, uint8_t port, uint8_t ide_cmd, |
| 616 | void *buffer, size_t bufsize, uint64_t sector); |
| 617 | void ahci_exec(AHCIQState *ahci, uint8_t port, |
| 618 | uint8_t op, const AHCIOpts *opts); |
| 619 | void ahci_atapi_test_ready(AHCIQState *ahci, uint8_t port, bool ready, |
| 620 | uint8_t expected_sense); |
| 621 | void ahci_atapi_get_sense(AHCIQState *ahci, uint8_t port, |
| 622 | uint8_t *sense, uint8_t *asc); |
| 623 | void ahci_atapi_eject(AHCIQState *ahci, uint8_t port); |
| 624 | void ahci_atapi_load(AHCIQState *ahci, uint8_t port); |
| 625 | |
| 626 | /* Command: Fine-grained lifecycle */ |
| 627 | AHCICommand *ahci_command_create(uint8_t command_name); |
| 628 | AHCICommand *ahci_atapi_command_create(uint8_t scsi_cmd, uint16_t bcl, bool dma); |
| 629 | void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t port); |
| 630 | void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd); |
| 631 | void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd); |
| 632 | void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd); |
| 633 | void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd); |
| 634 | void ahci_command_free(AHCICommand *cmd); |
| 635 | |
| 636 | /* Command: adjustments */ |
| 637 | void ahci_command_set_flags(AHCICommand *cmd, uint16_t cmdh_flags); |
| 638 | void ahci_command_clr_flags(AHCICommand *cmd, uint16_t cmdh_flags); |
| 639 | void ahci_command_set_offset(AHCICommand *cmd, uint64_t lba_sect); |
| 640 | void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer); |
| 641 | void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes); |
| 642 | void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size); |
| 643 | void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes, |
| 644 | unsigned prd_size); |
| 645 | void ahci_command_set_count(AHCICommand *cmd, uint16_t count); |
| 646 | void ahci_command_expect_error(AHCICommand *cmd, uint8_t err); |
| 647 | void ahci_command_set_acmd(AHCICommand *cmd, void *acmd); |
| 648 | void ahci_command_enable_atapi_dma(AHCICommand *cmd); |
| 649 | void ahci_command_adjust(AHCICommand *cmd, uint64_t lba_sect, uint64_t gbuffer, |
| 650 | uint64_t xbytes, unsigned prd_size); |
| 651 | |
| 652 | /* Command: Misc */ |
| 653 | uint8_t ahci_command_slot(AHCICommand *cmd); |
| 654 | void ahci_write_fis(AHCIQState *ahci, AHCICommand *cmd); |
| 655 | |
| 656 | #endif |