ide-lib.c 4.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/ide.h>
#include <linux/bitops.h>

/**
 *	ide_toggle_bounce	-	handle bounce buffering
 *	@drive: drive to update
 *	@on: on/off boolean
 *
 *	Enable or disable bounce buffering for the device. Drives move
 *	between PIO and DMA and that changes the rules we need.
 */
16

L
Linus Torvalds 已提交
17 18 19 20
void ide_toggle_bounce(ide_drive_t *drive, int on)
{
	u64 addr = BLK_BOUNCE_HIGH;	/* dma64_addr_t */

21 22 23
	if (!PCI_DMA_BUS_IS_PHYS) {
		addr = BLK_BOUNCE_ANY;
	} else if (on && drive->media == ide_disk) {
24 25 26 27
		struct device *dev = drive->hwif->dev;

		if (dev && dev->dma_mask)
			addr = *dev->dma_mask;
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35
	}

	if (drive->queue)
		blk_queue_bounce_limit(drive->queue, addr);
}

static void ide_dump_opcode(ide_drive_t *drive)
{
36
	struct request *rq = drive->hwif->rq;
37
	struct ide_cmd *cmd = NULL;
L
Linus Torvalds 已提交
38 39 40

	if (!rq)
		return;
41 42

	if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
43
		cmd = rq->special;
L
Linus Torvalds 已提交
44

45
	printk(KERN_ERR "ide: failed opcode was: ");
46
	if (cmd == NULL)
47
		printk(KERN_CONT "unknown\n");
L
Linus Torvalds 已提交
48
	else
49
		printk(KERN_CONT "0x%02x\n", cmd->tf.command);
L
Linus Torvalds 已提交
50 51
}

52
u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
53 54 55 56 57 58 59 60 61 62 63 64
{
	u32 high, low;

	if (lba48)
		high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
			tf->hob_lbal;
	else
		high = tf->device & 0xf;
	low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;

	return ((u64)high << 24) | low;
}
65
EXPORT_SYMBOL_GPL(ide_get_lba_addr);
66 67 68

static void ide_dump_sector(ide_drive_t *drive)
{
69 70
	struct ide_cmd cmd;
	struct ide_taskfile *tf = &cmd.tf;
B
Bartlomiej Zolnierkiewicz 已提交
71
	u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
72

73
	memset(&cmd, 0, sizeof(cmd));
74 75 76 77 78 79
	if (lba48) {
		cmd.valid.in.tf  = IDE_VALID_LBA;
		cmd.valid.in.hob = IDE_VALID_LBA;
		cmd.tf_flags = IDE_TFLAG_LBA48;
	} else
		cmd.valid.in.tf  = IDE_VALID_LBA | IDE_VALID_DEVICE;
80

81
	drive->hwif->tp_ops->tf_read(drive, &cmd);
82 83

	if (lba48 || (tf->device & ATA_LBA))
84
		printk(KERN_CONT ", LBAsect=%llu",
A
Andrew Morton 已提交
85
			(unsigned long long)ide_get_lba_addr(tf, lba48));
86
	else
87 88
		printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
			tf->device & 0xf, tf->lbal);
89 90
}

91
static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
L
Linus Torvalds 已提交
92
{
93 94 95
	printk(KERN_ERR "{ ");
	if (err & ATA_ABORTED)
		printk(KERN_CONT "DriveStatusError ");
96
	if (err & ATA_ICRC)
97 98 99 100 101 102 103 104 105 106 107
		printk(KERN_CONT "%s",
			(err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
	if (err & ATA_UNC)
		printk(KERN_CONT "UncorrectableError ");
	if (err & ATA_IDNF)
		printk(KERN_CONT "SectorIdNotFound ");
	if (err & ATA_TRK0NF)
		printk(KERN_CONT "TrackZeroNotFound ");
	if (err & ATA_AMNF)
		printk(KERN_CONT "AddrMarkNotFound ");
	printk(KERN_CONT "}");
108 109
	if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
	    (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
110 111
		struct request *rq = drive->hwif->rq;

112
		ide_dump_sector(drive);
113 114

		if (rq)
115
			printk(KERN_CONT ", sector=%llu",
116
			       (unsigned long long)rq->sector);
L
Linus Torvalds 已提交
117
	}
118
	printk(KERN_CONT "\n");
119 120 121 122
}

static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
{
123 124 125 126 127 128 129 130 131 132 133 134 135
	printk(KERN_ERR "{ ");
	if (err & ATAPI_ILI)
		printk(KERN_CONT "IllegalLengthIndication ");
	if (err & ATAPI_EOM)
		printk(KERN_CONT "EndOfMedia ");
	if (err & ATA_ABORTED)
		printk(KERN_CONT "AbortedCommand ");
	if (err & ATA_MCR)
		printk(KERN_CONT "MediaChangeRequested ");
	if (err & ATAPI_LFS)
		printk(KERN_CONT "LastFailedSense=0x%02x ",
			(err & ATAPI_LFS) >> 4);
	printk(KERN_CONT "}\n");
L
Linus Torvalds 已提交
136 137 138
}

/**
139
 *	ide_dump_status		-	translate ATA/ATAPI error
L
Linus Torvalds 已提交
140 141 142 143 144
 *	@drive: drive that status applies to
 *	@msg: text message to print
 *	@stat: status byte to decode
 *
 *	Error reporting, in human readable form (luxurious, but a memory hog).
145 146
 *	Combines the drive name, message and status byte to provide a
 *	user understandable explanation of the device error.
L
Linus Torvalds 已提交
147 148
 */

149
u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
L
Linus Torvalds 已提交
150
{
151
	u8 err = 0;
L
Linus Torvalds 已提交
152

153
	printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
154
	if (stat & ATA_BUSY)
155
		printk(KERN_CONT "Busy ");
L
Linus Torvalds 已提交
156
	else {
157 158 159 160 161 162 163 164 165 166 167 168 169 170
		if (stat & ATA_DRDY)
			printk(KERN_CONT "DriveReady ");
		if (stat & ATA_DF)
			printk(KERN_CONT "DeviceFault ");
		if (stat & ATA_DSC)
			printk(KERN_CONT "SeekComplete ");
		if (stat & ATA_DRQ)
			printk(KERN_CONT "DataRequest ");
		if (stat & ATA_CORR)
			printk(KERN_CONT "CorrectedError ");
		if (stat & ATA_IDX)
			printk(KERN_CONT "Index ");
		if (stat & ATA_ERR)
			printk(KERN_CONT "Error ");
L
Linus Torvalds 已提交
171
	}
172
	printk(KERN_CONT "}\n");
173
	if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
174
		err = ide_read_error(drive);
175
		printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
176 177 178 179
		if (drive->media == ide_disk)
			ide_dump_ata_error(drive, err);
		else
			ide_dump_atapi_error(drive, err);
L
Linus Torvalds 已提交
180 181
	}
	ide_dump_opcode(drive);
182
	return err;
L
Linus Torvalds 已提交
183 184
}
EXPORT_SYMBOL(ide_dump_status);