ide-iops.c 13.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Copyright (C) 2000-2002	Andre Hedrick <andre@linux-ide.org>
A
Alan Cox 已提交
3
 *  Copyright (C) 2003		Red Hat
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/genhd.h>
#include <linux/blkpg.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/bitops.h>
23
#include <linux/nmi.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29

#include <asm/byteorder.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>

30
void SELECT_MASK(ide_drive_t *drive, int mask)
L
Linus Torvalds 已提交
31
{
32 33 34 35
	const struct ide_port_ops *port_ops = drive->hwif->port_ops;

	if (port_ops && port_ops->maskproc)
		port_ops->maskproc(drive, mask);
L
Linus Torvalds 已提交
36 37
}

38 39
u8 ide_read_error(ide_drive_t *drive)
{
S
Sergei Shtylyov 已提交
40
	struct ide_taskfile tf;
41

S
Sergei Shtylyov 已提交
42
	drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_ERROR);
43

S
Sergei Shtylyov 已提交
44
	return tf.error;
45 46 47
}
EXPORT_SYMBOL_GPL(ide_read_error);

48
void ide_fix_driveid(u16 *id)
L
Linus Torvalds 已提交
49 50 51 52
{
#ifndef __LITTLE_ENDIAN
# ifdef __BIG_ENDIAN
	int i;
53

54
	for (i = 0; i < 256; i++)
55
		id[i] = __le16_to_cpu(id[i]);
L
Linus Torvalds 已提交
56 57 58 59 60 61
# else
#  error "Please fix <asm/byteorder.h>"
# endif
#endif
}

62 63 64 65
/*
 * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
 * removing leading/trailing blanks and compressing internal blanks.
 * It is primarily used to tidy up the model name/number fields as
66
 * returned by the ATA_CMD_ID_ATA[PI] commands.
67 68
 */

69
void ide_fixstring(u8 *s, const int bytecount, const int byteswap)
L
Linus Torvalds 已提交
70
{
71
	u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
L
Linus Torvalds 已提交
72 73 74

	if (byteswap) {
		/* convert from big-endian to host byte order */
75 76
		for (p = s ; p != end ; p += 2)
			be16_to_cpus((u16 *) p);
L
Linus Torvalds 已提交
77
	}
78

L
Linus Torvalds 已提交
79
	/* strip leading blanks */
80
	p = s;
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	while (s != end && *s == ' ')
		++s;
	/* compress internal blanks and strip trailing blanks */
	while (s != end && *s) {
		if (*s++ != ' ' || (s != end && *s && *s != ' '))
			*p++ = *(s-1);
	}
	/* wipe out trailing garbage */
	while (p != end)
		*p++ = '\0';
}
EXPORT_SYMBOL(ide_fixstring);

/*
 * This routine busy-waits for the drive status to be not "busy".
 * It then checks the status for all of the "good" bits and none
 * of the "bad" bits, and if all is okay it returns 0.  All other
98
 * cases return error -- caller may then invoke ide_error().
L
Linus Torvalds 已提交
99 100 101 102 103 104
 *
 * This routine should get fixed to not hog the cpu during extra long waits..
 * That could be done by busy-waiting for the first jiffy or two, and then
 * setting a timer to wake up at half second intervals thereafter,
 * until timeout is achieved, before timing out.
 */
105 106
static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
			   unsigned long timeout, u8 *rstat)
