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