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 |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | //#define DEBUG_STATUS |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <stdint.h> |
| 34 | #include <string.h> |
| 35 | #include <libpayload.h> |
| 36 | #include <pci.h> |
| 37 | #include <storage/ata.h> |
| 38 | #include <storage/ahci.h> |
| 39 | |
| 40 | #include "ahci_private.h" |
| 41 | |
| 42 | |
| 43 | #ifdef DEBUG_STATUS |
| 44 | static inline u32 _ahci_clear_status(volatile u32 *const reg, |
| 45 | const char *const r, |
| 46 | const char *const f) |
| 47 | { |
| 48 | const u32 bits = *reg; |
| 49 | if (bits) |
| 50 | *reg = bits; |
| 51 | printf("ahci: %s: %s == 0x%08x\n", f, r, bits); |
| 52 | |
| 53 | return bits; |
| 54 | } |
| 55 | #define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__) |
| 56 | #else |
| 57 | static inline u32 _ahci_clear_status(volatile u32 *const reg) |
| 58 | { |
| 59 | const u32 bits = *reg; |
| 60 | if (bits) |
| 61 | *reg = bits; |
| 62 | return bits; |
| 63 | } |
| 64 | #define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r) |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | static inline int ahci_port_is_active(const hba_port_t *const port) |
| 69 | { |
| 70 | return (port->sata_status & (HBA_PxSSTS_IPM_MASK | HBA_PxSSTS_DET_MASK)) |
| 71 | == (HBA_PxSSTS_IPM_ACTIVE | HBA_PxSSTS_DET_ESTABLISHED); |
| 72 | } |
| 73 | |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 74 | /** Do minimal error recovery. */ |
Edward O'Callaghan | efc5841 | 2014-01-23 08:30:42 +1100 | [diff] [blame^] | 75 | int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status) |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 76 | { |
| 77 | /* Command engine has to be restarted. |
| 78 | We don't call ahci_cmdengine_stop() here as it also checks |
| 79 | HBA_PxCMD_FR which won't clear on fatal errors. */ |
| 80 | dev->port->cmd_stat &= ~HBA_PxCMD_ST; |
| 81 | |
| 82 | /* Always clear sata_error. */ |
| 83 | ahci_clear_status(dev->port, sata_error); |
| 84 | |
| 85 | /* Perform COMRESET if appropriate. */ |
| 86 | const u32 tfd = dev->port->taskfile_data; |
| 87 | if ((tfd & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)) | |
| 88 | (intr_status & HBA_PxIS_PCS)) { |
| 89 | const u32 sctl = dev->port->sata_control & ~HBA_PxSCTL_DET_MASK; |
| 90 | dev->port->sata_control = sctl | HBA_PxSCTL_DET_COMRESET; |
| 91 | mdelay(1); |
| 92 | dev->port->sata_control = sctl; |
| 93 | } |
| 94 | |
| 95 | if (ahci_port_is_active(dev->port)) |
| 96 | /* Start command engine. */ |
| 97 | return ahci_cmdengine_start(dev->port); |
| 98 | else |
| 99 | return -1; |
| 100 | } |
| 101 | |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 102 | static int ahci_dev_init(hba_ctrl_t *const ctrl, |
| 103 | hba_port_t *const port, |
| 104 | const int portnum) |
| 105 | { |
| 106 | int ret = 1; |
| 107 | |
| 108 | const int ncs = HBA_CAPS_DECODE_NCS(ctrl->caps); |
| 109 | |
| 110 | /* Allocate command list, one command table and received FIS. */ |
| 111 | cmd_t *const cmdlist = memalign(1024, ncs * sizeof(cmd_t)); |
| 112 | cmdtable_t *const cmdtable = memalign(128, sizeof(cmdtable_t)); |
| 113 | rcvd_fis_t *const rcvd_fis = memalign(256, sizeof(rcvd_fis_t)); |
| 114 | /* Allocate our device structure. */ |
| 115 | ahci_dev_t *const dev = calloc(1, sizeof(ahci_dev_t)); |
| 116 | if (!cmdlist || !cmdtable || !rcvd_fis || !dev) |
| 117 | goto _cleanup_ret; |
| 118 | memset((void *)cmdlist, '\0', ncs * sizeof(cmd_t)); |
| 119 | memset((void *)cmdtable, '\0', sizeof(*cmdtable)); |
| 120 | memset((void *)rcvd_fis, '\0', sizeof(*rcvd_fis)); |
| 121 | |
| 122 | /* Set command list base and received FIS base. */ |
| 123 | if (ahci_cmdengine_stop(port)) |
| 124 | return 1; |
| 125 | port->cmdlist_base = virt_to_phys(cmdlist); |
| 126 | port->frameinfo_base = virt_to_phys(rcvd_fis); |
| 127 | if (ahci_cmdengine_start(port)) |
| 128 | return 1; |
| 129 | /* Put port into active state. */ |
| 130 | port->cmd_stat |= HBA_PxCMD_ICC_ACTIVE; |
| 131 | |
| 132 | dev->ctrl = ctrl; |
| 133 | dev->port = port; |
| 134 | dev->cmdlist = cmdlist; |
| 135 | dev->cmdtable = cmdtable; |
| 136 | dev->rcvd_fis = rcvd_fis; |
| 137 | |
Nico Huber | 354066e | 2013-06-17 17:42:35 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Wait for D2H Register FIS with device' signature. |
| 140 | * The drive has to spin up here, so wait up to 30s. |
| 141 | */ |
| 142 | const int timeout_s = 30; /* Time out after 30s. */ |
| 143 | int timeout = timeout_s * 100; |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 144 | while ((port->taskfile_data & HBA_PxTFD_BSY) && timeout--) |
| 145 | mdelay(10); |
| 146 | |
Nico Huber | 354066e | 2013-06-17 17:42:35 +0200 | [diff] [blame] | 147 | if (port->taskfile_data & HBA_PxTFD_BSY) |
| 148 | printf("ahci: Timed out after %d seconds " |
| 149 | "of waiting for device to spin up.\n", timeout_s); |
| 150 | |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 151 | /* Initialize device or fall through to clean up. */ |
| 152 | switch (port->signature) { |
| 153 | case HBA_PxSIG_ATA: |
| 154 | printf("ahci: ATA drive on port #%d.\n", portnum); |
| 155 | #ifdef CONFIG_STORAGE_ATA |
| 156 | dev->ata_dev.identify = ahci_identify_device; |
| 157 | dev->ata_dev.read_sectors = ahci_ata_read_sectors; |
| 158 | return ata_attach_device(&dev->ata_dev, PORT_TYPE_SATA); |
| 159 | #endif |
| 160 | break; |
| 161 | case HBA_PxSIG_ATAPI: |
| 162 | printf("ahci: ATAPI drive on port #%d.\n", portnum); |
| 163 | #ifdef CONFIG_STORAGE_ATAPI |
| 164 | dev->atapi_dev.identify = ahci_identify_device; |
| 165 | dev->atapi_dev.packet_read_cmd = ahci_packet_read_cmd; |
| 166 | return atapi_attach_device(&dev->atapi_dev, PORT_TYPE_SATA); |
| 167 | #endif |
| 168 | break; |
| 169 | default: |
| 170 | printf("ahci: Unsupported device (signature == 0x%08x) " |
| 171 | "on port #%d.\n", port->signature, portnum); |
| 172 | break; |
| 173 | } |
| 174 | ret = 2; |
| 175 | |
| 176 | _cleanup_ret: |
| 177 | /* Clean up (not reached for initialized devices). */ |
| 178 | if (dev) |
| 179 | free(dev); |
| 180 | if (!ahci_cmdengine_stop(port)) { |
| 181 | port->cmdlist_base = 0; |
| 182 | port->frameinfo_base = 0; |
| 183 | if (rcvd_fis) |
| 184 | free((void *)rcvd_fis); |
| 185 | if (cmdtable) |
| 186 | free((void *)cmdtable); |
| 187 | if (cmdlist) |
| 188 | free((void *)cmdlist); |
| 189 | } |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static void ahci_port_probe(hba_ctrl_t *const ctrl, |
| 194 | hba_port_t *const port, |
| 195 | const int portnum) |
| 196 | { |
| 197 | /* If staggered spin-up is supported, spin-up device. */ |
| 198 | if (ctrl->caps & HBA_CAPS_SSS) { |
| 199 | port->cmd_stat |= HBA_PxCMD_SUD; |
| 200 | } |
| 201 | |
| 202 | /* Wait 1s if we just told the device to spin up or |
| 203 | if it's the first port. */ |
| 204 | if ((ctrl->caps & HBA_CAPS_SSS) || |
| 205 | !(ctrl->ports_impl & ((1 << (portnum - 1)) - 1))) { |
| 206 | /* Wait for port to become active. */ |
| 207 | int timeout = 100; /* Time out after 100 * 100us == 10ms. */ |
| 208 | while (!ahci_port_is_active(port) && timeout--) |
| 209 | udelay(100); |
| 210 | } |
| 211 | if (!ahci_port_is_active(port)) |
| 212 | return; |
| 213 | |
| 214 | ahci_clear_status(port, sata_error); |
| 215 | ahci_clear_status(port, intr_status); |
| 216 | |
| 217 | ahci_dev_init(ctrl, port, portnum); |
| 218 | } |
| 219 | |
| 220 | #ifdef CONFIG_STORAGE_AHCI_ONLY_TESTED |
| 221 | static u32 working_controllers[] = { |
Nico Huber | 5d1edf6 | 2013-05-21 12:26:47 +0200 | [diff] [blame] | 222 | 0x8086 | 0x2929 << 16, /* Mobile ICH9 */ |
| 223 | 0x8086 | 0x1e03 << 16, /* Mobile Panther Point PCH */ |
Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 224 | }; |
| 225 | #endif |
| 226 | static void ahci_init_pci(pcidev_t dev) |
| 227 | { |
| 228 | int i; |
| 229 | |
| 230 | const u16 class = pci_read_config16(dev, 0xa); |
| 231 | if (class != 0x0106) |
| 232 | return; |
| 233 | const u16 vendor = pci_read_config16(dev, 0x00); |
| 234 | const u16 device = pci_read_config16(dev, 0x02); |
| 235 | |
| 236 | #ifdef CONFIG_STORAGE_AHCI_ONLY_TESTED |
| 237 | const u32 vendor_device = pci_read_config32(dev, 0x0); |
| 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", PCI_BUS(dev), |
| 244 | PCI_SLOT(dev), PCI_FUNC(dev), vendor, device); |
| 245 | return; |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | printf("ahci: Found SATA controller %02x:%02x.%02x (%04x:%04x).\n", |
| 250 | PCI_BUS(dev), PCI_SLOT(dev), PCI_FUNC(dev), vendor, device); |
| 251 | |
| 252 | hba_ctrl_t *const ctrl = phys_to_virt( |
| 253 | pci_read_config32(dev, 0x24) & ~0x3ff); |
| 254 | hba_port_t *const ports = ctrl->ports; |
| 255 | |
| 256 | /* Reset host controller. */ |
| 257 | ctrl->global_ctrl |= HBA_CTRL_RESET; |
| 258 | /* Reset has to be finished after 1s. */ |
| 259 | delay(1); |
| 260 | if (ctrl->global_ctrl & HBA_CTRL_RESET) { |
| 261 | printf("ahci: ERROR: " |
| 262 | "Controller reset didn't finish within 1s.\n"); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | /* Set AHCI access mode. */ |
| 267 | ctrl->global_ctrl |= HBA_CTRL_AHCI_EN; |
| 268 | |
| 269 | /* Probe for devices. */ |
| 270 | for (i = 0; i < 32; ++i) { |
| 271 | if (ctrl->ports_impl & (1 << i)) |
| 272 | ahci_port_probe(ctrl, &ports[i], i + 1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void ahci_initialize(void) |
| 277 | { |
| 278 | int bus, dev, func; |
| 279 | |
| 280 | for (bus = 0; bus < 256; ++bus) { |
| 281 | for (dev = 0; dev < 32; ++dev) { |
| 282 | const u16 class = |
| 283 | pci_read_config16(PCI_DEV(bus, dev, 0), 0xa); |
| 284 | if (class != 0xffff) { |
| 285 | for (func = 0; func < 8; ++func) |
| 286 | ahci_init_pci(PCI_DEV(bus, dev, func)); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |