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