Home | History | Annotate | Line # | Download | only in ata
ata_wdc.c revision 1.95.2.2
      1  1.95.2.2      yamt /*	$NetBSD: ata_wdc.c,v 1.95.2.2 2012/10/30 17:20:52 yamt 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.95.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: ata_wdc.c,v 1.95.2.2 2012/10/30 17:20:52 yamt 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.45   thorpej static int	wdc_ata_bio(struct ata_drive_datas*, struct ata_bio*);
    107      1.72   thorpej static void	wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
    108      1.72   thorpej static void	_wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
    109      1.72   thorpej static int	wdc_ata_bio_intr(struct ata_channel *, struct ata_xfer *,
    110      1.45   thorpej 				 int);
    111      1.72   thorpej static void	wdc_ata_bio_kill_xfer(struct ata_channel *,
    112      1.58    bouyer 				      struct ata_xfer *, int);
    113      1.79     perry static void	wdc_ata_bio_done(struct ata_channel *, struct ata_xfer *);
    114      1.45   thorpej static int	wdc_ata_err(struct ata_drive_datas *, struct ata_bio *);
    115       1.2    bouyer #define WDC_ATA_NOERR 0x00 /* Drive doesn't report an error */
    116       1.2    bouyer #define WDC_ATA_RECOV 0x01 /* There was a recovered error */
    117       1.2    bouyer #define WDC_ATA_ERR   0x02 /* Drive reports an error */
    118      1.45   thorpej static int	wdc_ata_addref(struct ata_drive_datas *);
    119      1.45   thorpej static void	wdc_ata_delref(struct ata_drive_datas *);
    120      1.32    bouyer 
    121      1.32    bouyer const struct ata_bustype wdc_ata_bustype = {
    122      1.32    bouyer 	SCSIPI_BUSTYPE_ATA,
    123      1.32    bouyer 	wdc_ata_bio,
    124      1.59    bouyer 	wdc_reset_drive,
    125      1.74   thorpej 	wdc_reset_channel,
    126      1.32    bouyer 	wdc_exec_command,
    127      1.32    bouyer 	ata_get_params,
    128      1.32    bouyer 	wdc_ata_addref,
    129      1.32    bouyer 	wdc_ata_delref,
    130      1.66   thorpej 	ata_kill_pending,
    131      1.32    bouyer };
    132      1.32    bouyer 
    133       1.2    bouyer /*
    134      1.64   thorpej  * Handle block I/O operation. Return ATACMD_COMPLETE, ATACMD_QUEUED, or
    135      1.64   thorpej  * ATACMD_TRY_AGAIN. Must be called at splbio().
    136       1.2    bouyer  */
    137      1.45   thorpej static int
    138      1.44   thorpej wdc_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
    139       1.2    bouyer {
    140      1.48   thorpej 	struct ata_xfer *xfer;
    141      1.72   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    142      1.73   thorpej 	struct atac_softc *atac = chp->ch_atac;
    143       1.2    bouyer 
    144      1.65   thorpej 	xfer = ata_get_xfer(ATAXF_NOSLEEP);
    145       1.2    bouyer 	if (xfer == NULL)
    146      1.64   thorpej 		return ATACMD_TRY_AGAIN;
    147      1.73   thorpej 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
    148      1.30     bjh21 		ata_bio->flags |= ATA_POLL;
    149       1.2    bouyer 	if (ata_bio->flags & ATA_POLL)
    150       1.2    bouyer 		xfer->c_flags |= C_POLL;
    151      1.85     itohy #if NATA_DMA
    152  1.95.2.2      yamt 	if ((drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) &&
    153       1.2    bouyer 	    (ata_bio->flags & ATA_SINGLE) == 0)
    154       1.2    bouyer 		xfer->c_flags |= C_DMA;
    155      1.85     itohy #endif
    156      1.85     itohy #if NATA_DMA && NATA_PIOBM
    157      1.85     itohy 	else
    158      1.85     itohy #endif
    159      1.84     itohy #if NATA_PIOBM
    160      1.85     itohy 	if (atac->atac_cap & ATAC_CAP_PIOBM)
    161      1.84     itohy 		xfer->c_flags |= C_PIOBM;
    162      1.84     itohy #endif
    163      1.48   thorpej 	xfer->c_drive = drvp->drive;
    164      1.48   thorpej 	xfer->c_cmd = ata_bio;
    165      1.48   thorpej 	xfer->c_databuf = ata_bio->databuf;
    166       1.2    bouyer 	xfer->c_bcount = ata_bio->bcount;
    167       1.2    bouyer 	xfer->c_start = wdc_ata_bio_start;
    168       1.2    bouyer 	xfer->c_intr = wdc_ata_bio_intr;
    169      1.22     enami 	xfer->c_kill_xfer = wdc_ata_bio_kill_xfer;
    170      1.68   thorpej 	ata_exec_xfer(chp, xfer);
    171      1.64   thorpej 	return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED;
    172       1.2    bouyer }
    173       1.2    bouyer 
    174      1.45   thorpej static void
    175      1.72   thorpej wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
    176       1.2    bouyer {
    177      1.73   thorpej 	struct atac_softc *atac = chp->ch_atac;
    178      1.73   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    179      1.72   thorpej 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    180      1.48   thorpej 	struct ata_bio *ata_bio = xfer->c_cmd;
    181      1.48   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    182      1.95  christos 	int wait_flags;
    183      1.80  christos 	const char *errstring;
    184      1.94    bouyer #ifdef WDC_NO_IDS
    185      1.94    bouyer 	wait_flags = AT_POLL;
    186      1.94    bouyer #else
    187      1.95  christos 	wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
    188      1.94    bouyer #endif
    189      1.41    bouyer 
    190  1.95.2.2      yamt 	ATADEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d state %d drive_flags 0x%x "
    191  1.95.2.2      yamt 	    "c_flags 0x%x ch_flags 0x%x\n",
    192  1.95.2.2      yamt 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    193  1.95.2.2      yamt 	    drvp->state, drvp->drive_flags, xfer->c_flags, chp->ch_flags),
    194      1.20    bouyer 	    DEBUG_XFERS);
    195      1.20    bouyer 
    196      1.41    bouyer 	/* Do control operations specially. */
    197      1.41    bouyer 	if (__predict_false(drvp->state < READY)) {
    198      1.41    bouyer 		/*
    199      1.41    bouyer 		 * Actually, we want to be careful not to mess with the control
    200      1.41    bouyer 		 * state if the device is currently busy, but we can assume
    201      1.41    bouyer 		 * that we never get to this point if that's the case.
    202      1.41    bouyer 		 */
    203      1.81     peter 		/* If it's not a polled command, we need the kernel thread */
    204      1.91    bouyer 		if ((xfer->c_flags & C_POLL) == 0 &&
    205      1.91    bouyer 		    (chp->ch_flags & ATACH_TH_RUN) == 0) {
    206      1.42    bouyer 			chp->ch_queue->queue_freeze++;
    207      1.52   thorpej 			wakeup(&chp->ch_thread);
    208      1.41    bouyer 			return;
    209      1.41    bouyer 		}
    210      1.41    bouyer 		/*
    211      1.41    bouyer 		 * disable interrupts, all commands here should be quick
    212      1.93       snj 		 * enough to be able to poll, and we don't go here that often
    213      1.41    bouyer 		 */
    214      1.72   thorpej 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    215      1.41    bouyer 		    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.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    223      1.41    bouyer 			goto ctrltimeout;
    224      1.48   thorpej 		wdccommandshort(chp, xfer->c_drive, WDCC_RECAL);
    225      1.41    bouyer 		/* Wait for at last 400ns for status bit to be valid */
    226      1.41    bouyer 		DELAY(1);
    227      1.41    bouyer 		errstring = "recal";
    228      1.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    229      1.41    bouyer 			goto ctrltimeout;
    230      1.41    bouyer 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    231      1.41    bouyer 			goto ctrlerror;
    232      1.41    bouyer 		/* Don't try to set modes if controller can't be adjusted */
    233      1.73   thorpej 		if (atac->atac_set_modes == NULL)
    234      1.41    bouyer 			goto geometry;
    235      1.41    bouyer 		/* Also don't try if the drive didn't report its mode */
    236  1.95.2.2      yamt 		if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0)
    237      1.41    bouyer 			goto geometry;
    238      1.41    bouyer 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    239      1.41    bouyer 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    240      1.41    bouyer 		errstring = "piomode";
    241      1.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    242      1.41    bouyer 			goto ctrltimeout;
    243      1.41    bouyer 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    244      1.41    bouyer 			goto ctrlerror;
    245      1.85     itohy #if NATA_DMA
    246      1.85     itohy #if NATA_UDMA
    247  1.95.2.2      yamt 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
    248      1.41    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    249      1.41    bouyer 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    250      1.85     itohy 		} else
    251      1.85     itohy #endif
    252  1.95.2.2      yamt 		if (drvp->drive_flags & ATA_DRIVE_DMA) {
    253      1.41    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    254      1.41    bouyer 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    255      1.41    bouyer 		} else {
    256      1.41    bouyer 			goto geometry;
    257      1.79     perry 		}
    258      1.41    bouyer 		errstring = "dmamode";
    259      1.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    260      1.41    bouyer 			goto ctrltimeout;
    261      1.41    bouyer 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    262      1.41    bouyer 			goto ctrlerror;
    263      1.85     itohy #endif	/* NATA_DMA */
    264      1.41    bouyer geometry:
    265      1.41    bouyer 		if (ata_bio->flags & ATA_LBA)
    266      1.41    bouyer 			goto multimode;
    267      1.48   thorpej 		wdccommand(chp, xfer->c_drive, WDCC_IDP,
    268      1.41    bouyer 		    ata_bio->lp->d_ncylinders,
    269      1.41    bouyer 		    ata_bio->lp->d_ntracks - 1, 0, ata_bio->lp->d_nsectors,
    270      1.41    bouyer 		    (ata_bio->lp->d_type == DTYPE_ST506) ?
    271      1.41    bouyer 			ata_bio->lp->d_precompcyl / 4 : 0);
    272      1.41    bouyer 		errstring = "geometry";
    273      1.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    274      1.41    bouyer 			goto ctrltimeout;
    275      1.41    bouyer 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    276      1.41    bouyer 			goto ctrlerror;
    277      1.41    bouyer multimode:
    278      1.41    bouyer 		if (ata_bio->multi == 1)
    279      1.41    bouyer 			goto ready;
    280      1.48   thorpej 		wdccommand(chp, xfer->c_drive, WDCC_SETMULTI, 0, 0, 0,
    281      1.41    bouyer 		    ata_bio->multi, 0);
    282      1.41    bouyer 		errstring = "setmulti";
    283      1.41    bouyer 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
    284      1.41    bouyer 			goto ctrltimeout;
    285      1.41    bouyer 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
    286      1.41    bouyer 			goto ctrlerror;
    287      1.41    bouyer ready:
    288      1.41    bouyer 		drvp->state = READY;
    289      1.41    bouyer 		/*
    290      1.41    bouyer 		 * The drive is usable now
    291      1.41    bouyer 		 */
    292      1.72   thorpej 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    293      1.41    bouyer 		    WDCTL_4BIT);
    294      1.54    bouyer 		delay(10); /* some drives need a little delay here */
    295      1.41    bouyer 	}
    296      1.41    bouyer 
    297      1.20    bouyer 	_wdc_ata_bio_start(chp, xfer);
    298      1.41    bouyer 	return;
    299      1.41    bouyer ctrltimeout:
    300      1.41    bouyer 	printf("%s:%d:%d: %s timed out\n",
    301      1.88      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    302      1.51   thorpej 	    errstring);
    303      1.41    bouyer 	ata_bio->error = TIMEOUT;
    304      1.41    bouyer 	goto ctrldone;
    305      1.41    bouyer ctrlerror:
    306      1.41    bouyer 	printf("%s:%d:%d: %s ",
    307      1.88      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    308      1.41    bouyer 	    errstring);
    309      1.41    bouyer 	if (chp->ch_status & WDCS_DWF) {
    310      1.41    bouyer 		printf("drive fault\n");
    311      1.41    bouyer 		ata_bio->error = ERR_DF;
    312      1.41    bouyer 	} else {
    313      1.41    bouyer 		printf("error (%x)\n", chp->ch_error);
    314      1.41    bouyer 		ata_bio->r_error = chp->ch_error;
    315      1.41    bouyer 		ata_bio->error = ERROR;
    316      1.41    bouyer 	}
    317      1.41    bouyer ctrldone:
    318      1.41    bouyer 	drvp->state = 0;
    319      1.41    bouyer 	wdc_ata_bio_done(chp, xfer);
    320      1.72   thorpej 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    321      1.41    bouyer 	return;
    322      1.20    bouyer }
    323      1.20    bouyer 
    324      1.45   thorpej static void
    325      1.72   thorpej _wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
    326      1.20    bouyer {
    327      1.73   thorpej 	struct atac_softc *atac = chp->ch_atac;
    328      1.73   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    329      1.72   thorpej 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    330      1.48   thorpej 	struct ata_bio *ata_bio = xfer->c_cmd;
    331      1.48   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    332      1.51   thorpej 	int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
    333       1.2    bouyer 	u_int16_t cyl;
    334       1.2    bouyer 	u_int8_t head, sect, cmd = 0;
    335      1.85     itohy 	int nblks;
    336      1.85     itohy #if NATA_DMA || NATA_PIOBM
    337      1.85     itohy 	int error, dma_flags = 0;
    338      1.85     itohy #endif
    339       1.2    bouyer 
    340      1.71   thorpej 	ATADEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",
    341      1.88      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
    342      1.20    bouyer 	    DEBUG_INTR | DEBUG_XFERS);
    343       1.2    bouyer 
    344      1.85     itohy #if NATA_DMA || NATA_PIOBM
    345      1.84     itohy 	if (xfer->c_flags & (C_DMA | C_PIOBM)) {
    346      1.85     itohy #if NATA_DMA
    347      1.23    bouyer 		if (drvp->n_xfers <= NXFER)
    348      1.23    bouyer 			drvp->n_xfers++;
    349      1.85     itohy #endif
    350       1.2    bouyer 		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;
    351      1.39  nakayama 		if (ata_bio->flags & ATA_LBA48)
    352      1.39  nakayama 			dma_flags |= WDC_DMA_LBA48;
    353       1.2    bouyer 	}
    354      1.85     itohy #endif
    355       1.2    bouyer again:
    356       1.2    bouyer 	/*
    357       1.2    bouyer 	 *
    358       1.2    bouyer 	 * When starting a multi-sector transfer, or doing single-sector
    359       1.2    bouyer 	 * transfers...
    360       1.2    bouyer 	 */
    361       1.2    bouyer 	if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
    362       1.2    bouyer 		if (ata_bio->flags & ATA_SINGLE)
    363       1.2    bouyer 			nblks = 1;
    364      1.79     perry 		else
    365       1.2    bouyer 			nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
    366       1.2    bouyer 		/* Check for bad sectors and adjust transfer, if necessary. */
    367       1.2    bouyer 		if ((ata_bio->lp->d_flags & D_BADSECT) != 0) {
    368       1.2    bouyer 			long blkdiff;
    369       1.2    bouyer 			int i;
    370       1.2    bouyer 			for (i = 0; (blkdiff = ata_bio->badsect[i]) != -1;
    371       1.2    bouyer 			    i++) {
    372       1.2    bouyer 				blkdiff -= ata_bio->blkno;
    373       1.2    bouyer 				if (blkdiff < 0)
    374       1.2    bouyer 					continue;
    375       1.2    bouyer 				if (blkdiff == 0) {
    376       1.2    bouyer 					/* Replace current block of transfer. */
    377       1.2    bouyer 					ata_bio->blkno =
    378       1.2    bouyer 					    ata_bio->lp->d_secperunit -
    379       1.2    bouyer 					    ata_bio->lp->d_nsectors - i - 1;
    380       1.2    bouyer 				}
    381       1.2    bouyer 				if (blkdiff < nblks) {
    382       1.2    bouyer 					/* Bad block inside transfer. */
    383       1.2    bouyer 					ata_bio->flags |= ATA_SINGLE;
    384       1.2    bouyer 					nblks = 1;
    385       1.2    bouyer 				}
    386       1.2    bouyer 				break;
    387       1.2    bouyer 			}
    388       1.2    bouyer 		/* Transfer is okay now. */
    389       1.2    bouyer 		}
    390      1.34  christos 		if (ata_bio->flags & ATA_LBA48) {
    391      1.34  christos 			sect = 0;
    392      1.34  christos 			cyl =  0;
    393      1.34  christos 			head = 0;
    394      1.34  christos 		} else if (ata_bio->flags & ATA_LBA) {
    395       1.2    bouyer 			sect = (ata_bio->blkno >> 0) & 0xff;
    396       1.2    bouyer 			cyl = (ata_bio->blkno >> 8) & 0xffff;
    397       1.2    bouyer 			head = (ata_bio->blkno >> 24) & 0x0f;
    398       1.2    bouyer 			head |= WDSD_LBA;
    399       1.2    bouyer 		} else {
    400       1.2    bouyer 			int blkno = ata_bio->blkno;
    401       1.2    bouyer 			sect = blkno % ata_bio->lp->d_nsectors;
    402       1.2    bouyer 			sect++;    /* Sectors begin with 1, not 0. */
    403       1.2    bouyer 			blkno /= ata_bio->lp->d_nsectors;
    404       1.2    bouyer 			head = blkno % ata_bio->lp->d_ntracks;
    405       1.2    bouyer 			blkno /= ata_bio->lp->d_ntracks;
    406       1.2    bouyer 			cyl = blkno;
    407       1.2    bouyer 			head |= WDSD_CHS;
    408       1.2    bouyer 		}
    409      1.85     itohy #if NATA_DMA
    410       1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    411       1.2    bouyer 			ata_bio->nblks = nblks;
    412       1.2    bouyer 			ata_bio->nbytes = xfer->c_bcount;
    413       1.2    bouyer 			cmd = (ata_bio->flags & ATA_READ) ?
    414       1.2    bouyer 			    WDCC_READDMA : WDCC_WRITEDMA;
    415       1.2    bouyer 	    		/* Init the DMA channel. */
    416      1.82   thorpej 			error = (*wdc->dma_init)(wdc->dma_arg,
    417      1.51   thorpej 			    chp->ch_channel, xfer->c_drive,
    418      1.79     perry 			    (char *)xfer->c_databuf + xfer->c_skip,
    419      1.82   thorpej 			    ata_bio->nbytes, dma_flags);
    420      1.82   thorpej 			if (error) {
    421      1.82   thorpej 				if (error == EINVAL) {
    422      1.82   thorpej 					/*
    423      1.82   thorpej 					 * We can't do DMA on this transfer
    424      1.82   thorpej 					 * for some reason.  Fall back to
    425      1.82   thorpej 					 * PIO.
    426      1.82   thorpej 					 */
    427      1.82   thorpej 					xfer->c_flags &= ~C_DMA;
    428      1.82   thorpej 					error = 0;
    429      1.82   thorpej 					goto do_pio;
    430      1.82   thorpej 				}
    431       1.2    bouyer 				ata_bio->error = ERR_DMA;
    432       1.2    bouyer 				ata_bio->r_error = 0;
    433       1.2    bouyer 				wdc_ata_bio_done(chp, xfer);
    434       1.2    bouyer 				return;
    435       1.2    bouyer 			}
    436       1.2    bouyer 			/* Initiate command */
    437      1.70   thorpej 			if (wdc->select)
    438      1.51   thorpej 				wdc->select(chp, xfer->c_drive);
    439      1.72   thorpej 			bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
    440      1.48   thorpej 			    0, WDSD_IBM | (xfer->c_drive << 4));
    441      1.49   thorpej 			switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
    442      1.41    bouyer 			case WDCWAIT_OK:
    443      1.79     perry 				break;
    444      1.41    bouyer 			case WDCWAIT_TOUT:
    445       1.2    bouyer 				goto timeout;
    446      1.41    bouyer 			case WDCWAIT_THR:
    447      1.41    bouyer 				return;
    448      1.41    bouyer 			}
    449      1.34  christos 			if (ata_bio->flags & ATA_LBA48) {
    450      1.75   thorpej 			    wdccommandext(chp, xfer->c_drive, atacmd_to48(cmd),
    451  1.95.2.1      yamt 				(u_int64_t)ata_bio->blkno, nblks, 0);
    452      1.34  christos 			} else {
    453      1.48   thorpej 			    wdccommand(chp, xfer->c_drive, cmd, cyl,
    454      1.34  christos 				head, sect, nblks, 0);
    455      1.34  christos 			}
    456       1.2    bouyer 			/* start the DMA channel */
    457      1.51   thorpej 			(*wdc->dma_start)(wdc->dma_arg,
    458      1.51   thorpej 			    chp->ch_channel, xfer->c_drive);
    459      1.72   thorpej 			chp->ch_flags |= ATACH_DMA_WAIT;
    460      1.41    bouyer 			/* start timeout machinery */
    461      1.41    bouyer 			if ((xfer->c_flags & C_POLL) == 0)
    462      1.41    bouyer 				callout_reset(&chp->ch_callout,
    463      1.41    bouyer 				    ATA_DELAY / 1000 * hz, wdctimeout, chp);
    464       1.2    bouyer 			/* wait for irq */
    465       1.2    bouyer 			goto intr;
    466       1.2    bouyer 		} /* else not DMA */
    467      1.82   thorpej  do_pio:
    468      1.85     itohy #endif	/* NATA_DMA */
    469      1.84     itohy #if NATA_PIOBM
    470      1.84     itohy 		if ((xfer->c_flags & C_PIOBM) && xfer->c_skip == 0) {
    471      1.84     itohy 			if (ata_bio->flags & ATA_POLL) {
    472      1.84     itohy 				/* XXX not supported yet --- fall back to PIO */
    473      1.84     itohy 				xfer->c_flags &= ~C_PIOBM;
    474      1.84     itohy 			} else {
    475      1.84     itohy 				/* Init the DMA channel. */
    476      1.84     itohy 				error = (*wdc->dma_init)(wdc->dma_arg,
    477      1.84     itohy 				    chp->ch_channel, xfer->c_drive,
    478      1.84     itohy 				    (char *)xfer->c_databuf + xfer->c_skip,
    479      1.84     itohy 				    xfer->c_bcount,
    480      1.84     itohy 				    dma_flags | WDC_DMA_PIOBM_ATA);
    481      1.84     itohy 				if (error) {
    482      1.84     itohy 					if (error == EINVAL) {
    483      1.84     itohy 						/*
    484      1.84     itohy 						 * We can't do DMA on this
    485      1.84     itohy 						 * transfer for some reason.
    486      1.84     itohy 						 * Fall back to PIO.
    487      1.84     itohy 						 */
    488      1.84     itohy 						xfer->c_flags &= ~C_PIOBM;
    489      1.84     itohy 						error = 0;
    490      1.84     itohy 					} else {
    491      1.84     itohy 						ata_bio->error = ERR_DMA;
    492      1.84     itohy 						ata_bio->r_error = 0;
    493      1.84     itohy 						wdc_ata_bio_done(chp, xfer);
    494      1.84     itohy 						return;
    495      1.84     itohy 					}
    496      1.84     itohy 				}
    497      1.84     itohy 			}
    498      1.84     itohy 		}
    499      1.84     itohy #endif
    500       1.2    bouyer 		ata_bio->nblks = min(nblks, ata_bio->multi);
    501       1.2    bouyer 		ata_bio->nbytes = ata_bio->nblks * ata_bio->lp->d_secsize;
    502      1.56   mycroft 		KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0);
    503      1.56   mycroft 		if (ata_bio->nblks > 1) {
    504       1.2    bouyer 			cmd = (ata_bio->flags & ATA_READ) ?
    505       1.2    bouyer 			    WDCC_READMULTI : WDCC_WRITEMULTI;
    506       1.2    bouyer 		} else {
    507       1.2    bouyer 			cmd = (ata_bio->flags & ATA_READ) ?
    508       1.2    bouyer 			    WDCC_READ : WDCC_WRITE;
    509       1.2    bouyer 		}
    510       1.2    bouyer 		/* Initiate command! */
    511      1.70   thorpej 		if (wdc->select)
    512      1.51   thorpej 			wdc->select(chp, xfer->c_drive);
    513      1.72   thorpej 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    514      1.48   thorpej 		    WDSD_IBM | (xfer->c_drive << 4));
    515      1.49   thorpej 		switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
    516      1.41    bouyer 		case WDCWAIT_OK:
    517      1.41    bouyer 			break;
    518      1.41    bouyer 		case WDCWAIT_TOUT:
    519       1.2    bouyer 			goto timeout;
    520      1.41    bouyer 		case WDCWAIT_THR:
    521      1.41    bouyer 			return;
    522      1.41    bouyer 		}
    523      1.34  christos 		if (ata_bio->flags & ATA_LBA48) {
    524      1.75   thorpej 		    wdccommandext(chp, xfer->c_drive, atacmd_to48(cmd),
    525  1.95.2.1      yamt 			(u_int64_t)ata_bio->blkno, nblks, 0);
    526      1.34  christos 		} else {
    527      1.48   thorpej 		    wdccommand(chp, xfer->c_drive, cmd, cyl,
    528      1.34  christos 			head, sect, nblks,
    529      1.34  christos 			(ata_bio->lp->d_type == DTYPE_ST506) ?
    530      1.34  christos 			ata_bio->lp->d_precompcyl / 4 : 0);
    531      1.34  christos 		}
    532      1.41    bouyer 		/* start timeout machinery */
    533      1.41    bouyer 		if ((xfer->c_flags & C_POLL) == 0)
    534      1.41    bouyer 			callout_reset(&chp->ch_callout,
    535      1.41    bouyer 			    ATA_DELAY / 1000 * hz, wdctimeout, chp);
    536       1.2    bouyer 	} else if (ata_bio->nblks > 1) {
    537       1.2    bouyer 		/* The number of blocks in the last stretch may be smaller. */
    538       1.2    bouyer 		nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
    539       1.2    bouyer 		if (ata_bio->nblks > nblks) {
    540       1.2    bouyer 		ata_bio->nblks = nblks;
    541       1.2    bouyer 		ata_bio->nbytes = xfer->c_bcount;
    542       1.2    bouyer 		}
    543       1.2    bouyer 	}
    544       1.2    bouyer 	/* If this was a write and not using DMA, push the data. */
    545       1.2    bouyer 	if ((ata_bio->flags & ATA_READ) == 0) {
    546      1.41    bouyer 		/*
    547      1.41    bouyer 		 * we have to busy-wait here, we can't rely on running in
    548      1.41    bouyer 		 * thread context.
    549      1.41    bouyer 		 */
    550      1.49   thorpej 		if (wdc_wait_for_drq(chp, ATA_DELAY, AT_POLL) != 0) {
    551       1.2    bouyer 			printf("%s:%d:%d: timeout waiting for DRQ, "
    552       1.2    bouyer 			    "st=0x%02x, err=0x%02x\n",
    553      1.88      cube 			    device_xname(atac->atac_dev), chp->ch_channel,
    554      1.48   thorpej 			    xfer->c_drive, chp->ch_status, chp->ch_error);
    555      1.16    bouyer 			if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
    556       1.2    bouyer 				ata_bio->error = TIMEOUT;
    557       1.2    bouyer 			wdc_ata_bio_done(chp, xfer);
    558       1.2    bouyer 			return;
    559       1.2    bouyer 		}
    560      1.16    bouyer 		if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {
    561       1.2    bouyer 			wdc_ata_bio_done(chp, xfer);
    562       1.2    bouyer 			return;
    563       1.2    bouyer 		}
    564      1.84     itohy #if NATA_PIOBM
    565      1.84     itohy 		if (xfer->c_flags & C_PIOBM) {
    566      1.84     itohy 			/* start the busmastering PIO */
    567      1.84     itohy 			(*wdc->piobm_start)(wdc->dma_arg,
    568      1.84     itohy 			    chp->ch_channel, xfer->c_drive,
    569      1.84     itohy 			    xfer->c_skip, ata_bio->nbytes, 0);
    570      1.84     itohy 			chp->ch_flags |= ATACH_DMA_WAIT;
    571      1.84     itohy 		} else
    572      1.84     itohy #endif
    573      1.84     itohy 
    574      1.63   mycroft 		wdc->dataout_pio(chp, drvp->drive_flags,
    575      1.62   mycroft 		    (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
    576       1.2    bouyer 	}
    577       1.2    bouyer 
    578      1.85     itohy #if NATA_DMA
    579      1.85     itohy intr:
    580      1.85     itohy #endif
    581      1.85     itohy 	/* Wait for IRQ (either real or polled) */
    582       1.2    bouyer 	if ((ata_bio->flags & ATA_POLL) == 0) {
    583      1.72   thorpej 		chp->ch_flags |= ATACH_IRQ_WAIT;
    584       1.2    bouyer 	} else {
    585       1.2    bouyer 		/* Wait for at last 400ns for status bit to be valid */
    586       1.2    bouyer 		delay(1);
    587      1.85     itohy #if NATA_DMA
    588      1.72   thorpej 		if (chp->ch_flags & ATACH_DMA_WAIT) {
    589      1.26    bouyer 			wdc_dmawait(chp, xfer, ATA_DELAY);
    590      1.72   thorpej 			chp->ch_flags &= ~ATACH_DMA_WAIT;
    591      1.26    bouyer 		}
    592      1.85     itohy #endif
    593      1.19    bouyer 		wdc_ata_bio_intr(chp, xfer, 0);
    594       1.2    bouyer 		if ((ata_bio->flags & ATA_ITSDONE) == 0)
    595       1.2    bouyer 			goto again;
    596       1.2    bouyer 	}
    597       1.2    bouyer 	return;
    598       1.2    bouyer timeout:
    599       1.2    bouyer 	printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",
    600      1.88      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    601       1.2    bouyer 	    chp->ch_status, chp->ch_error);
    602      1.16    bouyer 	if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
    603       1.2    bouyer 		ata_bio->error = TIMEOUT;
    604       1.2    bouyer 	wdc_ata_bio_done(chp, xfer);
    605       1.2    bouyer 	return;
    606       1.2    bouyer }
    607       1.2    bouyer 
    608      1.45   thorpej static int
    609      1.72   thorpej wdc_ata_bio_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
    610       1.2    bouyer {
    611      1.73   thorpej 	struct atac_softc *atac = chp->ch_atac;
    612      1.73   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    613      1.48   thorpej 	struct ata_bio *ata_bio = xfer->c_cmd;
    614      1.48   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    615       1.2    bouyer 	int drv_err;
    616       1.2    bouyer 
    617      1.71   thorpej 	ATADEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
    618      1.88      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
    619       1.2    bouyer 	    DEBUG_INTR | DEBUG_XFERS);
    620       1.2    bouyer 
    621       1.2    bouyer 
    622       1.2    bouyer 	/* Is it not a transfer, but a control operation? */
    623       1.2    bouyer 	if (drvp->state < READY) {
    624       1.2    bouyer 		printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
    625      1.88      cube 		    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    626       1.2    bouyer 		    drvp->state);
    627      1.38    provos 		panic("wdc_ata_bio_intr: bad state");
    628       1.2    bouyer 	}
    629       1.2    bouyer 
    630      1.20    bouyer 	/*
    631      1.20    bouyer 	 * if we missed an interrupt in a PIO transfer, reset and restart.
    632      1.20    bouyer 	 * Don't try to continue transfer, we may have missed cycles.
    633      1.20    bouyer 	 */
    634      1.20    bouyer 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    635      1.20    bouyer 		ata_bio->error = TIMEOUT;
    636      1.20    bouyer 		wdc_ata_bio_done(chp, xfer);
    637      1.20    bouyer 		return 1;
    638      1.20    bouyer 	}
    639      1.20    bouyer 
    640      1.84     itohy #if NATA_PIOBM
    641      1.84     itohy 	/* Transfer-done interrupt for busmastering PIO read */
    642      1.84     itohy 	if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
    643      1.84     itohy 		chp->ch_flags &= ~ATACH_PIOBM_WAIT;
    644      1.84     itohy 		goto end;
    645      1.84     itohy 	}
    646      1.84     itohy #endif
    647      1.84     itohy 
    648      1.49   thorpej 	/* Ack interrupt done by wdc_wait_for_unbusy */
    649      1.49   thorpej 	if (wdc_wait_for_unbusy(chp, (irq == 0) ? ATA_DELAY : 0, AT_POLL) < 0) {
    650      1.19    bouyer 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    651      1.18    bouyer 			return 0; /* IRQ was not for us */
    652       1.2    bouyer 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
    653      1.88      cube 		    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    654       1.2    bouyer 		    xfer->c_bcount, xfer->c_skip);
    655       1.2    bouyer 		ata_bio->error = TIMEOUT;
    656       1.2    bouyer 		wdc_ata_bio_done(chp, xfer);
    657       1.2    bouyer 		return 1;
    658       1.2    bouyer 	}
    659      1.70   thorpej 	if (wdc->irqack)
    660      1.51   thorpej 		wdc->irqack(chp);
    661      1.79     perry 
    662      1.16    bouyer 	drv_err = wdc_ata_err(drvp, ata_bio);
    663       1.2    bouyer 
    664      1.85     itohy #if NATA_DMA
    665       1.2    bouyer 	/* If we were using DMA, Turn off the DMA channel and check for error */
    666       1.2    bouyer 	if (xfer->c_flags & C_DMA) {
    667       1.2    bouyer 		if (ata_bio->flags & ATA_POLL) {
    668       1.2    bouyer 			/*
    669      1.13    bouyer 			 * IDE drives deassert WDCS_BSY before transfer is
    670       1.2    bouyer 			 * complete when using DMA. Polling for DRQ to deassert
    671      1.37       wiz 			 * is not enough DRQ is not required to be
    672       1.7    bouyer 			 * asserted for DMA transfers, so poll for DRDY.
    673       1.2    bouyer 			 */
    674       1.2    bouyer 			if (wdcwait(chp, WDCS_DRDY | WDCS_DRQ, WDCS_DRDY,
    675      1.41    bouyer 			    ATA_DELAY, ATA_POLL) == WDCWAIT_TOUT) {
    676       1.7    bouyer 				printf("%s:%d:%d: polled transfer timed out "
    677      1.88      cube 				    "(st=0x%x)\n",
    678      1.88      cube 				    device_xname(atac->atac_dev),
    679      1.51   thorpej 				    chp->ch_channel, xfer->c_drive,
    680      1.48   thorpej 				    chp->ch_status);
    681       1.2    bouyer 				ata_bio->error = TIMEOUT;
    682      1.10    bouyer 				drv_err = WDC_ATA_ERR;
    683      1.10    bouyer 			}
    684      1.10    bouyer 		}
    685      1.51   thorpej 		if (wdc->dma_status != 0) {
    686      1.10    bouyer 			if (drv_err != WDC_ATA_ERR) {
    687      1.10    bouyer 				ata_bio->error = ERR_DMA;
    688      1.10    bouyer 				drv_err = WDC_ATA_ERR;
    689       1.2    bouyer 			}
    690       1.2    bouyer 		}
    691       1.2    bouyer 		if (chp->ch_status & WDCS_DRQ) {
    692       1.2    bouyer 			if (drv_err != WDC_ATA_ERR) {
    693       1.2    bouyer 				printf("%s:%d:%d: intr with DRQ (st=0x%x)\n",
    694      1.88      cube 				    device_xname(atac->atac_dev),
    695      1.88      cube 				    chp->ch_channel,
    696      1.48   thorpej 				    xfer->c_drive, chp->ch_status);
    697       1.2    bouyer 				ata_bio->error = TIMEOUT;
    698       1.2    bouyer 				drv_err = WDC_ATA_ERR;
    699       1.2    bouyer 			}
    700       1.2    bouyer 		}
    701      1.77    bouyer 		if (drv_err != WDC_ATA_ERR)
    702      1.77    bouyer 			goto end;
    703      1.78    bouyer 		if (ata_bio->r_error & WDCE_CRC || ata_bio->error == ERR_DMA)
    704      1.55   mycroft 			ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    705       1.2    bouyer 	}
    706      1.85     itohy #endif	/* NATA_DMA */
    707       1.2    bouyer 
    708       1.2    bouyer 	/* if we had an error, end */
    709       1.2    bouyer 	if (drv_err == WDC_ATA_ERR) {
    710       1.2    bouyer 		wdc_ata_bio_done(chp, xfer);
    711       1.2    bouyer 		return 1;
    712       1.2    bouyer 	}
    713       1.2    bouyer 
    714       1.2    bouyer 	/* If this was a read and not using DMA, fetch the data. */
    715       1.2    bouyer 	if ((ata_bio->flags & ATA_READ) != 0) {
    716      1.14    bouyer 		if ((chp->ch_status & WDCS_DRQ) != WDCS_DRQ) {
    717       1.2    bouyer 			printf("%s:%d:%d: read intr before drq\n",
    718      1.88      cube 			    device_xname(atac->atac_dev), chp->ch_channel,
    719      1.48   thorpej 			    xfer->c_drive);
    720       1.2    bouyer 			ata_bio->error = TIMEOUT;
    721       1.2    bouyer 			wdc_ata_bio_done(chp, xfer);
    722       1.2    bouyer 			return 1;
    723       1.2    bouyer 		}
    724      1.84     itohy #if NATA_PIOBM
    725      1.84     itohy 		if (xfer->c_flags & C_PIOBM) {
    726      1.84     itohy 			/* start the busmastering PIO */
    727      1.84     itohy 			(*wdc->piobm_start)(wdc->dma_arg,
    728      1.84     itohy 			    chp->ch_channel, xfer->c_drive,
    729      1.84     itohy 			    xfer->c_skip, ata_bio->nbytes,
    730      1.84     itohy 			    WDC_PIOBM_XFER_IRQ);
    731      1.84     itohy 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT;
    732      1.84     itohy 			return 1;
    733      1.84     itohy 		} else
    734      1.84     itohy #endif
    735      1.63   mycroft 		wdc->datain_pio(chp, drvp->drive_flags,
    736      1.62   mycroft 		    (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
    737       1.2    bouyer 	}
    738       1.2    bouyer 
    739      1.85     itohy #if NATA_DMA || NATA_PIOBM
    740       1.2    bouyer end:
    741      1.85     itohy #endif
    742       1.2    bouyer 	ata_bio->blkno += ata_bio->nblks;
    743       1.2    bouyer 	ata_bio->blkdone += ata_bio->nblks;
    744       1.2    bouyer 	xfer->c_skip += ata_bio->nbytes;
    745       1.2    bouyer 	xfer->c_bcount -= ata_bio->nbytes;
    746       1.2    bouyer 	/* See if this transfer is complete. */
    747       1.2    bouyer 	if (xfer->c_bcount > 0) {
    748       1.2    bouyer 		if ((ata_bio->flags & ATA_POLL) == 0) {
    749       1.2    bouyer 			/* Start the next operation */
    750      1.20    bouyer 			_wdc_ata_bio_start(chp, xfer);
    751       1.2    bouyer 		} else {
    752      1.20    bouyer 			/* Let _wdc_ata_bio_start do the loop */
    753       1.2    bouyer 			return 1;
    754       1.2    bouyer 		}
    755       1.2    bouyer 	} else { /* Done with this transfer */
    756       1.2    bouyer 		ata_bio->error = NOERROR;
    757       1.2    bouyer 		wdc_ata_bio_done(chp, xfer);
    758       1.2    bouyer 	}
    759       1.2    bouyer 	return 1;
    760      1.22     enami }
    761      1.22     enami 
    762      1.45   thorpej static void
    763      1.72   thorpej wdc_ata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
    764      1.58    bouyer     int reason)
    765      1.22     enami {
    766      1.48   thorpej 	struct ata_bio *ata_bio = xfer->c_cmd;
    767      1.48   thorpej 	int drive = xfer->c_drive;
    768      1.22     enami 
    769      1.65   thorpej 	ata_free_xfer(chp, xfer);
    770      1.22     enami 
    771      1.22     enami 	ata_bio->flags |= ATA_ITSDONE;
    772      1.58    bouyer 	switch (reason) {
    773      1.58    bouyer 	case KILL_GONE:
    774      1.58    bouyer 		ata_bio->error = ERR_NODEV;
    775      1.58    bouyer 		break;
    776      1.58    bouyer 	case KILL_RESET:
    777      1.58    bouyer 		ata_bio->error = ERR_RESET;
    778      1.58    bouyer 		break;
    779      1.58    bouyer 	default:
    780      1.58    bouyer 		printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
    781      1.58    bouyer 		    reason);
    782      1.58    bouyer 		panic("wdc_ata_bio_kill_xfer");
    783      1.58    bouyer 	}
    784      1.22     enami 	ata_bio->r_error = WDCE_ABRT;
    785  1.95.2.2      yamt 	ATADEBUG_PRINT(("wdc_ata_bio_kill_xfer: drv_done\n"), DEBUG_XFERS);
    786      1.46   thorpej 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
    787       1.2    bouyer }
    788       1.2    bouyer 
    789      1.45   thorpej static void
    790      1.72   thorpej wdc_ata_bio_done(struct ata_channel *chp, struct ata_xfer *xfer)
    791       1.2    bouyer {
    792      1.48   thorpej 	struct ata_bio *ata_bio = xfer->c_cmd;
    793      1.48   thorpej 	int drive = xfer->c_drive;
    794       1.2    bouyer 
    795      1.71   thorpej 	ATADEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
    796      1.88      cube 	    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    797      1.88      cube 	    xfer->c_drive, (u_int)xfer->c_flags),
    798       1.4    bouyer 	    DEBUG_XFERS);
    799      1.10    bouyer 
    800      1.24   thorpej 	callout_stop(&chp->ch_callout);
    801       1.2    bouyer 
    802       1.2    bouyer 	/* feed back residual bcount to our caller */
    803       1.2    bouyer 	ata_bio->bcount = xfer->c_bcount;
    804       1.2    bouyer 
    805      1.60    bouyer 	/* mark controller inactive and free xfer */
    806      1.60    bouyer 	chp->ch_queue->active_xfer = NULL;
    807      1.65   thorpej 	ata_free_xfer(chp, xfer);
    808       1.2    bouyer 
    809  1.95.2.2      yamt 	if (chp->ch_drive[drive].drive_flags & ATA_DRIVE_WAITDRAIN) {
    810      1.61    bouyer 		ata_bio->error = ERR_NODEV;
    811  1.95.2.2      yamt 		chp->ch_drive[drive].drive_flags &= ~ATA_DRIVE_WAITDRAIN;
    812      1.61    bouyer 		wakeup(&chp->ch_queue->active_xfer);
    813      1.61    bouyer 	}
    814       1.2    bouyer 	ata_bio->flags |= ATA_ITSDONE;
    815      1.71   thorpej 	ATADEBUG_PRINT(("wdc_ata_done: drv_done\n"), DEBUG_XFERS);
    816      1.46   thorpej 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
    817      1.71   thorpej 	ATADEBUG_PRINT(("atastart from wdc_ata_done, flags 0x%x\n",
    818       1.4    bouyer 	    chp->ch_flags), DEBUG_XFERS);
    819      1.69   thorpej 	atastart(chp);
    820       1.2    bouyer }
    821       1.2    bouyer 
    822      1.45   thorpej static int
    823      1.44   thorpej wdc_ata_err(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
    824       1.2    bouyer {
    825      1.72   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    826       1.2    bouyer 	ata_bio->error = 0;
    827       1.2    bouyer 	if (chp->ch_status & WDCS_BSY) {
    828       1.2    bouyer 		ata_bio->error = TIMEOUT;
    829       1.2    bouyer 		return WDC_ATA_ERR;
    830       1.2    bouyer 	}
    831       1.2    bouyer 
    832       1.2    bouyer 	if (chp->ch_status & WDCS_DWF) {
    833       1.2    bouyer 		ata_bio->error = ERR_DF;
    834       1.2    bouyer 		return WDC_ATA_ERR;
    835       1.2    bouyer 	}
    836       1.2    bouyer 
    837       1.2    bouyer 	if (chp->ch_status & WDCS_ERR) {
    838       1.2    bouyer 		ata_bio->error = ERROR;
    839       1.2    bouyer 		ata_bio->r_error = chp->ch_error;
    840       1.2    bouyer 		if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
    841       1.2    bouyer 		    WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
    842       1.2    bouyer 			return WDC_ATA_ERR;
    843       1.2    bouyer 		return WDC_ATA_NOERR;
    844       1.2    bouyer 	}
    845       1.2    bouyer 
    846       1.2    bouyer 	if (chp->ch_status & WDCS_CORR)
    847       1.2    bouyer 		ata_bio->flags |= ATA_CORR;
    848       1.2    bouyer 	return WDC_ATA_NOERR;
    849       1.8   thorpej }
    850       1.8   thorpej 
    851      1.45   thorpej static int
    852      1.44   thorpej wdc_ata_addref(struct ata_drive_datas *drvp)
    853       1.8   thorpej {
    854      1.72   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    855       1.8   thorpej 
    856      1.67   thorpej 	return (ata_addref(chp));
    857       1.8   thorpej }
    858       1.8   thorpej 
    859      1.45   thorpej static void
    860      1.44   thorpej wdc_ata_delref(struct ata_drive_datas *drvp)
    861       1.8   thorpej {
    862      1.72   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    863       1.8   thorpej 
    864      1.67   thorpej 	ata_delref(chp);
    865       1.2    bouyer }
    866