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