ide-lib.c revision 3d1c1cc962cebaae6a70fd89a0adb29ad10a2a12
1#include <linux/config.h> 2#include <linux/module.h> 3#include <linux/types.h> 4#include <linux/string.h> 5#include <linux/kernel.h> 6#include <linux/timer.h> 7#include <linux/mm.h> 8#include <linux/interrupt.h> 9#include <linux/major.h> 10#include <linux/errno.h> 11#include <linux/genhd.h> 12#include <linux/blkpg.h> 13#include <linux/slab.h> 14#include <linux/pci.h> 15#include <linux/delay.h> 16#include <linux/hdreg.h> 17#include <linux/ide.h> 18#include <linux/bitops.h> 19 20#include <asm/byteorder.h> 21#include <asm/irq.h> 22#include <asm/uaccess.h> 23#include <asm/io.h> 24 25/* 26 * IDE library routines. These are plug in code that most 27 * drivers can use but occasionally may be weird enough 28 * to want to do their own thing with 29 * 30 * Add common non I/O op stuff here. Make sure it has proper 31 * kernel-doc function headers or your patch will be rejected 32 */ 33 34 35/** 36 * ide_xfer_verbose - return IDE mode names 37 * @xfer_rate: rate to name 38 * 39 * Returns a constant string giving the name of the mode 40 * requested. 41 */ 42 43char *ide_xfer_verbose (u8 xfer_rate) 44{ 45 switch(xfer_rate) { 46 case XFER_UDMA_7: return("UDMA 7"); 47 case XFER_UDMA_6: return("UDMA 6"); 48 case XFER_UDMA_5: return("UDMA 5"); 49 case XFER_UDMA_4: return("UDMA 4"); 50 case XFER_UDMA_3: return("UDMA 3"); 51 case XFER_UDMA_2: return("UDMA 2"); 52 case XFER_UDMA_1: return("UDMA 1"); 53 case XFER_UDMA_0: return("UDMA 0"); 54 case XFER_MW_DMA_2: return("MW DMA 2"); 55 case XFER_MW_DMA_1: return("MW DMA 1"); 56 case XFER_MW_DMA_0: return("MW DMA 0"); 57 case XFER_SW_DMA_2: return("SW DMA 2"); 58 case XFER_SW_DMA_1: return("SW DMA 1"); 59 case XFER_SW_DMA_0: return("SW DMA 0"); 60 case XFER_PIO_4: return("PIO 4"); 61 case XFER_PIO_3: return("PIO 3"); 62 case XFER_PIO_2: return("PIO 2"); 63 case XFER_PIO_1: return("PIO 1"); 64 case XFER_PIO_0: return("PIO 0"); 65 case XFER_PIO_SLOW: return("PIO SLOW"); 66 default: return("XFER ERROR"); 67 } 68} 69 70EXPORT_SYMBOL(ide_xfer_verbose); 71 72/** 73 * ide_dma_speed - compute DMA speed 74 * @drive: drive 75 * @mode; intended mode 76 * 77 * Checks the drive capabilities and returns the speed to use 78 * for the transfer. Returns -1 if the requested mode is unknown 79 * (eg PIO) 80 */ 81 82u8 ide_dma_speed(ide_drive_t *drive, u8 mode) 83{ 84 struct hd_driveid *id = drive->id; 85 ide_hwif_t *hwif = HWIF(drive); 86 u8 speed = 0; 87 88 if (drive->media != ide_disk && hwif->atapi_dma == 0) 89 return 0; 90 91 switch(mode) { 92 case 0x04: 93 if ((id->dma_ultra & 0x0040) && 94 (id->dma_ultra & hwif->ultra_mask)) 95 { speed = XFER_UDMA_6; break; } 96 case 0x03: 97 if ((id->dma_ultra & 0x0020) && 98 (id->dma_ultra & hwif->ultra_mask)) 99 { speed = XFER_UDMA_5; break; } 100 case 0x02: 101 if ((id->dma_ultra & 0x0010) && 102 (id->dma_ultra & hwif->ultra_mask)) 103 { speed = XFER_UDMA_4; break; } 104 if ((id->dma_ultra & 0x0008) && 105 (id->dma_ultra & hwif->ultra_mask)) 106 { speed = XFER_UDMA_3; break; } 107 case 0x01: 108 if ((id->dma_ultra & 0x0004) && 109 (id->dma_ultra & hwif->ultra_mask)) 110 { speed = XFER_UDMA_2; break; } 111 if ((id->dma_ultra & 0x0002) && 112 (id->dma_ultra & hwif->ultra_mask)) 113 { speed = XFER_UDMA_1; break; } 114 if ((id->dma_ultra & 0x0001) && 115 (id->dma_ultra & hwif->ultra_mask)) 116 { speed = XFER_UDMA_0; break; } 117 case 0x00: 118 if ((id->dma_mword & 0x0004) && 119 (id->dma_mword & hwif->mwdma_mask)) 120 { speed = XFER_MW_DMA_2; break; } 121 if ((id->dma_mword & 0x0002) && 122 (id->dma_mword & hwif->mwdma_mask)) 123 { speed = XFER_MW_DMA_1; break; } 124 if ((id->dma_mword & 0x0001) && 125 (id->dma_mword & hwif->mwdma_mask)) 126 { speed = XFER_MW_DMA_0; break; } 127 if ((id->dma_1word & 0x0004) && 128 (id->dma_1word & hwif->swdma_mask)) 129 { speed = XFER_SW_DMA_2; break; } 130 if ((id->dma_1word & 0x0002) && 131 (id->dma_1word & hwif->swdma_mask)) 132 { speed = XFER_SW_DMA_1; break; } 133 if ((id->dma_1word & 0x0001) && 134 (id->dma_1word & hwif->swdma_mask)) 135 { speed = XFER_SW_DMA_0; break; } 136 } 137 138// printk("%s: %s: mode 0x%02x, speed 0x%02x\n", 139// __FUNCTION__, drive->name, mode, speed); 140 141 return speed; 142} 143 144EXPORT_SYMBOL(ide_dma_speed); 145 146 147/** 148 * ide_rate_filter - return best speed for mode 149 * @mode: modes available 150 * @speed: desired speed 151 * 152 * Given the available DMA/UDMA mode this function returns 153 * the best available speed at or below the speed requested. 154 */ 155 156u8 ide_rate_filter (u8 mode, u8 speed) 157{ 158#ifdef CONFIG_BLK_DEV_IDEDMA 159 static u8 speed_max[] = { 160 XFER_MW_DMA_2, XFER_UDMA_2, XFER_UDMA_4, 161 XFER_UDMA_5, XFER_UDMA_6 162 }; 163 164// printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed); 165 166 /* So that we remember to update this if new modes appear */ 167 BUG_ON(mode > 4); 168 return min(speed, speed_max[mode]); 169#else /* !CONFIG_BLK_DEV_IDEDMA */ 170 return min(speed, (u8)XFER_PIO_4); 171#endif /* CONFIG_BLK_DEV_IDEDMA */ 172} 173 174EXPORT_SYMBOL(ide_rate_filter); 175 176int ide_dma_enable (ide_drive_t *drive) 177{ 178 ide_hwif_t *hwif = HWIF(drive); 179 struct hd_driveid *id = drive->id; 180 181 return ((int) ((((id->dma_ultra >> 8) & hwif->ultra_mask) || 182 ((id->dma_mword >> 8) & hwif->mwdma_mask) || 183 ((id->dma_1word >> 8) & hwif->swdma_mask)) ? 1 : 0)); 184} 185 186EXPORT_SYMBOL(ide_dma_enable); 187 188/* 189 * Standard (generic) timings for PIO modes, from ATA2 specification. 190 * These timings are for access to the IDE data port register *only*. 191 * Some drives may specify a mode, while also specifying a different 192 * value for cycle_time (from drive identification data). 193 */ 194const ide_pio_timings_t ide_pio_timings[6] = { 195 { 70, 165, 600 }, /* PIO Mode 0 */ 196 { 50, 125, 383 }, /* PIO Mode 1 */ 197 { 30, 100, 240 }, /* PIO Mode 2 */ 198 { 30, 80, 180 }, /* PIO Mode 3 with IORDY */ 199 { 25, 70, 120 }, /* PIO Mode 4 with IORDY */ 200 { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */ 201}; 202 203EXPORT_SYMBOL_GPL(ide_pio_timings); 204 205/* 206 * Shared data/functions for determining best PIO mode for an IDE drive. 207 * Most of this stuff originally lived in cmd640.c, and changes to the 208 * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid 209 * breaking the fragile cmd640.c support. 210 */ 211 212/* 213 * Black list. Some drives incorrectly report their maximal PIO mode, 214 * at least in respect to CMD640. Here we keep info on some known drives. 215 */ 216static struct ide_pio_info { 217 const char *name; 218 int pio; 219} ide_pio_blacklist [] = { 220/* { "Conner Peripherals 1275MB - CFS1275A", 4 }, */ 221 { "Conner Peripherals 540MB - CFS540A", 3 }, 222 223 { "WDC AC2700", 3 }, 224 { "WDC AC2540", 3 }, 225 { "WDC AC2420", 3 }, 226 { "WDC AC2340", 3 }, 227 { "WDC AC2250", 0 }, 228 { "WDC AC2200", 0 }, 229 { "WDC AC21200", 4 }, 230 { "WDC AC2120", 0 }, 231 { "WDC AC2850", 3 }, 232 { "WDC AC1270", 3 }, 233 { "WDC AC1170", 1 }, 234 { "WDC AC1210", 1 }, 235 { "WDC AC280", 0 }, 236/* { "WDC AC21000", 4 }, */ 237 { "WDC AC31000", 3 }, 238 { "WDC AC31200", 3 }, 239/* { "WDC AC31600", 4 }, */ 240 241 { "Maxtor 7131 AT", 1 }, 242 { "Maxtor 7171 AT", 1 }, 243 { "Maxtor 7213 AT", 1 }, 244 { "Maxtor 7245 AT", 1 }, 245 { "Maxtor 7345 AT", 1 }, 246 { "Maxtor 7546 AT", 3 }, 247 { "Maxtor 7540 AV", 3 }, 248 249 { "SAMSUNG SHD-3121A", 1 }, 250 { "SAMSUNG SHD-3122A", 1 }, 251 { "SAMSUNG SHD-3172A", 1 }, 252 253/* { "ST51080A", 4 }, 254 * { "ST51270A", 4 }, 255 * { "ST31220A", 4 }, 256 * { "ST31640A", 4 }, 257 * { "ST32140A", 4 }, 258 * { "ST3780A", 4 }, 259 */ 260 { "ST5660A", 3 }, 261 { "ST3660A", 3 }, 262 { "ST3630A", 3 }, 263 { "ST3655A", 3 }, 264 { "ST3391A", 3 }, 265 { "ST3390A", 1 }, 266 { "ST3600A", 1 }, 267 { "ST3290A", 0 }, 268 { "ST3144A", 0 }, 269 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */ 270 /* drive) according to Seagates FIND-ATA program */ 271 272 { "QUANTUM ELS127A", 0 }, 273 { "QUANTUM ELS170A", 0 }, 274 { "QUANTUM LPS240A", 0 }, 275 { "QUANTUM LPS210A", 3 }, 276 { "QUANTUM LPS270A", 3 }, 277 { "QUANTUM LPS365A", 3 }, 278 { "QUANTUM LPS540A", 3 }, 279 { "QUANTUM LIGHTNING 540A", 3 }, 280 { "QUANTUM LIGHTNING 730A", 3 }, 281 282 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */ 283 { "QUANTUM FIREBALL_640", 3 }, 284 { "QUANTUM FIREBALL_1080", 3 }, 285 { "QUANTUM FIREBALL_1280", 3 }, 286 { NULL, 0 } 287}; 288 289/** 290 * ide_scan_pio_blacklist - check for a blacklisted drive 291 * @model: Drive model string 292 * 293 * This routine searches the ide_pio_blacklist for an entry 294 * matching the start/whole of the supplied model name. 295 * 296 * Returns -1 if no match found. 297 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[]. 298 */ 299 300static int ide_scan_pio_blacklist (char *model) 301{ 302 struct ide_pio_info *p; 303 304 for (p = ide_pio_blacklist; p->name != NULL; p++) { 305 if (strncmp(p->name, model, strlen(p->name)) == 0) 306 return p->pio; 307 } 308 return -1; 309} 310 311/** 312 * ide_get_best_pio_mode - get PIO mode from drive 313 * @driver: drive to consider 314 * @mode_wanted: preferred mode 315 * @max_mode: highest allowed 316 * @d: pio data 317 * 318 * This routine returns the recommended PIO settings for a given drive, 319 * based on the drive->id information and the ide_pio_blacklist[]. 320 * This is used by most chipset support modules when "auto-tuning". 321 * 322 * Drive PIO mode auto selection 323 */ 324 325u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode, ide_pio_data_t *d) 326{ 327 int pio_mode; 328 int cycle_time = 0; 329 int use_iordy = 0; 330 struct hd_driveid* id = drive->id; 331 int overridden = 0; 332 int blacklisted = 0; 333 334 if (mode_wanted != 255) { 335 pio_mode = mode_wanted; 336 } else if (!drive->id) { 337 pio_mode = 0; 338 } else if ((pio_mode = ide_scan_pio_blacklist(id->model)) != -1) { 339 overridden = 1; 340 blacklisted = 1; 341 use_iordy = (pio_mode > 2); 342 } else { 343 pio_mode = id->tPIO; 344 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */ 345 pio_mode = 2; 346 overridden = 1; 347 } 348 if (id->field_valid & 2) { /* drive implements ATA2? */ 349 if (id->capability & 8) { /* drive supports use_iordy? */ 350 use_iordy = 1; 351 cycle_time = id->eide_pio_iordy; 352 if (id->eide_pio_modes & 7) { 353 overridden = 0; 354 if (id->eide_pio_modes & 4) 355 pio_mode = 5; 356 else if (id->eide_pio_modes & 2) 357 pio_mode = 4; 358 else 359 pio_mode = 3; 360 } 361 } else { 362 cycle_time = id->eide_pio; 363 } 364 } 365 366#if 0 367 if (drive->id->major_rev_num & 0x0004) printk("ATA-2 "); 368#endif 369 370 /* 371 * Conservative "downgrade" for all pre-ATA2 drives 372 */ 373 if (pio_mode && pio_mode < 4) { 374 pio_mode--; 375 overridden = 1; 376#if 0 377 use_iordy = (pio_mode > 2); 378#endif 379 if (cycle_time && cycle_time < ide_pio_timings[pio_mode].cycle_time) 380 cycle_time = 0; /* use standard timing */ 381 } 382 } 383 if (pio_mode > max_mode) { 384 pio_mode = max_mode; 385 cycle_time = 0; 386 } 387 if (d) { 388 d->pio_mode = pio_mode; 389 d->cycle_time = cycle_time ? cycle_time : ide_pio_timings[pio_mode].cycle_time; 390 d->use_iordy = use_iordy; 391 d->overridden = overridden; 392 d->blacklisted = blacklisted; 393 } 394 return pio_mode; 395} 396 397EXPORT_SYMBOL_GPL(ide_get_best_pio_mode); 398 399/** 400 * ide_toggle_bounce - handle bounce buffering 401 * @drive: drive to update 402 * @on: on/off boolean 403 * 404 * Enable or disable bounce buffering for the device. Drives move 405 * between PIO and DMA and that changes the rules we need. 406 */ 407 408void ide_toggle_bounce(ide_drive_t *drive, int on) 409{ 410 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */ 411 412 if (!PCI_DMA_BUS_IS_PHYS) { 413 addr = BLK_BOUNCE_ANY; 414 } else if (on && drive->media == ide_disk) { 415 if (HWIF(drive)->pci_dev) 416 addr = HWIF(drive)->pci_dev->dma_mask; 417 } 418 419 if (drive->queue) 420 blk_queue_bounce_limit(drive->queue, addr); 421} 422 423/** 424 * ide_set_xfer_rate - set transfer rate 425 * @drive: drive to set 426 * @speed: speed to attempt to set 427 * 428 * General helper for setting the speed of an IDE device. This 429 * function knows about user enforced limits from the configuration 430 * which speedproc() does not. High level drivers should never 431 * invoke speedproc() directly. 432 */ 433 434int ide_set_xfer_rate(ide_drive_t *drive, u8 rate) 435{ 436#ifndef CONFIG_BLK_DEV_IDEDMA 437 rate = min(rate, (u8) XFER_PIO_4); 438#endif 439 if(HWIF(drive)->speedproc) 440 return HWIF(drive)->speedproc(drive, rate); 441 else 442 return -1; 443} 444 445EXPORT_SYMBOL_GPL(ide_set_xfer_rate); 446 447static void ide_dump_opcode(ide_drive_t *drive) 448{ 449 struct request *rq; 450 u8 opcode = 0; 451 int found = 0; 452 453 spin_lock(&ide_lock); 454 rq = NULL; 455 if (HWGROUP(drive)) 456 rq = HWGROUP(drive)->rq; 457 spin_unlock(&ide_lock); 458 if (!rq) 459 return; 460 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) { 461 char *args = rq->buffer; 462 if (args) { 463 opcode = args[0]; 464 found = 1; 465 } 466 } else if (rq->flags & REQ_DRIVE_TASKFILE) { 467 ide_task_t *args = rq->special; 468 if (args) { 469 task_struct_t *tf = (task_struct_t *) args->tfRegister; 470 opcode = tf->command; 471 found = 1; 472 } 473 } 474 475 printk("ide: failed opcode was: "); 476 if (!found) 477 printk("unknown\n"); 478 else 479 printk("0x%02x\n", opcode); 480} 481 482static u8 ide_dump_ata_status(ide_drive_t *drive, const char *msg, u8 stat) 483{ 484 ide_hwif_t *hwif = HWIF(drive); 485 unsigned long flags; 486 u8 err = 0; 487 488 local_irq_save(flags); 489 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat); 490 if (stat & BUSY_STAT) 491 printk("Busy "); 492 else { 493 if (stat & READY_STAT) printk("DriveReady "); 494 if (stat & WRERR_STAT) printk("DeviceFault "); 495 if (stat & SEEK_STAT) printk("SeekComplete "); 496 if (stat & DRQ_STAT) printk("DataRequest "); 497 if (stat & ECC_STAT) printk("CorrectedError "); 498 if (stat & INDEX_STAT) printk("Index "); 499 if (stat & ERR_STAT) printk("Error "); 500 } 501 printk("}\n"); 502 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) { 503 err = hwif->INB(IDE_ERROR_REG); 504 printk("%s: %s: error=0x%02x { ", drive->name, msg, err); 505 if (err & ABRT_ERR) printk("DriveStatusError "); 506 if (err & ICRC_ERR) 507 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector "); 508 if (err & ECC_ERR) printk("UncorrectableError "); 509 if (err & ID_ERR) printk("SectorIdNotFound "); 510 if (err & TRK0_ERR) printk("TrackZeroNotFound "); 511 if (err & MARK_ERR) printk("AddrMarkNotFound "); 512 printk("}"); 513 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || 514 (err & (ECC_ERR|ID_ERR|MARK_ERR))) { 515 if (drive->addressing == 1) { 516 __u64 sectors = 0; 517 u32 low = 0, high = 0; 518 low = ide_read_24(drive); 519 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG); 520 high = ide_read_24(drive); 521 sectors = ((__u64)high << 24) | low; 522 printk(", LBAsect=%llu, high=%d, low=%d", 523 (unsigned long long) sectors, 524 high, low); 525 } else { 526 u8 cur = hwif->INB(IDE_SELECT_REG); 527 if (cur & 0x40) { /* using LBA? */ 528 printk(", LBAsect=%ld", (unsigned long) 529 ((cur&0xf)<<24) 530 |(hwif->INB(IDE_HCYL_REG)<<16) 531 |(hwif->INB(IDE_LCYL_REG)<<8) 532 | hwif->INB(IDE_SECTOR_REG)); 533 } else { 534 printk(", CHS=%d/%d/%d", 535 (hwif->INB(IDE_HCYL_REG)<<8) + 536 hwif->INB(IDE_LCYL_REG), 537 cur & 0xf, 538 hwif->INB(IDE_SECTOR_REG)); 539 } 540 } 541 if (HWGROUP(drive) && HWGROUP(drive)->rq) 542 printk(", sector=%llu", 543 (unsigned long long)HWGROUP(drive)->rq->sector); 544 } 545 printk("\n"); 546 } 547 ide_dump_opcode(drive); 548 local_irq_restore(flags); 549 return err; 550} 551 552/** 553 * ide_dump_atapi_status - print human readable atapi status 554 * @drive: drive that status applies to 555 * @msg: text message to print 556 * @stat: status byte to decode 557 * 558 * Error reporting, in human readable form (luxurious, but a memory hog). 559 */ 560 561static u8 ide_dump_atapi_status(ide_drive_t *drive, const char *msg, u8 stat) 562{ 563 unsigned long flags; 564 565 atapi_status_t status; 566 atapi_error_t error; 567 568 status.all = stat; 569 error.all = 0; 570 local_irq_save(flags); 571 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat); 572 if (status.b.bsy) 573 printk("Busy "); 574 else { 575 if (status.b.drdy) printk("DriveReady "); 576 if (status.b.df) printk("DeviceFault "); 577 if (status.b.dsc) printk("SeekComplete "); 578 if (status.b.drq) printk("DataRequest "); 579 if (status.b.corr) printk("CorrectedError "); 580 if (status.b.idx) printk("Index "); 581 if (status.b.check) printk("Error "); 582 } 583 printk("}\n"); 584 if (status.b.check && !status.b.bsy) { 585 error.all = HWIF(drive)->INB(IDE_ERROR_REG); 586 printk("%s: %s: error=0x%02x { ", drive->name, msg, error.all); 587 if (error.b.ili) printk("IllegalLengthIndication "); 588 if (error.b.eom) printk("EndOfMedia "); 589 if (error.b.abrt) printk("AbortedCommand "); 590 if (error.b.mcr) printk("MediaChangeRequested "); 591 if (error.b.sense_key) printk("LastFailedSense=0x%02x ", 592 error.b.sense_key); 593 printk("}\n"); 594 } 595 ide_dump_opcode(drive); 596 local_irq_restore(flags); 597 return error.all; 598} 599 600/** 601 * ide_dump_status - translate ATA/ATAPI error 602 * @drive: drive the error occured on 603 * @msg: information string 604 * @stat: status byte 605 * 606 * Error reporting, in human readable form (luxurious, but a memory hog). 607 * Combines the drive name, message and status byte to provide a 608 * user understandable explanation of the device error. 609 */ 610 611u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat) 612{ 613 if (drive->media == ide_disk) 614 return ide_dump_ata_status(drive, msg, stat); 615 return ide_dump_atapi_status(drive, msg, stat); 616} 617 618EXPORT_SYMBOL(ide_dump_status); 619