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