Merge branch 'linux-2.6'
[sfrench/cifs-2.6.git] / drivers / ide / ide-lib.c
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 static const char *udma_str[] =
25          { "UDMA/16", "UDMA/25",  "UDMA/33",  "UDMA/44",
26            "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
27 static const char *mwdma_str[] =
28         { "MWDMA0", "MWDMA1", "MWDMA2" };
29 static const char *swdma_str[] =
30         { "SWDMA0", "SWDMA1", "SWDMA2" };
31 static const char *pio_str[] =
32         { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
33
34 /**
35  *      ide_xfer_verbose        -       return IDE mode names
36  *      @mode: transfer mode
37  *
38  *      Returns a constant string giving the name of the mode
39  *      requested.
40  */
41
42 const char *ide_xfer_verbose(u8 mode)
43 {
44         const char *s;
45         u8 i = mode & 0xf;
46
47         if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
48                 s = udma_str[i];
49         else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
50                 s = mwdma_str[i];
51         else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
52                 s = swdma_str[i];
53         else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
54                 s = pio_str[i & 0x7];
55         else if (mode == XFER_PIO_SLOW)
56                 s = "PIO SLOW";
57         else
58                 s = "XFER ERROR";
59
60         return s;
61 }
62
63 EXPORT_SYMBOL(ide_xfer_verbose);
64
65 /**
66  *      ide_rate_filter         -       filter transfer mode
67  *      @drive: IDE device
68  *      @speed: desired speed
69  *
70  *      Given the available transfer modes this function returns
71  *      the best available speed at or below the speed requested.
72  *
73  *      TODO: check device PIO capabilities
74  */
75
76 static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
77 {
78         ide_hwif_t *hwif = drive->hwif;
79         u8 mode = ide_find_dma_mode(drive, speed);
80
81         if (mode == 0) {
82                 if (hwif->pio_mask)
83                         mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
84                 else
85                         mode = XFER_PIO_4;
86         }
87
88 //      printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed);
89
90         return min(speed, mode);
91 }
92
93 /*
94  * Standard (generic) timings for PIO modes, from ATA2 specification.
95  * These timings are for access to the IDE data port register *only*.
96  * Some drives may specify a mode, while also specifying a different
97  * value for cycle_time (from drive identification data).
98  */
99 const ide_pio_timings_t ide_pio_timings[6] = {
100         { 70,   165,    600 },  /* PIO Mode 0 */
101         { 50,   125,    383 },  /* PIO Mode 1 */
102         { 30,   100,    240 },  /* PIO Mode 2 */
103         { 30,   80,     180 },  /* PIO Mode 3 with IORDY */
104         { 25,   70,     120 },  /* PIO Mode 4 with IORDY */
105         { 20,   50,     100 }   /* PIO Mode 5 with IORDY (nonstandard) */
106 };
107
108 EXPORT_SYMBOL_GPL(ide_pio_timings);
109
110 /*
111  * Shared data/functions for determining best PIO mode for an IDE drive.
112  * Most of this stuff originally lived in cmd640.c, and changes to the
113  * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
114  * breaking the fragile cmd640.c support.
115  */
116
117 /*
118  * Black list. Some drives incorrectly report their maximal PIO mode,
119  * at least in respect to CMD640. Here we keep info on some known drives.
120  */
121 static struct ide_pio_info {
122         const char      *name;
123         int             pio;
124 } ide_pio_blacklist [] = {
125         { "Conner Peripherals 540MB - CFS540A", 3 },
126
127         { "WDC AC2700",  3 },
128         { "WDC AC2540",  3 },
129         { "WDC AC2420",  3 },
130         { "WDC AC2340",  3 },
131         { "WDC AC2250",  0 },
132         { "WDC AC2200",  0 },
133         { "WDC AC21200", 4 },
134         { "WDC AC2120",  0 },
135         { "WDC AC2850",  3 },
136         { "WDC AC1270",  3 },
137         { "WDC AC1170",  1 },
138         { "WDC AC1210",  1 },
139         { "WDC AC280",   0 },
140         { "WDC AC31000", 3 },
141         { "WDC AC31200", 3 },
142
143         { "Maxtor 7131 AT", 1 },
144         { "Maxtor 7171 AT", 1 },
145         { "Maxtor 7213 AT", 1 },
146         { "Maxtor 7245 AT", 1 },
147         { "Maxtor 7345 AT", 1 },
148         { "Maxtor 7546 AT", 3 },
149         { "Maxtor 7540 AV", 3 },
150
151         { "SAMSUNG SHD-3121A", 1 },
152         { "SAMSUNG SHD-3122A", 1 },
153         { "SAMSUNG SHD-3172A", 1 },
154
155         { "ST5660A",  3 },
156         { "ST3660A",  3 },
157         { "ST3630A",  3 },
158         { "ST3655A",  3 },
159         { "ST3391A",  3 },
160         { "ST3390A",  1 },
161         { "ST3600A",  1 },
162         { "ST3290A",  0 },
163         { "ST3144A",  0 },
164         { "ST3491A",  1 },      /* reports 3, should be 1 or 2 (depending on */ 
165                                 /* drive) according to Seagates FIND-ATA program */
166
167         { "QUANTUM ELS127A", 0 },
168         { "QUANTUM ELS170A", 0 },
169         { "QUANTUM LPS240A", 0 },
170         { "QUANTUM LPS210A", 3 },
171         { "QUANTUM LPS270A", 3 },
172         { "QUANTUM LPS365A", 3 },
173         { "QUANTUM LPS540A", 3 },
174         { "QUANTUM LIGHTNING 540A", 3 },
175         { "QUANTUM LIGHTNING 730A", 3 },
176
177         { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
178         { "QUANTUM FIREBALL_640", 3 }, 
179         { "QUANTUM FIREBALL_1080", 3 },
180         { "QUANTUM FIREBALL_1280", 3 },
181         { NULL, 0 }
182 };
183
184 /**
185  *      ide_scan_pio_blacklist  -       check for a blacklisted drive
186  *      @model: Drive model string
187  *
188  *      This routine searches the ide_pio_blacklist for an entry
189  *      matching the start/whole of the supplied model name.
190  *
191  *      Returns -1 if no match found.
192  *      Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
193  */
194
195 static int ide_scan_pio_blacklist (char *model)
196 {
197         struct ide_pio_info *p;
198
199         for (p = ide_pio_blacklist; p->name != NULL; p++) {
200                 if (strncmp(p->name, model, strlen(p->name)) == 0)
201                         return p->pio;
202         }
203         return -1;
204 }
205
206 unsigned int ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
207 {
208         struct hd_driveid *id = drive->id;
209         int cycle_time = 0;
210
211         if (id->field_valid & 2) {
212                 if (id->capability & 8)
213                         cycle_time = id->eide_pio_iordy;
214                 else
215                         cycle_time = id->eide_pio;
216         }
217
218         /* conservative "downgrade" for all pre-ATA2 drives */
219         if (pio < 3) {
220                 if (cycle_time && cycle_time < ide_pio_timings[pio].cycle_time)
221                         cycle_time = 0; /* use standard timing */
222         }
223
224         return cycle_time ? cycle_time : ide_pio_timings[pio].cycle_time;
225 }
226
227 EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
228
229 /**
230  *      ide_get_best_pio_mode   -       get PIO mode from drive
231  *      @drive: drive to consider
232  *      @mode_wanted: preferred mode
233  *      @max_mode: highest allowed mode
234  *
235  *      This routine returns the recommended PIO settings for a given drive,
236  *      based on the drive->id information and the ide_pio_blacklist[].
237  *
238  *      Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
239  *      This is used by most chipset support modules when "auto-tuning".
240  */
241
242 u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
243 {
244         int pio_mode;
245         struct hd_driveid* id = drive->id;
246         int overridden  = 0;
247
248         if (mode_wanted != 255)
249                 return min_t(u8, mode_wanted, max_mode);
250
251         if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
252             (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
253                 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
254         } else {
255                 pio_mode = id->tPIO;
256                 if (pio_mode > 2) {     /* 2 is maximum allowed tPIO value */
257                         pio_mode = 2;
258                         overridden = 1;
259                 }
260                 if (id->field_valid & 2) {        /* drive implements ATA2? */
261                         if (id->capability & 8) { /* IORDY supported? */
262                                 if (id->eide_pio_modes & 7) {
263                                         overridden = 0;
264                                         if (id->eide_pio_modes & 4)
265                                                 pio_mode = 5;
266                                         else if (id->eide_pio_modes & 2)
267                                                 pio_mode = 4;
268                                         else
269                                                 pio_mode = 3;
270                                 }
271                         }
272                 }
273
274                 if (overridden)
275                         printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
276                                          drive->name);
277
278                 /*
279                  * Conservative "downgrade" for all pre-ATA2 drives
280                  */
281                 if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_DOWNGRADE) == 0 &&
282                     pio_mode && pio_mode < 4) {
283                         pio_mode--;
284                         printk(KERN_INFO "%s: applying conservative "
285                                          "PIO \"downgrade\"\n", drive->name);
286                 }
287         }
288
289         if (pio_mode > max_mode)
290                 pio_mode = max_mode;
291
292         return pio_mode;
293 }
294
295 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
296
297 /* req_pio == "255" for auto-tune */
298 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
299 {
300         ide_hwif_t *hwif = drive->hwif;
301         u8 host_pio, pio;
302
303         if (hwif->set_pio_mode == NULL)
304                 return;
305
306         BUG_ON(hwif->pio_mask == 0x00);
307
308         host_pio = fls(hwif->pio_mask) - 1;
309
310         pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
311
312         /*
313          * TODO:
314          * - report device max PIO mode
315          * - check req_pio != 255 against device max PIO mode
316          */
317         printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
318                           drive->name, host_pio, req_pio,
319                           req_pio == 255 ? "(auto-tune)" : "", pio);
320
321         (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
322 }
323
324 EXPORT_SYMBOL_GPL(ide_set_pio);
325
326 /**
327  *      ide_toggle_bounce       -       handle bounce buffering
328  *      @drive: drive to update
329  *      @on: on/off boolean
330  *
331  *      Enable or disable bounce buffering for the device. Drives move
332  *      between PIO and DMA and that changes the rules we need.
333  */
334  
335 void ide_toggle_bounce(ide_drive_t *drive, int on)
336 {
337         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
338
339         if (!PCI_DMA_BUS_IS_PHYS) {
340                 addr = BLK_BOUNCE_ANY;
341         } else if (on && drive->media == ide_disk) {
342                 struct device *dev = drive->hwif->dev;
343
344                 if (dev && dev->dma_mask)
345                         addr = *dev->dma_mask;
346         }
347
348         if (drive->queue)
349                 blk_queue_bounce_limit(drive->queue, addr);
350 }
351
352 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
353 {
354         ide_hwif_t *hwif = drive->hwif;
355
356         if (hwif->set_pio_mode == NULL)
357                 return -1;
358
359         /*
360          * TODO: temporary hack for some legacy host drivers that didn't
361          * set transfer mode on the device in ->set_pio_mode method...
362          */
363         if (hwif->set_dma_mode == NULL) {
364                 hwif->set_pio_mode(drive, mode - XFER_PIO_0);
365                 return 0;
366         }
367
368         if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
369                 if (ide_config_drive_speed(drive, mode))
370                         return -1;
371                 hwif->set_pio_mode(drive, mode - XFER_PIO_0);
372                 return 0;
373         } else {
374                 hwif->set_pio_mode(drive, mode - XFER_PIO_0);
375                 return ide_config_drive_speed(drive, mode);
376         }
377 }
378
379 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
380 {
381         ide_hwif_t *hwif = drive->hwif;
382
383         if (hwif->set_dma_mode == NULL)
384                 return -1;
385
386         if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
387                 if (ide_config_drive_speed(drive, mode))
388                         return -1;
389                 hwif->set_dma_mode(drive, mode);
390                 return 0;
391         } else {
392                 hwif->set_dma_mode(drive, mode);
393                 return ide_config_drive_speed(drive, mode);
394         }
395 }
396
397 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
398
399 /**
400  *      ide_set_xfer_rate       -       set transfer rate
401  *      @drive: drive to set
402  *      @rate: speed to attempt to set
403  *      
404  *      General helper for setting the speed of an IDE device. This
405  *      function knows about user enforced limits from the configuration
406  *      which ->set_pio_mode/->set_dma_mode does not.
407  */
408
409 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
410 {
411         ide_hwif_t *hwif = drive->hwif;
412
413         if (hwif->set_dma_mode == NULL)
414                 return -1;
415
416         rate = ide_rate_filter(drive, rate);
417
418         if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
419                 return ide_set_pio_mode(drive, rate);
420
421         /*
422          * TODO: transfer modes 0x00-0x07 passed from the user-space are
423          * currently handled here which needs fixing (please note that such
424          * case could happen iff the transfer mode has already been set on
425          * the device by ide-proc.c::set_xfer_rate()).
426          */
427         if (rate < XFER_PIO_0) {
428                 if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
429                         return ide_set_dma_mode(drive, rate);
430                 else
431                         return ide_config_drive_speed(drive, rate);
432         }
433
434         return ide_set_dma_mode(drive, rate);
435 }
436
437 static void ide_dump_opcode(ide_drive_t *drive)
438 {
439         struct request *rq;
440         ide_task_t *task = NULL;
441
442         spin_lock(&ide_lock);
443         rq = NULL;
444         if (HWGROUP(drive))
445                 rq = HWGROUP(drive)->rq;
446         spin_unlock(&ide_lock);
447         if (!rq)
448                 return;
449
450         if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
451                 task = rq->special;
452
453         printk("ide: failed opcode was: ");
454         if (task == NULL)
455                 printk(KERN_CONT "unknown\n");
456         else
457                 printk(KERN_CONT "0x%02x\n", task->tf.command);
458 }
459
460 u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
461 {
462         u32 high, low;
463
464         if (lba48)
465                 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
466                         tf->hob_lbal;
467         else
468                 high = tf->device & 0xf;
469         low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
470
471         return ((u64)high << 24) | low;
472 }
473 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
474
475 static void ide_dump_sector(ide_drive_t *drive)
476 {
477         ide_task_t task;
478         struct ide_taskfile *tf = &task.tf;
479         int lba48 = (drive->addressing == 1) ? 1 : 0;
480
481         memset(&task, 0, sizeof(task));
482         if (lba48)
483                 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
484                                 IDE_TFLAG_LBA48;
485         else
486                 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
487
488         ide_tf_read(drive, &task);
489
490         if (lba48 || (tf->device & ATA_LBA))
491                 printk(", LBAsect=%llu",
492                         (unsigned long long)ide_get_lba_addr(tf, lba48));
493         else
494                 printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
495                                          tf->device & 0xf, tf->lbal);
496 }
497
498 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
499 {
500         printk("{ ");
501         if (err & ABRT_ERR)     printk("DriveStatusError ");
502         if (err & ICRC_ERR)
503                 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
504         if (err & ECC_ERR)      printk("UncorrectableError ");
505         if (err & ID_ERR)       printk("SectorIdNotFound ");
506         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
507         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
508         printk("}");
509         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
510             (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
511                 ide_dump_sector(drive);
512                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
513                         printk(", sector=%llu",
514                                (unsigned long long)HWGROUP(drive)->rq->sector);
515         }
516         printk("\n");
517 }
518
519 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
520 {
521         printk("{ ");
522         if (err & ILI_ERR)      printk("IllegalLengthIndication ");
523         if (err & EOM_ERR)      printk("EndOfMedia ");
524         if (err & ABRT_ERR)     printk("AbortedCommand ");
525         if (err & MCR_ERR)      printk("MediaChangeRequested ");
526         if (err & LFS_ERR)      printk("LastFailedSense=0x%02x ",
527                                        (err & LFS_ERR) >> 4);
528         printk("}\n");
529 }
530
531 /**
532  *      ide_dump_status         -       translate ATA/ATAPI error
533  *      @drive: drive that status applies to
534  *      @msg: text message to print
535  *      @stat: status byte to decode
536  *
537  *      Error reporting, in human readable form (luxurious, but a memory hog).
538  *      Combines the drive name, message and status byte to provide a
539  *      user understandable explanation of the device error.
540  */
541
542 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
543 {
544         unsigned long flags;
545         u8 err = 0;
546
547         local_irq_save(flags);
548         printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
549         if (stat & BUSY_STAT)
550                 printk("Busy ");
551         else {
552                 if (stat & READY_STAT)  printk("DriveReady ");
553                 if (stat & WRERR_STAT)  printk("DeviceFault ");
554                 if (stat & SEEK_STAT)   printk("SeekComplete ");
555                 if (stat & DRQ_STAT)    printk("DataRequest ");
556                 if (stat & ECC_STAT)    printk("CorrectedError ");
557                 if (stat & INDEX_STAT)  printk("Index ");
558                 if (stat & ERR_STAT)    printk("Error ");
559         }
560         printk("}\n");
561         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
562                 err = ide_read_error(drive);
563                 printk("%s: %s: error=0x%02x ", drive->name, msg, err);
564                 if (drive->media == ide_disk)
565                         ide_dump_ata_error(drive, err);
566                 else
567                         ide_dump_atapi_error(drive, err);
568         }
569         ide_dump_opcode(drive);
570         local_irq_restore(flags);
571         return err;
572 }
573
574 EXPORT_SYMBOL(ide_dump_status);