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