xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision e8cf3f55e66e1d6c12fc328642467a4fd8fd52b7)
1258223a3SMatthew Dillon /*
2258223a3SMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
3258223a3SMatthew Dillon  *
4258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
5258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
6258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
7258223a3SMatthew Dillon  *
8258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15258223a3SMatthew Dillon  *
16258223a3SMatthew Dillon  *
17258223a3SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
18258223a3SMatthew Dillon  *
19258223a3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
20258223a3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
21258223a3SMatthew Dillon  *
22258223a3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
23258223a3SMatthew Dillon  * modification, are permitted provided that the following conditions
24258223a3SMatthew Dillon  * are met:
25258223a3SMatthew Dillon  *
26258223a3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
27258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
28258223a3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
29258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
30258223a3SMatthew Dillon  *    the documentation and/or other materials provided with the
31258223a3SMatthew Dillon  *    distribution.
32258223a3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
33258223a3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
34258223a3SMatthew Dillon  *    from this software without specific, prior written permission.
35258223a3SMatthew Dillon  *
36258223a3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37258223a3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38258223a3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
39258223a3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
40258223a3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
41258223a3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
42258223a3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43258223a3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
44258223a3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45258223a3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46258223a3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47258223a3SMatthew Dillon  * SUCH DAMAGE.
48258223a3SMatthew Dillon  *
49258223a3SMatthew Dillon  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
50258223a3SMatthew Dillon  */
51258223a3SMatthew Dillon 
52258223a3SMatthew Dillon #include "ahci.h"
53258223a3SMatthew Dillon 
54f4553de1SMatthew Dillon void	ahci_port_interrupt_enable(struct ahci_port *ap);
55258223a3SMatthew Dillon 
56258223a3SMatthew Dillon int	ahci_load_prdt(struct ahci_ccb *);
57258223a3SMatthew Dillon void	ahci_unload_prdt(struct ahci_ccb *);
58258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
59258223a3SMatthew Dillon 				    int nsegs, int error);
60258223a3SMatthew Dillon void	ahci_start(struct ahci_ccb *);
6117eab71eSMatthew Dillon int	ahci_port_softreset(struct ahci_port *ap);
621980eff3SMatthew Dillon int	ahci_port_hardreset(struct ahci_port *ap, int hard);
63cf5f3a81SMatthew Dillon void	ahci_port_hardstop(struct ahci_port *ap);
64258223a3SMatthew Dillon 
65831bc9e3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *);
66831bc9e3SMatthew Dillon void	ahci_check_active_timeouts(struct ahci_port *ap);
67258223a3SMatthew Dillon 
68831bc9e3SMatthew Dillon void	ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at);
69831bc9e3SMatthew Dillon void	ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at);
704c339a5fSMatthew Dillon void	ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb);
714c339a5fSMatthew Dillon void	ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t mask);
72258223a3SMatthew Dillon 
7312feb904SMatthew Dillon int	ahci_port_read_ncq_error(struct ahci_port *, int);
74258223a3SMatthew Dillon 
75258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
76258223a3SMatthew Dillon void	ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
77258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
78258223a3SMatthew Dillon 
7912feb904SMatthew Dillon static void ahci_dummy_done(struct ata_xfer *xa);
8012feb904SMatthew Dillon static void ahci_empty_done(struct ahci_ccb *ccb);
8112feb904SMatthew Dillon static void ahci_ata_cmd_done(struct ahci_ccb *ccb);
82258223a3SMatthew Dillon 
83fd8bd957SMatthew Dillon /*
84fd8bd957SMatthew Dillon  * Initialize the global AHCI hardware.  This code does not set up any of
85fd8bd957SMatthew Dillon  * its ports.
86fd8bd957SMatthew Dillon  */
87258223a3SMatthew Dillon int
88258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc)
89258223a3SMatthew Dillon {
9012feb904SMatthew Dillon 	u_int32_t	cap, pi, pleft;
91831bc9e3SMatthew Dillon 	int		i;
92831bc9e3SMatthew Dillon 	struct ahci_port *ap;
93258223a3SMatthew Dillon 
94258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
95258223a3SMatthew Dillon 		ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
96258223a3SMatthew Dillon 
97b012a2caSMatthew Dillon 	/*
98b012a2caSMatthew Dillon 	 * save BIOS initialised parameters, enable staggered spin up
99b012a2caSMatthew Dillon 	 */
100258223a3SMatthew Dillon 	cap = ahci_read(sc, AHCI_REG_CAP);
101258223a3SMatthew Dillon 	cap &= AHCI_REG_CAP_SMPS;
102258223a3SMatthew Dillon 	cap |= AHCI_REG_CAP_SSS;
103258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
104258223a3SMatthew Dillon 
105831bc9e3SMatthew Dillon 	/*
106b012a2caSMatthew Dillon 	 * Unconditionally reset the controller, do not conditionalize on
107b012a2caSMatthew Dillon 	 * trying to figure it if it was previously active or not.
108b012a2caSMatthew Dillon 	 *
109b012a2caSMatthew Dillon 	 * NOTE: On AE before HR.  The AHCI-1.1 spec has a note in section
110b012a2caSMatthew Dillon 	 *	 5.2.2.1 regarding this.  HR should be set to 1 only after
111b012a2caSMatthew Dillon 	 *	 AE is set to 1.  The reset sequence will clear HR when
112b012a2caSMatthew Dillon 	 *	 it completes, and will also clear AE if SAM is 0.  AE must
113b012a2caSMatthew Dillon 	 *	 then be set again.  When SAM is 1 the AE bit typically reads
114b012a2caSMatthew Dillon 	 *	 as 1 (and is read-only).
115b012a2caSMatthew Dillon 	 *
116b012a2caSMatthew Dillon 	 * NOTE: Avoid PCI[e] transaction burst by issuing dummy reads,
117b012a2caSMatthew Dillon 	 *	 otherwise the writes will only be separated by a few
118b012a2caSMatthew Dillon 	 *	 nanoseconds.
119b012a2caSMatthew Dillon 	 *
120b012a2caSMatthew Dillon 	 * NOTE BRICKS (1)
121b012a2caSMatthew Dillon 	 *
122b012a2caSMatthew Dillon 	 *	If you have a port multiplier and it does not have a device
123b012a2caSMatthew Dillon 	 *	in target 0, and it probes normally, but a later operation
124b012a2caSMatthew Dillon 	 *	mis-probes a target behind that PM, it is possible for the
125b012a2caSMatthew Dillon 	 *	port to brick such that only (a) a power cycle of the host
126b012a2caSMatthew Dillon 	 *	or (b) placing a device in target 0 will fix the problem.
127b012a2caSMatthew Dillon 	 *	Power cycling the PM has no effect (it works fine on another
128b012a2caSMatthew Dillon 	 *	host port).  This issue is unrelated to CLO.
129b012a2caSMatthew Dillon 	 */
1304e21f4daSMatthew Dillon 	/*
1314e21f4daSMatthew Dillon 	 * Wait for any prior reset sequence to complete
1324e21f4daSMatthew Dillon 	 */
1334e21f4daSMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC,
1344e21f4daSMatthew Dillon 			 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
1354e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "Controller is stuck in reset\n");
1364e21f4daSMatthew Dillon 		return (1);
1374e21f4daSMatthew Dillon 	}
138b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
1394e21f4daSMatthew Dillon 	ahci_os_sleep(500);
140b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
141b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_HR);
1424e21f4daSMatthew Dillon 	ahci_os_sleep(500);
143b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
144b012a2caSMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC,
145b012a2caSMatthew Dillon 			 AHCI_REG_GHC_HR, AHCI_REG_GHC_HR) != 0) {
1464e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "unable to reset controller\n");
147b012a2caSMatthew Dillon 		return (1);
148b012a2caSMatthew Dillon 	}
1494e21f4daSMatthew Dillon 	if (ahci_read(sc, AHCI_REG_GHC) & AHCI_REG_GHC_AE) {
1504e21f4daSMatthew Dillon 		device_printf(sc->sc_dev, "AE did not auto-clear!\n");
1514e21f4daSMatthew Dillon 		ahci_write(sc, AHCI_REG_GHC, 0);
1524e21f4daSMatthew Dillon 		ahci_os_sleep(500);
1534e21f4daSMatthew Dillon 	}
154b012a2caSMatthew Dillon 
155b012a2caSMatthew Dillon 	/*
156b012a2caSMatthew Dillon 	 * Enable ahci (global interrupts disabled)
157b012a2caSMatthew Dillon 	 *
158b012a2caSMatthew Dillon 	 * Restore saved parameters.  Avoid pci transaction burst write
159b012a2caSMatthew Dillon 	 * by issuing dummy reads.
160b012a2caSMatthew Dillon 	 */
1614e21f4daSMatthew Dillon 	ahci_os_sleep(500);
162b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
163b012a2caSMatthew Dillon 	ahci_os_sleep(500);
164b012a2caSMatthew Dillon 
165b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
166b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_CAP, cap);
167b012a2caSMatthew Dillon 	ahci_write(sc, AHCI_REG_PI, pi);
168b012a2caSMatthew Dillon 	ahci_read(sc, AHCI_REG_GHC);		/* flush */
169b012a2caSMatthew Dillon 
170b012a2caSMatthew Dillon 	/*
171b012a2caSMatthew Dillon 	 * Intel hocus pocus in case the BIOS has not set the chip up
172b012a2caSMatthew Dillon 	 * properly for AHCI operation.
173b012a2caSMatthew Dillon 	 */
174b012a2caSMatthew Dillon 	if (pci_get_vendor(sc->sc_dev) == PCI_VENDOR_INTEL) {
175b012a2caSMatthew Dillon 	        if ((pci_read_config(sc->sc_dev, 0x92, 2) & 0x0F) != 0x0F)
176b012a2caSMatthew Dillon 			device_printf(sc->sc_dev, "Intel hocus pocus\n");
177b012a2caSMatthew Dillon 		pci_write_config(sc->sc_dev, 0x92,
178b012a2caSMatthew Dillon 			     pci_read_config(sc->sc_dev, 0x92, 2) | 0x0F, 2);
179b012a2caSMatthew Dillon 	}
180b012a2caSMatthew Dillon 
181b012a2caSMatthew Dillon 	/*
182831bc9e3SMatthew Dillon 	 * This is a hack that currently does not appear to have
183831bc9e3SMatthew Dillon 	 * a significant effect, but I noticed the port registers
184831bc9e3SMatthew Dillon 	 * do not appear to be completely cleared after the host
185831bc9e3SMatthew Dillon 	 * controller is reset.
18612feb904SMatthew Dillon 	 *
18712feb904SMatthew Dillon 	 * Use a temporary ap structure so we can call ahci_pwrite().
1884e21f4daSMatthew Dillon 	 *
1894e21f4daSMatthew Dillon 	 * We must be sure to stop the port
190831bc9e3SMatthew Dillon 	 */
191831bc9e3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
192831bc9e3SMatthew Dillon 	ap->ap_sc = sc;
19312feb904SMatthew Dillon 	pleft = pi;
19412feb904SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; ++i) {
19512feb904SMatthew Dillon 		if (pleft == 0)
19612feb904SMatthew Dillon 			break;
197831bc9e3SMatthew Dillon 		if ((pi & (1 << i)) == 0)
198831bc9e3SMatthew Dillon 			continue;
199831bc9e3SMatthew Dillon 		if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
200831bc9e3SMatthew Dillon 		    AHCI_PORT_REGION(i), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
201831bc9e3SMatthew Dillon 			device_printf(sc->sc_dev, "can't map port\n");
202831bc9e3SMatthew Dillon 			return (1);
203831bc9e3SMatthew Dillon 		}
2044e21f4daSMatthew Dillon 		/*
2054e21f4daSMatthew Dillon 		 * NOTE!  Setting AHCI_PREG_SCTL_DET_DISABLE on AHCI1.0 or
2064e21f4daSMatthew Dillon 		 *	  AHCI1.1 can brick the chipset.  Not only brick it,
2074e21f4daSMatthew Dillon 		 *	  but also crash the PC.  The bit seems unreliable
2084e21f4daSMatthew Dillon 		 *	  on AHCI1.2 as well.
2094e21f4daSMatthew Dillon 		 */
2104e21f4daSMatthew Dillon 		ahci_port_stop(ap, 1);
2114e21f4daSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
212831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
213831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
21412feb904SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << i);
215831bc9e3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
21612feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, -1);
21712feb904SMatthew Dillon 		sc->sc_portmask |= (1 << i);
21812feb904SMatthew Dillon 		pleft &= ~(1 << i);
219831bc9e3SMatthew Dillon 	}
22012feb904SMatthew Dillon 	sc->sc_numports = i;
221831bc9e3SMatthew Dillon 	kfree(ap, M_DEVBUF);
222831bc9e3SMatthew Dillon 
223258223a3SMatthew Dillon 	return (0);
224258223a3SMatthew Dillon }
225258223a3SMatthew Dillon 
226fd8bd957SMatthew Dillon /*
227fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
228fd8bd957SMatthew Dillon  */
229258223a3SMatthew Dillon int
230258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port)
231258223a3SMatthew Dillon {
232258223a3SMatthew Dillon 	struct ahci_port	*ap;
2331980eff3SMatthew Dillon 	struct ata_port		*at;
234258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
235258223a3SMatthew Dillon 	u_int64_t		dva;
236258223a3SMatthew Dillon 	u_int32_t		cmd;
23712feb904SMatthew Dillon 	u_int32_t		data;
238258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*hdr;
239258223a3SMatthew Dillon 	struct ahci_cmd_table	*table;
240258223a3SMatthew Dillon 	int	rc = ENOMEM;
241258223a3SMatthew Dillon 	int	error;
242258223a3SMatthew Dillon 	int	i;
243258223a3SMatthew Dillon 
244258223a3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
24512feb904SMatthew Dillon 	ap->ap_err_scratch = kmalloc(512, M_DEVBUF, M_WAITOK | M_ZERO);
246258223a3SMatthew Dillon 
247258223a3SMatthew Dillon 	ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
248258223a3SMatthew Dillon 		  device_get_name(sc->sc_dev),
249258223a3SMatthew Dillon 		  device_get_unit(sc->sc_dev),
250258223a3SMatthew Dillon 		  port);
251258223a3SMatthew Dillon 	sc->sc_ports[port] = ap;
252258223a3SMatthew Dillon 
2531980eff3SMatthew Dillon 	/*
2541980eff3SMatthew Dillon 	 * Allocate enough so we never have to reallocate, it makes
2551980eff3SMatthew Dillon 	 * it easier.
2561980eff3SMatthew Dillon 	 *
2571980eff3SMatthew Dillon 	 * ap_pmcount will be reduced by the scan if we encounter the
2581980eff3SMatthew Dillon 	 * port multiplier port prior to target 15.
259b012a2caSMatthew Dillon 	 *
260b012a2caSMatthew Dillon 	 * kmalloc power-of-2 allocations are guaranteed not to cross
261b012a2caSMatthew Dillon 	 * a page boundary.  Make sure the identify sub-structure in the
262b012a2caSMatthew Dillon 	 * at structure does not cross a page boundary, just in case the
263b012a2caSMatthew Dillon 	 * part is AHCI-1.1 and can't handle multiple DRQ blocks.
2641980eff3SMatthew Dillon 	 */
265b012a2caSMatthew Dillon 	if (ap->ap_ata[0] == NULL) {
266b012a2caSMatthew Dillon 		int pw2;
267b012a2caSMatthew Dillon 
268b012a2caSMatthew Dillon 		for (pw2 = 1; pw2 < sizeof(*at); pw2 <<= 1)
269b012a2caSMatthew Dillon 			;
2701980eff3SMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
271b012a2caSMatthew Dillon 			at = kmalloc(pw2, M_DEVBUF, M_INTWAIT | M_ZERO);
272b012a2caSMatthew Dillon 			ap->ap_ata[i] = at;
2731980eff3SMatthew Dillon 			at->at_ahci_port = ap;
2741980eff3SMatthew Dillon 			at->at_target = i;
2753209f581SMatthew Dillon 			at->at_probe = ATA_PROBE_NEED_INIT;
276831bc9e3SMatthew Dillon 			at->at_features |= ATA_PORT_F_RESCAN;
2771980eff3SMatthew Dillon 			ksnprintf(at->at_name, sizeof(at->at_name),
2781980eff3SMatthew Dillon 				  "%s.%d", ap->ap_name, i);
2791980eff3SMatthew Dillon 		}
2801980eff3SMatthew Dillon 	}
281258223a3SMatthew Dillon 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
282258223a3SMatthew Dillon 	    AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
283258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
284258223a3SMatthew Dillon 			      "unable to create register window for port %d\n",
285258223a3SMatthew Dillon 			      port);
286258223a3SMatthew Dillon 		goto freeport;
287258223a3SMatthew Dillon 	}
288258223a3SMatthew Dillon 
289258223a3SMatthew Dillon 	ap->ap_sc = sc;
290258223a3SMatthew Dillon 	ap->ap_num = port;
2913209f581SMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_INIT;
292258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_free);
293258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_pending);
294258223a3SMatthew Dillon 	lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
295258223a3SMatthew Dillon 
296258223a3SMatthew Dillon 	/* Disable port interrupts */
297258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
298831bc9e3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
299258223a3SMatthew Dillon 
30017eab71eSMatthew Dillon 	/*
30117eab71eSMatthew Dillon 	 * Sec 10.1.2 - deinitialise port if it is already running
30217eab71eSMatthew Dillon 	 */
303258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
304258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
305258223a3SMatthew Dillon 		    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
306258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
307258223a3SMatthew Dillon 		int r;
308258223a3SMatthew Dillon 
309258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
310258223a3SMatthew Dillon 		if (r) {
311258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
312258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
313258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
314258223a3SMatthew Dillon 			rc = ENXIO;
315258223a3SMatthew Dillon 			goto freeport;
316258223a3SMatthew Dillon 		}
317258223a3SMatthew Dillon 
318258223a3SMatthew Dillon 		/* Write DET to zero */
319cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
320258223a3SMatthew Dillon 	}
321258223a3SMatthew Dillon 
322258223a3SMatthew Dillon 	/* Allocate RFIS */
323258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
324258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
325cf5f3a81SMatthew Dillon 		kprintf("%s: NORFIS\n", PORTNAME(ap));
326258223a3SMatthew Dillon 		goto nomem;
327258223a3SMatthew Dillon 	}
328258223a3SMatthew Dillon 
329258223a3SMatthew Dillon 	/* Setup RFIS base address */
330258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
331258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
332258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
333258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
334258223a3SMatthew Dillon 
335831bc9e3SMatthew Dillon 	/* Clear SERR before starting FIS reception or ST or anything */
336831bc9e3SMatthew Dillon 	ahci_flush_tfd(ap);
337831bc9e3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
338831bc9e3SMatthew Dillon 
339258223a3SMatthew Dillon 	/* Enable FIS reception and activate port. */
340258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
3411980eff3SMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
342258223a3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
343258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
344258223a3SMatthew Dillon 
345258223a3SMatthew Dillon 	/* Check whether port activated.  Skip it if not. */
346258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
347258223a3SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
348cf5f3a81SMatthew Dillon 		kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
349258223a3SMatthew Dillon 		rc = ENXIO;
350258223a3SMatthew Dillon 		goto freeport;
351258223a3SMatthew Dillon 	}
352258223a3SMatthew Dillon 
353258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
354258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
355258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
356258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
357258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
358258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
359258223a3SMatthew Dillon 			      port);
360258223a3SMatthew Dillon 		goto freeport;
361258223a3SMatthew Dillon 	}
362258223a3SMatthew Dillon 
363258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
364258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
365258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
366258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
367258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
368258223a3SMatthew Dillon nomem:
369258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
370258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
371258223a3SMatthew Dillon 			      port);
372258223a3SMatthew Dillon 		goto freeport;
373258223a3SMatthew Dillon 	}
374258223a3SMatthew Dillon 
375258223a3SMatthew Dillon 	/* Setup command list base address */
376258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
377258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
378258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
379258223a3SMatthew Dillon 
380258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
381258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
382258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
383258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
384258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
385258223a3SMatthew Dillon 
386258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
387258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
388258223a3SMatthew Dillon 		if (error) {
389258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
390258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
391258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
392258223a3SMatthew Dillon 			goto freeport;
393258223a3SMatthew Dillon 		}
394258223a3SMatthew Dillon 
395258223a3SMatthew Dillon 		callout_init(&ccb->ccb_timeout);
396258223a3SMatthew Dillon 		ccb->ccb_slot = i;
397258223a3SMatthew Dillon 		ccb->ccb_port = ap;
398258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
399258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
400258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
401258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
402258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
403258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
404258223a3SMatthew Dillon 
405258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
406258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
407258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
408258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
409258223a3SMatthew Dillon 
410258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
4111067474aSMatthew Dillon 
4121067474aSMatthew Dillon 		/*
4131067474aSMatthew Dillon 		 * CCB[1] is the error CCB and is not get or put.  It is
4141067474aSMatthew Dillon 		 * also used for probing.  Numerous HBAs only load the
4151067474aSMatthew Dillon 		 * signature from CCB[1] so it MUST be used for the second
4161067474aSMatthew Dillon 		 * FIS.
4171067474aSMatthew Dillon 		 */
4181067474aSMatthew Dillon 		if (i == 1)
4191067474aSMatthew Dillon 			ap->ap_err_ccb = ccb;
4201067474aSMatthew Dillon 		else
421258223a3SMatthew Dillon 			ahci_put_ccb(ccb);
422258223a3SMatthew Dillon 	}
423258223a3SMatthew Dillon 
42412feb904SMatthew Dillon 	/*
42512feb904SMatthew Dillon 	 * Wait for ICC change to complete
42612feb904SMatthew Dillon 	 */
427258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
428258223a3SMatthew Dillon 
429fd8bd957SMatthew Dillon 	/*
43012feb904SMatthew Dillon 	 * Calculate the interrupt mask
43112feb904SMatthew Dillon 	 */
43212feb904SMatthew Dillon 	data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
43312feb904SMatthew Dillon 	       AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
43412feb904SMatthew Dillon 	       AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
43512feb904SMatthew Dillon 	       AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
43612feb904SMatthew Dillon 	       AHCI_PREG_IE_DHRE | AHCI_PREG_IE_SDBE;
43712feb904SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
43812feb904SMatthew Dillon 		data |= AHCI_PREG_IE_IPME;
43912feb904SMatthew Dillon #ifdef AHCI_COALESCE
44012feb904SMatthew Dillon 	if (sc->sc_ccc_ports & (1 << port)
44112feb904SMatthew Dillon 		data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
44212feb904SMatthew Dillon #endif
44312feb904SMatthew Dillon 	ap->ap_intmask = data;
44412feb904SMatthew Dillon 
44512feb904SMatthew Dillon 	/*
446*e8cf3f55SMatthew Dillon 	 * Start the port helper thread.  The helper thread will call
447*e8cf3f55SMatthew Dillon 	 * ahci_port_init() so the ports can all be started in parallel.
448*e8cf3f55SMatthew Dillon 	 * A failure by ahci_port_init() does not deallocate the port
449*e8cf3f55SMatthew Dillon 	 * since we still want hot-plug events.
450fd8bd957SMatthew Dillon 	 */
451f4553de1SMatthew Dillon 	ahci_os_start_port(ap);
452fd8bd957SMatthew Dillon 	return(0);
453fd8bd957SMatthew Dillon freeport:
454fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
455fd8bd957SMatthew Dillon 	return (rc);
456fd8bd957SMatthew Dillon }
457fd8bd957SMatthew Dillon 
458fd8bd957SMatthew Dillon /*
459fd8bd957SMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.
460fd8bd957SMatthew Dillon  *
461fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
462fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
463fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
464fd8bd957SMatthew Dillon  *
46522181ab7SMatthew Dillon  * This function is the only way to move a failed port back to active
46622181ab7SMatthew Dillon  * status.
46722181ab7SMatthew Dillon  *
468fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
469fd8bd957SMatthew Dillon  */
470fd8bd957SMatthew Dillon int
47112feb904SMatthew Dillon ahci_port_init(struct ahci_port *ap)
472fd8bd957SMatthew Dillon {
473fd8bd957SMatthew Dillon 	/*
47412feb904SMatthew Dillon 	 * Register [re]initialization
475fd8bd957SMatthew Dillon 	 */
47612feb904SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
4771980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
4781980eff3SMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
4791980eff3SMatthew Dillon 	ap->ap_pmcount = 0;
480*e8cf3f55SMatthew Dillon 
481*e8cf3f55SMatthew Dillon 	/*
482*e8cf3f55SMatthew Dillon 	 * Cycle the port before enabling its interrupt.  This makes sure
483*e8cf3f55SMatthew Dillon 	 * that the CI and SACT registers are clear.  It might not be
484*e8cf3f55SMatthew Dillon 	 * necesary now that we sequence the interrupt enablement properly
485*e8cf3f55SMatthew Dillon 	 * but I'm keeping it in.
486*e8cf3f55SMatthew Dillon 	 */
487*e8cf3f55SMatthew Dillon 	ahci_port_start(ap);
488*e8cf3f55SMatthew Dillon 	ahci_port_stop(ap, 0);
489f4553de1SMatthew Dillon 	ahci_port_interrupt_enable(ap);
49012feb904SMatthew Dillon 	return (0);
491f4553de1SMatthew Dillon }
492f4553de1SMatthew Dillon 
493f4553de1SMatthew Dillon /*
494f4553de1SMatthew Dillon  * Enable or re-enable interrupts on a port.
495f4553de1SMatthew Dillon  *
496f4553de1SMatthew Dillon  * This routine is called from the port initialization code or from the
497f4553de1SMatthew Dillon  * helper thread as the real interrupt may be forced to turn off certain
498f4553de1SMatthew Dillon  * interrupt sources.
499f4553de1SMatthew Dillon  */
500f4553de1SMatthew Dillon void
501f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap)
502f4553de1SMatthew Dillon {
50312feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, ap->ap_intmask);
5041980eff3SMatthew Dillon }
505258223a3SMatthew Dillon 
506fd8bd957SMatthew Dillon /*
5073209f581SMatthew Dillon  * Run the port / target state machine from a main context.
5083209f581SMatthew Dillon  *
5093209f581SMatthew Dillon  * The state machine for the port is always run.
5103209f581SMatthew Dillon  *
5113209f581SMatthew Dillon  * If atx is non-NULL run the state machine for a particular target.
5123209f581SMatthew Dillon  * If atx is NULL run the state machine for all targets.
5133209f581SMatthew Dillon  */
5143209f581SMatthew Dillon void
515831bc9e3SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap, int initial)
5163209f581SMatthew Dillon {
5173209f581SMatthew Dillon 	struct ata_port *at;
5183209f581SMatthew Dillon 	u_int32_t data;
5193209f581SMatthew Dillon 	int target;
5203209f581SMatthew Dillon 	int didsleep;
521831bc9e3SMatthew Dillon 	int loop;
5223209f581SMatthew Dillon 
523831bc9e3SMatthew Dillon 	/*
524831bc9e3SMatthew Dillon 	 * State machine for port.  Note that CAM is not yet associated
525831bc9e3SMatthew Dillon 	 * during the initial parallel probe and the port's probe state
526831bc9e3SMatthew Dillon 	 * will not get past ATA_PROBE_NEED_IDENT.
527831bc9e3SMatthew Dillon 	 */
528c408a8b3SMatthew Dillon 	{
5291067474aSMatthew Dillon 		if (initial == 0 && ap->ap_probe <= ATA_PROBE_NEED_HARD_RESET) {
5301067474aSMatthew Dillon 			kprintf("%s: Waiting 10 seconds on insertion\n",
5311067474aSMatthew Dillon 				PORTNAME(ap));
5321067474aSMatthew Dillon 			ahci_os_sleep(10000);
5331067474aSMatthew Dillon 			initial = 1;
5343209f581SMatthew Dillon 		}
5351067474aSMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT)
53612feb904SMatthew Dillon 			ahci_port_init(ap);
5373209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
5383209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 1);
5393209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
5403209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 0);
5413209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
5423209f581SMatthew Dillon 			ahci_cam_probe(ap, NULL);
5433209f581SMatthew Dillon 	}
5443209f581SMatthew Dillon 	if (ap->ap_type != ATA_PORT_T_PM) {
5453209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_FAILED) {
5463209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 0);
547f4553de1SMatthew Dillon 		} else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
5483209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 1);
5493209f581SMatthew Dillon 		}
5503209f581SMatthew Dillon 		return;
5513209f581SMatthew Dillon 	}
5523209f581SMatthew Dillon 
553831bc9e3SMatthew Dillon 	/*
554831bc9e3SMatthew Dillon 	 * Port Multiplier state machine.
555831bc9e3SMatthew Dillon 	 *
556831bc9e3SMatthew Dillon 	 * Get a mask of changed targets and combine with any runnable
557831bc9e3SMatthew Dillon 	 * states already present.
558831bc9e3SMatthew Dillon 	 */
559831bc9e3SMatthew Dillon 	for (loop = 0; ;++loop) {
5602cc2e845SMatthew Dillon 		if (ahci_pm_read(ap, 15, SATA_PMREG_EINFO, &data)) {
5613209f581SMatthew Dillon 			kprintf("%s: PM unable to read hot-plug bitmap\n",
5623209f581SMatthew Dillon 				PORTNAME(ap));
5633209f581SMatthew Dillon 			break;
5643209f581SMatthew Dillon 		}
5653209f581SMatthew Dillon 
5663209f581SMatthew Dillon 		/*
567831bc9e3SMatthew Dillon 		 * Do at least one loop, then stop if no more state changes
568831bc9e3SMatthew Dillon 		 * have occured.  The PM might not generate a new
569831bc9e3SMatthew Dillon 		 * notification until we clear the entire bitmap.
5703209f581SMatthew Dillon 		 */
571831bc9e3SMatthew Dillon 		if (loop && data == 0)
5723209f581SMatthew Dillon 			break;
5733209f581SMatthew Dillon 
5743209f581SMatthew Dillon 		/*
5753209f581SMatthew Dillon 		 * New devices showing up in the bitmap require some spin-up
5763209f581SMatthew Dillon 		 * time before we start probing them.  Reset didsleep.  The
5773209f581SMatthew Dillon 		 * first new device we detect will sleep before probing.
578831bc9e3SMatthew Dillon 		 *
579831bc9e3SMatthew Dillon 		 * This only applies to devices whos change bit is set in
580831bc9e3SMatthew Dillon 		 * the data, and does not apply to the initial boot-time
581831bc9e3SMatthew Dillon 		 * probe.
5823209f581SMatthew Dillon 		 */
5833209f581SMatthew Dillon 		didsleep = 0;
5843209f581SMatthew Dillon 
5853209f581SMatthew Dillon 		for (target = 0; target < ap->ap_pmcount; ++target) {
586b012a2caSMatthew Dillon 			at = ap->ap_ata[target];
5873209f581SMatthew Dillon 
5883209f581SMatthew Dillon 			/*
5893209f581SMatthew Dillon 			 * Check the target state for targets behind the PM
5903209f581SMatthew Dillon 			 * which have changed state.  This will adjust
5913209f581SMatthew Dillon 			 * at_probe and set ATA_PORT_F_RESCAN
5923209f581SMatthew Dillon 			 *
5931067474aSMatthew Dillon 			 * We want to wait at least 10 seconds before probing
5943209f581SMatthew Dillon 			 * a newly inserted device.  If the check status
5953209f581SMatthew Dillon 			 * indicates a device is present and in need of a
5963209f581SMatthew Dillon 			 * hard reset, we make sure we have slept before
5973209f581SMatthew Dillon 			 * continuing.
598831bc9e3SMatthew Dillon 			 *
5991067474aSMatthew Dillon 			 * We also need to wait at least 1 second for the
6001067474aSMatthew Dillon 			 * PHY state to change after insertion, if we
6011067474aSMatthew Dillon 			 * haven't already waited the 10 seconds.
6021067474aSMatthew Dillon 			 *
603831bc9e3SMatthew Dillon 			 * NOTE: When pm_check_good finds a good port it
604831bc9e3SMatthew Dillon 			 *	 typically starts us in probe state
605831bc9e3SMatthew Dillon 			 *	 NEED_HARD_RESET rather than INIT.
6063209f581SMatthew Dillon 			 */
6073209f581SMatthew Dillon 			if (data & (1 << target)) {
6081067474aSMatthew Dillon 				if (initial == 0 && didsleep == 0)
6091067474aSMatthew Dillon 					ahci_os_sleep(1000);
6103209f581SMatthew Dillon 				ahci_pm_check_good(ap, target);
611831bc9e3SMatthew Dillon 				if (initial == 0 && didsleep == 0 &&
612831bc9e3SMatthew Dillon 				    at->at_probe <= ATA_PROBE_NEED_HARD_RESET
613831bc9e3SMatthew Dillon 				) {
6143209f581SMatthew Dillon 					didsleep = 1;
615121d8e75SMatthew Dillon 					kprintf("%s: Waiting 10 seconds on insertion\n", PORTNAME(ap));
616121d8e75SMatthew Dillon 					ahci_os_sleep(10000);
6173209f581SMatthew Dillon 				}
6183209f581SMatthew Dillon 			}
619831bc9e3SMatthew Dillon 
620831bc9e3SMatthew Dillon 			/*
621831bc9e3SMatthew Dillon 			 * Report hot-plug events before the probe state
622831bc9e3SMatthew Dillon 			 * really gets hot.  Only actual events are reported
623831bc9e3SMatthew Dillon 			 * here to reduce spew.
624831bc9e3SMatthew Dillon 			 */
625831bc9e3SMatthew Dillon 			if (data & (1 << target)) {
626831bc9e3SMatthew Dillon 				kprintf("%s: HOTPLUG (PM) - ", ATANAME(ap, at));
627831bc9e3SMatthew Dillon 				switch(at->at_probe) {
628831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_INIT:
629831bc9e3SMatthew Dillon 				case ATA_PROBE_NEED_HARD_RESET:
630831bc9e3SMatthew Dillon 					kprintf("Device inserted\n");
631831bc9e3SMatthew Dillon 					break;
632831bc9e3SMatthew Dillon 				case ATA_PROBE_FAILED:
633831bc9e3SMatthew Dillon 					kprintf("Device removed\n");
634831bc9e3SMatthew Dillon 					break;
635831bc9e3SMatthew Dillon 				default:
636831bc9e3SMatthew Dillon 					kprintf("Device probe in progress\n");
637831bc9e3SMatthew Dillon 					break;
638831bc9e3SMatthew Dillon 				}
6393209f581SMatthew Dillon 			}
6403209f581SMatthew Dillon 
6413209f581SMatthew Dillon 			/*
642831bc9e3SMatthew Dillon 			 * Run through the state machine as necessary if
643831bc9e3SMatthew Dillon 			 * the port is not marked failed.
644831bc9e3SMatthew Dillon 			 *
645831bc9e3SMatthew Dillon 			 * The state machine may stop at NEED_IDENT if
646831bc9e3SMatthew Dillon 			 * CAM is not yet attached.
647831bc9e3SMatthew Dillon 			 *
648831bc9e3SMatthew Dillon 			 * Acquire exclusive access to the port while we
649831bc9e3SMatthew Dillon 			 * are doing this.  This prevents command-completion
650831bc9e3SMatthew Dillon 			 * from queueing commands for non-polled targets
651831bc9e3SMatthew Dillon 			 * inbetween our probe steps.  We need to do this
652831bc9e3SMatthew Dillon 			 * because the reset probes can generate severe PHY
653831bc9e3SMatthew Dillon 			 * and protocol errors and soft-brick the port.
6543209f581SMatthew Dillon 			 */
655831bc9e3SMatthew Dillon 			if (at->at_probe != ATA_PROBE_FAILED &&
656831bc9e3SMatthew Dillon 			    at->at_probe != ATA_PROBE_GOOD) {
657831bc9e3SMatthew Dillon 				ahci_beg_exclusive_access(ap, at);
6583209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_INIT)
65912feb904SMatthew Dillon 					ahci_pm_port_init(ap, at);
6603209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
6613209f581SMatthew Dillon 					ahci_port_reset(ap, at, 1);
6623209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
6633209f581SMatthew Dillon 					ahci_port_reset(ap, at, 0);
6643209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_IDENT)
6653209f581SMatthew Dillon 					ahci_cam_probe(ap, at);
666831bc9e3SMatthew Dillon 				ahci_end_exclusive_access(ap, at);
6673209f581SMatthew Dillon 			}
6683209f581SMatthew Dillon 
6693209f581SMatthew Dillon 			/*
670831bc9e3SMatthew Dillon 			 * Add or remove from CAM
6713209f581SMatthew Dillon 			 */
6723209f581SMatthew Dillon 			if (at->at_features & ATA_PORT_F_RESCAN) {
6733209f581SMatthew Dillon 				at->at_features &= ~ATA_PORT_F_RESCAN;
6743209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_FAILED) {
6753209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 0);
676f4553de1SMatthew Dillon 				} else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
6773209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 1);
6783209f581SMatthew Dillon 				}
6793209f581SMatthew Dillon 			}
6803560ed94SMatthew Dillon 			data &= ~(1 << target);
6813560ed94SMatthew Dillon 		}
6823560ed94SMatthew Dillon 		if (data) {
6833560ed94SMatthew Dillon 			kprintf("%s: WARNING (PM): extra bits set in "
6843560ed94SMatthew Dillon 				"EINFO: %08x\n", PORTNAME(ap), data);
6853560ed94SMatthew Dillon 			while (target < AHCI_MAX_PMPORTS) {
6863560ed94SMatthew Dillon 				ahci_pm_check_good(ap, target);
6873560ed94SMatthew Dillon 				++target;
6883560ed94SMatthew Dillon 			}
6893209f581SMatthew Dillon 		}
6903209f581SMatthew Dillon 	}
6913209f581SMatthew Dillon }
6923209f581SMatthew Dillon 
6933209f581SMatthew Dillon 
6943209f581SMatthew Dillon /*
695fd8bd957SMatthew Dillon  * De-initialize and detach a port.
696fd8bd957SMatthew Dillon  */
697258223a3SMatthew Dillon void
698258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
699258223a3SMatthew Dillon {
700258223a3SMatthew Dillon 	struct ahci_port	*ap = sc->sc_ports[port];
701258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
702b012a2caSMatthew Dillon 	int i;
703258223a3SMatthew Dillon 
70417eab71eSMatthew Dillon 	/*
70517eab71eSMatthew Dillon 	 * Ensure port is disabled and its interrupts are all flushed.
70617eab71eSMatthew Dillon 	 */
707258223a3SMatthew Dillon 	if (ap->ap_sc) {
70817eab71eSMatthew Dillon 		ahci_port_stop(ap, 1);
709f4553de1SMatthew Dillon 		ahci_os_stop_port(ap);
710258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
711258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
712258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
713258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
714258223a3SMatthew Dillon 	}
715258223a3SMatthew Dillon 
716258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
717258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
718258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
719258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
720258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
721258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
722258223a3SMatthew Dillon 			}
723258223a3SMatthew Dillon 		}
7241067474aSMatthew Dillon 		if ((ccb = ap->ap_err_ccb) != NULL) {
7251067474aSMatthew Dillon 			if (ccb->ccb_dmamap) {
7261067474aSMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
7271067474aSMatthew Dillon 						   ccb->ccb_dmamap);
7281067474aSMatthew Dillon 				ccb->ccb_dmamap = NULL;
7291067474aSMatthew Dillon 			}
7301067474aSMatthew Dillon 			ap->ap_err_ccb = NULL;
7311067474aSMatthew Dillon 		}
732258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
733258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
734258223a3SMatthew Dillon 	}
735258223a3SMatthew Dillon 
736258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
737258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
738258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
739258223a3SMatthew Dillon 	}
740258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
741258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
742258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
743258223a3SMatthew Dillon 	}
744258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
745258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
746258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
747258223a3SMatthew Dillon 	}
7481980eff3SMatthew Dillon 	if (ap->ap_ata) {
749b012a2caSMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
750b012a2caSMatthew Dillon 			if (ap->ap_ata[i]) {
751b012a2caSMatthew Dillon 				kfree(ap->ap_ata[i], M_DEVBUF);
752b012a2caSMatthew Dillon 				ap->ap_ata[i] = NULL;
753b012a2caSMatthew Dillon 			}
754b012a2caSMatthew Dillon 		}
7551980eff3SMatthew Dillon 	}
75612feb904SMatthew Dillon 	if (ap->ap_err_scratch) {
75712feb904SMatthew Dillon 		kfree(ap->ap_err_scratch, M_DEVBUF);
75812feb904SMatthew Dillon 		ap->ap_err_scratch = NULL;
75912feb904SMatthew Dillon 	}
760258223a3SMatthew Dillon 
761258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
762258223a3SMatthew Dillon 
763258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
764258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
765258223a3SMatthew Dillon }
766258223a3SMatthew Dillon 
767fd8bd957SMatthew Dillon /*
768fd8bd957SMatthew Dillon  * Start high-level command processing on the port
769fd8bd957SMatthew Dillon  */
770258223a3SMatthew Dillon int
77117eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap)
772258223a3SMatthew Dillon {
77312feb904SMatthew Dillon 	u_int32_t	r, s, is, tfd;
774258223a3SMatthew Dillon 
77517eab71eSMatthew Dillon 	/*
77617eab71eSMatthew Dillon 	 * FRE must be turned on before ST.  Wait for FR to go active
77717eab71eSMatthew Dillon 	 * before turning on ST.  The spec doesn't seem to think this
77817eab71eSMatthew Dillon 	 * is necessary but waiting here avoids an on-off race in the
77917eab71eSMatthew Dillon 	 * ahci_port_stop() code.
78017eab71eSMatthew Dillon 	 */
78112feb904SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD);
78217eab71eSMatthew Dillon 	if ((r & AHCI_PREG_CMD_FRE) == 0) {
783258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_FRE;
78417eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
78517eab71eSMatthew Dillon 	}
78617eab71eSMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
78717eab71eSMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
78817eab71eSMatthew Dillon 			kprintf("%s: Cannot start FIS reception\n",
78917eab71eSMatthew Dillon 				PORTNAME(ap));
79017eab71eSMatthew Dillon 			return (2);
79117eab71eSMatthew Dillon 		}
79217eab71eSMatthew Dillon 	}
79317eab71eSMatthew Dillon 
79417eab71eSMatthew Dillon 	/*
79517eab71eSMatthew Dillon 	 * Turn on ST, wait for CR to come up.
79617eab71eSMatthew Dillon 	 */
797258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_ST;
798258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
79917eab71eSMatthew Dillon 	if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
8008bf6a3ffSMatthew Dillon 		s = ahci_pread(ap, AHCI_PREG_SERR);
8018bf6a3ffSMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
8028bf6a3ffSMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
8031980eff3SMatthew Dillon 		kprintf("%s: Cannot start command DMA\n"
8041980eff3SMatthew Dillon 			"NCMP=%b NSERR=%b\n"
80512feb904SMatthew Dillon 			"NEWIS=%b\n"
80612feb904SMatthew Dillon 			"NEWTFD=%b\n",
8071980eff3SMatthew Dillon 			PORTNAME(ap),
8081980eff3SMatthew Dillon 			r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
80912feb904SMatthew Dillon 			is, AHCI_PFMT_IS,
81012feb904SMatthew Dillon 			tfd, AHCI_PFMT_TFD_STS);
81117eab71eSMatthew Dillon 		return (1);
81217eab71eSMatthew Dillon 	}
813258223a3SMatthew Dillon 
814258223a3SMatthew Dillon #ifdef AHCI_COALESCE
81517eab71eSMatthew Dillon 	/*
81617eab71eSMatthew Dillon 	 * (Re-)enable coalescing on the port.
81717eab71eSMatthew Dillon 	 */
818258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
819258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
820258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
821258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
822258223a3SMatthew Dillon 	}
823258223a3SMatthew Dillon #endif
824258223a3SMatthew Dillon 
825258223a3SMatthew Dillon 	return (0);
826258223a3SMatthew Dillon }
827258223a3SMatthew Dillon 
828fd8bd957SMatthew Dillon /*
829fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
8304c339a5fSMatthew Dillon  *
8314c339a5fSMatthew Dillon  * WARNING!  If the port is stopped while CR is still active our saved
8324c339a5fSMatthew Dillon  *	     CI/SACT will race any commands completed by the command
8334c339a5fSMatthew Dillon  *	     processor prior to being able to stop.  Thus we never call
8344c339a5fSMatthew Dillon  *	     this function unless we intend to dispose of any remaining
8354c339a5fSMatthew Dillon  *	     active commands.  In particular, this complicates the timeout
8364c339a5fSMatthew Dillon  *	     code.
837fd8bd957SMatthew Dillon  */
838258223a3SMatthew Dillon int
839258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
840258223a3SMatthew Dillon {
841258223a3SMatthew Dillon 	u_int32_t	r;
842258223a3SMatthew Dillon 
843258223a3SMatthew Dillon #ifdef AHCI_COALESCE
84417eab71eSMatthew Dillon 	/*
84517eab71eSMatthew Dillon 	 * Disable coalescing on the port while it is stopped.
84617eab71eSMatthew Dillon 	 */
847258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
848258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
849258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
850258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
851258223a3SMatthew Dillon 	}
852258223a3SMatthew Dillon #endif
853258223a3SMatthew Dillon 
85417eab71eSMatthew Dillon 	/*
85517eab71eSMatthew Dillon 	 * Turn off ST, then wait for CR to go off.
85617eab71eSMatthew Dillon 	 */
857258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
858258223a3SMatthew Dillon 	r &= ~AHCI_PREG_CMD_ST;
859258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
860258223a3SMatthew Dillon 
86117eab71eSMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
86217eab71eSMatthew Dillon 		kprintf("%s: Port bricked, unable to stop (ST)\n",
86317eab71eSMatthew Dillon 			PORTNAME(ap));
864258223a3SMatthew Dillon 		return (1);
86517eab71eSMatthew Dillon 	}
866258223a3SMatthew Dillon 
8671980eff3SMatthew Dillon #if 0
86817eab71eSMatthew Dillon 	/*
86917eab71eSMatthew Dillon 	 * Turn off FRE, then wait for FR to go off.  FRE cannot
87017eab71eSMatthew Dillon 	 * be turned off until CR transitions to 0.
87117eab71eSMatthew Dillon 	 */
8721980eff3SMatthew Dillon 	if ((r & AHCI_PREG_CMD_FR) == 0) {
8731980eff3SMatthew Dillon 		kprintf("%s: FR stopped, clear FRE for next start\n",
8741980eff3SMatthew Dillon 			PORTNAME(ap));
8751980eff3SMatthew Dillon 		stop_fis_rx = 2;
8761980eff3SMatthew Dillon 	}
8771980eff3SMatthew Dillon #endif
87817eab71eSMatthew Dillon 	if (stop_fis_rx) {
87917eab71eSMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
88017eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
88117eab71eSMatthew Dillon 		if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
88217eab71eSMatthew Dillon 			kprintf("%s: Port bricked, unable to stop (FRE)\n",
88317eab71eSMatthew Dillon 				PORTNAME(ap));
884258223a3SMatthew Dillon 			return (2);
88517eab71eSMatthew Dillon 		}
88617eab71eSMatthew Dillon 	}
887258223a3SMatthew Dillon 
888258223a3SMatthew Dillon 	return (0);
889258223a3SMatthew Dillon }
890258223a3SMatthew Dillon 
891fd8bd957SMatthew Dillon /*
892fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
893fd8bd957SMatthew Dillon  */
894258223a3SMatthew Dillon int
895258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
896258223a3SMatthew Dillon {
897258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
898258223a3SMatthew Dillon 	u_int32_t			cmd;
899258223a3SMatthew Dillon 
900258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
901258223a3SMatthew Dillon 	if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
902258223a3SMatthew Dillon 		return (1);
903258223a3SMatthew Dillon 
904258223a3SMatthew Dillon 	/* Issue CLO */
905258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
906258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
907258223a3SMatthew Dillon 
908258223a3SMatthew Dillon 	/* Wait for completion */
909258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
910258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
911258223a3SMatthew Dillon 		return (1);
912258223a3SMatthew Dillon 	}
913258223a3SMatthew Dillon 
914258223a3SMatthew Dillon 	return (0);
915258223a3SMatthew Dillon }
916258223a3SMatthew Dillon 
917fd8bd957SMatthew Dillon /*
9181980eff3SMatthew Dillon  * Reset a port.
91917eab71eSMatthew Dillon  *
9201980eff3SMatthew Dillon  * If hard is 0 perform a softreset of the port.
92117eab71eSMatthew Dillon  * If hard is 1 perform a hard reset of the port.
9221980eff3SMatthew Dillon  *
9231980eff3SMatthew Dillon  * If at is non-NULL an indirect port via a port-multiplier is being
9241980eff3SMatthew Dillon  * reset, otherwise a direct port is being reset.
9251980eff3SMatthew Dillon  *
9261980eff3SMatthew Dillon  * NOTE: Indirect ports can only be soft-reset.
92717eab71eSMatthew Dillon  */
92817eab71eSMatthew Dillon int
9291980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
93017eab71eSMatthew Dillon {
93117eab71eSMatthew Dillon 	int rc;
93217eab71eSMatthew Dillon 
93317eab71eSMatthew Dillon 	if (hard) {
9341980eff3SMatthew Dillon 		if (at)
9351980eff3SMatthew Dillon 			rc = ahci_pm_hardreset(ap, at->at_target, hard);
9361980eff3SMatthew Dillon 		else
9371980eff3SMatthew Dillon 			rc = ahci_port_hardreset(ap, hard);
93817eab71eSMatthew Dillon 	} else {
9391980eff3SMatthew Dillon 		if (at)
9401980eff3SMatthew Dillon 			rc = ahci_pm_softreset(ap, at->at_target);
9411980eff3SMatthew Dillon 		else
94217eab71eSMatthew Dillon 			rc = ahci_port_softreset(ap);
94317eab71eSMatthew Dillon 	}
94417eab71eSMatthew Dillon 	return(rc);
94517eab71eSMatthew Dillon }
94617eab71eSMatthew Dillon 
94717eab71eSMatthew Dillon /*
948fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
949fd8bd957SMatthew Dillon  *
9501980eff3SMatthew Dillon  * (at) will be NULL when soft-resetting a directly-attached device, and
9511980eff3SMatthew Dillon  * non-NULL when soft-resetting a device through a port multiplier.
9521980eff3SMatthew Dillon  *
953fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
9541980eff3SMatthew Dillon  * a reset to the connected device using device commands.
955fd8bd957SMatthew Dillon  */
956258223a3SMatthew Dillon int
957258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
958258223a3SMatthew Dillon {
959258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
960258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
961258223a3SMatthew Dillon 	u_int8_t		*fis;
9623209f581SMatthew Dillon 	int			error;
963258223a3SMatthew Dillon 
9643209f581SMatthew Dillon 	error = EIO;
9651980eff3SMatthew Dillon 
966074579dfSMatthew Dillon 	if (bootverbose) {
9671980eff3SMatthew Dillon 		kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
9681980eff3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
969074579dfSMatthew Dillon 	}
9701980eff3SMatthew Dillon 
971258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
972258223a3SMatthew Dillon 
973258223a3SMatthew Dillon 	crit_enter();
9741980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
9751980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
976258223a3SMatthew Dillon 
9771980eff3SMatthew Dillon 	/*
9781980eff3SMatthew Dillon 	 * Remember port state in cmd (main to restore start/stop)
9791980eff3SMatthew Dillon 	 *
9801980eff3SMatthew Dillon 	 * Idle port.
9811980eff3SMatthew Dillon 	 */
982258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
983258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
984258223a3SMatthew Dillon 			PORTNAME(ap));
985258223a3SMatthew Dillon 		goto err;
986258223a3SMatthew Dillon 	}
987cf5f3a81SMatthew Dillon 
988cf5f3a81SMatthew Dillon 	/*
9891980eff3SMatthew Dillon 	 * Request CLO if device appears hung.
990cf5f3a81SMatthew Dillon 	 */
991258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
992258223a3SMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
993258223a3SMatthew Dillon 		ahci_port_clo(ap);
994258223a3SMatthew Dillon 	}
995258223a3SMatthew Dillon 
9961980eff3SMatthew Dillon 	/*
9971980eff3SMatthew Dillon 	 * This is an attempt to clear errors so a new signature will
9981980eff3SMatthew Dillon 	 * be latched.  It isn't working properly.  XXX
9991980eff3SMatthew Dillon 	 */
1000cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
10011980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1002258223a3SMatthew Dillon 
1003258223a3SMatthew Dillon 	/* Restart port */
100417eab71eSMatthew Dillon 	if (ahci_port_start(ap)) {
1005258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
1006258223a3SMatthew Dillon 		        PORTNAME(ap));
1007258223a3SMatthew Dillon 		goto err;
1008258223a3SMatthew Dillon 	}
1009258223a3SMatthew Dillon 
1010258223a3SMatthew Dillon 	/* Check whether CLO worked */
1011258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
1012258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1013258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
1014258223a3SMatthew Dillon 			PORTNAME(ap),
1015258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
1016258223a3SMatthew Dillon 			? "failed" : "unsupported");
10173209f581SMatthew Dillon 		error = EBUSY;
1018258223a3SMatthew Dillon 		goto err;
1019258223a3SMatthew Dillon 	}
1020258223a3SMatthew Dillon 
1021cec85a37SMatthew Dillon 	/*
1022cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
1023cec85a37SMatthew Dillon 	 *
1024cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1025cec85a37SMatthew Dillon 	 * everything.
10261067474aSMatthew Dillon 	 *
10271067474aSMatthew Dillon 	 * NOTE!  This CCB is used for both the first and second commands.
10281067474aSMatthew Dillon 	 *	  The second command must use CCB slot 1 to properly load
10291067474aSMatthew Dillon 	 *	  the signature.
1030cec85a37SMatthew Dillon 	 */
1031258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
103212feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
103312feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_EXCLUSIVE;
10341067474aSMatthew Dillon 	KKASSERT(ccb->ccb_slot == 1);
10351980eff3SMatthew Dillon 	ccb->ccb_xa.at = NULL;
1036258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1037258223a3SMatthew Dillon 
1038258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
1039cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
10401980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
10411980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
1042258223a3SMatthew Dillon 
1043258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1044258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1045258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
1046258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
1047258223a3SMatthew Dillon 
1048258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
104912feb904SMatthew Dillon 
1050831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
10515f8c1efdSMatthew Dillon 		kprintf("%s: First FIS failed\n", PORTNAME(ap));
1052258223a3SMatthew Dillon 		goto err;
1053cec85a37SMatthew Dillon 	}
1054258223a3SMatthew Dillon 
1055cec85a37SMatthew Dillon 	/*
1056831bc9e3SMatthew Dillon 	 * WARNING!	TIME SENSITIVE SPACE!	WARNING!
1057831bc9e3SMatthew Dillon 	 *
1058831bc9e3SMatthew Dillon 	 * The two FISes are supposed to be back to back.  Don't issue other
1059831bc9e3SMatthew Dillon 	 * commands or even delay if we can help it.
10601980eff3SMatthew Dillon 	 */
10611980eff3SMatthew Dillon 
10621980eff3SMatthew Dillon 	/*
1063cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
1064cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
1065cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
1066cec85a37SMatthew Dillon 	 * from the AHCI document.
1067cec85a37SMatthew Dillon 	 *
1068b089d0bfSMatthew Dillon 	 * Give the device 3ms before sending the second FIS.
1069cec85a37SMatthew Dillon 	 *
1070cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
1071cec85a37SMatthew Dillon 	 * everything.
1072cec85a37SMatthew Dillon 	 */
107312feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_POLL | ATA_F_AUTOSENSE | ATA_F_EXCLUSIVE;
107412feb904SMatthew Dillon 
1075cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
10761980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
10771980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_4BIT;
1078258223a3SMatthew Dillon 
1079258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
1080258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1081258223a3SMatthew Dillon 
1082258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
1083831bc9e3SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
10845f8c1efdSMatthew Dillon 		kprintf("%s: Second FIS failed\n", PORTNAME(ap));
1085258223a3SMatthew Dillon 		goto err;
1086cec85a37SMatthew Dillon 	}
1087258223a3SMatthew Dillon 
10881980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
10891980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1090258223a3SMatthew Dillon 		kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1091258223a3SMatthew Dillon 			PORTNAME(ap),
1092258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
10933209f581SMatthew Dillon 		error = EBUSY;
1094258223a3SMatthew Dillon 		goto err;
1095258223a3SMatthew Dillon 	}
10963209f581SMatthew Dillon 	ahci_os_sleep(10);
1097258223a3SMatthew Dillon 
1098fd8bd957SMatthew Dillon 	/*
1099fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
1100fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
1101fd8bd957SMatthew Dillon 	 *
1102fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
1103fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
1104fd8bd957SMatthew Dillon 	 * unexpectedly.
1105fd8bd957SMatthew Dillon 	 */
11061980eff3SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE) {
11071980eff3SMatthew Dillon 		ap->ap_type = ahci_port_signature_detect(ap, NULL);
1108fd8bd957SMatthew Dillon 	} else {
11091980eff3SMatthew Dillon 		if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
11101980eff3SMatthew Dillon 			kprintf("%s: device signature unexpectedly "
11111980eff3SMatthew Dillon 				"changed\n", PORTNAME(ap));
11123209f581SMatthew Dillon 			error = EBUSY; /* XXX */
1113fd8bd957SMatthew Dillon 		}
1114fd8bd957SMatthew Dillon 	}
11153209f581SMatthew Dillon 	error = 0;
11161980eff3SMatthew Dillon 
11173209f581SMatthew Dillon 	ahci_os_sleep(3);
1118258223a3SMatthew Dillon err:
1119258223a3SMatthew Dillon 	if (ccb != NULL) {
1120258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
11211980eff3SMatthew Dillon 
11221980eff3SMatthew Dillon 		/*
11231980eff3SMatthew Dillon 		 * If the target is busy use CLO to clear the busy
11241980eff3SMatthew Dillon 		 * condition.  The BSY should be cleared on the next
11251980eff3SMatthew Dillon 		 * start.
11261980eff3SMatthew Dillon 		 */
11271980eff3SMatthew Dillon 		if (ahci_pread(ap, AHCI_PREG_TFD) &
11281980eff3SMatthew Dillon 		    (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
11291980eff3SMatthew Dillon 			ahci_port_clo(ap);
11301980eff3SMatthew Dillon 		}
1131258223a3SMatthew Dillon 	}
1132258223a3SMatthew Dillon 
1133cf5f3a81SMatthew Dillon 	/*
1134cf5f3a81SMatthew Dillon 	 * If we failed to softreset make the port quiescent, otherwise
1135cf5f3a81SMatthew Dillon 	 * make sure the port's start/stop state matches what it was on
1136cf5f3a81SMatthew Dillon 	 * entry.
11371980eff3SMatthew Dillon 	 *
11381980eff3SMatthew Dillon 	 * Don't kill the port if the softreset is on a port multiplier
11391980eff3SMatthew Dillon 	 * target, that would kill all the targets!
1140cf5f3a81SMatthew Dillon 	 */
11413209f581SMatthew Dillon 	if (error) {
1142cf5f3a81SMatthew Dillon 		ahci_port_hardstop(ap);
11433209f581SMatthew Dillon 		/* ap_probe set to failed */
1144cf5f3a81SMatthew Dillon 	} else {
11453209f581SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_IDENT;
114612feb904SMatthew Dillon 		ap->ap_pmcount = 1;
11474c339a5fSMatthew Dillon 		ahci_port_start(ap);
1148cf5f3a81SMatthew Dillon 	}
11493209f581SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
1150258223a3SMatthew Dillon 	crit_exit();
1151258223a3SMatthew Dillon 
1152074579dfSMatthew Dillon 	if (bootverbose)
11531980eff3SMatthew Dillon 		kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
11541980eff3SMatthew Dillon 
11553209f581SMatthew Dillon 	return (error);
1156258223a3SMatthew Dillon }
1157258223a3SMatthew Dillon 
1158fd8bd957SMatthew Dillon /*
1159fd8bd957SMatthew Dillon  * AHCI port reset, Section 10.4.2
1160fd8bd957SMatthew Dillon  *
1161fd8bd957SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
1162fd8bd957SMatthew Dillon  * connected to the port could still end-up hung.
1163fd8bd957SMatthew Dillon  */
1164258223a3SMatthew Dillon int
11651980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard)
1166258223a3SMatthew Dillon {
1167258223a3SMatthew Dillon 	u_int32_t cmd, r;
116812feb904SMatthew Dillon 	u_int32_t data;
11693209f581SMatthew Dillon 	int	error;
11701980eff3SMatthew Dillon 	int	loop;
1171258223a3SMatthew Dillon 
117212feb904SMatthew Dillon 	if (bootverbose)
117312feb904SMatthew Dillon 		kprintf("%s: START HARDRESET\n", PORTNAME(ap));
11741980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
1175cf5f3a81SMatthew Dillon 
1176cf5f3a81SMatthew Dillon 	/*
11771980eff3SMatthew Dillon 	 * Idle the port,
11781980eff3SMatthew Dillon 	 */
11791980eff3SMatthew Dillon 	ahci_port_stop(ap, 0);
11801980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
11811980eff3SMatthew Dillon 
11821980eff3SMatthew Dillon 	/*
11831980eff3SMatthew Dillon 	 * The port may have been quiescent with its SUD bit cleared, so
11841980eff3SMatthew Dillon 	 * set the SUD (spin up device).
1185cf5f3a81SMatthew Dillon 	 */
1186cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1187cf5f3a81SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD;
1188cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1189258223a3SMatthew Dillon 
11901980eff3SMatthew Dillon 	/*
11914e21f4daSMatthew Dillon 	 * Perform device detection.
11921067474aSMatthew Dillon 	 *
11934e21f4daSMatthew Dillon 	 * NOTE!  AHCi_PREG_SCTL_DET_DISABLE seems to be highly unreliable
11944e21f4daSMatthew Dillon 	 *	  on multiple chipsets and can brick the chipset or even
11954e21f4daSMatthew Dillon 	 *	  the whole PC.  Never use it.
11961980eff3SMatthew Dillon 	 */
11971980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1198258223a3SMatthew Dillon 
11991980eff3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED;
12001980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
12013209f581SMatthew Dillon 	ahci_os_sleep(10);
12021980eff3SMatthew Dillon 
12031980eff3SMatthew Dillon 	/*
12041980eff3SMatthew Dillon 	 * Start transmitting COMRESET.  COMRESET must be sent for at
12051980eff3SMatthew Dillon 	 * least 1ms.
12061980eff3SMatthew Dillon 	 */
12071980eff3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1208074579dfSMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num))
1209258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
1210074579dfSMatthew Dillon 	else
1211258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
1212258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1213831bc9e3SMatthew Dillon 
1214831bc9e3SMatthew Dillon 	/*
1215831bc9e3SMatthew Dillon 	 * Through trial and error it seems to take around 100ms
1216831bc9e3SMatthew Dillon 	 * for the detect logic to settle down.  If this is too
1217831bc9e3SMatthew Dillon 	 * short the softreset code will fail.
1218831bc9e3SMatthew Dillon 	 */
1219831bc9e3SMatthew Dillon 	ahci_os_sleep(100);
1220cf5f3a81SMatthew Dillon 
1221cf5f3a81SMatthew Dillon 	/*
1222cf5f3a81SMatthew Dillon 	 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1223cf5f3a81SMatthew Dillon 	 * since we are hard-resetting the port we might as well clear
1224cf5f3a81SMatthew Dillon 	 * the whole enchillada
1225cf5f3a81SMatthew Dillon 	 */
1226cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1227cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1228258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
1229258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
1230258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1231258223a3SMatthew Dillon 
12321980eff3SMatthew Dillon 	/*
12331980eff3SMatthew Dillon 	 * Try to determine if there is a device on the port.
12341980eff3SMatthew Dillon 	 *
12351980eff3SMatthew Dillon 	 * Give the device 3/10 second to at least be detected.
12361980eff3SMatthew Dillon 	 * If we fail clear PRCS (phy detect) since we may cycled
12371980eff3SMatthew Dillon 	 * the phy and probably caused another PRCS interrupt.
12381980eff3SMatthew Dillon 	 */
123976497a9cSMatthew Dillon 	loop = 300;
124076497a9cSMatthew Dillon 	while (loop > 0) {
12411980eff3SMatthew Dillon 		r = ahci_pread(ap, AHCI_PREG_SSTS);
12421980eff3SMatthew Dillon 		if (r & AHCI_PREG_SSTS_DET)
12431980eff3SMatthew Dillon 			break;
124476497a9cSMatthew Dillon 		loop -= ahci_os_softsleep();
12451980eff3SMatthew Dillon 	}
12461980eff3SMatthew Dillon 	if (loop == 0) {
12471980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
1248074579dfSMatthew Dillon 		if (bootverbose) {
12491980eff3SMatthew Dillon 			kprintf("%s: Port appears to be unplugged\n",
12501980eff3SMatthew Dillon 				PORTNAME(ap));
1251074579dfSMatthew Dillon 		}
12523209f581SMatthew Dillon 		error = ENODEV;
125312feb904SMatthew Dillon 		goto done;
1254258223a3SMatthew Dillon 	}
1255258223a3SMatthew Dillon 
1256cec85a37SMatthew Dillon 	/*
12571980eff3SMatthew Dillon 	 * There is something on the port.  Give the device 3 seconds
12581980eff3SMatthew Dillon 	 * to fully negotiate.
12591980eff3SMatthew Dillon 	 */
126012feb904SMatthew Dillon 	if (ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
12611980eff3SMatthew Dillon 			  AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
1262074579dfSMatthew Dillon 		if (bootverbose) {
12631980eff3SMatthew Dillon 			kprintf("%s: Device may be powered down\n",
12641980eff3SMatthew Dillon 				PORTNAME(ap));
1265074579dfSMatthew Dillon 		}
12663209f581SMatthew Dillon 		error = ENODEV;
126712feb904SMatthew Dillon 		goto pmdetect;
12681980eff3SMatthew Dillon 	}
12691980eff3SMatthew Dillon 
127012feb904SMatthew Dillon 	/*
127112feb904SMatthew Dillon 	 * We got something that definitely looks like a device.  Give
127212feb904SMatthew Dillon 	 * the device time to send us its first D2H FIS.  Waiting for
127312feb904SMatthew Dillon 	 * BSY to clear accomplishes this.
127412feb904SMatthew Dillon 	 *
127512feb904SMatthew Dillon 	 * NOTE that a port multiplier may or may not clear BSY here,
127612feb904SMatthew Dillon 	 * depending on what is sitting in target 0 behind it.
127712feb904SMatthew Dillon 	 */
1278c408a8b3SMatthew Dillon 	ahci_flush_tfd(ap);
1279c408a8b3SMatthew Dillon 
128012feb904SMatthew Dillon 	if (ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
12811980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
128212feb904SMatthew Dillon 		error = EBUSY;
12831980eff3SMatthew Dillon 	} else {
12843209f581SMatthew Dillon 		error = 0;
12851980eff3SMatthew Dillon 	}
1286258223a3SMatthew Dillon 
128712feb904SMatthew Dillon pmdetect:
1288cf5f3a81SMatthew Dillon 	/*
128912feb904SMatthew Dillon 	 * Do the PM port probe regardless of how things turned out on
129012feb904SMatthew Dillon 	 * the BSY check.
1291cf5f3a81SMatthew Dillon 	 */
129212feb904SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM)
129312feb904SMatthew Dillon 		error = ahci_pm_port_probe(ap, error);
129412feb904SMatthew Dillon 
129512feb904SMatthew Dillon done:
129612feb904SMatthew Dillon 	/*
129712feb904SMatthew Dillon 	 * Finish up.
129812feb904SMatthew Dillon 	 */
129912feb904SMatthew Dillon 	switch(error) {
130012feb904SMatthew Dillon 	case 0:
130112feb904SMatthew Dillon 		/*
130212feb904SMatthew Dillon 		 * All good, make sure the port is running and set the
130312feb904SMatthew Dillon 		 * probe state.  Ignore the signature junk (it's unreliable)
130412feb904SMatthew Dillon 		 * until we get to the softreset code.
130512feb904SMatthew Dillon 		 */
130612feb904SMatthew Dillon 		if (ahci_port_start(ap)) {
130712feb904SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
130812feb904SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
130912feb904SMatthew Dillon 			error = EBUSY;
131012feb904SMatthew Dillon 			goto done;
131112feb904SMatthew Dillon 		}
1312f4553de1SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
1313f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_GOOD;
1314f4553de1SMatthew Dillon 		else
1315f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
131612feb904SMatthew Dillon 		break;
131712feb904SMatthew Dillon 	case ENODEV:
1318fd8bd957SMatthew Dillon 		/*
131912feb904SMatthew Dillon 		 * Normal device probe failure
13201980eff3SMatthew Dillon 		 */
132112feb904SMatthew Dillon 		data = ahci_pread(ap, AHCI_PREG_SSTS);
13221980eff3SMatthew Dillon 
132312feb904SMatthew Dillon 		switch(data & AHCI_PREG_SSTS_DET) {
132412feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
132512feb904SMatthew Dillon 			kprintf("%s: Device not communicating\n",
13261980eff3SMatthew Dillon 				PORTNAME(ap));
132712feb904SMatthew Dillon 			break;
132812feb904SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
132912feb904SMatthew Dillon 			kprintf("%s: PHY offline\n",
133012feb904SMatthew Dillon 				PORTNAME(ap));
133112feb904SMatthew Dillon 			break;
133212feb904SMatthew Dillon 		default:
133312feb904SMatthew Dillon 			kprintf("%s: No device detected\n",
133412feb904SMatthew Dillon 				PORTNAME(ap));
133512feb904SMatthew Dillon 			break;
13361980eff3SMatthew Dillon 		}
133712feb904SMatthew Dillon 		ahci_port_hardstop(ap);
133812feb904SMatthew Dillon 		break;
133912feb904SMatthew Dillon 	default:
13401980eff3SMatthew Dillon 		/*
134112feb904SMatthew Dillon 		 * Abnormal probe (EBUSY)
13421980eff3SMatthew Dillon 		 */
134312feb904SMatthew Dillon 		kprintf("%s: Device on port is bricked\n",
134412feb904SMatthew Dillon 			PORTNAME(ap));
134512feb904SMatthew Dillon 		ahci_port_hardstop(ap);
134612feb904SMatthew Dillon #if 0
134712feb904SMatthew Dillon 		rc = ahci_port_reset(ap, atx, 0);
134812feb904SMatthew Dillon 		if (rc) {
134912feb904SMatthew Dillon 			kprintf("%s: Unable unbrick device\n",
135012feb904SMatthew Dillon 				PORTNAME(ap));
13511980eff3SMatthew Dillon 		} else {
135212feb904SMatthew Dillon 			kprintf("%s: Successfully unbricked\n",
13533209f581SMatthew Dillon 				PORTNAME(ap));
135412feb904SMatthew Dillon 		}
135512feb904SMatthew Dillon #endif
135612feb904SMatthew Dillon 		break;
13573209f581SMatthew Dillon 	}
13581067474aSMatthew Dillon 
13591067474aSMatthew Dillon 	/*
136012feb904SMatthew Dillon 	 * Clean up
13611067474aSMatthew Dillon 	 */
136212feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
136312feb904SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
13643209f581SMatthew Dillon 
136512feb904SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
13661980eff3SMatthew Dillon 
136712feb904SMatthew Dillon 	if (bootverbose)
136812feb904SMatthew Dillon 		kprintf("%s: END HARDRESET %d\n", PORTNAME(ap), error);
1369831bc9e3SMatthew Dillon 	return (error);
13701980eff3SMatthew Dillon }
13711980eff3SMatthew Dillon 
13721980eff3SMatthew Dillon /*
1373cf5f3a81SMatthew Dillon  * Hard-stop on hot-swap device removal.  See 10.10.1
1374cf5f3a81SMatthew Dillon  *
1375cf5f3a81SMatthew Dillon  * Place the port in a mode that will allow it to detect hot-swap insertions.
1376cf5f3a81SMatthew Dillon  * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1377cf5f3a81SMatthew Dillon  * seem to do the job.
1378cf5f3a81SMatthew Dillon  */
1379cf5f3a81SMatthew Dillon void
1380cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap)
1381cf5f3a81SMatthew Dillon {
138276497a9cSMatthew Dillon 	struct ahci_ccb *ccb;
13831980eff3SMatthew Dillon 	struct ata_port *at;
1384cf5f3a81SMatthew Dillon 	u_int32_t r;
1385cf5f3a81SMatthew Dillon 	u_int32_t cmd;
138676497a9cSMatthew Dillon 	int slot;
13871980eff3SMatthew Dillon 	int i;
1388cf5f3a81SMatthew Dillon 
1389cf5f3a81SMatthew Dillon 	/*
1390cf5f3a81SMatthew Dillon 	 * Stop the port.  We can't modify things like SUD if the port
1391cf5f3a81SMatthew Dillon 	 * is running.
1392cf5f3a81SMatthew Dillon 	 */
1393cf5f3a81SMatthew Dillon 	ap->ap_state = AP_S_FATAL_ERROR;
13941980eff3SMatthew Dillon 	ap->ap_probe = ATA_PROBE_FAILED;
13951980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1396cf5f3a81SMatthew Dillon 	ahci_port_stop(ap, 0);
1397cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
1398cf5f3a81SMatthew Dillon 
1399cf5f3a81SMatthew Dillon 	/*
14001980eff3SMatthew Dillon 	 * Clean up AT sub-ports on SATA port.
14011980eff3SMatthew Dillon 	 */
14021980eff3SMatthew Dillon 	for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
1403b012a2caSMatthew Dillon 		at = ap->ap_ata[i];
14041980eff3SMatthew Dillon 		at->at_type = ATA_PORT_T_NONE;
14053209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_FAILED;
14061980eff3SMatthew Dillon 	}
14071980eff3SMatthew Dillon 
14081980eff3SMatthew Dillon 	/*
14091980eff3SMatthew Dillon 	 * Turn off port-multiplier control bit
14101980eff3SMatthew Dillon 	 */
14111980eff3SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_PMA) {
14121980eff3SMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_PMA;
14131980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
14141980eff3SMatthew Dillon 	}
14151980eff3SMatthew Dillon 
14161980eff3SMatthew Dillon 	/*
1417cf5f3a81SMatthew Dillon 	 * Make sure FRE is active.  There isn't anything we can do if it
1418cf5f3a81SMatthew Dillon 	 * fails so just ignore errors.
1419cf5f3a81SMatthew Dillon 	 */
1420cf5f3a81SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1421cf5f3a81SMatthew Dillon 		cmd |= AHCI_PREG_CMD_FRE;
1422cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1423cf5f3a81SMatthew Dillon 		if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1424cf5f3a81SMatthew Dillon 			ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1425cf5f3a81SMatthew Dillon 	}
1426cf5f3a81SMatthew Dillon 
1427cf5f3a81SMatthew Dillon 	/*
1428cf5f3a81SMatthew Dillon 	 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1429cf5f3a81SMatthew Dillon 	 * 10.10.1 place us in the Listen state.
1430cf5f3a81SMatthew Dillon 	 *
1431cf5f3a81SMatthew Dillon 	 * Deactivating SUD only applies if the controller supports SUD.
1432cf5f3a81SMatthew Dillon 	 */
1433cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
14343209f581SMatthew Dillon 	ahci_os_sleep(1);
1435cf5f3a81SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_SUD) {
1436cf5f3a81SMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_SUD;
1437cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1438cf5f3a81SMatthew Dillon 	}
14393209f581SMatthew Dillon 	ahci_os_sleep(1);
1440cf5f3a81SMatthew Dillon 
1441cf5f3a81SMatthew Dillon 	/*
1442cf5f3a81SMatthew Dillon 	 * Transition su to the spin-up state.  HVA shall send COMRESET and
1443cf5f3a81SMatthew Dillon 	 * begin initialization sequence (whatever that means).
1444cf5f3a81SMatthew Dillon 	 *
1445cf5f3a81SMatthew Dillon 	 * This only applies if the controller supports SUD.
14464e21f4daSMatthew Dillon 	 * NEVER use AHCI_PREG_DET_DISABLE.
1447cf5f3a81SMatthew Dillon 	 */
1448cf5f3a81SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD;
1449cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
14503209f581SMatthew Dillon 	ahci_os_sleep(1);
1451cf5f3a81SMatthew Dillon 
1452cf5f3a81SMatthew Dillon 	/*
1453cf5f3a81SMatthew Dillon 	 * Transition us to the Reset state.  Theoretically we send a
1454cf5f3a81SMatthew Dillon 	 * continuous stream of COMRESETs in this state.
1455cf5f3a81SMatthew Dillon 	 */
1456cf5f3a81SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1457cf5f3a81SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
1458cf5f3a81SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1459cf5f3a81SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
1460cf5f3a81SMatthew Dillon 	} else {
1461cf5f3a81SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
1462cf5f3a81SMatthew Dillon 	}
1463cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
14643209f581SMatthew Dillon 	ahci_os_sleep(1);
1465cf5f3a81SMatthew Dillon 
1466cf5f3a81SMatthew Dillon 	/*
1467cf5f3a81SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
1468cf5f3a81SMatthew Dillon 	 */
1469cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1470cf5f3a81SMatthew Dillon 
1471cf5f3a81SMatthew Dillon 	/*
147276497a9cSMatthew Dillon 	 * Clean out pending ccbs
147376497a9cSMatthew Dillon 	 */
147476497a9cSMatthew Dillon 	while (ap->ap_active) {
147576497a9cSMatthew Dillon 		slot = ffs(ap->ap_active) - 1;
147676497a9cSMatthew Dillon 		ap->ap_active &= ~(1 << slot);
147776497a9cSMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
147876497a9cSMatthew Dillon 		--ap->ap_active_cnt;
147976497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
148076497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
148176497a9cSMatthew Dillon 			callout_stop(&ccb->ccb_timeout);
148276497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
148376497a9cSMatthew Dillon 		}
148476497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
148576497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
148676497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
148776497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
148876497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
148976497a9cSMatthew Dillon 	}
149076497a9cSMatthew Dillon 	while (ap->ap_sactive) {
149176497a9cSMatthew Dillon 		slot = ffs(ap->ap_sactive) - 1;
149276497a9cSMatthew Dillon 		ap->ap_sactive &= ~(1 << slot);
149376497a9cSMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
149476497a9cSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
149576497a9cSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_RUNNING) {
149676497a9cSMatthew Dillon 			callout_stop(&ccb->ccb_timeout);
149776497a9cSMatthew Dillon 			ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
149876497a9cSMatthew Dillon 		}
149976497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~(ATA_F_TIMEOUT_DESIRED |
150076497a9cSMatthew Dillon 				       ATA_F_TIMEOUT_EXPIRED);
150176497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
150276497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
150376497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
150476497a9cSMatthew Dillon 	}
150576497a9cSMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
150676497a9cSMatthew Dillon 
150776497a9cSMatthew Dillon 	while ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) != NULL) {
150876497a9cSMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
150976497a9cSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
151076497a9cSMatthew Dillon 		ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_DESIRED;
151176497a9cSMatthew Dillon 		ccb->ccb_done(ccb);
151276497a9cSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
151376497a9cSMatthew Dillon 	}
151476497a9cSMatthew Dillon 
151576497a9cSMatthew Dillon 	/*
1516cf5f3a81SMatthew Dillon 	 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1517cf5f3a81SMatthew Dillon 	 * hopefully send us a DIAG_X-related interrupt if it receives
1518cf5f3a81SMatthew Dillon 	 * a COMINIT, and if not that then at least a Phy transition
1519cf5f3a81SMatthew Dillon 	 * interrupt.
1520cf5f3a81SMatthew Dillon 	 *
1521cf5f3a81SMatthew Dillon 	 * If we transition INIT from 1->0 to begin the initalization
1522cf5f3a81SMatthew Dillon 	 * sequence it is unclear if that sequence will remain active
1523cf5f3a81SMatthew Dillon 	 * until the next device insertion.
1524cf5f3a81SMatthew Dillon 	 *
1525cf5f3a81SMatthew Dillon 	 * If we go back to the listen state it is unclear if the
1526cf5f3a81SMatthew Dillon 	 * device will actually send us a COMINIT, since we aren't
1527cf5f3a81SMatthew Dillon 	 * sending any COMRESET's
1528cf5f3a81SMatthew Dillon 	 */
1529cf5f3a81SMatthew Dillon 	/* NOP */
1530cf5f3a81SMatthew Dillon }
1531cf5f3a81SMatthew Dillon 
1532cf5f3a81SMatthew Dillon /*
1533c408a8b3SMatthew Dillon  * We can't loop on the X bit, a continuous COMINIT received will make
1534c408a8b3SMatthew Dillon  * it loop forever.  Just assume one event has built up and clear X
1535c408a8b3SMatthew Dillon  * so the task file descriptor can update.
1536cf5f3a81SMatthew Dillon  */
1537cf5f3a81SMatthew Dillon void
1538cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap)
1539cf5f3a81SMatthew Dillon {
1540cf5f3a81SMatthew Dillon 	u_int32_t r;
1541cf5f3a81SMatthew Dillon 
1542cf5f3a81SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SERR);
1543c408a8b3SMatthew Dillon 	if (r & AHCI_PREG_SERR_DIAG_X)
15441980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
1545cf5f3a81SMatthew Dillon }
1546cf5f3a81SMatthew Dillon 
1547cf5f3a81SMatthew Dillon /*
1548fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
1549fd8bd957SMatthew Dillon  * DISK.
1550fd8bd957SMatthew Dillon  */
1551fd8bd957SMatthew Dillon int
15521980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1553fd8bd957SMatthew Dillon {
1554fd8bd957SMatthew Dillon 	u_int32_t sig;
1555fd8bd957SMatthew Dillon 
1556fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
1557074579dfSMatthew Dillon 	if (bootverbose)
15581980eff3SMatthew Dillon 		kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1559fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1560fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
15611980eff3SMatthew Dillon 	} else if ((sig & 0xffff0000) ==
15621980eff3SMatthew Dillon 		 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
15631980eff3SMatthew Dillon 		return(ATA_PORT_T_PM);
1564fd8bd957SMatthew Dillon 	} else {
1565fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
1566fd8bd957SMatthew Dillon 	}
1567fd8bd957SMatthew Dillon }
1568fd8bd957SMatthew Dillon 
1569fd8bd957SMatthew Dillon /*
1570fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
1571fd8bd957SMatthew Dillon  */
1572258223a3SMatthew Dillon int
1573258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
1574258223a3SMatthew Dillon {
1575258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1576258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1577258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1578258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
1579258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1580258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
1581258223a3SMatthew Dillon 	int				error;
1582258223a3SMatthew Dillon 
1583258223a3SMatthew Dillon 	if (xa->datalen == 0) {
1584258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
1585258223a3SMatthew Dillon 		return (0);
1586258223a3SMatthew Dillon 	}
1587258223a3SMatthew Dillon 
1588258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
1589258223a3SMatthew Dillon 				xa->data, xa->datalen,
1590258223a3SMatthew Dillon 				ahci_load_prdt_callback,
1591258223a3SMatthew Dillon 				&prdt,
1592258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
1593258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1594258223a3SMatthew Dillon 	if (error != 0) {
1595258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1596258223a3SMatthew Dillon 		return (1);
1597258223a3SMatthew Dillon 	}
159812feb904SMatthew Dillon #if 0
1599258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
1600258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
160112feb904SMatthew Dillon #endif
1602258223a3SMatthew Dillon 
1603258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1604258223a3SMatthew Dillon 
1605b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_READ)
1606b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREREAD);
1607b012a2caSMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
1608b012a2caSMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap, BUS_DMASYNC_PREWRITE);
1609258223a3SMatthew Dillon 
1610258223a3SMatthew Dillon 	return (0);
1611258223a3SMatthew Dillon }
1612258223a3SMatthew Dillon 
1613258223a3SMatthew Dillon /*
1614258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
1615258223a3SMatthew Dillon  * list is a temporary structure.
1616258223a3SMatthew Dillon  */
1617258223a3SMatthew Dillon static
1618258223a3SMatthew Dillon void
1619258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1620258223a3SMatthew Dillon 			int error)
1621258223a3SMatthew Dillon {
1622258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
1623258223a3SMatthew Dillon 	u_int64_t addr;
1624258223a3SMatthew Dillon 
1625258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
1626258223a3SMatthew Dillon 
1627258223a3SMatthew Dillon 	while (nsegs) {
1628258223a3SMatthew Dillon 		addr = segs->ds_addr;
1629258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1630258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
1631258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
1632258223a3SMatthew Dillon 		--nsegs;
1633258223a3SMatthew Dillon 		if (nsegs)
1634258223a3SMatthew Dillon 			++prd;
1635258223a3SMatthew Dillon 		++segs;
1636258223a3SMatthew Dillon 	}
1637258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
1638258223a3SMatthew Dillon }
1639258223a3SMatthew Dillon 
1640258223a3SMatthew Dillon void
1641258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
1642258223a3SMatthew Dillon {
1643258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1644258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1645258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1646258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1647258223a3SMatthew Dillon 
1648258223a3SMatthew Dillon 	if (xa->datalen != 0) {
1649b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_READ) {
1650258223a3SMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
1651b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTREAD);
1652b012a2caSMatthew Dillon 		}
1653b012a2caSMatthew Dillon 		if (xa->flags & ATA_F_WRITE) {
1654b012a2caSMatthew Dillon 			bus_dmamap_sync(sc->sc_tag_data, dmap,
1655b012a2caSMatthew Dillon 					BUS_DMASYNC_POSTWRITE);
1656b012a2caSMatthew Dillon 		}
1657258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
1658258223a3SMatthew Dillon 
1659f7d09f74SMatthew Dillon 		/*
1660f7d09f74SMatthew Dillon 		 * prdbc is only updated by hardware for non-NCQ commands.
1661f7d09f74SMatthew Dillon 		 */
1662f7d09f74SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1663f7d09f74SMatthew Dillon 			xa->resid = 0;
1664f7d09f74SMatthew Dillon 		} else {
166550a3ecb6SMatthew Dillon 			if (ccb->ccb_cmd_hdr->prdbc == 0 &&
166650a3ecb6SMatthew Dillon 			    ccb->ccb_xa.state == ATA_S_COMPLETE) {
1667f7d09f74SMatthew Dillon 				kprintf("%s: WARNING!  Unload prdbc resid "
1668f7d09f74SMatthew Dillon 					"was zero! tag=%d\n",
166912feb904SMatthew Dillon 					ATANAME(ap, xa->at), ccb->ccb_slot);
167012feb904SMatthew Dillon 			}
1671258223a3SMatthew Dillon 			xa->resid = xa->datalen -
1672258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
1673258223a3SMatthew Dillon 		}
1674258223a3SMatthew Dillon 	}
1675f7d09f74SMatthew Dillon }
1676258223a3SMatthew Dillon 
16775f8c1efdSMatthew Dillon /*
16785f8c1efdSMatthew Dillon  * Start a command and poll for completion.
16795f8c1efdSMatthew Dillon  *
16803209f581SMatthew Dillon  * timeout is in ms and only counts once the command gets on-chip.
16813209f581SMatthew Dillon  *
1682831bc9e3SMatthew Dillon  * Returns ATA_S_* state, compare against ATA_S_COMPLETE to determine
1683831bc9e3SMatthew Dillon  * that no error occured.
1684831bc9e3SMatthew Dillon  *
16855f8c1efdSMatthew Dillon  * NOTE: If the caller specifies a NULL timeout function the caller is
16865f8c1efdSMatthew Dillon  *	 responsible for clearing hardware state on failure, but we will
16875f8c1efdSMatthew Dillon  *	 deal with removing the ccb from any pending queue.
16885f8c1efdSMatthew Dillon  *
16895f8c1efdSMatthew Dillon  * NOTE: NCQ should never be used with this function.
1690cf5f3a81SMatthew Dillon  *
1691cf5f3a81SMatthew Dillon  * NOTE: If the port is in a failed state and stopped we do not try
1692cf5f3a81SMatthew Dillon  *	 to activate the ccb.
16935f8c1efdSMatthew Dillon  */
1694258223a3SMatthew Dillon int
1695831bc9e3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout,
1696831bc9e3SMatthew Dillon 	  void (*timeout_fn)(struct ahci_ccb *))
1697258223a3SMatthew Dillon {
1698258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
1699258223a3SMatthew Dillon 
1700cf5f3a81SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1701cf5f3a81SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
1702831bc9e3SMatthew Dillon 		return(ccb->ccb_xa.state);
1703cf5f3a81SMatthew Dillon 	}
1704258223a3SMatthew Dillon 	crit_enter();
170512feb904SMatthew Dillon #if 0
170612feb904SMatthew Dillon 	kprintf("%s: Start command %02x tag=%d\n",
170712feb904SMatthew Dillon 		ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
170812feb904SMatthew Dillon 		ccb->ccb_xa.fis->command, ccb->ccb_slot);
170912feb904SMatthew Dillon #endif
1710258223a3SMatthew Dillon 	ahci_start(ccb);
17111980eff3SMatthew Dillon 
1712258223a3SMatthew Dillon 	do {
1713f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
1714831bc9e3SMatthew Dillon 		switch(ccb->ccb_xa.state) {
1715831bc9e3SMatthew Dillon 		case ATA_S_ONCHIP:
1716831bc9e3SMatthew Dillon 			timeout -= ahci_os_softsleep();
1717f4553de1SMatthew Dillon 			break;
1718831bc9e3SMatthew Dillon 		case ATA_S_PENDING:
1719831bc9e3SMatthew Dillon 			ahci_os_softsleep();
1720831bc9e3SMatthew Dillon 			ahci_check_active_timeouts(ap);
1721831bc9e3SMatthew Dillon 			break;
1722831bc9e3SMatthew Dillon 		default:
1723831bc9e3SMatthew Dillon 			crit_exit();
1724831bc9e3SMatthew Dillon 			return (ccb->ccb_xa.state);
1725f4553de1SMatthew Dillon 		}
17263209f581SMatthew Dillon 	} while (timeout > 0);
17275f8c1efdSMatthew Dillon 
1728831bc9e3SMatthew Dillon 	kprintf("%s: Poll timeout slot %d CMD: %b TFD: 0x%b SERR: %b\n",
1729831bc9e3SMatthew Dillon 		ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot,
1730831bc9e3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
1731831bc9e3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS,
1732831bc9e3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SERR), AHCI_PFMT_SERR);
17335f8c1efdSMatthew Dillon 
1734258223a3SMatthew Dillon 	timeout_fn(ccb);
1735831bc9e3SMatthew Dillon 
1736258223a3SMatthew Dillon 	crit_exit();
1737258223a3SMatthew Dillon 
1738831bc9e3SMatthew Dillon 	return(ccb->ccb_xa.state);
1739831bc9e3SMatthew Dillon }
1740831bc9e3SMatthew Dillon 
1741831bc9e3SMatthew Dillon /*
1742831bc9e3SMatthew Dillon  * When polling we have to check if the currently active CCB(s)
1743831bc9e3SMatthew Dillon  * have timed out as the callout will be deadlocked while we
1744831bc9e3SMatthew Dillon  * hold the port lock.
1745831bc9e3SMatthew Dillon  */
1746831bc9e3SMatthew Dillon void
1747831bc9e3SMatthew Dillon ahci_check_active_timeouts(struct ahci_port *ap)
1748831bc9e3SMatthew Dillon {
1749831bc9e3SMatthew Dillon 	struct ahci_ccb *ccb;
1750831bc9e3SMatthew Dillon 	u_int32_t mask;
1751831bc9e3SMatthew Dillon 	int tag;
1752831bc9e3SMatthew Dillon 
1753831bc9e3SMatthew Dillon 	mask = ap->ap_active | ap->ap_sactive;
1754831bc9e3SMatthew Dillon 	while (mask) {
1755831bc9e3SMatthew Dillon 		tag = ffs(mask) - 1;
1756831bc9e3SMatthew Dillon 		mask &= ~(1 << tag);
1757831bc9e3SMatthew Dillon 		ccb = &ap->ap_ccbs[tag];
1758831bc9e3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
1759831bc9e3SMatthew Dillon 			ahci_ata_cmd_timeout(ccb);
1760831bc9e3SMatthew Dillon 		}
1761831bc9e3SMatthew Dillon 	}
1762258223a3SMatthew Dillon }
1763258223a3SMatthew Dillon 
17643209f581SMatthew Dillon static
17653209f581SMatthew Dillon __inline
17663209f581SMatthew Dillon void
17673209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb)
17683209f581SMatthew Dillon {
17693209f581SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
17703209f581SMatthew Dillon 		ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
17713209f581SMatthew Dillon 		callout_reset(&ccb->ccb_timeout,
17723209f581SMatthew Dillon 			      (ccb->ccb_xa.timeout * hz + 999) / 1000,
17733209f581SMatthew Dillon 			      ahci_ata_cmd_timeout_unserialized, ccb);
17743209f581SMatthew Dillon 	}
17753209f581SMatthew Dillon }
17763209f581SMatthew Dillon 
1777258223a3SMatthew Dillon void
1778258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
1779258223a3SMatthew Dillon {
1780258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1781258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1782258223a3SMatthew Dillon 
1783258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1784258223a3SMatthew Dillon 
1785258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
1786258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
1787258223a3SMatthew Dillon 
1788258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
1789258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
1790258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1791258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1792258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
1793258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1794258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1795258223a3SMatthew Dillon 
1796258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
1797258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
1798258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1799258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
1800258223a3SMatthew Dillon 
18011980eff3SMatthew Dillon 	/*
18024c339a5fSMatthew Dillon 	 * There's no point trying to optimize this, it only shaves a few
18034c339a5fSMatthew Dillon 	 * nanoseconds so just queue the command and call our generic issue.
18041980eff3SMatthew Dillon 	 */
18054c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, ccb);
1806258223a3SMatthew Dillon }
1807258223a3SMatthew Dillon 
1808831bc9e3SMatthew Dillon /*
1809831bc9e3SMatthew Dillon  * While holding the port lock acquire exclusive access to the port.
1810831bc9e3SMatthew Dillon  *
1811831bc9e3SMatthew Dillon  * This is used when running the state machine to initialize and identify
1812831bc9e3SMatthew Dillon  * targets over a port multiplier.  Setting exclusive access prevents
1813831bc9e3SMatthew Dillon  * ahci_port_intr() from activating any requests sitting on the pending
1814831bc9e3SMatthew Dillon  * queue.
1815831bc9e3SMatthew Dillon  */
1816831bc9e3SMatthew Dillon void
1817831bc9e3SMatthew Dillon ahci_beg_exclusive_access(struct ahci_port *ap, struct ata_port *at)
1818831bc9e3SMatthew Dillon {
1819831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) == 0);
1820831bc9e3SMatthew Dillon 	ap->ap_flags |= AP_F_EXCLUSIVE_ACCESS;
1821831bc9e3SMatthew Dillon 	while (ap->ap_active || ap->ap_sactive) {
1822831bc9e3SMatthew Dillon 		ahci_port_intr(ap, 1);
1823831bc9e3SMatthew Dillon 		ahci_os_softsleep();
1824831bc9e3SMatthew Dillon 	}
1825831bc9e3SMatthew Dillon }
1826831bc9e3SMatthew Dillon 
1827831bc9e3SMatthew Dillon void
1828831bc9e3SMatthew Dillon ahci_end_exclusive_access(struct ahci_port *ap, struct ata_port *at)
1829831bc9e3SMatthew Dillon {
1830831bc9e3SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) != 0);
1831831bc9e3SMatthew Dillon 	ap->ap_flags &= ~AP_F_EXCLUSIVE_ACCESS;
18324c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
1833831bc9e3SMatthew Dillon }
1834831bc9e3SMatthew Dillon 
183512feb904SMatthew Dillon #if 0
183612feb904SMatthew Dillon 
183712feb904SMatthew Dillon static void
183812feb904SMatthew Dillon fubar(struct ahci_ccb *ccb)
183912feb904SMatthew Dillon {
184012feb904SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
184112feb904SMatthew Dillon 	struct ahci_cmd_hdr	*cmd;
184212feb904SMatthew Dillon 	struct ahci_cmd_table	*tab;
184312feb904SMatthew Dillon 	struct ahci_prdt	*prdt;
184412feb904SMatthew Dillon 	int i;
184512feb904SMatthew Dillon 
184612feb904SMatthew Dillon 	kprintf("%s: ISSUE %02x\n",
184712feb904SMatthew Dillon 		ATANAME(ap, ccb->ccb_xa.at),
184812feb904SMatthew Dillon 		ccb->ccb_xa.fis->command);
184912feb904SMatthew Dillon 	cmd = ccb->ccb_cmd_hdr;
185012feb904SMatthew Dillon 	tab = ccb->ccb_cmd_table;
185112feb904SMatthew Dillon 	prdt = ccb->ccb_cmd_table->prdt;
185212feb904SMatthew Dillon 	kprintf("cmd flags=%04x prdtl=%d prdbc=%d ctba=%08x%08x\n",
185312feb904SMatthew Dillon 		cmd->flags, cmd->prdtl, cmd->prdbc,
185412feb904SMatthew Dillon 		cmd->ctba_hi, cmd->ctba_lo);
185512feb904SMatthew Dillon 	for (i = 0; i < cmd->prdtl; ++i) {
185612feb904SMatthew Dillon 		kprintf("\t%d dba=%08x%08x res=%08x flags=%08x\n",
185712feb904SMatthew Dillon 			i, prdt->dba_hi, prdt->dba_lo, prdt->reserved,
185812feb904SMatthew Dillon 			prdt->flags);
185912feb904SMatthew Dillon 	}
186012feb904SMatthew Dillon 	kprintf("tab\n");
186112feb904SMatthew Dillon }
186212feb904SMatthew Dillon 
186312feb904SMatthew Dillon #endif
186412feb904SMatthew Dillon 
18651980eff3SMatthew Dillon /*
18664c339a5fSMatthew Dillon  * If ccb is not NULL enqueue and/or issue it.
18674c339a5fSMatthew Dillon  *
18684c339a5fSMatthew Dillon  * If ccb is NULL issue whatever we can from the queue.  However, nothing
18694c339a5fSMatthew Dillon  * new is issued if the exclusive access flag is set or expired ccb's are
18704c339a5fSMatthew Dillon  * present.
18714c339a5fSMatthew Dillon  *
18724c339a5fSMatthew Dillon  * If existing commands are still active (ap_active/ap_sactive) we can only
18734c339a5fSMatthew Dillon  * issue matching new commands.
18741980eff3SMatthew Dillon  */
18754c339a5fSMatthew Dillon void
18764c339a5fSMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, struct ahci_ccb *ccb)
18774c339a5fSMatthew Dillon {
18784c339a5fSMatthew Dillon 	u_int32_t		mask;
18794c339a5fSMatthew Dillon 	int			limit;
1880258223a3SMatthew Dillon 
18811980eff3SMatthew Dillon 	/*
18824c339a5fSMatthew Dillon 	 * Enqueue the ccb.
18834c339a5fSMatthew Dillon 	 *
18844c339a5fSMatthew Dillon 	 * If just running the queue and in exclusive access mode we
18854c339a5fSMatthew Dillon 	 * just return.  Also in this case if there are any expired ccb's
18864c339a5fSMatthew Dillon 	 * we want to clear the queue so the port can be safely stopped.
18874c339a5fSMatthew Dillon 	 */
18884c339a5fSMatthew Dillon 	if (ccb) {
18894c339a5fSMatthew Dillon 		TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
18904c339a5fSMatthew Dillon 	} else if ((ap->ap_flags & AP_F_EXCLUSIVE_ACCESS) || ap->ap_expired) {
18914c339a5fSMatthew Dillon 		return;
18924c339a5fSMatthew Dillon 	}
18934c339a5fSMatthew Dillon 
18944c339a5fSMatthew Dillon 	/*
18954c339a5fSMatthew Dillon 	 * Pull the next ccb off the queue and run it if possible.
18964c339a5fSMatthew Dillon 	 */
18974c339a5fSMatthew Dillon 	if ((ccb = TAILQ_FIRST(&ap->ap_ccb_pending)) == NULL)
18984c339a5fSMatthew Dillon 		return;
18994c339a5fSMatthew Dillon 
190012feb904SMatthew Dillon 	/*
190112feb904SMatthew Dillon 	 * Handle exclusivity requirements.
190212feb904SMatthew Dillon 	 *
190312feb904SMatthew Dillon 	 * ATA_F_EXCLUSIVE is used when we want to be the only command
190412feb904SMatthew Dillon 	 * running.
190512feb904SMatthew Dillon 	 *
190612feb904SMatthew Dillon 	 * ATA_F_AUTOSENSE is used when we want the D2H rfis loaded
190712feb904SMatthew Dillon 	 * back into the ccb on a normal (non-errored) command completion.
190812feb904SMatthew Dillon 	 * For example, for PM requests to target 15.  Because the AHCI
190912feb904SMatthew Dillon 	 * spec does not stop the command processor and has only one rfis
191012feb904SMatthew Dillon 	 * area (for non-FBSS anyway), AUTOSENSE currently implies EXCLUSIVE.
191112feb904SMatthew Dillon 	 * Otherwise multiple completions can destroy the rfis data before
191212feb904SMatthew Dillon 	 * we have a chance to copy it.
191312feb904SMatthew Dillon 	 */
191412feb904SMatthew Dillon 	if (ap->ap_active & ~ap->ap_expired) {
191512feb904SMatthew Dillon 		/*
191612feb904SMatthew Dillon 		 * There may be multiple ccb's already running,
191712feb904SMatthew Dillon 		 * if any are running and ap_run_flags sets
191812feb904SMatthew Dillon 		 * one of these flags then we know only one is
191912feb904SMatthew Dillon 		 * running.
192012feb904SMatthew Dillon 		 *
192112feb904SMatthew Dillon 		 * XXX Current AUTOSENSE code forces exclusivity
192212feb904SMatthew Dillon 		 *     to simplify the code.
192312feb904SMatthew Dillon 		 */
192412feb904SMatthew Dillon 		if (ap->ap_run_flags &
192512feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
192612feb904SMatthew Dillon 			return;
192712feb904SMatthew Dillon 		}
192812feb904SMatthew Dillon 
192912feb904SMatthew Dillon 		if (ccb->ccb_xa.flags &
193012feb904SMatthew Dillon 		    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) {
193112feb904SMatthew Dillon 			return;
193212feb904SMatthew Dillon 		}
193312feb904SMatthew Dillon 	}
193412feb904SMatthew Dillon 
193512feb904SMatthew Dillon 
19364c339a5fSMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
19374c339a5fSMatthew Dillon 		/*
19384c339a5fSMatthew Dillon 		 * The next command is a NCQ command and can be issued as
19394c339a5fSMatthew Dillon 		 * long as currently active commands are not standard.
19404c339a5fSMatthew Dillon 		 */
19414c339a5fSMatthew Dillon 		if (ap->ap_active) {
19424c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
19434c339a5fSMatthew Dillon 			return;
19444c339a5fSMatthew Dillon 		}
19454c339a5fSMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
19464c339a5fSMatthew Dillon 
19474c339a5fSMatthew Dillon 		mask = 0;
19484c339a5fSMatthew Dillon 		do {
19494c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
19504c339a5fSMatthew Dillon 			mask |= 1 << ccb->ccb_slot;
19514c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
195212feb904SMatthew Dillon 			ahci_start_timeout(ccb);
195312feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
19544c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
195512feb904SMatthew Dillon 		} while (ccb && (ccb->ccb_xa.flags & ATA_F_NCQ) &&
195612feb904SMatthew Dillon 			 (ap->ap_run_flags &
195712feb904SMatthew Dillon 			     (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE)) == 0);
19584c339a5fSMatthew Dillon 
19594c339a5fSMatthew Dillon 		ap->ap_sactive |= mask;
19604c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SACT, mask);
19614c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CI, mask);
19624c339a5fSMatthew Dillon 	} else {
19634c339a5fSMatthew Dillon 		/*
19644c339a5fSMatthew Dillon 		 * The next command is a standard command and can be issued
19654c339a5fSMatthew Dillon 		 * as long as currently active commands are not NCQ.
19664c339a5fSMatthew Dillon 		 *
19674c339a5fSMatthew Dillon 		 * We limit ourself to 1 command if we have a port multiplier,
19684c339a5fSMatthew Dillon 		 * (at least without FBSS support), otherwise timeouts on
19694c339a5fSMatthew Dillon 		 * one port can race completions on other ports (see
19704c339a5fSMatthew Dillon 		 * ahci_ata_cmd_timeout() for more information).
19714c339a5fSMatthew Dillon 		 *
19724c339a5fSMatthew Dillon 		 * If not on a port multiplier generally allow up to 4
19734c339a5fSMatthew Dillon 		 * standard commands to be enqueued.  Remember that the
19744c339a5fSMatthew Dillon 		 * command processor will still process them sequentially.
19751980eff3SMatthew Dillon 		 */
19761980eff3SMatthew Dillon 		if (ap->ap_sactive)
1977258223a3SMatthew Dillon 			return;
19784c339a5fSMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
19794c339a5fSMatthew Dillon 			limit = 1;
19804c339a5fSMatthew Dillon 		else if (ap->ap_sc->sc_ncmds > 4)
19814c339a5fSMatthew Dillon 			limit = 4;
19824c339a5fSMatthew Dillon 		else
19834c339a5fSMatthew Dillon 			limit = 2;
1984258223a3SMatthew Dillon 
19854c339a5fSMatthew Dillon 		while (ap->ap_active_cnt < limit && ccb &&
19864c339a5fSMatthew Dillon 		       (ccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
19874c339a5fSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
198812feb904SMatthew Dillon #if 0
198912feb904SMatthew Dillon 			fubar(ccb);
199012feb904SMatthew Dillon #endif
19914c339a5fSMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
1992258223a3SMatthew Dillon 			ap->ap_active_cnt++;
199312feb904SMatthew Dillon 			ap->ap_run_flags = ccb->ccb_xa.flags;
19944c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
19954c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
199612feb904SMatthew Dillon 			ahci_start_timeout(ccb);
19974c339a5fSMatthew Dillon 			ccb = TAILQ_FIRST(&ap->ap_ccb_pending);
199812feb904SMatthew Dillon 			if (ccb && (ccb->ccb_xa.flags &
199912feb904SMatthew Dillon 				    (ATA_F_EXCLUSIVE | ATA_F_AUTOSENSE))) {
200012feb904SMatthew Dillon 				break;
200112feb904SMatthew Dillon 			}
20021980eff3SMatthew Dillon 		}
2003258223a3SMatthew Dillon 	}
2004258223a3SMatthew Dillon }
2005258223a3SMatthew Dillon 
2006258223a3SMatthew Dillon void
2007258223a3SMatthew Dillon ahci_intr(void *arg)
2008258223a3SMatthew Dillon {
2009258223a3SMatthew Dillon 	struct ahci_softc	*sc = arg;
2010f4553de1SMatthew Dillon 	struct ahci_port	*ap;
201112feb904SMatthew Dillon 	u_int32_t		is;
201212feb904SMatthew Dillon 	u_int32_t		ack;
2013258223a3SMatthew Dillon 	int			port;
2014258223a3SMatthew Dillon 
2015f4553de1SMatthew Dillon 	/*
2016f4553de1SMatthew Dillon 	 * Check if the master enable is up, and whether any interrupts are
2017f4553de1SMatthew Dillon 	 * pending.
2018f4553de1SMatthew Dillon 	 */
2019f4553de1SMatthew Dillon 	if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
2020f4553de1SMatthew Dillon 		return;
2021258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
202212feb904SMatthew Dillon 	if (is == 0 || is == 0xffffffff) {
2023258223a3SMatthew Dillon 		return;
202412feb904SMatthew Dillon 	}
202512feb904SMatthew Dillon 	is &= sc->sc_portmask;
2026258223a3SMatthew Dillon 
2027258223a3SMatthew Dillon #ifdef AHCI_COALESCE
2028258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
2029258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
2030258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
2031258223a3SMatthew Dillon 		    DEVNAME(sc));
2032258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
2033258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
2034258223a3SMatthew Dillon 	}
2035258223a3SMatthew Dillon #endif
2036258223a3SMatthew Dillon 
2037f4553de1SMatthew Dillon 	/*
2038f4553de1SMatthew Dillon 	 * Process interrupts for each port in a non-blocking fashion.
203912feb904SMatthew Dillon 	 *
204012feb904SMatthew Dillon 	 * The global IS bit is forced on if any unmasked port interrupts
204112feb904SMatthew Dillon 	 * are pending, even if we clear.
2042f4553de1SMatthew Dillon 	 */
204312feb904SMatthew Dillon 	for (ack = 0; is; is &= ~(1 << port)) {
2044258223a3SMatthew Dillon 		port = ffs(is) - 1;
204512feb904SMatthew Dillon 		ack |= 1 << port;
204612feb904SMatthew Dillon 
2047f4553de1SMatthew Dillon 		ap = sc->sc_ports[port];
204812feb904SMatthew Dillon 		if (ap == NULL)
204912feb904SMatthew Dillon 			continue;
205012feb904SMatthew Dillon 
2051f4553de1SMatthew Dillon 		if (ahci_os_lock_port_nb(ap) == 0) {
2052f4553de1SMatthew Dillon 			ahci_port_intr(ap, 0);
2053f4553de1SMatthew Dillon 			ahci_os_unlock_port(ap);
2054f4553de1SMatthew Dillon 		} else {
2055f4553de1SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2056f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2057f4553de1SMatthew Dillon 		}
2058f4553de1SMatthew Dillon 	}
2059258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, ack);
2060258223a3SMatthew Dillon }
2061258223a3SMatthew Dillon 
2062f4553de1SMatthew Dillon /*
2063f4553de1SMatthew Dillon  * Core called from helper thread.
2064f4553de1SMatthew Dillon  */
20653209f581SMatthew Dillon void
2066f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask)
2067f4553de1SMatthew Dillon {
2068f4553de1SMatthew Dillon 	/*
2069f4553de1SMatthew Dillon 	 * Process any expired timedouts.
2070f4553de1SMatthew Dillon 	 */
2071f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
2072f4553de1SMatthew Dillon 	if (mask & AP_SIGF_TIMEOUT) {
2073831bc9e3SMatthew Dillon 		ahci_check_active_timeouts(ap);
2074f4553de1SMatthew Dillon 	}
2075f4553de1SMatthew Dillon 
2076f4553de1SMatthew Dillon 	/*
2077f4553de1SMatthew Dillon 	 * Process port interrupts which require a higher level of
2078f4553de1SMatthew Dillon 	 * intervention.
2079f4553de1SMatthew Dillon 	 */
2080f4553de1SMatthew Dillon 	if (mask & AP_SIGF_PORTINT) {
2081f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2082f4553de1SMatthew Dillon 		ahci_port_interrupt_enable(ap);
2083831bc9e3SMatthew Dillon 		ahci_os_unlock_port(ap);
208412feb904SMatthew Dillon 	} else if (ap->ap_probe != ATA_PROBE_FAILED) {
208512feb904SMatthew Dillon 		ahci_port_intr(ap, 1);
208612feb904SMatthew Dillon 		ahci_port_interrupt_enable(ap);
208712feb904SMatthew Dillon 		ahci_os_unlock_port(ap);
2088f4553de1SMatthew Dillon 	} else {
2089f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
2090f4553de1SMatthew Dillon 	}
2091f4553de1SMatthew Dillon }
2092f4553de1SMatthew Dillon 
2093f4553de1SMatthew Dillon /*
2094f4553de1SMatthew Dillon  * Core per-port interrupt handler.
2095f4553de1SMatthew Dillon  *
2096f4553de1SMatthew Dillon  * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2097f4553de1SMatthew Dillon  * deal with normal command completions which do not require blocking.
2098f4553de1SMatthew Dillon  */
2099f4553de1SMatthew Dillon void
2100f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable)
2101258223a3SMatthew Dillon {
2102258223a3SMatthew Dillon 	struct ahci_softc	*sc = ap->ap_sc;
21033209f581SMatthew Dillon 	u_int32_t		is, ci_saved, ci_masked;
210422181ab7SMatthew Dillon 	int			slot;
2105258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
21061980eff3SMatthew Dillon 	struct ata_port		*ccb_at = NULL;
2107258223a3SMatthew Dillon 	volatile u_int32_t	*active;
2108f4553de1SMatthew Dillon 	const u_int32_t		blockable_mask = AHCI_PREG_IS_TFES |
2109f4553de1SMatthew Dillon 						 AHCI_PREG_IS_IFS |
2110f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PCS |
2111f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PRCS |
2112f4553de1SMatthew Dillon 						 AHCI_PREG_IS_HBFS |
2113f4553de1SMatthew Dillon 						 AHCI_PREG_IS_OFS |
2114f4553de1SMatthew Dillon 						 AHCI_PREG_IS_UFS;
2115f4553de1SMatthew Dillon 
211622181ab7SMatthew Dillon 	enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
211722181ab7SMatthew Dillon 	       NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2118258223a3SMatthew Dillon 
2119f4553de1SMatthew Dillon 	/*
2120f4553de1SMatthew Dillon 	 * All basic command completions are always processed.
2121f4553de1SMatthew Dillon 	 */
212212feb904SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
2123cec07d75SMatthew Dillon 	if (is & AHCI_PREG_IS_DPS)
2124cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2125258223a3SMatthew Dillon 
2126f4553de1SMatthew Dillon 	/*
2127f4553de1SMatthew Dillon 	 * If we can't block then we can't handle these here.  Disable
2128f4553de1SMatthew Dillon 	 * the interrupts in question so we don't live-lock, the helper
2129f4553de1SMatthew Dillon 	 * thread will re-enable them.
2130f4553de1SMatthew Dillon 	 *
2131f4553de1SMatthew Dillon 	 * If the port is in a completely failed state we do not want
2132dbef6246SMatthew Dillon 	 * to drop through to failed-command-processing if blockable is 0,
2133f4553de1SMatthew Dillon 	 * just let the thread deal with it all.
2134dbef6246SMatthew Dillon 	 *
2135dbef6246SMatthew Dillon 	 * Otherwise we fall through and still handle DHRS and any commands
2136dbef6246SMatthew Dillon 	 * which completed normally.  Even if we are errored we haven't
2137dbef6246SMatthew Dillon 	 * stopped the port yet so CI/SACT are still good.
2138f4553de1SMatthew Dillon 	 */
2139f4553de1SMatthew Dillon 	if (blockable == 0) {
2140f4553de1SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
214112feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2142f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2143f4553de1SMatthew Dillon 			return;
2144f4553de1SMatthew Dillon 		}
2145f4553de1SMatthew Dillon 		if (is & blockable_mask) {
214612feb904SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE, 0);
2147f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
214812feb904SMatthew Dillon 			return;
2149f4553de1SMatthew Dillon 		}
2150f4553de1SMatthew Dillon 	}
2151f4553de1SMatthew Dillon 
21523209f581SMatthew Dillon 	/*
2153f4553de1SMatthew Dillon 	 * Either NCQ or non-NCQ commands will be active, never both.
21543209f581SMatthew Dillon 	 */
2155258223a3SMatthew Dillon 	if (ap->ap_sactive) {
2156258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
2157258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
2158258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2159258223a3SMatthew Dillon 		active = &ap->ap_sactive;
2160258223a3SMatthew Dillon 	} else {
2161258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2162258223a3SMatthew Dillon 		active = &ap->ap_active;
2163258223a3SMatthew Dillon 	}
216412feb904SMatthew Dillon 	KKASSERT(!(ap->ap_sactive && ap->ap_active));
216512feb904SMatthew Dillon #if 0
216612feb904SMatthew Dillon 	kprintf("CHECK act=%08x/%08x sact=%08x/%08x\n",
216712feb904SMatthew Dillon 		ap->ap_active, ahci_pread(ap, AHCI_PREG_CI),
216812feb904SMatthew Dillon 		ap->ap_sactive, ahci_pread(ap, AHCI_PREG_SACT));
216912feb904SMatthew Dillon #endif
2170258223a3SMatthew Dillon 
21711980eff3SMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
2172cf5f3a81SMatthew Dillon 		/*
2173f4553de1SMatthew Dillon 		 * Command failed (blockable).
2174f4553de1SMatthew Dillon 		 *
2175f4553de1SMatthew Dillon 		 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
21761980eff3SMatthew Dillon 		 *
21771980eff3SMatthew Dillon 		 * This stops command processing.
2178cf5f3a81SMatthew Dillon 		 */
2179258223a3SMatthew Dillon 		u_int32_t tfd, serr;
2180258223a3SMatthew Dillon 		int	err_slot;
2181258223a3SMatthew Dillon 
218212feb904SMatthew Dillon process_error:
2183258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
2184258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2185258223a3SMatthew Dillon 
2186cf5f3a81SMatthew Dillon 		/*
218712feb904SMatthew Dillon 		 * Load the error slot and restart command processing.
218812feb904SMatthew Dillon 		 * CLO if we need to.  The error slot may not be valid.
218912feb904SMatthew Dillon 		 * MUST BE DONE BEFORE CLEARING ST!
219012feb904SMatthew Dillon 		 *
219112feb904SMatthew Dillon 		 * Cycle ST.
219212feb904SMatthew Dillon 		 *
219312feb904SMatthew Dillon 		 * It is unclear but we may have to clear SERR to reenable
219412feb904SMatthew Dillon 		 * error processing.
2195cf5f3a81SMatthew Dillon 		 */
219612feb904SMatthew Dillon 		err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap, AHCI_PREG_CMD));
219712feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
219812feb904SMatthew Dillon 					      AHCI_PREG_IS_PSS |
219912feb904SMatthew Dillon 					      AHCI_PREG_IS_DHRS |
220012feb904SMatthew Dillon 					      AHCI_PREG_IS_SDBS);
220112feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_PSS |
220212feb904SMatthew Dillon 			AHCI_PREG_IS_DHRS | AHCI_PREG_IS_SDBS);
220312feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2204258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
220512feb904SMatthew Dillon 		ahci_os_hardsleep(10);
220612feb904SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
220712feb904SMatthew Dillon 			kprintf("%s: Issuing CLO\n", PORTNAME(ap));
220812feb904SMatthew Dillon 			ahci_port_clo(ap);
220912feb904SMatthew Dillon 		}
221012feb904SMatthew Dillon 		ahci_port_start(ap);
221122181ab7SMatthew Dillon 		need = NEED_RESTART;
2212258223a3SMatthew Dillon 
221350a3ecb6SMatthew Dillon 		/*
221450a3ecb6SMatthew Dillon 		 * ATAPI errors are fairly common from probing, just
221550a3ecb6SMatthew Dillon 		 * report disk errors or if bootverbose is on.
221650a3ecb6SMatthew Dillon 		 */
221750a3ecb6SMatthew Dillon 		if (bootverbose || ap->ap_type != ATA_PORT_T_ATAPI) {
221812feb904SMatthew Dillon 			kprintf("%s: TFES slot %d ci_saved = %08x\n",
221912feb904SMatthew Dillon 				PORTNAME(ap), err_slot, ci_saved);
222050a3ecb6SMatthew Dillon 		}
2221258223a3SMatthew Dillon 
22221980eff3SMatthew Dillon 		/*
222312feb904SMatthew Dillon 		 * If we got an error on an error CCB just complete it
222412feb904SMatthew Dillon 		 * with an error.  ci_saved has the mask to restart
222512feb904SMatthew Dillon 		 * (the err_ccb will be removed from it by finish_error).
22261980eff3SMatthew Dillon 		 */
222712feb904SMatthew Dillon 		if (ap->ap_flags & AP_F_ERR_CCB_RESERVED) {
222812feb904SMatthew Dillon 			err_slot = ap->ap_err_ccb->ccb_slot;
222912feb904SMatthew Dillon 			goto finish_error;
2230258223a3SMatthew Dillon 		}
2231258223a3SMatthew Dillon 
22321980eff3SMatthew Dillon 		/*
223312feb904SMatthew Dillon 		 * If NCQ commands were active get the error slot from
223412feb904SMatthew Dillon 		 * the log page.  NCQ is not supported for PM's so this
223512feb904SMatthew Dillon 		 * is a direct-attached target.
22361980eff3SMatthew Dillon 		 *
223712feb904SMatthew Dillon 		 * Otherwise if no commands were active we have a problem.
223812feb904SMatthew Dillon 		 *
223912feb904SMatthew Dillon 		 * Otherwise if the error slot is bad we have a problem.
224012feb904SMatthew Dillon 		 *
224112feb904SMatthew Dillon 		 * Otherwise process the error for the slot.
22421980eff3SMatthew Dillon 		 */
224312feb904SMatthew Dillon 		if (ap->ap_sactive) {
224412feb904SMatthew Dillon 			err_slot = ahci_port_read_ncq_error(ap, 0);
224512feb904SMatthew Dillon 		} else if (ap->ap_active == 0) {
224612feb904SMatthew Dillon 			kprintf("%s: TFES with no commands pending\n",
224712feb904SMatthew Dillon 				PORTNAME(ap));
224812feb904SMatthew Dillon 			err_slot = -1;
224912feb904SMatthew Dillon 		} else if (err_slot < 0 || err_slot >= ap->ap_sc->sc_ncmds) {
225012feb904SMatthew Dillon 			kprintf("%s: bad error slot %d\n",
2251258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
225212feb904SMatthew Dillon 			err_slot = -1;
2253258223a3SMatthew Dillon 		} else {
225412feb904SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
225512feb904SMatthew Dillon 
22564c339a5fSMatthew Dillon 			/*
225712feb904SMatthew Dillon 			 * Validate the errored ccb.  Note that ccb_at can
225812feb904SMatthew Dillon 			 * be NULL for direct-attached ccb's.
225912feb904SMatthew Dillon 			 *
226012feb904SMatthew Dillon 			 * Copy received taskfile data from the RFIS.
22614c339a5fSMatthew Dillon 			 */
226212feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
226312feb904SMatthew Dillon 				ccb_at = ccb->ccb_xa.at;
226412feb904SMatthew Dillon 				memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
226512feb904SMatthew Dillon 				       sizeof(struct ata_fis_d2h));
226650a3ecb6SMatthew Dillon 				if (bootverbose) {
226750a3ecb6SMatthew Dillon 					kprintf("%s: Copying rfis slot %d\n",
226812feb904SMatthew Dillon 						ATANAME(ap, ccb_at), err_slot);
226950a3ecb6SMatthew Dillon 				}
227012feb904SMatthew Dillon 			} else {
227112feb904SMatthew Dillon 				kprintf("%s: Cannot copy rfis, CCB slot "
227212feb904SMatthew Dillon 					"%d is not on-chip (state=%d)\n",
227312feb904SMatthew Dillon 					ATANAME(ap, ccb->ccb_xa.at),
227412feb904SMatthew Dillon 					err_slot, ccb->ccb_xa.state);
227512feb904SMatthew Dillon 				err_slot = -1;
227612feb904SMatthew Dillon 			}
2277258223a3SMatthew Dillon 		}
2278258223a3SMatthew Dillon 
2279258223a3SMatthew Dillon 		/*
228012feb904SMatthew Dillon 		 * If we could not determine the errored slot then
228112feb904SMatthew Dillon 		 * reset the port.
2282258223a3SMatthew Dillon 		 */
228312feb904SMatthew Dillon 		if (err_slot < 0) {
228412feb904SMatthew Dillon 			kprintf("%s: TFES: Unable to determine errored slot\n",
228512feb904SMatthew Dillon 				PORTNAME(ap));
22861980eff3SMatthew Dillon 			if (ap->ap_flags & AP_F_IN_RESET)
22871980eff3SMatthew Dillon 				goto fatal;
2288258223a3SMatthew Dillon 			goto failall;
2289258223a3SMatthew Dillon 		}
2290258223a3SMatthew Dillon 
229112feb904SMatthew Dillon 		/*
229212feb904SMatthew Dillon 		 * Finish error on slot.  We will restart ci_saved
229312feb904SMatthew Dillon 		 * commands except the errored slot which we generate
229412feb904SMatthew Dillon 		 * a failure for.
229512feb904SMatthew Dillon 		 */
229612feb904SMatthew Dillon finish_error:
229712feb904SMatthew Dillon 		ccb = &ap->ap_ccbs[err_slot];
2298258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
2299258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2300258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
23011980eff3SMatthew Dillon 	} else if (is & AHCI_PREG_IS_DHRS) {
23021980eff3SMatthew Dillon 		/*
2303f4553de1SMatthew Dillon 		 * Command posted D2H register FIS to the rfis (non-blocking).
2304f4553de1SMatthew Dillon 		 *
230512feb904SMatthew Dillon 		 * A normal completion with an error may set DHRS instead
230612feb904SMatthew Dillon 		 * of TFES.  The CCS bits are only valid if ERR was set.
230712feb904SMatthew Dillon 		 * If ERR is set command processing was probably stopped.
23088bf6a3ffSMatthew Dillon 		 *
230912feb904SMatthew Dillon 		 * If ERR was not set we can only copy-back data for
231012feb904SMatthew Dillon 		 * exclusive-mode commands because otherwise we won't know
231112feb904SMatthew Dillon 		 * which tag the rfis belonged to.
231212feb904SMatthew Dillon 		 *
231312feb904SMatthew Dillon 		 * err_slot must be read from the CCS before any other port
231412feb904SMatthew Dillon 		 * action, such as stopping the port.
231512feb904SMatthew Dillon 		 *
231612feb904SMatthew Dillon 		 * WARNING!	This is not well documented in the AHCI spec.
231712feb904SMatthew Dillon 		 *		It can be found in the state machine tables
231812feb904SMatthew Dillon 		 *		but not in the explanations.
23191980eff3SMatthew Dillon 		 */
232012feb904SMatthew Dillon 		u_int32_t tfd;
232112feb904SMatthew Dillon 		u_int32_t cmd;
23221980eff3SMatthew Dillon 		int err_slot;
23231980eff3SMatthew Dillon 
232412feb904SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
232512feb904SMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD);
232612feb904SMatthew Dillon 
232712feb904SMatthew Dillon 		if ((tfd & AHCI_PREG_TFD_STS_ERR) &&
232812feb904SMatthew Dillon 		    (cmd & AHCI_PREG_CMD_CR) == 0) {
23291980eff3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(
23301980eff3SMatthew Dillon 						ahci_pread(ap, AHCI_PREG_CMD));
23311980eff3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
233212feb904SMatthew Dillon 			kprintf("%s: DHRS tfd=%b err_slot=%d cmd=%02x\n",
233312feb904SMatthew Dillon 				PORTNAME(ap),
233412feb904SMatthew Dillon 				tfd, AHCI_PFMT_TFD_STS,
233512feb904SMatthew Dillon 				err_slot, ccb->ccb_xa.fis->command);
233612feb904SMatthew Dillon 			goto process_error;
2337258223a3SMatthew Dillon 		}
233812feb904SMatthew Dillon 		/*
233912feb904SMatthew Dillon 		 * NO ELSE... copy back is in the normal command completion
234012feb904SMatthew Dillon 		 * code and only if no error occured and ATA_F_AUTOSENSE
234112feb904SMatthew Dillon 		 * was set.
234212feb904SMatthew Dillon 		 */
2343cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
23441980eff3SMatthew Dillon 	}
23451980eff3SMatthew Dillon 
23461980eff3SMatthew Dillon 	/*
2347f4553de1SMatthew Dillon 	 * Device notification to us (non-blocking)
23481980eff3SMatthew Dillon 	 *
234912feb904SMatthew Dillon 	 * NOTE!  On some parts notification bits can cause an IPMS
235012feb904SMatthew Dillon 	 *	  interrupt instead of a SDBS interrupt.
2351cec07d75SMatthew Dillon 	 *
235212feb904SMatthew Dillon 	 * NOTE!  On some parts (e.g. VBOX, probably intel ICHx),
235312feb904SMatthew Dillon 	 *	  SDBS notifies us of the completion of a NCQ command
235412feb904SMatthew Dillon 	 *	  and DBS does not.
23551980eff3SMatthew Dillon 	 */
235612feb904SMatthew Dillon 	if (is & (AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS)) {
23571980eff3SMatthew Dillon 		u_int32_t data;
23581980eff3SMatthew Dillon 
235912feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
236012feb904SMatthew Dillon 				AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
236112feb904SMatthew Dillon 		if (sc->sc_cap & AHCI_REG_CAP_SSNTF) {
23621980eff3SMatthew Dillon 			data = ahci_pread(ap, AHCI_PREG_SNTF);
2363cec07d75SMatthew Dillon 			if (data) {
236412feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_IS,
236512feb904SMatthew Dillon 						AHCI_PREG_IS_SDBS);
236612feb904SMatthew Dillon 				kprintf("%s: NOTIFY %08x\n",
236712feb904SMatthew Dillon 					PORTNAME(ap), data);
236812feb904SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SERR,
236912feb904SMatthew Dillon 						AHCI_PREG_SERR_DIAG_N);
23703209f581SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SNTF, data);
23713209f581SMatthew Dillon 				ahci_cam_changed(ap, NULL, -1);
23721980eff3SMatthew Dillon 			}
23731980eff3SMatthew Dillon 		}
237412feb904SMatthew Dillon 		is &= ~(AHCI_PREG_IS_SDBS | AHCI_PREG_IS_IPMS);
237512feb904SMatthew Dillon 	}
23763209f581SMatthew Dillon 
23773209f581SMatthew Dillon 	/*
2378f4553de1SMatthew Dillon 	 * Spurious IFS errors (blockable).
2379f4553de1SMatthew Dillon 	 *
23803209f581SMatthew Dillon 	 * Spurious IFS errors can occur while we are doing a reset
23813209f581SMatthew Dillon 	 * sequence through a PM.  Try to recover if we are being asked
23823209f581SMatthew Dillon 	 * to ignore IFS errors during these periods.
23833209f581SMatthew Dillon 	 */
23843209f581SMatthew Dillon 	if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
23851980eff3SMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
23863209f581SMatthew Dillon 		if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
23871980eff3SMatthew Dillon 			kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
23881980eff3SMatthew Dillon 				PORTNAME(ap),
23891980eff3SMatthew Dillon 				is, AHCI_PFMT_IS,
23901980eff3SMatthew Dillon 				serr, AHCI_PFMT_SERR);
23913209f581SMatthew Dillon 			ap->ap_flags |= AP_F_IFS_IGNORED;
23923209f581SMatthew Dillon 		}
23933209f581SMatthew Dillon 		ap->ap_flags |= AP_F_IFS_OCCURED;
23941980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
23951980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
23961980eff3SMatthew Dillon 		is &= ~AHCI_PREG_IS_IFS;
23971980eff3SMatthew Dillon 		ahci_port_stop(ap, 0);
23981980eff3SMatthew Dillon 		ahci_port_start(ap);
239912feb904SMatthew Dillon 		kprintf("%s: Spurious IFS error\n", PORTNAME(ap));
240012feb904SMatthew Dillon 		goto failall;
240112feb904SMatthew Dillon 		/* need = NEED_RESTART; */
24021980eff3SMatthew Dillon 	}
2403258223a3SMatthew Dillon 
2404258223a3SMatthew Dillon 	/*
2405f4553de1SMatthew Dillon 	 * Port change (hot-plug) (blockable).
2406258223a3SMatthew Dillon 	 *
2407258223a3SMatthew Dillon 	 * A PCS interrupt will occur on hot-plug once communication is
2408258223a3SMatthew Dillon 	 * established.
2409258223a3SMatthew Dillon 	 *
2410258223a3SMatthew Dillon 	 * A PRCS interrupt will occur on hot-unplug (and possibly also
2411258223a3SMatthew Dillon 	 * on hot-plug).
2412258223a3SMatthew Dillon 	 *
241322181ab7SMatthew Dillon 	 * XXX We can then check the CPS (Cold Presence State) bit, if
241422181ab7SMatthew Dillon 	 * supported, to determine if a device is plugged in or not and do
241522181ab7SMatthew Dillon 	 * the right thing.
241622181ab7SMatthew Dillon 	 *
241722181ab7SMatthew Dillon 	 * WARNING:  A PCS interrupt is cleared by clearing DIAG_X, and
241822181ab7SMatthew Dillon 	 *	     can also occur if an unsolicited COMINIT is received.
241922181ab7SMatthew Dillon 	 *	     If this occurs command processing is automatically
242022181ab7SMatthew Dillon 	 *	     stopped (CR goes inactive) and the port must be stopped
242122181ab7SMatthew Dillon 	 *	     and restarted.
2422258223a3SMatthew Dillon 	 */
2423258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
242412feb904SMatthew Dillon 		kprintf("%s: Transient Errors: %b\n",
242512feb904SMatthew Dillon 			PORTNAME(ap), is, AHCI_PFMT_IS);
242612feb904SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
242712feb904SMatthew Dillon 			(AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
2428cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2429cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2430cec07d75SMatthew Dillon 		is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
243122181ab7SMatthew Dillon 		ahci_port_stop(ap, 0);
2432258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2433258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
243412feb904SMatthew Dillon 			if (ap->ap_probe == ATA_PROBE_FAILED) {
243522181ab7SMatthew Dillon 				need = NEED_HOTPLUG_INSERT;
243622181ab7SMatthew Dillon 				goto fatal;
2437258223a3SMatthew Dillon 			}
243822181ab7SMatthew Dillon 			need = NEED_RESTART;
2439258223a3SMatthew Dillon 			break;
2440258223a3SMatthew Dillon 		default:
244112feb904SMatthew Dillon 			if (ap->ap_type != ATA_PROBE_FAILED) {
244222181ab7SMatthew Dillon 				need = NEED_HOTPLUG_REMOVE;
244322181ab7SMatthew Dillon 				goto fatal;
2444258223a3SMatthew Dillon 			}
244522181ab7SMatthew Dillon 			need = NEED_RESTART;
2446258223a3SMatthew Dillon 			break;
2447258223a3SMatthew Dillon 		}
2448258223a3SMatthew Dillon 	}
2449258223a3SMatthew Dillon 
245022181ab7SMatthew Dillon 	/*
2451f4553de1SMatthew Dillon 	 * Check for remaining errors - they are fatal. (blockable)
245222181ab7SMatthew Dillon 	 */
2453258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2454258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2455cec07d75SMatthew Dillon 		u_int32_t serr;
2456cec07d75SMatthew Dillon 
2457cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2458cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2459cec07d75SMatthew Dillon 				  AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2460cec07d75SMatthew Dillon 				  AHCI_PREG_IS_UFS));
2461cec07d75SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2462831bc9e3SMatthew Dillon 		kprintf("%s: Unrecoverable errors (IS: %b, SERR: %b), "
24634444122dSMatthew Dillon 			"disabling port.\n",
24644444122dSMatthew Dillon 			PORTNAME(ap),
24654444122dSMatthew Dillon 			is, AHCI_PFMT_IS,
24661980eff3SMatthew Dillon 			serr, AHCI_PFMT_SERR
24674444122dSMatthew Dillon 		);
2468831bc9e3SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2469831bc9e3SMatthew Dillon 			AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2470831bc9e3SMatthew Dillon 		        AHCI_PREG_IS_UFS);
2471258223a3SMatthew Dillon 		/* XXX try recovery first */
2472258223a3SMatthew Dillon 		goto fatal;
2473258223a3SMatthew Dillon 	}
2474258223a3SMatthew Dillon 
247522181ab7SMatthew Dillon 	/*
247622181ab7SMatthew Dillon 	 * Fail all outstanding commands if we know the port won't recover.
24771980eff3SMatthew Dillon 	 *
24781980eff3SMatthew Dillon 	 * We may have a ccb_at if the failed command is known and was
24791980eff3SMatthew Dillon 	 * being sent to a device over a port multiplier (PM).  In this
24801980eff3SMatthew Dillon 	 * case if the port itself has not completely failed we fail just
24811980eff3SMatthew Dillon 	 * the commands related to that target.
248212feb904SMatthew Dillon 	 *
248312feb904SMatthew Dillon 	 * ci_saved contains the mask of active commands as of when the
248412feb904SMatthew Dillon 	 * error occured, prior to any port stops.
248522181ab7SMatthew Dillon 	 */
2486258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
2487258223a3SMatthew Dillon fatal:
2488258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
2489cf5f3a81SMatthew Dillon 		ahci_port_stop(ap, 0);
249012feb904SMatthew Dillon failall:
249112feb904SMatthew Dillon 		kprintf("%s: Failing all commands\n", PORTNAME(ap));
2492258223a3SMatthew Dillon 
24931980eff3SMatthew Dillon 		/*
249412feb904SMatthew Dillon 		 * Error all the active slots not already errored.  If
249512feb904SMatthew Dillon 		 * running across a PM try to error out just the slots
249612feb904SMatthew Dillon 		 * related to the target.
24971980eff3SMatthew Dillon 		 */
249812feb904SMatthew Dillon 		ci_masked = ci_saved & *active & ~ap->ap_expired;
2499258223a3SMatthew Dillon 		while (ci_masked) {
2500258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
2501258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
25021980eff3SMatthew Dillon 			if (ccb_at == ccb->ccb_xa.at ||
25031980eff3SMatthew Dillon 			    ap->ap_state == AP_S_FATAL_ERROR) {
250412feb904SMatthew Dillon 				ccb->ccb_xa.state = ATA_S_TIMEOUT;
250512feb904SMatthew Dillon 				ap->ap_expired |= 1 << slot;
250612feb904SMatthew Dillon 				ci_saved &= ~(1 << slot);
2507258223a3SMatthew Dillon 			}
250812feb904SMatthew Dillon 			ci_masked &= ~(1 << slot);
25091980eff3SMatthew Dillon 		}
2510258223a3SMatthew Dillon 
251112feb904SMatthew Dillon 		/*
251212feb904SMatthew Dillon 		 * Clear bits in ci_saved (cause completions to be run)
251312feb904SMatthew Dillon 		 * for all slots which are not active.
251412feb904SMatthew Dillon 		 */
2515258223a3SMatthew Dillon 		ci_saved &= ~*active;
2516258223a3SMatthew Dillon 
2517258223a3SMatthew Dillon 		/*
2518258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
2519258223a3SMatthew Dillon 		 *
2520258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
2521258223a3SMatthew Dillon 		 * a livelock.
2522258223a3SMatthew Dillon 		 */
2523258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
252422181ab7SMatthew Dillon 			if (need == NEED_RESTART)
252522181ab7SMatthew Dillon 				need = NEED_NOTHING;
2526258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
2527258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2528258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2529258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
2530258223a3SMatthew Dillon 		}
2531258223a3SMatthew Dillon 	}
2532258223a3SMatthew Dillon 
2533258223a3SMatthew Dillon 	/*
2534f4553de1SMatthew Dillon 	 * CCB completion (non blocking).
2535f4553de1SMatthew Dillon 	 *
2536258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
2537258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
2538258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
2539cf5f3a81SMatthew Dillon 	 *
2540cf5f3a81SMatthew Dillon 	 * Any active bits not saved are completed within the restrictions
2541cf5f3a81SMatthew Dillon 	 * imposed by the caller.
2542258223a3SMatthew Dillon 	 */
25433209f581SMatthew Dillon 	ci_masked = ~ci_saved & *active;
2544258223a3SMatthew Dillon 	while (ci_masked) {
2545258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
2546258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
2547258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
2548258223a3SMatthew Dillon 
2549258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2550258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2551258223a3SMatthew Dillon 		    " (error)" : "");
2552258223a3SMatthew Dillon 
2553258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
2554258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2555258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2556258223a3SMatthew Dillon 
2557258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
2558258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2559258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2560258223a3SMatthew Dillon 
2561258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
2562258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2563258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
2564258223a3SMatthew Dillon 
2565258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
25661980eff3SMatthew Dillon 		if (active == &ap->ap_active) {
25671980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
25681980eff3SMatthew Dillon 			--ap->ap_active_cnt;
25691980eff3SMatthew Dillon 		}
25704c339a5fSMatthew Dillon 
25714c339a5fSMatthew Dillon 		/*
25724c339a5fSMatthew Dillon 		 * Complete the ccb.  If the ccb was marked expired it
25734c339a5fSMatthew Dillon 		 * was probably already removed from the command processor,
25744c339a5fSMatthew Dillon 		 * so don't take the clear ci_saved bit as meaning the
25754c339a5fSMatthew Dillon 		 * command actually succeeded, it didn't.
25764c339a5fSMatthew Dillon 		 */
25774c339a5fSMatthew Dillon 		if (ap->ap_expired & (1 << ccb->ccb_slot)) {
257876497a9cSMatthew Dillon 			ap->ap_expired &= ~(1 << ccb->ccb_slot);
25794c339a5fSMatthew Dillon 			ccb->ccb_xa.state = ATA_S_TIMEOUT;
2580258223a3SMatthew Dillon 			ccb->ccb_done(ccb);
25814c339a5fSMatthew Dillon 			ccb->ccb_xa.complete(&ccb->ccb_xa);
25824c339a5fSMatthew Dillon 		} else {
258312feb904SMatthew Dillon 			if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
25844c339a5fSMatthew Dillon 				ccb->ccb_xa.state = ATA_S_COMPLETE;
258512feb904SMatthew Dillon 				if (ccb->ccb_xa.flags & ATA_F_AUTOSENSE) {
258612feb904SMatthew Dillon 					memcpy(&ccb->ccb_xa.rfis,
258712feb904SMatthew Dillon 					    ap->ap_rfis->rfis,
258812feb904SMatthew Dillon 					    sizeof(struct ata_fis_d2h));
258912feb904SMatthew Dillon 					if (ccb->ccb_xa.state == ATA_S_TIMEOUT)
259012feb904SMatthew Dillon 						ccb->ccb_xa.state = ATA_S_ERROR;
259112feb904SMatthew Dillon 				}
259212feb904SMatthew Dillon 			}
25934c339a5fSMatthew Dillon 			ccb->ccb_done(ccb);
25944c339a5fSMatthew Dillon 		}
2595258223a3SMatthew Dillon 	}
259612feb904SMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
2597258223a3SMatthew Dillon 
2598f4553de1SMatthew Dillon 	/*
2599f4553de1SMatthew Dillon 	 * Cleanup.  Will not be set if non-blocking.
2600f4553de1SMatthew Dillon 	 */
260122181ab7SMatthew Dillon 	switch(need) {
260222181ab7SMatthew Dillon 	case NEED_RESTART:
260322181ab7SMatthew Dillon 		/*
260422181ab7SMatthew Dillon 		 * A recoverable error occured and we can restart outstanding
260522181ab7SMatthew Dillon 		 * commands on the port.
260622181ab7SMatthew Dillon 		 */
260712feb904SMatthew Dillon 		ci_saved &= ~ap->ap_expired;
2608258223a3SMatthew Dillon 		if (ci_saved) {
260912feb904SMatthew Dillon 			kprintf("%s: Restart %08x\n", PORTNAME(ap), ci_saved);
26104c339a5fSMatthew Dillon 			ahci_issue_saved_commands(ap, ci_saved);
2611258223a3SMatthew Dillon 		}
261222181ab7SMatthew Dillon 		break;
261322181ab7SMatthew Dillon 	case NEED_HOTPLUG_INSERT:
261422181ab7SMatthew Dillon 		/*
2615cf5f3a81SMatthew Dillon 		 * A hot-plug insertion event has occured and all
2616cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
26171980eff3SMatthew Dillon 		 *
26181980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
26191980eff3SMatthew Dillon 		 * resetting the port.
262022181ab7SMatthew Dillon 		 */
26211980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
262222181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device inserted\n",
262322181ab7SMatthew Dillon 				PORTNAME(ap));
26243209f581SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_INIT;
26253209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
26261980eff3SMatthew Dillon 		}
262722181ab7SMatthew Dillon 		break;
262822181ab7SMatthew Dillon 	case NEED_HOTPLUG_REMOVE:
2629cf5f3a81SMatthew Dillon 		/*
2630cf5f3a81SMatthew Dillon 		 * A hot-plug removal event has occured and all
2631cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
26321980eff3SMatthew Dillon 		 *
26331980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
26341980eff3SMatthew Dillon 		 * resetting the port.
2635cf5f3a81SMatthew Dillon 		 */
26361980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
263722181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device removed\n",
263822181ab7SMatthew Dillon 				PORTNAME(ap));
2639cf5f3a81SMatthew Dillon 			ahci_port_hardstop(ap);
26403209f581SMatthew Dillon 			/* ap_probe set to failed */
26413209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
26421980eff3SMatthew Dillon 		}
264322181ab7SMatthew Dillon 		break;
264422181ab7SMatthew Dillon 	default:
264522181ab7SMatthew Dillon 		break;
2646258223a3SMatthew Dillon 	}
2647258223a3SMatthew Dillon }
2648258223a3SMatthew Dillon 
2649258223a3SMatthew Dillon struct ahci_ccb *
2650258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
2651258223a3SMatthew Dillon {
2652258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
2653258223a3SMatthew Dillon 
2654258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2655258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2656258223a3SMatthew Dillon 	if (ccb != NULL) {
2657258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2658258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2659258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
26601980eff3SMatthew Dillon 		ccb->ccb_xa.at = NULL;
2661258223a3SMatthew Dillon 	}
2662258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2663258223a3SMatthew Dillon 
2664258223a3SMatthew Dillon 	return (ccb);
2665258223a3SMatthew Dillon }
2666258223a3SMatthew Dillon 
2667258223a3SMatthew Dillon void
2668258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
2669258223a3SMatthew Dillon {
2670258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
2671258223a3SMatthew Dillon 
2672258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
2673258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2674258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2675258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2676258223a3SMatthew Dillon }
2677258223a3SMatthew Dillon 
2678258223a3SMatthew Dillon struct ahci_ccb *
2679258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
2680258223a3SMatthew Dillon {
2681258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
2682258223a3SMatthew Dillon 	u_int32_t sact;
2683b012a2caSMatthew Dillon 	u_int32_t ci;
2684258223a3SMatthew Dillon 
2685258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
2686b012a2caSMatthew Dillon 
2687b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
2688258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
2689192ee1d0SMatthew Dillon 		if (sact != 0) {
2690192ee1d0SMatthew Dillon 			kprintf("%s: ahci_get_err_ccb but SACT %08x != 0?\n",
2691192ee1d0SMatthew Dillon 				PORTNAME(ap), sact);
2692192ee1d0SMatthew Dillon 		}
2693b012a2caSMatthew Dillon 	}
2694b012a2caSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
2695b012a2caSMatthew Dillon 	if (ci) {
2696b012a2caSMatthew Dillon 		kprintf("%s: ahci_get_err_ccb: ci not 0 (%08x)\n",
2697b012a2caSMatthew Dillon 			ap->ap_name, ci);
2698b012a2caSMatthew Dillon 	}
2699b012a2caSMatthew Dillon 	KKASSERT(ci == 0);
2700baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) == 0);
2701baef7501SMatthew Dillon 	ap->ap_flags |= AP_F_ERR_CCB_RESERVED;
2702258223a3SMatthew Dillon 
2703258223a3SMatthew Dillon 	/* Save outstanding command state. */
2704258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
2705258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2706258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
2707258223a3SMatthew Dillon 
2708258223a3SMatthew Dillon 	/*
2709258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
2710258223a3SMatthew Dillon 	 * run prematurely.
2711258223a3SMatthew Dillon 	 */
2712258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2713258223a3SMatthew Dillon 
2714258223a3SMatthew Dillon 	/*
2715258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
2716258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
2717258223a3SMatthew Dillon 	 */
27181067474aSMatthew Dillon 	err_ccb = ap->ap_err_ccb;
2719258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
2720258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
2721258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
2722258223a3SMatthew Dillon 
2723258223a3SMatthew Dillon 	return err_ccb;
2724258223a3SMatthew Dillon }
2725258223a3SMatthew Dillon 
2726258223a3SMatthew Dillon void
2727258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
2728258223a3SMatthew Dillon {
2729258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
2730258223a3SMatthew Dillon 	u_int32_t sact;
27315f8c1efdSMatthew Dillon 	u_int32_t ci;
2732258223a3SMatthew Dillon 
2733baef7501SMatthew Dillon 	KKASSERT((ap->ap_flags & AP_F_ERR_CCB_RESERVED) != 0);
2734baef7501SMatthew Dillon 
27355f8c1efdSMatthew Dillon 	/*
27365f8c1efdSMatthew Dillon 	 * No commands may be active on the chip
27375f8c1efdSMatthew Dillon 	 */
2738b012a2caSMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) {
2739258223a3SMatthew Dillon 		sact = ahci_pread(ap, AHCI_PREG_SACT);
27405f8c1efdSMatthew Dillon 		if (sact) {
27415f8c1efdSMatthew Dillon 			panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
27425f8c1efdSMatthew Dillon 			      ccb->ccb_slot, sact);
2743258223a3SMatthew Dillon 		}
2744b012a2caSMatthew Dillon 	}
27455f8c1efdSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
27465f8c1efdSMatthew Dillon 	if (ci) {
2747cf5f3a81SMatthew Dillon 		panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2748cf5f3a81SMatthew Dillon 		      "(act=%08x sact=%08x)\n",
2749cf5f3a81SMatthew Dillon 		      ccb->ccb_slot, ci,
2750cf5f3a81SMatthew Dillon 		      ap->ap_active, ap->ap_sactive);
27515f8c1efdSMatthew Dillon 	}
2752258223a3SMatthew Dillon 
27531067474aSMatthew Dillon 	KKASSERT(ccb == ap->ap_err_ccb);
2754258223a3SMatthew Dillon 
2755258223a3SMatthew Dillon 	/* Restore outstanding command state */
2756258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
2757258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2758258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
2759258223a3SMatthew Dillon 
2760baef7501SMatthew Dillon 	ap->ap_flags &= ~AP_F_ERR_CCB_RESERVED;
2761258223a3SMatthew Dillon }
2762258223a3SMatthew Dillon 
27631980eff3SMatthew Dillon /*
27641980eff3SMatthew Dillon  * Read log page to get NCQ error.
27651980eff3SMatthew Dillon  *
27661980eff3SMatthew Dillon  * NOTE: NCQ not currently supported on port multipliers. XXX
27671980eff3SMatthew Dillon  */
2768258223a3SMatthew Dillon int
276912feb904SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int target)
2770258223a3SMatthew Dillon {
277112feb904SMatthew Dillon 	struct ata_log_page_10h	*log;
2772258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
2773258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
2774258223a3SMatthew Dillon 	struct ata_fis_h2d	*fis;
277512feb904SMatthew Dillon 	int			err_slot;
2776258223a3SMatthew Dillon 
277712feb904SMatthew Dillon 	if (bootverbose) {
277812feb904SMatthew Dillon 		kprintf("%s: READ LOG PAGE target %d\n", PORTNAME(ap),
277912feb904SMatthew Dillon 			target);
278012feb904SMatthew Dillon 	}
2781258223a3SMatthew Dillon 
278212feb904SMatthew Dillon 	/*
278312feb904SMatthew Dillon 	 * Prep error CCB for READ LOG EXT, page 10h, 1 sector.
278412feb904SMatthew Dillon 	 *
278512feb904SMatthew Dillon 	 * Getting err_ccb clears active/sactive/active_cnt, putting
278612feb904SMatthew Dillon 	 * it back restores the fields.
278712feb904SMatthew Dillon 	 */
2788258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
278912feb904SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_READ | ATA_F_POLL;
2790258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
2791258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
279212feb904SMatthew Dillon 	ccb->ccb_xa.complete = ahci_dummy_done;
2793b012a2caSMatthew Dillon 	ccb->ccb_xa.at = ap->ap_ata[target];
2794258223a3SMatthew Dillon 
2795258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
279612feb904SMatthew Dillon 	bzero(fis, sizeof(*fis));
2797258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
279812feb904SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD | target;
2799258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
2800258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
2801258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
2802258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
2803258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
2804258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
2805258223a3SMatthew Dillon 	fis->device = 0;
2806258223a3SMatthew Dillon 
280712feb904SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
2808258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
2809258223a3SMatthew Dillon 
2810258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
281112feb904SMatthew Dillon 		err_slot = -1;
2812258223a3SMatthew Dillon 		goto err;
2813258223a3SMatthew Dillon 	}
2814258223a3SMatthew Dillon 
2815258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
281612feb904SMatthew Dillon 	if (ahci_poll(ccb, 1000, ahci_quick_timeout) != ATA_S_COMPLETE) {
281712feb904SMatthew Dillon 		err_slot = -1;
2818258223a3SMatthew Dillon 		ahci_unload_prdt(ccb);
281912feb904SMatthew Dillon 		goto err;
282012feb904SMatthew Dillon 	}
282112feb904SMatthew Dillon 	ahci_unload_prdt(ccb);
2822258223a3SMatthew Dillon 
282312feb904SMatthew Dillon 	/*
282412feb904SMatthew Dillon 	 * Success, extract failed register set and tags from the scratch
282512feb904SMatthew Dillon 	 * space.
282612feb904SMatthew Dillon 	 */
2827258223a3SMatthew Dillon 	log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2828258223a3SMatthew Dillon 	if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2829258223a3SMatthew Dillon 		/* Not queued bit was set - wasn't an NCQ error? */
283012feb904SMatthew Dillon 		kprintf("%s: read NCQ error page, but not an NCQ error?\n",
2831258223a3SMatthew Dillon 			PORTNAME(ap));
283212feb904SMatthew Dillon 		err_slot = -1;
2833258223a3SMatthew Dillon 	} else {
2834258223a3SMatthew Dillon 		/* Copy back the log record as a D2H register FIS. */
283512feb904SMatthew Dillon 		err_slot = log->err_regs.type & ATA_LOG_10H_TYPE_TAG_MASK;
2836258223a3SMatthew Dillon 
2837258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[err_slot];
283812feb904SMatthew Dillon 		if (ccb->ccb_xa.state == ATA_S_ONCHIP) {
283912feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d\n",
284012feb904SMatthew Dillon 				ATANAME(ap, ccb->ccb_xa.at),
284112feb904SMatthew Dillon 				err_slot);
2842258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
2843258223a3SMatthew Dillon 				sizeof(struct ata_fis_d2h));
2844258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
2845258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.flags = 0;
284612feb904SMatthew Dillon 		} else {
284712feb904SMatthew Dillon 			kprintf("%s: read NCQ error page slot=%d, "
284812feb904SMatthew Dillon 				"slot does not match any cmds\n",
284912feb904SMatthew Dillon 				ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
285012feb904SMatthew Dillon 				err_slot);
285112feb904SMatthew Dillon 			err_slot = -1;
2852258223a3SMatthew Dillon 		}
2853258223a3SMatthew Dillon 	}
285412feb904SMatthew Dillon err:
285512feb904SMatthew Dillon 	ahci_put_err_ccb(ccb);
285612feb904SMatthew Dillon 	kprintf("%s: DONE log page target %d err_slot=%d\n",
285712feb904SMatthew Dillon 		PORTNAME(ap), target, err_slot);
285812feb904SMatthew Dillon 	return (err_slot);
2859258223a3SMatthew Dillon }
2860258223a3SMatthew Dillon 
2861258223a3SMatthew Dillon /*
2862258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
2863258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
2864258223a3SMatthew Dillon  * single physical base address.
2865258223a3SMatthew Dillon  */
2866258223a3SMatthew Dillon struct ahci_dmamem *
2867258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
2868258223a3SMatthew Dillon {
2869258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
2870258223a3SMatthew Dillon 	int	error;
2871258223a3SMatthew Dillon 
2872258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2873258223a3SMatthew Dillon 
2874258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2875258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
2876258223a3SMatthew Dillon 	if (error == 0) {
2877258223a3SMatthew Dillon 		adm->adm_tag = tag;
2878258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
2879258223a3SMatthew Dillon 					adm->adm_kva,
2880258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
2881258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
2882258223a3SMatthew Dillon 					0);
2883258223a3SMatthew Dillon 	}
2884258223a3SMatthew Dillon 	if (error) {
2885258223a3SMatthew Dillon 		if (adm->adm_map) {
2886258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
2887258223a3SMatthew Dillon 			adm->adm_map = NULL;
2888258223a3SMatthew Dillon 			adm->adm_tag = NULL;
2889258223a3SMatthew Dillon 			adm->adm_kva = NULL;
2890258223a3SMatthew Dillon 		}
2891258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
2892258223a3SMatthew Dillon 		adm = NULL;
2893258223a3SMatthew Dillon 	}
2894258223a3SMatthew Dillon 	return (adm);
2895258223a3SMatthew Dillon }
2896258223a3SMatthew Dillon 
2897258223a3SMatthew Dillon static
2898258223a3SMatthew Dillon void
2899258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2900258223a3SMatthew Dillon {
2901258223a3SMatthew Dillon 	KKASSERT(error == 0);
2902258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
2903258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
2904258223a3SMatthew Dillon }
2905258223a3SMatthew Dillon 
2906258223a3SMatthew Dillon 
2907258223a3SMatthew Dillon void
2908258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
2909258223a3SMatthew Dillon {
2910258223a3SMatthew Dillon 	if (adm->adm_map) {
2911258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2912258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2913258223a3SMatthew Dillon 		adm->adm_map = NULL;
2914258223a3SMatthew Dillon 		adm->adm_tag = NULL;
2915258223a3SMatthew Dillon 		adm->adm_kva = NULL;
2916258223a3SMatthew Dillon 	}
2917258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
2918258223a3SMatthew Dillon }
2919258223a3SMatthew Dillon 
2920258223a3SMatthew Dillon u_int32_t
2921258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
2922258223a3SMatthew Dillon {
2923258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2924258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
2925258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2926258223a3SMatthew Dillon }
2927258223a3SMatthew Dillon 
2928258223a3SMatthew Dillon void
2929258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
2930258223a3SMatthew Dillon {
2931258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2932258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2933258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
2934258223a3SMatthew Dillon }
2935258223a3SMatthew Dillon 
2936258223a3SMatthew Dillon u_int32_t
2937258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
2938258223a3SMatthew Dillon {
2939258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2940258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
2941258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2942258223a3SMatthew Dillon }
2943258223a3SMatthew Dillon 
2944258223a3SMatthew Dillon void
2945258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
2946258223a3SMatthew Dillon {
2947258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2948258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2949258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
2950258223a3SMatthew Dillon }
2951258223a3SMatthew Dillon 
2952831bc9e3SMatthew Dillon /*
2953831bc9e3SMatthew Dillon  * Wait up to (timeout) milliseconds for the masked port register to
2954831bc9e3SMatthew Dillon  * match the target.
2955831bc9e3SMatthew Dillon  *
2956831bc9e3SMatthew Dillon  * Timeout is in milliseconds.
2957831bc9e3SMatthew Dillon  */
2958258223a3SMatthew Dillon int
2959cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
2960cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
2961258223a3SMatthew Dillon {
2962831bc9e3SMatthew Dillon 	int	t;
2963258223a3SMatthew Dillon 
2964831bc9e3SMatthew Dillon 	/*
2965831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
2966831bc9e3SMatthew Dillon 	 */
2967831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
2968258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
2969258223a3SMatthew Dillon 			return (0);
2970831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
2971258223a3SMatthew Dillon 	}
2972258223a3SMatthew Dillon 
2973831bc9e3SMatthew Dillon 	do {
2974831bc9e3SMatthew Dillon 		timeout -= ahci_os_softsleep();
2975831bc9e3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
2976831bc9e3SMatthew Dillon 			return (0);
2977831bc9e3SMatthew Dillon 	} while (timeout > 0);
2978831bc9e3SMatthew Dillon 	return (1);
2979831bc9e3SMatthew Dillon }
2980831bc9e3SMatthew Dillon 
2981831bc9e3SMatthew Dillon int
2982831bc9e3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
2983831bc9e3SMatthew Dillon 	     u_int32_t target)
2984831bc9e3SMatthew Dillon {
2985831bc9e3SMatthew Dillon 	int	t;
2986831bc9e3SMatthew Dillon 
2987831bc9e3SMatthew Dillon 	/*
2988831bc9e3SMatthew Dillon 	 * Loop hard up to 100uS
2989831bc9e3SMatthew Dillon 	 */
2990831bc9e3SMatthew Dillon 	for (t = 0; t < 100; ++t) {
2991831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
2992831bc9e3SMatthew Dillon 			return (0);
2993831bc9e3SMatthew Dillon 		ahci_os_hardsleep(1);	/* us */
2994831bc9e3SMatthew Dillon 	}
2995831bc9e3SMatthew Dillon 
2996831bc9e3SMatthew Dillon 	/*
2997831bc9e3SMatthew Dillon 	 * And one millisecond the slow way
2998831bc9e3SMatthew Dillon 	 */
2999831bc9e3SMatthew Dillon 	t = 1000;
3000831bc9e3SMatthew Dillon 	do {
3001831bc9e3SMatthew Dillon 		t -= ahci_os_softsleep();
3002831bc9e3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
3003831bc9e3SMatthew Dillon 			return (0);
3004831bc9e3SMatthew Dillon 	} while (t > 0);
3005831bc9e3SMatthew Dillon 
3006258223a3SMatthew Dillon 	return (1);
3007258223a3SMatthew Dillon }
3008258223a3SMatthew Dillon 
3009831bc9e3SMatthew Dillon 
30101980eff3SMatthew Dillon /*
30111980eff3SMatthew Dillon  * Acquire an ata transfer.
30121980eff3SMatthew Dillon  *
30131980eff3SMatthew Dillon  * Pass a NULL at for direct-attached transfers, and a non-NULL at for
30141980eff3SMatthew Dillon  * targets that go through the port multiplier.
30151980eff3SMatthew Dillon  */
3016258223a3SMatthew Dillon struct ata_xfer *
30171980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
3018258223a3SMatthew Dillon {
3019258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
3020258223a3SMatthew Dillon 
3021258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
3022258223a3SMatthew Dillon 	if (ccb == NULL) {
3023258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
3024258223a3SMatthew Dillon 		    PORTNAME(ap));
3025258223a3SMatthew Dillon 		return (NULL);
3026258223a3SMatthew Dillon 	}
3027258223a3SMatthew Dillon 
3028258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
3029258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
3030258223a3SMatthew Dillon 
30312cc2e845SMatthew Dillon 	bzero(ccb->ccb_xa.fis, sizeof(*ccb->ccb_xa.fis));
30321980eff3SMatthew Dillon 	ccb->ccb_xa.at = at;
3033258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
3034258223a3SMatthew Dillon 
3035258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
3036258223a3SMatthew Dillon }
3037258223a3SMatthew Dillon 
3038258223a3SMatthew Dillon void
3039258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
3040258223a3SMatthew Dillon {
3041258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3042258223a3SMatthew Dillon 
3043258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
3044258223a3SMatthew Dillon 
3045258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
3046258223a3SMatthew Dillon }
3047258223a3SMatthew Dillon 
3048258223a3SMatthew Dillon int
3049258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
3050258223a3SMatthew Dillon {
3051258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
3052258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
3053258223a3SMatthew Dillon 
3054258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
3055258223a3SMatthew Dillon 
3056258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
3057258223a3SMatthew Dillon 		goto failcmd;
3058258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
3059258223a3SMatthew Dillon 
3060258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
3061258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
30621980eff3SMatthew Dillon 	if (ccb->ccb_xa.at) {
30631980eff3SMatthew Dillon 		cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
30641980eff3SMatthew Dillon 					   AHCI_CMD_LIST_FLAG_PMP_SHIFT);
30651980eff3SMatthew Dillon 	}
3066258223a3SMatthew Dillon 
3067258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
3068258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
3069258223a3SMatthew Dillon 
3070258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
3071258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
3072258223a3SMatthew Dillon 
3073258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
3074258223a3SMatthew Dillon 		goto failcmd;
3075258223a3SMatthew Dillon 
3076258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
3077258223a3SMatthew Dillon 
3078831bc9e3SMatthew Dillon 	if (xa->flags & ATA_F_POLL)
3079831bc9e3SMatthew Dillon 		return (ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout));
3080258223a3SMatthew Dillon 
3081258223a3SMatthew Dillon 	crit_enter();
3082f4553de1SMatthew Dillon 	KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
30833209f581SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_DESIRED;
3084258223a3SMatthew Dillon 	ahci_start(ccb);
3085258223a3SMatthew Dillon 	crit_exit();
3086831bc9e3SMatthew Dillon 	return (xa->state);
3087258223a3SMatthew Dillon 
3088258223a3SMatthew Dillon failcmd:
3089258223a3SMatthew Dillon 	crit_enter();
3090258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
3091258223a3SMatthew Dillon 	xa->complete(xa);
3092258223a3SMatthew Dillon 	crit_exit();
3093831bc9e3SMatthew Dillon 	return (ATA_S_ERROR);
3094258223a3SMatthew Dillon }
3095258223a3SMatthew Dillon 
3096258223a3SMatthew Dillon void
3097258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
3098258223a3SMatthew Dillon {
3099258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
3100258223a3SMatthew Dillon 
3101831bc9e3SMatthew Dillon 	/*
3102831bc9e3SMatthew Dillon 	 * NOTE: callout does not lock port and may race us modifying
3103831bc9e3SMatthew Dillon 	 * the flags, so make sure its stopped.
3104831bc9e3SMatthew Dillon 	 */
3105258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3106258223a3SMatthew Dillon 		callout_stop(&ccb->ccb_timeout);
3107831bc9e3SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3108258223a3SMatthew Dillon 	}
3109f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3110258223a3SMatthew Dillon 
31114c339a5fSMatthew Dillon 	KKASSERT(xa->state != ATA_S_ONCHIP);
3112258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
3113258223a3SMatthew Dillon 
3114258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
3115258223a3SMatthew Dillon 		xa->complete(xa);
3116258223a3SMatthew Dillon }
3117258223a3SMatthew Dillon 
3118f4553de1SMatthew Dillon /*
3119f4553de1SMatthew Dillon  * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3120f4553de1SMatthew Dillon  * while the callout is runing.
3121f4553de1SMatthew Dillon  *
3122f4553de1SMatthew Dillon  * We can't safely get the port lock here or delay, we could block
3123f4553de1SMatthew Dillon  * the callout thread.
3124f4553de1SMatthew Dillon  */
3125258223a3SMatthew Dillon static void
3126258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
3127258223a3SMatthew Dillon {
3128258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
3129258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
3130258223a3SMatthew Dillon 
3131f4553de1SMatthew Dillon 	ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3132f4553de1SMatthew Dillon 	ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3133f4553de1SMatthew Dillon 	ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3134258223a3SMatthew Dillon }
3135258223a3SMatthew Dillon 
31364c339a5fSMatthew Dillon /*
31374c339a5fSMatthew Dillon  * Timeout code, typically called when the port command processor is running.
31384c339a5fSMatthew Dillon  *
31394c339a5fSMatthew Dillon  * We have to be very very careful here.  We cannot stop the port unless
31404c339a5fSMatthew Dillon  * CR is already clear or the only active commands remaining are timed-out
31414c339a5fSMatthew Dillon  * ones.  Otherwise stopping the port will race the command processor and
31424c339a5fSMatthew Dillon  * we can lose events.  While we can theoretically just restart everything
31434c339a5fSMatthew Dillon  * that could result in a double-issue which will not work for ATAPI commands.
31444c339a5fSMatthew Dillon  */
31451980eff3SMatthew Dillon void
3146831bc9e3SMatthew Dillon ahci_ata_cmd_timeout(struct ahci_ccb *ccb)
3147258223a3SMatthew Dillon {
3148258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
3149258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
31504c339a5fSMatthew Dillon 	struct ata_port		*at;
31514c339a5fSMatthew Dillon 	int			ci_saved;
31524c339a5fSMatthew Dillon 	int			slot;
3153258223a3SMatthew Dillon 
31544c339a5fSMatthew Dillon 	at = ccb->ccb_xa.at;
31554c339a5fSMatthew Dillon 
31564c339a5fSMatthew Dillon 	kprintf("%s: CMD TIMEOUT state=%d slot=%d\n"
31574c339a5fSMatthew Dillon 		"\tcmd-reg 0x%b\n"
31584c339a5fSMatthew Dillon 		"\tsactive=%08x active=%08x expired=%08x\n"
315908fb24a7SMatthew Dillon 		"\t   sact=%08x     ci=%08x\n"
316008fb24a7SMatthew Dillon 		"\t    STS=%b\n",
31614c339a5fSMatthew Dillon 		ATANAME(ap, at),
31624c339a5fSMatthew Dillon 		ccb->ccb_xa.state, ccb->ccb_slot,
3163258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
31644c339a5fSMatthew Dillon 		ap->ap_sactive, ap->ap_active, ap->ap_expired,
3165258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
316608fb24a7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI),
316708fb24a7SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS
316808fb24a7SMatthew Dillon 	);
316908fb24a7SMatthew Dillon 
3170258223a3SMatthew Dillon 
31719e145b23SMatthew Dillon 	/*
31729e145b23SMatthew Dillon 	 * NOTE: Timeout will not be running if the command was polled.
31733209f581SMatthew Dillon 	 *	 If we got here at least one of these flags should be set.
31749e145b23SMatthew Dillon 	 */
31753209f581SMatthew Dillon 	KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
31763209f581SMatthew Dillon 			      ATA_F_TIMEOUT_RUNNING));
3177f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3178258223a3SMatthew Dillon 
3179258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
3180258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
31814c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
31824c339a5fSMatthew Dillon 		ccb->ccb_done(ccb);
31834c339a5fSMatthew Dillon 		xa->complete(xa);
31844c339a5fSMatthew Dillon 		ahci_issue_pending_commands(ap, NULL);
31854c339a5fSMatthew Dillon 		return;
31864c339a5fSMatthew Dillon 	}
31874c339a5fSMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
31884c339a5fSMatthew Dillon 		kprintf("%s: Unexpected state during timeout: %d\n",
31894c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_xa.state);
31904c339a5fSMatthew Dillon 		return;
31914c339a5fSMatthew Dillon 	}
31924c339a5fSMatthew Dillon 
31934c339a5fSMatthew Dillon 	/*
31944c339a5fSMatthew Dillon 	 * Ok, we can only get this command off the chip if CR is inactive
31954c339a5fSMatthew Dillon 	 * or if the only commands running on the chip are all expired.
31964c339a5fSMatthew Dillon 	 * Otherwise we have to wait until the port is in a safe state.
31974c339a5fSMatthew Dillon 	 *
31984c339a5fSMatthew Dillon 	 * Do not set state here, it will cause polls to return when the
31994c339a5fSMatthew Dillon 	 * ccb is not yet off the chip.
32004c339a5fSMatthew Dillon 	 */
32014c339a5fSMatthew Dillon 	ap->ap_expired |= 1 << ccb->ccb_slot;
32024c339a5fSMatthew Dillon 
32034c339a5fSMatthew Dillon 	if ((ahci_pread(ap, AHCI_PREG_CMD) & AHCI_PREG_CMD_CR) &&
32044c339a5fSMatthew Dillon 	    (ap->ap_active | ap->ap_sactive) != ap->ap_expired) {
32054c339a5fSMatthew Dillon 		/*
32064c339a5fSMatthew Dillon 		 * If using FBSS or NCQ we can't safely stop the port
32074c339a5fSMatthew Dillon 		 * right now.
32084c339a5fSMatthew Dillon 		 */
32094c339a5fSMatthew Dillon 		kprintf("%s: Deferred timeout until its safe, slot %d\n",
32104c339a5fSMatthew Dillon 			ATANAME(ap, at), ccb->ccb_slot);
32114c339a5fSMatthew Dillon 		return;
32124c339a5fSMatthew Dillon 	}
32134c339a5fSMatthew Dillon 
32144c339a5fSMatthew Dillon 	/*
32154c339a5fSMatthew Dillon 	 * We can safely stop the port and process all expired ccb's,
32164c339a5fSMatthew Dillon 	 * which will include our current ccb.
32174c339a5fSMatthew Dillon 	 */
32184c339a5fSMatthew Dillon 	ci_saved = (ap->ap_sactive) ? ahci_pread(ap, AHCI_PREG_SACT) :
32194c339a5fSMatthew Dillon 				      ahci_pread(ap, AHCI_PREG_CI);
32204c339a5fSMatthew Dillon 	ahci_port_stop(ap, 0);
32214c339a5fSMatthew Dillon 
32224c339a5fSMatthew Dillon 	while (ap->ap_expired) {
32234c339a5fSMatthew Dillon 		slot = ffs(ap->ap_expired) - 1;
32244c339a5fSMatthew Dillon 		ap->ap_expired &= ~(1 << slot);
32254c339a5fSMatthew Dillon 		ci_saved &= ~(1 << slot);
32264c339a5fSMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
32274c339a5fSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
32284c339a5fSMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ) {
32294c339a5fSMatthew Dillon 			KKASSERT(ap->ap_sactive & (1 << slot));
32304c339a5fSMatthew Dillon 			ap->ap_sactive &= ~(1 << slot);
32314c339a5fSMatthew Dillon 		} else {
32324c339a5fSMatthew Dillon 			KKASSERT(ap->ap_active & (1 << slot));
32334c339a5fSMatthew Dillon 			ap->ap_active &= ~(1 << slot);
32341980eff3SMatthew Dillon 			--ap->ap_active_cnt;
32351980eff3SMatthew Dillon 		}
3236258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
32374c339a5fSMatthew Dillon 		ccb->ccb_xa.complete(&ccb->ccb_xa);
3238258223a3SMatthew Dillon 	}
32394c339a5fSMatthew Dillon 	/* ccb invalid now */
3240258223a3SMatthew Dillon 
32414c339a5fSMatthew Dillon 	/*
32424c339a5fSMatthew Dillon 	 * We can safely CLO the port to clear any BSY/DRQ, a case which
32434c339a5fSMatthew Dillon 	 * can occur with port multipliers.  This will unbrick the port
32444c339a5fSMatthew Dillon 	 * and allow commands to other targets behind the PM continue.
32454c339a5fSMatthew Dillon 	 * (FBSS).
32464c339a5fSMatthew Dillon 	 *
32474c339a5fSMatthew Dillon 	 * Finally, once the port has been restarted we can issue any
32484c339a5fSMatthew Dillon 	 * previously saved pending commands, and run the port interrupt
32494c339a5fSMatthew Dillon 	 * code to handle any completions which may have occured when
32504c339a5fSMatthew Dillon 	 * we saved CI.
32514c339a5fSMatthew Dillon 	 */
32524c339a5fSMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
32534c339a5fSMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
32544c339a5fSMatthew Dillon 		kprintf("%s: Warning, issuing CLO after timeout\n",
32554c339a5fSMatthew Dillon 			ATANAME(ap, at));
3256131be210SMatthew Dillon 		ahci_port_clo(ap);
32574c339a5fSMatthew Dillon 	}
3258131be210SMatthew Dillon 	ahci_port_start(ap);
32594c339a5fSMatthew Dillon 	ahci_issue_saved_commands(ap, ci_saved & ~ap->ap_expired);
32604c339a5fSMatthew Dillon 	ahci_issue_pending_commands(ap, NULL);
32614c339a5fSMatthew Dillon 	ahci_port_intr(ap, 0);
32624c339a5fSMatthew Dillon }
32634c339a5fSMatthew Dillon 
3264cf5f3a81SMatthew Dillon /*
32654c339a5fSMatthew Dillon  * Issue a previously saved set of commands
3266cf5f3a81SMatthew Dillon  */
32674c339a5fSMatthew Dillon void
32684c339a5fSMatthew Dillon ahci_issue_saved_commands(struct ahci_port *ap, u_int32_t ci_saved)
32694c339a5fSMatthew Dillon {
32704c339a5fSMatthew Dillon 	if (ci_saved) {
32714c339a5fSMatthew Dillon 		KKASSERT(!((ap->ap_active & ci_saved) &&
32724c339a5fSMatthew Dillon 			   (ap->ap_sactive & ci_saved)));
32734c339a5fSMatthew Dillon 		KKASSERT((ci_saved & ap->ap_expired) == 0);
32744c339a5fSMatthew Dillon 		if (ap->ap_sactive & ci_saved)
32754c339a5fSMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
32764c339a5fSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
3277131be210SMatthew Dillon 	}
3278258223a3SMatthew Dillon }
3279258223a3SMatthew Dillon 
3280831bc9e3SMatthew Dillon /*
3281831bc9e3SMatthew Dillon  * Used by the softreset, pmprobe, and read_ncq_error only, in very
3282831bc9e3SMatthew Dillon  * specialized, controlled circumstances.
3283831bc9e3SMatthew Dillon  *
3284831bc9e3SMatthew Dillon  * Only one command may be pending.
3285831bc9e3SMatthew Dillon  */
3286831bc9e3SMatthew Dillon void
3287831bc9e3SMatthew Dillon ahci_quick_timeout(struct ahci_ccb *ccb)
3288831bc9e3SMatthew Dillon {
3289831bc9e3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
3290831bc9e3SMatthew Dillon 
3291831bc9e3SMatthew Dillon 	switch (ccb->ccb_xa.state) {
3292831bc9e3SMatthew Dillon 	case ATA_S_PENDING:
3293831bc9e3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3294831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3295831bc9e3SMatthew Dillon 		break;
3296831bc9e3SMatthew Dillon 	case ATA_S_ONCHIP:
3297831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active == (1 << ccb->ccb_slot) &&
3298831bc9e3SMatthew Dillon 			 ap->ap_sactive == 0);
3299831bc9e3SMatthew Dillon 		ahci_port_stop(ap, 0);
3300831bc9e3SMatthew Dillon 		ahci_port_start(ap);
3301831bc9e3SMatthew Dillon 
3302831bc9e3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
3303831bc9e3SMatthew Dillon 		ap->ap_active &= ~(1 << ccb->ccb_slot);
3304831bc9e3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt > 0);
3305831bc9e3SMatthew Dillon 		--ap->ap_active_cnt;
3306831bc9e3SMatthew Dillon 		break;
3307831bc9e3SMatthew Dillon 	default:
3308831bc9e3SMatthew Dillon 		panic("%s: ahci_quick_timeout: ccb in bad state %d",
3309831bc9e3SMatthew Dillon 		      ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_xa.state);
3310831bc9e3SMatthew Dillon 	}
3311831bc9e3SMatthew Dillon }
3312831bc9e3SMatthew Dillon 
331312feb904SMatthew Dillon static void
331412feb904SMatthew Dillon ahci_dummy_done(struct ata_xfer *xa)
331512feb904SMatthew Dillon {
331612feb904SMatthew Dillon }
331712feb904SMatthew Dillon 
331812feb904SMatthew Dillon static void
3319258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
3320258223a3SMatthew Dillon {
3321258223a3SMatthew Dillon }
3322