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