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