ata_wdc.c revision 1.72 1 /* $NetBSD: ata_wdc.c,v 1.72 2004/08/14 15:08:04 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.72 2004/08/14 15:08:04 thorpej Exp $");
70
71 #ifndef ATADEBUG
72 #define ATADEBUG
73 #endif /* ATADEBUG */
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 ATADEBUG
107 extern int wdcdebug_wd_mask; /* inited in wd.c */
108 #define ATADEBUG_PRINT(args, level) \
109 if (wdcdebug_wd_mask & (level)) \
110 printf args
111 #else
112 #define ATADEBUG_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 ata_channel *,struct ata_xfer *);
119 static void _wdc_ata_bio_start(struct ata_channel *,struct ata_xfer *);
120 static int wdc_ata_bio_intr(struct ata_channel *, struct ata_xfer *,
121 int);
122 static void wdc_ata_bio_kill_xfer(struct ata_channel *,
123 struct ata_xfer *, int);
124 static void wdc_ata_bio_done(struct ata_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 ata_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 ata_channel *chp = drvp->chnl_softc;
177 struct wdc_softc *wdc = chp->ch_wdc;
178
179 xfer = ata_get_xfer(ATAXF_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 ata_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 ata_channel *chp, struct ata_xfer *xfer)
202 {
203 struct wdc_softc *wdc = chp->ch_wdc;
204 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
205 struct ata_bio *ata_bio = xfer->c_cmd;
206 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
207 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
208 char *errstring;
209
210 ATADEBUG_PRINT(("wdc_ata_bio_start %s:%d:%d\n",
211 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
212 DEBUG_XFERS);
213
214 /* Do control operations specially. */
215 if (__predict_false(drvp->state < READY)) {
216 /*
217 * Actually, we want to be careful not to mess with the control
218 * state if the device is currently busy, but we can assume
219 * that we never get to this point if that's the case.
220 */
221 /* If it's not a polled command, we need the kenrel thread */
222 if ((xfer->c_flags & C_POLL) == 0 &&
223 (chp->ch_flags & ATACH_TH_RUN) == 0) {
224 chp->ch_queue->queue_freeze++;
225 wakeup(&chp->ch_thread);
226 return;
227 }
228 /*
229 * disable interrupts, all commands here should be quick
230 * enouth to be able to poll, and we don't go here that often
231 */
232 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
233 WDCTL_4BIT | WDCTL_IDS);
234 if (wdc->select)
235 wdc->select(chp, xfer->c_drive);
236 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
237 WDSD_IBM | (xfer->c_drive << 4));
238 DELAY(10);
239 errstring = "wait";
240 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
241 goto ctrltimeout;
242 wdccommandshort(chp, xfer->c_drive, WDCC_RECAL);
243 /* Wait for at last 400ns for status bit to be valid */
244 DELAY(1);
245 errstring = "recal";
246 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
247 goto ctrltimeout;
248 if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
249 goto ctrlerror;
250 /* Don't try to set modes if controller can't be adjusted */
251 if (wdc->set_modes == NULL)
252 goto geometry;
253 /* Also don't try if the drive didn't report its mode */
254 if ((drvp->drive_flags & DRIVE_MODE) == 0)
255 goto geometry;
256 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
257 0x08 | drvp->PIO_mode, WDSF_SET_MODE);
258 errstring = "piomode";
259 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
260 goto ctrltimeout;
261 if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
262 goto ctrlerror;
263 if (drvp->drive_flags & DRIVE_UDMA) {
264 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
265 0x40 | drvp->UDMA_mode, WDSF_SET_MODE);
266 } else if (drvp->drive_flags & DRIVE_DMA) {
267 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0,
268 0x20 | drvp->DMA_mode, WDSF_SET_MODE);
269 } else {
270 goto geometry;
271 }
272 errstring = "dmamode";
273 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
274 goto ctrltimeout;
275 if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
276 goto ctrlerror;
277 geometry:
278 if (ata_bio->flags & ATA_LBA)
279 goto multimode;
280 wdccommand(chp, xfer->c_drive, WDCC_IDP,
281 ata_bio->lp->d_ncylinders,
282 ata_bio->lp->d_ntracks - 1, 0, ata_bio->lp->d_nsectors,
283 (ata_bio->lp->d_type == DTYPE_ST506) ?
284 ata_bio->lp->d_precompcyl / 4 : 0);
285 errstring = "geometry";
286 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
287 goto ctrltimeout;
288 if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
289 goto ctrlerror;
290 multimode:
291 if (ata_bio->multi == 1)
292 goto ready;
293 wdccommand(chp, xfer->c_drive, WDCC_SETMULTI, 0, 0, 0,
294 ata_bio->multi, 0);
295 errstring = "setmulti";
296 if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY, wait_flags))
297 goto ctrltimeout;
298 if (chp->ch_status & (WDCS_ERR | WDCS_DWF))
299 goto ctrlerror;
300 ready:
301 drvp->state = READY;
302 /*
303 * The drive is usable now
304 */
305 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
306 WDCTL_4BIT);
307 delay(10); /* some drives need a little delay here */
308 }
309
310 _wdc_ata_bio_start(chp, xfer);
311 return;
312 ctrltimeout:
313 printf("%s:%d:%d: %s timed out\n",
314 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
315 errstring);
316 ata_bio->error = TIMEOUT;
317 goto ctrldone;
318 ctrlerror:
319 printf("%s:%d:%d: %s ",
320 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
321 errstring);
322 if (chp->ch_status & WDCS_DWF) {
323 printf("drive fault\n");
324 ata_bio->error = ERR_DF;
325 } else {
326 printf("error (%x)\n", chp->ch_error);
327 ata_bio->r_error = chp->ch_error;
328 ata_bio->error = ERROR;
329 }
330 ctrldone:
331 drvp->state = 0;
332 wdc_ata_bio_done(chp, xfer);
333 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
334 return;
335 }
336
337 static void
338 _wdc_ata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
339 {
340 struct wdc_softc *wdc = chp->ch_wdc;
341 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
342 struct ata_bio *ata_bio = xfer->c_cmd;
343 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
344 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
345 u_int16_t cyl;
346 u_int8_t head, sect, cmd = 0;
347 int nblks;
348 int dma_flags = 0;
349
350 ATADEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",
351 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
352 DEBUG_INTR | DEBUG_XFERS);
353
354 if (xfer->c_flags & C_DMA) {
355 if (drvp->n_xfers <= NXFER)
356 drvp->n_xfers++;
357 dma_flags = (ata_bio->flags & ATA_READ) ? WDC_DMA_READ : 0;
358 if (ata_bio->flags & ATA_LBA48)
359 dma_flags |= WDC_DMA_LBA48;
360 }
361 again:
362 /*
363 *
364 * When starting a multi-sector transfer, or doing single-sector
365 * transfers...
366 */
367 if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {
368 if (ata_bio->flags & ATA_SINGLE)
369 nblks = 1;
370 else
371 nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
372 /* Check for bad sectors and adjust transfer, if necessary. */
373 if ((ata_bio->lp->d_flags & D_BADSECT) != 0) {
374 long blkdiff;
375 int i;
376 for (i = 0; (blkdiff = ata_bio->badsect[i]) != -1;
377 i++) {
378 blkdiff -= ata_bio->blkno;
379 if (blkdiff < 0)
380 continue;
381 if (blkdiff == 0) {
382 /* Replace current block of transfer. */
383 ata_bio->blkno =
384 ata_bio->lp->d_secperunit -
385 ata_bio->lp->d_nsectors - i - 1;
386 }
387 if (blkdiff < nblks) {
388 /* Bad block inside transfer. */
389 ata_bio->flags |= ATA_SINGLE;
390 nblks = 1;
391 }
392 break;
393 }
394 /* Transfer is okay now. */
395 }
396 if (ata_bio->flags & ATA_LBA48) {
397 sect = 0;
398 cyl = 0;
399 head = 0;
400 } else if (ata_bio->flags & ATA_LBA) {
401 sect = (ata_bio->blkno >> 0) & 0xff;
402 cyl = (ata_bio->blkno >> 8) & 0xffff;
403 head = (ata_bio->blkno >> 24) & 0x0f;
404 head |= WDSD_LBA;
405 } else {
406 int blkno = ata_bio->blkno;
407 sect = blkno % ata_bio->lp->d_nsectors;
408 sect++; /* Sectors begin with 1, not 0. */
409 blkno /= ata_bio->lp->d_nsectors;
410 head = blkno % ata_bio->lp->d_ntracks;
411 blkno /= ata_bio->lp->d_ntracks;
412 cyl = blkno;
413 head |= WDSD_CHS;
414 }
415 if (xfer->c_flags & C_DMA) {
416 ata_bio->nblks = nblks;
417 ata_bio->nbytes = xfer->c_bcount;
418 cmd = (ata_bio->flags & ATA_READ) ?
419 WDCC_READDMA : WDCC_WRITEDMA;
420 /* Init the DMA channel. */
421 if ((*wdc->dma_init)(wdc->dma_arg,
422 chp->ch_channel, xfer->c_drive,
423 (char *)xfer->c_databuf + xfer->c_skip,
424 ata_bio->nbytes, dma_flags) != 0) {
425 ata_bio->error = ERR_DMA;
426 ata_bio->r_error = 0;
427 wdc_ata_bio_done(chp, xfer);
428 return;
429 }
430 /* Initiate command */
431 if (wdc->select)
432 wdc->select(chp, xfer->c_drive);
433 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
434 0, WDSD_IBM | (xfer->c_drive << 4));
435 switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
436 case WDCWAIT_OK:
437 break;
438 case WDCWAIT_TOUT:
439 goto timeout;
440 case WDCWAIT_THR:
441 return;
442 }
443 if (ata_bio->flags & ATA_LBA48) {
444 wdccommandext(chp, xfer->c_drive, to48(cmd),
445 (u_int64_t)ata_bio->blkno, nblks);
446 } else {
447 wdccommand(chp, xfer->c_drive, cmd, cyl,
448 head, sect, nblks, 0);
449 }
450 /* start the DMA channel */
451 (*wdc->dma_start)(wdc->dma_arg,
452 chp->ch_channel, xfer->c_drive);
453 chp->ch_flags |= ATACH_DMA_WAIT;
454 /* start timeout machinery */
455 if ((xfer->c_flags & C_POLL) == 0)
456 callout_reset(&chp->ch_callout,
457 ATA_DELAY / 1000 * hz, wdctimeout, chp);
458 /* wait for irq */
459 goto intr;
460 } /* else not DMA */
461 ata_bio->nblks = min(nblks, ata_bio->multi);
462 ata_bio->nbytes = ata_bio->nblks * ata_bio->lp->d_secsize;
463 KASSERT(nblks == 1 || (ata_bio->flags & ATA_SINGLE) == 0);
464 if (ata_bio->nblks > 1) {
465 cmd = (ata_bio->flags & ATA_READ) ?
466 WDCC_READMULTI : WDCC_WRITEMULTI;
467 } else {
468 cmd = (ata_bio->flags & ATA_READ) ?
469 WDCC_READ : WDCC_WRITE;
470 }
471 /* Initiate command! */
472 if (wdc->select)
473 wdc->select(chp, xfer->c_drive);
474 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
475 WDSD_IBM | (xfer->c_drive << 4));
476 switch(wdc_wait_for_ready(chp, ATA_DELAY, wait_flags)) {
477 case WDCWAIT_OK:
478 break;
479 case WDCWAIT_TOUT:
480 goto timeout;
481 case WDCWAIT_THR:
482 return;
483 }
484 if (ata_bio->flags & ATA_LBA48) {
485 wdccommandext(chp, xfer->c_drive, to48(cmd),
486 (u_int64_t) ata_bio->blkno, nblks);
487 } else {
488 wdccommand(chp, xfer->c_drive, cmd, cyl,
489 head, sect, nblks,
490 (ata_bio->lp->d_type == DTYPE_ST506) ?
491 ata_bio->lp->d_precompcyl / 4 : 0);
492 }
493 /* start timeout machinery */
494 if ((xfer->c_flags & C_POLL) == 0)
495 callout_reset(&chp->ch_callout,
496 ATA_DELAY / 1000 * hz, wdctimeout, chp);
497 } else if (ata_bio->nblks > 1) {
498 /* The number of blocks in the last stretch may be smaller. */
499 nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
500 if (ata_bio->nblks > nblks) {
501 ata_bio->nblks = nblks;
502 ata_bio->nbytes = xfer->c_bcount;
503 }
504 }
505 /* If this was a write and not using DMA, push the data. */
506 if ((ata_bio->flags & ATA_READ) == 0) {
507 /*
508 * we have to busy-wait here, we can't rely on running in
509 * thread context.
510 */
511 if (wdc_wait_for_drq(chp, ATA_DELAY, AT_POLL) != 0) {
512 printf("%s:%d:%d: timeout waiting for DRQ, "
513 "st=0x%02x, err=0x%02x\n",
514 wdc->sc_dev.dv_xname, chp->ch_channel,
515 xfer->c_drive, chp->ch_status, chp->ch_error);
516 if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
517 ata_bio->error = TIMEOUT;
518 wdc_ata_bio_done(chp, xfer);
519 return;
520 }
521 if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {
522 wdc_ata_bio_done(chp, xfer);
523 return;
524 }
525 wdc->dataout_pio(chp, drvp->drive_flags,
526 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
527 }
528
529 intr: /* Wait for IRQ (either real or polled) */
530 if ((ata_bio->flags & ATA_POLL) == 0) {
531 chp->ch_flags |= ATACH_IRQ_WAIT;
532 } else {
533 /* Wait for at last 400ns for status bit to be valid */
534 delay(1);
535 if (chp->ch_flags & ATACH_DMA_WAIT) {
536 wdc_dmawait(chp, xfer, ATA_DELAY);
537 chp->ch_flags &= ~ATACH_DMA_WAIT;
538 }
539 wdc_ata_bio_intr(chp, xfer, 0);
540 if ((ata_bio->flags & ATA_ITSDONE) == 0)
541 goto again;
542 }
543 return;
544 timeout:
545 printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",
546 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
547 chp->ch_status, chp->ch_error);
548 if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)
549 ata_bio->error = TIMEOUT;
550 wdc_ata_bio_done(chp, xfer);
551 return;
552 }
553
554 static int
555 wdc_ata_bio_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
556 {
557 struct wdc_softc *wdc = chp->ch_wdc;
558 struct ata_bio *ata_bio = xfer->c_cmd;
559 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
560 int drv_err;
561
562 ATADEBUG_PRINT(("wdc_ata_bio_intr %s:%d:%d\n",
563 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
564 DEBUG_INTR | DEBUG_XFERS);
565
566
567 /* Is it not a transfer, but a control operation? */
568 if (drvp->state < READY) {
569 printf("%s:%d:%d: bad state %d in wdc_ata_bio_intr\n",
570 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
571 drvp->state);
572 panic("wdc_ata_bio_intr: bad state");
573 }
574
575 /*
576 * if we missed an interrupt in a PIO transfer, reset and restart.
577 * Don't try to continue transfer, we may have missed cycles.
578 */
579 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) {
580 ata_bio->error = TIMEOUT;
581 wdc_ata_bio_done(chp, xfer);
582 return 1;
583 }
584
585 /* Ack interrupt done by wdc_wait_for_unbusy */
586 if (wdc_wait_for_unbusy(chp, (irq == 0) ? ATA_DELAY : 0, AT_POLL) < 0) {
587 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
588 return 0; /* IRQ was not for us */
589 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip%d\n",
590 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
591 xfer->c_bcount, xfer->c_skip);
592 ata_bio->error = TIMEOUT;
593 wdc_ata_bio_done(chp, xfer);
594 return 1;
595 }
596 if (wdc->irqack)
597 wdc->irqack(chp);
598
599 drv_err = wdc_ata_err(drvp, ata_bio);
600
601 /* If we were using DMA, Turn off the DMA channel and check for error */
602 if (xfer->c_flags & C_DMA) {
603 if (ata_bio->flags & ATA_POLL) {
604 /*
605 * IDE drives deassert WDCS_BSY before transfer is
606 * complete when using DMA. Polling for DRQ to deassert
607 * is not enough DRQ is not required to be
608 * asserted for DMA transfers, so poll for DRDY.
609 */
610 if (wdcwait(chp, WDCS_DRDY | WDCS_DRQ, WDCS_DRDY,
611 ATA_DELAY, ATA_POLL) == WDCWAIT_TOUT) {
612 printf("%s:%d:%d: polled transfer timed out "
613 "(st=0x%x)\n", wdc->sc_dev.dv_xname,
614 chp->ch_channel, xfer->c_drive,
615 chp->ch_status);
616 ata_bio->error = TIMEOUT;
617 drv_err = WDC_ATA_ERR;
618 }
619 }
620 if (wdc->dma_status != 0) {
621 if (drv_err != WDC_ATA_ERR) {
622 ata_bio->error = ERR_DMA;
623 drv_err = WDC_ATA_ERR;
624 }
625 }
626 if (chp->ch_status & WDCS_DRQ) {
627 if (drv_err != WDC_ATA_ERR) {
628 printf("%s:%d:%d: intr with DRQ (st=0x%x)\n",
629 wdc->sc_dev.dv_xname, chp->ch_channel,
630 xfer->c_drive, chp->ch_status);
631 ata_bio->error = TIMEOUT;
632 drv_err = WDC_ATA_ERR;
633 }
634 }
635 if (ata_bio->r_error & WDCE_CRC)
636 ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0);
637 if (drv_err != WDC_ATA_ERR)
638 goto end;
639 }
640
641 /* if we had an error, end */
642 if (drv_err == WDC_ATA_ERR) {
643 wdc_ata_bio_done(chp, xfer);
644 return 1;
645 }
646
647 /* If this was a read and not using DMA, fetch the data. */
648 if ((ata_bio->flags & ATA_READ) != 0) {
649 if ((chp->ch_status & WDCS_DRQ) != WDCS_DRQ) {
650 printf("%s:%d:%d: read intr before drq\n",
651 wdc->sc_dev.dv_xname, chp->ch_channel,
652 xfer->c_drive);
653 ata_bio->error = TIMEOUT;
654 wdc_ata_bio_done(chp, xfer);
655 return 1;
656 }
657 wdc->datain_pio(chp, drvp->drive_flags,
658 (char *)xfer->c_databuf + xfer->c_skip, ata_bio->nbytes);
659 }
660
661 end:
662 ata_bio->blkno += ata_bio->nblks;
663 ata_bio->blkdone += ata_bio->nblks;
664 xfer->c_skip += ata_bio->nbytes;
665 xfer->c_bcount -= ata_bio->nbytes;
666 /* See if this transfer is complete. */
667 if (xfer->c_bcount > 0) {
668 if ((ata_bio->flags & ATA_POLL) == 0) {
669 /* Start the next operation */
670 _wdc_ata_bio_start(chp, xfer);
671 } else {
672 /* Let _wdc_ata_bio_start do the loop */
673 return 1;
674 }
675 } else { /* Done with this transfer */
676 ata_bio->error = NOERROR;
677 wdc_ata_bio_done(chp, xfer);
678 }
679 return 1;
680 }
681
682 static void
683 wdc_ata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
684 int reason)
685 {
686 struct ata_bio *ata_bio = xfer->c_cmd;
687 int drive = xfer->c_drive;
688
689 ata_free_xfer(chp, xfer);
690
691 ata_bio->flags |= ATA_ITSDONE;
692 switch (reason) {
693 case KILL_GONE:
694 ata_bio->error = ERR_NODEV;
695 break;
696 case KILL_RESET:
697 ata_bio->error = ERR_RESET;
698 break;
699 default:
700 printf("wdc_ata_bio_kill_xfer: unknown reason %d\n",
701 reason);
702 panic("wdc_ata_bio_kill_xfer");
703 }
704 ata_bio->r_error = WDCE_ABRT;
705 ATADEBUG_PRINT(("wdc_ata_done: drv_done\n"), DEBUG_XFERS);
706 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
707 }
708
709 static void
710 wdc_ata_bio_done(struct ata_channel *chp, struct ata_xfer *xfer)
711 {
712 struct wdc_softc *wdc = chp->ch_wdc;
713 struct ata_bio *ata_bio = xfer->c_cmd;
714 int drive = xfer->c_drive;
715
716 ATADEBUG_PRINT(("wdc_ata_bio_done %s:%d:%d: flags 0x%x\n",
717 wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive,
718 (u_int)xfer->c_flags),
719 DEBUG_XFERS);
720
721 callout_stop(&chp->ch_callout);
722
723 /* feed back residual bcount to our caller */
724 ata_bio->bcount = xfer->c_bcount;
725
726 /* mark controller inactive and free xfer */
727 chp->ch_queue->active_xfer = NULL;
728 ata_free_xfer(chp, xfer);
729
730 if (chp->ch_drive[drive].drive_flags & DRIVE_WAITDRAIN) {
731 ata_bio->error = ERR_NODEV;
732 chp->ch_drive[drive].drive_flags &= ~DRIVE_WAITDRAIN;
733 wakeup(&chp->ch_queue->active_xfer);
734 }
735 ata_bio->flags |= ATA_ITSDONE;
736 ATADEBUG_PRINT(("wdc_ata_done: drv_done\n"), DEBUG_XFERS);
737 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
738 ATADEBUG_PRINT(("atastart from wdc_ata_done, flags 0x%x\n",
739 chp->ch_flags), DEBUG_XFERS);
740 atastart(chp);
741 }
742
743 static int
744 wdc_ata_err(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
745 {
746 struct ata_channel *chp = drvp->chnl_softc;
747 ata_bio->error = 0;
748 if (chp->ch_status & WDCS_BSY) {
749 ata_bio->error = TIMEOUT;
750 return WDC_ATA_ERR;
751 }
752
753 if (chp->ch_status & WDCS_DWF) {
754 ata_bio->error = ERR_DF;
755 return WDC_ATA_ERR;
756 }
757
758 if (chp->ch_status & WDCS_ERR) {
759 ata_bio->error = ERROR;
760 ata_bio->r_error = chp->ch_error;
761 if (ata_bio->r_error & (WDCE_BBK | WDCE_UNC | WDCE_IDNF |
762 WDCE_ABRT | WDCE_TK0NF | WDCE_AMNF))
763 return WDC_ATA_ERR;
764 return WDC_ATA_NOERR;
765 }
766
767 if (chp->ch_status & WDCS_CORR)
768 ata_bio->flags |= ATA_CORR;
769 return WDC_ATA_NOERR;
770 }
771
772 static int
773 wdc_ata_addref(struct ata_drive_datas *drvp)
774 {
775 struct ata_channel *chp = drvp->chnl_softc;
776
777 return (ata_addref(chp));
778 }
779
780 static void
781 wdc_ata_delref(struct ata_drive_datas *drvp)
782 {
783 struct ata_channel *chp = drvp->chnl_softc;
784
785 ata_delref(chp);
786 }
787