blob: e386a32dc9ba888c110b42971d57a1bab2662f49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/string.h>
3#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/ide.h>
6#include <linux/bitops.h>
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/**
9 * ide_toggle_bounce - handle bounce buffering
10 * @drive: drive to update
11 * @on: on/off boolean
12 *
13 * Enable or disable bounce buffering for the device. Drives move
14 * between PIO and DMA and that changes the rules we need.
15 */
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017void ide_toggle_bounce(ide_drive_t *drive, int on)
18{
19 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
20
James Bottomley65931782005-11-18 23:13:33 +010021 if (!PCI_DMA_BUS_IS_PHYS) {
22 addr = BLK_BOUNCE_ANY;
23 } else if (on && drive->media == ide_disk) {
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +010024 struct device *dev = drive->hwif->dev;
25
26 if (dev && dev->dma_mask)
27 addr = *dev->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 }
29
30 if (drive->queue)
31 blk_queue_bounce_limit(drive->queue, addr);
32}
33
Sergei Shtylyov745483f2009-04-08 14:13:02 +020034u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010035{
Sergei Shtylyov745483f2009-04-08 14:13:02 +020036 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010037 u32 high, low;
38
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010039 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
Sergei Shtylyov745483f2009-04-08 14:13:02 +020040 if (lba48) {
41 tf = &cmd->hob;
42 high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
43 } else
44 high = tf->device & 0xf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010045
46 return ((u64)high << 24) | low;
47}
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +010048EXPORT_SYMBOL_GPL(ide_get_lba_addr);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010049
50static void ide_dump_sector(ide_drive_t *drive)
51{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010052 struct ide_cmd cmd;
53 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020054 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010055
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010056 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov60f85012009-04-08 14:13:01 +020057 if (lba48) {
58 cmd.valid.in.tf = IDE_VALID_LBA;
59 cmd.valid.in.hob = IDE_VALID_LBA;
60 cmd.tf_flags = IDE_TFLAG_LBA48;
61 } else
62 cmd.valid.in.tf = IDE_VALID_LBA | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010063
Sergei Shtylyov3153c262009-04-08 14:13:03 +020064 ide_tf_readback(drive, &cmd);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010065
66 if (lba48 || (tf->device & ATA_LBA))
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010067 printk(KERN_CONT ", LBAsect=%llu",
Sergei Shtylyov745483f2009-04-08 14:13:02 +020068 (unsigned long long)ide_get_lba_addr(&cmd, lba48));
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010069 else
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010070 printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
71 tf->device & 0xf, tf->lbal);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010072}
73
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010074static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Bartlomiej Zolnierkiewicz26bfcf22009-05-22 16:23:37 +020076 printk(KERN_CONT "{ ");
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010077 if (err & ATA_ABORTED)
78 printk(KERN_CONT "DriveStatusError ");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020079 if (err & ATA_ICRC)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010080 printk(KERN_CONT "%s",
81 (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
82 if (err & ATA_UNC)
83 printk(KERN_CONT "UncorrectableError ");
84 if (err & ATA_IDNF)
85 printk(KERN_CONT "SectorIdNotFound ");
86 if (err & ATA_TRK0NF)
87 printk(KERN_CONT "TrackZeroNotFound ");
88 if (err & ATA_AMNF)
89 printk(KERN_CONT "AddrMarkNotFound ");
90 printk(KERN_CONT "}");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020091 if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
92 (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010093 struct request *rq = drive->hwif->rq;
94
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010095 ide_dump_sector(drive);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010096
97 if (rq)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010098 printk(KERN_CONT ", sector=%llu",
Tejun Heo9780e2d2009-05-07 22:24:40 +090099 (unsigned long long)blk_rq_pos(rq));
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100100 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100101 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100104static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
105{
Bartlomiej Zolnierkiewicz26bfcf22009-05-22 16:23:37 +0200106 printk(KERN_CONT "{ ");
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100107 if (err & ATAPI_ILI)
108 printk(KERN_CONT "IllegalLengthIndication ");
109 if (err & ATAPI_EOM)
110 printk(KERN_CONT "EndOfMedia ");
111 if (err & ATA_ABORTED)
112 printk(KERN_CONT "AbortedCommand ");
113 if (err & ATA_MCR)
114 printk(KERN_CONT "MediaChangeRequested ");
115 if (err & ATAPI_LFS)
116 printk(KERN_CONT "LastFailedSense=0x%02x ",
117 (err & ATAPI_LFS) >> 4);
118 printk(KERN_CONT "}\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/**
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100122 * ide_dump_status - translate ATA/ATAPI error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * @drive: drive that status applies to
124 * @msg: text message to print
125 * @stat: status byte to decode
126 *
127 * Error reporting, in human readable form (luxurious, but a memory hog).
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100128 * Combines the drive name, message and status byte to provide a
129 * user understandable explanation of the device error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100132u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100134 u8 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100136 printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200137 if (stat & ATA_BUSY)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100138 printk(KERN_CONT "Busy ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else {
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100140 if (stat & ATA_DRDY)
141 printk(KERN_CONT "DriveReady ");
142 if (stat & ATA_DF)
143 printk(KERN_CONT "DeviceFault ");
144 if (stat & ATA_DSC)
145 printk(KERN_CONT "SeekComplete ");
146 if (stat & ATA_DRQ)
147 printk(KERN_CONT "DataRequest ");
148 if (stat & ATA_CORR)
149 printk(KERN_CONT "CorrectedError ");
150 if (stat & ATA_IDX)
151 printk(KERN_CONT "Index ");
152 if (stat & ATA_ERR)
153 printk(KERN_CONT "Error ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100155 printk(KERN_CONT "}\n");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200156 if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100157 err = ide_read_error(drive);
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100158 printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100159 if (drive->media == ide_disk)
160 ide_dump_ata_error(drive, err);
161 else
162 ide_dump_atapi_error(drive, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Bartlomiej Zolnierkiewiczcc301372009-05-22 16:23:38 +0200164
165 printk(KERN_ERR "%s: possibly failed opcode: 0x%02x\n",
166 drive->name, drive->hwif->cmd.tf.command);
167
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170EXPORT_SYMBOL(ide_dump_status);