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 541980eff3SMatthew Dillon int ahci_port_init(struct ahci_port *ap, struct ata_port *at); 5517eab71eSMatthew Dillon int ahci_port_start(struct ahci_port *); 56258223a3SMatthew Dillon int ahci_port_stop(struct ahci_port *, int); 57258223a3SMatthew Dillon int ahci_port_clo(struct ahci_port *); 58258223a3SMatthew Dillon 59258223a3SMatthew Dillon int ahci_load_prdt(struct ahci_ccb *); 60258223a3SMatthew Dillon void ahci_unload_prdt(struct ahci_ccb *); 61258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, 62258223a3SMatthew Dillon int nsegs, int error); 63258223a3SMatthew Dillon void ahci_start(struct ahci_ccb *); 6417eab71eSMatthew Dillon int ahci_port_softreset(struct ahci_port *ap); 651980eff3SMatthew Dillon int ahci_port_pmprobe(struct ahci_port *ap); 661980eff3SMatthew Dillon int ahci_port_hardreset(struct ahci_port *ap, int hard); 67cf5f3a81SMatthew Dillon void ahci_port_hardstop(struct ahci_port *ap); 68cf5f3a81SMatthew Dillon void ahci_flush_tfd(struct ahci_port *ap); 69258223a3SMatthew Dillon 70258223a3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *arg); 71258223a3SMatthew Dillon 72258223a3SMatthew Dillon void ahci_issue_pending_ncq_commands(struct ahci_port *); 73258223a3SMatthew Dillon void ahci_issue_pending_commands(struct ahci_port *, int); 74258223a3SMatthew Dillon 75258223a3SMatthew Dillon struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *); 76258223a3SMatthew Dillon void ahci_put_err_ccb(struct ahci_ccb *); 77258223a3SMatthew Dillon 78258223a3SMatthew Dillon int ahci_port_read_ncq_error(struct ahci_port *, int *); 79258223a3SMatthew Dillon 80258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag); 81258223a3SMatthew Dillon void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *); 82258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error); 83258223a3SMatthew Dillon 84258223a3SMatthew Dillon void ahci_empty_done(struct ahci_ccb *ccb); 85258223a3SMatthew Dillon void ahci_ata_cmd_done(struct ahci_ccb *ccb); 86258223a3SMatthew Dillon 87258223a3SMatthew Dillon /* Wait for all bits in _b to be cleared */ 88cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \ 89cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0) 90cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to, _r, _b) \ 91cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), 0) 92258223a3SMatthew Dillon 93258223a3SMatthew Dillon /* Wait for all bits in _b to be set */ 94cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \ 95cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b)) 96cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \ 97cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), (_b)) 98cec85a37SMatthew Dillon 99cec85a37SMatthew Dillon #define AHCI_PWAIT_TIMEOUT 1000 100258223a3SMatthew Dillon 101fd8bd957SMatthew Dillon /* 102fd8bd957SMatthew Dillon * Initialize the global AHCI hardware. This code does not set up any of 103fd8bd957SMatthew Dillon * its ports. 104fd8bd957SMatthew Dillon */ 105258223a3SMatthew Dillon int 106258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc) 107258223a3SMatthew Dillon { 108258223a3SMatthew Dillon u_int32_t cap, pi; 109258223a3SMatthew Dillon 110258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b", 111258223a3SMatthew Dillon ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC); 112258223a3SMatthew Dillon 113258223a3SMatthew Dillon /* save BIOS initialised parameters, enable staggered spin up */ 114258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 115258223a3SMatthew Dillon cap &= AHCI_REG_CAP_SMPS; 116258223a3SMatthew Dillon cap |= AHCI_REG_CAP_SSS; 117258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 118258223a3SMatthew Dillon 11917eab71eSMatthew Dillon /* 12017eab71eSMatthew Dillon * Unconditionally reset the controller, do not conditionalize on 12117eab71eSMatthew Dillon * trying to figure it if it was previously active or not. 12217eab71eSMatthew Dillon */ 123258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR); 124258223a3SMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR, 125258223a3SMatthew Dillon AHCI_REG_GHC_HR) != 0) { 126258223a3SMatthew Dillon device_printf(sc->sc_dev, 127258223a3SMatthew Dillon "unable to reset controller\n"); 128258223a3SMatthew Dillon return (1); 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; 1481980eff3SMatthew Dillon struct ata_port *at; 149258223a3SMatthew Dillon struct ahci_ccb *ccb; 150258223a3SMatthew Dillon u_int64_t dva; 151258223a3SMatthew Dillon u_int32_t cmd; 152258223a3SMatthew Dillon struct ahci_cmd_hdr *hdr; 153258223a3SMatthew Dillon struct ahci_cmd_table *table; 154258223a3SMatthew Dillon int rc = ENOMEM; 155258223a3SMatthew Dillon int error; 156258223a3SMatthew Dillon int i; 157258223a3SMatthew Dillon 158258223a3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 159258223a3SMatthew Dillon if (ap == NULL) { 160258223a3SMatthew Dillon device_printf(sc->sc_dev, 161258223a3SMatthew Dillon "unable to allocate memory for port %d\n", 162258223a3SMatthew Dillon port); 163258223a3SMatthew Dillon goto reterr; 164258223a3SMatthew Dillon } 165258223a3SMatthew Dillon 166258223a3SMatthew Dillon ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d", 167258223a3SMatthew Dillon device_get_name(sc->sc_dev), 168258223a3SMatthew Dillon device_get_unit(sc->sc_dev), 169258223a3SMatthew Dillon port); 170258223a3SMatthew Dillon sc->sc_ports[port] = ap; 171258223a3SMatthew Dillon 1721980eff3SMatthew Dillon /* 1731980eff3SMatthew Dillon * Allocate enough so we never have to reallocate, it makes 1741980eff3SMatthew Dillon * it easier. 1751980eff3SMatthew Dillon * 1761980eff3SMatthew Dillon * ap_pmcount will be reduced by the scan if we encounter the 1771980eff3SMatthew Dillon * port multiplier port prior to target 15. 1781980eff3SMatthew Dillon */ 1791980eff3SMatthew Dillon if (ap->ap_ata == NULL) { 1801980eff3SMatthew Dillon ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * AHCI_MAX_PMPORTS, 1811980eff3SMatthew Dillon M_DEVBUF, M_INTWAIT | M_ZERO); 1821980eff3SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 1831980eff3SMatthew Dillon at = &ap->ap_ata[i]; 1841980eff3SMatthew Dillon at->at_ahci_port = ap; 1851980eff3SMatthew Dillon at->at_target = i; 1863209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 1871980eff3SMatthew Dillon ksnprintf(at->at_name, sizeof(at->at_name), 1881980eff3SMatthew Dillon "%s.%d", ap->ap_name, i); 1891980eff3SMatthew Dillon } 1901980eff3SMatthew Dillon } 191258223a3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 192258223a3SMatthew Dillon AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 193258223a3SMatthew Dillon device_printf(sc->sc_dev, 194258223a3SMatthew Dillon "unable to create register window for port %d\n", 195258223a3SMatthew Dillon port); 196258223a3SMatthew Dillon goto freeport; 197258223a3SMatthew Dillon } 198258223a3SMatthew Dillon 199258223a3SMatthew Dillon ap->ap_sc = sc; 200258223a3SMatthew Dillon ap->ap_num = port; 2013209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 202258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_free); 203258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_pending); 204258223a3SMatthew Dillon lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0); 205258223a3SMatthew Dillon 206258223a3SMatthew Dillon /* Disable port interrupts */ 207258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 208258223a3SMatthew Dillon 20917eab71eSMatthew Dillon /* 21017eab71eSMatthew Dillon * Sec 10.1.2 - deinitialise port if it is already running 21117eab71eSMatthew Dillon */ 212258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 213258223a3SMatthew Dillon if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR | 214258223a3SMatthew Dillon AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) || 215258223a3SMatthew Dillon (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) { 216258223a3SMatthew Dillon int r; 217258223a3SMatthew Dillon 218258223a3SMatthew Dillon r = ahci_port_stop(ap, 1); 219258223a3SMatthew Dillon if (r) { 220258223a3SMatthew Dillon device_printf(sc->sc_dev, 221258223a3SMatthew Dillon "unable to disable %s, ignoring port %d\n", 222258223a3SMatthew Dillon ((r == 2) ? "CR" : "FR"), port); 223258223a3SMatthew Dillon rc = ENXIO; 224258223a3SMatthew Dillon goto freeport; 225258223a3SMatthew Dillon } 226258223a3SMatthew Dillon 227258223a3SMatthew Dillon /* Write DET to zero */ 228cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 229258223a3SMatthew Dillon } 230258223a3SMatthew Dillon 231258223a3SMatthew Dillon /* Allocate RFIS */ 232258223a3SMatthew Dillon ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis); 233258223a3SMatthew Dillon if (ap->ap_dmamem_rfis == NULL) { 234cf5f3a81SMatthew Dillon kprintf("%s: NORFIS\n", PORTNAME(ap)); 235258223a3SMatthew Dillon goto nomem; 236258223a3SMatthew Dillon } 237258223a3SMatthew Dillon 238258223a3SMatthew Dillon /* Setup RFIS base address */ 239258223a3SMatthew Dillon ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis); 240258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis); 241258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32)); 242258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva); 243258223a3SMatthew Dillon 244258223a3SMatthew Dillon /* Enable FIS reception and activate port. */ 245258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 2461980eff3SMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 247258223a3SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 248258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 249258223a3SMatthew Dillon 250258223a3SMatthew Dillon /* Check whether port activated. Skip it if not. */ 251258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 252258223a3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 253cf5f3a81SMatthew Dillon kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap)); 254258223a3SMatthew Dillon rc = ENXIO; 255258223a3SMatthew Dillon goto freeport; 256258223a3SMatthew Dillon } 257258223a3SMatthew Dillon 258258223a3SMatthew Dillon /* Allocate a CCB for each command slot */ 259258223a3SMatthew Dillon ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF, 260258223a3SMatthew Dillon M_WAITOK | M_ZERO); 261258223a3SMatthew Dillon if (ap->ap_ccbs == NULL) { 262258223a3SMatthew Dillon device_printf(sc->sc_dev, 263258223a3SMatthew Dillon "unable to allocate command list for port %d\n", 264258223a3SMatthew Dillon port); 265258223a3SMatthew Dillon goto freeport; 266258223a3SMatthew Dillon } 267258223a3SMatthew Dillon 268258223a3SMatthew Dillon /* Command List Structures and Command Tables */ 269258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh); 270258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt); 271258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table == NULL || 272258223a3SMatthew Dillon ap->ap_dmamem_cmd_list == NULL) { 273258223a3SMatthew Dillon nomem: 274258223a3SMatthew Dillon device_printf(sc->sc_dev, 275258223a3SMatthew Dillon "unable to allocate DMA memory for port %d\n", 276258223a3SMatthew Dillon port); 277258223a3SMatthew Dillon goto freeport; 278258223a3SMatthew Dillon } 279258223a3SMatthew Dillon 280258223a3SMatthew Dillon /* Setup command list base address */ 281258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list); 282258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32)); 283258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva); 284258223a3SMatthew Dillon 285258223a3SMatthew Dillon /* Split CCB allocation into CCBs and assign to command header/table */ 286258223a3SMatthew Dillon hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list); 287258223a3SMatthew Dillon table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table); 288258223a3SMatthew Dillon for (i = 0; i < sc->sc_ncmds; i++) { 289258223a3SMatthew Dillon ccb = &ap->ap_ccbs[i]; 290258223a3SMatthew Dillon 291258223a3SMatthew Dillon error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW, 292258223a3SMatthew Dillon &ccb->ccb_dmamap); 293258223a3SMatthew Dillon if (error) { 294258223a3SMatthew Dillon device_printf(sc->sc_dev, 295258223a3SMatthew Dillon "unable to create dmamap for port %d " 296258223a3SMatthew Dillon "ccb %d\n", port, i); 297258223a3SMatthew Dillon goto freeport; 298258223a3SMatthew Dillon } 299258223a3SMatthew Dillon 300258223a3SMatthew Dillon callout_init(&ccb->ccb_timeout); 301258223a3SMatthew Dillon ccb->ccb_slot = i; 302258223a3SMatthew Dillon ccb->ccb_port = ap; 303258223a3SMatthew Dillon ccb->ccb_cmd_hdr = &hdr[i]; 304258223a3SMatthew Dillon ccb->ccb_cmd_table = &table[i]; 305258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) + 306258223a3SMatthew Dillon ccb->ccb_slot * sizeof(struct ahci_cmd_table); 307258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32)); 308258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva); 309258223a3SMatthew Dillon 310258223a3SMatthew Dillon ccb->ccb_xa.fis = 311258223a3SMatthew Dillon (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 312258223a3SMatthew Dillon ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd; 313258223a3SMatthew Dillon ccb->ccb_xa.tag = i; 314258223a3SMatthew Dillon 315258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 316258223a3SMatthew Dillon ahci_put_ccb(ccb); 317258223a3SMatthew Dillon } 318258223a3SMatthew Dillon 319258223a3SMatthew Dillon /* Wait for ICC change to complete */ 320258223a3SMatthew Dillon ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC); 321258223a3SMatthew Dillon 322fd8bd957SMatthew Dillon /* 323fd8bd957SMatthew Dillon * Do device-related port initialization. A failure here does not 324fd8bd957SMatthew Dillon * cause the port to be deallocated as we want to receive future 325fd8bd957SMatthew Dillon * hot-plug events. 326fd8bd957SMatthew Dillon */ 3271980eff3SMatthew Dillon ahci_port_init(ap, NULL); 328fd8bd957SMatthew Dillon return(0); 329fd8bd957SMatthew Dillon freeport: 330fd8bd957SMatthew Dillon ahci_port_free(sc, port); 331fd8bd957SMatthew Dillon reterr: 332fd8bd957SMatthew Dillon return (rc); 333fd8bd957SMatthew Dillon } 334fd8bd957SMatthew Dillon 335fd8bd957SMatthew Dillon /* 336fd8bd957SMatthew Dillon * [re]initialize an idle port. No CCBs should be active. 337fd8bd957SMatthew Dillon * 3381980eff3SMatthew Dillon * If at is NULL we are initializing a directly connected port, otherwise 3391980eff3SMatthew Dillon * we are indirectly initializing a port multiplier port. 3401980eff3SMatthew Dillon * 341fd8bd957SMatthew Dillon * This function is called during the initial port allocation sequence 342fd8bd957SMatthew Dillon * and is also called on hot-plug insertion. We take no chances and 343fd8bd957SMatthew Dillon * use a portreset instead of a softreset. 344fd8bd957SMatthew Dillon * 34522181ab7SMatthew Dillon * This function is the only way to move a failed port back to active 34622181ab7SMatthew Dillon * status. 34722181ab7SMatthew Dillon * 348fd8bd957SMatthew Dillon * Returns 0 if a device is successfully detected. 349fd8bd957SMatthew Dillon */ 350fd8bd957SMatthew Dillon int 3511980eff3SMatthew Dillon ahci_port_init(struct ahci_port *ap, struct ata_port *at) 352fd8bd957SMatthew Dillon { 3531980eff3SMatthew Dillon u_int32_t data; 354fd8bd957SMatthew Dillon int rc; 355fd8bd957SMatthew Dillon 356fd8bd957SMatthew Dillon /* 3571980eff3SMatthew Dillon * Clear all notification bits 358fd8bd957SMatthew Dillon */ 3591980eff3SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 3601980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 3611980eff3SMatthew Dillon 3621980eff3SMatthew Dillon /* 3631980eff3SMatthew Dillon * Hard-reset the port. If a device is detected but it is busy 3641980eff3SMatthew Dillon * we try a second time, this time cycling the phy as well. 3651980eff3SMatthew Dillon */ 3661980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_HARD_RESET; 3671980eff3SMatthew Dillon rc = ahci_port_reset(ap, at, 1); 3681980eff3SMatthew Dillon if (rc == EBUSY) { 3691980eff3SMatthew Dillon rc = ahci_port_reset(ap, at, 2); 37017eab71eSMatthew Dillon } 371fd8bd957SMatthew Dillon 372258223a3SMatthew Dillon switch (rc) { 373258223a3SMatthew Dillon case ENODEV: 374fd8bd957SMatthew Dillon /* 375fd8bd957SMatthew Dillon * We had problems talking to the device on the port. 376fd8bd957SMatthew Dillon */ 377258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 378258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 379419cb1abSMatthew Dillon kprintf("%s: Device not communicating\n", PORTNAME(ap)); 380258223a3SMatthew Dillon break; 381258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_PHYOFFLINE: 382419cb1abSMatthew Dillon kprintf("%s: PHY offline\n", PORTNAME(ap)); 383258223a3SMatthew Dillon break; 384258223a3SMatthew Dillon default: 385419cb1abSMatthew Dillon kprintf("%s: No device detected\n", PORTNAME(ap)); 386258223a3SMatthew Dillon break; 387258223a3SMatthew Dillon } 388258223a3SMatthew Dillon break; 389258223a3SMatthew Dillon 390258223a3SMatthew Dillon case EBUSY: 391fd8bd957SMatthew Dillon /* 39217eab71eSMatthew Dillon * The device on the port is still telling us its busy, 39317eab71eSMatthew Dillon * which means that it is not properly handling a SATA 39417eab71eSMatthew Dillon * port COMRESET. 395fd8bd957SMatthew Dillon * 39617eab71eSMatthew Dillon * It may be possible to softreset the device using CLO 39717eab71eSMatthew Dillon * and a device reset command. 398fd8bd957SMatthew Dillon */ 39917eab71eSMatthew Dillon kprintf("%s: Device on port is bricked, trying softreset\n", 40017eab71eSMatthew Dillon PORTNAME(ap)); 401258223a3SMatthew Dillon 4021980eff3SMatthew Dillon rc = ahci_port_reset(ap, at, 0); 403258223a3SMatthew Dillon if (rc) { 40417eab71eSMatthew Dillon kprintf("%s: Unable unbrick device\n", 405fd8bd957SMatthew Dillon PORTNAME(ap)); 406fd8bd957SMatthew Dillon } else { 40717eab71eSMatthew Dillon kprintf("%s: Successfully unbricked\n", 408fd8bd957SMatthew Dillon PORTNAME(ap)); 409258223a3SMatthew Dillon } 410258223a3SMatthew Dillon break; 411258223a3SMatthew Dillon 412258223a3SMatthew Dillon default: 413258223a3SMatthew Dillon break; 414258223a3SMatthew Dillon } 415258223a3SMatthew Dillon 416258223a3SMatthew Dillon /* 41717eab71eSMatthew Dillon * Command transfers can only be enabled if a device was successfully 41817eab71eSMatthew Dillon * detected. 4191980eff3SMatthew Dillon * 4201980eff3SMatthew Dillon * Allocate or deallocate the ap_ata array here too. 421258223a3SMatthew Dillon */ 4221980eff3SMatthew Dillon switch(ap->ap_type) { 4231980eff3SMatthew Dillon case ATA_PORT_T_NONE: 4241980eff3SMatthew Dillon ap->ap_pmcount = 0; 4251980eff3SMatthew Dillon break; 4261980eff3SMatthew Dillon case ATA_PORT_T_PM: 4271980eff3SMatthew Dillon /* already set */ 4281980eff3SMatthew Dillon break; 4291980eff3SMatthew Dillon default: 4301980eff3SMatthew Dillon ap->ap_pmcount = 1; 4311980eff3SMatthew Dillon break; 4321980eff3SMatthew Dillon } 4331980eff3SMatthew Dillon 4341980eff3SMatthew Dillon /* 4351980eff3SMatthew Dillon * Start the port if we succeeded. 4361980eff3SMatthew Dillon * 4371980eff3SMatthew Dillon * There's nothing to start for devices behind a port multiplier. 4381980eff3SMatthew Dillon */ 4391980eff3SMatthew Dillon if (rc == 0 && at == NULL) { 44017eab71eSMatthew Dillon if (ahci_port_start(ap)) { 441fd8bd957SMatthew Dillon kprintf("%s: failed to start command DMA on port, " 442fd8bd957SMatthew Dillon "disabling\n", PORTNAME(ap)); 443258223a3SMatthew Dillon rc = ENXIO; /* couldn't start port */ 444258223a3SMatthew Dillon } 445258223a3SMatthew Dillon } 446258223a3SMatthew Dillon 44717eab71eSMatthew Dillon /* 4483209f581SMatthew Dillon * Flush interrupts on the port. XXX 4491980eff3SMatthew Dillon * 4501980eff3SMatthew Dillon * Enable interrupts on the port whether a device is sitting on 4511980eff3SMatthew Dillon * it or not, to handle hot-plug events. 45217eab71eSMatthew Dillon */ 4531980eff3SMatthew Dillon if (at == NULL) { 454258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 455fd8bd957SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num); 456258223a3SMatthew Dillon 4571980eff3SMatthew Dillon data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE | 458258223a3SMatthew Dillon AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE | 459258223a3SMatthew Dillon AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE | 460258223a3SMatthew Dillon AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE | 4611980eff3SMatthew Dillon AHCI_PREG_IE_DHRE; 4621980eff3SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 4631980eff3SMatthew Dillon data |= AHCI_PREG_IE_SDBE; 464258223a3SMatthew Dillon #ifdef AHCI_COALESCE 4651980eff3SMatthew Dillon if (sc->sc_ccc_ports & (1 << port) 4661980eff3SMatthew Dillon data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE); 467258223a3SMatthew Dillon #endif 4681980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, data); 4691980eff3SMatthew Dillon } 470258223a3SMatthew Dillon return(rc); 471258223a3SMatthew Dillon } 472258223a3SMatthew Dillon 473fd8bd957SMatthew Dillon /* 4743209f581SMatthew Dillon * Run the port / target state machine from a main context. 4753209f581SMatthew Dillon * 4763209f581SMatthew Dillon * The state machine for the port is always run. 4773209f581SMatthew Dillon * 4783209f581SMatthew Dillon * If atx is non-NULL run the state machine for a particular target. 4793209f581SMatthew Dillon * If atx is NULL run the state machine for all targets. 4803209f581SMatthew Dillon */ 4813209f581SMatthew Dillon void 4823209f581SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap) 4833209f581SMatthew Dillon { 4843209f581SMatthew Dillon struct ata_port *at; 4853209f581SMatthew Dillon u_int32_t data; 4863209f581SMatthew Dillon int target; 4873209f581SMatthew Dillon int didsleep; 4883209f581SMatthew Dillon 4893209f581SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 4903209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT) { 4913209f581SMatthew Dillon for (target = 0; target < AHCI_MAX_PMPORTS; ++target) { 4923209f581SMatthew Dillon at = &ap->ap_ata[target]; 4933209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 4943209f581SMatthew Dillon } 4953209f581SMatthew Dillon ahci_port_init(ap, NULL); 4963209f581SMatthew Dillon } 4973209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) 4983209f581SMatthew Dillon ahci_port_reset(ap, NULL, 1); 4993209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET) 5003209f581SMatthew Dillon ahci_port_reset(ap, NULL, 0); 5013209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 5023209f581SMatthew Dillon ahci_cam_probe(ap, NULL); 5033209f581SMatthew Dillon } 5043209f581SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM) { 5053209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 5063209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 0); 5073209f581SMatthew Dillon } else if (ap->ap_probe == ATA_PROBE_GOOD) { 5083209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 1); 5093209f581SMatthew Dillon } 5103209f581SMatthew Dillon return; 5113209f581SMatthew Dillon } 5123209f581SMatthew Dillon 5133209f581SMatthew Dillon for (;;) { 5143209f581SMatthew Dillon if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) { 5153209f581SMatthew Dillon kprintf("%s: PM unable to read hot-plug bitmap\n", 5163209f581SMatthew Dillon PORTNAME(ap)); 5173209f581SMatthew Dillon break; 5183209f581SMatthew Dillon } 5193209f581SMatthew Dillon data &= (1 << ap->ap_pmcount) - 1; 5203209f581SMatthew Dillon 5213209f581SMatthew Dillon /* 5223209f581SMatthew Dillon * Stop if no ports on the target have indicated a state 5233209f581SMatthew Dillon * change. 5243209f581SMatthew Dillon */ 5253209f581SMatthew Dillon if (data == 0) 5263209f581SMatthew Dillon break; 5273209f581SMatthew Dillon 5283209f581SMatthew Dillon /* 5293209f581SMatthew Dillon * New devices showing up in the bitmap require some spin-up 5303209f581SMatthew Dillon * time before we start probing them. Reset didsleep. The 5313209f581SMatthew Dillon * first new device we detect will sleep before probing. 5323209f581SMatthew Dillon */ 5333209f581SMatthew Dillon didsleep = 0; 5343209f581SMatthew Dillon 5353209f581SMatthew Dillon for (target = 0; target < ap->ap_pmcount; ++target) { 5363209f581SMatthew Dillon at = &ap->ap_ata[target]; 5373209f581SMatthew Dillon 5383209f581SMatthew Dillon /* 5393209f581SMatthew Dillon * Check the target state for targets behind the PM 5403209f581SMatthew Dillon * which have changed state. This will adjust 5413209f581SMatthew Dillon * at_probe and set ATA_PORT_F_RESCAN 5423209f581SMatthew Dillon * 5433209f581SMatthew Dillon * We want to wait at least 4 seconds before probing 5443209f581SMatthew Dillon * a newly inserted device. If the check status 5453209f581SMatthew Dillon * indicates a device is present and in need of a 5463209f581SMatthew Dillon * hard reset, we make sure we have slept before 5473209f581SMatthew Dillon * continuing. 5483209f581SMatthew Dillon */ 5493209f581SMatthew Dillon if (data & (1 << target)) { 5503209f581SMatthew Dillon ahci_pm_check_good(ap, target); 5513209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) { 5523209f581SMatthew Dillon if (didsleep == 0) { 5533209f581SMatthew Dillon didsleep = 1; 5543209f581SMatthew Dillon ahci_os_sleep(4000); 5553209f581SMatthew Dillon } 5563209f581SMatthew Dillon } 5573209f581SMatthew Dillon } 5583209f581SMatthew Dillon 5593209f581SMatthew Dillon /* 5603209f581SMatthew Dillon * Run through the state machine as necessary. 5613209f581SMatthew Dillon */ 5623209f581SMatthew Dillon if (at->at_type == ATA_PORT_T_NONE && 5633209f581SMatthew Dillon at->at_probe != ATA_PROBE_FAILED) { 5643209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_INIT) 5653209f581SMatthew Dillon ahci_port_init(ap, at); 5663209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) 5673209f581SMatthew Dillon ahci_port_reset(ap, at, 1); 5683209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET) 5693209f581SMatthew Dillon ahci_port_reset(ap, at, 0); 5703209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_IDENT) 5713209f581SMatthew Dillon ahci_cam_probe(ap, at); 5723209f581SMatthew Dillon } 5733209f581SMatthew Dillon 5743209f581SMatthew Dillon if (data & (1 << target)) { 5753209f581SMatthew Dillon kprintf("%s: HOTPLUG event, ", 5763209f581SMatthew Dillon ATANAME(ap, at)); 5773209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_GOOD) 5783209f581SMatthew Dillon kprintf("device inserted\n"); 5793209f581SMatthew Dillon else 5803209f581SMatthew Dillon kprintf("device removed\n"); 5813209f581SMatthew Dillon } 5823209f581SMatthew Dillon 5833209f581SMatthew Dillon /* 5843209f581SMatthew Dillon * Initial conditions set automatic add/rem 5853209f581SMatthew Dillon */ 5863209f581SMatthew Dillon if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET) 5873209f581SMatthew Dillon at->at_features |= ATA_PORT_F_RESCAN; 5883209f581SMatthew Dillon 5893209f581SMatthew Dillon /* 5903209f581SMatthew Dillon * add or remove from CAM 5913209f581SMatthew Dillon */ 5923209f581SMatthew Dillon if (at->at_features & ATA_PORT_F_RESCAN) { 5933209f581SMatthew Dillon at->at_features &= ~ATA_PORT_F_RESCAN; 5943209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_FAILED) { 5953209f581SMatthew Dillon ahci_cam_changed(ap, at, 0); 5963209f581SMatthew Dillon } else if (at->at_probe == ATA_PROBE_GOOD) { 5973209f581SMatthew Dillon ahci_cam_changed(ap, at, 1); 5983209f581SMatthew Dillon } 5993209f581SMatthew Dillon } 6003209f581SMatthew Dillon } 6013209f581SMatthew Dillon } 6023209f581SMatthew Dillon } 6033209f581SMatthew Dillon 6043209f581SMatthew Dillon 6053209f581SMatthew Dillon /* 606fd8bd957SMatthew Dillon * De-initialize and detach a port. 607fd8bd957SMatthew Dillon */ 608258223a3SMatthew Dillon void 609258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 610258223a3SMatthew Dillon { 611258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 612258223a3SMatthew Dillon struct ahci_ccb *ccb; 613258223a3SMatthew Dillon 61417eab71eSMatthew Dillon /* 61517eab71eSMatthew Dillon * Ensure port is disabled and its interrupts are all flushed. 61617eab71eSMatthew Dillon */ 617258223a3SMatthew Dillon if (ap->ap_sc) { 61817eab71eSMatthew Dillon ahci_port_stop(ap, 1); 619258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 620258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 621258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 622258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 623258223a3SMatthew Dillon } 624258223a3SMatthew Dillon 625258223a3SMatthew Dillon if (ap->ap_ccbs) { 626258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 627258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 628258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 629258223a3SMatthew Dillon ccb->ccb_dmamap); 630258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 631258223a3SMatthew Dillon } 632258223a3SMatthew Dillon } 633258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 634258223a3SMatthew Dillon ap->ap_ccbs = NULL; 635258223a3SMatthew Dillon } 636258223a3SMatthew Dillon 637258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 638258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 639258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 640258223a3SMatthew Dillon } 641258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 642258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 643258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 644258223a3SMatthew Dillon } 645258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 646258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 647258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 648258223a3SMatthew Dillon } 6491980eff3SMatthew Dillon if (ap->ap_ata) { 6501980eff3SMatthew Dillon kfree(ap->ap_ata, M_DEVBUF); 6511980eff3SMatthew Dillon ap->ap_ata = NULL; 6521980eff3SMatthew Dillon } 653258223a3SMatthew Dillon 654258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 655258223a3SMatthew Dillon 656258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 657258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 658258223a3SMatthew Dillon } 659258223a3SMatthew Dillon 660fd8bd957SMatthew Dillon /* 661fd8bd957SMatthew Dillon * Start high-level command processing on the port 662fd8bd957SMatthew Dillon */ 663258223a3SMatthew Dillon int 66417eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap) 665258223a3SMatthew Dillon { 6668bf6a3ffSMatthew Dillon u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd; 667258223a3SMatthew Dillon 66817eab71eSMatthew Dillon /* 66917eab71eSMatthew Dillon * FRE must be turned on before ST. Wait for FR to go active 67017eab71eSMatthew Dillon * before turning on ST. The spec doesn't seem to think this 67117eab71eSMatthew Dillon * is necessary but waiting here avoids an on-off race in the 67217eab71eSMatthew Dillon * ahci_port_stop() code. 67317eab71eSMatthew Dillon */ 674*cec07d75SMatthew Dillon /* XXX REMOVE ME */ 6751980eff3SMatthew Dillon olds = ahci_pread(ap, AHCI_PREG_SERR); 6761980eff3SMatthew Dillon oldis= ahci_pread(ap, AHCI_PREG_IS); 6778bf6a3ffSMatthew Dillon oldtfd = ahci_pread(ap, AHCI_PREG_TFD); 6781980eff3SMatthew Dillon oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 67917eab71eSMatthew Dillon if ((r & AHCI_PREG_CMD_FRE) == 0) { 680258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 68117eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 68217eab71eSMatthew Dillon } 68317eab71eSMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) { 68417eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 68517eab71eSMatthew Dillon kprintf("%s: Cannot start FIS reception\n", 68617eab71eSMatthew Dillon PORTNAME(ap)); 68717eab71eSMatthew Dillon return (2); 68817eab71eSMatthew Dillon } 68917eab71eSMatthew Dillon } 69017eab71eSMatthew Dillon 69117eab71eSMatthew Dillon /* 69217eab71eSMatthew Dillon * Turn on ST, wait for CR to come up. 69317eab71eSMatthew Dillon */ 694258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 695258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 69617eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 6978bf6a3ffSMatthew Dillon s = ahci_pread(ap, AHCI_PREG_SERR); 6988bf6a3ffSMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 6998bf6a3ffSMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 7001980eff3SMatthew Dillon kprintf("%s: Cannot start command DMA\n" 7011980eff3SMatthew Dillon "OCMD=%b OSERR=%b\n" 7021980eff3SMatthew Dillon "NCMP=%b NSERR=%b\n" 7038bf6a3ffSMatthew Dillon "OLDIS=%b\nNEWIS=%b\n" 7048bf6a3ffSMatthew Dillon "OLDTFD=%b\nNEWTFD=%b\n", 7051980eff3SMatthew Dillon PORTNAME(ap), 7061980eff3SMatthew Dillon oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR, 7071980eff3SMatthew Dillon r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR, 7088bf6a3ffSMatthew Dillon oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS, 7098bf6a3ffSMatthew Dillon oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS); 71017eab71eSMatthew Dillon return (1); 71117eab71eSMatthew Dillon } 712258223a3SMatthew Dillon 713258223a3SMatthew Dillon #ifdef AHCI_COALESCE 71417eab71eSMatthew Dillon /* 71517eab71eSMatthew Dillon * (Re-)enable coalescing on the port. 71617eab71eSMatthew Dillon */ 717258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 718258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 719258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 720258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 721258223a3SMatthew Dillon } 722258223a3SMatthew Dillon #endif 723258223a3SMatthew Dillon 724258223a3SMatthew Dillon return (0); 725258223a3SMatthew Dillon } 726258223a3SMatthew Dillon 727fd8bd957SMatthew Dillon /* 728fd8bd957SMatthew Dillon * Stop high-level command processing on a port 729fd8bd957SMatthew Dillon */ 730258223a3SMatthew Dillon int 731258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 732258223a3SMatthew Dillon { 733258223a3SMatthew Dillon u_int32_t r; 734258223a3SMatthew Dillon 735258223a3SMatthew Dillon #ifdef AHCI_COALESCE 73617eab71eSMatthew Dillon /* 73717eab71eSMatthew Dillon * Disable coalescing on the port while it is stopped. 73817eab71eSMatthew Dillon */ 739258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 740258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 741258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 742258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 743258223a3SMatthew Dillon } 744258223a3SMatthew Dillon #endif 745258223a3SMatthew Dillon 74617eab71eSMatthew Dillon /* 74717eab71eSMatthew Dillon * Turn off ST, then wait for CR to go off. 74817eab71eSMatthew Dillon */ 749258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 750258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 751258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 752258223a3SMatthew Dillon 75317eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 75417eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (ST)\n", 75517eab71eSMatthew Dillon PORTNAME(ap)); 756258223a3SMatthew Dillon return (1); 75717eab71eSMatthew Dillon } 758258223a3SMatthew Dillon 7591980eff3SMatthew Dillon #if 0 76017eab71eSMatthew Dillon /* 76117eab71eSMatthew Dillon * Turn off FRE, then wait for FR to go off. FRE cannot 76217eab71eSMatthew Dillon * be turned off until CR transitions to 0. 76317eab71eSMatthew Dillon */ 7641980eff3SMatthew Dillon if ((r & AHCI_PREG_CMD_FR) == 0) { 7651980eff3SMatthew Dillon kprintf("%s: FR stopped, clear FRE for next start\n", 7661980eff3SMatthew Dillon PORTNAME(ap)); 7671980eff3SMatthew Dillon stop_fis_rx = 2; 7681980eff3SMatthew Dillon } 7691980eff3SMatthew Dillon #endif 77017eab71eSMatthew Dillon if (stop_fis_rx) { 77117eab71eSMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 77217eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 77317eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 77417eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (FRE)\n", 77517eab71eSMatthew Dillon PORTNAME(ap)); 776258223a3SMatthew Dillon return (2); 77717eab71eSMatthew Dillon } 77817eab71eSMatthew Dillon } 779258223a3SMatthew Dillon 780258223a3SMatthew Dillon return (0); 781258223a3SMatthew Dillon } 782258223a3SMatthew Dillon 783fd8bd957SMatthew Dillon /* 784fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 785fd8bd957SMatthew Dillon */ 786258223a3SMatthew Dillon int 787258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 788258223a3SMatthew Dillon { 789258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 790258223a3SMatthew Dillon u_int32_t cmd; 791258223a3SMatthew Dillon 792258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 793258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 794258223a3SMatthew Dillon return (1); 795258223a3SMatthew Dillon 796258223a3SMatthew Dillon /* Issue CLO */ 797258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 798258223a3SMatthew Dillon #ifdef DIAGNOSTIC 799258223a3SMatthew Dillon if (cmd & AHCI_PREG_CMD_ST) { 800258223a3SMatthew Dillon kprintf("%s: CLO requested while port running\n", 801258223a3SMatthew Dillon PORTNAME(ap)); 802258223a3SMatthew Dillon } 803258223a3SMatthew Dillon #endif 804258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 805258223a3SMatthew Dillon 806258223a3SMatthew Dillon /* Wait for completion */ 807258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 808258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 809258223a3SMatthew Dillon return (1); 810258223a3SMatthew Dillon } 811258223a3SMatthew Dillon 812258223a3SMatthew Dillon return (0); 813258223a3SMatthew Dillon } 814258223a3SMatthew Dillon 815fd8bd957SMatthew Dillon /* 8161980eff3SMatthew Dillon * Reset a port. 81717eab71eSMatthew Dillon * 8181980eff3SMatthew Dillon * If hard is 0 perform a softreset of the port. 81917eab71eSMatthew Dillon * If hard is 1 perform a hard reset of the port. 8201980eff3SMatthew Dillon * If hard is 2 perform a hard reset of the port and cycle the phy. 8211980eff3SMatthew Dillon * 8221980eff3SMatthew Dillon * If at is non-NULL an indirect port via a port-multiplier is being 8231980eff3SMatthew Dillon * reset, otherwise a direct port is being reset. 8241980eff3SMatthew Dillon * 8251980eff3SMatthew Dillon * NOTE: Indirect ports can only be soft-reset. 82617eab71eSMatthew Dillon */ 82717eab71eSMatthew Dillon int 8281980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard) 82917eab71eSMatthew Dillon { 83017eab71eSMatthew Dillon int rc; 83117eab71eSMatthew Dillon 83217eab71eSMatthew Dillon if (hard) { 8331980eff3SMatthew Dillon if (at) 8341980eff3SMatthew Dillon rc = ahci_pm_hardreset(ap, at->at_target, hard); 8351980eff3SMatthew Dillon else 8361980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 83717eab71eSMatthew Dillon } else { 8381980eff3SMatthew Dillon if (at) 8391980eff3SMatthew Dillon rc = ahci_pm_softreset(ap, at->at_target); 8401980eff3SMatthew Dillon else 84117eab71eSMatthew Dillon rc = ahci_port_softreset(ap); 8421980eff3SMatthew Dillon #if 0 8431980eff3SMatthew Dillon if (rc && at == NULL) 8441980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 8451980eff3SMatthew Dillon #endif 84617eab71eSMatthew Dillon } 84717eab71eSMatthew Dillon return(rc); 84817eab71eSMatthew Dillon } 84917eab71eSMatthew Dillon 85017eab71eSMatthew Dillon /* 851fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 852fd8bd957SMatthew Dillon * 8531980eff3SMatthew Dillon * (at) will be NULL when soft-resetting a directly-attached device, and 8541980eff3SMatthew Dillon * non-NULL when soft-resetting a device through a port multiplier. 8551980eff3SMatthew Dillon * 856fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 8571980eff3SMatthew Dillon * a reset to the connected device using device commands. 858fd8bd957SMatthew Dillon */ 859258223a3SMatthew Dillon int 860258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 861258223a3SMatthew Dillon { 862258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 863258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 864258223a3SMatthew Dillon u_int8_t *fis; 8653209f581SMatthew Dillon int error; 866258223a3SMatthew Dillon u_int32_t cmd; 867258223a3SMatthew Dillon 8683209f581SMatthew Dillon error = EIO; 8691980eff3SMatthew Dillon 8701980eff3SMatthew Dillon kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap), 8711980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD); 8721980eff3SMatthew Dillon 873258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 874258223a3SMatthew Dillon 875258223a3SMatthew Dillon crit_enter(); 8761980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 8771980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 878258223a3SMatthew Dillon 8791980eff3SMatthew Dillon /* 8801980eff3SMatthew Dillon * Remember port state in cmd (main to restore start/stop) 8811980eff3SMatthew Dillon * 8821980eff3SMatthew Dillon * Idle port. 8831980eff3SMatthew Dillon */ 8841980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 885258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 886258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 887258223a3SMatthew Dillon PORTNAME(ap)); 888258223a3SMatthew Dillon goto err; 889258223a3SMatthew Dillon } 890cf5f3a81SMatthew Dillon 891cf5f3a81SMatthew Dillon /* 8921980eff3SMatthew Dillon * Request CLO if device appears hung. 893cf5f3a81SMatthew Dillon */ 894258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 895258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 896258223a3SMatthew Dillon ahci_port_clo(ap); 897258223a3SMatthew Dillon } 898258223a3SMatthew Dillon 8991980eff3SMatthew Dillon /* 9001980eff3SMatthew Dillon * This is an attempt to clear errors so a new signature will 9011980eff3SMatthew Dillon * be latched. It isn't working properly. XXX 9021980eff3SMatthew Dillon */ 903cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 9041980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 905258223a3SMatthew Dillon 906258223a3SMatthew Dillon /* Restart port */ 90717eab71eSMatthew Dillon if (ahci_port_start(ap)) { 908258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 909258223a3SMatthew Dillon PORTNAME(ap)); 910258223a3SMatthew Dillon goto err; 911258223a3SMatthew Dillon } 912258223a3SMatthew Dillon 913258223a3SMatthew Dillon /* Check whether CLO worked */ 914258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 915258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 916258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 917258223a3SMatthew Dillon PORTNAME(ap), 918258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 919258223a3SMatthew Dillon ? "failed" : "unsupported"); 9203209f581SMatthew Dillon error = EBUSY; 921258223a3SMatthew Dillon goto err; 922258223a3SMatthew Dillon } 923258223a3SMatthew Dillon 924cec85a37SMatthew Dillon /* 925cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 926cec85a37SMatthew Dillon * 927cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 928cec85a37SMatthew Dillon * everything. 929cec85a37SMatthew Dillon */ 930258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 9311980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 932258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 933258223a3SMatthew Dillon 934258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 935cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 9361980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 9371980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 938258223a3SMatthew Dillon 939258223a3SMatthew Dillon cmd_slot->prdtl = 0; 940258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 941258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 942258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 943258223a3SMatthew Dillon 944258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 9455f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 9463209f581SMatthew Dillon if (ahci_poll(ccb, 1000, NULL) != 0 || 9471980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 9485f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 949258223a3SMatthew Dillon goto err; 950cec85a37SMatthew Dillon } 951258223a3SMatthew Dillon 952cec85a37SMatthew Dillon /* 9531980eff3SMatthew Dillon * The device may muff the PHY up. 9541980eff3SMatthew Dillon */ 9553209f581SMatthew Dillon ahci_os_sleep(10); /* 3ms min, use 10 */ 9561980eff3SMatthew Dillon 9571980eff3SMatthew Dillon /* 958cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 959cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 960cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 961cec85a37SMatthew Dillon * from the AHCI document. 962cec85a37SMatthew Dillon * 963b089d0bfSMatthew Dillon * Give the device 3ms before sending the second FIS. 964cec85a37SMatthew Dillon * 965cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 966cec85a37SMatthew Dillon * everything. 967cec85a37SMatthew Dillon */ 968cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 9691980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 9701980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 971258223a3SMatthew Dillon 972258223a3SMatthew Dillon cmd_slot->prdtl = 0; 973258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 974258223a3SMatthew Dillon 975258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 9765f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 9773209f581SMatthew Dillon if (ahci_poll(ccb, 1000, NULL) != 0 || 9781980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 9795f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 980258223a3SMatthew Dillon goto err; 981cec85a37SMatthew Dillon } 982258223a3SMatthew Dillon 9831980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 9841980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 985258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 986258223a3SMatthew Dillon PORTNAME(ap), 987258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 9883209f581SMatthew Dillon error = EBUSY; 989258223a3SMatthew Dillon goto err; 990258223a3SMatthew Dillon } 9913209f581SMatthew Dillon ahci_os_sleep(10); 992258223a3SMatthew Dillon 993fd8bd957SMatthew Dillon /* 994fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 995fd8bd957SMatthew Dillon * normal portreset we assign the port type. 996fd8bd957SMatthew Dillon * 997fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 998fd8bd957SMatthew Dillon * processing code then report if the device signature changed 999fd8bd957SMatthew Dillon * unexpectedly. 1000fd8bd957SMatthew Dillon */ 10011980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 10021980eff3SMatthew Dillon ap->ap_type = ahci_port_signature_detect(ap, NULL); 1003fd8bd957SMatthew Dillon } else { 10041980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) { 10051980eff3SMatthew Dillon kprintf("%s: device signature unexpectedly " 10061980eff3SMatthew Dillon "changed\n", PORTNAME(ap)); 10073209f581SMatthew Dillon error = EBUSY; /* XXX */ 1008fd8bd957SMatthew Dillon } 1009fd8bd957SMatthew Dillon } 10103209f581SMatthew Dillon error = 0; 10111980eff3SMatthew Dillon 10123209f581SMatthew Dillon ahci_os_sleep(3); 1013258223a3SMatthew Dillon err: 1014258223a3SMatthew Dillon if (ccb != NULL) { 10151980eff3SMatthew Dillon /* 10161980eff3SMatthew Dillon * Abort our command, if it failed, by stopping command DMA. 10171980eff3SMatthew Dillon */ 10183209f581SMatthew Dillon if (error && (ap->ap_active & (1 << ccb->ccb_slot))) { 1019258223a3SMatthew Dillon kprintf("%s: stopping the port, softreset slot " 1020258223a3SMatthew Dillon "%d was still active.\n", 1021258223a3SMatthew Dillon PORTNAME(ap), 1022258223a3SMatthew Dillon ccb->ccb_slot); 1023258223a3SMatthew Dillon ahci_port_stop(ap, 0); 1024258223a3SMatthew Dillon } 1025258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 10261980eff3SMatthew Dillon fis[15] = 0; 1027258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 10281980eff3SMatthew Dillon 10291980eff3SMatthew Dillon /* 10301980eff3SMatthew Dillon * If the target is busy use CLO to clear the busy 10311980eff3SMatthew Dillon * condition. The BSY should be cleared on the next 10321980eff3SMatthew Dillon * start. 10331980eff3SMatthew Dillon */ 10341980eff3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 10351980eff3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 10361980eff3SMatthew Dillon ahci_port_clo(ap); 10371980eff3SMatthew Dillon } 1038258223a3SMatthew Dillon } 1039258223a3SMatthew Dillon 1040cf5f3a81SMatthew Dillon /* 1041cf5f3a81SMatthew Dillon * If we failed to softreset make the port quiescent, otherwise 1042cf5f3a81SMatthew Dillon * make sure the port's start/stop state matches what it was on 1043cf5f3a81SMatthew Dillon * entry. 10441980eff3SMatthew Dillon * 10451980eff3SMatthew Dillon * Don't kill the port if the softreset is on a port multiplier 10461980eff3SMatthew Dillon * target, that would kill all the targets! 1047cf5f3a81SMatthew Dillon */ 10483209f581SMatthew Dillon if (error) { 1049cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 10503209f581SMatthew Dillon /* ap_probe set to failed */ 1051cf5f3a81SMatthew Dillon } else if (cmd & AHCI_PREG_CMD_ST) { 10523209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 10531980eff3SMatthew Dillon kprintf("%s: STARTING PORT\n", PORTNAME(ap)); 1054cf5f3a81SMatthew Dillon ahci_port_start(ap); 1055cf5f3a81SMatthew Dillon } else { 10563209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 10571980eff3SMatthew Dillon kprintf("%s: STOPPING PORT\n", PORTNAME(ap)); 1058cf5f3a81SMatthew Dillon ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE)); 1059cf5f3a81SMatthew Dillon } 10603209f581SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 1061258223a3SMatthew Dillon crit_exit(); 1062258223a3SMatthew Dillon 10631980eff3SMatthew Dillon kprintf("%s: END SOFTRESET\n", PORTNAME(ap)); 10641980eff3SMatthew Dillon 10653209f581SMatthew Dillon return (error); 1066258223a3SMatthew Dillon } 1067258223a3SMatthew Dillon 1068fd8bd957SMatthew Dillon /* 1069fd8bd957SMatthew Dillon * AHCI port reset, Section 10.4.2 1070fd8bd957SMatthew Dillon * 1071fd8bd957SMatthew Dillon * This function does a hard reset of the port. Note that the device 1072fd8bd957SMatthew Dillon * connected to the port could still end-up hung. 1073fd8bd957SMatthew Dillon */ 1074258223a3SMatthew Dillon int 10751980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard) 1076258223a3SMatthew Dillon { 1077258223a3SMatthew Dillon u_int32_t cmd, r; 10783209f581SMatthew Dillon int error; 10791980eff3SMatthew Dillon int loop; 10801980eff3SMatthew Dillon int type; 1081258223a3SMatthew Dillon 1082258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap)); 1083258223a3SMatthew Dillon 10841980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 1085cf5f3a81SMatthew Dillon 1086cf5f3a81SMatthew Dillon /* 10871980eff3SMatthew Dillon * Idle the port, 10881980eff3SMatthew Dillon */ 10891980eff3SMatthew Dillon ahci_port_stop(ap, 0); 10901980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 10913209f581SMatthew Dillon error = 0; 10921980eff3SMatthew Dillon 10931980eff3SMatthew Dillon /* 10941980eff3SMatthew Dillon * The port may have been quiescent with its SUD bit cleared, so 10951980eff3SMatthew Dillon * set the SUD (spin up device). 1096cf5f3a81SMatthew Dillon */ 1097cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1098cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1099cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1100258223a3SMatthew Dillon 11011980eff3SMatthew Dillon /* 11021980eff3SMatthew Dillon * Perform device detection. Cycle the PHY off, wait 10ms. 11031980eff3SMatthew Dillon * This simulates the SATA cable being physically unplugged. 11041980eff3SMatthew Dillon */ 11051980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1106258223a3SMatthew Dillon 11071980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED; 11081980eff3SMatthew Dillon if (hard == 2) 11091980eff3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_DISABLE; 11101980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 11113209f581SMatthew Dillon ahci_os_sleep(10); 11121980eff3SMatthew Dillon 11131980eff3SMatthew Dillon /* 11141980eff3SMatthew Dillon * Start transmitting COMRESET. COMRESET must be sent for at 11151980eff3SMatthew Dillon * least 1ms. 11161980eff3SMatthew Dillon */ 11171980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1118258223a3SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1119258223a3SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1120258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1121258223a3SMatthew Dillon } else { 1122258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1123258223a3SMatthew Dillon } 1124258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 11253209f581SMatthew Dillon ahci_os_sleep(1); 1126cf5f3a81SMatthew Dillon 1127cf5f3a81SMatthew Dillon /* 1128cf5f3a81SMatthew Dillon * Only SERR_DIAG_X needs to be cleared for TFD updates, but 1129cf5f3a81SMatthew Dillon * since we are hard-resetting the port we might as well clear 1130cf5f3a81SMatthew Dillon * the whole enchillada 1131cf5f3a81SMatthew Dillon */ 1132cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1133cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1134258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 1135258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 1136258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1137258223a3SMatthew Dillon 11381980eff3SMatthew Dillon /* 11391980eff3SMatthew Dillon * Try to determine if there is a device on the port. 11401980eff3SMatthew Dillon * 11411980eff3SMatthew Dillon * Give the device 3/10 second to at least be detected. 11421980eff3SMatthew Dillon * If we fail clear PRCS (phy detect) since we may cycled 11431980eff3SMatthew Dillon * the phy and probably caused another PRCS interrupt. 11441980eff3SMatthew Dillon */ 11451980eff3SMatthew Dillon for (loop = 30; loop; --loop) { 11461980eff3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 11471980eff3SMatthew Dillon if (r & AHCI_PREG_SSTS_DET) 11481980eff3SMatthew Dillon break; 11493209f581SMatthew Dillon ahci_os_sleep(10); 11501980eff3SMatthew Dillon } 11511980eff3SMatthew Dillon if (loop == 0) { 11521980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 11531980eff3SMatthew Dillon kprintf("%s: Port appears to be unplugged\n", 11541980eff3SMatthew Dillon PORTNAME(ap)); 11553209f581SMatthew Dillon error = ENODEV; 1156258223a3SMatthew Dillon } 1157258223a3SMatthew Dillon 1158cec85a37SMatthew Dillon /* 11591980eff3SMatthew Dillon * There is something on the port. Give the device 3 seconds 11601980eff3SMatthew Dillon * to fully negotiate. 11611980eff3SMatthew Dillon */ 11623209f581SMatthew Dillon if (error == 0 && 11631980eff3SMatthew Dillon ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS, 11641980eff3SMatthew Dillon AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) { 11651980eff3SMatthew Dillon kprintf("%s: Device may be powered down\n", 11661980eff3SMatthew Dillon PORTNAME(ap)); 11673209f581SMatthew Dillon error = ENODEV; 11681980eff3SMatthew Dillon } 11691980eff3SMatthew Dillon 11701980eff3SMatthew Dillon /* 11711980eff3SMatthew Dillon * Wait for the device to become ready. 1172cec85a37SMatthew Dillon * 1173b089d0bfSMatthew Dillon * This can take more then a second, give it 3 seconds. If we 1174b089d0bfSMatthew Dillon * succeed give the device another 3ms after that. 11751980eff3SMatthew Dillon * 11763209f581SMatthew Dillon * NOTE: Port multipliers can do two things here. First they can 11771980eff3SMatthew Dillon * return device-ready if a device is on target 0 and also 11781980eff3SMatthew Dillon * return the signature for that device. If there is no 11791980eff3SMatthew Dillon * device on target 0 then BSY/DRQ is never cleared and 11801980eff3SMatthew Dillon * it never comes ready. 1181cec85a37SMatthew Dillon */ 11823209f581SMatthew Dillon if (error == 0 && 11831980eff3SMatthew Dillon ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD, 11841980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 11851980eff3SMatthew Dillon /* 11861980eff3SMatthew Dillon * The device is bricked or its a port multiplier and will 11871980eff3SMatthew Dillon * not unbusy until we do the pmprobe CLO softreset sequence. 11881980eff3SMatthew Dillon */ 11893209f581SMatthew Dillon error = ahci_port_pmprobe(ap); 11903209f581SMatthew Dillon if (error) { 1191258223a3SMatthew Dillon kprintf("%s: Device will not come ready 0x%b\n", 1192258223a3SMatthew Dillon PORTNAME(ap), 11931980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), 11941980eff3SMatthew Dillon AHCI_PFMT_TFD_STS); 11951980eff3SMatthew Dillon } else { 11961980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 1197258223a3SMatthew Dillon } 11983209f581SMatthew Dillon } else if (error == 0) { 11991980eff3SMatthew Dillon /* 12001980eff3SMatthew Dillon * We generally will not get a port multiplier signature in 12011980eff3SMatthew Dillon * this case even if this is a port multiplier, because of 12021980eff3SMatthew Dillon * Intel's stupidity. We almost certainly got target 0 12031980eff3SMatthew Dillon * behind the PM, if there is a PM. 12041980eff3SMatthew Dillon * 12051980eff3SMatthew Dillon * Save the signature and probe for a PM. If we do not 12061980eff3SMatthew Dillon * find a PM then use the saved signature and return 12071980eff3SMatthew Dillon * success. 12081980eff3SMatthew Dillon */ 12091980eff3SMatthew Dillon type = ahci_port_signature_detect(ap, NULL); 12103209f581SMatthew Dillon error = ahci_port_pmprobe(ap); 12113209f581SMatthew Dillon if (error) { 12121980eff3SMatthew Dillon ap->ap_type = type; 12131980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 12143209f581SMatthew Dillon error = 0; 12151980eff3SMatthew Dillon } else { 12161980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 12173209f581SMatthew Dillon kprintf("%s: Port multiplier detected\n", 12181980eff3SMatthew Dillon PORTNAME(ap)); 12191980eff3SMatthew Dillon } 12201980eff3SMatthew Dillon } 1221258223a3SMatthew Dillon 1222cf5f3a81SMatthew Dillon /* 12231980eff3SMatthew Dillon * hard-stop the port if we failed. This will set ap_probe 12241980eff3SMatthew Dillon * to FAILED. 1225cf5f3a81SMatthew Dillon */ 12261980eff3SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 12273209f581SMatthew Dillon if (error) { 12283209f581SMatthew Dillon ahci_port_hardstop(ap); 12293209f581SMatthew Dillon /* ap_probe set to failed */ 12303209f581SMatthew Dillon } else { 12313209f581SMatthew Dillon ap->ap_probe = (ap->ap_type == ATA_PORT_T_PM) ? 12323209f581SMatthew Dillon ATA_PROBE_GOOD : ATA_PROBE_NEED_SOFT_RESET; 12333209f581SMatthew Dillon } 12343209f581SMatthew Dillon return (error); 1235258223a3SMatthew Dillon } 1236258223a3SMatthew Dillon 1237fd8bd957SMatthew Dillon /* 12381980eff3SMatthew Dillon * AHCI port multiplier probe. This routine is run by the hardreset code 12391980eff3SMatthew Dillon * if it gets past the device detect, whether or not BSY is found to be 12401980eff3SMatthew Dillon * stuck. 12411980eff3SMatthew Dillon * 12421980eff3SMatthew Dillon * We MUST use CLO to properly probe whether the port multiplier exists 12431980eff3SMatthew Dillon * or not. 12441980eff3SMatthew Dillon * 12451980eff3SMatthew Dillon * Return 0 on success, non-zero on failure. 12461980eff3SMatthew Dillon */ 12471980eff3SMatthew Dillon int 12481980eff3SMatthew Dillon ahci_port_pmprobe(struct ahci_port *ap) 12491980eff3SMatthew Dillon { 12501980eff3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 12511980eff3SMatthew Dillon struct ahci_ccb *ccb = NULL; 12521980eff3SMatthew Dillon u_int8_t *fis = NULL; 12531980eff3SMatthew Dillon int rc = EIO; 12541980eff3SMatthew Dillon u_int32_t cmd; 12551980eff3SMatthew Dillon int count; 12561980eff3SMatthew Dillon 12571980eff3SMatthew Dillon /* 12581980eff3SMatthew Dillon * If we don't support port multipliers don't try to detect one. 12591980eff3SMatthew Dillon */ 12601980eff3SMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0) 12611980eff3SMatthew Dillon return (ENODEV); 12621980eff3SMatthew Dillon 12631980eff3SMatthew Dillon count = 2; 12641980eff3SMatthew Dillon #if 0 12651980eff3SMatthew Dillon kprintf("%s: START PMPROBE\n", PORTNAME(ap)); 12661980eff3SMatthew Dillon #endif 12671980eff3SMatthew Dillon retry: 12681980eff3SMatthew Dillon /* 12691980eff3SMatthew Dillon * This code is only called from hardreset, which does not 12701980eff3SMatthew Dillon * high level command processing. The port should be stopped. 12711980eff3SMatthew Dillon * 12721980eff3SMatthew Dillon * Set PMA mode while the port is stopped. 12731980eff3SMatthew Dillon * 12741980eff3SMatthew Dillon * NOTE: On retry the port might be running, stopped, or failed. 12751980eff3SMatthew Dillon */ 12761980eff3SMatthew Dillon ahci_port_stop(ap, 0); 12771980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 12781980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 12791980eff3SMatthew Dillon cmd |= AHCI_PREG_CMD_PMA; 12801980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 12811980eff3SMatthew Dillon 12821980eff3SMatthew Dillon /* 12831980eff3SMatthew Dillon * Flush any errors and request CLO unconditionally, then start 12841980eff3SMatthew Dillon * the port. 12851980eff3SMatthew Dillon */ 12861980eff3SMatthew Dillon ahci_flush_tfd(ap); 12871980eff3SMatthew Dillon ahci_port_clo(ap); 12881980eff3SMatthew Dillon if (ahci_port_start(ap)) { 12891980eff3SMatthew Dillon kprintf("%s: PMPROBE failed to start port, cannot softreset\n", 12901980eff3SMatthew Dillon PORTNAME(ap)); 12911980eff3SMatthew Dillon goto err; 12921980eff3SMatthew Dillon } 12931980eff3SMatthew Dillon 12941980eff3SMatthew Dillon /* 12951980eff3SMatthew Dillon * Check whether CLO worked 12961980eff3SMatthew Dillon */ 12971980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 12981980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 12991980eff3SMatthew Dillon kprintf("%s: PMPROBE CLO %s, need port reset\n", 13001980eff3SMatthew Dillon PORTNAME(ap), 13011980eff3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 13021980eff3SMatthew Dillon ? "failed" : "unsupported"); 13031980eff3SMatthew Dillon rc = EBUSY; 13041980eff3SMatthew Dillon goto err; 13051980eff3SMatthew Dillon } 13061980eff3SMatthew Dillon 13071980eff3SMatthew Dillon /* 13081980eff3SMatthew Dillon * Prep the first H2D command with SRST feature & clear busy/reset 13091980eff3SMatthew Dillon * flags. 13101980eff3SMatthew Dillon */ 13111980eff3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 13121980eff3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 13131980eff3SMatthew Dillon 13141980eff3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 13151980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 13161980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 13171980eff3SMatthew Dillon fis[1] = 0x0F; /* Target 15 */ 13181980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT; 13191980eff3SMatthew Dillon 13201980eff3SMatthew Dillon cmd_slot->prdtl = 0; 13211980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 13221980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 13231980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 13241980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 13251980eff3SMatthew Dillon 13261980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 13271980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 13281980eff3SMatthew Dillon 13293209f581SMatthew Dillon if (ahci_poll(ccb, 1000, NULL) != 0 || 13301980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 13311980eff3SMatthew Dillon kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap)); 13321980eff3SMatthew Dillon if (--count) { 13331980eff3SMatthew Dillon fis[15] = 0; 13341980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 13351980eff3SMatthew Dillon goto retry; 13361980eff3SMatthew Dillon } 13371980eff3SMatthew Dillon goto err; 13381980eff3SMatthew Dillon } 13391980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 13401980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 13411980eff3SMatthew Dillon kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap)); 13421980eff3SMatthew Dillon } 13431980eff3SMatthew Dillon 13441980eff3SMatthew Dillon /* 13451980eff3SMatthew Dillon * The device may have muffed up the PHY when it reset. 13461980eff3SMatthew Dillon */ 13473209f581SMatthew Dillon ahci_os_sleep(10); 13481980eff3SMatthew Dillon ahci_flush_tfd(ap); 13491980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 13501980eff3SMatthew Dillon /* ahci_pm_phy_status(ap, 15, &cmd); */ 13511980eff3SMatthew Dillon 13521980eff3SMatthew Dillon /* 13531980eff3SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 13541980eff3SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 13551980eff3SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 13561980eff3SMatthew Dillon * from the AHCI document. 13571980eff3SMatthew Dillon * 13581980eff3SMatthew Dillon * Give the device 3ms before sending the second FIS. 13591980eff3SMatthew Dillon * 13601980eff3SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 13611980eff3SMatthew Dillon * everything. 13621980eff3SMatthew Dillon */ 13631980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 13641980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 13651980eff3SMatthew Dillon fis[1] = 0x0F; 13661980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 13671980eff3SMatthew Dillon 13681980eff3SMatthew Dillon cmd_slot->prdtl = 0; 13691980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 13701980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 13711980eff3SMatthew Dillon 13721980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 13731980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 13741980eff3SMatthew Dillon 13753209f581SMatthew Dillon if (ahci_poll(ccb, 1000, NULL) != 0 || 13761980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 13771980eff3SMatthew Dillon kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap)); 13781980eff3SMatthew Dillon if (--count) { 13791980eff3SMatthew Dillon fis[15] = 0; 13801980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 13811980eff3SMatthew Dillon goto retry; 13821980eff3SMatthew Dillon } 13831980eff3SMatthew Dillon goto err; 13841980eff3SMatthew Dillon } 13851980eff3SMatthew Dillon 13861980eff3SMatthew Dillon /* 13871980eff3SMatthew Dillon * What? We succeeded? Yup, but for some reason the signature 13881980eff3SMatthew Dillon * is still latched from the original detect (that saw target 0 13891980eff3SMatthew Dillon * behind the PM), and I don't know how to clear the condition 13901980eff3SMatthew Dillon * other then by retrying the whole reset sequence. 13911980eff3SMatthew Dillon */ 13921980eff3SMatthew Dillon if (--count) { 13931980eff3SMatthew Dillon fis[15] = 0; 13941980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 13951980eff3SMatthew Dillon goto retry; 13961980eff3SMatthew Dillon } 13971980eff3SMatthew Dillon 13981980eff3SMatthew Dillon /* 13991980eff3SMatthew Dillon * Get the signature. The caller sets the ap fields. 14001980eff3SMatthew Dillon */ 14011980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) { 14021980eff3SMatthew Dillon ap->ap_ata[15].at_probe = ATA_PROBE_GOOD; 14031980eff3SMatthew Dillon rc = 0; 14041980eff3SMatthew Dillon } else { 14051980eff3SMatthew Dillon rc = EBUSY; 14061980eff3SMatthew Dillon } 14071980eff3SMatthew Dillon 14081980eff3SMatthew Dillon /* 14091980eff3SMatthew Dillon * Fall through / clean up the CCB and perform error processing. 14101980eff3SMatthew Dillon */ 14111980eff3SMatthew Dillon err: 14121980eff3SMatthew Dillon if (ccb != NULL) { 14131980eff3SMatthew Dillon /* 14141980eff3SMatthew Dillon * Abort our command, if it failed, by stopping command DMA. 14151980eff3SMatthew Dillon */ 14161980eff3SMatthew Dillon #if 0 14171980eff3SMatthew Dillon kprintf("rc=%d active=%08x sactive=%08x slot=%d\n", 14181980eff3SMatthew Dillon rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot); 14191980eff3SMatthew Dillon #endif 14201980eff3SMatthew Dillon if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) { 14211980eff3SMatthew Dillon kprintf("%s: PMP stopping the port, softreset slot " 14221980eff3SMatthew Dillon "%d was still active.\n", 14231980eff3SMatthew Dillon PORTNAME(ap), 14241980eff3SMatthew Dillon ccb->ccb_slot); 14251980eff3SMatthew Dillon ahci_port_stop(ap, 0); 14261980eff3SMatthew Dillon } 14271980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 14281980eff3SMatthew Dillon fis[15] = 0; 14291980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 14301980eff3SMatthew Dillon } 14311980eff3SMatthew Dillon 14323209f581SMatthew Dillon if (rc == 0 && ahci_pm_identify(ap)) { 14333209f581SMatthew Dillon kprintf("%s: PM - cannot identify port multiplier\n", 14343209f581SMatthew Dillon PORTNAME(ap)); 14353209f581SMatthew Dillon rc = EBUSY; 14363209f581SMatthew Dillon } 14373209f581SMatthew Dillon #if 0 14383209f581SMatthew Dillon if (rc == 0 && ahci_pm_set_feature(ap, ATA_SATAFT_ASYNCNOTIFY, 1)) { 14393209f581SMatthew Dillon kprintf("%s: PM - Warning, cannot enable async notify\n", 14403209f581SMatthew Dillon PORTNAME(ap)); 14413209f581SMatthew Dillon /* ignore error */ 14423209f581SMatthew Dillon } 14433209f581SMatthew Dillon if (rc == 0) { 14443209f581SMatthew Dillon u_int32_t data; 14453209f581SMatthew Dillon if (ahci_pm_read(ap, 2, 4, &data)) 14463209f581SMatthew Dillon kprintf("Cannot read snotify\n"); 14473209f581SMatthew Dillon else 14483209f581SMatthew Dillon kprintf("Read snotify %08x\n", data); 14493209f581SMatthew Dillon } 14503209f581SMatthew Dillon #endif 14513209f581SMatthew Dillon 14521980eff3SMatthew Dillon /* 14531980eff3SMatthew Dillon * If we failed turn off PMA, otherwise identify the port multiplier. 14541980eff3SMatthew Dillon * CAM will iterate the devices. 14551980eff3SMatthew Dillon */ 14561980eff3SMatthew Dillon if (rc) { 14571980eff3SMatthew Dillon ahci_port_stop(ap, 0); 14581980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 14591980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 14601980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 14611980eff3SMatthew Dillon } 14621980eff3SMatthew Dillon ahci_port_stop(ap, 0); 14631980eff3SMatthew Dillon 14641980eff3SMatthew Dillon #if 0 14651980eff3SMatthew Dillon kprintf("%s: END PMPROBE\n", PORTNAME(ap)); 14661980eff3SMatthew Dillon #endif 14671980eff3SMatthew Dillon 14681980eff3SMatthew Dillon return(rc); 14691980eff3SMatthew Dillon } 14701980eff3SMatthew Dillon 14711980eff3SMatthew Dillon 14721980eff3SMatthew Dillon /* 1473cf5f3a81SMatthew Dillon * Hard-stop on hot-swap device removal. See 10.10.1 1474cf5f3a81SMatthew Dillon * 1475cf5f3a81SMatthew Dillon * Place the port in a mode that will allow it to detect hot-swap insertions. 1476cf5f3a81SMatthew Dillon * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't 1477cf5f3a81SMatthew Dillon * seem to do the job. 1478cf5f3a81SMatthew Dillon */ 1479cf5f3a81SMatthew Dillon void 1480cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap) 1481cf5f3a81SMatthew Dillon { 14821980eff3SMatthew Dillon struct ata_port *at; 1483cf5f3a81SMatthew Dillon u_int32_t r; 1484cf5f3a81SMatthew Dillon u_int32_t cmd; 14851980eff3SMatthew Dillon int i; 1486cf5f3a81SMatthew Dillon 1487cf5f3a81SMatthew Dillon /* 1488cf5f3a81SMatthew Dillon * Stop the port. We can't modify things like SUD if the port 1489cf5f3a81SMatthew Dillon * is running. 1490cf5f3a81SMatthew Dillon */ 1491cf5f3a81SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 14921980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 14931980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1494cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 1495cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 1496cf5f3a81SMatthew Dillon 1497cf5f3a81SMatthew Dillon /* 14981980eff3SMatthew Dillon * Clean up AT sub-ports on SATA port. 14991980eff3SMatthew Dillon */ 15001980eff3SMatthew Dillon for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) { 15011980eff3SMatthew Dillon at = &ap->ap_ata[i]; 15021980eff3SMatthew Dillon at->at_type = ATA_PORT_T_NONE; 15033209f581SMatthew Dillon at->at_probe = ATA_PROBE_FAILED; 15041980eff3SMatthew Dillon } 15051980eff3SMatthew Dillon 15061980eff3SMatthew Dillon /* 15071980eff3SMatthew Dillon * Turn off port-multiplier control bit 15081980eff3SMatthew Dillon */ 15091980eff3SMatthew Dillon if (cmd & AHCI_PREG_CMD_PMA) { 15101980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 15111980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 15121980eff3SMatthew Dillon } 15131980eff3SMatthew Dillon 15141980eff3SMatthew Dillon /* 1515cf5f3a81SMatthew Dillon * Make sure FRE is active. There isn't anything we can do if it 1516cf5f3a81SMatthew Dillon * fails so just ignore errors. 1517cf5f3a81SMatthew Dillon */ 1518cf5f3a81SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 1519cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE; 1520cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1521cf5f3a81SMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) 1522cf5f3a81SMatthew Dillon ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR); 1523cf5f3a81SMatthew Dillon } 1524cf5f3a81SMatthew Dillon 1525cf5f3a81SMatthew Dillon /* 1526cf5f3a81SMatthew Dillon * 10.10.3 DET must be set to 0 before setting SUD to 0. 1527cf5f3a81SMatthew Dillon * 10.10.1 place us in the Listen state. 1528cf5f3a81SMatthew Dillon * 1529cf5f3a81SMatthew Dillon * Deactivating SUD only applies if the controller supports SUD. 1530cf5f3a81SMatthew Dillon */ 1531cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 15323209f581SMatthew Dillon ahci_os_sleep(1); 1533cf5f3a81SMatthew Dillon if (cmd & AHCI_PREG_CMD_SUD) { 1534cf5f3a81SMatthew Dillon cmd &= ~AHCI_PREG_CMD_SUD; 1535cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1536cf5f3a81SMatthew Dillon } 15373209f581SMatthew Dillon ahci_os_sleep(1); 1538cf5f3a81SMatthew Dillon 1539cf5f3a81SMatthew Dillon /* 1540cf5f3a81SMatthew Dillon * Transition su to the spin-up state. HVA shall send COMRESET and 1541cf5f3a81SMatthew Dillon * begin initialization sequence (whatever that means). 1542cf5f3a81SMatthew Dillon * 1543cf5f3a81SMatthew Dillon * This only applies if the controller supports SUD. 1544cf5f3a81SMatthew Dillon */ 1545cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1546cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 15473209f581SMatthew Dillon ahci_os_sleep(1); 1548cf5f3a81SMatthew Dillon 1549cf5f3a81SMatthew Dillon /* 1550cf5f3a81SMatthew Dillon * Transition us to the Reset state. Theoretically we send a 1551cf5f3a81SMatthew Dillon * continuous stream of COMRESETs in this state. 1552cf5f3a81SMatthew Dillon */ 1553cf5f3a81SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1554cf5f3a81SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1555cf5f3a81SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1556cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1557cf5f3a81SMatthew Dillon } else { 1558cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1559cf5f3a81SMatthew Dillon } 1560cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 15613209f581SMatthew Dillon ahci_os_sleep(1); 1562cf5f3a81SMatthew Dillon 1563cf5f3a81SMatthew Dillon /* 1564cf5f3a81SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 1565cf5f3a81SMatthew Dillon */ 1566cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1567cf5f3a81SMatthew Dillon 1568cf5f3a81SMatthew Dillon /* 1569cf5f3a81SMatthew Dillon * Leave us in COMRESET (both SUD and INIT active), the HBA should 1570cf5f3a81SMatthew Dillon * hopefully send us a DIAG_X-related interrupt if it receives 1571cf5f3a81SMatthew Dillon * a COMINIT, and if not that then at least a Phy transition 1572cf5f3a81SMatthew Dillon * interrupt. 1573cf5f3a81SMatthew Dillon * 1574cf5f3a81SMatthew Dillon * If we transition INIT from 1->0 to begin the initalization 1575cf5f3a81SMatthew Dillon * sequence it is unclear if that sequence will remain active 1576cf5f3a81SMatthew Dillon * until the next device insertion. 1577cf5f3a81SMatthew Dillon * 1578cf5f3a81SMatthew Dillon * If we go back to the listen state it is unclear if the 1579cf5f3a81SMatthew Dillon * device will actually send us a COMINIT, since we aren't 1580cf5f3a81SMatthew Dillon * sending any COMRESET's 1581cf5f3a81SMatthew Dillon */ 1582cf5f3a81SMatthew Dillon /* NOP */ 1583cf5f3a81SMatthew Dillon } 1584cf5f3a81SMatthew Dillon 1585cf5f3a81SMatthew Dillon /* 1586cf5f3a81SMatthew Dillon * Multiple events may have built up in the TFD. The spec is not very 1587cf5f3a81SMatthew Dillon * clear on this but it does seem to serialize events so clearing DIAG_X 1588cf5f3a81SMatthew Dillon * just once might not do the job during a reset sequence. 1589cf5f3a81SMatthew Dillon */ 1590cf5f3a81SMatthew Dillon void 1591cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap) 1592cf5f3a81SMatthew Dillon { 1593cf5f3a81SMatthew Dillon u_int32_t r; 1594cf5f3a81SMatthew Dillon 1595cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 15961980eff3SMatthew Dillon while (r & AHCI_PREG_SERR_DIAG_X) { 15971980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X); 15983209f581SMatthew Dillon ahci_os_sleep(1); 1599cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 1600cf5f3a81SMatthew Dillon } 1601cf5f3a81SMatthew Dillon } 1602cf5f3a81SMatthew Dillon 1603cf5f3a81SMatthew Dillon /* 1604fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 1605fd8bd957SMatthew Dillon * DISK. 1606fd8bd957SMatthew Dillon */ 1607fd8bd957SMatthew Dillon int 16081980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at) 1609fd8bd957SMatthew Dillon { 1610fd8bd957SMatthew Dillon u_int32_t sig; 1611fd8bd957SMatthew Dillon 1612fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 16131980eff3SMatthew Dillon kprintf("%s: sig %08x\n", ATANAME(ap, at), sig); 1614fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 1615fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 16161980eff3SMatthew Dillon } else if ((sig & 0xffff0000) == 16171980eff3SMatthew Dillon (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) { 16181980eff3SMatthew Dillon return(ATA_PORT_T_PM); 1619fd8bd957SMatthew Dillon } else { 1620fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 1621fd8bd957SMatthew Dillon } 1622fd8bd957SMatthew Dillon } 1623fd8bd957SMatthew Dillon 1624fd8bd957SMatthew Dillon /* 1625fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 1626fd8bd957SMatthew Dillon */ 1627258223a3SMatthew Dillon int 1628258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 1629258223a3SMatthew Dillon { 1630258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1631258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1632258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1633258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 1634258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1635258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 1636258223a3SMatthew Dillon int error; 1637258223a3SMatthew Dillon 1638258223a3SMatthew Dillon if (xa->datalen == 0) { 1639258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 1640258223a3SMatthew Dillon return (0); 1641258223a3SMatthew Dillon } 1642258223a3SMatthew Dillon 1643258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 1644258223a3SMatthew Dillon xa->data, xa->datalen, 1645258223a3SMatthew Dillon ahci_load_prdt_callback, 1646258223a3SMatthew Dillon &prdt, 1647258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 1648258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 1649258223a3SMatthew Dillon if (error != 0) { 1650258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 1651258223a3SMatthew Dillon return (1); 1652258223a3SMatthew Dillon } 1653258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 1654258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 1655258223a3SMatthew Dillon 1656258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 1657258223a3SMatthew Dillon 1658258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1659258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1660258223a3SMatthew Dillon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1661258223a3SMatthew Dillon 1662258223a3SMatthew Dillon return (0); 1663258223a3SMatthew Dillon 1664258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1665258223a3SMatthew Dillon diagerr: 1666258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1667258223a3SMatthew Dillon return (1); 1668258223a3SMatthew Dillon #endif 1669258223a3SMatthew Dillon } 1670258223a3SMatthew Dillon 1671258223a3SMatthew Dillon /* 1672258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 1673258223a3SMatthew Dillon * list is a temporary structure. 1674258223a3SMatthew Dillon */ 1675258223a3SMatthew Dillon static 1676258223a3SMatthew Dillon void 1677258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 1678258223a3SMatthew Dillon int error) 1679258223a3SMatthew Dillon { 1680258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 1681258223a3SMatthew Dillon u_int64_t addr; 1682258223a3SMatthew Dillon 1683258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 1684258223a3SMatthew Dillon 1685258223a3SMatthew Dillon while (nsegs) { 1686258223a3SMatthew Dillon addr = segs->ds_addr; 1687258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 1688258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 1689258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1690258223a3SMatthew Dillon KKASSERT((addr & 1) == 0); 1691258223a3SMatthew Dillon KKASSERT((segs->ds_len & 1) == 0); 1692258223a3SMatthew Dillon #endif 1693258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 1694258223a3SMatthew Dillon --nsegs; 1695258223a3SMatthew Dillon if (nsegs) 1696258223a3SMatthew Dillon ++prd; 1697258223a3SMatthew Dillon ++segs; 1698258223a3SMatthew Dillon } 1699258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 1700258223a3SMatthew Dillon } 1701258223a3SMatthew Dillon 1702258223a3SMatthew Dillon void 1703258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 1704258223a3SMatthew Dillon { 1705258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1706258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1707258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1708258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1709258223a3SMatthew Dillon 1710258223a3SMatthew Dillon if (xa->datalen != 0) { 1711258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1712258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1713258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1714258223a3SMatthew Dillon 1715258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1716258223a3SMatthew Dillon 1717258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) 1718258223a3SMatthew Dillon xa->resid = 0; 1719258223a3SMatthew Dillon else 1720258223a3SMatthew Dillon xa->resid = xa->datalen - 1721258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 1722258223a3SMatthew Dillon } 1723258223a3SMatthew Dillon } 1724258223a3SMatthew Dillon 17255f8c1efdSMatthew Dillon /* 17265f8c1efdSMatthew Dillon * Start a command and poll for completion. 17275f8c1efdSMatthew Dillon * 17283209f581SMatthew Dillon * timeout is in ms and only counts once the command gets on-chip. 17293209f581SMatthew Dillon * 17305f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 17315f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 17325f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 17335f8c1efdSMatthew Dillon * 17345f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 1735cf5f3a81SMatthew Dillon * 1736cf5f3a81SMatthew Dillon * NOTE: If the port is in a failed state and stopped we do not try 1737cf5f3a81SMatthew Dillon * to activate the ccb. 17385f8c1efdSMatthew Dillon */ 1739258223a3SMatthew Dillon int 1740258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *)) 1741258223a3SMatthew Dillon { 1742258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1743258223a3SMatthew Dillon 1744cf5f3a81SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) { 1745cf5f3a81SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1746cf5f3a81SMatthew Dillon return(1); 1747cf5f3a81SMatthew Dillon } 1748258223a3SMatthew Dillon crit_enter(); 1749258223a3SMatthew Dillon ahci_start(ccb); 17501980eff3SMatthew Dillon 1751258223a3SMatthew Dillon do { 17523209f581SMatthew Dillon ahci_port_intr(ap); 17533209f581SMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP && 17543209f581SMatthew Dillon ccb->ccb_xa.state != ATA_S_PENDING) { 1755258223a3SMatthew Dillon crit_exit(); 1756258223a3SMatthew Dillon return (0); 1757258223a3SMatthew Dillon } 17583209f581SMatthew Dillon ahci_os_sleep(100); 17593209f581SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) 17603209f581SMatthew Dillon timeout -= 100; 17613209f581SMatthew Dillon } while (timeout > 0); 17625f8c1efdSMatthew Dillon 17635f8c1efdSMatthew Dillon kprintf("%s: Poll timed-out for slot %d state %d\n", 17641980eff3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, ccb->ccb_xa.state); 17655f8c1efdSMatthew Dillon 17665f8c1efdSMatthew Dillon if (timeout_fn != NULL) { 1767258223a3SMatthew Dillon timeout_fn(ccb); 17685f8c1efdSMatthew Dillon } else { 17695f8c1efdSMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) 17705f8c1efdSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 17715f8c1efdSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 17725f8c1efdSMatthew Dillon } 1773258223a3SMatthew Dillon crit_exit(); 1774258223a3SMatthew Dillon 1775258223a3SMatthew Dillon return (1); 1776258223a3SMatthew Dillon } 1777258223a3SMatthew Dillon 17783209f581SMatthew Dillon static 17793209f581SMatthew Dillon __inline 17803209f581SMatthew Dillon void 17813209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb) 17823209f581SMatthew Dillon { 17833209f581SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) { 17843209f581SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING; 17853209f581SMatthew Dillon callout_reset(&ccb->ccb_timeout, 17863209f581SMatthew Dillon (ccb->ccb_xa.timeout * hz + 999) / 1000, 17873209f581SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 17883209f581SMatthew Dillon } 17893209f581SMatthew Dillon } 17903209f581SMatthew Dillon 1791258223a3SMatthew Dillon void 1792258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 1793258223a3SMatthew Dillon { 1794258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1795258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1796258223a3SMatthew Dillon 1797258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 1798258223a3SMatthew Dillon 1799258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 1800258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 1801258223a3SMatthew Dillon 1802258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 1803258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 1804258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 1805258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1806258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 1807258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 1808258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1809258223a3SMatthew Dillon 1810258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 1811258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 1812258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 1813258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 1814258223a3SMatthew Dillon 1815258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 18161980eff3SMatthew Dillon /* 18171980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 18181980eff3SMatthew Dillon * standard commands. 18191980eff3SMatthew Dillon */ 18201980eff3SMatthew Dillon if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) { 1821258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 18221980eff3SMatthew Dillon } else { 18233209f581SMatthew Dillon ahci_start_timeout(ccb); 1824258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1825258223a3SMatthew Dillon ap->ap_sactive |= (1 << ccb->ccb_slot); 1826258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1827258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot); 1828258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1829258223a3SMatthew Dillon } 1830258223a3SMatthew Dillon } else { 18315f8c1efdSMatthew Dillon /* 18325f8c1efdSMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 18331980eff3SMatthew Dillon * command. Allow up to <limit> non-NCQ commands to be active. 18341980eff3SMatthew Dillon * 18351980eff3SMatthew Dillon * XXX If ap is a port multiplier only allow 1. At least the 18361980eff3SMatthew Dillon * NVidia-MCP77 part seems to barf if more then one 18371980eff3SMatthew Dillon * command is activated, even though it isn't NCQ. 18381980eff3SMatthew Dillon * 18391980eff3SMatthew Dillon * If I set up more then one I get phy errors and the 18401980eff3SMatthew Dillon * port fails. 18415f8c1efdSMatthew Dillon */ 18421980eff3SMatthew Dillon int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2; 18431980eff3SMatthew Dillon if (ap->ap_sactive || ap->ap_active_cnt >= limit) { 1844258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 18451980eff3SMatthew Dillon } else { 18463209f581SMatthew Dillon ahci_start_timeout(ccb); 1847258223a3SMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 1848258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1849258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1850258223a3SMatthew Dillon ap->ap_active_cnt++; 1851258223a3SMatthew Dillon } 1852258223a3SMatthew Dillon } 1853258223a3SMatthew Dillon } 1854258223a3SMatthew Dillon 1855258223a3SMatthew Dillon void 1856258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap) 1857258223a3SMatthew Dillon { 1858258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1859258223a3SMatthew Dillon u_int32_t sact_change = 0; 1860258223a3SMatthew Dillon 1861258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1862258223a3SMatthew Dillon 1863258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1864258223a3SMatthew Dillon if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ)) 1865258223a3SMatthew Dillon return; 1866258223a3SMatthew Dillon 1867258223a3SMatthew Dillon /* Start all the NCQ commands at the head of the pending list. */ 1868258223a3SMatthew Dillon do { 1869258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 18703209f581SMatthew Dillon ahci_start_timeout(nextccb); 1871258223a3SMatthew Dillon sact_change |= 1 << nextccb->ccb_slot; 1872258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1873258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1874258223a3SMatthew Dillon } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)); 1875258223a3SMatthew Dillon 1876258223a3SMatthew Dillon ap->ap_sactive |= sact_change; 1877258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, sact_change); 1878258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, sact_change); 1879258223a3SMatthew Dillon 1880258223a3SMatthew Dillon return; 1881258223a3SMatthew Dillon } 1882258223a3SMatthew Dillon 1883258223a3SMatthew Dillon void 1884258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq) 1885258223a3SMatthew Dillon { 1886258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1887258223a3SMatthew Dillon 1888258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1889258223a3SMatthew Dillon if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) { 1890258223a3SMatthew Dillon KKASSERT(last_was_ncq == 0); /* otherwise it should have 1891258223a3SMatthew Dillon * been started already. */ 1892258223a3SMatthew Dillon 18931980eff3SMatthew Dillon /* 18941980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 18951980eff3SMatthew Dillon * standard commands. 18961980eff3SMatthew Dillon */ 1897258223a3SMatthew Dillon if (ap->ap_active == 0) 1898258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 1899258223a3SMatthew Dillon else 19001980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 1901258223a3SMatthew Dillon } else if (nextccb) { 19021980eff3SMatthew Dillon if (ap->ap_sactive || last_was_ncq) 1903258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1904258223a3SMatthew Dillon 19051980eff3SMatthew Dillon /* 19061980eff3SMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 19071980eff3SMatthew Dillon * command. Then keep up to 2 standard commands on-chip at 19081980eff3SMatthew Dillon * a time. 19091980eff3SMatthew Dillon */ 19101980eff3SMatthew Dillon if (ap->ap_sactive) 1911258223a3SMatthew Dillon return; 1912258223a3SMatthew Dillon 19131980eff3SMatthew Dillon while (ap->ap_active_cnt < 2 && 19141980eff3SMatthew Dillon nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) { 1915258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 19163209f581SMatthew Dillon ahci_start_timeout(nextccb); 1917258223a3SMatthew Dillon ap->ap_active |= 1 << nextccb->ccb_slot; 1918258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1919258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot); 1920258223a3SMatthew Dillon ap->ap_active_cnt++; 1921258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 19221980eff3SMatthew Dillon } 1923258223a3SMatthew Dillon } 1924258223a3SMatthew Dillon } 1925258223a3SMatthew Dillon 1926258223a3SMatthew Dillon void 1927258223a3SMatthew Dillon ahci_intr(void *arg) 1928258223a3SMatthew Dillon { 1929258223a3SMatthew Dillon struct ahci_softc *sc = arg; 1930258223a3SMatthew Dillon u_int32_t is, ack = 0; 1931258223a3SMatthew Dillon int port; 1932258223a3SMatthew Dillon 1933258223a3SMatthew Dillon /* Read global interrupt status */ 1934258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 1935258223a3SMatthew Dillon if (is == 0 || is == 0xffffffff) 1936258223a3SMatthew Dillon return; 1937258223a3SMatthew Dillon ack = is; 1938258223a3SMatthew Dillon 1939258223a3SMatthew Dillon #ifdef AHCI_COALESCE 1940258223a3SMatthew Dillon /* Check coalescing interrupt first */ 1941258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 1942258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 1943258223a3SMatthew Dillon DEVNAME(sc)); 1944258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 1945258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 1946258223a3SMatthew Dillon } 1947258223a3SMatthew Dillon #endif 1948258223a3SMatthew Dillon 1949258223a3SMatthew Dillon /* Process interrupts for each port */ 1950258223a3SMatthew Dillon while (is) { 1951258223a3SMatthew Dillon port = ffs(is) - 1; 19523209f581SMatthew Dillon if (sc->sc_ports[port]) 19533209f581SMatthew Dillon ahci_port_intr(sc->sc_ports[port]); 1954258223a3SMatthew Dillon is &= ~(1 << port); 1955258223a3SMatthew Dillon } 1956258223a3SMatthew Dillon 1957258223a3SMatthew Dillon /* Finally, acknowledge global interrupt */ 1958258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, ack); 1959258223a3SMatthew Dillon } 1960258223a3SMatthew Dillon 19613209f581SMatthew Dillon void 19623209f581SMatthew Dillon ahci_port_intr(struct ahci_port *ap) 1963258223a3SMatthew Dillon { 1964258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 19653209f581SMatthew Dillon u_int32_t is, ci_saved, ci_masked; 196622181ab7SMatthew Dillon int slot; 1967258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 19681980eff3SMatthew Dillon struct ata_port *ccb_at = NULL; 1969258223a3SMatthew Dillon volatile u_int32_t *active; 1970258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1971258223a3SMatthew Dillon u_int32_t tmp; 1972258223a3SMatthew Dillon #endif 197322181ab7SMatthew Dillon enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT, 197422181ab7SMatthew Dillon NEED_HOTPLUG_REMOVE } need = NEED_NOTHING; 1975258223a3SMatthew Dillon 1976258223a3SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 1977*cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_DPS) 1978*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS); 1979258223a3SMatthew Dillon 19801980eff3SMatthew Dillon #if 0 19811980eff3SMatthew Dillon kprintf("%s: INTERRUPT %b\n", PORTNAME(ap), 19821980eff3SMatthew Dillon is, AHCI_PFMT_IS); 19831980eff3SMatthew Dillon #endif 19841980eff3SMatthew Dillon 19853209f581SMatthew Dillon /* 19863209f581SMatthew Dillon * Ack the port interrupt 19873209f581SMatthew Dillon */ 1988258223a3SMatthew Dillon if (ap->ap_sactive) { 1989258223a3SMatthew Dillon /* Active NCQ commands - use SActive instead of CI */ 1990258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 1991258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1992258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 1993258223a3SMatthew Dillon active = &ap->ap_sactive; 1994258223a3SMatthew Dillon } else { 1995258223a3SMatthew Dillon /* Save CI */ 1996258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 1997258223a3SMatthew Dillon active = &ap->ap_active; 1998258223a3SMatthew Dillon } 1999258223a3SMatthew Dillon 20001980eff3SMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 2001cf5f3a81SMatthew Dillon /* 2002cf5f3a81SMatthew Dillon * Command failed. See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. 20031980eff3SMatthew Dillon * 20041980eff3SMatthew Dillon * This stops command processing. 2005cf5f3a81SMatthew Dillon */ 2006258223a3SMatthew Dillon u_int32_t tfd, serr; 2007258223a3SMatthew Dillon int err_slot; 2008258223a3SMatthew Dillon 2009258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 2010258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2011258223a3SMatthew Dillon 2012cf5f3a81SMatthew Dillon /* 2013cf5f3a81SMatthew Dillon * If no NCQ commands are active the error slot is easily 2014cf5f3a81SMatthew Dillon * determined, otherwise we have to extract the error 2015cf5f3a81SMatthew Dillon * from the log page. 2016cf5f3a81SMatthew Dillon */ 2017258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 2018cf5f3a81SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 2019cf5f3a81SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 2020258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 20211980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 2022258223a3SMatthew Dillon 2023258223a3SMatthew Dillon /* Preserve received taskfile data from the RFIS. */ 2024258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 2025258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 2026cf5f3a81SMatthew Dillon } else { 2027cf5f3a81SMatthew Dillon err_slot = -1; 2028cf5f3a81SMatthew Dillon } 2029258223a3SMatthew Dillon 20301980eff3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n", 20311980eff3SMatthew Dillon PORTNAME(ap), err_slot, 20321980eff3SMatthew Dillon tfd, AHCI_PFMT_TFD_STS, 20331980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 2034258223a3SMatthew Dillon 2035cf5f3a81SMatthew Dillon /* Stopping the port clears CI and SACT */ 2036258223a3SMatthew Dillon ahci_port_stop(ap, 0); 203722181ab7SMatthew Dillon need = NEED_RESTART; 2038258223a3SMatthew Dillon 2039cf5f3a81SMatthew Dillon /* 2040cf5f3a81SMatthew Dillon * Clear SERR (primarily DIAG_X) to enable capturing of the 2041cf5f3a81SMatthew Dillon * next error. 2042cf5f3a81SMatthew Dillon */ 2043258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 2044258223a3SMatthew Dillon 2045258223a3SMatthew Dillon /* Acknowledge the interrupts we can recover from. */ 2046cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2047*cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS)); 20481980eff3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS); 2049258223a3SMatthew Dillon 2050258223a3SMatthew Dillon /* If device hasn't cleared its busy status, try to idle it. */ 2051258223a3SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 20521980eff3SMatthew Dillon kprintf("%s: Attempting to idle device ccb=%p\n", 20531980eff3SMatthew Dillon PORTNAME(ap), ccb_at); 20541980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 20551980eff3SMatthew Dillon goto fatal; 20561980eff3SMatthew Dillon /* 20571980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 20581980eff3SMatthew Dillon * 20591980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 20601980eff3SMatthew Dillon * busy condition and make the ahci port usable for 20611980eff3SMatthew Dillon * the remaining devices. 20621980eff3SMatthew Dillon */ 20631980eff3SMatthew Dillon if (ccb_at) { 20641980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 20651980eff3SMatthew Dillon ahci_port_clo(ap); 20661980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 206717eab71eSMatthew Dillon kprintf("%s: Unable to idle device, port " 206817eab71eSMatthew Dillon "bricked on us\n", 2069258223a3SMatthew Dillon PORTNAME(ap)); 2070258223a3SMatthew Dillon goto fatal; 2071258223a3SMatthew Dillon } 2072258223a3SMatthew Dillon 2073258223a3SMatthew Dillon /* Had to reset device, can't gather extended info. */ 2074258223a3SMatthew Dillon } else if (ap->ap_sactive) { 20751980eff3SMatthew Dillon /* 20761980eff3SMatthew Dillon * Recover the NCQ error from log page 10h. 20771980eff3SMatthew Dillon * 20781980eff3SMatthew Dillon * XXX NCQ currently not supported with port 20791980eff3SMatthew Dillon * multiplier. 20801980eff3SMatthew Dillon */ 2081258223a3SMatthew Dillon ahci_port_read_ncq_error(ap, &err_slot); 2082*cec07d75SMatthew Dillon kprintf("recover from NCQ error err_slot %d\n", 2083*cec07d75SMatthew Dillon err_slot); 2084258223a3SMatthew Dillon if (err_slot < 0) 2085258223a3SMatthew Dillon goto failall; 2086258223a3SMatthew Dillon 2087258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n", 2088258223a3SMatthew Dillon PORTNAME(ap), err_slot); 2089258223a3SMatthew Dillon 2090258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2091258223a3SMatthew Dillon } else { 2092258223a3SMatthew Dillon /* Didn't reset, could gather extended info from log. */ 20931980eff3SMatthew Dillon kprintf("%s: didn't reset err_slot %d " 20941980eff3SMatthew Dillon "sact=%08x act=%08x\n", 20951980eff3SMatthew Dillon PORTNAME(ap), 2096cf5f3a81SMatthew Dillon err_slot, ap->ap_sactive, ap->ap_active); 2097258223a3SMatthew Dillon } 2098258223a3SMatthew Dillon 2099258223a3SMatthew Dillon /* 2100258223a3SMatthew Dillon * If we couldn't determine the errored slot, reset the port 2101258223a3SMatthew Dillon * and fail all the active slots. 2102258223a3SMatthew Dillon */ 2103258223a3SMatthew Dillon if (err_slot == -1) { 21041980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 21051980eff3SMatthew Dillon goto fatal; 21061980eff3SMatthew Dillon /* 21071980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 21081980eff3SMatthew Dillon * 21091980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 21101980eff3SMatthew Dillon * busy condition and make the ahci port usable for 21111980eff3SMatthew Dillon * the remaining devices. 21121980eff3SMatthew Dillon */ 21131980eff3SMatthew Dillon if (ccb_at) { 21141980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 21151980eff3SMatthew Dillon ahci_port_clo(ap); 21161980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 211717eab71eSMatthew Dillon kprintf("%s: Unable to idle device after " 211817eab71eSMatthew Dillon "NCQ error, port bricked on us\n", 2119258223a3SMatthew Dillon PORTNAME(ap)); 2120258223a3SMatthew Dillon goto fatal; 2121258223a3SMatthew Dillon } 2122258223a3SMatthew Dillon kprintf("%s: couldn't recover NCQ error, failing " 2123258223a3SMatthew Dillon "all outstanding commands.\n", 2124258223a3SMatthew Dillon PORTNAME(ap)); 2125258223a3SMatthew Dillon goto failall; 2126258223a3SMatthew Dillon } 2127258223a3SMatthew Dillon 2128258223a3SMatthew Dillon /* Clear the failed command in saved CI so completion runs. */ 2129258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 2130258223a3SMatthew Dillon 2131258223a3SMatthew Dillon /* Note the error in the ata_xfer. */ 2132258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2133258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2134258223a3SMatthew Dillon 2135258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2136258223a3SMatthew Dillon /* There may only be one outstanding standard command now. */ 2137258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 2138258223a3SMatthew Dillon tmp = ci_saved; 2139258223a3SMatthew Dillon if (tmp) { 2140258223a3SMatthew Dillon slot = ffs(tmp) - 1; 2141258223a3SMatthew Dillon tmp &= ~(1 << slot); 2142258223a3SMatthew Dillon KKASSERT(tmp == 0); 2143258223a3SMatthew Dillon } 2144258223a3SMatthew Dillon } 2145258223a3SMatthew Dillon #endif 21461980eff3SMatthew Dillon } else if (is & AHCI_PREG_IS_DHRS) { 21471980eff3SMatthew Dillon /* 21481980eff3SMatthew Dillon * Command posted D2H register FIS to the rfis. This 21498bf6a3ffSMatthew Dillon * does NOT stop command processing and it is unclear 21508bf6a3ffSMatthew Dillon * how we are supposed to deal with it other then using 21518bf6a3ffSMatthew Dillon * only a queue of 1. 21528bf6a3ffSMatthew Dillon * 21538bf6a3ffSMatthew Dillon * We must copy the port rfis to the ccb and restart 21548bf6a3ffSMatthew Dillon * command processing. ahci_pm_read() does not function 21558bf6a3ffSMatthew Dillon * without this support. 21561980eff3SMatthew Dillon */ 21571980eff3SMatthew Dillon int err_slot; 21581980eff3SMatthew Dillon 21591980eff3SMatthew Dillon if (ap->ap_sactive == 0) { 21601980eff3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 21611980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 21621980eff3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 21631980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 21641980eff3SMatthew Dillon 21651980eff3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 21661980eff3SMatthew Dillon sizeof(struct ata_fis_d2h)); 21671980eff3SMatthew Dillon } else { 21681980eff3SMatthew Dillon kprintf("%s: Unexpected DHRS posted while " 21691980eff3SMatthew Dillon "NCQ running\n", PORTNAME(ap)); 21701980eff3SMatthew Dillon err_slot = -1; 2171258223a3SMatthew Dillon } 2172*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS); 2173*cec07d75SMatthew Dillon is &= ~AHCI_PREG_IS_DHRS; 21741980eff3SMatthew Dillon } 21751980eff3SMatthew Dillon 21761980eff3SMatthew Dillon /* 21771980eff3SMatthew Dillon * Device notification to us. 21781980eff3SMatthew Dillon * 2179*cec07d75SMatthew Dillon * NOTE! On some parts notification bits can get set without 2180*cec07d75SMatthew Dillon * generating an interrupt. It is unclear whether this is 2181*cec07d75SMatthew Dillon * a bug in the PM (sending a DTOH device setbits with 'N' set 2182*cec07d75SMatthew Dillon * and 'I' not set), or a bug in the host controller. 2183*cec07d75SMatthew Dillon * 2184*cec07d75SMatthew Dillon * It only seems to occur under load. 21851980eff3SMatthew Dillon */ 2186*cec07d75SMatthew Dillon if (/*(is & AHCI_PREG_IS_SDBS) &&*/ (sc->sc_cap & AHCI_REG_CAP_SSNTF)) { 21871980eff3SMatthew Dillon u_int32_t data; 2188*cec07d75SMatthew Dillon const char *xstr; 21891980eff3SMatthew Dillon 21901980eff3SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SNTF); 2191*cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_SDBS) { 21921980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS); 2193*cec07d75SMatthew Dillon is &= ~AHCI_PREG_IS_SDBS; 2194*cec07d75SMatthew Dillon xstr = " (no SDBS!)"; 2195*cec07d75SMatthew Dillon } else { 2196*cec07d75SMatthew Dillon xstr = ""; 2197*cec07d75SMatthew Dillon } 2198*cec07d75SMatthew Dillon if (data) { 2199*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS); 2200*cec07d75SMatthew Dillon 2201*cec07d75SMatthew Dillon kprintf("%s: NOTIFY %08x%s\n", 2202*cec07d75SMatthew Dillon PORTNAME(ap), data, xstr); 2203*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_N); 22043209f581SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, data); 22053209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 22061980eff3SMatthew Dillon } 22071980eff3SMatthew Dillon } 22083209f581SMatthew Dillon 22093209f581SMatthew Dillon /* 22103209f581SMatthew Dillon * Spurious IFS errors can occur while we are doing a reset 22113209f581SMatthew Dillon * sequence through a PM. Try to recover if we are being asked 22123209f581SMatthew Dillon * to ignore IFS errors during these periods. 22133209f581SMatthew Dillon */ 22143209f581SMatthew Dillon if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) { 22151980eff3SMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 22163209f581SMatthew Dillon if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) { 22171980eff3SMatthew Dillon kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n", 22181980eff3SMatthew Dillon PORTNAME(ap), 22191980eff3SMatthew Dillon is, AHCI_PFMT_IS, 22201980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 22213209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_IGNORED; 22223209f581SMatthew Dillon } 22233209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_OCCURED; 22241980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 22251980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 22261980eff3SMatthew Dillon is &= ~AHCI_PREG_IS_IFS; 22271980eff3SMatthew Dillon ahci_port_stop(ap, 0); 22281980eff3SMatthew Dillon ahci_port_start(ap); 22291980eff3SMatthew Dillon need = NEED_RESTART; 22301980eff3SMatthew Dillon } 2231258223a3SMatthew Dillon 2232258223a3SMatthew Dillon /* 2233258223a3SMatthew Dillon * Port change (hot-plug). 2234258223a3SMatthew Dillon * 2235258223a3SMatthew Dillon * A PCS interrupt will occur on hot-plug once communication is 2236258223a3SMatthew Dillon * established. 2237258223a3SMatthew Dillon * 2238258223a3SMatthew Dillon * A PRCS interrupt will occur on hot-unplug (and possibly also 2239258223a3SMatthew Dillon * on hot-plug). 2240258223a3SMatthew Dillon * 224122181ab7SMatthew Dillon * XXX We can then check the CPS (Cold Presence State) bit, if 224222181ab7SMatthew Dillon * supported, to determine if a device is plugged in or not and do 224322181ab7SMatthew Dillon * the right thing. 224422181ab7SMatthew Dillon * 224522181ab7SMatthew Dillon * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and 224622181ab7SMatthew Dillon * can also occur if an unsolicited COMINIT is received. 224722181ab7SMatthew Dillon * If this occurs command processing is automatically 224822181ab7SMatthew Dillon * stopped (CR goes inactive) and the port must be stopped 224922181ab7SMatthew Dillon * and restarted. 2250258223a3SMatthew Dillon */ 2251258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 2252*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2253*cec07d75SMatthew Dillon is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)); 2254*cec07d75SMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2255258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 22561980eff3SMatthew Dillon (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X)); 225722181ab7SMatthew Dillon ahci_port_stop(ap, 0); 2258258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 2259258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 22601980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 226122181ab7SMatthew Dillon need = NEED_HOTPLUG_INSERT; 226222181ab7SMatthew Dillon goto fatal; 2263258223a3SMatthew Dillon } 226422181ab7SMatthew Dillon need = NEED_RESTART; 2265258223a3SMatthew Dillon break; 2266258223a3SMatthew Dillon default: 22671980eff3SMatthew Dillon if (ap->ap_type != ATA_PORT_T_NONE) { 226822181ab7SMatthew Dillon need = NEED_HOTPLUG_REMOVE; 226922181ab7SMatthew Dillon goto fatal; 2270258223a3SMatthew Dillon } 227122181ab7SMatthew Dillon need = NEED_RESTART; 2272258223a3SMatthew Dillon break; 2273258223a3SMatthew Dillon } 2274258223a3SMatthew Dillon } 2275258223a3SMatthew Dillon 227622181ab7SMatthew Dillon /* 227722181ab7SMatthew Dillon * Check for remaining errors - they are fatal. 227822181ab7SMatthew Dillon */ 2279258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 2280258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 2281*cec07d75SMatthew Dillon u_int32_t serr; 2282*cec07d75SMatthew Dillon 2283*cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2284*cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2285*cec07d75SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2286*cec07d75SMatthew Dillon AHCI_PREG_IS_UFS)); 2287*cec07d75SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2288*cec07d75SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2289*cec07d75SMatthew Dillon AHCI_PREG_IS_UFS); 2290*cec07d75SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 22911980eff3SMatthew Dillon kprintf("%s: unrecoverable errors (IS: %b, SERR: %b), " 22924444122dSMatthew Dillon "disabling port.\n", 22934444122dSMatthew Dillon PORTNAME(ap), 22944444122dSMatthew Dillon is, AHCI_PFMT_IS, 22951980eff3SMatthew Dillon serr, AHCI_PFMT_SERR 22964444122dSMatthew Dillon ); 2297258223a3SMatthew Dillon /* XXX try recovery first */ 2298258223a3SMatthew Dillon goto fatal; 2299258223a3SMatthew Dillon } 2300258223a3SMatthew Dillon 230122181ab7SMatthew Dillon /* 230222181ab7SMatthew Dillon * Fail all outstanding commands if we know the port won't recover. 23031980eff3SMatthew Dillon * 23041980eff3SMatthew Dillon * We may have a ccb_at if the failed command is known and was 23051980eff3SMatthew Dillon * being sent to a device over a port multiplier (PM). In this 23061980eff3SMatthew Dillon * case if the port itself has not completely failed we fail just 23071980eff3SMatthew Dillon * the commands related to that target. 230822181ab7SMatthew Dillon */ 2309258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2310258223a3SMatthew Dillon fatal: 2311258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 2312258223a3SMatthew Dillon failall: 2313258223a3SMatthew Dillon 2314cf5f3a81SMatthew Dillon /* Stopping the port clears CI/SACT */ 2315cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 2316258223a3SMatthew Dillon 23171980eff3SMatthew Dillon /* 23181980eff3SMatthew Dillon * Error all the active slots. If running across a PM 23191980eff3SMatthew Dillon * try to error out just the slots related to the target. 23201980eff3SMatthew Dillon */ 2321258223a3SMatthew Dillon ci_masked = ci_saved & *active; 2322258223a3SMatthew Dillon while (ci_masked) { 2323258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2324258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 23251980eff3SMatthew Dillon if (ccb_at == ccb->ccb_xa.at || 23261980eff3SMatthew Dillon ap->ap_state == AP_S_FATAL_ERROR) { 2327258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2328258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2329258223a3SMatthew Dillon } 23301980eff3SMatthew Dillon } 2331258223a3SMatthew Dillon 2332258223a3SMatthew Dillon /* Run completion for all active slots. */ 2333258223a3SMatthew Dillon ci_saved &= ~*active; 2334258223a3SMatthew Dillon 2335258223a3SMatthew Dillon /* 2336258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 2337258223a3SMatthew Dillon * 2338258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 2339258223a3SMatthew Dillon * a livelock. 2340258223a3SMatthew Dillon */ 2341258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 234222181ab7SMatthew Dillon if (need == NEED_RESTART) 234322181ab7SMatthew Dillon need = NEED_NOTHING; 2344258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2345258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2346258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2347258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 2348258223a3SMatthew Dillon } 2349258223a3SMatthew Dillon } 2350258223a3SMatthew Dillon 2351258223a3SMatthew Dillon /* 2352258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 2353258223a3SMatthew Dillon * changed to zero some time after we activated it. 2354258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 2355cf5f3a81SMatthew Dillon * 2356cf5f3a81SMatthew Dillon * Any active bits not saved are completed within the restrictions 2357cf5f3a81SMatthew Dillon * imposed by the caller. 2358258223a3SMatthew Dillon */ 23593209f581SMatthew Dillon ci_masked = ~ci_saved & *active; 2360258223a3SMatthew Dillon while (ci_masked) { 2361258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2362258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2363258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2364258223a3SMatthew Dillon 2365258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 2366258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 2367258223a3SMatthew Dillon " (error)" : ""); 2368258223a3SMatthew Dillon 2369258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2370258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2371258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2372258223a3SMatthew Dillon 2373258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2374258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2375258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2376258223a3SMatthew Dillon 2377258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2378258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2379258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 2380258223a3SMatthew Dillon 2381258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 23821980eff3SMatthew Dillon if (active == &ap->ap_active) { 23831980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 23841980eff3SMatthew Dillon --ap->ap_active_cnt; 23851980eff3SMatthew Dillon } 2386258223a3SMatthew Dillon ccb->ccb_done(ccb); 2387258223a3SMatthew Dillon } 2388258223a3SMatthew Dillon 238922181ab7SMatthew Dillon switch(need) { 239022181ab7SMatthew Dillon case NEED_RESTART: 239122181ab7SMatthew Dillon /* 239222181ab7SMatthew Dillon * A recoverable error occured and we can restart outstanding 239322181ab7SMatthew Dillon * commands on the port. 239422181ab7SMatthew Dillon */ 239517eab71eSMatthew Dillon ahci_port_start(ap); 2396258223a3SMatthew Dillon 2397258223a3SMatthew Dillon if (ci_saved) { 2398258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2399258223a3SMatthew Dillon tmp = ci_saved; 2400258223a3SMatthew Dillon while (tmp) { 2401258223a3SMatthew Dillon slot = ffs(tmp) - 1; 2402258223a3SMatthew Dillon tmp &= ~(1 << slot); 2403258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2404258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2405258223a3SMatthew Dillon KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) == 2406258223a3SMatthew Dillon (!!ap->ap_sactive)); 2407258223a3SMatthew Dillon } 2408258223a3SMatthew Dillon #endif 2409258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr " 2410258223a3SMatthew Dillon "re-enabling%s slots %08x\n", PORTNAME(ap), 2411258223a3SMatthew Dillon ap->ap_sactive ? " NCQ" : "", ci_saved); 2412258223a3SMatthew Dillon 2413258223a3SMatthew Dillon if (ap->ap_sactive) 2414258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 2415258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 2416258223a3SMatthew Dillon } 241722181ab7SMatthew Dillon break; 241822181ab7SMatthew Dillon case NEED_HOTPLUG_INSERT: 241922181ab7SMatthew Dillon /* 2420cf5f3a81SMatthew Dillon * A hot-plug insertion event has occured and all 2421cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 24221980eff3SMatthew Dillon * 24231980eff3SMatthew Dillon * Don't recurse if this occurs while we are 24241980eff3SMatthew Dillon * resetting the port. 242522181ab7SMatthew Dillon */ 24261980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 242722181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 242822181ab7SMatthew Dillon PORTNAME(ap)); 24293209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 24303209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 24311980eff3SMatthew Dillon } 243222181ab7SMatthew Dillon break; 243322181ab7SMatthew Dillon case NEED_HOTPLUG_REMOVE: 2434cf5f3a81SMatthew Dillon /* 2435cf5f3a81SMatthew Dillon * A hot-plug removal event has occured and all 2436cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 24371980eff3SMatthew Dillon * 24381980eff3SMatthew Dillon * Don't recurse if this occurs while we are 24391980eff3SMatthew Dillon * resetting the port. 2440cf5f3a81SMatthew Dillon */ 24411980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 244222181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 244322181ab7SMatthew Dillon PORTNAME(ap)); 2444cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 24453209f581SMatthew Dillon /* ap_probe set to failed */ 24463209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 24471980eff3SMatthew Dillon } 244822181ab7SMatthew Dillon break; 244922181ab7SMatthew Dillon default: 245022181ab7SMatthew Dillon break; 2451258223a3SMatthew Dillon } 2452258223a3SMatthew Dillon } 2453258223a3SMatthew Dillon 2454258223a3SMatthew Dillon struct ahci_ccb * 2455258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 2456258223a3SMatthew Dillon { 2457258223a3SMatthew Dillon struct ahci_ccb *ccb; 2458258223a3SMatthew Dillon 2459258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2460258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 2461258223a3SMatthew Dillon if (ccb != NULL) { 2462258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 2463258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 2464258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 24651980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 2466258223a3SMatthew Dillon } 2467258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2468258223a3SMatthew Dillon 2469258223a3SMatthew Dillon return (ccb); 2470258223a3SMatthew Dillon } 2471258223a3SMatthew Dillon 2472258223a3SMatthew Dillon void 2473258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 2474258223a3SMatthew Dillon { 2475258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2476258223a3SMatthew Dillon 2477258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2478258223a3SMatthew Dillon if (ccb->ccb_xa.state != ATA_S_COMPLETE && 2479258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_TIMEOUT && 2480258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_ERROR) { 2481258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, " 2482258223a3SMatthew Dillon "slot %d\n", 2483258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), ccb->ccb_xa.state, 2484258223a3SMatthew Dillon ccb->ccb_slot); 2485258223a3SMatthew Dillon } 2486258223a3SMatthew Dillon #endif 2487258223a3SMatthew Dillon 2488258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 2489258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2490258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 2491258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2492258223a3SMatthew Dillon } 2493258223a3SMatthew Dillon 2494258223a3SMatthew Dillon struct ahci_ccb * 2495258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 2496258223a3SMatthew Dillon { 2497258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 2498258223a3SMatthew Dillon u_int32_t sact; 2499258223a3SMatthew Dillon 2500258223a3SMatthew Dillon /* No commands may be active on the chip. */ 2501258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 2502258223a3SMatthew Dillon if (sact != 0) 2503258223a3SMatthew Dillon kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact); 2504258223a3SMatthew Dillon KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0); 2505258223a3SMatthew Dillon 2506258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2507258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy == 0); 2508258223a3SMatthew Dillon ap->ap_err_busy = 1; 2509258223a3SMatthew Dillon #endif 2510258223a3SMatthew Dillon /* Save outstanding command state. */ 2511258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 2512258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 2513258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 2514258223a3SMatthew Dillon 2515258223a3SMatthew Dillon /* 2516258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 2517258223a3SMatthew Dillon * run prematurely. 2518258223a3SMatthew Dillon */ 2519258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 2520258223a3SMatthew Dillon 2521258223a3SMatthew Dillon /* 2522258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 2523258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 2524258223a3SMatthew Dillon */ 2525258223a3SMatthew Dillon err_ccb = ahci_get_ccb(ap); 2526258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 2527258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 2528258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 2529258223a3SMatthew Dillon 2530258223a3SMatthew Dillon return err_ccb; 2531258223a3SMatthew Dillon } 2532258223a3SMatthew Dillon 2533258223a3SMatthew Dillon void 2534258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 2535258223a3SMatthew Dillon { 2536258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2537258223a3SMatthew Dillon u_int32_t sact; 25385f8c1efdSMatthew Dillon u_int32_t ci; 2539258223a3SMatthew Dillon 2540258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2541258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy); 2542258223a3SMatthew Dillon #endif 25435f8c1efdSMatthew Dillon /* 25445f8c1efdSMatthew Dillon * No commands may be active on the chip 25455f8c1efdSMatthew Dillon */ 2546258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 25475f8c1efdSMatthew Dillon if (sact) { 25485f8c1efdSMatthew Dillon panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n", 25495f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 2550258223a3SMatthew Dillon } 25515f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 25525f8c1efdSMatthew Dillon if (ci) { 2553cf5f3a81SMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0 " 2554cf5f3a81SMatthew Dillon "(act=%08x sact=%08x)\n", 2555cf5f3a81SMatthew Dillon ccb->ccb_slot, ci, 2556cf5f3a81SMatthew Dillon ap->ap_active, ap->ap_sactive); 25575f8c1efdSMatthew Dillon } 2558258223a3SMatthew Dillon 2559258223a3SMatthew Dillon /* Done with the CCB */ 2560258223a3SMatthew Dillon ahci_put_ccb(ccb); 2561258223a3SMatthew Dillon 2562258223a3SMatthew Dillon /* Restore outstanding command state */ 2563258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 2564258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 2565258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 2566258223a3SMatthew Dillon 2567258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2568258223a3SMatthew Dillon ap->ap_err_busy = 0; 2569258223a3SMatthew Dillon #endif 2570258223a3SMatthew Dillon } 2571258223a3SMatthew Dillon 25721980eff3SMatthew Dillon /* 25731980eff3SMatthew Dillon * Read log page to get NCQ error. 25741980eff3SMatthew Dillon * 25751980eff3SMatthew Dillon * NOTE: NCQ not currently supported on port multipliers. XXX 25761980eff3SMatthew Dillon */ 2577258223a3SMatthew Dillon int 2578258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp) 2579258223a3SMatthew Dillon { 2580258223a3SMatthew Dillon struct ahci_ccb *ccb; 2581258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 2582258223a3SMatthew Dillon u_int32_t cmd; 2583258223a3SMatthew Dillon struct ata_fis_h2d *fis; 2584258223a3SMatthew Dillon int rc = EIO; 2585258223a3SMatthew Dillon 2586258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap)); 2587258223a3SMatthew Dillon 2588258223a3SMatthew Dillon /* Save command register state. */ 2589258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 2590258223a3SMatthew Dillon 2591258223a3SMatthew Dillon /* Port should have been idled already. Start it. */ 2592258223a3SMatthew Dillon KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0); 259317eab71eSMatthew Dillon ahci_port_start(ap); 2594258223a3SMatthew Dillon 2595258223a3SMatthew Dillon /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */ 2596258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 2597258223a3SMatthew Dillon ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL; 2598258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 2599258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 2600258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 2601258223a3SMatthew Dillon bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table)); 2602258223a3SMatthew Dillon 2603258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 2604258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 2605258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 2606258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 2607258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 2608258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 2609258223a3SMatthew Dillon fis->sector_count_exp = 0; 2610258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 2611258223a3SMatthew Dillon fis->lba_mid_exp = 0; 2612258223a3SMatthew Dillon fis->device = 0; 2613258223a3SMatthew Dillon 2614258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 2615258223a3SMatthew Dillon 2616258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 2617258223a3SMatthew Dillon rc = ENOMEM; /* XXX caller must abort all commands */ 2618258223a3SMatthew Dillon goto err; 2619258223a3SMatthew Dillon } 2620258223a3SMatthew Dillon 2621258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 26223209f581SMatthew Dillon if (ahci_poll(ccb, 1000, NULL) != 0) 2623258223a3SMatthew Dillon goto err; 2624258223a3SMatthew Dillon 2625258223a3SMatthew Dillon rc = 0; 2626258223a3SMatthew Dillon err: 2627258223a3SMatthew Dillon /* Abort our command, if it failed, by stopping command DMA. */ 26281980eff3SMatthew Dillon if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) { 2629258223a3SMatthew Dillon kprintf("%s: log page read failed, slot %d was still active.\n", 2630258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 2631258223a3SMatthew Dillon ahci_port_stop(ap, 0); 2632258223a3SMatthew Dillon } 2633258223a3SMatthew Dillon 2634258223a3SMatthew Dillon /* Done with the error CCB now. */ 2635258223a3SMatthew Dillon ahci_unload_prdt(ccb); 2636258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 2637258223a3SMatthew Dillon 2638258223a3SMatthew Dillon /* Extract failed register set and tags from the scratch space. */ 2639258223a3SMatthew Dillon if (rc == 0) { 2640258223a3SMatthew Dillon struct ata_log_page_10h *log; 2641258223a3SMatthew Dillon int err_slot; 2642258223a3SMatthew Dillon 2643258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 2644258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 2645258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 2646258223a3SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ " 2647258223a3SMatthew Dillon "error?\n", 2648258223a3SMatthew Dillon PORTNAME(ap)); 2649258223a3SMatthew Dillon rc = ESRCH; 2650258223a3SMatthew Dillon } else { 2651258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 2652258223a3SMatthew Dillon *err_slotp = err_slot = log->err_regs.type & 2653258223a3SMatthew Dillon ATA_LOG_10H_TYPE_TAG_MASK; 2654258223a3SMatthew Dillon 2655258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2656258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, &log->err_regs, 2657258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 2658258223a3SMatthew Dillon ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 2659258223a3SMatthew Dillon ccb->ccb_xa.rfis.flags = 0; 2660258223a3SMatthew Dillon } 2661258223a3SMatthew Dillon } 2662258223a3SMatthew Dillon 2663258223a3SMatthew Dillon /* Restore saved CMD register state */ 2664258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 2665258223a3SMatthew Dillon 2666258223a3SMatthew Dillon return (rc); 2667258223a3SMatthew Dillon } 2668258223a3SMatthew Dillon 2669258223a3SMatthew Dillon /* 2670258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 2671258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 2672258223a3SMatthew Dillon * single physical base address. 2673258223a3SMatthew Dillon */ 2674258223a3SMatthew Dillon struct ahci_dmamem * 2675258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 2676258223a3SMatthew Dillon { 2677258223a3SMatthew Dillon struct ahci_dmamem *adm; 2678258223a3SMatthew Dillon int error; 2679258223a3SMatthew Dillon 2680258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 2681258223a3SMatthew Dillon 2682258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 2683258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 2684258223a3SMatthew Dillon if (error == 0) { 2685258223a3SMatthew Dillon adm->adm_tag = tag; 2686258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 2687258223a3SMatthew Dillon adm->adm_kva, 2688258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 2689258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 2690258223a3SMatthew Dillon 0); 2691258223a3SMatthew Dillon } 2692258223a3SMatthew Dillon if (error) { 2693258223a3SMatthew Dillon if (adm->adm_map) { 2694258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 2695258223a3SMatthew Dillon adm->adm_map = NULL; 2696258223a3SMatthew Dillon adm->adm_tag = NULL; 2697258223a3SMatthew Dillon adm->adm_kva = NULL; 2698258223a3SMatthew Dillon } 2699258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2700258223a3SMatthew Dillon adm = NULL; 2701258223a3SMatthew Dillon } 2702258223a3SMatthew Dillon return (adm); 2703258223a3SMatthew Dillon } 2704258223a3SMatthew Dillon 2705258223a3SMatthew Dillon static 2706258223a3SMatthew Dillon void 2707258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 2708258223a3SMatthew Dillon { 2709258223a3SMatthew Dillon KKASSERT(error == 0); 2710258223a3SMatthew Dillon KKASSERT(nsegs == 1); 2711258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 2712258223a3SMatthew Dillon } 2713258223a3SMatthew Dillon 2714258223a3SMatthew Dillon 2715258223a3SMatthew Dillon void 2716258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 2717258223a3SMatthew Dillon { 2718258223a3SMatthew Dillon if (adm->adm_map) { 2719258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 2720258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 2721258223a3SMatthew Dillon adm->adm_map = NULL; 2722258223a3SMatthew Dillon adm->adm_tag = NULL; 2723258223a3SMatthew Dillon adm->adm_kva = NULL; 2724258223a3SMatthew Dillon } 2725258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2726258223a3SMatthew Dillon } 2727258223a3SMatthew Dillon 2728258223a3SMatthew Dillon u_int32_t 2729258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 2730258223a3SMatthew Dillon { 2731258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2732258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 2733258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 2734258223a3SMatthew Dillon } 2735258223a3SMatthew Dillon 2736258223a3SMatthew Dillon void 2737258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 2738258223a3SMatthew Dillon { 2739258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 2740258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2741258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 2742258223a3SMatthew Dillon } 2743258223a3SMatthew Dillon 2744258223a3SMatthew Dillon int 2745258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 2746258223a3SMatthew Dillon u_int32_t target) 2747258223a3SMatthew Dillon { 2748258223a3SMatthew Dillon int i; 2749258223a3SMatthew Dillon 2750258223a3SMatthew Dillon for (i = 0; i < 1000; i++) { 2751258223a3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 2752258223a3SMatthew Dillon return (0); 27533209f581SMatthew Dillon ahci_os_sleep(1); 2754258223a3SMatthew Dillon } 2755258223a3SMatthew Dillon 2756258223a3SMatthew Dillon return (1); 2757258223a3SMatthew Dillon } 2758258223a3SMatthew Dillon 2759258223a3SMatthew Dillon u_int32_t 2760258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 2761258223a3SMatthew Dillon { 2762258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 2763258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 2764258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 2765258223a3SMatthew Dillon } 2766258223a3SMatthew Dillon 2767258223a3SMatthew Dillon void 2768258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 2769258223a3SMatthew Dillon { 2770258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 2771258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 2772258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 2773258223a3SMatthew Dillon } 2774258223a3SMatthew Dillon 2775258223a3SMatthew Dillon int 2776cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 2777cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 2778258223a3SMatthew Dillon { 2779258223a3SMatthew Dillon int i; 2780258223a3SMatthew Dillon 2781cec85a37SMatthew Dillon for (i = 0; i < timeout; i++) { 2782258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 2783258223a3SMatthew Dillon return (0); 27843209f581SMatthew Dillon ahci_os_sleep(1); 2785258223a3SMatthew Dillon } 2786258223a3SMatthew Dillon 2787258223a3SMatthew Dillon return (1); 2788258223a3SMatthew Dillon } 2789258223a3SMatthew Dillon 27901980eff3SMatthew Dillon /* 27911980eff3SMatthew Dillon * Acquire an ata transfer. 27921980eff3SMatthew Dillon * 27931980eff3SMatthew Dillon * Pass a NULL at for direct-attached transfers, and a non-NULL at for 27941980eff3SMatthew Dillon * targets that go through the port multiplier. 27951980eff3SMatthew Dillon */ 2796258223a3SMatthew Dillon struct ata_xfer * 27971980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at) 2798258223a3SMatthew Dillon { 2799258223a3SMatthew Dillon struct ahci_ccb *ccb; 2800258223a3SMatthew Dillon 2801258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 2802258223a3SMatthew Dillon if (ccb == NULL) { 2803258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 2804258223a3SMatthew Dillon PORTNAME(ap)); 2805258223a3SMatthew Dillon return (NULL); 2806258223a3SMatthew Dillon } 2807258223a3SMatthew Dillon 2808258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 2809258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 2810258223a3SMatthew Dillon 28111980eff3SMatthew Dillon ccb->ccb_xa.at = at; 2812258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 2813258223a3SMatthew Dillon 2814258223a3SMatthew Dillon return (&ccb->ccb_xa); 2815258223a3SMatthew Dillon } 2816258223a3SMatthew Dillon 2817258223a3SMatthew Dillon void 2818258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 2819258223a3SMatthew Dillon { 2820258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 2821258223a3SMatthew Dillon 2822258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 2823258223a3SMatthew Dillon 2824258223a3SMatthew Dillon ahci_put_ccb(ccb); 2825258223a3SMatthew Dillon } 2826258223a3SMatthew Dillon 2827258223a3SMatthew Dillon int 2828258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 2829258223a3SMatthew Dillon { 2830258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 2831258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 2832258223a3SMatthew Dillon 2833258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 2834258223a3SMatthew Dillon 2835258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 2836258223a3SMatthew Dillon goto failcmd; 28371980eff3SMatthew Dillon #if 0 28381980eff3SMatthew Dillon kprintf("%s: started std command %b ccb %d ccb_at %p %d\n", 28391980eff3SMatthew Dillon ATANAME(ccb->ccb_port, ccb->ccb_xa.at), 28401980eff3SMatthew Dillon ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD, 28411980eff3SMatthew Dillon ccb->ccb_slot, 28421980eff3SMatthew Dillon ccb->ccb_xa.at, 28431980eff3SMatthew Dillon ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1); 28441980eff3SMatthew Dillon #endif 2845258223a3SMatthew Dillon 2846258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 2847258223a3SMatthew Dillon 2848258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 2849258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 28501980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 28511980eff3SMatthew Dillon cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target << 28521980eff3SMatthew Dillon AHCI_CMD_LIST_FLAG_PMP_SHIFT); 28531980eff3SMatthew Dillon } 2854258223a3SMatthew Dillon 2855258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 2856258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 2857258223a3SMatthew Dillon 2858258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 2859258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 2860258223a3SMatthew Dillon 2861258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 2862258223a3SMatthew Dillon goto failcmd; 2863258223a3SMatthew Dillon 2864258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 2865258223a3SMatthew Dillon 2866258223a3SMatthew Dillon if (xa->flags & ATA_F_POLL) { 2867258223a3SMatthew Dillon ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout); 2868258223a3SMatthew Dillon return (ATA_COMPLETE); 2869258223a3SMatthew Dillon } 2870258223a3SMatthew Dillon 2871258223a3SMatthew Dillon crit_enter(); 28723209f581SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_DESIRED; 2873258223a3SMatthew Dillon ahci_start(ccb); 2874258223a3SMatthew Dillon crit_exit(); 2875258223a3SMatthew Dillon return (ATA_QUEUED); 2876258223a3SMatthew Dillon 2877258223a3SMatthew Dillon failcmd: 2878258223a3SMatthew Dillon crit_enter(); 2879258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 2880258223a3SMatthew Dillon xa->complete(xa); 2881258223a3SMatthew Dillon crit_exit(); 2882258223a3SMatthew Dillon return (ATA_ERROR); 2883258223a3SMatthew Dillon } 2884258223a3SMatthew Dillon 2885258223a3SMatthew Dillon void 2886258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 2887258223a3SMatthew Dillon { 2888258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 2889258223a3SMatthew Dillon 2890258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 2891258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 2892258223a3SMatthew Dillon callout_stop(&ccb->ccb_timeout); 2893258223a3SMatthew Dillon } 28943209f581SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_DESIRED; 2895258223a3SMatthew Dillon 2896258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) 2897258223a3SMatthew Dillon ahci_issue_pending_commands(ccb->ccb_port, 2898258223a3SMatthew Dillon xa->flags & ATA_F_NCQ); 2899258223a3SMatthew Dillon 2900258223a3SMatthew Dillon ahci_unload_prdt(ccb); 2901258223a3SMatthew Dillon 2902258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP) 2903258223a3SMatthew Dillon xa->state = ATA_S_COMPLETE; 2904258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2905258223a3SMatthew Dillon else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT) 2906258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, " 2907258223a3SMatthew Dillon "slot %d\n", 2908258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot); 2909258223a3SMatthew Dillon #endif 2910258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 2911258223a3SMatthew Dillon xa->complete(xa); 2912258223a3SMatthew Dillon } 2913258223a3SMatthew Dillon 2914258223a3SMatthew Dillon static void 2915258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 2916258223a3SMatthew Dillon { 2917258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 2918258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2919258223a3SMatthew Dillon 2920258223a3SMatthew Dillon lwkt_serialize_enter(&ap->ap_sc->sc_serializer); 2921258223a3SMatthew Dillon ahci_ata_cmd_timeout(arg); 2922258223a3SMatthew Dillon lwkt_serialize_exit(&ap->ap_sc->sc_serializer); 2923258223a3SMatthew Dillon } 2924258223a3SMatthew Dillon 29251980eff3SMatthew Dillon void 2926258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg) 2927258223a3SMatthew Dillon { 2928258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 2929258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 2930258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2931258223a3SMatthew Dillon volatile u_int32_t *active; 2932258223a3SMatthew Dillon int ccb_was_started, ncq_cmd; 2933131be210SMatthew Dillon int status; 2934258223a3SMatthew Dillon 2935258223a3SMatthew Dillon crit_enter(); 2936cf5f3a81SMatthew Dillon kprintf("%s: CMD TIMEOUT cmd-reg 0x%b\n" 2937cf5f3a81SMatthew Dillon "\tsactive=%08x active=%08x\n" 2938258223a3SMatthew Dillon "\t sact=%08x ci=%08x\n", 29391980eff3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 2940258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 2941cf5f3a81SMatthew Dillon ap->ap_sactive, ap->ap_active, 2942258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 2943258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI)); 2944258223a3SMatthew Dillon 29459e145b23SMatthew Dillon /* 29469e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 29473209f581SMatthew Dillon * If we got here at least one of these flags should be set. 29489e145b23SMatthew Dillon */ 29493209f581SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED | 29503209f581SMatthew Dillon ATA_F_TIMEOUT_RUNNING)); 2951258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 2952258223a3SMatthew Dillon ncq_cmd = (xa->flags & ATA_F_NCQ); 2953258223a3SMatthew Dillon active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active; 2954258223a3SMatthew Dillon 2955258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 2956258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out " 2957258223a3SMatthew Dillon "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot); 2958258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 2959258223a3SMatthew Dillon ccb_was_started = 0; 2960258223a3SMatthew Dillon } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 2961258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already " 2962258223a3SMatthew Dillon "handled%s\n", PORTNAME(ap), ccb->ccb_slot, 2963258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot)) ? 2964258223a3SMatthew Dillon " but slot is still active?" : "."); 2965258223a3SMatthew Dillon goto ret; 2966258223a3SMatthew Dillon } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) & 2967258223a3SMatthew Dillon (1 << ccb->ccb_slot)) == 0 && 2968258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot))) { 2969258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but " 2970258223a3SMatthew Dillon "IRQ handler didn't detect it. Why?\n", PORTNAME(ap), 2971258223a3SMatthew Dillon ccb->ccb_slot); 2972258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 29731980eff3SMatthew Dillon if (ncq_cmd == 0) { 29741980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 29751980eff3SMatthew Dillon --ap->ap_active_cnt; 29761980eff3SMatthew Dillon } 2977258223a3SMatthew Dillon ccb->ccb_done(ccb); 2978258223a3SMatthew Dillon goto ret; 2979258223a3SMatthew Dillon } else { 2980258223a3SMatthew Dillon ccb_was_started = 1; 2981258223a3SMatthew Dillon } 2982258223a3SMatthew Dillon 2983258223a3SMatthew Dillon /* Complete the slot with a timeout error. */ 2984258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 2985258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 29861980eff3SMatthew Dillon if (ncq_cmd == 0) { 29871980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 29881980eff3SMatthew Dillon --ap->ap_active_cnt; 29891980eff3SMatthew Dillon } 2990258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap)); 2991258223a3SMatthew Dillon ccb->ccb_done(ccb); /* This won't issue pending commands or run the 2992258223a3SMatthew Dillon atascsi completion. */ 2993258223a3SMatthew Dillon 2994258223a3SMatthew Dillon /* Reset port to abort running command. */ 2995258223a3SMatthew Dillon if (ccb_was_started) { 2996258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command " 2997258223a3SMatthew Dillon "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" 2998258223a3SMatthew Dillon : "", ccb->ccb_slot, *active); 29991980eff3SMatthew Dillon /* XXX */ 30003209f581SMatthew Dillon if (ccb->ccb_xa.at && ap->ap_type == ATA_PORT_T_PM) { 30011980eff3SMatthew Dillon /* XXX how do we unbrick a PM target? */ 3002131be210SMatthew Dillon kprintf("%s: PM target bricked and timed-out, " 3003131be210SMatthew Dillon "disabling PM target but trying to " 3004131be210SMatthew Dillon "leave the port intact\n", 3005131be210SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at)); 3006131be210SMatthew Dillon ccb->ccb_xa.at->at_probe = ATA_PROBE_FAILED; 30073209f581SMatthew Dillon ahci_port_intr(ap); 3008131be210SMatthew Dillon ahci_port_stop(ap, 0); 3009131be210SMatthew Dillon ahci_port_clo(ap); 3010131be210SMatthew Dillon ahci_port_start(ap); 3011131be210SMatthew Dillon status = 0; 30121980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) { 3013cf5f3a81SMatthew Dillon /* 3014cf5f3a81SMatthew Dillon * If the softreset failed place the port in a 3015cf5f3a81SMatthew Dillon * failed state and use ahci_port_intr() to cancel 3016cf5f3a81SMatthew Dillon * any remaining commands. 3017cf5f3a81SMatthew Dillon */ 301817eab71eSMatthew Dillon kprintf("%s: Unable to reset during timeout, port " 301917eab71eSMatthew Dillon "bricked on us\n", 3020258223a3SMatthew Dillon PORTNAME(ap)); 3021258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 30223209f581SMatthew Dillon ahci_port_intr(ap); 3023131be210SMatthew Dillon status = 1; 3024cf5f3a81SMatthew Dillon } else { 3025131be210SMatthew Dillon status = 0; 3026131be210SMatthew Dillon } 3027131be210SMatthew Dillon if (status == 0) { 3028cf5f3a81SMatthew Dillon /* 3029cf5f3a81SMatthew Dillon * Restart any other commands that were aborted 3030cf5f3a81SMatthew Dillon * by the reset. 3031cf5f3a81SMatthew Dillon */ 3032258223a3SMatthew Dillon if (*active) { 3033258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots " 3034258223a3SMatthew Dillon "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "", 3035258223a3SMatthew Dillon *active); 3036258223a3SMatthew Dillon if (ncq_cmd) 3037258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, *active); 3038258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, *active); 3039258223a3SMatthew Dillon } 3040258223a3SMatthew Dillon } 3041cf5f3a81SMatthew Dillon } 3042258223a3SMatthew Dillon 3043258223a3SMatthew Dillon /* Issue any pending commands now. */ 3044258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap)); 3045258223a3SMatthew Dillon if (ccb_was_started) 3046258223a3SMatthew Dillon ahci_issue_pending_commands(ap, ncq_cmd); 3047258223a3SMatthew Dillon else if (ap->ap_active == 0) 3048258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 3049258223a3SMatthew Dillon 3050258223a3SMatthew Dillon /* Complete the timed out ata_xfer I/O (may generate new I/O). */ 3051258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap)); 3052258223a3SMatthew Dillon xa->complete(xa); 3053258223a3SMatthew Dillon 3054258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap)); 3055258223a3SMatthew Dillon ret: 3056258223a3SMatthew Dillon crit_exit(); 3057258223a3SMatthew Dillon } 3058258223a3SMatthew Dillon 3059258223a3SMatthew Dillon void 3060258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 3061258223a3SMatthew Dillon { 3062258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 3063258223a3SMatthew Dillon } 3064