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