xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision cec85a37534fc8449308734c4d53fe8a31c2772c)
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 
54fd8bd957SMatthew Dillon int	ahci_port_init(struct ahci_port *ap);
55258223a3SMatthew Dillon int	ahci_port_start(struct ahci_port *, int);
56258223a3SMatthew Dillon int	ahci_port_stop(struct ahci_port *, int);
57258223a3SMatthew Dillon int	ahci_port_clo(struct ahci_port *);
58258223a3SMatthew Dillon 
59fd8bd957SMatthew Dillon int	ahci_port_signature_detect(struct ahci_port *ap);
60258223a3SMatthew Dillon int	ahci_load_prdt(struct ahci_ccb *);
61258223a3SMatthew Dillon void	ahci_unload_prdt(struct ahci_ccb *);
62258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
63258223a3SMatthew Dillon 				    int nsegs, int error);
64258223a3SMatthew Dillon int	ahci_poll(struct ahci_ccb *, int, void (*)(void *));
65258223a3SMatthew Dillon void	ahci_start(struct ahci_ccb *);
66258223a3SMatthew Dillon 
67258223a3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *arg);
68258223a3SMatthew Dillon static void ahci_ata_cmd_timeout(void *arg);
69258223a3SMatthew Dillon 
70258223a3SMatthew Dillon void	ahci_issue_pending_ncq_commands(struct ahci_port *);
71258223a3SMatthew Dillon void	ahci_issue_pending_commands(struct ahci_port *, int);
72258223a3SMatthew Dillon 
73258223a3SMatthew Dillon u_int32_t	ahci_port_intr(struct ahci_port *, u_int32_t);
74258223a3SMatthew Dillon 
75258223a3SMatthew Dillon struct ahci_ccb	*ahci_get_ccb(struct ahci_port *);
76258223a3SMatthew Dillon void	ahci_put_ccb(struct ahci_ccb *);
77258223a3SMatthew Dillon 
78258223a3SMatthew Dillon struct ahci_ccb	*ahci_get_err_ccb(struct ahci_port *);
79258223a3SMatthew Dillon void	ahci_put_err_ccb(struct ahci_ccb *);
80258223a3SMatthew Dillon 
81258223a3SMatthew Dillon int	ahci_port_read_ncq_error(struct ahci_port *, int *);
82258223a3SMatthew Dillon 
83258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
84258223a3SMatthew Dillon void	ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
85258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
86258223a3SMatthew Dillon 
87258223a3SMatthew Dillon void	ahci_empty_done(struct ahci_ccb *ccb);
88258223a3SMatthew Dillon void	ahci_ata_cmd_done(struct ahci_ccb *ccb);
89258223a3SMatthew Dillon 
90258223a3SMatthew Dillon /* Wait for all bits in _b to be cleared */
91*cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \
92*cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
93*cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to,  _r, _b) \
94*cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
95258223a3SMatthew Dillon 
96258223a3SMatthew Dillon /* Wait for all bits in _b to be set */
97*cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \
98*cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
99*cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \
100*cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
101*cec85a37SMatthew Dillon 
102*cec85a37SMatthew Dillon #define AHCI_PWAIT_TIMEOUT	1000
103258223a3SMatthew Dillon 
104fd8bd957SMatthew Dillon /*
105fd8bd957SMatthew Dillon  * Initialize the global AHCI hardware.  This code does not set up any of
106fd8bd957SMatthew Dillon  * its ports.
107fd8bd957SMatthew Dillon  */
108258223a3SMatthew Dillon int
109258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc)
110258223a3SMatthew Dillon {
111258223a3SMatthew Dillon 	u_int32_t	cap, pi;
112258223a3SMatthew Dillon 
113258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
114258223a3SMatthew Dillon 		ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
115258223a3SMatthew Dillon 
116258223a3SMatthew Dillon 	/* save BIOS initialised parameters, enable staggered spin up */
117258223a3SMatthew Dillon 	cap = ahci_read(sc, AHCI_REG_CAP);
118258223a3SMatthew Dillon 	cap &= AHCI_REG_CAP_SMPS;
119258223a3SMatthew Dillon 	cap |= AHCI_REG_CAP_SSS;
120258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
121258223a3SMatthew Dillon 
122258223a3SMatthew Dillon 	if (AHCI_REG_GHC_AE & ahci_read(sc, AHCI_REG_GHC)) {
123258223a3SMatthew Dillon 		/* reset the controller */
124258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR);
125258223a3SMatthew Dillon 		if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR,
126258223a3SMatthew Dillon 		    AHCI_REG_GHC_HR) != 0) {
127258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
128258223a3SMatthew Dillon 				      "unable to reset controller\n");
129258223a3SMatthew Dillon 			return (1);
130258223a3SMatthew Dillon 		}
131258223a3SMatthew Dillon 	}
132258223a3SMatthew Dillon 
133258223a3SMatthew Dillon 	/* enable ahci (global interrupts disabled) */
134258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
135258223a3SMatthew Dillon 
136258223a3SMatthew Dillon 	/* restore parameters */
137258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_CAP, cap);
138258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_PI, pi);
139258223a3SMatthew Dillon 
140258223a3SMatthew Dillon 	return (0);
141258223a3SMatthew Dillon }
142258223a3SMatthew Dillon 
143fd8bd957SMatthew Dillon /*
144fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
145fd8bd957SMatthew Dillon  */
146258223a3SMatthew Dillon int
147258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port)
148258223a3SMatthew Dillon {
149258223a3SMatthew Dillon 	struct ahci_port		*ap;
150258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
151258223a3SMatthew Dillon 	u_int64_t			dva;
152258223a3SMatthew Dillon 	u_int32_t			cmd;
153258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*hdr;
154258223a3SMatthew Dillon 	struct ahci_cmd_table		*table;
155258223a3SMatthew Dillon 	int	rc = ENOMEM;
156258223a3SMatthew Dillon 	int	error;
157258223a3SMatthew Dillon 	int	i;
158258223a3SMatthew Dillon 
159258223a3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
160258223a3SMatthew Dillon 	if (ap == NULL) {
161258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
162258223a3SMatthew Dillon 			      "unable to allocate memory for port %d\n",
163258223a3SMatthew Dillon 			      port);
164258223a3SMatthew Dillon 		goto reterr;
165258223a3SMatthew Dillon 	}
166258223a3SMatthew Dillon 
167258223a3SMatthew Dillon 	ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
168258223a3SMatthew Dillon 		  device_get_name(sc->sc_dev),
169258223a3SMatthew Dillon 		  device_get_unit(sc->sc_dev),
170258223a3SMatthew Dillon 		  port);
171258223a3SMatthew Dillon 	sc->sc_ports[port] = ap;
172258223a3SMatthew Dillon 
173258223a3SMatthew Dillon 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
174258223a3SMatthew Dillon 	    AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
175258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
176258223a3SMatthew Dillon 			      "unable to create register window for port %d\n",
177258223a3SMatthew Dillon 			      port);
178258223a3SMatthew Dillon 		goto freeport;
179258223a3SMatthew Dillon 	}
180258223a3SMatthew Dillon 
181258223a3SMatthew Dillon 	ap->ap_sc = sc;
182258223a3SMatthew Dillon 	ap->ap_num = port;
183258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_free);
184258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_pending);
185258223a3SMatthew Dillon 	lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
186258223a3SMatthew Dillon 
187258223a3SMatthew Dillon 	/* Disable port interrupts */
188258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
189258223a3SMatthew Dillon 
190258223a3SMatthew Dillon 	/* Sec 10.1.2 - deinitialise port if it is already running */
191258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
192258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
193258223a3SMatthew Dillon 	    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
194258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
195258223a3SMatthew Dillon 		int r;
196258223a3SMatthew Dillon 
197258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
198258223a3SMatthew Dillon 		if (r) {
199258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
200258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
201258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
202258223a3SMatthew Dillon 			rc = ENXIO;
203258223a3SMatthew Dillon 			goto freeport;
204258223a3SMatthew Dillon 		}
205258223a3SMatthew Dillon 
206258223a3SMatthew Dillon 		/* Write DET to zero */
207258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, 0);
208258223a3SMatthew Dillon 	}
209258223a3SMatthew Dillon 
210258223a3SMatthew Dillon 	/* Allocate RFIS */
211258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
212258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
213258223a3SMatthew Dillon 		kprintf("NORFIS\n");
214258223a3SMatthew Dillon 		goto nomem;
215258223a3SMatthew Dillon 	}
216258223a3SMatthew Dillon 
217258223a3SMatthew Dillon 	/* Setup RFIS base address */
218258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
219258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
220258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
221258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
222258223a3SMatthew Dillon 
223258223a3SMatthew Dillon 	/* Enable FIS reception and activate port. */
224258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
225258223a3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
226258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
227258223a3SMatthew Dillon 
228258223a3SMatthew Dillon 	/* Check whether port activated.  Skip it if not. */
229258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
230258223a3SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
231258223a3SMatthew Dillon 		kprintf("NOT-ACTIVATED\n");
232258223a3SMatthew Dillon 		rc = ENXIO;
233258223a3SMatthew Dillon 		goto freeport;
234258223a3SMatthew Dillon 	}
235258223a3SMatthew Dillon 
236258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
237258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
238258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
239258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
240258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
241258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
242258223a3SMatthew Dillon 			      port);
243258223a3SMatthew Dillon 		goto freeport;
244258223a3SMatthew Dillon 	}
245258223a3SMatthew Dillon 
246258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
247258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
248258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
249258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
250258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
251258223a3SMatthew Dillon nomem:
252258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
253258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
254258223a3SMatthew Dillon 			      port);
255258223a3SMatthew Dillon 		goto freeport;
256258223a3SMatthew Dillon 	}
257258223a3SMatthew Dillon 
258258223a3SMatthew Dillon 	/* Setup command list base address */
259258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
260258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
261258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
262258223a3SMatthew Dillon 
263258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
264258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
265258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
266258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
267258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
268258223a3SMatthew Dillon 
269258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
270258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
271258223a3SMatthew Dillon 		if (error) {
272258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
273258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
274258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
275258223a3SMatthew Dillon 			goto freeport;
276258223a3SMatthew Dillon 		}
277258223a3SMatthew Dillon 
278258223a3SMatthew Dillon 		callout_init(&ccb->ccb_timeout);
279258223a3SMatthew Dillon 		ccb->ccb_slot = i;
280258223a3SMatthew Dillon 		ccb->ccb_port = ap;
281258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
282258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
283258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
284258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
285258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
286258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
287258223a3SMatthew Dillon 
288258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
289258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
290258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
291258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
292258223a3SMatthew Dillon 
293258223a3SMatthew Dillon 		ccb->ccb_xa.ata_put_xfer = ahci_ata_put_xfer;
294258223a3SMatthew Dillon 
295258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
296258223a3SMatthew Dillon 		ahci_put_ccb(ccb);
297258223a3SMatthew Dillon 	}
298258223a3SMatthew Dillon 
299258223a3SMatthew Dillon 	/* Wait for ICC change to complete */
300258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
301258223a3SMatthew Dillon 
302fd8bd957SMatthew Dillon 	/*
303fd8bd957SMatthew Dillon 	 * Do device-related port initialization.  A failure here does not
304fd8bd957SMatthew Dillon 	 * cause the port to be deallocated as we want to receive future
305fd8bd957SMatthew Dillon 	 * hot-plug events.
306fd8bd957SMatthew Dillon 	 */
307fd8bd957SMatthew Dillon 	ahci_port_init(ap);
308fd8bd957SMatthew Dillon 	return(0);
309fd8bd957SMatthew Dillon freeport:
310fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
311fd8bd957SMatthew Dillon reterr:
312fd8bd957SMatthew Dillon 	return (rc);
313fd8bd957SMatthew Dillon }
314fd8bd957SMatthew Dillon 
315fd8bd957SMatthew Dillon /*
316fd8bd957SMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.
317fd8bd957SMatthew Dillon  *
318fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
319fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
320fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
321fd8bd957SMatthew Dillon  *
322fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
323fd8bd957SMatthew Dillon  */
324fd8bd957SMatthew Dillon int
325fd8bd957SMatthew Dillon ahci_port_init(struct ahci_port *ap)
326fd8bd957SMatthew Dillon {
327fd8bd957SMatthew Dillon 	int rc;
328fd8bd957SMatthew Dillon 
329fd8bd957SMatthew Dillon 	/*
330fd8bd957SMatthew Dillon 	 * Hard-reset the port.
331fd8bd957SMatthew Dillon 	 */
332258223a3SMatthew Dillon 	rc = ahci_port_portreset(ap);
333fd8bd957SMatthew Dillon 
334258223a3SMatthew Dillon 	switch (rc) {
335258223a3SMatthew Dillon 	case ENODEV:
336fd8bd957SMatthew Dillon 		/*
337fd8bd957SMatthew Dillon 		 * We had problems talking to the device on the port.
338fd8bd957SMatthew Dillon 		 */
339258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
340258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
341419cb1abSMatthew Dillon 			kprintf("%s: Device not communicating\n", PORTNAME(ap));
342258223a3SMatthew Dillon 			break;
343258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
344419cb1abSMatthew Dillon 			kprintf("%s: PHY offline\n", PORTNAME(ap));
345258223a3SMatthew Dillon 			break;
346258223a3SMatthew Dillon 		default:
347419cb1abSMatthew Dillon 			kprintf("%s: No device detected\n", PORTNAME(ap));
348258223a3SMatthew Dillon 			break;
349258223a3SMatthew Dillon 		}
350258223a3SMatthew Dillon 		break;
351258223a3SMatthew Dillon 
352258223a3SMatthew Dillon 	case EBUSY:
353fd8bd957SMatthew Dillon 		/*
354fd8bd957SMatthew Dillon 		 * The device on the port is still telling us its busy.
355fd8bd957SMatthew Dillon 		 *
356fd8bd957SMatthew Dillon 		 * We try a softreset on the device.
357fd8bd957SMatthew Dillon 		 */
358fd8bd957SMatthew Dillon 		kprintf("%s: Device on port did not come ready, TFD: 0x%b\n",
359fd8bd957SMatthew Dillon 			PORTNAME(ap),
360258223a3SMatthew Dillon 		      ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
361258223a3SMatthew Dillon 
362258223a3SMatthew Dillon 		/* Try a soft reset to clear busy */
363258223a3SMatthew Dillon 		rc = ahci_port_softreset(ap);
364258223a3SMatthew Dillon 		if (rc) {
365fd8bd957SMatthew Dillon 			kprintf("%s: Unable to clear busy device\n",
366fd8bd957SMatthew Dillon 				PORTNAME(ap));
367fd8bd957SMatthew Dillon 		} else {
368fd8bd957SMatthew Dillon 			kprintf("%s: Successfully reset busy device\n",
369fd8bd957SMatthew Dillon 				PORTNAME(ap));
370258223a3SMatthew Dillon 		}
371258223a3SMatthew Dillon 		break;
372258223a3SMatthew Dillon 
373258223a3SMatthew Dillon 	default:
374258223a3SMatthew Dillon 		break;
375258223a3SMatthew Dillon 	}
376258223a3SMatthew Dillon 
377258223a3SMatthew Dillon 	/*
378258223a3SMatthew Dillon 	 * Enable command transfers on the port if a device was detected.
379258223a3SMatthew Dillon 	 * Otherwise leave them disabled but leave the port structure
380258223a3SMatthew Dillon 	 * intact so we get hot-plug interrupts.
381258223a3SMatthew Dillon 	 */
382258223a3SMatthew Dillon 	if (rc == 0) {
383258223a3SMatthew Dillon 		if (ahci_port_start(ap, 0)) {
384fd8bd957SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
385fd8bd957SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
386258223a3SMatthew Dillon 			rc = ENXIO;	/* couldn't start port */
387258223a3SMatthew Dillon 		}
388258223a3SMatthew Dillon 	}
389258223a3SMatthew Dillon 
390258223a3SMatthew Dillon 	/* Flush interrupts for port */
391258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
392fd8bd957SMatthew Dillon 	ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num);
393258223a3SMatthew Dillon 
394258223a3SMatthew Dillon 	/* Enable port interrupts */
395258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE,
396258223a3SMatthew Dillon 			AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
397258223a3SMatthew Dillon 			AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
398258223a3SMatthew Dillon 			AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
399258223a3SMatthew Dillon 			AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
400258223a3SMatthew Dillon #ifdef AHCI_COALESCE
401258223a3SMatthew Dillon 	    ((sc->sc_ccc_ports & (1 << port)) ?
402258223a3SMatthew Dillon 			0 : (AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE))
403258223a3SMatthew Dillon #else
404258223a3SMatthew Dillon 			AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE
405258223a3SMatthew Dillon #endif
406258223a3SMatthew Dillon 	);
407258223a3SMatthew Dillon 	return(rc);
408258223a3SMatthew Dillon }
409258223a3SMatthew Dillon 
410fd8bd957SMatthew Dillon /*
411fd8bd957SMatthew Dillon  * De-initialize and detach a port.
412fd8bd957SMatthew Dillon  */
413258223a3SMatthew Dillon void
414258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
415258223a3SMatthew Dillon {
416258223a3SMatthew Dillon 	struct ahci_port		*ap = sc->sc_ports[port];
417258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
418258223a3SMatthew Dillon 
419258223a3SMatthew Dillon 	/* Ensure port is disabled and its interrupts are flushed */
420258223a3SMatthew Dillon 	if (ap->ap_sc) {
421258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
422258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
423258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
424258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
425258223a3SMatthew Dillon 	}
426258223a3SMatthew Dillon 
427258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
428258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
429258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
430258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
431258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
432258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
433258223a3SMatthew Dillon 			}
434258223a3SMatthew Dillon 		}
435258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
436258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
437258223a3SMatthew Dillon 	}
438258223a3SMatthew Dillon 
439258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
440258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
441258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
442258223a3SMatthew Dillon 	}
443258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
444258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
445258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
446258223a3SMatthew Dillon 	}
447258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
448258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
449258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
450258223a3SMatthew Dillon 	}
451258223a3SMatthew Dillon 
452258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
453258223a3SMatthew Dillon 
454258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
455258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
456258223a3SMatthew Dillon }
457258223a3SMatthew Dillon 
458fd8bd957SMatthew Dillon /*
459fd8bd957SMatthew Dillon  * Start high-level command processing on the port
460fd8bd957SMatthew Dillon  */
461258223a3SMatthew Dillon int
462258223a3SMatthew Dillon ahci_port_start(struct ahci_port *ap, int fre_only)
463258223a3SMatthew Dillon {
464258223a3SMatthew Dillon 	u_int32_t			r;
465258223a3SMatthew Dillon 
466258223a3SMatthew Dillon 	/* Turn on FRE (and ST) */
467258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
468258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_FRE;
469258223a3SMatthew Dillon 	if (!fre_only)
470258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_ST;
471258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
472258223a3SMatthew Dillon 
473258223a3SMatthew Dillon #ifdef AHCI_COALESCE
474258223a3SMatthew Dillon 	/* (Re-)enable coalescing on the port. */
475258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
476258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
477258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
478258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
479258223a3SMatthew Dillon 	}
480258223a3SMatthew Dillon #endif
481258223a3SMatthew Dillon 
482258223a3SMatthew Dillon 	if (!(ap->ap_sc->sc_flags & AHCI_F_IGN_FR)) {
483258223a3SMatthew Dillon 		/* Wait for FR to come on */
484258223a3SMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR))
485258223a3SMatthew Dillon 			return (2);
486258223a3SMatthew Dillon 	}
487258223a3SMatthew Dillon 
488258223a3SMatthew Dillon 	/* Wait for CR to come on */
489258223a3SMatthew Dillon 	if (!fre_only && ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR))
490258223a3SMatthew Dillon 		return (1);
491258223a3SMatthew Dillon 
492258223a3SMatthew Dillon 	return (0);
493258223a3SMatthew Dillon }
494258223a3SMatthew Dillon 
495fd8bd957SMatthew Dillon /*
496fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
497fd8bd957SMatthew Dillon  */
498258223a3SMatthew Dillon int
499258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
500258223a3SMatthew Dillon {
501258223a3SMatthew Dillon 	u_int32_t			r;
502258223a3SMatthew Dillon 
503258223a3SMatthew Dillon #ifdef AHCI_COALESCE
504258223a3SMatthew Dillon 	/* Disable coalescing on the port while it is stopped. */
505258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
506258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
507258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
508258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
509258223a3SMatthew Dillon 	}
510258223a3SMatthew Dillon #endif
511258223a3SMatthew Dillon 
512258223a3SMatthew Dillon 	/* Turn off ST (and FRE) */
513258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
514258223a3SMatthew Dillon 	r &= ~AHCI_PREG_CMD_ST;
515258223a3SMatthew Dillon 	if (stop_fis_rx)
516258223a3SMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
517258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
518258223a3SMatthew Dillon 
519258223a3SMatthew Dillon 	/* Wait for CR to go off */
520258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR))
521258223a3SMatthew Dillon 		return (1);
522258223a3SMatthew Dillon 
523258223a3SMatthew Dillon 	/* Wait for FR to go off */
524258223a3SMatthew Dillon 	if (stop_fis_rx && ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR))
525258223a3SMatthew Dillon 		return (2);
526258223a3SMatthew Dillon 
527258223a3SMatthew Dillon 	return (0);
528258223a3SMatthew Dillon }
529258223a3SMatthew Dillon 
530fd8bd957SMatthew Dillon /*
531fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
532fd8bd957SMatthew Dillon  */
533258223a3SMatthew Dillon int
534258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
535258223a3SMatthew Dillon {
536258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
537258223a3SMatthew Dillon 	u_int32_t			cmd;
538258223a3SMatthew Dillon 
539258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
540258223a3SMatthew Dillon 	if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
541258223a3SMatthew Dillon 		return (1);
542258223a3SMatthew Dillon 
543258223a3SMatthew Dillon 	/* Issue CLO */
544258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
545258223a3SMatthew Dillon #ifdef DIAGNOSTIC
546258223a3SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_ST) {
547258223a3SMatthew Dillon 		kprintf("%s: CLO requested while port running\n",
548258223a3SMatthew Dillon 			PORTNAME(ap));
549258223a3SMatthew Dillon 	}
550258223a3SMatthew Dillon #endif
551258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
552258223a3SMatthew Dillon 
553258223a3SMatthew Dillon 	/* Wait for completion */
554258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
555258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
556258223a3SMatthew Dillon 		return (1);
557258223a3SMatthew Dillon 	}
558258223a3SMatthew Dillon 
559258223a3SMatthew Dillon 	return (0);
560258223a3SMatthew Dillon }
561258223a3SMatthew Dillon 
562fd8bd957SMatthew Dillon /*
563fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
564fd8bd957SMatthew Dillon  *
565fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
566fd8bd957SMatthew Dillon  * a reset to the connected device.
567fd8bd957SMatthew Dillon  */
568258223a3SMatthew Dillon int
569258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
570258223a3SMatthew Dillon {
571258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = NULL;
572258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
573258223a3SMatthew Dillon 	u_int8_t			*fis;
574258223a3SMatthew Dillon 	int				rc = EIO;
575258223a3SMatthew Dillon 	u_int32_t			cmd;
576258223a3SMatthew Dillon 
577258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
578258223a3SMatthew Dillon 
579258223a3SMatthew Dillon 	crit_enter();
580258223a3SMatthew Dillon 
581258223a3SMatthew Dillon 	/* Save previous command register state */
582258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
583258223a3SMatthew Dillon 
584258223a3SMatthew Dillon 	/* Idle port */
585258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
586258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
587258223a3SMatthew Dillon 			PORTNAME(ap));
588258223a3SMatthew Dillon 		goto err;
589258223a3SMatthew Dillon 	}
590258223a3SMatthew Dillon 
591258223a3SMatthew Dillon 	/* Request CLO if device appears hung */
592258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
593258223a3SMatthew Dillon 	     (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
594258223a3SMatthew Dillon 		ahci_port_clo(ap);
595258223a3SMatthew Dillon 	}
596258223a3SMatthew Dillon 
597258223a3SMatthew Dillon 	/* Clear port errors to permit TFD transfer */
598258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR));
599258223a3SMatthew Dillon 
600258223a3SMatthew Dillon 	/* Restart port */
601258223a3SMatthew Dillon 	if (ahci_port_start(ap, 0)) {
602258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
603258223a3SMatthew Dillon 		        PORTNAME(ap));
604258223a3SMatthew Dillon 		goto err;
605258223a3SMatthew Dillon 	}
606258223a3SMatthew Dillon 
607258223a3SMatthew Dillon 	/* Check whether CLO worked */
608258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
609258223a3SMatthew Dillon 	    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
610258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
611258223a3SMatthew Dillon 			PORTNAME(ap),
612258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
613258223a3SMatthew Dillon 			? "failed" : "unsupported");
614258223a3SMatthew Dillon 		rc = EBUSY;
615258223a3SMatthew Dillon 		goto err;
616258223a3SMatthew Dillon 	}
617258223a3SMatthew Dillon 
618*cec85a37SMatthew Dillon 	/*
619*cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
620*cec85a37SMatthew Dillon 	 *
621*cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
622*cec85a37SMatthew Dillon 	 * everything.
623*cec85a37SMatthew Dillon 	 */
624258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
625258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
626258223a3SMatthew Dillon 	bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
627258223a3SMatthew Dillon 
628258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
629*cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
630258223a3SMatthew Dillon 	fis[0] = 0x27;	/* Host to device */
631258223a3SMatthew Dillon 	fis[15] = 0x04;	/* SRST DEVCTL */
632258223a3SMatthew Dillon 
633258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
634258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
635258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
636258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
637258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); /* Write */
638258223a3SMatthew Dillon 
639258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
640*cec85a37SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0) {
641*cec85a37SMatthew Dillon 		kprintf("First FIS failed\n");
642258223a3SMatthew Dillon 		goto err;
643*cec85a37SMatthew Dillon 	}
644258223a3SMatthew Dillon 
645*cec85a37SMatthew Dillon 	/*
646*cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
647*cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
648*cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
649*cec85a37SMatthew Dillon 	 * from the AHCI document.
650*cec85a37SMatthew Dillon 	 *
651*cec85a37SMatthew Dillon 	 * Give the device 1/10 of a second before sending the second
652*cec85a37SMatthew Dillon 	 * FIS.
653*cec85a37SMatthew Dillon 	 *
654*cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
655*cec85a37SMatthew Dillon 	 * everything.
656*cec85a37SMatthew Dillon 	 */
657*cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
658258223a3SMatthew Dillon 	fis[0] = 0x27;	/* Host to device */
659258223a3SMatthew Dillon 	fis[15] = 0;
660258223a3SMatthew Dillon 
661258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
662258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
663258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
664258223a3SMatthew Dillon 
665258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
666*cec85a37SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0) {
667*cec85a37SMatthew Dillon 		kprintf("Second FIS failed\n");
668258223a3SMatthew Dillon 		goto err;
669*cec85a37SMatthew Dillon 	}
670258223a3SMatthew Dillon 
671258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY |
672258223a3SMatthew Dillon 	    AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) {
673258223a3SMatthew Dillon 		kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
674258223a3SMatthew Dillon 			PORTNAME(ap),
675258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
676258223a3SMatthew Dillon 		rc = EBUSY;
677258223a3SMatthew Dillon 		goto err;
678258223a3SMatthew Dillon 	}
679258223a3SMatthew Dillon 
680fd8bd957SMatthew Dillon 	/*
681fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
682fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
683fd8bd957SMatthew Dillon 	 *
684fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
685fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
686fd8bd957SMatthew Dillon 	 * unexpectedly.
687fd8bd957SMatthew Dillon 	 */
688fd8bd957SMatthew Dillon 	if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) {
689fd8bd957SMatthew Dillon 		ap->ap_ata.ap_type = ahci_port_signature_detect(ap);
690fd8bd957SMatthew Dillon 	} else {
691fd8bd957SMatthew Dillon 		if (ahci_port_signature_detect(ap) != ap->ap_ata.ap_type) {
692fd8bd957SMatthew Dillon 			kprintf("%s: device signature unexpectedly changed\n",
693fd8bd957SMatthew Dillon 				PORTNAME(ap));
694fd8bd957SMatthew Dillon 			rc = EBUSY;
695fd8bd957SMatthew Dillon 		}
696fd8bd957SMatthew Dillon 	}
697fd8bd957SMatthew Dillon 
698258223a3SMatthew Dillon 	rc = 0;
699258223a3SMatthew Dillon err:
700258223a3SMatthew Dillon 	if (ccb != NULL) {
701258223a3SMatthew Dillon 		/* Abort our command, if it failed, by stopping command DMA. */
702258223a3SMatthew Dillon 		if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) {
703258223a3SMatthew Dillon 			kprintf("%s: stopping the port, softreset slot "
704258223a3SMatthew Dillon 				"%d was still active.\n",
705258223a3SMatthew Dillon 				PORTNAME(ap),
706258223a3SMatthew Dillon 				ccb->ccb_slot);
707258223a3SMatthew Dillon 			ahci_port_stop(ap, 0);
708258223a3SMatthew Dillon 		}
709258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
710258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
711258223a3SMatthew Dillon 	}
712258223a3SMatthew Dillon 
713258223a3SMatthew Dillon 	/* Restore saved CMD register state */
714258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
715258223a3SMatthew Dillon 
716258223a3SMatthew Dillon 	crit_exit();
717258223a3SMatthew Dillon 
718258223a3SMatthew Dillon 	return (rc);
719258223a3SMatthew Dillon }
720258223a3SMatthew Dillon 
721fd8bd957SMatthew Dillon /*
722fd8bd957SMatthew Dillon  * AHCI port reset, Section 10.4.2
723fd8bd957SMatthew Dillon  *
724fd8bd957SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
725fd8bd957SMatthew Dillon  * connected to the port could still end-up hung.
726fd8bd957SMatthew Dillon  */
727258223a3SMatthew Dillon int
728258223a3SMatthew Dillon ahci_port_portreset(struct ahci_port *ap)
729258223a3SMatthew Dillon {
730258223a3SMatthew Dillon 	u_int32_t			cmd, r;
731258223a3SMatthew Dillon 	int				rc;
732258223a3SMatthew Dillon 
733258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
734258223a3SMatthew Dillon 
735258223a3SMatthew Dillon 	/* Save previous command register state */
736258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
737258223a3SMatthew Dillon 
738258223a3SMatthew Dillon 	/* Clear ST, ignoring failure */
739258223a3SMatthew Dillon 	ahci_port_stop(ap, 0);
740258223a3SMatthew Dillon 
741258223a3SMatthew Dillon 	/* Perform device detection */
742258223a3SMatthew Dillon 	ap->ap_ata.ap_type = ATA_PORT_T_NONE;
743258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, 0);
744258223a3SMatthew Dillon 	DELAY(10000);
745258223a3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
746258223a3SMatthew Dillon 
747258223a3SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
748258223a3SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
749258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
750258223a3SMatthew Dillon 	} else {
751258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
752258223a3SMatthew Dillon 	}
753258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
754258223a3SMatthew Dillon 	DELAY(10000);	/* wait at least 1ms for COMRESET to be sent */
755258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
756258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
757258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
758258223a3SMatthew Dillon 	DELAY(10000);
759258223a3SMatthew Dillon 
760258223a3SMatthew Dillon 	/* Wait for device to be detected and communications established */
761*cec85a37SMatthew Dillon 	if (ahci_pwait_eq(ap, 1000,
762*cec85a37SMatthew Dillon 			  AHCI_PREG_SSTS, AHCI_PREG_SSTS_DET,
763258223a3SMatthew Dillon 			  AHCI_PREG_SSTS_DET_DEV)) {
764258223a3SMatthew Dillon 		rc = ENODEV;
765258223a3SMatthew Dillon 		goto err;
766258223a3SMatthew Dillon 	}
767258223a3SMatthew Dillon 
768258223a3SMatthew Dillon 	/* Clear SERR (incl X bit), so TFD can update */
769258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR));
770258223a3SMatthew Dillon 
771*cec85a37SMatthew Dillon 	/*
772*cec85a37SMatthew Dillon 	 * Wait for device to become ready
773*cec85a37SMatthew Dillon 	 *
774*cec85a37SMatthew Dillon 	 * This can take more then a second, give it 3 seconds.
775*cec85a37SMatthew Dillon 	 */
776*cec85a37SMatthew Dillon 	if (ahci_pwait_clr_to(ap, 3000,
777*cec85a37SMatthew Dillon 			       AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY |
778258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) {
779258223a3SMatthew Dillon 		rc = EBUSY;
780258223a3SMatthew Dillon 		kprintf("%s: Device will not come ready 0x%b\n",
781258223a3SMatthew Dillon 			PORTNAME(ap),
782258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
783258223a3SMatthew Dillon 		goto err;
784258223a3SMatthew Dillon 	}
785258223a3SMatthew Dillon 
786fd8bd957SMatthew Dillon 	ap->ap_ata.ap_type = ahci_port_signature_detect(ap);
787258223a3SMatthew Dillon 	rc = 0;
788258223a3SMatthew Dillon err:
789258223a3SMatthew Dillon 	/* Restore preserved port state */
790258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
791258223a3SMatthew Dillon 
792258223a3SMatthew Dillon 	return (rc);
793258223a3SMatthew Dillon }
794258223a3SMatthew Dillon 
795fd8bd957SMatthew Dillon /*
796fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
797fd8bd957SMatthew Dillon  * DISK.
798fd8bd957SMatthew Dillon  */
799fd8bd957SMatthew Dillon int
800fd8bd957SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap)
801fd8bd957SMatthew Dillon {
802fd8bd957SMatthew Dillon 	u_int32_t sig;
803fd8bd957SMatthew Dillon 
804fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
805fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
806fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
807fd8bd957SMatthew Dillon 	} else {
808fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
809fd8bd957SMatthew Dillon 	}
810fd8bd957SMatthew Dillon }
811fd8bd957SMatthew Dillon 
812fd8bd957SMatthew Dillon /*
813fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
814fd8bd957SMatthew Dillon  */
815258223a3SMatthew Dillon int
816258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
817258223a3SMatthew Dillon {
818258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
819258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
820258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
821258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
822258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
823258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
824258223a3SMatthew Dillon 	int				error;
825258223a3SMatthew Dillon 
826258223a3SMatthew Dillon 	if (xa->datalen == 0) {
827258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
828258223a3SMatthew Dillon 		return (0);
829258223a3SMatthew Dillon 	}
830258223a3SMatthew Dillon 
831258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
832258223a3SMatthew Dillon 				xa->data, xa->datalen,
833258223a3SMatthew Dillon 				ahci_load_prdt_callback,
834258223a3SMatthew Dillon 				&prdt,
835258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
836258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
837258223a3SMatthew Dillon 	if (error != 0) {
838258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
839258223a3SMatthew Dillon 		return (1);
840258223a3SMatthew Dillon 	}
841258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
842258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
843258223a3SMatthew Dillon 
844258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
845258223a3SMatthew Dillon 
846258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_data, dmap,
847258223a3SMatthew Dillon 			(xa->flags & ATA_F_READ) ?
848258223a3SMatthew Dillon 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
849258223a3SMatthew Dillon 
850258223a3SMatthew Dillon 	return (0);
851258223a3SMatthew Dillon 
852258223a3SMatthew Dillon #ifdef DIAGNOSTIC
853258223a3SMatthew Dillon diagerr:
854258223a3SMatthew Dillon 	bus_dmamap_unload(sc->sc_tag_data, dmap);
855258223a3SMatthew Dillon 	return (1);
856258223a3SMatthew Dillon #endif
857258223a3SMatthew Dillon }
858258223a3SMatthew Dillon 
859258223a3SMatthew Dillon /*
860258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
861258223a3SMatthew Dillon  * list is a temporary structure.
862258223a3SMatthew Dillon  */
863258223a3SMatthew Dillon static
864258223a3SMatthew Dillon void
865258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
866258223a3SMatthew Dillon 			int error)
867258223a3SMatthew Dillon {
868258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
869258223a3SMatthew Dillon 	u_int64_t addr;
870258223a3SMatthew Dillon 
871258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
872258223a3SMatthew Dillon 
873258223a3SMatthew Dillon 	while (nsegs) {
874258223a3SMatthew Dillon 		addr = segs->ds_addr;
875258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
876258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
877258223a3SMatthew Dillon #ifdef DIAGNOSTIC
878258223a3SMatthew Dillon 		KKASSERT((addr & 1) == 0);
879258223a3SMatthew Dillon 		KKASSERT((segs->ds_len & 1) == 0);
880258223a3SMatthew Dillon #endif
881258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
882258223a3SMatthew Dillon 		--nsegs;
883258223a3SMatthew Dillon 		if (nsegs)
884258223a3SMatthew Dillon 			++prd;
885258223a3SMatthew Dillon 		++segs;
886258223a3SMatthew Dillon 	}
887258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
888258223a3SMatthew Dillon }
889258223a3SMatthew Dillon 
890258223a3SMatthew Dillon void
891258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
892258223a3SMatthew Dillon {
893258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
894258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
895258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
896258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
897258223a3SMatthew Dillon 
898258223a3SMatthew Dillon 	if (xa->datalen != 0) {
899258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap,
900258223a3SMatthew Dillon 				(xa->flags & ATA_F_READ) ?
901258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
902258223a3SMatthew Dillon 
903258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
904258223a3SMatthew Dillon 
905258223a3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ)
906258223a3SMatthew Dillon 			xa->resid = 0;
907258223a3SMatthew Dillon 		else
908258223a3SMatthew Dillon 			xa->resid = xa->datalen -
909258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
910258223a3SMatthew Dillon 	}
911258223a3SMatthew Dillon }
912258223a3SMatthew Dillon 
913258223a3SMatthew Dillon int
914258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *))
915258223a3SMatthew Dillon {
916258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
917258223a3SMatthew Dillon 
918258223a3SMatthew Dillon 	crit_enter();
919258223a3SMatthew Dillon 	ahci_start(ccb);
920258223a3SMatthew Dillon 	do {
921258223a3SMatthew Dillon 		if (ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS) &
922258223a3SMatthew Dillon 		    (1 << ccb->ccb_slot)) {
923258223a3SMatthew Dillon 			crit_exit();
924258223a3SMatthew Dillon 			return (0);
925258223a3SMatthew Dillon 		}
926258223a3SMatthew Dillon 		DELAY(1000000 / hz);
927258223a3SMatthew Dillon 	} while (--timeout > 0);
928258223a3SMatthew Dillon 	kprintf("timeout ccb state %d\n", ccb->ccb_xa.state);
929258223a3SMatthew Dillon 
930258223a3SMatthew Dillon 	if (timeout_fn != NULL)
931258223a3SMatthew Dillon 		timeout_fn(ccb);
932258223a3SMatthew Dillon 	crit_exit();
933258223a3SMatthew Dillon 
934258223a3SMatthew Dillon 	return (1);
935258223a3SMatthew Dillon }
936258223a3SMatthew Dillon 
937258223a3SMatthew Dillon void
938258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
939258223a3SMatthew Dillon {
940258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
941258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
942258223a3SMatthew Dillon 
943258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
944258223a3SMatthew Dillon 
945258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
946258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
947258223a3SMatthew Dillon 
948258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
949258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
950258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
951258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
952258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
953258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
954258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
955258223a3SMatthew Dillon 
956258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
957258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
958258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
959258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
960258223a3SMatthew Dillon 
961258223a3SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
962258223a3SMatthew Dillon 		/* Issue NCQ commands only when there are no outstanding
963258223a3SMatthew Dillon 		 * standard commands. */
964258223a3SMatthew Dillon 		if (ap->ap_active != 0 || !TAILQ_EMPTY(&ap->ap_ccb_pending))
965258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
966258223a3SMatthew Dillon 		else {
967258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
968258223a3SMatthew Dillon 			ap->ap_sactive |= (1 << ccb->ccb_slot);
969258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
970258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
971258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
972258223a3SMatthew Dillon 		}
973258223a3SMatthew Dillon 	} else {
974258223a3SMatthew Dillon 		/* Wait for all NCQ commands to finish before issuing standard
975258223a3SMatthew Dillon 		 * command. */
976258223a3SMatthew Dillon 		if (ap->ap_sactive != 0 || ap->ap_active_cnt == 2)
977258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
978258223a3SMatthew Dillon 		else if (ap->ap_active_cnt < 2) {
979258223a3SMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
980258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
981258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
982258223a3SMatthew Dillon 			ap->ap_active_cnt++;
983258223a3SMatthew Dillon 		}
984258223a3SMatthew Dillon 	}
985258223a3SMatthew Dillon }
986258223a3SMatthew Dillon 
987258223a3SMatthew Dillon void
988258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap)
989258223a3SMatthew Dillon {
990258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
991258223a3SMatthew Dillon 	u_int32_t			sact_change = 0;
992258223a3SMatthew Dillon 
993258223a3SMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
994258223a3SMatthew Dillon 
995258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
996258223a3SMatthew Dillon 	if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
997258223a3SMatthew Dillon 		return;
998258223a3SMatthew Dillon 
999258223a3SMatthew Dillon 	/* Start all the NCQ commands at the head of the pending list. */
1000258223a3SMatthew Dillon 	do {
1001258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1002258223a3SMatthew Dillon 		sact_change |= 1 << nextccb->ccb_slot;
1003258223a3SMatthew Dillon 		nextccb->ccb_xa.state = ATA_S_ONCHIP;
1004258223a3SMatthew Dillon 		nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1005258223a3SMatthew Dillon 	} while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
1006258223a3SMatthew Dillon 
1007258223a3SMatthew Dillon 	ap->ap_sactive |= sact_change;
1008258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
1009258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
1010258223a3SMatthew Dillon 
1011258223a3SMatthew Dillon 	return;
1012258223a3SMatthew Dillon }
1013258223a3SMatthew Dillon 
1014258223a3SMatthew Dillon void
1015258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
1016258223a3SMatthew Dillon {
1017258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
1018258223a3SMatthew Dillon 
1019258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1020258223a3SMatthew Dillon 	if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) {
1021258223a3SMatthew Dillon 		KKASSERT(last_was_ncq == 0);	/* otherwise it should have
1022258223a3SMatthew Dillon 						 * been started already. */
1023258223a3SMatthew Dillon 
1024258223a3SMatthew Dillon 		/* Issue NCQ commands only when there are no outstanding
1025258223a3SMatthew Dillon 		 * standard commands. */
1026258223a3SMatthew Dillon 		ap->ap_active_cnt--;
1027258223a3SMatthew Dillon 		if (ap->ap_active == 0)
1028258223a3SMatthew Dillon 			ahci_issue_pending_ncq_commands(ap);
1029258223a3SMatthew Dillon 		else
1030258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 1);
1031258223a3SMatthew Dillon 	} else if (nextccb) {
1032258223a3SMatthew Dillon 		if (ap->ap_sactive != 0 || last_was_ncq)
1033258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
1034258223a3SMatthew Dillon 
1035258223a3SMatthew Dillon 		/* Wait for all NCQ commands to finish before issuing standard
1036258223a3SMatthew Dillon 		 * command. */
1037258223a3SMatthew Dillon 		if (ap->ap_sactive != 0)
1038258223a3SMatthew Dillon 			return;
1039258223a3SMatthew Dillon 
1040258223a3SMatthew Dillon 		/* Keep up to 2 standard commands on-chip at a time. */
1041258223a3SMatthew Dillon 		do {
1042258223a3SMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1043258223a3SMatthew Dillon 			ap->ap_active |= 1 << nextccb->ccb_slot;
1044258223a3SMatthew Dillon 			nextccb->ccb_xa.state = ATA_S_ONCHIP;
1045258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
1046258223a3SMatthew Dillon 			if (last_was_ncq)
1047258223a3SMatthew Dillon 				ap->ap_active_cnt++;
1048258223a3SMatthew Dillon 			if (ap->ap_active_cnt == 2)
1049258223a3SMatthew Dillon 				break;
1050258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 1);
1051258223a3SMatthew Dillon 			nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1052258223a3SMatthew Dillon 		} while (nextccb && !(nextccb->ccb_xa.flags & ATA_F_NCQ));
1053258223a3SMatthew Dillon 	} else if (!last_was_ncq) {
1054258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 1 || ap->ap_active_cnt == 2);
1055258223a3SMatthew Dillon 
1056258223a3SMatthew Dillon 		/* Standard command finished, none waiting to start. */
1057258223a3SMatthew Dillon 		ap->ap_active_cnt--;
1058258223a3SMatthew Dillon 	} else {
1059258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
1060258223a3SMatthew Dillon 
1061258223a3SMatthew Dillon 		/* NCQ command finished. */
1062258223a3SMatthew Dillon 	}
1063258223a3SMatthew Dillon }
1064258223a3SMatthew Dillon 
1065258223a3SMatthew Dillon void
1066258223a3SMatthew Dillon ahci_intr(void *arg)
1067258223a3SMatthew Dillon {
1068258223a3SMatthew Dillon 	struct ahci_softc		*sc = arg;
1069258223a3SMatthew Dillon 	u_int32_t			is, ack = 0;
1070258223a3SMatthew Dillon 	int				port;
1071258223a3SMatthew Dillon 
1072258223a3SMatthew Dillon 	/* Read global interrupt status */
1073258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
1074258223a3SMatthew Dillon 	if (is == 0 || is == 0xffffffff)
1075258223a3SMatthew Dillon 		return;
1076258223a3SMatthew Dillon 	ack = is;
1077258223a3SMatthew Dillon 
1078258223a3SMatthew Dillon #ifdef AHCI_COALESCE
1079258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
1080258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
1081258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
1082258223a3SMatthew Dillon 		    DEVNAME(sc));
1083258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
1084258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
1085258223a3SMatthew Dillon 	}
1086258223a3SMatthew Dillon #endif
1087258223a3SMatthew Dillon 
1088258223a3SMatthew Dillon 	/* Process interrupts for each port */
1089258223a3SMatthew Dillon 	while (is) {
1090258223a3SMatthew Dillon 		port = ffs(is) - 1;
1091258223a3SMatthew Dillon 		if (sc->sc_ports[port]) {
1092258223a3SMatthew Dillon 			ahci_port_intr(sc->sc_ports[port],
1093258223a3SMatthew Dillon 				       AHCI_PREG_CI_ALL_SLOTS);
1094258223a3SMatthew Dillon 		}
1095258223a3SMatthew Dillon 		is &= ~(1 << port);
1096258223a3SMatthew Dillon 	}
1097258223a3SMatthew Dillon 
1098258223a3SMatthew Dillon 	/* Finally, acknowledge global interrupt */
1099258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, ack);
1100258223a3SMatthew Dillon }
1101258223a3SMatthew Dillon 
1102258223a3SMatthew Dillon u_int32_t
1103258223a3SMatthew Dillon ahci_port_intr(struct ahci_port *ap, u_int32_t ci_mask)
1104258223a3SMatthew Dillon {
1105258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1106258223a3SMatthew Dillon 	u_int32_t			is, ci_saved, ci_masked, processed = 0;
1107258223a3SMatthew Dillon 	int				slot, need_restart = 0;
1108258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = NULL;
1109258223a3SMatthew Dillon 	volatile u_int32_t		*active;
1110258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1111258223a3SMatthew Dillon 	u_int32_t			tmp;
1112258223a3SMatthew Dillon #endif
1113258223a3SMatthew Dillon 
1114258223a3SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
1115258223a3SMatthew Dillon 
1116258223a3SMatthew Dillon 	/* Ack port interrupt only if checking all command slots. */
1117258223a3SMatthew Dillon 	if (ci_mask == AHCI_PREG_CI_ALL_SLOTS)
1118258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is);
1119258223a3SMatthew Dillon 
1120258223a3SMatthew Dillon 	if (is)
1121258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: interrupt: %b\n", PORTNAME(ap),
1122258223a3SMatthew Dillon 			is, AHCI_PFMT_IS);
1123258223a3SMatthew Dillon 
1124258223a3SMatthew Dillon 	if (ap->ap_sactive) {
1125258223a3SMatthew Dillon 		/* Active NCQ commands - use SActive instead of CI */
1126258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
1127258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
1128258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
1129258223a3SMatthew Dillon 		active = &ap->ap_sactive;
1130258223a3SMatthew Dillon 	} else {
1131258223a3SMatthew Dillon 		/* Save CI */
1132258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
1133258223a3SMatthew Dillon 		active = &ap->ap_active;
1134258223a3SMatthew Dillon 	}
1135258223a3SMatthew Dillon 
1136258223a3SMatthew Dillon 	/* Command failed.  See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. */
1137258223a3SMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
1138258223a3SMatthew Dillon 		u_int32_t		tfd, serr;
1139258223a3SMatthew Dillon 		int			err_slot;
1140258223a3SMatthew Dillon 
1141258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
1142258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
1143258223a3SMatthew Dillon 
1144258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
1145258223a3SMatthew Dillon 			/* Errored slot is easy to determine from CMD. */
1146258223a3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap,
1147258223a3SMatthew Dillon 			    AHCI_PREG_CMD));
1148258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1149258223a3SMatthew Dillon 
1150258223a3SMatthew Dillon 			/* Preserve received taskfile data from the RFIS. */
1151258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
1152258223a3SMatthew Dillon 			    sizeof(struct ata_fis_d2h));
1153258223a3SMatthew Dillon 		} else
1154258223a3SMatthew Dillon 			err_slot = -1;	/* Must extract error from log page */
1155258223a3SMatthew Dillon 
1156258223a3SMatthew Dillon 		DPRINTF(AHCI_D_VERBOSE, "%s: errored slot %d, TFD: %b, SERR:"
1157258223a3SMatthew Dillon 		    " %b, DIAG: %b\n", PORTNAME(ap), err_slot, tfd,
1158258223a3SMatthew Dillon 		    AHCI_PFMT_TFD_STS, AHCI_PREG_SERR_ERR(serr),
1159258223a3SMatthew Dillon 		    AHCI_PFMT_SERR_ERR, AHCI_PREG_SERR_DIAG(serr),
1160258223a3SMatthew Dillon 		    AHCI_PFMT_SERR_DIAG);
1161258223a3SMatthew Dillon 
1162258223a3SMatthew Dillon 		/* Turn off ST to clear CI and SACT. */
1163258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
1164258223a3SMatthew Dillon 		need_restart = 1;
1165258223a3SMatthew Dillon 
1166258223a3SMatthew Dillon 		/* Clear SERR to enable capturing new errors. */
1167258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
1168258223a3SMatthew Dillon 
1169258223a3SMatthew Dillon 		/* Acknowledge the interrupts we can recover from. */
1170258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
1171258223a3SMatthew Dillon 		    AHCI_PREG_IS_IFS);
1172258223a3SMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
1173258223a3SMatthew Dillon 
1174258223a3SMatthew Dillon 		/* If device hasn't cleared its busy status, try to idle it. */
1175258223a3SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1176258223a3SMatthew Dillon 			kprintf("%s: attempting to idle device\n",
1177258223a3SMatthew Dillon 				PORTNAME(ap));
1178258223a3SMatthew Dillon 			if (ahci_port_softreset(ap)) {
1179258223a3SMatthew Dillon 				kprintf("%s: failed to soft reset device\n",
1180258223a3SMatthew Dillon 					PORTNAME(ap));
1181258223a3SMatthew Dillon 				if (ahci_port_portreset(ap)) {
1182258223a3SMatthew Dillon 					kprintf("%s: failed to port reset "
1183258223a3SMatthew Dillon 						"device, give up on it\n",
1184258223a3SMatthew Dillon 						PORTNAME(ap));
1185258223a3SMatthew Dillon 					goto fatal;
1186258223a3SMatthew Dillon 				}
1187258223a3SMatthew Dillon 			}
1188258223a3SMatthew Dillon 
1189258223a3SMatthew Dillon 			/* Had to reset device, can't gather extended info. */
1190258223a3SMatthew Dillon 		} else if (ap->ap_sactive) {
1191258223a3SMatthew Dillon 			/* Recover the NCQ error from log page 10h. */
1192258223a3SMatthew Dillon 			ahci_port_read_ncq_error(ap, &err_slot);
1193258223a3SMatthew Dillon 			if (err_slot < 0)
1194258223a3SMatthew Dillon 				goto failall;
1195258223a3SMatthew Dillon 
1196258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
1197258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
1198258223a3SMatthew Dillon 
1199258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1200258223a3SMatthew Dillon 		} else {
1201258223a3SMatthew Dillon 			/* Didn't reset, could gather extended info from log. */
1202258223a3SMatthew Dillon 		}
1203258223a3SMatthew Dillon 
1204258223a3SMatthew Dillon 		/*
1205258223a3SMatthew Dillon 		 * If we couldn't determine the errored slot, reset the port
1206258223a3SMatthew Dillon 		 * and fail all the active slots.
1207258223a3SMatthew Dillon 		 */
1208258223a3SMatthew Dillon 		if (err_slot == -1) {
1209258223a3SMatthew Dillon 			if (ahci_port_softreset(ap) != 0 &&
1210258223a3SMatthew Dillon 			    ahci_port_portreset(ap) != 0) {
1211258223a3SMatthew Dillon 				kprintf("%s: couldn't reset after NCQ error, "
1212258223a3SMatthew Dillon 					"disabling device.\n",
1213258223a3SMatthew Dillon 					PORTNAME(ap));
1214258223a3SMatthew Dillon 				goto fatal;
1215258223a3SMatthew Dillon 			}
1216258223a3SMatthew Dillon 			kprintf("%s: couldn't recover NCQ error, failing "
1217258223a3SMatthew Dillon 				"all outstanding commands.\n",
1218258223a3SMatthew Dillon 				PORTNAME(ap));
1219258223a3SMatthew Dillon 			goto failall;
1220258223a3SMatthew Dillon 		}
1221258223a3SMatthew Dillon 
1222258223a3SMatthew Dillon 		/* Clear the failed command in saved CI so completion runs. */
1223258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
1224258223a3SMatthew Dillon 
1225258223a3SMatthew Dillon 		/* Note the error in the ata_xfer. */
1226258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
1227258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
1228258223a3SMatthew Dillon 
1229258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1230258223a3SMatthew Dillon 		/* There may only be one outstanding standard command now. */
1231258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
1232258223a3SMatthew Dillon 			tmp = ci_saved;
1233258223a3SMatthew Dillon 			if (tmp) {
1234258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
1235258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
1236258223a3SMatthew Dillon 				KKASSERT(tmp == 0);
1237258223a3SMatthew Dillon 			}
1238258223a3SMatthew Dillon 		}
1239258223a3SMatthew Dillon #endif
1240258223a3SMatthew Dillon 	}
1241258223a3SMatthew Dillon 
1242258223a3SMatthew Dillon 	/*
1243258223a3SMatthew Dillon 	 * Port change (hot-plug).
1244258223a3SMatthew Dillon 	 *
1245258223a3SMatthew Dillon 	 * A PCS interrupt will occur on hot-plug once communication is
1246258223a3SMatthew Dillon 	 * established.
1247258223a3SMatthew Dillon 	 *
1248258223a3SMatthew Dillon 	 * A PRCS interrupt will occur on hot-unplug (and possibly also
1249258223a3SMatthew Dillon 	 * on hot-plug).
1250258223a3SMatthew Dillon 	 *
1251258223a3SMatthew Dillon 	 * We can then check the CPS (Cold Presence State) bit to determine
1252258223a3SMatthew Dillon 	 * if a device is plugged in or not and do the right thing.
1253258223a3SMatthew Dillon 	 */
1254258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
1255258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
1256258223a3SMatthew Dillon 			(AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) << 16);
1257*cec85a37SMatthew Dillon 
1258258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
1259258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
1260258223a3SMatthew Dillon 			if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) {
1261*cec85a37SMatthew Dillon 				kprintf("%s: HOTPLUG - Device inserted\n",
1262258223a3SMatthew Dillon 					PORTNAME(ap));
1263fd8bd957SMatthew Dillon 				if (ahci_port_init(ap) == 0)
1264fd8bd957SMatthew Dillon 					ahci_cam_changed(ap, 1);
1265258223a3SMatthew Dillon 			}
1266258223a3SMatthew Dillon 			break;
1267258223a3SMatthew Dillon 		default:
1268258223a3SMatthew Dillon 			if (ap->ap_ata.ap_type != ATA_PORT_T_NONE) {
1269258223a3SMatthew Dillon 				kprintf("%s: HOTPLUG - Device removed\n",
1270258223a3SMatthew Dillon 					PORTNAME(ap));
1271258223a3SMatthew Dillon 				ahci_port_portreset(ap);
1272fd8bd957SMatthew Dillon 				ahci_cam_changed(ap, 0);
1273258223a3SMatthew Dillon 			}
1274258223a3SMatthew Dillon 			break;
1275258223a3SMatthew Dillon 		}
1276258223a3SMatthew Dillon 	}
1277258223a3SMatthew Dillon 
1278258223a3SMatthew Dillon 	/* Check for remaining errors - they are fatal. */
1279258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
1280258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
12814444122dSMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
12824444122dSMatthew Dillon 		kprintf("%s: unrecoverable errors (IS: %b, SERR: %b %b), "
12834444122dSMatthew Dillon 			"disabling port.\n",
12844444122dSMatthew Dillon 			PORTNAME(ap),
12854444122dSMatthew Dillon 			is, AHCI_PFMT_IS,
12864444122dSMatthew Dillon 			AHCI_PREG_SERR_ERR(serr), AHCI_PFMT_SERR_ERR,
12874444122dSMatthew Dillon 			AHCI_PREG_SERR_DIAG(serr), AHCI_PFMT_SERR_DIAG
12884444122dSMatthew Dillon 		);
1289258223a3SMatthew Dillon 		/* XXX try recovery first */
1290258223a3SMatthew Dillon 		goto fatal;
1291258223a3SMatthew Dillon 	}
1292258223a3SMatthew Dillon 
1293258223a3SMatthew Dillon 	/* Fail all outstanding commands if we know the port won't recover. */
1294258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
1295258223a3SMatthew Dillon fatal:
1296258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
1297258223a3SMatthew Dillon failall:
1298258223a3SMatthew Dillon 
1299258223a3SMatthew Dillon 		/* Ensure port is shut down. */
1300258223a3SMatthew Dillon 		ahci_port_stop(ap, 1);
1301258223a3SMatthew Dillon 
1302258223a3SMatthew Dillon 		/* Error all the active slots. */
1303258223a3SMatthew Dillon 		ci_masked = ci_saved & *active;
1304258223a3SMatthew Dillon 		while (ci_masked) {
1305258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
1306258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
1307258223a3SMatthew Dillon 			ci_masked &= ~(1 << slot);
1308258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ERROR;
1309258223a3SMatthew Dillon 		}
1310258223a3SMatthew Dillon 
1311258223a3SMatthew Dillon 		/* Run completion for all active slots. */
1312258223a3SMatthew Dillon 		ci_saved &= ~*active;
1313258223a3SMatthew Dillon 
1314258223a3SMatthew Dillon 		/*
1315258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
1316258223a3SMatthew Dillon 		 *
1317258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
1318258223a3SMatthew Dillon 		 * a livelock.
1319258223a3SMatthew Dillon 		 */
1320258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
1321258223a3SMatthew Dillon 			need_restart = 0;
1322258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
1323258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
1324258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
1325258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
1326258223a3SMatthew Dillon 		}
1327258223a3SMatthew Dillon 	}
1328258223a3SMatthew Dillon 
1329258223a3SMatthew Dillon 	/*
1330258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
1331258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
1332258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
1333258223a3SMatthew Dillon 	 */
1334258223a3SMatthew Dillon 	ci_masked = ~ci_saved & *active & ci_mask;
1335258223a3SMatthew Dillon 	while (ci_masked) {
1336258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
1337258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
1338258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
1339258223a3SMatthew Dillon 
1340258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
1341258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
1342258223a3SMatthew Dillon 		    " (error)" : "");
1343258223a3SMatthew Dillon 
1344258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
1345258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1346258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
1347258223a3SMatthew Dillon 
1348258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
1349258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1350258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
1351258223a3SMatthew Dillon 
1352258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
1353258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1354258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
1355258223a3SMatthew Dillon 
1356258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
1357258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
1358258223a3SMatthew Dillon 
1359258223a3SMatthew Dillon 		processed |= 1 << ccb->ccb_slot;
1360258223a3SMatthew Dillon 	}
1361258223a3SMatthew Dillon 
1362258223a3SMatthew Dillon 	if (need_restart) {
1363258223a3SMatthew Dillon 		/* Restart command DMA on the port */
1364258223a3SMatthew Dillon 		ahci_port_start(ap, 0);
1365258223a3SMatthew Dillon 
1366258223a3SMatthew Dillon 		/* Re-enable outstanding commands on port. */
1367258223a3SMatthew Dillon 		if (ci_saved) {
1368258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1369258223a3SMatthew Dillon 			tmp = ci_saved;
1370258223a3SMatthew Dillon 			while (tmp) {
1371258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
1372258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
1373258223a3SMatthew Dillon 				ccb = &ap->ap_ccbs[slot];
1374258223a3SMatthew Dillon 				KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
1375258223a3SMatthew Dillon 				KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
1376258223a3SMatthew Dillon 				    (!!ap->ap_sactive));
1377258223a3SMatthew Dillon 			}
1378258223a3SMatthew Dillon #endif
1379258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
1380258223a3SMatthew Dillon 			    "re-enabling%s slots %08x\n", PORTNAME(ap),
1381258223a3SMatthew Dillon 			    ap->ap_sactive ? " NCQ" : "", ci_saved);
1382258223a3SMatthew Dillon 
1383258223a3SMatthew Dillon 			if (ap->ap_sactive)
1384258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
1385258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
1386258223a3SMatthew Dillon 		}
1387258223a3SMatthew Dillon 	}
1388258223a3SMatthew Dillon 
1389258223a3SMatthew Dillon 	return (processed);
1390258223a3SMatthew Dillon }
1391258223a3SMatthew Dillon 
1392258223a3SMatthew Dillon struct ahci_ccb *
1393258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
1394258223a3SMatthew Dillon {
1395258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
1396258223a3SMatthew Dillon 
1397258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
1398258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
1399258223a3SMatthew Dillon 	if (ccb != NULL) {
1400258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
1401258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
1402258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
1403258223a3SMatthew Dillon 	}
1404258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
1405258223a3SMatthew Dillon 
1406258223a3SMatthew Dillon 	return (ccb);
1407258223a3SMatthew Dillon }
1408258223a3SMatthew Dillon 
1409258223a3SMatthew Dillon void
1410258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
1411258223a3SMatthew Dillon {
1412258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1413258223a3SMatthew Dillon 
1414258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1415258223a3SMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
1416258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_TIMEOUT &&
1417258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_ERROR) {
1418258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
1419258223a3SMatthew Dillon 			"slot %d\n",
1420258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
1421258223a3SMatthew Dillon 			ccb->ccb_slot);
1422258223a3SMatthew Dillon 	}
1423258223a3SMatthew Dillon #endif
1424258223a3SMatthew Dillon 
1425258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
1426258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
1427258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
1428258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
1429258223a3SMatthew Dillon }
1430258223a3SMatthew Dillon 
1431258223a3SMatthew Dillon struct ahci_ccb *
1432258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
1433258223a3SMatthew Dillon {
1434258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
1435258223a3SMatthew Dillon 	u_int32_t sact;
1436258223a3SMatthew Dillon 
1437258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
1438258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
1439258223a3SMatthew Dillon 	if (sact != 0)
1440258223a3SMatthew Dillon 		kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
1441258223a3SMatthew Dillon 	KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
1442258223a3SMatthew Dillon 
1443258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1444258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy == 0);
1445258223a3SMatthew Dillon 	ap->ap_err_busy = 1;
1446258223a3SMatthew Dillon #endif
1447258223a3SMatthew Dillon 	/* Save outstanding command state. */
1448258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
1449258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
1450258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
1451258223a3SMatthew Dillon 
1452258223a3SMatthew Dillon 	/*
1453258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
1454258223a3SMatthew Dillon 	 * run prematurely.
1455258223a3SMatthew Dillon 	 */
1456258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
1457258223a3SMatthew Dillon 
1458258223a3SMatthew Dillon 	/*
1459258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
1460258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
1461258223a3SMatthew Dillon 	 */
1462258223a3SMatthew Dillon 	err_ccb = ahci_get_ccb(ap);
1463258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
1464258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
1465258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
1466258223a3SMatthew Dillon 
1467258223a3SMatthew Dillon 	return err_ccb;
1468258223a3SMatthew Dillon }
1469258223a3SMatthew Dillon 
1470258223a3SMatthew Dillon void
1471258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
1472258223a3SMatthew Dillon {
1473258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
1474258223a3SMatthew Dillon 	u_int32_t sact;
1475258223a3SMatthew Dillon 
1476258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1477258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy);
1478258223a3SMatthew Dillon #endif
1479258223a3SMatthew Dillon 	/* No commands may be active on the chip */
1480258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
1481258223a3SMatthew Dillon 	if (sact != 0) {
1482258223a3SMatthew Dillon 		kprintf("ahci_port_err_ccb_restore but SACT %08x != 0?\n",
1483258223a3SMatthew Dillon 			sact);
1484258223a3SMatthew Dillon 	}
1485258223a3SMatthew Dillon 	KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
1486258223a3SMatthew Dillon 
1487258223a3SMatthew Dillon 	/* Done with the CCB */
1488258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
1489258223a3SMatthew Dillon 
1490258223a3SMatthew Dillon 	/* Restore outstanding command state */
1491258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
1492258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
1493258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
1494258223a3SMatthew Dillon 
1495258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1496258223a3SMatthew Dillon 	ap->ap_err_busy = 0;
1497258223a3SMatthew Dillon #endif
1498258223a3SMatthew Dillon }
1499258223a3SMatthew Dillon 
1500258223a3SMatthew Dillon int
1501258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
1502258223a3SMatthew Dillon {
1503258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
1504258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
1505258223a3SMatthew Dillon 	u_int32_t			cmd;
1506258223a3SMatthew Dillon 	struct ata_fis_h2d		*fis;
1507258223a3SMatthew Dillon 	int				rc = EIO;
1508258223a3SMatthew Dillon 
1509258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
1510258223a3SMatthew Dillon 
1511258223a3SMatthew Dillon 	/* Save command register state. */
1512258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1513258223a3SMatthew Dillon 
1514258223a3SMatthew Dillon 	/* Port should have been idled already.  Start it. */
1515258223a3SMatthew Dillon 	KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
1516258223a3SMatthew Dillon 	ahci_port_start(ap, 0);
1517258223a3SMatthew Dillon 
1518258223a3SMatthew Dillon 	/* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
1519258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
1520258223a3SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
1521258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
1522258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
1523258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1524258223a3SMatthew Dillon 	bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
1525258223a3SMatthew Dillon 
1526258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
1527258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
1528258223a3SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD;
1529258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
1530258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
1531258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
1532258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
1533258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
1534258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
1535258223a3SMatthew Dillon 	fis->device = 0;
1536258223a3SMatthew Dillon 
1537258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1538258223a3SMatthew Dillon 
1539258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
1540258223a3SMatthew Dillon 		rc = ENOMEM;	/* XXX caller must abort all commands */
1541258223a3SMatthew Dillon 		goto err;
1542258223a3SMatthew Dillon 	}
1543258223a3SMatthew Dillon 
1544258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
1545258223a3SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0)
1546258223a3SMatthew Dillon 		goto err;
1547258223a3SMatthew Dillon 
1548258223a3SMatthew Dillon 	rc = 0;
1549258223a3SMatthew Dillon err:
1550258223a3SMatthew Dillon 	/* Abort our command, if it failed, by stopping command DMA. */
1551258223a3SMatthew Dillon 	if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) {
1552258223a3SMatthew Dillon 		kprintf("%s: log page read failed, slot %d was still active.\n",
1553258223a3SMatthew Dillon 			PORTNAME(ap), ccb->ccb_slot);
1554258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
1555258223a3SMatthew Dillon 	}
1556258223a3SMatthew Dillon 
1557258223a3SMatthew Dillon 	/* Done with the error CCB now. */
1558258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
1559258223a3SMatthew Dillon 	ahci_put_err_ccb(ccb);
1560258223a3SMatthew Dillon 
1561258223a3SMatthew Dillon 	/* Extract failed register set and tags from the scratch space. */
1562258223a3SMatthew Dillon 	if (rc == 0) {
1563258223a3SMatthew Dillon 		struct ata_log_page_10h		*log;
1564258223a3SMatthew Dillon 		int				err_slot;
1565258223a3SMatthew Dillon 
1566258223a3SMatthew Dillon 		log = (struct ata_log_page_10h *)ap->ap_err_scratch;
1567258223a3SMatthew Dillon 		if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
1568258223a3SMatthew Dillon 			/* Not queued bit was set - wasn't an NCQ error? */
1569258223a3SMatthew Dillon 			kprintf("%s: read NCQ error page, but not an NCQ "
1570258223a3SMatthew Dillon 				"error?\n",
1571258223a3SMatthew Dillon 				PORTNAME(ap));
1572258223a3SMatthew Dillon 			rc = ESRCH;
1573258223a3SMatthew Dillon 		} else {
1574258223a3SMatthew Dillon 			/* Copy back the log record as a D2H register FIS. */
1575258223a3SMatthew Dillon 			*err_slotp = err_slot = log->err_regs.type &
1576258223a3SMatthew Dillon 			    ATA_LOG_10H_TYPE_TAG_MASK;
1577258223a3SMatthew Dillon 
1578258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1579258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
1580258223a3SMatthew Dillon 			    sizeof(struct ata_fis_d2h));
1581258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
1582258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.flags = 0;
1583258223a3SMatthew Dillon 		}
1584258223a3SMatthew Dillon 	}
1585258223a3SMatthew Dillon 
1586258223a3SMatthew Dillon 	/* Restore saved CMD register state */
1587258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1588258223a3SMatthew Dillon 
1589258223a3SMatthew Dillon 	return (rc);
1590258223a3SMatthew Dillon }
1591258223a3SMatthew Dillon 
1592258223a3SMatthew Dillon /*
1593258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
1594258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
1595258223a3SMatthew Dillon  * single physical base address.
1596258223a3SMatthew Dillon  */
1597258223a3SMatthew Dillon struct ahci_dmamem *
1598258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
1599258223a3SMatthew Dillon {
1600258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
1601258223a3SMatthew Dillon 	int	error;
1602258223a3SMatthew Dillon 
1603258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
1604258223a3SMatthew Dillon 
1605258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
1606258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
1607258223a3SMatthew Dillon 	if (error == 0) {
1608258223a3SMatthew Dillon 		adm->adm_tag = tag;
1609258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
1610258223a3SMatthew Dillon 					adm->adm_kva,
1611258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
1612258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
1613258223a3SMatthew Dillon 					0);
1614258223a3SMatthew Dillon 	}
1615258223a3SMatthew Dillon 	if (error) {
1616258223a3SMatthew Dillon 		if (adm->adm_map) {
1617258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
1618258223a3SMatthew Dillon 			adm->adm_map = NULL;
1619258223a3SMatthew Dillon 			adm->adm_tag = NULL;
1620258223a3SMatthew Dillon 			adm->adm_kva = NULL;
1621258223a3SMatthew Dillon 		}
1622258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
1623258223a3SMatthew Dillon 		adm = NULL;
1624258223a3SMatthew Dillon 	}
1625258223a3SMatthew Dillon 	return (adm);
1626258223a3SMatthew Dillon }
1627258223a3SMatthew Dillon 
1628258223a3SMatthew Dillon static
1629258223a3SMatthew Dillon void
1630258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
1631258223a3SMatthew Dillon {
1632258223a3SMatthew Dillon 	KKASSERT(error == 0);
1633258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
1634258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
1635258223a3SMatthew Dillon }
1636258223a3SMatthew Dillon 
1637258223a3SMatthew Dillon 
1638258223a3SMatthew Dillon void
1639258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
1640258223a3SMatthew Dillon {
1641258223a3SMatthew Dillon 	if (adm->adm_map) {
1642258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
1643258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
1644258223a3SMatthew Dillon 		adm->adm_map = NULL;
1645258223a3SMatthew Dillon 		adm->adm_tag = NULL;
1646258223a3SMatthew Dillon 		adm->adm_kva = NULL;
1647258223a3SMatthew Dillon 	}
1648258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
1649258223a3SMatthew Dillon }
1650258223a3SMatthew Dillon 
1651258223a3SMatthew Dillon u_int32_t
1652258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
1653258223a3SMatthew Dillon {
1654258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1655258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
1656258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
1657258223a3SMatthew Dillon }
1658258223a3SMatthew Dillon 
1659258223a3SMatthew Dillon void
1660258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
1661258223a3SMatthew Dillon {
1662258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
1663258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1664258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
1665258223a3SMatthew Dillon }
1666258223a3SMatthew Dillon 
1667258223a3SMatthew Dillon int
1668258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
1669258223a3SMatthew Dillon 	     u_int32_t target)
1670258223a3SMatthew Dillon {
1671258223a3SMatthew Dillon 	int				i;
1672258223a3SMatthew Dillon 
1673258223a3SMatthew Dillon 	for (i = 0; i < 1000; i++) {
1674258223a3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
1675258223a3SMatthew Dillon 			return (0);
1676258223a3SMatthew Dillon 		DELAY(1000);
1677258223a3SMatthew Dillon 	}
1678258223a3SMatthew Dillon 
1679258223a3SMatthew Dillon 	return (1);
1680258223a3SMatthew Dillon }
1681258223a3SMatthew Dillon 
1682258223a3SMatthew Dillon u_int32_t
1683258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
1684258223a3SMatthew Dillon {
1685258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
1686258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
1687258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
1688258223a3SMatthew Dillon }
1689258223a3SMatthew Dillon 
1690258223a3SMatthew Dillon void
1691258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
1692258223a3SMatthew Dillon {
1693258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
1694258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
1695258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
1696258223a3SMatthew Dillon }
1697258223a3SMatthew Dillon 
1698258223a3SMatthew Dillon int
1699*cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
1700*cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
1701258223a3SMatthew Dillon {
1702258223a3SMatthew Dillon 	int				i;
1703258223a3SMatthew Dillon 
1704*cec85a37SMatthew Dillon 	for (i = 0; i < timeout; i++) {
1705258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
1706258223a3SMatthew Dillon 			return (0);
1707258223a3SMatthew Dillon 		DELAY(1000);
1708258223a3SMatthew Dillon 	}
1709258223a3SMatthew Dillon 
1710258223a3SMatthew Dillon 	return (1);
1711258223a3SMatthew Dillon }
1712258223a3SMatthew Dillon 
1713258223a3SMatthew Dillon struct ata_xfer *
1714258223a3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap)
1715258223a3SMatthew Dillon {
1716258223a3SMatthew Dillon 	/*struct ahci_softc	*sc = ap->ap_sc;*/
1717258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
1718258223a3SMatthew Dillon 
1719258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
1720258223a3SMatthew Dillon 	if (ccb == NULL) {
1721258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
1722258223a3SMatthew Dillon 		    PORTNAME(ap));
1723258223a3SMatthew Dillon 		return (NULL);
1724258223a3SMatthew Dillon 	}
1725258223a3SMatthew Dillon 
1726258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
1727258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
1728258223a3SMatthew Dillon 
1729258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
1730258223a3SMatthew Dillon 
1731258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
1732258223a3SMatthew Dillon }
1733258223a3SMatthew Dillon 
1734258223a3SMatthew Dillon void
1735258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
1736258223a3SMatthew Dillon {
1737258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
1738258223a3SMatthew Dillon 
1739258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
1740258223a3SMatthew Dillon 
1741258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
1742258223a3SMatthew Dillon }
1743258223a3SMatthew Dillon 
1744258223a3SMatthew Dillon int
1745258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
1746258223a3SMatthew Dillon {
1747258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
1748258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
1749258223a3SMatthew Dillon 
1750258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
1751258223a3SMatthew Dillon 
17524444122dSMatthew Dillon #if 0
17534444122dSMatthew Dillon 	kprintf("ahci_ata_cmd xa->flags %08x type %08x cmd=%08x\n",
17544444122dSMatthew Dillon 		xa->flags,
17554444122dSMatthew Dillon 		xa->fis->type,
17564444122dSMatthew Dillon 		xa->fis->command);
17574444122dSMatthew Dillon #endif
17584444122dSMatthew Dillon 
1759258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
1760258223a3SMatthew Dillon 		goto failcmd;
1761258223a3SMatthew Dillon 
1762258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
1763258223a3SMatthew Dillon 
1764258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1765258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
1766258223a3SMatthew Dillon 
1767258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
1768258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
1769258223a3SMatthew Dillon 
1770258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
1771258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
1772258223a3SMatthew Dillon 
1773258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
1774258223a3SMatthew Dillon 		goto failcmd;
1775258223a3SMatthew Dillon 
1776258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
1777258223a3SMatthew Dillon 
1778258223a3SMatthew Dillon 	if (xa->flags & ATA_F_POLL) {
1779258223a3SMatthew Dillon 		ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout);
1780258223a3SMatthew Dillon 		return (ATA_COMPLETE);
1781258223a3SMatthew Dillon 	}
1782258223a3SMatthew Dillon 
1783258223a3SMatthew Dillon 	crit_enter();
1784258223a3SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_RUNNING;
1785258223a3SMatthew Dillon 	callout_reset(&ccb->ccb_timeout, xa->timeout,
1786258223a3SMatthew Dillon 		      ahci_ata_cmd_timeout_unserialized, ccb);
1787258223a3SMatthew Dillon 	ahci_start(ccb);
1788258223a3SMatthew Dillon 	crit_exit();
1789258223a3SMatthew Dillon 	return (ATA_QUEUED);
1790258223a3SMatthew Dillon 
1791258223a3SMatthew Dillon failcmd:
1792258223a3SMatthew Dillon 	crit_enter();
1793258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
1794258223a3SMatthew Dillon 	xa->complete(xa);
1795258223a3SMatthew Dillon 	crit_exit();
1796258223a3SMatthew Dillon 	return (ATA_ERROR);
1797258223a3SMatthew Dillon }
1798258223a3SMatthew Dillon 
1799258223a3SMatthew Dillon void
1800258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
1801258223a3SMatthew Dillon {
1802258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1803258223a3SMatthew Dillon 
1804258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
1805258223a3SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
1806258223a3SMatthew Dillon 		callout_stop(&ccb->ccb_timeout);
1807258223a3SMatthew Dillon 	}
1808258223a3SMatthew Dillon 
1809258223a3SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR)
1810258223a3SMatthew Dillon 		ahci_issue_pending_commands(ccb->ccb_port,
1811258223a3SMatthew Dillon 		    xa->flags & ATA_F_NCQ);
1812258223a3SMatthew Dillon 
1813258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
1814258223a3SMatthew Dillon 
1815258223a3SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP)
1816258223a3SMatthew Dillon 		xa->state = ATA_S_COMPLETE;
1817258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1818258223a3SMatthew Dillon 	else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
1819258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
1820258223a3SMatthew Dillon 			"slot %d\n",
1821258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
1822258223a3SMatthew Dillon #endif
1823258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
1824258223a3SMatthew Dillon 		xa->complete(xa);
1825258223a3SMatthew Dillon }
1826258223a3SMatthew Dillon 
1827258223a3SMatthew Dillon static void
1828258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
1829258223a3SMatthew Dillon {
1830258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
1831258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
1832258223a3SMatthew Dillon 
1833258223a3SMatthew Dillon 	lwkt_serialize_enter(&ap->ap_sc->sc_serializer);
1834258223a3SMatthew Dillon 	ahci_ata_cmd_timeout(arg);
1835258223a3SMatthew Dillon 	lwkt_serialize_exit(&ap->ap_sc->sc_serializer);
1836258223a3SMatthew Dillon }
1837258223a3SMatthew Dillon 
1838258223a3SMatthew Dillon static void
1839258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg)
1840258223a3SMatthew Dillon {
1841258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
1842258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
1843258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
1844258223a3SMatthew Dillon 	volatile u_int32_t	*active;
1845258223a3SMatthew Dillon 	int			ccb_was_started, ncq_cmd;
1846258223a3SMatthew Dillon 
1847258223a3SMatthew Dillon 	crit_enter();
1848258223a3SMatthew Dillon 	kprintf("CMD TIMEOUT port-cmd-reg 0x%b\n"
1849258223a3SMatthew Dillon 		"\tactive=%08x sactive=%08x\n"
1850258223a3SMatthew Dillon 		"\t  sact=%08x      ci=%08x\n",
1851258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
1852258223a3SMatthew Dillon 		ap->ap_active, ap->ap_sactive,
1853258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
1854258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI));
1855258223a3SMatthew Dillon 
1856258223a3SMatthew Dillon 	KKASSERT(xa->flags & ATA_F_TIMEOUT_RUNNING);
1857258223a3SMatthew Dillon 	xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
1858258223a3SMatthew Dillon 	ncq_cmd = (xa->flags & ATA_F_NCQ);
1859258223a3SMatthew Dillon 	active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
1860258223a3SMatthew Dillon 
1861258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
1862258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
1863258223a3SMatthew Dillon 		    "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
1864258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1865258223a3SMatthew Dillon 		ccb_was_started = 0;
1866258223a3SMatthew Dillon 	} else if (ccb->ccb_xa.state == ATA_S_ONCHIP && ahci_port_intr(ap,
1867258223a3SMatthew Dillon 	    1 << ccb->ccb_slot)) {
1868258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: final poll of port completed "
1869258223a3SMatthew Dillon 		    "command in slot %d\n", PORTNAME(ap), ccb->ccb_slot);
1870258223a3SMatthew Dillon 		goto ret;
1871258223a3SMatthew Dillon 	} else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
1872258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
1873258223a3SMatthew Dillon 		    "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
1874258223a3SMatthew Dillon 		    (*active & (1 << ccb->ccb_slot)) ?
1875258223a3SMatthew Dillon 		    " but slot is still active?" : ".");
1876258223a3SMatthew Dillon 		goto ret;
1877258223a3SMatthew Dillon 	} else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
1878258223a3SMatthew Dillon 		    (1 << ccb->ccb_slot)) == 0 &&
1879258223a3SMatthew Dillon 		   (*active & (1 << ccb->ccb_slot))) {
1880258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but "
1881258223a3SMatthew Dillon 		    "IRQ handler didn't detect it.  Why?\n", PORTNAME(ap),
1882258223a3SMatthew Dillon 		    ccb->ccb_slot);
1883258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
1884258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
1885258223a3SMatthew Dillon 		goto ret;
1886258223a3SMatthew Dillon 	} else {
1887258223a3SMatthew Dillon 		ccb_was_started = 1;
1888258223a3SMatthew Dillon 	}
1889258223a3SMatthew Dillon 
1890258223a3SMatthew Dillon 	/* Complete the slot with a timeout error. */
1891258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_TIMEOUT;
1892258223a3SMatthew Dillon 	*active &= ~(1 << ccb->ccb_slot);
1893258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
1894258223a3SMatthew Dillon 	ccb->ccb_done(ccb);	/* This won't issue pending commands or run the
1895258223a3SMatthew Dillon 				   atascsi completion. */
1896258223a3SMatthew Dillon 
1897258223a3SMatthew Dillon 	/* Reset port to abort running command. */
1898258223a3SMatthew Dillon 	if (ccb_was_started) {
1899258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
1900258223a3SMatthew Dillon 		    "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
1901258223a3SMatthew Dillon 		    : "", ccb->ccb_slot, *active);
1902258223a3SMatthew Dillon 		if (ahci_port_softreset(ap) != 0 && ahci_port_portreset(ap)
1903258223a3SMatthew Dillon 		    != 0) {
1904258223a3SMatthew Dillon 			kprintf("%s: failed to reset port during timeout "
1905258223a3SMatthew Dillon 				"handling, disabling it\n",
1906258223a3SMatthew Dillon 				PORTNAME(ap));
1907258223a3SMatthew Dillon 			ap->ap_state = AP_S_FATAL_ERROR;
1908258223a3SMatthew Dillon 		}
1909258223a3SMatthew Dillon 
1910258223a3SMatthew Dillon 		/* Restart any other commands that were aborted by the reset. */
1911258223a3SMatthew Dillon 		if (*active) {
1912258223a3SMatthew Dillon 			DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
1913258223a3SMatthew Dillon 			    "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
1914258223a3SMatthew Dillon 			    *active);
1915258223a3SMatthew Dillon 			if (ncq_cmd)
1916258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, *active);
1917258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, *active);
1918258223a3SMatthew Dillon 		}
1919258223a3SMatthew Dillon 	}
1920258223a3SMatthew Dillon 
1921258223a3SMatthew Dillon 	/* Issue any pending commands now. */
1922258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
1923258223a3SMatthew Dillon 	if (ccb_was_started)
1924258223a3SMatthew Dillon 		ahci_issue_pending_commands(ap, ncq_cmd);
1925258223a3SMatthew Dillon 	else if (ap->ap_active == 0)
1926258223a3SMatthew Dillon 		ahci_issue_pending_ncq_commands(ap);
1927258223a3SMatthew Dillon 
1928258223a3SMatthew Dillon 	/* Complete the timed out ata_xfer I/O (may generate new I/O). */
1929258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
1930258223a3SMatthew Dillon 	xa->complete(xa);
1931258223a3SMatthew Dillon 
1932258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
1933258223a3SMatthew Dillon ret:
1934258223a3SMatthew Dillon 	crit_exit();
1935258223a3SMatthew Dillon }
1936258223a3SMatthew Dillon 
1937258223a3SMatthew Dillon void
1938258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
1939258223a3SMatthew Dillon {
1940258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_COMPLETE;
1941258223a3SMatthew Dillon }
1942