L
Linus Torvalds 已提交
107
{
108
	ide_hwif_t *hwif = drive->hwif;
109
	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
L
Linus Torvalds 已提交
110
	unsigned long flags;
111 112
	int i;
	u8 stat;
L
Linus Torvalds 已提交
113 114

	udelay(1);	/* spec allows drive 400ns to assert "BUSY" */
115
	stat = tp_ops->read_status(hwif);
116

117
	if (stat & ATA_BUSY) {
118
		local_save_flags(flags);
119
		local_irq_enable_in_hardirq();
L
Linus Torvalds 已提交
120
		timeout += jiffies;
121
		while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
L
Linus Torvalds 已提交
122 123 124 125 126 127
			if (time_after(jiffies, timeout)) {
				/*
				 * One last read after the timeout in case
				 * heavy interrupt load made us not make any
				 * progress during the timeout..
				 */
128
				stat = tp_ops->read_status(hwif);
129
				if ((stat & ATA_BUSY) == 0)
L
Linus Torvalds 已提交
130 131 132
					break;

				local_irq_restore(flags);
133 134
				*rstat = stat;
				return -EBUSY;
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
			}
		}
		local_irq_restore(flags);
	}
	/*
	 * Allow status to settle, then read it again.
	 * A few rare drives vastly violate the 400ns spec here,
	 * so we'll wait up to 10usec for a "good" status
	 * rather than expensively fail things immediately.
	 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
	 */
	for (i = 0; i < 10; i++) {
		udelay(1);
148
		stat = tp_ops->read_status(hwif);
149 150

		if (OK_STAT(stat, good, bad)) {
151
			*rstat = stat;
L
Linus Torvalds 已提交
152
			return 0;
153
		}
L
Linus Torvalds 已提交
154
	}
155 156 157 158 159 160 161 162 163
	*rstat = stat;
	return -EFAULT;
}

/*
 * In case of error returns error value after doing "*startstop = ide_error()".
 * The caller should return the updated value of "startstop" in this case,
 * "startstop" is unchanged when the function returns 0.
 */
164 165
int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good,
		  u8 bad, unsigned long timeout)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
	int err;
	u8 stat;

	/* bail early if we've exceeded max_failures */
	if (drive->max_failures && (drive->failures > drive->max_failures)) {
		*startstop = ide_stopped;
		return 1;
	}

	err = __ide_wait_stat(drive, good, bad, timeout, &stat);

	if (err) {
		char *s = (err == -EBUSY) ? "status timeout" : "status error";
		*startstop = ide_error(drive, s, stat);
	}

	return err;
L
Linus Torvalds 已提交
184 185 186
}
EXPORT_SYMBOL(ide_wait_stat);

187 188 189
/**
 *	ide_in_drive_list	-	look for drive in black/white list
 *	@id: drive identifier
190
 *	@table: list to inspect
191 192 193 194 195
 *
 *	Look for a drive in the blacklist and the whitelist tables
 *	Returns 1 if the drive is found in the table.
 */

