Home | History | Annotate | Line # | Download | only in ic
siisata.c revision 1.4
      1 /* $NetBSD: siisata.c,v 1.4 2009/06/17 19:12:48 cegger Exp $ */
      2 
      3 /* from ahcisata_core.c */
      4 
      5 /*
      6  * Copyright (c) 2006 Manuel Bouyer.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Manuel Bouyer.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /* from atapi_wdc.c */
     36 
     37 /*
     38  * Copyright (c) 1998, 2001 Manuel Bouyer.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by Manuel Bouyer.
     51  * 4. The name of the author may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*-
     67  * Copyright (c) 2007, 2008 Jonathan A. Kollasch.
     68  * All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  *
     79  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     80  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     81  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     82  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     83  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     84  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     85  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     86  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     87  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     88  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     89  *
     90  */
     91 
     92 #include <sys/types.h>
     93 #include <sys/malloc.h>
     94 #include <sys/param.h>
     95 #include <sys/kernel.h>
     96 #include <sys/systm.h>
     97 #include <sys/syslog.h>
     98 #include <sys/disklabel.h>
     99 #include <sys/buf.h>
    100 
    101 #include <uvm/uvm_extern.h>
    102 
    103 #include <dev/ata/atareg.h>
    104 #include <dev/ata/satavar.h>
    105 #include <dev/ata/satareg.h>
    106 #include <dev/ata/satafisvar.h>
    107 #include <dev/ic/siisatavar.h>
    108 #include <dev/ic/wdcreg.h>
    109 
    110 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
    111 
    112 #include "atapibus.h"
    113 
    114 #ifdef SIISATA_DEBUG
    115 #if 0
    116 int siisata_debug_mask = 0xffff;
    117 #else
    118 int siisata_debug_mask = 0;
    119 #endif
    120 #endif
    121 
    122 #define ATA_DELAY 10000		/* 10s for a drive I/O */
    123 
    124 static void siisata_attach_port(struct siisata_softc *, int);
    125 static void siisata_intr_port(struct siisata_channel *);
    126 
    127 void siisata_probe_drive(struct ata_channel *);
    128 void siisata_setup_channel(struct ata_channel *);
    129 
    130 int siisata_ata_bio(struct ata_drive_datas *, struct ata_bio *);
    131 void siisata_reset_drive(struct ata_drive_datas *, int);
    132 void siisata_reset_channel(struct ata_channel *, int);
    133 int siisata_ata_addref(struct ata_drive_datas *);
    134 void siisata_ata_delref(struct ata_drive_datas *);
    135 void siisata_killpending(struct ata_drive_datas *);
    136 
    137 void siisata_cmd_start(struct ata_channel *, struct ata_xfer *);
    138 int siisata_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
    139 void siisata_cmd_done(struct ata_channel *, struct ata_xfer *, int);
    140 void siisata_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
    141 
    142 void siisata_bio_start(struct ata_channel *, struct ata_xfer *);
    143 int siisata_bio_complete(struct ata_channel *, struct ata_xfer *, int);
    144 void siisata_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
    145 int siisata_exec_command(struct ata_drive_datas *, struct ata_command *);
    146 
    147 void siisata_timeout(void *);
    148 
    149 static void siisata_reinit_port(struct ata_channel *);
    150 static void siisata_device_reset(struct ata_channel *);
    151 static void siisata_activate_prb(struct siisata_channel *, int);
    152 static void siisata_deactivate_prb(struct siisata_channel *, int);
    153 static int siisata_dma_setup(struct ata_channel *chp, int slot,
    154     void *data, size_t, int);
    155 
    156 #if NATAPIBUS > 0
    157 void siisata_atapibus_attach(struct atabus_softc *);
    158 void siisata_atapi_probe_device(struct atapibus_softc *, int);
    159 void siisata_atapi_minphys(struct buf *);
    160 void siisata_atapi_start(struct ata_channel *,struct ata_xfer *);
    161 int siisata_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
    162 void siisata_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
    163 void siisata_atapi_done(struct ata_channel *, struct ata_xfer *, int);
    164 void siisata_atapi_scsipi_request(struct scsipi_channel *,
    165     scsipi_adapter_req_t, void *);
    166 void siisata_atapi_kill_pending(struct scsipi_periph *);
    167 #endif /* NATAPIBUS */
    168 
    169 const struct ata_bustype siisata_ata_bustype = {
    170 	SCSIPI_BUSTYPE_ATA,
    171 	siisata_ata_bio,
    172 	siisata_reset_drive,
    173 	siisata_reset_channel,
    174 	siisata_exec_command,
    175 	ata_get_params,
    176 	siisata_ata_addref,
    177 	siisata_ata_delref,
    178 	siisata_killpending
    179 };
    180 
    181 #if NATAPIBUS > 0
    182 static const struct scsipi_bustype siisata_atapi_bustype = {
    183 	SCSIPI_BUSTYPE_ATAPI,
    184 	atapi_scsipi_cmd,
    185 	atapi_interpret_sense,
    186 	atapi_print_addr,
    187 	siisata_atapi_kill_pending
    188 };
    189 #endif /* NATAPIBUS */
    190 
    191 
    192 void
    193 siisata_attach(struct siisata_softc *sc)
    194 {
    195 	int i;
    196 
    197 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
    198 	    SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)), DEBUG_FUNCS);
    199 
    200 	sc->sc_atac.atac_cap = ATAC_CAP_DMA | ATAC_CAP_UDMA;
    201 	sc->sc_atac.atac_pio_cap = 4;
    202 	sc->sc_atac.atac_dma_cap = 2;
    203 	sc->sc_atac.atac_udma_cap = 6;
    204 	sc->sc_atac.atac_channels = sc->sc_chanarray;
    205 	sc->sc_atac.atac_probe = siisata_probe_drive;
    206 	sc->sc_atac.atac_bustype_ata = &siisata_ata_bustype;
    207 	sc->sc_atac.atac_set_modes = siisata_setup_channel;
    208 #if NATAPIBUS > 0
    209 	sc->sc_atac.atac_atapibus_attach = siisata_atapibus_attach;
    210 #endif
    211 
    212 	/* come out of reset state */
    213 	GRWRITE(sc, GR_GC, 0);
    214 
    215 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
    216 		siisata_attach_port(sc, i);
    217 	}
    218 
    219 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
    220 	    SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)),
    221 	    DEBUG_FUNCS);
    222 	return;
    223 }
    224 
    225 static void
    226 siisata_init_port(struct siisata_softc *sc, int port)
    227 {
    228 	struct siisata_channel *schp;
    229 	struct ata_channel *chp;
    230 
    231 	schp = &sc->sc_channels[port];
    232 	chp = (struct ata_channel *)schp;
    233 
    234 	/* come out of reset, 64-bit activation */
    235 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCC),
    236 	    PR_PC_32BA | PR_PC_PORT_RESET);
    237 	/* initialize port */
    238 	siisata_reinit_port(chp);
    239 	/* clear any interrupts */
    240 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
    241 	/* enable CmdErrr+CmdCmpl interrupting */
    242 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIES),
    243 	    PR_PIS_CMDERRR | PR_PIS_CMDCMPL);
    244 	/* enable port interrupt */
    245 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
    246 }
    247 
    248 static void
    249 siisata_attach_port(struct siisata_softc *sc, int port)
    250 {
    251 	int j;
    252 	bus_dma_segment_t seg;
    253 	int dmasize;
    254 	int error;
    255 	int rseg;
    256 	void *prbp;
    257 	struct siisata_channel *schp;
    258 	struct ata_channel *chp;
    259 
    260 	schp = &sc->sc_channels[port];
    261 	chp = (struct ata_channel *)schp;
    262 	sc->sc_chanarray[port] = chp;
    263 	chp->ch_channel = port;
    264 	chp->ch_atac = &sc->sc_atac;
    265 	chp->ch_queue = malloc(sizeof(struct ata_queue),
    266 			       M_DEVBUF, M_NOWAIT);
    267 	if (chp->ch_queue == NULL) {
    268 		aprint_error_dev(sc->sc_atac.atac_dev,
    269 		    "port %d: can't allocate memory "
    270 		    "for command queue\n", chp->ch_channel);
    271 		return;
    272 	}
    273 
    274 	dmasize = SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS;
    275 
    276 	SIISATA_DEBUG_PRINT(("%s: %s: dmasize: %d\n", SIISATANAME(sc),
    277 	    __func__, dmasize), DEBUG_FUNCS);
    278 
    279 	error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
    280 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
    281 	if (error) {
    282 		aprint_error_dev(sc->sc_atac.atac_dev,
    283 		    "unable to allocate PRB table memory, "
    284 		    "error=%d\n", error);
    285 		return;
    286 	}
    287 
    288 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, dmasize,
    289 	    &prbp, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    290 	if (error) {
    291 		aprint_error_dev(sc->sc_atac.atac_dev,
    292 		    "unable to map PRB table memory, "
    293 		    "error=%d\n", error);
    294 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    295 		return;
    296 	}
    297 
    298 	error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
    299 	    BUS_DMA_NOWAIT, &schp->sch_prbd);
    300 	if (error) {
    301 		aprint_error_dev(sc->sc_atac.atac_dev,
    302 		    "unable to create PRB table map, "
    303 		    "error=%d\n", error);
    304 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
    305 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    306 		return;
    307 	}
    308 
    309 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_prbd,
    310 	    prbp, dmasize, NULL, BUS_DMA_NOWAIT);
    311 	if (error) {
    312 		aprint_error_dev(sc->sc_atac.atac_dev,
    313 		    "unable to load PRB table map, "
    314 		    "error=%d\n", error);
    315 		bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
    316 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
    317 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    318 		return;
    319 	}
    320 
    321 	for (j = 0; j < SIISATA_MAX_SLOTS; j++) {
    322 		schp->sch_prb[j] = (struct siisata_prb *)
    323 		    ((char *)prbp + SIISATA_CMD_SIZE * j);
    324 		schp->sch_bus_prb[j] =
    325 		    schp->sch_prbd->dm_segs[0].ds_addr +
    326 		    SIISATA_CMD_SIZE * j;
    327 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
    328 		    SIISATA_NSGE, MAXPHYS, 0,
    329 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    330 		    &schp->sch_datad[j]);
    331 		if (error) {
    332 			aprint_error_dev(sc->sc_atac.atac_dev,
    333 			    "couldn't create xfer DMA map, error=%d\n",
    334 			    error);
    335 			return;
    336 		}
    337 	}
    338 
    339 	chp->ch_ndrive = 1;
    340 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
    341 	    PRX(chp->ch_channel, PRO_SSTATUS), 4, &schp->sch_sstatus) != 0) {
    342 		aprint_error_dev(sc->sc_atac.atac_dev,
    343 		    "couldn't map port %d SStatus regs\n",
    344 		    chp->ch_channel);
    345 		return;
    346 	}
    347 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
    348 	    PRX(chp->ch_channel, PRO_SCONTROL), 4, &schp->sch_scontrol) != 0) {
    349 		aprint_error_dev(sc->sc_atac.atac_dev,
    350 		    "couldn't map port %d SControl regs\n",
    351 		    chp->ch_channel);
    352 		return;
    353 	}
    354 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
    355 	    PRX(chp->ch_channel, PRO_SERROR), 4, &schp->sch_serror) != 0) {
    356 		aprint_error_dev(sc->sc_atac.atac_dev,
    357 		    "couldn't map port %d SError regs\n",
    358 		    chp->ch_channel);
    359 		return;
    360 	}
    361 
    362 	siisata_init_port(sc, port);
    363 
    364 	ata_channel_attach(chp);
    365 
    366 	return;
    367 }
    368 
    369 int
    370 siisata_detach(struct siisata_softc *sc, int flags)
    371 {
    372 	struct atac_softc *atac = &sc->sc_atac;
    373 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
    374 	struct siisata_channel *schp;
    375 	struct ata_channel *chp;
    376 	bus_dmamap_t dmam;
    377 	int i, j, error;
    378 
    379 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
    380 		schp = &sc->sc_channels[i];
    381 		chp = sc->sc_chanarray[i];
    382 
    383 		if (chp->atabus == NULL)
    384 			continue;
    385 		if ((error = config_detach(chp->atabus, flags)) != 0)
    386 			return error;
    387 
    388 		for (j = 0; j < SIISATA_MAX_SLOTS; j++)
    389 			bus_dmamap_destroy(sc->sc_dmat, schp->sch_datad[j]);
    390 
    391 		dmam = schp->sch_prbd;
    392 		bus_dmamap_unload(sc->sc_dmat, dmam);
    393 		bus_dmamap_destroy(sc->sc_dmat, dmam);
    394 		bus_dmamem_unmap(sc->sc_dmat, schp->sch_prb[0],
    395 		    dmam->dm_mapsize);
    396 		bus_dmamem_free(sc->sc_dmat, dmam->dm_segs, dmam->dm_nsegs);
    397 
    398 		free(chp->ch_queue, M_DEVBUF);
    399 		chp->atabus = NULL;
    400 	}
    401 
    402 	if (adapt->adapt_refcnt != 0)
    403 		return EBUSY;
    404 
    405 	/* leave the chip in reset */
    406 	GRWRITE(sc, GR_GC, GR_GC_GLBLRST);
    407 
    408 	return 0;
    409 }
    410 
    411 void
    412 siisata_resume(struct siisata_softc *sc)
    413 {
    414 	int i;
    415 
    416 	/* come out of reset state */
    417 	GRWRITE(sc, GR_GC, 0);
    418 
    419 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
    420 		siisata_init_port(sc, i);
    421 	}
    422 
    423 }
    424 
    425 int
    426 siisata_intr(void *v)
    427 {
    428 	struct siisata_softc *sc = v;
    429 	uint32_t is;
    430 	int i, r = 0;
    431 	while ((is = GRREAD(sc, GR_GIS))) {
    432 		SIISATA_DEBUG_PRINT(("%s: %s: GR_GIS: 0x%08x\n",
    433 		    SIISATANAME(sc), __func__, is), DEBUG_INTR);
    434 		r = 1;
    435 		for (i = 0; i < sc->sc_atac.atac_nchannels; i++)
    436 			if (is & GR_GIS_PXIS(i))
    437 				siisata_intr_port(&sc->sc_channels[i]);
    438 	}
    439 	return r;
    440 }
    441 
    442 static void
    443 siisata_intr_port(struct siisata_channel *schp)
    444 {
    445 	struct siisata_softc *sc;
    446 	struct ata_channel *chp;
    447 	struct ata_xfer *xfer;
    448 	int slot;
    449 	uint32_t pss, pis;
    450 	uint32_t prbfis;
    451 
    452 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
    453 	chp = &schp->ata_channel;
    454 	xfer = chp->ch_queue->active_xfer;
    455 	slot = SIISATA_NON_NCQ_SLOT;
    456 
    457 	SIISATA_DEBUG_PRINT(("%s: %s port %d\n",
    458 	    SIISATANAME(sc), __func__, chp->ch_channel), DEBUG_INTR);
    459 
    460 	pis = PRREAD(sc, PRX(chp->ch_channel, PRO_PIS));
    461 
    462 	if (pis & PR_PIS_CMDCMPL) {
    463 		/* get slot status, clearing completion interrupt */
    464 		pss = PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
    465 		/* is this expected? */
    466 		/* XXX improve */
    467 		if ((schp->sch_active_slots & __BIT(slot)) == 0) {
    468 			log(LOG_WARNING, "%s: unexpected command "
    469 			    "completion on port %d\n",
    470 			    SIISATANAME(sc), chp->ch_channel);
    471 			return;
    472 		}
    473 	} else if (pis & PR_PIS_CMDERRR) {
    474 		uint32_t ec;
    475 
    476 		/* emulate a CRC error by default */
    477 		chp->ch_status = WDCS_ERR;
    478 		chp->ch_error = WDCE_CRC;
    479 
    480 		ec = PRREAD(sc, PRX(chp->ch_channel, PRO_PCE));
    481 		if (ec <= PR_PCE_DATAFISERROR) {
    482 			if (ec != PR_PCE_DATAFISERROR) {
    483 				/* read in specific information about error */
    484 				prbfis = bus_space_read_stream_4(
    485 				    sc->sc_prt, sc->sc_prh,
    486 		    		    PRSX(chp->ch_channel, slot, PRSO_FIS));
    487 				/* set ch_status and ch_error */
    488 				satafis_sdb_parse(chp, (uint8_t *)&prbfis);
    489 			}
    490 			siisata_reinit_port(chp);
    491 		} else {
    492 			/* okay, we have a "Fatal Error" */
    493 			siisata_device_reset(chp);
    494 		}
    495 	}
    496 
    497 	KASSERT(xfer != NULL);
    498 	KASSERT(xfer->c_intr != NULL);
    499 	xfer->c_intr(chp, xfer, slot);
    500 
    501 	/* clear some (ok, all) ints */
    502 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
    503 
    504 	return;
    505 }
    506 
    507 void
    508 siisata_reset_drive(struct ata_drive_datas *drvp, int flags)
    509 {
    510 	struct ata_channel *chp = drvp->chnl_softc;
    511 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    512 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    513 	struct siisata_prb *prb;
    514 	kmutex_t mtx;
    515 	kcondvar_t cv;
    516 	int slot = SIISATA_NON_NCQ_SLOT;
    517 	int i;
    518 	int wait;
    519 
    520 	mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
    521 	cv_init(&cv, "siipd");
    522 
    523 	wait = mstohz(10);
    524 	wait = wait ? wait : 1;
    525 
    526 	/* wait for ready */
    527 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) & PR_PS_PORT_READY))
    528 		DELAY(10);
    529 
    530 	prb = schp->sch_prb[slot];
    531 	memset(prb, 0, sizeof(struct siisata_prb));
    532 	prb->prb_control =
    533 	    htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
    534 
    535 	siisata_activate_prb(schp, slot);
    536 
    537 	for(i = 0; i < (31000/(1000/(wait*hz))); i++) {
    538 		if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
    539 		    PR_PXSS(slot))
    540 			break;
    541 		else
    542 			cv_timedwait(&cv, &mtx, wait);
    543 	}
    544 
    545 	siisata_deactivate_prb(schp, slot);
    546 
    547 	log(LOG_DEBUG, "%s: ch_status %x ch_error %x\n",
    548 	    __func__, chp->ch_status, chp->ch_error);
    549 
    550 #if 1
    551 	/* attempt to downgrade signaling in event of CRC error */
    552 	/* XXX should be part of the MI (S)ATA subsystem */
    553 	if (chp->ch_status == 0x51 && chp->ch_error == 0x84) {
    554 		bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
    555 		    SControl_IPM_NONE | SControl_SPD_G1 | SControl_DET_INIT);
    556 		DELAY(10);
    557 		bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
    558 		    SControl_IPM_NONE | SControl_SPD_G1);
    559 		DELAY(10);
    560 		for (;;) {
    561 			if ((bus_space_read_4(sc->sc_prt, schp->sch_sstatus, 0)
    562 			    & SStatus_DET_mask) == SStatus_DET_DEV)
    563 				break;
    564 			DELAY(10);
    565 		}
    566 	}
    567 #endif
    568 
    569 #if 1
    570 	chp->ch_status = 0;
    571 	chp->ch_error = 0;
    572 #endif
    573 
    574 	cv_destroy(&cv);
    575 	mutex_destroy(&mtx);
    576 
    577 	return;
    578 }
    579 
    580 void
    581 siisata_reset_channel(struct ata_channel *chp, int flags)
    582 {
    583 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    584 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    585 
    586 	SIISATA_DEBUG_PRINT(("%s: %s\n", SIISATANAME(sc), __func__),
    587 	    DEBUG_FUNCS);
    588 
    589 	if (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
    590 	    schp->sch_sstatus) != SStatus_DET_DEV) {
    591 		log(LOG_CRIT, "%s port %d: reset failed\n",
    592 		    SIISATANAME(sc), chp->ch_channel);
    593 		/* XXX and then ? */
    594 	}
    595 	/* wait for ready */
    596 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) & PR_PS_PORT_READY))
    597 		DELAY(10);
    598 	PRWRITE(sc, PRX(chp->ch_channel, PRO_SERROR),
    599 	    PRREAD(sc, PRX(chp->ch_channel, PRO_SERROR)));
    600 	if (chp->ch_queue->active_xfer) {
    601 		chp->ch_queue->active_xfer->c_kill_xfer(chp,
    602 		    chp->ch_queue->active_xfer, KILL_RESET);
    603 	}
    604 
    605 	return;
    606 }
    607 
    608 int
    609 siisata_ata_addref(struct ata_drive_datas *drvp)
    610 {
    611 	return 0;
    612 }
    613 
    614 void
    615 siisata_ata_delref(struct ata_drive_datas *drvp)
    616 {
    617 	return;
    618 }
    619 
    620 void
    621 siisata_killpending(struct ata_drive_datas *drvp)
    622 {
    623 	return;
    624 }
    625 
    626 void
    627 siisata_probe_drive(struct ata_channel *chp)
    628 {
    629 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    630 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    631 	int i;
    632 	int s;
    633 	uint32_t sig;
    634 	int slot = SIISATA_NON_NCQ_SLOT;
    635 	struct siisata_prb *prb;
    636 	kmutex_t mtx;
    637 	kcondvar_t cv;
    638 	int wait;
    639 
    640 	SIISATA_DEBUG_PRINT(("%s: %s: port %d start\n", SIISATANAME(sc),
    641 	    __func__, chp->ch_channel), DEBUG_FUNCS);
    642 
    643 	/* XXX This should be done by other code. */
    644 	for (i = 0; i < chp->ch_ndrive; i++) {
    645 		chp->ch_drive[i].chnl_softc = chp;
    646 		chp->ch_drive[i].drive = i;
    647 	}
    648 
    649 	mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
    650 	cv_init(&cv, "siipd");
    651 
    652 	wait = mstohz(10);
    653 	wait = wait ? wait : 1;
    654 
    655 	switch (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
    656 		schp->sch_sstatus)) {
    657 	case SStatus_DET_DEV:
    658 		/* wait for ready */
    659 		while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PCS))
    660 		    & PR_PS_PORT_READY))
    661 			DELAY(10);
    662 
    663 		prb = schp->sch_prb[slot];
    664 		memset(prb, 0, sizeof(struct siisata_prb));
    665 		prb->prb_control =
    666 		    htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
    667 
    668 		siisata_activate_prb(schp, slot);
    669 
    670 		for(i = 0; i < (31000/(1000/(wait*hz))); i++) {
    671 			if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
    672 			    PR_PXSS(slot))
    673 				break;
    674 			else
    675 				cv_timedwait(&cv, &mtx, wait);
    676 		}
    677 
    678 		siisata_deactivate_prb(schp, slot);
    679 
    680 		/* read the signature out of the FIS */
    681 		sig = 0;
    682 		sig |= (PRREAD(sc, PRSX(chp->ch_channel, slot,
    683 		    PRSO_FIS+0x4)) & 0x00ffffff) << 8;
    684 		sig |= PRREAD(sc, PRSX(chp->ch_channel, slot,
    685 		    PRSO_FIS+0xc)) & 0xff;
    686 
    687 		SIISATA_DEBUG_PRINT(("%s: %s: sig=0x%08x\n", SIISATANAME(sc),
    688 		    __func__, sig), DEBUG_PROBE);
    689 
    690 		/* some ATAPI devices have bogus lower two bytes, sigh */
    691 		if ((sig & 0xffff0000) == 0xeb140000) {
    692 			sig &= 0xffff0000;
    693 			sig |= 0x00000101;
    694 		}
    695 
    696 		s = splbio();
    697 		switch (sig) {
    698 		case 0xeb140101:
    699 			chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
    700 			break;
    701 		case 0x00000101:
    702 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
    703 			break;
    704 		default:
    705 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
    706 			aprint_verbose_dev(sc->sc_atac.atac_dev,
    707 			    "Unrecognized signature 0x%08x on port %d. "
    708 			    "Assuming it's a disk.\n", sig, chp->ch_channel);
    709 			break;
    710 		}
    711 		splx(s);
    712 		break;
    713 	default:
    714 		break;
    715 	}
    716 
    717 	cv_destroy(&cv);
    718 	mutex_destroy(&mtx);
    719 
    720 	SIISATA_DEBUG_PRINT(("%s: %s: port %d done\n", SIISATANAME(sc),
    721 	    __func__, chp->ch_channel), DEBUG_PROBE);
    722 	return;
    723 }
    724 
    725 void
    726 siisata_setup_channel(struct ata_channel *chp)
    727 {
    728 	return;
    729 }
    730 
    731 int
    732 siisata_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
    733 {
    734 	struct ata_channel *chp = drvp->chnl_softc;
    735 	struct ata_xfer *xfer;
    736 	int ret;
    737 	int s;
    738 
    739 	SIISATA_DEBUG_PRINT(("%s: %s begins\n",
    740 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
    741 	    DEBUG_FUNCS);
    742 
    743 	xfer = ata_get_xfer(ata_c->flags & AT_WAIT ?
    744 	    ATAXF_CANSLEEP : ATAXF_NOSLEEP);
    745 	if (xfer == NULL)
    746 		return ATACMD_TRY_AGAIN;
    747 	if (ata_c->flags & AT_POLL)
    748 		xfer->c_flags |= C_POLL;
    749 	if (ata_c->flags & AT_WAIT)
    750 		xfer->c_flags |= C_WAIT;
    751 	xfer->c_drive = drvp->drive;
    752 	xfer->c_databuf = ata_c->data;
    753 	xfer->c_bcount = ata_c->bcount;
    754 	xfer->c_cmd = ata_c;
    755 	xfer->c_start = siisata_cmd_start;
    756 	xfer->c_intr = siisata_cmd_complete;
    757 	xfer->c_kill_xfer = siisata_cmd_kill_xfer;
    758 	s = splbio();
    759 	ata_exec_xfer(chp, xfer);
    760 #ifdef DIAGNOSTIC
    761 	if ((ata_c->flags & AT_POLL) != 0 &&
    762 	    (ata_c->flags & AT_DONE) == 0)
    763 		panic("%s: polled command not done", __func__);
    764 #endif
    765 	if (ata_c->flags & AT_DONE) {
    766 		ret = ATACMD_COMPLETE;
    767 	} else {
    768 		if (ata_c->flags & AT_WAIT) {
    769 			while ((ata_c->flags & AT_DONE) == 0) {
    770 				SIISATA_DEBUG_PRINT(("%s: %s: sleeping\n",
    771 				    SIISATANAME(
    772 				    (struct siisata_softc *)chp->ch_atac),
    773 				    __func__), DEBUG_FUNCS);
    774 				tsleep(ata_c, PRIBIO, "siicmd", 0);
    775 			}
    776 			ret = ATACMD_COMPLETE;
    777 		} else {
    778 			ret = ATACMD_QUEUED;
    779 		}
    780 	}
    781 	splx(s);
    782 	SIISATA_DEBUG_PRINT( ("%s: %s ends\n",
    783 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
    784 	    DEBUG_FUNCS);
    785 	return ret;
    786 }
    787 
    788 void
    789 siisata_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
    790 {
    791 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    792 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    793 	struct ata_command *ata_c = xfer->c_cmd;
    794 	int slot = SIISATA_NON_NCQ_SLOT;
    795 	struct siisata_prb *prb;
    796 	int i;
    797 
    798 	SIISATA_DEBUG_PRINT(("%s: %s port %d, slot %d\n",
    799 	    SIISATANAME(sc), __func__, chp->ch_channel, slot), DEBUG_FUNCS);
    800 
    801 	prb = schp->sch_prb[slot];
    802 	memset(prb, 0, sizeof(struct siisata_prb));
    803 
    804 	satafis_rhd_construct_cmd(ata_c, prb->prb_fis);
    805 
    806 	memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
    807 
    808 	if (siisata_dma_setup(chp, slot,
    809 	    (ata_c->flags & (AT_READ | AT_WRITE)) ? ata_c->data : NULL,
    810 	    ata_c->bcount,
    811 	    (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
    812 		ata_c->flags |= AT_DF;
    813 		siisata_cmd_complete(chp, xfer, slot);
    814 		return;
    815 	}
    816 
    817 	if (xfer->c_flags & C_POLL) {
    818 		/* polled command, disable interrupts */
    819 		prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
    820 	}
    821 
    822 	/* go for it */
    823 	siisata_activate_prb(schp, slot);
    824 
    825 	if ((ata_c->flags & AT_POLL) == 0) {
    826 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
    827 		callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
    828 		    siisata_timeout, chp);
    829 		goto out;
    830 	}
    831 
    832 	/*
    833 	 * polled command
    834 	 */
    835 	for (i = 0; i < ata_c->timeout / 10; i++) {
    836 		if (ata_c->flags & AT_DONE)
    837 			break;
    838 		siisata_intr_port(schp);
    839 		DELAY(10000);
    840 	}
    841 
    842 	if ((ata_c->flags & AT_DONE) == 0) {
    843 		ata_c->flags |= AT_TIMEOU;
    844 		siisata_cmd_complete(chp, xfer, slot);
    845 	}
    846 
    847 	/* reenable interrupts */
    848 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
    849 out:
    850 	SIISATA_DEBUG_PRINT(
    851 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
    852 	return;
    853 }
    854 
    855 void
    856 siisata_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
    857     int reason)
    858 {
    859 	int slot = SIISATA_NON_NCQ_SLOT;
    860 
    861 	struct ata_command *ata_c = xfer->c_cmd;
    862 	switch (reason) {
    863 	case KILL_GONE:
    864 		ata_c->flags |= AT_GONE;
    865 		break;
    866 	case KILL_RESET:
    867 		ata_c->flags |= AT_RESET;
    868 		break;
    869 	default:
    870 		panic("%s: port %d: unknown reason %d",
    871 		   __func__, chp->ch_channel, reason);
    872 	}
    873 	siisata_cmd_done(chp, xfer, slot);
    874 }
    875 
    876 int
    877 siisata_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
    878 {
    879 	struct ata_command *ata_c = xfer->c_cmd;
    880 #ifdef SIISATA_DEBUG
    881 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    882 #endif
    883 
    884 	SIISATA_DEBUG_PRINT(
    885 	    ("%s: %s\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
    886 
    887 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
    888 	if (xfer->c_flags & C_TIMEOU)
    889 		ata_c->flags |= AT_TIMEOU;
    890 	else
    891 		callout_stop(&chp->ch_callout);
    892 
    893 
    894 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
    895 		siisata_cmd_kill_xfer(chp, xfer, KILL_GONE);
    896 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
    897 		wakeup(&chp->ch_queue->active_xfer);
    898 		return 0;
    899 	}
    900 
    901 	chp->ch_queue->active_xfer = NULL;
    902 
    903 	{
    904 		ata_c->r_head = 0;
    905 		ata_c->r_count = 0;
    906 		ata_c->r_sector = 0;
    907 		ata_c->r_cyl = 0;
    908 		if (chp->ch_status & WDCS_BSY) {
    909 			ata_c->flags |= AT_TIMEOU;
    910 		} else if (chp->ch_status & WDCS_ERR) {
    911 			ata_c->r_error = chp->ch_error;
    912 			ata_c->flags |= AT_ERROR;
    913 		}
    914 	}
    915 	siisata_cmd_done(chp, xfer, slot);
    916 	return 0;
    917 }
    918 
    919 void
    920 siisata_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
    921 {
    922 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    923 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    924 	struct ata_command *ata_c = xfer->c_cmd;
    925 	int i;
    926 	uint16_t *idwordbuf;
    927 
    928 	SIISATA_DEBUG_PRINT(
    929 	    ("%s: %s.\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
    930 
    931 	siisata_deactivate_prb(schp, slot);
    932 
    933 	if (ata_c->flags & (AT_READ | AT_WRITE)) {
    934 		bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
    935 		    schp->sch_datad[slot]->dm_mapsize,
    936 		    (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
    937 		    BUS_DMASYNC_POSTWRITE);
    938 		bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
    939 	}
    940 
    941 	idwordbuf = xfer->c_databuf;
    942 
    943 	/* correct the endianess of IDENTIFY data */
    944 	if (ata_c->r_command == WDCC_IDENTIFY ||
    945 	    ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
    946 		for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
    947 			idwordbuf[i] = le16toh(idwordbuf[i]);
    948 		}
    949 	}
    950 
    951 	ata_c->flags |= AT_DONE;
    952 	if (PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC)))
    953 		ata_c->flags |= AT_XFDONE;
    954 
    955 	ata_free_xfer(chp, xfer);
    956 	if (ata_c->flags & AT_WAIT)
    957 		wakeup(ata_c);
    958 	else if (ata_c->callback)
    959 		ata_c->callback(ata_c->callback_arg);
    960 	atastart(chp);
    961 	return;
    962 }
    963 
    964 int
    965 siisata_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
    966 {
    967 	struct ata_channel *chp = drvp->chnl_softc;
    968 	struct ata_xfer *xfer;
    969 
    970 	SIISATA_DEBUG_PRINT( ("%s: %s.\n",
    971 	    SIISATANAME((struct siisata_softc *)chp->ch_atac),
    972 	    __func__), DEBUG_FUNCS);
    973 
    974 	xfer = ata_get_xfer(ATAXF_NOSLEEP);
    975 	if (xfer == NULL)
    976 		return ATACMD_TRY_AGAIN;
    977 	if (ata_bio->flags & ATA_POLL)
    978 		xfer->c_flags |= C_POLL;
    979 	xfer->c_drive = drvp->drive;
    980 	xfer->c_cmd = ata_bio;
    981 	xfer->c_databuf = ata_bio->databuf;
    982 	xfer->c_bcount = ata_bio->bcount;
    983 	xfer->c_start = siisata_bio_start;
    984 	xfer->c_intr = siisata_bio_complete;
    985 	xfer->c_kill_xfer = siisata_bio_kill_xfer;
    986 	ata_exec_xfer(chp, xfer);
    987 	return (ata_bio->flags & ATA_ITSDONE) ?
    988 	    ATACMD_COMPLETE : ATACMD_QUEUED;
    989 }
    990 
    991 void
    992 siisata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
    993 {
    994 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
    995 	struct siisata_channel *schp = (struct siisata_channel *)chp;
    996 	struct siisata_prb *prb;
    997 	struct ata_bio *ata_bio = xfer->c_cmd;
    998 	int slot = SIISATA_NON_NCQ_SLOT;
    999 	int i;
   1000 
   1001 	SIISATA_DEBUG_PRINT(
   1002 	    ("%s: %s port %d, slot %d\n",
   1003 	    SIISATANAME(sc), __func__, chp->ch_channel, slot),
   1004 	    DEBUG_FUNCS);
   1005 
   1006 	prb = schp->sch_prb[slot];
   1007 	memset(prb, 0, sizeof(struct siisata_prb));
   1008 
   1009 	satafis_rhd_construct_bio(xfer, prb->prb_fis);
   1010 
   1011 	memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
   1012 
   1013 	if (siisata_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
   1014 	    (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
   1015 		ata_bio->error = ERR_DMA;
   1016 		ata_bio->r_error = 0;
   1017 		siisata_bio_complete(chp, xfer, slot);
   1018 		return;
   1019 	}
   1020 
   1021 	if (xfer->c_flags & C_POLL) {
   1022 		/* polled command, disable interrupts */
   1023 		prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
   1024 	}
   1025 
   1026 	siisata_activate_prb(schp, slot);
   1027 
   1028 	if ((ata_bio->flags & ATA_POLL) == 0) {
   1029 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
   1030 		callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
   1031 		    siisata_timeout, chp);
   1032 		goto out;
   1033 	}
   1034 
   1035 	/*
   1036 	 * polled command
   1037 	 */
   1038 	for (i = 0; i < ATA_DELAY / 10; i++) {
   1039 		if (ata_bio->flags & ATA_ITSDONE)
   1040 			break;
   1041 		siisata_intr_port(schp);
   1042 		DELAY(10000);
   1043 	}
   1044 
   1045 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
   1046 out:
   1047 	SIISATA_DEBUG_PRINT(
   1048 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
   1049 	return;
   1050 }
   1051 
   1052 void
   1053 siisata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
   1054     int reason)
   1055 {
   1056 	struct siisata_channel *schp = (struct siisata_channel *)chp;
   1057 	struct ata_bio *ata_bio = xfer->c_cmd;
   1058 	int drive = xfer->c_drive;
   1059 	int slot = SIISATA_NON_NCQ_SLOT;
   1060 
   1061 	SIISATA_DEBUG_PRINT(("%s: %s: port %d\n",
   1062 	    SIISATANAME((struct siisata_softc *)chp->ch_atac),
   1063 	    __func__, chp->ch_channel), DEBUG_FUNCS);
   1064 
   1065 	siisata_deactivate_prb(schp, slot);
   1066 
   1067 	ata_free_xfer(chp, xfer);
   1068 	ata_bio->flags |= ATA_ITSDONE;
   1069 	switch (reason) {
   1070 	case KILL_GONE:
   1071 		ata_bio->error = ERR_NODEV;
   1072 		break;
   1073 	case KILL_RESET:
   1074 		ata_bio->error = ERR_RESET;
   1075 		break;
   1076 	default:
   1077 		panic("%s: port %d: unknown reason %d",
   1078 		   __func__, chp->ch_channel, reason);
   1079 	}
   1080 	ata_bio->r_error = WDCE_ABRT;
   1081 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
   1082 }
   1083 
   1084 int
   1085 siisata_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
   1086 {
   1087 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1088 	struct siisata_channel *schp = (struct siisata_channel *)chp;
   1089 	struct ata_bio *ata_bio = xfer->c_cmd;
   1090 	int drive = xfer->c_drive;
   1091 
   1092 	schp->sch_active_slots &= ~__BIT(slot);
   1093 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
   1094 	if (xfer->c_flags & C_TIMEOU) {
   1095 		ata_bio->error = TIMEOUT;
   1096 	} else {
   1097 		callout_stop(&chp->ch_callout);
   1098 		ata_bio->error = NOERROR;
   1099 	}
   1100 
   1101 	chp->ch_queue->active_xfer = NULL;
   1102 
   1103 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
   1104 	    schp->sch_datad[slot]->dm_mapsize,
   1105 	    (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
   1106 	    BUS_DMASYNC_POSTWRITE);
   1107 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
   1108 
   1109 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
   1110 		siisata_bio_kill_xfer(chp, xfer, KILL_GONE);
   1111 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
   1112 		wakeup(&chp->ch_queue->active_xfer);
   1113 		return 0;
   1114 	}
   1115 	ata_free_xfer(chp, xfer);
   1116 	ata_bio->flags |= ATA_ITSDONE;
   1117 	if (chp->ch_status & WDCS_DWF) {
   1118 		ata_bio->error = ERR_DF;
   1119 	} else if (chp->ch_status & WDCS_ERR) {
   1120 		ata_bio->error = ERROR;
   1121 		ata_bio->r_error = chp->ch_error;
   1122 	} else if (chp->ch_status & WDCS_CORR)
   1123 		ata_bio->flags |= ATA_CORR;
   1124 
   1125 	SIISATA_DEBUG_PRINT(("%s: %s bcount: %ld", SIISATANAME(sc),
   1126 	    __func__, ata_bio->bcount), DEBUG_XFERS);
   1127 	if ((ata_bio->flags & ATA_READ) || (ata_bio->error == NOERROR))
   1128 		ata_bio->bcount -= PRREAD(sc,
   1129 		    PRSX(chp->ch_channel, slot, PRSO_RTC));
   1130 	SIISATA_DEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
   1131 	if (ata_bio->flags & ATA_POLL)
   1132 		return 1;
   1133 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
   1134 	atastart(chp);
   1135 	return 0;
   1136 }
   1137 
   1138 void
   1139 siisata_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 slot = SIISATA_NON_NCQ_SLOT;
   1144 	int s = splbio();
   1145 	SIISATA_DEBUG_PRINT(("%s: %p\n", __func__, xfer), DEBUG_INTR);
   1146 	if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
   1147 		xfer->c_flags |= C_TIMEOU;
   1148 		xfer->c_intr(chp, xfer, slot);
   1149 	}
   1150 	splx(s);
   1151 }
   1152 
   1153 static int
   1154 siisata_dma_setup(struct ata_channel *chp, int slot, void *data,
   1155     size_t count, int op)
   1156 {
   1157 
   1158 	int error, seg;
   1159 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1160 	struct siisata_channel *schp = (struct siisata_channel *)chp;
   1161 
   1162 	struct siisata_prb *prbp;
   1163 
   1164 	prbp = schp->sch_prb[slot];
   1165 
   1166 	if (data == NULL) {
   1167 		goto end;
   1168 	}
   1169 
   1170 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_datad[slot],
   1171 	    data, count, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
   1172 	if (error) {
   1173 		log(LOG_ERR, "%s port %d: "
   1174 		    "failed to load xfer in slot %d: error %d\n",
   1175 		    SIISATANAME(sc), chp->ch_channel, slot, error);
   1176 		return error;
   1177 	}
   1178 
   1179 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
   1180 	    schp->sch_datad[slot]->dm_mapsize,
   1181 	    (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1182 
   1183 	/* make sure it's clean */
   1184 	memset(prbp->prb_sge, 0, SIISATA_NSGE * sizeof(struct siisata_prb));
   1185 
   1186 	SIISATA_DEBUG_PRINT(("%s: %d segs, %ld count\n", __func__,
   1187 	    schp->sch_datad[slot]->dm_nsegs, (long unsigned int) count),
   1188 	    DEBUG_FUNCS | DEBUG_DEBUG);
   1189 
   1190 	for (seg = 0; seg < schp->sch_datad[slot]->dm_nsegs; seg++) {
   1191 		prbp->prb_sge[seg].sge_da =
   1192 		    htole64(schp->sch_datad[slot]->dm_segs[seg].ds_addr);
   1193 		prbp->prb_sge[seg].sge_dc =
   1194 		    htole32(schp->sch_datad[slot]->dm_segs[seg].ds_len);
   1195 		prbp->prb_sge[seg].sge_flags = htole32(0);
   1196 	}
   1197 	prbp->prb_sge[seg - 1].sge_flags |= htole32(SGE_FLAG_TRM);
   1198 end:
   1199 	return 0;
   1200 }
   1201 
   1202 static void
   1203 siisata_activate_prb(struct siisata_channel *schp, int slot)
   1204 {
   1205 	struct siisata_softc *sc;
   1206 	bus_size_t offset;
   1207 	bus_addr_t pprb;
   1208 	int port;
   1209 
   1210 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
   1211 
   1212 	KASSERTMSG(((schp->sch_active_slots & __BIT(slot)) == __BIT(slot)),
   1213 	    ("%s: trying to activate active slot %d", SIISATANAME(sc), slot));
   1214 
   1215 	port = schp->ata_channel.ch_channel;
   1216 
   1217 	offset = PRO_CARX(port, slot);
   1218 
   1219 	pprb = schp->sch_bus_prb[slot];
   1220 
   1221 
   1222 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_PREWRITE);
   1223 	/* keep track of what's going on */
   1224 	schp->sch_active_slots |= __BIT(slot);
   1225 
   1226 
   1227 	PRWRITE(sc, offset, pprb);
   1228 	offset += 4;
   1229 #if 0
   1230 	if (sizeof(bus_addr_t) == 8)
   1231 		PRWRITE(sc, offset, (pprb >> 32));
   1232 	else
   1233 #endif
   1234 		PRWRITE(sc, offset, 0);
   1235 }
   1236 
   1237 static void
   1238 siisata_deactivate_prb(struct siisata_channel *schp, int slot)
   1239 {
   1240 	struct siisata_softc *sc;
   1241 
   1242 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
   1243 
   1244 	KASSERTMSG(((schp->sch_active_slots & __BIT(slot)) == 0),
   1245 	    ("%s: trying to deactivate inactive slot %d", SIISATANAME(sc),
   1246 	    slot));
   1247 
   1248 	schp->sch_active_slots &= ~__BIT(slot); /* mark free */
   1249 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_POSTWRITE);
   1250 }
   1251 
   1252 static void
   1253 siisata_reinit_port(struct ata_channel *chp)
   1254 {
   1255 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1256 
   1257 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS),
   1258 	    PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) | PR_PC_PORT_INITIALIZE);
   1259 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) & PR_PS_PORT_READY))
   1260 		DELAY(10);
   1261 }
   1262 
   1263 static void
   1264 siisata_device_reset(struct ata_channel *chp)
   1265 {
   1266 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1267 
   1268 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS),
   1269 	    PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) | PR_PC_DEVICE_RESET);
   1270 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PCS)) & PR_PS_PORT_READY))
   1271 		DELAY(10);
   1272 }
   1273 
   1274 
   1275 #if NATAPIBUS > 0
   1276 void
   1277 siisata_atapibus_attach(struct atabus_softc *ata_sc)
   1278 {
   1279 	struct ata_channel *chp = ata_sc->sc_chan;
   1280 	struct atac_softc *atac = chp->ch_atac;
   1281 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
   1282 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
   1283 
   1284 	/*
   1285 	 * Fill in the scsipi_adapter.
   1286 	 */
   1287 	adapt->adapt_dev = atac->atac_dev;
   1288 	adapt->adapt_nchannels = atac->atac_nchannels;
   1289 	adapt->adapt_request = siisata_atapi_scsipi_request;
   1290 	adapt->adapt_minphys = siisata_atapi_minphys;
   1291 	atac->atac_atapi_adapter.atapi_probe_device =
   1292 	    siisata_atapi_probe_device;
   1293 
   1294 	/*
   1295 	 * Fill in the scsipi_channel.
   1296 	 */
   1297 	memset(chan, 0, sizeof(*chan));
   1298 	chan->chan_adapter = adapt;
   1299 	chan->chan_bustype = &siisata_atapi_bustype;
   1300 	chan->chan_channel = chp->ch_channel;
   1301 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
   1302 	chan->chan_openings = 1;
   1303 	chan->chan_max_periph = 1;
   1304 	chan->chan_ntargets = 1;
   1305 	chan->chan_nluns = 1;
   1306 
   1307 	chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
   1308 	    atapiprint);
   1309 }
   1310 
   1311 void
   1312 siisata_atapi_minphys(struct buf *bp)
   1313 {
   1314 	if (bp->b_bcount > MAXPHYS)
   1315 		bp->b_bcount = MAXPHYS;
   1316 	minphys(bp);
   1317 }
   1318 
   1319 /*
   1320  * Kill off all pending xfers for a periph.
   1321  *
   1322  * Must be called at splbio().
   1323  */
   1324 void
   1325 siisata_atapi_kill_pending(struct scsipi_periph *periph)
   1326 {
   1327 	struct atac_softc *atac =
   1328 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
   1329 	struct ata_channel *chp =
   1330 	    atac->atac_channels[periph->periph_channel->chan_channel];
   1331 
   1332 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
   1333 }
   1334 
   1335 void
   1336 siisata_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
   1337     int reason)
   1338 {
   1339 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
   1340 
   1341 	/* remove this command from xfer queue */
   1342 	switch (reason) {
   1343 	case KILL_GONE:
   1344 		sc_xfer->error = XS_DRIVER_STUFFUP;
   1345 		break;
   1346 	case KILL_RESET:
   1347 		sc_xfer->error = XS_RESET;
   1348 		break;
   1349 	default:
   1350 		panic("%s: port %d: unknown reason %d",
   1351 		   __func__, chp->ch_channel, reason);
   1352 	}
   1353 	ata_free_xfer(chp, xfer);
   1354 	scsipi_done(sc_xfer);
   1355 }
   1356 
   1357 void
   1358 siisata_atapi_probe_device(struct atapibus_softc *sc, int target)
   1359 {
   1360 	struct scsipi_channel *chan = sc->sc_channel;
   1361 	struct scsipi_periph *periph;
   1362 	struct ataparams ids;
   1363 	struct ataparams *id = &ids;
   1364 	struct siisata_softc *siic =
   1365 	    device_private(chan->chan_adapter->adapt_dev);
   1366 	struct atac_softc *atac = &siic->sc_atac;
   1367 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
   1368 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
   1369 	struct scsipibus_attach_args sa;
   1370 	char serial_number[21], model[41], firmware_revision[9];
   1371 	int s;
   1372 
   1373 	/* skip if already attached */
   1374 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
   1375 		return;
   1376 
   1377 	/* if no ATAPI device detected at attach time, skip */
   1378 	if ((drvp->drive_flags & DRIVE_ATAPI) == 0) {
   1379 		SIISATA_DEBUG_PRINT(("%s: drive %d "
   1380 		    "not present\n", __func__, target), DEBUG_PROBE);
   1381 		return;
   1382 	}
   1383 
   1384 	/* Some ATAPI devices need a bit more time after software reset. */
   1385 	delay(5000);
   1386 	if (ata_get_params(drvp, AT_WAIT, id) == 0) {
   1387 #ifdef ATAPI_DEBUG_PROBE
   1388 		log(LOG_DEBUG, "%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
   1389 		    device_xname(sc->sc_dev), target,
   1390 		    id->atap_config & ATAPI_CFG_CMD_MASK,
   1391 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
   1392 #endif
   1393 		periph = scsipi_alloc_periph(M_NOWAIT);
   1394 		if (periph == NULL) {
   1395 			aprint_error_dev(sc->sc_dev,
   1396 			    "%s: unable to allocate periph for "
   1397 			    "channel %d drive %d\n", __func__,
   1398 			    chp->ch_channel, target);
   1399 			return;
   1400 		}
   1401 		periph->periph_dev = NULL;
   1402 		periph->periph_channel = chan;
   1403 		periph->periph_switch = &atapi_probe_periphsw;
   1404 		periph->periph_target = target;
   1405 		periph->periph_lun = 0;
   1406 		periph->periph_quirks = PQUIRK_ONLYBIG;
   1407 
   1408 #ifdef SCSIPI_DEBUG
   1409 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
   1410 		    SCSIPI_DEBUG_TARGET == target)
   1411 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
   1412 #endif
   1413 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
   1414 		if (id->atap_config & ATAPI_CFG_REMOV)
   1415 			periph->periph_flags |= PERIPH_REMOVABLE;
   1416 		if (periph->periph_type == T_SEQUENTIAL) {
   1417 			s = splbio();
   1418 			drvp->drive_flags |= DRIVE_ATAPIST;
   1419 			splx(s);
   1420 		}
   1421 
   1422 		sa.sa_periph = periph;
   1423 		sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
   1424 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
   1425 		    T_REMOV : T_FIXED;
   1426 		scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
   1427 		scsipi_strvis((u_char *)serial_number, 20,
   1428 		    id->atap_serial, 20);
   1429 		scsipi_strvis((u_char *)firmware_revision, 8,
   1430 		    id->atap_revision, 8);
   1431 		sa.sa_inqbuf.vendor = model;
   1432 		sa.sa_inqbuf.product = serial_number;
   1433 		sa.sa_inqbuf.revision = firmware_revision;
   1434 
   1435 		/*
   1436 		 * Determine the operating mode capabilities of the device.
   1437 		 */
   1438 		if ((id->atap_config & ATAPI_CFG_CMD_MASK)
   1439 		    == ATAPI_CFG_CMD_16) {
   1440 			periph->periph_cap |= PERIPH_CAP_CMD16;
   1441 
   1442 			/* configure port for packet length */
   1443 			PRWRITE(siic, PRX(chp->ch_channel, PRO_PCS),
   1444 			    PRREAD(siic, PRX(chp->ch_channel, PRO_PCS)) |
   1445 			    PR_PC_PACKET_LENGTH);
   1446 		}
   1447 		/* XXX This is gross. */
   1448 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
   1449 
   1450 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
   1451 
   1452 		if (drvp->drv_softc)
   1453 			ata_probe_caps(drvp);
   1454 		else {
   1455 			s = splbio();
   1456 			drvp->drive_flags &= ~DRIVE_ATAPI;
   1457 			splx(s);
   1458 		}
   1459 	} else {
   1460 		SIISATA_DEBUG_PRINT(("%s: ATAPI_IDENTIFY_DEVICE "
   1461 		    "failed for drive %s:%d:%d: error 0x%x\n",
   1462 		    __func__, SIISATANAME(siic), chp->ch_channel, target,
   1463 		    chp->ch_error), DEBUG_PROBE);
   1464 		s = splbio();
   1465 		drvp->drive_flags &= ~DRIVE_ATAPI;
   1466 		splx(s);
   1467 	}
   1468 }
   1469 
   1470 void
   1471 siisata_atapi_scsipi_request(struct scsipi_channel *chan,
   1472     scsipi_adapter_req_t req, void *arg)
   1473 {
   1474 	struct scsipi_adapter *adapt = chan->chan_adapter;
   1475 	struct scsipi_periph *periph;
   1476 	struct scsipi_xfer *sc_xfer;
   1477 	struct siisata_softc *sc = device_private(adapt->adapt_dev);
   1478 	struct atac_softc *atac = &sc->sc_atac;
   1479 	struct ata_xfer *xfer;
   1480 	int channel = chan->chan_channel;
   1481 	int drive, s;
   1482 
   1483 	switch (req) {
   1484 	case ADAPTER_REQ_RUN_XFER:
   1485 		sc_xfer = arg;
   1486 		periph = sc_xfer->xs_periph;
   1487 		drive = periph->periph_target;
   1488 
   1489 		SIISATA_DEBUG_PRINT(("%s: %s:%d:%d\n", __func__,
   1490 		    device_xname(atac->atac_dev), channel, drive),
   1491 		    DEBUG_XFERS);
   1492 
   1493 		if (!device_is_active(atac->atac_dev)) {
   1494 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1495 			scsipi_done(sc_xfer);
   1496 			return;
   1497 		}
   1498 		xfer = ata_get_xfer(ATAXF_NOSLEEP);
   1499 		if (xfer == NULL) {
   1500 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
   1501 			scsipi_done(sc_xfer);
   1502 			return;
   1503 		}
   1504 
   1505 		if (sc_xfer->xs_control & XS_CTL_POLL)
   1506 			xfer->c_flags |= C_POLL;
   1507 		xfer->c_drive = drive;
   1508 		xfer->c_flags |= C_ATAPI;
   1509 		xfer->c_cmd = sc_xfer;
   1510 		xfer->c_databuf = sc_xfer->data;
   1511 		xfer->c_bcount = sc_xfer->datalen;
   1512 		xfer->c_start = siisata_atapi_start;
   1513 		xfer->c_intr = siisata_atapi_complete;
   1514 		xfer->c_kill_xfer = siisata_atapi_kill_xfer;
   1515 		xfer->c_dscpoll = 0;
   1516 		s = splbio();
   1517 		ata_exec_xfer(atac->atac_channels[channel], xfer);
   1518 #ifdef DIAGNOSTIC
   1519 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
   1520 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
   1521 			panic("%s: polled command not done", __func__);
   1522 #endif
   1523 		splx(s);
   1524 		return;
   1525 
   1526 	default:
   1527 		/* Not supported, nothing to do. */
   1528 		;
   1529 	}
   1530 }
   1531 
   1532 void
   1533 siisata_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
   1534 {
   1535 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1536 	struct siisata_channel *schp = (struct siisata_channel *)chp;
   1537 	struct siisata_prb *prbp;
   1538 
   1539 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
   1540 
   1541 	int slot = SIISATA_NON_NCQ_SLOT;
   1542 	int i;
   1543 
   1544 	SIISATA_DEBUG_PRINT( ("%s: %s:%d:%d, scsi flags 0x%x\n", __func__,
   1545 	    SIISATANAME(sc), chp->ch_channel,
   1546 	    chp->ch_drive[xfer->c_drive].drive, sc_xfer->xs_control),
   1547 	    DEBUG_XFERS);
   1548 
   1549 	prbp = schp->sch_prb[slot];
   1550 	memset(prbp, 0, sizeof(struct siisata_prb));
   1551 
   1552 
   1553 	/* fill in direction for ATAPI command */
   1554 	if ((sc_xfer->xs_control & XS_CTL_DATA_IN))
   1555 		prbp->prb_control |= htole16(PRB_CF_PACKET_READ);
   1556 	if ((sc_xfer->xs_control & XS_CTL_DATA_OUT))
   1557 		prbp->prb_control |= htole16(PRB_CF_PACKET_WRITE);
   1558 
   1559 	satafis_rhd_construct_atapi(xfer, prbp->prb_fis);
   1560 
   1561 	/* copy over ATAPI command */
   1562 	memcpy(prbp->prb_atapi, sc_xfer->cmd, sc_xfer->cmdlen);
   1563 
   1564 	if (siisata_dma_setup(chp, slot,
   1565 		(sc_xfer->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ?
   1566 		xfer->c_databuf : NULL,
   1567 		xfer->c_bcount,
   1568 		(sc_xfer->xs_control & XS_CTL_DATA_IN) ?
   1569 		BUS_DMA_READ : BUS_DMA_WRITE)
   1570 	)
   1571 		panic("%s", __func__);
   1572 
   1573 	if (xfer->c_flags & C_POLL) {
   1574 		/* polled command, disable interrupts */
   1575 		prbp->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
   1576 	}
   1577 
   1578 	siisata_activate_prb(schp, slot);
   1579 
   1580 	if ((xfer->c_flags & C_POLL) == 0) {
   1581 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
   1582 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
   1583 		    siisata_timeout, chp);
   1584 		goto out;
   1585 	}
   1586 
   1587 	/*
   1588 	 * polled command
   1589 	 */
   1590 	for (i = 0; i < ATA_DELAY / 10; i++) {
   1591 		if (sc_xfer->xs_status & XS_STS_DONE)
   1592 			break;
   1593 		siisata_intr_port(schp);
   1594 		DELAY(10000);
   1595 	}
   1596 	if ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
   1597 		sc_xfer->error = XS_TIMEOUT;
   1598 		siisata_atapi_complete(chp, xfer, slot);
   1599 	}
   1600 	/* reenable interrupts */
   1601 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
   1602 out:
   1603 	SIISATA_DEBUG_PRINT(
   1604 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
   1605 	return;
   1606 }
   1607 
   1608 int
   1609 siisata_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer,
   1610     int slot)
   1611 {
   1612 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
   1613 	struct siisata_channel *schp = (struct siisata_channel *)chp;
   1614 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
   1615 
   1616 	SIISATA_DEBUG_PRINT(
   1617 	    ("%s: %s()\n", SIISATANAME(sc), __func__), DEBUG_INTR);
   1618 
   1619 	/* this comamnd is not active any more */
   1620 	schp->sch_active_slots &= ~__BIT(slot);
   1621 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
   1622 	if (xfer->c_flags & C_TIMEOU) {
   1623 		sc_xfer->error = XS_TIMEOUT;
   1624 	} else {
   1625 		callout_stop(&chp->ch_callout);
   1626 		sc_xfer->error = XS_NOERROR;
   1627 	}
   1628 
   1629 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
   1630 	    schp->sch_datad[slot]->dm_mapsize,
   1631 	    (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
   1632 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1633 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
   1634 
   1635 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
   1636 		siisata_atapi_kill_xfer(chp, xfer, KILL_GONE);
   1637 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
   1638 		wakeup(&chp->ch_queue->active_xfer);
   1639 		return 0; /* XXX verify */
   1640 	}
   1641 
   1642 	chp->ch_queue->active_xfer = NULL;
   1643 	ata_free_xfer(chp, xfer);
   1644 
   1645 	sc_xfer->resid = sc_xfer->datalen;
   1646 	sc_xfer->resid -= PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC));
   1647 	SIISATA_DEBUG_PRINT(("%s: %s datalen %d resid %d\n", SIISATANAME(sc),
   1648 	    __func__, sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
   1649 	if ((chp->ch_status & WDCS_ERR) &&
   1650 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
   1651 	    sc_xfer->resid == sc_xfer->datalen)) {
   1652 		sc_xfer->error = XS_SHORTSENSE;
   1653 		sc_xfer->sense.atapi_sense = chp->ch_error;
   1654 		if ((sc_xfer->xs_periph->periph_quirks &
   1655 		    PQUIRK_NOSENSE) == 0) {
   1656 			/* request sense */
   1657 			sc_xfer->error = XS_BUSY;
   1658 			sc_xfer->status = SCSI_CHECK;
   1659 		}
   1660 	}
   1661 	scsipi_done(sc_xfer);
   1662 	atastart(chp);
   1663 	return 0; /* XXX verify */
   1664 }
   1665 
   1666 #endif /* NATAPIBUS */
   1667