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