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