xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision d90e4fd11dfe04d50a912628b090cd5d02cdea2d)
1258223a3SMatthew Dillon /*
2fb00c6edSMatthew Dillon  * (MPSAFE)
3fb00c6edSMatthew Dillon  *
4258223a3SMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
5258223a3SMatthew Dillon  *
6258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
7258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
8258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
9258223a3SMatthew Dillon  *
10258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17258223a3SMatthew Dillon  *
18258223a3SMatthew Dillon  *
19258223a3SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
20258223a3SMatthew Dillon  *
21258223a3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
22258223a3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
23258223a3SMatthew Dillon  *
24258223a3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
25258223a3SMatthew Dillon  * modification, are permitted provided that the following conditions
26258223a3SMatthew Dillon  * are met:
27258223a3SMatthew Dillon  *
28258223a3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
29258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
30258223a3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
31258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
32258223a3SMatthew Dillon  *    the documentation and/or other materials provided with the
33258223a3SMatthew Dillon  *    distribution.
34258223a3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
35258223a3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
36258223a3SMatthew Dillon  *    from this software without specific, prior written permission.
37258223a3SMatthew Dillon  *
38258223a3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39258223a3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40258223a3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41258223a3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
42258223a3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43258223a3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44258223a3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45258223a3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46258223a3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47258223a3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48258223a3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49258223a3SMatthew Dillon  * SUCH DAMAGE.
50258223a3SMatthew Dillon  *
51258223a3SMatthew Dillon  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
52258223a3SMatthew Dillon  */
53258223a3SMatthew Dillon 
54258223a3SMatthew Dillon #include "ahci.h"
55258223a3SMatthew Dillon 
56f4553de1SMatthew Dillon void	ahci_port_interrupt_enable(struct ahci_port *ap);
57258223a3SMatthew Dillon 
58258223a3SMatthew Dillon int	ahci_load_prdt(struct ahci_ccb *);
59258223a3SMatthew Dillon void	ahci_unload_prdt(struct ahci_ccb *);
60258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
61258223a3SMatthew Dillon 				    int nsegs, int error);
62258223a3SMatthew Dillon void	ahci_start(struct ahci_ccb *);
6317eab71eSMatthew Dillon int	ahci_port_softreset(struct ahci_port *ap);
641980eff3SMatthew Dillon int	ahci_port_hardreset(struct ahci_port *ap, int hard);
65cf5f3a81SMatthew Dillon void	ahci_port_hardstop(struct ahci_port *ap);
66258223a3SMatthew Dillon 
67831bc9e3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *);
68831bc9e3SMatthew Dillon void	ahci_check_active_timeouts(struct ahci_port *ap);
69258223a3SMatthew Dillon 
70831bc9e3SMatthew Dillon void	ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at);
71831bc9e3SMatthew Dillon void	ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at);
724c339a5fSMatthew Dillon void	ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb);
734c339a5fSMatthew Dillon void	ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t mask);
74258223a3SMatthew Dillon 
7512feb904SMatthew Dillon int	ahci_port_read_ncq_error(struct ahci_port *, int);
76258223a3SMatthew Dillon 
77258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
78258223a3SMatthew Dillon void	ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
79258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
80258223a3SMatthew Dillon 
8112feb904SMatthew Dillon static void ahci_dummy_done(struct ata_xfer *xa);
8212feb904SMatthew Dillon static void ahci_empty_done(struct ahci_ccb *ccb);
8312feb904SMatthew Dillon static void ahci_ata_cmd_done(struct ahci_ccb *ccb);
84492bffafSMatthew Dillon static u_int32_t ahci_pactive(struct ahci_port *ap);
85258223a3SMatthew Dillon 
86fd8bd957SMatthew Dillon /*
87fd8bd957SMatthew Dillon  * Initialize the global AHCI hardware.  This code does not set up any of
88fd8bd957SMatthew Dillon  * its ports.
89fd8bd957SMatthew Dillon  */
90258223a3SMatthew Dillon int
91258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc)
92258223a3SMatthew Dillon {
934b450139SMatthew Dillon 	u_int32_t	pi, pleft;
944b450139SMatthew Dillon 	u_int32_t	bios_cap, vers;
95831bc9e3SMatthew Dillon 	int		i;
96831bc9e3SMatthew Dillon 	struct ahci_port *ap;
97258223a3SMatthew Dillon 
98258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
99258223a3SMatthew Dillon 		ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
100258223a3SMatthew Dillon 
101b012a2caSMatthew Dillon 	/*
1024b450139SMatthew Dillon 	 * AHCI version.
1034b450139SMatthew Dillon 	 */
1044b450139SMatthew Dillon 	vers = ahci_read(sc, AHCI_REG_VS);
1054b450139SMatthew Dillon 
1064b450139SMatthew Dillon 	/*
107b012a2caSMatthew Dillon 	 * save BIOS initialised parameters, enable staggered spin up
108b012a2caSMatthew Dillon 	 */
1094b450139SMatthew Dillon 	bios_cap = ahci_read(sc, AHCI_REG_CAP);
1104b450139SMatthew Dillon 	bios_cap &= AHCI_REG_CAP_SMPS | AHCI_REG_CAP_SSS;
1114b450139SMatthew Dillon 
112258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
113258223a3SMatthew Dillon 
114831bc9e3SMatthew Dillon 	/*
115b012a2caSMatthew Dillon 	 * Unconditionally reset the controller, do not conditionalize on
116b012a2caSMatthew Dillon 	 * trying to figure it if it was previously active or not.
117b012a2caSMatthew Dillon 	 *
118b012a2caSMatthew Dillon 	 * NOTE: On AE before HR.  The AHCI-1.1 spec has a note in section
119b012a2caSMatthew Dillon 	 *	 5.2.2.1 regarding this.  HR should be set to 1 only after
120b012a2caSMatthew Dillon 	 *	 AE is set to 1.  The reset sequence will clear HR when
121b012a2caSMatthew Dillon 	 *	 it completes, and will also clear AE if SAM is 0.  AE must
122b012a2caSMatthew Dillon 	 *	 then be set again.  When SAM is 1 the AE bit typically reads
123b012a2caSMatthew Dillon 	 *	 as 1 (and is read-only).
124b012a2caSMatthew Dillon 	 *
125b012a2caSMatthew Dillon 	 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
126b012a2caSMatthew Dillon 	 *	 otherwise the writes will only be separated by a few
127b012a2caSMatthew Dillon 	 *	 nanoseconds.
128b012a2caSMatthew Dillon 	 *
129b012a2caSMatthew Dillon 	 * NOTE BRICKS (1)
130b012a2caSMatthew Dillon 	 *
131b012a2caSMatthew Dillon 	 *	If you have a port multiplier and it does not have a device
132b012a2caSMatthew Dillon 	 *	in target 0, and it probes normally, but a later operation
133b012a2caSMatthew Dillon 	 *	mis-probes a target behind that PM, it is possible for the
134b012a2caSMatthew Dillon 	 *	port to brick such that only (a) a power cycle of the host
135b012a2caSMatthew Dillon 	 *	or (b) placing a device in target 0 will fix the problem.
136b012a2caSMatthew Dillon 	 *	Power cycling the PM has no effect (it works fine on another
137b012a2caSMatthew Dillon 	 *	host port).  This issue is unrelated to CLO.
138b012a2caSMatthew Dillon 	 */
1394e21f4daSMatthew Dillon 	/*
1404e21f4daSMatthew Dillon 	 * Wait for any prior reset sequence to complete
1414e21f4daSMatthew Dillon 	 */
1424e21f4daSMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC,
1434e21f4daSMatthew Dillon 			 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
1444e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "Controller is stuck in reset\n");
1454e21f4daSMatthew Dillon 		return (1);
1464e21f4daSMatthew Dillon 	}
147b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
1484e21f4daSMatthew Dillon 	ahci_os_sleep(500);
149b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
150b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
1514e21f4daSMatthew Dillon 	ahci_os_sleep(500);
152b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
153b012a2caSMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC,
154b012a2caSMatthew Dillon 			 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
1554e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "unable to reset controller\n");
156b012a2caSMatthew Dillon 		return (1);
157b012a2caSMatthew Dillon 	}
1584e21f4daSMatthew Dillon 	if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
1594e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "AE did not auto-clear!\n");
1604e21f4daSMatthew Dillon 		ahci_write(sc, AHCI_REG_GHC, 0);
1614e21f4daSMatthew Dillon 		ahci_os_sleep(500);
1624e21f4daSMatthew Dillon 	}
163b012a2caSMatthew Dillon 
164b012a2caSMatthew Dillon 	/*
165b012a2caSMatthew Dillon 	 * Enable ahci (global interrupts disabled)
166b012a2caSMatthew Dillon 	 *
167b012a2caSMatthew Dillon 	 * Restore saved parameters.  Avoid pci transaction burst write
168b012a2caSMatthew Dillon 	 * by issuing dummy reads.
169b012a2caSMatthew Dillon 	 */
1704e21f4daSMatthew Dillon 	ahci_os_sleep(500);
171b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
172b012a2caSMatthew Dillon 	ahci_os_sleep(500);
173b012a2caSMatthew Dillon 
174b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
1754b450139SMatthew Dillon 
1764b450139SMatthew Dillon 	bios_cap |= AHCI_REG_CAP_SSS;
1774b450139SMatthew Dillon 	ahci_write(sc, AHCI_REG_CAP, ahci_read(sc, AHCI_REG_CAP) | bios_cap);
178b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_PI, pi);
179b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
180b012a2caSMatthew Dillon 
181b012a2caSMatthew Dillon 	/*
182b012a2caSMatthew Dillon 	 * Intel hocus pocus in case the BIOS has not set the chip up
183b012a2caSMatthew Dillon 	 * properly for AHCI operation.
184b012a2caSMatthew Dillon 	 */
185b012a2caSMatthew Dillon 	if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
186b012a2caSMatthew Dillon 	        if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
187b012a2caSMatthew Dillon 			device_printf(sc->sc_dev, "Intel hocus pocus\n");
188b012a2caSMatthew Dillon 		pci_write_config(sc->sc_dev, 0x92,
189b012a2caSMatthew Dillon 			     pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
190b012a2caSMatthew Dillon 	}
191b012a2caSMatthew Dillon 
192b012a2caSMatthew Dillon 	/*
193831bc9e3SMatthew Dillon 	 * This is a hack that currently does not appear to have
194831bc9e3SMatthew Dillon 	 * a significant effect, but I noticed the port registers
195831bc9e3SMatthew Dillon 	 * do not appear to be completely cleared after the host
196831bc9e3SMatthew Dillon 	 * controller is reset.
19712feb904SMatthew Dillon 	 *
19812feb904SMatthew Dillon 	 * Use a temporary ap structure so we can call ahci_pwrite().
1994e21f4daSMatthew Dillon 	 *
2004e21f4daSMatthew Dillon 	 * We must be sure to stop the port
201831bc9e3SMatthew Dillon 	 */
202831bc9e3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
203831bc9e3SMatthew Dillon 	ap->ap_sc = sc;
20412feb904SMatthew Dillon 	pleft = pi;
20512feb904SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; ++i) {
20612feb904SMatthew Dillon 		if (pleft == 0)
20712feb904SMatthew Dillon 			break;
208831bc9e3SMatthew Dillon 		if ((pi & (1 << i)) == 0)
209831bc9e3SMatthew Dillon 			continue;
210831bc9e3SMatthew Dillon 		if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
211831bc9e3SMatthew Dillon 		    AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
212831bc9e3SMatthew Dillon 			device_printf(sc->sc_dev, "can't map port\n");
213831bc9e3SMatthew Dillon 			return (1);
214831bc9e3SMatthew Dillon 		}
2154e21f4daSMatthew Dillon 		/*
2164e21f4daSMatthew Dillon 		 * NOTE!  Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
2174e21f4daSMatthew Dillon 		 *	  AHCI1.1 can brick the chipset.  Not only brick it,
2184e21f4daSMatthew Dillon 		 *	  but also crash the PC.  The bit seems unreliable
2194e21f4daSMatthew Dillon 		 *	  on AHCI1.2 as well.
2204e21f4daSMatthew Dillon 		 */
2214e21f4daSMatthew Dillon 		ahci_port_stop(ap, 1);
2224e21f4daSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
223831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
224831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
22512feb904SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
226831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
22712feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, -1);
22812feb904SMatthew Dillon 		sc->sc_portmask |= (1 << i);
22912feb904SMatthew Dillon 		pleft &= ~(1 << i);
230831bc9e3SMatthew Dillon 	}
23112feb904SMatthew Dillon 	sc->sc_numports = i;
232831bc9e3SMatthew Dillon 	kfree(ap, M_DEVBUF);
233831bc9e3SMatthew Dillon 
234258223a3SMatthew Dillon 	return (0);
235258223a3SMatthew Dillon }
236258223a3SMatthew Dillon 
237fd8bd957SMatthew Dillon /*
238fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
239fd8bd957SMatthew Dillon  */
240258223a3SMatthew Dillon int
241258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port)
242258223a3SMatthew Dillon {
243258223a3SMatthew Dillon 	struct ahci_port	*ap;
2441980eff3SMatthew Dillon 	struct ata_port		*at;
245258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
246258223a3SMatthew Dillon 	u_int64_t		dva;
247258223a3SMatthew Dillon 	u_int32_t		cmd;
24812feb904SMatthew Dillon 	u_int32_t		data;
249258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*hdr;
250258223a3SMatthew Dillon 	struct ahci_cmd_table	*table;
251258223a3SMatthew Dillon 	int	rc = ENOMEM;
252258223a3SMatthew Dillon 	int	error;
253258223a3SMatthew Dillon 	int	i;
254258223a3SMatthew Dillon 
255258223a3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
25612feb904SMatthew Dillon 	ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
257258223a3SMatthew Dillon 
258258223a3SMatthew Dillon 	ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
259258223a3SMatthew Dillon 		  device_get_name(sc->sc_dev),
260258223a3SMatthew Dillon 		  device_get_unit(sc->sc_dev),
261258223a3SMatthew Dillon 		  port);
262258223a3SMatthew Dillon 	sc->sc_ports[port] = ap;
263258223a3SMatthew Dillon 
2641980eff3SMatthew Dillon 	/*
2651980eff3SMatthew Dillon 	 * Allocate enough so we never have to reallocate, it makes
2661980eff3SMatthew Dillon 	 * it easier.
2671980eff3SMatthew Dillon 	 *
2681980eff3SMatthew Dillon 	 * ap_pmcount will be reduced by the scan if we encounter the
2691980eff3SMatthew Dillon 	 * port multiplier port prior to target 15.
270b012a2caSMatthew Dillon 	 *
271b012a2caSMatthew Dillon 	 * kmalloc power-of-2 allocations are guaranteed not to cross
272b012a2caSMatthew Dillon 	 * a page boundary.  Make sure the identify sub-structure in the
273b012a2caSMatthew Dillon 	 * at structure does not cross a page boundary, just in case the
274b012a2caSMatthew Dillon 	 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
2751980eff3SMatthew Dillon 	 */
276b012a2caSMatthew Dillon 	if (ap->ap_ata[0] == NULL) {
277b012a2caSMatthew Dillon 		int pw2;
278b012a2caSMatthew Dillon 
279b012a2caSMatthew Dillon 		for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
280b012a2caSMatthew Dillon 			;
2811980eff3SMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
282b012a2caSMatthew Dillon 			at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
283b012a2caSMatthew Dillon 			ap->ap_ata[i] = at;
2841980eff3SMatthew Dillon 			at->at_ahci_port = ap;
2851980eff3SMatthew Dillon 			at->at_target = i;
2863209f581SMatthew Dillon 			at->at_probe = ATA_PROBE_NEED_INIT;
287831bc9e3SMatthew Dillon 			at->at_features |= ATA_PORT_F_RESCAN;
2881980eff3SMatthew Dillon 			ksnprintf(at->at_name, sizeof(at->at_name),
2891980eff3SMatthew Dillon 				  "%s.%d", ap->ap_name, i);
2901980eff3SMatthew Dillon 		}
2911980eff3SMatthew Dillon 	}
292258223a3SMatthew Dillon 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
293258223a3SMatthew Dillon 	    AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
294258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
295258223a3SMatthew Dillon 			      "unable to create register window for port %d\n",
296258223a3SMatthew Dillon 			      port);
297258223a3SMatthew Dillon 		goto freeport;
298258223a3SMatthew Dillon 	}
299258223a3SMatthew Dillon 
300258223a3SMatthew Dillon 	ap->ap_sc = sc;
301258223a3SMatthew Dillon 	ap->ap_num = port;
3023209f581SMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_INIT;
303f17a0cedSMatthew Dillon 	ap->link_pwr_mgmt = AHCI_LINK_PWR_MGMT_NONE;
304f17a0cedSMatthew Dillon 	ap->sysctl_tree = NULL;
305258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_free);
306258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_pending);
307258223a3SMatthew Dillon 	lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
308258223a3SMatthew Dillon 
309258223a3SMatthew Dillon 	/* Disable port interrupts */
310258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
311831bc9e3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
312258223a3SMatthew Dillon 
31317eab71eSMatthew Dillon 	/*
31417eab71eSMatthew Dillon 	 * Sec 10.1.2 - deinitialise port if it is already running
31517eab71eSMatthew Dillon 	 */
316258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
3170be9576aSMatthew Dillon 	kprintf("%s: Caps %b\n", PORTNAME(ap), cmd, AHCI_PFMT_CMD);
3180be9576aSMatthew Dillon 
319258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
320258223a3SMatthew Dillon 		    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
321258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
322258223a3SMatthew Dillon 		int r;
323258223a3SMatthew Dillon 
324258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
325258223a3SMatthew Dillon 		if (r) {
326258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
327258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
328258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
329258223a3SMatthew Dillon 			rc = ENXIO;
330258223a3SMatthew Dillon 			goto freeport;
331258223a3SMatthew Dillon 		}
332258223a3SMatthew Dillon 
333258223a3SMatthew Dillon 		/* Write DET to zero */
334cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
335258223a3SMatthew Dillon 	}
336258223a3SMatthew Dillon 
337258223a3SMatthew Dillon 	/* Allocate RFIS */
338258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
339258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
340cf5f3a81SMatthew Dillon 		kprintf("%s: NORFIS\n", PORTNAME(ap));
341258223a3SMatthew Dillon 		goto nomem;
342258223a3SMatthew Dillon 	}
343258223a3SMatthew Dillon 
344258223a3SMatthew Dillon 	/* Setup RFIS base address */
345258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
346258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
347258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
348258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
349258223a3SMatthew Dillon 
350831bc9e3SMatthew Dillon 	/* Clear SERR before starting FIS reception or ST or anything */
351831bc9e3SMatthew Dillon 	ahci_flush_tfd(ap);
352831bc9e3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
353831bc9e3SMatthew Dillon 
354258223a3SMatthew Dillon 	/* Enable FIS reception and activate port. */
355258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
3561980eff3SMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
357258223a3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
358258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
359258223a3SMatthew Dillon 
360258223a3SMatthew Dillon 	/* Check whether port activated.  Skip it if not. */
361258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
362258223a3SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
363cf5f3a81SMatthew Dillon 		kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
364258223a3SMatthew Dillon 		rc = ENXIO;
365258223a3SMatthew Dillon 		goto freeport;
366258223a3SMatthew Dillon 	}
367258223a3SMatthew Dillon 
368258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
369258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
370258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
371258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
372258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
373258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
374258223a3SMatthew Dillon 			      port);
375258223a3SMatthew Dillon 		goto freeport;
376258223a3SMatthew Dillon 	}
377258223a3SMatthew Dillon 
378258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
379258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
380258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
381258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
382258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
383258223a3SMatthew Dillon nomem:
384258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
385258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
386258223a3SMatthew Dillon 			      port);
387258223a3SMatthew Dillon 		goto freeport;
388258223a3SMatthew Dillon 	}
389258223a3SMatthew Dillon 
390258223a3SMatthew Dillon 	/* Setup command list base address */
391258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
392258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
393258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
394258223a3SMatthew Dillon 
395258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
396258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
397258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
398258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
399258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
400258223a3SMatthew Dillon 
401258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
402258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
403258223a3SMatthew Dillon 		if (error) {
404258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
405258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
406258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
407258223a3SMatthew Dillon 			goto freeport;
408258223a3SMatthew Dillon 		}
409258223a3SMatthew Dillon 
410bf0ecf68SMatthew Dillon 		callout_init_mp(&ccb->ccb_timeout);
411258223a3SMatthew Dillon 		ccb->ccb_slot = i;
412258223a3SMatthew Dillon 		ccb->ccb_port = ap;
413258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
414258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
415258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
416258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
417258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
418258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
419258223a3SMatthew Dillon 
420258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
421258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
422258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
423258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
424258223a3SMatthew Dillon 
425258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
4261067474aSMatthew Dillon 
4271067474aSMatthew Dillon 		/*
4281067474aSMatthew Dillon 		 * CCB[1] is the error CCB and is not get or put.  It is
4291067474aSMatthew Dillon 		 * also used for probing.  Numerous HBAs only load the
4301067474aSMatthew Dillon 		 * signature from CCB[1] so it MUST be used for the second
4311067474aSMatthew Dillon 		 * FIS.
4321067474aSMatthew Dillon 		 */
4331067474aSMatthew Dillon 		if (i == 1)
4341067474aSMatthew Dillon 			ap->ap_err_ccb = ccb;
4351067474aSMatthew Dillon 		else
436258223a3SMatthew Dillon 			ahci_put_ccb(ccb);
437258223a3SMatthew Dillon 	}
438258223a3SMatthew Dillon 
43912feb904SMatthew Dillon 	/*
44012feb904SMatthew Dillon 	 * Wait for ICC change to complete
44112feb904SMatthew Dillon 	 */
442258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
443258223a3SMatthew Dillon 
444fd8bd957SMatthew Dillon 	/*
44512feb904SMatthew Dillon 	 * Calculate the interrupt mask
44612feb904SMatthew Dillon 	 */
44712feb904SMatthew Dillon 	data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
44812feb904SMatthew Dillon 	       AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
44912feb904SMatthew Dillon 	       AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
45012feb904SMatthew Dillon 	       AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
45112feb904SMatthew Dillon 	       AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
45212feb904SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
45312feb904SMatthew Dillon 		data |= AHCI_PREG_IE_IPME;
45412feb904SMatthew Dillon #ifdef AHCI_COALESCE
45512feb904SMatthew Dillon 	if (sc->sc_ccc_ports & (1 << port)
45612feb904SMatthew Dillon 		data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
45712feb904SMatthew Dillon #endif
45812feb904SMatthew Dillon 	ap->ap_intmask = data;
45912feb904SMatthew Dillon 
46012feb904SMatthew Dillon 	/*
461e8cf3f55SMatthew Dillon 	 * Start the port helper thread.  The helper thread will call
462e8cf3f55SMatthew Dillon 	 * ahci_port_init() so the ports can all be started in parallel.
463e8cf3f55SMatthew Dillon 	 * A failure by ahci_port_init() does not deallocate the port
464e8cf3f55SMatthew Dillon 	 * since we still want hot-plug events.
465fd8bd957SMatthew Dillon 	 */
466f4553de1SMatthew Dillon 	ahci_os_start_port(ap);
467fd8bd957SMatthew Dillon 	return(0);
468fd8bd957SMatthew Dillon freeport:
469fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
470fd8bd957SMatthew Dillon 	return (rc);
471fd8bd957SMatthew Dillon }
472fd8bd957SMatthew Dillon 
473fd8bd957SMatthew Dillon /*
474492bffafSMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.  (from port thread)
475fd8bd957SMatthew Dillon  *
476fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
477fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
478fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
479fd8bd957SMatthew Dillon  *
48022181ab7SMatthew Dillon  * This function is the only way to move a failed port back to active
48122181ab7SMatthew Dillon  * status.
48222181ab7SMatthew Dillon  *
483fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
484fd8bd957SMatthew Dillon  */
485fd8bd957SMatthew Dillon int
48612feb904SMatthew Dillon ahci_port_init(struct ahci_port *ap)
487fd8bd957SMatthew Dillon {
488492bffafSMatthew Dillon 	u_int32_t cmd;
489e8cf3f55SMatthew Dillon 
490e8cf3f55SMatthew Dillon 	/*
491492bffafSMatthew Dillon 	 * Register [re]initialization
492492bffafSMatthew Dillon 	 *
493f17a0cedSMatthew Dillon 	 * Flush the TFD and SERR and make sure the port is stopped before
494f17a0cedSMatthew Dillon 	 * enabling its interrupt.  We no longer cycle the port start as
495f17a0cedSMatthew Dillon 	 * the port should not be started unless a device is present.
496f17a0cedSMatthew Dillon 	 *
497f17a0cedSMatthew Dillon 	 * XXX should we enable FIS reception? (FRE)?
498e8cf3f55SMatthew Dillon 	 */
499492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
500492bffafSMatthew Dillon 	ahci_port_stop(ap, 0);
501492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
502492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
503f17a0cedSMatthew Dillon 	ahci_flush_tfd(ap);
504f17a0cedSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
505492bffafSMatthew Dillon 
506492bffafSMatthew Dillon 	/*
507493d3201SMatthew Dillon 	 * If we are being harsh try to kill the port completely.  Normally
508493d3201SMatthew Dillon 	 * we would want to hold on to some of the state the BIOS may have
509493d3201SMatthew Dillon 	 * set, such as SUD (spin up device).
510492bffafSMatthew Dillon 	 *
511492bffafSMatthew Dillon 	 * AP_F_HARSH_REINIT is cleared in the hard reset state
512492bffafSMatthew Dillon 	 */
513492bffafSMatthew Dillon 	if (ap->ap_flags & AP_F_HARSH_REINIT) {
514492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
515492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
516492bffafSMatthew Dillon 
517492bffafSMatthew Dillon 		ahci_os_sleep(1000);
518492bffafSMatthew Dillon 
519492bffafSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
520492bffafSMatthew Dillon 		cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
521492bffafSMatthew Dillon 		cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD |
522492bffafSMatthew Dillon 		       AHCI_PREG_CMD_SUD;
523492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
524492bffafSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
525492bffafSMatthew Dillon 		if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
526492bffafSMatthew Dillon 			kprintf("%s: Warning: FRE did not come up during "
527492bffafSMatthew Dillon 				"harsh reinitialization\n",
528492bffafSMatthew Dillon 				PORTNAME(ap));
529492bffafSMatthew Dillon 		}
530492bffafSMatthew Dillon 		ahci_os_sleep(1000);
531492bffafSMatthew Dillon 	}
532492bffafSMatthew Dillon 
533492bffafSMatthew Dillon 	/*
534492bffafSMatthew Dillon 	 * Clear any pending garbage and re-enable the interrupt before
535492bffafSMatthew Dillon 	 * going to the next stage.
536492bffafSMatthew Dillon 	 */
537492bffafSMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
538492bffafSMatthew Dillon 	ap->ap_pmcount = 0;
539492bffafSMatthew Dillon 
540492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
541492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
542492bffafSMatthew Dillon 	ahci_flush_tfd(ap);
543492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
544492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, -1);
545492bffafSMatthew Dillon 
546f4553de1SMatthew Dillon 	ahci_port_interrupt_enable(ap);
547492bffafSMatthew Dillon 
54812feb904SMatthew Dillon 	return (0);
549f4553de1SMatthew Dillon }
550f4553de1SMatthew Dillon 
551f4553de1SMatthew Dillon /*
552f4553de1SMatthew Dillon  * Enable or re-enable interrupts on a port.
553f4553de1SMatthew Dillon  *
554f4553de1SMatthew Dillon  * This routine is called from the port initialization code or from the
555f4553de1SMatthew Dillon  * helper thread as the real interrupt may be forced to turn off certain
556f4553de1SMatthew Dillon  * interrupt sources.
557f4553de1SMatthew Dillon  */
558f4553de1SMatthew Dillon void
559f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap)
560f4553de1SMatthew Dillon {
56112feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
5621980eff3SMatthew Dillon }
563258223a3SMatthew Dillon 
564fd8bd957SMatthew Dillon /*
565f5caeaa0SMatthew Dillon  * Manage the agressive link power management capability.
566f17a0cedSMatthew Dillon  */
567f17a0cedSMatthew Dillon void
568f17a0cedSMatthew Dillon ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
569f17a0cedSMatthew Dillon {
570f17a0cedSMatthew Dillon 	u_int32_t cmd, sctl;
571f17a0cedSMatthew Dillon 
572f17a0cedSMatthew Dillon 	if (link_pwr_mgmt == ap->link_pwr_mgmt)
573f17a0cedSMatthew Dillon 		return;
574f17a0cedSMatthew Dillon 
575f17a0cedSMatthew Dillon 	if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
576f17a0cedSMatthew Dillon 		kprintf("%s: link power management not supported.\n",
577f17a0cedSMatthew Dillon 			PORTNAME(ap));
578f17a0cedSMatthew Dillon 		return;
579f17a0cedSMatthew Dillon 	}
580f17a0cedSMatthew Dillon 
581f17a0cedSMatthew Dillon 	ahci_os_lock_port(ap);
582f17a0cedSMatthew Dillon 
583f17a0cedSMatthew Dillon 	if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
584f17a0cedSMatthew Dillon 	    (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
585f17a0cedSMatthew Dillon 		kprintf("%s: enabling aggressive link power management.\n",
586f17a0cedSMatthew Dillon 			PORTNAME(ap));
587f17a0cedSMatthew Dillon 
588795adb22SMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
589795adb22SMatthew Dillon 
590f17a0cedSMatthew Dillon 		ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
591f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
592f17a0cedSMatthew Dillon 
593f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
594f17a0cedSMatthew Dillon 		sctl &= ~(AHCI_PREG_SCTL_IPM_DISABLED);
595f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
596f17a0cedSMatthew Dillon 
597795adb22SMatthew Dillon 		/*
598795adb22SMatthew Dillon 		 * Enable device initiated link power management for
599795adb22SMatthew Dillon 		 * directly attached devices that support it.
600795adb22SMatthew Dillon 		 */
601795adb22SMatthew Dillon 		if (ap->ap_type != ATA_PORT_T_PM &&
602750495d0SImre Vadász 		    (ap->ap_ata[0]->at_identify.satafsup &
603750495d0SImre Vadász 		    SATA_FEATURE_SUP_DEVIPS)) {
604795adb22SMatthew Dillon 			if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
605795adb22SMatthew Dillon 				kprintf("%s: Could not enable device initiated "
606795adb22SMatthew Dillon 				    "link power management.\n",
607795adb22SMatthew Dillon 				    PORTNAME(ap));
608795adb22SMatthew Dillon 		}
609795adb22SMatthew Dillon 
610f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
611f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ASP;
612f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ALPE;
613f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
614f17a0cedSMatthew Dillon 
615f17a0cedSMatthew Dillon 	} else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
616f17a0cedSMatthew Dillon 	           (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
617f17a0cedSMatthew Dillon 		kprintf("%s: enabling medium link power management.\n",
618f17a0cedSMatthew Dillon 			PORTNAME(ap));
619f17a0cedSMatthew Dillon 
620795adb22SMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
621795adb22SMatthew Dillon 
622f17a0cedSMatthew Dillon 		ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
623f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
624f17a0cedSMatthew Dillon 
625f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
626795adb22SMatthew Dillon 		sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
627795adb22SMatthew Dillon 		sctl &= ~AHCI_PREG_SCTL_IPM_NOPARTIAL;
628f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
629f17a0cedSMatthew Dillon 
630f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
631f17a0cedSMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_ASP;
632f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ALPE;
633f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
634f17a0cedSMatthew Dillon 
635f17a0cedSMatthew Dillon 	} else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
636f17a0cedSMatthew Dillon 		kprintf("%s: disabling link power management.\n",
637f17a0cedSMatthew Dillon 			PORTNAME(ap));
638f17a0cedSMatthew Dillon 
639795adb22SMatthew Dillon 		/* Disable device initiated link power management */
640795adb22SMatthew Dillon 		if (ap->ap_type != ATA_PORT_T_PM &&
641750495d0SImre Vadász 		    (ap->ap_ata[0]->at_identify.satafsup &
642750495d0SImre Vadász 		    SATA_FEATURE_SUP_DEVIPS)) {
643795adb22SMatthew Dillon 			ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
644750495d0SImre Vadász 		}
645795adb22SMatthew Dillon 
646f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
647f17a0cedSMatthew Dillon 		cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
648f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
649f17a0cedSMatthew Dillon 
650f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
651f17a0cedSMatthew Dillon 		sctl |= AHCI_PREG_SCTL_IPM_DISABLED;
652f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
653f17a0cedSMatthew Dillon 
654f17a0cedSMatthew Dillon 		/* let the drive come back to avoid PRCS interrupts later */
655f17a0cedSMatthew Dillon 		ahci_os_unlock_port(ap);
656f17a0cedSMatthew Dillon 		ahci_os_sleep(1000);
657f17a0cedSMatthew Dillon 		ahci_os_lock_port(ap);
658f17a0cedSMatthew Dillon 
659795adb22SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
660795adb22SMatthew Dillon 			    AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
661f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
662f17a0cedSMatthew Dillon 
663f17a0cedSMatthew Dillon 		ap->ap_intmask |= AHCI_PREG_IE_PRCE;
664f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
665f17a0cedSMatthew Dillon 
666f17a0cedSMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
667f17a0cedSMatthew Dillon 	} else {
668f17a0cedSMatthew Dillon 		kprintf("%s: unsupported link power management state %d.\n",
669f17a0cedSMatthew Dillon 			PORTNAME(ap), link_pwr_mgmt);
670f17a0cedSMatthew Dillon 	}
671f17a0cedSMatthew Dillon 
672f17a0cedSMatthew Dillon 	ahci_os_unlock_port(ap);
673f17a0cedSMatthew Dillon }
674f17a0cedSMatthew Dillon 
675795adb22SMatthew Dillon /*
676795adb22SMatthew Dillon  * Return current link power state.
677795adb22SMatthew Dillon  */
678795adb22SMatthew Dillon int
679795adb22SMatthew Dillon ahci_port_link_pwr_state(struct ahci_port *ap)
680795adb22SMatthew Dillon {
681795adb22SMatthew Dillon 	uint32_t r;
682795adb22SMatthew Dillon 
683795adb22SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SSTS);
684*d90e4fd1SImre Vadász 	switch (r & AHCI_PREG_SSTS_IPM) {
685*d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_ACTIVE:
686795adb22SMatthew Dillon 		return 1;
687*d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_PARTIAL:
688795adb22SMatthew Dillon 		return 2;
689*d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_SLUMBER:
690795adb22SMatthew Dillon 		return 3;
691*d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_DEVSLEEP:
692*d90e4fd1SImre Vadász 		return 4;
693795adb22SMatthew Dillon 	default:
694795adb22SMatthew Dillon 		return 0;
695795adb22SMatthew Dillon 	}
696795adb22SMatthew Dillon }
697f17a0cedSMatthew Dillon 
698f17a0cedSMatthew Dillon /*
6993209f581SMatthew Dillon  * Run the port / target state machine from a main context.
7003209f581SMatthew Dillon  *
7013209f581SMatthew Dillon  * The state machine for the port is always run.
7023209f581SMatthew Dillon  *
7033209f581SMatthew Dillon  * If atx is non-NULL run the state machine for a particular target.
7043209f581SMatthew Dillon  * If atx is NULL run the state machine for all targets.
7053209f581SMatthew Dillon  */
7063209f581SMatthew Dillon void
707831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial)
7083209f581SMatthew Dillon {
7093209f581SMatthew Dillon 	struct ata_port *at;
7103209f581SMatthew Dillon 	u_int32_t data;
7113209f581SMatthew Dillon 	int target;
7123209f581SMatthew Dillon 	int didsleep;
713831bc9e3SMatthew Dillon 	int loop;
7143209f581SMatthew Dillon 
715831bc9e3SMatthew Dillon 	/*
716831bc9e3SMatthew Dillon 	 * State machine for port.  Note that CAM is not yet associated
717831bc9e3SMatthew Dillon 	 * during the initial parallel probe and the port's probe state
718831bc9e3SMatthew Dillon 	 * will not get past ATA_PROBE_NEED_IDENT.
719831bc9e3SMatthew Dillon 	 */
720c408a8b3SMatthew Dillon 	{
7211067474aSMatthew Dillon 		if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
7221067474aSMatthew Dillon 			kprintf("%s: Waiting 10 seconds on insertion\n",
7231067474aSMatthew Dillon 				PORTNAME(ap));
7241067474aSMatthew Dillon 			ahci_os_sleep(10000);
7251067474aSMatthew Dillon 			initial = 1;
7263209f581SMatthew Dillon 		}
7271067474aSMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT)
72812feb904SMatthew Dillon 			ahci_port_init(ap);
7293209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
7303209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 1);
7313209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
7323209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 0);
7333209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
7343209f581SMatthew Dillon 			ahci_cam_probe(ap, NULL);
7353209f581SMatthew Dillon 	}
7363209f581SMatthew Dillon 	if (ap->ap_type != ATA_PORT_T_PM) {
7373209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_FAILED) {
7383209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 0);
739f4553de1SMatthew Dillon 		} else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
7403209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 1);
7413209f581SMatthew Dillon 		}
7423209f581SMatthew Dillon 		return;
7433209f581SMatthew Dillon 	}
7443209f581SMatthew Dillon 
745831bc9e3SMatthew Dillon 	/*
746831bc9e3SMatthew Dillon 	 * Port Multiplier state machine.
747831bc9e3SMatthew Dillon 	 *
748831bc9e3SMatthew Dillon 	 * Get a mask of changed targets and combine with any runnable
749831bc9e3SMatthew Dillon 	 * states already present.
750831bc9e3SMatthew Dillon 	 */
751831bc9e3SMatthew Dillon 	for (loop = 0; ;++loop) {
7522cc2e845SMatthew Dillon 		if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
7533209f581SMatthew Dillon 			kprintf("%s: PM unable to read hot-plug bitmap\n",
7543209f581SMatthew Dillon 				PORTNAME(ap));
7553209f581SMatthew Dillon 			break;
7563209f581SMatthew Dillon 		}
7573209f581SMatthew Dillon 
7583209f581SMatthew Dillon 		/*
759831bc9e3SMatthew Dillon 		 * Do at least one loop, then stop if no more state changes
760831bc9e3SMatthew Dillon 		 * have occured.  The PM might not generate a new
761831bc9e3SMatthew Dillon 		 * notification until we clear the entire bitmap.
7623209f581SMatthew Dillon 		 */
763831bc9e3SMatthew Dillon 		if (loop && data == 0)
7643209f581SMatthew Dillon 			break;
7653209f581SMatthew Dillon 
7663209f581SMatthew Dillon 		/*
7673209f581SMatthew Dillon 		 * New devices showing up in the bitmap require some spin-up
7683209f581SMatthew Dillon 		 * time before we start probing them.  Reset didsleep.  The
7693209f581SMatthew Dillon 		 * first new device we detect will sleep before probing.
770831bc9e3SMatthew Dillon 		 *
771831bc9e3SMatthew Dillon 		 * This only applies to devices whos change bit is set in
772831bc9e3SMatthew Dillon 		 * the data, and does not apply to the initial boot-time
773831bc9e3SMatthew Dillon 		 * probe.
7743209f581SMatthew Dillon 		 */
7753209f581SMatthew Dillon 		didsleep = 0;
7763209f581SMatthew Dillon 
7773209f581SMatthew Dillon 		for (target = 0; target < ap->ap_pmcount; ++target) {
778b012a2caSMatthew Dillon 			at = ap->ap_ata[target];
7793209f581SMatthew Dillon 
7803209f581SMatthew Dillon 			/*
7813209f581SMatthew Dillon 			 * Check the target state for targets behind the PM
7823209f581SMatthew Dillon 			 * which have changed state.  This will adjust
7833209f581SMatthew Dillon 			 * at_probe and set ATA_PORT_F_RESCAN
7843209f581SMatthew Dillon 			 *
7851067474aSMatthew Dillon 			 * We want to wait at least 10 seconds before probing
7863209f581SMatthew Dillon 			 * a newly inserted device.  If the check status
7873209f581SMatthew Dillon 			 * indicates a device is present and in need of a
7883209f581SMatthew Dillon 			 * hard reset, we make sure we have slept before
7893209f581SMatthew Dillon 			 * continuing.
790831bc9e3SMatthew Dillon 			 *
7911067474aSMatthew Dillon 			 * We also need to wait at least 1 second for the
7921067474aSMatthew Dillon 			 * PHY state to change after insertion, if we
7931067474aSMatthew Dillon 			 * haven't already waited the 10 seconds.
7941067474aSMatthew Dillon 			 *
795831bc9e3SMatthew Dillon 			 * NOTE: When pm_check_good finds a good port it
796831bc9e3SMatthew Dillon 			 *	 typically starts us in probe state
797831bc9e3SMatthew Dillon 			 *	 NEED_HARD_RESET rather than INIT.
7983209f581SMatthew Dillon 			 */
7993209f581SMatthew Dillon 			if (data & (1 << target)) {
8001067474aSMatthew Dillon 				if (initial == 0 && didsleep == 0)
8011067474aSMatthew Dillon 					ahci_os_sleep(1000);
8023209f581SMatthew Dillon 				ahci_pm_check_good(ap, target);
803831bc9e3SMatthew Dillon 				if (initial == 0 && didsleep == 0 &&
804831bc9e3SMatthew Dillon 				    at->at_probe <= ATA_PROBE_NEED_HARD_RESET
805831bc9e3SMatthew Dillon 				) {
8063209f581SMatthew Dillon 					didsleep = 1;
807121d8e75SMatthew Dillon 					kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
808121d8e75SMatthew Dillon 					ahci_os_sleep(10000);
8093209f581SMatthew Dillon 				}
8103209f581SMatthew Dillon 			}
811831bc9e3SMatthew Dillon 
812831bc9e3SMatthew Dillon 			/*
813831bc9e3SMatthew Dillon 			 * Report hot-plug events before the probe state
814831bc9e3SMatthew Dillon 			 * really gets hot.  Only actual events are reported
815831bc9e3SMatthew Dillon 			 * here to reduce spew.
816831bc9e3SMatthew Dillon 			 */
817831bc9e3SMatthew Dillon 			if (data & (1 << target)) {
818831bc9e3SMatthew Dillon 				kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
819831bc9e3SMatthew Dillon 				switch(at->at_probe) {
820831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_INIT:
821831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_HARD_RESET:
822831bc9e3SMatthew Dillon 					kprintf("Device inserted\n");
823831bc9e3SMatthew Dillon 					break;
824831bc9e3SMatthew Dillon 				case ATA_PROBE_FAILED:
825831bc9e3SMatthew Dillon 					kprintf("Device removed\n");
826831bc9e3SMatthew Dillon 					break;
827831bc9e3SMatthew Dillon 				default:
828831bc9e3SMatthew Dillon 					kprintf("Device probe in progress\n");
829831bc9e3SMatthew Dillon 					break;
830831bc9e3SMatthew Dillon 				}
8313209f581SMatthew Dillon 			}
8323209f581SMatthew Dillon 
8333209f581SMatthew Dillon 			/*
834831bc9e3SMatthew Dillon 			 * Run through the state machine as necessary if
835831bc9e3SMatthew Dillon 			 * the port is not marked failed.
836831bc9e3SMatthew Dillon 			 *
837831bc9e3SMatthew Dillon 			 * The state machine may stop at NEED_IDENT if
838831bc9e3SMatthew Dillon 			 * CAM is not yet attached.
839831bc9e3SMatthew Dillon 			 *
840831bc9e3SMatthew Dillon 			 * Acquire exclusive access to the port while we
841831bc9e3SMatthew Dillon 			 * are doing this.  This prevents command-completion
842831bc9e3SMatthew Dillon 			 * from queueing commands for non-polled targets
843831bc9e3SMatthew Dillon 			 * inbetween our probe steps.  We need to do this
844831bc9e3SMatthew Dillon 			 * because the reset probes can generate severe PHY
845831bc9e3SMatthew Dillon 			 * and protocol errors and soft-brick the port.
8463209f581SMatthew Dillon 			 */
847831bc9e3SMatthew Dillon 			if (at->at_probe != ATA_PROBE_FAILED &&
848831bc9e3SMatthew Dillon 			    at->at_probe != ATA_PROBE_GOOD) {
849831bc9e3SMatthew Dillon 				ahci_beg_exclusive_access(ap, at);
8503209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_INIT)
85112feb904SMatthew Dillon 					ahci_pm_port_init(ap, at);
8523209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
8533209f581SMatthew Dillon 					ahci_port_reset(ap, at, 1);
8543209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
8553209f581SMatthew Dillon 					ahci_port_reset(ap, at, 0);
8563209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_IDENT)
8573209f581SMatthew Dillon 					ahci_cam_probe(ap, at);
858831bc9e3SMatthew Dillon 				ahci_end_exclusive_access(ap, at);
8593209f581SMatthew Dillon 			}
8603209f581SMatthew Dillon 
8613209f581SMatthew Dillon 			/*
862831bc9e3SMatthew Dillon 			 * Add or remove from CAM
8633209f581SMatthew Dillon 			 */
8643209f581SMatthew Dillon 			if (at->at_features & ATA_PORT_F_RESCAN) {
8653209f581SMatthew Dillon 				at->at_features &= ~ATA_PORT_F_RESCAN;
8663209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_FAILED) {
8673209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 0);
868f4553de1SMatthew Dillon 				} else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
8693209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 1);
8703209f581SMatthew Dillon 				}
8713209f581SMatthew Dillon 			}
8723560ed94SMatthew Dillon 			data &= ~(1 << target);
8733560ed94SMatthew Dillon 		}
8743560ed94SMatthew Dillon 		if (data) {
8753560ed94SMatthew Dillon 			kprintf("%s: WARNING (PM): extra bits set in "
8763560ed94SMatthew Dillon 				"EINFO: %08x\n", PORTNAME(ap), data);
8773560ed94SMatthew Dillon 			while (target < AHCI_MAX_PMPORTS) {
8783560ed94SMatthew Dillon 				ahci_pm_check_good(ap, target);
8793560ed94SMatthew Dillon 				++target;
8803560ed94SMatthew Dillon 			}
8813209f581SMatthew Dillon 		}
8823209f581SMatthew Dillon 	}
8833209f581SMatthew Dillon }
8843209f581SMatthew Dillon 
8853209f581SMatthew Dillon 
8863209f581SMatthew Dillon /*
887fd8bd957SMatthew Dillon  * De-initialize and detach a port.
888fd8bd957SMatthew Dillon  */
889258223a3SMatthew Dillon void
890258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
891258223a3SMatthew Dillon {
892258223a3SMatthew Dillon 	struct ahci_port	*ap = sc->sc_ports[port];
893258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
894b012a2caSMatthew Dillon 	int i;
895258223a3SMatthew Dillon 
89617eab71eSMatthew Dillon 	/*
89717eab71eSMatthew Dillon 	 * Ensure port is disabled and its interrupts are all flushed.
89817eab71eSMatthew Dillon 	 */
899258223a3SMatthew Dillon 	if (ap->ap_sc) {
90017eab71eSMatthew Dillon 		ahci_port_stop(ap, 1);
901f4553de1SMatthew Dillon 		ahci_os_stop_port(ap);
902258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
903258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
904258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
905258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
906258223a3SMatthew Dillon 	}
907258223a3SMatthew Dillon 
908258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
909258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
910258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
911258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
912258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
913258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
914258223a3SMatthew Dillon 			}
915258223a3SMatthew Dillon 		}
9161067474aSMatthew Dillon 		if ((ccb = ap->ap_err_ccb) != NULL) {
9171067474aSMatthew Dillon 			if (ccb->ccb_dmamap) {
9181067474aSMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
9191067474aSMatthew Dillon 						   ccb->ccb_dmamap);
9201067474aSMatthew Dillon 				ccb->ccb_dmamap = NULL;
9211067474aSMatthew Dillon 			}
9221067474aSMatthew Dillon 			ap->ap_err_ccb = NULL;
9231067474aSMatthew Dillon 		}
924258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
925258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
926258223a3SMatthew Dillon 	}
927258223a3SMatthew Dillon 
928258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
929258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
930258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
931258223a3SMatthew Dillon 	}
932258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
933258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
934258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
935258223a3SMatthew Dillon 	}
936258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
937258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
938258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
939258223a3SMatthew Dillon 	}
9401980eff3SMatthew Dillon 	if (ap->ap_ata) {
941b012a2caSMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
942b012a2caSMatthew Dillon 			if (ap->ap_ata[i]) {
943b012a2caSMatthew Dillon 				kfree(ap->ap_ata[i], M_DEVBUF);
944b012a2caSMatthew Dillon 				ap->ap_ata[i] = NULL;
945b012a2caSMatthew Dillon 			}
946b012a2caSMatthew Dillon 		}
9471980eff3SMatthew Dillon 	}
94812feb904SMatthew Dillon 	if (ap->ap_err_scratch) {
94912feb904SMatthew Dillon 		kfree(ap->ap_err_scratch, M_DEVBUF);
95012feb904SMatthew Dillon 		ap->ap_err_scratch = NULL;
95112feb904SMatthew Dillon 	}
952258223a3SMatthew Dillon 
953258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
954258223a3SMatthew Dillon 
955258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
956258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
957258223a3SMatthew Dillon }
958258223a3SMatthew Dillon 
959492bffafSMatthew Dillon static
960492bffafSMatthew Dillon u_int32_t
961492bffafSMatthew Dillon ahci_pactive(struct ahci_port *ap)
962492bffafSMatthew Dillon {
963492bffafSMatthew Dillon 	u_int32_t mask;
964492bffafSMatthew Dillon 
965492bffafSMatthew Dillon 	mask = ahci_pread(ap, AHCI_PREG_CI);
966492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ)
967492bffafSMatthew Dillon 		mask |= ahci_pread(ap, AHCI_PREG_SACT);
968492bffafSMatthew Dillon 	return(mask);
969492bffafSMatthew Dillon }
970492bffafSMatthew Dillon 
971fd8bd957SMatthew Dillon /*
972fd8bd957SMatthew Dillon  * Start high-level command processing on the port
973fd8bd957SMatthew Dillon  */
974258223a3SMatthew Dillon int
97517eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap)
976258223a3SMatthew Dillon {
97712feb904SMatthew Dillon 	u_int32_t	r, s, is, tfd;
978258223a3SMatthew Dillon 
97917eab71eSMatthew Dillon 	/*
98017eab71eSMatthew Dillon 	 * FRE must be turned on before ST.  Wait for FR to go active
98117eab71eSMatthew Dillon 	 * before turning on ST.  The spec doesn't seem to think this
98217eab71eSMatthew Dillon 	 * is necessary but waiting here avoids an on-off race in the
98317eab71eSMatthew Dillon 	 * ahci_port_stop() code.
98417eab71eSMatthew Dillon 	 */
98512feb904SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD);
98617eab71eSMatthew Dillon 	if ((r & AHCI_PREG_CMD_FRE) == 0) {
987258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_FRE;
98817eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
98917eab71eSMatthew Dillon 	}
99017eab71eSMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
99117eab71eSMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
99217eab71eSMatthew Dillon 			kprintf("%s: Cannot start FIS reception\n",
99317eab71eSMatthew Dillon 				PORTNAME(ap));
99417eab71eSMatthew Dillon 			return (2);
99517eab71eSMatthew Dillon 		}
996f17a0cedSMatthew Dillon 	} else {
997f17a0cedSMatthew Dillon 		ahci_os_sleep(10);
99817eab71eSMatthew Dillon 	}
99917eab71eSMatthew Dillon 
100017eab71eSMatthew Dillon 	/*
100117eab71eSMatthew Dillon 	 * Turn on ST, wait for CR to come up.
100217eab71eSMatthew Dillon 	 */
1003258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_ST;
1004258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
1005f17a0cedSMatthew Dillon 	if (ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
10068bf6a3ffSMatthew Dillon 		s = ahci_pread(ap, AHCI_PREG_SERR);
10078bf6a3ffSMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
10088bf6a3ffSMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
10091980eff3SMatthew Dillon 		kprintf("%s: Cannot start command DMA\n"
10101980eff3SMatthew Dillon 			"NCMP=%b NSERR=%b\n"
101112feb904SMatthew Dillon 			"NEWIS=%b\n"
101212feb904SMatthew Dillon 			"NEWTFD=%b\n",
10131980eff3SMatthew Dillon 			PORTNAME(ap),
10141980eff3SMatthew Dillon 			r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
101512feb904SMatthew Dillon 			is, AHCI_PFMT_IS,
101612feb904SMatthew Dillon 			tfd, AHCI_PFMT_TFD_STS);
101717eab71eSMatthew Dillon 		return (1);
101817eab71eSMatthew Dillon 	}
1019258223a3SMatthew Dillon 
1020258223a3SMatthew Dillon #ifdef AHCI_COALESCE
102117eab71eSMatthew Dillon 	/*
102217eab71eSMatthew Dillon 	 * (Re-)enable coalescing on the port.
102317eab71eSMatthew Dillon 	 */
1024258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1025258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
1026258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1027258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
1028258223a3SMatthew Dillon 	}
1029258223a3SMatthew Dillon #endif
1030258223a3SMatthew Dillon 
1031258223a3SMatthew Dillon 	return (0);
1032258223a3SMatthew Dillon }
1033258223a3SMatthew Dillon 
1034fd8bd957SMatthew Dillon /*
1035fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
10364c339a5fSMatthew Dillon  *
10374c339a5fSMatthew Dillon  * WARNING!  If the port is stopped while CR is still active our saved
10384c339a5fSMatthew Dillon  *	     CI/SACT will race any commands completed by the command
10394c339a5fSMatthew Dillon  *	     processor prior to being able to stop.  Thus we never call
10404c339a5fSMatthew Dillon  *	     this function unless we intend to dispose of any remaining
10414c339a5fSMatthew Dillon  *	     active commands.  In particular, this complicates the timeout
10424c339a5fSMatthew Dillon  *	     code.
1043fd8bd957SMatthew Dillon  */
1044258223a3SMatthew Dillon int
1045258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
1046258223a3SMatthew Dillon {
1047258223a3SMatthew Dillon 	u_int32_t	r;
1048258223a3SMatthew Dillon 
1049258223a3SMatthew Dillon #ifdef AHCI_COALESCE
105017eab71eSMatthew Dillon 	/*
105117eab71eSMatthew Dillon 	 * Disable coalescing on the port while it is stopped.
105217eab71eSMatthew Dillon 	 */
1053258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1054258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
1055258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1056258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
1057258223a3SMatthew Dillon 	}
1058258223a3SMatthew Dillon #endif
1059258223a3SMatthew Dillon 
106017eab71eSMatthew Dillon 	/*
106117eab71eSMatthew Dillon 	 * Turn off ST, then wait for CR to go off.
106217eab71eSMatthew Dillon 	 */
1063258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1064258223a3SMatthew Dillon 	r &= ~AHCI_PREG_CMD_ST;
1065258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
1066258223a3SMatthew Dillon 
106717eab71eSMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
106817eab71eSMatthew Dillon 		kprintf("%s: Port bricked, unable to stop (ST)\n",
106917eab71eSMatthew Dillon 			PORTNAME(ap));
1070258223a3SMatthew Dillon 		return (1);
107117eab71eSMatthew Dillon 	}
1072258223a3SMatthew Dillon 
10731980eff3SMatthew Dillon #if 0
107417eab71eSMatthew Dillon 	/*
107517eab71eSMatthew Dillon 	 * Turn off FRE, then wait for FR to go off.  FRE cannot
107617eab71eSMatthew Dillon 	 * be turned off until CR transitions to 0.
107717eab71eSMatthew Dillon 	 */
10781980eff3SMatthew Dillon 	if ((r & AHCI_PREG_CMD_FR) == 0) {
10791980eff3SMatthew Dillon 		kprintf("%s: FR stopped, clear FRE for next start\n",
10801980eff3SMatthew Dillon 			PORTNAME(ap));
10811980eff3SMatthew Dillon 		stop_fis_rx = 2;
10821980eff3SMatthew Dillon 	}
10831980eff3SMatthew Dillon #endif
108417eab71eSMatthew Dillon 	if (stop_fis_rx) {
108517eab71eSMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
108617eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
108717eab71eSMatthew Dillon 		if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
108817eab71eSMatthew Dillon 			kprintf("%s: Port bricked, unable to stop (FRE)\n",
108917eab71eSMatthew Dillon 				PORTNAME(ap));
1090258223a3SMatthew Dillon 			return (2);
109117eab71eSMatthew Dillon 		}
109217eab71eSMatthew Dillon 	}
1093258223a3SMatthew Dillon 
1094258223a3SMatthew Dillon 	return (0);
1095258223a3SMatthew Dillon }
1096258223a3SMatthew Dillon 
1097fd8bd957SMatthew Dillon /*
1098fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1099fd8bd957SMatthew Dillon  */
1100258223a3SMatthew Dillon int
1101258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
1102258223a3SMatthew Dillon {
1103258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1104258223a3SMatthew Dillon 	u_int32_t			cmd;
1105258223a3SMatthew Dillon 
1106258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
1107258223a3SMatthew Dillon 	if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
1108258223a3SMatthew Dillon 		return (1);
1109258223a3SMatthew Dillon 
1110258223a3SMatthew Dillon 	/* Issue CLO */
1111258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1112258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1113258223a3SMatthew Dillon 
1114258223a3SMatthew Dillon 	/* Wait for completion */
1115258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1116258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1117258223a3SMatthew Dillon 		return (1);
1118258223a3SMatthew Dillon 	}
1119258223a3SMatthew Dillon 
1120258223a3SMatthew Dillon 	return (0);
1121258223a3SMatthew Dillon }
1122258223a3SMatthew Dillon 
1123fd8bd957SMatthew Dillon /*
11241980eff3SMatthew Dillon  * Reset a port.
112517eab71eSMatthew Dillon  *
11261980eff3SMatthew Dillon  * If hard is 0 perform a softreset of the port.
112717eab71eSMatthew Dillon  * If hard is 1 perform a hard reset of the port.
11281980eff3SMatthew Dillon  *
11291980eff3SMatthew Dillon  * If at is non-NULL an indirect port via a port-multiplier is being
11301980eff3SMatthew Dillon  * reset, otherwise a direct port is being reset.
11311980eff3SMatthew Dillon  *
11321980eff3SMatthew Dillon  * NOTE: Indirect ports can only be soft-reset.
113317eab71eSMatthew Dillon  */
113417eab71eSMatthew Dillon int
11351980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
113617eab71eSMatthew Dillon {
113717eab71eSMatthew Dillon 	int rc;
113817eab71eSMatthew Dillon 
113917eab71eSMatthew Dillon 	if (hard) {
11401980eff3SMatthew Dillon 		if (at)
11411980eff3SMatthew Dillon 			rc = ahci_pm_hardreset(ap, at->at_target, hard);
11421980eff3SMatthew Dillon 		else
11431980eff3SMatthew Dillon 			rc = ahci_port_hardreset(ap, hard);
114417eab71eSMatthew Dillon 	} else {
11451980eff3SMatthew Dillon 		if (at)
11461980eff3SMatthew Dillon 			rc = ahci_pm_softreset(ap, at->at_target);
11471980eff3SMatthew Dillon 		else
114817eab71eSMatthew Dillon 			rc = ahci_port_softreset(ap);
114917eab71eSMatthew Dillon 	}
115017eab71eSMatthew Dillon 	return(rc);
115117eab71eSMatthew Dillon }
115217eab71eSMatthew Dillon 
115317eab71eSMatthew Dillon /*
1154fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
1155fd8bd957SMatthew Dillon  *
11561980eff3SMatthew Dillon  * (at) will be NULL when soft-resetting a directly-attached device, and
11571980eff3SMatthew Dillon  * non-NULL when soft-resetting a device through a port multiplier.
11581980eff3SMatthew Dillon  *
1159fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
11601980eff3SMatthew Dillon  * a reset to the connected device using device commands.
1161fd8bd957SMatthew Dillon  */
1162258223a3SMatthew Dillon int
1163258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
1164258223a3SMatthew Dillon {
1165258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
1166258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
1167258223a3SMatthew Dillon 	u_int8_t		*fis;
11683209f581SMatthew Dillon 	int			error;
1169258223a3SMatthew Dillon 
11703209f581SMatthew Dillon 	error = EIO;
11711980eff3SMatthew Dillon 
1172074579dfSMatthew Dillon 	if (bootverbose) {
11731980eff3SMatthew Dillon 		kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
11741980eff3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
1175074579dfSMatthew Dillon 	}
11761980eff3SMatthew Dillon 
1177258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1178258223a3SMatthew Dillon 
1179258223a3SMatthew Dillon 	crit_enter();
11801980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
11811980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
1182258223a3SMatthew Dillon 
11831980eff3SMatthew Dillon 	/*
11841980eff3SMatthew Dillon 	 * Remember port state in cmd (main to restore start/stop)
11851980eff3SMatthew Dillon 	 *
11861980eff3SMatthew Dillon 	 * Idle port.
11871980eff3SMatthew Dillon 	 */
1188258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
1189258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
1190258223a3SMatthew Dillon 			PORTNAME(ap));
1191258223a3SMatthew Dillon 		goto err;
1192258223a3SMatthew Dillon 	}
1193cf5f3a81SMatthew Dillon 
1194cf5f3a81SMatthew Dillon 	/*
11951980eff3SMatthew Dillon 	 * Request CLO if device appears hung.
1196cf5f3a81SMatthew Dillon 	 */
1197258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
1198258223a3SMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1199258223a3SMatthew Dillon 		ahci_port_clo(ap);
1200258223a3SMatthew Dillon 	}
1201258223a3SMatthew Dillon 
12021980eff3SMatthew Dillon 	/*
12031980eff3SMatthew Dillon 	 * This is an attempt to clear errors so a new signature will
12041980eff3SMatthew Dillon 	 * be latched.  It isn't working properly.  XXX
12051980eff3SMatthew Dillon 	 */
1206cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
12071980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1208258223a3SMatthew Dillon 
1209258223a3SMatthew Dillon 	/* Restart port */
121017eab71eSMatthew Dillon 	if (ahci_port_start(ap)) {
1211258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
1212258223a3SMatthew Dillon 		        PORTNAME(ap));
1213258223a3SMatthew Dillon 		goto err;
1214258223a3SMatthew Dillon 	}
1215258223a3SMatthew Dillon 
1216258223a3SMatthew Dillon 	/* Check whether CLO worked */
1217258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1218258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1219258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
1220258223a3SMatthew Dillon 			PORTNAME(ap),
1221258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1222258223a3SMatthew Dillon 			? "failed" : "unsupported");
12233209f581SMatthew Dillon 		error = EBUSY;
1224258223a3SMatthew Dillon 		goto err;
1225258223a3SMatthew Dillon 	}
1226258223a3SMatthew Dillon 
1227cec85a37SMatthew Dillon 	/*
1228cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
1229cec85a37SMatthew Dillon 	 *
1230cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1231cec85a37SMatthew Dillon 	 * everything.
12321067474aSMatthew Dillon 	 *
12331067474aSMatthew Dillon 	 * NOTE!  This CCB is used for both the first and second commands.
12341067474aSMatthew Dillon 	 *	  The second command must use CCB slot 1 to properly load
12351067474aSMatthew Dillon 	 *	  the signature.
1236cec85a37SMatthew Dillon 	 */
1237258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
123812feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
123912feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
12401067474aSMatthew Dillon 	KKASSERT(ccb->ccb_slot == 1);
12411980eff3SMatthew Dillon 	ccb->ccb_xa.at = NULL;
1242258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1243258223a3SMatthew Dillon 
1244258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
1245cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
12461980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
12471980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1248258223a3SMatthew Dillon 
1249258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1250258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1251258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1252258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1253258223a3SMatthew Dillon 
1254258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
125512feb904SMatthew Dillon 
1256831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
12575f8c1efdSMatthew Dillon 		kprintf("%s: First FIS failed\n", PORTNAME(ap));
1258258223a3SMatthew Dillon 		goto err;
1259cec85a37SMatthew Dillon 	}
1260258223a3SMatthew Dillon 
1261cec85a37SMatthew Dillon 	/*
1262831bc9e3SMatthew Dillon 	 * WARNING!	TIME SENSITIVE SPACE!	WARNING!
1263831bc9e3SMatthew Dillon 	 *
1264831bc9e3SMatthew Dillon 	 * The two FISes are supposed to be back to back.  Don't issue other
1265831bc9e3SMatthew Dillon 	 * commands or even delay if we can help it.
12661980eff3SMatthew Dillon 	 */
12671980eff3SMatthew Dillon 
12681980eff3SMatthew Dillon 	/*
1269cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
1270cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
1271cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
1272cec85a37SMatthew Dillon 	 * from the AHCI document.
1273cec85a37SMatthew Dillon 	 *
1274cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1275cec85a37SMatthew Dillon 	 * everything.
1276cec85a37SMatthew Dillon 	 */
127712feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
127812feb904SMatthew Dillon 
1279cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
12801980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
12811980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_4BIT;
1282258223a3SMatthew Dillon 
1283258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1284258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1285258223a3SMatthew Dillon 
1286258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
1287831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
12885f8c1efdSMatthew Dillon 		kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1289258223a3SMatthew Dillon 		goto err;
1290cec85a37SMatthew Dillon 	}
1291258223a3SMatthew Dillon 
12921980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
12931980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1294258223a3SMatthew Dillon 		kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1295258223a3SMatthew Dillon 			PORTNAME(ap),
1296258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
12973209f581SMatthew Dillon 		error = EBUSY;
1298258223a3SMatthew Dillon 		goto err;
1299258223a3SMatthew Dillon 	}
1300258223a3SMatthew Dillon 
1301fd8bd957SMatthew Dillon 	/*
1302fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
1303fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
1304fd8bd957SMatthew Dillon 	 *
1305fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
1306fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
1307fd8bd957SMatthew Dillon 	 * unexpectedly.
1308fd8bd957SMatthew Dillon 	 */
1309493d3201SMatthew Dillon 	ahci_os_sleep(100);
13101980eff3SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE) {
13111980eff3SMatthew Dillon 		ap->ap_type = ahci_port_signature_detect(ap, NULL);
1312fd8bd957SMatthew Dillon 	} else {
13131980eff3SMatthew Dillon 		if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
13141980eff3SMatthew Dillon 			kprintf("%s: device signature unexpectedly "
13151980eff3SMatthew Dillon 				"changed\n", PORTNAME(ap));
13163209f581SMatthew Dillon 			error = EBUSY; /* XXX */
1317fd8bd957SMatthew Dillon 		}
1318fd8bd957SMatthew Dillon 	}
13193209f581SMatthew Dillon 	error = 0;
13201980eff3SMatthew Dillon 
13213209f581SMatthew Dillon 	ahci_os_sleep(3);
1322258223a3SMatthew Dillon err:
1323258223a3SMatthew Dillon 	if (ccb != NULL) {
1324258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
13251980eff3SMatthew Dillon 
13261980eff3SMatthew Dillon 		/*
13271980eff3SMatthew Dillon 		 * If the target is busy use CLO to clear the busy
13281980eff3SMatthew Dillon 		 * condition.  The BSY should be cleared on the next
13291980eff3SMatthew Dillon 		 * start.
13301980eff3SMatthew Dillon 		 */
13311980eff3SMatthew Dillon 		if (ahci_pread(ap, AHCI_PREG_TFD) &
13321980eff3SMatthew Dillon 		    (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
13331980eff3SMatthew Dillon 			ahci_port_clo(ap);
13341980eff3SMatthew Dillon 		}
1335258223a3SMatthew Dillon 	}
1336258223a3SMatthew Dillon 
1337cf5f3a81SMatthew Dillon 	/*
1338cf5f3a81SMatthew Dillon 	 * If we failed to softreset make the port quiescent, otherwise
1339cf5f3a81SMatthew Dillon 	 * make sure the port's start/stop state matches what it was on
1340cf5f3a81SMatthew Dillon 	 * entry.
13411980eff3SMatthew Dillon 	 *
13421980eff3SMatthew Dillon 	 * Don't kill the port if the softreset is on a port multiplier
13431980eff3SMatthew Dillon 	 * target, that would kill all the targets!
1344cf5f3a81SMatthew Dillon 	 */
13453209f581SMatthew Dillon 	if (error) {
1346cf5f3a81SMatthew Dillon 		ahci_port_hardstop(ap);
13473209f581SMatthew Dillon 		/* ap_probe set to failed */
1348cf5f3a81SMatthew Dillon 	} else {
13493209f581SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_IDENT;
135012feb904SMatthew Dillon 		ap->ap_pmcount = 1;
13514c339a5fSMatthew Dillon 		ahci_port_start(ap);
1352cf5f3a81SMatthew Dillon 	}
13533209f581SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
1354258223a3SMatthew Dillon 	crit_exit();
1355258223a3SMatthew Dillon 
1356074579dfSMatthew Dillon 	if (bootverbose)
13571980eff3SMatthew Dillon 		kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
13581980eff3SMatthew Dillon 
13593209f581SMatthew Dillon 	return (error);
1360258223a3SMatthew Dillon }
1361258223a3SMatthew Dillon 
1362fd8bd957SMatthew Dillon /*
1363493d3201SMatthew Dillon  * Issue just do the core COMRESET and basic device detection on a port.
1364fd8bd957SMatthew Dillon  *
1365493d3201SMatthew Dillon  * NOTE: Only called by ahci_port_hardreset().
1366fd8bd957SMatthew Dillon  */
1367493d3201SMatthew Dillon static int
1368493d3201SMatthew Dillon ahci_comreset(struct ahci_port *ap, int *pmdetectp)
1369258223a3SMatthew Dillon {
1370493d3201SMatthew Dillon 	u_int32_t cmd;
1371493d3201SMatthew Dillon 	u_int32_t r;
13723209f581SMatthew Dillon 	int error;
13731980eff3SMatthew Dillon 	int loop;
1374f2dba700SMatthew Dillon 	int retries = 0;
1375258223a3SMatthew Dillon 
1376cf5f3a81SMatthew Dillon 	/*
13771980eff3SMatthew Dillon 	 * Idle the port,
13781980eff3SMatthew Dillon 	 */
1379493d3201SMatthew Dillon 	*pmdetectp = 0;
13801980eff3SMatthew Dillon 	ahci_port_stop(ap, 0);
13811980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
1382493d3201SMatthew Dillon 	ahci_os_sleep(10);
13831980eff3SMatthew Dillon 
13841980eff3SMatthew Dillon 	/*
13851980eff3SMatthew Dillon 	 * The port may have been quiescent with its SUD bit cleared, so
13861980eff3SMatthew Dillon 	 * set the SUD (spin up device).
1387493d3201SMatthew Dillon 	 *
1388493d3201SMatthew Dillon 	 * NOTE: I do not know if SUD is a hardware pin/low-level signal
1389493d3201SMatthew Dillon 	 *	 or if it is messaged.
1390cf5f3a81SMatthew Dillon 	 */
1391cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1392493d3201SMatthew Dillon 
1393493d3201SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD;
1394cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1395493d3201SMatthew Dillon 	ahci_os_sleep(10);
1396258223a3SMatthew Dillon 
13971980eff3SMatthew Dillon 	/*
1398493d3201SMatthew Dillon 	 * Make sure that all power management is disabled.
13991067474aSMatthew Dillon 	 *
1400493d3201SMatthew Dillon 	 * NOTE!  AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable
14014e21f4daSMatthew Dillon 	 *	  on multiple chipsets and can brick the chipset or even
14024e21f4daSMatthew Dillon 	 *	  the whole PC.  Never use it.
14031980eff3SMatthew Dillon 	 */
14041980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1405258223a3SMatthew Dillon 
1406493d3201SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED |
1407493d3201SMatthew Dillon 	    AHCI_PREG_SCTL_SPM_DISABLED;
14081980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1409f2dba700SMatthew Dillon 
1410f2dba700SMatthew Dillon retry:
1411f2dba700SMatthew Dillon 	/*
1412f2dba700SMatthew Dillon 	 * Give the new power management state time to settle, then clear
1413f2dba700SMatthew Dillon 	 * pending status.
1414f2dba700SMatthew Dillon 	 */
1415f2dba700SMatthew Dillon 	ahci_os_sleep(1000);
1416f2dba700SMatthew Dillon 	ahci_flush_tfd(ap);
1417f2dba700SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
14181980eff3SMatthew Dillon 
14191980eff3SMatthew Dillon 	/*
1420493d3201SMatthew Dillon 	 * Start transmitting COMRESET.  The spec says that COMRESET must
1421493d3201SMatthew Dillon 	 * be sent for at least 1ms but in actual fact numerous devices
1422493d3201SMatthew Dillon 	 * appear to take much longer.  Delay a whole second here.
1423493d3201SMatthew Dillon 	 *
1424493d3201SMatthew Dillon 	 * In addition, SATA-3 ports can take longer to train, so even
1425493d3201SMatthew Dillon 	 * SATA-2 devices which would normally detect very quickly may
1426493d3201SMatthew Dillon 	 * take longer when plugged into a SATA-3 port.
14271980eff3SMatthew Dillon 	 */
1428493d3201SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_INIT;
14298986d351SMatthew Dillon 	switch(AhciForceGen) {
14308986d351SMatthew Dillon 	case 0:
1431258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
14328986d351SMatthew Dillon 		break;
14338986d351SMatthew Dillon 	case 1:
14348986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
14358986d351SMatthew Dillon 		break;
14368986d351SMatthew Dillon 	case 2:
14378986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN2;
14388986d351SMatthew Dillon 		break;
14398986d351SMatthew Dillon 	case 3:
14408986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN3;
14418986d351SMatthew Dillon 		break;
14428986d351SMatthew Dillon 	default:
14438986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN3;
14448986d351SMatthew Dillon 		break;
14458986d351SMatthew Dillon 	}
1446258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1447492bffafSMatthew Dillon 	ahci_os_sleep(1000);
1448493d3201SMatthew Dillon 
1449492bffafSMatthew Dillon 	ap->ap_flags &= ~AP_F_HARSH_REINIT;
1450cf5f3a81SMatthew Dillon 
1451cf5f3a81SMatthew Dillon 	/*
1452cf5f3a81SMatthew Dillon 	 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1453cf5f3a81SMatthew Dillon 	 * since we are hard-resetting the port we might as well clear
1454f2dba700SMatthew Dillon 	 * the whole enchillada.  Also be sure to clear any spurious BSY
1455f2dba700SMatthew Dillon 	 * prior to clearing INIT.
1456493d3201SMatthew Dillon 	 *
1457493d3201SMatthew Dillon 	 * Wait 1 whole second after clearing INIT before checking
1458493d3201SMatthew Dillon 	 * the device detection bits in an attempt to work around chipsets
1459493d3201SMatthew Dillon 	 * which do not properly mask PCS/PRCS during low level init.
1460cf5f3a81SMatthew Dillon 	 */
1461cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1462cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1463f2dba700SMatthew Dillon /*	ahci_port_clo(ap);*/
1464493d3201SMatthew Dillon 	ahci_os_sleep(10);
1465493d3201SMatthew Dillon 
1466f2dba700SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_SPD;
1467258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
1468258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
1469258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1470493d3201SMatthew Dillon 	ahci_os_sleep(1000);
1471258223a3SMatthew Dillon 
14721980eff3SMatthew Dillon 	/*
14731980eff3SMatthew Dillon 	 * Try to determine if there is a device on the port.
14741980eff3SMatthew Dillon 	 *
14751980eff3SMatthew Dillon 	 * Give the device 3/10 second to at least be detected.
14761980eff3SMatthew Dillon 	 * If we fail clear PRCS (phy detect) since we may cycled
14771980eff3SMatthew Dillon 	 * the phy and probably caused another PRCS interrupt.
14781980eff3SMatthew Dillon 	 */
147976497a9cSMatthew Dillon 	loop = 300;
148076497a9cSMatthew Dillon 	while (loop > 0) {
14811980eff3SMatthew Dillon 		r = ahci_pread(ap, AHCI_PREG_SSTS);
14821980eff3SMatthew Dillon 		if (r & AHCI_PREG_SSTS_DET)
14831980eff3SMatthew Dillon 			break;
148476497a9cSMatthew Dillon 		loop -= ahci_os_softsleep();
14851980eff3SMatthew Dillon 	}
14861980eff3SMatthew Dillon 	if (loop == 0) {
14871980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1488074579dfSMatthew Dillon 		if (bootverbose) {
14891980eff3SMatthew Dillon 			kprintf("%s: Port appears to be unplugged\n",
14901980eff3SMatthew Dillon 				PORTNAME(ap));
1491074579dfSMatthew Dillon 		}
14923209f581SMatthew Dillon 		error = ENODEV;
149312feb904SMatthew Dillon 		goto done;
1494258223a3SMatthew Dillon 	}
1495258223a3SMatthew Dillon 
1496cec85a37SMatthew Dillon 	/*
1497493d3201SMatthew Dillon 	 * There is something on the port.  Regardless of what happens
1498493d3201SMatthew Dillon 	 * after this tell the caller to try to detect a port multiplier.
1499493d3201SMatthew Dillon 	 *
1500493d3201SMatthew Dillon 	 * Give the device 3 seconds to fully negotiate.
15011980eff3SMatthew Dillon 	 */
1502493d3201SMatthew Dillon 	*pmdetectp = 1;
1503493d3201SMatthew Dillon 
150412feb904SMatthew Dillon 	if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
15051980eff3SMatthew Dillon 			  AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1506074579dfSMatthew Dillon 		if (bootverbose) {
15071980eff3SMatthew Dillon 			kprintf("%s: Device may be powered down\n",
15081980eff3SMatthew Dillon 				PORTNAME(ap));
1509074579dfSMatthew Dillon 		}
15103209f581SMatthew Dillon 		error = ENODEV;
1511493d3201SMatthew Dillon 		goto done;
15121980eff3SMatthew Dillon 	}
15131980eff3SMatthew Dillon 
151412feb904SMatthew Dillon 	/*
151512feb904SMatthew Dillon 	 * We got something that definitely looks like a device.  Give
151612feb904SMatthew Dillon 	 * the device time to send us its first D2H FIS.  Waiting for
151712feb904SMatthew Dillon 	 * BSY to clear accomplishes this.
151812feb904SMatthew Dillon 	 *
1519493d3201SMatthew Dillon 	 * NOTE: A port multiplier may or may not clear BSY here,
152012feb904SMatthew Dillon 	 *	 depending on what is sitting in target 0 behind it.
1521f2dba700SMatthew Dillon 	 *
1522f2dba700SMatthew Dillon 	 * NOTE: Intel SSDs seem to have compatibility problems with Intel
1523f2dba700SMatthew Dillon 	 *	 mobo's on cold boots and may leave BSY set.  A single
1524f2dba700SMatthew Dillon 	 *	 retry works around the problem.  This is definitely a bug
1525f2dba700SMatthew Dillon 	 *	 with the mobo and/or the SSD and does not appear to occur
1526f2dba700SMatthew Dillon 	 *	 with other devices connected to the same port.
152712feb904SMatthew Dillon 	 */
1528c408a8b3SMatthew Dillon 	ahci_flush_tfd(ap);
1529f2dba700SMatthew Dillon 	if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD,
15301980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1531f2dba700SMatthew Dillon 		kprintf("%s: Device BUSY: %b\n",
1532f2dba700SMatthew Dillon 			PORTNAME(ap),
1533f2dba700SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD),
1534f2dba700SMatthew Dillon 				AHCI_PFMT_TFD_STS);
1535f2dba700SMatthew Dillon 		if (retries == 0) {
1536f2dba700SMatthew Dillon 			kprintf("%s: Retrying\n", PORTNAME(ap));
1537f2dba700SMatthew Dillon 			retries = 1;
1538f2dba700SMatthew Dillon 			goto retry;
1539f2dba700SMatthew Dillon 		}
154012feb904SMatthew Dillon 		error = EBUSY;
15411980eff3SMatthew Dillon 	} else {
15423209f581SMatthew Dillon 		error = 0;
15431980eff3SMatthew Dillon 	}
1544258223a3SMatthew Dillon 
154512feb904SMatthew Dillon done:
1546493d3201SMatthew Dillon 	ahci_flush_tfd(ap);
1547493d3201SMatthew Dillon 	return error;
1548493d3201SMatthew Dillon }
1549493d3201SMatthew Dillon 
1550493d3201SMatthew Dillon 
1551493d3201SMatthew Dillon /*
1552493d3201SMatthew Dillon  * AHCI port reset, Section 10.4.2
1553493d3201SMatthew Dillon  *
1554493d3201SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
1555493d3201SMatthew Dillon  * connected to the port could still end-up hung.
1556493d3201SMatthew Dillon  */
1557493d3201SMatthew Dillon int
1558493d3201SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard)
1559493d3201SMatthew Dillon {
1560493d3201SMatthew Dillon 	u_int32_t data;
1561493d3201SMatthew Dillon 	int	error;
1562493d3201SMatthew Dillon 	int	pmdetect;
1563493d3201SMatthew Dillon 
1564493d3201SMatthew Dillon 	if (bootverbose)
1565493d3201SMatthew Dillon 		kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1566493d3201SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
1567493d3201SMatthew Dillon 
1568493d3201SMatthew Dillon 	error = ahci_comreset(ap, &pmdetect);
1569493d3201SMatthew Dillon 
1570493d3201SMatthew Dillon 	/*
1571493d3201SMatthew Dillon 	 * We may be asked to perform a port multiplier check even if the
1572493d3201SMatthew Dillon 	 * comreset failed.  This typically occurs when the PM has nothing
1573493d3201SMatthew Dillon 	 * in slot 0, which can cause BSY to remain set.
1574493d3201SMatthew Dillon 	 *
1575493d3201SMatthew Dillon 	 * If the PM detection is successful it will override (error),
1576493d3201SMatthew Dillon 	 * otherwise (error) is retained.  If an error does occur it
1577493d3201SMatthew Dillon 	 * is possible that a normal device has blown up on us DUE to
1578493d3201SMatthew Dillon 	 * the PM detection code, so re-run the comreset and assume
1579493d3201SMatthew Dillon 	 * a normal device.
1580493d3201SMatthew Dillon 	 */
1581493d3201SMatthew Dillon 	if (pmdetect) {
1582493d3201SMatthew Dillon 		if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) {
1583493d3201SMatthew Dillon 			error = ahci_pm_port_probe(ap, error);
1584493d3201SMatthew Dillon 			if (error) {
1585493d3201SMatthew Dillon 				error = ahci_comreset(ap, &pmdetect);
1586493d3201SMatthew Dillon 			}
1587493d3201SMatthew Dillon 		}
1588493d3201SMatthew Dillon 	}
1589493d3201SMatthew Dillon 
159012feb904SMatthew Dillon 	/*
159112feb904SMatthew Dillon 	 * Finish up.
159212feb904SMatthew Dillon 	 */
1593493d3201SMatthew Dillon 	ahci_os_sleep(500);
1594493d3201SMatthew Dillon 
159512feb904SMatthew Dillon 	switch(error) {
159612feb904SMatthew Dillon 	case 0:
159712feb904SMatthew Dillon 		/*
159812feb904SMatthew Dillon 		 * All good, make sure the port is running and set the
159912feb904SMatthew Dillon 		 * probe state.  Ignore the signature junk (it's unreliable)
160012feb904SMatthew Dillon 		 * until we get to the softreset code.
160112feb904SMatthew Dillon 		 */
160212feb904SMatthew Dillon 		if (ahci_port_start(ap)) {
160312feb904SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
160412feb904SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
160512feb904SMatthew Dillon 			error = EBUSY;
1606493d3201SMatthew Dillon 			break;
160712feb904SMatthew Dillon 		}
1608f4553de1SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
1609f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_GOOD;
1610f4553de1SMatthew Dillon 		else
1611f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
161212feb904SMatthew Dillon 		break;
161312feb904SMatthew Dillon 	case ENODEV:
1614fd8bd957SMatthew Dillon 		/*
161512feb904SMatthew Dillon 		 * Normal device probe failure
16161980eff3SMatthew Dillon 		 */
161712feb904SMatthew Dillon 		data = ahci_pread(ap, AHCI_PREG_SSTS);
16181980eff3SMatthew Dillon 
161912feb904SMatthew Dillon 		switch(data & AHCI_PREG_SSTS_DET) {
162012feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
162112feb904SMatthew Dillon 			kprintf("%s: Device not communicating\n",
16221980eff3SMatthew Dillon 				PORTNAME(ap));
162312feb904SMatthew Dillon 			break;
162412feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
162512feb904SMatthew Dillon 			kprintf("%s: PHY offline\n",
162612feb904SMatthew Dillon 				PORTNAME(ap));
162712feb904SMatthew Dillon 			break;
162812feb904SMatthew Dillon 		default:
162912feb904SMatthew Dillon 			kprintf("%s: No device detected\n",
163012feb904SMatthew Dillon 				PORTNAME(ap));
163112feb904SMatthew Dillon 			break;
16321980eff3SMatthew Dillon 		}
163312feb904SMatthew Dillon 		ahci_port_hardstop(ap);
163412feb904SMatthew Dillon 		break;
163512feb904SMatthew Dillon 	default:
16361980eff3SMatthew Dillon 		/*
163712feb904SMatthew Dillon 		 * Abnormal probe (EBUSY)
16381980eff3SMatthew Dillon 		 */
163912feb904SMatthew Dillon 		kprintf("%s: Device on port is bricked\n",
164012feb904SMatthew Dillon 			PORTNAME(ap));
164112feb904SMatthew Dillon 		ahci_port_hardstop(ap);
164212feb904SMatthew Dillon #if 0
164312feb904SMatthew Dillon 		rc = ahci_port_reset(ap, atx, 0);
164412feb904SMatthew Dillon 		if (rc) {
164512feb904SMatthew Dillon 			kprintf("%s: Unable unbrick device\n",
164612feb904SMatthew Dillon 				PORTNAME(ap));
16471980eff3SMatthew Dillon 		} else {
164812feb904SMatthew Dillon 			kprintf("%s: Successfully unbricked\n",
16493209f581SMatthew Dillon 				PORTNAME(ap));
165012feb904SMatthew Dillon 		}
165112feb904SMatthew Dillon #endif
165212feb904SMatthew Dillon 		break;
16533209f581SMatthew Dillon 	}
16541067474aSMatthew Dillon 
16551067474aSMatthew Dillon 	/*
165612feb904SMatthew Dillon 	 * Clean up
16571067474aSMatthew Dillon 	 */
165812feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
165912feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
16603209f581SMatthew Dillon 
166112feb904SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
16621980eff3SMatthew Dillon 
166312feb904SMatthew Dillon 	if (bootverbose)
166412feb904SMatthew Dillon 		kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1665831bc9e3SMatthew Dillon 	return (error);
16661980eff3SMatthew Dillon }
16671980eff3SMatthew Dillon 
16681980eff3SMatthew Dillon /*
1669cf5f3a81SMatthew Dillon  * Hard-stop on hot-swap device removal.  See 10.10.1
1670cf5f3a81SMatthew Dillon  *
1671cf5f3a81SMatthew Dillon  * Place the port in a mode that will allow it to detect hot-swap insertions.
1672cf5f3a81SMatthew Dillon  * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1673cf5f3a81SMatthew Dillon  * seem to do the job.
1674f17a0cedSMatthew Dillon  *
1675f17a0cedSMatthew Dillon  * FIS reception is left enabled but command processing is disabled.
1676f17a0cedSMatthew Dillon  * Cycling FIS reception (FRE) can brick ports.
1677cf5f3a81SMatthew Dillon  */
1678cf5f3a81SMatthew Dillon void
1679cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap)
1680cf5f3a81SMatthew Dillon {
168176497a9cSMatthew Dillon 	struct ahci_ccb *ccb;
16821980eff3SMatthew Dillon 	struct ata_port *at;
1683cf5f3a81SMatthew Dillon 	u_int32_t r;
1684cf5f3a81SMatthew Dillon 	u_int32_t cmd;
168576497a9cSMatthew Dillon 	int slot;
16861980eff3SMatthew Dillon 	int i;
1687bb79834dSMatthew Dillon 	int serial;
1688cf5f3a81SMatthew Dillon 
1689cf5f3a81SMatthew Dillon 	/*
1690cf5f3a81SMatthew Dillon 	 * Stop the port.  We can't modify things like SUD if the port
1691cf5f3a81SMatthew Dillon 	 * is running.
1692cf5f3a81SMatthew Dillon 	 */
1693cf5f3a81SMatthew Dillon 	ap->ap_state = AP_S_FATAL_ERROR;
16941980eff3SMatthew Dillon 	ap->ap_probe = ATA_PROBE_FAILED;
16951980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1696cf5f3a81SMatthew Dillon 	ahci_port_stop(ap, 0);
1697cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
1698492bffafSMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC);
1699492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1700cf5f3a81SMatthew Dillon 
1701cf5f3a81SMatthew Dillon 	/*
17021980eff3SMatthew Dillon 	 * Clean up AT sub-ports on SATA port.
17031980eff3SMatthew Dillon 	 */
17041980eff3SMatthew Dillon 	for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1705b012a2caSMatthew Dillon 		at = ap->ap_ata[i];
17061980eff3SMatthew Dillon 		at->at_type = ATA_PORT_T_NONE;
17073209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_FAILED;
17081980eff3SMatthew Dillon 	}
17091980eff3SMatthew Dillon 
17101980eff3SMatthew Dillon 	/*
1711cf5f3a81SMatthew Dillon 	 * Make sure FRE is active.  There isn't anything we can do if it
1712cf5f3a81SMatthew Dillon 	 * fails so just ignore errors.
1713cf5f3a81SMatthew Dillon 	 */
1714cf5f3a81SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1715cf5f3a81SMatthew Dillon 		cmd |= AHCI_PREG_CMD_FRE;
1716cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1717cf5f3a81SMatthew Dillon 		if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1718cf5f3a81SMatthew Dillon 			ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1719cf5f3a81SMatthew Dillon 	}
1720cf5f3a81SMatthew Dillon 
1721cf5f3a81SMatthew Dillon 	/*
1722cf5f3a81SMatthew Dillon 	 * 10.10.1 place us in the Listen state.
1723cf5f3a81SMatthew Dillon 	 *
17245502cf24SMatthew Dillon 	 * 10.10.3 DET must be set to 0 and found to be 0 before
17255502cf24SMatthew Dillon 	 * setting SUD to 0.
17265502cf24SMatthew Dillon 	 *
17275502cf24SMatthew Dillon 	 * Deactivating SUD only applies if the controller supports SUD, it
17285502cf24SMatthew Dillon 	 * is a bit unclear what happens w/regards to detecting hotplug
17295502cf24SMatthew Dillon 	 * if it doesn't.
1730cf5f3a81SMatthew Dillon 	 */
17315502cf24SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED |
17325502cf24SMatthew Dillon 	    AHCI_PREG_SCTL_SPM_DISABLED;
17335502cf24SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
17345502cf24SMatthew Dillon 	ahci_os_sleep(10);
1735cf5f3a81SMatthew Dillon 	cmd &= ~AHCI_PREG_CMD_SUD;
1736cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
17375502cf24SMatthew Dillon 	ahci_os_sleep(10);
1738cf5f3a81SMatthew Dillon 
1739cf5f3a81SMatthew Dillon 	/*
17405502cf24SMatthew Dillon 	 * 10.10.1
17415502cf24SMatthew Dillon 	 *
17425502cf24SMatthew Dillon 	 * Transition su to the spin-up state.  HBA shall send COMRESET and
17435502cf24SMatthew Dillon 	 * begin initialization sequence (whatever that means).  Presumably
17445502cf24SMatthew Dillon 	 * this is edge-triggered.  Following the spin-up state the HBA
17455502cf24SMatthew Dillon 	 * will automatically transition to the Normal state.
1746cf5f3a81SMatthew Dillon 	 *
1747cf5f3a81SMatthew Dillon 	 * This only applies if the controller supports SUD.
17484e21f4daSMatthew Dillon 	 * NEVER use AHCI_PREG_DET_DISABLE.
1749cf5f3a81SMatthew Dillon 	 */
17505502cf24SMatthew Dillon 	cmd |= AHCI_PREG_CMD_POD |
17515502cf24SMatthew Dillon 	       AHCI_PREG_CMD_SUD |
17525502cf24SMatthew Dillon 	       AHCI_PREG_CMD_ICC_ACTIVE;
1753cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
17545502cf24SMatthew Dillon 	ahci_os_sleep(10);
1755cf5f3a81SMatthew Dillon 
1756cf5f3a81SMatthew Dillon 	/*
1757cf5f3a81SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
1758cf5f3a81SMatthew Dillon 	 */
1759cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1760cf5f3a81SMatthew Dillon 
1761cf5f3a81SMatthew Dillon 	/*
176276497a9cSMatthew Dillon 	 * Clean out pending ccbs
176376497a9cSMatthew Dillon 	 */
1764bb79834dSMatthew Dillon restart:
176576497a9cSMatthew Dillon 	while (ap->ap_active) {
176676497a9cSMatthew Dillon 		slot = ffs(ap->ap_active) - 1;
176776497a9cSMatthew Dillon 		ap->ap_active &= ~(1 << slot);
176876497a9cSMatthew Dillon 		--ap->ap_active_cnt;
176976497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
177076497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1771bb79834dSMatthew Dillon 			serial = ccb->ccb_xa.serial;
177246528d33SMatthew Dillon 			callout_stop_sync(&ccb->ccb_timeout);
1773bb79834dSMatthew Dillon 			if (serial != ccb->ccb_xa.serial) {
1774bb79834dSMatthew Dillon 				kprintf("%s: Warning: timeout race ccb %p\n",
1775bb79834dSMatthew Dillon 					PORTNAME(ap), ccb);
1776bb79834dSMatthew Dillon 				goto restart;
1777bb79834dSMatthew Dillon 			}
177876497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
177976497a9cSMatthew Dillon 		}
178046528d33SMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
178176497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
178276497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
178376497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
178476497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
178576497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
178676497a9cSMatthew Dillon 	}
178776497a9cSMatthew Dillon 	while (ap->ap_sactive) {
178876497a9cSMatthew Dillon 		slot = ffs(ap->ap_sactive) - 1;
178976497a9cSMatthew Dillon 		ap->ap_sactive &= ~(1 << slot);
179076497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
179176497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1792bb79834dSMatthew Dillon 			serial = ccb->ccb_xa.serial;
179346528d33SMatthew Dillon 			callout_stop_sync(&ccb->ccb_timeout);
1794bb79834dSMatthew Dillon 			if (serial != ccb->ccb_xa.serial) {
1795bb79834dSMatthew Dillon 				kprintf("%s: Warning: timeout race ccb %p\n",
1796bb79834dSMatthew Dillon 					PORTNAME(ap), ccb);
1797bb79834dSMatthew Dillon 				goto restart;
1798bb79834dSMatthew Dillon 			}
179976497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
180076497a9cSMatthew Dillon 		}
180146528d33SMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
180276497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
180376497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
180476497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
180576497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
180676497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
180776497a9cSMatthew Dillon 	}
180876497a9cSMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
180976497a9cSMatthew Dillon 
181076497a9cSMatthew Dillon 	while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
181176497a9cSMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
181276497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
181376497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
181476497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
181576497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
181676497a9cSMatthew Dillon 	}
181776497a9cSMatthew Dillon 
181876497a9cSMatthew Dillon 	/*
18195502cf24SMatthew Dillon 	 * Hot-plug device detection should work at this point.  e.g. on
18205502cf24SMatthew Dillon 	 * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug
18215502cf24SMatthew Dillon 	 * detection and entering RESET (continuous COMRESET by setting INIT)
18225502cf24SMatthew Dillon 	 * will actually prevent hot-plug detection from working properly.
1823cf5f3a81SMatthew Dillon 	 *
18245502cf24SMatthew Dillon 	 * There may be cases where this will fail to work, I have some
18255502cf24SMatthew Dillon 	 * additional code to place the HBA in RESET (send continuous
18265502cf24SMatthew Dillon 	 * COMRESET) and hopefully get DIAG.X or other events when something
18275502cf24SMatthew Dillon 	 * is plugged in.  Unfortunately this isn't universal and can
18285502cf24SMatthew Dillon 	 * also prevent events from generating interrupts.
1829cf5f3a81SMatthew Dillon 	 */
18305502cf24SMatthew Dillon 
18315502cf24SMatthew Dillon #if 0
18325502cf24SMatthew Dillon 	/*
18335502cf24SMatthew Dillon 	 * Transition us to the Reset state.  Theoretically we send a
18345502cf24SMatthew Dillon 	 * continuous stream of COMRESETs in this state.
18355502cf24SMatthew Dillon 	 */
18365502cf24SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_INIT;
18375502cf24SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
18385502cf24SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
18395502cf24SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
18405502cf24SMatthew Dillon 	} else {
18415502cf24SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
18425502cf24SMatthew Dillon 	}
18435502cf24SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
18445502cf24SMatthew Dillon 	ahci_os_sleep(10);
18455502cf24SMatthew Dillon 
18465502cf24SMatthew Dillon 	/*
18475502cf24SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
18485502cf24SMatthew Dillon 	 */
18495502cf24SMatthew Dillon 	ahci_flush_tfd(ap);
18505502cf24SMatthew Dillon #endif
1851cf5f3a81SMatthew Dillon 	/* NOP */
1852cf5f3a81SMatthew Dillon }
1853cf5f3a81SMatthew Dillon 
1854cf5f3a81SMatthew Dillon /*
1855c408a8b3SMatthew Dillon  * We can't loop on the X bit, a continuous COMINIT received will make
1856c408a8b3SMatthew Dillon  * it loop forever.  Just assume one event has built up and clear X
1857c408a8b3SMatthew Dillon  * so the task file descriptor can update.
1858cf5f3a81SMatthew Dillon  */
1859cf5f3a81SMatthew Dillon void
1860cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap)
1861cf5f3a81SMatthew Dillon {
1862cf5f3a81SMatthew Dillon 	u_int32_t r;
1863cf5f3a81SMatthew Dillon 
1864cf5f3a81SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SERR);
1865c408a8b3SMatthew Dillon 	if (r & AHCI_PREG_SERR_DIAG_X)
18661980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1867cf5f3a81SMatthew Dillon }
1868cf5f3a81SMatthew Dillon 
1869cf5f3a81SMatthew Dillon /*
1870fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
1871fd8bd957SMatthew Dillon  * DISK.
1872fd8bd957SMatthew Dillon  */
1873fd8bd957SMatthew Dillon int
18741980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1875fd8bd957SMatthew Dillon {
1876fd8bd957SMatthew Dillon 	u_int32_t sig;
1877fd8bd957SMatthew Dillon 
1878fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
1879074579dfSMatthew Dillon 	if (bootverbose)
18801980eff3SMatthew Dillon 		kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1881fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1882fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
18831980eff3SMatthew Dillon 	} else if ((sig & 0xffff0000) ==
18841980eff3SMatthew Dillon 		 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
18851980eff3SMatthew Dillon 		return(ATA_PORT_T_PM);
1886fd8bd957SMatthew Dillon 	} else {
1887fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
1888fd8bd957SMatthew Dillon 	}
1889fd8bd957SMatthew Dillon }
1890fd8bd957SMatthew Dillon 
1891fd8bd957SMatthew Dillon /*
1892fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
1893fd8bd957SMatthew Dillon  */
1894258223a3SMatthew Dillon int
1895258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
1896258223a3SMatthew Dillon {
1897258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1898258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1899258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1900258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
1901258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1902258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
1903258223a3SMatthew Dillon 	int				error;
1904258223a3SMatthew Dillon 
1905258223a3SMatthew Dillon 	if (xa->datalen == 0) {
1906258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
1907258223a3SMatthew Dillon 		return (0);
1908258223a3SMatthew Dillon 	}
1909258223a3SMatthew Dillon 
1910258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
1911258223a3SMatthew Dillon 				xa->data, xa->datalen,
1912258223a3SMatthew Dillon 				ahci_load_prdt_callback,
1913258223a3SMatthew Dillon 				&prdt,
1914258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
1915258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1916258223a3SMatthew Dillon 	if (error != 0) {
1917258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1918258223a3SMatthew Dillon 		return (1);
1919258223a3SMatthew Dillon 	}
192012feb904SMatthew Dillon #if 0
1921258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
1922258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
192312feb904SMatthew Dillon #endif
1924258223a3SMatthew Dillon 
1925258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1926258223a3SMatthew Dillon 
1927b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_READ)
1928b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1929b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
1930b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1931258223a3SMatthew Dillon 
1932258223a3SMatthew Dillon 	return (0);
1933258223a3SMatthew Dillon }
1934258223a3SMatthew Dillon 
1935258223a3SMatthew Dillon /*
1936258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
1937258223a3SMatthew Dillon  * list is a temporary structure.
1938258223a3SMatthew Dillon  */
1939258223a3SMatthew Dillon static
1940258223a3SMatthew Dillon void
1941258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1942258223a3SMatthew Dillon 			int error)
1943258223a3SMatthew Dillon {
1944258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
1945258223a3SMatthew Dillon 	u_int64_t addr;
1946258223a3SMatthew Dillon 
1947258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
1948258223a3SMatthew Dillon 
1949258223a3SMatthew Dillon 	while (nsegs) {
1950258223a3SMatthew Dillon 		addr = segs->ds_addr;
1951258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1952258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
1953258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
1954258223a3SMatthew Dillon 		--nsegs;
1955258223a3SMatthew Dillon 		if (nsegs)
1956258223a3SMatthew Dillon 			++prd;
1957258223a3SMatthew Dillon 		++segs;
1958258223a3SMatthew Dillon 	}
1959258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
1960258223a3SMatthew Dillon }
1961258223a3SMatthew Dillon 
1962258223a3SMatthew Dillon void
1963258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
1964258223a3SMatthew Dillon {
1965258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1966258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1967258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1968258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1969258223a3SMatthew Dillon 
1970258223a3SMatthew Dillon 	if (xa->datalen != 0) {
1971b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_READ) {
1972258223a3SMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
1973b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTREAD);
1974b012a2caSMatthew Dillon 		}
1975b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_WRITE) {
1976b012a2caSMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
1977b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTWRITE);
1978b012a2caSMatthew Dillon 		}
1979258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
1980258223a3SMatthew Dillon 
1981f7d09f74SMatthew Dillon 		/*
1982f7d09f74SMatthew Dillon 		 * prdbc is only updated by hardware for non-NCQ commands.
1983f7d09f74SMatthew Dillon 		 */
1984f7d09f74SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1985f7d09f74SMatthew Dillon 			xa->resid = 0;
1986f7d09f74SMatthew Dillon 		} else {
198750a3ecb6SMatthew Dillon 			if (ccb->ccb_cmd_hdr->prdbc == 0 &&
198850a3ecb6SMatthew Dillon 			    ccb->ccb_xa.state == ATA_S_COMPLETE) {
1989f7d09f74SMatthew Dillon 				kprintf("%s: WARNING!  Unload prdbc resid "
1990f7d09f74SMatthew Dillon 					"was zero! tag=%d\n",
199112feb904SMatthew Dillon 					ATANAME(ap, xa->at), ccb->ccb_slot);
199212feb904SMatthew Dillon 			}
1993258223a3SMatthew Dillon 			xa->resid = xa->datalen -
1994258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
1995258223a3SMatthew Dillon 		}
1996258223a3SMatthew Dillon 	}
1997f7d09f74SMatthew Dillon }
1998258223a3SMatthew Dillon 
19995f8c1efdSMatthew Dillon /*
20005f8c1efdSMatthew Dillon  * Start a command and poll for completion.
20015f8c1efdSMatthew Dillon  *
20023209f581SMatthew Dillon  * timeout is in ms and only counts once the command gets on-chip.
20033209f581SMatthew Dillon  *
2004831bc9e3SMatthew Dillon  * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
2005831bc9e3SMatthew Dillon  * that no error occured.
2006831bc9e3SMatthew Dillon  *
20075f8c1efdSMatthew Dillon  * NOTE: If the caller specifies a NULL timeout function the caller is
20085f8c1efdSMatthew Dillon  *	 responsible for clearing hardware state on failure, but we will
20095f8c1efdSMatthew Dillon  *	 deal with removing the ccb from any pending queue.
20105f8c1efdSMatthew Dillon  *
20115f8c1efdSMatthew Dillon  * NOTE: NCQ should never be used with this function.
2012cf5f3a81SMatthew Dillon  *
2013cf5f3a81SMatthew Dillon  * NOTE: If the port is in a failed state and stopped we do not try
2014cf5f3a81SMatthew Dillon  *	 to activate the ccb.
20155f8c1efdSMatthew Dillon  */
2016258223a3SMatthew Dillon int
2017831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout,
2018831bc9e3SMatthew Dillon 	  void (*timeout_fn)(struct ahci_ccb *))
2019258223a3SMatthew Dillon {
2020258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
2021258223a3SMatthew Dillon 
2022cf5f3a81SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
2023cf5f3a81SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
2024831bc9e3SMatthew Dillon 		return(ccb->ccb_xa.state);
2025cf5f3a81SMatthew Dillon 	}
2026258223a3SMatthew Dillon 	crit_enter();
202712feb904SMatthew Dillon #if 0
202812feb904SMatthew Dillon 	kprintf("%s: Start command %02x tag=%d\n",
202912feb904SMatthew Dillon 		ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
203012feb904SMatthew Dillon 		ccb->ccb_xa.fis->command, ccb->ccb_slot);
203112feb904SMatthew Dillon #endif
2032258223a3SMatthew Dillon 	ahci_start(ccb);
20331980eff3SMatthew Dillon 
2034258223a3SMatthew Dillon 	do {
2035f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2036831bc9e3SMatthew Dillon 		switch(ccb->ccb_xa.state) {
2037831bc9e3SMatthew Dillon 		case ATA_S_ONCHIP:
2038831bc9e3SMatthew Dillon 			timeout -= ahci_os_softsleep();
2039f4553de1SMatthew Dillon 			break;
2040831bc9e3SMatthew Dillon 		case ATA_S_PENDING:
2041831bc9e3SMatthew Dillon 			ahci_os_softsleep();
2042831bc9e3SMatthew Dillon 			ahci_check_active_timeouts(ap);
2043831bc9e3SMatthew Dillon 			break;
2044831bc9e3SMatthew Dillon 		default:
2045831bc9e3SMatthew Dillon 			crit_exit();
2046831bc9e3SMatthew Dillon 			return (ccb->ccb_xa.state);
2047f4553de1SMatthew Dillon 		}
20483209f581SMatthew Dillon 	} while (timeout > 0);
20495f8c1efdSMatthew Dillon 
2050492bffafSMatthew Dillon 	if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) {
2051831bc9e3SMatthew Dillon 		kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
2052831bc9e3SMatthew Dillon 			ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
2053831bc9e3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
2054831bc9e3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
2055831bc9e3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
2056492bffafSMatthew Dillon 	}
20575f8c1efdSMatthew Dillon 
2058258223a3SMatthew Dillon 	timeout_fn(ccb);
2059831bc9e3SMatthew Dillon 
2060258223a3SMatthew Dillon 	crit_exit();
2061258223a3SMatthew Dillon 
2062831bc9e3SMatthew Dillon 	return(ccb->ccb_xa.state);
2063831bc9e3SMatthew Dillon }
2064831bc9e3SMatthew Dillon 
2065831bc9e3SMatthew Dillon /*
2066831bc9e3SMatthew Dillon  * When polling we have to check if the currently active CCB(s)
2067831bc9e3SMatthew Dillon  * have timed out as the callout will be deadlocked while we
2068831bc9e3SMatthew Dillon  * hold the port lock.
2069831bc9e3SMatthew Dillon  */
2070831bc9e3SMatthew Dillon void
2071831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap)
2072831bc9e3SMatthew Dillon {
2073831bc9e3SMatthew Dillon 	struct ahci_ccb *ccb;
2074831bc9e3SMatthew Dillon 	u_int32_t mask;
2075831bc9e3SMatthew Dillon 	int tag;
2076831bc9e3SMatthew Dillon 
2077831bc9e3SMatthew Dillon 	mask = ap->ap_active | ap->ap_sactive;
2078831bc9e3SMatthew Dillon 	while (mask) {
2079831bc9e3SMatthew Dillon 		tag = ffs(mask) - 1;
2080831bc9e3SMatthew Dillon 		mask &= ~(1 << tag);
2081831bc9e3SMatthew Dillon 		ccb = &ap->ap_ccbs[tag];
2082831bc9e3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2083831bc9e3SMatthew Dillon 			ahci_ata_cmd_timeout(ccb);
2084831bc9e3SMatthew Dillon 		}
2085831bc9e3SMatthew Dillon 	}
2086258223a3SMatthew Dillon }
2087258223a3SMatthew Dillon 
20883209f581SMatthew Dillon static
20893209f581SMatthew Dillon __inline
20903209f581SMatthew Dillon void
20913209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb)
20923209f581SMatthew Dillon {
20933209f581SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
20943209f581SMatthew Dillon 		ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
20953209f581SMatthew Dillon 		callout_reset(&ccb->ccb_timeout,
20963209f581SMatthew Dillon 			      (ccb->ccb_xa.timeout * hz + 999) / 1000,
20973209f581SMatthew Dillon 			      ahci_ata_cmd_timeout_unserialized, ccb);
20983209f581SMatthew Dillon 	}
20993209f581SMatthew Dillon }
21003209f581SMatthew Dillon 
2101258223a3SMatthew Dillon void
2102258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
2103258223a3SMatthew Dillon {
2104258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
2105258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
2106258223a3SMatthew Dillon 
2107258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2108258223a3SMatthew Dillon 
2109258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
2110258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
2111258223a3SMatthew Dillon 
2112258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
2113258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
2114258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2115258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
2116258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
2117258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2118258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
2119258223a3SMatthew Dillon 
2120258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
2121258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
2122258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2123258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
2124258223a3SMatthew Dillon 
21251980eff3SMatthew Dillon 	/*
21264c339a5fSMatthew Dillon 	 * There's no point trying to optimize this, it only shaves a few
21274c339a5fSMatthew Dillon 	 * nanoseconds so just queue the command and call our generic issue.
21281980eff3SMatthew Dillon 	 */
21294c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, ccb);
2130258223a3SMatthew Dillon }
2131258223a3SMatthew Dillon 
2132831bc9e3SMatthew Dillon /*
2133831bc9e3SMatthew Dillon  * While holding the port lock acquire exclusive access to the port.
2134831bc9e3SMatthew Dillon  *
2135831bc9e3SMatthew Dillon  * This is used when running the state machine to initialize and identify
2136831bc9e3SMatthew Dillon  * targets over a port multiplier.  Setting exclusive access prevents
2137831bc9e3SMatthew Dillon  * ahci_port_intr() from activating any requests sitting on the pending
2138831bc9e3SMatthew Dillon  * queue.
2139831bc9e3SMatthew Dillon  */
2140831bc9e3SMatthew Dillon void
2141831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2142831bc9e3SMatthew Dillon {
2143831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
2144831bc9e3SMatthew Dillon 	ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
2145831bc9e3SMatthew Dillon 	while (ap->ap_active || ap->ap_sactive) {
2146831bc9e3SMatthew Dillon 		ahci_port_intr(ap, 1);
2147831bc9e3SMatthew Dillon 		ahci_os_softsleep();
2148831bc9e3SMatthew Dillon 	}
2149831bc9e3SMatthew Dillon }
2150831bc9e3SMatthew Dillon 
2151831bc9e3SMatthew Dillon void
2152831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2153831bc9e3SMatthew Dillon {
2154831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2155831bc9e3SMatthew Dillon 	ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
21564c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
2157831bc9e3SMatthew Dillon }
2158831bc9e3SMatthew Dillon 
21591980eff3SMatthew Dillon /*
21604c339a5fSMatthew Dillon  * If ccb is not NULL enqueue and/or issue it.
21614c339a5fSMatthew Dillon  *
21624c339a5fSMatthew Dillon  * If ccb is NULL issue whatever we can from the queue.  However, nothing
21634c339a5fSMatthew Dillon  * new is issued if the exclusive access flag is set or expired ccb's are
21644c339a5fSMatthew Dillon  * present.
21654c339a5fSMatthew Dillon  *
21664c339a5fSMatthew Dillon  * If existing commands are still active (ap_active/ap_sactive) we can only
21674c339a5fSMatthew Dillon  * issue matching new commands.
21681980eff3SMatthew Dillon  */
21694c339a5fSMatthew Dillon void
21704c339a5fSMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
21714c339a5fSMatthew Dillon {
21724c339a5fSMatthew Dillon 	u_int32_t		mask;
21734c339a5fSMatthew Dillon 	int			limit;
2174258223a3SMatthew Dillon 
21751980eff3SMatthew Dillon 	/*
21764c339a5fSMatthew Dillon 	 * Enqueue the ccb.
21774c339a5fSMatthew Dillon 	 *
21784c339a5fSMatthew Dillon 	 * If just running the queue and in exclusive access mode we
21794c339a5fSMatthew Dillon 	 * just return.  Also in this case if there are any expired ccb's
21804c339a5fSMatthew Dillon 	 * we want to clear the queue so the port can be safely stopped.
21814c339a5fSMatthew Dillon 	 */
21824c339a5fSMatthew Dillon 	if (ccb) {
21834c339a5fSMatthew Dillon 		TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
21844c339a5fSMatthew Dillon 	} else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
21854c339a5fSMatthew Dillon 		return;
21864c339a5fSMatthew Dillon 	}
21874c339a5fSMatthew Dillon 
21884c339a5fSMatthew Dillon 	/*
21894c339a5fSMatthew Dillon 	 * Pull the next ccb off the queue and run it if possible.
2190c1fd1d86SMatthew Dillon 	 *
2191c1fd1d86SMatthew Dillon 	 * The error CCB supercedes all normal queue operations and
2192c1fd1d86SMatthew Dillon 	 * implies exclusive access while the error CCB is active.
21934c339a5fSMatthew Dillon 	 */
2194c1fd1d86SMatthew Dillon 	if (ccb != ap->ap_err_ccb) {
21954c339a5fSMatthew Dillon 		if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
21964c339a5fSMatthew Dillon 			return;
2197c1fd1d86SMatthew Dillon 		if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2198c1fd1d86SMatthew Dillon 			kprintf("DELAY CCB slot %d\n", ccb->ccb_slot);
2199c1fd1d86SMatthew Dillon 			return;
2200c1fd1d86SMatthew Dillon 		}
2201c1fd1d86SMatthew Dillon 	}
22024c339a5fSMatthew Dillon 
220312feb904SMatthew Dillon 	/*
220412feb904SMatthew Dillon 	 * Handle exclusivity requirements.
220512feb904SMatthew Dillon 	 *
220612feb904SMatthew Dillon 	 * ATA_F_EXCLUSIVE is used when we want to be the only command
220712feb904SMatthew Dillon 	 * running.
220812feb904SMatthew Dillon 	 *
220912feb904SMatthew Dillon 	 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
221012feb904SMatthew Dillon 	 * back into the ccb on a normal (non-errored) command completion.
221112feb904SMatthew Dillon 	 * For example, for PM requests to target 15.  Because the AHCI
221212feb904SMatthew Dillon 	 * spec does not stop the command processor and has only one rfis
221312feb904SMatthew Dillon 	 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
221412feb904SMatthew Dillon 	 * Otherwise multiple completions can destroy the rfis data before
221512feb904SMatthew Dillon 	 * we have a chance to copy it.
221612feb904SMatthew Dillon 	 */
221712feb904SMatthew Dillon 	if (ap->ap_active & ~ap->ap_expired) {
221812feb904SMatthew Dillon 		/*
221912feb904SMatthew Dillon 		 * There may be multiple ccb's already running,
222012feb904SMatthew Dillon 		 * if any are running and ap_run_flags sets
222112feb904SMatthew Dillon 		 * one of these flags then we know only one is
222212feb904SMatthew Dillon 		 * running.
222312feb904SMatthew Dillon 		 *
222412feb904SMatthew Dillon 		 * XXX Current AUTOSENSE code forces exclusivity
222512feb904SMatthew Dillon 		 *     to simplify the code.
222612feb904SMatthew Dillon 		 */
222712feb904SMatthew Dillon 		if (ap->ap_run_flags &
222812feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
222912feb904SMatthew Dillon 			return;
223012feb904SMatthew Dillon 		}
223112feb904SMatthew Dillon 
223212feb904SMatthew Dillon 		if (ccb->ccb_xa.flags &
223312feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
223412feb904SMatthew Dillon 			return;
223512feb904SMatthew Dillon 		}
223612feb904SMatthew Dillon 	}
223712feb904SMatthew Dillon 
22384c339a5fSMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
22394c339a5fSMatthew Dillon 		/*
22404c339a5fSMatthew Dillon 		 * The next command is a NCQ command and can be issued as
22414c339a5fSMatthew Dillon 		 * long as currently active commands are not standard.
22424c339a5fSMatthew Dillon 		 */
22434c339a5fSMatthew Dillon 		if (ap->ap_active) {
22444c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
22454c339a5fSMatthew Dillon 			return;
22464c339a5fSMatthew Dillon 		}
22474c339a5fSMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
22484c339a5fSMatthew Dillon 
22494c339a5fSMatthew Dillon 		mask = 0;
22504c339a5fSMatthew Dillon 		do {
22514c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2252d16d3400SMatthew Dillon 			KKASSERT((mask & (1 << ccb->ccb_slot)) == 0);
22534c339a5fSMatthew Dillon 			mask |= 1 << ccb->ccb_slot;
2254d16d3400SMatthew Dillon 			KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2255d16d3400SMatthew Dillon 			KKASSERT(ccb == &ap->ap_ccbs[ccb->ccb_slot]);
22564c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
225712feb904SMatthew Dillon 			ahci_start_timeout(ccb);
225812feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
22594c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
226012feb904SMatthew Dillon 		} while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
226112feb904SMatthew Dillon 			 (ap->ap_run_flags &
226212feb904SMatthew Dillon 			     (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
22634c339a5fSMatthew Dillon 
2264d16d3400SMatthew Dillon 		KKASSERT(((ap->ap_active | ap->ap_sactive) & mask) == 0);
2265d16d3400SMatthew Dillon 
22664c339a5fSMatthew Dillon 		ap->ap_sactive |= mask;
22674c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SACT, mask);
22684c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CI, mask);
22694c339a5fSMatthew Dillon 	} else {
22704c339a5fSMatthew Dillon 		/*
22714c339a5fSMatthew Dillon 		 * The next command is a standard command and can be issued
22724c339a5fSMatthew Dillon 		 * as long as currently active commands are not NCQ.
22734c339a5fSMatthew Dillon 		 *
22744c339a5fSMatthew Dillon 		 * We limit ourself to 1 command if we have a port multiplier,
22754c339a5fSMatthew Dillon 		 * (at least without FBSS support), otherwise timeouts on
22764c339a5fSMatthew Dillon 		 * one port can race completions on other ports (see
22774c339a5fSMatthew Dillon 		 * ahci_ata_cmd_timeout() for more information).
22784c339a5fSMatthew Dillon 		 *
22794c339a5fSMatthew Dillon 		 * If not on a port multiplier generally allow up to 4
22804c339a5fSMatthew Dillon 		 * standard commands to be enqueued.  Remember that the
22814c339a5fSMatthew Dillon 		 * command processor will still process them sequentially.
22821980eff3SMatthew Dillon 		 */
22831980eff3SMatthew Dillon 		if (ap->ap_sactive)
2284258223a3SMatthew Dillon 			return;
22854c339a5fSMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
22864c339a5fSMatthew Dillon 			limit = 1;
22874c339a5fSMatthew Dillon 		else if (ap->ap_sc->sc_ncmds > 4)
22884c339a5fSMatthew Dillon 			limit = 4;
22894c339a5fSMatthew Dillon 		else
22904c339a5fSMatthew Dillon 			limit = 2;
2291258223a3SMatthew Dillon 
22924c339a5fSMatthew Dillon 		while (ap->ap_active_cnt < limit && ccb &&
22934c339a5fSMatthew Dillon 		       (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
22944c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2295d16d3400SMatthew Dillon 			KKASSERT(((ap->ap_active | ap->ap_sactive) &
2296d16d3400SMatthew Dillon 				  (1 << ccb->ccb_slot)) == 0);
22974c339a5fSMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
2298258223a3SMatthew Dillon 			ap->ap_active_cnt++;
229912feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
23004c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
230112feb904SMatthew Dillon 			ahci_start_timeout(ccb);
2302d16d3400SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
230322726f69SMatthew Dillon 			if ((ap->ap_run_flags &
230422726f69SMatthew Dillon 			    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
230522726f69SMatthew Dillon 				break;
230622726f69SMatthew Dillon 			}
23074c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
230812feb904SMatthew Dillon 			if (ccb && (ccb->ccb_xa.flags &
230912feb904SMatthew Dillon 				    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
231012feb904SMatthew Dillon 				break;
231112feb904SMatthew Dillon 			}
23121980eff3SMatthew Dillon 		}
2313258223a3SMatthew Dillon 	}
2314258223a3SMatthew Dillon }
2315258223a3SMatthew Dillon 
2316258223a3SMatthew Dillon void
2317258223a3SMatthew Dillon ahci_intr(void *arg)
2318258223a3SMatthew Dillon {
2319258223a3SMatthew Dillon 	struct ahci_softc	*sc = arg;
2320f4553de1SMatthew Dillon 	struct ahci_port	*ap;
232112feb904SMatthew Dillon 	u_int32_t		is;
232212feb904SMatthew Dillon 	u_int32_t		ack;
2323258223a3SMatthew Dillon 	int			port;
2324258223a3SMatthew Dillon 
2325f4553de1SMatthew Dillon 	/*
2326f4553de1SMatthew Dillon 	 * Check if the master enable is up, and whether any interrupts are
2327f4553de1SMatthew Dillon 	 * pending.
2328f4553de1SMatthew Dillon 	 */
2329f4553de1SMatthew Dillon 	if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2330f4553de1SMatthew Dillon 		return;
2331258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
233212feb904SMatthew Dillon 	if (is == 0 || is == 0xffffffff) {
2333258223a3SMatthew Dillon 		return;
233412feb904SMatthew Dillon 	}
233512feb904SMatthew Dillon 	is &= sc->sc_portmask;
2336258223a3SMatthew Dillon 
2337258223a3SMatthew Dillon #ifdef AHCI_COALESCE
2338258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
2339258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
2340258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2341258223a3SMatthew Dillon 		    DEVNAME(sc));
2342258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
2343258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
2344258223a3SMatthew Dillon 	}
2345258223a3SMatthew Dillon #endif
2346258223a3SMatthew Dillon 
2347f4553de1SMatthew Dillon 	/*
2348f4553de1SMatthew Dillon 	 * Process interrupts for each port in a non-blocking fashion.
234912feb904SMatthew Dillon 	 *
23503d102df7SMatthew Dillon 	 * The global IS bit is supposed to be forced on if any unmasked
23513d102df7SMatthew Dillon 	 * port interrupt is pending, even if we clear it.
23523d102df7SMatthew Dillon 	 *
23533d102df7SMatthew Dillon 	 * However it would appear that it is simply latched on some parts,
23543d102df7SMatthew Dillon 	 * which means we have to clear it BEFORE processing the status bits
23553d102df7SMatthew Dillon 	 * to avoid races.
2356f4553de1SMatthew Dillon 	 */
23573d102df7SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, is);
235812feb904SMatthew Dillon 	for (ack = 0; is; is &= ~(1 << port)) {
2359258223a3SMatthew Dillon 		port = ffs(is) - 1;
236012feb904SMatthew Dillon 		ack |= 1 << port;
236112feb904SMatthew Dillon 
2362f4553de1SMatthew Dillon 		ap = sc->sc_ports[port];
236312feb904SMatthew Dillon 		if (ap == NULL)
236412feb904SMatthew Dillon 			continue;
236512feb904SMatthew Dillon 
2366f4553de1SMatthew Dillon 		if (ahci_os_lock_port_nb(ap) == 0) {
2367f4553de1SMatthew Dillon 			ahci_port_intr(ap, 0);
2368f4553de1SMatthew Dillon 			ahci_os_unlock_port(ap);
2369f4553de1SMatthew Dillon 		} else {
2370f4553de1SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2371f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2372f4553de1SMatthew Dillon 		}
2373f4553de1SMatthew Dillon 	}
2374258223a3SMatthew Dillon }
2375258223a3SMatthew Dillon 
2376f4553de1SMatthew Dillon /*
2377f4553de1SMatthew Dillon  * Core called from helper thread.
2378f4553de1SMatthew Dillon  */
23793209f581SMatthew Dillon void
2380f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask)
2381f4553de1SMatthew Dillon {
2382f4553de1SMatthew Dillon 	/*
2383f4553de1SMatthew Dillon 	 * Process any expired timedouts.
2384f4553de1SMatthew Dillon 	 */
2385f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
2386f4553de1SMatthew Dillon 	if (mask & AP_SIGF_TIMEOUT) {
2387831bc9e3SMatthew Dillon 		ahci_check_active_timeouts(ap);
2388f4553de1SMatthew Dillon 	}
2389f4553de1SMatthew Dillon 
2390f4553de1SMatthew Dillon 	/*
2391f4553de1SMatthew Dillon 	 * Process port interrupts which require a higher level of
2392f4553de1SMatthew Dillon 	 * intervention.
2393f4553de1SMatthew Dillon 	 */
2394f4553de1SMatthew Dillon 	if (mask & AP_SIGF_PORTINT) {
2395f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2396f4553de1SMatthew Dillon 		ahci_port_interrupt_enable(ap);
239712feb904SMatthew Dillon 	} else if (ap->ap_probe != ATA_PROBE_FAILED) {
239812feb904SMatthew Dillon 		ahci_port_intr(ap, 1);
239912feb904SMatthew Dillon 		ahci_port_interrupt_enable(ap);
2400f4553de1SMatthew Dillon 	}
2401d16d3400SMatthew Dillon 	ahci_os_unlock_port(ap);
2402f4553de1SMatthew Dillon }
2403f4553de1SMatthew Dillon 
2404f4553de1SMatthew Dillon /*
2405f4553de1SMatthew Dillon  * Core per-port interrupt handler.
2406f4553de1SMatthew Dillon  *
2407f4553de1SMatthew Dillon  * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2408f4553de1SMatthew Dillon  * deal with normal command completions which do not require blocking.
2409f4553de1SMatthew Dillon  */
2410f4553de1SMatthew Dillon void
2411f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable)
2412258223a3SMatthew Dillon {
2413258223a3SMatthew Dillon 	struct ahci_softc	*sc = ap->ap_sc;
24143209f581SMatthew Dillon 	u_int32_t		is, ci_saved, ci_masked;
241522181ab7SMatthew Dillon 	int			slot;
2416492bffafSMatthew Dillon 	int			stopped = 0;
2417258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
24181980eff3SMatthew Dillon 	struct ata_port		*ccb_at = NULL;
2419258223a3SMatthew Dillon 	volatile u_int32_t	*active;
2420f4553de1SMatthew Dillon 	const u_int32_t		blockable_mask = AHCI_PREG_IS_TFES |
2421f4553de1SMatthew Dillon 						 AHCI_PREG_IS_IFS |
2422f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PCS |
2423f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PRCS |
2424f4553de1SMatthew Dillon 						 AHCI_PREG_IS_HBFS |
2425f4553de1SMatthew Dillon 						 AHCI_PREG_IS_OFS |
2426f4553de1SMatthew Dillon 						 AHCI_PREG_IS_UFS;
2427f4553de1SMatthew Dillon 
2428492bffafSMatthew Dillon 	enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART,
2429492bffafSMatthew Dillon 	       NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2430258223a3SMatthew Dillon 
2431f4553de1SMatthew Dillon 	/*
2432f4553de1SMatthew Dillon 	 * All basic command completions are always processed.
2433f4553de1SMatthew Dillon 	 */
243412feb904SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
2435cec07d75SMatthew Dillon 	if (is & AHCI_PREG_IS_DPS)
2436cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2437258223a3SMatthew Dillon 
2438f4553de1SMatthew Dillon 	/*
2439f4553de1SMatthew Dillon 	 * If we can't block then we can't handle these here.  Disable
2440f4553de1SMatthew Dillon 	 * the interrupts in question so we don't live-lock, the helper
2441f4553de1SMatthew Dillon 	 * thread will re-enable them.
2442f4553de1SMatthew Dillon 	 *
2443f4553de1SMatthew Dillon 	 * If the port is in a completely failed state we do not want
2444dbef6246SMatthew Dillon 	 * to drop through to failed-command-processing if blockable is 0,
2445f4553de1SMatthew Dillon 	 * just let the thread deal with it all.
2446dbef6246SMatthew Dillon 	 *
2447dbef6246SMatthew Dillon 	 * Otherwise we fall through and still handle DHRS and any commands
2448dbef6246SMatthew Dillon 	 * which completed normally.  Even if we are errored we haven't
2449dbef6246SMatthew Dillon 	 * stopped the port yet so CI/SACT are still good.
2450f4553de1SMatthew Dillon 	 */
2451f4553de1SMatthew Dillon 	if (blockable == 0) {
2452f4553de1SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
245312feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2454f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2455f4553de1SMatthew Dillon 			return;
2456f4553de1SMatthew Dillon 		}
2457f4553de1SMatthew Dillon 		if (is & blockable_mask) {
245812feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2459f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
246012feb904SMatthew Dillon 			return;
2461f4553de1SMatthew Dillon 		}
2462f4553de1SMatthew Dillon 	}
2463f4553de1SMatthew Dillon 
24643209f581SMatthew Dillon 	/*
2465f4553de1SMatthew Dillon 	 * Either NCQ or non-NCQ commands will be active, never both.
24663209f581SMatthew Dillon 	 */
2467258223a3SMatthew Dillon 	if (ap->ap_sactive) {
2468258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
2469258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
2470258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2471258223a3SMatthew Dillon 		active = &ap->ap_sactive;
2472258223a3SMatthew Dillon 	} else {
2473258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2474258223a3SMatthew Dillon 		active = &ap->ap_active;
2475258223a3SMatthew Dillon 	}
247612feb904SMatthew Dillon 	KKASSERT(!(ap->ap_sactive && ap->ap_active));
2477d16d3400SMatthew Dillon 	KKASSERT((ci_saved & (ap->ap_sactive | ap->ap_active)) == ci_saved);
247812feb904SMatthew Dillon #if 0
247912feb904SMatthew Dillon 	kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
248012feb904SMatthew Dillon 		ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
248112feb904SMatthew Dillon 		ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
248212feb904SMatthew Dillon #endif
2483258223a3SMatthew Dillon 
2484492bffafSMatthew Dillon 	/*
2485492bffafSMatthew Dillon 	 * Ignore AHCI_PREG_IS_PRCS when link power management is on
2486492bffafSMatthew Dillon 	 */
2487795adb22SMatthew Dillon 	if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2488795adb22SMatthew Dillon 		is &= ~AHCI_PREG_IS_PRCS;
2489795adb22SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
2490795adb22SMatthew Dillon 			    AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2491795adb22SMatthew Dillon 	}
2492795adb22SMatthew Dillon 
2493cf5f3a81SMatthew Dillon 	/*
2494f4553de1SMatthew Dillon 	 * Command failed (blockable).
2495f4553de1SMatthew Dillon 	 *
2496f4553de1SMatthew Dillon 	 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
24971980eff3SMatthew Dillon 	 *
24981980eff3SMatthew Dillon 	 * This stops command processing.
2499cf5f3a81SMatthew Dillon 	 */
2500492bffafSMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
2501258223a3SMatthew Dillon 		u_int32_t tfd, serr;
2502258223a3SMatthew Dillon 		int	err_slot;
2503258223a3SMatthew Dillon 
250412feb904SMatthew Dillon process_error:
2505258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
2506258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2507258223a3SMatthew Dillon 
2508cf5f3a81SMatthew Dillon 		/*
250912feb904SMatthew Dillon 		 * Load the error slot and restart command processing.
251012feb904SMatthew Dillon 		 * CLO if we need to.  The error slot may not be valid.
251112feb904SMatthew Dillon 		 * MUST BE DONE BEFORE CLEARING ST!
251212feb904SMatthew Dillon 		 *
251312feb904SMatthew Dillon 		 * Cycle ST.
251412feb904SMatthew Dillon 		 *
251512feb904SMatthew Dillon 		 * It is unclear but we may have to clear SERR to reenable
251612feb904SMatthew Dillon 		 * error processing.
2517cf5f3a81SMatthew Dillon 		 */
251812feb904SMatthew Dillon 		err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
251912feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
252012feb904SMatthew Dillon 					      AHCI_PREG_IS_PSS |
252112feb904SMatthew Dillon 					      AHCI_PREG_IS_DHRS |
252212feb904SMatthew Dillon 					      AHCI_PREG_IS_SDBS);
252312feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
252412feb904SMatthew Dillon 			AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
252512feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2526258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
252712feb904SMatthew Dillon 		ahci_os_hardsleep(10);
252812feb904SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
252912feb904SMatthew Dillon 			kprintf("%s: Issuing CLO\n", PORTNAME(ap));
253012feb904SMatthew Dillon 			ahci_port_clo(ap);
253112feb904SMatthew Dillon 		}
2532492bffafSMatthew Dillon 
2533492bffafSMatthew Dillon 		/*
2534492bffafSMatthew Dillon 		 * We are now stopped and need a restart.  If we have to
2535492bffafSMatthew Dillon 		 * process a NCQ error we will temporarily start and then
2536492bffafSMatthew Dillon 		 * stop the port again, so this condition holds.
2537492bffafSMatthew Dillon 		 */
2538492bffafSMatthew Dillon 		stopped = 1;
253922181ab7SMatthew Dillon 		need = NEED_RESTART;
2540258223a3SMatthew Dillon 
254150a3ecb6SMatthew Dillon 		/*
254250a3ecb6SMatthew Dillon 		 * ATAPI errors are fairly common from probing, just
254350a3ecb6SMatthew Dillon 		 * report disk errors or if bootverbose is on.
254450a3ecb6SMatthew Dillon 		 */
254550a3ecb6SMatthew Dillon 		if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
254612feb904SMatthew Dillon 			kprintf("%s: TFES slot %d ci_saved = %08x\n",
254712feb904SMatthew Dillon 				PORTNAME(ap), err_slot, ci_saved);
254850a3ecb6SMatthew Dillon 		}
2549258223a3SMatthew Dillon 
25501980eff3SMatthew Dillon 		/*
255112feb904SMatthew Dillon 		 * If we got an error on an error CCB just complete it
255212feb904SMatthew Dillon 		 * with an error.  ci_saved has the mask to restart
255312feb904SMatthew Dillon 		 * (the err_ccb will be removed from it by finish_error).
25541980eff3SMatthew Dillon 		 */
255512feb904SMatthew Dillon 		if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
255612feb904SMatthew Dillon 			err_slot = ap->ap_err_ccb->ccb_slot;
255712feb904SMatthew Dillon 			goto finish_error;
2558258223a3SMatthew Dillon 		}
2559258223a3SMatthew Dillon 
25601980eff3SMatthew Dillon 		/*
256112feb904SMatthew Dillon 		 * If NCQ commands were active get the error slot from
256212feb904SMatthew Dillon 		 * the log page.  NCQ is not supported for PM's so this
256312feb904SMatthew Dillon 		 * is a direct-attached target.
25641980eff3SMatthew Dillon 		 *
256512feb904SMatthew Dillon 		 * Otherwise if no commands were active we have a problem.
256612feb904SMatthew Dillon 		 *
256712feb904SMatthew Dillon 		 * Otherwise if the error slot is bad we have a problem.
256812feb904SMatthew Dillon 		 *
256912feb904SMatthew Dillon 		 * Otherwise process the error for the slot.
25701980eff3SMatthew Dillon 		 */
257112feb904SMatthew Dillon 		if (ap->ap_sactive) {
2572492bffafSMatthew Dillon 			ahci_port_start(ap);
257312feb904SMatthew Dillon 			err_slot = ahci_port_read_ncq_error(ap, 0);
2574492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
257512feb904SMatthew Dillon 		} else if (ap->ap_active == 0) {
257612feb904SMatthew Dillon 			kprintf("%s: TFES with no commands pending\n",
257712feb904SMatthew Dillon 				PORTNAME(ap));
257812feb904SMatthew Dillon 			err_slot = -1;
257912feb904SMatthew Dillon 		} else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
258012feb904SMatthew Dillon 			kprintf("%s: bad error slot %d\n",
2581258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
258212feb904SMatthew Dillon 			err_slot = -1;
2583258223a3SMatthew Dillon 		} else {
258412feb904SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
258512feb904SMatthew Dillon 
25864c339a5fSMatthew Dillon 			/*
258712feb904SMatthew Dillon 			 * Validate the errored ccb.  Note that ccb_at can
258812feb904SMatthew Dillon 			 * be NULL for direct-attached ccb's.
258912feb904SMatthew Dillon 			 *
259012feb904SMatthew Dillon 			 * Copy received taskfile data from the RFIS.
25914c339a5fSMatthew Dillon 			 */
259212feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
259312feb904SMatthew Dillon 				ccb_at = ccb->ccb_xa.at;
259412feb904SMatthew Dillon 				memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
259512feb904SMatthew Dillon 				       sizeof(struct ata_fis_d2h));
259650a3ecb6SMatthew Dillon 				if (bootverbose) {
259750a3ecb6SMatthew Dillon 					kprintf("%s: Copying rfis slot %d\n",
259812feb904SMatthew Dillon 						ATANAME(ap, ccb_at), err_slot);
259950a3ecb6SMatthew Dillon 				}
260012feb904SMatthew Dillon 			} else {
260112feb904SMatthew Dillon 				kprintf("%s: Cannot copy rfis, CCB slot "
260212feb904SMatthew Dillon 					"%d is not on-chip (state=%d)\n",
260312feb904SMatthew Dillon 					ATANAME(ap, ccb->ccb_xa.at),
260412feb904SMatthew Dillon 					err_slot, ccb->ccb_xa.state);
260512feb904SMatthew Dillon 				err_slot = -1;
260612feb904SMatthew Dillon 			}
2607258223a3SMatthew Dillon 		}
2608258223a3SMatthew Dillon 
2609258223a3SMatthew Dillon 		/*
261012feb904SMatthew Dillon 		 * If we could not determine the errored slot then
261112feb904SMatthew Dillon 		 * reset the port.
2612258223a3SMatthew Dillon 		 */
261312feb904SMatthew Dillon 		if (err_slot < 0) {
261412feb904SMatthew Dillon 			kprintf("%s: TFES: Unable to determine errored slot\n",
261512feb904SMatthew Dillon 				PORTNAME(ap));
26161980eff3SMatthew Dillon 			if (ap->ap_flags & AP_F_IN_RESET)
26171980eff3SMatthew Dillon 				goto fatal;
2618258223a3SMatthew Dillon 			goto failall;
2619258223a3SMatthew Dillon 		}
2620258223a3SMatthew Dillon 
262112feb904SMatthew Dillon 		/*
262212feb904SMatthew Dillon 		 * Finish error on slot.  We will restart ci_saved
262312feb904SMatthew Dillon 		 * commands except the errored slot which we generate
262412feb904SMatthew Dillon 		 * a failure for.
262512feb904SMatthew Dillon 		 */
262612feb904SMatthew Dillon finish_error:
262712feb904SMatthew Dillon 		ccb = &ap->ap_ccbs[err_slot];
2628258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
2629258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2630258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
26311980eff3SMatthew Dillon 	} else if (is & AHCI_PREG_IS_DHRS) {
26321980eff3SMatthew Dillon 		/*
2633f4553de1SMatthew Dillon 		 * Command posted D2H register FIS to the rfis (non-blocking).
2634f4553de1SMatthew Dillon 		 *
263512feb904SMatthew Dillon 		 * A normal completion with an error may set DHRS instead
263612feb904SMatthew Dillon 		 * of TFES.  The CCS bits are only valid if ERR was set.
263712feb904SMatthew Dillon 		 * If ERR is set command processing was probably stopped.
26388bf6a3ffSMatthew Dillon 		 *
263912feb904SMatthew Dillon 		 * If ERR was not set we can only copy-back data for
264012feb904SMatthew Dillon 		 * exclusive-mode commands because otherwise we won't know
264112feb904SMatthew Dillon 		 * which tag the rfis belonged to.
264212feb904SMatthew Dillon 		 *
264312feb904SMatthew Dillon 		 * err_slot must be read from the CCS before any other port
264412feb904SMatthew Dillon 		 * action, such as stopping the port.
264512feb904SMatthew Dillon 		 *
264612feb904SMatthew Dillon 		 * WARNING!	This is not well documented in the AHCI spec.
264712feb904SMatthew Dillon 		 *		It can be found in the state machine tables
264812feb904SMatthew Dillon 		 *		but not in the explanations.
26491980eff3SMatthew Dillon 		 */
265012feb904SMatthew Dillon 		u_int32_t tfd;
265112feb904SMatthew Dillon 		u_int32_t cmd;
26521980eff3SMatthew Dillon 		int err_slot;
26531980eff3SMatthew Dillon 
265412feb904SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
265512feb904SMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
265612feb904SMatthew Dillon 
26573d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
265812feb904SMatthew Dillon 		if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
265912feb904SMatthew Dillon 		    (cmd & AHCI_PREG_CMD_CR) == 0) {
26601980eff3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(
26611980eff3SMatthew Dillon 						ahci_pread(ap, AHCI_PREG_CMD));
26621980eff3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
266312feb904SMatthew Dillon 			kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
266412feb904SMatthew Dillon 				PORTNAME(ap),
266512feb904SMatthew Dillon 				tfd, AHCI_PFMT_TFD_STS,
266612feb904SMatthew Dillon 				err_slot, ccb->ccb_xa.fis->command);
266712feb904SMatthew Dillon 			goto process_error;
2668258223a3SMatthew Dillon 		}
266912feb904SMatthew Dillon 		/*
267012feb904SMatthew Dillon 		 * NO ELSE... copy back is in the normal command completion
267112feb904SMatthew Dillon 		 * code and only if no error occured and ATA_F_AUTOSENSE
267212feb904SMatthew Dillon 		 * was set.
267312feb904SMatthew Dillon 		 */
26741980eff3SMatthew Dillon 	}
26751980eff3SMatthew Dillon 
26761980eff3SMatthew Dillon 	/*
2677f4553de1SMatthew Dillon 	 * Device notification to us (non-blocking)
26781980eff3SMatthew Dillon 	 *
267912feb904SMatthew Dillon 	 * NOTE!  On some parts notification bits can cause an IPMS
268012feb904SMatthew Dillon 	 *	  interrupt instead of a SDBS interrupt.
2681cec07d75SMatthew Dillon 	 *
268212feb904SMatthew Dillon 	 * NOTE!  On some parts (e.g. VBOX, probably intel ICHx),
268312feb904SMatthew Dillon 	 *	  SDBS notifies us of the completion of a NCQ command
268412feb904SMatthew Dillon 	 *	  and DBS does not.
26851980eff3SMatthew Dillon 	 */
268612feb904SMatthew Dillon 	if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
26871980eff3SMatthew Dillon 		u_int32_t data;
26881980eff3SMatthew Dillon 
268912feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
269012feb904SMatthew Dillon 				AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
269112feb904SMatthew Dillon 		if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
26921980eff3SMatthew Dillon 			data = ahci_pread(ap, AHCI_PREG_SNTF);
2693cec07d75SMatthew Dillon 			if (data) {
269412feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_IS,
269512feb904SMatthew Dillon 						AHCI_PREG_IS_SDBS);
269612feb904SMatthew Dillon 				kprintf("%s: NOTIFY %08x\n",
269712feb904SMatthew Dillon 					PORTNAME(ap), data);
269812feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SERR,
269912feb904SMatthew Dillon 						AHCI_PREG_SERR_DIAG_N);
27003209f581SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SNTF, data);
27013209f581SMatthew Dillon 				ahci_cam_changed(ap, NULL, -1);
27021980eff3SMatthew Dillon 			}
27031980eff3SMatthew Dillon 		}
270412feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
270512feb904SMatthew Dillon 	}
27063209f581SMatthew Dillon 
27073209f581SMatthew Dillon 	/*
2708492bffafSMatthew Dillon 	 * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set.
2709f4553de1SMatthew Dillon 	 *
27103209f581SMatthew Dillon 	 * Spurious IFS errors can occur while we are doing a reset
2711492bffafSMatthew Dillon 	 * sequence through a PM, probably due to an unexpected FIS
2712492bffafSMatthew Dillon 	 * being received during the PM target reset sequence.  Chipsets
2713492bffafSMatthew Dillon 	 * are supposed to mask these events but some do not.
2714492bffafSMatthew Dillon 	 *
2715492bffafSMatthew Dillon 	 * Try to recover from the condition.
27163209f581SMatthew Dillon 	 */
27173209f581SMatthew Dillon 	if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
27181980eff3SMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
27193209f581SMatthew Dillon 		if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2720492bffafSMatthew Dillon 			kprintf("%s: IFS during PM probe (ignored) "
2721492bffafSMatthew Dillon 				"IS=%b, SERR=%b\n",
27221980eff3SMatthew Dillon 				PORTNAME(ap),
27231980eff3SMatthew Dillon 				is, AHCI_PFMT_IS,
27241980eff3SMatthew Dillon 				serr, AHCI_PFMT_SERR);
27253209f581SMatthew Dillon 			ap->ap_flags |= AP_F_IFS_IGNORED;
27263209f581SMatthew Dillon 		}
2727492bffafSMatthew Dillon 
2728492bffafSMatthew Dillon 		/*
2729492bffafSMatthew Dillon 		 * Try to clear the error condition.  The IFS error killed
2730492bffafSMatthew Dillon 		 * the port so stop it so we can restart it.
2731492bffafSMatthew Dillon 		 */
27321980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
27333d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
27341980eff3SMatthew Dillon 		is &= ~AHCI_PREG_IS_IFS;
2735492bffafSMatthew Dillon 		need = NEED_RESTART;
273612feb904SMatthew Dillon 		goto failall;
27371980eff3SMatthew Dillon 	}
2738258223a3SMatthew Dillon 
2739258223a3SMatthew Dillon 	/*
2740f4553de1SMatthew Dillon 	 * Port change (hot-plug) (blockable).
2741258223a3SMatthew Dillon 	 *
2742492bffafSMatthew Dillon 	 * A PRCS interrupt can occur:
2743492bffafSMatthew Dillon 	 *	(1) On hot-unplug / normal-unplug (phy lost)
2744492bffafSMatthew Dillon 	 *	(2) Sometimes on hot-plug too.
2745258223a3SMatthew Dillon 	 *
2746492bffafSMatthew Dillon 	 * A PCS interrupt can occur in a number of situations:
2747492bffafSMatthew Dillon 	 *	(1) On hot-plug once communication is established
2748492bffafSMatthew Dillon 	 *	(2) On hot-unplug sometimes.
2749492bffafSMatthew Dillon 	 *	(3) For chipsets with badly written firmware it can occur
2750492bffafSMatthew Dillon 	 *	    during INIT/RESET sequences due to the device reset.
2751492bffafSMatthew Dillon 	 *	(4) For chipsets with badly written firmware it can occur
2752492bffafSMatthew Dillon 	 *	    when it thinks an unsolicited COMRESET is received
2753492bffafSMatthew Dillon 	 *	    during a INIT/RESET sequence, even though we actually
2754492bffafSMatthew Dillon 	 *	    did request it.
2755258223a3SMatthew Dillon 	 *
275622181ab7SMatthew Dillon 	 * XXX We can then check the CPS (Cold Presence State) bit, if
275722181ab7SMatthew Dillon 	 * supported, to determine if a device is plugged in or not and do
275822181ab7SMatthew Dillon 	 * the right thing.
275922181ab7SMatthew Dillon 	 *
2760492bffafSMatthew Dillon 	 * PCS interrupts are cleared by clearing DIAG_X.  If this occurs
2761492bffafSMatthew Dillon 	 * command processing is automatically stopped (CR goes inactive)
2762492bffafSMatthew Dillon 	 * and the port must be stopped and restarted.
2763492bffafSMatthew Dillon 	 *
2764492bffafSMatthew Dillon 	 * WARNING: AMD parts (e.g. 880G chipset, probably others) can
2765492bffafSMatthew Dillon 	 *	    generate PCS on initialization even when device is
2766492bffafSMatthew Dillon 	 *	    already connected up.  It is unclear why this happens.
2767492bffafSMatthew Dillon 	 *	    Depending on the state of the device detect this can
2768492bffafSMatthew Dillon 	 *	    cause us to go into harsh reinit or hot-plug insertion
2769492bffafSMatthew Dillon 	 *	    mode.
2770492bffafSMatthew Dillon 	 *
2771492bffafSMatthew Dillon 	 * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET
2772492bffafSMatthew Dillon 	 *	    continues to flow in from the device), we must clear the
2773492bffafSMatthew Dillon 	 *	    interrupt in all cases and enforce a delay to prevent
2774492bffafSMatthew Dillon 	 *	    a livelock and give the port time to settle down.
2775492bffafSMatthew Dillon 	 *	    Only print something if we aren't in INIT/HARD-RESET.
2776258223a3SMatthew Dillon 	 */
2777258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
27783d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
27793d102df7SMatthew Dillon 			    is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2780492bffafSMatthew Dillon 		/*
2781492bffafSMatthew Dillon 		 * Try to clear the error.  Because of the repetitiveness
2782492bffafSMatthew Dillon 		 * of this interrupt avoid any harsh action if the port is
2783492bffafSMatthew Dillon 		 * already in the init or hard-reset probe state.
2784492bffafSMatthew Dillon 		 */
2785492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2786492bffafSMatthew Dillon 		/* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */
2787492bffafSMatthew Dillon 
2788493d3201SMatthew Dillon 		/*
2789493d3201SMatthew Dillon 		 * Ignore PCS/PRCS errors during probes (but still clear the
2790493d3201SMatthew Dillon 		 * interrupt to avoid a livelock).  The AMD 880/890/SB850
2791493d3201SMatthew Dillon 		 * chipsets do not mask PCS/PRCS internally during reset
2792493d3201SMatthew Dillon 		 * sequences.
2793493d3201SMatthew Dillon 		 */
27945502cf24SMatthew Dillon 		if (ap->ap_flags & AP_F_IN_RESET)
2795493d3201SMatthew Dillon 			goto skip_pcs;
2796493d3201SMatthew Dillon 
2797492bffafSMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT ||
2798492bffafSMatthew Dillon 		    ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) {
2799cec07d75SMatthew Dillon 			is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2800492bffafSMatthew Dillon 			need = NEED_NOTHING;
2801492bffafSMatthew Dillon 			ahci_os_sleep(1000);
2802492bffafSMatthew Dillon 			goto failall;
2803492bffafSMatthew Dillon 		}
2804492bffafSMatthew Dillon 		kprintf("%s: Transient Errors: %b (%d)\n",
2805492bffafSMatthew Dillon 			PORTNAME(ap), is, AHCI_PFMT_IS, ap->ap_probe);
2806492bffafSMatthew Dillon 		is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2807492bffafSMatthew Dillon 		ahci_os_sleep(200);
2808492bffafSMatthew Dillon 
2809492bffafSMatthew Dillon 		/*
2810492bffafSMatthew Dillon 		 * Stop the port and figure out what to do next.
2811492bffafSMatthew Dillon 		 */
281222181ab7SMatthew Dillon 		ahci_port_stop(ap, 0);
2813492bffafSMatthew Dillon 		stopped = 1;
28140be9576aSMatthew Dillon 
2815258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2816258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
2817492bffafSMatthew Dillon 			/*
2818492bffafSMatthew Dillon 			 * Device detect
2819492bffafSMatthew Dillon 			 */
282012feb904SMatthew Dillon 			if (ap->ap_probe == ATA_PROBE_FAILED) {
282122181ab7SMatthew Dillon 				need = NEED_HOTPLUG_INSERT;
282222181ab7SMatthew Dillon 				goto fatal;
2823258223a3SMatthew Dillon 			}
282422181ab7SMatthew Dillon 			need = NEED_RESTART;
2825258223a3SMatthew Dillon 			break;
2826492bffafSMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
2827492bffafSMatthew Dillon 			/*
2828492bffafSMatthew Dillon 			 * Device not communicating.  AMD parts seem to
2829492bffafSMatthew Dillon 			 * like to throw this error on initialization
2830492bffafSMatthew Dillon 			 * for no reason that I can fathom.
2831492bffafSMatthew Dillon 			 */
2832492bffafSMatthew Dillon 			kprintf("%s: Device present but not communicating, "
2833492bffafSMatthew Dillon 				"attempting port restart\n",
2834492bffafSMatthew Dillon 				PORTNAME(ap));
2835492bffafSMatthew Dillon 			need = NEED_REINIT;
2836492bffafSMatthew Dillon 			goto fatal;
2837258223a3SMatthew Dillon 		default:
28380be9576aSMatthew Dillon 			if (ap->ap_probe != ATA_PROBE_FAILED) {
283922181ab7SMatthew Dillon 				need = NEED_HOTPLUG_REMOVE;
284022181ab7SMatthew Dillon 				goto fatal;
2841258223a3SMatthew Dillon 			}
284222181ab7SMatthew Dillon 			need = NEED_RESTART;
2843258223a3SMatthew Dillon 			break;
2844258223a3SMatthew Dillon 		}
2845493d3201SMatthew Dillon skip_pcs:
2846493d3201SMatthew Dillon 		;
2847258223a3SMatthew Dillon 	}
2848258223a3SMatthew Dillon 
284922181ab7SMatthew Dillon 	/*
2850f4553de1SMatthew Dillon 	 * Check for remaining errors - they are fatal. (blockable)
285122181ab7SMatthew Dillon 	 */
2852258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2853258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2854cec07d75SMatthew Dillon 		u_int32_t serr;
2855cec07d75SMatthew Dillon 
2856cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2857cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2858cec07d75SMatthew Dillon 				  AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2859cec07d75SMatthew Dillon 				  AHCI_PREG_IS_UFS));
2860cec07d75SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2861831bc9e3SMatthew Dillon 		kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
28624444122dSMatthew Dillon 			"disabling port.\n",
28634444122dSMatthew Dillon 			PORTNAME(ap),
28644444122dSMatthew Dillon 			is, AHCI_PFMT_IS,
28651980eff3SMatthew Dillon 			serr, AHCI_PFMT_SERR
28664444122dSMatthew Dillon 		);
2867831bc9e3SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2868831bc9e3SMatthew Dillon 			AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2869831bc9e3SMatthew Dillon 		        AHCI_PREG_IS_UFS);
2870492bffafSMatthew Dillon 
2871492bffafSMatthew Dillon 		/*
2872492bffafSMatthew Dillon 		 * Fail all commands but then what?  For now try to
2873492bffafSMatthew Dillon 		 * reinitialize the port.
2874492bffafSMatthew Dillon 		 */
2875492bffafSMatthew Dillon 		need = NEED_REINIT;
2876258223a3SMatthew Dillon 		goto fatal;
2877258223a3SMatthew Dillon 	}
2878258223a3SMatthew Dillon 
287922181ab7SMatthew Dillon 	/*
288022181ab7SMatthew Dillon 	 * Fail all outstanding commands if we know the port won't recover.
28811980eff3SMatthew Dillon 	 *
28821980eff3SMatthew Dillon 	 * We may have a ccb_at if the failed command is known and was
28831980eff3SMatthew Dillon 	 * being sent to a device over a port multiplier (PM).  In this
28841980eff3SMatthew Dillon 	 * case if the port itself has not completely failed we fail just
28851980eff3SMatthew Dillon 	 * the commands related to that target.
288612feb904SMatthew Dillon 	 *
288712feb904SMatthew Dillon 	 * ci_saved contains the mask of active commands as of when the
288812feb904SMatthew Dillon 	 * error occured, prior to any port stops.
288922181ab7SMatthew Dillon 	 */
2890258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
2891258223a3SMatthew Dillon fatal:
2892258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
289312feb904SMatthew Dillon failall:
2894492bffafSMatthew Dillon 		ahci_port_stop(ap, 0);
2895492bffafSMatthew Dillon 		stopped = 1;
2896258223a3SMatthew Dillon 
28971980eff3SMatthew Dillon 		/*
2898492bffafSMatthew Dillon 		 * Error all the active slots not already errored.
28991980eff3SMatthew Dillon 		 */
290012feb904SMatthew Dillon 		ci_masked = ci_saved & *active & ~ap->ap_expired;
2901492bffafSMatthew Dillon 		if (ci_masked) {
2902492bffafSMatthew Dillon 			kprintf("%s: Failing all commands: %08x\n",
2903492bffafSMatthew Dillon 				PORTNAME(ap), ci_masked);
2904492bffafSMatthew Dillon 		}
2905492bffafSMatthew Dillon 
2906258223a3SMatthew Dillon 		while (ci_masked) {
2907258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
2908258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
290912feb904SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_TIMEOUT;
291012feb904SMatthew Dillon 			ap->ap_expired |= 1 << slot;
291112feb904SMatthew Dillon 			ci_saved &= ~(1 << slot);
291212feb904SMatthew Dillon 			ci_masked &= ~(1 << slot);
29131980eff3SMatthew Dillon 		}
2914258223a3SMatthew Dillon 
291512feb904SMatthew Dillon 		/*
291612feb904SMatthew Dillon 		 * Clear bits in ci_saved (cause completions to be run)
291712feb904SMatthew Dillon 		 * for all slots which are not active.
291812feb904SMatthew Dillon 		 */
2919258223a3SMatthew Dillon 		ci_saved &= ~*active;
2920258223a3SMatthew Dillon 
2921258223a3SMatthew Dillon 		/*
2922258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
2923258223a3SMatthew Dillon 		 *
2924258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
2925258223a3SMatthew Dillon 		 * a livelock.
2926258223a3SMatthew Dillon 		 */
2927258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
292822181ab7SMatthew Dillon 			if (need == NEED_RESTART)
292922181ab7SMatthew Dillon 				need = NEED_NOTHING;
2930258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
2931258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2932258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2933258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
2934258223a3SMatthew Dillon 		}
2935258223a3SMatthew Dillon 	}
2936258223a3SMatthew Dillon 
2937258223a3SMatthew Dillon 	/*
2938492bffafSMatthew Dillon 	 * If we are stopped the AHCI chipset is supposed to have cleared
2939492bffafSMatthew Dillon 	 * CI and SACT.  Did it?  If it didn't we try very hard to clear
2940492bffafSMatthew Dillon 	 * the fields otherwise we may end up completing CCBs which are
2941492bffafSMatthew Dillon 	 * actually still active.
2942492bffafSMatthew Dillon 	 *
2943492bffafSMatthew Dillon 	 * IFS errors on (at least) AMD chipsets create this confusion.
2944492bffafSMatthew Dillon 	 */
2945492bffafSMatthew Dillon 	if (stopped) {
2946492bffafSMatthew Dillon 		u_int32_t mask;
2947492bffafSMatthew Dillon 		if ((mask = ahci_pactive(ap)) != 0) {
2948492bffafSMatthew Dillon 			kprintf("%s: chipset failed to clear "
2949492bffafSMatthew Dillon 				"active cmds %08x\n",
2950492bffafSMatthew Dillon 				PORTNAME(ap), mask);
2951492bffafSMatthew Dillon 			ahci_port_start(ap);
2952492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
2953492bffafSMatthew Dillon 			if ((mask = ahci_pactive(ap)) != 0) {
2954492bffafSMatthew Dillon 				kprintf("%s: unable to prod the chip into "
2955492bffafSMatthew Dillon 					"clearing active cmds %08x\n",
2956492bffafSMatthew Dillon 					PORTNAME(ap), mask);
2957492bffafSMatthew Dillon 				/* what do we do now? */
2958492bffafSMatthew Dillon 			}
2959492bffafSMatthew Dillon 		}
2960492bffafSMatthew Dillon 	}
2961492bffafSMatthew Dillon 
2962492bffafSMatthew Dillon 	/*
2963f4553de1SMatthew Dillon 	 * CCB completion (non blocking).
2964f4553de1SMatthew Dillon 	 *
2965258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
2966258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
2967258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
2968cf5f3a81SMatthew Dillon 	 *
2969cf5f3a81SMatthew Dillon 	 * Any active bits not saved are completed within the restrictions
2970cf5f3a81SMatthew Dillon 	 * imposed by the caller.
2971258223a3SMatthew Dillon 	 */
29723209f581SMatthew Dillon 	ci_masked = ~ci_saved & *active;
2973258223a3SMatthew Dillon 	while (ci_masked) {
2974258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
2975258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
2976258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
2977258223a3SMatthew Dillon 
2978258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2979258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2980258223a3SMatthew Dillon 		    " (error)" : "");
2981258223a3SMatthew Dillon 
2982258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
2983258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2984258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2985258223a3SMatthew Dillon 
2986258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
2987258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2988258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2989258223a3SMatthew Dillon 
2990258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
2991258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2992258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
2993258223a3SMatthew Dillon 
2994258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
29951980eff3SMatthew Dillon 		if (active == &ap->ap_active) {
29961980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
29971980eff3SMatthew Dillon 			--ap->ap_active_cnt;
29981980eff3SMatthew Dillon 		}
29994c339a5fSMatthew Dillon 
30004c339a5fSMatthew Dillon 		/*
30014c339a5fSMatthew Dillon 		 * Complete the ccb.  If the ccb was marked expired it
30024c339a5fSMatthew Dillon 		 * was probably already removed from the command processor,
30034c339a5fSMatthew Dillon 		 * so don't take the clear ci_saved bit as meaning the
30044c339a5fSMatthew Dillon 		 * command actually succeeded, it didn't.
30054c339a5fSMatthew Dillon 		 */
30064c339a5fSMatthew Dillon 		if (ap->ap_expired & (1 << ccb->ccb_slot)) {
300776497a9cSMatthew Dillon 			ap->ap_expired &= ~(1 << ccb->ccb_slot);
30084c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_TIMEOUT;
3009258223a3SMatthew Dillon 			ccb->ccb_done(ccb);
30104c339a5fSMatthew Dillon 			ccb->ccb_xa.complete(&ccb->ccb_xa);
30114c339a5fSMatthew Dillon 		} else {
301212feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
30134c339a5fSMatthew Dillon 				ccb->ccb_xa.state = ATA_S_COMPLETE;
301412feb904SMatthew Dillon 				if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
301512feb904SMatthew Dillon 					memcpy(&ccb->ccb_xa.rfis,
301612feb904SMatthew Dillon 					    ap->ap_rfis->rfis,
301712feb904SMatthew Dillon 					    sizeof(struct ata_fis_d2h));
301812feb904SMatthew Dillon 					if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
301912feb904SMatthew Dillon 						ccb->ccb_xa.state = ATA_S_ERROR;
302012feb904SMatthew Dillon 				}
302112feb904SMatthew Dillon 			}
30224c339a5fSMatthew Dillon 			ccb->ccb_done(ccb);
30234c339a5fSMatthew Dillon 		}
3024258223a3SMatthew Dillon 	}
3025258223a3SMatthew Dillon 
3026f4553de1SMatthew Dillon 	/*
3027f4553de1SMatthew Dillon 	 * Cleanup.  Will not be set if non-blocking.
3028f4553de1SMatthew Dillon 	 */
302922181ab7SMatthew Dillon 	switch(need) {
3030f3de36f7SMatthew Dillon 	case NEED_NOTHING:
3031f3de36f7SMatthew Dillon 		/*
3032f3de36f7SMatthew Dillon 		 * If operating normally and not stopped the interrupt was
3033f3de36f7SMatthew Dillon 		 * probably just a normal completion and we may be able to
3034f3de36f7SMatthew Dillon 		 * issue more commands.
3035f3de36f7SMatthew Dillon 		 */
3036f3de36f7SMatthew Dillon 		if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR)
3037f3de36f7SMatthew Dillon 			ahci_issue_pending_commands(ap, NULL);
3038f3de36f7SMatthew Dillon 		break;
303922181ab7SMatthew Dillon 	case NEED_RESTART:
304022181ab7SMatthew Dillon 		/*
304122181ab7SMatthew Dillon 		 * A recoverable error occured and we can restart outstanding
304222181ab7SMatthew Dillon 		 * commands on the port.
304322181ab7SMatthew Dillon 		 */
304412feb904SMatthew Dillon 		ci_saved &= ~ap->ap_expired;
3045258223a3SMatthew Dillon 		if (ci_saved) {
304612feb904SMatthew Dillon 			kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
30474c339a5fSMatthew Dillon 			ahci_issue_saved_commands(ap, ci_saved);
3048258223a3SMatthew Dillon 		}
3049492bffafSMatthew Dillon 
3050492bffafSMatthew Dillon 		/*
3051492bffafSMatthew Dillon 		 * Potentially issue new commands if not in a failed
3052492bffafSMatthew Dillon 		 * state.
3053492bffafSMatthew Dillon 		 */
3054492bffafSMatthew Dillon 		if (ap->ap_state != AP_S_FATAL_ERROR) {
3055492bffafSMatthew Dillon 			ahci_port_start(ap);
3056492bffafSMatthew Dillon 			ahci_issue_pending_commands(ap, NULL);
3057492bffafSMatthew Dillon 		}
3058492bffafSMatthew Dillon 		break;
3059492bffafSMatthew Dillon 	case NEED_REINIT:
3060492bffafSMatthew Dillon 		/*
3061492bffafSMatthew Dillon 		 * Something horrible happened to the port and we
3062492bffafSMatthew Dillon 		 * need to reinitialize it.
3063492bffafSMatthew Dillon 		 */
3064492bffafSMatthew Dillon 		kprintf("%s: REINIT - Attempting to reinitialize the port "
3065492bffafSMatthew Dillon 			"after it had a horrible accident\n",
3066492bffafSMatthew Dillon 			PORTNAME(ap));
3067492bffafSMatthew Dillon 		ap->ap_flags |= AP_F_IN_RESET;
3068492bffafSMatthew Dillon 		ap->ap_flags |= AP_F_HARSH_REINIT;
3069492bffafSMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_INIT;
3070492bffafSMatthew Dillon 		ahci_cam_changed(ap, NULL, -1);
307122181ab7SMatthew Dillon 		break;
307222181ab7SMatthew Dillon 	case NEED_HOTPLUG_INSERT:
307322181ab7SMatthew Dillon 		/*
3074cf5f3a81SMatthew Dillon 		 * A hot-plug insertion event has occured and all
3075cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
30761980eff3SMatthew Dillon 		 *
30771980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
30781980eff3SMatthew Dillon 		 * resetting the port.
307922181ab7SMatthew Dillon 		 */
30801980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
308122181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device inserted\n",
308222181ab7SMatthew Dillon 				PORTNAME(ap));
30833209f581SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_INIT;
30843209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
30851980eff3SMatthew Dillon 		}
308622181ab7SMatthew Dillon 		break;
308722181ab7SMatthew Dillon 	case NEED_HOTPLUG_REMOVE:
3088cf5f3a81SMatthew Dillon 		/*
3089cf5f3a81SMatthew Dillon 		 * A hot-plug removal event has occured and all
3090cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
30911980eff3SMatthew Dillon 		 *
30921980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
30931980eff3SMatthew Dillon 		 * resetting the port.
3094cf5f3a81SMatthew Dillon 		 */
30951980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
309622181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device removed\n",
309722181ab7SMatthew Dillon 				PORTNAME(ap));
3098cf5f3a81SMatthew Dillon 			ahci_port_hardstop(ap);
30993209f581SMatthew Dillon 			/* ap_probe set to failed */
31003209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
31011980eff3SMatthew Dillon 		}
310222181ab7SMatthew Dillon 		break;
310322181ab7SMatthew Dillon 	default:
310422181ab7SMatthew Dillon 		break;
3105258223a3SMatthew Dillon 	}
3106258223a3SMatthew Dillon }
3107258223a3SMatthew Dillon 
3108258223a3SMatthew Dillon struct ahci_ccb *
3109258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
3110258223a3SMatthew Dillon {
3111258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
3112258223a3SMatthew Dillon 
3113258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3114258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
3115258223a3SMatthew Dillon 	if (ccb != NULL) {
3116d16d3400SMatthew Dillon 		KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3117258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
3118258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
3119258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
3120492bffafSMatthew Dillon 		ccb->ccb_xa.flags = 0;
31211980eff3SMatthew Dillon 		ccb->ccb_xa.at = NULL;
3122258223a3SMatthew Dillon 	}
3123258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3124258223a3SMatthew Dillon 
3125258223a3SMatthew Dillon 	return (ccb);
3126258223a3SMatthew Dillon }
3127258223a3SMatthew Dillon 
3128258223a3SMatthew Dillon void
3129258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
3130258223a3SMatthew Dillon {
3131258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
3132258223a3SMatthew Dillon 
3133d16d3400SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state != ATA_S_PUT);
3134d16d3400SMatthew Dillon 	KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3135258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3136dcdc0770SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
3137bb79834dSMatthew Dillon 	++ccb->ccb_xa.serial;
3138258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
3139258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3140258223a3SMatthew Dillon }
3141258223a3SMatthew Dillon 
3142258223a3SMatthew Dillon struct ahci_ccb *
3143258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
3144258223a3SMatthew Dillon {
3145258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
3146258223a3SMatthew Dillon 	u_int32_t sact;
3147b012a2caSMatthew Dillon 	u_int32_t ci;
3148258223a3SMatthew Dillon 
3149258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
3150b012a2caSMatthew Dillon 
3151b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3152258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
3153192ee1d0SMatthew Dillon 		if (sact != 0) {
3154192ee1d0SMatthew Dillon 			kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
3155192ee1d0SMatthew Dillon 				PORTNAME(ap), sact);
3156192ee1d0SMatthew Dillon 		}
3157b012a2caSMatthew Dillon 	}
3158b012a2caSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
3159b012a2caSMatthew Dillon 	if (ci) {
3160b012a2caSMatthew Dillon 		kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
3161b012a2caSMatthew Dillon 			ap->ap_name, ci);
3162b012a2caSMatthew Dillon 	}
3163b012a2caSMatthew Dillon 	KKASSERT(ci == 0);
3164baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
3165baef7501SMatthew Dillon 	ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
3166258223a3SMatthew Dillon 
3167258223a3SMatthew Dillon 	/* Save outstanding command state. */
3168258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
3169258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
3170258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
3171258223a3SMatthew Dillon 
3172258223a3SMatthew Dillon 	/*
3173258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
3174258223a3SMatthew Dillon 	 * run prematurely.
3175258223a3SMatthew Dillon 	 */
3176258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
3177258223a3SMatthew Dillon 
3178258223a3SMatthew Dillon 	/*
3179258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
3180258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
3181258223a3SMatthew Dillon 	 */
31821067474aSMatthew Dillon 	err_ccb = ap->ap_err_ccb;
3183258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
3184258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
3185258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
3186258223a3SMatthew Dillon 
3187258223a3SMatthew Dillon 	return err_ccb;
3188258223a3SMatthew Dillon }
3189258223a3SMatthew Dillon 
3190258223a3SMatthew Dillon void
3191258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
3192258223a3SMatthew Dillon {
3193258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
3194258223a3SMatthew Dillon 	u_int32_t sact;
31955f8c1efdSMatthew Dillon 	u_int32_t ci;
3196258223a3SMatthew Dillon 
3197baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
3198baef7501SMatthew Dillon 
31995f8c1efdSMatthew Dillon 	/*
32005f8c1efdSMatthew Dillon 	 * No commands may be active on the chip
32015f8c1efdSMatthew Dillon 	 */
3202b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3203258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
32045f8c1efdSMatthew Dillon 		if (sact) {
3205ed20d0e3SSascha Wildner 			panic("ahci_port_err_ccb(%d) but SACT %08x != 0",
32065f8c1efdSMatthew Dillon 			      ccb->ccb_slot, sact);
3207258223a3SMatthew Dillon 		}
3208b012a2caSMatthew Dillon 	}
32095f8c1efdSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
32105f8c1efdSMatthew Dillon 	if (ci) {
3211cf5f3a81SMatthew Dillon 		panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
3212cf5f3a81SMatthew Dillon 		      "(act=%08x sact=%08x)\n",
3213cf5f3a81SMatthew Dillon 		      ccb->ccb_slot, ci,
3214cf5f3a81SMatthew Dillon 		      ap->ap_active, ap->ap_sactive);
32155f8c1efdSMatthew Dillon 	}
3216258223a3SMatthew Dillon 
32171067474aSMatthew Dillon 	KKASSERT(ccb == ap->ap_err_ccb);
3218258223a3SMatthew Dillon 
3219258223a3SMatthew Dillon 	/* Restore outstanding command state */
3220258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
3221258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
3222258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
3223258223a3SMatthew Dillon 
3224baef7501SMatthew Dillon 	ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
3225258223a3SMatthew Dillon }
3226258223a3SMatthew Dillon 
32271980eff3SMatthew Dillon /*
32281980eff3SMatthew Dillon  * Read log page to get NCQ error.
32291980eff3SMatthew Dillon  *
32301980eff3SMatthew Dillon  * NOTE: NCQ not currently supported on port multipliers. XXX
32311980eff3SMatthew Dillon  */
3232258223a3SMatthew Dillon int
323312feb904SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int target)
3234258223a3SMatthew Dillon {
323512feb904SMatthew Dillon 	struct ata_log_page_10h	*log;
3236258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
3237e1014452SMatthew Dillon 	struct ahci_ccb		*ccb2;
3238258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
3239258223a3SMatthew Dillon 	struct ata_fis_h2d	*fis;
324012feb904SMatthew Dillon 	int			err_slot;
3241258223a3SMatthew Dillon 
324212feb904SMatthew Dillon 	if (bootverbose) {
324312feb904SMatthew Dillon 		kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
324412feb904SMatthew Dillon 			target);
324512feb904SMatthew Dillon 	}
3246258223a3SMatthew Dillon 
324712feb904SMatthew Dillon 	/*
324812feb904SMatthew Dillon 	 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
324912feb904SMatthew Dillon 	 *
325012feb904SMatthew Dillon 	 * Getting err_ccb clears active/sactive/active_cnt, putting
325112feb904SMatthew Dillon 	 * it back restores the fields.
325212feb904SMatthew Dillon 	 */
3253258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
325412feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
3255258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
3256258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
325712feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
3258b012a2caSMatthew Dillon 	ccb->ccb_xa.at = ap->ap_ata[target];
3259258223a3SMatthew Dillon 
3260258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
326112feb904SMatthew Dillon 	bzero(fis, sizeof(*fis));
3262258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
326312feb904SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD | target;
3264258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
3265258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
3266258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
3267258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
3268258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
3269258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
3270258223a3SMatthew Dillon 	fis->device = 0;
3271258223a3SMatthew Dillon 
327212feb904SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
3273258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
3274258223a3SMatthew Dillon 
3275258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
327612feb904SMatthew Dillon 		err_slot = -1;
3277258223a3SMatthew Dillon 		goto err;
3278258223a3SMatthew Dillon 	}
3279258223a3SMatthew Dillon 
3280258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
328112feb904SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
328212feb904SMatthew Dillon 		err_slot = -1;
3283258223a3SMatthew Dillon 		ahci_unload_prdt(ccb);
328412feb904SMatthew Dillon 		goto err;
328512feb904SMatthew Dillon 	}
328612feb904SMatthew Dillon 	ahci_unload_prdt(ccb);
3287258223a3SMatthew Dillon 
328812feb904SMatthew Dillon 	/*
328912feb904SMatthew Dillon 	 * Success, extract failed register set and tags from the scratch
329012feb904SMatthew Dillon 	 * space.
329112feb904SMatthew Dillon 	 */
3292258223a3SMatthew Dillon 	log = (struct ata_log_page_10h *)ap->ap_err_scratch;
3293258223a3SMatthew Dillon 	if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
3294258223a3SMatthew Dillon 		/* Not queued bit was set - wasn't an NCQ error? */
329512feb904SMatthew Dillon 		kprintf("%s: read NCQ error page, but not an NCQ error?\n",
3296258223a3SMatthew Dillon 			PORTNAME(ap));
329712feb904SMatthew Dillon 		err_slot = -1;
3298258223a3SMatthew Dillon 	} else {
3299258223a3SMatthew Dillon 		/* Copy back the log record as a D2H register FIS. */
330012feb904SMatthew Dillon 		err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
3301258223a3SMatthew Dillon 
3302e1014452SMatthew Dillon 		ccb2 = &ap->ap_ccbs[err_slot];
3303e1014452SMatthew Dillon 		if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
330412feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d\n",
3305e1014452SMatthew Dillon 				ATANAME(ap, ccb2->ccb_xa.at),
330612feb904SMatthew Dillon 				err_slot);
3307e1014452SMatthew Dillon 			memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
3308258223a3SMatthew Dillon 				sizeof(struct ata_fis_d2h));
3309e1014452SMatthew Dillon 			ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3310e1014452SMatthew Dillon 			ccb2->ccb_xa.rfis.flags = 0;
331112feb904SMatthew Dillon 		} else {
331212feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d, "
331312feb904SMatthew Dillon 				"slot does not match any cmds\n",
3314e1014452SMatthew Dillon 				ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
331512feb904SMatthew Dillon 				err_slot);
331612feb904SMatthew Dillon 			err_slot = -1;
3317258223a3SMatthew Dillon 		}
3318258223a3SMatthew Dillon 	}
331912feb904SMatthew Dillon err:
332012feb904SMatthew Dillon 	ahci_put_err_ccb(ccb);
332112feb904SMatthew Dillon 	kprintf("%s: DONE log page target %d err_slot=%d\n",
332212feb904SMatthew Dillon 		PORTNAME(ap), target, err_slot);
332312feb904SMatthew Dillon 	return (err_slot);
3324258223a3SMatthew Dillon }
3325258223a3SMatthew Dillon 
3326258223a3SMatthew Dillon /*
3327258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
3328258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
3329258223a3SMatthew Dillon  * single physical base address.
3330258223a3SMatthew Dillon  */
3331258223a3SMatthew Dillon struct ahci_dmamem *
3332258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3333258223a3SMatthew Dillon {
3334258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
3335258223a3SMatthew Dillon 	int	error;
3336258223a3SMatthew Dillon 
3337258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3338258223a3SMatthew Dillon 
3339258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3340258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
3341258223a3SMatthew Dillon 	if (error == 0) {
3342258223a3SMatthew Dillon 		adm->adm_tag = tag;
3343258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
3344258223a3SMatthew Dillon 					adm->adm_kva,
3345258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
3346258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
3347258223a3SMatthew Dillon 					0);
3348258223a3SMatthew Dillon 	}
3349258223a3SMatthew Dillon 	if (error) {
3350258223a3SMatthew Dillon 		if (adm->adm_map) {
3351258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
3352258223a3SMatthew Dillon 			adm->adm_map = NULL;
3353258223a3SMatthew Dillon 			adm->adm_tag = NULL;
3354258223a3SMatthew Dillon 			adm->adm_kva = NULL;
3355258223a3SMatthew Dillon 		}
3356258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
3357258223a3SMatthew Dillon 		adm = NULL;
3358258223a3SMatthew Dillon 	}
3359258223a3SMatthew Dillon 	return (adm);
3360258223a3SMatthew Dillon }
3361258223a3SMatthew Dillon 
3362258223a3SMatthew Dillon static
3363258223a3SMatthew Dillon void
3364258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3365258223a3SMatthew Dillon {
3366258223a3SMatthew Dillon 	KKASSERT(error == 0);
3367258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
3368258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
3369258223a3SMatthew Dillon }
3370258223a3SMatthew Dillon 
3371258223a3SMatthew Dillon 
3372258223a3SMatthew Dillon void
3373258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3374258223a3SMatthew Dillon {
3375258223a3SMatthew Dillon 	if (adm->adm_map) {
3376258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3377258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3378258223a3SMatthew Dillon 		adm->adm_map = NULL;
3379258223a3SMatthew Dillon 		adm->adm_tag = NULL;
3380258223a3SMatthew Dillon 		adm->adm_kva = NULL;
3381258223a3SMatthew Dillon 	}
3382258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
3383258223a3SMatthew Dillon }
3384258223a3SMatthew Dillon 
3385258223a3SMatthew Dillon u_int32_t
3386258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
3387258223a3SMatthew Dillon {
3388258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3389258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
3390258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3391258223a3SMatthew Dillon }
3392258223a3SMatthew Dillon 
3393258223a3SMatthew Dillon void
3394258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3395258223a3SMatthew Dillon {
3396258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3397258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3398258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
3399258223a3SMatthew Dillon }
3400258223a3SMatthew Dillon 
3401258223a3SMatthew Dillon u_int32_t
3402258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
3403258223a3SMatthew Dillon {
3404258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3405258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
3406258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3407258223a3SMatthew Dillon }
3408258223a3SMatthew Dillon 
3409258223a3SMatthew Dillon void
3410258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3411258223a3SMatthew Dillon {
3412258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3413258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3414258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
3415258223a3SMatthew Dillon }
3416258223a3SMatthew Dillon 
3417831bc9e3SMatthew Dillon /*
3418831bc9e3SMatthew Dillon  * Wait up to (timeout) milliseconds for the masked port register to
3419831bc9e3SMatthew Dillon  * match the target.
3420831bc9e3SMatthew Dillon  *
3421831bc9e3SMatthew Dillon  * Timeout is in milliseconds.
3422831bc9e3SMatthew Dillon  */
3423258223a3SMatthew Dillon int
3424cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
3425cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
3426258223a3SMatthew Dillon {
3427831bc9e3SMatthew Dillon 	int	t;
3428258223a3SMatthew Dillon 
3429831bc9e3SMatthew Dillon 	/*
3430831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
3431831bc9e3SMatthew Dillon 	 */
3432831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
3433258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
3434258223a3SMatthew Dillon 			return (0);
3435831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
3436258223a3SMatthew Dillon 	}
3437258223a3SMatthew Dillon 
3438831bc9e3SMatthew Dillon 	do {
3439831bc9e3SMatthew Dillon 		timeout -= ahci_os_softsleep();
3440831bc9e3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
3441831bc9e3SMatthew Dillon 			return (0);
3442831bc9e3SMatthew Dillon 	} while (timeout > 0);
3443831bc9e3SMatthew Dillon 	return (1);
3444831bc9e3SMatthew Dillon }
3445831bc9e3SMatthew Dillon 
3446831bc9e3SMatthew Dillon int
3447831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3448831bc9e3SMatthew Dillon 	     u_int32_t target)
3449831bc9e3SMatthew Dillon {
3450831bc9e3SMatthew Dillon 	int	t;
3451831bc9e3SMatthew Dillon 
3452831bc9e3SMatthew Dillon 	/*
3453831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
3454831bc9e3SMatthew Dillon 	 */
3455831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
3456831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
3457831bc9e3SMatthew Dillon 			return (0);
3458831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
3459831bc9e3SMatthew Dillon 	}
3460831bc9e3SMatthew Dillon 
3461831bc9e3SMatthew Dillon 	/*
3462831bc9e3SMatthew Dillon 	 * And one millisecond the slow way
3463831bc9e3SMatthew Dillon 	 */
3464831bc9e3SMatthew Dillon 	t = 1000;
3465831bc9e3SMatthew Dillon 	do {
3466831bc9e3SMatthew Dillon 		t -= ahci_os_softsleep();
3467831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
3468831bc9e3SMatthew Dillon 			return (0);
3469831bc9e3SMatthew Dillon 	} while (t > 0);
3470831bc9e3SMatthew Dillon 
3471258223a3SMatthew Dillon 	return (1);
3472258223a3SMatthew Dillon }
3473258223a3SMatthew Dillon 
3474831bc9e3SMatthew Dillon 
34751980eff3SMatthew Dillon /*
34761980eff3SMatthew Dillon  * Acquire an ata transfer.
34771980eff3SMatthew Dillon  *
34781980eff3SMatthew Dillon  * Pass a NULL at for direct-attached transfers, and a non-NULL at for
34791980eff3SMatthew Dillon  * targets that go through the port multiplier.
34801980eff3SMatthew Dillon  */
3481258223a3SMatthew Dillon struct ata_xfer *
34821980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3483258223a3SMatthew Dillon {
3484258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
3485258223a3SMatthew Dillon 
3486258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
3487258223a3SMatthew Dillon 	if (ccb == NULL) {
3488258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3489258223a3SMatthew Dillon 		    PORTNAME(ap));
3490258223a3SMatthew Dillon 		return (NULL);
3491258223a3SMatthew Dillon 	}
3492258223a3SMatthew Dillon 
3493258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3494258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
3495258223a3SMatthew Dillon 
34962cc2e845SMatthew Dillon 	bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
34971980eff3SMatthew Dillon 	ccb->ccb_xa.at = at;
3498258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3499258223a3SMatthew Dillon 
3500258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
3501258223a3SMatthew Dillon }
3502258223a3SMatthew Dillon 
3503258223a3SMatthew Dillon void
3504258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
3505258223a3SMatthew Dillon {
3506258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3507258223a3SMatthew Dillon 
3508258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3509258223a3SMatthew Dillon 
3510258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
3511258223a3SMatthew Dillon }
3512258223a3SMatthew Dillon 
3513258223a3SMatthew Dillon int
3514258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
3515258223a3SMatthew Dillon {
3516258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3517258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
3518258223a3SMatthew Dillon 
3519258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
3520258223a3SMatthew Dillon 
3521258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3522258223a3SMatthew Dillon 		goto failcmd;
3523258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
3524258223a3SMatthew Dillon 
3525258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
3526258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
35271980eff3SMatthew Dillon 	if (ccb->ccb_xa.at) {
35281980eff3SMatthew Dillon 		cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
35291980eff3SMatthew Dillon 					   AHCI_CMD_LIST_FLAG_PMP_SHIFT);
35301980eff3SMatthew Dillon 	}
3531258223a3SMatthew Dillon 
3532258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
3533258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3534258223a3SMatthew Dillon 
3535258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
3536258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3537258223a3SMatthew Dillon 
3538258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
3539258223a3SMatthew Dillon 		goto failcmd;
3540258223a3SMatthew Dillon 
3541258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
3542258223a3SMatthew Dillon 
3543831bc9e3SMatthew Dillon 	if (xa->flags & ATA_F_POLL)
3544831bc9e3SMatthew Dillon 		return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3545258223a3SMatthew Dillon 
3546258223a3SMatthew Dillon 	crit_enter();
3547f4553de1SMatthew Dillon 	KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
35483209f581SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_DESIRED;
3549258223a3SMatthew Dillon 	ahci_start(ccb);
3550258223a3SMatthew Dillon 	crit_exit();
3551831bc9e3SMatthew Dillon 	return (xa->state);
3552258223a3SMatthew Dillon 
3553258223a3SMatthew Dillon failcmd:
3554258223a3SMatthew Dillon 	crit_enter();
3555258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
3556258223a3SMatthew Dillon 	xa->complete(xa);
3557258223a3SMatthew Dillon 	crit_exit();
3558831bc9e3SMatthew Dillon 	return (ATA_S_ERROR);
3559258223a3SMatthew Dillon }
3560258223a3SMatthew Dillon 
3561258223a3SMatthew Dillon void
3562258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
3563258223a3SMatthew Dillon {
3564258223a3SMatthew Dillon 	struct ata_xfer	*xa = &ccb->ccb_xa;
3565bb79834dSMatthew Dillon 	int serial;
3566258223a3SMatthew Dillon 
3567831bc9e3SMatthew Dillon 	/*
3568bb79834dSMatthew Dillon 	 * NOTE: Callout does not lock port and may race us modifying
3569831bc9e3SMatthew Dillon 	 *	 the flags, so make sure its stopped.
3570bb79834dSMatthew Dillon 	 *
3571bb79834dSMatthew Dillon 	 *	 A callout race can clean up the ccb.  A change in the
3572bb79834dSMatthew Dillon 	 *	 serial number should catch this condition.
3573831bc9e3SMatthew Dillon 	 */
3574258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3575bb79834dSMatthew Dillon 		serial = ccb->ccb_xa.serial;
357646528d33SMatthew Dillon 		callout_stop_sync(&ccb->ccb_timeout);
3577bb79834dSMatthew Dillon 		if (serial != ccb->ccb_xa.serial) {
3578bb79834dSMatthew Dillon 			kprintf("%s: Warning: timeout race ccb %p\n",
3579bb79834dSMatthew Dillon 				PORTNAME(ccb->ccb_port), ccb);
3580bb79834dSMatthew Dillon 			return;
3581bb79834dSMatthew Dillon 		}
35828dc94ed9SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3583258223a3SMatthew Dillon 	}
3584f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
358546528d33SMatthew Dillon 	ccb->ccb_port->ap_expired &= ~(1 << ccb->ccb_slot);
3586258223a3SMatthew Dillon 
358746528d33SMatthew Dillon 	KKASSERT(xa->state != ATA_S_ONCHIP && xa->state != ATA_S_PUT);
3588258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
3589258223a3SMatthew Dillon 
3590258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
3591258223a3SMatthew Dillon 		xa->complete(xa);
3592258223a3SMatthew Dillon }
3593258223a3SMatthew Dillon 
3594f4553de1SMatthew Dillon /*
3595f4553de1SMatthew Dillon  * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3596f4553de1SMatthew Dillon  * while the callout is runing.
3597f4553de1SMatthew Dillon  *
3598f4553de1SMatthew Dillon  * We can't safely get the port lock here or delay, we could block
3599f4553de1SMatthew Dillon  * the callout thread.
3600f4553de1SMatthew Dillon  */
3601258223a3SMatthew Dillon static void
3602258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
3603258223a3SMatthew Dillon {
3604258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
3605258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
3606258223a3SMatthew Dillon 
360746528d33SMatthew Dillon 	KKASSERT(ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING);
3608f4553de1SMatthew Dillon 	ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3609f4553de1SMatthew Dillon 	ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3610f4553de1SMatthew Dillon 	ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3611258223a3SMatthew Dillon }
3612258223a3SMatthew Dillon 
36134c339a5fSMatthew Dillon /*
36144c339a5fSMatthew Dillon  * Timeout code, typically called when the port command processor is running.
36154c339a5fSMatthew Dillon  *
36164c339a5fSMatthew Dillon  * We have to be very very careful here.  We cannot stop the port unless
36174c339a5fSMatthew Dillon  * CR is already clear or the only active commands remaining are timed-out
36184c339a5fSMatthew Dillon  * ones.  Otherwise stopping the port will race the command processor and
36194c339a5fSMatthew Dillon  * we can lose events.  While we can theoretically just restart everything
36204c339a5fSMatthew Dillon  * that could result in a double-issue which will not work for ATAPI commands.
36214c339a5fSMatthew Dillon  */
36221980eff3SMatthew Dillon void
3623831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3624258223a3SMatthew Dillon {
3625258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
3626258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
36274c339a5fSMatthew Dillon 	struct ata_port		*at;
3628492bffafSMatthew Dillon 	u_int32_t		ci_saved;
3629492bffafSMatthew Dillon 	u_int32_t		mask;
36304c339a5fSMatthew Dillon 	int			slot;
3631258223a3SMatthew Dillon 
36324c339a5fSMatthew Dillon 	at = ccb->ccb_xa.at;
36334c339a5fSMatthew Dillon 
36344c339a5fSMatthew Dillon 	kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
36353d102df7SMatthew Dillon 		"\tglb-status 0x%08x\n"
36364c339a5fSMatthew Dillon 		"\tcmd-reg 0x%b\n"
36373d102df7SMatthew Dillon 		"\tport_status 0x%b\n"
36384c339a5fSMatthew Dillon 		"\tsactive=%08x active=%08x expired=%08x\n"
363908fb24a7SMatthew Dillon 		"\t   sact=%08x     ci=%08x\n"
364008fb24a7SMatthew Dillon 		"\t    STS=%b\n",
36414c339a5fSMatthew Dillon 		ATANAME(ap, at),
36424c339a5fSMatthew Dillon 		ccb->ccb_xa.state, ccb->ccb_slot,
36433d102df7SMatthew Dillon 		ahci_read(ap->ap_sc, AHCI_REG_IS),
3644258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
36453d102df7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_IS), AHCI_PFMT_IS,
36464c339a5fSMatthew Dillon 		ap->ap_sactive, ap->ap_active, ap->ap_expired,
3647258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
364808fb24a7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI),
364908fb24a7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
365008fb24a7SMatthew Dillon 	);
365108fb24a7SMatthew Dillon 
3652258223a3SMatthew Dillon 
36539e145b23SMatthew Dillon 	/*
36549e145b23SMatthew Dillon 	 * NOTE: Timeout will not be running if the command was polled.
36553209f581SMatthew Dillon 	 *	 If we got here at least one of these flags should be set.
36569e145b23SMatthew Dillon 	 */
36573209f581SMatthew Dillon 	KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
36583209f581SMatthew Dillon 			      ATA_F_TIMEOUT_RUNNING));
3659f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3660258223a3SMatthew Dillon 
3661258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
3662258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
36634c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
36644c339a5fSMatthew Dillon 		ccb->ccb_done(ccb);
36654c339a5fSMatthew Dillon 		xa->complete(xa);
36664c339a5fSMatthew Dillon 		ahci_issue_pending_commands(ap, NULL);
36674c339a5fSMatthew Dillon 		return;
36684c339a5fSMatthew Dillon 	}
36694c339a5fSMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
36704c339a5fSMatthew Dillon 		kprintf("%s: Unexpected state during timeout: %d\n",
36714c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_xa.state);
36724c339a5fSMatthew Dillon 		return;
36734c339a5fSMatthew Dillon 	}
36744c339a5fSMatthew Dillon 
36754c339a5fSMatthew Dillon 	/*
36764c339a5fSMatthew Dillon 	 * Ok, we can only get this command off the chip if CR is inactive
36774c339a5fSMatthew Dillon 	 * or if the only commands running on the chip are all expired.
36784c339a5fSMatthew Dillon 	 * Otherwise we have to wait until the port is in a safe state.
36794c339a5fSMatthew Dillon 	 *
36804c339a5fSMatthew Dillon 	 * Do not set state here, it will cause polls to return when the
36814c339a5fSMatthew Dillon 	 * ccb is not yet off the chip.
36824c339a5fSMatthew Dillon 	 */
36834c339a5fSMatthew Dillon 	ap->ap_expired |= 1 << ccb->ccb_slot;
36844c339a5fSMatthew Dillon 
36854c339a5fSMatthew Dillon 	if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
36864c339a5fSMatthew Dillon 	    (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
36874c339a5fSMatthew Dillon 		/*
36884c339a5fSMatthew Dillon 		 * If using FBSS or NCQ we can't safely stop the port
36894c339a5fSMatthew Dillon 		 * right now.
36904c339a5fSMatthew Dillon 		 */
36914c339a5fSMatthew Dillon 		kprintf("%s: Deferred timeout until its safe, slot %d\n",
36924c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_slot);
36934c339a5fSMatthew Dillon 		return;
36944c339a5fSMatthew Dillon 	}
36954c339a5fSMatthew Dillon 
36964c339a5fSMatthew Dillon 	/*
36974c339a5fSMatthew Dillon 	 * We can safely stop the port and process all expired ccb's,
36984c339a5fSMatthew Dillon 	 * which will include our current ccb.
36994c339a5fSMatthew Dillon 	 */
37004c339a5fSMatthew Dillon 	ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
37014c339a5fSMatthew Dillon 				      ahci_pread(ap, AHCI_PREG_CI);
37024c339a5fSMatthew Dillon 	ahci_port_stop(ap, 0);
37034c339a5fSMatthew Dillon 
37044c339a5fSMatthew Dillon 	while (ap->ap_expired) {
37054c339a5fSMatthew Dillon 		slot = ffs(ap->ap_expired) - 1;
37064c339a5fSMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
37074c339a5fSMatthew Dillon 		ci_saved &= ~(1 << slot);
37084c339a5fSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
37094c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
37104c339a5fSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
37114c339a5fSMatthew Dillon 			KKASSERT(ap->ap_sactive & (1 << slot));
37124c339a5fSMatthew Dillon 			ap->ap_sactive &= ~(1 << slot);
37134c339a5fSMatthew Dillon 		} else {
37144c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active & (1 << slot));
37154c339a5fSMatthew Dillon 			ap->ap_active &= ~(1 << slot);
37161980eff3SMatthew Dillon 			--ap->ap_active_cnt;
37171980eff3SMatthew Dillon 		}
3718258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
37194c339a5fSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
3720258223a3SMatthew Dillon 	}
37214c339a5fSMatthew Dillon 	/* ccb invalid now */
3722258223a3SMatthew Dillon 
37234c339a5fSMatthew Dillon 	/*
37244c339a5fSMatthew Dillon 	 * We can safely CLO the port to clear any BSY/DRQ, a case which
37254c339a5fSMatthew Dillon 	 * can occur with port multipliers.  This will unbrick the port
37264c339a5fSMatthew Dillon 	 * and allow commands to other targets behind the PM continue.
37274c339a5fSMatthew Dillon 	 * (FBSS).
37284c339a5fSMatthew Dillon 	 *
37294c339a5fSMatthew Dillon 	 * Finally, once the port has been restarted we can issue any
37304c339a5fSMatthew Dillon 	 * previously saved pending commands, and run the port interrupt
37314c339a5fSMatthew Dillon 	 * code to handle any completions which may have occured when
37324c339a5fSMatthew Dillon 	 * we saved CI.
37334c339a5fSMatthew Dillon 	 */
37344c339a5fSMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
37354c339a5fSMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
37364c339a5fSMatthew Dillon 		kprintf("%s: Warning, issuing CLO after timeout\n",
37374c339a5fSMatthew Dillon 			ATANAME(ap, at));
3738131be210SMatthew Dillon 		ahci_port_clo(ap);
37394c339a5fSMatthew Dillon 	}
3740131be210SMatthew Dillon 	ahci_port_start(ap);
3741492bffafSMatthew Dillon 
3742492bffafSMatthew Dillon 	/*
3743492bffafSMatthew Dillon 	 * We absolutely must make sure the chipset cleared activity on
3744492bffafSMatthew Dillon 	 * all slots.  This sometimes might not happen due to races with
3745492bffafSMatthew Dillon 	 * a chipset interrupt which stops the port before we can manage
3746492bffafSMatthew Dillon 	 * to.  For some reason some chipsets don't clear the active
3747492bffafSMatthew Dillon 	 * commands when we turn off CMD_ST after the chip has stopped
3748492bffafSMatthew Dillon 	 * operations itself.
3749492bffafSMatthew Dillon 	 */
3750492bffafSMatthew Dillon 	if (ahci_pactive(ap) != 0) {
3751492bffafSMatthew Dillon 		ahci_port_stop(ap, 0);
3752492bffafSMatthew Dillon 		ahci_port_start(ap);
3753492bffafSMatthew Dillon 		if ((mask = ahci_pactive(ap)) != 0) {
3754492bffafSMatthew Dillon 			kprintf("%s: quick-timeout: chipset failed "
3755492bffafSMatthew Dillon 				"to clear active cmds %08x\n",
3756492bffafSMatthew Dillon 				PORTNAME(ap), mask);
3757492bffafSMatthew Dillon 		}
3758492bffafSMatthew Dillon 	}
37594c339a5fSMatthew Dillon 	ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
37604c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
37614c339a5fSMatthew Dillon 	ahci_port_intr(ap, 0);
37624c339a5fSMatthew Dillon }
37634c339a5fSMatthew Dillon 
3764cf5f3a81SMatthew Dillon /*
37654c339a5fSMatthew Dillon  * Issue a previously saved set of commands
3766cf5f3a81SMatthew Dillon  */
37674c339a5fSMatthew Dillon void
37684c339a5fSMatthew Dillon ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
37694c339a5fSMatthew Dillon {
37704c339a5fSMatthew Dillon 	if (ci_saved) {
37714c339a5fSMatthew Dillon 		KKASSERT(!((ap->ap_active & ci_saved) &&
37724c339a5fSMatthew Dillon 			   (ap->ap_sactive & ci_saved)));
37734c339a5fSMatthew Dillon 		KKASSERT((ci_saved & ap->ap_expired) == 0);
37744c339a5fSMatthew Dillon 		if (ap->ap_sactive & ci_saved)
37754c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
37764c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3777131be210SMatthew Dillon 	}
3778258223a3SMatthew Dillon }
3779258223a3SMatthew Dillon 
3780831bc9e3SMatthew Dillon /*
3781831bc9e3SMatthew Dillon  * Used by the softreset, pmprobe, and read_ncq_error only, in very
3782831bc9e3SMatthew Dillon  * specialized, controlled circumstances.
3783831bc9e3SMatthew Dillon  *
3784831bc9e3SMatthew Dillon  * Only one command may be pending.
3785831bc9e3SMatthew Dillon  */
3786831bc9e3SMatthew Dillon void
3787831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb)
3788831bc9e3SMatthew Dillon {
3789831bc9e3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
3790492bffafSMatthew Dillon 	u_int32_t mask;
3791831bc9e3SMatthew Dillon 
3792831bc9e3SMatthew Dillon 	switch (ccb->ccb_xa.state) {
3793831bc9e3SMatthew Dillon 	case ATA_S_PENDING:
3794831bc9e3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3795831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3796831bc9e3SMatthew Dillon 		break;
3797831bc9e3SMatthew Dillon 	case ATA_S_ONCHIP:
3798492bffafSMatthew Dillon 		/*
3799492bffafSMatthew Dillon 		 * We have to clear the command on-chip.
3800492bffafSMatthew Dillon 		 */
3801831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3802831bc9e3SMatthew Dillon 			 ap->ap_sactive == 0);
3803831bc9e3SMatthew Dillon 		ahci_port_stop(ap, 0);
3804831bc9e3SMatthew Dillon 		ahci_port_start(ap);
3805492bffafSMatthew Dillon 		if (ahci_pactive(ap) != 0) {
3806492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
3807492bffafSMatthew Dillon 			ahci_port_start(ap);
3808492bffafSMatthew Dillon 			if ((mask = ahci_pactive(ap)) != 0) {
3809492bffafSMatthew Dillon 				kprintf("%s: quick-timeout: chipset failed "
3810492bffafSMatthew Dillon 					"to clear active cmds %08x\n",
3811492bffafSMatthew Dillon 					PORTNAME(ap), mask);
3812492bffafSMatthew Dillon 			}
3813492bffafSMatthew Dillon 		}
3814831bc9e3SMatthew Dillon 
3815831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3816831bc9e3SMatthew Dillon 		ap->ap_active &= ~(1 << ccb->ccb_slot);
3817831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt > 0);
3818831bc9e3SMatthew Dillon 		--ap->ap_active_cnt;
3819831bc9e3SMatthew Dillon 		break;
3820831bc9e3SMatthew Dillon 	default:
3821831bc9e3SMatthew Dillon 		panic("%s: ahci_quick_timeout: ccb in bad state %d",
3822831bc9e3SMatthew Dillon 		      ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3823831bc9e3SMatthew Dillon 	}
3824831bc9e3SMatthew Dillon }
3825831bc9e3SMatthew Dillon 
382612feb904SMatthew Dillon static void
382712feb904SMatthew Dillon ahci_dummy_done(struct ata_xfer *xa)
382812feb904SMatthew Dillon {
382912feb904SMatthew Dillon }
383012feb904SMatthew Dillon 
383112feb904SMatthew Dillon static void
3832258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
3833258223a3SMatthew Dillon {
3834258223a3SMatthew Dillon }
3835795adb22SMatthew Dillon 
3836795adb22SMatthew Dillon int
3837492bffafSMatthew Dillon ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
3838492bffafSMatthew Dillon 		 int feature, int enable)
3839795adb22SMatthew Dillon {
3840795adb22SMatthew Dillon 	struct ata_port *at;
3841795adb22SMatthew Dillon 	struct ata_xfer *xa;
3842795adb22SMatthew Dillon 	int error;
3843795adb22SMatthew Dillon 
3844795adb22SMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
3845795adb22SMatthew Dillon 
3846795adb22SMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
3847795adb22SMatthew Dillon 
3848795adb22SMatthew Dillon 	xa->fis->type = ATA_FIS_TYPE_H2D;
3849795adb22SMatthew Dillon 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
3850795adb22SMatthew Dillon 	xa->fis->command = ATA_C_SET_FEATURES;
3851750495d0SImre Vadász 	xa->fis->features = enable ? ATA_SF_SATAFT_ENA : ATA_SF_SATAFT_DIS;
3852795adb22SMatthew Dillon 	xa->fis->sector_count = feature;
3853795adb22SMatthew Dillon 	xa->fis->control = ATA_FIS_CONTROL_4BIT;
3854795adb22SMatthew Dillon 
3855795adb22SMatthew Dillon 	xa->complete = ahci_dummy_done;
3856795adb22SMatthew Dillon 	xa->datalen = 0;
3857795adb22SMatthew Dillon 	xa->flags = ATA_F_POLL;
3858795adb22SMatthew Dillon 	xa->timeout = 1000;
3859795adb22SMatthew Dillon 
3860795adb22SMatthew Dillon 	if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
3861795adb22SMatthew Dillon 		error = 0;
3862795adb22SMatthew Dillon 	else
3863795adb22SMatthew Dillon 		error = EIO;
3864795adb22SMatthew Dillon 	ahci_ata_put_xfer(xa);
3865795adb22SMatthew Dillon 	return(error);
3866795adb22SMatthew Dillon }
3867