1 /*- 2 * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ata.h" 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/endian.h> 35 #include <sys/ata.h> 36 #include <sys/conf.h> 37 #include <sys/ctype.h> 38 #include <sys/bus.h> 39 #include <sys/sema.h> 40 #include <sys/taskqueue.h> 41 #include <vm/uma.h> 42 #include <machine/bus.h> 43 #include <sys/rman.h> 44 #include <dev/ata/ata-all.h> 45 #include <dev/ata/ata-pci.h> 46 #include <ata_if.h> 47 48 /* prototypes */ 49 static int ata_generic_status(device_t dev); 50 static int ata_wait(struct ata_channel *ch, int unit, u_int8_t); 51 static void ata_pio_read(struct ata_request *, int); 52 static void ata_pio_write(struct ata_request *, int); 53 static void ata_tf_read(struct ata_request *); 54 static void ata_tf_write(struct ata_request *); 55 56 /* 57 * low level ATA functions 58 */ 59 void 60 ata_generic_hw(device_t dev) 61 { 62 struct ata_channel *ch = device_get_softc(dev); 63 64 ch->hw.begin_transaction = ata_begin_transaction; 65 ch->hw.end_transaction = ata_end_transaction; 66 ch->hw.status = ata_generic_status; 67 ch->hw.softreset = NULL; 68 ch->hw.command = ata_generic_command; 69 ch->hw.tf_read = ata_tf_read; 70 ch->hw.tf_write = ata_tf_write; 71 ch->hw.pm_read = NULL; 72 ch->hw.pm_write = NULL; 73 } 74 75 /* must be called with ATA channel locked and state_mtx held */ 76 int 77 ata_begin_transaction(struct ata_request *request) 78 { 79 struct ata_channel *ch = device_get_softc(request->parent); 80 int dummy, error; 81 82 ATA_DEBUG_RQ(request, "begin transaction"); 83 84 /* disable ATAPI DMA writes if HW doesn't support it */ 85 if ((ch->flags & ATA_NO_ATAPI_DMA) && 86 (request->flags & ATA_R_ATAPI) == ATA_R_ATAPI) 87 request->flags &= ~ATA_R_DMA; 88 if ((ch->flags & ATA_ATAPI_DMA_RO) && 89 ((request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)) == 90 (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE))) 91 request->flags &= ~ATA_R_DMA; 92 93 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA)) { 94 95 /* ATA PIO data transfer and control commands */ 96 default: 97 { 98 /* record command direction here as our request might be gone later */ 99 int write = (request->flags & ATA_R_WRITE); 100 101 /* issue command */ 102 if (ch->hw.command(request)) { 103 device_printf(request->parent, "error issuing %s command\n", 104 ata_cmd2str(request)); 105 request->result = EIO; 106 goto begin_finished; 107 } 108 109 /* device reset doesn't interrupt */ 110 if (request->u.ata.command == ATA_DEVICE_RESET) { 111 112 int timeout = 1000000; 113 do { 114 DELAY(10); 115 request->status = ATA_IDX_INB(ch, ATA_STATUS); 116 } while (request->status & ATA_S_BUSY && timeout--); 117 if (request->status & ATA_S_ERROR) 118 request->error = ATA_IDX_INB(ch, ATA_ERROR); 119 goto begin_finished; 120 } 121 122 /* if write command output the data */ 123 if (write) { 124 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) { 125 device_printf(request->parent, 126 "timeout waiting for write DRQ\n"); 127 request->result = EIO; 128 goto begin_finished; 129 } 130 ata_pio_write(request, request->transfersize); 131 } 132 } 133 goto begin_continue; 134 135 /* ATA DMA data transfer commands */ 136 case ATA_R_DMA: 137 /* check sanity, setup SG list and DMA engine */ 138 if ((error = ch->dma.load(request, NULL, &dummy))) { 139 device_printf(request->parent, "setting up DMA failed\n"); 140 request->result = error; 141 goto begin_finished; 142 } 143 144 /* start DMA engine if necessary */ 145 if ((ch->flags & ATA_DMA_BEFORE_CMD) && 146 ch->dma.start && ch->dma.start(request)) { 147 device_printf(request->parent, "error starting DMA\n"); 148 request->result = EIO; 149 goto begin_finished; 150 } 151 152 /* issue command */ 153 if (ch->hw.command(request)) { 154 device_printf(request->parent, "error issuing %s command\n", 155 ata_cmd2str(request)); 156 request->result = EIO; 157 goto begin_finished; 158 } 159 160 /* start DMA engine */ 161 if (!(ch->flags & ATA_DMA_BEFORE_CMD) && 162 ch->dma.start && ch->dma.start(request)) { 163 device_printf(request->parent, "error starting DMA\n"); 164 request->result = EIO; 165 goto begin_finished; 166 } 167 goto begin_continue; 168 169 /* ATAPI PIO commands */ 170 case ATA_R_ATAPI: 171 /* is this just a POLL DSC command ? */ 172 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) { 173 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit)); 174 DELAY(10); 175 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC)) 176 request->result = EBUSY; 177 goto begin_finished; 178 } 179 180 /* start ATAPI operation */ 181 if (ch->hw.command(request)) { 182 device_printf(request->parent, "error issuing ATA PACKET command\n"); 183 request->result = EIO; 184 goto begin_finished; 185 } 186 goto begin_continue; 187 188 /* ATAPI DMA commands */ 189 case ATA_R_ATAPI|ATA_R_DMA: 190 /* is this just a POLL DSC command ? */ 191 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) { 192 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit)); 193 DELAY(10); 194 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC)) 195 request->result = EBUSY; 196 goto begin_finished; 197 } 198 199 /* check sanity, setup SG list and DMA engine */ 200 if ((error = ch->dma.load(request, NULL, &dummy))) { 201 device_printf(request->parent, "setting up DMA failed\n"); 202 request->result = error; 203 goto begin_finished; 204 } 205 206 /* start ATAPI operation */ 207 if (ch->hw.command(request)) { 208 device_printf(request->parent, "error issuing ATA PACKET command\n"); 209 request->result = EIO; 210 goto begin_finished; 211 } 212 213 /* start DMA engine */ 214 if (ch->dma.start && ch->dma.start(request)) { 215 request->result = EIO; 216 goto begin_finished; 217 } 218 goto begin_continue; 219 } 220 /* NOT REACHED */ 221 printf("ata_begin_transaction OOPS!!!\n"); 222 223 begin_finished: 224 if (ch->dma.unload) { 225 ch->dma.unload(request); 226 } 227 return ATA_OP_FINISHED; 228 229 begin_continue: 230 callout_reset(&request->callout, request->timeout * hz, 231 (timeout_t*)ata_timeout, request); 232 return ATA_OP_CONTINUES; 233 } 234 235 /* must be called with ATA channel locked and state_mtx held */ 236 int 237 ata_end_transaction(struct ata_request *request) 238 { 239 struct ata_channel *ch = device_get_softc(request->parent); 240 int length; 241 242 ATA_DEBUG_RQ(request, "end transaction"); 243 244 /* clear interrupt and get status */ 245 request->status = ATA_IDX_INB(ch, ATA_STATUS); 246 247 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_CONTROL)) { 248 249 /* ATA PIO data transfer and control commands */ 250 default: 251 252 /* on timeouts we have no data or anything so just return */ 253 if (request->flags & ATA_R_TIMEOUT) 254 goto end_finished; 255 256 /* on control commands read back registers to the request struct */ 257 if (request->flags & ATA_R_CONTROL) { 258 ch->hw.tf_read(request); 259 } 260 261 /* if we got an error we are done with the HW */ 262 if (request->status & ATA_S_ERROR) { 263 request->error = ATA_IDX_INB(ch, ATA_ERROR); 264 goto end_finished; 265 } 266 267 /* are we moving data ? */ 268 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) { 269 270 /* if read data get it */ 271 if (request->flags & ATA_R_READ) { 272 int flags = ATA_S_DRQ; 273 274 if (request->u.ata.command != ATA_ATAPI_IDENTIFY) 275 flags |= ATA_S_READY; 276 if (ata_wait(ch, request->unit, flags) < 0) { 277 device_printf(request->parent, 278 "timeout waiting for read DRQ\n"); 279 request->result = EIO; 280 goto end_finished; 281 } 282 ata_pio_read(request, request->transfersize); 283 } 284 285 /* update how far we've gotten */ 286 request->donecount += request->transfersize; 287 288 /* do we need a scoop more ? */ 289 if (request->bytecount > request->donecount) { 290 291 /* set this transfer size according to HW capabilities */ 292 request->transfersize = 293 min((request->bytecount - request->donecount), 294 request->transfersize); 295 296 /* if data write command, output the data */ 297 if (request->flags & ATA_R_WRITE) { 298 299 /* if we get an error here we are done with the HW */ 300 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) { 301 device_printf(request->parent, 302 "timeout waiting for write DRQ\n"); 303 request->status = ATA_IDX_INB(ch, ATA_STATUS); 304 goto end_finished; 305 } 306 307 /* output data and return waiting for new interrupt */ 308 ata_pio_write(request, request->transfersize); 309 goto end_continue; 310 } 311 312 /* if data read command, return & wait for interrupt */ 313 if (request->flags & ATA_R_READ) 314 goto end_continue; 315 } 316 } 317 /* done with HW */ 318 goto end_finished; 319 320 /* ATA DMA data transfer commands */ 321 case ATA_R_DMA: 322 323 /* stop DMA engine and get status */ 324 if (ch->dma.stop) 325 request->dma->status = ch->dma.stop(request); 326 327 /* did we get error or data */ 328 if (request->status & ATA_S_ERROR) 329 request->error = ATA_IDX_INB(ch, ATA_ERROR); 330 else if (request->dma->status & ATA_BMSTAT_ERROR) 331 request->status |= ATA_S_ERROR; 332 else if (!(request->flags & ATA_R_TIMEOUT)) 333 request->donecount = request->bytecount; 334 335 /* release SG list etc */ 336 ch->dma.unload(request); 337 338 /* done with HW */ 339 goto end_finished; 340 341 /* ATAPI PIO commands */ 342 case ATA_R_ATAPI: 343 length = ATA_IDX_INB(ch, ATA_CYL_LSB)|(ATA_IDX_INB(ch, ATA_CYL_MSB)<<8); 344 345 /* on timeouts we have no data or anything so just return */ 346 if (request->flags & ATA_R_TIMEOUT) 347 goto end_finished; 348 349 switch ((ATA_IDX_INB(ch, ATA_IREASON) & (ATA_I_CMD | ATA_I_IN)) | 350 (request->status & ATA_S_DRQ)) { 351 352 case ATAPI_P_CMDOUT: 353 /* this seems to be needed for some (slow) devices */ 354 DELAY(10); 355 356 if (!(request->status & ATA_S_DRQ)) { 357 device_printf(request->parent, "command interrupt without DRQ\n"); 358 request->status = ATA_S_ERROR; 359 goto end_finished; 360 } 361 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb, 362 (request->flags & ATA_R_ATAPI16) ? 8 : 6); 363 /* return wait for interrupt */ 364 goto end_continue; 365 366 case ATAPI_P_WRITE: 367 if (request->flags & ATA_R_READ) { 368 request->status = ATA_S_ERROR; 369 device_printf(request->parent, 370 "%s trying to write on read buffer\n", 371 ata_cmd2str(request)); 372 goto end_finished; 373 break; 374 } 375 ata_pio_write(request, length); 376 request->donecount += length; 377 378 /* set next transfer size according to HW capabilities */ 379 request->transfersize = min((request->bytecount-request->donecount), 380 request->transfersize); 381 /* return wait for interrupt */ 382 goto end_continue; 383 384 case ATAPI_P_READ: 385 if (request->flags & ATA_R_WRITE) { 386 request->status = ATA_S_ERROR; 387 device_printf(request->parent, 388 "%s trying to read on write buffer\n", 389 ata_cmd2str(request)); 390 goto end_finished; 391 } 392 ata_pio_read(request, length); 393 request->donecount += length; 394 395 /* set next transfer size according to HW capabilities */ 396 request->transfersize = min((request->bytecount-request->donecount), 397 request->transfersize); 398 /* return wait for interrupt */ 399 goto end_continue; 400 401 case ATAPI_P_DONEDRQ: 402 device_printf(request->parent, 403 "WARNING - %s DONEDRQ non conformant device\n", 404 ata_cmd2str(request)); 405 if (request->flags & ATA_R_READ) { 406 ata_pio_read(request, length); 407 request->donecount += length; 408 } 409 else if (request->flags & ATA_R_WRITE) { 410 ata_pio_write(request, length); 411 request->donecount += length; 412 } 413 else 414 request->status = ATA_S_ERROR; 415 /* FALLTHROUGH */ 416 417 case ATAPI_P_ABORT: 418 case ATAPI_P_DONE: 419 if (request->status & (ATA_S_ERROR | ATA_S_DWF)) 420 request->error = ATA_IDX_INB(ch, ATA_ERROR); 421 goto end_finished; 422 423 default: 424 device_printf(request->parent, "unknown transfer phase\n"); 425 request->status = ATA_S_ERROR; 426 } 427 428 /* done with HW */ 429 goto end_finished; 430 431 /* ATAPI DMA commands */ 432 case ATA_R_ATAPI|ATA_R_DMA: 433 434 /* stop DMA engine and get status */ 435 if (ch->dma.stop) 436 request->dma->status = ch->dma.stop(request); 437 438 /* did we get error or data */ 439 if (request->status & (ATA_S_ERROR | ATA_S_DWF)) 440 request->error = ATA_IDX_INB(ch, ATA_ERROR); 441 else if (request->dma->status & ATA_BMSTAT_ERROR) 442 request->status |= ATA_S_ERROR; 443 else if (!(request->flags & ATA_R_TIMEOUT)) 444 request->donecount = request->bytecount; 445 446 /* release SG list etc */ 447 ch->dma.unload(request); 448 449 /* done with HW */ 450 goto end_finished; 451 } 452 /* NOT REACHED */ 453 printf("ata_end_transaction OOPS!!\n"); 454 455 end_finished: 456 callout_stop(&request->callout); 457 return ATA_OP_FINISHED; 458 459 end_continue: 460 return ATA_OP_CONTINUES; 461 } 462 463 /* must be called with ATA channel locked and state_mtx held */ 464 void 465 ata_generic_reset(device_t dev) 466 { 467 struct ata_channel *ch = device_get_softc(dev); 468 469 u_int8_t ostat0 = 0, stat0 = 0, ostat1 = 0, stat1 = 0; 470 u_int8_t err = 0, lsb = 0, msb = 0; 471 int mask = 0, timeout; 472 473 /* do we have any signs of ATA/ATAPI HW being present ? */ 474 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER)); 475 DELAY(10); 476 ostat0 = ATA_IDX_INB(ch, ATA_STATUS); 477 if (((ostat0 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) && 478 ostat0 != 0xa5) { 479 stat0 = ATA_S_BUSY; 480 mask |= 0x01; 481 } 482 483 /* in some setups we dont want to test for a slave */ 484 if (!(ch->flags & ATA_NO_SLAVE)) { 485 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_SLAVE)); 486 DELAY(10); 487 ostat1 = ATA_IDX_INB(ch, ATA_STATUS); 488 if (((ostat1 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) && 489 ostat1 != 0xa5) { 490 stat1 = ATA_S_BUSY; 491 mask |= 0x02; 492 } 493 } 494 495 if (bootverbose) 496 device_printf(dev, "reset tp1 mask=%02x ostat0=%02x ostat1=%02x\n", 497 mask, ostat0, ostat1); 498 499 /* if nothing showed up there is no need to get any further */ 500 /* XXX SOS is that too strong?, we just might loose devices here */ 501 ch->devices = 0; 502 if (!mask) 503 return; 504 505 /* reset (both) devices on this channel */ 506 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER)); 507 DELAY(10); 508 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS | ATA_A_RESET); 509 ata_udelay(10000); 510 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS); 511 ata_udelay(100000); 512 ATA_IDX_INB(ch, ATA_ERROR); 513 514 /* wait for BUSY to go inactive */ 515 for (timeout = 0; timeout < 310; timeout++) { 516 if ((mask & 0x01) && (stat0 & ATA_S_BUSY)) { 517 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_MASTER)); 518 DELAY(10); 519 if (ch->flags & ATA_STATUS_IS_LONG) 520 stat0 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff; 521 else 522 stat0 = ATA_IDX_INB(ch, ATA_STATUS); 523 err = ATA_IDX_INB(ch, ATA_ERROR); 524 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB); 525 msb = ATA_IDX_INB(ch, ATA_CYL_MSB); 526 if (bootverbose) 527 device_printf(dev, 528 "stat0=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n", 529 stat0, err, lsb, msb); 530 if (stat0 == err && lsb == err && msb == err && 531 timeout > (stat0 & ATA_S_BUSY ? 100 : 10)) 532 mask &= ~0x01; 533 if (!(stat0 & ATA_S_BUSY)) { 534 if ((err & 0x7f) == ATA_E_ILI) { 535 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) { 536 ch->devices |= ATA_ATAPI_MASTER; 537 } 538 else if (lsb == 0 && msb == 0 && (stat0 & ATA_S_READY)) { 539 ch->devices |= ATA_ATA_MASTER; 540 } 541 } 542 else if ((stat0 & 0x0f) && err == lsb && err == msb) { 543 stat0 |= ATA_S_BUSY; 544 } 545 } 546 } 547 548 if ((mask & 0x02) && (stat1 & ATA_S_BUSY) && 549 !((mask & 0x01) && (stat0 & ATA_S_BUSY))) { 550 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_SLAVE)); 551 DELAY(10); 552 if (ch->flags & ATA_STATUS_IS_LONG) 553 stat1 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff; 554 else 555 stat1 = ATA_IDX_INB(ch, ATA_STATUS); 556 err = ATA_IDX_INB(ch, ATA_ERROR); 557 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB); 558 msb = ATA_IDX_INB(ch, ATA_CYL_MSB); 559 if (bootverbose) 560 device_printf(dev, 561 "stat1=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n", 562 stat1, err, lsb, msb); 563 if (stat1 == err && lsb == err && msb == err && 564 timeout > (stat1 & ATA_S_BUSY ? 100 : 10)) 565 mask &= ~0x02; 566 if (!(stat1 & ATA_S_BUSY)) { 567 if ((err & 0x7f) == ATA_E_ILI) { 568 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) { 569 ch->devices |= ATA_ATAPI_SLAVE; 570 } 571 else if (lsb == 0 && msb == 0 && (stat1 & ATA_S_READY)) { 572 ch->devices |= ATA_ATA_SLAVE; 573 } 574 } 575 else if ((stat1 & 0x0f) && err == lsb && err == msb) { 576 stat1 |= ATA_S_BUSY; 577 } 578 } 579 } 580 581 if ((ch->flags & ATA_KNOWN_PRESENCE) == 0 && 582 timeout > ((mask == 0x03) ? 20 : 10)) { 583 if ((mask & 0x01) && stat0 == 0xff) 584 mask &= ~0x01; 585 if ((mask & 0x02) && stat1 == 0xff) 586 mask &= ~0x02; 587 } 588 if (((mask & 0x01) == 0 || !(stat0 & ATA_S_BUSY)) && 589 ((mask & 0x02) == 0 || !(stat1 & ATA_S_BUSY))) 590 break; 591 ata_udelay(100000); 592 } 593 594 if (bootverbose) 595 device_printf(dev, "reset tp2 stat0=%02x stat1=%02x devices=0x%x\n", 596 stat0, stat1, ch->devices); 597 } 598 599 /* must be called with ATA channel locked and state_mtx held */ 600 int 601 ata_generic_status(device_t dev) 602 { 603 struct ata_channel *ch = device_get_softc(dev); 604 605 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 606 DELAY(100); 607 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 608 return 0; 609 } 610 return 1; 611 } 612 613 static int 614 ata_wait(struct ata_channel *ch, int unit, u_int8_t mask) 615 { 616 u_int8_t status; 617 int timeout = 0; 618 619 DELAY(1); 620 621 /* wait at max 1 second for device to get !BUSY */ 622 while (timeout < 1000000) { 623 status = ATA_IDX_INB(ch, ATA_ALTSTAT); 624 625 /* if drive fails status, reselect the drive and try again */ 626 if (status == 0xff) { 627 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(unit)); 628 timeout += 1000; 629 DELAY(1000); 630 continue; 631 } 632 633 /* are we done ? */ 634 if (!(status & ATA_S_BUSY)) 635 break; 636 637 if (timeout > 1000) { 638 timeout += 1000; 639 DELAY(1000); 640 } 641 else { 642 timeout += 10; 643 DELAY(10); 644 } 645 } 646 if (timeout >= 1000000) 647 return -2; 648 if (!mask) 649 return (status & ATA_S_ERROR); 650 651 DELAY(1); 652 653 /* wait 50 msec for bits wanted */ 654 timeout = 5000; 655 while (timeout--) { 656 status = ATA_IDX_INB(ch, ATA_ALTSTAT); 657 if ((status & mask) == mask) 658 return (status & ATA_S_ERROR); 659 DELAY(10); 660 } 661 return -3; 662 } 663 664 int 665 ata_generic_command(struct ata_request *request) 666 { 667 struct ata_channel *ch = device_get_softc(request->parent); 668 669 /* select device */ 670 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit)); 671 672 /* ready to issue command ? */ 673 if (ata_wait(ch, request->unit, 0) < 0) { 674 device_printf(request->parent, "timeout waiting to issue command\n"); 675 request->flags |= ATA_R_TIMEOUT; 676 return (-1); 677 } 678 679 /* enable interrupt */ 680 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT); 681 682 if (request->flags & ATA_R_ATAPI) { 683 int timeout = 5000; 684 int res; 685 686 /* issue packet command to controller */ 687 if (request->flags & ATA_R_DMA) { 688 ATA_IDX_OUTB(ch, ATA_FEATURE, ATA_F_DMA); 689 ATA_IDX_OUTB(ch, ATA_CYL_LSB, 0); 690 ATA_IDX_OUTB(ch, ATA_CYL_MSB, 0); 691 } 692 else { 693 ATA_IDX_OUTB(ch, ATA_FEATURE, 0); 694 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->transfersize); 695 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->transfersize >> 8); 696 } 697 ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_PACKET_CMD); 698 699 /* command interrupt device ? just return and wait for interrupt */ 700 if (request->flags & ATA_R_ATAPI_INTR) 701 return (0); 702 703 /* command processed ? */ 704 res = ata_wait(ch, request->unit, 0); 705 if (res != 0) { 706 if (res < 0) { 707 device_printf(request->parent, 708 "timeout waiting for PACKET command\n"); 709 request->flags |= ATA_R_TIMEOUT; 710 } 711 return (-1); 712 } 713 /* wait for ready to write ATAPI command block */ 714 while (timeout--) { 715 int reason = ATA_IDX_INB(ch, ATA_IREASON); 716 int status = ATA_IDX_INB(ch, ATA_STATUS); 717 718 if (((reason & (ATA_I_CMD | ATA_I_IN)) | 719 (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT) 720 break; 721 DELAY(20); 722 } 723 if (timeout <= 0) { 724 device_printf(request->parent, 725 "timeout waiting for ATAPI ready\n"); 726 request->flags |= ATA_R_TIMEOUT; 727 return (-1); 728 } 729 730 /* this seems to be needed for some (slow) devices */ 731 DELAY(10); 732 733 /* output command block */ 734 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb, 735 (request->flags & ATA_R_ATAPI16) ? 8 : 6); 736 } 737 else { 738 ch->hw.tf_write(request); 739 740 /* issue command to controller */ 741 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command); 742 } 743 return (0); 744 } 745 746 static void 747 ata_tf_read(struct ata_request *request) 748 { 749 struct ata_channel *ch = device_get_softc(request->parent); 750 751 if (request->flags & ATA_R_48BIT) { 752 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT | ATA_A_HOB); 753 request->u.ata.count = (ATA_IDX_INB(ch, ATA_COUNT) << 8); 754 request->u.ata.lba = 755 ((u_int64_t)(ATA_IDX_INB(ch, ATA_SECTOR)) << 24) | 756 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_LSB)) << 32) | 757 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_MSB)) << 40); 758 759 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT); 760 request->u.ata.count |= ATA_IDX_INB(ch, ATA_COUNT); 761 request->u.ata.lba |= 762 (ATA_IDX_INB(ch, ATA_SECTOR) | 763 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) | 764 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16)); 765 } 766 else { 767 request->u.ata.count = ATA_IDX_INB(ch, ATA_COUNT); 768 request->u.ata.lba = ATA_IDX_INB(ch, ATA_SECTOR) | 769 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) | 770 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16) | 771 ((ATA_IDX_INB(ch, ATA_DRIVE) & 0xf) << 24); 772 } 773 } 774 775 static void 776 ata_tf_write(struct ata_request *request) 777 { 778 struct ata_channel *ch = device_get_softc(request->parent); 779 #ifndef ATA_CAM 780 struct ata_device *atadev = device_get_softc(request->dev); 781 #endif 782 783 if (request->flags & ATA_R_48BIT) { 784 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature >> 8); 785 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature); 786 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count >> 8); 787 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count); 788 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba >> 24); 789 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba); 790 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 32); 791 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8); 792 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 40); 793 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16); 794 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit)); 795 } 796 else { 797 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature); 798 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count); 799 #ifndef ATA_CAM 800 if (atadev->flags & ATA_D_USE_CHS) { 801 int heads, sectors; 802 803 if (atadev->param.atavalid & ATA_FLAG_54_58) { 804 heads = atadev->param.current_heads; 805 sectors = atadev->param.current_sectors; 806 } 807 else { 808 heads = atadev->param.heads; 809 sectors = atadev->param.sectors; 810 } 811 812 ATA_IDX_OUTB(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1); 813 ATA_IDX_OUTB(ch, ATA_CYL_LSB, 814 (request->u.ata.lba / (sectors * heads))); 815 ATA_IDX_OUTB(ch, ATA_CYL_MSB, 816 (request->u.ata.lba / (sectors * heads)) >> 8); 817 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit) | 818 (((request->u.ata.lba% (sectors * heads)) / 819 sectors) & 0xf)); 820 } 821 else { 822 #endif 823 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba); 824 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8); 825 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16); 826 ATA_IDX_OUTB(ch, ATA_DRIVE, 827 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) | 828 ((request->u.ata.lba >> 24) & 0x0f)); 829 #ifndef ATA_CAM 830 } 831 #endif 832 } 833 } 834 835 static void 836 ata_pio_read(struct ata_request *request, int length) 837 { 838 struct ata_channel *ch = device_get_softc(request->parent); 839 int size = min(request->transfersize, length); 840 int resid; 841 uint8_t buf[2]; 842 843 if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t))) { 844 ATA_IDX_INSW_STRM(ch, ATA_DATA, 845 (void*)((uintptr_t)request->data+request->donecount), 846 size / sizeof(int16_t)); 847 if (size & 1) { 848 ATA_IDX_INSW_STRM(ch, ATA_DATA, (void*)buf, 1); 849 ((uint8_t *)request->data + request->donecount + 850 (size & ~1))[0] = buf[0]; 851 } 852 } else 853 ATA_IDX_INSL_STRM(ch, ATA_DATA, 854 (void*)((uintptr_t)request->data+request->donecount), 855 size / sizeof(int32_t)); 856 857 if (request->transfersize < length) { 858 device_printf(request->parent, "WARNING - %s read data overrun %d>%d\n", 859 ata_cmd2str(request), length, request->transfersize); 860 for (resid = request->transfersize + (size & 1); resid < length; 861 resid += sizeof(int16_t)) 862 ATA_IDX_INW(ch, ATA_DATA); 863 } 864 } 865 866 static void 867 ata_pio_write(struct ata_request *request, int length) 868 { 869 struct ata_channel *ch = device_get_softc(request->parent); 870 int size = min(request->transfersize, length); 871 int resid; 872 uint8_t buf[2]; 873 874 if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t))) { 875 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, 876 (void*)((uintptr_t)request->data+request->donecount), 877 size / sizeof(int16_t)); 878 if (size & 1) { 879 buf[0] = ((uint8_t *)request->data + request->donecount + 880 (size & ~1))[0]; 881 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (void*)buf, 1); 882 } 883 } else 884 ATA_IDX_OUTSL_STRM(ch, ATA_DATA, 885 (void*)((uintptr_t)request->data+request->donecount), 886 size / sizeof(int32_t)); 887 888 if (request->transfersize < length) { 889 device_printf(request->parent, "WARNING - %s write data underrun %d>%d\n", 890 ata_cmd2str(request), length, request->transfersize); 891 for (resid = request->transfersize + (size & 1); resid < length; 892 resid += sizeof(int16_t)) 893 ATA_IDX_OUTW(ch, ATA_DATA, 0); 894 } 895 } 896