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