196
int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
197
{
198 199 200 201
	for ( ; table->id_model; table++)
		if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
		    (!table->id_firmware ||
		     strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
202 203 204
			return 1;
	return 0;
}
205 206
EXPORT_SYMBOL_GPL(ide_in_drive_list);

207 208
/*
 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
209
 * Some optical devices with the buggy firmwares have the same problem.
210 211 212
 */
static const struct drive_list_entry ivb_list[] = {
	{ "QUANTUM FIREBALLlct10 05"	, "A03.0900"	},
213
	{ "QUANTUM FIREBALLlct20 30"	, "APL.0900"	},
214
	{ "TSSTcorp CDDVDW SH-S202J"	, "SB00"	},
215 216 217
	{ "TSSTcorp CDDVDW SH-S202J"	, "SB01"	},
	{ "TSSTcorp CDDVDW SH-S202N"	, "SB00"	},
	{ "TSSTcorp CDDVDW SH-S202N"	, "SB01"	},
218 219
	{ "TSSTcorp CDDVDW SH-S202H"	, "SB00"	},
	{ "TSSTcorp CDDVDW SH-S202H"	, "SB01"	},
220
	{ "SAMSUNG SP0822N"		, "WA100-10"	},
221 222 223
	{ NULL				, NULL		}
};

L
Linus Torvalds 已提交
224 225 226 227
/*
 *  All hosts that use the 80c ribbon must use!
 *  The name is derived from upper byte of word 93 and the 80c ribbon.
 */
228
u8 eighty_ninty_three(ide_drive_t *drive)
L
Linus Torvalds 已提交
229
{
230
	ide_hwif_t *hwif = drive->hwif;
231
	u16 *id = drive->id;
232
	int ivb = ide_in_drive_list(id, ivb_list);
233

234 235 236
	if (hwif->cbl == ATA_CBL_PATA40_SHORT)
		return 1;

237 238 239 240
	if (ivb)
		printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
				  drive->name);

241
	if (ata_id_is_sata(id) && !ivb)
242 243
		return 1;

244
	if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
245
		goto no_80w;
246

247 248
	/*
	 * FIXME:
249
	 * - change master/slave IDENTIFY order
250
	 * - force bit13 (80c cable present) check also for !ivb devices
251 252
	 *   (unless the slave device is pre-ATA3)
	 */
253
	if (id[ATA_ID_HW_CONFIG] & 0x4000)
254 255
		return 1;

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	if (ivb) {
		const char *model = (char *)&id[ATA_ID_PROD];

		if (strstr(model, "TSSTcorp CDDVDW SH-S202")) {
			/*
			 * These ATAPI devices always report 80c cable
			 * so we have to depend on the host in this case.
			 */
			if (hwif->cbl == ATA_CBL_PATA80)
				return 1;
		} else {
			/* Depend on the device side cable detection. */
			if (id[ATA_ID_HW_CONFIG] & 0x2000)
				return 1;
		}
	}
272
no_80w:
B
Bartlomiej Zolnierkiewicz 已提交
273
	if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
274 275 276 277
		return 0;

	printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
			    "limiting max speed to UDMA33\n",
278 279
			    drive->name,
			    hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
280

B
Bartlomiej Zolnierkiewicz 已提交
281
	drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
282 283

	return 0;
L
Linus Torvalds 已提交
284 285
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static const char *nien_quirk_list[] = {
	"QUANTUM FIREBALLlct08 08",
	"QUANTUM FIREBALLP KA6.4",
	"QUANTUM FIREBALLP KA9.1",
	"QUANTUM FIREBALLP KX13.6",
	"QUANTUM FIREBALLP KX20.5",
	"QUANTUM FIREBALLP KX27.3",
	"QUANTUM FIREBALLP LM20.4",
	"QUANTUM FIREBALLP LM20.5",
	NULL
};

void ide_check_nien_quirk_list(ide_drive_t *drive)
{
	const char **list, *m = (char *)&drive->id[ATA_ID_PROD];

	for (list = nien_quirk_list; *list != NULL; list++)
		if (strstr(m, *list) != NULL) {
304
			drive->dev_flags |= IDE_DFLAG_NIEN_QUIRK;
305 306 307 308
			return;
		}
}

309
int ide_driveid_update(ide_drive_t *drive)
L
Linus Torvalds 已提交
310
{
311
	u16 *id;
312
	int rc;
313 314 315 316

	id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
	if (id == NULL)
		return 0;
L
Linus Torvalds 已提交
317 318

	SELECT_MASK(drive, 1);
319 320
	rc = ide_dev_read_id(drive, ATA_CMD_ID_ATA, id);
	SELECT_MASK(drive, 0);
321

322
	if (rc)
323
		goto out_err;
324

325 326 327
	drive->id[ATA_ID_UDMA_MODES]  = id[ATA_ID_UDMA_MODES];
	drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
	drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
328
	drive->id[ATA_ID_CFA_MODES]   = id[ATA_ID_CFA_MODES];
329 330 331 332
	/* anything more ? */

	kfree(id);

L
Linus Torvalds 已提交
333
	return 1;
334 335 336 337 338
out_err:
	if (rc == 2)
		printk(KERN_ERR "%s: %s: bad status\n", drive->name, __func__);
	kfree(id);
	return 0;
L
Linus Torvalds 已提交
339 340
}

341
int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
L
Linus Torvalds 已提交
342
{
343
	ide_hwif_t *hwif = drive->hwif;
344
	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
S
Sergei Shtylyov 已提交
345
	struct ide_taskfile tf;
346
	u16 *id = drive->id, i;
347
	int error = 0;
L
Linus Torvalds 已提交
348 349 350
	u8 stat;

#ifdef CONFIG_BLK_DEV_IDEDMA
351 352
	if (hwif->dma_ops)	/* check if host supports DMA */
		hwif->dma_ops->dma_host_set(drive, 0);
L
Linus Torvalds 已提交
353 354
#endif

355
	/* Skip setting PIO flow-control modes on pre-EIDE drives */
356
	if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
357 358
		goto skip;

L
Linus Torvalds 已提交
359 360 361 362 363 364
	/*
	 * Don't use ide_wait_cmd here - it will
	 * attempt to set_geometry and recalibrate,
	 * but for some reason these don't work at
	 * this point (lost interrupt).
	 */
365

L
Linus Torvalds 已提交
366 367 368 369 370 371
	/*
	 *	FIXME: we race against the running IRQ here if
	 *	this is called from non IRQ context. If we use
	 *	disable_irq() we hang on the error path. Work
	 *	is needed.
	 */
372 373
	disable_irq_nosync(hwif->irq);

L
Linus Torvalds 已提交
374
	udelay(1);
S
Sergei Shtylyov 已提交
375
	tp_ops->dev_select(drive);
376
	SELECT_MASK(drive, 1);
L
Linus Torvalds 已提交
377
	udelay(1);
378
	tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
379

S
Sergei Shtylyov 已提交
380 381 382
	memset(&tf, 0, sizeof(tf));
	tf.feature = SETFEATURES_XFER;
	tf.nsect   = speed;
383

S
Sergei Shtylyov 已提交
384
	tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE | IDE_VALID_NSECT);
385

386
	tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
387

388
	if (drive->dev_flags & IDE_DFLAG_NIEN_QUIRK)
389
		tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
L
Linus Torvalds 已提交
390

391
	error = __ide_wait_stat(drive, drive->ready_stat,
392
				ATA_BUSY | ATA_DRQ | ATA_ERR,
393
				WAIT_CMD, &stat);
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403

	SELECT_MASK(drive, 0);

	enable_irq(hwif->irq);

	if (error) {
		(void) ide_dump_status(drive, "set_drive_speed_status", stat);
		return error;
	}

404 405 406 407
	if (speed >= XFER_SW_DMA_0) {
		id[ATA_ID_UDMA_MODES]  &= ~0xFF00;
		id[ATA_ID_MWDMA_MODES] &= ~0x0700;
		id[ATA_ID_SWDMA_MODES] &= ~0x0700;
408 409 410 411
		if (ata_id_is_cfa(id))
			id[ATA_ID_CFA_MODES] &= ~0x0E00;
	} else	if (ata_id_is_cfa(id))
		id[ATA_ID_CFA_MODES] &= ~0x01C0;
L
Linus Torvalds 已提交
412

413
 skip:
L
Linus Torvalds 已提交
414
#ifdef CONFIG_BLK_DEV_IDEDMA
B
Bartlomiej Zolnierkiewicz 已提交
415
	if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
416 417
		hwif->dma_ops->dma_host_set(drive, 1);
	else if (hwif->dma_ops)	/* check if host supports DMA */
418
		ide_dma_off_quietly(drive);
L
Linus Torvalds 已提交
419 420
#endif

421 422 423
	if (speed >= XFER_UDMA_0) {
		i = 1 << (speed - XFER_UDMA_0);
		id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
424 425 426
	} else if (ata_id_is_cfa(id) && speed >= XFER_MW_DMA_3) {
		i = speed - XFER_MW_DMA_2;
		id[ATA_ID_CFA_MODES] |= i << 9;
427 428 429 430 431 432
	} else if (speed >= XFER_MW_DMA_0) {
		i = 1 << (speed - XFER_MW_DMA_0);
		id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
	} else if (speed >= XFER_SW_DMA_0) {
		i = 1 << (speed - XFER_SW_DMA_0);
		id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
433 434 435
	} else if (ata_id_is_cfa(id) && speed >= XFER_PIO_5) {
		i = speed - XFER_PIO_4;
		id[ATA_ID_CFA_MODES] |= i << 6;
L
Linus Torvalds 已提交
436
	}
437

L
Linus Torvalds 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	if (!drive->init_speed)
		drive->init_speed = speed;
	drive->current_speed = speed;
	return error;
}

