xref: /dflybsd-src/sys/dev/disk/ahci/ahci.c (revision f4553de13c8796ae9536441d5de2afa0aa352699)
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 
54*f4553de1SMatthew Dillon int	ahci_port_start(struct ahci_port *ap);
55*f4553de1SMatthew Dillon int	ahci_port_stop(struct ahci_port *ap, int stop_fis_rx);
56*f4553de1SMatthew Dillon int	ahci_port_clo(struct ahci_port *ap);
57*f4553de1SMatthew Dillon void	ahci_port_interrupt_enable(struct ahci_port *ap);
58258223a3SMatthew Dillon 
59258223a3SMatthew Dillon int	ahci_load_prdt(struct ahci_ccb *);
60258223a3SMatthew Dillon void	ahci_unload_prdt(struct ahci_ccb *);
61258223a3SMatthew Dillon static void ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs,
62258223a3SMatthew Dillon 				    int nsegs, int error);
63258223a3SMatthew Dillon void	ahci_start(struct ahci_ccb *);
6417eab71eSMatthew Dillon int	ahci_port_softreset(struct ahci_port *ap);
651980eff3SMatthew Dillon int	ahci_port_pmprobe(struct ahci_port *ap);
661980eff3SMatthew Dillon int	ahci_port_hardreset(struct ahci_port *ap, int hard);
67cf5f3a81SMatthew Dillon void	ahci_port_hardstop(struct ahci_port *ap);
68cf5f3a81SMatthew Dillon void	ahci_flush_tfd(struct ahci_port *ap);
69258223a3SMatthew Dillon 
70258223a3SMatthew Dillon static void ahci_ata_cmd_timeout_unserialized(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_err_ccb(struct ahci_port *);
76258223a3SMatthew Dillon void	ahci_put_err_ccb(struct ahci_ccb *);
77258223a3SMatthew Dillon 
78258223a3SMatthew Dillon int	ahci_port_read_ncq_error(struct ahci_port *, int *);
79258223a3SMatthew Dillon 
80258223a3SMatthew Dillon struct ahci_dmamem *ahci_dmamem_alloc(struct ahci_softc *, bus_dma_tag_t tag);
81258223a3SMatthew Dillon void	ahci_dmamem_free(struct ahci_softc *, struct ahci_dmamem *);
82258223a3SMatthew Dillon static void ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error);
83258223a3SMatthew Dillon 
84258223a3SMatthew Dillon void	ahci_empty_done(struct ahci_ccb *ccb);
85258223a3SMatthew Dillon void	ahci_ata_cmd_done(struct ahci_ccb *ccb);
86258223a3SMatthew Dillon 
87258223a3SMatthew Dillon /* Wait for all bits in _b to be cleared */
88cec85a37SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \
89cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
90cec85a37SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to,  _r, _b) \
91cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
92258223a3SMatthew Dillon 
93258223a3SMatthew Dillon /* Wait for all bits in _b to be set */
94cec85a37SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \
95cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
96cec85a37SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \
97cec85a37SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
98cec85a37SMatthew Dillon 
99cec85a37SMatthew Dillon #define AHCI_PWAIT_TIMEOUT	1000
100258223a3SMatthew Dillon 
101fd8bd957SMatthew Dillon /*
102fd8bd957SMatthew Dillon  * Initialize the global AHCI hardware.  This code does not set up any of
103fd8bd957SMatthew Dillon  * its ports.
104fd8bd957SMatthew Dillon  */
105258223a3SMatthew Dillon int
106258223a3SMatthew Dillon ahci_init(struct ahci_softc *sc)
107258223a3SMatthew Dillon {
108258223a3SMatthew Dillon 	u_int32_t	cap, pi;
109258223a3SMatthew Dillon 
110258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, " GHC 0x%b",
111258223a3SMatthew Dillon 		ahci_read(sc, AHCI_REG_GHC), AHCI_FMT_GHC);
112258223a3SMatthew Dillon 
113258223a3SMatthew Dillon 	/* save BIOS initialised parameters, enable staggered spin up */
114258223a3SMatthew Dillon 	cap = ahci_read(sc, AHCI_REG_CAP);
115258223a3SMatthew Dillon 	cap &= AHCI_REG_CAP_SMPS;
116258223a3SMatthew Dillon 	cap |= AHCI_REG_CAP_SSS;
117258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
118258223a3SMatthew Dillon 
11917eab71eSMatthew Dillon 	/*
12017eab71eSMatthew Dillon 	 * Unconditionally reset the controller, do not conditionalize on
12117eab71eSMatthew Dillon 	 * trying to figure it if it was previously active or not.
12217eab71eSMatthew Dillon 	 */
123258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR);
124258223a3SMatthew Dillon 	if (ahci_wait_ne(sc, AHCI_REG_GHC, AHCI_REG_GHC_HR,
125258223a3SMatthew Dillon 	    AHCI_REG_GHC_HR) != 0) {
126258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
127258223a3SMatthew Dillon 			      "unable to reset controller\n");
128258223a3SMatthew Dillon 		return (1);
129258223a3SMatthew Dillon 	}
130258223a3SMatthew Dillon 
131258223a3SMatthew Dillon 	/* enable ahci (global interrupts disabled) */
132258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE);
133258223a3SMatthew Dillon 
134258223a3SMatthew Dillon 	/* restore parameters */
135258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_CAP, cap);
136258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_PI, pi);
137258223a3SMatthew Dillon 
138258223a3SMatthew Dillon 	return (0);
139258223a3SMatthew Dillon }
140258223a3SMatthew Dillon 
141fd8bd957SMatthew Dillon /*
142fd8bd957SMatthew Dillon  * Allocate and initialize an AHCI port.
143fd8bd957SMatthew Dillon  */
144258223a3SMatthew Dillon int
145258223a3SMatthew Dillon ahci_port_alloc(struct ahci_softc *sc, u_int port)
146258223a3SMatthew Dillon {
147258223a3SMatthew Dillon 	struct ahci_port	*ap;
1481980eff3SMatthew Dillon 	struct ata_port		*at;
149258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
150258223a3SMatthew Dillon 	u_int64_t		dva;
151258223a3SMatthew Dillon 	u_int32_t		cmd;
152258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*hdr;
153258223a3SMatthew Dillon 	struct ahci_cmd_table	*table;
154258223a3SMatthew Dillon 	int	rc = ENOMEM;
155258223a3SMatthew Dillon 	int	error;
156258223a3SMatthew Dillon 	int	i;
157258223a3SMatthew Dillon 
158258223a3SMatthew Dillon 	ap = kmalloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO);
159258223a3SMatthew Dillon 	if (ap == NULL) {
160258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
161258223a3SMatthew Dillon 			      "unable to allocate memory for port %d\n",
162258223a3SMatthew Dillon 			      port);
163258223a3SMatthew Dillon 		goto reterr;
164258223a3SMatthew Dillon 	}
165258223a3SMatthew Dillon 
166258223a3SMatthew Dillon 	ksnprintf(ap->ap_name, sizeof(ap->ap_name), "%s%d.%d",
167258223a3SMatthew Dillon 		  device_get_name(sc->sc_dev),
168258223a3SMatthew Dillon 		  device_get_unit(sc->sc_dev),
169258223a3SMatthew Dillon 		  port);
170258223a3SMatthew Dillon 	sc->sc_ports[port] = ap;
171258223a3SMatthew Dillon 
1721980eff3SMatthew Dillon 	/*
1731980eff3SMatthew Dillon 	 * Allocate enough so we never have to reallocate, it makes
1741980eff3SMatthew Dillon 	 * it easier.
1751980eff3SMatthew Dillon 	 *
1761980eff3SMatthew Dillon 	 * ap_pmcount will be reduced by the scan if we encounter the
1771980eff3SMatthew Dillon 	 * port multiplier port prior to target 15.
1781980eff3SMatthew Dillon 	 */
1791980eff3SMatthew Dillon 	if (ap->ap_ata == NULL) {
1801980eff3SMatthew Dillon 		ap->ap_ata = kmalloc(sizeof(*ap->ap_ata) * AHCI_MAX_PMPORTS,
1811980eff3SMatthew Dillon 				     M_DEVBUF, M_INTWAIT | M_ZERO);
1821980eff3SMatthew Dillon 		for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
1831980eff3SMatthew Dillon 			at = &ap->ap_ata[i];
1841980eff3SMatthew Dillon 			at->at_ahci_port = ap;
1851980eff3SMatthew Dillon 			at->at_target = i;
1863209f581SMatthew Dillon 			at->at_probe = ATA_PROBE_NEED_INIT;
1871980eff3SMatthew Dillon 			ksnprintf(at->at_name, sizeof(at->at_name),
1881980eff3SMatthew Dillon 				  "%s.%d", ap->ap_name, i);
1891980eff3SMatthew Dillon 		}
1901980eff3SMatthew Dillon 	}
191258223a3SMatthew Dillon 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
192258223a3SMatthew Dillon 	    AHCI_PORT_REGION(port), AHCI_PORT_SIZE, &ap->ap_ioh) != 0) {
193258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
194258223a3SMatthew Dillon 			      "unable to create register window for port %d\n",
195258223a3SMatthew Dillon 			      port);
196258223a3SMatthew Dillon 		goto freeport;
197258223a3SMatthew Dillon 	}
198258223a3SMatthew Dillon 
199258223a3SMatthew Dillon 	ap->ap_sc = sc;
200258223a3SMatthew Dillon 	ap->ap_num = port;
2013209f581SMatthew Dillon 	ap->ap_probe = ATA_PROBE_NEED_INIT;
202258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_free);
203258223a3SMatthew Dillon 	TAILQ_INIT(&ap->ap_ccb_pending);
204258223a3SMatthew Dillon 	lockinit(&ap->ap_ccb_lock, "ahcipo", 0, 0);
205258223a3SMatthew Dillon 
206258223a3SMatthew Dillon 	/* Disable port interrupts */
207258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, 0);
208258223a3SMatthew Dillon 
20917eab71eSMatthew Dillon 	/*
21017eab71eSMatthew Dillon 	 * Sec 10.1.2 - deinitialise port if it is already running
21117eab71eSMatthew Dillon 	 */
212258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
213258223a3SMatthew Dillon 	if ((cmd & (AHCI_PREG_CMD_ST | AHCI_PREG_CMD_CR |
214258223a3SMatthew Dillon 		    AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_FR)) ||
215258223a3SMatthew Dillon 	    (ahci_pread(ap, AHCI_PREG_SCTL) & AHCI_PREG_SCTL_DET)) {
216258223a3SMatthew Dillon 		int r;
217258223a3SMatthew Dillon 
218258223a3SMatthew Dillon 		r = ahci_port_stop(ap, 1);
219258223a3SMatthew Dillon 		if (r) {
220258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
221258223a3SMatthew Dillon 				  "unable to disable %s, ignoring port %d\n",
222258223a3SMatthew Dillon 				  ((r == 2) ? "CR" : "FR"), port);
223258223a3SMatthew Dillon 			rc = ENXIO;
224258223a3SMatthew Dillon 			goto freeport;
225258223a3SMatthew Dillon 		}
226258223a3SMatthew Dillon 
227258223a3SMatthew Dillon 		/* Write DET to zero */
228cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
229258223a3SMatthew Dillon 	}
230258223a3SMatthew Dillon 
231258223a3SMatthew Dillon 	/* Allocate RFIS */
232258223a3SMatthew Dillon 	ap->ap_dmamem_rfis = ahci_dmamem_alloc(sc, sc->sc_tag_rfis);
233258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis == NULL) {
234cf5f3a81SMatthew Dillon 		kprintf("%s: NORFIS\n", PORTNAME(ap));
235258223a3SMatthew Dillon 		goto nomem;
236258223a3SMatthew Dillon 	}
237258223a3SMatthew Dillon 
238258223a3SMatthew Dillon 	/* Setup RFIS base address */
239258223a3SMatthew Dillon 	ap->ap_rfis = (struct ahci_rfis *) AHCI_DMA_KVA(ap->ap_dmamem_rfis);
240258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_rfis);
241258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FBU, (u_int32_t)(dva >> 32));
242258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_FB, (u_int32_t)dva);
243258223a3SMatthew Dillon 
244258223a3SMatthew Dillon 	/* Enable FIS reception and activate port. */
245258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
2461980eff3SMatthew Dillon 	cmd &= ~(AHCI_PREG_CMD_CLO | AHCI_PREG_CMD_PMA);
247258223a3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_FRE | AHCI_PREG_CMD_POD | AHCI_PREG_CMD_SUD;
248258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_ICC_ACTIVE);
249258223a3SMatthew Dillon 
250258223a3SMatthew Dillon 	/* Check whether port activated.  Skip it if not. */
251258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
252258223a3SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
253cf5f3a81SMatthew Dillon 		kprintf("%s: NOT-ACTIVATED\n", PORTNAME(ap));
254258223a3SMatthew Dillon 		rc = ENXIO;
255258223a3SMatthew Dillon 		goto freeport;
256258223a3SMatthew Dillon 	}
257258223a3SMatthew Dillon 
258258223a3SMatthew Dillon 	/* Allocate a CCB for each command slot */
259258223a3SMatthew Dillon 	ap->ap_ccbs = kmalloc(sizeof(struct ahci_ccb) * sc->sc_ncmds, M_DEVBUF,
260258223a3SMatthew Dillon 			      M_WAITOK | M_ZERO);
261258223a3SMatthew Dillon 	if (ap->ap_ccbs == NULL) {
262258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
263258223a3SMatthew Dillon 			      "unable to allocate command list for port %d\n",
264258223a3SMatthew Dillon 			      port);
265258223a3SMatthew Dillon 		goto freeport;
266258223a3SMatthew Dillon 	}
267258223a3SMatthew Dillon 
268258223a3SMatthew Dillon 	/* Command List Structures and Command Tables */
269258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_list = ahci_dmamem_alloc(sc, sc->sc_tag_cmdh);
270258223a3SMatthew Dillon 	ap->ap_dmamem_cmd_table = ahci_dmamem_alloc(sc, sc->sc_tag_cmdt);
271258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table == NULL ||
272258223a3SMatthew Dillon 	    ap->ap_dmamem_cmd_list == NULL) {
273258223a3SMatthew Dillon nomem:
274258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
275258223a3SMatthew Dillon 			      "unable to allocate DMA memory for port %d\n",
276258223a3SMatthew Dillon 			      port);
277258223a3SMatthew Dillon 		goto freeport;
278258223a3SMatthew Dillon 	}
279258223a3SMatthew Dillon 
280258223a3SMatthew Dillon 	/* Setup command list base address */
281258223a3SMatthew Dillon 	dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_list);
282258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLBU, (u_int32_t)(dva >> 32));
283258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CLB, (u_int32_t)dva);
284258223a3SMatthew Dillon 
285258223a3SMatthew Dillon 	/* Split CCB allocation into CCBs and assign to command header/table */
286258223a3SMatthew Dillon 	hdr = AHCI_DMA_KVA(ap->ap_dmamem_cmd_list);
287258223a3SMatthew Dillon 	table = AHCI_DMA_KVA(ap->ap_dmamem_cmd_table);
288258223a3SMatthew Dillon 	for (i = 0; i < sc->sc_ncmds; i++) {
289258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[i];
290258223a3SMatthew Dillon 
291258223a3SMatthew Dillon 		error = bus_dmamap_create(sc->sc_tag_data, BUS_DMA_ALLOCNOW,
292258223a3SMatthew Dillon 					  &ccb->ccb_dmamap);
293258223a3SMatthew Dillon 		if (error) {
294258223a3SMatthew Dillon 			device_printf(sc->sc_dev,
295258223a3SMatthew Dillon 				      "unable to create dmamap for port %d "
296258223a3SMatthew Dillon 				      "ccb %d\n", port, i);
297258223a3SMatthew Dillon 			goto freeport;
298258223a3SMatthew Dillon 		}
299258223a3SMatthew Dillon 
300258223a3SMatthew Dillon 		callout_init(&ccb->ccb_timeout);
301258223a3SMatthew Dillon 		ccb->ccb_slot = i;
302258223a3SMatthew Dillon 		ccb->ccb_port = ap;
303258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr = &hdr[i];
304258223a3SMatthew Dillon 		ccb->ccb_cmd_table = &table[i];
305258223a3SMatthew Dillon 		dva = AHCI_DMA_DVA(ap->ap_dmamem_cmd_table) +
306258223a3SMatthew Dillon 		    ccb->ccb_slot * sizeof(struct ahci_cmd_table);
307258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_hi = htole32((u_int32_t)(dva >> 32));
308258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->ctba_lo = htole32((u_int32_t)dva);
309258223a3SMatthew Dillon 
310258223a3SMatthew Dillon 		ccb->ccb_xa.fis =
311258223a3SMatthew Dillon 		    (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
312258223a3SMatthew Dillon 		ccb->ccb_xa.packetcmd = ccb->ccb_cmd_table->acmd;
313258223a3SMatthew Dillon 		ccb->ccb_xa.tag = i;
314258223a3SMatthew Dillon 
315258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_COMPLETE;
316258223a3SMatthew Dillon 		ahci_put_ccb(ccb);
317258223a3SMatthew Dillon 	}
318258223a3SMatthew Dillon 
319258223a3SMatthew Dillon 	/* Wait for ICC change to complete */
320258223a3SMatthew Dillon 	ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_ICC);
321258223a3SMatthew Dillon 
322fd8bd957SMatthew Dillon 	/*
323*f4553de1SMatthew Dillon 	 * Start the port.  The helper thread will call ahci_port_init()
324*f4553de1SMatthew Dillon 	 * so the ports can all be started in parallel.  A failure by
325*f4553de1SMatthew Dillon 	 * ahci_port_init() does not deallocate the port since we still
326*f4553de1SMatthew Dillon 	 * want hot-plug events.
327fd8bd957SMatthew Dillon 	 */
328*f4553de1SMatthew Dillon 	ahci_os_start_port(ap);
329fd8bd957SMatthew Dillon 	return(0);
330fd8bd957SMatthew Dillon freeport:
331fd8bd957SMatthew Dillon 	ahci_port_free(sc, port);
332fd8bd957SMatthew Dillon reterr:
333fd8bd957SMatthew Dillon 	return (rc);
334fd8bd957SMatthew Dillon }
335fd8bd957SMatthew Dillon 
336fd8bd957SMatthew Dillon /*
337fd8bd957SMatthew Dillon  * [re]initialize an idle port.  No CCBs should be active.
338fd8bd957SMatthew Dillon  *
3391980eff3SMatthew Dillon  * If at is NULL we are initializing a directly connected port, otherwise
3401980eff3SMatthew Dillon  * we are indirectly initializing a port multiplier port.
3411980eff3SMatthew Dillon  *
342fd8bd957SMatthew Dillon  * This function is called during the initial port allocation sequence
343fd8bd957SMatthew Dillon  * and is also called on hot-plug insertion.  We take no chances and
344fd8bd957SMatthew Dillon  * use a portreset instead of a softreset.
345fd8bd957SMatthew Dillon  *
34622181ab7SMatthew Dillon  * This function is the only way to move a failed port back to active
34722181ab7SMatthew Dillon  * status.
34822181ab7SMatthew Dillon  *
349fd8bd957SMatthew Dillon  * Returns 0 if a device is successfully detected.
350fd8bd957SMatthew Dillon  */
351fd8bd957SMatthew Dillon int
352*f4553de1SMatthew Dillon ahci_port_init(struct ahci_port *ap, struct ata_port *atx)
353fd8bd957SMatthew Dillon {
354fd8bd957SMatthew Dillon 	int rc;
355fd8bd957SMatthew Dillon 
356fd8bd957SMatthew Dillon 	/*
3571980eff3SMatthew Dillon 	 * Clear all notification bits
358fd8bd957SMatthew Dillon 	 */
3591980eff3SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
3601980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SNTF, -1);
3611980eff3SMatthew Dillon 
3621980eff3SMatthew Dillon 	/*
3631980eff3SMatthew Dillon 	 * Hard-reset the port.  If a device is detected but it is busy
3641980eff3SMatthew Dillon 	 * we try a second time, this time cycling the phy as well.
3651980eff3SMatthew Dillon 	 */
366*f4553de1SMatthew Dillon 	if (atx)
367*f4553de1SMatthew Dillon 		atx->at_probe = ATA_PROBE_NEED_HARD_RESET;
368*f4553de1SMatthew Dillon 	else
3691980eff3SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_HARD_RESET;
370*f4553de1SMatthew Dillon 	rc = ahci_port_reset(ap, atx, 1);
3711980eff3SMatthew Dillon 	if (rc == EBUSY) {
372*f4553de1SMatthew Dillon 		rc = ahci_port_reset(ap, atx, 2);
37317eab71eSMatthew Dillon 	}
374fd8bd957SMatthew Dillon 
375258223a3SMatthew Dillon 	switch (rc) {
376258223a3SMatthew Dillon 	case ENODEV:
377fd8bd957SMatthew Dillon 		/*
378fd8bd957SMatthew Dillon 		 * We had problems talking to the device on the port.
379fd8bd957SMatthew Dillon 		 */
380258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
381258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV_NE:
382419cb1abSMatthew Dillon 			kprintf("%s: Device not communicating\n", PORTNAME(ap));
383258223a3SMatthew Dillon 			break;
384258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_PHYOFFLINE:
385419cb1abSMatthew Dillon 			kprintf("%s: PHY offline\n", PORTNAME(ap));
386258223a3SMatthew Dillon 			break;
387258223a3SMatthew Dillon 		default:
388419cb1abSMatthew Dillon 			kprintf("%s: No device detected\n", PORTNAME(ap));
389258223a3SMatthew Dillon 			break;
390258223a3SMatthew Dillon 		}
391258223a3SMatthew Dillon 		break;
392258223a3SMatthew Dillon 
393258223a3SMatthew Dillon 	case EBUSY:
394fd8bd957SMatthew Dillon 		/*
39517eab71eSMatthew Dillon 		 * The device on the port is still telling us its busy,
39617eab71eSMatthew Dillon 		 * which means that it is not properly handling a SATA
39717eab71eSMatthew Dillon 		 * port COMRESET.
398fd8bd957SMatthew Dillon 		 *
39917eab71eSMatthew Dillon 		 * It may be possible to softreset the device using CLO
40017eab71eSMatthew Dillon 		 * and a device reset command.
401fd8bd957SMatthew Dillon 		 */
40217eab71eSMatthew Dillon 		kprintf("%s: Device on port is bricked, trying softreset\n",
40317eab71eSMatthew Dillon 			PORTNAME(ap));
404258223a3SMatthew Dillon 
405*f4553de1SMatthew Dillon 		rc = ahci_port_reset(ap, atx, 0);
406258223a3SMatthew Dillon 		if (rc) {
40717eab71eSMatthew Dillon 			kprintf("%s: Unable unbrick device\n",
408fd8bd957SMatthew Dillon 				PORTNAME(ap));
409fd8bd957SMatthew Dillon 		} else {
41017eab71eSMatthew Dillon 			kprintf("%s: Successfully unbricked\n",
411fd8bd957SMatthew Dillon 				PORTNAME(ap));
412258223a3SMatthew Dillon 		}
413258223a3SMatthew Dillon 		break;
414258223a3SMatthew Dillon 
415258223a3SMatthew Dillon 	default:
416258223a3SMatthew Dillon 		break;
417258223a3SMatthew Dillon 	}
418258223a3SMatthew Dillon 
419258223a3SMatthew Dillon 	/*
42017eab71eSMatthew Dillon 	 * Command transfers can only be enabled if a device was successfully
42117eab71eSMatthew Dillon 	 * detected.
4221980eff3SMatthew Dillon 	 *
4231980eff3SMatthew Dillon 	 * Allocate or deallocate the ap_ata array here too.
424258223a3SMatthew Dillon 	 */
4251980eff3SMatthew Dillon 	switch(ap->ap_type) {
4261980eff3SMatthew Dillon 	case ATA_PORT_T_NONE:
4271980eff3SMatthew Dillon 		ap->ap_pmcount = 0;
4281980eff3SMatthew Dillon 		break;
4291980eff3SMatthew Dillon 	case ATA_PORT_T_PM:
4301980eff3SMatthew Dillon 		/* already set */
4311980eff3SMatthew Dillon 		break;
4321980eff3SMatthew Dillon 	default:
4331980eff3SMatthew Dillon 		ap->ap_pmcount = 1;
4341980eff3SMatthew Dillon 		break;
4351980eff3SMatthew Dillon 	}
4361980eff3SMatthew Dillon 
4371980eff3SMatthew Dillon 	/*
4381980eff3SMatthew Dillon 	 * Start the port if we succeeded.
4391980eff3SMatthew Dillon 	 *
4401980eff3SMatthew Dillon 	 * There's nothing to start for devices behind a port multiplier.
4411980eff3SMatthew Dillon 	 */
442*f4553de1SMatthew Dillon 	if (rc == 0 && atx == NULL) {
44317eab71eSMatthew Dillon 		if (ahci_port_start(ap)) {
444fd8bd957SMatthew Dillon 			kprintf("%s: failed to start command DMA on port, "
445fd8bd957SMatthew Dillon 			        "disabling\n", PORTNAME(ap));
446258223a3SMatthew Dillon 			rc = ENXIO;	/* couldn't start port */
447258223a3SMatthew Dillon 		}
448258223a3SMatthew Dillon 	}
449258223a3SMatthew Dillon 
45017eab71eSMatthew Dillon 	/*
4513209f581SMatthew Dillon 	 * Flush interrupts on the port. XXX
4521980eff3SMatthew Dillon 	 *
4531980eff3SMatthew Dillon 	 * Enable interrupts on the port whether a device is sitting on
4541980eff3SMatthew Dillon 	 * it or not, to handle hot-plug events.
45517eab71eSMatthew Dillon 	 */
456*f4553de1SMatthew Dillon 	if (atx == NULL) {
457258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
458fd8bd957SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_IS, 1 << ap->ap_num);
459258223a3SMatthew Dillon 
460*f4553de1SMatthew Dillon 		ahci_port_interrupt_enable(ap);
461*f4553de1SMatthew Dillon 	}
462*f4553de1SMatthew Dillon 	return(rc);
463*f4553de1SMatthew Dillon }
464*f4553de1SMatthew Dillon 
465*f4553de1SMatthew Dillon /*
466*f4553de1SMatthew Dillon  * Enable or re-enable interrupts on a port.
467*f4553de1SMatthew Dillon  *
468*f4553de1SMatthew Dillon  * This routine is called from the port initialization code or from the
469*f4553de1SMatthew Dillon  * helper thread as the real interrupt may be forced to turn off certain
470*f4553de1SMatthew Dillon  * interrupt sources.
471*f4553de1SMatthew Dillon  */
472*f4553de1SMatthew Dillon void
473*f4553de1SMatthew Dillon ahci_port_interrupt_enable(struct ahci_port *ap)
474*f4553de1SMatthew Dillon {
475*f4553de1SMatthew Dillon 	u_int32_t data;
476*f4553de1SMatthew Dillon 
4771980eff3SMatthew Dillon 	data = AHCI_PREG_IE_TFEE | AHCI_PREG_IE_HBFE |
478258223a3SMatthew Dillon 	       AHCI_PREG_IE_IFE | AHCI_PREG_IE_OFE |
479258223a3SMatthew Dillon 	       AHCI_PREG_IE_DPE | AHCI_PREG_IE_UFE |
480258223a3SMatthew Dillon 	       AHCI_PREG_IE_PCE | AHCI_PREG_IE_PRCE |
4811980eff3SMatthew Dillon 	       AHCI_PREG_IE_DHRE;
4821980eff3SMatthew Dillon 	if (ap->ap_sc->sc_cap & AHCI_REG_CAP_SSNTF)
4831980eff3SMatthew Dillon 		data |= AHCI_PREG_IE_SDBE;
484258223a3SMatthew Dillon #ifdef AHCI_COALESCE
4851980eff3SMatthew Dillon 	if (sc->sc_ccc_ports & (1 << port)
4861980eff3SMatthew Dillon 		data &= ~(AHCI_PREG_IE_SDBE | AHCI_PREG_IE_DHRE);
487258223a3SMatthew Dillon #endif
4881980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_IE, data);
4891980eff3SMatthew Dillon }
490258223a3SMatthew Dillon 
491fd8bd957SMatthew Dillon /*
4923209f581SMatthew Dillon  * Run the port / target state machine from a main context.
4933209f581SMatthew Dillon  *
4943209f581SMatthew Dillon  * The state machine for the port is always run.
4953209f581SMatthew Dillon  *
4963209f581SMatthew Dillon  * If atx is non-NULL run the state machine for a particular target.
4973209f581SMatthew Dillon  * If atx is NULL run the state machine for all targets.
4983209f581SMatthew Dillon  */
4993209f581SMatthew Dillon void
5003209f581SMatthew Dillon ahci_port_state_machine(struct ahci_port *ap)
5013209f581SMatthew Dillon {
5023209f581SMatthew Dillon 	struct ata_port *at;
5033209f581SMatthew Dillon 	u_int32_t data;
5043209f581SMatthew Dillon 	int target;
5053209f581SMatthew Dillon 	int didsleep;
5063209f581SMatthew Dillon 
5073209f581SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE) {
5083209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_INIT) {
5093209f581SMatthew Dillon 			for (target = 0; target < AHCI_MAX_PMPORTS; ++target) {
5103209f581SMatthew Dillon 				at = &ap->ap_ata[target];
5113209f581SMatthew Dillon 				at->at_probe = ATA_PROBE_NEED_INIT;
5123209f581SMatthew Dillon 			}
5133209f581SMatthew Dillon 			ahci_port_init(ap, NULL);
5143209f581SMatthew Dillon 		}
5153209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_HARD_RESET)
5163209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 1);
5173209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_SOFT_RESET)
5183209f581SMatthew Dillon 			ahci_port_reset(ap, NULL, 0);
5193209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
5203209f581SMatthew Dillon 			ahci_cam_probe(ap, NULL);
5213209f581SMatthew Dillon 	}
5223209f581SMatthew Dillon 	if (ap->ap_type != ATA_PORT_T_PM) {
5233209f581SMatthew Dillon 		if (ap->ap_probe == ATA_PROBE_FAILED) {
5243209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 0);
525*f4553de1SMatthew Dillon 		} else if (ap->ap_probe >= ATA_PROBE_NEED_IDENT) {
5263209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, 1);
5273209f581SMatthew Dillon 		}
5283209f581SMatthew Dillon 		return;
5293209f581SMatthew Dillon 	}
5303209f581SMatthew Dillon 
5313209f581SMatthew Dillon 	for (;;) {
5323209f581SMatthew Dillon 		if (ahci_pm_read(ap, 15, AHCI_PMREG_EINFO, &data)) {
5333209f581SMatthew Dillon 			kprintf("%s: PM unable to read hot-plug bitmap\n",
5343209f581SMatthew Dillon 				PORTNAME(ap));
5353209f581SMatthew Dillon 			break;
5363209f581SMatthew Dillon 		}
5373209f581SMatthew Dillon 		data &= (1 << ap->ap_pmcount) - 1;
5383209f581SMatthew Dillon 
5393209f581SMatthew Dillon 		/*
5403209f581SMatthew Dillon 		 * Stop if no ports on the target have indicated a state
5413209f581SMatthew Dillon 		 * change.
5423209f581SMatthew Dillon 		 */
5433209f581SMatthew Dillon 		if (data == 0)
5443209f581SMatthew Dillon 			break;
5453209f581SMatthew Dillon 
5463209f581SMatthew Dillon 		/*
5473209f581SMatthew Dillon 		 * New devices showing up in the bitmap require some spin-up
5483209f581SMatthew Dillon 		 * time before we start probing them.  Reset didsleep.  The
5493209f581SMatthew Dillon 		 * first new device we detect will sleep before probing.
5503209f581SMatthew Dillon 		 */
5513209f581SMatthew Dillon 		didsleep = 0;
5523209f581SMatthew Dillon 
5533209f581SMatthew Dillon 		for (target = 0; target < ap->ap_pmcount; ++target) {
5543209f581SMatthew Dillon 			at = &ap->ap_ata[target];
5553209f581SMatthew Dillon 
5563209f581SMatthew Dillon 			/*
5573209f581SMatthew Dillon 			 * Check the target state for targets behind the PM
5583209f581SMatthew Dillon 			 * which have changed state.  This will adjust
5593209f581SMatthew Dillon 			 * at_probe and set ATA_PORT_F_RESCAN
5603209f581SMatthew Dillon 			 *
5613209f581SMatthew Dillon 			 * We want to wait at least 4 seconds before probing
5623209f581SMatthew Dillon 			 * a newly inserted device.  If the check status
5633209f581SMatthew Dillon 			 * indicates a device is present and in need of a
5643209f581SMatthew Dillon 			 * hard reset, we make sure we have slept before
5653209f581SMatthew Dillon 			 * continuing.
5663209f581SMatthew Dillon 			 */
5673209f581SMatthew Dillon 			if (data & (1 << target)) {
5683209f581SMatthew Dillon 				ahci_pm_check_good(ap, target);
5693209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_HARD_RESET) {
5703209f581SMatthew Dillon 					if (didsleep == 0) {
5713209f581SMatthew Dillon 						didsleep = 1;
5723209f581SMatthew Dillon 						ahci_os_sleep(4000);
5733209f581SMatthew Dillon 					}
5743209f581SMatthew Dillon 				}
5753209f581SMatthew Dillon 			}
5763209f581SMatthew Dillon 
5773209f581SMatthew Dillon 			/*
5783209f581SMatthew Dillon 			 * Run through the state machine as necessary.
5793209f581SMatthew Dillon 			 */
5803209f581SMatthew Dillon 			if (at->at_type == ATA_PORT_T_NONE &&
5813209f581SMatthew Dillon 			    at->at_probe != ATA_PROBE_FAILED) {
5823209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_INIT)
5833209f581SMatthew Dillon 					ahci_port_init(ap, at);
5843209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_HARD_RESET)
5853209f581SMatthew Dillon 					ahci_port_reset(ap, at, 1);
5863209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_SOFT_RESET)
5873209f581SMatthew Dillon 					ahci_port_reset(ap, at, 0);
5883209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_NEED_IDENT)
5893209f581SMatthew Dillon 					ahci_cam_probe(ap, at);
5903209f581SMatthew Dillon 			}
5913209f581SMatthew Dillon 
5923209f581SMatthew Dillon 			if (data & (1 << target)) {
5933209f581SMatthew Dillon 				kprintf("%s: HOTPLUG event, ",
5943209f581SMatthew Dillon 					ATANAME(ap, at));
595*f4553de1SMatthew Dillon 				if (at->at_probe >= ATA_PROBE_NEED_IDENT)
5963209f581SMatthew Dillon 					kprintf("device inserted\n");
5973209f581SMatthew Dillon 				else
5983209f581SMatthew Dillon 					kprintf("device removed\n");
5993209f581SMatthew Dillon 			}
6003209f581SMatthew Dillon 
6013209f581SMatthew Dillon 			/*
6023209f581SMatthew Dillon 			 * Initial conditions set automatic add/rem
6033209f581SMatthew Dillon 			 */
6043209f581SMatthew Dillon 			if (at->at_probe <= ATA_PROBE_NEED_HARD_RESET)
6053209f581SMatthew Dillon 				at->at_features |= ATA_PORT_F_RESCAN;
6063209f581SMatthew Dillon 
6073209f581SMatthew Dillon 			/*
6083209f581SMatthew Dillon 			 * add or remove from CAM
6093209f581SMatthew Dillon 			 */
6103209f581SMatthew Dillon 			if (at->at_features & ATA_PORT_F_RESCAN) {
6113209f581SMatthew Dillon 				at->at_features &= ~ATA_PORT_F_RESCAN;
6123209f581SMatthew Dillon 				if (at->at_probe == ATA_PROBE_FAILED) {
6133209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 0);
614*f4553de1SMatthew Dillon 				} else if (at->at_probe >= ATA_PROBE_NEED_IDENT) {
6153209f581SMatthew Dillon 					ahci_cam_changed(ap, at, 1);
6163209f581SMatthew Dillon 				}
6173209f581SMatthew Dillon 			}
6183209f581SMatthew Dillon 		}
6193209f581SMatthew Dillon 	}
6203209f581SMatthew Dillon }
6213209f581SMatthew Dillon 
6223209f581SMatthew Dillon 
6233209f581SMatthew Dillon /*
624fd8bd957SMatthew Dillon  * De-initialize and detach a port.
625fd8bd957SMatthew Dillon  */
626258223a3SMatthew Dillon void
627258223a3SMatthew Dillon ahci_port_free(struct ahci_softc *sc, u_int port)
628258223a3SMatthew Dillon {
629258223a3SMatthew Dillon 	struct ahci_port		*ap = sc->sc_ports[port];
630258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
631258223a3SMatthew Dillon 
63217eab71eSMatthew Dillon 	/*
63317eab71eSMatthew Dillon 	 * Ensure port is disabled and its interrupts are all flushed.
63417eab71eSMatthew Dillon 	 */
635258223a3SMatthew Dillon 	if (ap->ap_sc) {
63617eab71eSMatthew Dillon 		ahci_port_stop(ap, 1);
637*f4553de1SMatthew Dillon 		ahci_os_stop_port(ap);
638258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, 0);
639258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IE, 0);
640258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, ahci_pread(ap, AHCI_PREG_IS));
641258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_IS, 1 << port);
642258223a3SMatthew Dillon 	}
643258223a3SMatthew Dillon 
644258223a3SMatthew Dillon 	if (ap->ap_ccbs) {
645258223a3SMatthew Dillon 		while ((ccb = ahci_get_ccb(ap)) != NULL) {
646258223a3SMatthew Dillon 			if (ccb->ccb_dmamap) {
647258223a3SMatthew Dillon 				bus_dmamap_destroy(sc->sc_tag_data,
648258223a3SMatthew Dillon 						   ccb->ccb_dmamap);
649258223a3SMatthew Dillon 				ccb->ccb_dmamap = NULL;
650258223a3SMatthew Dillon 			}
651258223a3SMatthew Dillon 		}
652258223a3SMatthew Dillon 		kfree(ap->ap_ccbs, M_DEVBUF);
653258223a3SMatthew Dillon 		ap->ap_ccbs = NULL;
654258223a3SMatthew Dillon 	}
655258223a3SMatthew Dillon 
656258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_list) {
657258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_list);
658258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_list = NULL;
659258223a3SMatthew Dillon 	}
660258223a3SMatthew Dillon 	if (ap->ap_dmamem_rfis) {
661258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_rfis);
662258223a3SMatthew Dillon 		ap->ap_dmamem_rfis = NULL;
663258223a3SMatthew Dillon 	}
664258223a3SMatthew Dillon 	if (ap->ap_dmamem_cmd_table) {
665258223a3SMatthew Dillon 		ahci_dmamem_free(sc, ap->ap_dmamem_cmd_table);
666258223a3SMatthew Dillon 		ap->ap_dmamem_cmd_table = NULL;
667258223a3SMatthew Dillon 	}
6681980eff3SMatthew Dillon 	if (ap->ap_ata) {
6691980eff3SMatthew Dillon 		kfree(ap->ap_ata, M_DEVBUF);
6701980eff3SMatthew Dillon 		ap->ap_ata = NULL;
6711980eff3SMatthew Dillon 	}
672258223a3SMatthew Dillon 
673258223a3SMatthew Dillon 	/* bus_space(9) says we dont free the subregions handle */
674258223a3SMatthew Dillon 
675258223a3SMatthew Dillon 	kfree(ap, M_DEVBUF);
676258223a3SMatthew Dillon 	sc->sc_ports[port] = NULL;
677258223a3SMatthew Dillon }
678258223a3SMatthew Dillon 
679fd8bd957SMatthew Dillon /*
680fd8bd957SMatthew Dillon  * Start high-level command processing on the port
681fd8bd957SMatthew Dillon  */
682258223a3SMatthew Dillon int
68317eab71eSMatthew Dillon ahci_port_start(struct ahci_port *ap)
684258223a3SMatthew Dillon {
6858bf6a3ffSMatthew Dillon 	u_int32_t	r, oldr, s, olds, is, oldis, tfd, oldtfd;
686258223a3SMatthew Dillon 
68717eab71eSMatthew Dillon 	/*
68817eab71eSMatthew Dillon 	 * FRE must be turned on before ST.  Wait for FR to go active
68917eab71eSMatthew Dillon 	 * before turning on ST.  The spec doesn't seem to think this
69017eab71eSMatthew Dillon 	 * is necessary but waiting here avoids an on-off race in the
69117eab71eSMatthew Dillon 	 * ahci_port_stop() code.
69217eab71eSMatthew Dillon 	 */
693cec07d75SMatthew Dillon 	 /* XXX REMOVE ME */
6941980eff3SMatthew Dillon 	olds = ahci_pread(ap, AHCI_PREG_SERR);
6951980eff3SMatthew Dillon 	oldis= ahci_pread(ap, AHCI_PREG_IS);
6968bf6a3ffSMatthew Dillon 	oldtfd = ahci_pread(ap, AHCI_PREG_TFD);
6971980eff3SMatthew Dillon 	oldr = r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
69817eab71eSMatthew Dillon 	if ((r & AHCI_PREG_CMD_FRE) == 0) {
699258223a3SMatthew Dillon 		r |= AHCI_PREG_CMD_FRE;
70017eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
70117eab71eSMatthew Dillon 	}
70217eab71eSMatthew Dillon 	if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0) {
70317eab71eSMatthew Dillon 		if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
70417eab71eSMatthew Dillon 			kprintf("%s: Cannot start FIS reception\n",
70517eab71eSMatthew Dillon 				PORTNAME(ap));
70617eab71eSMatthew Dillon 			return (2);
70717eab71eSMatthew Dillon 		}
70817eab71eSMatthew Dillon 	}
70917eab71eSMatthew Dillon 
71017eab71eSMatthew Dillon 	/*
71117eab71eSMatthew Dillon 	 * Turn on ST, wait for CR to come up.
71217eab71eSMatthew Dillon 	 */
713258223a3SMatthew Dillon 	r |= AHCI_PREG_CMD_ST;
714258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
71517eab71eSMatthew Dillon 	if (ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
7168bf6a3ffSMatthew Dillon 		s = ahci_pread(ap, AHCI_PREG_SERR);
7178bf6a3ffSMatthew Dillon 		is = ahci_pread(ap, AHCI_PREG_IS);
7188bf6a3ffSMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
7191980eff3SMatthew Dillon 		kprintf("%s: Cannot start command DMA\n"
7201980eff3SMatthew Dillon 			"OCMD=%b OSERR=%b\n"
7211980eff3SMatthew Dillon 			"NCMP=%b NSERR=%b\n"
7228bf6a3ffSMatthew Dillon 			"OLDIS=%b\nNEWIS=%b\n"
7238bf6a3ffSMatthew Dillon 			"OLDTFD=%b\nNEWTFD=%b\n",
7241980eff3SMatthew Dillon 			PORTNAME(ap),
7251980eff3SMatthew Dillon 			oldr, AHCI_PFMT_CMD, olds, AHCI_PFMT_SERR,
7261980eff3SMatthew Dillon 			r, AHCI_PFMT_CMD, s, AHCI_PFMT_SERR,
7278bf6a3ffSMatthew Dillon 			oldis, AHCI_PFMT_IS, is, AHCI_PFMT_IS,
7288bf6a3ffSMatthew Dillon 			oldtfd, AHCI_PFMT_TFD_STS, tfd, AHCI_PFMT_TFD_STS);
72917eab71eSMatthew Dillon 		return (1);
73017eab71eSMatthew Dillon 	}
731258223a3SMatthew Dillon 
732258223a3SMatthew Dillon #ifdef AHCI_COALESCE
73317eab71eSMatthew Dillon 	/*
73417eab71eSMatthew Dillon 	 * (Re-)enable coalescing on the port.
73517eab71eSMatthew Dillon 	 */
736258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
737258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur |= (1 << ap->ap_num);
738258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
739258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
740258223a3SMatthew Dillon 	}
741258223a3SMatthew Dillon #endif
742258223a3SMatthew Dillon 
743258223a3SMatthew Dillon 	return (0);
744258223a3SMatthew Dillon }
745258223a3SMatthew Dillon 
746fd8bd957SMatthew Dillon /*
747fd8bd957SMatthew Dillon  * Stop high-level command processing on a port
748fd8bd957SMatthew Dillon  */
749258223a3SMatthew Dillon int
750258223a3SMatthew Dillon ahci_port_stop(struct ahci_port *ap, int stop_fis_rx)
751258223a3SMatthew Dillon {
752258223a3SMatthew Dillon 	u_int32_t			r;
753258223a3SMatthew Dillon 
754258223a3SMatthew Dillon #ifdef AHCI_COALESCE
75517eab71eSMatthew Dillon 	/*
75617eab71eSMatthew Dillon 	 * Disable coalescing on the port while it is stopped.
75717eab71eSMatthew Dillon 	 */
758258223a3SMatthew Dillon 	if (ap->ap_sc->sc_ccc_ports & (1 << ap->ap_num)) {
759258223a3SMatthew Dillon 		ap->ap_sc->sc_ccc_ports_cur &= ~(1 << ap->ap_num);
760258223a3SMatthew Dillon 		ahci_write(ap->ap_sc, AHCI_REG_CCC_PORTS,
761258223a3SMatthew Dillon 		    ap->ap_sc->sc_ccc_ports_cur);
762258223a3SMatthew Dillon 	}
763258223a3SMatthew Dillon #endif
764258223a3SMatthew Dillon 
76517eab71eSMatthew Dillon 	/*
76617eab71eSMatthew Dillon 	 * Turn off ST, then wait for CR to go off.
76717eab71eSMatthew Dillon 	 */
768258223a3SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
769258223a3SMatthew Dillon 	r &= ~AHCI_PREG_CMD_ST;
770258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, r);
771258223a3SMatthew Dillon 
77217eab71eSMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CR)) {
77317eab71eSMatthew Dillon 		kprintf("%s: Port bricked, unable to stop (ST)\n",
77417eab71eSMatthew Dillon 			PORTNAME(ap));
775258223a3SMatthew Dillon 		return (1);
77617eab71eSMatthew Dillon 	}
777258223a3SMatthew Dillon 
7781980eff3SMatthew Dillon #if 0
77917eab71eSMatthew Dillon 	/*
78017eab71eSMatthew Dillon 	 * Turn off FRE, then wait for FR to go off.  FRE cannot
78117eab71eSMatthew Dillon 	 * be turned off until CR transitions to 0.
78217eab71eSMatthew Dillon 	 */
7831980eff3SMatthew Dillon 	if ((r & AHCI_PREG_CMD_FR) == 0) {
7841980eff3SMatthew Dillon 		kprintf("%s: FR stopped, clear FRE for next start\n",
7851980eff3SMatthew Dillon 			PORTNAME(ap));
7861980eff3SMatthew Dillon 		stop_fis_rx = 2;
7871980eff3SMatthew Dillon 	}
7881980eff3SMatthew Dillon #endif
78917eab71eSMatthew Dillon 	if (stop_fis_rx) {
79017eab71eSMatthew Dillon 		r &= ~AHCI_PREG_CMD_FRE;
79117eab71eSMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, r);
79217eab71eSMatthew Dillon 		if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR)) {
79317eab71eSMatthew Dillon 			kprintf("%s: Port bricked, unable to stop (FRE)\n",
79417eab71eSMatthew Dillon 				PORTNAME(ap));
795258223a3SMatthew Dillon 			return (2);
79617eab71eSMatthew Dillon 		}
79717eab71eSMatthew Dillon 	}
798258223a3SMatthew Dillon 
799258223a3SMatthew Dillon 	return (0);
800258223a3SMatthew Dillon }
801258223a3SMatthew Dillon 
802fd8bd957SMatthew Dillon /*
803fd8bd957SMatthew Dillon  * AHCI command list override -> forcibly clear TFD.STS.{BSY,DRQ}
804fd8bd957SMatthew Dillon  */
805258223a3SMatthew Dillon int
806258223a3SMatthew Dillon ahci_port_clo(struct ahci_port *ap)
807258223a3SMatthew Dillon {
808258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
809258223a3SMatthew Dillon 	u_int32_t			cmd;
810258223a3SMatthew Dillon 
811258223a3SMatthew Dillon 	/* Only attempt CLO if supported by controller */
812258223a3SMatthew Dillon 	if ((ahci_read(sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO) == 0)
813258223a3SMatthew Dillon 		return (1);
814258223a3SMatthew Dillon 
815258223a3SMatthew Dillon 	/* Issue CLO */
816258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
817258223a3SMatthew Dillon #ifdef DIAGNOSTIC
818258223a3SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_ST) {
819258223a3SMatthew Dillon 		kprintf("%s: CLO requested while port running\n",
820258223a3SMatthew Dillon 			PORTNAME(ap));
821258223a3SMatthew Dillon 	}
822258223a3SMatthew Dillon #endif
823258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd | AHCI_PREG_CMD_CLO);
824258223a3SMatthew Dillon 
825258223a3SMatthew Dillon 	/* Wait for completion */
826258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_CLO)) {
827258223a3SMatthew Dillon 		kprintf("%s: CLO did not complete\n", PORTNAME(ap));
828258223a3SMatthew Dillon 		return (1);
829258223a3SMatthew Dillon 	}
830258223a3SMatthew Dillon 
831258223a3SMatthew Dillon 	return (0);
832258223a3SMatthew Dillon }
833258223a3SMatthew Dillon 
834fd8bd957SMatthew Dillon /*
8351980eff3SMatthew Dillon  * Reset a port.
83617eab71eSMatthew Dillon  *
8371980eff3SMatthew Dillon  * If hard is 0 perform a softreset of the port.
83817eab71eSMatthew Dillon  * If hard is 1 perform a hard reset of the port.
8391980eff3SMatthew Dillon  * If hard is 2 perform a hard reset of the port and cycle the phy.
8401980eff3SMatthew Dillon  *
8411980eff3SMatthew Dillon  * If at is non-NULL an indirect port via a port-multiplier is being
8421980eff3SMatthew Dillon  * reset, otherwise a direct port is being reset.
8431980eff3SMatthew Dillon  *
8441980eff3SMatthew Dillon  * NOTE: Indirect ports can only be soft-reset.
84517eab71eSMatthew Dillon  */
84617eab71eSMatthew Dillon int
8471980eff3SMatthew Dillon ahci_port_reset(struct ahci_port *ap, struct ata_port *at, int hard)
84817eab71eSMatthew Dillon {
84917eab71eSMatthew Dillon 	int rc;
85017eab71eSMatthew Dillon 
85117eab71eSMatthew Dillon 	if (hard) {
8521980eff3SMatthew Dillon 		if (at)
8531980eff3SMatthew Dillon 			rc = ahci_pm_hardreset(ap, at->at_target, hard);
8541980eff3SMatthew Dillon 		else
8551980eff3SMatthew Dillon 			rc = ahci_port_hardreset(ap, hard);
85617eab71eSMatthew Dillon 	} else {
8571980eff3SMatthew Dillon 		if (at)
8581980eff3SMatthew Dillon 			rc = ahci_pm_softreset(ap, at->at_target);
8591980eff3SMatthew Dillon 		else
86017eab71eSMatthew Dillon 			rc = ahci_port_softreset(ap);
8611980eff3SMatthew Dillon #if 0
8621980eff3SMatthew Dillon 		if (rc && at == NULL)
8631980eff3SMatthew Dillon 			rc = ahci_port_hardreset(ap, hard);
8641980eff3SMatthew Dillon #endif
86517eab71eSMatthew Dillon 	}
86617eab71eSMatthew Dillon 	return(rc);
86717eab71eSMatthew Dillon }
86817eab71eSMatthew Dillon 
86917eab71eSMatthew Dillon /*
870fd8bd957SMatthew Dillon  * AHCI soft reset, Section 10.4.1
871fd8bd957SMatthew Dillon  *
8721980eff3SMatthew Dillon  * (at) will be NULL when soft-resetting a directly-attached device, and
8731980eff3SMatthew Dillon  * non-NULL when soft-resetting a device through a port multiplier.
8741980eff3SMatthew Dillon  *
875fd8bd957SMatthew Dillon  * This function keeps port communications intact and attempts to generate
8761980eff3SMatthew Dillon  * a reset to the connected device using device commands.
877fd8bd957SMatthew Dillon  */
878258223a3SMatthew Dillon int
879258223a3SMatthew Dillon ahci_port_softreset(struct ahci_port *ap)
880258223a3SMatthew Dillon {
881258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
882258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*cmd_slot;
883258223a3SMatthew Dillon 	u_int8_t		*fis;
8843209f581SMatthew Dillon 	int			error;
885258223a3SMatthew Dillon 	u_int32_t		cmd;
886258223a3SMatthew Dillon 
8873209f581SMatthew Dillon 	error = EIO;
8881980eff3SMatthew Dillon 
8891980eff3SMatthew Dillon 	kprintf("%s: START SOFTRESET %b\n", PORTNAME(ap),
8901980eff3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD);
8911980eff3SMatthew Dillon 
892258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: soft reset\n", PORTNAME(ap));
893258223a3SMatthew Dillon 
894258223a3SMatthew Dillon 	crit_enter();
8951980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
8961980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
897258223a3SMatthew Dillon 
8981980eff3SMatthew Dillon 	/*
8991980eff3SMatthew Dillon 	 * Remember port state in cmd (main to restore start/stop)
9001980eff3SMatthew Dillon 	 *
9011980eff3SMatthew Dillon 	 * Idle port.
9021980eff3SMatthew Dillon 	 */
9031980eff3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
904258223a3SMatthew Dillon 	if (ahci_port_stop(ap, 0)) {
905258223a3SMatthew Dillon 		kprintf("%s: failed to stop port, cannot softreset\n",
906258223a3SMatthew Dillon 			PORTNAME(ap));
907258223a3SMatthew Dillon 		goto err;
908258223a3SMatthew Dillon 	}
909cf5f3a81SMatthew Dillon 
910cf5f3a81SMatthew Dillon 	/*
9111980eff3SMatthew Dillon 	 * Request CLO if device appears hung.
912cf5f3a81SMatthew Dillon 	 */
913258223a3SMatthew Dillon 	if (ahci_pread(ap, AHCI_PREG_TFD) &
914258223a3SMatthew Dillon 		   (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
915258223a3SMatthew Dillon 		ahci_port_clo(ap);
916258223a3SMatthew Dillon 	}
917258223a3SMatthew Dillon 
9181980eff3SMatthew Dillon 	/*
9191980eff3SMatthew Dillon 	 * This is an attempt to clear errors so a new signature will
9201980eff3SMatthew Dillon 	 * be latched.  It isn't working properly.  XXX
9211980eff3SMatthew Dillon 	 */
922cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
9231980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
924258223a3SMatthew Dillon 
925258223a3SMatthew Dillon 	/* Restart port */
92617eab71eSMatthew Dillon 	if (ahci_port_start(ap)) {
927258223a3SMatthew Dillon 		kprintf("%s: failed to start port, cannot softreset\n",
928258223a3SMatthew Dillon 		        PORTNAME(ap));
929258223a3SMatthew Dillon 		goto err;
930258223a3SMatthew Dillon 	}
931258223a3SMatthew Dillon 
932258223a3SMatthew Dillon 	/* Check whether CLO worked */
933258223a3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
934258223a3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
935258223a3SMatthew Dillon 		kprintf("%s: CLO %s, need port reset\n",
936258223a3SMatthew Dillon 			PORTNAME(ap),
937258223a3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
938258223a3SMatthew Dillon 			? "failed" : "unsupported");
9393209f581SMatthew Dillon 		error = EBUSY;
940258223a3SMatthew Dillon 		goto err;
941258223a3SMatthew Dillon 	}
942258223a3SMatthew Dillon 
943cec85a37SMatthew Dillon 	/*
944cec85a37SMatthew Dillon 	 * Prep first D2H command with SRST feature & clear busy/reset flags
945cec85a37SMatthew Dillon 	 *
946cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
947cec85a37SMatthew Dillon 	 * everything.
948cec85a37SMatthew Dillon 	 */
949258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
9501980eff3SMatthew Dillon 	ccb->ccb_xa.at = NULL;
951258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
952258223a3SMatthew Dillon 
953258223a3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
954cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
9551980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
9561980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_SRST|ATA_FIS_CONTROL_4BIT;
957258223a3SMatthew Dillon 
958258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
959258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
960258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
961258223a3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
962258223a3SMatthew Dillon 
963258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
9645f8c1efdSMatthew Dillon 	ccb->ccb_xa.flags = 0;
9653209f581SMatthew Dillon 	if (ahci_poll(ccb, 1000, NULL) != 0 ||
9661980eff3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_COMPLETE) {
9675f8c1efdSMatthew Dillon 		kprintf("%s: First FIS failed\n", PORTNAME(ap));
968258223a3SMatthew Dillon 		goto err;
969cec85a37SMatthew Dillon 	}
970258223a3SMatthew Dillon 
971cec85a37SMatthew Dillon 	/*
9721980eff3SMatthew Dillon 	 * The device may muff the PHY up.
9731980eff3SMatthew Dillon 	 */
9743209f581SMatthew Dillon 	ahci_os_sleep(10);	/* 3ms min, use 10 */
9751980eff3SMatthew Dillon 
9761980eff3SMatthew Dillon 	/*
977cec85a37SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
978cec85a37SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
979cec85a37SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
980cec85a37SMatthew Dillon 	 * from the AHCI document.
981cec85a37SMatthew Dillon 	 *
982b089d0bfSMatthew Dillon 	 * Give the device 3ms before sending the second FIS.
983cec85a37SMatthew Dillon 	 *
984cec85a37SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
985cec85a37SMatthew Dillon 	 * everything.
986cec85a37SMatthew Dillon 	 */
987cec85a37SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
9881980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
9891980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_4BIT;
990258223a3SMatthew Dillon 
991258223a3SMatthew Dillon 	cmd_slot->prdtl = 0;
992258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
993258223a3SMatthew Dillon 
994258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
9955f8c1efdSMatthew Dillon 	ccb->ccb_xa.flags = 0;
9963209f581SMatthew Dillon 	if (ahci_poll(ccb, 1000, NULL) != 0 ||
9971980eff3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_COMPLETE) {
9985f8c1efdSMatthew Dillon 		kprintf("%s: Second FIS failed\n", PORTNAME(ap));
999258223a3SMatthew Dillon 		goto err;
1000cec85a37SMatthew Dillon 	}
1001258223a3SMatthew Dillon 
10021980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
10031980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
1004258223a3SMatthew Dillon 		kprintf("%s: device didn't come ready after reset, TFD: 0x%b\n",
1005258223a3SMatthew Dillon 			PORTNAME(ap),
1006258223a3SMatthew Dillon 			ahci_pread(ap, AHCI_PREG_TFD), AHCI_PFMT_TFD_STS);
10073209f581SMatthew Dillon 		error = EBUSY;
1008258223a3SMatthew Dillon 		goto err;
1009258223a3SMatthew Dillon 	}
10103209f581SMatthew Dillon 	ahci_os_sleep(10);
1011258223a3SMatthew Dillon 
1012fd8bd957SMatthew Dillon 	/*
1013fd8bd957SMatthew Dillon 	 * If the softreset is trying to clear a BSY condition after a
1014fd8bd957SMatthew Dillon 	 * normal portreset we assign the port type.
1015fd8bd957SMatthew Dillon 	 *
1016fd8bd957SMatthew Dillon 	 * If the softreset is being run first as part of the ccb error
1017fd8bd957SMatthew Dillon 	 * processing code then report if the device signature changed
1018fd8bd957SMatthew Dillon 	 * unexpectedly.
1019fd8bd957SMatthew Dillon 	 */
10201980eff3SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE) {
10211980eff3SMatthew Dillon 		ap->ap_type = ahci_port_signature_detect(ap, NULL);
1022fd8bd957SMatthew Dillon 	} else {
10231980eff3SMatthew Dillon 		if (ahci_port_signature_detect(ap, NULL) != ap->ap_type) {
10241980eff3SMatthew Dillon 			kprintf("%s: device signature unexpectedly "
10251980eff3SMatthew Dillon 				"changed\n", PORTNAME(ap));
10263209f581SMatthew Dillon 			error = EBUSY; /* XXX */
1027fd8bd957SMatthew Dillon 		}
1028fd8bd957SMatthew Dillon 	}
10293209f581SMatthew Dillon 	error = 0;
10301980eff3SMatthew Dillon 
10313209f581SMatthew Dillon 	ahci_os_sleep(3);
1032258223a3SMatthew Dillon err:
1033258223a3SMatthew Dillon 	if (ccb != NULL) {
10341980eff3SMatthew Dillon 		/*
10351980eff3SMatthew Dillon 		 * Abort our command, if it failed, by stopping command DMA.
10361980eff3SMatthew Dillon 		 */
10373209f581SMatthew Dillon 		if (error && (ap->ap_active & (1 << ccb->ccb_slot))) {
1038258223a3SMatthew Dillon 			kprintf("%s: stopping the port, softreset slot "
1039258223a3SMatthew Dillon 				"%d was still active.\n",
1040258223a3SMatthew Dillon 				PORTNAME(ap),
1041258223a3SMatthew Dillon 				ccb->ccb_slot);
1042258223a3SMatthew Dillon 			ahci_port_stop(ap, 0);
1043258223a3SMatthew Dillon 		}
1044258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
10451980eff3SMatthew Dillon 		fis[15] = 0;
1046258223a3SMatthew Dillon 		ahci_put_err_ccb(ccb);
10471980eff3SMatthew Dillon 
10481980eff3SMatthew Dillon 		/*
10491980eff3SMatthew Dillon 		 * If the target is busy use CLO to clear the busy
10501980eff3SMatthew Dillon 		 * condition.  The BSY should be cleared on the next
10511980eff3SMatthew Dillon 		 * start.
10521980eff3SMatthew Dillon 		 */
10531980eff3SMatthew Dillon 		if (ahci_pread(ap, AHCI_PREG_TFD) &
10541980eff3SMatthew Dillon 		    (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
10551980eff3SMatthew Dillon 			ahci_port_clo(ap);
10561980eff3SMatthew Dillon 		}
1057258223a3SMatthew Dillon 	}
1058258223a3SMatthew Dillon 
1059cf5f3a81SMatthew Dillon 	/*
1060cf5f3a81SMatthew Dillon 	 * If we failed to softreset make the port quiescent, otherwise
1061cf5f3a81SMatthew Dillon 	 * make sure the port's start/stop state matches what it was on
1062cf5f3a81SMatthew Dillon 	 * entry.
10631980eff3SMatthew Dillon 	 *
10641980eff3SMatthew Dillon 	 * Don't kill the port if the softreset is on a port multiplier
10651980eff3SMatthew Dillon 	 * target, that would kill all the targets!
1066cf5f3a81SMatthew Dillon 	 */
10673209f581SMatthew Dillon 	if (error) {
1068cf5f3a81SMatthew Dillon 		ahci_port_hardstop(ap);
10693209f581SMatthew Dillon 		/* ap_probe set to failed */
1070cf5f3a81SMatthew Dillon 	} else if (cmd & AHCI_PREG_CMD_ST) {
10713209f581SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_IDENT;
10721980eff3SMatthew Dillon 		kprintf("%s: STARTING PORT\n", PORTNAME(ap));
1073cf5f3a81SMatthew Dillon 		ahci_port_start(ap);
1074cf5f3a81SMatthew Dillon 	} else {
10753209f581SMatthew Dillon 		ap->ap_probe = ATA_PROBE_NEED_IDENT;
10761980eff3SMatthew Dillon 		kprintf("%s: STOPPING PORT\n", PORTNAME(ap));
1077cf5f3a81SMatthew Dillon 		ahci_port_stop(ap, !(cmd & AHCI_PREG_CMD_FRE));
1078cf5f3a81SMatthew Dillon 	}
10793209f581SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
1080258223a3SMatthew Dillon 	crit_exit();
1081258223a3SMatthew Dillon 
10821980eff3SMatthew Dillon 	kprintf("%s: END SOFTRESET\n", PORTNAME(ap));
10831980eff3SMatthew Dillon 
10843209f581SMatthew Dillon 	return (error);
1085258223a3SMatthew Dillon }
1086258223a3SMatthew Dillon 
1087fd8bd957SMatthew Dillon /*
1088fd8bd957SMatthew Dillon  * AHCI port reset, Section 10.4.2
1089fd8bd957SMatthew Dillon  *
1090fd8bd957SMatthew Dillon  * This function does a hard reset of the port.  Note that the device
1091fd8bd957SMatthew Dillon  * connected to the port could still end-up hung.
1092fd8bd957SMatthew Dillon  */
1093258223a3SMatthew Dillon int
10941980eff3SMatthew Dillon ahci_port_hardreset(struct ahci_port *ap, int hard)
1095258223a3SMatthew Dillon {
1096258223a3SMatthew Dillon 	u_int32_t cmd, r;
10973209f581SMatthew Dillon 	int	error;
10981980eff3SMatthew Dillon 	int	loop;
10991980eff3SMatthew Dillon 	int	type;
1100258223a3SMatthew Dillon 
1101258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: port reset\n", PORTNAME(ap));
1102258223a3SMatthew Dillon 
11031980eff3SMatthew Dillon 	ap->ap_flags |= AP_F_IN_RESET;
1104cf5f3a81SMatthew Dillon 
1105cf5f3a81SMatthew Dillon 	/*
11061980eff3SMatthew Dillon 	 * Idle the port,
11071980eff3SMatthew Dillon 	 */
11081980eff3SMatthew Dillon 	ahci_port_stop(ap, 0);
11091980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
11103209f581SMatthew Dillon 	error = 0;
11111980eff3SMatthew Dillon 
11121980eff3SMatthew Dillon 	/*
11131980eff3SMatthew Dillon 	 * The port may have been quiescent with its SUD bit cleared, so
11141980eff3SMatthew Dillon 	 * set the SUD (spin up device).
1115cf5f3a81SMatthew Dillon 	 */
1116cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
1117cf5f3a81SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD;
1118cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1119258223a3SMatthew Dillon 
11201980eff3SMatthew Dillon 	/*
11211980eff3SMatthew Dillon 	 * Perform device detection.  Cycle the PHY off, wait 10ms.
11221980eff3SMatthew Dillon 	 * This simulates the SATA cable being physically unplugged.
11231980eff3SMatthew Dillon 	 */
11241980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1125258223a3SMatthew Dillon 
11261980eff3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED;
11271980eff3SMatthew Dillon 	if (hard == 2)
11281980eff3SMatthew Dillon 		r |= AHCI_PREG_SCTL_DET_DISABLE;
11291980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
11303209f581SMatthew Dillon 	ahci_os_sleep(10);
11311980eff3SMatthew Dillon 
11321980eff3SMatthew Dillon 	/*
11331980eff3SMatthew Dillon 	 * Start transmitting COMRESET.  COMRESET must be sent for at
11341980eff3SMatthew Dillon 	 * least 1ms.
11351980eff3SMatthew Dillon 	 */
11361980eff3SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1137258223a3SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
1138258223a3SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1139258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
1140258223a3SMatthew Dillon 	} else {
1141258223a3SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
1142258223a3SMatthew Dillon 	}
1143258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
11443209f581SMatthew Dillon 	ahci_os_sleep(1);
1145cf5f3a81SMatthew Dillon 
1146cf5f3a81SMatthew Dillon 	/*
1147cf5f3a81SMatthew Dillon 	 * Only SERR_DIAG_X needs to be cleared for TFD updates, but
1148cf5f3a81SMatthew Dillon 	 * since we are hard-resetting the port we might as well clear
1149cf5f3a81SMatthew Dillon 	 * the whole enchillada
1150cf5f3a81SMatthew Dillon 	 */
1151cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1152cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
1153258223a3SMatthew Dillon 	r &= ~AHCI_PREG_SCTL_DET_INIT;
1154258223a3SMatthew Dillon 	r |= AHCI_PREG_SCTL_DET_NONE;
1155258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
1156258223a3SMatthew Dillon 
11571980eff3SMatthew Dillon 	/*
11581980eff3SMatthew Dillon 	 * Try to determine if there is a device on the port.
11591980eff3SMatthew Dillon 	 *
11601980eff3SMatthew Dillon 	 * Give the device 3/10 second to at least be detected.
11611980eff3SMatthew Dillon 	 * If we fail clear PRCS (phy detect) since we may cycled
11621980eff3SMatthew Dillon 	 * the phy and probably caused another PRCS interrupt.
11631980eff3SMatthew Dillon 	 */
11641980eff3SMatthew Dillon 	for (loop = 30; loop; --loop) {
11651980eff3SMatthew Dillon 		r = ahci_pread(ap, AHCI_PREG_SSTS);
11661980eff3SMatthew Dillon 		if (r & AHCI_PREG_SSTS_DET)
11671980eff3SMatthew Dillon 			break;
11683209f581SMatthew Dillon 		ahci_os_sleep(10);
11691980eff3SMatthew Dillon 	}
11701980eff3SMatthew Dillon 	if (loop == 0) {
11711980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_PRCS);
11721980eff3SMatthew Dillon 		kprintf("%s: Port appears to be unplugged\n",
11731980eff3SMatthew Dillon 			PORTNAME(ap));
11743209f581SMatthew Dillon 		error = ENODEV;
1175258223a3SMatthew Dillon 	}
1176258223a3SMatthew Dillon 
1177cec85a37SMatthew Dillon 	/*
11781980eff3SMatthew Dillon 	 * There is something on the port.  Give the device 3 seconds
11791980eff3SMatthew Dillon 	 * to fully negotiate.
11801980eff3SMatthew Dillon 	 */
11813209f581SMatthew Dillon 	if (error == 0 &&
11821980eff3SMatthew Dillon 	    ahci_pwait_eq(ap, 3000, AHCI_PREG_SSTS,
11831980eff3SMatthew Dillon 			  AHCI_PREG_SSTS_DET, AHCI_PREG_SSTS_DET_DEV)) {
11841980eff3SMatthew Dillon 		kprintf("%s: Device may be powered down\n",
11851980eff3SMatthew Dillon 			PORTNAME(ap));
11863209f581SMatthew Dillon 		error = ENODEV;
11871980eff3SMatthew Dillon 	}
11881980eff3SMatthew Dillon 
11891980eff3SMatthew Dillon 	/*
11901980eff3SMatthew Dillon 	 * Wait for the device to become ready.
1191cec85a37SMatthew Dillon 	 *
1192b089d0bfSMatthew Dillon 	 * This can take more then a second, give it 3 seconds.  If we
1193b089d0bfSMatthew Dillon 	 * succeed give the device another 3ms after that.
11941980eff3SMatthew Dillon 	 *
11953209f581SMatthew Dillon 	 * NOTE: Port multipliers can do two things here.  First they can
11961980eff3SMatthew Dillon 	 *	 return device-ready if a device is on target 0 and also
11971980eff3SMatthew Dillon 	 *	 return the signature for that device.  If there is no
11981980eff3SMatthew Dillon 	 *	 device on target 0 then BSY/DRQ is never cleared and
11991980eff3SMatthew Dillon 	 *	 it never comes ready.
1200cec85a37SMatthew Dillon 	 */
12013209f581SMatthew Dillon 	if (error == 0 &&
12021980eff3SMatthew Dillon 	    ahci_pwait_clr_to(ap, 3000, AHCI_PREG_TFD,
12031980eff3SMatthew Dillon 			    AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
12041980eff3SMatthew Dillon 		/*
12051980eff3SMatthew Dillon 		 * The device is bricked or its a port multiplier and will
12061980eff3SMatthew Dillon 		 * not unbusy until we do the pmprobe CLO softreset sequence.
12071980eff3SMatthew Dillon 		 */
12083209f581SMatthew Dillon 		error = ahci_port_pmprobe(ap);
12093209f581SMatthew Dillon 		if (error) {
1210258223a3SMatthew Dillon 			kprintf("%s: Device will not come ready 0x%b\n",
1211258223a3SMatthew Dillon 				PORTNAME(ap),
12121980eff3SMatthew Dillon 				ahci_pread(ap, AHCI_PREG_TFD),
12131980eff3SMatthew Dillon 				AHCI_PFMT_TFD_STS);
12141980eff3SMatthew Dillon 		} else {
12151980eff3SMatthew Dillon 			ap->ap_type = ATA_PORT_T_PM;
1216258223a3SMatthew Dillon 		}
12173209f581SMatthew Dillon 	} else if (error == 0) {
12181980eff3SMatthew Dillon 		/*
12191980eff3SMatthew Dillon 		 * We generally will not get a port multiplier signature in
12201980eff3SMatthew Dillon 		 * this case even if this is a port multiplier, because of
12211980eff3SMatthew Dillon 		 * Intel's stupidity.  We almost certainly got target 0
12221980eff3SMatthew Dillon 		 * behind the PM, if there is a PM.
12231980eff3SMatthew Dillon 		 *
12241980eff3SMatthew Dillon 		 * Save the signature and probe for a PM.  If we do not
12251980eff3SMatthew Dillon 		 * find a PM then use the saved signature and return
12261980eff3SMatthew Dillon 		 * success.
12271980eff3SMatthew Dillon 		 */
12281980eff3SMatthew Dillon 		type = ahci_port_signature_detect(ap, NULL);
12293209f581SMatthew Dillon 		error = ahci_port_pmprobe(ap);
12303209f581SMatthew Dillon 		if (error) {
12311980eff3SMatthew Dillon 			ap->ap_type = type;
12323209f581SMatthew Dillon 			error = 0;
12331980eff3SMatthew Dillon 		} else {
12341980eff3SMatthew Dillon 			ap->ap_type = ATA_PORT_T_PM;
12353209f581SMatthew Dillon 			kprintf("%s: Port multiplier detected\n",
12361980eff3SMatthew Dillon 				PORTNAME(ap));
12371980eff3SMatthew Dillon 		}
12381980eff3SMatthew Dillon 	}
1239258223a3SMatthew Dillon 
1240cf5f3a81SMatthew Dillon 	/*
12411980eff3SMatthew Dillon 	 * hard-stop the port if we failed.  This will set ap_probe
12421980eff3SMatthew Dillon 	 * to FAILED.
1243cf5f3a81SMatthew Dillon 	 */
12441980eff3SMatthew Dillon 	ap->ap_flags &= ~AP_F_IN_RESET;
12453209f581SMatthew Dillon 	if (error) {
12463209f581SMatthew Dillon 		ahci_port_hardstop(ap);
12473209f581SMatthew Dillon 		/* ap_probe set to failed */
12483209f581SMatthew Dillon 	} else {
1249*f4553de1SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
1250*f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_GOOD;
1251*f4553de1SMatthew Dillon 		else
1252*f4553de1SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_SOFT_RESET;
12533209f581SMatthew Dillon 	}
12543209f581SMatthew Dillon 	return (error);
1255258223a3SMatthew Dillon }
1256258223a3SMatthew Dillon 
1257fd8bd957SMatthew Dillon /*
12581980eff3SMatthew Dillon  * AHCI port multiplier probe.  This routine is run by the hardreset code
12591980eff3SMatthew Dillon  * if it gets past the device detect, whether or not BSY is found to be
12601980eff3SMatthew Dillon  * stuck.
12611980eff3SMatthew Dillon  *
12621980eff3SMatthew Dillon  * We MUST use CLO to properly probe whether the port multiplier exists
12631980eff3SMatthew Dillon  * or not.
12641980eff3SMatthew Dillon  *
12651980eff3SMatthew Dillon  * Return 0 on success, non-zero on failure.
12661980eff3SMatthew Dillon  */
12671980eff3SMatthew Dillon int
12681980eff3SMatthew Dillon ahci_port_pmprobe(struct ahci_port *ap)
12691980eff3SMatthew Dillon {
12701980eff3SMatthew Dillon 	struct ahci_cmd_hdr *cmd_slot;
12711980eff3SMatthew Dillon 	struct ahci_ccb	*ccb = NULL;
12721980eff3SMatthew Dillon 	u_int8_t	*fis = NULL;
12731980eff3SMatthew Dillon 	int		rc = EIO;
12741980eff3SMatthew Dillon 	u_int32_t	cmd;
12751980eff3SMatthew Dillon 	int		count;
12761980eff3SMatthew Dillon 
12771980eff3SMatthew Dillon 	/*
12781980eff3SMatthew Dillon 	 * If we don't support port multipliers don't try to detect one.
12791980eff3SMatthew Dillon 	 */
12801980eff3SMatthew Dillon 	if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SPM) == 0)
12811980eff3SMatthew Dillon 		return (ENODEV);
12821980eff3SMatthew Dillon 
12831980eff3SMatthew Dillon 	count = 2;
12841980eff3SMatthew Dillon #if 0
12851980eff3SMatthew Dillon 	kprintf("%s: START PMPROBE\n", PORTNAME(ap));
12861980eff3SMatthew Dillon #endif
12871980eff3SMatthew Dillon retry:
12881980eff3SMatthew Dillon 	/*
12891980eff3SMatthew Dillon 	 * This code is only called from hardreset, which does not
12901980eff3SMatthew Dillon 	 * high level command processing.  The port should be stopped.
12911980eff3SMatthew Dillon 	 *
12921980eff3SMatthew Dillon 	 * Set PMA mode while the port is stopped.
12931980eff3SMatthew Dillon 	 *
12941980eff3SMatthew Dillon 	 * NOTE: On retry the port might be running, stopped, or failed.
12951980eff3SMatthew Dillon 	 */
12961980eff3SMatthew Dillon 	ahci_port_stop(ap, 0);
12971980eff3SMatthew Dillon 	ap->ap_state = AP_S_NORMAL;
12981980eff3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
12991980eff3SMatthew Dillon 	cmd |= AHCI_PREG_CMD_PMA;
13001980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
13011980eff3SMatthew Dillon 
13021980eff3SMatthew Dillon 	/*
13031980eff3SMatthew Dillon 	 * Flush any errors and request CLO unconditionally, then start
13041980eff3SMatthew Dillon 	 * the port.
13051980eff3SMatthew Dillon 	 */
13061980eff3SMatthew Dillon 	ahci_flush_tfd(ap);
13071980eff3SMatthew Dillon 	ahci_port_clo(ap);
13081980eff3SMatthew Dillon 	if (ahci_port_start(ap)) {
13091980eff3SMatthew Dillon 		kprintf("%s: PMPROBE failed to start port, cannot softreset\n",
13101980eff3SMatthew Dillon 		        PORTNAME(ap));
13111980eff3SMatthew Dillon 		goto err;
13121980eff3SMatthew Dillon 	}
13131980eff3SMatthew Dillon 
13141980eff3SMatthew Dillon 	/*
13151980eff3SMatthew Dillon 	 * Check whether CLO worked
13161980eff3SMatthew Dillon 	 */
13171980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
13181980eff3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
13191980eff3SMatthew Dillon 		kprintf("%s: PMPROBE CLO %s, need port reset\n",
13201980eff3SMatthew Dillon 			PORTNAME(ap),
13211980eff3SMatthew Dillon 			(ahci_read(ap->ap_sc, AHCI_REG_CAP) & AHCI_REG_CAP_SCLO)
13221980eff3SMatthew Dillon 			? "failed" : "unsupported");
13231980eff3SMatthew Dillon 		rc = EBUSY;
13241980eff3SMatthew Dillon 		goto err;
13251980eff3SMatthew Dillon 	}
13261980eff3SMatthew Dillon 
13271980eff3SMatthew Dillon 	/*
13281980eff3SMatthew Dillon 	 * Prep the first H2D command with SRST feature & clear busy/reset
13291980eff3SMatthew Dillon 	 * flags.
13301980eff3SMatthew Dillon 	 */
13311980eff3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
13321980eff3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
13331980eff3SMatthew Dillon 
13341980eff3SMatthew Dillon 	fis = ccb->ccb_cmd_table->cfis;
13351980eff3SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
13361980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
13371980eff3SMatthew Dillon 	fis[1] = 0x0F;			/* Target 15 */
13381980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_SRST | ATA_FIS_CONTROL_4BIT;
13391980eff3SMatthew Dillon 
13401980eff3SMatthew Dillon 	cmd_slot->prdtl = 0;
13411980eff3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
13421980eff3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_C); /* Clear busy on OK */
13431980eff3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_R); /* Reset */
13441980eff3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
13451980eff3SMatthew Dillon 
13461980eff3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
13471980eff3SMatthew Dillon 	ccb->ccb_xa.flags = 0;
13481980eff3SMatthew Dillon 
13493209f581SMatthew Dillon 	if (ahci_poll(ccb, 1000, NULL) != 0 ||
13501980eff3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_COMPLETE) {
13511980eff3SMatthew Dillon 		kprintf("%s: PMPROBE First FIS failed\n", PORTNAME(ap));
13521980eff3SMatthew Dillon 		if (--count) {
13531980eff3SMatthew Dillon 			fis[15] = 0;
13541980eff3SMatthew Dillon 			ahci_put_err_ccb(ccb);
13551980eff3SMatthew Dillon 			goto retry;
13561980eff3SMatthew Dillon 		}
13571980eff3SMatthew Dillon 		goto err;
13581980eff3SMatthew Dillon 	}
13591980eff3SMatthew Dillon 	if (ahci_pwait_clr(ap, AHCI_PREG_TFD,
13601980eff3SMatthew Dillon 			       AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
13611980eff3SMatthew Dillon 		kprintf("%s: PMPROBE Busy after first FIS\n", PORTNAME(ap));
13621980eff3SMatthew Dillon 	}
13631980eff3SMatthew Dillon 
13641980eff3SMatthew Dillon 	/*
13651980eff3SMatthew Dillon 	 * The device may have muffed up the PHY when it reset.
13661980eff3SMatthew Dillon 	 */
13673209f581SMatthew Dillon 	ahci_os_sleep(10);
13681980eff3SMatthew Dillon 	ahci_flush_tfd(ap);
13691980eff3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SERR, -1);
13701980eff3SMatthew Dillon 	/* ahci_pm_phy_status(ap, 15, &cmd); */
13711980eff3SMatthew Dillon 
13721980eff3SMatthew Dillon 	/*
13731980eff3SMatthew Dillon 	 * Prep second D2H command to read status and complete reset sequence
13741980eff3SMatthew Dillon 	 * AHCI 10.4.1 and "Serial ATA Revision 2.6".  I can't find the ATA
13751980eff3SMatthew Dillon 	 * Rev 2.6 and it is unclear how the second FIS should be set up
13761980eff3SMatthew Dillon 	 * from the AHCI document.
13771980eff3SMatthew Dillon 	 *
13781980eff3SMatthew Dillon 	 * Give the device 3ms before sending the second FIS.
13791980eff3SMatthew Dillon 	 *
13801980eff3SMatthew Dillon 	 * It is unclear which other fields in the FIS are used.  Just zero
13811980eff3SMatthew Dillon 	 * everything.
13821980eff3SMatthew Dillon 	 */
13831980eff3SMatthew Dillon 	bzero(fis, sizeof(ccb->ccb_cmd_table->cfis));
13841980eff3SMatthew Dillon 	fis[0] = ATA_FIS_TYPE_H2D;
13851980eff3SMatthew Dillon 	fis[1] = 0x0F;
13861980eff3SMatthew Dillon 	fis[15] = ATA_FIS_CONTROL_4BIT;
13871980eff3SMatthew Dillon 
13881980eff3SMatthew Dillon 	cmd_slot->prdtl = 0;
13891980eff3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
13901980eff3SMatthew Dillon 	cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_PMP); /* port 0xF */
13911980eff3SMatthew Dillon 
13921980eff3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
13931980eff3SMatthew Dillon 	ccb->ccb_xa.flags = 0;
13941980eff3SMatthew Dillon 
13953209f581SMatthew Dillon 	if (ahci_poll(ccb, 1000, NULL) != 0 ||
13961980eff3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_COMPLETE) {
13971980eff3SMatthew Dillon 		kprintf("%s: PMPROBE Second FIS failed\n", PORTNAME(ap));
13981980eff3SMatthew Dillon 		if (--count) {
13991980eff3SMatthew Dillon 			fis[15] = 0;
14001980eff3SMatthew Dillon 			ahci_put_err_ccb(ccb);
14011980eff3SMatthew Dillon 			goto retry;
14021980eff3SMatthew Dillon 		}
14031980eff3SMatthew Dillon 		goto err;
14041980eff3SMatthew Dillon 	}
14051980eff3SMatthew Dillon 
14061980eff3SMatthew Dillon 	/*
14071980eff3SMatthew Dillon 	 * What? We succeeded?  Yup, but for some reason the signature
14081980eff3SMatthew Dillon 	 * is still latched from the original detect (that saw target 0
14091980eff3SMatthew Dillon 	 * behind the PM), and I don't know how to clear the condition
14101980eff3SMatthew Dillon 	 * other then by retrying the whole reset sequence.
14111980eff3SMatthew Dillon 	 */
14121980eff3SMatthew Dillon 	if (--count) {
14131980eff3SMatthew Dillon 		fis[15] = 0;
14141980eff3SMatthew Dillon 		ahci_put_err_ccb(ccb);
14151980eff3SMatthew Dillon 		goto retry;
14161980eff3SMatthew Dillon 	}
14171980eff3SMatthew Dillon 
14181980eff3SMatthew Dillon 	/*
14191980eff3SMatthew Dillon 	 * Get the signature.  The caller sets the ap fields.
14201980eff3SMatthew Dillon 	 */
14211980eff3SMatthew Dillon 	if (ahci_port_signature_detect(ap, NULL) == ATA_PORT_T_PM) {
14221980eff3SMatthew Dillon 		ap->ap_ata[15].at_probe = ATA_PROBE_GOOD;
14231980eff3SMatthew Dillon 		rc = 0;
14241980eff3SMatthew Dillon 	} else {
14251980eff3SMatthew Dillon 		rc = EBUSY;
14261980eff3SMatthew Dillon 	}
14271980eff3SMatthew Dillon 
14281980eff3SMatthew Dillon 	/*
14291980eff3SMatthew Dillon 	 * Fall through / clean up the CCB and perform error processing.
14301980eff3SMatthew Dillon 	 */
14311980eff3SMatthew Dillon err:
14321980eff3SMatthew Dillon 	if (ccb != NULL) {
14331980eff3SMatthew Dillon 		/*
14341980eff3SMatthew Dillon 		 * Abort our command, if it failed, by stopping command DMA.
14351980eff3SMatthew Dillon 		 */
14361980eff3SMatthew Dillon #if 0
14371980eff3SMatthew Dillon 		kprintf("rc=%d active=%08x sactive=%08x slot=%d\n",
14381980eff3SMatthew Dillon 			rc, ap->ap_active, ap->ap_sactive, ccb->ccb_slot);
14391980eff3SMatthew Dillon #endif
14401980eff3SMatthew Dillon 		if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
14411980eff3SMatthew Dillon 			kprintf("%s: PMP stopping the port, softreset slot "
14421980eff3SMatthew Dillon 				"%d was still active.\n",
14431980eff3SMatthew Dillon 				PORTNAME(ap),
14441980eff3SMatthew Dillon 				ccb->ccb_slot);
14451980eff3SMatthew Dillon 			ahci_port_stop(ap, 0);
14461980eff3SMatthew Dillon 		}
14471980eff3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
14481980eff3SMatthew Dillon 		fis[15] = 0;
14491980eff3SMatthew Dillon 		ahci_put_err_ccb(ccb);
14501980eff3SMatthew Dillon 	}
14511980eff3SMatthew Dillon 
14523209f581SMatthew Dillon 	if (rc == 0 && ahci_pm_identify(ap)) {
14533209f581SMatthew Dillon 		kprintf("%s: PM - cannot identify port multiplier\n",
14543209f581SMatthew Dillon 			PORTNAME(ap));
14553209f581SMatthew Dillon 		rc = EBUSY;
14563209f581SMatthew Dillon 	}
14573209f581SMatthew Dillon #if 0
14583209f581SMatthew Dillon 	if (rc == 0 && ahci_pm_set_feature(ap, ATA_SATAFT_ASYNCNOTIFY, 1)) {
14593209f581SMatthew Dillon 		kprintf("%s: PM - Warning, cannot enable async notify\n",
14603209f581SMatthew Dillon 			PORTNAME(ap));
14613209f581SMatthew Dillon 		/* ignore error */
14623209f581SMatthew Dillon 	}
14633209f581SMatthew Dillon 	if (rc == 0) {
14643209f581SMatthew Dillon 		u_int32_t data;
14653209f581SMatthew Dillon 		if (ahci_pm_read(ap, 2, 4, &data))
14663209f581SMatthew Dillon 			kprintf("Cannot read snotify\n");
14673209f581SMatthew Dillon 		else
14683209f581SMatthew Dillon 			kprintf("Read snotify %08x\n", data);
14693209f581SMatthew Dillon 	}
14703209f581SMatthew Dillon #endif
14713209f581SMatthew Dillon 
14721980eff3SMatthew Dillon 	/*
14731980eff3SMatthew Dillon 	 * If we failed turn off PMA, otherwise identify the port multiplier.
14741980eff3SMatthew Dillon 	 * CAM will iterate the devices.
14751980eff3SMatthew Dillon 	 */
14761980eff3SMatthew Dillon 	if (rc) {
14771980eff3SMatthew Dillon 		ahci_port_stop(ap, 0);
14781980eff3SMatthew Dillon 		cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
14791980eff3SMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_PMA;
14801980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
14811980eff3SMatthew Dillon 	}
14821980eff3SMatthew Dillon 	ahci_port_stop(ap, 0);
14831980eff3SMatthew Dillon 
14841980eff3SMatthew Dillon #if 0
14851980eff3SMatthew Dillon 	kprintf("%s: END PMPROBE\n", PORTNAME(ap));
14861980eff3SMatthew Dillon #endif
14871980eff3SMatthew Dillon 
14881980eff3SMatthew Dillon 	return(rc);
14891980eff3SMatthew Dillon }
14901980eff3SMatthew Dillon 
14911980eff3SMatthew Dillon 
14921980eff3SMatthew Dillon /*
1493cf5f3a81SMatthew Dillon  * Hard-stop on hot-swap device removal.  See 10.10.1
1494cf5f3a81SMatthew Dillon  *
1495cf5f3a81SMatthew Dillon  * Place the port in a mode that will allow it to detect hot-swap insertions.
1496cf5f3a81SMatthew Dillon  * This is a bit imprecise because just setting-up SCTL to DET_INIT doesn't
1497cf5f3a81SMatthew Dillon  * seem to do the job.
1498cf5f3a81SMatthew Dillon  */
1499cf5f3a81SMatthew Dillon void
1500cf5f3a81SMatthew Dillon ahci_port_hardstop(struct ahci_port *ap)
1501cf5f3a81SMatthew Dillon {
15021980eff3SMatthew Dillon 	struct ata_port *at;
1503cf5f3a81SMatthew Dillon 	u_int32_t r;
1504cf5f3a81SMatthew Dillon 	u_int32_t cmd;
15051980eff3SMatthew Dillon 	int i;
1506cf5f3a81SMatthew Dillon 
1507cf5f3a81SMatthew Dillon 	/*
1508cf5f3a81SMatthew Dillon 	 * Stop the port.  We can't modify things like SUD if the port
1509cf5f3a81SMatthew Dillon 	 * is running.
1510cf5f3a81SMatthew Dillon 	 */
1511cf5f3a81SMatthew Dillon 	ap->ap_state = AP_S_FATAL_ERROR;
15121980eff3SMatthew Dillon 	ap->ap_probe = ATA_PROBE_FAILED;
15131980eff3SMatthew Dillon 	ap->ap_type = ATA_PORT_T_NONE;
1514cf5f3a81SMatthew Dillon 	ahci_port_stop(ap, 0);
1515cf5f3a81SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD);
1516cf5f3a81SMatthew Dillon 
1517cf5f3a81SMatthew Dillon 	/*
15181980eff3SMatthew Dillon 	 * Clean up AT sub-ports on SATA port.
15191980eff3SMatthew Dillon 	 */
15201980eff3SMatthew Dillon 	for (i = 0; ap->ap_ata && i < AHCI_MAX_PMPORTS; ++i) {
15211980eff3SMatthew Dillon 		at = &ap->ap_ata[i];
15221980eff3SMatthew Dillon 		at->at_type = ATA_PORT_T_NONE;
15233209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_FAILED;
15241980eff3SMatthew Dillon 	}
15251980eff3SMatthew Dillon 
15261980eff3SMatthew Dillon 	/*
15271980eff3SMatthew Dillon 	 * Turn off port-multiplier control bit
15281980eff3SMatthew Dillon 	 */
15291980eff3SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_PMA) {
15301980eff3SMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_PMA;
15311980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
15321980eff3SMatthew Dillon 	}
15331980eff3SMatthew Dillon 
15341980eff3SMatthew Dillon 	/*
1535cf5f3a81SMatthew Dillon 	 * Make sure FRE is active.  There isn't anything we can do if it
1536cf5f3a81SMatthew Dillon 	 * fails so just ignore errors.
1537cf5f3a81SMatthew Dillon 	 */
1538cf5f3a81SMatthew Dillon 	if ((cmd & AHCI_PREG_CMD_FRE) == 0) {
1539cf5f3a81SMatthew Dillon 		cmd |= AHCI_PREG_CMD_FRE;
1540cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1541cf5f3a81SMatthew Dillon 		if ((ap->ap_sc->sc_flags & AHCI_F_IGN_FR) == 0)
1542cf5f3a81SMatthew Dillon 			ahci_pwait_set(ap, AHCI_PREG_CMD, AHCI_PREG_CMD_FR);
1543cf5f3a81SMatthew Dillon 	}
1544cf5f3a81SMatthew Dillon 
1545cf5f3a81SMatthew Dillon 	/*
1546cf5f3a81SMatthew Dillon 	 * 10.10.3 DET must be set to 0 before setting SUD to 0.
1547cf5f3a81SMatthew Dillon 	 * 10.10.1 place us in the Listen state.
1548cf5f3a81SMatthew Dillon 	 *
1549cf5f3a81SMatthew Dillon 	 * Deactivating SUD only applies if the controller supports SUD.
1550cf5f3a81SMatthew Dillon 	 */
1551cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, AHCI_PREG_SCTL_IPM_DISABLED);
15523209f581SMatthew Dillon 	ahci_os_sleep(1);
1553cf5f3a81SMatthew Dillon 	if (cmd & AHCI_PREG_CMD_SUD) {
1554cf5f3a81SMatthew Dillon 		cmd &= ~AHCI_PREG_CMD_SUD;
1555cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
1556cf5f3a81SMatthew Dillon 	}
15573209f581SMatthew Dillon 	ahci_os_sleep(1);
1558cf5f3a81SMatthew Dillon 
1559cf5f3a81SMatthew Dillon 	/*
1560cf5f3a81SMatthew Dillon 	 * Transition su to the spin-up state.  HVA shall send COMRESET and
1561cf5f3a81SMatthew Dillon 	 * begin initialization sequence (whatever that means).
1562cf5f3a81SMatthew Dillon 	 *
1563cf5f3a81SMatthew Dillon 	 * This only applies if the controller supports SUD.
1564cf5f3a81SMatthew Dillon 	 */
1565cf5f3a81SMatthew Dillon 	cmd |= AHCI_PREG_CMD_SUD;
1566cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
15673209f581SMatthew Dillon 	ahci_os_sleep(1);
1568cf5f3a81SMatthew Dillon 
1569cf5f3a81SMatthew Dillon 	/*
1570cf5f3a81SMatthew Dillon 	 * Transition us to the Reset state.  Theoretically we send a
1571cf5f3a81SMatthew Dillon 	 * continuous stream of COMRESETs in this state.
1572cf5f3a81SMatthew Dillon 	 */
1573cf5f3a81SMatthew Dillon 	r = AHCI_PREG_SCTL_IPM_DISABLED | AHCI_PREG_SCTL_DET_INIT;
1574cf5f3a81SMatthew Dillon 	if (AhciForceGen1 & (1 << ap->ap_num)) {
1575cf5f3a81SMatthew Dillon 		kprintf("%s: Force 1.5Gbits\n", PORTNAME(ap));
1576cf5f3a81SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_GEN1;
1577cf5f3a81SMatthew Dillon 	} else {
1578cf5f3a81SMatthew Dillon 		r |= AHCI_PREG_SCTL_SPD_ANY;
1579cf5f3a81SMatthew Dillon 	}
1580cf5f3a81SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SCTL, r);
15813209f581SMatthew Dillon 	ahci_os_sleep(1);
1582cf5f3a81SMatthew Dillon 
1583cf5f3a81SMatthew Dillon 	/*
1584cf5f3a81SMatthew Dillon 	 * Flush SERR_DIAG_X so the TFD can update.
1585cf5f3a81SMatthew Dillon 	 */
1586cf5f3a81SMatthew Dillon 	ahci_flush_tfd(ap);
1587cf5f3a81SMatthew Dillon 
1588cf5f3a81SMatthew Dillon 	/*
1589cf5f3a81SMatthew Dillon 	 * Leave us in COMRESET (both SUD and INIT active), the HBA should
1590cf5f3a81SMatthew Dillon 	 * hopefully send us a DIAG_X-related interrupt if it receives
1591cf5f3a81SMatthew Dillon 	 * a COMINIT, and if not that then at least a Phy transition
1592cf5f3a81SMatthew Dillon 	 * interrupt.
1593cf5f3a81SMatthew Dillon 	 *
1594cf5f3a81SMatthew Dillon 	 * If we transition INIT from 1->0 to begin the initalization
1595cf5f3a81SMatthew Dillon 	 * sequence it is unclear if that sequence will remain active
1596cf5f3a81SMatthew Dillon 	 * until the next device insertion.
1597cf5f3a81SMatthew Dillon 	 *
1598cf5f3a81SMatthew Dillon 	 * If we go back to the listen state it is unclear if the
1599cf5f3a81SMatthew Dillon 	 * device will actually send us a COMINIT, since we aren't
1600cf5f3a81SMatthew Dillon 	 * sending any COMRESET's
1601cf5f3a81SMatthew Dillon 	 */
1602cf5f3a81SMatthew Dillon 	/* NOP */
1603cf5f3a81SMatthew Dillon }
1604cf5f3a81SMatthew Dillon 
1605cf5f3a81SMatthew Dillon /*
1606cf5f3a81SMatthew Dillon  * Multiple events may have built up in the TFD.  The spec is not very
1607cf5f3a81SMatthew Dillon  * clear on this but it does seem to serialize events so clearing DIAG_X
1608cf5f3a81SMatthew Dillon  * just once might not do the job during a reset sequence.
1609cf5f3a81SMatthew Dillon  */
1610cf5f3a81SMatthew Dillon void
1611cf5f3a81SMatthew Dillon ahci_flush_tfd(struct ahci_port *ap)
1612cf5f3a81SMatthew Dillon {
1613cf5f3a81SMatthew Dillon 	u_int32_t r;
1614cf5f3a81SMatthew Dillon 
1615cf5f3a81SMatthew Dillon 	r = ahci_pread(ap, AHCI_PREG_SERR);
16161980eff3SMatthew Dillon 	while (r & AHCI_PREG_SERR_DIAG_X) {
16171980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_X);
16183209f581SMatthew Dillon 		ahci_os_sleep(1);
1619cf5f3a81SMatthew Dillon 		r = ahci_pread(ap, AHCI_PREG_SERR);
1620cf5f3a81SMatthew Dillon 	}
1621cf5f3a81SMatthew Dillon }
1622cf5f3a81SMatthew Dillon 
1623cf5f3a81SMatthew Dillon /*
1624fd8bd957SMatthew Dillon  * Figure out what type of device is connected to the port, ATAPI or
1625fd8bd957SMatthew Dillon  * DISK.
1626fd8bd957SMatthew Dillon  */
1627fd8bd957SMatthew Dillon int
16281980eff3SMatthew Dillon ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at)
1629fd8bd957SMatthew Dillon {
1630fd8bd957SMatthew Dillon 	u_int32_t sig;
1631fd8bd957SMatthew Dillon 
1632fd8bd957SMatthew Dillon 	sig = ahci_pread(ap, AHCI_PREG_SIG);
16331980eff3SMatthew Dillon 	kprintf("%s: sig %08x\n", ATANAME(ap, at), sig);
1634fd8bd957SMatthew Dillon 	if ((sig & 0xffff0000) == (SATA_SIGNATURE_ATAPI & 0xffff0000)) {
1635fd8bd957SMatthew Dillon 		return(ATA_PORT_T_ATAPI);
16361980eff3SMatthew Dillon 	} else if ((sig & 0xffff0000) ==
16371980eff3SMatthew Dillon 		 (SATA_SIGNATURE_PORT_MULTIPLIER & 0xffff0000)) {
16381980eff3SMatthew Dillon 		return(ATA_PORT_T_PM);
1639fd8bd957SMatthew Dillon 	} else {
1640fd8bd957SMatthew Dillon 		return(ATA_PORT_T_DISK);
1641fd8bd957SMatthew Dillon 	}
1642fd8bd957SMatthew Dillon }
1643fd8bd957SMatthew Dillon 
1644fd8bd957SMatthew Dillon /*
1645fd8bd957SMatthew Dillon  * Load the DMA descriptor table for a CCB's buffer.
1646fd8bd957SMatthew Dillon  */
1647258223a3SMatthew Dillon int
1648258223a3SMatthew Dillon ahci_load_prdt(struct ahci_ccb *ccb)
1649258223a3SMatthew Dillon {
1650258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1651258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1652258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1653258223a3SMatthew Dillon 	struct ahci_prdt		*prdt = ccb->ccb_cmd_table->prdt;
1654258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1655258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot = ccb->ccb_cmd_hdr;
1656258223a3SMatthew Dillon 	int				error;
1657258223a3SMatthew Dillon 
1658258223a3SMatthew Dillon 	if (xa->datalen == 0) {
1659258223a3SMatthew Dillon 		ccb->ccb_cmd_hdr->prdtl = 0;
1660258223a3SMatthew Dillon 		return (0);
1661258223a3SMatthew Dillon 	}
1662258223a3SMatthew Dillon 
1663258223a3SMatthew Dillon 	error = bus_dmamap_load(sc->sc_tag_data, dmap,
1664258223a3SMatthew Dillon 				xa->data, xa->datalen,
1665258223a3SMatthew Dillon 				ahci_load_prdt_callback,
1666258223a3SMatthew Dillon 				&prdt,
1667258223a3SMatthew Dillon 				((xa->flags & ATA_F_NOWAIT) ?
1668258223a3SMatthew Dillon 				    BUS_DMA_NOWAIT : BUS_DMA_WAITOK));
1669258223a3SMatthew Dillon 	if (error != 0) {
1670258223a3SMatthew Dillon 		kprintf("%s: error %d loading dmamap\n", PORTNAME(ap), error);
1671258223a3SMatthew Dillon 		return (1);
1672258223a3SMatthew Dillon 	}
1673258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PIO)
1674258223a3SMatthew Dillon 		prdt->flags |= htole32(AHCI_PRDT_FLAG_INTR);
1675258223a3SMatthew Dillon 
1676258223a3SMatthew Dillon 	cmd_slot->prdtl = htole16(prdt - ccb->ccb_cmd_table->prdt + 1);
1677258223a3SMatthew Dillon 
1678258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_data, dmap,
1679258223a3SMatthew Dillon 			(xa->flags & ATA_F_READ) ?
1680258223a3SMatthew Dillon 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1681258223a3SMatthew Dillon 
1682258223a3SMatthew Dillon 	return (0);
1683258223a3SMatthew Dillon 
1684258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1685258223a3SMatthew Dillon diagerr:
1686258223a3SMatthew Dillon 	bus_dmamap_unload(sc->sc_tag_data, dmap);
1687258223a3SMatthew Dillon 	return (1);
1688258223a3SMatthew Dillon #endif
1689258223a3SMatthew Dillon }
1690258223a3SMatthew Dillon 
1691258223a3SMatthew Dillon /*
1692258223a3SMatthew Dillon  * Callback from BUSDMA system to load the segment list.  The passed segment
1693258223a3SMatthew Dillon  * list is a temporary structure.
1694258223a3SMatthew Dillon  */
1695258223a3SMatthew Dillon static
1696258223a3SMatthew Dillon void
1697258223a3SMatthew Dillon ahci_load_prdt_callback(void *info, bus_dma_segment_t *segs, int nsegs,
1698258223a3SMatthew Dillon 			int error)
1699258223a3SMatthew Dillon {
1700258223a3SMatthew Dillon 	struct ahci_prdt *prd = *(void **)info;
1701258223a3SMatthew Dillon 	u_int64_t addr;
1702258223a3SMatthew Dillon 
1703258223a3SMatthew Dillon 	KKASSERT(nsegs <= AHCI_MAX_PRDT);
1704258223a3SMatthew Dillon 
1705258223a3SMatthew Dillon 	while (nsegs) {
1706258223a3SMatthew Dillon 		addr = segs->ds_addr;
1707258223a3SMatthew Dillon 		prd->dba_hi = htole32((u_int32_t)(addr >> 32));
1708258223a3SMatthew Dillon 		prd->dba_lo = htole32((u_int32_t)addr);
1709258223a3SMatthew Dillon #ifdef DIAGNOSTIC
1710258223a3SMatthew Dillon 		KKASSERT((addr & 1) == 0);
1711258223a3SMatthew Dillon 		KKASSERT((segs->ds_len & 1) == 0);
1712258223a3SMatthew Dillon #endif
1713258223a3SMatthew Dillon 		prd->flags = htole32(segs->ds_len - 1);
1714258223a3SMatthew Dillon 		--nsegs;
1715258223a3SMatthew Dillon 		if (nsegs)
1716258223a3SMatthew Dillon 			++prd;
1717258223a3SMatthew Dillon 		++segs;
1718258223a3SMatthew Dillon 	}
1719258223a3SMatthew Dillon 	*(void **)info = prd;	/* return last valid segment */
1720258223a3SMatthew Dillon }
1721258223a3SMatthew Dillon 
1722258223a3SMatthew Dillon void
1723258223a3SMatthew Dillon ahci_unload_prdt(struct ahci_ccb *ccb)
1724258223a3SMatthew Dillon {
1725258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1726258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1727258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
1728258223a3SMatthew Dillon 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
1729258223a3SMatthew Dillon 
1730258223a3SMatthew Dillon 	if (xa->datalen != 0) {
1731258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_data, dmap,
1732258223a3SMatthew Dillon 				(xa->flags & ATA_F_READ) ?
1733258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1734258223a3SMatthew Dillon 
1735258223a3SMatthew Dillon 		bus_dmamap_unload(sc->sc_tag_data, dmap);
1736258223a3SMatthew Dillon 
1737258223a3SMatthew Dillon 		if (ccb->ccb_xa.flags & ATA_F_NCQ)
1738258223a3SMatthew Dillon 			xa->resid = 0;
1739258223a3SMatthew Dillon 		else
1740258223a3SMatthew Dillon 			xa->resid = xa->datalen -
1741258223a3SMatthew Dillon 			    le32toh(ccb->ccb_cmd_hdr->prdbc);
1742258223a3SMatthew Dillon 	}
1743258223a3SMatthew Dillon }
1744258223a3SMatthew Dillon 
17455f8c1efdSMatthew Dillon /*
17465f8c1efdSMatthew Dillon  * Start a command and poll for completion.
17475f8c1efdSMatthew Dillon  *
17483209f581SMatthew Dillon  * timeout is in ms and only counts once the command gets on-chip.
17493209f581SMatthew Dillon  *
17505f8c1efdSMatthew Dillon  * NOTE: If the caller specifies a NULL timeout function the caller is
17515f8c1efdSMatthew Dillon  *	 responsible for clearing hardware state on failure, but we will
17525f8c1efdSMatthew Dillon  *	 deal with removing the ccb from any pending queue.
17535f8c1efdSMatthew Dillon  *
17545f8c1efdSMatthew Dillon  * NOTE: NCQ should never be used with this function.
1755cf5f3a81SMatthew Dillon  *
1756cf5f3a81SMatthew Dillon  * NOTE: If the port is in a failed state and stopped we do not try
1757cf5f3a81SMatthew Dillon  *	 to activate the ccb.
17585f8c1efdSMatthew Dillon  */
1759258223a3SMatthew Dillon int
1760258223a3SMatthew Dillon ahci_poll(struct ahci_ccb *ccb, int timeout, void (*timeout_fn)(void *))
1761258223a3SMatthew Dillon {
1762258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
1763*f4553de1SMatthew Dillon 	int xtimeout = timeout * 2;
1764258223a3SMatthew Dillon 
1765cf5f3a81SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR) {
1766cf5f3a81SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
1767cf5f3a81SMatthew Dillon 		return(1);
1768cf5f3a81SMatthew Dillon 	}
1769258223a3SMatthew Dillon 	crit_enter();
1770258223a3SMatthew Dillon 	ahci_start(ccb);
17711980eff3SMatthew Dillon 
1772258223a3SMatthew Dillon 	do {
1773*f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
17743209f581SMatthew Dillon 		if (ccb->ccb_xa.state != ATA_S_ONCHIP &&
17753209f581SMatthew Dillon 		    ccb->ccb_xa.state != ATA_S_PENDING) {
1776258223a3SMatthew Dillon 			crit_exit();
1777258223a3SMatthew Dillon 			return (0);
1778258223a3SMatthew Dillon 		}
17793209f581SMatthew Dillon 		ahci_os_sleep(100);
1780*f4553de1SMatthew Dillon 		if (xtimeout < 0) {
1781*f4553de1SMatthew Dillon 			kprintf("poll timeout %d xa.state = %d\n", timeout, ccb->ccb_xa.state);
1782*f4553de1SMatthew Dillon 			Debugger("Excessive poll");
1783*f4553de1SMatthew Dillon 			break;
1784*f4553de1SMatthew Dillon 		}
1785*f4553de1SMatthew Dillon 		xtimeout -= 100;
17863209f581SMatthew Dillon 		if (ccb->ccb_xa.state == ATA_S_ONCHIP)
17873209f581SMatthew Dillon 			timeout -= 100;
17883209f581SMatthew Dillon 	} while (timeout > 0);
17895f8c1efdSMatthew Dillon 
17905f8c1efdSMatthew Dillon 	kprintf("%s: Poll timed-out for slot %d state %d\n",
17911980eff3SMatthew Dillon 		ATANAME(ap, ccb->ccb_xa.at), ccb->ccb_slot, ccb->ccb_xa.state);
17925f8c1efdSMatthew Dillon 
17935f8c1efdSMatthew Dillon 	if (timeout_fn != NULL) {
1794258223a3SMatthew Dillon 		timeout_fn(ccb);
17955f8c1efdSMatthew Dillon 	} else {
17965f8c1efdSMatthew Dillon 		if (ccb->ccb_xa.state == ATA_S_PENDING)
17975f8c1efdSMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
17985f8c1efdSMatthew Dillon 		ccb->ccb_xa.state = ATA_S_TIMEOUT;
17995f8c1efdSMatthew Dillon 	}
1800258223a3SMatthew Dillon 	crit_exit();
1801258223a3SMatthew Dillon 
1802258223a3SMatthew Dillon 	return (1);
1803258223a3SMatthew Dillon }
1804258223a3SMatthew Dillon 
18053209f581SMatthew Dillon static
18063209f581SMatthew Dillon __inline
18073209f581SMatthew Dillon void
18083209f581SMatthew Dillon ahci_start_timeout(struct ahci_ccb *ccb)
18093209f581SMatthew Dillon {
18103209f581SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_DESIRED) {
18113209f581SMatthew Dillon 		ccb->ccb_xa.flags |= ATA_F_TIMEOUT_RUNNING;
18123209f581SMatthew Dillon 		callout_reset(&ccb->ccb_timeout,
18133209f581SMatthew Dillon 			      (ccb->ccb_xa.timeout * hz + 999) / 1000,
18143209f581SMatthew Dillon 			      ahci_ata_cmd_timeout_unserialized, ccb);
18153209f581SMatthew Dillon 	}
18163209f581SMatthew Dillon }
18173209f581SMatthew Dillon 
1818258223a3SMatthew Dillon void
1819258223a3SMatthew Dillon ahci_start(struct ahci_ccb *ccb)
1820258223a3SMatthew Dillon {
1821258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
1822258223a3SMatthew Dillon 	struct ahci_softc		*sc = ap->ap_sc;
1823258223a3SMatthew Dillon 
1824258223a3SMatthew Dillon 	KKASSERT(ccb->ccb_xa.state == ATA_S_PENDING);
1825258223a3SMatthew Dillon 
1826258223a3SMatthew Dillon 	/* Zero transferred byte count before transfer */
1827258223a3SMatthew Dillon 	ccb->ccb_cmd_hdr->prdbc = 0;
1828258223a3SMatthew Dillon 
1829258223a3SMatthew Dillon 	/* Sync command list entry and corresponding command table entry */
1830258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdh,
1831258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
1832258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1833258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_cmdt,
1834258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
1835258223a3SMatthew Dillon 			BUS_DMASYNC_PREWRITE);
1836258223a3SMatthew Dillon 
1837258223a3SMatthew Dillon 	/* Prepare RFIS area for write by controller */
1838258223a3SMatthew Dillon 	bus_dmamap_sync(sc->sc_tag_rfis,
1839258223a3SMatthew Dillon 			AHCI_DMA_MAP(ap->ap_dmamem_rfis),
1840258223a3SMatthew Dillon 			BUS_DMASYNC_PREREAD);
1841258223a3SMatthew Dillon 
1842258223a3SMatthew Dillon 	if (ccb->ccb_xa.flags & ATA_F_NCQ) {
18431980eff3SMatthew Dillon 		/*
18441980eff3SMatthew Dillon 		 * Issue NCQ commands only when there are no outstanding
18451980eff3SMatthew Dillon 		 * standard commands.
18461980eff3SMatthew Dillon 		 */
18471980eff3SMatthew Dillon 		if (ap->ap_active || TAILQ_FIRST(&ap->ap_ccb_pending)) {
1848258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
18491980eff3SMatthew Dillon 		} else {
18503209f581SMatthew Dillon 			ahci_start_timeout(ccb);
1851258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
1852258223a3SMatthew Dillon 			ap->ap_sactive |= (1 << ccb->ccb_slot);
1853258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
1854258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SACT, 1 << ccb->ccb_slot);
1855258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1856258223a3SMatthew Dillon 		}
1857258223a3SMatthew Dillon 	} else {
18585f8c1efdSMatthew Dillon 		/*
18595f8c1efdSMatthew Dillon 		 * Wait for all NCQ commands to finish before issuing standard
18601980eff3SMatthew Dillon 		 * command.  Allow up to <limit> non-NCQ commands to be active.
18611980eff3SMatthew Dillon 		 *
18621980eff3SMatthew Dillon 		 * XXX If ap is a port multiplier only allow 1.  At least the
18631980eff3SMatthew Dillon 		 *     NVidia-MCP77 part seems to barf if more then one
18641980eff3SMatthew Dillon 		 *     command is activated, even though it isn't NCQ.
18651980eff3SMatthew Dillon 		 *
18661980eff3SMatthew Dillon 		 *     If I set up more then one I get phy errors and the
18671980eff3SMatthew Dillon 		 *     port fails.
18685f8c1efdSMatthew Dillon 		 */
18691980eff3SMatthew Dillon 		int limit = (ap->ap_type == ATA_PORT_T_PM) ? 1 : 2;
18701980eff3SMatthew Dillon 		if (ap->ap_sactive || ap->ap_active_cnt >= limit) {
1871258223a3SMatthew Dillon 			TAILQ_INSERT_TAIL(&ap->ap_ccb_pending, ccb, ccb_entry);
18721980eff3SMatthew Dillon 		} else {
18733209f581SMatthew Dillon 			ahci_start_timeout(ccb);
1874258223a3SMatthew Dillon 			ap->ap_active |= 1 << ccb->ccb_slot;
1875258223a3SMatthew Dillon 			ccb->ccb_xa.state = ATA_S_ONCHIP;
1876258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << ccb->ccb_slot);
1877258223a3SMatthew Dillon 			ap->ap_active_cnt++;
1878258223a3SMatthew Dillon 		}
1879258223a3SMatthew Dillon 	}
1880258223a3SMatthew Dillon }
1881258223a3SMatthew Dillon 
1882258223a3SMatthew Dillon void
1883258223a3SMatthew Dillon ahci_issue_pending_ncq_commands(struct ahci_port *ap)
1884258223a3SMatthew Dillon {
1885258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
1886258223a3SMatthew Dillon 	u_int32_t			sact_change = 0;
1887258223a3SMatthew Dillon 
1888258223a3SMatthew Dillon 	KKASSERT(ap->ap_active_cnt == 0);
1889258223a3SMatthew Dillon 
1890258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1891258223a3SMatthew Dillon 	if (nextccb == NULL || !(nextccb->ccb_xa.flags & ATA_F_NCQ))
1892258223a3SMatthew Dillon 		return;
1893258223a3SMatthew Dillon 
1894258223a3SMatthew Dillon 	/* Start all the NCQ commands at the head of the pending list. */
1895258223a3SMatthew Dillon 	do {
1896258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
18973209f581SMatthew Dillon 		ahci_start_timeout(nextccb);
1898258223a3SMatthew Dillon 		sact_change |= 1 << nextccb->ccb_slot;
1899258223a3SMatthew Dillon 		nextccb->ccb_xa.state = ATA_S_ONCHIP;
1900258223a3SMatthew Dillon 		nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1901258223a3SMatthew Dillon 	} while (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ));
1902258223a3SMatthew Dillon 
1903258223a3SMatthew Dillon 	ap->ap_sactive |= sact_change;
1904258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_SACT, sact_change);
1905258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CI, sact_change);
1906258223a3SMatthew Dillon 
1907258223a3SMatthew Dillon 	return;
1908258223a3SMatthew Dillon }
1909258223a3SMatthew Dillon 
1910258223a3SMatthew Dillon void
1911258223a3SMatthew Dillon ahci_issue_pending_commands(struct ahci_port *ap, int last_was_ncq)
1912258223a3SMatthew Dillon {
1913258223a3SMatthew Dillon 	struct ahci_ccb			*nextccb;
1914258223a3SMatthew Dillon 
1915258223a3SMatthew Dillon 	nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
1916258223a3SMatthew Dillon 	if (nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ)) {
1917258223a3SMatthew Dillon 		KKASSERT(last_was_ncq == 0);	/* otherwise it should have
1918258223a3SMatthew Dillon 						 * been started already. */
1919258223a3SMatthew Dillon 
19201980eff3SMatthew Dillon 		/*
19211980eff3SMatthew Dillon 		 * Issue NCQ commands only when there are no outstanding
19221980eff3SMatthew Dillon 		 * standard commands.
19231980eff3SMatthew Dillon 		 */
1924258223a3SMatthew Dillon 		if (ap->ap_active == 0)
1925258223a3SMatthew Dillon 			ahci_issue_pending_ncq_commands(ap);
1926258223a3SMatthew Dillon 		else
19271980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
1928258223a3SMatthew Dillon 	} else if (nextccb) {
19291980eff3SMatthew Dillon 		if (ap->ap_sactive || last_was_ncq)
1930258223a3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt == 0);
1931258223a3SMatthew Dillon 
19321980eff3SMatthew Dillon 		/*
19331980eff3SMatthew Dillon 		 * Wait for all NCQ commands to finish before issuing standard
19341980eff3SMatthew Dillon 		 * command.  Then keep up to 2 standard commands on-chip at
19351980eff3SMatthew Dillon 		 * a time.
19361980eff3SMatthew Dillon 		 */
19371980eff3SMatthew Dillon 		if (ap->ap_sactive)
1938258223a3SMatthew Dillon 			return;
1939258223a3SMatthew Dillon 
19401980eff3SMatthew Dillon 		while (ap->ap_active_cnt < 2 &&
19411980eff3SMatthew Dillon 		       nextccb && (nextccb->ccb_xa.flags & ATA_F_NCQ) == 0) {
1942258223a3SMatthew Dillon 			TAILQ_REMOVE(&ap->ap_ccb_pending, nextccb, ccb_entry);
19433209f581SMatthew Dillon 			ahci_start_timeout(nextccb);
1944258223a3SMatthew Dillon 			ap->ap_active |= 1 << nextccb->ccb_slot;
1945258223a3SMatthew Dillon 			nextccb->ccb_xa.state = ATA_S_ONCHIP;
1946258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, 1 << nextccb->ccb_slot);
1947258223a3SMatthew Dillon 			ap->ap_active_cnt++;
1948258223a3SMatthew Dillon 			nextccb = TAILQ_FIRST(&ap->ap_ccb_pending);
19491980eff3SMatthew Dillon 		}
1950258223a3SMatthew Dillon 	}
1951258223a3SMatthew Dillon }
1952258223a3SMatthew Dillon 
1953258223a3SMatthew Dillon void
1954258223a3SMatthew Dillon ahci_intr(void *arg)
1955258223a3SMatthew Dillon {
1956258223a3SMatthew Dillon 	struct ahci_softc	*sc = arg;
1957*f4553de1SMatthew Dillon 	struct ahci_port	*ap;
1958258223a3SMatthew Dillon 	u_int32_t		is, ack = 0;
1959258223a3SMatthew Dillon 	int			port;
1960258223a3SMatthew Dillon 
1961*f4553de1SMatthew Dillon 	/*
1962*f4553de1SMatthew Dillon 	 * Check if the master enable is up, and whether any interrupts are
1963*f4553de1SMatthew Dillon 	 * pending.
1964*f4553de1SMatthew Dillon 	 */
1965*f4553de1SMatthew Dillon 	if ((sc->sc_flags & AHCI_F_INT_GOOD) == 0)
1966*f4553de1SMatthew Dillon 		return;
1967258223a3SMatthew Dillon 	is = ahci_read(sc, AHCI_REG_IS);
1968258223a3SMatthew Dillon 	if (is == 0 || is == 0xffffffff)
1969258223a3SMatthew Dillon 		return;
1970258223a3SMatthew Dillon 	ack = is;
1971258223a3SMatthew Dillon 
1972258223a3SMatthew Dillon #ifdef AHCI_COALESCE
1973258223a3SMatthew Dillon 	/* Check coalescing interrupt first */
1974258223a3SMatthew Dillon 	if (is & sc->sc_ccc_mask) {
1975258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: command coalescing interrupt\n",
1976258223a3SMatthew Dillon 		    DEVNAME(sc));
1977258223a3SMatthew Dillon 		is &= ~sc->sc_ccc_mask;
1978258223a3SMatthew Dillon 		is |= sc->sc_ccc_ports_cur;
1979258223a3SMatthew Dillon 	}
1980258223a3SMatthew Dillon #endif
1981258223a3SMatthew Dillon 
1982*f4553de1SMatthew Dillon 	/*
1983*f4553de1SMatthew Dillon 	 * Process interrupts for each port in a non-blocking fashion.
1984*f4553de1SMatthew Dillon 	 */
1985258223a3SMatthew Dillon 	while (is) {
1986258223a3SMatthew Dillon 		port = ffs(is) - 1;
1987*f4553de1SMatthew Dillon 		ap = sc->sc_ports[port];
1988*f4553de1SMatthew Dillon 		if (ap) {
1989*f4553de1SMatthew Dillon 			if (ahci_os_lock_port_nb(ap) == 0) {
1990*f4553de1SMatthew Dillon 				ahci_port_intr(ap, 0);
1991*f4553de1SMatthew Dillon 				ahci_os_unlock_port(ap);
1992*f4553de1SMatthew Dillon 			} else {
1993*f4553de1SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_IE, 0);
1994*f4553de1SMatthew Dillon 				ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
1995*f4553de1SMatthew Dillon 			}
1996*f4553de1SMatthew Dillon 		}
1997258223a3SMatthew Dillon 		is &= ~(1 << port);
1998258223a3SMatthew Dillon 	}
1999258223a3SMatthew Dillon 
2000258223a3SMatthew Dillon 	/* Finally, acknowledge global interrupt */
2001258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_IS, ack);
2002258223a3SMatthew Dillon }
2003258223a3SMatthew Dillon 
2004*f4553de1SMatthew Dillon /*
2005*f4553de1SMatthew Dillon  * Core called from helper thread.
2006*f4553de1SMatthew Dillon  */
20073209f581SMatthew Dillon void
2008*f4553de1SMatthew Dillon ahci_port_thread_core(struct ahci_port *ap, int mask)
2009*f4553de1SMatthew Dillon {
2010*f4553de1SMatthew Dillon 	struct ahci_ccb	*ccb;
2011*f4553de1SMatthew Dillon 	int i;
2012*f4553de1SMatthew Dillon 
2013*f4553de1SMatthew Dillon 	/*
2014*f4553de1SMatthew Dillon 	 * Process any expired timedouts.
2015*f4553de1SMatthew Dillon 	 */
2016*f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
2017*f4553de1SMatthew Dillon 	if (mask & AP_SIGF_TIMEOUT) {
2018*f4553de1SMatthew Dillon 		kprintf("%s: timeout", PORTNAME(ap));
2019*f4553de1SMatthew Dillon 		for (i = 0; i < ap->ap_sc->sc_ncmds; i++) {
2020*f4553de1SMatthew Dillon 			ccb = &ap->ap_ccbs[i];
2021*f4553de1SMatthew Dillon 			if (ccb->ccb_xa.flags & ATA_F_TIMEOUT_EXPIRED) {
2022*f4553de1SMatthew Dillon 				kprintf("%s: timeout slot %d\n",
2023*f4553de1SMatthew Dillon 					PORTNAME(ap), ccb->ccb_slot);
2024*f4553de1SMatthew Dillon 				ahci_ata_cmd_timeout(ccb);
2025*f4553de1SMatthew Dillon 			}
2026*f4553de1SMatthew Dillon 		}
2027*f4553de1SMatthew Dillon 	}
2028*f4553de1SMatthew Dillon 
2029*f4553de1SMatthew Dillon 	/*
2030*f4553de1SMatthew Dillon 	 * Process port interrupts which require a higher level of
2031*f4553de1SMatthew Dillon 	 * intervention.
2032*f4553de1SMatthew Dillon 	 */
2033*f4553de1SMatthew Dillon 	if (mask & AP_SIGF_PORTINT) {
2034*f4553de1SMatthew Dillon 		ahci_port_intr(ap, 1);
2035*f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
2036*f4553de1SMatthew Dillon 		ahci_port_interrupt_enable(ap);
2037*f4553de1SMatthew Dillon 	} else {
2038*f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
2039*f4553de1SMatthew Dillon 	}
2040*f4553de1SMatthew Dillon }
2041*f4553de1SMatthew Dillon 
2042*f4553de1SMatthew Dillon /*
2043*f4553de1SMatthew Dillon  * Core per-port interrupt handler.
2044*f4553de1SMatthew Dillon  *
2045*f4553de1SMatthew Dillon  * If blockable is 0 we cannot call ahci_os_sleep() at all and we can only
2046*f4553de1SMatthew Dillon  * deal with normal command completions which do not require blocking.
2047*f4553de1SMatthew Dillon  */
2048*f4553de1SMatthew Dillon void
2049*f4553de1SMatthew Dillon ahci_port_intr(struct ahci_port *ap, int blockable)
2050258223a3SMatthew Dillon {
2051258223a3SMatthew Dillon 	struct ahci_softc	*sc = ap->ap_sc;
20523209f581SMatthew Dillon 	u_int32_t		is, ci_saved, ci_masked;
205322181ab7SMatthew Dillon 	int			slot;
2054258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = NULL;
20551980eff3SMatthew Dillon 	struct ata_port		*ccb_at = NULL;
2056258223a3SMatthew Dillon 	volatile u_int32_t	*active;
2057258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2058258223a3SMatthew Dillon 	u_int32_t		tmp;
2059258223a3SMatthew Dillon #endif
2060*f4553de1SMatthew Dillon 	const u_int32_t		blockable_mask = AHCI_PREG_IS_TFES |
2061*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_IFS |
2062*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PCS |
2063*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_PRCS |
2064*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_HBFS |
2065*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_OFS |
2066*f4553de1SMatthew Dillon 						 AHCI_PREG_IS_UFS;
2067*f4553de1SMatthew Dillon 
206822181ab7SMatthew Dillon 	enum { NEED_NOTHING, NEED_RESTART, NEED_HOTPLUG_INSERT,
206922181ab7SMatthew Dillon 	       NEED_HOTPLUG_REMOVE } need = NEED_NOTHING;
2070258223a3SMatthew Dillon 
2071258223a3SMatthew Dillon 	is = ahci_pread(ap, AHCI_PREG_IS);
2072*f4553de1SMatthew Dillon 
2073*f4553de1SMatthew Dillon 	/*
2074*f4553de1SMatthew Dillon 	 * All basic command completions are always processed.
2075*f4553de1SMatthew Dillon 	 */
2076cec07d75SMatthew Dillon 	if (is & AHCI_PREG_IS_DPS)
2077cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, is & AHCI_PREG_IS_DPS);
2078258223a3SMatthew Dillon 
2079*f4553de1SMatthew Dillon 	/*
2080*f4553de1SMatthew Dillon 	 * If we can't block then we can't handle these here.  Disable
2081*f4553de1SMatthew Dillon 	 * the interrupts in question so we don't live-lock, the helper
2082*f4553de1SMatthew Dillon 	 * thread will re-enable them.
2083*f4553de1SMatthew Dillon 	 *
2084*f4553de1SMatthew Dillon 	 * If the port is in a completely failed state we do not want
2085*f4553de1SMatthew Dillon 	 * to drop through to failed-command-processinf if blockable is 0,
2086*f4553de1SMatthew Dillon 	 * just let the thread deal with it all.
2087*f4553de1SMatthew Dillon 	 */
2088*f4553de1SMatthew Dillon 	if (blockable == 0) {
2089*f4553de1SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
2090*f4553de1SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE,
2091*f4553de1SMatthew Dillon 				    ahci_pread(ap, AHCI_PREG_IE) & ~is);
2092*f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2093*f4553de1SMatthew Dillon 			return;
2094*f4553de1SMatthew Dillon 		}
2095*f4553de1SMatthew Dillon 		if (is & blockable_mask) {
2096*f4553de1SMatthew Dillon 			is &= blockable_mask | AHCI_PREG_IS_DHRS;
2097*f4553de1SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IE,
2098*f4553de1SMatthew Dillon 				    ahci_pread(ap, AHCI_PREG_IE) & ~is);
2099*f4553de1SMatthew Dillon 			ahci_os_signal_port_thread(ap, AP_SIGF_PORTINT);
2100*f4553de1SMatthew Dillon 		}
2101*f4553de1SMatthew Dillon 	}
2102*f4553de1SMatthew Dillon 
21031980eff3SMatthew Dillon #if 0
21041980eff3SMatthew Dillon 	kprintf("%s: INTERRUPT %b\n", PORTNAME(ap),
21051980eff3SMatthew Dillon 		is, AHCI_PFMT_IS);
21061980eff3SMatthew Dillon #endif
21071980eff3SMatthew Dillon 
21083209f581SMatthew Dillon 	/*
2109*f4553de1SMatthew Dillon 	 * Either NCQ or non-NCQ commands will be active, never both.
21103209f581SMatthew Dillon 	 */
2111258223a3SMatthew Dillon 	if (ap->ap_sactive) {
2112258223a3SMatthew Dillon 		KKASSERT(ap->ap_active == 0);
2113258223a3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt == 0);
2114258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_SACT);
2115258223a3SMatthew Dillon 		active = &ap->ap_sactive;
2116258223a3SMatthew Dillon 	} else {
2117258223a3SMatthew Dillon 		ci_saved = ahci_pread(ap, AHCI_PREG_CI);
2118258223a3SMatthew Dillon 		active = &ap->ap_active;
2119258223a3SMatthew Dillon 	}
2120258223a3SMatthew Dillon 
21211980eff3SMatthew Dillon 	if (is & AHCI_PREG_IS_TFES) {
2122cf5f3a81SMatthew Dillon 		/*
2123*f4553de1SMatthew Dillon 		 * Command failed (blockable).
2124*f4553de1SMatthew Dillon 		 *
2125*f4553de1SMatthew Dillon 		 * See AHCI 1.1 spec 6.2.2.1 and 6.2.2.2.
21261980eff3SMatthew Dillon 		 *
21271980eff3SMatthew Dillon 		 * This stops command processing.
2128cf5f3a81SMatthew Dillon 		 */
2129258223a3SMatthew Dillon 		u_int32_t tfd, serr;
2130258223a3SMatthew Dillon 		int	err_slot;
2131258223a3SMatthew Dillon 
2132258223a3SMatthew Dillon 		tfd = ahci_pread(ap, AHCI_PREG_TFD);
2133258223a3SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
2134258223a3SMatthew Dillon 
2135cf5f3a81SMatthew Dillon 		/*
2136cf5f3a81SMatthew Dillon 		 * If no NCQ commands are active the error slot is easily
2137cf5f3a81SMatthew Dillon 		 * determined, otherwise we have to extract the error
2138cf5f3a81SMatthew Dillon 		 * from the log page.
2139cf5f3a81SMatthew Dillon 		 */
2140258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
2141cf5f3a81SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(
2142cf5f3a81SMatthew Dillon 					ahci_pread(ap, AHCI_PREG_CMD));
2143258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
21441980eff3SMatthew Dillon 			ccb_at = ccb->ccb_xa.at;	/* can be NULL */
2145258223a3SMatthew Dillon 
2146258223a3SMatthew Dillon 			/* Preserve received taskfile data from the RFIS. */
2147258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
2148258223a3SMatthew Dillon 			       sizeof(struct ata_fis_d2h));
2149cf5f3a81SMatthew Dillon 		} else {
2150cf5f3a81SMatthew Dillon 			err_slot = -1;
2151cf5f3a81SMatthew Dillon 		}
2152258223a3SMatthew Dillon 
21531980eff3SMatthew Dillon 		DPRINTF(AHCI_D_VERBOSE, "%s: errd slot %d, TFD: %b, SERR: %b\n",
21541980eff3SMatthew Dillon 			PORTNAME(ap), err_slot,
21551980eff3SMatthew Dillon 			tfd, AHCI_PFMT_TFD_STS,
21561980eff3SMatthew Dillon 			serr, AHCI_PFMT_SERR);
2157258223a3SMatthew Dillon 
2158cf5f3a81SMatthew Dillon 		/* Stopping the port clears CI and SACT */
2159258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
216022181ab7SMatthew Dillon 		need = NEED_RESTART;
2161258223a3SMatthew Dillon 
2162cf5f3a81SMatthew Dillon 		/*
2163cf5f3a81SMatthew Dillon 		 * Clear SERR (primarily DIAG_X) to enable capturing of the
2164cf5f3a81SMatthew Dillon 		 * next error.
2165cf5f3a81SMatthew Dillon 		 */
2166258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, serr);
2167258223a3SMatthew Dillon 
2168258223a3SMatthew Dillon 		/* Acknowledge the interrupts we can recover from. */
2169cf5f3a81SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2170cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS));
21711980eff3SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_IFS);
2172258223a3SMatthew Dillon 
2173258223a3SMatthew Dillon 		/* If device hasn't cleared its busy status, try to idle it. */
2174258223a3SMatthew Dillon 		if (tfd & (AHCI_PREG_TFD_STS_BSY | AHCI_PREG_TFD_STS_DRQ)) {
21751980eff3SMatthew Dillon 			kprintf("%s: Attempting to idle device ccb=%p\n",
21761980eff3SMatthew Dillon 				PORTNAME(ap), ccb_at);
21771980eff3SMatthew Dillon 			if (ap->ap_flags & AP_F_IN_RESET)
21781980eff3SMatthew Dillon 				goto fatal;
21791980eff3SMatthew Dillon 			/*
21801980eff3SMatthew Dillon 			 * XXX how do we unbrick a PM target (ccb_at != NULL).
21811980eff3SMatthew Dillon 			 *
21821980eff3SMatthew Dillon 			 * For now fail the target and use CLO to clear the
21831980eff3SMatthew Dillon 			 * busy condition and make the ahci port usable for
21841980eff3SMatthew Dillon 			 * the remaining devices.
21851980eff3SMatthew Dillon 			 */
21861980eff3SMatthew Dillon 			if (ccb_at) {
21871980eff3SMatthew Dillon 				ccb_at->at_probe = ATA_PROBE_FAILED;
21881980eff3SMatthew Dillon 				ahci_port_clo(ap);
21891980eff3SMatthew Dillon 			} else if (ahci_port_reset(ap, ccb_at, 0)) {
219017eab71eSMatthew Dillon 				kprintf("%s: Unable to idle device, port "
219117eab71eSMatthew Dillon 					"bricked on us\n",
2192258223a3SMatthew Dillon 					PORTNAME(ap));
2193258223a3SMatthew Dillon 				goto fatal;
2194258223a3SMatthew Dillon 			}
2195258223a3SMatthew Dillon 
2196258223a3SMatthew Dillon 			/* Had to reset device, can't gather extended info. */
2197258223a3SMatthew Dillon 		} else if (ap->ap_sactive) {
21981980eff3SMatthew Dillon 			/*
21991980eff3SMatthew Dillon 			 * Recover the NCQ error from log page 10h.
22001980eff3SMatthew Dillon 			 *
22011980eff3SMatthew Dillon 			 * XXX NCQ currently not supported with port
22021980eff3SMatthew Dillon 			 *     multiplier.
22031980eff3SMatthew Dillon 			 */
2204258223a3SMatthew Dillon 			ahci_port_read_ncq_error(ap, &err_slot);
2205cec07d75SMatthew Dillon 			kprintf("recover from NCQ error err_slot %d\n",
2206cec07d75SMatthew Dillon 				err_slot);
2207258223a3SMatthew Dillon 			if (err_slot < 0)
2208258223a3SMatthew Dillon 				goto failall;
2209258223a3SMatthew Dillon 
2210258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: NCQ errored slot %d\n",
2211258223a3SMatthew Dillon 				PORTNAME(ap), err_slot);
2212258223a3SMatthew Dillon 
2213258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
2214258223a3SMatthew Dillon 		} else {
2215258223a3SMatthew Dillon 			/* Didn't reset, could gather extended info from log. */
22161980eff3SMatthew Dillon 			kprintf("%s: didn't reset err_slot %d "
22171980eff3SMatthew Dillon 				"sact=%08x act=%08x\n",
22181980eff3SMatthew Dillon 				PORTNAME(ap),
2219cf5f3a81SMatthew Dillon 				err_slot, ap->ap_sactive, ap->ap_active);
2220258223a3SMatthew Dillon 		}
2221258223a3SMatthew Dillon 
2222258223a3SMatthew Dillon 		/*
2223258223a3SMatthew Dillon 		 * If we couldn't determine the errored slot, reset the port
2224258223a3SMatthew Dillon 		 * and fail all the active slots.
2225258223a3SMatthew Dillon 		 */
2226258223a3SMatthew Dillon 		if (err_slot == -1) {
22271980eff3SMatthew Dillon 			if (ap->ap_flags & AP_F_IN_RESET)
22281980eff3SMatthew Dillon 				goto fatal;
22291980eff3SMatthew Dillon 			/*
22301980eff3SMatthew Dillon 			 * XXX how do we unbrick a PM target (ccb_at != NULL).
22311980eff3SMatthew Dillon 			 *
22321980eff3SMatthew Dillon 			 * For now fail the target and use CLO to clear the
22331980eff3SMatthew Dillon 			 * busy condition and make the ahci port usable for
22341980eff3SMatthew Dillon 			 * the remaining devices.
22351980eff3SMatthew Dillon 			 */
22361980eff3SMatthew Dillon 			if (ccb_at) {
22371980eff3SMatthew Dillon 				ccb_at->at_probe = ATA_PROBE_FAILED;
22381980eff3SMatthew Dillon 				ahci_port_clo(ap);
22391980eff3SMatthew Dillon 			} else if (ahci_port_reset(ap, ccb_at, 0)) {
224017eab71eSMatthew Dillon 				kprintf("%s: Unable to idle device after "
224117eab71eSMatthew Dillon 					"NCQ error, port bricked on us\n",
2242258223a3SMatthew Dillon 					PORTNAME(ap));
2243258223a3SMatthew Dillon 				goto fatal;
2244258223a3SMatthew Dillon 			}
2245258223a3SMatthew Dillon 			kprintf("%s: couldn't recover NCQ error, failing "
2246258223a3SMatthew Dillon 				"all outstanding commands.\n",
2247258223a3SMatthew Dillon 				PORTNAME(ap));
2248258223a3SMatthew Dillon 			goto failall;
2249258223a3SMatthew Dillon 		}
2250258223a3SMatthew Dillon 
2251258223a3SMatthew Dillon 		/* Clear the failed command in saved CI so completion runs. */
2252258223a3SMatthew Dillon 		ci_saved &= ~(1 << err_slot);
2253258223a3SMatthew Dillon 
2254258223a3SMatthew Dillon 		/* Note the error in the ata_xfer. */
2255258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2256258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_ERROR;
2257258223a3SMatthew Dillon 
2258258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2259258223a3SMatthew Dillon 		/* There may only be one outstanding standard command now. */
2260258223a3SMatthew Dillon 		if (ap->ap_sactive == 0) {
2261258223a3SMatthew Dillon 			tmp = ci_saved;
2262258223a3SMatthew Dillon 			if (tmp) {
2263258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
2264258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
2265258223a3SMatthew Dillon 				KKASSERT(tmp == 0);
2266258223a3SMatthew Dillon 			}
2267258223a3SMatthew Dillon 		}
2268258223a3SMatthew Dillon #endif
22691980eff3SMatthew Dillon 	} else if (is & AHCI_PREG_IS_DHRS) {
22701980eff3SMatthew Dillon 		/*
2271*f4553de1SMatthew Dillon 		 * Command posted D2H register FIS to the rfis (non-blocking).
2272*f4553de1SMatthew Dillon 		 *
22731980eff3SMatthew Dillon 		 * Command posted D2H register FIS to the rfis.  This
22748bf6a3ffSMatthew Dillon 		 * does NOT stop command processing and it is unclear
22758bf6a3ffSMatthew Dillon 		 * how we are supposed to deal with it other then using
22768bf6a3ffSMatthew Dillon 		 * only a queue of 1.
22778bf6a3ffSMatthew Dillon 		 *
22788bf6a3ffSMatthew Dillon 		 * We must copy the port rfis to the ccb and restart
22798bf6a3ffSMatthew Dillon 		 * command processing.  ahci_pm_read() does not function
22808bf6a3ffSMatthew Dillon 		 * without this support.
22811980eff3SMatthew Dillon 		 */
22821980eff3SMatthew Dillon 		int	err_slot;
22831980eff3SMatthew Dillon 
22841980eff3SMatthew Dillon 		if (ap->ap_sactive == 0) {
22851980eff3SMatthew Dillon 			err_slot = AHCI_PREG_CMD_CCS(
22861980eff3SMatthew Dillon 					ahci_pread(ap, AHCI_PREG_CMD));
22871980eff3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
22881980eff3SMatthew Dillon 			ccb_at = ccb->ccb_xa.at;	/* can be NULL */
22891980eff3SMatthew Dillon 
22901980eff3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, ap->ap_rfis->rfis,
22911980eff3SMatthew Dillon 			       sizeof(struct ata_fis_d2h));
22921980eff3SMatthew Dillon 		} else {
22931980eff3SMatthew Dillon 			kprintf("%s: Unexpected DHRS posted while "
22941980eff3SMatthew Dillon 				"NCQ running\n", PORTNAME(ap));
22951980eff3SMatthew Dillon 			err_slot = -1;
2296258223a3SMatthew Dillon 		}
2297cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_DHRS);
2298cec07d75SMatthew Dillon 		is &= ~AHCI_PREG_IS_DHRS;
22991980eff3SMatthew Dillon 	}
23001980eff3SMatthew Dillon 
23011980eff3SMatthew Dillon 	/*
2302*f4553de1SMatthew Dillon 	 * Device notification to us (non-blocking)
23031980eff3SMatthew Dillon 	 *
2304cec07d75SMatthew Dillon 	 * NOTE!  On some parts notification bits can get set without
2305cec07d75SMatthew Dillon 	 *	  generating an interrupt.  It is unclear whether this is
2306cec07d75SMatthew Dillon 	 *	  a bug in the PM (sending a DTOH device setbits with 'N' set
2307cec07d75SMatthew Dillon 	 *	  and 'I' not set), or a bug in the host controller.
2308cec07d75SMatthew Dillon 	 *
2309cec07d75SMatthew Dillon 	 *	  It only seems to occur under load.
23101980eff3SMatthew Dillon 	 */
2311cec07d75SMatthew Dillon 	if (/*(is & AHCI_PREG_IS_SDBS) &&*/ (sc->sc_cap & AHCI_REG_CAP_SSNTF)) {
23121980eff3SMatthew Dillon 		u_int32_t data;
2313cec07d75SMatthew Dillon 		const char *xstr;
23141980eff3SMatthew Dillon 
23151980eff3SMatthew Dillon 		data = ahci_pread(ap, AHCI_PREG_SNTF);
2316cec07d75SMatthew Dillon 		if (is & AHCI_PREG_IS_SDBS) {
23171980eff3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2318cec07d75SMatthew Dillon 			is &= ~AHCI_PREG_IS_SDBS;
2319cec07d75SMatthew Dillon 			xstr = " (no SDBS!)";
2320cec07d75SMatthew Dillon 		} else {
2321cec07d75SMatthew Dillon 			xstr = "";
2322cec07d75SMatthew Dillon 		}
2323cec07d75SMatthew Dillon 		if (data) {
2324cec07d75SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_SDBS);
2325cec07d75SMatthew Dillon 
2326cec07d75SMatthew Dillon 			kprintf("%s: NOTIFY %08x%s\n",
2327cec07d75SMatthew Dillon 				PORTNAME(ap), data, xstr);
2328cec07d75SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SERR, AHCI_PREG_SERR_DIAG_N);
23293209f581SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_SNTF, data);
23303209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
23311980eff3SMatthew Dillon 		}
23321980eff3SMatthew Dillon 	}
23333209f581SMatthew Dillon 
23343209f581SMatthew Dillon 	/*
2335*f4553de1SMatthew Dillon 	 * Spurious IFS errors (blockable).
2336*f4553de1SMatthew Dillon 	 *
23373209f581SMatthew Dillon 	 * Spurious IFS errors can occur while we are doing a reset
23383209f581SMatthew Dillon 	 * sequence through a PM.  Try to recover if we are being asked
23393209f581SMatthew Dillon 	 * to ignore IFS errors during these periods.
23403209f581SMatthew Dillon 	 */
23413209f581SMatthew Dillon 	if ((is & AHCI_PREG_IS_IFS) && (ap->ap_flags & AP_F_IGNORE_IFS)) {
23421980eff3SMatthew Dillon 		u_int32_t serr = ahci_pread(ap, AHCI_PREG_SERR);
23433209f581SMatthew Dillon 		if ((ap->ap_flags & AP_F_IFS_IGNORED) == 0) {
23441980eff3SMatthew Dillon 			kprintf("%s: Ignoring IFS (XXX) (IS: %b, SERR: %b)\n",
23451980eff3SMatthew Dillon 				PORTNAME(ap),
23461980eff3SMatthew Dillon 				is, AHCI_PFMT_IS,
23471980eff3SMatthew Dillon 				serr, AHCI_PFMT_SERR);
23483209f581SMatthew Dillon 			ap->ap_flags |= AP_F_IFS_IGNORED;
23493209f581SMatthew Dillon 		}
23503209f581SMatthew Dillon 		ap->ap_flags |= AP_F_IFS_OCCURED;
23511980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR, -1);
23521980eff3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS, AHCI_PREG_IS_IFS);
23531980eff3SMatthew Dillon 		is &= ~AHCI_PREG_IS_IFS;
23541980eff3SMatthew Dillon 		ahci_port_stop(ap, 0);
23551980eff3SMatthew Dillon 		ahci_port_start(ap);
23561980eff3SMatthew Dillon 		need = NEED_RESTART;
23571980eff3SMatthew Dillon 	}
2358258223a3SMatthew Dillon 
2359258223a3SMatthew Dillon 	/*
2360*f4553de1SMatthew Dillon 	 * Port change (hot-plug) (blockable).
2361258223a3SMatthew Dillon 	 *
2362258223a3SMatthew Dillon 	 * A PCS interrupt will occur on hot-plug once communication is
2363258223a3SMatthew Dillon 	 * established.
2364258223a3SMatthew Dillon 	 *
2365258223a3SMatthew Dillon 	 * A PRCS interrupt will occur on hot-unplug (and possibly also
2366258223a3SMatthew Dillon 	 * on hot-plug).
2367258223a3SMatthew Dillon 	 *
236822181ab7SMatthew Dillon 	 * XXX We can then check the CPS (Cold Presence State) bit, if
236922181ab7SMatthew Dillon 	 * supported, to determine if a device is plugged in or not and do
237022181ab7SMatthew Dillon 	 * the right thing.
237122181ab7SMatthew Dillon 	 *
237222181ab7SMatthew Dillon 	 * WARNING:  A PCS interrupt is cleared by clearing DIAG_X, and
237322181ab7SMatthew Dillon 	 *	     can also occur if an unsolicited COMINIT is received.
237422181ab7SMatthew Dillon 	 *	     If this occurs command processing is automatically
237522181ab7SMatthew Dillon 	 *	     stopped (CR goes inactive) and the port must be stopped
237622181ab7SMatthew Dillon 	 *	     and restarted.
2377258223a3SMatthew Dillon 	 */
2378258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS)) {
2379cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2380cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS));
2381cec07d75SMatthew Dillon 		is &= ~(AHCI_PREG_IS_PCS | AHCI_PREG_IS_PRCS);
2382258223a3SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_SERR,
23831980eff3SMatthew Dillon 			(AHCI_PREG_SERR_DIAG_N | AHCI_PREG_SERR_DIAG_X));
238422181ab7SMatthew Dillon 		ahci_port_stop(ap, 0);
2385258223a3SMatthew Dillon 		switch (ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_DET) {
2386258223a3SMatthew Dillon 		case AHCI_PREG_SSTS_DET_DEV:
23871980eff3SMatthew Dillon 			if (ap->ap_type == ATA_PORT_T_NONE) {
238822181ab7SMatthew Dillon 				need = NEED_HOTPLUG_INSERT;
238922181ab7SMatthew Dillon 				goto fatal;
2390258223a3SMatthew Dillon 			}
239122181ab7SMatthew Dillon 			need = NEED_RESTART;
2392258223a3SMatthew Dillon 			break;
2393258223a3SMatthew Dillon 		default:
23941980eff3SMatthew Dillon 			if (ap->ap_type != ATA_PORT_T_NONE) {
239522181ab7SMatthew Dillon 				need = NEED_HOTPLUG_REMOVE;
239622181ab7SMatthew Dillon 				goto fatal;
2397258223a3SMatthew Dillon 			}
239822181ab7SMatthew Dillon 			need = NEED_RESTART;
2399258223a3SMatthew Dillon 			break;
2400258223a3SMatthew Dillon 		}
2401258223a3SMatthew Dillon 	}
2402258223a3SMatthew Dillon 
240322181ab7SMatthew Dillon 	/*
2404*f4553de1SMatthew Dillon 	 * Check for remaining errors - they are fatal. (blockable)
240522181ab7SMatthew Dillon 	 */
2406258223a3SMatthew Dillon 	if (is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS | AHCI_PREG_IS_IFS |
2407258223a3SMatthew Dillon 		  AHCI_PREG_IS_OFS | AHCI_PREG_IS_UFS)) {
2408cec07d75SMatthew Dillon 		u_int32_t serr;
2409cec07d75SMatthew Dillon 
2410cec07d75SMatthew Dillon 		ahci_pwrite(ap, AHCI_PREG_IS,
2411cec07d75SMatthew Dillon 			    is & (AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2412cec07d75SMatthew Dillon 				  AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2413cec07d75SMatthew Dillon 				  AHCI_PREG_IS_UFS));
2414cec07d75SMatthew Dillon 		is &= ~(AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2415cec07d75SMatthew Dillon 			AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2416cec07d75SMatthew Dillon 		        AHCI_PREG_IS_UFS);
2417cec07d75SMatthew Dillon 		serr = ahci_pread(ap, AHCI_PREG_SERR);
24181980eff3SMatthew Dillon 		kprintf("%s: unrecoverable errors (IS: %b, SERR: %b), "
24194444122dSMatthew Dillon 			"disabling port.\n",
24204444122dSMatthew Dillon 			PORTNAME(ap),
24214444122dSMatthew Dillon 			is, AHCI_PFMT_IS,
24221980eff3SMatthew Dillon 			serr, AHCI_PFMT_SERR
24234444122dSMatthew Dillon 		);
2424258223a3SMatthew Dillon 		/* XXX try recovery first */
2425258223a3SMatthew Dillon 		goto fatal;
2426258223a3SMatthew Dillon 	}
2427258223a3SMatthew Dillon 
242822181ab7SMatthew Dillon 	/*
242922181ab7SMatthew Dillon 	 * Fail all outstanding commands if we know the port won't recover.
24301980eff3SMatthew Dillon 	 *
24311980eff3SMatthew Dillon 	 * We may have a ccb_at if the failed command is known and was
24321980eff3SMatthew Dillon 	 * being sent to a device over a port multiplier (PM).  In this
24331980eff3SMatthew Dillon 	 * case if the port itself has not completely failed we fail just
24341980eff3SMatthew Dillon 	 * the commands related to that target.
243522181ab7SMatthew Dillon 	 */
2436258223a3SMatthew Dillon 	if (ap->ap_state == AP_S_FATAL_ERROR) {
2437258223a3SMatthew Dillon fatal:
2438258223a3SMatthew Dillon 		ap->ap_state = AP_S_FATAL_ERROR;
2439258223a3SMatthew Dillon failall:
2440258223a3SMatthew Dillon 
2441cf5f3a81SMatthew Dillon 		/* Stopping the port clears CI/SACT */
2442cf5f3a81SMatthew Dillon 		ahci_port_stop(ap, 0);
2443258223a3SMatthew Dillon 
24441980eff3SMatthew Dillon 		/*
24451980eff3SMatthew Dillon 		 * Error all the active slots.  If running across a PM
24461980eff3SMatthew Dillon 		 * try to error out just the slots related to the target.
24471980eff3SMatthew Dillon 		 */
2448258223a3SMatthew Dillon 		ci_masked = ci_saved & *active;
2449258223a3SMatthew Dillon 		while (ci_masked) {
2450258223a3SMatthew Dillon 			slot = ffs(ci_masked) - 1;
2451258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[slot];
24521980eff3SMatthew Dillon 			if (ccb_at == ccb->ccb_xa.at ||
24531980eff3SMatthew Dillon 			    ap->ap_state == AP_S_FATAL_ERROR) {
2454258223a3SMatthew Dillon 				ci_masked &= ~(1 << slot);
2455258223a3SMatthew Dillon 				ccb->ccb_xa.state = ATA_S_ERROR;
2456258223a3SMatthew Dillon 			}
24571980eff3SMatthew Dillon 		}
2458258223a3SMatthew Dillon 
2459258223a3SMatthew Dillon 		/* Run completion for all active slots. */
2460258223a3SMatthew Dillon 		ci_saved &= ~*active;
2461258223a3SMatthew Dillon 
2462258223a3SMatthew Dillon 		/*
2463258223a3SMatthew Dillon 		 * Don't restart the port if our problems were deemed fatal.
2464258223a3SMatthew Dillon 		 *
2465258223a3SMatthew Dillon 		 * Also acknowlege all fatal interrupt sources to prevent
2466258223a3SMatthew Dillon 		 * a livelock.
2467258223a3SMatthew Dillon 		 */
2468258223a3SMatthew Dillon 		if (ap->ap_state == AP_S_FATAL_ERROR) {
246922181ab7SMatthew Dillon 			if (need == NEED_RESTART)
247022181ab7SMatthew Dillon 				need = NEED_NOTHING;
2471258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_IS,
2472258223a3SMatthew Dillon 				    AHCI_PREG_IS_TFES | AHCI_PREG_IS_HBFS |
2473258223a3SMatthew Dillon 				    AHCI_PREG_IS_IFS | AHCI_PREG_IS_OFS |
2474258223a3SMatthew Dillon 				    AHCI_PREG_IS_UFS);
2475258223a3SMatthew Dillon 		}
2476258223a3SMatthew Dillon 	}
2477258223a3SMatthew Dillon 
2478258223a3SMatthew Dillon 	/*
2479*f4553de1SMatthew Dillon 	 * CCB completion (non blocking).
2480*f4553de1SMatthew Dillon 	 *
2481258223a3SMatthew Dillon 	 * CCB completion is detected by noticing its slot's bit in CI has
2482258223a3SMatthew Dillon 	 * changed to zero some time after we activated it.
2483258223a3SMatthew Dillon 	 * If we are polling, we may only be interested in particular slot(s).
2484cf5f3a81SMatthew Dillon 	 *
2485cf5f3a81SMatthew Dillon 	 * Any active bits not saved are completed within the restrictions
2486cf5f3a81SMatthew Dillon 	 * imposed by the caller.
2487258223a3SMatthew Dillon 	 */
24883209f581SMatthew Dillon 	ci_masked = ~ci_saved & *active;
2489258223a3SMatthew Dillon 	while (ci_masked) {
2490258223a3SMatthew Dillon 		slot = ffs(ci_masked) - 1;
2491258223a3SMatthew Dillon 		ccb = &ap->ap_ccbs[slot];
2492258223a3SMatthew Dillon 		ci_masked &= ~(1 << slot);
2493258223a3SMatthew Dillon 
2494258223a3SMatthew Dillon 		DPRINTF(AHCI_D_INTR, "%s: slot %d is complete%s\n",
2495258223a3SMatthew Dillon 		    PORTNAME(ap), slot, ccb->ccb_xa.state == ATA_S_ERROR ?
2496258223a3SMatthew Dillon 		    " (error)" : "");
2497258223a3SMatthew Dillon 
2498258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdh,
2499258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_list),
2500258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2501258223a3SMatthew Dillon 
2502258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_cmdt,
2503258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_cmd_table),
2504258223a3SMatthew Dillon 				BUS_DMASYNC_POSTWRITE);
2505258223a3SMatthew Dillon 
2506258223a3SMatthew Dillon 		bus_dmamap_sync(sc->sc_tag_rfis,
2507258223a3SMatthew Dillon 				AHCI_DMA_MAP(ap->ap_dmamem_rfis),
2508258223a3SMatthew Dillon 				BUS_DMASYNC_POSTREAD);
2509258223a3SMatthew Dillon 
2510258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
25111980eff3SMatthew Dillon 		if (active == &ap->ap_active) {
25121980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
25131980eff3SMatthew Dillon 			--ap->ap_active_cnt;
25141980eff3SMatthew Dillon 		}
2515258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
2516258223a3SMatthew Dillon 	}
2517258223a3SMatthew Dillon 
2518*f4553de1SMatthew Dillon 	/*
2519*f4553de1SMatthew Dillon 	 * Cleanup.  Will not be set if non-blocking.
2520*f4553de1SMatthew Dillon 	 */
252122181ab7SMatthew Dillon 	switch(need) {
252222181ab7SMatthew Dillon 	case NEED_RESTART:
252322181ab7SMatthew Dillon 		/*
252422181ab7SMatthew Dillon 		 * A recoverable error occured and we can restart outstanding
252522181ab7SMatthew Dillon 		 * commands on the port.
252622181ab7SMatthew Dillon 		 */
252717eab71eSMatthew Dillon 		ahci_port_start(ap);
2528258223a3SMatthew Dillon 
2529258223a3SMatthew Dillon 		if (ci_saved) {
2530258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2531258223a3SMatthew Dillon 			tmp = ci_saved;
2532258223a3SMatthew Dillon 			while (tmp) {
2533258223a3SMatthew Dillon 				slot = ffs(tmp) - 1;
2534258223a3SMatthew Dillon 				tmp &= ~(1 << slot);
2535258223a3SMatthew Dillon 				ccb = &ap->ap_ccbs[slot];
2536258223a3SMatthew Dillon 				KKASSERT(ccb->ccb_xa.state == ATA_S_ONCHIP);
2537258223a3SMatthew Dillon 				KKASSERT((!!(ccb->ccb_xa.flags & ATA_F_NCQ)) ==
2538258223a3SMatthew Dillon 				    (!!ap->ap_sactive));
2539258223a3SMatthew Dillon 			}
2540258223a3SMatthew Dillon #endif
2541258223a3SMatthew Dillon 			DPRINTF(AHCI_D_VERBOSE, "%s: ahci_port_intr "
2542258223a3SMatthew Dillon 			    "re-enabling%s slots %08x\n", PORTNAME(ap),
2543258223a3SMatthew Dillon 			    ap->ap_sactive ? " NCQ" : "", ci_saved);
2544258223a3SMatthew Dillon 
2545258223a3SMatthew Dillon 			if (ap->ap_sactive)
2546258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_SACT, ci_saved);
2547258223a3SMatthew Dillon 			ahci_pwrite(ap, AHCI_PREG_CI, ci_saved);
2548258223a3SMatthew Dillon 		}
254922181ab7SMatthew Dillon 		break;
255022181ab7SMatthew Dillon 	case NEED_HOTPLUG_INSERT:
255122181ab7SMatthew Dillon 		/*
2552cf5f3a81SMatthew Dillon 		 * A hot-plug insertion event has occured and all
2553cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
25541980eff3SMatthew Dillon 		 *
25551980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
25561980eff3SMatthew Dillon 		 * resetting the port.
255722181ab7SMatthew Dillon 		 */
25581980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
255922181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device inserted\n",
256022181ab7SMatthew Dillon 				PORTNAME(ap));
25613209f581SMatthew Dillon 			ap->ap_probe = ATA_PROBE_NEED_INIT;
25623209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
25631980eff3SMatthew Dillon 		}
256422181ab7SMatthew Dillon 		break;
256522181ab7SMatthew Dillon 	case NEED_HOTPLUG_REMOVE:
2566cf5f3a81SMatthew Dillon 		/*
2567cf5f3a81SMatthew Dillon 		 * A hot-plug removal event has occured and all
2568cf5f3a81SMatthew Dillon 		 * outstanding commands have already been revoked.
25691980eff3SMatthew Dillon 		 *
25701980eff3SMatthew Dillon 		 * Don't recurse if this occurs while we are
25711980eff3SMatthew Dillon 		 * resetting the port.
2572cf5f3a81SMatthew Dillon 		 */
25731980eff3SMatthew Dillon 		if ((ap->ap_flags & AP_F_IN_RESET) == 0) {
257422181ab7SMatthew Dillon 			kprintf("%s: HOTPLUG - Device removed\n",
257522181ab7SMatthew Dillon 				PORTNAME(ap));
2576cf5f3a81SMatthew Dillon 			ahci_port_hardstop(ap);
25773209f581SMatthew Dillon 			/* ap_probe set to failed */
25783209f581SMatthew Dillon 			ahci_cam_changed(ap, NULL, -1);
25791980eff3SMatthew Dillon 		}
258022181ab7SMatthew Dillon 		break;
258122181ab7SMatthew Dillon 	default:
258222181ab7SMatthew Dillon 		break;
2583258223a3SMatthew Dillon 	}
2584258223a3SMatthew Dillon }
2585258223a3SMatthew Dillon 
2586258223a3SMatthew Dillon struct ahci_ccb *
2587258223a3SMatthew Dillon ahci_get_ccb(struct ahci_port *ap)
2588258223a3SMatthew Dillon {
2589258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
2590258223a3SMatthew Dillon 
2591258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2592258223a3SMatthew Dillon 	ccb = TAILQ_FIRST(&ap->ap_ccb_free);
2593258223a3SMatthew Dillon 	if (ccb != NULL) {
2594258223a3SMatthew Dillon 		KKASSERT(ccb->ccb_xa.state == ATA_S_PUT);
2595258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_free, ccb, ccb_entry);
2596258223a3SMatthew Dillon 		ccb->ccb_xa.state = ATA_S_SETUP;
25971980eff3SMatthew Dillon 		ccb->ccb_xa.at = NULL;
2598258223a3SMatthew Dillon 	}
2599258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2600258223a3SMatthew Dillon 
2601258223a3SMatthew Dillon 	return (ccb);
2602258223a3SMatthew Dillon }
2603258223a3SMatthew Dillon 
2604258223a3SMatthew Dillon void
2605258223a3SMatthew Dillon ahci_put_ccb(struct ahci_ccb *ccb)
2606258223a3SMatthew Dillon {
2607258223a3SMatthew Dillon 	struct ahci_port		*ap = ccb->ccb_port;
2608258223a3SMatthew Dillon 
2609258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2610258223a3SMatthew Dillon 	if (ccb->ccb_xa.state != ATA_S_COMPLETE &&
2611258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_TIMEOUT &&
2612258223a3SMatthew Dillon 	    ccb->ccb_xa.state != ATA_S_ERROR) {
2613258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_put_ccb, "
2614258223a3SMatthew Dillon 			"slot %d\n",
2615258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), ccb->ccb_xa.state,
2616258223a3SMatthew Dillon 			ccb->ccb_slot);
2617258223a3SMatthew Dillon 	}
2618258223a3SMatthew Dillon #endif
2619258223a3SMatthew Dillon 
2620258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PUT;
2621258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_EXCLUSIVE);
2622258223a3SMatthew Dillon 	TAILQ_INSERT_TAIL(&ap->ap_ccb_free, ccb, ccb_entry);
2623258223a3SMatthew Dillon 	lockmgr(&ap->ap_ccb_lock, LK_RELEASE);
2624258223a3SMatthew Dillon }
2625258223a3SMatthew Dillon 
2626258223a3SMatthew Dillon struct ahci_ccb *
2627258223a3SMatthew Dillon ahci_get_err_ccb(struct ahci_port *ap)
2628258223a3SMatthew Dillon {
2629258223a3SMatthew Dillon 	struct ahci_ccb *err_ccb;
2630258223a3SMatthew Dillon 	u_int32_t sact;
2631258223a3SMatthew Dillon 
2632258223a3SMatthew Dillon 	/* No commands may be active on the chip. */
2633258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
2634258223a3SMatthew Dillon 	if (sact != 0)
2635258223a3SMatthew Dillon 		kprintf("ahci_get_err_ccb but SACT %08x != 0?\n", sact);
2636258223a3SMatthew Dillon 	KKASSERT(ahci_pread(ap, AHCI_PREG_CI) == 0);
2637258223a3SMatthew Dillon 
2638258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2639258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy == 0);
2640258223a3SMatthew Dillon 	ap->ap_err_busy = 1;
2641258223a3SMatthew Dillon #endif
2642258223a3SMatthew Dillon 	/* Save outstanding command state. */
2643258223a3SMatthew Dillon 	ap->ap_err_saved_active = ap->ap_active;
2644258223a3SMatthew Dillon 	ap->ap_err_saved_active_cnt = ap->ap_active_cnt;
2645258223a3SMatthew Dillon 	ap->ap_err_saved_sactive = ap->ap_sactive;
2646258223a3SMatthew Dillon 
2647258223a3SMatthew Dillon 	/*
2648258223a3SMatthew Dillon 	 * Pretend we have no commands outstanding, so that completions won't
2649258223a3SMatthew Dillon 	 * run prematurely.
2650258223a3SMatthew Dillon 	 */
2651258223a3SMatthew Dillon 	ap->ap_active = ap->ap_active_cnt = ap->ap_sactive = 0;
2652258223a3SMatthew Dillon 
2653258223a3SMatthew Dillon 	/*
2654258223a3SMatthew Dillon 	 * Grab a CCB to use for error recovery.  This should never fail, as
2655258223a3SMatthew Dillon 	 * we ask atascsi to reserve one for us at init time.
2656258223a3SMatthew Dillon 	 */
2657258223a3SMatthew Dillon 	err_ccb = ahci_get_ccb(ap);
2658258223a3SMatthew Dillon 	KKASSERT(err_ccb != NULL);
2659258223a3SMatthew Dillon 	err_ccb->ccb_xa.flags = 0;
2660258223a3SMatthew Dillon 	err_ccb->ccb_done = ahci_empty_done;
2661258223a3SMatthew Dillon 
2662258223a3SMatthew Dillon 	return err_ccb;
2663258223a3SMatthew Dillon }
2664258223a3SMatthew Dillon 
2665258223a3SMatthew Dillon void
2666258223a3SMatthew Dillon ahci_put_err_ccb(struct ahci_ccb *ccb)
2667258223a3SMatthew Dillon {
2668258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_port;
2669258223a3SMatthew Dillon 	u_int32_t sact;
26705f8c1efdSMatthew Dillon 	u_int32_t ci;
2671258223a3SMatthew Dillon 
2672258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2673258223a3SMatthew Dillon 	KKASSERT(ap->ap_err_busy);
2674258223a3SMatthew Dillon #endif
26755f8c1efdSMatthew Dillon 	/*
26765f8c1efdSMatthew Dillon 	 * No commands may be active on the chip
26775f8c1efdSMatthew Dillon 	 */
2678258223a3SMatthew Dillon 	sact = ahci_pread(ap, AHCI_PREG_SACT);
26795f8c1efdSMatthew Dillon 	if (sact) {
26805f8c1efdSMatthew Dillon 		panic("ahci_port_err_ccb(%d) but SACT %08x != 0\n",
26815f8c1efdSMatthew Dillon 		      ccb->ccb_slot, sact);
2682258223a3SMatthew Dillon 	}
26835f8c1efdSMatthew Dillon 	ci = ahci_pread(ap, AHCI_PREG_CI);
26845f8c1efdSMatthew Dillon 	if (ci) {
2685cf5f3a81SMatthew Dillon 		panic("ahci_put_err_ccb(%d) but CI %08x != 0 "
2686cf5f3a81SMatthew Dillon 		      "(act=%08x sact=%08x)\n",
2687cf5f3a81SMatthew Dillon 		      ccb->ccb_slot, ci,
2688cf5f3a81SMatthew Dillon 		      ap->ap_active, ap->ap_sactive);
26895f8c1efdSMatthew Dillon 	}
2690258223a3SMatthew Dillon 
2691258223a3SMatthew Dillon 	/* Done with the CCB */
2692258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
2693258223a3SMatthew Dillon 
2694258223a3SMatthew Dillon 	/* Restore outstanding command state */
2695258223a3SMatthew Dillon 	ap->ap_sactive = ap->ap_err_saved_sactive;
2696258223a3SMatthew Dillon 	ap->ap_active_cnt = ap->ap_err_saved_active_cnt;
2697258223a3SMatthew Dillon 	ap->ap_active = ap->ap_err_saved_active;
2698258223a3SMatthew Dillon 
2699258223a3SMatthew Dillon #ifdef DIAGNOSTIC
2700258223a3SMatthew Dillon 	ap->ap_err_busy = 0;
2701258223a3SMatthew Dillon #endif
2702258223a3SMatthew Dillon }
2703258223a3SMatthew Dillon 
27041980eff3SMatthew Dillon /*
27051980eff3SMatthew Dillon  * Read log page to get NCQ error.
27061980eff3SMatthew Dillon  *
27071980eff3SMatthew Dillon  * NOTE: NCQ not currently supported on port multipliers. XXX
27081980eff3SMatthew Dillon  */
2709258223a3SMatthew Dillon int
2710258223a3SMatthew Dillon ahci_port_read_ncq_error(struct ahci_port *ap, int *err_slotp)
2711258223a3SMatthew Dillon {
2712258223a3SMatthew Dillon 	struct ahci_ccb			*ccb;
2713258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
2714258223a3SMatthew Dillon 	u_int32_t			cmd;
2715258223a3SMatthew Dillon 	struct ata_fis_h2d		*fis;
2716258223a3SMatthew Dillon 	int				rc = EIO;
2717258223a3SMatthew Dillon 
2718258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: read log page\n", PORTNAME(ap));
2719258223a3SMatthew Dillon 
2720258223a3SMatthew Dillon 	/* Save command register state. */
2721258223a3SMatthew Dillon 	cmd = ahci_pread(ap, AHCI_PREG_CMD) & ~AHCI_PREG_CMD_ICC;
2722258223a3SMatthew Dillon 
2723258223a3SMatthew Dillon 	/* Port should have been idled already.  Start it. */
2724258223a3SMatthew Dillon 	KKASSERT((cmd & AHCI_PREG_CMD_CR) == 0);
272517eab71eSMatthew Dillon 	ahci_port_start(ap);
2726258223a3SMatthew Dillon 
2727258223a3SMatthew Dillon 	/* Prep error CCB for READ LOG EXT, page 10h, 1 sector. */
2728258223a3SMatthew Dillon 	ccb = ahci_get_err_ccb(ap);
2729258223a3SMatthew Dillon 	ccb->ccb_xa.flags = ATA_F_NOWAIT | ATA_F_READ | ATA_F_POLL;
2730258223a3SMatthew Dillon 	ccb->ccb_xa.data = ap->ap_err_scratch;
2731258223a3SMatthew Dillon 	ccb->ccb_xa.datalen = 512;
2732258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
2733258223a3SMatthew Dillon 	bzero(ccb->ccb_cmd_table, sizeof(struct ahci_cmd_table));
2734258223a3SMatthew Dillon 
2735258223a3SMatthew Dillon 	fis = (struct ata_fis_h2d *)ccb->ccb_cmd_table->cfis;
2736258223a3SMatthew Dillon 	fis->type = ATA_FIS_TYPE_H2D;
2737258223a3SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD;
2738258223a3SMatthew Dillon 	fis->command = ATA_C_READ_LOG_EXT;
2739258223a3SMatthew Dillon 	fis->lba_low = 0x10;		/* queued error log page (10h) */
2740258223a3SMatthew Dillon 	fis->sector_count = 1;		/* number of sectors (1) */
2741258223a3SMatthew Dillon 	fis->sector_count_exp = 0;
2742258223a3SMatthew Dillon 	fis->lba_mid = 0;		/* starting offset */
2743258223a3SMatthew Dillon 	fis->lba_mid_exp = 0;
2744258223a3SMatthew Dillon 	fis->device = 0;
2745258223a3SMatthew Dillon 
2746258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5);	/* FIS length: 5 DWORDS */
2747258223a3SMatthew Dillon 
2748258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0) {
2749258223a3SMatthew Dillon 		rc = ENOMEM;	/* XXX caller must abort all commands */
2750258223a3SMatthew Dillon 		goto err;
2751258223a3SMatthew Dillon 	}
2752258223a3SMatthew Dillon 
2753258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_PENDING;
27543209f581SMatthew Dillon 	if (ahci_poll(ccb, 1000, NULL) != 0)
2755258223a3SMatthew Dillon 		goto err;
2756258223a3SMatthew Dillon 
2757258223a3SMatthew Dillon 	rc = 0;
2758258223a3SMatthew Dillon err:
2759258223a3SMatthew Dillon 	/* Abort our command, if it failed, by stopping command DMA. */
27601980eff3SMatthew Dillon 	if (rc && (ap->ap_active & (1 << ccb->ccb_slot))) {
2761258223a3SMatthew Dillon 		kprintf("%s: log page read failed, slot %d was still active.\n",
2762258223a3SMatthew Dillon 			PORTNAME(ap), ccb->ccb_slot);
2763258223a3SMatthew Dillon 		ahci_port_stop(ap, 0);
2764258223a3SMatthew Dillon 	}
2765258223a3SMatthew Dillon 
2766258223a3SMatthew Dillon 	/* Done with the error CCB now. */
2767258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
2768258223a3SMatthew Dillon 	ahci_put_err_ccb(ccb);
2769258223a3SMatthew Dillon 
2770258223a3SMatthew Dillon 	/* Extract failed register set and tags from the scratch space. */
2771258223a3SMatthew Dillon 	if (rc == 0) {
2772258223a3SMatthew Dillon 		struct ata_log_page_10h		*log;
2773258223a3SMatthew Dillon 		int				err_slot;
2774258223a3SMatthew Dillon 
2775258223a3SMatthew Dillon 		log = (struct ata_log_page_10h *)ap->ap_err_scratch;
2776258223a3SMatthew Dillon 		if (log->err_regs.type & ATA_LOG_10H_TYPE_NOTQUEUED) {
2777258223a3SMatthew Dillon 			/* Not queued bit was set - wasn't an NCQ error? */
2778258223a3SMatthew Dillon 			kprintf("%s: read NCQ error page, but not an NCQ "
2779258223a3SMatthew Dillon 				"error?\n",
2780258223a3SMatthew Dillon 				PORTNAME(ap));
2781258223a3SMatthew Dillon 			rc = ESRCH;
2782258223a3SMatthew Dillon 		} else {
2783258223a3SMatthew Dillon 			/* Copy back the log record as a D2H register FIS. */
2784258223a3SMatthew Dillon 			*err_slotp = err_slot = log->err_regs.type &
2785258223a3SMatthew Dillon 			    ATA_LOG_10H_TYPE_TAG_MASK;
2786258223a3SMatthew Dillon 
2787258223a3SMatthew Dillon 			ccb = &ap->ap_ccbs[err_slot];
2788258223a3SMatthew Dillon 			memcpy(&ccb->ccb_xa.rfis, &log->err_regs,
2789258223a3SMatthew Dillon 			    sizeof(struct ata_fis_d2h));
2790258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.type = ATA_FIS_TYPE_D2H;
2791258223a3SMatthew Dillon 			ccb->ccb_xa.rfis.flags = 0;
2792258223a3SMatthew Dillon 		}
2793258223a3SMatthew Dillon 	}
2794258223a3SMatthew Dillon 
2795258223a3SMatthew Dillon 	/* Restore saved CMD register state */
2796258223a3SMatthew Dillon 	ahci_pwrite(ap, AHCI_PREG_CMD, cmd);
2797258223a3SMatthew Dillon 
2798258223a3SMatthew Dillon 	return (rc);
2799258223a3SMatthew Dillon }
2800258223a3SMatthew Dillon 
2801258223a3SMatthew Dillon /*
2802258223a3SMatthew Dillon  * Allocate memory for various structures DMAd by hardware.  The maximum
2803258223a3SMatthew Dillon  * number of segments for these tags is 1 so the DMA memory will have a
2804258223a3SMatthew Dillon  * single physical base address.
2805258223a3SMatthew Dillon  */
2806258223a3SMatthew Dillon struct ahci_dmamem *
2807258223a3SMatthew Dillon ahci_dmamem_alloc(struct ahci_softc *sc, bus_dma_tag_t tag)
2808258223a3SMatthew Dillon {
2809258223a3SMatthew Dillon 	struct ahci_dmamem *adm;
2810258223a3SMatthew Dillon 	int	error;
2811258223a3SMatthew Dillon 
2812258223a3SMatthew Dillon 	adm = kmalloc(sizeof(*adm), M_DEVBUF, M_INTWAIT | M_ZERO);
2813258223a3SMatthew Dillon 
2814258223a3SMatthew Dillon 	error = bus_dmamem_alloc(tag, (void **)&adm->adm_kva,
2815258223a3SMatthew Dillon 				 BUS_DMA_ZERO, &adm->adm_map);
2816258223a3SMatthew Dillon 	if (error == 0) {
2817258223a3SMatthew Dillon 		adm->adm_tag = tag;
2818258223a3SMatthew Dillon 		error = bus_dmamap_load(tag, adm->adm_map,
2819258223a3SMatthew Dillon 					adm->adm_kva,
2820258223a3SMatthew Dillon 					bus_dma_tag_getmaxsize(tag),
2821258223a3SMatthew Dillon 					ahci_dmamem_saveseg, &adm->adm_busaddr,
2822258223a3SMatthew Dillon 					0);
2823258223a3SMatthew Dillon 	}
2824258223a3SMatthew Dillon 	if (error) {
2825258223a3SMatthew Dillon 		if (adm->adm_map) {
2826258223a3SMatthew Dillon 			bus_dmamap_destroy(tag, adm->adm_map);
2827258223a3SMatthew Dillon 			adm->adm_map = NULL;
2828258223a3SMatthew Dillon 			adm->adm_tag = NULL;
2829258223a3SMatthew Dillon 			adm->adm_kva = NULL;
2830258223a3SMatthew Dillon 		}
2831258223a3SMatthew Dillon 		kfree(adm, M_DEVBUF);
2832258223a3SMatthew Dillon 		adm = NULL;
2833258223a3SMatthew Dillon 	}
2834258223a3SMatthew Dillon 	return (adm);
2835258223a3SMatthew Dillon }
2836258223a3SMatthew Dillon 
2837258223a3SMatthew Dillon static
2838258223a3SMatthew Dillon void
2839258223a3SMatthew Dillon ahci_dmamem_saveseg(void *info, bus_dma_segment_t *segs, int nsegs, int error)
2840258223a3SMatthew Dillon {
2841258223a3SMatthew Dillon 	KKASSERT(error == 0);
2842258223a3SMatthew Dillon 	KKASSERT(nsegs == 1);
2843258223a3SMatthew Dillon 	*(bus_addr_t *)info = segs->ds_addr;
2844258223a3SMatthew Dillon }
2845258223a3SMatthew Dillon 
2846258223a3SMatthew Dillon 
2847258223a3SMatthew Dillon void
2848258223a3SMatthew Dillon ahci_dmamem_free(struct ahci_softc *sc, struct ahci_dmamem *adm)
2849258223a3SMatthew Dillon {
2850258223a3SMatthew Dillon 	if (adm->adm_map) {
2851258223a3SMatthew Dillon 		bus_dmamap_unload(adm->adm_tag, adm->adm_map);
2852258223a3SMatthew Dillon 		bus_dmamap_destroy(adm->adm_tag, adm->adm_map);
2853258223a3SMatthew Dillon 		adm->adm_map = NULL;
2854258223a3SMatthew Dillon 		adm->adm_tag = NULL;
2855258223a3SMatthew Dillon 		adm->adm_kva = NULL;
2856258223a3SMatthew Dillon 	}
2857258223a3SMatthew Dillon 	kfree(adm, M_DEVBUF);
2858258223a3SMatthew Dillon }
2859258223a3SMatthew Dillon 
2860258223a3SMatthew Dillon u_int32_t
2861258223a3SMatthew Dillon ahci_read(struct ahci_softc *sc, bus_size_t r)
2862258223a3SMatthew Dillon {
2863258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2864258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
2865258223a3SMatthew Dillon 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
2866258223a3SMatthew Dillon }
2867258223a3SMatthew Dillon 
2868258223a3SMatthew Dillon void
2869258223a3SMatthew Dillon ahci_write(struct ahci_softc *sc, bus_size_t r, u_int32_t v)
2870258223a3SMatthew Dillon {
2871258223a3SMatthew Dillon 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2872258223a3SMatthew Dillon 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2873258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
2874258223a3SMatthew Dillon }
2875258223a3SMatthew Dillon 
2876258223a3SMatthew Dillon int
2877258223a3SMatthew Dillon ahci_wait_ne(struct ahci_softc *sc, bus_size_t r, u_int32_t mask,
2878258223a3SMatthew Dillon 	     u_int32_t target)
2879258223a3SMatthew Dillon {
2880258223a3SMatthew Dillon 	int				i;
2881258223a3SMatthew Dillon 
2882258223a3SMatthew Dillon 	for (i = 0; i < 1000; i++) {
2883258223a3SMatthew Dillon 		if ((ahci_read(sc, r) & mask) != target)
2884258223a3SMatthew Dillon 			return (0);
28853209f581SMatthew Dillon 		ahci_os_sleep(1);
2886258223a3SMatthew Dillon 	}
2887258223a3SMatthew Dillon 
2888258223a3SMatthew Dillon 	return (1);
2889258223a3SMatthew Dillon }
2890258223a3SMatthew Dillon 
2891258223a3SMatthew Dillon u_int32_t
2892258223a3SMatthew Dillon ahci_pread(struct ahci_port *ap, bus_size_t r)
2893258223a3SMatthew Dillon {
2894258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2895258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_READ);
2896258223a3SMatthew Dillon 	return (bus_space_read_4(ap->ap_sc->sc_iot, ap->ap_ioh, r));
2897258223a3SMatthew Dillon }
2898258223a3SMatthew Dillon 
2899258223a3SMatthew Dillon void
2900258223a3SMatthew Dillon ahci_pwrite(struct ahci_port *ap, bus_size_t r, u_int32_t v)
2901258223a3SMatthew Dillon {
2902258223a3SMatthew Dillon 	bus_space_write_4(ap->ap_sc->sc_iot, ap->ap_ioh, r, v);
2903258223a3SMatthew Dillon 	bus_space_barrier(ap->ap_sc->sc_iot, ap->ap_ioh, r, 4,
2904258223a3SMatthew Dillon 			  BUS_SPACE_BARRIER_WRITE);
2905258223a3SMatthew Dillon }
2906258223a3SMatthew Dillon 
2907258223a3SMatthew Dillon int
2908cec85a37SMatthew Dillon ahci_pwait_eq(struct ahci_port *ap, int timeout,
2909cec85a37SMatthew Dillon 	      bus_size_t r, u_int32_t mask, u_int32_t target)
2910258223a3SMatthew Dillon {
2911258223a3SMatthew Dillon 	int				i;
2912258223a3SMatthew Dillon 
2913cec85a37SMatthew Dillon 	for (i = 0; i < timeout; i++) {
2914258223a3SMatthew Dillon 		if ((ahci_pread(ap, r) & mask) == target)
2915258223a3SMatthew Dillon 			return (0);
29163209f581SMatthew Dillon 		ahci_os_sleep(1);
2917258223a3SMatthew Dillon 	}
2918258223a3SMatthew Dillon 
2919258223a3SMatthew Dillon 	return (1);
2920258223a3SMatthew Dillon }
2921258223a3SMatthew Dillon 
29221980eff3SMatthew Dillon /*
29231980eff3SMatthew Dillon  * Acquire an ata transfer.
29241980eff3SMatthew Dillon  *
29251980eff3SMatthew Dillon  * Pass a NULL at for direct-attached transfers, and a non-NULL at for
29261980eff3SMatthew Dillon  * targets that go through the port multiplier.
29271980eff3SMatthew Dillon  */
2928258223a3SMatthew Dillon struct ata_xfer *
29291980eff3SMatthew Dillon ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at)
2930258223a3SMatthew Dillon {
2931258223a3SMatthew Dillon 	struct ahci_ccb		*ccb;
2932258223a3SMatthew Dillon 
2933258223a3SMatthew Dillon 	ccb = ahci_get_ccb(ap);
2934258223a3SMatthew Dillon 	if (ccb == NULL) {
2935258223a3SMatthew Dillon 		DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer: NULL ccb\n",
2936258223a3SMatthew Dillon 		    PORTNAME(ap));
2937258223a3SMatthew Dillon 		return (NULL);
2938258223a3SMatthew Dillon 	}
2939258223a3SMatthew Dillon 
2940258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "%s: ahci_ata_get_xfer got slot %d\n",
2941258223a3SMatthew Dillon 	    PORTNAME(ap), ccb->ccb_slot);
2942258223a3SMatthew Dillon 
29431980eff3SMatthew Dillon 	ccb->ccb_xa.at = at;
2944258223a3SMatthew Dillon 	ccb->ccb_xa.fis->type = ATA_FIS_TYPE_H2D;
2945258223a3SMatthew Dillon 
2946258223a3SMatthew Dillon 	return (&ccb->ccb_xa);
2947258223a3SMatthew Dillon }
2948258223a3SMatthew Dillon 
2949258223a3SMatthew Dillon void
2950258223a3SMatthew Dillon ahci_ata_put_xfer(struct ata_xfer *xa)
2951258223a3SMatthew Dillon {
2952258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
2953258223a3SMatthew Dillon 
2954258223a3SMatthew Dillon 	DPRINTF(AHCI_D_XFER, "ahci_ata_put_xfer slot %d\n", ccb->ccb_slot);
2955258223a3SMatthew Dillon 
2956258223a3SMatthew Dillon 	ahci_put_ccb(ccb);
2957258223a3SMatthew Dillon }
2958258223a3SMatthew Dillon 
2959258223a3SMatthew Dillon int
2960258223a3SMatthew Dillon ahci_ata_cmd(struct ata_xfer *xa)
2961258223a3SMatthew Dillon {
2962258223a3SMatthew Dillon 	struct ahci_ccb			*ccb = (struct ahci_ccb *)xa;
2963258223a3SMatthew Dillon 	struct ahci_cmd_hdr		*cmd_slot;
2964258223a3SMatthew Dillon 
2965258223a3SMatthew Dillon 	KKASSERT(xa->state == ATA_S_SETUP);
2966258223a3SMatthew Dillon 
2967258223a3SMatthew Dillon 	if (ccb->ccb_port->ap_state == AP_S_FATAL_ERROR)
2968258223a3SMatthew Dillon 		goto failcmd;
29691980eff3SMatthew Dillon #if 0
29701980eff3SMatthew Dillon 	kprintf("%s: started std command %b ccb %d ccb_at %p %d\n",
29711980eff3SMatthew Dillon 		ATANAME(ccb->ccb_port, ccb->ccb_xa.at),
29721980eff3SMatthew Dillon 		ahci_pread(ccb->ccb_port, AHCI_PREG_CMD), AHCI_PFMT_CMD,
29731980eff3SMatthew Dillon 		ccb->ccb_slot,
29741980eff3SMatthew Dillon 		ccb->ccb_xa.at,
29751980eff3SMatthew Dillon 		ccb->ccb_xa.at ? ccb->ccb_xa.at->at_target : -1);
29761980eff3SMatthew Dillon #endif
2977258223a3SMatthew Dillon 
2978258223a3SMatthew Dillon 	ccb->ccb_done = ahci_ata_cmd_done;
2979258223a3SMatthew Dillon 
2980258223a3SMatthew Dillon 	cmd_slot = ccb->ccb_cmd_hdr;
2981258223a3SMatthew Dillon 	cmd_slot->flags = htole16(5); /* FIS length (in DWORDs) */
29821980eff3SMatthew Dillon 	if (ccb->ccb_xa.at) {
29831980eff3SMatthew Dillon 		cmd_slot->flags |= htole16(ccb->ccb_xa.at->at_target <<
29841980eff3SMatthew Dillon 					   AHCI_CMD_LIST_FLAG_PMP_SHIFT);
29851980eff3SMatthew Dillon 	}
2986258223a3SMatthew Dillon 
2987258223a3SMatthew Dillon 	if (xa->flags & ATA_F_WRITE)
2988258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_W);
2989258223a3SMatthew Dillon 
2990258223a3SMatthew Dillon 	if (xa->flags & ATA_F_PACKET)
2991258223a3SMatthew Dillon 		cmd_slot->flags |= htole16(AHCI_CMD_LIST_FLAG_A);
2992258223a3SMatthew Dillon 
2993258223a3SMatthew Dillon 	if (ahci_load_prdt(ccb) != 0)
2994258223a3SMatthew Dillon 		goto failcmd;
2995258223a3SMatthew Dillon 
2996258223a3SMatthew Dillon 	xa->state = ATA_S_PENDING;
2997258223a3SMatthew Dillon 
2998258223a3SMatthew Dillon 	if (xa->flags & ATA_F_POLL) {
2999258223a3SMatthew Dillon 		ahci_poll(ccb, xa->timeout, ahci_ata_cmd_timeout);
3000258223a3SMatthew Dillon 		return (ATA_COMPLETE);
3001258223a3SMatthew Dillon 	}
3002258223a3SMatthew Dillon 
3003258223a3SMatthew Dillon 	crit_enter();
3004*f4553de1SMatthew Dillon 	KKASSERT((xa->flags & ATA_F_TIMEOUT_EXPIRED) == 0);
30053209f581SMatthew Dillon 	xa->flags |= ATA_F_TIMEOUT_DESIRED;
3006258223a3SMatthew Dillon 	ahci_start(ccb);
3007258223a3SMatthew Dillon 	crit_exit();
3008258223a3SMatthew Dillon 	return (ATA_QUEUED);
3009258223a3SMatthew Dillon 
3010258223a3SMatthew Dillon failcmd:
3011258223a3SMatthew Dillon 	crit_enter();
3012258223a3SMatthew Dillon 	xa->state = ATA_S_ERROR;
3013258223a3SMatthew Dillon 	xa->complete(xa);
3014258223a3SMatthew Dillon 	crit_exit();
3015258223a3SMatthew Dillon 	return (ATA_ERROR);
3016258223a3SMatthew Dillon }
3017258223a3SMatthew Dillon 
3018258223a3SMatthew Dillon void
3019258223a3SMatthew Dillon ahci_ata_cmd_done(struct ahci_ccb *ccb)
3020258223a3SMatthew Dillon {
3021258223a3SMatthew Dillon 	struct ata_xfer			*xa = &ccb->ccb_xa;
3022258223a3SMatthew Dillon 
3023258223a3SMatthew Dillon 	if (xa->flags & ATA_F_TIMEOUT_RUNNING) {
3024258223a3SMatthew Dillon 		xa->flags &= ~ATA_F_TIMEOUT_RUNNING;
3025258223a3SMatthew Dillon 		callout_stop(&ccb->ccb_timeout);
3026258223a3SMatthew Dillon 	}
3027*f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_DESIRED | ATA_F_TIMEOUT_EXPIRED);
3028258223a3SMatthew Dillon 
3029*f4553de1SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP || xa->state == ATA_S_ERROR) {
3030258223a3SMatthew Dillon 		ahci_issue_pending_commands(ccb->ccb_port,
3031258223a3SMatthew Dillon 					    xa->flags & ATA_F_NCQ);
3032*f4553de1SMatthew Dillon 	}
3033258223a3SMatthew Dillon 
3034258223a3SMatthew Dillon 	ahci_unload_prdt(ccb);
3035258223a3SMatthew Dillon 
3036258223a3SMatthew Dillon 	if (xa->state == ATA_S_ONCHIP)
3037258223a3SMatthew Dillon 		xa->state = ATA_S_COMPLETE;
3038258223a3SMatthew Dillon #ifdef DIAGNOSTIC
3039258223a3SMatthew Dillon 	else if (xa->state != ATA_S_ERROR && xa->state != ATA_S_TIMEOUT)
3040258223a3SMatthew Dillon 		kprintf("%s: invalid ata_xfer state %02x in ahci_ata_cmd_done, "
3041258223a3SMatthew Dillon 			"slot %d\n",
3042258223a3SMatthew Dillon 			PORTNAME(ccb->ccb_port), xa->state, ccb->ccb_slot);
3043258223a3SMatthew Dillon #endif
3044258223a3SMatthew Dillon 	if (xa->state != ATA_S_TIMEOUT)
3045258223a3SMatthew Dillon 		xa->complete(xa);
3046258223a3SMatthew Dillon }
3047258223a3SMatthew Dillon 
3048*f4553de1SMatthew Dillon /*
3049*f4553de1SMatthew Dillon  * Timeout from callout, MPSAFE - nothing can mess with the CCB's flags
3050*f4553de1SMatthew Dillon  * while the callout is runing.
3051*f4553de1SMatthew Dillon  *
3052*f4553de1SMatthew Dillon  * We can't safely get the port lock here or delay, we could block
3053*f4553de1SMatthew Dillon  * the callout thread.
3054*f4553de1SMatthew Dillon  */
3055258223a3SMatthew Dillon static void
3056258223a3SMatthew Dillon ahci_ata_cmd_timeout_unserialized(void *arg)
3057258223a3SMatthew Dillon {
3058258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
3059258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
3060258223a3SMatthew Dillon 
3061*f4553de1SMatthew Dillon 	ccb->ccb_xa.flags &= ~ATA_F_TIMEOUT_RUNNING;
3062*f4553de1SMatthew Dillon 	ccb->ccb_xa.flags |= ATA_F_TIMEOUT_EXPIRED;
3063*f4553de1SMatthew Dillon 	ahci_os_signal_port_thread(ap, AP_SIGF_TIMEOUT);
3064258223a3SMatthew Dillon }
3065258223a3SMatthew Dillon 
30661980eff3SMatthew Dillon void
3067258223a3SMatthew Dillon ahci_ata_cmd_timeout(void *arg)
3068258223a3SMatthew Dillon {
3069258223a3SMatthew Dillon 	struct ahci_ccb		*ccb = arg;
3070258223a3SMatthew Dillon 	struct ata_xfer		*xa = &ccb->ccb_xa;
3071258223a3SMatthew Dillon 	struct ahci_port	*ap = ccb->ccb_port;
3072258223a3SMatthew Dillon 	volatile u_int32_t	*active;
3073258223a3SMatthew Dillon 	int			ccb_was_started, ncq_cmd;
3074131be210SMatthew Dillon 	int			status;
3075258223a3SMatthew Dillon 
3076258223a3SMatthew Dillon 	crit_enter();
3077cf5f3a81SMatthew Dillon 	kprintf("%s: CMD TIMEOUT cmd-reg 0x%b\n"
3078cf5f3a81SMatthew Dillon 		"\tsactive=%08x active=%08x\n"
3079258223a3SMatthew Dillon 		"\t   sact=%08x     ci=%08x\n",
30801980eff3SMatthew Dillon 		ATANAME(ap, ccb->ccb_xa.at),
3081258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CMD), AHCI_PFMT_CMD,
3082cf5f3a81SMatthew Dillon 		ap->ap_sactive, ap->ap_active,
3083258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_SACT),
3084258223a3SMatthew Dillon 		ahci_pread(ap, AHCI_PREG_CI));
3085258223a3SMatthew Dillon 
30869e145b23SMatthew Dillon 	/*
30879e145b23SMatthew Dillon 	 * NOTE: Timeout will not be running if the command was polled.
30883209f581SMatthew Dillon 	 *	 If we got here at least one of these flags should be set.
30899e145b23SMatthew Dillon 	 */
30903209f581SMatthew Dillon 	KKASSERT(xa->flags & (ATA_F_POLL | ATA_F_TIMEOUT_DESIRED |
30913209f581SMatthew Dillon 			      ATA_F_TIMEOUT_RUNNING));
3092*f4553de1SMatthew Dillon 	xa->flags &= ~(ATA_F_TIMEOUT_RUNNING | ATA_F_TIMEOUT_EXPIRED);
3093258223a3SMatthew Dillon 	ncq_cmd = (xa->flags & ATA_F_NCQ);
3094258223a3SMatthew Dillon 	active = ncq_cmd ? &ap->ap_sactive : &ap->ap_active;
3095258223a3SMatthew Dillon 
3096258223a3SMatthew Dillon 	if (ccb->ccb_xa.state == ATA_S_PENDING) {
3097258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command for slot %d timed out "
3098258223a3SMatthew Dillon 		    "before it got on chip\n", PORTNAME(ap), ccb->ccb_slot);
3099258223a3SMatthew Dillon 		TAILQ_REMOVE(&ap->ap_ccb_pending, ccb, ccb_entry);
3100258223a3SMatthew Dillon 		ccb_was_started = 0;
3101258223a3SMatthew Dillon 	} else if (ccb->ccb_xa.state != ATA_S_ONCHIP) {
3102258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d already "
3103258223a3SMatthew Dillon 		    "handled%s\n", PORTNAME(ap), ccb->ccb_slot,
3104258223a3SMatthew Dillon 		    (*active & (1 << ccb->ccb_slot)) ?
3105258223a3SMatthew Dillon 		    " but slot is still active?" : ".");
3106258223a3SMatthew Dillon 		goto ret;
3107258223a3SMatthew Dillon 	} else if ((ahci_pread(ap, ncq_cmd ? AHCI_PREG_SACT : AHCI_PREG_CI) &
3108258223a3SMatthew Dillon 		    (1 << ccb->ccb_slot)) == 0 &&
3109258223a3SMatthew Dillon 		   (*active & (1 << ccb->ccb_slot))) {
3110258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: command slot %d completed but "
3111258223a3SMatthew Dillon 		    "IRQ handler didn't detect it.  Why?\n", PORTNAME(ap),
3112258223a3SMatthew Dillon 		    ccb->ccb_slot);
3113258223a3SMatthew Dillon 		*active &= ~(1 << ccb->ccb_slot);
31141980eff3SMatthew Dillon 		if (ncq_cmd == 0) {
31151980eff3SMatthew Dillon 			KKASSERT(ap->ap_active_cnt > 0);
31161980eff3SMatthew Dillon 			--ap->ap_active_cnt;
31171980eff3SMatthew Dillon 		}
3118258223a3SMatthew Dillon 		ccb->ccb_done(ccb);
3119258223a3SMatthew Dillon 		goto ret;
3120258223a3SMatthew Dillon 	} else {
3121258223a3SMatthew Dillon 		ccb_was_started = 1;
3122258223a3SMatthew Dillon 	}
3123258223a3SMatthew Dillon 
3124258223a3SMatthew Dillon 	/* Complete the slot with a timeout error. */
3125258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_TIMEOUT;
3126258223a3SMatthew Dillon 	*active &= ~(1 << ccb->ccb_slot);
31271980eff3SMatthew Dillon 	if (ncq_cmd == 0) {
31281980eff3SMatthew Dillon 		KKASSERT(ap->ap_active_cnt > 0);
31291980eff3SMatthew Dillon 		--ap->ap_active_cnt;
31301980eff3SMatthew Dillon 	}
3131258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (1)\n", PORTNAME(ap));
3132258223a3SMatthew Dillon 	ccb->ccb_done(ccb);	/* This won't issue pending commands or run the
3133258223a3SMatthew Dillon 				   atascsi completion. */
3134258223a3SMatthew Dillon 
3135258223a3SMatthew Dillon 	/* Reset port to abort running command. */
3136258223a3SMatthew Dillon 	if (ccb_was_started) {
3137258223a3SMatthew Dillon 		DPRINTF(AHCI_D_TIMEOUT, "%s: resetting port to abort%s command "
3138258223a3SMatthew Dillon 		    "in slot %d, active %08x\n", PORTNAME(ap), ncq_cmd ? " NCQ"
3139258223a3SMatthew Dillon 		    : "", ccb->ccb_slot, *active);
31401980eff3SMatthew Dillon 		/* XXX */
31413209f581SMatthew Dillon 		if (ccb->ccb_xa.at && ap->ap_type == ATA_PORT_T_PM) {
31421980eff3SMatthew Dillon 			/* XXX how do we unbrick a PM target? */
3143131be210SMatthew Dillon 			kprintf("%s: PM target bricked and timed-out, "
3144131be210SMatthew Dillon 				"disabling PM target but trying to "
3145131be210SMatthew Dillon 				"leave the port intact\n",
3146131be210SMatthew Dillon 				ATANAME(ap, ccb->ccb_xa.at));
3147131be210SMatthew Dillon 			ccb->ccb_xa.at->at_probe = ATA_PROBE_FAILED;
3148*f4553de1SMatthew Dillon 			ahci_port_intr(ap, 1);
3149131be210SMatthew Dillon 			ahci_port_stop(ap, 0);
3150131be210SMatthew Dillon 			ahci_port_clo(ap);
3151131be210SMatthew Dillon 			ahci_port_start(ap);
3152131be210SMatthew Dillon 			status = 0;
31531980eff3SMatthew Dillon 		} else if (ahci_port_reset(ap, ccb->ccb_xa.at, 0)) {
3154cf5f3a81SMatthew Dillon 			/*
3155cf5f3a81SMatthew Dillon 			 * If the softreset failed place the port in a
3156cf5f3a81SMatthew Dillon 			 * failed state and use ahci_port_intr() to cancel
3157cf5f3a81SMatthew Dillon 			 * any remaining commands.
3158cf5f3a81SMatthew Dillon 			 */
315917eab71eSMatthew Dillon 			kprintf("%s: Unable to reset during timeout, port "
316017eab71eSMatthew Dillon 				"bricked on us\n",
3161258223a3SMatthew Dillon 				PORTNAME(ap));
3162258223a3SMatthew Dillon 			ap->ap_state = AP_S_FATAL_ERROR;
3163*f4553de1SMatthew Dillon 			ahci_port_intr(ap, 1);
3164131be210SMatthew Dillon 			status = 1;
3165cf5f3a81SMatthew Dillon 		} else {
3166131be210SMatthew Dillon 			status = 0;
3167131be210SMatthew Dillon 		}
3168131be210SMatthew Dillon 		if (status == 0) {
3169cf5f3a81SMatthew Dillon 			/*
3170cf5f3a81SMatthew Dillon 			 * Restart any other commands that were aborted
3171cf5f3a81SMatthew Dillon 			 * by the reset.
3172cf5f3a81SMatthew Dillon 			 */
3173258223a3SMatthew Dillon 			if (*active) {
3174258223a3SMatthew Dillon 				DPRINTF(AHCI_D_TIMEOUT, "%s: re-enabling%s slots "
3175258223a3SMatthew Dillon 				    "%08x\n", PORTNAME(ap), ncq_cmd ? " NCQ" : "",
3176258223a3SMatthew Dillon 				    *active);
3177258223a3SMatthew Dillon 				if (ncq_cmd)
3178258223a3SMatthew Dillon 					ahci_pwrite(ap, AHCI_PREG_SACT, *active);
3179258223a3SMatthew Dillon 				ahci_pwrite(ap, AHCI_PREG_CI, *active);
3180258223a3SMatthew Dillon 			}
3181258223a3SMatthew Dillon 		}
3182cf5f3a81SMatthew Dillon 	}
3183258223a3SMatthew Dillon 
3184258223a3SMatthew Dillon 	/* Issue any pending commands now. */
3185258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: issue pending\n", PORTNAME(ap));
3186258223a3SMatthew Dillon 	if (ccb_was_started)
3187258223a3SMatthew Dillon 		ahci_issue_pending_commands(ap, ncq_cmd);
3188258223a3SMatthew Dillon 	else if (ap->ap_active == 0)
3189258223a3SMatthew Dillon 		ahci_issue_pending_ncq_commands(ap);
3190258223a3SMatthew Dillon 
3191258223a3SMatthew Dillon 	/* Complete the timed out ata_xfer I/O (may generate new I/O). */
3192258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: run completion (2)\n", PORTNAME(ap));
3193258223a3SMatthew Dillon 	xa->complete(xa);
3194258223a3SMatthew Dillon 
3195258223a3SMatthew Dillon 	DPRINTF(AHCI_D_TIMEOUT, "%s: splx\n", PORTNAME(ap));
3196258223a3SMatthew Dillon ret:
3197258223a3SMatthew Dillon 	crit_exit();
3198258223a3SMatthew Dillon }
3199258223a3SMatthew Dillon 
3200258223a3SMatthew Dillon void
3201258223a3SMatthew Dillon ahci_empty_done(struct ahci_ccb *ccb)
3202258223a3SMatthew Dillon {
3203258223a3SMatthew Dillon 	ccb->ccb_xa.state = ATA_S_COMPLETE;
3204258223a3SMatthew Dillon }
3205