Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2012 secunet Security Networks AG |
Edward O'Callaghan | efc5841 | 2014-01-23 08:30:42 +1100 | [diff] [blame] | 5 | * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com> |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef _AHCI_PRIVATE_H |
| 32 | #define _AHCI_PRIVATE_H |
| 33 | |
| 34 | #include <stdint.h> |
| 35 | #include <storage/ata.h> |
| 36 | #include <storage/atapi.h> |
| 37 | |
| 38 | typedef volatile struct { |
| 39 | u64 cmdlist_base; |
| 40 | u64 frameinfo_base; |
| 41 | u32 intr_status; |
| 42 | u32 intr_enable; |
| 43 | u32 cmd_stat; |
| 44 | u32 _reserved0; |
| 45 | u32 taskfile_data; |
| 46 | u32 signature; |
| 47 | u32 sata_status; |
| 48 | u32 sata_control; |
| 49 | u32 sata_error; |
| 50 | u32 sata_active; |
| 51 | u32 cmd_issue; |
| 52 | u32 sata_notify; |
| 53 | u32 fis_based_switch; |
| 54 | u32 _reserved1[11]; |
| 55 | u32 _vendor[4]; |
| 56 | } hba_port_t; |
| 57 | |
| 58 | #define HBA_PxIS_TFES (1 << 30) /* TFES - Task File Error Status */ |
| 59 | #define HBA_PxIS_HBFS (1 << 29) /* HBFS - Host Bus Fatal Error Status */ |
| 60 | #define HBA_PxIS_HBDS (1 << 28) /* HBDS - Host Bus Data Error Status */ |
| 61 | #define HBA_PxIS_IFS (1 << 27) /* IFS - Interface Fatal Error Status */ |
| 62 | #define HBA_PxIS_FATAL (HBA_PxIS_TFES | HBA_PxIS_HBFS | \ |
| 63 | HBA_PxIS_HBDS | HBA_PxIS_IFS) |
| 64 | #define HBA_PxIS_PCS (1 << 6) /* PCS - Port Connect Change Status */ |
| 65 | |
| 66 | #define HBA_PxCMD_ICC_SHIFT 28 |
| 67 | #define HBA_PxCMD_ICC_MASK (0xf << HBA_PxCMD_ICC_SHIFT) |
| 68 | #define HBA_PxCMD_ICC_ACTIVE (0x1 << HBA_PxCMD_ICC_SHIFT) |
| 69 | #define HBA_PxCMD_CR (1 << 15) /* CR - Command list Running */ |
| 70 | #define HBA_PxCMD_FR (1 << 14) /* FR - FIS receive Running */ |
| 71 | #define HBA_PxCMD_FRE (1 << 4) /* FRE - FIS Receive Enable */ |
| 72 | #define HBA_PxCMD_SUD (1 << 1) /* SUD - Spin-Up Device */ |
| 73 | #define HBA_PxCMD_ST (1 << 0) /* ST - Start (command processing) */ |
| 74 | |
| 75 | #define HBA_PxTFD_BSY (1 << 7) |
| 76 | #define HBA_PxTFD_DRQ (1 << 3) |
| 77 | |
| 78 | #define HBA_PxSSTS_IPM_SHIFT 8 |
| 79 | #define HBA_PxSSTS_IPM_MASK (0xf << HBA_PxSSTS_IPM_SHIFT) |
| 80 | #define HBA_PxSSTS_IPM_ACTIVE (1 << HBA_PxSSTS_IPM_SHIFT) |
| 81 | #define HBA_PxSSTS_DET_SHIFT 0 |
| 82 | #define HBA_PxSSTS_DET_MASK (0xf << HBA_PxSSTS_DET_SHIFT) |
| 83 | #define HBA_PxSSTS_DET_ESTABLISHED (3 << HBA_PxSSTS_DET_SHIFT) |
| 84 | |
| 85 | #define HBA_PxSCTL_DET_SHIFT 0 |
| 86 | #define HBA_PxSCTL_DET_MASK (0xf << HBA_PxSCTL_DET_SHIFT) |
| 87 | #define HBA_PxSCTL_DET_COMRESET (0x1 << HBA_PxSCTL_DET_SHIFT) |
| 88 | |
| 89 | #define HBA_PxSIG_ATA 0x00000101 /* SATA drive */ |
| 90 | #define HBA_PxSIG_ATAPI 0xeb140101 /* SATAPI drive */ |
| 91 | #define HBA_PxSIG_SEMB 0xc33c0101 /* Enclosure management bridge */ |
| 92 | #define HBA_PxSIG_PM 0x96690101 /* Port multiplier */ |
| 93 | |
| 94 | typedef volatile struct { |
| 95 | u32 caps; |
| 96 | u32 global_ctrl; |
| 97 | u32 intr_status; |
| 98 | u32 ports_impl; |
| 99 | u32 version; |
| 100 | u32 ccc_ctrl; /* CCC - Command Completion Coalescing */ |
| 101 | u32 ccc_ports; |
| 102 | u32 em_location; /* EM - Enclosure Management */ |
| 103 | u32 em_ctrl; |
| 104 | u32 ext_caps; |
| 105 | u32 handoff_ctrl_stat; |
| 106 | u32 _reserved0[13]; |
| 107 | u32 _reserved_nvmchi[16]; |
| 108 | u32 _vendor[24]; |
| 109 | hba_port_t ports[32]; |
| 110 | } hba_ctrl_t; |
| 111 | |
| 112 | #define HBA_CAPS_SSS (1 << 27) /* SSS - Supports Staggered Spin-up */ |
| 113 | #define HBA_CAPS_NCS_SHIFT 8 /* NCS - Number of Command Slots */ |
| 114 | #define HBA_CAPS_NCS_MASK (0x1f << HBA_CAPS_NCS_SHIFT) |
| 115 | |
| 116 | #define HBA_CAPS_DECODE_NCS(caps) (((caps & HBA_CAPS_NCS_MASK) \ |
| 117 | >> HBA_CAPS_NCS_SHIFT) \ |
| 118 | + 1) |
| 119 | |
| 120 | #define HBA_CTRL_AHCI_EN (1 << 31) |
| 121 | #define HBA_CTRL_INTR_EN (1 << 1) |
| 122 | #define HBA_CTRL_RESET (1 << 0) |
| 123 | |
| 124 | |
| 125 | typedef volatile struct { |
| 126 | u8 dma_setup_fis[28]; |
| 127 | u8 _reserved0[4]; |
| 128 | u8 pio_setup_fis[20]; |
| 129 | u8 _reserved1[12]; |
| 130 | u8 d2h_register_fis[20]; |
| 131 | u8 _reserved2[4]; |
| 132 | u8 set_device_bits_fis[8]; |
| 133 | u8 unknown_fis[64]; |
| 134 | u8 _reserved3[96]; |
| 135 | } rcvd_fis_t; |
| 136 | |
| 137 | typedef volatile struct { |
| 138 | u16 cmd; |
| 139 | u16 prdt_length; |
| 140 | u32 prd_bytes; |
| 141 | u64 cmdtable_base; |
| 142 | u8 _reserved[16]; |
| 143 | } cmd_t; |
| 144 | |
| 145 | #define CMD_PMP_SHIFT 12 /* PMP - Port Multiplier Port */ |
| 146 | #define CMD_PMP_MASK (0xf << CMD_PMP_SHIFT) |
| 147 | #define CMD_PMP(x) ((x << CMD_PMP_SHIFT) & CMD_PMP_MASK) |
| 148 | #define CMD_CBuROK (1 << 10) /* C - Clear Busy upon R_OK */ |
| 149 | #define CMD_BIST (1 << 9) /* B - BIST - Built-In Selft Test */ |
| 150 | #define CMD_RESET (1 << 8) /* R - Reset */ |
| 151 | #define CMD_PREFETCH (1 << 7) /* P - Prefetch (PRDTs or ATAPI cmd) */ |
| 152 | #define CMD_WRITE (1 << 6) /* W - Write (host to device) */ |
| 153 | #define CMD_ATAPI (1 << 5) /* A - ATAPI cmd */ |
| 154 | #define CMD_CFL_SHIFT 0 /* CFL - Command FIS Length */ |
| 155 | #define CMD_CFL_MASK (0xf << CMD_CFL_SHIFT) |
| 156 | #define CMD_CFL(x) ((((x) >> 2) << CMD_CFL_SHIFT) & CMD_CFL_MASK) |
| 157 | |
| 158 | typedef volatile struct { |
| 159 | u8 fis[64]; |
| 160 | u8 atapi_cmd[16]; |
| 161 | u8 _reserved0[48]; |
| 162 | struct { |
| 163 | u64 data_base; |
| 164 | u32 _reserved0; |
| 165 | u32 flags; |
| 166 | } prdt[8]; /* Can be up to 65,535 prds, |
| 167 | but implementation needs multiple of 128 bytes. */ |
| 168 | } cmdtable_t; |
| 169 | |
| 170 | #define BYTES_PER_PRD_SHIFT 20 |
| 171 | #define BYTES_PER_PRD (4 << 20) |
| 172 | |
| 173 | enum { |
| 174 | FIS_HOST_TO_DEVICE = 0x27, |
| 175 | }; |
| 176 | #define FIS_H2D_CMD (1 << 7) |
| 177 | #define FIS_H2D_FIS_LEN 20 |
| 178 | #define FIS_H2D_DEV_LBA (1 << 6) |
| 179 | |
| 180 | #define PRD_TABLE_I (1 << 31) /* I - Interrupt on Completion */ |
| 181 | #define PRD_TABLE_BYTES_MASK 0x3fffff |
| 182 | #define PRD_TABLE_BYTES(x) (((x) - 1) & PRD_TABLE_BYTES_MASK) |
| 183 | |
| 184 | typedef struct { |
| 185 | union { |
| 186 | ata_dev_t ata_dev; |
| 187 | atapi_dev_t atapi_dev; |
| 188 | }; |
| 189 | |
| 190 | hba_ctrl_t *ctrl; |
| 191 | hba_port_t *port; |
| 192 | |
| 193 | cmd_t *cmdlist; |
| 194 | cmdtable_t *cmdtable; |
| 195 | rcvd_fis_t *rcvd_fis; |
| 196 | |
| 197 | u8 *buf, *user_buf; |
| 198 | int write_back; |
| 199 | size_t buflen; |
| 200 | } ahci_dev_t; |
| 201 | |
Edward O'Callaghan | efc5841 | 2014-01-23 08:30:42 +1100 | [diff] [blame] | 202 | /* |
| 203 | * ahci_common.c |
| 204 | */ |
| 205 | int ahci_cmdengine_start(hba_port_t *const port); |
| 206 | |
| 207 | int ahci_cmdengine_stop(hba_port_t *const port); |
| 208 | |
| 209 | ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev); |
| 210 | |
| 211 | size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, |
| 212 | u8 *const user_buf, size_t buf_len, |
| 213 | const int out); |
| 214 | |
| 215 | int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf); |
| 216 | |
| 217 | int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status); |
| 218 | |
| 219 | /* |
| 220 | * ahci_atapi.c |
| 221 | */ |
| 222 | ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, |
| 223 | const u8 *const cmd, const size_t cmdlen, |
| 224 | u8 *const buf, const size_t buflen); |
| 225 | |
| 226 | /* |
| 227 | * ahci_ata.c |
| 228 | */ |
| 229 | ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, |
| 230 | const lba_t start, size_t count, |
| 231 | u8 *const buf); |
| 232 | |
| 233 | |
| 234 | #endif /* _AHCI_PRIVATE_H */ |