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