Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.137
      1  1.137  jdolecek /*	$NetBSD: atapi_wdc.c,v 1.137 2020/04/04 21:36:15 jdolecek 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  *
     15   1.35    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.35    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.35    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.92     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.35    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.35    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.35    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.35    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.35    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.35    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25    1.2    bouyer  */
     26   1.44     lukem 
     27   1.44     lukem #include <sys/cdefs.h>
     28  1.137  jdolecek __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.137 2020/04/04 21:36:15 jdolecek Exp $");
     29    1.2    bouyer 
     30   1.86   thorpej #ifndef ATADEBUG
     31   1.86   thorpej #define ATADEBUG
     32   1.86   thorpej #endif /* ATADEBUG */
     33    1.2    bouyer 
     34    1.2    bouyer #include <sys/param.h>
     35    1.2    bouyer #include <sys/systm.h>
     36    1.2    bouyer #include <sys/kernel.h>
     37    1.2    bouyer #include <sys/file.h>
     38    1.2    bouyer #include <sys/stat.h>
     39    1.2    bouyer #include <sys/buf.h>
     40    1.2    bouyer #include <sys/malloc.h>
     41    1.2    bouyer #include <sys/device.h>
     42    1.2    bouyer #include <sys/syslog.h>
     43    1.2    bouyer #include <sys/proc.h>
     44   1.39   mycroft #include <sys/dvdio.h>
     45    1.2    bouyer 
     46  1.105        ad #include <sys/intr.h>
     47  1.105        ad #include <sys/bus.h>
     48    1.2    bouyer 
     49    1.2    bouyer #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     50   1.65   thorpej #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     51   1.65   thorpej #define	bus_space_write_multi_stream_4	bus_space_write_multi_4
     52   1.65   thorpej #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     53   1.65   thorpej #define	bus_space_read_multi_stream_4	bus_space_read_multi_4
     54    1.2    bouyer #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     55    1.2    bouyer 
     56  1.101     itohy #include <dev/ata/ataconf.h>
     57    1.2    bouyer #include <dev/ata/atareg.h>
     58    1.2    bouyer #include <dev/ata/atavar.h>
     59    1.2    bouyer #include <dev/ic/wdcreg.h>
     60    1.2    bouyer #include <dev/ic/wdcvar.h>
     61    1.2    bouyer 
     62   1.40    bouyer #include <dev/scsipi/scsi_all.h> /* for SCSI status */
     63   1.40    bouyer 
     64    1.2    bouyer #define DEBUG_INTR   0x01
     65    1.2    bouyer #define DEBUG_XFERS  0x02
     66    1.2    bouyer #define DEBUG_STATUS 0x04
     67    1.2    bouyer #define DEBUG_FUNCS  0x08
     68    1.2    bouyer #define DEBUG_PROBE  0x10
     69   1.86   thorpej #ifdef ATADEBUG
     70  1.130   mlelstv #ifndef ATADEBUG_ATAPI_MASK
     71  1.130   mlelstv #define ATADEBUG_ATAPI_MASK 0x0
     72  1.130   mlelstv #endif
     73  1.130   mlelstv int wdcdebug_atapi_mask = ATADEBUG_ATAPI_MASK;
     74   1.86   thorpej #define ATADEBUG_PRINT(args, level) \
     75    1.2    bouyer 	if (wdcdebug_atapi_mask & (level)) \
     76    1.2    bouyer 		printf args
     77    1.2    bouyer #else
     78   1.86   thorpej #define ATADEBUG_PRINT(args, level)
     79    1.2    bouyer #endif
     80    1.2    bouyer 
     81    1.2    bouyer #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     82   1.59    bouyer #define ATAPI_MODE_DELAY 1000	/* 1s, timeout for SET_FEATYRE cmds */
     83   1.65   thorpej 
     84   1.65   thorpej static int	wdc_atapi_get_params(struct scsipi_channel *, int,
     85   1.65   thorpej 				     struct ataparams *);
     86   1.65   thorpej static void	wdc_atapi_probe_device(struct atapibus_softc *, int);
     87   1.65   thorpej static void	wdc_atapi_minphys (struct buf *bp);
     88  1.124  jdolecek static int	wdc_atapi_start(struct ata_channel *,struct ata_xfer *);
     89   1.87   thorpej static int	wdc_atapi_intr(struct ata_channel *, struct ata_xfer *, int);
     90   1.87   thorpej static void	wdc_atapi_kill_xfer(struct ata_channel *,
     91   1.71    bouyer 				    struct ata_xfer *, int);
     92  1.133  jdolecek static void	wdc_atapi_phase_complete(struct ata_xfer *, int);
     93  1.124  jdolecek static void	wdc_atapi_poll(struct ata_channel *, 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.136  riastrad 	.bustype_type = SCSIPI_BUSTYPE_ATAPI,
    105  1.136  riastrad 	.bustype_cmd = atapi_scsipi_cmd,
    106  1.136  riastrad 	.bustype_interpret_sense = atapi_interpret_sense,
    107  1.136  riastrad 	.bustype_printaddr = atapi_print_addr,
    108  1.136  riastrad 	.bustype_kill_pending = wdc_atapi_kill_pending,
    109  1.136  riastrad 	.bustype_async_event_xfer_mode = NULL,
    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.106      cube 	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.135       chs 	chan->chan_ntargets = chp->ch_ndrives;
    142   1.40    bouyer 	chan->chan_nluns = 1;
    143   1.40    bouyer 
    144  1.106      cube 	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.123   mlelstv  * Must be called with adapter lock held
    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.106      cube 	    device_private(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.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
    177  1.124  jdolecek 	bool deactivate = true;
    178   1.28     enami 
    179   1.28     enami 	/* remove this command from xfer queue */
    180   1.71    bouyer 	switch (reason) {
    181  1.124  jdolecek 	case KILL_GONE_INACTIVE:
    182  1.124  jdolecek 		deactivate = false;
    183  1.124  jdolecek 		/* FALLTHROUGH */
    184   1.71    bouyer 	case KILL_GONE:
    185   1.71    bouyer 		sc_xfer->error = XS_DRIVER_STUFFUP;
    186   1.71    bouyer 		break;
    187   1.71    bouyer 	case KILL_RESET:
    188   1.71    bouyer 		sc_xfer->error = XS_RESET;
    189   1.71    bouyer 		break;
    190   1.71    bouyer 	default:
    191   1.71    bouyer 		printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
    192   1.71    bouyer 		    reason);
    193   1.71    bouyer 		panic("wdc_ata_bio_kill_xfer");
    194   1.71    bouyer 	}
    195  1.124  jdolecek 
    196  1.124  jdolecek 	if (deactivate)
    197  1.124  jdolecek 		ata_deactivate_xfer(chp, xfer);
    198  1.124  jdolecek 
    199   1.81   thorpej 	ata_free_xfer(chp, xfer);
    200   1.73    bouyer 	scsipi_done(sc_xfer);
    201   1.28     enami }
    202   1.28     enami 
    203   1.65   thorpej static int
    204   1.65   thorpej wdc_atapi_get_params(struct scsipi_channel *chan, int drive,
    205   1.65   thorpej     struct ataparams *id)
    206    1.2    bouyer {
    207  1.106      cube 	struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
    208   1.88   thorpej 	struct atac_softc *atac = &wdc->sc_atac;
    209   1.87   thorpej 	struct wdc_regs *wdr = &wdc->regs[chan->chan_channel];
    210   1.88   thorpej 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
    211  1.124  jdolecek 	struct ata_xfer *xfer;
    212  1.124  jdolecek 	int rv;
    213  1.124  jdolecek 
    214  1.131  jdolecek 	xfer = ata_get_xfer(chp, false);
    215  1.124  jdolecek 	if (xfer == NULL) {
    216  1.124  jdolecek 		printf("wdc_atapi_get_params: no xfer\n");
    217  1.124  jdolecek 		return EBUSY;
    218  1.124  jdolecek 	}
    219    1.2    bouyer 
    220  1.124  jdolecek 	xfer->c_ata_c.r_command = ATAPI_SOFT_RESET;
    221  1.124  jdolecek 	xfer->c_ata_c.r_st_bmask = 0;
    222  1.124  jdolecek 	xfer->c_ata_c.r_st_pmask = 0;
    223  1.124  jdolecek 	xfer->c_ata_c.flags = AT_WAIT | AT_POLL;
    224  1.124  jdolecek 	xfer->c_ata_c.timeout = WDC_RESET_WAIT;
    225  1.124  jdolecek 	if (wdc_exec_command(&chp->ch_drive[drive], xfer) != ATACMD_COMPLETE) {
    226   1.59    bouyer 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    227   1.59    bouyer 		    " drive %s:%d:%d: driver failed\n",
    228  1.106      cube 		    device_xname(atac->atac_dev), chp->ch_channel, drive);
    229   1.59    bouyer 		panic("wdc_atapi_get_params");
    230   1.59    bouyer 	}
    231  1.124  jdolecek 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    232   1.86   thorpej 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    233   1.59    bouyer 		    "failed for drive %s:%d:%d: error 0x%x\n",
    234  1.106      cube 		    device_xname(atac->atac_dev), chp->ch_channel, drive,
    235  1.124  jdolecek 		    xfer->c_ata_c.r_error), DEBUG_PROBE);
    236  1.124  jdolecek 		rv = -1;
    237  1.124  jdolecek 		goto out_xfer;
    238    1.2    bouyer 	}
    239   1.59    bouyer 	chp->ch_drive[drive].state = 0;
    240    1.2    bouyer 
    241  1.124  jdolecek 	ata_free_xfer(chp, xfer);
    242  1.124  jdolecek 
    243   1.99  christos 	(void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0);
    244   1.92     perry 
    245    1.2    bouyer 	/* Some ATAPI devices need a bit more time after software reset. */
    246    1.2    bouyer 	delay(5000);
    247   1.57   mycroft 	if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) {
    248   1.86   thorpej 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    249  1.124  jdolecek 		    "failed for drive %s:%d:%d\n",
    250  1.124  jdolecek 		    device_xname(atac->atac_dev), chp->ch_channel, drive),
    251  1.124  jdolecek 		    DEBUG_PROBE);
    252  1.124  jdolecek 		rv = -1;
    253  1.124  jdolecek 		goto out;
    254  1.124  jdolecek 	}
    255  1.124  jdolecek 	rv = 0;
    256  1.124  jdolecek out:
    257  1.124  jdolecek 	return rv;
    258  1.124  jdolecek 
    259  1.124  jdolecek out_xfer:
    260  1.124  jdolecek 	ata_free_xfer(chp, xfer);
    261  1.124  jdolecek 	return rv;
    262    1.2    bouyer }
    263    1.2    bouyer 
    264   1.65   thorpej static void
    265   1.65   thorpej wdc_atapi_probe_device(struct atapibus_softc *sc, int target)
    266   1.33    bouyer {
    267   1.40    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    268   1.40    bouyer 	struct scsipi_periph *periph;
    269   1.33    bouyer 	struct ataparams ids;
    270   1.33    bouyer 	struct ataparams *id = &ids;
    271  1.106      cube 	struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
    272   1.88   thorpej 	struct atac_softc *atac = &wdc->sc_atac;
    273   1.88   thorpej 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
    274   1.46    bouyer 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
    275   1.40    bouyer 	struct scsipibus_attach_args sa;
    276   1.33    bouyer 	char serial_number[21], model[41], firmware_revision[9];
    277   1.89   thorpej 	int s;
    278   1.33    bouyer 
    279   1.40    bouyer 	/* skip if already attached */
    280   1.40    bouyer 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    281   1.33    bouyer 		return;
    282   1.40    bouyer 
    283  1.119    bouyer 	/* if no ATAPI device detected at wdc attach time, skip */
    284  1.119    bouyer 	if (drvp->drive_type != ATA_DRIVET_ATAPI) {
    285  1.119    bouyer 		ATADEBUG_PRINT(("wdc_atapi_probe_device: "
    286  1.119    bouyer 		    "drive %d not present\n", target), DEBUG_PROBE);
    287  1.119    bouyer 		return;
    288  1.119    bouyer 	}
    289  1.119    bouyer 
    290   1.57   mycroft 	if (wdc_atapi_get_params(chan, target, id) == 0) {
    291   1.33    bouyer #ifdef ATAPI_DEBUG_PROBE
    292   1.33    bouyer 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    293  1.107      cube 		    device_xname(sc->sc_dev), target,
    294   1.33    bouyer 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    295   1.33    bouyer 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    296   1.33    bouyer #endif
    297  1.134       chs 		periph = scsipi_alloc_periph(M_WAITOK);
    298   1.40    bouyer 		periph->periph_dev = NULL;
    299   1.40    bouyer 		periph->periph_channel = chan;
    300   1.40    bouyer 		periph->periph_switch = &atapi_probe_periphsw;
    301   1.40    bouyer 		periph->periph_target = target;
    302   1.40    bouyer 		periph->periph_lun = 0;
    303   1.56   mycroft 		periph->periph_quirks = PQUIRK_ONLYBIG;
    304   1.40    bouyer 
    305   1.40    bouyer #ifdef SCSIPI_DEBUG
    306   1.40    bouyer 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
    307   1.40    bouyer 		    SCSIPI_DEBUG_TARGET == target)
    308   1.40    bouyer 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    309   1.40    bouyer #endif
    310   1.40    bouyer 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
    311   1.40    bouyer 		if (id->atap_config & ATAPI_CFG_REMOV)
    312   1.40    bouyer 			periph->periph_flags |= PERIPH_REMOVABLE;
    313   1.89   thorpej 		if (periph->periph_type == T_SEQUENTIAL) {
    314   1.89   thorpej 			s = splbio();
    315  1.118    bouyer 			drvp->drive_flags |= ATA_DRIVE_ATAPIDSCW;
    316   1.89   thorpej 			splx(s);
    317   1.89   thorpej 		}
    318   1.40    bouyer 
    319   1.40    bouyer 		sa.sa_periph = periph;
    320   1.40    bouyer 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    321   1.40    bouyer 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
    322   1.40    bouyer 		    T_REMOV : T_FIXED;
    323  1.122  christos 		strnvisx(model, sizeof(model), id->atap_model,
    324  1.122  christos 		    sizeof(id->atap_model), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    325  1.122  christos 		strnvisx(serial_number, sizeof(serial_number),
    326  1.122  christos 		    id->atap_serial, sizeof(id->atap_serial),
    327  1.122  christos 		    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    328  1.122  christos 		strnvisx(firmware_revision, sizeof(firmware_revision),
    329  1.122  christos 		    id->atap_revision, sizeof(id->atap_revision),
    330  1.122  christos 		    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    331   1.33    bouyer 		sa.sa_inqbuf.vendor = model;
    332   1.33    bouyer 		sa.sa_inqbuf.product = serial_number;
    333   1.33    bouyer 		sa.sa_inqbuf.revision = firmware_revision;
    334   1.40    bouyer 
    335   1.40    bouyer 		/*
    336   1.40    bouyer 		 * Determine the operating mode capabilities of the device.
    337   1.40    bouyer 		 */
    338   1.40    bouyer 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    339   1.40    bouyer 			periph->periph_cap |= PERIPH_CAP_CMD16;
    340   1.40    bouyer 		/* XXX This is gross. */
    341   1.40    bouyer 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
    342   1.40    bouyer 
    343   1.40    bouyer 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
    344   1.40    bouyer 
    345   1.40    bouyer 		if (drvp->drv_softc)
    346   1.80   thorpej 			ata_probe_caps(drvp);
    347   1.89   thorpej 		else {
    348   1.89   thorpej 			s = splbio();
    349  1.118    bouyer 			drvp->drive_type = ATA_DRIVET_NONE;
    350   1.89   thorpej 			splx(s);
    351   1.89   thorpej 		}
    352   1.69    bouyer 	} else {
    353   1.89   thorpej 		s = splbio();
    354  1.118    bouyer 		drvp->drive_type = ATA_DRIVET_NONE;
    355   1.89   thorpej 		splx(s);
    356   1.33    bouyer 	}
    357   1.33    bouyer }
    358   1.33    bouyer 
    359  1.131  jdolecek static const struct ata_xfer_ops wdc_atapi_xfer_ops = {
    360  1.131  jdolecek 	.c_start = wdc_atapi_start,
    361  1.131  jdolecek 	.c_intr = wdc_atapi_intr,
    362  1.131  jdolecek 	.c_poll = wdc_atapi_poll,
    363  1.131  jdolecek 	.c_abort = wdc_atapi_reset,
    364  1.131  jdolecek 	.c_kill_xfer = wdc_atapi_kill_xfer,
    365  1.131  jdolecek };
    366  1.131  jdolecek 
    367   1.65   thorpej static void
    368   1.65   thorpej wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    369   1.65   thorpej     void *arg)
    370   1.40    bouyer {
    371   1.40    bouyer 	struct scsipi_adapter *adapt = chan->chan_adapter;
    372   1.40    bouyer 	struct scsipi_periph *periph;
    373    1.2    bouyer 	struct scsipi_xfer *sc_xfer;
    374  1.106      cube 	struct wdc_softc *wdc = device_private(adapt->adapt_dev);
    375   1.88   thorpej 	struct atac_softc *atac = &wdc->sc_atac;
    376   1.63   thorpej 	struct ata_xfer *xfer;
    377   1.40    bouyer 	int channel = chan->chan_channel;
    378   1.40    bouyer 	int drive, s;
    379   1.40    bouyer 
    380   1.40    bouyer 	switch (req) {
    381   1.40    bouyer 	case ADAPTER_REQ_RUN_XFER:
    382   1.40    bouyer 		sc_xfer = arg;
    383   1.40    bouyer 		periph = sc_xfer->xs_periph;
    384   1.40    bouyer 		drive = periph->periph_target;
    385   1.40    bouyer 
    386   1.86   thorpej 		ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
    387  1.106      cube 		    device_xname(atac->atac_dev), channel, drive),
    388  1.106      cube 		    DEBUG_XFERS);
    389  1.106      cube 		if (!device_is_active(atac->atac_dev)) {
    390   1.40    bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
    391   1.40    bouyer 			scsipi_done(sc_xfer);
    392   1.40    bouyer 			return;
    393   1.40    bouyer 		}
    394   1.40    bouyer 
    395  1.131  jdolecek 		xfer = ata_get_xfer(atac->atac_channels[channel], false);
    396   1.40    bouyer 		if (xfer == NULL) {
    397   1.40    bouyer 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
    398   1.40    bouyer 			scsipi_done(sc_xfer);
    399   1.40    bouyer 			return;
    400   1.40    bouyer 		}
    401   1.40    bouyer 
    402   1.40    bouyer 		if (sc_xfer->xs_control & XS_CTL_POLL)
    403   1.40    bouyer 			xfer->c_flags |= C_POLL;
    404  1.102     itohy #if NATA_DMA
    405   1.88   thorpej 		if ((atac->atac_channels[channel]->ch_drive[drive].drive_flags &
    406  1.118    bouyer 		    (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) && sc_xfer->datalen > 0)
    407   1.59    bouyer 			xfer->c_flags |= C_DMA;
    408  1.102     itohy #endif
    409  1.102     itohy #if NATA_DMA && NATA_PIOBM
    410  1.102     itohy 		else
    411  1.102     itohy #endif
    412  1.101     itohy #if NATA_PIOBM
    413  1.102     itohy 		if ((atac->atac_cap & ATAC_CAP_PIOBM) &&
    414  1.101     itohy 		    sc_xfer->datalen > 0)
    415  1.101     itohy 			xfer->c_flags |= C_PIOBM;
    416  1.101     itohy #endif
    417   1.63   thorpej 		xfer->c_drive = drive;
    418   1.40    bouyer 		xfer->c_flags |= C_ATAPI;
    419  1.102     itohy #if NATA_DMA
    420   1.40    bouyer 		if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
    421   1.40    bouyer 		    sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
    422   1.40    bouyer 		    sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
    423   1.40    bouyer 			/*
    424   1.40    bouyer 			 * DVD authentication commands must always be done in
    425   1.40    bouyer 			 * PIO mode.
    426   1.40    bouyer 			 */
    427   1.59    bouyer 			xfer->c_flags &= ~C_DMA;
    428   1.40    bouyer 		}
    429  1.104   reinoud 
    430   1.52    bouyer 		/*
    431  1.104   reinoud 		 * DMA normally can't deal with transfers which are not a
    432  1.104   reinoud 		 * multiple of its databus width. It's a bug to request odd
    433  1.104   reinoud 		 * length transfers for ATAPI.
    434  1.104   reinoud 		 *
    435  1.104   reinoud 		 * Some devices also can't cope with unaligned DMA xfers
    436  1.104   reinoud 		 * either. Also some devices seem to not handle DMA xfers of
    437  1.104   reinoud 		 * less than 4 bytes.
    438  1.104   reinoud 		 *
    439  1.104   reinoud 		 * By enforcing at least 4 byte aligned offset and length for
    440  1.104   reinoud 		 * DMA, we might use PIO where DMA could be allowed but better
    441  1.104   reinoud 		 * safe than sorry as recent problems proved.
    442  1.104   reinoud 		 *
    443  1.104   reinoud 		 * Offending structures that are thus done by PIO instead of
    444  1.104   reinoud 		 * DMA are normally small structures since all bulkdata is
    445  1.104   reinoud 		 * aligned. But as the request may come from userland, we have
    446  1.104   reinoud 		 * to protect against it anyway.
    447  1.104   reinoud 		 *
    448  1.104   reinoud 		 * XXX check for the 32 bit wide flag?
    449   1.52    bouyer 		 */
    450  1.104   reinoud 
    451  1.104   reinoud 		if (((uintptr_t) sc_xfer->data) & 0x03)
    452  1.103   reinoud 			xfer->c_flags &= ~C_DMA;
    453  1.104   reinoud 		if ((sc_xfer->datalen < 4) || (sc_xfer->datalen & 0x03))
    454   1.59    bouyer 			xfer->c_flags &= ~C_DMA;
    455  1.102     itohy #endif	/* NATA_DMA */
    456   1.52    bouyer 
    457   1.63   thorpej 		xfer->c_databuf = sc_xfer->data;
    458   1.40    bouyer 		xfer->c_bcount = sc_xfer->datalen;
    459  1.131  jdolecek 		xfer->ops = &wdc_atapi_xfer_ops;
    460  1.131  jdolecek 		xfer->c_scsipi = sc_xfer;
    461  1.131  jdolecek 		xfer->c_atapi.c_dscpoll = 0;
    462   1.40    bouyer 		s = splbio();
    463   1.88   thorpej 		ata_exec_xfer(atac->atac_channels[channel], xfer);
    464    1.2    bouyer #ifdef DIAGNOSTIC
    465   1.40    bouyer 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    466   1.40    bouyer 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    467   1.40    bouyer 			panic("wdc_atapi_scsipi_request: polled command "
    468   1.40    bouyer 			    "not done");
    469    1.2    bouyer #endif
    470   1.40    bouyer 		splx(s);
    471   1.40    bouyer 		return;
    472   1.40    bouyer 
    473   1.40    bouyer 	default:
    474   1.40    bouyer 		/* Not supported, nothing to do. */
    475   1.41     lukem 		;
    476   1.40    bouyer 	}
    477    1.2    bouyer }
    478    1.2    bouyer 
    479  1.124  jdolecek static int
    480   1.87   thorpej wdc_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
    481    1.2    bouyer {
    482   1.88   thorpej 	struct atac_softc *atac = chp->ch_atac;
    483   1.88   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    484   1.87   thorpej 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    485  1.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
    486   1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    487   1.59    bouyer 	int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0;
    488  1.124  jdolecek 	int tfd;
    489   1.93  christos 	const char *errstring;
    490    1.2    bouyer 
    491   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    492  1.106      cube 	    device_xname(atac->atac_dev), chp->ch_channel, drvp->drive,
    493   1.27   thorpej 	    sc_xfer->xs_control), DEBUG_XFERS);
    494  1.124  jdolecek 
    495  1.124  jdolecek 	ata_channel_lock_owned(chp);
    496  1.124  jdolecek 
    497  1.102     itohy #if NATA_DMA
    498   1.59    bouyer 	if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER))
    499   1.59    bouyer 		drvp->n_xfers++;
    500  1.102     itohy #endif
    501   1.59    bouyer 	/* Do control operations specially. */
    502   1.59    bouyer 	if (__predict_false(drvp->state < READY)) {
    503   1.94     peter 		/* If it's not a polled command, we need the kernel thread */
    504  1.137  jdolecek 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0
    505  1.137  jdolecek 		    && !ata_is_thread_run(chp))
    506  1.124  jdolecek 			return ATASTART_TH;
    507   1.59    bouyer 		/*
    508   1.59    bouyer 		 * disable interrupts, all commands here should be quick
    509  1.111       snj 		 * enough to be able to poll, and we don't go here that often
    510   1.59    bouyer 		 */
    511  1.120   tsutsui 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    512   1.59    bouyer 		     WDCTL_4BIT | WDCTL_IDS);
    513   1.85   thorpej 		if (wdc->select)
    514   1.67   thorpej 			wdc->select(chp, xfer->c_drive);
    515   1.87   thorpej 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    516   1.63   thorpej 		    WDSD_IBM | (xfer->c_drive << 4));
    517   1.59    bouyer 		/* Don't try to set mode if controller can't be adjusted */
    518   1.88   thorpej 		if (atac->atac_set_modes == NULL)
    519   1.59    bouyer 			goto ready;
    520   1.59    bouyer 		/* Also don't try if the drive didn't report its mode */
    521  1.118    bouyer 		if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0)
    522   1.59    bouyer 			goto ready;
    523   1.59    bouyer 		errstring = "unbusy";
    524  1.124  jdolecek 		if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd))
    525   1.59    bouyer 			goto timeout;
    526   1.59    bouyer 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    527   1.59    bouyer 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    528   1.59    bouyer 		errstring = "piomode";
    529  1.124  jdolecek 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags,
    530  1.124  jdolecek 		    &tfd))
    531   1.59    bouyer 			goto timeout;
    532  1.124  jdolecek 		if (ATACH_ST(tfd) & WDCS_ERR) {
    533  1.124  jdolecek 			if (ATACH_ST(tfd) == WDCE_ABRT) {
    534   1.59    bouyer 				/*
    535   1.76   mycroft 				 * Some ATAPI drives reject PIO settings.
    536   1.76   mycroft 				 * Fall back to PIO mode 3 since that's the
    537   1.76   mycroft 				 * minimum for ATAPI.
    538   1.59    bouyer 				 */
    539   1.76   mycroft 				printf("%s:%d:%d: PIO mode %d rejected, "
    540   1.76   mycroft 				    "falling back to PIO mode 3\n",
    541  1.106      cube 				    device_xname(atac->atac_dev),
    542   1.76   mycroft 				    chp->ch_channel, xfer->c_drive,
    543   1.76   mycroft 				    drvp->PIO_mode);
    544   1.76   mycroft 				if (drvp->PIO_mode > 3)
    545   1.76   mycroft 					drvp->PIO_mode = 3;
    546   1.76   mycroft 			} else
    547   1.76   mycroft 				goto error;
    548   1.59    bouyer 		}
    549  1.102     itohy #if NATA_DMA
    550  1.102     itohy #if NATA_UDMA
    551  1.118    bouyer 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
    552   1.59    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    553   1.59    bouyer 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    554  1.102     itohy 		} else
    555  1.102     itohy #endif
    556  1.118    bouyer 		if (drvp->drive_flags & ATA_DRIVE_DMA) {
    557   1.59    bouyer 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    558   1.59    bouyer 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    559   1.59    bouyer 		} else {
    560   1.59    bouyer 			goto ready;
    561   1.59    bouyer 		}
    562   1.59    bouyer 		errstring = "dmamode";
    563  1.124  jdolecek 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags,
    564  1.124  jdolecek 		    &tfd))
    565   1.59    bouyer 			goto timeout;
    566  1.124  jdolecek 		if (ATACH_ST(tfd) & WDCS_ERR) {
    567  1.124  jdolecek 			if (ATACH_ERR(tfd) == WDCE_ABRT) {
    568  1.102     itohy #if NATA_UDMA
    569  1.118    bouyer 				if (drvp->drive_flags & ATA_DRIVE_UDMA)
    570   1.76   mycroft 					goto error;
    571  1.102     itohy 				else
    572  1.102     itohy #endif
    573  1.102     itohy 				{
    574   1.76   mycroft 					/*
    575   1.76   mycroft 					 * The drive rejected our DMA setting.
    576   1.76   mycroft 					 * Fall back to mode 1.
    577   1.76   mycroft 					 */
    578   1.76   mycroft 					printf("%s:%d:%d: DMA mode %d rejected, "
    579   1.76   mycroft 					    "falling back to DMA mode 0\n",
    580  1.106      cube 					    device_xname(atac->atac_dev),
    581   1.76   mycroft 					    chp->ch_channel, xfer->c_drive,
    582   1.76   mycroft 					    drvp->DMA_mode);
    583   1.76   mycroft 					if (drvp->DMA_mode > 0)
    584   1.76   mycroft 						drvp->DMA_mode = 0;
    585   1.76   mycroft 				}
    586   1.76   mycroft 			} else
    587   1.76   mycroft 				goto error;
    588   1.76   mycroft 		}
    589  1.102     itohy #endif	/* NATA_DMA */
    590   1.59    bouyer ready:
    591   1.59    bouyer 		drvp->state = READY;
    592   1.87   thorpej 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    593   1.59    bouyer 		    WDCTL_4BIT);
    594   1.70    bouyer 		delay(10); /* some drives need a little delay here */
    595   1.30    bouyer 	}
    596   1.24    bouyer 	/* start timeout machinery */
    597   1.27   thorpej 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    598  1.131  jdolecek 		callout_reset(&chp->c_timo_callout, mstohz(sc_xfer->timeout),
    599  1.131  jdolecek 		    wdctimeout, chp);
    600   1.59    bouyer 
    601   1.85   thorpej 	if (wdc->select)
    602   1.67   thorpej 		wdc->select(chp, xfer->c_drive);
    603   1.87   thorpej 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    604   1.63   thorpej 	    WDSD_IBM | (xfer->c_drive << 4));
    605  1.124  jdolecek 	switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd)) {
    606   1.59    bouyer 	case WDCWAIT_OK:
    607   1.59    bouyer 		break;
    608   1.59    bouyer 	case WDCWAIT_TOUT:
    609    1.2    bouyer 		printf("wdc_atapi_start: not ready, st = %02x\n",
    610  1.124  jdolecek 		    ATACH_ST(tfd));
    611    1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    612  1.124  jdolecek 		return ATASTART_ABORT;
    613   1.59    bouyer 	case WDCWAIT_THR:
    614  1.124  jdolecek 		return ATASTART_TH;
    615    1.2    bouyer 	}
    616    1.2    bouyer 
    617    1.2    bouyer 	/*
    618    1.2    bouyer 	 * Even with WDCS_ERR, the device should accept a command packet
    619    1.2    bouyer 	 * Limit length to what can be stuffed into the cylinder register
    620    1.2    bouyer 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    621    1.2    bouyer 	 * but not all devices do that and it's not obvious from the
    622    1.2    bouyer 	 * ATAPI spec that that behaviour should be expected.  If more
    623    1.2    bouyer 	 * data is necessary, multiple data transfer phases will be done.
    624    1.2    bouyer 	 */
    625    1.5    bouyer 
    626   1.92     perry 	wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD,
    627   1.29    bouyer 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    628   1.92     perry 	    0, 0, 0,
    629  1.102     itohy #if NATA_DMA
    630  1.102     itohy 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA :
    631  1.102     itohy #endif
    632  1.102     itohy 	    0
    633  1.102     itohy 	    );
    634   1.92     perry 
    635  1.101     itohy #if NATA_PIOBM
    636  1.101     itohy 	if (xfer->c_flags & C_PIOBM) {
    637  1.101     itohy 		int error;
    638  1.101     itohy 		int dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    639  1.101     itohy 		    ?  WDC_DMA_READ : 0;
    640  1.101     itohy 		if (xfer->c_flags & C_POLL) {
    641  1.101     itohy 			/* XXX not supported yet --- fall back to PIO */
    642  1.101     itohy 			xfer->c_flags &= ~C_PIOBM;
    643  1.101     itohy 		} else {
    644  1.101     itohy 			/* Init the DMA channel. */
    645  1.101     itohy 			error = (*wdc->dma_init)(wdc->dma_arg,
    646  1.101     itohy 			    chp->ch_channel, xfer->c_drive,
    647  1.101     itohy 			    (char *)xfer->c_databuf,
    648  1.101     itohy 			    xfer->c_bcount,
    649  1.101     itohy 			    dma_flags | WDC_DMA_PIOBM_ATAPI);
    650  1.101     itohy 			if (error) {
    651  1.101     itohy 				if (error == EINVAL) {
    652  1.101     itohy 					/*
    653  1.101     itohy 					 * We can't do DMA on this transfer
    654  1.101     itohy 					 * for some reason.  Fall back to
    655  1.101     itohy 					 * PIO.
    656  1.101     itohy 					 */
    657  1.101     itohy 					xfer->c_flags &= ~C_PIOBM;
    658  1.101     itohy 					error = 0;
    659  1.101     itohy 				} else {
    660  1.101     itohy 					sc_xfer->error = XS_DRIVER_STUFFUP;
    661  1.101     itohy 					errstring = "piobm";
    662  1.101     itohy 					goto error;
    663  1.101     itohy 				}
    664  1.101     itohy 			}
    665  1.101     itohy 		}
    666  1.101     itohy 	}
    667  1.101     itohy #endif
    668    1.2    bouyer 	/*
    669   1.92     perry 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    670  1.124  jdolecek 	 * the interrupt routine. Poll routine will exit early in this case.
    671    1.2    bouyer 	 */
    672   1.40    bouyer 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
    673  1.124  jdolecek 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL))
    674  1.124  jdolecek 		return ATASTART_POLL;
    675  1.129  jdolecek 	else {
    676  1.129  jdolecek 		chp->ch_flags |= ATACH_IRQ_WAIT;
    677  1.124  jdolecek 		return ATASTART_STARTED;
    678  1.129  jdolecek 	}
    679  1.124  jdolecek 
    680   1.59    bouyer timeout:
    681   1.59    bouyer 	printf("%s:%d:%d: %s timed out\n",
    682  1.106      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    683   1.67   thorpej 	    errstring);
    684   1.59    bouyer 	sc_xfer->error = XS_TIMEOUT;
    685   1.87   thorpej 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    686   1.70    bouyer 	delay(10); /* some drives need a little delay here */
    687  1.124  jdolecek 	return ATASTART_ABORT;
    688  1.124  jdolecek 
    689   1.59    bouyer error:
    690   1.59    bouyer 	printf("%s:%d:%d: %s ",
    691  1.106      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
    692   1.59    bouyer 	    errstring);
    693  1.124  jdolecek 	printf("error (0x%x)\n", ATACH_ERR(tfd));
    694   1.59    bouyer 	sc_xfer->error = XS_SHORTSENSE;
    695  1.124  jdolecek 	sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
    696   1.87   thorpej 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    697   1.70    bouyer 	delay(10); /* some drives need a little delay here */
    698  1.124  jdolecek 	return ATASTART_ABORT;
    699  1.124  jdolecek }
    700  1.124  jdolecek 
    701  1.124  jdolecek static void
    702  1.124  jdolecek wdc_atapi_poll(struct ata_channel *chp, struct ata_xfer *xfer)
    703  1.124  jdolecek {
    704  1.124  jdolecek 	/*
    705  1.124  jdolecek 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    706  1.124  jdolecek 	 * the interrupt routine. If it is a polled command, call the interrupt
    707  1.124  jdolecek 	 * routine until command is done.
    708  1.124  jdolecek 	 */
    709  1.124  jdolecek 	const bool poll = ((xfer->c_scsipi->xs_control & XS_CTL_POLL) != 0);
    710  1.124  jdolecek 
    711  1.124  jdolecek 	/* Wait for at last 400ns for status bit to be valid */
    712  1.124  jdolecek 	DELAY(1);
    713  1.124  jdolecek 	wdc_atapi_intr(chp, xfer, 0);
    714  1.124  jdolecek 
    715  1.124  jdolecek 	if (!poll)
    716  1.124  jdolecek 		return;
    717  1.124  jdolecek 
    718  1.124  jdolecek #if NATA_DMA
    719  1.124  jdolecek 	if (chp->ch_flags & ATACH_DMA_WAIT) {
    720  1.124  jdolecek 		wdc_dmawait(chp, xfer, xfer->c_scsipi->timeout);
    721  1.124  jdolecek 		chp->ch_flags &= ~ATACH_DMA_WAIT;
    722  1.124  jdolecek 	}
    723  1.124  jdolecek #endif
    724  1.124  jdolecek 	while ((xfer->c_scsipi->xs_status & XS_STS_DONE) == 0) {
    725  1.124  jdolecek 		/* Wait for at last 400ns for status bit to be valid */
    726  1.124  jdolecek 		DELAY(1);
    727  1.124  jdolecek 		wdc_atapi_intr(chp, xfer, 0);
    728  1.124  jdolecek 	}
    729    1.2    bouyer }
    730    1.2    bouyer 
    731   1.65   thorpej static int
    732  1.128  jdolecek wdc_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
    733    1.2    bouyer {
    734   1.88   thorpej 	struct atac_softc *atac = chp->ch_atac;
    735   1.88   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    736   1.87   thorpej 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    737  1.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
    738   1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    739    1.2    bouyer 	int len, phase, i, retries=0;
    740  1.124  jdolecek 	int ire, tfd;
    741  1.102     itohy #if NATA_DMA
    742  1.102     itohy 	int error;
    743  1.102     itohy #endif
    744  1.102     itohy #if NATA_DMA || NATA_PIOBM
    745    1.2    bouyer 	int dma_flags = 0;
    746  1.102     itohy #endif
    747    1.8    bouyer 	void *cmd;
    748    1.2    bouyer 
    749   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    750  1.106      cube 	    device_xname(atac->atac_dev), chp->ch_channel, drvp->drive),
    751   1.67   thorpej 	    DEBUG_INTR);
    752    1.2    bouyer 
    753  1.124  jdolecek 	ata_channel_lock(chp);
    754  1.124  jdolecek 
    755    1.2    bouyer 	/* Is it not a transfer, but a control operation? */
    756    1.2    bouyer 	if (drvp->state < READY) {
    757    1.2    bouyer 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    758  1.106      cube 		    device_xname(atac->atac_dev), chp->ch_channel,
    759  1.106      cube 		    xfer->c_drive, drvp->state);
    760   1.55    provos 		panic("wdc_atapi_intr: bad state");
    761    1.2    bouyer 	}
    762   1.24    bouyer 	/*
    763   1.24    bouyer 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    764   1.24    bouyer 	 * Don't try to continue transfer, we may have missed cycles.
    765   1.24    bouyer 	 */
    766   1.24    bouyer 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    767  1.124  jdolecek 		ata_channel_unlock(chp);
    768   1.24    bouyer 		sc_xfer->error = XS_TIMEOUT;
    769   1.24    bouyer 		wdc_atapi_reset(chp, xfer);
    770   1.25    bouyer 		return 1;
    771   1.92     perry 	}
    772   1.24    bouyer 
    773  1.101     itohy #if NATA_PIOBM
    774  1.101     itohy 	/* Transfer-done interrupt for busmastering PIO operation */
    775  1.101     itohy 	if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
    776  1.101     itohy 		chp->ch_flags &= ~ATACH_PIOBM_WAIT;
    777  1.101     itohy 
    778  1.101     itohy 		/* restore transfer length */
    779  1.101     itohy 		len = xfer->c_bcount;
    780  1.131  jdolecek 		if (xfer->c_atapi.c_lenoff < 0)
    781  1.131  jdolecek 			len += xfer->c_atapi.c_lenoff;
    782  1.101     itohy 
    783  1.101     itohy 		if (sc_xfer->xs_control & XS_CTL_DATA_IN)
    784  1.101     itohy 			goto end_piobm_datain;
    785  1.101     itohy 		else
    786  1.101     itohy 			goto end_piobm_dataout;
    787  1.101     itohy 	}
    788  1.101     itohy #endif
    789  1.101     itohy 
    790   1.64   thorpej 	/* Ack interrupt done in wdc_wait_for_unbusy */
    791   1.85   thorpej 	if (wdc->select)
    792   1.67   thorpej 		wdc->select(chp, xfer->c_drive);
    793   1.87   thorpej 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    794   1.63   thorpej 	    WDSD_IBM | (xfer->c_drive << 4));
    795   1.64   thorpej 	if (wdc_wait_for_unbusy(chp,
    796  1.128  jdolecek 	    (irq == 0) ? sc_xfer->timeout : 0, AT_POLL, &tfd) == WDCWAIT_TOUT) {
    797  1.128  jdolecek 		if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
    798  1.124  jdolecek 			ata_channel_unlock(chp);
    799   1.19    bouyer 			return 0; /* IRQ was not for us */
    800  1.124  jdolecek 		}
    801   1.16    bouyer 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    802  1.106      cube 		    device_xname(atac->atac_dev), chp->ch_channel,
    803  1.106      cube 		    xfer->c_drive, xfer->c_bcount, xfer->c_skip);
    804  1.102     itohy #if NATA_DMA
    805   1.33    bouyer 		if (xfer->c_flags & C_DMA) {
    806   1.59    bouyer 			ata_dmaerr(drvp,
    807   1.59    bouyer 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    808   1.33    bouyer 		}
    809  1.102     itohy #endif
    810    1.3    bouyer 		sc_xfer->error = XS_TIMEOUT;
    811  1.132  jdolecek 		ata_channel_unlock(chp);
    812    1.2    bouyer 		wdc_atapi_reset(chp, xfer);
    813    1.2    bouyer 		return 1;
    814    1.2    bouyer 	}
    815   1.85   thorpej 	if (wdc->irqack)
    816   1.67   thorpej 		wdc->irqack(chp);
    817   1.36    bouyer 
    818  1.102     itohy #if NATA_DMA
    819   1.48    bouyer 	/*
    820   1.48    bouyer 	 * If we missed an IRQ and were using DMA, flag it as a DMA error
    821   1.48    bouyer 	 * and reset device.
    822   1.48    bouyer 	 */
    823   1.33    bouyer 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
    824   1.59    bouyer 		ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    825   1.48    bouyer 		sc_xfer->error = XS_RESET;
    826  1.132  jdolecek 		ata_channel_unlock(chp);
    827   1.48    bouyer 		wdc_atapi_reset(chp, xfer);
    828   1.48    bouyer 		return (1);
    829   1.33    bouyer 	}
    830  1.102     itohy #endif
    831   1.92     perry 	/*
    832    1.8    bouyer 	 * if the request sense command was aborted, report the short sense
    833    1.8    bouyer 	 * previously recorded, else continue normal processing
    834    1.8    bouyer 	 */
    835    1.8    bouyer 
    836  1.102     itohy #if NATA_DMA || NATA_PIOBM
    837  1.101     itohy 	if (xfer->c_flags & (C_DMA | C_PIOBM))
    838   1.40    bouyer 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    839   1.40    bouyer 		    ?  WDC_DMA_READ : 0;
    840  1.102     itohy #endif
    841    1.2    bouyer again:
    842   1.87   thorpej 	len = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0) +
    843   1.87   thorpej 	    256 * bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 0);
    844   1.87   thorpej 	ire = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_ireason], 0);
    845  1.124  jdolecek 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (ATACH_ST(tfd) & WDCS_DRQ);
    846   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    847    1.2    bouyer 	    "ire 0x%x :", xfer->c_bcount,
    848  1.124  jdolecek 	    len, ATACH_ST(tfd), ATACH_ERR(tfd), ire), DEBUG_INTR);
    849    1.2    bouyer 
    850    1.2    bouyer 	switch (phase) {
    851    1.2    bouyer 	case PHASE_CMDOUT:
    852   1.57   mycroft 		cmd = sc_xfer->cmd;
    853   1.86   thorpej 		ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    854  1.102     itohy #if NATA_DMA
    855    1.2    bouyer 		/* Init the DMA channel if necessary */
    856    1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    857   1.95   thorpej 			error = (*wdc->dma_init)(wdc->dma_arg,
    858   1.67   thorpej 			    chp->ch_channel, xfer->c_drive,
    859   1.95   thorpej 			    xfer->c_databuf, xfer->c_bcount, dma_flags);
    860   1.95   thorpej 			if (error) {
    861   1.95   thorpej 				if (error == EINVAL) {
    862   1.95   thorpej 					/*
    863   1.95   thorpej 					 * We can't do DMA on this transfer
    864   1.95   thorpej 					 * for some reason.  Fall back to
    865   1.95   thorpej 					 * PIO.
    866   1.95   thorpej 					 */
    867   1.95   thorpej 					xfer->c_flags &= ~C_DMA;
    868   1.95   thorpej 					error = 0;
    869   1.95   thorpej 				} else {
    870   1.95   thorpej 					sc_xfer->error = XS_DRIVER_STUFFUP;
    871   1.95   thorpej 					break;
    872   1.95   thorpej 				}
    873    1.2    bouyer 			}
    874    1.2    bouyer 		}
    875  1.102     itohy #endif
    876   1.75   mycroft 
    877    1.2    bouyer 		/* send packet command */
    878    1.2    bouyer 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    879   1.77   mycroft 		wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen);
    880   1.75   mycroft 
    881  1.102     itohy #if NATA_DMA
    882    1.2    bouyer 		/* Start the DMA channel if necessary */
    883    1.2    bouyer 		if (xfer->c_flags & C_DMA) {
    884   1.67   thorpej 			(*wdc->dma_start)(wdc->dma_arg,
    885   1.67   thorpej 			    chp->ch_channel, xfer->c_drive);
    886   1.87   thorpej 			chp->ch_flags |= ATACH_DMA_WAIT;
    887    1.2    bouyer 		}
    888  1.102     itohy #endif
    889  1.129  jdolecek 
    890  1.129  jdolecek 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    891  1.129  jdolecek 			chp->ch_flags |= ATACH_IRQ_WAIT;
    892  1.129  jdolecek 		}
    893  1.129  jdolecek 
    894  1.124  jdolecek 		ata_channel_unlock(chp);
    895    1.2    bouyer 		return 1;
    896    1.2    bouyer 
    897    1.2    bouyer 	 case PHASE_DATAOUT:
    898    1.2    bouyer 		/* write data */
    899   1.86   thorpej 		ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    900  1.102     itohy #if NATA_DMA
    901   1.27   thorpej 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    902    1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    903   1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    904    1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    905   1.59    bouyer 				ata_dmaerr(drvp,
    906   1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    907    1.2    bouyer 			}
    908    1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    909  1.132  jdolecek 			ata_channel_unlock(chp);
    910    1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    911    1.2    bouyer 			return 1;
    912    1.2    bouyer 		}
    913  1.102     itohy #endif
    914  1.131  jdolecek 		xfer->c_atapi.c_lenoff = len - xfer->c_bcount;
    915    1.2    bouyer 		if (xfer->c_bcount < len) {
    916    1.2    bouyer 			printf("wdc_atapi_intr: warning: write only "
    917    1.2    bouyer 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    918  1.101     itohy 			len = xfer->c_bcount;
    919  1.101     itohy 		}
    920  1.101     itohy 
    921  1.101     itohy #if NATA_PIOBM
    922  1.101     itohy 		if (xfer->c_flags & C_PIOBM) {
    923  1.101     itohy 			/* start the busmastering PIO */
    924  1.101     itohy 			(*wdc->piobm_start)(wdc->dma_arg,
    925  1.101     itohy 			    chp->ch_channel, xfer->c_drive,
    926  1.101     itohy 			    xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
    927  1.129  jdolecek 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
    928  1.129  jdolecek 			    ATACH_PIOBM_WAIT;
    929  1.124  jdolecek 			ata_channel_unlock(chp);
    930  1.101     itohy 			return 1;
    931    1.2    bouyer 		}
    932  1.101     itohy #endif
    933  1.101     itohy 		wdc->dataout_pio(chp, drvp->drive_flags,
    934  1.120   tsutsui 		    (char *)xfer->c_databuf + xfer->c_skip, len);
    935  1.101     itohy 
    936  1.101     itohy #if NATA_PIOBM
    937  1.101     itohy 	end_piobm_dataout:
    938  1.101     itohy #endif
    939  1.131  jdolecek 		for (i = xfer->c_atapi.c_lenoff; i > 0; i -= 2)
    940  1.101     itohy 			bus_space_write_2(wdr->cmd_iot,
    941  1.101     itohy 			    wdr->cmd_iohs[wd_data], 0, 0);
    942  1.101     itohy 
    943  1.101     itohy 		xfer->c_skip += len;
    944  1.101     itohy 		xfer->c_bcount -= len;
    945  1.129  jdolecek 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    946  1.129  jdolecek 			chp->ch_flags |= ATACH_IRQ_WAIT;
    947  1.129  jdolecek 		}
    948  1.124  jdolecek 		ata_channel_unlock(chp);
    949    1.2    bouyer 		return 1;
    950    1.2    bouyer 
    951    1.2    bouyer 	case PHASE_DATAIN:
    952    1.2    bouyer 		/* Read data */
    953   1.86   thorpej 		ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    954  1.102     itohy #if NATA_DMA
    955   1.92     perry 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
    956    1.2    bouyer 		    (xfer->c_flags & C_DMA) != 0) {
    957   1.15    bouyer 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    958    1.2    bouyer 			if (xfer->c_flags & C_DMA) {
    959   1.59    bouyer 				ata_dmaerr(drvp,
    960   1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    961    1.2    bouyer 			}
    962    1.3    bouyer 			sc_xfer->error = XS_TIMEOUT;
    963  1.132  jdolecek 			ata_channel_unlock(chp);
    964    1.2    bouyer 			wdc_atapi_reset(chp, xfer);
    965    1.2    bouyer 			return 1;
    966    1.2    bouyer 		}
    967  1.102     itohy #endif
    968  1.131  jdolecek 		xfer->c_atapi.c_lenoff = len - xfer->c_bcount;
    969    1.2    bouyer 		if (xfer->c_bcount < len) {
    970    1.2    bouyer 			printf("wdc_atapi_intr: warning: reading only "
    971    1.2    bouyer 			    "%d of %d bytes\n", xfer->c_bcount, len);
    972  1.101     itohy 			len = xfer->c_bcount;
    973  1.101     itohy 		}
    974  1.101     itohy 
    975  1.101     itohy #if NATA_PIOBM
    976  1.101     itohy 		if (xfer->c_flags & C_PIOBM) {
    977  1.101     itohy 			/* start the busmastering PIO */
    978  1.101     itohy 			(*wdc->piobm_start)(wdc->dma_arg,
    979  1.101     itohy 			    chp->ch_channel, xfer->c_drive,
    980  1.101     itohy 			    xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
    981  1.129  jdolecek 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
    982  1.129  jdolecek 			    ATACH_PIOBM_WAIT;
    983  1.124  jdolecek 			ata_channel_unlock(chp);
    984  1.101     itohy 			return 1;
    985  1.101     itohy 		}
    986  1.101     itohy #endif
    987  1.101     itohy 		wdc->datain_pio(chp, drvp->drive_flags,
    988  1.101     itohy 		    (char *)xfer->c_databuf + xfer->c_skip, len);
    989  1.101     itohy 
    990  1.101     itohy #if NATA_PIOBM
    991  1.101     itohy 	end_piobm_datain:
    992  1.101     itohy #endif
    993  1.131  jdolecek 		if (xfer->c_atapi.c_lenoff > 0)
    994  1.131  jdolecek 			wdcbit_bucket(chp, xfer->c_atapi.c_lenoff);
    995  1.101     itohy 
    996  1.101     itohy 		xfer->c_skip += len;
    997  1.101     itohy 		xfer->c_bcount -= len;
    998  1.129  jdolecek 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    999  1.129  jdolecek 			chp->ch_flags |= ATACH_IRQ_WAIT;
   1000  1.129  jdolecek 		}
   1001  1.124  jdolecek 		ata_channel_unlock(chp);
   1002    1.2    bouyer 		return 1;
   1003    1.2    bouyer 
   1004    1.2    bouyer 	case PHASE_ABORTED:
   1005    1.2    bouyer 	case PHASE_COMPLETED:
   1006   1.86   thorpej 		ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
   1007  1.102     itohy #if NATA_DMA
   1008    1.2    bouyer 		if (xfer->c_flags & C_DMA) {
   1009   1.40    bouyer 			xfer->c_bcount -= sc_xfer->datalen;
   1010   1.40    bouyer 		}
   1011  1.102     itohy #endif
   1012   1.40    bouyer 		sc_xfer->resid = xfer->c_bcount;
   1013  1.124  jdolecek 		/* this will unlock channel lock too */
   1014  1.133  jdolecek 		wdc_atapi_phase_complete(xfer, tfd);
   1015   1.45    bouyer 		return(1);
   1016    1.2    bouyer 
   1017    1.2    bouyer 	default:
   1018    1.2    bouyer 		if (++retries<500) {
   1019    1.2    bouyer 			DELAY(100);
   1020  1.124  jdolecek 			tfd = ATACH_ERR_ST(
   1021  1.124  jdolecek 			    bus_space_read_1(wdr->cmd_iot,
   1022  1.124  jdolecek 				wdr->cmd_iohs[wd_error], 0),
   1023  1.124  jdolecek 			    bus_space_read_1(wdr->cmd_iot,
   1024  1.124  jdolecek 				wdr->cmd_iohs[wd_status], 0)
   1025  1.124  jdolecek 			);
   1026    1.2    bouyer 			goto again;
   1027    1.2    bouyer 		}
   1028    1.2    bouyer 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
   1029  1.124  jdolecek 		if (ATACH_ST(tfd) & WDCS_ERR) {
   1030    1.8    bouyer 			sc_xfer->error = XS_SHORTSENSE;
   1031  1.124  jdolecek 			sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
   1032    1.2    bouyer 		} else {
   1033  1.102     itohy #if NATA_DMA
   1034   1.33    bouyer 			if (xfer->c_flags & C_DMA) {
   1035   1.59    bouyer 				ata_dmaerr(drvp,
   1036   1.59    bouyer 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
   1037   1.33    bouyer 			}
   1038  1.102     itohy #endif
   1039   1.16    bouyer 			sc_xfer->error = XS_RESET;
   1040  1.132  jdolecek 			ata_channel_unlock(chp);
   1041   1.16    bouyer 			wdc_atapi_reset(chp, xfer);
   1042   1.16    bouyer 			return (1);
   1043    1.2    bouyer 		}
   1044    1.2    bouyer 	}
   1045   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
   1046    1.2    bouyer 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
   1047    1.2    bouyer 	    DEBUG_INTR);
   1048  1.124  jdolecek 	ata_channel_unlock(chp);
   1049    1.2    bouyer 	wdc_atapi_done(chp, xfer);
   1050    1.2    bouyer 	return (1);
   1051    1.2    bouyer }
   1052    1.2    bouyer 
   1053   1.65   thorpej static void
   1054  1.133  jdolecek wdc_atapi_phase_complete(struct ata_xfer *xfer, int tfd)
   1055   1.45    bouyer {
   1056   1.87   thorpej 	struct ata_channel *chp = xfer->c_chp;
   1057   1.88   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1058  1.102     itohy #if NATA_DMA || NATA_PIOBM
   1059   1.88   thorpej 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
   1060  1.102     itohy #endif
   1061  1.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
   1062   1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
   1063  1.124  jdolecek 
   1064  1.124  jdolecek 	ata_channel_lock_owned(chp);
   1065   1.45    bouyer 
   1066   1.45    bouyer 	/* wait for DSC if needed */
   1067  1.118    bouyer 	if (drvp->drive_flags & ATA_DRIVE_ATAPIDSCW) {
   1068   1.86   thorpej 		ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
   1069  1.106      cube 		    "polldsc %d\n", device_xname(atac->atac_dev),
   1070  1.106      cube 		    chp->ch_channel,
   1071  1.131  jdolecek 		    xfer->c_drive, xfer->c_atapi.c_dscpoll), DEBUG_XFERS);
   1072   1.57   mycroft #if 1
   1073   1.57   mycroft 		if (cold)
   1074   1.57   mycroft 			panic("wdc_atapi_phase_complete: cold");
   1075   1.57   mycroft #endif
   1076   1.59    bouyer 		if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10,
   1077  1.124  jdolecek 		    AT_POLL, &tfd) == WDCWAIT_TOUT) {
   1078   1.57   mycroft 			/* 10ms not enough, try again in 1 tick */
   1079  1.131  jdolecek 			if (xfer->c_atapi.c_dscpoll++ >
   1080   1.57   mycroft 			    mstohz(sc_xfer->timeout)) {
   1081   1.59    bouyer 				printf("%s:%d:%d: wait_for_dsc "
   1082   1.59    bouyer 				    "failed\n",
   1083  1.106      cube 				    device_xname(atac->atac_dev),
   1084   1.67   thorpej 				    chp->ch_channel, xfer->c_drive);
   1085  1.124  jdolecek 				ata_channel_unlock(chp);
   1086   1.45    bouyer 				sc_xfer->error = XS_TIMEOUT;
   1087   1.45    bouyer 				wdc_atapi_reset(chp, xfer);
   1088  1.124  jdolecek 			} else {
   1089  1.131  jdolecek 				callout_reset(&chp->c_timo_callout, 1,
   1090  1.131  jdolecek 				    wdc_atapi_polldsc, chp);
   1091  1.124  jdolecek 				ata_channel_unlock(chp);
   1092  1.124  jdolecek 			}
   1093   1.57   mycroft 			return;
   1094   1.45    bouyer 		}
   1095  1.127  christos 	}
   1096  1.127  christos 
   1097   1.45    bouyer 	/*
   1098   1.92     perry 	 * Some drive occasionally set WDCS_ERR with
   1099   1.45    bouyer 	 * "ATA illegal length indication" in the error
   1100   1.45    bouyer 	 * register. If we read some data the sense is valid
   1101   1.45    bouyer 	 * anyway, so don't report the error.
   1102   1.45    bouyer 	 */
   1103  1.124  jdolecek 	if (ATACH_ST(tfd) & WDCS_ERR &&
   1104   1.45    bouyer 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
   1105   1.45    bouyer 	    sc_xfer->resid == sc_xfer->datalen)) {
   1106   1.45    bouyer 		/* save the short sense */
   1107   1.45    bouyer 		sc_xfer->error = XS_SHORTSENSE;
   1108  1.124  jdolecek 		sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
   1109   1.45    bouyer 		if ((sc_xfer->xs_periph->periph_quirks &
   1110   1.45    bouyer 		    PQUIRK_NOSENSE) == 0) {
   1111   1.45    bouyer 			/* ask scsipi to send a REQUEST_SENSE */
   1112   1.45    bouyer 			sc_xfer->error = XS_BUSY;
   1113   1.45    bouyer 			sc_xfer->status = SCSI_CHECK;
   1114  1.102     itohy 		}
   1115  1.102     itohy #if NATA_DMA || NATA_PIOBM
   1116  1.102     itohy 		else if (wdc->dma_status &
   1117   1.45    bouyer 		    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
   1118  1.102     itohy #if NATA_DMA
   1119   1.59    bouyer 			ata_dmaerr(drvp,
   1120   1.59    bouyer 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
   1121  1.102     itohy #endif
   1122   1.45    bouyer 			sc_xfer->error = XS_RESET;
   1123  1.132  jdolecek 			ata_channel_unlock(chp);
   1124   1.45    bouyer 			wdc_atapi_reset(chp, xfer);
   1125   1.45    bouyer 			return;
   1126   1.45    bouyer 		}
   1127  1.102     itohy #endif
   1128   1.45    bouyer 	}
   1129   1.45    bouyer 	if (xfer->c_bcount != 0) {
   1130   1.86   thorpej 		ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is "
   1131   1.45    bouyer 		    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
   1132   1.45    bouyer 	}
   1133   1.45    bouyer #ifdef DIAGNOSTIC
   1134   1.45    bouyer 	if (xfer->c_bcount < 0) {
   1135   1.45    bouyer 		printf("wdc_atapi_intr warning: bcount value "
   1136   1.45    bouyer 		    "is %d after io\n", xfer->c_bcount);
   1137   1.45    bouyer 	}
   1138   1.45    bouyer #endif
   1139   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
   1140   1.45    bouyer 	    "error 0x%x sense 0x%x\n", sc_xfer->error,
   1141   1.45    bouyer 	    sc_xfer->sense.atapi_sense), DEBUG_INTR);
   1142  1.124  jdolecek 	ata_channel_unlock(chp);
   1143   1.45    bouyer 	wdc_atapi_done(chp, xfer);
   1144   1.45    bouyer }
   1145   1.45    bouyer 
   1146   1.65   thorpej static void
   1147   1.87   thorpej wdc_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer)
   1148    1.2    bouyer {
   1149   1.88   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1150  1.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
   1151    1.2    bouyer 
   1152   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
   1153  1.106      cube 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
   1154   1.91   reinoud 	    (u_int)xfer->c_flags), DEBUG_XFERS);
   1155  1.124  jdolecek 
   1156  1.124  jdolecek 	if (ata_waitdrain_xfer_check(chp, xfer))
   1157  1.124  jdolecek 		return;
   1158  1.124  jdolecek 
   1159  1.124  jdolecek 	ata_deactivate_xfer(chp, xfer);
   1160   1.81   thorpej 	ata_free_xfer(chp, xfer);
   1161   1.40    bouyer 
   1162   1.86   thorpej 	ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
   1163   1.25    bouyer 	scsipi_done(sc_xfer);
   1164   1.86   thorpej 	ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n",
   1165    1.4    bouyer 	    chp->ch_flags), DEBUG_XFERS);
   1166   1.84   thorpej 	atastart(chp);
   1167    1.2    bouyer }
   1168    1.2    bouyer 
   1169   1.65   thorpej static void
   1170   1.87   thorpej wdc_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer)
   1171    1.2    bouyer {
   1172   1.88   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1173   1.63   thorpej 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
   1174  1.124  jdolecek 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
   1175  1.124  jdolecek 	int tfd;
   1176    1.2    bouyer 
   1177  1.124  jdolecek 	ata_channel_lock(chp);
   1178   1.63   thorpej 	wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
   1179    1.2    bouyer 	drvp->state = 0;
   1180  1.124  jdolecek 	if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL, &tfd) != 0) {
   1181    1.2    bouyer 		printf("%s:%d:%d: reset failed\n",
   1182  1.106      cube 		    device_xname(atac->atac_dev), chp->ch_channel,
   1183   1.63   thorpej 		    xfer->c_drive);
   1184    1.2    bouyer 		sc_xfer->error = XS_SELTIMEOUT;
   1185    1.2    bouyer 	}
   1186  1.124  jdolecek 	ata_channel_unlock(chp);
   1187    1.2    bouyer 	wdc_atapi_done(chp, xfer);
   1188    1.2    bouyer 	return;
   1189   1.45    bouyer }
   1190   1.45    bouyer 
   1191   1.59    bouyer static void
   1192   1.65   thorpej wdc_atapi_polldsc(void *arg)
   1193   1.45    bouyer {
   1194  1.131  jdolecek 	struct ata_channel *chp = arg;
   1195  1.131  jdolecek 	struct ata_xfer *xfer = ata_queue_get_active_xfer(chp);
   1196  1.131  jdolecek 
   1197  1.131  jdolecek 	KASSERT(xfer != NULL);
   1198  1.124  jdolecek 
   1199  1.124  jdolecek 	ata_channel_lock(chp);
   1200   1.65   thorpej 
   1201  1.124  jdolecek 	/* this will unlock channel lock too */
   1202  1.133  jdolecek 	wdc_atapi_phase_complete(xfer, 0);
   1203    1.2    bouyer }
   1204