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