Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.52
      1 /*	$NetBSD: atapi_wdc.c,v 1.52 2002/05/18 14:49:55 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  *
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.52 2002/05/18 14:49:55 bouyer Exp $");
     36 
     37 #ifndef WDCDEBUG
     38 #define WDCDEBUG
     39 #endif /* WDCDEBUG */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/file.h>
     45 #include <sys/stat.h>
     46 #include <sys/buf.h>
     47 #include <sys/malloc.h>
     48 #include <sys/device.h>
     49 #include <sys/syslog.h>
     50 #include <sys/proc.h>
     51 #include <sys/dvdio.h>
     52 
     53 #include <machine/intr.h>
     54 #include <machine/bus.h>
     55 
     56 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     57 #define    bus_space_write_multi_stream_2    bus_space_write_multi_2
     58 #define    bus_space_write_multi_stream_4    bus_space_write_multi_4
     59 #define    bus_space_read_multi_stream_2    bus_space_read_multi_2
     60 #define    bus_space_read_multi_stream_4    bus_space_read_multi_4
     61 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     62 
     63 #include <dev/ata/atareg.h>
     64 #include <dev/ata/atavar.h>
     65 #include <dev/ic/wdcreg.h>
     66 #include <dev/ic/wdcvar.h>
     67 
     68 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
     69 
     70 #define DEBUG_INTR   0x01
     71 #define DEBUG_XFERS  0x02
     72 #define DEBUG_STATUS 0x04
     73 #define DEBUG_FUNCS  0x08
     74 #define DEBUG_PROBE  0x10
     75 #ifdef WDCDEBUG
     76 int wdcdebug_atapi_mask = 0;
     77 #define WDCDEBUG_PRINT(args, level) \
     78 	if (wdcdebug_atapi_mask & (level)) \
     79 		printf args
     80 #else
     81 #define WDCDEBUG_PRINT(args, level)
     82 #endif
     83 
     84 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     85 int   wdc_atapi_get_params __P((struct scsipi_channel *, int, int,
     86 				struct ataparams *));
     87 void  wdc_atapi_probe_device __P((struct atapibus_softc *, int));
     88 void  wdc_atapi_minphys  __P((struct buf *bp));
     89 void  wdc_atapi_start	__P((struct channel_softc *,struct wdc_xfer *));
     90 int   wdc_atapi_intr	 __P((struct channel_softc *, struct wdc_xfer *, int));
     91 void  wdc_atapi_kill_xfer __P((struct channel_softc *, struct wdc_xfer *));
     92 int   wdc_atapi_ctrl	 __P((struct channel_softc *, struct wdc_xfer *, int));
     93 void  wdc_atapi_phase_complete __P((struct wdc_xfer *));
     94 void  wdc_atapi_done	 __P((struct channel_softc *, struct wdc_xfer *));
     95 void  wdc_atapi_reset	 __P((struct channel_softc *, struct wdc_xfer *));
     96 void  wdc_atapi_scsipi_request __P((struct scsipi_channel *,
     97 	scsipi_adapter_req_t, void *));
     98 void  wdc_atapi_kill_pending __P((struct scsipi_periph *));
     99 void  wdc_atapi_polldsc __P((void *arg));
    100 
    101 #define MAX_SIZE MAXPHYS
    102 
    103 const struct scsipi_bustype wdc_atapi_bustype = {
    104 	SCSIPI_BUSTYPE_ATAPI,
    105 	atapi_scsipi_cmd,
    106 	atapi_interpret_sense,
    107 	atapi_print_addr,
    108 	wdc_atapi_kill_pending,
    109 };
    110 
    111 void
    112 wdc_atapibus_attach(chp)
    113 	struct channel_softc *chp;
    114 {
    115 	struct wdc_softc *wdc = chp->wdc;
    116 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    117 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
    118 
    119 	/*
    120 	 * Fill in the scsipi_adapter.
    121 	 */
    122 	adapt->adapt_dev = &wdc->sc_dev;
    123 	adapt->adapt_nchannels = wdc->nchannels;
    124 	adapt->adapt_request = wdc_atapi_scsipi_request;
    125 	adapt->adapt_minphys = wdc_atapi_minphys;
    126 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
    127 		adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
    128 	wdc->sc_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device;
    129 
    130 	/*
    131 	 * Fill in the scsipi_channel.
    132 	 */
    133 	memset(chan, 0, sizeof(*chan));
    134 	chan->chan_adapter = adapt;
    135 	chan->chan_bustype = &wdc_atapi_bustype;
    136 	chan->chan_channel = chp->channel;
    137 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
    138 	chan->chan_openings = 1;
    139 	chan->chan_max_periph = 1;
    140 	chan->chan_ntargets = 2;
    141 	chan->chan_nluns = 1;
    142 
    143 	chp->atapibus = config_found(&wdc->sc_dev, chan, atapiprint);
    144 }
    145 
    146 void
    147 wdc_atapi_minphys(bp)
    148 	struct buf *bp;
    149 {
    150 
    151 	if (bp->b_bcount > MAX_SIZE)
    152 		bp->b_bcount = MAX_SIZE;
    153 	minphys(bp);
    154 }
    155 
    156 /*
    157  * Kill off all pending xfers for a periph.
    158  *
    159  * Must be called at splbio().
    160  */
    161 void
    162 wdc_atapi_kill_pending(periph)
    163 	struct scsipi_periph *periph;
    164 {
    165 	struct wdc_softc *wdc =
    166 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    167 	struct channel_softc *chp =
    168 	    wdc->channels[periph->periph_channel->chan_channel];
    169 
    170 	wdc_kill_pending(chp);
    171 }
    172 
    173 void
    174 wdc_atapi_kill_xfer(chp, xfer)
    175 	struct channel_softc *chp;
    176 	struct wdc_xfer *xfer;
    177 {
    178 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    179 
    180 	callout_stop(&chp->ch_callout);
    181 	/* remove this command from xfer queue */
    182 	wdc_free_xfer(chp, xfer);
    183 	sc_xfer->error = XS_DRIVER_STUFFUP;
    184 	scsipi_done(sc_xfer);
    185 }
    186 
    187 int
    188 wdc_atapi_get_params(chan, drive, flags, id)
    189 	struct scsipi_channel *chan;
    190 	int drive, flags;
    191 	struct ataparams *id;
    192 {
    193 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    194 	struct channel_softc *chp = wdc->channels[chan->chan_channel];
    195 	struct wdc_command wdc_c;
    196 
    197 	/* if no ATAPI device detected at wdc attach time, skip */
    198 	/*
    199 	 * XXX this will break scsireprobe if this is of any interest for
    200 	 * ATAPI devices one day.
    201 	 */
    202 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    203 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    204 		    drive), DEBUG_PROBE);
    205 		return -1;
    206 	}
    207 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    208 	wdc_c.r_command = ATAPI_SOFT_RESET;
    209 	wdc_c.r_st_bmask = 0;
    210 	wdc_c.r_st_pmask = 0;
    211 	wdc_c.flags = AT_POLL;
    212 	wdc_c.timeout = WDC_RESET_WAIT;
    213 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    214 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    215 		    " drive %s:%d:%d: driver failed\n",
    216 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
    217 		panic("wdc_atapi_get_params");
    218 	}
    219 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    220 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    221 		    "failed for drive %s:%d:%d: error 0x%x\n",
    222 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    223 		    wdc_c.r_error), DEBUG_PROBE);
    224 		return -1;
    225 	}
    226 	chp->ch_drive[drive].state = 0;
    227 
    228 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
    229 
    230 	/* Some ATAPI devices need a bit more time after software reset. */
    231 	delay(5000);
    232 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
    233 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    234 		    "failed for drive %s:%d:%d: error 0x%x\n",
    235 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    236 		    wdc_c.r_error), DEBUG_PROBE);
    237 		return -1;
    238 	}
    239 	return 0;
    240 }
    241 
    242 void
    243 wdc_atapi_probe_device(sc, target)
    244 	struct atapibus_softc *sc;
    245 	int target;
    246 {
    247 	struct scsipi_channel *chan = sc->sc_channel;
    248 	struct scsipi_periph *periph;
    249 	struct ataparams ids;
    250 	struct ataparams *id = &ids;
    251 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    252 	struct channel_softc *chp = wdc->channels[chan->chan_channel];
    253 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
    254 	struct scsipibus_attach_args sa;
    255 	char serial_number[21], model[41], firmware_revision[9];
    256 
    257 	/* skip if already attached */
    258 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    259 		return;
    260 
    261 	if (wdc_atapi_get_params(chan, target,
    262 	    XS_CTL_POLL|XS_CTL_NOSLEEP, id) == 0) {
    263 #ifdef ATAPI_DEBUG_PROBE
    264 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    265 		    sc->sc_dev.dv_xname, target,
    266 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    267 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    268 #endif
    269 		periph = scsipi_alloc_periph(M_NOWAIT);
    270 		if (periph == NULL) {
    271 			printf("%s: unable to allocate periph for drive %d\n",
    272 			    sc->sc_dev.dv_xname, target);
    273 			return;
    274 		}
    275 		periph->periph_dev = NULL;
    276 		periph->periph_channel = chan;
    277 		periph->periph_switch = &atapi_probe_periphsw;
    278 		periph->periph_target = target;
    279 		periph->periph_lun = 0;
    280 
    281 #ifdef SCSIPI_DEBUG
    282 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
    283 		    SCSIPI_DEBUG_TARGET == target)
    284 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    285 #endif
    286 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
    287 		if (id->atap_config & ATAPI_CFG_REMOV)
    288 			periph->periph_flags |= PERIPH_REMOVABLE;
    289 		if (periph->periph_type == T_SEQUENTIAL)
    290 			drvp->drive_flags |= DRIVE_ATAPIST;
    291 
    292 		sa.sa_periph = periph;
    293 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    294 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
    295 		    T_REMOV : T_FIXED;
    296 		scsipi_strvis(model, 40, id->atap_model, 40);
    297 		scsipi_strvis(serial_number, 20, id->atap_serial, 20);
    298 		scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
    299 		sa.sa_inqbuf.vendor = model;
    300 		sa.sa_inqbuf.product = serial_number;
    301 		sa.sa_inqbuf.revision = firmware_revision;
    302 
    303 		/*
    304 		 * Determine the operating mode capabilities of the device.
    305 		 */
    306 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    307 			periph->periph_cap |= PERIPH_CAP_CMD16;
    308 		/* XXX This is gross. */
    309 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
    310 
    311 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
    312 
    313 		if (drvp->drv_softc)
    314 			wdc_probe_caps(drvp);
    315 	}
    316 }
    317 
    318 void
    319 wdc_atapi_scsipi_request(chan, req, arg)
    320 	struct scsipi_channel *chan;
    321 	scsipi_adapter_req_t req;
    322 	void *arg;
    323 {
    324 	struct scsipi_adapter *adapt = chan->chan_adapter;
    325 	struct scsipi_periph *periph;
    326 	struct scsipi_xfer *sc_xfer;
    327 	struct wdc_softc *wdc = (void *)adapt->adapt_dev;
    328 	struct wdc_xfer *xfer;
    329 	int channel = chan->chan_channel;
    330 	int drive, s;
    331 
    332 	switch (req) {
    333 	case ADAPTER_REQ_RUN_XFER:
    334 		sc_xfer = arg;
    335 		periph = sc_xfer->xs_periph;
    336 		drive = periph->periph_target;
    337 
    338 		WDCDEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
    339 		    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
    340 		if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    341 			sc_xfer->error = XS_DRIVER_STUFFUP;
    342 			scsipi_done(sc_xfer);
    343 			return;
    344 		}
    345 
    346 		xfer = wdc_get_xfer(WDC_NOSLEEP);
    347 		if (xfer == NULL) {
    348 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
    349 			scsipi_done(sc_xfer);
    350 			return;
    351 		}
    352 
    353 		if (sc_xfer->xs_control & XS_CTL_POLL)
    354 			xfer->c_flags |= C_POLL;
    355 		xfer->drive = drive;
    356 		xfer->c_flags |= C_ATAPI;
    357 		if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
    358 		    sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
    359 		    sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
    360 			/*
    361 			 * DVD authentication commands must always be done in
    362 			 * PIO mode.
    363 			 */
    364 			xfer->c_flags |= C_FORCEPIO;
    365 		}
    366 		/*
    367 		 * DMA can't deal with transfers which are not a multiple of
    368 		 * 2 bytes. It's a bug to request such transfers for ATAPI
    369 		 * but as the request can come from userland, we have to
    370 		 * protect against it.
    371 		 */
    372 		if (sc_xfer->datalen & 0x01)
    373 			xfer->c_flags |= C_FORCEPIO;
    374 
    375 		xfer->cmd = sc_xfer;
    376 		xfer->databuf = sc_xfer->data;
    377 		xfer->c_bcount = sc_xfer->datalen;
    378 		xfer->c_start = wdc_atapi_start;
    379 		xfer->c_intr = wdc_atapi_intr;
    380 		xfer->c_kill_xfer = wdc_atapi_kill_xfer;
    381 		xfer->c_dscpoll = 0;
    382 		s = splbio();
    383 		wdc_exec_xfer(wdc->channels[channel], xfer);
    384 #ifdef DIAGNOSTIC
    385 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    386 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    387 			panic("wdc_atapi_scsipi_request: polled command "
    388 			    "not done");
    389 #endif
    390 		splx(s);
    391 		return;
    392 
    393 	default:
    394 		/* Not supported, nothing to do. */
    395 		;
    396 	}
    397 }
    398 
    399 void
    400 wdc_atapi_start(chp, xfer)
    401 	struct channel_softc *chp;
    402 	struct wdc_xfer *xfer;
    403 {
    404 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    405 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    406 
    407 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    408 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
    409 	    sc_xfer->xs_control), DEBUG_XFERS);
    410 	/* Adjust C_DMA, it may have changed if we are requesting sense */
    411 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    412 	    sc_xfer->datalen > 0 && !(xfer->c_flags & C_FORCEPIO)) {
    413 		if (drvp->n_xfers <= NXFER)
    414 			drvp->n_xfers++;
    415 		xfer->c_flags |= C_DMA;
    416 	} else {
    417 		xfer->c_flags &= ~C_DMA;
    418 	}
    419 	/* start timeout machinery */
    420 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    421 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
    422 		    wdctimeout, chp);
    423 	/* Do control operations specially. */
    424 	if (drvp->state < READY) {
    425 		if (drvp->state != RESET) {
    426 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
    427 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    428 			    xfer->drive, drvp->state);
    429 			panic("wdc_atapi_start: bad state");
    430 		}
    431 		drvp->state = PIOMODE;
    432 		wdc_atapi_ctrl(chp, xfer, 0);
    433 		return;
    434 	}
    435 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    436 	    WDSD_IBM | (xfer->drive << 4));
    437 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
    438 		printf("wdc_atapi_start: not ready, st = %02x\n",
    439 		    chp->ch_status);
    440 		sc_xfer->error = XS_TIMEOUT;
    441 		wdc_atapi_reset(chp, xfer);
    442 		return;
    443 	}
    444 
    445 	/*
    446 	 * Even with WDCS_ERR, the device should accept a command packet
    447 	 * Limit length to what can be stuffed into the cylinder register
    448 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    449 	 * but not all devices do that and it's not obvious from the
    450 	 * ATAPI spec that that behaviour should be expected.  If more
    451 	 * data is necessary, multiple data transfer phases will be done.
    452 	 */
    453 
    454 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
    455 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    456 	    0, 0, 0,
    457 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    458 
    459 	/*
    460 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    461 	 * the interrupt routine. If it is a polled command, call the interrupt
    462 	 * routine until command is done.
    463 	 */
    464 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
    465 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    466 		/* Wait for at last 400ns for status bit to be valid */
    467 		DELAY(1);
    468 		if (chp->ch_flags & WDCF_DMA_WAIT) {
    469 			wdc_dmawait(chp, xfer, sc_xfer->timeout);
    470 			chp->ch_flags &= ~WDCF_DMA_WAIT;
    471 		}
    472 		wdc_atapi_intr(chp, xfer, 0);
    473 	} else {
    474 		chp->ch_flags |= WDCF_IRQ_WAIT;
    475 	}
    476 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    477 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    478 			/* Wait for at last 400ns for status bit to be valid */
    479 			DELAY(1);
    480 			wdc_atapi_intr(chp, xfer, 0);
    481 		}
    482 	}
    483 }
    484 
    485 int
    486 wdc_atapi_intr(chp, xfer, irq)
    487 	struct channel_softc *chp;
    488 	struct wdc_xfer *xfer;
    489 	int irq;
    490 {
    491 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    492 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    493 	int len, phase, i, retries=0;
    494 	int ire;
    495 	int dma_flags = 0;
    496 	void *cmd;
    497 
    498 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    499 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
    500 
    501 	/* Is it not a transfer, but a control operation? */
    502 	if (drvp->state < READY) {
    503 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    504 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    505 		    drvp->state);
    506 		panic("wdc_atapi_intr: bad state\n");
    507 	}
    508 	/*
    509 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    510 	 * Don't try to continue transfer, we may have missed cycles.
    511 	 */
    512 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    513 		sc_xfer->error = XS_TIMEOUT;
    514 		wdc_atapi_reset(chp, xfer);
    515 		return 1;
    516 	}
    517 
    518 	/* Ack interrupt done in wait_for_unbusy */
    519 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    520 	    WDSD_IBM | (xfer->drive << 4));
    521 	if (wait_for_unbusy(chp,
    522 	    (irq == 0) ? sc_xfer->timeout : 0) != 0) {
    523 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    524 			return 0; /* IRQ was not for us */
    525 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    526 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    527 		    xfer->c_bcount, xfer->c_skip);
    528 		if (xfer->c_flags & C_DMA) {
    529 			ata_dmaerr(drvp);
    530 		}
    531 		sc_xfer->error = XS_TIMEOUT;
    532 		wdc_atapi_reset(chp, xfer);
    533 		return 1;
    534 	}
    535 	if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    536 		chp->wdc->irqack(chp);
    537 
    538 	/*
    539 	 * If we missed an IRQ and were using DMA, flag it as a DMA error
    540 	 * and reset device.
    541 	 */
    542 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
    543 		ata_dmaerr(drvp);
    544 		sc_xfer->error = XS_RESET;
    545 		wdc_atapi_reset(chp, xfer);
    546 		return (1);
    547 	}
    548 	/*
    549 	 * if the request sense command was aborted, report the short sense
    550 	 * previously recorded, else continue normal processing
    551 	 */
    552 
    553 	if (xfer->c_flags & C_DMA)
    554 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    555 		    ?  WDC_DMA_READ : 0;
    556 again:
    557 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
    558 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
    559 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
    560 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    561 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    562 	    "ire 0x%x :", xfer->c_bcount,
    563 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    564 
    565 	switch (phase) {
    566 	case PHASE_CMDOUT:
    567 		cmd  = sc_xfer->cmd;
    568 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    569 		/* Init the DMA channel if necessary */
    570 		if (xfer->c_flags & C_DMA) {
    571 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    572 			    chp->channel, xfer->drive,
    573 			    xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
    574 				sc_xfer->error = XS_DRIVER_STUFFUP;
    575 				break;
    576 			}
    577 		}
    578 		/* send packet command */
    579 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    580 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    581 			if (drvp->drive_flags & DRIVE_CAP32) {
    582 				bus_space_write_multi_4(chp->data32iot,
    583 				    chp->data32ioh, 0,
    584 				    (u_int32_t *)cmd,
    585 				    sc_xfer->cmdlen >> 2);
    586 			} else {
    587 				bus_space_write_multi_2(chp->cmd_iot,
    588 				    chp->cmd_ioh, wd_data,
    589 				    (u_int16_t *)cmd,
    590 				    sc_xfer->cmdlen >> 1);
    591 			}
    592 		} else {
    593 			if (drvp->drive_flags & DRIVE_CAP32) {
    594 				bus_space_write_multi_stream_4(chp->data32iot,
    595 				    chp->data32ioh, 0,
    596 				    (u_int32_t *)cmd,
    597 				    sc_xfer->cmdlen >> 2);
    598 			} else {
    599 				bus_space_write_multi_stream_2(chp->cmd_iot,
    600 				    chp->cmd_ioh, wd_data,
    601 				    (u_int16_t *)cmd,
    602 				    sc_xfer->cmdlen >> 1);
    603 			}
    604 		}
    605 		/* Start the DMA channel if necessary */
    606 		if (xfer->c_flags & C_DMA) {
    607 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    608 			    chp->channel, xfer->drive);
    609 			chp->ch_flags |= WDCF_DMA_WAIT;
    610 		}
    611 
    612 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    613 			chp->ch_flags |= WDCF_IRQ_WAIT;
    614 		}
    615 		return 1;
    616 
    617 	 case PHASE_DATAOUT:
    618 		/* write data */
    619 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    620 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    621 		    (xfer->c_flags & C_DMA) != 0) {
    622 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    623 			if (xfer->c_flags & C_DMA) {
    624 				ata_dmaerr(drvp);
    625 			}
    626 			sc_xfer->error = XS_TIMEOUT;
    627 			wdc_atapi_reset(chp, xfer);
    628 			return 1;
    629 		}
    630 		if (xfer->c_bcount < len) {
    631 			printf("wdc_atapi_intr: warning: write only "
    632 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    633 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    634 				bus_space_write_multi_2(chp->cmd_iot,
    635 				    chp->cmd_ioh, wd_data,
    636 				    (u_int16_t *)((char *)xfer->databuf +
    637 				                  xfer->c_skip),
    638 				    xfer->c_bcount >> 1);
    639 			} else {
    640 				bus_space_write_multi_stream_2(chp->cmd_iot,
    641 				    chp->cmd_ioh, wd_data,
    642 				    (u_int16_t *)((char *)xfer->databuf +
    643 				                  xfer->c_skip),
    644 				    xfer->c_bcount >> 1);
    645 			}
    646 			for (i = xfer->c_bcount; i < len; i += 2)
    647 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
    648 				    wd_data, 0);
    649 			xfer->c_skip += xfer->c_bcount;
    650 			xfer->c_bcount = 0;
    651 		} else {
    652 			if (drvp->drive_flags & DRIVE_CAP32) {
    653 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    654 				bus_space_write_multi_4(chp->data32iot,
    655 				    chp->data32ioh, 0,
    656 				    (u_int32_t *)((char *)xfer->databuf +
    657 				                  xfer->c_skip),
    658 				    len >> 2);
    659 			    else
    660 				bus_space_write_multi_stream_4(chp->data32iot,
    661 				    chp->data32ioh, wd_data,
    662 				    (u_int32_t *)((char *)xfer->databuf +
    663 				                  xfer->c_skip),
    664 				    len >> 2);
    665 
    666 			    xfer->c_skip += len & 0xfffffffc;
    667 			    xfer->c_bcount -= len & 0xfffffffc;
    668 			    len = len & 0x03;
    669 			}
    670 			if (len > 0) {
    671 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    672 				bus_space_write_multi_2(chp->cmd_iot,
    673 				    chp->cmd_ioh, wd_data,
    674 				    (u_int16_t *)((char *)xfer->databuf +
    675 				                  xfer->c_skip),
    676 				    len >> 1);
    677 			    else
    678 				bus_space_write_multi_stream_2(chp->cmd_iot,
    679 				    chp->cmd_ioh, wd_data,
    680 				    (u_int16_t *)((char *)xfer->databuf +
    681 				                  xfer->c_skip),
    682 				    len >> 1);
    683 			    xfer->c_skip += len;
    684 			    xfer->c_bcount -= len;
    685 			}
    686 		}
    687 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    688 			chp->ch_flags |= WDCF_IRQ_WAIT;
    689 		}
    690 		return 1;
    691 
    692 	case PHASE_DATAIN:
    693 		/* Read data */
    694 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    695 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
    696 		    (xfer->c_flags & C_DMA) != 0) {
    697 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    698 			if (xfer->c_flags & C_DMA) {
    699 				ata_dmaerr(drvp);
    700 			}
    701 			sc_xfer->error = XS_TIMEOUT;
    702 			wdc_atapi_reset(chp, xfer);
    703 			return 1;
    704 		}
    705 		if (xfer->c_bcount < len) {
    706 			printf("wdc_atapi_intr: warning: reading only "
    707 			    "%d of %d bytes\n", xfer->c_bcount, len);
    708 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    709 			    bus_space_read_multi_2(chp->cmd_iot,
    710 			    chp->cmd_ioh, wd_data,
    711 			    (u_int16_t *)((char *)xfer->databuf +
    712 			                  xfer->c_skip),
    713 			    xfer->c_bcount >> 1);
    714 			} else {
    715 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    716 			    chp->cmd_ioh, wd_data,
    717 			    (u_int16_t *)((char *)xfer->databuf +
    718 			                  xfer->c_skip),
    719 			    xfer->c_bcount >> 1);
    720 			}
    721 			wdcbit_bucket(chp, len - xfer->c_bcount);
    722 			xfer->c_skip += xfer->c_bcount;
    723 			xfer->c_bcount = 0;
    724 		} else {
    725 			if (drvp->drive_flags & DRIVE_CAP32) {
    726 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    727 				bus_space_read_multi_4(chp->data32iot,
    728 				    chp->data32ioh, 0,
    729 				    (u_int32_t *)((char *)xfer->databuf +
    730 				                  xfer->c_skip),
    731 				    len >> 2);
    732 			    else
    733 				bus_space_read_multi_stream_4(chp->data32iot,
    734 				    chp->data32ioh, wd_data,
    735 				    (u_int32_t *)((char *)xfer->databuf +
    736 				                  xfer->c_skip),
    737 				    len >> 2);
    738 
    739 			    xfer->c_skip += len & 0xfffffffc;
    740 			    xfer->c_bcount -= len & 0xfffffffc;
    741 			    len = len & 0x03;
    742 			}
    743 			if (len > 0) {
    744 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    745 				bus_space_read_multi_2(chp->cmd_iot,
    746 				    chp->cmd_ioh, wd_data,
    747 				    (u_int16_t *)((char *)xfer->databuf +
    748 				                  xfer->c_skip),
    749 				    len >> 1);
    750 			    else
    751 				bus_space_read_multi_stream_2(chp->cmd_iot,
    752 				    chp->cmd_ioh, wd_data,
    753 				    (u_int16_t *)((char *)xfer->databuf +
    754 				                  xfer->c_skip),
    755 				    len >> 1);
    756 			    xfer->c_skip += len;
    757 			    xfer->c_bcount -=len;
    758 			}
    759 		}
    760 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    761 			chp->ch_flags |= WDCF_IRQ_WAIT;
    762 		}
    763 		return 1;
    764 
    765 	case PHASE_ABORTED:
    766 	case PHASE_COMPLETED:
    767 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    768 		if (xfer->c_flags & C_DMA) {
    769 			xfer->c_bcount -= sc_xfer->datalen;
    770 		}
    771 		sc_xfer->resid = xfer->c_bcount;
    772 		wdc_atapi_phase_complete(xfer);
    773 		return(1);
    774 
    775 	default:
    776 		if (++retries<500) {
    777 			DELAY(100);
    778 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    779 			    chp->cmd_ioh, wd_status);
    780 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    781 			    chp->cmd_ioh, wd_error);
    782 			goto again;
    783 		}
    784 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    785 		if (chp->ch_status & WDCS_ERR) {
    786 			sc_xfer->error = XS_SHORTSENSE;
    787 			sc_xfer->sense.atapi_sense = chp->ch_error;
    788 		} else {
    789 			if (xfer->c_flags & C_DMA) {
    790 				ata_dmaerr(drvp);
    791 			}
    792 			sc_xfer->error = XS_RESET;
    793 			wdc_atapi_reset(chp, xfer);
    794 			return (1);
    795 		}
    796 	}
    797 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    798 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    799 	    DEBUG_INTR);
    800 	wdc_atapi_done(chp, xfer);
    801 	return (1);
    802 }
    803 
    804 int
    805 wdc_atapi_ctrl(chp, xfer, irq)
    806 	struct channel_softc *chp;
    807 	struct wdc_xfer *xfer;
    808 	int irq;
    809 {
    810 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    811 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    812 	char *errstring = NULL;
    813 	int delay = (irq == 0) ? ATAPI_DELAY : 0;
    814 
    815 	/* Ack interrupt done in wait_for_unbusy */
    816 again:
    817 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
    818 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
    819 	    DEBUG_INTR | DEBUG_FUNCS);
    820 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    821 	    WDSD_IBM | (xfer->drive << 4));
    822 	switch (drvp->state) {
    823 	case PIOMODE:
    824 		/* Don't try to set mode if controller can't be adjusted */
    825 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    826 			goto ready;
    827 		/* Also don't try if the drive didn't report its mode */
    828 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    829 			goto ready;
    830 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    831 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    832 		drvp->state = PIOMODE_WAIT;
    833 		break;
    834 	case PIOMODE_WAIT:
    835 		errstring = "piomode";
    836 		if (wait_for_unbusy(chp, delay))
    837 			goto timeout;
    838 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    839 			chp->wdc->irqack(chp);
    840 		if (chp->ch_status & WDCS_ERR) {
    841 			if (chp->ch_error == WDCE_ABRT) {
    842 				/*
    843 				 * some ATAPI drives rejects pio settings.
    844 				 * all we can do here is fall back to PIO 0
    845 				 */
    846 				drvp->drive_flags &= ~DRIVE_MODE;
    847 				drvp->drive_flags &= ~(DRIVE_DMA|DRIVE_UDMA);
    848 				drvp->PIO_mode = 0;
    849 				drvp->DMA_mode = 0;
    850 				printf("%s:%d:%d: pio setting rejected, "
    851 				    "falling back to PIO mode 0\n",
    852 				    chp->wdc->sc_dev.dv_xname,
    853 				    chp->channel, xfer->drive);
    854 				chp->wdc->set_modes(chp);
    855 				goto ready;
    856 			}
    857 			goto error;
    858 		}
    859 	/* fall through */
    860 
    861 	case DMAMODE:
    862 		if (drvp->drive_flags & DRIVE_UDMA) {
    863 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    864 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    865 		} else if (drvp->drive_flags & DRIVE_DMA) {
    866 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    867 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    868 		} else {
    869 			goto ready;
    870 		}
    871 		drvp->state = DMAMODE_WAIT;
    872 		break;
    873 	case DMAMODE_WAIT:
    874 		errstring = "dmamode";
    875 		if (wait_for_unbusy(chp, delay))
    876 			goto timeout;
    877 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    878 			chp->wdc->irqack(chp);
    879 		if (chp->ch_status & WDCS_ERR)
    880 			goto error;
    881 	/* fall through */
    882 
    883 	case READY:
    884 	ready:
    885 		drvp->state = READY;
    886 		xfer->c_intr = wdc_atapi_intr;
    887 		callout_stop(&chp->ch_callout);
    888 		wdc_atapi_start(chp, xfer);
    889 		return 1;
    890 	}
    891 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    892 		chp->ch_flags |= WDCF_IRQ_WAIT;
    893 		xfer->c_intr = wdc_atapi_ctrl;
    894 	} else {
    895 		goto again;
    896 	}
    897 	return 1;
    898 
    899 timeout:
    900 	if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
    901 		return 0; /* IRQ was not for us */
    902 	}
    903 	printf("%s:%d:%d: %s timed out\n",
    904 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    905 	sc_xfer->error = XS_TIMEOUT;
    906 	wdc_atapi_reset(chp, xfer);
    907 	return 1;
    908 error:
    909 	printf("%s:%d:%d: %s ",
    910 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    911 	    errstring);
    912 	printf("error (0x%x)\n", chp->ch_error);
    913 	sc_xfer->error = XS_SHORTSENSE;
    914 	sc_xfer->sense.atapi_sense = chp->ch_error;
    915 	wdc_atapi_reset(chp, xfer);
    916 	return 1;
    917 }
    918 
    919 void
    920 wdc_atapi_phase_complete(xfer)
    921 	struct wdc_xfer *xfer;
    922 {
    923 	struct channel_softc *chp = xfer->chp;
    924 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    925 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    926 
    927 	/* wait for DSC if needed */
    928 	if (drvp->drive_flags & DRIVE_ATAPIST) {
    929 		WDCDEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
    930 		    "polldsc %d\n", chp->wdc->sc_dev.dv_xname, chp->channel,
    931 		    xfer->drive, xfer->c_dscpoll), DEBUG_XFERS);
    932 		if (cold) {
    933 			if (wdcwait(chp, WDCS_DSC, WDCS_DSC,
    934 			    sc_xfer->timeout)) {
    935 				printf("%s:%d:%d: wait_for_dsc failed\n",
    936 				    chp->wdc->sc_dev.dv_xname, chp->channel,
    937 				    xfer->drive);
    938 				sc_xfer->error = XS_TIMEOUT;
    939 				wdc_atapi_reset(chp, xfer);
    940 				return;
    941 			}
    942 		} else {
    943 			if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10)) {
    944 				/* 10ms not enouth, try again in 1 tick */
    945 				if (xfer->c_dscpoll++ >
    946 				    mstohz(sc_xfer->timeout)) {
    947 					printf("%s:%d:%d: wait_for_dsc "
    948 					    "failed\n",
    949 					    chp->wdc->sc_dev.dv_xname,
    950 					    chp->channel, xfer->drive);
    951 					sc_xfer->error = XS_TIMEOUT;
    952 					wdc_atapi_reset(chp, xfer);
    953 					return;
    954 				}
    955 				callout_reset(&chp->ch_callout, 1,
    956 				    wdc_atapi_polldsc, xfer);
    957 				return;
    958 			}
    959 		}
    960 	}
    961 
    962 	/*
    963 	 * Some drive occasionally set WDCS_ERR with
    964 	 * "ATA illegal length indication" in the error
    965 	 * register. If we read some data the sense is valid
    966 	 * anyway, so don't report the error.
    967 	 */
    968 	if (chp->ch_status & WDCS_ERR &&
    969 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
    970 	    sc_xfer->resid == sc_xfer->datalen)) {
    971 		/* save the short sense */
    972 		sc_xfer->error = XS_SHORTSENSE;
    973 		sc_xfer->sense.atapi_sense = chp->ch_error;
    974 		if ((sc_xfer->xs_periph->periph_quirks &
    975 		    PQUIRK_NOSENSE) == 0) {
    976 			/* ask scsipi to send a REQUEST_SENSE */
    977 			sc_xfer->error = XS_BUSY;
    978 			sc_xfer->status = SCSI_CHECK;
    979 		} else if (chp->wdc->dma_status &
    980 		    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
    981 			ata_dmaerr(drvp);
    982 			sc_xfer->error = XS_RESET;
    983 			wdc_atapi_reset(chp, xfer);
    984 			return;
    985 		}
    986 	}
    987 	if (xfer->c_bcount != 0) {
    988 		WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    989 		    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    990 	}
    991 #ifdef DIAGNOSTIC
    992 	if (xfer->c_bcount < 0) {
    993 		printf("wdc_atapi_intr warning: bcount value "
    994 		    "is %d after io\n", xfer->c_bcount);
    995 	}
    996 #endif
    997 	WDCDEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
    998 	    "error 0x%x sense 0x%x\n", sc_xfer->error,
    999 	    sc_xfer->sense.atapi_sense), DEBUG_INTR);
   1000 	wdc_atapi_done(chp, xfer);
   1001 }
   1002 
   1003 void
   1004 wdc_atapi_done(chp, xfer)
   1005 	struct channel_softc *chp;
   1006 	struct wdc_xfer *xfer;
   1007 {
   1008 	struct scsipi_xfer *sc_xfer = xfer->cmd;
   1009 
   1010 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
   1011 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
   1012 	    (u_int)xfer->c_flags), DEBUG_XFERS);
   1013 	callout_stop(&chp->ch_callout);
   1014 	/* remove this command from xfer queue */
   1015 	wdc_free_xfer(chp, xfer);
   1016 
   1017 	WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
   1018 	scsipi_done(sc_xfer);
   1019 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
   1020 	    chp->ch_flags), DEBUG_XFERS);
   1021 	wdcstart(chp);
   1022 }
   1023 
   1024 void
   1025 wdc_atapi_reset(chp, xfer)
   1026 	struct channel_softc *chp;
   1027 	struct wdc_xfer *xfer;
   1028 {
   1029 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
   1030 	struct scsipi_xfer *sc_xfer = xfer->cmd;
   1031 
   1032 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
   1033 	drvp->state = 0;
   1034 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
   1035 		printf("%s:%d:%d: reset failed\n",
   1036 		    chp->wdc->sc_dev.dv_xname, chp->channel,
   1037 		    xfer->drive);
   1038 		sc_xfer->error = XS_SELTIMEOUT;
   1039 	}
   1040 	wdc_atapi_done(chp, xfer);
   1041 	return;
   1042 }
   1043 
   1044 void
   1045 wdc_atapi_polldsc(arg)
   1046 	void *arg;
   1047 {
   1048 	wdc_atapi_phase_complete(arg);
   1049 }
   1050