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