More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / ide-iops.c
blob06d6292cf70f3aaa5c73d8c3780ecb908891dd49
1 /*
2 * linux/drivers/ide/ide-iops.c Version 0.37 Mar 05, 2003
4 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
7 */
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/mm.h>
16 #include <linux/interrupt.h>
17 #include <linux/major.h>
18 #include <linux/errno.h>
19 #include <linux/genhd.h>
20 #include <linux/blkpg.h>
21 #include <linux/slab.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/hdreg.h>
25 #include <linux/ide.h>
27 #include <asm/byteorder.h>
28 #include <asm/irq.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/bitops.h>
34 * IDE operator we assign to an unplugged device so that
35 * we don't trash new hardware assigned the same resources
38 static u8 ide_unplugged_inb (unsigned long port)
40 return 0xFF;
43 static u16 ide_unplugged_inw (unsigned long port)
45 return 0xFFFF;
48 static void ide_unplugged_insw (unsigned long port, void *addr, u32 count)
52 static u32 ide_unplugged_inl (unsigned long port)
54 return 0xFFFFFFFF;
57 static void ide_unplugged_insl (unsigned long port, void *addr, u32 count)
61 static void ide_unplugged_outb (u8 val, unsigned long port)
65 static void ide_unplugged_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
69 static void ide_unplugged_outw (u16 val, unsigned long port)
73 static void ide_unplugged_outsw (unsigned long port, void *addr, u32 count)
77 static void ide_unplugged_outl (u32 val, unsigned long port)
81 static void ide_unplugged_outsl (unsigned long port, void *addr, u32 count)
85 void unplugged_hwif_iops (ide_hwif_t *hwif)
87 hwif->OUTB = ide_unplugged_outb;
88 hwif->OUTBSYNC = ide_unplugged_outbsync;
89 hwif->OUTW = ide_unplugged_outw;
90 hwif->OUTL = ide_unplugged_outl;
91 hwif->OUTSW = ide_unplugged_outsw;
92 hwif->OUTSL = ide_unplugged_outsl;
93 hwif->INB = ide_unplugged_inb;
94 hwif->INW = ide_unplugged_inw;
95 hwif->INL = ide_unplugged_inl;
96 hwif->INSW = ide_unplugged_insw;
97 hwif->INSL = ide_unplugged_insl;
100 EXPORT_SYMBOL(unplugged_hwif_iops);
103 * Conventional PIO operations for ATA devices
106 static u8 ide_inb (unsigned long port)
108 return (u8) inb(port);
111 static u16 ide_inw (unsigned long port)
113 return (u16) inw(port);
116 static void ide_insw (unsigned long port, void *addr, u32 count)
118 return insw(port, addr, count);
121 static u32 ide_inl (unsigned long port)
123 return (u32) inl(port);
126 static void ide_insl (unsigned long port, void *addr, u32 count)
128 insl(port, addr, count);
131 static void ide_outb (u8 val, unsigned long port)
133 outb(val, port);
136 static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
138 outb(addr, port);
141 static void ide_outw (u16 val, unsigned long port)
143 outw(val, port);
146 static void ide_outsw (unsigned long port, void *addr, u32 count)
148 outsw(port, addr, count);
151 static void ide_outl (u32 val, unsigned long port)
153 outl(val, port);
156 static void ide_outsl (unsigned long port, void *addr, u32 count)
158 outsl(port, addr, count);
161 void default_hwif_iops (ide_hwif_t *hwif)
163 hwif->OUTB = ide_outb;
164 hwif->OUTBSYNC = ide_outbsync;
165 hwif->OUTW = ide_outw;
166 hwif->OUTL = ide_outl;
167 hwif->OUTSW = ide_outsw;
168 hwif->OUTSL = ide_outsl;
169 hwif->INB = ide_inb;
170 hwif->INW = ide_inw;
171 hwif->INL = ide_inl;
172 hwif->INSW = ide_insw;
173 hwif->INSL = ide_insl;
176 EXPORT_SYMBOL(default_hwif_iops);
179 * MMIO operations, typically used for SATA controllers
182 static u8 ide_mm_inb (unsigned long port)
184 return (u8) readb(port);
187 static u16 ide_mm_inw (unsigned long port)
189 return (u16) readw(port);
192 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
194 __ide_mm_insw(port, addr, count);
197 static u32 ide_mm_inl (unsigned long port)
199 return (u32) readl(port);
202 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
204 __ide_mm_insl(port, addr, count);
207 static void ide_mm_outb (u8 value, unsigned long port)
209 writeb(value, port);
212 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
214 writeb(value, port);
217 static void ide_mm_outw (u16 value, unsigned long port)
219 writew(value, port);
222 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
224 __ide_mm_outsw(port, addr, count);
227 static void ide_mm_outl (u32 value, unsigned long port)
229 writel(value, port);
232 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
234 __ide_mm_outsl(port, addr, count);
237 void default_hwif_mmiops (ide_hwif_t *hwif)
239 hwif->OUTB = ide_mm_outb;
240 /* Most systems will need to override OUTBSYNC, alas however
241 this one is controller specific! */
242 hwif->OUTBSYNC = ide_mm_outbsync;
243 hwif->OUTW = ide_mm_outw;
244 hwif->OUTL = ide_mm_outl;
245 hwif->OUTSW = ide_mm_outsw;
246 hwif->OUTSL = ide_mm_outsl;
247 hwif->INB = ide_mm_inb;
248 hwif->INW = ide_mm_inw;
249 hwif->INL = ide_mm_inl;
250 hwif->INSW = ide_mm_insw;
251 hwif->INSL = ide_mm_insl;
254 EXPORT_SYMBOL(default_hwif_mmiops);
256 void default_hwif_transport (ide_hwif_t *hwif)
258 hwif->ata_input_data = ata_input_data;
259 hwif->ata_output_data = ata_output_data;
260 hwif->atapi_input_bytes = atapi_input_bytes;
261 hwif->atapi_output_bytes = atapi_output_bytes;
264 EXPORT_SYMBOL(default_hwif_transport);
266 u32 ide_read_24 (ide_drive_t *drive)
268 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
269 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
270 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
271 return (hcyl<<16)|(lcyl<<8)|sect;
274 EXPORT_SYMBOL(ide_read_24);
276 void SELECT_DRIVE (ide_drive_t *drive)
278 if (HWIF(drive)->selectproc)
279 HWIF(drive)->selectproc(drive);
280 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
283 EXPORT_SYMBOL(SELECT_DRIVE);
285 void SELECT_INTERRUPT (ide_drive_t *drive)
287 if (HWIF(drive)->intrproc)
288 HWIF(drive)->intrproc(drive);
289 else
290 HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
293 EXPORT_SYMBOL(SELECT_INTERRUPT);
295 void SELECT_MASK (ide_drive_t *drive, int mask)
297 if (HWIF(drive)->maskproc)
298 HWIF(drive)->maskproc(drive, mask);
301 EXPORT_SYMBOL(SELECT_MASK);
303 void QUIRK_LIST (ide_drive_t *drive)
305 if (HWIF(drive)->quirkproc)
306 drive->quirk_list = HWIF(drive)->quirkproc(drive);
309 EXPORT_SYMBOL(QUIRK_LIST);
312 * Some localbus EIDE interfaces require a special access sequence
313 * when using 32-bit I/O instructions to transfer data. We call this
314 * the "vlb_sync" sequence, which consists of three successive reads
315 * of the sector count register location, with interrupts disabled
316 * to ensure that the reads all happen together.
318 void ata_vlb_sync (ide_drive_t *drive, unsigned long port)
320 (void) HWIF(drive)->INB(port);
321 (void) HWIF(drive)->INB(port);
322 (void) HWIF(drive)->INB(port);
325 EXPORT_SYMBOL(ata_vlb_sync);
328 * This is used for most PIO data transfers *from* the IDE interface
330 void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
332 ide_hwif_t *hwif = HWIF(drive);
333 u8 io_32bit = drive->io_32bit;
335 if (io_32bit) {
336 if (io_32bit & 2) {
337 unsigned long flags;
338 local_irq_save(flags);
339 ata_vlb_sync(drive, IDE_NSECTOR_REG);
340 hwif->INSL(IDE_DATA_REG, buffer, wcount);
341 local_irq_restore(flags);
342 } else
343 hwif->INSL(IDE_DATA_REG, buffer, wcount);
344 } else {
345 hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
349 EXPORT_SYMBOL(ata_input_data);
352 * This is used for most PIO data transfers *to* the IDE interface
354 void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
356 ide_hwif_t *hwif = HWIF(drive);
357 u8 io_32bit = drive->io_32bit;
359 if (io_32bit) {
360 if (io_32bit & 2) {
361 unsigned long flags;
362 local_irq_save(flags);
363 ata_vlb_sync(drive, IDE_NSECTOR_REG);
364 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
365 local_irq_restore(flags);
366 } else
367 hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
368 } else {
369 hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
373 EXPORT_SYMBOL(ata_output_data);
376 * The following routines are mainly used by the ATAPI drivers.
378 * These routines will round up any request for an odd number of bytes,
379 * so if an odd bytecount is specified, be sure that there's at least one
380 * extra byte allocated for the buffer.
383 void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
385 ide_hwif_t *hwif = HWIF(drive);
387 ++bytecount;
388 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
389 if (MACH_IS_ATARI || MACH_IS_Q40) {
390 /* Atari has a byte-swapped IDE interface */
391 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
392 return;
394 #endif /* CONFIG_ATARI || CONFIG_Q40 */
395 hwif->ata_input_data(drive, buffer, bytecount / 4);
396 if ((bytecount & 0x03) >= 2)
397 hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
400 EXPORT_SYMBOL(atapi_input_bytes);
402 void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
404 ide_hwif_t *hwif = HWIF(drive);
406 ++bytecount;
407 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
408 if (MACH_IS_ATARI || MACH_IS_Q40) {
409 /* Atari has a byte-swapped IDE interface */
410 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
411 return;
413 #endif /* CONFIG_ATARI || CONFIG_Q40 */
414 hwif->ata_output_data(drive, buffer, bytecount / 4);
415 if ((bytecount & 0x03) >= 2)
416 hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
419 EXPORT_SYMBOL(atapi_output_bytes);
422 * Beginning of Taskfile OPCODE Library and feature sets.
424 void ide_fix_driveid (struct hd_driveid *id)
426 #ifndef __LITTLE_ENDIAN
427 # ifdef __BIG_ENDIAN
428 int i;
429 u16 *stringcast;
431 id->config = __le16_to_cpu(id->config);
432 id->cyls = __le16_to_cpu(id->cyls);
433 id->reserved2 = __le16_to_cpu(id->reserved2);
434 id->heads = __le16_to_cpu(id->heads);
435 id->track_bytes = __le16_to_cpu(id->track_bytes);
436 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
437 id->sectors = __le16_to_cpu(id->sectors);
438 id->vendor0 = __le16_to_cpu(id->vendor0);
439 id->vendor1 = __le16_to_cpu(id->vendor1);
440 id->vendor2 = __le16_to_cpu(id->vendor2);
441 stringcast = (u16 *)&id->serial_no[0];
442 for (i = 0; i < (20/2); i++)
443 stringcast[i] = __le16_to_cpu(stringcast[i]);
444 id->buf_type = __le16_to_cpu(id->buf_type);
445 id->buf_size = __le16_to_cpu(id->buf_size);
446 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
447 stringcast = (u16 *)&id->fw_rev[0];
448 for (i = 0; i < (8/2); i++)
449 stringcast[i] = __le16_to_cpu(stringcast[i]);
450 stringcast = (u16 *)&id->model[0];
451 for (i = 0; i < (40/2); i++)
452 stringcast[i] = __le16_to_cpu(stringcast[i]);
453 id->dword_io = __le16_to_cpu(id->dword_io);
454 id->reserved50 = __le16_to_cpu(id->reserved50);
455 id->field_valid = __le16_to_cpu(id->field_valid);
456 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
457 id->cur_heads = __le16_to_cpu(id->cur_heads);
458 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
459 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
460 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
461 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
462 id->dma_1word = __le16_to_cpu(id->dma_1word);
463 id->dma_mword = __le16_to_cpu(id->dma_mword);
464 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
465 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
466 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
467 id->eide_pio = __le16_to_cpu(id->eide_pio);
468 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
469 for (i = 0; i < 2; ++i)
470 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
471 for (i = 0; i < 4; ++i)
472 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
473 id->queue_depth = __le16_to_cpu(id->queue_depth);
474 for (i = 0; i < 4; ++i)
475 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
476 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
477 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
478 id->command_set_1 = __le16_to_cpu(id->command_set_1);
479 id->command_set_2 = __le16_to_cpu(id->command_set_2);
480 id->cfsse = __le16_to_cpu(id->cfsse);
481 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
482 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
483 id->csf_default = __le16_to_cpu(id->csf_default);
484 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
485 id->trseuc = __le16_to_cpu(id->trseuc);
486 id->trsEuc = __le16_to_cpu(id->trsEuc);
487 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
488 id->mprc = __le16_to_cpu(id->mprc);
489 id->hw_config = __le16_to_cpu(id->hw_config);
490 id->acoustic = __le16_to_cpu(id->acoustic);
491 id->msrqs = __le16_to_cpu(id->msrqs);
492 id->sxfert = __le16_to_cpu(id->sxfert);
493 id->sal = __le16_to_cpu(id->sal);
494 id->spg = __le32_to_cpu(id->spg);
495 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
496 for (i = 0; i < 22; i++)
497 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
498 id->last_lun = __le16_to_cpu(id->last_lun);
499 id->word127 = __le16_to_cpu(id->word127);
500 id->dlf = __le16_to_cpu(id->dlf);
501 id->csfo = __le16_to_cpu(id->csfo);
502 for (i = 0; i < 26; i++)
503 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
504 id->word156 = __le16_to_cpu(id->word156);
505 for (i = 0; i < 3; i++)
506 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
507 id->cfa_power = __le16_to_cpu(id->cfa_power);
508 for (i = 0; i < 14; i++)
509 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
510 for (i = 0; i < 31; i++)
511 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
512 for (i = 0; i < 48; i++)
513 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
514 id->integrity_word = __le16_to_cpu(id->integrity_word);
515 # else
516 # error "Please fix <asm/byteorder.h>"
517 # endif
518 #endif
521 EXPORT_SYMBOL(ide_fix_driveid);
523 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
525 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
527 if (byteswap) {
528 /* convert from big-endian to host byte order */
529 for (p = end ; p != s;) {
530 unsigned short *pp = (unsigned short *) (p -= 2);
531 *pp = ntohs(*pp);
534 /* strip leading blanks */
535 while (s != end && *s == ' ')
536 ++s;
537 /* compress internal blanks and strip trailing blanks */
538 while (s != end && *s) {
539 if (*s++ != ' ' || (s != end && *s && *s != ' '))
540 *p++ = *(s-1);
542 /* wipe out trailing garbage */
543 while (p != end)
544 *p++ = '\0';
547 EXPORT_SYMBOL(ide_fixstring);
550 * Needed for PCI irq sharing
552 int drive_is_ready (ide_drive_t *drive)
554 ide_hwif_t *hwif = HWIF(drive);
555 u8 stat = 0;
557 if (drive->waiting_for_dma)
558 return hwif->ide_dma_test_irq(drive);
560 #if 0
561 /* need to guarantee 400ns since last command was issued */
562 udelay(1);
563 #endif
565 #ifdef CONFIG_IDEPCI_SHARE_IRQ
567 * We do a passive status test under shared PCI interrupts on
568 * cards that truly share the ATA side interrupt, but may also share
569 * an interrupt with another pci card/device. We make no assumptions
570 * about possible isa-pnp and pci-pnp issues yet.
572 if (IDE_CONTROL_REG)
573 stat = hwif->INB(IDE_ALTSTATUS_REG);
574 else
575 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
576 /* Note: this may clear a pending IRQ!! */
577 stat = hwif->INB(IDE_STATUS_REG);
579 if (stat & BUSY_STAT)
580 /* drive busy: definitely not interrupting */
581 return 0;
583 /* drive ready: *might* be interrupting */
584 return 1;
587 EXPORT_SYMBOL(drive_is_ready);
590 * Global for All, and taken from ide-pmac.c. Can be called
591 * with spinlock held & IRQs disabled, so don't schedule !
593 int wait_for_ready (ide_drive_t *drive, int timeout)
595 ide_hwif_t *hwif = HWIF(drive);
596 u8 stat = 0;
598 while(--timeout) {
599 stat = hwif->INB(IDE_STATUS_REG);
600 if (!(stat & BUSY_STAT)) {
601 if (drive->ready_stat == 0)
602 break;
603 else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
604 break;
606 mdelay(1);
608 if ((stat & ERR_STAT) || timeout <= 0) {
609 if (stat & ERR_STAT) {
610 printk(KERN_ERR "%s: wait_for_ready, "
611 "error status: %x\n", drive->name, stat);
613 return 1;
615 return 0;
618 EXPORT_SYMBOL(wait_for_ready);
621 * This routine busy-waits for the drive status to be not "busy".
622 * It then checks the status for all of the "good" bits and none
623 * of the "bad" bits, and if all is okay it returns 0. All other
624 * cases return 1 after invoking ide_error() -- caller should just return.
626 * This routine should get fixed to not hog the cpu during extra long waits..
627 * That could be done by busy-waiting for the first jiffy or two, and then
628 * setting a timer to wake up at half second intervals thereafter,
629 * until timeout is achieved, before timing out.
631 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
633 ide_hwif_t *hwif = HWIF(drive);
634 u8 stat;
635 int i;
636 unsigned long flags;
638 /* bail early if we've exceeded max_failures */
639 if (drive->max_failures && (drive->failures > drive->max_failures)) {
640 *startstop = ide_stopped;
641 return 1;
644 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
645 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
646 local_irq_set(flags);
647 timeout += jiffies;
648 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
649 if (time_after(jiffies, timeout)) {
650 local_irq_restore(flags);
651 *startstop = DRIVER(drive)->error(drive, "status timeout", stat);
652 return 1;
655 local_irq_restore(flags);
658 * Allow status to settle, then read it again.
659 * A few rare drives vastly violate the 400ns spec here,
660 * so we'll wait up to 10usec for a "good" status
661 * rather than expensively fail things immediately.
662 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
664 for (i = 0; i < 10; i++) {
665 udelay(1);
666 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
667 return 0;
669 *startstop = DRIVER(drive)->error(drive, "status error", stat);
670 return 1;
673 EXPORT_SYMBOL(ide_wait_stat);
676 * All hosts that use the 80c ribbon must use!
677 * The name is derived from upper byte of word 93 and the 80c ribbon.
679 u8 eighty_ninty_three (ide_drive_t *drive)
681 #if 0
682 if (!HWIF(drive)->udma_four)
683 return 0;
685 if (drive->id->major_rev_num) {
686 int hssbd = 0;
687 int i;
689 * Determine highest Supported SPEC
691 for (i=1; i<=15; i++)
692 if (drive->id->major_rev_num & (1<<i))
693 hssbd++;
695 switch (hssbd) {
696 case 7:
697 case 6:
698 case 5:
699 /* ATA-4 and older do not support above Ultra 33 */
700 default:
701 return 0;
705 return ((u8) (
706 #ifndef CONFIG_IDEDMA_IVB
707 (drive->id->hw_config & 0x4000) &&
708 #endif /* CONFIG_IDEDMA_IVB */
709 (drive->id->hw_config & 0x6000)) ? 1 : 0);
711 #else
713 return ((u8) ((HWIF(drive)->udma_four) &&
714 #ifndef CONFIG_IDEDMA_IVB
715 (drive->id->hw_config & 0x4000) &&
716 #endif /* CONFIG_IDEDMA_IVB */
717 (drive->id->hw_config & 0x6000)) ? 1 : 0);
718 #endif
721 EXPORT_SYMBOL(eighty_ninty_three);
723 int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
725 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
726 (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
727 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
728 #ifndef CONFIG_IDEDMA_IVB
729 if ((drive->id->hw_config & 0x6000) == 0) {
730 #else /* !CONFIG_IDEDMA_IVB */
731 if (((drive->id->hw_config & 0x2000) == 0) ||
732 ((drive->id->hw_config & 0x4000) == 0)) {
733 #endif /* CONFIG_IDEDMA_IVB */
734 printk("%s: Speed warnings UDMA 3/4/5 is not "
735 "functional.\n", drive->name);
736 return 1;
738 if (!HWIF(drive)->udma_four) {
739 printk("%s: Speed warnings UDMA 3/4/5 is not "
740 "functional.\n",
741 HWIF(drive)->name);
742 return 1;
745 return 0;
748 EXPORT_SYMBOL(ide_ata66_check);
751 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
752 * 1 : Safe to update drive->id DMA registers.
753 * 0 : OOPs not allowed.
755 int set_transfer (ide_drive_t *drive, ide_task_t *args)
757 if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
758 (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
759 (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
760 (drive->id->dma_ultra ||
761 drive->id->dma_mword ||
762 drive->id->dma_1word))
763 return 1;
765 return 0;
768 EXPORT_SYMBOL(set_transfer);
770 u8 ide_auto_reduce_xfer (ide_drive_t *drive)
772 if (!drive->crc_count)
773 return drive->current_speed;
774 drive->crc_count = 0;
776 switch(drive->current_speed) {
777 case XFER_UDMA_7: return XFER_UDMA_6;
778 case XFER_UDMA_6: return XFER_UDMA_5;
779 case XFER_UDMA_5: return XFER_UDMA_4;
780 case XFER_UDMA_4: return XFER_UDMA_3;
781 case XFER_UDMA_3: return XFER_UDMA_2;
782 case XFER_UDMA_2: return XFER_UDMA_1;
783 case XFER_UDMA_1: return XFER_UDMA_0;
785 * OOPS we do not goto non Ultra DMA modes
786 * without iCRC's available we force
787 * the system to PIO and make the user
788 * invoke the ATA-1 ATA-2 DMA modes.
790 case XFER_UDMA_0:
791 default: return XFER_PIO_4;
795 EXPORT_SYMBOL(ide_auto_reduce_xfer);
798 * Update the
800 int ide_driveid_update (ide_drive_t *drive)
802 ide_hwif_t *hwif = HWIF(drive);
803 struct hd_driveid *id;
804 #if 0
805 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
806 if (!id)
807 return 0;
809 taskfile_lib_get_identify(drive, (char *)&id);
811 ide_fix_driveid(id);
812 if (id) {
813 drive->id->dma_ultra = id->dma_ultra;
814 drive->id->dma_mword = id->dma_mword;
815 drive->id->dma_1word = id->dma_1word;
816 /* anything more ? */
817 kfree(id);
819 return 1;
820 #else
822 * Re-read drive->id for possible DMA mode
823 * change (copied from ide-probe.c)
825 unsigned long timeout, flags;
827 SELECT_MASK(drive, 1);
828 if (IDE_CONTROL_REG)
829 hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
830 ide_delay_50ms();
831 hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
832 timeout = jiffies + WAIT_WORSTCASE;
833 do {
834 if (time_after(jiffies, timeout)) {
835 SELECT_MASK(drive, 0);
836 return 0; /* drive timed-out */
838 ide_delay_50ms(); /* give drive a breather */
839 } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
840 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
841 if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
842 SELECT_MASK(drive, 0);
843 printk("%s: CHECK for good STATUS\n", drive->name);
844 return 0;
846 local_irq_save(flags);
847 SELECT_MASK(drive, 0);
848 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
849 if (!id) {
850 local_irq_restore(flags);
851 return 0;
853 ata_input_data(drive, id, SECTOR_WORDS);
854 (void) hwif->INB(IDE_STATUS_REG); /* clear drive IRQ */
855 local_irq_enable();
856 local_irq_restore(flags);
857 ide_fix_driveid(id);
858 if (id) {
859 drive->id->dma_ultra = id->dma_ultra;
860 drive->id->dma_mword = id->dma_mword;
861 drive->id->dma_1word = id->dma_1word;
862 /* anything more ? */
863 kfree(id);
866 return 1;
867 #endif
870 EXPORT_SYMBOL(ide_driveid_update);
873 * Similar to ide_wait_stat(), except it never calls ide_error internally.
874 * This is a kludge to handle the new ide_config_drive_speed() function,
875 * and should not otherwise be used anywhere. Eventually, the tuneproc's
876 * should be updated to return ide_startstop_t, in which case we can get
877 * rid of this abomination again. :) -ml
879 * It is gone..........
881 * const char *msg == consider adding for verbose errors.
883 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
885 ide_hwif_t *hwif = HWIF(drive);
886 int i, error = 1;
887 u8 stat;
889 // while (HWGROUP(drive)->busy)
890 // ide_delay_50ms();
892 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
893 hwif->ide_dma_host_off(drive);
894 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
897 * Don't use ide_wait_cmd here - it will
898 * attempt to set_geometry and recalibrate,
899 * but for some reason these don't work at
900 * this point (lost interrupt).
903 * Select the drive, and issue the SETFEATURES command
905 disable_irq_nosync(hwif->irq);
908 * FIXME: we race against the running IRQ here if
909 * this is called from non IRQ context. If we use
910 * disable_irq() we hang on the error path. Work
911 * is needed.
914 udelay(1);
915 SELECT_DRIVE(drive);
916 SELECT_MASK(drive, 0);
917 udelay(1);
918 if (IDE_CONTROL_REG)
919 hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
920 hwif->OUTB(speed, IDE_NSECTOR_REG);
921 hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
922 hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
923 if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
924 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
925 udelay(1);
927 * Wait for drive to become non-BUSY
929 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
930 unsigned long flags, timeout;
931 local_irq_set(flags);
932 timeout = jiffies + WAIT_CMD;
933 while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
934 if (time_after(jiffies, timeout))
935 break;
937 local_irq_restore(flags);
941 * Allow status to settle, then read it again.
942 * A few rare drives vastly violate the 400ns spec here,
943 * so we'll wait up to 10usec for a "good" status
944 * rather than expensively fail things immediately.
945 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
947 for (i = 0; i < 10; i++) {
948 udelay(1);
949 if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
950 error = 0;
951 break;
955 SELECT_MASK(drive, 0);
957 enable_irq(hwif->irq);
959 if (error) {
960 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
961 return error;
964 drive->id->dma_ultra &= ~0xFF00;
965 drive->id->dma_mword &= ~0x0F00;
966 drive->id->dma_1word &= ~0x0F00;
968 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
969 if (speed >= XFER_SW_DMA_0)
970 hwif->ide_dma_host_on(drive);
971 else
972 hwif->ide_dma_off_quietly(drive);
973 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
975 switch(speed) {
976 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
977 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
978 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
979 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
980 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
981 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
982 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
983 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
984 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
985 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
986 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
987 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
988 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
989 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
990 default: break;
992 if (!drive->init_speed)
993 drive->init_speed = speed;
994 drive->current_speed = speed;
995 return error;
998 EXPORT_SYMBOL(ide_config_drive_speed);
1002 * This should get invoked any time we exit the driver to
1003 * wait for an interrupt response from a drive. handler() points
1004 * at the appropriate code to handle the next interrupt, and a
1005 * timer is started to prevent us from waiting forever in case
1006 * something goes wrong (see the ide_timer_expiry() handler later on).
1008 * See also ide_execute_command
1010 void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1011 unsigned int timeout, ide_expiry_t *expiry)
1013 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1015 if (hwgroup->handler != NULL) {
1016 printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
1017 "old=%p, new=%p\n",
1018 drive->name, hwgroup->handler, handler);
1020 hwgroup->handler = handler;
1021 hwgroup->expiry = expiry;
1022 hwgroup->timer.expires = jiffies + timeout;
1023 add_timer(&hwgroup->timer);
1026 EXPORT_SYMBOL(__ide_set_handler);
1028 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
1029 unsigned int timeout, ide_expiry_t *expiry)
1031 unsigned long flags;
1032 spin_lock_irqsave(&ide_lock, flags);
1033 __ide_set_handler(drive, handler, timeout, expiry);
1034 spin_unlock_irqrestore(&ide_lock, flags);
1037 EXPORT_SYMBOL(ide_set_handler);
1040 * ide_execute_command - execute an IDE command
1041 * @drive: IDE drive to issue the command against
1042 * @command: command byte to write
1043 * @handler: handler for next phase
1044 * @timeout: timeout for command
1045 * @expiry: handler to run on timeout
1047 * Helper function to issue an IDE command. This handles the
1048 * atomicity requirements, command timing and ensures that the
1049 * handler and IRQ setup do not race. All IDE command kick off
1050 * should go via this function or do equivalent locking.
1053 void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
1055 unsigned long flags;
1056 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1057 ide_hwif_t *hwif = HWIF(drive);
1059 spin_lock_irqsave(&ide_lock, flags);
1061 if(hwgroup->handler)
1062 BUG();
1063 hwgroup->handler = handler;
1064 hwgroup->expiry = expiry;
1065 hwgroup->timer.expires = jiffies + timeout;
1066 add_timer(&hwgroup->timer);
1067 hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
1068 /* Drive takes 400nS to respond, we must avoid the IRQ being
1069 serviced before that.
1071 FIXME: we could skip this delay with care on non shared
1072 devices
1074 ndelay(400);
1075 spin_unlock_irqrestore(&ide_lock, flags);
1078 EXPORT_SYMBOL(ide_execute_command);
1081 /* needed below */
1082 static ide_startstop_t do_reset1 (ide_drive_t *, int);
1085 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1086 * during an atapi drive reset operation. If the drive has not yet responded,
1087 * and we have not yet hit our maximum waiting time, then the timer is restarted
1088 * for another 50ms.
1090 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
1092 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1093 ide_hwif_t *hwif = HWIF(drive);
1094 u8 stat;
1096 SELECT_DRIVE(drive);
1097 udelay (10);
1099 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1100 printk("%s: ATAPI reset complete\n", drive->name);
1101 } else {
1102 if (time_before(jiffies, hwgroup->poll_timeout)) {
1103 if (HWGROUP(drive)->handler != NULL)
1104 BUG();
1105 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1106 /* continue polling */
1107 return ide_started;
1109 /* end of polling */
1110 hwgroup->poll_timeout = 0;
1111 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1112 drive->name, stat);
1113 /* do it the old fashioned way */
1114 return do_reset1(drive, 1);
1116 /* done polling */
1117 hwgroup->poll_timeout = 0;
1118 return ide_stopped;
1122 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1123 * during an ide reset operation. If the drives have not yet responded,
1124 * and we have not yet hit our maximum waiting time, then the timer is restarted
1125 * for another 50ms.
1127 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1129 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1130 ide_hwif_t *hwif = HWIF(drive);
1131 u8 tmp;
1133 if (hwif->reset_poll != NULL) {
1134 if (hwif->reset_poll(drive)) {
1135 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1136 hwif->name, drive->name);
1137 return ide_stopped;
1141 if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
1142 if (time_before(jiffies, hwgroup->poll_timeout)) {
1143 if (HWGROUP(drive)->handler != NULL)
1144 BUG();
1145 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1146 /* continue polling */
1147 return ide_started;
1149 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1150 drive->failures++;
1151 } else {
1152 printk("%s: reset: ", hwif->name);
1153 if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
1154 printk("success\n");
1155 drive->failures = 0;
1156 } else {
1157 drive->failures++;
1158 #if FANCY_STATUS_DUMPS
1159 printk("master: ");
1160 switch (tmp & 0x7f) {
1161 case 1: printk("passed");
1162 break;
1163 case 2: printk("formatter device error");
1164 break;
1165 case 3: printk("sector buffer error");
1166 break;
1167 case 4: printk("ECC circuitry error");
1168 break;
1169 case 5: printk("controlling MPU error");
1170 break;
1171 default:printk("error (0x%02x?)", tmp);
1173 if (tmp & 0x80)
1174 printk("; slave: failed");
1175 printk("\n");
1176 #else
1177 printk("failed\n");
1178 #endif /* FANCY_STATUS_DUMPS */
1181 hwgroup->poll_timeout = 0; /* done polling */
1182 return ide_stopped;
1185 void check_dma_crc (ide_drive_t *drive)
1187 if (drive->crc_count) {
1188 (void) HWIF(drive)->ide_dma_off_quietly(drive);
1189 ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
1190 if (drive->current_speed >= XFER_SW_DMA_0)
1191 (void) HWIF(drive)->ide_dma_on(drive);
1192 } else {
1193 (void) HWIF(drive)->ide_dma_off(drive);
1197 void pre_reset (ide_drive_t *drive)
1199 DRIVER(drive)->pre_reset(drive);
1201 if (!drive->keep_settings) {
1202 if (drive->using_dma) {
1203 check_dma_crc(drive);
1204 } else {
1205 drive->unmask = 0;
1206 drive->io_32bit = 0;
1208 return;
1210 if (drive->using_dma)
1211 check_dma_crc(drive);
1213 if (HWIF(drive)->pre_reset != NULL)
1214 HWIF(drive)->pre_reset(drive);
1219 * do_reset1() attempts to recover a confused drive by resetting it.
1220 * Unfortunately, resetting a disk drive actually resets all devices on
1221 * the same interface, so it can really be thought of as resetting the
1222 * interface rather than resetting the drive.
1224 * ATAPI devices have their own reset mechanism which allows them to be
1225 * individually reset without clobbering other devices on the same interface.
1227 * Unfortunately, the IDE interface does not generate an interrupt to let
1228 * us know when the reset operation has finished, so we must poll for this.
1229 * Equally poor, though, is the fact that this may a very long time to complete,
1230 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1231 * we set a timer to poll at 50ms intervals.
1233 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1235 unsigned int unit;
1236 unsigned long flags;
1237 ide_hwif_t *hwif;
1238 ide_hwgroup_t *hwgroup;
1240 spin_lock_irqsave(&ide_lock, flags);
1241 hwif = HWIF(drive);
1242 hwgroup = HWGROUP(drive);
1244 /* We must not reset with running handlers */
1245 if(hwgroup->handler != NULL)
1246 BUG();
1248 /* For an ATAPI device, first try an ATAPI SRST. */
1249 if (drive->media != ide_disk && !do_not_try_atapi) {
1250 pre_reset(drive);
1251 SELECT_DRIVE(drive);
1252 udelay (20);
1253 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
1254 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1255 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1256 spin_unlock_irqrestore(&ide_lock, flags);
1257 return ide_started;
1261 * First, reset any device state data we were maintaining
1262 * for any of the drives on this interface.
1264 for (unit = 0; unit < MAX_DRIVES; ++unit)
1265 pre_reset(&hwif->drives[unit]);
1267 #if OK_TO_RESET_CONTROLLER
1268 if (!IDE_CONTROL_REG) {
1269 spin_unlock_irqrestore(&ide_lock, flags);
1270 return ide_stopped;
1274 * Note that we also set nIEN while resetting the device,
1275 * to mask unwanted interrupts from the interface during the reset.
1276 * However, due to the design of PC hardware, this will cause an
1277 * immediate interrupt due to the edge transition it produces.
1278 * This single interrupt gives us a "fast poll" for drives that
1279 * recover from reset very quickly, saving us the first 50ms wait time.
1281 /* set SRST and nIEN */
1282 hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
1283 /* more than enough time */
1284 udelay(10);
1285 if (drive->quirk_list == 2) {
1286 /* clear SRST and nIEN */
1287 hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
1288 } else {
1289 /* clear SRST, leave nIEN */
1290 hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
1292 /* more than enough time */
1293 udelay(10);
1294 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1295 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1298 * Some weird controller like resetting themselves to a strange
1299 * state when the disks are reset this way. At least, the Winbond
1300 * 553 documentation says that
1302 if (hwif->resetproc != NULL) {
1303 hwif->resetproc(drive);
1306 #endif /* OK_TO_RESET_CONTROLLER */
1308 spin_unlock_irqrestore(&ide_lock, flags);
1309 return ide_started;
1313 * ide_do_reset() is the entry point to the drive/interface reset code.
1316 ide_startstop_t ide_do_reset (ide_drive_t *drive)
1318 return do_reset1(drive, 0);
1321 EXPORT_SYMBOL(ide_do_reset);
1324 * ide_wait_not_busy() waits for the currently selected device on the hwif
1325 * to report a non-busy status, see comments in probe_hwif().
1327 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1329 u8 stat = 0;
1331 while(timeout--) {
1333 * Turn this into a schedule() sleep once I'm sure
1334 * about locking issues (2.5 work ?).
1336 mdelay(1);
1337 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1338 if ((stat & BUSY_STAT) == 0)
1339 return 0;
1341 * Assume a value of 0xff means nothing is connected to
1342 * the interface and it doesn't implement the pull-down
1343 * resistor on D7.
1345 if (stat == 0xff)
1346 return -ENODEV;
1348 return -EBUSY;
1351 EXPORT_SYMBOL_GPL(ide_wait_not_busy);