Mon Jan 10 11:18:15 2011 UTC ()
Don't call bus_dmamap_load(9) and bus_dmamap_sync(9) on command xfers
if (AT_READ|AT_WRITE) in ata_c->flags is set but ata_c->bcount is zero.
Someone actually tries to put such a command and it causes
DIAGNOSTIC panic in x86/bus_dma.c:_bus_dmamap_sync().
I think bus_dma(9) API itself may allow calls with mapsize==0
but there are many MD code that asserts offset>=mapsize or len==0.

The problem is reported and fix is confirmed by Takuro KUBOTA
with XEN DOM0 kernel (which has options DIAGNOSTIC).


(tsutsui)
diff -r1.30 -r1.31 src/sys/dev/ic/ahcisata_core.c

cvs diff -r1.30 -r1.31 src/sys/dev/ic/ahcisata_core.c (switch to unified diff)

--- src/sys/dev/ic/ahcisata_core.c 2010/11/13 13:52:00 1.30
+++ src/sys/dev/ic/ahcisata_core.c 2011/01/10 11:18:14 1.31
@@ -1,1616 +1,1617 @@ @@ -1,1616 +1,1617 @@
1/* $NetBSD: ahcisata_core.c,v 1.30 2010/11/13 13:52:00 uebayasi Exp $ */ 1/* $NetBSD: ahcisata_core.c,v 1.31 2011/01/10 11:18:14 tsutsui Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2006 Manuel Bouyer. 4 * Copyright (c) 2006 Manuel Bouyer.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * 25 *
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: ahcisata_core.c,v 1.30 2010/11/13 13:52:00 uebayasi Exp $"); 29__KERNEL_RCSID(0, "$NetBSD: ahcisata_core.c,v 1.31 2011/01/10 11:18:14 tsutsui Exp $");
30 30
31#include <sys/types.h> 31#include <sys/types.h>
32#include <sys/malloc.h> 32#include <sys/malloc.h>
33#include <sys/param.h> 33#include <sys/param.h>
34#include <sys/kernel.h> 34#include <sys/kernel.h>
35#include <sys/systm.h> 35#include <sys/systm.h>
36#include <sys/disklabel.h> 36#include <sys/disklabel.h>
37#include <sys/proc.h> 37#include <sys/proc.h>
38#include <sys/buf.h> 38#include <sys/buf.h>
39 39
40#include <dev/ata/atareg.h> 40#include <dev/ata/atareg.h>
41#include <dev/ata/satavar.h> 41#include <dev/ata/satavar.h>
42#include <dev/ata/satareg.h> 42#include <dev/ata/satareg.h>
43#include <dev/ata/satafisvar.h> 43#include <dev/ata/satafisvar.h>
44#include <dev/ata/satafisreg.h> 44#include <dev/ata/satafisreg.h>
45#include <dev/ic/ahcisatavar.h> 45#include <dev/ic/ahcisatavar.h>
46 46
47#include <dev/scsipi/scsi_all.h> /* for SCSI status */ 47#include <dev/scsipi/scsi_all.h> /* for SCSI status */
48 48
49#include "atapibus.h" 49#include "atapibus.h"
50 50
51#ifdef AHCI_DEBUG 51#ifdef AHCI_DEBUG
52int ahcidebug_mask = 0x0; 52int ahcidebug_mask = 0x0;
53#endif 53#endif
54 54
55static void ahci_probe_drive(struct ata_channel *); 55static void ahci_probe_drive(struct ata_channel *);
56static void ahci_setup_channel(struct ata_channel *); 56static void ahci_setup_channel(struct ata_channel *);
57 57
58static int ahci_ata_bio(struct ata_drive_datas *, struct ata_bio *); 58static int ahci_ata_bio(struct ata_drive_datas *, struct ata_bio *);
59static void ahci_reset_drive(struct ata_drive_datas *, int); 59static void ahci_reset_drive(struct ata_drive_datas *, int);
60static void ahci_reset_channel(struct ata_channel *, int); 60static void ahci_reset_channel(struct ata_channel *, int);
61static int ahci_exec_command(struct ata_drive_datas *, struct ata_command *); 61static int ahci_exec_command(struct ata_drive_datas *, struct ata_command *);
62static int ahci_ata_addref(struct ata_drive_datas *); 62static int ahci_ata_addref(struct ata_drive_datas *);
63static void ahci_ata_delref(struct ata_drive_datas *); 63static void ahci_ata_delref(struct ata_drive_datas *);
64static void ahci_killpending(struct ata_drive_datas *); 64static void ahci_killpending(struct ata_drive_datas *);
65 65
66static void ahci_cmd_start(struct ata_channel *, struct ata_xfer *); 66static void ahci_cmd_start(struct ata_channel *, struct ata_xfer *);
67static int ahci_cmd_complete(struct ata_channel *, struct ata_xfer *, int); 67static int ahci_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
68static void ahci_cmd_done(struct ata_channel *, struct ata_xfer *, int); 68static void ahci_cmd_done(struct ata_channel *, struct ata_xfer *, int);
69static void ahci_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ; 69static void ahci_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
70static void ahci_bio_start(struct ata_channel *, struct ata_xfer *); 70static void ahci_bio_start(struct ata_channel *, struct ata_xfer *);
71static int ahci_bio_complete(struct ata_channel *, struct ata_xfer *, int); 71static int ahci_bio_complete(struct ata_channel *, struct ata_xfer *, int);
72static void ahci_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ; 72static void ahci_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
73static void ahci_channel_stop(struct ahci_softc *, struct ata_channel *, int); 73static void ahci_channel_stop(struct ahci_softc *, struct ata_channel *, int);
74static void ahci_channel_start(struct ahci_softc *, struct ata_channel *); 74static void ahci_channel_start(struct ahci_softc *, struct ata_channel *);
75static void ahci_timeout(void *); 75static void ahci_timeout(void *);
76static int ahci_dma_setup(struct ata_channel *, int, void *, size_t, int); 76static int ahci_dma_setup(struct ata_channel *, int, void *, size_t, int);
77 77
78#if NATAPIBUS > 0 78#if NATAPIBUS > 0
79static void ahci_atapibus_attach(struct atabus_softc *); 79static void ahci_atapibus_attach(struct atabus_softc *);
80static void ahci_atapi_kill_pending(struct scsipi_periph *); 80static void ahci_atapi_kill_pending(struct scsipi_periph *);
81static void ahci_atapi_minphys(struct buf *); 81static void ahci_atapi_minphys(struct buf *);
82static void ahci_atapi_scsipi_request(struct scsipi_channel *, 82static void ahci_atapi_scsipi_request(struct scsipi_channel *,
83 scsipi_adapter_req_t, void *); 83 scsipi_adapter_req_t, void *);
84static void ahci_atapi_start(struct ata_channel *, struct ata_xfer *); 84static void ahci_atapi_start(struct ata_channel *, struct ata_xfer *);
85static int ahci_atapi_complete(struct ata_channel *, struct ata_xfer *, int); 85static int ahci_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
86static void ahci_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int); 86static void ahci_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
87static void ahci_atapi_probe_device(struct atapibus_softc *, int); 87static void ahci_atapi_probe_device(struct atapibus_softc *, int);
88 88
89static const struct scsipi_bustype ahci_atapi_bustype = { 89static const struct scsipi_bustype ahci_atapi_bustype = {
90 SCSIPI_BUSTYPE_ATAPI, 90 SCSIPI_BUSTYPE_ATAPI,
91 atapi_scsipi_cmd, 91 atapi_scsipi_cmd,
92 atapi_interpret_sense, 92 atapi_interpret_sense,
93 atapi_print_addr, 93 atapi_print_addr,
94 ahci_atapi_kill_pending, 94 ahci_atapi_kill_pending,
95}; 95};
96#endif /* NATAPIBUS */ 96#endif /* NATAPIBUS */
97 97
98#define ATA_DELAY 10000 /* 10s for a drive I/O */ 98#define ATA_DELAY 10000 /* 10s for a drive I/O */
99#define ATA_RESET_DELAY 31000 /* 31s for a drive reset */ 99#define ATA_RESET_DELAY 31000 /* 31s for a drive reset */
100#define AHCI_RST_WAIT (ATA_RESET_DELAY / 10) 100#define AHCI_RST_WAIT (ATA_RESET_DELAY / 10)
101 101
102const struct ata_bustype ahci_ata_bustype = { 102const struct ata_bustype ahci_ata_bustype = {
103 SCSIPI_BUSTYPE_ATA, 103 SCSIPI_BUSTYPE_ATA,
104 ahci_ata_bio, 104 ahci_ata_bio,
105 ahci_reset_drive, 105 ahci_reset_drive,
106 ahci_reset_channel, 106 ahci_reset_channel,
107 ahci_exec_command, 107 ahci_exec_command,
108 ata_get_params, 108 ata_get_params,
109 ahci_ata_addref, 109 ahci_ata_addref,
110 ahci_ata_delref, 110 ahci_ata_delref,
111 ahci_killpending 111 ahci_killpending
112}; 112};
113 113
114static void ahci_intr_port(struct ahci_softc *, struct ahci_channel *); 114static void ahci_intr_port(struct ahci_softc *, struct ahci_channel *);
115static void ahci_setup_port(struct ahci_softc *sc, int i); 115static void ahci_setup_port(struct ahci_softc *sc, int i);
116 116
117static int 117static int
118ahci_reset(struct ahci_softc *sc) 118ahci_reset(struct ahci_softc *sc)
119{ 119{
120 int i; 120 int i;
121 121
122 /* reset controller */ 122 /* reset controller */
123 AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_HR); 123 AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_HR);
124 /* wait up to 1s for reset to complete */ 124 /* wait up to 1s for reset to complete */
125 for (i = 0; i < 1000; i++) { 125 for (i = 0; i < 1000; i++) {
126 delay(1000); 126 delay(1000);
127 if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR) == 0) 127 if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR) == 0)
128 break; 128 break;
129 } 129 }
130 if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR)) { 130 if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR)) {
131 aprint_error("%s: reset failed\n", AHCINAME(sc)); 131 aprint_error("%s: reset failed\n", AHCINAME(sc));
132 return -1; 132 return -1;
133 } 133 }
134 /* enable ahci mode */ 134 /* enable ahci mode */
135 AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_AE); 135 AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_AE);
136 return 0; 136 return 0;
137} 137}
138 138
139static void 139static void
140ahci_setup_ports(struct ahci_softc *sc) 140ahci_setup_ports(struct ahci_softc *sc)
141{ 141{
142 uint32_t ahci_ports; 142 uint32_t ahci_ports;
143 int i, port; 143 int i, port;
144  144
145 ahci_ports = AHCI_READ(sc, AHCI_PI); 145 ahci_ports = AHCI_READ(sc, AHCI_PI);
146 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) { 146 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
147 if ((ahci_ports & (1 << i)) == 0) 147 if ((ahci_ports & (1 << i)) == 0)
148 continue; 148 continue;
149 if (port >= sc->sc_atac.atac_nchannels) { 149 if (port >= sc->sc_atac.atac_nchannels) {
150 aprint_error("%s: more ports than announced\n", 150 aprint_error("%s: more ports than announced\n",
151 AHCINAME(sc)); 151 AHCINAME(sc));
152 break; 152 break;
153 } 153 }
154 ahci_setup_port(sc, i); 154 ahci_setup_port(sc, i);
155 } 155 }
156} 156}
157 157
158static void 158static void
159ahci_reprobe_drives(struct ahci_softc *sc) 159ahci_reprobe_drives(struct ahci_softc *sc)
160{ 160{
161 uint32_t ahci_ports; 161 uint32_t ahci_ports;
162 int i, port; 162 int i, port;
163 struct ahci_channel *achp; 163 struct ahci_channel *achp;
164 struct ata_channel *chp; 164 struct ata_channel *chp;
165 165
166 ahci_ports = AHCI_READ(sc, AHCI_PI); 166 ahci_ports = AHCI_READ(sc, AHCI_PI);
167 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) { 167 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
168 if ((ahci_ports & (1 << i)) == 0) 168 if ((ahci_ports & (1 << i)) == 0)
169 continue; 169 continue;
170 if (port >= sc->sc_atac.atac_nchannels) { 170 if (port >= sc->sc_atac.atac_nchannels) {
171 aprint_error("%s: more ports than announced\n", 171 aprint_error("%s: more ports than announced\n",
172 AHCINAME(sc)); 172 AHCINAME(sc));
173 break; 173 break;
174 } 174 }
175 achp = &sc->sc_channels[i]; 175 achp = &sc->sc_channels[i];
176 chp = &achp->ata_channel; 176 chp = &achp->ata_channel;
177 177
178 ahci_probe_drive(chp); 178 ahci_probe_drive(chp);
179 } 179 }
180} 180}
181 181
182static void 182static void
183ahci_setup_port(struct ahci_softc *sc, int i) 183ahci_setup_port(struct ahci_softc *sc, int i)
184{ 184{
185 struct ahci_channel *achp; 185 struct ahci_channel *achp;
186 186
187 achp = &sc->sc_channels[i]; 187 achp = &sc->sc_channels[i];
188 188
189 AHCI_WRITE(sc, AHCI_P_CLB(i), achp->ahcic_bus_cmdh); 189 AHCI_WRITE(sc, AHCI_P_CLB(i), achp->ahcic_bus_cmdh);
190 AHCI_WRITE(sc, AHCI_P_CLBU(i), (uint64_t)achp->ahcic_bus_cmdh>>32); 190 AHCI_WRITE(sc, AHCI_P_CLBU(i), (uint64_t)achp->ahcic_bus_cmdh>>32);
191 AHCI_WRITE(sc, AHCI_P_FB(i), achp->ahcic_bus_rfis); 191 AHCI_WRITE(sc, AHCI_P_FB(i), achp->ahcic_bus_rfis);
192 AHCI_WRITE(sc, AHCI_P_FBU(i), (uint64_t)achp->ahcic_bus_rfis>>32); 192 AHCI_WRITE(sc, AHCI_P_FBU(i), (uint64_t)achp->ahcic_bus_rfis>>32);
193} 193}
194 194
195static void 195static void
196ahci_enable_intrs(struct ahci_softc *sc) 196ahci_enable_intrs(struct ahci_softc *sc)
197{ 197{
198 198
199 /* clear interrupts */ 199 /* clear interrupts */
200 AHCI_WRITE(sc, AHCI_IS, AHCI_READ(sc, AHCI_IS)); 200 AHCI_WRITE(sc, AHCI_IS, AHCI_READ(sc, AHCI_IS));
201 /* enable interrupts */ 201 /* enable interrupts */
202 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE); 202 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
203} 203}
204 204
205void 205void
206ahci_attach(struct ahci_softc *sc) 206ahci_attach(struct ahci_softc *sc)
207{ 207{
208 uint32_t ahci_cap, ahci_rev, ahci_ports; 208 uint32_t ahci_cap, ahci_rev, ahci_ports;
209 int i, j, port; 209 int i, j, port;
210 struct ahci_channel *achp; 210 struct ahci_channel *achp;
211 struct ata_channel *chp; 211 struct ata_channel *chp;
212 int error; 212 int error;
213 int dmasize; 213 int dmasize;
214 void *cmdhp; 214 void *cmdhp;
215 void *cmdtblp; 215 void *cmdtblp;
216 216
217 if (ahci_reset(sc) != 0) 217 if (ahci_reset(sc) != 0)
218 return; 218 return;
219 219
220 ahci_cap = AHCI_READ(sc, AHCI_CAP); 220 ahci_cap = AHCI_READ(sc, AHCI_CAP);
221 sc->sc_atac.atac_nchannels = (ahci_cap & AHCI_CAP_NPMASK) + 1; 221 sc->sc_atac.atac_nchannels = (ahci_cap & AHCI_CAP_NPMASK) + 1;
222 sc->sc_ncmds = ((ahci_cap & AHCI_CAP_NCS) >> 8) + 1; 222 sc->sc_ncmds = ((ahci_cap & AHCI_CAP_NCS) >> 8) + 1;
223 ahci_rev = AHCI_READ(sc, AHCI_VS); 223 ahci_rev = AHCI_READ(sc, AHCI_VS);
224 aprint_normal("%s: AHCI revision ", AHCINAME(sc)); 224 aprint_normal("%s: AHCI revision ", AHCINAME(sc));
225 switch(ahci_rev) { 225 switch(ahci_rev) {
226 case AHCI_VS_10: 226 case AHCI_VS_10:
227 aprint_normal("1.0"); 227 aprint_normal("1.0");
228 break; 228 break;
229 case AHCI_VS_11: 229 case AHCI_VS_11:
230 aprint_normal("1.1"); 230 aprint_normal("1.1");
231 break; 231 break;
232 case AHCI_VS_12: 232 case AHCI_VS_12:
233 aprint_normal("1.2"); 233 aprint_normal("1.2");
234 break; 234 break;
235 default: 235 default:
236 aprint_normal("0x%x", ahci_rev); 236 aprint_normal("0x%x", ahci_rev);
237 break; 237 break;
238 } 238 }
239 239
240 aprint_normal(", %d ports, %d command slots, features 0x%x\n", 240 aprint_normal(", %d ports, %d command slots, features 0x%x\n",
241 sc->sc_atac.atac_nchannels, sc->sc_ncmds, 241 sc->sc_atac.atac_nchannels, sc->sc_ncmds,
242 ahci_cap & ~(AHCI_CAP_NPMASK|AHCI_CAP_NCS)); 242 ahci_cap & ~(AHCI_CAP_NPMASK|AHCI_CAP_NCS));
243 sc->sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DMA | ATAC_CAP_UDMA; 243 sc->sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DMA | ATAC_CAP_UDMA;
244 sc->sc_atac.atac_cap |= sc->sc_atac_capflags; 244 sc->sc_atac.atac_cap |= sc->sc_atac_capflags;
245 sc->sc_atac.atac_pio_cap = 4; 245 sc->sc_atac.atac_pio_cap = 4;
246 sc->sc_atac.atac_dma_cap = 2; 246 sc->sc_atac.atac_dma_cap = 2;
247 sc->sc_atac.atac_udma_cap = 6; 247 sc->sc_atac.atac_udma_cap = 6;
248 sc->sc_atac.atac_channels = sc->sc_chanarray; 248 sc->sc_atac.atac_channels = sc->sc_chanarray;
249 sc->sc_atac.atac_probe = ahci_probe_drive; 249 sc->sc_atac.atac_probe = ahci_probe_drive;
250 sc->sc_atac.atac_bustype_ata = &ahci_ata_bustype; 250 sc->sc_atac.atac_bustype_ata = &ahci_ata_bustype;
251 sc->sc_atac.atac_set_modes = ahci_setup_channel; 251 sc->sc_atac.atac_set_modes = ahci_setup_channel;
252#if NATAPIBUS > 0 252#if NATAPIBUS > 0
253 sc->sc_atac.atac_atapibus_attach = ahci_atapibus_attach; 253 sc->sc_atac.atac_atapibus_attach = ahci_atapibus_attach;
254#endif 254#endif
255 255
256 dmasize = 256 dmasize =
257 (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels; 257 (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels;
258 error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0, 258 error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
259 &sc->sc_cmd_hdr_seg, 1, &sc->sc_cmd_hdr_nseg, BUS_DMA_NOWAIT); 259 &sc->sc_cmd_hdr_seg, 1, &sc->sc_cmd_hdr_nseg, BUS_DMA_NOWAIT);
260 if (error) { 260 if (error) {
261 aprint_error("%s: unable to allocate command header memory" 261 aprint_error("%s: unable to allocate command header memory"
262 ", error=%d\n", AHCINAME(sc), error); 262 ", error=%d\n", AHCINAME(sc), error);
263 return; 263 return;
264 } 264 }
265 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cmd_hdr_seg, 265 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cmd_hdr_seg,
266 sc->sc_cmd_hdr_nseg, dmasize, 266 sc->sc_cmd_hdr_nseg, dmasize,
267 &cmdhp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 267 &cmdhp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
268 if (error) { 268 if (error) {
269 aprint_error("%s: unable to map command header memory" 269 aprint_error("%s: unable to map command header memory"
270 ", error=%d\n", AHCINAME(sc), error); 270 ", error=%d\n", AHCINAME(sc), error);
271 return; 271 return;
272 } 272 }
273 error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0, 273 error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
274 BUS_DMA_NOWAIT, &sc->sc_cmd_hdrd); 274 BUS_DMA_NOWAIT, &sc->sc_cmd_hdrd);
275 if (error) { 275 if (error) {
276 aprint_error("%s: unable to create command header map" 276 aprint_error("%s: unable to create command header map"
277 ", error=%d\n", AHCINAME(sc), error); 277 ", error=%d\n", AHCINAME(sc), error);
278 return; 278 return;
279 } 279 }
280 error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmd_hdrd, 280 error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmd_hdrd,
281 cmdhp, dmasize, NULL, BUS_DMA_NOWAIT); 281 cmdhp, dmasize, NULL, BUS_DMA_NOWAIT);
282 if (error) { 282 if (error) {
283 aprint_error("%s: unable to load command header map" 283 aprint_error("%s: unable to load command header map"
284 ", error=%d\n", AHCINAME(sc), error); 284 ", error=%d\n", AHCINAME(sc), error);
285 return; 285 return;
286 } 286 }
287 sc->sc_cmd_hdr = cmdhp; 287 sc->sc_cmd_hdr = cmdhp;
288 288
289 ahci_enable_intrs(sc); 289 ahci_enable_intrs(sc);
290 290
291 ahci_ports = AHCI_READ(sc, AHCI_PI); 291 ahci_ports = AHCI_READ(sc, AHCI_PI);
292 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) { 292 for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
293 if ((ahci_ports & (1 << i)) == 0) 293 if ((ahci_ports & (1 << i)) == 0)
294 continue; 294 continue;
295 if (port >= sc->sc_atac.atac_nchannels) { 295 if (port >= sc->sc_atac.atac_nchannels) {
296 aprint_error("%s: more ports than announced\n", 296 aprint_error("%s: more ports than announced\n",
297 AHCINAME(sc)); 297 AHCINAME(sc));
298 break; 298 break;
299 } 299 }
300 achp = &sc->sc_channels[i]; 300 achp = &sc->sc_channels[i];
301 chp = &achp->ata_channel; 301 chp = &achp->ata_channel;
302 sc->sc_chanarray[i] = chp; 302 sc->sc_chanarray[i] = chp;
303 chp->ch_channel = i; 303 chp->ch_channel = i;
304 chp->ch_atac = &sc->sc_atac; 304 chp->ch_atac = &sc->sc_atac;
305 chp->ch_queue = malloc(sizeof(struct ata_queue), 305 chp->ch_queue = malloc(sizeof(struct ata_queue),
306 M_DEVBUF, M_NOWAIT); 306 M_DEVBUF, M_NOWAIT);
307 if (chp->ch_queue == NULL) { 307 if (chp->ch_queue == NULL) {
308 aprint_error("%s port %d: can't allocate memory for " 308 aprint_error("%s port %d: can't allocate memory for "
309 "command queue", AHCINAME(sc), i); 309 "command queue", AHCINAME(sc), i);
310 break; 310 break;
311 } 311 }
312 dmasize = AHCI_CMDTBL_SIZE * sc->sc_ncmds; 312 dmasize = AHCI_CMDTBL_SIZE * sc->sc_ncmds;
313 error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0, 313 error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
314 &achp->ahcic_cmd_tbl_seg, 1, &achp->ahcic_cmd_tbl_nseg, 314 &achp->ahcic_cmd_tbl_seg, 1, &achp->ahcic_cmd_tbl_nseg,
315 BUS_DMA_NOWAIT); 315 BUS_DMA_NOWAIT);
316 if (error) { 316 if (error) {
317 aprint_error("%s: unable to allocate command table " 317 aprint_error("%s: unable to allocate command table "
318 "memory, error=%d\n", AHCINAME(sc), error); 318 "memory, error=%d\n", AHCINAME(sc), error);
319 break; 319 break;
320 } 320 }
321 error = bus_dmamem_map(sc->sc_dmat, &achp->ahcic_cmd_tbl_seg, 321 error = bus_dmamem_map(sc->sc_dmat, &achp->ahcic_cmd_tbl_seg,
322 achp->ahcic_cmd_tbl_nseg, dmasize, 322 achp->ahcic_cmd_tbl_nseg, dmasize,
323 &cmdtblp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 323 &cmdtblp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
324 if (error) { 324 if (error) {
325 aprint_error("%s: unable to map command table memory" 325 aprint_error("%s: unable to map command table memory"
326 ", error=%d\n", AHCINAME(sc), error); 326 ", error=%d\n", AHCINAME(sc), error);
327 break; 327 break;
328 } 328 }
329 error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0, 329 error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
330 BUS_DMA_NOWAIT, &achp->ahcic_cmd_tbld); 330 BUS_DMA_NOWAIT, &achp->ahcic_cmd_tbld);
331 if (error) { 331 if (error) {
332 aprint_error("%s: unable to create command table map" 332 aprint_error("%s: unable to create command table map"
333 ", error=%d\n", AHCINAME(sc), error); 333 ", error=%d\n", AHCINAME(sc), error);
334 break; 334 break;
335 } 335 }
336 error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_cmd_tbld, 336 error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_cmd_tbld,
337 cmdtblp, dmasize, NULL, BUS_DMA_NOWAIT); 337 cmdtblp, dmasize, NULL, BUS_DMA_NOWAIT);
338 if (error) { 338 if (error) {
339 aprint_error("%s: unable to load command table map" 339 aprint_error("%s: unable to load command table map"
340 ", error=%d\n", AHCINAME(sc), error); 340 ", error=%d\n", AHCINAME(sc), error);
341 break; 341 break;
342 } 342 }
343 achp->ahcic_cmdh = (struct ahci_cmd_header *) 343 achp->ahcic_cmdh = (struct ahci_cmd_header *)
344 ((char *)cmdhp + AHCI_CMDH_SIZE * port); 344 ((char *)cmdhp + AHCI_CMDH_SIZE * port);
345 achp->ahcic_bus_cmdh = sc->sc_cmd_hdrd->dm_segs[0].ds_addr + 345 achp->ahcic_bus_cmdh = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
346 AHCI_CMDH_SIZE * port; 346 AHCI_CMDH_SIZE * port;
347 achp->ahcic_rfis = (struct ahci_r_fis *) 347 achp->ahcic_rfis = (struct ahci_r_fis *)
348 ((char *)cmdhp +  348 ((char *)cmdhp +
349 AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +  349 AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
350 AHCI_RFIS_SIZE * port); 350 AHCI_RFIS_SIZE * port);
351 achp->ahcic_bus_rfis = sc->sc_cmd_hdrd->dm_segs[0].ds_addr + 351 achp->ahcic_bus_rfis = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
352 AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +  352 AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
353 AHCI_RFIS_SIZE * port; 353 AHCI_RFIS_SIZE * port;
354 AHCIDEBUG_PRINT(("port %d cmdh %p (0x%" PRIx64 ") " 354 AHCIDEBUG_PRINT(("port %d cmdh %p (0x%" PRIx64 ") "
355 "rfis %p (0x%" PRIx64 ")\n", i, 355 "rfis %p (0x%" PRIx64 ")\n", i,
356 achp->ahcic_cmdh, (uint64_t)achp->ahcic_bus_cmdh, 356 achp->ahcic_cmdh, (uint64_t)achp->ahcic_bus_cmdh,
357 achp->ahcic_rfis, (uint64_t)achp->ahcic_bus_rfis), 357 achp->ahcic_rfis, (uint64_t)achp->ahcic_bus_rfis),
358 DEBUG_PROBE); 358 DEBUG_PROBE);
359  359
360 for (j = 0; j < sc->sc_ncmds; j++) { 360 for (j = 0; j < sc->sc_ncmds; j++) {
361 achp->ahcic_cmd_tbl[j] = (struct ahci_cmd_tbl *) 361 achp->ahcic_cmd_tbl[j] = (struct ahci_cmd_tbl *)
362 ((char *)cmdtblp + AHCI_CMDTBL_SIZE * j); 362 ((char *)cmdtblp + AHCI_CMDTBL_SIZE * j);
363 achp->ahcic_bus_cmd_tbl[j] = 363 achp->ahcic_bus_cmd_tbl[j] =
364 achp->ahcic_cmd_tbld->dm_segs[0].ds_addr + 364 achp->ahcic_cmd_tbld->dm_segs[0].ds_addr +
365 AHCI_CMDTBL_SIZE * j; 365 AHCI_CMDTBL_SIZE * j;
366 achp->ahcic_cmdh[j].cmdh_cmdtba = 366 achp->ahcic_cmdh[j].cmdh_cmdtba =
367 htole64(achp->ahcic_bus_cmd_tbl[j]); 367 htole64(achp->ahcic_bus_cmd_tbl[j]);
368 AHCIDEBUG_PRINT(("port %d/%d tbl %p (0x%" PRIx64 ")\n", i, j, 368 AHCIDEBUG_PRINT(("port %d/%d tbl %p (0x%" PRIx64 ")\n", i, j,
369 achp->ahcic_cmd_tbl[j], 369 achp->ahcic_cmd_tbl[j],
370 (uint64_t)achp->ahcic_bus_cmd_tbl[j]), DEBUG_PROBE); 370 (uint64_t)achp->ahcic_bus_cmd_tbl[j]), DEBUG_PROBE);
371 /* The xfer DMA map */ 371 /* The xfer DMA map */
372 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 372 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
373 AHCI_NPRD, 0x400000 /* 4MB */, 0, 373 AHCI_NPRD, 0x400000 /* 4MB */, 0,
374 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, 374 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
375 &achp->ahcic_datad[j]); 375 &achp->ahcic_datad[j]);
376 if (error) { 376 if (error) {
377 aprint_error("%s: couldn't alloc xfer DMA map, " 377 aprint_error("%s: couldn't alloc xfer DMA map, "
378 "error=%d\n", AHCINAME(sc), error); 378 "error=%d\n", AHCINAME(sc), error);
379 goto end; 379 goto end;
380 } 380 }
381 } 381 }
382 ahci_setup_port(sc, i); 382 ahci_setup_port(sc, i);
383 chp->ch_ndrive = 1; 383 chp->ch_ndrive = 1;
384 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih, 384 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
385 AHCI_P_SSTS(i), 4, &achp->ahcic_sstatus) != 0) { 385 AHCI_P_SSTS(i), 4, &achp->ahcic_sstatus) != 0) {
386 aprint_error("%s: couldn't map channel %d " 386 aprint_error("%s: couldn't map channel %d "
387 "sata_status regs\n", AHCINAME(sc), i); 387 "sata_status regs\n", AHCINAME(sc), i);
388 break; 388 break;
389 } 389 }
390 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih, 390 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
391 AHCI_P_SCTL(i), 4, &achp->ahcic_scontrol) != 0) { 391 AHCI_P_SCTL(i), 4, &achp->ahcic_scontrol) != 0) {
392 aprint_error("%s: couldn't map channel %d " 392 aprint_error("%s: couldn't map channel %d "
393 "sata_control regs\n", AHCINAME(sc), i); 393 "sata_control regs\n", AHCINAME(sc), i);
394 break; 394 break;
395 } 395 }
396 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih, 396 if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
397 AHCI_P_SERR(i), 4, &achp->ahcic_serror) != 0) { 397 AHCI_P_SERR(i), 4, &achp->ahcic_serror) != 0) {
398 aprint_error("%s: couldn't map channel %d " 398 aprint_error("%s: couldn't map channel %d "
399 "sata_error regs\n", AHCINAME(sc), i); 399 "sata_error regs\n", AHCINAME(sc), i);
400 break; 400 break;
401 } 401 }
402 ata_channel_attach(chp); 402 ata_channel_attach(chp);
403 port++; 403 port++;
404end: 404end:
405 continue; 405 continue;
406 } 406 }
407} 407}
408 408
409int 409int
410ahci_detach(struct ahci_softc *sc, int flags) 410ahci_detach(struct ahci_softc *sc, int flags)
411{ 411{
412 struct atac_softc *atac; 412 struct atac_softc *atac;
413 struct ahci_channel *achp; 413 struct ahci_channel *achp;
414 struct ata_channel *chp; 414 struct ata_channel *chp;
415 struct scsipi_adapter *adapt; 415 struct scsipi_adapter *adapt;
416 uint32_t ahci_ports; 416 uint32_t ahci_ports;
417 int i, j; 417 int i, j;
418 int error; 418 int error;
419 419
420 atac = &sc->sc_atac; 420 atac = &sc->sc_atac;
421 adapt = &atac->atac_atapi_adapter._generic; 421 adapt = &atac->atac_atapi_adapter._generic;
422 422
423 ahci_ports = AHCI_READ(sc, AHCI_PI); 423 ahci_ports = AHCI_READ(sc, AHCI_PI);
424 for (i = 0; i < AHCI_MAX_PORTS; i++) { 424 for (i = 0; i < AHCI_MAX_PORTS; i++) {
425 achp = &sc->sc_channels[i]; 425 achp = &sc->sc_channels[i];
426 chp = &achp->ata_channel; 426 chp = &achp->ata_channel;
427 427
428 if ((ahci_ports & (1 << i)) == 0) 428 if ((ahci_ports & (1 << i)) == 0)
429 continue; 429 continue;
430 if (i >= sc->sc_atac.atac_nchannels) { 430 if (i >= sc->sc_atac.atac_nchannels) {
431 aprint_error("%s: more ports than announced\n", 431 aprint_error("%s: more ports than announced\n",
432 AHCINAME(sc)); 432 AHCINAME(sc));
433 break; 433 break;
434 } 434 }
435 435
436 if (chp->atabus == NULL) 436 if (chp->atabus == NULL)
437 continue; 437 continue;
438 if ((error = config_detach(chp->atabus, flags)) != 0) 438 if ((error = config_detach(chp->atabus, flags)) != 0)
439 return error; 439 return error;
440 440
441 for (j = 0; j < sc->sc_ncmds; j++) 441 for (j = 0; j < sc->sc_ncmds; j++)
442 bus_dmamap_destroy(sc->sc_dmat, achp->ahcic_datad[j]); 442 bus_dmamap_destroy(sc->sc_dmat, achp->ahcic_datad[j]);
443 443
444 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_cmd_tbld); 444 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_cmd_tbld);
445 bus_dmamap_destroy(sc->sc_dmat, achp->ahcic_cmd_tbld); 445 bus_dmamap_destroy(sc->sc_dmat, achp->ahcic_cmd_tbld);
446 bus_dmamem_unmap(sc->sc_dmat, achp->ahcic_cmd_tbl[0], 446 bus_dmamem_unmap(sc->sc_dmat, achp->ahcic_cmd_tbl[0],
447 AHCI_CMDTBL_SIZE * sc->sc_ncmds); 447 AHCI_CMDTBL_SIZE * sc->sc_ncmds);
448 bus_dmamem_free(sc->sc_dmat, &achp->ahcic_cmd_tbl_seg, 448 bus_dmamem_free(sc->sc_dmat, &achp->ahcic_cmd_tbl_seg,
449 achp->ahcic_cmd_tbl_nseg); 449 achp->ahcic_cmd_tbl_nseg);
450 450
451 free(chp->ch_queue, M_DEVBUF); 451 free(chp->ch_queue, M_DEVBUF);
452 chp->atabus = NULL; 452 chp->atabus = NULL;
453 } 453 }
454 454
455 bus_dmamap_unload(sc->sc_dmat, sc->sc_cmd_hdrd); 455 bus_dmamap_unload(sc->sc_dmat, sc->sc_cmd_hdrd);
456 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmd_hdrd); 456 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmd_hdrd);
457 bus_dmamem_unmap(sc->sc_dmat, sc->sc_cmd_hdr, 457 bus_dmamem_unmap(sc->sc_dmat, sc->sc_cmd_hdr,
458 (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels); 458 (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels);
459 bus_dmamem_free(sc->sc_dmat, &sc->sc_cmd_hdr_seg, sc->sc_cmd_hdr_nseg); 459 bus_dmamem_free(sc->sc_dmat, &sc->sc_cmd_hdr_seg, sc->sc_cmd_hdr_nseg);
460 460
461 if (adapt->adapt_refcnt != 0) 461 if (adapt->adapt_refcnt != 0)
462 return EBUSY; 462 return EBUSY;
463 463
464 return 0; 464 return 0;
465} 465}
466 466
467void 467void
468ahci_resume(struct ahci_softc *sc) 468ahci_resume(struct ahci_softc *sc)
469{ 469{
470 ahci_reset(sc); 470 ahci_reset(sc);
471 ahci_setup_ports(sc); 471 ahci_setup_ports(sc);
472 ahci_reprobe_drives(sc); 472 ahci_reprobe_drives(sc);
473 ahci_enable_intrs(sc); 473 ahci_enable_intrs(sc);
474} 474}
475 475
476int 476int
477ahci_intr(void *v) 477ahci_intr(void *v)
478{ 478{
479 struct ahci_softc *sc = v; 479 struct ahci_softc *sc = v;
480 uint32_t is; 480 uint32_t is;
481 int i, r = 0; 481 int i, r = 0;
482 482
483 while ((is = AHCI_READ(sc, AHCI_IS))) { 483 while ((is = AHCI_READ(sc, AHCI_IS))) {
484 AHCIDEBUG_PRINT(("%s ahci_intr 0x%x\n", AHCINAME(sc), is), 484 AHCIDEBUG_PRINT(("%s ahci_intr 0x%x\n", AHCINAME(sc), is),
485 DEBUG_INTR); 485 DEBUG_INTR);
486 r = 1; 486 r = 1;
487 AHCI_WRITE(sc, AHCI_IS, is); 487 AHCI_WRITE(sc, AHCI_IS, is);
488 for (i = 0; i < AHCI_MAX_PORTS; i++) 488 for (i = 0; i < AHCI_MAX_PORTS; i++)
489 if (is & (1 << i)) 489 if (is & (1 << i))
490 ahci_intr_port(sc, &sc->sc_channels[i]); 490 ahci_intr_port(sc, &sc->sc_channels[i]);
491 } 491 }
492 return r; 492 return r;
493} 493}
494 494
495static void 495static void
496ahci_intr_port(struct ahci_softc *sc, struct ahci_channel *achp) 496ahci_intr_port(struct ahci_softc *sc, struct ahci_channel *achp)
497{ 497{
498 uint32_t is, tfd; 498 uint32_t is, tfd;
499 struct ata_channel *chp = &achp->ata_channel; 499 struct ata_channel *chp = &achp->ata_channel;
500 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 500 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
501 int slot; 501 int slot;
502 502
503 is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel)); 503 is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
504 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), is); 504 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), is);
505 AHCIDEBUG_PRINT(("ahci_intr_port %s port %d is 0x%x CI 0x%x\n", AHCINAME(sc), 505 AHCIDEBUG_PRINT(("ahci_intr_port %s port %d is 0x%x CI 0x%x\n", AHCINAME(sc),
506 chp->ch_channel, is, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), 506 chp->ch_channel, is, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
507 DEBUG_INTR); 507 DEBUG_INTR);
508 508
509 if (is & (AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS | 509 if (is & (AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
510 AHCI_P_IX_OFS | AHCI_P_IX_UFS)) { 510 AHCI_P_IX_OFS | AHCI_P_IX_UFS)) {
511 slot = (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) 511 slot = (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))
512 & AHCI_P_CMD_CCS_MASK) >> AHCI_P_CMD_CCS_SHIFT; 512 & AHCI_P_CMD_CCS_MASK) >> AHCI_P_CMD_CCS_SHIFT;
513 if ((achp->ahcic_cmds_active & (1 << slot)) == 0) 513 if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
514 return; 514 return;
515 /* stop channel */ 515 /* stop channel */
516 ahci_channel_stop(sc, chp, 0); 516 ahci_channel_stop(sc, chp, 0);
517 if (slot != 0) { 517 if (slot != 0) {
518 printf("ahci_intr_port: slot %d\n", slot); 518 printf("ahci_intr_port: slot %d\n", slot);
519 panic("ahci_intr_port"); 519 panic("ahci_intr_port");
520 } 520 }
521 if (is & AHCI_P_IX_TFES) { 521 if (is & AHCI_P_IX_TFES) {
522 tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel)); 522 tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
523 chp->ch_error = 523 chp->ch_error =
524 (tfd & AHCI_P_TFD_ERR_MASK) >> AHCI_P_TFD_ERR_SHIFT; 524 (tfd & AHCI_P_TFD_ERR_MASK) >> AHCI_P_TFD_ERR_SHIFT;
525 chp->ch_status = (tfd & 0xff); 525 chp->ch_status = (tfd & 0xff);
526 } else { 526 } else {
527 /* emulate a CRC error */ 527 /* emulate a CRC error */
528 chp->ch_error = WDCE_CRC; 528 chp->ch_error = WDCE_CRC;
529 chp->ch_status = WDCS_ERR; 529 chp->ch_status = WDCS_ERR;
530 } 530 }
531 xfer->c_intr(chp, xfer, is); 531 xfer->c_intr(chp, xfer, is);
532 /* if channel has not been restarted, do it now */ 532 /* if channel has not been restarted, do it now */
533 if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) 533 if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
534 == 0) 534 == 0)
535 ahci_channel_start(sc, chp); 535 ahci_channel_start(sc, chp);
536 } else { 536 } else {
537 slot = 0; /* XXX */ 537 slot = 0; /* XXX */
538 is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel)); 538 is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
539 AHCIDEBUG_PRINT(("ahci_intr_port port %d is 0x%x act 0x%x CI 0x%x\n", 539 AHCIDEBUG_PRINT(("ahci_intr_port port %d is 0x%x act 0x%x CI 0x%x\n",
540 chp->ch_channel, is, achp->ahcic_cmds_active, 540 chp->ch_channel, is, achp->ahcic_cmds_active,
541 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_INTR); 541 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_INTR);
542 if ((achp->ahcic_cmds_active & (1 << slot)) == 0) 542 if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
543 return; 543 return;
544 if ((AHCI_READ(sc, AHCI_P_CI(chp->ch_channel)) & (1 << slot)) 544 if ((AHCI_READ(sc, AHCI_P_CI(chp->ch_channel)) & (1 << slot))
545 == 0) { 545 == 0) {
546 xfer->c_intr(chp, xfer, 0); 546 xfer->c_intr(chp, xfer, 0);
547 } 547 }
548 } 548 }
549} 549}
550 550
551static void 551static void
552ahci_reset_drive(struct ata_drive_datas *drvp, int flags) 552ahci_reset_drive(struct ata_drive_datas *drvp, int flags)
553{ 553{
554 struct ata_channel *chp = drvp->chnl_softc; 554 struct ata_channel *chp = drvp->chnl_softc;
555 ata_reset_channel(chp, flags); 555 ata_reset_channel(chp, flags);
556 return; 556 return;
557} 557}
558 558
559static void 559static void
560ahci_reset_channel(struct ata_channel *chp, int flags) 560ahci_reset_channel(struct ata_channel *chp, int flags)
561{ 561{
562 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 562 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
563 struct ahci_channel *achp = (struct ahci_channel *)chp; 563 struct ahci_channel *achp = (struct ahci_channel *)chp;
564 int i, tfd; 564 int i, tfd;
565 565
566 ahci_channel_stop(sc, chp, flags); 566 ahci_channel_stop(sc, chp, flags);
567 if (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol, 567 if (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
568 achp->ahcic_sstatus) != SStatus_DET_DEV) { 568 achp->ahcic_sstatus) != SStatus_DET_DEV) {
569 printf("%s: port reset failed\n", AHCINAME(sc)); 569 printf("%s: port reset failed\n", AHCINAME(sc));
570 /* XXX and then ? */ 570 /* XXX and then ? */
571 } 571 }
572 if (chp->ch_queue->active_xfer) { 572 if (chp->ch_queue->active_xfer) {
573 chp->ch_queue->active_xfer->c_kill_xfer(chp, 573 chp->ch_queue->active_xfer->c_kill_xfer(chp,
574 chp->ch_queue->active_xfer, KILL_RESET); 574 chp->ch_queue->active_xfer, KILL_RESET);
575 } 575 }
576 tsleep(&sc, PRIBIO, "ahcirst", mstohz(500)); 576 tsleep(&sc, PRIBIO, "ahcirst", mstohz(500));
577 /* clear port interrupt register */ 577 /* clear port interrupt register */
578 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff); 578 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
579 /* clear SErrors and start operations */ 579 /* clear SErrors and start operations */
580 ahci_channel_start(sc, chp); 580 ahci_channel_start(sc, chp);
581 /* wait 31s for BSY to clear */ 581 /* wait 31s for BSY to clear */
582 for (i = 0; i <AHCI_RST_WAIT; i++) { 582 for (i = 0; i <AHCI_RST_WAIT; i++) {
583 tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel)); 583 tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
584 if ((((tfd & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT) 584 if ((((tfd & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT)
585 & WDCS_BSY) == 0) 585 & WDCS_BSY) == 0)
586 break; 586 break;
587 tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10)); 587 tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10));
588 } 588 }
589 if (i == AHCI_RST_WAIT) 589 if (i == AHCI_RST_WAIT)
590 aprint_error("%s: BSY never cleared, TD 0x%x\n", 590 aprint_error("%s: BSY never cleared, TD 0x%x\n",
591 AHCINAME(sc), tfd); 591 AHCINAME(sc), tfd);
592 AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10), 592 AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10),
593 DEBUG_PROBE); 593 DEBUG_PROBE);
594 /* clear port interrupt register */ 594 /* clear port interrupt register */
595 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff); 595 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
596 596
597 return; 597 return;
598} 598}
599 599
600static int 600static int
601ahci_ata_addref(struct ata_drive_datas *drvp) 601ahci_ata_addref(struct ata_drive_datas *drvp)
602{ 602{
603 return 0; 603 return 0;
604} 604}
605 605
606static void 606static void
607ahci_ata_delref(struct ata_drive_datas *drvp) 607ahci_ata_delref(struct ata_drive_datas *drvp)
608{ 608{
609 return; 609 return;
610} 610}
611 611
612static void 612static void
613ahci_killpending(struct ata_drive_datas *drvp) 613ahci_killpending(struct ata_drive_datas *drvp)
614{ 614{
615 return; 615 return;
616} 616}
617 617
618static void 618static void
619ahci_probe_drive(struct ata_channel *chp) 619ahci_probe_drive(struct ata_channel *chp)
620{ 620{
621 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 621 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
622 struct ahci_channel *achp = (struct ahci_channel *)chp; 622 struct ahci_channel *achp = (struct ahci_channel *)chp;
623 int i, s; 623 int i, s;
624 uint32_t sig; 624 uint32_t sig;
625 625
626 /* XXX This should be done by other code. */ 626 /* XXX This should be done by other code. */
627 for (i = 0; i < chp->ch_ndrive; i++) { 627 for (i = 0; i < chp->ch_ndrive; i++) {
628 chp->ch_drive[i].chnl_softc = chp; 628 chp->ch_drive[i].chnl_softc = chp;
629 chp->ch_drive[i].drive = i; 629 chp->ch_drive[i].drive = i;
630 } 630 }
631 631
632 /* bring interface up, accept FISs, power up and spin up device */ 632 /* bring interface up, accept FISs, power up and spin up device */
633 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel), 633 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
634 AHCI_P_CMD_ICC_AC | AHCI_P_CMD_FRE | 634 AHCI_P_CMD_ICC_AC | AHCI_P_CMD_FRE |
635 AHCI_P_CMD_POD | AHCI_P_CMD_SUD); 635 AHCI_P_CMD_POD | AHCI_P_CMD_SUD);
636 /* reset the PHY and bring online */ 636 /* reset the PHY and bring online */
637 switch (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol, 637 switch (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
638 achp->ahcic_sstatus)) { 638 achp->ahcic_sstatus)) {
639 case SStatus_DET_DEV: 639 case SStatus_DET_DEV:
640 tsleep(&sc, PRIBIO, "ahcidv", mstohz(500)); 640 tsleep(&sc, PRIBIO, "ahcidv", mstohz(500));
641 /* clear port interrupt register */ 641 /* clear port interrupt register */
642 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff); 642 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
643 /* clear SErrors and start operations */ 643 /* clear SErrors and start operations */
644 ahci_channel_start(sc, chp); 644 ahci_channel_start(sc, chp);
645 /* wait 31s for BSY to clear */ 645 /* wait 31s for BSY to clear */
646 for (i = 0; i <AHCI_RST_WAIT; i++) { 646 for (i = 0; i <AHCI_RST_WAIT; i++) {
647 sig = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel)); 647 sig = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
648 if ((((sig & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT) 648 if ((((sig & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT)
649 & WDCS_BSY) == 0) 649 & WDCS_BSY) == 0)
650 break; 650 break;
651 tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10)); 651 tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10));
652 } 652 }
653 if (i == AHCI_RST_WAIT) { 653 if (i == AHCI_RST_WAIT) {
654 aprint_error("%s: BSY never cleared, TD 0x%x\n", 654 aprint_error("%s: BSY never cleared, TD 0x%x\n",
655 AHCINAME(sc), sig); 655 AHCINAME(sc), sig);
656 return; 656 return;
657 } 657 }
658 AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10), 658 AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10),
659 DEBUG_PROBE); 659 DEBUG_PROBE);
660 sig = AHCI_READ(sc, AHCI_P_SIG(chp->ch_channel)); 660 sig = AHCI_READ(sc, AHCI_P_SIG(chp->ch_channel));
661 AHCIDEBUG_PRINT(("%s: port %d: sig=0x%x CMD=0x%x\n", 661 AHCIDEBUG_PRINT(("%s: port %d: sig=0x%x CMD=0x%x\n",
662 AHCINAME(sc), chp->ch_channel, sig, 662 AHCINAME(sc), chp->ch_channel, sig,
663 AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))), DEBUG_PROBE); 663 AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))), DEBUG_PROBE);
664 /* 664 /*
665 * scnt and sn are supposed to be 0x1 for ATAPI, but in some 665 * scnt and sn are supposed to be 0x1 for ATAPI, but in some
666 * cases we get wrong values here, so ignore it. 666 * cases we get wrong values here, so ignore it.
667 */ 667 */
668 s = splbio(); 668 s = splbio();
669 if ((sig & 0xffff0000) == 0xeb140000) { 669 if ((sig & 0xffff0000) == 0xeb140000) {
670 chp->ch_drive[0].drive_flags |= DRIVE_ATAPI; 670 chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
671 } else 671 } else
672 chp->ch_drive[0].drive_flags |= DRIVE_ATA; 672 chp->ch_drive[0].drive_flags |= DRIVE_ATA;
673 splx(s); 673 splx(s);
674 /* clear port interrupt register */ 674 /* clear port interrupt register */
675 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff); 675 AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
676 /* and enable interrupts */ 676 /* and enable interrupts */
677 AHCI_WRITE(sc, AHCI_P_IE(chp->ch_channel), 677 AHCI_WRITE(sc, AHCI_P_IE(chp->ch_channel),
678 AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS | 678 AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
679 AHCI_P_IX_OFS | AHCI_P_IX_DPS | AHCI_P_IX_UFS | 679 AHCI_P_IX_OFS | AHCI_P_IX_DPS | AHCI_P_IX_UFS |
680 AHCI_P_IX_DHRS); 680 AHCI_P_IX_DHRS);
681 /* wait 500ms before actually starting operations */ 681 /* wait 500ms before actually starting operations */
682 tsleep(&sc, PRIBIO, "ahciprb", mstohz(500)); 682 tsleep(&sc, PRIBIO, "ahciprb", mstohz(500));
683 break; 683 break;
684 684
685 default: 685 default:
686 break; 686 break;
687 } 687 }
688} 688}
689 689
690static void 690static void
691ahci_setup_channel(struct ata_channel *chp) 691ahci_setup_channel(struct ata_channel *chp)
692{ 692{
693 return; 693 return;
694} 694}
695 695
696static int 696static int
697ahci_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c) 697ahci_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
698{ 698{
699 struct ata_channel *chp = drvp->chnl_softc; 699 struct ata_channel *chp = drvp->chnl_softc;
700 struct ata_xfer *xfer; 700 struct ata_xfer *xfer;
701 int ret; 701 int ret;
702 int s; 702 int s;
703 703
704 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 704 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
705 AHCIDEBUG_PRINT(("ahci_exec_command port %d CI 0x%x\n", 705 AHCIDEBUG_PRINT(("ahci_exec_command port %d CI 0x%x\n",
706 chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), 706 chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
707 DEBUG_XFERS); 707 DEBUG_XFERS);
708 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP : 708 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP :
709 ATAXF_NOSLEEP); 709 ATAXF_NOSLEEP);
710 if (xfer == NULL) { 710 if (xfer == NULL) {
711 return ATACMD_TRY_AGAIN; 711 return ATACMD_TRY_AGAIN;
712 } 712 }
713 if (ata_c->flags & AT_POLL) 713 if (ata_c->flags & AT_POLL)
714 xfer->c_flags |= C_POLL; 714 xfer->c_flags |= C_POLL;
715 if (ata_c->flags & AT_WAIT) 715 if (ata_c->flags & AT_WAIT)
716 xfer->c_flags |= C_WAIT; 716 xfer->c_flags |= C_WAIT;
717 xfer->c_drive = drvp->drive; 717 xfer->c_drive = drvp->drive;
718 xfer->c_databuf = ata_c->data; 718 xfer->c_databuf = ata_c->data;
719 xfer->c_bcount = ata_c->bcount; 719 xfer->c_bcount = ata_c->bcount;
720 xfer->c_cmd = ata_c; 720 xfer->c_cmd = ata_c;
721 xfer->c_start = ahci_cmd_start; 721 xfer->c_start = ahci_cmd_start;
722 xfer->c_intr = ahci_cmd_complete; 722 xfer->c_intr = ahci_cmd_complete;
723 xfer->c_kill_xfer = ahci_cmd_kill_xfer; 723 xfer->c_kill_xfer = ahci_cmd_kill_xfer;
724 s = splbio(); 724 s = splbio();
725 ata_exec_xfer(chp, xfer); 725 ata_exec_xfer(chp, xfer);
726#ifdef DIAGNOSTIC 726#ifdef DIAGNOSTIC
727 if ((ata_c->flags & AT_POLL) != 0 && 727 if ((ata_c->flags & AT_POLL) != 0 &&
728 (ata_c->flags & AT_DONE) == 0) 728 (ata_c->flags & AT_DONE) == 0)
729 panic("ahci_exec_command: polled command not done"); 729 panic("ahci_exec_command: polled command not done");
730#endif 730#endif
731 if (ata_c->flags & AT_DONE) { 731 if (ata_c->flags & AT_DONE) {
732 ret = ATACMD_COMPLETE; 732 ret = ATACMD_COMPLETE;
733 } else { 733 } else {
734 if (ata_c->flags & AT_WAIT) { 734 if (ata_c->flags & AT_WAIT) {
735 while ((ata_c->flags & AT_DONE) == 0) { 735 while ((ata_c->flags & AT_DONE) == 0) {
736 tsleep(ata_c, PRIBIO, "ahcicmd", 0); 736 tsleep(ata_c, PRIBIO, "ahcicmd", 0);
737 } 737 }
738 ret = ATACMD_COMPLETE; 738 ret = ATACMD_COMPLETE;
739 } else { 739 } else {
740 ret = ATACMD_QUEUED; 740 ret = ATACMD_QUEUED;
741 } 741 }
742 } 742 }
743 splx(s); 743 splx(s);
744 return ret; 744 return ret;
745} 745}
746 746
747static void 747static void
748ahci_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer) 748ahci_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
749{ 749{
750 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 750 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
751 struct ahci_channel *achp = (struct ahci_channel *)chp; 751 struct ahci_channel *achp = (struct ahci_channel *)chp;
752 struct ata_command *ata_c = xfer->c_cmd; 752 struct ata_command *ata_c = xfer->c_cmd;
753 int slot = 0 /* XXX slot */; 753 int slot = 0 /* XXX slot */;
754 struct ahci_cmd_tbl *cmd_tbl; 754 struct ahci_cmd_tbl *cmd_tbl;
755 struct ahci_cmd_header *cmd_h; 755 struct ahci_cmd_header *cmd_h;
756 int i; 756 int i;
757 int channel = chp->ch_channel; 757 int channel = chp->ch_channel;
758 758
759 AHCIDEBUG_PRINT(("ahci_cmd_start CI 0x%x\n", 759 AHCIDEBUG_PRINT(("ahci_cmd_start CI 0x%x\n",
760 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS); 760 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
761 761
762 cmd_tbl = achp->ahcic_cmd_tbl[slot]; 762 cmd_tbl = achp->ahcic_cmd_tbl[slot];
763 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel, 763 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
764 cmd_tbl), DEBUG_XFERS); 764 cmd_tbl), DEBUG_XFERS);
765 765
766 satafis_rhd_construct_cmd(ata_c, cmd_tbl->cmdt_cfis); 766 satafis_rhd_construct_cmd(ata_c, cmd_tbl->cmdt_cfis);
767 767
768 cmd_h = &achp->ahcic_cmdh[slot]; 768 cmd_h = &achp->ahcic_cmdh[slot];
769 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc), 769 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
770 chp->ch_channel, cmd_h), DEBUG_XFERS); 770 chp->ch_channel, cmd_h), DEBUG_XFERS);
771 if (ahci_dma_setup(chp, slot, 771 if (ahci_dma_setup(chp, slot,
772 (ata_c->flags & (AT_READ|AT_WRITE)) ? ata_c->data : NULL, 772 (ata_c->flags & (AT_READ|AT_WRITE) && ata_c->bcount > 0) ?
 773 ata_c->data : NULL,
773 ata_c->bcount, 774 ata_c->bcount,
774 (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) { 775 (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
775 ata_c->flags |= AT_DF; 776 ata_c->flags |= AT_DF;
776 ahci_cmd_complete(chp, xfer, slot); 777 ahci_cmd_complete(chp, xfer, slot);
777 return; 778 return;
778 } 779 }
779 cmd_h->cmdh_flags = htole16( 780 cmd_h->cmdh_flags = htole16(
780 ((ata_c->flags & AT_WRITE) ? AHCI_CMDH_F_WR : 0) | 781 ((ata_c->flags & AT_WRITE) ? AHCI_CMDH_F_WR : 0) |
781 RHD_FISLEN / 4); 782 RHD_FISLEN / 4);
782 cmd_h->cmdh_prdbc = 0; 783 cmd_h->cmdh_prdbc = 0;
783 AHCI_CMDH_SYNC(sc, achp, slot, 784 AHCI_CMDH_SYNC(sc, achp, slot,
784 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 785 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
785 786
786 if (ata_c->flags & AT_POLL) { 787 if (ata_c->flags & AT_POLL) {
787 /* polled command, disable interrupts */ 788 /* polled command, disable interrupts */
788 AHCI_WRITE(sc, AHCI_GHC, 789 AHCI_WRITE(sc, AHCI_GHC,
789 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE); 790 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
790 } 791 }
791 chp->ch_flags |= ATACH_IRQ_WAIT; 792 chp->ch_flags |= ATACH_IRQ_WAIT;
792 chp->ch_status = 0; 793 chp->ch_status = 0;
793 /* start command */ 794 /* start command */
794 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot); 795 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
795 /* and says we started this command */ 796 /* and says we started this command */
796 achp->ahcic_cmds_active |= 1 << slot; 797 achp->ahcic_cmds_active |= 1 << slot;
797 798
798 if ((ata_c->flags & AT_POLL) == 0) { 799 if ((ata_c->flags & AT_POLL) == 0) {
799 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 800 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
800 callout_reset(&chp->ch_callout, mstohz(ata_c->timeout), 801 callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
801 ahci_timeout, chp); 802 ahci_timeout, chp);
802 return; 803 return;
803 } 804 }
804 /* 805 /*
805 * Polled command.  806 * Polled command.
806 */ 807 */
807 for (i = 0; i < ata_c->timeout / 10; i++) { 808 for (i = 0; i < ata_c->timeout / 10; i++) {
808 if (ata_c->flags & AT_DONE) 809 if (ata_c->flags & AT_DONE)
809 break; 810 break;
810 ahci_intr_port(sc, achp); 811 ahci_intr_port(sc, achp);
811 if (ata_c->flags & AT_WAIT) 812 if (ata_c->flags & AT_WAIT)
812 tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10)); 813 tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
813 else 814 else
814 delay(10000); 815 delay(10000);
815 } 816 }
816 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,  817 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
817 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS), 818 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
818 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)), 819 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
819 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)), 820 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
820 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))), 821 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
821 DEBUG_XFERS); 822 DEBUG_XFERS);
822 if ((ata_c->flags & AT_DONE) == 0) { 823 if ((ata_c->flags & AT_DONE) == 0) {
823 ata_c->flags |= AT_TIMEOU; 824 ata_c->flags |= AT_TIMEOU;
824 ahci_cmd_complete(chp, xfer, slot); 825 ahci_cmd_complete(chp, xfer, slot);
825 } 826 }
826 /* reenable interrupts */ 827 /* reenable interrupts */
827 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE); 828 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
828} 829}
829 830
830static void 831static void
831ahci_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason) 832ahci_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
832{ 833{
833 struct ata_command *ata_c = xfer->c_cmd; 834 struct ata_command *ata_c = xfer->c_cmd;
834 AHCIDEBUG_PRINT(("ahci_cmd_kill_xfer channel %d\n", chp->ch_channel), 835 AHCIDEBUG_PRINT(("ahci_cmd_kill_xfer channel %d\n", chp->ch_channel),
835 DEBUG_FUNCS); 836 DEBUG_FUNCS);
836 837
837 switch (reason) { 838 switch (reason) {
838 case KILL_GONE: 839 case KILL_GONE:
839 ata_c->flags |= AT_GONE; 840 ata_c->flags |= AT_GONE;
840 break; 841 break;
841 case KILL_RESET: 842 case KILL_RESET:
842 ata_c->flags |= AT_RESET; 843 ata_c->flags |= AT_RESET;
843 break; 844 break;
844 default: 845 default:
845 printf("ahci_cmd_kill_xfer: unknown reason %d\n", reason); 846 printf("ahci_cmd_kill_xfer: unknown reason %d\n", reason);
846 panic("ahci_cmd_kill_xfer"); 847 panic("ahci_cmd_kill_xfer");
847 } 848 }
848 ahci_cmd_done(chp, xfer, 0 /* XXX slot */); 849 ahci_cmd_done(chp, xfer, 0 /* XXX slot */);
849} 850}
850 851
851static int 852static int
852ahci_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is) 853ahci_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
853{ 854{
854 int slot = 0; /* XXX slot */ 855 int slot = 0; /* XXX slot */
855 struct ata_command *ata_c = xfer->c_cmd; 856 struct ata_command *ata_c = xfer->c_cmd;
856 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 857 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
857 struct ahci_channel *achp = (struct ahci_channel *)chp; 858 struct ahci_channel *achp = (struct ahci_channel *)chp;
858 859
859 AHCIDEBUG_PRINT(("ahci_cmd_complete channel %d CMD 0x%x CI 0x%x\n", 860 AHCIDEBUG_PRINT(("ahci_cmd_complete channel %d CMD 0x%x CI 0x%x\n",
860 chp->ch_channel, AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)), 861 chp->ch_channel, AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)),
861 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), 862 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
862 DEBUG_FUNCS); 863 DEBUG_FUNCS);
863 chp->ch_flags &= ~ATACH_IRQ_WAIT; 864 chp->ch_flags &= ~ATACH_IRQ_WAIT;
864 if (xfer->c_flags & C_TIMEOU) { 865 if (xfer->c_flags & C_TIMEOU) {
865 ata_c->flags |= AT_TIMEOU; 866 ata_c->flags |= AT_TIMEOU;
866 } else 867 } else
867 callout_stop(&chp->ch_callout); 868 callout_stop(&chp->ch_callout);
868 869
869 chp->ch_queue->active_xfer = NULL; 870 chp->ch_queue->active_xfer = NULL;
870 871
871 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) { 872 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
872 ahci_cmd_kill_xfer(chp, xfer, KILL_GONE); 873 ahci_cmd_kill_xfer(chp, xfer, KILL_GONE);
873 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN; 874 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
874 wakeup(&chp->ch_queue->active_xfer); 875 wakeup(&chp->ch_queue->active_xfer);
875 return 0; 876 return 0;
876 } 877 }
877 878
878 if (chp->ch_status & WDCS_BSY) { 879 if (chp->ch_status & WDCS_BSY) {
879 ata_c->flags |= AT_TIMEOU; 880 ata_c->flags |= AT_TIMEOU;
880 } else if (chp->ch_status & WDCS_ERR) { 881 } else if (chp->ch_status & WDCS_ERR) {
881 ata_c->r_error = chp->ch_error; 882 ata_c->r_error = chp->ch_error;
882 ata_c->flags |= AT_ERROR; 883 ata_c->flags |= AT_ERROR;
883 } 884 }
884 885
885 if (ata_c->flags & AT_READREG) 886 if (ata_c->flags & AT_READREG)
886 satafis_rdh_cmd_readreg(ata_c, achp->ahcic_rfis->rfis_rfis); 887 satafis_rdh_cmd_readreg(ata_c, achp->ahcic_rfis->rfis_rfis);
887 888
888 ahci_cmd_done(chp, xfer, slot); 889 ahci_cmd_done(chp, xfer, slot);
889 return 0; 890 return 0;
890} 891}
891 892
892static void 893static void
893ahci_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot) 894ahci_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
894{ 895{
895 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 896 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
896 struct ahci_channel *achp = (struct ahci_channel *)chp; 897 struct ahci_channel *achp = (struct ahci_channel *)chp;
897 struct ata_command *ata_c = xfer->c_cmd; 898 struct ata_command *ata_c = xfer->c_cmd;
898 uint16_t *idwordbuf; 899 uint16_t *idwordbuf;
899 int i; 900 int i;
900 901
901 AHCIDEBUG_PRINT(("ahci_cmd_done channel %d\n", chp->ch_channel), 902 AHCIDEBUG_PRINT(("ahci_cmd_done channel %d\n", chp->ch_channel),
902 DEBUG_FUNCS); 903 DEBUG_FUNCS);
903 904
904 /* this comamnd is not active any more */ 905 /* this comamnd is not active any more */
905 achp->ahcic_cmds_active &= ~(1 << slot); 906 achp->ahcic_cmds_active &= ~(1 << slot);
906 907
907 if (ata_c->flags & (AT_READ|AT_WRITE)) { 908 if (ata_c->flags & (AT_READ|AT_WRITE) && ata_c->bcount > 0) {
908 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0, 909 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
909 achp->ahcic_datad[slot]->dm_mapsize, 910 achp->ahcic_datad[slot]->dm_mapsize,
910 (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD : 911 (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
911 BUS_DMASYNC_POSTWRITE); 912 BUS_DMASYNC_POSTWRITE);
912 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]); 913 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
913 } 914 }
914 915
915 AHCI_CMDH_SYNC(sc, achp, slot, 916 AHCI_CMDH_SYNC(sc, achp, slot,
916 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 917 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
917 918
918 /* ata(4) expects IDENTIFY data to be in host endianess */ 919 /* ata(4) expects IDENTIFY data to be in host endianess */
919 if (ata_c->r_command == WDCC_IDENTIFY || 920 if (ata_c->r_command == WDCC_IDENTIFY ||
920 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) { 921 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
921 idwordbuf = xfer->c_databuf; 922 idwordbuf = xfer->c_databuf;
922 for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) { 923 for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
923 idwordbuf[i] = le16toh(idwordbuf[i]); 924 idwordbuf[i] = le16toh(idwordbuf[i]);
924 } 925 }
925 } 926 }
926 927
927 ata_c->flags |= AT_DONE; 928 ata_c->flags |= AT_DONE;
928 if (achp->ahcic_cmdh[slot].cmdh_prdbc) 929 if (achp->ahcic_cmdh[slot].cmdh_prdbc)
929 ata_c->flags |= AT_XFDONE; 930 ata_c->flags |= AT_XFDONE;
930 931
931 ata_free_xfer(chp, xfer); 932 ata_free_xfer(chp, xfer);
932 if (ata_c->flags & AT_WAIT) 933 if (ata_c->flags & AT_WAIT)
933 wakeup(ata_c); 934 wakeup(ata_c);
934 else if (ata_c->callback) 935 else if (ata_c->callback)
935 ata_c->callback(ata_c->callback_arg); 936 ata_c->callback(ata_c->callback_arg);
936 atastart(chp); 937 atastart(chp);
937 return; 938 return;
938} 939}
939 940
940static int 941static int
941ahci_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio) 942ahci_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
942{ 943{
943 struct ata_channel *chp = drvp->chnl_softc; 944 struct ata_channel *chp = drvp->chnl_softc;
944 struct ata_xfer *xfer; 945 struct ata_xfer *xfer;
945 946
946 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 947 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
947 AHCIDEBUG_PRINT(("ahci_ata_bio port %d CI 0x%x\n", 948 AHCIDEBUG_PRINT(("ahci_ata_bio port %d CI 0x%x\n",
948 chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), 949 chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
949 DEBUG_XFERS); 950 DEBUG_XFERS);
950 xfer = ata_get_xfer(ATAXF_NOSLEEP); 951 xfer = ata_get_xfer(ATAXF_NOSLEEP);
951 if (xfer == NULL) { 952 if (xfer == NULL) {
952 return ATACMD_TRY_AGAIN; 953 return ATACMD_TRY_AGAIN;
953 } 954 }
954 if (ata_bio->flags & ATA_POLL) 955 if (ata_bio->flags & ATA_POLL)
955 xfer->c_flags |= C_POLL; 956 xfer->c_flags |= C_POLL;
956 xfer->c_drive = drvp->drive; 957 xfer->c_drive = drvp->drive;
957 xfer->c_cmd = ata_bio; 958 xfer->c_cmd = ata_bio;
958 xfer->c_databuf = ata_bio->databuf; 959 xfer->c_databuf = ata_bio->databuf;
959 xfer->c_bcount = ata_bio->bcount; 960 xfer->c_bcount = ata_bio->bcount;
960 xfer->c_start = ahci_bio_start; 961 xfer->c_start = ahci_bio_start;
961 xfer->c_intr = ahci_bio_complete; 962 xfer->c_intr = ahci_bio_complete;
962 xfer->c_kill_xfer = ahci_bio_kill_xfer; 963 xfer->c_kill_xfer = ahci_bio_kill_xfer;
963 ata_exec_xfer(chp, xfer); 964 ata_exec_xfer(chp, xfer);
964 return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED; 965 return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED;
965} 966}
966 967
967static void 968static void
968ahci_bio_start(struct ata_channel *chp, struct ata_xfer *xfer) 969ahci_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
969{ 970{
970 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 971 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
971 struct ahci_channel *achp = (struct ahci_channel *)chp; 972 struct ahci_channel *achp = (struct ahci_channel *)chp;
972 struct ata_bio *ata_bio = xfer->c_cmd; 973 struct ata_bio *ata_bio = xfer->c_cmd;
973 int slot = 0 /* XXX slot */; 974 int slot = 0 /* XXX slot */;
974 struct ahci_cmd_tbl *cmd_tbl; 975 struct ahci_cmd_tbl *cmd_tbl;
975 struct ahci_cmd_header *cmd_h; 976 struct ahci_cmd_header *cmd_h;
976 int i; 977 int i;
977 int channel = chp->ch_channel; 978 int channel = chp->ch_channel;
978 979
979 AHCIDEBUG_PRINT(("ahci_bio_start CI 0x%x\n", 980 AHCIDEBUG_PRINT(("ahci_bio_start CI 0x%x\n",
980 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS); 981 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
981 982
982 cmd_tbl = achp->ahcic_cmd_tbl[slot]; 983 cmd_tbl = achp->ahcic_cmd_tbl[slot];
983 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel, 984 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
984 cmd_tbl), DEBUG_XFERS); 985 cmd_tbl), DEBUG_XFERS);
985 986
986 satafis_rhd_construct_bio(xfer, cmd_tbl->cmdt_cfis); 987 satafis_rhd_construct_bio(xfer, cmd_tbl->cmdt_cfis);
987 988
988 cmd_h = &achp->ahcic_cmdh[slot]; 989 cmd_h = &achp->ahcic_cmdh[slot];
989 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc), 990 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
990 chp->ch_channel, cmd_h), DEBUG_XFERS); 991 chp->ch_channel, cmd_h), DEBUG_XFERS);
991 if (ahci_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount, 992 if (ahci_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
992 (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) { 993 (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
993 ata_bio->error = ERR_DMA; 994 ata_bio->error = ERR_DMA;
994 ata_bio->r_error = 0; 995 ata_bio->r_error = 0;
995 ahci_bio_complete(chp, xfer, slot); 996 ahci_bio_complete(chp, xfer, slot);
996 return; 997 return;
997 } 998 }
998 cmd_h->cmdh_flags = htole16( 999 cmd_h->cmdh_flags = htole16(
999 ((ata_bio->flags & ATA_READ) ? 0 : AHCI_CMDH_F_WR) | 1000 ((ata_bio->flags & ATA_READ) ? 0 : AHCI_CMDH_F_WR) |
1000 RHD_FISLEN / 4); 1001 RHD_FISLEN / 4);
1001 cmd_h->cmdh_prdbc = 0; 1002 cmd_h->cmdh_prdbc = 0;
1002 AHCI_CMDH_SYNC(sc, achp, slot, 1003 AHCI_CMDH_SYNC(sc, achp, slot,
1003 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1004 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1004 1005
1005 if (xfer->c_flags & C_POLL) { 1006 if (xfer->c_flags & C_POLL) {
1006 /* polled command, disable interrupts */ 1007 /* polled command, disable interrupts */
1007 AHCI_WRITE(sc, AHCI_GHC, 1008 AHCI_WRITE(sc, AHCI_GHC,
1008 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE); 1009 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
1009 } 1010 }
1010 chp->ch_flags |= ATACH_IRQ_WAIT; 1011 chp->ch_flags |= ATACH_IRQ_WAIT;
1011 chp->ch_status = 0; 1012 chp->ch_status = 0;
1012 /* start command */ 1013 /* start command */
1013 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot); 1014 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
1014 /* and says we started this command */ 1015 /* and says we started this command */
1015 achp->ahcic_cmds_active |= 1 << slot; 1016 achp->ahcic_cmds_active |= 1 << slot;
1016 1017
1017 if ((xfer->c_flags & C_POLL) == 0) { 1018 if ((xfer->c_flags & C_POLL) == 0) {
1018 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1019 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1019 callout_reset(&chp->ch_callout, mstohz(ATA_DELAY), 1020 callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
1020 ahci_timeout, chp); 1021 ahci_timeout, chp);
1021 return; 1022 return;
1022 } 1023 }
1023 /* 1024 /*
1024 * Polled command.  1025 * Polled command.
1025 */ 1026 */
1026 for (i = 0; i < ATA_DELAY / 10; i++) { 1027 for (i = 0; i < ATA_DELAY / 10; i++) {
1027 if (ata_bio->flags & ATA_ITSDONE) 1028 if (ata_bio->flags & ATA_ITSDONE)
1028 break; 1029 break;
1029 ahci_intr_port(sc, achp); 1030 ahci_intr_port(sc, achp);
1030 if (ata_bio->flags & ATA_NOSLEEP) 1031 if (ata_bio->flags & ATA_NOSLEEP)
1031 delay(10000); 1032 delay(10000);
1032 else 1033 else
1033 tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10)); 1034 tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
1034 } 1035 }
1035 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,  1036 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
1036 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS), 1037 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
1037 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)), 1038 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
1038 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)), 1039 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
1039 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))), 1040 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
1040 DEBUG_XFERS); 1041 DEBUG_XFERS);
1041 if ((ata_bio->flags & ATA_ITSDONE) == 0) { 1042 if ((ata_bio->flags & ATA_ITSDONE) == 0) {
1042 ata_bio->error = TIMEOUT; 1043 ata_bio->error = TIMEOUT;
1043 ahci_bio_complete(chp, xfer, slot); 1044 ahci_bio_complete(chp, xfer, slot);
1044 } 1045 }
1045 /* reenable interrupts */ 1046 /* reenable interrupts */
1046 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE); 1047 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
1047} 1048}
1048 1049
1049static void 1050static void
1050ahci_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason) 1051ahci_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
1051{ 1052{
1052 int slot = 0; /* XXX slot */ 1053 int slot = 0; /* XXX slot */
1053 int drive = xfer->c_drive; 1054 int drive = xfer->c_drive;
1054 struct ata_bio *ata_bio = xfer->c_cmd; 1055 struct ata_bio *ata_bio = xfer->c_cmd;
1055 struct ahci_channel *achp = (struct ahci_channel *)chp; 1056 struct ahci_channel *achp = (struct ahci_channel *)chp;
1056 AHCIDEBUG_PRINT(("ahci_bio_kill_xfer channel %d\n", chp->ch_channel), 1057 AHCIDEBUG_PRINT(("ahci_bio_kill_xfer channel %d\n", chp->ch_channel),
1057 DEBUG_FUNCS); 1058 DEBUG_FUNCS);
1058 1059
1059 achp->ahcic_cmds_active &= ~(1 << slot); 1060 achp->ahcic_cmds_active &= ~(1 << slot);
1060 ata_free_xfer(chp, xfer); 1061 ata_free_xfer(chp, xfer);
1061 ata_bio->flags |= ATA_ITSDONE; 1062 ata_bio->flags |= ATA_ITSDONE;
1062 switch (reason) { 1063 switch (reason) {
1063 case KILL_GONE: 1064 case KILL_GONE:
1064 ata_bio->error = ERR_NODEV; 1065 ata_bio->error = ERR_NODEV;
1065 break; 1066 break;
1066 case KILL_RESET: 1067 case KILL_RESET:
1067 ata_bio->error = ERR_RESET; 1068 ata_bio->error = ERR_RESET;
1068 break; 1069 break;
1069 default: 1070 default:
1070 printf("ahci_bio_kill_xfer: unknown reason %d\n", reason); 1071 printf("ahci_bio_kill_xfer: unknown reason %d\n", reason);
1071 panic("ahci_bio_kill_xfer"); 1072 panic("ahci_bio_kill_xfer");
1072 } 1073 }
1073 ata_bio->r_error = WDCE_ABRT; 1074 ata_bio->r_error = WDCE_ABRT;
1074 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc); 1075 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1075} 1076}
1076 1077
1077static int 1078static int
1078ahci_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is) 1079ahci_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
1079{ 1080{
1080 int slot = 0; /* XXX slot */ 1081 int slot = 0; /* XXX slot */
1081 struct ata_bio *ata_bio = xfer->c_cmd; 1082 struct ata_bio *ata_bio = xfer->c_cmd;
1082 int drive = xfer->c_drive; 1083 int drive = xfer->c_drive;
1083 struct ahci_channel *achp = (struct ahci_channel *)chp; 1084 struct ahci_channel *achp = (struct ahci_channel *)chp;
1084 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 1085 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1085 1086
1086 AHCIDEBUG_PRINT(("ahci_bio_complete channel %d\n", chp->ch_channel), 1087 AHCIDEBUG_PRINT(("ahci_bio_complete channel %d\n", chp->ch_channel),
1087 DEBUG_FUNCS); 1088 DEBUG_FUNCS);
1088 1089
1089 achp->ahcic_cmds_active &= ~(1 << slot); 1090 achp->ahcic_cmds_active &= ~(1 << slot);
1090 chp->ch_flags &= ~ATACH_IRQ_WAIT; 1091 chp->ch_flags &= ~ATACH_IRQ_WAIT;
1091 if (xfer->c_flags & C_TIMEOU) { 1092 if (xfer->c_flags & C_TIMEOU) {
1092 ata_bio->error = TIMEOUT; 1093 ata_bio->error = TIMEOUT;
1093 } else { 1094 } else {
1094 callout_stop(&chp->ch_callout); 1095 callout_stop(&chp->ch_callout);
1095 ata_bio->error = NOERROR; 1096 ata_bio->error = NOERROR;
1096 } 1097 }
1097 1098
1098 chp->ch_queue->active_xfer = NULL; 1099 chp->ch_queue->active_xfer = NULL;
1099 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0, 1100 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1100 achp->ahcic_datad[slot]->dm_mapsize, 1101 achp->ahcic_datad[slot]->dm_mapsize,
1101 (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD : 1102 (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1102 BUS_DMASYNC_POSTWRITE); 1103 BUS_DMASYNC_POSTWRITE);
1103 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]); 1104 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
1104 1105
1105 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) { 1106 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1106 ahci_bio_kill_xfer(chp, xfer, KILL_GONE); 1107 ahci_bio_kill_xfer(chp, xfer, KILL_GONE);
1107 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN; 1108 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1108 wakeup(&chp->ch_queue->active_xfer); 1109 wakeup(&chp->ch_queue->active_xfer);
1109 return 0; 1110 return 0;
1110 } 1111 }
1111 ata_free_xfer(chp, xfer); 1112 ata_free_xfer(chp, xfer);
1112 ata_bio->flags |= ATA_ITSDONE; 1113 ata_bio->flags |= ATA_ITSDONE;
1113 if (chp->ch_status & WDCS_DWF) {  1114 if (chp->ch_status & WDCS_DWF) {
1114 ata_bio->error = ERR_DF; 1115 ata_bio->error = ERR_DF;
1115 } else if (chp->ch_status & WDCS_ERR) { 1116 } else if (chp->ch_status & WDCS_ERR) {
1116 ata_bio->error = ERROR; 1117 ata_bio->error = ERROR;
1117 ata_bio->r_error = chp->ch_error; 1118 ata_bio->r_error = chp->ch_error;
1118 } else if (chp->ch_status & WDCS_CORR) 1119 } else if (chp->ch_status & WDCS_CORR)
1119 ata_bio->flags |= ATA_CORR; 1120 ata_bio->flags |= ATA_CORR;
1120 1121
1121 AHCI_CMDH_SYNC(sc, achp, slot, 1122 AHCI_CMDH_SYNC(sc, achp, slot,
1122 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1123 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1123 AHCIDEBUG_PRINT(("ahci_bio_complete bcount %ld", 1124 AHCIDEBUG_PRINT(("ahci_bio_complete bcount %ld",
1124 ata_bio->bcount), DEBUG_XFERS); 1125 ata_bio->bcount), DEBUG_XFERS);
1125 /*  1126 /*
1126 * if it was a write, complete data buffer may have been transfered 1127 * if it was a write, complete data buffer may have been transfered
1127 * before error detection; in this case don't use cmdh_prdbc 1128 * before error detection; in this case don't use cmdh_prdbc
1128 * as it won't reflect what was written to media. Assume nothing 1129 * as it won't reflect what was written to media. Assume nothing
1129 * was transfered and leave bcount as-is. 1130 * was transfered and leave bcount as-is.
1130 */ 1131 */
1131 if ((ata_bio->flags & ATA_READ) || ata_bio->error == NOERROR) 1132 if ((ata_bio->flags & ATA_READ) || ata_bio->error == NOERROR)
1132 ata_bio->bcount -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc); 1133 ata_bio->bcount -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc);
1133 AHCIDEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS); 1134 AHCIDEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1134 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc); 1135 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1135 atastart(chp); 1136 atastart(chp);
1136 return 0; 1137 return 0;
1137} 1138}
1138 1139
1139static void 1140static void
1140ahci_channel_stop(struct ahci_softc *sc, struct ata_channel *chp, int flags) 1141ahci_channel_stop(struct ahci_softc *sc, struct ata_channel *chp, int flags)
1141{ 1142{
1142 int i; 1143 int i;
1143 /* stop channel */ 1144 /* stop channel */
1144 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel), 1145 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1145 AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & ~AHCI_P_CMD_ST); 1146 AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & ~AHCI_P_CMD_ST);
1146 /* wait 1s for channel to stop */ 1147 /* wait 1s for channel to stop */
1147 for (i = 0; i <100; i++) { 1148 for (i = 0; i <100; i++) {
1148 if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) 1149 if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
1149 == 0) 1150 == 0)
1150 break; 1151 break;
1151 if (flags & AT_WAIT) 1152 if (flags & AT_WAIT)
1152 tsleep(&sc, PRIBIO, "ahcirst", mstohz(10)); 1153 tsleep(&sc, PRIBIO, "ahcirst", mstohz(10));
1153 else 1154 else
1154 delay(10000); 1155 delay(10000);
1155 } 1156 }
1156 if (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) { 1157 if (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) {
1157 printf("%s: channel wouldn't stop\n", AHCINAME(sc)); 1158 printf("%s: channel wouldn't stop\n", AHCINAME(sc));
1158 /* XXX controller reset ? */ 1159 /* XXX controller reset ? */
1159 return; 1160 return;
1160 } 1161 }
1161} 1162}
1162 1163
1163static void 1164static void
1164ahci_channel_start(struct ahci_softc *sc, struct ata_channel *chp) 1165ahci_channel_start(struct ahci_softc *sc, struct ata_channel *chp)
1165{ 1166{
1166 /* clear error */ 1167 /* clear error */
1167 AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel), 1168 AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel),
1168 AHCI_READ(sc, AHCI_P_SERR(chp->ch_channel))); 1169 AHCI_READ(sc, AHCI_P_SERR(chp->ch_channel)));
1169 1170
1170 /* and start controller */ 1171 /* and start controller */
1171 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel), 1172 AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1172 AHCI_P_CMD_ICC_AC | AHCI_P_CMD_POD | AHCI_P_CMD_SUD | 1173 AHCI_P_CMD_ICC_AC | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
1173 AHCI_P_CMD_FRE | AHCI_P_CMD_ST); 1174 AHCI_P_CMD_FRE | AHCI_P_CMD_ST);
1174} 1175}
1175 1176
1176static void 1177static void
1177ahci_timeout(void *v) 1178ahci_timeout(void *v)
1178{ 1179{
1179 struct ata_channel *chp = (struct ata_channel *)v; 1180 struct ata_channel *chp = (struct ata_channel *)v;
1180 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1181 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1181 int s = splbio(); 1182 int s = splbio();
1182 AHCIDEBUG_PRINT(("ahci_timeout xfer %p\n", xfer), DEBUG_INTR); 1183 AHCIDEBUG_PRINT(("ahci_timeout xfer %p\n", xfer), DEBUG_INTR);
1183 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) { 1184 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1184 xfer->c_flags |= C_TIMEOU; 1185 xfer->c_flags |= C_TIMEOU;
1185 xfer->c_intr(chp, xfer, 0); 1186 xfer->c_intr(chp, xfer, 0);
1186 } 1187 }
1187 splx(s); 1188 splx(s);
1188} 1189}
1189 1190
1190static int 1191static int
1191ahci_dma_setup(struct ata_channel *chp, int slot, void *data, 1192ahci_dma_setup(struct ata_channel *chp, int slot, void *data,
1192 size_t count, int op) 1193 size_t count, int op)
1193{ 1194{
1194 int error, seg; 1195 int error, seg;
1195 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 1196 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1196 struct ahci_channel *achp = (struct ahci_channel *)chp; 1197 struct ahci_channel *achp = (struct ahci_channel *)chp;
1197 struct ahci_cmd_tbl *cmd_tbl; 1198 struct ahci_cmd_tbl *cmd_tbl;
1198 struct ahci_cmd_header *cmd_h; 1199 struct ahci_cmd_header *cmd_h;
1199 1200
1200 cmd_h = &achp->ahcic_cmdh[slot]; 1201 cmd_h = &achp->ahcic_cmdh[slot];
1201 cmd_tbl = achp->ahcic_cmd_tbl[slot]; 1202 cmd_tbl = achp->ahcic_cmd_tbl[slot];
1202 1203
1203 if (data == NULL) { 1204 if (data == NULL) {
1204 cmd_h->cmdh_prdtl = 0; 1205 cmd_h->cmdh_prdtl = 0;
1205 goto end; 1206 goto end;
1206 } 1207 }
1207 1208
1208 error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_datad[slot], 1209 error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_datad[slot],
1209 data, count, NULL, 1210 data, count, NULL,
1210 BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op); 1211 BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1211 if (error) { 1212 if (error) {
1212 printf("%s port %d: failed to load xfer: %d\n", 1213 printf("%s port %d: failed to load xfer: %d\n",
1213 AHCINAME(sc), chp->ch_channel, error); 1214 AHCINAME(sc), chp->ch_channel, error);
1214 return error; 1215 return error;
1215 } 1216 }
1216 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0, 1217 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1217 achp->ahcic_datad[slot]->dm_mapsize,  1218 achp->ahcic_datad[slot]->dm_mapsize,
1218 (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1219 (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1219 for (seg = 0; seg < achp->ahcic_datad[slot]->dm_nsegs; seg++) { 1220 for (seg = 0; seg < achp->ahcic_datad[slot]->dm_nsegs; seg++) {
1220 cmd_tbl->cmdt_prd[seg].prd_dba = htole64( 1221 cmd_tbl->cmdt_prd[seg].prd_dba = htole64(
1221 achp->ahcic_datad[slot]->dm_segs[seg].ds_addr); 1222 achp->ahcic_datad[slot]->dm_segs[seg].ds_addr);
1222 cmd_tbl->cmdt_prd[seg].prd_dbc = htole32( 1223 cmd_tbl->cmdt_prd[seg].prd_dbc = htole32(
1223 achp->ahcic_datad[slot]->dm_segs[seg].ds_len - 1); 1224 achp->ahcic_datad[slot]->dm_segs[seg].ds_len - 1);
1224 } 1225 }
1225 cmd_tbl->cmdt_prd[seg - 1].prd_dbc |= htole32(AHCI_PRD_DBC_IPC); 1226 cmd_tbl->cmdt_prd[seg - 1].prd_dbc |= htole32(AHCI_PRD_DBC_IPC);
1226 cmd_h->cmdh_prdtl = htole16(achp->ahcic_datad[slot]->dm_nsegs); 1227 cmd_h->cmdh_prdtl = htole16(achp->ahcic_datad[slot]->dm_nsegs);
1227end: 1228end:
1228 AHCI_CMDTBL_SYNC(sc, achp, slot, BUS_DMASYNC_PREWRITE); 1229 AHCI_CMDTBL_SYNC(sc, achp, slot, BUS_DMASYNC_PREWRITE);
1229 return 0; 1230 return 0;
1230} 1231}
1231 1232
1232#if NATAPIBUS > 0 1233#if NATAPIBUS > 0
1233static void 1234static void
1234ahci_atapibus_attach(struct atabus_softc * ata_sc) 1235ahci_atapibus_attach(struct atabus_softc * ata_sc)
1235{ 1236{
1236 struct ata_channel *chp = ata_sc->sc_chan; 1237 struct ata_channel *chp = ata_sc->sc_chan;
1237 struct atac_softc *atac = chp->ch_atac; 1238 struct atac_softc *atac = chp->ch_atac;
1238 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 1239 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1239 struct scsipi_channel *chan = &chp->ch_atapi_channel; 1240 struct scsipi_channel *chan = &chp->ch_atapi_channel;
1240 /* 1241 /*
1241 * Fill in the scsipi_adapter. 1242 * Fill in the scsipi_adapter.
1242 */ 1243 */
1243 adapt->adapt_dev = atac->atac_dev; 1244 adapt->adapt_dev = atac->atac_dev;
1244 adapt->adapt_nchannels = atac->atac_nchannels; 1245 adapt->adapt_nchannels = atac->atac_nchannels;
1245 adapt->adapt_request = ahci_atapi_scsipi_request; 1246 adapt->adapt_request = ahci_atapi_scsipi_request;
1246 adapt->adapt_minphys = ahci_atapi_minphys; 1247 adapt->adapt_minphys = ahci_atapi_minphys;
1247 atac->atac_atapi_adapter.atapi_probe_device = ahci_atapi_probe_device; 1248 atac->atac_atapi_adapter.atapi_probe_device = ahci_atapi_probe_device;
1248 1249
1249 /* 1250 /*
1250 * Fill in the scsipi_channel. 1251 * Fill in the scsipi_channel.
1251 */ 1252 */
1252 memset(chan, 0, sizeof(*chan)); 1253 memset(chan, 0, sizeof(*chan));
1253 chan->chan_adapter = adapt; 1254 chan->chan_adapter = adapt;
1254 chan->chan_bustype = &ahci_atapi_bustype; 1255 chan->chan_bustype = &ahci_atapi_bustype;
1255 chan->chan_channel = chp->ch_channel; 1256 chan->chan_channel = chp->ch_channel;
1256 chan->chan_flags = SCSIPI_CHAN_OPENINGS; 1257 chan->chan_flags = SCSIPI_CHAN_OPENINGS;
1257 chan->chan_openings = 1; 1258 chan->chan_openings = 1;
1258 chan->chan_max_periph = 1; 1259 chan->chan_max_periph = 1;
1259 chan->chan_ntargets = 1; 1260 chan->chan_ntargets = 1;
1260 chan->chan_nluns = 1; 1261 chan->chan_nluns = 1;
1261 chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan, 1262 chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
1262 atapiprint); 1263 atapiprint);
1263} 1264}
1264 1265
1265static void 1266static void
1266ahci_atapi_minphys(struct buf *bp) 1267ahci_atapi_minphys(struct buf *bp)
1267{ 1268{
1268 if (bp->b_bcount > MAXPHYS) 1269 if (bp->b_bcount > MAXPHYS)
1269 bp->b_bcount = MAXPHYS; 1270 bp->b_bcount = MAXPHYS;
1270 minphys(bp); 1271 minphys(bp);
1271} 1272}
1272 1273
1273/* 1274/*
1274 * Kill off all pending xfers for a periph. 1275 * Kill off all pending xfers for a periph.
1275 * 1276 *
1276 * Must be called at splbio(). 1277 * Must be called at splbio().
1277 */ 1278 */
1278static void 1279static void
1279ahci_atapi_kill_pending(struct scsipi_periph *periph) 1280ahci_atapi_kill_pending(struct scsipi_periph *periph)
1280{ 1281{
1281 struct atac_softc *atac = 1282 struct atac_softc *atac =
1282 device_private(periph->periph_channel->chan_adapter->adapt_dev); 1283 device_private(periph->periph_channel->chan_adapter->adapt_dev);
1283 struct ata_channel *chp = 1284 struct ata_channel *chp =
1284 atac->atac_channels[periph->periph_channel->chan_channel]; 1285 atac->atac_channels[periph->periph_channel->chan_channel];
1285 1286
1286 ata_kill_pending(&chp->ch_drive[periph->periph_target]); 1287 ata_kill_pending(&chp->ch_drive[periph->periph_target]);
1287} 1288}
1288 1289
1289static void 1290static void
1290ahci_atapi_scsipi_request(struct scsipi_channel *chan, 1291ahci_atapi_scsipi_request(struct scsipi_channel *chan,
1291 scsipi_adapter_req_t req, void *arg) 1292 scsipi_adapter_req_t req, void *arg)
1292{ 1293{
1293 struct scsipi_adapter *adapt = chan->chan_adapter; 1294 struct scsipi_adapter *adapt = chan->chan_adapter;
1294 struct scsipi_periph *periph; 1295 struct scsipi_periph *periph;
1295 struct scsipi_xfer *sc_xfer; 1296 struct scsipi_xfer *sc_xfer;
1296 struct ahci_softc *sc = device_private(adapt->adapt_dev); 1297 struct ahci_softc *sc = device_private(adapt->adapt_dev);
1297 struct atac_softc *atac = &sc->sc_atac; 1298 struct atac_softc *atac = &sc->sc_atac;
1298 struct ata_xfer *xfer; 1299 struct ata_xfer *xfer;
1299 int channel = chan->chan_channel; 1300 int channel = chan->chan_channel;
1300 int drive, s; 1301 int drive, s;
1301 1302
1302 switch (req) { 1303 switch (req) {
1303 case ADAPTER_REQ_RUN_XFER: 1304 case ADAPTER_REQ_RUN_XFER:
1304 sc_xfer = arg; 1305 sc_xfer = arg;
1305 periph = sc_xfer->xs_periph; 1306 periph = sc_xfer->xs_periph;
1306 drive = periph->periph_target; 1307 drive = periph->periph_target;
1307 if (!device_is_active(atac->atac_dev)) { 1308 if (!device_is_active(atac->atac_dev)) {
1308 sc_xfer->error = XS_DRIVER_STUFFUP; 1309 sc_xfer->error = XS_DRIVER_STUFFUP;
1309 scsipi_done(sc_xfer); 1310 scsipi_done(sc_xfer);
1310 return; 1311 return;
1311 } 1312 }
1312 xfer = ata_get_xfer(ATAXF_NOSLEEP); 1313 xfer = ata_get_xfer(ATAXF_NOSLEEP);
1313 if (xfer == NULL) { 1314 if (xfer == NULL) {
1314 sc_xfer->error = XS_RESOURCE_SHORTAGE; 1315 sc_xfer->error = XS_RESOURCE_SHORTAGE;
1315 scsipi_done(sc_xfer); 1316 scsipi_done(sc_xfer);
1316 return; 1317 return;
1317 } 1318 }
1318 1319
1319 if (sc_xfer->xs_control & XS_CTL_POLL) 1320 if (sc_xfer->xs_control & XS_CTL_POLL)
1320 xfer->c_flags |= C_POLL; 1321 xfer->c_flags |= C_POLL;
1321 xfer->c_drive = drive; 1322 xfer->c_drive = drive;
1322 xfer->c_flags |= C_ATAPI; 1323 xfer->c_flags |= C_ATAPI;
1323 xfer->c_cmd = sc_xfer; 1324 xfer->c_cmd = sc_xfer;
1324 xfer->c_databuf = sc_xfer->data; 1325 xfer->c_databuf = sc_xfer->data;
1325 xfer->c_bcount = sc_xfer->datalen; 1326 xfer->c_bcount = sc_xfer->datalen;
1326 xfer->c_start = ahci_atapi_start; 1327 xfer->c_start = ahci_atapi_start;
1327 xfer->c_intr = ahci_atapi_complete; 1328 xfer->c_intr = ahci_atapi_complete;
1328 xfer->c_kill_xfer = ahci_atapi_kill_xfer; 1329 xfer->c_kill_xfer = ahci_atapi_kill_xfer;
1329 xfer->c_dscpoll = 0; 1330 xfer->c_dscpoll = 0;
1330 s = splbio(); 1331 s = splbio();
1331 ata_exec_xfer(atac->atac_channels[channel], xfer); 1332 ata_exec_xfer(atac->atac_channels[channel], xfer);
1332#ifdef DIAGNOSTIC 1333#ifdef DIAGNOSTIC
1333 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 && 1334 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
1334 (sc_xfer->xs_status & XS_STS_DONE) == 0) 1335 (sc_xfer->xs_status & XS_STS_DONE) == 0)
1335 panic("ahci_atapi_scsipi_request: polled command " 1336 panic("ahci_atapi_scsipi_request: polled command "
1336 "not done"); 1337 "not done");
1337#endif 1338#endif
1338 splx(s); 1339 splx(s);
1339 return; 1340 return;
1340 default: 1341 default:
1341 /* Not supported, nothing to do. */ 1342 /* Not supported, nothing to do. */
1342 ; 1343 ;
1343 } 1344 }
1344} 1345}
1345 1346
1346static void 1347static void
1347ahci_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer) 1348ahci_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
1348{ 1349{
1349 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 1350 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1350 struct ahci_channel *achp = (struct ahci_channel *)chp; 1351 struct ahci_channel *achp = (struct ahci_channel *)chp;
1351 struct scsipi_xfer *sc_xfer = xfer->c_cmd; 1352 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1352 int slot = 0 /* XXX slot */; 1353 int slot = 0 /* XXX slot */;
1353 struct ahci_cmd_tbl *cmd_tbl; 1354 struct ahci_cmd_tbl *cmd_tbl;
1354 struct ahci_cmd_header *cmd_h; 1355 struct ahci_cmd_header *cmd_h;
1355 int i; 1356 int i;
1356 int channel = chp->ch_channel; 1357 int channel = chp->ch_channel;
1357 1358
1358 AHCIDEBUG_PRINT(("ahci_atapi_start CI 0x%x\n", 1359 AHCIDEBUG_PRINT(("ahci_atapi_start CI 0x%x\n",
1359 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS); 1360 AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
1360 1361
1361 cmd_tbl = achp->ahcic_cmd_tbl[slot]; 1362 cmd_tbl = achp->ahcic_cmd_tbl[slot];
1362 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel, 1363 AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
1363 cmd_tbl), DEBUG_XFERS); 1364 cmd_tbl), DEBUG_XFERS);
1364 1365
1365 satafis_rhd_construct_atapi(xfer, cmd_tbl->cmdt_cfis); 1366 satafis_rhd_construct_atapi(xfer, cmd_tbl->cmdt_cfis);
1366 memset(&cmd_tbl->cmdt_acmd, 0, sizeof(cmd_tbl->cmdt_acmd)); 1367 memset(&cmd_tbl->cmdt_acmd, 0, sizeof(cmd_tbl->cmdt_acmd));
1367 memcpy(cmd_tbl->cmdt_acmd, sc_xfer->cmd, sc_xfer->cmdlen); 1368 memcpy(cmd_tbl->cmdt_acmd, sc_xfer->cmd, sc_xfer->cmdlen);
1368 1369
1369 cmd_h = &achp->ahcic_cmdh[slot]; 1370 cmd_h = &achp->ahcic_cmdh[slot];
1370 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc), 1371 AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
1371 chp->ch_channel, cmd_h), DEBUG_XFERS); 1372 chp->ch_channel, cmd_h), DEBUG_XFERS);
1372 if (ahci_dma_setup(chp, slot, sc_xfer->datalen ? sc_xfer->data : NULL, 1373 if (ahci_dma_setup(chp, slot, sc_xfer->datalen ? sc_xfer->data : NULL,
1373 sc_xfer->datalen, 1374 sc_xfer->datalen,
1374 (sc_xfer->xs_control & XS_CTL_DATA_IN) ? 1375 (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1375 BUS_DMA_READ : BUS_DMA_WRITE)) { 1376 BUS_DMA_READ : BUS_DMA_WRITE)) {
1376 sc_xfer->error = XS_DRIVER_STUFFUP; 1377 sc_xfer->error = XS_DRIVER_STUFFUP;
1377 ahci_atapi_complete(chp, xfer, slot); 1378 ahci_atapi_complete(chp, xfer, slot);
1378 return; 1379 return;
1379 } 1380 }
1380 cmd_h->cmdh_flags = htole16( 1381 cmd_h->cmdh_flags = htole16(
1381 ((sc_xfer->xs_control & XS_CTL_DATA_OUT) ? AHCI_CMDH_F_WR : 0) | 1382 ((sc_xfer->xs_control & XS_CTL_DATA_OUT) ? AHCI_CMDH_F_WR : 0) |
1382 RHD_FISLEN / 4 | AHCI_CMDH_F_A); 1383 RHD_FISLEN / 4 | AHCI_CMDH_F_A);
1383 cmd_h->cmdh_prdbc = 0; 1384 cmd_h->cmdh_prdbc = 0;
1384 AHCI_CMDH_SYNC(sc, achp, slot, 1385 AHCI_CMDH_SYNC(sc, achp, slot,
1385 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1386 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1386 1387
1387 if (xfer->c_flags & C_POLL) { 1388 if (xfer->c_flags & C_POLL) {
1388 /* polled command, disable interrupts */ 1389 /* polled command, disable interrupts */
1389 AHCI_WRITE(sc, AHCI_GHC, 1390 AHCI_WRITE(sc, AHCI_GHC,
1390 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE); 1391 AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
1391 } 1392 }
1392 chp->ch_flags |= ATACH_IRQ_WAIT; 1393 chp->ch_flags |= ATACH_IRQ_WAIT;
1393 chp->ch_status = 0; 1394 chp->ch_status = 0;
1394 /* start command */ 1395 /* start command */
1395 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot); 1396 AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
1396 /* and says we started this command */ 1397 /* and says we started this command */
1397 achp->ahcic_cmds_active |= 1 << slot; 1398 achp->ahcic_cmds_active |= 1 << slot;
1398 1399
1399 if ((xfer->c_flags & C_POLL) == 0) { 1400 if ((xfer->c_flags & C_POLL) == 0) {
1400 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1401 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1401 callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout), 1402 callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
1402 ahci_timeout, chp); 1403 ahci_timeout, chp);
1403 return; 1404 return;
1404 } 1405 }
1405 /* 1406 /*
1406 * Polled command.  1407 * Polled command.
1407 */ 1408 */
1408 for (i = 0; i < ATA_DELAY / 10; i++) { 1409 for (i = 0; i < ATA_DELAY / 10; i++) {
1409 if (sc_xfer->xs_status & XS_STS_DONE) 1410 if (sc_xfer->xs_status & XS_STS_DONE)
1410 break; 1411 break;
1411 ahci_intr_port(sc, achp); 1412 ahci_intr_port(sc, achp);
1412 delay(10000); 1413 delay(10000);
1413 } 1414 }
1414 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,  1415 AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
1415 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS), 1416 AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
1416 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)), 1417 AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
1417 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)), 1418 AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
1418 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))), 1419 AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
1419 DEBUG_XFERS); 1420 DEBUG_XFERS);
1420 if ((sc_xfer->xs_status & XS_STS_DONE) == 0) { 1421 if ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
1421 sc_xfer->error = XS_TIMEOUT; 1422 sc_xfer->error = XS_TIMEOUT;
1422 ahci_atapi_complete(chp, xfer, slot); 1423 ahci_atapi_complete(chp, xfer, slot);
1423 } 1424 }
1424 /* reenable interrupts */ 1425 /* reenable interrupts */
1425 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE); 1426 AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
1426} 1427}
1427 1428
1428static int 1429static int
1429ahci_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 1430ahci_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
1430{ 1431{
1431 int slot = 0; /* XXX slot */ 1432 int slot = 0; /* XXX slot */
1432 struct scsipi_xfer *sc_xfer = xfer->c_cmd; 1433 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1433 int drive = xfer->c_drive; 1434 int drive = xfer->c_drive;
1434 struct ahci_channel *achp = (struct ahci_channel *)chp; 1435 struct ahci_channel *achp = (struct ahci_channel *)chp;
1435 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac; 1436 struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1436 1437
1437 AHCIDEBUG_PRINT(("ahci_atapi_complete channel %d\n", chp->ch_channel), 1438 AHCIDEBUG_PRINT(("ahci_atapi_complete channel %d\n", chp->ch_channel),
1438 DEBUG_FUNCS); 1439 DEBUG_FUNCS);
1439 1440
1440 achp->ahcic_cmds_active &= ~(1 << slot); 1441 achp->ahcic_cmds_active &= ~(1 << slot);
1441 chp->ch_flags &= ~ATACH_IRQ_WAIT; 1442 chp->ch_flags &= ~ATACH_IRQ_WAIT;
1442 if (xfer->c_flags & C_TIMEOU) { 1443 if (xfer->c_flags & C_TIMEOU) {
1443 sc_xfer->error = XS_TIMEOUT; 1444 sc_xfer->error = XS_TIMEOUT;
1444 } else { 1445 } else {
1445 callout_stop(&chp->ch_callout); 1446 callout_stop(&chp->ch_callout);
1446 sc_xfer->error = 0; 1447 sc_xfer->error = 0;
1447 } 1448 }
1448 1449
1449 chp->ch_queue->active_xfer = NULL; 1450 chp->ch_queue->active_xfer = NULL;
1450 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0, 1451 bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1451 achp->ahcic_datad[slot]->dm_mapsize, 1452 achp->ahcic_datad[slot]->dm_mapsize,
1452 (sc_xfer->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD : 1453 (sc_xfer->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
1453 BUS_DMASYNC_POSTWRITE); 1454 BUS_DMASYNC_POSTWRITE);
1454 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]); 1455 bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
1455 1456
1456 if (chp->ch_drive[drive].drive_flags & DRIVE_WAITDRAIN) { 1457 if (chp->ch_drive[drive].drive_flags & DRIVE_WAITDRAIN) {
1457 ahci_atapi_kill_xfer(chp, xfer, KILL_GONE); 1458 ahci_atapi_kill_xfer(chp, xfer, KILL_GONE);
1458 chp->ch_drive[drive].drive_flags &= ~DRIVE_WAITDRAIN; 1459 chp->ch_drive[drive].drive_flags &= ~DRIVE_WAITDRAIN;
1459 wakeup(&chp->ch_queue->active_xfer); 1460 wakeup(&chp->ch_queue->active_xfer);
1460 return 0; 1461 return 0;
1461 } 1462 }
1462 ata_free_xfer(chp, xfer); 1463 ata_free_xfer(chp, xfer);
1463 1464
1464 AHCI_CMDH_SYNC(sc, achp, slot, 1465 AHCI_CMDH_SYNC(sc, achp, slot,
1465 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1466 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1466 sc_xfer->resid = sc_xfer->datalen; 1467 sc_xfer->resid = sc_xfer->datalen;
1467 sc_xfer->resid -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc); 1468 sc_xfer->resid -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc);
1468 AHCIDEBUG_PRINT(("ahci_atapi_complete datalen %d resid %d\n", 1469 AHCIDEBUG_PRINT(("ahci_atapi_complete datalen %d resid %d\n",
1469 sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS); 1470 sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
1470 if (chp->ch_status & WDCS_ERR &&  1471 if (chp->ch_status & WDCS_ERR &&
1471 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 || 1472 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1472 sc_xfer->resid == sc_xfer->datalen)) { 1473 sc_xfer->resid == sc_xfer->datalen)) {
1473 sc_xfer->error = XS_SHORTSENSE; 1474 sc_xfer->error = XS_SHORTSENSE;
1474 sc_xfer->sense.atapi_sense = chp->ch_error; 1475 sc_xfer->sense.atapi_sense = chp->ch_error;
1475 if ((sc_xfer->xs_periph->periph_quirks & 1476 if ((sc_xfer->xs_periph->periph_quirks &
1476 PQUIRK_NOSENSE) == 0) { 1477 PQUIRK_NOSENSE) == 0) {
1477 /* ask scsipi to send a REQUEST_SENSE */ 1478 /* ask scsipi to send a REQUEST_SENSE */
1478 sc_xfer->error = XS_BUSY; 1479 sc_xfer->error = XS_BUSY;
1479 sc_xfer->status = SCSI_CHECK; 1480 sc_xfer->status = SCSI_CHECK;
1480 } 1481 }
1481 }  1482 }
1482 scsipi_done(sc_xfer); 1483 scsipi_done(sc_xfer);
1483 atastart(chp); 1484 atastart(chp);
1484 return 0; 1485 return 0;
1485} 1486}
1486 1487
1487static void 1488static void
1488ahci_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason) 1489ahci_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
1489{ 1490{
1490 struct scsipi_xfer *sc_xfer = xfer->c_cmd; 1491 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1491 struct ahci_channel *achp = (struct ahci_channel *)chp; 1492 struct ahci_channel *achp = (struct ahci_channel *)chp;
1492 int slot = 0; /* XXX slot */ 1493 int slot = 0; /* XXX slot */
1493 1494
1494 achp->ahcic_cmds_active &= ~(1 << slot); 1495 achp->ahcic_cmds_active &= ~(1 << slot);
1495 1496
1496 /* remove this command from xfer queue */ 1497 /* remove this command from xfer queue */
1497 switch (reason) { 1498 switch (reason) {
1498 case KILL_GONE: 1499 case KILL_GONE:
1499 sc_xfer->error = XS_DRIVER_STUFFUP; 1500 sc_xfer->error = XS_DRIVER_STUFFUP;
1500 break; 1501 break;
1501 case KILL_RESET: 1502 case KILL_RESET:
1502 sc_xfer->error = XS_RESET; 1503 sc_xfer->error = XS_RESET;
1503 break; 1504 break;
1504 default: 1505 default:
1505 printf("ahci_ata_atapi_kill_xfer: unknown reason %d\n", reason); 1506 printf("ahci_ata_atapi_kill_xfer: unknown reason %d\n", reason);
1506 panic("ahci_ata_atapi_kill_xfer"); 1507 panic("ahci_ata_atapi_kill_xfer");
1507 } 1508 }
1508 ata_free_xfer(chp, xfer); 1509 ata_free_xfer(chp, xfer);
1509 scsipi_done(sc_xfer); 1510 scsipi_done(sc_xfer);
1510} 1511}
1511 1512
1512static void 1513static void
1513ahci_atapi_probe_device(struct atapibus_softc *sc, int target) 1514ahci_atapi_probe_device(struct atapibus_softc *sc, int target)
1514{ 1515{
1515 struct scsipi_channel *chan = sc->sc_channel; 1516 struct scsipi_channel *chan = sc->sc_channel;
1516 struct scsipi_periph *periph; 1517 struct scsipi_periph *periph;
1517 struct ataparams ids; 1518 struct ataparams ids;
1518 struct ataparams *id = &ids; 1519 struct ataparams *id = &ids;
1519 struct ahci_softc *ahcic = 1520 struct ahci_softc *ahcic =
1520 device_private(chan->chan_adapter->adapt_dev); 1521 device_private(chan->chan_adapter->adapt_dev);
1521 struct atac_softc *atac = &ahcic->sc_atac; 1522 struct atac_softc *atac = &ahcic->sc_atac;
1522 struct ata_channel *chp = atac->atac_channels[chan->chan_channel]; 1523 struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
1523 struct ata_drive_datas *drvp = &chp->ch_drive[target]; 1524 struct ata_drive_datas *drvp = &chp->ch_drive[target];
1524 struct scsipibus_attach_args sa; 1525 struct scsipibus_attach_args sa;
1525 char serial_number[21], model[41], firmware_revision[9]; 1526 char serial_number[21], model[41], firmware_revision[9];
1526 int s; 1527 int s;
1527 1528
1528 /* skip if already attached */ 1529 /* skip if already attached */
1529 if (scsipi_lookup_periph(chan, target, 0) != NULL) 1530 if (scsipi_lookup_periph(chan, target, 0) != NULL)
1530 return; 1531 return;
1531 1532
1532 /* if no ATAPI device detected at attach time, skip */ 1533 /* if no ATAPI device detected at attach time, skip */
1533 if ((drvp->drive_flags & DRIVE_ATAPI) == 0) { 1534 if ((drvp->drive_flags & DRIVE_ATAPI) == 0) {
1534 AHCIDEBUG_PRINT(("ahci_atapi_probe_device: drive %d " 1535 AHCIDEBUG_PRINT(("ahci_atapi_probe_device: drive %d "
1535 "not present\n", target), DEBUG_PROBE); 1536 "not present\n", target), DEBUG_PROBE);
1536 return; 1537 return;
1537 } 1538 }
1538 1539
1539 /* Some ATAPI devices need a bit more time after software reset. */ 1540 /* Some ATAPI devices need a bit more time after software reset. */
1540 delay(5000); 1541 delay(5000);
1541 if (ata_get_params(drvp, AT_WAIT, id) == 0) { 1542 if (ata_get_params(drvp, AT_WAIT, id) == 0) {
1542#ifdef ATAPI_DEBUG_PROBE 1543#ifdef ATAPI_DEBUG_PROBE
1543 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 1544 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
1544 AHCINAME(ahcic), target, 1545 AHCINAME(ahcic), target,
1545 id->atap_config & ATAPI_CFG_CMD_MASK, 1546 id->atap_config & ATAPI_CFG_CMD_MASK,
1546 id->atap_config & ATAPI_CFG_DRQ_MASK); 1547 id->atap_config & ATAPI_CFG_DRQ_MASK);
1547#endif 1548#endif
1548 periph = scsipi_alloc_periph(M_NOWAIT); 1549 periph = scsipi_alloc_periph(M_NOWAIT);
1549 if (periph == NULL) { 1550 if (periph == NULL) {
1550 aprint_error_dev(sc->sc_dev, 1551 aprint_error_dev(sc->sc_dev,
1551 "unable to allocate periph for drive %d\n", 1552 "unable to allocate periph for drive %d\n",
1552 target); 1553 target);
1553 return; 1554 return;
1554 } 1555 }
1555 periph->periph_dev = NULL; 1556 periph->periph_dev = NULL;
1556 periph->periph_channel = chan; 1557 periph->periph_channel = chan;
1557 periph->periph_switch = &atapi_probe_periphsw; 1558 periph->periph_switch = &atapi_probe_periphsw;
1558 periph->periph_target = target; 1559 periph->periph_target = target;
1559 periph->periph_lun = 0; 1560 periph->periph_lun = 0;
1560 periph->periph_quirks = PQUIRK_ONLYBIG; 1561 periph->periph_quirks = PQUIRK_ONLYBIG;
1561 1562
1562#ifdef SCSIPI_DEBUG 1563#ifdef SCSIPI_DEBUG
1563 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI && 1564 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
1564 SCSIPI_DEBUG_TARGET == target) 1565 SCSIPI_DEBUG_TARGET == target)
1565 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS; 1566 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
1566#endif 1567#endif
1567 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config); 1568 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
1568 if (id->atap_config & ATAPI_CFG_REMOV) 1569 if (id->atap_config & ATAPI_CFG_REMOV)
1569 periph->periph_flags |= PERIPH_REMOVABLE; 1570 periph->periph_flags |= PERIPH_REMOVABLE;
1570 if (periph->periph_type == T_SEQUENTIAL) { 1571 if (periph->periph_type == T_SEQUENTIAL) {
1571 s = splbio(); 1572 s = splbio();
1572 drvp->drive_flags |= DRIVE_ATAPIST; 1573 drvp->drive_flags |= DRIVE_ATAPIST;
1573 splx(s); 1574 splx(s);
1574 } 1575 }
1575 1576
1576 sa.sa_periph = periph; 1577 sa.sa_periph = periph;
1577 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 1578 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
1578 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ? 1579 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
1579 T_REMOV : T_FIXED; 1580 T_REMOV : T_FIXED;
1580 scsipi_strvis((u_char *)model, 40, id->atap_model, 40); 1581 scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
1581 scsipi_strvis((u_char *)serial_number, 20, id->atap_serial, 1582 scsipi_strvis((u_char *)serial_number, 20, id->atap_serial,
1582 20); 1583 20);
1583 scsipi_strvis((u_char *)firmware_revision, 8, 1584 scsipi_strvis((u_char *)firmware_revision, 8,
1584 id->atap_revision, 8); 1585 id->atap_revision, 8);
1585 sa.sa_inqbuf.vendor = model; 1586 sa.sa_inqbuf.vendor = model;
1586 sa.sa_inqbuf.product = serial_number; 1587 sa.sa_inqbuf.product = serial_number;
1587 sa.sa_inqbuf.revision = firmware_revision; 1588 sa.sa_inqbuf.revision = firmware_revision;
1588 1589
1589 /* 1590 /*
1590 * Determine the operating mode capabilities of the device. 1591 * Determine the operating mode capabilities of the device.
1591 */ 1592 */
1592 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 1593 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
1593 periph->periph_cap |= PERIPH_CAP_CMD16; 1594 periph->periph_cap |= PERIPH_CAP_CMD16;
1594 /* XXX This is gross. */ 1595 /* XXX This is gross. */
1595 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK); 1596 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
1596 1597
1597 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa); 1598 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
1598 1599
1599 if (drvp->drv_softc) 1600 if (drvp->drv_softc)
1600 ata_probe_caps(drvp); 1601 ata_probe_caps(drvp);
1601 else { 1602 else {
1602 s = splbio(); 1603 s = splbio();
1603 drvp->drive_flags &= ~DRIVE_ATAPI; 1604 drvp->drive_flags &= ~DRIVE_ATAPI;
1604 splx(s); 1605 splx(s);
1605 } 1606 }
1606 } else { 1607 } else {
1607 AHCIDEBUG_PRINT(("ahci_atapi_get_params: ATAPI_IDENTIFY_DEVICE " 1608 AHCIDEBUG_PRINT(("ahci_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
1608 "failed for drive %s:%d:%d: error 0x%x\n", 1609 "failed for drive %s:%d:%d: error 0x%x\n",
1609 AHCINAME(ahcic), chp->ch_channel, target, 1610 AHCINAME(ahcic), chp->ch_channel, target,
1610 chp->ch_error), DEBUG_PROBE); 1611 chp->ch_error), DEBUG_PROBE);
1611 s = splbio(); 1612 s = splbio();
1612 drvp->drive_flags &= ~DRIVE_ATAPI; 1613 drvp->drive_flags &= ~DRIVE_ATAPI;
1613 splx(s); 1614 splx(s);
1614 } 1615 }
1615} 1616}
1616#endif /* NATAPIBUS */ 1617#endif /* NATAPIBUS */