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