Home | History | Annotate | Line # | Download | only in ata
ata_wdc.c revision 1.18
      1 /*	$NetBSD: ata_wdc.c,v 1.18 1999/03/25 16:17:36 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 /*-
     37  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to The NetBSD Foundation
     41  * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *        This product includes software developed by the NetBSD
     54  *        Foundation, Inc. and its contributors.
     55  * 4. Neither the name of The NetBSD Foundation nor the names of its
     56  *    contributors may be used to endorse or promote products derived
     57  *    from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #ifndef WDCDEBUG
     73 #define WDCDEBUG
     74 #endif /* WDCDEBUG */
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/file.h>
     80 #include <sys/stat.h>
     81 #include <sys/buf.h>
     82 #include <sys/malloc.h>
     83 #include <sys/device.h>
     84 #include <sys/disklabel.h>
     85 #include <sys/syslog.h>
     86 #include <sys/proc.h>
     87 
     88 #include <machine/intr.h>
     89 #include <machine/bus.h>
     90 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     91 #define    bus_space_write_multi_stream_2    bus_space_write_multi_2
     92 #define    bus_space_write_multi_stream_4    bus_space_write_multi_4
     93 #define    bus_space_read_multi_stream_2    bus_space_read_multi_2
     94 #define    bus_space_read_multi_stream_4    bus_space_read_multi_4
     95 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     96 
     97 #include <dev/ata/atareg.h>
     98 #include <dev/ata/atavar.h>
     99 #include <dev/ic/wdcreg.h>
    100 #include <dev/ic/wdcvar.h>
    101 #include <dev/ata/wdvar.h>
    102 
    103 #define DEBUG_INTR   0x01
    104 #define DEBUG_XFERS  0x02
    105 #define DEBUG_STATUS 0x04
    106 #define DEBUG_FUNCS  0x08
    107 #define DEBUG_PROBE  0x10
    108 #ifdef WDCDEBUG
    109 int wdcdebug_wd_mask = 0;
    110 #define WDCDEBUG_PRINT(args, level) \
    111 	if (wdcdebug_wd_mask & (level)) \
    112 		printf args
    113 #else
    114 #define WDCDEBUG_PRINT(args, level)
    115 #endif
    116 
    117 #define ATA_DELAY 10000 /* 10s for a drive I/O */
    118 
    119 void  wdc_ata_bio_start  __P((struct channel_softc *,struct wdc_xfer *));
    120 int   wdc_ata_bio_intr   __P((struct channel_softc *, struct wdc_xfer *));
    121 void  wdc_ata_bio_done   __P((struct channel_softc *, struct wdc_xfer *));
    122 int   wdc_ata_ctrl_intr __P((struct channel_softc *, struct wdc_xfer *));
    123 int   wdc_ata_err __P((struct ata_drive_datas *, struct ata_bio *));
    124 #define WDC_ATA_NOERR 0x00 /* Drive doesn't report an error */
    125 #define WDC_ATA_RECOV 0x01 /* There was a recovered error */
    126 #define WDC_ATA_ERR   0x02 /* Drive reports an error */
    127 
    128 /*
    129  * Handle block I/O operation. Return WDC_COMPLETE, WDC_QUEUED, or
    130  * WDC_TRY_AGAIN. Must be called at splio().
    131  */
    132 int
    133 wdc_ata_bio(drvp, ata_bio)
    134 	struct ata_drive_datas *drvp;
    135 	struct ata_bio *ata_bio;
    136 {
    137 	struct wdc_xfer *xfer;
    138 	struct channel_softc *chp = drvp->chnl_softc;
    139 
    140 	xfer = wdc_get_xfer(WDC_NOSLEEP);
    141 	if (xfer == NULL)
    142 		return WDC_TRY_AGAIN;
    143 	if (ata_bio->flags & ATA_POLL)
    144 		xfer->c_flags |= C_POLL;
    145 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    146 	    (ata_bio->flags & ATA_SINGLE) == 0)
    147 		xfer->c_flags |= C_DMA;
    148 	xfer->drive = drvp->drive;
    149 	xfer->cmd = ata_bio;
    150 	xfer->databuf = ata_bio->databuf;
    151 	xfer->c_bcount = ata_bio->bcount;
    152 	xfer->c_start = wdc_ata_bio_start;
    153 	xfer->c_intr = wdc_ata_bio_intr;
    154 	wdc_exec_xfer(chp, xfer);
    155 	return (ata_bio->flags & ATA_ITSDONE) ? WDC_COMPLETE : WDC_QUEUED;
    156 }
    157 
    158 void
    159 wdc_ata_bio_start(chp, xfer)
    160 	struct channel_softc *chp;
    161 	struct wdc_xfer *xfer;
    162 {
    163 	struct ata_bio *ata_bio = xfer->cmd;
    164 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    165 	u_int16_t cyl;
    166 	u_int8_t head, sect, cmd = 0;
    167 	int nblks;
    168 	int ata_delay;
    169 	int dma_flags = 0;
    170 
    171 	WDCDEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d\n",
    172 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
    173 	    DEBUG_XFERS);
    174 
    175 	/* Do control operations specially. */
    176 	if (drvp->state < READY) {
    177 		/*
    178 		 * Actually, we want to be careful not to mess with the control
    179 		 * state if the device is currently busy, but we can assume
    180 		 * that we never get to this point if that's the case.
    181 		 */
    182 		/* at this point, we should only be in RECAL state */
    183 		if (drvp->state != RECAL) {
    184 			printf("%s:%d:%d: bad state %d in wdc_ata_bio_start\n",
    185 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    186 			    xfer->drive, drvp->state);
    187 			panic("wdc_ata_bio_start: bad state");
    188 		}
    189 		xfer->c_intr = wdc_ata_ctrl_intr;
    190 		bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    191 		    WDSD_IBM | (xfer->drive << 4));
    192 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY) != 0)
    193 			goto timeout;
    194 		wdccommandshort(chp, xfer->drive, WDCC_RECAL);
    195 		drvp->state = RECAL_WAIT;
    196 		if ((ata_bio->flags & ATA_POLL) == 0) {
    197 			chp->ch_flags |= WDCF_IRQ_WAIT;
    198 			timeout(wdctimeout, chp,
    199 			    ATA_DELAY / 1000 * hz);
    200 		} else {
    201 			/* Wait for at last 400ns for status bit to be valid */
    202 			delay(1);
    203 			wdc_ata_ctrl_intr(chp, xfer);
    204 		}
    205 		return;
    206 	}
    207 
    208 	if (xfer->c_flags & C_DMA) {
    209 		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;
    210 		dma_flags |= (ata_bio->flags & ATA_POLL) ?  WDC_DMA_POLL : 0;
    211 	}
    212 	if (ata_bio->flags & ATA_SINGLE)
    213 		ata_delay = ATA_DELAY;
    214 	else
    215 		ata_delay = ATA_DELAY;
    216 again:
    217 	/*
    218 	 *
    219 	 * When starting a multi-sector transfer, or doing single-sector
    220 	 * transfers...
    221 	 */
    222 	if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
    223 		if (ata_bio->flags & ATA_SINGLE)
    224 			nblks = 1;
    225 		else
    226 			nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
    227 		/* Check for bad sectors and adjust transfer, if necessary. */
    228 		if ((ata_bio->lp->d_flags & D_BADSECT) != 0) {
    229 			long blkdiff;
    230 			int i;
    231 			for (i = 0; (blkdiff = ata_bio->badsect[i]) != -1;
    232 			    i++) {
    233 				blkdiff -= ata_bio->blkno;
    234 				if (blkdiff < 0)
    235 					continue;
    236 				if (blkdiff == 0) {
    237 					/* Replace current block of transfer. */
    238 					ata_bio->blkno =
    239 					    ata_bio->lp->d_secperunit -
    240 					    ata_bio->lp->d_nsectors - i - 1;
    241 				}
    242 				if (blkdiff < nblks) {
    243 					/* Bad block inside transfer. */
    244 					ata_bio->flags |= ATA_SINGLE;
    245 					nblks = 1;
    246 				}
    247 				break;
    248 			}
    249 		/* Transfer is okay now. */
    250 		}
    251 		if (ata_bio->flags & ATA_LBA) {
    252 			sect = (ata_bio->blkno >> 0) & 0xff;
    253 			cyl = (ata_bio->blkno >> 8) & 0xffff;
    254 			head = (ata_bio->blkno >> 24) & 0x0f;
    255 			head |= WDSD_LBA;
    256 		} else {
    257 			int blkno = ata_bio->blkno;
    258 			sect = blkno % ata_bio->lp->d_nsectors;
    259 			sect++;    /* Sectors begin with 1, not 0. */
    260 			blkno /= ata_bio->lp->d_nsectors;
    261 			head = blkno % ata_bio->lp->d_ntracks;
    262 			blkno /= ata_bio->lp->d_ntracks;
    263 			cyl = blkno;
    264 			head |= WDSD_CHS;
    265 		}
    266 		if (xfer->c_flags & C_DMA) {
    267 			ata_bio->nblks = nblks;
    268 			ata_bio->nbytes = xfer->c_bcount;
    269 			cmd = (ata_bio->flags & ATA_READ) ?
    270 			    WDCC_READDMA : WDCC_WRITEDMA;
    271 			nblks = ata_bio->nblks;
    272 	    		/* Init the DMA channel. */
    273 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    274 			    chp->channel, xfer->drive,
    275 			    (char *)xfer->databuf + xfer->c_skip,
    276 			    ata_bio->nbytes, dma_flags) != 0) {
    277 				ata_bio->error = ERR_DMA;
    278 				ata_bio->r_error = 0;
    279 				wdc_ata_bio_done(chp, xfer);
    280 				return;
    281 			}
    282 			/* Initiate command */
    283 			bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    284 			    WDSD_IBM | (xfer->drive << 4));
    285 			if (wait_for_ready(chp, ata_delay) < 0)
    286 				goto timeout;
    287 			wdccommand(chp, xfer->drive, cmd, cyl,
    288 			    head, sect, nblks, 0);
    289 			/* start the DMA channel */
    290 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    291 			    chp->channel, xfer->drive, dma_flags);
    292 			/* wait for irq */
    293 			goto intr;
    294 		} /* else not DMA */
    295 		ata_bio->nblks = min(nblks, ata_bio->multi);
    296 		ata_bio->nbytes = ata_bio->nblks * ata_bio->lp->d_secsize;
    297 		if (ata_bio->nblks > 1 && (ata_bio->flags & ATA_SINGLE) == 0) {
    298 			cmd = (ata_bio->flags & ATA_READ) ?
    299 			    WDCC_READMULTI : WDCC_WRITEMULTI;
    300 		} else {
    301 			cmd = (ata_bio->flags & ATA_READ) ?
    302 			    WDCC_READ : WDCC_WRITE;
    303 		}
    304 		/* Initiate command! */
    305 		bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    306 		    WDSD_IBM | (xfer->drive << 4));
    307 		if (wait_for_ready(chp, ata_delay) < 0)
    308 			goto timeout;
    309 		wdccommand(chp, xfer->drive, cmd, cyl,
    310 		    head, sect, nblks,
    311 		    (ata_bio->lp->d_type == DTYPE_ST506) ?
    312 		    ata_bio->lp->d_precompcyl / 4 : 0);
    313 	} else if (ata_bio->nblks > 1) {
    314 		/* The number of blocks in the last stretch may be smaller. */
    315 		nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
    316 		if (ata_bio->nblks > nblks) {
    317 		ata_bio->nblks = nblks;
    318 		ata_bio->nbytes = xfer->c_bcount;
    319 		}
    320 	}
    321 	/* If this was a write and not using DMA, push the data. */
    322 	if ((ata_bio->flags & ATA_READ) == 0) {
    323 		if (wait_for_drq(chp, ata_delay) != 0) {
    324 			printf("%s:%d:%d: timeout waiting for DRQ, "
    325 			    "st=0x%02x, err=0x%02x\n",
    326 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    327 			    xfer->drive, chp->ch_status, chp->ch_error);
    328 			if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
    329 				ata_bio->error = TIMEOUT;
    330 			wdc_ata_bio_done(chp, xfer);
    331 			return;
    332 		}
    333 		if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {
    334 			wdc_ata_bio_done(chp, xfer);
    335 			return;
    336 		}
    337 		if ((chp->wdc->cap & WDC_CAPABILITY_ATA_NOSTREAM)) {
    338 			if (drvp->drive_flags & DRIVE_CAP32) {
    339 				bus_space_write_multi_4(chp->data32iot,
    340 				    chp->data32ioh, 0,
    341 				    (u_int32_t *)((char *)xfer->databuf +
    342 				                  xfer->c_skip),
    343 				    ata_bio->nbytes >> 2);
    344 			} else {
    345 				bus_space_write_multi_2(chp->cmd_iot,
    346 				    chp->cmd_ioh, wd_data,
    347 				    (u_int16_t *)((char *)xfer->databuf +
    348 				                  xfer->c_skip),
    349 				    ata_bio->nbytes >> 1);
    350 			}
    351 		} else {
    352 			if (drvp->drive_flags & DRIVE_CAP32) {
    353 				bus_space_write_multi_stream_4(chp->data32iot,
    354 				    chp->data32ioh, 0,
    355 				    (u_int32_t *)((char *)xfer->databuf +
    356 				                  xfer->c_skip),
    357 				    ata_bio->nbytes >> 2);
    358 			} else {
    359 				bus_space_write_multi_stream_2(chp->cmd_iot,
    360 				    chp->cmd_ioh, wd_data,
    361 				    (u_int16_t *)((char *)xfer->databuf +
    362 				                  xfer->c_skip),
    363 				    ata_bio->nbytes >> 1);
    364 			}
    365 		}
    366 	}
    367 
    368 intr:	/* Wait for IRQ (either real or polled) */
    369 	if ((ata_bio->flags & ATA_POLL) == 0) {
    370 		chp->ch_flags |= WDCF_IRQ_WAIT;
    371 		timeout(wdctimeout, chp, ata_delay / 1000 * hz);
    372 	} else {
    373 		/* Wait for at last 400ns for status bit to be valid */
    374 		delay(1);
    375 		wdc_ata_bio_intr(chp, xfer);
    376 		if ((ata_bio->flags & ATA_ITSDONE) == 0)
    377 			goto again;
    378 	}
    379 	return;
    380 timeout:
    381 	printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",
    382 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    383 	    chp->ch_status, chp->ch_error);
    384 	if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
    385 		ata_bio->error = TIMEOUT;
    386 	wdc_ata_bio_done(chp, xfer);
    387 	return;
    388 }
    389 
    390 int
    391 wdc_ata_bio_intr(chp, xfer)
    392 	struct channel_softc *chp;
    393 	struct wdc_xfer *xfer;
    394 {
    395 	struct ata_bio *ata_bio = xfer->cmd;
    396 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    397 	int drv_err;
    398 	int dma_flags = 0;
    399 
    400 	WDCDEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
    401 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
    402 	    DEBUG_INTR | DEBUG_XFERS);
    403 
    404 
    405 	/* Is it not a transfer, but a control operation? */
    406 	if (drvp->state < READY) {
    407 		printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
    408 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    409 		    drvp->state);
    410 		panic("wdc_ata_bio_intr: bad state\n");
    411 	}
    412 
    413 	if (xfer->c_flags & C_DMA) {
    414 		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;
    415 		dma_flags |= (ata_bio->flags & ATA_POLL) ?  WDC_DMA_POLL : 0;
    416 	}
    417 
    418 	/* Ack interrupt done by wait_for_unbusy */
    419 	if (wait_for_unbusy(chp,
    420 	    (ata_bio->flags & ATA_POLL) ? ATA_DELAY : 0) < 0) {
    421 		if ((ata_bio->flags & ATA_POLL) == 0 &&
    422 		    (xfer->c_flags & C_TIMEOU) == 0)
    423 			return 0; /* IRQ was not for us */
    424 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
    425 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    426 		    xfer->c_bcount, xfer->c_skip);
    427 		/* if we were using DMA, turn off DMA channel */
    428 		if (xfer->c_flags & C_DMA) {
    429 			(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    430 			    chp->channel, xfer->drive, dma_flags);
    431 			drvp->n_dmaerrs++;
    432 		}
    433 		ata_bio->error = TIMEOUT;
    434 		wdc_ata_bio_done(chp, xfer);
    435 		return 1;
    436 	}
    437 
    438 	drv_err = wdc_ata_err(drvp, ata_bio);
    439 
    440 	/* If we were using DMA, Turn off the DMA channel and check for error */
    441 	if (xfer->c_flags & C_DMA) {
    442 		if (ata_bio->flags & ATA_POLL) {
    443 			/*
    444 			 * IDE drives deassert WDCS_BSY before transfer is
    445 			 * complete when using DMA. Polling for DRQ to deassert
    446 			 * is not enouth DRQ is not required to be
    447 			 * asserted for DMA transfers, so poll for DRDY.
    448 			 */
    449 			if (wdcwait(chp, WDCS_DRDY | WDCS_DRQ, WDCS_DRDY,
    450 			    ATA_DELAY) < 0) {
    451 				printf("%s:%d:%d: polled transfer timed out "
    452 				    "(st=0x%x)\n", chp->wdc->sc_dev.dv_xname,
    453 				    chp->channel, xfer->drive, chp->ch_status);
    454 				ata_bio->error = TIMEOUT;
    455 				drv_err = WDC_ATA_ERR;
    456 			}
    457 		}
    458 		if ((*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    459 		    chp->channel, xfer->drive, dma_flags) != 0) {
    460 			if (drv_err != WDC_ATA_ERR) {
    461 				ata_bio->error = ERR_DMA;
    462 				drv_err = WDC_ATA_ERR;
    463 			}
    464 		}
    465 		if (chp->ch_status & WDCS_DRQ) {
    466 			if (drv_err != WDC_ATA_ERR) {
    467 				printf("%s:%d:%d: intr with DRQ (st=0x%x)\n",
    468 				    chp->wdc->sc_dev.dv_xname, chp->channel,
    469 				    xfer->drive, chp->ch_status);
    470 				ata_bio->error = TIMEOUT;
    471 				drv_err = WDC_ATA_ERR;
    472 			}
    473 		}
    474 		if (drv_err != WDC_ATA_ERR)
    475 			goto end;
    476 		drvp->n_dmaerrs++;
    477 	}
    478 
    479 	/* if we had an error, end */
    480 	if (drv_err == WDC_ATA_ERR) {
    481 		wdc_ata_bio_done(chp, xfer);
    482 		return 1;
    483 	}
    484 
    485 	/* If this was a read and not using DMA, fetch the data. */
    486 	if ((ata_bio->flags & ATA_READ) != 0) {
    487 		if ((chp->ch_status & WDCS_DRQ) != WDCS_DRQ) {
    488 			printf("%s:%d:%d: read intr before drq\n",
    489 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    490 			    xfer->drive);
    491 			ata_bio->error = TIMEOUT;
    492 			wdc_ata_bio_done(chp, xfer);
    493 			return 1;
    494 		}
    495 		if ((chp->wdc->cap & WDC_CAPABILITY_ATA_NOSTREAM)) {
    496 			if (drvp->drive_flags & DRIVE_CAP32) {
    497 				bus_space_read_multi_4(chp->data32iot,
    498 				    chp->data32ioh, 0,
    499 				    (u_int32_t *)((char *)xfer->databuf +
    500 				                  xfer->c_skip),
    501 				    ata_bio->nbytes >> 2);
    502 			} else {
    503 				bus_space_read_multi_2(chp->cmd_iot,
    504 				    chp->cmd_ioh, wd_data,
    505 				    (u_int16_t *)((char *)xfer->databuf +
    506 				                  xfer->c_skip),
    507 				    ata_bio->nbytes >> 1);
    508 			}
    509 		} else {
    510 			if (drvp->drive_flags & DRIVE_CAP32) {
    511 				bus_space_read_multi_stream_4(chp->data32iot,
    512 				    chp->data32ioh, 0,
    513 				    (u_int32_t *)((char *)xfer->databuf +
    514 				                  xfer->c_skip),
    515 				    ata_bio->nbytes >> 2);
    516 			} else {
    517 				bus_space_read_multi_stream_2(chp->cmd_iot,
    518 				    chp->cmd_ioh, wd_data,
    519 				    (u_int16_t *)((char *)xfer->databuf +
    520 				                  xfer->c_skip),
    521 				    ata_bio->nbytes >> 1);
    522 			}
    523 		}
    524 	}
    525 
    526 end:
    527 	ata_bio->blkno += ata_bio->nblks;
    528 	ata_bio->blkdone += ata_bio->nblks;
    529 	xfer->c_skip += ata_bio->nbytes;
    530 	xfer->c_bcount -= ata_bio->nbytes;
    531 	/* See if this transfer is complete. */
    532 	if (xfer->c_bcount > 0) {
    533 		if ((ata_bio->flags & ATA_POLL) == 0) {
    534 			/* Start the next operation */
    535 			wdc_ata_bio_start(chp, xfer);
    536 		} else {
    537 			/* Let wdc_ata_bio_start do the loop */
    538 			return 1;
    539 		}
    540 	} else { /* Done with this transfer */
    541 		ata_bio->error = NOERROR;
    542 		wdc_ata_bio_done(chp, xfer);
    543 	}
    544 	return 1;
    545 }
    546 
    547 void
    548 wdc_ata_bio_done(chp, xfer)
    549 	struct channel_softc *chp;
    550 	struct wdc_xfer *xfer;
    551 {
    552 	struct ata_bio *ata_bio = xfer->cmd;
    553 	int need_done = xfer->c_flags & C_NEEDDONE;
    554 	int drive = xfer->drive;
    555 	struct ata_drive_datas *drvp = &chp->ch_drive[drive];
    556 
    557 	WDCDEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
    558 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    559 	    (u_int)xfer->c_flags),
    560 	    DEBUG_XFERS);
    561 
    562 	if (ata_bio->error == NOERROR)
    563 		drvp->n_dmaerrs = 0;
    564 	else if (drvp->n_dmaerrs >= NERRS_MAX) {
    565 		wdc_downgrade_mode(drvp);
    566 	}
    567 
    568 	/* feed back residual bcount to our caller */
    569 	ata_bio->bcount = xfer->c_bcount;
    570 
    571 	/* remove this command from xfer queue */
    572 	wdc_free_xfer(chp, xfer);
    573 
    574 	ata_bio->flags |= ATA_ITSDONE;
    575 	if (need_done) {
    576 		WDCDEBUG_PRINT(("wdc_ata_done: wddone\n"), DEBUG_XFERS);
    577 		wddone(chp->ch_drive[drive].drv_softc);
    578 	}
    579 	WDCDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
    580 	    chp->ch_flags), DEBUG_XFERS);
    581 	wdcstart(chp);
    582 }
    583 
    584 /*
    585  * Implement operations needed before read/write.
    586  */
    587 int
    588 wdc_ata_ctrl_intr(chp, xfer)
    589 	struct channel_softc *chp;
    590 	struct wdc_xfer *xfer;
    591 {
    592 	struct ata_bio *ata_bio = xfer->cmd;
    593 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    594 	char *errstring = NULL;
    595 	int delay = (ata_bio->flags & ATA_POLL) ? ATA_DELAY : 0;
    596 
    597 	WDCDEBUG_PRINT(("wdc_ata_ctrl_intr: state %d\n", drvp->state),
    598 	    DEBUG_FUNCS);
    599 
    600 again:
    601 	switch (drvp->state) {
    602 	case RECAL:    /* Should not be in this state here */
    603 		panic("wdc_ata_ctrl_intr: state==RECAL");
    604 		break;
    605 
    606 	case RECAL_WAIT:
    607 		errstring = "recal";
    608 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
    609 			goto timeout;
    610 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    611 			goto error;
    612 	/* fall through */
    613 
    614 	case PIOMODE:
    615 		/* Don't try to set modes if controller can't be adjusted */
    616 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    617 			goto geometry;
    618 		/* Also don't try if the drive didn't report its mode */
    619 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    620 			goto geometry;
    621 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    622 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    623 		drvp->state = PIOMODE_WAIT;
    624 		break;
    625 
    626 	case PIOMODE_WAIT:
    627 		errstring = "piomode";
    628 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
    629 			goto timeout;
    630 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    631 			goto error;
    632 	/* fall through */
    633 
    634 	case DMAMODE:
    635 		if (drvp->drive_flags & DRIVE_UDMA) {
    636 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    637 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    638 		} else if (drvp->drive_flags & DRIVE_DMA) {
    639 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    640 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    641 		} else {
    642 			goto geometry;
    643 		}
    644 		drvp->state = DMAMODE_WAIT;
    645 		break;
    646 	case DMAMODE_WAIT:
    647 		errstring = "dmamode";
    648 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
    649 			goto timeout;
    650 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    651 			goto error;
    652 	/* fall through */
    653 
    654 	case GEOMETRY:
    655 	geometry:
    656 		if (ata_bio->flags & ATA_LBA)
    657 			goto multimode;
    658 		wdccommand(chp, xfer->drive, WDCC_IDP,
    659 		    ata_bio->lp->d_ncylinders,
    660 		    ata_bio->lp->d_ntracks - 1, 0, ata_bio->lp->d_nsectors,
    661 		    (ata_bio->lp->d_type == DTYPE_ST506) ?
    662 			ata_bio->lp->d_precompcyl / 4 : 0);
    663 		drvp->state = GEOMETRY_WAIT;
    664 		break;
    665 
    666 	case GEOMETRY_WAIT:
    667 		errstring = "geometry";
    668 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
    669 			goto timeout;
    670 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    671 			goto error;
    672 		/* fall through */
    673 
    674 	case MULTIMODE:
    675 	multimode:
    676 		if (ata_bio->multi == 1)
    677 			goto ready;
    678 		wdccommand(chp, xfer->drive, WDCC_SETMULTI, 0, 0, 0,
    679 		    ata_bio->multi, 0);
    680 		drvp->state = MULTIMODE_WAIT;
    681 		break;
    682 
    683 	case MULTIMODE_WAIT:
    684 		errstring = "setmulti";
    685 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
    686 			goto timeout;
    687 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    688 			goto error;
    689 		/* fall through */
    690 
    691 	case READY:
    692 	ready:
    693 		drvp->state = READY;
    694 		/*
    695 		 * The drive is usable now
    696 		 */
    697 		xfer->c_intr = wdc_ata_bio_intr;
    698 		wdc_ata_bio_start(chp, xfer);
    699 		return 1;
    700 	}
    701 
    702 	if ((ata_bio->flags & ATA_POLL) == 0) {
    703 		chp->ch_flags |= WDCF_IRQ_WAIT;
    704 		timeout(wdctimeout, chp, ATA_DELAY / 1000 * hz);
    705 	} else {
    706 		goto again;
    707 	}
    708 	return 1;
    709 
    710 timeout:
    711 	if ((xfer->c_flags & C_TIMEOU) == 0 && delay == 0) {
    712 		return 0; /* IRQ was not for us */
    713 	}
    714 	printf("%s:%d:%d: %s timed out\n",
    715 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    716 	ata_bio->error = TIMEOUT;
    717 	drvp->state = 0;
    718 	wdc_ata_bio_done(chp, xfer);
    719 	return 0;
    720 error:
    721 	printf("%s:%d:%d: %s ",
    722 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    723 	    errstring);
    724 	if (chp->ch_status & WDCS_DWF) {
    725 		printf("drive fault\n");
    726 		ata_bio->error = ERR_DF;
    727 	} else {
    728 		printf("error (%x)\n", chp->ch_error);
    729 		ata_bio->r_error = chp->ch_error;
    730 		ata_bio->error = ERROR;
    731 	}
    732 	drvp->state = 0;
    733 	wdc_ata_bio_done(chp, xfer);
    734 	return 1;
    735 }
    736 
    737 int
    738 wdc_ata_err(drvp, ata_bio)
    739 	struct ata_drive_datas *drvp;
    740 	struct ata_bio *ata_bio;
    741 {
    742 	struct channel_softc *chp = drvp->chnl_softc;
    743 	ata_bio->error = 0;
    744 	if (chp->ch_status & WDCS_BSY) {
    745 		ata_bio->error = TIMEOUT;
    746 		return WDC_ATA_ERR;
    747 	}
    748 
    749 	if (chp->ch_status & WDCS_DWF) {
    750 		ata_bio->error = ERR_DF;
    751 		return WDC_ATA_ERR;
    752 	}
    753 
    754 	if (chp->ch_status & WDCS_ERR) {
    755 		ata_bio->error = ERROR;
    756 		ata_bio->r_error = chp->ch_error;
    757 		if (drvp->drive_flags & DRIVE_UDMA &&
    758 		    (ata_bio->r_error & WDCE_CRC)) {
    759 			/*
    760 			 * Record the CRC error, to avoid downgrading to
    761 			 * multiword DMA
    762 			 */
    763 			drvp->drive_flags |= DRIVE_DMAERR;
    764 		}
    765 		if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
    766 		    WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
    767 			return WDC_ATA_ERR;
    768 		return WDC_ATA_NOERR;
    769 	}
    770 
    771 	if (chp->ch_status & WDCS_CORR)
    772 		ata_bio->flags |= ATA_CORR;
    773 	return WDC_ATA_NOERR;
    774 }
    775 
    776 int
    777 wdc_ata_addref(drvp)
    778 	struct ata_drive_datas *drvp;
    779 {
    780 	struct channel_softc *chp = drvp->chnl_softc;
    781 
    782 	return (wdc_addref(chp));
    783 }
    784 
    785 void
    786 wdc_ata_delref(drvp)
    787 	struct ata_drive_datas *drvp;
    788 {
    789 	struct channel_softc *chp = drvp->chnl_softc;
    790 
    791 	wdc_delref(chp);
    792 }
    793