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 { 9312feb904SMatthew Dillon u_int32_t cap, pi, pleft; 94831bc9e3SMatthew Dillon int i; 95831bc9e3SMatthew Dillon struct ahci_port *ap; 96258223a3SMatthew Dillon 97258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b", 98258223a3SMatthew Dillon ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC); 99258223a3SMatthew Dillon 100b012a2caSMatthew Dillon /* 101b012a2caSMatthew Dillon * save BIOS initialised parameters, enable staggered spin up 102b012a2caSMatthew Dillon */ 103258223a3SMatthew Dillon cap = ahci_read(sc, AHCI_REG_CAP); 104258223a3SMatthew Dillon cap &= AHCI_REG_CAP_SMPS; 105258223a3SMatthew Dillon cap |= AHCI_REG_CAP_SSS; 106258223a3SMatthew Dillon pi = ahci_read(sc, AHCI_REG_PI); 107258223a3SMatthew Dillon 108831bc9e3SMatthew Dillon /* 109b012a2caSMatthew Dillon * Unconditionally reset the controller, do not conditionalize on 110b012a2caSMatthew Dillon * trying to figure it if it was previously active or not. 111b012a2caSMatthew Dillon * 112b012a2caSMatthew Dillon * NOTE: On AE before HR. The AHCI-1.1 spec has a note in section 113b012a2caSMatthew Dillon * 5.2.2.1 regarding this. HR should be set to 1 only after 114b012a2caSMatthew Dillon * AE is set to 1. The reset sequence will clear HR when 115b012a2caSMatthew Dillon * it completes, and will also clear AE if SAM is 0. AE must 116b012a2caSMatthew Dillon * then be set again. When SAM is 1 the AE bit typically reads 117b012a2caSMatthew Dillon * as 1 (and is read-only). 118b012a2caSMatthew Dillon * 119b012a2caSMatthew Dillon * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads, 120b012a2caSMatthew Dillon * otherwise the writes will only be separated by a few 121b012a2caSMatthew Dillon * nanoseconds. 122b012a2caSMatthew Dillon * 123b012a2caSMatthew Dillon * NOTE BRICKS (1) 124b012a2caSMatthew Dillon * 125b012a2caSMatthew Dillon * If you have a port multiplier and it does not have a device 126b012a2caSMatthew Dillon * in target 0, and it probes normally, but a later operation 127b012a2caSMatthew Dillon * mis-probes a target behind that PM, it is possible for the 128b012a2caSMatthew Dillon * port to brick such that only (a) a power cycle of the host 129b012a2caSMatthew Dillon * or (b) placing a device in target 0 will fix the problem. 130b012a2caSMatthew Dillon * Power cycling the PM has no effect (it works fine on another 131b012a2caSMatthew Dillon * host port). This issue is unrelated to CLO. 132b012a2caSMatthew Dillon */ 1334e21f4daSMatthew Dillon /* 1344e21f4daSMatthew Dillon * Wait for any prior reset sequence to complete 1354e21f4daSMatthew Dillon */ 1364e21f4daSMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, 1374e21f4daSMatthew Dillon AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) { 1384e21f4daSMatthew Dillon device_printf(sc->sc_dev, "Controller is stuck in reset\n"); 1394e21f4daSMatthew Dillon return (1); 1404e21f4daSMatthew Dillon } 141b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 1424e21f4daSMatthew Dillon ahci_os_sleep(500); 143b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 144b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR); 1454e21f4daSMatthew Dillon ahci_os_sleep(500); 146b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 147b012a2caSMatthew Dillon if (ahci_wait_ne(sc, AHCI_REG_GHC, 148b012a2caSMatthew Dillon AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) { 1494e21f4daSMatthew Dillon device_printf(sc->sc_dev, "unable to reset controller\n"); 150b012a2caSMatthew Dillon return (1); 151b012a2caSMatthew Dillon } 1524e21f4daSMatthew Dillon if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) { 1534e21f4daSMatthew Dillon device_printf(sc->sc_dev, "AE did not auto-clear!\n"); 1544e21f4daSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, 0); 1554e21f4daSMatthew Dillon ahci_os_sleep(500); 1564e21f4daSMatthew Dillon } 157b012a2caSMatthew Dillon 158b012a2caSMatthew Dillon /* 159b012a2caSMatthew Dillon * Enable ahci (global interrupts disabled) 160b012a2caSMatthew Dillon * 161b012a2caSMatthew Dillon * Restore saved parameters. Avoid pci transaction burst write 162b012a2caSMatthew Dillon * by issuing dummy reads. 163b012a2caSMatthew Dillon */ 1644e21f4daSMatthew Dillon ahci_os_sleep(500); 165b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE); 166b012a2caSMatthew Dillon ahci_os_sleep(500); 167b012a2caSMatthew Dillon 168b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 169b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_CAP, cap); 170b012a2caSMatthew Dillon ahci_write(sc, AHCI_REG_PI, pi); 171b012a2caSMatthew Dillon ahci_read(sc, AHCI_REG_GHC); /* flush */ 172b012a2caSMatthew Dillon 173b012a2caSMatthew Dillon /* 174b012a2caSMatthew Dillon * Intel hocus pocus in case the BIOS has not set the chip up 175b012a2caSMatthew Dillon * properly for AHCI operation. 176b012a2caSMatthew Dillon */ 177b012a2caSMatthew Dillon if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) { 178b012a2caSMatthew Dillon if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F) 179b012a2caSMatthew Dillon device_printf(sc->sc_dev, "Intel hocus pocus\n"); 180b012a2caSMatthew Dillon pci_write_config(sc->sc_dev, 0x92, 181b012a2caSMatthew Dillon pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2); 182b012a2caSMatthew Dillon } 183b012a2caSMatthew Dillon 184b012a2caSMatthew Dillon /* 185831bc9e3SMatthew Dillon * This is a hack that currently does not appear to have 186831bc9e3SMatthew Dillon * a significant effect, but I noticed the port registers 187831bc9e3SMatthew Dillon * do not appear to be completely cleared after the host 188831bc9e3SMatthew Dillon * controller is reset. 18912feb904SMatthew Dillon * 19012feb904SMatthew Dillon * Use a temporary ap structure so we can call ahci_pwrite(). 1914e21f4daSMatthew Dillon * 1924e21f4daSMatthew Dillon * We must be sure to stop the port 193831bc9e3SMatthew Dillon */ 194831bc9e3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 195831bc9e3SMatthew Dillon ap->ap_sc = sc; 19612feb904SMatthew Dillon pleft = pi; 19712feb904SMatthew Dillon for (i = 0; i < AHCI_MAX_PORTS; ++i) { 19812feb904SMatthew Dillon if (pleft == 0) 19912feb904SMatthew Dillon break; 200831bc9e3SMatthew Dillon if ((pi & (1 << i)) == 0) 201831bc9e3SMatthew Dillon continue; 202831bc9e3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 203831bc9e3SMatthew Dillon AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 204831bc9e3SMatthew Dillon device_printf(sc->sc_dev, "can't map port\n"); 205831bc9e3SMatthew Dillon return (1); 206831bc9e3SMatthew Dillon } 2074e21f4daSMatthew Dillon /* 2084e21f4daSMatthew Dillon * NOTE! Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or 2094e21f4daSMatthew Dillon * AHCI1.1 can brick the chipset. Not only brick it, 2104e21f4daSMatthew Dillon * but also crash the PC. The bit seems unreliable 2114e21f4daSMatthew Dillon * on AHCI1.2 as well. 2124e21f4daSMatthew Dillon */ 2134e21f4daSMatthew Dillon ahci_port_stop(ap, 1); 2144e21f4daSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 215831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 216831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 21712feb904SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i); 218831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 21912feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, -1); 22012feb904SMatthew Dillon sc->sc_portmask |= (1 << i); 22112feb904SMatthew Dillon pleft &= ~(1 << i); 222831bc9e3SMatthew Dillon } 22312feb904SMatthew Dillon sc->sc_numports = i; 224831bc9e3SMatthew Dillon kfree(ap, M_DEVBUF); 225831bc9e3SMatthew Dillon 226258223a3SMatthew Dillon return (0); 227258223a3SMatthew Dillon } 228258223a3SMatthew Dillon 229fd8bd957SMatthew Dillon /* 230fd8bd957SMatthew Dillon * Allocate and initialize an AHCI port. 231fd8bd957SMatthew Dillon */ 232258223a3SMatthew Dillon int 233258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port) 234258223a3SMatthew Dillon { 235258223a3SMatthew Dillon struct ahci_port *ap; 2361980eff3SMatthew Dillon struct ata_port *at; 237258223a3SMatthew Dillon struct ahci_ccb *ccb; 238258223a3SMatthew Dillon u_int64_t dva; 239258223a3SMatthew Dillon u_int32_t cmd; 24012feb904SMatthew Dillon u_int32_t data; 241258223a3SMatthew Dillon struct ahci_cmd_hdr *hdr; 242258223a3SMatthew Dillon struct ahci_cmd_table *table; 243258223a3SMatthew Dillon int rc = ENOMEM; 244258223a3SMatthew Dillon int error; 245258223a3SMatthew Dillon int i; 246258223a3SMatthew Dillon 247258223a3SMatthew Dillon ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO); 24812feb904SMatthew Dillon ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO); 249258223a3SMatthew Dillon 250258223a3SMatthew Dillon ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d", 251258223a3SMatthew Dillon device_get_name(sc->sc_dev), 252258223a3SMatthew Dillon device_get_unit(sc->sc_dev), 253258223a3SMatthew Dillon port); 254258223a3SMatthew Dillon sc->sc_ports[port] = ap; 255258223a3SMatthew Dillon 2561980eff3SMatthew Dillon /* 2571980eff3SMatthew Dillon * Allocate enough so we never have to reallocate, it makes 2581980eff3SMatthew Dillon * it easier. 2591980eff3SMatthew Dillon * 2601980eff3SMatthew Dillon * ap_pmcount will be reduced by the scan if we encounter the 2611980eff3SMatthew Dillon * port multiplier port prior to target 15. 262b012a2caSMatthew Dillon * 263b012a2caSMatthew Dillon * kmalloc power-of-2 allocations are guaranteed not to cross 264b012a2caSMatthew Dillon * a page boundary. Make sure the identify sub-structure in the 265b012a2caSMatthew Dillon * at structure does not cross a page boundary, just in case the 266b012a2caSMatthew Dillon * part is AHCI-1.1 and can't handle multiple DRQ blocks. 2671980eff3SMatthew Dillon */ 268b012a2caSMatthew Dillon if (ap->ap_ata[0] == NULL) { 269b012a2caSMatthew Dillon int pw2; 270b012a2caSMatthew Dillon 271b012a2caSMatthew Dillon for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1) 272b012a2caSMatthew Dillon ; 2731980eff3SMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 274b012a2caSMatthew Dillon at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO); 275b012a2caSMatthew Dillon ap->ap_ata[i] = at; 2761980eff3SMatthew Dillon at->at_ahci_port = ap; 2771980eff3SMatthew Dillon at->at_target = i; 2783209f581SMatthew Dillon at->at_probe = ATA_PROBE_NEED_INIT; 279831bc9e3SMatthew Dillon at->at_features |= ATA_PORT_F_RESCAN; 2801980eff3SMatthew Dillon ksnprintf(at->at_name, sizeof(at->at_name), 2811980eff3SMatthew Dillon "%s.%d", ap->ap_name, i); 2821980eff3SMatthew Dillon } 2831980eff3SMatthew Dillon } 284258223a3SMatthew Dillon if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 285258223a3SMatthew Dillon AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) { 286258223a3SMatthew Dillon device_printf(sc->sc_dev, 287258223a3SMatthew Dillon "unable to create register window for port %d\n", 288258223a3SMatthew Dillon port); 289258223a3SMatthew Dillon goto freeport; 290258223a3SMatthew Dillon } 291258223a3SMatthew Dillon 292258223a3SMatthew Dillon ap->ap_sc = sc; 293258223a3SMatthew Dillon ap->ap_num = port; 2943209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 295f17a0cedSMatthew Dillon ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE; 296f17a0cedSMatthew Dillon ap->sysctl_tree = NULL; 297258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_free); 298258223a3SMatthew Dillon TAILQ_INIT(&ap->ap_ccb_pending); 299258223a3SMatthew Dillon lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0); 300258223a3SMatthew Dillon 301258223a3SMatthew Dillon /* Disable port interrupts */ 302258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 303831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 304258223a3SMatthew Dillon 30517eab71eSMatthew Dillon /* 30617eab71eSMatthew Dillon * Sec 10.1.2 - deinitialise port if it is already running 30717eab71eSMatthew Dillon */ 308258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 3090be9576aSMatthew Dillon kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD); 3100be9576aSMatthew Dillon 311258223a3SMatthew Dillon if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR | 312258223a3SMatthew Dillon AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) || 313258223a3SMatthew Dillon (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) { 314258223a3SMatthew Dillon int r; 315258223a3SMatthew Dillon 316258223a3SMatthew Dillon r = ahci_port_stop(ap, 1); 317258223a3SMatthew Dillon if (r) { 318258223a3SMatthew Dillon device_printf(sc->sc_dev, 319258223a3SMatthew Dillon "unable to disable %s, ignoring port %d\n", 320258223a3SMatthew Dillon ((r == 2) ? "CR" : "FR"), port); 321258223a3SMatthew Dillon rc = ENXIO; 322258223a3SMatthew Dillon goto freeport; 323258223a3SMatthew Dillon } 324258223a3SMatthew Dillon 325258223a3SMatthew Dillon /* Write DET to zero */ 326cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 327258223a3SMatthew Dillon } 328258223a3SMatthew Dillon 329258223a3SMatthew Dillon /* Allocate RFIS */ 330258223a3SMatthew Dillon ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis); 331258223a3SMatthew Dillon if (ap->ap_dmamem_rfis == NULL) { 332cf5f3a81SMatthew Dillon kprintf("%s: NORFIS\n", PORTNAME(ap)); 333258223a3SMatthew Dillon goto nomem; 334258223a3SMatthew Dillon } 335258223a3SMatthew Dillon 336258223a3SMatthew Dillon /* Setup RFIS base address */ 337258223a3SMatthew Dillon ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis); 338258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis); 339258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32)); 340258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva); 341258223a3SMatthew Dillon 342831bc9e3SMatthew Dillon /* Clear SERR before starting FIS reception or ST or anything */ 343831bc9e3SMatthew Dillon ahci_flush_tfd(ap); 344831bc9e3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 345831bc9e3SMatthew Dillon 346258223a3SMatthew Dillon /* Enable FIS reception and activate port. */ 347258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 3481980eff3SMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 349258223a3SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 350258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 351258223a3SMatthew Dillon 352258223a3SMatthew Dillon /* Check whether port activated. Skip it if not. */ 353258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 354258223a3SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 355cf5f3a81SMatthew Dillon kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap)); 356258223a3SMatthew Dillon rc = ENXIO; 357258223a3SMatthew Dillon goto freeport; 358258223a3SMatthew Dillon } 359258223a3SMatthew Dillon 360258223a3SMatthew Dillon /* Allocate a CCB for each command slot */ 361258223a3SMatthew Dillon ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF, 362258223a3SMatthew Dillon M_WAITOK | M_ZERO); 363258223a3SMatthew Dillon if (ap->ap_ccbs == NULL) { 364258223a3SMatthew Dillon device_printf(sc->sc_dev, 365258223a3SMatthew Dillon "unable to allocate command list for port %d\n", 366258223a3SMatthew Dillon port); 367258223a3SMatthew Dillon goto freeport; 368258223a3SMatthew Dillon } 369258223a3SMatthew Dillon 370258223a3SMatthew Dillon /* Command List Structures and Command Tables */ 371258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh); 372258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt); 373258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table == NULL || 374258223a3SMatthew Dillon ap->ap_dmamem_cmd_list == NULL) { 375258223a3SMatthew Dillon nomem: 376258223a3SMatthew Dillon device_printf(sc->sc_dev, 377258223a3SMatthew Dillon "unable to allocate DMA memory for port %d\n", 378258223a3SMatthew Dillon port); 379258223a3SMatthew Dillon goto freeport; 380258223a3SMatthew Dillon } 381258223a3SMatthew Dillon 382258223a3SMatthew Dillon /* Setup command list base address */ 383258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list); 384258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32)); 385258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva); 386258223a3SMatthew Dillon 387258223a3SMatthew Dillon /* Split CCB allocation into CCBs and assign to command header/table */ 388258223a3SMatthew Dillon hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list); 389258223a3SMatthew Dillon table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table); 390258223a3SMatthew Dillon for (i = 0; i < sc->sc_ncmds; i++) { 391258223a3SMatthew Dillon ccb = &ap->ap_ccbs[i]; 392258223a3SMatthew Dillon 393258223a3SMatthew Dillon error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW, 394258223a3SMatthew Dillon &ccb->ccb_dmamap); 395258223a3SMatthew Dillon if (error) { 396258223a3SMatthew Dillon device_printf(sc->sc_dev, 397258223a3SMatthew Dillon "unable to create dmamap for port %d " 398258223a3SMatthew Dillon "ccb %d\n", port, i); 399258223a3SMatthew Dillon goto freeport; 400258223a3SMatthew Dillon } 401258223a3SMatthew Dillon 402258223a3SMatthew Dillon callout_init(&ccb->ccb_timeout); 403258223a3SMatthew Dillon ccb->ccb_slot = i; 404258223a3SMatthew Dillon ccb->ccb_port = ap; 405258223a3SMatthew Dillon ccb->ccb_cmd_hdr = &hdr[i]; 406258223a3SMatthew Dillon ccb->ccb_cmd_table = &table[i]; 407258223a3SMatthew Dillon dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) + 408258223a3SMatthew Dillon ccb->ccb_slot * sizeof(struct ahci_cmd_table); 409258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32)); 410258223a3SMatthew Dillon ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva); 411258223a3SMatthew Dillon 412258223a3SMatthew Dillon ccb->ccb_xa.fis = 413258223a3SMatthew Dillon (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 414258223a3SMatthew Dillon ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd; 415258223a3SMatthew Dillon ccb->ccb_xa.tag = i; 416258223a3SMatthew Dillon 417258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 4181067474aSMatthew Dillon 4191067474aSMatthew Dillon /* 4201067474aSMatthew Dillon * CCB[1] is the error CCB and is not get or put. It is 4211067474aSMatthew Dillon * also used for probing. Numerous HBAs only load the 4221067474aSMatthew Dillon * signature from CCB[1] so it MUST be used for the second 4231067474aSMatthew Dillon * FIS. 4241067474aSMatthew Dillon */ 4251067474aSMatthew Dillon if (i == 1) 4261067474aSMatthew Dillon ap->ap_err_ccb = ccb; 4271067474aSMatthew Dillon else 428258223a3SMatthew Dillon ahci_put_ccb(ccb); 429258223a3SMatthew Dillon } 430258223a3SMatthew Dillon 43112feb904SMatthew Dillon /* 43212feb904SMatthew Dillon * Wait for ICC change to complete 43312feb904SMatthew Dillon */ 434258223a3SMatthew Dillon ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC); 435258223a3SMatthew Dillon 436fd8bd957SMatthew Dillon /* 43712feb904SMatthew Dillon * Calculate the interrupt mask 43812feb904SMatthew Dillon */ 43912feb904SMatthew Dillon data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE | 44012feb904SMatthew Dillon AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE | 44112feb904SMatthew Dillon AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE | 44212feb904SMatthew Dillon AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE | 44312feb904SMatthew Dillon AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE; 44412feb904SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 44512feb904SMatthew Dillon data |= AHCI_PREG_IE_IPME; 44612feb904SMatthew Dillon #ifdef AHCI_COALESCE 44712feb904SMatthew Dillon if (sc->sc_ccc_ports & (1 << port) 44812feb904SMatthew Dillon data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE); 44912feb904SMatthew Dillon #endif 45012feb904SMatthew Dillon ap->ap_intmask = data; 45112feb904SMatthew Dillon 45212feb904SMatthew Dillon /* 453e8cf3f55SMatthew Dillon * Start the port helper thread. The helper thread will call 454e8cf3f55SMatthew Dillon * ahci_port_init() so the ports can all be started in parallel. 455e8cf3f55SMatthew Dillon * A failure by ahci_port_init() does not deallocate the port 456e8cf3f55SMatthew Dillon * since we still want hot-plug events. 457fd8bd957SMatthew Dillon */ 458f4553de1SMatthew Dillon ahci_os_start_port(ap); 459fd8bd957SMatthew Dillon return(0); 460fd8bd957SMatthew Dillon freeport: 461fd8bd957SMatthew Dillon ahci_port_free(sc, port); 462fd8bd957SMatthew Dillon return (rc); 463fd8bd957SMatthew Dillon } 464fd8bd957SMatthew Dillon 465fd8bd957SMatthew Dillon /* 466492bffafSMatthew Dillon * [re]initialize an idle port. No CCBs should be active. (from port thread) 467fd8bd957SMatthew Dillon * 468fd8bd957SMatthew Dillon * This function is called during the initial port allocation sequence 469fd8bd957SMatthew Dillon * and is also called on hot-plug insertion. We take no chances and 470fd8bd957SMatthew Dillon * use a portreset instead of a softreset. 471fd8bd957SMatthew Dillon * 47222181ab7SMatthew Dillon * This function is the only way to move a failed port back to active 47322181ab7SMatthew Dillon * status. 47422181ab7SMatthew Dillon * 475fd8bd957SMatthew Dillon * Returns 0 if a device is successfully detected. 476fd8bd957SMatthew Dillon */ 477fd8bd957SMatthew Dillon int 47812feb904SMatthew Dillon ahci_port_init(struct ahci_port *ap) 479fd8bd957SMatthew Dillon { 480492bffafSMatthew Dillon u_int32_t cmd; 481e8cf3f55SMatthew Dillon 482e8cf3f55SMatthew Dillon /* 483492bffafSMatthew Dillon * Register [re]initialization 484492bffafSMatthew Dillon * 485f17a0cedSMatthew Dillon * Flush the TFD and SERR and make sure the port is stopped before 486f17a0cedSMatthew Dillon * enabling its interrupt. We no longer cycle the port start as 487f17a0cedSMatthew Dillon * the port should not be started unless a device is present. 488f17a0cedSMatthew Dillon * 489f17a0cedSMatthew Dillon * XXX should we enable FIS reception? (FRE)? 490e8cf3f55SMatthew Dillon */ 491492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 492492bffafSMatthew Dillon ahci_port_stop(ap, 0); 493492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 494492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 495f17a0cedSMatthew Dillon ahci_flush_tfd(ap); 496f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 497492bffafSMatthew Dillon 498492bffafSMatthew Dillon /* 499492bffafSMatthew Dillon * If we are being harsh try to kill the port completely. 500492bffafSMatthew Dillon * 501492bffafSMatthew Dillon * AP_F_HARSH_REINIT is cleared in the hard reset state 502492bffafSMatthew Dillon */ 503492bffafSMatthew Dillon if (ap->ap_flags & AP_F_HARSH_REINIT) { 504492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 505492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 506492bffafSMatthew Dillon 507492bffafSMatthew Dillon ahci_os_sleep(1000); 508492bffafSMatthew Dillon 509492bffafSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 510492bffafSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA); 511492bffafSMatthew Dillon cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | 512492bffafSMatthew Dillon AHCI_PREG_CMD_SUD; 513492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE); 514492bffafSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 515492bffafSMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 516492bffafSMatthew Dillon kprintf("%s: Warning: FRE did not come up during " 517492bffafSMatthew Dillon "harsh reinitialization\n", 518492bffafSMatthew Dillon PORTNAME(ap)); 519492bffafSMatthew Dillon } 520492bffafSMatthew Dillon ahci_os_sleep(1000); 521492bffafSMatthew Dillon } 522492bffafSMatthew Dillon 523492bffafSMatthew Dillon /* 524492bffafSMatthew Dillon * Clear any pending garbage and re-enable the interrupt before 525492bffafSMatthew Dillon * going to the next stage. 526492bffafSMatthew Dillon */ 527492bffafSMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_HARD_RESET; 528492bffafSMatthew Dillon ap->ap_pmcount = 0; 529492bffafSMatthew Dillon 530492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF) 531492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, -1); 532492bffafSMatthew Dillon ahci_flush_tfd(ap); 533492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 534492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, -1); 535492bffafSMatthew Dillon 536f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 537492bffafSMatthew Dillon 53812feb904SMatthew Dillon return (0); 539f4553de1SMatthew Dillon } 540f4553de1SMatthew Dillon 541f4553de1SMatthew Dillon /* 542f4553de1SMatthew Dillon * Enable or re-enable interrupts on a port. 543f4553de1SMatthew Dillon * 544f4553de1SMatthew Dillon * This routine is called from the port initialization code or from the 545f4553de1SMatthew Dillon * helper thread as the real interrupt may be forced to turn off certain 546f4553de1SMatthew Dillon * interrupt sources. 547f4553de1SMatthew Dillon */ 548f4553de1SMatthew Dillon void 549f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap) 550f4553de1SMatthew Dillon { 55112feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask); 5521980eff3SMatthew Dillon } 553258223a3SMatthew Dillon 554fd8bd957SMatthew Dillon /* 555f5caeaa0SMatthew Dillon * Manage the agressive link power management capability. 556f17a0cedSMatthew Dillon */ 557f17a0cedSMatthew Dillon void 558f17a0cedSMatthew Dillon ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt) 559f17a0cedSMatthew Dillon { 560f17a0cedSMatthew Dillon u_int32_t cmd, sctl; 561f17a0cedSMatthew Dillon 562f17a0cedSMatthew Dillon if (link_pwr_mgmt == ap->link_pwr_mgmt) 563f17a0cedSMatthew Dillon return; 564f17a0cedSMatthew Dillon 565f17a0cedSMatthew Dillon if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) { 566f17a0cedSMatthew Dillon kprintf("%s: link power management not supported.\n", 567f17a0cedSMatthew Dillon PORTNAME(ap)); 568f17a0cedSMatthew Dillon return; 569f17a0cedSMatthew Dillon } 570f17a0cedSMatthew Dillon 571f17a0cedSMatthew Dillon ahci_os_lock_port(ap); 572f17a0cedSMatthew Dillon 573f17a0cedSMatthew Dillon if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR && 574f17a0cedSMatthew Dillon (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) { 575f17a0cedSMatthew Dillon kprintf("%s: enabling aggressive link power management.\n", 576f17a0cedSMatthew Dillon PORTNAME(ap)); 577f17a0cedSMatthew Dillon 578795adb22SMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 579795adb22SMatthew Dillon 580f17a0cedSMatthew Dillon ap->ap_intmask &= ~AHCI_PREG_IE_PRCE; 581f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 582f17a0cedSMatthew Dillon 583f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 584f17a0cedSMatthew Dillon sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED); 585f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 586f17a0cedSMatthew Dillon 587795adb22SMatthew Dillon /* 588795adb22SMatthew Dillon * Enable device initiated link power management for 589795adb22SMatthew Dillon * directly attached devices that support it. 590795adb22SMatthew Dillon */ 591795adb22SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM && 592795adb22SMatthew Dillon ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) { 593795adb22SMatthew Dillon if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1)) 594795adb22SMatthew Dillon kprintf("%s: Could not enable device initiated " 595795adb22SMatthew Dillon "link power management.\n", 596795adb22SMatthew Dillon PORTNAME(ap)); 597795adb22SMatthew Dillon } 598795adb22SMatthew Dillon 599f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 600f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ASP; 601f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ALPE; 602f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 603f17a0cedSMatthew Dillon 604f17a0cedSMatthew Dillon } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM && 605f17a0cedSMatthew Dillon (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) { 606f17a0cedSMatthew Dillon kprintf("%s: enabling medium link power management.\n", 607f17a0cedSMatthew Dillon PORTNAME(ap)); 608f17a0cedSMatthew Dillon 609795adb22SMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 610795adb22SMatthew Dillon 611f17a0cedSMatthew Dillon ap->ap_intmask &= ~AHCI_PREG_IE_PRCE; 612f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 613f17a0cedSMatthew Dillon 614f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 615795adb22SMatthew Dillon sctl |= AHCI_PREG_SCTL_IPM_DISABLED; 616795adb22SMatthew Dillon sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL; 617f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 618f17a0cedSMatthew Dillon 619f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 620f17a0cedSMatthew Dillon cmd &= ~AHCI_PREG_CMD_ASP; 621f17a0cedSMatthew Dillon cmd |= AHCI_PREG_CMD_ALPE; 622f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 623f17a0cedSMatthew Dillon 624f17a0cedSMatthew Dillon } else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) { 625f17a0cedSMatthew Dillon kprintf("%s: disabling link power management.\n", 626f17a0cedSMatthew Dillon PORTNAME(ap)); 627f17a0cedSMatthew Dillon 628795adb22SMatthew Dillon /* Disable device initiated link power management */ 629795adb22SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM && 630795adb22SMatthew Dillon ap->ap_ata[0]->at_identify.satafsup & (1 << 3)) 631795adb22SMatthew Dillon ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0); 632795adb22SMatthew Dillon 633f17a0cedSMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 634f17a0cedSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP); 635f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 636f17a0cedSMatthew Dillon 637f17a0cedSMatthew Dillon sctl = ahci_pread(ap, AHCI_PREG_SCTL); 638f17a0cedSMatthew Dillon sctl |= AHCI_PREG_SCTL_IPM_DISABLED; 639f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, sctl); 640f17a0cedSMatthew Dillon 641f17a0cedSMatthew Dillon /* let the drive come back to avoid PRCS interrupts later */ 642f17a0cedSMatthew Dillon ahci_os_unlock_port(ap); 643f17a0cedSMatthew Dillon ahci_os_sleep(1000); 644f17a0cedSMatthew Dillon ahci_os_lock_port(ap); 645f17a0cedSMatthew Dillon 646795adb22SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 647795adb22SMatthew Dillon AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W); 648f17a0cedSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 649f17a0cedSMatthew Dillon 650f17a0cedSMatthew Dillon ap->ap_intmask |= AHCI_PREG_IE_PRCE; 651f17a0cedSMatthew Dillon ahci_port_interrupt_enable(ap); 652f17a0cedSMatthew Dillon 653f17a0cedSMatthew Dillon ap->link_pwr_mgmt = link_pwr_mgmt; 654f17a0cedSMatthew Dillon } else { 655f17a0cedSMatthew Dillon kprintf("%s: unsupported link power management state %d.\n", 656f17a0cedSMatthew Dillon PORTNAME(ap), link_pwr_mgmt); 657f17a0cedSMatthew Dillon } 658f17a0cedSMatthew Dillon 659f17a0cedSMatthew Dillon ahci_os_unlock_port(ap); 660f17a0cedSMatthew Dillon } 661f17a0cedSMatthew Dillon 662795adb22SMatthew Dillon /* 663795adb22SMatthew Dillon * Return current link power state. 664795adb22SMatthew Dillon */ 665795adb22SMatthew Dillon int 666795adb22SMatthew Dillon ahci_port_link_pwr_state(struct ahci_port *ap) 667795adb22SMatthew Dillon { 668795adb22SMatthew Dillon uint32_t r; 669795adb22SMatthew Dillon 670795adb22SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 671795adb22SMatthew Dillon switch (r & SATA_PM_SSTS_IPM) { 672795adb22SMatthew Dillon case SATA_PM_SSTS_IPM_ACTIVE: 673795adb22SMatthew Dillon return 1; 674795adb22SMatthew Dillon case SATA_PM_SSTS_IPM_PARTIAL: 675795adb22SMatthew Dillon return 2; 676795adb22SMatthew Dillon case SATA_PM_SSTS_IPM_SLUMBER: 677795adb22SMatthew Dillon return 3; 678795adb22SMatthew Dillon default: 679795adb22SMatthew Dillon return 0; 680795adb22SMatthew Dillon } 681795adb22SMatthew Dillon } 682f17a0cedSMatthew Dillon 683f17a0cedSMatthew Dillon /* 6843209f581SMatthew Dillon * Run the port / target state machine from a main context. 6853209f581SMatthew Dillon * 6863209f581SMatthew Dillon * The state machine for the port is always run. 6873209f581SMatthew Dillon * 6883209f581SMatthew Dillon * If atx is non-NULL run the state machine for a particular target. 6893209f581SMatthew Dillon * If atx is NULL run the state machine for all targets. 6903209f581SMatthew Dillon */ 6913209f581SMatthew Dillon void 692831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial) 6933209f581SMatthew Dillon { 6943209f581SMatthew Dillon struct ata_port *at; 6953209f581SMatthew Dillon u_int32_t data; 6963209f581SMatthew Dillon int target; 6973209f581SMatthew Dillon int didsleep; 698831bc9e3SMatthew Dillon int loop; 6993209f581SMatthew Dillon 700831bc9e3SMatthew Dillon /* 701831bc9e3SMatthew Dillon * State machine for port. Note that CAM is not yet associated 702831bc9e3SMatthew Dillon * during the initial parallel probe and the port's probe state 703831bc9e3SMatthew Dillon * will not get past ATA_PROBE_NEED_IDENT. 704831bc9e3SMatthew Dillon */ 705c408a8b3SMatthew Dillon { 7061067474aSMatthew Dillon if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) { 7071067474aSMatthew Dillon kprintf("%s: Waiting 10 seconds on insertion\n", 7081067474aSMatthew Dillon PORTNAME(ap)); 7091067474aSMatthew Dillon ahci_os_sleep(10000); 7101067474aSMatthew Dillon initial = 1; 7113209f581SMatthew Dillon } 7121067474aSMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT) 71312feb904SMatthew Dillon ahci_port_init(ap); 7143209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) 7153209f581SMatthew Dillon ahci_port_reset(ap, NULL, 1); 7163209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET) 7173209f581SMatthew Dillon ahci_port_reset(ap, NULL, 0); 7183209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_IDENT) 7193209f581SMatthew Dillon ahci_cam_probe(ap, NULL); 7203209f581SMatthew Dillon } 7213209f581SMatthew Dillon if (ap->ap_type != ATA_PORT_T_PM) { 7223209f581SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 7233209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 0); 724f4553de1SMatthew Dillon } else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) { 7253209f581SMatthew Dillon ahci_cam_changed(ap, NULL, 1); 7263209f581SMatthew Dillon } 7273209f581SMatthew Dillon return; 7283209f581SMatthew Dillon } 7293209f581SMatthew Dillon 730831bc9e3SMatthew Dillon /* 731831bc9e3SMatthew Dillon * Port Multiplier state machine. 732831bc9e3SMatthew Dillon * 733831bc9e3SMatthew Dillon * Get a mask of changed targets and combine with any runnable 734831bc9e3SMatthew Dillon * states already present. 735831bc9e3SMatthew Dillon */ 736831bc9e3SMatthew Dillon for (loop = 0; ;++loop) { 7372cc2e845SMatthew Dillon if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) { 7383209f581SMatthew Dillon kprintf("%s: PM unable to read hot-plug bitmap\n", 7393209f581SMatthew Dillon PORTNAME(ap)); 7403209f581SMatthew Dillon break; 7413209f581SMatthew Dillon } 7423209f581SMatthew Dillon 7433209f581SMatthew Dillon /* 744831bc9e3SMatthew Dillon * Do at least one loop, then stop if no more state changes 745831bc9e3SMatthew Dillon * have occured. The PM might not generate a new 746831bc9e3SMatthew Dillon * notification until we clear the entire bitmap. 7473209f581SMatthew Dillon */ 748831bc9e3SMatthew Dillon if (loop && data == 0) 7493209f581SMatthew Dillon break; 7503209f581SMatthew Dillon 7513209f581SMatthew Dillon /* 7523209f581SMatthew Dillon * New devices showing up in the bitmap require some spin-up 7533209f581SMatthew Dillon * time before we start probing them. Reset didsleep. The 7543209f581SMatthew Dillon * first new device we detect will sleep before probing. 755831bc9e3SMatthew Dillon * 756831bc9e3SMatthew Dillon * This only applies to devices whos change bit is set in 757831bc9e3SMatthew Dillon * the data, and does not apply to the initial boot-time 758831bc9e3SMatthew Dillon * probe. 7593209f581SMatthew Dillon */ 7603209f581SMatthew Dillon didsleep = 0; 7613209f581SMatthew Dillon 7623209f581SMatthew Dillon for (target = 0; target < ap->ap_pmcount; ++target) { 763b012a2caSMatthew Dillon at = ap->ap_ata[target]; 7643209f581SMatthew Dillon 7653209f581SMatthew Dillon /* 7663209f581SMatthew Dillon * Check the target state for targets behind the PM 7673209f581SMatthew Dillon * which have changed state. This will adjust 7683209f581SMatthew Dillon * at_probe and set ATA_PORT_F_RESCAN 7693209f581SMatthew Dillon * 7701067474aSMatthew Dillon * We want to wait at least 10 seconds before probing 7713209f581SMatthew Dillon * a newly inserted device. If the check status 7723209f581SMatthew Dillon * indicates a device is present and in need of a 7733209f581SMatthew Dillon * hard reset, we make sure we have slept before 7743209f581SMatthew Dillon * continuing. 775831bc9e3SMatthew Dillon * 7761067474aSMatthew Dillon * We also need to wait at least 1 second for the 7771067474aSMatthew Dillon * PHY state to change after insertion, if we 7781067474aSMatthew Dillon * haven't already waited the 10 seconds. 7791067474aSMatthew Dillon * 780831bc9e3SMatthew Dillon * NOTE: When pm_check_good finds a good port it 781831bc9e3SMatthew Dillon * typically starts us in probe state 782831bc9e3SMatthew Dillon * NEED_HARD_RESET rather than INIT. 7833209f581SMatthew Dillon */ 7843209f581SMatthew Dillon if (data & (1 << target)) { 7851067474aSMatthew Dillon if (initial == 0 && didsleep == 0) 7861067474aSMatthew Dillon ahci_os_sleep(1000); 7873209f581SMatthew Dillon ahci_pm_check_good(ap, target); 788831bc9e3SMatthew Dillon if (initial == 0 && didsleep == 0 && 789831bc9e3SMatthew Dillon at->at_probe <= ATA_PROBE_NEED_HARD_RESET 790831bc9e3SMatthew Dillon ) { 7913209f581SMatthew Dillon didsleep = 1; 792121d8e75SMatthew Dillon kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap)); 793121d8e75SMatthew Dillon ahci_os_sleep(10000); 7943209f581SMatthew Dillon } 7953209f581SMatthew Dillon } 796831bc9e3SMatthew Dillon 797831bc9e3SMatthew Dillon /* 798831bc9e3SMatthew Dillon * Report hot-plug events before the probe state 799831bc9e3SMatthew Dillon * really gets hot. Only actual events are reported 800831bc9e3SMatthew Dillon * here to reduce spew. 801831bc9e3SMatthew Dillon */ 802831bc9e3SMatthew Dillon if (data & (1 << target)) { 803831bc9e3SMatthew Dillon kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at)); 804831bc9e3SMatthew Dillon switch(at->at_probe) { 805831bc9e3SMatthew Dillon case ATA_PROBE_NEED_INIT: 806831bc9e3SMatthew Dillon case ATA_PROBE_NEED_HARD_RESET: 807831bc9e3SMatthew Dillon kprintf("Device inserted\n"); 808831bc9e3SMatthew Dillon break; 809831bc9e3SMatthew Dillon case ATA_PROBE_FAILED: 810831bc9e3SMatthew Dillon kprintf("Device removed\n"); 811831bc9e3SMatthew Dillon break; 812831bc9e3SMatthew Dillon default: 813831bc9e3SMatthew Dillon kprintf("Device probe in progress\n"); 814831bc9e3SMatthew Dillon break; 815831bc9e3SMatthew Dillon } 8163209f581SMatthew Dillon } 8173209f581SMatthew Dillon 8183209f581SMatthew Dillon /* 819831bc9e3SMatthew Dillon * Run through the state machine as necessary if 820831bc9e3SMatthew Dillon * the port is not marked failed. 821831bc9e3SMatthew Dillon * 822831bc9e3SMatthew Dillon * The state machine may stop at NEED_IDENT if 823831bc9e3SMatthew Dillon * CAM is not yet attached. 824831bc9e3SMatthew Dillon * 825831bc9e3SMatthew Dillon * Acquire exclusive access to the port while we 826831bc9e3SMatthew Dillon * are doing this. This prevents command-completion 827831bc9e3SMatthew Dillon * from queueing commands for non-polled targets 828831bc9e3SMatthew Dillon * inbetween our probe steps. We need to do this 829831bc9e3SMatthew Dillon * because the reset probes can generate severe PHY 830831bc9e3SMatthew Dillon * and protocol errors and soft-brick the port. 8313209f581SMatthew Dillon */ 832831bc9e3SMatthew Dillon if (at->at_probe != ATA_PROBE_FAILED && 833831bc9e3SMatthew Dillon at->at_probe != ATA_PROBE_GOOD) { 834831bc9e3SMatthew Dillon ahci_beg_exclusive_access(ap, at); 8353209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_INIT) 83612feb904SMatthew Dillon ahci_pm_port_init(ap, at); 8373209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) 8383209f581SMatthew Dillon ahci_port_reset(ap, at, 1); 8393209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET) 8403209f581SMatthew Dillon ahci_port_reset(ap, at, 0); 8413209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_NEED_IDENT) 8423209f581SMatthew Dillon ahci_cam_probe(ap, at); 843831bc9e3SMatthew Dillon ahci_end_exclusive_access(ap, at); 8443209f581SMatthew Dillon } 8453209f581SMatthew Dillon 8463209f581SMatthew Dillon /* 847831bc9e3SMatthew Dillon * Add or remove from CAM 8483209f581SMatthew Dillon */ 8493209f581SMatthew Dillon if (at->at_features & ATA_PORT_F_RESCAN) { 8503209f581SMatthew Dillon at->at_features &= ~ATA_PORT_F_RESCAN; 8513209f581SMatthew Dillon if (at->at_probe == ATA_PROBE_FAILED) { 8523209f581SMatthew Dillon ahci_cam_changed(ap, at, 0); 853f4553de1SMatthew Dillon } else if (at->at_probe >= ATA_PROBE_NEED_IDENT) { 8543209f581SMatthew Dillon ahci_cam_changed(ap, at, 1); 8553209f581SMatthew Dillon } 8563209f581SMatthew Dillon } 8573560ed94SMatthew Dillon data &= ~(1 << target); 8583560ed94SMatthew Dillon } 8593560ed94SMatthew Dillon if (data) { 8603560ed94SMatthew Dillon kprintf("%s: WARNING (PM): extra bits set in " 8613560ed94SMatthew Dillon "EINFO: %08x\n", PORTNAME(ap), data); 8623560ed94SMatthew Dillon while (target < AHCI_MAX_PMPORTS) { 8633560ed94SMatthew Dillon ahci_pm_check_good(ap, target); 8643560ed94SMatthew Dillon ++target; 8653560ed94SMatthew Dillon } 8663209f581SMatthew Dillon } 8673209f581SMatthew Dillon } 8683209f581SMatthew Dillon } 8693209f581SMatthew Dillon 8703209f581SMatthew Dillon 8713209f581SMatthew Dillon /* 872fd8bd957SMatthew Dillon * De-initialize and detach a port. 873fd8bd957SMatthew Dillon */ 874258223a3SMatthew Dillon void 875258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port) 876258223a3SMatthew Dillon { 877258223a3SMatthew Dillon struct ahci_port *ap = sc->sc_ports[port]; 878258223a3SMatthew Dillon struct ahci_ccb *ccb; 879b012a2caSMatthew Dillon int i; 880258223a3SMatthew Dillon 88117eab71eSMatthew Dillon /* 88217eab71eSMatthew Dillon * Ensure port is disabled and its interrupts are all flushed. 88317eab71eSMatthew Dillon */ 884258223a3SMatthew Dillon if (ap->ap_sc) { 88517eab71eSMatthew Dillon ahci_port_stop(ap, 1); 886f4553de1SMatthew Dillon ahci_os_stop_port(ap); 887258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, 0); 888258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 889258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS)); 890258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, 1 << port); 891258223a3SMatthew Dillon } 892258223a3SMatthew Dillon 893258223a3SMatthew Dillon if (ap->ap_ccbs) { 894258223a3SMatthew Dillon while ((ccb = ahci_get_ccb(ap)) != NULL) { 895258223a3SMatthew Dillon if (ccb->ccb_dmamap) { 896258223a3SMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 897258223a3SMatthew Dillon ccb->ccb_dmamap); 898258223a3SMatthew Dillon ccb->ccb_dmamap = NULL; 899258223a3SMatthew Dillon } 900258223a3SMatthew Dillon } 9011067474aSMatthew Dillon if ((ccb = ap->ap_err_ccb) != NULL) { 9021067474aSMatthew Dillon if (ccb->ccb_dmamap) { 9031067474aSMatthew Dillon bus_dmamap_destroy(sc->sc_tag_data, 9041067474aSMatthew Dillon ccb->ccb_dmamap); 9051067474aSMatthew Dillon ccb->ccb_dmamap = NULL; 9061067474aSMatthew Dillon } 9071067474aSMatthew Dillon ap->ap_err_ccb = NULL; 9081067474aSMatthew Dillon } 909258223a3SMatthew Dillon kfree(ap->ap_ccbs, M_DEVBUF); 910258223a3SMatthew Dillon ap->ap_ccbs = NULL; 911258223a3SMatthew Dillon } 912258223a3SMatthew Dillon 913258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_list) { 914258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list); 915258223a3SMatthew Dillon ap->ap_dmamem_cmd_list = NULL; 916258223a3SMatthew Dillon } 917258223a3SMatthew Dillon if (ap->ap_dmamem_rfis) { 918258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_rfis); 919258223a3SMatthew Dillon ap->ap_dmamem_rfis = NULL; 920258223a3SMatthew Dillon } 921258223a3SMatthew Dillon if (ap->ap_dmamem_cmd_table) { 922258223a3SMatthew Dillon ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table); 923258223a3SMatthew Dillon ap->ap_dmamem_cmd_table = NULL; 924258223a3SMatthew Dillon } 9251980eff3SMatthew Dillon if (ap->ap_ata) { 926b012a2caSMatthew Dillon for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { 927b012a2caSMatthew Dillon if (ap->ap_ata[i]) { 928b012a2caSMatthew Dillon kfree(ap->ap_ata[i], M_DEVBUF); 929b012a2caSMatthew Dillon ap->ap_ata[i] = NULL; 930b012a2caSMatthew Dillon } 931b012a2caSMatthew Dillon } 9321980eff3SMatthew Dillon } 93312feb904SMatthew Dillon if (ap->ap_err_scratch) { 93412feb904SMatthew Dillon kfree(ap->ap_err_scratch, M_DEVBUF); 93512feb904SMatthew Dillon ap->ap_err_scratch = NULL; 93612feb904SMatthew Dillon } 937258223a3SMatthew Dillon 938258223a3SMatthew Dillon /* bus_space(9) says we dont free the subregions handle */ 939258223a3SMatthew Dillon 940258223a3SMatthew Dillon kfree(ap, M_DEVBUF); 941258223a3SMatthew Dillon sc->sc_ports[port] = NULL; 942258223a3SMatthew Dillon } 943258223a3SMatthew Dillon 944492bffafSMatthew Dillon static 945492bffafSMatthew Dillon u_int32_t 946492bffafSMatthew Dillon ahci_pactive(struct ahci_port *ap) 947492bffafSMatthew Dillon { 948492bffafSMatthew Dillon u_int32_t mask; 949492bffafSMatthew Dillon 950492bffafSMatthew Dillon mask = ahci_pread(ap, AHCI_PREG_CI); 951492bffafSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) 952492bffafSMatthew Dillon mask |= ahci_pread(ap, AHCI_PREG_SACT); 953492bffafSMatthew Dillon return(mask); 954492bffafSMatthew Dillon } 955492bffafSMatthew Dillon 956fd8bd957SMatthew Dillon /* 957fd8bd957SMatthew Dillon * Start high-level command processing on the port 958fd8bd957SMatthew Dillon */ 959258223a3SMatthew Dillon int 96017eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap) 961258223a3SMatthew Dillon { 96212feb904SMatthew Dillon u_int32_t r, s, is, tfd; 963258223a3SMatthew Dillon 96417eab71eSMatthew Dillon /* 96517eab71eSMatthew Dillon * FRE must be turned on before ST. Wait for FR to go active 96617eab71eSMatthew Dillon * before turning on ST. The spec doesn't seem to think this 96717eab71eSMatthew Dillon * is necessary but waiting here avoids an on-off race in the 96817eab71eSMatthew Dillon * ahci_port_stop() code. 96917eab71eSMatthew Dillon */ 97012feb904SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD); 97117eab71eSMatthew Dillon if ((r & AHCI_PREG_CMD_FRE) == 0) { 972258223a3SMatthew Dillon r |= AHCI_PREG_CMD_FRE; 97317eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 97417eab71eSMatthew Dillon } 97517eab71eSMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) { 97617eab71eSMatthew Dillon if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 97717eab71eSMatthew Dillon kprintf("%s: Cannot start FIS reception\n", 97817eab71eSMatthew Dillon PORTNAME(ap)); 97917eab71eSMatthew Dillon return (2); 98017eab71eSMatthew Dillon } 981f17a0cedSMatthew Dillon } else { 982f17a0cedSMatthew Dillon ahci_os_sleep(10); 98317eab71eSMatthew Dillon } 98417eab71eSMatthew Dillon 98517eab71eSMatthew Dillon /* 98617eab71eSMatthew Dillon * Turn on ST, wait for CR to come up. 98717eab71eSMatthew Dillon */ 988258223a3SMatthew Dillon r |= AHCI_PREG_CMD_ST; 989258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 990f17a0cedSMatthew Dillon if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 9918bf6a3ffSMatthew Dillon s = ahci_pread(ap, AHCI_PREG_SERR); 9928bf6a3ffSMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 9938bf6a3ffSMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 9941980eff3SMatthew Dillon kprintf("%s: Cannot start command DMA\n" 9951980eff3SMatthew Dillon "NCMP=%b NSERR=%b\n" 99612feb904SMatthew Dillon "NEWIS=%b\n" 99712feb904SMatthew Dillon "NEWTFD=%b\n", 9981980eff3SMatthew Dillon PORTNAME(ap), 9991980eff3SMatthew Dillon r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR, 100012feb904SMatthew Dillon is, AHCI_PFMT_IS, 100112feb904SMatthew Dillon tfd, AHCI_PFMT_TFD_STS); 100217eab71eSMatthew Dillon return (1); 100317eab71eSMatthew Dillon } 1004258223a3SMatthew Dillon 1005258223a3SMatthew Dillon #ifdef AHCI_COALESCE 100617eab71eSMatthew Dillon /* 100717eab71eSMatthew Dillon * (Re-)enable coalescing on the port. 100817eab71eSMatthew Dillon */ 1009258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 1010258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num); 1011258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 1012258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 1013258223a3SMatthew Dillon } 1014258223a3SMatthew Dillon #endif 1015258223a3SMatthew Dillon 1016258223a3SMatthew Dillon return (0); 1017258223a3SMatthew Dillon } 1018258223a3SMatthew Dillon 1019fd8bd957SMatthew Dillon /* 1020fd8bd957SMatthew Dillon * Stop high-level command processing on a port 10214c339a5fSMatthew Dillon * 10224c339a5fSMatthew Dillon * WARNING! If the port is stopped while CR is still active our saved 10234c339a5fSMatthew Dillon * CI/SACT will race any commands completed by the command 10244c339a5fSMatthew Dillon * processor prior to being able to stop. Thus we never call 10254c339a5fSMatthew Dillon * this function unless we intend to dispose of any remaining 10264c339a5fSMatthew Dillon * active commands. In particular, this complicates the timeout 10274c339a5fSMatthew Dillon * code. 1028fd8bd957SMatthew Dillon */ 1029258223a3SMatthew Dillon int 1030258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx) 1031258223a3SMatthew Dillon { 1032258223a3SMatthew Dillon u_int32_t r; 1033258223a3SMatthew Dillon 1034258223a3SMatthew Dillon #ifdef AHCI_COALESCE 103517eab71eSMatthew Dillon /* 103617eab71eSMatthew Dillon * Disable coalescing on the port while it is stopped. 103717eab71eSMatthew Dillon */ 1038258223a3SMatthew Dillon if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) { 1039258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num); 1040258223a3SMatthew Dillon ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS, 1041258223a3SMatthew Dillon ap->ap_sc->sc_ccc_ports_cur); 1042258223a3SMatthew Dillon } 1043258223a3SMatthew Dillon #endif 1044258223a3SMatthew Dillon 104517eab71eSMatthew Dillon /* 104617eab71eSMatthew Dillon * Turn off ST, then wait for CR to go off. 104717eab71eSMatthew Dillon */ 1048258223a3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1049258223a3SMatthew Dillon r &= ~AHCI_PREG_CMD_ST; 1050258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 1051258223a3SMatthew Dillon 105217eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) { 105317eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (ST)\n", 105417eab71eSMatthew Dillon PORTNAME(ap)); 1055258223a3SMatthew Dillon return (1); 105617eab71eSMatthew Dillon } 1057258223a3SMatthew Dillon 10581980eff3SMatthew Dillon #if 0 105917eab71eSMatthew Dillon /* 106017eab71eSMatthew Dillon * Turn off FRE, then wait for FR to go off. FRE cannot 106117eab71eSMatthew Dillon * be turned off until CR transitions to 0. 106217eab71eSMatthew Dillon */ 10631980eff3SMatthew Dillon if ((r & AHCI_PREG_CMD_FR) == 0) { 10641980eff3SMatthew Dillon kprintf("%s: FR stopped, clear FRE for next start\n", 10651980eff3SMatthew Dillon PORTNAME(ap)); 10661980eff3SMatthew Dillon stop_fis_rx = 2; 10671980eff3SMatthew Dillon } 10681980eff3SMatthew Dillon #endif 106917eab71eSMatthew Dillon if (stop_fis_rx) { 107017eab71eSMatthew Dillon r &= ~AHCI_PREG_CMD_FRE; 107117eab71eSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, r); 107217eab71eSMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) { 107317eab71eSMatthew Dillon kprintf("%s: Port bricked, unable to stop (FRE)\n", 107417eab71eSMatthew Dillon PORTNAME(ap)); 1075258223a3SMatthew Dillon return (2); 107617eab71eSMatthew Dillon } 107717eab71eSMatthew Dillon } 1078258223a3SMatthew Dillon 1079258223a3SMatthew Dillon return (0); 1080258223a3SMatthew Dillon } 1081258223a3SMatthew Dillon 1082fd8bd957SMatthew Dillon /* 1083fd8bd957SMatthew Dillon * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ} 1084fd8bd957SMatthew Dillon */ 1085258223a3SMatthew Dillon int 1086258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap) 1087258223a3SMatthew Dillon { 1088258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1089258223a3SMatthew Dillon u_int32_t cmd; 1090258223a3SMatthew Dillon 1091258223a3SMatthew Dillon /* Only attempt CLO if supported by controller */ 1092258223a3SMatthew Dillon if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0) 1093258223a3SMatthew Dillon return (1); 1094258223a3SMatthew Dillon 1095258223a3SMatthew Dillon /* Issue CLO */ 1096258223a3SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1097258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO); 1098258223a3SMatthew Dillon 1099258223a3SMatthew Dillon /* Wait for completion */ 1100258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) { 1101258223a3SMatthew Dillon kprintf("%s: CLO did not complete\n", PORTNAME(ap)); 1102258223a3SMatthew Dillon return (1); 1103258223a3SMatthew Dillon } 1104258223a3SMatthew Dillon 1105258223a3SMatthew Dillon return (0); 1106258223a3SMatthew Dillon } 1107258223a3SMatthew Dillon 1108fd8bd957SMatthew Dillon /* 11091980eff3SMatthew Dillon * Reset a port. 111017eab71eSMatthew Dillon * 11111980eff3SMatthew Dillon * If hard is 0 perform a softreset of the port. 111217eab71eSMatthew Dillon * If hard is 1 perform a hard reset of the port. 11131980eff3SMatthew Dillon * 11141980eff3SMatthew Dillon * If at is non-NULL an indirect port via a port-multiplier is being 11151980eff3SMatthew Dillon * reset, otherwise a direct port is being reset. 11161980eff3SMatthew Dillon * 11171980eff3SMatthew Dillon * NOTE: Indirect ports can only be soft-reset. 111817eab71eSMatthew Dillon */ 111917eab71eSMatthew Dillon int 11201980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard) 112117eab71eSMatthew Dillon { 112217eab71eSMatthew Dillon int rc; 112317eab71eSMatthew Dillon 112417eab71eSMatthew Dillon if (hard) { 11251980eff3SMatthew Dillon if (at) 11261980eff3SMatthew Dillon rc = ahci_pm_hardreset(ap, at->at_target, hard); 11271980eff3SMatthew Dillon else 11281980eff3SMatthew Dillon rc = ahci_port_hardreset(ap, hard); 112917eab71eSMatthew Dillon } else { 11301980eff3SMatthew Dillon if (at) 11311980eff3SMatthew Dillon rc = ahci_pm_softreset(ap, at->at_target); 11321980eff3SMatthew Dillon else 113317eab71eSMatthew Dillon rc = ahci_port_softreset(ap); 113417eab71eSMatthew Dillon } 113517eab71eSMatthew Dillon return(rc); 113617eab71eSMatthew Dillon } 113717eab71eSMatthew Dillon 113817eab71eSMatthew Dillon /* 1139fd8bd957SMatthew Dillon * AHCI soft reset, Section 10.4.1 1140fd8bd957SMatthew Dillon * 11411980eff3SMatthew Dillon * (at) will be NULL when soft-resetting a directly-attached device, and 11421980eff3SMatthew Dillon * non-NULL when soft-resetting a device through a port multiplier. 11431980eff3SMatthew Dillon * 1144fd8bd957SMatthew Dillon * This function keeps port communications intact and attempts to generate 11451980eff3SMatthew Dillon * a reset to the connected device using device commands. 1146fd8bd957SMatthew Dillon */ 1147258223a3SMatthew Dillon int 1148258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap) 1149258223a3SMatthew Dillon { 1150258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 1151258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 1152258223a3SMatthew Dillon u_int8_t *fis; 11533209f581SMatthew Dillon int error; 1154258223a3SMatthew Dillon 11553209f581SMatthew Dillon error = EIO; 11561980eff3SMatthew Dillon 1157074579dfSMatthew Dillon if (bootverbose) { 11581980eff3SMatthew Dillon kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap), 11591980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD); 1160074579dfSMatthew Dillon } 11611980eff3SMatthew Dillon 1162258223a3SMatthew Dillon DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap)); 1163258223a3SMatthew Dillon 1164258223a3SMatthew Dillon crit_enter(); 11651980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 11661980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 1167258223a3SMatthew Dillon 11681980eff3SMatthew Dillon /* 11691980eff3SMatthew Dillon * Remember port state in cmd (main to restore start/stop) 11701980eff3SMatthew Dillon * 11711980eff3SMatthew Dillon * Idle port. 11721980eff3SMatthew Dillon */ 1173258223a3SMatthew Dillon if (ahci_port_stop(ap, 0)) { 1174258223a3SMatthew Dillon kprintf("%s: failed to stop port, cannot softreset\n", 1175258223a3SMatthew Dillon PORTNAME(ap)); 1176258223a3SMatthew Dillon goto err; 1177258223a3SMatthew Dillon } 1178cf5f3a81SMatthew Dillon 1179cf5f3a81SMatthew Dillon /* 11801980eff3SMatthew Dillon * Request CLO if device appears hung. 1181cf5f3a81SMatthew Dillon */ 1182258223a3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 1183258223a3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1184258223a3SMatthew Dillon ahci_port_clo(ap); 1185258223a3SMatthew Dillon } 1186258223a3SMatthew Dillon 11871980eff3SMatthew Dillon /* 11881980eff3SMatthew Dillon * This is an attempt to clear errors so a new signature will 11891980eff3SMatthew Dillon * be latched. It isn't working properly. XXX 11901980eff3SMatthew Dillon */ 1191cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 11921980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1193258223a3SMatthew Dillon 1194258223a3SMatthew Dillon /* Restart port */ 119517eab71eSMatthew Dillon if (ahci_port_start(ap)) { 1196258223a3SMatthew Dillon kprintf("%s: failed to start port, cannot softreset\n", 1197258223a3SMatthew Dillon PORTNAME(ap)); 1198258223a3SMatthew Dillon goto err; 1199258223a3SMatthew Dillon } 1200258223a3SMatthew Dillon 1201258223a3SMatthew Dillon /* Check whether CLO worked */ 1202258223a3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 1203258223a3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1204258223a3SMatthew Dillon kprintf("%s: CLO %s, need port reset\n", 1205258223a3SMatthew Dillon PORTNAME(ap), 1206258223a3SMatthew Dillon (ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) 1207258223a3SMatthew Dillon ? "failed" : "unsupported"); 12083209f581SMatthew Dillon error = EBUSY; 1209258223a3SMatthew Dillon goto err; 1210258223a3SMatthew Dillon } 1211258223a3SMatthew Dillon 1212cec85a37SMatthew Dillon /* 1213cec85a37SMatthew Dillon * Prep first D2H command with SRST feature & clear busy/reset flags 1214cec85a37SMatthew Dillon * 1215cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1216cec85a37SMatthew Dillon * everything. 12171067474aSMatthew Dillon * 12181067474aSMatthew Dillon * NOTE! This CCB is used for both the first and second commands. 12191067474aSMatthew Dillon * The second command must use CCB slot 1 to properly load 12201067474aSMatthew Dillon * the signature. 1221cec85a37SMatthew Dillon */ 1222258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 122312feb904SMatthew Dillon ccb->ccb_xa.complete = ahci_dummy_done; 122412feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE; 12251067474aSMatthew Dillon KKASSERT(ccb->ccb_slot == 1); 12261980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 1227258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 1228258223a3SMatthew Dillon 1229258223a3SMatthew Dillon fis = ccb->ccb_cmd_table->cfis; 1230cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 12311980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 12321980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT; 1233258223a3SMatthew Dillon 1234258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1235258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1236258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */ 1237258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */ 1238258223a3SMatthew Dillon 1239258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 124012feb904SMatthew Dillon 1241831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 12425f8c1efdSMatthew Dillon kprintf("%s: First FIS failed\n", PORTNAME(ap)); 1243258223a3SMatthew Dillon goto err; 1244cec85a37SMatthew Dillon } 1245258223a3SMatthew Dillon 1246cec85a37SMatthew Dillon /* 1247831bc9e3SMatthew Dillon * WARNING! TIME SENSITIVE SPACE! WARNING! 1248831bc9e3SMatthew Dillon * 1249831bc9e3SMatthew Dillon * The two FISes are supposed to be back to back. Don't issue other 1250831bc9e3SMatthew Dillon * commands or even delay if we can help it. 12511980eff3SMatthew Dillon */ 12521980eff3SMatthew Dillon 12531980eff3SMatthew Dillon /* 1254cec85a37SMatthew Dillon * Prep second D2H command to read status and complete reset sequence 1255cec85a37SMatthew Dillon * AHCI 10.4.1 and "Serial ATA Revision 2.6". I can't find the ATA 1256cec85a37SMatthew Dillon * Rev 2.6 and it is unclear how the second FIS should be set up 1257cec85a37SMatthew Dillon * from the AHCI document. 1258cec85a37SMatthew Dillon * 1259b089d0bfSMatthew Dillon * Give the device 3ms before sending the second FIS. 1260cec85a37SMatthew Dillon * 1261cec85a37SMatthew Dillon * It is unclear which other fields in the FIS are used. Just zero 1262cec85a37SMatthew Dillon * everything. 1263cec85a37SMatthew Dillon */ 126412feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE; 126512feb904SMatthew Dillon 1266cec85a37SMatthew Dillon bzero(fis, sizeof(ccb->ccb_cmd_table->cfis)); 12671980eff3SMatthew Dillon fis[0] = ATA_FIS_TYPE_H2D; 12681980eff3SMatthew Dillon fis[15] = ATA_FIS_CONTROL_4BIT; 1269258223a3SMatthew Dillon 1270258223a3SMatthew Dillon cmd_slot->prdtl = 0; 1271258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 1272258223a3SMatthew Dillon 1273258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 1274831bc9e3SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 12755f8c1efdSMatthew Dillon kprintf("%s: Second FIS failed\n", PORTNAME(ap)); 1276258223a3SMatthew Dillon goto err; 1277cec85a37SMatthew Dillon } 1278258223a3SMatthew Dillon 12791980eff3SMatthew Dillon if (ahci_pwait_clr(ap, AHCI_PREG_TFD, 12801980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 1281258223a3SMatthew Dillon kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n", 1282258223a3SMatthew Dillon PORTNAME(ap), 1283258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS); 12843209f581SMatthew Dillon error = EBUSY; 1285258223a3SMatthew Dillon goto err; 1286258223a3SMatthew Dillon } 12873209f581SMatthew Dillon ahci_os_sleep(10); 1288258223a3SMatthew Dillon 1289fd8bd957SMatthew Dillon /* 1290fd8bd957SMatthew Dillon * If the softreset is trying to clear a BSY condition after a 1291fd8bd957SMatthew Dillon * normal portreset we assign the port type. 1292fd8bd957SMatthew Dillon * 1293fd8bd957SMatthew Dillon * If the softreset is being run first as part of the ccb error 1294fd8bd957SMatthew Dillon * processing code then report if the device signature changed 1295fd8bd957SMatthew Dillon * unexpectedly. 1296fd8bd957SMatthew Dillon */ 12971980eff3SMatthew Dillon if (ap->ap_type == ATA_PORT_T_NONE) { 12981980eff3SMatthew Dillon ap->ap_type = ahci_port_signature_detect(ap, NULL); 1299fd8bd957SMatthew Dillon } else { 13001980eff3SMatthew Dillon if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) { 13011980eff3SMatthew Dillon kprintf("%s: device signature unexpectedly " 13021980eff3SMatthew Dillon "changed\n", PORTNAME(ap)); 13033209f581SMatthew Dillon error = EBUSY; /* XXX */ 1304fd8bd957SMatthew Dillon } 1305fd8bd957SMatthew Dillon } 13063209f581SMatthew Dillon error = 0; 13071980eff3SMatthew Dillon 13083209f581SMatthew Dillon ahci_os_sleep(3); 1309258223a3SMatthew Dillon err: 1310258223a3SMatthew Dillon if (ccb != NULL) { 1311258223a3SMatthew Dillon ahci_put_err_ccb(ccb); 13121980eff3SMatthew Dillon 13131980eff3SMatthew Dillon /* 13141980eff3SMatthew Dillon * If the target is busy use CLO to clear the busy 13151980eff3SMatthew Dillon * condition. The BSY should be cleared on the next 13161980eff3SMatthew Dillon * start. 13171980eff3SMatthew Dillon */ 13181980eff3SMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 13191980eff3SMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 13201980eff3SMatthew Dillon ahci_port_clo(ap); 13211980eff3SMatthew Dillon } 1322258223a3SMatthew Dillon } 1323258223a3SMatthew Dillon 1324cf5f3a81SMatthew Dillon /* 1325cf5f3a81SMatthew Dillon * If we failed to softreset make the port quiescent, otherwise 1326cf5f3a81SMatthew Dillon * make sure the port's start/stop state matches what it was on 1327cf5f3a81SMatthew Dillon * entry. 13281980eff3SMatthew Dillon * 13291980eff3SMatthew Dillon * Don't kill the port if the softreset is on a port multiplier 13301980eff3SMatthew Dillon * target, that would kill all the targets! 1331cf5f3a81SMatthew Dillon */ 13323209f581SMatthew Dillon if (error) { 1333cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 13343209f581SMatthew Dillon /* ap_probe set to failed */ 1335cf5f3a81SMatthew Dillon } else { 13363209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_IDENT; 133712feb904SMatthew Dillon ap->ap_pmcount = 1; 13384c339a5fSMatthew Dillon ahci_port_start(ap); 1339cf5f3a81SMatthew Dillon } 13403209f581SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 1341258223a3SMatthew Dillon crit_exit(); 1342258223a3SMatthew Dillon 1343074579dfSMatthew Dillon if (bootverbose) 13441980eff3SMatthew Dillon kprintf("%s: END SOFTRESET\n", PORTNAME(ap)); 13451980eff3SMatthew Dillon 13463209f581SMatthew Dillon return (error); 1347258223a3SMatthew Dillon } 1348258223a3SMatthew Dillon 1349fd8bd957SMatthew Dillon /* 1350fd8bd957SMatthew Dillon * AHCI port reset, Section 10.4.2 1351fd8bd957SMatthew Dillon * 1352fd8bd957SMatthew Dillon * This function does a hard reset of the port. Note that the device 1353fd8bd957SMatthew Dillon * connected to the port could still end-up hung. 1354fd8bd957SMatthew Dillon */ 1355258223a3SMatthew Dillon int 13561980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard) 1357258223a3SMatthew Dillon { 1358258223a3SMatthew Dillon u_int32_t cmd, r; 135912feb904SMatthew Dillon u_int32_t data; 13603209f581SMatthew Dillon int error; 13611980eff3SMatthew Dillon int loop; 1362258223a3SMatthew Dillon 136312feb904SMatthew Dillon if (bootverbose) 136412feb904SMatthew Dillon kprintf("%s: START HARDRESET\n", PORTNAME(ap)); 13651980eff3SMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 1366cf5f3a81SMatthew Dillon 1367cf5f3a81SMatthew Dillon /* 13681980eff3SMatthew Dillon * Idle the port, 13691980eff3SMatthew Dillon */ 13701980eff3SMatthew Dillon ahci_port_stop(ap, 0); 13711980eff3SMatthew Dillon ap->ap_state = AP_S_NORMAL; 13721980eff3SMatthew Dillon 13731980eff3SMatthew Dillon /* 13741980eff3SMatthew Dillon * The port may have been quiescent with its SUD bit cleared, so 13751980eff3SMatthew Dillon * set the SUD (spin up device). 1376cf5f3a81SMatthew Dillon */ 1377cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC; 1378cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_SUD; 1379cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1380258223a3SMatthew Dillon 13811980eff3SMatthew Dillon /* 13824e21f4daSMatthew Dillon * Perform device detection. 13831067474aSMatthew Dillon * 13844e21f4daSMatthew Dillon * NOTE! AHCi_PREG_SCTL_DET_DISABLE seems to be highly unreliable 13854e21f4daSMatthew Dillon * on multiple chipsets and can brick the chipset or even 13864e21f4daSMatthew Dillon * the whole PC. Never use it. 13871980eff3SMatthew Dillon */ 13881980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1389258223a3SMatthew Dillon 13901980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED; 13911980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 13923209f581SMatthew Dillon ahci_os_sleep(10); 13931980eff3SMatthew Dillon 13941980eff3SMatthew Dillon /* 13951980eff3SMatthew Dillon * Start transmitting COMRESET. COMRESET must be sent for at 13961980eff3SMatthew Dillon * least 1ms. 13971980eff3SMatthew Dillon */ 13981980eff3SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1399074579dfSMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) 1400258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1401074579dfSMatthew Dillon else 1402258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1403258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1404831bc9e3SMatthew Dillon 1405831bc9e3SMatthew Dillon /* 1406831bc9e3SMatthew Dillon * Through trial and error it seems to take around 100ms 1407831bc9e3SMatthew Dillon * for the detect logic to settle down. If this is too 1408831bc9e3SMatthew Dillon * short the softreset code will fail. 1409831bc9e3SMatthew Dillon */ 1410492bffafSMatthew Dillon if (ap->ap_flags & AP_F_HARSH_REINIT) 1411492bffafSMatthew Dillon ahci_os_sleep(1000); 1412492bffafSMatthew Dillon else 1413492bffafSMatthew Dillon ahci_os_sleep(200); 1414492bffafSMatthew Dillon ap->ap_flags &= ~AP_F_HARSH_REINIT; 1415cf5f3a81SMatthew Dillon 1416cf5f3a81SMatthew Dillon /* 1417cf5f3a81SMatthew Dillon * Only SERR_DIAG_X needs to be cleared for TFD updates, but 1418cf5f3a81SMatthew Dillon * since we are hard-resetting the port we might as well clear 1419cf5f3a81SMatthew Dillon * the whole enchillada 1420cf5f3a81SMatthew Dillon */ 1421cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1422cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 1423258223a3SMatthew Dillon r &= ~AHCI_PREG_SCTL_DET_INIT; 1424258223a3SMatthew Dillon r |= AHCI_PREG_SCTL_DET_NONE; 1425258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 1426258223a3SMatthew Dillon 14271980eff3SMatthew Dillon /* 14281980eff3SMatthew Dillon * Try to determine if there is a device on the port. 14291980eff3SMatthew Dillon * 14301980eff3SMatthew Dillon * Give the device 3/10 second to at least be detected. 14311980eff3SMatthew Dillon * If we fail clear PRCS (phy detect) since we may cycled 14321980eff3SMatthew Dillon * the phy and probably caused another PRCS interrupt. 14331980eff3SMatthew Dillon */ 143476497a9cSMatthew Dillon loop = 300; 143576497a9cSMatthew Dillon while (loop > 0) { 14361980eff3SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SSTS); 14371980eff3SMatthew Dillon if (r & AHCI_PREG_SSTS_DET) 14381980eff3SMatthew Dillon break; 143976497a9cSMatthew Dillon loop -= ahci_os_softsleep(); 14401980eff3SMatthew Dillon } 14411980eff3SMatthew Dillon if (loop == 0) { 14421980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS); 1443074579dfSMatthew Dillon if (bootverbose) { 14441980eff3SMatthew Dillon kprintf("%s: Port appears to be unplugged\n", 14451980eff3SMatthew Dillon PORTNAME(ap)); 1446074579dfSMatthew Dillon } 14473209f581SMatthew Dillon error = ENODEV; 144812feb904SMatthew Dillon goto done; 1449258223a3SMatthew Dillon } 1450258223a3SMatthew Dillon 1451cec85a37SMatthew Dillon /* 14521980eff3SMatthew Dillon * There is something on the port. Give the device 3 seconds 14531980eff3SMatthew Dillon * to fully negotiate. 14541980eff3SMatthew Dillon */ 145512feb904SMatthew Dillon if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS, 14561980eff3SMatthew Dillon AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) { 1457074579dfSMatthew Dillon if (bootverbose) { 14581980eff3SMatthew Dillon kprintf("%s: Device may be powered down\n", 14591980eff3SMatthew Dillon PORTNAME(ap)); 1460074579dfSMatthew Dillon } 14613209f581SMatthew Dillon error = ENODEV; 146212feb904SMatthew Dillon goto pmdetect; 14631980eff3SMatthew Dillon } 14641980eff3SMatthew Dillon 146512feb904SMatthew Dillon /* 146612feb904SMatthew Dillon * We got something that definitely looks like a device. Give 146712feb904SMatthew Dillon * the device time to send us its first D2H FIS. Waiting for 146812feb904SMatthew Dillon * BSY to clear accomplishes this. 146912feb904SMatthew Dillon * 147012feb904SMatthew Dillon * NOTE that a port multiplier may or may not clear BSY here, 147112feb904SMatthew Dillon * depending on what is sitting in target 0 behind it. 147212feb904SMatthew Dillon */ 1473c408a8b3SMatthew Dillon ahci_flush_tfd(ap); 1474c408a8b3SMatthew Dillon 147512feb904SMatthew Dillon if (ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD, 14761980eff3SMatthew Dillon AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 147712feb904SMatthew Dillon error = EBUSY; 14781980eff3SMatthew Dillon } else { 14793209f581SMatthew Dillon error = 0; 14801980eff3SMatthew Dillon } 1481258223a3SMatthew Dillon 148212feb904SMatthew Dillon pmdetect: 1483cf5f3a81SMatthew Dillon /* 148412feb904SMatthew Dillon * Do the PM port probe regardless of how things turned out on 148512feb904SMatthew Dillon * the BSY check. 1486cf5f3a81SMatthew Dillon */ 148712feb904SMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) 148812feb904SMatthew Dillon error = ahci_pm_port_probe(ap, error); 148912feb904SMatthew Dillon 149012feb904SMatthew Dillon done: 149112feb904SMatthew Dillon /* 149212feb904SMatthew Dillon * Finish up. 149312feb904SMatthew Dillon */ 149412feb904SMatthew Dillon switch(error) { 149512feb904SMatthew Dillon case 0: 149612feb904SMatthew Dillon /* 149712feb904SMatthew Dillon * All good, make sure the port is running and set the 149812feb904SMatthew Dillon * probe state. Ignore the signature junk (it's unreliable) 149912feb904SMatthew Dillon * until we get to the softreset code. 150012feb904SMatthew Dillon */ 150112feb904SMatthew Dillon if (ahci_port_start(ap)) { 150212feb904SMatthew Dillon kprintf("%s: failed to start command DMA on port, " 150312feb904SMatthew Dillon "disabling\n", PORTNAME(ap)); 150412feb904SMatthew Dillon error = EBUSY; 150512feb904SMatthew Dillon goto done; 150612feb904SMatthew Dillon } 1507f4553de1SMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 1508f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_GOOD; 1509f4553de1SMatthew Dillon else 1510f4553de1SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET; 151112feb904SMatthew Dillon break; 151212feb904SMatthew Dillon case ENODEV: 1513fd8bd957SMatthew Dillon /* 151412feb904SMatthew Dillon * Normal device probe failure 15151980eff3SMatthew Dillon */ 151612feb904SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SSTS); 15171980eff3SMatthew Dillon 151812feb904SMatthew Dillon switch(data & AHCI_PREG_SSTS_DET) { 151912feb904SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 152012feb904SMatthew Dillon kprintf("%s: Device not communicating\n", 15211980eff3SMatthew Dillon PORTNAME(ap)); 152212feb904SMatthew Dillon break; 152312feb904SMatthew Dillon case AHCI_PREG_SSTS_DET_PHYOFFLINE: 152412feb904SMatthew Dillon kprintf("%s: PHY offline\n", 152512feb904SMatthew Dillon PORTNAME(ap)); 152612feb904SMatthew Dillon break; 152712feb904SMatthew Dillon default: 152812feb904SMatthew Dillon kprintf("%s: No device detected\n", 152912feb904SMatthew Dillon PORTNAME(ap)); 153012feb904SMatthew Dillon break; 15311980eff3SMatthew Dillon } 153212feb904SMatthew Dillon ahci_port_hardstop(ap); 153312feb904SMatthew Dillon break; 153412feb904SMatthew Dillon default: 15351980eff3SMatthew Dillon /* 153612feb904SMatthew Dillon * Abnormal probe (EBUSY) 15371980eff3SMatthew Dillon */ 153812feb904SMatthew Dillon kprintf("%s: Device on port is bricked\n", 153912feb904SMatthew Dillon PORTNAME(ap)); 154012feb904SMatthew Dillon ahci_port_hardstop(ap); 154112feb904SMatthew Dillon #if 0 154212feb904SMatthew Dillon rc = ahci_port_reset(ap, atx, 0); 154312feb904SMatthew Dillon if (rc) { 154412feb904SMatthew Dillon kprintf("%s: Unable unbrick device\n", 154512feb904SMatthew Dillon PORTNAME(ap)); 15461980eff3SMatthew Dillon } else { 154712feb904SMatthew Dillon kprintf("%s: Successfully unbricked\n", 15483209f581SMatthew Dillon PORTNAME(ap)); 154912feb904SMatthew Dillon } 155012feb904SMatthew Dillon #endif 155112feb904SMatthew Dillon break; 15523209f581SMatthew Dillon } 15531067474aSMatthew Dillon 15541067474aSMatthew Dillon /* 155512feb904SMatthew Dillon * Clean up 15561067474aSMatthew Dillon */ 155712feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 155812feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 15593209f581SMatthew Dillon 156012feb904SMatthew Dillon ap->ap_flags &= ~AP_F_IN_RESET; 15611980eff3SMatthew Dillon 156212feb904SMatthew Dillon if (bootverbose) 156312feb904SMatthew Dillon kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error); 1564831bc9e3SMatthew Dillon return (error); 15651980eff3SMatthew Dillon } 15661980eff3SMatthew Dillon 15671980eff3SMatthew Dillon /* 1568cf5f3a81SMatthew Dillon * Hard-stop on hot-swap device removal. See 10.10.1 1569cf5f3a81SMatthew Dillon * 1570cf5f3a81SMatthew Dillon * Place the port in a mode that will allow it to detect hot-swap insertions. 1571cf5f3a81SMatthew Dillon * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't 1572cf5f3a81SMatthew Dillon * seem to do the job. 1573f17a0cedSMatthew Dillon * 1574f17a0cedSMatthew Dillon * FIS reception is left enabled but command processing is disabled. 1575f17a0cedSMatthew Dillon * Cycling FIS reception (FRE) can brick ports. 1576cf5f3a81SMatthew Dillon */ 1577cf5f3a81SMatthew Dillon void 1578cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap) 1579cf5f3a81SMatthew Dillon { 158076497a9cSMatthew Dillon struct ahci_ccb *ccb; 15811980eff3SMatthew Dillon struct ata_port *at; 1582cf5f3a81SMatthew Dillon u_int32_t r; 1583cf5f3a81SMatthew Dillon u_int32_t cmd; 158476497a9cSMatthew Dillon int slot; 15851980eff3SMatthew Dillon int i; 1586cf5f3a81SMatthew Dillon 1587cf5f3a81SMatthew Dillon /* 1588cf5f3a81SMatthew Dillon * Stop the port. We can't modify things like SUD if the port 1589cf5f3a81SMatthew Dillon * is running. 1590cf5f3a81SMatthew Dillon */ 1591cf5f3a81SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 15921980eff3SMatthew Dillon ap->ap_probe = ATA_PROBE_FAILED; 15931980eff3SMatthew Dillon ap->ap_type = ATA_PORT_T_NONE; 1594cf5f3a81SMatthew Dillon ahci_port_stop(ap, 0); 1595cf5f3a81SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 1596492bffafSMatthew Dillon cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC); 1597492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1598cf5f3a81SMatthew Dillon 1599cf5f3a81SMatthew Dillon /* 16001980eff3SMatthew Dillon * Clean up AT sub-ports on SATA port. 16011980eff3SMatthew Dillon */ 16021980eff3SMatthew Dillon for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) { 1603b012a2caSMatthew Dillon at = ap->ap_ata[i]; 16041980eff3SMatthew Dillon at->at_type = ATA_PORT_T_NONE; 16053209f581SMatthew Dillon at->at_probe = ATA_PROBE_FAILED; 16061980eff3SMatthew Dillon } 16071980eff3SMatthew Dillon 16081980eff3SMatthew Dillon /* 1609cf5f3a81SMatthew Dillon * Make sure FRE is active. There isn't anything we can do if it 1610cf5f3a81SMatthew Dillon * fails so just ignore errors. 1611cf5f3a81SMatthew Dillon */ 1612cf5f3a81SMatthew Dillon if ((cmd & AHCI_PREG_CMD_FRE) == 0) { 1613cf5f3a81SMatthew Dillon cmd |= AHCI_PREG_CMD_FRE; 1614cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1615cf5f3a81SMatthew Dillon if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) 1616cf5f3a81SMatthew Dillon ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR); 1617cf5f3a81SMatthew Dillon } 1618cf5f3a81SMatthew Dillon 1619cf5f3a81SMatthew Dillon /* 1620cf5f3a81SMatthew Dillon * 10.10.3 DET must be set to 0 before setting SUD to 0. 1621cf5f3a81SMatthew Dillon * 10.10.1 place us in the Listen state. 1622cf5f3a81SMatthew Dillon * 1623cf5f3a81SMatthew Dillon * Deactivating SUD only applies if the controller supports SUD. 1624cf5f3a81SMatthew Dillon */ 1625cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED); 16263209f581SMatthew Dillon ahci_os_sleep(1); 1627cf5f3a81SMatthew Dillon if (cmd & AHCI_PREG_CMD_SUD) { 1628cf5f3a81SMatthew Dillon cmd &= ~AHCI_PREG_CMD_SUD; 1629cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 1630cf5f3a81SMatthew Dillon } 16313209f581SMatthew Dillon ahci_os_sleep(1); 1632cf5f3a81SMatthew Dillon 1633cf5f3a81SMatthew Dillon /* 1634cf5f3a81SMatthew Dillon * Transition su to the spin-up state. HVA shall send COMRESET and 1635cf5f3a81SMatthew Dillon * begin initialization sequence (whatever that means). 1636cf5f3a81SMatthew Dillon * 1637cf5f3a81SMatthew Dillon * This only applies if the controller supports SUD. 16384e21f4daSMatthew Dillon * NEVER use AHCI_PREG_DET_DISABLE. 1639cf5f3a81SMatthew Dillon */ 1640492bffafSMatthew Dillon cmd |= AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD; 1641492bffafSMatthew Dillon cmd |= AHCI_PREG_CMD_ICC_ACTIVE; 1642cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CMD, cmd); 16433209f581SMatthew Dillon ahci_os_sleep(1); 1644cf5f3a81SMatthew Dillon 1645cf5f3a81SMatthew Dillon /* 1646cf5f3a81SMatthew Dillon * Transition us to the Reset state. Theoretically we send a 1647cf5f3a81SMatthew Dillon * continuous stream of COMRESETs in this state. 1648cf5f3a81SMatthew Dillon */ 1649cf5f3a81SMatthew Dillon r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT; 1650cf5f3a81SMatthew Dillon if (AhciForceGen1 & (1 << ap->ap_num)) { 1651cf5f3a81SMatthew Dillon kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap)); 1652cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_GEN1; 1653cf5f3a81SMatthew Dillon } else { 1654cf5f3a81SMatthew Dillon r |= AHCI_PREG_SCTL_SPD_ANY; 1655cf5f3a81SMatthew Dillon } 1656cf5f3a81SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SCTL, r); 16573209f581SMatthew Dillon ahci_os_sleep(1); 1658cf5f3a81SMatthew Dillon 1659cf5f3a81SMatthew Dillon /* 1660cf5f3a81SMatthew Dillon * Flush SERR_DIAG_X so the TFD can update. 1661cf5f3a81SMatthew Dillon */ 1662cf5f3a81SMatthew Dillon ahci_flush_tfd(ap); 1663cf5f3a81SMatthew Dillon 1664cf5f3a81SMatthew Dillon /* 166576497a9cSMatthew Dillon * Clean out pending ccbs 166676497a9cSMatthew Dillon */ 166776497a9cSMatthew Dillon while (ap->ap_active) { 166876497a9cSMatthew Dillon slot = ffs(ap->ap_active) - 1; 166976497a9cSMatthew Dillon ap->ap_active &= ~(1 << slot); 167076497a9cSMatthew Dillon ap->ap_expired &= ~(1 << slot); 167176497a9cSMatthew Dillon --ap->ap_active_cnt; 167276497a9cSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 167376497a9cSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) { 167476497a9cSMatthew Dillon callout_stop(&ccb->ccb_timeout); 167576497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 167676497a9cSMatthew Dillon } 167776497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED | 167876497a9cSMatthew Dillon ATA_F_TIMEOUT_EXPIRED); 167976497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 168076497a9cSMatthew Dillon ccb->ccb_done(ccb); 168176497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 168276497a9cSMatthew Dillon } 168376497a9cSMatthew Dillon while (ap->ap_sactive) { 168476497a9cSMatthew Dillon slot = ffs(ap->ap_sactive) - 1; 168576497a9cSMatthew Dillon ap->ap_sactive &= ~(1 << slot); 168676497a9cSMatthew Dillon ap->ap_expired &= ~(1 << slot); 168776497a9cSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 168876497a9cSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) { 168976497a9cSMatthew Dillon callout_stop(&ccb->ccb_timeout); 169076497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 169176497a9cSMatthew Dillon } 169276497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED | 169376497a9cSMatthew Dillon ATA_F_TIMEOUT_EXPIRED); 169476497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 169576497a9cSMatthew Dillon ccb->ccb_done(ccb); 169676497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 169776497a9cSMatthew Dillon } 169876497a9cSMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 169976497a9cSMatthew Dillon 170076497a9cSMatthew Dillon while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) { 170176497a9cSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 170276497a9cSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 170376497a9cSMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED; 170476497a9cSMatthew Dillon ccb->ccb_done(ccb); 170576497a9cSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 170676497a9cSMatthew Dillon } 170776497a9cSMatthew Dillon 170876497a9cSMatthew Dillon /* 1709cf5f3a81SMatthew Dillon * Leave us in COMRESET (both SUD and INIT active), the HBA should 1710cf5f3a81SMatthew Dillon * hopefully send us a DIAG_X-related interrupt if it receives 1711cf5f3a81SMatthew Dillon * a COMINIT, and if not that then at least a Phy transition 1712cf5f3a81SMatthew Dillon * interrupt. 1713cf5f3a81SMatthew Dillon * 1714cf5f3a81SMatthew Dillon * If we transition INIT from 1->0 to begin the initalization 1715cf5f3a81SMatthew Dillon * sequence it is unclear if that sequence will remain active 1716cf5f3a81SMatthew Dillon * until the next device insertion. 1717cf5f3a81SMatthew Dillon * 1718cf5f3a81SMatthew Dillon * If we go back to the listen state it is unclear if the 1719cf5f3a81SMatthew Dillon * device will actually send us a COMINIT, since we aren't 1720cf5f3a81SMatthew Dillon * sending any COMRESET's 1721cf5f3a81SMatthew Dillon */ 1722cf5f3a81SMatthew Dillon /* NOP */ 1723cf5f3a81SMatthew Dillon } 1724cf5f3a81SMatthew Dillon 1725cf5f3a81SMatthew Dillon /* 1726c408a8b3SMatthew Dillon * We can't loop on the X bit, a continuous COMINIT received will make 1727c408a8b3SMatthew Dillon * it loop forever. Just assume one event has built up and clear X 1728c408a8b3SMatthew Dillon * so the task file descriptor can update. 1729cf5f3a81SMatthew Dillon */ 1730cf5f3a81SMatthew Dillon void 1731cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap) 1732cf5f3a81SMatthew Dillon { 1733cf5f3a81SMatthew Dillon u_int32_t r; 1734cf5f3a81SMatthew Dillon 1735cf5f3a81SMatthew Dillon r = ahci_pread(ap, AHCI_PREG_SERR); 1736c408a8b3SMatthew Dillon if (r & AHCI_PREG_SERR_DIAG_X) 17371980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X); 1738cf5f3a81SMatthew Dillon } 1739cf5f3a81SMatthew Dillon 1740cf5f3a81SMatthew Dillon /* 1741fd8bd957SMatthew Dillon * Figure out what type of device is connected to the port, ATAPI or 1742fd8bd957SMatthew Dillon * DISK. 1743fd8bd957SMatthew Dillon */ 1744fd8bd957SMatthew Dillon int 17451980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at) 1746fd8bd957SMatthew Dillon { 1747fd8bd957SMatthew Dillon u_int32_t sig; 1748fd8bd957SMatthew Dillon 1749fd8bd957SMatthew Dillon sig = ahci_pread(ap, AHCI_PREG_SIG); 1750074579dfSMatthew Dillon if (bootverbose) 17511980eff3SMatthew Dillon kprintf("%s: sig %08x\n", ATANAME(ap, at), sig); 1752fd8bd957SMatthew Dillon if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) { 1753fd8bd957SMatthew Dillon return(ATA_PORT_T_ATAPI); 17541980eff3SMatthew Dillon } else if ((sig & 0xffff0000) == 17551980eff3SMatthew Dillon (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) { 17561980eff3SMatthew Dillon return(ATA_PORT_T_PM); 1757fd8bd957SMatthew Dillon } else { 1758fd8bd957SMatthew Dillon return(ATA_PORT_T_DISK); 1759fd8bd957SMatthew Dillon } 1760fd8bd957SMatthew Dillon } 1761fd8bd957SMatthew Dillon 1762fd8bd957SMatthew Dillon /* 1763fd8bd957SMatthew Dillon * Load the DMA descriptor table for a CCB's buffer. 1764fd8bd957SMatthew Dillon */ 1765258223a3SMatthew Dillon int 1766258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb) 1767258223a3SMatthew Dillon { 1768258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1769258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1770258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1771258223a3SMatthew Dillon struct ahci_prdt *prdt = ccb->ccb_cmd_table->prdt; 1772258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1773258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot = ccb->ccb_cmd_hdr; 1774258223a3SMatthew Dillon int error; 1775258223a3SMatthew Dillon 1776258223a3SMatthew Dillon if (xa->datalen == 0) { 1777258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdtl = 0; 1778258223a3SMatthew Dillon return (0); 1779258223a3SMatthew Dillon } 1780258223a3SMatthew Dillon 1781258223a3SMatthew Dillon error = bus_dmamap_load(sc->sc_tag_data, dmap, 1782258223a3SMatthew Dillon xa->data, xa->datalen, 1783258223a3SMatthew Dillon ahci_load_prdt_callback, 1784258223a3SMatthew Dillon &prdt, 1785258223a3SMatthew Dillon ((xa->flags & ATA_F_NOWAIT) ? 1786258223a3SMatthew Dillon BUS_DMA_NOWAIT : BUS_DMA_WAITOK)); 1787258223a3SMatthew Dillon if (error != 0) { 1788258223a3SMatthew Dillon kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error); 1789258223a3SMatthew Dillon return (1); 1790258223a3SMatthew Dillon } 179112feb904SMatthew Dillon #if 0 1792258223a3SMatthew Dillon if (xa->flags & ATA_F_PIO) 1793258223a3SMatthew Dillon prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR); 179412feb904SMatthew Dillon #endif 1795258223a3SMatthew Dillon 1796258223a3SMatthew Dillon cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1); 1797258223a3SMatthew Dillon 1798b012a2caSMatthew Dillon if (xa->flags & ATA_F_READ) 1799b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD); 1800b012a2caSMatthew Dillon if (xa->flags & ATA_F_WRITE) 1801b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE); 1802258223a3SMatthew Dillon 1803258223a3SMatthew Dillon return (0); 1804258223a3SMatthew Dillon } 1805258223a3SMatthew Dillon 1806258223a3SMatthew Dillon /* 1807258223a3SMatthew Dillon * Callback from BUSDMA system to load the segment list. The passed segment 1808258223a3SMatthew Dillon * list is a temporary structure. 1809258223a3SMatthew Dillon */ 1810258223a3SMatthew Dillon static 1811258223a3SMatthew Dillon void 1812258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs, 1813258223a3SMatthew Dillon int error) 1814258223a3SMatthew Dillon { 1815258223a3SMatthew Dillon struct ahci_prdt *prd = *(void **)info; 1816258223a3SMatthew Dillon u_int64_t addr; 1817258223a3SMatthew Dillon 1818258223a3SMatthew Dillon KKASSERT(nsegs <= AHCI_MAX_PRDT); 1819258223a3SMatthew Dillon 1820258223a3SMatthew Dillon while (nsegs) { 1821258223a3SMatthew Dillon addr = segs->ds_addr; 1822258223a3SMatthew Dillon prd->dba_hi = htole32((u_int32_t)(addr >> 32)); 1823258223a3SMatthew Dillon prd->dba_lo = htole32((u_int32_t)addr); 1824258223a3SMatthew Dillon prd->flags = htole32(segs->ds_len - 1); 1825258223a3SMatthew Dillon --nsegs; 1826258223a3SMatthew Dillon if (nsegs) 1827258223a3SMatthew Dillon ++prd; 1828258223a3SMatthew Dillon ++segs; 1829258223a3SMatthew Dillon } 1830258223a3SMatthew Dillon *(void **)info = prd; /* return last valid segment */ 1831258223a3SMatthew Dillon } 1832258223a3SMatthew Dillon 1833258223a3SMatthew Dillon void 1834258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb) 1835258223a3SMatthew Dillon { 1836258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1837258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1838258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 1839258223a3SMatthew Dillon bus_dmamap_t dmap = ccb->ccb_dmamap; 1840258223a3SMatthew Dillon 1841258223a3SMatthew Dillon if (xa->datalen != 0) { 1842b012a2caSMatthew Dillon if (xa->flags & ATA_F_READ) { 1843258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1844b012a2caSMatthew Dillon BUS_DMASYNC_POSTREAD); 1845b012a2caSMatthew Dillon } 1846b012a2caSMatthew Dillon if (xa->flags & ATA_F_WRITE) { 1847b012a2caSMatthew Dillon bus_dmamap_sync(sc->sc_tag_data, dmap, 1848b012a2caSMatthew Dillon BUS_DMASYNC_POSTWRITE); 1849b012a2caSMatthew Dillon } 1850258223a3SMatthew Dillon bus_dmamap_unload(sc->sc_tag_data, dmap); 1851258223a3SMatthew Dillon 1852f7d09f74SMatthew Dillon /* 1853f7d09f74SMatthew Dillon * prdbc is only updated by hardware for non-NCQ commands. 1854f7d09f74SMatthew Dillon */ 1855f7d09f74SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 1856f7d09f74SMatthew Dillon xa->resid = 0; 1857f7d09f74SMatthew Dillon } else { 185850a3ecb6SMatthew Dillon if (ccb->ccb_cmd_hdr->prdbc == 0 && 185950a3ecb6SMatthew Dillon ccb->ccb_xa.state == ATA_S_COMPLETE) { 1860f7d09f74SMatthew Dillon kprintf("%s: WARNING! Unload prdbc resid " 1861f7d09f74SMatthew Dillon "was zero! tag=%d\n", 186212feb904SMatthew Dillon ATANAME(ap, xa->at), ccb->ccb_slot); 186312feb904SMatthew Dillon } 1864258223a3SMatthew Dillon xa->resid = xa->datalen - 1865258223a3SMatthew Dillon le32toh(ccb->ccb_cmd_hdr->prdbc); 1866258223a3SMatthew Dillon } 1867258223a3SMatthew Dillon } 1868f7d09f74SMatthew Dillon } 1869258223a3SMatthew Dillon 18705f8c1efdSMatthew Dillon /* 18715f8c1efdSMatthew Dillon * Start a command and poll for completion. 18725f8c1efdSMatthew Dillon * 18733209f581SMatthew Dillon * timeout is in ms and only counts once the command gets on-chip. 18743209f581SMatthew Dillon * 1875831bc9e3SMatthew Dillon * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine 1876831bc9e3SMatthew Dillon * that no error occured. 1877831bc9e3SMatthew Dillon * 18785f8c1efdSMatthew Dillon * NOTE: If the caller specifies a NULL timeout function the caller is 18795f8c1efdSMatthew Dillon * responsible for clearing hardware state on failure, but we will 18805f8c1efdSMatthew Dillon * deal with removing the ccb from any pending queue. 18815f8c1efdSMatthew Dillon * 18825f8c1efdSMatthew Dillon * NOTE: NCQ should never be used with this function. 1883cf5f3a81SMatthew Dillon * 1884cf5f3a81SMatthew Dillon * NOTE: If the port is in a failed state and stopped we do not try 1885cf5f3a81SMatthew Dillon * to activate the ccb. 18865f8c1efdSMatthew Dillon */ 1887258223a3SMatthew Dillon int 1888831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, 1889831bc9e3SMatthew Dillon void (*timeout_fn)(struct ahci_ccb *)) 1890258223a3SMatthew Dillon { 1891258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1892258223a3SMatthew Dillon 1893cf5f3a81SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) { 1894cf5f3a81SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 1895831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 1896cf5f3a81SMatthew Dillon } 1897258223a3SMatthew Dillon crit_enter(); 189812feb904SMatthew Dillon #if 0 189912feb904SMatthew Dillon kprintf("%s: Start command %02x tag=%d\n", 190012feb904SMatthew Dillon ATANAME(ccb->ccb_port, ccb->ccb_xa.at), 190112feb904SMatthew Dillon ccb->ccb_xa.fis->command, ccb->ccb_slot); 190212feb904SMatthew Dillon #endif 1903258223a3SMatthew Dillon ahci_start(ccb); 19041980eff3SMatthew Dillon 1905258223a3SMatthew Dillon do { 1906f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 1907831bc9e3SMatthew Dillon switch(ccb->ccb_xa.state) { 1908831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 1909831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 1910f4553de1SMatthew Dillon break; 1911831bc9e3SMatthew Dillon case ATA_S_PENDING: 1912831bc9e3SMatthew Dillon ahci_os_softsleep(); 1913831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 1914831bc9e3SMatthew Dillon break; 1915831bc9e3SMatthew Dillon default: 1916831bc9e3SMatthew Dillon crit_exit(); 1917831bc9e3SMatthew Dillon return (ccb->ccb_xa.state); 1918f4553de1SMatthew Dillon } 19193209f581SMatthew Dillon } while (timeout > 0); 19205f8c1efdSMatthew Dillon 1921492bffafSMatthew Dillon if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) { 1922831bc9e3SMatthew Dillon kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n", 1923831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, 1924831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 1925831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS, 1926831bc9e3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR); 1927492bffafSMatthew Dillon } 19285f8c1efdSMatthew Dillon 1929258223a3SMatthew Dillon timeout_fn(ccb); 1930831bc9e3SMatthew Dillon 1931258223a3SMatthew Dillon crit_exit(); 1932258223a3SMatthew Dillon 1933831bc9e3SMatthew Dillon return(ccb->ccb_xa.state); 1934831bc9e3SMatthew Dillon } 1935831bc9e3SMatthew Dillon 1936831bc9e3SMatthew Dillon /* 1937831bc9e3SMatthew Dillon * When polling we have to check if the currently active CCB(s) 1938831bc9e3SMatthew Dillon * have timed out as the callout will be deadlocked while we 1939831bc9e3SMatthew Dillon * hold the port lock. 1940831bc9e3SMatthew Dillon */ 1941831bc9e3SMatthew Dillon void 1942831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap) 1943831bc9e3SMatthew Dillon { 1944831bc9e3SMatthew Dillon struct ahci_ccb *ccb; 1945831bc9e3SMatthew Dillon u_int32_t mask; 1946831bc9e3SMatthew Dillon int tag; 1947831bc9e3SMatthew Dillon 1948831bc9e3SMatthew Dillon mask = ap->ap_active | ap->ap_sactive; 1949831bc9e3SMatthew Dillon while (mask) { 1950831bc9e3SMatthew Dillon tag = ffs(mask) - 1; 1951831bc9e3SMatthew Dillon mask &= ~(1 << tag); 1952831bc9e3SMatthew Dillon ccb = &ap->ap_ccbs[tag]; 1953831bc9e3SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) { 1954831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(ccb); 1955831bc9e3SMatthew Dillon } 1956831bc9e3SMatthew Dillon } 1957258223a3SMatthew Dillon } 1958258223a3SMatthew Dillon 19593209f581SMatthew Dillon static 19603209f581SMatthew Dillon __inline 19613209f581SMatthew Dillon void 19623209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb) 19633209f581SMatthew Dillon { 19643209f581SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) { 19653209f581SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING; 19663209f581SMatthew Dillon callout_reset(&ccb->ccb_timeout, 19673209f581SMatthew Dillon (ccb->ccb_xa.timeout * hz + 999) / 1000, 19683209f581SMatthew Dillon ahci_ata_cmd_timeout_unserialized, ccb); 19693209f581SMatthew Dillon } 19703209f581SMatthew Dillon } 19713209f581SMatthew Dillon 1972258223a3SMatthew Dillon void 1973258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb) 1974258223a3SMatthew Dillon { 1975258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 1976258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 1977258223a3SMatthew Dillon 1978258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING); 1979258223a3SMatthew Dillon 1980258223a3SMatthew Dillon /* Zero transferred byte count before transfer */ 1981258223a3SMatthew Dillon ccb->ccb_cmd_hdr->prdbc = 0; 1982258223a3SMatthew Dillon 1983258223a3SMatthew Dillon /* Sync command list entry and corresponding command table entry */ 1984258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 1985258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 1986258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1987258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 1988258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 1989258223a3SMatthew Dillon BUS_DMASYNC_PREWRITE); 1990258223a3SMatthew Dillon 1991258223a3SMatthew Dillon /* Prepare RFIS area for write by controller */ 1992258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 1993258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 1994258223a3SMatthew Dillon BUS_DMASYNC_PREREAD); 1995258223a3SMatthew Dillon 19961980eff3SMatthew Dillon /* 19974c339a5fSMatthew Dillon * There's no point trying to optimize this, it only shaves a few 19984c339a5fSMatthew Dillon * nanoseconds so just queue the command and call our generic issue. 19991980eff3SMatthew Dillon */ 20004c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, ccb); 2001258223a3SMatthew Dillon } 2002258223a3SMatthew Dillon 2003831bc9e3SMatthew Dillon /* 2004831bc9e3SMatthew Dillon * While holding the port lock acquire exclusive access to the port. 2005831bc9e3SMatthew Dillon * 2006831bc9e3SMatthew Dillon * This is used when running the state machine to initialize and identify 2007831bc9e3SMatthew Dillon * targets over a port multiplier. Setting exclusive access prevents 2008831bc9e3SMatthew Dillon * ahci_port_intr() from activating any requests sitting on the pending 2009831bc9e3SMatthew Dillon * queue. 2010831bc9e3SMatthew Dillon */ 2011831bc9e3SMatthew Dillon void 2012831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at) 2013831bc9e3SMatthew Dillon { 2014831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0); 2015831bc9e3SMatthew Dillon ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS; 2016831bc9e3SMatthew Dillon while (ap->ap_active || ap->ap_sactive) { 2017831bc9e3SMatthew Dillon ahci_port_intr(ap, 1); 2018831bc9e3SMatthew Dillon ahci_os_softsleep(); 2019831bc9e3SMatthew Dillon } 2020831bc9e3SMatthew Dillon } 2021831bc9e3SMatthew Dillon 2022831bc9e3SMatthew Dillon void 2023831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at) 2024831bc9e3SMatthew Dillon { 2025831bc9e3SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0); 2026831bc9e3SMatthew Dillon ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS; 20274c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 2028831bc9e3SMatthew Dillon } 2029831bc9e3SMatthew Dillon 203012feb904SMatthew Dillon #if 0 203112feb904SMatthew Dillon 203212feb904SMatthew Dillon static void 203312feb904SMatthew Dillon fubar(struct ahci_ccb *ccb) 203412feb904SMatthew Dillon { 203512feb904SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 203612feb904SMatthew Dillon struct ahci_cmd_hdr *cmd; 203712feb904SMatthew Dillon struct ahci_cmd_table *tab; 203812feb904SMatthew Dillon struct ahci_prdt *prdt; 203912feb904SMatthew Dillon int i; 204012feb904SMatthew Dillon 204112feb904SMatthew Dillon kprintf("%s: ISSUE %02x\n", 204212feb904SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 204312feb904SMatthew Dillon ccb->ccb_xa.fis->command); 204412feb904SMatthew Dillon cmd = ccb->ccb_cmd_hdr; 204512feb904SMatthew Dillon tab = ccb->ccb_cmd_table; 204612feb904SMatthew Dillon prdt = ccb->ccb_cmd_table->prdt; 204712feb904SMatthew Dillon kprintf("cmd flags=%04x prdtl=%d prdbc=%d ctba=%08x%08x\n", 204812feb904SMatthew Dillon cmd->flags, cmd->prdtl, cmd->prdbc, 204912feb904SMatthew Dillon cmd->ctba_hi, cmd->ctba_lo); 205012feb904SMatthew Dillon for (i = 0; i < cmd->prdtl; ++i) { 205112feb904SMatthew Dillon kprintf("\t%d dba=%08x%08x res=%08x flags=%08x\n", 205212feb904SMatthew Dillon i, prdt->dba_hi, prdt->dba_lo, prdt->reserved, 205312feb904SMatthew Dillon prdt->flags); 205412feb904SMatthew Dillon } 205512feb904SMatthew Dillon kprintf("tab\n"); 205612feb904SMatthew Dillon } 205712feb904SMatthew Dillon 205812feb904SMatthew Dillon #endif 205912feb904SMatthew Dillon 20601980eff3SMatthew Dillon /* 20614c339a5fSMatthew Dillon * If ccb is not NULL enqueue and/or issue it. 20624c339a5fSMatthew Dillon * 20634c339a5fSMatthew Dillon * If ccb is NULL issue whatever we can from the queue. However, nothing 20644c339a5fSMatthew Dillon * new is issued if the exclusive access flag is set or expired ccb's are 20654c339a5fSMatthew Dillon * present. 20664c339a5fSMatthew Dillon * 20674c339a5fSMatthew Dillon * If existing commands are still active (ap_active/ap_sactive) we can only 20684c339a5fSMatthew Dillon * issue matching new commands. 20691980eff3SMatthew Dillon */ 20704c339a5fSMatthew Dillon void 20714c339a5fSMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb) 20724c339a5fSMatthew Dillon { 20734c339a5fSMatthew Dillon u_int32_t mask; 20744c339a5fSMatthew Dillon int limit; 2075258223a3SMatthew Dillon 20761980eff3SMatthew Dillon /* 20774c339a5fSMatthew Dillon * Enqueue the ccb. 20784c339a5fSMatthew Dillon * 20794c339a5fSMatthew Dillon * If just running the queue and in exclusive access mode we 20804c339a5fSMatthew Dillon * just return. Also in this case if there are any expired ccb's 20814c339a5fSMatthew Dillon * we want to clear the queue so the port can be safely stopped. 20824c339a5fSMatthew Dillon */ 20834c339a5fSMatthew Dillon if (ccb) { 20844c339a5fSMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry); 20854c339a5fSMatthew Dillon } else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) { 20864c339a5fSMatthew Dillon return; 20874c339a5fSMatthew Dillon } 20884c339a5fSMatthew Dillon 20894c339a5fSMatthew Dillon /* 20904c339a5fSMatthew Dillon * Pull the next ccb off the queue and run it if possible. 20914c339a5fSMatthew Dillon */ 20924c339a5fSMatthew Dillon if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL) 20934c339a5fSMatthew Dillon return; 20944c339a5fSMatthew Dillon 209512feb904SMatthew Dillon /* 209612feb904SMatthew Dillon * Handle exclusivity requirements. 209712feb904SMatthew Dillon * 209812feb904SMatthew Dillon * ATA_F_EXCLUSIVE is used when we want to be the only command 209912feb904SMatthew Dillon * running. 210012feb904SMatthew Dillon * 210112feb904SMatthew Dillon * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded 210212feb904SMatthew Dillon * back into the ccb on a normal (non-errored) command completion. 210312feb904SMatthew Dillon * For example, for PM requests to target 15. Because the AHCI 210412feb904SMatthew Dillon * spec does not stop the command processor and has only one rfis 210512feb904SMatthew Dillon * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE. 210612feb904SMatthew Dillon * Otherwise multiple completions can destroy the rfis data before 210712feb904SMatthew Dillon * we have a chance to copy it. 210812feb904SMatthew Dillon */ 210912feb904SMatthew Dillon if (ap->ap_active & ~ap->ap_expired) { 211012feb904SMatthew Dillon /* 211112feb904SMatthew Dillon * There may be multiple ccb's already running, 211212feb904SMatthew Dillon * if any are running and ap_run_flags sets 211312feb904SMatthew Dillon * one of these flags then we know only one is 211412feb904SMatthew Dillon * running. 211512feb904SMatthew Dillon * 211612feb904SMatthew Dillon * XXX Current AUTOSENSE code forces exclusivity 211712feb904SMatthew Dillon * to simplify the code. 211812feb904SMatthew Dillon */ 211912feb904SMatthew Dillon if (ap->ap_run_flags & 212012feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) { 212112feb904SMatthew Dillon return; 212212feb904SMatthew Dillon } 212312feb904SMatthew Dillon 212412feb904SMatthew Dillon if (ccb->ccb_xa.flags & 212512feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) { 212612feb904SMatthew Dillon return; 212712feb904SMatthew Dillon } 212812feb904SMatthew Dillon } 212912feb904SMatthew Dillon 21304c339a5fSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 21314c339a5fSMatthew Dillon /* 21324c339a5fSMatthew Dillon * The next command is a NCQ command and can be issued as 21334c339a5fSMatthew Dillon * long as currently active commands are not standard. 21344c339a5fSMatthew Dillon */ 21354c339a5fSMatthew Dillon if (ap->ap_active) { 21364c339a5fSMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 21374c339a5fSMatthew Dillon return; 21384c339a5fSMatthew Dillon } 21394c339a5fSMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 21404c339a5fSMatthew Dillon 21414c339a5fSMatthew Dillon mask = 0; 21424c339a5fSMatthew Dillon do { 21434c339a5fSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 21444c339a5fSMatthew Dillon mask |= 1 << ccb->ccb_slot; 21454c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 214612feb904SMatthew Dillon ahci_start_timeout(ccb); 214712feb904SMatthew Dillon ap->ap_run_flags = ccb->ccb_xa.flags; 21484c339a5fSMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_pending); 214912feb904SMatthew Dillon } while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) && 215012feb904SMatthew Dillon (ap->ap_run_flags & 215112feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0); 21524c339a5fSMatthew Dillon 21534c339a5fSMatthew Dillon ap->ap_sactive |= mask; 21544c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, mask); 21554c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, mask); 21564c339a5fSMatthew Dillon } else { 21574c339a5fSMatthew Dillon /* 21584c339a5fSMatthew Dillon * The next command is a standard command and can be issued 21594c339a5fSMatthew Dillon * as long as currently active commands are not NCQ. 21604c339a5fSMatthew Dillon * 21614c339a5fSMatthew Dillon * We limit ourself to 1 command if we have a port multiplier, 21624c339a5fSMatthew Dillon * (at least without FBSS support), otherwise timeouts on 21634c339a5fSMatthew Dillon * one port can race completions on other ports (see 21644c339a5fSMatthew Dillon * ahci_ata_cmd_timeout() for more information). 21654c339a5fSMatthew Dillon * 21664c339a5fSMatthew Dillon * If not on a port multiplier generally allow up to 4 21674c339a5fSMatthew Dillon * standard commands to be enqueued. Remember that the 21684c339a5fSMatthew Dillon * command processor will still process them sequentially. 21691980eff3SMatthew Dillon */ 21701980eff3SMatthew Dillon if (ap->ap_sactive) 2171258223a3SMatthew Dillon return; 21724c339a5fSMatthew Dillon if (ap->ap_type == ATA_PORT_T_PM) 21734c339a5fSMatthew Dillon limit = 1; 21744c339a5fSMatthew Dillon else if (ap->ap_sc->sc_ncmds > 4) 21754c339a5fSMatthew Dillon limit = 4; 21764c339a5fSMatthew Dillon else 21774c339a5fSMatthew Dillon limit = 2; 2178258223a3SMatthew Dillon 21794c339a5fSMatthew Dillon while (ap->ap_active_cnt < limit && ccb && 21804c339a5fSMatthew Dillon (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) { 21814c339a5fSMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 218212feb904SMatthew Dillon #if 0 218312feb904SMatthew Dillon fubar(ccb); 218412feb904SMatthew Dillon #endif 21854c339a5fSMatthew Dillon ap->ap_active |= 1 << ccb->ccb_slot; 2186258223a3SMatthew Dillon ap->ap_active_cnt++; 218712feb904SMatthew Dillon ap->ap_run_flags = ccb->ccb_xa.flags; 21884c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_ONCHIP; 21894c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot); 219012feb904SMatthew Dillon ahci_start_timeout(ccb); 219122726f69SMatthew Dillon if ((ap->ap_run_flags & 219222726f69SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) { 219322726f69SMatthew Dillon break; 219422726f69SMatthew Dillon } 21954c339a5fSMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_pending); 219612feb904SMatthew Dillon if (ccb && (ccb->ccb_xa.flags & 219712feb904SMatthew Dillon (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) { 219812feb904SMatthew Dillon break; 219912feb904SMatthew Dillon } 22001980eff3SMatthew Dillon } 2201258223a3SMatthew Dillon } 2202258223a3SMatthew Dillon } 2203258223a3SMatthew Dillon 2204258223a3SMatthew Dillon void 2205258223a3SMatthew Dillon ahci_intr(void *arg) 2206258223a3SMatthew Dillon { 2207258223a3SMatthew Dillon struct ahci_softc *sc = arg; 2208f4553de1SMatthew Dillon struct ahci_port *ap; 220912feb904SMatthew Dillon u_int32_t is; 221012feb904SMatthew Dillon u_int32_t ack; 2211258223a3SMatthew Dillon int port; 2212258223a3SMatthew Dillon 2213f4553de1SMatthew Dillon /* 2214f4553de1SMatthew Dillon * Check if the master enable is up, and whether any interrupts are 2215f4553de1SMatthew Dillon * pending. 2216f4553de1SMatthew Dillon */ 2217f4553de1SMatthew Dillon if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0) 2218f4553de1SMatthew Dillon return; 2219258223a3SMatthew Dillon is = ahci_read(sc, AHCI_REG_IS); 222012feb904SMatthew Dillon if (is == 0 || is == 0xffffffff) { 2221258223a3SMatthew Dillon return; 222212feb904SMatthew Dillon } 222312feb904SMatthew Dillon is &= sc->sc_portmask; 2224258223a3SMatthew Dillon 2225258223a3SMatthew Dillon #ifdef AHCI_COALESCE 2226258223a3SMatthew Dillon /* Check coalescing interrupt first */ 2227258223a3SMatthew Dillon if (is & sc->sc_ccc_mask) { 2228258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n", 2229258223a3SMatthew Dillon DEVNAME(sc)); 2230258223a3SMatthew Dillon is &= ~sc->sc_ccc_mask; 2231258223a3SMatthew Dillon is |= sc->sc_ccc_ports_cur; 2232258223a3SMatthew Dillon } 2233258223a3SMatthew Dillon #endif 2234258223a3SMatthew Dillon 2235f4553de1SMatthew Dillon /* 2236f4553de1SMatthew Dillon * Process interrupts for each port in a non-blocking fashion. 223712feb904SMatthew Dillon * 223812feb904SMatthew Dillon * The global IS bit is forced on if any unmasked port interrupts 223912feb904SMatthew Dillon * are pending, even if we clear. 2240f4553de1SMatthew Dillon */ 224112feb904SMatthew Dillon for (ack = 0; is; is &= ~(1 << port)) { 2242258223a3SMatthew Dillon port = ffs(is) - 1; 224312feb904SMatthew Dillon ack |= 1 << port; 224412feb904SMatthew Dillon 2245f4553de1SMatthew Dillon ap = sc->sc_ports[port]; 224612feb904SMatthew Dillon if (ap == NULL) 224712feb904SMatthew Dillon continue; 224812feb904SMatthew Dillon 2249f4553de1SMatthew Dillon if (ahci_os_lock_port_nb(ap) == 0) { 2250f4553de1SMatthew Dillon ahci_port_intr(ap, 0); 2251f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 2252f4553de1SMatthew Dillon } else { 2253f4553de1SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2254f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2255f4553de1SMatthew Dillon } 2256f4553de1SMatthew Dillon } 2257258223a3SMatthew Dillon ahci_write(sc, AHCI_REG_IS, ack); 2258258223a3SMatthew Dillon } 2259258223a3SMatthew Dillon 2260f4553de1SMatthew Dillon /* 2261f4553de1SMatthew Dillon * Core called from helper thread. 2262f4553de1SMatthew Dillon */ 22633209f581SMatthew Dillon void 2264f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask) 2265f4553de1SMatthew Dillon { 2266f4553de1SMatthew Dillon /* 2267f4553de1SMatthew Dillon * Process any expired timedouts. 2268f4553de1SMatthew Dillon */ 2269f4553de1SMatthew Dillon ahci_os_lock_port(ap); 2270f4553de1SMatthew Dillon if (mask & AP_SIGF_TIMEOUT) { 2271831bc9e3SMatthew Dillon ahci_check_active_timeouts(ap); 2272f4553de1SMatthew Dillon } 2273f4553de1SMatthew Dillon 2274f4553de1SMatthew Dillon /* 2275f4553de1SMatthew Dillon * Process port interrupts which require a higher level of 2276f4553de1SMatthew Dillon * intervention. 2277f4553de1SMatthew Dillon */ 2278f4553de1SMatthew Dillon if (mask & AP_SIGF_PORTINT) { 2279f4553de1SMatthew Dillon ahci_port_intr(ap, 1); 2280f4553de1SMatthew Dillon ahci_port_interrupt_enable(ap); 2281831bc9e3SMatthew Dillon ahci_os_unlock_port(ap); 228212feb904SMatthew Dillon } else if (ap->ap_probe != ATA_PROBE_FAILED) { 228312feb904SMatthew Dillon ahci_port_intr(ap, 1); 228412feb904SMatthew Dillon ahci_port_interrupt_enable(ap); 228512feb904SMatthew Dillon ahci_os_unlock_port(ap); 2286f4553de1SMatthew Dillon } else { 2287f4553de1SMatthew Dillon ahci_os_unlock_port(ap); 2288f4553de1SMatthew Dillon } 2289f4553de1SMatthew Dillon } 2290f4553de1SMatthew Dillon 2291f4553de1SMatthew Dillon /* 2292f4553de1SMatthew Dillon * Core per-port interrupt handler. 2293f4553de1SMatthew Dillon * 2294f4553de1SMatthew Dillon * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only 2295f4553de1SMatthew Dillon * deal with normal command completions which do not require blocking. 2296f4553de1SMatthew Dillon */ 2297f4553de1SMatthew Dillon void 2298f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable) 2299258223a3SMatthew Dillon { 2300258223a3SMatthew Dillon struct ahci_softc *sc = ap->ap_sc; 23013209f581SMatthew Dillon u_int32_t is, ci_saved, ci_masked; 230222181ab7SMatthew Dillon int slot; 2303492bffafSMatthew Dillon int stopped = 0; 2304258223a3SMatthew Dillon struct ahci_ccb *ccb = NULL; 23051980eff3SMatthew Dillon struct ata_port *ccb_at = NULL; 2306258223a3SMatthew Dillon volatile u_int32_t *active; 2307f4553de1SMatthew Dillon const u_int32_t blockable_mask = AHCI_PREG_IS_TFES | 2308f4553de1SMatthew Dillon AHCI_PREG_IS_IFS | 2309f4553de1SMatthew Dillon AHCI_PREG_IS_PCS | 2310f4553de1SMatthew Dillon AHCI_PREG_IS_PRCS | 2311f4553de1SMatthew Dillon AHCI_PREG_IS_HBFS | 2312f4553de1SMatthew Dillon AHCI_PREG_IS_OFS | 2313f4553de1SMatthew Dillon AHCI_PREG_IS_UFS; 2314f4553de1SMatthew Dillon 2315492bffafSMatthew Dillon enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART, 2316492bffafSMatthew Dillon NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING; 2317258223a3SMatthew Dillon 2318f4553de1SMatthew Dillon /* 2319f4553de1SMatthew Dillon * All basic command completions are always processed. 2320f4553de1SMatthew Dillon */ 232112feb904SMatthew Dillon is = ahci_pread(ap, AHCI_PREG_IS); 2322cec07d75SMatthew Dillon if (is & AHCI_PREG_IS_DPS) 2323cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS); 2324258223a3SMatthew Dillon 2325f4553de1SMatthew Dillon /* 2326f4553de1SMatthew Dillon * If we can't block then we can't handle these here. Disable 2327f4553de1SMatthew Dillon * the interrupts in question so we don't live-lock, the helper 2328f4553de1SMatthew Dillon * thread will re-enable them. 2329f4553de1SMatthew Dillon * 2330f4553de1SMatthew Dillon * If the port is in a completely failed state we do not want 2331dbef6246SMatthew Dillon * to drop through to failed-command-processing if blockable is 0, 2332f4553de1SMatthew Dillon * just let the thread deal with it all. 2333dbef6246SMatthew Dillon * 2334dbef6246SMatthew Dillon * Otherwise we fall through and still handle DHRS and any commands 2335dbef6246SMatthew Dillon * which completed normally. Even if we are errored we haven't 2336dbef6246SMatthew Dillon * stopped the port yet so CI/SACT are still good. 2337f4553de1SMatthew Dillon */ 2338f4553de1SMatthew Dillon if (blockable == 0) { 2339f4553de1SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 234012feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2341f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 2342f4553de1SMatthew Dillon return; 2343f4553de1SMatthew Dillon } 2344f4553de1SMatthew Dillon if (is & blockable_mask) { 234512feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IE, 0); 2346f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT); 234712feb904SMatthew Dillon return; 2348f4553de1SMatthew Dillon } 2349f4553de1SMatthew Dillon } 2350f4553de1SMatthew Dillon 23513209f581SMatthew Dillon /* 2352f4553de1SMatthew Dillon * Either NCQ or non-NCQ commands will be active, never both. 23533209f581SMatthew Dillon */ 2354258223a3SMatthew Dillon if (ap->ap_sactive) { 2355258223a3SMatthew Dillon KKASSERT(ap->ap_active == 0); 2356258223a3SMatthew Dillon KKASSERT(ap->ap_active_cnt == 0); 2357258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_SACT); 2358258223a3SMatthew Dillon active = &ap->ap_sactive; 2359258223a3SMatthew Dillon } else { 2360258223a3SMatthew Dillon ci_saved = ahci_pread(ap, AHCI_PREG_CI); 2361258223a3SMatthew Dillon active = &ap->ap_active; 2362258223a3SMatthew Dillon } 236312feb904SMatthew Dillon KKASSERT(!(ap->ap_sactive && ap->ap_active)); 236412feb904SMatthew Dillon #if 0 236512feb904SMatthew Dillon kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n", 236612feb904SMatthew Dillon ap->ap_active, ahci_pread(ap, AHCI_PREG_CI), 236712feb904SMatthew Dillon ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT)); 236812feb904SMatthew Dillon #endif 2369258223a3SMatthew Dillon 2370492bffafSMatthew Dillon /* 2371492bffafSMatthew Dillon * Ignore AHCI_PREG_IS_PRCS when link power management is on 2372492bffafSMatthew Dillon */ 2373795adb22SMatthew Dillon if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) { 2374795adb22SMatthew Dillon is &= ~AHCI_PREG_IS_PRCS; 2375795adb22SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 2376795adb22SMatthew Dillon AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W); 2377795adb22SMatthew Dillon } 2378795adb22SMatthew Dillon 2379cf5f3a81SMatthew Dillon /* 2380f4553de1SMatthew Dillon * Command failed (blockable). 2381f4553de1SMatthew Dillon * 2382f4553de1SMatthew Dillon * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. 23831980eff3SMatthew Dillon * 23841980eff3SMatthew Dillon * This stops command processing. 2385cf5f3a81SMatthew Dillon */ 2386492bffafSMatthew Dillon if (is & AHCI_PREG_IS_TFES) { 2387258223a3SMatthew Dillon u_int32_t tfd, serr; 2388258223a3SMatthew Dillon int err_slot; 2389258223a3SMatthew Dillon 239012feb904SMatthew Dillon process_error: 2391258223a3SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 2392258223a3SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2393258223a3SMatthew Dillon 2394cf5f3a81SMatthew Dillon /* 239512feb904SMatthew Dillon * Load the error slot and restart command processing. 239612feb904SMatthew Dillon * CLO if we need to. The error slot may not be valid. 239712feb904SMatthew Dillon * MUST BE DONE BEFORE CLEARING ST! 239812feb904SMatthew Dillon * 239912feb904SMatthew Dillon * Cycle ST. 240012feb904SMatthew Dillon * 240112feb904SMatthew Dillon * It is unclear but we may have to clear SERR to reenable 240212feb904SMatthew Dillon * error processing. 2403cf5f3a81SMatthew Dillon */ 240412feb904SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD)); 240512feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES | 240612feb904SMatthew Dillon AHCI_PREG_IS_PSS | 240712feb904SMatthew Dillon AHCI_PREG_IS_DHRS | 240812feb904SMatthew Dillon AHCI_PREG_IS_SDBS); 240912feb904SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS | 241012feb904SMatthew Dillon AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS); 241112feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, serr); 2412258223a3SMatthew Dillon ahci_port_stop(ap, 0); 241312feb904SMatthew Dillon ahci_os_hardsleep(10); 241412feb904SMatthew Dillon if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 241512feb904SMatthew Dillon kprintf("%s: Issuing CLO\n", PORTNAME(ap)); 241612feb904SMatthew Dillon ahci_port_clo(ap); 241712feb904SMatthew Dillon } 2418492bffafSMatthew Dillon 2419492bffafSMatthew Dillon /* 2420492bffafSMatthew Dillon * We are now stopped and need a restart. If we have to 2421492bffafSMatthew Dillon * process a NCQ error we will temporarily start and then 2422492bffafSMatthew Dillon * stop the port again, so this condition holds. 2423492bffafSMatthew Dillon */ 2424492bffafSMatthew Dillon stopped = 1; 242522181ab7SMatthew Dillon need = NEED_RESTART; 2426258223a3SMatthew Dillon 242750a3ecb6SMatthew Dillon /* 242850a3ecb6SMatthew Dillon * ATAPI errors are fairly common from probing, just 242950a3ecb6SMatthew Dillon * report disk errors or if bootverbose is on. 243050a3ecb6SMatthew Dillon */ 243150a3ecb6SMatthew Dillon if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) { 243212feb904SMatthew Dillon kprintf("%s: TFES slot %d ci_saved = %08x\n", 243312feb904SMatthew Dillon PORTNAME(ap), err_slot, ci_saved); 243450a3ecb6SMatthew Dillon } 2435258223a3SMatthew Dillon 24361980eff3SMatthew Dillon /* 243712feb904SMatthew Dillon * If we got an error on an error CCB just complete it 243812feb904SMatthew Dillon * with an error. ci_saved has the mask to restart 243912feb904SMatthew Dillon * (the err_ccb will be removed from it by finish_error). 24401980eff3SMatthew Dillon */ 244112feb904SMatthew Dillon if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) { 244212feb904SMatthew Dillon err_slot = ap->ap_err_ccb->ccb_slot; 244312feb904SMatthew Dillon goto finish_error; 2444258223a3SMatthew Dillon } 2445258223a3SMatthew Dillon 24461980eff3SMatthew Dillon /* 244712feb904SMatthew Dillon * If NCQ commands were active get the error slot from 244812feb904SMatthew Dillon * the log page. NCQ is not supported for PM's so this 244912feb904SMatthew Dillon * is a direct-attached target. 24501980eff3SMatthew Dillon * 245112feb904SMatthew Dillon * Otherwise if no commands were active we have a problem. 245212feb904SMatthew Dillon * 245312feb904SMatthew Dillon * Otherwise if the error slot is bad we have a problem. 245412feb904SMatthew Dillon * 245512feb904SMatthew Dillon * Otherwise process the error for the slot. 24561980eff3SMatthew Dillon */ 245712feb904SMatthew Dillon if (ap->ap_sactive) { 2458492bffafSMatthew Dillon ahci_port_start(ap); 245912feb904SMatthew Dillon err_slot = ahci_port_read_ncq_error(ap, 0); 2460492bffafSMatthew Dillon ahci_port_stop(ap, 0); 246112feb904SMatthew Dillon } else if (ap->ap_active == 0) { 246212feb904SMatthew Dillon kprintf("%s: TFES with no commands pending\n", 246312feb904SMatthew Dillon PORTNAME(ap)); 246412feb904SMatthew Dillon err_slot = -1; 246512feb904SMatthew Dillon } else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) { 246612feb904SMatthew Dillon kprintf("%s: bad error slot %d\n", 2467258223a3SMatthew Dillon PORTNAME(ap), err_slot); 246812feb904SMatthew Dillon err_slot = -1; 2469258223a3SMatthew Dillon } else { 247012feb904SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 247112feb904SMatthew Dillon 24724c339a5fSMatthew Dillon /* 247312feb904SMatthew Dillon * Validate the errored ccb. Note that ccb_at can 247412feb904SMatthew Dillon * be NULL for direct-attached ccb's. 247512feb904SMatthew Dillon * 247612feb904SMatthew Dillon * Copy received taskfile data from the RFIS. 24774c339a5fSMatthew Dillon */ 247812feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) { 247912feb904SMatthew Dillon ccb_at = ccb->ccb_xa.at; 248012feb904SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis, 248112feb904SMatthew Dillon sizeof(struct ata_fis_d2h)); 248250a3ecb6SMatthew Dillon if (bootverbose) { 248350a3ecb6SMatthew Dillon kprintf("%s: Copying rfis slot %d\n", 248412feb904SMatthew Dillon ATANAME(ap, ccb_at), err_slot); 248550a3ecb6SMatthew Dillon } 248612feb904SMatthew Dillon } else { 248712feb904SMatthew Dillon kprintf("%s: Cannot copy rfis, CCB slot " 248812feb904SMatthew Dillon "%d is not on-chip (state=%d)\n", 248912feb904SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), 249012feb904SMatthew Dillon err_slot, ccb->ccb_xa.state); 249112feb904SMatthew Dillon err_slot = -1; 249212feb904SMatthew Dillon } 2493258223a3SMatthew Dillon } 2494258223a3SMatthew Dillon 2495258223a3SMatthew Dillon /* 249612feb904SMatthew Dillon * If we could not determine the errored slot then 249712feb904SMatthew Dillon * reset the port. 2498258223a3SMatthew Dillon */ 249912feb904SMatthew Dillon if (err_slot < 0) { 250012feb904SMatthew Dillon kprintf("%s: TFES: Unable to determine errored slot\n", 250112feb904SMatthew Dillon PORTNAME(ap)); 25021980eff3SMatthew Dillon if (ap->ap_flags & AP_F_IN_RESET) 25031980eff3SMatthew Dillon goto fatal; 2504258223a3SMatthew Dillon goto failall; 2505258223a3SMatthew Dillon } 2506258223a3SMatthew Dillon 250712feb904SMatthew Dillon /* 250812feb904SMatthew Dillon * Finish error on slot. We will restart ci_saved 250912feb904SMatthew Dillon * commands except the errored slot which we generate 251012feb904SMatthew Dillon * a failure for. 251112feb904SMatthew Dillon */ 251212feb904SMatthew Dillon finish_error: 251312feb904SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 2514258223a3SMatthew Dillon ci_saved &= ~(1 << err_slot); 2515258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP); 2516258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 25171980eff3SMatthew Dillon } else if (is & AHCI_PREG_IS_DHRS) { 25181980eff3SMatthew Dillon /* 2519f4553de1SMatthew Dillon * Command posted D2H register FIS to the rfis (non-blocking). 2520f4553de1SMatthew Dillon * 252112feb904SMatthew Dillon * A normal completion with an error may set DHRS instead 252212feb904SMatthew Dillon * of TFES. The CCS bits are only valid if ERR was set. 252312feb904SMatthew Dillon * If ERR is set command processing was probably stopped. 25248bf6a3ffSMatthew Dillon * 252512feb904SMatthew Dillon * If ERR was not set we can only copy-back data for 252612feb904SMatthew Dillon * exclusive-mode commands because otherwise we won't know 252712feb904SMatthew Dillon * which tag the rfis belonged to. 252812feb904SMatthew Dillon * 252912feb904SMatthew Dillon * err_slot must be read from the CCS before any other port 253012feb904SMatthew Dillon * action, such as stopping the port. 253112feb904SMatthew Dillon * 253212feb904SMatthew Dillon * WARNING! This is not well documented in the AHCI spec. 253312feb904SMatthew Dillon * It can be found in the state machine tables 253412feb904SMatthew Dillon * but not in the explanations. 25351980eff3SMatthew Dillon */ 253612feb904SMatthew Dillon u_int32_t tfd; 253712feb904SMatthew Dillon u_int32_t cmd; 25381980eff3SMatthew Dillon int err_slot; 25391980eff3SMatthew Dillon 254012feb904SMatthew Dillon tfd = ahci_pread(ap, AHCI_PREG_TFD); 254112feb904SMatthew Dillon cmd = ahci_pread(ap, AHCI_PREG_CMD); 254212feb904SMatthew Dillon 254312feb904SMatthew Dillon if ((tfd & AHCI_PREG_TFD_STS_ERR) && 254412feb904SMatthew Dillon (cmd & AHCI_PREG_CMD_CR) == 0) { 25451980eff3SMatthew Dillon err_slot = AHCI_PREG_CMD_CCS( 25461980eff3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD)); 25471980eff3SMatthew Dillon ccb = &ap->ap_ccbs[err_slot]; 254812feb904SMatthew Dillon kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n", 254912feb904SMatthew Dillon PORTNAME(ap), 255012feb904SMatthew Dillon tfd, AHCI_PFMT_TFD_STS, 255112feb904SMatthew Dillon err_slot, ccb->ccb_xa.fis->command); 255212feb904SMatthew Dillon goto process_error; 2553258223a3SMatthew Dillon } 255412feb904SMatthew Dillon /* 255512feb904SMatthew Dillon * NO ELSE... copy back is in the normal command completion 255612feb904SMatthew Dillon * code and only if no error occured and ATA_F_AUTOSENSE 255712feb904SMatthew Dillon * was set. 255812feb904SMatthew Dillon */ 2559cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS); 25601980eff3SMatthew Dillon } 25611980eff3SMatthew Dillon 25621980eff3SMatthew Dillon /* 2563f4553de1SMatthew Dillon * Device notification to us (non-blocking) 25641980eff3SMatthew Dillon * 256512feb904SMatthew Dillon * NOTE! On some parts notification bits can cause an IPMS 256612feb904SMatthew Dillon * interrupt instead of a SDBS interrupt. 2567cec07d75SMatthew Dillon * 256812feb904SMatthew Dillon * NOTE! On some parts (e.g. VBOX, probably intel ICHx), 256912feb904SMatthew Dillon * SDBS notifies us of the completion of a NCQ command 257012feb904SMatthew Dillon * and DBS does not. 25711980eff3SMatthew Dillon */ 257212feb904SMatthew Dillon if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) { 25731980eff3SMatthew Dillon u_int32_t data; 25741980eff3SMatthew Dillon 257512feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 257612feb904SMatthew Dillon AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS); 257712feb904SMatthew Dillon if (sc->sc_cap & AHCI_REG_CAP_SSNTF) { 25781980eff3SMatthew Dillon data = ahci_pread(ap, AHCI_PREG_SNTF); 2579cec07d75SMatthew Dillon if (data) { 258012feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 258112feb904SMatthew Dillon AHCI_PREG_IS_SDBS); 258212feb904SMatthew Dillon kprintf("%s: NOTIFY %08x\n", 258312feb904SMatthew Dillon PORTNAME(ap), data); 258412feb904SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, 258512feb904SMatthew Dillon AHCI_PREG_SERR_DIAG_N); 25863209f581SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SNTF, data); 25873209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 25881980eff3SMatthew Dillon } 25891980eff3SMatthew Dillon } 259012feb904SMatthew Dillon is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS); 259112feb904SMatthew Dillon } 25923209f581SMatthew Dillon 25933209f581SMatthew Dillon /* 2594492bffafSMatthew Dillon * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set. 2595f4553de1SMatthew Dillon * 25963209f581SMatthew Dillon * Spurious IFS errors can occur while we are doing a reset 2597492bffafSMatthew Dillon * sequence through a PM, probably due to an unexpected FIS 2598492bffafSMatthew Dillon * being received during the PM target reset sequence. Chipsets 2599492bffafSMatthew Dillon * are supposed to mask these events but some do not. 2600492bffafSMatthew Dillon * 2601492bffafSMatthew Dillon * Try to recover from the condition. 26023209f581SMatthew Dillon */ 26033209f581SMatthew Dillon if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) { 26041980eff3SMatthew Dillon u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR); 26053209f581SMatthew Dillon if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) { 2606492bffafSMatthew Dillon kprintf("%s: IFS during PM probe (ignored) " 2607492bffafSMatthew Dillon "IS=%b, SERR=%b\n", 26081980eff3SMatthew Dillon PORTNAME(ap), 26091980eff3SMatthew Dillon is, AHCI_PFMT_IS, 26101980eff3SMatthew Dillon serr, AHCI_PFMT_SERR); 26113209f581SMatthew Dillon ap->ap_flags |= AP_F_IFS_IGNORED; 26123209f581SMatthew Dillon } 2613492bffafSMatthew Dillon 2614492bffafSMatthew Dillon /* 2615492bffafSMatthew Dillon * Try to clear the error condition. The IFS error killed 2616492bffafSMatthew Dillon * the port so stop it so we can restart it. 2617492bffafSMatthew Dillon */ 26181980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 26191980eff3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS); 26201980eff3SMatthew Dillon is &= ~AHCI_PREG_IS_IFS; 2621492bffafSMatthew Dillon need = NEED_RESTART; 262212feb904SMatthew Dillon goto failall; 26231980eff3SMatthew Dillon } 2624258223a3SMatthew Dillon 2625258223a3SMatthew Dillon /* 2626f4553de1SMatthew Dillon * Port change (hot-plug) (blockable). 2627258223a3SMatthew Dillon * 2628492bffafSMatthew Dillon * A PRCS interrupt can occur: 2629492bffafSMatthew Dillon * (1) On hot-unplug / normal-unplug (phy lost) 2630492bffafSMatthew Dillon * (2) Sometimes on hot-plug too. 2631258223a3SMatthew Dillon * 2632492bffafSMatthew Dillon * A PCS interrupt can occur in a number of situations: 2633492bffafSMatthew Dillon * (1) On hot-plug once communication is established 2634492bffafSMatthew Dillon * (2) On hot-unplug sometimes. 2635492bffafSMatthew Dillon * (3) For chipsets with badly written firmware it can occur 2636492bffafSMatthew Dillon * during INIT/RESET sequences due to the device reset. 2637492bffafSMatthew Dillon * (4) For chipsets with badly written firmware it can occur 2638492bffafSMatthew Dillon * when it thinks an unsolicited COMRESET is received 2639492bffafSMatthew Dillon * during a INIT/RESET sequence, even though we actually 2640492bffafSMatthew Dillon * did request it. 2641258223a3SMatthew Dillon * 264222181ab7SMatthew Dillon * XXX We can then check the CPS (Cold Presence State) bit, if 264322181ab7SMatthew Dillon * supported, to determine if a device is plugged in or not and do 264422181ab7SMatthew Dillon * the right thing. 264522181ab7SMatthew Dillon * 2646492bffafSMatthew Dillon * PCS interrupts are cleared by clearing DIAG_X. If this occurs 2647492bffafSMatthew Dillon * command processing is automatically stopped (CR goes inactive) 2648492bffafSMatthew Dillon * and the port must be stopped and restarted. 2649492bffafSMatthew Dillon * 2650492bffafSMatthew Dillon * WARNING: AMD parts (e.g. 880G chipset, probably others) can 2651492bffafSMatthew Dillon * generate PCS on initialization even when device is 2652492bffafSMatthew Dillon * already connected up. It is unclear why this happens. 2653492bffafSMatthew Dillon * Depending on the state of the device detect this can 2654492bffafSMatthew Dillon * cause us to go into harsh reinit or hot-plug insertion 2655492bffafSMatthew Dillon * mode. 2656492bffafSMatthew Dillon * 2657492bffafSMatthew Dillon * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET 2658492bffafSMatthew Dillon * continues to flow in from the device), we must clear the 2659492bffafSMatthew Dillon * interrupt in all cases and enforce a delay to prevent 2660492bffafSMatthew Dillon * a livelock and give the port time to settle down. 2661492bffafSMatthew Dillon * Only print something if we aren't in INIT/HARD-RESET. 2662258223a3SMatthew Dillon */ 2663258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) { 2664492bffafSMatthew Dillon /* 2665492bffafSMatthew Dillon * Try to clear the error. Because of the repetitiveness 2666492bffafSMatthew Dillon * of this interrupt avoid any harsh action if the port is 2667492bffafSMatthew Dillon * already in the init or hard-reset probe state. 2668492bffafSMatthew Dillon */ 2669492bffafSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SERR, -1); 2670492bffafSMatthew Dillon /* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */ 2671cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2672cec07d75SMatthew Dillon is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)); 2673492bffafSMatthew Dillon 2674492bffafSMatthew Dillon if (ap->ap_probe == ATA_PROBE_NEED_INIT || 2675492bffafSMatthew Dillon ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) { 2676cec07d75SMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2677492bffafSMatthew Dillon need = NEED_NOTHING; 2678492bffafSMatthew Dillon ahci_os_sleep(1000); 2679492bffafSMatthew Dillon goto failall; 2680492bffafSMatthew Dillon } 2681492bffafSMatthew Dillon kprintf("%s: Transient Errors: %b (%d)\n", 2682492bffafSMatthew Dillon PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe); 2683492bffafSMatthew Dillon is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS); 2684492bffafSMatthew Dillon ahci_os_sleep(200); 2685492bffafSMatthew Dillon 2686492bffafSMatthew Dillon /* 2687492bffafSMatthew Dillon * Stop the port and figure out what to do next. 2688492bffafSMatthew Dillon */ 268922181ab7SMatthew Dillon ahci_port_stop(ap, 0); 2690492bffafSMatthew Dillon stopped = 1; 26910be9576aSMatthew Dillon 2692258223a3SMatthew Dillon switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) { 2693258223a3SMatthew Dillon case AHCI_PREG_SSTS_DET_DEV: 2694492bffafSMatthew Dillon /* 2695492bffafSMatthew Dillon * Device detect 2696492bffafSMatthew Dillon */ 269712feb904SMatthew Dillon if (ap->ap_probe == ATA_PROBE_FAILED) { 269822181ab7SMatthew Dillon need = NEED_HOTPLUG_INSERT; 269922181ab7SMatthew Dillon goto fatal; 2700258223a3SMatthew Dillon } 270122181ab7SMatthew Dillon need = NEED_RESTART; 2702258223a3SMatthew Dillon break; 2703492bffafSMatthew Dillon case AHCI_PREG_SSTS_DET_DEV_NE: 2704492bffafSMatthew Dillon /* 2705492bffafSMatthew Dillon * Device not communicating. AMD parts seem to 2706492bffafSMatthew Dillon * like to throw this error on initialization 2707492bffafSMatthew Dillon * for no reason that I can fathom. 2708492bffafSMatthew Dillon */ 2709492bffafSMatthew Dillon kprintf("%s: Device present but not communicating, " 2710492bffafSMatthew Dillon "attempting port restart\n", 2711492bffafSMatthew Dillon PORTNAME(ap)); 2712492bffafSMatthew Dillon need = NEED_REINIT; 2713492bffafSMatthew Dillon goto fatal; 2714258223a3SMatthew Dillon default: 27150be9576aSMatthew Dillon if (ap->ap_probe != ATA_PROBE_FAILED) { 271622181ab7SMatthew Dillon need = NEED_HOTPLUG_REMOVE; 271722181ab7SMatthew Dillon goto fatal; 2718258223a3SMatthew Dillon } 271922181ab7SMatthew Dillon need = NEED_RESTART; 2720258223a3SMatthew Dillon break; 2721258223a3SMatthew Dillon } 2722258223a3SMatthew Dillon } 2723258223a3SMatthew Dillon 272422181ab7SMatthew Dillon /* 2725f4553de1SMatthew Dillon * Check for remaining errors - they are fatal. (blockable) 272622181ab7SMatthew Dillon */ 2727258223a3SMatthew Dillon if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS | 2728258223a3SMatthew Dillon AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) { 2729cec07d75SMatthew Dillon u_int32_t serr; 2730cec07d75SMatthew Dillon 2731cec07d75SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2732cec07d75SMatthew Dillon is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2733cec07d75SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2734cec07d75SMatthew Dillon AHCI_PREG_IS_UFS)); 2735cec07d75SMatthew Dillon serr = ahci_pread(ap, AHCI_PREG_SERR); 2736831bc9e3SMatthew Dillon kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), " 27374444122dSMatthew Dillon "disabling port.\n", 27384444122dSMatthew Dillon PORTNAME(ap), 27394444122dSMatthew Dillon is, AHCI_PFMT_IS, 27401980eff3SMatthew Dillon serr, AHCI_PFMT_SERR 27414444122dSMatthew Dillon ); 2742831bc9e3SMatthew Dillon is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2743831bc9e3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2744831bc9e3SMatthew Dillon AHCI_PREG_IS_UFS); 2745492bffafSMatthew Dillon 2746492bffafSMatthew Dillon /* 2747492bffafSMatthew Dillon * Fail all commands but then what? For now try to 2748492bffafSMatthew Dillon * reinitialize the port. 2749492bffafSMatthew Dillon */ 2750492bffafSMatthew Dillon need = NEED_REINIT; 2751258223a3SMatthew Dillon goto fatal; 2752258223a3SMatthew Dillon } 2753258223a3SMatthew Dillon 275422181ab7SMatthew Dillon /* 275522181ab7SMatthew Dillon * Fail all outstanding commands if we know the port won't recover. 27561980eff3SMatthew Dillon * 27571980eff3SMatthew Dillon * We may have a ccb_at if the failed command is known and was 27581980eff3SMatthew Dillon * being sent to a device over a port multiplier (PM). In this 27591980eff3SMatthew Dillon * case if the port itself has not completely failed we fail just 27601980eff3SMatthew Dillon * the commands related to that target. 276112feb904SMatthew Dillon * 276212feb904SMatthew Dillon * ci_saved contains the mask of active commands as of when the 276312feb904SMatthew Dillon * error occured, prior to any port stops. 276422181ab7SMatthew Dillon */ 2765258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 2766258223a3SMatthew Dillon fatal: 2767258223a3SMatthew Dillon ap->ap_state = AP_S_FATAL_ERROR; 276812feb904SMatthew Dillon failall: 2769492bffafSMatthew Dillon ahci_port_stop(ap, 0); 2770492bffafSMatthew Dillon stopped = 1; 2771258223a3SMatthew Dillon 27721980eff3SMatthew Dillon /* 2773492bffafSMatthew Dillon * Error all the active slots not already errored. 27741980eff3SMatthew Dillon */ 277512feb904SMatthew Dillon ci_masked = ci_saved & *active & ~ap->ap_expired; 2776492bffafSMatthew Dillon if (ci_masked) { 2777492bffafSMatthew Dillon kprintf("%s: Failing all commands: %08x\n", 2778492bffafSMatthew Dillon PORTNAME(ap), ci_masked); 2779492bffafSMatthew Dillon } 2780492bffafSMatthew Dillon 2781258223a3SMatthew Dillon while (ci_masked) { 2782258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2783258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 278412feb904SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 278512feb904SMatthew Dillon ap->ap_expired |= 1 << slot; 278612feb904SMatthew Dillon ci_saved &= ~(1 << slot); 278712feb904SMatthew Dillon ci_masked &= ~(1 << slot); 27881980eff3SMatthew Dillon } 2789258223a3SMatthew Dillon 279012feb904SMatthew Dillon /* 279112feb904SMatthew Dillon * Clear bits in ci_saved (cause completions to be run) 279212feb904SMatthew Dillon * for all slots which are not active. 279312feb904SMatthew Dillon */ 2794258223a3SMatthew Dillon ci_saved &= ~*active; 2795258223a3SMatthew Dillon 2796258223a3SMatthew Dillon /* 2797258223a3SMatthew Dillon * Don't restart the port if our problems were deemed fatal. 2798258223a3SMatthew Dillon * 2799258223a3SMatthew Dillon * Also acknowlege all fatal interrupt sources to prevent 2800258223a3SMatthew Dillon * a livelock. 2801258223a3SMatthew Dillon */ 2802258223a3SMatthew Dillon if (ap->ap_state == AP_S_FATAL_ERROR) { 280322181ab7SMatthew Dillon if (need == NEED_RESTART) 280422181ab7SMatthew Dillon need = NEED_NOTHING; 2805258223a3SMatthew Dillon ahci_pwrite(ap, AHCI_PREG_IS, 2806258223a3SMatthew Dillon AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | 2807258223a3SMatthew Dillon AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS | 2808258223a3SMatthew Dillon AHCI_PREG_IS_UFS); 2809258223a3SMatthew Dillon } 2810258223a3SMatthew Dillon } 2811258223a3SMatthew Dillon 2812258223a3SMatthew Dillon /* 2813492bffafSMatthew Dillon * If we are stopped the AHCI chipset is supposed to have cleared 2814492bffafSMatthew Dillon * CI and SACT. Did it? If it didn't we try very hard to clear 2815492bffafSMatthew Dillon * the fields otherwise we may end up completing CCBs which are 2816492bffafSMatthew Dillon * actually still active. 2817492bffafSMatthew Dillon * 2818492bffafSMatthew Dillon * IFS errors on (at least) AMD chipsets create this confusion. 2819492bffafSMatthew Dillon */ 2820492bffafSMatthew Dillon if (stopped) { 2821492bffafSMatthew Dillon u_int32_t mask; 2822492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 2823492bffafSMatthew Dillon kprintf("%s: chipset failed to clear " 2824492bffafSMatthew Dillon "active cmds %08x\n", 2825492bffafSMatthew Dillon PORTNAME(ap), mask); 2826492bffafSMatthew Dillon ahci_port_start(ap); 2827492bffafSMatthew Dillon ahci_port_stop(ap, 0); 2828492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 2829492bffafSMatthew Dillon kprintf("%s: unable to prod the chip into " 2830492bffafSMatthew Dillon "clearing active cmds %08x\n", 2831492bffafSMatthew Dillon PORTNAME(ap), mask); 2832492bffafSMatthew Dillon /* what do we do now? */ 2833492bffafSMatthew Dillon } 2834492bffafSMatthew Dillon } 2835492bffafSMatthew Dillon } 2836492bffafSMatthew Dillon 2837492bffafSMatthew Dillon /* 2838f4553de1SMatthew Dillon * CCB completion (non blocking). 2839f4553de1SMatthew Dillon * 2840258223a3SMatthew Dillon * CCB completion is detected by noticing its slot's bit in CI has 2841258223a3SMatthew Dillon * changed to zero some time after we activated it. 2842258223a3SMatthew Dillon * If we are polling, we may only be interested in particular slot(s). 2843cf5f3a81SMatthew Dillon * 2844cf5f3a81SMatthew Dillon * Any active bits not saved are completed within the restrictions 2845cf5f3a81SMatthew Dillon * imposed by the caller. 2846258223a3SMatthew Dillon */ 28473209f581SMatthew Dillon ci_masked = ~ci_saved & *active; 2848258223a3SMatthew Dillon while (ci_masked) { 2849258223a3SMatthew Dillon slot = ffs(ci_masked) - 1; 2850258223a3SMatthew Dillon ccb = &ap->ap_ccbs[slot]; 2851258223a3SMatthew Dillon ci_masked &= ~(1 << slot); 2852258223a3SMatthew Dillon 2853258223a3SMatthew Dillon DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n", 2854258223a3SMatthew Dillon PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ? 2855258223a3SMatthew Dillon " (error)" : ""); 2856258223a3SMatthew Dillon 2857258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdh, 2858258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_list), 2859258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2860258223a3SMatthew Dillon 2861258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_cmdt, 2862258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_cmd_table), 2863258223a3SMatthew Dillon BUS_DMASYNC_POSTWRITE); 2864258223a3SMatthew Dillon 2865258223a3SMatthew Dillon bus_dmamap_sync(sc->sc_tag_rfis, 2866258223a3SMatthew Dillon AHCI_DMA_MAP(ap->ap_dmamem_rfis), 2867258223a3SMatthew Dillon BUS_DMASYNC_POSTREAD); 2868258223a3SMatthew Dillon 2869258223a3SMatthew Dillon *active &= ~(1 << ccb->ccb_slot); 28701980eff3SMatthew Dillon if (active == &ap->ap_active) { 28711980eff3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 28721980eff3SMatthew Dillon --ap->ap_active_cnt; 28731980eff3SMatthew Dillon } 28744c339a5fSMatthew Dillon 28754c339a5fSMatthew Dillon /* 28764c339a5fSMatthew Dillon * Complete the ccb. If the ccb was marked expired it 28774c339a5fSMatthew Dillon * was probably already removed from the command processor, 28784c339a5fSMatthew Dillon * so don't take the clear ci_saved bit as meaning the 28794c339a5fSMatthew Dillon * command actually succeeded, it didn't. 28804c339a5fSMatthew Dillon */ 28814c339a5fSMatthew Dillon if (ap->ap_expired & (1 << ccb->ccb_slot)) { 288276497a9cSMatthew Dillon ap->ap_expired &= ~(1 << ccb->ccb_slot); 28834c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 2884258223a3SMatthew Dillon ccb->ccb_done(ccb); 28854c339a5fSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 28864c339a5fSMatthew Dillon } else { 288712feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_ONCHIP) { 28884c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_COMPLETE; 288912feb904SMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) { 289012feb904SMatthew Dillon memcpy(&ccb->ccb_xa.rfis, 289112feb904SMatthew Dillon ap->ap_rfis->rfis, 289212feb904SMatthew Dillon sizeof(struct ata_fis_d2h)); 289312feb904SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_TIMEOUT) 289412feb904SMatthew Dillon ccb->ccb_xa.state = ATA_S_ERROR; 289512feb904SMatthew Dillon } 289612feb904SMatthew Dillon } 28974c339a5fSMatthew Dillon ccb->ccb_done(ccb); 28984c339a5fSMatthew Dillon } 2899258223a3SMatthew Dillon } 2900258223a3SMatthew Dillon 2901f4553de1SMatthew Dillon /* 2902f4553de1SMatthew Dillon * Cleanup. Will not be set if non-blocking. 2903f4553de1SMatthew Dillon */ 290422181ab7SMatthew Dillon switch(need) { 2905*f3de36f7SMatthew Dillon case NEED_NOTHING: 2906*f3de36f7SMatthew Dillon /* 2907*f3de36f7SMatthew Dillon * If operating normally and not stopped the interrupt was 2908*f3de36f7SMatthew Dillon * probably just a normal completion and we may be able to 2909*f3de36f7SMatthew Dillon * issue more commands. 2910*f3de36f7SMatthew Dillon */ 2911*f3de36f7SMatthew Dillon if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR) 2912*f3de36f7SMatthew Dillon ahci_issue_pending_commands(ap, NULL); 2913*f3de36f7SMatthew Dillon break; 291422181ab7SMatthew Dillon case NEED_RESTART: 291522181ab7SMatthew Dillon /* 291622181ab7SMatthew Dillon * A recoverable error occured and we can restart outstanding 291722181ab7SMatthew Dillon * commands on the port. 291822181ab7SMatthew Dillon */ 291912feb904SMatthew Dillon ci_saved &= ~ap->ap_expired; 2920258223a3SMatthew Dillon if (ci_saved) { 292112feb904SMatthew Dillon kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved); 29224c339a5fSMatthew Dillon ahci_issue_saved_commands(ap, ci_saved); 2923258223a3SMatthew Dillon } 2924492bffafSMatthew Dillon 2925492bffafSMatthew Dillon /* 2926492bffafSMatthew Dillon * Potentially issue new commands if not in a failed 2927492bffafSMatthew Dillon * state. 2928492bffafSMatthew Dillon */ 2929492bffafSMatthew Dillon if (ap->ap_state != AP_S_FATAL_ERROR) { 2930492bffafSMatthew Dillon ahci_port_start(ap); 2931492bffafSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 2932492bffafSMatthew Dillon } 2933492bffafSMatthew Dillon break; 2934492bffafSMatthew Dillon case NEED_REINIT: 2935492bffafSMatthew Dillon /* 2936492bffafSMatthew Dillon * Something horrible happened to the port and we 2937492bffafSMatthew Dillon * need to reinitialize it. 2938492bffafSMatthew Dillon */ 2939492bffafSMatthew Dillon kprintf("%s: REINIT - Attempting to reinitialize the port " 2940492bffafSMatthew Dillon "after it had a horrible accident\n", 2941492bffafSMatthew Dillon PORTNAME(ap)); 2942492bffafSMatthew Dillon ap->ap_flags |= AP_F_IN_RESET; 2943492bffafSMatthew Dillon ap->ap_flags |= AP_F_HARSH_REINIT; 2944492bffafSMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 2945492bffafSMatthew Dillon ahci_cam_changed(ap, NULL, -1); 294622181ab7SMatthew Dillon break; 294722181ab7SMatthew Dillon case NEED_HOTPLUG_INSERT: 294822181ab7SMatthew Dillon /* 2949cf5f3a81SMatthew Dillon * A hot-plug insertion event has occured and all 2950cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 29511980eff3SMatthew Dillon * 29521980eff3SMatthew Dillon * Don't recurse if this occurs while we are 29531980eff3SMatthew Dillon * resetting the port. 295422181ab7SMatthew Dillon */ 29551980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 295622181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device inserted\n", 295722181ab7SMatthew Dillon PORTNAME(ap)); 29583209f581SMatthew Dillon ap->ap_probe = ATA_PROBE_NEED_INIT; 29593209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 29601980eff3SMatthew Dillon } 296122181ab7SMatthew Dillon break; 296222181ab7SMatthew Dillon case NEED_HOTPLUG_REMOVE: 2963cf5f3a81SMatthew Dillon /* 2964cf5f3a81SMatthew Dillon * A hot-plug removal event has occured and all 2965cf5f3a81SMatthew Dillon * outstanding commands have already been revoked. 29661980eff3SMatthew Dillon * 29671980eff3SMatthew Dillon * Don't recurse if this occurs while we are 29681980eff3SMatthew Dillon * resetting the port. 2969cf5f3a81SMatthew Dillon */ 29701980eff3SMatthew Dillon if ((ap->ap_flags & AP_F_IN_RESET) == 0) { 297122181ab7SMatthew Dillon kprintf("%s: HOTPLUG - Device removed\n", 297222181ab7SMatthew Dillon PORTNAME(ap)); 2973cf5f3a81SMatthew Dillon ahci_port_hardstop(ap); 29743209f581SMatthew Dillon /* ap_probe set to failed */ 29753209f581SMatthew Dillon ahci_cam_changed(ap, NULL, -1); 29761980eff3SMatthew Dillon } 297722181ab7SMatthew Dillon break; 297822181ab7SMatthew Dillon default: 297922181ab7SMatthew Dillon break; 2980258223a3SMatthew Dillon } 2981258223a3SMatthew Dillon } 2982258223a3SMatthew Dillon 2983258223a3SMatthew Dillon struct ahci_ccb * 2984258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap) 2985258223a3SMatthew Dillon { 2986258223a3SMatthew Dillon struct ahci_ccb *ccb; 2987258223a3SMatthew Dillon 2988258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 2989258223a3SMatthew Dillon ccb = TAILQ_FIRST(&ap->ap_ccb_free); 2990258223a3SMatthew Dillon if (ccb != NULL) { 2991258223a3SMatthew Dillon KKASSERT(ccb->ccb_xa.state == ATA_S_PUT); 2992258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry); 2993258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_SETUP; 2994492bffafSMatthew Dillon ccb->ccb_xa.flags = 0; 29951980eff3SMatthew Dillon ccb->ccb_xa.at = NULL; 2996258223a3SMatthew Dillon } 2997258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 2998258223a3SMatthew Dillon 2999258223a3SMatthew Dillon return (ccb); 3000258223a3SMatthew Dillon } 3001258223a3SMatthew Dillon 3002258223a3SMatthew Dillon void 3003258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb) 3004258223a3SMatthew Dillon { 3005258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3006258223a3SMatthew Dillon 3007258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PUT; 3008258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE); 3009258223a3SMatthew Dillon TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry); 3010258223a3SMatthew Dillon lockmgr(&ap->ap_ccb_lock, LK_RELEASE); 3011258223a3SMatthew Dillon } 3012258223a3SMatthew Dillon 3013258223a3SMatthew Dillon struct ahci_ccb * 3014258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap) 3015258223a3SMatthew Dillon { 3016258223a3SMatthew Dillon struct ahci_ccb *err_ccb; 3017258223a3SMatthew Dillon u_int32_t sact; 3018b012a2caSMatthew Dillon u_int32_t ci; 3019258223a3SMatthew Dillon 3020258223a3SMatthew Dillon /* No commands may be active on the chip. */ 3021b012a2caSMatthew Dillon 3022b012a2caSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) { 3023258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 3024192ee1d0SMatthew Dillon if (sact != 0) { 3025192ee1d0SMatthew Dillon kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n", 3026192ee1d0SMatthew Dillon PORTNAME(ap), sact); 3027192ee1d0SMatthew Dillon } 3028b012a2caSMatthew Dillon } 3029b012a2caSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 3030b012a2caSMatthew Dillon if (ci) { 3031b012a2caSMatthew Dillon kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n", 3032b012a2caSMatthew Dillon ap->ap_name, ci); 3033b012a2caSMatthew Dillon } 3034b012a2caSMatthew Dillon KKASSERT(ci == 0); 3035baef7501SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0); 3036baef7501SMatthew Dillon ap->ap_flags |= AP_F_ERR_CCB_RESERVED; 3037258223a3SMatthew Dillon 3038258223a3SMatthew Dillon /* Save outstanding command state. */ 3039258223a3SMatthew Dillon ap->ap_err_saved_active = ap->ap_active; 3040258223a3SMatthew Dillon ap->ap_err_saved_active_cnt = ap->ap_active_cnt; 3041258223a3SMatthew Dillon ap->ap_err_saved_sactive = ap->ap_sactive; 3042258223a3SMatthew Dillon 3043258223a3SMatthew Dillon /* 3044258223a3SMatthew Dillon * Pretend we have no commands outstanding, so that completions won't 3045258223a3SMatthew Dillon * run prematurely. 3046258223a3SMatthew Dillon */ 3047258223a3SMatthew Dillon ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0; 3048258223a3SMatthew Dillon 3049258223a3SMatthew Dillon /* 3050258223a3SMatthew Dillon * Grab a CCB to use for error recovery. This should never fail, as 3051258223a3SMatthew Dillon * we ask atascsi to reserve one for us at init time. 3052258223a3SMatthew Dillon */ 30531067474aSMatthew Dillon err_ccb = ap->ap_err_ccb; 3054258223a3SMatthew Dillon KKASSERT(err_ccb != NULL); 3055258223a3SMatthew Dillon err_ccb->ccb_xa.flags = 0; 3056258223a3SMatthew Dillon err_ccb->ccb_done = ahci_empty_done; 3057258223a3SMatthew Dillon 3058258223a3SMatthew Dillon return err_ccb; 3059258223a3SMatthew Dillon } 3060258223a3SMatthew Dillon 3061258223a3SMatthew Dillon void 3062258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb) 3063258223a3SMatthew Dillon { 3064258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3065258223a3SMatthew Dillon u_int32_t sact; 30665f8c1efdSMatthew Dillon u_int32_t ci; 3067258223a3SMatthew Dillon 3068baef7501SMatthew Dillon KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0); 3069baef7501SMatthew Dillon 30705f8c1efdSMatthew Dillon /* 30715f8c1efdSMatthew Dillon * No commands may be active on the chip 30725f8c1efdSMatthew Dillon */ 3073b012a2caSMatthew Dillon if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) { 3074258223a3SMatthew Dillon sact = ahci_pread(ap, AHCI_PREG_SACT); 30755f8c1efdSMatthew Dillon if (sact) { 30765f8c1efdSMatthew Dillon panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n", 30775f8c1efdSMatthew Dillon ccb->ccb_slot, sact); 3078258223a3SMatthew Dillon } 3079b012a2caSMatthew Dillon } 30805f8c1efdSMatthew Dillon ci = ahci_pread(ap, AHCI_PREG_CI); 30815f8c1efdSMatthew Dillon if (ci) { 3082cf5f3a81SMatthew Dillon panic("ahci_put_err_ccb(%d) but CI %08x != 0 " 3083cf5f3a81SMatthew Dillon "(act=%08x sact=%08x)\n", 3084cf5f3a81SMatthew Dillon ccb->ccb_slot, ci, 3085cf5f3a81SMatthew Dillon ap->ap_active, ap->ap_sactive); 30865f8c1efdSMatthew Dillon } 3087258223a3SMatthew Dillon 30881067474aSMatthew Dillon KKASSERT(ccb == ap->ap_err_ccb); 3089258223a3SMatthew Dillon 3090258223a3SMatthew Dillon /* Restore outstanding command state */ 3091258223a3SMatthew Dillon ap->ap_sactive = ap->ap_err_saved_sactive; 3092258223a3SMatthew Dillon ap->ap_active_cnt = ap->ap_err_saved_active_cnt; 3093258223a3SMatthew Dillon ap->ap_active = ap->ap_err_saved_active; 3094258223a3SMatthew Dillon 3095baef7501SMatthew Dillon ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED; 3096258223a3SMatthew Dillon } 3097258223a3SMatthew Dillon 30981980eff3SMatthew Dillon /* 30991980eff3SMatthew Dillon * Read log page to get NCQ error. 31001980eff3SMatthew Dillon * 31011980eff3SMatthew Dillon * NOTE: NCQ not currently supported on port multipliers. XXX 31021980eff3SMatthew Dillon */ 3103258223a3SMatthew Dillon int 310412feb904SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int target) 3105258223a3SMatthew Dillon { 310612feb904SMatthew Dillon struct ata_log_page_10h *log; 3107258223a3SMatthew Dillon struct ahci_ccb *ccb; 3108e1014452SMatthew Dillon struct ahci_ccb *ccb2; 3109258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 3110258223a3SMatthew Dillon struct ata_fis_h2d *fis; 311112feb904SMatthew Dillon int err_slot; 3112258223a3SMatthew Dillon 311312feb904SMatthew Dillon if (bootverbose) { 311412feb904SMatthew Dillon kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap), 311512feb904SMatthew Dillon target); 311612feb904SMatthew Dillon } 3117258223a3SMatthew Dillon 311812feb904SMatthew Dillon /* 311912feb904SMatthew Dillon * Prep error CCB for READ LOG EXT, page 10h, 1 sector. 312012feb904SMatthew Dillon * 312112feb904SMatthew Dillon * Getting err_ccb clears active/sactive/active_cnt, putting 312212feb904SMatthew Dillon * it back restores the fields. 312312feb904SMatthew Dillon */ 3124258223a3SMatthew Dillon ccb = ahci_get_err_ccb(ap); 312512feb904SMatthew Dillon ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL; 3126258223a3SMatthew Dillon ccb->ccb_xa.data = ap->ap_err_scratch; 3127258223a3SMatthew Dillon ccb->ccb_xa.datalen = 512; 312812feb904SMatthew Dillon ccb->ccb_xa.complete = ahci_dummy_done; 3129b012a2caSMatthew Dillon ccb->ccb_xa.at = ap->ap_ata[target]; 3130258223a3SMatthew Dillon 3131258223a3SMatthew Dillon fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis; 313212feb904SMatthew Dillon bzero(fis, sizeof(*fis)); 3133258223a3SMatthew Dillon fis->type = ATA_FIS_TYPE_H2D; 313412feb904SMatthew Dillon fis->flags = ATA_H2D_FLAGS_CMD | target; 3135258223a3SMatthew Dillon fis->command = ATA_C_READ_LOG_EXT; 3136258223a3SMatthew Dillon fis->lba_low = 0x10; /* queued error log page (10h) */ 3137258223a3SMatthew Dillon fis->sector_count = 1; /* number of sectors (1) */ 3138258223a3SMatthew Dillon fis->sector_count_exp = 0; 3139258223a3SMatthew Dillon fis->lba_mid = 0; /* starting offset */ 3140258223a3SMatthew Dillon fis->lba_mid_exp = 0; 3141258223a3SMatthew Dillon fis->device = 0; 3142258223a3SMatthew Dillon 314312feb904SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 3144258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length: 5 DWORDS */ 3145258223a3SMatthew Dillon 3146258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) { 314712feb904SMatthew Dillon err_slot = -1; 3148258223a3SMatthew Dillon goto err; 3149258223a3SMatthew Dillon } 3150258223a3SMatthew Dillon 3151258223a3SMatthew Dillon ccb->ccb_xa.state = ATA_S_PENDING; 315212feb904SMatthew Dillon if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) { 315312feb904SMatthew Dillon err_slot = -1; 3154258223a3SMatthew Dillon ahci_unload_prdt(ccb); 315512feb904SMatthew Dillon goto err; 315612feb904SMatthew Dillon } 315712feb904SMatthew Dillon ahci_unload_prdt(ccb); 3158258223a3SMatthew Dillon 315912feb904SMatthew Dillon /* 316012feb904SMatthew Dillon * Success, extract failed register set and tags from the scratch 316112feb904SMatthew Dillon * space. 316212feb904SMatthew Dillon */ 3163258223a3SMatthew Dillon log = (struct ata_log_page_10h *)ap->ap_err_scratch; 3164258223a3SMatthew Dillon if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) { 3165258223a3SMatthew Dillon /* Not queued bit was set - wasn't an NCQ error? */ 316612feb904SMatthew Dillon kprintf("%s: read NCQ error page, but not an NCQ error?\n", 3167258223a3SMatthew Dillon PORTNAME(ap)); 316812feb904SMatthew Dillon err_slot = -1; 3169258223a3SMatthew Dillon } else { 3170258223a3SMatthew Dillon /* Copy back the log record as a D2H register FIS. */ 317112feb904SMatthew Dillon err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK; 3172258223a3SMatthew Dillon 3173e1014452SMatthew Dillon ccb2 = &ap->ap_ccbs[err_slot]; 3174e1014452SMatthew Dillon if (ccb2->ccb_xa.state == ATA_S_ONCHIP) { 317512feb904SMatthew Dillon kprintf("%s: read NCQ error page slot=%d\n", 3176e1014452SMatthew Dillon ATANAME(ap, ccb2->ccb_xa.at), 317712feb904SMatthew Dillon err_slot); 3178e1014452SMatthew Dillon memcpy(&ccb2->ccb_xa.rfis, &log->err_regs, 3179258223a3SMatthew Dillon sizeof(struct ata_fis_d2h)); 3180e1014452SMatthew Dillon ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H; 3181e1014452SMatthew Dillon ccb2->ccb_xa.rfis.flags = 0; 318212feb904SMatthew Dillon } else { 318312feb904SMatthew Dillon kprintf("%s: read NCQ error page slot=%d, " 318412feb904SMatthew Dillon "slot does not match any cmds\n", 3185e1014452SMatthew Dillon ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at), 318612feb904SMatthew Dillon err_slot); 318712feb904SMatthew Dillon err_slot = -1; 3188258223a3SMatthew Dillon } 3189258223a3SMatthew Dillon } 319012feb904SMatthew Dillon err: 319112feb904SMatthew Dillon ahci_put_err_ccb(ccb); 319212feb904SMatthew Dillon kprintf("%s: DONE log page target %d err_slot=%d\n", 319312feb904SMatthew Dillon PORTNAME(ap), target, err_slot); 319412feb904SMatthew Dillon return (err_slot); 3195258223a3SMatthew Dillon } 3196258223a3SMatthew Dillon 3197258223a3SMatthew Dillon /* 3198258223a3SMatthew Dillon * Allocate memory for various structures DMAd by hardware. The maximum 3199258223a3SMatthew Dillon * number of segments for these tags is 1 so the DMA memory will have a 3200258223a3SMatthew Dillon * single physical base address. 3201258223a3SMatthew Dillon */ 3202258223a3SMatthew Dillon struct ahci_dmamem * 3203258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag) 3204258223a3SMatthew Dillon { 3205258223a3SMatthew Dillon struct ahci_dmamem *adm; 3206258223a3SMatthew Dillon int error; 3207258223a3SMatthew Dillon 3208258223a3SMatthew Dillon adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO); 3209258223a3SMatthew Dillon 3210258223a3SMatthew Dillon error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva, 3211258223a3SMatthew Dillon BUS_DMA_ZERO, &adm->adm_map); 3212258223a3SMatthew Dillon if (error == 0) { 3213258223a3SMatthew Dillon adm->adm_tag = tag; 3214258223a3SMatthew Dillon error = bus_dmamap_load(tag, adm->adm_map, 3215258223a3SMatthew Dillon adm->adm_kva, 3216258223a3SMatthew Dillon bus_dma_tag_getmaxsize(tag), 3217258223a3SMatthew Dillon ahci_dmamem_saveseg, &adm->adm_busaddr, 3218258223a3SMatthew Dillon 0); 3219258223a3SMatthew Dillon } 3220258223a3SMatthew Dillon if (error) { 3221258223a3SMatthew Dillon if (adm->adm_map) { 3222258223a3SMatthew Dillon bus_dmamap_destroy(tag, adm->adm_map); 3223258223a3SMatthew Dillon adm->adm_map = NULL; 3224258223a3SMatthew Dillon adm->adm_tag = NULL; 3225258223a3SMatthew Dillon adm->adm_kva = NULL; 3226258223a3SMatthew Dillon } 3227258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 3228258223a3SMatthew Dillon adm = NULL; 3229258223a3SMatthew Dillon } 3230258223a3SMatthew Dillon return (adm); 3231258223a3SMatthew Dillon } 3232258223a3SMatthew Dillon 3233258223a3SMatthew Dillon static 3234258223a3SMatthew Dillon void 3235258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error) 3236258223a3SMatthew Dillon { 3237258223a3SMatthew Dillon KKASSERT(error == 0); 3238258223a3SMatthew Dillon KKASSERT(nsegs == 1); 3239258223a3SMatthew Dillon *(bus_addr_t *)info = segs->ds_addr; 3240258223a3SMatthew Dillon } 3241258223a3SMatthew Dillon 3242258223a3SMatthew Dillon 3243258223a3SMatthew Dillon void 3244258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm) 3245258223a3SMatthew Dillon { 3246258223a3SMatthew Dillon if (adm->adm_map) { 3247258223a3SMatthew Dillon bus_dmamap_unload(adm->adm_tag, adm->adm_map); 3248258223a3SMatthew Dillon bus_dmamap_destroy(adm->adm_tag, adm->adm_map); 3249258223a3SMatthew Dillon adm->adm_map = NULL; 3250258223a3SMatthew Dillon adm->adm_tag = NULL; 3251258223a3SMatthew Dillon adm->adm_kva = NULL; 3252258223a3SMatthew Dillon } 3253258223a3SMatthew Dillon kfree(adm, M_DEVBUF); 3254258223a3SMatthew Dillon } 3255258223a3SMatthew Dillon 3256258223a3SMatthew Dillon u_int32_t 3257258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r) 3258258223a3SMatthew Dillon { 3259258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 3260258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 3261258223a3SMatthew Dillon return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r)); 3262258223a3SMatthew Dillon } 3263258223a3SMatthew Dillon 3264258223a3SMatthew Dillon void 3265258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v) 3266258223a3SMatthew Dillon { 3267258223a3SMatthew Dillon bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v); 3268258223a3SMatthew Dillon bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4, 3269258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 3270258223a3SMatthew Dillon } 3271258223a3SMatthew Dillon 3272258223a3SMatthew Dillon u_int32_t 3273258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r) 3274258223a3SMatthew Dillon { 3275258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3276258223a3SMatthew Dillon BUS_SPACE_BARRIER_READ); 3277258223a3SMatthew Dillon return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r)); 3278258223a3SMatthew Dillon } 3279258223a3SMatthew Dillon 3280258223a3SMatthew Dillon void 3281258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v) 3282258223a3SMatthew Dillon { 3283258223a3SMatthew Dillon bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v); 3284258223a3SMatthew Dillon bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4, 3285258223a3SMatthew Dillon BUS_SPACE_BARRIER_WRITE); 3286258223a3SMatthew Dillon } 3287258223a3SMatthew Dillon 3288831bc9e3SMatthew Dillon /* 3289831bc9e3SMatthew Dillon * Wait up to (timeout) milliseconds for the masked port register to 3290831bc9e3SMatthew Dillon * match the target. 3291831bc9e3SMatthew Dillon * 3292831bc9e3SMatthew Dillon * Timeout is in milliseconds. 3293831bc9e3SMatthew Dillon */ 3294258223a3SMatthew Dillon int 3295cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout, 3296cec85a37SMatthew Dillon bus_size_t r, u_int32_t mask, u_int32_t target) 3297258223a3SMatthew Dillon { 3298831bc9e3SMatthew Dillon int t; 3299258223a3SMatthew Dillon 3300831bc9e3SMatthew Dillon /* 3301831bc9e3SMatthew Dillon * Loop hard up to 100uS 3302831bc9e3SMatthew Dillon */ 3303831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3304258223a3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3305258223a3SMatthew Dillon return (0); 3306831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3307258223a3SMatthew Dillon } 3308258223a3SMatthew Dillon 3309831bc9e3SMatthew Dillon do { 3310831bc9e3SMatthew Dillon timeout -= ahci_os_softsleep(); 3311831bc9e3SMatthew Dillon if ((ahci_pread(ap, r) & mask) == target) 3312831bc9e3SMatthew Dillon return (0); 3313831bc9e3SMatthew Dillon } while (timeout > 0); 3314831bc9e3SMatthew Dillon return (1); 3315831bc9e3SMatthew Dillon } 3316831bc9e3SMatthew Dillon 3317831bc9e3SMatthew Dillon int 3318831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask, 3319831bc9e3SMatthew Dillon u_int32_t target) 3320831bc9e3SMatthew Dillon { 3321831bc9e3SMatthew Dillon int t; 3322831bc9e3SMatthew Dillon 3323831bc9e3SMatthew Dillon /* 3324831bc9e3SMatthew Dillon * Loop hard up to 100uS 3325831bc9e3SMatthew Dillon */ 3326831bc9e3SMatthew Dillon for (t = 0; t < 100; ++t) { 3327831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3328831bc9e3SMatthew Dillon return (0); 3329831bc9e3SMatthew Dillon ahci_os_hardsleep(1); /* us */ 3330831bc9e3SMatthew Dillon } 3331831bc9e3SMatthew Dillon 3332831bc9e3SMatthew Dillon /* 3333831bc9e3SMatthew Dillon * And one millisecond the slow way 3334831bc9e3SMatthew Dillon */ 3335831bc9e3SMatthew Dillon t = 1000; 3336831bc9e3SMatthew Dillon do { 3337831bc9e3SMatthew Dillon t -= ahci_os_softsleep(); 3338831bc9e3SMatthew Dillon if ((ahci_read(sc, r) & mask) != target) 3339831bc9e3SMatthew Dillon return (0); 3340831bc9e3SMatthew Dillon } while (t > 0); 3341831bc9e3SMatthew Dillon 3342258223a3SMatthew Dillon return (1); 3343258223a3SMatthew Dillon } 3344258223a3SMatthew Dillon 3345831bc9e3SMatthew Dillon 33461980eff3SMatthew Dillon /* 33471980eff3SMatthew Dillon * Acquire an ata transfer. 33481980eff3SMatthew Dillon * 33491980eff3SMatthew Dillon * Pass a NULL at for direct-attached transfers, and a non-NULL at for 33501980eff3SMatthew Dillon * targets that go through the port multiplier. 33511980eff3SMatthew Dillon */ 3352258223a3SMatthew Dillon struct ata_xfer * 33531980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at) 3354258223a3SMatthew Dillon { 3355258223a3SMatthew Dillon struct ahci_ccb *ccb; 3356258223a3SMatthew Dillon 3357258223a3SMatthew Dillon ccb = ahci_get_ccb(ap); 3358258223a3SMatthew Dillon if (ccb == NULL) { 3359258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n", 3360258223a3SMatthew Dillon PORTNAME(ap)); 3361258223a3SMatthew Dillon return (NULL); 3362258223a3SMatthew Dillon } 3363258223a3SMatthew Dillon 3364258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n", 3365258223a3SMatthew Dillon PORTNAME(ap), ccb->ccb_slot); 3366258223a3SMatthew Dillon 33672cc2e845SMatthew Dillon bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis)); 33681980eff3SMatthew Dillon ccb->ccb_xa.at = at; 3369258223a3SMatthew Dillon ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D; 3370258223a3SMatthew Dillon 3371258223a3SMatthew Dillon return (&ccb->ccb_xa); 3372258223a3SMatthew Dillon } 3373258223a3SMatthew Dillon 3374258223a3SMatthew Dillon void 3375258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa) 3376258223a3SMatthew Dillon { 3377258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3378258223a3SMatthew Dillon 3379258223a3SMatthew Dillon DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot); 3380258223a3SMatthew Dillon 3381258223a3SMatthew Dillon ahci_put_ccb(ccb); 3382258223a3SMatthew Dillon } 3383258223a3SMatthew Dillon 3384258223a3SMatthew Dillon int 3385258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa) 3386258223a3SMatthew Dillon { 3387258223a3SMatthew Dillon struct ahci_ccb *ccb = (struct ahci_ccb *)xa; 3388258223a3SMatthew Dillon struct ahci_cmd_hdr *cmd_slot; 3389258223a3SMatthew Dillon 3390258223a3SMatthew Dillon KKASSERT(xa->state == ATA_S_SETUP); 3391258223a3SMatthew Dillon 3392258223a3SMatthew Dillon if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) 3393258223a3SMatthew Dillon goto failcmd; 3394258223a3SMatthew Dillon ccb->ccb_done = ahci_ata_cmd_done; 3395258223a3SMatthew Dillon 3396258223a3SMatthew Dillon cmd_slot = ccb->ccb_cmd_hdr; 3397258223a3SMatthew Dillon cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */ 33981980eff3SMatthew Dillon if (ccb->ccb_xa.at) { 33991980eff3SMatthew Dillon cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target << 34001980eff3SMatthew Dillon AHCI_CMD_LIST_FLAG_PMP_SHIFT); 34011980eff3SMatthew Dillon } 3402258223a3SMatthew Dillon 3403258223a3SMatthew Dillon if (xa->flags & ATA_F_WRITE) 3404258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); 3405258223a3SMatthew Dillon 3406258223a3SMatthew Dillon if (xa->flags & ATA_F_PACKET) 3407258223a3SMatthew Dillon cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A); 3408258223a3SMatthew Dillon 3409258223a3SMatthew Dillon if (ahci_load_prdt(ccb) != 0) 3410258223a3SMatthew Dillon goto failcmd; 3411258223a3SMatthew Dillon 3412258223a3SMatthew Dillon xa->state = ATA_S_PENDING; 3413258223a3SMatthew Dillon 3414831bc9e3SMatthew Dillon if (xa->flags & ATA_F_POLL) 3415831bc9e3SMatthew Dillon return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout)); 3416258223a3SMatthew Dillon 3417258223a3SMatthew Dillon crit_enter(); 3418f4553de1SMatthew Dillon KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0); 34193209f581SMatthew Dillon xa->flags |= ATA_F_TIMEOUT_DESIRED; 3420258223a3SMatthew Dillon ahci_start(ccb); 3421258223a3SMatthew Dillon crit_exit(); 3422831bc9e3SMatthew Dillon return (xa->state); 3423258223a3SMatthew Dillon 3424258223a3SMatthew Dillon failcmd: 3425258223a3SMatthew Dillon crit_enter(); 3426258223a3SMatthew Dillon xa->state = ATA_S_ERROR; 3427258223a3SMatthew Dillon xa->complete(xa); 3428258223a3SMatthew Dillon crit_exit(); 3429831bc9e3SMatthew Dillon return (ATA_S_ERROR); 3430258223a3SMatthew Dillon } 3431258223a3SMatthew Dillon 3432258223a3SMatthew Dillon void 3433258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb) 3434258223a3SMatthew Dillon { 3435258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3436258223a3SMatthew Dillon 3437831bc9e3SMatthew Dillon /* 3438831bc9e3SMatthew Dillon * NOTE: callout does not lock port and may race us modifying 3439831bc9e3SMatthew Dillon * the flags, so make sure its stopped. 3440831bc9e3SMatthew Dillon */ 3441258223a3SMatthew Dillon if (xa->flags & ATA_F_TIMEOUT_RUNNING) { 3442258223a3SMatthew Dillon callout_stop(&ccb->ccb_timeout); 3443831bc9e3SMatthew Dillon xa->flags &= ~ATA_F_TIMEOUT_RUNNING; 3444258223a3SMatthew Dillon } 3445f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED); 3446258223a3SMatthew Dillon 34474c339a5fSMatthew Dillon KKASSERT(xa->state != ATA_S_ONCHIP); 3448258223a3SMatthew Dillon ahci_unload_prdt(ccb); 3449258223a3SMatthew Dillon 3450258223a3SMatthew Dillon if (xa->state != ATA_S_TIMEOUT) 3451258223a3SMatthew Dillon xa->complete(xa); 3452258223a3SMatthew Dillon } 3453258223a3SMatthew Dillon 3454f4553de1SMatthew Dillon /* 3455f4553de1SMatthew Dillon * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags 3456f4553de1SMatthew Dillon * while the callout is runing. 3457f4553de1SMatthew Dillon * 3458f4553de1SMatthew Dillon * We can't safely get the port lock here or delay, we could block 3459f4553de1SMatthew Dillon * the callout thread. 3460f4553de1SMatthew Dillon */ 3461258223a3SMatthew Dillon static void 3462258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg) 3463258223a3SMatthew Dillon { 3464258223a3SMatthew Dillon struct ahci_ccb *ccb = arg; 3465258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3466258223a3SMatthew Dillon 3467f4553de1SMatthew Dillon ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING; 3468f4553de1SMatthew Dillon ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED; 3469f4553de1SMatthew Dillon ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT); 3470258223a3SMatthew Dillon } 3471258223a3SMatthew Dillon 34724c339a5fSMatthew Dillon /* 34734c339a5fSMatthew Dillon * Timeout code, typically called when the port command processor is running. 34744c339a5fSMatthew Dillon * 34754c339a5fSMatthew Dillon * We have to be very very careful here. We cannot stop the port unless 34764c339a5fSMatthew Dillon * CR is already clear or the only active commands remaining are timed-out 34774c339a5fSMatthew Dillon * ones. Otherwise stopping the port will race the command processor and 34784c339a5fSMatthew Dillon * we can lose events. While we can theoretically just restart everything 34794c339a5fSMatthew Dillon * that could result in a double-issue which will not work for ATAPI commands. 34804c339a5fSMatthew Dillon */ 34811980eff3SMatthew Dillon void 3482831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb) 3483258223a3SMatthew Dillon { 3484258223a3SMatthew Dillon struct ata_xfer *xa = &ccb->ccb_xa; 3485258223a3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 34864c339a5fSMatthew Dillon struct ata_port *at; 3487492bffafSMatthew Dillon u_int32_t ci_saved; 3488492bffafSMatthew Dillon u_int32_t mask; 34894c339a5fSMatthew Dillon int slot; 3490258223a3SMatthew Dillon 34914c339a5fSMatthew Dillon at = ccb->ccb_xa.at; 34924c339a5fSMatthew Dillon 34934c339a5fSMatthew Dillon kprintf("%s: CMD TIMEOUT state=%d slot=%d\n" 34944c339a5fSMatthew Dillon "\tcmd-reg 0x%b\n" 34954c339a5fSMatthew Dillon "\tsactive=%08x active=%08x expired=%08x\n" 349608fb24a7SMatthew Dillon "\t sact=%08x ci=%08x\n" 349708fb24a7SMatthew Dillon "\t STS=%b\n", 34984c339a5fSMatthew Dillon ATANAME(ap, at), 34994c339a5fSMatthew Dillon ccb->ccb_xa.state, ccb->ccb_slot, 3500258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD, 35014c339a5fSMatthew Dillon ap->ap_sactive, ap->ap_active, ap->ap_expired, 3502258223a3SMatthew Dillon ahci_pread(ap, AHCI_PREG_SACT), 350308fb24a7SMatthew Dillon ahci_pread(ap, AHCI_PREG_CI), 350408fb24a7SMatthew Dillon ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS 350508fb24a7SMatthew Dillon ); 350608fb24a7SMatthew Dillon 3507258223a3SMatthew Dillon 35089e145b23SMatthew Dillon /* 35099e145b23SMatthew Dillon * NOTE: Timeout will not be running if the command was polled. 35103209f581SMatthew Dillon * If we got here at least one of these flags should be set. 35119e145b23SMatthew Dillon */ 35123209f581SMatthew Dillon KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED | 35133209f581SMatthew Dillon ATA_F_TIMEOUT_RUNNING)); 3514f4553de1SMatthew Dillon xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED); 3515258223a3SMatthew Dillon 3516258223a3SMatthew Dillon if (ccb->ccb_xa.state == ATA_S_PENDING) { 3517258223a3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 35184c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 35194c339a5fSMatthew Dillon ccb->ccb_done(ccb); 35204c339a5fSMatthew Dillon xa->complete(xa); 35214c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 35224c339a5fSMatthew Dillon return; 35234c339a5fSMatthew Dillon } 35244c339a5fSMatthew Dillon if (ccb->ccb_xa.state != ATA_S_ONCHIP) { 35254c339a5fSMatthew Dillon kprintf("%s: Unexpected state during timeout: %d\n", 35264c339a5fSMatthew Dillon ATANAME(ap, at), ccb->ccb_xa.state); 35274c339a5fSMatthew Dillon return; 35284c339a5fSMatthew Dillon } 35294c339a5fSMatthew Dillon 35304c339a5fSMatthew Dillon /* 35314c339a5fSMatthew Dillon * Ok, we can only get this command off the chip if CR is inactive 35324c339a5fSMatthew Dillon * or if the only commands running on the chip are all expired. 35334c339a5fSMatthew Dillon * Otherwise we have to wait until the port is in a safe state. 35344c339a5fSMatthew Dillon * 35354c339a5fSMatthew Dillon * Do not set state here, it will cause polls to return when the 35364c339a5fSMatthew Dillon * ccb is not yet off the chip. 35374c339a5fSMatthew Dillon */ 35384c339a5fSMatthew Dillon ap->ap_expired |= 1 << ccb->ccb_slot; 35394c339a5fSMatthew Dillon 35404c339a5fSMatthew Dillon if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) && 35414c339a5fSMatthew Dillon (ap->ap_active | ap->ap_sactive) != ap->ap_expired) { 35424c339a5fSMatthew Dillon /* 35434c339a5fSMatthew Dillon * If using FBSS or NCQ we can't safely stop the port 35444c339a5fSMatthew Dillon * right now. 35454c339a5fSMatthew Dillon */ 35464c339a5fSMatthew Dillon kprintf("%s: Deferred timeout until its safe, slot %d\n", 35474c339a5fSMatthew Dillon ATANAME(ap, at), ccb->ccb_slot); 35484c339a5fSMatthew Dillon return; 35494c339a5fSMatthew Dillon } 35504c339a5fSMatthew Dillon 35514c339a5fSMatthew Dillon /* 35524c339a5fSMatthew Dillon * We can safely stop the port and process all expired ccb's, 35534c339a5fSMatthew Dillon * which will include our current ccb. 35544c339a5fSMatthew Dillon */ 35554c339a5fSMatthew Dillon ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) : 35564c339a5fSMatthew Dillon ahci_pread(ap, AHCI_PREG_CI); 35574c339a5fSMatthew Dillon ahci_port_stop(ap, 0); 35584c339a5fSMatthew Dillon 35594c339a5fSMatthew Dillon while (ap->ap_expired) { 35604c339a5fSMatthew Dillon slot = ffs(ap->ap_expired) - 1; 35614c339a5fSMatthew Dillon ap->ap_expired &= ~(1 << slot); 35624c339a5fSMatthew Dillon ci_saved &= ~(1 << slot); 35634c339a5fSMatthew Dillon ccb = &ap->ap_ccbs[slot]; 35644c339a5fSMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 35654c339a5fSMatthew Dillon if (ccb->ccb_xa.flags & ATA_F_NCQ) { 35664c339a5fSMatthew Dillon KKASSERT(ap->ap_sactive & (1 << slot)); 35674c339a5fSMatthew Dillon ap->ap_sactive &= ~(1 << slot); 35684c339a5fSMatthew Dillon } else { 35694c339a5fSMatthew Dillon KKASSERT(ap->ap_active & (1 << slot)); 35704c339a5fSMatthew Dillon ap->ap_active &= ~(1 << slot); 35711980eff3SMatthew Dillon --ap->ap_active_cnt; 35721980eff3SMatthew Dillon } 3573258223a3SMatthew Dillon ccb->ccb_done(ccb); 35744c339a5fSMatthew Dillon ccb->ccb_xa.complete(&ccb->ccb_xa); 3575258223a3SMatthew Dillon } 35764c339a5fSMatthew Dillon /* ccb invalid now */ 3577258223a3SMatthew Dillon 35784c339a5fSMatthew Dillon /* 35794c339a5fSMatthew Dillon * We can safely CLO the port to clear any BSY/DRQ, a case which 35804c339a5fSMatthew Dillon * can occur with port multipliers. This will unbrick the port 35814c339a5fSMatthew Dillon * and allow commands to other targets behind the PM continue. 35824c339a5fSMatthew Dillon * (FBSS). 35834c339a5fSMatthew Dillon * 35844c339a5fSMatthew Dillon * Finally, once the port has been restarted we can issue any 35854c339a5fSMatthew Dillon * previously saved pending commands, and run the port interrupt 35864c339a5fSMatthew Dillon * code to handle any completions which may have occured when 35874c339a5fSMatthew Dillon * we saved CI. 35884c339a5fSMatthew Dillon */ 35894c339a5fSMatthew Dillon if (ahci_pread(ap, AHCI_PREG_TFD) & 35904c339a5fSMatthew Dillon (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) { 35914c339a5fSMatthew Dillon kprintf("%s: Warning, issuing CLO after timeout\n", 35924c339a5fSMatthew Dillon ATANAME(ap, at)); 3593131be210SMatthew Dillon ahci_port_clo(ap); 35944c339a5fSMatthew Dillon } 3595131be210SMatthew Dillon ahci_port_start(ap); 3596492bffafSMatthew Dillon 3597492bffafSMatthew Dillon /* 3598492bffafSMatthew Dillon * We absolutely must make sure the chipset cleared activity on 3599492bffafSMatthew Dillon * all slots. This sometimes might not happen due to races with 3600492bffafSMatthew Dillon * a chipset interrupt which stops the port before we can manage 3601492bffafSMatthew Dillon * to. For some reason some chipsets don't clear the active 3602492bffafSMatthew Dillon * commands when we turn off CMD_ST after the chip has stopped 3603492bffafSMatthew Dillon * operations itself. 3604492bffafSMatthew Dillon */ 3605492bffafSMatthew Dillon if (ahci_pactive(ap) != 0) { 3606492bffafSMatthew Dillon ahci_port_stop(ap, 0); 3607492bffafSMatthew Dillon ahci_port_start(ap); 3608492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 3609492bffafSMatthew Dillon kprintf("%s: quick-timeout: chipset failed " 3610492bffafSMatthew Dillon "to clear active cmds %08x\n", 3611492bffafSMatthew Dillon PORTNAME(ap), mask); 3612492bffafSMatthew Dillon } 3613492bffafSMatthew Dillon } 36144c339a5fSMatthew Dillon ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired); 36154c339a5fSMatthew Dillon ahci_issue_pending_commands(ap, NULL); 36164c339a5fSMatthew Dillon ahci_port_intr(ap, 0); 36174c339a5fSMatthew Dillon } 36184c339a5fSMatthew Dillon 3619cf5f3a81SMatthew Dillon /* 36204c339a5fSMatthew Dillon * Issue a previously saved set of commands 3621cf5f3a81SMatthew Dillon */ 36224c339a5fSMatthew Dillon void 36234c339a5fSMatthew Dillon ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved) 36244c339a5fSMatthew Dillon { 36254c339a5fSMatthew Dillon if (ci_saved) { 36264c339a5fSMatthew Dillon KKASSERT(!((ap->ap_active & ci_saved) && 36274c339a5fSMatthew Dillon (ap->ap_sactive & ci_saved))); 36284c339a5fSMatthew Dillon KKASSERT((ci_saved & ap->ap_expired) == 0); 36294c339a5fSMatthew Dillon if (ap->ap_sactive & ci_saved) 36304c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved); 36314c339a5fSMatthew Dillon ahci_pwrite(ap, AHCI_PREG_CI, ci_saved); 3632131be210SMatthew Dillon } 3633258223a3SMatthew Dillon } 3634258223a3SMatthew Dillon 3635831bc9e3SMatthew Dillon /* 3636831bc9e3SMatthew Dillon * Used by the softreset, pmprobe, and read_ncq_error only, in very 3637831bc9e3SMatthew Dillon * specialized, controlled circumstances. 3638831bc9e3SMatthew Dillon * 3639831bc9e3SMatthew Dillon * Only one command may be pending. 3640831bc9e3SMatthew Dillon */ 3641831bc9e3SMatthew Dillon void 3642831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb) 3643831bc9e3SMatthew Dillon { 3644831bc9e3SMatthew Dillon struct ahci_port *ap = ccb->ccb_port; 3645492bffafSMatthew Dillon u_int32_t mask; 3646831bc9e3SMatthew Dillon 3647831bc9e3SMatthew Dillon switch (ccb->ccb_xa.state) { 3648831bc9e3SMatthew Dillon case ATA_S_PENDING: 3649831bc9e3SMatthew Dillon TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry); 3650831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3651831bc9e3SMatthew Dillon break; 3652831bc9e3SMatthew Dillon case ATA_S_ONCHIP: 3653492bffafSMatthew Dillon /* 3654492bffafSMatthew Dillon * We have to clear the command on-chip. 3655492bffafSMatthew Dillon */ 3656831bc9e3SMatthew Dillon KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) && 3657831bc9e3SMatthew Dillon ap->ap_sactive == 0); 3658831bc9e3SMatthew Dillon ahci_port_stop(ap, 0); 3659831bc9e3SMatthew Dillon ahci_port_start(ap); 3660492bffafSMatthew Dillon if (ahci_pactive(ap) != 0) { 3661492bffafSMatthew Dillon ahci_port_stop(ap, 0); 3662492bffafSMatthew Dillon ahci_port_start(ap); 3663492bffafSMatthew Dillon if ((mask = ahci_pactive(ap)) != 0) { 3664492bffafSMatthew Dillon kprintf("%s: quick-timeout: chipset failed " 3665492bffafSMatthew Dillon "to clear active cmds %08x\n", 3666492bffafSMatthew Dillon PORTNAME(ap), mask); 3667492bffafSMatthew Dillon } 3668492bffafSMatthew Dillon } 3669831bc9e3SMatthew Dillon 3670831bc9e3SMatthew Dillon ccb->ccb_xa.state = ATA_S_TIMEOUT; 3671831bc9e3SMatthew Dillon ap->ap_active &= ~(1 << ccb->ccb_slot); 3672831bc9e3SMatthew Dillon KKASSERT(ap->ap_active_cnt > 0); 3673831bc9e3SMatthew Dillon --ap->ap_active_cnt; 3674831bc9e3SMatthew Dillon break; 3675831bc9e3SMatthew Dillon default: 3676831bc9e3SMatthew Dillon panic("%s: ahci_quick_timeout: ccb in bad state %d", 3677831bc9e3SMatthew Dillon ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state); 3678831bc9e3SMatthew Dillon } 3679831bc9e3SMatthew Dillon } 3680831bc9e3SMatthew Dillon 368112feb904SMatthew Dillon static void 368212feb904SMatthew Dillon ahci_dummy_done(struct ata_xfer *xa) 368312feb904SMatthew Dillon { 368412feb904SMatthew Dillon } 368512feb904SMatthew Dillon 368612feb904SMatthew Dillon static void 3687258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb) 3688258223a3SMatthew Dillon { 3689258223a3SMatthew Dillon } 3690795adb22SMatthew Dillon 3691795adb22SMatthew Dillon int 3692492bffafSMatthew Dillon ahci_set_feature(struct ahci_port *ap, struct ata_port *atx, 3693492bffafSMatthew Dillon int feature, int enable) 3694795adb22SMatthew Dillon { 3695795adb22SMatthew Dillon struct ata_port *at; 3696795adb22SMatthew Dillon struct ata_xfer *xa; 3697795adb22SMatthew Dillon int error; 3698795adb22SMatthew Dillon 3699795adb22SMatthew Dillon at = atx ? atx : ap->ap_ata[0]; 3700795adb22SMatthew Dillon 3701795adb22SMatthew Dillon xa = ahci_ata_get_xfer(ap, atx); 3702795adb22SMatthew Dillon 3703795adb22SMatthew Dillon xa->fis->type = ATA_FIS_TYPE_H2D; 3704795adb22SMatthew Dillon xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; 3705795adb22SMatthew Dillon xa->fis->command = ATA_C_SET_FEATURES; 3706795adb22SMatthew Dillon xa->fis->features = enable ? ATA_C_SATA_FEATURE_ENA : 3707795adb22SMatthew Dillon ATA_C_SATA_FEATURE_DIS; 3708795adb22SMatthew Dillon xa->fis->sector_count = feature; 3709795adb22SMatthew Dillon xa->fis->control = ATA_FIS_CONTROL_4BIT; 3710795adb22SMatthew Dillon 3711795adb22SMatthew Dillon xa->complete = ahci_dummy_done; 3712795adb22SMatthew Dillon xa->datalen = 0; 3713795adb22SMatthew Dillon xa->flags = ATA_F_POLL; 3714795adb22SMatthew Dillon xa->timeout = 1000; 3715795adb22SMatthew Dillon 3716795adb22SMatthew Dillon if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) 3717795adb22SMatthew Dillon error = 0; 3718795adb22SMatthew Dillon else 3719795adb22SMatthew Dillon error = EIO; 3720795adb22SMatthew Dillon ahci_ata_put_xfer(xa); 3721795adb22SMatthew Dillon return(error); 3722795adb22SMatthew Dillon } 3723