xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision 17eab71eccd5f3dda84b5d70ba6e7156732915c1)
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);
55*17eab71eSMatthew Dillon int	ahci_port_start(struct ahci_port *);
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 *);
66*17eab71eSMatthew Dillon int	ahci_port_softreset(struct ahci_port *ap);
67*17eab71eSMatthew Dillon int	ahci_port_hardreset(struct ahci_port *ap);
68258223a3SMatthew Dillon 
69258223a3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(void *arg);
70258223a3SMatthew Dillon static void ahci_ata_cmd_timeout(void *arg);
71258223a3SMatthew Dillon 
72258223a3SMatthew Dillon void	ahci_issue_pending_ncq_commands(struct ahci_port *);
73258223a3SMatthew Dillon void	ahci_issue_pending_commands(struct ahci_port *, int);
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 */
91cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \
92cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
93cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to,  _r, _b) \
94cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
95258223a3SMatthew Dillon 
96258223a3SMatthew Dillon /* Wait for all bits in _b to be set */
97cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \
98cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
99cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \
100cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
101cec85a37SMatthew Dillon 
102cec85a37SMatthew 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 
122*17eab71eSMatthew Dillon 	/*
123*17eab71eSMatthew Dillon 	 * Unconditionally reset the controller, do not conditionalize on
124*17eab71eSMatthew Dillon 	 * trying to figure it if it was previously active or not.
125*17eab71eSMatthew Dillon 	 */
126258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR);
127258223a3SMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR,
128258223a3SMatthew Dillon 	    AHCI_REG_GHC_HR) != 0) {
129258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
130258223a3SMatthew Dillon 			      "unable to reset controller\n");
131258223a3SMatthew Dillon 		return (1);
132258223a3SMatthew Dillon 	}
133258223a3SMatthew Dillon 
134258223a3SMatthew Dillon 	/* enable ahci (global interrupts disabled) */
135258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
136258223a3SMatthew Dillon 
137258223a3SMatthew Dillon 	/* restore parameters */
138258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_CAP, cap);
139258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_PI, pi);
140258223a3SMatthew Dillon 
141258223a3SMatthew Dillon 	return (0);
142258223a3SMatthew Dillon }
143258223a3SMatthew Dillon 
144fd8bd957SMatthew Dillon /*
145fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
146fd8bd957SMatthew Dillon  */
147258223a3SMatthew Dillon int
148258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port)
149258223a3SMatthew Dillon {
150258223a3SMatthew Dillon 	struct ahci_port		*ap;
151258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
152258223a3SMatthew Dillon 	u_int64_t			dva;
153258223a3SMatthew Dillon 	u_int32_t			cmd;
154258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*hdr;
155258223a3SMatthew Dillon 	struct ahci_cmd_table		*table;
156258223a3SMatthew Dillon 	int	rc = ENOMEM;
157258223a3SMatthew Dillon 	int	error;
158258223a3SMatthew Dillon 	int	i;
159258223a3SMatthew Dillon 
160258223a3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
161258223a3SMatthew Dillon 	if (ap == NULL) {
162258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
163258223a3SMatthew Dillon 			      "unable to allocate memory for port %d\n",
164258223a3SMatthew Dillon 			      port);
165258223a3SMatthew Dillon 		goto reterr;
166258223a3SMatthew Dillon 	}
167258223a3SMatthew Dillon 
168258223a3SMatthew Dillon 	ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
169258223a3SMatthew Dillon 		  device_get_name(sc->sc_dev),
170258223a3SMatthew Dillon 		  device_get_unit(sc->sc_dev),
171258223a3SMatthew Dillon 		  port);
172258223a3SMatthew Dillon 	sc->sc_ports[port] = ap;
173258223a3SMatthew Dillon 
174258223a3SMatthew Dillon 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
175258223a3SMatthew Dillon 	    AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
176258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
177258223a3SMatthew Dillon 			      "unable to create register window for port %d\n",
178258223a3SMatthew Dillon 			      port);
179258223a3SMatthew Dillon 		goto freeport;
180258223a3SMatthew Dillon 	}
181258223a3SMatthew Dillon 
182258223a3SMatthew Dillon 	ap->ap_sc = sc;
183258223a3SMatthew Dillon 	ap->ap_num = port;
184258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_free);
185258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_pending);
186258223a3SMatthew Dillon 	lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
187258223a3SMatthew Dillon 
188258223a3SMatthew Dillon 	/* Disable port interrupts */
189258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
190258223a3SMatthew Dillon 
191*17eab71eSMatthew Dillon 	/*
192*17eab71eSMatthew Dillon 	 * Sec 10.1.2 - deinitialise port if it is already running
193*17eab71eSMatthew Dillon 	 */
194258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
195258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
196258223a3SMatthew Dillon 		    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
197258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
198258223a3SMatthew Dillon 		int r;
199258223a3SMatthew Dillon 
200258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
201258223a3SMatthew Dillon 		if (r) {
202258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
203258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
204258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
205258223a3SMatthew Dillon 			rc = ENXIO;
206258223a3SMatthew Dillon 			goto freeport;
207258223a3SMatthew Dillon 		}
208258223a3SMatthew Dillon 
209258223a3SMatthew Dillon 		/* Write DET to zero */
210258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, 0);
211258223a3SMatthew Dillon 	}
212258223a3SMatthew Dillon 
213258223a3SMatthew Dillon 	/* Allocate RFIS */
214258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
215258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
216258223a3SMatthew Dillon 		kprintf("NORFIS\n");
217258223a3SMatthew Dillon 		goto nomem;
218258223a3SMatthew Dillon 	}
219258223a3SMatthew Dillon 
220258223a3SMatthew Dillon 	/* Setup RFIS base address */
221258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
222258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
223258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
224258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
225258223a3SMatthew Dillon 
226258223a3SMatthew Dillon 	/* Enable FIS reception and activate port. */
227258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
228258223a3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
229258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
230258223a3SMatthew Dillon 
231258223a3SMatthew Dillon 	/* Check whether port activated.  Skip it if not. */
232258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
233258223a3SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
234258223a3SMatthew Dillon 		kprintf("NOT-ACTIVATED\n");
235258223a3SMatthew Dillon 		rc = ENXIO;
236258223a3SMatthew Dillon 		goto freeport;
237258223a3SMatthew Dillon 	}
238258223a3SMatthew Dillon 
239258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
240258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
241258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
242258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
243258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
244258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
245258223a3SMatthew Dillon 			      port);
246258223a3SMatthew Dillon 		goto freeport;
247258223a3SMatthew Dillon 	}
248258223a3SMatthew Dillon 
249258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
250258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
251258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
252258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
253258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
254258223a3SMatthew Dillon nomem:
255258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
256258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
257258223a3SMatthew Dillon 			      port);
258258223a3SMatthew Dillon 		goto freeport;
259258223a3SMatthew Dillon 	}
260258223a3SMatthew Dillon 
261258223a3SMatthew Dillon 	/* Setup command list base address */
262258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
263258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
264258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
265258223a3SMatthew Dillon 
266258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
267258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
268258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
269258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
270258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
271258223a3SMatthew Dillon 
272258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
273258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
274258223a3SMatthew Dillon 		if (error) {
275258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
276258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
277258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
278258223a3SMatthew Dillon 			goto freeport;
279258223a3SMatthew Dillon 		}
280258223a3SMatthew Dillon 
281258223a3SMatthew Dillon 		callout_init(&ccb->ccb_timeout);
282258223a3SMatthew Dillon 		ccb->ccb_slot = i;
283258223a3SMatthew Dillon 		ccb->ccb_port = ap;
284258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
285258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
286258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
287258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
288258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
289258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
290258223a3SMatthew Dillon 
291258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
292258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
293258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
294258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
295258223a3SMatthew Dillon 
296258223a3SMatthew Dillon 		ccb->ccb_xa.ata_put_xfer = ahci_ata_put_xfer;
297258223a3SMatthew Dillon 
298258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
299258223a3SMatthew Dillon 		ahci_put_ccb(ccb);
300258223a3SMatthew Dillon 	}
301258223a3SMatthew Dillon 
302258223a3SMatthew Dillon 	/* Wait for ICC change to complete */
303258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
304258223a3SMatthew Dillon 
305fd8bd957SMatthew Dillon 	/*
306fd8bd957SMatthew Dillon 	 * Do device-related port initialization.  A failure here does not
307fd8bd957SMatthew Dillon 	 * cause the port to be deallocated as we want to receive future
308fd8bd957SMatthew Dillon 	 * hot-plug events.
309fd8bd957SMatthew Dillon 	 */
310fd8bd957SMatthew Dillon 	ahci_port_init(ap);
311fd8bd957SMatthew Dillon 	return(0);
312fd8bd957SMatthew Dillon freeport:
313fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
314fd8bd957SMatthew Dillon reterr:
315fd8bd957SMatthew Dillon 	return (rc);
316fd8bd957SMatthew Dillon }
317fd8bd957SMatthew Dillon 
318fd8bd957SMatthew Dillon /*
319fd8bd957SMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.
320fd8bd957SMatthew Dillon  *
321fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
322fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
323fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
324fd8bd957SMatthew Dillon  *
325fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
326fd8bd957SMatthew Dillon  */
327fd8bd957SMatthew Dillon int
328fd8bd957SMatthew Dillon ahci_port_init(struct ahci_port *ap)
329fd8bd957SMatthew Dillon {
330fd8bd957SMatthew Dillon 	int rc;
331fd8bd957SMatthew Dillon 
332fd8bd957SMatthew Dillon 	/*
333fd8bd957SMatthew Dillon 	 * Hard-reset the port.
334fd8bd957SMatthew Dillon 	 */
335*17eab71eSMatthew Dillon 	if ((rc = ahci_port_reset(ap, 1)) != 0) {
336*17eab71eSMatthew Dillon 		rc = ahci_port_reset(ap, 1);
337*17eab71eSMatthew Dillon 		if (rc == 0) {
338*17eab71eSMatthew Dillon 			kprintf("%s: Device successfully reset on second try\n",
339*17eab71eSMatthew Dillon 				PORTNAME(ap));
340*17eab71eSMatthew Dillon 		}
341*17eab71eSMatthew Dillon 	}
342fd8bd957SMatthew Dillon 
343258223a3SMatthew Dillon 	switch (rc) {
344258223a3SMatthew Dillon 	case ENODEV:
345fd8bd957SMatthew Dillon 		/*
346fd8bd957SMatthew Dillon 		 * We had problems talking to the device on the port.
347fd8bd957SMatthew Dillon 		 */
348258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
349258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
350419cb1abSMatthew Dillon 			kprintf("%s: Device not communicating\n", PORTNAME(ap));
351258223a3SMatthew Dillon 			break;
352258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
353419cb1abSMatthew Dillon 			kprintf("%s: PHY offline\n", PORTNAME(ap));
354258223a3SMatthew Dillon 			break;
355258223a3SMatthew Dillon 		default:
356419cb1abSMatthew Dillon 			kprintf("%s: No device detected\n", PORTNAME(ap));
357258223a3SMatthew Dillon 			break;
358258223a3SMatthew Dillon 		}
359258223a3SMatthew Dillon 		break;
360258223a3SMatthew Dillon 
361258223a3SMatthew Dillon 	case EBUSY:
362fd8bd957SMatthew Dillon 		/*
363*17eab71eSMatthew Dillon 		 * The device on the port is still telling us its busy,
364*17eab71eSMatthew Dillon 		 * which means that it is not properly handling a SATA
365*17eab71eSMatthew Dillon 		 * port COMRESET.
366fd8bd957SMatthew Dillon 		 *
367*17eab71eSMatthew Dillon 		 * It may be possible to softreset the device using CLO
368*17eab71eSMatthew Dillon 		 * and a device reset command.
369fd8bd957SMatthew Dillon 		 */
370*17eab71eSMatthew Dillon 		kprintf("%s: Device on port is bricked, trying softreset\n",
371*17eab71eSMatthew Dillon 			PORTNAME(ap));
372258223a3SMatthew Dillon 
373*17eab71eSMatthew Dillon 		rc = ahci_port_reset(ap, 0);
374258223a3SMatthew Dillon 		if (rc) {
375*17eab71eSMatthew Dillon 			kprintf("%s: Unable unbrick device\n",
376fd8bd957SMatthew Dillon 				PORTNAME(ap));
377fd8bd957SMatthew Dillon 		} else {
378*17eab71eSMatthew Dillon 			kprintf("%s: Successfully unbricked\n",
379fd8bd957SMatthew Dillon 				PORTNAME(ap));
380258223a3SMatthew Dillon 		}
381258223a3SMatthew Dillon 		break;
382258223a3SMatthew Dillon 
383258223a3SMatthew Dillon 	default:
384258223a3SMatthew Dillon 		break;
385258223a3SMatthew Dillon 	}
386258223a3SMatthew Dillon 
387258223a3SMatthew Dillon 	/*
388*17eab71eSMatthew Dillon 	 * Command transfers can only be enabled if a device was successfully
389*17eab71eSMatthew Dillon 	 * detected.
390258223a3SMatthew Dillon 	 */
391258223a3SMatthew Dillon 	if (rc == 0) {
392*17eab71eSMatthew Dillon 		if (ahci_port_start(ap)) {
393fd8bd957SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
394fd8bd957SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
395258223a3SMatthew Dillon 			rc = ENXIO;	/* couldn't start port */
396258223a3SMatthew Dillon 		}
397258223a3SMatthew Dillon 	}
398258223a3SMatthew Dillon 
399*17eab71eSMatthew Dillon 	/*
400*17eab71eSMatthew Dillon 	 * Flush and enable interrupts on the port whether a device is
401*17eab71eSMatthew Dillon 	 * sitting on it or not, to handle hot-plug events.
402*17eab71eSMatthew Dillon 	 */
403258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
404fd8bd957SMatthew Dillon 	ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num);
405258223a3SMatthew Dillon 
406258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE,
407258223a3SMatthew Dillon 			AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
408258223a3SMatthew Dillon 			AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
409258223a3SMatthew Dillon 			AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
410258223a3SMatthew Dillon 			AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
411258223a3SMatthew Dillon #ifdef AHCI_COALESCE
412258223a3SMatthew Dillon 	    ((sc->sc_ccc_ports & (1 << port)) ?
413258223a3SMatthew Dillon 			0 : (AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE))
414258223a3SMatthew Dillon #else
415258223a3SMatthew Dillon 			AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE
416258223a3SMatthew Dillon #endif
417258223a3SMatthew Dillon 	);
418258223a3SMatthew Dillon 	return(rc);
419258223a3SMatthew Dillon }
420258223a3SMatthew Dillon 
421fd8bd957SMatthew Dillon /*
422fd8bd957SMatthew Dillon  * De-initialize and detach a port.
423fd8bd957SMatthew Dillon  */
424258223a3SMatthew Dillon void
425258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
426258223a3SMatthew Dillon {
427258223a3SMatthew Dillon 	struct ahci_port		*ap = sc->sc_ports[port];
428258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
429258223a3SMatthew Dillon 
430*17eab71eSMatthew Dillon 	/*
431*17eab71eSMatthew Dillon 	 * Ensure port is disabled and its interrupts are all flushed.
432*17eab71eSMatthew Dillon 	 */
433258223a3SMatthew Dillon 	if (ap->ap_sc) {
434*17eab71eSMatthew Dillon 		ahci_port_stop(ap, 1);
435258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
436258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
437258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
438258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
439258223a3SMatthew Dillon 	}
440258223a3SMatthew Dillon 
441258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
442258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
443258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
444258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
445258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
446258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
447258223a3SMatthew Dillon 			}
448258223a3SMatthew Dillon 		}
449258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
450258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
451258223a3SMatthew Dillon 	}
452258223a3SMatthew Dillon 
453258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
454258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
455258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
456258223a3SMatthew Dillon 	}
457258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
458258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
459258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
460258223a3SMatthew Dillon 	}
461258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
462258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
463258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
464258223a3SMatthew Dillon 	}
465258223a3SMatthew Dillon 
466258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
467258223a3SMatthew Dillon 
468258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
469258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
470258223a3SMatthew Dillon }
471258223a3SMatthew Dillon 
472fd8bd957SMatthew Dillon /*
473fd8bd957SMatthew Dillon  * Start high-level command processing on the port
474fd8bd957SMatthew Dillon  */
475258223a3SMatthew Dillon int
476*17eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap)
477258223a3SMatthew Dillon {
478258223a3SMatthew Dillon 	u_int32_t			r;
479258223a3SMatthew Dillon 
480*17eab71eSMatthew Dillon 	/*
481*17eab71eSMatthew Dillon 	 * FRE must be turned on before ST.  Wait for FR to go active
482*17eab71eSMatthew Dillon 	 * before turning on ST.  The spec doesn't seem to think this
483*17eab71eSMatthew Dillon 	 * is necessary but waiting here avoids an on-off race in the
484*17eab71eSMatthew Dillon 	 * ahci_port_stop() code.
485*17eab71eSMatthew Dillon 	 */
486258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
487*17eab71eSMatthew Dillon 	if ((r & AHCI_PREG_CMD_FRE) == 0) {
488258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_FRE;
489*17eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
490*17eab71eSMatthew Dillon 	}
491*17eab71eSMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
492*17eab71eSMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
493*17eab71eSMatthew Dillon 			kprintf("%s: Cannot start FIS reception\n",
494*17eab71eSMatthew Dillon 				PORTNAME(ap));
495*17eab71eSMatthew Dillon 			return (2);
496*17eab71eSMatthew Dillon 		}
497*17eab71eSMatthew Dillon 	}
498*17eab71eSMatthew Dillon 
499*17eab71eSMatthew Dillon 	/*
500*17eab71eSMatthew Dillon 	 * Turn on ST, wait for CR to come up.
501*17eab71eSMatthew Dillon 	 */
502258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_ST;
503258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
504*17eab71eSMatthew Dillon 	if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
505*17eab71eSMatthew Dillon 		kprintf("%s: Cannot start command DMA\n",
506*17eab71eSMatthew Dillon 			PORTNAME(ap));
507*17eab71eSMatthew Dillon 		return (1);
508*17eab71eSMatthew Dillon 	}
509258223a3SMatthew Dillon 
510258223a3SMatthew Dillon #ifdef AHCI_COALESCE
511*17eab71eSMatthew Dillon 	/*
512*17eab71eSMatthew Dillon 	 * (Re-)enable coalescing on the port.
513*17eab71eSMatthew Dillon 	 */
514258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
515258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
516258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
517258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
518258223a3SMatthew Dillon 	}
519258223a3SMatthew Dillon #endif
520258223a3SMatthew Dillon 
521258223a3SMatthew Dillon 	return (0);
522258223a3SMatthew Dillon }
523258223a3SMatthew Dillon 
524fd8bd957SMatthew Dillon /*
525fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
526fd8bd957SMatthew Dillon  */
527258223a3SMatthew Dillon int
528258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
529258223a3SMatthew Dillon {
530258223a3SMatthew Dillon 	u_int32_t			r;
531258223a3SMatthew Dillon 
532258223a3SMatthew Dillon #ifdef AHCI_COALESCE
533*17eab71eSMatthew Dillon 	/*
534*17eab71eSMatthew Dillon 	 * Disable coalescing on the port while it is stopped.
535*17eab71eSMatthew Dillon 	 */
536258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
537258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
538258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
539258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
540258223a3SMatthew Dillon 	}
541258223a3SMatthew Dillon #endif
542258223a3SMatthew Dillon 
543*17eab71eSMatthew Dillon 	/*
544*17eab71eSMatthew Dillon 	 * Turn off ST, then wait for CR to go off.
545*17eab71eSMatthew Dillon 	 */
546258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
547258223a3SMatthew Dillon 	r &= ~AHCI_PREG_CMD_ST;
548258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
549258223a3SMatthew Dillon 
550*17eab71eSMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
551*17eab71eSMatthew Dillon 		kprintf("%s: Port bricked, unable to stop (ST)\n",
552*17eab71eSMatthew Dillon 			PORTNAME(ap));
553258223a3SMatthew Dillon 		return (1);
554*17eab71eSMatthew Dillon 	}
555258223a3SMatthew Dillon 
556*17eab71eSMatthew Dillon 	/*
557*17eab71eSMatthew Dillon 	 * Turn off FRE, then wait for FR to go off.  FRE cannot
558*17eab71eSMatthew Dillon 	 * be turned off until CR transitions to 0.
559*17eab71eSMatthew Dillon 	 */
560*17eab71eSMatthew Dillon 	if (stop_fis_rx) {
561*17eab71eSMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
562*17eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
563*17eab71eSMatthew Dillon 		if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
564*17eab71eSMatthew Dillon 			kprintf("%s: Port bricked, unable to stop (FRE)\n",
565*17eab71eSMatthew Dillon 				PORTNAME(ap));
566258223a3SMatthew Dillon 			return (2);
567*17eab71eSMatthew Dillon 		}
568*17eab71eSMatthew Dillon 	}
569258223a3SMatthew Dillon 
570258223a3SMatthew Dillon 	return (0);
571258223a3SMatthew Dillon }
572258223a3SMatthew Dillon 
573fd8bd957SMatthew Dillon /*
574fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
575fd8bd957SMatthew Dillon  */
576258223a3SMatthew Dillon int
577258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
578258223a3SMatthew Dillon {
579258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
580258223a3SMatthew Dillon 	u_int32_t			cmd;
581258223a3SMatthew Dillon 
582258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
583258223a3SMatthew Dillon 	if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
584258223a3SMatthew Dillon 		return (1);
585258223a3SMatthew Dillon 
586258223a3SMatthew Dillon 	/* Issue CLO */
587258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
588258223a3SMatthew Dillon #ifdef DIAGNOSTIC
589258223a3SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_ST) {
590258223a3SMatthew Dillon 		kprintf("%s: CLO requested while port running\n",
591258223a3SMatthew Dillon 			PORTNAME(ap));
592258223a3SMatthew Dillon 	}
593258223a3SMatthew Dillon #endif
594258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
595258223a3SMatthew Dillon 
596258223a3SMatthew Dillon 	/* Wait for completion */
597258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
598258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
599258223a3SMatthew Dillon 		return (1);
600258223a3SMatthew Dillon 	}
601258223a3SMatthew Dillon 
602258223a3SMatthew Dillon 	return (0);
603258223a3SMatthew Dillon }
604258223a3SMatthew Dillon 
605fd8bd957SMatthew Dillon /*
606*17eab71eSMatthew Dillon  * If hard is 0 perform a softreset of the port and if that fails
607*17eab71eSMatthew Dillon  * fall through and issue a hard reset.
608*17eab71eSMatthew Dillon  *
609*17eab71eSMatthew Dillon  * If hard is 1 perform a hardreset of the port.
610*17eab71eSMatthew Dillon  */
611*17eab71eSMatthew Dillon int
612*17eab71eSMatthew Dillon ahci_port_reset(struct ahci_port *ap, int hard)
613*17eab71eSMatthew Dillon {
614*17eab71eSMatthew Dillon 	int rc;
615*17eab71eSMatthew Dillon 
616*17eab71eSMatthew Dillon 	if (hard) {
617*17eab71eSMatthew Dillon 		rc = ahci_port_hardreset(ap);
618*17eab71eSMatthew Dillon 	} else {
619*17eab71eSMatthew Dillon 		rc = ahci_port_softreset(ap);
620*17eab71eSMatthew Dillon 		if (rc)
621*17eab71eSMatthew Dillon 			rc = ahci_port_hardreset(ap);
622*17eab71eSMatthew Dillon 	}
623*17eab71eSMatthew Dillon 	return(rc);
624*17eab71eSMatthew Dillon }
625*17eab71eSMatthew Dillon 
626*17eab71eSMatthew Dillon /*
627fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
628fd8bd957SMatthew Dillon  *
629fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
630fd8bd957SMatthew Dillon  * a reset to the connected device.
631fd8bd957SMatthew Dillon  */
632258223a3SMatthew Dillon int
633258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
634258223a3SMatthew Dillon {
635258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = NULL;
636258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
637258223a3SMatthew Dillon 	u_int8_t			*fis;
638258223a3SMatthew Dillon 	int				rc = EIO;
639258223a3SMatthew Dillon 	u_int32_t			cmd;
640258223a3SMatthew Dillon 
641258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
642258223a3SMatthew Dillon 
643258223a3SMatthew Dillon 	crit_enter();
644258223a3SMatthew Dillon 
645258223a3SMatthew Dillon 	/* Save previous command register state */
646258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
647258223a3SMatthew Dillon 
648258223a3SMatthew Dillon 	/* Idle port */
649258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
650258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
651258223a3SMatthew Dillon 			PORTNAME(ap));
652258223a3SMatthew Dillon 		goto err;
653258223a3SMatthew Dillon 	}
654258223a3SMatthew Dillon 
655258223a3SMatthew Dillon 	/* Request CLO if device appears hung */
656258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
657258223a3SMatthew Dillon 	     (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
658258223a3SMatthew Dillon 		ahci_port_clo(ap);
659258223a3SMatthew Dillon 	}
660258223a3SMatthew Dillon 
661258223a3SMatthew Dillon 	/* Clear port errors to permit TFD transfer */
662258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR));
663258223a3SMatthew Dillon 
664258223a3SMatthew Dillon 	/* Restart port */
665*17eab71eSMatthew Dillon 	if (ahci_port_start(ap)) {
666258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
667258223a3SMatthew Dillon 		        PORTNAME(ap));
668258223a3SMatthew Dillon 		goto err;
669258223a3SMatthew Dillon 	}
670258223a3SMatthew Dillon 
671258223a3SMatthew Dillon 	/* Check whether CLO worked */
672258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
673258223a3SMatthew Dillon 	    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
674258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
675258223a3SMatthew Dillon 			PORTNAME(ap),
676258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
677258223a3SMatthew Dillon 			? "failed" : "unsupported");
678258223a3SMatthew Dillon 		rc = EBUSY;
679258223a3SMatthew Dillon 		goto err;
680258223a3SMatthew Dillon 	}
681258223a3SMatthew Dillon 
682cec85a37SMatthew Dillon 	/*
683cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
684cec85a37SMatthew Dillon 	 *
685cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
686cec85a37SMatthew Dillon 	 * everything.
687cec85a37SMatthew Dillon 	 */
688258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
689258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
690258223a3SMatthew Dillon 	bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
691258223a3SMatthew Dillon 
692258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
693cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
694258223a3SMatthew Dillon 	fis[0] = 0x27;	/* Host to device */
695258223a3SMatthew Dillon 	fis[15] = 0x04;	/* SRST DEVCTL */
696258223a3SMatthew Dillon 
697258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
698258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
699258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
700258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
701258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W); /* Write */
702258223a3SMatthew Dillon 
703258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
7045f8c1efdSMatthew Dillon 	ccb->ccb_xa.flags = 0;
705cec85a37SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0) {
7065f8c1efdSMatthew Dillon 		kprintf("%s: First FIS failed\n", PORTNAME(ap));
707258223a3SMatthew Dillon 		goto err;
708cec85a37SMatthew Dillon 	}
709258223a3SMatthew Dillon 
710cec85a37SMatthew Dillon 	/*
711cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
712cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
713cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
714cec85a37SMatthew Dillon 	 * from the AHCI document.
715cec85a37SMatthew Dillon 	 *
716cec85a37SMatthew Dillon 	 * Give the device 1/10 of a second before sending the second
717cec85a37SMatthew Dillon 	 * FIS.
718cec85a37SMatthew Dillon 	 *
719cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
720cec85a37SMatthew Dillon 	 * everything.
721cec85a37SMatthew Dillon 	 */
722cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
723258223a3SMatthew Dillon 	fis[0] = 0x27;	/* Host to device */
724258223a3SMatthew Dillon 	fis[15] = 0;
725258223a3SMatthew Dillon 
726258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
727258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
728258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
729258223a3SMatthew Dillon 
730258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
7315f8c1efdSMatthew Dillon 	ccb->ccb_xa.flags = 0;
732cec85a37SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0) {
7335f8c1efdSMatthew Dillon 		kprintf("%s: Second FIS failed\n", PORTNAME(ap));
734258223a3SMatthew Dillon 		goto err;
735cec85a37SMatthew Dillon 	}
736258223a3SMatthew Dillon 
737258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY |
738258223a3SMatthew Dillon 	    AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) {
739258223a3SMatthew Dillon 		kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
740258223a3SMatthew Dillon 			PORTNAME(ap),
741258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
742258223a3SMatthew Dillon 		rc = EBUSY;
743258223a3SMatthew Dillon 		goto err;
744258223a3SMatthew Dillon 	}
745258223a3SMatthew Dillon 
746fd8bd957SMatthew Dillon 	/*
747fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
748fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
749fd8bd957SMatthew Dillon 	 *
750fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
751fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
752fd8bd957SMatthew Dillon 	 * unexpectedly.
753fd8bd957SMatthew Dillon 	 */
754fd8bd957SMatthew Dillon 	if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) {
755fd8bd957SMatthew Dillon 		ap->ap_ata.ap_type = ahci_port_signature_detect(ap);
756fd8bd957SMatthew Dillon 	} else {
757fd8bd957SMatthew Dillon 		if (ahci_port_signature_detect(ap) != ap->ap_ata.ap_type) {
758fd8bd957SMatthew Dillon 			kprintf("%s: device signature unexpectedly changed\n",
759fd8bd957SMatthew Dillon 				PORTNAME(ap));
760fd8bd957SMatthew Dillon 			rc = EBUSY;
761fd8bd957SMatthew Dillon 		}
762fd8bd957SMatthew Dillon 	}
763fd8bd957SMatthew Dillon 
764258223a3SMatthew Dillon 	rc = 0;
765258223a3SMatthew Dillon err:
766258223a3SMatthew Dillon 	if (ccb != NULL) {
767258223a3SMatthew Dillon 		/* Abort our command, if it failed, by stopping command DMA. */
768*17eab71eSMatthew Dillon 		kprintf("rc=%d active=%08x sactive=%08x slot=%d\n",
769*17eab71eSMatthew Dillon 			rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot);
770258223a3SMatthew Dillon 		if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) {
771258223a3SMatthew Dillon 			kprintf("%s: stopping the port, softreset slot "
772258223a3SMatthew Dillon 				"%d was still active.\n",
773258223a3SMatthew Dillon 				PORTNAME(ap),
774258223a3SMatthew Dillon 				ccb->ccb_slot);
775258223a3SMatthew Dillon 			ahci_port_stop(ap, 0);
776258223a3SMatthew Dillon 		}
777258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
778258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
779258223a3SMatthew Dillon 	}
780258223a3SMatthew Dillon 
781258223a3SMatthew Dillon 	/* Restore saved CMD register state */
782258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
783258223a3SMatthew Dillon 
784258223a3SMatthew Dillon 	crit_exit();
785258223a3SMatthew Dillon 
786258223a3SMatthew Dillon 	return (rc);
787258223a3SMatthew Dillon }
788258223a3SMatthew Dillon 
789fd8bd957SMatthew Dillon /*
790fd8bd957SMatthew Dillon  * AHCI port reset, Section 10.4.2
791fd8bd957SMatthew Dillon  *
792fd8bd957SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
793fd8bd957SMatthew Dillon  * connected to the port could still end-up hung.
794fd8bd957SMatthew Dillon  */
795258223a3SMatthew Dillon int
796*17eab71eSMatthew Dillon ahci_port_hardreset(struct ahci_port *ap)
797258223a3SMatthew Dillon {
798258223a3SMatthew Dillon 	u_int32_t			cmd, r;
799258223a3SMatthew Dillon 	int				rc;
800258223a3SMatthew Dillon 
801258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
802258223a3SMatthew Dillon 
803258223a3SMatthew Dillon 	/* Save previous command register state */
804258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
805258223a3SMatthew Dillon 
806258223a3SMatthew Dillon 	/* Clear ST, ignoring failure */
807258223a3SMatthew Dillon 	ahci_port_stop(ap, 0);
808258223a3SMatthew Dillon 
809258223a3SMatthew Dillon 	/* Perform device detection */
810258223a3SMatthew Dillon 	ap->ap_ata.ap_type = ATA_PORT_T_NONE;
811258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, 0);
812258223a3SMatthew Dillon 	DELAY(10000);
813258223a3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
814258223a3SMatthew Dillon 
815258223a3SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
816258223a3SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
817258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
818258223a3SMatthew Dillon 	} else {
819258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
820258223a3SMatthew Dillon 	}
821258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
822258223a3SMatthew Dillon 	DELAY(10000);	/* wait at least 1ms for COMRESET to be sent */
823258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
824258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
825258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
826258223a3SMatthew Dillon 	DELAY(10000);
827258223a3SMatthew Dillon 
828258223a3SMatthew Dillon 	/* Wait for device to be detected and communications established */
829cec85a37SMatthew Dillon 	if (ahci_pwait_eq(ap, 1000,
830cec85a37SMatthew Dillon 			  AHCI_PREG_SSTS, AHCI_PREG_SSTS_DET,
831258223a3SMatthew Dillon 			  AHCI_PREG_SSTS_DET_DEV)) {
832258223a3SMatthew Dillon 		rc = ENODEV;
833258223a3SMatthew Dillon 		goto err;
834258223a3SMatthew Dillon 	}
835258223a3SMatthew Dillon 
836258223a3SMatthew Dillon 	/* Clear SERR (incl X bit), so TFD can update */
837258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, ahci_pread(ap, AHCI_PREG_SERR));
838258223a3SMatthew Dillon 
839cec85a37SMatthew Dillon 	/*
840cec85a37SMatthew Dillon 	 * Wait for device to become ready
841cec85a37SMatthew Dillon 	 *
842cec85a37SMatthew Dillon 	 * This can take more then a second, give it 3 seconds.
843cec85a37SMatthew Dillon 	 */
844cec85a37SMatthew Dillon 	if (ahci_pwait_clr_to(ap, 3000,
845cec85a37SMatthew Dillon 			       AHCI_PREG_TFD, AHCI_PREG_TFD_STS_BSY |
846258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_DRQ | AHCI_PREG_TFD_STS_ERR)) {
847258223a3SMatthew Dillon 		rc = EBUSY;
848258223a3SMatthew Dillon 		kprintf("%s: Device will not come ready 0x%b\n",
849258223a3SMatthew Dillon 			PORTNAME(ap),
850258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
851258223a3SMatthew Dillon 		goto err;
852258223a3SMatthew Dillon 	}
853258223a3SMatthew Dillon 
854fd8bd957SMatthew Dillon 	ap->ap_ata.ap_type = ahci_port_signature_detect(ap);
855258223a3SMatthew Dillon 	rc = 0;
856258223a3SMatthew Dillon err:
857258223a3SMatthew Dillon 	/* Restore preserved port state */
858258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
859258223a3SMatthew Dillon 
860258223a3SMatthew Dillon 	return (rc);
861258223a3SMatthew Dillon }
862258223a3SMatthew Dillon 
863fd8bd957SMatthew Dillon /*
864fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
865fd8bd957SMatthew Dillon  * DISK.
866fd8bd957SMatthew Dillon  */
867fd8bd957SMatthew Dillon int
868fd8bd957SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap)
869fd8bd957SMatthew Dillon {
870fd8bd957SMatthew Dillon 	u_int32_t sig;
871fd8bd957SMatthew Dillon 
872fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
873fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
874fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
875fd8bd957SMatthew Dillon 	} else {
876fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
877fd8bd957SMatthew Dillon 	}
878fd8bd957SMatthew Dillon }
879fd8bd957SMatthew Dillon 
880fd8bd957SMatthew Dillon /*
881fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
882fd8bd957SMatthew Dillon  */
883258223a3SMatthew Dillon int
884258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
885258223a3SMatthew Dillon {
886258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
887258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
888258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
889258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
890258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
891258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
892258223a3SMatthew Dillon 	int				error;
893258223a3SMatthew Dillon 
894258223a3SMatthew Dillon 	if (xa->datalen == 0) {
895258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
896258223a3SMatthew Dillon 		return (0);
897258223a3SMatthew Dillon 	}
898258223a3SMatthew Dillon 
899258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
900258223a3SMatthew Dillon 				xa->data, xa->datalen,
901258223a3SMatthew Dillon 				ahci_load_prdt_callback,
902258223a3SMatthew Dillon 				&prdt,
903258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
904258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
905258223a3SMatthew Dillon 	if (error != 0) {
906258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
907258223a3SMatthew Dillon 		return (1);
908258223a3SMatthew Dillon 	}
909258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
910258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
911258223a3SMatthew Dillon 
912258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
913258223a3SMatthew Dillon 
914258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_data, dmap,
915258223a3SMatthew Dillon 			(xa->flags & ATA_F_READ) ?
916258223a3SMatthew Dillon 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
917258223a3SMatthew Dillon 
918258223a3SMatthew Dillon 	return (0);
919258223a3SMatthew Dillon 
920258223a3SMatthew Dillon #ifdef DIAGNOSTIC
921258223a3SMatthew Dillon diagerr:
922258223a3SMatthew Dillon 	bus_dmamap_unload(sc->sc_tag_data, dmap);
923258223a3SMatthew Dillon 	return (1);
924258223a3SMatthew Dillon #endif
925258223a3SMatthew Dillon }
926258223a3SMatthew Dillon 
927258223a3SMatthew Dillon /*
928258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
929258223a3SMatthew Dillon  * list is a temporary structure.
930258223a3SMatthew Dillon  */
931258223a3SMatthew Dillon static
932258223a3SMatthew Dillon void
933258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
934258223a3SMatthew Dillon 			int error)
935258223a3SMatthew Dillon {
936258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
937258223a3SMatthew Dillon 	u_int64_t addr;
938258223a3SMatthew Dillon 
939258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
940258223a3SMatthew Dillon 
941258223a3SMatthew Dillon 	while (nsegs) {
942258223a3SMatthew Dillon 		addr = segs->ds_addr;
943258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
944258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
945258223a3SMatthew Dillon #ifdef DIAGNOSTIC
946258223a3SMatthew Dillon 		KKASSERT((addr & 1) == 0);
947258223a3SMatthew Dillon 		KKASSERT((segs->ds_len & 1) == 0);
948258223a3SMatthew Dillon #endif
949258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
950258223a3SMatthew Dillon 		--nsegs;
951258223a3SMatthew Dillon 		if (nsegs)
952258223a3SMatthew Dillon 			++prd;
953258223a3SMatthew Dillon 		++segs;
954258223a3SMatthew Dillon 	}
955258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
956258223a3SMatthew Dillon }
957258223a3SMatthew Dillon 
958258223a3SMatthew Dillon void
959258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
960258223a3SMatthew Dillon {
961258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
962258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
963258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
964258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
965258223a3SMatthew Dillon 
966258223a3SMatthew Dillon 	if (xa->datalen != 0) {
967258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap,
968258223a3SMatthew Dillon 				(xa->flags & ATA_F_READ) ?
969258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
970258223a3SMatthew Dillon 
971258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
972258223a3SMatthew Dillon 
973258223a3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ)
974258223a3SMatthew Dillon 			xa->resid = 0;
975258223a3SMatthew Dillon 		else
976258223a3SMatthew Dillon 			xa->resid = xa->datalen -
977258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
978258223a3SMatthew Dillon 	}
979258223a3SMatthew Dillon }
980258223a3SMatthew Dillon 
9815f8c1efdSMatthew Dillon /*
9825f8c1efdSMatthew Dillon  * Start a command and poll for completion.
9835f8c1efdSMatthew Dillon  *
9845f8c1efdSMatthew Dillon  * NOTE: If the caller specifies a NULL timeout function the caller is
9855f8c1efdSMatthew Dillon  *	 responsible for clearing hardware state on failure, but we will
9865f8c1efdSMatthew Dillon  *	 deal with removing the ccb from any pending queue.
9875f8c1efdSMatthew Dillon  *
9885f8c1efdSMatthew Dillon  * NOTE: NCQ should never be used with this function.
9895f8c1efdSMatthew Dillon  */
990258223a3SMatthew Dillon int
991258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *))
992258223a3SMatthew Dillon {
993258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
9945f8c1efdSMatthew Dillon 	u_int32_t 	slot_mask = 1 << ccb->ccb_slot;
995258223a3SMatthew Dillon 
996258223a3SMatthew Dillon 	crit_enter();
997258223a3SMatthew Dillon 	ahci_start(ccb);
998258223a3SMatthew Dillon 	do {
9995f8c1efdSMatthew Dillon 		if (ahci_port_intr(ap, AHCI_PREG_CI_ALL_SLOTS) & slot_mask) {
1000258223a3SMatthew Dillon 			crit_exit();
1001258223a3SMatthew Dillon 			return (0);
1002258223a3SMatthew Dillon 		}
10035f8c1efdSMatthew Dillon 		if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
10045f8c1efdSMatthew Dillon 		    ccb->ccb_xa.state != ATA_S_PENDING) {
10055f8c1efdSMatthew Dillon 			break;
10065f8c1efdSMatthew Dillon 		}
1007258223a3SMatthew Dillon 		DELAY(1000000 / hz);
1008258223a3SMatthew Dillon 	} while (--timeout > 0);
1009258223a3SMatthew Dillon 
10105f8c1efdSMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
10115f8c1efdSMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_PENDING) {
10125f8c1efdSMatthew Dillon 		kprintf("%s: Warning poll completed unexpectedly for slot %d\n",
10135f8c1efdSMatthew Dillon 			PORTNAME(ap), ccb->ccb_slot);
10145f8c1efdSMatthew Dillon 		crit_exit();
10155f8c1efdSMatthew Dillon 		return (0);
10165f8c1efdSMatthew Dillon 	}
10175f8c1efdSMatthew Dillon 
10185f8c1efdSMatthew Dillon 	kprintf("%s: Poll timed-out for slot %d state %d\n",
10195f8c1efdSMatthew Dillon 		PORTNAME(ap), ccb->ccb_slot, ccb->ccb_xa.state);
10205f8c1efdSMatthew Dillon 
10215f8c1efdSMatthew Dillon 	if (timeout_fn != NULL) {
1022258223a3SMatthew Dillon 		timeout_fn(ccb);
10235f8c1efdSMatthew Dillon 	} else {
10245f8c1efdSMatthew Dillon 		if (ccb->ccb_xa.state == ATA_S_PENDING)
10255f8c1efdSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
10265f8c1efdSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
10275f8c1efdSMatthew Dillon 	}
1028258223a3SMatthew Dillon 	crit_exit();
1029258223a3SMatthew Dillon 
1030258223a3SMatthew Dillon 	return (1);
1031258223a3SMatthew Dillon }
1032258223a3SMatthew Dillon 
1033258223a3SMatthew Dillon void
1034258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
1035258223a3SMatthew Dillon {
1036258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1037258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1038258223a3SMatthew Dillon 
1039258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1040258223a3SMatthew Dillon 
1041258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
1042258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
1043258223a3SMatthew Dillon 
1044258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
1045258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
1046258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1047258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1048258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
1049258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1050258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1051258223a3SMatthew Dillon 
1052258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
1053258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
1054258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1055258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
1056258223a3SMatthew Dillon 
1057258223a3SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
1058258223a3SMatthew Dillon 		/* Issue NCQ commands only when there are no outstanding
1059258223a3SMatthew Dillon 		 * standard commands. */
1060258223a3SMatthew Dillon 		if (ap->ap_active != 0 || !TAILQ_EMPTY(&ap->ap_ccb_pending))
1061258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1062258223a3SMatthew Dillon 		else {
1063258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
1064258223a3SMatthew Dillon 			ap->ap_sactive |= (1 << ccb->ccb_slot);
1065258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
1066258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
1067258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1068258223a3SMatthew Dillon 		}
1069258223a3SMatthew Dillon 	} else {
10705f8c1efdSMatthew Dillon 		/*
10715f8c1efdSMatthew Dillon 		 * Wait for all NCQ commands to finish before issuing standard
10725f8c1efdSMatthew Dillon 		 * command.
10735f8c1efdSMatthew Dillon 		 */
1074258223a3SMatthew Dillon 		if (ap->ap_sactive != 0 || ap->ap_active_cnt == 2)
1075258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
1076258223a3SMatthew Dillon 		else if (ap->ap_active_cnt < 2) {
1077258223a3SMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
1078258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
1079258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1080258223a3SMatthew Dillon 			ap->ap_active_cnt++;
1081258223a3SMatthew Dillon 		}
1082258223a3SMatthew Dillon 	}
1083258223a3SMatthew Dillon }
1084258223a3SMatthew Dillon 
1085258223a3SMatthew Dillon void
1086258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap)
1087258223a3SMatthew Dillon {
1088258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
1089258223a3SMatthew Dillon 	u_int32_t			sact_change = 0;
1090258223a3SMatthew Dillon 
1091258223a3SMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
1092258223a3SMatthew Dillon 
1093258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1094258223a3SMatthew Dillon 	if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
1095258223a3SMatthew Dillon 		return;
1096258223a3SMatthew Dillon 
1097258223a3SMatthew Dillon 	/* Start all the NCQ commands at the head of the pending list. */
1098258223a3SMatthew Dillon 	do {
1099258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1100258223a3SMatthew Dillon 		sact_change |= 1 << nextccb->ccb_slot;
1101258223a3SMatthew Dillon 		nextccb->ccb_xa.state = ATA_S_ONCHIP;
1102258223a3SMatthew Dillon 		nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1103258223a3SMatthew Dillon 	} while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
1104258223a3SMatthew Dillon 
1105258223a3SMatthew Dillon 	ap->ap_sactive |= sact_change;
1106258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
1107258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
1108258223a3SMatthew Dillon 
1109258223a3SMatthew Dillon 	return;
1110258223a3SMatthew Dillon }
1111258223a3SMatthew Dillon 
1112258223a3SMatthew Dillon void
1113258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
1114258223a3SMatthew Dillon {
1115258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
1116258223a3SMatthew Dillon 
1117258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1118258223a3SMatthew Dillon 	if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) {
1119258223a3SMatthew Dillon 		KKASSERT(last_was_ncq == 0);	/* otherwise it should have
1120258223a3SMatthew Dillon 						 * been started already. */
1121258223a3SMatthew Dillon 
1122258223a3SMatthew Dillon 		/* Issue NCQ commands only when there are no outstanding
1123258223a3SMatthew Dillon 		 * standard commands. */
1124258223a3SMatthew Dillon 		ap->ap_active_cnt--;
1125258223a3SMatthew Dillon 		if (ap->ap_active == 0)
1126258223a3SMatthew Dillon 			ahci_issue_pending_ncq_commands(ap);
1127258223a3SMatthew Dillon 		else
1128258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 1);
1129258223a3SMatthew Dillon 	} else if (nextccb) {
1130258223a3SMatthew Dillon 		if (ap->ap_sactive != 0 || last_was_ncq)
1131258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
1132258223a3SMatthew Dillon 
1133258223a3SMatthew Dillon 		/* Wait for all NCQ commands to finish before issuing standard
1134258223a3SMatthew Dillon 		 * command. */
1135258223a3SMatthew Dillon 		if (ap->ap_sactive != 0)
1136258223a3SMatthew Dillon 			return;
1137258223a3SMatthew Dillon 
1138258223a3SMatthew Dillon 		/* Keep up to 2 standard commands on-chip at a time. */
1139258223a3SMatthew Dillon 		do {
1140258223a3SMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
1141258223a3SMatthew Dillon 			ap->ap_active |= 1 << nextccb->ccb_slot;
1142258223a3SMatthew Dillon 			nextccb->ccb_xa.state = ATA_S_ONCHIP;
1143258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
1144258223a3SMatthew Dillon 			if (last_was_ncq)
1145258223a3SMatthew Dillon 				ap->ap_active_cnt++;
1146258223a3SMatthew Dillon 			if (ap->ap_active_cnt == 2)
1147258223a3SMatthew Dillon 				break;
1148258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 1);
1149258223a3SMatthew Dillon 			nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1150258223a3SMatthew Dillon 		} while (nextccb && !(nextccb->ccb_xa.flags & ATA_F_NCQ));
1151258223a3SMatthew Dillon 	} else if (!last_was_ncq) {
1152258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 1 || ap->ap_active_cnt == 2);
1153258223a3SMatthew Dillon 
1154258223a3SMatthew Dillon 		/* Standard command finished, none waiting to start. */
1155258223a3SMatthew Dillon 		ap->ap_active_cnt--;
1156258223a3SMatthew Dillon 	} else {
1157258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
1158258223a3SMatthew Dillon 
1159258223a3SMatthew Dillon 		/* NCQ command finished. */
1160258223a3SMatthew Dillon 	}
1161258223a3SMatthew Dillon }
1162258223a3SMatthew Dillon 
1163258223a3SMatthew Dillon void
1164258223a3SMatthew Dillon ahci_intr(void *arg)
1165258223a3SMatthew Dillon {
1166258223a3SMatthew Dillon 	struct ahci_softc		*sc = arg;
1167258223a3SMatthew Dillon 	u_int32_t			is, ack = 0;
1168258223a3SMatthew Dillon 	int				port;
1169258223a3SMatthew Dillon 
1170258223a3SMatthew Dillon 	/* Read global interrupt status */
1171258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
1172258223a3SMatthew Dillon 	if (is == 0 || is == 0xffffffff)
1173258223a3SMatthew Dillon 		return;
1174258223a3SMatthew Dillon 	ack = is;
1175258223a3SMatthew Dillon 
1176258223a3SMatthew Dillon #ifdef AHCI_COALESCE
1177258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
1178258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
1179258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
1180258223a3SMatthew Dillon 		    DEVNAME(sc));
1181258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
1182258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
1183258223a3SMatthew Dillon 	}
1184258223a3SMatthew Dillon #endif
1185258223a3SMatthew Dillon 
1186258223a3SMatthew Dillon 	/* Process interrupts for each port */
1187258223a3SMatthew Dillon 	while (is) {
1188258223a3SMatthew Dillon 		port = ffs(is) - 1;
1189258223a3SMatthew Dillon 		if (sc->sc_ports[port]) {
1190258223a3SMatthew Dillon 			ahci_port_intr(sc->sc_ports[port],
1191258223a3SMatthew Dillon 				       AHCI_PREG_CI_ALL_SLOTS);
1192258223a3SMatthew Dillon 		}
1193258223a3SMatthew Dillon 		is &= ~(1 << port);
1194258223a3SMatthew Dillon 	}
1195258223a3SMatthew Dillon 
1196258223a3SMatthew Dillon 	/* Finally, acknowledge global interrupt */
1197258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, ack);
1198258223a3SMatthew Dillon }
1199258223a3SMatthew Dillon 
1200258223a3SMatthew Dillon u_int32_t
1201258223a3SMatthew Dillon ahci_port_intr(struct ahci_port *ap, u_int32_t ci_mask)
1202258223a3SMatthew Dillon {
1203258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1204258223a3SMatthew Dillon 	u_int32_t			is, ci_saved, ci_masked, processed = 0;
1205258223a3SMatthew Dillon 	int				slot, need_restart = 0;
1206258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = NULL;
1207258223a3SMatthew Dillon 	volatile u_int32_t		*active;
1208258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1209258223a3SMatthew Dillon 	u_int32_t			tmp;
1210258223a3SMatthew Dillon #endif
1211258223a3SMatthew Dillon 
1212258223a3SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
1213258223a3SMatthew Dillon 
1214258223a3SMatthew Dillon 	/* Ack port interrupt only if checking all command slots. */
1215258223a3SMatthew Dillon 	if (ci_mask == AHCI_PREG_CI_ALL_SLOTS)
1216258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is);
1217258223a3SMatthew Dillon 
1218258223a3SMatthew Dillon 	if (is)
1219258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: interrupt: %b\n", PORTNAME(ap),
1220258223a3SMatthew Dillon 			is, AHCI_PFMT_IS);
1221258223a3SMatthew Dillon 
1222258223a3SMatthew Dillon 	if (ap->ap_sactive) {
1223258223a3SMatthew Dillon 		/* Active NCQ commands - use SActive instead of CI */
1224258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
1225258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
1226258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
1227258223a3SMatthew Dillon 		active = &ap->ap_sactive;
1228258223a3SMatthew Dillon 	} else {
1229258223a3SMatthew Dillon 		/* Save CI */
1230258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
1231258223a3SMatthew Dillon 		active = &ap->ap_active;
1232258223a3SMatthew Dillon 	}
1233258223a3SMatthew Dillon 
1234258223a3SMatthew Dillon 	/* Command failed.  See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2. */
1235258223a3SMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
1236258223a3SMatthew Dillon 		u_int32_t		tfd, serr;
1237258223a3SMatthew Dillon 		int			err_slot;
1238258223a3SMatthew Dillon 
1239258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
1240258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
1241258223a3SMatthew Dillon 
1242258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
1243258223a3SMatthew Dillon 			/* Errored slot is easy to determine from CMD. */
1244258223a3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(ahci_pread(ap,
1245258223a3SMatthew Dillon 			    AHCI_PREG_CMD));
1246258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1247258223a3SMatthew Dillon 
1248258223a3SMatthew Dillon 			/* Preserve received taskfile data from the RFIS. */
1249258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
1250258223a3SMatthew Dillon 			    sizeof(struct ata_fis_d2h));
1251258223a3SMatthew Dillon 		} else
1252258223a3SMatthew Dillon 			err_slot = -1;	/* Must extract error from log page */
1253258223a3SMatthew Dillon 
1254258223a3SMatthew Dillon 		DPRINTF(AHCI_D_VERBOSE, "%s: errored slot %d, TFD: %b, SERR:"
1255258223a3SMatthew Dillon 		    " %b, DIAG: %b\n", PORTNAME(ap), err_slot, tfd,
1256258223a3SMatthew Dillon 		    AHCI_PFMT_TFD_STS, AHCI_PREG_SERR_ERR(serr),
1257258223a3SMatthew Dillon 		    AHCI_PFMT_SERR_ERR, AHCI_PREG_SERR_DIAG(serr),
1258258223a3SMatthew Dillon 		    AHCI_PFMT_SERR_DIAG);
1259258223a3SMatthew Dillon 
1260258223a3SMatthew Dillon 		/* Turn off ST to clear CI and SACT. */
1261258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
1262258223a3SMatthew Dillon 		need_restart = 1;
1263258223a3SMatthew Dillon 
1264258223a3SMatthew Dillon 		/* Clear SERR to enable capturing new errors. */
1265258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
1266258223a3SMatthew Dillon 
1267258223a3SMatthew Dillon 		/* Acknowledge the interrupts we can recover from. */
1268258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_TFES |
1269258223a3SMatthew Dillon 		    AHCI_PREG_IS_IFS);
1270258223a3SMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
1271258223a3SMatthew Dillon 
1272258223a3SMatthew Dillon 		/* If device hasn't cleared its busy status, try to idle it. */
1273258223a3SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1274*17eab71eSMatthew Dillon 			kprintf("%s: Attempting to idle device\n",
1275258223a3SMatthew Dillon 				PORTNAME(ap));
1276*17eab71eSMatthew Dillon 			if (ahci_port_reset(ap, 0)) {
1277*17eab71eSMatthew Dillon 				kprintf("%s: Unable to idle device, port "
1278*17eab71eSMatthew Dillon 					"bricked on us\n",
1279258223a3SMatthew Dillon 					PORTNAME(ap));
1280258223a3SMatthew Dillon 				goto fatal;
1281258223a3SMatthew Dillon 			}
1282258223a3SMatthew Dillon 
1283258223a3SMatthew Dillon 			/* Had to reset device, can't gather extended info. */
1284258223a3SMatthew Dillon 		} else if (ap->ap_sactive) {
1285258223a3SMatthew Dillon 			/* Recover the NCQ error from log page 10h. */
1286258223a3SMatthew Dillon 			ahci_port_read_ncq_error(ap, &err_slot);
1287258223a3SMatthew Dillon 			if (err_slot < 0)
1288258223a3SMatthew Dillon 				goto failall;
1289258223a3SMatthew Dillon 
1290258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
1291258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
1292258223a3SMatthew Dillon 
1293258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1294258223a3SMatthew Dillon 		} else {
1295258223a3SMatthew Dillon 			/* Didn't reset, could gather extended info from log. */
1296258223a3SMatthew Dillon 		}
1297258223a3SMatthew Dillon 
1298258223a3SMatthew Dillon 		/*
1299258223a3SMatthew Dillon 		 * If we couldn't determine the errored slot, reset the port
1300258223a3SMatthew Dillon 		 * and fail all the active slots.
1301258223a3SMatthew Dillon 		 */
1302258223a3SMatthew Dillon 		if (err_slot == -1) {
1303*17eab71eSMatthew Dillon 			if (ahci_port_reset(ap, 0)) {
1304*17eab71eSMatthew Dillon 				kprintf("%s: Unable to idle device after "
1305*17eab71eSMatthew Dillon 					"NCQ error, port bricked on us\n",
1306258223a3SMatthew Dillon 					PORTNAME(ap));
1307258223a3SMatthew Dillon 				goto fatal;
1308258223a3SMatthew Dillon 			}
1309258223a3SMatthew Dillon 			kprintf("%s: couldn't recover NCQ error, failing "
1310258223a3SMatthew Dillon 				"all outstanding commands.\n",
1311258223a3SMatthew Dillon 				PORTNAME(ap));
1312258223a3SMatthew Dillon 			goto failall;
1313258223a3SMatthew Dillon 		}
1314258223a3SMatthew Dillon 
1315258223a3SMatthew Dillon 		/* Clear the failed command in saved CI so completion runs. */
1316258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
1317258223a3SMatthew Dillon 
1318258223a3SMatthew Dillon 		/* Note the error in the ata_xfer. */
1319258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
1320258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
1321258223a3SMatthew Dillon 
1322258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1323258223a3SMatthew Dillon 		/* There may only be one outstanding standard command now. */
1324258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
1325258223a3SMatthew Dillon 			tmp = ci_saved;
1326258223a3SMatthew Dillon 			if (tmp) {
1327258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
1328258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
1329258223a3SMatthew Dillon 				KKASSERT(tmp == 0);
1330258223a3SMatthew Dillon 			}
1331258223a3SMatthew Dillon 		}
1332258223a3SMatthew Dillon #endif
1333258223a3SMatthew Dillon 	}
1334258223a3SMatthew Dillon 
1335258223a3SMatthew Dillon 	/*
1336258223a3SMatthew Dillon 	 * Port change (hot-plug).
1337258223a3SMatthew Dillon 	 *
1338258223a3SMatthew Dillon 	 * A PCS interrupt will occur on hot-plug once communication is
1339258223a3SMatthew Dillon 	 * established.
1340258223a3SMatthew Dillon 	 *
1341258223a3SMatthew Dillon 	 * A PRCS interrupt will occur on hot-unplug (and possibly also
1342258223a3SMatthew Dillon 	 * on hot-plug).
1343258223a3SMatthew Dillon 	 *
1344258223a3SMatthew Dillon 	 * We can then check the CPS (Cold Presence State) bit to determine
1345258223a3SMatthew Dillon 	 * if a device is plugged in or not and do the right thing.
1346258223a3SMatthew Dillon 	 */
1347258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
1348258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
1349258223a3SMatthew Dillon 			(AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X) << 16);
1350cec85a37SMatthew Dillon 
1351258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
1352258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
1353258223a3SMatthew Dillon 			if (ap->ap_ata.ap_type == ATA_PORT_T_NONE) {
1354cec85a37SMatthew Dillon 				kprintf("%s: HOTPLUG - Device inserted\n",
1355258223a3SMatthew Dillon 					PORTNAME(ap));
1356fd8bd957SMatthew Dillon 				if (ahci_port_init(ap) == 0)
1357fd8bd957SMatthew Dillon 					ahci_cam_changed(ap, 1);
1358258223a3SMatthew Dillon 			}
1359258223a3SMatthew Dillon 			break;
1360258223a3SMatthew Dillon 		default:
1361258223a3SMatthew Dillon 			if (ap->ap_ata.ap_type != ATA_PORT_T_NONE) {
1362258223a3SMatthew Dillon 				kprintf("%s: HOTPLUG - Device removed\n",
1363258223a3SMatthew Dillon 					PORTNAME(ap));
1364*17eab71eSMatthew Dillon 				ahci_port_reset(ap, 1);
1365fd8bd957SMatthew Dillon 				ahci_cam_changed(ap, 0);
1366258223a3SMatthew Dillon 			}
1367258223a3SMatthew Dillon 			break;
1368258223a3SMatthew Dillon 		}
1369258223a3SMatthew Dillon 	}
1370258223a3SMatthew Dillon 
1371258223a3SMatthew Dillon 	/* Check for remaining errors - they are fatal. */
1372258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
1373258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
13744444122dSMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
13754444122dSMatthew Dillon 		kprintf("%s: unrecoverable errors (IS: %b, SERR: %b %b), "
13764444122dSMatthew Dillon 			"disabling port.\n",
13774444122dSMatthew Dillon 			PORTNAME(ap),
13784444122dSMatthew Dillon 			is, AHCI_PFMT_IS,
13794444122dSMatthew Dillon 			AHCI_PREG_SERR_ERR(serr), AHCI_PFMT_SERR_ERR,
13804444122dSMatthew Dillon 			AHCI_PREG_SERR_DIAG(serr), AHCI_PFMT_SERR_DIAG
13814444122dSMatthew Dillon 		);
1382258223a3SMatthew Dillon 		/* XXX try recovery first */
1383258223a3SMatthew Dillon 		goto fatal;
1384258223a3SMatthew Dillon 	}
1385258223a3SMatthew Dillon 
1386258223a3SMatthew Dillon 	/* Fail all outstanding commands if we know the port won't recover. */
1387258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
1388258223a3SMatthew Dillon fatal:
1389258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
1390258223a3SMatthew Dillon failall:
1391258223a3SMatthew Dillon 
1392258223a3SMatthew Dillon 		/* Ensure port is shut down. */
1393258223a3SMatthew Dillon 		ahci_port_stop(ap, 1);
1394258223a3SMatthew Dillon 
1395258223a3SMatthew Dillon 		/* Error all the active slots. */
1396258223a3SMatthew Dillon 		ci_masked = ci_saved & *active;
1397258223a3SMatthew Dillon 		while (ci_masked) {
1398258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
1399258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
1400258223a3SMatthew Dillon 			ci_masked &= ~(1 << slot);
1401258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ERROR;
1402258223a3SMatthew Dillon 		}
1403258223a3SMatthew Dillon 
1404258223a3SMatthew Dillon 		/* Run completion for all active slots. */
1405258223a3SMatthew Dillon 		ci_saved &= ~*active;
1406258223a3SMatthew Dillon 
1407258223a3SMatthew Dillon 		/*
1408258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
1409258223a3SMatthew Dillon 		 *
1410258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
1411258223a3SMatthew Dillon 		 * a livelock.
1412258223a3SMatthew Dillon 		 */
1413258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
1414258223a3SMatthew Dillon 			need_restart = 0;
1415258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
1416258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
1417258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
1418258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
1419258223a3SMatthew Dillon 		}
1420258223a3SMatthew Dillon 	}
1421258223a3SMatthew Dillon 
1422258223a3SMatthew Dillon 	/*
1423258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
1424258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
1425258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
1426258223a3SMatthew Dillon 	 */
1427258223a3SMatthew Dillon 	ci_masked = ~ci_saved & *active & ci_mask;
1428258223a3SMatthew Dillon 	while (ci_masked) {
1429258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
1430258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
1431258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
1432258223a3SMatthew Dillon 
1433258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
1434258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
1435258223a3SMatthew Dillon 		    " (error)" : "");
1436258223a3SMatthew Dillon 
1437258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
1438258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1439258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
1440258223a3SMatthew Dillon 
1441258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
1442258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1443258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
1444258223a3SMatthew Dillon 
1445258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
1446258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1447258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
1448258223a3SMatthew Dillon 
1449258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
1450258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
1451258223a3SMatthew Dillon 
1452258223a3SMatthew Dillon 		processed |= 1 << ccb->ccb_slot;
1453258223a3SMatthew Dillon 	}
1454258223a3SMatthew Dillon 
1455258223a3SMatthew Dillon 	if (need_restart) {
1456258223a3SMatthew Dillon 		/* Restart command DMA on the port */
1457*17eab71eSMatthew Dillon 		ahci_port_start(ap);
1458258223a3SMatthew Dillon 
1459258223a3SMatthew Dillon 		/* Re-enable outstanding commands on port. */
1460258223a3SMatthew Dillon 		if (ci_saved) {
1461258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1462258223a3SMatthew Dillon 			tmp = ci_saved;
1463258223a3SMatthew Dillon 			while (tmp) {
1464258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
1465258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
1466258223a3SMatthew Dillon 				ccb = &ap->ap_ccbs[slot];
1467258223a3SMatthew Dillon 				KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
1468258223a3SMatthew Dillon 				KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
1469258223a3SMatthew Dillon 				    (!!ap->ap_sactive));
1470258223a3SMatthew Dillon 			}
1471258223a3SMatthew Dillon #endif
1472258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
1473258223a3SMatthew Dillon 			    "re-enabling%s slots %08x\n", PORTNAME(ap),
1474258223a3SMatthew Dillon 			    ap->ap_sactive ? " NCQ" : "", ci_saved);
1475258223a3SMatthew Dillon 
1476258223a3SMatthew Dillon 			if (ap->ap_sactive)
1477258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
1478258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
1479258223a3SMatthew Dillon 		}
1480258223a3SMatthew Dillon 	}
1481258223a3SMatthew Dillon 
1482258223a3SMatthew Dillon 	return (processed);
1483258223a3SMatthew Dillon }
1484258223a3SMatthew Dillon 
1485258223a3SMatthew Dillon struct ahci_ccb *
1486258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
1487258223a3SMatthew Dillon {
1488258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
1489258223a3SMatthew Dillon 
1490258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
1491258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
1492258223a3SMatthew Dillon 	if (ccb != NULL) {
1493258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
1494258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
1495258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
1496258223a3SMatthew Dillon 	}
1497258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
1498258223a3SMatthew Dillon 
1499258223a3SMatthew Dillon 	return (ccb);
1500258223a3SMatthew Dillon }
1501258223a3SMatthew Dillon 
1502258223a3SMatthew Dillon void
1503258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
1504258223a3SMatthew Dillon {
1505258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1506258223a3SMatthew Dillon 
1507258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1508258223a3SMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
1509258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_TIMEOUT &&
1510258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_ERROR) {
1511258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
1512258223a3SMatthew Dillon 			"slot %d\n",
1513258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
1514258223a3SMatthew Dillon 			ccb->ccb_slot);
1515258223a3SMatthew Dillon 	}
1516258223a3SMatthew Dillon #endif
1517258223a3SMatthew Dillon 
1518258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
1519258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
1520258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
1521258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
1522258223a3SMatthew Dillon }
1523258223a3SMatthew Dillon 
1524258223a3SMatthew Dillon struct ahci_ccb *
1525258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
1526258223a3SMatthew Dillon {
1527258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
1528258223a3SMatthew Dillon 	u_int32_t sact;
1529258223a3SMatthew Dillon 
1530258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
1531258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
1532258223a3SMatthew Dillon 	if (sact != 0)
1533258223a3SMatthew Dillon 		kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
1534258223a3SMatthew Dillon 	KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
1535258223a3SMatthew Dillon 
1536258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1537258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy == 0);
1538258223a3SMatthew Dillon 	ap->ap_err_busy = 1;
1539258223a3SMatthew Dillon #endif
1540258223a3SMatthew Dillon 	/* Save outstanding command state. */
1541258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
1542258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
1543258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
1544258223a3SMatthew Dillon 
1545258223a3SMatthew Dillon 	/*
1546258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
1547258223a3SMatthew Dillon 	 * run prematurely.
1548258223a3SMatthew Dillon 	 */
1549258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
1550258223a3SMatthew Dillon 
1551258223a3SMatthew Dillon 	/*
1552258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
1553258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
1554258223a3SMatthew Dillon 	 */
1555258223a3SMatthew Dillon 	err_ccb = ahci_get_ccb(ap);
1556258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
1557258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
1558258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
1559258223a3SMatthew Dillon 
1560258223a3SMatthew Dillon 	return err_ccb;
1561258223a3SMatthew Dillon }
1562258223a3SMatthew Dillon 
1563258223a3SMatthew Dillon void
1564258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
1565258223a3SMatthew Dillon {
1566258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
1567258223a3SMatthew Dillon 	u_int32_t sact;
15685f8c1efdSMatthew Dillon 	u_int32_t ci;
1569258223a3SMatthew Dillon 
1570258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1571258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy);
1572258223a3SMatthew Dillon #endif
15735f8c1efdSMatthew Dillon 	/*
15745f8c1efdSMatthew Dillon 	 * No commands may be active on the chip
15755f8c1efdSMatthew Dillon 	 */
1576258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
15775f8c1efdSMatthew Dillon 	if (sact) {
15785f8c1efdSMatthew Dillon 		panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
15795f8c1efdSMatthew Dillon 		      ccb->ccb_slot, sact);
1580258223a3SMatthew Dillon 	}
15815f8c1efdSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
15825f8c1efdSMatthew Dillon 	if (ci) {
15835f8c1efdSMatthew Dillon 		panic("ahci_put_err_ccb(%d) but CI %08x != 0\n",
15845f8c1efdSMatthew Dillon 		      ccb->ccb_slot, ci);
15855f8c1efdSMatthew Dillon 	}
1586258223a3SMatthew Dillon 
1587258223a3SMatthew Dillon 	/* Done with the CCB */
1588258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
1589258223a3SMatthew Dillon 
1590258223a3SMatthew Dillon 	/* Restore outstanding command state */
1591258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
1592258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
1593258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
1594258223a3SMatthew Dillon 
1595258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1596258223a3SMatthew Dillon 	ap->ap_err_busy = 0;
1597258223a3SMatthew Dillon #endif
1598258223a3SMatthew Dillon }
1599258223a3SMatthew Dillon 
1600258223a3SMatthew Dillon int
1601258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
1602258223a3SMatthew Dillon {
1603258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
1604258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
1605258223a3SMatthew Dillon 	u_int32_t			cmd;
1606258223a3SMatthew Dillon 	struct ata_fis_h2d		*fis;
1607258223a3SMatthew Dillon 	int				rc = EIO;
1608258223a3SMatthew Dillon 
1609258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
1610258223a3SMatthew Dillon 
1611258223a3SMatthew Dillon 	/* Save command register state. */
1612258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1613258223a3SMatthew Dillon 
1614258223a3SMatthew Dillon 	/* Port should have been idled already.  Start it. */
1615258223a3SMatthew Dillon 	KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
1616*17eab71eSMatthew Dillon 	ahci_port_start(ap);
1617258223a3SMatthew Dillon 
1618258223a3SMatthew Dillon 	/* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
1619258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
1620258223a3SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
1621258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
1622258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
1623258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1624258223a3SMatthew Dillon 	bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
1625258223a3SMatthew Dillon 
1626258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
1627258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
1628258223a3SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD;
1629258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
1630258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
1631258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
1632258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
1633258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
1634258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
1635258223a3SMatthew Dillon 	fis->device = 0;
1636258223a3SMatthew Dillon 
1637258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
1638258223a3SMatthew Dillon 
1639258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
1640258223a3SMatthew Dillon 		rc = ENOMEM;	/* XXX caller must abort all commands */
1641258223a3SMatthew Dillon 		goto err;
1642258223a3SMatthew Dillon 	}
1643258223a3SMatthew Dillon 
1644258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
1645258223a3SMatthew Dillon 	if (ahci_poll(ccb, hz, NULL) != 0)
1646258223a3SMatthew Dillon 		goto err;
1647258223a3SMatthew Dillon 
1648258223a3SMatthew Dillon 	rc = 0;
1649258223a3SMatthew Dillon err:
1650258223a3SMatthew Dillon 	/* Abort our command, if it failed, by stopping command DMA. */
1651258223a3SMatthew Dillon 	if (rc != 0 && (ap->ap_active & (1 << ccb->ccb_slot))) {
1652258223a3SMatthew Dillon 		kprintf("%s: log page read failed, slot %d was still active.\n",
1653258223a3SMatthew Dillon 			PORTNAME(ap), ccb->ccb_slot);
1654258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
1655258223a3SMatthew Dillon 	}
1656258223a3SMatthew Dillon 
1657258223a3SMatthew Dillon 	/* Done with the error CCB now. */
1658258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
1659258223a3SMatthew Dillon 	ahci_put_err_ccb(ccb);
1660258223a3SMatthew Dillon 
1661258223a3SMatthew Dillon 	/* Extract failed register set and tags from the scratch space. */
1662258223a3SMatthew Dillon 	if (rc == 0) {
1663258223a3SMatthew Dillon 		struct ata_log_page_10h		*log;
1664258223a3SMatthew Dillon 		int				err_slot;
1665258223a3SMatthew Dillon 
1666258223a3SMatthew Dillon 		log = (struct ata_log_page_10h *)ap->ap_err_scratch;
1667258223a3SMatthew Dillon 		if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
1668258223a3SMatthew Dillon 			/* Not queued bit was set - wasn't an NCQ error? */
1669258223a3SMatthew Dillon 			kprintf("%s: read NCQ error page, but not an NCQ "
1670258223a3SMatthew Dillon 				"error?\n",
1671258223a3SMatthew Dillon 				PORTNAME(ap));
1672258223a3SMatthew Dillon 			rc = ESRCH;
1673258223a3SMatthew Dillon 		} else {
1674258223a3SMatthew Dillon 			/* Copy back the log record as a D2H register FIS. */
1675258223a3SMatthew Dillon 			*err_slotp = err_slot = log->err_regs.type &
1676258223a3SMatthew Dillon 			    ATA_LOG_10H_TYPE_TAG_MASK;
1677258223a3SMatthew Dillon 
1678258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
1679258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
1680258223a3SMatthew Dillon 			    sizeof(struct ata_fis_d2h));
1681258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
1682258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.flags = 0;
1683258223a3SMatthew Dillon 		}
1684258223a3SMatthew Dillon 	}
1685258223a3SMatthew Dillon 
1686258223a3SMatthew Dillon 	/* Restore saved CMD register state */
1687258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1688258223a3SMatthew Dillon 
1689258223a3SMatthew Dillon 	return (rc);
1690258223a3SMatthew Dillon }
1691258223a3SMatthew Dillon 
1692258223a3SMatthew Dillon /*
1693258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
1694258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
1695258223a3SMatthew Dillon  * single physical base address.
1696258223a3SMatthew Dillon  */
1697258223a3SMatthew Dillon struct ahci_dmamem *
1698258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
1699258223a3SMatthew Dillon {
1700258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
1701258223a3SMatthew Dillon 	int	error;
1702258223a3SMatthew Dillon 
1703258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
1704258223a3SMatthew Dillon 
1705258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
1706258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
1707258223a3SMatthew Dillon 	if (error == 0) {
1708258223a3SMatthew Dillon 		adm->adm_tag = tag;
1709258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
1710258223a3SMatthew Dillon 					adm->adm_kva,
1711258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
1712258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
1713258223a3SMatthew Dillon 					0);
1714258223a3SMatthew Dillon 	}
1715258223a3SMatthew Dillon 	if (error) {
1716258223a3SMatthew Dillon 		if (adm->adm_map) {
1717258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
1718258223a3SMatthew Dillon 			adm->adm_map = NULL;
1719258223a3SMatthew Dillon 			adm->adm_tag = NULL;
1720258223a3SMatthew Dillon 			adm->adm_kva = NULL;
1721258223a3SMatthew Dillon 		}
1722258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
1723258223a3SMatthew Dillon 		adm = NULL;
1724258223a3SMatthew Dillon 	}
1725258223a3SMatthew Dillon 	return (adm);
1726258223a3SMatthew Dillon }
1727258223a3SMatthew Dillon 
1728258223a3SMatthew Dillon static
1729258223a3SMatthew Dillon void
1730258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
1731258223a3SMatthew Dillon {
1732258223a3SMatthew Dillon 	KKASSERT(error == 0);
1733258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
1734258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
1735258223a3SMatthew Dillon }
1736258223a3SMatthew Dillon 
1737258223a3SMatthew Dillon 
1738258223a3SMatthew Dillon void
1739258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
1740258223a3SMatthew Dillon {
1741258223a3SMatthew Dillon 	if (adm->adm_map) {
1742258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
1743258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
1744258223a3SMatthew Dillon 		adm->adm_map = NULL;
1745258223a3SMatthew Dillon 		adm->adm_tag = NULL;
1746258223a3SMatthew Dillon 		adm->adm_kva = NULL;
1747258223a3SMatthew Dillon 	}
1748258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
1749258223a3SMatthew Dillon }
1750258223a3SMatthew Dillon 
1751258223a3SMatthew Dillon u_int32_t
1752258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
1753258223a3SMatthew Dillon {
1754258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1755258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
1756258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
1757258223a3SMatthew Dillon }
1758258223a3SMatthew Dillon 
1759258223a3SMatthew Dillon void
1760258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
1761258223a3SMatthew Dillon {
1762258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
1763258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1764258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
1765258223a3SMatthew Dillon }
1766258223a3SMatthew Dillon 
1767258223a3SMatthew Dillon int
1768258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
1769258223a3SMatthew Dillon 	     u_int32_t target)
1770258223a3SMatthew Dillon {
1771258223a3SMatthew Dillon 	int				i;
1772258223a3SMatthew Dillon 
1773258223a3SMatthew Dillon 	for (i = 0; i < 1000; i++) {
1774258223a3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
1775258223a3SMatthew Dillon 			return (0);
1776258223a3SMatthew Dillon 		DELAY(1000);
1777258223a3SMatthew Dillon 	}
1778258223a3SMatthew Dillon 
1779258223a3SMatthew Dillon 	return (1);
1780258223a3SMatthew Dillon }
1781258223a3SMatthew Dillon 
1782258223a3SMatthew Dillon u_int32_t
1783258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
1784258223a3SMatthew Dillon {
1785258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
1786258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
1787258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
1788258223a3SMatthew Dillon }
1789258223a3SMatthew Dillon 
1790258223a3SMatthew Dillon void
1791258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
1792258223a3SMatthew Dillon {
1793258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
1794258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
1795258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
1796258223a3SMatthew Dillon }
1797258223a3SMatthew Dillon 
1798258223a3SMatthew Dillon int
1799cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
1800cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
1801258223a3SMatthew Dillon {
1802258223a3SMatthew Dillon 	int				i;
1803258223a3SMatthew Dillon 
1804cec85a37SMatthew Dillon 	for (i = 0; i < timeout; i++) {
1805258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
1806258223a3SMatthew Dillon 			return (0);
1807258223a3SMatthew Dillon 		DELAY(1000);
1808258223a3SMatthew Dillon 	}
1809258223a3SMatthew Dillon 
1810258223a3SMatthew Dillon 	return (1);
1811258223a3SMatthew Dillon }
1812258223a3SMatthew Dillon 
1813258223a3SMatthew Dillon struct ata_xfer *
1814258223a3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap)
1815258223a3SMatthew Dillon {
1816258223a3SMatthew Dillon 	/*struct ahci_softc	*sc = ap->ap_sc;*/
1817258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
1818258223a3SMatthew Dillon 
1819258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
1820258223a3SMatthew Dillon 	if (ccb == NULL) {
1821258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
1822258223a3SMatthew Dillon 		    PORTNAME(ap));
1823258223a3SMatthew Dillon 		return (NULL);
1824258223a3SMatthew Dillon 	}
1825258223a3SMatthew Dillon 
1826258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
1827258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
1828258223a3SMatthew Dillon 
1829258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
1830258223a3SMatthew Dillon 
1831258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
1832258223a3SMatthew Dillon }
1833258223a3SMatthew Dillon 
1834258223a3SMatthew Dillon void
1835258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
1836258223a3SMatthew Dillon {
1837258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
1838258223a3SMatthew Dillon 
1839258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
1840258223a3SMatthew Dillon 
1841258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
1842258223a3SMatthew Dillon }
1843258223a3SMatthew Dillon 
1844258223a3SMatthew Dillon int
1845258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
1846258223a3SMatthew Dillon {
1847258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
1848258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
1849258223a3SMatthew Dillon 
1850258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
1851258223a3SMatthew Dillon 
18524444122dSMatthew Dillon #if 0
18534444122dSMatthew Dillon 	kprintf("ahci_ata_cmd xa->flags %08x type %08x cmd=%08x\n",
18544444122dSMatthew Dillon 		xa->flags,
18554444122dSMatthew Dillon 		xa->fis->type,
18564444122dSMatthew Dillon 		xa->fis->command);
18574444122dSMatthew Dillon #endif
18584444122dSMatthew Dillon 
1859258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
1860258223a3SMatthew Dillon 		goto failcmd;
1861258223a3SMatthew Dillon 
1862258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
1863258223a3SMatthew Dillon 
1864258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
1865258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
1866258223a3SMatthew Dillon 
1867258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
1868258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
1869258223a3SMatthew Dillon 
1870258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
1871258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
1872258223a3SMatthew Dillon 
1873258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
1874258223a3SMatthew Dillon 		goto failcmd;
1875258223a3SMatthew Dillon 
1876258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
1877258223a3SMatthew Dillon 
1878258223a3SMatthew Dillon 	if (xa->flags & ATA_F_POLL) {
1879258223a3SMatthew Dillon 		ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout);
1880258223a3SMatthew Dillon 		return (ATA_COMPLETE);
1881258223a3SMatthew Dillon 	}
1882258223a3SMatthew Dillon 
1883258223a3SMatthew Dillon 	crit_enter();
1884258223a3SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_RUNNING;
1885258223a3SMatthew Dillon 	callout_reset(&ccb->ccb_timeout, xa->timeout,
1886258223a3SMatthew Dillon 		      ahci_ata_cmd_timeout_unserialized, ccb);
1887258223a3SMatthew Dillon 	ahci_start(ccb);
1888258223a3SMatthew Dillon 	crit_exit();
1889258223a3SMatthew Dillon 	return (ATA_QUEUED);
1890258223a3SMatthew Dillon 
1891258223a3SMatthew Dillon failcmd:
1892258223a3SMatthew Dillon 	crit_enter();
1893258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
1894258223a3SMatthew Dillon 	xa->complete(xa);
1895258223a3SMatthew Dillon 	crit_exit();
1896258223a3SMatthew Dillon 	return (ATA_ERROR);
1897258223a3SMatthew Dillon }
1898258223a3SMatthew Dillon 
1899258223a3SMatthew Dillon void
1900258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
1901258223a3SMatthew Dillon {
1902258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1903258223a3SMatthew Dillon 
1904258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
1905258223a3SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
1906258223a3SMatthew Dillon 		callout_stop(&ccb->ccb_timeout);
1907258223a3SMatthew Dillon 	}
1908258223a3SMatthew Dillon 
1909258223a3SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR)
1910258223a3SMatthew Dillon 		ahci_issue_pending_commands(ccb->ccb_port,
1911258223a3SMatthew Dillon 		    xa->flags & ATA_F_NCQ);
1912258223a3SMatthew Dillon 
1913258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
1914258223a3SMatthew Dillon 
1915258223a3SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP)
1916258223a3SMatthew Dillon 		xa->state = ATA_S_COMPLETE;
1917258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1918258223a3SMatthew Dillon 	else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
1919258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
1920258223a3SMatthew Dillon 			"slot %d\n",
1921258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
1922258223a3SMatthew Dillon #endif
1923258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
1924258223a3SMatthew Dillon 		xa->complete(xa);
1925258223a3SMatthew Dillon }
1926258223a3SMatthew Dillon 
1927258223a3SMatthew Dillon static void
1928258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
1929258223a3SMatthew Dillon {
1930258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
1931258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
1932258223a3SMatthew Dillon 
1933258223a3SMatthew Dillon 	lwkt_serialize_enter(&ap->ap_sc->sc_serializer);
1934258223a3SMatthew Dillon 	ahci_ata_cmd_timeout(arg);
1935258223a3SMatthew Dillon 	lwkt_serialize_exit(&ap->ap_sc->sc_serializer);
1936258223a3SMatthew Dillon }
1937258223a3SMatthew Dillon 
1938258223a3SMatthew Dillon static void
1939258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg)
1940258223a3SMatthew Dillon {
1941258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
1942258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
1943258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
1944258223a3SMatthew Dillon 	volatile u_int32_t	*active;
1945258223a3SMatthew Dillon 	int			ccb_was_started, ncq_cmd;
1946258223a3SMatthew Dillon 
1947258223a3SMatthew Dillon 	crit_enter();
1948258223a3SMatthew Dillon 	kprintf("CMD TIMEOUT port-cmd-reg 0x%b\n"
1949258223a3SMatthew Dillon 		"\tactive=%08x sactive=%08x\n"
1950258223a3SMatthew Dillon 		"\t  sact=%08x      ci=%08x\n",
1951258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
1952258223a3SMatthew Dillon 		ap->ap_active, ap->ap_sactive,
1953258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
1954258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI));
1955258223a3SMatthew Dillon 
19569e145b23SMatthew Dillon 	/*
19579e145b23SMatthew Dillon 	 * NOTE: Timeout will not be running if the command was polled.
19589e145b23SMatthew Dillon 	 */
19599e145b23SMatthew Dillon 	KKASSERT(xa->flags & (ATA_F_POLL|ATA_F_TIMEOUT_RUNNING));
1960258223a3SMatthew Dillon 	xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
1961258223a3SMatthew Dillon 	ncq_cmd = (xa->flags & ATA_F_NCQ);
1962258223a3SMatthew Dillon 	active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
1963258223a3SMatthew Dillon 
1964258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
1965258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
1966258223a3SMatthew Dillon 		    "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
1967258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
1968258223a3SMatthew Dillon 		ccb_was_started = 0;
1969258223a3SMatthew Dillon 	} else if (ccb->ccb_xa.state == ATA_S_ONCHIP && ahci_port_intr(ap,
1970258223a3SMatthew Dillon 	    1 << ccb->ccb_slot)) {
1971258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: final poll of port completed "
1972258223a3SMatthew Dillon 		    "command in slot %d\n", PORTNAME(ap), ccb->ccb_slot);
1973258223a3SMatthew Dillon 		goto ret;
1974258223a3SMatthew Dillon 	} else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
1975258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
1976258223a3SMatthew Dillon 		    "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
1977258223a3SMatthew Dillon 		    (*active & (1 << ccb->ccb_slot)) ?
1978258223a3SMatthew Dillon 		    " but slot is still active?" : ".");
1979258223a3SMatthew Dillon 		goto ret;
1980258223a3SMatthew Dillon 	} else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
1981258223a3SMatthew Dillon 		    (1 << ccb->ccb_slot)) == 0 &&
1982258223a3SMatthew Dillon 		   (*active & (1 << ccb->ccb_slot))) {
1983258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but "
1984258223a3SMatthew Dillon 		    "IRQ handler didn't detect it.  Why?\n", PORTNAME(ap),
1985258223a3SMatthew Dillon 		    ccb->ccb_slot);
1986258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
1987258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
1988258223a3SMatthew Dillon 		goto ret;
1989258223a3SMatthew Dillon 	} else {
1990258223a3SMatthew Dillon 		ccb_was_started = 1;
1991258223a3SMatthew Dillon 	}
1992258223a3SMatthew Dillon 
1993258223a3SMatthew Dillon 	/* Complete the slot with a timeout error. */
1994258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_TIMEOUT;
1995258223a3SMatthew Dillon 	*active &= ~(1 << ccb->ccb_slot);
1996258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
1997258223a3SMatthew Dillon 	ccb->ccb_done(ccb);	/* This won't issue pending commands or run the
1998258223a3SMatthew Dillon 				   atascsi completion. */
1999258223a3SMatthew Dillon 
2000258223a3SMatthew Dillon 	/* Reset port to abort running command. */
2001258223a3SMatthew Dillon 	if (ccb_was_started) {
2002258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
2003258223a3SMatthew Dillon 		    "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
2004258223a3SMatthew Dillon 		    : "", ccb->ccb_slot, *active);
2005*17eab71eSMatthew Dillon 		if (ahci_port_reset(ap, 0)) {
2006*17eab71eSMatthew Dillon 			kprintf("%s: Unable to reset during timeout, port "
2007*17eab71eSMatthew Dillon 				"bricked on us\n",
2008258223a3SMatthew Dillon 				PORTNAME(ap));
2009258223a3SMatthew Dillon 			ap->ap_state = AP_S_FATAL_ERROR;
2010258223a3SMatthew Dillon 		}
2011258223a3SMatthew Dillon 
2012258223a3SMatthew Dillon 		/* Restart any other commands that were aborted by the reset. */
2013258223a3SMatthew Dillon 		if (*active) {
2014258223a3SMatthew Dillon 			DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
2015258223a3SMatthew Dillon 			    "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
2016258223a3SMatthew Dillon 			    *active);
2017258223a3SMatthew Dillon 			if (ncq_cmd)
2018258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, *active);
2019258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, *active);
2020258223a3SMatthew Dillon 		}
2021258223a3SMatthew Dillon 	}
2022258223a3SMatthew Dillon 
2023258223a3SMatthew Dillon 	/* Issue any pending commands now. */
2024258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
2025258223a3SMatthew Dillon 	if (ccb_was_started)
2026258223a3SMatthew Dillon 		ahci_issue_pending_commands(ap, ncq_cmd);
2027258223a3SMatthew Dillon 	else if (ap->ap_active == 0)
2028258223a3SMatthew Dillon 		ahci_issue_pending_ncq_commands(ap);
2029258223a3SMatthew Dillon 
2030258223a3SMatthew Dillon 	/* Complete the timed out ata_xfer I/O (may generate new I/O). */
2031258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
2032258223a3SMatthew Dillon 	xa->complete(xa);
2033258223a3SMatthew Dillon 
2034258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
2035258223a3SMatthew Dillon ret:
2036258223a3SMatthew Dillon 	crit_exit();
2037258223a3SMatthew Dillon }
2038258223a3SMatthew Dillon 
2039258223a3SMatthew Dillon void
2040258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
2041258223a3SMatthew Dillon {
2042258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_COMPLETE;
2043258223a3SMatthew Dillon }
2044