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 54f4553de1SMatthew Dillon int ahci_port_start(struct ahci_port *ap); 55f4553de1SMatthew Dillon int ahci_port_stop(struct ahci_port *ap, int stop_fis_rx); 56f4553de1SMatthew Dillon int ahci_port_clo(struct ahci_port *ap); 57f4553de1SMatthew Dillon void ahci_port_interrupt_enable(struct ahci_port *ap); 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 70831bc9e3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *); 71831bc9e3SMatthew Dillon void ahci_quick_timeout(struct ahci_ccb *ccb); 72831bc9e3SMatthew Dillon void ahci_check_active_timeouts(struct ahci_port *ap); 73258223a3SMatthew Dillon 74831bc9e3SMatthew Dillon void ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at); 75831bc9e3SMatthew Dillon void ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at); 76258223a3SMatthew Dillon void ahci_issue_pending_ncq_commands(struct ahci_port *); 77258223a3SMatthew Dillon void ahci_issue_pending_commands(struct ahci_port *, int); 78258223a3SMatthew Dillon 79258223a3SMatthew Dillon struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *); 80258223a3SMatthew Dillon void ahci_put_err_ccb(struct ahci_ccb *); 81258223a3SMatthew Dillon 82258223a3SMatthew Dillon int ahci_port_read_ncq_error(struct ahci_port *, int *); 83258223a3SMatthew Dillon 84258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag); 85258223a3SMatthew Dillon void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *); 86258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error); 87258223a3SMatthew Dillon 88258223a3SMatthew Dillon void ahci_empty_done(struct ahci_ccb *ccb); 89258223a3SMatthew Dillon void ahci_ata_cmd_done(struct ahci_ccb *ccb); 90258223a3SMatthew Dillon 91258223a3SMatthew Dillon /* Wait for all bits in _b to be cleared */ 92cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \ 93cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0) 94cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to, _r, _b) \ 95cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), 0) 96258223a3SMatthew Dillon 97258223a3SMatthew Dillon /* Wait for all bits in _b to be set */ 98cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \ 99cec85a37SMatthew Dillon ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b)) 100cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \ 101cec85a37SMatthew Dillon ahci_pwait_eq((_ap), _to, (_r), (_b), (_b)) 102cec85a37SMatthew Dillon 103cec85a37SMatthew Dillon #define AHCI_PWAIT_TIMEOUT 1000 104258223a3SMatthew Dillon 105fd8bd957SMatthew Dillon /* 106fd8bd957SMatthew Dillon * Initialize the global AHCI hardware. This code does not set up any of 107fd8bd957SMatthew Dillon * its ports. 108fd8bd957SMatthew Dillon */ 109258223a3SMatthew Dillon int 110258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc) 111258223a3SMatthew Dillon { 112258223a3SMatthew Dillon u_int32_t cap, pi; 113831bc9e3SMatthew Dillon int i; 114831bc9e3SMatthew Dillon struct ahci_port *ap; 115258223a3SMatthew Dillon 116258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b", 117258223a3SMatthew Dillon ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC); 118258223a3SMatthew Dillon 119258223a3SMatthew Dillon /* save BIOS initialised parameters, enable staggered spin up */ 120258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 121258223a3SMatthew Dillon cap &= AHCI_REG_CAP_SMPS; 122258223a3SMatthew Dillon cap |= AHCI_REG_CAP_SSS; 123258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 124258223a3SMatthew Dillon 125831bc9e3SMatthew Dillon #if 1 126831bc9e3SMatthew Dillon /* 127831bc9e3SMatthew Dillon * This is a hack that currently does not appear to have 128831bc9e3SMatthew Dillon * a significant effect, but I noticed the port registers 129831bc9e3SMatthew Dillon * do not appear to be completely cleared after the host 130831bc9e3SMatthew Dillon * controller is reset. 131831bc9e3SMatthew Dillon */ 132831bc9e3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 133831bc9e3SMatthew Dillon ap->ap_sc = sc; 134831bc9e3SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 135831bc9e3SMatthew Dillon if ((pi & (1 << i)) == 0) 136831bc9e3SMatthew Dillon continue; 137831bc9e3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 138831bc9e3SMatthew Dillon AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 139831bc9e3SMatthew Dillon device_printf(sc->sc_dev, "can't map port\n"); 140831bc9e3SMatthew Dillon return (1); 141831bc9e3SMatthew Dillon } 142831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED | 143831bc9e3SMatthew Dillon AHCI_PREG_SCTL_DET_DISABLE); 144831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 145831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 146831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 147831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 0); 148831bc9e3SMatthew Dillon } 149831bc9e3SMatthew Dillon kfree(ap, M_DEVBUF); 150831bc9e3SMatthew Dillon #endif 151831bc9e3SMatthew Dillon 15217eab71eSMatthew Dillon /* 15317eab71eSMatthew Dillon * Unconditionally reset the controller, do not conditionalize on 15417eab71eSMatthew Dillon * trying to figure it if it was previously active or not. 155831bc9e3SMatthew Dillon * 156831bc9e3SMatthew Dillon * NOTE BRICKS (1) 157831bc9e3SMatthew Dillon * 158831bc9e3SMatthew Dillon * If you have a port multiplier and it does not have a device 159831bc9e3SMatthew Dillon * in target 0, and it probes normally, but a later operation 160831bc9e3SMatthew Dillon * mis-probes a target behind that PM, it is possible for the 161831bc9e3SMatthew Dillon * port to brick such that only (a) a power cycle of the host 162831bc9e3SMatthew Dillon * or (b) placing a device in target 0 will fix the problem. 163831bc9e3SMatthew Dillon * Power cycling the PM has no effect (it works fine on another 164831bc9e3SMatthew Dillon * host port). This issue is unrelated to CLO. 16517eab71eSMatthew Dillon */ 166258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR); 167831bc9e3SMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, 168831bc9e3SMatthew Dillon AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) { 169258223a3SMatthew Dillon device_printf(sc->sc_dev, 170258223a3SMatthew Dillon "unable to reset controller\n"); 171258223a3SMatthew Dillon return (1); 172258223a3SMatthew Dillon } 173831bc9e3SMatthew Dillon ahci_os_sleep(100); 174258223a3SMatthew Dillon 175258223a3SMatthew Dillon /* enable ahci (global interrupts disabled) */ 176258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 177258223a3SMatthew Dillon 178258223a3SMatthew Dillon /* restore parameters */ 179258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_CAP, cap); 180258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_PI, pi); 181258223a3SMatthew Dillon 182258223a3SMatthew Dillon return (0); 183258223a3SMatthew Dillon } 184258223a3SMatthew Dillon 185fd8bd957SMatthew Dillon /* 186fd8bd957SMatthew Dillon * Allocate and initialize an AHCI port. 187fd8bd957SMatthew Dillon */ 188258223a3SMatthew Dillon int 189258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port) 190258223a3SMatthew Dillon { 191258223a3SMatthew Dillon struct ahci_port *ap; 1921980eff3SMatthew Dillon struct ata_port *at; 193258223a3SMatthew Dillon struct ahci_ccb *ccb; 194258223a3SMatthew Dillon u_int64_t dva; 195258223a3SMatthew Dillon u_int32_t cmd; 196258223a3SMatthew Dillon struct ahci_cmd_hdr *hdr; 197258223a3SMatthew Dillon struct ahci_cmd_table *table; 198258223a3SMatthew Dillon int rc = ENOMEM; 199258223a3SMatthew Dillon int error; 200258223a3SMatthew Dillon int i; 201258223a3SMatthew Dillon 202258223a3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 203258223a3SMatthew Dillon 204258223a3SMatthew Dillon ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d", 205258223a3SMatthew Dillon device_get_name(sc->sc_dev), 206258223a3SMatthew Dillon device_get_unit(sc->sc_dev), 207258223a3SMatthew Dillon port); 208258223a3SMatthew Dillon sc->sc_ports[port] = ap; 209258223a3SMatthew Dillon 2101980eff3SMatthew Dillon /* 2111980eff3SMatthew Dillon * Allocate enough so we never have to reallocate, it makes 2121980eff3SMatthew Dillon * it easier. 2131980eff3SMatthew Dillon * 2141980eff3SMatthew Dillon * ap_pmcount will be reduced by the scan if we encounter the 2151980eff3SMatthew Dillon * port multiplier port prior to target 15. 2161980eff3SMatthew Dillon */ 2171980eff3SMatthew Dillon if (ap->ap_ata == NULL) { 2181980eff3SMatthew Dillon ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * AHCI_MAX_PMPORTS, 2191980eff3SMatthew Dillon M_DEVBUF, M_INTWAIT | M_ZERO); 2201980eff3SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 2211980eff3SMatthew Dillon at = &ap->ap_ata[i]; 2221980eff3SMatthew Dillon at->at_ahci_port = ap; 2231980eff3SMatthew Dillon at->at_target = i; 2243209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 225831bc9e3SMatthew Dillon at->at_features |= ATA_PORT_F_RESCAN; 2261980eff3SMatthew Dillon ksnprintf(at->at_name, sizeof(at->at_name), 2271980eff3SMatthew Dillon "%s.%d", ap->ap_name, i); 2281980eff3SMatthew Dillon } 2291980eff3SMatthew Dillon } 230258223a3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 231258223a3SMatthew Dillon AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 232258223a3SMatthew Dillon device_printf(sc->sc_dev, 233258223a3SMatthew Dillon "unable to create register window for port %d\n", 234258223a3SMatthew Dillon port); 235258223a3SMatthew Dillon goto freeport; 236258223a3SMatthew Dillon } 237258223a3SMatthew Dillon 238258223a3SMatthew Dillon ap->ap_sc = sc; 239258223a3SMatthew Dillon ap->ap_num = port; 2403209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 241258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_free); 242258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_pending); 243258223a3SMatthew Dillon lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0); 244258223a3SMatthew Dillon 245258223a3SMatthew Dillon /* Disable port interrupts */ 246258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 247831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 248258223a3SMatthew Dillon 24917eab71eSMatthew Dillon /* 25017eab71eSMatthew Dillon * Sec 10.1.2 - deinitialise port if it is already running 25117eab71eSMatthew Dillon */ 252258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 253258223a3SMatthew Dillon if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR | 254258223a3SMatthew Dillon AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) || 255258223a3SMatthew Dillon (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) { 256258223a3SMatthew Dillon int r; 257258223a3SMatthew Dillon 258258223a3SMatthew Dillon r = ahci_port_stop(ap, 1); 259258223a3SMatthew Dillon if (r) { 260258223a3SMatthew Dillon device_printf(sc->sc_dev, 261258223a3SMatthew Dillon "unable to disable %s, ignoring port %d\n", 262258223a3SMatthew Dillon ((r == 2) ? "CR" : "FR"), port); 263258223a3SMatthew Dillon rc = ENXIO; 264258223a3SMatthew Dillon goto freeport; 265258223a3SMatthew Dillon } 266258223a3SMatthew Dillon 267258223a3SMatthew Dillon /* Write DET to zero */ 268cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 269258223a3SMatthew Dillon } 270258223a3SMatthew Dillon 271258223a3SMatthew Dillon /* Allocate RFIS */ 272258223a3SMatthew Dillon ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis); 273258223a3SMatthew Dillon if (ap->ap_dmamem_rfis == NULL) { 274cf5f3a81SMatthew Dillon kprintf("%s: NORFIS\n", PORTNAME(ap)); 275258223a3SMatthew Dillon goto nomem; 276258223a3SMatthew Dillon } 277258223a3SMatthew Dillon 278258223a3SMatthew Dillon /* Setup RFIS base address */ 279258223a3SMatthew Dillon ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis); 280258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis); 281258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32)); 282258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva); 283258223a3SMatthew Dillon 284831bc9e3SMatthew Dillon /* Clear SERR before starting FIS reception or ST or anything */ 285831bc9e3SMatthew Dillon ahci_flush_tfd(ap); 286831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 287831bc9e3SMatthew Dillon 288258223a3SMatthew Dillon /* Enable FIS reception and activate port. */ 289258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 2901980eff3SMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 291258223a3SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 292258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 293258223a3SMatthew Dillon 294258223a3SMatthew Dillon /* Check whether port activated. Skip it if not. */ 295258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 296258223a3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 297cf5f3a81SMatthew Dillon kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap)); 298258223a3SMatthew Dillon rc = ENXIO; 299258223a3SMatthew Dillon goto freeport; 300258223a3SMatthew Dillon } 301258223a3SMatthew Dillon 302258223a3SMatthew Dillon /* Allocate a CCB for each command slot */ 303258223a3SMatthew Dillon ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF, 304258223a3SMatthew Dillon M_WAITOK | M_ZERO); 305258223a3SMatthew Dillon if (ap->ap_ccbs == NULL) { 306258223a3SMatthew Dillon device_printf(sc->sc_dev, 307258223a3SMatthew Dillon "unable to allocate command list for port %d\n", 308258223a3SMatthew Dillon port); 309258223a3SMatthew Dillon goto freeport; 310258223a3SMatthew Dillon } 311258223a3SMatthew Dillon 312258223a3SMatthew Dillon /* Command List Structures and Command Tables */ 313258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh); 314258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt); 315258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table == NULL || 316258223a3SMatthew Dillon ap->ap_dmamem_cmd_list == NULL) { 317258223a3SMatthew Dillon nomem: 318258223a3SMatthew Dillon device_printf(sc->sc_dev, 319258223a3SMatthew Dillon "unable to allocate DMA memory for port %d\n", 320258223a3SMatthew Dillon port); 321258223a3SMatthew Dillon goto freeport; 322258223a3SMatthew Dillon } 323258223a3SMatthew Dillon 324258223a3SMatthew Dillon /* Setup command list base address */ 325258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list); 326258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32)); 327258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva); 328258223a3SMatthew Dillon 329258223a3SMatthew Dillon /* Split CCB allocation into CCBs and assign to command header/table */ 330258223a3SMatthew Dillon hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list); 331258223a3SMatthew Dillon table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table); 332258223a3SMatthew Dillon for (i = 0; i < sc->sc_ncmds; i++) { 333258223a3SMatthew Dillon ccb = &ap->ap_ccbs[i]; 334258223a3SMatthew Dillon 335258223a3SMatthew Dillon error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW, 336258223a3SMatthew Dillon &ccb->ccb_dmamap); 337258223a3SMatthew Dillon if (error) { 338258223a3SMatthew Dillon device_printf(sc->sc_dev, 339258223a3SMatthew Dillon "unable to create dmamap for port %d " 340258223a3SMatthew Dillon "ccb %d\n", port, i); 341258223a3SMatthew Dillon goto freeport; 342258223a3SMatthew Dillon } 343258223a3SMatthew Dillon 344258223a3SMatthew Dillon callout_init(&ccb->ccb_timeout); 345258223a3SMatthew Dillon ccb->ccb_slot = i; 346258223a3SMatthew Dillon ccb->ccb_port = ap; 347258223a3SMatthew Dillon ccb->ccb_cmd_hdr = &hdr[i]; 348258223a3SMatthew Dillon ccb->ccb_cmd_table = &table[i]; 349258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) + 350258223a3SMatthew Dillon ccb->ccb_slot * sizeof(struct ahci_cmd_table); 351258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32)); 352258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva); 353258223a3SMatthew Dillon 354258223a3SMatthew Dillon ccb->ccb_xa.fis = 355258223a3SMatthew Dillon (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 356258223a3SMatthew Dillon ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd; 357258223a3SMatthew Dillon ccb->ccb_xa.tag = i; 358258223a3SMatthew Dillon 359258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 360258223a3SMatthew Dillon ahci_put_ccb(ccb); 361258223a3SMatthew Dillon } 362258223a3SMatthew Dillon 363258223a3SMatthew Dillon /* Wait for ICC change to complete */ 364258223a3SMatthew Dillon ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC); 365258223a3SMatthew Dillon 366fd8bd957SMatthew Dillon /* 367f4553de1SMatthew Dillon * Start the port. The helper thread will call ahci_port_init() 368f4553de1SMatthew Dillon * so the ports can all be started in parallel. A failure by 369f4553de1SMatthew Dillon * ahci_port_init() does not deallocate the port since we still 370f4553de1SMatthew Dillon * want hot-plug events. 371fd8bd957SMatthew Dillon */ 372f4553de1SMatthew Dillon ahci_os_start_port(ap); 373fd8bd957SMatthew Dillon return(0); 374fd8bd957SMatthew Dillon freeport: 375fd8bd957SMatthew Dillon ahci_port_free(sc, port); 376fd8bd957SMatthew Dillon return (rc); 377fd8bd957SMatthew Dillon } 378fd8bd957SMatthew Dillon 379fd8bd957SMatthew Dillon /* 380fd8bd957SMatthew Dillon * [re]initialize an idle port. No CCBs should be active. 381fd8bd957SMatthew Dillon * 3821980eff3SMatthew Dillon * If at is NULL we are initializing a directly connected port, otherwise 3831980eff3SMatthew Dillon * we are indirectly initializing a port multiplier port. 3841980eff3SMatthew Dillon * 385fd8bd957SMatthew Dillon * This function is called during the initial port allocation sequence 386fd8bd957SMatthew Dillon * and is also called on hot-plug insertion. We take no chances and 387fd8bd957SMatthew Dillon * use a portreset instead of a softreset. 388fd8bd957SMatthew Dillon * 38922181ab7SMatthew Dillon * This function is the only way to move a failed port back to active 39022181ab7SMatthew Dillon * status. 39122181ab7SMatthew Dillon * 392fd8bd957SMatthew Dillon * Returns 0 if a device is successfully detected. 393fd8bd957SMatthew Dillon */ 394fd8bd957SMatthew Dillon int 395f4553de1SMatthew Dillon ahci_port_init(struct ahci_port *ap, struct ata_port *atx) 396fd8bd957SMatthew Dillon { 397*121d8e75SMatthew Dillon u_int32_t data; 398fd8bd957SMatthew Dillon int rc; 399fd8bd957SMatthew Dillon 400fd8bd957SMatthew Dillon /* 4011980eff3SMatthew Dillon * Clear all notification bits 402fd8bd957SMatthew Dillon */ 403*121d8e75SMatthew Dillon if (atx == NULL && (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)) 4041980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 4051980eff3SMatthew Dillon 4061980eff3SMatthew Dillon /* 4071980eff3SMatthew Dillon * Hard-reset the port. If a device is detected but it is busy 4081980eff3SMatthew Dillon * we try a second time, this time cycling the phy as well. 4091980eff3SMatthew Dillon */ 410f4553de1SMatthew Dillon if (atx) 411f4553de1SMatthew Dillon atx->at_probe = ATA_PROBE_NEED_HARD_RESET; 412f4553de1SMatthew Dillon else 4131980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_HARD_RESET; 414831bc9e3SMatthew Dillon rc = ahci_port_reset(ap, atx, 2); 415831bc9e3SMatthew Dillon #if 0 416f4553de1SMatthew Dillon rc = ahci_port_reset(ap, atx, 1); 4171980eff3SMatthew Dillon if (rc == EBUSY) { 418f4553de1SMatthew Dillon rc = ahci_port_reset(ap, atx, 2); 41917eab71eSMatthew Dillon } 420831bc9e3SMatthew Dillon #endif 421fd8bd957SMatthew Dillon 422258223a3SMatthew Dillon switch (rc) { 423258223a3SMatthew Dillon case ENODEV: 424fd8bd957SMatthew Dillon /* 425fd8bd957SMatthew Dillon * We had problems talking to the device on the port. 426fd8bd957SMatthew Dillon */ 427*121d8e75SMatthew Dillon if (atx) { 428*121d8e75SMatthew Dillon ahci_pm_read(ap, atx->at_target, 429*121d8e75SMatthew Dillon AHCI_PMREG_SSTS, &data); 430*121d8e75SMatthew Dillon } else { 431*121d8e75SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SSTS); 432*121d8e75SMatthew Dillon } 433*121d8e75SMatthew Dillon 434*121d8e75SMatthew Dillon switch(data & AHCI_PREG_SSTS_DET) { 435258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 436*121d8e75SMatthew Dillon kprintf("%s: Device not communicating\n", 437*121d8e75SMatthew Dillon ATANAME(ap, atx)); 438258223a3SMatthew Dillon break; 439258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_PHYOFFLINE: 440*121d8e75SMatthew Dillon kprintf("%s: PHY offline\n", 441*121d8e75SMatthew Dillon ATANAME(ap, atx)); 442258223a3SMatthew Dillon break; 443258223a3SMatthew Dillon default: 444*121d8e75SMatthew Dillon kprintf("%s: No device detected\n", 445*121d8e75SMatthew Dillon ATANAME(ap, atx)); 446258223a3SMatthew Dillon break; 447258223a3SMatthew Dillon } 448258223a3SMatthew Dillon break; 449258223a3SMatthew Dillon 450258223a3SMatthew Dillon case EBUSY: 451fd8bd957SMatthew Dillon /* 45217eab71eSMatthew Dillon * The device on the port is still telling us its busy, 45317eab71eSMatthew Dillon * which means that it is not properly handling a SATA 45417eab71eSMatthew Dillon * port COMRESET. 455fd8bd957SMatthew Dillon * 45617eab71eSMatthew Dillon * It may be possible to softreset the device using CLO 45717eab71eSMatthew Dillon * and a device reset command. 458fd8bd957SMatthew Dillon */ 459*121d8e75SMatthew Dillon if (atx) { 460*121d8e75SMatthew Dillon kprintf("%s: Device on port is bricked, giving up\n", 461*121d8e75SMatthew Dillon ATANAME(ap, atx)); 462*121d8e75SMatthew Dillon } else { 463*121d8e75SMatthew Dillon kprintf("%s: Device on port is bricked, " 464*121d8e75SMatthew Dillon "trying softreset\n", PORTNAME(ap)); 465258223a3SMatthew Dillon 466f4553de1SMatthew Dillon rc = ahci_port_reset(ap, atx, 0); 467258223a3SMatthew Dillon if (rc) { 46817eab71eSMatthew Dillon kprintf("%s: Unable unbrick device\n", 469fd8bd957SMatthew Dillon PORTNAME(ap)); 470fd8bd957SMatthew Dillon } else { 47117eab71eSMatthew Dillon kprintf("%s: Successfully unbricked\n", 472fd8bd957SMatthew Dillon PORTNAME(ap)); 473258223a3SMatthew Dillon } 474*121d8e75SMatthew Dillon } 475258223a3SMatthew Dillon break; 476258223a3SMatthew Dillon 477258223a3SMatthew Dillon default: 478258223a3SMatthew Dillon break; 479258223a3SMatthew Dillon } 480258223a3SMatthew Dillon 481258223a3SMatthew Dillon /* 48217eab71eSMatthew Dillon * Command transfers can only be enabled if a device was successfully 48317eab71eSMatthew Dillon * detected. 4841980eff3SMatthew Dillon * 4851980eff3SMatthew Dillon * Allocate or deallocate the ap_ata array here too. 486258223a3SMatthew Dillon */ 487*121d8e75SMatthew Dillon if (atx == NULL) { 4881980eff3SMatthew Dillon switch(ap->ap_type) { 4891980eff3SMatthew Dillon case ATA_PORT_T_NONE: 4901980eff3SMatthew Dillon ap->ap_pmcount = 0; 4911980eff3SMatthew Dillon break; 4921980eff3SMatthew Dillon case ATA_PORT_T_PM: 4931980eff3SMatthew Dillon /* already set */ 4941980eff3SMatthew Dillon break; 4951980eff3SMatthew Dillon default: 4961980eff3SMatthew Dillon ap->ap_pmcount = 1; 4971980eff3SMatthew Dillon break; 4981980eff3SMatthew Dillon } 499*121d8e75SMatthew Dillon } 5001980eff3SMatthew Dillon 5011980eff3SMatthew Dillon /* 5021980eff3SMatthew Dillon * Start the port if we succeeded. 5031980eff3SMatthew Dillon * 5041980eff3SMatthew Dillon * There's nothing to start for devices behind a port multiplier. 5051980eff3SMatthew Dillon */ 506f4553de1SMatthew Dillon if (rc == 0 && atx == NULL) { 50717eab71eSMatthew Dillon if (ahci_port_start(ap)) { 508fd8bd957SMatthew Dillon kprintf("%s: failed to start command DMA on port, " 509fd8bd957SMatthew Dillon "disabling\n", PORTNAME(ap)); 510258223a3SMatthew Dillon rc = ENXIO; /* couldn't start port */ 511258223a3SMatthew Dillon } 512258223a3SMatthew Dillon } 513258223a3SMatthew Dillon 51417eab71eSMatthew Dillon /* 5153209f581SMatthew Dillon * Flush interrupts on the port. XXX 5161980eff3SMatthew Dillon * 5171980eff3SMatthew Dillon * Enable interrupts on the port whether a device is sitting on 5181980eff3SMatthew Dillon * it or not, to handle hot-plug events. 51917eab71eSMatthew Dillon */ 520f4553de1SMatthew Dillon if (atx == NULL) { 521258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 522fd8bd957SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num); 523258223a3SMatthew Dillon 524f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 525f4553de1SMatthew Dillon } 526f4553de1SMatthew Dillon return(rc); 527f4553de1SMatthew Dillon } 528f4553de1SMatthew Dillon 529f4553de1SMatthew Dillon /* 530f4553de1SMatthew Dillon * Enable or re-enable interrupts on a port. 531f4553de1SMatthew Dillon * 532f4553de1SMatthew Dillon * This routine is called from the port initialization code or from the 533f4553de1SMatthew Dillon * helper thread as the real interrupt may be forced to turn off certain 534f4553de1SMatthew Dillon * interrupt sources. 535f4553de1SMatthew Dillon */ 536f4553de1SMatthew Dillon void 537f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap) 538f4553de1SMatthew Dillon { 539f4553de1SMatthew Dillon u_int32_t data; 540f4553de1SMatthew Dillon 5411980eff3SMatthew Dillon data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE | 542258223a3SMatthew Dillon AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE | 543258223a3SMatthew Dillon AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE | 544258223a3SMatthew Dillon AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE | 5451980eff3SMatthew Dillon AHCI_PREG_IE_DHRE; 5461980eff3SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 5471980eff3SMatthew Dillon data |= AHCI_PREG_IE_SDBE; 548258223a3SMatthew Dillon #ifdef AHCI_COALESCE 5491980eff3SMatthew Dillon if (sc->sc_ccc_ports & (1 << port) 5501980eff3SMatthew Dillon data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE); 551258223a3SMatthew Dillon #endif 5521980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, data); 5531980eff3SMatthew Dillon } 554258223a3SMatthew Dillon 555fd8bd957SMatthew Dillon /* 5563209f581SMatthew Dillon * Run the port / target state machine from a main context. 5573209f581SMatthew Dillon * 5583209f581SMatthew Dillon * The state machine for the port is always run. 5593209f581SMatthew Dillon * 5603209f581SMatthew Dillon * If atx is non-NULL run the state machine for a particular target. 5613209f581SMatthew Dillon * If atx is NULL run the state machine for all targets. 5623209f581SMatthew Dillon */ 5633209f581SMatthew Dillon void 564831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial) 5653209f581SMatthew Dillon { 5663209f581SMatthew Dillon struct ata_port *at; 5673209f581SMatthew Dillon u_int32_t data; 5683209f581SMatthew Dillon int target; 5693209f581SMatthew Dillon int didsleep; 570831bc9e3SMatthew Dillon int loop; 5713209f581SMatthew Dillon 572831bc9e3SMatthew Dillon /* 573831bc9e3SMatthew Dillon * State machine for port. Note that CAM is not yet associated 574831bc9e3SMatthew Dillon * during the initial parallel probe and the port's probe state 575831bc9e3SMatthew Dillon * will not get past ATA_PROBE_NEED_IDENT. 576831bc9e3SMatthew Dillon */ 5773209f581SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 5783209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT) { 5793209f581SMatthew Dillon for (target = 0; target < AHCI_MAX_PMPORTS; ++target) { 5803209f581SMatthew Dillon at = &ap->ap_ata[target]; 5813209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 582831bc9e3SMatthew Dillon at->at_features |= ATA_PORT_F_RESCAN; 5833209f581SMatthew Dillon } 5843209f581SMatthew Dillon ahci_port_init(ap, NULL); 5853209f581SMatthew Dillon } 5863209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) 5873209f581SMatthew Dillon ahci_port_reset(ap, NULL, 1); 5883209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET) 5893209f581SMatthew Dillon ahci_port_reset(ap, NULL, 0); 5903209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 5913209f581SMatthew Dillon ahci_cam_probe(ap, NULL); 5923209f581SMatthew Dillon } 5933209f581SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM) { 5943209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 5953209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 0); 596f4553de1SMatthew Dillon } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) { 5973209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 1); 5983209f581SMatthew Dillon } 5993209f581SMatthew Dillon return; 6003209f581SMatthew Dillon } 6013209f581SMatthew Dillon 602831bc9e3SMatthew Dillon /* 603831bc9e3SMatthew Dillon * Port Multiplier state machine. 604831bc9e3SMatthew Dillon * 605831bc9e3SMatthew Dillon * Get a mask of changed targets and combine with any runnable 606831bc9e3SMatthew Dillon * states already present. 607831bc9e3SMatthew Dillon */ 608831bc9e3SMatthew Dillon for (loop = 0; ;++loop) { 6093209f581SMatthew Dillon if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) { 6103209f581SMatthew Dillon kprintf("%s: PM unable to read hot-plug bitmap\n", 6113209f581SMatthew Dillon PORTNAME(ap)); 6123209f581SMatthew Dillon break; 6133209f581SMatthew Dillon } 6143209f581SMatthew Dillon data &= (1 << ap->ap_pmcount) - 1; 6153209f581SMatthew Dillon 6163209f581SMatthew Dillon /* 617831bc9e3SMatthew Dillon * Do at least one loop, then stop if no more state changes 618831bc9e3SMatthew Dillon * have occured. The PM might not generate a new 619831bc9e3SMatthew Dillon * notification until we clear the entire bitmap. 6203209f581SMatthew Dillon */ 621831bc9e3SMatthew Dillon if (loop && data == 0) 6223209f581SMatthew Dillon break; 6233209f581SMatthew Dillon 6243209f581SMatthew Dillon /* 6253209f581SMatthew Dillon * New devices showing up in the bitmap require some spin-up 6263209f581SMatthew Dillon * time before we start probing them. Reset didsleep. The 6273209f581SMatthew Dillon * first new device we detect will sleep before probing. 628831bc9e3SMatthew Dillon * 629831bc9e3SMatthew Dillon * This only applies to devices whos change bit is set in 630831bc9e3SMatthew Dillon * the data, and does not apply to the initial boot-time 631831bc9e3SMatthew Dillon * probe. 6323209f581SMatthew Dillon */ 6333209f581SMatthew Dillon didsleep = 0; 6343209f581SMatthew Dillon 6353209f581SMatthew Dillon for (target = 0; target < ap->ap_pmcount; ++target) { 6363209f581SMatthew Dillon at = &ap->ap_ata[target]; 6373209f581SMatthew Dillon 6383209f581SMatthew Dillon /* 6393209f581SMatthew Dillon * Check the target state for targets behind the PM 6403209f581SMatthew Dillon * which have changed state. This will adjust 6413209f581SMatthew Dillon * at_probe and set ATA_PORT_F_RESCAN 6423209f581SMatthew Dillon * 6433209f581SMatthew Dillon * We want to wait at least 4 seconds before probing 6443209f581SMatthew Dillon * a newly inserted device. If the check status 6453209f581SMatthew Dillon * indicates a device is present and in need of a 6463209f581SMatthew Dillon * hard reset, we make sure we have slept before 6473209f581SMatthew Dillon * continuing. 648831bc9e3SMatthew Dillon * 649831bc9e3SMatthew Dillon * NOTE: When pm_check_good finds a good port it 650831bc9e3SMatthew Dillon * typically starts us in probe state 651831bc9e3SMatthew Dillon * NEED_HARD_RESET rather than INIT. 6523209f581SMatthew Dillon */ 6533209f581SMatthew Dillon if (data & (1 << target)) { 6543209f581SMatthew Dillon ahci_pm_check_good(ap, target); 655831bc9e3SMatthew Dillon if (initial == 0 && didsleep == 0 && 656831bc9e3SMatthew Dillon at->at_probe <= ATA_PROBE_NEED_HARD_RESET 657831bc9e3SMatthew Dillon ) { 6583209f581SMatthew Dillon didsleep = 1; 659*121d8e75SMatthew Dillon kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap)); 660*121d8e75SMatthew Dillon ahci_os_sleep(10000); 6613209f581SMatthew Dillon } 6623209f581SMatthew Dillon } 663831bc9e3SMatthew Dillon 664831bc9e3SMatthew Dillon /* 665831bc9e3SMatthew Dillon * Report hot-plug events before the probe state 666831bc9e3SMatthew Dillon * really gets hot. Only actual events are reported 667831bc9e3SMatthew Dillon * here to reduce spew. 668831bc9e3SMatthew Dillon */ 669831bc9e3SMatthew Dillon if (data & (1 << target)) { 670831bc9e3SMatthew Dillon kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at)); 671831bc9e3SMatthew Dillon switch(at->at_probe) { 672831bc9e3SMatthew Dillon case ATA_PROBE_NEED_INIT: 673831bc9e3SMatthew Dillon case ATA_PROBE_NEED_HARD_RESET: 674831bc9e3SMatthew Dillon kprintf("Device inserted\n"); 675831bc9e3SMatthew Dillon break; 676831bc9e3SMatthew Dillon case ATA_PROBE_FAILED: 677831bc9e3SMatthew Dillon kprintf("Device removed\n"); 678831bc9e3SMatthew Dillon break; 679831bc9e3SMatthew Dillon default: 680831bc9e3SMatthew Dillon kprintf("Device probe in progress\n"); 681831bc9e3SMatthew Dillon break; 682831bc9e3SMatthew Dillon } 6833209f581SMatthew Dillon } 6843209f581SMatthew Dillon 6853209f581SMatthew Dillon /* 686831bc9e3SMatthew Dillon * Run through the state machine as necessary if 687831bc9e3SMatthew Dillon * the port is not marked failed. 688831bc9e3SMatthew Dillon * 689831bc9e3SMatthew Dillon * The state machine may stop at NEED_IDENT if 690831bc9e3SMatthew Dillon * CAM is not yet attached. 691831bc9e3SMatthew Dillon * 692831bc9e3SMatthew Dillon * Acquire exclusive access to the port while we 693831bc9e3SMatthew Dillon * are doing this. This prevents command-completion 694831bc9e3SMatthew Dillon * from queueing commands for non-polled targets 695831bc9e3SMatthew Dillon * inbetween our probe steps. We need to do this 696831bc9e3SMatthew Dillon * because the reset probes can generate severe PHY 697831bc9e3SMatthew Dillon * and protocol errors and soft-brick the port. 6983209f581SMatthew Dillon */ 699831bc9e3SMatthew Dillon if (at->at_probe != ATA_PROBE_FAILED && 700831bc9e3SMatthew Dillon at->at_probe != ATA_PROBE_GOOD) { 701831bc9e3SMatthew Dillon ahci_beg_exclusive_access(ap, at); 7023209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_INIT) 7033209f581SMatthew Dillon ahci_port_init(ap, at); 7043209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) 7053209f581SMatthew Dillon ahci_port_reset(ap, at, 1); 7063209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET) 7073209f581SMatthew Dillon ahci_port_reset(ap, at, 0); 7083209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_IDENT) 7093209f581SMatthew Dillon ahci_cam_probe(ap, at); 710831bc9e3SMatthew Dillon ahci_end_exclusive_access(ap, at); 7113209f581SMatthew Dillon } 7123209f581SMatthew Dillon 7133209f581SMatthew Dillon /* 714831bc9e3SMatthew Dillon * Add or remove from CAM 7153209f581SMatthew Dillon */ 7163209f581SMatthew Dillon if (at->at_features & ATA_PORT_F_RESCAN) { 7173209f581SMatthew Dillon at->at_features &= ~ATA_PORT_F_RESCAN; 7183209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_FAILED) { 7193209f581SMatthew Dillon ahci_cam_changed(ap, at, 0); 720f4553de1SMatthew Dillon } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) { 7213209f581SMatthew Dillon ahci_cam_changed(ap, at, 1); 7223209f581SMatthew Dillon } 7233209f581SMatthew Dillon } 7243209f581SMatthew Dillon } 7253209f581SMatthew Dillon } 7263209f581SMatthew Dillon } 7273209f581SMatthew Dillon 7283209f581SMatthew Dillon 7293209f581SMatthew Dillon /* 730fd8bd957SMatthew Dillon * De-initialize and detach a port. 731fd8bd957SMatthew Dillon */ 732258223a3SMatthew Dillon void 733258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 734258223a3SMatthew Dillon { 735258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 736258223a3SMatthew Dillon struct ahci_ccb *ccb; 737258223a3SMatthew Dillon 73817eab71eSMatthew Dillon /* 73917eab71eSMatthew Dillon * Ensure port is disabled and its interrupts are all flushed. 74017eab71eSMatthew Dillon */ 741258223a3SMatthew Dillon if (ap->ap_sc) { 74217eab71eSMatthew Dillon ahci_port_stop(ap, 1); 743f4553de1SMatthew Dillon ahci_os_stop_port(ap); 744258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 745258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 746258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 747258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 748258223a3SMatthew Dillon } 749258223a3SMatthew Dillon 750258223a3SMatthew Dillon if (ap->ap_ccbs) { 751258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 752258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 753258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 754258223a3SMatthew Dillon ccb->ccb_dmamap); 755258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 756258223a3SMatthew Dillon } 757258223a3SMatthew Dillon } 758258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 759258223a3SMatthew Dillon ap->ap_ccbs = NULL; 760258223a3SMatthew Dillon } 761258223a3SMatthew Dillon 762258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 763258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 764258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 765258223a3SMatthew Dillon } 766258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 767258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 768258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 769258223a3SMatthew Dillon } 770258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 771258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 772258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 773258223a3SMatthew Dillon } 7741980eff3SMatthew Dillon if (ap->ap_ata) { 7751980eff3SMatthew Dillon kfree(ap->ap_ata, M_DEVBUF); 7761980eff3SMatthew Dillon ap->ap_ata = NULL; 7771980eff3SMatthew Dillon } 778258223a3SMatthew Dillon 779258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 780258223a3SMatthew Dillon 781258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 782258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 783258223a3SMatthew Dillon } 784258223a3SMatthew Dillon 785fd8bd957SMatthew Dillon /* 786fd8bd957SMatthew Dillon * Start high-level command processing on the port 787fd8bd957SMatthew Dillon */ 788258223a3SMatthew Dillon int 78917eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap) 790258223a3SMatthew Dillon { 7918bf6a3ffSMatthew Dillon u_int32_t r, oldr, s, olds, is, oldis, tfd, oldtfd; 792258223a3SMatthew Dillon 79317eab71eSMatthew Dillon /* 79417eab71eSMatthew Dillon * FRE must be turned on before ST. Wait for FR to go active 79517eab71eSMatthew Dillon * before turning on ST. The spec doesn't seem to think this 79617eab71eSMatthew Dillon * is necessary but waiting here avoids an on-off race in the 79717eab71eSMatthew Dillon * ahci_port_stop() code. 79817eab71eSMatthew Dillon */ 799cec07d75SMatthew Dillon /* XXX REMOVE ME */ 8001980eff3SMatthew Dillon olds = ahci_pread(ap, AHCI_PREG_SERR); 8011980eff3SMatthew Dillon oldis= ahci_pread(ap, AHCI_PREG_IS); 8028bf6a3ffSMatthew Dillon oldtfd = ahci_pread(ap, AHCI_PREG_TFD); 8031980eff3SMatthew Dillon oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 80417eab71eSMatthew Dillon if ((r & AHCI_PREG_CMD_FRE) == 0) { 805258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 80617eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 80717eab71eSMatthew Dillon } 80817eab71eSMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) { 80917eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 81017eab71eSMatthew Dillon kprintf("%s: Cannot start FIS reception\n", 81117eab71eSMatthew Dillon PORTNAME(ap)); 81217eab71eSMatthew Dillon return (2); 81317eab71eSMatthew Dillon } 81417eab71eSMatthew Dillon } 81517eab71eSMatthew Dillon 81617eab71eSMatthew Dillon /* 81717eab71eSMatthew Dillon * Turn on ST, wait for CR to come up. 81817eab71eSMatthew Dillon */ 819258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 820258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 82117eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 8228bf6a3ffSMatthew Dillon s = ahci_pread(ap, AHCI_PREG_SERR); 8238bf6a3ffSMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 8248bf6a3ffSMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 8251980eff3SMatthew Dillon kprintf("%s: Cannot start command DMA\n" 8261980eff3SMatthew Dillon "OCMD=%b OSERR=%b\n" 8271980eff3SMatthew Dillon "NCMP=%b NSERR=%b\n" 8288bf6a3ffSMatthew Dillon "OLDIS=%b\nNEWIS=%b\n" 8298bf6a3ffSMatthew Dillon "OLDTFD=%b\nNEWTFD=%b\n", 8301980eff3SMatthew Dillon PORTNAME(ap), 8311980eff3SMatthew Dillon oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR, 8321980eff3SMatthew Dillon r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR, 8338bf6a3ffSMatthew Dillon oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS, 8348bf6a3ffSMatthew Dillon oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS); 83517eab71eSMatthew Dillon return (1); 83617eab71eSMatthew Dillon } 837258223a3SMatthew Dillon 838258223a3SMatthew Dillon #ifdef AHCI_COALESCE 83917eab71eSMatthew Dillon /* 84017eab71eSMatthew Dillon * (Re-)enable coalescing on the port. 84117eab71eSMatthew Dillon */ 842258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 843258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 844258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 845258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 846258223a3SMatthew Dillon } 847258223a3SMatthew Dillon #endif 848258223a3SMatthew Dillon 849258223a3SMatthew Dillon return (0); 850258223a3SMatthew Dillon } 851258223a3SMatthew Dillon 852fd8bd957SMatthew Dillon /* 853fd8bd957SMatthew Dillon * Stop high-level command processing on a port 854fd8bd957SMatthew Dillon */ 855258223a3SMatthew Dillon int 856258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 857258223a3SMatthew Dillon { 858258223a3SMatthew Dillon u_int32_t r; 859258223a3SMatthew Dillon 860258223a3SMatthew Dillon #ifdef AHCI_COALESCE 86117eab71eSMatthew Dillon /* 86217eab71eSMatthew Dillon * Disable coalescing on the port while it is stopped. 86317eab71eSMatthew Dillon */ 864258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 865258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 866258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 867258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 868258223a3SMatthew Dillon } 869258223a3SMatthew Dillon #endif 870258223a3SMatthew Dillon 87117eab71eSMatthew Dillon /* 87217eab71eSMatthew Dillon * Turn off ST, then wait for CR to go off. 87317eab71eSMatthew Dillon */ 874258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 875258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 876258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 877258223a3SMatthew Dillon 87817eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 87917eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (ST)\n", 88017eab71eSMatthew Dillon PORTNAME(ap)); 881258223a3SMatthew Dillon return (1); 88217eab71eSMatthew Dillon } 883258223a3SMatthew Dillon 8841980eff3SMatthew Dillon #if 0 88517eab71eSMatthew Dillon /* 88617eab71eSMatthew Dillon * Turn off FRE, then wait for FR to go off. FRE cannot 88717eab71eSMatthew Dillon * be turned off until CR transitions to 0. 88817eab71eSMatthew Dillon */ 8891980eff3SMatthew Dillon if ((r & AHCI_PREG_CMD_FR) == 0) { 8901980eff3SMatthew Dillon kprintf("%s: FR stopped, clear FRE for next start\n", 8911980eff3SMatthew Dillon PORTNAME(ap)); 8921980eff3SMatthew Dillon stop_fis_rx = 2; 8931980eff3SMatthew Dillon } 8941980eff3SMatthew Dillon #endif 89517eab71eSMatthew Dillon if (stop_fis_rx) { 89617eab71eSMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 89717eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 89817eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 89917eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (FRE)\n", 90017eab71eSMatthew Dillon PORTNAME(ap)); 901258223a3SMatthew Dillon return (2); 90217eab71eSMatthew Dillon } 90317eab71eSMatthew Dillon } 904258223a3SMatthew Dillon 905258223a3SMatthew Dillon return (0); 906258223a3SMatthew Dillon } 907258223a3SMatthew Dillon 908fd8bd957SMatthew Dillon /* 909fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 910fd8bd957SMatthew Dillon */ 911258223a3SMatthew Dillon int 912258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 913258223a3SMatthew Dillon { 914258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 915258223a3SMatthew Dillon u_int32_t cmd; 916258223a3SMatthew Dillon 917258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 918258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 919258223a3SMatthew Dillon return (1); 920258223a3SMatthew Dillon 921258223a3SMatthew Dillon /* Issue CLO */ 922258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 923258223a3SMatthew Dillon #ifdef DIAGNOSTIC 924258223a3SMatthew Dillon if (cmd & AHCI_PREG_CMD_ST) { 925258223a3SMatthew Dillon kprintf("%s: CLO requested while port running\n", 926258223a3SMatthew Dillon PORTNAME(ap)); 927258223a3SMatthew Dillon } 928258223a3SMatthew Dillon #endif 929258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 930258223a3SMatthew Dillon 931258223a3SMatthew Dillon /* Wait for completion */ 932258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 933258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 934258223a3SMatthew Dillon return (1); 935258223a3SMatthew Dillon } 936258223a3SMatthew Dillon 937258223a3SMatthew Dillon return (0); 938258223a3SMatthew Dillon } 939258223a3SMatthew Dillon 940fd8bd957SMatthew Dillon /* 9411980eff3SMatthew Dillon * Reset a port. 94217eab71eSMatthew Dillon * 9431980eff3SMatthew Dillon * If hard is 0 perform a softreset of the port. 94417eab71eSMatthew Dillon * If hard is 1 perform a hard reset of the port. 9451980eff3SMatthew Dillon * If hard is 2 perform a hard reset of the port and cycle the phy. 9461980eff3SMatthew Dillon * 9471980eff3SMatthew Dillon * If at is non-NULL an indirect port via a port-multiplier is being 9481980eff3SMatthew Dillon * reset, otherwise a direct port is being reset. 9491980eff3SMatthew Dillon * 9501980eff3SMatthew Dillon * NOTE: Indirect ports can only be soft-reset. 95117eab71eSMatthew Dillon */ 95217eab71eSMatthew Dillon int 9531980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard) 95417eab71eSMatthew Dillon { 95517eab71eSMatthew Dillon int rc; 95617eab71eSMatthew Dillon 95717eab71eSMatthew Dillon if (hard) { 9581980eff3SMatthew Dillon if (at) 9591980eff3SMatthew Dillon rc = ahci_pm_hardreset(ap, at->at_target, hard); 9601980eff3SMatthew Dillon else 9611980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 96217eab71eSMatthew Dillon } else { 9631980eff3SMatthew Dillon if (at) 9641980eff3SMatthew Dillon rc = ahci_pm_softreset(ap, at->at_target); 9651980eff3SMatthew Dillon else 96617eab71eSMatthew Dillon rc = ahci_port_softreset(ap); 96717eab71eSMatthew Dillon } 96817eab71eSMatthew Dillon return(rc); 96917eab71eSMatthew Dillon } 97017eab71eSMatthew Dillon 97117eab71eSMatthew Dillon /* 972fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 973fd8bd957SMatthew Dillon * 9741980eff3SMatthew Dillon * (at) will be NULL when soft-resetting a directly-attached device, and 9751980eff3SMatthew Dillon * non-NULL when soft-resetting a device through a port multiplier. 9761980eff3SMatthew Dillon * 977fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 9781980eff3SMatthew Dillon * a reset to the connected device using device commands. 979fd8bd957SMatthew Dillon */ 980258223a3SMatthew Dillon int 981258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 982258223a3SMatthew Dillon { 983258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 984258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 985258223a3SMatthew Dillon u_int8_t *fis; 9863209f581SMatthew Dillon int error; 987258223a3SMatthew Dillon u_int32_t cmd; 988258223a3SMatthew Dillon 9893209f581SMatthew Dillon error = EIO; 9901980eff3SMatthew Dillon 9911980eff3SMatthew Dillon kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap), 9921980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD); 9931980eff3SMatthew Dillon 994258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 995258223a3SMatthew Dillon 996258223a3SMatthew Dillon crit_enter(); 9971980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 9981980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 999258223a3SMatthew Dillon 10001980eff3SMatthew Dillon /* 10011980eff3SMatthew Dillon * Remember port state in cmd (main to restore start/stop) 10021980eff3SMatthew Dillon * 10031980eff3SMatthew Dillon * Idle port. 10041980eff3SMatthew Dillon */ 10051980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1006258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 1007258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 1008258223a3SMatthew Dillon PORTNAME(ap)); 1009258223a3SMatthew Dillon goto err; 1010258223a3SMatthew Dillon } 1011cf5f3a81SMatthew Dillon 1012cf5f3a81SMatthew Dillon /* 10131980eff3SMatthew Dillon * Request CLO if device appears hung. 1014cf5f3a81SMatthew Dillon */ 1015258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 1016258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1017258223a3SMatthew Dillon ahci_port_clo(ap); 1018258223a3SMatthew Dillon } 1019258223a3SMatthew Dillon 10201980eff3SMatthew Dillon /* 10211980eff3SMatthew Dillon * This is an attempt to clear errors so a new signature will 10221980eff3SMatthew Dillon * be latched. It isn't working properly. XXX 10231980eff3SMatthew Dillon */ 1024cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 10251980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1026258223a3SMatthew Dillon 1027258223a3SMatthew Dillon /* Restart port */ 102817eab71eSMatthew Dillon if (ahci_port_start(ap)) { 1029258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 1030258223a3SMatthew Dillon PORTNAME(ap)); 1031258223a3SMatthew Dillon goto err; 1032258223a3SMatthew Dillon } 1033258223a3SMatthew Dillon 1034258223a3SMatthew Dillon /* Check whether CLO worked */ 1035258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 1036258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1037258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 1038258223a3SMatthew Dillon PORTNAME(ap), 1039258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 1040258223a3SMatthew Dillon ? "failed" : "unsupported"); 10413209f581SMatthew Dillon error = EBUSY; 1042258223a3SMatthew Dillon goto err; 1043258223a3SMatthew Dillon } 1044258223a3SMatthew Dillon 1045cec85a37SMatthew Dillon /* 1046cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 1047cec85a37SMatthew Dillon * 1048cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1049cec85a37SMatthew Dillon * everything. 1050cec85a37SMatthew Dillon */ 1051258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 10521980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 1053258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 1054258223a3SMatthew Dillon 1055258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 1056cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 10571980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 10581980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 1059258223a3SMatthew Dillon 1060258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1061258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1062258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 1063258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 1064258223a3SMatthew Dillon 1065258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 10665f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 1067831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 10685f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 1069258223a3SMatthew Dillon goto err; 1070cec85a37SMatthew Dillon } 1071258223a3SMatthew Dillon 1072cec85a37SMatthew Dillon /* 1073831bc9e3SMatthew Dillon * WARNING! TIME SENSITIVE SPACE! WARNING! 1074831bc9e3SMatthew Dillon * 1075831bc9e3SMatthew Dillon * The two FISes are supposed to be back to back. Don't issue other 1076831bc9e3SMatthew Dillon * commands or even delay if we can help it. 10771980eff3SMatthew Dillon */ 10781980eff3SMatthew Dillon 10791980eff3SMatthew Dillon /* 1080cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 1081cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 1082cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 1083cec85a37SMatthew Dillon * from the AHCI document. 1084cec85a37SMatthew Dillon * 1085b089d0bfSMatthew Dillon * Give the device 3ms before sending the second FIS. 1086cec85a37SMatthew Dillon * 1087cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1088cec85a37SMatthew Dillon * everything. 1089cec85a37SMatthew Dillon */ 1090cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 10911980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 10921980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 1093258223a3SMatthew Dillon 1094258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1095258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1096258223a3SMatthew Dillon 1097258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 10985f8c1efdSMatthew Dillon ccb->ccb_xa.flags = 0; 1099831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 11005f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 1101258223a3SMatthew Dillon goto err; 1102cec85a37SMatthew Dillon } 1103258223a3SMatthew Dillon 11041980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 11051980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1106258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 1107258223a3SMatthew Dillon PORTNAME(ap), 1108258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 11093209f581SMatthew Dillon error = EBUSY; 1110258223a3SMatthew Dillon goto err; 1111258223a3SMatthew Dillon } 11123209f581SMatthew Dillon ahci_os_sleep(10); 1113258223a3SMatthew Dillon 1114fd8bd957SMatthew Dillon /* 1115fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 1116fd8bd957SMatthew Dillon * normal portreset we assign the port type. 1117fd8bd957SMatthew Dillon * 1118fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 1119fd8bd957SMatthew Dillon * processing code then report if the device signature changed 1120fd8bd957SMatthew Dillon * unexpectedly. 1121fd8bd957SMatthew Dillon */ 11221980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 11231980eff3SMatthew Dillon ap->ap_type = ahci_port_signature_detect(ap, NULL); 1124fd8bd957SMatthew Dillon } else { 11251980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) { 11261980eff3SMatthew Dillon kprintf("%s: device signature unexpectedly " 11271980eff3SMatthew Dillon "changed\n", PORTNAME(ap)); 11283209f581SMatthew Dillon error = EBUSY; /* XXX */ 1129fd8bd957SMatthew Dillon } 1130fd8bd957SMatthew Dillon } 11313209f581SMatthew Dillon error = 0; 11321980eff3SMatthew Dillon 11333209f581SMatthew Dillon ahci_os_sleep(3); 1134258223a3SMatthew Dillon err: 1135258223a3SMatthew Dillon if (ccb != NULL) { 1136258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 11371980eff3SMatthew Dillon 11381980eff3SMatthew Dillon /* 11391980eff3SMatthew Dillon * If the target is busy use CLO to clear the busy 11401980eff3SMatthew Dillon * condition. The BSY should be cleared on the next 11411980eff3SMatthew Dillon * start. 11421980eff3SMatthew Dillon */ 11431980eff3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 11441980eff3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 11451980eff3SMatthew Dillon ahci_port_clo(ap); 11461980eff3SMatthew Dillon } 1147258223a3SMatthew Dillon } 1148258223a3SMatthew Dillon 1149cf5f3a81SMatthew Dillon /* 1150cf5f3a81SMatthew Dillon * If we failed to softreset make the port quiescent, otherwise 1151cf5f3a81SMatthew Dillon * make sure the port's start/stop state matches what it was on 1152cf5f3a81SMatthew Dillon * entry. 11531980eff3SMatthew Dillon * 11541980eff3SMatthew Dillon * Don't kill the port if the softreset is on a port multiplier 11551980eff3SMatthew Dillon * target, that would kill all the targets! 1156cf5f3a81SMatthew Dillon */ 11573209f581SMatthew Dillon if (error) { 1158cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 11593209f581SMatthew Dillon /* ap_probe set to failed */ 1160cf5f3a81SMatthew Dillon } else if (cmd & AHCI_PREG_CMD_ST) { 11613209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 11621980eff3SMatthew Dillon kprintf("%s: STARTING PORT\n", PORTNAME(ap)); 1163cf5f3a81SMatthew Dillon ahci_port_start(ap); 1164cf5f3a81SMatthew Dillon } else { 11653209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 11661980eff3SMatthew Dillon kprintf("%s: STOPPING PORT\n", PORTNAME(ap)); 1167cf5f3a81SMatthew Dillon ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE)); 1168cf5f3a81SMatthew Dillon } 11693209f581SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 1170258223a3SMatthew Dillon crit_exit(); 1171258223a3SMatthew Dillon 11721980eff3SMatthew Dillon kprintf("%s: END SOFTRESET\n", PORTNAME(ap)); 11731980eff3SMatthew Dillon 11743209f581SMatthew Dillon return (error); 1175258223a3SMatthew Dillon } 1176258223a3SMatthew Dillon 1177fd8bd957SMatthew Dillon /* 1178fd8bd957SMatthew Dillon * AHCI port reset, Section 10.4.2 1179fd8bd957SMatthew Dillon * 1180fd8bd957SMatthew Dillon * This function does a hard reset of the port. Note that the device 1181fd8bd957SMatthew Dillon * connected to the port could still end-up hung. 1182fd8bd957SMatthew Dillon */ 1183258223a3SMatthew Dillon int 11841980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard) 1185258223a3SMatthew Dillon { 1186258223a3SMatthew Dillon u_int32_t cmd, r; 11873209f581SMatthew Dillon int error; 11881980eff3SMatthew Dillon int loop; 11891980eff3SMatthew Dillon int type; 1190258223a3SMatthew Dillon 1191258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap)); 1192258223a3SMatthew Dillon 11931980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 1194cf5f3a81SMatthew Dillon 1195cf5f3a81SMatthew Dillon /* 11961980eff3SMatthew Dillon * Idle the port, 11971980eff3SMatthew Dillon */ 11981980eff3SMatthew Dillon ahci_port_stop(ap, 0); 11991980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 12003209f581SMatthew Dillon error = 0; 12011980eff3SMatthew Dillon 12021980eff3SMatthew Dillon /* 12031980eff3SMatthew Dillon * The port may have been quiescent with its SUD bit cleared, so 12041980eff3SMatthew Dillon * set the SUD (spin up device). 1205cf5f3a81SMatthew Dillon */ 1206cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1207cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1208cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1209258223a3SMatthew Dillon 12101980eff3SMatthew Dillon /* 12111980eff3SMatthew Dillon * Perform device detection. Cycle the PHY off, wait 10ms. 12121980eff3SMatthew Dillon * This simulates the SATA cable being physically unplugged. 12131980eff3SMatthew Dillon */ 12141980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1215258223a3SMatthew Dillon 12161980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED; 12171980eff3SMatthew Dillon if (hard == 2) 12181980eff3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_DISABLE; 12191980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 12203209f581SMatthew Dillon ahci_os_sleep(10); 12211980eff3SMatthew Dillon 12221980eff3SMatthew Dillon /* 12231980eff3SMatthew Dillon * Start transmitting COMRESET. COMRESET must be sent for at 12241980eff3SMatthew Dillon * least 1ms. 12251980eff3SMatthew Dillon */ 12261980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1227258223a3SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1228258223a3SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1229258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1230258223a3SMatthew Dillon } else { 1231258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1232258223a3SMatthew Dillon } 1233258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1234831bc9e3SMatthew Dillon 1235831bc9e3SMatthew Dillon /* 1236831bc9e3SMatthew Dillon * Through trial and error it seems to take around 100ms 1237831bc9e3SMatthew Dillon * for the detect logic to settle down. If this is too 1238831bc9e3SMatthew Dillon * short the softreset code will fail. 1239831bc9e3SMatthew Dillon */ 1240831bc9e3SMatthew Dillon ahci_os_sleep(100); 1241cf5f3a81SMatthew Dillon 1242cf5f3a81SMatthew Dillon /* 1243cf5f3a81SMatthew Dillon * Only SERR_DIAG_X needs to be cleared for TFD updates, but 1244cf5f3a81SMatthew Dillon * since we are hard-resetting the port we might as well clear 1245cf5f3a81SMatthew Dillon * the whole enchillada 1246cf5f3a81SMatthew Dillon */ 1247cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1248cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1249258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 1250258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 1251258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1252258223a3SMatthew Dillon 12531980eff3SMatthew Dillon /* 12541980eff3SMatthew Dillon * Try to determine if there is a device on the port. 12551980eff3SMatthew Dillon * 12561980eff3SMatthew Dillon * Give the device 3/10 second to at least be detected. 12571980eff3SMatthew Dillon * If we fail clear PRCS (phy detect) since we may cycled 12581980eff3SMatthew Dillon * the phy and probably caused another PRCS interrupt. 12591980eff3SMatthew Dillon */ 12601980eff3SMatthew Dillon for (loop = 30; loop; --loop) { 12611980eff3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 12621980eff3SMatthew Dillon if (r & AHCI_PREG_SSTS_DET) 12631980eff3SMatthew Dillon break; 12643209f581SMatthew Dillon ahci_os_sleep(10); 12651980eff3SMatthew Dillon } 12661980eff3SMatthew Dillon if (loop == 0) { 12671980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 12681980eff3SMatthew Dillon kprintf("%s: Port appears to be unplugged\n", 12691980eff3SMatthew Dillon PORTNAME(ap)); 12703209f581SMatthew Dillon error = ENODEV; 1271258223a3SMatthew Dillon } 1272258223a3SMatthew Dillon 1273cec85a37SMatthew Dillon /* 12741980eff3SMatthew Dillon * There is something on the port. Give the device 3 seconds 12751980eff3SMatthew Dillon * to fully negotiate. 12761980eff3SMatthew Dillon */ 12773209f581SMatthew Dillon if (error == 0 && 12781980eff3SMatthew Dillon ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS, 12791980eff3SMatthew Dillon AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) { 12801980eff3SMatthew Dillon kprintf("%s: Device may be powered down\n", 12811980eff3SMatthew Dillon PORTNAME(ap)); 12823209f581SMatthew Dillon error = ENODEV; 12831980eff3SMatthew Dillon } 12841980eff3SMatthew Dillon 12851980eff3SMatthew Dillon /* 12861980eff3SMatthew Dillon * Wait for the device to become ready. 1287cec85a37SMatthew Dillon * 1288b089d0bfSMatthew Dillon * This can take more then a second, give it 3 seconds. If we 1289b089d0bfSMatthew Dillon * succeed give the device another 3ms after that. 12901980eff3SMatthew Dillon * 12913209f581SMatthew Dillon * NOTE: Port multipliers can do two things here. First they can 12921980eff3SMatthew Dillon * return device-ready if a device is on target 0 and also 12931980eff3SMatthew Dillon * return the signature for that device. If there is no 12941980eff3SMatthew Dillon * device on target 0 then BSY/DRQ is never cleared and 12951980eff3SMatthew Dillon * it never comes ready. 1296cec85a37SMatthew Dillon */ 12973209f581SMatthew Dillon if (error == 0 && 12981980eff3SMatthew Dillon ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD, 12991980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 13001980eff3SMatthew Dillon /* 13011980eff3SMatthew Dillon * The device is bricked or its a port multiplier and will 13021980eff3SMatthew Dillon * not unbusy until we do the pmprobe CLO softreset sequence. 13031980eff3SMatthew Dillon */ 13043209f581SMatthew Dillon error = ahci_port_pmprobe(ap); 13053209f581SMatthew Dillon if (error) { 1306258223a3SMatthew Dillon kprintf("%s: Device will not come ready 0x%b\n", 1307258223a3SMatthew Dillon PORTNAME(ap), 13081980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), 13091980eff3SMatthew Dillon AHCI_PFMT_TFD_STS); 13101980eff3SMatthew Dillon } else { 13111980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 1312258223a3SMatthew Dillon } 13133209f581SMatthew Dillon } else if (error == 0) { 13141980eff3SMatthew Dillon /* 13151980eff3SMatthew Dillon * We generally will not get a port multiplier signature in 13161980eff3SMatthew Dillon * this case even if this is a port multiplier, because of 13171980eff3SMatthew Dillon * Intel's stupidity. We almost certainly got target 0 13181980eff3SMatthew Dillon * behind the PM, if there is a PM. 13191980eff3SMatthew Dillon * 13201980eff3SMatthew Dillon * Save the signature and probe for a PM. If we do not 13211980eff3SMatthew Dillon * find a PM then use the saved signature and return 13221980eff3SMatthew Dillon * success. 13231980eff3SMatthew Dillon */ 13241980eff3SMatthew Dillon type = ahci_port_signature_detect(ap, NULL); 13253209f581SMatthew Dillon error = ahci_port_pmprobe(ap); 13263209f581SMatthew Dillon if (error) { 13271980eff3SMatthew Dillon ap->ap_type = type; 13283209f581SMatthew Dillon error = 0; 13291980eff3SMatthew Dillon } else { 13301980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_PM; 13313209f581SMatthew Dillon kprintf("%s: Port multiplier detected\n", 13321980eff3SMatthew Dillon PORTNAME(ap)); 13331980eff3SMatthew Dillon } 13341980eff3SMatthew Dillon } 1335258223a3SMatthew Dillon 1336cf5f3a81SMatthew Dillon /* 13371980eff3SMatthew Dillon * hard-stop the port if we failed. This will set ap_probe 13381980eff3SMatthew Dillon * to FAILED. 1339cf5f3a81SMatthew Dillon */ 13401980eff3SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 13413209f581SMatthew Dillon if (error) { 13423209f581SMatthew Dillon ahci_port_hardstop(ap); 13433209f581SMatthew Dillon /* ap_probe set to failed */ 13443209f581SMatthew Dillon } else { 1345f4553de1SMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 1346f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_GOOD; 1347f4553de1SMatthew Dillon else 1348f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 13493209f581SMatthew Dillon } 13503209f581SMatthew Dillon return (error); 1351258223a3SMatthew Dillon } 1352258223a3SMatthew Dillon 1353fd8bd957SMatthew Dillon /* 13541980eff3SMatthew Dillon * AHCI port multiplier probe. This routine is run by the hardreset code 13551980eff3SMatthew Dillon * if it gets past the device detect, whether or not BSY is found to be 13561980eff3SMatthew Dillon * stuck. 13571980eff3SMatthew Dillon * 13581980eff3SMatthew Dillon * We MUST use CLO to properly probe whether the port multiplier exists 13591980eff3SMatthew Dillon * or not. 13601980eff3SMatthew Dillon * 13611980eff3SMatthew Dillon * Return 0 on success, non-zero on failure. 13621980eff3SMatthew Dillon */ 13631980eff3SMatthew Dillon int 13641980eff3SMatthew Dillon ahci_port_pmprobe(struct ahci_port *ap) 13651980eff3SMatthew Dillon { 13661980eff3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 13671980eff3SMatthew Dillon struct ahci_ccb *ccb = NULL; 13681980eff3SMatthew Dillon u_int8_t *fis = NULL; 1369831bc9e3SMatthew Dillon int error = EIO; 13701980eff3SMatthew Dillon u_int32_t cmd; 13711980eff3SMatthew Dillon int count; 13721980eff3SMatthew Dillon 13731980eff3SMatthew Dillon /* 13741980eff3SMatthew Dillon * If we don't support port multipliers don't try to detect one. 13751980eff3SMatthew Dillon */ 13761980eff3SMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0) 13771980eff3SMatthew Dillon return (ENODEV); 13781980eff3SMatthew Dillon 13791980eff3SMatthew Dillon count = 2; 1380831bc9e3SMatthew Dillon #if 1 13811980eff3SMatthew Dillon kprintf("%s: START PMPROBE\n", PORTNAME(ap)); 13821980eff3SMatthew Dillon #endif 13831980eff3SMatthew Dillon retry: 13841980eff3SMatthew Dillon /* 13851980eff3SMatthew Dillon * This code is only called from hardreset, which does not 13861980eff3SMatthew Dillon * high level command processing. The port should be stopped. 13871980eff3SMatthew Dillon * 13881980eff3SMatthew Dillon * Set PMA mode while the port is stopped. 13891980eff3SMatthew Dillon * 13901980eff3SMatthew Dillon * NOTE: On retry the port might be running, stopped, or failed. 13911980eff3SMatthew Dillon */ 13921980eff3SMatthew Dillon ahci_port_stop(ap, 0); 13931980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 13941980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1395831bc9e3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_PMA) == 0) { 13961980eff3SMatthew Dillon cmd |= AHCI_PREG_CMD_PMA; 13971980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1398831bc9e3SMatthew Dillon } 13991980eff3SMatthew Dillon 14001980eff3SMatthew Dillon /* 14011980eff3SMatthew Dillon * Flush any errors and request CLO unconditionally, then start 14021980eff3SMatthew Dillon * the port. 14031980eff3SMatthew Dillon */ 14041980eff3SMatthew Dillon ahci_flush_tfd(ap); 14051980eff3SMatthew Dillon ahci_port_clo(ap); 14061980eff3SMatthew Dillon if (ahci_port_start(ap)) { 14071980eff3SMatthew Dillon kprintf("%s: PMPROBE failed to start port, cannot softreset\n", 14081980eff3SMatthew Dillon PORTNAME(ap)); 14091980eff3SMatthew Dillon goto err; 14101980eff3SMatthew Dillon } 14111980eff3SMatthew Dillon 14121980eff3SMatthew Dillon /* 14131980eff3SMatthew Dillon * Check whether CLO worked 14141980eff3SMatthew Dillon */ 14151980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 14161980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 14171980eff3SMatthew Dillon kprintf("%s: PMPROBE CLO %s, need port reset\n", 14181980eff3SMatthew Dillon PORTNAME(ap), 14191980eff3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 14201980eff3SMatthew Dillon ? "failed" : "unsupported"); 1421831bc9e3SMatthew Dillon error = EBUSY; 14221980eff3SMatthew Dillon goto err; 14231980eff3SMatthew Dillon } 14241980eff3SMatthew Dillon 14251980eff3SMatthew Dillon /* 14261980eff3SMatthew Dillon * Prep the first H2D command with SRST feature & clear busy/reset 14271980eff3SMatthew Dillon * flags. 14281980eff3SMatthew Dillon */ 14291980eff3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 14301980eff3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 14311980eff3SMatthew Dillon 14321980eff3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 14331980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 14341980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 14351980eff3SMatthew Dillon fis[1] = 0x0F; /* Target 15 */ 14361980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT; 14371980eff3SMatthew Dillon 14381980eff3SMatthew Dillon cmd_slot->prdtl = 0; 14391980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 14401980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 14411980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 14421980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 14431980eff3SMatthew Dillon 14441980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 14451980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 14461980eff3SMatthew Dillon 1447831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 14481980eff3SMatthew Dillon kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap)); 14491980eff3SMatthew Dillon if (--count) { 14501980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 14511980eff3SMatthew Dillon goto retry; 14521980eff3SMatthew Dillon } 14531980eff3SMatthew Dillon goto err; 14541980eff3SMatthew Dillon } 14551980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 14561980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 14571980eff3SMatthew Dillon kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap)); 14581980eff3SMatthew Dillon } 14591980eff3SMatthew Dillon 14601980eff3SMatthew Dillon /* 14611980eff3SMatthew Dillon * The device may have muffed up the PHY when it reset. 14621980eff3SMatthew Dillon */ 1463831bc9e3SMatthew Dillon ahci_os_sleep(100); 14641980eff3SMatthew Dillon ahci_flush_tfd(ap); 14651980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 14661980eff3SMatthew Dillon /* ahci_pm_phy_status(ap, 15, &cmd); */ 14671980eff3SMatthew Dillon 14681980eff3SMatthew Dillon /* 14691980eff3SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 14701980eff3SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 14711980eff3SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 14721980eff3SMatthew Dillon * from the AHCI document. 14731980eff3SMatthew Dillon * 14741980eff3SMatthew Dillon * Give the device 3ms before sending the second FIS. 14751980eff3SMatthew Dillon * 14761980eff3SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 14771980eff3SMatthew Dillon * everything. 14781980eff3SMatthew Dillon */ 14791980eff3SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 14801980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 14811980eff3SMatthew Dillon fis[1] = 0x0F; 14821980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 14831980eff3SMatthew Dillon 14841980eff3SMatthew Dillon cmd_slot->prdtl = 0; 14851980eff3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 14861980eff3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */ 14871980eff3SMatthew Dillon 14881980eff3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 14891980eff3SMatthew Dillon ccb->ccb_xa.flags = 0; 14901980eff3SMatthew Dillon 1491831bc9e3SMatthew Dillon if (ahci_poll(ccb, 5000, ahci_quick_timeout) != ATA_S_COMPLETE) { 14921980eff3SMatthew Dillon kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap)); 14931980eff3SMatthew Dillon if (--count) { 14941980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 14951980eff3SMatthew Dillon goto retry; 14961980eff3SMatthew Dillon } 14971980eff3SMatthew Dillon goto err; 14981980eff3SMatthew Dillon } 14991980eff3SMatthew Dillon 15001980eff3SMatthew Dillon /* 15011980eff3SMatthew Dillon * What? We succeeded? Yup, but for some reason the signature 15021980eff3SMatthew Dillon * is still latched from the original detect (that saw target 0 15031980eff3SMatthew Dillon * behind the PM), and I don't know how to clear the condition 15041980eff3SMatthew Dillon * other then by retrying the whole reset sequence. 15051980eff3SMatthew Dillon */ 15061980eff3SMatthew Dillon if (--count) { 15071980eff3SMatthew Dillon fis[15] = 0; 15081980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 15091980eff3SMatthew Dillon goto retry; 15101980eff3SMatthew Dillon } 15111980eff3SMatthew Dillon 15121980eff3SMatthew Dillon /* 15131980eff3SMatthew Dillon * Get the signature. The caller sets the ap fields. 15141980eff3SMatthew Dillon */ 15151980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) { 15161980eff3SMatthew Dillon ap->ap_ata[15].at_probe = ATA_PROBE_GOOD; 1517831bc9e3SMatthew Dillon error = 0; 15181980eff3SMatthew Dillon } else { 1519831bc9e3SMatthew Dillon error = EBUSY; 15201980eff3SMatthew Dillon } 15211980eff3SMatthew Dillon 15221980eff3SMatthew Dillon /* 15231980eff3SMatthew Dillon * Fall through / clean up the CCB and perform error processing. 15241980eff3SMatthew Dillon */ 15251980eff3SMatthew Dillon err: 1526831bc9e3SMatthew Dillon if (ccb != NULL) 15271980eff3SMatthew Dillon ahci_put_err_ccb(ccb); 15281980eff3SMatthew Dillon 1529831bc9e3SMatthew Dillon if (error == 0 && ahci_pm_identify(ap)) { 15303209f581SMatthew Dillon kprintf("%s: PM - cannot identify port multiplier\n", 15313209f581SMatthew Dillon PORTNAME(ap)); 1532831bc9e3SMatthew Dillon error = EBUSY; 15333209f581SMatthew Dillon } 15343209f581SMatthew Dillon #if 0 1535831bc9e3SMatthew Dillon if (error == 0 && ahci_pm_set_feature(ap, ATA_SATAFT_ASYNCNOTIFY, 1)) { 15363209f581SMatthew Dillon kprintf("%s: PM - Warning, cannot enable async notify\n", 15373209f581SMatthew Dillon PORTNAME(ap)); 15383209f581SMatthew Dillon /* ignore error */ 15393209f581SMatthew Dillon } 1540831bc9e3SMatthew Dillon if (error == 0) { 15413209f581SMatthew Dillon u_int32_t data; 15423209f581SMatthew Dillon if (ahci_pm_read(ap, 2, 4, &data)) 15433209f581SMatthew Dillon kprintf("Cannot read snotify\n"); 15443209f581SMatthew Dillon else 15453209f581SMatthew Dillon kprintf("Read snotify %08x\n", data); 15463209f581SMatthew Dillon } 15473209f581SMatthew Dillon #endif 15483209f581SMatthew Dillon 15491980eff3SMatthew Dillon /* 15501980eff3SMatthew Dillon * If we failed turn off PMA, otherwise identify the port multiplier. 15511980eff3SMatthew Dillon * CAM will iterate the devices. 15521980eff3SMatthew Dillon */ 1553831bc9e3SMatthew Dillon if (error) { 15541980eff3SMatthew Dillon ahci_port_stop(ap, 0); 15551980eff3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 15561980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 15571980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 15581980eff3SMatthew Dillon } 15591980eff3SMatthew Dillon ahci_port_stop(ap, 0); 15601980eff3SMatthew Dillon 1561831bc9e3SMatthew Dillon return(error); 15621980eff3SMatthew Dillon } 15631980eff3SMatthew Dillon 15641980eff3SMatthew Dillon 15651980eff3SMatthew Dillon /* 1566cf5f3a81SMatthew Dillon * Hard-stop on hot-swap device removal. See 10.10.1 1567cf5f3a81SMatthew Dillon * 1568cf5f3a81SMatthew Dillon * Place the port in a mode that will allow it to detect hot-swap insertions. 1569cf5f3a81SMatthew Dillon * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't 1570cf5f3a81SMatthew Dillon * seem to do the job. 1571cf5f3a81SMatthew Dillon */ 1572cf5f3a81SMatthew Dillon void 1573cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap) 1574cf5f3a81SMatthew Dillon { 15751980eff3SMatthew Dillon struct ata_port *at; 1576cf5f3a81SMatthew Dillon u_int32_t r; 1577cf5f3a81SMatthew Dillon u_int32_t cmd; 15781980eff3SMatthew Dillon int i; 1579cf5f3a81SMatthew Dillon 1580cf5f3a81SMatthew Dillon /* 1581cf5f3a81SMatthew Dillon * Stop the port. We can't modify things like SUD if the port 1582cf5f3a81SMatthew Dillon * is running. 1583cf5f3a81SMatthew Dillon */ 1584cf5f3a81SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 15851980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 15861980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1587cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 1588cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 1589cf5f3a81SMatthew Dillon 1590cf5f3a81SMatthew Dillon /* 15911980eff3SMatthew Dillon * Clean up AT sub-ports on SATA port. 15921980eff3SMatthew Dillon */ 15931980eff3SMatthew Dillon for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) { 15941980eff3SMatthew Dillon at = &ap->ap_ata[i]; 15951980eff3SMatthew Dillon at->at_type = ATA_PORT_T_NONE; 15963209f581SMatthew Dillon at->at_probe = ATA_PROBE_FAILED; 15971980eff3SMatthew Dillon } 15981980eff3SMatthew Dillon 15991980eff3SMatthew Dillon /* 16001980eff3SMatthew Dillon * Turn off port-multiplier control bit 16011980eff3SMatthew Dillon */ 16021980eff3SMatthew Dillon if (cmd & AHCI_PREG_CMD_PMA) { 16031980eff3SMatthew Dillon cmd &= ~AHCI_PREG_CMD_PMA; 16041980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 16051980eff3SMatthew Dillon } 16061980eff3SMatthew Dillon 16071980eff3SMatthew Dillon /* 1608cf5f3a81SMatthew Dillon * Make sure FRE is active. There isn't anything we can do if it 1609cf5f3a81SMatthew Dillon * fails so just ignore errors. 1610cf5f3a81SMatthew Dillon */ 1611cf5f3a81SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 1612cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE; 1613cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1614cf5f3a81SMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) 1615cf5f3a81SMatthew Dillon ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR); 1616cf5f3a81SMatthew Dillon } 1617cf5f3a81SMatthew Dillon 1618cf5f3a81SMatthew Dillon /* 1619cf5f3a81SMatthew Dillon * 10.10.3 DET must be set to 0 before setting SUD to 0. 1620cf5f3a81SMatthew Dillon * 10.10.1 place us in the Listen state. 1621cf5f3a81SMatthew Dillon * 1622cf5f3a81SMatthew Dillon * Deactivating SUD only applies if the controller supports SUD. 1623cf5f3a81SMatthew Dillon */ 1624cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 16253209f581SMatthew Dillon ahci_os_sleep(1); 1626cf5f3a81SMatthew Dillon if (cmd & AHCI_PREG_CMD_SUD) { 1627cf5f3a81SMatthew Dillon cmd &= ~AHCI_PREG_CMD_SUD; 1628cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1629cf5f3a81SMatthew Dillon } 16303209f581SMatthew Dillon ahci_os_sleep(1); 1631cf5f3a81SMatthew Dillon 1632cf5f3a81SMatthew Dillon /* 1633cf5f3a81SMatthew Dillon * Transition su to the spin-up state. HVA shall send COMRESET and 1634cf5f3a81SMatthew Dillon * begin initialization sequence (whatever that means). 1635cf5f3a81SMatthew Dillon * 1636cf5f3a81SMatthew Dillon * This only applies if the controller supports SUD. 1637cf5f3a81SMatthew Dillon */ 1638cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1639cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 16403209f581SMatthew Dillon ahci_os_sleep(1); 1641cf5f3a81SMatthew Dillon 1642cf5f3a81SMatthew Dillon /* 1643cf5f3a81SMatthew Dillon * Transition us to the Reset state. Theoretically we send a 1644cf5f3a81SMatthew Dillon * continuous stream of COMRESETs in this state. 1645cf5f3a81SMatthew Dillon */ 1646cf5f3a81SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1647cf5f3a81SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1648cf5f3a81SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1649cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1650cf5f3a81SMatthew Dillon } else { 1651cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1652cf5f3a81SMatthew Dillon } 1653cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 16543209f581SMatthew Dillon ahci_os_sleep(1); 1655cf5f3a81SMatthew Dillon 1656cf5f3a81SMatthew Dillon /* 1657cf5f3a81SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 1658cf5f3a81SMatthew Dillon */ 1659cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1660cf5f3a81SMatthew Dillon 1661cf5f3a81SMatthew Dillon /* 1662cf5f3a81SMatthew Dillon * Leave us in COMRESET (both SUD and INIT active), the HBA should 1663cf5f3a81SMatthew Dillon * hopefully send us a DIAG_X-related interrupt if it receives 1664cf5f3a81SMatthew Dillon * a COMINIT, and if not that then at least a Phy transition 1665cf5f3a81SMatthew Dillon * interrupt. 1666cf5f3a81SMatthew Dillon * 1667cf5f3a81SMatthew Dillon * If we transition INIT from 1->0 to begin the initalization 1668cf5f3a81SMatthew Dillon * sequence it is unclear if that sequence will remain active 1669cf5f3a81SMatthew Dillon * until the next device insertion. 1670cf5f3a81SMatthew Dillon * 1671cf5f3a81SMatthew Dillon * If we go back to the listen state it is unclear if the 1672cf5f3a81SMatthew Dillon * device will actually send us a COMINIT, since we aren't 1673cf5f3a81SMatthew Dillon * sending any COMRESET's 1674cf5f3a81SMatthew Dillon */ 1675cf5f3a81SMatthew Dillon /* NOP */ 1676cf5f3a81SMatthew Dillon } 1677cf5f3a81SMatthew Dillon 1678cf5f3a81SMatthew Dillon /* 1679cf5f3a81SMatthew Dillon * Multiple events may have built up in the TFD. The spec is not very 1680cf5f3a81SMatthew Dillon * clear on this but it does seem to serialize events so clearing DIAG_X 1681cf5f3a81SMatthew Dillon * just once might not do the job during a reset sequence. 1682831bc9e3SMatthew Dillon * 1683831bc9e3SMatthew Dillon * XXX this probably isn't right. 1684cf5f3a81SMatthew Dillon */ 1685cf5f3a81SMatthew Dillon void 1686cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap) 1687cf5f3a81SMatthew Dillon { 1688cf5f3a81SMatthew Dillon u_int32_t r; 1689cf5f3a81SMatthew Dillon 1690cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 16911980eff3SMatthew Dillon while (r & AHCI_PREG_SERR_DIAG_X) { 16921980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X); 16933209f581SMatthew Dillon ahci_os_sleep(1); 1694cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 1695cf5f3a81SMatthew Dillon } 1696cf5f3a81SMatthew Dillon } 1697cf5f3a81SMatthew Dillon 1698cf5f3a81SMatthew Dillon /* 1699fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 1700fd8bd957SMatthew Dillon * DISK. 1701fd8bd957SMatthew Dillon */ 1702fd8bd957SMatthew Dillon int 17031980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at) 1704fd8bd957SMatthew Dillon { 1705fd8bd957SMatthew Dillon u_int32_t sig; 1706fd8bd957SMatthew Dillon 1707fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 17081980eff3SMatthew Dillon kprintf("%s: sig %08x\n", ATANAME(ap, at), sig); 1709fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 1710fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 17111980eff3SMatthew Dillon } else if ((sig & 0xffff0000) == 17121980eff3SMatthew Dillon (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) { 17131980eff3SMatthew Dillon return(ATA_PORT_T_PM); 1714fd8bd957SMatthew Dillon } else { 1715fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 1716fd8bd957SMatthew Dillon } 1717fd8bd957SMatthew Dillon } 1718fd8bd957SMatthew Dillon 1719fd8bd957SMatthew Dillon /* 1720fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 1721fd8bd957SMatthew Dillon */ 1722258223a3SMatthew Dillon int 1723258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 1724258223a3SMatthew Dillon { 1725258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1726258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1727258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1728258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 1729258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1730258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 1731258223a3SMatthew Dillon int error; 1732258223a3SMatthew Dillon 1733258223a3SMatthew Dillon if (xa->datalen == 0) { 1734258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 1735258223a3SMatthew Dillon return (0); 1736258223a3SMatthew Dillon } 1737258223a3SMatthew Dillon 1738258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 1739258223a3SMatthew Dillon xa->data, xa->datalen, 1740258223a3SMatthew Dillon ahci_load_prdt_callback, 1741258223a3SMatthew Dillon &prdt, 1742258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 1743258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 1744258223a3SMatthew Dillon if (error != 0) { 1745258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 1746258223a3SMatthew Dillon return (1); 1747258223a3SMatthew Dillon } 1748258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 1749258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 1750258223a3SMatthew Dillon 1751258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 1752258223a3SMatthew Dillon 1753258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1754258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1755258223a3SMatthew Dillon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1756258223a3SMatthew Dillon 1757258223a3SMatthew Dillon return (0); 1758258223a3SMatthew Dillon 1759258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1760258223a3SMatthew Dillon diagerr: 1761258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1762258223a3SMatthew Dillon return (1); 1763258223a3SMatthew Dillon #endif 1764258223a3SMatthew Dillon } 1765258223a3SMatthew Dillon 1766258223a3SMatthew Dillon /* 1767258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 1768258223a3SMatthew Dillon * list is a temporary structure. 1769258223a3SMatthew Dillon */ 1770258223a3SMatthew Dillon static 1771258223a3SMatthew Dillon void 1772258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 1773258223a3SMatthew Dillon int error) 1774258223a3SMatthew Dillon { 1775258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 1776258223a3SMatthew Dillon u_int64_t addr; 1777258223a3SMatthew Dillon 1778258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 1779258223a3SMatthew Dillon 1780258223a3SMatthew Dillon while (nsegs) { 1781258223a3SMatthew Dillon addr = segs->ds_addr; 1782258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 1783258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 1784258223a3SMatthew Dillon #ifdef DIAGNOSTIC 1785258223a3SMatthew Dillon KKASSERT((addr & 1) == 0); 1786258223a3SMatthew Dillon KKASSERT((segs->ds_len & 1) == 0); 1787258223a3SMatthew Dillon #endif 1788258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 1789258223a3SMatthew Dillon --nsegs; 1790258223a3SMatthew Dillon if (nsegs) 1791258223a3SMatthew Dillon ++prd; 1792258223a3SMatthew Dillon ++segs; 1793258223a3SMatthew Dillon } 1794258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 1795258223a3SMatthew Dillon } 1796258223a3SMatthew Dillon 1797258223a3SMatthew Dillon void 1798258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 1799258223a3SMatthew Dillon { 1800258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1801258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1802258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1803258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1804258223a3SMatthew Dillon 1805258223a3SMatthew Dillon if (xa->datalen != 0) { 1806258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1807258223a3SMatthew Dillon (xa->flags & ATA_F_READ) ? 1808258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1809258223a3SMatthew Dillon 1810258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1811258223a3SMatthew Dillon 1812258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) 1813258223a3SMatthew Dillon xa->resid = 0; 1814258223a3SMatthew Dillon else 1815258223a3SMatthew Dillon xa->resid = xa->datalen - 1816258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 1817258223a3SMatthew Dillon } 1818258223a3SMatthew Dillon } 1819258223a3SMatthew Dillon 18205f8c1efdSMatthew Dillon /* 18215f8c1efdSMatthew Dillon * Start a command and poll for completion. 18225f8c1efdSMatthew Dillon * 18233209f581SMatthew Dillon * timeout is in ms and only counts once the command gets on-chip. 18243209f581SMatthew Dillon * 1825831bc9e3SMatthew Dillon * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine 1826831bc9e3SMatthew Dillon * that no error occured. 1827831bc9e3SMatthew Dillon * 18285f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 18295f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 18305f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 18315f8c1efdSMatthew Dillon * 18325f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 1833cf5f3a81SMatthew Dillon * 1834cf5f3a81SMatthew Dillon * NOTE: If the port is in a failed state and stopped we do not try 1835cf5f3a81SMatthew Dillon * to activate the ccb. 18365f8c1efdSMatthew Dillon */ 1837258223a3SMatthew Dillon int 1838831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, 1839831bc9e3SMatthew Dillon void (*timeout_fn)(struct ahci_ccb *)) 1840258223a3SMatthew Dillon { 1841258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1842258223a3SMatthew Dillon 1843cf5f3a81SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) { 1844cf5f3a81SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1845831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 1846cf5f3a81SMatthew Dillon } 1847258223a3SMatthew Dillon crit_enter(); 1848258223a3SMatthew Dillon ahci_start(ccb); 18491980eff3SMatthew Dillon 1850258223a3SMatthew Dillon do { 1851f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 1852831bc9e3SMatthew Dillon switch(ccb->ccb_xa.state) { 1853831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 1854831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 1855f4553de1SMatthew Dillon break; 1856831bc9e3SMatthew Dillon case ATA_S_PENDING: 1857831bc9e3SMatthew Dillon ahci_os_softsleep(); 1858831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 1859831bc9e3SMatthew Dillon break; 1860831bc9e3SMatthew Dillon default: 1861831bc9e3SMatthew Dillon crit_exit(); 1862831bc9e3SMatthew Dillon return (ccb->ccb_xa.state); 1863f4553de1SMatthew Dillon } 18643209f581SMatthew Dillon } while (timeout > 0); 18655f8c1efdSMatthew Dillon 1866831bc9e3SMatthew Dillon kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n", 1867831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, 1868831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 1869831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS, 1870831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR); 18715f8c1efdSMatthew Dillon 1872258223a3SMatthew Dillon timeout_fn(ccb); 1873831bc9e3SMatthew Dillon 1874258223a3SMatthew Dillon crit_exit(); 1875258223a3SMatthew Dillon 1876831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 1877831bc9e3SMatthew Dillon } 1878831bc9e3SMatthew Dillon 1879831bc9e3SMatthew Dillon /* 1880831bc9e3SMatthew Dillon * When polling we have to check if the currently active CCB(s) 1881831bc9e3SMatthew Dillon * have timed out as the callout will be deadlocked while we 1882831bc9e3SMatthew Dillon * hold the port lock. 1883831bc9e3SMatthew Dillon */ 1884831bc9e3SMatthew Dillon void 1885831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap) 1886831bc9e3SMatthew Dillon { 1887831bc9e3SMatthew Dillon struct ahci_ccb *ccb; 1888831bc9e3SMatthew Dillon u_int32_t mask; 1889831bc9e3SMatthew Dillon int tag; 1890831bc9e3SMatthew Dillon 1891831bc9e3SMatthew Dillon mask = ap->ap_active | ap->ap_sactive; 1892831bc9e3SMatthew Dillon while (mask) { 1893831bc9e3SMatthew Dillon tag = ffs(mask) - 1; 1894831bc9e3SMatthew Dillon mask &= ~(1 << tag); 1895831bc9e3SMatthew Dillon ccb = &ap->ap_ccbs[tag]; 1896831bc9e3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) { 1897831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(ccb); 1898831bc9e3SMatthew Dillon } 1899831bc9e3SMatthew Dillon } 1900258223a3SMatthew Dillon } 1901258223a3SMatthew Dillon 19023209f581SMatthew Dillon static 19033209f581SMatthew Dillon __inline 19043209f581SMatthew Dillon void 19053209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb) 19063209f581SMatthew Dillon { 19073209f581SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) { 19083209f581SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING; 19093209f581SMatthew Dillon callout_reset(&ccb->ccb_timeout, 19103209f581SMatthew Dillon (ccb->ccb_xa.timeout * hz + 999) / 1000, 19113209f581SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 19123209f581SMatthew Dillon } 19133209f581SMatthew Dillon } 19143209f581SMatthew Dillon 1915258223a3SMatthew Dillon void 1916258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 1917258223a3SMatthew Dillon { 1918258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1919258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1920258223a3SMatthew Dillon 1921258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 1922258223a3SMatthew Dillon 1923258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 1924258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 1925258223a3SMatthew Dillon 1926258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 1927258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 1928258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 1929258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1930258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 1931258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 1932258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1933258223a3SMatthew Dillon 1934258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 1935258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 1936258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 1937258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 1938258223a3SMatthew Dillon 1939258223a3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 19401980eff3SMatthew Dillon /* 19411980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 19421980eff3SMatthew Dillon * standard commands. 19431980eff3SMatthew Dillon */ 19441980eff3SMatthew Dillon if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) { 1945258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 19461980eff3SMatthew Dillon } else { 19473209f581SMatthew Dillon ahci_start_timeout(ccb); 1948258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 1949258223a3SMatthew Dillon ap->ap_sactive |= (1 << ccb->ccb_slot); 1950258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1951258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot); 1952258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1953258223a3SMatthew Dillon } 1954258223a3SMatthew Dillon } else { 19555f8c1efdSMatthew Dillon /* 19565f8c1efdSMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 19571980eff3SMatthew Dillon * command. Allow up to <limit> non-NCQ commands to be active. 19581980eff3SMatthew Dillon * 19591980eff3SMatthew Dillon * XXX If ap is a port multiplier only allow 1. At least the 19601980eff3SMatthew Dillon * NVidia-MCP77 part seems to barf if more then one 19611980eff3SMatthew Dillon * command is activated, even though it isn't NCQ. 19621980eff3SMatthew Dillon * 19631980eff3SMatthew Dillon * If I set up more then one I get phy errors and the 19641980eff3SMatthew Dillon * port fails. 19655f8c1efdSMatthew Dillon */ 19661980eff3SMatthew Dillon int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2; 19671980eff3SMatthew Dillon if (ap->ap_sactive || ap->ap_active_cnt >= limit) { 1968258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 19691980eff3SMatthew Dillon } else { 19703209f581SMatthew Dillon ahci_start_timeout(ccb); 1971258223a3SMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 1972258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 1973258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 1974258223a3SMatthew Dillon ap->ap_active_cnt++; 1975258223a3SMatthew Dillon } 1976258223a3SMatthew Dillon } 1977258223a3SMatthew Dillon } 1978258223a3SMatthew Dillon 1979831bc9e3SMatthew Dillon /* 1980831bc9e3SMatthew Dillon * While holding the port lock acquire exclusive access to the port. 1981831bc9e3SMatthew Dillon * 1982831bc9e3SMatthew Dillon * This is used when running the state machine to initialize and identify 1983831bc9e3SMatthew Dillon * targets over a port multiplier. Setting exclusive access prevents 1984831bc9e3SMatthew Dillon * ahci_port_intr() from activating any requests sitting on the pending 1985831bc9e3SMatthew Dillon * queue. 1986831bc9e3SMatthew Dillon */ 1987831bc9e3SMatthew Dillon void 1988831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at) 1989831bc9e3SMatthew Dillon { 1990831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0); 1991831bc9e3SMatthew Dillon ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS; 1992831bc9e3SMatthew Dillon while (ap->ap_active || ap->ap_sactive) { 1993831bc9e3SMatthew Dillon ahci_port_intr(ap, 1); 1994831bc9e3SMatthew Dillon ahci_os_softsleep(); 1995831bc9e3SMatthew Dillon } 1996831bc9e3SMatthew Dillon } 1997831bc9e3SMatthew Dillon 1998831bc9e3SMatthew Dillon void 1999831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at) 2000831bc9e3SMatthew Dillon { 2001831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0); 2002831bc9e3SMatthew Dillon ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS; 2003831bc9e3SMatthew Dillon if (ap->ap_active == 0 && ap->ap_sactive == 0) 2004831bc9e3SMatthew Dillon ahci_issue_pending_commands(ap, 0); 2005831bc9e3SMatthew Dillon } 2006831bc9e3SMatthew Dillon 2007258223a3SMatthew Dillon void 2008258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap) 2009258223a3SMatthew Dillon { 2010258223a3SMatthew Dillon struct ahci_ccb *nextccb; 2011258223a3SMatthew Dillon u_int32_t sact_change = 0; 2012258223a3SMatthew Dillon 2013258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 2014258223a3SMatthew Dillon 2015258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 2016258223a3SMatthew Dillon if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ)) 2017258223a3SMatthew Dillon return; 2018831bc9e3SMatthew Dillon if (ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) 2019831bc9e3SMatthew Dillon return; 2020258223a3SMatthew Dillon 2021258223a3SMatthew Dillon /* Start all the NCQ commands at the head of the pending list. */ 2022258223a3SMatthew Dillon do { 2023258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 20243209f581SMatthew Dillon ahci_start_timeout(nextccb); 2025258223a3SMatthew Dillon sact_change |= 1 << nextccb->ccb_slot; 2026258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 2027258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 2028258223a3SMatthew Dillon } while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)); 2029258223a3SMatthew Dillon 2030258223a3SMatthew Dillon ap->ap_sactive |= sact_change; 2031258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, sact_change); 2032258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, sact_change); 2033258223a3SMatthew Dillon 2034258223a3SMatthew Dillon return; 2035258223a3SMatthew Dillon } 2036258223a3SMatthew Dillon 2037258223a3SMatthew Dillon void 2038258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq) 2039258223a3SMatthew Dillon { 2040258223a3SMatthew Dillon struct ahci_ccb *nextccb; 2041258223a3SMatthew Dillon 2042258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 2043831bc9e3SMatthew Dillon if (nextccb == NULL) 2044831bc9e3SMatthew Dillon return; 2045831bc9e3SMatthew Dillon if (ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) 2046831bc9e3SMatthew Dillon return; 2047831bc9e3SMatthew Dillon 2048831bc9e3SMatthew Dillon if (nextccb->ccb_xa.flags & ATA_F_NCQ) { 2049258223a3SMatthew Dillon KKASSERT(last_was_ncq == 0); /* otherwise it should have 2050258223a3SMatthew Dillon * been started already. */ 2051258223a3SMatthew Dillon 20521980eff3SMatthew Dillon /* 20531980eff3SMatthew Dillon * Issue NCQ commands only when there are no outstanding 20541980eff3SMatthew Dillon * standard commands. 20551980eff3SMatthew Dillon */ 2056258223a3SMatthew Dillon if (ap->ap_active == 0) 2057258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 2058258223a3SMatthew Dillon else 20591980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 2060831bc9e3SMatthew Dillon } else { 20611980eff3SMatthew Dillon if (ap->ap_sactive || last_was_ncq) 2062258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 2063258223a3SMatthew Dillon 20641980eff3SMatthew Dillon /* 20651980eff3SMatthew Dillon * Wait for all NCQ commands to finish before issuing standard 20661980eff3SMatthew Dillon * command. Then keep up to 2 standard commands on-chip at 20671980eff3SMatthew Dillon * a time. 20681980eff3SMatthew Dillon */ 20691980eff3SMatthew Dillon if (ap->ap_sactive) 2070258223a3SMatthew Dillon return; 2071258223a3SMatthew Dillon 20721980eff3SMatthew Dillon while (ap->ap_active_cnt < 2 && 20731980eff3SMatthew Dillon nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) { 2074258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry); 20753209f581SMatthew Dillon ahci_start_timeout(nextccb); 2076258223a3SMatthew Dillon ap->ap_active |= 1 << nextccb->ccb_slot; 2077258223a3SMatthew Dillon nextccb->ccb_xa.state = ATA_S_ONCHIP; 2078258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot); 2079258223a3SMatthew Dillon ap->ap_active_cnt++; 2080258223a3SMatthew Dillon nextccb = TAILQ_FIRST(&ap->ap_ccb_pending); 20811980eff3SMatthew Dillon } 2082258223a3SMatthew Dillon } 2083258223a3SMatthew Dillon } 2084258223a3SMatthew Dillon 2085258223a3SMatthew Dillon void 2086258223a3SMatthew Dillon ahci_intr(void *arg) 2087258223a3SMatthew Dillon { 2088258223a3SMatthew Dillon struct ahci_softc *sc = arg; 2089f4553de1SMatthew Dillon struct ahci_port *ap; 2090258223a3SMatthew Dillon u_int32_t is, ack = 0; 2091258223a3SMatthew Dillon int port; 2092258223a3SMatthew Dillon 2093f4553de1SMatthew Dillon /* 2094f4553de1SMatthew Dillon * Check if the master enable is up, and whether any interrupts are 2095f4553de1SMatthew Dillon * pending. 2096f4553de1SMatthew Dillon */ 2097f4553de1SMatthew Dillon if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0) 2098f4553de1SMatthew Dillon return; 2099258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 2100258223a3SMatthew Dillon if (is == 0 || is == 0xffffffff) 2101258223a3SMatthew Dillon return; 2102258223a3SMatthew Dillon ack = is; 2103258223a3SMatthew Dillon 2104258223a3SMatthew Dillon #ifdef AHCI_COALESCE 2105258223a3SMatthew Dillon /* Check coalescing interrupt first */ 2106258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 2107258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 2108258223a3SMatthew Dillon DEVNAME(sc)); 2109258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 2110258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 2111258223a3SMatthew Dillon } 2112258223a3SMatthew Dillon #endif 2113258223a3SMatthew Dillon 2114f4553de1SMatthew Dillon /* 2115f4553de1SMatthew Dillon * Process interrupts for each port in a non-blocking fashion. 2116f4553de1SMatthew Dillon */ 2117258223a3SMatthew Dillon while (is) { 2118258223a3SMatthew Dillon port = ffs(is) - 1; 2119f4553de1SMatthew Dillon ap = sc->sc_ports[port]; 2120f4553de1SMatthew Dillon if (ap) { 2121f4553de1SMatthew Dillon if (ahci_os_lock_port_nb(ap) == 0) { 2122f4553de1SMatthew Dillon ahci_port_intr(ap, 0); 2123f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 2124f4553de1SMatthew Dillon } else { 2125f4553de1SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2126f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2127f4553de1SMatthew Dillon } 2128f4553de1SMatthew Dillon } 2129258223a3SMatthew Dillon is &= ~(1 << port); 2130258223a3SMatthew Dillon } 2131258223a3SMatthew Dillon 2132258223a3SMatthew Dillon /* Finally, acknowledge global interrupt */ 2133258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, ack); 2134258223a3SMatthew Dillon } 2135258223a3SMatthew Dillon 2136f4553de1SMatthew Dillon /* 2137f4553de1SMatthew Dillon * Core called from helper thread. 2138f4553de1SMatthew Dillon */ 21393209f581SMatthew Dillon void 2140f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask) 2141f4553de1SMatthew Dillon { 2142f4553de1SMatthew Dillon /* 2143f4553de1SMatthew Dillon * Process any expired timedouts. 2144f4553de1SMatthew Dillon */ 2145f4553de1SMatthew Dillon ahci_os_lock_port(ap); 2146f4553de1SMatthew Dillon if (mask & AP_SIGF_TIMEOUT) { 2147831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 2148f4553de1SMatthew Dillon } 2149f4553de1SMatthew Dillon 2150f4553de1SMatthew Dillon /* 2151f4553de1SMatthew Dillon * Process port interrupts which require a higher level of 2152f4553de1SMatthew Dillon * intervention. 2153f4553de1SMatthew Dillon */ 2154f4553de1SMatthew Dillon if (mask & AP_SIGF_PORTINT) { 2155f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 2156f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 2157831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 2158f4553de1SMatthew Dillon } else { 2159f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 2160f4553de1SMatthew Dillon } 2161f4553de1SMatthew Dillon } 2162f4553de1SMatthew Dillon 2163f4553de1SMatthew Dillon /* 2164f4553de1SMatthew Dillon * Core per-port interrupt handler. 2165f4553de1SMatthew Dillon * 2166f4553de1SMatthew Dillon * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only 2167f4553de1SMatthew Dillon * deal with normal command completions which do not require blocking. 2168f4553de1SMatthew Dillon */ 2169f4553de1SMatthew Dillon void 2170f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable) 2171258223a3SMatthew Dillon { 2172258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 21733209f581SMatthew Dillon u_int32_t is, ci_saved, ci_masked; 217422181ab7SMatthew Dillon int slot; 2175258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 21761980eff3SMatthew Dillon struct ata_port *ccb_at = NULL; 2177258223a3SMatthew Dillon volatile u_int32_t *active; 2178258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2179258223a3SMatthew Dillon u_int32_t tmp; 2180258223a3SMatthew Dillon #endif 2181f4553de1SMatthew Dillon const u_int32_t blockable_mask = AHCI_PREG_IS_TFES | 2182f4553de1SMatthew Dillon AHCI_PREG_IS_IFS | 2183f4553de1SMatthew Dillon AHCI_PREG_IS_PCS | 2184f4553de1SMatthew Dillon AHCI_PREG_IS_PRCS | 2185f4553de1SMatthew Dillon AHCI_PREG_IS_HBFS | 2186f4553de1SMatthew Dillon AHCI_PREG_IS_OFS | 2187f4553de1SMatthew Dillon AHCI_PREG_IS_UFS; 2188f4553de1SMatthew Dillon 218922181ab7SMatthew Dillon enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT, 219022181ab7SMatthew Dillon NEED_HOTPLUG_REMOVE } need = NEED_NOTHING; 2191258223a3SMatthew Dillon 2192258223a3SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 2193f4553de1SMatthew Dillon 2194f4553de1SMatthew Dillon /* 2195f4553de1SMatthew Dillon * All basic command completions are always processed. 2196f4553de1SMatthew Dillon */ 2197cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_DPS) 2198cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS); 2199258223a3SMatthew Dillon 2200f4553de1SMatthew Dillon /* 2201f4553de1SMatthew Dillon * If we can't block then we can't handle these here. Disable 2202f4553de1SMatthew Dillon * the interrupts in question so we don't live-lock, the helper 2203f4553de1SMatthew Dillon * thread will re-enable them. 2204f4553de1SMatthew Dillon * 2205f4553de1SMatthew Dillon * If the port is in a completely failed state we do not want 2206dbef6246SMatthew Dillon * to drop through to failed-command-processing if blockable is 0, 2207f4553de1SMatthew Dillon * just let the thread deal with it all. 2208dbef6246SMatthew Dillon * 2209dbef6246SMatthew Dillon * Otherwise we fall through and still handle DHRS and any commands 2210dbef6246SMatthew Dillon * which completed normally. Even if we are errored we haven't 2211dbef6246SMatthew Dillon * stopped the port yet so CI/SACT are still good. 2212f4553de1SMatthew Dillon */ 2213f4553de1SMatthew Dillon if (blockable == 0) { 2214f4553de1SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2215f4553de1SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 2216f4553de1SMatthew Dillon ahci_pread(ap, AHCI_PREG_IE) & ~is); 2217f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2218f4553de1SMatthew Dillon return; 2219f4553de1SMatthew Dillon } 2220f4553de1SMatthew Dillon if (is & blockable_mask) { 2221f4553de1SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 2222dbef6246SMatthew Dillon ahci_pread(ap, AHCI_PREG_IE) & ~blockable_mask); 2223dbef6246SMatthew Dillon is &= ~blockable_mask; 2224f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2225f4553de1SMatthew Dillon } 2226f4553de1SMatthew Dillon } 2227f4553de1SMatthew Dillon 22281980eff3SMatthew Dillon #if 0 22291980eff3SMatthew Dillon kprintf("%s: INTERRUPT %b\n", PORTNAME(ap), 22301980eff3SMatthew Dillon is, AHCI_PFMT_IS); 22311980eff3SMatthew Dillon #endif 22321980eff3SMatthew Dillon 22333209f581SMatthew Dillon /* 2234f4553de1SMatthew Dillon * Either NCQ or non-NCQ commands will be active, never both. 22353209f581SMatthew Dillon */ 2236258223a3SMatthew Dillon if (ap->ap_sactive) { 2237258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 2238258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 2239258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 2240258223a3SMatthew Dillon active = &ap->ap_sactive; 2241258223a3SMatthew Dillon } else { 2242258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 2243258223a3SMatthew Dillon active = &ap->ap_active; 2244258223a3SMatthew Dillon } 2245258223a3SMatthew Dillon 22461980eff3SMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 2247cf5f3a81SMatthew Dillon /* 2248f4553de1SMatthew Dillon * Command failed (blockable). 2249f4553de1SMatthew Dillon * 2250f4553de1SMatthew Dillon * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. 22511980eff3SMatthew Dillon * 22521980eff3SMatthew Dillon * This stops command processing. 2253cf5f3a81SMatthew Dillon */ 2254258223a3SMatthew Dillon u_int32_t tfd, serr; 2255258223a3SMatthew Dillon int err_slot; 2256258223a3SMatthew Dillon 2257258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 2258258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2259258223a3SMatthew Dillon 2260cf5f3a81SMatthew Dillon /* 2261cf5f3a81SMatthew Dillon * If no NCQ commands are active the error slot is easily 2262cf5f3a81SMatthew Dillon * determined, otherwise we have to extract the error 2263cf5f3a81SMatthew Dillon * from the log page. 2264cf5f3a81SMatthew Dillon */ 2265258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 2266cf5f3a81SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 2267cf5f3a81SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 2268258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 22691980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 2270258223a3SMatthew Dillon 2271258223a3SMatthew Dillon /* Preserve received taskfile data from the RFIS. */ 2272258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 2273258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 2274cf5f3a81SMatthew Dillon } else { 2275cf5f3a81SMatthew Dillon err_slot = -1; 2276cf5f3a81SMatthew Dillon } 2277258223a3SMatthew Dillon 22781980eff3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n", 22791980eff3SMatthew Dillon PORTNAME(ap), err_slot, 22801980eff3SMatthew Dillon tfd, AHCI_PFMT_TFD_STS, 22811980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 2282258223a3SMatthew Dillon 2283cf5f3a81SMatthew Dillon /* Stopping the port clears CI and SACT */ 2284258223a3SMatthew Dillon ahci_port_stop(ap, 0); 228522181ab7SMatthew Dillon need = NEED_RESTART; 2286258223a3SMatthew Dillon 2287cf5f3a81SMatthew Dillon /* 2288cf5f3a81SMatthew Dillon * Clear SERR (primarily DIAG_X) to enable capturing of the 2289cf5f3a81SMatthew Dillon * next error. 2290cf5f3a81SMatthew Dillon */ 2291258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 2292258223a3SMatthew Dillon 2293258223a3SMatthew Dillon /* Acknowledge the interrupts we can recover from. */ 2294cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2295cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS)); 22961980eff3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS); 2297258223a3SMatthew Dillon 2298258223a3SMatthew Dillon /* If device hasn't cleared its busy status, try to idle it. */ 2299258223a3SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 23001980eff3SMatthew Dillon kprintf("%s: Attempting to idle device ccb=%p\n", 23011980eff3SMatthew Dillon PORTNAME(ap), ccb_at); 23021980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 23031980eff3SMatthew Dillon goto fatal; 23041980eff3SMatthew Dillon /* 23051980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 23061980eff3SMatthew Dillon * 23071980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 23081980eff3SMatthew Dillon * busy condition and make the ahci port usable for 23091980eff3SMatthew Dillon * the remaining devices. 23101980eff3SMatthew Dillon */ 23111980eff3SMatthew Dillon if (ccb_at) { 23121980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 23131980eff3SMatthew Dillon ahci_port_clo(ap); 23141980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 231517eab71eSMatthew Dillon kprintf("%s: Unable to idle device, port " 231617eab71eSMatthew Dillon "bricked on us\n", 2317258223a3SMatthew Dillon PORTNAME(ap)); 2318258223a3SMatthew Dillon goto fatal; 2319258223a3SMatthew Dillon } 2320258223a3SMatthew Dillon 2321258223a3SMatthew Dillon /* Had to reset device, can't gather extended info. */ 2322258223a3SMatthew Dillon } else if (ap->ap_sactive) { 23231980eff3SMatthew Dillon /* 23241980eff3SMatthew Dillon * Recover the NCQ error from log page 10h. 23251980eff3SMatthew Dillon * 23261980eff3SMatthew Dillon * XXX NCQ currently not supported with port 23271980eff3SMatthew Dillon * multiplier. 23281980eff3SMatthew Dillon */ 2329258223a3SMatthew Dillon ahci_port_read_ncq_error(ap, &err_slot); 2330cec07d75SMatthew Dillon kprintf("recover from NCQ error err_slot %d\n", 2331cec07d75SMatthew Dillon err_slot); 2332258223a3SMatthew Dillon if (err_slot < 0) 2333258223a3SMatthew Dillon goto failall; 2334258223a3SMatthew Dillon 2335258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n", 2336258223a3SMatthew Dillon PORTNAME(ap), err_slot); 2337258223a3SMatthew Dillon 2338258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2339258223a3SMatthew Dillon } else { 2340258223a3SMatthew Dillon /* Didn't reset, could gather extended info from log. */ 23411980eff3SMatthew Dillon kprintf("%s: didn't reset err_slot %d " 23421980eff3SMatthew Dillon "sact=%08x act=%08x\n", 23431980eff3SMatthew Dillon PORTNAME(ap), 2344cf5f3a81SMatthew Dillon err_slot, ap->ap_sactive, ap->ap_active); 2345258223a3SMatthew Dillon } 2346258223a3SMatthew Dillon 2347258223a3SMatthew Dillon /* 2348258223a3SMatthew Dillon * If we couldn't determine the errored slot, reset the port 2349258223a3SMatthew Dillon * and fail all the active slots. 2350258223a3SMatthew Dillon */ 2351258223a3SMatthew Dillon if (err_slot == -1) { 23521980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 23531980eff3SMatthew Dillon goto fatal; 23541980eff3SMatthew Dillon /* 23551980eff3SMatthew Dillon * XXX how do we unbrick a PM target (ccb_at != NULL). 23561980eff3SMatthew Dillon * 23571980eff3SMatthew Dillon * For now fail the target and use CLO to clear the 23581980eff3SMatthew Dillon * busy condition and make the ahci port usable for 23591980eff3SMatthew Dillon * the remaining devices. 23601980eff3SMatthew Dillon */ 23611980eff3SMatthew Dillon if (ccb_at) { 23621980eff3SMatthew Dillon ccb_at->at_probe = ATA_PROBE_FAILED; 23631980eff3SMatthew Dillon ahci_port_clo(ap); 23641980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb_at, 0)) { 236517eab71eSMatthew Dillon kprintf("%s: Unable to idle device after " 236617eab71eSMatthew Dillon "NCQ error, port bricked on us\n", 2367258223a3SMatthew Dillon PORTNAME(ap)); 2368258223a3SMatthew Dillon goto fatal; 2369258223a3SMatthew Dillon } 2370258223a3SMatthew Dillon kprintf("%s: couldn't recover NCQ error, failing " 2371258223a3SMatthew Dillon "all outstanding commands.\n", 2372258223a3SMatthew Dillon PORTNAME(ap)); 2373258223a3SMatthew Dillon goto failall; 2374258223a3SMatthew Dillon } 2375258223a3SMatthew Dillon 2376258223a3SMatthew Dillon /* Clear the failed command in saved CI so completion runs. */ 2377258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 2378258223a3SMatthew Dillon 2379258223a3SMatthew Dillon /* Note the error in the ata_xfer. */ 2380258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2381258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2382258223a3SMatthew Dillon 2383258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2384258223a3SMatthew Dillon /* There may only be one outstanding standard command now. */ 2385258223a3SMatthew Dillon if (ap->ap_sactive == 0) { 2386258223a3SMatthew Dillon tmp = ci_saved; 2387258223a3SMatthew Dillon if (tmp) { 2388258223a3SMatthew Dillon slot = ffs(tmp) - 1; 2389258223a3SMatthew Dillon tmp &= ~(1 << slot); 2390258223a3SMatthew Dillon KKASSERT(tmp == 0); 2391258223a3SMatthew Dillon } 2392258223a3SMatthew Dillon } 2393258223a3SMatthew Dillon #endif 23941980eff3SMatthew Dillon } else if (is & AHCI_PREG_IS_DHRS) { 23951980eff3SMatthew Dillon /* 2396f4553de1SMatthew Dillon * Command posted D2H register FIS to the rfis (non-blocking). 2397f4553de1SMatthew Dillon * 23981980eff3SMatthew Dillon * Command posted D2H register FIS to the rfis. This 23998bf6a3ffSMatthew Dillon * does NOT stop command processing and it is unclear 24008bf6a3ffSMatthew Dillon * how we are supposed to deal with it other then using 24018bf6a3ffSMatthew Dillon * only a queue of 1. 24028bf6a3ffSMatthew Dillon * 24038bf6a3ffSMatthew Dillon * We must copy the port rfis to the ccb and restart 24048bf6a3ffSMatthew Dillon * command processing. ahci_pm_read() does not function 24058bf6a3ffSMatthew Dillon * without this support. 24061980eff3SMatthew Dillon */ 24071980eff3SMatthew Dillon int err_slot; 24081980eff3SMatthew Dillon 24091980eff3SMatthew Dillon if (ap->ap_sactive == 0) { 24101980eff3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 24111980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 24121980eff3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 24131980eff3SMatthew Dillon ccb_at = ccb->ccb_xa.at; /* can be NULL */ 24141980eff3SMatthew Dillon 24151980eff3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 24161980eff3SMatthew Dillon sizeof(struct ata_fis_d2h)); 24171980eff3SMatthew Dillon } else { 24181980eff3SMatthew Dillon kprintf("%s: Unexpected DHRS posted while " 24191980eff3SMatthew Dillon "NCQ running\n", PORTNAME(ap)); 24201980eff3SMatthew Dillon err_slot = -1; 2421258223a3SMatthew Dillon } 2422cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS); 2423cec07d75SMatthew Dillon is &= ~AHCI_PREG_IS_DHRS; 24241980eff3SMatthew Dillon } 24251980eff3SMatthew Dillon 24261980eff3SMatthew Dillon /* 2427f4553de1SMatthew Dillon * Device notification to us (non-blocking) 24281980eff3SMatthew Dillon * 2429cec07d75SMatthew Dillon * NOTE! On some parts notification bits can get set without 2430cec07d75SMatthew Dillon * generating an interrupt. It is unclear whether this is 2431cec07d75SMatthew Dillon * a bug in the PM (sending a DTOH device setbits with 'N' set 2432cec07d75SMatthew Dillon * and 'I' not set), or a bug in the host controller. 2433cec07d75SMatthew Dillon * 2434cec07d75SMatthew Dillon * It only seems to occur under load. 24351980eff3SMatthew Dillon */ 2436cec07d75SMatthew Dillon if (/*(is & AHCI_PREG_IS_SDBS) &&*/ (sc->sc_cap & AHCI_REG_CAP_SSNTF)) { 24371980eff3SMatthew Dillon u_int32_t data; 2438cec07d75SMatthew Dillon const char *xstr; 24391980eff3SMatthew Dillon 24401980eff3SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SNTF); 2441cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_SDBS) { 24421980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS); 2443cec07d75SMatthew Dillon is &= ~AHCI_PREG_IS_SDBS; 2444cec07d75SMatthew Dillon xstr = " (no SDBS!)"; 2445cec07d75SMatthew Dillon } else { 2446cec07d75SMatthew Dillon xstr = ""; 2447cec07d75SMatthew Dillon } 2448cec07d75SMatthew Dillon if (data) { 2449cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS); 2450cec07d75SMatthew Dillon 2451cec07d75SMatthew Dillon kprintf("%s: NOTIFY %08x%s\n", 2452cec07d75SMatthew Dillon PORTNAME(ap), data, xstr); 2453cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_N); 24543209f581SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, data); 24553209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 24561980eff3SMatthew Dillon } 24571980eff3SMatthew Dillon } 24583209f581SMatthew Dillon 24593209f581SMatthew Dillon /* 2460f4553de1SMatthew Dillon * Spurious IFS errors (blockable). 2461f4553de1SMatthew Dillon * 24623209f581SMatthew Dillon * Spurious IFS errors can occur while we are doing a reset 24633209f581SMatthew Dillon * sequence through a PM. Try to recover if we are being asked 24643209f581SMatthew Dillon * to ignore IFS errors during these periods. 24653209f581SMatthew Dillon */ 24663209f581SMatthew Dillon if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) { 24671980eff3SMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 24683209f581SMatthew Dillon if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) { 24691980eff3SMatthew Dillon kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n", 24701980eff3SMatthew Dillon PORTNAME(ap), 24711980eff3SMatthew Dillon is, AHCI_PFMT_IS, 24721980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 24733209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_IGNORED; 24743209f581SMatthew Dillon } 24753209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_OCCURED; 24761980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 24771980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 24781980eff3SMatthew Dillon is &= ~AHCI_PREG_IS_IFS; 24791980eff3SMatthew Dillon ahci_port_stop(ap, 0); 24801980eff3SMatthew Dillon ahci_port_start(ap); 24811980eff3SMatthew Dillon need = NEED_RESTART; 24821980eff3SMatthew Dillon } 2483258223a3SMatthew Dillon 2484258223a3SMatthew Dillon /* 2485f4553de1SMatthew Dillon * Port change (hot-plug) (blockable). 2486258223a3SMatthew Dillon * 2487258223a3SMatthew Dillon * A PCS interrupt will occur on hot-plug once communication is 2488258223a3SMatthew Dillon * established. 2489258223a3SMatthew Dillon * 2490258223a3SMatthew Dillon * A PRCS interrupt will occur on hot-unplug (and possibly also 2491258223a3SMatthew Dillon * on hot-plug). 2492258223a3SMatthew Dillon * 249322181ab7SMatthew Dillon * XXX We can then check the CPS (Cold Presence State) bit, if 249422181ab7SMatthew Dillon * supported, to determine if a device is plugged in or not and do 249522181ab7SMatthew Dillon * the right thing. 249622181ab7SMatthew Dillon * 249722181ab7SMatthew Dillon * WARNING: A PCS interrupt is cleared by clearing DIAG_X, and 249822181ab7SMatthew Dillon * can also occur if an unsolicited COMINIT is received. 249922181ab7SMatthew Dillon * If this occurs command processing is automatically 250022181ab7SMatthew Dillon * stopped (CR goes inactive) and the port must be stopped 250122181ab7SMatthew Dillon * and restarted. 2502258223a3SMatthew Dillon */ 2503258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 2504cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2505cec07d75SMatthew Dillon is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)); 2506cec07d75SMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2507258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 25081980eff3SMatthew Dillon (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X)); 250922181ab7SMatthew Dillon ahci_port_stop(ap, 0); 2510258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 2511258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 25121980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 251322181ab7SMatthew Dillon need = NEED_HOTPLUG_INSERT; 251422181ab7SMatthew Dillon goto fatal; 2515258223a3SMatthew Dillon } 251622181ab7SMatthew Dillon need = NEED_RESTART; 2517258223a3SMatthew Dillon break; 2518258223a3SMatthew Dillon default: 25191980eff3SMatthew Dillon if (ap->ap_type != ATA_PORT_T_NONE) { 252022181ab7SMatthew Dillon need = NEED_HOTPLUG_REMOVE; 252122181ab7SMatthew Dillon goto fatal; 2522258223a3SMatthew Dillon } 252322181ab7SMatthew Dillon need = NEED_RESTART; 2524258223a3SMatthew Dillon break; 2525258223a3SMatthew Dillon } 2526258223a3SMatthew Dillon } 2527258223a3SMatthew Dillon 252822181ab7SMatthew Dillon /* 2529f4553de1SMatthew Dillon * Check for remaining errors - they are fatal. (blockable) 253022181ab7SMatthew Dillon */ 2531258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 2532258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 2533cec07d75SMatthew Dillon u_int32_t serr; 2534cec07d75SMatthew Dillon 2535cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2536cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2537cec07d75SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2538cec07d75SMatthew Dillon AHCI_PREG_IS_UFS)); 2539cec07d75SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2540831bc9e3SMatthew Dillon kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), " 25414444122dSMatthew Dillon "disabling port.\n", 25424444122dSMatthew Dillon PORTNAME(ap), 25434444122dSMatthew Dillon is, AHCI_PFMT_IS, 25441980eff3SMatthew Dillon serr, AHCI_PFMT_SERR 25454444122dSMatthew Dillon ); 2546831bc9e3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2547831bc9e3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2548831bc9e3SMatthew Dillon AHCI_PREG_IS_UFS); 2549258223a3SMatthew Dillon /* XXX try recovery first */ 2550258223a3SMatthew Dillon goto fatal; 2551258223a3SMatthew Dillon } 2552258223a3SMatthew Dillon 255322181ab7SMatthew Dillon /* 255422181ab7SMatthew Dillon * Fail all outstanding commands if we know the port won't recover. 25551980eff3SMatthew Dillon * 25561980eff3SMatthew Dillon * We may have a ccb_at if the failed command is known and was 25571980eff3SMatthew Dillon * being sent to a device over a port multiplier (PM). In this 25581980eff3SMatthew Dillon * case if the port itself has not completely failed we fail just 25591980eff3SMatthew Dillon * the commands related to that target. 256022181ab7SMatthew Dillon */ 2561258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2562258223a3SMatthew Dillon fatal: 2563258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 2564258223a3SMatthew Dillon failall: 2565258223a3SMatthew Dillon 2566cf5f3a81SMatthew Dillon /* Stopping the port clears CI/SACT */ 2567cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 2568258223a3SMatthew Dillon 25691980eff3SMatthew Dillon /* 25701980eff3SMatthew Dillon * Error all the active slots. If running across a PM 25711980eff3SMatthew Dillon * try to error out just the slots related to the target. 25721980eff3SMatthew Dillon */ 2573258223a3SMatthew Dillon ci_masked = ci_saved & *active; 2574258223a3SMatthew Dillon while (ci_masked) { 2575258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2576258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 25771980eff3SMatthew Dillon if (ccb_at == ccb->ccb_xa.at || 25781980eff3SMatthew Dillon ap->ap_state == AP_S_FATAL_ERROR) { 2579258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2580258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2581258223a3SMatthew Dillon } 25821980eff3SMatthew Dillon } 2583258223a3SMatthew Dillon 2584258223a3SMatthew Dillon /* Run completion for all active slots. */ 2585258223a3SMatthew Dillon ci_saved &= ~*active; 2586258223a3SMatthew Dillon 2587258223a3SMatthew Dillon /* 2588258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 2589258223a3SMatthew Dillon * 2590258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 2591258223a3SMatthew Dillon * a livelock. 2592258223a3SMatthew Dillon */ 2593258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 259422181ab7SMatthew Dillon if (need == NEED_RESTART) 259522181ab7SMatthew Dillon need = NEED_NOTHING; 2596258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2597258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2598258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2599258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 2600258223a3SMatthew Dillon } 2601258223a3SMatthew Dillon } 2602258223a3SMatthew Dillon 2603258223a3SMatthew Dillon /* 2604f4553de1SMatthew Dillon * CCB completion (non blocking). 2605f4553de1SMatthew Dillon * 2606258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 2607258223a3SMatthew Dillon * changed to zero some time after we activated it. 2608258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 2609cf5f3a81SMatthew Dillon * 2610cf5f3a81SMatthew Dillon * Any active bits not saved are completed within the restrictions 2611cf5f3a81SMatthew Dillon * imposed by the caller. 2612258223a3SMatthew Dillon */ 26133209f581SMatthew Dillon ci_masked = ~ci_saved & *active; 2614258223a3SMatthew Dillon while (ci_masked) { 2615258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2616258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2617258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2618258223a3SMatthew Dillon 2619258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 2620258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 2621258223a3SMatthew Dillon " (error)" : ""); 2622258223a3SMatthew Dillon 2623258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2624258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2625258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2626258223a3SMatthew Dillon 2627258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2628258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2629258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2630258223a3SMatthew Dillon 2631258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2632258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2633258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 2634258223a3SMatthew Dillon 2635258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 26361980eff3SMatthew Dillon if (active == &ap->ap_active) { 26371980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 26381980eff3SMatthew Dillon --ap->ap_active_cnt; 26391980eff3SMatthew Dillon } 2640258223a3SMatthew Dillon ccb->ccb_done(ccb); 2641258223a3SMatthew Dillon } 2642258223a3SMatthew Dillon 2643f4553de1SMatthew Dillon /* 2644f4553de1SMatthew Dillon * Cleanup. Will not be set if non-blocking. 2645f4553de1SMatthew Dillon */ 264622181ab7SMatthew Dillon switch(need) { 264722181ab7SMatthew Dillon case NEED_RESTART: 264822181ab7SMatthew Dillon /* 264922181ab7SMatthew Dillon * A recoverable error occured and we can restart outstanding 265022181ab7SMatthew Dillon * commands on the port. 265122181ab7SMatthew Dillon */ 265217eab71eSMatthew Dillon ahci_port_start(ap); 2653258223a3SMatthew Dillon 2654258223a3SMatthew Dillon if (ci_saved) { 2655258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2656258223a3SMatthew Dillon tmp = ci_saved; 2657258223a3SMatthew Dillon while (tmp) { 2658258223a3SMatthew Dillon slot = ffs(tmp) - 1; 2659258223a3SMatthew Dillon tmp &= ~(1 << slot); 2660258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2661258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2662258223a3SMatthew Dillon KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) == 2663258223a3SMatthew Dillon (!!ap->ap_sactive)); 2664258223a3SMatthew Dillon } 2665258223a3SMatthew Dillon #endif 2666258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr " 2667258223a3SMatthew Dillon "re-enabling%s slots %08x\n", PORTNAME(ap), 2668258223a3SMatthew Dillon ap->ap_sactive ? " NCQ" : "", ci_saved); 2669258223a3SMatthew Dillon 2670258223a3SMatthew Dillon if (ap->ap_sactive) 2671258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 2672258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 2673258223a3SMatthew Dillon } 267422181ab7SMatthew Dillon break; 267522181ab7SMatthew Dillon case NEED_HOTPLUG_INSERT: 267622181ab7SMatthew Dillon /* 2677cf5f3a81SMatthew Dillon * A hot-plug insertion event has occured and all 2678cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 26791980eff3SMatthew Dillon * 26801980eff3SMatthew Dillon * Don't recurse if this occurs while we are 26811980eff3SMatthew Dillon * resetting the port. 268222181ab7SMatthew Dillon */ 26831980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 268422181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 268522181ab7SMatthew Dillon PORTNAME(ap)); 26863209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 26873209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 26881980eff3SMatthew Dillon } 268922181ab7SMatthew Dillon break; 269022181ab7SMatthew Dillon case NEED_HOTPLUG_REMOVE: 2691cf5f3a81SMatthew Dillon /* 2692cf5f3a81SMatthew Dillon * A hot-plug removal event has occured and all 2693cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 26941980eff3SMatthew Dillon * 26951980eff3SMatthew Dillon * Don't recurse if this occurs while we are 26961980eff3SMatthew Dillon * resetting the port. 2697cf5f3a81SMatthew Dillon */ 26981980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 269922181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 270022181ab7SMatthew Dillon PORTNAME(ap)); 2701cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 27023209f581SMatthew Dillon /* ap_probe set to failed */ 27033209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 27041980eff3SMatthew Dillon } 270522181ab7SMatthew Dillon break; 270622181ab7SMatthew Dillon default: 270722181ab7SMatthew Dillon break; 2708258223a3SMatthew Dillon } 2709258223a3SMatthew Dillon } 2710258223a3SMatthew Dillon 2711258223a3SMatthew Dillon struct ahci_ccb * 2712258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 2713258223a3SMatthew Dillon { 2714258223a3SMatthew Dillon struct ahci_ccb *ccb; 2715258223a3SMatthew Dillon 2716258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2717258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 2718258223a3SMatthew Dillon if (ccb != NULL) { 2719258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 2720258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 2721258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 27221980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 2723258223a3SMatthew Dillon } 2724258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2725258223a3SMatthew Dillon 2726258223a3SMatthew Dillon return (ccb); 2727258223a3SMatthew Dillon } 2728258223a3SMatthew Dillon 2729258223a3SMatthew Dillon void 2730258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 2731258223a3SMatthew Dillon { 2732258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2733258223a3SMatthew Dillon 2734258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2735258223a3SMatthew Dillon if (ccb->ccb_xa.state != ATA_S_COMPLETE && 2736258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_TIMEOUT && 2737258223a3SMatthew Dillon ccb->ccb_xa.state != ATA_S_ERROR) { 2738258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, " 2739258223a3SMatthew Dillon "slot %d\n", 2740258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), ccb->ccb_xa.state, 2741258223a3SMatthew Dillon ccb->ccb_slot); 2742258223a3SMatthew Dillon } 2743258223a3SMatthew Dillon #endif 2744258223a3SMatthew Dillon 2745258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 2746258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2747258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 2748258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2749258223a3SMatthew Dillon } 2750258223a3SMatthew Dillon 2751258223a3SMatthew Dillon struct ahci_ccb * 2752258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 2753258223a3SMatthew Dillon { 2754258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 2755258223a3SMatthew Dillon u_int32_t sact; 2756258223a3SMatthew Dillon 2757258223a3SMatthew Dillon /* No commands may be active on the chip. */ 2758258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 2759258223a3SMatthew Dillon if (sact != 0) 2760258223a3SMatthew Dillon kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact); 2761258223a3SMatthew Dillon KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0); 2762258223a3SMatthew Dillon 2763258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2764258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy == 0); 2765258223a3SMatthew Dillon ap->ap_err_busy = 1; 2766258223a3SMatthew Dillon #endif 2767258223a3SMatthew Dillon /* Save outstanding command state. */ 2768258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 2769258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 2770258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 2771258223a3SMatthew Dillon 2772258223a3SMatthew Dillon /* 2773258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 2774258223a3SMatthew Dillon * run prematurely. 2775258223a3SMatthew Dillon */ 2776258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 2777258223a3SMatthew Dillon 2778258223a3SMatthew Dillon /* 2779258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 2780258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 2781258223a3SMatthew Dillon */ 2782258223a3SMatthew Dillon err_ccb = ahci_get_ccb(ap); 2783258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 2784258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 2785258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 2786258223a3SMatthew Dillon 2787258223a3SMatthew Dillon return err_ccb; 2788258223a3SMatthew Dillon } 2789258223a3SMatthew Dillon 2790258223a3SMatthew Dillon void 2791258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 2792258223a3SMatthew Dillon { 2793258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2794258223a3SMatthew Dillon u_int32_t sact; 27955f8c1efdSMatthew Dillon u_int32_t ci; 2796258223a3SMatthew Dillon 2797258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2798258223a3SMatthew Dillon KKASSERT(ap->ap_err_busy); 2799258223a3SMatthew Dillon #endif 28005f8c1efdSMatthew Dillon /* 28015f8c1efdSMatthew Dillon * No commands may be active on the chip 28025f8c1efdSMatthew Dillon */ 2803258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 28045f8c1efdSMatthew Dillon if (sact) { 28055f8c1efdSMatthew Dillon panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n", 28065f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 2807258223a3SMatthew Dillon } 28085f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 28095f8c1efdSMatthew Dillon if (ci) { 2810cf5f3a81SMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0 " 2811cf5f3a81SMatthew Dillon "(act=%08x sact=%08x)\n", 2812cf5f3a81SMatthew Dillon ccb->ccb_slot, ci, 2813cf5f3a81SMatthew Dillon ap->ap_active, ap->ap_sactive); 28145f8c1efdSMatthew Dillon } 2815258223a3SMatthew Dillon 2816258223a3SMatthew Dillon /* Done with the CCB */ 2817258223a3SMatthew Dillon ahci_put_ccb(ccb); 2818258223a3SMatthew Dillon 2819258223a3SMatthew Dillon /* Restore outstanding command state */ 2820258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 2821258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 2822258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 2823258223a3SMatthew Dillon 2824258223a3SMatthew Dillon #ifdef DIAGNOSTIC 2825258223a3SMatthew Dillon ap->ap_err_busy = 0; 2826258223a3SMatthew Dillon #endif 2827258223a3SMatthew Dillon } 2828258223a3SMatthew Dillon 28291980eff3SMatthew Dillon /* 28301980eff3SMatthew Dillon * Read log page to get NCQ error. 28311980eff3SMatthew Dillon * 28321980eff3SMatthew Dillon * NOTE: NCQ not currently supported on port multipliers. XXX 28331980eff3SMatthew Dillon */ 2834258223a3SMatthew Dillon int 2835258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp) 2836258223a3SMatthew Dillon { 2837258223a3SMatthew Dillon struct ahci_ccb *ccb; 2838258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 2839258223a3SMatthew Dillon u_int32_t cmd; 2840258223a3SMatthew Dillon struct ata_fis_h2d *fis; 2841258223a3SMatthew Dillon int rc = EIO; 2842258223a3SMatthew Dillon 2843258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap)); 2844258223a3SMatthew Dillon 2845258223a3SMatthew Dillon /* Save command register state. */ 2846258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 2847258223a3SMatthew Dillon 2848258223a3SMatthew Dillon /* Port should have been idled already. Start it. */ 2849258223a3SMatthew Dillon KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0); 285017eab71eSMatthew Dillon ahci_port_start(ap); 2851258223a3SMatthew Dillon 2852258223a3SMatthew Dillon /* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */ 2853258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 2854258223a3SMatthew Dillon ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL; 2855258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 2856258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 2857258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 2858258223a3SMatthew Dillon bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table)); 2859258223a3SMatthew Dillon 2860258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 2861258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 2862258223a3SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD; 2863258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 2864258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 2865258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 2866258223a3SMatthew Dillon fis->sector_count_exp = 0; 2867258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 2868258223a3SMatthew Dillon fis->lba_mid_exp = 0; 2869258223a3SMatthew Dillon fis->device = 0; 2870258223a3SMatthew Dillon 2871258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 2872258223a3SMatthew Dillon 2873258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 2874258223a3SMatthew Dillon rc = ENOMEM; /* XXX caller must abort all commands */ 2875258223a3SMatthew Dillon goto err; 2876258223a3SMatthew Dillon } 2877258223a3SMatthew Dillon 2878258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 2879831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != 0) 2880258223a3SMatthew Dillon goto err; 2881258223a3SMatthew Dillon 2882258223a3SMatthew Dillon rc = 0; 2883258223a3SMatthew Dillon err: 2884258223a3SMatthew Dillon /* Abort our command, if it failed, by stopping command DMA. */ 2885831bc9e3SMatthew Dillon if (rc) { 2886258223a3SMatthew Dillon kprintf("%s: log page read failed, slot %d was still active.\n", 2887258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 2888258223a3SMatthew Dillon } 2889258223a3SMatthew Dillon 2890258223a3SMatthew Dillon /* Done with the error CCB now. */ 2891258223a3SMatthew Dillon ahci_unload_prdt(ccb); 2892258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 2893258223a3SMatthew Dillon 2894258223a3SMatthew Dillon /* Extract failed register set and tags from the scratch space. */ 2895258223a3SMatthew Dillon if (rc == 0) { 2896258223a3SMatthew Dillon struct ata_log_page_10h *log; 2897258223a3SMatthew Dillon int err_slot; 2898258223a3SMatthew Dillon 2899258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 2900258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 2901258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 2902258223a3SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ " 2903258223a3SMatthew Dillon "error?\n", 2904258223a3SMatthew Dillon PORTNAME(ap)); 2905258223a3SMatthew Dillon rc = ESRCH; 2906258223a3SMatthew Dillon } else { 2907258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 2908258223a3SMatthew Dillon *err_slotp = err_slot = log->err_regs.type & 2909258223a3SMatthew Dillon ATA_LOG_10H_TYPE_TAG_MASK; 2910258223a3SMatthew Dillon 2911258223a3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2912258223a3SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, &log->err_regs, 2913258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 2914258223a3SMatthew Dillon ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 2915258223a3SMatthew Dillon ccb->ccb_xa.rfis.flags = 0; 2916258223a3SMatthew Dillon } 2917258223a3SMatthew Dillon } 2918258223a3SMatthew Dillon 2919258223a3SMatthew Dillon /* Restore saved CMD register state */ 2920258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 2921258223a3SMatthew Dillon 2922258223a3SMatthew Dillon return (rc); 2923258223a3SMatthew Dillon } 2924258223a3SMatthew Dillon 2925258223a3SMatthew Dillon /* 2926258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 2927258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 2928258223a3SMatthew Dillon * single physical base address. 2929258223a3SMatthew Dillon */ 2930258223a3SMatthew Dillon struct ahci_dmamem * 2931258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 2932258223a3SMatthew Dillon { 2933258223a3SMatthew Dillon struct ahci_dmamem *adm; 2934258223a3SMatthew Dillon int error; 2935258223a3SMatthew Dillon 2936258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 2937258223a3SMatthew Dillon 2938258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 2939258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 2940258223a3SMatthew Dillon if (error == 0) { 2941258223a3SMatthew Dillon adm->adm_tag = tag; 2942258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 2943258223a3SMatthew Dillon adm->adm_kva, 2944258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 2945258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 2946258223a3SMatthew Dillon 0); 2947258223a3SMatthew Dillon } 2948258223a3SMatthew Dillon if (error) { 2949258223a3SMatthew Dillon if (adm->adm_map) { 2950258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 2951258223a3SMatthew Dillon adm->adm_map = NULL; 2952258223a3SMatthew Dillon adm->adm_tag = NULL; 2953258223a3SMatthew Dillon adm->adm_kva = NULL; 2954258223a3SMatthew Dillon } 2955258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2956258223a3SMatthew Dillon adm = NULL; 2957258223a3SMatthew Dillon } 2958258223a3SMatthew Dillon return (adm); 2959258223a3SMatthew Dillon } 2960258223a3SMatthew Dillon 2961258223a3SMatthew Dillon static 2962258223a3SMatthew Dillon void 2963258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 2964258223a3SMatthew Dillon { 2965258223a3SMatthew Dillon KKASSERT(error == 0); 2966258223a3SMatthew Dillon KKASSERT(nsegs == 1); 2967258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 2968258223a3SMatthew Dillon } 2969258223a3SMatthew Dillon 2970258223a3SMatthew Dillon 2971258223a3SMatthew Dillon void 2972258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 2973258223a3SMatthew Dillon { 2974258223a3SMatthew Dillon if (adm->adm_map) { 2975258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 2976258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 2977258223a3SMatthew Dillon adm->adm_map = NULL; 2978258223a3SMatthew Dillon adm->adm_tag = NULL; 2979258223a3SMatthew Dillon adm->adm_kva = NULL; 2980258223a3SMatthew Dillon } 2981258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 2982258223a3SMatthew Dillon } 2983258223a3SMatthew Dillon 2984258223a3SMatthew Dillon u_int32_t 2985258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 2986258223a3SMatthew Dillon { 2987258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2988258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 2989258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 2990258223a3SMatthew Dillon } 2991258223a3SMatthew Dillon 2992258223a3SMatthew Dillon void 2993258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 2994258223a3SMatthew Dillon { 2995258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 2996258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 2997258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 2998258223a3SMatthew Dillon } 2999258223a3SMatthew Dillon 3000258223a3SMatthew Dillon u_int32_t 3001258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 3002258223a3SMatthew Dillon { 3003258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3004258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 3005258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 3006258223a3SMatthew Dillon } 3007258223a3SMatthew Dillon 3008258223a3SMatthew Dillon void 3009258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 3010258223a3SMatthew Dillon { 3011258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 3012258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3013258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 3014258223a3SMatthew Dillon } 3015258223a3SMatthew Dillon 3016831bc9e3SMatthew Dillon /* 3017831bc9e3SMatthew Dillon * Wait up to (timeout) milliseconds for the masked port register to 3018831bc9e3SMatthew Dillon * match the target. 3019831bc9e3SMatthew Dillon * 3020831bc9e3SMatthew Dillon * Timeout is in milliseconds. 3021831bc9e3SMatthew Dillon */ 3022258223a3SMatthew Dillon int 3023cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 3024cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 3025258223a3SMatthew Dillon { 3026831bc9e3SMatthew Dillon int t; 3027258223a3SMatthew Dillon 3028831bc9e3SMatthew Dillon /* 3029831bc9e3SMatthew Dillon * Loop hard up to 100uS 3030831bc9e3SMatthew Dillon */ 3031831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3032258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3033258223a3SMatthew Dillon return (0); 3034831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3035258223a3SMatthew Dillon } 3036258223a3SMatthew Dillon 3037831bc9e3SMatthew Dillon do { 3038831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 3039831bc9e3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3040831bc9e3SMatthew Dillon return (0); 3041831bc9e3SMatthew Dillon } while (timeout > 0); 3042831bc9e3SMatthew Dillon return (1); 3043831bc9e3SMatthew Dillon } 3044831bc9e3SMatthew Dillon 3045831bc9e3SMatthew Dillon int 3046831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 3047831bc9e3SMatthew Dillon u_int32_t target) 3048831bc9e3SMatthew Dillon { 3049831bc9e3SMatthew Dillon int t; 3050831bc9e3SMatthew Dillon 3051831bc9e3SMatthew Dillon /* 3052831bc9e3SMatthew Dillon * Loop hard up to 100uS 3053831bc9e3SMatthew Dillon */ 3054831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3055831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3056831bc9e3SMatthew Dillon return (0); 3057831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3058831bc9e3SMatthew Dillon } 3059831bc9e3SMatthew Dillon 3060831bc9e3SMatthew Dillon /* 3061831bc9e3SMatthew Dillon * And one millisecond the slow way 3062831bc9e3SMatthew Dillon */ 3063831bc9e3SMatthew Dillon t = 1000; 3064831bc9e3SMatthew Dillon do { 3065831bc9e3SMatthew Dillon t -= ahci_os_softsleep(); 3066831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3067831bc9e3SMatthew Dillon return (0); 3068831bc9e3SMatthew Dillon } while (t > 0); 3069831bc9e3SMatthew Dillon 3070258223a3SMatthew Dillon return (1); 3071258223a3SMatthew Dillon } 3072258223a3SMatthew Dillon 3073831bc9e3SMatthew Dillon 30741980eff3SMatthew Dillon /* 30751980eff3SMatthew Dillon * Acquire an ata transfer. 30761980eff3SMatthew Dillon * 30771980eff3SMatthew Dillon * Pass a NULL at for direct-attached transfers, and a non-NULL at for 30781980eff3SMatthew Dillon * targets that go through the port multiplier. 30791980eff3SMatthew Dillon */ 3080258223a3SMatthew Dillon struct ata_xfer * 30811980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at) 3082258223a3SMatthew Dillon { 3083258223a3SMatthew Dillon struct ahci_ccb *ccb; 3084258223a3SMatthew Dillon 3085258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 3086258223a3SMatthew Dillon if (ccb == NULL) { 3087258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 3088258223a3SMatthew Dillon PORTNAME(ap)); 3089258223a3SMatthew Dillon return (NULL); 3090258223a3SMatthew Dillon } 3091258223a3SMatthew Dillon 3092258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 3093258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 3094258223a3SMatthew Dillon 30951980eff3SMatthew Dillon ccb->ccb_xa.at = at; 3096258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 3097258223a3SMatthew Dillon 3098258223a3SMatthew Dillon return (&ccb->ccb_xa); 3099258223a3SMatthew Dillon } 3100258223a3SMatthew Dillon 3101258223a3SMatthew Dillon void 3102258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 3103258223a3SMatthew Dillon { 3104258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3105258223a3SMatthew Dillon 3106258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 3107258223a3SMatthew Dillon 3108258223a3SMatthew Dillon ahci_put_ccb(ccb); 3109258223a3SMatthew Dillon } 3110258223a3SMatthew Dillon 3111258223a3SMatthew Dillon int 3112258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 3113258223a3SMatthew Dillon { 3114258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3115258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 3116258223a3SMatthew Dillon 3117258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 3118258223a3SMatthew Dillon 3119258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 3120258223a3SMatthew Dillon goto failcmd; 31211980eff3SMatthew Dillon #if 0 31221980eff3SMatthew Dillon kprintf("%s: started std command %b ccb %d ccb_at %p %d\n", 31231980eff3SMatthew Dillon ATANAME(ccb->ccb_port, ccb->ccb_xa.at), 31241980eff3SMatthew Dillon ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD, 31251980eff3SMatthew Dillon ccb->ccb_slot, 31261980eff3SMatthew Dillon ccb->ccb_xa.at, 31271980eff3SMatthew Dillon ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1); 31281980eff3SMatthew Dillon #endif 3129258223a3SMatthew Dillon 3130258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 3131258223a3SMatthew Dillon 3132258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 3133258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 31341980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 31351980eff3SMatthew Dillon cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target << 31361980eff3SMatthew Dillon AHCI_CMD_LIST_FLAG_PMP_SHIFT); 31371980eff3SMatthew Dillon } 3138258223a3SMatthew Dillon 3139258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 3140258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 3141258223a3SMatthew Dillon 3142258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 3143258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 3144258223a3SMatthew Dillon 3145258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 3146258223a3SMatthew Dillon goto failcmd; 3147258223a3SMatthew Dillon 3148258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 3149258223a3SMatthew Dillon 3150831bc9e3SMatthew Dillon if (xa->flags & ATA_F_POLL) 3151831bc9e3SMatthew Dillon return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout)); 3152258223a3SMatthew Dillon 3153258223a3SMatthew Dillon crit_enter(); 3154f4553de1SMatthew Dillon KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0); 31553209f581SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_DESIRED; 3156258223a3SMatthew Dillon ahci_start(ccb); 3157258223a3SMatthew Dillon crit_exit(); 3158831bc9e3SMatthew Dillon return (xa->state); 3159258223a3SMatthew Dillon 3160258223a3SMatthew Dillon failcmd: 3161258223a3SMatthew Dillon crit_enter(); 3162258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 3163258223a3SMatthew Dillon xa->complete(xa); 3164258223a3SMatthew Dillon crit_exit(); 3165831bc9e3SMatthew Dillon return (ATA_S_ERROR); 3166258223a3SMatthew Dillon } 3167258223a3SMatthew Dillon 3168258223a3SMatthew Dillon void 3169258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 3170258223a3SMatthew Dillon { 3171258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3172258223a3SMatthew Dillon 3173831bc9e3SMatthew Dillon /* 3174831bc9e3SMatthew Dillon * NOTE: callout does not lock port and may race us modifying 3175831bc9e3SMatthew Dillon * the flags, so make sure its stopped. 3176831bc9e3SMatthew Dillon */ 3177258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 3178258223a3SMatthew Dillon callout_stop(&ccb->ccb_timeout); 3179831bc9e3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 3180258223a3SMatthew Dillon } 3181f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED); 3182258223a3SMatthew Dillon 3183f4553de1SMatthew Dillon if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) { 3184258223a3SMatthew Dillon ahci_issue_pending_commands(ccb->ccb_port, 3185258223a3SMatthew Dillon xa->flags & ATA_F_NCQ); 3186f4553de1SMatthew Dillon } 3187258223a3SMatthew Dillon 3188258223a3SMatthew Dillon ahci_unload_prdt(ccb); 3189258223a3SMatthew Dillon 3190258223a3SMatthew Dillon if (xa->state == ATA_S_ONCHIP) 3191258223a3SMatthew Dillon xa->state = ATA_S_COMPLETE; 3192258223a3SMatthew Dillon #ifdef DIAGNOSTIC 3193258223a3SMatthew Dillon else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT) 3194258223a3SMatthew Dillon kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, " 3195258223a3SMatthew Dillon "slot %d\n", 3196258223a3SMatthew Dillon PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot); 3197258223a3SMatthew Dillon #endif 3198258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 3199258223a3SMatthew Dillon xa->complete(xa); 3200258223a3SMatthew Dillon } 3201258223a3SMatthew Dillon 3202f4553de1SMatthew Dillon /* 3203f4553de1SMatthew Dillon * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags 3204f4553de1SMatthew Dillon * while the callout is runing. 3205f4553de1SMatthew Dillon * 3206f4553de1SMatthew Dillon * We can't safely get the port lock here or delay, we could block 3207f4553de1SMatthew Dillon * the callout thread. 3208f4553de1SMatthew Dillon */ 3209258223a3SMatthew Dillon static void 3210258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 3211258223a3SMatthew Dillon { 3212258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 3213258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3214258223a3SMatthew Dillon 3215f4553de1SMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 3216f4553de1SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED; 3217f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT); 3218258223a3SMatthew Dillon } 3219258223a3SMatthew Dillon 32201980eff3SMatthew Dillon void 3221831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb) 3222258223a3SMatthew Dillon { 3223258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3224258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3225258223a3SMatthew Dillon volatile u_int32_t *active; 3226258223a3SMatthew Dillon int ccb_was_started, ncq_cmd; 3227131be210SMatthew Dillon int status; 3228258223a3SMatthew Dillon 3229258223a3SMatthew Dillon crit_enter(); 3230831bc9e3SMatthew Dillon kprintf("%s: CMD TIMEOUT state=%d cmd-reg 0x%b\n" 3231cf5f3a81SMatthew Dillon "\tsactive=%08x active=%08x\n" 3232258223a3SMatthew Dillon "\t sact=%08x ci=%08x\n", 32331980eff3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 3234831bc9e3SMatthew Dillon ccb->ccb_xa.state, 3235258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 3236cf5f3a81SMatthew Dillon ap->ap_sactive, ap->ap_active, 3237258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 3238258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI)); 3239258223a3SMatthew Dillon 32409e145b23SMatthew Dillon /* 32419e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 32423209f581SMatthew Dillon * If we got here at least one of these flags should be set. 32439e145b23SMatthew Dillon */ 32443209f581SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED | 32453209f581SMatthew Dillon ATA_F_TIMEOUT_RUNNING)); 3246f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED); 3247258223a3SMatthew Dillon ncq_cmd = (xa->flags & ATA_F_NCQ); 3248258223a3SMatthew Dillon active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active; 3249258223a3SMatthew Dillon 3250258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 3251258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out " 3252258223a3SMatthew Dillon "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot); 3253258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 3254258223a3SMatthew Dillon ccb_was_started = 0; 3255258223a3SMatthew Dillon } else if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 3256258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already " 3257258223a3SMatthew Dillon "handled%s\n", PORTNAME(ap), ccb->ccb_slot, 3258258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot)) ? 3259258223a3SMatthew Dillon " but slot is still active?" : "."); 3260258223a3SMatthew Dillon goto ret; 3261258223a3SMatthew Dillon } else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) & 3262258223a3SMatthew Dillon (1 << ccb->ccb_slot)) == 0 && 3263258223a3SMatthew Dillon (*active & (1 << ccb->ccb_slot))) { 3264831bc9e3SMatthew Dillon kprintf("%s: ahci_port_intr() failed to detect " 3265831bc9e3SMatthew Dillon "completed slot\n", ATANAME(ap, ccb->ccb_xa.at)); 3266258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 32671980eff3SMatthew Dillon if (ncq_cmd == 0) { 32681980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 32691980eff3SMatthew Dillon --ap->ap_active_cnt; 32701980eff3SMatthew Dillon } 3271258223a3SMatthew Dillon ccb->ccb_done(ccb); 3272258223a3SMatthew Dillon goto ret; 3273258223a3SMatthew Dillon } else { 3274258223a3SMatthew Dillon ccb_was_started = 1; 3275258223a3SMatthew Dillon } 3276258223a3SMatthew Dillon 3277258223a3SMatthew Dillon /* Complete the slot with a timeout error. */ 3278258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3279258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 32801980eff3SMatthew Dillon if (ncq_cmd == 0) { 32811980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 32821980eff3SMatthew Dillon --ap->ap_active_cnt; 32831980eff3SMatthew Dillon } 3284258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap)); 3285258223a3SMatthew Dillon ccb->ccb_done(ccb); /* This won't issue pending commands or run the 3286258223a3SMatthew Dillon atascsi completion. */ 3287258223a3SMatthew Dillon 3288258223a3SMatthew Dillon /* Reset port to abort running command. */ 3289258223a3SMatthew Dillon if (ccb_was_started) { 3290258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command " 3291258223a3SMatthew Dillon "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" 3292258223a3SMatthew Dillon : "", ccb->ccb_slot, *active); 32931980eff3SMatthew Dillon /* XXX */ 32943209f581SMatthew Dillon if (ccb->ccb_xa.at && ap->ap_type == ATA_PORT_T_PM) { 32951980eff3SMatthew Dillon /* XXX how do we unbrick a PM target? */ 3296131be210SMatthew Dillon kprintf("%s: PM target bricked and timed-out, " 3297131be210SMatthew Dillon "disabling PM target but trying to " 3298131be210SMatthew Dillon "leave the port intact\n", 3299131be210SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at)); 3300131be210SMatthew Dillon ccb->ccb_xa.at->at_probe = ATA_PROBE_FAILED; 3301f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 3302131be210SMatthew Dillon ahci_port_stop(ap, 0); 3303131be210SMatthew Dillon ahci_port_clo(ap); 3304131be210SMatthew Dillon ahci_port_start(ap); 3305131be210SMatthew Dillon status = 0; 33061980eff3SMatthew Dillon } else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) { 3307cf5f3a81SMatthew Dillon /* 3308cf5f3a81SMatthew Dillon * If the softreset failed place the port in a 3309cf5f3a81SMatthew Dillon * failed state and use ahci_port_intr() to cancel 3310cf5f3a81SMatthew Dillon * any remaining commands. 3311cf5f3a81SMatthew Dillon */ 331217eab71eSMatthew Dillon kprintf("%s: Unable to reset during timeout, port " 331317eab71eSMatthew Dillon "bricked on us\n", 3314258223a3SMatthew Dillon PORTNAME(ap)); 3315258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 3316f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 3317131be210SMatthew Dillon status = 1; 3318cf5f3a81SMatthew Dillon } else { 3319131be210SMatthew Dillon status = 0; 3320131be210SMatthew Dillon } 3321131be210SMatthew Dillon if (status == 0) { 3322cf5f3a81SMatthew Dillon /* 3323cf5f3a81SMatthew Dillon * Restart any other commands that were aborted 3324cf5f3a81SMatthew Dillon * by the reset. 3325cf5f3a81SMatthew Dillon */ 3326258223a3SMatthew Dillon if (*active) { 3327258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots " 3328258223a3SMatthew Dillon "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "", 3329258223a3SMatthew Dillon *active); 3330258223a3SMatthew Dillon if (ncq_cmd) 3331258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, *active); 3332258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, *active); 3333258223a3SMatthew Dillon } 3334258223a3SMatthew Dillon } 3335cf5f3a81SMatthew Dillon } 3336258223a3SMatthew Dillon 3337258223a3SMatthew Dillon /* Issue any pending commands now. */ 3338258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap)); 3339258223a3SMatthew Dillon if (ccb_was_started) 3340258223a3SMatthew Dillon ahci_issue_pending_commands(ap, ncq_cmd); 3341258223a3SMatthew Dillon else if (ap->ap_active == 0) 3342258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(ap); 3343258223a3SMatthew Dillon 3344258223a3SMatthew Dillon /* Complete the timed out ata_xfer I/O (may generate new I/O). */ 3345258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap)); 3346258223a3SMatthew Dillon xa->complete(xa); 3347258223a3SMatthew Dillon 3348258223a3SMatthew Dillon DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap)); 3349258223a3SMatthew Dillon ret: 3350258223a3SMatthew Dillon crit_exit(); 3351258223a3SMatthew Dillon } 3352258223a3SMatthew Dillon 3353831bc9e3SMatthew Dillon /* 3354831bc9e3SMatthew Dillon * Used by the softreset, pmprobe, and read_ncq_error only, in very 3355831bc9e3SMatthew Dillon * specialized, controlled circumstances. 3356831bc9e3SMatthew Dillon * 3357831bc9e3SMatthew Dillon * Only one command may be pending. 3358831bc9e3SMatthew Dillon */ 3359831bc9e3SMatthew Dillon void 3360831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb) 3361831bc9e3SMatthew Dillon { 3362831bc9e3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3363831bc9e3SMatthew Dillon 3364831bc9e3SMatthew Dillon switch (ccb->ccb_xa.state) { 3365831bc9e3SMatthew Dillon case ATA_S_PENDING: 3366831bc9e3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 3367831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3368831bc9e3SMatthew Dillon break; 3369831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 3370831bc9e3SMatthew Dillon KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) && 3371831bc9e3SMatthew Dillon ap->ap_sactive == 0); 3372831bc9e3SMatthew Dillon ahci_port_stop(ap, 0); 3373831bc9e3SMatthew Dillon ahci_port_start(ap); 3374831bc9e3SMatthew Dillon 3375831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3376831bc9e3SMatthew Dillon ap->ap_active &= ~(1 << ccb->ccb_slot); 3377831bc9e3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 3378831bc9e3SMatthew Dillon --ap->ap_active_cnt; 3379831bc9e3SMatthew Dillon break; 3380831bc9e3SMatthew Dillon default: 3381831bc9e3SMatthew Dillon panic("%s: ahci_quick_timeout: ccb in bad state %d", 3382831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state); 3383831bc9e3SMatthew Dillon } 3384831bc9e3SMatthew Dillon } 3385831bc9e3SMatthew Dillon 3386258223a3SMatthew Dillon void 3387258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 3388258223a3SMatthew Dillon { 3389831bc9e3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) 3390258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 3391258223a3SMatthew Dillon } 3392