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