ata_wdc.c revision 1.105.6.2 1 /* $NetBSD: ata_wdc.c,v 1.105.6.2 2017/04/15 12:01:23 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.105.6.2 2017/04/15 12:01:23 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/malloc.h>
70 #include <sys/device.h>
71 #include <sys/disklabel.h>
72 #include <sys/syslog.h>
73 #include <sys/proc.h>
74
75 #include <sys/intr.h>
76 #include <sys/bus.h>
77 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
78 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
79 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
80 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
81 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
82 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
83
84 #include <dev/ata/ataconf.h>
85 #include <dev/ata/atareg.h>
86 #include <dev/ata/atavar.h>
87 #include <dev/ic/wdcreg.h>
88 #include <dev/ic/wdcvar.h>
89
90 #define DEBUG_INTR 0x01
91 #define DEBUG_XFERS 0x02
92 #define DEBUG_STATUS 0x04
93 #define DEBUG_FUNCS 0x08
94 #define DEBUG_PROBE 0x10
95 #ifdef ATADEBUG
96 extern int wdcdebug_wd_mask; /* inited in wd.c */
97 #define ATADEBUG_PRINT(args, level) \
98 if (wdcdebug_wd_mask & (level)) \
99 printf args
100 #else
101 #define ATADEBUG_PRINT(args, level)
102 #endif
103
104 #define ATA_DELAY 10000 /* 10s for a drive I/O */
105
106 static int wdc_ata_bio(struct ata_drive_datas*, struct ata_xfer *);
107 static void wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
108 static void _wdc_ata_bio_start(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 *);
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 };
132
133 /*
134 * Handle block I/O operation. Return ATACMD_COMPLETE, ATACMD_QUEUED, or
135 * ATACMD_TRY_AGAIN. Must be called at splbio().
136 */
137 static int
138 wdc_ata_bio(struct ata_drive_datas *drvp, struct ata_xfer *xfer)
139 {
140 struct ata_channel *chp = drvp->chnl_softc;
141 struct atac_softc *atac = chp->ch_atac;
142 struct ata_bio *ata_bio = &xfer->c_bio;
143
144 if (atac->atac_cap & ATAC_CAP_NOIRQ)
145 ata_bio->flags |= ATA_POLL;
146 if (ata_bio->flags & ATA_POLL)
147 xfer->c_flags |= C_POLL;
148 #if NATA_DMA
149 if ((drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) &&
150 (ata_bio->flags & ATA_SINGLE) == 0)
151 xfer->c_flags |= C_DMA;
152 #endif
153 #if NATA_DMA && NATA_PIOBM
154 else
155 #endif
156 #if NATA_PIOBM
157 if (atac->atac_cap & ATAC_CAP_PIOBM)
158 xfer->c_flags |= C_PIOBM;
159 #endif
160 xfer->c_drive = drvp->drive;
161 xfer->c_databuf = ata_bio->databuf;
162 xfer->c_bcount = ata_bio->bcount;
163 xfer->c_start = wdc_ata_bio_start;
164 xfer->c_intr = wdc_ata_bio_intr;
165 xfer->c_kill_xfer = wdc_ata_bio_kill_xfer;
166 ata_exec_xfer(chp, xfer);
167 return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED;
168 }
169
170 static void
171 wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
172 {
173 struct atac_softc *atac = chp->ch_atac;
174 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
175 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
176 struct ata_bio *ata_bio = &xfer->c_bio;
177 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
178 int wait_flags;
179 const char *errstring;
180 #ifdef WDC_NO_IDS
181 wait_flags = AT_POLL;
182 #else
183 wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
184 #endif
185
186 ATADEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d state %d drive_flags 0x%x "
187 "c_flags 0x%x ch_flags 0x%x\n",
188 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
189 drvp->state, drvp->drive_flags, xfer->c_flags, chp->ch_flags),
190 DEBUG_XFERS);
191
192 /* Do control operations specially. */
193 if (__predict_false(drvp->state < READY)) {
194 /*
195 * Actually, we want to be careful not to mess with the control
196 * state if the device is currently busy, but we can assume
197 * that we never get to this point if that's the case.
198 */
199 /* If it's not a polled command, we need the kernel thread */
200 if ((xfer->c_flags & C_POLL) == 0 &&
201 (chp->ch_flags & ATACH_TH_RUN) == 0) {
202 chp->ch_queue->queue_freeze++;
203 wakeup(&chp->ch_thread);
204 return;
205 }
206 /*
207 * disable interrupts, all commands here should be quick
208 * enough to be able to poll, and we don't go here that often
209 */
210 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
211 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh,
212 wd_aux_ctlr, 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 & ATA_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 & ATA_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 & ATA_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;
254 }
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 drvp->lp->d_ncylinders,
266 drvp->lp->d_ntracks - 1, 0, drvp->lp->d_nsectors,
267 (drvp->lp->d_type == DKTYPE_ST506) ?
268 drvp->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 (drvp->multi == 1)
276 goto ready;
277 wdccommand(chp, xfer->c_drive, WDCC_SETMULTI, 0, 0, 0,
278 drvp->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;
286 /*
287 * The drive is usable now
288 */
289 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
290 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh,
291 wd_aux_ctlr, WDCTL_4BIT);
292 delay(10); /* some drives need a little delay here */
293 }
294
295 _wdc_ata_bio_start(chp, xfer);
296 return;
297 ctrltimeout:
298 printf("%s:%d:%d: %s timed out\n",
299 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
300 errstring);
301 ata_bio->error = TIMEOUT;
302 goto ctrldone;
303 ctrlerror:
304 printf("%s:%d:%d: %s ",
305 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
306 errstring);
307 if (chp->ch_status & WDCS_DWF) {
308 printf("drive fault\n");
309 ata_bio->error = ERR_DF;
310 } else {
311 printf("error (%x)\n", chp->ch_error);
312 ata_bio->r_error = chp->ch_error;
313 ata_bio->error = ERROR;
314 }
315 ctrldone:
316 drvp->state = 0;
317 wdc_ata_bio_done(chp, xfer);
318
319 if (! (wdc->cap & WDC_CAPABILITY_NO_AUXCTL))
320 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
321 WDCTL_4BIT);
322 return;
323 }
324
325 static void
326 _wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
327 {
328 struct atac_softc *atac = chp->ch_atac;
329 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
330 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
331 struct ata_bio *ata_bio = &xfer->c_bio;
332 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
333 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
334 uint16_t cyl;
335 uint8_t head, sect, cmd = 0;
336 int nblks;
337 #if NATA_DMA || NATA_PIOBM
338 int error, dma_flags = 0;
339 #endif
340
341 ATADEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",
342 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
343 DEBUG_INTR | DEBUG_XFERS);
344
345 #if NATA_DMA || NATA_PIOBM
346 if (xfer->c_flags & (C_DMA | C_PIOBM)) {
347 #if NATA_DMA
348 if (drvp->n_xfers <= NXFER)
349 drvp->n_xfers++;
350 #endif
351 dma_flags = (ata_bio->flags & ATA_READ) ? WDC_DMA_READ : 0;
352 if (ata_bio->flags & ATA_LBA48)
353 dma_flags |= WDC_DMA_LBA48;
354 }
355 #endif
356 again:
357 /*
358 *
359 * When starting a multi-sector transfer, or doing single-sector
360 * transfers...
361 */
362 if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
363 if (ata_bio->flags & ATA_SINGLE)
364 nblks = 1;
365 else
366 nblks = xfer->c_bcount / drvp->lp->d_secsize;
367 /* Check for bad sectors and adjust transfer, if necessary. */
368 if ((drvp->lp->d_flags & D_BADSECT) != 0) {
369 long blkdiff;
370 int i;
371 for (i = 0; (blkdiff = drvp->badsect[i]) != -1;
372 i++) {
373 blkdiff -= ata_bio->blkno;
374 if (blkdiff < 0)
375 continue;
376 if (blkdiff == 0) {
377 /* Replace current block of transfer. */
378 ata_bio->blkno =
379 drvp->lp->d_secperunit -
380 drvp->lp->d_nsectors - i - 1;
381 }
382 if (blkdiff < nblks) {
383 /* Bad block inside transfer. */
384 ata_bio->flags |= ATA_SINGLE;
385 nblks = 1;
386 }
387 break;
388 }
389 /* Transfer is okay now. */
390 }
391 if (ata_bio->flags & ATA_LBA48) {
392 sect = 0;
393 cyl = 0;
394 head = 0;
395 } else if (ata_bio->flags & ATA_LBA) {
396 sect = (ata_bio->blkno >> 0) & 0xff;
397 cyl = (ata_bio->blkno >> 8) & 0xffff;
398 head = (ata_bio->blkno >> 24) & 0x0f;
399 head |= WDSD_LBA;
400 } else {
401 int blkno = ata_bio->blkno;
402 sect = blkno % drvp->lp->d_nsectors;
403 sect++; /* Sectors begin with 1, not 0. */
404 blkno /= drvp->lp->d_nsectors;
405 head = blkno % drvp->lp->d_ntracks;
406 blkno /= drvp->lp->d_ntracks;
407 cyl = blkno;
408 head |= WDSD_CHS;
409 }
410 #if NATA_DMA
411 if (xfer->c_flags & C_DMA) {
412 ata_bio->nblks = nblks;
413 ata_bio->nbytes = xfer->c_bcount;
414 cmd = (ata_bio->flags & ATA_READ) ?
415 WDCC_READDMA : WDCC_WRITEDMA;
416 /* Init the DMA channel. */
417 error = (*wdc->dma_init)(wdc->dma_arg,
418 chp->ch_channel, xfer->c_drive,
419 (char *)xfer->c_databuf + xfer->c_skip,
420 ata_bio->nbytes, dma_flags);
421 if (error) {
422 if (error == EINVAL) {
423 /*
424 * We can't do DMA on this transfer
425 * for some reason. Fall back to
426 * PIO.
427 */
428 xfer->c_flags &= ~C_DMA;
429 error = 0;
430 goto do_pio;
431 }
432 ata_bio->error = ERR_DMA;
433 ata_bio->r_error = 0;
434 wdc_ata_bio_done(chp, xfer);
435 return;
436 }
437 /* Initiate command */
438 if (wdc->select)
439 wdc->select(chp, xfer->c_drive);
440 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
441 0, WDSD_IBM | (xfer->c_drive << 4));
442 switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
443 case WDCWAIT_OK:
444 break;
445 case WDCWAIT_TOUT:
446 goto timeout;
447 case WDCWAIT_THR:
448 return;
449 }
450 if (ata_bio->flags & ATA_LBA48) {
451 wdccommandext(chp, xfer->c_drive, atacmd_to48(cmd),
452 ata_bio->blkno, nblks, 0, WDSD_LBA);
453 } else {
454 wdccommand(chp, xfer->c_drive, cmd, cyl,
455 head, sect, nblks, 0);
456 }
457 /* start the DMA channel */
458 (*wdc->dma_start)(wdc->dma_arg,
459 chp->ch_channel, xfer->c_drive);
460 chp->ch_flags |= ATACH_DMA_WAIT;
461 /* start timeout machinery */
462 if ((xfer->c_flags & C_POLL) == 0)
463 callout_reset(&chp->ch_callout,
464 ATA_DELAY / 1000 * hz, wdctimeout, chp);
465 /* wait for irq */
466 goto intr;
467 } /* else not DMA */
468 do_pio:
469 #endif /* NATA_DMA */
470 #if NATA_PIOBM
471 if ((xfer->c_flags & C_PIOBM) && xfer->c_skip == 0) {
472 if (ata_bio->flags & ATA_POLL) {
473 /* XXX not supported yet --- fall back to PIO */
474 xfer->c_flags &= ~C_PIOBM;
475 } else {
476 /* Init the DMA channel. */
477 error = (*wdc->dma_init)(wdc->dma_arg,
478 chp->ch_channel, xfer->c_drive,
479 (char *)xfer->c_databuf + xfer->c_skip,
480 xfer->c_bcount,
481 dma_flags | WDC_DMA_PIOBM_ATA);
482 if (error) {
483 if (error == EINVAL) {
484 /*
485 * We can't do DMA on this
486 * transfer for some reason.
487 * Fall back to PIO.
488 */
489 xfer->c_flags &= ~C_PIOBM;
490 error = 0;
491 } else {
492 ata_bio->error = ERR_DMA;
493 ata_bio->r_error = 0;
494 wdc_ata_bio_done(chp, xfer);
495 return;
496 }
497 }
498 }
499 }
500 #endif
501 ata_bio->nblks = min(nblks, drvp->multi);
502 ata_bio->nbytes = ata_bio->nblks * drvp->lp->d_secsize;
503 KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0);
504 if (ata_bio->nblks > 1) {
505 cmd = (ata_bio->flags & ATA_READ) ?
506 WDCC_READMULTI : WDCC_WRITEMULTI;
507 } else {
508 cmd = (ata_bio->flags & ATA_READ) ?
509 WDCC_READ : WDCC_WRITE;
510 }
511 /* Initiate command! */
512 if (wdc->select)
513 wdc->select(chp, xfer->c_drive);
514 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
515 WDSD_IBM | (xfer->c_drive << 4));
516 switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
517 case WDCWAIT_OK:
518 break;
519 case WDCWAIT_TOUT:
520 goto timeout;
521 case WDCWAIT_THR:
522 return;
523 }
524 if (ata_bio->flags & ATA_LBA48) {
525 wdccommandext(chp, xfer->c_drive, atacmd_to48(cmd),
526 ata_bio->blkno, nblks, 0, WDSD_LBA);
527 } else {
528 wdccommand(chp, xfer->c_drive, cmd, cyl,
529 head, sect, nblks,
530 (drvp->lp->d_type == DKTYPE_ST506) ?
531 drvp->lp->d_precompcyl / 4 : 0);
532 }
533 /* start timeout machinery */
534 if ((xfer->c_flags & C_POLL) == 0)
535 callout_reset(&chp->ch_callout,
536 ATA_DELAY / 1000 * hz, wdctimeout, chp);
537 } else if (ata_bio->nblks > 1) {
538 /* The number of blocks in the last stretch may be smaller. */
539 nblks = xfer->c_bcount / drvp->lp->d_secsize;
540 if (ata_bio->nblks > nblks) {
541 ata_bio->nblks = nblks;
542 ata_bio->nbytes = xfer->c_bcount;
543 }
544 }
545 /* If this was a write and not using DMA, push the data. */
546 if ((ata_bio->flags & ATA_READ) == 0) {
547 /*
548 * we have to busy-wait here, we can't rely on running in
549 * thread context.
550 */
551 if (wdc_wait_for_drq(chp, ATA_DELAY, AT_POLL) != 0) {
552 printf("%s:%d:%d: timeout waiting for DRQ, "
553 "st=0x%02x, err=0x%02x\n",
554 device_xname(atac->atac_dev), chp->ch_channel,
555 xfer->c_drive, chp->ch_status, chp->ch_error);
556 if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
557 ata_bio->error = TIMEOUT;
558 wdc_ata_bio_done(chp, xfer);
559 return;
560 }
561 if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {
562 wdc_ata_bio_done(chp, xfer);
563 return;
564 }
565 #if NATA_PIOBM
566 if (xfer->c_flags & C_PIOBM) {
567 /* start the busmastering PIO */
568 (*wdc->piobm_start)(wdc->dma_arg,
569 chp->ch_channel, xfer->c_drive,
570 xfer->c_skip, ata_bio->nbytes, 0);
571 chp->ch_flags |= ATACH_DMA_WAIT;
572 } else
573 #endif
574
575 wdc->dataout_pio(chp, drvp->drive_flags,
576 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
577 }
578
579 #if NATA_DMA
580 intr:
581 #endif
582 /* Wait for IRQ (either real or polled) */
583 if ((ata_bio->flags & ATA_POLL) == 0) {
584 chp->ch_flags |= ATACH_IRQ_WAIT;
585 } else {
586 /* Wait for at last 400ns for status bit to be valid */
587 delay(1);
588 #if NATA_DMA
589 if (chp->ch_flags & ATACH_DMA_WAIT) {
590 wdc_dmawait(chp, xfer, ATA_DELAY);
591 chp->ch_flags &= ~ATACH_DMA_WAIT;
592 }
593 #endif
594 wdc_ata_bio_intr(chp, xfer, 0);
595 if ((ata_bio->flags & ATA_ITSDONE) == 0)
596 goto again;
597 }
598 return;
599 timeout:
600 printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",
601 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
602 chp->ch_status, chp->ch_error);
603 if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
604 ata_bio->error = TIMEOUT;
605 wdc_ata_bio_done(chp, xfer);
606 return;
607 }
608
609 static int
610 wdc_ata_bio_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
611 {
612 struct atac_softc *atac = chp->ch_atac;
613 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
614 struct ata_bio *ata_bio = &xfer->c_bio;
615 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
616 int drv_err;
617
618 ATADEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
619 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive),
620 DEBUG_INTR | DEBUG_XFERS);
621
622
623 /* Is it not a transfer, but a control operation? */
624 if (drvp->state < READY) {
625 printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
626 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
627 drvp->state);
628 panic("wdc_ata_bio_intr: bad state");
629 }
630
631 /*
632 * if we missed an interrupt in a PIO transfer, reset and restart.
633 * Don't try to continue transfer, we may have missed cycles.
634 */
635 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
636 ata_bio->error = TIMEOUT;
637 wdc_ata_bio_done(chp, xfer);
638 return 1;
639 }
640
641 #if NATA_PIOBM
642 /* Transfer-done interrupt for busmastering PIO read */
643 if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) {
644 chp->ch_flags &= ~ATACH_PIOBM_WAIT;
645 goto end;
646 }
647 #endif
648
649 /* Ack interrupt done by wdc_wait_for_unbusy */
650 if (wdc_wait_for_unbusy(chp, (irq == 0) ? ATA_DELAY : 0, AT_POLL) < 0) {
651 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
652 return 0; /* IRQ was not for us */
653 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
654 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
655 xfer->c_bcount, xfer->c_skip);
656 ata_bio->error = TIMEOUT;
657 wdc_ata_bio_done(chp, xfer);
658 return 1;
659 }
660 if (wdc->irqack)
661 wdc->irqack(chp);
662
663 drv_err = wdc_ata_err(drvp, ata_bio);
664
665 #if NATA_DMA
666 /* If we were using DMA, Turn off the DMA channel and check for error */
667 if (xfer->c_flags & C_DMA) {
668 if (ata_bio->flags & ATA_POLL) {
669 /*
670 * IDE drives deassert WDCS_BSY before transfer is
671 * complete when using DMA. Polling for DRQ to deassert
672 * is not enough DRQ is not required to be
673 * asserted for DMA transfers, so poll for DRDY.
674 */
675 if (wdcwait(chp, WDCS_DRDY | WDCS_DRQ, WDCS_DRDY,
676 ATA_DELAY, ATA_POLL) == WDCWAIT_TOUT) {
677 printf("%s:%d:%d: polled transfer timed out "
678 "(st=0x%x)\n",
679 device_xname(atac->atac_dev),
680 chp->ch_channel, xfer->c_drive,
681 chp->ch_status);
682 ata_bio->error = TIMEOUT;
683 drv_err = WDC_ATA_ERR;
684 }
685 }
686 if (wdc->dma_status != 0) {
687 if (drv_err != WDC_ATA_ERR) {
688 ata_bio->error = ERR_DMA;
689 drv_err = WDC_ATA_ERR;
690 }
691 }
692 if (chp->ch_status & WDCS_DRQ) {
693 if (drv_err != WDC_ATA_ERR) {
694 printf("%s:%d:%d: intr with DRQ (st=0x%x)\n",
695 device_xname(atac->atac_dev),
696 chp->ch_channel,
697 xfer->c_drive, chp->ch_status);
698 ata_bio->error = TIMEOUT;
699 drv_err = WDC_ATA_ERR;
700 }
701 }
702 if (drv_err != WDC_ATA_ERR)
703 goto end;
704 if (ata_bio->r_error & WDCE_CRC || ata_bio->error == ERR_DMA)
705 ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
706 }
707 #endif /* NATA_DMA */
708
709 /* if we had an error, end */
710 if (drv_err == WDC_ATA_ERR) {
711 wdc_ata_bio_done(chp, xfer);
712 return 1;
713 }
714
715 /* If this was a read and not using DMA, fetch the data. */
716 if ((ata_bio->flags & ATA_READ) != 0) {
717 if ((chp->ch_status & WDCS_DRQ) != WDCS_DRQ) {
718 printf("%s:%d:%d: read intr before drq\n",
719 device_xname(atac->atac_dev), chp->ch_channel,
720 xfer->c_drive);
721 ata_bio->error = TIMEOUT;
722 wdc_ata_bio_done(chp, xfer);
723 return 1;
724 }
725 #if NATA_PIOBM
726 if (xfer->c_flags & C_PIOBM) {
727 /* start the busmastering PIO */
728 (*wdc->piobm_start)(wdc->dma_arg,
729 chp->ch_channel, xfer->c_drive,
730 xfer->c_skip, ata_bio->nbytes,
731 WDC_PIOBM_XFER_IRQ);
732 chp->ch_flags |= ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT;
733 return 1;
734 } else
735 #endif
736 wdc->datain_pio(chp, drvp->drive_flags,
737 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
738 }
739
740 #if NATA_DMA || NATA_PIOBM
741 end:
742 #endif
743 ata_bio->blkno += ata_bio->nblks;
744 ata_bio->blkdone += ata_bio->nblks;
745 xfer->c_skip += ata_bio->nbytes;
746 xfer->c_bcount -= ata_bio->nbytes;
747 /* See if this transfer is complete. */
748 if (xfer->c_bcount > 0) {
749 if ((ata_bio->flags & ATA_POLL) == 0) {
750 /* Start the next operation */
751 _wdc_ata_bio_start(chp, xfer);
752 } else {
753 /* Let _wdc_ata_bio_start do the loop */
754 return 1;
755 }
756 } else { /* Done with this transfer */
757 ata_bio->error = NOERROR;
758 wdc_ata_bio_done(chp, xfer);
759 }
760 return 1;
761 }
762
763 static void
764 wdc_ata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
765 int reason)
766 {
767 struct ata_bio *ata_bio = &xfer->c_bio;
768 int drive = xfer->c_drive;
769
770 ata_bio->flags |= ATA_ITSDONE;
771 switch (reason) {
772 case KILL_GONE:
773 ata_bio->error = ERR_NODEV;
774 break;
775 case KILL_RESET:
776 ata_bio->error = ERR_RESET;
777 break;
778 default:
779 printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
780 reason);
781 panic("wdc_ata_bio_kill_xfer");
782 }
783 ata_bio->r_error = WDCE_ABRT;
784 ATADEBUG_PRINT(("wdc_ata_bio_kill_xfer: drv_done\n"), DEBUG_XFERS);
785 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
786 }
787
788 static void
789 wdc_ata_bio_done(struct ata_channel *chp, struct ata_xfer *xfer)
790 {
791 struct ata_bio *ata_bio = &xfer->c_bio;
792 int drive = xfer->c_drive;
793
794 ATADEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
795 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
796 xfer->c_drive, (u_int)xfer->c_flags),
797 DEBUG_XFERS);
798
799 callout_stop(&chp->ch_callout);
800
801 /* feed back residual bcount to our caller */
802 ata_bio->bcount = xfer->c_bcount;
803
804 /* mark controller inactive and free xfer */
805 ata_deactivate_xfer(chp, xfer);
806
807 if (ata_waitdrain_check(chp, drive)) {
808 ata_bio->error = ERR_NODEV;
809 }
810 ata_bio->flags |= ATA_ITSDONE;
811 ATADEBUG_PRINT(("wdc_ata_done: drv_done\n"), DEBUG_XFERS);
812 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
813 ATADEBUG_PRINT(("atastart from wdc_ata_done, flags 0x%x\n",
814 chp->ch_flags), DEBUG_XFERS);
815 atastart(chp);
816 }
817
818 static int
819 wdc_ata_err(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
820 {
821 struct ata_channel *chp = drvp->chnl_softc;
822 ata_bio->error = 0;
823 if (chp->ch_status & WDCS_BSY) {
824 ata_bio->error = TIMEOUT;
825 return WDC_ATA_ERR;
826 }
827
828 if (chp->ch_status & WDCS_DWF) {
829 ata_bio->error = ERR_DF;
830 return WDC_ATA_ERR;
831 }
832
833 if (chp->ch_status & WDCS_ERR) {
834 ata_bio->error = ERROR;
835 ata_bio->r_error = chp->ch_error;
836 if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
837 WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
838 return WDC_ATA_ERR;
839 return WDC_ATA_NOERR;
840 }
841
842 if (chp->ch_status & WDCS_CORR)
843 ata_bio->flags |= ATA_CORR;
844 return WDC_ATA_NOERR;
845 }
846
847 static int
848 wdc_ata_addref(struct ata_drive_datas *drvp)
849 {
850 struct ata_channel *chp = drvp->chnl_softc;
851
852 return (ata_addref(chp));
853 }
854
855 static void
856 wdc_ata_delref(struct ata_drive_datas *drvp)
857 {
858 struct ata_channel *chp = drvp->chnl_softc;
859
860 ata_delref(chp);
861 }
862