1258223a3SMatthew Dillon /* 2258223a3SMatthew Dillon * Copyright (c) 2006 David Gwynne <dlg@openbsd.org> 3258223a3SMatthew Dillon * 4258223a3SMatthew Dillon * Permission to use, copy, modify, and distribute this software for any 5258223a3SMatthew Dillon * purpose with or without fee is hereby granted, provided that the above 6258223a3SMatthew Dillon * copyright notice and this permission notice appear in all copies. 7258223a3SMatthew Dillon * 8258223a3SMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9258223a3SMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10258223a3SMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11258223a3SMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12258223a3SMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13258223a3SMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14258223a3SMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15258223a3SMatthew Dillon * 16258223a3SMatthew Dillon * 17258223a3SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved. 18258223a3SMatthew Dillon * 19258223a3SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 20258223a3SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 21258223a3SMatthew Dillon * 22258223a3SMatthew Dillon * Redistribution and use in source and binary forms, with or without 23258223a3SMatthew Dillon * modification, are permitted provided that the following conditions 24258223a3SMatthew Dillon * are met: 25258223a3SMatthew Dillon * 26258223a3SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 27258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer. 28258223a3SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 29258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer in 30258223a3SMatthew Dillon * the documentation and/or other materials provided with the 31258223a3SMatthew Dillon * distribution. 32258223a3SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 33258223a3SMatthew Dillon * contributors may be used to endorse or promote products derived 34258223a3SMatthew Dillon * from this software without specific, prior written permission. 35258223a3SMatthew Dillon * 36258223a3SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 37258223a3SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 38258223a3SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 39258223a3SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 40258223a3SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 41258223a3SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 42258223a3SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43258223a3SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 44258223a3SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45258223a3SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46258223a3SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47258223a3SMatthew Dillon * SUCH DAMAGE. 48258223a3SMatthew Dillon * 49258223a3SMatthew Dillon * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $ 50258223a3SMatthew Dillon */ 51258223a3SMatthew Dillon 52258223a3SMatthew Dillon #include "ahci.h" 53258223a3SMatthew Dillon 54258223a3SMatthew Dillon static int ahci_vt8251_attach(device_t); 55258223a3SMatthew Dillon static int ahci_ati_sb600_attach(device_t); 56258223a3SMatthew Dillon static int ahci_nvidia_mcp_attach(device_t); 57258223a3SMatthew Dillon static int ahci_pci_attach(device_t); 58258223a3SMatthew Dillon static int ahci_pci_detach(device_t); 59258223a3SMatthew Dillon 60258223a3SMatthew Dillon static const struct ahci_device ahci_devices[] = { 61258223a3SMatthew Dillon { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8251_SATA, 62258223a3SMatthew Dillon ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" }, 63258223a3SMatthew Dillon { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_SATA, 64258223a3SMatthew Dillon ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" }, 65258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 66258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" }, 67258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_AHCI_1, 68258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" }, 69258223a3SMatthew Dillon { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_AHCI_5, 70258223a3SMatthew Dillon ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" }, 71258223a3SMatthew Dillon { 0, 0, 72258223a3SMatthew Dillon ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" } 73258223a3SMatthew Dillon }; 74258223a3SMatthew Dillon 75258223a3SMatthew Dillon u_int32_t AhciForceGen1 = 0; /* XXX add sysctl/kenv support */ 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); 89258223a3SMatthew Dillon 90258223a3SMatthew Dillon for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) { 91258223a3SMatthew Dillon if (ad->ad_vendor == vendor && ad->ad_product == product) 92258223a3SMatthew Dillon return (ad); 93258223a3SMatthew Dillon } 94258223a3SMatthew Dillon 95258223a3SMatthew Dillon /* 96258223a3SMatthew Dillon * Last ad is the default match if the PCI device matches SATA. 97258223a3SMatthew Dillon */ 98258223a3SMatthew Dillon if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA && 99258223a3SMatthew Dillon progif == PCIP_STORAGE_SATA_AHCI_1_0) { 100258223a3SMatthew Dillon kprintf("match generic sata\n"); 101258223a3SMatthew Dillon return (ad); 102258223a3SMatthew Dillon } 103258223a3SMatthew Dillon 104258223a3SMatthew Dillon return (NULL); 105258223a3SMatthew Dillon } 106258223a3SMatthew Dillon 107258223a3SMatthew Dillon /* 108258223a3SMatthew Dillon * Attach functions. They all eventually fall through to ahci_pci_attach(). 109258223a3SMatthew Dillon */ 110258223a3SMatthew Dillon static int 111258223a3SMatthew Dillon ahci_vt8251_attach(device_t dev) 112258223a3SMatthew Dillon { 113258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 114258223a3SMatthew Dillon 115258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_NO_NCQ; 116258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 117258223a3SMatthew Dillon } 118258223a3SMatthew Dillon 119258223a3SMatthew Dillon static int 120258223a3SMatthew Dillon ahci_ati_sb600_attach(device_t dev) 121258223a3SMatthew Dillon { 122258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 123258223a3SMatthew Dillon pcireg_t magic; 124258223a3SMatthew Dillon u_int8_t subclass = pci_get_subclass(dev); 125258223a3SMatthew Dillon u_int8_t revid; 126258223a3SMatthew Dillon 127258223a3SMatthew Dillon if (subclass == PCIS_STORAGE_IDE) { 128258223a3SMatthew Dillon revid = pci_read_config(dev, PCIR_REVID, 1); 129258223a3SMatthew Dillon magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4); 130258223a3SMatthew Dillon pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 131258223a3SMatthew Dillon magic | AHCI_PCI_ATI_SB600_LOCKED, 4); 132258223a3SMatthew Dillon pci_write_config(dev, PCIR_REVID, 133258223a3SMatthew Dillon (PCIC_STORAGE << 24) | 134258223a3SMatthew Dillon (PCIS_STORAGE_SATA << 16) | 135258223a3SMatthew Dillon (PCIP_STORAGE_SATA_AHCI_1_0 << 8) | 136258223a3SMatthew Dillon revid, 4); 137258223a3SMatthew Dillon pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4); 138258223a3SMatthew Dillon } 139258223a3SMatthew Dillon 140258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_IGN_FR; 141258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 142258223a3SMatthew Dillon } 143258223a3SMatthew Dillon 144258223a3SMatthew Dillon static int 145258223a3SMatthew Dillon ahci_nvidia_mcp_attach(device_t dev) 146258223a3SMatthew Dillon { 147258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 148258223a3SMatthew Dillon 149258223a3SMatthew Dillon sc->sc_flags |= AHCI_F_IGN_FR; 150258223a3SMatthew Dillon return (ahci_pci_attach(dev)); 151258223a3SMatthew Dillon } 152258223a3SMatthew Dillon 153258223a3SMatthew Dillon static int 154258223a3SMatthew Dillon ahci_pci_attach(device_t dev) 155258223a3SMatthew Dillon { 156258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 157f4553de1SMatthew Dillon struct ahci_port *ap; 158258223a3SMatthew Dillon const char *gen; 159258223a3SMatthew Dillon u_int32_t cap, pi, reg; 160258223a3SMatthew Dillon bus_addr_t addr; 161258223a3SMatthew Dillon int i; 162258223a3SMatthew Dillon int error; 163258223a3SMatthew Dillon const char *revision; 164258223a3SMatthew Dillon 165258223a3SMatthew Dillon /* 166258223a3SMatthew Dillon * Map the AHCI controller's IRQ and BAR(5) (hardware registers) 167258223a3SMatthew Dillon */ 168258223a3SMatthew Dillon sc->sc_dev = dev; 169258223a3SMatthew Dillon sc->sc_rid_irq = AHCI_IRQ_RID; 170258223a3SMatthew Dillon sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq, 171258223a3SMatthew Dillon RF_SHAREABLE | RF_ACTIVE); 172258223a3SMatthew Dillon if (sc->sc_irq == NULL) { 173258223a3SMatthew Dillon device_printf(dev, "unable to map interrupt\n"); 174258223a3SMatthew Dillon ahci_pci_detach(dev); 175258223a3SMatthew Dillon return (ENXIO); 176258223a3SMatthew Dillon } 177258223a3SMatthew Dillon 178258223a3SMatthew Dillon /* 179258223a3SMatthew Dillon * When mapping the register window store the tag and handle 180258223a3SMatthew Dillon * separately so we can use the tag with per-port bus handle 181258223a3SMatthew Dillon * sub-spaces. 182258223a3SMatthew Dillon */ 183258223a3SMatthew Dillon sc->sc_rid_regs = PCIR_BAR(5); 184258223a3SMatthew Dillon sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 185258223a3SMatthew Dillon &sc->sc_rid_regs, RF_ACTIVE); 186258223a3SMatthew Dillon if (sc->sc_regs == NULL) { 187258223a3SMatthew Dillon device_printf(dev, "unable to map registers\n"); 188258223a3SMatthew Dillon ahci_pci_detach(dev); 189258223a3SMatthew Dillon return (ENXIO); 190258223a3SMatthew Dillon } 191258223a3SMatthew Dillon sc->sc_iot = rman_get_bustag(sc->sc_regs); 192258223a3SMatthew Dillon sc->sc_ioh = rman_get_bushandle(sc->sc_regs); 193258223a3SMatthew Dillon 194258223a3SMatthew Dillon /* 195258223a3SMatthew Dillon * Initialize the chipset and then set the interrupt vector up 196258223a3SMatthew Dillon */ 197258223a3SMatthew Dillon error = ahci_init(sc); 198258223a3SMatthew Dillon if (error) { 199258223a3SMatthew Dillon ahci_pci_detach(dev); 200258223a3SMatthew Dillon return (ENXIO); 201258223a3SMatthew Dillon } 202258223a3SMatthew Dillon 203258223a3SMatthew Dillon /* 204258223a3SMatthew Dillon * Get the AHCI capabilities and max number of concurrent 205258223a3SMatthew Dillon * command tags and set up the DMA tags. 206258223a3SMatthew Dillon */ 207258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 208258223a3SMatthew Dillon if (sc->sc_flags & AHCI_F_NO_NCQ) 209258223a3SMatthew Dillon cap &= ~AHCI_REG_CAP_SNCQ; 210258223a3SMatthew Dillon sc->sc_cap = cap; 211258223a3SMatthew Dillon sc->sc_ncmds = AHCI_REG_CAP_NCS(cap); 212258223a3SMatthew Dillon 213258223a3SMatthew Dillon addr = (cap & AHCI_REG_CAP_S64A) ? 214258223a3SMatthew Dillon BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT; 215258223a3SMatthew Dillon 216258223a3SMatthew Dillon /* 217258223a3SMatthew Dillon * DMA tags for allocation of DMA memory buffers, lists, and so 218258223a3SMatthew Dillon * forth. These are typically per-port. 219258223a3SMatthew Dillon */ 220258223a3SMatthew Dillon error = 0; 221258223a3SMatthew Dillon error += bus_dma_tag_create( 222258223a3SMatthew Dillon NULL, /* parent tag */ 223258223a3SMatthew Dillon 256, /* alignment */ 224258223a3SMatthew Dillon PAGE_SIZE, /* boundary */ 225258223a3SMatthew Dillon addr, /* loaddr? */ 226258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 227258223a3SMatthew Dillon NULL, /* filter */ 228258223a3SMatthew Dillon NULL, /* filterarg */ 229258223a3SMatthew Dillon sizeof(struct ahci_rfis), /* [max]size */ 230258223a3SMatthew Dillon 1, /* maxsegs */ 231258223a3SMatthew Dillon sizeof(struct ahci_rfis), /* maxsegsz */ 232258223a3SMatthew Dillon 0, /* flags */ 233258223a3SMatthew Dillon &sc->sc_tag_rfis); /* return tag */ 234258223a3SMatthew Dillon 235258223a3SMatthew Dillon error += bus_dma_tag_create( 236258223a3SMatthew Dillon NULL, /* parent tag */ 237258223a3SMatthew Dillon 32, /* alignment */ 238258223a3SMatthew Dillon 4096 * 1024, /* boundary */ 239258223a3SMatthew Dillon addr, /* loaddr? */ 240258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 241258223a3SMatthew Dillon NULL, /* filter */ 242258223a3SMatthew Dillon NULL, /* filterarg */ 243258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_hdr), 244258223a3SMatthew Dillon 1, /* maxsegs */ 245258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_hdr), 246258223a3SMatthew Dillon 0, /* flags */ 247258223a3SMatthew Dillon &sc->sc_tag_cmdh); /* return tag */ 248258223a3SMatthew Dillon 249258223a3SMatthew Dillon /* 250258223a3SMatthew Dillon * NOTE: ahci_cmd_table is sized to a power of 2 251258223a3SMatthew Dillon */ 252258223a3SMatthew Dillon error += bus_dma_tag_create( 253258223a3SMatthew Dillon NULL, /* parent tag */ 254258223a3SMatthew Dillon sizeof(struct ahci_cmd_table), /* alignment */ 255258223a3SMatthew Dillon 4096 * 1024, /* boundary */ 256258223a3SMatthew Dillon addr, /* loaddr? */ 257258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 258258223a3SMatthew Dillon NULL, /* filter */ 259258223a3SMatthew Dillon NULL, /* filterarg */ 260258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_table), 261258223a3SMatthew Dillon 1, /* maxsegs */ 262258223a3SMatthew Dillon sc->sc_ncmds * sizeof(struct ahci_cmd_table), 263258223a3SMatthew Dillon 0, /* flags */ 264258223a3SMatthew Dillon &sc->sc_tag_cmdt); /* return tag */ 265258223a3SMatthew Dillon 266258223a3SMatthew Dillon /* 267258223a3SMatthew Dillon * The data tag is used for later dmamaps and not immediately 268258223a3SMatthew Dillon * allocated. 269258223a3SMatthew Dillon */ 270258223a3SMatthew Dillon error += bus_dma_tag_create( 271258223a3SMatthew Dillon NULL, /* parent tag */ 272258223a3SMatthew Dillon 4, /* alignment */ 273258223a3SMatthew Dillon 0, /* boundary */ 274258223a3SMatthew Dillon addr, /* loaddr? */ 275258223a3SMatthew Dillon BUS_SPACE_MAXADDR, /* hiaddr */ 276258223a3SMatthew Dillon NULL, /* filter */ 277258223a3SMatthew Dillon NULL, /* filterarg */ 278258223a3SMatthew Dillon 4096 * 1024, /* maxiosize */ 279258223a3SMatthew Dillon AHCI_MAX_PRDT, /* maxsegs */ 280258223a3SMatthew Dillon 65536, /* maxsegsz */ 281258223a3SMatthew Dillon 0, /* flags */ 282258223a3SMatthew Dillon &sc->sc_tag_data); /* return tag */ 283258223a3SMatthew Dillon 284258223a3SMatthew Dillon if (error) { 285258223a3SMatthew Dillon device_printf(dev, "unable to create dma tags\n"); 286258223a3SMatthew Dillon ahci_pci_detach(dev); 287258223a3SMatthew Dillon return (ENXIO); 288258223a3SMatthew Dillon } 289258223a3SMatthew Dillon 290258223a3SMatthew Dillon switch (cap & AHCI_REG_CAP_ISS) { 291258223a3SMatthew Dillon case AHCI_REG_CAP_ISS_G1: 292258223a3SMatthew Dillon gen = "1 (1.5Gbps)"; 293258223a3SMatthew Dillon break; 294258223a3SMatthew Dillon case AHCI_REG_CAP_ISS_G1_2: 295258223a3SMatthew Dillon gen = "1 (1.5Gbps) and 2 (3Gbps)"; 296258223a3SMatthew Dillon break; 297258223a3SMatthew Dillon default: 298258223a3SMatthew Dillon gen = "unknown"; 299258223a3SMatthew Dillon break; 300258223a3SMatthew Dillon } 301258223a3SMatthew Dillon 302258223a3SMatthew Dillon /* check the revision */ 303258223a3SMatthew Dillon reg = ahci_read(sc, AHCI_REG_VS); 304258223a3SMatthew Dillon switch (reg) { 305258223a3SMatthew Dillon case AHCI_REG_VS_0_95: 306258223a3SMatthew Dillon revision = "AHCI 0.95"; 307258223a3SMatthew Dillon break; 308258223a3SMatthew Dillon case AHCI_REG_VS_1_0: 309258223a3SMatthew Dillon revision = "AHCI 1.0"; 310258223a3SMatthew Dillon break; 311258223a3SMatthew Dillon case AHCI_REG_VS_1_1: 312258223a3SMatthew Dillon revision = "AHCI 1.1"; 313258223a3SMatthew Dillon break; 314258223a3SMatthew Dillon case AHCI_REG_VS_1_2: 315258223a3SMatthew Dillon revision = "AHCI 1.2"; 316258223a3SMatthew Dillon break; 317258223a3SMatthew Dillon default: 318258223a3SMatthew Dillon device_printf(sc->sc_dev, 319258223a3SMatthew Dillon "Warning: Unknown AHCI revision 0x%08x\n", reg); 320258223a3SMatthew Dillon revision = "AHCI <unknown>"; 321258223a3SMatthew Dillon break; 322258223a3SMatthew Dillon } 323258223a3SMatthew Dillon 324258223a3SMatthew Dillon device_printf(dev, 325258223a3SMatthew Dillon "%s capabilities 0x%b, %d ports, %d tags/port, gen %s\n", 326258223a3SMatthew Dillon revision, 327258223a3SMatthew Dillon cap, AHCI_FMT_CAP, 328258223a3SMatthew Dillon AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen); 329258223a3SMatthew Dillon 330258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 331258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n", 332258223a3SMatthew Dillon DEVNAME(sc), pi); 333258223a3SMatthew Dillon 334258223a3SMatthew Dillon #ifdef AHCI_COALESCE 335258223a3SMatthew Dillon /* Naive coalescing support - enable for all ports. */ 336258223a3SMatthew Dillon if (cap & AHCI_REG_CAP_CCCS) { 337258223a3SMatthew Dillon u_int16_t ccc_timeout = 20; 338258223a3SMatthew Dillon u_int8_t ccc_numcomplete = 12; 339258223a3SMatthew Dillon u_int32_t ccc_ctl; 340258223a3SMatthew Dillon 341258223a3SMatthew Dillon /* disable coalescing during reconfiguration. */ 342258223a3SMatthew Dillon ccc_ctl = ahci_read(sc, AHCI_REG_CCC_CTL); 343258223a3SMatthew Dillon ccc_ctl &= ~0x00000001; 344258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl); 345258223a3SMatthew Dillon 346258223a3SMatthew Dillon sc->sc_ccc_mask = 1 << AHCI_REG_CCC_CTL_INT(ccc_ctl); 347258223a3SMatthew Dillon if (pi & sc->sc_ccc_mask) { 348258223a3SMatthew Dillon /* A conflict with the implemented port list? */ 349258223a3SMatthew Dillon printf("%s: coalescing interrupt/implemented port list " 350258223a3SMatthew Dillon "conflict, PI: %08x, ccc_mask: %08x\n", 351258223a3SMatthew Dillon DEVNAME(sc), pi, sc->sc_ccc_mask); 352258223a3SMatthew Dillon sc->sc_ccc_mask = 0; 353258223a3SMatthew Dillon goto noccc; 354258223a3SMatthew Dillon } 355258223a3SMatthew Dillon 356258223a3SMatthew Dillon /* ahci_port_start will enable each port when it starts. */ 357258223a3SMatthew Dillon sc->sc_ccc_ports = pi; 358258223a3SMatthew Dillon sc->sc_ccc_ports_cur = 0; 359258223a3SMatthew Dillon 360258223a3SMatthew Dillon /* program thresholds and enable overall coalescing. */ 361258223a3SMatthew Dillon ccc_ctl &= ~0xffffff00; 362258223a3SMatthew Dillon ccc_ctl |= (ccc_timeout << 16) | (ccc_numcomplete << 8); 363258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl); 364258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_PORTS, 0); 365258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl | 1); 366258223a3SMatthew Dillon } 367258223a3SMatthew Dillon noccc: 368258223a3SMatthew Dillon #endif 369258223a3SMatthew Dillon /* 370258223a3SMatthew Dillon * Allocate per-port resources 371258223a3SMatthew Dillon * 372258223a3SMatthew Dillon * Ignore attach errors, leave the port intact for 373258223a3SMatthew Dillon * rescan and continue the loop. 374f4553de1SMatthew Dillon * 375f4553de1SMatthew Dillon * All ports are attached in parallel but the CAM scan-bus 376f4553de1SMatthew Dillon * is held up until all ports are attached so we get a deterministic 377f4553de1SMatthew Dillon * order. 378258223a3SMatthew Dillon */ 379258223a3SMatthew Dillon for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) { 380258223a3SMatthew Dillon if ((pi & (1 << i)) == 0) { 381258223a3SMatthew Dillon /* dont allocate stuff if the port isnt implemented */ 382258223a3SMatthew Dillon continue; 383258223a3SMatthew Dillon } 384258223a3SMatthew Dillon error = ahci_port_alloc(sc, i); 385258223a3SMatthew Dillon } 386258223a3SMatthew Dillon 387258223a3SMatthew Dillon /* 388258223a3SMatthew Dillon * Setup the interrupt vector and enable interrupts. Note that 389258223a3SMatthew Dillon * since the irq may be shared we do not set it up until we are 390258223a3SMatthew Dillon * ready to go. 391258223a3SMatthew Dillon */ 392258223a3SMatthew Dillon if (error == 0) { 393258223a3SMatthew Dillon error = bus_setup_intr(dev, sc->sc_irq, 0, ahci_intr, sc, 394f4553de1SMatthew Dillon &sc->sc_irq_handle, NULL); 395258223a3SMatthew Dillon } 396258223a3SMatthew Dillon 397258223a3SMatthew Dillon if (error) { 398258223a3SMatthew Dillon device_printf(dev, "unable to install interrupt\n"); 399258223a3SMatthew Dillon ahci_pci_detach(dev); 400258223a3SMatthew Dillon return (ENXIO); 401258223a3SMatthew Dillon } 402f4553de1SMatthew Dillon 403f4553de1SMatthew Dillon /* 404f4553de1SMatthew Dillon * Master interrupt enable, and call ahci_intr() in case we race 405f4553de1SMatthew Dillon * our AHCI_F_INT_GOOD flag. 406f4553de1SMatthew Dillon */ 407f4553de1SMatthew Dillon crit_enter(); 408258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE); 409f4553de1SMatthew Dillon sc->sc_flags |= AHCI_F_INT_GOOD; 410f4553de1SMatthew Dillon crit_exit(); 411f4553de1SMatthew Dillon ahci_intr(sc); 412f4553de1SMatthew Dillon 413f4553de1SMatthew Dillon /* 414f4553de1SMatthew Dillon * All ports are probing in parallel. Wait for them to finish 415f4553de1SMatthew Dillon * and then issue the cam attachment and bus scan serially so 416f4553de1SMatthew Dillon * the 'da' assignments are deterministic. 417f4553de1SMatthew Dillon */ 418f4553de1SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; i++) { 419f4553de1SMatthew Dillon if ((ap = sc->sc_ports[i]) != NULL) { 420f4553de1SMatthew Dillon while (ap->ap_signal & AP_SIGF_INIT) 421f4553de1SMatthew Dillon tsleep(&ap->ap_signal, 0, "ahprb1", hz); 422*831bc9e3SMatthew Dillon ahci_os_lock_port(ap); 423f4553de1SMatthew Dillon if (ahci_cam_attach(ap) == 0) { 424f4553de1SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 425*831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 426f4553de1SMatthew Dillon while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) { 427f4553de1SMatthew Dillon tsleep(&ap->ap_flags, 0, "ahprb2", hz); 428f4553de1SMatthew Dillon } 429*831bc9e3SMatthew Dillon } else { 430*831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 431f4553de1SMatthew Dillon } 432f4553de1SMatthew Dillon } 433f4553de1SMatthew Dillon } 434258223a3SMatthew Dillon 435258223a3SMatthew Dillon return(0); 436258223a3SMatthew Dillon } 437258223a3SMatthew Dillon 438258223a3SMatthew Dillon /* 439258223a3SMatthew Dillon * Device unload / detachment 440258223a3SMatthew Dillon */ 441258223a3SMatthew Dillon static int 442258223a3SMatthew Dillon ahci_pci_detach(device_t dev) 443258223a3SMatthew Dillon { 444258223a3SMatthew Dillon struct ahci_softc *sc = device_get_softc(dev); 445258223a3SMatthew Dillon struct ahci_port *ap; 446258223a3SMatthew Dillon int i; 447258223a3SMatthew Dillon 448258223a3SMatthew Dillon /* 449258223a3SMatthew Dillon * Disable the controller and de-register the interrupt, if any. 450258223a3SMatthew Dillon * 451f4553de1SMatthew Dillon * XXX interlock last interrupt? 452258223a3SMatthew Dillon */ 453f4553de1SMatthew Dillon sc->sc_flags &= ~AHCI_F_INT_GOOD; 454f4553de1SMatthew Dillon if (sc->sc_regs) 455258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, 0); 456f4553de1SMatthew Dillon 457258223a3SMatthew Dillon if (sc->sc_irq_handle) { 458258223a3SMatthew Dillon bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle); 459258223a3SMatthew Dillon sc->sc_irq_handle = NULL; 460258223a3SMatthew Dillon } 461258223a3SMatthew Dillon 462258223a3SMatthew Dillon /* 463258223a3SMatthew Dillon * Free port structures and DMA memory 464258223a3SMatthew Dillon */ 465258223a3SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; i++) { 466258223a3SMatthew Dillon ap = sc->sc_ports[i]; 467258223a3SMatthew Dillon if (ap) { 468258223a3SMatthew Dillon ahci_cam_detach(ap); 469258223a3SMatthew Dillon ahci_port_free(sc, i); 470258223a3SMatthew Dillon } 471258223a3SMatthew Dillon } 472258223a3SMatthew Dillon 473258223a3SMatthew Dillon /* 474258223a3SMatthew Dillon * Clean up the bus space 475258223a3SMatthew Dillon */ 476258223a3SMatthew Dillon if (sc->sc_irq) { 477258223a3SMatthew Dillon bus_release_resource(dev, SYS_RES_IRQ, 478258223a3SMatthew Dillon sc->sc_rid_irq, sc->sc_irq); 479258223a3SMatthew Dillon sc->sc_irq = NULL; 480258223a3SMatthew Dillon } 481258223a3SMatthew Dillon if (sc->sc_regs) { 482258223a3SMatthew Dillon bus_release_resource(dev, SYS_RES_MEMORY, 483258223a3SMatthew Dillon sc->sc_rid_regs, sc->sc_regs); 484258223a3SMatthew Dillon sc->sc_regs = NULL; 485258223a3SMatthew Dillon } 486258223a3SMatthew Dillon 487258223a3SMatthew Dillon if (sc->sc_tag_rfis) { 488258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_rfis); 489258223a3SMatthew Dillon sc->sc_tag_rfis = NULL; 490258223a3SMatthew Dillon } 491258223a3SMatthew Dillon if (sc->sc_tag_cmdh) { 492258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_cmdh); 493258223a3SMatthew Dillon sc->sc_tag_cmdh = NULL; 494258223a3SMatthew Dillon } 495258223a3SMatthew Dillon if (sc->sc_tag_cmdt) { 496258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_cmdt); 497258223a3SMatthew Dillon sc->sc_tag_cmdt = NULL; 498258223a3SMatthew Dillon } 499258223a3SMatthew Dillon if (sc->sc_tag_data) { 500258223a3SMatthew Dillon bus_dma_tag_destroy(sc->sc_tag_data); 501258223a3SMatthew Dillon sc->sc_tag_data = NULL; 502258223a3SMatthew Dillon } 503258223a3SMatthew Dillon 504258223a3SMatthew Dillon return (0); 505258223a3SMatthew Dillon } 506