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