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