ide-lib.c 10.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/ide.h>
#include <linux/bitops.h>

8 9 10 11 12 13 14 15 16
static const char *udma_str[] =
	 { "UDMA/16", "UDMA/25",  "UDMA/33",  "UDMA/44",
	   "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
static const char *mwdma_str[] =
	{ "MWDMA0", "MWDMA1", "MWDMA2" };
static const char *swdma_str[] =
	{ "SWDMA0", "SWDMA1", "SWDMA2" };
static const char *pio_str[] =
	{ "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
L
Linus Torvalds 已提交
17 18 19

/**
 *	ide_xfer_verbose	-	return IDE mode names
20
 *	@mode: transfer mode
L
Linus Torvalds 已提交
21 22 23 24 25
 *
 *	Returns a constant string giving the name of the mode
 *	requested.
 */

26
const char *ide_xfer_verbose(u8 mode)
L
Linus Torvalds 已提交
27
{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	const char *s;
	u8 i = mode & 0xf;

	if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
		s = udma_str[i];
	else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
		s = mwdma_str[i];
	else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
		s = swdma_str[i];
	else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
		s = pio_str[i & 0x7];
	else if (mode == XFER_PIO_SLOW)
		s = "PIO SLOW";
	else
		s = "XFER ERROR";

	return s;
L
Linus Torvalds 已提交
45 46 47 48 49
}

EXPORT_SYMBOL(ide_xfer_verbose);

/**
50 51
 *	ide_rate_filter		-	filter transfer mode
 *	@drive: IDE device
L
Linus Torvalds 已提交
52 53
 *	@speed: desired speed
 *
54
 *	Given the available transfer modes this function returns
L
Linus Torvalds 已提交
55
 *	the best available speed at or below the speed requested.
56
 *
57
 *	TODO: check device PIO capabilities
L
Linus Torvalds 已提交
58 59
 */

60
static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
L
Linus Torvalds 已提交
61
{
62
	ide_hwif_t *hwif = drive->hwif;
63
	u8 mode = ide_find_dma_mode(drive, speed);
64

65 66 67 68 69 70
	if (mode == 0) {
		if (hwif->pio_mask)
			mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
		else
			mode = XFER_PIO_4;
	}
L
Linus Torvalds 已提交
71

72
/*	printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
L
Linus Torvalds 已提交
73

74
	return min(speed, mode);
L
Linus Torvalds 已提交
75 76 77 78
}

/**
 *	ide_get_best_pio_mode	-	get PIO mode from drive
79
 *	@drive: drive to consider
L
Linus Torvalds 已提交
80
 *	@mode_wanted: preferred mode
81
 *	@max_mode: highest allowed mode
L
Linus Torvalds 已提交
82 83 84 85
 *
 *	This routine returns the recommended PIO settings for a given drive,
 *	based on the drive->id information and the ide_pio_blacklist[].
 *
86 87
 *	Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
 *	This is used by most chipset support modules when "auto-tuning".
L
Linus Torvalds 已提交
88 89
 */

90
u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
L
Linus Torvalds 已提交
91
{
92 93
	u16 *id = drive->id;
	int pio_mode = -1, overridden = 0;
L
Linus Torvalds 已提交
94

95 96 97
	if (mode_wanted != 255)
		return min_t(u8, mode_wanted, max_mode);

98 99 100 101
	if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
		pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);

	if (pio_mode != -1) {
102
		printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
L
Linus Torvalds 已提交
103
	} else {
104
		pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
L
Linus Torvalds 已提交
105 106 107 108
		if (pio_mode > 2) {	/* 2 is maximum allowed tPIO value */
			pio_mode = 2;
			overridden = 1;
		}
109 110

		if (id[ATA_ID_FIELD_VALID] & 2) {	      /* ATA2? */
111
			if (ata_id_has_iordy(id)) {
112
				if (id[ATA_ID_PIO_MODES] & 7) {
L
Linus Torvalds 已提交
113
					overridden = 0;
114
					if (id[ATA_ID_PIO_MODES] & 4)
L
Linus Torvalds 已提交
115
						pio_mode = 5;
116
					else if (id[ATA_ID_PIO_MODES] & 2)
L
Linus Torvalds 已提交
117 118 119 120 121 122 123
						pio_mode = 4;
					else
						pio_mode = 3;
				}
			}
		}

124 125 126
		if (overridden)
			printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
					 drive->name);
L
Linus Torvalds 已提交
127
	}
128 129

	if (pio_mode > max_mode)
L
Linus Torvalds 已提交
130
		pio_mode = max_mode;
131

L
Linus Torvalds 已提交
132 133 134 135 136
	return pio_mode;
}

EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);

