xref: /netbsd-src/sys/dev/scsipi/atapi_wdc.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: atapi_wdc.c,v 1.138 2020/04/13 10:49:34 jdolecek 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.138 2020/04/13 10:49:34 jdolecek Exp $");
29 
30 #ifndef ATADEBUG
31 #define ATADEBUG
32 #endif /* ATADEBUG */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/buf.h>
40 #include <sys/malloc.h>
41 #include <sys/device.h>
42 #include <sys/syslog.h>
43 #include <sys/proc.h>
44 #include <sys/dvdio.h>
45 
46 #include <sys/intr.h>
47 #include <sys/bus.h>
48 
49 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
50 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
51 #define	bus_space_write_multi_stream_4	bus_space_write_multi_4
52 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
53 #define	bus_space_read_multi_stream_4	bus_space_read_multi_4
54 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
55 
56 #include <dev/ata/ataconf.h>
57 #include <dev/ata/atareg.h>
58 #include <dev/ata/atavar.h>
59 #include <dev/ic/wdcreg.h>
60 #include <dev/ic/wdcvar.h>
61 
62 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
63 
64 #define DEBUG_INTR   0x01
65 #define DEBUG_XFERS  0x02
66 #define DEBUG_STATUS 0x04
67 #define DEBUG_FUNCS  0x08
68 #define DEBUG_PROBE  0x10
69 #ifdef ATADEBUG
70 #ifndef ATADEBUG_ATAPI_MASK
71 #define ATADEBUG_ATAPI_MASK 0x0
72 #endif
73 int wdcdebug_atapi_mask = ATADEBUG_ATAPI_MASK;
74 #define ATADEBUG_PRINT(args, level) \
75 	if (wdcdebug_atapi_mask & (level)) \
76 		printf args
77 #else
78 #define ATADEBUG_PRINT(args, level)
79 #endif
80 
81 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
82 #define ATAPI_MODE_DELAY 1000	/* 1s, timeout for SET_FEATYRE cmds */
83 
84 static int	wdc_atapi_get_params(struct scsipi_channel *, int,
85 				     struct ataparams *);
86 static void	wdc_atapi_probe_device(struct atapibus_softc *, int);
87 static void	wdc_atapi_minphys (struct buf *bp);
88 static int	wdc_atapi_start(struct ata_channel *,struct ata_xfer *);
89 static int	wdc_atapi_intr(struct ata_channel *, struct ata_xfer *, int);
90 static void	wdc_atapi_kill_xfer(struct ata_channel *,
91 				    struct ata_xfer *, int);
92 static void	wdc_atapi_phase_complete(struct ata_xfer *, int);
93 static void	wdc_atapi_poll(struct ata_channel *, struct ata_xfer *);
94 static void	wdc_atapi_done(struct ata_channel *, struct ata_xfer *);
95 static void	wdc_atapi_reset(struct ata_channel *, struct ata_xfer *);
96 static void	wdc_atapi_scsipi_request(struct scsipi_channel *,
97 					 scsipi_adapter_req_t, void *);
98 static void	wdc_atapi_kill_pending(struct scsipi_periph *);
99 static void	wdc_atapi_polldsc(void *arg);
100 
101 #define MAX_SIZE MAXPHYS
102 
103 static const struct scsipi_bustype wdc_atapi_bustype = {
104 	.bustype_type = SCSIPI_BUSTYPE_ATAPI,
105 	.bustype_cmd = atapi_scsipi_cmd,
106 	.bustype_interpret_sense = atapi_interpret_sense,
107 	.bustype_printaddr = atapi_print_addr,
108 	.bustype_kill_pending = wdc_atapi_kill_pending,
109 	.bustype_async_event_xfer_mode = NULL,
110 };
111 
112 void
113 wdc_atapibus_attach(struct atabus_softc *ata_sc)
114 {
115 	struct ata_channel *chp = ata_sc->sc_chan;
116 	struct atac_softc *atac = chp->ch_atac;
117 	struct scsipi_adapter *adapt = &atac->atac_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 = atac->atac_dev;
124 	adapt->adapt_nchannels = atac->atac_nchannels;
125 	adapt->adapt_request = wdc_atapi_scsipi_request;
126 	adapt->adapt_minphys = wdc_atapi_minphys;
127 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
128 		adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
129 	atac->atac_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->ch_channel;
138 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
139 	chan->chan_openings = 1;
140 	chan->chan_max_periph = 1;
141 	chan->chan_ntargets = chp->ch_ndrives;
142 	chan->chan_nluns = 1;
143 
144 	chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
145 		atapiprint);
146 }
147 
148 static void
149 wdc_atapi_minphys(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 with adapter lock held
161  */
162 static void
163 wdc_atapi_kill_pending(struct scsipi_periph *periph)
164 {
165 	struct atac_softc *atac =
166 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
167 	struct ata_channel *chp =
168 	    atac->atac_channels[periph->periph_channel->chan_channel];
169 
170 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
171 }
172 
173 static void
174 wdc_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
175 {
176 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
177 	bool deactivate = true;
178 
179 	/* remove this command from xfer queue */
180 	switch (reason) {
181 	case KILL_GONE_INACTIVE:
182 		deactivate = false;
183 		/* FALLTHROUGH */
184 	case KILL_GONE:
185 		sc_xfer->error = XS_DRIVER_STUFFUP;
186 		break;
187 	case KILL_RESET:
188 		sc_xfer->error = XS_RESET;
189 		break;
190 	default:
191 		printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
192 		    reason);
193 		panic("wdc_ata_bio_kill_xfer");
194 	}
195 
196 	if (deactivate)
197 		ata_deactivate_xfer(chp, xfer);
198 
199 	ata_free_xfer(chp, xfer);
200 	scsipi_done(sc_xfer);
201 }
202 
203 static int
204 wdc_atapi_get_params(struct scsipi_channel *chan, int drive,
205     struct ataparams *id)
206 {
207 	struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
208 	struct atac_softc *atac = &wdc->sc_atac;
209 	struct wdc_regs *wdr = &wdc->regs[chan->chan_channel];
210 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
211 	struct ata_xfer *xfer;
212 	int rv;
213 
214 	xfer = ata_get_xfer(chp, false);
215 	if (xfer == NULL) {
216 		printf("wdc_atapi_get_params: no xfer\n");
217 		return EBUSY;
218 	}
219 
220 	xfer->c_ata_c.r_command = ATAPI_SOFT_RESET;
221 	xfer->c_ata_c.r_st_bmask = 0;
222 	xfer->c_ata_c.r_st_pmask = 0;
223 	xfer->c_ata_c.flags = AT_WAIT | AT_POLL;
224 	xfer->c_ata_c.timeout = WDC_RESET_WAIT;
225 
226 	wdc_exec_command(&chp->ch_drive[drive], xfer);
227 	ata_wait_cmd(chp, xfer);
228 
229 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
230 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
231 		    "failed for drive %s:%d:%d: error 0x%x\n",
232 		    device_xname(atac->atac_dev), chp->ch_channel, drive,
233 		    xfer->c_ata_c.r_error), DEBUG_PROBE);
234 		rv = -1;
235 		goto out_xfer;
236 	}
237 	chp->ch_drive[drive].state = 0;
238 
239 	ata_free_xfer(chp, xfer);
240 
241 	(void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0);
242 
243 	/* Some ATAPI devices need a bit more time after software reset. */
244 	delay(5000);
245 	if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) {
246 		ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
247 		    "failed for drive %s:%d:%d\n",
248 		    device_xname(atac->atac_dev), chp->ch_channel, drive),
249 		    DEBUG_PROBE);
250 		rv = -1;
251 		goto out;
252 	}
253 	rv = 0;
254 out:
255 	return rv;
256 
257 out_xfer:
258 	ata_free_xfer(chp, xfer);
259 	return rv;
260 }
261 
262 static void
263 wdc_atapi_probe_device(struct atapibus_softc *sc, int target)
264 {
265 	struct scsipi_channel *chan = sc->sc_channel;
266 	struct scsipi_periph *periph;
267 	struct ataparams ids;
268 	struct ataparams *id = &ids;
269 	struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev);
270 	struct atac_softc *atac = &wdc->sc_atac;
271 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
272 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
273 	struct scsipibus_attach_args sa;
274 	char serial_number[21], model[41], firmware_revision[9];
275 	int s;
276 
277 	/* skip if already attached */
278 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
279 		return;
280 
281 	/* if no ATAPI device detected at wdc attach time, skip */
282 	if (drvp->drive_type != ATA_DRIVET_ATAPI) {
283 		ATADEBUG_PRINT(("wdc_atapi_probe_device: "
284 		    "drive %d not present\n", target), DEBUG_PROBE);
285 		return;
286 	}
287 
288 	if (wdc_atapi_get_params(chan, target, id) == 0) {
289 #ifdef ATAPI_DEBUG_PROBE
290 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
291 		    device_xname(sc->sc_dev), target,
292 		    id->atap_config & ATAPI_CFG_CMD_MASK,
293 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
294 #endif
295 		periph = scsipi_alloc_periph(M_WAITOK);
296 		periph->periph_dev = NULL;
297 		periph->periph_channel = chan;
298 		periph->periph_switch = &atapi_probe_periphsw;
299 		periph->periph_target = target;
300 		periph->periph_lun = 0;
301 		periph->periph_quirks = PQUIRK_ONLYBIG;
302 
303 #ifdef SCSIPI_DEBUG
304 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
305 		    SCSIPI_DEBUG_TARGET == target)
306 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
307 #endif
308 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
309 		if (id->atap_config & ATAPI_CFG_REMOV)
310 			periph->periph_flags |= PERIPH_REMOVABLE;
311 		if (periph->periph_type == T_SEQUENTIAL) {
312 			s = splbio();
313 			drvp->drive_flags |= ATA_DRIVE_ATAPIDSCW;
314 			splx(s);
315 		}
316 
317 		sa.sa_periph = periph;
318 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
319 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
320 		    T_REMOV : T_FIXED;
321 		strnvisx(model, sizeof(model), id->atap_model,
322 		    sizeof(id->atap_model), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
323 		strnvisx(serial_number, sizeof(serial_number),
324 		    id->atap_serial, sizeof(id->atap_serial),
325 		    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
326 		strnvisx(firmware_revision, sizeof(firmware_revision),
327 		    id->atap_revision, sizeof(id->atap_revision),
328 		    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
329 		sa.sa_inqbuf.vendor = model;
330 		sa.sa_inqbuf.product = serial_number;
331 		sa.sa_inqbuf.revision = firmware_revision;
332 
333 		/*
334 		 * Determine the operating mode capabilities of the device.
335 		 */
336 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
337 			periph->periph_cap |= PERIPH_CAP_CMD16;
338 		/* XXX This is gross. */
339 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
340 
341 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
342 
343 		if (drvp->drv_softc)
344 			ata_probe_caps(drvp);
345 		else {
346 			s = splbio();
347 			drvp->drive_type = ATA_DRIVET_NONE;
348 			splx(s);
349 		}
350 	} else {
351 		s = splbio();
352 		drvp->drive_type = ATA_DRIVET_NONE;
353 		splx(s);
354 	}
355 }
356 
357 static const struct ata_xfer_ops wdc_atapi_xfer_ops = {
358 	.c_start = wdc_atapi_start,
359 	.c_intr = wdc_atapi_intr,
360 	.c_poll = wdc_atapi_poll,
361 	.c_abort = wdc_atapi_reset,
362 	.c_kill_xfer = wdc_atapi_kill_xfer,
363 };
364 
365 static void
366 wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
367     void *arg)
368 {
369 	struct scsipi_adapter *adapt = chan->chan_adapter;
370 	struct scsipi_periph *periph;
371 	struct scsipi_xfer *sc_xfer;
372 	struct wdc_softc *wdc = device_private(adapt->adapt_dev);
373 	struct atac_softc *atac = &wdc->sc_atac;
374 	struct ata_xfer *xfer;
375 	int channel = chan->chan_channel;
376 	int drive, s;
377 
378 	switch (req) {
379 	case ADAPTER_REQ_RUN_XFER:
380 		sc_xfer = arg;
381 		periph = sc_xfer->xs_periph;
382 		drive = periph->periph_target;
383 
384 		ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n",
385 		    device_xname(atac->atac_dev), channel, drive),
386 		    DEBUG_XFERS);
387 		if (!device_is_active(atac->atac_dev)) {
388 			sc_xfer->error = XS_DRIVER_STUFFUP;
389 			scsipi_done(sc_xfer);
390 			return;
391 		}
392 
393 		xfer = ata_get_xfer(atac->atac_channels[channel], false);
394 		if (xfer == NULL) {
395 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
396 			scsipi_done(sc_xfer);
397 			return;
398 		}
399 
400 		if (sc_xfer->xs_control & XS_CTL_POLL)
401 			xfer->c_flags |= C_POLL;
402 #if NATA_DMA
403 		if ((atac->atac_channels[channel]->ch_drive[drive].drive_flags &
404 		    (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) && sc_xfer->datalen > 0)
405 			xfer->c_flags |= C_DMA;
406 #endif
407 #if NATA_DMA && NATA_PIOBM
408 		else
409 #endif
410 #if NATA_PIOBM
411 		if ((atac->atac_cap & ATAC_CAP_PIOBM) &&
412 		    sc_xfer->datalen > 0)
413 			xfer->c_flags |= C_PIOBM;
414 #endif
415 		xfer->c_drive = drive;
416 		xfer->c_flags |= C_ATAPI;
417 #if NATA_DMA
418 		if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY ||
419 		    sc_xfer->cmd->opcode == GPCMD_SEND_KEY ||
420 		    sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) {
421 			/*
422 			 * DVD authentication commands must always be done in
423 			 * PIO mode.
424 			 */
425 			xfer->c_flags &= ~C_DMA;
426 		}
427 
428 		/*
429 		 * DMA normally can't deal with transfers which are not a
430 		 * multiple of its databus width. It's a bug to request odd
431 		 * length transfers for ATAPI.
432 		 *
433 		 * Some devices also can't cope with unaligned DMA xfers
434 		 * either. Also some devices seem to not handle DMA xfers of
435 		 * less than 4 bytes.
436 		 *
437 		 * By enforcing at least 4 byte aligned offset and length for
438 		 * DMA, we might use PIO where DMA could be allowed but better
439 		 * safe than sorry as recent problems proved.
440 		 *
441 		 * Offending structures that are thus done by PIO instead of
442 		 * DMA are normally small structures since all bulkdata is
443 		 * aligned. But as the request may come from userland, we have
444 		 * to protect against it anyway.
445 		 *
446 		 * XXX check for the 32 bit wide flag?
447 		 */
448 
449 		if (((uintptr_t) sc_xfer->data) & 0x03)
450 			xfer->c_flags &= ~C_DMA;
451 		if ((sc_xfer->datalen < 4) || (sc_xfer->datalen & 0x03))
452 			xfer->c_flags &= ~C_DMA;
453 #endif	/* NATA_DMA */
454 
455 		xfer->c_databuf = sc_xfer->data;
456 		xfer->c_bcount = sc_xfer->datalen;
457 		xfer->ops = &wdc_atapi_xfer_ops;
458 		xfer->c_scsipi = sc_xfer;
459 		xfer->c_atapi.c_dscpoll = 0;
460 		s = splbio();
461 		ata_exec_xfer(atac->atac_channels[channel], xfer);
462 #ifdef DIAGNOSTIC
463 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
464 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
465 			panic("wdc_atapi_scsipi_request: polled command "
466 			    "not done");
467 #endif
468 		splx(s);
469 		return;
470 
471 	default:
472 		/* Not supported, nothing to do. */
473 		;
474 	}
475 }
476 
477 static int
478 wdc_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
479 {
480 	struct atac_softc *atac = chp->ch_atac;
481 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
482 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
483 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
484 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
485 	int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0;
486 	int tfd;
487 	const char *errstring;
488 
489 	ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
490 	    device_xname(atac->atac_dev), chp->ch_channel, drvp->drive,
491 	    sc_xfer->xs_control), DEBUG_XFERS);
492 
493 	ata_channel_lock_owned(chp);
494 
495 #if NATA_DMA
496 	if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER))
497 		drvp->n_xfers++;
498 #endif
499 	/* Do control operations specially. */
500 	if (__predict_false(drvp->state < READY)) {
501 		/* If it's not a polled command, we need the kernel thread */
502 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0
503 		    && !ata_is_thread_run(chp))
504 			return ATASTART_TH;
505 		/*
506 		 * disable interrupts, all commands here should be quick
507 		 * enough to be able to poll, and we don't go here that often
508 		 */
509 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
510 		     WDCTL_4BIT | WDCTL_IDS);
511 		if (wdc->select)
512 			wdc->select(chp, xfer->c_drive);
513 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
514 		    WDSD_IBM | (xfer->c_drive << 4));
515 		/* Don't try to set mode if controller can't be adjusted */
516 		if (atac->atac_set_modes == NULL)
517 			goto ready;
518 		/* Also don't try if the drive didn't report its mode */
519 		if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0)
520 			goto ready;
521 		errstring = "unbusy";
522 		if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd))
523 			goto timeout;
524 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
525 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
526 		errstring = "piomode";
527 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags,
528 		    &tfd))
529 			goto timeout;
530 		if (ATACH_ST(tfd) & WDCS_ERR) {
531 			if (ATACH_ST(tfd) == WDCE_ABRT) {
532 				/*
533 				 * Some ATAPI drives reject PIO settings.
534 				 * Fall back to PIO mode 3 since that's the
535 				 * minimum for ATAPI.
536 				 */
537 				printf("%s:%d:%d: PIO mode %d rejected, "
538 				    "falling back to PIO mode 3\n",
539 				    device_xname(atac->atac_dev),
540 				    chp->ch_channel, xfer->c_drive,
541 				    drvp->PIO_mode);
542 				if (drvp->PIO_mode > 3)
543 					drvp->PIO_mode = 3;
544 			} else
545 				goto error;
546 		}
547 #if NATA_DMA
548 #if NATA_UDMA
549 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
550 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
551 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
552 		} else
553 #endif
554 		if (drvp->drive_flags & ATA_DRIVE_DMA) {
555 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
556 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
557 		} else {
558 			goto ready;
559 		}
560 		errstring = "dmamode";
561 		if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags,
562 		    &tfd))
563 			goto timeout;
564 		if (ATACH_ST(tfd) & WDCS_ERR) {
565 			if (ATACH_ERR(tfd) == WDCE_ABRT) {
566 #if NATA_UDMA
567 				if (drvp->drive_flags & ATA_DRIVE_UDMA)
568 					goto error;
569 				else
570 #endif
571 				{
572 					/*
573 					 * The drive rejected our DMA setting.
574 					 * Fall back to mode 1.
575 					 */
576 					printf("%s:%d:%d: DMA mode %d rejected, "
577 					    "falling back to DMA mode 0\n",
578 					    device_xname(atac->atac_dev),
579 					    chp->ch_channel, xfer->c_drive,
580 					    drvp->DMA_mode);
581 					if (drvp->DMA_mode > 0)
582 						drvp->DMA_mode = 0;
583 				}
584 			} else
585 				goto error;
586 		}
587 #endif	/* NATA_DMA */
588 ready:
589 		drvp->state = READY;
590 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
591 		    WDCTL_4BIT);
592 		delay(10); /* some drives need a little delay here */
593 	}
594 	/* start timeout machinery */
595 	if ((sc_xfer->xs_control & XS_CTL_POLL) == 0)
596 		callout_reset(&chp->c_timo_callout, mstohz(sc_xfer->timeout),
597 		    wdctimeout, chp);
598 
599 	if (wdc->select)
600 		wdc->select(chp, xfer->c_drive);
601 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
602 	    WDSD_IBM | (xfer->c_drive << 4));
603 	switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd)) {
604 	case WDCWAIT_OK:
605 		break;
606 	case WDCWAIT_TOUT:
607 		printf("wdc_atapi_start: not ready, st = %02x\n",
608 		    ATACH_ST(tfd));
609 		sc_xfer->error = XS_TIMEOUT;
610 		return ATASTART_ABORT;
611 	case WDCWAIT_THR:
612 		return ATASTART_TH;
613 	}
614 
615 	/*
616 	 * Even with WDCS_ERR, the device should accept a command packet
617 	 * Limit length to what can be stuffed into the cylinder register
618 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
619 	 * but not all devices do that and it's not obvious from the
620 	 * ATAPI spec that that behaviour should be expected.  If more
621 	 * data is necessary, multiple data transfer phases will be done.
622 	 */
623 
624 	wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD,
625 	    xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff,
626 	    0, 0, 0,
627 #if NATA_DMA
628 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA :
629 #endif
630 	    0
631 	    );
632 
633 #if NATA_PIOBM
634 	if (xfer->c_flags & C_PIOBM) {
635 		int error;
636 		int dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
637 		    ?  WDC_DMA_READ : 0;
638 		if (xfer->c_flags & C_POLL) {
639 			/* XXX not supported yet --- fall back to PIO */
640 			xfer->c_flags &= ~C_PIOBM;
641 		} else {
642 			/* Init the DMA channel. */
643 			error = (*wdc->dma_init)(wdc->dma_arg,
644 			    chp->ch_channel, xfer->c_drive,
645 			    (char *)xfer->c_databuf,
646 			    xfer->c_bcount,
647 			    dma_flags | WDC_DMA_PIOBM_ATAPI);
648 			if (error) {
649 				if (error == EINVAL) {
650 					/*
651 					 * We can't do DMA on this transfer
652 					 * for some reason.  Fall back to
653 					 * PIO.
654 					 */
655 					xfer->c_flags &= ~C_PIOBM;
656 					error = 0;
657 				} else {
658 					sc_xfer->error = XS_DRIVER_STUFFUP;
659 					errstring = "piobm";
660 					goto error;
661 				}
662 			}
663 		}
664 	}
665 #endif
666 	/*
667 	 * If there is no interrupt for CMD input, busy-wait for it (done in
668 	 * the interrupt routine. Poll routine will exit early in this case.
669 	 */
670 	if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) !=
671 	    ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL))
672 		return ATASTART_POLL;
673 	else {
674 		chp->ch_flags |= ATACH_IRQ_WAIT;
675 		return ATASTART_STARTED;
676 	}
677 
678 timeout:
679 	printf("%s:%d:%d: %s timed out\n",
680 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
681 	    errstring);
682 	sc_xfer->error = XS_TIMEOUT;
683 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
684 	delay(10); /* some drives need a little delay here */
685 	return ATASTART_ABORT;
686 
687 error:
688 	printf("%s:%d:%d: %s ",
689 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
690 	    errstring);
691 	printf("error (0x%x)\n", ATACH_ERR(tfd));
692 	sc_xfer->error = XS_SHORTSENSE;
693 	sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
694 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
695 	delay(10); /* some drives need a little delay here */
696 	return ATASTART_ABORT;
697 }
698 
699 static void
700 wdc_atapi_poll(struct ata_channel *chp, struct ata_xfer *xfer)
701 {
702 	/*
703 	 * If there is no interrupt for CMD input, busy-wait for it (done in
704 	 * the interrupt routine. If it is a polled command, call the interrupt
705 	 * routine until command is done.
706 	 */
707 	const bool poll = ((xfer->c_scsipi->xs_control & XS_CTL_POLL) != 0);
708 
709 	/* Wait for at last 400ns for status bit to be valid */
710 	DELAY(1);
711 	wdc_atapi_intr(chp, xfer, 0);
712 
713 	if (!poll)
714 		return;
715 
716 #if NATA_DMA
717 	if (chp->ch_flags & ATACH_DMA_WAIT) {
718 		wdc_dmawait(chp, xfer, xfer->c_scsipi->timeout);
719 		chp->ch_flags &= ~ATACH_DMA_WAIT;
720 	}
721 #endif
722 	while ((xfer->c_scsipi->xs_status & XS_STS_DONE) == 0) {
723 		/* Wait for at last 400ns for status bit to be valid */
724 		DELAY(1);
725 		wdc_atapi_intr(chp, xfer, 0);
726 	}
727 }
728 
729 static int
730 wdc_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
731 {
732 	struct atac_softc *atac = chp->ch_atac;
733 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
734 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
735 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
736 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
737 	int len, phase, i, retries=0;
738 	int ire, tfd;
739 #if NATA_DMA
740 	int error;
741 #endif
742 #if NATA_DMA || NATA_PIOBM
743 	int dma_flags = 0;
744 #endif
745 	void *cmd;
746 
747 	ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
748 	    device_xname(atac->atac_dev), chp->ch_channel, drvp->drive),
749 	    DEBUG_INTR);
750 
751 	ata_channel_lock(chp);
752 
753 	/* Is it not a transfer, but a control operation? */
754 	if (drvp->state < READY) {
755 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
756 		    device_xname(atac->atac_dev), chp->ch_channel,
757 		    xfer->c_drive, drvp->state);
758 		panic("wdc_atapi_intr: bad state");
759 	}
760 	/*
761 	 * If we missed an interrupt in a PIO transfer, reset and restart.
762 	 * Don't try to continue transfer, we may have missed cycles.
763 	 */
764 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
765 		ata_channel_unlock(chp);
766 		sc_xfer->error = XS_TIMEOUT;
767 		wdc_atapi_reset(chp, xfer);
768 		return 1;
769 	}
770 
771 #if NATA_PIOBM
772 	/* Transfer-done interrupt for busmastering PIO operation */
773 	if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
774 		chp->ch_flags &= ~ATACH_PIOBM_WAIT;
775 
776 		/* restore transfer length */
777 		len = xfer->c_bcount;
778 		if (xfer->c_atapi.c_lenoff < 0)
779 			len += xfer->c_atapi.c_lenoff;
780 
781 		if (sc_xfer->xs_control & XS_CTL_DATA_IN)
782 			goto end_piobm_datain;
783 		else
784 			goto end_piobm_dataout;
785 	}
786 #endif
787 
788 	/* Ack interrupt done in wdc_wait_for_unbusy */
789 	if (wdc->select)
790 		wdc->select(chp, xfer->c_drive);
791 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
792 	    WDSD_IBM | (xfer->c_drive << 4));
793 	if (wdc_wait_for_unbusy(chp,
794 	    (irq == 0) ? sc_xfer->timeout : 0, AT_POLL, &tfd) == WDCWAIT_TOUT) {
795 		if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
796 			ata_channel_unlock(chp);
797 			return 0; /* IRQ was not for us */
798 		}
799 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n",
800 		    device_xname(atac->atac_dev), chp->ch_channel,
801 		    xfer->c_drive, xfer->c_bcount, xfer->c_skip);
802 #if NATA_DMA
803 		if (xfer->c_flags & C_DMA) {
804 			ata_dmaerr(drvp,
805 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
806 		}
807 #endif
808 		sc_xfer->error = XS_TIMEOUT;
809 		ata_channel_unlock(chp);
810 		wdc_atapi_reset(chp, xfer);
811 		return 1;
812 	}
813 	if (wdc->irqack)
814 		wdc->irqack(chp);
815 
816 #if NATA_DMA
817 	/*
818 	 * If we missed an IRQ and were using DMA, flag it as a DMA error
819 	 * and reset device.
820 	 */
821 	if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) {
822 		ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
823 		sc_xfer->error = XS_RESET;
824 		ata_channel_unlock(chp);
825 		wdc_atapi_reset(chp, xfer);
826 		return (1);
827 	}
828 #endif
829 	/*
830 	 * if the request sense command was aborted, report the short sense
831 	 * previously recorded, else continue normal processing
832 	 */
833 
834 #if NATA_DMA || NATA_PIOBM
835 	if (xfer->c_flags & (C_DMA | C_PIOBM))
836 		dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN)
837 		    ?  WDC_DMA_READ : 0;
838 #endif
839 again:
840 	len = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0) +
841 	    256 * bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 0);
842 	ire = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_ireason], 0);
843 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (ATACH_ST(tfd) & WDCS_DRQ);
844 	ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
845 	    "ire 0x%x :", xfer->c_bcount,
846 	    len, ATACH_ST(tfd), ATACH_ERR(tfd), ire), DEBUG_INTR);
847 
848 	switch (phase) {
849 	case PHASE_CMDOUT:
850 		cmd = sc_xfer->cmd;
851 		ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
852 #if NATA_DMA
853 		/* Init the DMA channel if necessary */
854 		if (xfer->c_flags & C_DMA) {
855 			error = (*wdc->dma_init)(wdc->dma_arg,
856 			    chp->ch_channel, xfer->c_drive,
857 			    xfer->c_databuf, xfer->c_bcount, dma_flags);
858 			if (error) {
859 				if (error == EINVAL) {
860 					/*
861 					 * We can't do DMA on this transfer
862 					 * for some reason.  Fall back to
863 					 * PIO.
864 					 */
865 					xfer->c_flags &= ~C_DMA;
866 					error = 0;
867 				} else {
868 					sc_xfer->error = XS_DRIVER_STUFFUP;
869 					break;
870 				}
871 			}
872 		}
873 #endif
874 
875 		/* send packet command */
876 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
877 		wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen);
878 
879 #if NATA_DMA
880 		/* Start the DMA channel if necessary */
881 		if (xfer->c_flags & C_DMA) {
882 			(*wdc->dma_start)(wdc->dma_arg,
883 			    chp->ch_channel, xfer->c_drive);
884 			chp->ch_flags |= ATACH_DMA_WAIT;
885 		}
886 #endif
887 
888 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
889 			chp->ch_flags |= ATACH_IRQ_WAIT;
890 		}
891 
892 		ata_channel_unlock(chp);
893 		return 1;
894 
895 	 case PHASE_DATAOUT:
896 		/* write data */
897 		ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
898 #if NATA_DMA
899 		if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 ||
900 		    (xfer->c_flags & C_DMA) != 0) {
901 			printf("wdc_atapi_intr: bad data phase DATAOUT\n");
902 			if (xfer->c_flags & C_DMA) {
903 				ata_dmaerr(drvp,
904 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
905 			}
906 			sc_xfer->error = XS_TIMEOUT;
907 			ata_channel_unlock(chp);
908 			wdc_atapi_reset(chp, xfer);
909 			return 1;
910 		}
911 #endif
912 		xfer->c_atapi.c_lenoff = len - xfer->c_bcount;
913 		if (xfer->c_bcount < len) {
914 			printf("wdc_atapi_intr: warning: write only "
915 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
916 			len = xfer->c_bcount;
917 		}
918 
919 #if NATA_PIOBM
920 		if (xfer->c_flags & C_PIOBM) {
921 			/* start the busmastering PIO */
922 			(*wdc->piobm_start)(wdc->dma_arg,
923 			    chp->ch_channel, xfer->c_drive,
924 			    xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
925 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
926 			    ATACH_PIOBM_WAIT;
927 			ata_channel_unlock(chp);
928 			return 1;
929 		}
930 #endif
931 		wdc->dataout_pio(chp, drvp->drive_flags,
932 		    (char *)xfer->c_databuf + xfer->c_skip, len);
933 
934 #if NATA_PIOBM
935 	end_piobm_dataout:
936 #endif
937 		for (i = xfer->c_atapi.c_lenoff; i > 0; i -= 2)
938 			bus_space_write_2(wdr->cmd_iot,
939 			    wdr->cmd_iohs[wd_data], 0, 0);
940 
941 		xfer->c_skip += len;
942 		xfer->c_bcount -= len;
943 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
944 			chp->ch_flags |= ATACH_IRQ_WAIT;
945 		}
946 		ata_channel_unlock(chp);
947 		return 1;
948 
949 	case PHASE_DATAIN:
950 		/* Read data */
951 		ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
952 #if NATA_DMA
953 		if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 ||
954 		    (xfer->c_flags & C_DMA) != 0) {
955 			printf("wdc_atapi_intr: bad data phase DATAIN\n");
956 			if (xfer->c_flags & C_DMA) {
957 				ata_dmaerr(drvp,
958 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
959 			}
960 			sc_xfer->error = XS_TIMEOUT;
961 			ata_channel_unlock(chp);
962 			wdc_atapi_reset(chp, xfer);
963 			return 1;
964 		}
965 #endif
966 		xfer->c_atapi.c_lenoff = len - xfer->c_bcount;
967 		if (xfer->c_bcount < len) {
968 			printf("wdc_atapi_intr: warning: reading only "
969 			    "%d of %d bytes\n", xfer->c_bcount, len);
970 			len = xfer->c_bcount;
971 		}
972 
973 #if NATA_PIOBM
974 		if (xfer->c_flags & C_PIOBM) {
975 			/* start the busmastering PIO */
976 			(*wdc->piobm_start)(wdc->dma_arg,
977 			    chp->ch_channel, xfer->c_drive,
978 			    xfer->c_skip, len, WDC_PIOBM_XFER_IRQ);
979 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT |
980 			    ATACH_PIOBM_WAIT;
981 			ata_channel_unlock(chp);
982 			return 1;
983 		}
984 #endif
985 		wdc->datain_pio(chp, drvp->drive_flags,
986 		    (char *)xfer->c_databuf + xfer->c_skip, len);
987 
988 #if NATA_PIOBM
989 	end_piobm_datain:
990 #endif
991 		if (xfer->c_atapi.c_lenoff > 0)
992 			wdcbit_bucket(chp, xfer->c_atapi.c_lenoff);
993 
994 		xfer->c_skip += len;
995 		xfer->c_bcount -= len;
996 		if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) {
997 			chp->ch_flags |= ATACH_IRQ_WAIT;
998 		}
999 		ata_channel_unlock(chp);
1000 		return 1;
1001 
1002 	case PHASE_ABORTED:
1003 	case PHASE_COMPLETED:
1004 		ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
1005 #if NATA_DMA
1006 		if (xfer->c_flags & C_DMA) {
1007 			xfer->c_bcount -= sc_xfer->datalen;
1008 		}
1009 #endif
1010 		sc_xfer->resid = xfer->c_bcount;
1011 		/* this will unlock channel lock too */
1012 		wdc_atapi_phase_complete(xfer, tfd);
1013 		return(1);
1014 
1015 	default:
1016 		if (++retries<500) {
1017 			DELAY(100);
1018 			tfd = ATACH_ERR_ST(
1019 			    bus_space_read_1(wdr->cmd_iot,
1020 				wdr->cmd_iohs[wd_error], 0),
1021 			    bus_space_read_1(wdr->cmd_iot,
1022 				wdr->cmd_iohs[wd_status], 0)
1023 			);
1024 			goto again;
1025 		}
1026 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
1027 		if (ATACH_ST(tfd) & WDCS_ERR) {
1028 			sc_xfer->error = XS_SHORTSENSE;
1029 			sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
1030 		} else {
1031 #if NATA_DMA
1032 			if (xfer->c_flags & C_DMA) {
1033 				ata_dmaerr(drvp,
1034 				    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
1035 			}
1036 #endif
1037 			sc_xfer->error = XS_RESET;
1038 			ata_channel_unlock(chp);
1039 			wdc_atapi_reset(chp, xfer);
1040 			return (1);
1041 		}
1042 	}
1043 	ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
1044 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
1045 	    DEBUG_INTR);
1046 	ata_channel_unlock(chp);
1047 	wdc_atapi_done(chp, xfer);
1048 	return (1);
1049 }
1050 
1051 static void
1052 wdc_atapi_phase_complete(struct ata_xfer *xfer, int tfd)
1053 {
1054 	struct ata_channel *chp = xfer->c_chp;
1055 	struct atac_softc *atac = chp->ch_atac;
1056 #if NATA_DMA || NATA_PIOBM
1057 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1058 #endif
1059 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1060 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
1061 
1062 	ata_channel_lock_owned(chp);
1063 
1064 	/* wait for DSC if needed */
1065 	if (drvp->drive_flags & ATA_DRIVE_ATAPIDSCW) {
1066 		ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) "
1067 		    "polldsc %d\n", device_xname(atac->atac_dev),
1068 		    chp->ch_channel,
1069 		    xfer->c_drive, xfer->c_atapi.c_dscpoll), DEBUG_XFERS);
1070 #if 1
1071 		if (cold)
1072 			panic("wdc_atapi_phase_complete: cold");
1073 #endif
1074 		if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10,
1075 		    AT_POLL, &tfd) == WDCWAIT_TOUT) {
1076 			/* 10ms not enough, try again in 1 tick */
1077 			if (xfer->c_atapi.c_dscpoll++ >
1078 			    mstohz(sc_xfer->timeout)) {
1079 				printf("%s:%d:%d: wait_for_dsc "
1080 				    "failed\n",
1081 				    device_xname(atac->atac_dev),
1082 				    chp->ch_channel, xfer->c_drive);
1083 				ata_channel_unlock(chp);
1084 				sc_xfer->error = XS_TIMEOUT;
1085 				wdc_atapi_reset(chp, xfer);
1086 			} else {
1087 				callout_reset(&chp->c_timo_callout, 1,
1088 				    wdc_atapi_polldsc, chp);
1089 				ata_channel_unlock(chp);
1090 			}
1091 			return;
1092 		}
1093 	}
1094 
1095 	/*
1096 	 * Some drive occasionally set WDCS_ERR with
1097 	 * "ATA illegal length indication" in the error
1098 	 * register. If we read some data the sense is valid
1099 	 * anyway, so don't report the error.
1100 	 */
1101 	if (ATACH_ST(tfd) & WDCS_ERR &&
1102 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1103 	    sc_xfer->resid == sc_xfer->datalen)) {
1104 		/* save the short sense */
1105 		sc_xfer->error = XS_SHORTSENSE;
1106 		sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
1107 		if ((sc_xfer->xs_periph->periph_quirks &
1108 		    PQUIRK_NOSENSE) == 0) {
1109 			/* ask scsipi to send a REQUEST_SENSE */
1110 			sc_xfer->error = XS_BUSY;
1111 			sc_xfer->status = SCSI_CHECK;
1112 		}
1113 #if NATA_DMA || NATA_PIOBM
1114 		else if (wdc->dma_status &
1115 		    (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) {
1116 #if NATA_DMA
1117 			ata_dmaerr(drvp,
1118 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
1119 #endif
1120 			sc_xfer->error = XS_RESET;
1121 			ata_channel_unlock(chp);
1122 			wdc_atapi_reset(chp, xfer);
1123 			return;
1124 		}
1125 #endif
1126 	}
1127 	if (xfer->c_bcount != 0) {
1128 		ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is "
1129 		    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
1130 	}
1131 #ifdef DIAGNOSTIC
1132 	if (xfer->c_bcount < 0) {
1133 		printf("wdc_atapi_intr warning: bcount value "
1134 		    "is %d after io\n", xfer->c_bcount);
1135 	}
1136 #endif
1137 	ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), "
1138 	    "error 0x%x sense 0x%x\n", sc_xfer->error,
1139 	    sc_xfer->sense.atapi_sense), DEBUG_INTR);
1140 	ata_channel_unlock(chp);
1141 	wdc_atapi_done(chp, xfer);
1142 }
1143 
1144 static void
1145 wdc_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer)
1146 {
1147 	struct atac_softc *atac = chp->ch_atac;
1148 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1149 
1150 	ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
1151 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
1152 	    (u_int)xfer->c_flags), DEBUG_XFERS);
1153 
1154 	if (ata_waitdrain_xfer_check(chp, xfer))
1155 		return;
1156 
1157 	ata_deactivate_xfer(chp, xfer);
1158 	ata_free_xfer(chp, xfer);
1159 
1160 	ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
1161 	scsipi_done(sc_xfer);
1162 	ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n",
1163 	    chp->ch_flags), DEBUG_XFERS);
1164 	atastart(chp);
1165 }
1166 
1167 static void
1168 wdc_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer)
1169 {
1170 	struct atac_softc *atac = chp->ch_atac;
1171 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
1172 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1173 	int tfd;
1174 
1175 	ata_channel_lock(chp);
1176 	wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
1177 	drvp->state = 0;
1178 	if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL, &tfd) != 0) {
1179 		printf("%s:%d:%d: reset failed\n",
1180 		    device_xname(atac->atac_dev), chp->ch_channel,
1181 		    xfer->c_drive);
1182 		sc_xfer->error = XS_SELTIMEOUT;
1183 	}
1184 	ata_channel_unlock(chp);
1185 	wdc_atapi_done(chp, xfer);
1186 	return;
1187 }
1188 
1189 static void
1190 wdc_atapi_polldsc(void *arg)
1191 {
1192 	struct ata_channel *chp = arg;
1193 	struct ata_xfer *xfer = ata_queue_get_active_xfer(chp);
1194 
1195 	KASSERT(xfer != NULL);
1196 
1197 	ata_channel_lock(chp);
1198 
1199 	/* this will unlock channel lock too */
1200 	wdc_atapi_phase_complete(xfer, 0);
1201 }
1202