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