1258223a3SMatthew Dillon /* 2*fb00c6edSMatthew Dillon * (MPSAFE) 3*fb00c6edSMatthew Dillon * 4258223a3SMatthew Dillon * Copyright (c) 2006 David Gwynne <dlg@openbsd.org> 5258223a3SMatthew Dillon * 6258223a3SMatthew Dillon * Permission to use, copy, modify, and distribute this software for any 7258223a3SMatthew Dillon * purpose with or without fee is hereby granted, provided that the above 8258223a3SMatthew Dillon * copyright notice and this permission notice appear in all copies. 9258223a3SMatthew Dillon * 10258223a3SMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11258223a3SMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12258223a3SMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13258223a3SMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14258223a3SMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15258223a3SMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16258223a3SMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17258223a3SMatthew Dillon * 18258223a3SMatthew Dillon * 19258223a3SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved. 20258223a3SMatthew Dillon * 21258223a3SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 22258223a3SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 23258223a3SMatthew Dillon * 24258223a3SMatthew Dillon * Redistribution and use in source and binary forms, with or without 25258223a3SMatthew Dillon * modification, are permitted provided that the following conditions 26258223a3SMatthew Dillon * are met: 27258223a3SMatthew Dillon * 28258223a3SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 29258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer. 30258223a3SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 31258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer in 32258223a3SMatthew Dillon * the documentation and/or other materials provided with the 33258223a3SMatthew Dillon * distribution. 34258223a3SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 35258223a3SMatthew Dillon * contributors may be used to endorse or promote products derived 36258223a3SMatthew Dillon * from this software without specific, prior written permission. 37258223a3SMatthew Dillon * 38258223a3SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 39258223a3SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 40258223a3SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 41258223a3SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 42258223a3SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 43258223a3SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 44258223a3SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45258223a3SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 46258223a3SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 47258223a3SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 48258223a3SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49258223a3SMatthew Dillon * SUCH DAMAGE. 50258223a3SMatthew Dillon * 51258223a3SMatthew Dillon * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $ 52258223a3SMatthew Dillon */ 53258223a3SMatthew Dillon 54258223a3SMatthew Dillon #include "ahci.h" 55258223a3SMatthew Dillon 56258223a3SMatthew Dillon static int ahci_vt8251_attach(device_t); 57258223a3SMatthew Dillon static int ahci_ati_sb600_attach(device_t); 58258223a3SMatthew Dillon static int ahci_nvidia_mcp_attach(device_t); 59258223a3SMatthew Dillon static int ahci_pci_attach(device_t); 60258223a3SMatthew Dillon static int ahci_pci_detach(device_t); 61258223a3SMatthew Dillon 62258223a3SMatthew Dillon static const struct ahci_device ahci_devices[] = { 63258223a3SMatthew Dillon { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8251_SATA, 64258223a3SMatthew Dillon ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" }, 65258223a3SMatthew Dillon { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_SATA, 66258223a3SMatthew Dillon ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" }, 67258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 68258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" }, 69258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_AHCI_1, 70258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" }, 71258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_5, 72258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" }, 73258223a3SMatthew Dillon { 0, 0, 74258223a3SMatthew Dillon ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" } 75258223a3SMatthew Dillon }; 76258223a3SMatthew Dillon 77258223a3SMatthew Dillon /* 78258223a3SMatthew Dillon * Match during probe and attach. The device does not yet have a softc. 79258223a3SMatthew Dillon */ 80258223a3SMatthew Dillon const struct ahci_device * 81258223a3SMatthew Dillon ahci_lookup_device(device_t dev) 82258223a3SMatthew Dillon { 83258223a3SMatthew Dillon const struct ahci_device *ad; 84258223a3SMatthew Dillon u_int16_t vendor = pci_get_vendor(dev); 85258223a3SMatthew Dillon u_int16_t product = pci_get_device(dev); 86258223a3SMatthew Dillon u_int8_t class = pci_get_class(dev); 87258223a3SMatthew Dillon u_int8_t subclass = pci_get_subclass(dev); 88258223a3SMatthew Dillon u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1); 89e95151e4SMatthew Dillon int is_ahci; 90258223a3SMatthew Dillon 91e95151e4SMatthew Dillon /* 92e95151e4SMatthew Dillon * Generally speaking if the pci device does not identify as 93e95151e4SMatthew Dillon * AHCI we skip it. 94e95151e4SMatthew Dillon */ 95e95151e4SMatthew Dillon if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA && 96e95151e4SMatthew Dillon progif == PCIP_STORAGE_SATA_AHCI_1_0) { 97e95151e4SMatthew Dillon is_ahci = 1; 98e95151e4SMatthew Dillon } else { 99e95151e4SMatthew Dillon is_ahci = 0; 100e95151e4SMatthew Dillon } 10112feb904SMatthew Dillon 102258223a3SMatthew Dillon for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) { 103258223a3SMatthew Dillon if (ad->ad_vendor == vendor && ad->ad_product == product) 104258223a3SMatthew Dillon return (ad); 105258223a3SMatthew Dillon } 106258223a3SMatthew Dillon 107258223a3SMatthew Dillon /* 108258223a3SMatthew Dillon * Last ad is the default match if the PCI device matches SATA. 109258223a3SMatthew Dillon */ 110e95151e4SMatthew Dillon if (is_ahci == 0) 111e95151e4SMatthew Dillon ad = NULL; 112258223a3SMatthew Dillon return (ad); 113258223a3SMatthew Dillon } 114258223a3SMatthew Dillon 115258223a3SMatthew Dillon /* 116258223a3SMatthew Dillon * Attach functions. They all eventually fall through to ahci_pci_attach(). 117258223a3SMatthew Dillon */ 118258223a3SMatthew Dillon static int 119258223a3SMatthew Dillon ahci_vt8251_attach(device_t dev) 120258223a3SMatthew Dillon { 121258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 122258223a3SMatthew Dillon 123258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_NO_NCQ; 124258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 125258223a3SMatthew Dillon } 126258223a3SMatthew Dillon 127258223a3SMatthew Dillon static int 128258223a3SMatthew Dillon ahci_ati_sb600_attach(device_t dev) 129258223a3SMatthew Dillon { 130258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 131258223a3SMatthew Dillon pcireg_t magic; 132258223a3SMatthew Dillon u_int8_t subclass = pci_get_subclass(dev); 133258223a3SMatthew Dillon u_int8_t revid; 134258223a3SMatthew Dillon 135258223a3SMatthew Dillon if (subclass == PCIS_STORAGE_IDE) { 136258223a3SMatthew Dillon revid = pci_read_config(dev, PCIR_REVID, 1); 137258223a3SMatthew Dillon magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4); 138258223a3SMatthew Dillon pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 139258223a3SMatthew Dillon magic | AHCI_PCI_ATI_SB600_LOCKED, 4); 140258223a3SMatthew Dillon pci_write_config(dev, PCIR_REVID, 141258223a3SMatthew Dillon (PCIC_STORAGE << 24) | 142258223a3SMatthew Dillon (PCIS_STORAGE_SATA << 16) | 143258223a3SMatthew Dillon (PCIP_STORAGE_SATA_AHCI_1_0 << 8) | 144258223a3SMatthew Dillon revid, 4); 145258223a3SMatthew Dillon pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4); 146258223a3SMatthew Dillon } 147258223a3SMatthew Dillon 148258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_IGN_FR; 149258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 150258223a3SMatthew Dillon } 151258223a3SMatthew Dillon 152258223a3SMatthew Dillon static int 153258223a3SMatthew Dillon ahci_nvidia_mcp_attach(device_t dev) 154258223a3SMatthew Dillon { 155258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 156258223a3SMatthew Dillon 157258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_IGN_FR; 158258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 159258223a3SMatthew Dillon } 160258223a3SMatthew Dillon 161258223a3SMatthew Dillon static int 162258223a3SMatthew Dillon ahci_pci_attach(device_t dev) 163258223a3SMatthew Dillon { 164258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 165f4553de1SMatthew Dillon struct ahci_port *ap; 166258223a3SMatthew Dillon const char *gen; 167258223a3SMatthew Dillon u_int32_t cap, pi, reg; 168258223a3SMatthew Dillon bus_addr_t addr; 169258223a3SMatthew Dillon int i; 170258223a3SMatthew Dillon int error; 171258223a3SMatthew Dillon const char *revision; 172258223a3SMatthew Dillon 17312feb904SMatthew Dillon if (pci_read_config(dev, PCIR_COMMAND, 2) & 0x0400) { 17477f3425bSMatthew Dillon device_printf(dev, "BIOS disabled PCI interrupt, " 17577f3425bSMatthew Dillon "re-enabling\n"); 17612feb904SMatthew Dillon pci_write_config(dev, PCIR_COMMAND, 17712feb904SMatthew Dillon pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2); 17812feb904SMatthew Dillon } 17912feb904SMatthew Dillon 18012feb904SMatthew Dillon 181258223a3SMatthew Dillon /* 182258223a3SMatthew Dillon * Map the AHCI controller's IRQ and BAR(5) (hardware registers) 183258223a3SMatthew Dillon */ 184258223a3SMatthew Dillon sc->sc_dev = dev; 185258223a3SMatthew Dillon sc->sc_rid_irq = AHCI_IRQ_RID; 186258223a3SMatthew Dillon sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq, 187258223a3SMatthew Dillon RF_SHAREABLE | RF_ACTIVE); 188258223a3SMatthew Dillon if (sc->sc_irq == NULL) { 189258223a3SMatthew Dillon device_printf(dev, "unable to map interrupt\n"); 190258223a3SMatthew Dillon ahci_pci_detach(dev); 191258223a3SMatthew Dillon return (ENXIO); 192258223a3SMatthew Dillon } 193258223a3SMatthew Dillon 194258223a3SMatthew Dillon /* 195258223a3SMatthew Dillon * When mapping the register window store the tag and handle 196258223a3SMatthew Dillon * separately so we can use the tag with per-port bus handle 197258223a3SMatthew Dillon * sub-spaces. 198258223a3SMatthew Dillon */ 199258223a3SMatthew Dillon sc->sc_rid_regs = PCIR_BAR(5); 200258223a3SMatthew Dillon sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 201258223a3SMatthew Dillon &sc->sc_rid_regs, RF_ACTIVE); 202258223a3SMatthew Dillon if (sc->sc_regs == NULL) { 203258223a3SMatthew Dillon device_printf(dev, "unable to map registers\n"); 204258223a3SMatthew Dillon ahci_pci_detach(dev); 205258223a3SMatthew Dillon return (ENXIO); 206258223a3SMatthew Dillon } 207258223a3SMatthew Dillon sc->sc_iot = rman_get_bustag(sc->sc_regs); 208258223a3SMatthew Dillon sc->sc_ioh = rman_get_bushandle(sc->sc_regs); 209258223a3SMatthew Dillon 210258223a3SMatthew Dillon /* 211258223a3SMatthew Dillon * Initialize the chipset and then set the interrupt vector up 212258223a3SMatthew Dillon */ 213258223a3SMatthew Dillon error = ahci_init(sc); 214258223a3SMatthew Dillon if (error) { 215258223a3SMatthew Dillon ahci_pci_detach(dev); 216258223a3SMatthew Dillon return (ENXIO); 217258223a3SMatthew Dillon } 218258223a3SMatthew Dillon 219258223a3SMatthew Dillon /* 220258223a3SMatthew Dillon * Get the AHCI capabilities and max number of concurrent 221258223a3SMatthew Dillon * command tags and set up the DMA tags. 222258223a3SMatthew Dillon */ 223258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 224258223a3SMatthew Dillon if (sc->sc_flags & AHCI_F_NO_NCQ) 225258223a3SMatthew Dillon cap &= ~AHCI_REG_CAP_SNCQ; 226258223a3SMatthew Dillon sc->sc_cap = cap; 2271067474aSMatthew Dillon 2281067474aSMatthew Dillon /* 2291067474aSMatthew Dillon * We assume at least 4 commands. 2301067474aSMatthew Dillon */ 231258223a3SMatthew Dillon sc->sc_ncmds = AHCI_REG_CAP_NCS(cap); 2321067474aSMatthew Dillon if (sc->sc_ncmds < 4) { 2331067474aSMatthew Dillon device_printf(dev, "NCS must probe a value >= 4\n"); 2341067474aSMatthew Dillon ahci_pci_detach(dev); 2351067474aSMatthew Dillon return (ENXIO); 2361067474aSMatthew Dillon } 237258223a3SMatthew Dillon 238258223a3SMatthew Dillon addr = (cap & AHCI_REG_CAP_S64A) ? 239258223a3SMatthew Dillon BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT; 240258223a3SMatthew Dillon 241258223a3SMatthew Dillon /* 242258223a3SMatthew Dillon * DMA tags for allocation of DMA memory buffers, lists, and so 243258223a3SMatthew Dillon * forth. These are typically per-port. 244258223a3SMatthew Dillon */ 245258223a3SMatthew Dillon error = 0; 246258223a3SMatthew Dillon error += bus_dma_tag_create( 247258223a3SMatthew Dillon NULL, /* parent tag */ 248258223a3SMatthew Dillon 256, /* alignment */ 249258223a3SMatthew Dillon PAGE_SIZE, /* boundary */ 250258223a3SMatthew Dillon addr, /* loaddr? */ 251258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 252258223a3SMatthew Dillon NULL, /* filter */ 253258223a3SMatthew Dillon NULL, /* filterarg */ 254258223a3SMatthew Dillon sizeof(struct ahci_rfis), /* [max]size */ 255258223a3SMatthew Dillon 1, /* maxsegs */ 256258223a3SMatthew Dillon sizeof(struct ahci_rfis), /* maxsegsz */ 257258223a3SMatthew Dillon 0, /* flags */ 258258223a3SMatthew Dillon &sc->sc_tag_rfis); /* return tag */ 259258223a3SMatthew Dillon 260258223a3SMatthew Dillon error += bus_dma_tag_create( 261258223a3SMatthew Dillon NULL, /* parent tag */ 262258223a3SMatthew Dillon 32, /* alignment */ 263258223a3SMatthew Dillon 4096 * 1024, /* boundary */ 264258223a3SMatthew Dillon addr, /* loaddr? */ 265258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 266258223a3SMatthew Dillon NULL, /* filter */ 267258223a3SMatthew Dillon NULL, /* filterarg */ 268258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_hdr), 269258223a3SMatthew Dillon 1, /* maxsegs */ 270258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_hdr), 271258223a3SMatthew Dillon 0, /* flags */ 272258223a3SMatthew Dillon &sc->sc_tag_cmdh); /* return tag */ 273258223a3SMatthew Dillon 274258223a3SMatthew Dillon /* 275258223a3SMatthew Dillon * NOTE: ahci_cmd_table is sized to a power of 2 276258223a3SMatthew Dillon */ 277258223a3SMatthew Dillon error += bus_dma_tag_create( 278258223a3SMatthew Dillon NULL, /* parent tag */ 279258223a3SMatthew Dillon sizeof(struct ahci_cmd_table), /* alignment */ 280258223a3SMatthew Dillon 4096 * 1024, /* boundary */ 281258223a3SMatthew Dillon addr, /* loaddr? */ 282258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 283258223a3SMatthew Dillon NULL, /* filter */ 284258223a3SMatthew Dillon NULL, /* filterarg */ 285258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_table), 286258223a3SMatthew Dillon 1, /* maxsegs */ 287258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_table), 288258223a3SMatthew Dillon 0, /* flags */ 289258223a3SMatthew Dillon &sc->sc_tag_cmdt); /* return tag */ 290258223a3SMatthew Dillon 291258223a3SMatthew Dillon /* 292258223a3SMatthew Dillon * The data tag is used for later dmamaps and not immediately 293258223a3SMatthew Dillon * allocated. 294258223a3SMatthew Dillon */ 295258223a3SMatthew Dillon error += bus_dma_tag_create( 296258223a3SMatthew Dillon NULL, /* parent tag */ 297258223a3SMatthew Dillon 4, /* alignment */ 298258223a3SMatthew Dillon 0, /* boundary */ 299258223a3SMatthew Dillon addr, /* loaddr? */ 300258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 301258223a3SMatthew Dillon NULL, /* filter */ 302258223a3SMatthew Dillon NULL, /* filterarg */ 303258223a3SMatthew Dillon 4096 * 1024, /* maxiosize */ 304258223a3SMatthew Dillon AHCI_MAX_PRDT, /* maxsegs */ 305258223a3SMatthew Dillon 65536, /* maxsegsz */ 306258223a3SMatthew Dillon 0, /* flags */ 307258223a3SMatthew Dillon &sc->sc_tag_data); /* return tag */ 308258223a3SMatthew Dillon 309258223a3SMatthew Dillon if (error) { 310258223a3SMatthew Dillon device_printf(dev, "unable to create dma tags\n"); 311258223a3SMatthew Dillon ahci_pci_detach(dev); 312258223a3SMatthew Dillon return (ENXIO); 313258223a3SMatthew Dillon } 314258223a3SMatthew Dillon 315258223a3SMatthew Dillon switch (cap & AHCI_REG_CAP_ISS) { 316258223a3SMatthew Dillon case AHCI_REG_CAP_ISS_G1: 317258223a3SMatthew Dillon gen = "1 (1.5Gbps)"; 318258223a3SMatthew Dillon break; 319258223a3SMatthew Dillon case AHCI_REG_CAP_ISS_G1_2: 320258223a3SMatthew Dillon gen = "1 (1.5Gbps) and 2 (3Gbps)"; 321258223a3SMatthew Dillon break; 322258223a3SMatthew Dillon default: 323258223a3SMatthew Dillon gen = "unknown"; 324258223a3SMatthew Dillon break; 325258223a3SMatthew Dillon } 326258223a3SMatthew Dillon 327258223a3SMatthew Dillon /* check the revision */ 328258223a3SMatthew Dillon reg = ahci_read(sc, AHCI_REG_VS); 329258223a3SMatthew Dillon switch (reg) { 330258223a3SMatthew Dillon case AHCI_REG_VS_0_95: 331258223a3SMatthew Dillon revision = "AHCI 0.95"; 332258223a3SMatthew Dillon break; 333258223a3SMatthew Dillon case AHCI_REG_VS_1_0: 334258223a3SMatthew Dillon revision = "AHCI 1.0"; 335258223a3SMatthew Dillon break; 336258223a3SMatthew Dillon case AHCI_REG_VS_1_1: 337258223a3SMatthew Dillon revision = "AHCI 1.1"; 338258223a3SMatthew Dillon break; 339258223a3SMatthew Dillon case AHCI_REG_VS_1_2: 340258223a3SMatthew Dillon revision = "AHCI 1.2"; 341258223a3SMatthew Dillon break; 342258223a3SMatthew Dillon default: 343258223a3SMatthew Dillon device_printf(sc->sc_dev, 344258223a3SMatthew Dillon "Warning: Unknown AHCI revision 0x%08x\n", reg); 345258223a3SMatthew Dillon revision = "AHCI <unknown>"; 346258223a3SMatthew Dillon break; 347258223a3SMatthew Dillon } 348258223a3SMatthew Dillon 349258223a3SMatthew Dillon device_printf(dev, 350258223a3SMatthew Dillon "%s capabilities 0x%b, %d ports, %d tags/port, gen %s\n", 351258223a3SMatthew Dillon revision, 352258223a3SMatthew Dillon cap, AHCI_FMT_CAP, 353258223a3SMatthew Dillon AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen); 354258223a3SMatthew Dillon 355258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 356258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n", 357258223a3SMatthew Dillon DEVNAME(sc), pi); 358258223a3SMatthew Dillon 359258223a3SMatthew Dillon #ifdef AHCI_COALESCE 360258223a3SMatthew Dillon /* Naive coalescing support - enable for all ports. */ 361258223a3SMatthew Dillon if (cap & AHCI_REG_CAP_CCCS) { 362258223a3SMatthew Dillon u_int16_t ccc_timeout = 20; 363258223a3SMatthew Dillon u_int8_t ccc_numcomplete = 12; 364258223a3SMatthew Dillon u_int32_t ccc_ctl; 365258223a3SMatthew Dillon 366258223a3SMatthew Dillon /* disable coalescing during reconfiguration. */ 367258223a3SMatthew Dillon ccc_ctl = ahci_read(sc, AHCI_REG_CCC_CTL); 368258223a3SMatthew Dillon ccc_ctl &= ~0x00000001; 369258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl); 370258223a3SMatthew Dillon 371258223a3SMatthew Dillon sc->sc_ccc_mask = 1 << AHCI_REG_CCC_CTL_INT(ccc_ctl); 372258223a3SMatthew Dillon if (pi & sc->sc_ccc_mask) { 373258223a3SMatthew Dillon /* A conflict with the implemented port list? */ 374258223a3SMatthew Dillon printf("%s: coalescing interrupt/implemented port list " 375258223a3SMatthew Dillon "conflict, PI: %08x, ccc_mask: %08x\n", 376258223a3SMatthew Dillon DEVNAME(sc), pi, sc->sc_ccc_mask); 377258223a3SMatthew Dillon sc->sc_ccc_mask = 0; 378258223a3SMatthew Dillon goto noccc; 379258223a3SMatthew Dillon } 380258223a3SMatthew Dillon 381258223a3SMatthew Dillon /* ahci_port_start will enable each port when it starts. */ 382258223a3SMatthew Dillon sc->sc_ccc_ports = pi; 383258223a3SMatthew Dillon sc->sc_ccc_ports_cur = 0; 384258223a3SMatthew Dillon 385258223a3SMatthew Dillon /* program thresholds and enable overall coalescing. */ 386258223a3SMatthew Dillon ccc_ctl &= ~0xffffff00; 387258223a3SMatthew Dillon ccc_ctl |= (ccc_timeout << 16) | (ccc_numcomplete << 8); 388258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl); 389258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_PORTS, 0); 390258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl | 1); 391258223a3SMatthew Dillon } 392258223a3SMatthew Dillon noccc: 393258223a3SMatthew Dillon #endif 394258223a3SMatthew Dillon /* 395258223a3SMatthew Dillon * Allocate per-port resources 396258223a3SMatthew Dillon * 397258223a3SMatthew Dillon * Ignore attach errors, leave the port intact for 398258223a3SMatthew Dillon * rescan and continue the loop. 399f4553de1SMatthew Dillon * 400f4553de1SMatthew Dillon * All ports are attached in parallel but the CAM scan-bus 401f4553de1SMatthew Dillon * is held up until all ports are attached so we get a deterministic 402f4553de1SMatthew Dillon * order. 403258223a3SMatthew Dillon */ 404258223a3SMatthew Dillon for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) { 405258223a3SMatthew Dillon if ((pi & (1 << i)) == 0) { 406258223a3SMatthew Dillon /* dont allocate stuff if the port isnt implemented */ 407258223a3SMatthew Dillon continue; 408258223a3SMatthew Dillon } 409258223a3SMatthew Dillon error = ahci_port_alloc(sc, i); 410258223a3SMatthew Dillon } 411258223a3SMatthew Dillon 412258223a3SMatthew Dillon /* 413258223a3SMatthew Dillon * Setup the interrupt vector and enable interrupts. Note that 414258223a3SMatthew Dillon * since the irq may be shared we do not set it up until we are 415258223a3SMatthew Dillon * ready to go. 416258223a3SMatthew Dillon */ 417258223a3SMatthew Dillon if (error == 0) { 418*fb00c6edSMatthew Dillon error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, 419*fb00c6edSMatthew Dillon ahci_intr, sc, 420f4553de1SMatthew Dillon &sc->sc_irq_handle, NULL); 421258223a3SMatthew Dillon } 422258223a3SMatthew Dillon 423258223a3SMatthew Dillon if (error) { 424258223a3SMatthew Dillon device_printf(dev, "unable to install interrupt\n"); 425258223a3SMatthew Dillon ahci_pci_detach(dev); 426258223a3SMatthew Dillon return (ENXIO); 427258223a3SMatthew Dillon } 428f4553de1SMatthew Dillon 429f4553de1SMatthew Dillon /* 430e8cf3f55SMatthew Dillon * Before marking the sc as good, which allows the interrupt 431e8cf3f55SMatthew Dillon * subsystem to operate on the ports, wait for all the port threads 432e8cf3f55SMatthew Dillon * to get past their initial pre-probe init. Otherwise an interrupt 433e8cf3f55SMatthew Dillon * may try to process the port before it has been initialized. 434e8cf3f55SMatthew Dillon */ 435e8cf3f55SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; i++) { 436e8cf3f55SMatthew Dillon if ((ap = sc->sc_ports[i]) != NULL) { 437e8cf3f55SMatthew Dillon while (ap->ap_signal & AP_SIGF_THREAD_SYNC) 438e8cf3f55SMatthew Dillon tsleep(&ap->ap_signal, 0, "ahprb1", hz); 439e8cf3f55SMatthew Dillon } 440e8cf3f55SMatthew Dillon } 441e8cf3f55SMatthew Dillon 442e8cf3f55SMatthew Dillon /* 443f4553de1SMatthew Dillon * Master interrupt enable, and call ahci_intr() in case we race 444f4553de1SMatthew Dillon * our AHCI_F_INT_GOOD flag. 445f4553de1SMatthew Dillon */ 446f4553de1SMatthew Dillon crit_enter(); 447258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE); 448f4553de1SMatthew Dillon sc->sc_flags |= AHCI_F_INT_GOOD; 449f4553de1SMatthew Dillon crit_exit(); 450f4553de1SMatthew Dillon ahci_intr(sc); 451f4553de1SMatthew Dillon 452f4553de1SMatthew Dillon /* 453f4553de1SMatthew Dillon * All ports are probing in parallel. Wait for them to finish 454f4553de1SMatthew Dillon * and then issue the cam attachment and bus scan serially so 455f4553de1SMatthew Dillon * the 'da' assignments are deterministic. 456f4553de1SMatthew Dillon */ 457f4553de1SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; i++) { 458f4553de1SMatthew Dillon if ((ap = sc->sc_ports[i]) != NULL) { 459f4553de1SMatthew Dillon while (ap->ap_signal & AP_SIGF_INIT) 460e8cf3f55SMatthew Dillon tsleep(&ap->ap_signal, 0, "ahprb2", hz); 461831bc9e3SMatthew Dillon ahci_os_lock_port(ap); 462f4553de1SMatthew Dillon if (ahci_cam_attach(ap) == 0) { 463f4553de1SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 464831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 465f4553de1SMatthew Dillon while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) { 466f4553de1SMatthew Dillon tsleep(&ap->ap_flags, 0, "ahprb2", hz); 467f4553de1SMatthew Dillon } 468831bc9e3SMatthew Dillon } else { 469831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 470f4553de1SMatthew Dillon } 471f4553de1SMatthew Dillon } 472f4553de1SMatthew Dillon } 473258223a3SMatthew Dillon 474258223a3SMatthew Dillon return(0); 475258223a3SMatthew Dillon } 476258223a3SMatthew Dillon 477258223a3SMatthew Dillon /* 478258223a3SMatthew Dillon * Device unload / detachment 479258223a3SMatthew Dillon */ 480258223a3SMatthew Dillon static int 481258223a3SMatthew Dillon ahci_pci_detach(device_t dev) 482258223a3SMatthew Dillon { 483258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 484258223a3SMatthew Dillon struct ahci_port *ap; 485258223a3SMatthew Dillon int i; 486258223a3SMatthew Dillon 487258223a3SMatthew Dillon /* 488258223a3SMatthew Dillon * Disable the controller and de-register the interrupt, if any. 489258223a3SMatthew Dillon * 490f4553de1SMatthew Dillon * XXX interlock last interrupt? 491258223a3SMatthew Dillon */ 492f4553de1SMatthew Dillon sc->sc_flags &= ~AHCI_F_INT_GOOD; 493f4553de1SMatthew Dillon if (sc->sc_regs) 494258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, 0); 495f4553de1SMatthew Dillon 496258223a3SMatthew Dillon if (sc->sc_irq_handle) { 497258223a3SMatthew Dillon bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle); 498258223a3SMatthew Dillon sc->sc_irq_handle = NULL; 499258223a3SMatthew Dillon } 500258223a3SMatthew Dillon 501258223a3SMatthew Dillon /* 502258223a3SMatthew Dillon * Free port structures and DMA memory 503258223a3SMatthew Dillon */ 504258223a3SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; i++) { 505258223a3SMatthew Dillon ap = sc->sc_ports[i]; 506258223a3SMatthew Dillon if (ap) { 507258223a3SMatthew Dillon ahci_cam_detach(ap); 508258223a3SMatthew Dillon ahci_port_free(sc, i); 509258223a3SMatthew Dillon } 510258223a3SMatthew Dillon } 511258223a3SMatthew Dillon 512258223a3SMatthew Dillon /* 513258223a3SMatthew Dillon * Clean up the bus space 514258223a3SMatthew Dillon */ 515258223a3SMatthew Dillon if (sc->sc_irq) { 516258223a3SMatthew Dillon bus_release_resource(dev, SYS_RES_IRQ, 517258223a3SMatthew Dillon sc->sc_rid_irq, sc->sc_irq); 518258223a3SMatthew Dillon sc->sc_irq = NULL; 519258223a3SMatthew Dillon } 520258223a3SMatthew Dillon if (sc->sc_regs) { 521258223a3SMatthew Dillon bus_release_resource(dev, SYS_RES_MEMORY, 522258223a3SMatthew Dillon sc->sc_rid_regs, sc->sc_regs); 523258223a3SMatthew Dillon sc->sc_regs = NULL; 524258223a3SMatthew Dillon } 525258223a3SMatthew Dillon 526258223a3SMatthew Dillon if (sc->sc_tag_rfis) { 527258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_rfis); 528258223a3SMatthew Dillon sc->sc_tag_rfis = NULL; 529258223a3SMatthew Dillon } 530258223a3SMatthew Dillon if (sc->sc_tag_cmdh) { 531258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_cmdh); 532258223a3SMatthew Dillon sc->sc_tag_cmdh = NULL; 533258223a3SMatthew Dillon } 534258223a3SMatthew Dillon if (sc->sc_tag_cmdt) { 535258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_cmdt); 536258223a3SMatthew Dillon sc->sc_tag_cmdt = NULL; 537258223a3SMatthew Dillon } 538258223a3SMatthew Dillon if (sc->sc_tag_data) { 539258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_data); 540258223a3SMatthew Dillon sc->sc_tag_data = NULL; 541258223a3SMatthew Dillon } 542258223a3SMatthew Dillon 543258223a3SMatthew Dillon return (0); 544258223a3SMatthew Dillon } 545