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