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