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 54fd8bd957SMatthew Dillon int ahci_port_init(struct ahci_port *ap); 55258223a3SMatthew Dillon int ahci_port_start(struct ahci_port *, int); 56258223a3SMatthew Dillon int ahci_port_stop(struct ahci_port *, int); 57258223a3SMatthew Dillon int ahci_port_clo(struct ahci_port *); 58258223a3SMatthew Dillon 59fd8bd957SMatthew Dillon int ahci_port_signature_detect(struct ahci_port *ap); 60258223a3SMatthew Dillon int ahci_load_prdt(struct ahci_ccb *); 61258223a3SMatthew Dillon void ahci_unload_prdt(struct ahci_ccb *); 62258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, 63258223a3SMatthew Dillon int nsegs, int error); 64258223a3SMatthew Dillon int ahci_poll(struct ahci_ccb *, int, void (*)(void *)); 65258223a3SMatthew Dillon void ahci_start(struct ahci_ccb *); 66258223a3SMatthew Dillon 67258223a3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *arg); 68258223a3SMatthew Dillon static void ahci_ata_cmd_timeout(void *arg); 69258223a3SMatthew Dillon 70258223a3SMatthew Dillon void ahci_issue_pending_ncq_commands(struct ahci_port *); 71258223a3SMatthew Dillon void ahci_issue_pending_commands(struct ahci_port *, int); 72258223a3SMatthew Dillon 73258223a3SMatthew Dillon struct ahci_ccb *ahci_get_ccb(struct ahci_port *); 74258223a3SMatthew Dillon void ahci_put_ccb(struct ahci_ccb *); 75258223a3SMatthew Dillon 76258223a3SMatthew Dillon struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *); 77258223a3SMatthew Dillon void ahci_put_err_ccb(struct ahci_ccb *); 78258223a3SMatthew Dillon 79258223a3SMatthew Dillon int ahci_port_read_ncq_error(struct ahci_port *, int *); 80258223a3SMatthew Dillon 81258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag); 82258223a3SMatthew Dillon void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *); 83258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error); 84258223a3SMatthew Dillon 85258223a3SMatthew Dillon void ahci_empty_done(struct ahci_ccb *ccb); 86258223a3SMatthew Dillon void ahci_ata_cmd_done(struct ahci_ccb *ccb); 87258223a3SMatthew Dillon 88258223a3SMatthew Dillon /* Wait for all bits in _b to be cleared */ 89cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \ 90cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0) 91cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to, _r, _b) \ 92cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), 0) 93258223a3SMatthew Dillon 94258223a3SMatthew Dillon /* Wait for all bits in _b to be set */ 95cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \ 96cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b)) 97cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \ 98cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), (_b)) 99cec85a37SMatthew Dillon 100cec85a37SMatthew Dillon #define AHCI_PWAIT_TIMEOUT 1000 101258223a3SMatthew Dillon 102fd8bd957SMatthew Dillon /* 103fd8bd957SMatthew Dillon * Initialize the global AHCI hardware. This code does not set up any of 104fd8bd957SMatthew Dillon * its ports. 105fd8bd957SMatthew Dillon */ 106258223a3SMatthew Dillon int 107258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc) 108258223a3SMatthew Dillon { 109258223a3SMatthew Dillon u_int32_t cap, pi; 110258223a3SMatthew Dillon 111258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b", 112258223a3SMatthew Dillon ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC); 113258223a3SMatthew Dillon 114258223a3SMatthew Dillon /* save BIOS initialised parameters, enable staggered spin up */ 115258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 116258223a3SMatthew Dillon cap &= AHCI_REG_CAP_SMPS; 117258223a3SMatthew Dillon cap |= AHCI_REG_CAP_SSS; 118258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 119258223a3SMatthew Dillon 120258223a3SMatthew Dillon if (AHCI_REG_GHC_AE & ahci_read(sc, AHCI_REG_GHC)) { 121258223a3SMatthew Dillon /* reset the controller */ 122258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR); 123258223a3SMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR, 124258223a3SMatthew Dillon AHCI_REG_GHC_HR) != 0) { 125258223a3SMatthew Dillon device_printf(sc->sc_dev, 126258223a3SMatthew Dillon "unable to reset controller\n"); 127258223a3SMatthew Dillon return (1); 128258223a3SMatthew Dillon } 129258223a3SMatthew Dillon } 130258223a3SMatthew Dillon 131258223a3SMatthew Dillon /* enable ahci (global interrupts disabled) */ 132258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 133258223a3SMatthew Dillon 134258223a3SMatthew Dillon /* restore parameters */ 135258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CAP, cap); 136258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_PI, pi); 137258223a3SMatthew Dillon 138258223a3SMatthew Dillon return (0); 139258223a3SMatthew Dillon } 140258223a3SMatthew Dillon 141fd8bd957SMatthew Dillon /* 142fd8bd957SMatthew Dillon * Allocate and initialize an AHCI port. 143fd8bd957SMatthew Dillon */ 144258223a3SMatthew Dillon int 145258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port) 146258223a3SMatthew Dillon { 147258223a3SMatthew Dillon struct ahci_port *ap; 148258223a3SMatthew Dillon struct ahci_ccb *ccb; 149258223a3SMatthew Dillon u_int64_t dva; 150258223a3SMatthew Dillon u_int32_t cmd; 151258223a3SMatthew Dillon struct ahci_cmd_hdr *hdr; 152258223a3SMatthew Dillon struct ahci_cmd_table *table; 153258223a3SMatthew Dillon int rc = ENOMEM; 154258223a3SMatthew Dillon int error; 155258223a3SMatthew Dillon int i; 156258223a3SMatthew Dillon 157258223a3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 158258223a3SMatthew Dillon if (ap == NULL) { 159258223a3SMatthew Dillon device_printf(sc->sc_dev, 160258223a3SMatthew Dillon "unable to allocate memory for port %d\n", 161258223a3SMatthew Dillon port); 162258223a3SMatthew Dillon goto reterr; 163258223a3SMatthew Dillon } 164258223a3SMatthew Dillon 165258223a3SMatthew Dillon ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d", 166258223a3SMatthew Dillon device_get_name(sc->sc_dev), 167258223a3SMatthew Dillon device_get_unit(sc->sc_dev), 168258223a3SMatthew Dillon port); 169258223a3SMatthew Dillon sc->sc_ports[port] = ap; 170258223a3SMatthew Dillon 171258223a3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 172258223a3SMatthew Dillon AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 173258223a3SMatthew Dillon device_printf(sc->sc_dev, 174258223a3SMatthew Dillon "unable to create register window for port %d\n", 175258223a3SMatthew Dillon port); 176258223a3SMatthew Dillon goto freeport; 177258223a3SMatthew Dillon } 178258223a3SMatthew Dillon 179258223a3SMatthew Dillon ap->ap_sc = sc; 180258223a3SMatthew Dillon ap->ap_num = port; 181258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_free); 182258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_pending); 183258223a3SMatthew Dillon lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0); 184258223a3SMatthew Dillon 185258223a3SMatthew Dillon /* Disable port interrupts */ 186258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 187258223a3SMatthew Dillon 188258223a3SMatthew Dillon /* Sec 10.1.2 - deinitialise port if it is already running */ 189258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 190258223a3SMatthew Dillon if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR | 191258223a3SMatthew Dillon AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) || 192258223a3SMatthew Dillon (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) { 193258223a3SMatthew Dillon int r; 194258223a3SMatthew Dillon 195258223a3SMatthew Dillon r = ahci_port_stop(ap, 1); 196258223a3SMatthew Dillon if (r) { 197258223a3SMatthew Dillon device_printf(sc->sc_dev, 198258223a3SMatthew Dillon "unable to disable %s, ignoring port %d\n", 199258223a3SMatthew Dillon ((r == 2) ? "CR" : "FR"), port); 200258223a3SMatthew Dillon rc = ENXIO; 201258223a3SMatthew Dillon goto freeport; 202258223a3SMatthew Dillon } 203258223a3SMatthew Dillon 204258223a3SMatthew Dillon /* Write DET to zero */ 205258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, 0); 206258223a3SMatthew Dillon } 207258223a3SMatthew Dillon 208258223a3SMatthew Dillon /* Allocate RFIS */ 209258223a3SMatthew Dillon ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis); 210258223a3SMatthew Dillon if (ap->ap_dmamem_rfis == NULL) { 211258223a3SMatthew Dillon kprintf("NORFIS\n"); 212258223a3SMatthew Dillon goto nomem; 213258223a3SMatthew Dillon } 214258223a3SMatthew Dillon 215258223a3SMatthew Dillon /* Setup RFIS base address */ 216258223a3SMatthew Dillon ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis); 217258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis); 218258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32)); 219258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva); 220258223a3SMatthew Dillon 221258223a3SMatthew Dillon /* Enable FIS reception and activate port. */ 222258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 223258223a3SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 224258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 225258223a3SMatthew Dillon 226258223a3SMatthew Dillon /* Check whether port activated. Skip it if not. */ 227258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 228258223a3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 229258223a3SMatthew Dillon kprintf("NOT-ACTIVATED\n"); 230258223a3SMatthew Dillon rc = ENXIO; 231258223a3SMatthew Dillon goto freeport; 232258223a3SMatthew Dillon } 233258223a3SMatthew Dillon 234258223a3SMatthew Dillon /* Allocate a CCB for each command slot */ 235258223a3SMatthew Dillon ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF, 236258223a3SMatthew Dillon M_WAITOK | M_ZERO); 237258223a3SMatthew Dillon if (ap->ap_ccbs == NULL) { 238258223a3SMatthew Dillon device_printf(sc->sc_dev, 239258223a3SMatthew Dillon "unable to allocate command list for port %d\n", 240258223a3SMatthew Dillon port); 241258223a3SMatthew Dillon goto freeport; 242258223a3SMatthew Dillon } 243258223a3SMatthew Dillon 244258223a3SMatthew Dillon /* Command List Structures and Command Tables */ 245258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh); 246258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt); 247258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table == NULL || 248258223a3SMatthew Dillon ap->ap_dmamem_cmd_list == NULL) { 249258223a3SMatthew Dillon nomem: 250258223a3SMatthew Dillon device_printf(sc->sc_dev, 251258223a3SMatthew Dillon "unable to allocate DMA memory for port %d\n", 252258223a3SMatthew Dillon port); 253258223a3SMatthew Dillon goto freeport; 254258223a3SMatthew Dillon } 255258223a3SMatthew Dillon 256258223a3SMatthew Dillon /* Setup command list base address */ 257258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list); 258258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32)); 259258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva); 260258223a3SMatthew Dillon 261258223a3SMatthew Dillon /* Split CCB allocation into CCBs and assign to command header/table */ 262258223a3SMatthew Dillon hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list); 263258223a3SMatthew Dillon table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table); 264258223a3SMatthew Dillon for (i = 0; i < sc->sc_ncmds; i++) { 265258223a3SMatthew Dillon ccb = &ap->ap_ccbs[i]; 266258223a3SMatthew Dillon 267258223a3SMatthew Dillon error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW, 268258223a3SMatthew Dillon &ccb->ccb_dmamap); 269258223a3SMatthew Dillon if (error) { 270258223a3SMatthew Dillon device_printf(sc->sc_dev, 271258223a3SMatthew Dillon "unable to create dmamap for port %d " 272258223a3SMatthew Dillon "ccb %d\n", port, i); 273258223a3SMatthew Dillon goto freeport; 274258223a3SMatthew Dillon } 275258223a3SMatthew Dillon 276258223a3SMatthew Dillon callout_init(&ccb->ccb_timeout); 277258223a3SMatthew Dillon ccb->ccb_slot = i; 278258223a3SMatthew Dillon ccb->ccb_port = ap; 279258223a3SMatthew Dillon ccb->ccb_cmd_hdr = &hdr[i]; 280258223a3SMatthew Dillon ccb->ccb_cmd_table = &table[i]; 281258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) + 282258223a3SMatthew Dillon ccb->ccb_slot * sizeof(struct ahci_cmd_table); 283258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32)); 284258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva); 285258223a3SMatthew Dillon 286258223a3SMatthew Dillon ccb->ccb_xa.fis = 287258223a3SMatthew Dillon (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 288258223a3SMatthew Dillon ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd; 289258223a3SMatthew Dillon ccb->ccb_xa.tag = i; 290258223a3SMatthew Dillon 291258223a3SMatthew Dillon ccb->ccb_xa.ata_put_xfer = ahci_ata_put_xfer; 292258223a3SMatthew Dillon 293258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 294258223a3SMatthew Dillon ahci_put_ccb(ccb); 295258223a3SMatthew Dillon } 296258223a3SMatthew Dillon 297258223a3SMatthew Dillon /* Wait for ICC change to complete */ 298258223a3SMatthew Dillon ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC); 299258223a3SMatthew Dillon 300fd8bd957SMatthew Dillon /* 301fd8bd957SMatthew Dillon * Do device-related port initialization. A failure here does not 302fd8bd957SMatthew Dillon * cause the port to be deallocated as we want to receive future 303fd8bd957SMatthew Dillon * hot-plug events. 304fd8bd957SMatthew Dillon */ 305fd8bd957SMatthew Dillon ahci_port_init(ap); 306fd8bd957SMatthew Dillon return(0); 307fd8bd957SMatthew Dillon freeport: 308fd8bd957SMatthew Dillon ahci_port_free(sc, port); 309fd8bd957SMatthew Dillon reterr: 310fd8bd957SMatthew Dillon return (rc); 311fd8bd957SMatthew Dillon } 312fd8bd957SMatthew Dillon 313fd8bd957SMatthew Dillon /* 314fd8bd957SMatthew Dillon * [re]initialize an idle port. No CCBs should be active. 315fd8bd957SMatthew Dillon * 316fd8bd957SMatthew Dillon * This function is called during the initial port allocation sequence 317fd8bd957SMatthew Dillon * and is also called on hot-plug insertion. We take no chances and 318fd8bd957SMatthew Dillon * use a portreset instead of a softreset. 319fd8bd957SMatthew Dillon * 320fd8bd957SMatthew Dillon * Returns 0 if a device is successfully detected. 321fd8bd957SMatthew Dillon */ 322fd8bd957SMatthew Dillon int 323fd8bd957SMatthew Dillon ahci_port_init(struct ahci_port *ap) 324fd8bd957SMatthew Dillon { 325fd8bd957SMatthew Dillon int rc; 326fd8bd957SMatthew Dillon 327fd8bd957SMatthew Dillon /* 328fd8bd957SMatthew Dillon * Hard-reset the port. 329fd8bd957SMatthew Dillon */ 330258223a3SMatthew Dillon rc = ahci_port_portreset(ap); 331fd8bd957SMatthew Dillon 332258223a3SMatthew Dillon switch (rc) { 333258223a3SMatthew Dillon case ENODEV: 334fd8bd957SMatthew Dillon /* 335fd8bd957SMatthew Dillon * We had problems talking to the device on the port. 336fd8bd957SMatthew Dillon */ 337258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 338258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 339419cb1abSMatthew Dillon kprintf("%s: Device not communicating\n", PORTNAME(ap)); 340258223a3SMatthew Dillon break; 341258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_PHYOFFLINE: 342419cb1abSMatthew Dillon kprintf("%s: PHY offline\n", PORTNAME(ap)); 343258223a3SMatthew Dillon break; 344258223a3SMatthew Dillon default: 345419cb1abSMatthew Dillon kprintf("%s: No device detected\n", PORTNAME(ap)); 346258223a3SMatthew Dillon break; 347258223a3SMatthew Dillon } 348258223a3SMatthew Dillon break; 349258223a3SMatthew Dillon 350258223a3SMatthew Dillon case EBUSY: 351fd8bd957SMatthew Dillon /* 352fd8bd957SMatthew Dillon * The device on the port is still telling us its busy. 353fd8bd957SMatthew Dillon * 354fd8bd957SMatthew Dillon * We try a softreset on the device. 355fd8bd957SMatthew Dillon */ 356fd8bd957SMatthew Dillon kprintf("%s: Device on port did not come ready, TFD: 0x%b\n", 357fd8bd957SMatthew Dillon PORTNAME(ap), 358258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 359258223a3SMatthew Dillon 360258223a3SMatthew Dillon /* Try a soft reset to clear busy */ 361258223a3SMatthew Dillon rc = ahci_port_softreset(ap); 362258223a3SMatthew Dillon if (rc) { 363fd8bd957SMatthew Dillon kprintf("%s: Unable to clear busy device\n", 364fd8bd957SMatthew Dillon PORTNAME(ap)); 365fd8bd957SMatthew Dillon } else { 366fd8bd957SMatthew Dillon kprintf("%s: Successfully reset busy device\n", 367fd8bd957SMatthew Dillon PORTNAME(ap)); 368258223a3SMatthew Dillon } 369258223a3SMatthew Dillon break; 370258223a3SMatthew Dillon 371258223a3SMatthew Dillon default: 372258223a3SMatthew Dillon break; 373258223a3SMatthew Dillon } 374258223a3SMatthew Dillon 375258223a3SMatthew Dillon /* 376258223a3SMatthew Dillon * Enable command transfers on the port if a device was detected. 377258223a3SMatthew Dillon * Otherwise leave them disabled but leave the port structure 378258223a3SMatthew Dillon * intact so we get hot-plug interrupts. 379258223a3SMatthew Dillon */ 380258223a3SMatthew Dillon if (rc == 0) { 381258223a3SMatthew Dillon if (ahci_port_start(ap, 0)) { 382fd8bd957SMatthew Dillon kprintf("%s: failed to start command DMA on port, " 383fd8bd957SMatthew Dillon "disabling\n", PORTNAME(ap)); 384258223a3SMatthew Dillon rc = ENXIO; /* couldn't start port */ 385258223a3SMatthew Dillon } 386258223a3SMatthew Dillon } 387258223a3SMatthew Dillon 388258223a3SMatthew Dillon /* Flush interrupts for port */ 389258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 390fd8bd957SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num); 391258223a3SMatthew Dillon 392258223a3SMatthew Dillon /* Enable port interrupts */ 393258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 394258223a3SMatthew Dillon AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE | 395258223a3SMatthew Dillon AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE | 396258223a3SMatthew Dillon AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE | 397258223a3SMatthew Dillon AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE | 398258223a3SMatthew Dillon #ifdef AHCI_COALESCE 399258223a3SMatthew Dillon ((sc->sc_ccc_ports & (1 << port)) ? 400258223a3SMatthew Dillon 0 : (AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE)) 401258223a3SMatthew Dillon #else 402258223a3SMatthew Dillon AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE 403258223a3SMatthew Dillon #endif 404258223a3SMatthew Dillon ); 405258223a3SMatthew Dillon return(rc); 406258223a3SMatthew Dillon } 407258223a3SMatthew Dillon 408fd8bd957SMatthew Dillon /* 409fd8bd957SMatthew Dillon * De-initialize and detach a port. 410fd8bd957SMatthew Dillon */ 411258223a3SMatthew Dillon void 412258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 413258223a3SMatthew Dillon { 414258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 415258223a3SMatthew Dillon struct ahci_ccb *ccb; 416258223a3SMatthew Dillon 417258223a3SMatthew Dillon /* Ensure port is disabled and its interrupts are flushed */ 418258223a3SMatthew Dillon if (ap->ap_sc) { 419258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 420258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 421258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 422258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 423258223a3SMatthew Dillon } 424258223a3SMatthew Dillon 425258223a3SMatthew Dillon if (ap->ap_ccbs) { 426258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 427258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 428258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 429258223a3SMatthew Dillon ccb->ccb_dmamap); 430258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 431258223a3SMatthew Dillon } 432258223a3SMatthew Dillon } 433258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 434258223a3SMatthew Dillon ap->ap_ccbs = NULL; 435258223a3SMatthew Dillon } 436258223a3SMatthew Dillon 437258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 438258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 439258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 440258223a3SMatthew Dillon } 441258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 442258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 443258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 444258223a3SMatthew Dillon } 445258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 446258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 447258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 448258223a3SMatthew Dillon } 449258223a3SMatthew Dillon 450258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 451258223a3SMatthew Dillon 452258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 453258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 454258223a3SMatthew Dillon } 455258223a3SMatthew Dillon 456fd8bd957SMatthew Dillon /* 457fd8bd957SMatthew Dillon * Start high-level command processing on the port 458fd8bd957SMatthew Dillon */ 459258223a3SMatthew Dillon int 460258223a3SMatthew Dillon ahci_port_start(struct ahci_port *ap, int fre_only) 461258223a3SMatthew Dillon { 462258223a3SMatthew Dillon u_int32_t r; 463258223a3SMatthew Dillon 464258223a3SMatthew Dillon /* Turn on FRE (and ST) */ 465258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 466258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 467258223a3SMatthew Dillon if (!fre_only) 468258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 469258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 470258223a3SMatthew Dillon 471258223a3SMatthew Dillon #ifdef AHCI_COALESCE 472258223a3SMatthew Dillon /* (Re-)enable coalescing on the port. */ 473258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 474258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 475258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 476258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 477258223a3SMatthew Dillon } 478258223a3SMatthew Dillon #endif 479258223a3SMatthew Dillon 480258223a3SMatthew Dillon if (!(ap->ap_sc->sc_flags & AHCI_F_IGN_FR)) { 481258223a3SMatthew Dillon /* Wait for FR to come on */ 482258223a3SMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) 483258223a3SMatthew Dillon return (2); 484258223a3SMatthew Dillon } 485258223a3SMatthew Dillon 486258223a3SMatthew Dillon /* Wait for CR to come on */ 487258223a3SMatthew Dillon if (!fre_only && ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) 488258223a3SMatthew Dillon return (1); 489258223a3SMatthew Dillon 490258223a3SMatthew Dillon return (0); 491258223a3SMatthew Dillon } 492258223a3SMatthew Dillon 493fd8bd957SMatthew Dillon /* 494fd8bd957SMatthew Dillon * Stop high-level command processing on a port 495fd8bd957SMatthew Dillon */ 496258223a3SMatthew Dillon int 497258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 498258223a3SMatthew Dillon { 499258223a3SMatthew Dillon u_int32_t r; 500258223a3SMatthew Dillon 501258223a3SMatthew Dillon #ifdef AHCI_COALESCE 502258223a3SMatthew Dillon /* Disable coalescing on the port while it is stopped. */ 503258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 504258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 505258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 506258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 507258223a3SMatthew Dillon } 508258223a3SMatthew Dillon #endif 509258223a3SMatthew Dillon 510258223a3SMatthew Dillon /* Turn off ST (and FRE) */ 511258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 512258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 513258223a3SMatthew Dillon if (stop_fis_rx) 514258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 515258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 516258223a3SMatthew Dillon 517258223a3SMatthew Dillon /* Wait for CR to go off */ 518258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) 519258223a3SMatthew Dillon return (1); 520258223a3SMatthew Dillon 521258223a3SMatthew Dillon /* Wait for FR to go off */ 522258223a3SMatthew Dillon if (stop_fis_rx && ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) 523258223a3SMatthew Dillon return (2); 524258223a3SMatthew Dillon 525258223a3SMatthew Dillon return (0); 526258223a3SMatthew Dillon } 527258223a3SMatthew Dillon 528fd8bd957SMatthew Dillon /* 529fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 530fd8bd957SMatthew Dillon */ 531258223a3SMatthew Dillon int 532258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 533258223a3SMatthew Dillon { 534258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 535258223a3SMatthew Dillon u_int32_t cmd; 536258223a3SMatthew Dillon 537258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 538258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 539258223a3SMatthew Dillon return (1); 540258223a3SMatthew Dillon 541258223a3SMatthew Dillon /* Issue CLO */ 542258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 543258223a3SMatthew Dillon #ifdef DIAGNOSTIC 544258223a3SMatthew Dillon if (cmd & AHCI_PREG_CMD_ST) { 545258223a3SMatthew Dillon kprintf("%s: CLO requested while port running\n", 546258223a3SMatthew Dillon PORTNAME(ap)); 547258223a3SMatthew Dillon } 548258223a3SMatthew Dillon #endif 549258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 550258223a3SMatthew Dillon 551258223a3SMatthew Dillon /* Wait for completion */ 552258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 553258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 554258223a3SMatthew Dillon return (1); 555258223a3SMatthew Dillon } 556258223a3SMatthew Dillon 557258223a3SMatthew Dillon return (0); 558258223a3SMatthew Dillon } 559258223a3SMatthew Dillon 560fd8bd957SMatthew Dillon /* 561fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 562fd8bd957SMatthew Dillon * 563fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 564fd8bd957SMatthew Dillon * a reset to the connected device. 565fd8bd957SMatthew Dillon */ 566258223a3SMatthew Dillon int 567258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 568258223a3SMatthew Dillon { 569258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 570258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 571258223a3SMatthew Dillon u_int8_t *fis; 572258223a3SMatthew Dillon int rc = EIO; 573258223a3SMatthew Dillon u_int32_t cmd; 574258223a3SMatthew Dillon 575258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 576258223a3SMatthew Dillon 577258223a3SMatthew Dillon crit_enter(); 578258223a3SMatthew Dillon 579258223a3SMatthew Dillon /* Save previous command register state */ 580258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 581258223a3SMatthew Dillon 582258223a3SMatthew Dillon /* Idle port */ 583258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 584258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 585258223a3SMatthew Dillon PORTNAME(ap)); 586258223a3SMatthew Dillon goto err; 587258223a3SMatthew Dillon } 588258223a3SMatthew Dillon 589258223a3SMatthew Dillon /* Request CLO if device appears hung */ 590258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 591258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 592258223a3SMatthew Dillon ahci_port_clo(ap); 593258223a3SMatthew Dillon } 594258223a3SMatthew Dillon 595258223a3SMatthew Dillon /* Clear port errors to permit TFD transfer */ 596258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR)); 597258223a3SMatthew Dillon 598258223a3SMatthew Dillon /* Restart port */ 599258223a3SMatthew Dillon if (ahci_port_start(ap, 0)) { 600258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 601258223a3SMatthew Dillon PORTNAME(ap)); 602258223a3SMatthew Dillon goto err; 603258223a3SMatthew Dillon } 604258223a3SMatthew Dillon 605258223a3SMatthew Dillon /* Check whether CLO worked */ 606258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 607258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 608258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 609258223a3SMatthew Dillon PORTNAME(ap), 610258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 611258223a3SMatthew Dillon ? "failed" : "unsupported"); 612258223a3SMatthew Dillon rc = EBUSY; 613258223a3SMatthew Dillon goto err; 614258223a3SMatthew Dillon } 615258223a3SMatthew Dillon 616cec85a37SMatthew Dillon /* 617cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 618cec85a37SMatthew Dillon * 619cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 620cec85a37SMatthew Dillon * everything. 621cec85a37SMatthew Dillon */ 622258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 623258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 624258223a3SMatthew Dillon bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table)); 625258223a3SMatthew Dillon 626258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 627cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 628258223a3SMatthew Dillon fis[0] = 0x27; /* Host to device */ 629258223a3SMatthew Dillon fis[15] = 0x04; /* SRST DEVCTL */ 630258223a3SMatthew Dillon 631258223a3SMatthew Dillon cmd_slot->prdtl = 0; 632258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 633258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 634258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 635258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); /* Write */ 636258223a3SMatthew Dillon 637258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 6385f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 639cec85a37SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0) { 6405f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 641258223a3SMatthew Dillon goto err; 642cec85a37SMatthew Dillon } 643258223a3SMatthew Dillon 644cec85a37SMatthew Dillon /* 645cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 646cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 647cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 648cec85a37SMatthew Dillon * from the AHCI document. 649cec85a37SMatthew Dillon * 650cec85a37SMatthew Dillon * Give the device 1/10 of a second before sending the second 651cec85a37SMatthew Dillon * FIS. 652cec85a37SMatthew Dillon * 653cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 654cec85a37SMatthew Dillon * everything. 655cec85a37SMatthew Dillon */ 656cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 657258223a3SMatthew Dillon fis[0] = 0x27; /* Host to device */ 658258223a3SMatthew Dillon fis[15] = 0; 659258223a3SMatthew Dillon 660258223a3SMatthew Dillon cmd_slot->prdtl = 0; 661258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 662258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 663258223a3SMatthew Dillon 664258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 6655f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 666cec85a37SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0) { 6675f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 668258223a3SMatthew Dillon goto err; 669cec85a37SMatthew Dillon } 670258223a3SMatthew Dillon 671258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY | 672258223a3SMatthew Dillon AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) { 673258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 674258223a3SMatthew Dillon PORTNAME(ap), 675258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 676258223a3SMatthew Dillon rc = EBUSY; 677258223a3SMatthew Dillon goto err; 678258223a3SMatthew Dillon } 679258223a3SMatthew Dillon 680fd8bd957SMatthew Dillon /* 681fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 682fd8bd957SMatthew Dillon * normal portreset we assign the port type. 683fd8bd957SMatthew Dillon * 684fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 685fd8bd957SMatthew Dillon * processing code then report if the device signature changed 686fd8bd957SMatthew Dillon * unexpectedly. 687fd8bd957SMatthew Dillon */ 688fd8bd957SMatthew Dillon if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) { 689fd8bd957SMatthew Dillon ap->ap_ata.ap_type = ahci_port_signature_detect(ap); 690fd8bd957SMatthew Dillon } else { 691fd8bd957SMatthew Dillon if (ahci_port_signature_detect(ap) != ap->ap_ata.ap_type) { 692fd8bd957SMatthew Dillon kprintf("%s: device signature unexpectedly changed\n", 693fd8bd957SMatthew Dillon PORTNAME(ap)); 694fd8bd957SMatthew Dillon rc = EBUSY; 695fd8bd957SMatthew Dillon } 696fd8bd957SMatthew Dillon } 697fd8bd957SMatthew Dillon 698258223a3SMatthew Dillon rc = 0; 699258223a3SMatthew Dillon err: 700258223a3SMatthew Dillon if (ccb != NULL) { 701258223a3SMatthew Dillon /* Abort our command, if it failed, by stopping command DMA. */ 702258223a3SMatthew Dillon if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) { 703258223a3SMatthew Dillon kprintf("%s: stopping the port, softreset slot " 704258223a3SMatthew Dillon "%d was still active.\n", 705258223a3SMatthew Dillon PORTNAME(ap), 706258223a3SMatthew Dillon ccb->ccb_slot); 707258223a3SMatthew Dillon ahci_port_stop(ap, 0); 708258223a3SMatthew Dillon } 709258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 710258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 711258223a3SMatthew Dillon } 712258223a3SMatthew Dillon 713258223a3SMatthew Dillon /* Restore saved CMD register state */ 714258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 715258223a3SMatthew Dillon 716258223a3SMatthew Dillon crit_exit(); 717258223a3SMatthew Dillon 718258223a3SMatthew Dillon return (rc); 719258223a3SMatthew Dillon } 720258223a3SMatthew Dillon 721fd8bd957SMatthew Dillon /* 722fd8bd957SMatthew Dillon * AHCI port reset, Section 10.4.2 723fd8bd957SMatthew Dillon * 724fd8bd957SMatthew Dillon * This function does a hard reset of the port. Note that the device 725fd8bd957SMatthew Dillon * connected to the port could still end-up hung. 726fd8bd957SMatthew Dillon */ 727258223a3SMatthew Dillon int 728258223a3SMatthew Dillon ahci_port_portreset(struct ahci_port *ap) 729258223a3SMatthew Dillon { 730258223a3SMatthew Dillon u_int32_t cmd, r; 731258223a3SMatthew Dillon int rc; 732258223a3SMatthew Dillon 733258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap)); 734258223a3SMatthew Dillon 735258223a3SMatthew Dillon /* Save previous command register state */ 736258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 737258223a3SMatthew Dillon 738258223a3SMatthew Dillon /* Clear ST, ignoring failure */ 739258223a3SMatthew Dillon ahci_port_stop(ap, 0); 740258223a3SMatthew Dillon 741258223a3SMatthew Dillon /* Perform device detection */ 742258223a3SMatthew Dillon ap->ap_ata.ap_type = ATA_PORT_T_NONE; 743258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, 0); 744258223a3SMatthew Dillon DELAY(10000); 745258223a3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 746258223a3SMatthew Dillon 747258223a3SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 748258223a3SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 749258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 750258223a3SMatthew Dillon } else { 751258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 752258223a3SMatthew Dillon } 753258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 754258223a3SMatthew Dillon DELAY(10000); /* wait at least 1ms for COMRESET to be sent */ 755258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 756258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 757258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 758258223a3SMatthew Dillon DELAY(10000); 759258223a3SMatthew Dillon 760258223a3SMatthew Dillon /* Wait for device to be detected and communications established */ 761cec85a37SMatthew Dillon if (ahci_pwait_eq(ap, 1000, 762cec85a37SMatthew Dillon AHCI_PREG_SSTS, AHCI_PREG_SSTS_DET, 763258223a3SMatthew Dillon AHCI_PREG_SSTS_DET_DEV)) { 764258223a3SMatthew Dillon rc = ENODEV; 765258223a3SMatthew Dillon goto err; 766258223a3SMatthew Dillon } 767258223a3SMatthew Dillon 768258223a3SMatthew Dillon /* Clear SERR (incl X bit), so TFD can update */ 769258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR)); 770258223a3SMatthew Dillon 771cec85a37SMatthew Dillon /* 772cec85a37SMatthew Dillon * Wait for device to become ready 773cec85a37SMatthew Dillon * 774cec85a37SMatthew Dillon * This can take more then a second, give it 3 seconds. 775cec85a37SMatthew Dillon */ 776cec85a37SMatthew Dillon if (ahci_pwait_clr_to(ap, 3000, 777cec85a37SMatthew Dillon AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY | 778258223a3SMatthew Dillon AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) { 779258223a3SMatthew Dillon rc = EBUSY; 780258223a3SMatthew Dillon kprintf("%s: Device will not come ready 0x%b\n", 781258223a3SMatthew Dillon PORTNAME(ap), 782258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 783258223a3SMatthew Dillon goto err; 784258223a3SMatthew Dillon } 785258223a3SMatthew Dillon 786fd8bd957SMatthew Dillon ap->ap_ata.ap_type = ahci_port_signature_detect(ap); 787258223a3SMatthew Dillon rc = 0; 788258223a3SMatthew Dillon err: 789258223a3SMatthew Dillon /* Restore preserved port state */ 790258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 791258223a3SMatthew Dillon 792258223a3SMatthew Dillon return (rc); 793258223a3SMatthew Dillon } 794258223a3SMatthew Dillon 795fd8bd957SMatthew Dillon /* 796fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 797fd8bd957SMatthew Dillon * DISK. 798fd8bd957SMatthew Dillon */ 799fd8bd957SMatthew Dillon int 800fd8bd957SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap) 801fd8bd957SMatthew Dillon { 802fd8bd957SMatthew Dillon u_int32_t sig; 803fd8bd957SMatthew Dillon 804fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 805fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 806fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 807fd8bd957SMatthew Dillon } else { 808fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 809fd8bd957SMatthew Dillon } 810fd8bd957SMatthew Dillon } 811fd8bd957SMatthew Dillon 812fd8bd957SMatthew Dillon /* 813fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 814fd8bd957SMatthew Dillon */ 815258223a3SMatthew Dillon int 816258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 817258223a3SMatthew Dillon { 818258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 819258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 820258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 821258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 822258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 823258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 824258223a3SMatthew Dillon int error; 825258223a3SMatthew Dillon 826258223a3SMatthew Dillon if (xa->datalen == 0) { 827258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 828258223a3SMatthew Dillon return (0); 829258223a3SMatthew Dillon } 830258223a3SMatthew Dillon 831258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 832258223a3SMatthew Dillon xa->data, xa->datalen, 833258223a3SMatthew Dillon ahci_load_prdt_callback, 834258223a3SMatthew Dillon &prdt, 835258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 836258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 837258223a3SMatthew Dillon if (error != 0) { 838258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 839258223a3SMatthew Dillon return (1); 840258223a3SMatthew Dillon } 841258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 842258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 843258223a3SMatthew Dillon 844258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 845258223a3SMatthew Dillon 846258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 847258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 848258223a3SMatthew Dillon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 849258223a3SMatthew Dillon 850258223a3SMatthew Dillon return (0); 851258223a3SMatthew Dillon 852258223a3SMatthew Dillon #ifdef DIAGNOSTIC 853258223a3SMatthew Dillon diagerr: 854258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 855258223a3SMatthew Dillon return (1); 856258223a3SMatthew Dillon #endif 857258223a3SMatthew Dillon } 858258223a3SMatthew Dillon 859258223a3SMatthew Dillon /* 860258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 861258223a3SMatthew Dillon * list is a temporary structure. 862258223a3SMatthew Dillon */ 863258223a3SMatthew Dillon static 864258223a3SMatthew Dillon void 865258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 866258223a3SMatthew Dillon int error) 867258223a3SMatthew Dillon { 868258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 869258223a3SMatthew Dillon u_int64_t addr; 870258223a3SMatthew Dillon 871258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 872258223a3SMatthew Dillon 873258223a3SMatthew Dillon while (nsegs) { 874258223a3SMatthew Dillon addr = segs->ds_addr; 875258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 876258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 877258223a3SMatthew Dillon #ifdef DIAGNOSTIC 878258223a3SMatthew Dillon KKASSERT((addr & 1) == 0); 879258223a3SMatthew Dillon KKASSERT((segs->ds_len & 1) == 0); 880258223a3SMatthew Dillon #endif 881258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 882258223a3SMatthew Dillon --nsegs; 883258223a3SMatthew Dillon if (nsegs) 884258223a3SMatthew Dillon ++prd; 885258223a3SMatthew Dillon ++segs; 886258223a3SMatthew Dillon } 887258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 888258223a3SMatthew Dillon } 889258223a3SMatthew Dillon 890258223a3SMatthew Dillon void 891258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 892258223a3SMatthew Dillon { 893258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 894258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 895258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 896258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 897258223a3SMatthew Dillon 898258223a3SMatthew Dillon if (xa->datalen != 0) { 899258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 900258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 901258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 902258223a3SMatthew Dillon 903258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 904258223a3SMatthew Dillon 905258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) 906258223a3SMatthew Dillon xa->resid = 0; 907258223a3SMatthew Dillon else 908258223a3SMatthew Dillon xa->resid = xa->datalen - 909258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 910258223a3SMatthew Dillon } 911258223a3SMatthew Dillon } 912258223a3SMatthew Dillon 9135f8c1efdSMatthew Dillon /* 9145f8c1efdSMatthew Dillon * Start a command and poll for completion. 9155f8c1efdSMatthew Dillon * 9165f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 9175f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 9185f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 9195f8c1efdSMatthew Dillon * 9205f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 9215f8c1efdSMatthew Dillon */ 922258223a3SMatthew Dillon int 923258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *)) 924258223a3SMatthew Dillon { 925258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 9265f8c1efdSMatthew Dillon u_int32_t slot_mask = 1 << ccb->ccb_slot; 927258223a3SMatthew Dillon 928258223a3SMatthew Dillon crit_enter(); 929258223a3SMatthew Dillon ahci_start(ccb); 930258223a3SMatthew Dillon do { 9315f8c1efdSMatthew Dillon if (ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS) & slot_mask) { 932258223a3SMatthew Dillon crit_exit(); 933258223a3SMatthew Dillon return (0); 934258223a3SMatthew Dillon } 9355f8c1efdSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP && 9365f8c1efdSMatthew Dillon ccb->ccb_xa.state != ATA_S_PENDING) { 9375f8c1efdSMatthew Dillon break; 9385f8c1efdSMatthew Dillon } 939258223a3SMatthew Dillon DELAY(1000000 / hz); 940258223a3SMatthew Dillon } while (--timeout > 0); 941258223a3SMatthew Dillon 9425f8c1efdSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP && 9435f8c1efdSMatthew Dillon ccb->ccb_xa.state != ATA_S_PENDING) { 9445f8c1efdSMatthew Dillon kprintf("%s: Warning poll completed unexpectedly for slot %d\n", 9455f8c1efdSMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 9465f8c1efdSMatthew Dillon crit_exit(); 9475f8c1efdSMatthew Dillon return (0); 9485f8c1efdSMatthew Dillon } 9495f8c1efdSMatthew Dillon 9505f8c1efdSMatthew Dillon kprintf("%s: Poll timed-out for slot %d state %d\n", 9515f8c1efdSMatthew Dillon PORTNAME(ap), ccb->ccb_slot, ccb->ccb_xa.state); 9525f8c1efdSMatthew Dillon 9535f8c1efdSMatthew Dillon if (timeout_fn != NULL) { 954258223a3SMatthew Dillon timeout_fn(ccb); 9555f8c1efdSMatthew Dillon } else { 9565f8c1efdSMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) 9575f8c1efdSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 9585f8c1efdSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 9595f8c1efdSMatthew Dillon } 960258223a3SMatthew Dillon crit_exit(); 961258223a3SMatthew Dillon 962258223a3SMatthew Dillon return (1); 963258223a3SMatthew Dillon } 964258223a3SMatthew Dillon 965258223a3SMatthew Dillon void 966258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 967258223a3SMatthew Dillon { 968258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 969258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 970258223a3SMatthew Dillon 971258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 972258223a3SMatthew Dillon 973258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 974258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 975258223a3SMatthew Dillon 976258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 977258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 978258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 979258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 980258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 981258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 982258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 983258223a3SMatthew Dillon 984258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 985258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 986258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 987258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 988258223a3SMatthew Dillon 989258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 990258223a3SMatthew Dillon /* Issue NCQ commands only when there are no outstanding 991258223a3SMatthew Dillon * standard commands. */ 992258223a3SMatthew Dillon if (ap->ap_active != 0 || !TAILQ_EMPTY(&ap->ap_ccb_pending)) 993258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 994258223a3SMatthew Dillon else { 995258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 996258223a3SMatthew Dillon ap->ap_sactive |= (1 << ccb->ccb_slot); 997258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 998258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot); 999258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1000258223a3SMatthew Dillon } 1001258223a3SMatthew Dillon } else { 10025f8c1efdSMatthew Dillon /* 10035f8c1efdSMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 10045f8c1efdSMatthew Dillon * command. 10055f8c1efdSMatthew Dillon */ 1006258223a3SMatthew Dillon if (ap->ap_sactive != 0 || ap->ap_active_cnt == 2) 1007258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 1008258223a3SMatthew Dillon else if (ap->ap_active_cnt < 2) { 1009258223a3SMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 1010258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1011258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1012258223a3SMatthew Dillon ap->ap_active_cnt++; 1013258223a3SMatthew Dillon } 1014258223a3SMatthew Dillon } 1015258223a3SMatthew Dillon } 1016258223a3SMatthew Dillon 1017258223a3SMatthew Dillon void 1018258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap) 1019258223a3SMatthew Dillon { 1020258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1021258223a3SMatthew Dillon u_int32_t sact_change = 0; 1022258223a3SMatthew Dillon 1023258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1024258223a3SMatthew Dillon 1025258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1026258223a3SMatthew Dillon if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ)) 1027258223a3SMatthew Dillon return; 1028258223a3SMatthew Dillon 1029258223a3SMatthew Dillon /* Start all the NCQ commands at the head of the pending list. */ 1030258223a3SMatthew Dillon do { 1031258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 1032258223a3SMatthew Dillon sact_change |= 1 << nextccb->ccb_slot; 1033258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1034258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1035258223a3SMatthew Dillon } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)); 1036258223a3SMatthew Dillon 1037258223a3SMatthew Dillon ap->ap_sactive |= sact_change; 1038258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, sact_change); 1039258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, sact_change); 1040258223a3SMatthew Dillon 1041258223a3SMatthew Dillon return; 1042258223a3SMatthew Dillon } 1043258223a3SMatthew Dillon 1044258223a3SMatthew Dillon void 1045258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq) 1046258223a3SMatthew Dillon { 1047258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1048258223a3SMatthew Dillon 1049258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1050258223a3SMatthew Dillon if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) { 1051258223a3SMatthew Dillon KKASSERT(last_was_ncq == 0); /* otherwise it should have 1052258223a3SMatthew Dillon * been started already. */ 1053258223a3SMatthew Dillon 1054258223a3SMatthew Dillon /* Issue NCQ commands only when there are no outstanding 1055258223a3SMatthew Dillon * standard commands. */ 1056258223a3SMatthew Dillon ap->ap_active_cnt--; 1057258223a3SMatthew Dillon if (ap->ap_active == 0) 1058258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 1059258223a3SMatthew Dillon else 1060258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 1); 1061258223a3SMatthew Dillon } else if (nextccb) { 1062258223a3SMatthew Dillon if (ap->ap_sactive != 0 || last_was_ncq) 1063258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1064258223a3SMatthew Dillon 1065258223a3SMatthew Dillon /* Wait for all NCQ commands to finish before issuing standard 1066258223a3SMatthew Dillon * command. */ 1067258223a3SMatthew Dillon if (ap->ap_sactive != 0) 1068258223a3SMatthew Dillon return; 1069258223a3SMatthew Dillon 1070258223a3SMatthew Dillon /* Keep up to 2 standard commands on-chip at a time. */ 1071258223a3SMatthew Dillon do { 1072258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 1073258223a3SMatthew Dillon ap->ap_active |= 1 << nextccb->ccb_slot; 1074258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1075258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot); 1076258223a3SMatthew Dillon if (last_was_ncq) 1077258223a3SMatthew Dillon ap->ap_active_cnt++; 1078258223a3SMatthew Dillon if (ap->ap_active_cnt == 2) 1079258223a3SMatthew Dillon break; 1080258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 1); 1081258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1082258223a3SMatthew Dillon } while (nextccb && !(nextccb->ccb_xa.flags & ATA_F_NCQ)); 1083258223a3SMatthew Dillon } else if (!last_was_ncq) { 1084258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 1 || ap->ap_active_cnt == 2); 1085258223a3SMatthew Dillon 1086258223a3SMatthew Dillon /* Standard command finished, none waiting to start. */ 1087258223a3SMatthew Dillon ap->ap_active_cnt--; 1088258223a3SMatthew Dillon } else { 1089258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1090258223a3SMatthew Dillon 1091258223a3SMatthew Dillon /* NCQ command finished. */ 1092258223a3SMatthew Dillon } 1093258223a3SMatthew Dillon } 1094258223a3SMatthew Dillon 1095258223a3SMatthew Dillon void 1096258223a3SMatthew Dillon ahci_intr(void *arg) 1097258223a3SMatthew Dillon { 1098258223a3SMatthew Dillon struct ahci_softc *sc = arg; 1099258223a3SMatthew Dillon u_int32_t is, ack = 0; 1100258223a3SMatthew Dillon int port; 1101258223a3SMatthew Dillon 1102258223a3SMatthew Dillon /* Read global interrupt status */ 1103258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 1104258223a3SMatthew Dillon if (is == 0 || is == 0xffffffff) 1105258223a3SMatthew Dillon return; 1106258223a3SMatthew Dillon ack = is; 1107258223a3SMatthew Dillon 1108258223a3SMatthew Dillon #ifdef AHCI_COALESCE 1109258223a3SMatthew Dillon /* Check coalescing interrupt first */ 1110258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 1111258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 1112258223a3SMatthew Dillon DEVNAME(sc)); 1113258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 1114258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 1115258223a3SMatthew Dillon } 1116258223a3SMatthew Dillon #endif 1117258223a3SMatthew Dillon 1118258223a3SMatthew Dillon /* Process interrupts for each port */ 1119258223a3SMatthew Dillon while (is) { 1120258223a3SMatthew Dillon port = ffs(is) - 1; 1121258223a3SMatthew Dillon if (sc->sc_ports[port]) { 1122258223a3SMatthew Dillon ahci_port_intr(sc->sc_ports[port], 1123258223a3SMatthew Dillon AHCI_PREG_CI_ALL_SLOTS); 1124258223a3SMatthew Dillon } 1125258223a3SMatthew Dillon is &= ~(1 << port); 1126258223a3SMatthew Dillon } 1127258223a3SMatthew Dillon 1128258223a3SMatthew Dillon /* Finally, acknowledge global interrupt */ 1129258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, ack); 1130258223a3SMatthew Dillon } 1131258223a3SMatthew Dillon 1132258223a3SMatthew Dillon u_int32_t 1133258223a3SMatthew Dillon ahci_port_intr(struct ahci_port *ap, u_int32_t ci_mask) 1134258223a3SMatthew Dillon { 1135258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1136258223a3SMatthew Dillon u_int32_t is, ci_saved, ci_masked, processed = 0; 1137258223a3SMatthew Dillon int slot, need_restart = 0; 1138258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 1139258223a3SMatthew Dillon volatile u_int32_t *active; 1140258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1141258223a3SMatthew Dillon u_int32_t tmp; 1142258223a3SMatthew Dillon #endif 1143258223a3SMatthew Dillon 1144258223a3SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 1145258223a3SMatthew Dillon 1146258223a3SMatthew Dillon /* Ack port interrupt only if checking all command slots. */ 1147258223a3SMatthew Dillon if (ci_mask == AHCI_PREG_CI_ALL_SLOTS) 1148258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is); 1149258223a3SMatthew Dillon 1150258223a3SMatthew Dillon if (is) 1151258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: interrupt: %b\n", PORTNAME(ap), 1152258223a3SMatthew Dillon is, AHCI_PFMT_IS); 1153258223a3SMatthew Dillon 1154258223a3SMatthew Dillon if (ap->ap_sactive) { 1155258223a3SMatthew Dillon /* Active NCQ commands - use SActive instead of CI */ 1156258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 1157258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1158258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 1159258223a3SMatthew Dillon active = &ap->ap_sactive; 1160258223a3SMatthew Dillon } else { 1161258223a3SMatthew Dillon /* Save CI */ 1162258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 1163258223a3SMatthew Dillon active = &ap->ap_active; 1164258223a3SMatthew Dillon } 1165258223a3SMatthew Dillon 1166258223a3SMatthew Dillon /* Command failed. See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. */ 1167258223a3SMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 1168258223a3SMatthew Dillon u_int32_t tfd, serr; 1169258223a3SMatthew Dillon int err_slot; 1170258223a3SMatthew Dillon 1171258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 1172258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 1173258223a3SMatthew Dillon 1174258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 1175258223a3SMatthew Dillon /* Errored slot is easy to determine from CMD. */ 1176258223a3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, 1177258223a3SMatthew Dillon AHCI_PREG_CMD)); 1178258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 1179258223a3SMatthew Dillon 1180258223a3SMatthew Dillon /* Preserve received taskfile data from the RFIS. */ 1181258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 1182258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 1183258223a3SMatthew Dillon } else 1184258223a3SMatthew Dillon err_slot = -1; /* Must extract error from log page */ 1185258223a3SMatthew Dillon 1186258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: errored slot %d, TFD: %b, SERR:" 1187258223a3SMatthew Dillon " %b, DIAG: %b\n", PORTNAME(ap), err_slot, tfd, 1188258223a3SMatthew Dillon AHCI_PFMT_TFD_STS, AHCI_PREG_SERR_ERR(serr), 1189258223a3SMatthew Dillon AHCI_PFMT_SERR_ERR, AHCI_PREG_SERR_DIAG(serr), 1190258223a3SMatthew Dillon AHCI_PFMT_SERR_DIAG); 1191258223a3SMatthew Dillon 1192258223a3SMatthew Dillon /* Turn off ST to clear CI and SACT. */ 1193258223a3SMatthew Dillon ahci_port_stop(ap, 0); 1194258223a3SMatthew Dillon need_restart = 1; 1195258223a3SMatthew Dillon 1196258223a3SMatthew Dillon /* Clear SERR to enable capturing new errors. */ 1197258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 1198258223a3SMatthew Dillon 1199258223a3SMatthew Dillon /* Acknowledge the interrupts we can recover from. */ 1200258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES | 1201258223a3SMatthew Dillon AHCI_PREG_IS_IFS); 1202258223a3SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 1203258223a3SMatthew Dillon 1204258223a3SMatthew Dillon /* If device hasn't cleared its busy status, try to idle it. */ 1205258223a3SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1206258223a3SMatthew Dillon kprintf("%s: attempting to idle device\n", 1207258223a3SMatthew Dillon PORTNAME(ap)); 1208258223a3SMatthew Dillon if (ahci_port_softreset(ap)) { 1209258223a3SMatthew Dillon kprintf("%s: failed to soft reset device\n", 1210258223a3SMatthew Dillon PORTNAME(ap)); 1211258223a3SMatthew Dillon if (ahci_port_portreset(ap)) { 1212258223a3SMatthew Dillon kprintf("%s: failed to port reset " 1213258223a3SMatthew Dillon "device, give up on it\n", 1214258223a3SMatthew Dillon PORTNAME(ap)); 1215258223a3SMatthew Dillon goto fatal; 1216258223a3SMatthew Dillon } 1217258223a3SMatthew Dillon } 1218258223a3SMatthew Dillon 1219258223a3SMatthew Dillon /* Had to reset device, can't gather extended info. */ 1220258223a3SMatthew Dillon } else if (ap->ap_sactive) { 1221258223a3SMatthew Dillon /* Recover the NCQ error from log page 10h. */ 1222258223a3SMatthew Dillon ahci_port_read_ncq_error(ap, &err_slot); 1223258223a3SMatthew Dillon if (err_slot < 0) 1224258223a3SMatthew Dillon goto failall; 1225258223a3SMatthew Dillon 1226258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n", 1227258223a3SMatthew Dillon PORTNAME(ap), err_slot); 1228258223a3SMatthew Dillon 1229258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 1230258223a3SMatthew Dillon } else { 1231258223a3SMatthew Dillon /* Didn't reset, could gather extended info from log. */ 1232258223a3SMatthew Dillon } 1233258223a3SMatthew Dillon 1234258223a3SMatthew Dillon /* 1235258223a3SMatthew Dillon * If we couldn't determine the errored slot, reset the port 1236258223a3SMatthew Dillon * and fail all the active slots. 1237258223a3SMatthew Dillon */ 1238258223a3SMatthew Dillon if (err_slot == -1) { 1239258223a3SMatthew Dillon if (ahci_port_softreset(ap) != 0 && 1240258223a3SMatthew Dillon ahci_port_portreset(ap) != 0) { 1241258223a3SMatthew Dillon kprintf("%s: couldn't reset after NCQ error, " 1242258223a3SMatthew Dillon "disabling device.\n", 1243258223a3SMatthew Dillon PORTNAME(ap)); 1244258223a3SMatthew Dillon goto fatal; 1245258223a3SMatthew Dillon } 1246258223a3SMatthew Dillon kprintf("%s: couldn't recover NCQ error, failing " 1247258223a3SMatthew Dillon "all outstanding commands.\n", 1248258223a3SMatthew Dillon PORTNAME(ap)); 1249258223a3SMatthew Dillon goto failall; 1250258223a3SMatthew Dillon } 1251258223a3SMatthew Dillon 1252258223a3SMatthew Dillon /* Clear the failed command in saved CI so completion runs. */ 1253258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 1254258223a3SMatthew Dillon 1255258223a3SMatthew Dillon /* Note the error in the ata_xfer. */ 1256258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 1257258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1258258223a3SMatthew Dillon 1259258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1260258223a3SMatthew Dillon /* There may only be one outstanding standard command now. */ 1261258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 1262258223a3SMatthew Dillon tmp = ci_saved; 1263258223a3SMatthew Dillon if (tmp) { 1264258223a3SMatthew Dillon slot = ffs(tmp) - 1; 1265258223a3SMatthew Dillon tmp &= ~(1 << slot); 1266258223a3SMatthew Dillon KKASSERT(tmp == 0); 1267258223a3SMatthew Dillon } 1268258223a3SMatthew Dillon } 1269258223a3SMatthew Dillon #endif 1270258223a3SMatthew Dillon } 1271258223a3SMatthew Dillon 1272258223a3SMatthew Dillon /* 1273258223a3SMatthew Dillon * Port change (hot-plug). 1274258223a3SMatthew Dillon * 1275258223a3SMatthew Dillon * A PCS interrupt will occur on hot-plug once communication is 1276258223a3SMatthew Dillon * established. 1277258223a3SMatthew Dillon * 1278258223a3SMatthew Dillon * A PRCS interrupt will occur on hot-unplug (and possibly also 1279258223a3SMatthew Dillon * on hot-plug). 1280258223a3SMatthew Dillon * 1281258223a3SMatthew Dillon * We can then check the CPS (Cold Presence State) bit to determine 1282258223a3SMatthew Dillon * if a device is plugged in or not and do the right thing. 1283258223a3SMatthew Dillon */ 1284258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 1285258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 1286258223a3SMatthew Dillon (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) << 16); 1287cec85a37SMatthew Dillon 1288258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 1289258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 1290258223a3SMatthew Dillon if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) { 1291cec85a37SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 1292258223a3SMatthew Dillon PORTNAME(ap)); 1293fd8bd957SMatthew Dillon if (ahci_port_init(ap) == 0) 1294fd8bd957SMatthew Dillon ahci_cam_changed(ap, 1); 1295258223a3SMatthew Dillon } 1296258223a3SMatthew Dillon break; 1297258223a3SMatthew Dillon default: 1298258223a3SMatthew Dillon if (ap->ap_ata.ap_type != ATA_PORT_T_NONE) { 1299258223a3SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 1300258223a3SMatthew Dillon PORTNAME(ap)); 1301258223a3SMatthew Dillon ahci_port_portreset(ap); 1302fd8bd957SMatthew Dillon ahci_cam_changed(ap, 0); 1303258223a3SMatthew Dillon } 1304258223a3SMatthew Dillon break; 1305258223a3SMatthew Dillon } 1306258223a3SMatthew Dillon } 1307258223a3SMatthew Dillon 1308258223a3SMatthew Dillon /* Check for remaining errors - they are fatal. */ 1309258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 1310258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 13114444122dSMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 13124444122dSMatthew Dillon kprintf("%s: unrecoverable errors (IS: %b, SERR: %b %b), " 13134444122dSMatthew Dillon "disabling port.\n", 13144444122dSMatthew Dillon PORTNAME(ap), 13154444122dSMatthew Dillon is, AHCI_PFMT_IS, 13164444122dSMatthew Dillon AHCI_PREG_SERR_ERR(serr), AHCI_PFMT_SERR_ERR, 13174444122dSMatthew Dillon AHCI_PREG_SERR_DIAG(serr), AHCI_PFMT_SERR_DIAG 13184444122dSMatthew Dillon ); 1319258223a3SMatthew Dillon /* XXX try recovery first */ 1320258223a3SMatthew Dillon goto fatal; 1321258223a3SMatthew Dillon } 1322258223a3SMatthew Dillon 1323258223a3SMatthew Dillon /* Fail all outstanding commands if we know the port won't recover. */ 1324258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 1325258223a3SMatthew Dillon fatal: 1326258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 1327258223a3SMatthew Dillon failall: 1328258223a3SMatthew Dillon 1329258223a3SMatthew Dillon /* Ensure port is shut down. */ 1330258223a3SMatthew Dillon ahci_port_stop(ap, 1); 1331258223a3SMatthew Dillon 1332258223a3SMatthew Dillon /* Error all the active slots. */ 1333258223a3SMatthew Dillon ci_masked = ci_saved & *active; 1334258223a3SMatthew Dillon while (ci_masked) { 1335258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 1336258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 1337258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 1338258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1339258223a3SMatthew Dillon } 1340258223a3SMatthew Dillon 1341258223a3SMatthew Dillon /* Run completion for all active slots. */ 1342258223a3SMatthew Dillon ci_saved &= ~*active; 1343258223a3SMatthew Dillon 1344258223a3SMatthew Dillon /* 1345258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 1346258223a3SMatthew Dillon * 1347258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 1348258223a3SMatthew Dillon * a livelock. 1349258223a3SMatthew Dillon */ 1350258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 1351258223a3SMatthew Dillon need_restart = 0; 1352258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 1353258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 1354258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 1355258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 1356258223a3SMatthew Dillon } 1357258223a3SMatthew Dillon } 1358258223a3SMatthew Dillon 1359258223a3SMatthew Dillon /* 1360258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 1361258223a3SMatthew Dillon * changed to zero some time after we activated it. 1362258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 1363258223a3SMatthew Dillon */ 1364258223a3SMatthew Dillon ci_masked = ~ci_saved & *active & ci_mask; 1365258223a3SMatthew Dillon while (ci_masked) { 1366258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 1367258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 1368258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 1369258223a3SMatthew Dillon 1370258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 1371258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 1372258223a3SMatthew Dillon " (error)" : ""); 1373258223a3SMatthew Dillon 1374258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 1375258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 1376258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 1377258223a3SMatthew Dillon 1378258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 1379258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 1380258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 1381258223a3SMatthew Dillon 1382258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 1383258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 1384258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 1385258223a3SMatthew Dillon 1386258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 1387258223a3SMatthew Dillon ccb->ccb_done(ccb); 1388258223a3SMatthew Dillon 1389258223a3SMatthew Dillon processed |= 1 << ccb->ccb_slot; 1390258223a3SMatthew Dillon } 1391258223a3SMatthew Dillon 1392258223a3SMatthew Dillon if (need_restart) { 1393258223a3SMatthew Dillon /* Restart command DMA on the port */ 1394258223a3SMatthew Dillon ahci_port_start(ap, 0); 1395258223a3SMatthew Dillon 1396258223a3SMatthew Dillon /* Re-enable outstanding commands on port. */ 1397258223a3SMatthew Dillon if (ci_saved) { 1398258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1399258223a3SMatthew Dillon tmp = ci_saved; 1400258223a3SMatthew Dillon while (tmp) { 1401258223a3SMatthew Dillon slot = ffs(tmp) - 1; 1402258223a3SMatthew Dillon tmp &= ~(1 << slot); 1403258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 1404258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 1405258223a3SMatthew Dillon KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) == 1406258223a3SMatthew Dillon (!!ap->ap_sactive)); 1407258223a3SMatthew Dillon } 1408258223a3SMatthew Dillon #endif 1409258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr " 1410258223a3SMatthew Dillon "re-enabling%s slots %08x\n", PORTNAME(ap), 1411258223a3SMatthew Dillon ap->ap_sactive ? " NCQ" : "", ci_saved); 1412258223a3SMatthew Dillon 1413258223a3SMatthew Dillon if (ap->ap_sactive) 1414258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 1415258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 1416258223a3SMatthew Dillon } 1417258223a3SMatthew Dillon } 1418258223a3SMatthew Dillon 1419258223a3SMatthew Dillon return (processed); 1420258223a3SMatthew Dillon } 1421258223a3SMatthew Dillon 1422258223a3SMatthew Dillon struct ahci_ccb * 1423258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 1424258223a3SMatthew Dillon { 1425258223a3SMatthew Dillon struct ahci_ccb *ccb; 1426258223a3SMatthew Dillon 1427258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 1428258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 1429258223a3SMatthew Dillon if (ccb != NULL) { 1430258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 1431258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 1432258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 1433258223a3SMatthew Dillon } 1434258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 1435258223a3SMatthew Dillon 1436258223a3SMatthew Dillon return (ccb); 1437258223a3SMatthew Dillon } 1438258223a3SMatthew Dillon 1439258223a3SMatthew Dillon void 1440258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 1441258223a3SMatthew Dillon { 1442258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1443258223a3SMatthew Dillon 1444258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1445258223a3SMatthew Dillon if (ccb->ccb_xa.state != ATA_S_COMPLETE && 1446258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_TIMEOUT && 1447258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_ERROR) { 1448258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, " 1449258223a3SMatthew Dillon "slot %d\n", 1450258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), ccb->ccb_xa.state, 1451258223a3SMatthew Dillon ccb->ccb_slot); 1452258223a3SMatthew Dillon } 1453258223a3SMatthew Dillon #endif 1454258223a3SMatthew Dillon 1455258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 1456258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 1457258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 1458258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 1459258223a3SMatthew Dillon } 1460258223a3SMatthew Dillon 1461258223a3SMatthew Dillon struct ahci_ccb * 1462258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 1463258223a3SMatthew Dillon { 1464258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 1465258223a3SMatthew Dillon u_int32_t sact; 1466258223a3SMatthew Dillon 1467258223a3SMatthew Dillon /* No commands may be active on the chip. */ 1468258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 1469258223a3SMatthew Dillon if (sact != 0) 1470258223a3SMatthew Dillon kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact); 1471258223a3SMatthew Dillon KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0); 1472258223a3SMatthew Dillon 1473258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1474258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy == 0); 1475258223a3SMatthew Dillon ap->ap_err_busy = 1; 1476258223a3SMatthew Dillon #endif 1477258223a3SMatthew Dillon /* Save outstanding command state. */ 1478258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 1479258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 1480258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 1481258223a3SMatthew Dillon 1482258223a3SMatthew Dillon /* 1483258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 1484258223a3SMatthew Dillon * run prematurely. 1485258223a3SMatthew Dillon */ 1486258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 1487258223a3SMatthew Dillon 1488258223a3SMatthew Dillon /* 1489258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 1490258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 1491258223a3SMatthew Dillon */ 1492258223a3SMatthew Dillon err_ccb = ahci_get_ccb(ap); 1493258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 1494258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 1495258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 1496258223a3SMatthew Dillon 1497258223a3SMatthew Dillon return err_ccb; 1498258223a3SMatthew Dillon } 1499258223a3SMatthew Dillon 1500258223a3SMatthew Dillon void 1501258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 1502258223a3SMatthew Dillon { 1503258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1504258223a3SMatthew Dillon u_int32_t sact; 15055f8c1efdSMatthew Dillon u_int32_t ci; 1506258223a3SMatthew Dillon 1507258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1508258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy); 1509258223a3SMatthew Dillon #endif 15105f8c1efdSMatthew Dillon /* 15115f8c1efdSMatthew Dillon * No commands may be active on the chip 15125f8c1efdSMatthew Dillon */ 1513258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 15145f8c1efdSMatthew Dillon if (sact) { 15155f8c1efdSMatthew Dillon panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n", 15165f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 1517258223a3SMatthew Dillon } 15185f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 15195f8c1efdSMatthew Dillon if (ci) { 15205f8c1efdSMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0\n", 15215f8c1efdSMatthew Dillon ccb->ccb_slot, ci); 15225f8c1efdSMatthew Dillon } 1523258223a3SMatthew Dillon 1524258223a3SMatthew Dillon /* Done with the CCB */ 1525258223a3SMatthew Dillon ahci_put_ccb(ccb); 1526258223a3SMatthew Dillon 1527258223a3SMatthew Dillon /* Restore outstanding command state */ 1528258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 1529258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 1530258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 1531258223a3SMatthew Dillon 1532258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1533258223a3SMatthew Dillon ap->ap_err_busy = 0; 1534258223a3SMatthew Dillon #endif 1535258223a3SMatthew Dillon } 1536258223a3SMatthew Dillon 1537258223a3SMatthew Dillon int 1538258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp) 1539258223a3SMatthew Dillon { 1540258223a3SMatthew Dillon struct ahci_ccb *ccb; 1541258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 1542258223a3SMatthew Dillon u_int32_t cmd; 1543258223a3SMatthew Dillon struct ata_fis_h2d *fis; 1544258223a3SMatthew Dillon int rc = EIO; 1545258223a3SMatthew Dillon 1546258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap)); 1547258223a3SMatthew Dillon 1548258223a3SMatthew Dillon /* Save command register state. */ 1549258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1550258223a3SMatthew Dillon 1551258223a3SMatthew Dillon /* Port should have been idled already. Start it. */ 1552258223a3SMatthew Dillon KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0); 1553258223a3SMatthew Dillon ahci_port_start(ap, 0); 1554258223a3SMatthew Dillon 1555258223a3SMatthew Dillon /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */ 1556258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 1557258223a3SMatthew Dillon ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL; 1558258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 1559258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 1560258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 1561258223a3SMatthew Dillon bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table)); 1562258223a3SMatthew Dillon 1563258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 1564258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 1565258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 1566258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 1567258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 1568258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 1569258223a3SMatthew Dillon fis->sector_count_exp = 0; 1570258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 1571258223a3SMatthew Dillon fis->lba_mid_exp = 0; 1572258223a3SMatthew Dillon fis->device = 0; 1573258223a3SMatthew Dillon 1574258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1575258223a3SMatthew Dillon 1576258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 1577258223a3SMatthew Dillon rc = ENOMEM; /* XXX caller must abort all commands */ 1578258223a3SMatthew Dillon goto err; 1579258223a3SMatthew Dillon } 1580258223a3SMatthew Dillon 1581258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 1582258223a3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0) 1583258223a3SMatthew Dillon goto err; 1584258223a3SMatthew Dillon 1585258223a3SMatthew Dillon rc = 0; 1586258223a3SMatthew Dillon err: 1587258223a3SMatthew Dillon /* Abort our command, if it failed, by stopping command DMA. */ 1588258223a3SMatthew Dillon if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) { 1589258223a3SMatthew Dillon kprintf("%s: log page read failed, slot %d was still active.\n", 1590258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 1591258223a3SMatthew Dillon ahci_port_stop(ap, 0); 1592258223a3SMatthew Dillon } 1593258223a3SMatthew Dillon 1594258223a3SMatthew Dillon /* Done with the error CCB now. */ 1595258223a3SMatthew Dillon ahci_unload_prdt(ccb); 1596258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 1597258223a3SMatthew Dillon 1598258223a3SMatthew Dillon /* Extract failed register set and tags from the scratch space. */ 1599258223a3SMatthew Dillon if (rc == 0) { 1600258223a3SMatthew Dillon struct ata_log_page_10h *log; 1601258223a3SMatthew Dillon int err_slot; 1602258223a3SMatthew Dillon 1603258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 1604258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 1605258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 1606258223a3SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ " 1607258223a3SMatthew Dillon "error?\n", 1608258223a3SMatthew Dillon PORTNAME(ap)); 1609258223a3SMatthew Dillon rc = ESRCH; 1610258223a3SMatthew Dillon } else { 1611258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 1612258223a3SMatthew Dillon *err_slotp = err_slot = log->err_regs.type & 1613258223a3SMatthew Dillon ATA_LOG_10H_TYPE_TAG_MASK; 1614258223a3SMatthew Dillon 1615258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 1616258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, &log->err_regs, 1617258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 1618258223a3SMatthew Dillon ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 1619258223a3SMatthew Dillon ccb->ccb_xa.rfis.flags = 0; 1620258223a3SMatthew Dillon } 1621258223a3SMatthew Dillon } 1622258223a3SMatthew Dillon 1623258223a3SMatthew Dillon /* Restore saved CMD register state */ 1624258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1625258223a3SMatthew Dillon 1626258223a3SMatthew Dillon return (rc); 1627258223a3SMatthew Dillon } 1628258223a3SMatthew Dillon 1629258223a3SMatthew Dillon /* 1630258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 1631258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 1632258223a3SMatthew Dillon * single physical base address. 1633258223a3SMatthew Dillon */ 1634258223a3SMatthew Dillon struct ahci_dmamem * 1635258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 1636258223a3SMatthew Dillon { 1637258223a3SMatthew Dillon struct ahci_dmamem *adm; 1638258223a3SMatthew Dillon int error; 1639258223a3SMatthew Dillon 1640258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 1641258223a3SMatthew Dillon 1642258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 1643258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 1644258223a3SMatthew Dillon if (error == 0) { 1645258223a3SMatthew Dillon adm->adm_tag = tag; 1646258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 1647258223a3SMatthew Dillon adm->adm_kva, 1648258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 1649258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 1650258223a3SMatthew Dillon 0); 1651258223a3SMatthew Dillon } 1652258223a3SMatthew Dillon if (error) { 1653258223a3SMatthew Dillon if (adm->adm_map) { 1654258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 1655258223a3SMatthew Dillon adm->adm_map = NULL; 1656258223a3SMatthew Dillon adm->adm_tag = NULL; 1657258223a3SMatthew Dillon adm->adm_kva = NULL; 1658258223a3SMatthew Dillon } 1659258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 1660258223a3SMatthew Dillon adm = NULL; 1661258223a3SMatthew Dillon } 1662258223a3SMatthew Dillon return (adm); 1663258223a3SMatthew Dillon } 1664258223a3SMatthew Dillon 1665258223a3SMatthew Dillon static 1666258223a3SMatthew Dillon void 1667258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 1668258223a3SMatthew Dillon { 1669258223a3SMatthew Dillon KKASSERT(error == 0); 1670258223a3SMatthew Dillon KKASSERT(nsegs == 1); 1671258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 1672258223a3SMatthew Dillon } 1673258223a3SMatthew Dillon 1674258223a3SMatthew Dillon 1675258223a3SMatthew Dillon void 1676258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 1677258223a3SMatthew Dillon { 1678258223a3SMatthew Dillon if (adm->adm_map) { 1679258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 1680258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 1681258223a3SMatthew Dillon adm->adm_map = NULL; 1682258223a3SMatthew Dillon adm->adm_tag = NULL; 1683258223a3SMatthew Dillon adm->adm_kva = NULL; 1684258223a3SMatthew Dillon } 1685258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 1686258223a3SMatthew Dillon } 1687258223a3SMatthew Dillon 1688258223a3SMatthew Dillon u_int32_t 1689258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 1690258223a3SMatthew Dillon { 1691258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 1692258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 1693258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 1694258223a3SMatthew Dillon } 1695258223a3SMatthew Dillon 1696258223a3SMatthew Dillon void 1697258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 1698258223a3SMatthew Dillon { 1699258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 1700258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 1701258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 1702258223a3SMatthew Dillon } 1703258223a3SMatthew Dillon 1704258223a3SMatthew Dillon int 1705258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 1706258223a3SMatthew Dillon u_int32_t target) 1707258223a3SMatthew Dillon { 1708258223a3SMatthew Dillon int i; 1709258223a3SMatthew Dillon 1710258223a3SMatthew Dillon for (i = 0; i < 1000; i++) { 1711258223a3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 1712258223a3SMatthew Dillon return (0); 1713258223a3SMatthew Dillon DELAY(1000); 1714258223a3SMatthew Dillon } 1715258223a3SMatthew Dillon 1716258223a3SMatthew Dillon return (1); 1717258223a3SMatthew Dillon } 1718258223a3SMatthew Dillon 1719258223a3SMatthew Dillon u_int32_t 1720258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 1721258223a3SMatthew Dillon { 1722258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 1723258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 1724258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 1725258223a3SMatthew Dillon } 1726258223a3SMatthew Dillon 1727258223a3SMatthew Dillon void 1728258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 1729258223a3SMatthew Dillon { 1730258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 1731258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 1732258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 1733258223a3SMatthew Dillon } 1734258223a3SMatthew Dillon 1735258223a3SMatthew Dillon int 1736cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 1737cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 1738258223a3SMatthew Dillon { 1739258223a3SMatthew Dillon int i; 1740258223a3SMatthew Dillon 1741cec85a37SMatthew Dillon for (i = 0; i < timeout; i++) { 1742258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 1743258223a3SMatthew Dillon return (0); 1744258223a3SMatthew Dillon DELAY(1000); 1745258223a3SMatthew Dillon } 1746258223a3SMatthew Dillon 1747258223a3SMatthew Dillon return (1); 1748258223a3SMatthew Dillon } 1749258223a3SMatthew Dillon 1750258223a3SMatthew Dillon struct ata_xfer * 1751258223a3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap) 1752258223a3SMatthew Dillon { 1753258223a3SMatthew Dillon /*struct ahci_softc *sc = ap->ap_sc;*/ 1754258223a3SMatthew Dillon struct ahci_ccb *ccb; 1755258223a3SMatthew Dillon 1756258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 1757258223a3SMatthew Dillon if (ccb == NULL) { 1758258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 1759258223a3SMatthew Dillon PORTNAME(ap)); 1760258223a3SMatthew Dillon return (NULL); 1761258223a3SMatthew Dillon } 1762258223a3SMatthew Dillon 1763258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 1764258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 1765258223a3SMatthew Dillon 1766258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 1767258223a3SMatthew Dillon 1768258223a3SMatthew Dillon return (&ccb->ccb_xa); 1769258223a3SMatthew Dillon } 1770258223a3SMatthew Dillon 1771258223a3SMatthew Dillon void 1772258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 1773258223a3SMatthew Dillon { 1774258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 1775258223a3SMatthew Dillon 1776258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 1777258223a3SMatthew Dillon 1778258223a3SMatthew Dillon ahci_put_ccb(ccb); 1779258223a3SMatthew Dillon } 1780258223a3SMatthew Dillon 1781258223a3SMatthew Dillon int 1782258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 1783258223a3SMatthew Dillon { 1784258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 1785258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 1786258223a3SMatthew Dillon 1787258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 1788258223a3SMatthew Dillon 17894444122dSMatthew Dillon #if 0 17904444122dSMatthew Dillon kprintf("ahci_ata_cmd xa->flags %08x type %08x cmd=%08x\n", 17914444122dSMatthew Dillon xa->flags, 17924444122dSMatthew Dillon xa->fis->type, 17934444122dSMatthew Dillon xa->fis->command); 17944444122dSMatthew Dillon #endif 17954444122dSMatthew Dillon 1796258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 1797258223a3SMatthew Dillon goto failcmd; 1798258223a3SMatthew Dillon 1799258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 1800258223a3SMatthew Dillon 1801258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 1802258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 1803258223a3SMatthew Dillon 1804258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 1805258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 1806258223a3SMatthew Dillon 1807258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 1808258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 1809258223a3SMatthew Dillon 1810258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 1811258223a3SMatthew Dillon goto failcmd; 1812258223a3SMatthew Dillon 1813258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 1814258223a3SMatthew Dillon 1815258223a3SMatthew Dillon if (xa->flags & ATA_F_POLL) { 1816258223a3SMatthew Dillon ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout); 1817258223a3SMatthew Dillon return (ATA_COMPLETE); 1818258223a3SMatthew Dillon } 1819258223a3SMatthew Dillon 1820258223a3SMatthew Dillon crit_enter(); 1821258223a3SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_RUNNING; 1822258223a3SMatthew Dillon callout_reset(&ccb->ccb_timeout, xa->timeout, 1823258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 1824258223a3SMatthew Dillon ahci_start(ccb); 1825258223a3SMatthew Dillon crit_exit(); 1826258223a3SMatthew Dillon return (ATA_QUEUED); 1827258223a3SMatthew Dillon 1828258223a3SMatthew Dillon failcmd: 1829258223a3SMatthew Dillon crit_enter(); 1830258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 1831258223a3SMatthew Dillon xa->complete(xa); 1832258223a3SMatthew Dillon crit_exit(); 1833258223a3SMatthew Dillon return (ATA_ERROR); 1834258223a3SMatthew Dillon } 1835258223a3SMatthew Dillon 1836258223a3SMatthew Dillon void 1837258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 1838258223a3SMatthew Dillon { 1839258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1840258223a3SMatthew Dillon 1841258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 1842258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 1843258223a3SMatthew Dillon callout_stop(&ccb->ccb_timeout); 1844258223a3SMatthew Dillon } 1845258223a3SMatthew Dillon 1846258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) 1847258223a3SMatthew Dillon ahci_issue_pending_commands(ccb->ccb_port, 1848258223a3SMatthew Dillon xa->flags & ATA_F_NCQ); 1849258223a3SMatthew Dillon 1850258223a3SMatthew Dillon ahci_unload_prdt(ccb); 1851258223a3SMatthew Dillon 1852258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP) 1853258223a3SMatthew Dillon xa->state = ATA_S_COMPLETE; 1854258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1855258223a3SMatthew Dillon else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT) 1856258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, " 1857258223a3SMatthew Dillon "slot %d\n", 1858258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot); 1859258223a3SMatthew Dillon #endif 1860258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 1861258223a3SMatthew Dillon xa->complete(xa); 1862258223a3SMatthew Dillon } 1863258223a3SMatthew Dillon 1864258223a3SMatthew Dillon static void 1865258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 1866258223a3SMatthew Dillon { 1867258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 1868258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1869258223a3SMatthew Dillon 1870258223a3SMatthew Dillon lwkt_serialize_enter(&ap->ap_sc->sc_serializer); 1871258223a3SMatthew Dillon ahci_ata_cmd_timeout(arg); 1872258223a3SMatthew Dillon lwkt_serialize_exit(&ap->ap_sc->sc_serializer); 1873258223a3SMatthew Dillon } 1874258223a3SMatthew Dillon 1875258223a3SMatthew Dillon static void 1876258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg) 1877258223a3SMatthew Dillon { 1878258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 1879258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1880258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1881258223a3SMatthew Dillon volatile u_int32_t *active; 1882258223a3SMatthew Dillon int ccb_was_started, ncq_cmd; 1883258223a3SMatthew Dillon 1884258223a3SMatthew Dillon crit_enter(); 1885258223a3SMatthew Dillon kprintf("CMD TIMEOUT port-cmd-reg 0x%b\n" 1886258223a3SMatthew Dillon "\tactive=%08x sactive=%08x\n" 1887258223a3SMatthew Dillon "\t sact=%08x ci=%08x\n", 1888258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 1889258223a3SMatthew Dillon ap->ap_active, ap->ap_sactive, 1890258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 1891258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI)); 1892258223a3SMatthew Dillon 1893*9e145b23SMatthew Dillon /* 1894*9e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 1895*9e145b23SMatthew Dillon */ 1896*9e145b23SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL|ATA_F_TIMEOUT_RUNNING)); 1897258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 1898258223a3SMatthew Dillon ncq_cmd = (xa->flags & ATA_F_NCQ); 1899258223a3SMatthew Dillon active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active; 1900258223a3SMatthew Dillon 1901258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 1902258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out " 1903258223a3SMatthew Dillon "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot); 1904258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 1905258223a3SMatthew Dillon ccb_was_started = 0; 1906258223a3SMatthew Dillon } else if (ccb->ccb_xa.state == ATA_S_ONCHIP && ahci_port_intr(ap, 1907258223a3SMatthew Dillon 1 << ccb->ccb_slot)) { 1908258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: final poll of port completed " 1909258223a3SMatthew Dillon "command in slot %d\n", PORTNAME(ap), ccb->ccb_slot); 1910258223a3SMatthew Dillon goto ret; 1911258223a3SMatthew Dillon } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 1912258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already " 1913258223a3SMatthew Dillon "handled%s\n", PORTNAME(ap), ccb->ccb_slot, 1914258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot)) ? 1915258223a3SMatthew Dillon " but slot is still active?" : "."); 1916258223a3SMatthew Dillon goto ret; 1917258223a3SMatthew Dillon } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) & 1918258223a3SMatthew Dillon (1 << ccb->ccb_slot)) == 0 && 1919258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot))) { 1920258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but " 1921258223a3SMatthew Dillon "IRQ handler didn't detect it. Why?\n", PORTNAME(ap), 1922258223a3SMatthew Dillon ccb->ccb_slot); 1923258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 1924258223a3SMatthew Dillon ccb->ccb_done(ccb); 1925258223a3SMatthew Dillon goto ret; 1926258223a3SMatthew Dillon } else { 1927258223a3SMatthew Dillon ccb_was_started = 1; 1928258223a3SMatthew Dillon } 1929258223a3SMatthew Dillon 1930258223a3SMatthew Dillon /* Complete the slot with a timeout error. */ 1931258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 1932258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 1933258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap)); 1934258223a3SMatthew Dillon ccb->ccb_done(ccb); /* This won't issue pending commands or run the 1935258223a3SMatthew Dillon atascsi completion. */ 1936258223a3SMatthew Dillon 1937258223a3SMatthew Dillon /* Reset port to abort running command. */ 1938258223a3SMatthew Dillon if (ccb_was_started) { 1939258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command " 1940258223a3SMatthew Dillon "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" 1941258223a3SMatthew Dillon : "", ccb->ccb_slot, *active); 1942258223a3SMatthew Dillon if (ahci_port_softreset(ap) != 0 && ahci_port_portreset(ap) 1943258223a3SMatthew Dillon != 0) { 1944258223a3SMatthew Dillon kprintf("%s: failed to reset port during timeout " 1945258223a3SMatthew Dillon "handling, disabling it\n", 1946258223a3SMatthew Dillon PORTNAME(ap)); 1947258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 1948258223a3SMatthew Dillon } 1949258223a3SMatthew Dillon 1950258223a3SMatthew Dillon /* Restart any other commands that were aborted by the reset. */ 1951258223a3SMatthew Dillon if (*active) { 1952258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots " 1953258223a3SMatthew Dillon "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "", 1954258223a3SMatthew Dillon *active); 1955258223a3SMatthew Dillon if (ncq_cmd) 1956258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, *active); 1957258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, *active); 1958258223a3SMatthew Dillon } 1959258223a3SMatthew Dillon } 1960258223a3SMatthew Dillon 1961258223a3SMatthew Dillon /* Issue any pending commands now. */ 1962258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap)); 1963258223a3SMatthew Dillon if (ccb_was_started) 1964258223a3SMatthew Dillon ahci_issue_pending_commands(ap, ncq_cmd); 1965258223a3SMatthew Dillon else if (ap->ap_active == 0) 1966258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 1967258223a3SMatthew Dillon 1968258223a3SMatthew Dillon /* Complete the timed out ata_xfer I/O (may generate new I/O). */ 1969258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap)); 1970258223a3SMatthew Dillon xa->complete(xa); 1971258223a3SMatthew Dillon 1972258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap)); 1973258223a3SMatthew Dillon ret: 1974258223a3SMatthew Dillon crit_exit(); 1975258223a3SMatthew Dillon } 1976258223a3SMatthew Dillon 1977258223a3SMatthew Dillon void 1978258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 1979258223a3SMatthew Dillon { 1980258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 1981258223a3SMatthew Dillon } 1982