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; 1861980eff3SMatthew Dillon at->at_probe = ATA_PROBE_NEED_HARD_RESET; 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; 2011980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_HARD_RESET; 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 /* 4481980eff3SMatthew Dillon * Flush interupts 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 /* 474fd8bd957SMatthew Dillon * De-initialize and detach a port. 475fd8bd957SMatthew Dillon */ 476258223a3SMatthew Dillon void 477258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 478258223a3SMatthew Dillon { 479258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 480258223a3SMatthew Dillon struct ahci_ccb *ccb; 481258223a3SMatthew Dillon 48217eab71eSMatthew Dillon /* 48317eab71eSMatthew Dillon * Ensure port is disabled and its interrupts are all flushed. 48417eab71eSMatthew Dillon */ 485258223a3SMatthew Dillon if (ap->ap_sc) { 48617eab71eSMatthew Dillon ahci_port_stop(ap, 1); 487258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 488258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 489258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 490258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 491258223a3SMatthew Dillon } 492258223a3SMatthew Dillon 493258223a3SMatthew Dillon if (ap->ap_ccbs) { 494258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 495258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 496258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 497258223a3SMatthew Dillon ccb->ccb_dmamap); 498258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 499258223a3SMatthew Dillon } 500258223a3SMatthew Dillon } 501258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 502258223a3SMatthew Dillon ap->ap_ccbs = NULL; 503258223a3SMatthew Dillon } 504258223a3SMatthew Dillon 505258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 506258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 507258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 508258223a3SMatthew Dillon } 509258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 510258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 511258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 512258223a3SMatthew Dillon } 513258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 514258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 515258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 516258223a3SMatthew Dillon } 5171980eff3SMatthew Dillon if (ap->ap_ata) { 5181980eff3SMatthew Dillon kfree(ap->ap_ata, M_DEVBUF); 5191980eff3SMatthew Dillon ap->ap_ata = NULL; 5201980eff3SMatthew Dillon } 521258223a3SMatthew Dillon 522258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 523258223a3SMatthew Dillon 524258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 525258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 526258223a3SMatthew Dillon } 527258223a3SMatthew Dillon 528fd8bd957SMatthew Dillon /* 529fd8bd957SMatthew Dillon * Start high-level command processing on the port 530fd8bd957SMatthew Dillon */ 531258223a3SMatthew Dillon int 53217eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap) 533258223a3SMatthew Dillon { 534*8bf6a3ffSMatthew Dillon u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd; 535258223a3SMatthew Dillon 53617eab71eSMatthew Dillon /* 53717eab71eSMatthew Dillon * FRE must be turned on before ST. Wait for FR to go active 53817eab71eSMatthew Dillon * before turning on ST. The spec doesn't seem to think this 53917eab71eSMatthew Dillon * is necessary but waiting here avoids an on-off race in the 54017eab71eSMatthew Dillon * ahci_port_stop() code. 54117eab71eSMatthew Dillon */ 5421980eff3SMatthew Dillon olds = ahci_pread(ap, AHCI_PREG_SERR); 5431980eff3SMatthew Dillon oldis= ahci_pread(ap, AHCI_PREG_IS); 544*8bf6a3ffSMatthew Dillon oldtfd = ahci_pread(ap, AHCI_PREG_TFD); 5451980eff3SMatthew Dillon oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 54617eab71eSMatthew Dillon if ((r & AHCI_PREG_CMD_FRE) == 0) { 547258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 54817eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 54917eab71eSMatthew Dillon } 55017eab71eSMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) { 55117eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 55217eab71eSMatthew Dillon kprintf("%s: Cannot start FIS reception\n", 55317eab71eSMatthew Dillon PORTNAME(ap)); 55417eab71eSMatthew Dillon return (2); 55517eab71eSMatthew Dillon } 55617eab71eSMatthew Dillon } 55717eab71eSMatthew Dillon 55817eab71eSMatthew Dillon /* 55917eab71eSMatthew Dillon * Turn on ST, wait for CR to come up. 56017eab71eSMatthew Dillon */ 561258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 562258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 56317eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 564*8bf6a3ffSMatthew Dillon s = ahci_pread(ap, AHCI_PREG_SERR); 565*8bf6a3ffSMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 566*8bf6a3ffSMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 5671980eff3SMatthew Dillon kprintf("%s: Cannot start command DMA\n" 5681980eff3SMatthew Dillon "OCMD=%b OSERR=%b\n" 5691980eff3SMatthew Dillon "NCMP=%b NSERR=%b\n" 570*8bf6a3ffSMatthew Dillon "OLDIS=%b\nNEWIS=%b\n" 571*8bf6a3ffSMatthew Dillon "OLDTFD=%b\nNEWTFD=%b\n", 5721980eff3SMatthew Dillon PORTNAME(ap), 5731980eff3SMatthew Dillon oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR, 5741980eff3SMatthew Dillon r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR, 575*8bf6a3ffSMatthew Dillon oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS, 576*8bf6a3ffSMatthew Dillon oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS); 57717eab71eSMatthew Dillon return (1); 57817eab71eSMatthew Dillon } 579258223a3SMatthew Dillon 580258223a3SMatthew Dillon #ifdef AHCI_COALESCE 58117eab71eSMatthew Dillon /* 58217eab71eSMatthew Dillon * (Re-)enable coalescing on the port. 58317eab71eSMatthew Dillon */ 584258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 585258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 586258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 587258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 588258223a3SMatthew Dillon } 589258223a3SMatthew Dillon #endif 590258223a3SMatthew Dillon 591258223a3SMatthew Dillon return (0); 592258223a3SMatthew Dillon } 593258223a3SMatthew Dillon 594fd8bd957SMatthew Dillon /* 595fd8bd957SMatthew Dillon * Stop high-level command processing on a port 596fd8bd957SMatthew Dillon */ 597258223a3SMatthew Dillon int 598258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 599258223a3SMatthew Dillon { 600258223a3SMatthew Dillon u_int32_t r; 601258223a3SMatthew Dillon 602258223a3SMatthew Dillon #ifdef AHCI_COALESCE 60317eab71eSMatthew Dillon /* 60417eab71eSMatthew Dillon * Disable coalescing on the port while it is stopped. 60517eab71eSMatthew Dillon */ 606258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 607258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 608258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 609258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 610258223a3SMatthew Dillon } 611258223a3SMatthew Dillon #endif 612258223a3SMatthew Dillon 61317eab71eSMatthew Dillon /* 61417eab71eSMatthew Dillon * Turn off ST, then wait for CR to go off. 61517eab71eSMatthew Dillon */ 616258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 617258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 618258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 619258223a3SMatthew Dillon 62017eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 62117eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (ST)\n", 62217eab71eSMatthew Dillon PORTNAME(ap)); 623258223a3SMatthew Dillon return (1); 62417eab71eSMatthew Dillon } 625258223a3SMatthew Dillon 6261980eff3SMatthew Dillon #if 0 62717eab71eSMatthew Dillon /* 62817eab71eSMatthew Dillon * Turn off FRE, then wait for FR to go off. FRE cannot 62917eab71eSMatthew Dillon * be turned off until CR transitions to 0. 63017eab71eSMatthew Dillon */ 6311980eff3SMatthew Dillon if ((r & AHCI_PREG_CMD_FR) == 0) { 6321980eff3SMatthew Dillon kprintf("%s: FR stopped, clear FRE for next start\n", 6331980eff3SMatthew Dillon PORTNAME(ap)); 6341980eff3SMatthew Dillon stop_fis_rx = 2; 6351980eff3SMatthew Dillon } 6361980eff3SMatthew Dillon #endif 63717eab71eSMatthew Dillon if (stop_fis_rx) { 63817eab71eSMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 63917eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 64017eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 64117eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (FRE)\n", 64217eab71eSMatthew Dillon PORTNAME(ap)); 643258223a3SMatthew Dillon return (2); 64417eab71eSMatthew Dillon } 64517eab71eSMatthew Dillon } 646258223a3SMatthew Dillon 647258223a3SMatthew Dillon return (0); 648258223a3SMatthew Dillon } 649258223a3SMatthew Dillon 650fd8bd957SMatthew Dillon /* 651fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 652fd8bd957SMatthew Dillon */ 653258223a3SMatthew Dillon int 654258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 655258223a3SMatthew Dillon { 656258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 657258223a3SMatthew Dillon u_int32_t cmd; 658258223a3SMatthew Dillon 659258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 660258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 661258223a3SMatthew Dillon return (1); 662258223a3SMatthew Dillon 663258223a3SMatthew Dillon /* Issue CLO */ 664258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 665258223a3SMatthew Dillon #ifdef DIAGNOSTIC 666258223a3SMatthew Dillon if (cmd & AHCI_PREG_CMD_ST) { 667258223a3SMatthew Dillon kprintf("%s: CLO requested while port running\n", 668258223a3SMatthew Dillon PORTNAME(ap)); 669258223a3SMatthew Dillon } 670258223a3SMatthew Dillon #endif 671258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 672258223a3SMatthew Dillon 673258223a3SMatthew Dillon /* Wait for completion */ 674258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 675258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 676258223a3SMatthew Dillon return (1); 677258223a3SMatthew Dillon } 678258223a3SMatthew Dillon 679258223a3SMatthew Dillon return (0); 680258223a3SMatthew Dillon } 681258223a3SMatthew Dillon 682fd8bd957SMatthew Dillon /* 6831980eff3SMatthew Dillon * Reset a port. 68417eab71eSMatthew Dillon * 6851980eff3SMatthew Dillon * If hard is 0 perform a softreset of the port. 68617eab71eSMatthew Dillon * If hard is 1 perform a hard reset of the port. 6871980eff3SMatthew Dillon * If hard is 2 perform a hard reset of the port and cycle the phy. 6881980eff3SMatthew Dillon * 6891980eff3SMatthew Dillon * If at is non-NULL an indirect port via a port-multiplier is being 6901980eff3SMatthew Dillon * reset, otherwise a direct port is being reset. 6911980eff3SMatthew Dillon * 6921980eff3SMatthew Dillon * NOTE: Indirect ports can only be soft-reset. 69317eab71eSMatthew Dillon */ 69417eab71eSMatthew Dillon int 6951980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard) 69617eab71eSMatthew Dillon { 69717eab71eSMatthew Dillon int rc; 69817eab71eSMatthew Dillon 69917eab71eSMatthew Dillon if (hard) { 7001980eff3SMatthew Dillon if (at) 7011980eff3SMatthew Dillon rc = ahci_pm_hardreset(ap, at->at_target, hard); 7021980eff3SMatthew Dillon else 7031980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 70417eab71eSMatthew Dillon } else { 7051980eff3SMatthew Dillon if (at) 7061980eff3SMatthew Dillon rc = ahci_pm_softreset(ap, at->at_target); 7071980eff3SMatthew Dillon else 70817eab71eSMatthew Dillon rc = ahci_port_softreset(ap); 7091980eff3SMatthew Dillon #if 0 7101980eff3SMatthew Dillon if (rc && at == NULL) 7111980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 7121980eff3SMatthew Dillon #endif 71317eab71eSMatthew Dillon } 71417eab71eSMatthew Dillon return(rc); 71517eab71eSMatthew Dillon } 71617eab71eSMatthew Dillon 71717eab71eSMatthew Dillon /* 718fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 719fd8bd957SMatthew Dillon * 7201980eff3SMatthew Dillon * (at) will be NULL when soft-resetting a directly-attached device, and 7211980eff3SMatthew Dillon * non-NULL when soft-resetting a device through a port multiplier. 7221980eff3SMatthew Dillon * 723fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 7241980eff3SMatthew Dillon * a reset to the connected device using device commands. 725fd8bd957SMatthew Dillon */ 726258223a3SMatthew Dillon int 727258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 728258223a3SMatthew Dillon { 729258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 730258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 731258223a3SMatthew Dillon u_int8_t *fis; 7321980eff3SMatthew Dillon int rc, count; 733258223a3SMatthew Dillon u_int32_t cmd; 734258223a3SMatthew Dillon 7351980eff3SMatthew Dillon rc = EIO; 7361980eff3SMatthew Dillon count = 10; /* device reset delay x 100ms */ 7371980eff3SMatthew Dillon 7381980eff3SMatthew Dillon kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap), 7391980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD); 7401980eff3SMatthew Dillon 741258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 742258223a3SMatthew Dillon 743258223a3SMatthew Dillon crit_enter(); 7441980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 7451980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 746258223a3SMatthew Dillon 7471980eff3SMatthew Dillon /* 7481980eff3SMatthew Dillon * Remember port state in cmd (main to restore start/stop) 7491980eff3SMatthew Dillon * 7501980eff3SMatthew Dillon * Idle port. 7511980eff3SMatthew Dillon */ 7521980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 753258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 754258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 755258223a3SMatthew Dillon PORTNAME(ap)); 756258223a3SMatthew Dillon goto err; 757258223a3SMatthew Dillon } 758cf5f3a81SMatthew Dillon 759cf5f3a81SMatthew Dillon /* 7601980eff3SMatthew Dillon * Request CLO if device appears hung. 761cf5f3a81SMatthew Dillon */ 762258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 763258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 764258223a3SMatthew Dillon ahci_port_clo(ap); 765258223a3SMatthew Dillon } 766258223a3SMatthew Dillon 7671980eff3SMatthew Dillon /* 7681980eff3SMatthew Dillon * This is an attempt to clear errors so a new signature will 7691980eff3SMatthew Dillon * be latched. It isn't working properly. XXX 7701980eff3SMatthew Dillon */ 771cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 7721980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 773258223a3SMatthew Dillon 774258223a3SMatthew Dillon /* Restart port */ 77517eab71eSMatthew Dillon if (ahci_port_start(ap)) { 776258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 777258223a3SMatthew Dillon PORTNAME(ap)); 778258223a3SMatthew Dillon goto err; 779258223a3SMatthew Dillon } 780258223a3SMatthew Dillon 781258223a3SMatthew Dillon /* Check whether CLO worked */ 782258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 783258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 784258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 785258223a3SMatthew Dillon PORTNAME(ap), 786258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 787258223a3SMatthew Dillon ? "failed" : "unsupported"); 788258223a3SMatthew Dillon rc = EBUSY; 789258223a3SMatthew Dillon goto err; 790258223a3SMatthew Dillon } 791258223a3SMatthew Dillon 792cec85a37SMatthew Dillon /* 793cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 794cec85a37SMatthew Dillon * 795cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 796cec85a37SMatthew Dillon * everything. 797cec85a37SMatthew Dillon */ 798258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 7991980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 800258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 801258223a3SMatthew Dillon 802258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 803cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 8041980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 8051980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 806258223a3SMatthew Dillon 807258223a3SMatthew Dillon cmd_slot->prdtl = 0; 808258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 809258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 810258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 811258223a3SMatthew Dillon 812258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 8135f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 8141980eff3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0 || 8151980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 8165f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 817258223a3SMatthew Dillon goto err; 818cec85a37SMatthew Dillon } 819258223a3SMatthew Dillon 820cec85a37SMatthew Dillon /* 8211980eff3SMatthew Dillon * The device may muff the PHY up. 8221980eff3SMatthew Dillon */ 8231980eff3SMatthew Dillon DELAY(10000); /* XXX 3000 */ 8241980eff3SMatthew Dillon 8251980eff3SMatthew Dillon /* 826cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 827cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 828cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 829cec85a37SMatthew Dillon * from the AHCI document. 830cec85a37SMatthew Dillon * 831b089d0bfSMatthew Dillon * Give the device 3ms before sending the second FIS. 832cec85a37SMatthew Dillon * 833cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 834cec85a37SMatthew Dillon * everything. 835cec85a37SMatthew Dillon */ 836cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 8371980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 8381980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 839258223a3SMatthew Dillon 840258223a3SMatthew Dillon cmd_slot->prdtl = 0; 841258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 842258223a3SMatthew Dillon 843258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 8445f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 8451980eff3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0 || 8461980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 8475f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 848258223a3SMatthew Dillon goto err; 849cec85a37SMatthew Dillon } 850258223a3SMatthew Dillon 8511980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 8521980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 853258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 854258223a3SMatthew Dillon PORTNAME(ap), 855258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 856258223a3SMatthew Dillon rc = EBUSY; 857258223a3SMatthew Dillon goto err; 858258223a3SMatthew Dillon } 8591980eff3SMatthew Dillon DELAY(10000); 860258223a3SMatthew Dillon 861fd8bd957SMatthew Dillon /* 862fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 863fd8bd957SMatthew Dillon * normal portreset we assign the port type. 864fd8bd957SMatthew Dillon * 865fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 866fd8bd957SMatthew Dillon * processing code then report if the device signature changed 867fd8bd957SMatthew Dillon * unexpectedly. 868fd8bd957SMatthew Dillon */ 8691980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 8701980eff3SMatthew Dillon ap->ap_type = ahci_port_signature_detect(ap, NULL); 871fd8bd957SMatthew Dillon } else { 8721980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) { 8731980eff3SMatthew Dillon kprintf("%s: device signature unexpectedly " 8741980eff3SMatthew Dillon "changed\n", PORTNAME(ap)); 8751980eff3SMatthew Dillon rc = EBUSY; /* XXX */ 876fd8bd957SMatthew Dillon } 877fd8bd957SMatthew Dillon } 878258223a3SMatthew Dillon rc = 0; 8791980eff3SMatthew Dillon 880b089d0bfSMatthew Dillon DELAY(3000); 881258223a3SMatthew Dillon err: 882258223a3SMatthew Dillon if (ccb != NULL) { 8831980eff3SMatthew Dillon /* 8841980eff3SMatthew Dillon * Abort our command, if it failed, by stopping command DMA. 8851980eff3SMatthew Dillon */ 886cf5f3a81SMatthew Dillon if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) { 887258223a3SMatthew Dillon kprintf("%s: stopping the port, softreset slot " 888258223a3SMatthew Dillon "%d was still active.\n", 889258223a3SMatthew Dillon PORTNAME(ap), 890258223a3SMatthew Dillon ccb->ccb_slot); 891258223a3SMatthew Dillon ahci_port_stop(ap, 0); 892258223a3SMatthew Dillon } 893258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 8941980eff3SMatthew Dillon fis[15] = 0; 895258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 8961980eff3SMatthew Dillon 8971980eff3SMatthew Dillon /* 8981980eff3SMatthew Dillon * If the target is busy use CLO to clear the busy 8991980eff3SMatthew Dillon * condition. The BSY should be cleared on the next 9001980eff3SMatthew Dillon * start. 9011980eff3SMatthew Dillon */ 9021980eff3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 9031980eff3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 9041980eff3SMatthew Dillon ahci_port_clo(ap); 9051980eff3SMatthew Dillon } 906258223a3SMatthew Dillon } 907258223a3SMatthew Dillon 908cf5f3a81SMatthew Dillon /* 909cf5f3a81SMatthew Dillon * If we failed to softreset make the port quiescent, otherwise 910cf5f3a81SMatthew Dillon * make sure the port's start/stop state matches what it was on 911cf5f3a81SMatthew Dillon * entry. 9121980eff3SMatthew Dillon * 9131980eff3SMatthew Dillon * Don't kill the port if the softreset is on a port multiplier 9141980eff3SMatthew Dillon * target, that would kill all the targets! 915cf5f3a81SMatthew Dillon */ 916cf5f3a81SMatthew Dillon if (rc) { 917cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 918cf5f3a81SMatthew Dillon } else if (cmd & AHCI_PREG_CMD_ST) { 9191980eff3SMatthew Dillon kprintf("%s: STARTING PORT\n", PORTNAME(ap)); 920cf5f3a81SMatthew Dillon ahci_port_start(ap); 921cf5f3a81SMatthew Dillon } else { 9221980eff3SMatthew Dillon kprintf("%s: STOPPING PORT\n", PORTNAME(ap)); 923cf5f3a81SMatthew Dillon ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE)); 924cf5f3a81SMatthew Dillon } 9251980eff3SMatthew Dillon if (rc) 9261980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 9271980eff3SMatthew Dillon else 9281980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 929258223a3SMatthew Dillon 930258223a3SMatthew Dillon crit_exit(); 931258223a3SMatthew Dillon 9321980eff3SMatthew Dillon kprintf("%s: END SOFTRESET\n", PORTNAME(ap)); 9331980eff3SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 9341980eff3SMatthew Dillon 935258223a3SMatthew Dillon return (rc); 936258223a3SMatthew Dillon } 937258223a3SMatthew Dillon 938fd8bd957SMatthew Dillon /* 939fd8bd957SMatthew Dillon * AHCI port reset, Section 10.4.2 940fd8bd957SMatthew Dillon * 941fd8bd957SMatthew Dillon * This function does a hard reset of the port. Note that the device 942fd8bd957SMatthew Dillon * connected to the port could still end-up hung. 943fd8bd957SMatthew Dillon */ 944258223a3SMatthew Dillon int 9451980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard) 946258223a3SMatthew Dillon { 947258223a3SMatthew Dillon u_int32_t cmd, r; 948258223a3SMatthew Dillon int rc; 9491980eff3SMatthew Dillon int loop; 9501980eff3SMatthew Dillon int type; 951258223a3SMatthew Dillon 952258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap)); 953258223a3SMatthew Dillon 9541980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 955cf5f3a81SMatthew Dillon 956cf5f3a81SMatthew Dillon /* 9571980eff3SMatthew Dillon * Idle the port, 9581980eff3SMatthew Dillon */ 9591980eff3SMatthew Dillon ahci_port_stop(ap, 0); 9601980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 9611980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 9621980eff3SMatthew Dillon rc = 0; 9631980eff3SMatthew Dillon 9641980eff3SMatthew Dillon /* 9651980eff3SMatthew Dillon * The port may have been quiescent with its SUD bit cleared, so 9661980eff3SMatthew Dillon * set the SUD (spin up device). 967cf5f3a81SMatthew Dillon */ 968cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 969cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 970cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 971258223a3SMatthew Dillon 9721980eff3SMatthew Dillon /* 9731980eff3SMatthew Dillon * Perform device detection. Cycle the PHY off, wait 10ms. 9741980eff3SMatthew Dillon * This simulates the SATA cable being physically unplugged. 9751980eff3SMatthew Dillon */ 9761980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 977258223a3SMatthew Dillon 9781980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED; 9791980eff3SMatthew Dillon if (hard == 2) 9801980eff3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_DISABLE; 9811980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 9821980eff3SMatthew Dillon DELAY(10000); 9831980eff3SMatthew Dillon 9841980eff3SMatthew Dillon /* 9851980eff3SMatthew Dillon * Start transmitting COMRESET. COMRESET must be sent for at 9861980eff3SMatthew Dillon * least 1ms. 9871980eff3SMatthew Dillon */ 9881980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 989258223a3SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 990258223a3SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 991258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 992258223a3SMatthew Dillon } else { 993258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 994258223a3SMatthew Dillon } 995258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 9961980eff3SMatthew Dillon DELAY(1000); 997cf5f3a81SMatthew Dillon 998cf5f3a81SMatthew Dillon /* 999cf5f3a81SMatthew Dillon * Only SERR_DIAG_X needs to be cleared for TFD updates, but 1000cf5f3a81SMatthew Dillon * since we are hard-resetting the port we might as well clear 1001cf5f3a81SMatthew Dillon * the whole enchillada 1002cf5f3a81SMatthew Dillon */ 1003cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1004cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1005258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 1006258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 1007258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1008258223a3SMatthew Dillon 10091980eff3SMatthew Dillon /* 10101980eff3SMatthew Dillon * Try to determine if there is a device on the port. 10111980eff3SMatthew Dillon * 10121980eff3SMatthew Dillon * Give the device 3/10 second to at least be detected. 10131980eff3SMatthew Dillon * If we fail clear PRCS (phy detect) since we may cycled 10141980eff3SMatthew Dillon * the phy and probably caused another PRCS interrupt. 10151980eff3SMatthew Dillon */ 10161980eff3SMatthew Dillon for (loop = 30; loop; --loop) { 10171980eff3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 10181980eff3SMatthew Dillon if (r & AHCI_PREG_SSTS_DET) 10191980eff3SMatthew Dillon break; 10201980eff3SMatthew Dillon DELAY(10000); 10211980eff3SMatthew Dillon } 10221980eff3SMatthew Dillon if (loop == 0) { 10231980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 10241980eff3SMatthew Dillon kprintf("%s: Port appears to be unplugged\n", 10251980eff3SMatthew Dillon PORTNAME(ap)); 1026258223a3SMatthew Dillon rc = ENODEV; 1027258223a3SMatthew Dillon } 1028258223a3SMatthew Dillon 1029cec85a37SMatthew Dillon /* 10301980eff3SMatthew Dillon * There is something on the port. Give the device 3 seconds 10311980eff3SMatthew Dillon * to fully negotiate. 10321980eff3SMatthew Dillon */ 10331980eff3SMatthew Dillon if (rc == 0 && 10341980eff3SMatthew Dillon ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS, 10351980eff3SMatthew Dillon AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) { 10361980eff3SMatthew Dillon kprintf("%s: Device may be powered down\n", 10371980eff3SMatthew Dillon PORTNAME(ap)); 10381980eff3SMatthew Dillon rc = ENODEV; 10391980eff3SMatthew Dillon } 10401980eff3SMatthew Dillon 10411980eff3SMatthew Dillon /* 10421980eff3SMatthew Dillon * Wait for the device to become ready. 1043cec85a37SMatthew Dillon * 1044b089d0bfSMatthew Dillon * This can take more then a second, give it 3 seconds. If we 1045b089d0bfSMatthew Dillon * succeed give the device another 3ms after that. 10461980eff3SMatthew Dillon * 10471980eff3SMatthew Dillon * NOTE: Port Multipliers can do two things here. First they can 10481980eff3SMatthew Dillon * return device-ready if a device is on target 0 and also 10491980eff3SMatthew Dillon * return the signature for that device. If there is no 10501980eff3SMatthew Dillon * device on target 0 then BSY/DRQ is never cleared and 10511980eff3SMatthew Dillon * it never comes ready. 1052cec85a37SMatthew Dillon */ 10531980eff3SMatthew Dillon if (rc == 0 && 10541980eff3SMatthew Dillon ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD, 10551980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 10561980eff3SMatthew Dillon /* 10571980eff3SMatthew Dillon * The device is bricked or its a port multiplier and will 10581980eff3SMatthew Dillon * not unbusy until we do the pmprobe CLO softreset sequence. 10591980eff3SMatthew Dillon */ 10601980eff3SMatthew Dillon rc = ahci_port_pmprobe(ap); 10611980eff3SMatthew Dillon if (rc) { 1062258223a3SMatthew Dillon kprintf("%s: Device will not come ready 0x%b\n", 1063258223a3SMatthew Dillon PORTNAME(ap), 10641980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), 10651980eff3SMatthew Dillon AHCI_PFMT_TFD_STS); 10661980eff3SMatthew Dillon } else { 10671980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 10681980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 10691980eff3SMatthew Dillon kprintf("%s: Port Multiplier detected\n", 10701980eff3SMatthew Dillon PORTNAME(ap)); 1071258223a3SMatthew Dillon } 10721980eff3SMatthew Dillon } else if (rc == 0) { 10731980eff3SMatthew Dillon /* 10741980eff3SMatthew Dillon * We generally will not get a port multiplier signature in 10751980eff3SMatthew Dillon * this case even if this is a port multiplier, because of 10761980eff3SMatthew Dillon * Intel's stupidity. We almost certainly got target 0 10771980eff3SMatthew Dillon * behind the PM, if there is a PM. 10781980eff3SMatthew Dillon * 10791980eff3SMatthew Dillon * Save the signature and probe for a PM. If we do not 10801980eff3SMatthew Dillon * find a PM then use the saved signature and return 10811980eff3SMatthew Dillon * success. 10821980eff3SMatthew Dillon */ 10831980eff3SMatthew Dillon type = ahci_port_signature_detect(ap, NULL); 10841980eff3SMatthew Dillon rc = ahci_port_pmprobe(ap); 10851980eff3SMatthew Dillon if (rc) { 10861980eff3SMatthew Dillon ap->ap_type = type; 10871980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 1088258223a3SMatthew Dillon rc = 0; 10891980eff3SMatthew Dillon } else { 10901980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 10911980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 10921980eff3SMatthew Dillon kprintf("%s: Port Multiplier detected\n", 10931980eff3SMatthew Dillon PORTNAME(ap)); 10941980eff3SMatthew Dillon } 10951980eff3SMatthew Dillon } 1096258223a3SMatthew Dillon 1097cf5f3a81SMatthew Dillon /* 10981980eff3SMatthew Dillon * hard-stop the port if we failed. This will set ap_probe 10991980eff3SMatthew Dillon * to FAILED. 1100cf5f3a81SMatthew Dillon */ 1101cf5f3a81SMatthew Dillon if (rc) 1102cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 11031980eff3SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 1104258223a3SMatthew Dillon return (rc); 1105258223a3SMatthew Dillon } 1106258223a3SMatthew Dillon 1107fd8bd957SMatthew Dillon /* 11081980eff3SMatthew Dillon * AHCI port multiplier probe. This routine is run by the hardreset code 11091980eff3SMatthew Dillon * if it gets past the device detect, whether or not BSY is found to be 11101980eff3SMatthew Dillon * stuck. 11111980eff3SMatthew Dillon * 11121980eff3SMatthew Dillon * We MUST use CLO to properly probe whether the port multiplier exists 11131980eff3SMatthew Dillon * or not. 11141980eff3SMatthew Dillon * 11151980eff3SMatthew Dillon * Return 0 on success, non-zero on failure. 11161980eff3SMatthew Dillon */ 11171980eff3SMatthew Dillon int 11181980eff3SMatthew Dillon ahci_port_pmprobe(struct ahci_port *ap) 11191980eff3SMatthew Dillon { 11201980eff3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 11211980eff3SMatthew Dillon struct ahci_ccb *ccb = NULL; 11221980eff3SMatthew Dillon u_int8_t *fis = NULL; 11231980eff3SMatthew Dillon int rc = EIO; 11241980eff3SMatthew Dillon u_int32_t cmd; 11251980eff3SMatthew Dillon int count; 11261980eff3SMatthew Dillon 11271980eff3SMatthew Dillon /* 11281980eff3SMatthew Dillon * If we don't support port multipliers don't try to detect one. 11291980eff3SMatthew Dillon */ 11301980eff3SMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0) 11311980eff3SMatthew Dillon return (ENODEV); 11321980eff3SMatthew Dillon 11331980eff3SMatthew Dillon count = 2; 11341980eff3SMatthew Dillon #if 0 11351980eff3SMatthew Dillon kprintf("%s: START PMPROBE\n", PORTNAME(ap)); 11361980eff3SMatthew Dillon #endif 11371980eff3SMatthew Dillon retry: 11381980eff3SMatthew Dillon /* 11391980eff3SMatthew Dillon * This code is only called from hardreset, which does not 11401980eff3SMatthew Dillon * high level command processing. The port should be stopped. 11411980eff3SMatthew Dillon * 11421980eff3SMatthew Dillon * Set PMA mode while the port is stopped. 11431980eff3SMatthew Dillon * 11441980eff3SMatthew Dillon * NOTE: On retry the port might be running, stopped, or failed. 11451980eff3SMatthew Dillon */ 11461980eff3SMatthew Dillon ahci_port_stop(ap, 0); 11471980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 11481980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 11491980eff3SMatthew Dillon cmd |= AHCI_PREG_CMD_PMA; 11501980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 11511980eff3SMatthew Dillon 11521980eff3SMatthew Dillon /* 11531980eff3SMatthew Dillon * Flush any errors and request CLO unconditionally, then start 11541980eff3SMatthew Dillon * the port. 11551980eff3SMatthew Dillon */ 11561980eff3SMatthew Dillon ahci_flush_tfd(ap); 11571980eff3SMatthew Dillon ahci_port_clo(ap); 11581980eff3SMatthew Dillon if (ahci_port_start(ap)) { 11591980eff3SMatthew Dillon kprintf("%s: PMPROBE failed to start port, cannot softreset\n", 11601980eff3SMatthew Dillon PORTNAME(ap)); 11611980eff3SMatthew Dillon goto err; 11621980eff3SMatthew Dillon } 11631980eff3SMatthew Dillon 11641980eff3SMatthew Dillon /* 11651980eff3SMatthew Dillon * Check whether CLO worked 11661980eff3SMatthew Dillon */ 11671980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 11681980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 11691980eff3SMatthew Dillon kprintf("%s: PMPROBE CLO %s, need port reset\n", 11701980eff3SMatthew Dillon PORTNAME(ap), 11711980eff3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 11721980eff3SMatthew Dillon ? "failed" : "unsupported"); 11731980eff3SMatthew Dillon rc = EBUSY; 11741980eff3SMatthew Dillon goto err; 11751980eff3SMatthew Dillon } 11761980eff3SMatthew Dillon 11771980eff3SMatthew Dillon /* 11781980eff3SMatthew Dillon * Prep the first H2D command with SRST feature & clear busy/reset 11791980eff3SMatthew Dillon * flags. 11801980eff3SMatthew Dillon */ 11811980eff3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 11821980eff3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 11831980eff3SMatthew Dillon 11841980eff3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 11851980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 11861980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 11871980eff3SMatthew Dillon fis[1] = 0x0F; /* Target 15 */ 11881980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT; 11891980eff3SMatthew Dillon 11901980eff3SMatthew Dillon cmd_slot->prdtl = 0; 11911980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 11921980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 11931980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 11941980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 11951980eff3SMatthew Dillon 11961980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 11971980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 11981980eff3SMatthew Dillon 11991980eff3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0 || 12001980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 12011980eff3SMatthew Dillon kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap)); 12021980eff3SMatthew Dillon if (--count) { 12031980eff3SMatthew Dillon fis[15] = 0; 12041980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 12051980eff3SMatthew Dillon goto retry; 12061980eff3SMatthew Dillon } 12071980eff3SMatthew Dillon goto err; 12081980eff3SMatthew Dillon } 12091980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 12101980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 12111980eff3SMatthew Dillon kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap)); 12121980eff3SMatthew Dillon } 12131980eff3SMatthew Dillon 12141980eff3SMatthew Dillon /* 12151980eff3SMatthew Dillon * The device may have muffed up the PHY when it reset. 12161980eff3SMatthew Dillon */ 12171980eff3SMatthew Dillon DELAY(10000); 12181980eff3SMatthew Dillon ahci_flush_tfd(ap); 12191980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 12201980eff3SMatthew Dillon /* ahci_pm_phy_status(ap, 15, &cmd); */ 12211980eff3SMatthew Dillon 12221980eff3SMatthew Dillon /* 12231980eff3SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 12241980eff3SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 12251980eff3SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 12261980eff3SMatthew Dillon * from the AHCI document. 12271980eff3SMatthew Dillon * 12281980eff3SMatthew Dillon * Give the device 3ms before sending the second FIS. 12291980eff3SMatthew Dillon * 12301980eff3SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 12311980eff3SMatthew Dillon * everything. 12321980eff3SMatthew Dillon */ 12331980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 12341980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 12351980eff3SMatthew Dillon fis[1] = 0x0F; 12361980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 12371980eff3SMatthew Dillon 12381980eff3SMatthew Dillon cmd_slot->prdtl = 0; 12391980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 12401980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 12411980eff3SMatthew Dillon 12421980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 12431980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 12441980eff3SMatthew Dillon 12451980eff3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0 || 12461980eff3SMatthew Dillon ccb->ccb_xa.state != ATA_S_COMPLETE) { 12471980eff3SMatthew Dillon kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap)); 12481980eff3SMatthew Dillon if (--count) { 12491980eff3SMatthew Dillon fis[15] = 0; 12501980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 12511980eff3SMatthew Dillon goto retry; 12521980eff3SMatthew Dillon } 12531980eff3SMatthew Dillon goto err; 12541980eff3SMatthew Dillon } 12551980eff3SMatthew Dillon 12561980eff3SMatthew Dillon /* 12571980eff3SMatthew Dillon * What? We succeeded? Yup, but for some reason the signature 12581980eff3SMatthew Dillon * is still latched from the original detect (that saw target 0 12591980eff3SMatthew Dillon * behind the PM), and I don't know how to clear the condition 12601980eff3SMatthew Dillon * other then by retrying the whole reset sequence. 12611980eff3SMatthew Dillon */ 12621980eff3SMatthew Dillon if (--count) { 12631980eff3SMatthew Dillon fis[15] = 0; 12641980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 12651980eff3SMatthew Dillon goto retry; 12661980eff3SMatthew Dillon } 12671980eff3SMatthew Dillon 12681980eff3SMatthew Dillon /* 12691980eff3SMatthew Dillon * Get the signature. The caller sets the ap fields. 12701980eff3SMatthew Dillon */ 12711980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) { 12721980eff3SMatthew Dillon ap->ap_ata[15].at_probe = ATA_PROBE_GOOD; 12731980eff3SMatthew Dillon rc = 0; 12741980eff3SMatthew Dillon } else { 12751980eff3SMatthew Dillon rc = EBUSY; 12761980eff3SMatthew Dillon } 12771980eff3SMatthew Dillon 12781980eff3SMatthew Dillon /* 12791980eff3SMatthew Dillon * Fall through / clean up the CCB and perform error processing. 12801980eff3SMatthew Dillon */ 12811980eff3SMatthew Dillon err: 12821980eff3SMatthew Dillon if (ccb != NULL) { 12831980eff3SMatthew Dillon /* 12841980eff3SMatthew Dillon * Abort our command, if it failed, by stopping command DMA. 12851980eff3SMatthew Dillon */ 12861980eff3SMatthew Dillon #if 0 12871980eff3SMatthew Dillon kprintf("rc=%d active=%08x sactive=%08x slot=%d\n", 12881980eff3SMatthew Dillon rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot); 12891980eff3SMatthew Dillon #endif 12901980eff3SMatthew Dillon if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) { 12911980eff3SMatthew Dillon kprintf("%s: PMP stopping the port, softreset slot " 12921980eff3SMatthew Dillon "%d was still active.\n", 12931980eff3SMatthew Dillon PORTNAME(ap), 12941980eff3SMatthew Dillon ccb->ccb_slot); 12951980eff3SMatthew Dillon ahci_port_stop(ap, 0); 12961980eff3SMatthew Dillon } 12971980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 12981980eff3SMatthew Dillon fis[15] = 0; 12991980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 13001980eff3SMatthew Dillon } 13011980eff3SMatthew Dillon 13021980eff3SMatthew Dillon /* 13031980eff3SMatthew Dillon * If we failed turn off PMA, otherwise identify the port multiplier. 13041980eff3SMatthew Dillon * CAM will iterate the devices. 13051980eff3SMatthew Dillon */ 13061980eff3SMatthew Dillon if (rc) { 13071980eff3SMatthew Dillon ahci_port_stop(ap, 0); 13081980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 13091980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 13101980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 13111980eff3SMatthew Dillon } else { 13121980eff3SMatthew Dillon ahci_pm_identify(ap); 13131980eff3SMatthew Dillon #if 0 13141980eff3SMatthew Dillon ahci_pm_hardreset(ap, 0); 13151980eff3SMatthew Dillon ahci_pm_hardreset(ap, 1); 13161980eff3SMatthew Dillon ahci_pm_hardreset(ap, 2); 13171980eff3SMatthew Dillon ahci_pm_hardreset(ap, 3); 13181980eff3SMatthew Dillon ahci_pm_hardreset(ap, 4); 13191980eff3SMatthew Dillon #endif 13201980eff3SMatthew Dillon } 13211980eff3SMatthew Dillon ahci_port_stop(ap, 0); 13221980eff3SMatthew Dillon 13231980eff3SMatthew Dillon #if 0 13241980eff3SMatthew Dillon kprintf("%s: END PMPROBE\n", PORTNAME(ap)); 13251980eff3SMatthew Dillon #endif 13261980eff3SMatthew Dillon 13271980eff3SMatthew Dillon return(rc); 13281980eff3SMatthew Dillon } 13291980eff3SMatthew Dillon 13301980eff3SMatthew Dillon 13311980eff3SMatthew Dillon /* 1332cf5f3a81SMatthew Dillon * Hard-stop on hot-swap device removal. See 10.10.1 1333cf5f3a81SMatthew Dillon * 1334cf5f3a81SMatthew Dillon * Place the port in a mode that will allow it to detect hot-swap insertions. 1335cf5f3a81SMatthew Dillon * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't 1336cf5f3a81SMatthew Dillon * seem to do the job. 1337cf5f3a81SMatthew Dillon */ 1338cf5f3a81SMatthew Dillon void 1339cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap) 1340cf5f3a81SMatthew Dillon { 13411980eff3SMatthew Dillon struct ata_port *at; 1342cf5f3a81SMatthew Dillon u_int32_t r; 1343cf5f3a81SMatthew Dillon u_int32_t cmd; 13441980eff3SMatthew Dillon int i; 1345cf5f3a81SMatthew Dillon 1346cf5f3a81SMatthew Dillon /* 1347cf5f3a81SMatthew Dillon * Stop the port. We can't modify things like SUD if the port 1348cf5f3a81SMatthew Dillon * is running. 1349cf5f3a81SMatthew Dillon */ 1350cf5f3a81SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 13511980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 13521980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1353cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 1354cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 1355cf5f3a81SMatthew Dillon 1356cf5f3a81SMatthew Dillon /* 13571980eff3SMatthew Dillon * Clean up AT sub-ports on SATA port. 13581980eff3SMatthew Dillon */ 13591980eff3SMatthew Dillon for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) { 13601980eff3SMatthew Dillon at = &ap->ap_ata[i]; 13611980eff3SMatthew Dillon at->at_type = ATA_PORT_T_NONE; 13621980eff3SMatthew Dillon at->at_probe = ATA_PORT_T_NONE; 13631980eff3SMatthew Dillon } 13641980eff3SMatthew Dillon 13651980eff3SMatthew Dillon /* 13661980eff3SMatthew Dillon * Turn off port-multiplier control bit 13671980eff3SMatthew Dillon */ 13681980eff3SMatthew Dillon if (cmd & AHCI_PREG_CMD_PMA) { 13691980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 13701980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 13711980eff3SMatthew Dillon } 13721980eff3SMatthew Dillon 13731980eff3SMatthew Dillon /* 1374cf5f3a81SMatthew Dillon * Make sure FRE is active. There isn't anything we can do if it 1375cf5f3a81SMatthew Dillon * fails so just ignore errors. 1376cf5f3a81SMatthew Dillon */ 1377cf5f3a81SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 1378cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE; 1379cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1380cf5f3a81SMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) 1381cf5f3a81SMatthew Dillon ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR); 1382cf5f3a81SMatthew Dillon } 1383cf5f3a81SMatthew Dillon 1384cf5f3a81SMatthew Dillon /* 1385cf5f3a81SMatthew Dillon * 10.10.3 DET must be set to 0 before setting SUD to 0. 1386cf5f3a81SMatthew Dillon * 10.10.1 place us in the Listen state. 1387cf5f3a81SMatthew Dillon * 1388cf5f3a81SMatthew Dillon * Deactivating SUD only applies if the controller supports SUD. 1389cf5f3a81SMatthew Dillon */ 1390cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 1391cf5f3a81SMatthew Dillon DELAY(1000); 1392cf5f3a81SMatthew Dillon if (cmd & AHCI_PREG_CMD_SUD) { 1393cf5f3a81SMatthew Dillon cmd &= ~AHCI_PREG_CMD_SUD; 1394cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1395cf5f3a81SMatthew Dillon } 1396cf5f3a81SMatthew Dillon DELAY(1000); 1397cf5f3a81SMatthew Dillon 1398cf5f3a81SMatthew Dillon /* 1399cf5f3a81SMatthew Dillon * Transition su to the spin-up state. HVA shall send COMRESET and 1400cf5f3a81SMatthew Dillon * begin initialization sequence (whatever that means). 1401cf5f3a81SMatthew Dillon * 1402cf5f3a81SMatthew Dillon * This only applies if the controller supports SUD. 1403cf5f3a81SMatthew Dillon */ 1404cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1405cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1406cf5f3a81SMatthew Dillon DELAY(1000); 1407cf5f3a81SMatthew Dillon 1408cf5f3a81SMatthew Dillon /* 1409cf5f3a81SMatthew Dillon * Transition us to the Reset state. Theoretically we send a 1410cf5f3a81SMatthew Dillon * continuous stream of COMRESETs in this state. 1411cf5f3a81SMatthew Dillon */ 1412cf5f3a81SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1413cf5f3a81SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1414cf5f3a81SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1415cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1416cf5f3a81SMatthew Dillon } else { 1417cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1418cf5f3a81SMatthew Dillon } 1419cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1420cf5f3a81SMatthew Dillon DELAY(1000); 1421cf5f3a81SMatthew Dillon 1422cf5f3a81SMatthew Dillon /* 1423cf5f3a81SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 1424cf5f3a81SMatthew Dillon */ 1425cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1426cf5f3a81SMatthew Dillon 1427cf5f3a81SMatthew Dillon /* 1428cf5f3a81SMatthew Dillon * Leave us in COMRESET (both SUD and INIT active), the HBA should 1429cf5f3a81SMatthew Dillon * hopefully send us a DIAG_X-related interrupt if it receives 1430cf5f3a81SMatthew Dillon * a COMINIT, and if not that then at least a Phy transition 1431cf5f3a81SMatthew Dillon * interrupt. 1432cf5f3a81SMatthew Dillon * 1433cf5f3a81SMatthew Dillon * If we transition INIT from 1->0 to begin the initalization 1434cf5f3a81SMatthew Dillon * sequence it is unclear if that sequence will remain active 1435cf5f3a81SMatthew Dillon * until the next device insertion. 1436cf5f3a81SMatthew Dillon * 1437cf5f3a81SMatthew Dillon * If we go back to the listen state it is unclear if the 1438cf5f3a81SMatthew Dillon * device will actually send us a COMINIT, since we aren't 1439cf5f3a81SMatthew Dillon * sending any COMRESET's 1440cf5f3a81SMatthew Dillon */ 1441cf5f3a81SMatthew Dillon /* NOP */ 1442cf5f3a81SMatthew Dillon } 1443cf5f3a81SMatthew Dillon 1444cf5f3a81SMatthew Dillon /* 1445cf5f3a81SMatthew Dillon * Multiple events may have built up in the TFD. The spec is not very 1446cf5f3a81SMatthew Dillon * clear on this but it does seem to serialize events so clearing DIAG_X 1447cf5f3a81SMatthew Dillon * just once might not do the job during a reset sequence. 1448cf5f3a81SMatthew Dillon */ 1449cf5f3a81SMatthew Dillon void 1450cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap) 1451cf5f3a81SMatthew Dillon { 1452cf5f3a81SMatthew Dillon u_int32_t r; 1453cf5f3a81SMatthew Dillon 1454cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 14551980eff3SMatthew Dillon while (r & AHCI_PREG_SERR_DIAG_X) { 14561980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X); 1457cf5f3a81SMatthew Dillon DELAY(1000); 1458cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 1459cf5f3a81SMatthew Dillon } 1460cf5f3a81SMatthew Dillon } 1461cf5f3a81SMatthew Dillon 1462cf5f3a81SMatthew Dillon /* 1463fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 1464fd8bd957SMatthew Dillon * DISK. 1465fd8bd957SMatthew Dillon */ 1466fd8bd957SMatthew Dillon int 14671980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at) 1468fd8bd957SMatthew Dillon { 1469fd8bd957SMatthew Dillon u_int32_t sig; 1470fd8bd957SMatthew Dillon 1471fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 14721980eff3SMatthew Dillon kprintf("%s: sig %08x\n", ATANAME(ap, at), sig); 1473fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 1474fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 14751980eff3SMatthew Dillon } else if ((sig & 0xffff0000) == 14761980eff3SMatthew Dillon (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) { 14771980eff3SMatthew Dillon kprintf("found PM\n"); 14781980eff3SMatthew Dillon return(ATA_PORT_T_PM); 1479fd8bd957SMatthew Dillon } else { 1480fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 1481fd8bd957SMatthew Dillon } 1482fd8bd957SMatthew Dillon } 1483fd8bd957SMatthew Dillon 1484fd8bd957SMatthew Dillon /* 1485fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 1486fd8bd957SMatthew Dillon */ 1487258223a3SMatthew Dillon int 1488258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 1489258223a3SMatthew Dillon { 1490258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1491258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1492258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1493258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 1494258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1495258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 1496258223a3SMatthew Dillon int error; 1497258223a3SMatthew Dillon 1498258223a3SMatthew Dillon if (xa->datalen == 0) { 1499258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 1500258223a3SMatthew Dillon return (0); 1501258223a3SMatthew Dillon } 1502258223a3SMatthew Dillon 1503258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 1504258223a3SMatthew Dillon xa->data, xa->datalen, 1505258223a3SMatthew Dillon ahci_load_prdt_callback, 1506258223a3SMatthew Dillon &prdt, 1507258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 1508258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 1509258223a3SMatthew Dillon if (error != 0) { 1510258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 1511258223a3SMatthew Dillon return (1); 1512258223a3SMatthew Dillon } 1513258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 1514258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 1515258223a3SMatthew Dillon 1516258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 1517258223a3SMatthew Dillon 1518258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1519258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1520258223a3SMatthew Dillon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1521258223a3SMatthew Dillon 1522258223a3SMatthew Dillon return (0); 1523258223a3SMatthew Dillon 1524258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1525258223a3SMatthew Dillon diagerr: 1526258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1527258223a3SMatthew Dillon return (1); 1528258223a3SMatthew Dillon #endif 1529258223a3SMatthew Dillon } 1530258223a3SMatthew Dillon 1531258223a3SMatthew Dillon /* 1532258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 1533258223a3SMatthew Dillon * list is a temporary structure. 1534258223a3SMatthew Dillon */ 1535258223a3SMatthew Dillon static 1536258223a3SMatthew Dillon void 1537258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 1538258223a3SMatthew Dillon int error) 1539258223a3SMatthew Dillon { 1540258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 1541258223a3SMatthew Dillon u_int64_t addr; 1542258223a3SMatthew Dillon 1543258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 1544258223a3SMatthew Dillon 1545258223a3SMatthew Dillon while (nsegs) { 1546258223a3SMatthew Dillon addr = segs->ds_addr; 1547258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 1548258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 1549258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1550258223a3SMatthew Dillon KKASSERT((addr & 1) == 0); 1551258223a3SMatthew Dillon KKASSERT((segs->ds_len & 1) == 0); 1552258223a3SMatthew Dillon #endif 1553258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 1554258223a3SMatthew Dillon --nsegs; 1555258223a3SMatthew Dillon if (nsegs) 1556258223a3SMatthew Dillon ++prd; 1557258223a3SMatthew Dillon ++segs; 1558258223a3SMatthew Dillon } 1559258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 1560258223a3SMatthew Dillon } 1561258223a3SMatthew Dillon 1562258223a3SMatthew Dillon void 1563258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 1564258223a3SMatthew Dillon { 1565258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1566258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1567258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1568258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1569258223a3SMatthew Dillon 1570258223a3SMatthew Dillon if (xa->datalen != 0) { 1571258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1572258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1573258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1574258223a3SMatthew Dillon 1575258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1576258223a3SMatthew Dillon 1577258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) 1578258223a3SMatthew Dillon xa->resid = 0; 1579258223a3SMatthew Dillon else 1580258223a3SMatthew Dillon xa->resid = xa->datalen - 1581258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 1582258223a3SMatthew Dillon } 1583258223a3SMatthew Dillon } 1584258223a3SMatthew Dillon 15855f8c1efdSMatthew Dillon /* 15865f8c1efdSMatthew Dillon * Start a command and poll for completion. 15875f8c1efdSMatthew Dillon * 15885f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 15895f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 15905f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 15915f8c1efdSMatthew Dillon * 15925f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 1593cf5f3a81SMatthew Dillon * 1594cf5f3a81SMatthew Dillon * NOTE: If the port is in a failed state and stopped we do not try 1595cf5f3a81SMatthew Dillon * to activate the ccb. 15965f8c1efdSMatthew Dillon */ 1597258223a3SMatthew Dillon int 1598258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *)) 1599258223a3SMatthew Dillon { 1600258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 16015f8c1efdSMatthew Dillon u_int32_t slot_mask = 1 << ccb->ccb_slot; 1602258223a3SMatthew Dillon 1603cf5f3a81SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) { 1604cf5f3a81SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1605cf5f3a81SMatthew Dillon return(1); 1606cf5f3a81SMatthew Dillon } 1607258223a3SMatthew Dillon crit_enter(); 1608258223a3SMatthew Dillon ahci_start(ccb); 16091980eff3SMatthew Dillon 1610258223a3SMatthew Dillon do { 16115f8c1efdSMatthew Dillon if (ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS) & slot_mask) { 1612258223a3SMatthew Dillon crit_exit(); 1613258223a3SMatthew Dillon return (0); 1614258223a3SMatthew Dillon } 16155f8c1efdSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP && 16165f8c1efdSMatthew Dillon ccb->ccb_xa.state != ATA_S_PENDING) { 16175f8c1efdSMatthew Dillon break; 16185f8c1efdSMatthew Dillon } 1619258223a3SMatthew Dillon DELAY(1000000 / hz); 1620258223a3SMatthew Dillon } while (--timeout > 0); 1621258223a3SMatthew Dillon 16225f8c1efdSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP && 16235f8c1efdSMatthew Dillon ccb->ccb_xa.state != ATA_S_PENDING) { 16245f8c1efdSMatthew Dillon kprintf("%s: Warning poll completed unexpectedly for slot %d\n", 16255f8c1efdSMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 16265f8c1efdSMatthew Dillon crit_exit(); 16275f8c1efdSMatthew Dillon return (0); 16285f8c1efdSMatthew Dillon } 16295f8c1efdSMatthew Dillon 16305f8c1efdSMatthew Dillon kprintf("%s: Poll timed-out for slot %d state %d\n", 16311980eff3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, ccb->ccb_xa.state); 16325f8c1efdSMatthew Dillon 16335f8c1efdSMatthew Dillon if (timeout_fn != NULL) { 1634258223a3SMatthew Dillon timeout_fn(ccb); 16355f8c1efdSMatthew Dillon } else { 16365f8c1efdSMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) 16375f8c1efdSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 16385f8c1efdSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 16395f8c1efdSMatthew Dillon } 1640258223a3SMatthew Dillon crit_exit(); 1641258223a3SMatthew Dillon 1642258223a3SMatthew Dillon return (1); 1643258223a3SMatthew Dillon } 1644258223a3SMatthew Dillon 1645258223a3SMatthew Dillon void 1646258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 1647258223a3SMatthew Dillon { 1648258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1649258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1650258223a3SMatthew Dillon 1651258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 1652258223a3SMatthew Dillon 1653258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 1654258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 1655258223a3SMatthew Dillon 1656258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 1657258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 1658258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 1659258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1660258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 1661258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 1662258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1663258223a3SMatthew Dillon 1664258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 1665258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 1666258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 1667258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 1668258223a3SMatthew Dillon 1669258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 16701980eff3SMatthew Dillon /* 16711980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 16721980eff3SMatthew Dillon * standard commands. 16731980eff3SMatthew Dillon */ 16741980eff3SMatthew Dillon if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) { 1675258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 16761980eff3SMatthew Dillon } else { 1677258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1678258223a3SMatthew Dillon ap->ap_sactive |= (1 << ccb->ccb_slot); 1679258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1680258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot); 1681258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1682258223a3SMatthew Dillon } 1683258223a3SMatthew Dillon } else { 16845f8c1efdSMatthew Dillon /* 16855f8c1efdSMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 16861980eff3SMatthew Dillon * command. Allow up to <limit> non-NCQ commands to be active. 16871980eff3SMatthew Dillon * 16881980eff3SMatthew Dillon * XXX If ap is a port multiplier only allow 1. At least the 16891980eff3SMatthew Dillon * NVidia-MCP77 part seems to barf if more then one 16901980eff3SMatthew Dillon * command is activated, even though it isn't NCQ. 16911980eff3SMatthew Dillon * 16921980eff3SMatthew Dillon * If I set up more then one I get phy errors and the 16931980eff3SMatthew Dillon * port fails. 16945f8c1efdSMatthew Dillon */ 16951980eff3SMatthew Dillon int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2; 16961980eff3SMatthew Dillon if (ap->ap_sactive || ap->ap_active_cnt >= limit) { 1697258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 16981980eff3SMatthew Dillon } else { 1699258223a3SMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 1700258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1701258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1702258223a3SMatthew Dillon ap->ap_active_cnt++; 1703258223a3SMatthew Dillon } 1704258223a3SMatthew Dillon } 1705258223a3SMatthew Dillon } 1706258223a3SMatthew Dillon 1707258223a3SMatthew Dillon void 1708258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap) 1709258223a3SMatthew Dillon { 1710258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1711258223a3SMatthew Dillon u_int32_t sact_change = 0; 1712258223a3SMatthew Dillon 1713258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1714258223a3SMatthew Dillon 1715258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1716258223a3SMatthew Dillon if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ)) 1717258223a3SMatthew Dillon return; 1718258223a3SMatthew Dillon 1719258223a3SMatthew Dillon /* Start all the NCQ commands at the head of the pending list. */ 1720258223a3SMatthew Dillon do { 1721258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 1722258223a3SMatthew Dillon sact_change |= 1 << nextccb->ccb_slot; 1723258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1724258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1725258223a3SMatthew Dillon } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)); 1726258223a3SMatthew Dillon 1727258223a3SMatthew Dillon ap->ap_sactive |= sact_change; 1728258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, sact_change); 1729258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, sact_change); 1730258223a3SMatthew Dillon 1731258223a3SMatthew Dillon return; 1732258223a3SMatthew Dillon } 1733258223a3SMatthew Dillon 1734258223a3SMatthew Dillon void 1735258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq) 1736258223a3SMatthew Dillon { 1737258223a3SMatthew Dillon struct ahci_ccb *nextccb; 1738258223a3SMatthew Dillon 1739258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 1740258223a3SMatthew Dillon if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) { 1741258223a3SMatthew Dillon KKASSERT(last_was_ncq == 0); /* otherwise it should have 1742258223a3SMatthew Dillon * been started already. */ 1743258223a3SMatthew Dillon 17441980eff3SMatthew Dillon /* 17451980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 17461980eff3SMatthew Dillon * standard commands. 17471980eff3SMatthew Dillon */ 1748258223a3SMatthew Dillon if (ap->ap_active == 0) 1749258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 1750258223a3SMatthew Dillon else 17511980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 1752258223a3SMatthew Dillon } else if (nextccb) { 17531980eff3SMatthew Dillon if (ap->ap_sactive || last_was_ncq) 1754258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1755258223a3SMatthew Dillon 17561980eff3SMatthew Dillon /* 17571980eff3SMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 17581980eff3SMatthew Dillon * command. Then keep up to 2 standard commands on-chip at 17591980eff3SMatthew Dillon * a time. 17601980eff3SMatthew Dillon */ 17611980eff3SMatthew Dillon if (ap->ap_sactive) 1762258223a3SMatthew Dillon return; 1763258223a3SMatthew Dillon 17641980eff3SMatthew Dillon while (ap->ap_active_cnt < 2 && 17651980eff3SMatthew Dillon nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) { 1766258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 1767258223a3SMatthew Dillon ap->ap_active |= 1 << nextccb->ccb_slot; 1768258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 1769258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot); 1770258223a3SMatthew Dillon ap->ap_active_cnt++; 1771258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 17721980eff3SMatthew Dillon } 1773258223a3SMatthew Dillon } 1774258223a3SMatthew Dillon } 1775258223a3SMatthew Dillon 1776258223a3SMatthew Dillon void 1777258223a3SMatthew Dillon ahci_intr(void *arg) 1778258223a3SMatthew Dillon { 1779258223a3SMatthew Dillon struct ahci_softc *sc = arg; 1780258223a3SMatthew Dillon u_int32_t is, ack = 0; 1781258223a3SMatthew Dillon int port; 1782258223a3SMatthew Dillon 1783258223a3SMatthew Dillon /* Read global interrupt status */ 1784258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 1785258223a3SMatthew Dillon if (is == 0 || is == 0xffffffff) 1786258223a3SMatthew Dillon return; 1787258223a3SMatthew Dillon ack = is; 1788258223a3SMatthew Dillon 1789258223a3SMatthew Dillon #ifdef AHCI_COALESCE 1790258223a3SMatthew Dillon /* Check coalescing interrupt first */ 1791258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 1792258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 1793258223a3SMatthew Dillon DEVNAME(sc)); 1794258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 1795258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 1796258223a3SMatthew Dillon } 1797258223a3SMatthew Dillon #endif 1798258223a3SMatthew Dillon 1799258223a3SMatthew Dillon /* Process interrupts for each port */ 1800258223a3SMatthew Dillon while (is) { 1801258223a3SMatthew Dillon port = ffs(is) - 1; 1802258223a3SMatthew Dillon if (sc->sc_ports[port]) { 1803258223a3SMatthew Dillon ahci_port_intr(sc->sc_ports[port], 1804258223a3SMatthew Dillon AHCI_PREG_CI_ALL_SLOTS); 1805258223a3SMatthew Dillon } 1806258223a3SMatthew Dillon is &= ~(1 << port); 1807258223a3SMatthew Dillon } 1808258223a3SMatthew Dillon 1809258223a3SMatthew Dillon /* Finally, acknowledge global interrupt */ 1810258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, ack); 1811258223a3SMatthew Dillon } 1812258223a3SMatthew Dillon 1813258223a3SMatthew Dillon u_int32_t 1814258223a3SMatthew Dillon ahci_port_intr(struct ahci_port *ap, u_int32_t ci_mask) 1815258223a3SMatthew Dillon { 1816258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1817258223a3SMatthew Dillon u_int32_t is, ci_saved, ci_masked, processed = 0; 181822181ab7SMatthew Dillon int slot; 1819258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 18201980eff3SMatthew Dillon struct ata_port *ccb_at = NULL; 1821258223a3SMatthew Dillon volatile u_int32_t *active; 1822258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1823258223a3SMatthew Dillon u_int32_t tmp; 1824258223a3SMatthew Dillon #endif 182522181ab7SMatthew Dillon enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT, 182622181ab7SMatthew Dillon NEED_HOTPLUG_REMOVE } need = NEED_NOTHING; 1827258223a3SMatthew Dillon 1828258223a3SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 1829258223a3SMatthew Dillon 18301980eff3SMatthew Dillon #if 0 18311980eff3SMatthew Dillon kprintf("%s: INTERRUPT %b\n", PORTNAME(ap), 18321980eff3SMatthew Dillon is, AHCI_PFMT_IS); 18331980eff3SMatthew Dillon #endif 18341980eff3SMatthew Dillon 1835258223a3SMatthew Dillon /* Ack port interrupt only if checking all command slots. */ 1836258223a3SMatthew Dillon if (ci_mask == AHCI_PREG_CI_ALL_SLOTS) 1837258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is); 1838258223a3SMatthew Dillon 1839258223a3SMatthew Dillon if (is) 1840258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: interrupt: %b\n", PORTNAME(ap), 1841258223a3SMatthew Dillon is, AHCI_PFMT_IS); 1842258223a3SMatthew Dillon 1843258223a3SMatthew Dillon if (ap->ap_sactive) { 1844258223a3SMatthew Dillon /* Active NCQ commands - use SActive instead of CI */ 1845258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 1846258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1847258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 1848258223a3SMatthew Dillon active = &ap->ap_sactive; 1849258223a3SMatthew Dillon } else { 1850258223a3SMatthew Dillon /* Save CI */ 1851258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 1852258223a3SMatthew Dillon active = &ap->ap_active; 1853258223a3SMatthew Dillon } 1854258223a3SMatthew Dillon 18551980eff3SMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 1856cf5f3a81SMatthew Dillon /* 1857cf5f3a81SMatthew Dillon * Command failed. See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. 18581980eff3SMatthew Dillon * 18591980eff3SMatthew Dillon * This stops command processing. 1860cf5f3a81SMatthew Dillon */ 1861258223a3SMatthew Dillon u_int32_t tfd, serr; 1862258223a3SMatthew Dillon int err_slot; 1863258223a3SMatthew Dillon 1864258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 1865258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 1866258223a3SMatthew Dillon 1867cf5f3a81SMatthew Dillon /* 1868cf5f3a81SMatthew Dillon * If no NCQ commands are active the error slot is easily 1869cf5f3a81SMatthew Dillon * determined, otherwise we have to extract the error 1870cf5f3a81SMatthew Dillon * from the log page. 1871cf5f3a81SMatthew Dillon */ 1872258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 1873cf5f3a81SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 1874cf5f3a81SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 1875258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 18761980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 1877258223a3SMatthew Dillon 1878258223a3SMatthew Dillon /* Preserve received taskfile data from the RFIS. */ 1879258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 1880258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 1881cf5f3a81SMatthew Dillon } else { 1882cf5f3a81SMatthew Dillon err_slot = -1; 1883cf5f3a81SMatthew Dillon } 1884258223a3SMatthew Dillon 18851980eff3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n", 18861980eff3SMatthew Dillon PORTNAME(ap), err_slot, 18871980eff3SMatthew Dillon tfd, AHCI_PFMT_TFD_STS, 18881980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 1889258223a3SMatthew Dillon 1890cf5f3a81SMatthew Dillon /* Stopping the port clears CI and SACT */ 1891258223a3SMatthew Dillon ahci_port_stop(ap, 0); 189222181ab7SMatthew Dillon need = NEED_RESTART; 1893258223a3SMatthew Dillon 1894cf5f3a81SMatthew Dillon /* 1895cf5f3a81SMatthew Dillon * Clear SERR (primarily DIAG_X) to enable capturing of the 1896cf5f3a81SMatthew Dillon * next error. 1897cf5f3a81SMatthew Dillon */ 1898258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 1899258223a3SMatthew Dillon 1900258223a3SMatthew Dillon /* Acknowledge the interrupts we can recover from. */ 1901cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 1902cf5f3a81SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS); 19031980eff3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS); 1904258223a3SMatthew Dillon 1905258223a3SMatthew Dillon /* If device hasn't cleared its busy status, try to idle it. */ 1906258223a3SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 19071980eff3SMatthew Dillon kprintf("%s: Attempting to idle device ccb=%p\n", 19081980eff3SMatthew Dillon PORTNAME(ap), ccb_at); 19091980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 19101980eff3SMatthew Dillon goto fatal; 19111980eff3SMatthew Dillon /* 19121980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 19131980eff3SMatthew Dillon * 19141980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 19151980eff3SMatthew Dillon * busy condition and make the ahci port usable for 19161980eff3SMatthew Dillon * the remaining devices. 19171980eff3SMatthew Dillon */ 19181980eff3SMatthew Dillon if (ccb_at) { 19191980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 19201980eff3SMatthew Dillon ahci_port_clo(ap); 19211980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 192217eab71eSMatthew Dillon kprintf("%s: Unable to idle device, port " 192317eab71eSMatthew Dillon "bricked on us\n", 1924258223a3SMatthew Dillon PORTNAME(ap)); 1925258223a3SMatthew Dillon goto fatal; 1926258223a3SMatthew Dillon } 1927258223a3SMatthew Dillon 1928258223a3SMatthew Dillon /* Had to reset device, can't gather extended info. */ 1929258223a3SMatthew Dillon } else if (ap->ap_sactive) { 19301980eff3SMatthew Dillon /* 19311980eff3SMatthew Dillon * Recover the NCQ error from log page 10h. 19321980eff3SMatthew Dillon * 19331980eff3SMatthew Dillon * XXX NCQ currently not supported with port 19341980eff3SMatthew Dillon * multiplier. 19351980eff3SMatthew Dillon */ 1936258223a3SMatthew Dillon ahci_port_read_ncq_error(ap, &err_slot); 1937cf5f3a81SMatthew Dillon kprintf("recover from NCQ error err_slot %d\n", err_slot); 1938258223a3SMatthew Dillon if (err_slot < 0) 1939258223a3SMatthew Dillon goto failall; 1940258223a3SMatthew Dillon 1941258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n", 1942258223a3SMatthew Dillon PORTNAME(ap), err_slot); 1943258223a3SMatthew Dillon 1944258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 1945258223a3SMatthew Dillon } else { 1946258223a3SMatthew Dillon /* Didn't reset, could gather extended info from log. */ 19471980eff3SMatthew Dillon kprintf("%s: didn't reset err_slot %d " 19481980eff3SMatthew Dillon "sact=%08x act=%08x\n", 19491980eff3SMatthew Dillon PORTNAME(ap), 1950cf5f3a81SMatthew Dillon err_slot, ap->ap_sactive, ap->ap_active); 1951258223a3SMatthew Dillon } 1952258223a3SMatthew Dillon 1953258223a3SMatthew Dillon /* 1954258223a3SMatthew Dillon * If we couldn't determine the errored slot, reset the port 1955258223a3SMatthew Dillon * and fail all the active slots. 1956258223a3SMatthew Dillon */ 1957258223a3SMatthew Dillon if (err_slot == -1) { 19581980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 19591980eff3SMatthew Dillon goto fatal; 19601980eff3SMatthew Dillon /* 19611980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 19621980eff3SMatthew Dillon * 19631980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 19641980eff3SMatthew Dillon * busy condition and make the ahci port usable for 19651980eff3SMatthew Dillon * the remaining devices. 19661980eff3SMatthew Dillon */ 19671980eff3SMatthew Dillon if (ccb_at) { 19681980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 19691980eff3SMatthew Dillon ahci_port_clo(ap); 19701980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 197117eab71eSMatthew Dillon kprintf("%s: Unable to idle device after " 197217eab71eSMatthew Dillon "NCQ error, port bricked on us\n", 1973258223a3SMatthew Dillon PORTNAME(ap)); 1974258223a3SMatthew Dillon goto fatal; 1975258223a3SMatthew Dillon } 1976258223a3SMatthew Dillon kprintf("%s: couldn't recover NCQ error, failing " 1977258223a3SMatthew Dillon "all outstanding commands.\n", 1978258223a3SMatthew Dillon PORTNAME(ap)); 1979258223a3SMatthew Dillon goto failall; 1980258223a3SMatthew Dillon } 1981258223a3SMatthew Dillon 1982258223a3SMatthew Dillon /* Clear the failed command in saved CI so completion runs. */ 1983258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 1984258223a3SMatthew Dillon 1985258223a3SMatthew Dillon /* Note the error in the ata_xfer. */ 1986258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 1987258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1988258223a3SMatthew Dillon 1989258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1990258223a3SMatthew Dillon /* There may only be one outstanding standard command now. */ 1991258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 1992258223a3SMatthew Dillon tmp = ci_saved; 1993258223a3SMatthew Dillon if (tmp) { 1994258223a3SMatthew Dillon slot = ffs(tmp) - 1; 1995258223a3SMatthew Dillon tmp &= ~(1 << slot); 1996258223a3SMatthew Dillon KKASSERT(tmp == 0); 1997258223a3SMatthew Dillon } 1998258223a3SMatthew Dillon } 1999258223a3SMatthew Dillon #endif 20001980eff3SMatthew Dillon } else if (is & AHCI_PREG_IS_DHRS) { 20011980eff3SMatthew Dillon /* 20021980eff3SMatthew Dillon * Command posted D2H register FIS to the rfis. This 2003*8bf6a3ffSMatthew Dillon * does NOT stop command processing and it is unclear 2004*8bf6a3ffSMatthew Dillon * how we are supposed to deal with it other then using 2005*8bf6a3ffSMatthew Dillon * only a queue of 1. 2006*8bf6a3ffSMatthew Dillon * 2007*8bf6a3ffSMatthew Dillon * We must copy the port rfis to the ccb and restart 2008*8bf6a3ffSMatthew Dillon * command processing. ahci_pm_read() does not function 2009*8bf6a3ffSMatthew Dillon * without this support. 20101980eff3SMatthew Dillon */ 20111980eff3SMatthew Dillon int err_slot; 20121980eff3SMatthew Dillon 20131980eff3SMatthew Dillon if (ap->ap_sactive == 0) { 20141980eff3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 20151980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 20161980eff3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 20171980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 20181980eff3SMatthew Dillon 20191980eff3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 20201980eff3SMatthew Dillon sizeof(struct ata_fis_d2h)); 20211980eff3SMatthew Dillon } else { 20221980eff3SMatthew Dillon kprintf("%s: Unexpected DHRS posted while " 20231980eff3SMatthew Dillon "NCQ running\n", PORTNAME(ap)); 20241980eff3SMatthew Dillon err_slot = -1; 2025258223a3SMatthew Dillon } 20261980eff3SMatthew Dillon } 20271980eff3SMatthew Dillon 20281980eff3SMatthew Dillon /* 20291980eff3SMatthew Dillon * Device notification to us. 20301980eff3SMatthew Dillon * 20311980eff3SMatthew Dillon * For some reason this interrupt can occur without any notification 20321980eff3SMatthew Dillon * bits actually being set. 20331980eff3SMatthew Dillon */ 20341980eff3SMatthew Dillon if ((is & AHCI_PREG_IS_SDBS) && (sc->sc_cap & AHCI_REG_CAP_SSNTF)) { 20351980eff3SMatthew Dillon u_int32_t data; 20361980eff3SMatthew Dillon 20371980eff3SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SNTF); 20381980eff3SMatthew Dillon if (data) { 20391980eff3SMatthew Dillon kprintf("%s: NOTIFY %08x\n", PORTNAME(ap), data); 20401980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, data); 20411980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS); 20421980eff3SMatthew Dillon } 20431980eff3SMatthew Dillon } 20441980eff3SMatthew Dillon #if 0 20451980eff3SMatthew Dillon /* XXX future IFS recovery code? or just scrap it */ 20461980eff3SMatthew Dillon if (is & AHCI_PREG_IS_IFS) { 20471980eff3SMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 20481980eff3SMatthew Dillon kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n", 20491980eff3SMatthew Dillon PORTNAME(ap), 20501980eff3SMatthew Dillon is, AHCI_PFMT_IS, 20511980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 20521980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 20531980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 20541980eff3SMatthew Dillon is &= ~AHCI_PREG_IS_IFS; 20551980eff3SMatthew Dillon ahci_port_stop(ap, 0); 20561980eff3SMatthew Dillon ahci_port_start(ap); 20571980eff3SMatthew Dillon need = NEED_RESTART; 20581980eff3SMatthew Dillon } 20591980eff3SMatthew Dillon #endif 2060258223a3SMatthew Dillon 2061258223a3SMatthew Dillon /* 2062258223a3SMatthew Dillon * Port change (hot-plug). 2063258223a3SMatthew Dillon * 2064258223a3SMatthew Dillon * A PCS interrupt will occur on hot-plug once communication is 2065258223a3SMatthew Dillon * established. 2066258223a3SMatthew Dillon * 2067258223a3SMatthew Dillon * A PRCS interrupt will occur on hot-unplug (and possibly also 2068258223a3SMatthew Dillon * on hot-plug). 2069258223a3SMatthew Dillon * 207022181ab7SMatthew Dillon * XXX We can then check the CPS (Cold Presence State) bit, if 207122181ab7SMatthew Dillon * supported, to determine if a device is plugged in or not and do 207222181ab7SMatthew Dillon * the right thing. 207322181ab7SMatthew Dillon * 207422181ab7SMatthew Dillon * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and 207522181ab7SMatthew Dillon * can also occur if an unsolicited COMINIT is received. 207622181ab7SMatthew Dillon * If this occurs command processing is automatically 207722181ab7SMatthew Dillon * stopped (CR goes inactive) and the port must be stopped 207822181ab7SMatthew Dillon * and restarted. 2079258223a3SMatthew Dillon */ 2080258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 2081258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 20821980eff3SMatthew Dillon (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X)); 208322181ab7SMatthew Dillon ahci_port_stop(ap, 0); 2084258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 2085258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 20861980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 208722181ab7SMatthew Dillon need = NEED_HOTPLUG_INSERT; 208822181ab7SMatthew Dillon goto fatal; 2089258223a3SMatthew Dillon } 209022181ab7SMatthew Dillon need = NEED_RESTART; 2091258223a3SMatthew Dillon break; 2092258223a3SMatthew Dillon default: 20931980eff3SMatthew Dillon if (ap->ap_type != ATA_PORT_T_NONE) { 209422181ab7SMatthew Dillon need = NEED_HOTPLUG_REMOVE; 209522181ab7SMatthew Dillon goto fatal; 2096258223a3SMatthew Dillon } 209722181ab7SMatthew Dillon need = NEED_RESTART; 2098258223a3SMatthew Dillon break; 2099258223a3SMatthew Dillon } 2100258223a3SMatthew Dillon } 2101258223a3SMatthew Dillon 210222181ab7SMatthew Dillon /* 210322181ab7SMatthew Dillon * Check for remaining errors - they are fatal. 210422181ab7SMatthew Dillon */ 2105258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 2106258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 21074444122dSMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 21081980eff3SMatthew Dillon kprintf("%s: unrecoverable errors (IS: %b, SERR: %b), " 21094444122dSMatthew Dillon "disabling port.\n", 21104444122dSMatthew Dillon PORTNAME(ap), 21114444122dSMatthew Dillon is, AHCI_PFMT_IS, 21121980eff3SMatthew Dillon serr, AHCI_PFMT_SERR 21134444122dSMatthew Dillon ); 2114258223a3SMatthew Dillon /* XXX try recovery first */ 2115258223a3SMatthew Dillon goto fatal; 2116258223a3SMatthew Dillon } 2117258223a3SMatthew Dillon 211822181ab7SMatthew Dillon /* 211922181ab7SMatthew Dillon * Fail all outstanding commands if we know the port won't recover. 21201980eff3SMatthew Dillon * 21211980eff3SMatthew Dillon * We may have a ccb_at if the failed command is known and was 21221980eff3SMatthew Dillon * being sent to a device over a port multiplier (PM). In this 21231980eff3SMatthew Dillon * case if the port itself has not completely failed we fail just 21241980eff3SMatthew Dillon * the commands related to that target. 212522181ab7SMatthew Dillon */ 2126258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2127258223a3SMatthew Dillon fatal: 2128258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 2129258223a3SMatthew Dillon failall: 2130258223a3SMatthew Dillon 2131cf5f3a81SMatthew Dillon /* Stopping the port clears CI/SACT */ 2132cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 2133258223a3SMatthew Dillon 21341980eff3SMatthew Dillon /* 21351980eff3SMatthew Dillon * Error all the active slots. If running across a PM 21361980eff3SMatthew Dillon * try to error out just the slots related to the target. 21371980eff3SMatthew Dillon */ 2138258223a3SMatthew Dillon ci_masked = ci_saved & *active; 2139258223a3SMatthew Dillon while (ci_masked) { 2140258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2141258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 21421980eff3SMatthew Dillon if (ccb_at == ccb->ccb_xa.at || 21431980eff3SMatthew Dillon ap->ap_state == AP_S_FATAL_ERROR) { 2144258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2145258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2146258223a3SMatthew Dillon } 21471980eff3SMatthew Dillon } 2148258223a3SMatthew Dillon 2149258223a3SMatthew Dillon /* Run completion for all active slots. */ 2150258223a3SMatthew Dillon ci_saved &= ~*active; 2151258223a3SMatthew Dillon 2152258223a3SMatthew Dillon /* 2153258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 2154258223a3SMatthew Dillon * 2155258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 2156258223a3SMatthew Dillon * a livelock. 2157258223a3SMatthew Dillon */ 2158258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 215922181ab7SMatthew Dillon if (need == NEED_RESTART) 216022181ab7SMatthew Dillon need = NEED_NOTHING; 2161258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2162258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2163258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2164258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 2165258223a3SMatthew Dillon } 2166258223a3SMatthew Dillon } 2167258223a3SMatthew Dillon 2168258223a3SMatthew Dillon /* 2169258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 2170258223a3SMatthew Dillon * changed to zero some time after we activated it. 2171258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 2172cf5f3a81SMatthew Dillon * 2173cf5f3a81SMatthew Dillon * Any active bits not saved are completed within the restrictions 2174cf5f3a81SMatthew Dillon * imposed by the caller. 2175258223a3SMatthew Dillon */ 2176258223a3SMatthew Dillon ci_masked = ~ci_saved & *active & ci_mask; 2177258223a3SMatthew Dillon while (ci_masked) { 2178258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2179258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2180258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2181258223a3SMatthew Dillon 2182258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 2183258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 2184258223a3SMatthew Dillon " (error)" : ""); 2185258223a3SMatthew Dillon 2186258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2187258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2188258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2189258223a3SMatthew Dillon 2190258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2191258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2192258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2193258223a3SMatthew Dillon 2194258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2195258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2196258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 2197258223a3SMatthew Dillon 2198258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 21991980eff3SMatthew Dillon if (active == &ap->ap_active) { 22001980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 22011980eff3SMatthew Dillon --ap->ap_active_cnt; 22021980eff3SMatthew Dillon } 2203258223a3SMatthew Dillon ccb->ccb_done(ccb); 2204258223a3SMatthew Dillon 2205258223a3SMatthew Dillon processed |= 1 << ccb->ccb_slot; 2206258223a3SMatthew Dillon } 2207258223a3SMatthew Dillon 220822181ab7SMatthew Dillon switch(need) { 220922181ab7SMatthew Dillon case NEED_RESTART: 221022181ab7SMatthew Dillon /* 221122181ab7SMatthew Dillon * A recoverable error occured and we can restart outstanding 221222181ab7SMatthew Dillon * commands on the port. 221322181ab7SMatthew Dillon */ 221417eab71eSMatthew Dillon ahci_port_start(ap); 2215258223a3SMatthew Dillon 2216258223a3SMatthew Dillon if (ci_saved) { 2217258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2218258223a3SMatthew Dillon tmp = ci_saved; 2219258223a3SMatthew Dillon while (tmp) { 2220258223a3SMatthew Dillon slot = ffs(tmp) - 1; 2221258223a3SMatthew Dillon tmp &= ~(1 << slot); 2222258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2223258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2224258223a3SMatthew Dillon KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) == 2225258223a3SMatthew Dillon (!!ap->ap_sactive)); 2226258223a3SMatthew Dillon } 2227258223a3SMatthew Dillon #endif 2228258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr " 2229258223a3SMatthew Dillon "re-enabling%s slots %08x\n", PORTNAME(ap), 2230258223a3SMatthew Dillon ap->ap_sactive ? " NCQ" : "", ci_saved); 2231258223a3SMatthew Dillon 2232258223a3SMatthew Dillon if (ap->ap_sactive) 2233258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 2234258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 2235258223a3SMatthew Dillon } 223622181ab7SMatthew Dillon break; 223722181ab7SMatthew Dillon case NEED_HOTPLUG_INSERT: 223822181ab7SMatthew Dillon /* 2239cf5f3a81SMatthew Dillon * A hot-plug insertion event has occured and all 2240cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 22411980eff3SMatthew Dillon * 22421980eff3SMatthew Dillon * Don't recurse if this occurs while we are 22431980eff3SMatthew Dillon * resetting the port. 224422181ab7SMatthew Dillon */ 22451980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 224622181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 224722181ab7SMatthew Dillon PORTNAME(ap)); 22481980eff3SMatthew Dillon if (ahci_port_init(ap, NULL) == 0) 224922181ab7SMatthew Dillon ahci_cam_changed(ap, 1); 22501980eff3SMatthew Dillon } 225122181ab7SMatthew Dillon break; 225222181ab7SMatthew Dillon case NEED_HOTPLUG_REMOVE: 2253cf5f3a81SMatthew Dillon /* 2254cf5f3a81SMatthew Dillon * A hot-plug removal event has occured and all 2255cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 22561980eff3SMatthew Dillon * 22571980eff3SMatthew Dillon * Don't recurse if this occurs while we are 22581980eff3SMatthew Dillon * resetting the port. 2259cf5f3a81SMatthew Dillon */ 22601980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 226122181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 226222181ab7SMatthew Dillon PORTNAME(ap)); 2263cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 226422181ab7SMatthew Dillon ahci_cam_changed(ap, 0); 22651980eff3SMatthew Dillon } 226622181ab7SMatthew Dillon break; 226722181ab7SMatthew Dillon default: 226822181ab7SMatthew Dillon break; 2269258223a3SMatthew Dillon } 2270258223a3SMatthew Dillon return (processed); 2271258223a3SMatthew Dillon } 2272258223a3SMatthew Dillon 2273258223a3SMatthew Dillon struct ahci_ccb * 2274258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 2275258223a3SMatthew Dillon { 2276258223a3SMatthew Dillon struct ahci_ccb *ccb; 2277258223a3SMatthew Dillon 2278258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2279258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 2280258223a3SMatthew Dillon if (ccb != NULL) { 2281258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 2282258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 2283258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 22841980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 2285258223a3SMatthew Dillon } 2286258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2287258223a3SMatthew Dillon 2288258223a3SMatthew Dillon return (ccb); 2289258223a3SMatthew Dillon } 2290258223a3SMatthew Dillon 2291258223a3SMatthew Dillon void 2292258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 2293258223a3SMatthew Dillon { 2294258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2295258223a3SMatthew Dillon 2296258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2297258223a3SMatthew Dillon if (ccb->ccb_xa.state != ATA_S_COMPLETE && 2298258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_TIMEOUT && 2299258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_ERROR) { 2300258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, " 2301258223a3SMatthew Dillon "slot %d\n", 2302258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), ccb->ccb_xa.state, 2303258223a3SMatthew Dillon ccb->ccb_slot); 2304258223a3SMatthew Dillon } 2305258223a3SMatthew Dillon #endif 2306258223a3SMatthew Dillon 2307258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 2308258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2309258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 2310258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2311258223a3SMatthew Dillon } 2312258223a3SMatthew Dillon 2313258223a3SMatthew Dillon struct ahci_ccb * 2314258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 2315258223a3SMatthew Dillon { 2316258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 2317258223a3SMatthew Dillon u_int32_t sact; 2318258223a3SMatthew Dillon 2319258223a3SMatthew Dillon /* No commands may be active on the chip. */ 2320258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 2321258223a3SMatthew Dillon if (sact != 0) 2322258223a3SMatthew Dillon kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact); 2323258223a3SMatthew Dillon KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0); 2324258223a3SMatthew Dillon 2325258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2326258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy == 0); 2327258223a3SMatthew Dillon ap->ap_err_busy = 1; 2328258223a3SMatthew Dillon #endif 2329258223a3SMatthew Dillon /* Save outstanding command state. */ 2330258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 2331258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 2332258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 2333258223a3SMatthew Dillon 2334258223a3SMatthew Dillon /* 2335258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 2336258223a3SMatthew Dillon * run prematurely. 2337258223a3SMatthew Dillon */ 2338258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 2339258223a3SMatthew Dillon 2340258223a3SMatthew Dillon /* 2341258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 2342258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 2343258223a3SMatthew Dillon */ 2344258223a3SMatthew Dillon err_ccb = ahci_get_ccb(ap); 2345258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 2346258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 2347258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 2348258223a3SMatthew Dillon 2349258223a3SMatthew Dillon return err_ccb; 2350258223a3SMatthew Dillon } 2351258223a3SMatthew Dillon 2352258223a3SMatthew Dillon void 2353258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 2354258223a3SMatthew Dillon { 2355258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2356258223a3SMatthew Dillon u_int32_t sact; 23575f8c1efdSMatthew Dillon u_int32_t ci; 2358258223a3SMatthew Dillon 2359258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2360258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy); 2361258223a3SMatthew Dillon #endif 23625f8c1efdSMatthew Dillon /* 23635f8c1efdSMatthew Dillon * No commands may be active on the chip 23645f8c1efdSMatthew Dillon */ 2365258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 23665f8c1efdSMatthew Dillon if (sact) { 23675f8c1efdSMatthew Dillon panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n", 23685f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 2369258223a3SMatthew Dillon } 23705f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 23715f8c1efdSMatthew Dillon if (ci) { 2372cf5f3a81SMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0 " 2373cf5f3a81SMatthew Dillon "(act=%08x sact=%08x)\n", 2374cf5f3a81SMatthew Dillon ccb->ccb_slot, ci, 2375cf5f3a81SMatthew Dillon ap->ap_active, ap->ap_sactive); 23765f8c1efdSMatthew Dillon } 2377258223a3SMatthew Dillon 2378258223a3SMatthew Dillon /* Done with the CCB */ 2379258223a3SMatthew Dillon ahci_put_ccb(ccb); 2380258223a3SMatthew Dillon 2381258223a3SMatthew Dillon /* Restore outstanding command state */ 2382258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 2383258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 2384258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 2385258223a3SMatthew Dillon 2386258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2387258223a3SMatthew Dillon ap->ap_err_busy = 0; 2388258223a3SMatthew Dillon #endif 2389258223a3SMatthew Dillon } 2390258223a3SMatthew Dillon 23911980eff3SMatthew Dillon /* 23921980eff3SMatthew Dillon * Read log page to get NCQ error. 23931980eff3SMatthew Dillon * 23941980eff3SMatthew Dillon * NOTE: NCQ not currently supported on port multipliers. XXX 23951980eff3SMatthew Dillon */ 2396258223a3SMatthew Dillon int 2397258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp) 2398258223a3SMatthew Dillon { 2399258223a3SMatthew Dillon struct ahci_ccb *ccb; 2400258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 2401258223a3SMatthew Dillon u_int32_t cmd; 2402258223a3SMatthew Dillon struct ata_fis_h2d *fis; 2403258223a3SMatthew Dillon int rc = EIO; 2404258223a3SMatthew Dillon 2405258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap)); 2406258223a3SMatthew Dillon 2407258223a3SMatthew Dillon /* Save command register state. */ 2408258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 2409258223a3SMatthew Dillon 2410258223a3SMatthew Dillon /* Port should have been idled already. Start it. */ 2411258223a3SMatthew Dillon KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0); 241217eab71eSMatthew Dillon ahci_port_start(ap); 2413258223a3SMatthew Dillon 2414258223a3SMatthew Dillon /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */ 2415258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 2416258223a3SMatthew Dillon ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL; 2417258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 2418258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 2419258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 2420258223a3SMatthew Dillon bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table)); 2421258223a3SMatthew Dillon 2422258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 2423258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 2424258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 2425258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 2426258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 2427258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 2428258223a3SMatthew Dillon fis->sector_count_exp = 0; 2429258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 2430258223a3SMatthew Dillon fis->lba_mid_exp = 0; 2431258223a3SMatthew Dillon fis->device = 0; 2432258223a3SMatthew Dillon 2433258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 2434258223a3SMatthew Dillon 2435258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 2436258223a3SMatthew Dillon rc = ENOMEM; /* XXX caller must abort all commands */ 2437258223a3SMatthew Dillon goto err; 2438258223a3SMatthew Dillon } 2439258223a3SMatthew Dillon 2440258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 2441258223a3SMatthew Dillon if (ahci_poll(ccb, hz, NULL) != 0) 2442258223a3SMatthew Dillon goto err; 2443258223a3SMatthew Dillon 2444258223a3SMatthew Dillon rc = 0; 2445258223a3SMatthew Dillon err: 2446258223a3SMatthew Dillon /* Abort our command, if it failed, by stopping command DMA. */ 24471980eff3SMatthew Dillon if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) { 2448258223a3SMatthew Dillon kprintf("%s: log page read failed, slot %d was still active.\n", 2449258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 2450258223a3SMatthew Dillon ahci_port_stop(ap, 0); 2451258223a3SMatthew Dillon } 2452258223a3SMatthew Dillon 2453258223a3SMatthew Dillon /* Done with the error CCB now. */ 2454258223a3SMatthew Dillon ahci_unload_prdt(ccb); 2455258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 2456258223a3SMatthew Dillon 2457258223a3SMatthew Dillon /* Extract failed register set and tags from the scratch space. */ 2458258223a3SMatthew Dillon if (rc == 0) { 2459258223a3SMatthew Dillon struct ata_log_page_10h *log; 2460258223a3SMatthew Dillon int err_slot; 2461258223a3SMatthew Dillon 2462258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 2463258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 2464258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 2465258223a3SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ " 2466258223a3SMatthew Dillon "error?\n", 2467258223a3SMatthew Dillon PORTNAME(ap)); 2468258223a3SMatthew Dillon rc = ESRCH; 2469258223a3SMatthew Dillon } else { 2470258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 2471258223a3SMatthew Dillon *err_slotp = err_slot = log->err_regs.type & 2472258223a3SMatthew Dillon ATA_LOG_10H_TYPE_TAG_MASK; 2473258223a3SMatthew Dillon 2474258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2475258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, &log->err_regs, 2476258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 2477258223a3SMatthew Dillon ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 2478258223a3SMatthew Dillon ccb->ccb_xa.rfis.flags = 0; 2479258223a3SMatthew Dillon } 2480258223a3SMatthew Dillon } 2481258223a3SMatthew Dillon 2482258223a3SMatthew Dillon /* Restore saved CMD register state */ 2483258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 2484258223a3SMatthew Dillon 2485258223a3SMatthew Dillon return (rc); 2486258223a3SMatthew Dillon } 2487258223a3SMatthew Dillon 2488258223a3SMatthew Dillon /* 2489258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 2490258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 2491258223a3SMatthew Dillon * single physical base address. 2492258223a3SMatthew Dillon */ 2493258223a3SMatthew Dillon struct ahci_dmamem * 2494258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 2495258223a3SMatthew Dillon { 2496258223a3SMatthew Dillon struct ahci_dmamem *adm; 2497258223a3SMatthew Dillon int error; 2498258223a3SMatthew Dillon 2499258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 2500258223a3SMatthew Dillon 2501258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 2502258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 2503258223a3SMatthew Dillon if (error == 0) { 2504258223a3SMatthew Dillon adm->adm_tag = tag; 2505258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 2506258223a3SMatthew Dillon adm->adm_kva, 2507258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 2508258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 2509258223a3SMatthew Dillon 0); 2510258223a3SMatthew Dillon } 2511258223a3SMatthew Dillon if (error) { 2512258223a3SMatthew Dillon if (adm->adm_map) { 2513258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 2514258223a3SMatthew Dillon adm->adm_map = NULL; 2515258223a3SMatthew Dillon adm->adm_tag = NULL; 2516258223a3SMatthew Dillon adm->adm_kva = NULL; 2517258223a3SMatthew Dillon } 2518258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2519258223a3SMatthew Dillon adm = NULL; 2520258223a3SMatthew Dillon } 2521258223a3SMatthew Dillon return (adm); 2522258223a3SMatthew Dillon } 2523258223a3SMatthew Dillon 2524258223a3SMatthew Dillon static 2525258223a3SMatthew Dillon void 2526258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 2527258223a3SMatthew Dillon { 2528258223a3SMatthew Dillon KKASSERT(error == 0); 2529258223a3SMatthew Dillon KKASSERT(nsegs == 1); 2530258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 2531258223a3SMatthew Dillon } 2532258223a3SMatthew Dillon 2533258223a3SMatthew Dillon 2534258223a3SMatthew Dillon void 2535258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 2536258223a3SMatthew Dillon { 2537258223a3SMatthew Dillon if (adm->adm_map) { 2538258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 2539258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 2540258223a3SMatthew Dillon adm->adm_map = NULL; 2541258223a3SMatthew Dillon adm->adm_tag = NULL; 2542258223a3SMatthew Dillon adm->adm_kva = NULL; 2543258223a3SMatthew Dillon } 2544258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2545258223a3SMatthew Dillon } 2546258223a3SMatthew Dillon 2547258223a3SMatthew Dillon u_int32_t 2548258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 2549258223a3SMatthew Dillon { 2550258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2551258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 2552258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 2553258223a3SMatthew Dillon } 2554258223a3SMatthew Dillon 2555258223a3SMatthew Dillon void 2556258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 2557258223a3SMatthew Dillon { 2558258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 2559258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2560258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 2561258223a3SMatthew Dillon } 2562258223a3SMatthew Dillon 2563258223a3SMatthew Dillon int 2564258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 2565258223a3SMatthew Dillon u_int32_t target) 2566258223a3SMatthew Dillon { 2567258223a3SMatthew Dillon int i; 2568258223a3SMatthew Dillon 2569258223a3SMatthew Dillon for (i = 0; i < 1000; i++) { 2570258223a3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 2571258223a3SMatthew Dillon return (0); 2572258223a3SMatthew Dillon DELAY(1000); 2573258223a3SMatthew Dillon } 2574258223a3SMatthew Dillon 2575258223a3SMatthew Dillon return (1); 2576258223a3SMatthew Dillon } 2577258223a3SMatthew Dillon 2578258223a3SMatthew Dillon u_int32_t 2579258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 2580258223a3SMatthew Dillon { 2581258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 2582258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 2583258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 2584258223a3SMatthew Dillon } 2585258223a3SMatthew Dillon 2586258223a3SMatthew Dillon void 2587258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 2588258223a3SMatthew Dillon { 2589258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 2590258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 2591258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 2592258223a3SMatthew Dillon } 2593258223a3SMatthew Dillon 2594258223a3SMatthew Dillon int 2595cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 2596cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 2597258223a3SMatthew Dillon { 2598258223a3SMatthew Dillon int i; 2599258223a3SMatthew Dillon 2600cec85a37SMatthew Dillon for (i = 0; i < timeout; i++) { 2601258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 2602258223a3SMatthew Dillon return (0); 2603258223a3SMatthew Dillon DELAY(1000); 2604258223a3SMatthew Dillon } 2605258223a3SMatthew Dillon 2606258223a3SMatthew Dillon return (1); 2607258223a3SMatthew Dillon } 2608258223a3SMatthew Dillon 26091980eff3SMatthew Dillon /* 26101980eff3SMatthew Dillon * Acquire an ata transfer. 26111980eff3SMatthew Dillon * 26121980eff3SMatthew Dillon * Pass a NULL at for direct-attached transfers, and a non-NULL at for 26131980eff3SMatthew Dillon * targets that go through the port multiplier. 26141980eff3SMatthew Dillon */ 2615258223a3SMatthew Dillon struct ata_xfer * 26161980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at) 2617258223a3SMatthew Dillon { 2618258223a3SMatthew Dillon struct ahci_ccb *ccb; 2619258223a3SMatthew Dillon 2620258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 2621258223a3SMatthew Dillon if (ccb == NULL) { 2622258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 2623258223a3SMatthew Dillon PORTNAME(ap)); 2624258223a3SMatthew Dillon return (NULL); 2625258223a3SMatthew Dillon } 2626258223a3SMatthew Dillon 2627258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 2628258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 2629258223a3SMatthew Dillon 26301980eff3SMatthew Dillon ccb->ccb_xa.at = at; 2631258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 2632258223a3SMatthew Dillon 2633258223a3SMatthew Dillon return (&ccb->ccb_xa); 2634258223a3SMatthew Dillon } 2635258223a3SMatthew Dillon 2636258223a3SMatthew Dillon void 2637258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 2638258223a3SMatthew Dillon { 2639258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 2640258223a3SMatthew Dillon 2641258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 2642258223a3SMatthew Dillon 2643258223a3SMatthew Dillon ahci_put_ccb(ccb); 2644258223a3SMatthew Dillon } 2645258223a3SMatthew Dillon 2646258223a3SMatthew Dillon int 2647258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 2648258223a3SMatthew Dillon { 2649258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 2650258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 2651258223a3SMatthew Dillon 2652258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 2653258223a3SMatthew Dillon 2654258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 2655258223a3SMatthew Dillon goto failcmd; 26561980eff3SMatthew Dillon #if 0 26571980eff3SMatthew Dillon kprintf("%s: started std command %b ccb %d ccb_at %p %d\n", 26581980eff3SMatthew Dillon ATANAME(ccb->ccb_port, ccb->ccb_xa.at), 26591980eff3SMatthew Dillon ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD, 26601980eff3SMatthew Dillon ccb->ccb_slot, 26611980eff3SMatthew Dillon ccb->ccb_xa.at, 26621980eff3SMatthew Dillon ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1); 26631980eff3SMatthew Dillon #endif 2664258223a3SMatthew Dillon 2665258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 2666258223a3SMatthew Dillon 2667258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 2668258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 26691980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 26701980eff3SMatthew Dillon cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target << 26711980eff3SMatthew Dillon AHCI_CMD_LIST_FLAG_PMP_SHIFT); 26721980eff3SMatthew Dillon } 2673258223a3SMatthew Dillon 2674258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 2675258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 2676258223a3SMatthew Dillon 2677258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 2678258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 2679258223a3SMatthew Dillon 2680258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 2681258223a3SMatthew Dillon goto failcmd; 2682258223a3SMatthew Dillon 2683258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 2684258223a3SMatthew Dillon 2685258223a3SMatthew Dillon if (xa->flags & ATA_F_POLL) { 2686258223a3SMatthew Dillon ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout); 2687258223a3SMatthew Dillon return (ATA_COMPLETE); 2688258223a3SMatthew Dillon } 2689258223a3SMatthew Dillon 2690258223a3SMatthew Dillon crit_enter(); 2691258223a3SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_RUNNING; 2692258223a3SMatthew Dillon callout_reset(&ccb->ccb_timeout, xa->timeout, 2693258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 2694258223a3SMatthew Dillon ahci_start(ccb); 2695258223a3SMatthew Dillon crit_exit(); 2696258223a3SMatthew Dillon return (ATA_QUEUED); 2697258223a3SMatthew Dillon 2698258223a3SMatthew Dillon failcmd: 2699258223a3SMatthew Dillon crit_enter(); 2700258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 2701258223a3SMatthew Dillon xa->complete(xa); 2702258223a3SMatthew Dillon crit_exit(); 2703258223a3SMatthew Dillon return (ATA_ERROR); 2704258223a3SMatthew Dillon } 2705258223a3SMatthew Dillon 2706258223a3SMatthew Dillon void 2707258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 2708258223a3SMatthew Dillon { 2709258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 2710258223a3SMatthew Dillon 2711258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 2712258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 2713258223a3SMatthew Dillon callout_stop(&ccb->ccb_timeout); 2714258223a3SMatthew Dillon } 2715258223a3SMatthew Dillon 2716258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) 2717258223a3SMatthew Dillon ahci_issue_pending_commands(ccb->ccb_port, 2718258223a3SMatthew Dillon xa->flags & ATA_F_NCQ); 2719258223a3SMatthew Dillon 2720258223a3SMatthew Dillon ahci_unload_prdt(ccb); 2721258223a3SMatthew Dillon 2722258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP) 2723258223a3SMatthew Dillon xa->state = ATA_S_COMPLETE; 2724258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2725258223a3SMatthew Dillon else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT) 2726258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, " 2727258223a3SMatthew Dillon "slot %d\n", 2728258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot); 2729258223a3SMatthew Dillon #endif 2730258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 2731258223a3SMatthew Dillon xa->complete(xa); 2732258223a3SMatthew Dillon } 2733258223a3SMatthew Dillon 2734258223a3SMatthew Dillon static void 2735258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 2736258223a3SMatthew Dillon { 2737258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 2738258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2739258223a3SMatthew Dillon 2740258223a3SMatthew Dillon lwkt_serialize_enter(&ap->ap_sc->sc_serializer); 2741258223a3SMatthew Dillon ahci_ata_cmd_timeout(arg); 2742258223a3SMatthew Dillon lwkt_serialize_exit(&ap->ap_sc->sc_serializer); 2743258223a3SMatthew Dillon } 2744258223a3SMatthew Dillon 27451980eff3SMatthew Dillon void 2746258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg) 2747258223a3SMatthew Dillon { 2748258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 2749258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 2750258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2751258223a3SMatthew Dillon volatile u_int32_t *active; 2752258223a3SMatthew Dillon int ccb_was_started, ncq_cmd; 2753258223a3SMatthew Dillon 2754258223a3SMatthew Dillon crit_enter(); 2755cf5f3a81SMatthew Dillon kprintf("%s: CMD TIMEOUT cmd-reg 0x%b\n" 2756cf5f3a81SMatthew Dillon "\tsactive=%08x active=%08x\n" 2757258223a3SMatthew Dillon "\t sact=%08x ci=%08x\n", 27581980eff3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 2759258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 2760cf5f3a81SMatthew Dillon ap->ap_sactive, ap->ap_active, 2761258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 2762258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI)); 2763258223a3SMatthew Dillon 27649e145b23SMatthew Dillon /* 27659e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 27669e145b23SMatthew Dillon */ 27679e145b23SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL|ATA_F_TIMEOUT_RUNNING)); 2768258223a3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 2769258223a3SMatthew Dillon ncq_cmd = (xa->flags & ATA_F_NCQ); 2770258223a3SMatthew Dillon active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active; 2771258223a3SMatthew Dillon 2772258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 2773258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out " 2774258223a3SMatthew Dillon "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot); 2775258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 2776258223a3SMatthew Dillon ccb_was_started = 0; 2777cf5f3a81SMatthew Dillon } else if (ccb->ccb_xa.state == ATA_S_ONCHIP && 2778cf5f3a81SMatthew Dillon ahci_port_intr(ap, 1 << ccb->ccb_slot)) { 2779258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: final poll of port completed " 2780258223a3SMatthew Dillon "command in slot %d\n", PORTNAME(ap), ccb->ccb_slot); 2781258223a3SMatthew Dillon goto ret; 2782258223a3SMatthew Dillon } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 2783258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already " 2784258223a3SMatthew Dillon "handled%s\n", PORTNAME(ap), ccb->ccb_slot, 2785258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot)) ? 2786258223a3SMatthew Dillon " but slot is still active?" : "."); 2787258223a3SMatthew Dillon goto ret; 2788258223a3SMatthew Dillon } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) & 2789258223a3SMatthew Dillon (1 << ccb->ccb_slot)) == 0 && 2790258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot))) { 2791258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but " 2792258223a3SMatthew Dillon "IRQ handler didn't detect it. Why?\n", PORTNAME(ap), 2793258223a3SMatthew Dillon ccb->ccb_slot); 2794258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 27951980eff3SMatthew Dillon if (ncq_cmd == 0) { 27961980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 27971980eff3SMatthew Dillon --ap->ap_active_cnt; 27981980eff3SMatthew Dillon } 2799258223a3SMatthew Dillon ccb->ccb_done(ccb); 2800258223a3SMatthew Dillon goto ret; 2801258223a3SMatthew Dillon } else { 2802258223a3SMatthew Dillon ccb_was_started = 1; 2803258223a3SMatthew Dillon } 2804258223a3SMatthew Dillon 2805258223a3SMatthew Dillon /* Complete the slot with a timeout error. */ 2806258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 2807258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 28081980eff3SMatthew Dillon if (ncq_cmd == 0) { 28091980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 28101980eff3SMatthew Dillon --ap->ap_active_cnt; 28111980eff3SMatthew Dillon } 2812258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap)); 2813258223a3SMatthew Dillon ccb->ccb_done(ccb); /* This won't issue pending commands or run the 2814258223a3SMatthew Dillon atascsi completion. */ 2815258223a3SMatthew Dillon 2816258223a3SMatthew Dillon /* Reset port to abort running command. */ 2817258223a3SMatthew Dillon if (ccb_was_started) { 2818258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command " 2819258223a3SMatthew Dillon "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" 2820258223a3SMatthew Dillon : "", ccb->ccb_slot, *active); 28211980eff3SMatthew Dillon /* XXX */ 28221980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 28231980eff3SMatthew Dillon /* XXX how do we unbrick a PM target? */ 28241980eff3SMatthew Dillon kprintf("%s: Unable to reset PM target during timeout" 28251980eff3SMatthew Dillon ", port bricked on us\n", 28261980eff3SMatthew Dillon PORTNAME(ap)); 28271980eff3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 28281980eff3SMatthew Dillon ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS); 28291980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) { 2830cf5f3a81SMatthew Dillon /* 2831cf5f3a81SMatthew Dillon * If the softreset failed place the port in a 2832cf5f3a81SMatthew Dillon * failed state and use ahci_port_intr() to cancel 2833cf5f3a81SMatthew Dillon * any remaining commands. 2834cf5f3a81SMatthew Dillon */ 283517eab71eSMatthew Dillon kprintf("%s: Unable to reset during timeout, port " 283617eab71eSMatthew Dillon "bricked on us\n", 2837258223a3SMatthew Dillon PORTNAME(ap)); 2838258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 2839cf5f3a81SMatthew Dillon ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS); 2840cf5f3a81SMatthew Dillon } else { 2841cf5f3a81SMatthew Dillon /* 2842cf5f3a81SMatthew Dillon * Restart any other commands that were aborted 2843cf5f3a81SMatthew Dillon * by the reset. 2844cf5f3a81SMatthew Dillon */ 2845258223a3SMatthew Dillon if (*active) { 2846258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots " 2847258223a3SMatthew Dillon "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "", 2848258223a3SMatthew Dillon *active); 2849258223a3SMatthew Dillon if (ncq_cmd) 2850258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, *active); 2851258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, *active); 2852258223a3SMatthew Dillon } 2853258223a3SMatthew Dillon } 2854cf5f3a81SMatthew Dillon } 2855258223a3SMatthew Dillon 2856258223a3SMatthew Dillon /* Issue any pending commands now. */ 2857258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap)); 2858258223a3SMatthew Dillon if (ccb_was_started) 2859258223a3SMatthew Dillon ahci_issue_pending_commands(ap, ncq_cmd); 2860258223a3SMatthew Dillon else if (ap->ap_active == 0) 2861258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 2862258223a3SMatthew Dillon 2863258223a3SMatthew Dillon /* Complete the timed out ata_xfer I/O (may generate new I/O). */ 2864258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap)); 2865258223a3SMatthew Dillon xa->complete(xa); 2866258223a3SMatthew Dillon 2867258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap)); 2868258223a3SMatthew Dillon ret: 2869258223a3SMatthew Dillon crit_exit(); 2870258223a3SMatthew Dillon } 2871258223a3SMatthew Dillon 2872258223a3SMatthew Dillon void 2873258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 2874258223a3SMatthew Dillon { 2875258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 2876258223a3SMatthew Dillon } 2877