xref: /openbsd/sys/dev/atapiscsi/atapiscsi.c (revision cecf84d4)
1 /*      $OpenBSD: atapiscsi.c,v 1.103 2014/09/14 14:17:24 jsg Exp $     */
2 
3 /*
4  * This code is derived from code with the copyright below.
5  */
6 
7 /*
8  * Copyright (c) 1996, 1998 Manuel Bouyer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/buf.h>
39 #include <sys/disklabel.h>
40 #include <sys/malloc.h>
41 #include <sys/reboot.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/timeout.h>
45 #include <scsi/scsi_all.h>
46 #include <scsi/scsi_disk.h>
47 #include <scsi/scsi_tape.h>
48 #include <scsi/scsiconf.h>
49 
50 #include <machine/bus.h>
51 #include <machine/cpu.h>
52 #include <machine/intr.h>
53 
54 #include <dev/ata/atareg.h>
55 #include <dev/ata/atavar.h>
56 #include <dev/ic/wdcreg.h>
57 #include <dev/ic/wdcvar.h>
58 #include <dev/ic/wdcevent.h>
59 
60 /* drive states stored in ata_drive_datas */
61 enum atapi_drive_states {
62 	ATAPI_RESET_BASE_STATE = 0,
63 	ATAPI_DEVICE_RESET_WAIT_STATE = 1,
64 	ATAPI_IDENTIFY_STATE = 2,
65 	ATAPI_IDENTIFY_WAIT_STATE = 3,
66 	ATAPI_PIOMODE_STATE = 4,
67 	ATAPI_PIOMODE_WAIT_STATE = 5,
68 	ATAPI_DMAMODE_STATE = 6,
69 	ATAPI_DMAMODE_WAIT_STATE = 7,
70 	ATAPI_READY_STATE = 8
71 };
72 
73 #define DEBUG_INTR   0x01
74 #define DEBUG_XFERS  0x02
75 #define DEBUG_STATUS 0x04
76 #define DEBUG_FUNCS  0x08
77 #define DEBUG_PROBE  0x10
78 #define DEBUG_DSC    0x20
79 #define DEBUG_POLL   0x40
80 #define DEBUG_ERRORS 0x80   /* Debug error handling code */
81 
82 #if defined(WDCDEBUG)
83 #ifndef WDCDEBUG_ATAPI_MASK
84 #define WDCDEBUG_ATAPI_MASK 0x00
85 #endif
86 int wdcdebug_atapi_mask = WDCDEBUG_ATAPI_MASK;
87 #define WDCDEBUG_PRINT(args, level) do {		\
88 	if ((wdcdebug_atapi_mask & (level)) != 0)	\
89 		printf args;				\
90 } while (0)
91 #else
92 #define WDCDEBUG_PRINT(args, level)
93 #endif
94 
95 /* 10 ms, this is used only before sending a cmd.  */
96 #define ATAPI_DELAY 10
97 #define ATAPI_RESET_DELAY 1000
98 #define ATAPI_RESET_WAIT 2000
99 #define ATAPI_CTRL_WAIT 4000
100 
101 /* When polling, let the exponential backoff max out at 1 second's interval. */
102 #define ATAPI_POLL_MAXTIC (hz)
103 
104 void  wdc_atapi_start(struct channel_softc *,struct wdc_xfer *);
105 
106 void  wdc_atapi_timer_handler(void *);
107 
108 void  wdc_atapi_real_start(struct channel_softc *, struct wdc_xfer *,
109     int, struct atapi_return_args *);
110 void  wdc_atapi_real_start_2(struct channel_softc *, struct wdc_xfer *,
111     int, struct atapi_return_args *);
112 void  wdc_atapi_intr_command(struct channel_softc *, struct wdc_xfer *,
113     int, struct atapi_return_args *);
114 void  wdc_atapi_intr_data(struct channel_softc *, struct wdc_xfer *,
115     int, struct atapi_return_args *);
116 void  wdc_atapi_intr_complete(struct channel_softc *, struct wdc_xfer *,
117     int, struct atapi_return_args *);
118 void  wdc_atapi_pio_intr(struct channel_softc *, struct wdc_xfer *,
119     int, struct atapi_return_args *);
120 void  wdc_atapi_send_packet(struct channel_softc *, struct wdc_xfer *,
121     int, struct atapi_return_args *);
122 void  wdc_atapi_ctrl(struct channel_softc *, struct wdc_xfer *,
123     int, struct atapi_return_args *);
124 
125 char  *wdc_atapi_in_data_phase(struct wdc_xfer *, int, int);
126 
127 int   wdc_atapi_intr(struct channel_softc *, struct wdc_xfer *, int);
128 void  wdc_atapi_done(struct channel_softc *, struct wdc_xfer *,
129 	int, struct atapi_return_args *);
130 void  wdc_atapi_reset(struct channel_softc *, struct wdc_xfer *,
131 	int, struct atapi_return_args *);
132 void  wdc_atapi_reset_2(struct channel_softc *, struct wdc_xfer *,
133 	int, struct atapi_return_args *);
134 
135 void  wdc_atapi_tape_done(struct channel_softc *, struct wdc_xfer *,
136 	int, struct atapi_return_args *);
137 #define MAX_SIZE MAXPHYS
138 
139 struct atapiscsi_softc;
140 struct atapiscsi_xfer;
141 
142 int	atapiscsi_match(struct device *, void *, void *);
143 void	atapiscsi_attach(struct device *, struct device *, void *);
144 int	atapiscsi_activate(struct device *, int);
145 int	atapiscsi_detach(struct device *, int);
146 int     atapi_to_scsi_sense(struct scsi_xfer *, u_int8_t);
147 
148 enum atapi_state { as_none, as_data, as_completed };
149 
150 struct atapiscsi_softc {
151 	struct device  sc_dev;
152 	struct  scsi_link  sc_adapterlink;
153 	struct channel_softc *chp;
154 	enum atapi_state protocol_phase;
155 
156 	int drive;
157 };
158 
159 void  wdc_atapi_minphys(struct buf *bp, struct scsi_link *sl);
160 int   wdc_atapi_ioctl(struct scsi_link *, u_long, caddr_t, int);
161 void  wdc_atapi_send_cmd(struct scsi_xfer *sc_xfer);
162 
163 static struct scsi_adapter atapiscsi_switch =
164 {
165 	wdc_atapi_send_cmd,
166 	wdc_atapi_minphys,
167 	NULL,
168 	NULL,
169 	wdc_atapi_ioctl
170 };
171 
172 /* Inital version shares bus_link structure so it can easily
173    be "attached to current" wdc driver */
174 
175 struct cfattach atapiscsi_ca = {
176 	sizeof(struct atapiscsi_softc), atapiscsi_match, atapiscsi_attach,
177 	    atapiscsi_detach, atapiscsi_activate
178 };
179 
180 struct cfdriver atapiscsi_cd = {
181 	NULL, "atapiscsi", DV_DULL
182 };
183 
184 
185 int
186 atapiscsi_match(struct device *parent, void *match, void *aux)
187 {
188 	struct ata_atapi_attach *aa_link = aux;
189 	struct cfdata *cf = match;
190 
191 	if (aa_link == NULL)
192 		return (0);
193 
194 	if (aa_link->aa_type != T_ATAPI)
195 		return (0);
196 
197 	if (cf->cf_loc[0] != aa_link->aa_channel &&
198 	    cf->cf_loc[0] != -1)
199 		return (0);
200 
201 	return (1);
202 }
203 
204 void
205 atapiscsi_attach(struct device *parent, struct device *self, void *aux)
206 {
207 	struct atapiscsi_softc *as = (struct atapiscsi_softc *)self;
208 	struct ata_atapi_attach *aa_link = aux;
209 	struct scsibus_attach_args saa;
210 	struct ata_drive_datas *drvp = aa_link->aa_drv_data;
211 	struct channel_softc *chp = drvp->chnl_softc;
212 	struct ataparams *id = &drvp->id;
213 	struct device *child;
214 
215 	extern struct scsi_iopool wdc_xfer_iopool;
216 
217 	printf("\n");
218 
219 	/* Initialize shared data. */
220 	scsi_init();
221 
222 #ifdef WDCDEBUG
223 	if (chp->wdc->sc_dev.dv_cfdata->cf_flags & WDC_OPTION_PROBE_VERBOSE)
224 		wdcdebug_atapi_mask |= DEBUG_PROBE;
225 #endif
226 
227 	as->chp = chp;
228 	as->drive = drvp->drive;
229 	as->sc_adapterlink.adapter_softc = as;
230 	as->sc_adapterlink.adapter_target = 7;
231 	as->sc_adapterlink.adapter_buswidth = 2;
232 	as->sc_adapterlink.adapter = &atapiscsi_switch;
233 	as->sc_adapterlink.luns = 1;
234 	as->sc_adapterlink.openings = 1;
235 	as->sc_adapterlink.flags = SDEV_ATAPI;
236 	as->sc_adapterlink.pool = &wdc_xfer_iopool;
237 
238 	strlcpy(drvp->drive_name, as->sc_dev.dv_xname,
239 	    sizeof(drvp->drive_name));
240 	drvp->cf_flags = as->sc_dev.dv_cfdata->cf_flags;
241 
242 	wdc_probe_caps(drvp, id);
243 
244 	WDCDEBUG_PRINT(
245 		("general config %04x capabilities %04x ",
246 		    id->atap_config, id->atap_capabilities1),
247 		    DEBUG_PROBE);
248 
249 	if ((NERRS_MAX - 2) > 0)
250 		drvp->n_dmaerrs = NERRS_MAX - 2;
251 	else
252 		drvp->n_dmaerrs = 0;
253 	drvp->drive_flags |= DRIVE_DEVICE_RESET;
254 
255 	/* Tape drives do funny DSC stuff */
256 	if (ATAPI_CFG_TYPE(id->atap_config) ==
257 	    ATAPI_CFG_TYPE_SEQUENTIAL)
258 		drvp->atapi_cap |= ACAP_DSC;
259 
260 	if ((id->atap_config & ATAPI_CFG_CMD_MASK) ==
261 	    ATAPI_CFG_CMD_16)
262 		drvp->atapi_cap |= ACAP_LEN;
263 
264 	drvp->atapi_cap |=
265 	    (id->atap_config & ATAPI_CFG_DRQ_MASK);
266 
267 	WDCDEBUG_PRINT(("driver caps %04x\n", drvp->atapi_cap),
268 	    DEBUG_PROBE);
269 
270 	bzero(&saa, sizeof(saa));
271 	saa.saa_sc_link = &as->sc_adapterlink;
272 
273 	child = config_found((struct device *)as, &saa, scsiprint);
274 
275 	if (child != NULL) {
276 		struct scsibus_softc *scsi = (struct scsibus_softc *)child;
277 		struct scsi_link *link = scsi_get_link(scsi, 0, 0);
278 
279 		if (link) {
280 			strlcpy(drvp->drive_name,
281 			    ((struct device *)(link->device_softc))->dv_xname,
282 			    sizeof(drvp->drive_name));
283 
284 			wdc_print_caps(drvp);
285 		}
286 	}
287 
288 #ifdef WDCDEBUG
289 	if (chp->wdc->sc_dev.dv_cfdata->cf_flags & WDC_OPTION_PROBE_VERBOSE)
290 		wdcdebug_atapi_mask &= ~DEBUG_PROBE;
291 #endif
292 }
293 
294 int
295 atapiscsi_activate(struct device *self, int act)
296 {
297 	struct atapiscsi_softc *as = (void *)self;
298  	struct channel_softc *chp = as->chp;
299 	struct ata_drive_datas *drvp = &chp->ch_drive[as->drive];
300 
301 	switch (act) {
302 	case DVACT_SUSPEND:
303 		break;
304 	case DVACT_RESUME:
305 		/*
306 		 * Do two resets separated by a small delay. The
307 		 * first wakes the controller, the second resets
308 		 * the channel
309 		 */
310 		wdc_disable_intr(chp);
311 		wdc_reset_channel(drvp, 1);
312 		delay(10000);
313 		wdc_reset_channel(drvp, 0);
314 		wdc_enable_intr(chp);
315 		break;
316 	}
317 	return (0);
318 }
319 
320 int
321 atapiscsi_detach(struct device *dev, int flags)
322 {
323 	return (config_detach_children(dev, flags));
324 }
325 
326 void
327 wdc_atapi_send_cmd(struct scsi_xfer *sc_xfer)
328 {
329 	struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc;
330  	struct channel_softc *chp = as->chp;
331 	struct ata_drive_datas *drvp = &chp->ch_drive[as->drive];
332 	struct wdc_xfer *xfer;
333 	int s;
334 	int idx;
335 
336 	WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d start\n",
337 	    chp->wdc->sc_dev.dv_xname, chp->channel, as->drive), DEBUG_XFERS);
338 
339 	if (sc_xfer->sc_link->target != 0) {
340 		sc_xfer->error = XS_DRIVER_STUFFUP;
341 		scsi_done(sc_xfer);
342 		return;
343 	}
344 
345 	xfer = sc_xfer->io;
346 	wdc_scrub_xfer(xfer);
347 	if (sc_xfer->flags & SCSI_POLL)
348 		xfer->c_flags |= C_POLL;
349 	xfer->drive = as->drive;
350 	xfer->c_flags |= C_ATAPI;
351 	xfer->cmd = sc_xfer;
352 	xfer->databuf = sc_xfer->data;
353 	xfer->c_bcount = sc_xfer->datalen;
354 	xfer->c_start = wdc_atapi_start;
355 	xfer->c_intr = wdc_atapi_intr;
356 
357 	timeout_set(&xfer->atapi_poll_to, wdc_atapi_timer_handler, chp);
358 
359 	WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d ",
360 	    chp->wdc->sc_dev.dv_xname, chp->channel, as->drive),
361 	    DEBUG_XFERS | DEBUG_ERRORS);
362 
363 	for (idx = 0; idx < sc_xfer->cmdlen; idx++) {
364 		WDCDEBUG_PRINT((" %02x",
365 				   ((unsigned char *)sc_xfer->cmd)[idx]),
366 		    DEBUG_XFERS | DEBUG_ERRORS);
367 	}
368 	WDCDEBUG_PRINT(("\n"), DEBUG_XFERS | DEBUG_ERRORS);
369 
370 	s = splbio();
371 
372 	if (drvp->atapi_cap & ACAP_DSC) {
373 		WDCDEBUG_PRINT(("about to send cmd 0x%x ",
374 		    sc_xfer->cmd->opcode), DEBUG_DSC);
375 		switch (sc_xfer->cmd->opcode) {
376 		case READ:
377 		case WRITE:
378 			xfer->c_flags |= C_MEDIA_ACCESS;
379 
380 			/* If we are not in buffer availability mode,
381 			   we limit the first request to 0 bytes, which
382 			   gets us into buffer availability mode without
383 			   holding the bus.  */
384 			if (!(drvp->drive_flags & DRIVE_DSCBA)) {
385 				xfer->c_bcount = 0;
386 				xfer->transfer_len =
387 				  _3btol(((struct scsi_rw_tape *)
388 					  sc_xfer->cmd)->len);
389 				_lto3b(0,
390 				    ((struct scsi_rw_tape *)
391 				    sc_xfer->cmd)->len);
392 				xfer->c_done = wdc_atapi_tape_done;
393 				WDCDEBUG_PRINT(
394 				    ("R/W in completion mode, do 0 blocks\n"),
395 				    DEBUG_DSC);
396 			} else
397 				WDCDEBUG_PRINT(("R/W %d blocks %d bytes\n",
398 				    _3btol(((struct scsi_rw_tape *)
399 					sc_xfer->cmd)->len),
400 				    sc_xfer->datalen),
401 				    DEBUG_DSC);
402 
403 			/* DSC will change to buffer availability mode.
404 			   We reflect this in wdc_atapi_intr.  */
405 			break;
406 
407 		case ERASE:		/* Media access commands */
408 		case LOAD:
409 		case REWIND:
410 		case SPACE:
411 		case WRITE_FILEMARKS:
412 #if 0
413 		case LOCATE:
414 		case READ_POSITION:
415 #endif
416 
417 			xfer->c_flags |= C_MEDIA_ACCESS;
418 			break;
419 
420 		default:
421 			WDCDEBUG_PRINT(("no media access\n"), DEBUG_DSC);
422 		}
423 	}
424 
425 	wdc_exec_xfer(chp, xfer);
426 	splx(s);
427 }
428 
429 void
430 wdc_atapi_minphys (struct buf *bp, struct scsi_link *sl)
431 {
432 	if (bp->b_bcount > MAX_SIZE)
433 		bp->b_bcount = MAX_SIZE;
434 	minphys(bp);
435 }
436 
437 int
438 wdc_atapi_ioctl (struct scsi_link *sc_link, u_long cmd, caddr_t addr, int flag)
439 {
440 	struct atapiscsi_softc *as = sc_link->adapter_softc;
441 	struct channel_softc *chp = as->chp;
442 	struct ata_drive_datas *drvp = &chp->ch_drive[as->drive];
443 
444 	if (sc_link->target != 0)
445 		return ENOTTY;
446 
447 	return (wdc_ioctl(drvp, cmd, addr, flag, curproc));
448 }
449 
450 
451 /*
452  * Returns 1 if we experienced an ATA-level abort command
453  *           (ABRT bit set but no additional sense)
454  *         0 if normal command processing
455  */
456 int
457 atapi_to_scsi_sense(struct scsi_xfer *xfer, u_int8_t flags)
458 {
459 	struct scsi_sense_data *sense = &xfer->sense;
460 	int ret = 0;
461 
462 	xfer->error = XS_SHORTSENSE;
463 
464 	sense->error_code = SSD_ERRCODE_VALID | SSD_ERRCODE_CURRENT;
465 	sense->flags = (flags >> 4);
466 
467 	WDCDEBUG_PRINT(("Atapi error: %d ", (flags >> 4)), DEBUG_ERRORS);
468 
469 	if ((flags & 4) && (sense->flags == 0)) {
470 		sense->flags = SKEY_ABORTED_COMMAND;
471 		WDCDEBUG_PRINT(("ABRT "), DEBUG_ERRORS);
472 		ret = 1;
473 	}
474 
475 	if (flags & 0x1) {
476 		sense->flags |= SSD_ILI;
477 		WDCDEBUG_PRINT(("ILI "), DEBUG_ERRORS);
478 	}
479 
480 	if (flags & 0x2) {
481 		sense->flags |= SSD_EOM;
482 		WDCDEBUG_PRINT(("EOM "), DEBUG_ERRORS);
483 	}
484 
485 	/* Media change requested */
486 	/* Let's ignore these in version 1 */
487 	if (flags & 0x8) {
488 		WDCDEBUG_PRINT(("MCR "), DEBUG_ERRORS);
489 		if (sense->flags == 0)
490 			xfer->error = XS_NOERROR;
491 	}
492 
493 	WDCDEBUG_PRINT(("\n"), DEBUG_ERRORS);
494 	return (ret);
495 }
496 
497 int wdc_atapi_drive_selected(struct channel_softc *, int);
498 
499 int
500 wdc_atapi_drive_selected(struct channel_softc *chp, int drive)
501 {
502 	u_int8_t reg = CHP_READ_REG(chp, wdr_sdh);
503 
504 	WDC_LOG_REG(chp, wdr_sdh, reg);
505 
506 	return ((reg & 0x10) == (drive << 4));
507 }
508 
509 enum atapi_context {
510 	ctxt_process = 0,
511 	ctxt_timer = 1,
512 	ctxt_interrupt = 2
513 };
514 
515 void wdc_atapi_the_machine(struct channel_softc *, struct wdc_xfer *,
516     enum atapi_context);
517 
518 void wdc_atapi_the_poll_machine(struct channel_softc *, struct wdc_xfer *);
519 
520 void
521 wdc_atapi_start(struct channel_softc *chp, struct wdc_xfer *xfer)
522 {
523 	xfer->next = wdc_atapi_real_start;
524 
525 	wdc_atapi_the_machine(chp, xfer, ctxt_process);
526 }
527 
528 
529 void
530 wdc_atapi_timer_handler(void *arg)
531 {
532 	struct channel_softc *chp = arg;
533 	struct wdc_xfer *xfer;
534 	int s;
535 
536 	s = splbio();
537 	xfer = TAILQ_FIRST(&chp->ch_queue->sc_xfer);
538 	if (xfer == NULL ||
539 	    !timeout_triggered(&xfer->atapi_poll_to)) {
540 		splx(s);
541 		return;
542 	}
543 	xfer->c_flags &= ~C_POLL_MACHINE;
544 	timeout_del(&xfer->atapi_poll_to);
545 	chp->ch_flags &= ~WDCF_IRQ_WAIT;
546 	wdc_atapi_the_machine(chp, xfer, ctxt_timer);
547 	splx(s);
548 }
549 
550 
551 int
552 wdc_atapi_intr(struct channel_softc *chp, struct wdc_xfer *xfer, int irq)
553 {
554 	timeout_del(&chp->ch_timo);
555 
556 	/* XXX we should consider an alternate signaling regime here */
557 	if (xfer->c_flags & C_TIMEOU) {
558 		xfer->c_flags &= ~C_TIMEOU;
559 		wdc_atapi_the_machine(chp, xfer, ctxt_timer);
560 		return (0);
561 	}
562 
563 	wdc_atapi_the_machine(chp, xfer, ctxt_interrupt);
564 
565 	return (-1);
566 }
567 
568 struct atapi_return_args {
569 	int timeout;
570 	int delay;
571 	int expect_irq;
572 };
573 
574 #define ARGS_INIT {-1, 0, 0}
575 
576 void
577 wdc_atapi_the_poll_machine(struct channel_softc *chp, struct wdc_xfer *xfer)
578 {
579 	int  idx = 0;
580 	int  current_timeout = 10;
581 
582 
583 	while (1) {
584 		struct atapi_return_args retargs = ARGS_INIT;
585 		idx++;
586 
587 		(xfer->next)(chp, xfer, (current_timeout * 1000 <= idx),
588 		    &retargs);
589 
590 		if (xfer->next == NULL) {
591 			wdc_free_xfer(chp, xfer);
592 			wdcstart(chp);
593 			return;
594 		}
595 
596 		if (retargs.timeout != -1) {
597 			current_timeout = retargs.timeout;
598 			idx = 0;
599 		}
600 
601 		if (retargs.delay != 0) {
602 			delay (1000 * retargs.delay);
603 			idx += 1000 * retargs.delay;
604 		}
605 
606 		DELAY(1);
607 	}
608 }
609 
610 
611 void
612 wdc_atapi_the_machine(struct channel_softc *chp, struct wdc_xfer *xfer,
613     enum atapi_context ctxt)
614 {
615 	int idx = 0;
616 	extern int ticks;
617 	int timeout_delay = hz / 10;
618 
619 	if (xfer->c_flags & C_POLL) {
620 		wdc_disable_intr(chp);
621 
622 		if (ctxt != ctxt_process) {
623 			if (ctxt == ctxt_interrupt)
624 				xfer->endticks = 1;
625 
626 			return;
627 		}
628 
629 		wdc_atapi_the_poll_machine(chp, xfer);
630 		return;
631 	}
632 
633 	/* Don't go through more than 50 state machine steps
634 	   before yielding. This tries to limit the amount of time
635 	   spent at high SPL */
636 	for (idx = 0; idx < 50; idx++) {
637 		struct atapi_return_args retargs = ARGS_INIT;
638 
639 		(xfer->next)(chp, xfer,
640 		    xfer->endticks && (ticks - xfer->endticks >= 0),
641 		    &retargs);
642 
643 		if (retargs.timeout != -1)
644 			/*
645 			 * Add 1 tick to compensate for the fact that we
646 			 * can be just microseconds before the tick changes.
647 			 */
648 			xfer->endticks =
649 			    max((retargs.timeout * hz) / 1000, 1) + 1 + ticks;
650 
651 		if (xfer->next == NULL) {
652 			if (xfer->c_flags & C_POLL_MACHINE)
653 				timeout_del(&xfer->atapi_poll_to);
654 
655 			wdc_free_xfer(chp, xfer);
656 			wdcstart(chp);
657 
658 			return;
659 		}
660 
661 		if (retargs.expect_irq) {
662 			int timeout_period;
663 			chp->ch_flags |= WDCF_IRQ_WAIT;
664 			timeout_period =  xfer->endticks - ticks;
665 			if (timeout_period < 1)
666 				timeout_period = 1;
667 			timeout_add(&chp->ch_timo, timeout_period);
668 			return;
669 		}
670 
671 		if (retargs.delay != 0) {
672 			timeout_delay = max(retargs.delay * hz / 1000, 1);
673 			break;
674 		}
675 
676 		DELAY(1);
677 	}
678 
679 	timeout_add(&xfer->atapi_poll_to, timeout_delay);
680 	xfer->c_flags |= C_POLL_MACHINE;
681 
682 	return;
683 }
684 
685 
686 void wdc_atapi_update_status(struct channel_softc *);
687 
688 void
689 wdc_atapi_update_status(struct channel_softc *chp)
690 {
691 	chp->ch_status = CHP_READ_REG(chp, wdr_status);
692 
693 	WDC_LOG_STATUS(chp, chp->ch_status);
694 
695 	if (chp->ch_status == 0xff && (chp->ch_flags & WDCF_ONESLAVE)) {
696 		wdc_set_drive(chp, 1);
697 
698 		chp->ch_status = CHP_READ_REG(chp, wdr_status);
699 		WDC_LOG_STATUS(chp, chp->ch_status);
700 	}
701 
702 	if ((chp->ch_status & (WDCS_BSY | WDCS_ERR)) == WDCS_ERR) {
703 		chp->ch_error = CHP_READ_REG(chp, wdr_error);
704 		WDC_LOG_ERROR(chp, chp->ch_error);
705 	}
706 }
707 
708 void
709 wdc_atapi_real_start(struct channel_softc *chp, struct wdc_xfer *xfer,
710     int timeout, struct atapi_return_args *ret)
711 {
712 #ifdef WDCDEBUG
713 	struct scsi_xfer *sc_xfer = xfer->cmd;
714 #endif
715 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
716 
717 	/*
718 	 * Only set the DMA flag if the transfer is reasonably large.
719 	 * At least one older drive failed to complete a 4 byte DMA transfer.
720 	 */
721 
722 	/* Turn off DMA flag on REQUEST SENSE */
723 
724 	if (!(xfer->c_flags & (C_POLL | C_SENSE | C_MEDIA_ACCESS)) &&
725 	    (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
726 	    (xfer->c_bcount > 100))
727 		xfer->c_flags |= C_DMA;
728 	else
729 		xfer->c_flags &= ~C_DMA;
730 
731 
732 	wdc_set_drive(chp, xfer->drive);
733 
734 	DELAY(1);
735 
736 	xfer->next = wdc_atapi_real_start_2;
737 	ret->timeout = ATAPI_DELAY;
738 
739 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x, "
740 	    "ATA flags 0x%x\n",
741 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
742 	    sc_xfer->flags, xfer->c_flags), DEBUG_XFERS);
743 
744 
745 	return;
746 }
747 
748 
749 void
750 wdc_atapi_real_start_2(struct channel_softc *chp, struct wdc_xfer *xfer,
751     int timeout, struct atapi_return_args *ret)
752 {
753 	struct scsi_xfer *sc_xfer = xfer->cmd;
754 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
755 
756 	if (timeout) {
757 		printf("wdc_atapi_start: not ready, st = %02x\n",
758 		    chp->ch_status);
759 
760 		sc_xfer->error = XS_TIMEOUT;
761 		xfer->next = wdc_atapi_reset;
762 		return;
763 	} else {
764 		wdc_atapi_update_status(chp);
765 
766 		if (chp->ch_status & (WDCS_BSY | WDCS_DRQ))
767 			return;
768 	}
769 
770 	/* Do control operations specially. */
771 	if (drvp->state < ATAPI_READY_STATE) {
772 		xfer->next = wdc_atapi_ctrl;
773 		return;
774 	}
775 
776 	xfer->next = wdc_atapi_send_packet;
777 	return;
778 }
779 
780 
781 void
782 wdc_atapi_send_packet(struct channel_softc *chp, struct wdc_xfer *xfer,
783     int timeout, struct atapi_return_args *ret)
784 {
785 	struct scsi_xfer *sc_xfer = xfer->cmd;
786 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
787 
788 	/*
789 	 * Even with WDCS_ERR, the device should accept a command packet.
790 	 * Limit length to what can be stuffed into the cylinder register
791 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
792 	 * but not all devices do that and it's not obvious from the
793 	 * ATAPI spec that this behaviour should be expected.  If more
794 	 * data is necessary, multiple data transfer phases will be done.
795 	 */
796 
797 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
798 	    xfer->c_bcount <= 0xfffe ? xfer->c_bcount : 0xfffe,
799 	    0, 0, 0,
800 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
801 
802 	if (xfer->c_flags & C_DMA)
803 		drvp->n_xfers++;
804 
805 	DELAY(1);
806 
807 	xfer->next = wdc_atapi_intr_command;
808 	ret->timeout = sc_xfer->timeout;
809 
810 	if ((drvp->atapi_cap & ATAPI_CFG_DRQ_MASK) == ATAPI_CFG_IRQ_DRQ) {
811 		/* We expect an IRQ to tell us of the next state */
812 		ret->expect_irq = 1;
813 	}
814 
815 	WDCDEBUG_PRINT(("wdc_atapi_send_packet %s:%d:%d command sent\n",
816 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive
817 	    ), DEBUG_XFERS);
818 	return;
819 }
820 
821 void
822 wdc_atapi_intr_command(struct channel_softc *chp, struct wdc_xfer *xfer,
823     int timeout, struct atapi_return_args *ret)
824 {
825 	struct scsi_xfer *sc_xfer = xfer->cmd;
826 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
827 	struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc;
828 	int i;
829 	u_int8_t cmd[16];
830 	struct scsi_sense *cmd_reqsense;
831 	int cmdlen = (drvp->atapi_cap & ACAP_LEN) ? 16 : 12;
832 	int dma_flags = ((sc_xfer->flags & SCSI_DATA_IN) ||
833 	    (xfer->c_flags & C_SENSE)) ?  WDC_DMA_READ : 0;
834 
835 	wdc_atapi_update_status(chp);
836 
837 	if ((chp->ch_status & WDCS_BSY) || !(chp->ch_status & WDCS_DRQ)) {
838 		if (timeout)
839 			goto timeout;
840 
841 		return;
842 	}
843 
844 	if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
845 		chp->wdc->irqack(chp);
846 
847 	bzero(cmd, sizeof(cmd));
848 
849 	if (xfer->c_flags & C_SENSE) {
850 		cmd_reqsense = (struct scsi_sense *)&cmd[0];
851 		cmd_reqsense->opcode = REQUEST_SENSE;
852 		cmd_reqsense->length = xfer->c_bcount;
853 	} else
854 		bcopy(sc_xfer->cmd, cmd, sc_xfer->cmdlen);
855 
856 	WDC_LOG_ATAPI_CMD(chp, xfer->drive, xfer->c_flags,
857 	    cmdlen, cmd);
858 
859 	for (i = 0; i < 12; i++)
860 		WDCDEBUG_PRINT(("%02x ", cmd[i]), DEBUG_INTR);
861 	WDCDEBUG_PRINT((": PHASE_CMDOUT\n"), DEBUG_INTR);
862 
863 	/* Init the DMA channel if necessary */
864 	if (xfer->c_flags & C_DMA) {
865 		if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
866 		    chp->channel, xfer->drive, xfer->databuf,
867 		    xfer->c_bcount, dma_flags) != 0) {
868 			sc_xfer->error = XS_DRIVER_STUFFUP;
869 
870 			xfer->next = wdc_atapi_reset;
871 			return;
872 		}
873 	}
874 
875 	wdc_output_bytes(drvp, cmd, cmdlen);
876 
877 	/* Start the DMA channel if necessary */
878 	if (xfer->c_flags & C_DMA) {
879 		(*chp->wdc->dma_start)(chp->wdc->dma_arg,
880 		    chp->channel, xfer->drive);
881 		xfer->next = wdc_atapi_intr_complete;
882 	} else {
883 		if (xfer->c_bcount == 0)
884 			as->protocol_phase = as_completed;
885 		else
886 			as->protocol_phase = as_data;
887 
888 		xfer->next = wdc_atapi_pio_intr;
889 	}
890 
891 	ret->expect_irq = 1;
892 
893 	/* If we read/write to a tape we will get into buffer
894 	   availability mode.  */
895 	if (drvp->atapi_cap & ACAP_DSC) {
896 		if ((sc_xfer->cmd->opcode == READ ||
897 		       sc_xfer->cmd->opcode == WRITE)) {
898 			drvp->drive_flags |= DRIVE_DSCBA;
899 			WDCDEBUG_PRINT(("set DSCBA\n"), DEBUG_DSC);
900 		} else if ((xfer->c_flags & C_MEDIA_ACCESS) &&
901 		    (drvp->drive_flags & DRIVE_DSCBA)) {
902 			/* Clause 3.2.4 of QIC-157 D.
903 
904 			   Any media access command other than read or
905 			   write will switch DSC back to completion
906 			   mode */
907 			drvp->drive_flags &= ~DRIVE_DSCBA;
908 			WDCDEBUG_PRINT(("clear DCSBA\n"), DEBUG_DSC);
909 		}
910 	}
911 
912 	return;
913 
914  timeout:
915 	printf ("%s:%d:%d: device timeout waiting to send SCSI packet\n",
916 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive);
917 
918 	sc_xfer->error = XS_TIMEOUT;
919 	xfer->next = wdc_atapi_reset;
920 	return;
921 }
922 
923 
924 char *
925 wdc_atapi_in_data_phase(struct wdc_xfer *xfer, int len, int ire)
926 {
927 	struct scsi_xfer *sc_xfer = xfer->cmd;
928 	struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc;
929 	char *message;
930 
931 	if (as->protocol_phase != as_data) {
932 		message = "unexpected data phase";
933 		goto unexpected_state;
934 	}
935 
936 	if (ire & WDCI_CMD) {
937 		message = "unexpectedly in command phase";
938 		goto unexpected_state;
939 	}
940 
941 	if (!(xfer->c_flags & C_SENSE)) {
942 		if (!(sc_xfer->flags & (SCSI_DATA_IN | SCSI_DATA_OUT))) {
943 			message = "data phase where none expected";
944 			goto unexpected_state;
945 		}
946 
947 		/* Make sure polarities match */
948 		if (((ire & WDCI_IN) == WDCI_IN) ==
949 		    ((sc_xfer->flags & SCSI_DATA_OUT) == SCSI_DATA_OUT)) {
950 			message = "data transfer direction disagreement";
951 			goto unexpected_state;
952 		}
953 	} else {
954 		if (!(ire & WDCI_IN)) {
955 			message = "data transfer direction disagreement during sense";
956 			goto unexpected_state;
957 		}
958 	}
959 
960 	if (len == 0) {
961 		message = "zero length transfer requested in data phase";
962 		goto unexpected_state;
963 	}
964 
965 
966 	return (0);
967 
968  unexpected_state:
969 
970 	return (message);
971 }
972 
973 void
974 wdc_atapi_intr_data(struct channel_softc *chp, struct wdc_xfer *xfer,
975     int timeout, struct atapi_return_args *ret)
976 {
977 	struct scsi_xfer *sc_xfer = xfer->cmd;
978 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
979 	int len, ire;
980 	char *message;
981 	int tohost;
982 
983 	len = (CHP_READ_REG(chp, wdr_cyl_hi) << 8) |
984 	    CHP_READ_REG(chp, wdr_cyl_lo);
985 	WDC_LOG_REG(chp, wdr_cyl_lo, len);
986 
987 	ire = CHP_READ_REG(chp, wdr_ireason);
988 	WDC_LOG_REG(chp, wdr_ireason, ire);
989 
990 	if ((message = wdc_atapi_in_data_phase(xfer, len, ire))) {
991 		/* The drive has dropped BSY before setting up the
992 		   registers correctly for DATA phase. This drive is
993 		   not compliant with ATA/ATAPI-4.
994 
995 		   Give the drive 100ms to get its house in order
996 		   before we try again.  */
997 		WDCDEBUG_PRINT(("wdc_atapi_intr: %s\n", message),
998 		    DEBUG_ERRORS);
999 
1000 		if (!timeout) {
1001 			ret->delay = 100;
1002 			return;
1003 		}
1004 	}
1005 
1006 	tohost = ((sc_xfer->flags & SCSI_DATA_IN) != 0 ||
1007 	    (xfer->c_flags & C_SENSE) != 0);
1008 
1009 	if (xfer->c_bcount >= len) {
1010 		WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d "
1011 		    "st 0x%b err 0x%x "
1012 		    "ire 0x%x\n", xfer->c_bcount,
1013 		    len, chp->ch_status, WDCS_BITS, chp->ch_error, ire),
1014 		    DEBUG_INTR);
1015 
1016 		/* Common case */
1017 		if (!tohost)
1018 			wdc_output_bytes(drvp, (u_int8_t *)xfer->databuf +
1019 			    xfer->c_skip, len);
1020 		else
1021 			wdc_input_bytes(drvp, (u_int8_t *)xfer->databuf +
1022 			    xfer->c_skip, len);
1023 
1024 		xfer->c_skip += len;
1025 		xfer->c_bcount -= len;
1026 	} else {
1027 		/* Exceptional case - drive want to transfer more
1028 		   data than we have buffer for */
1029 		if (!tohost) {
1030 			/* Wouldn't it be better to just abort here rather
1031 			   than to write random stuff to drive? */
1032 			printf("wdc_atapi_intr: warning: device requesting "
1033 			    "%d bytes, only %d left in buffer\n", len, xfer->c_bcount);
1034 
1035 			wdc_output_bytes(drvp, (u_int8_t *)xfer->databuf +
1036 			    xfer->c_skip, xfer->c_bcount);
1037 
1038 			CHP_WRITE_RAW_MULTI_2(chp, NULL,
1039 			    len - xfer->c_bcount);
1040 		} else {
1041 			printf("wdc_atapi_intr: warning: reading only "
1042 			    "%d of %d bytes\n", xfer->c_bcount, len);
1043 
1044 			wdc_input_bytes(drvp,
1045 			    (char *)xfer->databuf + xfer->c_skip,
1046 			    xfer->c_bcount);
1047 			wdcbit_bucket(chp, len - xfer->c_bcount);
1048 		}
1049 
1050 		xfer->c_skip += xfer->c_bcount;
1051 		xfer->c_bcount = 0;
1052 	}
1053 
1054 	ret->expect_irq = 1;
1055 	xfer->next = wdc_atapi_pio_intr;
1056 
1057 	return;
1058 }
1059 
1060 void
1061 wdc_atapi_intr_complete(struct channel_softc *chp, struct wdc_xfer *xfer,
1062     int timeout, struct atapi_return_args *ret)
1063 {
1064 	struct scsi_xfer *sc_xfer = xfer->cmd;
1065 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
1066 	struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc;
1067 
1068 	WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
1069 
1070 	if (xfer->c_flags & C_DMA) {
1071 		int retry;
1072 
1073 		if (timeout) {
1074 			sc_xfer->error = XS_TIMEOUT;
1075 			ata_dmaerr(drvp);
1076 
1077 			xfer->next = wdc_atapi_reset;
1078 			return;
1079 		}
1080 
1081 		for (retry = 5; retry > 0; retry--) {
1082 			wdc_atapi_update_status(chp);
1083 			if ((chp->ch_status & (WDCS_BSY | WDCS_DRQ)) == 0)
1084 				break;
1085 			DELAY(5);
1086 		}
1087 		if (retry == 0) {
1088 			ret->expect_irq = 1;
1089 			return;
1090 		}
1091 
1092 		chp->wdc->dma_status =
1093 		    (*chp->wdc->dma_finish)
1094 		    (chp->wdc->dma_arg, chp->channel,
1095 			xfer->drive, 1);
1096 
1097 		if (chp->wdc->dma_status & WDC_DMAST_UNDER)
1098 			xfer->c_bcount = 1;
1099 		else
1100 			xfer->c_bcount = 0;
1101 	}
1102 
1103 	as->protocol_phase = as_none;
1104 
1105 	if (xfer->c_flags & C_SENSE) {
1106 		if (chp->ch_status & WDCS_ERR) {
1107 			if (chp->ch_error & WDCE_ABRT) {
1108 				WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
1109 						"calling wdc_atapi_done()"
1110 					), DEBUG_INTR);
1111 				xfer->next = wdc_atapi_done;
1112 				return;
1113 			}
1114 
1115 			/*
1116 			 * request sense failed ! it's not supposed
1117  			 * to be possible
1118 			 */
1119 			sc_xfer->error = XS_SHORTSENSE;
1120 		} else if (xfer->c_bcount < sizeof(sc_xfer->sense)) {
1121 			/* use the sense we just read */
1122 			sc_xfer->error = XS_SENSE;
1123 		} else {
1124 			/*
1125 			 * command completed, but no data was read.
1126 			 * use the short sense we saved previously.
1127 			 */
1128 			sc_xfer->error = XS_SHORTSENSE;
1129 		}
1130 	} else {
1131 		sc_xfer->resid = xfer->c_bcount;
1132 		if (chp->ch_status & WDCS_ERR) {
1133 			if (!atapi_to_scsi_sense(sc_xfer, chp->ch_error) &&
1134 			    (sc_xfer->sc_link->quirks &
1135 			     ADEV_NOSENSE) == 0) {
1136 				/*
1137 				 * let the driver issue a
1138 				 * 'request sense'
1139 				 */
1140 				xfer->databuf = &sc_xfer->sense;
1141 				xfer->c_bcount = sizeof(sc_xfer->sense);
1142 				xfer->c_skip = 0;
1143 				xfer->c_done = NULL;
1144 				xfer->c_flags |= C_SENSE;
1145 				xfer->next = wdc_atapi_real_start;
1146 				return;
1147 			}
1148 		}
1149 	}
1150 
1151         if ((xfer->c_flags & C_DMA) &&
1152 	    (chp->wdc->dma_status & ~WDC_DMAST_UNDER)) {
1153 		ata_dmaerr(drvp);
1154 		sc_xfer->error = XS_RESET;
1155 
1156 		xfer->next = wdc_atapi_reset;
1157 		return;
1158 	}
1159 
1160 
1161 	if (xfer->c_bcount != 0) {
1162 		WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
1163 				"%d after io\n", xfer->c_bcount), DEBUG_XFERS);
1164 	}
1165 #ifdef DIAGNOSTIC
1166 	if (xfer->c_bcount < 0) {
1167 		printf("wdc_atapi_intr warning: bcount value "
1168 		       "is %d after io\n", xfer->c_bcount);
1169 	}
1170 #endif
1171 
1172 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
1173 			"\n", sc_xfer->error),
1174 		       DEBUG_INTR);
1175 
1176 
1177 	if (xfer->c_done)
1178 		xfer->next = xfer->c_done;
1179 	else
1180 		xfer->next = wdc_atapi_done;
1181 
1182 	return;
1183 }
1184 
1185 void
1186 wdc_atapi_pio_intr(struct channel_softc *chp, struct wdc_xfer *xfer,
1187     int timeout, struct atapi_return_args *ret)
1188 {
1189 	struct scsi_xfer *sc_xfer = xfer->cmd;
1190 	struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc;
1191 	u_int8_t ireason;
1192 
1193 	wdc_atapi_update_status(chp);
1194 
1195 	if (chp->ch_status & WDCS_BSY) {
1196 		if (timeout)
1197 			goto timeout;
1198 
1199 		return;
1200 	}
1201 
1202 	if (!wdc_atapi_drive_selected(chp, xfer->drive)) {
1203 		WDCDEBUG_PRINT(("wdc_atapi_intr_for_us: wrong drive selected\n"), DEBUG_INTR);
1204 		wdc_set_drive(chp, xfer->drive);
1205 		delay (1);
1206 
1207 		if (!timeout)
1208 			return;
1209 	}
1210 
1211 	if ((xfer->c_flags & C_MEDIA_ACCESS) &&
1212 	    !(chp->ch_status & (WDCS_DSC | WDCS_DRQ))) {
1213 		if (timeout)
1214 			goto timeout;
1215 
1216 		ret->delay = 100;
1217 		return;
1218 	}
1219 
1220 	if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
1221 		chp->wdc->irqack(chp);
1222 
1223 	ireason = CHP_READ_REG(chp, wdr_ireason);
1224 	WDC_LOG_REG(chp, wdr_ireason, ireason);
1225 
1226 	WDCDEBUG_PRINT(("Phase %d, (0x%b, 0x%x) ", as->protocol_phase,
1227 	    chp->ch_status, WDCS_BITS, ireason), DEBUG_INTR );
1228 
1229 	switch (as->protocol_phase) {
1230 	case as_data:
1231 		if ((chp->ch_status & WDCS_DRQ) ||
1232 		    (ireason & 3) != 3) {
1233 			if (timeout)
1234 				goto timeout;
1235 
1236 			wdc_atapi_intr_data(chp, xfer, timeout, ret);
1237 			return;
1238 		}
1239 
1240 	case as_completed:
1241 		if ((chp->ch_status & WDCS_DRQ) ||
1242 		    (ireason & 3) != 3) {
1243 			if (timeout)
1244 				goto timeout;
1245 
1246 			ret->delay = 100;
1247 			return;
1248 		}
1249 
1250 		wdc_atapi_intr_complete(chp, xfer, timeout, ret);
1251 		return;
1252 
1253 	default:
1254 		printf ("atapiscsi: Shouldn't get here\n");
1255 		sc_xfer->error = XS_DRIVER_STUFFUP;
1256 		xfer->next = wdc_atapi_reset;
1257 		return;
1258 	}
1259 
1260 	return;
1261 timeout:
1262 	ireason = CHP_READ_REG(chp, wdr_ireason);
1263 	WDC_LOG_REG(chp, wdr_ireason, ireason);
1264 
1265 	printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d, "
1266 	    "status=0x%b, ireason=0x%x\n",
1267 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
1268 	    xfer->c_bcount, xfer->c_skip, chp->ch_status, WDCS_BITS, ireason);
1269 
1270 	sc_xfer->error = XS_TIMEOUT;
1271 	xfer->next = wdc_atapi_reset;
1272 	return;
1273 }
1274 
1275 void
1276 wdc_atapi_ctrl(struct channel_softc *chp, struct wdc_xfer *xfer,
1277     int timeout, struct atapi_return_args *ret)
1278 {
1279 	struct scsi_xfer *sc_xfer = xfer->cmd;
1280 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
1281 	char *errstring = NULL;
1282 
1283  	wdc_atapi_update_status(chp);
1284 
1285 	if (!timeout) {
1286 		switch (drvp->state) {
1287 		case ATAPI_IDENTIFY_WAIT_STATE:
1288 			if (chp->ch_status & WDCS_BSY)
1289 				return;
1290 			break;
1291 		default:
1292 			if (chp->ch_status & (WDCS_BSY | WDCS_DRQ))
1293 				return;
1294 			break;
1295 		}
1296 	}
1297 
1298 	if (!wdc_atapi_drive_selected(chp, xfer->drive))
1299 	{
1300 		wdc_set_drive(chp, xfer->drive);
1301 		delay (1);
1302 	}
1303 
1304 	if (timeout) {
1305 		int trigger_timeout = 1;
1306 
1307 		switch (drvp->state) {
1308 		case ATAPI_DEVICE_RESET_WAIT_STATE:
1309 			errstring = "Device Reset Wait";
1310 			drvp->drive_flags &= ~DRIVE_DEVICE_RESET;
1311 			break;
1312 
1313 		case ATAPI_IDENTIFY_WAIT_STATE:
1314 			errstring = "Identify";
1315 			if (!(chp->ch_status & WDCS_BSY) &&
1316 			    (chp->ch_status & (WDCS_DRQ | WDCS_ERR)))
1317 				trigger_timeout = 0;
1318 
1319 			break;
1320 
1321 		case ATAPI_PIOMODE_STATE:
1322 			errstring = "Post-Identify";
1323 			if (!(chp->ch_status & (WDCS_BSY | WDCS_DRQ)))
1324 				trigger_timeout = 0;
1325 			break;
1326 
1327 		case ATAPI_PIOMODE_WAIT_STATE:
1328 			errstring = "PIOMODE";
1329 			if (chp->ch_status & (WDCS_BSY | WDCS_DRQ))
1330 				drvp->drive_flags &= ~DRIVE_MODE;
1331 			else
1332 				trigger_timeout = 0;
1333 			break;
1334 		case ATAPI_DMAMODE_WAIT_STATE:
1335 			errstring = "dmamode";
1336 			if (chp->ch_status & (WDCS_BSY | WDCS_DRQ))
1337 				drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1338 			else
1339 				trigger_timeout = 0;
1340 			break;
1341 
1342 		default:
1343 			errstring = "unknown state";
1344 			break;
1345 		}
1346 
1347 		if (trigger_timeout)
1348 			goto timeout;
1349 	}
1350 
1351 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
1352 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
1353 	    DEBUG_INTR | DEBUG_FUNCS);
1354 
1355 	switch (drvp->state) {
1356 		/* My ATAPI slave device likes to assert DASP-/PDIAG- until
1357 		   it is DEVICE RESET. This causes the LED to stay on.
1358 
1359 		   There is a trade-off here. This drive will cause any
1360 		   play-back or seeks happening to be interrupted.
1361 
1362 		   Note that the bus reset that triggered this state
1363 		   (which may have been caused by the other drive on
1364 		   the chain) need not interrupt this playback. It happens
1365 		   to on my Smart & Friendly CD burner.
1366 
1367 		   - csapuntz@
1368 		*/
1369 	case ATAPI_RESET_BASE_STATE:
1370 		if ((drvp->drive_flags & DRIVE_DEVICE_RESET) == 0) {
1371 			drvp->state = ATAPI_IDENTIFY_STATE;
1372 			break;
1373 		}
1374 
1375 		wdccommandshort(chp, drvp->drive, ATAPI_DEVICE_RESET);
1376 		drvp->state = ATAPI_DEVICE_RESET_WAIT_STATE;
1377 		ret->delay = ATAPI_RESET_DELAY;
1378 		ret->timeout = ATAPI_RESET_WAIT;
1379 		break;
1380 
1381 	case ATAPI_DEVICE_RESET_WAIT_STATE:
1382 		/* FALLTHROUGH */
1383 
1384 	case ATAPI_IDENTIFY_STATE:
1385 		wdccommandshort(chp, drvp->drive, ATAPI_IDENTIFY_DEVICE);
1386 		drvp->state = ATAPI_IDENTIFY_WAIT_STATE;
1387 		ret->delay = 10;
1388 		ret->timeout = ATAPI_RESET_WAIT;
1389 		break;
1390 
1391 	case ATAPI_IDENTIFY_WAIT_STATE: {
1392 		int idx = 0;
1393 
1394 		while ((chp->ch_status & WDCS_DRQ) &&
1395 		    idx++ < 20) {
1396 			wdcbit_bucket(chp, 512);
1397 
1398 			DELAY(1);
1399 			wdc_atapi_update_status(chp);
1400 		}
1401 
1402 		drvp->state = ATAPI_PIOMODE_STATE;
1403 		/*
1404 		 * Note, we can't go directly to set PIO mode
1405 		 * because the drive is free to assert BSY
1406 		 * after the transfer
1407 		 */
1408 		break;
1409 	}
1410 
1411 	case ATAPI_PIOMODE_STATE:
1412 		/* Don't try to set mode if controller can't be adjusted */
1413 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
1414 			goto ready;
1415 		/* Also don't try if the drive didn't report its mode */
1416 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
1417 			goto ready;
1418 		/* SET FEATURES 0x08 is only for PIO mode > 2 */
1419 		if (drvp->PIO_mode <= 2)
1420 			goto ready;
1421 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
1422 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
1423 		drvp->state = ATAPI_PIOMODE_WAIT_STATE;
1424 		ret->timeout = ATAPI_CTRL_WAIT;
1425 		ret->expect_irq = 1;
1426 		break;
1427 	case ATAPI_PIOMODE_WAIT_STATE:
1428 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
1429 			chp->wdc->irqack(chp);
1430 		if (chp->ch_status & WDCS_ERR) {
1431 			/* Downgrade straight to PIO mode 3 */
1432 			drvp->PIO_mode = 3;
1433 			chp->wdc->set_modes(chp);
1434 		}
1435 	/* FALLTHROUGH */
1436 
1437 	case ATAPI_DMAMODE_STATE:
1438 		if (drvp->drive_flags & DRIVE_UDMA) {
1439 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
1440 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
1441 		} else if (drvp->drive_flags & DRIVE_DMA) {
1442 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
1443 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
1444 		} else {
1445 			goto ready;
1446 		}
1447 		drvp->state = ATAPI_DMAMODE_WAIT_STATE;
1448 
1449 		ret->timeout = ATAPI_CTRL_WAIT;
1450 		ret->expect_irq = 1;
1451 		break;
1452 
1453 	case ATAPI_DMAMODE_WAIT_STATE:
1454 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
1455 			chp->wdc->irqack(chp);
1456 		if (chp->ch_status & WDCS_ERR)
1457 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1458 	/* FALLTHROUGH */
1459 
1460 	case ATAPI_READY_STATE:
1461 	ready:
1462 		drvp->state = ATAPI_READY_STATE;
1463 		xfer->next = wdc_atapi_real_start;
1464 		break;
1465 	}
1466 	return;
1467 
1468 timeout:
1469 	printf("%s:%d:%d: %s timed out\n",
1470 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
1471 	sc_xfer->error = XS_TIMEOUT;
1472 	xfer->next = wdc_atapi_reset;
1473 	return;
1474 
1475 }
1476 
1477 void
1478 wdc_atapi_tape_done(struct channel_softc *chp, struct wdc_xfer *xfer,
1479     int timeout, struct atapi_return_args *ret)
1480 {
1481 	struct scsi_xfer *sc_xfer = xfer->cmd;
1482 
1483 	if (sc_xfer->error != XS_NOERROR) {
1484 		xfer->next = wdc_atapi_done;
1485 		return;
1486 	}
1487 
1488 	_lto3b(xfer->transfer_len,
1489 	    ((struct scsi_rw_tape *)
1490 		sc_xfer->cmd)->len);
1491 
1492 	xfer->c_bcount = sc_xfer->datalen;
1493 	xfer->c_done = NULL;
1494 	xfer->c_skip = 0;
1495 
1496 	xfer->next = wdc_atapi_real_start;
1497 	return;
1498 }
1499 
1500 
1501 void
1502 wdc_atapi_done(struct channel_softc *chp, struct wdc_xfer *xfer,
1503     int timeout, struct atapi_return_args *ret)
1504 {
1505 	struct scsi_xfer *sc_xfer = xfer->cmd;
1506 
1507 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x error 0x%x\n",
1508 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
1509 	    (u_int)xfer->c_flags, sc_xfer->error), DEBUG_XFERS);
1510 	WDC_LOG_ATAPI_DONE(chp, xfer->drive, xfer->c_flags, sc_xfer->error);
1511 
1512 	if (xfer->c_flags & C_POLL)
1513 		wdc_enable_intr(chp);
1514 
1515 	scsi_done(sc_xfer);
1516 
1517 	xfer->next = NULL;
1518 	return;
1519 }
1520 
1521 
1522 void
1523 wdc_atapi_reset(struct channel_softc *chp, struct wdc_xfer *xfer,
1524     int timeout, struct atapi_return_args *ret)
1525 {
1526 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
1527 
1528 	if (drvp->state == 0) {
1529 		xfer->next = wdc_atapi_done;
1530 		return;
1531 	}
1532 
1533 	WDCDEBUG_PRINT(("wdc_atapi_reset\n"), DEBUG_XFERS);
1534 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
1535 	drvp->state = ATAPI_IDENTIFY_STATE;
1536 
1537 	drvp->n_resets++;
1538 	/* Some ATAPI devices need extra time to find their
1539 	   brains after a reset
1540 	 */
1541 	xfer->next = wdc_atapi_reset_2;
1542 	ret->delay = ATAPI_RESET_DELAY;
1543 	ret->timeout = ATAPI_RESET_WAIT;
1544 	return;
1545 }
1546 
1547 void
1548 wdc_atapi_reset_2(struct channel_softc *chp, struct wdc_xfer *xfer,
1549     int timeout, struct atapi_return_args *ret)
1550 {
1551 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
1552 	struct scsi_xfer *sc_xfer = xfer->cmd;
1553 
1554 	if (timeout) {
1555 		printf("%s:%d:%d: soft reset failed\n",
1556 		    chp->wdc->sc_dev.dv_xname, chp->channel,
1557 		    xfer->drive);
1558 		sc_xfer->error = XS_SELTIMEOUT;
1559 		wdc_reset_channel(drvp, 0);
1560 
1561 		xfer->next = wdc_atapi_done;
1562 		return;
1563 	}
1564 
1565 	wdc_atapi_update_status(chp);
1566 
1567 	if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) {
1568 		return;
1569 	}
1570 
1571 	xfer->next = wdc_atapi_done;
1572 	return;
1573 }
1574