Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.68
      1  1.68   thorpej /*	$NetBSD: atapi_wdc.c,v 1.68 2004/01/03 23:59:58 thorpej Exp $	*/
      2   1.2    bouyer 
      3   1.2    bouyer /*
      4  1.47    bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.
      5   1.2    bouyer  *
      6   1.2    bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2    bouyer  * modification, are permitted provided that the following conditions
      8   1.2    bouyer  * are met:
      9   1.2    bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2    bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2    bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2    bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2    bouyer  *    must display the following acknowledgement:
     16  1.51    bouyer  *	This product includes software developed by Manuel Bouyer.
     17  1.58    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18  1.58    bouyer  *    derived from this software without specific prior written permission.
     19   1.2    bouyer  *
     20  1.35    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.35    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.35    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.35    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.35    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.35    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.35    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.35    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.35    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.35    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2    bouyer  */
     31  1.44     lukem 
     32  1.44     lukem #include <sys/cdefs.h>
     33  1.68   thorpej __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.68 2004/01/03 23:59:58 thorpej Exp $");
     34   1.2    bouyer 
     35  1.18   hubertf #ifndef WDCDEBUG
     36   1.2    bouyer #define WDCDEBUG
     37  1.18   hubertf #endif /* WDCDEBUG */
     38   1.2    bouyer 
     39   1.2    bouyer #include <sys/param.h>
     40   1.2    bouyer #include <sys/systm.h>
     41   1.2    bouyer #include <sys/kernel.h>
     42   1.2    bouyer #include <sys/file.h>
     43   1.2    bouyer #include <sys/stat.h>
     44   1.2    bouyer #include <sys/buf.h>
     45   1.2    bouyer #include <sys/malloc.h>
     46   1.2    bouyer #include <sys/device.h>
     47   1.2    bouyer #include <sys/syslog.h>
     48   1.2    bouyer #include <sys/proc.h>
     49  1.39   mycroft #include <sys/dvdio.h>
     50   1.2    bouyer 
     51   1.2    bouyer #include <machine/intr.h>
     52   1.2    bouyer #include <machine/bus.h>
     53   1.2    bouyer 
     54   1.2    bouyer #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     55  1.65   thorpej #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     56  1.65   thorpej #define	bus_space_write_multi_stream_4	bus_space_write_multi_4
     57  1.65   thorpej #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     58  1.65   thorpej #define	bus_space_read_multi_stream_4	bus_space_read_multi_4
     59   1.2    bouyer #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     60   1.2    bouyer 
     61   1.2    bouyer #include <dev/ata/atareg.h>
     62   1.2    bouyer #include <dev/ata/atavar.h>
     63   1.2    bouyer #include <dev/ic/wdcreg.h>
     64   1.2    bouyer #include <dev/ic/wdcvar.h>
     65   1.2    bouyer 
     66  1.40    bouyer #include <dev/scsipi/scsi_all.h> /* for SCSI status */
     67  1.40    bouyer 
     68   1.2    bouyer #define DEBUG_INTR   0x01
     69   1.2    bouyer #define DEBUG_XFERS  0x02
     70   1.2    bouyer #define DEBUG_STATUS 0x04
     71   1.2    bouyer #define DEBUG_FUNCS  0x08
     72   1.2    bouyer #define DEBUG_PROBE  0x10
     73   1.2    bouyer #ifdef WDCDEBUG
     74   1.3    bouyer int wdcdebug_atapi_mask = 0;
     75   1.2    bouyer #define WDCDEBUG_PRINT(args, level) \
     76   1.2    bouyer 	if (wdcdebug_atapi_mask & (level)) \
     77   1.2    bouyer 		printf args
     78   1.2    bouyer #else
     79   1.2    bouyer #define WDCDEBUG_PRINT(args, level)
     80   1.2    bouyer #endif
     81   1.2    bouyer 
     82   1.2    bouyer #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     83  1.59    bouyer #define ATAPI_MODE_DELAY 1000	/* 1s, timeout for SET_FEATYRE cmds */
     84  1.65   thorpej 
     85  1.65   thorpej static int	wdc_atapi_get_params(struct scsipi_channel *, int,
     86  1.65   thorpej 				     struct ataparams *);
     87  1.65   thorpej static void	wdc_atapi_probe_device(struct atapibus_softc *, int);
     88  1.65   thorpej static void	wdc_atapi_minphys (struct buf *bp);
     89  1.66   thorpej static void	wdc_atapi_start(struct wdc_channel *,struct ata_xfer *);
     90  1.66   thorpej static int	wdc_atapi_intr(struct wdc_channel *, struct ata_xfer *, int);
     91  1.66   thorpej static void	wdc_atapi_kill_xfer(struct wdc_channel *, struct ata_xfer *);
     92  1.65   thorpej static void	wdc_atapi_phase_complete(struct ata_xfer *);
     93  1.66   thorpej static void	wdc_atapi_done(struct wdc_channel *, struct ata_xfer *);
     94  1.66   thorpej static void	wdc_atapi_reset(struct wdc_channel *, struct ata_xfer *);
     95  1.65   thorpej static void	wdc_atapi_scsipi_request(struct scsipi_channel *,
     96  1.65   thorpej 					 scsipi_adapter_req_t, void *);
     97  1.65   thorpej static void	wdc_atapi_kill_pending(struct scsipi_periph *);
     98  1.65   thorpej static void	wdc_atapi_polldsc(void *arg);
     99   1.2    bouyer 
    100   1.2    bouyer #define MAX_SIZE MAXPHYS
    101   1.2    bouyer 
    102  1.65   thorpej static const struct scsipi_bustype wdc_atapi_bustype = {
    103  1.40    bouyer 	SCSIPI_BUSTYPE_ATAPI,
    104  1.40    bouyer 	atapi_scsipi_cmd,
    105  1.40    bouyer 	atapi_interpret_sense,
    106  1.40    bouyer 	atapi_print_addr,
    107  1.40    bouyer 	wdc_atapi_kill_pending,
    108  1.40    bouyer };
    109  1.40    bouyer 
    110   1.2    bouyer void
    111  1.65   thorpej wdc_atapibus_attach(struct atabus_softc *ata_sc)
    112   1.2    bouyer {
    113  1.66   thorpej 	struct wdc_channel *chp = ata_sc->sc_chan;
    114  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    115  1.40    bouyer 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    116  1.40    bouyer 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
    117   1.2    bouyer 
    118   1.9   thorpej 	/*
    119  1.40    bouyer 	 * Fill in the scsipi_adapter.
    120   1.9   thorpej 	 */
    121  1.40    bouyer 	adapt->adapt_dev = &wdc->sc_dev;
    122  1.40    bouyer 	adapt->adapt_nchannels = wdc->nchannels;
    123  1.40    bouyer 	adapt->adapt_request = wdc_atapi_scsipi_request;
    124  1.40    bouyer 	adapt->adapt_minphys = wdc_atapi_minphys;
    125  1.42     bjh21 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
    126  1.42     bjh21 		adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
    127  1.40    bouyer 	wdc->sc_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device;
    128  1.40    bouyer 
    129  1.40    bouyer 	/*
    130  1.40    bouyer 	 * Fill in the scsipi_channel.
    131  1.40    bouyer 	 */
    132  1.40    bouyer 	memset(chan, 0, sizeof(*chan));
    133  1.40    bouyer 	chan->chan_adapter = adapt;
    134  1.40    bouyer 	chan->chan_bustype = &wdc_atapi_bustype;
    135  1.67   thorpej 	chan->chan_channel = chp->ch_channel;
    136  1.40    bouyer 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
    137  1.40    bouyer 	chan->chan_openings = 1;
    138  1.40    bouyer 	chan->chan_max_periph = 1;
    139  1.40    bouyer 	chan->chan_ntargets = 2;
    140  1.40    bouyer 	chan->chan_nluns = 1;
    141  1.40    bouyer 
    142  1.59    bouyer 	chp->atapibus = config_found(&ata_sc->sc_dev, chan, atapiprint);
    143   1.2    bouyer }
    144   1.2    bouyer 
    145  1.65   thorpej static void
    146  1.65   thorpej wdc_atapi_minphys(struct buf *bp)
    147   1.2    bouyer {
    148  1.40    bouyer 
    149  1.40    bouyer 	if (bp->b_bcount > MAX_SIZE)
    150   1.2    bouyer 		bp->b_bcount = MAX_SIZE;
    151   1.2    bouyer 	minphys(bp);
    152   1.2    bouyer }
    153   1.2    bouyer 
    154  1.28     enami /*
    155  1.40    bouyer  * Kill off all pending xfers for a periph.
    156  1.28     enami  *
    157  1.28     enami  * Must be called at splbio().
    158  1.28     enami  */
    159  1.65   thorpej static void
    160  1.65   thorpej wdc_atapi_kill_pending(struct scsipi_periph *periph)
    161  1.28     enami {
    162  1.40    bouyer 	struct wdc_softc *wdc =
    163  1.40    bouyer 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    164  1.66   thorpej 	struct wdc_channel *chp =
    165  1.40    bouyer 	    wdc->channels[periph->periph_channel->chan_channel];
    166  1.28     enami 
    167  1.28     enami 	wdc_kill_pending(chp);
    168  1.28     enami }
    169  1.28     enami 
    170  1.65   thorpej static void
    171  1.66   thorpej wdc_atapi_kill_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
    172  1.28     enami {
    173  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    174  1.28     enami 
    175  1.32   thorpej 	callout_stop(&chp->ch_callout);
    176  1.28     enami 	/* remove this command from xfer queue */
    177  1.28     enami 	wdc_free_xfer(chp, xfer);
    178  1.28     enami 	sc_xfer->error = XS_DRIVER_STUFFUP;
    179  1.28     enami 	scsipi_done(sc_xfer);
    180  1.28     enami }
    181  1.28     enami 
    182  1.65   thorpej static int
    183  1.65   thorpej wdc_atapi_get_params(struct scsipi_channel *chan, int drive,
    184  1.65   thorpej     struct ataparams *id)
    185   1.2    bouyer {
    186  1.40    bouyer 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    187  1.66   thorpej 	struct wdc_channel *chp = wdc->channels[chan->chan_channel];
    188   1.2    bouyer 	struct wdc_command wdc_c;
    189   1.2    bouyer 
    190   1.2    bouyer 	/* if no ATAPI device detected at wdc attach time, skip */
    191   1.2    bouyer 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    192   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    193   1.2    bouyer 		    drive), DEBUG_PROBE);
    194   1.2    bouyer 		return -1;
    195   1.2    bouyer 	}
    196  1.59    bouyer 
    197  1.59    bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    198  1.59    bouyer 	wdc_c.r_command = ATAPI_SOFT_RESET;
    199  1.59    bouyer 	wdc_c.r_st_bmask = 0;
    200  1.59    bouyer 	wdc_c.r_st_pmask = 0;
    201  1.59    bouyer 	wdc_c.flags = AT_POLL;
    202  1.59    bouyer 	wdc_c.timeout = WDC_RESET_WAIT;
    203  1.59    bouyer 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
    204  1.59    bouyer 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    205  1.59    bouyer 		    " drive %s:%d:%d: driver failed\n",
    206  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive);
    207  1.59    bouyer 		panic("wdc_atapi_get_params");
    208  1.59    bouyer 	}
    209  1.59    bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    210   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    211  1.59    bouyer 		    "failed for drive %s:%d:%d: error 0x%x\n",
    212  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive,
    213  1.59    bouyer 		    wdc_c.r_error), DEBUG_PROBE);
    214   1.2    bouyer 		return -1;
    215   1.2    bouyer 	}
    216  1.59    bouyer 	chp->ch_drive[drive].state = 0;
    217   1.2    bouyer 
    218  1.62      fvdl 	bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_status], 0);
    219  1.59    bouyer 
    220   1.2    bouyer 	/* Some ATAPI devices need a bit more time after software reset. */
    221   1.2    bouyer 	delay(5000);
    222  1.57   mycroft 	if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) {
    223   1.2    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    224   1.2    bouyer 		    "failed for drive %s:%d:%d: error 0x%x\n",
    225  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive,
    226   1.2    bouyer 		    wdc_c.r_error), DEBUG_PROBE);
    227   1.2    bouyer 		return -1;
    228   1.2    bouyer 	}
    229  1.40    bouyer 	return 0;
    230   1.2    bouyer }
    231   1.2    bouyer 
    232  1.65   thorpej static void
    233  1.65   thorpej wdc_atapi_probe_device(struct atapibus_softc *sc, int target)
    234  1.33    bouyer {
    235  1.40    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    236  1.40    bouyer 	struct scsipi_periph *periph;
    237  1.33    bouyer 	struct ataparams ids;
    238  1.33    bouyer 	struct ataparams *id = &ids;
    239  1.46    bouyer 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    240  1.66   thorpej 	struct wdc_channel *chp = wdc->channels[chan->chan_channel];
    241  1.46    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
    242  1.40    bouyer 	struct scsipibus_attach_args sa;
    243  1.33    bouyer 	char serial_number[21], model[41], firmware_revision[9];
    244  1.33    bouyer 
    245  1.40    bouyer 	/* skip if already attached */
    246  1.40    bouyer 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    247  1.33    bouyer 		return;
    248  1.40    bouyer 
    249  1.57   mycroft 	if (wdc_atapi_get_params(chan, target, id) == 0) {
    250  1.33    bouyer #ifdef ATAPI_DEBUG_PROBE
    251  1.33    bouyer 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    252  1.40    bouyer 		    sc->sc_dev.dv_xname, target,
    253  1.33    bouyer 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    254  1.33    bouyer 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    255  1.33    bouyer #endif
    256  1.40    bouyer 		periph = scsipi_alloc_periph(M_NOWAIT);
    257  1.40    bouyer 		if (periph == NULL) {
    258  1.40    bouyer 			printf("%s: unable to allocate periph for drive %d\n",
    259  1.40    bouyer 			    sc->sc_dev.dv_xname, target);
    260  1.40    bouyer 			return;
    261  1.33    bouyer 		}
    262  1.40    bouyer 		periph->periph_dev = NULL;
    263  1.40    bouyer 		periph->periph_channel = chan;
    264  1.40    bouyer 		periph->periph_switch = &atapi_probe_periphsw;
    265  1.40    bouyer 		periph->periph_target = target;
    266  1.40    bouyer 		periph->periph_lun = 0;
    267  1.56   mycroft 		periph->periph_quirks = PQUIRK_ONLYBIG;
    268  1.40    bouyer 
    269  1.40    bouyer #ifdef SCSIPI_DEBUG
    270  1.40    bouyer 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
    271  1.40    bouyer 		    SCSIPI_DEBUG_TARGET == target)
    272  1.40    bouyer 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    273  1.40    bouyer #endif
    274  1.40    bouyer 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
    275  1.40    bouyer 		if (id->atap_config & ATAPI_CFG_REMOV)
    276  1.40    bouyer 			periph->periph_flags |= PERIPH_REMOVABLE;
    277  1.45    bouyer 		if (periph->periph_type == T_SEQUENTIAL)
    278  1.45    bouyer 			drvp->drive_flags |= DRIVE_ATAPIST;
    279  1.40    bouyer 
    280  1.40    bouyer 		sa.sa_periph = periph;
    281  1.40    bouyer 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    282  1.40    bouyer 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
    283  1.40    bouyer 		    T_REMOV : T_FIXED;
    284  1.33    bouyer 		scsipi_strvis(model, 40, id->atap_model, 40);
    285  1.40    bouyer 		scsipi_strvis(serial_number, 20, id->atap_serial, 20);
    286  1.33    bouyer 		scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
    287  1.33    bouyer 		sa.sa_inqbuf.vendor = model;
    288  1.33    bouyer 		sa.sa_inqbuf.product = serial_number;
    289  1.33    bouyer 		sa.sa_inqbuf.revision = firmware_revision;
    290  1.40    bouyer 
    291  1.40    bouyer 		/*
    292  1.40    bouyer 		 * Determine the operating mode capabilities of the device.
    293  1.40    bouyer 		 */
    294  1.40    bouyer 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    295  1.40    bouyer 			periph->periph_cap |= PERIPH_CAP_CMD16;
    296  1.40    bouyer 		/* XXX This is gross. */
    297  1.40    bouyer 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
    298  1.40    bouyer 
    299  1.40    bouyer 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
    300  1.40    bouyer 
    301  1.40    bouyer 		if (drvp->drv_softc)
    302  1.33    bouyer 			wdc_probe_caps(drvp);
    303  1.60    bouyer 		else
    304  1.60    bouyer 			drvp->drive_flags &= ~DRIVE_ATAPI;
    305  1.33    bouyer 	}
    306  1.33    bouyer }
    307  1.33    bouyer 
    308  1.65   thorpej static void
    309  1.65   thorpej wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    310  1.65   thorpej     void *arg)
    311  1.40    bouyer {
    312  1.40    bouyer 	struct scsipi_adapter *adapt = chan->chan_adapter;
    313  1.40    bouyer 	struct scsipi_periph *periph;
    314   1.2    bouyer 	struct scsipi_xfer *sc_xfer;
    315  1.40    bouyer 	struct wdc_softc *wdc = (void *)adapt->adapt_dev;
    316  1.63   thorpej 	struct ata_xfer *xfer;
    317  1.40    bouyer 	int channel = chan->chan_channel;
    318  1.40    bouyer 	int drive, s;
    319  1.40    bouyer 
    320  1.40    bouyer 	switch (req) {
    321  1.40    bouyer 	case ADAPTER_REQ_RUN_XFER:
    322  1.40    bouyer 		sc_xfer = arg;
    323  1.40    bouyer 		periph = sc_xfer->xs_periph;
    324  1.40    bouyer 		drive = periph->periph_target;
    325  1.40    bouyer 
    326  1.40    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
    327  1.40    bouyer 		    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
    328  1.40    bouyer 		if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    329  1.40    bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
    330  1.40    bouyer 			scsipi_done(sc_xfer);
    331  1.40    bouyer 			return;
    332  1.40    bouyer 		}
    333  1.40    bouyer 
    334  1.40    bouyer 		xfer = wdc_get_xfer(WDC_NOSLEEP);
    335  1.40    bouyer 		if (xfer == NULL) {
    336  1.40    bouyer 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
    337  1.40    bouyer 			scsipi_done(sc_xfer);
    338  1.40    bouyer 			return;
    339  1.40    bouyer 		}
    340  1.40    bouyer 
    341  1.40    bouyer 		if (sc_xfer->xs_control & XS_CTL_POLL)
    342  1.40    bouyer 			xfer->c_flags |= C_POLL;
    343  1.59    bouyer 		if ((wdc->channels[channel]->ch_drive[drive].drive_flags &
    344  1.59    bouyer 		    (DRIVE_DMA | DRIVE_UDMA)) && sc_xfer->datalen > 0)
    345  1.59    bouyer 			xfer->c_flags |= C_DMA;
    346  1.63   thorpej 		xfer->c_drive = drive;
    347  1.40    bouyer 		xfer->c_flags |= C_ATAPI;
    348  1.40    bouyer 		if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
    349  1.40    bouyer 		    sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
    350  1.40    bouyer 		    sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
    351  1.40    bouyer 			/*
    352  1.40    bouyer 			 * DVD authentication commands must always be done in
    353  1.40    bouyer 			 * PIO mode.
    354  1.40    bouyer 			 */
    355  1.59    bouyer 			xfer->c_flags &= ~C_DMA;
    356  1.40    bouyer 		}
    357  1.52    bouyer 		/*
    358  1.52    bouyer 		 * DMA can't deal with transfers which are not a multiple of
    359  1.52    bouyer 		 * 2 bytes. It's a bug to request such transfers for ATAPI
    360  1.52    bouyer 		 * but as the request can come from userland, we have to
    361  1.52    bouyer 		 * protect against it.
    362  1.53    bouyer 		 * Also some devices seems to not handle DMA xfers of less than
    363  1.53    bouyer 		 * 4 bytes.
    364  1.52    bouyer 		 */
    365  1.53    bouyer 		if (sc_xfer->datalen < 4 || (sc_xfer->datalen & 0x01))
    366  1.59    bouyer 			xfer->c_flags &= ~C_DMA;
    367  1.52    bouyer 
    368  1.63   thorpej 		xfer->c_cmd = sc_xfer;
    369  1.63   thorpej 		xfer->c_databuf = sc_xfer->data;
    370  1.40    bouyer 		xfer->c_bcount = sc_xfer->datalen;
    371  1.40    bouyer 		xfer->c_start = wdc_atapi_start;
    372  1.40    bouyer 		xfer->c_intr = wdc_atapi_intr;
    373  1.40    bouyer 		xfer->c_kill_xfer = wdc_atapi_kill_xfer;
    374  1.45    bouyer 		xfer->c_dscpoll = 0;
    375  1.40    bouyer 		s = splbio();
    376  1.40    bouyer 		wdc_exec_xfer(wdc->channels[channel], xfer);
    377   1.2    bouyer #ifdef DIAGNOSTIC
    378  1.40    bouyer 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    379  1.40    bouyer 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    380  1.40    bouyer 			panic("wdc_atapi_scsipi_request: polled command "
    381  1.40    bouyer 			    "not done");
    382   1.2    bouyer #endif
    383  1.40    bouyer 		splx(s);
    384  1.40    bouyer 		return;
    385  1.40    bouyer 
    386  1.40    bouyer 	default:
    387  1.40    bouyer 		/* Not supported, nothing to do. */
    388  1.41     lukem 		;
    389  1.40    bouyer 	}
    390   1.2    bouyer }
    391   1.2    bouyer 
    392  1.65   thorpej static void
    393  1.66   thorpej wdc_atapi_start(struct wdc_channel *chp, struct ata_xfer *xfer)
    394   1.2    bouyer {
    395  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    396  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    397  1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    398  1.59    bouyer 	int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0;
    399  1.59    bouyer 	char *errstring;
    400   1.2    bouyer 
    401   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    402  1.67   thorpej 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive,
    403  1.27   thorpej 	    sc_xfer->xs_control), DEBUG_XFERS);
    404  1.59    bouyer 	if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER))
    405  1.59    bouyer 		drvp->n_xfers++;
    406  1.59    bouyer 	/* Do control operations specially. */
    407  1.59    bouyer 	if (__predict_false(drvp->state < READY)) {
    408  1.59    bouyer 		/* If it's not a polled command, we need the kenrel thread */
    409  1.59    bouyer 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0 &&
    410  1.59    bouyer 		    (chp->ch_flags & WDCF_TH_RUN) == 0) {
    411  1.61    bouyer 			chp->ch_queue->queue_freeze++;
    412  1.68   thorpej 			wakeup(&chp->ch_thread);
    413  1.59    bouyer 			return;
    414  1.59    bouyer 		}
    415  1.59    bouyer 		/*
    416  1.59    bouyer 		 * disable interrupts, all commands here should be quick
    417  1.59    bouyer 		 * enouth to be able to poll, and we don't go here that often
    418  1.59    bouyer 		 */
    419  1.59    bouyer 		 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
    420  1.59    bouyer 		     WDCTL_4BIT | WDCTL_IDS);
    421  1.67   thorpej 		if (wdc->cap & WDC_CAPABILITY_SELECT)
    422  1.67   thorpej 			wdc->select(chp, xfer->c_drive);
    423  1.62      fvdl 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    424  1.63   thorpej 		    WDSD_IBM | (xfer->c_drive << 4));
    425  1.59    bouyer 		/* Don't try to set mode if controller can't be adjusted */
    426  1.67   thorpej 		if ((wdc->cap & WDC_CAPABILITY_MODE) == 0)
    427  1.59    bouyer 			goto ready;
    428  1.59    bouyer 		/* Also don't try if the drive didn't report its mode */
    429  1.59    bouyer 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    430  1.59    bouyer 			goto ready;
    431  1.59    bouyer 		errstring = "unbusy";
    432  1.64   thorpej 		if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags))
    433  1.59    bouyer 			goto timeout;
    434  1.59    bouyer 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    435  1.59    bouyer 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    436  1.59    bouyer 		errstring = "piomode";
    437  1.64   thorpej 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
    438  1.59    bouyer 			goto timeout;
    439  1.59    bouyer 		if (chp->ch_status & WDCS_ERR) {
    440  1.59    bouyer 			if (chp->ch_error == WDCE_ABRT) {
    441  1.59    bouyer 				/*
    442  1.59    bouyer 				 * some ATAPI drives rejects pio settings.
    443  1.59    bouyer 				 * all we can do here is fall back to PIO 0
    444  1.59    bouyer 				 */
    445  1.59    bouyer 				drvp->drive_flags &= ~DRIVE_MODE;
    446  1.59    bouyer 				drvp->drive_flags &= ~(DRIVE_DMA|DRIVE_UDMA);
    447  1.59    bouyer 				drvp->PIO_mode = 0;
    448  1.59    bouyer 				drvp->DMA_mode = 0;
    449  1.59    bouyer 				printf("%s:%d:%d: pio setting rejected, "
    450  1.59    bouyer 				    "falling back to PIO mode 0\n",
    451  1.67   thorpej 				    wdc->sc_dev.dv_xname,
    452  1.67   thorpej 				    chp->ch_channel, xfer->c_drive);
    453  1.67   thorpej 				wdc->set_modes(chp);
    454  1.59    bouyer 				goto ready;
    455  1.59    bouyer 			}
    456  1.59    bouyer 			goto error;
    457  1.59    bouyer 		}
    458  1.59    bouyer 		if (drvp->drive_flags & DRIVE_UDMA) {
    459  1.59    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    460  1.59    bouyer 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    461  1.59    bouyer 		} else if (drvp->drive_flags & DRIVE_DMA) {
    462  1.59    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    463  1.59    bouyer 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    464  1.59    bouyer 		} else {
    465  1.59    bouyer 			goto ready;
    466  1.59    bouyer 		}
    467  1.59    bouyer 		errstring = "dmamode";
    468  1.64   thorpej 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
    469  1.59    bouyer 			goto timeout;
    470  1.59    bouyer 		if (chp->ch_status & WDCS_ERR)
    471  1.59    bouyer 			goto error;
    472  1.59    bouyer ready:
    473  1.59    bouyer 		drvp->state = READY;
    474  1.59    bouyer 		bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
    475  1.59    bouyer 		    WDCTL_4BIT);
    476  1.30    bouyer 	}
    477  1.24    bouyer 	/* start timeout machinery */
    478  1.27   thorpej 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    479  1.50    bouyer 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
    480  1.32   thorpej 		    wdctimeout, chp);
    481  1.59    bouyer 
    482  1.67   thorpej 	if (wdc->cap & WDC_CAPABILITY_SELECT)
    483  1.67   thorpej 		wdc->select(chp, xfer->c_drive);
    484  1.62      fvdl 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    485  1.63   thorpej 	    WDSD_IBM | (xfer->c_drive << 4));
    486  1.64   thorpej 	switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags)  < 0) {
    487  1.59    bouyer 	case WDCWAIT_OK:
    488  1.59    bouyer 		break;
    489  1.59    bouyer 	case WDCWAIT_TOUT:
    490   1.2    bouyer 		printf("wdc_atapi_start: not ready, st = %02x\n",
    491   1.2    bouyer 		    chp->ch_status);
    492   1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    493   1.2    bouyer 		wdc_atapi_reset(chp, xfer);
    494   1.2    bouyer 		return;
    495  1.59    bouyer 	case WDCWAIT_THR:
    496  1.59    bouyer 		return;
    497   1.2    bouyer 	}
    498   1.2    bouyer 
    499   1.2    bouyer 	/*
    500   1.2    bouyer 	 * Even with WDCS_ERR, the device should accept a command packet
    501   1.2    bouyer 	 * Limit length to what can be stuffed into the cylinder register
    502   1.2    bouyer 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    503   1.2    bouyer 	 * but not all devices do that and it's not obvious from the
    504   1.2    bouyer 	 * ATAPI spec that that behaviour should be expected.  If more
    505   1.2    bouyer 	 * data is necessary, multiple data transfer phases will be done.
    506   1.2    bouyer 	 */
    507   1.5    bouyer 
    508  1.63   thorpej 	wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD,
    509  1.29    bouyer 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    510   1.2    bouyer 	    0, 0, 0,
    511   1.2    bouyer 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    512   1.2    bouyer 
    513   1.2    bouyer 	/*
    514   1.2    bouyer 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    515   1.2    bouyer 	 * the interrupt routine. If it is a polled command, call the interrupt
    516   1.2    bouyer 	 * routine until command is done.
    517   1.2    bouyer 	 */
    518  1.40    bouyer 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
    519  1.27   thorpej 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    520   1.2    bouyer 		/* Wait for at last 400ns for status bit to be valid */
    521  1.20    bouyer 		DELAY(1);
    522  1.40    bouyer 		if (chp->ch_flags & WDCF_DMA_WAIT) {
    523  1.40    bouyer 			wdc_dmawait(chp, xfer, sc_xfer->timeout);
    524  1.40    bouyer 			chp->ch_flags &= ~WDCF_DMA_WAIT;
    525  1.40    bouyer 		}
    526  1.20    bouyer 		wdc_atapi_intr(chp, xfer, 0);
    527  1.21    bouyer 	} else {
    528  1.21    bouyer 		chp->ch_flags |= WDCF_IRQ_WAIT;
    529   1.2    bouyer 	}
    530  1.27   thorpej 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    531  1.27   thorpej 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    532   1.2    bouyer 			/* Wait for at last 400ns for status bit to be valid */
    533  1.20    bouyer 			DELAY(1);
    534  1.20    bouyer 			wdc_atapi_intr(chp, xfer, 0);
    535   1.2    bouyer 		}
    536   1.2    bouyer 	}
    537  1.59    bouyer 	return;
    538  1.59    bouyer timeout:
    539  1.59    bouyer 	printf("%s:%d:%d: %s timed out\n",
    540  1.67   thorpej 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    541  1.67   thorpej 	    errstring);
    542  1.59    bouyer 	sc_xfer->error = XS_TIMEOUT;
    543  1.59    bouyer 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    544  1.59    bouyer 	wdc_atapi_reset(chp, xfer);
    545  1.59    bouyer 	return;
    546  1.59    bouyer error:
    547  1.59    bouyer 	printf("%s:%d:%d: %s ",
    548  1.67   thorpej 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    549  1.59    bouyer 	    errstring);
    550  1.59    bouyer 	printf("error (0x%x)\n", chp->ch_error);
    551  1.59    bouyer 	sc_xfer->error = XS_SHORTSENSE;
    552  1.59    bouyer 	sc_xfer->sense.atapi_sense = chp->ch_error;
    553  1.59    bouyer 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    554  1.59    bouyer 	wdc_atapi_reset(chp, xfer);
    555  1.59    bouyer 	return;
    556   1.2    bouyer }
    557   1.2    bouyer 
    558  1.65   thorpej static int
    559  1.66   thorpej wdc_atapi_intr(struct wdc_channel *chp, struct ata_xfer *xfer, int irq)
    560   1.2    bouyer {
    561  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    562  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    563  1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    564   1.2    bouyer 	int len, phase, i, retries=0;
    565  1.33    bouyer 	int ire;
    566   1.2    bouyer 	int dma_flags = 0;
    567   1.8    bouyer 	void *cmd;
    568   1.2    bouyer 
    569   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    570  1.67   thorpej 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
    571  1.67   thorpej 	    DEBUG_INTR);
    572   1.2    bouyer 
    573   1.2    bouyer 	/* Is it not a transfer, but a control operation? */
    574   1.2    bouyer 	if (drvp->state < READY) {
    575   1.2    bouyer 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    576  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    577   1.2    bouyer 		    drvp->state);
    578  1.55    provos 		panic("wdc_atapi_intr: bad state");
    579   1.2    bouyer 	}
    580  1.24    bouyer 	/*
    581  1.24    bouyer 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    582  1.24    bouyer 	 * Don't try to continue transfer, we may have missed cycles.
    583  1.24    bouyer 	 */
    584  1.24    bouyer 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    585  1.24    bouyer 		sc_xfer->error = XS_TIMEOUT;
    586  1.24    bouyer 		wdc_atapi_reset(chp, xfer);
    587  1.25    bouyer 		return 1;
    588  1.24    bouyer 	}
    589  1.24    bouyer 
    590  1.64   thorpej 	/* Ack interrupt done in wdc_wait_for_unbusy */
    591  1.67   thorpej 	if (wdc->cap & WDC_CAPABILITY_SELECT)
    592  1.67   thorpej 		wdc->select(chp, xfer->c_drive);
    593  1.62      fvdl 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    594  1.63   thorpej 	    WDSD_IBM | (xfer->c_drive << 4));
    595  1.64   thorpej 	if (wdc_wait_for_unbusy(chp,
    596  1.59    bouyer 	    (irq == 0) ? sc_xfer->timeout : 0, AT_POLL) == WDCWAIT_TOUT) {
    597  1.20    bouyer 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    598  1.19    bouyer 			return 0; /* IRQ was not for us */
    599  1.16    bouyer 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    600  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    601   1.2    bouyer 		    xfer->c_bcount, xfer->c_skip);
    602  1.33    bouyer 		if (xfer->c_flags & C_DMA) {
    603  1.59    bouyer 			ata_dmaerr(drvp,
    604  1.59    bouyer 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    605  1.33    bouyer 		}
    606   1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    607   1.2    bouyer 		wdc_atapi_reset(chp, xfer);
    608   1.2    bouyer 		return 1;
    609   1.2    bouyer 	}
    610  1.67   thorpej 	if (wdc->cap & WDC_CAPABILITY_IRQACK)
    611  1.67   thorpej 		wdc->irqack(chp);
    612  1.36    bouyer 
    613  1.48    bouyer 	/*
    614  1.48    bouyer 	 * If we missed an IRQ and were using DMA, flag it as a DMA error
    615  1.48    bouyer 	 * and reset device.
    616  1.48    bouyer 	 */
    617  1.33    bouyer 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
    618  1.59    bouyer 		ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    619  1.48    bouyer 		sc_xfer->error = XS_RESET;
    620  1.48    bouyer 		wdc_atapi_reset(chp, xfer);
    621  1.48    bouyer 		return (1);
    622  1.33    bouyer 	}
    623   1.8    bouyer 	/*
    624   1.8    bouyer 	 * if the request sense command was aborted, report the short sense
    625   1.8    bouyer 	 * previously recorded, else continue normal processing
    626   1.8    bouyer 	 */
    627   1.8    bouyer 
    628  1.33    bouyer 	if (xfer->c_flags & C_DMA)
    629  1.40    bouyer 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    630  1.40    bouyer 		    ?  WDC_DMA_READ : 0;
    631   1.2    bouyer again:
    632  1.62      fvdl 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_lo], 0) +
    633  1.62      fvdl 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_hi], 0);
    634  1.62      fvdl 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_ireason], 0);
    635   1.2    bouyer 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    636   1.2    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    637   1.2    bouyer 	    "ire 0x%x :", xfer->c_bcount,
    638   1.2    bouyer 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    639   1.2    bouyer 
    640   1.2    bouyer 	switch (phase) {
    641   1.2    bouyer 	case PHASE_CMDOUT:
    642  1.57   mycroft 		cmd = sc_xfer->cmd;
    643   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    644   1.2    bouyer 		/* Init the DMA channel if necessary */
    645   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    646  1.67   thorpej 			if ((*wdc->dma_init)(wdc->dma_arg,
    647  1.67   thorpej 			    chp->ch_channel, xfer->c_drive,
    648  1.63   thorpej 			    xfer->c_databuf, xfer->c_bcount, dma_flags) != 0) {
    649   1.2    bouyer 				sc_xfer->error = XS_DRIVER_STUFFUP;
    650   1.2    bouyer 				break;
    651   1.2    bouyer 			}
    652   1.2    bouyer 		}
    653   1.2    bouyer 		/* send packet command */
    654   1.2    bouyer 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    655  1.67   thorpej 		if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    656  1.57   mycroft 			if (drvp->drive_flags & DRIVE_CAP32)
    657   1.2    bouyer 				bus_space_write_multi_4(chp->data32iot,
    658  1.57   mycroft 				    chp->data32ioh, 0, (u_int32_t *)cmd,
    659   1.2    bouyer 				    sc_xfer->cmdlen >> 2);
    660  1.57   mycroft 			else
    661   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    662  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0, (u_int16_t *)cmd,
    663   1.2    bouyer 				    sc_xfer->cmdlen >> 1);
    664   1.2    bouyer 		} else {
    665  1.57   mycroft 			if (drvp->drive_flags & DRIVE_CAP32)
    666   1.2    bouyer 				bus_space_write_multi_stream_4(chp->data32iot,
    667  1.57   mycroft 				    chp->data32ioh, 0, (u_int32_t *)cmd,
    668   1.2    bouyer 				    sc_xfer->cmdlen >> 2);
    669  1.57   mycroft 			else
    670   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    671  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0, (u_int16_t *)cmd,
    672   1.2    bouyer 				    sc_xfer->cmdlen >> 1);
    673   1.2    bouyer 		}
    674   1.2    bouyer 		/* Start the DMA channel if necessary */
    675   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    676  1.67   thorpej 			(*wdc->dma_start)(wdc->dma_arg,
    677  1.67   thorpej 			    chp->ch_channel, xfer->c_drive);
    678  1.33    bouyer 			chp->ch_flags |= WDCF_DMA_WAIT;
    679   1.2    bouyer 		}
    680   1.2    bouyer 
    681  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    682   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    683   1.2    bouyer 		}
    684   1.2    bouyer 		return 1;
    685   1.2    bouyer 
    686   1.2    bouyer 	 case PHASE_DATAOUT:
    687   1.2    bouyer 		/* write data */
    688   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    689  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    690   1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    691  1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    692   1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    693  1.59    bouyer 				ata_dmaerr(drvp,
    694  1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    695   1.2    bouyer 			}
    696   1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    697   1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    698   1.2    bouyer 			return 1;
    699   1.2    bouyer 		}
    700   1.2    bouyer 		if (xfer->c_bcount < len) {
    701   1.2    bouyer 			printf("wdc_atapi_intr: warning: write only "
    702   1.2    bouyer 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    703  1.67   thorpej 			if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    704   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    705  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    706  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    707  1.14   thorpej 				                  xfer->c_skip),
    708   1.2    bouyer 				    xfer->c_bcount >> 1);
    709   1.2    bouyer 			} else {
    710   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    711  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    712  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    713  1.14   thorpej 				                  xfer->c_skip),
    714   1.2    bouyer 				    xfer->c_bcount >> 1);
    715   1.2    bouyer 			}
    716   1.2    bouyer 			for (i = xfer->c_bcount; i < len; i += 2)
    717  1.62      fvdl 				bus_space_write_2(chp->cmd_iot,
    718  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0, 0);
    719   1.2    bouyer 			xfer->c_skip += xfer->c_bcount;
    720   1.2    bouyer 			xfer->c_bcount = 0;
    721   1.2    bouyer 		} else {
    722   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    723  1.67   thorpej 			    if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    724   1.2    bouyer 				bus_space_write_multi_4(chp->data32iot,
    725   1.2    bouyer 				    chp->data32ioh, 0,
    726  1.63   thorpej 				    (u_int32_t *)((char *)xfer->c_databuf +
    727  1.14   thorpej 				                  xfer->c_skip),
    728  1.13  augustss 				    len >> 2);
    729   1.2    bouyer 			    else
    730  1.12    bouyer 				bus_space_write_multi_stream_4(chp->data32iot,
    731  1.12    bouyer 				    chp->data32ioh, wd_data,
    732  1.63   thorpej 				    (u_int32_t *)((char *)xfer->c_databuf +
    733  1.14   thorpej 				                  xfer->c_skip),
    734  1.13  augustss 				    len >> 2);
    735   1.2    bouyer 
    736   1.2    bouyer 			    xfer->c_skip += len & 0xfffffffc;
    737   1.2    bouyer 			    xfer->c_bcount -= len & 0xfffffffc;
    738   1.2    bouyer 			    len = len & 0x03;
    739   1.2    bouyer 			}
    740   1.2    bouyer 			if (len > 0) {
    741  1.67   thorpej 			    if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    742   1.2    bouyer 				bus_space_write_multi_2(chp->cmd_iot,
    743  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    744  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    745  1.14   thorpej 				                  xfer->c_skip),
    746  1.13  augustss 				    len >> 1);
    747   1.2    bouyer 			    else
    748   1.2    bouyer 				bus_space_write_multi_stream_2(chp->cmd_iot,
    749  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    750  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    751  1.14   thorpej 				                  xfer->c_skip),
    752  1.13  augustss 				    len >> 1);
    753   1.2    bouyer 			    xfer->c_skip += len;
    754   1.2    bouyer 			    xfer->c_bcount -= len;
    755   1.2    bouyer 			}
    756   1.2    bouyer 		}
    757  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    758   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    759   1.2    bouyer 		}
    760   1.2    bouyer 		return 1;
    761   1.2    bouyer 
    762   1.2    bouyer 	case PHASE_DATAIN:
    763   1.2    bouyer 		/* Read data */
    764   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    765  1.40    bouyer 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
    766   1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    767  1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    768   1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    769  1.59    bouyer 				ata_dmaerr(drvp,
    770  1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    771   1.2    bouyer 			}
    772   1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    773   1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    774   1.2    bouyer 			return 1;
    775   1.2    bouyer 		}
    776   1.2    bouyer 		if (xfer->c_bcount < len) {
    777   1.2    bouyer 			printf("wdc_atapi_intr: warning: reading only "
    778   1.2    bouyer 			    "%d of %d bytes\n", xfer->c_bcount, len);
    779  1.67   thorpej 			if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
    780   1.2    bouyer 			    bus_space_read_multi_2(chp->cmd_iot,
    781  1.62      fvdl 			    chp->cmd_iohs[wd_data], 0,
    782  1.63   thorpej 			    (u_int16_t *)((char *)xfer->c_databuf +
    783  1.14   thorpej 			                  xfer->c_skip),
    784  1.13  augustss 			    xfer->c_bcount >> 1);
    785   1.2    bouyer 			} else {
    786   1.2    bouyer 			    bus_space_read_multi_stream_2(chp->cmd_iot,
    787  1.62      fvdl 			    chp->cmd_iohs[wd_data], 0,
    788  1.63   thorpej 			    (u_int16_t *)((char *)xfer->c_databuf +
    789  1.14   thorpej 			                  xfer->c_skip),
    790  1.13  augustss 			    xfer->c_bcount >> 1);
    791   1.2    bouyer 			}
    792   1.2    bouyer 			wdcbit_bucket(chp, len - xfer->c_bcount);
    793   1.2    bouyer 			xfer->c_skip += xfer->c_bcount;
    794   1.2    bouyer 			xfer->c_bcount = 0;
    795   1.2    bouyer 		} else {
    796   1.2    bouyer 			if (drvp->drive_flags & DRIVE_CAP32) {
    797  1.67   thorpej 			    if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    798   1.2    bouyer 				bus_space_read_multi_4(chp->data32iot,
    799   1.2    bouyer 				    chp->data32ioh, 0,
    800  1.63   thorpej 				    (u_int32_t *)((char *)xfer->c_databuf +
    801  1.14   thorpej 				                  xfer->c_skip),
    802  1.13  augustss 				    len >> 2);
    803   1.2    bouyer 			    else
    804  1.12    bouyer 				bus_space_read_multi_stream_4(chp->data32iot,
    805  1.12    bouyer 				    chp->data32ioh, wd_data,
    806  1.63   thorpej 				    (u_int32_t *)((char *)xfer->c_databuf +
    807  1.14   thorpej 				                  xfer->c_skip),
    808  1.13  augustss 				    len >> 2);
    809   1.2    bouyer 
    810   1.2    bouyer 			    xfer->c_skip += len & 0xfffffffc;
    811   1.2    bouyer 			    xfer->c_bcount -= len & 0xfffffffc;
    812   1.2    bouyer 			    len = len & 0x03;
    813   1.2    bouyer 			}
    814   1.2    bouyer 			if (len > 0) {
    815  1.67   thorpej 			    if ((wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
    816   1.2    bouyer 				bus_space_read_multi_2(chp->cmd_iot,
    817  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    818  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    819  1.14   thorpej 				                  xfer->c_skip),
    820  1.13  augustss 				    len >> 1);
    821   1.2    bouyer 			    else
    822   1.2    bouyer 				bus_space_read_multi_stream_2(chp->cmd_iot,
    823  1.62      fvdl 				    chp->cmd_iohs[wd_data], 0,
    824  1.63   thorpej 				    (u_int16_t *)((char *)xfer->c_databuf +
    825  1.14   thorpej 				                  xfer->c_skip),
    826  1.13  augustss 				    len >> 1);
    827   1.2    bouyer 			    xfer->c_skip += len;
    828   1.2    bouyer 			    xfer->c_bcount -=len;
    829   1.2    bouyer 			}
    830   1.2    bouyer 		}
    831  1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    832   1.2    bouyer 			chp->ch_flags |= WDCF_IRQ_WAIT;
    833   1.2    bouyer 		}
    834   1.2    bouyer 		return 1;
    835   1.2    bouyer 
    836   1.2    bouyer 	case PHASE_ABORTED:
    837   1.2    bouyer 	case PHASE_COMPLETED:
    838   1.2    bouyer 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    839   1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    840  1.40    bouyer 			xfer->c_bcount -= sc_xfer->datalen;
    841  1.40    bouyer 		}
    842  1.40    bouyer 		sc_xfer->resid = xfer->c_bcount;
    843  1.45    bouyer 		wdc_atapi_phase_complete(xfer);
    844  1.45    bouyer 		return(1);
    845   1.2    bouyer 
    846   1.2    bouyer 	default:
    847   1.2    bouyer 		if (++retries<500) {
    848   1.2    bouyer 			DELAY(100);
    849   1.2    bouyer 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
    850  1.62      fvdl 			    chp->cmd_iohs[wd_status], 0);
    851   1.2    bouyer 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
    852  1.62      fvdl 			    chp->cmd_iohs[wd_error], 0);
    853   1.2    bouyer 			goto again;
    854   1.2    bouyer 		}
    855   1.2    bouyer 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    856   1.2    bouyer 		if (chp->ch_status & WDCS_ERR) {
    857   1.8    bouyer 			sc_xfer->error = XS_SHORTSENSE;
    858   1.2    bouyer 			sc_xfer->sense.atapi_sense = chp->ch_error;
    859   1.2    bouyer 		} else {
    860  1.33    bouyer 			if (xfer->c_flags & C_DMA) {
    861  1.59    bouyer 				ata_dmaerr(drvp,
    862  1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    863  1.33    bouyer 			}
    864  1.16    bouyer 			sc_xfer->error = XS_RESET;
    865  1.16    bouyer 			wdc_atapi_reset(chp, xfer);
    866  1.16    bouyer 			return (1);
    867   1.2    bouyer 		}
    868   1.2    bouyer 	}
    869   1.2    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    870   1.2    bouyer 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    871   1.2    bouyer 	    DEBUG_INTR);
    872   1.2    bouyer 	wdc_atapi_done(chp, xfer);
    873   1.2    bouyer 	return (1);
    874   1.2    bouyer }
    875   1.2    bouyer 
    876  1.65   thorpej static void
    877  1.65   thorpej wdc_atapi_phase_complete(struct ata_xfer *xfer)
    878  1.45    bouyer {
    879  1.66   thorpej 	struct wdc_channel *chp = xfer->c_chp;
    880  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    881  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    882  1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    883  1.45    bouyer 
    884  1.45    bouyer 	/* wait for DSC if needed */
    885  1.45    bouyer 	if (drvp->drive_flags & DRIVE_ATAPIST) {
    886  1.45    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
    887  1.67   thorpej 		    "polldsc %d\n", wdc->sc_dev.dv_xname, chp->ch_channel,
    888  1.63   thorpej 		    xfer->c_drive, xfer->c_dscpoll), DEBUG_XFERS);
    889  1.57   mycroft #if 1
    890  1.57   mycroft 		if (cold)
    891  1.57   mycroft 			panic("wdc_atapi_phase_complete: cold");
    892  1.57   mycroft #endif
    893  1.59    bouyer 		if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10,
    894  1.59    bouyer 		    AT_POLL) == WDCWAIT_TOUT) {
    895  1.57   mycroft 			/* 10ms not enough, try again in 1 tick */
    896  1.57   mycroft 			if (xfer->c_dscpoll++ >
    897  1.57   mycroft 			    mstohz(sc_xfer->timeout)) {
    898  1.59    bouyer 				printf("%s:%d:%d: wait_for_dsc "
    899  1.59    bouyer 				    "failed\n",
    900  1.67   thorpej 				    wdc->sc_dev.dv_xname,
    901  1.67   thorpej 				    chp->ch_channel, xfer->c_drive);
    902  1.45    bouyer 				sc_xfer->error = XS_TIMEOUT;
    903  1.45    bouyer 				wdc_atapi_reset(chp, xfer);
    904  1.59    bouyer 				return;
    905  1.57   mycroft 			} else
    906  1.45    bouyer 				callout_reset(&chp->ch_callout, 1,
    907  1.45    bouyer 				    wdc_atapi_polldsc, xfer);
    908  1.57   mycroft 			return;
    909  1.45    bouyer 		}
    910  1.45    bouyer 	}
    911  1.45    bouyer 
    912  1.45    bouyer 	/*
    913  1.45    bouyer 	 * Some drive occasionally set WDCS_ERR with
    914  1.45    bouyer 	 * "ATA illegal length indication" in the error
    915  1.45    bouyer 	 * register. If we read some data the sense is valid
    916  1.45    bouyer 	 * anyway, so don't report the error.
    917  1.45    bouyer 	 */
    918  1.45    bouyer 	if (chp->ch_status & WDCS_ERR &&
    919  1.45    bouyer 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
    920  1.45    bouyer 	    sc_xfer->resid == sc_xfer->datalen)) {
    921  1.45    bouyer 		/* save the short sense */
    922  1.45    bouyer 		sc_xfer->error = XS_SHORTSENSE;
    923  1.45    bouyer 		sc_xfer->sense.atapi_sense = chp->ch_error;
    924  1.45    bouyer 		if ((sc_xfer->xs_periph->periph_quirks &
    925  1.45    bouyer 		    PQUIRK_NOSENSE) == 0) {
    926  1.45    bouyer 			/* ask scsipi to send a REQUEST_SENSE */
    927  1.45    bouyer 			sc_xfer->error = XS_BUSY;
    928  1.45    bouyer 			sc_xfer->status = SCSI_CHECK;
    929  1.67   thorpej 		} else if (wdc->dma_status &
    930  1.45    bouyer 		    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
    931  1.59    bouyer 			ata_dmaerr(drvp,
    932  1.59    bouyer 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    933  1.45    bouyer 			sc_xfer->error = XS_RESET;
    934  1.45    bouyer 			wdc_atapi_reset(chp, xfer);
    935  1.45    bouyer 			return;
    936  1.45    bouyer 		}
    937  1.45    bouyer 	}
    938  1.45    bouyer 	if (xfer->c_bcount != 0) {
    939  1.45    bouyer 		WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    940  1.45    bouyer 		    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    941  1.45    bouyer 	}
    942  1.45    bouyer #ifdef DIAGNOSTIC
    943  1.45    bouyer 	if (xfer->c_bcount < 0) {
    944  1.45    bouyer 		printf("wdc_atapi_intr warning: bcount value "
    945  1.45    bouyer 		    "is %d after io\n", xfer->c_bcount);
    946  1.45    bouyer 	}
    947  1.45    bouyer #endif
    948  1.45    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
    949  1.45    bouyer 	    "error 0x%x sense 0x%x\n", sc_xfer->error,
    950  1.45    bouyer 	    sc_xfer->sense.atapi_sense), DEBUG_INTR);
    951  1.45    bouyer 	wdc_atapi_done(chp, xfer);
    952  1.45    bouyer }
    953  1.45    bouyer 
    954  1.65   thorpej static void
    955  1.66   thorpej wdc_atapi_done(struct wdc_channel *chp, struct ata_xfer *xfer)
    956   1.2    bouyer {
    957  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    958  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    959   1.2    bouyer 
    960   1.5    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
    961  1.67   thorpej 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    962   1.5    bouyer 	    (u_int)xfer->c_flags), DEBUG_XFERS);
    963  1.32   thorpej 	callout_stop(&chp->ch_callout);
    964   1.2    bouyer 	/* remove this command from xfer queue */
    965   1.2    bouyer 	wdc_free_xfer(chp, xfer);
    966  1.40    bouyer 
    967  1.25    bouyer 	WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    968  1.25    bouyer 	scsipi_done(sc_xfer);
    969   1.2    bouyer 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
    970   1.4    bouyer 	    chp->ch_flags), DEBUG_XFERS);
    971  1.10  drochner 	wdcstart(chp);
    972   1.2    bouyer }
    973   1.2    bouyer 
    974  1.65   thorpej static void
    975  1.66   thorpej wdc_atapi_reset(struct wdc_channel *chp, struct ata_xfer *xfer)
    976   1.2    bouyer {
    977  1.67   thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    978  1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    979  1.63   thorpej 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    980   1.2    bouyer 
    981  1.63   thorpej 	wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
    982   1.2    bouyer 	drvp->state = 0;
    983  1.64   thorpej 	if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL) != 0) {
    984   1.2    bouyer 		printf("%s:%d:%d: reset failed\n",
    985  1.67   thorpej 		    wdc->sc_dev.dv_xname, chp->ch_channel,
    986  1.63   thorpej 		    xfer->c_drive);
    987   1.2    bouyer 		sc_xfer->error = XS_SELTIMEOUT;
    988   1.2    bouyer 	}
    989   1.2    bouyer 	wdc_atapi_done(chp, xfer);
    990   1.2    bouyer 	return;
    991  1.45    bouyer }
    992  1.45    bouyer 
    993  1.59    bouyer static void
    994  1.65   thorpej wdc_atapi_polldsc(void *arg)
    995  1.45    bouyer {
    996  1.65   thorpej 
    997  1.45    bouyer 	wdc_atapi_phase_complete(arg);
    998   1.2    bouyer }
    999