137 138 139 140
/* req_pio == "255" for auto-tune */
void ide_set_pio(ide_drive_t *drive, u8 req_pio)
{
	ide_hwif_t *hwif = drive->hwif;
141
	const struct ide_port_ops *port_ops = hwif->port_ops;
142 143
	u8 host_pio, pio;

144
	if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
145
	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		return;

	BUG_ON(hwif->pio_mask == 0x00);

	host_pio = fls(hwif->pio_mask) - 1;

	pio = ide_get_best_pio_mode(drive, req_pio, host_pio);

	/*
	 * TODO:
	 * - report device max PIO mode
	 * - check req_pio != 255 against device max PIO mode
	 */
	printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
			  drive->name, host_pio, req_pio,
			  req_pio == 255 ? "(auto-tune)" : "", pio);

163
	(void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
164 165 166 167
}

EXPORT_SYMBOL_GPL(ide_set_pio);

L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180
/**
 *	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.
 */
 
void ide_toggle_bounce(ide_drive_t *drive, int on)
{
	u64 addr = BLK_BOUNCE_HIGH;	/* dma64_addr_t */

181 182 183
	if (!PCI_DMA_BUS_IS_PHYS) {
		addr = BLK_BOUNCE_ANY;
	} else if (on && drive->media == ide_disk) {
184 185 186 187
		struct device *dev = drive->hwif->dev;

		if (dev && dev->dma_mask)
			addr = *dev->dma_mask;
L
Linus Torvalds 已提交
188 189 190 191 192 193
	}

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

194 195 196
int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
{
	ide_hwif_t *hwif = drive->hwif;
197
	const struct ide_port_ops *port_ops = hwif->port_ops;
198

199 200 201
	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
		return 0;

202
	if (port_ops == NULL || port_ops->set_pio_mode == NULL)
203 204 205 206 207 208
		return -1;

	/*
	 * TODO: temporary hack for some legacy host drivers that didn't
	 * set transfer mode on the device in ->set_pio_mode method...
	 */
209 210
	if (port_ops->set_dma_mode == NULL) {
		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
211 212 213 214 215 216
		return 0;
	}

	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
		if (ide_config_drive_speed(drive, mode))
			return -1;
217
		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
218 219
		return 0;
	} else {
220
		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
221 222 223 224 225 226 227
		return ide_config_drive_speed(drive, mode);
	}
}

int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
{
	ide_hwif_t *hwif = drive->hwif;
228
	const struct ide_port_ops *port_ops = hwif->port_ops;
229

230 231 232
	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
		return 0;

233
	if (port_ops == NULL || port_ops->set_dma_mode == NULL)
234 235 236 237 238
		return -1;

	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
		if (ide_config_drive_speed(drive, mode))
			return -1;
239
		port_ops->set_dma_mode(drive, mode);
240 241
		return 0;
	} else {
242
		port_ops->set_dma_mode(drive, mode);
243 244 245 246 247 248
		return ide_config_drive_speed(drive, mode);
	}
}

EXPORT_SYMBOL_GPL(ide_set_dma_mode);

L
Linus Torvalds 已提交
249 250 251
/**
 *	ide_set_xfer_rate	-	set transfer rate
 *	@drive: drive to set
252
 *	@rate: speed to attempt to set
L
Linus Torvalds 已提交
253 254 255
 *	
 *	General helper for setting the speed of an IDE device. This
 *	function knows about user enforced limits from the configuration
256
 *	which ->set_pio_mode/->set_dma_mode does not.
L
Linus Torvalds 已提交
257
 */
258

L
Linus Torvalds 已提交
259 260
int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
{
261
	ide_hwif_t *hwif = drive->hwif;
262
	const struct ide_port_ops *port_ops = hwif->port_ops;
263

264
	if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
265
	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
L
Linus Torvalds 已提交
266
		return -1;
267 268 269

	rate = ide_rate_filter(drive, rate);

270 271
	BUG_ON(rate < XFER_PIO_0);

272 273
	if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
		return ide_set_pio_mode(drive, rate);
274

275
	return ide_set_dma_mode(drive, rate);
L
Linus Torvalds 已提交
276 277 278 279 280
}

static void ide_dump_opcode(ide_drive_t *drive)
{
	struct request *rq;
281
	ide_task_t *task = NULL;
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289

	spin_lock(&ide_lock);
	rq = NULL;
	if (HWGROUP(drive))
		rq = HWGROUP(drive)->rq;
	spin_unlock(&ide_lock);
	if (!rq)
		return;
290 291 292

	if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
		task = rq->special;
L
Linus Torvalds 已提交
293 294

	printk("ide: failed opcode was: ");
295 296
	if (task == NULL)
		printk(KERN_CONT "unknown\n");
L
Linus Torvalds 已提交
297
	else
298
		printk(KERN_CONT "0x%02x\n", task->tf.command);
L
Linus Torvalds 已提交
299 300
}

