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