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