Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / ide / ide-lib.c
1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/export.h>
5 #include <linux/interrupt.h>
6 #include <linux/ide.h>
7 #include <linux/bitops.h>
8
9 /**
10  *      ide_toggle_bounce       -       handle bounce buffering
11  *      @drive: drive to update
12  *      @on: on/off boolean
13  *
14  *      Enable or disable bounce buffering for the device. Drives move
15  *      between PIO and DMA and that changes the rules we need.
16  */
17
18 void ide_toggle_bounce(ide_drive_t *drive, int on)
19 {
20         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
21
22 #ifndef CONFIG_XEN
23         if (!PCI_DMA_BUS_IS_PHYS) {
24                 addr = BLK_BOUNCE_ANY;
25         } else if (on && drive->media == ide_disk) {
26                 struct device *dev = drive->hwif->dev;
27
28                 if (dev && dev->dma_mask)
29                         addr = *dev->dma_mask;
30         }
31 #else
32         if (on && drive->media == ide_disk) {
33                 struct device *dev = drive->hwif->dev;
34
35                 if (!PCI_DMA_BUS_IS_PHYS)
36                         addr = BLK_BOUNCE_ANY;
37                 else if (dev && dev->dma_mask)
38                         addr = *dev->dma_mask;
39         }
40 #endif
41
42         if (drive->queue)
43                 blk_queue_bounce_limit(drive->queue, addr);
44 }
45
46 u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
47 {
48         struct ide_taskfile *tf = &cmd->tf;
49         u32 high, low;
50
51         low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
52         if (lba48) {
53                 tf = &cmd->hob;
54                 high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
55         } else
56                 high = tf->device & 0xf;
57
58         return ((u64)high << 24) | low;
59 }
60 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
61
62 static void ide_dump_sector(ide_drive_t *drive)
63 {
64         struct ide_cmd cmd;
65         struct ide_taskfile *tf = &cmd.tf;
66         u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
67
68         memset(&cmd, 0, sizeof(cmd));
69         if (lba48) {
70                 cmd.valid.in.tf  = IDE_VALID_LBA;
71                 cmd.valid.in.hob = IDE_VALID_LBA;
72                 cmd.tf_flags = IDE_TFLAG_LBA48;
73         } else
74                 cmd.valid.in.tf  = IDE_VALID_LBA | IDE_VALID_DEVICE;
75
76         ide_tf_readback(drive, &cmd);
77
78         if (lba48 || (tf->device & ATA_LBA))
79                 printk(KERN_CONT ", LBAsect=%llu",
80                         (unsigned long long)ide_get_lba_addr(&cmd, lba48));
81         else
82                 printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
83                         tf->device & 0xf, tf->lbal);
84 }
85
86 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
87 {
88         printk(KERN_CONT "{ ");
89         if (err & ATA_ABORTED)
90                 printk(KERN_CONT "DriveStatusError ");
91         if (err & ATA_ICRC)
92                 printk(KERN_CONT "%s",
93                         (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
94         if (err & ATA_UNC)
95                 printk(KERN_CONT "UncorrectableError ");
96         if (err & ATA_IDNF)
97                 printk(KERN_CONT "SectorIdNotFound ");
98         if (err & ATA_TRK0NF)
99                 printk(KERN_CONT "TrackZeroNotFound ");
100         if (err & ATA_AMNF)
101                 printk(KERN_CONT "AddrMarkNotFound ");
102         printk(KERN_CONT "}");
103         if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
104             (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
105                 struct request *rq = drive->hwif->rq;
106
107                 ide_dump_sector(drive);
108
109                 if (rq)
110                         printk(KERN_CONT ", sector=%llu",
111                                (unsigned long long)blk_rq_pos(rq));
112         }
113         printk(KERN_CONT "\n");
114 }
115
116 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
117 {
118         printk(KERN_CONT "{ ");
119         if (err & ATAPI_ILI)
120                 printk(KERN_CONT "IllegalLengthIndication ");
121         if (err & ATAPI_EOM)
122                 printk(KERN_CONT "EndOfMedia ");
123         if (err & ATA_ABORTED)
124                 printk(KERN_CONT "AbortedCommand ");
125         if (err & ATA_MCR)
126                 printk(KERN_CONT "MediaChangeRequested ");
127         if (err & ATAPI_LFS)
128                 printk(KERN_CONT "LastFailedSense=0x%02x ",
129                         (err & ATAPI_LFS) >> 4);
130         printk(KERN_CONT "}\n");
131 }
132
133 /**
134  *      ide_dump_status         -       translate ATA/ATAPI error
135  *      @drive: drive that status applies to
136  *      @msg: text message to print
137  *      @stat: status byte to decode
138  *
139  *      Error reporting, in human readable form (luxurious, but a memory hog).
140  *      Combines the drive name, message and status byte to provide a
141  *      user understandable explanation of the device error.
142  */
143
144 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
145 {
146         u8 err = 0;
147
148         printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
149         if (stat & ATA_BUSY)
150                 printk(KERN_CONT "Busy ");
151         else {
152                 if (stat & ATA_DRDY)
153                         printk(KERN_CONT "DriveReady ");
154                 if (stat & ATA_DF)
155                         printk(KERN_CONT "DeviceFault ");
156                 if (stat & ATA_DSC)
157                         printk(KERN_CONT "SeekComplete ");
158                 if (stat & ATA_DRQ)
159                         printk(KERN_CONT "DataRequest ");
160                 if (stat & ATA_CORR)
161                         printk(KERN_CONT "CorrectedError ");
162                 if (stat & ATA_IDX)
163                         printk(KERN_CONT "Index ");
164                 if (stat & ATA_ERR)
165                         printk(KERN_CONT "Error ");
166         }
167         printk(KERN_CONT "}\n");
168         if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
169                 err = ide_read_error(drive);
170                 printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
171                 if (drive->media == ide_disk)
172                         ide_dump_ata_error(drive, err);
173                 else
174                         ide_dump_atapi_error(drive, err);
175         }
176
177         printk(KERN_ERR "%s: possibly failed opcode: 0x%02x\n",
178                 drive->name, drive->hwif->cmd.tf.command);
179
180         return err;
181 }
182 EXPORT_SYMBOL(ide_dump_status);