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