xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision 2fa886e990f6d3537dfd862d967b5768a71834e7)
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
ahci_init(struct ahci_softc * sc)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 
98c3783d8fSzrj 	DPRINTF(AHCI_D_VERBOSE, " GHC 0x%pb%i",
99c3783d8fSzrj 		AHCI_FMT_GHC, ahci_read(sc, AHCI_REG_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);
14831075e01SMatthew Dillon 	ahci_os_sleep(250);
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);
15131075e01SMatthew Dillon 	ahci_os_sleep(250);
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);
16131075e01SMatthew Dillon 		ahci_os_sleep(250);
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 	 */
17031075e01SMatthew Dillon 	ahci_os_sleep(10);
171b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
17231075e01SMatthew Dillon 	ahci_os_sleep(10);
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);
2229abd2bb8SImre Vadász 		ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable);
223831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
224831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
22512feb904SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
226831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
22712feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, -1);
22812feb904SMatthew Dillon 		sc->sc_portmask |= (1 << i);
22912feb904SMatthew Dillon 		pleft &= ~(1 << i);
230831bc9e3SMatthew Dillon 	}
23112feb904SMatthew Dillon 	sc->sc_numports = i;
232831bc9e3SMatthew Dillon 	kfree(ap, M_DEVBUF);
233831bc9e3SMatthew Dillon 
234258223a3SMatthew Dillon 	return (0);
235258223a3SMatthew Dillon }
236258223a3SMatthew Dillon 
237fd8bd957SMatthew Dillon /*
238fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
239fd8bd957SMatthew Dillon  */
240258223a3SMatthew Dillon int
ahci_port_alloc(struct ahci_softc * sc,u_int port)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 
318258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
319258223a3SMatthew Dillon 		    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
320258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
321258223a3SMatthew Dillon 		int r;
322258223a3SMatthew Dillon 
323258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
324258223a3SMatthew Dillon 		if (r) {
325258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
326258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
327258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
328258223a3SMatthew Dillon 			rc = ENXIO;
329258223a3SMatthew Dillon 			goto freeport;
330258223a3SMatthew Dillon 		}
331258223a3SMatthew Dillon 
332258223a3SMatthew Dillon 		/* Write DET to zero */
3339abd2bb8SImre Vadász 		ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable);
334258223a3SMatthew Dillon 	}
335258223a3SMatthew Dillon 
336258223a3SMatthew Dillon 	/* Allocate RFIS */
337258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
338258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
339cf5f3a81SMatthew Dillon 		kprintf("%s: NORFIS\n", PORTNAME(ap));
340258223a3SMatthew Dillon 		goto nomem;
341258223a3SMatthew Dillon 	}
342258223a3SMatthew Dillon 
343258223a3SMatthew Dillon 	/* Setup RFIS base address */
344258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
3450e589b85SMatthew Dillon 	bzero(ap->ap_rfis, sc->sc_rfis_size);
3460e589b85SMatthew Dillon 
347258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
348258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
3490e589b85SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
350258223a3SMatthew Dillon 
351831bc9e3SMatthew Dillon 	/* Clear SERR before starting FIS reception or ST or anything */
352831bc9e3SMatthew Dillon 	ahci_flush_tfd(ap);
353831bc9e3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
354831bc9e3SMatthew Dillon 
355eb9f4c83SMatthew Dillon 	/*
35646d04d11SMatthew Dillon 	 * Power up any device sitting on the port.
35746d04d11SMatthew Dillon 	 *
35846d04d11SMatthew Dillon 	 * Don't turn on FIS reception here, it will be handled in the first
35946d04d11SMatthew Dillon 	 * ahci_port_start().
36046d04d11SMatthew Dillon 	 *
36146d04d11SMatthew Dillon 	 * Don't make the ICC ACTIVE here, it will be handled in port_init.
362eb9f4c83SMatthew Dillon 	 */
363258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
3641980eff3SMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
365eb9f4c83SMatthew Dillon 	cmd |= AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
36646d04d11SMatthew Dillon #if 0
36746d04d11SMatthew Dillon 	/* this will be done in ahci_pm_port_probe() */
36846d04d11SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM)
36946d04d11SMatthew Dillon 		cmd |= AHCI_PREG_CMD_PMA;
37046d04d11SMatthew Dillon #endif
37146d04d11SMatthew Dillon 
372eb9f4c83SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
373258223a3SMatthew Dillon 
374258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
375258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
376258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
377258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
378258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
379258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
380258223a3SMatthew Dillon 			      port);
381258223a3SMatthew Dillon 		goto freeport;
382258223a3SMatthew Dillon 	}
383258223a3SMatthew Dillon 
384258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
385258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
386258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
387258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
388258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
389258223a3SMatthew Dillon nomem:
390258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
391258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
392258223a3SMatthew Dillon 			      port);
393258223a3SMatthew Dillon 		goto freeport;
394258223a3SMatthew Dillon 	}
395258223a3SMatthew Dillon 
396258223a3SMatthew Dillon 	/* Setup command list base address */
397258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
398258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
3990e589b85SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
400258223a3SMatthew Dillon 
401258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
402258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
403258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
4040e589b85SMatthew Dillon 	bzero(hdr, sc->sc_cmdlist_size);
4050e589b85SMatthew Dillon 
406258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
407258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
408258223a3SMatthew Dillon 
409258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
410258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
411258223a3SMatthew Dillon 		if (error) {
412258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
413258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
414258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
415258223a3SMatthew Dillon 			goto freeport;
416258223a3SMatthew Dillon 		}
417258223a3SMatthew Dillon 
418bf0ecf68SMatthew Dillon 		callout_init_mp(&ccb->ccb_timeout);
419258223a3SMatthew Dillon 		ccb->ccb_slot = i;
420258223a3SMatthew Dillon 		ccb->ccb_port = ap;
421258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
422258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
423258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
424258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
425258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
426258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
427258223a3SMatthew Dillon 
428258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
429258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
430258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
431258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
432258223a3SMatthew Dillon 
433258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
4341067474aSMatthew Dillon 
4351067474aSMatthew Dillon 		/*
4361067474aSMatthew Dillon 		 * CCB[1] is the error CCB and is not get or put.  It is
4371067474aSMatthew Dillon 		 * also used for probing.  Numerous HBAs only load the
4381067474aSMatthew Dillon 		 * signature from CCB[1] so it MUST be used for the second
4391067474aSMatthew Dillon 		 * FIS.
4401067474aSMatthew Dillon 		 */
4411067474aSMatthew Dillon 		if (i == 1)
4421067474aSMatthew Dillon 			ap->ap_err_ccb = ccb;
4431067474aSMatthew Dillon 		else
444258223a3SMatthew Dillon 			ahci_put_ccb(ccb);
445258223a3SMatthew Dillon 	}
446258223a3SMatthew Dillon 
44712feb904SMatthew Dillon 	/*
44812feb904SMatthew Dillon 	 * Wait for ICC change to complete
44912feb904SMatthew Dillon 	 */
450258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
451258223a3SMatthew Dillon 
452fd8bd957SMatthew Dillon 	/*
45312feb904SMatthew Dillon 	 * Calculate the interrupt mask
45412feb904SMatthew Dillon 	 */
45512feb904SMatthew Dillon 	data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
45612feb904SMatthew Dillon 	       AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
45712feb904SMatthew Dillon 	       AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
45812feb904SMatthew Dillon 	       AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
45912feb904SMatthew Dillon 	       AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
46012feb904SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
46112feb904SMatthew Dillon 		data |= AHCI_PREG_IE_IPME;
46212feb904SMatthew Dillon #ifdef AHCI_COALESCE
46312feb904SMatthew Dillon 	if (sc->sc_ccc_ports & (1 << port)
46412feb904SMatthew Dillon 		data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
46512feb904SMatthew Dillon #endif
46612feb904SMatthew Dillon 	ap->ap_intmask = data;
46712feb904SMatthew Dillon 
46812feb904SMatthew Dillon 	/*
469e8cf3f55SMatthew Dillon 	 * Start the port helper thread.  The helper thread will call
470e8cf3f55SMatthew Dillon 	 * ahci_port_init() so the ports can all be started in parallel.
471e8cf3f55SMatthew Dillon 	 * A failure by ahci_port_init() does not deallocate the port
472e8cf3f55SMatthew Dillon 	 * since we still want hot-plug events.
473fd8bd957SMatthew Dillon 	 */
474f4553de1SMatthew Dillon 	ahci_os_start_port(ap);
475fd8bd957SMatthew Dillon 	return(0);
476fd8bd957SMatthew Dillon freeport:
477fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
478fd8bd957SMatthew Dillon 	return (rc);
479fd8bd957SMatthew Dillon }
480fd8bd957SMatthew Dillon 
481fd8bd957SMatthew Dillon /*
482492bffafSMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.  (from port thread)
483fd8bd957SMatthew Dillon  *
484fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
485fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
486fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
487fd8bd957SMatthew Dillon  *
48822181ab7SMatthew Dillon  * This function is the only way to move a failed port back to active
48922181ab7SMatthew Dillon  * status.
49022181ab7SMatthew Dillon  *
491fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
492fd8bd957SMatthew Dillon  */
493fd8bd957SMatthew Dillon int
49412feb904SMatthew Dillon ahci_port_init(struct ahci_port *ap)
495fd8bd957SMatthew Dillon {
496492bffafSMatthew Dillon 	u_int32_t cmd;
497e8cf3f55SMatthew Dillon 
498e8cf3f55SMatthew Dillon 	/*
499492bffafSMatthew Dillon 	 * Register [re]initialization
500492bffafSMatthew Dillon 	 *
501f17a0cedSMatthew Dillon 	 * Flush the TFD and SERR and make sure the port is stopped before
502f17a0cedSMatthew Dillon 	 * enabling its interrupt.  We no longer cycle the port start as
503f17a0cedSMatthew Dillon 	 * the port should not be started unless a device is present.
504f17a0cedSMatthew Dillon 	 *
505f17a0cedSMatthew Dillon 	 * XXX should we enable FIS reception? (FRE)?
506e8cf3f55SMatthew Dillon 	 */
507492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
508492bffafSMatthew Dillon 	ahci_port_stop(ap, 0);
509492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
510492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
511f17a0cedSMatthew Dillon 	ahci_flush_tfd(ap);
512f17a0cedSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
513492bffafSMatthew Dillon 
514492bffafSMatthew Dillon 	/*
515493d3201SMatthew Dillon 	 * If we are being harsh try to kill the port completely.  Normally
516493d3201SMatthew Dillon 	 * we would want to hold on to some of the state the BIOS may have
517493d3201SMatthew Dillon 	 * set, such as SUD (spin up device).
518492bffafSMatthew Dillon 	 *
519492bffafSMatthew Dillon 	 * AP_F_HARSH_REINIT is cleared in the hard reset state
520492bffafSMatthew Dillon 	 */
521492bffafSMatthew Dillon 	if (ap->ap_flags & AP_F_HARSH_REINIT) {
5229abd2bb8SImre Vadász 		ahci_pwrite(ap, AHCI_PREG_SCTL, ap->ap_sc->sc_ipm_disable);
523492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
524492bffafSMatthew Dillon 
525492bffafSMatthew Dillon 		ahci_os_sleep(1000);
526492bffafSMatthew Dillon 
527492bffafSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
528492bffafSMatthew Dillon 		cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
529eb9f4c83SMatthew Dillon 		cmd |= AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
530eb9f4c83SMatthew Dillon 		cmd |= AHCI_PREG_CMD_ICC_ACTIVE;
531eb9f4c83SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
532eb9f4c83SMatthew Dillon 		ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
533492bffafSMatthew Dillon 		ahci_os_sleep(1000);
534492bffafSMatthew Dillon 	}
535492bffafSMatthew Dillon 
536492bffafSMatthew Dillon 	/*
537492bffafSMatthew Dillon 	 * Clear any pending garbage and re-enable the interrupt before
538492bffafSMatthew Dillon 	 * going to the next stage.
539492bffafSMatthew Dillon 	 */
540492bffafSMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
541492bffafSMatthew Dillon 	ap->ap_pmcount = 0;
542492bffafSMatthew Dillon 
543492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
544492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
545492bffafSMatthew Dillon 	ahci_flush_tfd(ap);
546492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
547492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, -1);
548492bffafSMatthew Dillon 
549f4553de1SMatthew Dillon 	ahci_port_interrupt_enable(ap);
550492bffafSMatthew Dillon 
55112feb904SMatthew Dillon 	return (0);
552f4553de1SMatthew Dillon }
553f4553de1SMatthew Dillon 
554f4553de1SMatthew Dillon /*
555f4553de1SMatthew Dillon  * Enable or re-enable interrupts on a port.
556f4553de1SMatthew Dillon  *
557f4553de1SMatthew Dillon  * This routine is called from the port initialization code or from the
558f4553de1SMatthew Dillon  * helper thread as the real interrupt may be forced to turn off certain
559f4553de1SMatthew Dillon  * interrupt sources.
560f4553de1SMatthew Dillon  */
561f4553de1SMatthew Dillon void
562f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap)
563f4553de1SMatthew Dillon {
56412feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
5651980eff3SMatthew Dillon }
566258223a3SMatthew Dillon 
567fd8bd957SMatthew Dillon /*
568f5caeaa0SMatthew Dillon  * Manage the agressive link power management capability.
569f17a0cedSMatthew Dillon  */
570f17a0cedSMatthew Dillon void
571f17a0cedSMatthew Dillon ahci_port_link_pwr_mgmt(struct ahci_port *ap, int link_pwr_mgmt)
572f17a0cedSMatthew Dillon {
573f17a0cedSMatthew Dillon 	u_int32_t cmd, sctl;
574f17a0cedSMatthew Dillon 
575f17a0cedSMatthew Dillon 	if (link_pwr_mgmt == ap->link_pwr_mgmt)
576f17a0cedSMatthew Dillon 		return;
577f17a0cedSMatthew Dillon 
578f17a0cedSMatthew Dillon 	if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) == 0) {
579f17a0cedSMatthew Dillon 		kprintf("%s: link power management not supported.\n",
580f17a0cedSMatthew Dillon 			PORTNAME(ap));
581f17a0cedSMatthew Dillon 		return;
582f17a0cedSMatthew Dillon 	}
583f17a0cedSMatthew Dillon 
584f17a0cedSMatthew Dillon 	ahci_os_lock_port(ap);
585f17a0cedSMatthew Dillon 
586f17a0cedSMatthew Dillon 	if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_AGGR &&
587f17a0cedSMatthew Dillon 	    (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSC)) {
588f17a0cedSMatthew Dillon 		kprintf("%s: enabling aggressive link power management.\n",
589f17a0cedSMatthew Dillon 			PORTNAME(ap));
590f17a0cedSMatthew Dillon 
591795adb22SMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
592795adb22SMatthew Dillon 
593f17a0cedSMatthew Dillon 		ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
594f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
595f17a0cedSMatthew Dillon 
596f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
5979abd2bb8SImre Vadász 		sctl &= ~(AHCI_PREG_SCTL_IPM);
5989abd2bb8SImre Vadász 		if (ap->ap_sc->sc_cap2 & AHCI_REG_CAP2_SDS)
5999abd2bb8SImre Vadász 			sctl |= AHCI_PREG_SCTL_IPM_NODEVSLP;
600f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
601f17a0cedSMatthew Dillon 
602795adb22SMatthew Dillon 		/*
603795adb22SMatthew Dillon 		 * Enable device initiated link power management for
604795adb22SMatthew Dillon 		 * directly attached devices that support it.
605795adb22SMatthew Dillon 		 */
606795adb22SMatthew Dillon 		if (ap->ap_type != ATA_PORT_T_PM &&
607750495d0SImre Vadász 		    (ap->ap_ata[0]->at_identify.satafsup &
608750495d0SImre Vadász 		    SATA_FEATURE_SUP_DEVIPS)) {
609795adb22SMatthew Dillon 			if (ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 1))
610795adb22SMatthew Dillon 				kprintf("%s: Could not enable device initiated "
611795adb22SMatthew Dillon 				    "link power management.\n",
612795adb22SMatthew Dillon 				    PORTNAME(ap));
613795adb22SMatthew Dillon 		}
614795adb22SMatthew Dillon 
615f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
616f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ASP;
617f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ALPE;
618f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
619f17a0cedSMatthew Dillon 	} else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_MEDIUM &&
620f17a0cedSMatthew Dillon 	           (ap->ap_sc->sc_cap & AHCI_REG_CAP_PSC)) {
621f17a0cedSMatthew Dillon 		kprintf("%s: enabling medium link power management.\n",
622f17a0cedSMatthew Dillon 			PORTNAME(ap));
623f17a0cedSMatthew Dillon 
624795adb22SMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
625795adb22SMatthew Dillon 
626f17a0cedSMatthew Dillon 		ap->ap_intmask &= ~AHCI_PREG_IE_PRCE;
627f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
628f17a0cedSMatthew Dillon 
629f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
6309abd2bb8SImre Vadász 		sctl &= ~(AHCI_PREG_SCTL_IPM);
6319abd2bb8SImre Vadász 		sctl |= AHCI_PREG_SCTL_IPM_NOSLUMBER;
6329abd2bb8SImre Vadász 		if (ap->ap_sc->sc_cap2 & AHCI_REG_CAP2_SDS)
6339abd2bb8SImre Vadász 			sctl |= AHCI_PREG_SCTL_IPM_NODEVSLP;
634f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
635f17a0cedSMatthew Dillon 
636f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
637f17a0cedSMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_ASP;
638f17a0cedSMatthew Dillon 		cmd |= AHCI_PREG_CMD_ALPE;
639f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
640f17a0cedSMatthew Dillon 
641f17a0cedSMatthew Dillon 	} else if (link_pwr_mgmt == AHCI_LINK_PWR_MGMT_NONE) {
642f17a0cedSMatthew Dillon 		kprintf("%s: disabling link power management.\n",
643f17a0cedSMatthew Dillon 			PORTNAME(ap));
644f17a0cedSMatthew Dillon 
645795adb22SMatthew Dillon 		/* Disable device initiated link power management */
646795adb22SMatthew Dillon 		if (ap->ap_type != ATA_PORT_T_PM &&
647750495d0SImre Vadász 		    (ap->ap_ata[0]->at_identify.satafsup &
648750495d0SImre Vadász 		    SATA_FEATURE_SUP_DEVIPS)) {
649795adb22SMatthew Dillon 			ahci_set_feature(ap, NULL, ATA_SATAFT_DEVIPS, 0);
650750495d0SImre Vadász 		}
651795adb22SMatthew Dillon 
652f17a0cedSMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
653f17a0cedSMatthew Dillon 		cmd &= ~(AHCI_PREG_CMD_ALPE | AHCI_PREG_CMD_ASP);
654f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
655f17a0cedSMatthew Dillon 
656f17a0cedSMatthew Dillon 		sctl = ahci_pread(ap, AHCI_PREG_SCTL);
6579abd2bb8SImre Vadász 		sctl &= ~(AHCI_PREG_SCTL_IPM);
6589abd2bb8SImre Vadász 		sctl |= ap->ap_sc->sc_ipm_disable;
659f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, sctl);
660f17a0cedSMatthew Dillon 
661f17a0cedSMatthew Dillon 		/* let the drive come back to avoid PRCS interrupts later */
662f17a0cedSMatthew Dillon 		ahci_os_unlock_port(ap);
663f17a0cedSMatthew Dillon 		ahci_os_sleep(1000);
664f17a0cedSMatthew Dillon 		ahci_os_lock_port(ap);
665f17a0cedSMatthew Dillon 
666795adb22SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
667795adb22SMatthew Dillon 			    AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
668f17a0cedSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
669f17a0cedSMatthew Dillon 
670f17a0cedSMatthew Dillon 		ap->ap_intmask |= AHCI_PREG_IE_PRCE;
671f17a0cedSMatthew Dillon 		ahci_port_interrupt_enable(ap);
672f17a0cedSMatthew Dillon 
673f17a0cedSMatthew Dillon 		ap->link_pwr_mgmt = link_pwr_mgmt;
674f17a0cedSMatthew Dillon 	} else {
675f17a0cedSMatthew Dillon 		kprintf("%s: unsupported link power management state %d.\n",
676f17a0cedSMatthew Dillon 			PORTNAME(ap), link_pwr_mgmt);
677f17a0cedSMatthew Dillon 	}
678f17a0cedSMatthew Dillon 
679f17a0cedSMatthew Dillon 	ahci_os_unlock_port(ap);
680f17a0cedSMatthew Dillon }
681f17a0cedSMatthew Dillon 
682795adb22SMatthew Dillon /*
683795adb22SMatthew Dillon  * Return current link power state.
684795adb22SMatthew Dillon  */
685795adb22SMatthew Dillon int
686795adb22SMatthew Dillon ahci_port_link_pwr_state(struct ahci_port *ap)
687795adb22SMatthew Dillon {
688795adb22SMatthew Dillon 	uint32_t r;
689795adb22SMatthew Dillon 
690795adb22SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SSTS);
691d90e4fd1SImre Vadász 	switch (r & AHCI_PREG_SSTS_IPM) {
692d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_ACTIVE:
693795adb22SMatthew Dillon 		return 1;
694d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_PARTIAL:
695795adb22SMatthew Dillon 		return 2;
696d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_SLUMBER:
697795adb22SMatthew Dillon 		return 3;
698d90e4fd1SImre Vadász 	case AHCI_PREG_SSTS_IPM_DEVSLEEP:
699d90e4fd1SImre Vadász 		return 4;
700795adb22SMatthew Dillon 	default:
701795adb22SMatthew Dillon 		return 0;
702795adb22SMatthew Dillon 	}
703795adb22SMatthew Dillon }
704f17a0cedSMatthew Dillon 
705f17a0cedSMatthew Dillon /*
7063209f581SMatthew Dillon  * Run the port / target state machine from a main context.
7073209f581SMatthew Dillon  *
7083209f581SMatthew Dillon  * The state machine for the port is always run.
7093209f581SMatthew Dillon  *
7103209f581SMatthew Dillon  * If atx is non-NULL run the state machine for a particular target.
7113209f581SMatthew Dillon  * If atx is NULL run the state machine for all targets.
7123209f581SMatthew Dillon  */
7133209f581SMatthew Dillon void
714831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial)
7153209f581SMatthew Dillon {
7163209f581SMatthew Dillon 	struct ata_port *at;
7173209f581SMatthew Dillon 	u_int32_t data;
7183209f581SMatthew Dillon 	int target;
7193209f581SMatthew Dillon 	int didsleep;
720831bc9e3SMatthew Dillon 	int loop;
7213209f581SMatthew Dillon 
722831bc9e3SMatthew Dillon 	/*
723831bc9e3SMatthew Dillon 	 * State machine for port.  Note that CAM is not yet associated
724831bc9e3SMatthew Dillon 	 * during the initial parallel probe and the port's probe state
725831bc9e3SMatthew Dillon 	 * will not get past ATA_PROBE_NEED_IDENT.
726831bc9e3SMatthew Dillon 	 */
727c408a8b3SMatthew Dillon 	{
7281067474aSMatthew Dillon 		if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
729*2fa886e9SMatthew Dillon 			kprintf("%s: Waiting 5 seconds on insertion\n",
7301067474aSMatthew Dillon 				PORTNAME(ap));
731*2fa886e9SMatthew Dillon 			ahci_os_sleep(5000);
7321067474aSMatthew Dillon 			initial = 1;
7333209f581SMatthew Dillon 		}
7341067474aSMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT)
73512feb904SMatthew Dillon 			ahci_port_init(ap);
7363209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
7373209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 1);
7383209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
7393209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 0);
7403209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
7413209f581SMatthew Dillon 			ahci_cam_probe(ap, NULL);
7423209f581SMatthew Dillon 	}
7433209f581SMatthew Dillon 	if (ap->ap_type != ATA_PORT_T_PM) {
7443209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_FAILED) {
7453209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 0);
746f4553de1SMatthew Dillon 		} else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
7473209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 1);
7483209f581SMatthew Dillon 		}
7493209f581SMatthew Dillon 		return;
7503209f581SMatthew Dillon 	}
7513209f581SMatthew Dillon 
752831bc9e3SMatthew Dillon 	/*
753831bc9e3SMatthew Dillon 	 * Port Multiplier state machine.
754831bc9e3SMatthew Dillon 	 *
755831bc9e3SMatthew Dillon 	 * Get a mask of changed targets and combine with any runnable
756831bc9e3SMatthew Dillon 	 * states already present.
757831bc9e3SMatthew Dillon 	 */
758831bc9e3SMatthew Dillon 	for (loop = 0; ;++loop) {
7592cc2e845SMatthew Dillon 		if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
7603209f581SMatthew Dillon 			kprintf("%s: PM unable to read hot-plug bitmap\n",
7613209f581SMatthew Dillon 				PORTNAME(ap));
7623209f581SMatthew Dillon 			break;
7633209f581SMatthew Dillon 		}
7643209f581SMatthew Dillon 
7653209f581SMatthew Dillon 		/*
766831bc9e3SMatthew Dillon 		 * Do at least one loop, then stop if no more state changes
767831bc9e3SMatthew Dillon 		 * have occured.  The PM might not generate a new
768831bc9e3SMatthew Dillon 		 * notification until we clear the entire bitmap.
7693209f581SMatthew Dillon 		 */
770831bc9e3SMatthew Dillon 		if (loop && data == 0)
7713209f581SMatthew Dillon 			break;
7723209f581SMatthew Dillon 
7733209f581SMatthew Dillon 		/*
7743209f581SMatthew Dillon 		 * New devices showing up in the bitmap require some spin-up
7753209f581SMatthew Dillon 		 * time before we start probing them.  Reset didsleep.  The
7763209f581SMatthew Dillon 		 * first new device we detect will sleep before probing.
777831bc9e3SMatthew Dillon 		 *
778831bc9e3SMatthew Dillon 		 * This only applies to devices whos change bit is set in
779831bc9e3SMatthew Dillon 		 * the data, and does not apply to the initial boot-time
780831bc9e3SMatthew Dillon 		 * probe.
7813209f581SMatthew Dillon 		 */
7823209f581SMatthew Dillon 		didsleep = 0;
7833209f581SMatthew Dillon 
7843209f581SMatthew Dillon 		for (target = 0; target < ap->ap_pmcount; ++target) {
785b012a2caSMatthew Dillon 			at = ap->ap_ata[target];
7863209f581SMatthew Dillon 
7873209f581SMatthew Dillon 			/*
7883209f581SMatthew Dillon 			 * Check the target state for targets behind the PM
7893209f581SMatthew Dillon 			 * which have changed state.  This will adjust
7903209f581SMatthew Dillon 			 * at_probe and set ATA_PORT_F_RESCAN
7913209f581SMatthew Dillon 			 *
792*2fa886e9SMatthew Dillon 			 * We want to wait at least 5 seconds before probing
7933209f581SMatthew Dillon 			 * a newly inserted device.  If the check status
7943209f581SMatthew Dillon 			 * indicates a device is present and in need of a
7953209f581SMatthew Dillon 			 * hard reset, we make sure we have slept before
7963209f581SMatthew Dillon 			 * continuing.
797831bc9e3SMatthew Dillon 			 *
7981067474aSMatthew Dillon 			 * We also need to wait at least 1 second for the
7991067474aSMatthew Dillon 			 * PHY state to change after insertion, if we
800*2fa886e9SMatthew Dillon 			 * haven't already waited the 5 seconds.
8011067474aSMatthew Dillon 			 *
802831bc9e3SMatthew Dillon 			 * NOTE: When pm_check_good finds a good port it
803831bc9e3SMatthew Dillon 			 *	 typically starts us in probe state
804831bc9e3SMatthew Dillon 			 *	 NEED_HARD_RESET rather than INIT.
8053209f581SMatthew Dillon 			 */
8063209f581SMatthew Dillon 			if (data & (1 << target)) {
8071067474aSMatthew Dillon 				if (initial == 0 && didsleep == 0)
8081067474aSMatthew Dillon 					ahci_os_sleep(1000);
8093209f581SMatthew Dillon 				ahci_pm_check_good(ap, target);
810831bc9e3SMatthew Dillon 				if (initial == 0 && didsleep == 0 &&
811831bc9e3SMatthew Dillon 				    at->at_probe <= ATA_PROBE_NEED_HARD_RESET
812831bc9e3SMatthew Dillon 				) {
8133209f581SMatthew Dillon 					didsleep = 1;
814*2fa886e9SMatthew Dillon 					kprintf("%s: Waiting 5 seconds on insertion\n", PORTNAME(ap));
815*2fa886e9SMatthew Dillon 					ahci_os_sleep(5000);
8163209f581SMatthew Dillon 				}
8173209f581SMatthew Dillon 			}
818831bc9e3SMatthew Dillon 
819831bc9e3SMatthew Dillon 			/*
820831bc9e3SMatthew Dillon 			 * Report hot-plug events before the probe state
821831bc9e3SMatthew Dillon 			 * really gets hot.  Only actual events are reported
822831bc9e3SMatthew Dillon 			 * here to reduce spew.
823831bc9e3SMatthew Dillon 			 */
824831bc9e3SMatthew Dillon 			if (data & (1 << target)) {
825831bc9e3SMatthew Dillon 				kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
826831bc9e3SMatthew Dillon 				switch(at->at_probe) {
827831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_INIT:
828831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_HARD_RESET:
829831bc9e3SMatthew Dillon 					kprintf("Device inserted\n");
830831bc9e3SMatthew Dillon 					break;
831831bc9e3SMatthew Dillon 				case ATA_PROBE_FAILED:
832831bc9e3SMatthew Dillon 					kprintf("Device removed\n");
833831bc9e3SMatthew Dillon 					break;
834831bc9e3SMatthew Dillon 				default:
835831bc9e3SMatthew Dillon 					kprintf("Device probe in progress\n");
836831bc9e3SMatthew Dillon 					break;
837831bc9e3SMatthew Dillon 				}
8383209f581SMatthew Dillon 			}
8393209f581SMatthew Dillon 
8403209f581SMatthew Dillon 			/*
841831bc9e3SMatthew Dillon 			 * Run through the state machine as necessary if
842831bc9e3SMatthew Dillon 			 * the port is not marked failed.
843831bc9e3SMatthew Dillon 			 *
844831bc9e3SMatthew Dillon 			 * The state machine may stop at NEED_IDENT if
845831bc9e3SMatthew Dillon 			 * CAM is not yet attached.
846831bc9e3SMatthew Dillon 			 *
847831bc9e3SMatthew Dillon 			 * Acquire exclusive access to the port while we
848831bc9e3SMatthew Dillon 			 * are doing this.  This prevents command-completion
849831bc9e3SMatthew Dillon 			 * from queueing commands for non-polled targets
850831bc9e3SMatthew Dillon 			 * inbetween our probe steps.  We need to do this
851831bc9e3SMatthew Dillon 			 * because the reset probes can generate severe PHY
852831bc9e3SMatthew Dillon 			 * and protocol errors and soft-brick the port.
8533209f581SMatthew Dillon 			 */
854831bc9e3SMatthew Dillon 			if (at->at_probe != ATA_PROBE_FAILED &&
855831bc9e3SMatthew Dillon 			    at->at_probe != ATA_PROBE_GOOD) {
856831bc9e3SMatthew Dillon 				ahci_beg_exclusive_access(ap, at);
8573209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_INIT)
85812feb904SMatthew Dillon 					ahci_pm_port_init(ap, at);
8593209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
8603209f581SMatthew Dillon 					ahci_port_reset(ap, at, 1);
8613209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
8623209f581SMatthew Dillon 					ahci_port_reset(ap, at, 0);
8633209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_IDENT)
8643209f581SMatthew Dillon 					ahci_cam_probe(ap, at);
865831bc9e3SMatthew Dillon 				ahci_end_exclusive_access(ap, at);
8663209f581SMatthew Dillon 			}
8673209f581SMatthew Dillon 
8683209f581SMatthew Dillon 			/*
869831bc9e3SMatthew Dillon 			 * Add or remove from CAM
8703209f581SMatthew Dillon 			 */
8713209f581SMatthew Dillon 			if (at->at_features & ATA_PORT_F_RESCAN) {
8723209f581SMatthew Dillon 				at->at_features &= ~ATA_PORT_F_RESCAN;
8733209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_FAILED) {
8743209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 0);
875f4553de1SMatthew Dillon 				} else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
8763209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 1);
8773209f581SMatthew Dillon 				}
8783209f581SMatthew Dillon 			}
8793560ed94SMatthew Dillon 			data &= ~(1 << target);
8803560ed94SMatthew Dillon 		}
8813560ed94SMatthew Dillon 		if (data) {
8823560ed94SMatthew Dillon 			kprintf("%s: WARNING (PM): extra bits set in "
8833560ed94SMatthew Dillon 				"EINFO: %08x\n", PORTNAME(ap), data);
8843560ed94SMatthew Dillon 			while (target < AHCI_MAX_PMPORTS) {
8853560ed94SMatthew Dillon 				ahci_pm_check_good(ap, target);
8863560ed94SMatthew Dillon 				++target;
8873560ed94SMatthew Dillon 			}
8883209f581SMatthew Dillon 		}
8893209f581SMatthew Dillon 	}
8903209f581SMatthew Dillon }
8913209f581SMatthew Dillon 
8923209f581SMatthew Dillon 
8933209f581SMatthew Dillon /*
894fd8bd957SMatthew Dillon  * De-initialize and detach a port.
895fd8bd957SMatthew Dillon  */
896258223a3SMatthew Dillon void
897258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
898258223a3SMatthew Dillon {
899258223a3SMatthew Dillon 	struct ahci_port	*ap = sc->sc_ports[port];
900258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
901b012a2caSMatthew Dillon 	int i;
902258223a3SMatthew Dillon 
90317eab71eSMatthew Dillon 	/*
90417eab71eSMatthew Dillon 	 * Ensure port is disabled and its interrupts are all flushed.
90517eab71eSMatthew Dillon 	 */
906258223a3SMatthew Dillon 	if (ap->ap_sc) {
90717eab71eSMatthew Dillon 		ahci_port_stop(ap, 1);
908f4553de1SMatthew Dillon 		ahci_os_stop_port(ap);
909258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
910258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
911258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
912258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
913258223a3SMatthew Dillon 	}
914258223a3SMatthew Dillon 
915258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
916258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
917258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
918258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
919258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
920258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
921258223a3SMatthew Dillon 			}
922258223a3SMatthew Dillon 		}
9231067474aSMatthew Dillon 		if ((ccb = ap->ap_err_ccb) != NULL) {
9241067474aSMatthew Dillon 			if (ccb->ccb_dmamap) {
9251067474aSMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
9261067474aSMatthew Dillon 						   ccb->ccb_dmamap);
9271067474aSMatthew Dillon 				ccb->ccb_dmamap = NULL;
9281067474aSMatthew Dillon 			}
9291067474aSMatthew Dillon 			ap->ap_err_ccb = NULL;
9301067474aSMatthew Dillon 		}
931258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
932258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
933258223a3SMatthew Dillon 	}
934258223a3SMatthew Dillon 
935258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
936258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
937258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
938258223a3SMatthew Dillon 	}
939258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
940258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
941258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
942258223a3SMatthew Dillon 	}
943258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
944258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
945258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
946258223a3SMatthew Dillon 	}
9471980eff3SMatthew Dillon 	if (ap->ap_ata) {
948b012a2caSMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
949b012a2caSMatthew Dillon 			if (ap->ap_ata[i]) {
950b012a2caSMatthew Dillon 				kfree(ap->ap_ata[i], M_DEVBUF);
951b012a2caSMatthew Dillon 				ap->ap_ata[i] = NULL;
952b012a2caSMatthew Dillon 			}
953b012a2caSMatthew Dillon 		}
9541980eff3SMatthew Dillon 	}
95512feb904SMatthew Dillon 	if (ap->ap_err_scratch) {
95612feb904SMatthew Dillon 		kfree(ap->ap_err_scratch, M_DEVBUF);
95712feb904SMatthew Dillon 		ap->ap_err_scratch = NULL;
95812feb904SMatthew Dillon 	}
959258223a3SMatthew Dillon 
960258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
961258223a3SMatthew Dillon 
962258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
963258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
964258223a3SMatthew Dillon }
965258223a3SMatthew Dillon 
966492bffafSMatthew Dillon static
967492bffafSMatthew Dillon u_int32_t
968492bffafSMatthew Dillon ahci_pactive(struct ahci_port *ap)
969492bffafSMatthew Dillon {
970492bffafSMatthew Dillon 	u_int32_t mask;
971492bffafSMatthew Dillon 
972492bffafSMatthew Dillon 	mask = ahci_pread(ap, AHCI_PREG_CI);
973492bffafSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ)
974492bffafSMatthew Dillon 		mask |= ahci_pread(ap, AHCI_PREG_SACT);
975492bffafSMatthew Dillon 	return(mask);
976492bffafSMatthew Dillon }
977492bffafSMatthew Dillon 
978fd8bd957SMatthew Dillon /*
979fd8bd957SMatthew Dillon  * Start high-level command processing on the port
980fd8bd957SMatthew Dillon  */
981258223a3SMatthew Dillon int
98217eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap)
983258223a3SMatthew Dillon {
98412feb904SMatthew Dillon 	u_int32_t	r, s, is, tfd;
985258223a3SMatthew Dillon 
98617eab71eSMatthew Dillon 	/*
98717eab71eSMatthew Dillon 	 * FRE must be turned on before ST.  Wait for FR to go active
98817eab71eSMatthew Dillon 	 * before turning on ST.  The spec doesn't seem to think this
98917eab71eSMatthew Dillon 	 * is necessary but waiting here avoids an on-off race in the
99017eab71eSMatthew Dillon 	 * ahci_port_stop() code.
99117eab71eSMatthew Dillon 	 */
99212feb904SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD);
99317eab71eSMatthew Dillon 	if ((r & AHCI_PREG_CMD_FRE) == 0) {
994258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_FRE;
99517eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
99617eab71eSMatthew Dillon 	}
99717eab71eSMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
99817eab71eSMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
99917eab71eSMatthew Dillon 			kprintf("%s: Cannot start FIS reception\n",
100017eab71eSMatthew Dillon 				PORTNAME(ap));
100117eab71eSMatthew Dillon 			return (2);
100217eab71eSMatthew Dillon 		}
1003f17a0cedSMatthew Dillon 	} else {
1004f17a0cedSMatthew Dillon 		ahci_os_sleep(10);
100517eab71eSMatthew Dillon 	}
100617eab71eSMatthew Dillon 
100717eab71eSMatthew Dillon 	/*
100817eab71eSMatthew Dillon 	 * Turn on ST, wait for CR to come up.
100917eab71eSMatthew Dillon 	 */
1010258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_ST;
1011258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
1012eb9f4c83SMatthew Dillon 
1013eb9f4c83SMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_CR) == 0 &&
1014eb9f4c83SMatthew Dillon 	    ahci_pwait_set_to(ap, 2000, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
10158bf6a3ffSMatthew Dillon 		s = ahci_pread(ap, AHCI_PREG_SERR);
10168bf6a3ffSMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
10178bf6a3ffSMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
10181980eff3SMatthew Dillon 		kprintf("%s: Cannot start command DMA\n"
1019c3783d8fSzrj 			"NCMP=%pb%i NSERR=%pb%i\n"
1020c3783d8fSzrj 			"NEWIS=%pb%i\n"
1021c3783d8fSzrj 		        "NEWTFD=%pb%i\n",
10221980eff3SMatthew Dillon 			PORTNAME(ap),
1023c3783d8fSzrj 			AHCI_PFMT_CMD, r, AHCI_PFMT_SERR, s,
1024c3783d8fSzrj 			AHCI_PFMT_IS, is,
1025c3783d8fSzrj 			AHCI_PFMT_TFD_STS, tfd);
102617eab71eSMatthew Dillon 		return (1);
102717eab71eSMatthew Dillon 	}
1028258223a3SMatthew Dillon 
1029258223a3SMatthew Dillon #ifdef AHCI_COALESCE
103017eab71eSMatthew Dillon 	/*
103117eab71eSMatthew Dillon 	 * (Re-)enable coalescing on the port.
103217eab71eSMatthew Dillon 	 */
1033258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1034258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
1035258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1036258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
1037258223a3SMatthew Dillon 	}
1038258223a3SMatthew Dillon #endif
1039258223a3SMatthew Dillon 
1040258223a3SMatthew Dillon 	return (0);
1041258223a3SMatthew Dillon }
1042258223a3SMatthew Dillon 
1043fd8bd957SMatthew Dillon /*
1044fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
10454c339a5fSMatthew Dillon  *
10464c339a5fSMatthew Dillon  * WARNING!  If the port is stopped while CR is still active our saved
10474c339a5fSMatthew Dillon  *	     CI/SACT will race any commands completed by the command
10484c339a5fSMatthew Dillon  *	     processor prior to being able to stop.  Thus we never call
10494c339a5fSMatthew Dillon  *	     this function unless we intend to dispose of any remaining
10504c339a5fSMatthew Dillon  *	     active commands.  In particular, this complicates the timeout
10514c339a5fSMatthew Dillon  *	     code.
1052fd8bd957SMatthew Dillon  */
1053258223a3SMatthew Dillon int
1054258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
1055258223a3SMatthew Dillon {
1056258223a3SMatthew Dillon 	u_int32_t	r;
1057258223a3SMatthew Dillon 
1058258223a3SMatthew Dillon #ifdef AHCI_COALESCE
105917eab71eSMatthew Dillon 	/*
106017eab71eSMatthew Dillon 	 * Disable coalescing on the port while it is stopped.
106117eab71eSMatthew Dillon 	 */
1062258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
1063258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
1064258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
1065258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
1066258223a3SMatthew Dillon 	}
1067258223a3SMatthew Dillon #endif
1068258223a3SMatthew Dillon 
106917eab71eSMatthew Dillon 	/*
107017eab71eSMatthew Dillon 	 * Turn off ST, then wait for CR to go off.
107117eab71eSMatthew Dillon 	 */
1072258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
10738119d5f5SMatthew Dillon 	if (r & AHCI_PREG_CMD_ST) {
1074258223a3SMatthew Dillon 		r &= ~AHCI_PREG_CMD_ST;
1075258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
10768119d5f5SMatthew Dillon 	}
1077258223a3SMatthew Dillon 
107817eab71eSMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
107917eab71eSMatthew Dillon 		kprintf("%s: Port bricked, unable to stop (ST)\n",
108017eab71eSMatthew Dillon 			PORTNAME(ap));
1081258223a3SMatthew Dillon 		return (1);
108217eab71eSMatthew Dillon 	}
1083258223a3SMatthew Dillon 
10841980eff3SMatthew Dillon #if 0
108517eab71eSMatthew Dillon 	/*
108617eab71eSMatthew Dillon 	 * Turn off FRE, then wait for FR to go off.  FRE cannot
108717eab71eSMatthew Dillon 	 * be turned off until CR transitions to 0.
108817eab71eSMatthew Dillon 	 */
10891980eff3SMatthew Dillon 	if ((r & AHCI_PREG_CMD_FR) == 0) {
10901980eff3SMatthew Dillon 		kprintf("%s: FR stopped, clear FRE for next start\n",
10911980eff3SMatthew Dillon 			PORTNAME(ap));
10921980eff3SMatthew Dillon 		stop_fis_rx = 2;
10931980eff3SMatthew Dillon 	}
10941980eff3SMatthew Dillon #endif
109517eab71eSMatthew Dillon 	if (stop_fis_rx) {
109617eab71eSMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
109717eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
109817eab71eSMatthew Dillon 		if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
109917eab71eSMatthew Dillon 			kprintf("%s: Port bricked, unable to stop (FRE)\n",
110017eab71eSMatthew Dillon 				PORTNAME(ap));
1101258223a3SMatthew Dillon 			return (2);
110217eab71eSMatthew Dillon 		}
110317eab71eSMatthew Dillon 	}
1104258223a3SMatthew Dillon 	return (0);
1105258223a3SMatthew Dillon }
1106258223a3SMatthew Dillon 
1107fd8bd957SMatthew Dillon /*
1108fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
1109fd8bd957SMatthew Dillon  */
1110258223a3SMatthew Dillon int
1111258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
1112258223a3SMatthew Dillon {
1113258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1114258223a3SMatthew Dillon 	u_int32_t			cmd;
1115258223a3SMatthew Dillon 
1116258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
11178119d5f5SMatthew Dillon 	if ((sc->sc_cap & AHCI_REG_CAP_SCLO) == 0)
1118258223a3SMatthew Dillon 		return (1);
1119258223a3SMatthew Dillon 
1120258223a3SMatthew Dillon 	/* Issue CLO */
1121258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1122258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
1123258223a3SMatthew Dillon 
1124258223a3SMatthew Dillon 	/* Wait for completion */
1125258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
1126258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
1127258223a3SMatthew Dillon 		return (1);
1128258223a3SMatthew Dillon 	}
1129258223a3SMatthew Dillon 
1130258223a3SMatthew Dillon 	return (0);
1131258223a3SMatthew Dillon }
1132258223a3SMatthew Dillon 
1133fd8bd957SMatthew Dillon /*
11341980eff3SMatthew Dillon  * Reset a port.
113517eab71eSMatthew Dillon  *
11361980eff3SMatthew Dillon  * If hard is 0 perform a softreset of the port.
113717eab71eSMatthew Dillon  * If hard is 1 perform a hard reset of the port.
11381980eff3SMatthew Dillon  *
11391980eff3SMatthew Dillon  * If at is non-NULL an indirect port via a port-multiplier is being
11401980eff3SMatthew Dillon  * reset, otherwise a direct port is being reset.
11411980eff3SMatthew Dillon  *
11421980eff3SMatthew Dillon  * NOTE: Indirect ports can only be soft-reset.
114317eab71eSMatthew Dillon  */
114417eab71eSMatthew Dillon int
11451980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
114617eab71eSMatthew Dillon {
114717eab71eSMatthew Dillon 	int rc;
114817eab71eSMatthew Dillon 
114917eab71eSMatthew Dillon 	if (hard) {
11501980eff3SMatthew Dillon 		if (at)
11511980eff3SMatthew Dillon 			rc = ahci_pm_hardreset(ap, at->at_target, hard);
11521980eff3SMatthew Dillon 		else
11531980eff3SMatthew Dillon 			rc = ahci_port_hardreset(ap, hard);
115417eab71eSMatthew Dillon 	} else {
11551980eff3SMatthew Dillon 		if (at)
11561980eff3SMatthew Dillon 			rc = ahci_pm_softreset(ap, at->at_target);
11571980eff3SMatthew Dillon 		else
115817eab71eSMatthew Dillon 			rc = ahci_port_softreset(ap);
115917eab71eSMatthew Dillon 	}
116017eab71eSMatthew Dillon 	return(rc);
116117eab71eSMatthew Dillon }
116217eab71eSMatthew Dillon 
116317eab71eSMatthew Dillon /*
1164fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
1165fd8bd957SMatthew Dillon  *
11661980eff3SMatthew Dillon  * (at) will be NULL when soft-resetting a directly-attached device, and
11671980eff3SMatthew Dillon  * non-NULL when soft-resetting a device through a port multiplier.
11681980eff3SMatthew Dillon  *
1169fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
11701980eff3SMatthew Dillon  * a reset to the connected device using device commands.
1171fd8bd957SMatthew Dillon  */
1172258223a3SMatthew Dillon int
1173258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
1174258223a3SMatthew Dillon {
1175258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
1176258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
1177258223a3SMatthew Dillon 	u_int8_t		*fis;
11783209f581SMatthew Dillon 	int			error;
1179258223a3SMatthew Dillon 
11803209f581SMatthew Dillon 	error = EIO;
11811980eff3SMatthew Dillon 
1182074579dfSMatthew Dillon 	if (bootverbose) {
1183c3783d8fSzrj 		kprintf("%s: START SOFTRESET %pb%i\n", PORTNAME(ap),
1184c3783d8fSzrj 			AHCI_PFMT_CMD, ahci_pread(ap, AHCI_PREG_CMD));
1185074579dfSMatthew Dillon 	}
11861980eff3SMatthew Dillon 
1187258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
1188258223a3SMatthew Dillon 
1189258223a3SMatthew Dillon 	crit_enter();
11901980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
11911980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
1192258223a3SMatthew Dillon 
11931980eff3SMatthew Dillon 	/*
11941980eff3SMatthew Dillon 	 * Remember port state in cmd (main to restore start/stop)
11951980eff3SMatthew Dillon 	 *
11961980eff3SMatthew Dillon 	 * Idle port.
11971980eff3SMatthew Dillon 	 */
1198258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
1199258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
1200258223a3SMatthew Dillon 			PORTNAME(ap));
1201258223a3SMatthew Dillon 		goto err;
1202258223a3SMatthew Dillon 	}
1203cf5f3a81SMatthew Dillon 
1204cf5f3a81SMatthew Dillon 	/*
12051980eff3SMatthew Dillon 	 * Request CLO if device appears hung.
1206cf5f3a81SMatthew Dillon 	 */
1207258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
1208258223a3SMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1209258223a3SMatthew Dillon 		ahci_port_clo(ap);
1210258223a3SMatthew Dillon 	}
1211258223a3SMatthew Dillon 
12121980eff3SMatthew Dillon 	/*
12131980eff3SMatthew Dillon 	 * This is an attempt to clear errors so a new signature will
12141980eff3SMatthew Dillon 	 * be latched.  It isn't working properly.  XXX
12151980eff3SMatthew Dillon 	 */
1216cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
12171980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1218258223a3SMatthew Dillon 
1219258223a3SMatthew Dillon 	/* Restart port */
122017eab71eSMatthew Dillon 	if (ahci_port_start(ap)) {
1221258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
1222258223a3SMatthew Dillon 		        PORTNAME(ap));
1223258223a3SMatthew Dillon 		goto err;
1224258223a3SMatthew Dillon 	}
1225258223a3SMatthew Dillon 
1226258223a3SMatthew Dillon 	/* Check whether CLO worked */
1227258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1228258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1229258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
1230258223a3SMatthew Dillon 			PORTNAME(ap),
1231258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1232258223a3SMatthew Dillon 			? "failed" : "unsupported");
12333209f581SMatthew Dillon 		error = EBUSY;
1234258223a3SMatthew Dillon 		goto err;
1235258223a3SMatthew Dillon 	}
1236258223a3SMatthew Dillon 
1237cec85a37SMatthew Dillon 	/*
1238cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
1239cec85a37SMatthew Dillon 	 *
1240cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1241cec85a37SMatthew Dillon 	 * everything.
12421067474aSMatthew Dillon 	 *
12431067474aSMatthew Dillon 	 * NOTE!  This CCB is used for both the first and second commands.
12441067474aSMatthew Dillon 	 *	  The second command must use CCB slot 1 to properly load
12451067474aSMatthew Dillon 	 *	  the signature.
1246cec85a37SMatthew Dillon 	 */
1247258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
124812feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
124912feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
12501067474aSMatthew Dillon 	KKASSERT(ccb->ccb_slot == 1);
12511980eff3SMatthew Dillon 	ccb->ccb_xa.at = NULL;
1252258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1253258223a3SMatthew Dillon 
1254258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
1255cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
12561980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
12571980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1258258223a3SMatthew Dillon 
1259258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1260258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1261258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1262258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1263258223a3SMatthew Dillon 
1264258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
126512feb904SMatthew Dillon 
1266831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
12675f8c1efdSMatthew Dillon 		kprintf("%s: First FIS failed\n", PORTNAME(ap));
1268258223a3SMatthew Dillon 		goto err;
1269cec85a37SMatthew Dillon 	}
1270258223a3SMatthew Dillon 
1271cec85a37SMatthew Dillon 	/*
1272831bc9e3SMatthew Dillon 	 * WARNING!	TIME SENSITIVE SPACE!	WARNING!
1273831bc9e3SMatthew Dillon 	 *
1274831bc9e3SMatthew Dillon 	 * The two FISes are supposed to be back to back.  Don't issue other
1275831bc9e3SMatthew Dillon 	 * commands or even delay if we can help it.
12761980eff3SMatthew Dillon 	 */
12771980eff3SMatthew Dillon 
12781980eff3SMatthew Dillon 	/*
1279cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
1280cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
1281cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
1282cec85a37SMatthew Dillon 	 * from the AHCI document.
1283cec85a37SMatthew Dillon 	 *
1284cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1285cec85a37SMatthew Dillon 	 * everything.
1286cec85a37SMatthew Dillon 	 */
128712feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
128812feb904SMatthew Dillon 
1289cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
12901980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
12911980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_4BIT;
1292258223a3SMatthew Dillon 
1293258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1294258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1295258223a3SMatthew Dillon 
1296258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
1297831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
12985f8c1efdSMatthew Dillon 		kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1299258223a3SMatthew Dillon 		goto err;
1300cec85a37SMatthew Dillon 	}
1301258223a3SMatthew Dillon 
13021980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
13031980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1304c3783d8fSzrj 		kprintf("%s: device didn't come ready after reset, "
1305c3783d8fSzrj 			"TFD: 0x%pb%i\n", PORTNAME(ap),
1306c3783d8fSzrj 			AHCI_PFMT_TFD_STS, ahci_pread(ap, AHCI_PREG_TFD));
13073209f581SMatthew Dillon 		error = EBUSY;
1308258223a3SMatthew Dillon 		goto err;
1309258223a3SMatthew Dillon 	}
1310258223a3SMatthew Dillon 
1311fd8bd957SMatthew Dillon 	/*
1312fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
1313fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
1314fd8bd957SMatthew Dillon 	 *
1315fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
1316fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
1317fd8bd957SMatthew Dillon 	 * unexpectedly.
1318fd8bd957SMatthew Dillon 	 */
1319493d3201SMatthew Dillon 	ahci_os_sleep(100);
13201980eff3SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE) {
13211980eff3SMatthew Dillon 		ap->ap_type = ahci_port_signature_detect(ap, NULL);
1322fd8bd957SMatthew Dillon 	} else {
13231980eff3SMatthew Dillon 		if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
13241980eff3SMatthew Dillon 			kprintf("%s: device signature unexpectedly "
13251980eff3SMatthew Dillon 				"changed\n", PORTNAME(ap));
13263209f581SMatthew Dillon 			error = EBUSY; /* XXX */
1327fd8bd957SMatthew Dillon 		}
1328fd8bd957SMatthew Dillon 	}
13293209f581SMatthew Dillon 	error = 0;
13301980eff3SMatthew Dillon 
13313209f581SMatthew Dillon 	ahci_os_sleep(3);
1332258223a3SMatthew Dillon err:
1333258223a3SMatthew Dillon 	if (ccb != NULL) {
1334258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
13351980eff3SMatthew Dillon 
13361980eff3SMatthew Dillon 		/*
13371980eff3SMatthew Dillon 		 * If the target is busy use CLO to clear the busy
13381980eff3SMatthew Dillon 		 * condition.  The BSY should be cleared on the next
13391980eff3SMatthew Dillon 		 * start.
13401980eff3SMatthew Dillon 		 */
13411980eff3SMatthew Dillon 		if (ahci_pread(ap, AHCI_PREG_TFD) &
13421980eff3SMatthew Dillon 		    (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
13431980eff3SMatthew Dillon 			ahci_port_clo(ap);
13441980eff3SMatthew Dillon 		}
1345258223a3SMatthew Dillon 	}
1346258223a3SMatthew Dillon 
1347cf5f3a81SMatthew Dillon 	/*
1348cf5f3a81SMatthew Dillon 	 * If we failed to softreset make the port quiescent, otherwise
1349cf5f3a81SMatthew Dillon 	 * make sure the port's start/stop state matches what it was on
1350cf5f3a81SMatthew Dillon 	 * entry.
13511980eff3SMatthew Dillon 	 *
13521980eff3SMatthew Dillon 	 * Don't kill the port if the softreset is on a port multiplier
13531980eff3SMatthew Dillon 	 * target, that would kill all the targets!
1354cf5f3a81SMatthew Dillon 	 */
13553209f581SMatthew Dillon 	if (error) {
1356cf5f3a81SMatthew Dillon 		ahci_port_hardstop(ap);
13573209f581SMatthew Dillon 		/* ap_probe set to failed */
1358cf5f3a81SMatthew Dillon 	} else {
13593209f581SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_IDENT;
136012feb904SMatthew Dillon 		ap->ap_pmcount = 1;
13614c339a5fSMatthew Dillon 		ahci_port_start(ap);
1362cf5f3a81SMatthew Dillon 	}
13633209f581SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
1364258223a3SMatthew Dillon 	crit_exit();
1365258223a3SMatthew Dillon 
1366074579dfSMatthew Dillon 	if (bootverbose)
13671980eff3SMatthew Dillon 		kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
13681980eff3SMatthew Dillon 
13693209f581SMatthew Dillon 	return (error);
1370258223a3SMatthew Dillon }
1371258223a3SMatthew Dillon 
1372fd8bd957SMatthew Dillon /*
1373493d3201SMatthew Dillon  * Issue just do the core COMRESET and basic device detection on a port.
1374fd8bd957SMatthew Dillon  *
1375493d3201SMatthew Dillon  * NOTE: Only called by ahci_port_hardreset().
1376fd8bd957SMatthew Dillon  */
13778119d5f5SMatthew Dillon int
1378493d3201SMatthew Dillon ahci_comreset(struct ahci_port *ap, int *pmdetectp)
1379258223a3SMatthew Dillon {
1380493d3201SMatthew Dillon 	u_int32_t cmd;
1381493d3201SMatthew Dillon 	u_int32_t r;
13823209f581SMatthew Dillon 	int error;
13831980eff3SMatthew Dillon 	int loop;
1384f2dba700SMatthew Dillon 	int retries = 0;
1385258223a3SMatthew Dillon 
1386cf5f3a81SMatthew Dillon 	/*
1387eb9f4c83SMatthew Dillon 	 * Idle the port.  We must cycle FRE for certain chips that silently
1388eb9f4c83SMatthew Dillon 	 * clear FR on disconnect.  Normally we do not want to cycle FRE
1389eb9f4c83SMatthew Dillon 	 * because other chipsets might react badly to that.
13901980eff3SMatthew Dillon 	 */
1391493d3201SMatthew Dillon 	*pmdetectp = 0;
1392eb9f4c83SMatthew Dillon 	if (ap->ap_sc->sc_flags & AHCI_F_CYCLE_FR)
1393eb9f4c83SMatthew Dillon 		ahci_port_stop(ap, 1);
1394eb9f4c83SMatthew Dillon 	else
13951980eff3SMatthew Dillon 		ahci_port_stop(ap, 0);
13961980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
1397493d3201SMatthew Dillon 	ahci_os_sleep(10);
13981980eff3SMatthew Dillon 
13991980eff3SMatthew Dillon 	/*
14008119d5f5SMatthew Dillon 	 * FIS-based switching must be turned off when doing a hardware
14018119d5f5SMatthew Dillon 	 * reset, and will be turned on again during the PM probe.
14028119d5f5SMatthew Dillon 	 */
14038119d5f5SMatthew Dillon 	if (ap->ap_flags & AP_F_FBSS_ENABLED) {
14048119d5f5SMatthew Dillon 		ap->ap_flags &= ~AP_F_FBSS_ENABLED;
14058119d5f5SMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_FBS);
14068119d5f5SMatthew Dillon 		cmd &= ~AHCI_PREG_FBS_EN;
14078119d5f5SMatthew Dillon 		cmd |= AHCI_PREG_FBS_DEC;
14088119d5f5SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_FBS, cmd);
14098119d5f5SMatthew Dillon 	}
14108119d5f5SMatthew Dillon 
14118119d5f5SMatthew Dillon 	/*
14121980eff3SMatthew Dillon 	 * The port may have been quiescent with its SUD bit cleared, so
1413eb9f4c83SMatthew Dillon 	 * set the SUD (spin up device).  Also POD (Power up device),
1414eb9f4c83SMatthew Dillon 	 * and issue an ICC_ACTIVE request to bring up communications.
1415493d3201SMatthew Dillon 	 *
1416493d3201SMatthew Dillon 	 * NOTE: I do not know if SUD is a hardware pin/low-level signal
1417493d3201SMatthew Dillon 	 *	 or if it is messaged.
1418cf5f3a81SMatthew Dillon 	 */
1419eb9f4c83SMatthew Dillon 	r = ap->ap_sc->sc_ipm_disable;
1420eb9f4c83SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1421493d3201SMatthew Dillon 
1422eb9f4c83SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1423493d3201SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD | AHCI_PREG_CMD_POD;
1424eb9f4c83SMatthew Dillon 	cmd |= AHCI_PREG_CMD_ICC_ACTIVE;
1425cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1426eb9f4c83SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
1427eb9f4c83SMatthew Dillon 
1428eb9f4c83SMatthew Dillon 	/*
1429eb9f4c83SMatthew Dillon 	 * Some parts need FIS reception enabled to be able to COMINIT at
1430eb9f4c83SMatthew Dillon 	 * all, so we can't delay FRE until port-start.  Even though that
1431eb9f4c83SMatthew Dillon 	 * isn't what the spec says.
1432eb9f4c83SMatthew Dillon 	 *
1433eb9f4c83SMatthew Dillon 	 * This is typically the first enablement of FRE, but in most cases
1434eb9f4c83SMatthew Dillon 	 * we never turn it off making this a NOP for later calls.
1435eb9f4c83SMatthew Dillon 	 */
1436eb9f4c83SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE;
1437eb9f4c83SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1438eb9f4c83SMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1439eb9f4c83SMatthew Dillon 		ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1440258223a3SMatthew Dillon 
14411980eff3SMatthew Dillon 	/*
1442493d3201SMatthew Dillon 	 * Make sure that all power management is disabled.
14431067474aSMatthew Dillon 	 *
1444493d3201SMatthew Dillon 	 * NOTE!  AHCI_PREG_SCTL_DET_DISABLE seems to be highly unreliable
14454e21f4daSMatthew Dillon 	 *	  on multiple chipsets and can brick the chipset or even
14464e21f4daSMatthew Dillon 	 *	  the whole PC.  Never use it.
14471980eff3SMatthew Dillon 	 */
14481980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1449258223a3SMatthew Dillon 
1450f2dba700SMatthew Dillon retry:
1451f2dba700SMatthew Dillon 	/*
1452f2dba700SMatthew Dillon 	 * Give the new power management state time to settle, then clear
1453f2dba700SMatthew Dillon 	 * pending status.
1454f2dba700SMatthew Dillon 	 */
1455f2dba700SMatthew Dillon 	ahci_os_sleep(1000);
1456f2dba700SMatthew Dillon 	ahci_flush_tfd(ap);
1457f2dba700SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
14581980eff3SMatthew Dillon 
14591980eff3SMatthew Dillon 	/*
1460493d3201SMatthew Dillon 	 * Start transmitting COMRESET.  The spec says that COMRESET must
1461493d3201SMatthew Dillon 	 * be sent for at least 1ms but in actual fact numerous devices
1462493d3201SMatthew Dillon 	 * appear to take much longer.  Delay a whole second here.
1463493d3201SMatthew Dillon 	 *
1464493d3201SMatthew Dillon 	 * In addition, SATA-3 ports can take longer to train, so even
1465493d3201SMatthew Dillon 	 * SATA-2 devices which would normally detect very quickly may
1466493d3201SMatthew Dillon 	 * take longer when plugged into a SATA-3 port.
14671980eff3SMatthew Dillon 	 */
1468493d3201SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_INIT;
146931075e01SMatthew Dillon 
14708986d351SMatthew Dillon 	switch(AhciForceGen) {
14718986d351SMatthew Dillon 	case 0:
1472258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
14738986d351SMatthew Dillon 		break;
14748986d351SMatthew Dillon 	case 1:
14758986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
14768986d351SMatthew Dillon 		break;
14778986d351SMatthew Dillon 	case 2:
14788986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN2;
14798986d351SMatthew Dillon 		break;
14808986d351SMatthew Dillon 	case 3:
14818986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN3;
14828986d351SMatthew Dillon 		break;
14838986d351SMatthew Dillon 	default:
14848986d351SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN3;
14858986d351SMatthew Dillon 		break;
14868986d351SMatthew Dillon 	}
1487258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1488492bffafSMatthew Dillon 	ahci_os_sleep(1000);
1489493d3201SMatthew Dillon 
1490492bffafSMatthew Dillon 	ap->ap_flags &= ~AP_F_HARSH_REINIT;
1491cf5f3a81SMatthew Dillon 
1492cf5f3a81SMatthew Dillon 	/*
1493cf5f3a81SMatthew Dillon 	 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1494cf5f3a81SMatthew Dillon 	 * since we are hard-resetting the port we might as well clear
1495f2dba700SMatthew Dillon 	 * the whole enchillada.  Also be sure to clear any spurious BSY
1496f2dba700SMatthew Dillon 	 * prior to clearing INIT.
1497493d3201SMatthew Dillon 	 *
1498493d3201SMatthew Dillon 	 * Wait 1 whole second after clearing INIT before checking
1499493d3201SMatthew Dillon 	 * the device detection bits in an attempt to work around chipsets
1500493d3201SMatthew Dillon 	 * which do not properly mask PCS/PRCS during low level init.
1501cf5f3a81SMatthew Dillon 	 */
1502cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1503cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1504f2dba700SMatthew Dillon /*	ahci_port_clo(ap);*/
1505493d3201SMatthew Dillon 	ahci_os_sleep(10);
1506493d3201SMatthew Dillon 
1507f2dba700SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_SPD;
1508258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
1509258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
1510258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1511493d3201SMatthew Dillon 	ahci_os_sleep(1000);
1512258223a3SMatthew Dillon 
15131980eff3SMatthew Dillon 	/*
151446d04d11SMatthew Dillon 	 * Try to determine if there is a device on the port.  This operation
151546d04d11SMatthew Dillon 	 * typically runs in parallel on all ports belonging to an AHCI
151646d04d11SMatthew Dillon 	 * controller.
15171980eff3SMatthew Dillon 	 *
151846d04d11SMatthew Dillon 	 * 3/10 of a second (loop = 300) is plenty for directly attached
151946d04d11SMatthew Dillon 	 * devices, but not enough for some port multipliers, particularly
152046d04d11SMatthew Dillon 	 * if powered-on cold.  Since this operation runs in parallel,
152146d04d11SMatthew Dillon 	 * give us 2 seconds to detect.
152246d04d11SMatthew Dillon 	 *
152346d04d11SMatthew Dillon 	 * NOTE: The 10-second hot-swap delay prior to the COMRESET is not
152446d04d11SMatthew Dillon 	 *	 sufficient, since the first COMRESET after a cold power-on
152546d04d11SMatthew Dillon 	 *	 of a port-multiplier can take extra time.
152646d04d11SMatthew Dillon 	 *
15271980eff3SMatthew Dillon 	 * If we fail clear PRCS (phy detect) since we may cycled
15281980eff3SMatthew Dillon 	 * the phy and probably caused another PRCS interrupt.
15291980eff3SMatthew Dillon 	 */
153046d04d11SMatthew Dillon 	loop = 2000;
153176497a9cSMatthew Dillon 	while (loop > 0) {
15321980eff3SMatthew Dillon 		r = ahci_pread(ap, AHCI_PREG_SSTS);
15331980eff3SMatthew Dillon 		if (r & AHCI_PREG_SSTS_DET)
15341980eff3SMatthew Dillon 			break;
153576497a9cSMatthew Dillon 		loop -= ahci_os_softsleep();
15361980eff3SMatthew Dillon 	}
15371980eff3SMatthew Dillon 	if (loop == 0) {
15381980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1539074579dfSMatthew Dillon 		if (bootverbose) {
15401980eff3SMatthew Dillon 			kprintf("%s: Port appears to be unplugged\n",
15411980eff3SMatthew Dillon 				PORTNAME(ap));
1542074579dfSMatthew Dillon 		}
15433209f581SMatthew Dillon 		error = ENODEV;
154412feb904SMatthew Dillon 		goto done;
1545258223a3SMatthew Dillon 	}
1546258223a3SMatthew Dillon 
1547cec85a37SMatthew Dillon 	/*
1548493d3201SMatthew Dillon 	 * There is something on the port.  Regardless of what happens
1549493d3201SMatthew Dillon 	 * after this tell the caller to try to detect a port multiplier.
1550493d3201SMatthew Dillon 	 *
1551493d3201SMatthew Dillon 	 * Give the device 3 seconds to fully negotiate.
15521980eff3SMatthew Dillon 	 */
1553493d3201SMatthew Dillon 	*pmdetectp = 1;
1554493d3201SMatthew Dillon 
155512feb904SMatthew Dillon 	if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
15561980eff3SMatthew Dillon 			  AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1557074579dfSMatthew Dillon 		if (bootverbose) {
15581980eff3SMatthew Dillon 			kprintf("%s: Device may be powered down\n",
15591980eff3SMatthew Dillon 				PORTNAME(ap));
1560074579dfSMatthew Dillon 		}
15613209f581SMatthew Dillon 		error = ENODEV;
1562493d3201SMatthew Dillon 		goto done;
15631980eff3SMatthew Dillon 	}
15641980eff3SMatthew Dillon 
156512feb904SMatthew Dillon 	/*
156612feb904SMatthew Dillon 	 * We got something that definitely looks like a device.  Give
156712feb904SMatthew Dillon 	 * the device time to send us its first D2H FIS.  Waiting for
156812feb904SMatthew Dillon 	 * BSY to clear accomplishes this.
156912feb904SMatthew Dillon 	 *
157046d04d11SMatthew Dillon 	 * The target device might be hung in a BSY state depending on
157146d04d11SMatthew Dillon 	 * the order things are power cycled.  We want to retry the COMRESET
157246d04d11SMatthew Dillon 	 * at least once if we find the device BSY for reliable operation.
157346d04d11SMatthew Dillon 	 *
1574493d3201SMatthew Dillon 	 * NOTE: A port multiplier may or may not clear BSY here,
157546d04d11SMatthew Dillon 	 *	 particularly if it was previously configured and now
157646d04d11SMatthew Dillon 	 *	 its cable has been unplugged and plugged back in,
157746d04d11SMatthew Dillon 	 *	 and also depending on what is sitting in target 0 behind it.
1578f2dba700SMatthew Dillon 	 *
1579f2dba700SMatthew Dillon 	 * NOTE: Intel SSDs seem to have compatibility problems with Intel
1580f2dba700SMatthew Dillon 	 *	 mobo's on cold boots and may leave BSY set.  A single
1581f2dba700SMatthew Dillon 	 *	 retry works around the problem.  This is definitely a bug
1582f2dba700SMatthew Dillon 	 *	 with the mobo and/or the SSD and does not appear to occur
1583f2dba700SMatthew Dillon 	 *	 with other devices connected to the same port.
158412feb904SMatthew Dillon 	 */
1585c408a8b3SMatthew Dillon 	ahci_flush_tfd(ap);
1586f2dba700SMatthew Dillon 	if (ahci_pwait_clr_to(ap, 8000, AHCI_PREG_TFD,
15871980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1588c3783d8fSzrj 		kprintf("%s: Device BUSY: %pb%i\n", PORTNAME(ap),
1589c3783d8fSzrj 			AHCI_PFMT_TFD_STS, ahci_pread(ap, AHCI_PREG_TFD));
1590f2dba700SMatthew Dillon 		if (retries == 0) {
1591f2dba700SMatthew Dillon 			kprintf("%s: Retrying\n", PORTNAME(ap));
1592f2dba700SMatthew Dillon 			retries = 1;
1593f2dba700SMatthew Dillon 			goto retry;
1594f2dba700SMatthew Dillon 		}
159512feb904SMatthew Dillon 		error = EBUSY;
15961980eff3SMatthew Dillon 	} else {
159746d04d11SMatthew Dillon 		if (retries)
159846d04d11SMatthew Dillon 			kprintf("%s: Device Unbusied after retry\n",
159946d04d11SMatthew Dillon 				PORTNAME(ap));
16003209f581SMatthew Dillon 		error = 0;
16011980eff3SMatthew Dillon 	}
1602258223a3SMatthew Dillon 
160312feb904SMatthew Dillon done:
1604493d3201SMatthew Dillon 	ahci_flush_tfd(ap);
1605493d3201SMatthew Dillon 	return error;
1606493d3201SMatthew Dillon }
1607493d3201SMatthew Dillon 
1608493d3201SMatthew Dillon 
1609493d3201SMatthew Dillon /*
1610493d3201SMatthew Dillon  * AHCI port reset, Section 10.4.2
1611493d3201SMatthew Dillon  *
1612493d3201SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
1613493d3201SMatthew Dillon  * connected to the port could still end-up hung.
1614493d3201SMatthew Dillon  */
1615493d3201SMatthew Dillon int
1616493d3201SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard)
1617493d3201SMatthew Dillon {
1618493d3201SMatthew Dillon 	u_int32_t data;
1619493d3201SMatthew Dillon 	int	error;
1620493d3201SMatthew Dillon 	int	pmdetect;
1621493d3201SMatthew Dillon 
1622493d3201SMatthew Dillon 	if (bootverbose)
1623493d3201SMatthew Dillon 		kprintf("%s: START HARDRESET\n", PORTNAME(ap));
1624493d3201SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
1625493d3201SMatthew Dillon 
1626493d3201SMatthew Dillon 	error = ahci_comreset(ap, &pmdetect);
1627493d3201SMatthew Dillon 
1628493d3201SMatthew Dillon 	/*
1629493d3201SMatthew Dillon 	 * We may be asked to perform a port multiplier check even if the
1630493d3201SMatthew Dillon 	 * comreset failed.  This typically occurs when the PM has nothing
1631493d3201SMatthew Dillon 	 * in slot 0, which can cause BSY to remain set.
1632493d3201SMatthew Dillon 	 *
1633493d3201SMatthew Dillon 	 * If the PM detection is successful it will override (error),
1634493d3201SMatthew Dillon 	 * otherwise (error) is retained.  If an error does occur it
1635493d3201SMatthew Dillon 	 * is possible that a normal device has blown up on us DUE to
1636493d3201SMatthew Dillon 	 * the PM detection code, so re-run the comreset and assume
1637493d3201SMatthew Dillon 	 * a normal device.
1638493d3201SMatthew Dillon 	 */
1639493d3201SMatthew Dillon 	if (pmdetect) {
1640493d3201SMatthew Dillon 		if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) {
1641493d3201SMatthew Dillon 			error = ahci_pm_port_probe(ap, error);
1642493d3201SMatthew Dillon 			if (error) {
1643493d3201SMatthew Dillon 				error = ahci_comreset(ap, &pmdetect);
1644493d3201SMatthew Dillon 			}
1645493d3201SMatthew Dillon 		}
1646493d3201SMatthew Dillon 	}
1647493d3201SMatthew Dillon 
164812feb904SMatthew Dillon 	/*
164912feb904SMatthew Dillon 	 * Finish up.
165012feb904SMatthew Dillon 	 */
1651493d3201SMatthew Dillon 	ahci_os_sleep(500);
1652493d3201SMatthew Dillon 
165312feb904SMatthew Dillon 	switch(error) {
165412feb904SMatthew Dillon 	case 0:
165512feb904SMatthew Dillon 		/*
165612feb904SMatthew Dillon 		 * All good, make sure the port is running and set the
165712feb904SMatthew Dillon 		 * probe state.  Ignore the signature junk (it's unreliable)
165812feb904SMatthew Dillon 		 * until we get to the softreset code.
165912feb904SMatthew Dillon 		 */
166012feb904SMatthew Dillon 		if (ahci_port_start(ap)) {
166112feb904SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
166212feb904SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
166312feb904SMatthew Dillon 			error = EBUSY;
1664493d3201SMatthew Dillon 			break;
166512feb904SMatthew Dillon 		}
1666f4553de1SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
1667f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_GOOD;
1668f4553de1SMatthew Dillon 		else
1669f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
167012feb904SMatthew Dillon 		break;
167112feb904SMatthew Dillon 	case ENODEV:
1672fd8bd957SMatthew Dillon 		/*
167312feb904SMatthew Dillon 		 * Normal device probe failure
16741980eff3SMatthew Dillon 		 */
167512feb904SMatthew Dillon 		data = ahci_pread(ap, AHCI_PREG_SSTS);
16761980eff3SMatthew Dillon 
167712feb904SMatthew Dillon 		switch(data & AHCI_PREG_SSTS_DET) {
167812feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
167912feb904SMatthew Dillon 			kprintf("%s: Device not communicating\n",
16801980eff3SMatthew Dillon 				PORTNAME(ap));
168112feb904SMatthew Dillon 			break;
168212feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
168312feb904SMatthew Dillon 			kprintf("%s: PHY offline\n",
168412feb904SMatthew Dillon 				PORTNAME(ap));
168512feb904SMatthew Dillon 			break;
168612feb904SMatthew Dillon 		default:
168712feb904SMatthew Dillon 			kprintf("%s: No device detected\n",
168812feb904SMatthew Dillon 				PORTNAME(ap));
168912feb904SMatthew Dillon 			break;
16901980eff3SMatthew Dillon 		}
169112feb904SMatthew Dillon 		ahci_port_hardstop(ap);
169212feb904SMatthew Dillon 		break;
169312feb904SMatthew Dillon 	default:
16941980eff3SMatthew Dillon 		/*
169512feb904SMatthew Dillon 		 * Abnormal probe (EBUSY)
16961980eff3SMatthew Dillon 		 */
169712feb904SMatthew Dillon 		kprintf("%s: Device on port is bricked\n",
169812feb904SMatthew Dillon 			PORTNAME(ap));
169912feb904SMatthew Dillon 		ahci_port_hardstop(ap);
170012feb904SMatthew Dillon #if 0
170112feb904SMatthew Dillon 		rc = ahci_port_reset(ap, atx, 0);
170212feb904SMatthew Dillon 		if (rc) {
170312feb904SMatthew Dillon 			kprintf("%s: Unable unbrick device\n",
170412feb904SMatthew Dillon 				PORTNAME(ap));
17051980eff3SMatthew Dillon 		} else {
170612feb904SMatthew Dillon 			kprintf("%s: Successfully unbricked\n",
17073209f581SMatthew Dillon 				PORTNAME(ap));
170812feb904SMatthew Dillon 		}
170912feb904SMatthew Dillon #endif
171012feb904SMatthew Dillon 		break;
17113209f581SMatthew Dillon 	}
17121067474aSMatthew Dillon 
17131067474aSMatthew Dillon 	/*
171412feb904SMatthew Dillon 	 * Clean up
17151067474aSMatthew Dillon 	 */
171612feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
171712feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
17183209f581SMatthew Dillon 
171912feb904SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
17201980eff3SMatthew Dillon 
172112feb904SMatthew Dillon 	if (bootverbose)
172212feb904SMatthew Dillon 		kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1723831bc9e3SMatthew Dillon 	return (error);
17241980eff3SMatthew Dillon }
17251980eff3SMatthew Dillon 
17261980eff3SMatthew Dillon /*
1727cf5f3a81SMatthew Dillon  * Hard-stop on hot-swap device removal.  See 10.10.1
1728cf5f3a81SMatthew Dillon  *
1729cf5f3a81SMatthew Dillon  * Place the port in a mode that will allow it to detect hot-swap insertions.
1730cf5f3a81SMatthew Dillon  * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1731cf5f3a81SMatthew Dillon  * seem to do the job.
1732f17a0cedSMatthew Dillon  *
1733f17a0cedSMatthew Dillon  * FIS reception is left enabled but command processing is disabled.
1734f17a0cedSMatthew Dillon  * Cycling FIS reception (FRE) can brick ports.
1735cf5f3a81SMatthew Dillon  */
1736cf5f3a81SMatthew Dillon void
1737cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap)
1738cf5f3a81SMatthew Dillon {
173976497a9cSMatthew Dillon 	struct ahci_ccb *ccb;
17401980eff3SMatthew Dillon 	struct ata_port *at;
1741cf5f3a81SMatthew Dillon 	u_int32_t r;
1742cf5f3a81SMatthew Dillon 	u_int32_t cmd;
174376497a9cSMatthew Dillon 	int slot;
17441980eff3SMatthew Dillon 	int i;
1745bb79834dSMatthew Dillon 	int serial;
1746cf5f3a81SMatthew Dillon 
1747cf5f3a81SMatthew Dillon 	/*
1748cf5f3a81SMatthew Dillon 	 * Stop the port.  We can't modify things like SUD if the port
1749cf5f3a81SMatthew Dillon 	 * is running.
1750cf5f3a81SMatthew Dillon 	 */
1751cf5f3a81SMatthew Dillon 	ap->ap_state = AP_S_FATAL_ERROR;
17521980eff3SMatthew Dillon 	ap->ap_probe = ATA_PROBE_FAILED;
17531980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1754cf5f3a81SMatthew Dillon 	ahci_port_stop(ap, 0);
1755cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
1756492bffafSMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA | AHCI_PREG_CMD_ICC);
1757492bffafSMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1758cf5f3a81SMatthew Dillon 
1759cf5f3a81SMatthew Dillon 	/*
17601980eff3SMatthew Dillon 	 * Clean up AT sub-ports on SATA port.
17611980eff3SMatthew Dillon 	 */
17621980eff3SMatthew Dillon 	for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1763b012a2caSMatthew Dillon 		at = ap->ap_ata[i];
17641980eff3SMatthew Dillon 		at->at_type = ATA_PORT_T_NONE;
17653209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_FAILED;
17661980eff3SMatthew Dillon 	}
17671980eff3SMatthew Dillon 
17681980eff3SMatthew Dillon 	/*
1769cf5f3a81SMatthew Dillon 	 * 10.10.1 place us in the Listen state.
1770cf5f3a81SMatthew Dillon 	 *
17715502cf24SMatthew Dillon 	 * 10.10.3 DET must be set to 0 and found to be 0 before
17725502cf24SMatthew Dillon 	 * setting SUD to 0.
17735502cf24SMatthew Dillon 	 *
17745502cf24SMatthew Dillon 	 * Deactivating SUD only applies if the controller supports SUD, it
17755502cf24SMatthew Dillon 	 * is a bit unclear what happens w/regards to detecting hotplug
17765502cf24SMatthew Dillon 	 * if it doesn't.
1777eb9f4c83SMatthew Dillon 	 *
1778eb9f4c83SMatthew Dillon 	 * NOTE: AHCI_PREG_SCTL_SPM_* bits are not implemented by the spec
1779eb9f4c83SMatthew Dillon 	 *	 and must be zero.
1780cf5f3a81SMatthew Dillon 	 */
1781eb9f4c83SMatthew Dillon 	r = ap->ap_sc->sc_ipm_disable;
17825502cf24SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
17835502cf24SMatthew Dillon 	ahci_os_sleep(10);
1784eb9f4c83SMatthew Dillon 
1785eb9f4c83SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
1786cf5f3a81SMatthew Dillon 	cmd &= ~AHCI_PREG_CMD_SUD;
1787cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
17885502cf24SMatthew Dillon 	ahci_os_sleep(10);
1789cf5f3a81SMatthew Dillon 
1790cf5f3a81SMatthew Dillon 	/*
17915502cf24SMatthew Dillon 	 * 10.10.1
17925502cf24SMatthew Dillon 	 *
17935502cf24SMatthew Dillon 	 * Transition su to the spin-up state.  HBA shall send COMRESET and
17945502cf24SMatthew Dillon 	 * begin initialization sequence (whatever that means).  Presumably
17955502cf24SMatthew Dillon 	 * this is edge-triggered.  Following the spin-up state the HBA
17965502cf24SMatthew Dillon 	 * will automatically transition to the Normal state.
1797cf5f3a81SMatthew Dillon 	 *
1798cf5f3a81SMatthew Dillon 	 * This only applies if the controller supports SUD.
17994e21f4daSMatthew Dillon 	 * NEVER use AHCI_PREG_DET_DISABLE.
1800cf5f3a81SMatthew Dillon 	 */
18015502cf24SMatthew Dillon 	cmd |= AHCI_PREG_CMD_POD |
18025502cf24SMatthew Dillon 	       AHCI_PREG_CMD_SUD |
18035502cf24SMatthew Dillon 	       AHCI_PREG_CMD_ICC_ACTIVE;
1804cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1805eb9f4c83SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
1806cf5f3a81SMatthew Dillon 
1807cf5f3a81SMatthew Dillon 	/*
1808cf5f3a81SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
1809cf5f3a81SMatthew Dillon 	 */
1810cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1811cf5f3a81SMatthew Dillon 
1812cf5f3a81SMatthew Dillon 	/*
181376497a9cSMatthew Dillon 	 * Clean out pending ccbs
181476497a9cSMatthew Dillon 	 */
1815bb79834dSMatthew Dillon restart:
181676497a9cSMatthew Dillon 	while (ap->ap_active) {
181776497a9cSMatthew Dillon 		slot = ffs(ap->ap_active) - 1;
181876497a9cSMatthew Dillon 		ap->ap_active &= ~(1 << slot);
181976497a9cSMatthew Dillon 		--ap->ap_active_cnt;
182076497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
182176497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1822bb79834dSMatthew Dillon 			serial = ccb->ccb_xa.serial;
1823eb67213aSMatthew Dillon 			callout_cancel(&ccb->ccb_timeout);
1824bb79834dSMatthew Dillon 			if (serial != ccb->ccb_xa.serial) {
1825bb79834dSMatthew Dillon 				kprintf("%s: Warning: timeout race ccb %p\n",
1826bb79834dSMatthew Dillon 					PORTNAME(ap), ccb);
1827bb79834dSMatthew Dillon 				goto restart;
1828bb79834dSMatthew Dillon 			}
182976497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
183076497a9cSMatthew Dillon 		}
183146528d33SMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
183276497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
183376497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
183476497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
183576497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
183676497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
183776497a9cSMatthew Dillon 	}
183876497a9cSMatthew Dillon 	while (ap->ap_sactive) {
183976497a9cSMatthew Dillon 		slot = ffs(ap->ap_sactive) - 1;
184076497a9cSMatthew Dillon 		ap->ap_sactive &= ~(1 << slot);
184176497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
184276497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
1843bb79834dSMatthew Dillon 			serial = ccb->ccb_xa.serial;
1844eb67213aSMatthew Dillon 			callout_cancel(&ccb->ccb_timeout);
1845bb79834dSMatthew Dillon 			if (serial != ccb->ccb_xa.serial) {
1846bb79834dSMatthew Dillon 				kprintf("%s: Warning: timeout race ccb %p\n",
1847bb79834dSMatthew Dillon 					PORTNAME(ap), ccb);
1848bb79834dSMatthew Dillon 				goto restart;
1849bb79834dSMatthew Dillon 			}
185076497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
185176497a9cSMatthew Dillon 		}
185246528d33SMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
185376497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
185476497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
185576497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
185676497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
185776497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
185876497a9cSMatthew Dillon 	}
185976497a9cSMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
186076497a9cSMatthew Dillon 
186176497a9cSMatthew Dillon 	while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
186276497a9cSMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
186376497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
186476497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
186576497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
186676497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
186776497a9cSMatthew Dillon 	}
186876497a9cSMatthew Dillon 
186976497a9cSMatthew Dillon 	/*
18705502cf24SMatthew Dillon 	 * Hot-plug device detection should work at this point.  e.g. on
18715502cf24SMatthew Dillon 	 * AMD chipsets Spin-Up/Normal state is sufficient for hot-plug
18725502cf24SMatthew Dillon 	 * detection and entering RESET (continuous COMRESET by setting INIT)
18735502cf24SMatthew Dillon 	 * will actually prevent hot-plug detection from working properly.
1874cf5f3a81SMatthew Dillon 	 *
18755502cf24SMatthew Dillon 	 * There may be cases where this will fail to work, I have some
18765502cf24SMatthew Dillon 	 * additional code to place the HBA in RESET (send continuous
18775502cf24SMatthew Dillon 	 * COMRESET) and hopefully get DIAG.X or other events when something
18785502cf24SMatthew Dillon 	 * is plugged in.  Unfortunately this isn't universal and can
18795502cf24SMatthew Dillon 	 * also prevent events from generating interrupts.
1880cf5f3a81SMatthew Dillon 	 */
18815502cf24SMatthew Dillon 
18825502cf24SMatthew Dillon #if 0
18835502cf24SMatthew Dillon 	/*
18845502cf24SMatthew Dillon 	 * Transition us to the Reset state.  Theoretically we send a
18855502cf24SMatthew Dillon 	 * continuous stream of COMRESETs in this state.
18865502cf24SMatthew Dillon 	 */
18875502cf24SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_INIT;
18885502cf24SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
18895502cf24SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
18905502cf24SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
18915502cf24SMatthew Dillon 	} else {
18925502cf24SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
18935502cf24SMatthew Dillon 	}
18945502cf24SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
18955502cf24SMatthew Dillon 	ahci_os_sleep(10);
18965502cf24SMatthew Dillon 
18975502cf24SMatthew Dillon 	/*
18985502cf24SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
18995502cf24SMatthew Dillon 	 */
19005502cf24SMatthew Dillon 	ahci_flush_tfd(ap);
19015502cf24SMatthew Dillon #endif
1902cf5f3a81SMatthew Dillon 	/* NOP */
1903cf5f3a81SMatthew Dillon }
1904cf5f3a81SMatthew Dillon 
1905cf5f3a81SMatthew Dillon /*
1906c408a8b3SMatthew Dillon  * We can't loop on the X bit, a continuous COMINIT received will make
1907c408a8b3SMatthew Dillon  * it loop forever.  Just assume one event has built up and clear X
1908c408a8b3SMatthew Dillon  * so the task file descriptor can update.
1909cf5f3a81SMatthew Dillon  */
1910cf5f3a81SMatthew Dillon void
1911cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap)
1912cf5f3a81SMatthew Dillon {
1913cf5f3a81SMatthew Dillon 	u_int32_t r;
1914cf5f3a81SMatthew Dillon 
1915cf5f3a81SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SERR);
1916c408a8b3SMatthew Dillon 	if (r & AHCI_PREG_SERR_DIAG_X)
19171980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1918cf5f3a81SMatthew Dillon }
1919cf5f3a81SMatthew Dillon 
1920cf5f3a81SMatthew Dillon /*
1921fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
1922fd8bd957SMatthew Dillon  * DISK.
1923fd8bd957SMatthew Dillon  */
1924fd8bd957SMatthew Dillon int
19251980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1926fd8bd957SMatthew Dillon {
1927fd8bd957SMatthew Dillon 	u_int32_t sig;
1928fd8bd957SMatthew Dillon 
1929fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
1930074579dfSMatthew Dillon 	if (bootverbose)
193146d04d11SMatthew Dillon 		kprintf("%s: SIG %08x\n", ATANAME(ap, at), sig);
1932fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1933fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
19341980eff3SMatthew Dillon 	} else if ((sig & 0xffff0000) ==
19351980eff3SMatthew Dillon 		 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
19361980eff3SMatthew Dillon 		return(ATA_PORT_T_PM);
1937fd8bd957SMatthew Dillon 	} else {
1938fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
1939fd8bd957SMatthew Dillon 	}
1940fd8bd957SMatthew Dillon }
1941fd8bd957SMatthew Dillon 
1942fd8bd957SMatthew Dillon /*
1943fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
1944fd8bd957SMatthew Dillon  */
1945258223a3SMatthew Dillon int
1946258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
1947258223a3SMatthew Dillon {
1948258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1949258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1950258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1951258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
1952258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1953258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
1954258223a3SMatthew Dillon 	int				error;
1955258223a3SMatthew Dillon 
1956258223a3SMatthew Dillon 	if (xa->datalen == 0) {
1957258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
1958258223a3SMatthew Dillon 		return (0);
1959258223a3SMatthew Dillon 	}
1960258223a3SMatthew Dillon 
1961258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
1962258223a3SMatthew Dillon 				xa->data, xa->datalen,
1963258223a3SMatthew Dillon 				ahci_load_prdt_callback,
1964258223a3SMatthew Dillon 				&prdt,
1965258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
1966258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1967258223a3SMatthew Dillon 	if (error != 0) {
1968258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1969258223a3SMatthew Dillon 		return (1);
1970258223a3SMatthew Dillon 	}
197112feb904SMatthew Dillon #if 0
1972258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
1973258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
197412feb904SMatthew Dillon #endif
1975258223a3SMatthew Dillon 
1976258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1977258223a3SMatthew Dillon 
1978b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_READ)
1979b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1980b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
1981b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1982258223a3SMatthew Dillon 
1983258223a3SMatthew Dillon 	return (0);
1984258223a3SMatthew Dillon }
1985258223a3SMatthew Dillon 
1986258223a3SMatthew Dillon /*
1987258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
1988258223a3SMatthew Dillon  * list is a temporary structure.
1989258223a3SMatthew Dillon  */
1990258223a3SMatthew Dillon static
1991258223a3SMatthew Dillon void
1992258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1993258223a3SMatthew Dillon 			int error)
1994258223a3SMatthew Dillon {
1995258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
1996258223a3SMatthew Dillon 	u_int64_t addr;
1997258223a3SMatthew Dillon 
1998258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
1999258223a3SMatthew Dillon 
2000258223a3SMatthew Dillon 	while (nsegs) {
2001258223a3SMatthew Dillon 		addr = segs->ds_addr;
2002258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
2003258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
2004258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
2005258223a3SMatthew Dillon 		--nsegs;
2006258223a3SMatthew Dillon 		if (nsegs)
2007258223a3SMatthew Dillon 			++prd;
2008258223a3SMatthew Dillon 		++segs;
2009258223a3SMatthew Dillon 	}
2010258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
2011258223a3SMatthew Dillon }
2012258223a3SMatthew Dillon 
2013258223a3SMatthew Dillon void
2014258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
2015258223a3SMatthew Dillon {
2016258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
2017258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
2018258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
2019258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
2020258223a3SMatthew Dillon 
2021258223a3SMatthew Dillon 	if (xa->datalen != 0) {
2022b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_READ) {
2023258223a3SMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
2024b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTREAD);
2025b012a2caSMatthew Dillon 		}
2026b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_WRITE) {
2027b012a2caSMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
2028b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTWRITE);
2029b012a2caSMatthew Dillon 		}
2030258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
2031258223a3SMatthew Dillon 
2032f7d09f74SMatthew Dillon 		/*
2033f7d09f74SMatthew Dillon 		 * prdbc is only updated by hardware for non-NCQ commands.
2034f7d09f74SMatthew Dillon 		 */
2035f7d09f74SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
2036f7d09f74SMatthew Dillon 			xa->resid = 0;
2037f7d09f74SMatthew Dillon 		} else {
203850a3ecb6SMatthew Dillon 			if (ccb->ccb_cmd_hdr->prdbc == 0 &&
203950a3ecb6SMatthew Dillon 			    ccb->ccb_xa.state == ATA_S_COMPLETE) {
2040f7d09f74SMatthew Dillon 				kprintf("%s: WARNING!  Unload prdbc resid "
2041f7d09f74SMatthew Dillon 					"was zero! tag=%d\n",
204212feb904SMatthew Dillon 					ATANAME(ap, xa->at), ccb->ccb_slot);
204312feb904SMatthew Dillon 			}
2044258223a3SMatthew Dillon 			xa->resid = xa->datalen -
2045258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
2046258223a3SMatthew Dillon 		}
2047258223a3SMatthew Dillon 	}
2048f7d09f74SMatthew Dillon }
2049258223a3SMatthew Dillon 
20505f8c1efdSMatthew Dillon /*
20515f8c1efdSMatthew Dillon  * Start a command and poll for completion.
20525f8c1efdSMatthew Dillon  *
20533209f581SMatthew Dillon  * timeout is in ms and only counts once the command gets on-chip.
20543209f581SMatthew Dillon  *
2055831bc9e3SMatthew Dillon  * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
2056831bc9e3SMatthew Dillon  * that no error occured.
2057831bc9e3SMatthew Dillon  *
20585f8c1efdSMatthew Dillon  * NOTE: If the caller specifies a NULL timeout function the caller is
20595f8c1efdSMatthew Dillon  *	 responsible for clearing hardware state on failure, but we will
20605f8c1efdSMatthew Dillon  *	 deal with removing the ccb from any pending queue.
20615f8c1efdSMatthew Dillon  *
20625f8c1efdSMatthew Dillon  * NOTE: NCQ should never be used with this function.
2063cf5f3a81SMatthew Dillon  *
2064cf5f3a81SMatthew Dillon  * NOTE: If the port is in a failed state and stopped we do not try
2065cf5f3a81SMatthew Dillon  *	 to activate the ccb.
20665f8c1efdSMatthew Dillon  */
2067258223a3SMatthew Dillon int
2068831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout,
2069831bc9e3SMatthew Dillon 	  void (*timeout_fn)(struct ahci_ccb *))
2070258223a3SMatthew Dillon {
2071258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
2072258223a3SMatthew Dillon 
2073cf5f3a81SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
2074cf5f3a81SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
2075831bc9e3SMatthew Dillon 		return(ccb->ccb_xa.state);
2076cf5f3a81SMatthew Dillon 	}
2077258223a3SMatthew Dillon 	crit_enter();
207812feb904SMatthew Dillon #if 0
207912feb904SMatthew Dillon 	kprintf("%s: Start command %02x tag=%d\n",
208012feb904SMatthew Dillon 		ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
208112feb904SMatthew Dillon 		ccb->ccb_xa.fis->command, ccb->ccb_slot);
208212feb904SMatthew Dillon #endif
2083258223a3SMatthew Dillon 	ahci_start(ccb);
20841980eff3SMatthew Dillon 
2085258223a3SMatthew Dillon 	do {
2086f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2087831bc9e3SMatthew Dillon 		switch(ccb->ccb_xa.state) {
2088831bc9e3SMatthew Dillon 		case ATA_S_ONCHIP:
2089831bc9e3SMatthew Dillon 			timeout -= ahci_os_softsleep();
2090f4553de1SMatthew Dillon 			break;
2091831bc9e3SMatthew Dillon 		case ATA_S_PENDING:
209231075e01SMatthew Dillon 			timeout -= ahci_os_softsleep();
2093831bc9e3SMatthew Dillon 			ahci_check_active_timeouts(ap);
2094831bc9e3SMatthew Dillon 			break;
2095831bc9e3SMatthew Dillon 		default:
2096831bc9e3SMatthew Dillon 			crit_exit();
2097831bc9e3SMatthew Dillon 			return (ccb->ccb_xa.state);
2098f4553de1SMatthew Dillon 		}
20993209f581SMatthew Dillon 	} while (timeout > 0);
21005f8c1efdSMatthew Dillon 
2101492bffafSMatthew Dillon 	if ((ccb->ccb_xa.flags & ATA_F_SILENT) == 0) {
2102c3783d8fSzrj 		kprintf("%s: Poll timeout slot %d "
2103c3783d8fSzrj 			"CMD: %pb%i TFD: 0x%pb%i SERR: %pb%i\n",
2104831bc9e3SMatthew Dillon 			ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
2105c3783d8fSzrj 			AHCI_PFMT_CMD, ahci_pread(ap, AHCI_PREG_CMD),
2106c3783d8fSzrj 			AHCI_PFMT_TFD_STS, ahci_pread(ap, AHCI_PREG_TFD),
2107c3783d8fSzrj 			AHCI_PFMT_SERR, ahci_pread(ap, AHCI_PREG_SERR));
2108492bffafSMatthew Dillon 	}
21095f8c1efdSMatthew Dillon 
2110258223a3SMatthew Dillon 	timeout_fn(ccb);
2111831bc9e3SMatthew Dillon 
2112258223a3SMatthew Dillon 	crit_exit();
2113258223a3SMatthew Dillon 
2114831bc9e3SMatthew Dillon 	return(ccb->ccb_xa.state);
2115831bc9e3SMatthew Dillon }
2116831bc9e3SMatthew Dillon 
2117831bc9e3SMatthew Dillon /*
2118831bc9e3SMatthew Dillon  * When polling we have to check if the currently active CCB(s)
2119831bc9e3SMatthew Dillon  * have timed out as the callout will be deadlocked while we
2120831bc9e3SMatthew Dillon  * hold the port lock.
2121831bc9e3SMatthew Dillon  */
2122831bc9e3SMatthew Dillon void
2123831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap)
2124831bc9e3SMatthew Dillon {
2125831bc9e3SMatthew Dillon 	struct ahci_ccb *ccb;
2126831bc9e3SMatthew Dillon 	u_int32_t mask;
2127831bc9e3SMatthew Dillon 	int tag;
2128831bc9e3SMatthew Dillon 
2129831bc9e3SMatthew Dillon 	mask = ap->ap_active | ap->ap_sactive;
2130831bc9e3SMatthew Dillon 	while (mask) {
2131831bc9e3SMatthew Dillon 		tag = ffs(mask) - 1;
2132831bc9e3SMatthew Dillon 		mask &= ~(1 << tag);
2133831bc9e3SMatthew Dillon 		ccb = &ap->ap_ccbs[tag];
2134831bc9e3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2135831bc9e3SMatthew Dillon 			ahci_ata_cmd_timeout(ccb);
2136831bc9e3SMatthew Dillon 		}
2137831bc9e3SMatthew Dillon 	}
2138258223a3SMatthew Dillon }
2139258223a3SMatthew Dillon 
21403209f581SMatthew Dillon static
21413209f581SMatthew Dillon __inline
21423209f581SMatthew Dillon void
21433209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb)
21443209f581SMatthew Dillon {
21453209f581SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
21463209f581SMatthew Dillon 		ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
21473209f581SMatthew Dillon 		callout_reset(&ccb->ccb_timeout,
21483209f581SMatthew Dillon 			      (ccb->ccb_xa.timeout * hz + 999) / 1000,
21493209f581SMatthew Dillon 			      ahci_ata_cmd_timeout_unserialized, ccb);
21503209f581SMatthew Dillon 	}
21513209f581SMatthew Dillon }
21523209f581SMatthew Dillon 
2153258223a3SMatthew Dillon void
2154258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
2155258223a3SMatthew Dillon {
2156258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
2157258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
2158258223a3SMatthew Dillon 
2159258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2160258223a3SMatthew Dillon 
2161258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
2162258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
2163258223a3SMatthew Dillon 
2164258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
2165258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
2166258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2167258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
2168258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
2169258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2170258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
2171258223a3SMatthew Dillon 
2172258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
2173258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
2174258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2175258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
2176258223a3SMatthew Dillon 
21771980eff3SMatthew Dillon 	/*
21784c339a5fSMatthew Dillon 	 * There's no point trying to optimize this, it only shaves a few
21794c339a5fSMatthew Dillon 	 * nanoseconds so just queue the command and call our generic issue.
21801980eff3SMatthew Dillon 	 */
21814c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, ccb);
2182258223a3SMatthew Dillon }
2183258223a3SMatthew Dillon 
2184831bc9e3SMatthew Dillon /*
2185831bc9e3SMatthew Dillon  * While holding the port lock acquire exclusive access to the port.
2186831bc9e3SMatthew Dillon  *
2187831bc9e3SMatthew Dillon  * This is used when running the state machine to initialize and identify
2188831bc9e3SMatthew Dillon  * targets over a port multiplier.  Setting exclusive access prevents
2189831bc9e3SMatthew Dillon  * ahci_port_intr() from activating any requests sitting on the pending
2190831bc9e3SMatthew Dillon  * queue.
2191831bc9e3SMatthew Dillon  */
2192831bc9e3SMatthew Dillon void
2193831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2194831bc9e3SMatthew Dillon {
2195831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
2196831bc9e3SMatthew Dillon 	ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
2197831bc9e3SMatthew Dillon 	while (ap->ap_active || ap->ap_sactive) {
2198831bc9e3SMatthew Dillon 		ahci_port_intr(ap, 1);
2199831bc9e3SMatthew Dillon 		ahci_os_softsleep();
2200831bc9e3SMatthew Dillon 	}
2201831bc9e3SMatthew Dillon }
2202831bc9e3SMatthew Dillon 
2203831bc9e3SMatthew Dillon void
2204831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
2205831bc9e3SMatthew Dillon {
2206831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
2207831bc9e3SMatthew Dillon 	ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
22084c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
2209831bc9e3SMatthew Dillon }
2210831bc9e3SMatthew Dillon 
22111980eff3SMatthew Dillon /*
22124c339a5fSMatthew Dillon  * If ccb is not NULL enqueue and/or issue it.
22134c339a5fSMatthew Dillon  *
22144c339a5fSMatthew Dillon  * If ccb is NULL issue whatever we can from the queue.  However, nothing
22154c339a5fSMatthew Dillon  * new is issued if the exclusive access flag is set or expired ccb's are
22164c339a5fSMatthew Dillon  * present.
22174c339a5fSMatthew Dillon  *
22184c339a5fSMatthew Dillon  * If existing commands are still active (ap_active/ap_sactive) we can only
22194c339a5fSMatthew Dillon  * issue matching new commands.
22201980eff3SMatthew Dillon  */
22214c339a5fSMatthew Dillon void
22224c339a5fSMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
22234c339a5fSMatthew Dillon {
22244c339a5fSMatthew Dillon 	u_int32_t	mask;
22254c339a5fSMatthew Dillon 	int		limit;
22268119d5f5SMatthew Dillon 	struct ata_port	*ccb_at;
2227258223a3SMatthew Dillon 
22281980eff3SMatthew Dillon 	/*
22294c339a5fSMatthew Dillon 	 * Enqueue the ccb.
22304c339a5fSMatthew Dillon 	 *
22314c339a5fSMatthew Dillon 	 * If just running the queue and in exclusive access mode we
22324c339a5fSMatthew Dillon 	 * just return.  Also in this case if there are any expired ccb's
22334c339a5fSMatthew Dillon 	 * we want to clear the queue so the port can be safely stopped.
22344c339a5fSMatthew Dillon 	 */
22354c339a5fSMatthew Dillon 	if (ccb) {
22364c339a5fSMatthew Dillon 		TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
22374c339a5fSMatthew Dillon 	} else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
22384c339a5fSMatthew Dillon 		return;
22394c339a5fSMatthew Dillon 	}
22404c339a5fSMatthew Dillon 
22414c339a5fSMatthew Dillon 	/*
22424c339a5fSMatthew Dillon 	 * Pull the next ccb off the queue and run it if possible.
2243c1fd1d86SMatthew Dillon 	 *
2244c1fd1d86SMatthew Dillon 	 * The error CCB supercedes all normal queue operations and
2245c1fd1d86SMatthew Dillon 	 * implies exclusive access while the error CCB is active.
22464c339a5fSMatthew Dillon 	 */
2247c1fd1d86SMatthew Dillon 	if (ccb != ap->ap_err_ccb) {
22484c339a5fSMatthew Dillon 		if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
22494c339a5fSMatthew Dillon 			return;
2250c1fd1d86SMatthew Dillon 		if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
2251c1fd1d86SMatthew Dillon 			kprintf("DELAY CCB slot %d\n", ccb->ccb_slot);
2252c1fd1d86SMatthew Dillon 			return;
2253c1fd1d86SMatthew Dillon 		}
2254c1fd1d86SMatthew Dillon 	}
22554c339a5fSMatthew Dillon 
225612feb904SMatthew Dillon 	/*
225712feb904SMatthew Dillon 	 * Handle exclusivity requirements.
225812feb904SMatthew Dillon 	 *
225912feb904SMatthew Dillon 	 * ATA_F_EXCLUSIVE is used when we want to be the only command
226012feb904SMatthew Dillon 	 * running.
226112feb904SMatthew Dillon 	 *
226212feb904SMatthew Dillon 	 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
226312feb904SMatthew Dillon 	 * back into the ccb on a normal (non-errored) command completion.
226412feb904SMatthew Dillon 	 * For example, for PM requests to target 15.  Because the AHCI
226512feb904SMatthew Dillon 	 * spec does not stop the command processor and has only one rfis
226612feb904SMatthew Dillon 	 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
226712feb904SMatthew Dillon 	 * Otherwise multiple completions can destroy the rfis data before
226812feb904SMatthew Dillon 	 * we have a chance to copy it.
226912feb904SMatthew Dillon 	 */
227012feb904SMatthew Dillon 	if (ap->ap_active & ~ap->ap_expired) {
227112feb904SMatthew Dillon 		/*
227212feb904SMatthew Dillon 		 * There may be multiple ccb's already running,
227312feb904SMatthew Dillon 		 * if any are running and ap_run_flags sets
227412feb904SMatthew Dillon 		 * one of these flags then we know only one is
227512feb904SMatthew Dillon 		 * running.
227612feb904SMatthew Dillon 		 *
227712feb904SMatthew Dillon 		 * XXX Current AUTOSENSE code forces exclusivity
227812feb904SMatthew Dillon 		 *     to simplify the code.
227912feb904SMatthew Dillon 		 */
228012feb904SMatthew Dillon 		if (ap->ap_run_flags &
228112feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
228212feb904SMatthew Dillon 			return;
228312feb904SMatthew Dillon 		}
228412feb904SMatthew Dillon 
228512feb904SMatthew Dillon 		if (ccb->ccb_xa.flags &
228612feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
228712feb904SMatthew Dillon 			return;
228812feb904SMatthew Dillon 		}
228912feb904SMatthew Dillon 	}
229012feb904SMatthew Dillon 
22914c339a5fSMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
22924c339a5fSMatthew Dillon 		/*
22934c339a5fSMatthew Dillon 		 * The next command is a NCQ command and can be issued as
22944c339a5fSMatthew Dillon 		 * long as currently active commands are not standard.
22954c339a5fSMatthew Dillon 		 */
22964c339a5fSMatthew Dillon 		if (ap->ap_active) {
22974c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
22984c339a5fSMatthew Dillon 			return;
22994c339a5fSMatthew Dillon 		}
23004c339a5fSMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
23014c339a5fSMatthew Dillon 
23024c339a5fSMatthew Dillon 		mask = 0;
23034c339a5fSMatthew Dillon 		do {
23044c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2305d16d3400SMatthew Dillon 			KKASSERT((mask & (1 << ccb->ccb_slot)) == 0);
23064c339a5fSMatthew Dillon 			mask |= 1 << ccb->ccb_slot;
2307d16d3400SMatthew Dillon 			KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
2308d16d3400SMatthew Dillon 			KKASSERT(ccb == &ap->ap_ccbs[ccb->ccb_slot]);
23094c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
231012feb904SMatthew Dillon 			ahci_start_timeout(ccb);
231112feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
23128119d5f5SMatthew Dillon 
23138119d5f5SMatthew Dillon 			ccb_at = ccb->ccb_xa.at;
23148119d5f5SMatthew Dillon 			if (ap->ap_flags & AP_F_FBSS_ENABLED) {
23158119d5f5SMatthew Dillon 				ap->ap_sactive |= mask;
23168119d5f5SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, mask);
23178119d5f5SMatthew Dillon 				if (ccb_at) {
23188119d5f5SMatthew Dillon 					ahci_pwrite(ap, AHCI_PREG_FBS,
23198119d5f5SMatthew Dillon 						(ccb_at->at_target <<
23208119d5f5SMatthew Dillon 						 AHCI_PREG_FBS_DEV_SHIFT) |
23218119d5f5SMatthew Dillon 						AHCI_PREG_FBS_EN);
23228119d5f5SMatthew Dillon 				} else {
23238119d5f5SMatthew Dillon 					ahci_pwrite(ap, AHCI_PREG_FBS,
23248119d5f5SMatthew Dillon 						AHCI_PREG_FBS_EN);
23258119d5f5SMatthew Dillon 				}
23268119d5f5SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_CI, mask);
23278119d5f5SMatthew Dillon 				mask = 0;
23288119d5f5SMatthew Dillon 			}
23294c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
233012feb904SMatthew Dillon 		} while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
233112feb904SMatthew Dillon 			 (ap->ap_run_flags &
233212feb904SMatthew Dillon 			     (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
23334c339a5fSMatthew Dillon 
2334d16d3400SMatthew Dillon 		KKASSERT(((ap->ap_active | ap->ap_sactive) & mask) == 0);
2335d16d3400SMatthew Dillon 
23368119d5f5SMatthew Dillon 		if (mask) {
23374c339a5fSMatthew Dillon 			ap->ap_sactive |= mask;
23384c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, mask);
23394c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, mask);
23408119d5f5SMatthew Dillon 		}
23414c339a5fSMatthew Dillon 	} else {
23424c339a5fSMatthew Dillon 		/*
23434c339a5fSMatthew Dillon 		 * The next command is a standard command and can be issued
23444c339a5fSMatthew Dillon 		 * as long as currently active commands are not NCQ.
23454c339a5fSMatthew Dillon 		 *
23464c339a5fSMatthew Dillon 		 * We limit ourself to 1 command if we have a port multiplier,
23474c339a5fSMatthew Dillon 		 * (at least without FBSS support), otherwise timeouts on
23484c339a5fSMatthew Dillon 		 * one port can race completions on other ports (see
23494c339a5fSMatthew Dillon 		 * ahci_ata_cmd_timeout() for more information).
23504c339a5fSMatthew Dillon 		 *
23514c339a5fSMatthew Dillon 		 * If not on a port multiplier generally allow up to 4
23524c339a5fSMatthew Dillon 		 * standard commands to be enqueued.  Remember that the
23534c339a5fSMatthew Dillon 		 * command processor will still process them sequentially.
23541980eff3SMatthew Dillon 		 */
23551980eff3SMatthew Dillon 		if (ap->ap_sactive)
2356258223a3SMatthew Dillon 			return;
23578119d5f5SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM &&
23588119d5f5SMatthew Dillon 		    (ap->ap_flags & AP_F_FBSS_ENABLED) == 0) {
23594c339a5fSMatthew Dillon 			limit = 1;
23608119d5f5SMatthew Dillon 		} else if (ap->ap_sc->sc_ncmds > 4) {
23614c339a5fSMatthew Dillon 			limit = 4;
23628119d5f5SMatthew Dillon 		} else {
23634c339a5fSMatthew Dillon 			limit = 2;
23648119d5f5SMatthew Dillon 		}
2365258223a3SMatthew Dillon 
23664c339a5fSMatthew Dillon 		while (ap->ap_active_cnt < limit && ccb &&
23674c339a5fSMatthew Dillon 		       (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
23688119d5f5SMatthew Dillon 			ccb_at = ccb->ccb_xa.at;
23694c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
2370d16d3400SMatthew Dillon 			KKASSERT(((ap->ap_active | ap->ap_sactive) &
2371d16d3400SMatthew Dillon 				  (1 << ccb->ccb_slot)) == 0);
23724c339a5fSMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
2373258223a3SMatthew Dillon 			ap->ap_active_cnt++;
237412feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
23754c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
237612feb904SMatthew Dillon 			ahci_start_timeout(ccb);
23778119d5f5SMatthew Dillon 			if (ap->ap_flags & AP_F_FBSS_ENABLED) {
23788119d5f5SMatthew Dillon 				if (ccb_at) {
23798119d5f5SMatthew Dillon 					ahci_pwrite(ap, AHCI_PREG_FBS,
23808119d5f5SMatthew Dillon 						(ccb_at->at_target <<
23818119d5f5SMatthew Dillon 						 AHCI_PREG_FBS_DEV_SHIFT) |
23828119d5f5SMatthew Dillon 						AHCI_PREG_FBS_EN);
23838119d5f5SMatthew Dillon 				} else {
23848119d5f5SMatthew Dillon 					ahci_pwrite(ap, AHCI_PREG_FBS,
23858119d5f5SMatthew Dillon 						AHCI_PREG_FBS_EN);
23868119d5f5SMatthew Dillon 				}
23878119d5f5SMatthew Dillon 			}
2388d16d3400SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
238922726f69SMatthew Dillon 			if ((ap->ap_run_flags &
239022726f69SMatthew Dillon 			    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0) {
239122726f69SMatthew Dillon 				break;
239222726f69SMatthew Dillon 			}
23934c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
239412feb904SMatthew Dillon 			if (ccb && (ccb->ccb_xa.flags &
239512feb904SMatthew Dillon 				    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
239612feb904SMatthew Dillon 				break;
239712feb904SMatthew Dillon 			}
23981980eff3SMatthew Dillon 		}
2399258223a3SMatthew Dillon 	}
2400258223a3SMatthew Dillon }
2401258223a3SMatthew Dillon 
2402258223a3SMatthew Dillon void
2403258223a3SMatthew Dillon ahci_intr(void *arg)
2404258223a3SMatthew Dillon {
2405258223a3SMatthew Dillon 	struct ahci_softc	*sc = arg;
2406f4553de1SMatthew Dillon 	struct ahci_port	*ap;
240712feb904SMatthew Dillon 	u_int32_t		is;
240812feb904SMatthew Dillon 	u_int32_t		ack;
2409258223a3SMatthew Dillon 	int			port;
2410258223a3SMatthew Dillon 
2411f4553de1SMatthew Dillon 	/*
2412f4553de1SMatthew Dillon 	 * Check if the master enable is up, and whether any interrupts are
2413f4553de1SMatthew Dillon 	 * pending.
2414f4553de1SMatthew Dillon 	 */
2415f4553de1SMatthew Dillon 	if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2416f4553de1SMatthew Dillon 		return;
2417258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
241812feb904SMatthew Dillon 	if (is == 0 || is == 0xffffffff) {
2419258223a3SMatthew Dillon 		return;
242012feb904SMatthew Dillon 	}
242112feb904SMatthew Dillon 	is &= sc->sc_portmask;
2422258223a3SMatthew Dillon 
2423258223a3SMatthew Dillon #ifdef AHCI_COALESCE
2424258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
2425258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
2426258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2427258223a3SMatthew Dillon 		    DEVNAME(sc));
2428258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
2429258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
2430258223a3SMatthew Dillon 	}
2431258223a3SMatthew Dillon #endif
2432258223a3SMatthew Dillon 
2433f4553de1SMatthew Dillon 	/*
2434f4553de1SMatthew Dillon 	 * Process interrupts for each port in a non-blocking fashion.
243512feb904SMatthew Dillon 	 *
24363d102df7SMatthew Dillon 	 * The global IS bit is supposed to be forced on if any unmasked
24373d102df7SMatthew Dillon 	 * port interrupt is pending, even if we clear it.
24383d102df7SMatthew Dillon 	 *
24393d102df7SMatthew Dillon 	 * However it would appear that it is simply latched on some parts,
24403d102df7SMatthew Dillon 	 * which means we have to clear it BEFORE processing the status bits
24413d102df7SMatthew Dillon 	 * to avoid races.
2442f4553de1SMatthew Dillon 	 */
24433d102df7SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, is);
244412feb904SMatthew Dillon 	for (ack = 0; is; is &= ~(1 << port)) {
2445258223a3SMatthew Dillon 		port = ffs(is) - 1;
244612feb904SMatthew Dillon 		ack |= 1 << port;
244712feb904SMatthew Dillon 
2448f4553de1SMatthew Dillon 		ap = sc->sc_ports[port];
244912feb904SMatthew Dillon 		if (ap == NULL)
245012feb904SMatthew Dillon 			continue;
245112feb904SMatthew Dillon 
2452f4553de1SMatthew Dillon 		if (ahci_os_lock_port_nb(ap) == 0) {
2453f4553de1SMatthew Dillon 			ahci_port_intr(ap, 0);
2454f4553de1SMatthew Dillon 			ahci_os_unlock_port(ap);
2455f4553de1SMatthew Dillon 		} else {
2456f4553de1SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2457f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2458f4553de1SMatthew Dillon 		}
2459f4553de1SMatthew Dillon 	}
2460258223a3SMatthew Dillon }
2461258223a3SMatthew Dillon 
2462f4553de1SMatthew Dillon /*
2463f4553de1SMatthew Dillon  * Core called from helper thread.
2464f4553de1SMatthew Dillon  */
24653209f581SMatthew Dillon void
2466f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask)
2467f4553de1SMatthew Dillon {
2468f4553de1SMatthew Dillon 	/*
2469f4553de1SMatthew Dillon 	 * Process any expired timedouts.
2470f4553de1SMatthew Dillon 	 */
2471f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
2472f4553de1SMatthew Dillon 	if (mask & AP_SIGF_TIMEOUT) {
2473831bc9e3SMatthew Dillon 		ahci_check_active_timeouts(ap);
2474f4553de1SMatthew Dillon 	}
2475f4553de1SMatthew Dillon 
2476f4553de1SMatthew Dillon 	/*
2477f4553de1SMatthew Dillon 	 * Process port interrupts which require a higher level of
2478f4553de1SMatthew Dillon 	 * intervention.
2479f4553de1SMatthew Dillon 	 */
2480f4553de1SMatthew Dillon 	if (mask & AP_SIGF_PORTINT) {
2481f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2482f4553de1SMatthew Dillon 		ahci_port_interrupt_enable(ap);
248312feb904SMatthew Dillon 	} else if (ap->ap_probe != ATA_PROBE_FAILED) {
248412feb904SMatthew Dillon 		ahci_port_intr(ap, 1);
248512feb904SMatthew Dillon 		ahci_port_interrupt_enable(ap);
2486f4553de1SMatthew Dillon 	}
2487d16d3400SMatthew Dillon 	ahci_os_unlock_port(ap);
2488f4553de1SMatthew Dillon }
2489f4553de1SMatthew Dillon 
2490f4553de1SMatthew Dillon /*
2491f4553de1SMatthew Dillon  * Core per-port interrupt handler.
2492f4553de1SMatthew Dillon  *
2493f4553de1SMatthew Dillon  * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2494f4553de1SMatthew Dillon  * deal with normal command completions which do not require blocking.
2495f4553de1SMatthew Dillon  */
2496f4553de1SMatthew Dillon void
2497f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable)
2498258223a3SMatthew Dillon {
2499258223a3SMatthew Dillon 	struct ahci_softc	*sc = ap->ap_sc;
25003209f581SMatthew Dillon 	u_int32_t		is, ci_saved, ci_masked;
250122181ab7SMatthew Dillon 	int			slot;
2502492bffafSMatthew Dillon 	int			stopped = 0;
2503258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
25041980eff3SMatthew Dillon 	struct ata_port		*ccb_at = NULL;
2505258223a3SMatthew Dillon 	volatile u_int32_t	*active;
2506f4553de1SMatthew Dillon 	const u_int32_t		blockable_mask = AHCI_PREG_IS_TFES |
2507f4553de1SMatthew Dillon 						 AHCI_PREG_IS_IFS |
2508f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PCS |
2509f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PRCS |
2510f4553de1SMatthew Dillon 						 AHCI_PREG_IS_HBFS |
2511f4553de1SMatthew Dillon 						 AHCI_PREG_IS_OFS |
2512f4553de1SMatthew Dillon 						 AHCI_PREG_IS_UFS;
2513f4553de1SMatthew Dillon 
2514492bffafSMatthew Dillon 	enum { NEED_NOTHING, NEED_REINIT, NEED_RESTART,
2515492bffafSMatthew Dillon 	       NEED_HOTPLUG_INSERT, NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2516258223a3SMatthew Dillon 
2517f4553de1SMatthew Dillon 	/*
2518f4553de1SMatthew Dillon 	 * All basic command completions are always processed.
2519f4553de1SMatthew Dillon 	 */
252012feb904SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
2521cec07d75SMatthew Dillon 	if (is & AHCI_PREG_IS_DPS)
2522cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2523258223a3SMatthew Dillon 
2524f4553de1SMatthew Dillon 	/*
2525f4553de1SMatthew Dillon 	 * If we can't block then we can't handle these here.  Disable
2526f4553de1SMatthew Dillon 	 * the interrupts in question so we don't live-lock, the helper
2527f4553de1SMatthew Dillon 	 * thread will re-enable them.
2528f4553de1SMatthew Dillon 	 *
2529f4553de1SMatthew Dillon 	 * If the port is in a completely failed state we do not want
2530dbef6246SMatthew Dillon 	 * to drop through to failed-command-processing if blockable is 0,
2531f4553de1SMatthew Dillon 	 * just let the thread deal with it all.
2532dbef6246SMatthew Dillon 	 *
2533dbef6246SMatthew Dillon 	 * Otherwise we fall through and still handle DHRS and any commands
2534dbef6246SMatthew Dillon 	 * which completed normally.  Even if we are errored we haven't
2535dbef6246SMatthew Dillon 	 * stopped the port yet so CI/SACT are still good.
2536f4553de1SMatthew Dillon 	 */
2537f4553de1SMatthew Dillon 	if (blockable == 0) {
2538f4553de1SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
253912feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2540f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2541f4553de1SMatthew Dillon 			return;
2542f4553de1SMatthew Dillon 		}
2543f4553de1SMatthew Dillon 		if (is & blockable_mask) {
254412feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2545f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
254612feb904SMatthew Dillon 			return;
2547f4553de1SMatthew Dillon 		}
2548f4553de1SMatthew Dillon 	}
2549f4553de1SMatthew Dillon 
25503209f581SMatthew Dillon 	/*
2551f4553de1SMatthew Dillon 	 * Either NCQ or non-NCQ commands will be active, never both.
25523209f581SMatthew Dillon 	 */
2553258223a3SMatthew Dillon 	if (ap->ap_sactive) {
2554258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
2555258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
2556258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2557258223a3SMatthew Dillon 		active = &ap->ap_sactive;
2558258223a3SMatthew Dillon 	} else {
2559258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2560258223a3SMatthew Dillon 		active = &ap->ap_active;
2561258223a3SMatthew Dillon 	}
256212feb904SMatthew Dillon 	KKASSERT(!(ap->ap_sactive && ap->ap_active));
2563d16d3400SMatthew Dillon 	KKASSERT((ci_saved & (ap->ap_sactive | ap->ap_active)) == ci_saved);
256412feb904SMatthew Dillon #if 0
256512feb904SMatthew Dillon 	kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
256612feb904SMatthew Dillon 		ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
256712feb904SMatthew Dillon 		ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
256812feb904SMatthew Dillon #endif
2569258223a3SMatthew Dillon 
2570492bffafSMatthew Dillon 	/*
2571492bffafSMatthew Dillon 	 * Ignore AHCI_PREG_IS_PRCS when link power management is on
2572492bffafSMatthew Dillon 	 */
2573795adb22SMatthew Dillon 	if (ap->link_pwr_mgmt != AHCI_LINK_PWR_MGMT_NONE) {
2574795adb22SMatthew Dillon 		is &= ~AHCI_PREG_IS_PRCS;
2575795adb22SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
2576795adb22SMatthew Dillon 			    AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_W);
2577795adb22SMatthew Dillon 	}
2578795adb22SMatthew Dillon 
2579cf5f3a81SMatthew Dillon 	/*
2580f4553de1SMatthew Dillon 	 * Command failed (blockable).
2581f4553de1SMatthew Dillon 	 *
2582f4553de1SMatthew Dillon 	 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
25831980eff3SMatthew Dillon 	 *
25841980eff3SMatthew Dillon 	 * This stops command processing.
2585cf5f3a81SMatthew Dillon 	 */
2586492bffafSMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
2587258223a3SMatthew Dillon 		u_int32_t tfd, serr;
2588258223a3SMatthew Dillon 		int	err_slot;
2589258223a3SMatthew Dillon 
259012feb904SMatthew Dillon process_error:
2591258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
2592258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2593258223a3SMatthew Dillon 
2594cf5f3a81SMatthew Dillon 		/*
259512feb904SMatthew Dillon 		 * Load the error slot and restart command processing.
259612feb904SMatthew Dillon 		 * CLO if we need to.  The error slot may not be valid.
259712feb904SMatthew Dillon 		 * MUST BE DONE BEFORE CLEARING ST!
259812feb904SMatthew Dillon 		 *
259912feb904SMatthew Dillon 		 * Cycle ST.
260012feb904SMatthew Dillon 		 *
260112feb904SMatthew Dillon 		 * It is unclear but we may have to clear SERR to reenable
260212feb904SMatthew Dillon 		 * error processing.
2603cf5f3a81SMatthew Dillon 		 */
260412feb904SMatthew Dillon 		err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
260512feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
260612feb904SMatthew Dillon 					      AHCI_PREG_IS_PSS |
260712feb904SMatthew Dillon 					      AHCI_PREG_IS_DHRS |
260812feb904SMatthew Dillon 					      AHCI_PREG_IS_SDBS);
260912feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
261012feb904SMatthew Dillon 			AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
261112feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2612258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
261312feb904SMatthew Dillon 		ahci_os_hardsleep(10);
261412feb904SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
261512feb904SMatthew Dillon 			kprintf("%s: Issuing CLO\n", PORTNAME(ap));
261612feb904SMatthew Dillon 			ahci_port_clo(ap);
261712feb904SMatthew Dillon 		}
2618492bffafSMatthew Dillon 
2619492bffafSMatthew Dillon 		/*
2620492bffafSMatthew Dillon 		 * We are now stopped and need a restart.  If we have to
2621492bffafSMatthew Dillon 		 * process a NCQ error we will temporarily start and then
2622492bffafSMatthew Dillon 		 * stop the port again, so this condition holds.
2623492bffafSMatthew Dillon 		 */
2624492bffafSMatthew Dillon 		stopped = 1;
262522181ab7SMatthew Dillon 		need = NEED_RESTART;
2626258223a3SMatthew Dillon 
262750a3ecb6SMatthew Dillon 		/*
262850a3ecb6SMatthew Dillon 		 * ATAPI errors are fairly common from probing, just
262950a3ecb6SMatthew Dillon 		 * report disk errors or if bootverbose is on.
263050a3ecb6SMatthew Dillon 		 */
263150a3ecb6SMatthew Dillon 		if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
263212feb904SMatthew Dillon 			kprintf("%s: TFES slot %d ci_saved = %08x\n",
263312feb904SMatthew Dillon 				PORTNAME(ap), err_slot, ci_saved);
263450a3ecb6SMatthew Dillon 		}
2635258223a3SMatthew Dillon 
26361980eff3SMatthew Dillon 		/*
263712feb904SMatthew Dillon 		 * If we got an error on an error CCB just complete it
263812feb904SMatthew Dillon 		 * with an error.  ci_saved has the mask to restart
263912feb904SMatthew Dillon 		 * (the err_ccb will be removed from it by finish_error).
26401980eff3SMatthew Dillon 		 */
264112feb904SMatthew Dillon 		if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
264212feb904SMatthew Dillon 			err_slot = ap->ap_err_ccb->ccb_slot;
264312feb904SMatthew Dillon 			goto finish_error;
2644258223a3SMatthew Dillon 		}
2645258223a3SMatthew Dillon 
26461980eff3SMatthew Dillon 		/*
264712feb904SMatthew Dillon 		 * If NCQ commands were active get the error slot from
264812feb904SMatthew Dillon 		 * the log page.  NCQ is not supported for PM's so this
264912feb904SMatthew Dillon 		 * is a direct-attached target.
26501980eff3SMatthew Dillon 		 *
265112feb904SMatthew Dillon 		 * Otherwise if no commands were active we have a problem.
265212feb904SMatthew Dillon 		 *
265312feb904SMatthew Dillon 		 * Otherwise if the error slot is bad we have a problem.
265412feb904SMatthew Dillon 		 *
265512feb904SMatthew Dillon 		 * Otherwise process the error for the slot.
26561980eff3SMatthew Dillon 		 */
265712feb904SMatthew Dillon 		if (ap->ap_sactive) {
2658492bffafSMatthew Dillon 			ahci_port_start(ap);
265912feb904SMatthew Dillon 			err_slot = ahci_port_read_ncq_error(ap, 0);
2660492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
266112feb904SMatthew Dillon 		} else if (ap->ap_active == 0) {
266212feb904SMatthew Dillon 			kprintf("%s: TFES with no commands pending\n",
266312feb904SMatthew Dillon 				PORTNAME(ap));
266412feb904SMatthew Dillon 			err_slot = -1;
266512feb904SMatthew Dillon 		} else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
266612feb904SMatthew Dillon 			kprintf("%s: bad error slot %d\n",
2667258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
266812feb904SMatthew Dillon 			err_slot = -1;
2669258223a3SMatthew Dillon 		} else {
267012feb904SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
267112feb904SMatthew Dillon 
26724c339a5fSMatthew Dillon 			/*
267312feb904SMatthew Dillon 			 * Validate the errored ccb.  Note that ccb_at can
267412feb904SMatthew Dillon 			 * be NULL for direct-attached ccb's.
267512feb904SMatthew Dillon 			 *
267612feb904SMatthew Dillon 			 * Copy received taskfile data from the RFIS.
26774c339a5fSMatthew Dillon 			 */
267812feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
26798119d5f5SMatthew Dillon 				int fis_target;
26808119d5f5SMatthew Dillon 				uint32_t bytes;
26818119d5f5SMatthew Dillon 				intmax_t offset;
26828119d5f5SMatthew Dillon 				struct ata_fis_d2h *rfis;
26838119d5f5SMatthew Dillon 
268412feb904SMatthew Dillon 				ccb_at = ccb->ccb_xa.at;
26858119d5f5SMatthew Dillon 				if (ccb_at &&
26868119d5f5SMatthew Dillon 				    (ap->ap_flags & AP_F_FBSS_ENABLED))
26878119d5f5SMatthew Dillon 					fis_target = ccb_at->at_target;
26888119d5f5SMatthew Dillon 				else
26898119d5f5SMatthew Dillon 					fis_target = 0;
26908119d5f5SMatthew Dillon 
26918119d5f5SMatthew Dillon 				memcpy(&ccb->ccb_xa.rfis,
26928119d5f5SMatthew Dillon 				       ap->ap_rfis[fis_target].rfis,
269312feb904SMatthew Dillon 				       sizeof(struct ata_fis_d2h));
26948119d5f5SMatthew Dillon 				rfis = &ccb->ccb_xa.rfis;
26958119d5f5SMatthew Dillon 
26968119d5f5SMatthew Dillon 				offset = (intmax_t)rfis->lba_low |
26978119d5f5SMatthew Dillon 					((intmax_t)rfis->lba_mid << 8) |
26988119d5f5SMatthew Dillon 					((intmax_t)rfis->lba_high << 16) |
26998119d5f5SMatthew Dillon 					((intmax_t)rfis->lba_low_exp << 24) |
27008119d5f5SMatthew Dillon 					((intmax_t)rfis->lba_mid_exp << 32) |
27018119d5f5SMatthew Dillon 					((intmax_t)rfis->lba_high_exp << 40);
27028119d5f5SMatthew Dillon 				offset *= 512;
27038119d5f5SMatthew Dillon 				bytes = rfis->sector_count * 512;
27048119d5f5SMatthew Dillon 
27058119d5f5SMatthew Dillon 				/* NOTE: expect type == 0x34 */
27060e602b73SMatthew Dillon 				kprintf("%s: TFES ccb=%p slot=%d RFIS-%02x "
27070e602b73SMatthew Dillon 					"flg=%02x "
27088119d5f5SMatthew Dillon 					"st=%02x err=%02x dev=%02x "
27098119d5f5SMatthew Dillon 					"off=%jd/%d\n",
27108119d5f5SMatthew Dillon 					PORTNAME(ap),
27110e602b73SMatthew Dillon 					ccb->ccb_xa.atascsi_private,
27120e602b73SMatthew Dillon 					ccb->ccb_slot,
27138119d5f5SMatthew Dillon 					rfis->type,
27148119d5f5SMatthew Dillon 					rfis->flags,
27158119d5f5SMatthew Dillon 					rfis->status,
27168119d5f5SMatthew Dillon 					rfis->error,
27178119d5f5SMatthew Dillon 					rfis->device,
27188119d5f5SMatthew Dillon 					offset, bytes);
271912feb904SMatthew Dillon 			} else {
272012feb904SMatthew Dillon 				kprintf("%s: Cannot copy rfis, CCB slot "
272112feb904SMatthew Dillon 					"%d is not on-chip (state=%d)\n",
272212feb904SMatthew Dillon 					ATANAME(ap, ccb->ccb_xa.at),
272312feb904SMatthew Dillon 					err_slot, ccb->ccb_xa.state);
272412feb904SMatthew Dillon 				err_slot = -1;
272512feb904SMatthew Dillon 			}
2726258223a3SMatthew Dillon 		}
2727258223a3SMatthew Dillon 
2728258223a3SMatthew Dillon 		/*
272912feb904SMatthew Dillon 		 * If we could not determine the errored slot then
273012feb904SMatthew Dillon 		 * reset the port.
2731258223a3SMatthew Dillon 		 */
273212feb904SMatthew Dillon 		if (err_slot < 0) {
273312feb904SMatthew Dillon 			kprintf("%s: TFES: Unable to determine errored slot\n",
273412feb904SMatthew Dillon 				PORTNAME(ap));
27351980eff3SMatthew Dillon 			if (ap->ap_flags & AP_F_IN_RESET)
27361980eff3SMatthew Dillon 				goto fatal;
2737258223a3SMatthew Dillon 			goto failall;
2738258223a3SMatthew Dillon 		}
2739258223a3SMatthew Dillon 
274012feb904SMatthew Dillon 		/*
274112feb904SMatthew Dillon 		 * Finish error on slot.  We will restart ci_saved
274212feb904SMatthew Dillon 		 * commands except the errored slot which we generate
274312feb904SMatthew Dillon 		 * a failure for.
274412feb904SMatthew Dillon 		 */
274512feb904SMatthew Dillon finish_error:
274612feb904SMatthew Dillon 		ccb = &ap->ap_ccbs[err_slot];
2747258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
2748258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2749258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
27501980eff3SMatthew Dillon 	} else if (is & AHCI_PREG_IS_DHRS) {
27511980eff3SMatthew Dillon 		/*
2752f4553de1SMatthew Dillon 		 * Command posted D2H register FIS to the rfis (non-blocking).
2753f4553de1SMatthew Dillon 		 *
275412feb904SMatthew Dillon 		 * A normal completion with an error may set DHRS instead
275512feb904SMatthew Dillon 		 * of TFES.  The CCS bits are only valid if ERR was set.
275612feb904SMatthew Dillon 		 * If ERR is set command processing was probably stopped.
27578bf6a3ffSMatthew Dillon 		 *
275812feb904SMatthew Dillon 		 * If ERR was not set we can only copy-back data for
275912feb904SMatthew Dillon 		 * exclusive-mode commands because otherwise we won't know
276012feb904SMatthew Dillon 		 * which tag the rfis belonged to.
276112feb904SMatthew Dillon 		 *
276212feb904SMatthew Dillon 		 * err_slot must be read from the CCS before any other port
276312feb904SMatthew Dillon 		 * action, such as stopping the port.
276412feb904SMatthew Dillon 		 *
276512feb904SMatthew Dillon 		 * WARNING!	This is not well documented in the AHCI spec.
276612feb904SMatthew Dillon 		 *		It can be found in the state machine tables
276712feb904SMatthew Dillon 		 *		but not in the explanations.
27681980eff3SMatthew Dillon 		 */
276912feb904SMatthew Dillon 		u_int32_t tfd;
277012feb904SMatthew Dillon 		u_int32_t cmd;
27711980eff3SMatthew Dillon 		int err_slot;
27721980eff3SMatthew Dillon 
277312feb904SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
277412feb904SMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
277512feb904SMatthew Dillon 
27763d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2777eb9f4c83SMatthew Dillon 
2778eb9f4c83SMatthew Dillon 		/*
2779eb9f4c83SMatthew Dillon 		 * If command processing is turned off we can process the
2780eb9f4c83SMatthew Dillon 		 * error immediately.  Use the ST bit here instead of the
2781eb9f4c83SMatthew Dillon 		 * CR bit in case the CR bit is not implemented via the
2782eb9f4c83SMatthew Dillon 		 * F_IGN_CR quirk.
2783eb9f4c83SMatthew Dillon 		 */
278412feb904SMatthew Dillon 		if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
2785eb9f4c83SMatthew Dillon 		    (cmd & AHCI_PREG_CMD_ST) == 0) {
27861980eff3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(
27871980eff3SMatthew Dillon 						ahci_pread(ap, AHCI_PREG_CMD));
27881980eff3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
2789c3783d8fSzrj 			kprintf("%s: DHRS tfd=%pb%i err_slot=%d cmd=%02x\n",
2790c3783d8fSzrj 				PORTNAME(ap), AHCI_PFMT_TFD_STS, tfd,
279112feb904SMatthew Dillon 				err_slot, ccb->ccb_xa.fis->command);
279212feb904SMatthew Dillon 			goto process_error;
2793258223a3SMatthew Dillon 		}
279412feb904SMatthew Dillon 		/*
279512feb904SMatthew Dillon 		 * NO ELSE... copy back is in the normal command completion
279612feb904SMatthew Dillon 		 * code and only if no error occured and ATA_F_AUTOSENSE
279712feb904SMatthew Dillon 		 * was set.
279812feb904SMatthew Dillon 		 */
27991980eff3SMatthew Dillon 	}
28001980eff3SMatthew Dillon 
28011980eff3SMatthew Dillon 	/*
2802f4553de1SMatthew Dillon 	 * Device notification to us (non-blocking)
28031980eff3SMatthew Dillon 	 *
280412feb904SMatthew Dillon 	 * NOTE!  On some parts notification bits can cause an IPMS
280512feb904SMatthew Dillon 	 *	  interrupt instead of a SDBS interrupt.
2806cec07d75SMatthew Dillon 	 *
280712feb904SMatthew Dillon 	 * NOTE!  On some parts (e.g. VBOX, probably intel ICHx),
280812feb904SMatthew Dillon 	 *	  SDBS notifies us of the completion of a NCQ command
280912feb904SMatthew Dillon 	 *	  and DBS does not.
28101980eff3SMatthew Dillon 	 */
281112feb904SMatthew Dillon 	if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
28121980eff3SMatthew Dillon 		u_int32_t data;
28131980eff3SMatthew Dillon 
281412feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
281512feb904SMatthew Dillon 				AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
281612feb904SMatthew Dillon 		if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
28171980eff3SMatthew Dillon 			data = ahci_pread(ap, AHCI_PREG_SNTF);
2818cec07d75SMatthew Dillon 			if (data) {
281912feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_IS,
282012feb904SMatthew Dillon 						AHCI_PREG_IS_SDBS);
282112feb904SMatthew Dillon 				kprintf("%s: NOTIFY %08x\n",
282212feb904SMatthew Dillon 					PORTNAME(ap), data);
282312feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SERR,
282412feb904SMatthew Dillon 						AHCI_PREG_SERR_DIAG_N);
28253209f581SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SNTF, data);
28263209f581SMatthew Dillon 				ahci_cam_changed(ap, NULL, -1);
28271980eff3SMatthew Dillon 			}
28281980eff3SMatthew Dillon 		}
282912feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
283012feb904SMatthew Dillon 	}
28313209f581SMatthew Dillon 
28323209f581SMatthew Dillon 	/*
2833492bffafSMatthew Dillon 	 * Spurious IFS errors (blockable) - when AP_F_IGNORE_IFS is set.
2834f4553de1SMatthew Dillon 	 *
28353209f581SMatthew Dillon 	 * Spurious IFS errors can occur while we are doing a reset
2836492bffafSMatthew Dillon 	 * sequence through a PM, probably due to an unexpected FIS
2837492bffafSMatthew Dillon 	 * being received during the PM target reset sequence.  Chipsets
2838492bffafSMatthew Dillon 	 * are supposed to mask these events but some do not.
2839492bffafSMatthew Dillon 	 *
2840492bffafSMatthew Dillon 	 * Try to recover from the condition.
28413209f581SMatthew Dillon 	 */
28423209f581SMatthew Dillon 	if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
28431980eff3SMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
28443209f581SMatthew Dillon 		if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
2845492bffafSMatthew Dillon 			kprintf("%s: IFS during PM probe (ignored) "
2846c3783d8fSzrj 				"IS=%pb%i, SERR=%pb%i\n", PORTNAME(ap),
2847c3783d8fSzrj 				AHCI_PFMT_IS, is,
2848c3783d8fSzrj 				AHCI_PFMT_SERR, serr);
28493209f581SMatthew Dillon 			ap->ap_flags |= AP_F_IFS_IGNORED;
28503209f581SMatthew Dillon 		}
2851492bffafSMatthew Dillon 
2852492bffafSMatthew Dillon 		/*
2853492bffafSMatthew Dillon 		 * Try to clear the error condition.  The IFS error killed
2854492bffafSMatthew Dillon 		 * the port so stop it so we can restart it.
2855492bffafSMatthew Dillon 		 */
28561980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
28573d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
28581980eff3SMatthew Dillon 		is &= ~AHCI_PREG_IS_IFS;
2859492bffafSMatthew Dillon 		need = NEED_RESTART;
286012feb904SMatthew Dillon 		goto failall;
28611980eff3SMatthew Dillon 	}
2862258223a3SMatthew Dillon 
2863258223a3SMatthew Dillon 	/*
2864f4553de1SMatthew Dillon 	 * Port change (hot-plug) (blockable).
2865258223a3SMatthew Dillon 	 *
2866492bffafSMatthew Dillon 	 * A PRCS interrupt can occur:
2867492bffafSMatthew Dillon 	 *	(1) On hot-unplug / normal-unplug (phy lost)
2868492bffafSMatthew Dillon 	 *	(2) Sometimes on hot-plug too.
2869258223a3SMatthew Dillon 	 *
2870492bffafSMatthew Dillon 	 * A PCS interrupt can occur in a number of situations:
2871492bffafSMatthew Dillon 	 *	(1) On hot-plug once communication is established
2872492bffafSMatthew Dillon 	 *	(2) On hot-unplug sometimes.
2873492bffafSMatthew Dillon 	 *	(3) For chipsets with badly written firmware it can occur
2874492bffafSMatthew Dillon 	 *	    during INIT/RESET sequences due to the device reset.
2875492bffafSMatthew Dillon 	 *	(4) For chipsets with badly written firmware it can occur
2876492bffafSMatthew Dillon 	 *	    when it thinks an unsolicited COMRESET is received
2877492bffafSMatthew Dillon 	 *	    during a INIT/RESET sequence, even though we actually
2878492bffafSMatthew Dillon 	 *	    did request it.
2879258223a3SMatthew Dillon 	 *
288022181ab7SMatthew Dillon 	 * XXX We can then check the CPS (Cold Presence State) bit, if
288122181ab7SMatthew Dillon 	 * supported, to determine if a device is plugged in or not and do
288222181ab7SMatthew Dillon 	 * the right thing.
288322181ab7SMatthew Dillon 	 *
2884492bffafSMatthew Dillon 	 * PCS interrupts are cleared by clearing DIAG_X.  If this occurs
2885492bffafSMatthew Dillon 	 * command processing is automatically stopped (CR goes inactive)
2886492bffafSMatthew Dillon 	 * and the port must be stopped and restarted.
2887492bffafSMatthew Dillon 	 *
2888492bffafSMatthew Dillon 	 * WARNING: AMD parts (e.g. 880G chipset, probably others) can
2889492bffafSMatthew Dillon 	 *	    generate PCS on initialization even when device is
2890492bffafSMatthew Dillon 	 *	    already connected up.  It is unclear why this happens.
2891492bffafSMatthew Dillon 	 *	    Depending on the state of the device detect this can
2892492bffafSMatthew Dillon 	 *	    cause us to go into harsh reinit or hot-plug insertion
2893492bffafSMatthew Dillon 	 *	    mode.
2894492bffafSMatthew Dillon 	 *
2895492bffafSMatthew Dillon 	 * WARNING: PCS errors can be repetitive (e.g. unsolicited COMRESET
2896492bffafSMatthew Dillon 	 *	    continues to flow in from the device), we must clear the
2897492bffafSMatthew Dillon 	 *	    interrupt in all cases and enforce a delay to prevent
2898492bffafSMatthew Dillon 	 *	    a livelock and give the port time to settle down.
2899492bffafSMatthew Dillon 	 *	    Only print something if we aren't in INIT/HARD-RESET.
2900258223a3SMatthew Dillon 	 */
2901258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
29023d102df7SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
29033d102df7SMatthew Dillon 			    is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2904492bffafSMatthew Dillon 		/*
2905492bffafSMatthew Dillon 		 * Try to clear the error.  Because of the repetitiveness
2906492bffafSMatthew Dillon 		 * of this interrupt avoid any harsh action if the port is
2907492bffafSMatthew Dillon 		 * already in the init or hard-reset probe state.
2908492bffafSMatthew Dillon 		 */
2909492bffafSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
2910492bffafSMatthew Dillon 		/* (AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) */
2911492bffafSMatthew Dillon 
2912493d3201SMatthew Dillon 		/*
2913493d3201SMatthew Dillon 		 * Ignore PCS/PRCS errors during probes (but still clear the
2914493d3201SMatthew Dillon 		 * interrupt to avoid a livelock).  The AMD 880/890/SB850
2915493d3201SMatthew Dillon 		 * chipsets do not mask PCS/PRCS internally during reset
2916493d3201SMatthew Dillon 		 * sequences.
2917493d3201SMatthew Dillon 		 */
29185502cf24SMatthew Dillon 		if (ap->ap_flags & AP_F_IN_RESET)
2919493d3201SMatthew Dillon 			goto skip_pcs;
2920493d3201SMatthew Dillon 
2921492bffafSMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT ||
2922492bffafSMatthew Dillon 		    ap->ap_probe == ATA_PROBE_NEED_HARD_RESET) {
2923cec07d75SMatthew Dillon 			is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2924492bffafSMatthew Dillon 			need = NEED_NOTHING;
2925492bffafSMatthew Dillon 			ahci_os_sleep(1000);
2926492bffafSMatthew Dillon 			goto failall;
2927492bffafSMatthew Dillon 		}
2928c3783d8fSzrj 		kprintf("%s: Transient Errors: %pb%i (%d)\n",
2929c3783d8fSzrj 			PORTNAME(ap), AHCI_PFMT_IS, is, ap->ap_probe);
2930492bffafSMatthew Dillon 		is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2931492bffafSMatthew Dillon 		ahci_os_sleep(200);
2932492bffafSMatthew Dillon 
2933492bffafSMatthew Dillon 		/*
2934492bffafSMatthew Dillon 		 * Stop the port and figure out what to do next.
2935492bffafSMatthew Dillon 		 */
293622181ab7SMatthew Dillon 		ahci_port_stop(ap, 0);
2937492bffafSMatthew Dillon 		stopped = 1;
29380be9576aSMatthew Dillon 
2939258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2940258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
2941492bffafSMatthew Dillon 			/*
2942492bffafSMatthew Dillon 			 * Device detect
2943492bffafSMatthew Dillon 			 */
294412feb904SMatthew Dillon 			if (ap->ap_probe == ATA_PROBE_FAILED) {
294522181ab7SMatthew Dillon 				need = NEED_HOTPLUG_INSERT;
294622181ab7SMatthew Dillon 				goto fatal;
2947258223a3SMatthew Dillon 			}
294822181ab7SMatthew Dillon 			need = NEED_RESTART;
2949258223a3SMatthew Dillon 			break;
2950492bffafSMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
2951492bffafSMatthew Dillon 			/*
2952492bffafSMatthew Dillon 			 * Device not communicating.  AMD parts seem to
2953492bffafSMatthew Dillon 			 * like to throw this error on initialization
2954492bffafSMatthew Dillon 			 * for no reason that I can fathom.
2955492bffafSMatthew Dillon 			 */
2956492bffafSMatthew Dillon 			kprintf("%s: Device present but not communicating, "
2957492bffafSMatthew Dillon 				"attempting port restart\n",
2958492bffafSMatthew Dillon 				PORTNAME(ap));
2959492bffafSMatthew Dillon 			need = NEED_REINIT;
2960492bffafSMatthew Dillon 			goto fatal;
2961258223a3SMatthew Dillon 		default:
29620be9576aSMatthew Dillon 			if (ap->ap_probe != ATA_PROBE_FAILED) {
296322181ab7SMatthew Dillon 				need = NEED_HOTPLUG_REMOVE;
296422181ab7SMatthew Dillon 				goto fatal;
2965258223a3SMatthew Dillon 			}
296622181ab7SMatthew Dillon 			need = NEED_RESTART;
2967258223a3SMatthew Dillon 			break;
2968258223a3SMatthew Dillon 		}
2969493d3201SMatthew Dillon skip_pcs:
2970493d3201SMatthew Dillon 		;
2971258223a3SMatthew Dillon 	}
2972258223a3SMatthew Dillon 
297322181ab7SMatthew Dillon 	/*
2974f4553de1SMatthew Dillon 	 * Check for remaining errors - they are fatal. (blockable)
297522181ab7SMatthew Dillon 	 */
2976258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2977258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2978cec07d75SMatthew Dillon 		u_int32_t serr;
2979cec07d75SMatthew Dillon 
2980cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2981cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2982cec07d75SMatthew Dillon 				  AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2983cec07d75SMatthew Dillon 				  AHCI_PREG_IS_UFS));
2984cec07d75SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2985c3783d8fSzrj 		kprintf("%s: Unrecoverable errors (IS: %pb%i, SERR: %pb%i), "
2986c3783d8fSzrj 			"disabling port.\n", PORTNAME(ap),
2987c3783d8fSzrj 			AHCI_PFMT_IS, is, AHCI_PFMT_SERR, serr);
2988831bc9e3SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2989831bc9e3SMatthew Dillon 			AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2990831bc9e3SMatthew Dillon 		        AHCI_PREG_IS_UFS);
2991492bffafSMatthew Dillon 
2992492bffafSMatthew Dillon 		/*
2993492bffafSMatthew Dillon 		 * Fail all commands but then what?  For now try to
2994492bffafSMatthew Dillon 		 * reinitialize the port.
2995492bffafSMatthew Dillon 		 */
2996492bffafSMatthew Dillon 		need = NEED_REINIT;
2997258223a3SMatthew Dillon 		goto fatal;
2998258223a3SMatthew Dillon 	}
2999258223a3SMatthew Dillon 
300022181ab7SMatthew Dillon 	/*
300122181ab7SMatthew Dillon 	 * Fail all outstanding commands if we know the port won't recover.
30021980eff3SMatthew Dillon 	 *
30031980eff3SMatthew Dillon 	 * We may have a ccb_at if the failed command is known and was
30041980eff3SMatthew Dillon 	 * being sent to a device over a port multiplier (PM).  In this
30051980eff3SMatthew Dillon 	 * case if the port itself has not completely failed we fail just
30061980eff3SMatthew Dillon 	 * the commands related to that target.
300712feb904SMatthew Dillon 	 *
300812feb904SMatthew Dillon 	 * ci_saved contains the mask of active commands as of when the
300912feb904SMatthew Dillon 	 * error occured, prior to any port stops.
301022181ab7SMatthew Dillon 	 */
3011258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
3012258223a3SMatthew Dillon fatal:
3013258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
301412feb904SMatthew Dillon failall:
3015492bffafSMatthew Dillon 		ahci_port_stop(ap, 0);
3016492bffafSMatthew Dillon 		stopped = 1;
3017258223a3SMatthew Dillon 
30181980eff3SMatthew Dillon 		/*
3019492bffafSMatthew Dillon 		 * Error all the active slots not already errored.
30201980eff3SMatthew Dillon 		 */
302112feb904SMatthew Dillon 		ci_masked = ci_saved & *active & ~ap->ap_expired;
3022492bffafSMatthew Dillon 		if (ci_masked) {
3023492bffafSMatthew Dillon 			kprintf("%s: Failing all commands: %08x\n",
3024492bffafSMatthew Dillon 				PORTNAME(ap), ci_masked);
3025492bffafSMatthew Dillon 		}
3026492bffafSMatthew Dillon 
3027258223a3SMatthew Dillon 		while (ci_masked) {
3028258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
3029258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
303012feb904SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_TIMEOUT;
303112feb904SMatthew Dillon 			ap->ap_expired |= 1 << slot;
303212feb904SMatthew Dillon 			ci_saved &= ~(1 << slot);
303312feb904SMatthew Dillon 			ci_masked &= ~(1 << slot);
30341980eff3SMatthew Dillon 		}
3035258223a3SMatthew Dillon 
303612feb904SMatthew Dillon 		/*
303712feb904SMatthew Dillon 		 * Clear bits in ci_saved (cause completions to be run)
303812feb904SMatthew Dillon 		 * for all slots which are not active.
303912feb904SMatthew Dillon 		 */
3040258223a3SMatthew Dillon 		ci_saved &= ~*active;
3041258223a3SMatthew Dillon 
3042258223a3SMatthew Dillon 		/*
3043258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
3044258223a3SMatthew Dillon 		 *
3045258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
3046258223a3SMatthew Dillon 		 * a livelock.
3047258223a3SMatthew Dillon 		 */
3048258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
304922181ab7SMatthew Dillon 			if (need == NEED_RESTART)
305022181ab7SMatthew Dillon 				need = NEED_NOTHING;
3051258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
3052258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
3053258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
3054258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
3055258223a3SMatthew Dillon 		}
3056258223a3SMatthew Dillon 	}
3057258223a3SMatthew Dillon 
3058258223a3SMatthew Dillon 	/*
3059492bffafSMatthew Dillon 	 * If we are stopped the AHCI chipset is supposed to have cleared
3060492bffafSMatthew Dillon 	 * CI and SACT.  Did it?  If it didn't we try very hard to clear
3061492bffafSMatthew Dillon 	 * the fields otherwise we may end up completing CCBs which are
3062492bffafSMatthew Dillon 	 * actually still active.
3063492bffafSMatthew Dillon 	 *
3064492bffafSMatthew Dillon 	 * IFS errors on (at least) AMD chipsets create this confusion.
3065492bffafSMatthew Dillon 	 */
3066492bffafSMatthew Dillon 	if (stopped) {
3067492bffafSMatthew Dillon 		u_int32_t mask;
3068492bffafSMatthew Dillon 		if ((mask = ahci_pactive(ap)) != 0) {
3069492bffafSMatthew Dillon 			kprintf("%s: chipset failed to clear "
3070492bffafSMatthew Dillon 				"active cmds %08x\n",
3071492bffafSMatthew Dillon 				PORTNAME(ap), mask);
3072492bffafSMatthew Dillon 			ahci_port_start(ap);
3073492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
3074492bffafSMatthew Dillon 			if ((mask = ahci_pactive(ap)) != 0) {
3075492bffafSMatthew Dillon 				kprintf("%s: unable to prod the chip into "
3076492bffafSMatthew Dillon 					"clearing active cmds %08x\n",
3077492bffafSMatthew Dillon 					PORTNAME(ap), mask);
3078492bffafSMatthew Dillon 				/* what do we do now? */
3079492bffafSMatthew Dillon 			}
3080492bffafSMatthew Dillon 		}
3081492bffafSMatthew Dillon 	}
3082492bffafSMatthew Dillon 
3083492bffafSMatthew Dillon 	/*
3084f4553de1SMatthew Dillon 	 * CCB completion (non blocking).
3085f4553de1SMatthew Dillon 	 *
3086258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
3087258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
3088258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
3089cf5f3a81SMatthew Dillon 	 *
3090cf5f3a81SMatthew Dillon 	 * Any active bits not saved are completed within the restrictions
3091cf5f3a81SMatthew Dillon 	 * imposed by the caller.
3092258223a3SMatthew Dillon 	 */
30933209f581SMatthew Dillon 	ci_masked = ~ci_saved & *active;
3094258223a3SMatthew Dillon 	while (ci_masked) {
3095258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
3096258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
3097258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
3098258223a3SMatthew Dillon 
3099258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
3100258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
3101258223a3SMatthew Dillon 		    " (error)" : "");
3102258223a3SMatthew Dillon 
3103258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
3104258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
3105258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
3106258223a3SMatthew Dillon 
3107258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
3108258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
3109258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
3110258223a3SMatthew Dillon 
3111258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
3112258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
3113258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
3114258223a3SMatthew Dillon 
3115258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
31161980eff3SMatthew Dillon 		if (active == &ap->ap_active) {
31171980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
31181980eff3SMatthew Dillon 			--ap->ap_active_cnt;
31191980eff3SMatthew Dillon 		}
31204c339a5fSMatthew Dillon 
31214c339a5fSMatthew Dillon 		/*
31224c339a5fSMatthew Dillon 		 * Complete the ccb.  If the ccb was marked expired it
31234c339a5fSMatthew Dillon 		 * was probably already removed from the command processor,
31244c339a5fSMatthew Dillon 		 * so don't take the clear ci_saved bit as meaning the
31254c339a5fSMatthew Dillon 		 * command actually succeeded, it didn't.
31264c339a5fSMatthew Dillon 		 */
31274c339a5fSMatthew Dillon 		if (ap->ap_expired & (1 << ccb->ccb_slot)) {
312876497a9cSMatthew Dillon 			ap->ap_expired &= ~(1 << ccb->ccb_slot);
31294c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_TIMEOUT;
3130258223a3SMatthew Dillon 			ccb->ccb_done(ccb);
31314c339a5fSMatthew Dillon 			ccb->ccb_xa.complete(&ccb->ccb_xa);
31324c339a5fSMatthew Dillon 		} else {
313312feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
31344c339a5fSMatthew Dillon 				ccb->ccb_xa.state = ATA_S_COMPLETE;
313512feb904SMatthew Dillon 				if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
31368119d5f5SMatthew Dillon 					int fis_target;
31378119d5f5SMatthew Dillon 
31388119d5f5SMatthew Dillon 					ccb_at = ccb->ccb_xa.at;
31398119d5f5SMatthew Dillon 					if (ccb_at &&
31408119d5f5SMatthew Dillon 					    (ap->ap_flags & AP_F_FBSS_ENABLED))
31418119d5f5SMatthew Dillon 						fis_target = ccb_at->at_target;
31428119d5f5SMatthew Dillon 					else
31438119d5f5SMatthew Dillon 						fis_target = 0;
314412feb904SMatthew Dillon 					memcpy(&ccb->ccb_xa.rfis,
31458119d5f5SMatthew Dillon 					       ap->ap_rfis[fis_target].rfis,
314612feb904SMatthew Dillon 					       sizeof(struct ata_fis_d2h));
314712feb904SMatthew Dillon 					if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
314812feb904SMatthew Dillon 						ccb->ccb_xa.state = ATA_S_ERROR;
314912feb904SMatthew Dillon 				}
315012feb904SMatthew Dillon 			}
31514c339a5fSMatthew Dillon 			ccb->ccb_done(ccb);
31524c339a5fSMatthew Dillon 		}
3153258223a3SMatthew Dillon 	}
3154258223a3SMatthew Dillon 
3155f4553de1SMatthew Dillon 	/*
3156f4553de1SMatthew Dillon 	 * Cleanup.  Will not be set if non-blocking.
3157f4553de1SMatthew Dillon 	 */
315822181ab7SMatthew Dillon 	switch(need) {
3159f3de36f7SMatthew Dillon 	case NEED_NOTHING:
3160f3de36f7SMatthew Dillon 		/*
3161f3de36f7SMatthew Dillon 		 * If operating normally and not stopped the interrupt was
3162f3de36f7SMatthew Dillon 		 * probably just a normal completion and we may be able to
3163f3de36f7SMatthew Dillon 		 * issue more commands.
3164f3de36f7SMatthew Dillon 		 */
3165f3de36f7SMatthew Dillon 		if (stopped == 0 && ap->ap_state != AP_S_FATAL_ERROR)
3166f3de36f7SMatthew Dillon 			ahci_issue_pending_commands(ap, NULL);
3167f3de36f7SMatthew Dillon 		break;
316822181ab7SMatthew Dillon 	case NEED_RESTART:
316922181ab7SMatthew Dillon 		/*
317022181ab7SMatthew Dillon 		 * A recoverable error occured and we can restart outstanding
317122181ab7SMatthew Dillon 		 * commands on the port.
317222181ab7SMatthew Dillon 		 */
317312feb904SMatthew Dillon 		ci_saved &= ~ap->ap_expired;
3174258223a3SMatthew Dillon 		if (ci_saved) {
317512feb904SMatthew Dillon 			kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
31764c339a5fSMatthew Dillon 			ahci_issue_saved_commands(ap, ci_saved);
3177258223a3SMatthew Dillon 		}
3178492bffafSMatthew Dillon 
3179492bffafSMatthew Dillon 		/*
3180492bffafSMatthew Dillon 		 * Potentially issue new commands if not in a failed
3181492bffafSMatthew Dillon 		 * state.
3182492bffafSMatthew Dillon 		 */
3183492bffafSMatthew Dillon 		if (ap->ap_state != AP_S_FATAL_ERROR) {
3184492bffafSMatthew Dillon 			ahci_port_start(ap);
3185492bffafSMatthew Dillon 			ahci_issue_pending_commands(ap, NULL);
3186492bffafSMatthew Dillon 		}
3187492bffafSMatthew Dillon 		break;
3188492bffafSMatthew Dillon 	case NEED_REINIT:
3189492bffafSMatthew Dillon 		/*
3190492bffafSMatthew Dillon 		 * Something horrible happened to the port and we
3191492bffafSMatthew Dillon 		 * need to reinitialize it.
3192492bffafSMatthew Dillon 		 */
3193492bffafSMatthew Dillon 		kprintf("%s: REINIT - Attempting to reinitialize the port "
3194492bffafSMatthew Dillon 			"after it had a horrible accident\n",
3195492bffafSMatthew Dillon 			PORTNAME(ap));
3196492bffafSMatthew Dillon 		ap->ap_flags |= AP_F_IN_RESET;
3197492bffafSMatthew Dillon 		ap->ap_flags |= AP_F_HARSH_REINIT;
3198492bffafSMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_INIT;
3199492bffafSMatthew Dillon 		ahci_cam_changed(ap, NULL, -1);
320022181ab7SMatthew Dillon 		break;
320122181ab7SMatthew Dillon 	case NEED_HOTPLUG_INSERT:
320222181ab7SMatthew Dillon 		/*
3203cf5f3a81SMatthew Dillon 		 * A hot-plug insertion event has occured and all
3204cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
32051980eff3SMatthew Dillon 		 *
32061980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
32071980eff3SMatthew Dillon 		 * resetting the port.
320822181ab7SMatthew Dillon 		 */
32091980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
321022181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device inserted\n",
321122181ab7SMatthew Dillon 				PORTNAME(ap));
32123209f581SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_INIT;
32133209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
32141980eff3SMatthew Dillon 		}
321522181ab7SMatthew Dillon 		break;
321622181ab7SMatthew Dillon 	case NEED_HOTPLUG_REMOVE:
3217cf5f3a81SMatthew Dillon 		/*
3218cf5f3a81SMatthew Dillon 		 * A hot-plug removal event has occured and all
3219cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
32201980eff3SMatthew Dillon 		 *
32211980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
32221980eff3SMatthew Dillon 		 * resetting the port.
3223cf5f3a81SMatthew Dillon 		 */
32241980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
322522181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device removed\n",
322622181ab7SMatthew Dillon 				PORTNAME(ap));
3227cf5f3a81SMatthew Dillon 			ahci_port_hardstop(ap);
32283209f581SMatthew Dillon 			/* ap_probe set to failed */
32293209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
32301980eff3SMatthew Dillon 		}
323122181ab7SMatthew Dillon 		break;
323222181ab7SMatthew Dillon 	default:
323322181ab7SMatthew Dillon 		break;
3234258223a3SMatthew Dillon 	}
3235258223a3SMatthew Dillon }
3236258223a3SMatthew Dillon 
3237258223a3SMatthew Dillon struct ahci_ccb *
3238258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
3239258223a3SMatthew Dillon {
3240258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
3241258223a3SMatthew Dillon 
3242258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3243258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
3244258223a3SMatthew Dillon 	if (ccb != NULL) {
3245d16d3400SMatthew Dillon 		KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3246258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
3247258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
3248258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
3249492bffafSMatthew Dillon 		ccb->ccb_xa.flags = 0;
32501980eff3SMatthew Dillon 		ccb->ccb_xa.at = NULL;
3251258223a3SMatthew Dillon 	}
3252258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3253258223a3SMatthew Dillon 
3254258223a3SMatthew Dillon 	return (ccb);
3255258223a3SMatthew Dillon }
3256258223a3SMatthew Dillon 
3257258223a3SMatthew Dillon void
3258258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
3259258223a3SMatthew Dillon {
3260258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
3261258223a3SMatthew Dillon 
3262d16d3400SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state != ATA_S_PUT);
3263d16d3400SMatthew Dillon 	KKASSERT((ap->ap_sactive & (1 << ccb->ccb_slot)) == 0);
3264258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
3265dcdc0770SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
3266bb79834dSMatthew Dillon 	++ccb->ccb_xa.serial;
3267258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
3268258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
3269258223a3SMatthew Dillon }
3270258223a3SMatthew Dillon 
3271258223a3SMatthew Dillon struct ahci_ccb *
3272258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
3273258223a3SMatthew Dillon {
3274258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
3275258223a3SMatthew Dillon 	u_int32_t sact;
3276b012a2caSMatthew Dillon 	u_int32_t ci;
3277258223a3SMatthew Dillon 
3278258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
3279b012a2caSMatthew Dillon 
3280b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3281258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
3282192ee1d0SMatthew Dillon 		if (sact != 0) {
3283192ee1d0SMatthew Dillon 			kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
3284192ee1d0SMatthew Dillon 				PORTNAME(ap), sact);
3285192ee1d0SMatthew Dillon 		}
3286b012a2caSMatthew Dillon 	}
3287b012a2caSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
3288b012a2caSMatthew Dillon 	if (ci) {
3289b012a2caSMatthew Dillon 		kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
3290b012a2caSMatthew Dillon 			ap->ap_name, ci);
3291b012a2caSMatthew Dillon 	}
3292b012a2caSMatthew Dillon 	KKASSERT(ci == 0);
3293baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
3294baef7501SMatthew Dillon 	ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
3295258223a3SMatthew Dillon 
3296258223a3SMatthew Dillon 	/* Save outstanding command state. */
3297258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
3298258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
3299258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
3300258223a3SMatthew Dillon 
3301258223a3SMatthew Dillon 	/*
3302258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
3303258223a3SMatthew Dillon 	 * run prematurely.
3304258223a3SMatthew Dillon 	 */
3305258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
3306258223a3SMatthew Dillon 
3307258223a3SMatthew Dillon 	/*
3308258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
3309258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
3310258223a3SMatthew Dillon 	 */
33111067474aSMatthew Dillon 	err_ccb = ap->ap_err_ccb;
3312258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
3313258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
3314258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
3315258223a3SMatthew Dillon 
3316258223a3SMatthew Dillon 	return err_ccb;
3317258223a3SMatthew Dillon }
3318258223a3SMatthew Dillon 
3319258223a3SMatthew Dillon void
3320258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
3321258223a3SMatthew Dillon {
3322258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
3323258223a3SMatthew Dillon 	u_int32_t sact;
33245f8c1efdSMatthew Dillon 	u_int32_t ci;
3325258223a3SMatthew Dillon 
3326baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
3327baef7501SMatthew Dillon 
33285f8c1efdSMatthew Dillon 	/*
33295f8c1efdSMatthew Dillon 	 * No commands may be active on the chip
33305f8c1efdSMatthew Dillon 	 */
3331b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
3332258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
33335f8c1efdSMatthew Dillon 		if (sact) {
3334ed20d0e3SSascha Wildner 			panic("ahci_port_err_ccb(%d) but SACT %08x != 0",
33355f8c1efdSMatthew Dillon 			      ccb->ccb_slot, sact);
3336258223a3SMatthew Dillon 		}
3337b012a2caSMatthew Dillon 	}
33385f8c1efdSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
33395f8c1efdSMatthew Dillon 	if (ci) {
3340cf5f3a81SMatthew Dillon 		panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
3341cf5f3a81SMatthew Dillon 		      "(act=%08x sact=%08x)\n",
3342cf5f3a81SMatthew Dillon 		      ccb->ccb_slot, ci,
3343cf5f3a81SMatthew Dillon 		      ap->ap_active, ap->ap_sactive);
33445f8c1efdSMatthew Dillon 	}
3345258223a3SMatthew Dillon 
33461067474aSMatthew Dillon 	KKASSERT(ccb == ap->ap_err_ccb);
3347258223a3SMatthew Dillon 
3348258223a3SMatthew Dillon 	/* Restore outstanding command state */
3349258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
3350258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
3351258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
3352258223a3SMatthew Dillon 
3353baef7501SMatthew Dillon 	ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
3354258223a3SMatthew Dillon }
3355258223a3SMatthew Dillon 
33561980eff3SMatthew Dillon /*
33571980eff3SMatthew Dillon  * Read log page to get NCQ error.
33581980eff3SMatthew Dillon  *
33591980eff3SMatthew Dillon  * NOTE: NCQ not currently supported on port multipliers. XXX
33601980eff3SMatthew Dillon  */
3361258223a3SMatthew Dillon int
336212feb904SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int target)
3363258223a3SMatthew Dillon {
336412feb904SMatthew Dillon 	struct ata_log_page_10h	*log;
3365258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
3366e1014452SMatthew Dillon 	struct ahci_ccb		*ccb2;
3367258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
3368258223a3SMatthew Dillon 	struct ata_fis_h2d	*fis;
336912feb904SMatthew Dillon 	int			err_slot;
3370258223a3SMatthew Dillon 
337112feb904SMatthew Dillon 	if (bootverbose) {
337212feb904SMatthew Dillon 		kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
337312feb904SMatthew Dillon 			target);
337412feb904SMatthew Dillon 	}
3375258223a3SMatthew Dillon 
337612feb904SMatthew Dillon 	/*
337712feb904SMatthew Dillon 	 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
337812feb904SMatthew Dillon 	 *
337912feb904SMatthew Dillon 	 * Getting err_ccb clears active/sactive/active_cnt, putting
338012feb904SMatthew Dillon 	 * it back restores the fields.
338112feb904SMatthew Dillon 	 */
3382258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
338312feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
3384258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
3385258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
338612feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
3387b012a2caSMatthew Dillon 	ccb->ccb_xa.at = ap->ap_ata[target];
3388258223a3SMatthew Dillon 
3389258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
339012feb904SMatthew Dillon 	bzero(fis, sizeof(*fis));
3391258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
339212feb904SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD | target;
3393258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
3394258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
3395258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
3396258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
3397258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
3398258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
3399258223a3SMatthew Dillon 	fis->device = 0;
3400258223a3SMatthew Dillon 
340112feb904SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
3402258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
3403258223a3SMatthew Dillon 
3404258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
340512feb904SMatthew Dillon 		err_slot = -1;
3406258223a3SMatthew Dillon 		goto err;
3407258223a3SMatthew Dillon 	}
3408258223a3SMatthew Dillon 
3409258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
341012feb904SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
341112feb904SMatthew Dillon 		err_slot = -1;
3412258223a3SMatthew Dillon 		ahci_unload_prdt(ccb);
341312feb904SMatthew Dillon 		goto err;
341412feb904SMatthew Dillon 	}
341512feb904SMatthew Dillon 	ahci_unload_prdt(ccb);
3416258223a3SMatthew Dillon 
341712feb904SMatthew Dillon 	/*
341812feb904SMatthew Dillon 	 * Success, extract failed register set and tags from the scratch
341912feb904SMatthew Dillon 	 * space.
342012feb904SMatthew Dillon 	 */
3421258223a3SMatthew Dillon 	log = (struct ata_log_page_10h *)ap->ap_err_scratch;
3422258223a3SMatthew Dillon 	if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
3423258223a3SMatthew Dillon 		/* Not queued bit was set - wasn't an NCQ error? */
342412feb904SMatthew Dillon 		kprintf("%s: read NCQ error page, but not an NCQ error?\n",
3425258223a3SMatthew Dillon 			PORTNAME(ap));
342612feb904SMatthew Dillon 		err_slot = -1;
3427258223a3SMatthew Dillon 	} else {
3428258223a3SMatthew Dillon 		/* Copy back the log record as a D2H register FIS. */
342912feb904SMatthew Dillon 		err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
3430258223a3SMatthew Dillon 
3431e1014452SMatthew Dillon 		ccb2 = &ap->ap_ccbs[err_slot];
3432e1014452SMatthew Dillon 		if (ccb2->ccb_xa.state == ATA_S_ONCHIP) {
343312feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d\n",
3434e1014452SMatthew Dillon 				ATANAME(ap, ccb2->ccb_xa.at),
343512feb904SMatthew Dillon 				err_slot);
3436e1014452SMatthew Dillon 			memcpy(&ccb2->ccb_xa.rfis, &log->err_regs,
3437258223a3SMatthew Dillon 				sizeof(struct ata_fis_d2h));
3438e1014452SMatthew Dillon 			ccb2->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
3439e1014452SMatthew Dillon 			ccb2->ccb_xa.rfis.flags = 0;
344012feb904SMatthew Dillon 		} else {
344112feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d, "
344212feb904SMatthew Dillon 				"slot does not match any cmds\n",
3443e1014452SMatthew Dillon 				ATANAME(ccb2->ccb_port, ccb2->ccb_xa.at),
344412feb904SMatthew Dillon 				err_slot);
344512feb904SMatthew Dillon 			err_slot = -1;
3446258223a3SMatthew Dillon 		}
3447258223a3SMatthew Dillon 	}
344812feb904SMatthew Dillon err:
344912feb904SMatthew Dillon 	ahci_put_err_ccb(ccb);
345012feb904SMatthew Dillon 	kprintf("%s: DONE log page target %d err_slot=%d\n",
345112feb904SMatthew Dillon 		PORTNAME(ap), target, err_slot);
345212feb904SMatthew Dillon 	return (err_slot);
3453258223a3SMatthew Dillon }
3454258223a3SMatthew Dillon 
3455258223a3SMatthew Dillon /*
3456258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
3457258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
3458258223a3SMatthew Dillon  * single physical base address.
3459258223a3SMatthew Dillon  */
3460258223a3SMatthew Dillon struct ahci_dmamem *
3461258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
3462258223a3SMatthew Dillon {
3463258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
3464258223a3SMatthew Dillon 	int	error;
3465258223a3SMatthew Dillon 
3466258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
3467258223a3SMatthew Dillon 
3468258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
3469258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
3470258223a3SMatthew Dillon 	if (error == 0) {
3471258223a3SMatthew Dillon 		adm->adm_tag = tag;
3472258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
3473258223a3SMatthew Dillon 					adm->adm_kva,
3474258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
3475258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
3476258223a3SMatthew Dillon 					0);
3477258223a3SMatthew Dillon 	}
3478258223a3SMatthew Dillon 	if (error) {
3479258223a3SMatthew Dillon 		if (adm->adm_map) {
3480258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
3481258223a3SMatthew Dillon 			adm->adm_map = NULL;
3482258223a3SMatthew Dillon 			adm->adm_tag = NULL;
3483258223a3SMatthew Dillon 			adm->adm_kva = NULL;
3484258223a3SMatthew Dillon 		}
3485258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
3486258223a3SMatthew Dillon 		adm = NULL;
3487258223a3SMatthew Dillon 	}
3488258223a3SMatthew Dillon 	return (adm);
3489258223a3SMatthew Dillon }
3490258223a3SMatthew Dillon 
3491258223a3SMatthew Dillon static
3492258223a3SMatthew Dillon void
3493258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
3494258223a3SMatthew Dillon {
3495258223a3SMatthew Dillon 	KKASSERT(error == 0);
3496258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
3497258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
3498258223a3SMatthew Dillon }
3499258223a3SMatthew Dillon 
3500258223a3SMatthew Dillon 
3501258223a3SMatthew Dillon void
3502258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
3503258223a3SMatthew Dillon {
3504258223a3SMatthew Dillon 	if (adm->adm_map) {
3505258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
3506258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
3507258223a3SMatthew Dillon 		adm->adm_map = NULL;
3508258223a3SMatthew Dillon 		adm->adm_tag = NULL;
3509258223a3SMatthew Dillon 		adm->adm_kva = NULL;
3510258223a3SMatthew Dillon 	}
3511258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
3512258223a3SMatthew Dillon }
3513258223a3SMatthew Dillon 
3514258223a3SMatthew Dillon u_int32_t
3515258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
3516258223a3SMatthew Dillon {
3517258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3518258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
3519258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
3520258223a3SMatthew Dillon }
3521258223a3SMatthew Dillon 
3522258223a3SMatthew Dillon void
3523258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
3524258223a3SMatthew Dillon {
3525258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
3526258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
3527258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
3528258223a3SMatthew Dillon }
3529258223a3SMatthew Dillon 
3530258223a3SMatthew Dillon u_int32_t
3531258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
3532258223a3SMatthew Dillon {
3533258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3534258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
3535258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
3536258223a3SMatthew Dillon }
3537258223a3SMatthew Dillon 
3538258223a3SMatthew Dillon void
3539258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
3540258223a3SMatthew Dillon {
3541258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
3542258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
3543258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
3544258223a3SMatthew Dillon }
3545258223a3SMatthew Dillon 
3546831bc9e3SMatthew Dillon /*
3547831bc9e3SMatthew Dillon  * Wait up to (timeout) milliseconds for the masked port register to
3548831bc9e3SMatthew Dillon  * match the target.
3549831bc9e3SMatthew Dillon  *
3550831bc9e3SMatthew Dillon  * Timeout is in milliseconds.
3551831bc9e3SMatthew Dillon  */
3552258223a3SMatthew Dillon int
3553cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
3554cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
3555258223a3SMatthew Dillon {
3556831bc9e3SMatthew Dillon 	int	t;
3557258223a3SMatthew Dillon 
3558831bc9e3SMatthew Dillon 	/*
3559831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
3560831bc9e3SMatthew Dillon 	 */
3561831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
3562258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
3563258223a3SMatthew Dillon 			return (0);
3564831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
3565258223a3SMatthew Dillon 	}
3566258223a3SMatthew Dillon 
3567831bc9e3SMatthew Dillon 	do {
3568831bc9e3SMatthew Dillon 		timeout -= ahci_os_softsleep();
3569831bc9e3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
3570831bc9e3SMatthew Dillon 			return (0);
3571831bc9e3SMatthew Dillon 	} while (timeout > 0);
3572831bc9e3SMatthew Dillon 	return (1);
3573831bc9e3SMatthew Dillon }
3574831bc9e3SMatthew Dillon 
3575831bc9e3SMatthew Dillon int
3576831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
3577831bc9e3SMatthew Dillon 	     u_int32_t target)
3578831bc9e3SMatthew Dillon {
3579831bc9e3SMatthew Dillon 	int	t;
3580831bc9e3SMatthew Dillon 
3581831bc9e3SMatthew Dillon 	/*
3582831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
3583831bc9e3SMatthew Dillon 	 */
3584831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
3585831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
3586831bc9e3SMatthew Dillon 			return (0);
3587831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
3588831bc9e3SMatthew Dillon 	}
3589831bc9e3SMatthew Dillon 
3590831bc9e3SMatthew Dillon 	/*
3591831bc9e3SMatthew Dillon 	 * And one millisecond the slow way
3592831bc9e3SMatthew Dillon 	 */
3593831bc9e3SMatthew Dillon 	t = 1000;
3594831bc9e3SMatthew Dillon 	do {
3595831bc9e3SMatthew Dillon 		t -= ahci_os_softsleep();
3596831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
3597831bc9e3SMatthew Dillon 			return (0);
3598831bc9e3SMatthew Dillon 	} while (t > 0);
3599831bc9e3SMatthew Dillon 
3600258223a3SMatthew Dillon 	return (1);
3601258223a3SMatthew Dillon }
3602258223a3SMatthew Dillon 
3603831bc9e3SMatthew Dillon 
36041980eff3SMatthew Dillon /*
36051980eff3SMatthew Dillon  * Acquire an ata transfer.
36061980eff3SMatthew Dillon  *
36071980eff3SMatthew Dillon  * Pass a NULL at for direct-attached transfers, and a non-NULL at for
36081980eff3SMatthew Dillon  * targets that go through the port multiplier.
36091980eff3SMatthew Dillon  */
3610258223a3SMatthew Dillon struct ata_xfer *
36111980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3612258223a3SMatthew Dillon {
3613258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
3614258223a3SMatthew Dillon 
3615258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
3616258223a3SMatthew Dillon 	if (ccb == NULL) {
3617258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3618258223a3SMatthew Dillon 		    PORTNAME(ap));
3619258223a3SMatthew Dillon 		return (NULL);
3620258223a3SMatthew Dillon 	}
3621258223a3SMatthew Dillon 
3622258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3623258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
3624258223a3SMatthew Dillon 
36252cc2e845SMatthew Dillon 	bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
36261980eff3SMatthew Dillon 	ccb->ccb_xa.at = at;
3627258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3628258223a3SMatthew Dillon 
3629258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
3630258223a3SMatthew Dillon }
3631258223a3SMatthew Dillon 
3632258223a3SMatthew Dillon void
3633258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
3634258223a3SMatthew Dillon {
3635258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3636258223a3SMatthew Dillon 
3637258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3638258223a3SMatthew Dillon 
3639258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
3640258223a3SMatthew Dillon }
3641258223a3SMatthew Dillon 
3642258223a3SMatthew Dillon int
3643258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
3644258223a3SMatthew Dillon {
3645258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3646258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
3647258223a3SMatthew Dillon 
3648258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
3649258223a3SMatthew Dillon 
3650258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3651258223a3SMatthew Dillon 		goto failcmd;
3652258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
3653258223a3SMatthew Dillon 
3654258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
3655258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
36561980eff3SMatthew Dillon 	if (ccb->ccb_xa.at) {
36571980eff3SMatthew Dillon 		cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
36581980eff3SMatthew Dillon 					   AHCI_CMD_LIST_FLAG_PMP_SHIFT);
36591980eff3SMatthew Dillon 	}
3660258223a3SMatthew Dillon 
3661258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
3662258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3663258223a3SMatthew Dillon 
3664258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
3665258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3666258223a3SMatthew Dillon 
3667258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
3668258223a3SMatthew Dillon 		goto failcmd;
3669258223a3SMatthew Dillon 
3670258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
3671258223a3SMatthew Dillon 
3672831bc9e3SMatthew Dillon 	if (xa->flags & ATA_F_POLL)
3673831bc9e3SMatthew Dillon 		return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3674258223a3SMatthew Dillon 
3675258223a3SMatthew Dillon 	crit_enter();
3676f4553de1SMatthew Dillon 	KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
36773209f581SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_DESIRED;
3678258223a3SMatthew Dillon 	ahci_start(ccb);
3679258223a3SMatthew Dillon 	crit_exit();
3680831bc9e3SMatthew Dillon 	return (xa->state);
3681258223a3SMatthew Dillon 
3682258223a3SMatthew Dillon failcmd:
3683258223a3SMatthew Dillon 	crit_enter();
3684258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
3685258223a3SMatthew Dillon 	xa->complete(xa);
3686258223a3SMatthew Dillon 	crit_exit();
3687831bc9e3SMatthew Dillon 	return (ATA_S_ERROR);
3688258223a3SMatthew Dillon }
3689258223a3SMatthew Dillon 
36908406cf70SSascha Wildner static void
3691258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
3692258223a3SMatthew Dillon {
3693258223a3SMatthew Dillon 	struct ata_xfer	*xa = &ccb->ccb_xa;
3694bb79834dSMatthew Dillon 	int serial;
3695258223a3SMatthew Dillon 
3696831bc9e3SMatthew Dillon 	/*
3697bb79834dSMatthew Dillon 	 * NOTE: Callout does not lock port and may race us modifying
3698831bc9e3SMatthew Dillon 	 *	 the flags, so make sure its stopped.
3699bb79834dSMatthew Dillon 	 *
3700bb79834dSMatthew Dillon 	 *	 A callout race can clean up the ccb.  A change in the
3701bb79834dSMatthew Dillon 	 *	 serial number should catch this condition.
3702831bc9e3SMatthew Dillon 	 */
3703258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3704bb79834dSMatthew Dillon 		serial = ccb->ccb_xa.serial;
3705eb67213aSMatthew Dillon 		callout_cancel(&ccb->ccb_timeout);
3706bb79834dSMatthew Dillon 		if (serial != ccb->ccb_xa.serial) {
3707bb79834dSMatthew Dillon 			kprintf("%s: Warning: timeout race ccb %p\n",
3708bb79834dSMatthew Dillon 				PORTNAME(ccb->ccb_port), ccb);
3709bb79834dSMatthew Dillon 			return;
3710bb79834dSMatthew Dillon 		}
37118dc94ed9SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3712258223a3SMatthew Dillon 	}
3713f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
371446528d33SMatthew Dillon 	ccb->ccb_port->ap_expired &= ~(1 << ccb->ccb_slot);
3715258223a3SMatthew Dillon 
371646528d33SMatthew Dillon 	KKASSERT(xa->state != ATA_S_ONCHIP && xa->state != ATA_S_PUT);
3717258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
3718258223a3SMatthew Dillon 
3719258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
3720258223a3SMatthew Dillon 		xa->complete(xa);
3721258223a3SMatthew Dillon }
3722258223a3SMatthew Dillon 
3723f4553de1SMatthew Dillon /*
3724f4553de1SMatthew Dillon  * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3725f4553de1SMatthew Dillon  * while the callout is runing.
3726f4553de1SMatthew Dillon  *
3727f4553de1SMatthew Dillon  * We can't safely get the port lock here or delay, we could block
3728f4553de1SMatthew Dillon  * the callout thread.
3729f4553de1SMatthew Dillon  */
3730258223a3SMatthew Dillon static void
3731258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
3732258223a3SMatthew Dillon {
3733258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
3734258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
3735258223a3SMatthew Dillon 
373646528d33SMatthew Dillon 	KKASSERT(ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING);
3737f4553de1SMatthew Dillon 	ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3738f4553de1SMatthew Dillon 	ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3739f4553de1SMatthew Dillon 	ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3740258223a3SMatthew Dillon }
3741258223a3SMatthew Dillon 
37424c339a5fSMatthew Dillon /*
37434c339a5fSMatthew Dillon  * Timeout code, typically called when the port command processor is running.
37444c339a5fSMatthew Dillon  *
37454c339a5fSMatthew Dillon  * We have to be very very careful here.  We cannot stop the port unless
37464c339a5fSMatthew Dillon  * CR is already clear or the only active commands remaining are timed-out
37474c339a5fSMatthew Dillon  * ones.  Otherwise stopping the port will race the command processor and
37484c339a5fSMatthew Dillon  * we can lose events.  While we can theoretically just restart everything
37494c339a5fSMatthew Dillon  * that could result in a double-issue which will not work for ATAPI commands.
37504c339a5fSMatthew Dillon  */
37511980eff3SMatthew Dillon void
3752831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3753258223a3SMatthew Dillon {
3754258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
3755258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
37564c339a5fSMatthew Dillon 	struct ata_port		*at;
3757492bffafSMatthew Dillon 	u_int32_t		ci_saved;
3758492bffafSMatthew Dillon 	u_int32_t		mask;
37594c339a5fSMatthew Dillon 	int			slot;
3760258223a3SMatthew Dillon 
37614c339a5fSMatthew Dillon 	at = ccb->ccb_xa.at;
37624c339a5fSMatthew Dillon 
37634c339a5fSMatthew Dillon 	kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
37643d102df7SMatthew Dillon 		"\tglb-status 0x%08x\n"
3765c3783d8fSzrj 		"\tcmd-reg 0x%pb%i\n"
3766c3783d8fSzrj 		"\tport_status 0x%pb%i\n"
37674c339a5fSMatthew Dillon 		"\tsactive=%08x active=%08x expired=%08x\n"
376808fb24a7SMatthew Dillon 		"\t   sact=%08x     ci=%08x\n"
3769c3783d8fSzrj 		"\t    STS=%pb%i\n",
37704c339a5fSMatthew Dillon 		ATANAME(ap, at),
37714c339a5fSMatthew Dillon 		ccb->ccb_xa.state, ccb->ccb_slot,
37723d102df7SMatthew Dillon 		ahci_read(ap->ap_sc, AHCI_REG_IS),
3773c3783d8fSzrj 		AHCI_PFMT_CMD, ahci_pread(ap, AHCI_PREG_CMD),
3774c3783d8fSzrj 		AHCI_PFMT_IS, ahci_pread(ap, AHCI_PREG_IS),
37754c339a5fSMatthew Dillon 		ap->ap_sactive, ap->ap_active, ap->ap_expired,
3776258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
377708fb24a7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI),
3778c3783d8fSzrj 		AHCI_PFMT_TFD_STS, ahci_pread(ap, AHCI_PREG_TFD)
377908fb24a7SMatthew Dillon 	);
378008fb24a7SMatthew Dillon 
3781258223a3SMatthew Dillon 
37829e145b23SMatthew Dillon 	/*
37839e145b23SMatthew Dillon 	 * NOTE: Timeout will not be running if the command was polled.
37843209f581SMatthew Dillon 	 *	 If we got here at least one of these flags should be set.
37859e145b23SMatthew Dillon 	 */
37863209f581SMatthew Dillon 	KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
37873209f581SMatthew Dillon 			      ATA_F_TIMEOUT_RUNNING));
3788f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3789258223a3SMatthew Dillon 
3790258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
3791258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
37924c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
37934c339a5fSMatthew Dillon 		ccb->ccb_done(ccb);
37944c339a5fSMatthew Dillon 		xa->complete(xa);
37954c339a5fSMatthew Dillon 		ahci_issue_pending_commands(ap, NULL);
37964c339a5fSMatthew Dillon 		return;
37974c339a5fSMatthew Dillon 	}
37984c339a5fSMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
37994c339a5fSMatthew Dillon 		kprintf("%s: Unexpected state during timeout: %d\n",
38004c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_xa.state);
38014c339a5fSMatthew Dillon 		return;
38024c339a5fSMatthew Dillon 	}
38034c339a5fSMatthew Dillon 
38044c339a5fSMatthew Dillon 	/*
38054c339a5fSMatthew Dillon 	 * Ok, we can only get this command off the chip if CR is inactive
38064c339a5fSMatthew Dillon 	 * or if the only commands running on the chip are all expired.
38074c339a5fSMatthew Dillon 	 * Otherwise we have to wait until the port is in a safe state.
3808eb9f4c83SMatthew Dillon 	 * Use the ST bit here instead of the CR bit in case the CR bit is
3809eb9f4c83SMatthew Dillon 	 * not implemented via the F_IGN_CR quirk.
38104c339a5fSMatthew Dillon 	 *
38114c339a5fSMatthew Dillon 	 * Do not set state here, it will cause polls to return when the
38124c339a5fSMatthew Dillon 	 * ccb is not yet off the chip.
38134c339a5fSMatthew Dillon 	 */
38144c339a5fSMatthew Dillon 	ap->ap_expired |= 1 << ccb->ccb_slot;
38154c339a5fSMatthew Dillon 
3816eb9f4c83SMatthew Dillon 	if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_ST) &&
38174c339a5fSMatthew Dillon 	    (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
38184c339a5fSMatthew Dillon 		/*
38194c339a5fSMatthew Dillon 		 * If using FBSS or NCQ we can't safely stop the port
38204c339a5fSMatthew Dillon 		 * right now.
38214c339a5fSMatthew Dillon 		 */
38224c339a5fSMatthew Dillon 		kprintf("%s: Deferred timeout until its safe, slot %d\n",
38234c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_slot);
38244c339a5fSMatthew Dillon 		return;
38254c339a5fSMatthew Dillon 	}
38264c339a5fSMatthew Dillon 
38274c339a5fSMatthew Dillon 	/*
38284c339a5fSMatthew Dillon 	 * We can safely stop the port and process all expired ccb's,
38294c339a5fSMatthew Dillon 	 * which will include our current ccb.
38304c339a5fSMatthew Dillon 	 */
38314c339a5fSMatthew Dillon 	ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
38324c339a5fSMatthew Dillon 				      ahci_pread(ap, AHCI_PREG_CI);
38334c339a5fSMatthew Dillon 	ahci_port_stop(ap, 0);
38344c339a5fSMatthew Dillon 
38354c339a5fSMatthew Dillon 	while (ap->ap_expired) {
38364c339a5fSMatthew Dillon 		slot = ffs(ap->ap_expired) - 1;
38374c339a5fSMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
38384c339a5fSMatthew Dillon 		ci_saved &= ~(1 << slot);
38394c339a5fSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
38404c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
38414c339a5fSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
38424c339a5fSMatthew Dillon 			KKASSERT(ap->ap_sactive & (1 << slot));
38434c339a5fSMatthew Dillon 			ap->ap_sactive &= ~(1 << slot);
38444c339a5fSMatthew Dillon 		} else {
38454c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active & (1 << slot));
38464c339a5fSMatthew Dillon 			ap->ap_active &= ~(1 << slot);
38471980eff3SMatthew Dillon 			--ap->ap_active_cnt;
38481980eff3SMatthew Dillon 		}
3849258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
38504c339a5fSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
3851258223a3SMatthew Dillon 	}
38524c339a5fSMatthew Dillon 	/* ccb invalid now */
3853258223a3SMatthew Dillon 
38544c339a5fSMatthew Dillon 	/*
38554c339a5fSMatthew Dillon 	 * We can safely CLO the port to clear any BSY/DRQ, a case which
38564c339a5fSMatthew Dillon 	 * can occur with port multipliers.  This will unbrick the port
38574c339a5fSMatthew Dillon 	 * and allow commands to other targets behind the PM continue.
38584c339a5fSMatthew Dillon 	 * (FBSS).
38594c339a5fSMatthew Dillon 	 *
38604c339a5fSMatthew Dillon 	 * Finally, once the port has been restarted we can issue any
38614c339a5fSMatthew Dillon 	 * previously saved pending commands, and run the port interrupt
38624c339a5fSMatthew Dillon 	 * code to handle any completions which may have occured when
38634c339a5fSMatthew Dillon 	 * we saved CI.
38644c339a5fSMatthew Dillon 	 */
38654c339a5fSMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
38664c339a5fSMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
38674c339a5fSMatthew Dillon 		kprintf("%s: Warning, issuing CLO after timeout\n",
38684c339a5fSMatthew Dillon 			ATANAME(ap, at));
3869131be210SMatthew Dillon 		ahci_port_clo(ap);
38704c339a5fSMatthew Dillon 	}
3871131be210SMatthew Dillon 	ahci_port_start(ap);
3872492bffafSMatthew Dillon 
3873492bffafSMatthew Dillon 	/*
3874492bffafSMatthew Dillon 	 * We absolutely must make sure the chipset cleared activity on
3875492bffafSMatthew Dillon 	 * all slots.  This sometimes might not happen due to races with
3876492bffafSMatthew Dillon 	 * a chipset interrupt which stops the port before we can manage
3877492bffafSMatthew Dillon 	 * to.  For some reason some chipsets don't clear the active
3878492bffafSMatthew Dillon 	 * commands when we turn off CMD_ST after the chip has stopped
3879492bffafSMatthew Dillon 	 * operations itself.
3880492bffafSMatthew Dillon 	 */
3881492bffafSMatthew Dillon 	if (ahci_pactive(ap) != 0) {
3882492bffafSMatthew Dillon 		ahci_port_stop(ap, 0);
3883492bffafSMatthew Dillon 		ahci_port_start(ap);
3884492bffafSMatthew Dillon 		if ((mask = ahci_pactive(ap)) != 0) {
3885492bffafSMatthew Dillon 			kprintf("%s: quick-timeout: chipset failed "
3886492bffafSMatthew Dillon 				"to clear active cmds %08x\n",
3887492bffafSMatthew Dillon 				PORTNAME(ap), mask);
3888492bffafSMatthew Dillon 		}
3889492bffafSMatthew Dillon 	}
38904c339a5fSMatthew Dillon 	ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
38914c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
38924c339a5fSMatthew Dillon 	ahci_port_intr(ap, 0);
38934c339a5fSMatthew Dillon }
38944c339a5fSMatthew Dillon 
3895cf5f3a81SMatthew Dillon /*
38964c339a5fSMatthew Dillon  * Issue a previously saved set of commands
3897cf5f3a81SMatthew Dillon  */
38984c339a5fSMatthew Dillon void
38994c339a5fSMatthew Dillon ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
39004c339a5fSMatthew Dillon {
39018119d5f5SMatthew Dillon 	if (ci_saved && (ap->ap_flags & AP_F_FBSS_ENABLED) == 0) {
39024c339a5fSMatthew Dillon 		KKASSERT(!((ap->ap_active & ci_saved) &&
39034c339a5fSMatthew Dillon 			   (ap->ap_sactive & ci_saved)));
39044c339a5fSMatthew Dillon 		KKASSERT((ci_saved & ap->ap_expired) == 0);
39054c339a5fSMatthew Dillon 		if (ap->ap_sactive & ci_saved)
39064c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
39074c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
39088119d5f5SMatthew Dillon 	} else if (ci_saved) {
39098119d5f5SMatthew Dillon 		struct ata_port *ccb_at;
39108119d5f5SMatthew Dillon 		int i;
39118119d5f5SMatthew Dillon 		int fis_target;
39128119d5f5SMatthew Dillon 
39138119d5f5SMatthew Dillon 		for (i = 0; i < 32; ++i) {
39148119d5f5SMatthew Dillon 			if ((ci_saved & (1 << i)) == 0)
39158119d5f5SMatthew Dillon 				continue;
39168119d5f5SMatthew Dillon 			ccb_at = ap->ap_ccbs[i].ccb_xa.at;
39178119d5f5SMatthew Dillon 			if (ccb_at)
39188119d5f5SMatthew Dillon 				fis_target = ccb_at->at_target;
39198119d5f5SMatthew Dillon 			else
39208119d5f5SMatthew Dillon 				fis_target = 0;
39218119d5f5SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_FBS,
39228119d5f5SMatthew Dillon 				    (fis_target <<
39238119d5f5SMatthew Dillon 				     AHCI_PREG_FBS_DEV_SHIFT) |
39248119d5f5SMatthew Dillon 				    AHCI_PREG_FBS_EN);
39258119d5f5SMatthew Dillon 			if (ap->ap_sactive & (1 << i))
39268119d5f5SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, (1 << i));
39278119d5f5SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << i);
39288119d5f5SMatthew Dillon 		}
3929131be210SMatthew Dillon 	}
3930258223a3SMatthew Dillon }
3931258223a3SMatthew Dillon 
3932831bc9e3SMatthew Dillon /*
3933831bc9e3SMatthew Dillon  * Used by the softreset, pmprobe, and read_ncq_error only, in very
3934831bc9e3SMatthew Dillon  * specialized, controlled circumstances.
3935831bc9e3SMatthew Dillon  *
3936831bc9e3SMatthew Dillon  * Only one command may be pending.
3937831bc9e3SMatthew Dillon  */
3938831bc9e3SMatthew Dillon void
3939831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb)
3940831bc9e3SMatthew Dillon {
3941831bc9e3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
3942492bffafSMatthew Dillon 	u_int32_t mask;
3943831bc9e3SMatthew Dillon 
3944831bc9e3SMatthew Dillon 	switch (ccb->ccb_xa.state) {
3945831bc9e3SMatthew Dillon 	case ATA_S_PENDING:
3946831bc9e3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3947831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3948831bc9e3SMatthew Dillon 		break;
3949831bc9e3SMatthew Dillon 	case ATA_S_ONCHIP:
3950492bffafSMatthew Dillon 		/*
3951492bffafSMatthew Dillon 		 * We have to clear the command on-chip.
3952492bffafSMatthew Dillon 		 */
3953831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3954831bc9e3SMatthew Dillon 			 ap->ap_sactive == 0);
3955831bc9e3SMatthew Dillon 		ahci_port_stop(ap, 0);
3956831bc9e3SMatthew Dillon 		ahci_port_start(ap);
3957492bffafSMatthew Dillon 		if (ahci_pactive(ap) != 0) {
3958492bffafSMatthew Dillon 			ahci_port_stop(ap, 0);
3959492bffafSMatthew Dillon 			ahci_port_start(ap);
3960492bffafSMatthew Dillon 			if ((mask = ahci_pactive(ap)) != 0) {
3961492bffafSMatthew Dillon 				kprintf("%s: quick-timeout: chipset failed "
3962492bffafSMatthew Dillon 					"to clear active cmds %08x\n",
3963492bffafSMatthew Dillon 					PORTNAME(ap), mask);
3964492bffafSMatthew Dillon 			}
3965492bffafSMatthew Dillon 		}
3966831bc9e3SMatthew Dillon 
3967831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3968831bc9e3SMatthew Dillon 		ap->ap_active &= ~(1 << ccb->ccb_slot);
3969831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt > 0);
3970831bc9e3SMatthew Dillon 		--ap->ap_active_cnt;
3971831bc9e3SMatthew Dillon 		break;
3972831bc9e3SMatthew Dillon 	default:
3973831bc9e3SMatthew Dillon 		panic("%s: ahci_quick_timeout: ccb in bad state %d",
3974831bc9e3SMatthew Dillon 		      ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3975831bc9e3SMatthew Dillon 	}
3976831bc9e3SMatthew Dillon }
3977831bc9e3SMatthew Dillon 
397812feb904SMatthew Dillon static void
397912feb904SMatthew Dillon ahci_dummy_done(struct ata_xfer *xa)
398012feb904SMatthew Dillon {
398112feb904SMatthew Dillon }
398212feb904SMatthew Dillon 
398312feb904SMatthew Dillon static void
3984258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
3985258223a3SMatthew Dillon {
3986258223a3SMatthew Dillon }
3987795adb22SMatthew Dillon 
3988795adb22SMatthew Dillon int
3989492bffafSMatthew Dillon ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
3990492bffafSMatthew Dillon 		 int feature, int enable)
3991795adb22SMatthew Dillon {
3992795adb22SMatthew Dillon 	struct ata_port *at;
3993795adb22SMatthew Dillon 	struct ata_xfer *xa;
3994795adb22SMatthew Dillon 	int error;
3995795adb22SMatthew Dillon 
3996795adb22SMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
3997795adb22SMatthew Dillon 
3998795adb22SMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
3999795adb22SMatthew Dillon 
4000795adb22SMatthew Dillon 	xa->fis->type = ATA_FIS_TYPE_H2D;
4001795adb22SMatthew Dillon 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
4002795adb22SMatthew Dillon 	xa->fis->command = ATA_C_SET_FEATURES;
4003750495d0SImre Vadász 	xa->fis->features = enable ? ATA_SF_SATAFT_ENA : ATA_SF_SATAFT_DIS;
4004795adb22SMatthew Dillon 	xa->fis->sector_count = feature;
4005795adb22SMatthew Dillon 	xa->fis->control = ATA_FIS_CONTROL_4BIT;
4006795adb22SMatthew Dillon 
4007795adb22SMatthew Dillon 	xa->complete = ahci_dummy_done;
4008795adb22SMatthew Dillon 	xa->datalen = 0;
4009795adb22SMatthew Dillon 	xa->flags = ATA_F_POLL;
4010795adb22SMatthew Dillon 	xa->timeout = 1000;
4011795adb22SMatthew Dillon 
4012795adb22SMatthew Dillon 	if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
4013795adb22SMatthew Dillon 		error = 0;
4014795adb22SMatthew Dillon 	else
4015795adb22SMatthew Dillon 		error = EIO;
4016795adb22SMatthew Dillon 	ahci_ata_put_xfer(xa);
4017795adb22SMatthew Dillon 	return(error);
4018795adb22SMatthew Dillon }
4019