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