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