1258223a3SMatthew Dillon /* 2fb00c6edSMatthew Dillon * (MPSAFE) 3fb00c6edSMatthew Dillon * 4258223a3SMatthew Dillon * Copyright (c) 2006 David Gwynne <dlg@openbsd.org> 5258223a3SMatthew Dillon * 6258223a3SMatthew Dillon * Permission to use, copy, modify, and distribute this software for any 7258223a3SMatthew Dillon * purpose with or without fee is hereby granted, provided that the above 8258223a3SMatthew Dillon * copyright notice and this permission notice appear in all copies. 9258223a3SMatthew Dillon * 10258223a3SMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11258223a3SMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12258223a3SMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13258223a3SMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14258223a3SMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15258223a3SMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16258223a3SMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17258223a3SMatthew Dillon * 18258223a3SMatthew Dillon * 19258223a3SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved. 20258223a3SMatthew Dillon * 21258223a3SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 22258223a3SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 23258223a3SMatthew Dillon * 24258223a3SMatthew Dillon * Redistribution and use in source and binary forms, with or without 25258223a3SMatthew Dillon * modification, are permitted provided that the following conditions 26258223a3SMatthew Dillon * are met: 27258223a3SMatthew Dillon * 28258223a3SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 29258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer. 30258223a3SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 31258223a3SMatthew Dillon * notice, this list of conditions and the following disclaimer in 32258223a3SMatthew Dillon * the documentation and/or other materials provided with the 33258223a3SMatthew Dillon * distribution. 34258223a3SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 35258223a3SMatthew Dillon * contributors may be used to endorse or promote products derived 36258223a3SMatthew Dillon * from this software without specific, prior written permission. 37258223a3SMatthew Dillon * 38258223a3SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 39258223a3SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 40258223a3SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 41258223a3SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 42258223a3SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 43258223a3SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 44258223a3SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45258223a3SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 46258223a3SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 47258223a3SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 48258223a3SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49258223a3SMatthew Dillon * SUCH DAMAGE. 50258223a3SMatthew Dillon * 51258223a3SMatthew Dillon * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $ 52258223a3SMatthew Dillon */ 53258223a3SMatthew Dillon 54258223a3SMatthew Dillon #include "ahci.h" 55258223a3SMatthew Dillon 56f4553de1SMatthew Dillon void ahci_port_interrupt_enable(struct ahci_port *ap); 57258223a3SMatthew Dillon 58258223a3SMatthew Dillon int ahci_load_prdt(struct ahci_ccb *); 59258223a3SMatthew Dillon void ahci_unload_prdt(struct ahci_ccb *); 60258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, 61258223a3SMatthew Dillon int nsegs, int error); 62258223a3SMatthew Dillon void ahci_start(struct ahci_ccb *); 6317eab71eSMatthew Dillon int ahci_port_softreset(struct ahci_port *ap); 641980eff3SMatthew Dillon int ahci_port_hardreset(struct ahci_port *ap, int hard); 65cf5f3a81SMatthew Dillon void ahci_port_hardstop(struct ahci_port *ap); 66258223a3SMatthew Dillon 67831bc9e3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *); 68831bc9e3SMatthew Dillon void ahci_check_active_timeouts(struct ahci_port *ap); 69258223a3SMatthew Dillon 70831bc9e3SMatthew Dillon void ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at); 71831bc9e3SMatthew Dillon void ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at); 724c339a5fSMatthew Dillon void ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb); 734c339a5fSMatthew Dillon void ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t mask); 74258223a3SMatthew Dillon 7512feb904SMatthew Dillon int ahci_port_read_ncq_error(struct ahci_port *, int); 76258223a3SMatthew Dillon 77258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag); 78258223a3SMatthew Dillon void ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *); 79258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error); 80258223a3SMatthew Dillon 8112feb904SMatthew Dillon static void ahci_dummy_done(struct ata_xfer *xa); 8212feb904SMatthew Dillon static void ahci_empty_done(struct ahci_ccb *ccb); 8312feb904SMatthew Dillon static void ahci_ata_cmd_done(struct ahci_ccb *ccb); 84492bffafSMatthew Dillon static u_int32_t ahci_pactive(struct ahci_port *ap); 85258223a3SMatthew Dillon 86fd8bd957SMatthew Dillon /* 87fd8bd957SMatthew Dillon * Initialize the global AHCI hardware. This code does not set up any of 88fd8bd957SMatthew Dillon * its ports. 89fd8bd957SMatthew Dillon */ 90258223a3SMatthew Dillon int 91258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc) 92258223a3SMatthew Dillon { 934b450139SMatthew Dillon u_int32_t pi, pleft; 944b450139SMatthew Dillon u_int32_t bios_cap, vers; 95831bc9e3SMatthew Dillon int i; 96831bc9e3SMatthew Dillon struct ahci_port *ap; 97258223a3SMatthew Dillon 98258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b", 99258223a3SMatthew Dillon ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC); 100258223a3SMatthew Dillon 101b012a2caSMatthew Dillon /* 1024b450139SMatthew Dillon * AHCI version. 1034b450139SMatthew Dillon */ 1044b450139SMatthew Dillon vers = ahci_read(sc, AHCI_REG_VS); 1054b450139SMatthew Dillon 1064b450139SMatthew Dillon /* 107b012a2caSMatthew Dillon * save BIOS initialised parameters, enable staggered spin up 108b012a2caSMatthew Dillon */ 1094b450139SMatthew Dillon bios_cap = ahci_read(sc, AHCI_REG_CAP); 1104b450139SMatthew Dillon bios_cap &= AHCI_REG_CAP_SMPS | AHCI_REG_CAP_SSS; 1114b450139SMatthew Dillon 112258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 113258223a3SMatthew Dillon 114831bc9e3SMatthew Dillon /* 115b012a2caSMatthew Dillon * Unconditionally reset the controller, do not conditionalize on 116b012a2caSMatthew Dillon * trying to figure it if it was previously active or not. 117b012a2caSMatthew Dillon * 118b012a2caSMatthew Dillon * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section 119b012a2caSMatthew Dillon * 5.2.2.1 regarding this. HR should be set to 1 only after 120b012a2caSMatthew Dillon * AE is set to 1. The reset sequence will clear HR when 121b012a2caSMatthew Dillon * it completes, and will also clear AE if SAM is 0. AE must 122b012a2caSMatthew Dillon * then be set again. When SAM is 1 the AE bit typically reads 123b012a2caSMatthew Dillon * as 1 (and is read-only). 124b012a2caSMatthew Dillon * 125b012a2caSMatthew Dillon * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads, 126b012a2caSMatthew Dillon * otherwise the writes will only be separated by a few 127b012a2caSMatthew Dillon * nanoseconds. 128b012a2caSMatthew Dillon * 129b012a2caSMatthew Dillon * NOTE BRICKS (1) 130b012a2caSMatthew Dillon * 131b012a2caSMatthew Dillon * If you have a port multiplier and it does not have a device 132b012a2caSMatthew Dillon * in target 0, and it probes normally, but a later operation 133b012a2caSMatthew Dillon * mis-probes a target behind that PM, it is possible for the 134b012a2caSMatthew Dillon * port to brick such that only (a) a power cycle of the host 135b012a2caSMatthew Dillon * or (b) placing a device in target 0 will fix the problem. 136b012a2caSMatthew Dillon * Power cycling the PM has no effect (it works fine on another 137b012a2caSMatthew Dillon * host port). This issue is unrelated to CLO. 138b012a2caSMatthew Dillon */ 1394e21f4daSMatthew Dillon /* 1404e21f4daSMatthew Dillon * Wait for any prior reset sequence to complete 1414e21f4daSMatthew Dillon */ 1424e21f4daSMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, 1434e21f4daSMatthew Dillon AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) { 1444e21f4daSMatthew Dillon device_printf(sc->sc_dev, "Controller is stuck in reset\n"); 1454e21f4daSMatthew Dillon return (1); 1464e21f4daSMatthew Dillon } 147b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 1484e21f4daSMatthew Dillon ahci_os_sleep(500); 149b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 150b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR); 1514e21f4daSMatthew Dillon ahci_os_sleep(500); 152b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 153b012a2caSMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, 154b012a2caSMatthew Dillon AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) { 1554e21f4daSMatthew Dillon device_printf(sc->sc_dev, "unable to reset controller\n"); 156b012a2caSMatthew Dillon return (1); 157b012a2caSMatthew Dillon } 1584e21f4daSMatthew Dillon if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) { 1594e21f4daSMatthew Dillon device_printf(sc->sc_dev, "AE did not auto-clear!\n"); 1604e21f4daSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, 0); 1614e21f4daSMatthew Dillon ahci_os_sleep(500); 1624e21f4daSMatthew Dillon } 163b012a2caSMatthew Dillon 164b012a2caSMatthew Dillon /* 165b012a2caSMatthew Dillon * Enable ahci (global interrupts disabled) 166b012a2caSMatthew Dillon * 167b012a2caSMatthew Dillon * Restore saved parameters. Avoid pci transaction burst write 168b012a2caSMatthew Dillon * by issuing dummy reads. 169b012a2caSMatthew Dillon */ 1704e21f4daSMatthew Dillon ahci_os_sleep(500); 171b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 172b012a2caSMatthew Dillon ahci_os_sleep(500); 173b012a2caSMatthew Dillon 174b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 1754b450139SMatthew Dillon 1764b450139SMatthew Dillon bios_cap |= AHCI_REG_CAP_SSS; 1774b450139SMatthew Dillon ahci_write(sc, AHCI_REG_CAP, ahci_read(sc, AHCI_REG_CAP) | bios_cap); 178b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_PI, pi); 179b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 180b012a2caSMatthew Dillon 181b012a2caSMatthew Dillon /* 182b012a2caSMatthew Dillon * Intel hocus pocus in case the BIOS has not set the chip up 183b012a2caSMatthew Dillon * properly for AHCI operation. 184b012a2caSMatthew Dillon */ 185b012a2caSMatthew Dillon if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) { 186b012a2caSMatthew Dillon if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F) 187b012a2caSMatthew Dillon device_printf(sc->sc_dev, "Intel hocus pocus\n"); 188b012a2caSMatthew Dillon pci_write_config(sc->sc_dev, 0x92, 189b012a2caSMatthew Dillon pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2); 190b012a2caSMatthew Dillon } 191b012a2caSMatthew Dillon 192b012a2caSMatthew Dillon /* 193831bc9e3SMatthew Dillon * This is a hack that currently does not appear to have 194831bc9e3SMatthew Dillon * a significant effect, but I noticed the port registers 195831bc9e3SMatthew Dillon * do not appear to be completely cleared after the host 196831bc9e3SMatthew Dillon * controller is reset. 19712feb904SMatthew Dillon * 19812feb904SMatthew Dillon * Use a temporary ap structure so we can call ahci_pwrite(). 1994e21f4daSMatthew Dillon * 2004e21f4daSMatthew Dillon * We must be sure to stop the port 201831bc9e3SMatthew Dillon */ 202831bc9e3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 203831bc9e3SMatthew Dillon ap->ap_sc = sc; 20412feb904SMatthew Dillon pleft = pi; 20512feb904SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; ++i) { 20612feb904SMatthew Dillon if (pleft == 0) 20712feb904SMatthew Dillon break; 208831bc9e3SMatthew Dillon if ((pi & (1 << i)) == 0) 209831bc9e3SMatthew Dillon continue; 210831bc9e3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 211831bc9e3SMatthew Dillon AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 212831bc9e3SMatthew Dillon device_printf(sc->sc_dev, "can't map port\n"); 213831bc9e3SMatthew Dillon return (1); 214831bc9e3SMatthew Dillon } 2154e21f4daSMatthew Dillon /* 2164e21f4daSMatthew Dillon * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or 2174e21f4daSMatthew Dillon * AHCI1.1 can brick the chipset. Not only brick it, 2184e21f4daSMatthew Dillon * but also crash the PC. The bit seems unreliable 2194e21f4daSMatthew Dillon * on AHCI1.2 as well. 2204e21f4daSMatthew Dillon */ 2214e21f4daSMatthew Dillon ahci_port_stop(ap, 1); 222*9abd2bb8SImre Vadász ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable); 223831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 224831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 22512feb904SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i); 226831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 22712feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, -1); 22812feb904SMatthew Dillon sc->sc_portmask |= (1 << i); 22912feb904SMatthew Dillon pleft &= ~(1 << i); 230831bc9e3SMatthew Dillon } 23112feb904SMatthew Dillon sc->sc_numports = i; 232831bc9e3SMatthew Dillon kfree(ap, M_DEVBUF); 233831bc9e3SMatthew Dillon 234258223a3SMatthew Dillon return (0); 235258223a3SMatthew Dillon } 236258223a3SMatthew Dillon 237fd8bd957SMatthew Dillon /* 238fd8bd957SMatthew Dillon * Allocate and initialize an AHCI port. 239fd8bd957SMatthew Dillon */ 240258223a3SMatthew Dillon int 241258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port) 242258223a3SMatthew Dillon { 243258223a3SMatthew Dillon struct ahci_port *ap; 2441980eff3SMatthew Dillon struct ata_port *at; 245258223a3SMatthew Dillon struct ahci_ccb *ccb; 246258223a3SMatthew Dillon u_int64_t dva; 247258223a3SMatthew Dillon u_int32_t cmd; 24812feb904SMatthew Dillon u_int32_t data; 249258223a3SMatthew Dillon struct ahci_cmd_hdr *hdr; 250258223a3SMatthew Dillon struct ahci_cmd_table *table; 251258223a3SMatthew Dillon int rc = ENOMEM; 252258223a3SMatthew Dillon int error; 253258223a3SMatthew Dillon int i; 254258223a3SMatthew Dillon 255258223a3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 25612feb904SMatthew Dillon ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO); 257258223a3SMatthew Dillon 258258223a3SMatthew Dillon ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d", 259258223a3SMatthew Dillon device_get_name(sc->sc_dev), 260258223a3SMatthew Dillon device_get_unit(sc->sc_dev), 261258223a3SMatthew Dillon port); 262258223a3SMatthew Dillon sc->sc_ports[port] = ap; 263258223a3SMatthew Dillon 2641980eff3SMatthew Dillon /* 2651980eff3SMatthew Dillon * Allocate enough so we never have to reallocate, it makes 2661980eff3SMatthew Dillon * it easier. 2671980eff3SMatthew Dillon * 2681980eff3SMatthew Dillon * ap_pmcount will be reduced by the scan if we encounter the 2691980eff3SMatthew Dillon * port multiplier port prior to target 15. 270b012a2caSMatthew Dillon * 271b012a2caSMatthew Dillon * kmalloc power-of-2 allocations are guaranteed not to cross 272b012a2caSMatthew Dillon * a page boundary. Make sure the identify sub-structure in the 273b012a2caSMatthew Dillon * at structure does not cross a page boundary, just in case the 274b012a2caSMatthew Dillon * part is AHCI-1.1 and can't handle multiple DRQ blocks. 2751980eff3SMatthew Dillon */ 276b012a2caSMatthew Dillon if (ap->ap_ata[0] == NULL) { 277b012a2caSMatthew Dillon int pw2; 278b012a2caSMatthew Dillon 279b012a2caSMatthew Dillon for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1) 280b012a2caSMatthew Dillon ; 2811980eff3SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 282b012a2caSMatthew Dillon at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO); 283b012a2caSMatthew Dillon ap->ap_ata[i] = at; 2841980eff3SMatthew Dillon at->at_ahci_port = ap; 2851980eff3SMatthew Dillon at->at_target = i; 2863209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 287831bc9e3SMatthew Dillon at->at_features |= ATA_PORT_F_RESCAN; 2881980eff3SMatthew Dillon ksnprintf(at->at_name, sizeof(at->at_name), 2891980eff3SMatthew Dillon "%s.%d", ap->ap_name, i); 2901980eff3SMatthew Dillon } 2911980eff3SMatthew Dillon } 292258223a3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 293258223a3SMatthew Dillon AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 294258223a3SMatthew Dillon device_printf(sc->sc_dev, 295258223a3SMatthew Dillon "unable to create register window for port %d\n", 296258223a3SMatthew Dillon port); 297258223a3SMatthew Dillon goto freeport; 298258223a3SMatthew Dillon } 299258223a3SMatthew Dillon 300258223a3SMatthew Dillon ap->ap_sc = sc; 301258223a3SMatthew Dillon ap->ap_num = port; 3023209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 303f17a0cedSMatthew Dillon ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE; 304f17a0cedSMatthew Dillon ap->sysctl_tree = NULL; 305258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_free); 306258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_pending); 307258223a3SMatthew Dillon lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0); 308258223a3SMatthew Dillon 309258223a3SMatthew Dillon /* Disable port interrupts */ 310258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 311831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 312258223a3SMatthew Dillon 31317eab71eSMatthew Dillon /* 31417eab71eSMatthew Dillon * Sec 10.1.2 - deinitialise port if it is already running 31517eab71eSMatthew Dillon */ 316258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 3170be9576aSMatthew Dillon kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD); 3180be9576aSMatthew Dillon 319258223a3SMatthew Dillon if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR | 320258223a3SMatthew Dillon AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) || 321258223a3SMatthew Dillon (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) { 322258223a3SMatthew Dillon int r; 323258223a3SMatthew Dillon 324258223a3SMatthew Dillon r = ahci_port_stop(ap, 1); 325258223a3SMatthew Dillon if (r) { 326258223a3SMatthew Dillon device_printf(sc->sc_dev, 327258223a3SMatthew Dillon "unable to disable %s, ignoring port %d\n", 328258223a3SMatthew Dillon ((r == 2) ? "CR" : "FR"), port); 329258223a3SMatthew Dillon rc = ENXIO; 330258223a3SMatthew Dillon goto freeport; 331258223a3SMatthew Dillon } 332258223a3SMatthew Dillon 333258223a3SMatthew Dillon /* Write DET to zero */ 334*9abd2bb8SImre Vadász ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable); 335258223a3SMatthew Dillon } 336258223a3SMatthew Dillon 337258223a3SMatthew Dillon /* Allocate RFIS */ 338258223a3SMatthew Dillon ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis); 339258223a3SMatthew Dillon if (ap->ap_dmamem_rfis == NULL) { 340cf5f3a81SMatthew Dillon kprintf("%s: NORFIS\n", PORTNAME(ap)); 341258223a3SMatthew Dillon goto nomem; 342258223a3SMatthew Dillon } 343258223a3SMatthew Dillon 344258223a3SMatthew Dillon /* Setup RFIS base address */ 345258223a3SMatthew Dillon ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis); 346258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis); 347258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32)); 348258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva); 349258223a3SMatthew Dillon 350831bc9e3SMatthew Dillon /* Clear SERR before starting FIS reception or ST or anything */ 351831bc9e3SMatthew Dillon ahci_flush_tfd(ap); 352831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 353831bc9e3SMatthew Dillon 354258223a3SMatthew Dillon /* Enable FIS reception and activate port. */ 355258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 3561980eff3SMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 357258223a3SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 358258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 359258223a3SMatthew Dillon 360258223a3SMatthew Dillon /* Check whether port activated. Skip it if not. */ 361258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 362258223a3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 363cf5f3a81SMatthew Dillon kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap)); 364258223a3SMatthew Dillon rc = ENXIO; 365258223a3SMatthew Dillon goto freeport; 366258223a3SMatthew Dillon } 367258223a3SMatthew Dillon 368258223a3SMatthew Dillon /* Allocate a CCB for each command slot */ 369258223a3SMatthew Dillon ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF, 370258223a3SMatthew Dillon M_WAITOK | M_ZERO); 371258223a3SMatthew Dillon if (ap->ap_ccbs == NULL) { 372258223a3SMatthew Dillon device_printf(sc->sc_dev, 373258223a3SMatthew Dillon "unable to allocate command list for port %d\n", 374258223a3SMatthew Dillon port); 375258223a3SMatthew Dillon goto freeport; 376258223a3SMatthew Dillon } 377258223a3SMatthew Dillon 378258223a3SMatthew Dillon /* Command List Structures and Command Tables */ 379258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh); 380258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt); 381258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table == NULL || 382258223a3SMatthew Dillon ap->ap_dmamem_cmd_list == NULL) { 383258223a3SMatthew Dillon nomem: 384258223a3SMatthew Dillon device_printf(sc->sc_dev, 385258223a3SMatthew Dillon "unable to allocate DMA memory for port %d\n", 386258223a3SMatthew Dillon port); 387258223a3SMatthew Dillon goto freeport; 388258223a3SMatthew Dillon } 389258223a3SMatthew Dillon 390258223a3SMatthew Dillon /* Setup command list base address */ 391258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list); 392258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32)); 393258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva); 394258223a3SMatthew Dillon 395258223a3SMatthew Dillon /* Split CCB allocation into CCBs and assign to command header/table */ 396258223a3SMatthew Dillon hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list); 397258223a3SMatthew Dillon table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table); 398258223a3SMatthew Dillon for (i = 0; i < sc->sc_ncmds; i++) { 399258223a3SMatthew Dillon ccb = &ap->ap_ccbs[i]; 400258223a3SMatthew Dillon 401258223a3SMatthew Dillon error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW, 402258223a3SMatthew Dillon &ccb->ccb_dmamap); 403258223a3SMatthew Dillon if (error) { 404258223a3SMatthew Dillon device_printf(sc->sc_dev, 405258223a3SMatthew Dillon "unable to create dmamap for port %d " 406258223a3SMatthew Dillon "ccb %d\n", port, i); 407258223a3SMatthew Dillon goto freeport; 408258223a3SMatthew Dillon } 409258223a3SMatthew Dillon 410bf0ecf68SMatthew Dillon callout_init_mp(&ccb->ccb_timeout); 411258223a3SMatthew Dillon ccb->ccb_slot = i; 412258223a3SMatthew Dillon ccb->ccb_port = ap; 413258223a3SMatthew Dillon ccb->ccb_cmd_hdr = &hdr[i]; 414258223a3SMatthew Dillon ccb->ccb_cmd_table = &table[i]; 415258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) + 416258223a3SMatthew Dillon ccb->ccb_slot * sizeof(struct ahci_cmd_table); 417258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32)); 418258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva); 419258223a3SMatthew Dillon 420258223a3SMatthew Dillon ccb->ccb_xa.fis = 421258223a3SMatthew Dillon (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 422258223a3SMatthew Dillon ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd; 423258223a3SMatthew Dillon ccb->ccb_xa.tag = i; 424258223a3SMatthew Dillon 425258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 4261067474aSMatthew Dillon 4271067474aSMatthew Dillon /* 4281067474aSMatthew Dillon * CCB[1] is the error CCB and is not get or put. It is 4291067474aSMatthew Dillon * also used for probing. Numerous HBAs only load the 4301067474aSMatthew Dillon * signature from CCB[1] so it MUST be used for the second 4311067474aSMatthew Dillon * FIS. 4321067474aSMatthew Dillon */ 4331067474aSMatthew Dillon if (i == 1) 4341067474aSMatthew Dillon ap->ap_err_ccb = ccb; 4351067474aSMatthew Dillon else 436258223a3SMatthew Dillon ahci_put_ccb(ccb); 437258223a3SMatthew Dillon } 438258223a3SMatthew Dillon 43912feb904SMatthew Dillon /* 44012feb904SMatthew Dillon * Wait for ICC change to complete 44112feb904SMatthew Dillon */ 442258223a3SMatthew Dillon ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC); 443258223a3SMatthew Dillon 444fd8bd957SMatthew Dillon /* 44512feb904SMatthew Dillon * Calculate the interrupt mask 44612feb904SMatthew Dillon */ 44712feb904SMatthew Dillon data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE | 44812feb904SMatthew Dillon AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE | 44912feb904SMatthew Dillon AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE | 45012feb904SMatthew Dillon AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE | 45112feb904SMatthew Dillon AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE; 45212feb904SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 45312feb904SMatthew Dillon data |= AHCI_PREG_IE_IPME; 45412feb904SMatthew Dillon #ifdef AHCI_COALESCE 45512feb904SMatthew Dillon if (sc->sc_ccc_ports & (1 << port) 45612feb904SMatthew Dillon data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE); 45712feb904SMatthew Dillon #endif 45812feb904SMatthew Dillon ap->ap_intmask = data; 45912feb904SMatthew Dillon 46012feb904SMatthew Dillon /* 461e8cf3f55SMatthew Dillon * Start the port helper thread. The helper thread will call 462e8cf3f55SMatthew Dillon * ahci_port_init() so the ports can all be started in parallel. 463e8cf3f55SMatthew Dillon * A failure by ahci_port_init() does not deallocate the port 464e8cf3f55SMatthew Dillon * since we still want hot-plug events. 465fd8bd957SMatthew Dillon */ 466f4553de1SMatthew Dillon ahci_os_start_port(ap); 467fd8bd957SMatthew Dillon return(0); 468fd8bd957SMatthew Dillon freeport: 469fd8bd957SMatthew Dillon ahci_port_free(sc, port); 470fd8bd957SMatthew Dillon return (rc); 471fd8bd957SMatthew Dillon } 472fd8bd957SMatthew Dillon 473fd8bd957SMatthew Dillon /* 474492bffafSMatthew Dillon * [re]initialize an idle port. No CCBs should be active. (from port thread) 475fd8bd957SMatthew Dillon * 476fd8bd957SMatthew Dillon * This function is called during the initial port allocation sequence 477fd8bd957SMatthew Dillon * and is also called on hot-plug insertion. We take no chances and 478fd8bd957SMatthew Dillon * use a portreset instead of a softreset. 479fd8bd957SMatthew Dillon * 48022181ab7SMatthew Dillon * This function is the only way to move a failed port back to active 48122181ab7SMatthew Dillon * status. 48222181ab7SMatthew Dillon * 483fd8bd957SMatthew Dillon * Returns 0 if a device is successfully detected. 484fd8bd957SMatthew Dillon */ 485fd8bd957SMatthew Dillon int 48612feb904SMatthew Dillon ahci_port_init(struct ahci_port *ap) 487fd8bd957SMatthew Dillon { 488492bffafSMatthew Dillon u_int32_t cmd; 489e8cf3f55SMatthew Dillon 490e8cf3f55SMatthew Dillon /* 491492bffafSMatthew Dillon * Register [re]initialization 492492bffafSMatthew Dillon * 493f17a0cedSMatthew Dillon * Flush the TFD and SERR and make sure the port is stopped before 494f17a0cedSMatthew Dillon * enabling its interrupt. We no longer cycle the port start as 495f17a0cedSMatthew Dillon * the port should not be started unless a device is present. 496f17a0cedSMatthew Dillon * 497f17a0cedSMatthew Dillon * XXX should we enable FIS reception? (FRE)? 498e8cf3f55SMatthew Dillon */ 499492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 500492bffafSMatthew Dillon ahci_port_stop(ap, 0); 501492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 502492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 503f17a0cedSMatthew Dillon ahci_flush_tfd(ap); 504f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 505492bffafSMatthew Dillon 506492bffafSMatthew Dillon /* 507493d3201SMatthew Dillon * If we are being harsh try to kill the port completely. Normally 508493d3201SMatthew Dillon * we would want to hold on to some of the state the BIOS may have 509493d3201SMatthew Dillon * set, such as SUD (spin up device). 510492bffafSMatthew Dillon * 511492bffafSMatthew Dillon * AP_F_HARSH_REINIT is cleared in the hard reset state 512492bffafSMatthew Dillon */ 513492bffafSMatthew Dillon if (ap->ap_flags & AP_F_HARSH_REINIT) { 514*9abd2bb8SImre Vadász ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable); 515492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 516492bffafSMatthew Dillon 517492bffafSMatthew Dillon ahci_os_sleep(1000); 518492bffafSMatthew Dillon 519492bffafSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 520492bffafSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 521492bffafSMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | 522492bffafSMatthew Dillon AHCI_PREG_CMD_SUD; 523492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 524492bffafSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 525492bffafSMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 526492bffafSMatthew Dillon kprintf("%s: Warning: FRE did not come up during " 527492bffafSMatthew Dillon "harsh reinitialization\n", 528492bffafSMatthew Dillon PORTNAME(ap)); 529492bffafSMatthew Dillon } 530492bffafSMatthew Dillon ahci_os_sleep(1000); 531492bffafSMatthew Dillon } 532492bffafSMatthew Dillon 533492bffafSMatthew Dillon /* 534492bffafSMatthew Dillon * Clear any pending garbage and re-enable the interrupt before 535492bffafSMatthew Dillon * going to the next stage. 536492bffafSMatthew Dillon */ 537492bffafSMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_HARD_RESET; 538492bffafSMatthew Dillon ap->ap_pmcount = 0; 539492bffafSMatthew Dillon 540492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 541492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 542492bffafSMatthew Dillon ahci_flush_tfd(ap); 543492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 544492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, -1); 545492bffafSMatthew Dillon 546f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 547492bffafSMatthew Dillon 54812feb904SMatthew Dillon return (0); 549f4553de1SMatthew Dillon } 550f4553de1SMatthew Dillon 551f4553de1SMatthew Dillon /* 552f4553de1SMatthew Dillon * Enable or re-enable interrupts on a port. 553f4553de1SMatthew Dillon * 554f4553de1SMatthew Dillon * This routine is called from the port initialization code or from the 555f4553de1SMatthew Dillon * helper thread as the real interrupt may be forced to turn off certain 556f4553de1SMatthew Dillon * interrupt sources. 557f4553de1SMatthew Dillon */ 558f4553de1SMatthew Dillon void 559f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap) 560f4553de1SMatthew Dillon { 56112feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask); 5621980eff3SMatthew Dillon } 563258223a3SMatthew Dillon 564fd8bd957SMatthew Dillon /* 565f5caeaa0SMatthew Dillon * Manage the agressive link power management capability. 566f17a0cedSMatthew Dillon */ 567f17a0cedSMatthew Dillon void 568f17a0cedSMatthew Dillon ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt) 569f17a0cedSMatthew Dillon { 570f17a0cedSMatthew Dillon u_int32_t cmd, sctl; 571f17a0cedSMatthew Dillon 572f17a0cedSMatthew Dillon if (link_pwr_mgmt == ap->link_pwr_mgmt) 573f17a0cedSMatthew Dillon return; 574f17a0cedSMatthew Dillon 575f17a0cedSMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) { 576f17a0cedSMatthew Dillon kprintf("%s: link power management not supported.\n", 577f17a0cedSMatthew Dillon PORTNAME(ap)); 578f17a0cedSMatthew Dillon return; 579f17a0cedSMatthew Dillon } 580f17a0cedSMatthew Dillon 581f17a0cedSMatthew Dillon ahci_os_lock_port(ap); 582f17a0cedSMatthew Dillon 583f17a0cedSMatthew Dillon if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR && 584f17a0cedSMatthew Dillon (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) { 585f17a0cedSMatthew Dillon kprintf("%s: enabling aggressive link power management.\n", 586f17a0cedSMatthew Dillon PORTNAME(ap)); 587f17a0cedSMatthew Dillon 588795adb22SMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 589795adb22SMatthew Dillon 590f17a0cedSMatthew Dillon ap->ap_intmask &= ~AHCI_PREG_IE_PRCE; 591f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 592f17a0cedSMatthew Dillon 593f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 594*9abd2bb8SImre Vadász sctl &= ~(AHCI_PREG_SCTL_IPM); 595*9abd2bb8SImre Vadász if (ap->ap_sc->sc_cap2 & AHCI_REG_CAP2_SDS) 596*9abd2bb8SImre Vadász sctl |= AHCI_PREG_SCTL_IPM_NODEVSLP; 597f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 598f17a0cedSMatthew Dillon 599795adb22SMatthew Dillon /* 600795adb22SMatthew Dillon * Enable device initiated link power management for 601795adb22SMatthew Dillon * directly attached devices that support it. 602795adb22SMatthew Dillon */ 603795adb22SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM && 604750495d0SImre Vadász (ap->ap_ata[0]->at_identify.satafsup & 605750495d0SImre Vadász SATA_FEATURE_SUP_DEVIPS)) { 606795adb22SMatthew Dillon if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1)) 607795adb22SMatthew Dillon kprintf("%s: Could not enable device initiated " 608795adb22SMatthew Dillon "link power management.\n", 609795adb22SMatthew Dillon PORTNAME(ap)); 610795adb22SMatthew Dillon } 611795adb22SMatthew Dillon 612f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 613f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ASP; 614f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ALPE; 615f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 616f17a0cedSMatthew Dillon 617f17a0cedSMatthew Dillon } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM && 618f17a0cedSMatthew Dillon (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) { 619f17a0cedSMatthew Dillon kprintf("%s: enabling medium link power management.\n", 620f17a0cedSMatthew Dillon PORTNAME(ap)); 621f17a0cedSMatthew Dillon 622795adb22SMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 623795adb22SMatthew Dillon 624f17a0cedSMatthew Dillon ap->ap_intmask &= ~AHCI_PREG_IE_PRCE; 625f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 626f17a0cedSMatthew Dillon 627f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 628*9abd2bb8SImre Vadász sctl &= ~(AHCI_PREG_SCTL_IPM); 629*9abd2bb8SImre Vadász sctl |= AHCI_PREG_SCTL_IPM_NOSLUMBER; 630*9abd2bb8SImre Vadász if (ap->ap_sc->sc_cap2 & AHCI_REG_CAP2_SDS) 631*9abd2bb8SImre Vadász sctl |= AHCI_PREG_SCTL_IPM_NODEVSLP; 632f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 633f17a0cedSMatthew Dillon 634f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 635f17a0cedSMatthew Dillon cmd &= ~AHCI_PREG_CMD_ASP; 636f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ALPE; 637f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 638f17a0cedSMatthew Dillon 639f17a0cedSMatthew Dillon } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) { 640f17a0cedSMatthew Dillon kprintf("%s: disabling link power management.\n", 641f17a0cedSMatthew Dillon PORTNAME(ap)); 642f17a0cedSMatthew Dillon 643795adb22SMatthew Dillon /* Disable device initiated link power management */ 644795adb22SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM && 645750495d0SImre Vadász (ap->ap_ata[0]->at_identify.satafsup & 646750495d0SImre Vadász SATA_FEATURE_SUP_DEVIPS)) { 647795adb22SMatthew Dillon ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0); 648750495d0SImre Vadász } 649795adb22SMatthew Dillon 650f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 651f17a0cedSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP); 652f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 653f17a0cedSMatthew Dillon 654f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 655*9abd2bb8SImre Vadász sctl &= ~(AHCI_PREG_SCTL_IPM); 656*9abd2bb8SImre Vadász sctl |= ap->ap_sc->sc_ipm_disable; 657f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 658f17a0cedSMatthew Dillon 659f17a0cedSMatthew Dillon /* let the drive come back to avoid PRCS interrupts later */ 660f17a0cedSMatthew Dillon ahci_os_unlock_port(ap); 661f17a0cedSMatthew Dillon ahci_os_sleep(1000); 662f17a0cedSMatthew Dillon ahci_os_lock_port(ap); 663f17a0cedSMatthew Dillon 664795adb22SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 665795adb22SMatthew Dillon AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W); 666f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 667f17a0cedSMatthew Dillon 668f17a0cedSMatthew Dillon ap->ap_intmask |= AHCI_PREG_IE_PRCE; 669f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 670f17a0cedSMatthew Dillon 671f17a0cedSMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 672f17a0cedSMatthew Dillon } else { 673f17a0cedSMatthew Dillon kprintf("%s: unsupported link power management state %d.\n", 674f17a0cedSMatthew Dillon PORTNAME(ap), link_pwr_mgmt); 675f17a0cedSMatthew Dillon } 676f17a0cedSMatthew Dillon 677f17a0cedSMatthew Dillon ahci_os_unlock_port(ap); 678f17a0cedSMatthew Dillon } 679f17a0cedSMatthew Dillon 680795adb22SMatthew Dillon /* 681795adb22SMatthew Dillon * Return current link power state. 682795adb22SMatthew Dillon */ 683795adb22SMatthew Dillon int 684795adb22SMatthew Dillon ahci_port_link_pwr_state(struct ahci_port *ap) 685795adb22SMatthew Dillon { 686795adb22SMatthew Dillon uint32_t r; 687795adb22SMatthew Dillon 688795adb22SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 689d90e4fd1SImre Vadász switch (r & AHCI_PREG_SSTS_IPM) { 690d90e4fd1SImre Vadász case AHCI_PREG_SSTS_IPM_ACTIVE: 691795adb22SMatthew Dillon return 1; 692d90e4fd1SImre Vadász case AHCI_PREG_SSTS_IPM_PARTIAL: 693795adb22SMatthew Dillon return 2; 694d90e4fd1SImre Vadász case AHCI_PREG_SSTS_IPM_SLUMBER: 695795adb22SMatthew Dillon return 3; 696d90e4fd1SImre Vadász case AHCI_PREG_SSTS_IPM_DEVSLEEP: 697d90e4fd1SImre Vadász return 4; 698795adb22SMatthew Dillon default: 699795adb22SMatthew Dillon return 0; 700795adb22SMatthew Dillon } 701795adb22SMatthew Dillon } 702f17a0cedSMatthew Dillon 703f17a0cedSMatthew Dillon /* 7043209f581SMatthew Dillon * Run the port / target state machine from a main context. 7053209f581SMatthew Dillon * 7063209f581SMatthew Dillon * The state machine for the port is always run. 7073209f581SMatthew Dillon * 7083209f581SMatthew Dillon * If atx is non-NULL run the state machine for a particular target. 7093209f581SMatthew Dillon * If atx is NULL run the state machine for all targets. 7103209f581SMatthew Dillon */ 7113209f581SMatthew Dillon void 712831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial) 7133209f581SMatthew Dillon { 7143209f581SMatthew Dillon struct ata_port *at; 7153209f581SMatthew Dillon u_int32_t data; 7163209f581SMatthew Dillon int target; 7173209f581SMatthew Dillon int didsleep; 718831bc9e3SMatthew Dillon int loop; 7193209f581SMatthew Dillon 720831bc9e3SMatthew Dillon /* 721831bc9e3SMatthew Dillon * State machine for port. Note that CAM is not yet associated 722831bc9e3SMatthew Dillon * during the initial parallel probe and the port's probe state 723831bc9e3SMatthew Dillon * will not get past ATA_PROBE_NEED_IDENT. 724831bc9e3SMatthew Dillon */ 725c408a8b3SMatthew Dillon { 7261067474aSMatthew Dillon if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) { 7271067474aSMatthew Dillon kprintf("%s: Waiting 10 seconds on insertion\n", 7281067474aSMatthew Dillon PORTNAME(ap)); 7291067474aSMatthew Dillon ahci_os_sleep(10000); 7301067474aSMatthew Dillon initial = 1; 7313209f581SMatthew Dillon } 7321067474aSMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT) 73312feb904SMatthew Dillon ahci_port_init(ap); 7343209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) 7353209f581SMatthew Dillon ahci_port_reset(ap, NULL, 1); 7363209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET) 7373209f581SMatthew Dillon ahci_port_reset(ap, NULL, 0); 7383209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 7393209f581SMatthew Dillon ahci_cam_probe(ap, NULL); 7403209f581SMatthew Dillon } 7413209f581SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM) { 7423209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 7433209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 0); 744f4553de1SMatthew Dillon } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) { 7453209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 1); 7463209f581SMatthew Dillon } 7473209f581SMatthew Dillon return; 7483209f581SMatthew Dillon } 7493209f581SMatthew Dillon 750831bc9e3SMatthew Dillon /* 751831bc9e3SMatthew Dillon * Port Multiplier state machine. 752831bc9e3SMatthew Dillon * 753831bc9e3SMatthew Dillon * Get a mask of changed targets and combine with any runnable 754831bc9e3SMatthew Dillon * states already present. 755831bc9e3SMatthew Dillon */ 756831bc9e3SMatthew Dillon for (loop = 0; ;++loop) { 7572cc2e845SMatthew Dillon if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) { 7583209f581SMatthew Dillon kprintf("%s: PM unable to read hot-plug bitmap\n", 7593209f581SMatthew Dillon PORTNAME(ap)); 7603209f581SMatthew Dillon break; 7613209f581SMatthew Dillon } 7623209f581SMatthew Dillon 7633209f581SMatthew Dillon /* 764831bc9e3SMatthew Dillon * Do at least one loop, then stop if no more state changes 765831bc9e3SMatthew Dillon * have occured. The PM might not generate a new 766831bc9e3SMatthew Dillon * notification until we clear the entire bitmap. 7673209f581SMatthew Dillon */ 768831bc9e3SMatthew Dillon if (loop && data == 0) 7693209f581SMatthew Dillon break; 7703209f581SMatthew Dillon 7713209f581SMatthew Dillon /* 7723209f581SMatthew Dillon * New devices showing up in the bitmap require some spin-up 7733209f581SMatthew Dillon * time before we start probing them. Reset didsleep. The 7743209f581SMatthew Dillon * first new device we detect will sleep before probing. 775831bc9e3SMatthew Dillon * 776831bc9e3SMatthew Dillon * This only applies to devices whos change bit is set in 777831bc9e3SMatthew Dillon * the data, and does not apply to the initial boot-time 778831bc9e3SMatthew Dillon * probe. 7793209f581SMatthew Dillon */ 7803209f581SMatthew Dillon didsleep = 0; 7813209f581SMatthew Dillon 7823209f581SMatthew Dillon for (target = 0; target < ap->ap_pmcount; ++target) { 783b012a2caSMatthew Dillon at = ap->ap_ata[target]; 7843209f581SMatthew Dillon 7853209f581SMatthew Dillon /* 7863209f581SMatthew Dillon * Check the target state for targets behind the PM 7873209f581SMatthew Dillon * which have changed state. This will adjust 7883209f581SMatthew Dillon * at_probe and set ATA_PORT_F_RESCAN 7893209f581SMatthew Dillon * 7901067474aSMatthew Dillon * We want to wait at least 10 seconds before probing 7913209f581SMatthew Dillon * a newly inserted device. If the check status 7923209f581SMatthew Dillon * indicates a device is present and in need of a 7933209f581SMatthew Dillon * hard reset, we make sure we have slept before 7943209f581SMatthew Dillon * continuing. 795831bc9e3SMatthew Dillon * 7961067474aSMatthew Dillon * We also need to wait at least 1 second for the 7971067474aSMatthew Dillon * PHY state to change after insertion, if we 7981067474aSMatthew Dillon * haven't already waited the 10 seconds. 7991067474aSMatthew Dillon * 800831bc9e3SMatthew Dillon * NOTE: When pm_check_good finds a good port it 801831bc9e3SMatthew Dillon * typically starts us in probe state 802831bc9e3SMatthew Dillon * NEED_HARD_RESET rather than INIT. 8033209f581SMatthew Dillon */ 8043209f581SMatthew Dillon if (data & (1 << target)) { 8051067474aSMatthew Dillon if (initial == 0 && didsleep == 0) 8061067474aSMatthew Dillon ahci_os_sleep(1000); 8073209f581SMatthew Dillon ahci_pm_check_good(ap, target); 808831bc9e3SMatthew Dillon if (initial == 0 && didsleep == 0 && 809831bc9e3SMatthew Dillon at->at_probe <= ATA_PROBE_NEED_HARD_RESET 810831bc9e3SMatthew Dillon ) { 8113209f581SMatthew Dillon didsleep = 1; 812121d8e75SMatthew Dillon kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap)); 813121d8e75SMatthew Dillon ahci_os_sleep(10000); 8143209f581SMatthew Dillon } 8153209f581SMatthew Dillon } 816831bc9e3SMatthew Dillon 817831bc9e3SMatthew Dillon /* 818831bc9e3SMatthew Dillon * Report hot-plug events before the probe state 819831bc9e3SMatthew Dillon * really gets hot. Only actual events are reported 820831bc9e3SMatthew Dillon * here to reduce spew. 821831bc9e3SMatthew Dillon */ 822831bc9e3SMatthew Dillon if (data & (1 << target)) { 823831bc9e3SMatthew Dillon kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at)); 824831bc9e3SMatthew Dillon switch(at->at_probe) { 825831bc9e3SMatthew Dillon case ATA_PROBE_NEED_INIT: 826831bc9e3SMatthew Dillon case ATA_PROBE_NEED_HARD_RESET: 827831bc9e3SMatthew Dillon kprintf("Device inserted\n"); 828831bc9e3SMatthew Dillon break; 829831bc9e3SMatthew Dillon case ATA_PROBE_FAILED: 830831bc9e3SMatthew Dillon kprintf("Device removed\n"); 831831bc9e3SMatthew Dillon break; 832831bc9e3SMatthew Dillon default: 833831bc9e3SMatthew Dillon kprintf("Device probe in progress\n"); 834831bc9e3SMatthew Dillon break; 835831bc9e3SMatthew Dillon } 8363209f581SMatthew Dillon } 8373209f581SMatthew Dillon 8383209f581SMatthew Dillon /* 839831bc9e3SMatthew Dillon * Run through the state machine as necessary if 840831bc9e3SMatthew Dillon * the port is not marked failed. 841831bc9e3SMatthew Dillon * 842831bc9e3SMatthew Dillon * The state machine may stop at NEED_IDENT if 843831bc9e3SMatthew Dillon * CAM is not yet attached. 844831bc9e3SMatthew Dillon * 845831bc9e3SMatthew Dillon * Acquire exclusive access to the port while we 846831bc9e3SMatthew Dillon * are doing this. This prevents command-completion 847831bc9e3SMatthew Dillon * from queueing commands for non-polled targets 848831bc9e3SMatthew Dillon * inbetween our probe steps. We need to do this 849831bc9e3SMatthew Dillon * because the reset probes can generate severe PHY 850831bc9e3SMatthew Dillon * and protocol errors and soft-brick the port. 8513209f581SMatthew Dillon */ 852831bc9e3SMatthew Dillon if (at->at_probe != ATA_PROBE_FAILED && 853831bc9e3SMatthew Dillon at->at_probe != ATA_PROBE_GOOD) { 854831bc9e3SMatthew Dillon ahci_beg_exclusive_access(ap, at); 8553209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_INIT) 85612feb904SMatthew Dillon ahci_pm_port_init(ap, at); 8573209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) 8583209f581SMatthew Dillon ahci_port_reset(ap, at, 1); 8593209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET) 8603209f581SMatthew Dillon ahci_port_reset(ap, at, 0); 8613209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_IDENT) 8623209f581SMatthew Dillon ahci_cam_probe(ap, at); 863831bc9e3SMatthew Dillon ahci_end_exclusive_access(ap, at); 8643209f581SMatthew Dillon } 8653209f581SMatthew Dillon 8663209f581SMatthew Dillon /* 867831bc9e3SMatthew Dillon * Add or remove from CAM 8683209f581SMatthew Dillon */ 8693209f581SMatthew Dillon if (at->at_features & ATA_PORT_F_RESCAN) { 8703209f581SMatthew Dillon at->at_features &= ~ATA_PORT_F_RESCAN; 8713209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_FAILED) { 8723209f581SMatthew Dillon ahci_cam_changed(ap, at, 0); 873f4553de1SMatthew Dillon } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) { 8743209f581SMatthew Dillon ahci_cam_changed(ap, at, 1); 8753209f581SMatthew Dillon } 8763209f581SMatthew Dillon } 8773560ed94SMatthew Dillon data &= ~(1 << target); 8783560ed94SMatthew Dillon } 8793560ed94SMatthew Dillon if (data) { 8803560ed94SMatthew Dillon kprintf("%s: WARNING (PM): extra bits set in " 8813560ed94SMatthew Dillon "EINFO: %08x\n", PORTNAME(ap), data); 8823560ed94SMatthew Dillon while (target < AHCI_MAX_PMPORTS) { 8833560ed94SMatthew Dillon ahci_pm_check_good(ap, target); 8843560ed94SMatthew Dillon ++target; 8853560ed94SMatthew Dillon } 8863209f581SMatthew Dillon } 8873209f581SMatthew Dillon } 8883209f581SMatthew Dillon } 8893209f581SMatthew Dillon 8903209f581SMatthew Dillon 8913209f581SMatthew Dillon /* 892fd8bd957SMatthew Dillon * De-initialize and detach a port. 893fd8bd957SMatthew Dillon */ 894258223a3SMatthew Dillon void 895258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 896258223a3SMatthew Dillon { 897258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 898258223a3SMatthew Dillon struct ahci_ccb *ccb; 899b012a2caSMatthew Dillon int i; 900258223a3SMatthew Dillon 90117eab71eSMatthew Dillon /* 90217eab71eSMatthew Dillon * Ensure port is disabled and its interrupts are all flushed. 90317eab71eSMatthew Dillon */ 904258223a3SMatthew Dillon if (ap->ap_sc) { 90517eab71eSMatthew Dillon ahci_port_stop(ap, 1); 906f4553de1SMatthew Dillon ahci_os_stop_port(ap); 907258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 908258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 909258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 910258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 911258223a3SMatthew Dillon } 912258223a3SMatthew Dillon 913258223a3SMatthew Dillon if (ap->ap_ccbs) { 914258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 915258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 916258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 917258223a3SMatthew Dillon ccb->ccb_dmamap); 918258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 919258223a3SMatthew Dillon } 920258223a3SMatthew Dillon } 9211067474aSMatthew Dillon if ((ccb = ap->ap_err_ccb) != NULL) { 9221067474aSMatthew Dillon if (ccb->ccb_dmamap) { 9231067474aSMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 9241067474aSMatthew Dillon ccb->ccb_dmamap); 9251067474aSMatthew Dillon ccb->ccb_dmamap = NULL; 9261067474aSMatthew Dillon } 9271067474aSMatthew Dillon ap->ap_err_ccb = NULL; 9281067474aSMatthew Dillon } 929258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 930258223a3SMatthew Dillon ap->ap_ccbs = NULL; 931258223a3SMatthew Dillon } 932258223a3SMatthew Dillon 933258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 934258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 935258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 936258223a3SMatthew Dillon } 937258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 938258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 939258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 940258223a3SMatthew Dillon } 941258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 942258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 943258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 944258223a3SMatthew Dillon } 9451980eff3SMatthew Dillon if (ap->ap_ata) { 946b012a2caSMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 947b012a2caSMatthew Dillon if (ap->ap_ata[i]) { 948b012a2caSMatthew Dillon kfree(ap->ap_ata[i], M_DEVBUF); 949b012a2caSMatthew Dillon ap->ap_ata[i] = NULL; 950b012a2caSMatthew Dillon } 951b012a2caSMatthew Dillon } 9521980eff3SMatthew Dillon } 95312feb904SMatthew Dillon if (ap->ap_err_scratch) { 95412feb904SMatthew Dillon kfree(ap->ap_err_scratch, M_DEVBUF); 95512feb904SMatthew Dillon ap->ap_err_scratch = NULL; 95612feb904SMatthew Dillon } 957258223a3SMatthew Dillon 958258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 959258223a3SMatthew Dillon 960258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 961258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 962258223a3SMatthew Dillon } 963258223a3SMatthew Dillon 964492bffafSMatthew Dillon static 965492bffafSMatthew Dillon u_int32_t 966492bffafSMatthew Dillon ahci_pactive(struct ahci_port *ap) 967492bffafSMatthew Dillon { 968492bffafSMatthew Dillon u_int32_t mask; 969492bffafSMatthew Dillon 970492bffafSMatthew Dillon mask = ahci_pread(ap, AHCI_PREG_CI); 971492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) 972492bffafSMatthew Dillon mask |= ahci_pread(ap, AHCI_PREG_SACT); 973492bffafSMatthew Dillon return(mask); 974492bffafSMatthew Dillon } 975492bffafSMatthew Dillon 976fd8bd957SMatthew Dillon /* 977fd8bd957SMatthew Dillon * Start high-level command processing on the port 978fd8bd957SMatthew Dillon */ 979258223a3SMatthew Dillon int 98017eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap) 981258223a3SMatthew Dillon { 98212feb904SMatthew Dillon u_int32_t r, s, is, tfd; 983258223a3SMatthew Dillon 98417eab71eSMatthew Dillon /* 98517eab71eSMatthew Dillon * FRE must be turned on before ST. Wait for FR to go active 98617eab71eSMatthew Dillon * before turning on ST. The spec doesn't seem to think this 98717eab71eSMatthew Dillon * is necessary but waiting here avoids an on-off race in the 98817eab71eSMatthew Dillon * ahci_port_stop() code. 98917eab71eSMatthew Dillon */ 99012feb904SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD); 99117eab71eSMatthew Dillon if ((r & AHCI_PREG_CMD_FRE) == 0) { 992258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 99317eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 99417eab71eSMatthew Dillon } 99517eab71eSMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) { 99617eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 99717eab71eSMatthew Dillon kprintf("%s: Cannot start FIS reception\n", 99817eab71eSMatthew Dillon PORTNAME(ap)); 99917eab71eSMatthew Dillon return (2); 100017eab71eSMatthew Dillon } 1001f17a0cedSMatthew Dillon } else { 1002f17a0cedSMatthew Dillon ahci_os_sleep(10); 100317eab71eSMatthew Dillon } 100417eab71eSMatthew Dillon 100517eab71eSMatthew Dillon /* 100617eab71eSMatthew Dillon * Turn on ST, wait for CR to come up. 100717eab71eSMatthew Dillon */ 1008258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 1009258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 1010f17a0cedSMatthew Dillon if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 10118bf6a3ffSMatthew Dillon s = ahci_pread(ap, AHCI_PREG_SERR); 10128bf6a3ffSMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 10138bf6a3ffSMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 10141980eff3SMatthew Dillon kprintf("%s: Cannot start command DMA\n" 10151980eff3SMatthew Dillon "NCMP=%b NSERR=%b\n" 101612feb904SMatthew Dillon "NEWIS=%b\n" 101712feb904SMatthew Dillon "NEWTFD=%b\n", 10181980eff3SMatthew Dillon PORTNAME(ap), 10191980eff3SMatthew Dillon r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR, 102012feb904SMatthew Dillon is, AHCI_PFMT_IS, 102112feb904SMatthew Dillon tfd, AHCI_PFMT_TFD_STS); 102217eab71eSMatthew Dillon return (1); 102317eab71eSMatthew Dillon } 1024258223a3SMatthew Dillon 1025258223a3SMatthew Dillon #ifdef AHCI_COALESCE 102617eab71eSMatthew Dillon /* 102717eab71eSMatthew Dillon * (Re-)enable coalescing on the port. 102817eab71eSMatthew Dillon */ 1029258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 1030258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 1031258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 1032258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 1033258223a3SMatthew Dillon } 1034258223a3SMatthew Dillon #endif 1035258223a3SMatthew Dillon 1036258223a3SMatthew Dillon return (0); 1037258223a3SMatthew Dillon } 1038258223a3SMatthew Dillon 1039fd8bd957SMatthew Dillon /* 1040fd8bd957SMatthew Dillon * Stop high-level command processing on a port 10414c339a5fSMatthew Dillon * 10424c339a5fSMatthew Dillon * WARNING! If the port is stopped while CR is still active our saved 10434c339a5fSMatthew Dillon * CI/SACT will race any commands completed by the command 10444c339a5fSMatthew Dillon * processor prior to being able to stop. Thus we never call 10454c339a5fSMatthew Dillon * this function unless we intend to dispose of any remaining 10464c339a5fSMatthew Dillon * active commands. In particular, this complicates the timeout 10474c339a5fSMatthew Dillon * code. 1048fd8bd957SMatthew Dillon */ 1049258223a3SMatthew Dillon int 1050258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 1051258223a3SMatthew Dillon { 1052258223a3SMatthew Dillon u_int32_t r; 1053258223a3SMatthew Dillon 1054258223a3SMatthew Dillon #ifdef AHCI_COALESCE 105517eab71eSMatthew Dillon /* 105617eab71eSMatthew Dillon * Disable coalescing on the port while it is stopped. 105717eab71eSMatthew Dillon */ 1058258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 1059258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 1060258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 1061258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 1062258223a3SMatthew Dillon } 1063258223a3SMatthew Dillon #endif 1064258223a3SMatthew Dillon 106517eab71eSMatthew Dillon /* 106617eab71eSMatthew Dillon * Turn off ST, then wait for CR to go off. 106717eab71eSMatthew Dillon */ 1068258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1069258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 1070258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 1071258223a3SMatthew Dillon 107217eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 107317eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (ST)\n", 107417eab71eSMatthew Dillon PORTNAME(ap)); 1075258223a3SMatthew Dillon return (1); 107617eab71eSMatthew Dillon } 1077258223a3SMatthew Dillon 10781980eff3SMatthew Dillon #if 0 107917eab71eSMatthew Dillon /* 108017eab71eSMatthew Dillon * Turn off FRE, then wait for FR to go off. FRE cannot 108117eab71eSMatthew Dillon * be turned off until CR transitions to 0. 108217eab71eSMatthew Dillon */ 10831980eff3SMatthew Dillon if ((r & AHCI_PREG_CMD_FR) == 0) { 10841980eff3SMatthew Dillon kprintf("%s: FR stopped, clear FRE for next start\n", 10851980eff3SMatthew Dillon PORTNAME(ap)); 10861980eff3SMatthew Dillon stop_fis_rx = 2; 10871980eff3SMatthew Dillon } 10881980eff3SMatthew Dillon #endif 108917eab71eSMatthew Dillon if (stop_fis_rx) { 109017eab71eSMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 109117eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 109217eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 109317eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (FRE)\n", 109417eab71eSMatthew Dillon PORTNAME(ap)); 1095258223a3SMatthew Dillon return (2); 109617eab71eSMatthew Dillon } 109717eab71eSMatthew Dillon } 1098258223a3SMatthew Dillon 1099258223a3SMatthew Dillon return (0); 1100258223a3SMatthew Dillon } 1101258223a3SMatthew Dillon 1102fd8bd957SMatthew Dillon /* 1103fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 1104fd8bd957SMatthew Dillon */ 1105258223a3SMatthew Dillon int 1106258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 1107258223a3SMatthew Dillon { 1108258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1109258223a3SMatthew Dillon u_int32_t cmd; 1110258223a3SMatthew Dillon 1111258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 1112258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 1113258223a3SMatthew Dillon return (1); 1114258223a3SMatthew Dillon 1115258223a3SMatthew Dillon /* Issue CLO */ 1116258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1117258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 1118258223a3SMatthew Dillon 1119258223a3SMatthew Dillon /* Wait for completion */ 1120258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 1121258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 1122258223a3SMatthew Dillon return (1); 1123258223a3SMatthew Dillon } 1124258223a3SMatthew Dillon 1125258223a3SMatthew Dillon return (0); 1126258223a3SMatthew Dillon } 1127258223a3SMatthew Dillon 1128fd8bd957SMatthew Dillon /* 11291980eff3SMatthew Dillon * Reset a port. 113017eab71eSMatthew Dillon * 11311980eff3SMatthew Dillon * If hard is 0 perform a softreset of the port. 113217eab71eSMatthew Dillon * If hard is 1 perform a hard reset of the port. 11331980eff3SMatthew Dillon * 11341980eff3SMatthew Dillon * If at is non-NULL an indirect port via a port-multiplier is being 11351980eff3SMatthew Dillon * reset, otherwise a direct port is being reset. 11361980eff3SMatthew Dillon * 11371980eff3SMatthew Dillon * NOTE: Indirect ports can only be soft-reset. 113817eab71eSMatthew Dillon */ 113917eab71eSMatthew Dillon int 11401980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard) 114117eab71eSMatthew Dillon { 114217eab71eSMatthew Dillon int rc; 114317eab71eSMatthew Dillon 114417eab71eSMatthew Dillon if (hard) { 11451980eff3SMatthew Dillon if (at) 11461980eff3SMatthew Dillon rc = ahci_pm_hardreset(ap, at->at_target, hard); 11471980eff3SMatthew Dillon else 11481980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 114917eab71eSMatthew Dillon } else { 11501980eff3SMatthew Dillon if (at) 11511980eff3SMatthew Dillon rc = ahci_pm_softreset(ap, at->at_target); 11521980eff3SMatthew Dillon else 115317eab71eSMatthew Dillon rc = ahci_port_softreset(ap); 115417eab71eSMatthew Dillon } 115517eab71eSMatthew Dillon return(rc); 115617eab71eSMatthew Dillon } 115717eab71eSMatthew Dillon 115817eab71eSMatthew Dillon /* 1159fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 1160fd8bd957SMatthew Dillon * 11611980eff3SMatthew Dillon * (at) will be NULL when soft-resetting a directly-attached device, and 11621980eff3SMatthew Dillon * non-NULL when soft-resetting a device through a port multiplier. 11631980eff3SMatthew Dillon * 1164fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 11651980eff3SMatthew Dillon * a reset to the connected device using device commands. 1166fd8bd957SMatthew Dillon */ 1167258223a3SMatthew Dillon int 1168258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 1169258223a3SMatthew Dillon { 1170258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 1171258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 1172258223a3SMatthew Dillon u_int8_t *fis; 11733209f581SMatthew Dillon int error; 1174258223a3SMatthew Dillon 11753209f581SMatthew Dillon error = EIO; 11761980eff3SMatthew Dillon 1177074579dfSMatthew Dillon if (bootverbose) { 11781980eff3SMatthew Dillon kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap), 11791980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD); 1180074579dfSMatthew Dillon } 11811980eff3SMatthew Dillon 1182258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 1183258223a3SMatthew Dillon 1184258223a3SMatthew Dillon crit_enter(); 11851980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 11861980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 1187258223a3SMatthew Dillon 11881980eff3SMatthew Dillon /* 11891980eff3SMatthew Dillon * Remember port state in cmd (main to restore start/stop) 11901980eff3SMatthew Dillon * 11911980eff3SMatthew Dillon * Idle port. 11921980eff3SMatthew Dillon */ 1193258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 1194258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 1195258223a3SMatthew Dillon PORTNAME(ap)); 1196258223a3SMatthew Dillon goto err; 1197258223a3SMatthew Dillon } 1198cf5f3a81SMatthew Dillon 1199cf5f3a81SMatthew Dillon /* 12001980eff3SMatthew Dillon * Request CLO if device appears hung. 1201cf5f3a81SMatthew Dillon */ 1202258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 1203258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1204258223a3SMatthew Dillon ahci_port_clo(ap); 1205258223a3SMatthew Dillon } 1206258223a3SMatthew Dillon 12071980eff3SMatthew Dillon /* 12081980eff3SMatthew Dillon * This is an attempt to clear errors so a new signature will 12091980eff3SMatthew Dillon * be latched. It isn't working properly. XXX 12101980eff3SMatthew Dillon */ 1211cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 12121980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1213258223a3SMatthew Dillon 1214258223a3SMatthew Dillon /* Restart port */ 121517eab71eSMatthew Dillon if (ahci_port_start(ap)) { 1216258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 1217258223a3SMatthew Dillon PORTNAME(ap)); 1218258223a3SMatthew Dillon goto err; 1219258223a3SMatthew Dillon } 1220258223a3SMatthew Dillon 1221258223a3SMatthew Dillon /* Check whether CLO worked */ 1222258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 1223258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1224258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 1225258223a3SMatthew Dillon PORTNAME(ap), 1226258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 1227258223a3SMatthew Dillon ? "failed" : "unsupported"); 12283209f581SMatthew Dillon error = EBUSY; 1229258223a3SMatthew Dillon goto err; 1230258223a3SMatthew Dillon } 1231258223a3SMatthew Dillon 1232cec85a37SMatthew Dillon /* 1233cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 1234cec85a37SMatthew Dillon * 1235cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1236cec85a37SMatthew Dillon * everything. 12371067474aSMatthew Dillon * 12381067474aSMatthew Dillon * NOTE! This CCB is used for both the first and second commands. 12391067474aSMatthew Dillon * The second command must use CCB slot 1 to properly load 12401067474aSMatthew Dillon * the signature. 1241cec85a37SMatthew Dillon */ 1242258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 124312feb904SMatthew Dillon ccb->ccb_xa.complete = ahci_dummy_done; 124412feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE; 12451067474aSMatthew Dillon KKASSERT(ccb->ccb_slot == 1); 12461980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 1247258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 1248258223a3SMatthew Dillon 1249258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 1250cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 12511980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 12521980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 1253258223a3SMatthew Dillon 1254258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1255258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1256258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 1257258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 1258258223a3SMatthew Dillon 1259258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 126012feb904SMatthew Dillon 1261831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 12625f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 1263258223a3SMatthew Dillon goto err; 1264cec85a37SMatthew Dillon } 1265258223a3SMatthew Dillon 1266cec85a37SMatthew Dillon /* 1267831bc9e3SMatthew Dillon * WARNING! TIME SENSITIVE SPACE! WARNING! 1268831bc9e3SMatthew Dillon * 1269831bc9e3SMatthew Dillon * The two FISes are supposed to be back to back. Don't issue other 1270831bc9e3SMatthew Dillon * commands or even delay if we can help it. 12711980eff3SMatthew Dillon */ 12721980eff3SMatthew Dillon 12731980eff3SMatthew Dillon /* 1274cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 1275cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 1276cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 1277cec85a37SMatthew Dillon * from the AHCI document. 1278cec85a37SMatthew Dillon * 1279cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1280cec85a37SMatthew Dillon * everything. 1281cec85a37SMatthew Dillon */ 128212feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE; 128312feb904SMatthew Dillon 1284cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 12851980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 12861980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 1287258223a3SMatthew Dillon 1288258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1289258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1290258223a3SMatthew Dillon 1291258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 1292831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 12935f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 1294258223a3SMatthew Dillon goto err; 1295cec85a37SMatthew Dillon } 1296258223a3SMatthew Dillon 12971980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 12981980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1299258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 1300258223a3SMatthew Dillon PORTNAME(ap), 1301258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 13023209f581SMatthew Dillon error = EBUSY; 1303258223a3SMatthew Dillon goto err; 1304258223a3SMatthew Dillon } 1305258223a3SMatthew Dillon 1306fd8bd957SMatthew Dillon /* 1307fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 1308fd8bd957SMatthew Dillon * normal portreset we assign the port type. 1309fd8bd957SMatthew Dillon * 1310fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 1311fd8bd957SMatthew Dillon * processing code then report if the device signature changed 1312fd8bd957SMatthew Dillon * unexpectedly. 1313fd8bd957SMatthew Dillon */ 1314493d3201SMatthew Dillon ahci_os_sleep(100); 13151980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 13161980eff3SMatthew Dillon ap->ap_type = ahci_port_signature_detect(ap, NULL); 1317fd8bd957SMatthew Dillon } else { 13181980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) { 13191980eff3SMatthew Dillon kprintf("%s: device signature unexpectedly " 13201980eff3SMatthew Dillon "changed\n", PORTNAME(ap)); 13213209f581SMatthew Dillon error = EBUSY; /* XXX */ 1322fd8bd957SMatthew Dillon } 1323fd8bd957SMatthew Dillon } 13243209f581SMatthew Dillon error = 0; 13251980eff3SMatthew Dillon 13263209f581SMatthew Dillon ahci_os_sleep(3); 1327258223a3SMatthew Dillon err: 1328258223a3SMatthew Dillon if (ccb != NULL) { 1329258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 13301980eff3SMatthew Dillon 13311980eff3SMatthew Dillon /* 13321980eff3SMatthew Dillon * If the target is busy use CLO to clear the busy 13331980eff3SMatthew Dillon * condition. The BSY should be cleared on the next 13341980eff3SMatthew Dillon * start. 13351980eff3SMatthew Dillon */ 13361980eff3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 13371980eff3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 13381980eff3SMatthew Dillon ahci_port_clo(ap); 13391980eff3SMatthew Dillon } 1340258223a3SMatthew Dillon } 1341258223a3SMatthew Dillon 1342cf5f3a81SMatthew Dillon /* 1343cf5f3a81SMatthew Dillon * If we failed to softreset make the port quiescent, otherwise 1344cf5f3a81SMatthew Dillon * make sure the port's start/stop state matches what it was on 1345cf5f3a81SMatthew Dillon * entry. 13461980eff3SMatthew Dillon * 13471980eff3SMatthew Dillon * Don't kill the port if the softreset is on a port multiplier 13481980eff3SMatthew Dillon * target, that would kill all the targets! 1349cf5f3a81SMatthew Dillon */ 13503209f581SMatthew Dillon if (error) { 1351cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 13523209f581SMatthew Dillon /* ap_probe set to failed */ 1353cf5f3a81SMatthew Dillon } else { 13543209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 135512feb904SMatthew Dillon ap->ap_pmcount = 1; 13564c339a5fSMatthew Dillon ahci_port_start(ap); 1357cf5f3a81SMatthew Dillon } 13583209f581SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 1359258223a3SMatthew Dillon crit_exit(); 1360258223a3SMatthew Dillon 1361074579dfSMatthew Dillon if (bootverbose) 13621980eff3SMatthew Dillon kprintf("%s: END SOFTRESET\n", PORTNAME(ap)); 13631980eff3SMatthew Dillon 13643209f581SMatthew Dillon return (error); 1365258223a3SMatthew Dillon } 1366258223a3SMatthew Dillon 1367fd8bd957SMatthew Dillon /* 1368493d3201SMatthew Dillon * Issue just do the core COMRESET and basic device detection on a port. 1369fd8bd957SMatthew Dillon * 1370493d3201SMatthew Dillon * NOTE: Only called by ahci_port_hardreset(). 1371fd8bd957SMatthew Dillon */ 1372493d3201SMatthew Dillon static int 1373493d3201SMatthew Dillon ahci_comreset(struct ahci_port *ap, int *pmdetectp) 1374258223a3SMatthew Dillon { 1375493d3201SMatthew Dillon u_int32_t cmd; 1376493d3201SMatthew Dillon u_int32_t r; 13773209f581SMatthew Dillon int error; 13781980eff3SMatthew Dillon int loop; 1379f2dba700SMatthew Dillon int retries = 0; 1380258223a3SMatthew Dillon 1381cf5f3a81SMatthew Dillon /* 13821980eff3SMatthew Dillon * Idle the port, 13831980eff3SMatthew Dillon */ 1384493d3201SMatthew Dillon *pmdetectp = 0; 13851980eff3SMatthew Dillon ahci_port_stop(ap, 0); 13861980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 1387493d3201SMatthew Dillon ahci_os_sleep(10); 13881980eff3SMatthew Dillon 13891980eff3SMatthew Dillon /* 13901980eff3SMatthew Dillon * The port may have been quiescent with its SUD bit cleared, so 13911980eff3SMatthew Dillon * set the SUD (spin up device). 1392493d3201SMatthew Dillon * 1393493d3201SMatthew Dillon * NOTE: I do not know if SUD is a hardware pin/low-level signal 1394493d3201SMatthew Dillon * or if it is messaged. 1395cf5f3a81SMatthew Dillon */ 1396cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1397493d3201SMatthew Dillon 1398493d3201SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD; 1399cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1400493d3201SMatthew Dillon ahci_os_sleep(10); 1401258223a3SMatthew Dillon 14021980eff3SMatthew Dillon /* 1403493d3201SMatthew Dillon * Make sure that all power management is disabled. 14041067474aSMatthew Dillon * 1405493d3201SMatthew Dillon * NOTE! AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable 14064e21f4daSMatthew Dillon * on multiple chipsets and can brick the chipset or even 14074e21f4daSMatthew Dillon * the whole PC. Never use it. 14081980eff3SMatthew Dillon */ 14091980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1410258223a3SMatthew Dillon 1411*9abd2bb8SImre Vadász r = ap->ap_sc->sc_ipm_disable | AHCI_PREG_SCTL_SPM_DISABLED; 14121980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1413f2dba700SMatthew Dillon 1414f2dba700SMatthew Dillon retry: 1415f2dba700SMatthew Dillon /* 1416f2dba700SMatthew Dillon * Give the new power management state time to settle, then clear 1417f2dba700SMatthew Dillon * pending status. 1418f2dba700SMatthew Dillon */ 1419f2dba700SMatthew Dillon ahci_os_sleep(1000); 1420f2dba700SMatthew Dillon ahci_flush_tfd(ap); 1421f2dba700SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 14221980eff3SMatthew Dillon 14231980eff3SMatthew Dillon /* 1424493d3201SMatthew Dillon * Start transmitting COMRESET. The spec says that COMRESET must 1425493d3201SMatthew Dillon * be sent for at least 1ms but in actual fact numerous devices 1426493d3201SMatthew Dillon * appear to take much longer. Delay a whole second here. 1427493d3201SMatthew Dillon * 1428493d3201SMatthew Dillon * In addition, SATA-3 ports can take longer to train, so even 1429493d3201SMatthew Dillon * SATA-2 devices which would normally detect very quickly may 1430493d3201SMatthew Dillon * take longer when plugged into a SATA-3 port. 14311980eff3SMatthew Dillon */ 1432493d3201SMatthew Dillon r |= AHCI_PREG_SCTL_DET_INIT; 14338986d351SMatthew Dillon switch(AhciForceGen) { 14348986d351SMatthew Dillon case 0: 1435258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 14368986d351SMatthew Dillon break; 14378986d351SMatthew Dillon case 1: 14388986d351SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 14398986d351SMatthew Dillon break; 14408986d351SMatthew Dillon case 2: 14418986d351SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN2; 14428986d351SMatthew Dillon break; 14438986d351SMatthew Dillon case 3: 14448986d351SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN3; 14458986d351SMatthew Dillon break; 14468986d351SMatthew Dillon default: 14478986d351SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN3; 14488986d351SMatthew Dillon break; 14498986d351SMatthew Dillon } 1450258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1451492bffafSMatthew Dillon ahci_os_sleep(1000); 1452493d3201SMatthew Dillon 1453492bffafSMatthew Dillon ap->ap_flags &= ~AP_F_HARSH_REINIT; 1454cf5f3a81SMatthew Dillon 1455cf5f3a81SMatthew Dillon /* 1456cf5f3a81SMatthew Dillon * Only SERR_DIAG_X needs to be cleared for TFD updates, but 1457cf5f3a81SMatthew Dillon * since we are hard-resetting the port we might as well clear 1458f2dba700SMatthew Dillon * the whole enchillada. Also be sure to clear any spurious BSY 1459f2dba700SMatthew Dillon * prior to clearing INIT. 1460493d3201SMatthew Dillon * 1461493d3201SMatthew Dillon * Wait 1 whole second after clearing INIT before checking 1462493d3201SMatthew Dillon * the device detection bits in an attempt to work around chipsets 1463493d3201SMatthew Dillon * which do not properly mask PCS/PRCS during low level init. 1464cf5f3a81SMatthew Dillon */ 1465cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1466cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1467f2dba700SMatthew Dillon /* ahci_port_clo(ap);*/ 1468493d3201SMatthew Dillon ahci_os_sleep(10); 1469493d3201SMatthew Dillon 1470f2dba700SMatthew Dillon r &= ~AHCI_PREG_SCTL_SPD; 1471258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 1472258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 1473258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1474493d3201SMatthew Dillon ahci_os_sleep(1000); 1475258223a3SMatthew Dillon 14761980eff3SMatthew Dillon /* 14771980eff3SMatthew Dillon * Try to determine if there is a device on the port. 14781980eff3SMatthew Dillon * 14791980eff3SMatthew Dillon * Give the device 3/10 second to at least be detected. 14801980eff3SMatthew Dillon * If we fail clear PRCS (phy detect) since we may cycled 14811980eff3SMatthew Dillon * the phy and probably caused another PRCS interrupt. 14821980eff3SMatthew Dillon */ 148376497a9cSMatthew Dillon loop = 300; 148476497a9cSMatthew Dillon while (loop > 0) { 14851980eff3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 14861980eff3SMatthew Dillon if (r & AHCI_PREG_SSTS_DET) 14871980eff3SMatthew Dillon break; 148876497a9cSMatthew Dillon loop -= ahci_os_softsleep(); 14891980eff3SMatthew Dillon } 14901980eff3SMatthew Dillon if (loop == 0) { 14911980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 1492074579dfSMatthew Dillon if (bootverbose) { 14931980eff3SMatthew Dillon kprintf("%s: Port appears to be unplugged\n", 14941980eff3SMatthew Dillon PORTNAME(ap)); 1495074579dfSMatthew Dillon } 14963209f581SMatthew Dillon error = ENODEV; 149712feb904SMatthew Dillon goto done; 1498258223a3SMatthew Dillon } 1499258223a3SMatthew Dillon 1500cec85a37SMatthew Dillon /* 1501493d3201SMatthew Dillon * There is something on the port. Regardless of what happens 1502493d3201SMatthew Dillon * after this tell the caller to try to detect a port multiplier. 1503493d3201SMatthew Dillon * 1504493d3201SMatthew Dillon * Give the device 3 seconds to fully negotiate. 15051980eff3SMatthew Dillon */ 1506493d3201SMatthew Dillon *pmdetectp = 1; 1507493d3201SMatthew Dillon 150812feb904SMatthew Dillon if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS, 15091980eff3SMatthew Dillon AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) { 1510074579dfSMatthew Dillon if (bootverbose) { 15111980eff3SMatthew Dillon kprintf("%s: Device may be powered down\n", 15121980eff3SMatthew Dillon PORTNAME(ap)); 1513074579dfSMatthew Dillon } 15143209f581SMatthew Dillon error = ENODEV; 1515493d3201SMatthew Dillon goto done; 15161980eff3SMatthew Dillon } 15171980eff3SMatthew Dillon 151812feb904SMatthew Dillon /* 151912feb904SMatthew Dillon * We got something that definitely looks like a device. Give 152012feb904SMatthew Dillon * the device time to send us its first D2H FIS. Waiting for 152112feb904SMatthew Dillon * BSY to clear accomplishes this. 152212feb904SMatthew Dillon * 1523493d3201SMatthew Dillon * NOTE: A port multiplier may or may not clear BSY here, 152412feb904SMatthew Dillon * depending on what is sitting in target 0 behind it. 1525f2dba700SMatthew Dillon * 1526f2dba700SMatthew Dillon * NOTE: Intel SSDs seem to have compatibility problems with Intel 1527f2dba700SMatthew Dillon * mobo's on cold boots and may leave BSY set. A single 1528f2dba700SMatthew Dillon * retry works around the problem. This is definitely a bug 1529f2dba700SMatthew Dillon * with the mobo and/or the SSD and does not appear to occur 1530f2dba700SMatthew Dillon * with other devices connected to the same port. 153112feb904SMatthew Dillon */ 1532c408a8b3SMatthew Dillon ahci_flush_tfd(ap); 1533f2dba700SMatthew Dillon if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD, 15341980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1535f2dba700SMatthew Dillon kprintf("%s: Device BUSY: %b\n", 1536f2dba700SMatthew Dillon PORTNAME(ap), 1537f2dba700SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), 1538f2dba700SMatthew Dillon AHCI_PFMT_TFD_STS); 1539f2dba700SMatthew Dillon if (retries == 0) { 1540f2dba700SMatthew Dillon kprintf("%s: Retrying\n", PORTNAME(ap)); 1541f2dba700SMatthew Dillon retries = 1; 1542f2dba700SMatthew Dillon goto retry; 1543f2dba700SMatthew Dillon } 154412feb904SMatthew Dillon error = EBUSY; 15451980eff3SMatthew Dillon } else { 15463209f581SMatthew Dillon error = 0; 15471980eff3SMatthew Dillon } 1548258223a3SMatthew Dillon 154912feb904SMatthew Dillon done: 1550493d3201SMatthew Dillon ahci_flush_tfd(ap); 1551493d3201SMatthew Dillon return error; 1552493d3201SMatthew Dillon } 1553493d3201SMatthew Dillon 1554493d3201SMatthew Dillon 1555493d3201SMatthew Dillon /* 1556493d3201SMatthew Dillon * AHCI port reset, Section 10.4.2 1557493d3201SMatthew Dillon * 1558493d3201SMatthew Dillon * This function does a hard reset of the port. Note that the device 1559493d3201SMatthew Dillon * connected to the port could still end-up hung. 1560493d3201SMatthew Dillon */ 1561493d3201SMatthew Dillon int 1562493d3201SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard) 1563493d3201SMatthew Dillon { 1564493d3201SMatthew Dillon u_int32_t data; 1565493d3201SMatthew Dillon int error; 1566493d3201SMatthew Dillon int pmdetect; 1567493d3201SMatthew Dillon 1568493d3201SMatthew Dillon if (bootverbose) 1569493d3201SMatthew Dillon kprintf("%s: START HARDRESET\n", PORTNAME(ap)); 1570493d3201SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 1571493d3201SMatthew Dillon 1572493d3201SMatthew Dillon error = ahci_comreset(ap, &pmdetect); 1573493d3201SMatthew Dillon 1574493d3201SMatthew Dillon /* 1575493d3201SMatthew Dillon * We may be asked to perform a port multiplier check even if the 1576493d3201SMatthew Dillon * comreset failed. This typically occurs when the PM has nothing 1577493d3201SMatthew Dillon * in slot 0, which can cause BSY to remain set. 1578493d3201SMatthew Dillon * 1579493d3201SMatthew Dillon * If the PM detection is successful it will override (error), 1580493d3201SMatthew Dillon * otherwise (error) is retained. If an error does occur it 1581493d3201SMatthew Dillon * is possible that a normal device has blown up on us DUE to 1582493d3201SMatthew Dillon * the PM detection code, so re-run the comreset and assume 1583493d3201SMatthew Dillon * a normal device. 1584493d3201SMatthew Dillon */ 1585493d3201SMatthew Dillon if (pmdetect) { 1586493d3201SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) { 1587493d3201SMatthew Dillon error = ahci_pm_port_probe(ap, error); 1588493d3201SMatthew Dillon if (error) { 1589493d3201SMatthew Dillon error = ahci_comreset(ap, &pmdetect); 1590493d3201SMatthew Dillon } 1591493d3201SMatthew Dillon } 1592493d3201SMatthew Dillon } 1593493d3201SMatthew Dillon 159412feb904SMatthew Dillon /* 159512feb904SMatthew Dillon * Finish up. 159612feb904SMatthew Dillon */ 1597493d3201SMatthew Dillon ahci_os_sleep(500); 1598493d3201SMatthew Dillon 159912feb904SMatthew Dillon switch(error) { 160012feb904SMatthew Dillon case 0: 160112feb904SMatthew Dillon /* 160212feb904SMatthew Dillon * All good, make sure the port is running and set the 160312feb904SMatthew Dillon * probe state. Ignore the signature junk (it's unreliable) 160412feb904SMatthew Dillon * until we get to the softreset code. 160512feb904SMatthew Dillon */ 160612feb904SMatthew Dillon if (ahci_port_start(ap)) { 160712feb904SMatthew Dillon kprintf("%s: failed to start command DMA on port, " 160812feb904SMatthew Dillon "disabling\n", PORTNAME(ap)); 160912feb904SMatthew Dillon error = EBUSY; 1610493d3201SMatthew Dillon break; 161112feb904SMatthew Dillon } 1612f4553de1SMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 1613f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_GOOD; 1614f4553de1SMatthew Dillon else 1615f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 161612feb904SMatthew Dillon break; 161712feb904SMatthew Dillon case ENODEV: 1618fd8bd957SMatthew Dillon /* 161912feb904SMatthew Dillon * Normal device probe failure 16201980eff3SMatthew Dillon */ 162112feb904SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SSTS); 16221980eff3SMatthew Dillon 162312feb904SMatthew Dillon switch(data & AHCI_PREG_SSTS_DET) { 162412feb904SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 162512feb904SMatthew Dillon kprintf("%s: Device not communicating\n", 16261980eff3SMatthew Dillon PORTNAME(ap)); 162712feb904SMatthew Dillon break; 162812feb904SMatthew Dillon case AHCI_PREG_SSTS_DET_PHYOFFLINE: 162912feb904SMatthew Dillon kprintf("%s: PHY offline\n", 163012feb904SMatthew Dillon PORTNAME(ap)); 163112feb904SMatthew Dillon break; 163212feb904SMatthew Dillon default: 163312feb904SMatthew Dillon kprintf("%s: No device detected\n", 163412feb904SMatthew Dillon PORTNAME(ap)); 163512feb904SMatthew Dillon break; 16361980eff3SMatthew Dillon } 163712feb904SMatthew Dillon ahci_port_hardstop(ap); 163812feb904SMatthew Dillon break; 163912feb904SMatthew Dillon default: 16401980eff3SMatthew Dillon /* 164112feb904SMatthew Dillon * Abnormal probe (EBUSY) 16421980eff3SMatthew Dillon */ 164312feb904SMatthew Dillon kprintf("%s: Device on port is bricked\n", 164412feb904SMatthew Dillon PORTNAME(ap)); 164512feb904SMatthew Dillon ahci_port_hardstop(ap); 164612feb904SMatthew Dillon #if 0 164712feb904SMatthew Dillon rc = ahci_port_reset(ap, atx, 0); 164812feb904SMatthew Dillon if (rc) { 164912feb904SMatthew Dillon kprintf("%s: Unable unbrick device\n", 165012feb904SMatthew Dillon PORTNAME(ap)); 16511980eff3SMatthew Dillon } else { 165212feb904SMatthew Dillon kprintf("%s: Successfully unbricked\n", 16533209f581SMatthew Dillon PORTNAME(ap)); 165412feb904SMatthew Dillon } 165512feb904SMatthew Dillon #endif 165612feb904SMatthew Dillon break; 16573209f581SMatthew Dillon } 16581067474aSMatthew Dillon 16591067474aSMatthew Dillon /* 166012feb904SMatthew Dillon * Clean up 16611067474aSMatthew Dillon */ 166212feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 166312feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 16643209f581SMatthew Dillon 166512feb904SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 16661980eff3SMatthew Dillon 166712feb904SMatthew Dillon if (bootverbose) 166812feb904SMatthew Dillon kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error); 1669831bc9e3SMatthew Dillon return (error); 16701980eff3SMatthew Dillon } 16711980eff3SMatthew Dillon 16721980eff3SMatthew Dillon /* 1673cf5f3a81SMatthew Dillon * Hard-stop on hot-swap device removal. See 10.10.1 1674cf5f3a81SMatthew Dillon * 1675cf5f3a81SMatthew Dillon * Place the port in a mode that will allow it to detect hot-swap insertions. 1676cf5f3a81SMatthew Dillon * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't 1677cf5f3a81SMatthew Dillon * seem to do the job. 1678f17a0cedSMatthew Dillon * 1679f17a0cedSMatthew Dillon * FIS reception is left enabled but command processing is disabled. 1680f17a0cedSMatthew Dillon * Cycling FIS reception (FRE) can brick ports. 1681cf5f3a81SMatthew Dillon */ 1682cf5f3a81SMatthew Dillon void 1683cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap) 1684cf5f3a81SMatthew Dillon { 168576497a9cSMatthew Dillon struct ahci_ccb *ccb; 16861980eff3SMatthew Dillon struct ata_port *at; 1687cf5f3a81SMatthew Dillon u_int32_t r; 1688cf5f3a81SMatthew Dillon u_int32_t cmd; 168976497a9cSMatthew Dillon int slot; 16901980eff3SMatthew Dillon int i; 1691bb79834dSMatthew Dillon int serial; 1692cf5f3a81SMatthew Dillon 1693cf5f3a81SMatthew Dillon /* 1694cf5f3a81SMatthew Dillon * Stop the port. We can't modify things like SUD if the port 1695cf5f3a81SMatthew Dillon * is running. 1696cf5f3a81SMatthew Dillon */ 1697cf5f3a81SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 16981980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 16991980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1700cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 1701cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 1702492bffafSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC); 1703492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1704cf5f3a81SMatthew Dillon 1705cf5f3a81SMatthew Dillon /* 17061980eff3SMatthew Dillon * Clean up AT sub-ports on SATA port. 17071980eff3SMatthew Dillon */ 17081980eff3SMatthew Dillon for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) { 1709b012a2caSMatthew Dillon at = ap->ap_ata[i]; 17101980eff3SMatthew Dillon at->at_type = ATA_PORT_T_NONE; 17113209f581SMatthew Dillon at->at_probe = ATA_PROBE_FAILED; 17121980eff3SMatthew Dillon } 17131980eff3SMatthew Dillon 17141980eff3SMatthew Dillon /* 1715cf5f3a81SMatthew Dillon * Make sure FRE is active. There isn't anything we can do if it 1716cf5f3a81SMatthew Dillon * fails so just ignore errors. 1717cf5f3a81SMatthew Dillon */ 1718cf5f3a81SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 1719cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE; 1720cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1721cf5f3a81SMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) 1722cf5f3a81SMatthew Dillon ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR); 1723cf5f3a81SMatthew Dillon } 1724cf5f3a81SMatthew Dillon 1725cf5f3a81SMatthew Dillon /* 1726cf5f3a81SMatthew Dillon * 10.10.1 place us in the Listen state. 1727cf5f3a81SMatthew Dillon * 17285502cf24SMatthew Dillon * 10.10.3 DET must be set to 0 and found to be 0 before 17295502cf24SMatthew Dillon * setting SUD to 0. 17305502cf24SMatthew Dillon * 17315502cf24SMatthew Dillon * Deactivating SUD only applies if the controller supports SUD, it 17325502cf24SMatthew Dillon * is a bit unclear what happens w/regards to detecting hotplug 17335502cf24SMatthew Dillon * if it doesn't. 1734cf5f3a81SMatthew Dillon */ 1735*9abd2bb8SImre Vadász r = ap->ap_sc->sc_ipm_disable | AHCI_PREG_SCTL_SPM_DISABLED; 17365502cf24SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 17375502cf24SMatthew Dillon ahci_os_sleep(10); 1738cf5f3a81SMatthew Dillon cmd &= ~AHCI_PREG_CMD_SUD; 1739cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 17405502cf24SMatthew Dillon ahci_os_sleep(10); 1741cf5f3a81SMatthew Dillon 1742cf5f3a81SMatthew Dillon /* 17435502cf24SMatthew Dillon * 10.10.1 17445502cf24SMatthew Dillon * 17455502cf24SMatthew Dillon * Transition su to the spin-up state. HBA shall send COMRESET and 17465502cf24SMatthew Dillon * begin initialization sequence (whatever that means). Presumably 17475502cf24SMatthew Dillon * this is edge-triggered. Following the spin-up state the HBA 17485502cf24SMatthew Dillon * will automatically transition to the Normal state. 1749cf5f3a81SMatthew Dillon * 1750cf5f3a81SMatthew Dillon * This only applies if the controller supports SUD. 17514e21f4daSMatthew Dillon * NEVER use AHCI_PREG_DET_DISABLE. 1752cf5f3a81SMatthew Dillon */ 17535502cf24SMatthew Dillon cmd |= AHCI_PREG_CMD_POD | 17545502cf24SMatthew Dillon AHCI_PREG_CMD_SUD | 17555502cf24SMatthew Dillon AHCI_PREG_CMD_ICC_ACTIVE; 1756cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 17575502cf24SMatthew Dillon ahci_os_sleep(10); 1758cf5f3a81SMatthew Dillon 1759cf5f3a81SMatthew Dillon /* 1760cf5f3a81SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 1761cf5f3a81SMatthew Dillon */ 1762cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1763cf5f3a81SMatthew Dillon 1764cf5f3a81SMatthew Dillon /* 176576497a9cSMatthew Dillon * Clean out pending ccbs 176676497a9cSMatthew Dillon */ 1767bb79834dSMatthew Dillon restart: 176876497a9cSMatthew Dillon while (ap->ap_active) { 176976497a9cSMatthew Dillon slot = ffs(ap->ap_active) - 1; 177076497a9cSMatthew Dillon ap->ap_active &= ~(1 << slot); 177176497a9cSMatthew Dillon --ap->ap_active_cnt; 177276497a9cSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 177376497a9cSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) { 1774bb79834dSMatthew Dillon serial = ccb->ccb_xa.serial; 177546528d33SMatthew Dillon callout_stop_sync(&ccb->ccb_timeout); 1776bb79834dSMatthew Dillon if (serial != ccb->ccb_xa.serial) { 1777bb79834dSMatthew Dillon kprintf("%s: Warning: timeout race ccb %p\n", 1778bb79834dSMatthew Dillon PORTNAME(ap), ccb); 1779bb79834dSMatthew Dillon goto restart; 1780bb79834dSMatthew Dillon } 178176497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 178276497a9cSMatthew Dillon } 178346528d33SMatthew Dillon ap->ap_expired &= ~(1 << slot); 178476497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED | 178576497a9cSMatthew Dillon ATA_F_TIMEOUT_EXPIRED); 178676497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 178776497a9cSMatthew Dillon ccb->ccb_done(ccb); 178876497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 178976497a9cSMatthew Dillon } 179076497a9cSMatthew Dillon while (ap->ap_sactive) { 179176497a9cSMatthew Dillon slot = ffs(ap->ap_sactive) - 1; 179276497a9cSMatthew Dillon ap->ap_sactive &= ~(1 << slot); 179376497a9cSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 179476497a9cSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) { 1795bb79834dSMatthew Dillon serial = ccb->ccb_xa.serial; 179646528d33SMatthew Dillon callout_stop_sync(&ccb->ccb_timeout); 1797bb79834dSMatthew Dillon if (serial != ccb->ccb_xa.serial) { 1798bb79834dSMatthew Dillon kprintf("%s: Warning: timeout race ccb %p\n", 1799bb79834dSMatthew Dillon PORTNAME(ap), ccb); 1800bb79834dSMatthew Dillon goto restart; 1801bb79834dSMatthew Dillon } 180276497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 180376497a9cSMatthew Dillon } 180446528d33SMatthew Dillon ap->ap_expired &= ~(1 << slot); 180576497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED | 180676497a9cSMatthew Dillon ATA_F_TIMEOUT_EXPIRED); 180776497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 180876497a9cSMatthew Dillon ccb->ccb_done(ccb); 180976497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 181076497a9cSMatthew Dillon } 181176497a9cSMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 181276497a9cSMatthew Dillon 181376497a9cSMatthew Dillon while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) { 181476497a9cSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 181576497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 181676497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED; 181776497a9cSMatthew Dillon ccb->ccb_done(ccb); 181876497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 181976497a9cSMatthew Dillon } 182076497a9cSMatthew Dillon 182176497a9cSMatthew Dillon /* 18225502cf24SMatthew Dillon * Hot-plug device detection should work at this point. e.g. on 18235502cf24SMatthew Dillon * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug 18245502cf24SMatthew Dillon * detection and entering RESET (continuous COMRESET by setting INIT) 18255502cf24SMatthew Dillon * will actually prevent hot-plug detection from working properly. 1826cf5f3a81SMatthew Dillon * 18275502cf24SMatthew Dillon * There may be cases where this will fail to work, I have some 18285502cf24SMatthew Dillon * additional code to place the HBA in RESET (send continuous 18295502cf24SMatthew Dillon * COMRESET) and hopefully get DIAG.X or other events when something 18305502cf24SMatthew Dillon * is plugged in. Unfortunately this isn't universal and can 18315502cf24SMatthew Dillon * also prevent events from generating interrupts. 1832cf5f3a81SMatthew Dillon */ 18335502cf24SMatthew Dillon 18345502cf24SMatthew Dillon #if 0 18355502cf24SMatthew Dillon /* 18365502cf24SMatthew Dillon * Transition us to the Reset state. Theoretically we send a 18375502cf24SMatthew Dillon * continuous stream of COMRESETs in this state. 18385502cf24SMatthew Dillon */ 18395502cf24SMatthew Dillon r |= AHCI_PREG_SCTL_DET_INIT; 18405502cf24SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 18415502cf24SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 18425502cf24SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 18435502cf24SMatthew Dillon } else { 18445502cf24SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 18455502cf24SMatthew Dillon } 18465502cf24SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 18475502cf24SMatthew Dillon ahci_os_sleep(10); 18485502cf24SMatthew Dillon 18495502cf24SMatthew Dillon /* 18505502cf24SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 18515502cf24SMatthew Dillon */ 18525502cf24SMatthew Dillon ahci_flush_tfd(ap); 18535502cf24SMatthew Dillon #endif 1854cf5f3a81SMatthew Dillon /* NOP */ 1855cf5f3a81SMatthew Dillon } 1856cf5f3a81SMatthew Dillon 1857cf5f3a81SMatthew Dillon /* 1858c408a8b3SMatthew Dillon * We can't loop on the X bit, a continuous COMINIT received will make 1859c408a8b3SMatthew Dillon * it loop forever. Just assume one event has built up and clear X 1860c408a8b3SMatthew Dillon * so the task file descriptor can update. 1861cf5f3a81SMatthew Dillon */ 1862cf5f3a81SMatthew Dillon void 1863cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap) 1864cf5f3a81SMatthew Dillon { 1865cf5f3a81SMatthew Dillon u_int32_t r; 1866cf5f3a81SMatthew Dillon 1867cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 1868c408a8b3SMatthew Dillon if (r & AHCI_PREG_SERR_DIAG_X) 18691980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X); 1870cf5f3a81SMatthew Dillon } 1871cf5f3a81SMatthew Dillon 1872cf5f3a81SMatthew Dillon /* 1873fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 1874fd8bd957SMatthew Dillon * DISK. 1875fd8bd957SMatthew Dillon */ 1876fd8bd957SMatthew Dillon int 18771980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at) 1878fd8bd957SMatthew Dillon { 1879fd8bd957SMatthew Dillon u_int32_t sig; 1880fd8bd957SMatthew Dillon 1881fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 1882074579dfSMatthew Dillon if (bootverbose) 18831980eff3SMatthew Dillon kprintf("%s: sig %08x\n", ATANAME(ap, at), sig); 1884fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 1885fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 18861980eff3SMatthew Dillon } else if ((sig & 0xffff0000) == 18871980eff3SMatthew Dillon (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) { 18881980eff3SMatthew Dillon return(ATA_PORT_T_PM); 1889fd8bd957SMatthew Dillon } else { 1890fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 1891fd8bd957SMatthew Dillon } 1892fd8bd957SMatthew Dillon } 1893fd8bd957SMatthew Dillon 1894fd8bd957SMatthew Dillon /* 1895fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 1896fd8bd957SMatthew Dillon */ 1897258223a3SMatthew Dillon int 1898258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 1899258223a3SMatthew Dillon { 1900258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1901258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1902258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1903258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 1904258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1905258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 1906258223a3SMatthew Dillon int error; 1907258223a3SMatthew Dillon 1908258223a3SMatthew Dillon if (xa->datalen == 0) { 1909258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 1910258223a3SMatthew Dillon return (0); 1911258223a3SMatthew Dillon } 1912258223a3SMatthew Dillon 1913258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 1914258223a3SMatthew Dillon xa->data, xa->datalen, 1915258223a3SMatthew Dillon ahci_load_prdt_callback, 1916258223a3SMatthew Dillon &prdt, 1917258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 1918258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 1919258223a3SMatthew Dillon if (error != 0) { 1920258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 1921258223a3SMatthew Dillon return (1); 1922258223a3SMatthew Dillon } 192312feb904SMatthew Dillon #if 0 1924258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 1925258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 192612feb904SMatthew Dillon #endif 1927258223a3SMatthew Dillon 1928258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 1929258223a3SMatthew Dillon 1930b012a2caSMatthew Dillon if (xa->flags & ATA_F_READ) 1931b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD); 1932b012a2caSMatthew Dillon if (xa->flags & ATA_F_WRITE) 1933b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE); 1934258223a3SMatthew Dillon 1935258223a3SMatthew Dillon return (0); 1936258223a3SMatthew Dillon } 1937258223a3SMatthew Dillon 1938258223a3SMatthew Dillon /* 1939258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 1940258223a3SMatthew Dillon * list is a temporary structure. 1941258223a3SMatthew Dillon */ 1942258223a3SMatthew Dillon static 1943258223a3SMatthew Dillon void 1944258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 1945258223a3SMatthew Dillon int error) 1946258223a3SMatthew Dillon { 1947258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 1948258223a3SMatthew Dillon u_int64_t addr; 1949258223a3SMatthew Dillon 1950258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 1951258223a3SMatthew Dillon 1952258223a3SMatthew Dillon while (nsegs) { 1953258223a3SMatthew Dillon addr = segs->ds_addr; 1954258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 1955258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 1956258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 1957258223a3SMatthew Dillon --nsegs; 1958258223a3SMatthew Dillon if (nsegs) 1959258223a3SMatthew Dillon ++prd; 1960258223a3SMatthew Dillon ++segs; 1961258223a3SMatthew Dillon } 1962258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 1963258223a3SMatthew Dillon } 1964258223a3SMatthew Dillon 1965258223a3SMatthew Dillon void 1966258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 1967258223a3SMatthew Dillon { 1968258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1969258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1970258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1971258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1972258223a3SMatthew Dillon 1973258223a3SMatthew Dillon if (xa->datalen != 0) { 1974b012a2caSMatthew Dillon if (xa->flags & ATA_F_READ) { 1975258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1976b012a2caSMatthew Dillon BUS_DMASYNC_POSTREAD); 1977b012a2caSMatthew Dillon } 1978b012a2caSMatthew Dillon if (xa->flags & ATA_F_WRITE) { 1979b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1980b012a2caSMatthew Dillon BUS_DMASYNC_POSTWRITE); 1981b012a2caSMatthew Dillon } 1982258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1983258223a3SMatthew Dillon 1984f7d09f74SMatthew Dillon /* 1985f7d09f74SMatthew Dillon * prdbc is only updated by hardware for non-NCQ commands. 1986f7d09f74SMatthew Dillon */ 1987f7d09f74SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 1988f7d09f74SMatthew Dillon xa->resid = 0; 1989f7d09f74SMatthew Dillon } else { 199050a3ecb6SMatthew Dillon if (ccb->ccb_cmd_hdr->prdbc == 0 && 199150a3ecb6SMatthew Dillon ccb->ccb_xa.state == ATA_S_COMPLETE) { 1992f7d09f74SMatthew Dillon kprintf("%s: WARNING! Unload prdbc resid " 1993f7d09f74SMatthew Dillon "was zero! tag=%d\n", 199412feb904SMatthew Dillon ATANAME(ap, xa->at), ccb->ccb_slot); 199512feb904SMatthew Dillon } 1996258223a3SMatthew Dillon xa->resid = xa->datalen - 1997258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 1998258223a3SMatthew Dillon } 1999258223a3SMatthew Dillon } 2000f7d09f74SMatthew Dillon } 2001258223a3SMatthew Dillon 20025f8c1efdSMatthew Dillon /* 20035f8c1efdSMatthew Dillon * Start a command and poll for completion. 20045f8c1efdSMatthew Dillon * 20053209f581SMatthew Dillon * timeout is in ms and only counts once the command gets on-chip. 20063209f581SMatthew Dillon * 2007831bc9e3SMatthew Dillon * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine 2008831bc9e3SMatthew Dillon * that no error occured. 2009831bc9e3SMatthew Dillon * 20105f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 20115f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 20125f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 20135f8c1efdSMatthew Dillon * 20145f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 2015cf5f3a81SMatthew Dillon * 2016cf5f3a81SMatthew Dillon * NOTE: If the port is in a failed state and stopped we do not try 2017cf5f3a81SMatthew Dillon * to activate the ccb. 20185f8c1efdSMatthew Dillon */ 2019258223a3SMatthew Dillon int 2020831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, 2021831bc9e3SMatthew Dillon void (*timeout_fn)(struct ahci_ccb *)) 2022258223a3SMatthew Dillon { 2023258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2024258223a3SMatthew Dillon 2025cf5f3a81SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) { 2026cf5f3a81SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 2027831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 2028cf5f3a81SMatthew Dillon } 2029258223a3SMatthew Dillon crit_enter(); 203012feb904SMatthew Dillon #if 0 203112feb904SMatthew Dillon kprintf("%s: Start command %02x tag=%d\n", 203212feb904SMatthew Dillon ATANAME(ccb->ccb_port, ccb->ccb_xa.at), 203312feb904SMatthew Dillon ccb->ccb_xa.fis->command, ccb->ccb_slot); 203412feb904SMatthew Dillon #endif 2035258223a3SMatthew Dillon ahci_start(ccb); 20361980eff3SMatthew Dillon 2037258223a3SMatthew Dillon do { 2038f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 2039831bc9e3SMatthew Dillon switch(ccb->ccb_xa.state) { 2040831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 2041831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 2042f4553de1SMatthew Dillon break; 2043831bc9e3SMatthew Dillon case ATA_S_PENDING: 2044831bc9e3SMatthew Dillon ahci_os_softsleep(); 2045831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 2046831bc9e3SMatthew Dillon break; 2047831bc9e3SMatthew Dillon default: 2048831bc9e3SMatthew Dillon crit_exit(); 2049831bc9e3SMatthew Dillon return (ccb->ccb_xa.state); 2050f4553de1SMatthew Dillon } 20513209f581SMatthew Dillon } while (timeout > 0); 20525f8c1efdSMatthew Dillon 2053492bffafSMatthew Dillon if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) { 2054831bc9e3SMatthew Dillon kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n", 2055831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, 2056831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 2057831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS, 2058831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR); 2059492bffafSMatthew Dillon } 20605f8c1efdSMatthew Dillon 2061258223a3SMatthew Dillon timeout_fn(ccb); 2062831bc9e3SMatthew Dillon 2063258223a3SMatthew Dillon crit_exit(); 2064258223a3SMatthew Dillon 2065831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 2066831bc9e3SMatthew Dillon } 2067831bc9e3SMatthew Dillon 2068831bc9e3SMatthew Dillon /* 2069831bc9e3SMatthew Dillon * When polling we have to check if the currently active CCB(s) 2070831bc9e3SMatthew Dillon * have timed out as the callout will be deadlocked while we 2071831bc9e3SMatthew Dillon * hold the port lock. 2072831bc9e3SMatthew Dillon */ 2073831bc9e3SMatthew Dillon void 2074831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap) 2075831bc9e3SMatthew Dillon { 2076831bc9e3SMatthew Dillon struct ahci_ccb *ccb; 2077831bc9e3SMatthew Dillon u_int32_t mask; 2078831bc9e3SMatthew Dillon int tag; 2079831bc9e3SMatthew Dillon 2080831bc9e3SMatthew Dillon mask = ap->ap_active | ap->ap_sactive; 2081831bc9e3SMatthew Dillon while (mask) { 2082831bc9e3SMatthew Dillon tag = ffs(mask) - 1; 2083831bc9e3SMatthew Dillon mask &= ~(1 << tag); 2084831bc9e3SMatthew Dillon ccb = &ap->ap_ccbs[tag]; 2085831bc9e3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) { 2086831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(ccb); 2087831bc9e3SMatthew Dillon } 2088831bc9e3SMatthew Dillon } 2089258223a3SMatthew Dillon } 2090258223a3SMatthew Dillon 20913209f581SMatthew Dillon static 20923209f581SMatthew Dillon __inline 20933209f581SMatthew Dillon void 20943209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb) 20953209f581SMatthew Dillon { 20963209f581SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) { 20973209f581SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING; 20983209f581SMatthew Dillon callout_reset(&ccb->ccb_timeout, 20993209f581SMatthew Dillon (ccb->ccb_xa.timeout * hz + 999) / 1000, 21003209f581SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 21013209f581SMatthew Dillon } 21023209f581SMatthew Dillon } 21033209f581SMatthew Dillon 2104258223a3SMatthew Dillon void 2105258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 2106258223a3SMatthew Dillon { 2107258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 2108258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 2109258223a3SMatthew Dillon 2110258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 2111258223a3SMatthew Dillon 2112258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 2113258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 2114258223a3SMatthew Dillon 2115258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 2116258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2117258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2118258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 2119258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2120258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2121258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 2122258223a3SMatthew Dillon 2123258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 2124258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2125258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2126258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 2127258223a3SMatthew Dillon 21281980eff3SMatthew Dillon /* 21294c339a5fSMatthew Dillon * There's no point trying to optimize this, it only shaves a few 21304c339a5fSMatthew Dillon * nanoseconds so just queue the command and call our generic issue. 21311980eff3SMatthew Dillon */ 21324c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, ccb); 2133258223a3SMatthew Dillon } 2134258223a3SMatthew Dillon 2135831bc9e3SMatthew Dillon /* 2136831bc9e3SMatthew Dillon * While holding the port lock acquire exclusive access to the port. 2137831bc9e3SMatthew Dillon * 2138831bc9e3SMatthew Dillon * This is used when running the state machine to initialize and identify 2139831bc9e3SMatthew Dillon * targets over a port multiplier. Setting exclusive access prevents 2140831bc9e3SMatthew Dillon * ahci_port_intr() from activating any requests sitting on the pending 2141831bc9e3SMatthew Dillon * queue. 2142831bc9e3SMatthew Dillon */ 2143831bc9e3SMatthew Dillon void 2144831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at) 2145831bc9e3SMatthew Dillon { 2146831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0); 2147831bc9e3SMatthew Dillon ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS; 2148831bc9e3SMatthew Dillon while (ap->ap_active || ap->ap_sactive) { 2149831bc9e3SMatthew Dillon ahci_port_intr(ap, 1); 2150831bc9e3SMatthew Dillon ahci_os_softsleep(); 2151831bc9e3SMatthew Dillon } 2152831bc9e3SMatthew Dillon } 2153831bc9e3SMatthew Dillon 2154831bc9e3SMatthew Dillon void 2155831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at) 2156831bc9e3SMatthew Dillon { 2157831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0); 2158831bc9e3SMatthew Dillon ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS; 21594c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 2160831bc9e3SMatthew Dillon } 2161831bc9e3SMatthew Dillon 21621980eff3SMatthew Dillon /* 21634c339a5fSMatthew Dillon * If ccb is not NULL enqueue and/or issue it. 21644c339a5fSMatthew Dillon * 21654c339a5fSMatthew Dillon * If ccb is NULL issue whatever we can from the queue. However, nothing 21664c339a5fSMatthew Dillon * new is issued if the exclusive access flag is set or expired ccb's are 21674c339a5fSMatthew Dillon * present. 21684c339a5fSMatthew Dillon * 21694c339a5fSMatthew Dillon * If existing commands are still active (ap_active/ap_sactive) we can only 21704c339a5fSMatthew Dillon * issue matching new commands. 21711980eff3SMatthew Dillon */ 21724c339a5fSMatthew Dillon void 21734c339a5fSMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb) 21744c339a5fSMatthew Dillon { 21754c339a5fSMatthew Dillon u_int32_t mask; 21764c339a5fSMatthew Dillon int limit; 2177258223a3SMatthew Dillon 21781980eff3SMatthew Dillon /* 21794c339a5fSMatthew Dillon * Enqueue the ccb. 21804c339a5fSMatthew Dillon * 21814c339a5fSMatthew Dillon * If just running the queue and in exclusive access mode we 21824c339a5fSMatthew Dillon * just return. Also in this case if there are any expired ccb's 21834c339a5fSMatthew Dillon * we want to clear the queue so the port can be safely stopped. 21844c339a5fSMatthew Dillon */ 21854c339a5fSMatthew Dillon if (ccb) { 21864c339a5fSMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 21874c339a5fSMatthew Dillon } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) { 21884c339a5fSMatthew Dillon return; 21894c339a5fSMatthew Dillon } 21904c339a5fSMatthew Dillon 21914c339a5fSMatthew Dillon /* 21924c339a5fSMatthew Dillon * Pull the next ccb off the queue and run it if possible. 2193c1fd1d86SMatthew Dillon * 2194c1fd1d86SMatthew Dillon * The error CCB supercedes all normal queue operations and 2195c1fd1d86SMatthew Dillon * implies exclusive access while the error CCB is active. 21964c339a5fSMatthew Dillon */ 2197c1fd1d86SMatthew Dillon if (ccb != ap->ap_err_ccb) { 21984c339a5fSMatthew Dillon if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL) 21994c339a5fSMatthew Dillon return; 2200c1fd1d86SMatthew Dillon if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) { 2201c1fd1d86SMatthew Dillon kprintf("DELAY CCB slot %d\n", ccb->ccb_slot); 2202c1fd1d86SMatthew Dillon return; 2203c1fd1d86SMatthew Dillon } 2204c1fd1d86SMatthew Dillon } 22054c339a5fSMatthew Dillon 220612feb904SMatthew Dillon /* 220712feb904SMatthew Dillon * Handle exclusivity requirements. 220812feb904SMatthew Dillon * 220912feb904SMatthew Dillon * ATA_F_EXCLUSIVE is used when we want to be the only command 221012feb904SMatthew Dillon * running. 221112feb904SMatthew Dillon * 221212feb904SMatthew Dillon * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded 221312feb904SMatthew Dillon * back into the ccb on a normal (non-errored) command completion. 221412feb904SMatthew Dillon * For example, for PM requests to target 15. Because the AHCI 221512feb904SMatthew Dillon * spec does not stop the command processor and has only one rfis 221612feb904SMatthew Dillon * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE. 221712feb904SMatthew Dillon * Otherwise multiple completions can destroy the rfis data before 221812feb904SMatthew Dillon * we have a chance to copy it. 221912feb904SMatthew Dillon */ 222012feb904SMatthew Dillon if (ap->ap_active & ~ap->ap_expired) { 222112feb904SMatthew Dillon /* 222212feb904SMatthew Dillon * There may be multiple ccb's already running, 222312feb904SMatthew Dillon * if any are running and ap_run_flags sets 222412feb904SMatthew Dillon * one of these flags then we know only one is 222512feb904SMatthew Dillon * running. 222612feb904SMatthew Dillon * 222712feb904SMatthew Dillon * XXX Current AUTOSENSE code forces exclusivity 222812feb904SMatthew Dillon * to simplify the code. 222912feb904SMatthew Dillon */ 223012feb904SMatthew Dillon if (ap->ap_run_flags & 223112feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) { 223212feb904SMatthew Dillon return; 223312feb904SMatthew Dillon } 223412feb904SMatthew Dillon 223512feb904SMatthew Dillon if (ccb->ccb_xa.flags & 223612feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) { 223712feb904SMatthew Dillon return; 223812feb904SMatthew Dillon } 223912feb904SMatthew Dillon } 224012feb904SMatthew Dillon 22414c339a5fSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 22424c339a5fSMatthew Dillon /* 22434c339a5fSMatthew Dillon * The next command is a NCQ command and can be issued as 22444c339a5fSMatthew Dillon * long as currently active commands are not standard. 22454c339a5fSMatthew Dillon */ 22464c339a5fSMatthew Dillon if (ap->ap_active) { 22474c339a5fSMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 22484c339a5fSMatthew Dillon return; 22494c339a5fSMatthew Dillon } 22504c339a5fSMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 22514c339a5fSMatthew Dillon 22524c339a5fSMatthew Dillon mask = 0; 22534c339a5fSMatthew Dillon do { 22544c339a5fSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 2255d16d3400SMatthew Dillon KKASSERT((mask & (1 << ccb->ccb_slot)) == 0); 22564c339a5fSMatthew Dillon mask |= 1 << ccb->ccb_slot; 2257d16d3400SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 2258d16d3400SMatthew Dillon KKASSERT(ccb == &ap->ap_ccbs[ccb->ccb_slot]); 22594c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 226012feb904SMatthew Dillon ahci_start_timeout(ccb); 226112feb904SMatthew Dillon ap->ap_run_flags = ccb->ccb_xa.flags; 22624c339a5fSMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_pending); 226312feb904SMatthew Dillon } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) && 226412feb904SMatthew Dillon (ap->ap_run_flags & 226512feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0); 22664c339a5fSMatthew Dillon 2267d16d3400SMatthew Dillon KKASSERT(((ap->ap_active | ap->ap_sactive) & mask) == 0); 2268d16d3400SMatthew Dillon 22694c339a5fSMatthew Dillon ap->ap_sactive |= mask; 22704c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, mask); 22714c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, mask); 22724c339a5fSMatthew Dillon } else { 22734c339a5fSMatthew Dillon /* 22744c339a5fSMatthew Dillon * The next command is a standard command and can be issued 22754c339a5fSMatthew Dillon * as long as currently active commands are not NCQ. 22764c339a5fSMatthew Dillon * 22774c339a5fSMatthew Dillon * We limit ourself to 1 command if we have a port multiplier, 22784c339a5fSMatthew Dillon * (at least without FBSS support), otherwise timeouts on 22794c339a5fSMatthew Dillon * one port can race completions on other ports (see 22804c339a5fSMatthew Dillon * ahci_ata_cmd_timeout() for more information). 22814c339a5fSMatthew Dillon * 22824c339a5fSMatthew Dillon * If not on a port multiplier generally allow up to 4 22834c339a5fSMatthew Dillon * standard commands to be enqueued. Remember that the 22844c339a5fSMatthew Dillon * command processor will still process them sequentially. 22851980eff3SMatthew Dillon */ 22861980eff3SMatthew Dillon if (ap->ap_sactive) 2287258223a3SMatthew Dillon return; 22884c339a5fSMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 22894c339a5fSMatthew Dillon limit = 1; 22904c339a5fSMatthew Dillon else if (ap->ap_sc->sc_ncmds > 4) 22914c339a5fSMatthew Dillon limit = 4; 22924c339a5fSMatthew Dillon else 22934c339a5fSMatthew Dillon limit = 2; 2294258223a3SMatthew Dillon 22954c339a5fSMatthew Dillon while (ap->ap_active_cnt < limit && ccb && 22964c339a5fSMatthew Dillon (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) { 22974c339a5fSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 2298d16d3400SMatthew Dillon KKASSERT(((ap->ap_active | ap->ap_sactive) & 2299d16d3400SMatthew Dillon (1 << ccb->ccb_slot)) == 0); 23004c339a5fSMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 2301258223a3SMatthew Dillon ap->ap_active_cnt++; 230212feb904SMatthew Dillon ap->ap_run_flags = ccb->ccb_xa.flags; 23034c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 230412feb904SMatthew Dillon ahci_start_timeout(ccb); 2305d16d3400SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 230622726f69SMatthew Dillon if ((ap->ap_run_flags & 230722726f69SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) { 230822726f69SMatthew Dillon break; 230922726f69SMatthew Dillon } 23104c339a5fSMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_pending); 231112feb904SMatthew Dillon if (ccb && (ccb->ccb_xa.flags & 231212feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) { 231312feb904SMatthew Dillon break; 231412feb904SMatthew Dillon } 23151980eff3SMatthew Dillon } 2316258223a3SMatthew Dillon } 2317258223a3SMatthew Dillon } 2318258223a3SMatthew Dillon 2319258223a3SMatthew Dillon void 2320258223a3SMatthew Dillon ahci_intr(void *arg) 2321258223a3SMatthew Dillon { 2322258223a3SMatthew Dillon struct ahci_softc *sc = arg; 2323f4553de1SMatthew Dillon struct ahci_port *ap; 232412feb904SMatthew Dillon u_int32_t is; 232512feb904SMatthew Dillon u_int32_t ack; 2326258223a3SMatthew Dillon int port; 2327258223a3SMatthew Dillon 2328f4553de1SMatthew Dillon /* 2329f4553de1SMatthew Dillon * Check if the master enable is up, and whether any interrupts are 2330f4553de1SMatthew Dillon * pending. 2331f4553de1SMatthew Dillon */ 2332f4553de1SMatthew Dillon if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0) 2333f4553de1SMatthew Dillon return; 2334258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 233512feb904SMatthew Dillon if (is == 0 || is == 0xffffffff) { 2336258223a3SMatthew Dillon return; 233712feb904SMatthew Dillon } 233812feb904SMatthew Dillon is &= sc->sc_portmask; 2339258223a3SMatthew Dillon 2340258223a3SMatthew Dillon #ifdef AHCI_COALESCE 2341258223a3SMatthew Dillon /* Check coalescing interrupt first */ 2342258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 2343258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 2344258223a3SMatthew Dillon DEVNAME(sc)); 2345258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 2346258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 2347258223a3SMatthew Dillon } 2348258223a3SMatthew Dillon #endif 2349258223a3SMatthew Dillon 2350f4553de1SMatthew Dillon /* 2351f4553de1SMatthew Dillon * Process interrupts for each port in a non-blocking fashion. 235212feb904SMatthew Dillon * 23533d102df7SMatthew Dillon * The global IS bit is supposed to be forced on if any unmasked 23543d102df7SMatthew Dillon * port interrupt is pending, even if we clear it. 23553d102df7SMatthew Dillon * 23563d102df7SMatthew Dillon * However it would appear that it is simply latched on some parts, 23573d102df7SMatthew Dillon * which means we have to clear it BEFORE processing the status bits 23583d102df7SMatthew Dillon * to avoid races. 2359f4553de1SMatthew Dillon */ 23603d102df7SMatthew Dillon ahci_write(sc, AHCI_REG_IS, is); 236112feb904SMatthew Dillon for (ack = 0; is; is &= ~(1 << port)) { 2362258223a3SMatthew Dillon port = ffs(is) - 1; 236312feb904SMatthew Dillon ack |= 1 << port; 236412feb904SMatthew Dillon 2365f4553de1SMatthew Dillon ap = sc->sc_ports[port]; 236612feb904SMatthew Dillon if (ap == NULL) 236712feb904SMatthew Dillon continue; 236812feb904SMatthew Dillon 2369f4553de1SMatthew Dillon if (ahci_os_lock_port_nb(ap) == 0) { 2370f4553de1SMatthew Dillon ahci_port_intr(ap, 0); 2371f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 2372f4553de1SMatthew Dillon } else { 2373f4553de1SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2374f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2375f4553de1SMatthew Dillon } 2376f4553de1SMatthew Dillon } 2377258223a3SMatthew Dillon } 2378258223a3SMatthew Dillon 2379f4553de1SMatthew Dillon /* 2380f4553de1SMatthew Dillon * Core called from helper thread. 2381f4553de1SMatthew Dillon */ 23823209f581SMatthew Dillon void 2383f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask) 2384f4553de1SMatthew Dillon { 2385f4553de1SMatthew Dillon /* 2386f4553de1SMatthew Dillon * Process any expired timedouts. 2387f4553de1SMatthew Dillon */ 2388f4553de1SMatthew Dillon ahci_os_lock_port(ap); 2389f4553de1SMatthew Dillon if (mask & AP_SIGF_TIMEOUT) { 2390831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 2391f4553de1SMatthew Dillon } 2392f4553de1SMatthew Dillon 2393f4553de1SMatthew Dillon /* 2394f4553de1SMatthew Dillon * Process port interrupts which require a higher level of 2395f4553de1SMatthew Dillon * intervention. 2396f4553de1SMatthew Dillon */ 2397f4553de1SMatthew Dillon if (mask & AP_SIGF_PORTINT) { 2398f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 2399f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 240012feb904SMatthew Dillon } else if (ap->ap_probe != ATA_PROBE_FAILED) { 240112feb904SMatthew Dillon ahci_port_intr(ap, 1); 240212feb904SMatthew Dillon ahci_port_interrupt_enable(ap); 2403f4553de1SMatthew Dillon } 2404d16d3400SMatthew Dillon ahci_os_unlock_port(ap); 2405f4553de1SMatthew Dillon } 2406f4553de1SMatthew Dillon 2407f4553de1SMatthew Dillon /* 2408f4553de1SMatthew Dillon * Core per-port interrupt handler. 2409f4553de1SMatthew Dillon * 2410f4553de1SMatthew Dillon * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only 2411f4553de1SMatthew Dillon * deal with normal command completions which do not require blocking. 2412f4553de1SMatthew Dillon */ 2413f4553de1SMatthew Dillon void 2414f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable) 2415258223a3SMatthew Dillon { 2416258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 24173209f581SMatthew Dillon u_int32_t is, ci_saved, ci_masked; 241822181ab7SMatthew Dillon int slot; 2419492bffafSMatthew Dillon int stopped = 0; 2420258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 24211980eff3SMatthew Dillon struct ata_port *ccb_at = NULL; 2422258223a3SMatthew Dillon volatile u_int32_t *active; 2423f4553de1SMatthew Dillon const u_int32_t blockable_mask = AHCI_PREG_IS_TFES | 2424f4553de1SMatthew Dillon AHCI_PREG_IS_IFS | 2425f4553de1SMatthew Dillon AHCI_PREG_IS_PCS | 2426f4553de1SMatthew Dillon AHCI_PREG_IS_PRCS | 2427f4553de1SMatthew Dillon AHCI_PREG_IS_HBFS | 2428f4553de1SMatthew Dillon AHCI_PREG_IS_OFS | 2429f4553de1SMatthew Dillon AHCI_PREG_IS_UFS; 2430f4553de1SMatthew Dillon 2431492bffafSMatthew Dillon enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART, 2432492bffafSMatthew Dillon NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING; 2433258223a3SMatthew Dillon 2434f4553de1SMatthew Dillon /* 2435f4553de1SMatthew Dillon * All basic command completions are always processed. 2436f4553de1SMatthew Dillon */ 243712feb904SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 2438cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_DPS) 2439cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS); 2440258223a3SMatthew Dillon 2441f4553de1SMatthew Dillon /* 2442f4553de1SMatthew Dillon * If we can't block then we can't handle these here. Disable 2443f4553de1SMatthew Dillon * the interrupts in question so we don't live-lock, the helper 2444f4553de1SMatthew Dillon * thread will re-enable them. 2445f4553de1SMatthew Dillon * 2446f4553de1SMatthew Dillon * If the port is in a completely failed state we do not want 2447dbef6246SMatthew Dillon * to drop through to failed-command-processing if blockable is 0, 2448f4553de1SMatthew Dillon * just let the thread deal with it all. 2449dbef6246SMatthew Dillon * 2450dbef6246SMatthew Dillon * Otherwise we fall through and still handle DHRS and any commands 2451dbef6246SMatthew Dillon * which completed normally. Even if we are errored we haven't 2452dbef6246SMatthew Dillon * stopped the port yet so CI/SACT are still good. 2453f4553de1SMatthew Dillon */ 2454f4553de1SMatthew Dillon if (blockable == 0) { 2455f4553de1SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 245612feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2457f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2458f4553de1SMatthew Dillon return; 2459f4553de1SMatthew Dillon } 2460f4553de1SMatthew Dillon if (is & blockable_mask) { 246112feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2462f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 246312feb904SMatthew Dillon return; 2464f4553de1SMatthew Dillon } 2465f4553de1SMatthew Dillon } 2466f4553de1SMatthew Dillon 24673209f581SMatthew Dillon /* 2468f4553de1SMatthew Dillon * Either NCQ or non-NCQ commands will be active, never both. 24693209f581SMatthew Dillon */ 2470258223a3SMatthew Dillon if (ap->ap_sactive) { 2471258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 2472258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 2473258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 2474258223a3SMatthew Dillon active = &ap->ap_sactive; 2475258223a3SMatthew Dillon } else { 2476258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 2477258223a3SMatthew Dillon active = &ap->ap_active; 2478258223a3SMatthew Dillon } 247912feb904SMatthew Dillon KKASSERT(!(ap->ap_sactive && ap->ap_active)); 2480d16d3400SMatthew Dillon KKASSERT((ci_saved & (ap->ap_sactive | ap->ap_active)) == ci_saved); 248112feb904SMatthew Dillon #if 0 248212feb904SMatthew Dillon kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n", 248312feb904SMatthew Dillon ap->ap_active, ahci_pread(ap, AHCI_PREG_CI), 248412feb904SMatthew Dillon ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT)); 248512feb904SMatthew Dillon #endif 2486258223a3SMatthew Dillon 2487492bffafSMatthew Dillon /* 2488492bffafSMatthew Dillon * Ignore AHCI_PREG_IS_PRCS when link power management is on 2489492bffafSMatthew Dillon */ 2490795adb22SMatthew Dillon if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) { 2491795adb22SMatthew Dillon is &= ~AHCI_PREG_IS_PRCS; 2492795adb22SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 2493795adb22SMatthew Dillon AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W); 2494795adb22SMatthew Dillon } 2495795adb22SMatthew Dillon 2496cf5f3a81SMatthew Dillon /* 2497f4553de1SMatthew Dillon * Command failed (blockable). 2498f4553de1SMatthew Dillon * 2499f4553de1SMatthew Dillon * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. 25001980eff3SMatthew Dillon * 25011980eff3SMatthew Dillon * This stops command processing. 2502cf5f3a81SMatthew Dillon */ 2503492bffafSMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 2504258223a3SMatthew Dillon u_int32_t tfd, serr; 2505258223a3SMatthew Dillon int err_slot; 2506258223a3SMatthew Dillon 250712feb904SMatthew Dillon process_error: 2508258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 2509258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2510258223a3SMatthew Dillon 2511cf5f3a81SMatthew Dillon /* 251212feb904SMatthew Dillon * Load the error slot and restart command processing. 251312feb904SMatthew Dillon * CLO if we need to. The error slot may not be valid. 251412feb904SMatthew Dillon * MUST BE DONE BEFORE CLEARING ST! 251512feb904SMatthew Dillon * 251612feb904SMatthew Dillon * Cycle ST. 251712feb904SMatthew Dillon * 251812feb904SMatthew Dillon * It is unclear but we may have to clear SERR to reenable 251912feb904SMatthew Dillon * error processing. 2520cf5f3a81SMatthew Dillon */ 252112feb904SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD)); 252212feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES | 252312feb904SMatthew Dillon AHCI_PREG_IS_PSS | 252412feb904SMatthew Dillon AHCI_PREG_IS_DHRS | 252512feb904SMatthew Dillon AHCI_PREG_IS_SDBS); 252612feb904SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS | 252712feb904SMatthew Dillon AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS); 252812feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 2529258223a3SMatthew Dillon ahci_port_stop(ap, 0); 253012feb904SMatthew Dillon ahci_os_hardsleep(10); 253112feb904SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 253212feb904SMatthew Dillon kprintf("%s: Issuing CLO\n", PORTNAME(ap)); 253312feb904SMatthew Dillon ahci_port_clo(ap); 253412feb904SMatthew Dillon } 2535492bffafSMatthew Dillon 2536492bffafSMatthew Dillon /* 2537492bffafSMatthew Dillon * We are now stopped and need a restart. If we have to 2538492bffafSMatthew Dillon * process a NCQ error we will temporarily start and then 2539492bffafSMatthew Dillon * stop the port again, so this condition holds. 2540492bffafSMatthew Dillon */ 2541492bffafSMatthew Dillon stopped = 1; 254222181ab7SMatthew Dillon need = NEED_RESTART; 2543258223a3SMatthew Dillon 254450a3ecb6SMatthew Dillon /* 254550a3ecb6SMatthew Dillon * ATAPI errors are fairly common from probing, just 254650a3ecb6SMatthew Dillon * report disk errors or if bootverbose is on. 254750a3ecb6SMatthew Dillon */ 254850a3ecb6SMatthew Dillon if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) { 254912feb904SMatthew Dillon kprintf("%s: TFES slot %d ci_saved = %08x\n", 255012feb904SMatthew Dillon PORTNAME(ap), err_slot, ci_saved); 255150a3ecb6SMatthew Dillon } 2552258223a3SMatthew Dillon 25531980eff3SMatthew Dillon /* 255412feb904SMatthew Dillon * If we got an error on an error CCB just complete it 255512feb904SMatthew Dillon * with an error. ci_saved has the mask to restart 255612feb904SMatthew Dillon * (the err_ccb will be removed from it by finish_error). 25571980eff3SMatthew Dillon */ 255812feb904SMatthew Dillon if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) { 255912feb904SMatthew Dillon err_slot = ap->ap_err_ccb->ccb_slot; 256012feb904SMatthew Dillon goto finish_error; 2561258223a3SMatthew Dillon } 2562258223a3SMatthew Dillon 25631980eff3SMatthew Dillon /* 256412feb904SMatthew Dillon * If NCQ commands were active get the error slot from 256512feb904SMatthew Dillon * the log page. NCQ is not supported for PM's so this 256612feb904SMatthew Dillon * is a direct-attached target. 25671980eff3SMatthew Dillon * 256812feb904SMatthew Dillon * Otherwise if no commands were active we have a problem. 256912feb904SMatthew Dillon * 257012feb904SMatthew Dillon * Otherwise if the error slot is bad we have a problem. 257112feb904SMatthew Dillon * 257212feb904SMatthew Dillon * Otherwise process the error for the slot. 25731980eff3SMatthew Dillon */ 257412feb904SMatthew Dillon if (ap->ap_sactive) { 2575492bffafSMatthew Dillon ahci_port_start(ap); 257612feb904SMatthew Dillon err_slot = ahci_port_read_ncq_error(ap, 0); 2577492bffafSMatthew Dillon ahci_port_stop(ap, 0); 257812feb904SMatthew Dillon } else if (ap->ap_active == 0) { 257912feb904SMatthew Dillon kprintf("%s: TFES with no commands pending\n", 258012feb904SMatthew Dillon PORTNAME(ap)); 258112feb904SMatthew Dillon err_slot = -1; 258212feb904SMatthew Dillon } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) { 258312feb904SMatthew Dillon kprintf("%s: bad error slot %d\n", 2584258223a3SMatthew Dillon PORTNAME(ap), err_slot); 258512feb904SMatthew Dillon err_slot = -1; 2586258223a3SMatthew Dillon } else { 258712feb904SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 258812feb904SMatthew Dillon 25894c339a5fSMatthew Dillon /* 259012feb904SMatthew Dillon * Validate the errored ccb. Note that ccb_at can 259112feb904SMatthew Dillon * be NULL for direct-attached ccb's. 259212feb904SMatthew Dillon * 259312feb904SMatthew Dillon * Copy received taskfile data from the RFIS. 25944c339a5fSMatthew Dillon */ 259512feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) { 259612feb904SMatthew Dillon ccb_at = ccb->ccb_xa.at; 259712feb904SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 259812feb904SMatthew Dillon sizeof(struct ata_fis_d2h)); 259950a3ecb6SMatthew Dillon if (bootverbose) { 260050a3ecb6SMatthew Dillon kprintf("%s: Copying rfis slot %d\n", 260112feb904SMatthew Dillon ATANAME(ap, ccb_at), err_slot); 260250a3ecb6SMatthew Dillon } 260312feb904SMatthew Dillon } else { 260412feb904SMatthew Dillon kprintf("%s: Cannot copy rfis, CCB slot " 260512feb904SMatthew Dillon "%d is not on-chip (state=%d)\n", 260612feb904SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 260712feb904SMatthew Dillon err_slot, ccb->ccb_xa.state); 260812feb904SMatthew Dillon err_slot = -1; 260912feb904SMatthew Dillon } 2610258223a3SMatthew Dillon } 2611258223a3SMatthew Dillon 2612258223a3SMatthew Dillon /* 261312feb904SMatthew Dillon * If we could not determine the errored slot then 261412feb904SMatthew Dillon * reset the port. 2615258223a3SMatthew Dillon */ 261612feb904SMatthew Dillon if (err_slot < 0) { 261712feb904SMatthew Dillon kprintf("%s: TFES: Unable to determine errored slot\n", 261812feb904SMatthew Dillon PORTNAME(ap)); 26191980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 26201980eff3SMatthew Dillon goto fatal; 2621258223a3SMatthew Dillon goto failall; 2622258223a3SMatthew Dillon } 2623258223a3SMatthew Dillon 262412feb904SMatthew Dillon /* 262512feb904SMatthew Dillon * Finish error on slot. We will restart ci_saved 262612feb904SMatthew Dillon * commands except the errored slot which we generate 262712feb904SMatthew Dillon * a failure for. 262812feb904SMatthew Dillon */ 262912feb904SMatthew Dillon finish_error: 263012feb904SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2631258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 2632258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2633258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 26341980eff3SMatthew Dillon } else if (is & AHCI_PREG_IS_DHRS) { 26351980eff3SMatthew Dillon /* 2636f4553de1SMatthew Dillon * Command posted D2H register FIS to the rfis (non-blocking). 2637f4553de1SMatthew Dillon * 263812feb904SMatthew Dillon * A normal completion with an error may set DHRS instead 263912feb904SMatthew Dillon * of TFES. The CCS bits are only valid if ERR was set. 264012feb904SMatthew Dillon * If ERR is set command processing was probably stopped. 26418bf6a3ffSMatthew Dillon * 264212feb904SMatthew Dillon * If ERR was not set we can only copy-back data for 264312feb904SMatthew Dillon * exclusive-mode commands because otherwise we won't know 264412feb904SMatthew Dillon * which tag the rfis belonged to. 264512feb904SMatthew Dillon * 264612feb904SMatthew Dillon * err_slot must be read from the CCS before any other port 264712feb904SMatthew Dillon * action, such as stopping the port. 264812feb904SMatthew Dillon * 264912feb904SMatthew Dillon * WARNING! This is not well documented in the AHCI spec. 265012feb904SMatthew Dillon * It can be found in the state machine tables 265112feb904SMatthew Dillon * but not in the explanations. 26521980eff3SMatthew Dillon */ 265312feb904SMatthew Dillon u_int32_t tfd; 265412feb904SMatthew Dillon u_int32_t cmd; 26551980eff3SMatthew Dillon int err_slot; 26561980eff3SMatthew Dillon 265712feb904SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 265812feb904SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 265912feb904SMatthew Dillon 26603d102df7SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS); 266112feb904SMatthew Dillon if ((tfd & AHCI_PREG_TFD_STS_ERR) && 266212feb904SMatthew Dillon (cmd & AHCI_PREG_CMD_CR) == 0) { 26631980eff3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 26641980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 26651980eff3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 266612feb904SMatthew Dillon kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n", 266712feb904SMatthew Dillon PORTNAME(ap), 266812feb904SMatthew Dillon tfd, AHCI_PFMT_TFD_STS, 266912feb904SMatthew Dillon err_slot, ccb->ccb_xa.fis->command); 267012feb904SMatthew Dillon goto process_error; 2671258223a3SMatthew Dillon } 267212feb904SMatthew Dillon /* 267312feb904SMatthew Dillon * NO ELSE... copy back is in the normal command completion 267412feb904SMatthew Dillon * code and only if no error occured and ATA_F_AUTOSENSE 267512feb904SMatthew Dillon * was set. 267612feb904SMatthew Dillon */ 26771980eff3SMatthew Dillon } 26781980eff3SMatthew Dillon 26791980eff3SMatthew Dillon /* 2680f4553de1SMatthew Dillon * Device notification to us (non-blocking) 26811980eff3SMatthew Dillon * 268212feb904SMatthew Dillon * NOTE! On some parts notification bits can cause an IPMS 268312feb904SMatthew Dillon * interrupt instead of a SDBS interrupt. 2684cec07d75SMatthew Dillon * 268512feb904SMatthew Dillon * NOTE! On some parts (e.g. VBOX, probably intel ICHx), 268612feb904SMatthew Dillon * SDBS notifies us of the completion of a NCQ command 268712feb904SMatthew Dillon * and DBS does not. 26881980eff3SMatthew Dillon */ 268912feb904SMatthew Dillon if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) { 26901980eff3SMatthew Dillon u_int32_t data; 26911980eff3SMatthew Dillon 269212feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 269312feb904SMatthew Dillon AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS); 269412feb904SMatthew Dillon if (sc->sc_cap & AHCI_REG_CAP_SSNTF) { 26951980eff3SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SNTF); 2696cec07d75SMatthew Dillon if (data) { 269712feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 269812feb904SMatthew Dillon AHCI_PREG_IS_SDBS); 269912feb904SMatthew Dillon kprintf("%s: NOTIFY %08x\n", 270012feb904SMatthew Dillon PORTNAME(ap), data); 270112feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 270212feb904SMatthew Dillon AHCI_PREG_SERR_DIAG_N); 27033209f581SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, data); 27043209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 27051980eff3SMatthew Dillon } 27061980eff3SMatthew Dillon } 270712feb904SMatthew Dillon is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS); 270812feb904SMatthew Dillon } 27093209f581SMatthew Dillon 27103209f581SMatthew Dillon /* 2711492bffafSMatthew Dillon * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set. 2712f4553de1SMatthew Dillon * 27133209f581SMatthew Dillon * Spurious IFS errors can occur while we are doing a reset 2714492bffafSMatthew Dillon * sequence through a PM, probably due to an unexpected FIS 2715492bffafSMatthew Dillon * being received during the PM target reset sequence. Chipsets 2716492bffafSMatthew Dillon * are supposed to mask these events but some do not. 2717492bffafSMatthew Dillon * 2718492bffafSMatthew Dillon * Try to recover from the condition. 27193209f581SMatthew Dillon */ 27203209f581SMatthew Dillon if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) { 27211980eff3SMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 27223209f581SMatthew Dillon if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) { 2723492bffafSMatthew Dillon kprintf("%s: IFS during PM probe (ignored) " 2724492bffafSMatthew Dillon "IS=%b, SERR=%b\n", 27251980eff3SMatthew Dillon PORTNAME(ap), 27261980eff3SMatthew Dillon is, AHCI_PFMT_IS, 27271980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 27283209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_IGNORED; 27293209f581SMatthew Dillon } 2730492bffafSMatthew Dillon 2731492bffafSMatthew Dillon /* 2732492bffafSMatthew Dillon * Try to clear the error condition. The IFS error killed 2733492bffafSMatthew Dillon * the port so stop it so we can restart it. 2734492bffafSMatthew Dillon */ 27351980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 27363d102df7SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 27371980eff3SMatthew Dillon is &= ~AHCI_PREG_IS_IFS; 2738492bffafSMatthew Dillon need = NEED_RESTART; 273912feb904SMatthew Dillon goto failall; 27401980eff3SMatthew Dillon } 2741258223a3SMatthew Dillon 2742258223a3SMatthew Dillon /* 2743f4553de1SMatthew Dillon * Port change (hot-plug) (blockable). 2744258223a3SMatthew Dillon * 2745492bffafSMatthew Dillon * A PRCS interrupt can occur: 2746492bffafSMatthew Dillon * (1) On hot-unplug / normal-unplug (phy lost) 2747492bffafSMatthew Dillon * (2) Sometimes on hot-plug too. 2748258223a3SMatthew Dillon * 2749492bffafSMatthew Dillon * A PCS interrupt can occur in a number of situations: 2750492bffafSMatthew Dillon * (1) On hot-plug once communication is established 2751492bffafSMatthew Dillon * (2) On hot-unplug sometimes. 2752492bffafSMatthew Dillon * (3) For chipsets with badly written firmware it can occur 2753492bffafSMatthew Dillon * during INIT/RESET sequences due to the device reset. 2754492bffafSMatthew Dillon * (4) For chipsets with badly written firmware it can occur 2755492bffafSMatthew Dillon * when it thinks an unsolicited COMRESET is received 2756492bffafSMatthew Dillon * during a INIT/RESET sequence, even though we actually 2757492bffafSMatthew Dillon * did request it. 2758258223a3SMatthew Dillon * 275922181ab7SMatthew Dillon * XXX We can then check the CPS (Cold Presence State) bit, if 276022181ab7SMatthew Dillon * supported, to determine if a device is plugged in or not and do 276122181ab7SMatthew Dillon * the right thing. 276222181ab7SMatthew Dillon * 2763492bffafSMatthew Dillon * PCS interrupts are cleared by clearing DIAG_X. If this occurs 2764492bffafSMatthew Dillon * command processing is automatically stopped (CR goes inactive) 2765492bffafSMatthew Dillon * and the port must be stopped and restarted. 2766492bffafSMatthew Dillon * 2767492bffafSMatthew Dillon * WARNING: AMD parts (e.g. 880G chipset, probably others) can 2768492bffafSMatthew Dillon * generate PCS on initialization even when device is 2769492bffafSMatthew Dillon * already connected up. It is unclear why this happens. 2770492bffafSMatthew Dillon * Depending on the state of the device detect this can 2771492bffafSMatthew Dillon * cause us to go into harsh reinit or hot-plug insertion 2772492bffafSMatthew Dillon * mode. 2773492bffafSMatthew Dillon * 2774492bffafSMatthew Dillon * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET 2775492bffafSMatthew Dillon * continues to flow in from the device), we must clear the 2776492bffafSMatthew Dillon * interrupt in all cases and enforce a delay to prevent 2777492bffafSMatthew Dillon * a livelock and give the port time to settle down. 2778492bffafSMatthew Dillon * Only print something if we aren't in INIT/HARD-RESET. 2779258223a3SMatthew Dillon */ 2780258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 27813d102df7SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 27823d102df7SMatthew Dillon is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)); 2783492bffafSMatthew Dillon /* 2784492bffafSMatthew Dillon * Try to clear the error. Because of the repetitiveness 2785492bffafSMatthew Dillon * of this interrupt avoid any harsh action if the port is 2786492bffafSMatthew Dillon * already in the init or hard-reset probe state. 2787492bffafSMatthew Dillon */ 2788492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 2789492bffafSMatthew Dillon /* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */ 2790492bffafSMatthew Dillon 2791493d3201SMatthew Dillon /* 2792493d3201SMatthew Dillon * Ignore PCS/PRCS errors during probes (but still clear the 2793493d3201SMatthew Dillon * interrupt to avoid a livelock). The AMD 880/890/SB850 2794493d3201SMatthew Dillon * chipsets do not mask PCS/PRCS internally during reset 2795493d3201SMatthew Dillon * sequences. 2796493d3201SMatthew Dillon */ 27975502cf24SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 2798493d3201SMatthew Dillon goto skip_pcs; 2799493d3201SMatthew Dillon 2800492bffafSMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT || 2801492bffafSMatthew Dillon ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) { 2802cec07d75SMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2803492bffafSMatthew Dillon need = NEED_NOTHING; 2804492bffafSMatthew Dillon ahci_os_sleep(1000); 2805492bffafSMatthew Dillon goto failall; 2806492bffafSMatthew Dillon } 2807492bffafSMatthew Dillon kprintf("%s: Transient Errors: %b (%d)\n", 2808492bffafSMatthew Dillon PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe); 2809492bffafSMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2810492bffafSMatthew Dillon ahci_os_sleep(200); 2811492bffafSMatthew Dillon 2812492bffafSMatthew Dillon /* 2813492bffafSMatthew Dillon * Stop the port and figure out what to do next. 2814492bffafSMatthew Dillon */ 281522181ab7SMatthew Dillon ahci_port_stop(ap, 0); 2816492bffafSMatthew Dillon stopped = 1; 28170be9576aSMatthew Dillon 2818258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 2819258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 2820492bffafSMatthew Dillon /* 2821492bffafSMatthew Dillon * Device detect 2822492bffafSMatthew Dillon */ 282312feb904SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 282422181ab7SMatthew Dillon need = NEED_HOTPLUG_INSERT; 282522181ab7SMatthew Dillon goto fatal; 2826258223a3SMatthew Dillon } 282722181ab7SMatthew Dillon need = NEED_RESTART; 2828258223a3SMatthew Dillon break; 2829492bffafSMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 2830492bffafSMatthew Dillon /* 2831492bffafSMatthew Dillon * Device not communicating. AMD parts seem to 2832492bffafSMatthew Dillon * like to throw this error on initialization 2833492bffafSMatthew Dillon * for no reason that I can fathom. 2834492bffafSMatthew Dillon */ 2835492bffafSMatthew Dillon kprintf("%s: Device present but not communicating, " 2836492bffafSMatthew Dillon "attempting port restart\n", 2837492bffafSMatthew Dillon PORTNAME(ap)); 2838492bffafSMatthew Dillon need = NEED_REINIT; 2839492bffafSMatthew Dillon goto fatal; 2840258223a3SMatthew Dillon default: 28410be9576aSMatthew Dillon if (ap->ap_probe != ATA_PROBE_FAILED) { 284222181ab7SMatthew Dillon need = NEED_HOTPLUG_REMOVE; 284322181ab7SMatthew Dillon goto fatal; 2844258223a3SMatthew Dillon } 284522181ab7SMatthew Dillon need = NEED_RESTART; 2846258223a3SMatthew Dillon break; 2847258223a3SMatthew Dillon } 2848493d3201SMatthew Dillon skip_pcs: 2849493d3201SMatthew Dillon ; 2850258223a3SMatthew Dillon } 2851258223a3SMatthew Dillon 285222181ab7SMatthew Dillon /* 2853f4553de1SMatthew Dillon * Check for remaining errors - they are fatal. (blockable) 285422181ab7SMatthew Dillon */ 2855258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 2856258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 2857cec07d75SMatthew Dillon u_int32_t serr; 2858cec07d75SMatthew Dillon 2859cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2860cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2861cec07d75SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2862cec07d75SMatthew Dillon AHCI_PREG_IS_UFS)); 2863cec07d75SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2864831bc9e3SMatthew Dillon kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), " 28654444122dSMatthew Dillon "disabling port.\n", 28664444122dSMatthew Dillon PORTNAME(ap), 28674444122dSMatthew Dillon is, AHCI_PFMT_IS, 28681980eff3SMatthew Dillon serr, AHCI_PFMT_SERR 28694444122dSMatthew Dillon ); 2870831bc9e3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2871831bc9e3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2872831bc9e3SMatthew Dillon AHCI_PREG_IS_UFS); 2873492bffafSMatthew Dillon 2874492bffafSMatthew Dillon /* 2875492bffafSMatthew Dillon * Fail all commands but then what? For now try to 2876492bffafSMatthew Dillon * reinitialize the port. 2877492bffafSMatthew Dillon */ 2878492bffafSMatthew Dillon need = NEED_REINIT; 2879258223a3SMatthew Dillon goto fatal; 2880258223a3SMatthew Dillon } 2881258223a3SMatthew Dillon 288222181ab7SMatthew Dillon /* 288322181ab7SMatthew Dillon * Fail all outstanding commands if we know the port won't recover. 28841980eff3SMatthew Dillon * 28851980eff3SMatthew Dillon * We may have a ccb_at if the failed command is known and was 28861980eff3SMatthew Dillon * being sent to a device over a port multiplier (PM). In this 28871980eff3SMatthew Dillon * case if the port itself has not completely failed we fail just 28881980eff3SMatthew Dillon * the commands related to that target. 288912feb904SMatthew Dillon * 289012feb904SMatthew Dillon * ci_saved contains the mask of active commands as of when the 289112feb904SMatthew Dillon * error occured, prior to any port stops. 289222181ab7SMatthew Dillon */ 2893258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2894258223a3SMatthew Dillon fatal: 2895258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 289612feb904SMatthew Dillon failall: 2897492bffafSMatthew Dillon ahci_port_stop(ap, 0); 2898492bffafSMatthew Dillon stopped = 1; 2899258223a3SMatthew Dillon 29001980eff3SMatthew Dillon /* 2901492bffafSMatthew Dillon * Error all the active slots not already errored. 29021980eff3SMatthew Dillon */ 290312feb904SMatthew Dillon ci_masked = ci_saved & *active & ~ap->ap_expired; 2904492bffafSMatthew Dillon if (ci_masked) { 2905492bffafSMatthew Dillon kprintf("%s: Failing all commands: %08x\n", 2906492bffafSMatthew Dillon PORTNAME(ap), ci_masked); 2907492bffafSMatthew Dillon } 2908492bffafSMatthew Dillon 2909258223a3SMatthew Dillon while (ci_masked) { 2910258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2911258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 291212feb904SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 291312feb904SMatthew Dillon ap->ap_expired |= 1 << slot; 291412feb904SMatthew Dillon ci_saved &= ~(1 << slot); 291512feb904SMatthew Dillon ci_masked &= ~(1 << slot); 29161980eff3SMatthew Dillon } 2917258223a3SMatthew Dillon 291812feb904SMatthew Dillon /* 291912feb904SMatthew Dillon * Clear bits in ci_saved (cause completions to be run) 292012feb904SMatthew Dillon * for all slots which are not active. 292112feb904SMatthew Dillon */ 2922258223a3SMatthew Dillon ci_saved &= ~*active; 2923258223a3SMatthew Dillon 2924258223a3SMatthew Dillon /* 2925258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 2926258223a3SMatthew Dillon * 2927258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 2928258223a3SMatthew Dillon * a livelock. 2929258223a3SMatthew Dillon */ 2930258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 293122181ab7SMatthew Dillon if (need == NEED_RESTART) 293222181ab7SMatthew Dillon need = NEED_NOTHING; 2933258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2934258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2935258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2936258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 2937258223a3SMatthew Dillon } 2938258223a3SMatthew Dillon } 2939258223a3SMatthew Dillon 2940258223a3SMatthew Dillon /* 2941492bffafSMatthew Dillon * If we are stopped the AHCI chipset is supposed to have cleared 2942492bffafSMatthew Dillon * CI and SACT. Did it? If it didn't we try very hard to clear 2943492bffafSMatthew Dillon * the fields otherwise we may end up completing CCBs which are 2944492bffafSMatthew Dillon * actually still active. 2945492bffafSMatthew Dillon * 2946492bffafSMatthew Dillon * IFS errors on (at least) AMD chipsets create this confusion. 2947492bffafSMatthew Dillon */ 2948492bffafSMatthew Dillon if (stopped) { 2949492bffafSMatthew Dillon u_int32_t mask; 2950492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 2951492bffafSMatthew Dillon kprintf("%s: chipset failed to clear " 2952492bffafSMatthew Dillon "active cmds %08x\n", 2953492bffafSMatthew Dillon PORTNAME(ap), mask); 2954492bffafSMatthew Dillon ahci_port_start(ap); 2955492bffafSMatthew Dillon ahci_port_stop(ap, 0); 2956492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 2957492bffafSMatthew Dillon kprintf("%s: unable to prod the chip into " 2958492bffafSMatthew Dillon "clearing active cmds %08x\n", 2959492bffafSMatthew Dillon PORTNAME(ap), mask); 2960492bffafSMatthew Dillon /* what do we do now? */ 2961492bffafSMatthew Dillon } 2962492bffafSMatthew Dillon } 2963492bffafSMatthew Dillon } 2964492bffafSMatthew Dillon 2965492bffafSMatthew Dillon /* 2966f4553de1SMatthew Dillon * CCB completion (non blocking). 2967f4553de1SMatthew Dillon * 2968258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 2969258223a3SMatthew Dillon * changed to zero some time after we activated it. 2970258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 2971cf5f3a81SMatthew Dillon * 2972cf5f3a81SMatthew Dillon * Any active bits not saved are completed within the restrictions 2973cf5f3a81SMatthew Dillon * imposed by the caller. 2974258223a3SMatthew Dillon */ 29753209f581SMatthew Dillon ci_masked = ~ci_saved & *active; 2976258223a3SMatthew Dillon while (ci_masked) { 2977258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2978258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2979258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2980258223a3SMatthew Dillon 2981258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 2982258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 2983258223a3SMatthew Dillon " (error)" : ""); 2984258223a3SMatthew Dillon 2985258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2986258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2987258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2988258223a3SMatthew Dillon 2989258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2990258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2991258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2992258223a3SMatthew Dillon 2993258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2994258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2995258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 2996258223a3SMatthew Dillon 2997258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 29981980eff3SMatthew Dillon if (active == &ap->ap_active) { 29991980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 30001980eff3SMatthew Dillon --ap->ap_active_cnt; 30011980eff3SMatthew Dillon } 30024c339a5fSMatthew Dillon 30034c339a5fSMatthew Dillon /* 30044c339a5fSMatthew Dillon * Complete the ccb. If the ccb was marked expired it 30054c339a5fSMatthew Dillon * was probably already removed from the command processor, 30064c339a5fSMatthew Dillon * so don't take the clear ci_saved bit as meaning the 30074c339a5fSMatthew Dillon * command actually succeeded, it didn't. 30084c339a5fSMatthew Dillon */ 30094c339a5fSMatthew Dillon if (ap->ap_expired & (1 << ccb->ccb_slot)) { 301076497a9cSMatthew Dillon ap->ap_expired &= ~(1 << ccb->ccb_slot); 30114c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3012258223a3SMatthew Dillon ccb->ccb_done(ccb); 30134c339a5fSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 30144c339a5fSMatthew Dillon } else { 301512feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) { 30164c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 301712feb904SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) { 301812feb904SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, 301912feb904SMatthew Dillon ap->ap_rfis->rfis, 302012feb904SMatthew Dillon sizeof(struct ata_fis_d2h)); 302112feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_TIMEOUT) 302212feb904SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 302312feb904SMatthew Dillon } 302412feb904SMatthew Dillon } 30254c339a5fSMatthew Dillon ccb->ccb_done(ccb); 30264c339a5fSMatthew Dillon } 3027258223a3SMatthew Dillon } 3028258223a3SMatthew Dillon 3029f4553de1SMatthew Dillon /* 3030f4553de1SMatthew Dillon * Cleanup. Will not be set if non-blocking. 3031f4553de1SMatthew Dillon */ 303222181ab7SMatthew Dillon switch(need) { 3033f3de36f7SMatthew Dillon case NEED_NOTHING: 3034f3de36f7SMatthew Dillon /* 3035f3de36f7SMatthew Dillon * If operating normally and not stopped the interrupt was 3036f3de36f7SMatthew Dillon * probably just a normal completion and we may be able to 3037f3de36f7SMatthew Dillon * issue more commands. 3038f3de36f7SMatthew Dillon */ 3039f3de36f7SMatthew Dillon if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR) 3040f3de36f7SMatthew Dillon ahci_issue_pending_commands(ap, NULL); 3041f3de36f7SMatthew Dillon break; 304222181ab7SMatthew Dillon case NEED_RESTART: 304322181ab7SMatthew Dillon /* 304422181ab7SMatthew Dillon * A recoverable error occured and we can restart outstanding 304522181ab7SMatthew Dillon * commands on the port. 304622181ab7SMatthew Dillon */ 304712feb904SMatthew Dillon ci_saved &= ~ap->ap_expired; 3048258223a3SMatthew Dillon if (ci_saved) { 304912feb904SMatthew Dillon kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved); 30504c339a5fSMatthew Dillon ahci_issue_saved_commands(ap, ci_saved); 3051258223a3SMatthew Dillon } 3052492bffafSMatthew Dillon 3053492bffafSMatthew Dillon /* 3054492bffafSMatthew Dillon * Potentially issue new commands if not in a failed 3055492bffafSMatthew Dillon * state. 3056492bffafSMatthew Dillon */ 3057492bffafSMatthew Dillon if (ap->ap_state != AP_S_FATAL_ERROR) { 3058492bffafSMatthew Dillon ahci_port_start(ap); 3059492bffafSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 3060492bffafSMatthew Dillon } 3061492bffafSMatthew Dillon break; 3062492bffafSMatthew Dillon case NEED_REINIT: 3063492bffafSMatthew Dillon /* 3064492bffafSMatthew Dillon * Something horrible happened to the port and we 3065492bffafSMatthew Dillon * need to reinitialize it. 3066492bffafSMatthew Dillon */ 3067492bffafSMatthew Dillon kprintf("%s: REINIT - Attempting to reinitialize the port " 3068492bffafSMatthew Dillon "after it had a horrible accident\n", 3069492bffafSMatthew Dillon PORTNAME(ap)); 3070492bffafSMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 3071492bffafSMatthew Dillon ap->ap_flags |= AP_F_HARSH_REINIT; 3072492bffafSMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 3073492bffafSMatthew Dillon ahci_cam_changed(ap, NULL, -1); 307422181ab7SMatthew Dillon break; 307522181ab7SMatthew Dillon case NEED_HOTPLUG_INSERT: 307622181ab7SMatthew Dillon /* 3077cf5f3a81SMatthew Dillon * A hot-plug insertion event has occured and all 3078cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 30791980eff3SMatthew Dillon * 30801980eff3SMatthew Dillon * Don't recurse if this occurs while we are 30811980eff3SMatthew Dillon * resetting the port. 308222181ab7SMatthew Dillon */ 30831980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 308422181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 308522181ab7SMatthew Dillon PORTNAME(ap)); 30863209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 30873209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 30881980eff3SMatthew Dillon } 308922181ab7SMatthew Dillon break; 309022181ab7SMatthew Dillon case NEED_HOTPLUG_REMOVE: 3091cf5f3a81SMatthew Dillon /* 3092cf5f3a81SMatthew Dillon * A hot-plug removal event has occured and all 3093cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 30941980eff3SMatthew Dillon * 30951980eff3SMatthew Dillon * Don't recurse if this occurs while we are 30961980eff3SMatthew Dillon * resetting the port. 3097cf5f3a81SMatthew Dillon */ 30981980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 309922181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 310022181ab7SMatthew Dillon PORTNAME(ap)); 3101cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 31023209f581SMatthew Dillon /* ap_probe set to failed */ 31033209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 31041980eff3SMatthew Dillon } 310522181ab7SMatthew Dillon break; 310622181ab7SMatthew Dillon default: 310722181ab7SMatthew Dillon break; 3108258223a3SMatthew Dillon } 3109258223a3SMatthew Dillon } 3110258223a3SMatthew Dillon 3111258223a3SMatthew Dillon struct ahci_ccb * 3112258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 3113258223a3SMatthew Dillon { 3114258223a3SMatthew Dillon struct ahci_ccb *ccb; 3115258223a3SMatthew Dillon 3116258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 3117258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 3118258223a3SMatthew Dillon if (ccb != NULL) { 3119d16d3400SMatthew Dillon KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0); 3120258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 3121258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 3122258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 3123492bffafSMatthew Dillon ccb->ccb_xa.flags = 0; 31241980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 3125258223a3SMatthew Dillon } 3126258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 3127258223a3SMatthew Dillon 3128258223a3SMatthew Dillon return (ccb); 3129258223a3SMatthew Dillon } 3130258223a3SMatthew Dillon 3131258223a3SMatthew Dillon void 3132258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 3133258223a3SMatthew Dillon { 3134258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3135258223a3SMatthew Dillon 3136d16d3400SMatthew Dillon KKASSERT(ccb->ccb_xa.state != ATA_S_PUT); 3137d16d3400SMatthew Dillon KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0); 3138258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 3139dcdc0770SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 3140bb79834dSMatthew Dillon ++ccb->ccb_xa.serial; 3141258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 3142258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 3143258223a3SMatthew Dillon } 3144258223a3SMatthew Dillon 3145258223a3SMatthew Dillon struct ahci_ccb * 3146258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 3147258223a3SMatthew Dillon { 3148258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 3149258223a3SMatthew Dillon u_int32_t sact; 3150b012a2caSMatthew Dillon u_int32_t ci; 3151258223a3SMatthew Dillon 3152258223a3SMatthew Dillon /* No commands may be active on the chip. */ 3153b012a2caSMatthew Dillon 3154b012a2caSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) { 3155258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 3156192ee1d0SMatthew Dillon if (sact != 0) { 3157192ee1d0SMatthew Dillon kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n", 3158192ee1d0SMatthew Dillon PORTNAME(ap), sact); 3159192ee1d0SMatthew Dillon } 3160b012a2caSMatthew Dillon } 3161b012a2caSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 3162b012a2caSMatthew Dillon if (ci) { 3163b012a2caSMatthew Dillon kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n", 3164b012a2caSMatthew Dillon ap->ap_name, ci); 3165b012a2caSMatthew Dillon } 3166b012a2caSMatthew Dillon KKASSERT(ci == 0); 3167baef7501SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0); 3168baef7501SMatthew Dillon ap->ap_flags |= AP_F_ERR_CCB_RESERVED; 3169258223a3SMatthew Dillon 3170258223a3SMatthew Dillon /* Save outstanding command state. */ 3171258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 3172258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 3173258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 3174258223a3SMatthew Dillon 3175258223a3SMatthew Dillon /* 3176258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 3177258223a3SMatthew Dillon * run prematurely. 3178258223a3SMatthew Dillon */ 3179258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 3180258223a3SMatthew Dillon 3181258223a3SMatthew Dillon /* 3182258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 3183258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 3184258223a3SMatthew Dillon */ 31851067474aSMatthew Dillon err_ccb = ap->ap_err_ccb; 3186258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 3187258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 3188258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 3189258223a3SMatthew Dillon 3190258223a3SMatthew Dillon return err_ccb; 3191258223a3SMatthew Dillon } 3192258223a3SMatthew Dillon 3193258223a3SMatthew Dillon void 3194258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 3195258223a3SMatthew Dillon { 3196258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3197258223a3SMatthew Dillon u_int32_t sact; 31985f8c1efdSMatthew Dillon u_int32_t ci; 3199258223a3SMatthew Dillon 3200baef7501SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0); 3201baef7501SMatthew Dillon 32025f8c1efdSMatthew Dillon /* 32035f8c1efdSMatthew Dillon * No commands may be active on the chip 32045f8c1efdSMatthew Dillon */ 3205b012a2caSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) { 3206258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 32075f8c1efdSMatthew Dillon if (sact) { 3208ed20d0e3SSascha Wildner panic("ahci_port_err_ccb(%d) but SACT %08x != 0", 32095f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 3210258223a3SMatthew Dillon } 3211b012a2caSMatthew Dillon } 32125f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 32135f8c1efdSMatthew Dillon if (ci) { 3214cf5f3a81SMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0 " 3215cf5f3a81SMatthew Dillon "(act=%08x sact=%08x)\n", 3216cf5f3a81SMatthew Dillon ccb->ccb_slot, ci, 3217cf5f3a81SMatthew Dillon ap->ap_active, ap->ap_sactive); 32185f8c1efdSMatthew Dillon } 3219258223a3SMatthew Dillon 32201067474aSMatthew Dillon KKASSERT(ccb == ap->ap_err_ccb); 3221258223a3SMatthew Dillon 3222258223a3SMatthew Dillon /* Restore outstanding command state */ 3223258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 3224258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 3225258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 3226258223a3SMatthew Dillon 3227baef7501SMatthew Dillon ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED; 3228258223a3SMatthew Dillon } 3229258223a3SMatthew Dillon 32301980eff3SMatthew Dillon /* 32311980eff3SMatthew Dillon * Read log page to get NCQ error. 32321980eff3SMatthew Dillon * 32331980eff3SMatthew Dillon * NOTE: NCQ not currently supported on port multipliers. XXX 32341980eff3SMatthew Dillon */ 3235258223a3SMatthew Dillon int 323612feb904SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int target) 3237258223a3SMatthew Dillon { 323812feb904SMatthew Dillon struct ata_log_page_10h *log; 3239258223a3SMatthew Dillon struct ahci_ccb *ccb; 3240e1014452SMatthew Dillon struct ahci_ccb *ccb2; 3241258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 3242258223a3SMatthew Dillon struct ata_fis_h2d *fis; 324312feb904SMatthew Dillon int err_slot; 3244258223a3SMatthew Dillon 324512feb904SMatthew Dillon if (bootverbose) { 324612feb904SMatthew Dillon kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap), 324712feb904SMatthew Dillon target); 324812feb904SMatthew Dillon } 3249258223a3SMatthew Dillon 325012feb904SMatthew Dillon /* 325112feb904SMatthew Dillon * Prep error CCB for READ LOG EXT, page 10h, 1 sector. 325212feb904SMatthew Dillon * 325312feb904SMatthew Dillon * Getting err_ccb clears active/sactive/active_cnt, putting 325412feb904SMatthew Dillon * it back restores the fields. 325512feb904SMatthew Dillon */ 3256258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 325712feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL; 3258258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 3259258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 326012feb904SMatthew Dillon ccb->ccb_xa.complete = ahci_dummy_done; 3261b012a2caSMatthew Dillon ccb->ccb_xa.at = ap->ap_ata[target]; 3262258223a3SMatthew Dillon 3263258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 326412feb904SMatthew Dillon bzero(fis, sizeof(*fis)); 3265258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 326612feb904SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD | target; 3267258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 3268258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 3269258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 3270258223a3SMatthew Dillon fis->sector_count_exp = 0; 3271258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 3272258223a3SMatthew Dillon fis->lba_mid_exp = 0; 3273258223a3SMatthew Dillon fis->device = 0; 3274258223a3SMatthew Dillon 327512feb904SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 3276258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 3277258223a3SMatthew Dillon 3278258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 327912feb904SMatthew Dillon err_slot = -1; 3280258223a3SMatthew Dillon goto err; 3281258223a3SMatthew Dillon } 3282258223a3SMatthew Dillon 3283258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 328412feb904SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 328512feb904SMatthew Dillon err_slot = -1; 3286258223a3SMatthew Dillon ahci_unload_prdt(ccb); 328712feb904SMatthew Dillon goto err; 328812feb904SMatthew Dillon } 328912feb904SMatthew Dillon ahci_unload_prdt(ccb); 3290258223a3SMatthew Dillon 329112feb904SMatthew Dillon /* 329212feb904SMatthew Dillon * Success, extract failed register set and tags from the scratch 329312feb904SMatthew Dillon * space. 329412feb904SMatthew Dillon */ 3295258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 3296258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 3297258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 329812feb904SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ error?\n", 3299258223a3SMatthew Dillon PORTNAME(ap)); 330012feb904SMatthew Dillon err_slot = -1; 3301258223a3SMatthew Dillon } else { 3302258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 330312feb904SMatthew Dillon err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK; 3304258223a3SMatthew Dillon 3305e1014452SMatthew Dillon ccb2 = &ap->ap_ccbs[err_slot]; 3306e1014452SMatthew Dillon if (ccb2->ccb_xa.state == ATA_S_ONCHIP) { 330712feb904SMatthew Dillon kprintf("%s: read NCQ error page slot=%d\n", 3308e1014452SMatthew Dillon ATANAME(ap, ccb2->ccb_xa.at), 330912feb904SMatthew Dillon err_slot); 3310e1014452SMatthew Dillon memcpy(&ccb2->ccb_xa.rfis, &log->err_regs, 3311258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 3312e1014452SMatthew Dillon ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 3313e1014452SMatthew Dillon ccb2->ccb_xa.rfis.flags = 0; 331412feb904SMatthew Dillon } else { 331512feb904SMatthew Dillon kprintf("%s: read NCQ error page slot=%d, " 331612feb904SMatthew Dillon "slot does not match any cmds\n", 3317e1014452SMatthew Dillon ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at), 331812feb904SMatthew Dillon err_slot); 331912feb904SMatthew Dillon err_slot = -1; 3320258223a3SMatthew Dillon } 3321258223a3SMatthew Dillon } 332212feb904SMatthew Dillon err: 332312feb904SMatthew Dillon ahci_put_err_ccb(ccb); 332412feb904SMatthew Dillon kprintf("%s: DONE log page target %d err_slot=%d\n", 332512feb904SMatthew Dillon PORTNAME(ap), target, err_slot); 332612feb904SMatthew Dillon return (err_slot); 3327258223a3SMatthew Dillon } 3328258223a3SMatthew Dillon 3329258223a3SMatthew Dillon /* 3330258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 3331258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 3332258223a3SMatthew Dillon * single physical base address. 3333258223a3SMatthew Dillon */ 3334258223a3SMatthew Dillon struct ahci_dmamem * 3335258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 3336258223a3SMatthew Dillon { 3337258223a3SMatthew Dillon struct ahci_dmamem *adm; 3338258223a3SMatthew Dillon int error; 3339258223a3SMatthew Dillon 3340258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 3341258223a3SMatthew Dillon 3342258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 3343258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 3344258223a3SMatthew Dillon if (error == 0) { 3345258223a3SMatthew Dillon adm->adm_tag = tag; 3346258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 3347258223a3SMatthew Dillon adm->adm_kva, 3348258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 3349258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 3350258223a3SMatthew Dillon 0); 3351258223a3SMatthew Dillon } 3352258223a3SMatthew Dillon if (error) { 3353258223a3SMatthew Dillon if (adm->adm_map) { 3354258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 3355258223a3SMatthew Dillon adm->adm_map = NULL; 3356258223a3SMatthew Dillon adm->adm_tag = NULL; 3357258223a3SMatthew Dillon adm->adm_kva = NULL; 3358258223a3SMatthew Dillon } 3359258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 3360258223a3SMatthew Dillon adm = NULL; 3361258223a3SMatthew Dillon } 3362258223a3SMatthew Dillon return (adm); 3363258223a3SMatthew Dillon } 3364258223a3SMatthew Dillon 3365258223a3SMatthew Dillon static 3366258223a3SMatthew Dillon void 3367258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 3368258223a3SMatthew Dillon { 3369258223a3SMatthew Dillon KKASSERT(error == 0); 3370258223a3SMatthew Dillon KKASSERT(nsegs == 1); 3371258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 3372258223a3SMatthew Dillon } 3373258223a3SMatthew Dillon 3374258223a3SMatthew Dillon 3375258223a3SMatthew Dillon void 3376258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 3377258223a3SMatthew Dillon { 3378258223a3SMatthew Dillon if (adm->adm_map) { 3379258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 3380258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 3381258223a3SMatthew Dillon adm->adm_map = NULL; 3382258223a3SMatthew Dillon adm->adm_tag = NULL; 3383258223a3SMatthew Dillon adm->adm_kva = NULL; 3384258223a3SMatthew Dillon } 3385258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 3386258223a3SMatthew Dillon } 3387258223a3SMatthew Dillon 3388258223a3SMatthew Dillon u_int32_t 3389258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 3390258223a3SMatthew Dillon { 3391258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 3392258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 3393258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 3394258223a3SMatthew Dillon } 3395258223a3SMatthew Dillon 3396258223a3SMatthew Dillon void 3397258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 3398258223a3SMatthew Dillon { 3399258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 3400258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 3401258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 3402258223a3SMatthew Dillon } 3403258223a3SMatthew Dillon 3404258223a3SMatthew Dillon u_int32_t 3405258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 3406258223a3SMatthew Dillon { 3407258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3408258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 3409258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 3410258223a3SMatthew Dillon } 3411258223a3SMatthew Dillon 3412258223a3SMatthew Dillon void 3413258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 3414258223a3SMatthew Dillon { 3415258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 3416258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3417258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 3418258223a3SMatthew Dillon } 3419258223a3SMatthew Dillon 3420831bc9e3SMatthew Dillon /* 3421831bc9e3SMatthew Dillon * Wait up to (timeout) milliseconds for the masked port register to 3422831bc9e3SMatthew Dillon * match the target. 3423831bc9e3SMatthew Dillon * 3424831bc9e3SMatthew Dillon * Timeout is in milliseconds. 3425831bc9e3SMatthew Dillon */ 3426258223a3SMatthew Dillon int 3427cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 3428cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 3429258223a3SMatthew Dillon { 3430831bc9e3SMatthew Dillon int t; 3431258223a3SMatthew Dillon 3432831bc9e3SMatthew Dillon /* 3433831bc9e3SMatthew Dillon * Loop hard up to 100uS 3434831bc9e3SMatthew Dillon */ 3435831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3436258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3437258223a3SMatthew Dillon return (0); 3438831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3439258223a3SMatthew Dillon } 3440258223a3SMatthew Dillon 3441831bc9e3SMatthew Dillon do { 3442831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 3443831bc9e3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3444831bc9e3SMatthew Dillon return (0); 3445831bc9e3SMatthew Dillon } while (timeout > 0); 3446831bc9e3SMatthew Dillon return (1); 3447831bc9e3SMatthew Dillon } 3448831bc9e3SMatthew Dillon 3449831bc9e3SMatthew Dillon int 3450831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 3451831bc9e3SMatthew Dillon u_int32_t target) 3452831bc9e3SMatthew Dillon { 3453831bc9e3SMatthew Dillon int t; 3454831bc9e3SMatthew Dillon 3455831bc9e3SMatthew Dillon /* 3456831bc9e3SMatthew Dillon * Loop hard up to 100uS 3457831bc9e3SMatthew Dillon */ 3458831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3459831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3460831bc9e3SMatthew Dillon return (0); 3461831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3462831bc9e3SMatthew Dillon } 3463831bc9e3SMatthew Dillon 3464831bc9e3SMatthew Dillon /* 3465831bc9e3SMatthew Dillon * And one millisecond the slow way 3466831bc9e3SMatthew Dillon */ 3467831bc9e3SMatthew Dillon t = 1000; 3468831bc9e3SMatthew Dillon do { 3469831bc9e3SMatthew Dillon t -= ahci_os_softsleep(); 3470831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3471831bc9e3SMatthew Dillon return (0); 3472831bc9e3SMatthew Dillon } while (t > 0); 3473831bc9e3SMatthew Dillon 3474258223a3SMatthew Dillon return (1); 3475258223a3SMatthew Dillon } 3476258223a3SMatthew Dillon 3477831bc9e3SMatthew Dillon 34781980eff3SMatthew Dillon /* 34791980eff3SMatthew Dillon * Acquire an ata transfer. 34801980eff3SMatthew Dillon * 34811980eff3SMatthew Dillon * Pass a NULL at for direct-attached transfers, and a non-NULL at for 34821980eff3SMatthew Dillon * targets that go through the port multiplier. 34831980eff3SMatthew Dillon */ 3484258223a3SMatthew Dillon struct ata_xfer * 34851980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at) 3486258223a3SMatthew Dillon { 3487258223a3SMatthew Dillon struct ahci_ccb *ccb; 3488258223a3SMatthew Dillon 3489258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 3490258223a3SMatthew Dillon if (ccb == NULL) { 3491258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 3492258223a3SMatthew Dillon PORTNAME(ap)); 3493258223a3SMatthew Dillon return (NULL); 3494258223a3SMatthew Dillon } 3495258223a3SMatthew Dillon 3496258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 3497258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 3498258223a3SMatthew Dillon 34992cc2e845SMatthew Dillon bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis)); 35001980eff3SMatthew Dillon ccb->ccb_xa.at = at; 3501258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 3502258223a3SMatthew Dillon 3503258223a3SMatthew Dillon return (&ccb->ccb_xa); 3504258223a3SMatthew Dillon } 3505258223a3SMatthew Dillon 3506258223a3SMatthew Dillon void 3507258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 3508258223a3SMatthew Dillon { 3509258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3510258223a3SMatthew Dillon 3511258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 3512258223a3SMatthew Dillon 3513258223a3SMatthew Dillon ahci_put_ccb(ccb); 3514258223a3SMatthew Dillon } 3515258223a3SMatthew Dillon 3516258223a3SMatthew Dillon int 3517258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 3518258223a3SMatthew Dillon { 3519258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3520258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 3521258223a3SMatthew Dillon 3522258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 3523258223a3SMatthew Dillon 3524258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 3525258223a3SMatthew Dillon goto failcmd; 3526258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 3527258223a3SMatthew Dillon 3528258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 3529258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 35301980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 35311980eff3SMatthew Dillon cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target << 35321980eff3SMatthew Dillon AHCI_CMD_LIST_FLAG_PMP_SHIFT); 35331980eff3SMatthew Dillon } 3534258223a3SMatthew Dillon 3535258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 3536258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 3537258223a3SMatthew Dillon 3538258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 3539258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 3540258223a3SMatthew Dillon 3541258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 3542258223a3SMatthew Dillon goto failcmd; 3543258223a3SMatthew Dillon 3544258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 3545258223a3SMatthew Dillon 3546831bc9e3SMatthew Dillon if (xa->flags & ATA_F_POLL) 3547831bc9e3SMatthew Dillon return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout)); 3548258223a3SMatthew Dillon 3549258223a3SMatthew Dillon crit_enter(); 3550f4553de1SMatthew Dillon KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0); 35513209f581SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_DESIRED; 3552258223a3SMatthew Dillon ahci_start(ccb); 3553258223a3SMatthew Dillon crit_exit(); 3554831bc9e3SMatthew Dillon return (xa->state); 3555258223a3SMatthew Dillon 3556258223a3SMatthew Dillon failcmd: 3557258223a3SMatthew Dillon crit_enter(); 3558258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 3559258223a3SMatthew Dillon xa->complete(xa); 3560258223a3SMatthew Dillon crit_exit(); 3561831bc9e3SMatthew Dillon return (ATA_S_ERROR); 3562258223a3SMatthew Dillon } 3563258223a3SMatthew Dillon 3564258223a3SMatthew Dillon void 3565258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 3566258223a3SMatthew Dillon { 3567258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3568bb79834dSMatthew Dillon int serial; 3569258223a3SMatthew Dillon 3570831bc9e3SMatthew Dillon /* 3571bb79834dSMatthew Dillon * NOTE: Callout does not lock port and may race us modifying 3572831bc9e3SMatthew Dillon * the flags, so make sure its stopped. 3573bb79834dSMatthew Dillon * 3574bb79834dSMatthew Dillon * A callout race can clean up the ccb. A change in the 3575bb79834dSMatthew Dillon * serial number should catch this condition. 3576831bc9e3SMatthew Dillon */ 3577258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 3578bb79834dSMatthew Dillon serial = ccb->ccb_xa.serial; 357946528d33SMatthew Dillon callout_stop_sync(&ccb->ccb_timeout); 3580bb79834dSMatthew Dillon if (serial != ccb->ccb_xa.serial) { 3581bb79834dSMatthew Dillon kprintf("%s: Warning: timeout race ccb %p\n", 3582bb79834dSMatthew Dillon PORTNAME(ccb->ccb_port), ccb); 3583bb79834dSMatthew Dillon return; 3584bb79834dSMatthew Dillon } 35858dc94ed9SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 3586258223a3SMatthew Dillon } 3587f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED); 358846528d33SMatthew Dillon ccb->ccb_port->ap_expired &= ~(1 << ccb->ccb_slot); 3589258223a3SMatthew Dillon 359046528d33SMatthew Dillon KKASSERT(xa->state != ATA_S_ONCHIP && xa->state != ATA_S_PUT); 3591258223a3SMatthew Dillon ahci_unload_prdt(ccb); 3592258223a3SMatthew Dillon 3593258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 3594258223a3SMatthew Dillon xa->complete(xa); 3595258223a3SMatthew Dillon } 3596258223a3SMatthew Dillon 3597f4553de1SMatthew Dillon /* 3598f4553de1SMatthew Dillon * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags 3599f4553de1SMatthew Dillon * while the callout is runing. 3600f4553de1SMatthew Dillon * 3601f4553de1SMatthew Dillon * We can't safely get the port lock here or delay, we could block 3602f4553de1SMatthew Dillon * the callout thread. 3603f4553de1SMatthew Dillon */ 3604258223a3SMatthew Dillon static void 3605258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 3606258223a3SMatthew Dillon { 3607258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 3608258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3609258223a3SMatthew Dillon 361046528d33SMatthew Dillon KKASSERT(ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING); 3611f4553de1SMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 3612f4553de1SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED; 3613f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT); 3614258223a3SMatthew Dillon } 3615258223a3SMatthew Dillon 36164c339a5fSMatthew Dillon /* 36174c339a5fSMatthew Dillon * Timeout code, typically called when the port command processor is running. 36184c339a5fSMatthew Dillon * 36194c339a5fSMatthew Dillon * We have to be very very careful here. We cannot stop the port unless 36204c339a5fSMatthew Dillon * CR is already clear or the only active commands remaining are timed-out 36214c339a5fSMatthew Dillon * ones. Otherwise stopping the port will race the command processor and 36224c339a5fSMatthew Dillon * we can lose events. While we can theoretically just restart everything 36234c339a5fSMatthew Dillon * that could result in a double-issue which will not work for ATAPI commands. 36244c339a5fSMatthew Dillon */ 36251980eff3SMatthew Dillon void 3626831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb) 3627258223a3SMatthew Dillon { 3628258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3629258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 36304c339a5fSMatthew Dillon struct ata_port *at; 3631492bffafSMatthew Dillon u_int32_t ci_saved; 3632492bffafSMatthew Dillon u_int32_t mask; 36334c339a5fSMatthew Dillon int slot; 3634258223a3SMatthew Dillon 36354c339a5fSMatthew Dillon at = ccb->ccb_xa.at; 36364c339a5fSMatthew Dillon 36374c339a5fSMatthew Dillon kprintf("%s: CMD TIMEOUT state=%d slot=%d\n" 36383d102df7SMatthew Dillon "\tglb-status 0x%08x\n" 36394c339a5fSMatthew Dillon "\tcmd-reg 0x%b\n" 36403d102df7SMatthew Dillon "\tport_status 0x%b\n" 36414c339a5fSMatthew Dillon "\tsactive=%08x active=%08x expired=%08x\n" 364208fb24a7SMatthew Dillon "\t sact=%08x ci=%08x\n" 364308fb24a7SMatthew Dillon "\t STS=%b\n", 36444c339a5fSMatthew Dillon ATANAME(ap, at), 36454c339a5fSMatthew Dillon ccb->ccb_xa.state, ccb->ccb_slot, 36463d102df7SMatthew Dillon ahci_read(ap->ap_sc, AHCI_REG_IS), 3647258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 36483d102df7SMatthew Dillon ahci_pread(ap, AHCI_PREG_IS), AHCI_PFMT_IS, 36494c339a5fSMatthew Dillon ap->ap_sactive, ap->ap_active, ap->ap_expired, 3650258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 365108fb24a7SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI), 365208fb24a7SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS 365308fb24a7SMatthew Dillon ); 365408fb24a7SMatthew Dillon 3655258223a3SMatthew Dillon 36569e145b23SMatthew Dillon /* 36579e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 36583209f581SMatthew Dillon * If we got here at least one of these flags should be set. 36599e145b23SMatthew Dillon */ 36603209f581SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED | 36613209f581SMatthew Dillon ATA_F_TIMEOUT_RUNNING)); 3662f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED); 3663258223a3SMatthew Dillon 3664258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 3665258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 36664c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 36674c339a5fSMatthew Dillon ccb->ccb_done(ccb); 36684c339a5fSMatthew Dillon xa->complete(xa); 36694c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 36704c339a5fSMatthew Dillon return; 36714c339a5fSMatthew Dillon } 36724c339a5fSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 36734c339a5fSMatthew Dillon kprintf("%s: Unexpected state during timeout: %d\n", 36744c339a5fSMatthew Dillon ATANAME(ap, at), ccb->ccb_xa.state); 36754c339a5fSMatthew Dillon return; 36764c339a5fSMatthew Dillon } 36774c339a5fSMatthew Dillon 36784c339a5fSMatthew Dillon /* 36794c339a5fSMatthew Dillon * Ok, we can only get this command off the chip if CR is inactive 36804c339a5fSMatthew Dillon * or if the only commands running on the chip are all expired. 36814c339a5fSMatthew Dillon * Otherwise we have to wait until the port is in a safe state. 36824c339a5fSMatthew Dillon * 36834c339a5fSMatthew Dillon * Do not set state here, it will cause polls to return when the 36844c339a5fSMatthew Dillon * ccb is not yet off the chip. 36854c339a5fSMatthew Dillon */ 36864c339a5fSMatthew Dillon ap->ap_expired |= 1 << ccb->ccb_slot; 36874c339a5fSMatthew Dillon 36884c339a5fSMatthew Dillon if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) && 36894c339a5fSMatthew Dillon (ap->ap_active | ap->ap_sactive) != ap->ap_expired) { 36904c339a5fSMatthew Dillon /* 36914c339a5fSMatthew Dillon * If using FBSS or NCQ we can't safely stop the port 36924c339a5fSMatthew Dillon * right now. 36934c339a5fSMatthew Dillon */ 36944c339a5fSMatthew Dillon kprintf("%s: Deferred timeout until its safe, slot %d\n", 36954c339a5fSMatthew Dillon ATANAME(ap, at), ccb->ccb_slot); 36964c339a5fSMatthew Dillon return; 36974c339a5fSMatthew Dillon } 36984c339a5fSMatthew Dillon 36994c339a5fSMatthew Dillon /* 37004c339a5fSMatthew Dillon * We can safely stop the port and process all expired ccb's, 37014c339a5fSMatthew Dillon * which will include our current ccb. 37024c339a5fSMatthew Dillon */ 37034c339a5fSMatthew Dillon ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) : 37044c339a5fSMatthew Dillon ahci_pread(ap, AHCI_PREG_CI); 37054c339a5fSMatthew Dillon ahci_port_stop(ap, 0); 37064c339a5fSMatthew Dillon 37074c339a5fSMatthew Dillon while (ap->ap_expired) { 37084c339a5fSMatthew Dillon slot = ffs(ap->ap_expired) - 1; 37094c339a5fSMatthew Dillon ap->ap_expired &= ~(1 << slot); 37104c339a5fSMatthew Dillon ci_saved &= ~(1 << slot); 37114c339a5fSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 37124c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 37134c339a5fSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 37144c339a5fSMatthew Dillon KKASSERT(ap->ap_sactive & (1 << slot)); 37154c339a5fSMatthew Dillon ap->ap_sactive &= ~(1 << slot); 37164c339a5fSMatthew Dillon } else { 37174c339a5fSMatthew Dillon KKASSERT(ap->ap_active & (1 << slot)); 37184c339a5fSMatthew Dillon ap->ap_active &= ~(1 << slot); 37191980eff3SMatthew Dillon --ap->ap_active_cnt; 37201980eff3SMatthew Dillon } 3721258223a3SMatthew Dillon ccb->ccb_done(ccb); 37224c339a5fSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 3723258223a3SMatthew Dillon } 37244c339a5fSMatthew Dillon /* ccb invalid now */ 3725258223a3SMatthew Dillon 37264c339a5fSMatthew Dillon /* 37274c339a5fSMatthew Dillon * We can safely CLO the port to clear any BSY/DRQ, a case which 37284c339a5fSMatthew Dillon * can occur with port multipliers. This will unbrick the port 37294c339a5fSMatthew Dillon * and allow commands to other targets behind the PM continue. 37304c339a5fSMatthew Dillon * (FBSS). 37314c339a5fSMatthew Dillon * 37324c339a5fSMatthew Dillon * Finally, once the port has been restarted we can issue any 37334c339a5fSMatthew Dillon * previously saved pending commands, and run the port interrupt 37344c339a5fSMatthew Dillon * code to handle any completions which may have occured when 37354c339a5fSMatthew Dillon * we saved CI. 37364c339a5fSMatthew Dillon */ 37374c339a5fSMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 37384c339a5fSMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 37394c339a5fSMatthew Dillon kprintf("%s: Warning, issuing CLO after timeout\n", 37404c339a5fSMatthew Dillon ATANAME(ap, at)); 3741131be210SMatthew Dillon ahci_port_clo(ap); 37424c339a5fSMatthew Dillon } 3743131be210SMatthew Dillon ahci_port_start(ap); 3744492bffafSMatthew Dillon 3745492bffafSMatthew Dillon /* 3746492bffafSMatthew Dillon * We absolutely must make sure the chipset cleared activity on 3747492bffafSMatthew Dillon * all slots. This sometimes might not happen due to races with 3748492bffafSMatthew Dillon * a chipset interrupt which stops the port before we can manage 3749492bffafSMatthew Dillon * to. For some reason some chipsets don't clear the active 3750492bffafSMatthew Dillon * commands when we turn off CMD_ST after the chip has stopped 3751492bffafSMatthew Dillon * operations itself. 3752492bffafSMatthew Dillon */ 3753492bffafSMatthew Dillon if (ahci_pactive(ap) != 0) { 3754492bffafSMatthew Dillon ahci_port_stop(ap, 0); 3755492bffafSMatthew Dillon ahci_port_start(ap); 3756492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 3757492bffafSMatthew Dillon kprintf("%s: quick-timeout: chipset failed " 3758492bffafSMatthew Dillon "to clear active cmds %08x\n", 3759492bffafSMatthew Dillon PORTNAME(ap), mask); 3760492bffafSMatthew Dillon } 3761492bffafSMatthew Dillon } 37624c339a5fSMatthew Dillon ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired); 37634c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 37644c339a5fSMatthew Dillon ahci_port_intr(ap, 0); 37654c339a5fSMatthew Dillon } 37664c339a5fSMatthew Dillon 3767cf5f3a81SMatthew Dillon /* 37684c339a5fSMatthew Dillon * Issue a previously saved set of commands 3769cf5f3a81SMatthew Dillon */ 37704c339a5fSMatthew Dillon void 37714c339a5fSMatthew Dillon ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved) 37724c339a5fSMatthew Dillon { 37734c339a5fSMatthew Dillon if (ci_saved) { 37744c339a5fSMatthew Dillon KKASSERT(!((ap->ap_active & ci_saved) && 37754c339a5fSMatthew Dillon (ap->ap_sactive & ci_saved))); 37764c339a5fSMatthew Dillon KKASSERT((ci_saved & ap->ap_expired) == 0); 37774c339a5fSMatthew Dillon if (ap->ap_sactive & ci_saved) 37784c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 37794c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 3780131be210SMatthew Dillon } 3781258223a3SMatthew Dillon } 3782258223a3SMatthew Dillon 3783831bc9e3SMatthew Dillon /* 3784831bc9e3SMatthew Dillon * Used by the softreset, pmprobe, and read_ncq_error only, in very 3785831bc9e3SMatthew Dillon * specialized, controlled circumstances. 3786831bc9e3SMatthew Dillon * 3787831bc9e3SMatthew Dillon * Only one command may be pending. 3788831bc9e3SMatthew Dillon */ 3789831bc9e3SMatthew Dillon void 3790831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb) 3791831bc9e3SMatthew Dillon { 3792831bc9e3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3793492bffafSMatthew Dillon u_int32_t mask; 3794831bc9e3SMatthew Dillon 3795831bc9e3SMatthew Dillon switch (ccb->ccb_xa.state) { 3796831bc9e3SMatthew Dillon case ATA_S_PENDING: 3797831bc9e3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 3798831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3799831bc9e3SMatthew Dillon break; 3800831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 3801492bffafSMatthew Dillon /* 3802492bffafSMatthew Dillon * We have to clear the command on-chip. 3803492bffafSMatthew Dillon */ 3804831bc9e3SMatthew Dillon KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) && 3805831bc9e3SMatthew Dillon ap->ap_sactive == 0); 3806831bc9e3SMatthew Dillon ahci_port_stop(ap, 0); 3807831bc9e3SMatthew Dillon ahci_port_start(ap); 3808492bffafSMatthew Dillon if (ahci_pactive(ap) != 0) { 3809492bffafSMatthew Dillon ahci_port_stop(ap, 0); 3810492bffafSMatthew Dillon ahci_port_start(ap); 3811492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 3812492bffafSMatthew Dillon kprintf("%s: quick-timeout: chipset failed " 3813492bffafSMatthew Dillon "to clear active cmds %08x\n", 3814492bffafSMatthew Dillon PORTNAME(ap), mask); 3815492bffafSMatthew Dillon } 3816492bffafSMatthew Dillon } 3817831bc9e3SMatthew Dillon 3818831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3819831bc9e3SMatthew Dillon ap->ap_active &= ~(1 << ccb->ccb_slot); 3820831bc9e3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 3821831bc9e3SMatthew Dillon --ap->ap_active_cnt; 3822831bc9e3SMatthew Dillon break; 3823831bc9e3SMatthew Dillon default: 3824831bc9e3SMatthew Dillon panic("%s: ahci_quick_timeout: ccb in bad state %d", 3825831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state); 3826831bc9e3SMatthew Dillon } 3827831bc9e3SMatthew Dillon } 3828831bc9e3SMatthew Dillon 382912feb904SMatthew Dillon static void 383012feb904SMatthew Dillon ahci_dummy_done(struct ata_xfer *xa) 383112feb904SMatthew Dillon { 383212feb904SMatthew Dillon } 383312feb904SMatthew Dillon 383412feb904SMatthew Dillon static void 3835258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 3836258223a3SMatthew Dillon { 3837258223a3SMatthew Dillon } 3838795adb22SMatthew Dillon 3839795adb22SMatthew Dillon int 3840492bffafSMatthew Dillon ahci_set_feature(struct ahci_port *ap, struct ata_port *atx, 3841492bffafSMatthew Dillon int feature, int enable) 3842795adb22SMatthew Dillon { 3843795adb22SMatthew Dillon struct ata_port *at; 3844795adb22SMatthew Dillon struct ata_xfer *xa; 3845795adb22SMatthew Dillon int error; 3846795adb22SMatthew Dillon 3847795adb22SMatthew Dillon at = atx ? atx : ap->ap_ata[0]; 3848795adb22SMatthew Dillon 3849795adb22SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 3850795adb22SMatthew Dillon 3851795adb22SMatthew Dillon xa->fis->type = ATA_FIS_TYPE_H2D; 3852795adb22SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 3853795adb22SMatthew Dillon xa->fis->command = ATA_C_SET_FEATURES; 3854750495d0SImre Vadász xa->fis->features = enable ? ATA_SF_SATAFT_ENA : ATA_SF_SATAFT_DIS; 3855795adb22SMatthew Dillon xa->fis->sector_count = feature; 3856795adb22SMatthew Dillon xa->fis->control = ATA_FIS_CONTROL_4BIT; 3857795adb22SMatthew Dillon 3858795adb22SMatthew Dillon xa->complete = ahci_dummy_done; 3859795adb22SMatthew Dillon xa->datalen = 0; 3860795adb22SMatthew Dillon xa->flags = ATA_F_POLL; 3861795adb22SMatthew Dillon xa->timeout = 1000; 3862795adb22SMatthew Dillon 3863795adb22SMatthew Dillon if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 3864795adb22SMatthew Dillon error = 0; 3865795adb22SMatthew Dillon else 3866795adb22SMatthew Dillon error = EIO; 3867795adb22SMatthew Dillon ahci_ata_put_xfer(xa); 3868795adb22SMatthew Dillon return(error); 3869795adb22SMatthew Dillon } 3870