1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/interrupt.h>
5 #include <linux/hdreg.h>
7 #include <linux/bitops.h>
9 static const char *udma_str[] =
10 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
11 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
12 static const char *mwdma_str[] =
13 { "MWDMA0", "MWDMA1", "MWDMA2" };
14 static const char *swdma_str[] =
15 { "SWDMA0", "SWDMA1", "SWDMA2" };
16 static const char *pio_str[] =
17 { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
20 * ide_xfer_verbose - return IDE mode names
21 * @mode: transfer mode
23 * Returns a constant string giving the name of the mode
27 const char *ide_xfer_verbose(u8 mode)
32 if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
34 else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
36 else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
38 else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
40 else if (mode == XFER_PIO_SLOW)
48 EXPORT_SYMBOL(ide_xfer_verbose);
51 * ide_rate_filter - filter transfer mode
53 * @speed: desired speed
55 * Given the available transfer modes this function returns
56 * the best available speed at or below the speed requested.
58 * TODO: check device PIO capabilities
61 static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
63 ide_hwif_t *hwif = drive->hwif;
64 u8 mode = ide_find_dma_mode(drive, speed);
68 mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
73 /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
75 return min(speed, mode);
79 * Standard (generic) timings for PIO modes, from ATA2 specification.
80 * These timings are for access to the IDE data port register *only*.
81 * Some drives may specify a mode, while also specifying a different
82 * value for cycle_time (from drive identification data).
84 const ide_pio_timings_t ide_pio_timings[6] = {
85 { 70, 165, 600 }, /* PIO Mode 0 */
86 { 50, 125, 383 }, /* PIO Mode 1 */
87 { 30, 100, 240 }, /* PIO Mode 2 */
88 { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
89 { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
90 { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
93 EXPORT_SYMBOL_GPL(ide_pio_timings);
96 * Shared data/functions for determining best PIO mode for an IDE drive.
97 * Most of this stuff originally lived in cmd640.c, and changes to the
98 * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
99 * breaking the fragile cmd640.c support.
103 * Black list. Some drives incorrectly report their maximal PIO mode,
104 * at least in respect to CMD640. Here we keep info on some known drives.
106 static struct ide_pio_info {
109 } ide_pio_blacklist [] = {
110 { "Conner Peripherals 540MB - CFS540A", 3 },
118 { "WDC AC21200", 4 },
125 { "WDC AC31000", 3 },
126 { "WDC AC31200", 3 },
128 { "Maxtor 7131 AT", 1 },
129 { "Maxtor 7171 AT", 1 },
130 { "Maxtor 7213 AT", 1 },
131 { "Maxtor 7245 AT", 1 },
132 { "Maxtor 7345 AT", 1 },
133 { "Maxtor 7546 AT", 3 },
134 { "Maxtor 7540 AV", 3 },
136 { "SAMSUNG SHD-3121A", 1 },
137 { "SAMSUNG SHD-3122A", 1 },
138 { "SAMSUNG SHD-3172A", 1 },
149 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
150 /* drive) according to Seagates FIND-ATA program */
152 { "QUANTUM ELS127A", 0 },
153 { "QUANTUM ELS170A", 0 },
154 { "QUANTUM LPS240A", 0 },
155 { "QUANTUM LPS210A", 3 },
156 { "QUANTUM LPS270A", 3 },
157 { "QUANTUM LPS365A", 3 },
158 { "QUANTUM LPS540A", 3 },
159 { "QUANTUM LIGHTNING 540A", 3 },
160 { "QUANTUM LIGHTNING 730A", 3 },
162 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
163 { "QUANTUM FIREBALL_640", 3 },
164 { "QUANTUM FIREBALL_1080", 3 },
165 { "QUANTUM FIREBALL_1280", 3 },
170 * ide_scan_pio_blacklist - check for a blacklisted drive
171 * @model: Drive model string
173 * This routine searches the ide_pio_blacklist for an entry
174 * matching the start/whole of the supplied model name.
176 * Returns -1 if no match found.
177 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
180 static int ide_scan_pio_blacklist (char *model)
182 struct ide_pio_info *p;
184 for (p = ide_pio_blacklist; p->name != NULL; p++) {
185 if (strncmp(p->name, model, strlen(p->name)) == 0)
192 * ide_get_best_pio_mode - get PIO mode from drive
193 * @drive: drive to consider
194 * @mode_wanted: preferred mode
195 * @max_mode: highest allowed mode
197 * This routine returns the recommended PIO settings for a given drive,
198 * based on the drive->id information and the ide_pio_blacklist[].
200 * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
201 * This is used by most chipset support modules when "auto-tuning".
204 u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
207 struct hd_driveid* id = drive->id;
210 if (mode_wanted != 255)
211 return min_t(u8, mode_wanted, max_mode);
213 if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
214 (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
215 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
218 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
222 if (id->field_valid & 2) { /* drive implements ATA2? */
223 if (id->capability & 8) { /* IORDY supported? */
224 if (id->eide_pio_modes & 7) {
226 if (id->eide_pio_modes & 4)
228 else if (id->eide_pio_modes & 2)
237 printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
241 if (pio_mode > max_mode)
247 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
249 /* req_pio == "255" for auto-tune */
250 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
252 ide_hwif_t *hwif = drive->hwif;
253 const struct ide_port_ops *port_ops = hwif->port_ops;
256 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
257 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
260 BUG_ON(hwif->pio_mask == 0x00);
262 host_pio = fls(hwif->pio_mask) - 1;
264 pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
268 * - report device max PIO mode
269 * - check req_pio != 255 against device max PIO mode
271 printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
272 drive->name, host_pio, req_pio,
273 req_pio == 255 ? "(auto-tune)" : "", pio);
275 (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
278 EXPORT_SYMBOL_GPL(ide_set_pio);
281 * ide_toggle_bounce - handle bounce buffering
282 * @drive: drive to update
283 * @on: on/off boolean
285 * Enable or disable bounce buffering for the device. Drives move
286 * between PIO and DMA and that changes the rules we need.
289 void ide_toggle_bounce(ide_drive_t *drive, int on)
291 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
293 if (!PCI_DMA_BUS_IS_PHYS) {
294 addr = BLK_BOUNCE_ANY;
295 } else if (on && drive->media == ide_disk) {
296 struct device *dev = drive->hwif->dev;
298 if (dev && dev->dma_mask)
299 addr = *dev->dma_mask;
303 blk_queue_bounce_limit(drive->queue, addr);
306 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
308 ide_hwif_t *hwif = drive->hwif;
309 const struct ide_port_ops *port_ops = hwif->port_ops;
311 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
314 if (port_ops == NULL || port_ops->set_pio_mode == NULL)
318 * TODO: temporary hack for some legacy host drivers that didn't
319 * set transfer mode on the device in ->set_pio_mode method...
321 if (port_ops->set_dma_mode == NULL) {
322 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
326 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
327 if (ide_config_drive_speed(drive, mode))
329 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
332 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
333 return ide_config_drive_speed(drive, mode);
337 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
339 ide_hwif_t *hwif = drive->hwif;
340 const struct ide_port_ops *port_ops = hwif->port_ops;
342 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
345 if (port_ops == NULL || port_ops->set_dma_mode == NULL)
348 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
349 if (ide_config_drive_speed(drive, mode))
351 port_ops->set_dma_mode(drive, mode);
354 port_ops->set_dma_mode(drive, mode);
355 return ide_config_drive_speed(drive, mode);
359 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
362 * ide_set_xfer_rate - set transfer rate
363 * @drive: drive to set
364 * @rate: speed to attempt to set
366 * General helper for setting the speed of an IDE device. This
367 * function knows about user enforced limits from the configuration
368 * which ->set_pio_mode/->set_dma_mode does not.
371 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
373 ide_hwif_t *hwif = drive->hwif;
374 const struct ide_port_ops *port_ops = hwif->port_ops;
376 if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
377 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
380 rate = ide_rate_filter(drive, rate);
382 if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
383 return ide_set_pio_mode(drive, rate);
386 * TODO: transfer modes 0x00-0x07 passed from the user-space are
387 * currently handled here which needs fixing (please note that such
388 * case could happen iff the transfer mode has already been set on
389 * the device by ide-proc.c::set_xfer_rate()).
391 if (rate < XFER_PIO_0) {
392 if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
393 return ide_set_dma_mode(drive, rate);
395 return ide_config_drive_speed(drive, rate);
398 return ide_set_dma_mode(drive, rate);
401 static void ide_dump_opcode(ide_drive_t *drive)
404 ide_task_t *task = NULL;
406 spin_lock(&ide_lock);
409 rq = HWGROUP(drive)->rq;
410 spin_unlock(&ide_lock);
414 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
417 printk("ide: failed opcode was: ");
419 printk(KERN_CONT "unknown\n");
421 printk(KERN_CONT "0x%02x\n", task->tf.command);
424 u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
429 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
432 high = tf->device & 0xf;
433 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
435 return ((u64)high << 24) | low;
437 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
439 static void ide_dump_sector(ide_drive_t *drive)
442 struct ide_taskfile *tf = &task.tf;
443 int lba48 = (drive->addressing == 1) ? 1 : 0;
445 memset(&task, 0, sizeof(task));
447 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
450 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
452 drive->hwif->tf_read(drive, &task);
454 if (lba48 || (tf->device & ATA_LBA))
455 printk(", LBAsect=%llu",
456 (unsigned long long)ide_get_lba_addr(tf, lba48));
458 printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
459 tf->device & 0xf, tf->lbal);
462 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
465 if (err & ABRT_ERR) printk("DriveStatusError ");
467 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
468 if (err & ECC_ERR) printk("UncorrectableError ");
469 if (err & ID_ERR) printk("SectorIdNotFound ");
470 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
471 if (err & MARK_ERR) printk("AddrMarkNotFound ");
473 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
474 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
475 ide_dump_sector(drive);
476 if (HWGROUP(drive) && HWGROUP(drive)->rq)
477 printk(", sector=%llu",
478 (unsigned long long)HWGROUP(drive)->rq->sector);
483 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
486 if (err & ILI_ERR) printk("IllegalLengthIndication ");
487 if (err & EOM_ERR) printk("EndOfMedia ");
488 if (err & ABRT_ERR) printk("AbortedCommand ");
489 if (err & MCR_ERR) printk("MediaChangeRequested ");
490 if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
491 (err & LFS_ERR) >> 4);
496 * ide_dump_status - translate ATA/ATAPI error
497 * @drive: drive that status applies to
498 * @msg: text message to print
499 * @stat: status byte to decode
501 * Error reporting, in human readable form (luxurious, but a memory hog).
502 * Combines the drive name, message and status byte to provide a
503 * user understandable explanation of the device error.
506 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
511 local_irq_save(flags);
512 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
513 if (stat & BUSY_STAT)
516 if (stat & READY_STAT) printk("DriveReady ");
517 if (stat & WRERR_STAT) printk("DeviceFault ");
518 if (stat & SEEK_STAT) printk("SeekComplete ");
519 if (stat & DRQ_STAT) printk("DataRequest ");
520 if (stat & ECC_STAT) printk("CorrectedError ");
521 if (stat & INDEX_STAT) printk("Index ");
522 if (stat & ERR_STAT) printk("Error ");
525 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
526 err = ide_read_error(drive);
527 printk("%s: %s: error=0x%02x ", drive->name, msg, err);
528 if (drive->media == ide_disk)
529 ide_dump_ata_error(drive, err);
531 ide_dump_atapi_error(drive, err);
533 ide_dump_opcode(drive);
534 local_irq_restore(flags);
538 EXPORT_SYMBOL(ide_dump_status);