| 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 |
|
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <string.h> |
| 34 |
#include <libpayload.h> |
| 35 |
#include <pci.h> |
| 36 |
#include <pci/pci.h> |
| 37 |
#include <storage/ata.h> |
| 38 |
#include <storage/ahci.h> |
| 39 |
|
| 40 |
#include "ahci_private.h" |
| 41 |
|
| 42 |
#ifdef DEBUG_STATUS |
| 43 |
static inline u32 _ahci_clear_status(volatile u32 *const reg, |
| 44 |
const char *const r, |
| 45 |
const char *const f) |
| 46 |
{ |
| 47 |
const u32 bits = *reg; |
| 48 |
if (bits) |
| 49 |
*reg = bits; |
| 50 |
printf("ahci: %s: %s == 0x%08x\n", f, r, bits); |
| 51 |
|
| 52 |
return bits; |
| 53 |
} |
| 54 |
#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__) |
| 55 |
#else |
| 56 |
static inline u32 _ahci_clear_status(volatile u32 *const reg) |
| 57 |
{ |
| 58 |
const u32 bits = *reg; |
| 59 |
if (bits) |
| 60 |
*reg = bits; |
| 61 |
return bits; |
| 62 |
} |
| 63 |
#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r) |
| 64 |
#endif |
| 65 |
|
| 66 |
static inline int ahci_port_is_active(const hba_port_t *const port) |
| 67 |
{ |
| 68 |
return (port->sata_status & (HBA_PxSSTS_IPM_MASK | HBA_PxSSTS_DET_MASK)) |
| 69 |
== (HBA_PxSSTS_IPM_ACTIVE | HBA_PxSSTS_DET_ESTABLISHED); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status) |
| 74 |
{ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
dev->port->cmd_stat &= ~HBA_PxCMD_ST; |
| 79 |
|
| 80 |
|
| 81 |
ahci_clear_status(dev->port, sata_error); |
| 82 |
|
| 83 |
|
| 84 |
const u32 tfd = dev->port->taskfile_data; |
| 85 |
if ((tfd & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)) | |
| 86 |
(intr_status & HBA_PxIS_PCS)) { |
| 87 |
const u32 sctl = dev->port->sata_control & ~HBA_PxSCTL_DET_MASK; |
| 88 |
dev->port->sata_control = sctl | HBA_PxSCTL_DET_COMRESET; |
| 89 |
mdelay(1); |
| 90 |
dev->port->sata_control = sctl; |
| 91 |
} |
| 92 |
|
| 93 |
if (ahci_port_is_active(dev->port)) |
| 94 |
|
| 95 |
return ahci_cmdengine_start(dev->port); |
| 96 |
else |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
static int ahci_dev_init(hba_ctrl_t *const ctrl, |
| 101 |
hba_port_t *const port, |
| 102 |
const int portnum) |
| 103 |
{ |
| 104 |
int ret = 1; |
| 105 |
|
| 106 |
const int ncs = HBA_CAPS_DECODE_NCS(ctrl->caps); |
| 107 |
|
| 108 |
if (ahci_cmdengine_stop(port)) |
| 109 |
return 1; |
| 110 |
|
| 111 |
|
| 112 |
cmd_t *const cmdlist = memalign(1024, ncs * sizeof(cmd_t)); |
| 113 |
cmdtable_t *const cmdtable = memalign(128, sizeof(cmdtable_t)); |
| 114 |
rcvd_fis_t *const rcvd_fis = memalign(256, sizeof(rcvd_fis_t)); |
| 115 |
|
| 116 |
ahci_dev_t *const dev = calloc(1, sizeof(ahci_dev_t)); |
| 117 |
if (!cmdlist || !cmdtable || !rcvd_fis || !dev) |
| 118 |
goto _cleanup_ret; |
| 119 |
memset((void *)cmdlist, '\0', ncs * sizeof(cmd_t)); |
| 120 |
memset((void *)cmdtable, '\0', sizeof(*cmdtable)); |
| 121 |
memset((void *)rcvd_fis, '\0', sizeof(*rcvd_fis)); |
| 122 |
|
| 123 |
|
| 124 |
port->cmdlist_base = virt_to_phys(cmdlist); |
| 125 |
port->frameinfo_base = virt_to_phys(rcvd_fis); |
| 126 |
if (ahci_cmdengine_start(port)) |
| 127 |
goto _cleanup_ret; |
| 128 |
|
| 129 |
port->cmd_stat |= HBA_PxCMD_ICC_ACTIVE; |
| 130 |
|
| 131 |
dev->ctrl = ctrl; |
| 132 |
dev->port = port; |
| 133 |
dev->cmdlist = cmdlist; |
| 134 |
dev->cmdtable = cmdtable; |
| 135 |
dev->rcvd_fis = rcvd_fis; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
const int timeout_s = 30; |
| 142 |
int timeout = timeout_s * 100; |
| 143 |
while ((port->taskfile_data & HBA_PxTFD_BSY) && timeout--) |
| 144 |
mdelay(10); |
| 145 |
|
| 146 |
if (port->taskfile_data & HBA_PxTFD_BSY) |
| 147 |
printf("ahci: Timed out after %d seconds " |
| 148 |
"of waiting for device to spin up.\n", timeout_s); |
| 149 |
|
| 150 |
|
| 151 |
switch (port->signature) { |
| 152 |
case HBA_PxSIG_ATA: |
| 153 |
printf("ahci: ATA drive on port #%d.\n", portnum); |
| 154 |
#if CONFIG(LP_STORAGE_ATA) |
| 155 |
dev->ata_dev.identify = ahci_identify_device; |
| 156 |
dev->ata_dev.read_sectors = ahci_ata_read_sectors; |
| 157 |
return ata_attach_device(&dev->ata_dev, PORT_TYPE_SATA); |
| 158 |
#endif |
| 159 |
break; |
| 160 |
case HBA_PxSIG_ATAPI: |
| 161 |
printf("ahci: ATAPI drive on port #%d.\n", portnum); |
| 162 |
#if CONFIG(LP_STORAGE_ATAPI) |
| 163 |
dev->atapi_dev.identify = ahci_identify_device; |
| 164 |
dev->atapi_dev.packet_read_cmd = ahci_packet_read_cmd; |
| 165 |
return atapi_attach_device(&dev->atapi_dev, PORT_TYPE_SATA); |
| 166 |
#endif |
| 167 |
break; |
| 168 |
default: |
| 169 |
printf("ahci: Unsupported device (signature == 0x%08x) " |
| 170 |
"on port #%d.\n", port->signature, portnum); |
| 171 |
break; |
| 172 |
} |
| 173 |
ret = 2; |
| 174 |
|
| 175 |
_cleanup_ret: |
| 176 |
|
| 177 |
if (dev) |
| 178 |
free(dev); |
| 179 |
|
| 180 |
|
| 181 |
if (!ahci_cmdengine_stop(port)) { |
| 182 |
port->cmdlist_base = 0; |
| 183 |
port->frameinfo_base = 0; |
| 184 |
if (rcvd_fis) |
| 185 |
free((void *)rcvd_fis); |
| 186 |
if (cmdtable) |
| 187 |
free((void *)cmdtable); |
| 188 |
if (cmdlist) |
| 189 |
free((void *)cmdlist); |
| 190 |
} |
| 191 |
return ret; |
| 192 |
} |
| 193 |
|
| 194 |
static void ahci_port_probe(hba_ctrl_t *const ctrl, |
| 195 |
hba_port_t *const port, |
| 196 |
const int portnum) |
| 197 |
{ |
| 198 |
|
| 199 |
if (ctrl->caps & HBA_CAPS_SSS) { |
| 200 |
port->cmd_stat |= HBA_PxCMD_SUD; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if ((ctrl->caps & HBA_CAPS_SSS) || |
| 206 |
!(ctrl->ports_impl & ((1 << (portnum - 1)) - 1))) { |
| 207 |
|
| 208 |
int timeout = 100; |
| 209 |
while (!ahci_port_is_active(port) && timeout--) |
| 210 |
udelay(100); |
| 211 |
} |
| 212 |
if (!ahci_port_is_active(port)) |
| 213 |
return; |
| 214 |
|
| 215 |
ahci_clear_status(port, sata_error); |
| 216 |
ahci_clear_status(port, intr_status); |
| 217 |
|
| 218 |
ahci_dev_init(ctrl, port, portnum); |
| 219 |
} |
| 220 |
|
| 221 |
#if CONFIG(LP_STORAGE_AHCI_ONLY_TESTED) |
| 222 |
static u32 working_controllers[] = { |
| 223 |
0x8086 | 0x2929 << 16, |
| 224 |
0x8086 | 0x1c03 << 16, |
| 225 |
0x8086 | 0x1e03 << 16, |
| 226 |
0x8086 | 0xa102 << 16, |
| 227 |
0x8086 | 0xa353 << 16, |
| 228 |
0x8086 | 0x5ae3 << 16, |
| 229 |
}; |
| 230 |
#endif |
| 231 |
|
| 232 |
void ahci_initialize(struct pci_dev *dev) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
|
| 236 |
#if CONFIG(LP_STORAGE_AHCI_ONLY_TESTED) |
| 237 |
const u32 vendor_device = dev->vendor_id | dev->device_id << 16; |
| 238 |
for (i = 0; i < ARRAY_SIZE(working_controllers); ++i) |
| 239 |
if (vendor_device == working_controllers[i]) |
| 240 |
break; |
| 241 |
if (i == ARRAY_SIZE(working_controllers)) { |
| 242 |
printf("ahci: Not using untested SATA controller " |
| 243 |
"%02x:%02x.%02x (%04x:%04x).\n", dev->bus, |
| 244 |
dev->dev, dev->func, dev->vendor_id, dev->device_id); |
| 245 |
return; |
| 246 |
} |
| 247 |
#endif |
| 248 |
|
| 249 |
printf("ahci: Found SATA controller %02x:%02x.%02x (%04x:%04x).\n", |
| 250 |
dev->bus, dev->dev, dev->func, dev->vendor_id, dev->device_id); |
| 251 |
|
| 252 |
hba_ctrl_t *const ctrl = phys_to_virt(pci_read_long(dev, 0x24) & ~0x3ff); |
| 253 |
hba_port_t *const ports = ctrl->ports; |
| 254 |
|
| 255 |
|
| 256 |
ctrl->global_ctrl |= HBA_CTRL_RESET; |
| 257 |
|
| 258 |
int timeout = 10 * 1000; |
| 259 |
while (ctrl->global_ctrl & HBA_CTRL_RESET && timeout--) |
| 260 |
udelay(100); |
| 261 |
if (ctrl->global_ctrl & HBA_CTRL_RESET) { |
| 262 |
printf("ahci: ERROR: " |
| 263 |
"Controller reset didn't finish within 1s.\n"); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
ctrl->global_ctrl |= HBA_CTRL_AHCI_EN; |
| 269 |
|
| 270 |
|
| 271 |
const u16 command = pci_read_word(dev, PCI_COMMAND); |
| 272 |
pci_write_word(dev, PCI_COMMAND, command | PCI_COMMAND_MASTER); |
| 273 |
|
| 274 |
|
| 275 |
for (i = 0; i < 32; ++i) { |
| 276 |
if (ctrl->ports_impl & (1 << i)) |
| 277 |
ahci_port_probe(ctrl, &ports[i], i + 1); |
| 278 |
} |
| 279 |
} |