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