/*
 * This should get invoked any time we exit the driver to
 * wait for an interrupt response from a drive.  handler() points
 * at the appropriate code to handle the next interrupt, and a
 * timer is started to prevent us from waiting forever in case
 * something goes wrong (see the ide_timer_expiry() handler later on).
 *
 * See also ide_execute_command
 */
453
void __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
454
		       unsigned int timeout)
L
Linus Torvalds 已提交
455
{
456 457 458 459 460 461 462
	ide_hwif_t *hwif = drive->hwif;

	BUG_ON(hwif->handler);
	hwif->handler		= handler;
	hwif->timer.expires	= jiffies + timeout;
	hwif->req_gen_timer	= hwif->req_gen;
	add_timer(&hwif->timer);
L
Linus Torvalds 已提交
463 464
}

465 466
void ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
		     unsigned int timeout)
L
Linus Torvalds 已提交
467
{
468
	ide_hwif_t *hwif = drive->hwif;
L
Linus Torvalds 已提交
469
	unsigned long flags;
470

471
	spin_lock_irqsave(&hwif->lock, flags);
472
	__ide_set_handler(drive, handler, timeout);
473
	spin_unlock_irqrestore(&hwif->lock, flags);
L
Linus Torvalds 已提交
474 475
}
EXPORT_SYMBOL(ide_set_handler);
476

