Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.42
      1 /*	$NetBSD: atapi_wdc.c,v 1.42 2001/06/13 18:17:41 bjh21 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 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
    125 		adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
    126 	wdc->sc_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device;
    127 
    128 	/*
    129 	 * Fill in the scsipi_channel.
    130 	 */
    131 	memset(chan, 0, sizeof(*chan));
    132 	chan->chan_adapter = adapt;
    133 	chan->chan_bustype = &wdc_atapi_bustype;
    134 	chan->chan_channel = chp->channel;
    135 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
    136 	chan->chan_openings = 1;
    137 	chan->chan_max_periph = 1;
    138 	chan->chan_ntargets = 2;
    139 	chan->chan_nluns = 1;
    140 
    141 	memset(&aa, 0, sizeof(aa));
    142 	aa.aa_type = T_ATAPI;
    143 	aa.aa_channel = chan->chan_channel;
    144 	aa.aa_openings = chan->chan_openings;
    145 	aa.aa_drv_data = chp->ch_drive; /* pass the whole array */
    146 	aa.aa_bus_private = chan;
    147 	chp->atapibus = config_found(&wdc->sc_dev, &aa, atapiprint);
    148 }
    149 
    150 void
    151 wdc_atapi_minphys(bp)
    152 	struct buf *bp;
    153 {
    154 
    155 	if (bp->b_bcount > MAX_SIZE)
    156 		bp->b_bcount = MAX_SIZE;
    157 	minphys(bp);
    158 }
    159 
    160 /*
    161  * Kill off all pending xfers for a periph.
    162  *
    163  * Must be called at splbio().
    164  */
    165 void
    166 wdc_atapi_kill_pending(periph)
    167 	struct scsipi_periph *periph;
    168 {
    169 	struct wdc_softc *wdc =
    170 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    171 	struct channel_softc *chp =
    172 	    wdc->channels[periph->periph_channel->chan_channel];
    173 
    174 	wdc_kill_pending(chp);
    175 }
    176 
    177 void
    178 wdc_atapi_kill_xfer(chp, xfer)
    179 	struct channel_softc *chp;
    180 	struct wdc_xfer *xfer;
    181 {
    182 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    183 
    184 	callout_stop(&chp->ch_callout);
    185 	/* remove this command from xfer queue */
    186 	wdc_free_xfer(chp, xfer);
    187 	sc_xfer->error = XS_DRIVER_STUFFUP;
    188 	scsipi_done(sc_xfer);
    189 }
    190 
    191 int
    192 wdc_atapi_get_params(chan, drive, flags, id)
    193 	struct scsipi_channel *chan;
    194 	int drive, flags;
    195 	struct ataparams *id;
    196 {
    197 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    198 	struct channel_softc *chp = wdc->channels[chan->chan_channel];
    199 	struct wdc_command wdc_c;
    200 
    201 	/* if no ATAPI device detected at wdc attach time, skip */
    202 	/*
    203 	 * XXX this will break scsireprobe if this is of any interest for
    204 	 * ATAPI devices one day.
    205 	 */
    206 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    207 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    208 		    drive), DEBUG_PROBE);
    209 		return -1;
    210 	}
    211 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    212 	wdc_c.r_command = ATAPI_SOFT_RESET;
    213 	wdc_c.r_st_bmask = 0;
    214 	wdc_c.r_st_pmask = 0;
    215 	wdc_c.flags = AT_POLL;
    216 	wdc_c.timeout = WDC_RESET_WAIT;
    217 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    218 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    219 		    " drive %s:%d:%d: driver failed\n",
    220 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
    221 		panic("wdc_atapi_get_params");
    222 	}
    223 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    224 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    225 		    "failed for drive %s:%d:%d: error 0x%x\n",
    226 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    227 		    wdc_c.r_error), DEBUG_PROBE);
    228 		return -1;
    229 	}
    230 	chp->ch_drive[drive].state = 0;
    231 
    232 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
    233 
    234 	/* Some ATAPI devices need a bit more time after software reset. */
    235 	delay(5000);
    236 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
    237 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    238 		    "failed for drive %s:%d:%d: error 0x%x\n",
    239 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    240 		    wdc_c.r_error), DEBUG_PROBE);
    241 		return -1;
    242 	}
    243 	return 0;
    244 }
    245 
    246 void
    247 wdc_atapi_probe_device(sc, target)
    248 	struct atapibus_softc *sc;
    249 	int target;
    250 {
    251 	struct scsipi_channel *chan = sc->sc_channel;
    252 	struct scsipi_periph *periph;
    253 	struct ataparams ids;
    254 	struct ataparams *id = &ids;
    255 	struct ata_drive_datas *drvp = &sc->sc_drvs[target];
    256 	struct scsipibus_attach_args sa;
    257 	char serial_number[21], model[41], firmware_revision[9];
    258 
    259 	/* skip if already attached */
    260 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    261 		return;
    262 
    263 	if (wdc_atapi_get_params(chan, target,
    264 	    XS_CTL_POLL|XS_CTL_NOSLEEP, id) == 0) {
    265 #ifdef ATAPI_DEBUG_PROBE
    266 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    267 		    sc->sc_dev.dv_xname, target,
    268 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    269 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    270 #endif
    271 		periph = scsipi_alloc_periph(M_NOWAIT);
    272 		if (periph == NULL) {
    273 			printf("%s: unable to allocate periph for drive %d\n",
    274 			    sc->sc_dev.dv_xname, target);
    275 			return;
    276 		}
    277 		periph->periph_dev = NULL;
    278 		periph->periph_channel = chan;
    279 		periph->periph_switch = &atapi_probe_periphsw;
    280 		periph->periph_target = target;
    281 		periph->periph_lun = 0;
    282 
    283 #ifdef SCSIPI_DEBUG
    284 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
    285 		    SCSIPI_DEBUG_TARGET == target)
    286 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    287 #endif
    288 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
    289 		if (id->atap_config & ATAPI_CFG_REMOV)
    290 			periph->periph_flags |= PERIPH_REMOVABLE;
    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 		xfer->cmd = sc_xfer;
    367 		xfer->databuf = sc_xfer->data;
    368 		xfer->c_bcount = sc_xfer->datalen;
    369 		xfer->c_start = wdc_atapi_start;
    370 		xfer->c_intr = wdc_atapi_intr;
    371 		xfer->c_kill_xfer = wdc_atapi_kill_xfer;
    372 		s = splbio();
    373 		wdc_exec_xfer(wdc->channels[channel], xfer);
    374 #ifdef DIAGNOSTIC
    375 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    376 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    377 			panic("wdc_atapi_scsipi_request: polled command "
    378 			    "not done");
    379 #endif
    380 		splx(s);
    381 		return;
    382 
    383 	default:
    384 		/* Not supported, nothing to do. */
    385 		;
    386 	}
    387 }
    388 
    389 void
    390 wdc_atapi_start(chp, xfer)
    391 	struct channel_softc *chp;
    392 	struct wdc_xfer *xfer;
    393 {
    394 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    395 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    396 
    397 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    398 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
    399 	    sc_xfer->xs_control), DEBUG_XFERS);
    400 	/* Adjust C_DMA, it may have changed if we are requesting sense */
    401 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    402 	    sc_xfer->datalen > 0 && !(xfer->c_flags & C_FORCEPIO)) {
    403 		if (drvp->n_xfers <= NXFER)
    404 			drvp->n_xfers++;
    405 		xfer->c_flags |= C_DMA;
    406 	} else {
    407 		xfer->c_flags &= ~C_DMA;
    408 	}
    409 	/* start timeout machinery */
    410 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    411 		callout_reset(&chp->ch_callout, sc_xfer->timeout * hz / 1000,
    412 		    wdctimeout, chp);
    413 	/* Do control operations specially. */
    414 	if (drvp->state < READY) {
    415 		if (drvp->state != RESET) {
    416 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
    417 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    418 			    xfer->drive, drvp->state);
    419 			panic("wdc_atapi_start: bad state");
    420 		}
    421 		drvp->state = PIOMODE;
    422 		wdc_atapi_ctrl(chp, xfer, 0);
    423 		return;
    424 	}
    425 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    426 	    WDSD_IBM | (xfer->drive << 4));
    427 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
    428 		printf("wdc_atapi_start: not ready, st = %02x\n",
    429 		    chp->ch_status);
    430 		sc_xfer->error = XS_TIMEOUT;
    431 		wdc_atapi_reset(chp, xfer);
    432 		return;
    433 	}
    434 
    435 	/*
    436 	 * Even with WDCS_ERR, the device should accept a command packet
    437 	 * Limit length to what can be stuffed into the cylinder register
    438 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    439 	 * but not all devices do that and it's not obvious from the
    440 	 * ATAPI spec that that behaviour should be expected.  If more
    441 	 * data is necessary, multiple data transfer phases will be done.
    442 	 */
    443 
    444 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
    445 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    446 	    0, 0, 0,
    447 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    448 
    449 	/*
    450 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    451 	 * the interrupt routine. If it is a polled command, call the interrupt
    452 	 * routine until command is done.
    453 	 */
    454 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
    455 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    456 		/* Wait for at last 400ns for status bit to be valid */
    457 		DELAY(1);
    458 		if (chp->ch_flags & WDCF_DMA_WAIT) {
    459 			wdc_dmawait(chp, xfer, sc_xfer->timeout);
    460 			chp->ch_flags &= ~WDCF_DMA_WAIT;
    461 		}
    462 		wdc_atapi_intr(chp, xfer, 0);
    463 	} else {
    464 		chp->ch_flags |= WDCF_IRQ_WAIT;
    465 	}
    466 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    467 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    468 			/* Wait for at last 400ns for status bit to be valid */
    469 			DELAY(1);
    470 			wdc_atapi_intr(chp, xfer, 0);
    471 		}
    472 	}
    473 }
    474 
    475 int
    476 wdc_atapi_intr(chp, xfer, irq)
    477 	struct channel_softc *chp;
    478 	struct wdc_xfer *xfer;
    479 	int irq;
    480 {
    481 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    482 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    483 	int len, phase, i, retries=0;
    484 	int ire;
    485 	int dma_flags = 0;
    486 	void *cmd;
    487 
    488 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    489 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
    490 
    491 	/* Is it not a transfer, but a control operation? */
    492 	if (drvp->state < READY) {
    493 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    494 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    495 		    drvp->state);
    496 		panic("wdc_atapi_intr: bad state\n");
    497 	}
    498 	/*
    499 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    500 	 * Don't try to continue transfer, we may have missed cycles.
    501 	 */
    502 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    503 		sc_xfer->error = XS_TIMEOUT;
    504 		wdc_atapi_reset(chp, xfer);
    505 		return 1;
    506 	}
    507 
    508 	/* Ack interrupt done in wait_for_unbusy */
    509 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    510 	    WDSD_IBM | (xfer->drive << 4));
    511 	if (wait_for_unbusy(chp,
    512 	    (irq == 0) ? sc_xfer->timeout : 0) != 0) {
    513 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    514 			return 0; /* IRQ was not for us */
    515 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    516 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    517 		    xfer->c_bcount, xfer->c_skip);
    518 		if (xfer->c_flags & C_DMA) {
    519 			ata_dmaerr(drvp);
    520 		}
    521 		sc_xfer->error = XS_TIMEOUT;
    522 		wdc_atapi_reset(chp, xfer);
    523 		return 1;
    524 	}
    525 	if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    526 		chp->wdc->irqack(chp);
    527 
    528 	/* If we missed an IRQ and were using DMA, flag it as a DMA error */
    529 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
    530 		ata_dmaerr(drvp);
    531 	}
    532 	/*
    533 	 * if the request sense command was aborted, report the short sense
    534 	 * previously recorded, else continue normal processing
    535 	 */
    536 
    537 	if (xfer->c_flags & C_DMA)
    538 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    539 		    ?  WDC_DMA_READ : 0;
    540 again:
    541 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
    542 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
    543 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
    544 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    545 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    546 	    "ire 0x%x :", xfer->c_bcount,
    547 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    548 
    549 	switch (phase) {
    550 	case PHASE_CMDOUT:
    551 		cmd  = sc_xfer->cmd;
    552 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    553 		/* Init the DMA channel if necessary */
    554 		if (xfer->c_flags & C_DMA) {
    555 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    556 			    chp->channel, xfer->drive,
    557 			    xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
    558 				sc_xfer->error = XS_DRIVER_STUFFUP;
    559 				break;
    560 			}
    561 		}
    562 		/* send packet command */
    563 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    564 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    565 			if (drvp->drive_flags & DRIVE_CAP32) {
    566 				bus_space_write_multi_4(chp->data32iot,
    567 				    chp->data32ioh, 0,
    568 				    (u_int32_t *)cmd,
    569 				    sc_xfer->cmdlen >> 2);
    570 			} else {
    571 				bus_space_write_multi_2(chp->cmd_iot,
    572 				    chp->cmd_ioh, wd_data,
    573 				    (u_int16_t *)cmd,
    574 				    sc_xfer->cmdlen >> 1);
    575 			}
    576 		} else {
    577 			if (drvp->drive_flags & DRIVE_CAP32) {
    578 				bus_space_write_multi_stream_4(chp->data32iot,
    579 				    chp->data32ioh, 0,
    580 				    (u_int32_t *)cmd,
    581 				    sc_xfer->cmdlen >> 2);
    582 			} else {
    583 				bus_space_write_multi_stream_2(chp->cmd_iot,
    584 				    chp->cmd_ioh, wd_data,
    585 				    (u_int16_t *)cmd,
    586 				    sc_xfer->cmdlen >> 1);
    587 			}
    588 		}
    589 		/* Start the DMA channel if necessary */
    590 		if (xfer->c_flags & C_DMA) {
    591 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    592 			    chp->channel, xfer->drive);
    593 			chp->ch_flags |= WDCF_DMA_WAIT;
    594 		}
    595 
    596 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    597 			chp->ch_flags |= WDCF_IRQ_WAIT;
    598 		}
    599 		return 1;
    600 
    601 	 case PHASE_DATAOUT:
    602 		/* write data */
    603 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    604 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    605 		    (xfer->c_flags & C_DMA) != 0) {
    606 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    607 			if (xfer->c_flags & C_DMA) {
    608 				ata_dmaerr(drvp);
    609 			}
    610 			sc_xfer->error = XS_TIMEOUT;
    611 			wdc_atapi_reset(chp, xfer);
    612 			return 1;
    613 		}
    614 		if (xfer->c_bcount < len) {
    615 			printf("wdc_atapi_intr: warning: write only "
    616 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    617 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    618 				bus_space_write_multi_2(chp->cmd_iot,
    619 				    chp->cmd_ioh, wd_data,
    620 				    (u_int16_t *)((char *)xfer->databuf +
    621 				                  xfer->c_skip),
    622 				    xfer->c_bcount >> 1);
    623 			} else {
    624 				bus_space_write_multi_stream_2(chp->cmd_iot,
    625 				    chp->cmd_ioh, wd_data,
    626 				    (u_int16_t *)((char *)xfer->databuf +
    627 				                  xfer->c_skip),
    628 				    xfer->c_bcount >> 1);
    629 			}
    630 			for (i = xfer->c_bcount; i < len; i += 2)
    631 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
    632 				    wd_data, 0);
    633 			xfer->c_skip += xfer->c_bcount;
    634 			xfer->c_bcount = 0;
    635 		} else {
    636 			if (drvp->drive_flags & DRIVE_CAP32) {
    637 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    638 				bus_space_write_multi_4(chp->data32iot,
    639 				    chp->data32ioh, 0,
    640 				    (u_int32_t *)((char *)xfer->databuf +
    641 				                  xfer->c_skip),
    642 				    len >> 2);
    643 			    else
    644 				bus_space_write_multi_stream_4(chp->data32iot,
    645 				    chp->data32ioh, wd_data,
    646 				    (u_int32_t *)((char *)xfer->databuf +
    647 				                  xfer->c_skip),
    648 				    len >> 2);
    649 
    650 			    xfer->c_skip += len & 0xfffffffc;
    651 			    xfer->c_bcount -= len & 0xfffffffc;
    652 			    len = len & 0x03;
    653 			}
    654 			if (len > 0) {
    655 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    656 				bus_space_write_multi_2(chp->cmd_iot,
    657 				    chp->cmd_ioh, wd_data,
    658 				    (u_int16_t *)((char *)xfer->databuf +
    659 				                  xfer->c_skip),
    660 				    len >> 1);
    661 			    else
    662 				bus_space_write_multi_stream_2(chp->cmd_iot,
    663 				    chp->cmd_ioh, wd_data,
    664 				    (u_int16_t *)((char *)xfer->databuf +
    665 				                  xfer->c_skip),
    666 				    len >> 1);
    667 			    xfer->c_skip += len;
    668 			    xfer->c_bcount -= len;
    669 			}
    670 		}
    671 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    672 			chp->ch_flags |= WDCF_IRQ_WAIT;
    673 		}
    674 		return 1;
    675 
    676 	case PHASE_DATAIN:
    677 		/* Read data */
    678 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    679 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
    680 		    (xfer->c_flags & C_DMA) != 0) {
    681 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    682 			if (xfer->c_flags & C_DMA) {
    683 				ata_dmaerr(drvp);
    684 			}
    685 			sc_xfer->error = XS_TIMEOUT;
    686 			wdc_atapi_reset(chp, xfer);
    687 			return 1;
    688 		}
    689 		if (xfer->c_bcount < len) {
    690 			printf("wdc_atapi_intr: warning: reading only "
    691 			    "%d of %d bytes\n", xfer->c_bcount, len);
    692 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    693 			    bus_space_read_multi_2(chp->cmd_iot,
    694 			    chp->cmd_ioh, wd_data,
    695 			    (u_int16_t *)((char *)xfer->databuf +
    696 			                  xfer->c_skip),
    697 			    xfer->c_bcount >> 1);
    698 			} else {
    699 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    700 			    chp->cmd_ioh, wd_data,
    701 			    (u_int16_t *)((char *)xfer->databuf +
    702 			                  xfer->c_skip),
    703 			    xfer->c_bcount >> 1);
    704 			}
    705 			wdcbit_bucket(chp, len - xfer->c_bcount);
    706 			xfer->c_skip += xfer->c_bcount;
    707 			xfer->c_bcount = 0;
    708 		} else {
    709 			if (drvp->drive_flags & DRIVE_CAP32) {
    710 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    711 				bus_space_read_multi_4(chp->data32iot,
    712 				    chp->data32ioh, 0,
    713 				    (u_int32_t *)((char *)xfer->databuf +
    714 				                  xfer->c_skip),
    715 				    len >> 2);
    716 			    else
    717 				bus_space_read_multi_stream_4(chp->data32iot,
    718 				    chp->data32ioh, wd_data,
    719 				    (u_int32_t *)((char *)xfer->databuf +
    720 				                  xfer->c_skip),
    721 				    len >> 2);
    722 
    723 			    xfer->c_skip += len & 0xfffffffc;
    724 			    xfer->c_bcount -= len & 0xfffffffc;
    725 			    len = len & 0x03;
    726 			}
    727 			if (len > 0) {
    728 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    729 				bus_space_read_multi_2(chp->cmd_iot,
    730 				    chp->cmd_ioh, wd_data,
    731 				    (u_int16_t *)((char *)xfer->databuf +
    732 				                  xfer->c_skip),
    733 				    len >> 1);
    734 			    else
    735 				bus_space_read_multi_stream_2(chp->cmd_iot,
    736 				    chp->cmd_ioh, wd_data,
    737 				    (u_int16_t *)((char *)xfer->databuf +
    738 				                  xfer->c_skip),
    739 				    len >> 1);
    740 			    xfer->c_skip += len;
    741 			    xfer->c_bcount -=len;
    742 			}
    743 		}
    744 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    745 			chp->ch_flags |= WDCF_IRQ_WAIT;
    746 		}
    747 		return 1;
    748 
    749 	case PHASE_ABORTED:
    750 	case PHASE_COMPLETED:
    751 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    752 		/* turn off DMA channel */
    753 		if (xfer->c_flags & C_DMA) {
    754 			xfer->c_bcount -= sc_xfer->datalen;
    755 		}
    756 		sc_xfer->resid = xfer->c_bcount;
    757 		if (chp->ch_status & WDCS_ERR) {
    758 			/* save the short sense */
    759 			sc_xfer->error = XS_SHORTSENSE;
    760 			sc_xfer->sense.atapi_sense = chp->ch_error;
    761 			if ((sc_xfer->xs_periph->periph_quirks &
    762 			    PQUIRK_NOSENSE) == 0) {
    763 				/* ask scsipi to send a REQUEST_SENSE */
    764 				sc_xfer->error = XS_BUSY;
    765 				sc_xfer->status = SCSI_CHECK;
    766 			} else if (chp->wdc->dma_status &
    767 			    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
    768 				ata_dmaerr(drvp);
    769 				sc_xfer->error = XS_RESET;
    770 				wdc_atapi_reset(chp, xfer);
    771 				return (1);
    772 			}
    773 		}
    774 		if (xfer->c_bcount != 0) {
    775 			WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    776 			    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    777 		}
    778 #ifdef DIAGNOSTIC
    779 		if (xfer->c_bcount < 0) {
    780 			printf("wdc_atapi_intr warning: bcount value "
    781 			    "is %d after io\n", xfer->c_bcount);
    782 		}
    783 #endif
    784 		break;
    785 
    786 	default:
    787 		if (++retries<500) {
    788 			DELAY(100);
    789 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    790 			    chp->cmd_ioh, wd_status);
    791 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    792 			    chp->cmd_ioh, wd_error);
    793 			goto again;
    794 		}
    795 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    796 		if (chp->ch_status & WDCS_ERR) {
    797 			sc_xfer->error = XS_SHORTSENSE;
    798 			sc_xfer->sense.atapi_sense = chp->ch_error;
    799 		} else {
    800 			if (xfer->c_flags & C_DMA) {
    801 				ata_dmaerr(drvp);
    802 			}
    803 			sc_xfer->error = XS_RESET;
    804 			wdc_atapi_reset(chp, xfer);
    805 			return (1);
    806 		}
    807 	}
    808 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    809 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    810 	    DEBUG_INTR);
    811 	wdc_atapi_done(chp, xfer);
    812 	return (1);
    813 }
    814 
    815 int
    816 wdc_atapi_ctrl(chp, xfer, irq)
    817 	struct channel_softc *chp;
    818 	struct wdc_xfer *xfer;
    819 	int irq;
    820 {
    821 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    822 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    823 	char *errstring = NULL;
    824 	int delay = (irq == 0) ? ATAPI_DELAY : 0;
    825 
    826 	/* Ack interrupt done in wait_for_unbusy */
    827 again:
    828 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
    829 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
    830 	    DEBUG_INTR | DEBUG_FUNCS);
    831 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    832 	    WDSD_IBM | (xfer->drive << 4));
    833 	switch (drvp->state) {
    834 	case PIOMODE:
    835 		/* Don't try to set mode if controller can't be adjusted */
    836 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    837 			goto ready;
    838 		/* Also don't try if the drive didn't report its mode */
    839 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    840 			goto ready;
    841 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    842 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    843 		drvp->state = PIOMODE_WAIT;
    844 		break;
    845 	case PIOMODE_WAIT:
    846 		errstring = "piomode";
    847 		if (wait_for_unbusy(chp, delay))
    848 			goto timeout;
    849 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    850 			chp->wdc->irqack(chp);
    851 		if (chp->ch_status & WDCS_ERR) {
    852 			if (chp->ch_error == WDCE_ABRT) {
    853 				/*
    854 				 * some ATAPI drives rejects pio settings.
    855 				 * all we can do here is fall back to PIO 0
    856 				 */
    857 				drvp->drive_flags &= ~DRIVE_MODE;
    858 				drvp->drive_flags &= ~(DRIVE_DMA|DRIVE_UDMA);
    859 				drvp->PIO_mode = 0;
    860 				drvp->DMA_mode = 0;
    861 				printf("%s:%d:%d: pio setting rejected, "
    862 				    "falling back to PIO mode 0\n",
    863 				    chp->wdc->sc_dev.dv_xname,
    864 				    chp->channel, xfer->drive);
    865 				chp->wdc->set_modes(chp);
    866 				goto ready;
    867 			}
    868 			goto error;
    869 		}
    870 	/* fall through */
    871 
    872 	case DMAMODE:
    873 		if (drvp->drive_flags & DRIVE_UDMA) {
    874 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    875 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    876 		} else if (drvp->drive_flags & DRIVE_DMA) {
    877 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    878 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    879 		} else {
    880 			goto ready;
    881 		}
    882 		drvp->state = DMAMODE_WAIT;
    883 		break;
    884 	case DMAMODE_WAIT:
    885 		errstring = "dmamode";
    886 		if (wait_for_unbusy(chp, delay))
    887 			goto timeout;
    888 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
    889 			chp->wdc->irqack(chp);
    890 		if (chp->ch_status & WDCS_ERR)
    891 			goto error;
    892 	/* fall through */
    893 
    894 	case READY:
    895 	ready:
    896 		drvp->state = READY;
    897 		xfer->c_intr = wdc_atapi_intr;
    898 		callout_stop(&chp->ch_callout);
    899 		wdc_atapi_start(chp, xfer);
    900 		return 1;
    901 	}
    902 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    903 		chp->ch_flags |= WDCF_IRQ_WAIT;
    904 		xfer->c_intr = wdc_atapi_ctrl;
    905 	} else {
    906 		goto again;
    907 	}
    908 	return 1;
    909 
    910 timeout:
    911 	if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
    912 		return 0; /* IRQ was not for us */
    913 	}
    914 	printf("%s:%d:%d: %s timed out\n",
    915 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    916 	sc_xfer->error = XS_TIMEOUT;
    917 	wdc_atapi_reset(chp, xfer);
    918 	return 1;
    919 error:
    920 	printf("%s:%d:%d: %s ",
    921 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    922 	    errstring);
    923 	printf("error (0x%x)\n", chp->ch_error);
    924 	sc_xfer->error = XS_SHORTSENSE;
    925 	sc_xfer->sense.atapi_sense = chp->ch_error;
    926 	wdc_atapi_reset(chp, xfer);
    927 	return 1;
    928 }
    929 
    930 void
    931 wdc_atapi_done(chp, xfer)
    932 	struct channel_softc *chp;
    933 	struct wdc_xfer *xfer;
    934 {
    935 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    936 
    937 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
    938 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    939 	    (u_int)xfer->c_flags), DEBUG_XFERS);
    940 	callout_stop(&chp->ch_callout);
    941 	/* remove this command from xfer queue */
    942 	wdc_free_xfer(chp, xfer);
    943 
    944 	WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    945 	scsipi_done(sc_xfer);
    946 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
    947 	    chp->ch_flags), DEBUG_XFERS);
    948 	wdcstart(chp);
    949 }
    950 
    951 void
    952 wdc_atapi_reset(chp, xfer)
    953 	struct channel_softc *chp;
    954 	struct wdc_xfer *xfer;
    955 {
    956 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    957 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    958 
    959 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
    960 	drvp->state = 0;
    961 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
    962 		printf("%s:%d:%d: reset failed\n",
    963 		    chp->wdc->sc_dev.dv_xname, chp->channel,
    964 		    xfer->drive);
    965 		sc_xfer->error = XS_SELTIMEOUT;
    966 	}
    967 	wdc_atapi_done(chp, xfer);
    968 	return;
    969 }
    970