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