ide: remove drive->driveid
[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  *      ide_get_best_pio_mode   -       get PIO mode from drive
80  *      @drive: drive to consider
81  *      @mode_wanted: preferred mode
82  *      @max_mode: highest allowed mode
83  *
84  *      This routine returns the recommended PIO settings for a given drive,
85  *      based on the drive->id information and the ide_pio_blacklist[].
86  *
87  *      Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
88  *      This is used by most chipset support modules when "auto-tuning".
89  */
90
91 u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
92 {
93         u16 *id = drive->id;
94         int pio_mode = -1, overridden = 0;
95
96         if (mode_wanted != 255)
97                 return min_t(u8, mode_wanted, max_mode);
98
99         if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
100                 pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);
101
102         if (pio_mode != -1) {
103                 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
104         } else {
105                 pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
106                 if (pio_mode > 2) {     /* 2 is maximum allowed tPIO value */
107                         pio_mode = 2;
108                         overridden = 1;
109                 }
110
111                 if (id[ATA_ID_FIELD_VALID] & 2) {             /* ATA2? */
112                         if (ata_id_has_iordy(id)) {
113                                 if (id[ATA_ID_PIO_MODES] & 7) {
114                                         overridden = 0;
115                                         if (id[ATA_ID_PIO_MODES] & 4)
116                                                 pio_mode = 5;
117                                         else if (id[ATA_ID_PIO_MODES] & 2)
118                                                 pio_mode = 4;
119                                         else
120                                                 pio_mode = 3;
121                                 }
122                         }
123                 }
124
125                 if (overridden)
126                         printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
127                                          drive->name);
128         }
129
130         if (pio_mode > max_mode)
131                 pio_mode = max_mode;
132
133         return pio_mode;
134 }
135
136 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
137
138 /* req_pio == "255" for auto-tune */
139 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
140 {
141         ide_hwif_t *hwif = drive->hwif;
142         const struct ide_port_ops *port_ops = hwif->port_ops;
143         u8 host_pio, pio;
144
145         if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
146             (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
147                 return;
148
149         BUG_ON(hwif->pio_mask == 0x00);
150
151         host_pio = fls(hwif->pio_mask) - 1;
152
153         pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
154
155         /*
156          * TODO:
157          * - report device max PIO mode
158          * - check req_pio != 255 against device max PIO mode
159          */
160         printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
161                           drive->name, host_pio, req_pio,
162                           req_pio == 255 ? "(auto-tune)" : "", pio);
163
164         (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
165 }
166
167 EXPORT_SYMBOL_GPL(ide_set_pio);
168
169 /**
170  *      ide_toggle_bounce       -       handle bounce buffering
171  *      @drive: drive to update
172  *      @on: on/off boolean
173  *
174  *      Enable or disable bounce buffering for the device. Drives move
175  *      between PIO and DMA and that changes the rules we need.
176  */
177  
178 void ide_toggle_bounce(ide_drive_t *drive, int on)
179 {
180         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
181
182         if (!PCI_DMA_BUS_IS_PHYS) {
183                 addr = BLK_BOUNCE_ANY;
184         } else if (on && drive->media == ide_disk) {
185                 struct device *dev = drive->hwif->dev;
186
187                 if (dev && dev->dma_mask)
188                         addr = *dev->dma_mask;
189         }
190
191         if (drive->queue)
192                 blk_queue_bounce_limit(drive->queue, addr);
193 }
194
195 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
196 {
197         ide_hwif_t *hwif = drive->hwif;
198         const struct ide_port_ops *port_ops = hwif->port_ops;
199
200         if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
201                 return 0;
202
203         if (port_ops == NULL || port_ops->set_pio_mode == NULL)
204                 return -1;
205
206         /*
207          * TODO: temporary hack for some legacy host drivers that didn't
208          * set transfer mode on the device in ->set_pio_mode method...
209          */
210         if (port_ops->set_dma_mode == NULL) {
211                 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
212                 return 0;
213         }
214
215         if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
216                 if (ide_config_drive_speed(drive, mode))
217                         return -1;
218                 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
219                 return 0;
220         } else {
221                 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
222                 return ide_config_drive_speed(drive, mode);
223         }
224 }
225
226 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
227 {
228         ide_hwif_t *hwif = drive->hwif;
229         const struct ide_port_ops *port_ops = hwif->port_ops;
230
231         if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
232                 return 0;
233
234         if (port_ops == NULL || port_ops->set_dma_mode == NULL)
235                 return -1;
236
237         if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
238                 if (ide_config_drive_speed(drive, mode))
239                         return -1;
240                 port_ops->set_dma_mode(drive, mode);
241                 return 0;
242         } else {
243                 port_ops->set_dma_mode(drive, mode);
244                 return ide_config_drive_speed(drive, mode);
245         }
246 }
247
248 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
249
250 /**
251  *      ide_set_xfer_rate       -       set transfer rate
252  *      @drive: drive to set
253  *      @rate: speed to attempt to set
254  *      
255  *      General helper for setting the speed of an IDE device. This
256  *      function knows about user enforced limits from the configuration
257  *      which ->set_pio_mode/->set_dma_mode does not.
258  */
259
260 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
261 {
262         ide_hwif_t *hwif = drive->hwif;
263         const struct ide_port_ops *port_ops = hwif->port_ops;
264
265         if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
266             (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
267                 return -1;
268
269         rate = ide_rate_filter(drive, rate);
270
271         BUG_ON(rate < XFER_PIO_0);
272
273         if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
274                 return ide_set_pio_mode(drive, rate);
275
276         return ide_set_dma_mode(drive, rate);
277 }
278
279 static void ide_dump_opcode(ide_drive_t *drive)
280 {
281         struct request *rq;
282         ide_task_t *task = NULL;
283
284         spin_lock(&ide_lock);
285         rq = NULL;
286         if (HWGROUP(drive))
287                 rq = HWGROUP(drive)->rq;
288         spin_unlock(&ide_lock);
289         if (!rq)
290                 return;
291
292         if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
293                 task = rq->special;
294
295         printk("ide: failed opcode was: ");
296         if (task == NULL)
297                 printk(KERN_CONT "unknown\n");
298         else
299                 printk(KERN_CONT "0x%02x\n", task->tf.command);
300 }
301
302 u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
303 {
304         u32 high, low;
305
306         if (lba48)
307                 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
308                         tf->hob_lbal;
309         else
310                 high = tf->device & 0xf;
311         low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
312
313         return ((u64)high << 24) | low;
314 }
315 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
316
317 static void ide_dump_sector(ide_drive_t *drive)
318 {
319         ide_task_t task;
320         struct ide_taskfile *tf = &task.tf;
321         int lba48 = (drive->addressing == 1) ? 1 : 0;
322
323         memset(&task, 0, sizeof(task));
324         if (lba48)
325                 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
326                                 IDE_TFLAG_LBA48;
327         else
328                 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
329
330         drive->hwif->tp_ops->tf_read(drive, &task);
331
332         if (lba48 || (tf->device & ATA_LBA))
333                 printk(", LBAsect=%llu",
334                         (unsigned long long)ide_get_lba_addr(tf, lba48));
335         else
336                 printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
337                                          tf->device & 0xf, tf->lbal);
338 }
339
340 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
341 {
342         printk("{ ");
343         if (err & ABRT_ERR)     printk("DriveStatusError ");
344         if (err & ICRC_ERR)
345                 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
346         if (err & ECC_ERR)      printk("UncorrectableError ");
347         if (err & ID_ERR)       printk("SectorIdNotFound ");
348         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
349         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
350         printk("}");
351         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
352             (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
353                 ide_dump_sector(drive);
354                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
355                         printk(", sector=%llu",
356                                (unsigned long long)HWGROUP(drive)->rq->sector);
357         }
358         printk("\n");
359 }
360
361 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
362 {
363         printk("{ ");
364         if (err & ILI_ERR)      printk("IllegalLengthIndication ");
365         if (err & EOM_ERR)      printk("EndOfMedia ");
366         if (err & ABRT_ERR)     printk("AbortedCommand ");
367         if (err & MCR_ERR)      printk("MediaChangeRequested ");
368         if (err & LFS_ERR)      printk("LastFailedSense=0x%02x ",
369                                        (err & LFS_ERR) >> 4);
370         printk("}\n");
371 }
372
373 /**
374  *      ide_dump_status         -       translate ATA/ATAPI error
375  *      @drive: drive that status applies to
376  *      @msg: text message to print
377  *      @stat: status byte to decode
378  *
379  *      Error reporting, in human readable form (luxurious, but a memory hog).
380  *      Combines the drive name, message and status byte to provide a
381  *      user understandable explanation of the device error.
382  */
383
384 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
385 {
386         unsigned long flags;
387         u8 err = 0;
388
389         local_irq_save(flags);
390         printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
391         if (stat & BUSY_STAT)
392                 printk("Busy ");
393         else {
394                 if (stat & READY_STAT)  printk("DriveReady ");
395                 if (stat & WRERR_STAT)  printk("DeviceFault ");
396                 if (stat & SEEK_STAT)   printk("SeekComplete ");
397                 if (stat & DRQ_STAT)    printk("DataRequest ");
398                 if (stat & ECC_STAT)    printk("CorrectedError ");
399                 if (stat & INDEX_STAT)  printk("Index ");
400                 if (stat & ERR_STAT)    printk("Error ");
401         }
402         printk("}\n");
403         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
404                 err = ide_read_error(drive);
405                 printk("%s: %s: error=0x%02x ", drive->name, msg, err);
406                 if (drive->media == ide_disk)
407                         ide_dump_ata_error(drive, err);
408                 else
409                         ide_dump_atapi_error(drive, err);
410         }
411         ide_dump_opcode(drive);
412         local_irq_restore(flags);
413         return err;
414 }
415
416 EXPORT_SYMBOL(ide_dump_status);