blob: 7b9f655adbc2d184e1c07ded0f41def2326b4951 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
3#include <linux/string.h>
4#include <linux/kernel.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -04005#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/ide.h>
8#include <linux/bitops.h>
9
Sergei Shtylyov745483f2009-04-08 14:13:02 +020010u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010011{
Sergei Shtylyov745483f2009-04-08 14:13:02 +020012 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010013 u32 high, low;
14
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010015 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
Sergei Shtylyov745483f2009-04-08 14:13:02 +020016 if (lba48) {
17 tf = &cmd->hob;
18 high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
19 } else
20 high = tf->device & 0xf;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010021
22 return ((u64)high << 24) | low;
23}
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +010024EXPORT_SYMBOL_GPL(ide_get_lba_addr);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010025
26static void ide_dump_sector(ide_drive_t *drive)
27{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010028 struct ide_cmd cmd;
29 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020030 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010031
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010032 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov60f85012009-04-08 14:13:01 +020033 if (lba48) {
34 cmd.valid.in.tf = IDE_VALID_LBA;
35 cmd.valid.in.hob = IDE_VALID_LBA;
36 cmd.tf_flags = IDE_TFLAG_LBA48;
37 } else
38 cmd.valid.in.tf = IDE_VALID_LBA | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010039
Sergei Shtylyov3153c262009-04-08 14:13:03 +020040 ide_tf_readback(drive, &cmd);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010041
42 if (lba48 || (tf->device & ATA_LBA))
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010043 printk(KERN_CONT ", LBAsect=%llu",
Sergei Shtylyov745483f2009-04-08 14:13:02 +020044 (unsigned long long)ide_get_lba_addr(&cmd, lba48));
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010045 else
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010046 printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
47 tf->device & 0xf, tf->lbal);
Bartlomiej Zolnierkiewiczc2b57cd2008-01-25 22:17:17 +010048}
49
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010050static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Bartlomiej Zolnierkiewicz26bfcf22009-05-22 16:23:37 +020052 printk(KERN_CONT "{ ");
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010053 if (err & ATA_ABORTED)
54 printk(KERN_CONT "DriveStatusError ");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020055 if (err & ATA_ICRC)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010056 printk(KERN_CONT "%s",
57 (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
58 if (err & ATA_UNC)
59 printk(KERN_CONT "UncorrectableError ");
60 if (err & ATA_IDNF)
61 printk(KERN_CONT "SectorIdNotFound ");
62 if (err & ATA_TRK0NF)
63 printk(KERN_CONT "TrackZeroNotFound ");
64 if (err & ATA_AMNF)
65 printk(KERN_CONT "AddrMarkNotFound ");
66 printk(KERN_CONT "}");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020067 if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
68 (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010069 struct request *rq = drive->hwif->rq;
70
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010071 ide_dump_sector(drive);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010072
73 if (rq)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010074 printk(KERN_CONT ", sector=%llu",
Tejun Heo9780e2dd2009-05-07 22:24:40 +090075 (unsigned long long)blk_rq_pos(rq));
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010076 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010077 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010080static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
81{
Bartlomiej Zolnierkiewicz26bfcf22009-05-22 16:23:37 +020082 printk(KERN_CONT "{ ");
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +010083 if (err & ATAPI_ILI)
84 printk(KERN_CONT "IllegalLengthIndication ");
85 if (err & ATAPI_EOM)
86 printk(KERN_CONT "EndOfMedia ");
87 if (err & ATA_ABORTED)
88 printk(KERN_CONT "AbortedCommand ");
89 if (err & ATA_MCR)
90 printk(KERN_CONT "MediaChangeRequested ");
91 if (err & ATAPI_LFS)
92 printk(KERN_CONT "LastFailedSense=0x%02x ",
93 (err & ATAPI_LFS) >> 4);
94 printk(KERN_CONT "}\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/**
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +010098 * ide_dump_status - translate ATA/ATAPI error
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * @drive: drive that status applies to
100 * @msg: text message to print
101 * @stat: status byte to decode
102 *
103 * Error reporting, in human readable form (luxurious, but a memory hog).
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100104 * Combines the drive name, message and status byte to provide a
105 * user understandable explanation of the device error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100108u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100110 u8 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100112 printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200113 if (stat & ATA_BUSY)
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100114 printk(KERN_CONT "Busy ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 else {
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100116 if (stat & ATA_DRDY)
117 printk(KERN_CONT "DriveReady ");
118 if (stat & ATA_DF)
119 printk(KERN_CONT "DeviceFault ");
120 if (stat & ATA_DSC)
121 printk(KERN_CONT "SeekComplete ");
122 if (stat & ATA_DRQ)
123 printk(KERN_CONT "DataRequest ");
124 if (stat & ATA_CORR)
125 printk(KERN_CONT "CorrectedError ");
Hannes Reinecke27f00e52015-03-27 16:46:33 +0100126 if (stat & ATA_SENSE)
127 printk(KERN_CONT "Sense ");
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100128 if (stat & ATA_ERR)
129 printk(KERN_CONT "Error ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100131 printk(KERN_CONT "}\n");
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200132 if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100133 err = ide_read_error(drive);
Bartlomiej Zolnierkiewicz2f996ac2008-12-29 20:27:36 +0100134 printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
Bartlomiej Zolnierkiewicze62925d2008-01-25 22:17:17 +0100135 if (drive->media == ide_disk)
136 ide_dump_ata_error(drive, err);
137 else
138 ide_dump_atapi_error(drive, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Bartlomiej Zolnierkiewiczcc301372009-05-22 16:23:38 +0200140
141 printk(KERN_ERR "%s: possibly failed opcode: 0x%02x\n",
142 drive->name, drive->hwif->cmd.tf.command);
143
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146EXPORT_SYMBOL(ide_dump_status);