Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.29
      1  1.29    bouyer /*	$NetBSD: atapi_wdc.c,v 1.29 1999/11/04 21:16:53 bouyer Exp $	*/
      2   1.2    bouyer 
      3   1.2    bouyer /*
      4   1.2    bouyer  * Copyright (c) 1998 Manuel Bouyer.
      5   1.2    bouyer  *
      6   1.2    bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2    bouyer  * modification, are permitted provided that the following conditions
      8   1.2    bouyer  * are met:
      9   1.2    bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2    bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2    bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2    bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2    bouyer  *    must display the following acknowledgement:
     16   1.2    bouyer  *	This product includes software developed by the University of
     17   1.2    bouyer  *	California, Berkeley and its contributors.
     18   1.2    bouyer  * 4. Neither the name of the University nor the names of its contributors
     19   1.2    bouyer  *    may be used to endorse or promote products derived from this software
     20   1.2    bouyer  *    without specific prior written permission.
     21   1.2    bouyer  *
     22   1.2    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.2    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.2    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.2    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.2    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.2    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.2    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.2    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.2    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.2    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.2    bouyer  * SUCH DAMAGE.
     33   1.2    bouyer  *
     34   1.2    bouyer  */
     35   1.2    bouyer 
     36  1.18   hubertf #ifndef WDCDEBUG
     37   1.2    bouyer #define WDCDEBUG
     38  1.18   hubertf #endif /* WDCDEBUG */
     39   1.2    bouyer 
     40   1.2    bouyer #include <sys/param.h>
     41   1.2    bouyer #include <sys/systm.h>
     42   1.2    bouyer #include <sys/kernel.h>
     43   1.2    bouyer #include <sys/file.h>
     44   1.2    bouyer #include <sys/stat.h>
     45   1.2    bouyer #include <sys/buf.h>
     46   1.2    bouyer #include <sys/malloc.h>
     47   1.2    bouyer #include <sys/device.h>
     48   1.2    bouyer #include <sys/syslog.h>
     49   1.2    bouyer #include <sys/proc.h>
     50   1.2    bouyer 
     51   1.2    bouyer #include <vm/vm.h>
     52   1.2    bouyer 
     53   1.2    bouyer #include <machine/intr.h>
     54   1.2    bouyer #include <machine/bus.h>
     55   1.2    bouyer 
     56   1.2    bouyer #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     57   1.2    bouyer #define    bus_space_write_multi_stream_2    bus_space_write_multi_2
     58   1.2    bouyer #define    bus_space_write_multi_stream_4    bus_space_write_multi_4
     59   1.2    bouyer #define    bus_space_read_multi_stream_2    bus_space_read_multi_2
     60   1.2    bouyer #define    bus_space_read_multi_stream_4    bus_space_read_multi_4
     61   1.2    bouyer #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     62   1.2    bouyer 
     63   1.2    bouyer #include <dev/ata/atareg.h>
     64   1.2    bouyer #include <dev/ata/atavar.h>
     65   1.2    bouyer #include <dev/ic/wdcreg.h>
     66   1.2    bouyer #include <dev/ic/wdcvar.h>
     67   1.2    bouyer #include <dev/scsipi/scsipi_all.h>
     68   1.2    bouyer #include <dev/scsipi/scsipiconf.h>
     69   1.2    bouyer #include <dev/scsipi/atapiconf.h>
     70   1.2    bouyer 
     71   1.2    bouyer #define DEBUG_INTR   0x01
     72   1.2    bouyer #define DEBUG_XFERS  0x02
     73   1.2    bouyer #define DEBUG_STATUS 0x04
     74   1.2    bouyer #define DEBUG_FUNCS  0x08
     75   1.2    bouyer #define DEBUG_PROBE  0x10
     76   1.2    bouyer #ifdef WDCDEBUG
     77   1.3    bouyer int wdcdebug_atapi_mask = 0;
     78   1.2    bouyer #define WDCDEBUG_PRINT(args, level) \
     79   1.2    bouyer 	if (wdcdebug_atapi_mask & (level)) \
     80   1.2    bouyer 		printf args
     81   1.2    bouyer #else
     82   1.2    bouyer #define WDCDEBUG_PRINT(args, level)
     83   1.2    bouyer #endif
     84   1.2    bouyer 
     85   1.2    bouyer #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     86   1.2    bouyer 
     87   1.2    bouyer void  wdc_atapi_minphys  __P((struct buf *bp));
     88   1.2    bouyer void  wdc_atapi_start	__P((struct channel_softc *,struct wdc_xfer *));
     89  1.20    bouyer int   wdc_atapi_intr	 __P((struct channel_softc *, struct wdc_xfer *, int));
     90  1.28     enami void  wdc_atapi_kill_xfer __P((struct channel_softc *, struct wdc_xfer *));
     91  1.20    bouyer int   wdc_atapi_ctrl	 __P((struct channel_softc *, struct wdc_xfer *, int));
     92   1.2    bouyer void  wdc_atapi_done	 __P((struct channel_softc *, struct wdc_xfer *));
     93   1.2    bouyer void  wdc_atapi_reset	 __P((struct channel_softc *, struct wdc_xfer *));
     94   1.2    bouyer int   wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
     95   1.2    bouyer 
     96   1.2    bouyer #define MAX_SIZE MAXPHYS
     97   1.2    bouyer 
     98   1.2    bouyer void
     99   1.2    bouyer wdc_atapibus_attach(chp)
    100   1.2    bouyer 	struct channel_softc *chp;
    101   1.2    bouyer {
    102   1.2    bouyer 	struct wdc_softc *wdc = chp->wdc;
    103   1.2    bouyer 	int channel = chp->channel;
    104   1.2    bouyer 	struct ata_atapi_attach aa_link;
    105   1.2    bouyer 
    106   1.9   thorpej 	/*
    107   1.9   thorpej 	 * Fill in the adapter.
    108   1.9   thorpej 	 */
    109   1.9   thorpej 	wdc->sc_atapi_adapter.scsipi_cmd = wdc_atapi_send_cmd;
    110   1.9   thorpej 	wdc->sc_atapi_adapter.scsipi_minphys = wdc_atapi_minphys;
    111   1.9   thorpej 
    112   1.2    bouyer 	memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
    113   1.2    bouyer 	aa_link.aa_type = T_ATAPI;
    114   1.2    bouyer 	aa_link.aa_channel = channel;
    115   1.2    bouyer 	aa_link.aa_openings = 1;
    116   1.2    bouyer 	aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
    117   1.9   thorpej 	aa_link.aa_bus_private = &wdc->sc_atapi_adapter;
    118  1.26     enami 	chp->atapibus = config_found(&wdc->sc_dev, (void *)&aa_link,
    119  1.26     enami 	    atapi_print);
    120   1.2    bouyer }
    121   1.2    bouyer 
    122   1.2    bouyer void
    123   1.2    bouyer wdc_atapi_minphys (struct buf *bp)
    124   1.2    bouyer {
    125   1.2    bouyer 	if(bp->b_bcount > MAX_SIZE)
    126   1.2    bouyer 		bp->b_bcount = MAX_SIZE;
    127   1.2    bouyer 	minphys(bp);
    128   1.2    bouyer }
    129   1.2    bouyer 
    130  1.28     enami /*
    131  1.28     enami  * Kill off all pending xfers for a scsipi_link.
    132  1.28     enami  *
    133  1.28     enami  * Must be called at splbio().
    134  1.28     enami  */
    135  1.28     enami void
    136  1.28     enami atapi_kill_pending(sc_link)
    137  1.28     enami 	struct scsipi_link *sc_link;
    138  1.28     enami {
    139  1.28     enami 	struct wdc_softc *wdc = (void *)sc_link->adapter_softc;
    140  1.28     enami 	struct channel_softc *chp =
    141  1.28     enami 	    wdc->channels[sc_link->scsipi_atapi.channel];
    142  1.28     enami 
    143  1.28     enami 	wdc_kill_pending(chp);
    144  1.28     enami }
    145  1.28     enami 
    146  1.28     enami void
    147  1.28     enami wdc_atapi_kill_xfer(chp, xfer)
    148  1.28     enami 	struct channel_softc *chp;
    149  1.28     enami 	struct wdc_xfer *xfer;
    150  1.28     enami {
    151  1.28     enami 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    152  1.28     enami 
    153  1.28     enami 	untimeout(wdctimeout, chp);
    154  1.28     enami 	/* remove this command from xfer queue */
    155  1.28     enami 	wdc_free_xfer(chp, xfer);
    156  1.28     enami 	sc_xfer->xs_status |= XS_STS_DONE;
    157  1.28     enami 	sc_xfer->error = XS_DRIVER_STUFFUP;
    158  1.28     enami 	scsipi_done(sc_xfer);
    159  1.28     enami }
    160  1.28     enami 
    161   1.2    bouyer int
    162   1.2    bouyer wdc_atapi_get_params(ab_link, drive, flags, id)
    163   1.2    bouyer 	struct scsipi_link *ab_link;
    164   1.2    bouyer 	u_int8_t drive;
    165   1.2    bouyer 	int flags;
    166   1.2    bouyer 	struct ataparams *id;
    167   1.2    bouyer {
    168   1.2    bouyer 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
    169   1.2    bouyer 	struct channel_softc *chp =
    170  1.10  drochner 	    wdc->channels[ab_link->scsipi_atapi.channel];
    171   1.2    bouyer 	struct wdc_command wdc_c;
    172   1.2    bouyer 
    173   1.2    bouyer 	/* if no ATAPI device detected at wdc attach time, skip */
    174   1.2    bouyer 	/*
    175   1.2    bouyer 	 * XXX this will break scsireprobe if this is of any interest for
    176   1.2    bouyer 	 * ATAPI devices one day.
    177   1.2    bouyer 	 */
    178   1.2    bouyer 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    179   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    180   1.2    bouyer 		    drive), DEBUG_PROBE);
    181   1.2    bouyer 		return -1;
    182   1.2    bouyer 	}
    183   1.2    bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    184   1.2    bouyer 	wdc_c.r_command = ATAPI_SOFT_RESET;
    185   1.2    bouyer 	wdc_c.r_st_bmask = 0;
    186   1.2    bouyer 	wdc_c.r_st_pmask = 0;
    187   1.2    bouyer 	wdc_c.flags = AT_POLL;
    188   1.2    bouyer 	wdc_c.timeout = WDC_RESET_WAIT;
    189   1.2    bouyer 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    190   1.2    bouyer 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    191   1.2    bouyer 		    " drive %s:%d:%d: driver failed\n",
    192   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
    193   1.2    bouyer 		panic("wdc_atapi_get_params");
    194   1.2    bouyer 	}
    195   1.2    bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    196   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    197   1.2    bouyer 		    "failed for drive %s:%d:%d: error 0x%x\n",
    198   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    199   1.2    bouyer 		    wdc_c.r_error), DEBUG_PROBE);
    200   1.2    bouyer 		return -1;
    201   1.2    bouyer 	}
    202   1.2    bouyer 	chp->ch_drive[drive].state = 0;
    203   1.2    bouyer 
    204   1.2    bouyer 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
    205   1.2    bouyer 
    206   1.2    bouyer 	/* Some ATAPI devices need a bit more time after software reset. */
    207   1.2    bouyer 	delay(5000);
    208   1.2    bouyer 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
    209   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    210   1.2    bouyer 		    "failed for drive %s:%d:%d: error 0x%x\n",
    211   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
    212   1.2    bouyer 		    wdc_c.r_error), DEBUG_PROBE);
    213   1.2    bouyer 		return -1;
    214   1.2    bouyer 	}
    215   1.2    bouyer 	return COMPLETE;
    216   1.2    bouyer }
    217   1.2    bouyer 
    218   1.2    bouyer int
    219   1.2    bouyer wdc_atapi_send_cmd(sc_xfer)
    220   1.2    bouyer 	struct scsipi_xfer *sc_xfer;
    221   1.2    bouyer {
    222  1.12    bouyer 	struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc;
    223   1.2    bouyer 	struct wdc_xfer *xfer;
    224  1.27   thorpej 	int flags = sc_xfer->xs_control;
    225   1.2    bouyer 	int channel = sc_xfer->sc_link->scsipi_atapi.channel;
    226   1.2    bouyer 	int drive = sc_xfer->sc_link->scsipi_atapi.drive;
    227   1.2    bouyer 	int s, ret;
    228   1.2    bouyer 
    229   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n",
    230   1.5    bouyer 	    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
    231   1.2    bouyer 
    232  1.27   thorpej 	xfer = wdc_get_xfer((flags & XS_CTL_NOSLEEP) ?
    233  1.27   thorpej 	    WDC_NOSLEEP : WDC_CANSLEEP);
    234   1.2    bouyer 	if (xfer == NULL) {
    235   1.2    bouyer 		return TRY_AGAIN_LATER;
    236   1.2    bouyer 	}
    237  1.27   thorpej 	if (sc_xfer->xs_control & XS_CTL_POLL)
    238   1.2    bouyer 		xfer->c_flags |= C_POLL;
    239   1.2    bouyer 	xfer->drive = drive;
    240   1.2    bouyer 	xfer->c_flags |= C_ATAPI;
    241   1.2    bouyer 	xfer->cmd = sc_xfer;
    242   1.2    bouyer 	xfer->databuf = sc_xfer->data;
    243   1.2    bouyer 	xfer->c_bcount = sc_xfer->datalen;
    244   1.2    bouyer 	xfer->c_start = wdc_atapi_start;
    245   1.2    bouyer 	xfer->c_intr = wdc_atapi_intr;
    246  1.28     enami 	xfer->c_kill_xfer = wdc_atapi_kill_xfer;
    247   1.2    bouyer 	s = splbio();
    248  1.10  drochner 	wdc_exec_xfer(wdc->channels[channel], xfer);
    249   1.2    bouyer #ifdef DIAGNOSTIC
    250  1.27   thorpej 	if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    251  1.27   thorpej 	    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    252   1.2    bouyer 		panic("wdc_atapi_send_cmd: polled command not done");
    253   1.2    bouyer #endif
    254  1.27   thorpej 	ret = (sc_xfer->xs_status & XS_STS_DONE) ?
    255  1.27   thorpej 	    COMPLETE : SUCCESSFULLY_QUEUED;
    256   1.2    bouyer 	splx(s);
    257   1.2    bouyer 	return ret;
    258   1.2    bouyer }
    259   1.2    bouyer 
    260   1.2    bouyer void
    261   1.2    bouyer wdc_atapi_start(chp, xfer)
    262   1.2    bouyer 	struct channel_softc *chp;
    263   1.2    bouyer 	struct wdc_xfer *xfer;
    264   1.2    bouyer {
    265   1.2    bouyer 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    266   1.2    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    267   1.2    bouyer 
    268   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    269   1.5    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
    270  1.27   thorpej 	    sc_xfer->xs_control), DEBUG_XFERS);
    271  1.19    bouyer 	/* Adjust C_DMA, it may have changed if we are requesting sense */
    272  1.19    bouyer 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
    273  1.19    bouyer 	    (sc_xfer->datalen > 0 || (xfer->c_flags & C_SENSE)))
    274  1.19    bouyer 		xfer->c_flags |= C_DMA;
    275  1.19    bouyer 	else
    276  1.19    bouyer 		xfer->c_flags &= ~C_DMA;
    277  1.24    bouyer 	/* start timeout machinery */
    278  1.27   thorpej 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    279  1.24    bouyer 		timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
    280   1.2    bouyer 	/* Do control operations specially. */
    281   1.2    bouyer 	if (drvp->state < READY) {
    282   1.2    bouyer 		if (drvp->state != PIOMODE) {
    283   1.2    bouyer 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
    284   1.2    bouyer 			    chp->wdc->sc_dev.dv_xname, chp->channel,
    285   1.2    bouyer 			    xfer->drive, drvp->state);
    286   1.2    bouyer 			panic("wdc_atapi_start: bad state");
    287   1.2    bouyer 		}
    288  1.20    bouyer 		wdc_atapi_ctrl(chp, xfer, 0);
    289   1.2    bouyer 		return;
    290   1.2    bouyer 	}
    291   1.2    bouyer 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    292   1.2    bouyer 	    WDSD_IBM | (xfer->drive << 4));
    293   1.2    bouyer 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
    294   1.2    bouyer 		printf("wdc_atapi_start: not ready, st = %02x\n",
    295   1.2    bouyer 		    chp->ch_status);
    296   1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    297   1.2    bouyer 		wdc_atapi_reset(chp, xfer);
    298   1.2    bouyer 		return;
    299   1.2    bouyer 	}
    300   1.2    bouyer 
    301   1.2    bouyer 	/*
    302   1.2    bouyer 	 * Even with WDCS_ERR, the device should accept a command packet
    303   1.2    bouyer 	 * Limit length to what can be stuffed into the cylinder register
    304   1.2    bouyer 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    305   1.2    bouyer 	 * but not all devices do that and it's not obvious from the
    306   1.2    bouyer 	 * ATAPI spec that that behaviour should be expected.  If more
    307   1.2    bouyer 	 * data is necessary, multiple data transfer phases will be done.
    308   1.2    bouyer 	 */
    309   1.5    bouyer 
    310   1.2    bouyer 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
    311  1.29    bouyer 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    312   1.2    bouyer 	    0, 0, 0,
    313   1.2    bouyer 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    314   1.2    bouyer 
    315   1.2    bouyer 	/*
    316   1.2    bouyer 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    317   1.2    bouyer 	 * the interrupt routine. If it is a polled command, call the interrupt
    318   1.2    bouyer 	 * routine until command is done.
    319   1.2    bouyer 	 */
    320  1.21    bouyer 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & ATAPI_CFG_DRQ_MASK) !=
    321  1.27   thorpej 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    322   1.2    bouyer 		/* Wait for at last 400ns for status bit to be valid */
    323  1.20    bouyer 		DELAY(1);
    324  1.20    bouyer 		wdc_atapi_intr(chp, xfer, 0);
    325  1.21    bouyer 	} else {
    326  1.21    bouyer 		chp->ch_flags |= WDCF_IRQ_WAIT;
    327   1.2    bouyer 	}
    328  1.27   thorpej 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    329  1.27   thorpej 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    330   1.2    bouyer 			/* Wait for at last 400ns for status bit to be valid */
    331  1.20    bouyer 			DELAY(1);
    332  1.20    bouyer 			wdc_atapi_intr(chp, xfer, 0);
    333   1.2    bouyer 		}
    334   1.2    bouyer 	}
    335   1.2    bouyer }
    336   1.2    bouyer 
    337   1.2    bouyer int
    338  1.20    bouyer wdc_atapi_intr(chp, xfer, irq)
    339   1.2    bouyer 	struct channel_softc *chp;
    340   1.2    bouyer 	struct wdc_xfer *xfer;
    341  1.20    bouyer 	int irq;
    342   1.2    bouyer {
    343   1.2    bouyer 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    344   1.2    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    345   1.2    bouyer 	int len, phase, i, retries=0;
    346   1.2    bouyer 	int ire, dma_err = 0;
    347   1.2    bouyer 	int dma_flags = 0;
    348   1.8    bouyer 	struct scsipi_generic _cmd_reqsense;
    349   1.8    bouyer 	struct scsipi_sense *cmd_reqsense =
    350   1.8    bouyer 	    (struct scsipi_sense *)&_cmd_reqsense;
    351   1.8    bouyer 	void *cmd;
    352   1.2    bouyer 
    353   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    354   1.5    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
    355   1.2    bouyer 
    356   1.2    bouyer 	/* Is it not a transfer, but a control operation? */
    357   1.2    bouyer 	if (drvp->state < READY) {
    358   1.2    bouyer 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    359   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    360   1.2    bouyer 		    drvp->state);
    361   1.2    bouyer 		panic("wdc_atapi_intr: bad state\n");
    362   1.2    bouyer 	}
    363  1.24    bouyer 	/*
    364  1.24    bouyer 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    365  1.24    bouyer 	 * Don't try to continue transfer, we may have missed cycles.
    366  1.24    bouyer 	 */
    367  1.24    bouyer 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    368  1.24    bouyer 		sc_xfer->error = XS_TIMEOUT;
    369  1.24    bouyer 		wdc_atapi_reset(chp, xfer);
    370  1.25    bouyer 		return 1;
    371  1.24    bouyer 	}
    372  1.24    bouyer 
    373   1.2    bouyer 	/* Ack interrupt done in wait_for_unbusy */
    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.19    bouyer 	if (wait_for_unbusy(chp,
    377  1.20    bouyer 	    (irq == 0) ? sc_xfer->timeout : 0) != 0) {
    378  1.20    bouyer 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    379  1.19    bouyer 			return 0; /* IRQ was not for us */
    380  1.16    bouyer 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    381   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    382   1.2    bouyer 		    xfer->c_bcount, xfer->c_skip);
    383  1.11    bouyer 		if (xfer->c_flags & C_DMA)
    384  1.19    bouyer 			drvp->n_dmaerrs++;
    385   1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    386   1.2    bouyer 		wdc_atapi_reset(chp, xfer);
    387   1.2    bouyer 		return 1;
    388   1.2    bouyer 	}
    389  1.19    bouyer 	/* If we missed an IRQ and were using DMA, flag it as a DMA error */
    390  1.19    bouyer 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA))
    391  1.19    bouyer 		drvp->n_dmaerrs++;
    392   1.8    bouyer 	/*
    393   1.8    bouyer 	 * if the request sense command was aborted, report the short sense
    394   1.8    bouyer 	 * previously recorded, else continue normal processing
    395   1.8    bouyer 	 */
    396   1.8    bouyer 
    397   1.8    bouyer 	if ((xfer->c_flags & C_SENSE) != 0 &&
    398   1.8    bouyer 	    (chp->ch_status & WDCS_ERR) != 0 &&
    399   1.2    bouyer 	    (chp->ch_error & WDCE_ABRT) != 0) {
    400   1.8    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
    401   1.8    bouyer 		    "calling wdc_atapi_done(), sense 0x%x\n",
    402   1.8    bouyer 		    sc_xfer->sense.atapi_sense), DEBUG_INTR);
    403   1.2    bouyer 		wdc_atapi_done(chp, xfer);
    404   1.2    bouyer 		return 1;
    405   1.2    bouyer 	}
    406   1.2    bouyer 
    407   1.2    bouyer 	if (xfer->c_flags & C_DMA) {
    408  1.27   thorpej 		dma_flags = ((sc_xfer->xs_control & XS_CTL_DATA_IN) ||
    409  1.19    bouyer 		    (xfer->c_flags & C_SENSE)) ?  WDC_DMA_READ : 0;
    410  1.27   thorpej 		dma_flags |= sc_xfer->xs_control & XS_CTL_POLL ?
    411  1.27   thorpej 		    WDC_DMA_POLL : 0;
    412   1.2    bouyer 	}
    413   1.2    bouyer again:
    414   1.2    bouyer 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
    415   1.2    bouyer 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
    416   1.2    bouyer 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
    417   1.2    bouyer 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    418   1.2    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    419   1.2    bouyer 	    "ire 0x%x :", xfer->c_bcount,
    420   1.2    bouyer 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    421   1.2    bouyer 
    422   1.2    bouyer 	switch (phase) {
    423   1.2    bouyer 	case PHASE_CMDOUT:
    424   1.8    bouyer 		if (xfer->c_flags & C_SENSE) {
    425   1.8    bouyer 			memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
    426   1.8    bouyer 			cmd_reqsense->opcode = REQUEST_SENSE;
    427   1.8    bouyer 			cmd_reqsense->length = xfer->c_bcount;
    428   1.8    bouyer 			cmd = cmd_reqsense;
    429   1.8    bouyer 		} else {
    430   1.8    bouyer 			cmd  = sc_xfer->cmd;
    431   1.8    bouyer 		}
    432   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    433   1.2    bouyer 		/* Init the DMA channel if necessary */
    434   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    435   1.2    bouyer 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
    436   1.2    bouyer 			    chp->channel, xfer->drive,
    437   1.8    bouyer 			    xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
    438   1.2    bouyer 				sc_xfer->error = XS_DRIVER_STUFFUP;
    439   1.2    bouyer 				break;
    440   1.2    bouyer 			}
    441   1.2    bouyer 		}
    442   1.2    bouyer 		/* send packet command */
    443   1.2    bouyer 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    444   1.2    bouyer 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    445   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    446   1.2    bouyer 				bus_space_write_multi_4(chp->data32iot,
    447   1.2    bouyer 				    chp->data32ioh, 0,
    448   1.8    bouyer 				    (u_int32_t *)cmd,
    449   1.2    bouyer 				    sc_xfer->cmdlen >> 2);
    450   1.2    bouyer 			} else {
    451   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    452   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    453   1.8    bouyer 				    (u_int16_t *)cmd,
    454   1.2    bouyer 				    sc_xfer->cmdlen >> 1);
    455   1.2    bouyer 			}
    456   1.2    bouyer 		} else {
    457   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    458   1.2    bouyer 				bus_space_write_multi_stream_4(chp->data32iot,
    459   1.2    bouyer 				    chp->data32ioh, 0,
    460   1.8    bouyer 				    (u_int32_t *)cmd,
    461   1.2    bouyer 				    sc_xfer->cmdlen >> 2);
    462   1.2    bouyer 			} else {
    463   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    464   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    465   1.8    bouyer 				    (u_int16_t *)cmd,
    466   1.2    bouyer 				    sc_xfer->cmdlen >> 1);
    467   1.2    bouyer 			}
    468   1.2    bouyer 		}
    469   1.2    bouyer 		/* Start the DMA channel if necessary */
    470   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    471   1.2    bouyer 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
    472   1.2    bouyer 			    chp->channel, xfer->drive, dma_flags);
    473   1.2    bouyer 		}
    474   1.2    bouyer 
    475  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    476   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    477   1.2    bouyer 		}
    478   1.2    bouyer 		return 1;
    479   1.2    bouyer 
    480   1.2    bouyer 	 case PHASE_DATAOUT:
    481   1.2    bouyer 		/* write data */
    482   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    483  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    484   1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    485  1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    486   1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    487  1.11    bouyer 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    488  1.11    bouyer 				    chp->channel, xfer->drive, dma_flags);
    489  1.11    bouyer 				drvp->n_dmaerrs++;
    490   1.2    bouyer 			}
    491   1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    492   1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    493   1.2    bouyer 			return 1;
    494   1.2    bouyer 		}
    495   1.2    bouyer 		if (xfer->c_bcount < len) {
    496   1.2    bouyer 			printf("wdc_atapi_intr: warning: write only "
    497   1.2    bouyer 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    498   1.2    bouyer 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    499   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    500   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    501  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    502  1.14   thorpej 				                  xfer->c_skip),
    503   1.2    bouyer 				    xfer->c_bcount >> 1);
    504   1.2    bouyer 			} else {
    505   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    506   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    507  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    508  1.14   thorpej 				                  xfer->c_skip),
    509   1.2    bouyer 				    xfer->c_bcount >> 1);
    510   1.2    bouyer 			}
    511   1.2    bouyer 			for (i = xfer->c_bcount; i < len; i += 2)
    512   1.2    bouyer 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
    513   1.2    bouyer 				    wd_data, 0);
    514   1.2    bouyer 			xfer->c_skip += xfer->c_bcount;
    515   1.2    bouyer 			xfer->c_bcount = 0;
    516   1.2    bouyer 		} else {
    517   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    518   1.2    bouyer 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    519   1.2    bouyer 				bus_space_write_multi_4(chp->data32iot,
    520   1.2    bouyer 				    chp->data32ioh, 0,
    521  1.14   thorpej 				    (u_int32_t *)((char *)xfer->databuf +
    522  1.14   thorpej 				                  xfer->c_skip),
    523  1.13  augustss 				    len >> 2);
    524   1.2    bouyer 			    else
    525  1.12    bouyer 				bus_space_write_multi_stream_4(chp->data32iot,
    526  1.12    bouyer 				    chp->data32ioh, wd_data,
    527  1.14   thorpej 				    (u_int32_t *)((char *)xfer->databuf +
    528  1.14   thorpej 				                  xfer->c_skip),
    529  1.13  augustss 				    len >> 2);
    530   1.2    bouyer 
    531   1.2    bouyer 			    xfer->c_skip += len & 0xfffffffc;
    532   1.2    bouyer 			    xfer->c_bcount -= len & 0xfffffffc;
    533   1.2    bouyer 			    len = len & 0x03;
    534   1.2    bouyer 			}
    535   1.2    bouyer 			if (len > 0) {
    536   1.2    bouyer 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    537   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    538   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    539  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    540  1.14   thorpej 				                  xfer->c_skip),
    541  1.13  augustss 				    len >> 1);
    542   1.2    bouyer 			    else
    543   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    544   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    545  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    546  1.14   thorpej 				                  xfer->c_skip),
    547  1.13  augustss 				    len >> 1);
    548   1.2    bouyer 			    xfer->c_skip += len;
    549   1.2    bouyer 			    xfer->c_bcount -= len;
    550   1.2    bouyer 			}
    551   1.2    bouyer 		}
    552  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    553   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    554   1.2    bouyer 		}
    555   1.2    bouyer 		return 1;
    556   1.2    bouyer 
    557   1.2    bouyer 	case PHASE_DATAIN:
    558   1.2    bouyer 		/* Read data */
    559   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    560  1.27   thorpej 		if (((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 &&
    561   1.8    bouyer 		    (xfer->c_flags & C_SENSE) == 0) ||
    562   1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    563  1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    564   1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    565  1.11    bouyer 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    566  1.11    bouyer 				    chp->channel, xfer->drive, dma_flags);
    567  1.11    bouyer 				drvp->n_dmaerrs++;
    568   1.2    bouyer 			}
    569   1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    570   1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    571   1.2    bouyer 			return 1;
    572   1.2    bouyer 		}
    573   1.2    bouyer 		if (xfer->c_bcount < len) {
    574   1.2    bouyer 			printf("wdc_atapi_intr: warning: reading only "
    575   1.2    bouyer 			    "%d of %d bytes\n", xfer->c_bcount, len);
    576   1.2    bouyer 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    577   1.2    bouyer 			    bus_space_read_multi_2(chp->cmd_iot,
    578   1.2    bouyer 			    chp->cmd_ioh, wd_data,
    579  1.14   thorpej 			    (u_int16_t *)((char *)xfer->databuf +
    580  1.14   thorpej 			                  xfer->c_skip),
    581  1.13  augustss 			    xfer->c_bcount >> 1);
    582   1.2    bouyer 			} else {
    583   1.2    bouyer 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    584   1.2    bouyer 			    chp->cmd_ioh, wd_data,
    585  1.14   thorpej 			    (u_int16_t *)((char *)xfer->databuf +
    586  1.14   thorpej 			                  xfer->c_skip),
    587  1.13  augustss 			    xfer->c_bcount >> 1);
    588   1.2    bouyer 			}
    589   1.2    bouyer 			wdcbit_bucket(chp, len - xfer->c_bcount);
    590   1.2    bouyer 			xfer->c_skip += xfer->c_bcount;
    591   1.2    bouyer 			xfer->c_bcount = 0;
    592   1.2    bouyer 		} else {
    593   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    594   1.2    bouyer 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    595   1.2    bouyer 				bus_space_read_multi_4(chp->data32iot,
    596   1.2    bouyer 				    chp->data32ioh, 0,
    597  1.14   thorpej 				    (u_int32_t *)((char *)xfer->databuf +
    598  1.14   thorpej 				                  xfer->c_skip),
    599  1.13  augustss 				    len >> 2);
    600   1.2    bouyer 			    else
    601  1.12    bouyer 				bus_space_read_multi_stream_4(chp->data32iot,
    602  1.12    bouyer 				    chp->data32ioh, wd_data,
    603  1.14   thorpej 				    (u_int32_t *)((char *)xfer->databuf +
    604  1.14   thorpej 				                  xfer->c_skip),
    605  1.13  augustss 				    len >> 2);
    606   1.2    bouyer 
    607   1.2    bouyer 			    xfer->c_skip += len & 0xfffffffc;
    608   1.2    bouyer 			    xfer->c_bcount -= len & 0xfffffffc;
    609   1.2    bouyer 			    len = len & 0x03;
    610   1.2    bouyer 			}
    611   1.2    bouyer 			if (len > 0) {
    612   1.2    bouyer 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    613   1.2    bouyer 				bus_space_read_multi_2(chp->cmd_iot,
    614   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    615  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    616  1.14   thorpej 				                  xfer->c_skip),
    617  1.13  augustss 				    len >> 1);
    618   1.2    bouyer 			    else
    619   1.2    bouyer 				bus_space_read_multi_stream_2(chp->cmd_iot,
    620   1.2    bouyer 				    chp->cmd_ioh, wd_data,
    621  1.14   thorpej 				    (u_int16_t *)((char *)xfer->databuf +
    622  1.14   thorpej 				                  xfer->c_skip),
    623  1.13  augustss 				    len >> 1);
    624   1.2    bouyer 			    xfer->c_skip += len;
    625   1.2    bouyer 			    xfer->c_bcount -=len;
    626   1.2    bouyer 			}
    627   1.2    bouyer 		}
    628  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    629   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    630   1.2    bouyer 		}
    631   1.2    bouyer 		return 1;
    632   1.2    bouyer 
    633   1.2    bouyer 	case PHASE_ABORTED:
    634   1.2    bouyer 	case PHASE_COMPLETED:
    635   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    636   1.2    bouyer 		/* turn off DMA channel */
    637   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    638   1.2    bouyer 			dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
    639   1.2    bouyer 			    chp->channel, xfer->drive, dma_flags);
    640  1.20    bouyer 			if (xfer->c_flags & C_SENSE)
    641  1.20    bouyer 				xfer->c_bcount -=
    642  1.20    bouyer 				    sizeof(sc_xfer->sense.scsi_sense);
    643  1.20    bouyer 			else
    644  1.20    bouyer 				xfer->c_bcount -= sc_xfer->datalen;
    645   1.2    bouyer 		}
    646   1.8    bouyer 		if (xfer->c_flags & C_SENSE) {
    647   1.8    bouyer 			if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
    648   1.8    bouyer 				/*
    649   1.8    bouyer 				 * request sense failed ! it's not suppossed
    650   1.8    bouyer 				 * to be possible
    651   1.8    bouyer 				 */
    652  1.17    bouyer 				if (dma_err < 0)
    653  1.17    bouyer 					drvp->n_dmaerrs++;
    654  1.17    bouyer 				sc_xfer->error = XS_RESET;
    655  1.17    bouyer 				wdc_atapi_reset(chp, xfer);
    656  1.17    bouyer 				return (1);
    657  1.17    bouyer 			} else if (xfer->c_bcount <
    658  1.17    bouyer 			    sizeof(sc_xfer->sense.scsi_sense)) {
    659   1.8    bouyer 				/* use the sense we just read */
    660   1.8    bouyer 				sc_xfer->error = XS_SENSE;
    661  1.17    bouyer 			} else {
    662  1.17    bouyer 				/*
    663  1.17    bouyer 				 * command completed, but no data was read.
    664  1.17    bouyer 				 * use the short sense we saved previsouly.
    665  1.17    bouyer 				 */
    666  1.17    bouyer 				sc_xfer->error = XS_SHORTSENSE;
    667   1.8    bouyer 			}
    668   1.8    bouyer 		} else {
    669  1.23     perry 			sc_xfer->resid = xfer->c_bcount;
    670   1.8    bouyer 			if (chp->ch_status & WDCS_ERR) {
    671   1.8    bouyer 				/* save the short sense */
    672   1.8    bouyer 				sc_xfer->error = XS_SHORTSENSE;
    673   1.8    bouyer 				sc_xfer->sense.atapi_sense = chp->ch_error;
    674  1.12    bouyer 				if ((sc_xfer->sc_link->quirks &
    675  1.12    bouyer 				    ADEV_NOSENSE) == 0) {
    676  1.12    bouyer 					/*
    677  1.12    bouyer 					 * let the driver issue a
    678  1.12    bouyer 					 * 'request sense'
    679  1.12    bouyer 					 */
    680  1.12    bouyer 					xfer->databuf = &sc_xfer->sense;
    681  1.12    bouyer 					xfer->c_bcount =
    682  1.12    bouyer 					    sizeof(sc_xfer->sense.scsi_sense);
    683  1.23     perry 					xfer->c_skip = 0;
    684  1.12    bouyer 					xfer->c_flags |= C_SENSE;
    685  1.24    bouyer 					untimeout(wdctimeout, chp);
    686  1.12    bouyer 					wdc_atapi_start(chp, xfer);
    687  1.12    bouyer 					return 1;
    688  1.12    bouyer 				}
    689   1.8    bouyer 			} else if (dma_err < 0) {
    690  1.11    bouyer 				drvp->n_dmaerrs++;
    691  1.16    bouyer 				sc_xfer->error = XS_RESET;
    692  1.16    bouyer 				wdc_atapi_reset(chp, xfer);
    693  1.16    bouyer 				return (1);
    694   1.8    bouyer 			}
    695   1.2    bouyer 		}
    696   1.7    bouyer 		if (xfer->c_bcount != 0) {
    697   1.7    bouyer 			WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    698   1.7    bouyer 			    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    699   1.7    bouyer 		}
    700   1.2    bouyer #ifdef DIAGNOSTIC
    701   1.7    bouyer 		if (xfer->c_bcount < 0) {
    702   1.2    bouyer 			printf("wdc_atapi_intr warning: bcount value "
    703   1.2    bouyer 			    "is %d after io\n", xfer->c_bcount);
    704   1.2    bouyer 		}
    705   1.2    bouyer #endif
    706   1.2    bouyer 		break;
    707   1.2    bouyer 
    708   1.2    bouyer 	default:
    709   1.2    bouyer 		if (++retries<500) {
    710   1.2    bouyer 			DELAY(100);
    711   1.2    bouyer 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    712   1.2    bouyer 			    chp->cmd_ioh, wd_status);
    713   1.2    bouyer 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    714   1.2    bouyer 			    chp->cmd_ioh, wd_error);
    715   1.2    bouyer 			goto again;
    716   1.2    bouyer 		}
    717   1.2    bouyer 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    718   1.2    bouyer 		if (chp->ch_status & WDCS_ERR) {
    719   1.8    bouyer 			sc_xfer->error = XS_SHORTSENSE;
    720   1.2    bouyer 			sc_xfer->sense.atapi_sense = chp->ch_error;
    721   1.2    bouyer 		} else {
    722  1.16    bouyer 			sc_xfer->error = XS_RESET;
    723  1.16    bouyer 			wdc_atapi_reset(chp, xfer);
    724  1.16    bouyer 			return (1);
    725   1.2    bouyer 		}
    726   1.2    bouyer 	}
    727   1.2    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    728   1.2    bouyer 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    729   1.2    bouyer 	    DEBUG_INTR);
    730   1.2    bouyer 	wdc_atapi_done(chp, xfer);
    731   1.2    bouyer 	return (1);
    732   1.2    bouyer }
    733   1.2    bouyer 
    734   1.2    bouyer int
    735  1.20    bouyer wdc_atapi_ctrl(chp, xfer, irq)
    736   1.2    bouyer 	struct channel_softc *chp;
    737   1.2    bouyer 	struct wdc_xfer *xfer;
    738  1.20    bouyer 	int irq;
    739   1.2    bouyer {
    740   1.2    bouyer 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    741   1.2    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    742   1.2    bouyer 	char *errstring = NULL;
    743  1.20    bouyer 	int delay = (irq == 0) ? ATAPI_DELAY : 0;
    744   1.2    bouyer 
    745   1.2    bouyer 	/* Ack interrupt done in wait_for_unbusy */
    746   1.2    bouyer again:
    747   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
    748   1.5    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
    749   1.4    bouyer 	    DEBUG_INTR | DEBUG_FUNCS);
    750   1.2    bouyer 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
    751   1.2    bouyer 	    WDSD_IBM | (xfer->drive << 4));
    752   1.2    bouyer 	switch (drvp->state) {
    753   1.2    bouyer 	case PIOMODE:
    754  1.22    bouyer piomode:
    755   1.2    bouyer 		/* Don't try to set mode if controller can't be adjusted */
    756   1.2    bouyer 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
    757   1.2    bouyer 			goto ready;
    758   1.6    bouyer 		/* Also don't try if the drive didn't report its mode */
    759   1.6    bouyer 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    760   1.6    bouyer 			goto ready;;
    761   1.2    bouyer 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    762   1.2    bouyer 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    763   1.2    bouyer 		drvp->state = PIOMODE_WAIT;
    764   1.2    bouyer 		break;
    765   1.2    bouyer 	case PIOMODE_WAIT:
    766   1.2    bouyer 		errstring = "piomode";
    767  1.19    bouyer 		if (wait_for_unbusy(chp, delay))
    768   1.2    bouyer 			goto timeout;
    769  1.22    bouyer 		if (chp->ch_status & WDCS_ERR) {
    770  1.22    bouyer 			if (drvp->PIO_mode < 3) {
    771  1.22    bouyer 				drvp->PIO_mode = 3;
    772  1.22    bouyer 				goto piomode;
    773  1.22    bouyer 			} else {
    774  1.22    bouyer 				goto error;
    775  1.22    bouyer 			}
    776  1.22    bouyer 		}
    777   1.2    bouyer 	/* fall through */
    778   1.2    bouyer 
    779   1.2    bouyer 	case DMAMODE:
    780   1.2    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    781   1.2    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    782   1.2    bouyer 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    783   1.2    bouyer 		} else if (drvp->drive_flags & DRIVE_DMA) {
    784   1.2    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    785   1.2    bouyer 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    786   1.2    bouyer 		} else {
    787   1.2    bouyer 			goto ready;
    788   1.2    bouyer 		}
    789   1.2    bouyer 		drvp->state = DMAMODE_WAIT;
    790   1.2    bouyer 		break;
    791   1.2    bouyer 	case DMAMODE_WAIT:
    792   1.2    bouyer 		errstring = "dmamode";
    793  1.19    bouyer 		if (wait_for_unbusy(chp, delay))
    794   1.2    bouyer 			goto timeout;
    795   1.2    bouyer 		if (chp->ch_status & WDCS_ERR)
    796   1.2    bouyer 			goto error;
    797   1.2    bouyer 	/* fall through */
    798   1.2    bouyer 
    799   1.2    bouyer 	case READY:
    800   1.2    bouyer 	ready:
    801   1.2    bouyer 		drvp->state = READY;
    802   1.2    bouyer 		xfer->c_intr = wdc_atapi_intr;
    803  1.24    bouyer 		untimeout(wdctimeout, chp);
    804   1.2    bouyer 		wdc_atapi_start(chp, xfer);
    805   1.2    bouyer 		return 1;
    806   1.2    bouyer 	}
    807  1.27   thorpej 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    808   1.2    bouyer 		chp->ch_flags |= WDCF_IRQ_WAIT;
    809   1.2    bouyer 		xfer->c_intr = wdc_atapi_ctrl;
    810   1.2    bouyer 	} else {
    811   1.2    bouyer 		goto again;
    812   1.2    bouyer 	}
    813   1.2    bouyer 	return 1;
    814   1.2    bouyer 
    815   1.2    bouyer timeout:
    816  1.20    bouyer 	if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
    817   1.2    bouyer 		return 0; /* IRQ was not for us */
    818   1.2    bouyer 	}
    819   1.2    bouyer 	printf("%s:%d:%d: %s timed out\n",
    820   1.2    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
    821   1.3    bouyer 	sc_xfer->error = XS_TIMEOUT;
    822   1.2    bouyer 	wdc_atapi_reset(chp, xfer);
    823   1.2    bouyer 	return 1;
    824   1.2    bouyer error:
    825   1.2    bouyer 	printf("%s:%d:%d: %s ",
    826   1.2    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    827   1.2    bouyer 	    errstring);
    828   1.3    bouyer 	printf("error (0x%x)\n", chp->ch_error);
    829   1.8    bouyer 	sc_xfer->error = XS_SHORTSENSE;
    830   1.2    bouyer 	sc_xfer->sense.atapi_sense = chp->ch_error;
    831   1.3    bouyer 	wdc_atapi_reset(chp, xfer);
    832   1.2    bouyer 	return 1;
    833   1.2    bouyer }
    834   1.2    bouyer 
    835   1.2    bouyer void
    836   1.2    bouyer wdc_atapi_done(chp, xfer)
    837   1.2    bouyer 	struct channel_softc *chp;
    838   1.2    bouyer 	struct wdc_xfer *xfer;
    839   1.2    bouyer {
    840   1.2    bouyer 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    841  1.11    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    842   1.2    bouyer 
    843   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
    844   1.5    bouyer 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
    845   1.5    bouyer 	    (u_int)xfer->c_flags), DEBUG_XFERS);
    846  1.24    bouyer 	untimeout(wdctimeout, chp);
    847   1.2    bouyer 	/* remove this command from xfer queue */
    848   1.2    bouyer 	wdc_free_xfer(chp, xfer);
    849  1.27   thorpej 	sc_xfer->xs_status |= XS_STS_DONE;
    850  1.19    bouyer 	if (drvp->n_dmaerrs ||
    851  1.19    bouyer 	  (sc_xfer->error != XS_NOERROR && sc_xfer->error != XS_SENSE &&
    852  1.19    bouyer 	  sc_xfer->error != XS_SHORTSENSE)) {
    853  1.11    bouyer 		drvp->n_dmaerrs = 0;
    854  1.11    bouyer 		wdc_downgrade_mode(drvp);
    855  1.11    bouyer 	}
    856  1.11    bouyer 
    857  1.25    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    858  1.25    bouyer 	scsipi_done(sc_xfer);
    859   1.2    bouyer 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
    860   1.4    bouyer 	    chp->ch_flags), DEBUG_XFERS);
    861  1.10  drochner 	wdcstart(chp);
    862   1.2    bouyer }
    863   1.2    bouyer 
    864   1.2    bouyer void
    865   1.2    bouyer wdc_atapi_reset(chp, xfer)
    866   1.2    bouyer 	struct channel_softc *chp;
    867   1.2    bouyer 	struct wdc_xfer *xfer;
    868   1.2    bouyer {
    869   1.2    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
    870   1.2    bouyer 	struct scsipi_xfer *sc_xfer = xfer->cmd;
    871   1.2    bouyer 
    872   1.2    bouyer 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
    873   1.2    bouyer 	drvp->state = 0;
    874   1.2    bouyer 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
    875   1.2    bouyer 		printf("%s:%d:%d: reset failed\n",
    876   1.2    bouyer 		    chp->wdc->sc_dev.dv_xname, chp->channel,
    877   1.2    bouyer 		    xfer->drive);
    878   1.2    bouyer 		sc_xfer->error = XS_SELTIMEOUT;
    879   1.2    bouyer 	}
    880   1.2    bouyer 	wdc_atapi_done(chp, xfer);
    881   1.2    bouyer 	return;
    882   1.2    bouyer }
    883