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>
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>
20 #include <asm/byteorder.h>
22 #include <asm/uaccess.h>
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
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
36 * ide_xfer_verbose - return IDE mode names
37 * @xfer_rate: rate to name
39 * Returns a constant string giving the name of the mode
43 char *ide_xfer_verbose (u8 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");
70 EXPORT_SYMBOL(ide_xfer_verbose
);
73 * ide_dma_speed - compute DMA speed
75 * @mode; intended mode
77 * Checks the drive capabilities and returns the speed to use
78 * for the transfer. Returns -1 if the requested mode is unknown
82 u8
ide_dma_speed(ide_drive_t
*drive
, u8 mode
)
84 struct hd_driveid
*id
= drive
->id
;
85 ide_hwif_t
*hwif
= HWIF(drive
);
88 if (drive
->media
!= ide_disk
&& hwif
->atapi_dma
== 0)
93 if ((id
->dma_ultra
& 0x0040) &&
94 (id
->dma_ultra
& hwif
->ultra_mask
))
95 { speed
= XFER_UDMA_6
; break; }
97 if ((id
->dma_ultra
& 0x0020) &&
98 (id
->dma_ultra
& hwif
->ultra_mask
))
99 { speed
= XFER_UDMA_5
; break; }
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; }
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; }
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; }
138 // printk("%s: %s: mode 0x%02x, speed 0x%02x\n",
139 // __FUNCTION__, drive->name, mode, speed);
144 EXPORT_SYMBOL(ide_dma_speed
);
148 * ide_rate_filter - return best speed for mode
149 * @mode: modes available
150 * @speed: desired speed
152 * Given the available DMA/UDMA mode this function returns
153 * the best available speed at or below the speed requested.
156 u8
ide_rate_filter (u8 mode
, u8 speed
)
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
164 // printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed);
166 /* So that we remember to update this if new modes appear */
169 return min(speed
, speed_max
[mode
]);
170 #else /* !CONFIG_BLK_DEV_IDEDMA */
171 return min(speed
, (u8
)XFER_PIO_4
);
172 #endif /* CONFIG_BLK_DEV_IDEDMA */
175 EXPORT_SYMBOL(ide_rate_filter
);
177 int ide_dma_enable (ide_drive_t
*drive
)
179 ide_hwif_t
*hwif
= HWIF(drive
);
180 struct hd_driveid
*id
= drive
->id
;
182 return ((int) ((((id
->dma_ultra
>> 8) & hwif
->ultra_mask
) ||
183 ((id
->dma_mword
>> 8) & hwif
->mwdma_mask
) ||
184 ((id
->dma_1word
>> 8) & hwif
->swdma_mask
)) ? 1 : 0));
187 EXPORT_SYMBOL(ide_dma_enable
);
190 * Standard (generic) timings for PIO modes, from ATA2 specification.
191 * These timings are for access to the IDE data port register *only*.
192 * Some drives may specify a mode, while also specifying a different
193 * value for cycle_time (from drive identification data).
195 const ide_pio_timings_t ide_pio_timings
[6] = {
196 { 70, 165, 600 }, /* PIO Mode 0 */
197 { 50, 125, 383 }, /* PIO Mode 1 */
198 { 30, 100, 240 }, /* PIO Mode 2 */
199 { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
200 { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
201 { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
204 EXPORT_SYMBOL_GPL(ide_pio_timings
);
207 * Shared data/functions for determining best PIO mode for an IDE drive.
208 * Most of this stuff originally lived in cmd640.c, and changes to the
209 * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
210 * breaking the fragile cmd640.c support.
214 * Black list. Some drives incorrectly report their maximal PIO mode,
215 * at least in respect to CMD640. Here we keep info on some known drives.
217 static struct ide_pio_info
{
220 } ide_pio_blacklist
[] = {
221 /* { "Conner Peripherals 1275MB - CFS1275A", 4 }, */
222 { "Conner Peripherals 540MB - CFS540A", 3 },
230 { "WDC AC21200", 4 },
237 /* { "WDC AC21000", 4 }, */
238 { "WDC AC31000", 3 },
239 { "WDC AC31200", 3 },
240 /* { "WDC AC31600", 4 }, */
242 { "Maxtor 7131 AT", 1 },
243 { "Maxtor 7171 AT", 1 },
244 { "Maxtor 7213 AT", 1 },
245 { "Maxtor 7245 AT", 1 },
246 { "Maxtor 7345 AT", 1 },
247 { "Maxtor 7546 AT", 3 },
248 { "Maxtor 7540 AV", 3 },
250 { "SAMSUNG SHD-3121A", 1 },
251 { "SAMSUNG SHD-3122A", 1 },
252 { "SAMSUNG SHD-3172A", 1 },
254 /* { "ST51080A", 4 },
270 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
271 /* drive) according to Seagates FIND-ATA program */
273 { "QUANTUM ELS127A", 0 },
274 { "QUANTUM ELS170A", 0 },
275 { "QUANTUM LPS240A", 0 },
276 { "QUANTUM LPS210A", 3 },
277 { "QUANTUM LPS270A", 3 },
278 { "QUANTUM LPS365A", 3 },
279 { "QUANTUM LPS540A", 3 },
280 { "QUANTUM LIGHTNING 540A", 3 },
281 { "QUANTUM LIGHTNING 730A", 3 },
283 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
284 { "QUANTUM FIREBALL_640", 3 },
285 { "QUANTUM FIREBALL_1080", 3 },
286 { "QUANTUM FIREBALL_1280", 3 },
291 * ide_scan_pio_blacklist - check for a blacklisted drive
292 * @model: Drive model string
294 * This routine searches the ide_pio_blacklist for an entry
295 * matching the start/whole of the supplied model name.
297 * Returns -1 if no match found.
298 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
301 static int ide_scan_pio_blacklist (char *model
)
303 struct ide_pio_info
*p
;
305 for (p
= ide_pio_blacklist
; p
->name
!= NULL
; p
++) {
306 if (strncmp(p
->name
, model
, strlen(p
->name
)) == 0)
313 * ide_get_best_pio_mode - get PIO mode from drive
314 * @driver: drive to consider
315 * @mode_wanted: preferred mode
316 * @max_mode: highest allowed
319 * This routine returns the recommended PIO settings for a given drive,
320 * based on the drive->id information and the ide_pio_blacklist[].
321 * This is used by most chipset support modules when "auto-tuning".
323 * Drive PIO mode auto selection
326 u8
ide_get_best_pio_mode (ide_drive_t
*drive
, u8 mode_wanted
, u8 max_mode
, ide_pio_data_t
*d
)
331 struct hd_driveid
* id
= drive
->id
;
335 if (mode_wanted
!= 255) {
336 pio_mode
= mode_wanted
;
337 } else if (!drive
->id
) {
339 } else if ((pio_mode
= ide_scan_pio_blacklist(id
->model
)) != -1) {
342 use_iordy
= (pio_mode
> 2);
345 if (pio_mode
> 2) { /* 2 is maximum allowed tPIO value */
349 if (id
->field_valid
& 2) { /* drive implements ATA2? */
350 if (id
->capability
& 8) { /* drive supports use_iordy? */
352 cycle_time
= id
->eide_pio_iordy
;
353 if (id
->eide_pio_modes
& 7) {
355 if (id
->eide_pio_modes
& 4)
357 else if (id
->eide_pio_modes
& 2)
363 cycle_time
= id
->eide_pio
;
368 if (drive
->id
->major_rev_num
& 0x0004) printk("ATA-2 ");
372 * Conservative "downgrade" for all pre-ATA2 drives
374 if (pio_mode
&& pio_mode
< 4) {
378 use_iordy
= (pio_mode
> 2);
380 if (cycle_time
&& cycle_time
< ide_pio_timings
[pio_mode
].cycle_time
)
381 cycle_time
= 0; /* use standard timing */
384 if (pio_mode
> max_mode
) {
389 d
->pio_mode
= pio_mode
;
390 d
->cycle_time
= cycle_time
? cycle_time
: ide_pio_timings
[pio_mode
].cycle_time
;
391 d
->use_iordy
= use_iordy
;
392 d
->overridden
= overridden
;
393 d
->blacklisted
= blacklisted
;
398 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode
);
401 * ide_toggle_bounce - handle bounce buffering
402 * @drive: drive to update
403 * @on: on/off boolean
405 * Enable or disable bounce buffering for the device. Drives move
406 * between PIO and DMA and that changes the rules we need.
409 void ide_toggle_bounce(ide_drive_t
*drive
, int on
)
411 u64 addr
= BLK_BOUNCE_HIGH
; /* dma64_addr_t */
413 if (!PCI_DMA_BUS_IS_PHYS
) {
414 addr
= BLK_BOUNCE_ANY
;
415 } else if (on
&& drive
->media
== ide_disk
) {
416 if (HWIF(drive
)->pci_dev
)
417 addr
= HWIF(drive
)->pci_dev
->dma_mask
;
421 blk_queue_bounce_limit(drive
->queue
, addr
);
425 * ide_set_xfer_rate - set transfer rate
426 * @drive: drive to set
427 * @speed: speed to attempt to set
429 * General helper for setting the speed of an IDE device. This
430 * function knows about user enforced limits from the configuration
431 * which speedproc() does not. High level drivers should never
432 * invoke speedproc() directly.
435 int ide_set_xfer_rate(ide_drive_t
*drive
, u8 rate
)
437 #ifndef CONFIG_BLK_DEV_IDEDMA
438 rate
= min(rate
, (u8
) XFER_PIO_4
);
440 if(HWIF(drive
)->speedproc
)
441 return HWIF(drive
)->speedproc(drive
, rate
);
446 EXPORT_SYMBOL_GPL(ide_set_xfer_rate
);
448 static void ide_dump_opcode(ide_drive_t
*drive
)
454 spin_lock(&ide_lock
);
457 rq
= HWGROUP(drive
)->rq
;
458 spin_unlock(&ide_lock
);
461 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
)) {
462 char *args
= rq
->buffer
;
467 } else if (rq
->flags
& REQ_DRIVE_TASKFILE
) {
468 ide_task_t
*args
= rq
->special
;
470 task_struct_t
*tf
= (task_struct_t
*) args
->tfRegister
;
471 opcode
= tf
->command
;
476 printk("ide: failed opcode was: ");
480 printk("0x%02x\n", opcode
);
483 static u8
ide_dump_ata_status(ide_drive_t
*drive
, const char *msg
, u8 stat
)
485 ide_hwif_t
*hwif
= HWIF(drive
);
489 local_irq_set(flags
);
490 printk("%s: %s: status=0x%02x { ", drive
->name
, msg
, stat
);
491 if (stat
& BUSY_STAT
)
494 if (stat
& READY_STAT
) printk("DriveReady ");
495 if (stat
& WRERR_STAT
) printk("DeviceFault ");
496 if (stat
& SEEK_STAT
) printk("SeekComplete ");
497 if (stat
& DRQ_STAT
) printk("DataRequest ");
498 if (stat
& ECC_STAT
) printk("CorrectedError ");
499 if (stat
& INDEX_STAT
) printk("Index ");
500 if (stat
& ERR_STAT
) printk("Error ");
503 if ((stat
& (BUSY_STAT
|ERR_STAT
)) == ERR_STAT
) {
504 err
= hwif
->INB(IDE_ERROR_REG
);
505 printk("%s: %s: error=0x%02x { ", drive
->name
, msg
, err
);
506 if (err
& ABRT_ERR
) printk("DriveStatusError ");
508 printk((err
& ABRT_ERR
) ? "BadCRC " : "BadSector ");
509 if (err
& ECC_ERR
) printk("UncorrectableError ");
510 if (err
& ID_ERR
) printk("SectorIdNotFound ");
511 if (err
& TRK0_ERR
) printk("TrackZeroNotFound ");
512 if (err
& MARK_ERR
) printk("AddrMarkNotFound ");
514 if ((err
& (BBD_ERR
| ABRT_ERR
)) == BBD_ERR
||
515 (err
& (ECC_ERR
|ID_ERR
|MARK_ERR
))) {
516 if (drive
->addressing
== 1) {
518 u32 low
= 0, high
= 0;
519 low
= ide_read_24(drive
);
520 hwif
->OUTB(drive
->ctl
|0x80, IDE_CONTROL_REG
);
521 high
= ide_read_24(drive
);
522 sectors
= ((__u64
)high
<< 24) | low
;
523 printk(", LBAsect=%llu, high=%d, low=%d",
524 (unsigned long long) sectors
,
527 u8 cur
= hwif
->INB(IDE_SELECT_REG
);
528 if (cur
& 0x40) { /* using LBA? */
529 printk(", LBAsect=%ld", (unsigned long)
531 |(hwif
->INB(IDE_HCYL_REG
)<<16)
532 |(hwif
->INB(IDE_LCYL_REG
)<<8)
533 | hwif
->INB(IDE_SECTOR_REG
));
535 printk(", CHS=%d/%d/%d",
536 (hwif
->INB(IDE_HCYL_REG
)<<8) +
537 hwif
->INB(IDE_LCYL_REG
),
539 hwif
->INB(IDE_SECTOR_REG
));
542 if (HWGROUP(drive
) && HWGROUP(drive
)->rq
)
543 printk(", sector=%llu",
544 (unsigned long long)HWGROUP(drive
)->rq
->sector
);
548 ide_dump_opcode(drive
);
549 local_irq_restore(flags
);
554 * ide_dump_atapi_status - print human readable atapi status
555 * @drive: drive that status applies to
556 * @msg: text message to print
557 * @stat: status byte to decode
559 * Error reporting, in human readable form (luxurious, but a memory hog).
562 static u8
ide_dump_atapi_status(ide_drive_t
*drive
, const char *msg
, u8 stat
)
566 atapi_status_t status
;
571 local_irq_set(flags
);
572 printk("%s: %s: status=0x%02x { ", drive
->name
, msg
, stat
);
576 if (status
.b
.drdy
) printk("DriveReady ");
577 if (status
.b
.df
) printk("DeviceFault ");
578 if (status
.b
.dsc
) printk("SeekComplete ");
579 if (status
.b
.drq
) printk("DataRequest ");
580 if (status
.b
.corr
) printk("CorrectedError ");
581 if (status
.b
.idx
) printk("Index ");
582 if (status
.b
.check
) printk("Error ");
585 if (status
.b
.check
&& !status
.b
.bsy
) {
586 error
.all
= HWIF(drive
)->INB(IDE_ERROR_REG
);
587 printk("%s: %s: error=0x%02x { ", drive
->name
, msg
, error
.all
);
588 if (error
.b
.ili
) printk("IllegalLengthIndication ");
589 if (error
.b
.eom
) printk("EndOfMedia ");
590 if (error
.b
.abrt
) printk("AbortedCommand ");
591 if (error
.b
.mcr
) printk("MediaChangeRequested ");
592 if (error
.b
.sense_key
) printk("LastFailedSense=0x%02x ",
596 ide_dump_opcode(drive
);
597 local_irq_restore(flags
);
602 * ide_dump_status - translate ATA/ATAPI error
603 * @drive: drive the error occured on
604 * @msg: information string
607 * Error reporting, in human readable form (luxurious, but a memory hog).
608 * Combines the drive name, message and status byte to provide a
609 * user understandable explanation of the device error.
612 u8
ide_dump_status(ide_drive_t
*drive
, const char *msg
, u8 stat
)
614 if (drive
->media
== ide_disk
)
615 return ide_dump_ata_status(drive
, msg
, stat
);
616 return ide_dump_atapi_status(drive
, msg
, stat
);
619 EXPORT_SYMBOL(ide_dump_status
);