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