| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _AHCI_PRIVATE_H |
| 31 |
#define _AHCI_PRIVATE_H |
| 32 |
|
| 33 |
#include <stdint.h> |
| 34 |
#include <storage/ata.h> |
| 35 |
#include <storage/atapi.h> |
| 36 |
|
| 37 |
typedef volatile struct { |
| 38 |
u64 cmdlist_base; |
| 39 |
u64 frameinfo_base; |
| 40 |
u32 intr_status; |
| 41 |
u32 intr_enable; |
| 42 |
u32 cmd_stat; |
| 43 |
u32 _reserved0; |
| 44 |
u32 taskfile_data; |
| 45 |
u32 signature; |
| 46 |
u32 sata_status; |
| 47 |
u32 sata_control; |
| 48 |
u32 sata_error; |
| 49 |
u32 sata_active; |
| 50 |
u32 cmd_issue; |
| 51 |
u32 sata_notify; |
| 52 |
u32 fis_based_switch; |
| 53 |
u32 _reserved1[11]; |
| 54 |
u32 _vendor[4]; |
| 55 |
} hba_port_t; |
| 56 |
|
| 57 |
#define HBA_PxIS_TFES (1 << 30) |
| 58 |
#define HBA_PxIS_HBFS (1 << 29) |
| 59 |
#define HBA_PxIS_HBDS (1 << 28) |
| 60 |
#define HBA_PxIS_IFS (1 << 27) |
| 61 |
#define HBA_PxIS_FATAL (HBA_PxIS_TFES | HBA_PxIS_HBFS | \ |
| 62 |
HBA_PxIS_HBDS | HBA_PxIS_IFS) |
| 63 |
#define HBA_PxIS_PCS (1 << 6) |
| 64 |
|
| 65 |
#define HBA_PxCMD_ICC_SHIFT 28 |
| 66 |
#define HBA_PxCMD_ICC_MASK (0xf << HBA_PxCMD_ICC_SHIFT) |
| 67 |
#define HBA_PxCMD_ICC_ACTIVE (0x1 << HBA_PxCMD_ICC_SHIFT) |
| 68 |
#define HBA_PxCMD_CR (1 << 15) |
| 69 |
#define HBA_PxCMD_FR (1 << 14) |
| 70 |
#define HBA_PxCMD_FRE (1 << 4) |
| 71 |
#define HBA_PxCMD_SUD (1 << 1) |
| 72 |
#define HBA_PxCMD_ST (1 << 0) |
| 73 |
|
| 74 |
#define HBA_PxTFD_BSY (1 << 7) |
| 75 |
#define HBA_PxTFD_DRQ (1 << 3) |
| 76 |
|
| 77 |
#define HBA_PxSSTS_IPM_SHIFT 8 |
| 78 |
#define HBA_PxSSTS_IPM_MASK (0xf << HBA_PxSSTS_IPM_SHIFT) |
| 79 |
#define HBA_PxSSTS_IPM_ACTIVE (1 << HBA_PxSSTS_IPM_SHIFT) |
| 80 |
#define HBA_PxSSTS_DET_SHIFT 0 |
| 81 |
#define HBA_PxSSTS_DET_MASK (0xf << HBA_PxSSTS_DET_SHIFT) |
| 82 |
#define HBA_PxSSTS_DET_ESTABLISHED (3 << HBA_PxSSTS_DET_SHIFT) |
| 83 |
|
| 84 |
#define HBA_PxSCTL_DET_SHIFT 0 |
| 85 |
#define HBA_PxSCTL_DET_MASK (0xf << HBA_PxSCTL_DET_SHIFT) |
| 86 |
#define HBA_PxSCTL_DET_COMRESET (0x1 << HBA_PxSCTL_DET_SHIFT) |
| 87 |
|
| 88 |
#define HBA_PxSIG_ATA 0x00000101 |
| 89 |
#define HBA_PxSIG_ATAPI 0xeb140101 |
| 90 |
#define HBA_PxSIG_SEMB 0xc33c0101 |
| 91 |
#define HBA_PxSIG_PM 0x96690101 |
| 92 |
|
| 93 |
typedef volatile struct { |
| 94 |
u32 caps; |
| 95 |
u32 global_ctrl; |
| 96 |
u32 intr_status; |
| 97 |
u32 ports_impl; |
| 98 |
u32 version; |
| 99 |
u32 ccc_ctrl; |
| 100 |
u32 ccc_ports; |
| 101 |
u32 em_location; |
| 102 |
u32 em_ctrl; |
| 103 |
u32 ext_caps; |
| 104 |
u32 handoff_ctrl_stat; |
| 105 |
u32 _reserved0[13]; |
| 106 |
u32 _reserved_nvmchi[16]; |
| 107 |
u32 _vendor[24]; |
| 108 |
hba_port_t ports[32]; |
| 109 |
} hba_ctrl_t; |
| 110 |
|
| 111 |
#define HBA_CAPS_SSS (1 << 27) |
| 112 |
#define HBA_CAPS_NCS_SHIFT 8 |
| 113 |
#define HBA_CAPS_NCS_MASK (0x1f << HBA_CAPS_NCS_SHIFT) |
| 114 |
|
| 115 |
#define HBA_CAPS_DECODE_NCS(caps) (((caps & HBA_CAPS_NCS_MASK) \ |
| 116 |
>> HBA_CAPS_NCS_SHIFT) \ |
| 117 |
+ 1) |
| 118 |
|
| 119 |
#define HBA_CTRL_AHCI_EN (1 << 31) |
| 120 |
#define HBA_CTRL_INTR_EN (1 << 1) |
| 121 |
#define HBA_CTRL_RESET (1 << 0) |
| 122 |
|
| 123 |
typedef volatile struct { |
| 124 |
u8 dma_setup_fis[28]; |
| 125 |
u8 _reserved0[4]; |
| 126 |
u8 pio_setup_fis[20]; |
| 127 |
u8 _reserved1[12]; |
| 128 |
u8 d2h_register_fis[20]; |
| 129 |
u8 _reserved2[4]; |
| 130 |
u8 set_device_bits_fis[8]; |
| 131 |
u8 unknown_fis[64]; |
| 132 |
u8 _reserved3[96]; |
| 133 |
} rcvd_fis_t; |
| 134 |
|
| 135 |
typedef volatile struct { |
| 136 |
u16 cmd; |
| 137 |
u16 prdt_length; |
| 138 |
u32 prd_bytes; |
| 139 |
u64 cmdtable_base; |
| 140 |
u8 _reserved[16]; |
| 141 |
} cmd_t; |
| 142 |
|
| 143 |
#define CMD_PMP_SHIFT 12 |
| 144 |
#define CMD_PMP_MASK (0xf << CMD_PMP_SHIFT) |
| 145 |
#define CMD_PMP(x) ((x << CMD_PMP_SHIFT) & CMD_PMP_MASK) |
| 146 |
#define CMD_CBuROK (1 << 10) |
| 147 |
#define CMD_BIST (1 << 9) |
| 148 |
#define CMD_RESET (1 << 8) |
| 149 |
#define CMD_PREFETCH (1 << 7) |
| 150 |
#define CMD_WRITE (1 << 6) |
| 151 |
#define CMD_ATAPI (1 << 5) |
| 152 |
#define CMD_CFL_SHIFT 0 |
| 153 |
#define CMD_CFL_MASK (0xf << CMD_CFL_SHIFT) |
| 154 |
#define CMD_CFL(x) ((((x) >> 2) << CMD_CFL_SHIFT) & CMD_CFL_MASK) |
| 155 |
|
| 156 |
typedef volatile struct { |
| 157 |
u8 fis[64]; |
| 158 |
u8 atapi_cmd[16]; |
| 159 |
u8 _reserved0[48]; |
| 160 |
struct { |
| 161 |
u64 data_base; |
| 162 |
u32 _reserved0; |
| 163 |
u32 flags; |
| 164 |
} prdt[8]; |
| 165 |
|
| 166 |
} cmdtable_t; |
| 167 |
|
| 168 |
#define BYTES_PER_PRD_SHIFT 20 |
| 169 |
#define BYTES_PER_PRD (4 << 20) |
| 170 |
|
| 171 |
enum { |
| 172 |
FIS_HOST_TO_DEVICE = 0x27, |
| 173 |
}; |
| 174 |
#define FIS_H2D_CMD (1 << 7) |
| 175 |
#define FIS_H2D_FIS_LEN 20 |
| 176 |
#define FIS_H2D_DEV_LBA (1 << 6) |
| 177 |
|
| 178 |
#define PRD_TABLE_I (1 << 31) |
| 179 |
#define PRD_TABLE_BYTES_MASK 0x3fffff |
| 180 |
#define PRD_TABLE_BYTES(x) (((x) - 1) & PRD_TABLE_BYTES_MASK) |
| 181 |
|
| 182 |
typedef struct { |
| 183 |
union { |
| 184 |
ata_dev_t ata_dev; |
| 185 |
atapi_dev_t atapi_dev; |
| 186 |
}; |
| 187 |
|
| 188 |
hba_ctrl_t *ctrl; |
| 189 |
hba_port_t *port; |
| 190 |
|
| 191 |
cmd_t *cmdlist; |
| 192 |
cmdtable_t *cmdtable; |
| 193 |
rcvd_fis_t *rcvd_fis; |
| 194 |
|
| 195 |
u8 *buf, *user_buf; |
| 196 |
int write_back; |
| 197 |
size_t buflen; |
| 198 |
} ahci_dev_t; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int ahci_cmdengine_start(hba_port_t *const port); |
| 204 |
|
| 205 |
int ahci_cmdengine_stop(hba_port_t *const port); |
| 206 |
|
| 207 |
ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev); |
| 208 |
|
| 209 |
size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, |
| 210 |
u8 *const user_buf, size_t buf_len, |
| 211 |
const int out); |
| 212 |
|
| 213 |
int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf); |
| 214 |
|
| 215 |
int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, |
| 221 |
const u8 *const cmd, const size_t cmdlen, |
| 222 |
u8 *const buf, const size_t buflen); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, |
| 228 |
const lba_t start, size_t count, |
| 229 |
u8 *const buf); |
| 230 |
|
| 231 |
#endif |