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