Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.4
      1 /*	$NetBSD: atapi_wdc.c,v 1.4 1998/10/13 09:34:01 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 #define WDCDEBUG
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/file.h>
     42 #include <sys/stat.h>
     43 #include <sys/buf.h>
     44 #include <sys/malloc.h>
     45 #include <sys/device.h>
     46 #include <sys/syslog.h>
     47 #include <sys/proc.h>
     48 
     49 #include <vm/vm.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 #include <dev/scsipi/scsipi_all.h>
     66 #include <dev/scsipi/scsipiconf.h>
     67 #include <dev/scsipi/atapiconf.h>
     68 
     69 #define DEBUG_INTR   0x01
     70 #define DEBUG_XFERS  0x02
     71 #define DEBUG_STATUS 0x04
     72 #define DEBUG_FUNCS  0x08
     73 #define DEBUG_PROBE  0x10
     74 #ifdef WDCDEBUG
     75 int wdcdebug_atapi_mask = 0;
     76 #define WDCDEBUG_PRINT(args, level) \
     77 	if (wdcdebug_atapi_mask & (level)) \
     78 		printf args
     79 #else
     80 #define WDCDEBUG_PRINT(args, level)
     81 #endif
     82 
     83 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     84 
     85 void  wdc_atapi_minphys  __P((struct buf *bp));
     86 void  wdc_atapi_start	__P((struct channel_softc *,struct wdc_xfer *));
     87 int   wdc_atapi_intr	 __P((struct channel_softc *, struct wdc_xfer *));
     88 int   wdc_atapi_ctrl	 __P((struct channel_softc *, struct wdc_xfer *));
     89 void  wdc_atapi_done	 __P((struct channel_softc *, struct wdc_xfer *));
     90 void  wdc_atapi_reset	 __P((struct channel_softc *, struct wdc_xfer *));
     91 int   wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
     92 
     93 #define MAX_SIZE MAXPHYS
     94 
     95 static struct scsipi_adapter wdc_switch  = {
     96 	wdc_atapi_send_cmd,
     97 	wdc_atapi_minphys,
     98 	NULL,                   /* scsipi_ioctl */
     99 };
    100 
    101 void
    102 wdc_atapibus_attach(chp)
    103 	struct channel_softc *chp;
    104 {
    105 	struct wdc_softc *wdc = chp->wdc;
    106 	int channel = chp->channel;
    107 	struct ata_atapi_attach aa_link;
    108 
    109 	memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
    110 	aa_link.aa_type = T_ATAPI;
    111 	aa_link.aa_channel = channel;
    112 	aa_link.aa_openings = 1;
    113 	aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
    114 	aa_link.aa_bus_private = &wdc_switch;
    115 	(void)config_found(&wdc->sc_dev, (void *)&aa_link, atapi_print);
    116 }
    117 
    118 void
    119 wdc_atapi_minphys (struct buf *bp)
    120 {
    121 	if(bp->b_bcount > MAX_SIZE)
    122 		bp->b_bcount = MAX_SIZE;
    123 	minphys(bp);
    124 }
    125 
    126 int
    127 wdc_atapi_get_params(ab_link, drive, flags, id)
    128 	struct scsipi_link *ab_link;
    129 	u_int8_t drive;
    130 	int flags;
    131 	struct ataparams *id;
    132 {
    133 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
    134 	struct channel_softc *chp =
    135 	    &wdc->channels[ab_link->scsipi_atapi.channel];
    136 	struct wdc_command wdc_c;
    137 
    138 	/* if no ATAPI device detected at wdc attach time, skip */
    139 	/*
    140 	 * XXX this will break scsireprobe if this is of any interest for
    141 	 * ATAPI devices one day.
    142 	 */
    143 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    144 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    145 		    drive), DEBUG_PROBE);
    146 		return -1;
    147 	}
    148 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    149 	wdc_c.r_command = ATAPI_SOFT_RESET;
    150 	wdc_c.r_st_bmask = 0;
    151 	wdc_c.r_st_pmask = 0;
    152 	wdc_c.flags = AT_POLL;
    153 	wdc_c.timeout = WDC_RESET_WAIT;
    154 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    155 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    156 		    " drive %s:%d:%d: driver failed\n",
    157 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
    158 		panic("wdc_atapi_get_params");
    159 	}
    160 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    161 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    162 		    "failed for drive %s:%d:%d: error 0x%x\n",
    163 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    164 		    wdc_c.r_error), DEBUG_PROBE);
    165 		return -1;
    166 	}
    167 	chp->ch_drive[drive].state = 0;
    168 
    169 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
    170 
    171 	/* Some ATAPI devices need a bit more time after software reset. */
    172 	delay(5000);
    173 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
    174 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    175 		    "failed for drive %s:%d:%d: error 0x%x\n",
    176 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    177 		    wdc_c.r_error), DEBUG_PROBE);
    178 		return -1;
    179 	}
    180 	return COMPLETE;
    181 }
    182 
    183 int
    184 wdc_atapi_send_cmd(sc_xfer)
    185 	struct scsipi_xfer *sc_xfer;
    186 {
    187 	struct scsipi_link *sc_link = sc_xfer->sc_link;
    188 	struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
    189 	struct wdc_xfer *xfer;
    190 	struct ata_drive_datas *drvp;
    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\n"), DEBUG_XFERS);
    197 
    198 	xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? WDC_NOSLEEP : WDC_CANSLEEP);
    199 	if (xfer == NULL) {
    200 		return TRY_AGAIN_LATER;
    201 	}
    202 	if (sc_xfer->flags & SCSI_POLL)
    203 		xfer->c_flags |= C_POLL;
    204 	drvp = &wdc->channels[channel].ch_drive[drive];
    205 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    206 	    sc_xfer->datalen > 0)
    207 		xfer->c_flags |= C_DMA;
    208 	xfer->drive = drive;
    209 	xfer->c_flags |= C_ATAPI;
    210 	xfer->cmd = sc_xfer;
    211 	xfer->databuf = sc_xfer->data;
    212 	xfer->c_bcount = sc_xfer->datalen;
    213 	xfer->c_start = wdc_atapi_start;
    214 	xfer->c_intr = wdc_atapi_intr;
    215 	s = splbio();
    216 	wdc_exec_xfer(&wdc->channels[channel], xfer);
    217 #ifdef DIAGNOSTIC
    218 	if ((sc_xfer->flags & SCSI_POLL) != 0 &&
    219 	    (sc_xfer->flags & ITSDONE) == 0)
    220 		panic("wdc_atapi_send_cmd: polled command not done");
    221 #endif
    222 	ret = (sc_xfer->flags & ITSDONE) ? 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, scsi flags 0x%x \n",sc_xfer->flags),
    236 	    DEBUG_XFERS);
    237 	/* Do control operations specially. */
    238 	if (drvp->state < READY) {
    239 		if (drvp->state != PIOMODE) {
    240 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
    241 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    242 			    xfer->drive, drvp->state);
    243 			panic("wdc_atapi_start: bad state");
    244 		}
    245 		wdc_atapi_ctrl(chp, xfer);
    246 		return;
    247 	}
    248 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    249 	    WDSD_IBM | (xfer->drive << 4));
    250 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
    251 		printf("wdc_atapi_start: not ready, st = %02x\n",
    252 		    chp->ch_status);
    253 		sc_xfer->error = XS_TIMEOUT;
    254 		wdc_atapi_reset(chp, xfer);
    255 		return;
    256 	}
    257 
    258 	/*
    259 	 * Even with WDCS_ERR, the device should accept a command packet
    260 	 * Limit length to what can be stuffed into the cylinder register
    261 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    262 	 * but not all devices do that and it's not obvious from the
    263 	 * ATAPI spec that that behaviour should be expected.  If more
    264 	 * data is necessary, multiple data transfer phases will be done.
    265 	 */
    266 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
    267 	    sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
    268 	    0, 0, 0,
    269 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    270 
    271 	/*
    272 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    273 	 * the interrupt routine. If it is a polled command, call the interrupt
    274 	 * routine until command is done.
    275 	 */
    276 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & 0x0300) != ACAP_DRQ_INTR ||
    277 	    sc_xfer->flags & SCSI_POLL) {
    278 		/* Wait for at last 400ns for status bit to be valid */
    279 		delay(1);
    280 		if (wdc_atapi_intr(chp, xfer) == 0) {
    281 			sc_xfer->error = XS_TIMEOUT;
    282 			wdc_atapi_reset(chp, xfer);
    283 			return;
    284 		}
    285 	}
    286 	if (sc_xfer->flags & SCSI_POLL) {
    287 		while ((sc_xfer->flags & ITSDONE) == 0) {
    288 			/* Wait for at last 400ns for status bit to be valid */
    289 			delay(1);
    290 			if (wdc_atapi_intr(chp, xfer) == 0) {
    291 				sc_xfer->error = XS_SELTIMEOUT;
    292 				    /* do we know more ? */
    293 				wdc_atapi_done(chp, xfer);
    294 				return;
    295 			}
    296 		}
    297 	}
    298 }
    299 
    300 int
    301 wdc_atapi_intr(chp, xfer)
    302 	struct channel_softc *chp;
    303 	struct wdc_xfer *xfer;
    304 {
    305 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    306 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    307 	int len, phase, i, retries=0;
    308 	int ire, dma_err = 0;
    309 	int dma_flags = 0;
    310 
    311 	WDCDEBUG_PRINT(("wdc_atapi_intr\n"), DEBUG_INTR);
    312 
    313 	/* Is it not a transfer, but a control operation? */
    314 	if (drvp->state < READY) {
    315 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    316 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    317 		    drvp->state);
    318 		panic("wdc_atapi_intr: bad state\n");
    319 	}
    320 	/* Ack interrupt done in wait_for_unbusy */
    321 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    322 	    WDSD_IBM | (xfer->drive << 4));
    323 	if (wait_for_unbusy(chp, sc_xfer->timeout) != 0) {
    324 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
    325 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    326 		    xfer->c_bcount, xfer->c_skip);
    327 		sc_xfer->error = XS_TIMEOUT;
    328 		wdc_atapi_reset(chp, xfer);
    329 		return 1;
    330 	}
    331 	/* Error here only if the command is aborted */
    332 	if ((chp->ch_status & WDCS_ERR) != 0 &&
    333 	    (chp->ch_error & WDCE_ABRT) != 0) {
    334 		sc_xfer->error = XS_SENSE;
    335 		sc_xfer->sense.atapi_sense = chp->ch_error;
    336 		WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done(), "
    337 		    "sense 0x%x\n", sc_xfer->sense.atapi_sense), DEBUG_INTR);
    338 		wdc_atapi_done(chp, xfer);
    339 		return 1;
    340 	}
    341 
    342 	if (xfer->c_flags & C_DMA) {
    343 		dma_flags = (sc_xfer->flags & SCSI_DATA_IN) ?
    344 		    WDC_DMA_READ : 0;
    345 		dma_flags |= sc_xfer->flags & SCSI_POLL ? WDC_DMA_POLL : 0;
    346 	}
    347 again:
    348 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
    349 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
    350 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
    351 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    352 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    353 	    "ire 0x%x :", xfer->c_bcount,
    354 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    355 
    356 	switch (phase) {
    357 	case PHASE_CMDOUT:
    358 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    359 		/* Init the DMA channel if necessary */
    360 		if (xfer->c_flags & C_DMA) {
    361 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    362 			    chp->channel, xfer->drive,
    363 			    xfer->databuf, sc_xfer->datalen, dma_flags) != 0) {
    364 				sc_xfer->error = XS_DRIVER_STUFFUP;
    365 				break;
    366 			}
    367 		}
    368 		/* send packet command */
    369 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    370 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    371 			if (drvp->drive_flags & DRIVE_CAP32) {
    372 				bus_space_write_multi_4(chp->data32iot,
    373 				    chp->data32ioh, 0,
    374 				    (u_int32_t *)sc_xfer->cmd,
    375 				    sc_xfer->cmdlen >> 2);
    376 			} else {
    377 				bus_space_write_multi_2(chp->cmd_iot,
    378 				    chp->cmd_ioh, wd_data,
    379 				    (u_int16_t *)sc_xfer->cmd,
    380 				    sc_xfer->cmdlen >> 1);
    381 			}
    382 		} else {
    383 			if (drvp->drive_flags & DRIVE_CAP32) {
    384 				bus_space_write_multi_stream_4(chp->data32iot,
    385 				    chp->data32ioh, 0,
    386 				    (u_int32_t *)sc_xfer->cmd,
    387 				    sc_xfer->cmdlen >> 2);
    388 			} else {
    389 				bus_space_write_multi_stream_2(chp->cmd_iot,
    390 				    chp->cmd_ioh, wd_data,
    391 				    (u_int16_t *)sc_xfer->cmd,
    392 				    sc_xfer->cmdlen >> 1);
    393 			}
    394 		}
    395 		/* Start the DMA channel if necessary */
    396 		if (xfer->c_flags & C_DMA) {
    397 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    398 			    chp->channel, xfer->drive, dma_flags);
    399 		}
    400 
    401 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
    402 			chp->ch_flags |= WDCF_IRQ_WAIT;
    403 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    404 		}
    405 		return 1;
    406 
    407 	 case PHASE_DATAOUT:
    408 		/* write data */
    409 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    410 		if ((sc_xfer->flags & SCSI_DATA_OUT) == 0 ||
    411 		    (xfer->c_flags & C_DMA) != 0) {
    412 			printf("wdc_atapi_intr: bad data phase DATAOUT");
    413 			if (xfer->c_flags & C_DMA) {
    414 				printf(", falling back to PIO\n");
    415 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    416 			}
    417 			sc_xfer->error = XS_TIMEOUT;
    418 			wdc_atapi_reset(chp, xfer);
    419 			return 1;
    420 		}
    421 		if (xfer->c_bcount < len) {
    422 			printf("wdc_atapi_intr: warning: write only "
    423 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    424 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    425 				bus_space_write_multi_2(chp->cmd_iot,
    426 				    chp->cmd_ioh, wd_data,
    427 				    xfer->databuf + xfer->c_skip,
    428 				    xfer->c_bcount >> 1);
    429 			} else {
    430 				bus_space_write_multi_stream_2(chp->cmd_iot,
    431 				    chp->cmd_ioh, wd_data,
    432 				    xfer->databuf + xfer->c_skip,
    433 				    xfer->c_bcount >> 1);
    434 			}
    435 			for (i = xfer->c_bcount; i < len; i += 2)
    436 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
    437 				    wd_data, 0);
    438 			xfer->c_skip += xfer->c_bcount;
    439 			xfer->c_bcount = 0;
    440 		} else {
    441 			if (drvp->drive_flags & DRIVE_CAP32) {
    442 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    443 				bus_space_write_multi_4(chp->data32iot,
    444 				    chp->data32ioh, 0,
    445 				    xfer->databuf + xfer->c_skip, len >> 2);
    446 			    else
    447 				bus_space_write_multi_stream_4(chp->cmd_iot,
    448 				    chp->cmd_ioh, wd_data,
    449 				    xfer->databuf + xfer->c_skip, len >> 2);
    450 
    451 			    xfer->c_skip += len & 0xfffffffc;
    452 			    xfer->c_bcount -= len & 0xfffffffc;
    453 			    len = len & 0x03;
    454 			}
    455 			if (len > 0) {
    456 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    457 				bus_space_write_multi_2(chp->cmd_iot,
    458 				    chp->cmd_ioh, wd_data,
    459 				    xfer->databuf + xfer->c_skip, len >> 1);
    460 			    else
    461 				bus_space_write_multi_stream_2(chp->cmd_iot,
    462 				    chp->cmd_ioh, wd_data,
    463 				    xfer->databuf + xfer->c_skip, len >> 1);
    464 			    xfer->c_skip += len;
    465 			    xfer->c_bcount -= len;
    466 			}
    467 		}
    468 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
    469 			chp->ch_flags |= WDCF_IRQ_WAIT;
    470 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    471 		}
    472 		return 1;
    473 
    474 	case PHASE_DATAIN:
    475 		/* Read data */
    476 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    477 		if ((sc_xfer->flags & SCSI_DATA_IN) == 0 ||
    478 		    (xfer->c_flags & C_DMA) != 0) {
    479 			printf("wdc_atapi_intr: bad data phase DATAIN");
    480 			if (xfer->c_flags & C_DMA) {
    481 				printf(", falling back to PIO\n");
    482 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    483 			}
    484 			sc_xfer->error = XS_TIMEOUT;
    485 			wdc_atapi_reset(chp, xfer);
    486 			return 1;
    487 		}
    488 		if (xfer->c_bcount < len) {
    489 			printf("wdc_atapi_intr: warning: reading only "
    490 			    "%d of %d bytes\n", xfer->c_bcount, len);
    491 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    492 			    bus_space_read_multi_2(chp->cmd_iot,
    493 			    chp->cmd_ioh, wd_data,
    494 			    xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
    495 			} else {
    496 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    497 			    chp->cmd_ioh, wd_data,
    498 			    xfer->databuf + xfer->c_skip, xfer->c_bcount >> 1);
    499 			}
    500 			wdcbit_bucket(chp, len - xfer->c_bcount);
    501 			xfer->c_skip += xfer->c_bcount;
    502 			xfer->c_bcount = 0;
    503 		} else {
    504 			if (drvp->drive_flags & DRIVE_CAP32) {
    505 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    506 				bus_space_read_multi_4(chp->data32iot,
    507 				    chp->data32ioh, 0,
    508 				    xfer->databuf + xfer->c_skip, len >> 2);
    509 			    else
    510 				bus_space_read_multi_stream_4(chp->cmd_iot,
    511 				    chp->cmd_ioh, wd_data,
    512 				    xfer->databuf + xfer->c_skip, len >> 2);
    513 
    514 			    xfer->c_skip += len & 0xfffffffc;
    515 			    xfer->c_bcount -= len & 0xfffffffc;
    516 			    len = len & 0x03;
    517 			}
    518 			if (len > 0) {
    519 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    520 				bus_space_read_multi_2(chp->cmd_iot,
    521 				    chp->cmd_ioh, wd_data,
    522 				    xfer->databuf + xfer->c_skip, len >> 1);
    523 			    else
    524 				bus_space_read_multi_stream_2(chp->cmd_iot,
    525 				    chp->cmd_ioh, wd_data,
    526 				    xfer->databuf + xfer->c_skip, len >> 1);
    527 			    xfer->c_skip += len;
    528 			    xfer->c_bcount -=len;
    529 			}
    530 		}
    531 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
    532 			chp->ch_flags |= WDCF_IRQ_WAIT;
    533 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    534 		}
    535 		return 1;
    536 
    537 	case PHASE_ABORTED:
    538 	case PHASE_COMPLETED:
    539 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    540 		/* turn off DMA channel */
    541 		if (xfer->c_flags & C_DMA) {
    542 			dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    543 			    chp->channel, xfer->drive, dma_flags);
    544 			xfer->c_bcount -= sc_xfer->datalen;
    545 		}
    546 
    547 		if (chp->ch_status & WDCS_ERR) {
    548 			sc_xfer->error = XS_SENSE;
    549 			sc_xfer->sense.atapi_sense = chp->ch_error;
    550 		} else if (dma_err < 0) {
    551 			sc_xfer->error = XS_DRIVER_STUFFUP;
    552 		}
    553 #ifdef DIAGNOSTIC
    554 		if (xfer->c_bcount != 0) {
    555 			printf("wdc_atapi_intr warning: bcount value "
    556 			    "is %d after io\n", xfer->c_bcount);
    557 		}
    558 #endif
    559 		break;
    560 
    561 	default:
    562 		if (++retries<500) {
    563 			DELAY(100);
    564 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    565 			    chp->cmd_ioh, wd_status);
    566 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    567 			    chp->cmd_ioh, wd_error);
    568 			goto again;
    569 		}
    570 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    571 		if (chp->ch_status & WDCS_ERR) {
    572 			sc_xfer->error = XS_SENSE;
    573 			sc_xfer->sense.atapi_sense = chp->ch_error;
    574 		} else {
    575 			sc_xfer->error = XS_DRIVER_STUFFUP;
    576 		}
    577 	}
    578 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    579 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    580 	    DEBUG_INTR);
    581 	wdc_atapi_done(chp, xfer);
    582 	return (1);
    583 }
    584 
    585 int
    586 wdc_atapi_ctrl(chp, xfer)
    587 	struct channel_softc *chp;
    588 	struct wdc_xfer *xfer;
    589 {
    590 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    591 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    592 	char *errstring = NULL;
    593 
    594 	/* Ack interrupt done in wait_for_unbusy */
    595 again:
    596 	WDCDEBUG_PRINT(("wdc_atapi_ctrl state %d\n", drvp->state),
    597 	    DEBUG_INTR | DEBUG_FUNCS);
    598 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    599 	    WDSD_IBM | (xfer->drive << 4));
    600 	switch (drvp->state) {
    601 	case PIOMODE:
    602 		/* Don't try to set mode if controller can't be adjusted */
    603 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    604 			goto ready;
    605 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    606 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    607 		drvp->state = PIOMODE_WAIT;
    608 		break;
    609 	case PIOMODE_WAIT:
    610 		errstring = "piomode";
    611 		if (wait_for_unbusy(chp, ATAPI_DELAY))
    612 			goto timeout;
    613 		if (chp->ch_status & WDCS_ERR)
    614 			goto error;
    615 	/* fall through */
    616 
    617 	case DMAMODE:
    618 		if (drvp->drive_flags & DRIVE_UDMA) {
    619 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    620 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    621 		} else if (drvp->drive_flags & DRIVE_DMA) {
    622 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    623 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    624 		} else {
    625 			goto ready;
    626 		}
    627 		drvp->state = DMAMODE_WAIT;
    628 		break;
    629 	case DMAMODE_WAIT:
    630 		errstring = "dmamode";
    631 		if (wait_for_unbusy(chp, ATAPI_DELAY))
    632 			goto timeout;
    633 		if (chp->ch_status & WDCS_ERR)
    634 			goto error;
    635 	/* fall through */
    636 
    637 	case READY:
    638 	ready:
    639 		drvp->state = READY;
    640 		xfer->c_intr = wdc_atapi_intr;
    641 		wdc_atapi_start(chp, xfer);
    642 		return 1;
    643 	}
    644 	if ((sc_xfer->flags & SCSI_POLL) == 0) {
    645 		chp->ch_flags |= WDCF_IRQ_WAIT;
    646 		xfer->c_intr = wdc_atapi_ctrl;
    647 		timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    648 	} else {
    649 		goto again;
    650 	}
    651 	return 1;
    652 
    653 timeout:
    654 	if ((xfer->c_flags & C_TIMEOU) == 0 ) {
    655 		return 0; /* IRQ was not for us */
    656 	}
    657 	printf("%s:%d:%d: %s timed out\n",
    658 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    659 	sc_xfer->error = XS_TIMEOUT;
    660 	wdc_atapi_reset(chp, xfer);
    661 	return 1;
    662 error:
    663 	printf("%s:%d:%d: %s ",
    664 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    665 	    errstring);
    666 	printf("error (0x%x)\n", chp->ch_error);
    667 	sc_xfer->error = XS_SENSE;
    668 	sc_xfer->sense.atapi_sense = chp->ch_error;
    669 	wdc_atapi_reset(chp, xfer);
    670 	return 1;
    671 }
    672 
    673 void
    674 wdc_atapi_done(chp, xfer)
    675 	struct channel_softc *chp;
    676 	struct wdc_xfer *xfer;
    677 {
    678 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    679 	struct wdc_softc *wdc = chp->wdc;
    680 	int need_done =  xfer->c_flags & C_NEEDDONE;
    681 
    682 	WDCDEBUG_PRINT(("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags),
    683 	    DEBUG_XFERS);
    684 	sc_xfer->resid = xfer->c_bcount;
    685 	/* remove this command from xfer queue */
    686 	xfer->c_skip = 0;
    687 	wdc_free_xfer(chp, xfer);
    688 	sc_xfer->flags |= ITSDONE;
    689 	if (need_done) {
    690 		WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    691 		scsipi_done(sc_xfer);
    692 	}
    693 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
    694 	    chp->ch_flags), DEBUG_XFERS);
    695 	wdcstart(wdc, chp->channel);
    696 }
    697 
    698 void
    699 wdc_atapi_reset(chp, xfer)
    700 	struct channel_softc *chp;
    701 	struct wdc_xfer *xfer;
    702 {
    703 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    704 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    705 
    706 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
    707 	drvp->state = 0;
    708 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
    709 		printf("%s:%d:%d: reset failed\n",
    710 		    chp->wdc->sc_dev.dv_xname, chp->channel,
    711 		    xfer->drive);
    712 		sc_xfer->error = XS_SELTIMEOUT;
    713 		wdc_atapi_done(chp, xfer);
    714 		return;
    715 	}
    716 	wdc_atapi_done(chp, xfer);
    717 	return;
    718 }
    719