|
| 1 | +#ifndef __AHCI__ |
| 2 | +#define __AHCI__ |
| 3 | + |
| 4 | +#include "sata.hpp" |
| 5 | + |
| 6 | +// As per Serial ATA AHCI specification v1.3.1 |
| 7 | +// https://www.intel.com/content/www/us/en/io/serial-ata/serial-ata-ahci-spec-rev1-3-1.html |
| 8 | + |
| 9 | +/** |
| 10 | + * Generic Host Control structure of the Host Bus Adapter (HBA) |
| 11 | +*/ |
| 12 | +typedef struct { |
| 13 | + uint32_t clb; // Command List Base Address |
| 14 | + uint32_t clbu; // Command List Base Address Upper 32-Bit |
| 15 | + uint32_t fb; // FIS Base Address |
| 16 | + uint32_t fbu; // FIS Base Address Upper 32-Bits |
| 17 | + uint32_t is; // Interrupt Status |
| 18 | + uint32_t ie; // Interrupt Enable |
| 19 | + struct { |
| 20 | + uint8_t st: 1; // Start |
| 21 | + uint8_t sud: 1; // Spin-Up Device |
| 22 | + uint8_t pod: 1; // Power On Device |
| 23 | + uint8_t clo: 1; // Command List Override |
| 24 | + uint8_t fre: 1; // FIS Receive Enable |
| 25 | + uint8_t reserved0: 3; |
| 26 | + uint8_t ccs: 5; // Current Command Slot |
| 27 | + uint8_t mpss: 1; // Mechanical Presence Switch State |
| 28 | + uint8_t fr: 1; // FIS REceive Running |
| 29 | + uint8_t cr: 1; // Command List Running |
| 30 | + uint8_t cps: 1; // Cold Presence State |
| 31 | + uint8_t pma: 1; // Port Multiplier Attached |
| 32 | + uint8_t hpcp: 1; // Hot Plug Capable Port |
| 33 | + uint8_t mpsp: 1; // Mechanical Presence Switch Attached to Port |
| 34 | + uint8_t cpd: 1; // Cold Presence Detection |
| 35 | + uint8_t esp: 1; // External SATA Port |
| 36 | + uint8_t fbscp: 1; // FIS-based Switching Capable Port |
| 37 | + uint8_t apste: 1; // Automatic Partial to Slumber Transitions Enabled |
| 38 | + uint8_t atapi: 1; // Device is ATAPI |
| 39 | + uint8_t dlae: 1; // Drive LED on ATAPI Enable |
| 40 | + uint8_t alpe: 1; // Aggressive Link Power Management Enable |
| 41 | + uint8_t asp: 1; // Aggressive Slumber / Partial |
| 42 | + uint8_t icc: 4; // Interface Communication Control |
| 43 | + } __attribute__((packed)) cmd; // Command and Status |
| 44 | + uint32_t reserved0; |
| 45 | + uint32_t tfd; // Task File Data |
| 46 | + uint32_t sig; // Signature |
| 47 | + struct { |
| 48 | + uint8_t det: 4; // Device Detection |
| 49 | + uint8_t spd: 4; // Current Interface Speed |
| 50 | + uint8_t ipm: 4; // Interface Power Management |
| 51 | + uint32_t reserved0: 20; |
| 52 | + } __attribute__((packed)) ssts; // Serial ATA Status |
| 53 | + uint32_t sctl; // Serial ATA Control |
| 54 | + uint32_t serr; // Serial ATA Error |
| 55 | + uint32_t sact; // Serial ATA Active |
| 56 | + uint32_t ci; // Command Issue |
| 57 | + uint32_t sntf; // Serial ATA Notification |
| 58 | + uint32_t fbs; // FIS-based Switching Control |
| 59 | + uint32_t reserved1[11]; |
| 60 | + uint32_t vendor[4]; // Vendor Specific |
| 61 | +} __attribute__((packed)) g_ahci_hba_port; |
| 62 | + |
| 63 | +/** |
| 64 | + * HBAxPORT.SSTS.DET |
| 65 | + */ |
| 66 | +#define G_AHCI_HBA_PORT_SSTS_DET_NONE 0 // No device detected and Phy communication not established |
| 67 | +#define G_AHCI_HBA_PORT_SSTS_DET_PRESENT 1 // Device presence detected but Phy communication not established |
| 68 | +#define G_AHCI_HBA_PORT_SSTS_DET_READY 3 // Device presence detected and Phy communication established |
| 69 | +#define G_AHCI_HBA_PORT_SSTS_DET_OFFLINE 4 // Phy in offline mode as a result of the interface being disabled or running in a BIST loopback mode |
| 70 | + |
| 71 | + |
| 72 | +typedef struct { |
| 73 | + uint32_t cap; // Host Capabilities |
| 74 | + struct { |
| 75 | + uint32_t hbaReset: 1; // Bit 0 |
| 76 | + uint32_t interruptEnable: 1; // Bit 1 |
| 77 | + uint32_t reserved: 29; // Bits 2-30 |
| 78 | + uint32_t ahciEnable: 1; // Bit 31 |
| 79 | + } __attribute__((packed)) ghc; // Global Host Control |
| 80 | + uint32_t is; // Interrupt Status |
| 81 | + uint32_t pi; // Ports implemented |
| 82 | + uint32_t vs; // Version |
| 83 | + uint32_t cccCtl; // Command Completion Coalescing Control |
| 84 | + uint32_t cccPorts; // Command Completion Coalsecing Ports |
| 85 | + uint32_t emLoc; // Enclosure Management Location |
| 86 | + uint32_t emCtl; // Enclosure Management Control |
| 87 | + uint32_t cap2; // Host Capabilities Extended |
| 88 | + uint32_t bohc; // BIOS/OS Handoff Control and Status |
| 89 | +} __attribute__((packed)) g_ahci_hba_ghc; |
| 90 | + |
| 91 | +#define G_AHCI_HBA_PORT_OFFSET 0x100 |
| 92 | + |
| 93 | +/** |
| 94 | + * Command Header |
| 95 | + */ |
| 96 | +typedef struct { |
| 97 | + // DW0 |
| 98 | + uint8_t cfl: 5; // Command FIS Length |
| 99 | + uint8_t a: 1; // ATAPI |
| 100 | + uint8_t w: 1; // Write, 1: H2D, 0: D2H |
| 101 | + uint8_t p: 1; // Prefetchable |
| 102 | + |
| 103 | + uint8_t r: 1; // Reset |
| 104 | + uint8_t b: 1; // BIST |
| 105 | + uint8_t c: 1; // Clear Busy upon R_OK |
| 106 | + uint8_t reserved0: 1; |
| 107 | + uint8_t pmp: 4; // Port Multiplier Port |
| 108 | + |
| 109 | + uint16_t prdtl; // Physical Region Descriptor Table Length |
| 110 | + |
| 111 | + // DW1 |
| 112 | + uint32_t prdbc; // Physical Region Descriptor Byte Count |
| 113 | + |
| 114 | + // DW2 |
| 115 | + uint32_t ctba; // Command Table Descriptor Base Address |
| 116 | + |
| 117 | + // DW3 |
| 118 | + uint32_t ctbau; // Command Table Descriptor Base Address Upper 32-bits |
| 119 | + |
| 120 | + // DW4-7 |
| 121 | + uint32_t reserved1[4]; |
| 122 | +} __attribute__((packed)) g_hba_command_header; |
| 123 | + |
| 124 | +/** |
| 125 | + * Command Table |
| 126 | + */ |
| 127 | +typedef struct { |
| 128 | + uint8_t cfis[64]; |
| 129 | + uint8_t acmd[16]; |
| 130 | +} __attribute__((packed)) g_hba_command_table; |
| 131 | + |
| 132 | +#define G_HBA_COMMAND_TABLE_PRDT_OFFSET 0x80 |
| 133 | + |
| 134 | +/** |
| 135 | + * Physical Region Descriptor Table (PRDT) |
| 136 | + */ |
| 137 | +typedef struct { |
| 138 | + // DW0 |
| 139 | + uint32_t dba; // Data base address (lower 32 bits) |
| 140 | + |
| 141 | + // DW1 |
| 142 | + uint32_t dbau; // Data base address (upper 32 bits) |
| 143 | + |
| 144 | + // DW2 |
| 145 | + uint32_t reserved0; |
| 146 | + |
| 147 | + // DW3 |
| 148 | + uint32_t dbc: 22; // Data Byte Count |
| 149 | + uint32_t reserved: 9; |
| 150 | + uint32_t i: 1; // Interrupt on Completion |
| 151 | +} __attribute__((packed)) g_hba_prdt_entry; |
| 152 | + |
| 153 | +#define G_HBA_PRDT_MAX_ENTRIES 256 |
| 154 | + |
| 155 | + |
| 156 | +/** |
| 157 | + * Received FIS structure |
| 158 | + */ |
| 159 | +typedef struct { |
| 160 | + g_fis_dma_setup dsfis; // DMA Setup FIS |
| 161 | + uint8_t pad0[4]; |
| 162 | + |
| 163 | + g_fis_pio_setup_d2h psfis; // PIO Setup FIS |
| 164 | + uint8_t pad1[12]; |
| 165 | + |
| 166 | + g_fis_reg_d2h rfis; // D2H Register FIS |
| 167 | + uint8_t pad2[4]; |
| 168 | + |
| 169 | + g_fis_set_device_bits sdbfis; // Set Device Bits FIS |
| 170 | + |
| 171 | + uint8_t ufis[64]; // Unknown FIS |
| 172 | + |
| 173 | + uint8_t reserved0[0x100 - 0xA0]; |
| 174 | +} __attribute__((packed)) g_hba_fis; |
| 175 | + |
| 176 | +#endif |
0 commit comments