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