xref: /netbsd-src/sys/dev/ata/ata_wdc.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*	$NetBSD: ata_wdc.c,v 1.118 2020/05/21 09:11:33 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001, 2003 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*-
28  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
29  * All rights reserved.
30  *
31  * This code is derived from software contributed to The NetBSD Foundation
32  * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
44  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
45  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
47  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53  * POSSIBILITY OF SUCH DAMAGE.
54  */
55 
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: ata_wdc.c,v 1.118 2020/05/21 09:11:33 jdolecek Exp $");
58 
59 #include "opt_ata.h"
60 #include "opt_wdc.h"
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/file.h>
66 #include <sys/stat.h>
67 #include <sys/buf.h>
68 #include <sys/bufq.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/syslog.h>
72 #include <sys/proc.h>
73 
74 #include <sys/intr.h>
75 #include <sys/bus.h>
76 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
77 #define    bus_space_write_multi_stream_2    bus_space_write_multi_2
78 #define    bus_space_write_multi_stream_4    bus_space_write_multi_4
79 #define    bus_space_read_multi_stream_2    bus_space_read_multi_2
80 #define    bus_space_read_multi_stream_4    bus_space_read_multi_4
81 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
82 
83 #include <dev/ata/ataconf.h>
84 #include <dev/ata/atareg.h>
85 #include <dev/ata/atavar.h>
86 #include <dev/ic/wdcreg.h>
87 #include <dev/ic/wdcvar.h>
88 
89 #define DEBUG_INTR   0x01
90 #define DEBUG_XFERS  0x02
91 #define DEBUG_STATUS 0x04
92 #define DEBUG_FUNCS  0x08
93 #define DEBUG_PROBE  0x10
94 #ifdef ATADEBUG
95 extern int wdcdebug_wd_mask; /* inited in wd.c */
96 #define ATADEBUG_PRINT(args, level) \
97 	if (wdcdebug_wd_mask & (level)) \
98 		printf args
99 #else
100 #define ATADEBUG_PRINT(args, level)
101 #endif
102 
103 #define ATA_DELAY 10000 /* 10s for a drive I/O */
104 
105 static void	wdc_ata_bio(struct ata_drive_datas*, struct ata_xfer *);
106 static int	wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
107 static int	_wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
108 static void	wdc_ata_bio_poll(struct ata_channel *,struct ata_xfer *);
109 static int	wdc_ata_bio_intr(struct ata_channel *, struct ata_xfer *,
110 				 int);
111 static void	wdc_ata_bio_kill_xfer(struct ata_channel *,
112 				      struct ata_xfer *, int);
113 static void	wdc_ata_bio_done(struct ata_channel *, struct ata_xfer *);
114 static int	wdc_ata_err(struct ata_drive_datas *, struct ata_bio *, int);
115 #define WDC_ATA_NOERR 0x00 /* Drive doesn't report an error */
116 #define WDC_ATA_RECOV 0x01 /* There was a recovered error */
117 #define WDC_ATA_ERR   0x02 /* Drive reports an error */
118 static int	wdc_ata_addref(struct ata_drive_datas *);
119 static void	wdc_ata_delref(struct ata_drive_datas *);
120 
121 const struct ata_bustype wdc_ata_bustype = {
122 	SCSIPI_BUSTYPE_ATA,
123 	wdc_ata_bio,
124 	wdc_reset_drive,
125 	wdc_reset_channel,
126 	wdc_exec_command,
127 	ata_get_params,
128 	wdc_ata_addref,
129 	wdc_ata_delref,
130 	ata_kill_pending,
131 	NULL,
132 };
133 
134 static const struct ata_xfer_ops wdc_bio_xfer_ops = {
135 	.c_start = wdc_ata_bio_start,
136 	.c_poll = wdc_ata_bio_poll,
137 	.c_abort = wdc_ata_bio_done,
138 	.c_intr = wdc_ata_bio_intr,
139 	.c_kill_xfer = wdc_ata_bio_kill_xfer
140 };
141 
142 /*
143  * Handle block I/O operation.
144  */
145 static void
146 wdc_ata_bio(struct ata_drive_datas *drvp, struct ata_xfer *xfer)
147 {
148 	struct ata_channel *chp = drvp->chnl_softc;
149 	struct atac_softc *atac = chp->ch_atac;
150 	struct ata_bio *ata_bio = &xfer->c_bio;
151 
152 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
153 		ata_bio->flags |= ATA_POLL;
154 	if (ata_bio->flags & ATA_POLL)
155 		xfer->c_flags |= C_POLL;
156 #if NATA_DMA
157 	if ((drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) &&
158 	    (ata_bio->flags & ATA_SINGLE) == 0)
159 		xfer->c_flags |= C_DMA;
160 #endif
161 #if NATA_DMA && NATA_PIOBM
162 	else
163 #endif
164 #if NATA_PIOBM
165 	if (atac->atac_cap & ATAC_CAP_PIOBM)
166 		xfer->c_flags |= C_PIOBM;
167 #endif
168 	xfer->c_drive = drvp->drive;
169 	xfer->c_databuf = ata_bio->databuf;
170 	xfer->c_bcount = ata_bio->bcount;
171 	xfer->ops = &wdc_bio_xfer_ops;
172 	ata_exec_xfer(chp, xfer);
173 }
174 
175 static int
176 wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
177 {
178 	struct atac_softc *atac = chp->ch_atac;
179 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
180 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
181 	struct ata_bio *ata_bio = &xfer->c_bio;
182 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
183 	int wait_flags, tfd;
184 	const char *errstring;
185 #ifdef WDC_NO_IDS
186 	wait_flags = AT_POLL;
187 #else
188 	wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
189 #endif
190 
191 	ATADEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d state %d drive_flags 0x%x "
192 	    "c_flags 0x%x ch_flags 0x%x\n",
193 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
194 	    drvp->state, drvp->drive_flags, xfer->c_flags, chp->ch_flags),
195 	    DEBUG_XFERS);
196 
197 	ata_channel_lock_owned(chp);
198 
199 	/* Do control operations specially. */
200 	if (__predict_false(drvp->state < READY)) {
201 		/*
202 		 * Actually, we want to be careful not to mess with the control
203 		 * state if the device is currently busy, but we can assume
204 		 * that we never get to this point if that's the case.
205 		 */
206 		/* If it's not a polled command, we need the kernel thread */
207 		if ((xfer->c_flags & C_POLL) == 0 && !ata_is_thread_run(chp))
208 			return ATASTART_TH;
209 
210 		/*
211 		 * disable interrupts, all commands here should be quick
212 		 * enough to be able to poll, and we don't go here that often
213 		 */
214 		if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
215 			bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh,
216 			    wd_aux_ctlr, WDCTL_4BIT | WDCTL_IDS);
217 		if (wdc->select)
218 			wdc->select(chp, xfer->c_drive);
219 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
220 		    WDSD_IBM | (xfer->c_drive << 4));
221 		DELAY(10);
222 		errstring = "wait";
223 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
224 		    &tfd))
225 			goto ctrltimeout;
226 		wdccommandshort(chp, xfer->c_drive, WDCC_RECAL);
227 		/* Wait for at last 400ns for status bit to be valid */
228 		DELAY(1);
229 		errstring = "recal";
230 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
231 		    &tfd))
232 			goto ctrltimeout;
233 		if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF))
234 			goto ctrlerror;
235 		/* Don't try to set modes if controller can't be adjusted */
236 		if (atac->atac_set_modes == NULL)
237 			goto geometry;
238 		/* Also don't try if the drive didn't report its mode */
239 		if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0)
240 			goto geometry;
241 		wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
242 		    0x08 | drvp->PIO_mode, WDSF_SET_MODE);
243 		errstring = "piomode";
244 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
245 		    &tfd))
246 			goto ctrltimeout;
247 		if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF))
248 			goto ctrlerror;
249 #if NATA_DMA
250 #if NATA_UDMA
251 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
252 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
253 			    0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
254 		} else
255 #endif
256 		if (drvp->drive_flags & ATA_DRIVE_DMA) {
257 			wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
258 			    0x20 | drvp->DMA_mode, WDSF_SET_MODE);
259 		} else {
260 			goto geometry;
261 		}
262 		errstring = "dmamode";
263 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
264 		    &tfd))
265 			goto ctrltimeout;
266 		if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF))
267 			goto ctrlerror;
268 #endif	/* NATA_DMA */
269 geometry:
270 		if (ata_bio->flags & ATA_LBA)
271 			goto multimode;
272 		wdccommand(chp, xfer->c_drive, WDCC_IDP,
273 		    drvp->lp->d_ncylinders,
274 		    drvp->lp->d_ntracks - 1, 0, drvp->lp->d_nsectors,
275 		    (drvp->lp->d_type == DKTYPE_ST506) ?
276 			drvp->lp->d_precompcyl / 4 : 0);
277 		errstring = "geometry";
278 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
279 		    &tfd))
280 			goto ctrltimeout;
281 		if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF))
282 			goto ctrlerror;
283 multimode:
284 		if (drvp->multi == 1)
285 			goto ready;
286 		wdccommand(chp, xfer->c_drive, WDCC_SETMULTI, 0, 0, 0,
287 		    drvp->multi, 0);
288 		errstring = "setmulti";
289 		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags,
290 		    &tfd))
291 			goto ctrltimeout;
292 		if (ATACH_ST(tfd) & (WDCS_ERR | WDCS_DWF))
293 			goto ctrlerror;
294 ready:
295 		drvp->state = READY;
296 		/*
297 		 * The drive is usable now
298 		 */
299 		if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
300 			bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh,
301 			    wd_aux_ctlr, WDCTL_4BIT);
302 		delay(10); /* some drives need a little delay here */
303 	}
304 
305 	return _wdc_ata_bio_start(chp, xfer);
306 ctrltimeout:
307 	printf("%s:%d:%d: %s timed out\n",
308 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
309 	    errstring);
310 	ata_bio->error = TIMEOUT;
311 	goto ctrldone;
312 ctrlerror:
313 	printf("%s:%d:%d: %s ",
314 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
315 	    errstring);
316 	if (ATACH_ST(tfd) & WDCS_DWF) {
317 		printf("drive fault\n");
318 		ata_bio->error = ERR_DF;
319 	} else {
320 		ata_bio->r_error = ATACH_ERR(tfd);
321 		ata_bio->error = ERROR;
322 		printf("error (%x)\n", ata_bio->r_error);
323 	}
324 ctrldone:
325 	drvp->state = 0;
326 
327 	if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
328 		bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
329 		    WDCTL_4BIT);
330 	return ATASTART_ABORT;
331 }
332 
333 static int
334 _wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
335 {
336 	struct atac_softc *atac = chp->ch_atac;
337 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
338 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
339 	struct ata_bio *ata_bio = &xfer->c_bio;
340 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
341 	int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
342 	uint16_t cyl;
343 	uint8_t head, sect, cmd = 0;
344 	int nblks, tfd;
345 #if NATA_DMA || NATA_PIOBM
346 	int error, dma_flags = 0;
347 #endif
348 
349 	ATADEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",
350 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
351 	    DEBUG_INTR | DEBUG_XFERS);
352 
353 #if NATA_DMA || NATA_PIOBM
354 	if (xfer->c_flags & (C_DMA | C_PIOBM)) {
355 #if NATA_DMA
356 		if (drvp->n_xfers <= NXFER)
357 			drvp->n_xfers++;
358 #endif
359 		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;
360 		if (ata_bio->flags & ATA_LBA48)
361 			dma_flags |= WDC_DMA_LBA48;
362 	}
363 #endif
364 	/*
365 	 *
366 	 * When starting a multi-sector transfer, or doing single-sector
367 	 * transfers...
368 	 */
369 	if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
370 		if (ata_bio->flags & ATA_SINGLE)
371 			nblks = 1;
372 		else
373 			nblks = xfer->c_bcount / drvp->lp->d_secsize;
374 		/* Check for bad sectors and adjust transfer, if necessary. */
375 		if ((drvp->lp->d_flags & D_BADSECT) != 0) {
376 			long blkdiff;
377 			int i;
378 			for (i = 0; (blkdiff = drvp->badsect[i]) != -1;
379 			    i++) {
380 				blkdiff -= ata_bio->blkno;
381 				if (blkdiff < 0)
382 					continue;
383 				if (blkdiff == 0) {
384 					/* Replace current block of transfer. */
385 					ata_bio->blkno =
386 					    drvp->lp->d_secperunit -
387 					    drvp->lp->d_nsectors - i - 1;
388 				}
389 				if (blkdiff < nblks) {
390 					/* Bad block inside transfer. */
391 					ata_bio->flags |= ATA_SINGLE;
392 					nblks = 1;
393 				}
394 				break;
395 			}
396 		/* Transfer is okay now. */
397 		}
398 		if (ata_bio->flags & ATA_LBA48) {
399 			sect = 0;
400 			cyl =  0;
401 			head = 0;
402 		} else if (ata_bio->flags & ATA_LBA) {
403 			sect = (ata_bio->blkno >> 0) & 0xff;
404 			cyl = (ata_bio->blkno >> 8) & 0xffff;
405 			head = (ata_bio->blkno >> 24) & 0x0f;
406 			head |= WDSD_LBA;
407 		} else {
408 			int blkno = ata_bio->blkno;
409 			sect = blkno % drvp->lp->d_nsectors;
410 			sect++;    /* Sectors begin with 1, not 0. */
411 			blkno /= drvp->lp->d_nsectors;
412 			head = blkno % drvp->lp->d_ntracks;
413 			blkno /= drvp->lp->d_ntracks;
414 			cyl = blkno;
415 			head |= WDSD_CHS;
416 		}
417 #if NATA_DMA
418 		if (xfer->c_flags & C_DMA) {
419 			uint16_t count = nblks, features = 0;
420 
421 			ata_bio->nblks = nblks;
422 			ata_bio->nbytes = xfer->c_bcount;
423 			cmd = (ata_bio->flags & ATA_READ) ?
424 			    WDCC_READDMA : WDCC_WRITEDMA;
425 	    		/* Init the DMA channel. */
426 			error = (*wdc->dma_init)(wdc->dma_arg,
427 			    chp->ch_channel, xfer->c_drive,
428 			    (char *)xfer->c_databuf + xfer->c_skip,
429 			    ata_bio->nbytes, dma_flags);
430 			if (error) {
431 				if (error == EINVAL) {
432 					/*
433 					 * We can't do DMA on this transfer
434 					 * for some reason.  Fall back to
435 					 * PIO.
436 					 */
437 					xfer->c_flags &= ~C_DMA;
438 					error = 0;
439 					goto do_pio;
440 				}
441 				ata_bio->error = ERR_DMA;
442 				ata_bio->r_error = 0;
443 				return ATASTART_ABORT;
444 			}
445 			/* Initiate command */
446 			if (wdc->select)
447 				wdc->select(chp, xfer->c_drive);
448 			bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
449 			    0, WDSD_IBM | (xfer->c_drive << 4));
450 			switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags,
451 			    &tfd)) {
452 			case WDCWAIT_OK:
453 				break;
454 			case WDCWAIT_TOUT:
455 				goto timeout;
456 			case WDCWAIT_THR:
457 				return ATASTART_TH;
458 			}
459 			/* start the DMA channel before */
460 			if ((chp->ch_flags & ATACH_DMA_BEFORE_CMD) != 0)
461 				(*wdc->dma_start)(wdc->dma_arg,
462 				    chp->ch_channel, xfer->c_drive);
463 			if (ata_bio->flags & ATA_LBA48) {
464 			    uint8_t device = WDSD_LBA;
465 			    cmd = atacmd_to48(cmd);
466 
467 			    atacmd_toncq(xfer, &cmd, &count, &features,
468 			        &device);
469 
470 			    wdccommandext(chp, xfer->c_drive, cmd,
471 				ata_bio->blkno, count, features, device);
472 			} else {
473 			    wdccommand(chp, xfer->c_drive, cmd, cyl,
474 				head, sect, count, features);
475 			}
476 			/* start the DMA channel after */
477 			if ((chp->ch_flags & ATACH_DMA_BEFORE_CMD) == 0)
478 				(*wdc->dma_start)(wdc->dma_arg,
479 				    chp->ch_channel, xfer->c_drive);
480 			chp->ch_flags |= ATACH_DMA_WAIT;
481 			/* wait for irq */
482 			goto intr;
483 		} /* else not DMA */
484  do_pio:
485 #endif	/* NATA_DMA */
486 #if NATA_PIOBM
487 		if ((xfer->c_flags & C_PIOBM) && xfer->c_skip == 0) {
488 			if (ata_bio->flags & ATA_POLL) {
489 				/* XXX not supported yet --- fall back to PIO */
490 				xfer->c_flags &= ~C_PIOBM;
491 			} else {
492 				/* Init the DMA channel. */
493 				error = (*wdc->dma_init)(wdc->dma_arg,
494 				    chp->ch_channel, xfer->c_drive,
495 				    (char *)xfer->c_databuf + xfer->c_skip,
496 				    xfer->c_bcount,
497 				    dma_flags | WDC_DMA_PIOBM_ATA);
498 				if (error) {
499 					if (error == EINVAL) {
500 						/*
501 						 * We can't do DMA on this
502 						 * transfer for some reason.
503 						 * Fall back to PIO.
504 						 */
505 						xfer->c_flags &= ~C_PIOBM;
506 						error = 0;
507 					} else {
508 						ata_bio->error = ERR_DMA;
509 						ata_bio->r_error = 0;
510 						return ATASTART_ABORT;
511 					}
512 				}
513 			}
514 		}
515 #endif
516 		ata_bio->nblks = uimin(nblks, drvp->multi);
517 		ata_bio->nbytes = ata_bio->nblks * drvp->lp->d_secsize;
518 		KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0);
519 		if (ata_bio->nblks > 1) {
520 			cmd = (ata_bio->flags & ATA_READ) ?
521 			    WDCC_READMULTI : WDCC_WRITEMULTI;
522 		} else {
523 			cmd = (ata_bio->flags & ATA_READ) ?
524 			    WDCC_READ : WDCC_WRITE;
525 		}
526 		/* Initiate command! */
527 		if (wdc->select)
528 			wdc->select(chp, xfer->c_drive);
529 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
530 		    WDSD_IBM | (xfer->c_drive << 4));
531 		switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags, &tfd)) {
532 		case WDCWAIT_OK:
533 			break;
534 		case WDCWAIT_TOUT:
535 			goto timeout;
536 		case WDCWAIT_THR:
537 			return ATASTART_TH;
538 		}
539 		if (ata_bio->flags & ATA_LBA48) {
540 		    wdccommandext(chp, xfer->c_drive, atacmd_to48(cmd),
541 			ata_bio->blkno, nblks, 0, WDSD_LBA);
542 		} else {
543 		    wdccommand(chp, xfer->c_drive, cmd, cyl,
544 			head, sect, nblks,
545 			(drvp->lp->d_type == DKTYPE_ST506) ?
546 			drvp->lp->d_precompcyl / 4 : 0);
547 		}
548 	} else if (ata_bio->nblks > 1) {
549 		/* The number of blocks in the last stretch may be smaller. */
550 		nblks = xfer->c_bcount / drvp->lp->d_secsize;
551 		if (ata_bio->nblks > nblks) {
552 			ata_bio->nblks = nblks;
553 			ata_bio->nbytes = xfer->c_bcount;
554 		}
555 	}
556 	/* If this was a write and not using DMA, push the data. */
557 	if ((ata_bio->flags & ATA_READ) == 0) {
558 		/*
559 		 * we have to busy-wait here, we can't rely on running in
560 		 * thread context.
561 		 */
562 		if (wdc_wait_for_drq(chp, ATA_DELAY, AT_POLL, &tfd) != 0) {
563 			printf("%s:%d:%d: timeout waiting for DRQ, "
564 			    "st=0x%02x, err=0x%02x\n",
565 			    device_xname(atac->atac_dev), chp->ch_channel,
566 			    xfer->c_drive,
567 			    ATACH_ST(tfd), ATACH_ERR(tfd));
568 			if (wdc_ata_err(drvp, ata_bio, tfd) != WDC_ATA_ERR)
569 				ata_bio->error = TIMEOUT;
570 			return ATASTART_ABORT;
571 		}
572 		if (wdc_ata_err(drvp, ata_bio, tfd) == WDC_ATA_ERR) {
573 			return ATASTART_ABORT;
574 		}
575 #if NATA_PIOBM
576 		if (xfer->c_flags & C_PIOBM) {
577 			/* start the busmastering PIO */
578 			(*wdc->piobm_start)(wdc->dma_arg,
579 			    chp->ch_channel, xfer->c_drive,
580 			    xfer->c_skip, ata_bio->nbytes, 0);
581 			chp->ch_flags |= ATACH_DMA_WAIT;
582 		} else
583 #endif
584 
585 		wdc->dataout_pio(chp, drvp->drive_flags,
586 		    (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
587 	}
588 
589 #if NATA_DMA
590 intr:
591 #endif
592 	/* Wait for IRQ (either real or polled) */
593 	if ((xfer->c_flags & C_POLL) == 0) {
594 		/* start timeout machinery */
595 		callout_reset(&chp->c_timo_callout,
596 		    ATA_DELAY / 1000 * hz, wdctimeout, chp);
597 		chp->ch_flags |= ATACH_IRQ_WAIT;
598 		return ATASTART_STARTED;
599 	} else {
600 		return ATASTART_POLL;
601 	}
602 
603 timeout:
604 	printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",
605 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
606 	    ATACH_ST(tfd), ATACH_ERR(tfd));
607 	if (wdc_ata_err(drvp, ata_bio, tfd) != WDC_ATA_ERR)
608 		ata_bio->error = TIMEOUT;
609 	return ATASTART_ABORT;
610 }
611 
612 static void
613 wdc_ata_bio_poll(struct ata_channel *chp, struct ata_xfer *xfer)
614 {
615 	/* Wait for at last 400ns for status bit to be valid */
616 	delay(1);
617 #if NATA_DMA
618 	if (chp->ch_flags & ATACH_DMA_WAIT) {
619 		wdc_dmawait(chp, xfer, ATA_DELAY);
620 		chp->ch_flags &= ~ATACH_DMA_WAIT;
621 	}
622 #endif
623 	wdc_ata_bio_intr(chp, xfer, 0);
624 }
625 
626 static int
627 wdc_ata_bio_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
628 {
629 	struct atac_softc *atac = chp->ch_atac;
630 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
631 	struct ata_bio *ata_bio = &xfer->c_bio;
632 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
633 	int drv_err, tfd;
634 
635 	ATADEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
636 	    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
637 	    DEBUG_INTR | DEBUG_XFERS);
638 
639 	ata_channel_lock(chp);
640 
641 	/* Is it not a transfer, but a control operation? */
642 	if (drvp->state < READY) {
643 		printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
644 		    device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
645 		    drvp->state);
646 		panic("wdc_ata_bio_intr: bad state");
647 	}
648 
649 	/*
650 	 * if we missed an interrupt in a PIO transfer, reset and restart.
651 	 * Don't try to continue transfer, we may have missed cycles.
652 	 */
653 	if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
654 		ata_bio->error = TIMEOUT;
655 		goto err;
656 	}
657 
658 #if NATA_PIOBM
659 	/* Transfer-done interrupt for busmastering PIO read */
660 	if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
661 		chp->ch_flags &= ~ATACH_PIOBM_WAIT;
662 		goto end;
663 	}
664 #endif
665 
666 	/* Ack interrupt done by wdc_wait_for_unbusy */
667 	if (wdc_wait_for_unbusy(chp,
668 	    (irq == 0) ? ATA_DELAY : 0, AT_POLL, &tfd) < 0) {
669 		if (irq && (xfer->c_flags & C_TIMEOU) == 0) {
670 			ata_channel_unlock(chp);
671 			return 0; /* IRQ was not for us */
672 		}
673 		printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
674 		    device_xname(atac->atac_dev), chp->ch_channel,
675 		    xfer->c_drive, xfer->c_bcount, xfer->c_skip);
676 		ata_bio->error = TIMEOUT;
677 		goto err;
678 	}
679 	if (wdc->irqack)
680 		wdc->irqack(chp);
681 
682 	drv_err = wdc_ata_err(drvp, ata_bio, tfd);
683 
684 #if NATA_DMA
685 	/* If we were using DMA, Turn off the DMA channel and check for error */
686 	if (xfer->c_flags & C_DMA) {
687 		if (ata_bio->flags & ATA_POLL) {
688 			/*
689 			 * IDE drives deassert WDCS_BSY before transfer is
690 			 * complete when using DMA. Polling for DRQ to deassert
691 			 * is not enough DRQ is not required to be
692 			 * asserted for DMA transfers, so poll for DRDY.
693 			 */
694 			if (wdcwait(chp, WDCS_DRDY | WDCS_DRQ, WDCS_DRDY,
695 			    ATA_DELAY, ATA_POLL, &tfd) == WDCWAIT_TOUT) {
696 				printf("%s:%d:%d: polled transfer timed out "
697 				    "(st=0x%x)\n",
698 				    device_xname(atac->atac_dev),
699 				    chp->ch_channel, xfer->c_drive,
700 				    ATACH_ST(tfd));
701 				ata_bio->error = TIMEOUT;
702 				drv_err = WDC_ATA_ERR;
703 			}
704 		}
705 		if (wdc->dma_status != 0) {
706 			if (drv_err != WDC_ATA_ERR) {
707 				ata_bio->error = ERR_DMA;
708 				drv_err = WDC_ATA_ERR;
709 			}
710 		}
711 		if (ATACH_ST(tfd) & WDCS_DRQ) {
712 			if (drv_err != WDC_ATA_ERR) {
713 				printf("%s:%d:%d: intr with DRQ (st=0x%x)\n",
714 				    device_xname(atac->atac_dev),
715 				    chp->ch_channel,
716 				    xfer->c_drive, ATACH_ST(tfd));
717 				ata_bio->error = TIMEOUT;
718 				drv_err = WDC_ATA_ERR;
719 			}
720 		}
721 		if (drv_err != WDC_ATA_ERR)
722 			goto end;
723 		if (ata_bio->r_error & WDCE_CRC || ata_bio->error == ERR_DMA) {
724 			ata_dmaerr(drvp,
725 			    (xfer->c_flags & C_POLL) ? AT_POLL : 0);
726 			goto err;
727 		}
728 	}
729 #endif	/* NATA_DMA */
730 
731 	/* if we had an error, end */
732 	if (drv_err == WDC_ATA_ERR)
733 		goto err;
734 
735 	/* If this was a read and not using DMA, fetch the data. */
736 	if ((ata_bio->flags & ATA_READ) != 0) {
737 		if ((ATACH_ST(tfd) & WDCS_DRQ) != WDCS_DRQ) {
738 			printf("%s:%d:%d: read intr before drq\n",
739 			    device_xname(atac->atac_dev), chp->ch_channel,
740 			    xfer->c_drive);
741 			ata_bio->error = TIMEOUT;
742 			goto err;
743 		}
744 #if NATA_PIOBM
745 		if (xfer->c_flags & C_PIOBM) {
746 			/* start the busmastering PIO */
747 			(*wdc->piobm_start)(wdc->dma_arg,
748 			    chp->ch_channel, xfer->c_drive,
749 			    xfer->c_skip, ata_bio->nbytes,
750 			    WDC_PIOBM_XFER_IRQ);
751 			chp->ch_flags |= ATACH_DMA_WAIT | ATACH_PIOBM_WAIT;
752 			ata_channel_unlock(chp);
753 			return 1;
754 		}
755 #endif
756 		wdc->datain_pio(chp, drvp->drive_flags,
757 		    (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
758 	}
759 
760 #if NATA_DMA || NATA_PIOBM
761 end:
762 #endif
763 	ata_bio->blkno += ata_bio->nblks;
764 	ata_bio->blkdone += ata_bio->nblks;
765 	xfer->c_skip += ata_bio->nbytes;
766 	xfer->c_bcount -= ata_bio->nbytes;
767 
768 	/* See if this transfer is complete. */
769 	if (xfer->c_bcount > 0) {
770 		if ((ata_bio->flags & ATA_POLL) == 0) {
771 			/* Start the next operation */
772 			KASSERT((chp->ch_flags & ATACH_IRQ_WAIT) == 0);
773 			callout_stop(&chp->c_timo_callout);
774 			ata_xfer_start(xfer);
775 		} else {
776 			/* Let _wdc_ata_bio_start do the loop */
777 		}
778 		ata_channel_unlock(chp);
779 		return 1;
780 	}
781 
782 	/* Done with this transfer */
783 	ata_bio->error = NOERROR;
784 err:	ata_channel_unlock(chp);
785 	wdc_ata_bio_done(chp, xfer);
786 	return 1;
787 }
788 
789 static void
790 wdc_ata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
791     int reason)
792 {
793 	struct ata_bio *ata_bio = &xfer->c_bio;
794 	int drive = xfer->c_drive;
795 	bool deactivate = true;
796 
797 	ata_bio->flags |= ATA_ITSDONE;
798 	switch (reason) {
799 	case KILL_GONE_INACTIVE:
800 		deactivate = false;
801 		/* FALLTHROUGH */
802 	case KILL_GONE:
803 		ata_bio->error = ERR_NODEV;
804 		break;
805 	case KILL_RESET:
806 		ata_bio->error = ERR_RESET;
807 		break;
808 	default:
809 		printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
810 		    reason);
811 		panic("wdc_ata_bio_kill_xfer");
812 	}
813 	ata_bio->r_error = WDCE_ABRT;
814 
815 	if (deactivate)
816 		ata_deactivate_xfer(chp, xfer);
817 
818 	ATADEBUG_PRINT(("wdc_ata_bio_kill_xfer: drv_done\n"), DEBUG_XFERS);
819 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
820 }
821 
822 static void
823 wdc_ata_bio_done(struct ata_channel *chp, struct ata_xfer *xfer)
824 {
825 	struct ata_bio *ata_bio = &xfer->c_bio;
826 	int drive = xfer->c_drive;
827 
828 	ATADEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
829 	    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
830 	    xfer->c_drive, (u_int)xfer->c_flags),
831 	    DEBUG_XFERS);
832 
833 	if (ata_waitdrain_xfer_check(chp, xfer))
834 		return;
835 
836 	/* feed back residual bcount to our caller */
837 	ata_bio->bcount = xfer->c_bcount;
838 
839 	/* mark controller inactive and free xfer */
840 	ata_deactivate_xfer(chp, xfer);
841 
842 	ata_bio->flags |= ATA_ITSDONE;
843 	ATADEBUG_PRINT(("wdc_ata_done: drv_done\n"), DEBUG_XFERS);
844 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
845 	ATADEBUG_PRINT(("atastart from wdc_ata_done, flags 0x%x\n",
846 	    chp->ch_flags), DEBUG_XFERS);
847 	atastart(chp);
848 }
849 
850 static int
851 wdc_ata_err(struct ata_drive_datas *drvp, struct ata_bio *ata_bio, int tfd)
852 {
853 	ata_bio->error = 0;
854 	if (ATACH_ST(tfd) & WDCS_BSY) {
855 		ata_bio->error = TIMEOUT;
856 		return WDC_ATA_ERR;
857 	}
858 
859 	if (ATACH_ST(tfd) & WDCS_DWF) {
860 		ata_bio->error = ERR_DF;
861 		return WDC_ATA_ERR;
862 	}
863 
864 	if (ATACH_ST(tfd) & WDCS_ERR) {
865 		ata_bio->error = ERROR;
866 		ata_bio->r_error = ATACH_ERR(tfd);
867 		if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
868 		    WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
869 			return WDC_ATA_ERR;
870 		return WDC_ATA_NOERR;
871 	}
872 
873 	if (ATACH_ST(tfd) & WDCS_CORR)
874 		ata_bio->flags |= ATA_CORR;
875 	return WDC_ATA_NOERR;
876 }
877 
878 static int
879 wdc_ata_addref(struct ata_drive_datas *drvp)
880 {
881 	struct ata_channel *chp = drvp->chnl_softc;
882 
883 	return (ata_addref(chp));
884 }
885 
886 static void
887 wdc_ata_delref(struct ata_drive_datas *drvp)
888 {
889 	struct ata_channel *chp = drvp->chnl_softc;
890 
891 	ata_delref(chp);
892 }
893