ide-lib.c revision e4e8d02f56f5c0cefc6713384629e068193d706a
1#include <linux/types.h>
2#include <linux/string.h>
3#include <linux/kernel.h>
4#include <linux/interrupt.h>
5#include <linux/hdreg.h>
6#include <linux/ide.h>
7#include <linux/bitops.h>
8
9static const char *udma_str[] =
10	 { "UDMA/16", "UDMA/25",  "UDMA/33",  "UDMA/44",
11	   "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
12static const char *mwdma_str[] =
13	{ "MWDMA0", "MWDMA1", "MWDMA2" };
14static const char *swdma_str[] =
15	{ "SWDMA0", "SWDMA1", "SWDMA2" };
16static const char *pio_str[] =
17	{ "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
18
19/**
20 *	ide_xfer_verbose	-	return IDE mode names
21 *	@mode: transfer mode
22 *
23 *	Returns a constant string giving the name of the mode
24 *	requested.
25 */
26
27const char *ide_xfer_verbose(u8 mode)
28{
29	const char *s;
30	u8 i = mode & 0xf;
31
32	if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
33		s = udma_str[i];
34	else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
35		s = mwdma_str[i];
36	else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
37		s = swdma_str[i];
38	else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
39		s = pio_str[i & 0x7];
40	else if (mode == XFER_PIO_SLOW)
41		s = "PIO SLOW";
42	else
43		s = "XFER ERROR";
44
45	return s;
46}
47
48EXPORT_SYMBOL(ide_xfer_verbose);
49
50/**
51 *	ide_rate_filter		-	filter transfer mode
52 *	@drive: IDE device
53 *	@speed: desired speed
54 *
55 *	Given the available transfer modes this function returns
56 *	the best available speed at or below the speed requested.
57 *
58 *	TODO: check device PIO capabilities
59 */
60
61static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
62{
63	ide_hwif_t *hwif = drive->hwif;
64	u8 mode = ide_find_dma_mode(drive, speed);
65
66	if (mode == 0) {
67		if (hwif->pio_mask)
68			mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
69		else
70			mode = XFER_PIO_4;
71	}
72
73/*	printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
74
75	return min(speed, mode);
76}
77
78/*
79 * Standard (generic) timings for PIO modes, from ATA2 specification.
80 * These timings are for access to the IDE data port register *only*.
81 * Some drives may specify a mode, while also specifying a different
82 * value for cycle_time (from drive identification data).
83 */
84const ide_pio_timings_t ide_pio_timings[6] = {
85	{ 70,	165,	600 },	/* PIO Mode 0 */
86	{ 50,	125,	383 },	/* PIO Mode 1 */
87	{ 30,	100,	240 },	/* PIO Mode 2 */
88	{ 30,	80,	180 },	/* PIO Mode 3 with IORDY */
89	{ 25,	70,	120 },	/* PIO Mode 4 with IORDY */
90	{ 20,	50,	100 }	/* PIO Mode 5 with IORDY (nonstandard) */
91};
92
93EXPORT_SYMBOL_GPL(ide_pio_timings);
94
95/*
96 * Shared data/functions for determining best PIO mode for an IDE drive.
97 * Most of this stuff originally lived in cmd640.c, and changes to the
98 * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
99 * breaking the fragile cmd640.c support.
100 */
101
102/*
103 * Black list. Some drives incorrectly report their maximal PIO mode,
104 * at least in respect to CMD640. Here we keep info on some known drives.
105 */
106static struct ide_pio_info {
107	const char	*name;
108	int		pio;
109} ide_pio_blacklist [] = {
110	{ "Conner Peripherals 540MB - CFS540A", 3 },
111
112	{ "WDC AC2700",  3 },
113	{ "WDC AC2540",  3 },
114	{ "WDC AC2420",  3 },
115	{ "WDC AC2340",  3 },
116	{ "WDC AC2250",  0 },
117	{ "WDC AC2200",  0 },
118	{ "WDC AC21200", 4 },
119	{ "WDC AC2120",  0 },
120	{ "WDC AC2850",  3 },
121	{ "WDC AC1270",  3 },
122	{ "WDC AC1170",  1 },
123	{ "WDC AC1210",  1 },
124	{ "WDC AC280",   0 },
125	{ "WDC AC31000", 3 },
126	{ "WDC AC31200", 3 },
127
128	{ "Maxtor 7131 AT", 1 },
129	{ "Maxtor 7171 AT", 1 },
130	{ "Maxtor 7213 AT", 1 },
131	{ "Maxtor 7245 AT", 1 },
132	{ "Maxtor 7345 AT", 1 },
133	{ "Maxtor 7546 AT", 3 },
134	{ "Maxtor 7540 AV", 3 },
135
136	{ "SAMSUNG SHD-3121A", 1 },
137	{ "SAMSUNG SHD-3122A", 1 },
138	{ "SAMSUNG SHD-3172A", 1 },
139
140	{ "ST5660A",  3 },
141	{ "ST3660A",  3 },
142	{ "ST3630A",  3 },
143	{ "ST3655A",  3 },
144	{ "ST3391A",  3 },
145	{ "ST3390A",  1 },
146	{ "ST3600A",  1 },
147	{ "ST3290A",  0 },
148	{ "ST3144A",  0 },
149	{ "ST3491A",  1 },	/* reports 3, should be 1 or 2 (depending on */
150				/* drive) according to Seagates FIND-ATA program */
151
152	{ "QUANTUM ELS127A", 0 },
153	{ "QUANTUM ELS170A", 0 },
154	{ "QUANTUM LPS240A", 0 },
155	{ "QUANTUM LPS210A", 3 },
156	{ "QUANTUM LPS270A", 3 },
157	{ "QUANTUM LPS365A", 3 },
158	{ "QUANTUM LPS540A", 3 },
159	{ "QUANTUM LIGHTNING 540A", 3 },
160	{ "QUANTUM LIGHTNING 730A", 3 },
161
162        { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
163        { "QUANTUM FIREBALL_640", 3 },
164        { "QUANTUM FIREBALL_1080", 3 },
165        { "QUANTUM FIREBALL_1280", 3 },
166	{ NULL,	0 }
167};
168
169/**
170 *	ide_scan_pio_blacklist 	-	check for a blacklisted drive
171 *	@model: Drive model string
172 *
173 *	This routine searches the ide_pio_blacklist for an entry
174 *	matching the start/whole of the supplied model name.
175 *
176 *	Returns -1 if no match found.
177 *	Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
178 */
179
180static int ide_scan_pio_blacklist (char *model)
181{
182	struct ide_pio_info *p;
183
184	for (p = ide_pio_blacklist; p->name != NULL; p++) {
185		if (strncmp(p->name, model, strlen(p->name)) == 0)
186			return p->pio;
187	}
188	return -1;
189}
190
191unsigned int ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
192{
193	struct hd_driveid *id = drive->id;
194	int cycle_time = 0;
195
196	if (id->field_valid & 2) {
197		if (id->capability & 8)
198			cycle_time = id->eide_pio_iordy;
199		else
200			cycle_time = id->eide_pio;
201	}
202
203	/* conservative "downgrade" for all pre-ATA2 drives */
204	if (pio < 3) {
205		if (cycle_time && cycle_time < ide_pio_timings[pio].cycle_time)
206			cycle_time = 0; /* use standard timing */
207	}
208
209	return cycle_time ? cycle_time : ide_pio_timings[pio].cycle_time;
210}
211
212EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
213
214/**
215 *	ide_get_best_pio_mode	-	get PIO mode from drive
216 *	@drive: drive to consider
217 *	@mode_wanted: preferred mode
218 *	@max_mode: highest allowed mode
219 *
220 *	This routine returns the recommended PIO settings for a given drive,
221 *	based on the drive->id information and the ide_pio_blacklist[].
222 *
223 *	Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
224 *	This is used by most chipset support modules when "auto-tuning".
225 */
226
227u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
228{
229	int pio_mode;
230	struct hd_driveid* id = drive->id;
231	int overridden  = 0;
232
233	if (mode_wanted != 255)
234		return min_t(u8, mode_wanted, max_mode);
235
236	if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
237	    (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
238		printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
239	} else {
240		pio_mode = id->tPIO;
241		if (pio_mode > 2) {	/* 2 is maximum allowed tPIO value */
242			pio_mode = 2;
243			overridden = 1;
244		}
245		if (id->field_valid & 2) {	  /* drive implements ATA2? */
246			if (id->capability & 8) { /* IORDY supported? */
247				if (id->eide_pio_modes & 7) {
248					overridden = 0;
249					if (id->eide_pio_modes & 4)
250						pio_mode = 5;
251					else if (id->eide_pio_modes & 2)
252						pio_mode = 4;
253					else
254						pio_mode = 3;
255				}
256			}
257		}
258
259		if (overridden)
260			printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
261					 drive->name);
262	}
263
264	if (pio_mode > max_mode)
265		pio_mode = max_mode;
266
267	return pio_mode;
268}
269
270EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
271
272/* req_pio == "255" for auto-tune */
273void ide_set_pio(ide_drive_t *drive, u8 req_pio)
274{
275	ide_hwif_t *hwif = drive->hwif;
276	const struct ide_port_ops *port_ops = hwif->port_ops;
277	u8 host_pio, pio;
278
279	if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
280	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
281		return;
282
283	BUG_ON(hwif->pio_mask == 0x00);
284
285	host_pio = fls(hwif->pio_mask) - 1;
286
287	pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
288
289	/*
290	 * TODO:
291	 * - report device max PIO mode
292	 * - check req_pio != 255 against device max PIO mode
293	 */
294	printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
295			  drive->name, host_pio, req_pio,
296			  req_pio == 255 ? "(auto-tune)" : "", pio);
297
298	(void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
299}
300
301EXPORT_SYMBOL_GPL(ide_set_pio);
302
303/**
304 *	ide_toggle_bounce	-	handle bounce buffering
305 *	@drive: drive to update
306 *	@on: on/off boolean
307 *
308 *	Enable or disable bounce buffering for the device. Drives move
309 *	between PIO and DMA and that changes the rules we need.
310 */
311
312void ide_toggle_bounce(ide_drive_t *drive, int on)
313{
314	u64 addr = BLK_BOUNCE_HIGH;	/* dma64_addr_t */
315
316	if (!PCI_DMA_BUS_IS_PHYS) {
317		addr = BLK_BOUNCE_ANY;
318	} else if (on && drive->media == ide_disk) {
319		struct device *dev = drive->hwif->dev;
320
321		if (dev && dev->dma_mask)
322			addr = *dev->dma_mask;
323	}
324
325	if (drive->queue)
326		blk_queue_bounce_limit(drive->queue, addr);
327}
328
329int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
330{
331	ide_hwif_t *hwif = drive->hwif;
332	const struct ide_port_ops *port_ops = hwif->port_ops;
333
334	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
335		return 0;
336
337	if (port_ops == NULL || port_ops->set_pio_mode == NULL)
338		return -1;
339
340	/*
341	 * TODO: temporary hack for some legacy host drivers that didn't
342	 * set transfer mode on the device in ->set_pio_mode method...
343	 */
344	if (port_ops->set_dma_mode == NULL) {
345		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
346		return 0;
347	}
348
349	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
350		if (ide_config_drive_speed(drive, mode))
351			return -1;
352		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
353		return 0;
354	} else {
355		port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
356		return ide_config_drive_speed(drive, mode);
357	}
358}
359
360int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
361{
362	ide_hwif_t *hwif = drive->hwif;
363	const struct ide_port_ops *port_ops = hwif->port_ops;
364
365	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
366		return 0;
367
368	if (port_ops == NULL || port_ops->set_dma_mode == NULL)
369		return -1;
370
371	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
372		if (ide_config_drive_speed(drive, mode))
373			return -1;
374		port_ops->set_dma_mode(drive, mode);
375		return 0;
376	} else {
377		port_ops->set_dma_mode(drive, mode);
378		return ide_config_drive_speed(drive, mode);
379	}
380}
381
382EXPORT_SYMBOL_GPL(ide_set_dma_mode);
383
384/**
385 *	ide_set_xfer_rate	-	set transfer rate
386 *	@drive: drive to set
387 *	@rate: speed to attempt to set
388 *
389 *	General helper for setting the speed of an IDE device. This
390 *	function knows about user enforced limits from the configuration
391 *	which ->set_pio_mode/->set_dma_mode does not.
392 */
393
394int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
395{
396	ide_hwif_t *hwif = drive->hwif;
397	const struct ide_port_ops *port_ops = hwif->port_ops;
398
399	if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
400	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
401		return -1;
402
403	rate = ide_rate_filter(drive, rate);
404
405	if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
406		return ide_set_pio_mode(drive, rate);
407
408	/*
409	 * TODO: transfer modes 0x00-0x07 passed from the user-space are
410	 * currently handled here which needs fixing (please note that such
411	 * case could happen iff the transfer mode has already been set on
412	 * the device by ide-proc.c::set_xfer_rate()).
413	 */
414	if (rate < XFER_PIO_0) {
415		if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
416			return ide_set_dma_mode(drive, rate);
417		else
418			return ide_config_drive_speed(drive, rate);
419	}
420
421	return ide_set_dma_mode(drive, rate);
422}
423
424static void ide_dump_opcode(ide_drive_t *drive)
425{
426	struct request *rq;
427	ide_task_t *task = NULL;
428
429	spin_lock(&ide_lock);
430	rq = NULL;
431	if (HWGROUP(drive))
432		rq = HWGROUP(drive)->rq;
433	spin_unlock(&ide_lock);
434	if (!rq)
435		return;
436
437	if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
438		task = rq->special;
439
440	printk("ide: failed opcode was: ");
441	if (task == NULL)
442		printk(KERN_CONT "unknown\n");
443	else
444		printk(KERN_CONT "0x%02x\n", task->tf.command);
445}
446
447u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
448{
449	u32 high, low;
450
451	if (lba48)
452		high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
453			tf->hob_lbal;
454	else
455		high = tf->device & 0xf;
456	low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
457
458	return ((u64)high << 24) | low;
459}
460EXPORT_SYMBOL_GPL(ide_get_lba_addr);
461
462static void ide_dump_sector(ide_drive_t *drive)
463{
464	ide_task_t task;
465	struct ide_taskfile *tf = &task.tf;
466	int lba48 = (drive->addressing == 1) ? 1 : 0;
467
468	memset(&task, 0, sizeof(task));
469	if (lba48)
470		task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
471				IDE_TFLAG_LBA48;
472	else
473		task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
474
475	drive->hwif->tf_read(drive, &task);
476
477	if (lba48 || (tf->device & ATA_LBA))
478		printk(", LBAsect=%llu",
479			(unsigned long long)ide_get_lba_addr(tf, lba48));
480	else
481		printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
482					 tf->device & 0xf, tf->lbal);
483}
484
485static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
486{
487	printk("{ ");
488	if (err & ABRT_ERR)	printk("DriveStatusError ");
489	if (err & ICRC_ERR)
490		printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
491	if (err & ECC_ERR)	printk("UncorrectableError ");
492	if (err & ID_ERR)	printk("SectorIdNotFound ");
493	if (err & TRK0_ERR)	printk("TrackZeroNotFound ");
494	if (err & MARK_ERR)	printk("AddrMarkNotFound ");
495	printk("}");
496	if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
497	    (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
498		ide_dump_sector(drive);
499		if (HWGROUP(drive) && HWGROUP(drive)->rq)
500			printk(", sector=%llu",
501			       (unsigned long long)HWGROUP(drive)->rq->sector);
502	}
503	printk("\n");
504}
505
506static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
507{
508	printk("{ ");
509	if (err & ILI_ERR)	printk("IllegalLengthIndication ");
510	if (err & EOM_ERR)	printk("EndOfMedia ");
511	if (err & ABRT_ERR)	printk("AbortedCommand ");
512	if (err & MCR_ERR)	printk("MediaChangeRequested ");
513	if (err & LFS_ERR)	printk("LastFailedSense=0x%02x ",
514				       (err & LFS_ERR) >> 4);
515	printk("}\n");
516}
517
518/**
519 *	ide_dump_status		-	translate ATA/ATAPI error
520 *	@drive: drive that status applies to
521 *	@msg: text message to print
522 *	@stat: status byte to decode
523 *
524 *	Error reporting, in human readable form (luxurious, but a memory hog).
525 *	Combines the drive name, message and status byte to provide a
526 *	user understandable explanation of the device error.
527 */
528
529u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
530{
531	unsigned long flags;
532	u8 err = 0;
533
534	local_irq_save(flags);
535	printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
536	if (stat & BUSY_STAT)
537		printk("Busy ");
538	else {
539		if (stat & READY_STAT)	printk("DriveReady ");
540		if (stat & WRERR_STAT)	printk("DeviceFault ");
541		if (stat & SEEK_STAT)	printk("SeekComplete ");
542		if (stat & DRQ_STAT)	printk("DataRequest ");
543		if (stat & ECC_STAT)	printk("CorrectedError ");
544		if (stat & INDEX_STAT)	printk("Index ");
545		if (stat & ERR_STAT)	printk("Error ");
546	}
547	printk("}\n");
548	if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
549		err = ide_read_error(drive);
550		printk("%s: %s: error=0x%02x ", drive->name, msg, err);
551		if (drive->media == ide_disk)
552			ide_dump_ata_error(drive, err);
553		else
554			ide_dump_atapi_error(drive, err);
555	}
556	ide_dump_opcode(drive);
557	local_irq_restore(flags);
558	return err;
559}
560
561EXPORT_SYMBOL(ide_dump_status);
562