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