blob: 56ff8c46c7d10a4cf0eefa27bd69769ac52c7953 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void ide_dump_opcode(ide_drive_t *drive)
35{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010036 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010037 struct ide_cmd *cmd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!rq)
40 return;
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +010041
42 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010043 cmd = rq->special;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010045 printk(KERN_ERR "ide: failed opcode was: ");
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010046 if (cmd == NULL)
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +010047 printk(KERN_CONT "unknown\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 else
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010049 printk(KERN_CONT "0x%02x\n", cmd->tf.command);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Sergei Shtylyov745483f2009-04-08 14:13:02 +020052u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010053{
Sergei Shtylyov745483f2009-04-08 14:13:02 +020054 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010055 u32 high, low;
56
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010057 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
Sergei Shtylyov745483f2009-04-08 14:13:02 +020058 if (lba48) {
59 tf = &cmd->hob;
60 high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
61 } else
62 high = tf->device & 0xf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010063
64 return ((u64)high << 24) | low;
65}
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +010066EXPORT_SYMBOL_GPL(ide_get_lba_addr);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010067
68static void ide_dump_sector(ide_drive_t *drive)
69{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010070 struct ide_cmd cmd;
71 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020072 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010073
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010074 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov60f85012009-04-08 14:13:01 +020075 if (lba48) {
76 cmd.valid.in.tf = IDE_VALID_LBA;
77 cmd.valid.in.hob = IDE_VALID_LBA;
78 cmd.tf_flags = IDE_TFLAG_LBA48;
79 } else
80 cmd.valid.in.tf = IDE_VALID_LBA | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010081
Sergei Shtylyov3153c262009-04-08 14:13:03 +020082 ide_tf_readback(drive, &cmd);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010083
84 if (lba48 || (tf->device & ATA_LBA))
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010085 printk(KERN_CONT ", LBAsect=%llu",
Sergei Shtylyov745483f2009-04-08 14:13:02 +020086 (unsigned long long)ide_get_lba_addr(&cmd, lba48));
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010087 else
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010088 printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
89 tf->device & 0xf, tf->lbal);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010090}
91
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010092static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010094 printk(KERN_ERR "{ ");
95 if (err & ATA_ABORTED)
96 printk(KERN_CONT "DriveStatusError ");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020097 if (err & ATA_ICRC)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010098 printk(KERN_CONT "%s",
99 (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
100 if (err & ATA_UNC)
101 printk(KERN_CONT "UncorrectableError ");
102 if (err & ATA_IDNF)
103 printk(KERN_CONT "SectorIdNotFound ");
104 if (err & ATA_TRK0NF)
105 printk(KERN_CONT "TrackZeroNotFound ");
106 if (err & ATA_AMNF)
107 printk(KERN_CONT "AddrMarkNotFound ");
108 printk(KERN_CONT "}");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200109 if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
110 (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100111 struct request *rq = drive->hwif->rq;
112
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100113 ide_dump_sector(drive);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100114
115 if (rq)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100116 printk(KERN_CONT ", sector=%llu",
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100117 (unsigned long long)rq->sector);
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100118 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100119 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100122static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
123{
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100124 printk(KERN_ERR "{ ");
125 if (err & ATAPI_ILI)
126 printk(KERN_CONT "IllegalLengthIndication ");
127 if (err & ATAPI_EOM)
128 printk(KERN_CONT "EndOfMedia ");
129 if (err & ATA_ABORTED)
130 printk(KERN_CONT "AbortedCommand ");
131 if (err & ATA_MCR)
132 printk(KERN_CONT "MediaChangeRequested ");
133 if (err & ATAPI_LFS)
134 printk(KERN_CONT "LastFailedSense=0x%02x ",
135 (err & ATAPI_LFS) >> 4);
136 printk(KERN_CONT "}\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/**
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100140 * ide_dump_status - translate ATA/ATAPI error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * @drive: drive that status applies to
142 * @msg: text message to print
143 * @stat: status byte to decode
144 *
145 * Error reporting, in human readable form (luxurious, but a memory hog).
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100146 * Combines the drive name, message and status byte to provide a
147 * user understandable explanation of the device error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
149
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100150u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100152 u8 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100154 printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200155 if (stat & ATA_BUSY)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100156 printk(KERN_CONT "Busy ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 else {
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100158 if (stat & ATA_DRDY)
159 printk(KERN_CONT "DriveReady ");
160 if (stat & ATA_DF)
161 printk(KERN_CONT "DeviceFault ");
162 if (stat & ATA_DSC)
163 printk(KERN_CONT "SeekComplete ");
164 if (stat & ATA_DRQ)
165 printk(KERN_CONT "DataRequest ");
166 if (stat & ATA_CORR)
167 printk(KERN_CONT "CorrectedError ");
168 if (stat & ATA_IDX)
169 printk(KERN_CONT "Index ");
170 if (stat & ATA_ERR)
171 printk(KERN_CONT "Error ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100173 printk(KERN_CONT "}\n");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200174 if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100175 err = ide_read_error(drive);
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100176 printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100177 if (drive->media == ide_disk)
178 ide_dump_ata_error(drive, err);
179 else
180 ide_dump_atapi_error(drive, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 ide_dump_opcode(drive);
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL(ide_dump_status);