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