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