xref: /openbsd/sys/dev/ata/ata_wdc.c (revision 8932bfb7)
1 /*      $OpenBSD: ata_wdc.c,v 1.40 2011/07/15 16:44:18 deraadt Exp $	*/
2 /*	$NetBSD: ata_wdc.c,v 1.21 1999/08/09 09:43:11 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 2001 Manuel Bouyer.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*-
30  * Copyright (c) 1998 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  * POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/file.h>
62 #include <sys/stat.h>
63 #include <sys/buf.h>
64 #include <sys/malloc.h>
65 #include <sys/device.h>
66 #include <sys/disklabel.h>
67 #include <sys/disk.h>
68 #include <sys/syslog.h>
69 #include <sys/proc.h>
70 
71 #include <machine/intr.h>
72 #include <machine/bus.h>
73 
74 #include <dev/ata/atareg.h>
75 #include <dev/ata/atavar.h>
76 #include <dev/ic/wdcreg.h>
77 #include <dev/ic/wdcvar.h>
78 #include <dev/ata/wdvar.h>
79 
80 #define DEBUG_INTR   0x01
81 #define DEBUG_XFERS  0x02
82 #define DEBUG_STATUS 0x04
83 #define DEBUG_FUNCS  0x08
84 #define DEBUG_PROBE  0x10
85 
86 #ifdef WDCDEBUG
87 #ifndef WDCDEBUG_WD_MASK
88 #define WDCDEBUG_WD_MASK 0x00
89 #endif
90 int wdcdebug_wd_mask = WDCDEBUG_WD_MASK;
91 #define WDCDEBUG_PRINT(args, level) do {	\
92 	if ((wdcdebug_wd_mask & (level)) != 0)	\
93 		printf args;			\
94 } while (0)
95 #else
96 #define WDCDEBUG_PRINT(args, level)
97 #endif
98 
99 #define ATA_DELAY 45000 /* 45s for a drive I/O */
100 
101 void  wdc_ata_bio_start(struct channel_softc *, struct wdc_xfer *);
102 void  _wdc_ata_bio_start(struct channel_softc *, struct wdc_xfer *);
103 int   wdc_ata_bio_intr(struct channel_softc *, struct wdc_xfer *, int);
104 void  wdc_ata_bio_kill_xfer(struct channel_softc *, struct wdc_xfer *);
105 void  wdc_ata_bio_done(struct channel_softc *, struct wdc_xfer *);
106 int   wdc_ata_ctrl_intr(struct channel_softc *, struct wdc_xfer *, int);
107 int   wdc_ata_err(struct ata_drive_datas *, struct ata_bio *);
108 #define WDC_ATA_NOERR 0x00 /* Drive doesn't report an error */
109 #define WDC_ATA_RECOV 0x01 /* There was a recovered error */
110 #define WDC_ATA_ERR   0x02 /* Drive reports an error */
111 
112 int
113 wd_hibernate_io(dev_t dev, daddr_t blkno, caddr_t addr, size_t size, int wr, void *page)
114 {
115 	struct {
116 		struct wd_softc wd;
117 		struct wdc_xfer xfer;
118 		struct channel_softc chp;
119 	} *my = page;
120 	struct wd_softc *real_wd, *wd = &my->wd;
121 	struct wdc_xfer *xfer = &my->xfer;
122 	struct channel_softc *chp = &my->chp;
123 	struct ata_bio *ata_bio;
124 	extern struct cfdriver wd_cd;
125 
126 	real_wd = (struct wd_softc *)disk_lookup(&wd_cd, DISKUNIT(dev));
127 	if (real_wd == NULL)
128 		return (ENODEV);
129 
130 	/*
131 	 * Craft a fake set of softc and related structures
132 	 * which we think the driver modifies.  Some of these will
133 	 * have pointers which reach to unsafe places, but..
134 	 */
135 	bcopy(real_wd->drvp->chnl_softc, &my->chp, sizeof my->chp);
136 	chp->ch_drive[0].chnl_softc = chp;
137 	chp->ch_drive[1].chnl_softc = chp;
138 
139 	bcopy(real_wd, &my->wd, sizeof my->wd);
140 	ata_bio = &wd->sc_wdc_bio;
141 	ata_bio->wd = wd;		/* fixup ata_bio->wd */
142 	wd->drvp = &chp->ch_drive[real_wd->drvp->drive];
143 
144 	/* Fill the request and submit it */
145 	wd->sc_wdc_bio.blkno = blkno;
146 	wd->sc_wdc_bio.flags = ATA_POLL | ATA_LBA48;
147 	if (wr == 0)
148 		wd->sc_wdc_bio.flags |= ATA_READ;
149 	wd->sc_wdc_bio.bcount = size;
150 	wd->sc_wdc_bio.databuf = addr;
151 	wd->sc_wdc_bio.wd = wd;
152 
153 	bzero(&my->xfer, sizeof my->xfer);
154 	xfer->c_flags |= C_PRIVATEXFER;	/* Our xfer is totally private */
155 	xfer->c_flags |= C_POLL;
156 	xfer->drive = wd->drvp->drive;
157 	xfer->cmd = ata_bio;
158 	xfer->databuf = ata_bio->databuf;
159 	xfer->c_bcount = ata_bio->bcount;
160 	xfer->c_start = wdc_ata_bio_start;
161 	xfer->c_intr = wdc_ata_bio_intr;
162 	xfer->c_kill_xfer = wdc_ata_bio_kill_xfer;
163 	wdc_exec_xfer(chp, xfer);
164 	return (ata_bio->flags & ATA_ITSDONE) ? 0 : 1;
165 }
166 
167 /*
168  * Handle block I/O operation. Return WDC_COMPLETE, WDC_QUEUED, or
169  * WDC_TRY_AGAIN. Must be called at splbio().
170  */
171 int
172 wdc_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
173 {
174 	struct wdc_xfer *xfer;
175 	struct channel_softc *chp = drvp->chnl_softc;
176 
177 	xfer = wdc_get_xfer(WDC_NOSLEEP);
178 	if (xfer == NULL)
179 		return WDC_TRY_AGAIN;
180 	if (ata_bio->flags & ATA_POLL)
181 		xfer->c_flags |= C_POLL;
182 	if (!(ata_bio->flags & ATA_POLL) &&
183 	    (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) &&
184 	    (ata_bio->flags & ATA_SINGLE) == 0 &&
185 	    (ata_bio->bcount > 512 ||
186 	    (chp->wdc->quirks & WDC_QUIRK_NOSHORTDMA) == 0))
187 		xfer->c_flags |= C_DMA;
188 	xfer->drive = drvp->drive;
189 	xfer->cmd = ata_bio;
190 	xfer->databuf = ata_bio->databuf;
191 	xfer->c_bcount = ata_bio->bcount;
192 	xfer->c_start = wdc_ata_bio_start;
193 	xfer->c_intr = wdc_ata_bio_intr;
194 	xfer->c_kill_xfer = wdc_ata_bio_kill_xfer;
195 	wdc_exec_xfer(chp, xfer);
196 	return (ata_bio->flags & ATA_ITSDONE) ? WDC_COMPLETE : WDC_QUEUED;
197 }
198 
199 void
200 wdc_ata_bio_start(struct channel_softc *chp, struct wdc_xfer *xfer)
201 {
202 	struct ata_bio *ata_bio = xfer->cmd;
203 	WDCDEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d\n",
204 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
205 	    DEBUG_XFERS);
206 
207 	/* start timeout machinery */
208 	if ((ata_bio->flags & ATA_POLL) == 0)
209 		timeout_add_msec(&chp->ch_timo, ATA_DELAY);
210 	_wdc_ata_bio_start(chp, xfer);
211 }
212 
213 void
214 _wdc_ata_bio_start(struct channel_softc *chp, struct wdc_xfer *xfer)
215 {
216 	struct ata_bio *ata_bio = xfer->cmd;
217 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
218 	u_int16_t cyl;
219 	u_int8_t head, sect, cmd = 0;
220 	int nblks;
221 	int ata_delay;
222 	int error, dma_flags = 0;
223 
224 	WDCDEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",
225 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
226 	    DEBUG_INTR | DEBUG_XFERS);
227 	/* Do control operations specially. */
228 	if (drvp->state < READY) {
229 		/*
230 		 * Actually, we want to be careful not to mess with the control
231 		 * state if the device is currently busy, but we can assume
232 		 * that we never get to this point if that's the case.
233 		 */
234 		/* at this point, we should only be in RECAL state */
235 		if (drvp->state != RECAL) {
236 			printf("%s:%d:%d: bad state %d in _wdc_ata_bio_start\n",
237 			    chp->wdc->sc_dev.dv_xname, chp->channel,
238 			    xfer->drive, drvp->state);
239 			panic("_wdc_ata_bio_start: bad state");
240 		}
241 		xfer->c_intr = wdc_ata_ctrl_intr;
242 		wdc_set_drive(chp, xfer->drive);
243 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY) != 0)
244 			goto timeout;
245 		wdccommandshort(chp, xfer->drive, WDCC_RECAL);
246 		drvp->state = RECAL_WAIT;
247 		if ((ata_bio->flags & ATA_POLL) == 0) {
248 			chp->ch_flags |= WDCF_IRQ_WAIT;
249 		} else {
250 			/* Wait for at last 400ns for status bit to be valid */
251 			DELAY(1);
252 			wdc_ata_ctrl_intr(chp, xfer, 0);
253 		}
254 		return;
255 	}
256 
257 	if (xfer->c_flags & C_DMA) {
258 		if (drvp->n_xfers <= NXFER)
259 			drvp->n_xfers++;
260 		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;
261 		if (ata_bio->flags & ATA_LBA48)
262 			dma_flags |= WDC_DMA_LBA48;
263 	}
264 	if (ata_bio->flags & ATA_SINGLE)
265 		ata_delay = ATA_DELAY;
266 	else
267 		ata_delay = ATA_DELAY;
268 again:
269 	/*
270 	 *
271 	 * When starting a multi-sector transfer, or doing single-sector
272 	 * transfers...
273 	 */
274 	if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
275 		if (ata_bio->flags & ATA_SINGLE)
276 			nblks = 1;
277 		else
278 			nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
279 		if (ata_bio->flags & ATA_LBA) {
280 			sect = (ata_bio->blkno >> 0) & 0xff;
281 			cyl = (ata_bio->blkno >> 8) & 0xffff;
282 			head = (ata_bio->blkno >> 24) & 0x0f;
283 			head |= WDSD_LBA;
284 		} else {
285 			int blkno = ata_bio->blkno;
286 			sect = blkno % ata_bio->lp->d_nsectors;
287 			sect++;    /* Sectors begin with 1, not 0. */
288 			blkno /= ata_bio->lp->d_nsectors;
289 			head = blkno % ata_bio->lp->d_ntracks;
290 			blkno /= ata_bio->lp->d_ntracks;
291 			cyl = blkno;
292 			head |= WDSD_CHS;
293 		}
294 		if (xfer->c_flags & C_DMA) {
295 			ata_bio->nblks = nblks;
296 			ata_bio->nbytes = xfer->c_bcount;
297 			if (ata_bio->flags & ATA_LBA48)
298 				cmd = (ata_bio->flags & ATA_READ) ?
299 				    WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
300 			else
301 				cmd = (ata_bio->flags & ATA_READ) ?
302 				    WDCC_READDMA : WDCC_WRITEDMA;
303 	    		/* Init the DMA channel. */
304 			error = (*chp->wdc->dma_init)(chp->wdc->dma_arg,
305 			    chp->channel, xfer->drive,
306 			    (char *)xfer->databuf + xfer->c_skip,
307 			    ata_bio->nbytes, dma_flags);
308 			if (error) {
309 				if (error == EINVAL) {
310 					/*
311 					 * We can't do DMA on this transfer
312 					 * for some reason.  Fall back to
313 					 * PIO.
314 					 */
315 					xfer->c_flags &= ~C_DMA;
316 					error = 0;
317 					goto do_pio;
318 				}
319 				ata_bio->error = ERR_DMA;
320 				ata_bio->r_error = 0;
321 				wdc_ata_bio_done(chp, xfer);
322 				return;
323 			}
324 			/* Initiate command */
325 			wdc_set_drive(chp, xfer->drive);
326 			if (wait_for_ready(chp, ata_delay) < 0)
327 				goto timeout;
328 
329 			/* start the DMA channel (before) */
330 			if (chp->ch_flags & WDCF_DMA_BEFORE_CMD)
331 				(*chp->wdc->dma_start)(chp->wdc->dma_arg,
332 				    chp->channel, xfer->drive);
333 
334 			if (ata_bio->flags & ATA_LBA48) {
335 				wdccommandext(chp, xfer->drive, cmd,
336 				    (u_int64_t)ata_bio->blkno, nblks);
337 			} else {
338 				wdccommand(chp, xfer->drive, cmd, cyl,
339 				    head, sect, nblks, 0);
340 			}
341 
342 			/* start the DMA channel (after) */
343 			if ((chp->ch_flags & WDCF_DMA_BEFORE_CMD) == 0)
344 				(*chp->wdc->dma_start)(chp->wdc->dma_arg,
345 				    chp->channel, xfer->drive);
346 
347 			chp->ch_flags |= WDCF_DMA_WAIT;
348 			/* wait for irq */
349 			goto intr;
350 		} /* else not DMA */
351  do_pio:
352 		ata_bio->nblks = min(nblks, ata_bio->multi);
353 		ata_bio->nbytes = ata_bio->nblks * ata_bio->lp->d_secsize;
354 		KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0);
355 		if (ata_bio->nblks > 1) {
356 			if (ata_bio->flags & ATA_LBA48)
357 				cmd = (ata_bio->flags & ATA_READ) ?
358 				    WDCC_READMULTI_EXT : WDCC_WRITEMULTI_EXT;
359 			else
360 				cmd = (ata_bio->flags & ATA_READ) ?
361 				    WDCC_READMULTI : WDCC_WRITEMULTI;
362 		} else {
363 			if (ata_bio->flags & ATA_LBA48)
364 				cmd = (ata_bio->flags & ATA_READ) ?
365 				    WDCC_READ_EXT : WDCC_WRITE_EXT;
366 			else
367 				cmd = (ata_bio->flags & ATA_READ) ?
368 				    WDCC_READ : WDCC_WRITE;
369 		}
370 		/* Initiate command! */
371 		wdc_set_drive(chp, xfer->drive);
372 		if (wait_for_ready(chp, ata_delay) < 0)
373 			goto timeout;
374 		if (ata_bio->flags & ATA_LBA48) {
375 			wdccommandext(chp, xfer->drive, cmd,
376 			    (u_int64_t)ata_bio->blkno, nblks);
377 		} else {
378 			wdccommand(chp, xfer->drive, cmd, cyl,
379 			    head, sect, nblks,
380 			    (ata_bio->lp->d_type == DTYPE_ST506) ?
381 			    ata_bio->lp->d_precompcyl / 4 : 0);
382 		}
383 	} else if (ata_bio->nblks > 1) {
384 		/* The number of blocks in the last stretch may be smaller. */
385 		nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
386 		if (ata_bio->nblks > nblks) {
387 			ata_bio->nblks = nblks;
388 			ata_bio->nbytes = xfer->c_bcount;
389 		}
390 	}
391 	/* If this was a write and not using DMA, push the data. */
392 	if ((ata_bio->flags & ATA_READ) == 0) {
393 		if (wait_for_drq(chp, ata_delay) != 0) {
394 			printf("%s:%d:%d: timeout waiting for DRQ, "
395 			    "st=0x%b, err=0x%02x\n",
396 			    chp->wdc->sc_dev.dv_xname, chp->channel,
397 			    xfer->drive, chp->ch_status, WDCS_BITS,
398 			    chp->ch_error);
399 			if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
400 				ata_bio->error = TIMEOUT;
401 			wdc_ata_bio_done(chp, xfer);
402 			return;
403 		}
404 		if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {
405 			wdc_ata_bio_done(chp, xfer);
406 			return;
407 		}
408 		wdc_output_bytes(drvp, (char *)xfer->databuf + xfer->c_skip,
409 		    ata_bio->nbytes);
410 	}
411 
412 intr:	/* Wait for IRQ (either real or polled) */
413 	if ((ata_bio->flags & ATA_POLL) == 0) {
414 		chp->ch_flags |= WDCF_IRQ_WAIT;
415 	} else {
416 		/* Wait for at last 400ns for status bit to be valid */
417 		delay(1);
418 		if (chp->ch_flags & WDCF_DMA_WAIT) {
419 			wdc_dmawait(chp, xfer, ATA_DELAY);
420 			chp->ch_flags &= ~WDCF_DMA_WAIT;
421 		}
422 		wdc_ata_bio_intr(chp, xfer, 0);
423 		if ((ata_bio->flags & ATA_ITSDONE) == 0)
424 			goto again;
425 	}
426 	return;
427 timeout:
428 	if (chp->ch_status == 0xff)
429 		return;
430 	printf("%s:%d:%d: not ready, st=0x%b, err=0x%02x\n",
431 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
432 	    chp->ch_status, WDCS_BITS, chp->ch_error);
433 	if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
434 		ata_bio->error = TIMEOUT;
435 	wdc_ata_bio_done(chp, xfer);
436 	return;
437 }
438 
439 int
440 wdc_ata_bio_intr(struct channel_softc *chp, struct wdc_xfer *xfer, int irq)
441 {
442 	struct ata_bio *ata_bio = xfer->cmd;
443 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
444 	int drv_err;
445 
446 	WDCDEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
447 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),
448 	    DEBUG_INTR | DEBUG_XFERS);
449 
450 
451 	/* Is it not a transfer, but a control operation? */
452 	if (drvp->state < READY) {
453 		printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
454 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
455 		    drvp->state);
456 		panic("wdc_ata_bio_intr: bad state");
457 	}
458 
459 	/*
460 	 * reset on timeout. This will cause extra resets in the case
461 	 * of occasional lost interrupts
462 	 */
463 	if (xfer->c_flags & C_TIMEOU)
464 		goto timeout;
465 
466 	/* Ack interrupt done by wait_for_unbusy */
467 	if (wait_for_unbusy(chp,
468 	    (irq == 0) ? ATA_DELAY : 0) < 0) {
469 		if (irq)
470 			return 0; /* IRQ was not for us */
471 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
472 		    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
473 		    xfer->c_bcount, xfer->c_skip);
474 
475 		goto timeout;
476 	}
477 	if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
478 		chp->wdc->irqack(chp);
479 
480 	drv_err = wdc_ata_err(drvp, ata_bio);
481 
482 	if (xfer->c_flags & C_DMA) {
483 		if (chp->wdc->dma_status != 0) {
484 			if (drv_err != WDC_ATA_ERR) {
485 				ata_bio->error = ERR_DMA;
486 				drv_err = WDC_ATA_ERR;
487 			}
488 		}
489 		if (chp->ch_status & WDCS_DRQ) {
490 			if (drv_err != WDC_ATA_ERR) {
491 				printf("%s:%d:%d: intr with DRQ (st=0x%b)\n",
492 				    chp->wdc->sc_dev.dv_xname, chp->channel,
493 				    xfer->drive, chp->ch_status, WDCS_BITS);
494 				ata_bio->error = TIMEOUT;
495 				drv_err = WDC_ATA_ERR;
496 			}
497 		}
498 		if (drv_err != WDC_ATA_ERR)
499 			goto end;
500 		ata_dmaerr(drvp);
501 	}
502 
503 	/* if we had an error, end */
504 	if (drv_err == WDC_ATA_ERR) {
505 		wdc_ata_bio_done(chp, xfer);
506 		return 1;
507 	}
508 
509 	/* If this was a read and not using DMA, fetch the data. */
510 	if ((ata_bio->flags & ATA_READ) != 0) {
511 		if ((chp->ch_status & WDCS_DRQ) != WDCS_DRQ) {
512 			printf("%s:%d:%d: read intr before drq\n",
513 			    chp->wdc->sc_dev.dv_xname, chp->channel,
514 			    xfer->drive);
515 			ata_bio->error = TIMEOUT;
516 			wdc_ata_bio_done(chp, xfer);
517 			return 1;
518 		}
519 		wdc_input_bytes(drvp, (char *)xfer->databuf + xfer->c_skip,
520 		    ata_bio->nbytes);
521 	}
522 end:
523 	ata_bio->blkno += ata_bio->nblks;
524 	ata_bio->blkdone += ata_bio->nblks;
525 	xfer->c_skip += ata_bio->nbytes;
526 	xfer->c_bcount -= ata_bio->nbytes;
527 	/* See if this transfer is complete. */
528 	if (xfer->c_bcount > 0) {
529 		if ((ata_bio->flags & ATA_POLL) == 0) {
530 			/* Start the next operation */
531 			_wdc_ata_bio_start(chp, xfer);
532 		} else {
533 			/* Let _wdc_ata_bio_start do the loop */
534 			return 1;
535 		}
536 	} else { /* Done with this transfer */
537 		ata_bio->error = NOERROR;
538 		wdc_ata_bio_done(chp, xfer);
539 	}
540 	return 1;
541 
542 timeout:
543 	if (xfer->c_flags & C_DMA)
544 		ata_dmaerr(drvp);
545 
546 	ata_bio->error = TIMEOUT;
547 	wdc_ata_bio_done(chp, xfer);
548 	return 1;
549 }
550 
551 void
552 wdc_ata_bio_kill_xfer(struct channel_softc *chp, struct wdc_xfer *xfer)
553 {
554 	struct ata_bio *ata_bio = xfer->cmd;
555 
556 	timeout_del(&chp->ch_timo);
557 	/* remove this command from xfer queue */
558 	wdc_free_xfer(chp, xfer);
559 
560 	ata_bio->flags |= ATA_ITSDONE;
561 	ata_bio->error = ERR_NODEV;
562 	ata_bio->r_error = WDCE_ABRT;
563 	if ((ata_bio->flags & ATA_POLL) == 0) {
564 		WDCDEBUG_PRINT(("wdc_ata_done: wddone\n"), DEBUG_XFERS);
565 		wddone(ata_bio->wd);
566 	}
567 }
568 
569 void
570 wdc_ata_bio_done(struct channel_softc *chp, struct wdc_xfer *xfer)
571 {
572 	struct ata_bio *ata_bio = xfer->cmd;
573 
574 	WDCDEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
575 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
576 	    (u_int)xfer->c_flags),
577 	    DEBUG_XFERS);
578 
579 	if ((xfer->c_flags & C_PRIVATEXFER) == 0)
580 		timeout_del(&chp->ch_timo);
581 
582 	/* feed back residual bcount to our caller */
583 	ata_bio->bcount = xfer->c_bcount;
584 
585 	/* remove this command from xfer queue */
586 	wdc_free_xfer(chp, xfer);
587 
588 	ata_bio->flags |= ATA_ITSDONE;
589 	if ((ata_bio->flags & ATA_POLL) == 0) {
590 		WDCDEBUG_PRINT(("wdc_ata_done: wddone\n"), DEBUG_XFERS);
591 		wddone(ata_bio->wd);
592 	}
593 	WDCDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
594 	    chp->ch_flags), DEBUG_XFERS);
595 	wdcstart(chp);
596 }
597 
598 /*
599  * Implement operations needed before read/write.
600  */
601 int
602 wdc_ata_ctrl_intr(struct channel_softc *chp, struct wdc_xfer *xfer, int irq)
603 {
604 	struct ata_bio *ata_bio = xfer->cmd;
605 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];
606 	char *errstring = NULL;
607 	int delay = (irq == 0) ? ATA_DELAY : 0;
608 
609 	WDCDEBUG_PRINT(("wdc_ata_ctrl_intr: state %d\n", drvp->state),
610 	    DEBUG_FUNCS);
611 
612 again:
613 	switch (drvp->state) {
614 	case RECAL:    /* Should not be in this state here */
615 		panic("wdc_ata_ctrl_intr: state==RECAL");
616 		break;
617 
618 	case RECAL_WAIT:
619 		errstring = "recal";
620 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
621 			goto timeout;
622 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
623 			chp->wdc->irqack(chp);
624 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
625 			goto error;
626 	/* FALLTHROUGH */
627 
628 	case PIOMODE:
629 		/* Don't try to set modes if controller can't be adjusted */
630 		if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0)
631 			goto geometry;
632 		/* Also don't try if the drive didn't report its mode */
633 		if ((drvp->drive_flags & DRIVE_MODE) == 0)
634 			goto geometry;
635 		/* SET FEATURES 0x08 is only for PIO mode > 2 */
636 		if (drvp->PIO_mode <= 2)
637 			goto geometry;
638 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
639 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
640 		drvp->state = PIOMODE_WAIT;
641 		break;
642 
643 	case PIOMODE_WAIT:
644 		errstring = "piomode";
645 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
646 			goto timeout;
647 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
648 			chp->wdc->irqack(chp);
649 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
650 			goto error;
651 	/* FALLTHROUGH */
652 
653 	case DMAMODE:
654 		if (drvp->drive_flags & DRIVE_UDMA) {
655 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
656 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
657 		} else if (drvp->drive_flags & DRIVE_DMA) {
658 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
659 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
660 		} else {
661 			goto geometry;
662 		}
663 		drvp->state = DMAMODE_WAIT;
664 		break;
665 	case DMAMODE_WAIT:
666 		errstring = "dmamode";
667 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
668 			goto timeout;
669 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
670 			chp->wdc->irqack(chp);
671 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
672 			goto error;
673 	/* FALLTHROUGH */
674 
675 	case GEOMETRY:
676 	geometry:
677 		if (ata_bio->flags & ATA_LBA)
678 			goto multimode;
679 		wdccommand(chp, xfer->drive, WDCC_IDP,
680 		    ata_bio->lp->d_ncylinders,
681 		    ata_bio->lp->d_ntracks - 1, 0, ata_bio->lp->d_nsectors,
682 		    (ata_bio->lp->d_type == DTYPE_ST506) ?
683 			ata_bio->lp->d_precompcyl / 4 : 0);
684 		drvp->state = GEOMETRY_WAIT;
685 		break;
686 
687 	case GEOMETRY_WAIT:
688 		errstring = "geometry";
689 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
690 			goto timeout;
691 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
692 			chp->wdc->irqack(chp);
693 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
694 			goto error;
695 		/* FALLTHROUGH */
696 
697 	case MULTIMODE:
698 	multimode:
699 		if (ata_bio->multi == 1)
700 			goto ready;
701 		wdccommand(chp, xfer->drive, WDCC_SETMULTI, 0, 0, 0,
702 		    ata_bio->multi, 0);
703 		drvp->state = MULTIMODE_WAIT;
704 		break;
705 
706 	case MULTIMODE_WAIT:
707 		errstring = "setmulti";
708 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, delay))
709 			goto timeout;
710 		if (chp->wdc->cap & WDC_CAPABILITY_IRQACK)
711 			chp->wdc->irqack(chp);
712 		if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
713 			goto error;
714 		/* FALLTHROUGH */
715 
716 	case READY:
717 	ready:
718 		drvp->state = READY;
719 		/*
720 		 * The drive is usable now
721 		 */
722 		xfer->c_intr = wdc_ata_bio_intr;
723 		_wdc_ata_bio_start(chp, xfer);
724 		return 1;
725 	}
726 
727 	if ((ata_bio->flags & ATA_POLL) == 0) {
728 		chp->ch_flags |= WDCF_IRQ_WAIT;
729 	} else {
730 		goto again;
731 	}
732 	return 1;
733 
734 timeout:
735 	if (irq && (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 	ata_bio->error = TIMEOUT;
741 	drvp->state = 0;
742 	wdc_ata_bio_done(chp, xfer);
743 	return 0;
744 error:
745 	printf("%s:%d:%d: %s ",
746 	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,
747 	    errstring);
748 	if (chp->ch_status & WDCS_DWF) {
749 		printf("drive fault\n");
750 		ata_bio->error = ERR_DF;
751 	} else {
752 		printf("error (%x)\n", chp->ch_error);
753 		ata_bio->r_error = chp->ch_error;
754 		ata_bio->error = ERROR;
755 	}
756 	drvp->state = 0;
757 	wdc_ata_bio_done(chp, xfer);
758 	return 1;
759 }
760 
761 int
762 wdc_ata_err(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
763 {
764 	struct channel_softc *chp = drvp->chnl_softc;
765 	ata_bio->error = 0;
766 
767 	if (chp->ch_status == 0xff) {
768 		ata_bio->error = ERR_NODEV;
769 		return WDC_ATA_ERR;
770 	}
771 	if (chp->ch_status & WDCS_BSY) {
772 		ata_bio->error = TIMEOUT;
773 		return WDC_ATA_ERR;
774 	}
775 
776 	if (chp->ch_status & WDCS_DWF) {
777 		ata_bio->error = ERR_DF;
778 		return WDC_ATA_ERR;
779 	}
780 
781 	if (chp->ch_status & WDCS_ERR) {
782 		ata_bio->error = ERROR;
783 		ata_bio->r_error = chp->ch_error;
784 		if (drvp->drive_flags & DRIVE_UDMA &&
785 		    (ata_bio->r_error & WDCE_CRC)) {
786 			/*
787 			 * Record the CRC error, to avoid downgrading to
788 			 * multiword DMA
789 			 */
790 			drvp->drive_flags |= DRIVE_DMAERR;
791 		}
792 		if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
793 		    WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
794 			return WDC_ATA_ERR;
795 		return WDC_ATA_NOERR;
796 	}
797 
798 	if (chp->ch_status & WDCS_CORR)
799 		ata_bio->flags |= ATA_CORR;
800 	return WDC_ATA_NOERR;
801 }
802 
803 #if 0
804 int
805 wdc_ata_addref(drvp)
806 	struct ata_drive_datas *drvp;
807 {
808 	struct channel_softc *chp = drvp->chnl_softc;
809 
810 	return (wdc_addref(chp));
811 }
812 
813 void
814 wdc_ata_delref(drvp)
815 	struct ata_drive_datas *drvp;
816 {
817 	struct channel_softc *chp = drvp->chnl_softc;
818 
819 	wdc_delref(chp);
820 }
821 #endif
822