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