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