L
Linus Torvalds 已提交
477 478 479
/**
 *	ide_execute_command	-	execute an IDE command
 *	@drive: IDE drive to issue the command against
480
 *	@cmd: command
L
Linus Torvalds 已提交
481 482 483 484
 *	@handler: handler for next phase
 *	@timeout: timeout for command
 *
 *	Helper function to issue an IDE command. This handles the
485
 *	atomicity requirements, command timing and ensures that the
L
Linus Torvalds 已提交
486 487 488
 *	handler and IRQ setup do not race. All IDE command kick off
 *	should go via this function or do equivalent locking.
 */
489

490 491
void ide_execute_command(ide_drive_t *drive, struct ide_cmd *cmd,
			 ide_handler_t *handler, unsigned timeout)
L
Linus Torvalds 已提交
492
{
493
	ide_hwif_t *hwif = drive->hwif;
L
Linus Torvalds 已提交
494
	unsigned long flags;
495

496
	spin_lock_irqsave(&hwif->lock, flags);
497 498 499 500 501
	if ((cmd->protocol != ATAPI_PROT_DMA &&
	     cmd->protocol != ATAPI_PROT_PIO) ||
	    (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT))
		__ide_set_handler(drive, handler, timeout);
	hwif->tp_ops->exec_command(hwif, cmd->tf.command);
502 503 504 505 506 507
	/*
	 * Drive takes 400nS to respond, we must avoid the IRQ being
	 * serviced before that.
	 *
	 * FIXME: we could skip this delay with care on non shared devices
	 */
L
Linus Torvalds 已提交
508
	ndelay(400);
509
	spin_unlock_irqrestore(&hwif->lock, flags);
L
Linus Torvalds 已提交
510 511 512 513
}

/*
 * ide_wait_not_busy() waits for the currently selected device on the hwif
514
 * to report a non-busy status, see comments in ide_probe_port().
L
Linus Torvalds 已提交
515 516 517 518 519
 */
int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
{
	u8 stat = 0;

520
	while (timeout--) {
L
Linus Torvalds 已提交
521 522 523 524 525
		/*
		 * Turn this into a schedule() sleep once I'm sure
		 * about locking issues (2.5 work ?).
		 */
		mdelay(1);
526
		stat = hwif->tp_ops->read_status(hwif);
527
		if ((stat & ATA_BUSY) == 0)
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535
			return 0;
		/*
		 * Assume a value of 0xff means nothing is connected to
		 * the interface and it doesn't implement the pull-down
		 * resistor on D7.
		 */
		if (stat == 0xff)
			return -ENODEV;
536
		touch_softlockup_watchdog();
537
		touch_nmi_watchdog();
L
Linus Torvalds 已提交
538 539 540
	}
	return -EBUSY;
}