ahcisata_core.c revision 1.7 1 1.5 bouyer /* $NetBSD: ahcisata_core.c,v 1.7 2007/11/12 20:10:32 joerg Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 2006 Manuel Bouyer.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.1 bouyer * must display the following acknowledgement:
16 1.1 bouyer * This product includes software developed by Manuel Bouyer.
17 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.1 bouyer * derived from this software without specific prior written permission.
19 1.1 bouyer *
20 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 bouyer *
31 1.1 bouyer */
32 1.1 bouyer
33 1.1 bouyer #include <sys/cdefs.h>
34 1.5 bouyer __KERNEL_RCSID(0, "$NetBSD: ahcisata_core.c,v 1.7 2007/11/12 20:10:32 joerg Exp $");
35 1.1 bouyer
36 1.1 bouyer #include <sys/types.h>
37 1.1 bouyer #include <sys/malloc.h>
38 1.1 bouyer #include <sys/param.h>
39 1.1 bouyer #include <sys/kernel.h>
40 1.1 bouyer #include <sys/systm.h>
41 1.1 bouyer #include <sys/disklabel.h>
42 1.4 ad #include <sys/proc.h>
43 1.1 bouyer
44 1.1 bouyer #include <uvm/uvm_extern.h>
45 1.1 bouyer
46 1.1 bouyer #include <dev/ic/wdcreg.h>
47 1.1 bouyer #include <dev/ata/atareg.h>
48 1.1 bouyer #include <dev/ata/satavar.h>
49 1.1 bouyer #include <dev/ata/satareg.h>
50 1.1 bouyer #include <dev/ic/ahcisatavar.h>
51 1.1 bouyer
52 1.1 bouyer #ifdef AHCI_DEBUG
53 1.1 bouyer int ahcidebug_mask = 0x0;
54 1.1 bouyer #endif
55 1.1 bouyer
56 1.1 bouyer void ahci_probe_drive(struct ata_channel *);
57 1.1 bouyer void ahci_setup_channel(struct ata_channel *);
58 1.1 bouyer
59 1.1 bouyer int ahci_ata_bio(struct ata_drive_datas *, struct ata_bio *);
60 1.1 bouyer void ahci_reset_drive(struct ata_drive_datas *, int);
61 1.1 bouyer void ahci_reset_channel(struct ata_channel *, int);
62 1.1 bouyer int ahci_exec_command(struct ata_drive_datas *, struct ata_command *);
63 1.1 bouyer int ahci_ata_addref(struct ata_drive_datas *);
64 1.1 bouyer void ahci_ata_delref(struct ata_drive_datas *);
65 1.1 bouyer void ahci_killpending(struct ata_drive_datas *);
66 1.1 bouyer
67 1.1 bouyer void ahci_cmd_start(struct ata_channel *, struct ata_xfer *);
68 1.1 bouyer int ahci_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
69 1.1 bouyer void ahci_cmd_done(struct ata_channel *, struct ata_xfer *, int);
70 1.1 bouyer void ahci_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
71 1.1 bouyer void ahci_bio_start(struct ata_channel *, struct ata_xfer *);
72 1.1 bouyer int ahci_bio_complete(struct ata_channel *, struct ata_xfer *, int);
73 1.1 bouyer void ahci_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
74 1.5 bouyer void ahci_channel_stop(struct ahci_softc *, struct ata_channel *, int);
75 1.1 bouyer void ahci_channel_start(struct ahci_softc *, struct ata_channel *);
76 1.1 bouyer void ahci_timeout(void *);
77 1.1 bouyer int ahci_dma_setup(struct ata_channel *, int, void *, size_t, int);
78 1.1 bouyer
79 1.1 bouyer #define ATA_DELAY 10000 /* 10s for a drive I/O */
80 1.1 bouyer
81 1.1 bouyer const struct ata_bustype ahci_ata_bustype = {
82 1.1 bouyer SCSIPI_BUSTYPE_ATA,
83 1.1 bouyer ahci_ata_bio,
84 1.1 bouyer ahci_reset_drive,
85 1.1 bouyer ahci_reset_channel,
86 1.1 bouyer ahci_exec_command,
87 1.1 bouyer ata_get_params,
88 1.1 bouyer ahci_ata_addref,
89 1.1 bouyer ahci_ata_delref,
90 1.1 bouyer ahci_killpending
91 1.1 bouyer };
92 1.1 bouyer
93 1.1 bouyer void ahci_intr_port(struct ahci_softc *, struct ahci_channel *);
94 1.1 bouyer
95 1.7 joerg static void ahci_setup_port(struct ahci_softc *sc, int i);
96 1.7 joerg
97 1.7 joerg int
98 1.7 joerg ahci_reset(struct ahci_softc *sc)
99 1.1 bouyer {
100 1.7 joerg int i;
101 1.1 bouyer
102 1.1 bouyer /* reset controller */
103 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_HR);
104 1.1 bouyer /* wait up to 1s for reset to complete */
105 1.1 bouyer for (i = 0; i < 1000; i++) {
106 1.6 bouyer delay(1000);
107 1.1 bouyer if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR) == 0)
108 1.1 bouyer break;
109 1.1 bouyer }
110 1.1 bouyer if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR)) {
111 1.1 bouyer aprint_error("%s: reset failed\n", AHCINAME(sc));
112 1.7 joerg return -1;
113 1.1 bouyer }
114 1.1 bouyer /* enable ahci mode */
115 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_AE);
116 1.7 joerg return 0;
117 1.7 joerg }
118 1.1 bouyer
119 1.7 joerg void
120 1.7 joerg ahci_setup_ports(struct ahci_softc *sc)
121 1.7 joerg {
122 1.7 joerg u_int32_t ahci_ports;
123 1.7 joerg int i, port;
124 1.7 joerg
125 1.7 joerg ahci_ports = AHCI_READ(sc, AHCI_PI);
126 1.7 joerg for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
127 1.7 joerg if ((ahci_ports & (1 << i)) == 0)
128 1.7 joerg continue;
129 1.7 joerg if (port >= sc->sc_atac.atac_nchannels) {
130 1.7 joerg aprint_error("%s: more ports than announced\n",
131 1.7 joerg AHCINAME(sc));
132 1.7 joerg break;
133 1.7 joerg }
134 1.7 joerg ahci_setup_port(sc, i);
135 1.7 joerg }
136 1.7 joerg }
137 1.7 joerg
138 1.7 joerg void
139 1.7 joerg ahci_reprobe_drives(struct ahci_softc *sc)
140 1.7 joerg {
141 1.7 joerg u_int32_t ahci_ports;
142 1.7 joerg int i, port;
143 1.7 joerg struct ahci_channel *achp;
144 1.7 joerg struct ata_channel *chp;
145 1.7 joerg
146 1.7 joerg ahci_ports = AHCI_READ(sc, AHCI_PI);
147 1.7 joerg for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
148 1.7 joerg if ((ahci_ports & (1 << i)) == 0)
149 1.7 joerg continue;
150 1.7 joerg if (port >= sc->sc_atac.atac_nchannels) {
151 1.7 joerg aprint_error("%s: more ports than announced\n",
152 1.7 joerg AHCINAME(sc));
153 1.7 joerg break;
154 1.7 joerg }
155 1.7 joerg achp = &sc->sc_channels[i];
156 1.7 joerg chp = &achp->ata_channel;
157 1.7 joerg
158 1.7 joerg ahci_probe_drive(chp);
159 1.7 joerg }
160 1.7 joerg }
161 1.7 joerg
162 1.7 joerg static void
163 1.7 joerg ahci_setup_port(struct ahci_softc *sc, int i)
164 1.7 joerg {
165 1.7 joerg struct ahci_channel *achp;
166 1.7 joerg
167 1.7 joerg achp = &sc->sc_channels[i];
168 1.7 joerg
169 1.7 joerg AHCI_WRITE(sc, AHCI_P_CLB(i), achp->ahcic_bus_cmdh);
170 1.7 joerg AHCI_WRITE(sc, AHCI_P_CLBU(i), 0);
171 1.7 joerg AHCI_WRITE(sc, AHCI_P_FB(i), achp->ahcic_bus_rfis);
172 1.7 joerg AHCI_WRITE(sc, AHCI_P_FBU(i), 0);
173 1.7 joerg }
174 1.7 joerg
175 1.7 joerg void
176 1.7 joerg ahci_enable_intrs(struct ahci_softc *sc)
177 1.7 joerg {
178 1.7 joerg
179 1.7 joerg /* clear interrupts */
180 1.7 joerg AHCI_WRITE(sc, AHCI_IS, AHCI_READ(sc, AHCI_IS));
181 1.7 joerg /* enable interrupts */
182 1.7 joerg AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
183 1.7 joerg }
184 1.7 joerg
185 1.7 joerg void
186 1.7 joerg ahci_attach(struct ahci_softc *sc)
187 1.7 joerg {
188 1.7 joerg u_int32_t ahci_cap, ahci_rev, ahci_ports;
189 1.7 joerg int i, j, port;
190 1.7 joerg struct ahci_channel *achp;
191 1.7 joerg struct ata_channel *chp;
192 1.7 joerg int error;
193 1.7 joerg bus_dma_segment_t seg;
194 1.7 joerg int rseg;
195 1.7 joerg int dmasize;
196 1.7 joerg void *cmdhp;
197 1.7 joerg void *cmdtblp;
198 1.7 joerg
199 1.7 joerg if (ahci_reset(sc) != 0)
200 1.7 joerg return;
201 1.1 bouyer
202 1.1 bouyer ahci_cap = AHCI_READ(sc, AHCI_CAP);
203 1.1 bouyer sc->sc_atac.atac_nchannels = (ahci_cap & AHCI_CAP_NPMASK) + 1;
204 1.1 bouyer sc->sc_ncmds = ((ahci_cap & AHCI_CAP_NCS) >> 8) + 1;
205 1.1 bouyer ahci_rev = AHCI_READ(sc, AHCI_VS);
206 1.1 bouyer aprint_normal("%s: AHCI revision ", AHCINAME(sc));
207 1.1 bouyer switch(ahci_rev) {
208 1.1 bouyer case AHCI_VS_10:
209 1.1 bouyer aprint_normal("1.0");
210 1.1 bouyer break;
211 1.1 bouyer case AHCI_VS_11:
212 1.1 bouyer aprint_normal("1.1");
213 1.1 bouyer break;
214 1.1 bouyer default:
215 1.1 bouyer aprint_normal("0x%x", ahci_rev);
216 1.1 bouyer break;
217 1.1 bouyer }
218 1.1 bouyer
219 1.1 bouyer aprint_normal(", %d ports, %d command slots, features 0x%x\n",
220 1.1 bouyer sc->sc_atac.atac_nchannels, sc->sc_ncmds,
221 1.1 bouyer ahci_cap & ~(AHCI_CAP_NPMASK|AHCI_CAP_NCS));
222 1.1 bouyer sc->sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DMA | ATAC_CAP_UDMA;
223 1.1 bouyer sc->sc_atac.atac_pio_cap = 4;
224 1.1 bouyer sc->sc_atac.atac_dma_cap = 2;
225 1.1 bouyer sc->sc_atac.atac_udma_cap = 6;
226 1.1 bouyer sc->sc_atac.atac_channels = sc->sc_chanarray;
227 1.1 bouyer sc->sc_atac.atac_atapibus_attach = NULL; /* XXX */
228 1.1 bouyer sc->sc_atac.atac_probe = ahci_probe_drive;
229 1.1 bouyer sc->sc_atac.atac_bustype_ata = &ahci_ata_bustype;
230 1.1 bouyer sc->sc_atac.atac_set_modes = ahci_setup_channel;
231 1.1 bouyer
232 1.1 bouyer dmasize =
233 1.1 bouyer (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels;
234 1.1 bouyer error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
235 1.1 bouyer &seg, 1, &rseg, BUS_DMA_NOWAIT);
236 1.1 bouyer if (error) {
237 1.1 bouyer aprint_error("%s: unable to allocate command header memory"
238 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
239 1.1 bouyer return;
240 1.1 bouyer }
241 1.1 bouyer error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, dmasize,
242 1.1 bouyer &cmdhp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
243 1.1 bouyer if (error) {
244 1.1 bouyer aprint_error("%s: unable to map command header memory"
245 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
246 1.1 bouyer return;
247 1.1 bouyer }
248 1.1 bouyer error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
249 1.1 bouyer BUS_DMA_NOWAIT, &sc->sc_cmd_hdrd);
250 1.1 bouyer if (error) {
251 1.1 bouyer aprint_error("%s: unable to create command header map"
252 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
253 1.1 bouyer return;
254 1.1 bouyer }
255 1.1 bouyer error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmd_hdrd,
256 1.1 bouyer cmdhp, dmasize, NULL, BUS_DMA_NOWAIT);
257 1.1 bouyer if (error) {
258 1.1 bouyer aprint_error("%s: unable to load command header map"
259 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
260 1.1 bouyer return;
261 1.1 bouyer }
262 1.1 bouyer sc->sc_cmd_hdr = cmdhp;
263 1.1 bouyer
264 1.7 joerg ahci_enable_intrs(sc);
265 1.1 bouyer
266 1.1 bouyer ahci_ports = AHCI_READ(sc, AHCI_PI);
267 1.1 bouyer for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
268 1.1 bouyer if ((ahci_ports & (1 << i)) == 0)
269 1.1 bouyer continue;
270 1.1 bouyer if (port >= sc->sc_atac.atac_nchannels) {
271 1.1 bouyer aprint_error("%s: more ports than announced\n",
272 1.1 bouyer AHCINAME(sc));
273 1.1 bouyer break;
274 1.1 bouyer }
275 1.1 bouyer achp = &sc->sc_channels[i];
276 1.1 bouyer chp = (struct ata_channel *)achp;
277 1.1 bouyer sc->sc_chanarray[i] = chp;
278 1.1 bouyer chp->ch_channel = i;
279 1.1 bouyer chp->ch_atac = &sc->sc_atac;
280 1.1 bouyer chp->ch_queue = malloc(sizeof(struct ata_queue),
281 1.1 bouyer M_DEVBUF, M_NOWAIT);
282 1.1 bouyer if (chp->ch_queue == NULL) {
283 1.1 bouyer aprint_error("%s port %d: can't allocate memory for "
284 1.1 bouyer "command queue", AHCINAME(sc), i);
285 1.1 bouyer break;
286 1.1 bouyer }
287 1.1 bouyer dmasize = AHCI_CMDTBL_SIZE * sc->sc_ncmds;
288 1.1 bouyer error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
289 1.1 bouyer &seg, 1, &rseg, BUS_DMA_NOWAIT);
290 1.1 bouyer if (error) {
291 1.1 bouyer aprint_error("%s: unable to allocate command table "
292 1.1 bouyer "memory, error=%d\n", AHCINAME(sc), error);
293 1.1 bouyer break;
294 1.1 bouyer }
295 1.1 bouyer error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, dmasize,
296 1.1 bouyer &cmdtblp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
297 1.1 bouyer if (error) {
298 1.1 bouyer aprint_error("%s: unable to map command table memory"
299 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
300 1.1 bouyer break;
301 1.1 bouyer }
302 1.1 bouyer error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
303 1.1 bouyer BUS_DMA_NOWAIT, &achp->ahcic_cmd_tbld);
304 1.1 bouyer if (error) {
305 1.1 bouyer aprint_error("%s: unable to create command table map"
306 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
307 1.1 bouyer break;
308 1.1 bouyer }
309 1.1 bouyer error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_cmd_tbld,
310 1.1 bouyer cmdtblp, dmasize, NULL, BUS_DMA_NOWAIT);
311 1.1 bouyer if (error) {
312 1.1 bouyer aprint_error("%s: unable to load command table map"
313 1.1 bouyer ", error=%d\n", AHCINAME(sc), error);
314 1.1 bouyer break;
315 1.1 bouyer }
316 1.1 bouyer achp->ahcic_cmdh = (struct ahci_cmd_header *)
317 1.1 bouyer ((char *)cmdhp + AHCI_CMDH_SIZE * port);
318 1.1 bouyer achp->ahcic_bus_cmdh = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
319 1.1 bouyer AHCI_CMDH_SIZE * port;
320 1.1 bouyer achp->ahcic_rfis = (struct ahci_r_fis *)
321 1.1 bouyer ((char *)cmdhp +
322 1.1 bouyer AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
323 1.1 bouyer AHCI_RFIS_SIZE * port);
324 1.1 bouyer achp->ahcic_bus_rfis = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
325 1.1 bouyer AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
326 1.1 bouyer AHCI_RFIS_SIZE * port;
327 1.1 bouyer AHCIDEBUG_PRINT(("port %d cmdh %p (0x%x) rfis %p (0x%x)\n", i,
328 1.1 bouyer achp->ahcic_cmdh, (u_int)achp->ahcic_bus_cmdh,
329 1.1 bouyer achp->ahcic_rfis, (u_int)achp->ahcic_bus_rfis),
330 1.1 bouyer DEBUG_PROBE);
331 1.1 bouyer
332 1.1 bouyer for (j = 0; j < sc->sc_ncmds; j++) {
333 1.1 bouyer achp->ahcic_cmd_tbl[j] = (struct ahci_cmd_tbl *)
334 1.1 bouyer ((char *)cmdtblp + AHCI_CMDTBL_SIZE * j);
335 1.1 bouyer achp->ahcic_bus_cmd_tbl[j] =
336 1.1 bouyer achp->ahcic_cmd_tbld->dm_segs[0].ds_addr +
337 1.1 bouyer AHCI_CMDTBL_SIZE * j;
338 1.1 bouyer achp->ahcic_cmdh[j].cmdh_cmdtba =
339 1.1 bouyer htole32(achp->ahcic_bus_cmd_tbl[j]);
340 1.1 bouyer achp->ahcic_cmdh[j].cmdh_cmdtbau = htole32(0);
341 1.1 bouyer AHCIDEBUG_PRINT(("port %d/%d tbl %p (0x%x)\n", i, j,
342 1.1 bouyer achp->ahcic_cmd_tbl[j],
343 1.1 bouyer (u_int)achp->ahcic_bus_cmd_tbl[j]), DEBUG_PROBE);
344 1.1 bouyer /* The xfer DMA map */
345 1.1 bouyer error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
346 1.1 bouyer AHCI_NPRD, 0x400000 /* 4MB */, 0,
347 1.1 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
348 1.1 bouyer &achp->ahcic_datad[j]);
349 1.1 bouyer if (error) {
350 1.1 bouyer aprint_error("%s: couldn't alloc xfer DMA map, "
351 1.1 bouyer "error=%d\n", AHCINAME(sc), error);
352 1.1 bouyer goto end;
353 1.1 bouyer }
354 1.1 bouyer }
355 1.7 joerg ahci_setup_port(sc, i);
356 1.1 bouyer chp->ch_ndrive = 1;
357 1.1 bouyer if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
358 1.1 bouyer AHCI_P_SSTS(i), 1, &achp->ahcic_sstatus) != 0) {
359 1.1 bouyer aprint_error("%s: couldn't map channel %d "
360 1.1 bouyer "sata_status regs\n", AHCINAME(sc), i);
361 1.1 bouyer break;
362 1.1 bouyer }
363 1.1 bouyer if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
364 1.1 bouyer AHCI_P_SCTL(i), 1, &achp->ahcic_scontrol) != 0) {
365 1.1 bouyer aprint_error("%s: couldn't map channel %d "
366 1.1 bouyer "sata_control regs\n", AHCINAME(sc), i);
367 1.1 bouyer break;
368 1.1 bouyer }
369 1.1 bouyer if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
370 1.1 bouyer AHCI_P_SERR(i), 1, &achp->ahcic_serror) != 0) {
371 1.1 bouyer aprint_error("%s: couldn't map channel %d "
372 1.1 bouyer "sata_error regs\n", AHCINAME(sc), i);
373 1.1 bouyer break;
374 1.1 bouyer }
375 1.1 bouyer ata_channel_attach(chp);
376 1.1 bouyer port++;
377 1.1 bouyer end:
378 1.1 bouyer continue;
379 1.1 bouyer }
380 1.1 bouyer }
381 1.1 bouyer
382 1.1 bouyer int
383 1.1 bouyer ahci_intr(void *v)
384 1.1 bouyer {
385 1.1 bouyer struct ahci_softc *sc = v;
386 1.1 bouyer u_int32_t is;
387 1.1 bouyer int i, r = 0;
388 1.1 bouyer
389 1.1 bouyer while ((is = AHCI_READ(sc, AHCI_IS))) {
390 1.1 bouyer AHCIDEBUG_PRINT(("%s ahci_intr 0x%x\n", AHCINAME(sc), is),
391 1.1 bouyer DEBUG_INTR);
392 1.1 bouyer r = 1;
393 1.1 bouyer AHCI_WRITE(sc, AHCI_IS, is);
394 1.1 bouyer for (i = 0; i < AHCI_MAX_PORTS; i++)
395 1.1 bouyer if (is & (1 << i))
396 1.1 bouyer ahci_intr_port(sc, &sc->sc_channels[i]);
397 1.1 bouyer }
398 1.1 bouyer return r;
399 1.1 bouyer }
400 1.1 bouyer
401 1.1 bouyer void
402 1.1 bouyer ahci_intr_port(struct ahci_softc *sc, struct ahci_channel *achp)
403 1.1 bouyer {
404 1.1 bouyer u_int32_t is, tfd;
405 1.1 bouyer struct ata_channel *chp = &achp->ata_channel;
406 1.1 bouyer struct ata_xfer *xfer = chp->ch_queue->active_xfer;
407 1.1 bouyer int slot;
408 1.1 bouyer
409 1.1 bouyer is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
410 1.1 bouyer AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), is);
411 1.1 bouyer AHCIDEBUG_PRINT(("ahci_intr_port %s port %d is 0x%x CI 0x%x\n", AHCINAME(sc),
412 1.1 bouyer chp->ch_channel, is, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
413 1.1 bouyer DEBUG_INTR);
414 1.1 bouyer
415 1.1 bouyer if (is & (AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
416 1.1 bouyer AHCI_P_IX_OFS | AHCI_P_IX_UFS)) {
417 1.1 bouyer slot = (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))
418 1.1 bouyer & AHCI_P_CMD_CCS_MASK) >> AHCI_P_CMD_CCS_SHIFT;
419 1.1 bouyer if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
420 1.1 bouyer return;
421 1.1 bouyer /* stop channel */
422 1.5 bouyer ahci_channel_stop(sc, chp, 0);
423 1.1 bouyer if (slot != 0) {
424 1.1 bouyer printf("ahci_intr_port: slot %d\n", slot);
425 1.1 bouyer panic("ahci_intr_port");
426 1.1 bouyer }
427 1.1 bouyer if (is & AHCI_P_IX_TFES) {
428 1.1 bouyer tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
429 1.1 bouyer chp->ch_error =
430 1.1 bouyer (tfd & AHCI_P_TFD_ERR_MASK) >> AHCI_P_TFD_ERR_SHIFT;
431 1.1 bouyer chp->ch_status = (tfd & 0xff);
432 1.1 bouyer } else {
433 1.1 bouyer /* emulate a CRC error */
434 1.1 bouyer chp->ch_error = WDCE_CRC;
435 1.1 bouyer chp->ch_status = WDCS_ERR;
436 1.1 bouyer }
437 1.1 bouyer xfer->c_intr(chp, xfer, is);
438 1.5 bouyer /* if channel has not been restarted, do it now */
439 1.5 bouyer if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
440 1.5 bouyer == 0)
441 1.5 bouyer ahci_channel_start(sc, chp);
442 1.1 bouyer } else {
443 1.1 bouyer slot = 0; /* XXX */
444 1.1 bouyer is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
445 1.1 bouyer AHCIDEBUG_PRINT(("ahci_intr_port port %d is 0x%x act 0x%x CI 0x%x\n",
446 1.1 bouyer chp->ch_channel, is, achp->ahcic_cmds_active,
447 1.1 bouyer AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_INTR);
448 1.1 bouyer if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
449 1.1 bouyer return;
450 1.1 bouyer if ((AHCI_READ(sc, AHCI_P_CI(chp->ch_channel)) & (1 << slot))
451 1.1 bouyer == 0) {
452 1.1 bouyer xfer->c_intr(chp, xfer, 0);
453 1.1 bouyer }
454 1.1 bouyer }
455 1.1 bouyer }
456 1.1 bouyer
457 1.1 bouyer void
458 1.1 bouyer ahci_reset_drive(struct ata_drive_datas *drvp, int flags)
459 1.1 bouyer {
460 1.1 bouyer struct ata_channel *chp = drvp->chnl_softc;
461 1.1 bouyer ata_reset_channel(chp, flags);
462 1.1 bouyer return;
463 1.1 bouyer }
464 1.1 bouyer
465 1.1 bouyer void
466 1.1 bouyer ahci_reset_channel(struct ata_channel *chp, int flags)
467 1.1 bouyer {
468 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
469 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
470 1.1 bouyer
471 1.5 bouyer ahci_channel_stop(sc, chp, flags);
472 1.1 bouyer if (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
473 1.1 bouyer achp->ahcic_sstatus) != SStatus_DET_DEV) {
474 1.1 bouyer printf("%s: port reset failed\n", AHCINAME(sc));
475 1.1 bouyer /* XXX and then ? */
476 1.1 bouyer }
477 1.1 bouyer AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel),
478 1.1 bouyer AHCI_READ(sc, AHCI_P_SERR(chp->ch_channel)));
479 1.1 bouyer if (chp->ch_queue->active_xfer) {
480 1.1 bouyer chp->ch_queue->active_xfer->c_kill_xfer(chp,
481 1.1 bouyer chp->ch_queue->active_xfer, KILL_RESET);
482 1.1 bouyer }
483 1.1 bouyer ahci_channel_start(sc, chp);
484 1.1 bouyer return;
485 1.1 bouyer }
486 1.1 bouyer
487 1.1 bouyer int
488 1.1 bouyer ahci_ata_addref(struct ata_drive_datas *drvp)
489 1.1 bouyer {
490 1.1 bouyer return 0;
491 1.1 bouyer }
492 1.1 bouyer
493 1.1 bouyer void
494 1.1 bouyer ahci_ata_delref(struct ata_drive_datas *drvp)
495 1.1 bouyer {
496 1.1 bouyer return;
497 1.1 bouyer }
498 1.1 bouyer
499 1.1 bouyer void
500 1.1 bouyer ahci_killpending(struct ata_drive_datas *drvp)
501 1.1 bouyer {
502 1.1 bouyer return;
503 1.1 bouyer }
504 1.1 bouyer
505 1.1 bouyer void
506 1.1 bouyer ahci_probe_drive(struct ata_channel *chp)
507 1.1 bouyer {
508 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
509 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
510 1.1 bouyer int i, s;
511 1.1 bouyer u_int32_t sig;
512 1.1 bouyer
513 1.1 bouyer /* XXX This should be done by other code. */
514 1.1 bouyer for (i = 0; i < chp->ch_ndrive; i++) {
515 1.1 bouyer chp->ch_drive[i].chnl_softc = chp;
516 1.1 bouyer chp->ch_drive[i].drive = i;
517 1.1 bouyer }
518 1.1 bouyer
519 1.1 bouyer /* bring interface up, power up and spin up device */
520 1.1 bouyer AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
521 1.1 bouyer AHCI_P_CMD_ICC_AC | AHCI_P_CMD_POD | AHCI_P_CMD_SUD);
522 1.1 bouyer /* reset the PHY and bring online */
523 1.1 bouyer switch (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
524 1.1 bouyer achp->ahcic_sstatus)) {
525 1.1 bouyer case SStatus_DET_DEV:
526 1.1 bouyer AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel),
527 1.1 bouyer AHCI_READ(sc, AHCI_P_SERR(chp->ch_channel)));
528 1.1 bouyer sig = AHCI_READ(sc, AHCI_P_SIG(chp->ch_channel));
529 1.1 bouyer AHCIDEBUG_PRINT(("%s: port %d: sig=0x%x CMD=0x%x\n",
530 1.1 bouyer AHCINAME(sc), chp->ch_channel, sig,
531 1.1 bouyer AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))), DEBUG_PROBE);
532 1.1 bouyer /*
533 1.1 bouyer * scnt and sn are supposed to be 0x1 for ATAPI, but in some
534 1.1 bouyer * cases we get wrong values here, so ignore it.
535 1.1 bouyer */
536 1.1 bouyer s = splbio();
537 1.3 bouyer if ((sig & 0xffff0000) == 0xeb140000) {
538 1.3 bouyer aprint_error("%s port %d: ATAPI device ignored\n",
539 1.3 bouyer AHCINAME(sc), chp->ch_channel);
540 1.3 bouyer chp->ch_drive[0].drive_flags |= 0 /* DRIVE_ATAPI XXX */;
541 1.3 bouyer } else
542 1.1 bouyer chp->ch_drive[0].drive_flags |= DRIVE_ATA;
543 1.1 bouyer splx(s);
544 1.1 bouyer /* enable interrupts */
545 1.1 bouyer AHCI_WRITE(sc, AHCI_P_IE(chp->ch_channel),
546 1.1 bouyer AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
547 1.1 bouyer AHCI_P_IX_OFS | AHCI_P_IX_DPS | AHCI_P_IX_UFS |
548 1.1 bouyer AHCI_P_IX_DHRS);
549 1.1 bouyer /* and start operations */
550 1.1 bouyer ahci_channel_start(sc, chp);
551 1.1 bouyer break;
552 1.1 bouyer
553 1.1 bouyer default:
554 1.1 bouyer break;
555 1.1 bouyer }
556 1.1 bouyer }
557 1.1 bouyer
558 1.1 bouyer void
559 1.1 bouyer ahci_setup_channel(struct ata_channel *chp)
560 1.1 bouyer {
561 1.1 bouyer return;
562 1.1 bouyer }
563 1.1 bouyer
564 1.1 bouyer int
565 1.1 bouyer ahci_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
566 1.1 bouyer {
567 1.1 bouyer struct ata_channel *chp = drvp->chnl_softc;
568 1.1 bouyer struct ata_xfer *xfer;
569 1.1 bouyer int ret;
570 1.1 bouyer int s;
571 1.1 bouyer
572 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
573 1.1 bouyer AHCIDEBUG_PRINT(("ahci_exec_command port %d CI 0x%x\n",
574 1.1 bouyer chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
575 1.1 bouyer DEBUG_XFERS);
576 1.1 bouyer xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP :
577 1.1 bouyer ATAXF_NOSLEEP);
578 1.1 bouyer if (xfer == NULL) {
579 1.1 bouyer return ATACMD_TRY_AGAIN;
580 1.1 bouyer }
581 1.1 bouyer if (ata_c->flags & AT_POLL)
582 1.1 bouyer xfer->c_flags |= C_POLL;
583 1.1 bouyer if (ata_c->flags & AT_WAIT)
584 1.1 bouyer xfer->c_flags |= C_WAIT;
585 1.1 bouyer xfer->c_drive = drvp->drive;
586 1.1 bouyer xfer->c_databuf = ata_c->data;
587 1.1 bouyer xfer->c_bcount = ata_c->bcount;
588 1.1 bouyer xfer->c_cmd = ata_c;
589 1.1 bouyer xfer->c_start = ahci_cmd_start;
590 1.1 bouyer xfer->c_intr = ahci_cmd_complete;
591 1.1 bouyer xfer->c_kill_xfer = ahci_cmd_kill_xfer;
592 1.1 bouyer s = splbio();
593 1.1 bouyer ata_exec_xfer(chp, xfer);
594 1.1 bouyer #ifdef DIAGNOSTIC
595 1.1 bouyer if ((ata_c->flags & AT_POLL) != 0 &&
596 1.1 bouyer (ata_c->flags & AT_DONE) == 0)
597 1.1 bouyer panic("ahci_exec_command: polled command not done");
598 1.1 bouyer #endif
599 1.1 bouyer if (ata_c->flags & AT_DONE) {
600 1.1 bouyer ret = ATACMD_COMPLETE;
601 1.1 bouyer } else {
602 1.1 bouyer if (ata_c->flags & AT_WAIT) {
603 1.1 bouyer while ((ata_c->flags & AT_DONE) == 0) {
604 1.1 bouyer tsleep(ata_c, PRIBIO, "ahcicmd", 0);
605 1.1 bouyer }
606 1.1 bouyer ret = ATACMD_COMPLETE;
607 1.1 bouyer } else {
608 1.1 bouyer ret = ATACMD_QUEUED;
609 1.1 bouyer }
610 1.1 bouyer }
611 1.1 bouyer splx(s);
612 1.1 bouyer return ret;
613 1.1 bouyer }
614 1.1 bouyer
615 1.1 bouyer void
616 1.1 bouyer ahci_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
617 1.1 bouyer {
618 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
619 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
620 1.1 bouyer struct ata_command *ata_c = xfer->c_cmd;
621 1.1 bouyer int slot = 0 /* XXX slot */;
622 1.1 bouyer struct ahci_cmd_tbl *cmd_tbl;
623 1.1 bouyer struct ahci_cmd_header *cmd_h;
624 1.1 bouyer u_int8_t *fis;
625 1.1 bouyer int i;
626 1.1 bouyer int channel = chp->ch_channel;
627 1.1 bouyer
628 1.1 bouyer AHCIDEBUG_PRINT(("ahci_cmd_start CI 0x%x\n",
629 1.1 bouyer AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
630 1.1 bouyer
631 1.1 bouyer cmd_tbl = achp->ahcic_cmd_tbl[slot];
632 1.1 bouyer AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
633 1.1 bouyer cmd_tbl), DEBUG_XFERS);
634 1.1 bouyer fis = cmd_tbl->cmdt_cfis;
635 1.1 bouyer
636 1.1 bouyer fis[0] = 0x27; /* host to device */
637 1.1 bouyer fis[1] = 0x80; /* command FIS */
638 1.1 bouyer fis[2] = ata_c->r_command;
639 1.1 bouyer fis[3] = ata_c->r_features;
640 1.1 bouyer fis[4] = ata_c->r_sector;
641 1.1 bouyer fis[5] = ata_c->r_cyl & 0xff;
642 1.1 bouyer fis[6] = (ata_c->r_cyl >> 8) & 0xff;
643 1.1 bouyer fis[7] = ata_c->r_head & 0x0f;
644 1.1 bouyer fis[8] = 0;
645 1.1 bouyer fis[9] = 0;
646 1.1 bouyer fis[10] = 0;
647 1.1 bouyer fis[11] = 0;
648 1.1 bouyer fis[12] = ata_c->r_count;
649 1.1 bouyer fis[13] = 0;
650 1.1 bouyer fis[14] = 0;
651 1.1 bouyer fis[15] = WDCTL_4BIT;
652 1.1 bouyer fis[16] = 0;
653 1.1 bouyer fis[17] = 0;
654 1.1 bouyer fis[18] = 0;
655 1.1 bouyer fis[19] = 0;
656 1.1 bouyer
657 1.1 bouyer cmd_h = &achp->ahcic_cmdh[slot];
658 1.1 bouyer AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
659 1.1 bouyer chp->ch_channel, cmd_h), DEBUG_XFERS);
660 1.1 bouyer if (ahci_dma_setup(chp, slot,
661 1.1 bouyer (ata_c->flags & (AT_READ|AT_WRITE)) ? ata_c->data : NULL,
662 1.1 bouyer ata_c->bcount,
663 1.1 bouyer (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
664 1.1 bouyer ata_c->flags |= AT_DF;
665 1.1 bouyer ahci_cmd_complete(chp, xfer, slot);
666 1.1 bouyer return;
667 1.1 bouyer }
668 1.1 bouyer cmd_h->cmdh_flags = htole16(
669 1.1 bouyer ((ata_c->flags & AT_WRITE) ? AHCI_CMDH_F_WR : 0) |
670 1.1 bouyer 20 /* fis lenght */ / 4);
671 1.1 bouyer cmd_h->cmdh_prdbc = 0;
672 1.1 bouyer AHCI_CMDH_SYNC(sc, achp, slot,
673 1.1 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
674 1.1 bouyer
675 1.1 bouyer if (ata_c->flags & AT_POLL) {
676 1.1 bouyer /* polled command, disable interrupts */
677 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC,
678 1.1 bouyer AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
679 1.1 bouyer }
680 1.1 bouyer chp->ch_flags |= ATACH_IRQ_WAIT;
681 1.5 bouyer chp->ch_status = 0;
682 1.1 bouyer /* start command */
683 1.1 bouyer AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
684 1.1 bouyer /* and says we started this command */
685 1.1 bouyer achp->ahcic_cmds_active |= 1 << slot;
686 1.1 bouyer
687 1.1 bouyer if ((ata_c->flags & AT_POLL) == 0) {
688 1.1 bouyer chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
689 1.1 bouyer callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
690 1.1 bouyer ahci_timeout, chp);
691 1.1 bouyer return;
692 1.1 bouyer }
693 1.1 bouyer /*
694 1.1 bouyer * Polled command.
695 1.1 bouyer */
696 1.1 bouyer for (i = 0; i < ata_c->timeout / 10; i++) {
697 1.1 bouyer if (ata_c->flags & AT_DONE)
698 1.1 bouyer break;
699 1.1 bouyer ahci_intr_port(sc, achp);
700 1.1 bouyer if (ata_c->flags & AT_WAIT)
701 1.1 bouyer tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
702 1.1 bouyer else
703 1.1 bouyer delay(10000);
704 1.1 bouyer }
705 1.1 bouyer 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,
706 1.1 bouyer AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
707 1.1 bouyer AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
708 1.1 bouyer AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
709 1.1 bouyer AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
710 1.1 bouyer DEBUG_XFERS);
711 1.1 bouyer if ((ata_c->flags & AT_DONE) == 0) {
712 1.1 bouyer ata_c->flags |= AT_TIMEOU;
713 1.1 bouyer ahci_cmd_complete(chp, xfer, slot);
714 1.1 bouyer }
715 1.1 bouyer /* reenable interrupts */
716 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
717 1.1 bouyer }
718 1.1 bouyer
719 1.1 bouyer void
720 1.1 bouyer ahci_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
721 1.1 bouyer {
722 1.1 bouyer struct ata_command *ata_c = xfer->c_cmd;
723 1.1 bouyer AHCIDEBUG_PRINT(("ahci_cmd_kill_xfer channel %d\n", chp->ch_channel),
724 1.1 bouyer DEBUG_FUNCS);
725 1.1 bouyer
726 1.1 bouyer switch (reason) {
727 1.1 bouyer case KILL_GONE:
728 1.1 bouyer ata_c->flags |= AT_GONE;
729 1.1 bouyer break;
730 1.1 bouyer case KILL_RESET:
731 1.1 bouyer ata_c->flags |= AT_RESET;
732 1.1 bouyer break;
733 1.1 bouyer default:
734 1.1 bouyer printf("ahci_cmd_kill_xfer: unknown reason %d\n", reason);
735 1.1 bouyer panic("ahci_cmd_kill_xfer");
736 1.1 bouyer }
737 1.1 bouyer ahci_cmd_done(chp, xfer, 0 /* XXX slot */);
738 1.1 bouyer }
739 1.1 bouyer
740 1.1 bouyer int
741 1.1 bouyer ahci_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
742 1.1 bouyer {
743 1.1 bouyer int slot = 0; /* XXX slot */
744 1.1 bouyer struct ata_command *ata_c = xfer->c_cmd;
745 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
746 1.1 bouyer
747 1.1 bouyer AHCIDEBUG_PRINT(("ahci_cmd_complete channel %d CMD 0x%x CI 0x%x\n",
748 1.1 bouyer chp->ch_channel, AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)),
749 1.1 bouyer AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
750 1.1 bouyer DEBUG_FUNCS);
751 1.1 bouyer chp->ch_flags &= ~ATACH_IRQ_WAIT;
752 1.1 bouyer if (xfer->c_flags & C_TIMEOU) {
753 1.1 bouyer ata_c->flags |= AT_TIMEOU;
754 1.1 bouyer } else
755 1.1 bouyer callout_stop(&chp->ch_callout);
756 1.1 bouyer
757 1.1 bouyer chp->ch_queue->active_xfer = NULL;
758 1.1 bouyer
759 1.1 bouyer if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
760 1.1 bouyer ahci_cmd_kill_xfer(chp, xfer, KILL_GONE);
761 1.1 bouyer chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
762 1.1 bouyer wakeup(&chp->ch_queue->active_xfer);
763 1.1 bouyer return 0;
764 1.1 bouyer }
765 1.1 bouyer if (is) {
766 1.1 bouyer ata_c->r_head = 0;
767 1.1 bouyer ata_c->r_count = 0;
768 1.1 bouyer ata_c->r_sector = 0;
769 1.1 bouyer ata_c->r_cyl = 0;
770 1.1 bouyer if (chp->ch_status & WDCS_BSY) {
771 1.1 bouyer ata_c->flags |= AT_TIMEOU;
772 1.1 bouyer } else if (chp->ch_status & WDCS_ERR) {
773 1.1 bouyer ata_c->r_error = chp->ch_error;
774 1.1 bouyer ata_c->flags |= AT_ERROR;
775 1.1 bouyer }
776 1.1 bouyer }
777 1.1 bouyer ahci_cmd_done(chp, xfer, slot);
778 1.1 bouyer return 0;
779 1.1 bouyer }
780 1.1 bouyer
781 1.1 bouyer void
782 1.1 bouyer ahci_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
783 1.1 bouyer {
784 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
785 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
786 1.1 bouyer struct ata_command *ata_c = xfer->c_cmd;
787 1.1 bouyer
788 1.1 bouyer AHCIDEBUG_PRINT(("ahci_cmd_done channel %d\n", chp->ch_channel),
789 1.1 bouyer DEBUG_FUNCS);
790 1.1 bouyer
791 1.1 bouyer /* this comamnd is not active any more */
792 1.1 bouyer achp->ahcic_cmds_active &= ~(1 << slot);
793 1.1 bouyer
794 1.1 bouyer if (ata_c->flags & (AT_READ|AT_WRITE)) {
795 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
796 1.1 bouyer achp->ahcic_datad[slot]->dm_mapsize,
797 1.1 bouyer (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
798 1.1 bouyer BUS_DMASYNC_POSTWRITE);
799 1.1 bouyer bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
800 1.1 bouyer }
801 1.1 bouyer
802 1.2 fvdl AHCI_CMDH_SYNC(sc, achp, slot,
803 1.2 fvdl BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
804 1.2 fvdl
805 1.1 bouyer ata_c->flags |= AT_DONE;
806 1.1 bouyer if (achp->ahcic_cmdh[slot].cmdh_prdbc)
807 1.1 bouyer ata_c->flags |= AT_XFDONE;
808 1.1 bouyer
809 1.1 bouyer ata_free_xfer(chp, xfer);
810 1.1 bouyer if (ata_c->flags & AT_WAIT)
811 1.1 bouyer wakeup(ata_c);
812 1.1 bouyer else if (ata_c->callback)
813 1.1 bouyer ata_c->callback(ata_c->callback_arg);
814 1.1 bouyer atastart(chp);
815 1.1 bouyer return;
816 1.1 bouyer }
817 1.1 bouyer
818 1.1 bouyer int
819 1.1 bouyer ahci_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
820 1.1 bouyer {
821 1.1 bouyer struct ata_channel *chp = drvp->chnl_softc;
822 1.1 bouyer struct ata_xfer *xfer;
823 1.1 bouyer
824 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
825 1.1 bouyer AHCIDEBUG_PRINT(("ahci_ata_bio port %d CI 0x%x\n",
826 1.1 bouyer chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
827 1.1 bouyer DEBUG_XFERS);
828 1.1 bouyer xfer = ata_get_xfer(ATAXF_NOSLEEP);
829 1.1 bouyer if (xfer == NULL) {
830 1.1 bouyer return ATACMD_TRY_AGAIN;
831 1.1 bouyer }
832 1.1 bouyer if (ata_bio->flags & ATA_POLL)
833 1.1 bouyer xfer->c_flags |= C_POLL;
834 1.1 bouyer xfer->c_drive = drvp->drive;
835 1.1 bouyer xfer->c_cmd = ata_bio;
836 1.1 bouyer xfer->c_databuf = ata_bio->databuf;
837 1.1 bouyer xfer->c_bcount = ata_bio->bcount;
838 1.1 bouyer xfer->c_start = ahci_bio_start;
839 1.1 bouyer xfer->c_intr = ahci_bio_complete;
840 1.1 bouyer xfer->c_kill_xfer = ahci_bio_kill_xfer;
841 1.1 bouyer ata_exec_xfer(chp, xfer);
842 1.1 bouyer return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED;
843 1.1 bouyer }
844 1.1 bouyer
845 1.1 bouyer void
846 1.1 bouyer ahci_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
847 1.1 bouyer {
848 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
849 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
850 1.1 bouyer struct ata_bio *ata_bio = xfer->c_cmd;
851 1.1 bouyer int slot = 0 /* XXX slot */;
852 1.1 bouyer struct ahci_cmd_tbl *cmd_tbl;
853 1.1 bouyer struct ahci_cmd_header *cmd_h;
854 1.1 bouyer u_int8_t *fis;
855 1.1 bouyer int i, nblks;
856 1.1 bouyer int channel = chp->ch_channel;
857 1.1 bouyer
858 1.1 bouyer AHCIDEBUG_PRINT(("ahci_bio_start CI 0x%x\n",
859 1.1 bouyer AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
860 1.1 bouyer
861 1.1 bouyer nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
862 1.1 bouyer
863 1.1 bouyer cmd_tbl = achp->ahcic_cmd_tbl[slot];
864 1.1 bouyer AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
865 1.1 bouyer cmd_tbl), DEBUG_XFERS);
866 1.1 bouyer fis = cmd_tbl->cmdt_cfis;
867 1.1 bouyer
868 1.1 bouyer fis[0] = 0x27; /* host to device */
869 1.1 bouyer fis[1] = 0x80; /* command FIS */
870 1.1 bouyer if (ata_bio->flags & ATA_LBA48) {
871 1.1 bouyer fis[2] = (ata_bio->flags & ATA_READ) ?
872 1.1 bouyer WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
873 1.1 bouyer } else {
874 1.1 bouyer fis[2] =
875 1.1 bouyer (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA;
876 1.1 bouyer }
877 1.1 bouyer fis[3] = 0; /* features */
878 1.1 bouyer fis[4] = ata_bio->blkno & 0xff;
879 1.1 bouyer fis[5] = (ata_bio->blkno >> 8) & 0xff;
880 1.1 bouyer fis[6] = (ata_bio->blkno >> 16) & 0xff;
881 1.1 bouyer if (ata_bio->flags & ATA_LBA48) {
882 1.1 bouyer fis[7] = WDSD_LBA;
883 1.1 bouyer fis[8] = (ata_bio->blkno >> 24) & 0xff;
884 1.1 bouyer fis[9] = (ata_bio->blkno >> 32) & 0xff;
885 1.1 bouyer fis[10] = (ata_bio->blkno >> 40) & 0xff;
886 1.1 bouyer } else {
887 1.1 bouyer fis[7] = ((ata_bio->blkno >> 24) & 0x0f) | WDSD_LBA;
888 1.1 bouyer fis[8] = 0;
889 1.1 bouyer fis[9] = 0;
890 1.1 bouyer fis[10] = 0;
891 1.1 bouyer }
892 1.1 bouyer fis[11] = 0; /* ext features */
893 1.1 bouyer fis[12] = nblks & 0xff;
894 1.1 bouyer fis[13] = (ata_bio->flags & ATA_LBA48) ?
895 1.1 bouyer ((nblks >> 8) & 0xff) : 0;
896 1.1 bouyer fis[14] = 0;
897 1.1 bouyer fis[15] = WDCTL_4BIT;
898 1.1 bouyer fis[16] = 0;
899 1.1 bouyer fis[17] = 0;
900 1.1 bouyer fis[18] = 0;
901 1.1 bouyer fis[19] = 0;
902 1.1 bouyer
903 1.1 bouyer cmd_h = &achp->ahcic_cmdh[slot];
904 1.1 bouyer AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
905 1.1 bouyer chp->ch_channel, cmd_h), DEBUG_XFERS);
906 1.1 bouyer if (ahci_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
907 1.1 bouyer (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
908 1.1 bouyer ata_bio->error = ERR_DMA;
909 1.1 bouyer ata_bio->r_error = 0;
910 1.1 bouyer ahci_bio_complete(chp, xfer, slot);
911 1.1 bouyer return;
912 1.1 bouyer }
913 1.1 bouyer cmd_h->cmdh_flags = htole16(
914 1.1 bouyer ((ata_bio->flags & ATA_READ) ? 0 : AHCI_CMDH_F_WR) |
915 1.1 bouyer 20 /* fis lenght */ / 4);
916 1.1 bouyer cmd_h->cmdh_prdbc = 0;
917 1.2 fvdl AHCI_CMDH_SYNC(sc, achp, slot,
918 1.2 fvdl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
919 1.1 bouyer
920 1.1 bouyer if (xfer->c_flags & C_POLL) {
921 1.1 bouyer /* polled command, disable interrupts */
922 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC,
923 1.1 bouyer AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
924 1.1 bouyer }
925 1.1 bouyer chp->ch_flags |= ATACH_IRQ_WAIT;
926 1.5 bouyer chp->ch_status = 0;
927 1.1 bouyer /* start command */
928 1.1 bouyer AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
929 1.1 bouyer /* and says we started this command */
930 1.1 bouyer achp->ahcic_cmds_active |= 1 << slot;
931 1.1 bouyer
932 1.1 bouyer if ((xfer->c_flags & C_POLL) == 0) {
933 1.1 bouyer chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
934 1.1 bouyer callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
935 1.1 bouyer ahci_timeout, chp);
936 1.1 bouyer return;
937 1.1 bouyer }
938 1.1 bouyer /*
939 1.1 bouyer * Polled command.
940 1.1 bouyer */
941 1.1 bouyer for (i = 0; i < ATA_DELAY / 10; i++) {
942 1.1 bouyer if (ata_bio->flags & ATA_ITSDONE)
943 1.1 bouyer break;
944 1.1 bouyer ahci_intr_port(sc, achp);
945 1.1 bouyer if (ata_bio->flags & ATA_NOSLEEP)
946 1.1 bouyer delay(10000);
947 1.1 bouyer else
948 1.1 bouyer tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
949 1.1 bouyer }
950 1.1 bouyer 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,
951 1.1 bouyer AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
952 1.1 bouyer AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
953 1.1 bouyer AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
954 1.1 bouyer AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
955 1.1 bouyer DEBUG_XFERS);
956 1.1 bouyer if ((ata_bio->flags & ATA_ITSDONE) == 0) {
957 1.1 bouyer ata_bio->error = TIMEOUT;
958 1.1 bouyer ahci_bio_complete(chp, xfer, slot);
959 1.1 bouyer }
960 1.1 bouyer /* reenable interrupts */
961 1.1 bouyer AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
962 1.1 bouyer }
963 1.1 bouyer
964 1.1 bouyer void
965 1.1 bouyer ahci_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
966 1.1 bouyer {
967 1.1 bouyer int slot = 0; /* XXX slot */
968 1.1 bouyer int drive = xfer->c_drive;
969 1.1 bouyer struct ata_bio *ata_bio = xfer->c_cmd;
970 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
971 1.1 bouyer AHCIDEBUG_PRINT(("ahci_bio_kill_xfer channel %d\n", chp->ch_channel),
972 1.1 bouyer DEBUG_FUNCS);
973 1.1 bouyer
974 1.1 bouyer achp->ahcic_cmds_active &= ~(1 << slot);
975 1.1 bouyer ata_free_xfer(chp, xfer);
976 1.1 bouyer ata_bio->flags |= ATA_ITSDONE;
977 1.1 bouyer switch (reason) {
978 1.1 bouyer case KILL_GONE:
979 1.1 bouyer ata_bio->error = ERR_NODEV;
980 1.1 bouyer break;
981 1.1 bouyer case KILL_RESET:
982 1.1 bouyer ata_bio->error = ERR_RESET;
983 1.1 bouyer break;
984 1.1 bouyer default:
985 1.1 bouyer printf("ahci_bio_kill_xfer: unknown reason %d\n", reason);
986 1.1 bouyer panic("ahci_bio_kill_xfer");
987 1.1 bouyer }
988 1.1 bouyer ata_bio->r_error = WDCE_ABRT;
989 1.1 bouyer (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
990 1.1 bouyer }
991 1.1 bouyer
992 1.1 bouyer int
993 1.1 bouyer ahci_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
994 1.1 bouyer {
995 1.1 bouyer int slot = 0; /* XXX slot */
996 1.1 bouyer struct ata_bio *ata_bio = xfer->c_cmd;
997 1.1 bouyer int drive = xfer->c_drive;
998 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
999 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1000 1.1 bouyer
1001 1.1 bouyer AHCIDEBUG_PRINT(("ahci_bio_complete channel %d\n", chp->ch_channel),
1002 1.1 bouyer DEBUG_FUNCS);
1003 1.1 bouyer
1004 1.1 bouyer achp->ahcic_cmds_active &= ~(1 << slot);
1005 1.1 bouyer chp->ch_flags &= ~ATACH_IRQ_WAIT;
1006 1.5 bouyer if (xfer->c_flags & C_TIMEOU) {
1007 1.5 bouyer ata_bio->error = TIMEOUT;
1008 1.5 bouyer } else {
1009 1.5 bouyer callout_stop(&chp->ch_callout);
1010 1.5 bouyer ata_bio->error = 0;
1011 1.5 bouyer }
1012 1.1 bouyer
1013 1.1 bouyer chp->ch_queue->active_xfer = NULL;
1014 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1015 1.1 bouyer achp->ahcic_datad[slot]->dm_mapsize,
1016 1.1 bouyer (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1017 1.1 bouyer BUS_DMASYNC_POSTWRITE);
1018 1.1 bouyer bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
1019 1.1 bouyer
1020 1.1 bouyer if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1021 1.1 bouyer ahci_bio_kill_xfer(chp, xfer, KILL_GONE);
1022 1.1 bouyer chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1023 1.1 bouyer wakeup(&chp->ch_queue->active_xfer);
1024 1.1 bouyer return 0;
1025 1.1 bouyer }
1026 1.1 bouyer ata_free_xfer(chp, xfer);
1027 1.1 bouyer ata_bio->flags |= ATA_ITSDONE;
1028 1.1 bouyer if (chp->ch_status & WDCS_DWF) {
1029 1.1 bouyer ata_bio->error = ERR_DF;
1030 1.1 bouyer } else if (chp->ch_status & WDCS_ERR) {
1031 1.1 bouyer ata_bio->error = ERROR;
1032 1.1 bouyer ata_bio->r_error = chp->ch_error;
1033 1.1 bouyer } else if (chp->ch_status & WDCS_CORR)
1034 1.1 bouyer ata_bio->flags |= ATA_CORR;
1035 1.1 bouyer
1036 1.1 bouyer AHCI_CMDH_SYNC(sc, achp, slot,
1037 1.1 bouyer BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1038 1.1 bouyer AHCIDEBUG_PRINT(("ahci_bio_complete bcount %ld",
1039 1.1 bouyer ata_bio->bcount), DEBUG_XFERS);
1040 1.1 bouyer ata_bio->bcount -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc);
1041 1.1 bouyer AHCIDEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1042 1.1 bouyer (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1043 1.1 bouyer atastart(chp);
1044 1.1 bouyer return 0;
1045 1.1 bouyer }
1046 1.1 bouyer
1047 1.1 bouyer void
1048 1.5 bouyer ahci_channel_stop(struct ahci_softc *sc, struct ata_channel *chp, int flags)
1049 1.5 bouyer {
1050 1.5 bouyer int i;
1051 1.5 bouyer /* stop channel */
1052 1.5 bouyer AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1053 1.5 bouyer AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & ~AHCI_P_CMD_ST);
1054 1.5 bouyer /* wait 1s for channel to stop */
1055 1.5 bouyer for (i = 0; i <100; i++) {
1056 1.5 bouyer if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
1057 1.5 bouyer == 0)
1058 1.5 bouyer break;
1059 1.5 bouyer if (flags & AT_WAIT)
1060 1.5 bouyer tsleep(&sc, PRIBIO, "ahcirst", mstohz(10));
1061 1.5 bouyer else
1062 1.5 bouyer delay(10000);
1063 1.5 bouyer }
1064 1.5 bouyer if (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) {
1065 1.5 bouyer printf("%s: channel wouldn't stop\n", AHCINAME(sc));
1066 1.5 bouyer /* XXX controller reset ? */
1067 1.5 bouyer return;
1068 1.5 bouyer }
1069 1.5 bouyer }
1070 1.5 bouyer
1071 1.5 bouyer void
1072 1.1 bouyer ahci_channel_start(struct ahci_softc *sc, struct ata_channel *chp)
1073 1.1 bouyer {
1074 1.1 bouyer /* clear error */
1075 1.1 bouyer AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel), 0);
1076 1.1 bouyer
1077 1.1 bouyer /* and start controller */
1078 1.1 bouyer AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1079 1.1 bouyer AHCI_P_CMD_ICC_AC | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
1080 1.1 bouyer AHCI_P_CMD_FRE | AHCI_P_CMD_ST);
1081 1.1 bouyer }
1082 1.1 bouyer
1083 1.1 bouyer void
1084 1.1 bouyer ahci_timeout(void *v)
1085 1.1 bouyer {
1086 1.1 bouyer struct ata_channel *chp = (struct ata_channel *)v;
1087 1.1 bouyer struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1088 1.1 bouyer int s = splbio();
1089 1.1 bouyer AHCIDEBUG_PRINT(("ahci_timeout xfer %p\n", xfer), DEBUG_INTR);
1090 1.1 bouyer if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1091 1.1 bouyer xfer->c_flags |= C_TIMEOU;
1092 1.1 bouyer xfer->c_intr(chp, xfer, 0);
1093 1.1 bouyer }
1094 1.1 bouyer splx(s);
1095 1.1 bouyer }
1096 1.1 bouyer
1097 1.1 bouyer int
1098 1.1 bouyer ahci_dma_setup(struct ata_channel *chp, int slot, void *data,
1099 1.1 bouyer size_t count, int op)
1100 1.1 bouyer {
1101 1.1 bouyer int error, seg;
1102 1.1 bouyer struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1103 1.1 bouyer struct ahci_channel *achp = (struct ahci_channel *)chp;
1104 1.1 bouyer struct ahci_cmd_tbl *cmd_tbl;
1105 1.1 bouyer struct ahci_cmd_header *cmd_h;
1106 1.1 bouyer
1107 1.1 bouyer cmd_h = &achp->ahcic_cmdh[slot];
1108 1.1 bouyer cmd_tbl = achp->ahcic_cmd_tbl[slot];
1109 1.1 bouyer
1110 1.1 bouyer if (data == NULL) {
1111 1.1 bouyer cmd_h->cmdh_prdtl = 0;
1112 1.1 bouyer goto end;
1113 1.1 bouyer }
1114 1.1 bouyer
1115 1.1 bouyer error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_datad[slot],
1116 1.1 bouyer data, count, NULL,
1117 1.1 bouyer BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1118 1.1 bouyer if (error) {
1119 1.1 bouyer printf("%s port %d: failed to load xfer: %d\n",
1120 1.1 bouyer AHCINAME(sc), chp->ch_channel, error);
1121 1.1 bouyer return error;
1122 1.1 bouyer }
1123 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1124 1.1 bouyer achp->ahcic_datad[slot]->dm_mapsize,
1125 1.1 bouyer (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1126 1.1 bouyer for (seg = 0; seg < achp->ahcic_datad[slot]->dm_nsegs; seg++) {
1127 1.1 bouyer cmd_tbl->cmdt_prd[seg].prd_dba = htole32(
1128 1.1 bouyer achp->ahcic_datad[slot]->dm_segs[seg].ds_addr);
1129 1.1 bouyer cmd_tbl->cmdt_prd[seg].prd_dbau = 0;
1130 1.1 bouyer cmd_tbl->cmdt_prd[seg].prd_dbc = htole32(
1131 1.1 bouyer achp->ahcic_datad[slot]->dm_segs[seg].ds_len - 1);
1132 1.1 bouyer }
1133 1.1 bouyer cmd_tbl->cmdt_prd[seg - 1].prd_dbc |= htole32(AHCI_PRD_DBC_IPC);
1134 1.1 bouyer cmd_h->cmdh_prdtl = htole16(achp->ahcic_datad[slot]->dm_nsegs);
1135 1.1 bouyer end:
1136 1.1 bouyer AHCI_CMDTBL_SYNC(sc, achp, slot, BUS_DMASYNC_PREWRITE);
1137 1.1 bouyer return 0;
1138 1.1 bouyer }
1139