301
u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
302 303 304 305 306 307 308 309 310 311 312 313
{
	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;
}
314
EXPORT_SYMBOL_GPL(ide_get_lba_addr);
315 316 317 318 319 320 321 322 323 324 325 326 327 328

static void ide_dump_sector(ide_drive_t *drive)
{
	ide_task_t task;
	struct ide_taskfile *tf = &task.tf;
	int lba48 = (drive->addressing == 1) ? 1 : 0;

	memset(&task, 0, sizeof(task));
	if (lba48)
		task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
				IDE_TFLAG_LBA48;
	else
		task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;

329
	drive->hwif->tp_ops->tf_read(drive, &task);
330 331

	if (lba48 || (tf->device & ATA_LBA))
A
Andrew Morton 已提交
332 333
		printk(", LBAsect=%llu",
			(unsigned long long)ide_get_lba_addr(tf, lba48));
334 335 336 337 338
	else
		printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
					 tf->device & 0xf, tf->lbal);
}

339
static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
L
Linus Torvalds 已提交
340
{
341
	printk("{ ");
342 343 344 345 346 347 348
	if (err & ATA_ABORTED)	printk("DriveStatusError ");
	if (err & ATA_ICRC)
		printk((err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
	if (err & ATA_UNC)	printk("UncorrectableError ");
	if (err & ATA_IDNF)	printk("SectorIdNotFound ");
	if (err & ATA_TRK0NF)	printk("TrackZeroNotFound ");
	if (err & ATA_AMNF)	printk("AddrMarkNotFound ");
349
	printk("}");
350 351
	if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
	    (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
352 353 354 355
		ide_dump_sector(drive);
		if (HWGROUP(drive) && HWGROUP(drive)->rq)
			printk(", sector=%llu",
			       (unsigned long long)HWGROUP(drive)->rq->sector);
L
Linus Torvalds 已提交
356
	}
357 358 359 360 361 362
	printk("\n");
}

static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
{
	printk("{ ");
363 364 365 366 367 368
	if (err & ATAPI_ILI)	printk("IllegalLengthIndication ");
	if (err & ATAPI_EOM)	printk("EndOfMedia ");
	if (err & ATA_ABORTED)	printk("AbortedCommand ");
	if (err & ATA_MCR)	printk("MediaChangeRequested ");
	if (err & ATAPI_LFS)	printk("LastFailedSense=0x%02x ",
				       (err & ATAPI_LFS) >> 4);
369
	printk("}\n");
L
Linus Torvalds 已提交
370 371 372
}

/**
373
 *	ide_dump_status		-	translate ATA/ATAPI error
L
Linus Torvalds 已提交
374 375 376 377 378
 *	@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).
379 380
 *	Combines the drive name, message and status byte to provide a
 *	user understandable explanation of the device error.
L
Linus Torvalds 已提交
381 382
 */

383
u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
L
Linus Torvalds 已提交
384 385
{
	unsigned long flags;
386
	u8 err = 0;
L
Linus Torvalds 已提交
387

388
	local_irq_save(flags);
L
Linus Torvalds 已提交
389
	printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
390
	if (stat & ATA_BUSY)
L
Linus Torvalds 已提交
391 392
		printk("Busy ");
	else {
393 394 395 396 397 398 399
		if (stat & ATA_DRDY)	printk("DriveReady ");
		if (stat & ATA_DF)	printk("DeviceFault ");
		if (stat & ATA_DSC)	printk("SeekComplete ");
		if (stat & ATA_DRQ)	printk("DataRequest ");
		if (stat & ATA_CORR)	printk("CorrectedError ");
		if (stat & ATA_IDX)	printk("Index ");
		if (stat & ATA_ERR)	printk("Error ");
L
Linus Torvalds 已提交
400 401
	}
	printk("}\n");
402
	if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
403
		err = ide_read_error(drive);
404 405 406 407 408
		printk("%s: %s: error=0x%02x ", drive->name, msg, err);
		if (drive->media == ide_disk)
			ide_dump_ata_error(drive, err);
		else
			ide_dump_atapi_error(drive, err);
L
Linus Torvalds 已提交
409 410 411
	}
	ide_dump_opcode(drive);
	local_irq_restore(flags);
412
	return err;
L
Linus Torvalds 已提交
413 414 415
}

EXPORT_SYMBOL(ide_dump_status);