^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct ide_taskfile *tf = &cmd->tf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) u32 high, low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (lba48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) tf = &cmd->hob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) high = tf->device & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return ((u64)high << 24) | low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) EXPORT_SYMBOL_GPL(ide_get_lba_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static void ide_dump_sector(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct ide_cmd cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct ide_taskfile *tf = &cmd.tf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) memset(&cmd, 0, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (lba48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) cmd.valid.in.tf = IDE_VALID_LBA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) cmd.valid.in.hob = IDE_VALID_LBA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) cmd.tf_flags = IDE_TFLAG_LBA48;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) cmd.valid.in.tf = IDE_VALID_LBA | IDE_VALID_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ide_tf_readback(drive, &cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (lba48 || (tf->device & ATA_LBA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) printk(KERN_CONT ", LBAsect=%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) (unsigned long long)ide_get_lba_addr(&cmd, lba48));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) tf->device & 0xf, tf->lbal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) printk(KERN_CONT "{ ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (err & ATA_ABORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) printk(KERN_CONT "DriveStatusError ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (err & ATA_ICRC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) printk(KERN_CONT "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (err & ATA_UNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) printk(KERN_CONT "UncorrectableError ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (err & ATA_IDNF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) printk(KERN_CONT "SectorIdNotFound ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (err & ATA_TRK0NF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) printk(KERN_CONT "TrackZeroNotFound ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (err & ATA_AMNF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) printk(KERN_CONT "AddrMarkNotFound ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) printk(KERN_CONT "}");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct request *rq = drive->hwif->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ide_dump_sector(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) printk(KERN_CONT ", sector=%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (unsigned long long)blk_rq_pos(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) printk(KERN_CONT "{ ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (err & ATAPI_ILI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) printk(KERN_CONT "IllegalLengthIndication ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (err & ATAPI_EOM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) printk(KERN_CONT "EndOfMedia ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (err & ATA_ABORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printk(KERN_CONT "AbortedCommand ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (err & ATA_MCR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printk(KERN_CONT "MediaChangeRequested ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (err & ATAPI_LFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) printk(KERN_CONT "LastFailedSense=0x%02x ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) (err & ATAPI_LFS) >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) printk(KERN_CONT "}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * ide_dump_status - translate ATA/ATAPI error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @drive: drive that status applies to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @msg: text message to print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @stat: status byte to decode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Error reporting, in human readable form (luxurious, but a memory hog).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Combines the drive name, message and status byte to provide a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * user understandable explanation of the device error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u8 err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (stat & ATA_BUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) printk(KERN_CONT "Busy ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (stat & ATA_DRDY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) printk(KERN_CONT "DriveReady ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (stat & ATA_DF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) printk(KERN_CONT "DeviceFault ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (stat & ATA_DSC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) printk(KERN_CONT "SeekComplete ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (stat & ATA_DRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) printk(KERN_CONT "DataRequest ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (stat & ATA_CORR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) printk(KERN_CONT "CorrectedError ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (stat & ATA_SENSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) printk(KERN_CONT "Sense ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (stat & ATA_ERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) printk(KERN_CONT "Error ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) printk(KERN_CONT "}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err = ide_read_error(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (drive->media == ide_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ide_dump_ata_error(drive, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ide_dump_atapi_error(drive, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) printk(KERN_ERR "%s: possibly failed opcode: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) drive->name, drive->hwif->cmd.tf.command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) EXPORT_SYMBOL(ide_dump_status);