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