Home | History | Annotate | Line # | Download | only in scsipi
atapi_wdc.c revision 1.87
      1 /*	$NetBSD: atapi_wdc.c,v 1.87 2004/08/14 15:08:06 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 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 Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.87 2004/08/14 15:08:06 thorpej Exp $");
     34 
     35 #ifndef ATADEBUG
     36 #define ATADEBUG
     37 #endif /* ATADEBUG */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/file.h>
     43 #include <sys/stat.h>
     44 #include <sys/buf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/device.h>
     47 #include <sys/syslog.h>
     48 #include <sys/proc.h>
     49 #include <sys/dvdio.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 
     66 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
     67 
     68 #define DEBUG_INTR   0x01
     69 #define DEBUG_XFERS  0x02
     70 #define DEBUG_STATUS 0x04
     71 #define DEBUG_FUNCS  0x08
     72 #define DEBUG_PROBE  0x10
     73 #ifdef ATADEBUG
     74 int wdcdebug_atapi_mask = 0;
     75 #define ATADEBUG_PRINT(args, level) \
     76 	if (wdcdebug_atapi_mask & (level)) \
     77 		printf args
     78 #else
     79 #define ATADEBUG_PRINT(args, level)
     80 #endif
     81 
     82 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
     83 #define ATAPI_MODE_DELAY 1000	/* 1s, timeout for SET_FEATYRE cmds */
     84 
     85 static int	wdc_atapi_get_params(struct scsipi_channel *, int,
     86 				     struct ataparams *);
     87 static void	wdc_atapi_probe_device(struct atapibus_softc *, int);
     88 static void	wdc_atapi_minphys (struct buf *bp);
     89 static void	wdc_atapi_start(struct ata_channel *,struct ata_xfer *);
     90 static int	wdc_atapi_intr(struct ata_channel *, struct ata_xfer *, int);
     91 static void	wdc_atapi_kill_xfer(struct ata_channel *,
     92 				    struct ata_xfer *, int);
     93 static void	wdc_atapi_phase_complete(struct ata_xfer *);
     94 static void	wdc_atapi_done(struct ata_channel *, struct ata_xfer *);
     95 static void	wdc_atapi_reset(struct ata_channel *, struct ata_xfer *);
     96 static void	wdc_atapi_scsipi_request(struct scsipi_channel *,
     97 					 scsipi_adapter_req_t, void *);
     98 static void	wdc_atapi_kill_pending(struct scsipi_periph *);
     99 static void	wdc_atapi_polldsc(void *arg);
    100 
    101 #define MAX_SIZE MAXPHYS
    102 
    103 static const struct scsipi_bustype wdc_atapi_bustype = {
    104 	SCSIPI_BUSTYPE_ATAPI,
    105 	atapi_scsipi_cmd,
    106 	atapi_interpret_sense,
    107 	atapi_print_addr,
    108 	wdc_atapi_kill_pending,
    109 };
    110 
    111 void
    112 wdc_atapibus_attach(struct atabus_softc *ata_sc)
    113 {
    114 	struct ata_channel *chp = ata_sc->sc_chan;
    115 	struct wdc_softc *wdc = chp->ch_wdc;
    116 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    117 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
    118 
    119 	/*
    120 	 * Fill in the scsipi_adapter.
    121 	 */
    122 	adapt->adapt_dev = &wdc->sc_dev;
    123 	adapt->adapt_nchannels = wdc->nchannels;
    124 	adapt->adapt_request = wdc_atapi_scsipi_request;
    125 	adapt->adapt_minphys = wdc_atapi_minphys;
    126 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
    127 		adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
    128 	wdc->sc_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device;
    129 
    130 	/*
    131 	 * Fill in the scsipi_channel.
    132 	 */
    133 	memset(chan, 0, sizeof(*chan));
    134 	chan->chan_adapter = adapt;
    135 	chan->chan_bustype = &wdc_atapi_bustype;
    136 	chan->chan_channel = chp->ch_channel;
    137 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
    138 	chan->chan_openings = 1;
    139 	chan->chan_max_periph = 1;
    140 	chan->chan_ntargets = 2;
    141 	chan->chan_nluns = 1;
    142 
    143 	chp->atapibus = config_found(&ata_sc->sc_dev, chan, atapiprint);
    144 }
    145 
    146 static void
    147 wdc_atapi_minphys(struct buf *bp)
    148 {
    149 
    150 	if (bp->b_bcount > MAX_SIZE)
    151 		bp->b_bcount = MAX_SIZE;
    152 	minphys(bp);
    153 }
    154 
    155 /*
    156  * Kill off all pending xfers for a periph.
    157  *
    158  * Must be called at splbio().
    159  */
    160 static void
    161 wdc_atapi_kill_pending(struct scsipi_periph *periph)
    162 {
    163 	struct wdc_softc *wdc =
    164 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    165 	struct ata_channel *chp =
    166 	    wdc->channels[periph->periph_channel->chan_channel];
    167 
    168 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
    169 }
    170 
    171 static void
    172 wdc_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
    173 {
    174 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    175 
    176 	/* remove this command from xfer queue */
    177 	switch (reason) {
    178 	case KILL_GONE:
    179 		sc_xfer->error = XS_DRIVER_STUFFUP;
    180 		break;
    181 	case KILL_RESET:
    182 		sc_xfer->error = XS_RESET;
    183 		break;
    184 	default:
    185 		printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
    186 		    reason);
    187 		panic("wdc_ata_bio_kill_xfer");
    188 	}
    189 	ata_free_xfer(chp, xfer);
    190 	scsipi_done(sc_xfer);
    191 }
    192 
    193 static int
    194 wdc_atapi_get_params(struct scsipi_channel *chan, int drive,
    195     struct ataparams *id)
    196 {
    197 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    198 	struct wdc_regs *wdr = &wdc->regs[chan->chan_channel];
    199 	struct ata_channel *chp = wdc->channels[chan->chan_channel];
    200 	struct ata_command ata_c;
    201 
    202 	/* if no ATAPI device detected at wdc attach time, skip */
    203 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
    204 		ATADEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
    205 		    drive), DEBUG_PROBE);
    206 		return -1;
    207 	}
    208 
    209 	memset(&ata_c, 0, sizeof(struct ata_command));
    210 	ata_c.r_command = ATAPI_SOFT_RESET;
    211 	ata_c.r_st_bmask = 0;
    212 	ata_c.r_st_pmask = 0;
    213 	ata_c.flags = AT_WAIT | AT_POLL;
    214 	ata_c.timeout = WDC_RESET_WAIT;
    215 	if (wdc_exec_command(&chp->ch_drive[drive], &ata_c) != ATACMD_COMPLETE) {
    216 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
    217 		    " drive %s:%d:%d: driver failed\n",
    218 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive);
    219 		panic("wdc_atapi_get_params");
    220 	}
    221 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    222 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
    223 		    "failed for drive %s:%d:%d: error 0x%x\n",
    224 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive,
    225 		    ata_c.r_error), DEBUG_PROBE);
    226 		return -1;
    227 	}
    228 	chp->ch_drive[drive].state = 0;
    229 
    230 	bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0);
    231 
    232 	/* Some ATAPI devices need a bit more time after software reset. */
    233 	delay(5000);
    234 	if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) {
    235 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
    236 		    "failed for drive %s:%d:%d: error 0x%x\n",
    237 		    wdc->sc_dev.dv_xname, chp->ch_channel, drive,
    238 		    ata_c.r_error), DEBUG_PROBE);
    239 		return -1;
    240 	}
    241 	return 0;
    242 }
    243 
    244 static void
    245 wdc_atapi_probe_device(struct atapibus_softc *sc, int target)
    246 {
    247 	struct scsipi_channel *chan = sc->sc_channel;
    248 	struct scsipi_periph *periph;
    249 	struct ataparams ids;
    250 	struct ataparams *id = &ids;
    251 	struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev;
    252 	struct ata_channel *chp = wdc->channels[chan->chan_channel];
    253 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
    254 	struct scsipibus_attach_args sa;
    255 	char serial_number[21], model[41], firmware_revision[9];
    256 
    257 	/* skip if already attached */
    258 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    259 		return;
    260 
    261 	if (wdc_atapi_get_params(chan, target, id) == 0) {
    262 #ifdef ATAPI_DEBUG_PROBE
    263 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    264 		    sc->sc_dev.dv_xname, target,
    265 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    266 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    267 #endif
    268 		periph = scsipi_alloc_periph(M_NOWAIT);
    269 		if (periph == NULL) {
    270 			printf("%s: unable to allocate periph for drive %d\n",
    271 			    sc->sc_dev.dv_xname, target);
    272 			return;
    273 		}
    274 		periph->periph_dev = NULL;
    275 		periph->periph_channel = chan;
    276 		periph->periph_switch = &atapi_probe_periphsw;
    277 		periph->periph_target = target;
    278 		periph->periph_lun = 0;
    279 		periph->periph_quirks = PQUIRK_ONLYBIG;
    280 
    281 #ifdef SCSIPI_DEBUG
    282 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
    283 		    SCSIPI_DEBUG_TARGET == target)
    284 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    285 #endif
    286 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
    287 		if (id->atap_config & ATAPI_CFG_REMOV)
    288 			periph->periph_flags |= PERIPH_REMOVABLE;
    289 		if (periph->periph_type == T_SEQUENTIAL)
    290 			drvp->drive_flags |= DRIVE_ATAPIST;
    291 
    292 		sa.sa_periph = periph;
    293 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    294 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
    295 		    T_REMOV : T_FIXED;
    296 		scsipi_strvis(model, 40, id->atap_model, 40);
    297 		scsipi_strvis(serial_number, 20, id->atap_serial, 20);
    298 		scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
    299 		sa.sa_inqbuf.vendor = model;
    300 		sa.sa_inqbuf.product = serial_number;
    301 		sa.sa_inqbuf.revision = firmware_revision;
    302 
    303 		/*
    304 		 * Determine the operating mode capabilities of the device.
    305 		 */
    306 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    307 			periph->periph_cap |= PERIPH_CAP_CMD16;
    308 		/* XXX This is gross. */
    309 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
    310 
    311 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
    312 
    313 		if (drvp->drv_softc)
    314 			ata_probe_caps(drvp);
    315 		else
    316 			drvp->drive_flags &= ~DRIVE_ATAPI;
    317 	} else {
    318 		drvp->drive_flags &= ~DRIVE_ATAPI;
    319 	}
    320 }
    321 
    322 static void
    323 wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    324     void *arg)
    325 {
    326 	struct scsipi_adapter *adapt = chan->chan_adapter;
    327 	struct scsipi_periph *periph;
    328 	struct scsipi_xfer *sc_xfer;
    329 	struct wdc_softc *wdc = (void *)adapt->adapt_dev;
    330 	struct ata_xfer *xfer;
    331 	int channel = chan->chan_channel;
    332 	int drive, s;
    333 
    334 	switch (req) {
    335 	case ADAPTER_REQ_RUN_XFER:
    336 		sc_xfer = arg;
    337 		periph = sc_xfer->xs_periph;
    338 		drive = periph->periph_target;
    339 
    340 		ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
    341 		    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
    342 		if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    343 			sc_xfer->error = XS_DRIVER_STUFFUP;
    344 			scsipi_done(sc_xfer);
    345 			return;
    346 		}
    347 
    348 		xfer = ata_get_xfer(ATAXF_NOSLEEP);
    349 		if (xfer == NULL) {
    350 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
    351 			scsipi_done(sc_xfer);
    352 			return;
    353 		}
    354 
    355 		if (sc_xfer->xs_control & XS_CTL_POLL)
    356 			xfer->c_flags |= C_POLL;
    357 		if ((wdc->channels[channel]->ch_drive[drive].drive_flags &
    358 		    (DRIVE_DMA | DRIVE_UDMA)) && sc_xfer->datalen > 0)
    359 			xfer->c_flags |= C_DMA;
    360 		xfer->c_drive = drive;
    361 		xfer->c_flags |= C_ATAPI;
    362 		if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
    363 		    sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
    364 		    sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
    365 			/*
    366 			 * DVD authentication commands must always be done in
    367 			 * PIO mode.
    368 			 */
    369 			xfer->c_flags &= ~C_DMA;
    370 		}
    371 		/*
    372 		 * DMA can't deal with transfers which are not a multiple of
    373 		 * 2 bytes. It's a bug to request such transfers for ATAPI
    374 		 * but as the request can come from userland, we have to
    375 		 * protect against it.
    376 		 * Also some devices seems to not handle DMA xfers of less than
    377 		 * 4 bytes.
    378 		 */
    379 		if (sc_xfer->datalen < 4 || (sc_xfer->datalen & 0x01))
    380 			xfer->c_flags &= ~C_DMA;
    381 
    382 		xfer->c_cmd = sc_xfer;
    383 		xfer->c_databuf = sc_xfer->data;
    384 		xfer->c_bcount = sc_xfer->datalen;
    385 		xfer->c_start = wdc_atapi_start;
    386 		xfer->c_intr = wdc_atapi_intr;
    387 		xfer->c_kill_xfer = wdc_atapi_kill_xfer;
    388 		xfer->c_dscpoll = 0;
    389 		s = splbio();
    390 		ata_exec_xfer(wdc->channels[channel], xfer);
    391 #ifdef DIAGNOSTIC
    392 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
    393 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
    394 			panic("wdc_atapi_scsipi_request: polled command "
    395 			    "not done");
    396 #endif
    397 		splx(s);
    398 		return;
    399 
    400 	default:
    401 		/* Not supported, nothing to do. */
    402 		;
    403 	}
    404 }
    405 
    406 static void
    407 wdc_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
    408 {
    409 	struct wdc_softc *wdc = chp->ch_wdc;
    410 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    411 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    412 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    413 	int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0;
    414 	char *errstring;
    415 
    416 	ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
    417 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive,
    418 	    sc_xfer->xs_control), DEBUG_XFERS);
    419 	if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER))
    420 		drvp->n_xfers++;
    421 	/* Do control operations specially. */
    422 	if (__predict_false(drvp->state < READY)) {
    423 		/* If it's not a polled command, we need the kenrel thread */
    424 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0 &&
    425 		    (chp->ch_flags & ATACH_TH_RUN) == 0) {
    426 			chp->ch_queue->queue_freeze++;
    427 			wakeup(&chp->ch_thread);
    428 			return;
    429 		}
    430 		/*
    431 		 * disable interrupts, all commands here should be quick
    432 		 * enouth to be able to poll, and we don't go here that often
    433 		 */
    434 		 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    435 		     WDCTL_4BIT | WDCTL_IDS);
    436 		if (wdc->select)
    437 			wdc->select(chp, xfer->c_drive);
    438 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    439 		    WDSD_IBM | (xfer->c_drive << 4));
    440 		/* Don't try to set mode if controller can't be adjusted */
    441 		if (wdc->set_modes == NULL)
    442 			goto ready;
    443 		/* Also don't try if the drive didn't report its mode */
    444 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
    445 			goto ready;
    446 		errstring = "unbusy";
    447 		if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags))
    448 			goto timeout;
    449 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    450 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
    451 		errstring = "piomode";
    452 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
    453 			goto timeout;
    454 		if (chp->ch_status & WDCS_ERR) {
    455 			if (chp->ch_error == WDCE_ABRT) {
    456 				/*
    457 				 * Some ATAPI drives reject PIO settings.
    458 				 * Fall back to PIO mode 3 since that's the
    459 				 * minimum for ATAPI.
    460 				 */
    461 				printf("%s:%d:%d: PIO mode %d rejected, "
    462 				    "falling back to PIO mode 3\n",
    463 				    wdc->sc_dev.dv_xname,
    464 				    chp->ch_channel, xfer->c_drive,
    465 				    drvp->PIO_mode);
    466 				if (drvp->PIO_mode > 3)
    467 					drvp->PIO_mode = 3;
    468 			} else
    469 				goto error;
    470 		}
    471 		if (drvp->drive_flags & DRIVE_UDMA) {
    472 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    473 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
    474 		} else if (drvp->drive_flags & DRIVE_DMA) {
    475 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
    476 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
    477 		} else {
    478 			goto ready;
    479 		}
    480 		errstring = "dmamode";
    481 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags))
    482 			goto timeout;
    483 		if (chp->ch_status & WDCS_ERR) {
    484 			if (chp->ch_error == WDCE_ABRT) {
    485 				if (drvp->drive_flags & DRIVE_UDMA)
    486 					goto error;
    487 				else {
    488 					/*
    489 					 * The drive rejected our DMA setting.
    490 					 * Fall back to mode 1.
    491 					 */
    492 					printf("%s:%d:%d: DMA mode %d rejected, "
    493 					    "falling back to DMA mode 0\n",
    494 					    wdc->sc_dev.dv_xname,
    495 					    chp->ch_channel, xfer->c_drive,
    496 					    drvp->DMA_mode);
    497 					if (drvp->DMA_mode > 0)
    498 						drvp->DMA_mode = 0;
    499 				}
    500 			} else
    501 				goto error;
    502 		}
    503 ready:
    504 		drvp->state = READY;
    505 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    506 		    WDCTL_4BIT);
    507 		delay(10); /* some drives need a little delay here */
    508 	}
    509 	/* start timeout machinery */
    510 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
    511 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
    512 		    wdctimeout, chp);
    513 
    514 	if (wdc->select)
    515 		wdc->select(chp, xfer->c_drive);
    516 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    517 	    WDSD_IBM | (xfer->c_drive << 4));
    518 	switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags)  < 0) {
    519 	case WDCWAIT_OK:
    520 		break;
    521 	case WDCWAIT_TOUT:
    522 		printf("wdc_atapi_start: not ready, st = %02x\n",
    523 		    chp->ch_status);
    524 		sc_xfer->error = XS_TIMEOUT;
    525 		wdc_atapi_reset(chp, xfer);
    526 		return;
    527 	case WDCWAIT_THR:
    528 		return;
    529 	}
    530 
    531 	/*
    532 	 * Even with WDCS_ERR, the device should accept a command packet
    533 	 * Limit length to what can be stuffed into the cylinder register
    534 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
    535 	 * but not all devices do that and it's not obvious from the
    536 	 * ATAPI spec that that behaviour should be expected.  If more
    537 	 * data is necessary, multiple data transfer phases will be done.
    538 	 */
    539 
    540 	wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD,
    541 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
    542 	    0, 0, 0,
    543 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
    544 
    545 	/*
    546 	 * If there is no interrupt for CMD input, busy-wait for it (done in
    547 	 * the interrupt routine. If it is a polled command, call the interrupt
    548 	 * routine until command is done.
    549 	 */
    550 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
    551 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) {
    552 		/* Wait for at last 400ns for status bit to be valid */
    553 		DELAY(1);
    554 		wdc_atapi_intr(chp, xfer, 0);
    555 	} else {
    556 		chp->ch_flags |= ATACH_IRQ_WAIT;
    557 	}
    558 	if (sc_xfer->xs_control & XS_CTL_POLL) {
    559 		if (chp->ch_flags & ATACH_DMA_WAIT) {
    560 			wdc_dmawait(chp, xfer, sc_xfer->timeout);
    561 			chp->ch_flags &= ~ATACH_DMA_WAIT;
    562 		}
    563 		while ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
    564 			/* Wait for at last 400ns for status bit to be valid */
    565 			DELAY(1);
    566 			wdc_atapi_intr(chp, xfer, 0);
    567 		}
    568 	}
    569 	return;
    570 timeout:
    571 	printf("%s:%d:%d: %s timed out\n",
    572 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    573 	    errstring);
    574 	sc_xfer->error = XS_TIMEOUT;
    575 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    576 	delay(10); /* some drives need a little delay here */
    577 	wdc_atapi_reset(chp, xfer);
    578 	return;
    579 error:
    580 	printf("%s:%d:%d: %s ",
    581 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    582 	    errstring);
    583 	printf("error (0x%x)\n", chp->ch_error);
    584 	sc_xfer->error = XS_SHORTSENSE;
    585 	sc_xfer->sense.atapi_sense = chp->ch_error;
    586 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    587 	delay(10); /* some drives need a little delay here */
    588 	wdc_atapi_reset(chp, xfer);
    589 	return;
    590 }
    591 
    592 static int
    593 wdc_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
    594 {
    595 	struct wdc_softc *wdc = chp->ch_wdc;
    596 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    597 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    598 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    599 	int len, phase, i, retries=0;
    600 	int ire;
    601 	int dma_flags = 0;
    602 	void *cmd;
    603 
    604 	ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
    605 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
    606 	    DEBUG_INTR);
    607 
    608 	/* Is it not a transfer, but a control operation? */
    609 	if (drvp->state < READY) {
    610 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
    611 		    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    612 		    drvp->state);
    613 		panic("wdc_atapi_intr: bad state");
    614 	}
    615 	/*
    616 	 * If we missed an interrupt in a PIO transfer, reset and restart.
    617 	 * Don't try to continue transfer, we may have missed cycles.
    618 	 */
    619 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
    620 		sc_xfer->error = XS_TIMEOUT;
    621 		wdc_atapi_reset(chp, xfer);
    622 		return 1;
    623 	}
    624 
    625 	/* Ack interrupt done in wdc_wait_for_unbusy */
    626 	if (wdc->select)
    627 		wdc->select(chp, xfer->c_drive);
    628 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    629 	    WDSD_IBM | (xfer->c_drive << 4));
    630 	if (wdc_wait_for_unbusy(chp,
    631 	    (irq == 0) ? sc_xfer->timeout : 0, AT_POLL) == WDCWAIT_TOUT) {
    632 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
    633 			return 0; /* IRQ was not for us */
    634 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
    635 		    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    636 		    xfer->c_bcount, xfer->c_skip);
    637 		if (xfer->c_flags & C_DMA) {
    638 			ata_dmaerr(drvp,
    639 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    640 		}
    641 		sc_xfer->error = XS_TIMEOUT;
    642 		wdc_atapi_reset(chp, xfer);
    643 		return 1;
    644 	}
    645 	if (wdc->irqack)
    646 		wdc->irqack(chp);
    647 
    648 	/*
    649 	 * If we missed an IRQ and were using DMA, flag it as a DMA error
    650 	 * and reset device.
    651 	 */
    652 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
    653 		ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    654 		sc_xfer->error = XS_RESET;
    655 		wdc_atapi_reset(chp, xfer);
    656 		return (1);
    657 	}
    658 	/*
    659 	 * if the request sense command was aborted, report the short sense
    660 	 * previously recorded, else continue normal processing
    661 	 */
    662 
    663 	if (xfer->c_flags & C_DMA)
    664 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
    665 		    ?  WDC_DMA_READ : 0;
    666 again:
    667 	len = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0) +
    668 	    256 * bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 0);
    669 	ire = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_ireason], 0);
    670 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
    671 	ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
    672 	    "ire 0x%x :", xfer->c_bcount,
    673 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
    674 
    675 	switch (phase) {
    676 	case PHASE_CMDOUT:
    677 		cmd = sc_xfer->cmd;
    678 		ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
    679 		/* Init the DMA channel if necessary */
    680 		if (xfer->c_flags & C_DMA) {
    681 			if ((*wdc->dma_init)(wdc->dma_arg,
    682 			    chp->ch_channel, xfer->c_drive,
    683 			    xfer->c_databuf, xfer->c_bcount, dma_flags) != 0) {
    684 				sc_xfer->error = XS_DRIVER_STUFFUP;
    685 				break;
    686 			}
    687 		}
    688 
    689 		/* send packet command */
    690 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
    691 		wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen);
    692 
    693 		/* Start the DMA channel if necessary */
    694 		if (xfer->c_flags & C_DMA) {
    695 			(*wdc->dma_start)(wdc->dma_arg,
    696 			    chp->ch_channel, xfer->c_drive);
    697 			chp->ch_flags |= ATACH_DMA_WAIT;
    698 		}
    699 
    700 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    701 			chp->ch_flags |= ATACH_IRQ_WAIT;
    702 		}
    703 		return 1;
    704 
    705 	 case PHASE_DATAOUT:
    706 		/* write data */
    707 		ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
    708 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
    709 		    (xfer->c_flags & C_DMA) != 0) {
    710 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
    711 			if (xfer->c_flags & C_DMA) {
    712 				ata_dmaerr(drvp,
    713 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    714 			}
    715 			sc_xfer->error = XS_TIMEOUT;
    716 			wdc_atapi_reset(chp, xfer);
    717 			return 1;
    718 		}
    719 		if (xfer->c_bcount < len) {
    720 			printf("wdc_atapi_intr: warning: write only "
    721 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
    722 			wdc->dataout_pio(chp, drvp->drive_flags,
    723 		    	    (char *)xfer->c_databuf + xfer->c_skip,
    724 			    xfer->c_bcount);
    725 			for (i = xfer->c_bcount; i < len; i += 2)
    726 				bus_space_write_2(wdr->cmd_iot,
    727 				    wdr->cmd_iohs[wd_data], 0, 0);
    728 			xfer->c_skip += xfer->c_bcount;
    729 			xfer->c_bcount = 0;
    730 		} else {
    731 			wdc->dataout_pio(chp, drvp->drive_flags,
    732 		    	    (char *)xfer->c_databuf + xfer->c_skip, len);
    733 			xfer->c_skip += len;
    734 			xfer->c_bcount -= len;
    735 		}
    736 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    737 			chp->ch_flags |= ATACH_IRQ_WAIT;
    738 		}
    739 		return 1;
    740 
    741 	case PHASE_DATAIN:
    742 		/* Read data */
    743 		ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
    744 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
    745 		    (xfer->c_flags & C_DMA) != 0) {
    746 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
    747 			if (xfer->c_flags & C_DMA) {
    748 				ata_dmaerr(drvp,
    749 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    750 			}
    751 			sc_xfer->error = XS_TIMEOUT;
    752 			wdc_atapi_reset(chp, xfer);
    753 			return 1;
    754 		}
    755 		if (xfer->c_bcount < len) {
    756 			printf("wdc_atapi_intr: warning: reading only "
    757 			    "%d of %d bytes\n", xfer->c_bcount, len);
    758 			wdc->datain_pio(chp, drvp->drive_flags,
    759 			    (char *)xfer->c_databuf + xfer->c_skip,
    760 			    xfer->c_bcount);
    761 			wdcbit_bucket(chp, len - xfer->c_bcount);
    762 			xfer->c_skip += xfer->c_bcount;
    763 			xfer->c_bcount = 0;
    764 		} else {
    765 			wdc->datain_pio(chp, drvp->drive_flags,
    766 			    (char *)xfer->c_databuf + xfer->c_skip, len);
    767 			xfer->c_skip += len;
    768 			xfer->c_bcount -=len;
    769 		}
    770 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
    771 			chp->ch_flags |= ATACH_IRQ_WAIT;
    772 		}
    773 		return 1;
    774 
    775 	case PHASE_ABORTED:
    776 	case PHASE_COMPLETED:
    777 		ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
    778 		if (xfer->c_flags & C_DMA) {
    779 			xfer->c_bcount -= sc_xfer->datalen;
    780 		}
    781 		sc_xfer->resid = xfer->c_bcount;
    782 		wdc_atapi_phase_complete(xfer);
    783 		return(1);
    784 
    785 	default:
    786 		if (++retries<500) {
    787 			DELAY(100);
    788 			chp->ch_status = bus_space_read_1(wdr->cmd_iot,
    789 			    wdr->cmd_iohs[wd_status], 0);
    790 			chp->ch_error = bus_space_read_1(wdr->cmd_iot,
    791 			    wdr->cmd_iohs[wd_error], 0);
    792 			goto again;
    793 		}
    794 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
    795 		if (chp->ch_status & WDCS_ERR) {
    796 			sc_xfer->error = XS_SHORTSENSE;
    797 			sc_xfer->sense.atapi_sense = chp->ch_error;
    798 		} else {
    799 			if (xfer->c_flags & C_DMA) {
    800 				ata_dmaerr(drvp,
    801 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    802 			}
    803 			sc_xfer->error = XS_RESET;
    804 			wdc_atapi_reset(chp, xfer);
    805 			return (1);
    806 		}
    807 	}
    808 	ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
    809 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
    810 	    DEBUG_INTR);
    811 	wdc_atapi_done(chp, xfer);
    812 	return (1);
    813 }
    814 
    815 static void
    816 wdc_atapi_phase_complete(struct ata_xfer *xfer)
    817 {
    818 	struct ata_channel *chp = xfer->c_chp;
    819 	struct wdc_softc *wdc = chp->ch_wdc;
    820 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    821 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    822 
    823 	/* wait for DSC if needed */
    824 	if (drvp->drive_flags & DRIVE_ATAPIST) {
    825 		ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
    826 		    "polldsc %d\n", wdc->sc_dev.dv_xname, chp->ch_channel,
    827 		    xfer->c_drive, xfer->c_dscpoll), DEBUG_XFERS);
    828 #if 1
    829 		if (cold)
    830 			panic("wdc_atapi_phase_complete: cold");
    831 #endif
    832 		if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10,
    833 		    AT_POLL) == WDCWAIT_TOUT) {
    834 			/* 10ms not enough, try again in 1 tick */
    835 			if (xfer->c_dscpoll++ >
    836 			    mstohz(sc_xfer->timeout)) {
    837 				printf("%s:%d:%d: wait_for_dsc "
    838 				    "failed\n",
    839 				    wdc->sc_dev.dv_xname,
    840 				    chp->ch_channel, xfer->c_drive);
    841 				sc_xfer->error = XS_TIMEOUT;
    842 				wdc_atapi_reset(chp, xfer);
    843 				return;
    844 			} else
    845 				callout_reset(&chp->ch_callout, 1,
    846 				    wdc_atapi_polldsc, xfer);
    847 			return;
    848 		}
    849 	}
    850 
    851 	/*
    852 	 * Some drive occasionally set WDCS_ERR with
    853 	 * "ATA illegal length indication" in the error
    854 	 * register. If we read some data the sense is valid
    855 	 * anyway, so don't report the error.
    856 	 */
    857 	if (chp->ch_status & WDCS_ERR &&
    858 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
    859 	    sc_xfer->resid == sc_xfer->datalen)) {
    860 		/* save the short sense */
    861 		sc_xfer->error = XS_SHORTSENSE;
    862 		sc_xfer->sense.atapi_sense = chp->ch_error;
    863 		if ((sc_xfer->xs_periph->periph_quirks &
    864 		    PQUIRK_NOSENSE) == 0) {
    865 			/* ask scsipi to send a REQUEST_SENSE */
    866 			sc_xfer->error = XS_BUSY;
    867 			sc_xfer->status = SCSI_CHECK;
    868 		} else if (wdc->dma_status &
    869 		    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
    870 			ata_dmaerr(drvp,
    871 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
    872 			sc_xfer->error = XS_RESET;
    873 			wdc_atapi_reset(chp, xfer);
    874 			return;
    875 		}
    876 	}
    877 	if (xfer->c_bcount != 0) {
    878 		ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is "
    879 		    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
    880 	}
    881 #ifdef DIAGNOSTIC
    882 	if (xfer->c_bcount < 0) {
    883 		printf("wdc_atapi_intr warning: bcount value "
    884 		    "is %d after io\n", xfer->c_bcount);
    885 	}
    886 #endif
    887 	ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
    888 	    "error 0x%x sense 0x%x\n", sc_xfer->error,
    889 	    sc_xfer->sense.atapi_sense), DEBUG_INTR);
    890 	wdc_atapi_done(chp, xfer);
    891 }
    892 
    893 static void
    894 wdc_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer)
    895 {
    896 	struct wdc_softc *wdc = chp->ch_wdc;
    897 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    898 	int drive = xfer->c_drive;
    899 
    900 	ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
    901 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
    902 	    (u_int)xfer->c_flags), DEBUG_XFERS);
    903 	callout_stop(&chp->ch_callout);
    904 	/* mark controller inactive and free the command */
    905 	chp->ch_queue->active_xfer = NULL;
    906 	ata_free_xfer(chp, xfer);
    907 
    908 	if (chp->ch_drive[drive].drive_flags & DRIVE_WAITDRAIN) {
    909 		sc_xfer->error = XS_DRIVER_STUFFUP;
    910 		chp->ch_drive[drive].drive_flags &= ~DRIVE_WAITDRAIN;
    911 		wakeup(&chp->ch_queue->active_xfer);
    912 	}
    913 
    914 	ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
    915 	scsipi_done(sc_xfer);
    916 	ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n",
    917 	    chp->ch_flags), DEBUG_XFERS);
    918 	atastart(chp);
    919 }
    920 
    921 static void
    922 wdc_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer)
    923 {
    924 	struct wdc_softc *wdc = chp->ch_wdc;
    925 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
    926 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
    927 
    928 	wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
    929 	drvp->state = 0;
    930 	if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL) != 0) {
    931 		printf("%s:%d:%d: reset failed\n",
    932 		    wdc->sc_dev.dv_xname, chp->ch_channel,
    933 		    xfer->c_drive);
    934 		sc_xfer->error = XS_SELTIMEOUT;
    935 	}
    936 	wdc_atapi_done(chp, xfer);
    937 	return;
    938 }
    939 
    940 static void
    941 wdc_atapi_polldsc(void *arg)
    942 {
    943 
    944 	wdc_atapi_phase_complete(arg);
    945 }
    946