Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.27
      1 /*	$NetBSD: atapi_wdc.c,v 1.27 1999/09/30 22:57:52 thorpej 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  */
     35 
     36 #ifndef WDCDEBUG
     37 #define WDCDEBUG
     38 #endif /* WDCDEBUG */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/file.h>
     44 #include <sys/stat.h>
     45 #include <sys/buf.h>
     46 #include <sys/malloc.h>
     47 #include <sys/device.h>
     48 #include <sys/syslog.h>
     49 #include <sys/proc.h>
     50 
     51 #include <vm/vm.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 #include <dev/scsipi/scsipi_all.h>
     68 #include <dev/scsipi/scsipiconf.h>
     69 #include <dev/scsipi/atapiconf.h>
     70 
     71 #define DEBUG_INTR   0x01
     72 #define DEBUG_XFERS  0x02
     73 #define DEBUG_STATUS 0x04
     74 #define DEBUG_FUNCS  0x08
     75 #define DEBUG_PROBE  0x10
     76 #ifdef WDCDEBUG
     77 int wdcdebug_atapi_mask = 0;
     78 #define WDCDEBUG_PRINT(args, level) \
     79 	if (wdcdebug_atapi_mask & (level)) \
     80 		printf args
     81 #else
     82 #define WDCDEBUG_PRINT(args, level)
     83 #endif
     84 
     85 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     86 
     87 void  wdc_atapi_minphys  __P((struct buf *bp));
     88 void  wdc_atapi_start	__P((struct channel_softc *,struct wdc_xfer *));
     89 int   wdc_atapi_intr	 __P((struct channel_softc *, struct wdc_xfer *, int));
     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 int   wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
     94 
     95 #define MAX_SIZE MAXPHYS
     96 
     97 void
     98 wdc_atapibus_attach(chp)
     99 	struct channel_softc *chp;
    100 {
    101 	struct wdc_softc *wdc = chp->wdc;
    102 	int channel = chp->channel;
    103 	struct ata_atapi_attach aa_link;
    104 
    105 	/*
    106 	 * Fill in the adapter.
    107 	 */
    108 	wdc->sc_atapi_adapter.scsipi_cmd = wdc_atapi_send_cmd;
    109 	wdc->sc_atapi_adapter.scsipi_minphys = wdc_atapi_minphys;
    110 
    111 	memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
    112 	aa_link.aa_type = T_ATAPI;
    113 	aa_link.aa_channel = channel;
    114 	aa_link.aa_openings = 1;
    115 	aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
    116 	aa_link.aa_bus_private = &wdc->sc_atapi_adapter;
    117 	chp->atapibus = config_found(&wdc->sc_dev, (void *)&aa_link,
    118 	    atapi_print);
    119 }
    120 
    121 void
    122 wdc_atapi_minphys (struct buf *bp)
    123 {
    124 	if(bp->b_bcount > MAX_SIZE)
    125 		bp->b_bcount = MAX_SIZE;
    126 	minphys(bp);
    127 }
    128 
    129 int
    130 wdc_atapi_get_params(ab_link, drive, flags, id)
    131 	struct scsipi_link *ab_link;
    132 	u_int8_t drive;
    133 	int flags;
    134 	struct ataparams *id;
    135 {
    136 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
    137 	struct channel_softc *chp =
    138 	    wdc->channels[ab_link->scsipi_atapi.channel];
    139 	struct wdc_command wdc_c;
    140 
    141 	/* if no ATAPI device detected at wdc attach time, skip */
    142 	/*
    143 	 * XXX this will break scsireprobe if this is of any interest for
    144 	 * ATAPI devices one day.
    145 	 */
    146 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    147 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    148 		    drive), DEBUG_PROBE);
    149 		return -1;
    150 	}
    151 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    152 	wdc_c.r_command = ATAPI_SOFT_RESET;
    153 	wdc_c.r_st_bmask = 0;
    154 	wdc_c.r_st_pmask = 0;
    155 	wdc_c.flags = AT_POLL;
    156 	wdc_c.timeout = WDC_RESET_WAIT;
    157 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    158 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    159 		    " drive %s:%d:%d: driver failed\n",
    160 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
    161 		panic("wdc_atapi_get_params");
    162 	}
    163 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    164 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    165 		    "failed for drive %s:%d:%d: error 0x%x\n",
    166 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    167 		    wdc_c.r_error), DEBUG_PROBE);
    168 		return -1;
    169 	}
    170 	chp->ch_drive[drive].state = 0;
    171 
    172 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
    173 
    174 	/* Some ATAPI devices need a bit more time after software reset. */
    175 	delay(5000);
    176 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
    177 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    178 		    "failed for drive %s:%d:%d: error 0x%x\n",
    179 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    180 		    wdc_c.r_error), DEBUG_PROBE);
    181 		return -1;
    182 	}
    183 	return COMPLETE;
    184 }
    185 
    186 int
    187 wdc_atapi_send_cmd(sc_xfer)
    188 	struct scsipi_xfer *sc_xfer;
    189 {
    190 	struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc;
    191 	struct wdc_xfer *xfer;
    192 	int flags = sc_xfer->xs_control;
    193 	int channel = sc_xfer->sc_link->scsipi_atapi.channel;
    194 	int drive = sc_xfer->sc_link->scsipi_atapi.drive;
    195 	int s, ret;
    196 
    197 	WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n",
    198 	    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
    199 
    200 	xfer = wdc_get_xfer((flags & XS_CTL_NOSLEEP) ?
    201 	    WDC_NOSLEEP : WDC_CANSLEEP);
    202 	if (xfer == NULL) {
    203 		return TRY_AGAIN_LATER;
    204 	}
    205 	if (sc_xfer->xs_control & XS_CTL_POLL)
    206 		xfer->c_flags |= C_POLL;
    207 	xfer->drive = drive;
    208 	xfer->c_flags |= C_ATAPI;
    209 	xfer->cmd = sc_xfer;
    210 	xfer->databuf = sc_xfer->data;
    211 	xfer->c_bcount = sc_xfer->datalen;
    212 	xfer->c_start = wdc_atapi_start;
    213 	xfer->c_intr = wdc_atapi_intr;
    214 	s = splbio();
    215 	wdc_exec_xfer(wdc->channels[channel], xfer);
    216 #ifdef DIAGNOSTIC
    217 	if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    218 	    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    219 		panic("wdc_atapi_send_cmd: polled command not done");
    220 #endif
    221 	ret = (sc_xfer->xs_status & XS_STS_DONE) ?
    222 	    COMPLETE : SUCCESSFULLY_QUEUED;
    223 	splx(s);
    224 	return ret;
    225 }
    226 
    227 void
    228 wdc_atapi_start(chp, xfer)
    229 	struct channel_softc *chp;
    230 	struct wdc_xfer *xfer;
    231 {
    232 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    233 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    234 
    235 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    236 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
    237 	    sc_xfer->xs_control), DEBUG_XFERS);
    238 	/* Adjust C_DMA, it may have changed if we are requesting sense */
    239 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    240 	    (sc_xfer->datalen > 0 || (xfer->c_flags & C_SENSE)))
    241 		xfer->c_flags |= C_DMA;
    242 	else
    243 		xfer->c_flags &= ~C_DMA;
    244 	/* start timeout machinery */
    245 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    246 		timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    247 	/* Do control operations specially. */
    248 	if (drvp->state < READY) {
    249 		if (drvp->state != PIOMODE) {
    250 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
    251 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    252 			    xfer->drive, drvp->state);
    253 			panic("wdc_atapi_start: bad state");
    254 		}
    255 		wdc_atapi_ctrl(chp, xfer, 0);
    256 		return;
    257 	}
    258 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    259 	    WDSD_IBM | (xfer->drive << 4));
    260 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
    261 		printf("wdc_atapi_start: not ready, st = %02x\n",
    262 		    chp->ch_status);
    263 		sc_xfer->error = XS_TIMEOUT;
    264 		wdc_atapi_reset(chp, xfer);
    265 		return;
    266 	}
    267 
    268 	/*
    269 	 * Even with WDCS_ERR, the device should accept a command packet
    270 	 * Limit length to what can be stuffed into the cylinder register
    271 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    272 	 * but not all devices do that and it's not obvious from the
    273 	 * ATAPI spec that that behaviour should be expected.  If more
    274 	 * data is necessary, multiple data transfer phases will be done.
    275 	 */
    276 
    277 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
    278 	    sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
    279 	    0, 0, 0,
    280 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    281 
    282 	/*
    283 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    284 	 * the interrupt routine. If it is a polled command, call the interrupt
    285 	 * routine until command is done.
    286 	 */
    287 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & ATAPI_CFG_DRQ_MASK) !=
    288 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    289 		/* Wait for at last 400ns for status bit to be valid */
    290 		DELAY(1);
    291 		wdc_atapi_intr(chp, xfer, 0);
    292 	} else {
    293 		chp->ch_flags |= WDCF_IRQ_WAIT;
    294 	}
    295 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    296 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    297 			/* Wait for at last 400ns for status bit to be valid */
    298 			DELAY(1);
    299 			wdc_atapi_intr(chp, xfer, 0);
    300 		}
    301 	}
    302 }
    303 
    304 int
    305 wdc_atapi_intr(chp, xfer, irq)
    306 	struct channel_softc *chp;
    307 	struct wdc_xfer *xfer;
    308 	int irq;
    309 {
    310 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    311 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    312 	int len, phase, i, retries=0;
    313 	int ire, dma_err = 0;
    314 	int dma_flags = 0;
    315 	struct scsipi_generic _cmd_reqsense;
    316 	struct scsipi_sense *cmd_reqsense =
    317 	    (struct scsipi_sense *)&_cmd_reqsense;
    318 	void *cmd;
    319 
    320 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    321 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
    322 
    323 	/* Is it not a transfer, but a control operation? */
    324 	if (drvp->state < READY) {
    325 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    326 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    327 		    drvp->state);
    328 		panic("wdc_atapi_intr: bad state\n");
    329 	}
    330 	/*
    331 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    332 	 * Don't try to continue transfer, we may have missed cycles.
    333 	 */
    334 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    335 		sc_xfer->error = XS_TIMEOUT;
    336 		wdc_atapi_reset(chp, xfer);
    337 		return 1;
    338 	}
    339 
    340 	/* Ack interrupt done in wait_for_unbusy */
    341 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    342 	    WDSD_IBM | (xfer->drive << 4));
    343 	if (wait_for_unbusy(chp,
    344 	    (irq == 0) ? sc_xfer->timeout : 0) != 0) {
    345 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    346 			return 0; /* IRQ was not for us */
    347 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    348 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    349 		    xfer->c_bcount, xfer->c_skip);
    350 		if (xfer->c_flags & C_DMA)
    351 			drvp->n_dmaerrs++;
    352 		sc_xfer->error = XS_TIMEOUT;
    353 		wdc_atapi_reset(chp, xfer);
    354 		return 1;
    355 	}
    356 	/* If we missed an IRQ and were using DMA, flag it as a DMA error */
    357 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA))
    358 		drvp->n_dmaerrs++;
    359 	/*
    360 	 * if the request sense command was aborted, report the short sense
    361 	 * previously recorded, else continue normal processing
    362 	 */
    363 
    364 	if ((xfer->c_flags & C_SENSE) != 0 &&
    365 	    (chp->ch_status & WDCS_ERR) != 0 &&
    366 	    (chp->ch_error & WDCE_ABRT) != 0) {
    367 		WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
    368 		    "calling wdc_atapi_done(), sense 0x%x\n",
    369 		    sc_xfer->sense.atapi_sense), DEBUG_INTR);
    370 		wdc_atapi_done(chp, xfer);
    371 		return 1;
    372 	}
    373 
    374 	if (xfer->c_flags & C_DMA) {
    375 		dma_flags = ((sc_xfer->xs_control & XS_CTL_DATA_IN) ||
    376 		    (xfer->c_flags & C_SENSE)) ?  WDC_DMA_READ : 0;
    377 		dma_flags |= sc_xfer->xs_control & XS_CTL_POLL ?
    378 		    WDC_DMA_POLL : 0;
    379 	}
    380 again:
    381 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
    382 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
    383 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
    384 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    385 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    386 	    "ire 0x%x :", xfer->c_bcount,
    387 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    388 
    389 	switch (phase) {
    390 	case PHASE_CMDOUT:
    391 		if (xfer->c_flags & C_SENSE) {
    392 			memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
    393 			cmd_reqsense->opcode = REQUEST_SENSE;
    394 			cmd_reqsense->length = xfer->c_bcount;
    395 			cmd = cmd_reqsense;
    396 		} else {
    397 			cmd  = sc_xfer->cmd;
    398 		}
    399 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    400 		/* Init the DMA channel if necessary */
    401 		if (xfer->c_flags & C_DMA) {
    402 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    403 			    chp->channel, xfer->drive,
    404 			    xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
    405 				sc_xfer->error = XS_DRIVER_STUFFUP;
    406 				break;
    407 			}
    408 		}
    409 		/* send packet command */
    410 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    411 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    412 			if (drvp->drive_flags & DRIVE_CAP32) {
    413 				bus_space_write_multi_4(chp->data32iot,
    414 				    chp->data32ioh, 0,
    415 				    (u_int32_t *)cmd,
    416 				    sc_xfer->cmdlen >> 2);
    417 			} else {
    418 				bus_space_write_multi_2(chp->cmd_iot,
    419 				    chp->cmd_ioh, wd_data,
    420 				    (u_int16_t *)cmd,
    421 				    sc_xfer->cmdlen >> 1);
    422 			}
    423 		} else {
    424 			if (drvp->drive_flags & DRIVE_CAP32) {
    425 				bus_space_write_multi_stream_4(chp->data32iot,
    426 				    chp->data32ioh, 0,
    427 				    (u_int32_t *)cmd,
    428 				    sc_xfer->cmdlen >> 2);
    429 			} else {
    430 				bus_space_write_multi_stream_2(chp->cmd_iot,
    431 				    chp->cmd_ioh, wd_data,
    432 				    (u_int16_t *)cmd,
    433 				    sc_xfer->cmdlen >> 1);
    434 			}
    435 		}
    436 		/* Start the DMA channel if necessary */
    437 		if (xfer->c_flags & C_DMA) {
    438 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    439 			    chp->channel, xfer->drive, dma_flags);
    440 		}
    441 
    442 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    443 			chp->ch_flags |= WDCF_IRQ_WAIT;
    444 		}
    445 		return 1;
    446 
    447 	 case PHASE_DATAOUT:
    448 		/* write data */
    449 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    450 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    451 		    (xfer->c_flags & C_DMA) != 0) {
    452 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    453 			if (xfer->c_flags & C_DMA) {
    454 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    455 				    chp->channel, xfer->drive, dma_flags);
    456 				drvp->n_dmaerrs++;
    457 			}
    458 			sc_xfer->error = XS_TIMEOUT;
    459 			wdc_atapi_reset(chp, xfer);
    460 			return 1;
    461 		}
    462 		if (xfer->c_bcount < len) {
    463 			printf("wdc_atapi_intr: warning: write only "
    464 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    465 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    466 				bus_space_write_multi_2(chp->cmd_iot,
    467 				    chp->cmd_ioh, wd_data,
    468 				    (u_int16_t *)((char *)xfer->databuf +
    469 				                  xfer->c_skip),
    470 				    xfer->c_bcount >> 1);
    471 			} else {
    472 				bus_space_write_multi_stream_2(chp->cmd_iot,
    473 				    chp->cmd_ioh, wd_data,
    474 				    (u_int16_t *)((char *)xfer->databuf +
    475 				                  xfer->c_skip),
    476 				    xfer->c_bcount >> 1);
    477 			}
    478 			for (i = xfer->c_bcount; i < len; i += 2)
    479 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
    480 				    wd_data, 0);
    481 			xfer->c_skip += xfer->c_bcount;
    482 			xfer->c_bcount = 0;
    483 		} else {
    484 			if (drvp->drive_flags & DRIVE_CAP32) {
    485 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    486 				bus_space_write_multi_4(chp->data32iot,
    487 				    chp->data32ioh, 0,
    488 				    (u_int32_t *)((char *)xfer->databuf +
    489 				                  xfer->c_skip),
    490 				    len >> 2);
    491 			    else
    492 				bus_space_write_multi_stream_4(chp->data32iot,
    493 				    chp->data32ioh, wd_data,
    494 				    (u_int32_t *)((char *)xfer->databuf +
    495 				                  xfer->c_skip),
    496 				    len >> 2);
    497 
    498 			    xfer->c_skip += len & 0xfffffffc;
    499 			    xfer->c_bcount -= len & 0xfffffffc;
    500 			    len = len & 0x03;
    501 			}
    502 			if (len > 0) {
    503 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    504 				bus_space_write_multi_2(chp->cmd_iot,
    505 				    chp->cmd_ioh, wd_data,
    506 				    (u_int16_t *)((char *)xfer->databuf +
    507 				                  xfer->c_skip),
    508 				    len >> 1);
    509 			    else
    510 				bus_space_write_multi_stream_2(chp->cmd_iot,
    511 				    chp->cmd_ioh, wd_data,
    512 				    (u_int16_t *)((char *)xfer->databuf +
    513 				                  xfer->c_skip),
    514 				    len >> 1);
    515 			    xfer->c_skip += len;
    516 			    xfer->c_bcount -= len;
    517 			}
    518 		}
    519 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    520 			chp->ch_flags |= WDCF_IRQ_WAIT;
    521 		}
    522 		return 1;
    523 
    524 	case PHASE_DATAIN:
    525 		/* Read data */
    526 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    527 		if (((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 &&
    528 		    (xfer->c_flags & C_SENSE) == 0) ||
    529 		    (xfer->c_flags & C_DMA) != 0) {
    530 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    531 			if (xfer->c_flags & C_DMA) {
    532 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    533 				    chp->channel, xfer->drive, dma_flags);
    534 				drvp->n_dmaerrs++;
    535 			}
    536 			sc_xfer->error = XS_TIMEOUT;
    537 			wdc_atapi_reset(chp, xfer);
    538 			return 1;
    539 		}
    540 		if (xfer->c_bcount < len) {
    541 			printf("wdc_atapi_intr: warning: reading only "
    542 			    "%d of %d bytes\n", xfer->c_bcount, len);
    543 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    544 			    bus_space_read_multi_2(chp->cmd_iot,
    545 			    chp->cmd_ioh, wd_data,
    546 			    (u_int16_t *)((char *)xfer->databuf +
    547 			                  xfer->c_skip),
    548 			    xfer->c_bcount >> 1);
    549 			} else {
    550 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    551 			    chp->cmd_ioh, wd_data,
    552 			    (u_int16_t *)((char *)xfer->databuf +
    553 			                  xfer->c_skip),
    554 			    xfer->c_bcount >> 1);
    555 			}
    556 			wdcbit_bucket(chp, len - xfer->c_bcount);
    557 			xfer->c_skip += xfer->c_bcount;
    558 			xfer->c_bcount = 0;
    559 		} else {
    560 			if (drvp->drive_flags & DRIVE_CAP32) {
    561 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    562 				bus_space_read_multi_4(chp->data32iot,
    563 				    chp->data32ioh, 0,
    564 				    (u_int32_t *)((char *)xfer->databuf +
    565 				                  xfer->c_skip),
    566 				    len >> 2);
    567 			    else
    568 				bus_space_read_multi_stream_4(chp->data32iot,
    569 				    chp->data32ioh, wd_data,
    570 				    (u_int32_t *)((char *)xfer->databuf +
    571 				                  xfer->c_skip),
    572 				    len >> 2);
    573 
    574 			    xfer->c_skip += len & 0xfffffffc;
    575 			    xfer->c_bcount -= len & 0xfffffffc;
    576 			    len = len & 0x03;
    577 			}
    578 			if (len > 0) {
    579 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    580 				bus_space_read_multi_2(chp->cmd_iot,
    581 				    chp->cmd_ioh, wd_data,
    582 				    (u_int16_t *)((char *)xfer->databuf +
    583 				                  xfer->c_skip),
    584 				    len >> 1);
    585 			    else
    586 				bus_space_read_multi_stream_2(chp->cmd_iot,
    587 				    chp->cmd_ioh, wd_data,
    588 				    (u_int16_t *)((char *)xfer->databuf +
    589 				                  xfer->c_skip),
    590 				    len >> 1);
    591 			    xfer->c_skip += len;
    592 			    xfer->c_bcount -=len;
    593 			}
    594 		}
    595 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    596 			chp->ch_flags |= WDCF_IRQ_WAIT;
    597 		}
    598 		return 1;
    599 
    600 	case PHASE_ABORTED:
    601 	case PHASE_COMPLETED:
    602 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    603 		/* turn off DMA channel */
    604 		if (xfer->c_flags & C_DMA) {
    605 			dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    606 			    chp->channel, xfer->drive, dma_flags);
    607 			if (xfer->c_flags & C_SENSE)
    608 				xfer->c_bcount -=
    609 				    sizeof(sc_xfer->sense.scsi_sense);
    610 			else
    611 				xfer->c_bcount -= sc_xfer->datalen;
    612 		}
    613 		if (xfer->c_flags & C_SENSE) {
    614 			if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
    615 				/*
    616 				 * request sense failed ! it's not suppossed
    617 				 * to be possible
    618 				 */
    619 				if (dma_err < 0)
    620 					drvp->n_dmaerrs++;
    621 				sc_xfer->error = XS_RESET;
    622 				wdc_atapi_reset(chp, xfer);
    623 				return (1);
    624 			} else if (xfer->c_bcount <
    625 			    sizeof(sc_xfer->sense.scsi_sense)) {
    626 				/* use the sense we just read */
    627 				sc_xfer->error = XS_SENSE;
    628 			} else {
    629 				/*
    630 				 * command completed, but no data was read.
    631 				 * use the short sense we saved previsouly.
    632 				 */
    633 				sc_xfer->error = XS_SHORTSENSE;
    634 			}
    635 		} else {
    636 			sc_xfer->resid = xfer->c_bcount;
    637 			if (chp->ch_status & WDCS_ERR) {
    638 				/* save the short sense */
    639 				sc_xfer->error = XS_SHORTSENSE;
    640 				sc_xfer->sense.atapi_sense = chp->ch_error;
    641 				if ((sc_xfer->sc_link->quirks &
    642 				    ADEV_NOSENSE) == 0) {
    643 					/*
    644 					 * let the driver issue a
    645 					 * 'request sense'
    646 					 */
    647 					xfer->databuf = &sc_xfer->sense;
    648 					xfer->c_bcount =
    649 					    sizeof(sc_xfer->sense.scsi_sense);
    650 					xfer->c_skip = 0;
    651 					xfer->c_flags |= C_SENSE;
    652 					untimeout(wdctimeout, chp);
    653 					wdc_atapi_start(chp, xfer);
    654 					return 1;
    655 				}
    656 			} else if (dma_err < 0) {
    657 				drvp->n_dmaerrs++;
    658 				sc_xfer->error = XS_RESET;
    659 				wdc_atapi_reset(chp, xfer);
    660 				return (1);
    661 			}
    662 		}
    663 		if (xfer->c_bcount != 0) {
    664 			WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    665 			    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    666 		}
    667 #ifdef DIAGNOSTIC
    668 		if (xfer->c_bcount < 0) {
    669 			printf("wdc_atapi_intr warning: bcount value "
    670 			    "is %d after io\n", xfer->c_bcount);
    671 		}
    672 #endif
    673 		break;
    674 
    675 	default:
    676 		if (++retries<500) {
    677 			DELAY(100);
    678 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    679 			    chp->cmd_ioh, wd_status);
    680 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    681 			    chp->cmd_ioh, wd_error);
    682 			goto again;
    683 		}
    684 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    685 		if (chp->ch_status & WDCS_ERR) {
    686 			sc_xfer->error = XS_SHORTSENSE;
    687 			sc_xfer->sense.atapi_sense = chp->ch_error;
    688 		} else {
    689 			sc_xfer->error = XS_RESET;
    690 			wdc_atapi_reset(chp, xfer);
    691 			return (1);
    692 		}
    693 	}
    694 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    695 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    696 	    DEBUG_INTR);
    697 	wdc_atapi_done(chp, xfer);
    698 	return (1);
    699 }
    700 
    701 int
    702 wdc_atapi_ctrl(chp, xfer, irq)
    703 	struct channel_softc *chp;
    704 	struct wdc_xfer *xfer;
    705 	int irq;
    706 {
    707 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    708 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    709 	char *errstring = NULL;
    710 	int delay = (irq == 0) ? ATAPI_DELAY : 0;
    711 
    712 	/* Ack interrupt done in wait_for_unbusy */
    713 again:
    714 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
    715 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
    716 	    DEBUG_INTR | DEBUG_FUNCS);
    717 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    718 	    WDSD_IBM | (xfer->drive << 4));
    719 	switch (drvp->state) {
    720 	case PIOMODE:
    721 piomode:
    722 		/* Don't try to set mode if controller can't be adjusted */
    723 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    724 			goto ready;
    725 		/* Also don't try if the drive didn't report its mode */
    726 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    727 			goto ready;;
    728 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    729 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    730 		drvp->state = PIOMODE_WAIT;
    731 		break;
    732 	case PIOMODE_WAIT:
    733 		errstring = "piomode";
    734 		if (wait_for_unbusy(chp, delay))
    735 			goto timeout;
    736 		if (chp->ch_status & WDCS_ERR) {
    737 			if (drvp->PIO_mode < 3) {
    738 				drvp->PIO_mode = 3;
    739 				goto piomode;
    740 			} else {
    741 				goto error;
    742 			}
    743 		}
    744 	/* fall through */
    745 
    746 	case DMAMODE:
    747 		if (drvp->drive_flags & DRIVE_UDMA) {
    748 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    749 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    750 		} else if (drvp->drive_flags & DRIVE_DMA) {
    751 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    752 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    753 		} else {
    754 			goto ready;
    755 		}
    756 		drvp->state = DMAMODE_WAIT;
    757 		break;
    758 	case DMAMODE_WAIT:
    759 		errstring = "dmamode";
    760 		if (wait_for_unbusy(chp, delay))
    761 			goto timeout;
    762 		if (chp->ch_status & WDCS_ERR)
    763 			goto error;
    764 	/* fall through */
    765 
    766 	case READY:
    767 	ready:
    768 		drvp->state = READY;
    769 		xfer->c_intr = wdc_atapi_intr;
    770 		untimeout(wdctimeout, chp);
    771 		wdc_atapi_start(chp, xfer);
    772 		return 1;
    773 	}
    774 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    775 		chp->ch_flags |= WDCF_IRQ_WAIT;
    776 		xfer->c_intr = wdc_atapi_ctrl;
    777 	} else {
    778 		goto again;
    779 	}
    780 	return 1;
    781 
    782 timeout:
    783 	if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
    784 		return 0; /* IRQ was not for us */
    785 	}
    786 	printf("%s:%d:%d: %s timed out\n",
    787 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    788 	sc_xfer->error = XS_TIMEOUT;
    789 	wdc_atapi_reset(chp, xfer);
    790 	return 1;
    791 error:
    792 	printf("%s:%d:%d: %s ",
    793 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    794 	    errstring);
    795 	printf("error (0x%x)\n", chp->ch_error);
    796 	sc_xfer->error = XS_SHORTSENSE;
    797 	sc_xfer->sense.atapi_sense = chp->ch_error;
    798 	wdc_atapi_reset(chp, xfer);
    799 	return 1;
    800 }
    801 
    802 void
    803 wdc_atapi_done(chp, xfer)
    804 	struct channel_softc *chp;
    805 	struct wdc_xfer *xfer;
    806 {
    807 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    808 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    809 
    810 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
    811 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    812 	    (u_int)xfer->c_flags), DEBUG_XFERS);
    813 	untimeout(wdctimeout, chp);
    814 	/* remove this command from xfer queue */
    815 	wdc_free_xfer(chp, xfer);
    816 	sc_xfer->xs_status |= XS_STS_DONE;
    817 	if (drvp->n_dmaerrs ||
    818 	  (sc_xfer->error != XS_NOERROR && sc_xfer->error != XS_SENSE &&
    819 	  sc_xfer->error != XS_SHORTSENSE)) {
    820 		drvp->n_dmaerrs = 0;
    821 		wdc_downgrade_mode(drvp);
    822 	}
    823 
    824 	WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    825 	scsipi_done(sc_xfer);
    826 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
    827 	    chp->ch_flags), DEBUG_XFERS);
    828 	wdcstart(chp);
    829 }
    830 
    831 void
    832 wdc_atapi_reset(chp, xfer)
    833 	struct channel_softc *chp;
    834 	struct wdc_xfer *xfer;
    835 {
    836 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    837 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    838 
    839 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
    840 	drvp->state = 0;
    841 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
    842 		printf("%s:%d:%d: reset failed\n",
    843 		    chp->wdc->sc_dev.dv_xname, chp->channel,
    844 		    xfer->drive);
    845 		sc_xfer->error = XS_SELTIMEOUT;
    846 	}
    847 	wdc_atapi_done(chp, xfer);
    848 	return;
    849 }
    850