xref: /netbsd-src/sys/dev/scsipi/atapi_wdc.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: atapi_wdc.c,v 1.14 1999/01/09 03:16:17 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #define WDCDEBUG
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/buf.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/syslog.h>
47 #include <sys/proc.h>
48 
49 #include <vm/vm.h>
50 
51 #include <machine/intr.h>
52 #include <machine/bus.h>
53 
54 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
55 #define    bus_space_write_multi_stream_2    bus_space_write_multi_2
56 #define    bus_space_write_multi_stream_4    bus_space_write_multi_4
57 #define    bus_space_read_multi_stream_2    bus_space_read_multi_2
58 #define    bus_space_read_multi_stream_4    bus_space_read_multi_4
59 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
60 
61 #include <dev/ata/atareg.h>
62 #include <dev/ata/atavar.h>
63 #include <dev/ic/wdcreg.h>
64 #include <dev/ic/wdcvar.h>
65 #include <dev/scsipi/scsipi_all.h>
66 #include <dev/scsipi/scsipiconf.h>
67 #include <dev/scsipi/atapiconf.h>
68 
69 #define DEBUG_INTR   0x01
70 #define DEBUG_XFERS  0x02
71 #define DEBUG_STATUS 0x04
72 #define DEBUG_FUNCS  0x08
73 #define DEBUG_PROBE  0x10
74 #ifdef WDCDEBUG
75 int wdcdebug_atapi_mask = 0;
76 #define WDCDEBUG_PRINT(args, level) \
77 	if (wdcdebug_atapi_mask & (level)) \
78 		printf args
79 #else
80 #define WDCDEBUG_PRINT(args, level)
81 #endif
82 
83 #define ATAPI_DELAY 10	/* 10 ms, this is used only before sending a cmd */
84 
85 void  wdc_atapi_minphys  __P((struct buf *bp));
86 void  wdc_atapi_start	__P((struct channel_softc *,struct wdc_xfer *));
87 int   wdc_atapi_intr	 __P((struct channel_softc *, struct wdc_xfer *));
88 int   wdc_atapi_ctrl	 __P((struct channel_softc *, struct wdc_xfer *));
89 void  wdc_atapi_done	 __P((struct channel_softc *, struct wdc_xfer *));
90 void  wdc_atapi_reset	 __P((struct channel_softc *, struct wdc_xfer *));
91 int   wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer));
92 
93 #define MAX_SIZE MAXPHYS
94 
95 void
96 wdc_atapibus_attach(chp)
97 	struct channel_softc *chp;
98 {
99 	struct wdc_softc *wdc = chp->wdc;
100 	int channel = chp->channel;
101 	struct ata_atapi_attach aa_link;
102 
103 	/*
104 	 * Fill in the adapter.
105 	 */
106 	wdc->sc_atapi_adapter.scsipi_cmd = wdc_atapi_send_cmd;
107 	wdc->sc_atapi_adapter.scsipi_minphys = wdc_atapi_minphys;
108 
109 	memset(&aa_link, 0, sizeof(struct ata_atapi_attach));
110 	aa_link.aa_type = T_ATAPI;
111 	aa_link.aa_channel = channel;
112 	aa_link.aa_openings = 1;
113 	aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */
114 	aa_link.aa_bus_private = &wdc->sc_atapi_adapter;
115 	(void)config_found(&wdc->sc_dev, (void *)&aa_link, atapi_print);
116 }
117 
118 void
119 wdc_atapi_minphys (struct buf *bp)
120 {
121 	if(bp->b_bcount > MAX_SIZE)
122 		bp->b_bcount = MAX_SIZE;
123 	minphys(bp);
124 }
125 
126 int
127 wdc_atapi_get_params(ab_link, drive, flags, id)
128 	struct scsipi_link *ab_link;
129 	u_int8_t drive;
130 	int flags;
131 	struct ataparams *id;
132 {
133 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
134 	struct channel_softc *chp =
135 	    wdc->channels[ab_link->scsipi_atapi.channel];
136 	struct wdc_command wdc_c;
137 
138 	/* if no ATAPI device detected at wdc attach time, skip */
139 	/*
140 	 * XXX this will break scsireprobe if this is of any interest for
141 	 * ATAPI devices one day.
142 	 */
143 	if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) {
144 		WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n",
145 		    drive), DEBUG_PROBE);
146 		return -1;
147 	}
148 	memset(&wdc_c, 0, sizeof(struct wdc_command));
149 	wdc_c.r_command = ATAPI_SOFT_RESET;
150 	wdc_c.r_st_bmask = 0;
151 	wdc_c.r_st_pmask = 0;
152 	wdc_c.flags = AT_POLL;
153 	wdc_c.timeout = WDC_RESET_WAIT;
154 	if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) {
155 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for"
156 		    " drive %s:%d:%d: driver failed\n",
157 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive);
158 		panic("wdc_atapi_get_params");
159 	}
160 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
161 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET "
162 		    "failed for drive %s:%d:%d: error 0x%x\n",
163 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
164 		    wdc_c.r_error), DEBUG_PROBE);
165 		return -1;
166 	}
167 	chp->ch_drive[drive].state = 0;
168 
169 	bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status);
170 
171 	/* Some ATAPI devices need a bit more time after software reset. */
172 	delay(5000);
173 	if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) {
174 		WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
175 		    "failed for drive %s:%d:%d: error 0x%x\n",
176 		    chp->wdc->sc_dev.dv_xname, chp->channel, drive,
177 		    wdc_c.r_error), DEBUG_PROBE);
178 		return -1;
179 	}
180 	return COMPLETE;
181 }
182 
183 int
184 wdc_atapi_send_cmd(sc_xfer)
185 	struct scsipi_xfer *sc_xfer;
186 {
187 	struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc;
188 	struct wdc_xfer *xfer;
189 	struct ata_drive_datas *drvp;
190 	int flags = sc_xfer->flags;
191 	int channel = sc_xfer->sc_link->scsipi_atapi.channel;
192 	int drive = sc_xfer->sc_link->scsipi_atapi.drive;
193 	int s, ret;
194 
195 	WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n",
196 	    wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS);
197 
198 	xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? WDC_NOSLEEP : WDC_CANSLEEP);
199 	if (xfer == NULL) {
200 		return TRY_AGAIN_LATER;
201 	}
202 	if (sc_xfer->flags & SCSI_POLL)
203 		xfer->c_flags |= C_POLL;
204 	drvp = &wdc->channels[channel]->ch_drive[drive];
205 	if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
206 	    sc_xfer->datalen > 0)
207 		xfer->c_flags |= C_DMA;
208 	xfer->drive = drive;
209 	xfer->c_flags |= C_ATAPI;
210 	xfer->cmd = sc_xfer;
211 	xfer->databuf = sc_xfer->data;
212 	xfer->c_bcount = sc_xfer->datalen;
213 	xfer->c_start = wdc_atapi_start;
214 	xfer->c_intr = wdc_atapi_intr;
215 	s = splbio();
216 	wdc_exec_xfer(wdc->channels[channel], xfer);
217 #ifdef DIAGNOSTIC
218 	if ((sc_xfer->flags & SCSI_POLL) != 0 &&
219 	    (sc_xfer->flags & ITSDONE) == 0)
220 		panic("wdc_atapi_send_cmd: polled command not done");
221 #endif
222 	ret = (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
223 	splx(s);
224 	return ret;
225 }
226 
227 void
228 wdc_atapi_start(chp, xfer)
229 	struct channel_softc *chp;
230 	struct wdc_xfer *xfer;
231 {
232 	struct scsipi_xfer *sc_xfer = xfer->cmd;
233 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
234 
235 	WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n",
236 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive,
237 	    sc_xfer->flags), DEBUG_XFERS);
238 	/* Do control operations specially. */
239 	if (drvp->state < READY) {
240 		if (drvp->state != PIOMODE) {
241 			printf("%s:%d:%d: bad state %d in wdc_atapi_start\n",
242 			    chp->wdc->sc_dev.dv_xname, chp->channel,
243 			    xfer->drive, drvp->state);
244 			panic("wdc_atapi_start: bad state");
245 		}
246 		wdc_atapi_ctrl(chp, xfer);
247 		return;
248 	}
249 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
250 	    WDSD_IBM | (xfer->drive << 4));
251 	if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) {
252 		printf("wdc_atapi_start: not ready, st = %02x\n",
253 		    chp->ch_status);
254 		sc_xfer->error = XS_TIMEOUT;
255 		wdc_atapi_reset(chp, xfer);
256 		return;
257 	}
258 
259 	/*
260 	 * Even with WDCS_ERR, the device should accept a command packet
261 	 * Limit length to what can be stuffed into the cylinder register
262 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
263 	 * but not all devices do that and it's not obvious from the
264 	 * ATAPI spec that that behaviour should be expected.  If more
265 	 * data is necessary, multiple data transfer phases will be done.
266 	 */
267 
268 	wdccommand(chp, xfer->drive, ATAPI_PKT_CMD,
269 	    sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
270 	    0, 0, 0,
271 	    (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0);
272 
273 	/*
274 	 * If there is no interrupt for CMD input, busy-wait for it (done in
275 	 * the interrupt routine. If it is a polled command, call the interrupt
276 	 * routine until command is done.
277 	 */
278 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & 0x0300) != ACAP_DRQ_INTR ||
279 	    sc_xfer->flags & SCSI_POLL) {
280 		/* Wait for at last 400ns for status bit to be valid */
281 		delay(1);
282 		if (wdc_atapi_intr(chp, xfer) == 0) {
283 			sc_xfer->error = XS_TIMEOUT;
284 			wdc_atapi_reset(chp, xfer);
285 			return;
286 		}
287 	}
288 	if (sc_xfer->flags & SCSI_POLL) {
289 		while ((sc_xfer->flags & ITSDONE) == 0) {
290 			/* Wait for at last 400ns for status bit to be valid */
291 			delay(1);
292 			if (wdc_atapi_intr(chp, xfer) == 0) {
293 				sc_xfer->error = XS_SELTIMEOUT;
294 				    /* do we know more ? */
295 				wdc_atapi_done(chp, xfer);
296 				return;
297 			}
298 		}
299 	}
300 }
301 
302 int
303 wdc_atapi_intr(chp, xfer)
304 	struct channel_softc *chp;
305 	struct wdc_xfer *xfer;
306 {
307 	struct scsipi_xfer *sc_xfer = xfer->cmd;
308 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
309 	int len, phase, i, retries=0;
310 	int ire, dma_err = 0;
311 	int dma_flags = 0;
312 	struct scsipi_generic _cmd_reqsense;
313 	struct scsipi_sense *cmd_reqsense =
314 	    (struct scsipi_sense *)&_cmd_reqsense;
315 	void *cmd;
316 
317 	WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n",
318 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR);
319 
320 	/* Is it not a transfer, but a control operation? */
321 	if (drvp->state < READY) {
322 		printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n",
323 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
324 		    drvp->state);
325 		panic("wdc_atapi_intr: bad state\n");
326 	}
327 	/* Ack interrupt done in wait_for_unbusy */
328 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
329 	    WDSD_IBM | (xfer->drive << 4));
330 	if (wait_for_unbusy(chp, sc_xfer->timeout) != 0) {
331 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
332 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
333 		    xfer->c_bcount, xfer->c_skip);
334 		if (xfer->c_flags & C_DMA)
335 				drvp->n_dmaerrs++;
336 		sc_xfer->error = XS_TIMEOUT;
337 		wdc_atapi_reset(chp, xfer);
338 		return 1;
339 	}
340 	/*
341 	 * if the request sense command was aborted, report the short sense
342 	 * previously recorded, else continue normal processing
343 	 */
344 
345 	if ((xfer->c_flags & C_SENSE) != 0 &&
346 	    (chp->ch_status & WDCS_ERR) != 0 &&
347 	    (chp->ch_error & WDCE_ABRT) != 0) {
348 		WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, "
349 		    "calling wdc_atapi_done(), sense 0x%x\n",
350 		    sc_xfer->sense.atapi_sense), DEBUG_INTR);
351 		wdc_atapi_done(chp, xfer);
352 		return 1;
353 	}
354 
355 	if (xfer->c_flags & C_DMA) {
356 		dma_flags = (sc_xfer->flags & SCSI_DATA_IN) ?
357 		    WDC_DMA_READ : 0;
358 		dma_flags |= sc_xfer->flags & SCSI_POLL ? WDC_DMA_POLL : 0;
359 	}
360 again:
361 	len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) +
362 	    256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi);
363 	ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason);
364 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ);
365 	WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x "
366 	    "ire 0x%x :", xfer->c_bcount,
367 	    len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR);
368 
369 	switch (phase) {
370 	case PHASE_CMDOUT:
371 		if (xfer->c_flags & C_SENSE) {
372 			memset(cmd_reqsense, 0, sizeof(struct scsipi_generic));
373 			cmd_reqsense->opcode = REQUEST_SENSE;
374 			cmd_reqsense->length = xfer->c_bcount;
375 			cmd = cmd_reqsense;
376 		} else {
377 			cmd  = sc_xfer->cmd;
378 		}
379 		WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR);
380 		/* Init the DMA channel if necessary */
381 		if (xfer->c_flags & C_DMA) {
382 			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,
383 			    chp->channel, xfer->drive,
384 			    xfer->databuf, xfer->c_bcount, dma_flags) != 0) {
385 				sc_xfer->error = XS_DRIVER_STUFFUP;
386 				break;
387 			}
388 		}
389 		/* send packet command */
390 		/* Commands are 12 or 16 bytes long. It's 32-bit aligned */
391 		if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
392 			if (drvp->drive_flags & DRIVE_CAP32) {
393 				bus_space_write_multi_4(chp->data32iot,
394 				    chp->data32ioh, 0,
395 				    (u_int32_t *)cmd,
396 				    sc_xfer->cmdlen >> 2);
397 			} else {
398 				bus_space_write_multi_2(chp->cmd_iot,
399 				    chp->cmd_ioh, wd_data,
400 				    (u_int16_t *)cmd,
401 				    sc_xfer->cmdlen >> 1);
402 			}
403 		} else {
404 			if (drvp->drive_flags & DRIVE_CAP32) {
405 				bus_space_write_multi_stream_4(chp->data32iot,
406 				    chp->data32ioh, 0,
407 				    (u_int32_t *)cmd,
408 				    sc_xfer->cmdlen >> 2);
409 			} else {
410 				bus_space_write_multi_stream_2(chp->cmd_iot,
411 				    chp->cmd_ioh, wd_data,
412 				    (u_int16_t *)cmd,
413 				    sc_xfer->cmdlen >> 1);
414 			}
415 		}
416 		/* Start the DMA channel if necessary */
417 		if (xfer->c_flags & C_DMA) {
418 			(*chp->wdc->dma_start)(chp->wdc->dma_arg,
419 			    chp->channel, xfer->drive, dma_flags);
420 		}
421 
422 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
423 			chp->ch_flags |= WDCF_IRQ_WAIT;
424 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
425 		}
426 		return 1;
427 
428 	 case PHASE_DATAOUT:
429 		/* write data */
430 		WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR);
431 		if ((sc_xfer->flags & SCSI_DATA_OUT) == 0 ||
432 		    (xfer->c_flags & C_DMA) != 0) {
433 			printf("wdc_atapi_intr: bad data phase DATAOUT");
434 			if (xfer->c_flags & C_DMA) {
435 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
436 				    chp->channel, xfer->drive, dma_flags);
437 				drvp->n_dmaerrs++;
438 			}
439 			sc_xfer->error = XS_TIMEOUT;
440 			wdc_atapi_reset(chp, xfer);
441 			return 1;
442 		}
443 		if (xfer->c_bcount < len) {
444 			printf("wdc_atapi_intr: warning: write only "
445 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
446 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
447 				bus_space_write_multi_2(chp->cmd_iot,
448 				    chp->cmd_ioh, wd_data,
449 				    (u_int16_t *)((char *)xfer->databuf +
450 				                  xfer->c_skip),
451 				    xfer->c_bcount >> 1);
452 			} else {
453 				bus_space_write_multi_stream_2(chp->cmd_iot,
454 				    chp->cmd_ioh, wd_data,
455 				    (u_int16_t *)((char *)xfer->databuf +
456 				                  xfer->c_skip),
457 				    xfer->c_bcount >> 1);
458 			}
459 			for (i = xfer->c_bcount; i < len; i += 2)
460 				bus_space_write_2(chp->cmd_iot, chp->cmd_ioh,
461 				    wd_data, 0);
462 			xfer->c_skip += xfer->c_bcount;
463 			xfer->c_bcount = 0;
464 		} else {
465 			if (drvp->drive_flags & DRIVE_CAP32) {
466 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
467 				bus_space_write_multi_4(chp->data32iot,
468 				    chp->data32ioh, 0,
469 				    (u_int32_t *)((char *)xfer->databuf +
470 				                  xfer->c_skip),
471 				    len >> 2);
472 			    else
473 				bus_space_write_multi_stream_4(chp->data32iot,
474 				    chp->data32ioh, wd_data,
475 				    (u_int32_t *)((char *)xfer->databuf +
476 				                  xfer->c_skip),
477 				    len >> 2);
478 
479 			    xfer->c_skip += len & 0xfffffffc;
480 			    xfer->c_bcount -= len & 0xfffffffc;
481 			    len = len & 0x03;
482 			}
483 			if (len > 0) {
484 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
485 				bus_space_write_multi_2(chp->cmd_iot,
486 				    chp->cmd_ioh, wd_data,
487 				    (u_int16_t *)((char *)xfer->databuf +
488 				                  xfer->c_skip),
489 				    len >> 1);
490 			    else
491 				bus_space_write_multi_stream_2(chp->cmd_iot,
492 				    chp->cmd_ioh, wd_data,
493 				    (u_int16_t *)((char *)xfer->databuf +
494 				                  xfer->c_skip),
495 				    len >> 1);
496 			    xfer->c_skip += len;
497 			    xfer->c_bcount -= len;
498 			}
499 		}
500 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
501 			chp->ch_flags |= WDCF_IRQ_WAIT;
502 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
503 		}
504 		return 1;
505 
506 	case PHASE_DATAIN:
507 		/* Read data */
508 		WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR);
509 		if (((sc_xfer->flags & SCSI_DATA_IN) == 0 &&
510 		    (xfer->c_flags & C_SENSE) == 0) ||
511 		    (xfer->c_flags & C_DMA) != 0) {
512 			printf("wdc_atapi_intr: bad data phase DATAIN");
513 			if (xfer->c_flags & C_DMA) {
514 				(*chp->wdc->dma_finish)(chp->wdc->dma_arg,
515 				    chp->channel, xfer->drive, dma_flags);
516 				drvp->n_dmaerrs++;
517 			}
518 			sc_xfer->error = XS_TIMEOUT;
519 			wdc_atapi_reset(chp, xfer);
520 			return 1;
521 		}
522 		if (xfer->c_bcount < len) {
523 			printf("wdc_atapi_intr: warning: reading only "
524 			    "%d of %d bytes\n", xfer->c_bcount, len);
525 			if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) {
526 			    bus_space_read_multi_2(chp->cmd_iot,
527 			    chp->cmd_ioh, wd_data,
528 			    (u_int16_t *)((char *)xfer->databuf +
529 			                  xfer->c_skip),
530 			    xfer->c_bcount >> 1);
531 			} else {
532 			    bus_space_read_multi_stream_2(chp->cmd_iot,
533 			    chp->cmd_ioh, wd_data,
534 			    (u_int16_t *)((char *)xfer->databuf +
535 			                  xfer->c_skip),
536 			    xfer->c_bcount >> 1);
537 			}
538 			wdcbit_bucket(chp, len - xfer->c_bcount);
539 			xfer->c_skip += xfer->c_bcount;
540 			xfer->c_bcount = 0;
541 		} else {
542 			if (drvp->drive_flags & DRIVE_CAP32) {
543 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
544 				bus_space_read_multi_4(chp->data32iot,
545 				    chp->data32ioh, 0,
546 				    (u_int32_t *)((char *)xfer->databuf +
547 				                  xfer->c_skip),
548 				    len >> 2);
549 			    else
550 				bus_space_read_multi_stream_4(chp->data32iot,
551 				    chp->data32ioh, wd_data,
552 				    (u_int32_t *)((char *)xfer->databuf +
553 				                  xfer->c_skip),
554 				    len >> 2);
555 
556 			    xfer->c_skip += len & 0xfffffffc;
557 			    xfer->c_bcount -= len & 0xfffffffc;
558 			    len = len & 0x03;
559 			}
560 			if (len > 0) {
561 			    if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM))
562 				bus_space_read_multi_2(chp->cmd_iot,
563 				    chp->cmd_ioh, wd_data,
564 				    (u_int16_t *)((char *)xfer->databuf +
565 				                  xfer->c_skip),
566 				    len >> 1);
567 			    else
568 				bus_space_read_multi_stream_2(chp->cmd_iot,
569 				    chp->cmd_ioh, wd_data,
570 				    (u_int16_t *)((char *)xfer->databuf +
571 				                  xfer->c_skip),
572 				    len >> 1);
573 			    xfer->c_skip += len;
574 			    xfer->c_bcount -=len;
575 			}
576 		}
577 		if ((sc_xfer->flags & SCSI_POLL) == 0) {
578 			chp->ch_flags |= WDCF_IRQ_WAIT;
579 			timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
580 		}
581 		return 1;
582 
583 	case PHASE_ABORTED:
584 	case PHASE_COMPLETED:
585 		WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR);
586 		/* turn off DMA channel */
587 		if (xfer->c_flags & C_DMA) {
588 			dma_err = (*chp->wdc->dma_finish)(chp->wdc->dma_arg,
589 			    chp->channel, xfer->drive, dma_flags);
590 			xfer->c_bcount -= sc_xfer->datalen;
591 		}
592 		if (xfer->c_flags & C_SENSE) {
593 			if ((chp->ch_status & WDCS_ERR) || dma_err < 0) {
594 				/*
595 				 * request sense failed ! it's not suppossed
596 				 * to be possible
597 				 */
598 				sc_xfer->error = XS_DRIVER_STUFFUP;
599 			} else {
600 				/* use the sense we just read */
601 				sc_xfer->error = XS_SENSE;
602 			}
603 		} else {
604 			if (chp->ch_status & WDCS_ERR) {
605 				/* save the short sense */
606 				sc_xfer->error = XS_SHORTSENSE;
607 				sc_xfer->sense.atapi_sense = chp->ch_error;
608 				if ((sc_xfer->sc_link->quirks &
609 				    ADEV_NOSENSE) == 0) {
610 					/*
611 					 * let the driver issue a
612 					 * 'request sense'
613 					 */
614 					xfer->databuf = &sc_xfer->sense;
615 					xfer->c_bcount =
616 					    sizeof(sc_xfer->sense.scsi_sense);
617 					xfer->c_flags |= C_SENSE;
618 					wdc_atapi_start(chp, xfer);
619 					return 1;
620 				}
621 			} else if (dma_err < 0) {
622 				drvp->n_dmaerrs++;
623 				sc_xfer->error = XS_DRIVER_STUFFUP;
624 			}
625 		}
626 		if (xfer->c_bcount != 0) {
627 			WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is "
628 			    "%d after io\n", xfer->c_bcount), DEBUG_XFERS);
629 		}
630 #ifdef DIAGNOSTIC
631 		if (xfer->c_bcount < 0) {
632 			printf("wdc_atapi_intr warning: bcount value "
633 			    "is %d after io\n", xfer->c_bcount);
634 		}
635 #endif
636 		break;
637 
638 	default:
639 		if (++retries<500) {
640 			DELAY(100);
641 			chp->ch_status = bus_space_read_1(chp->cmd_iot,
642 			    chp->cmd_ioh, wd_status);
643 			chp->ch_error = bus_space_read_1(chp->cmd_iot,
644 			    chp->cmd_ioh, wd_error);
645 			goto again;
646 		}
647 		printf("wdc_atapi_intr: unknown phase 0x%x\n", phase);
648 		if (chp->ch_status & WDCS_ERR) {
649 			sc_xfer->error = XS_SHORTSENSE;
650 			sc_xfer->sense.atapi_sense = chp->ch_error;
651 		} else {
652 			sc_xfer->error = XS_DRIVER_STUFFUP;
653 		}
654 	}
655 	WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x "
656 	    "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense),
657 	    DEBUG_INTR);
658 	wdc_atapi_done(chp, xfer);
659 	return (1);
660 }
661 
662 int
663 wdc_atapi_ctrl(chp, xfer)
664 	struct channel_softc *chp;
665 	struct wdc_xfer *xfer;
666 {
667 	struct scsipi_xfer *sc_xfer = xfer->cmd;
668 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
669 	char *errstring = NULL;
670 
671 	/* Ack interrupt done in wait_for_unbusy */
672 again:
673 	WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n",
674 	    chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state),
675 	    DEBUG_INTR | DEBUG_FUNCS);
676 	bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh,
677 	    WDSD_IBM | (xfer->drive << 4));
678 	switch (drvp->state) {
679 	case PIOMODE:
680 		/* Don't try to set mode if controller can't be adjusted */
681 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
682 			goto ready;
683 		/* Also don't try if the drive didn't report its mode */
684 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
685 			goto ready;;
686 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
687 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
688 		drvp->state = PIOMODE_WAIT;
689 		break;
690 	case PIOMODE_WAIT:
691 		errstring = "piomode";
692 		if (wait_for_unbusy(chp, ATAPI_DELAY))
693 			goto timeout;
694 		if (chp->ch_status & WDCS_ERR)
695 			goto error;
696 	/* fall through */
697 
698 	case DMAMODE:
699 		if (drvp->drive_flags & DRIVE_UDMA) {
700 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
701 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
702 		} else if (drvp->drive_flags & DRIVE_DMA) {
703 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
704 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
705 		} else {
706 			goto ready;
707 		}
708 		drvp->state = DMAMODE_WAIT;
709 		break;
710 	case DMAMODE_WAIT:
711 		errstring = "dmamode";
712 		if (wait_for_unbusy(chp, ATAPI_DELAY))
713 			goto timeout;
714 		if (chp->ch_status & WDCS_ERR)
715 			goto error;
716 	/* fall through */
717 
718 	case READY:
719 	ready:
720 		drvp->state = READY;
721 		xfer->c_intr = wdc_atapi_intr;
722 		wdc_atapi_start(chp, xfer);
723 		return 1;
724 	}
725 	if ((sc_xfer->flags & SCSI_POLL) == 0) {
726 		chp->ch_flags |= WDCF_IRQ_WAIT;
727 		xfer->c_intr = wdc_atapi_ctrl;
728 		timeout(wdctimeout, chp, sc_xfer->timeout * hz / 1000);
729 	} else {
730 		goto again;
731 	}
732 	return 1;
733 
734 timeout:
735 	if ((xfer->c_flags & C_TIMEOU) == 0 ) {
736 		return 0; /* IRQ was not for us */
737 	}
738 	printf("%s:%d:%d: %s timed out\n",
739 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring);
740 	sc_xfer->error = XS_TIMEOUT;
741 	wdc_atapi_reset(chp, xfer);
742 	return 1;
743 error:
744 	printf("%s:%d:%d: %s ",
745 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
746 	    errstring);
747 	printf("error (0x%x)\n", chp->ch_error);
748 	sc_xfer->error = XS_SHORTSENSE;
749 	sc_xfer->sense.atapi_sense = chp->ch_error;
750 	wdc_atapi_reset(chp, xfer);
751 	return 1;
752 }
753 
754 void
755 wdc_atapi_done(chp, xfer)
756 	struct channel_softc *chp;
757 	struct wdc_xfer *xfer;
758 {
759 	struct scsipi_xfer *sc_xfer = xfer->cmd;
760 	int need_done =  xfer->c_flags & C_NEEDDONE;
761 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
762 
763 	WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n",
764 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
765 	    (u_int)xfer->c_flags), DEBUG_XFERS);
766 	sc_xfer->resid = xfer->c_bcount;
767 	/* remove this command from xfer queue */
768 	xfer->c_skip = 0;
769 	wdc_free_xfer(chp, xfer);
770 	sc_xfer->flags |= ITSDONE;
771 	if (sc_xfer->error == XS_NOERROR ||
772 	    sc_xfer->error == XS_SENSE ||
773 	    sc_xfer->error == XS_SHORTSENSE) {
774 		drvp->n_dmaerrs = 0;
775 	} else {
776 		wdc_downgrade_mode(drvp);
777 	}
778 
779 	if (need_done) {
780 		WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS);
781 		scsipi_done(sc_xfer);
782 	}
783 	WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n",
784 	    chp->ch_flags), DEBUG_XFERS);
785 	wdcstart(chp);
786 }
787 
788 void
789 wdc_atapi_reset(chp, xfer)
790 	struct channel_softc *chp;
791 	struct wdc_xfer *xfer;
792 {
793 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
794 	struct scsipi_xfer *sc_xfer = xfer->cmd;
795 
796 	wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET);
797 	drvp->state = 0;
798 	if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) {
799 		printf("%s:%d:%d: reset failed\n",
800 		    chp->wdc->sc_dev.dv_xname, chp->channel,
801 		    xfer->drive);
802 		sc_xfer->error = XS_SELTIMEOUT;
803 		wdc_atapi_done(chp, xfer);
804 		return;
805 	}
806 	wdc_atapi_done(chp, xfer);
807 	return;
808 }
809