1 /* $NetBSD: ata_wdc.c,v 1.89 2008/04/28 20:23:47 martin Exp $ */
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
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.
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
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 $");
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
73 #include <sys/malloc.h>
74 #include <sys/device.h>
75 #include <sys/disklabel.h>
76 #include <sys/syslog.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
100 extern int wdcdebug_wd_mask
; /* inited in wd.c */
101 #define ATADEBUG_PRINT(args, level) \
102 if (wdcdebug_wd_mask & (level)) \
105 #define ATADEBUG_PRINT(args, level)
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
*,
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
= {
138 * Handle block I/O operation. Return ATACMD_COMPLETE, ATACMD_QUEUED, or
139 * ATACMD_TRY_AGAIN. Must be called at splbio().
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
);
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
;
156 if ((drvp
->drive_flags
& (DRIVE_DMA
| DRIVE_UDMA
)) &&
157 (ata_bio
->flags
& ATA_SINGLE
) == 0)
158 xfer
->c_flags
|= C_DMA
;
160 #if NATA_DMA && NATA_PIOBM
164 if (atac
->atac_cap
& ATAC_CAP_PIOBM
)
165 xfer
->c_flags
|= C_PIOBM
;
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
;
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
),
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
);
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
);
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));
219 if (wdcwait(chp
, WDCS_DRDY
, WDCS_DRDY
, ATA_DELAY
, wait_flags
))
221 wdccommandshort(chp
, xfer
->c_drive
, WDCC_RECAL
);
222 /* Wait for at last 400ns for status bit to be valid */
225 if (wdcwait(chp
, WDCS_DRDY
, WDCS_DRDY
, ATA_DELAY
, wait_flags
))
227 if (chp
->ch_status
& (WDCS_ERR
| WDCS_DWF
))
229 /* Don't try to set modes if controller can't be adjusted */
230 if (atac
->atac_set_modes
== NULL
)
232 /* Also don't try if the drive didn't report its mode */
233 if ((drvp
->drive_flags
& DRIVE_MODE
) == 0)
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
))
240 if (chp
->ch_status
& (WDCS_ERR
| WDCS_DWF
))
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
);
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
);
255 errstring
= "dmamode";
256 if (wdcwait(chp
, WDCS_DRDY
, WDCS_DRDY
, ATA_DELAY
, wait_flags
))
258 if (chp
->ch_status
& (WDCS_ERR
| WDCS_DWF
))
260 #endif /* NATA_DMA */
262 if (ata_bio
->flags
& ATA_LBA
)
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
))
272 if (chp
->ch_status
& (WDCS_ERR
| WDCS_DWF
))
275 if (ata_bio
->multi
== 1)
277 wdccommand(chp
, xfer
->c_drive
, WDCC_SETMULTI
, 0, 0, 0,
279 errstring
= "setmulti";
280 if (wdcwait(chp
, WDCS_DRDY
, WDCS_DRDY
, ATA_DELAY
, wait_flags
))
282 if (chp
->ch_status
& (WDCS_ERR
| WDCS_DWF
))
287 * The drive is usable now
289 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
,
291 delay(10); /* some drives need a little delay here */
294 _wdc_ata_bio_start(chp
, xfer
);
297 printf("%s:%d:%d: %s timed out\n",
298 device_xname(atac
->atac_dev
), chp
->ch_channel
, xfer
->c_drive
,
300 ata_bio
->error
= TIMEOUT
;
303 printf("%s:%d:%d: %s ",
304 device_xname(atac
->atac_dev
), chp
->ch_channel
, xfer
->c_drive
,
306 if (chp
->ch_status
& WDCS_DWF
) {
307 printf("drive fault\n");
308 ata_bio
->error
= ERR_DF
;
310 printf("error (%x)\n", chp
->ch_error
);
311 ata_bio
->r_error
= chp
->ch_error
;
312 ata_bio
->error
= ERROR
;
316 wdc_ata_bio_done(chp
, xfer
);
317 bus_space_write_1(wdr
->ctl_iot
, wdr
->ctl_ioh
, wd_aux_ctlr
, WDCTL_4BIT
);
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;
331 u_int8_t head
, sect
, cmd
= 0;
333 #if NATA_DMA || NATA_PIOBM
334 int error
, dma_flags
= 0;
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
)) {
344 if (drvp
->n_xfers
<= NXFER
)
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
;
355 * When starting a multi-sector transfer, or doing single-sector
358 if (xfer
->c_skip
== 0 || (ata_bio
->flags
& ATA_SINGLE
) != 0) {
359 if (ata_bio
->flags
& ATA_SINGLE
)
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) {
367 for (i
= 0; (blkdiff
= ata_bio
->badsect
[i
]) != -1;
369 blkdiff
-= ata_bio
->blkno
;
373 /* Replace current block of transfer. */
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
;
385 /* Transfer is okay now. */
387 if (ata_bio
->flags
& ATA_LBA48
) {
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;
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
;
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
);
418 if (error
== EINVAL
) {
420 * We can't do DMA on this transfer
421 * for some reason. Fall back to
424 xfer
->c_flags
&= ~C_DMA
;
428 ata_bio
->error
= ERR_DMA
;
429 ata_bio
->r_error
= 0;
430 wdc_ata_bio_done(chp
, xfer
);
433 /* Initiate command */
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
)) {
446 if (ata_bio
->flags
& ATA_LBA48
) {
447 wdccommandext(chp
, xfer
->c_drive
, atacmd_to48(cmd
),
448 (u_int64_t
)ata_bio
->blkno
, nblks
);
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
);
465 #endif /* NATA_DMA */
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
;
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
,
477 dma_flags
| WDC_DMA_PIOBM_ATA
);
479 if (error
== EINVAL
) {
481 * We can't do DMA on this
482 * transfer for some reason.
485 xfer
->c_flags
&= ~C_PIOBM
;
488 ata_bio
->error
= ERR_DMA
;
489 ata_bio
->r_error
= 0;
490 wdc_ata_bio_done(chp
, xfer
);
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
;
504 cmd
= (ata_bio
->flags
& ATA_READ
) ?
505 WDCC_READ
: WDCC_WRITE
;
507 /* Initiate command! */
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
)) {
520 if (ata_bio
->flags
& ATA_LBA48
) {
521 wdccommandext(chp
, xfer
->c_drive
, atacmd_to48(cmd
),
522 (u_int64_t
) ata_bio
->blkno
, nblks
);
524 wdccommand(chp
, xfer
->c_drive
, cmd
, cyl
,
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
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
);
557 if (wdc_ata_err(drvp
, ata_bio
) == WDC_ATA_ERR
) {
558 wdc_ata_bio_done(chp
, xfer
);
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
;
571 wdc
->dataout_pio(chp
, drvp
->drive_flags
,
572 (char *)xfer
->c_databuf
+ xfer
->c_skip
, ata_bio
->nbytes
);
578 /* Wait for IRQ (either real or polled) */
579 if ((ata_bio
->flags
& ATA_POLL
) == 0) {
580 chp
->ch_flags
|= ATACH_IRQ_WAIT
;
582 /* Wait for at last 400ns for status bit to be valid */
585 if (chp
->ch_flags
& ATACH_DMA_WAIT
) {
586 wdc_dmawait(chp
, xfer
, ATA_DELAY
);
587 chp
->ch_flags
&= ~ATACH_DMA_WAIT
;
590 wdc_ata_bio_intr(chp
, xfer
, 0);
591 if ((ata_bio
->flags
& ATA_ITSDONE
) == 0)
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
);
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
];
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
,
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
);
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
;
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
);
659 drv_err
= wdc_ata_err(drvp
, ata_bio
);
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 "
675 device_xname(atac
->atac_dev
),
676 chp
->ch_channel
, xfer
->c_drive
,
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
),
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
)
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
);
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
,
717 ata_bio
->error
= TIMEOUT
;
718 wdc_ata_bio_done(chp
, xfer
);
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
,
728 chp
->ch_flags
|= ATACH_DMA_WAIT
| ATACH_PIOBM_WAIT
| ATACH_IRQ_WAIT
;
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
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
);
749 /* Let _wdc_ata_bio_start do the loop */
752 } else { /* Done with this transfer */
753 ata_bio
->error
= NOERROR
;
754 wdc_ata_bio_done(chp
, xfer
);
760 wdc_ata_bio_kill_xfer(struct ata_channel
*chp
, struct ata_xfer
*xfer
,
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
;
771 ata_bio
->error
= ERR_NODEV
;
774 ata_bio
->error
= ERR_RESET
;
777 printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
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
);
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
),
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
);
820 wdc_ata_err(struct ata_drive_datas
*drvp
, struct ata_bio
*ata_bio
)
822 struct ata_channel
*chp
= drvp
->chnl_softc
;
824 if (chp
->ch_status
& WDCS_BSY
) {
825 ata_bio
->error
= TIMEOUT
;
829 if (chp
->ch_status
& WDCS_DWF
) {
830 ata_bio
->error
= ERR_DF
;
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
))
840 return WDC_ATA_NOERR
;
843 if (chp
->ch_status
& WDCS_CORR
)
844 ata_bio
->flags
|= ATA_CORR
;
845 return WDC_ATA_NOERR
;
849 wdc_ata_addref(struct ata_drive_datas
*drvp
)
851 struct ata_channel
*chp
= drvp
->chnl_softc
;
853 return (ata_addref(chp
));
857 wdc_ata_delref(struct ata_drive_datas
*drvp
)
859 struct ata_channel
*chp
= drvp
->chnl_softc
;