Initial revision
[linux-2.6.git] / drivers / ide / ide-iops.c
index eb94342..d48ebc0 100644 (file)
 #include <linux/delay.h>
 #include <linux/hdreg.h>
 #include <linux/ide.h>
+#include <linux/bitops.h>
 
 #include <asm/byteorder.h>
 #include <asm/irq.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
-#include <asm/bitops.h>
 
 /*
  *     Conventional PIO operations for ATA devices
@@ -46,7 +46,7 @@ static u16 ide_inw (unsigned long port)
 
 static void ide_insw (unsigned long port, void *addr, u32 count)
 {
-       return insw(port, addr, count);
+       insw(port, addr, count);
 }
 
 static u32 ide_inl (unsigned long port)
@@ -112,57 +112,57 @@ EXPORT_SYMBOL(default_hwif_iops);
 
 static u8 ide_mm_inb (unsigned long port)
 {
-       return (u8) readb(port);
+       return (u8) readb((void __iomem *) port);
 }
 
 static u16 ide_mm_inw (unsigned long port)
 {
-       return (u16) readw(port);
+       return (u16) readw((void __iomem *) port);
 }
 
 static void ide_mm_insw (unsigned long port, void *addr, u32 count)
 {
-       __ide_mm_insw(port, addr, count);
+       __ide_mm_insw((void __iomem *) port, addr, count);
 }
 
 static u32 ide_mm_inl (unsigned long port)
 {
-       return (u32) readl(port);
+       return (u32) readl((void __iomem *) port);
 }
 
 static void ide_mm_insl (unsigned long port, void *addr, u32 count)
 {
-       __ide_mm_insl(port, addr, count);
+       __ide_mm_insl((void __iomem *) port, addr, count);
 }
 
 static void ide_mm_outb (u8 value, unsigned long port)
 {
-       writeb(value, port);
+       writeb(value, (void __iomem *) port);
 }
 
 static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
 {
-       writeb(value, port);    
+       writeb(value, (void __iomem *) port);
 }
 
 static void ide_mm_outw (u16 value, unsigned long port)
 {
-       writew(value, port);
+       writew(value, (void __iomem *) port);
 }
 
 static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
 {
-       __ide_mm_outsw(port, addr, count);
+       __ide_mm_outsw((void __iomem *) port, addr, count);
 }
 
 static void ide_mm_outl (u32 value, unsigned long port)
 {
-       writel(value, port);
+       writel(value, (void __iomem *) port);
 }
 
 static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
 {
-       __ide_mm_outsl(port, addr, count);
+       __ide_mm_outsl((void __iomem *) port, addr, count);
 }
 
 void default_hwif_mmiops (ide_hwif_t *hwif)
@@ -184,16 +184,6 @@ void default_hwif_mmiops (ide_hwif_t *hwif)
 
 EXPORT_SYMBOL(default_hwif_mmiops);
 
-void default_hwif_transport (ide_hwif_t *hwif)
-{
-       hwif->ata_input_data            = ata_input_data;
-       hwif->ata_output_data           = ata_output_data;
-       hwif->atapi_input_bytes         = atapi_input_bytes;
-       hwif->atapi_output_bytes        = atapi_output_bytes;
-}
-
-EXPORT_SYMBOL(default_hwif_transport);
-
 u32 ide_read_24 (ide_drive_t *drive)
 {
        u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
@@ -202,8 +192,6 @@ u32 ide_read_24 (ide_drive_t *drive)
        return (hcyl<<16)|(lcyl<<8)|sect;
 }
 
-EXPORT_SYMBOL(ide_read_24);
-
 void SELECT_DRIVE (ide_drive_t *drive)
 {
        if (HWIF(drive)->selectproc)
@@ -221,24 +209,18 @@ void SELECT_INTERRUPT (ide_drive_t *drive)
                HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
 }
 
-EXPORT_SYMBOL(SELECT_INTERRUPT);
-
 void SELECT_MASK (ide_drive_t *drive, int mask)
 {
        if (HWIF(drive)->maskproc)
                HWIF(drive)->maskproc(drive, mask);
 }
 
-EXPORT_SYMBOL(SELECT_MASK);
-
 void QUIRK_LIST (ide_drive_t *drive)
 {
        if (HWIF(drive)->quirkproc)
                drive->quirk_list = HWIF(drive)->quirkproc(drive);
 }
 
-EXPORT_SYMBOL(QUIRK_LIST);
-
 /*
  * Some localbus EIDE interfaces require a special access sequence
  * when using 32-bit I/O instructions to transfer data.  We call this
@@ -246,19 +228,17 @@ EXPORT_SYMBOL(QUIRK_LIST);
  * of the sector count register location, with interrupts disabled
  * to ensure that the reads all happen together.
  */
-void ata_vlb_sync (ide_drive_t *drive, unsigned long port)
+static void ata_vlb_sync(ide_drive_t *drive, unsigned long port)
 {
        (void) HWIF(drive)->INB(port);
        (void) HWIF(drive)->INB(port);
        (void) HWIF(drive)->INB(port);
 }
 
-EXPORT_SYMBOL(ata_vlb_sync);
-
 /*
  * This is used for most PIO data transfers *from* the IDE interface
  */
-void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
+static void ata_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
 {
        ide_hwif_t *hwif        = HWIF(drive);
        u8 io_32bit             = drive->io_32bit;
@@ -277,12 +257,10 @@ void ata_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
        }
 }
 
-EXPORT_SYMBOL(ata_input_data);
-
 /*
  * This is used for most PIO data transfers *to* the IDE interface
  */
-void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
+static void ata_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
 {
        ide_hwif_t *hwif        = HWIF(drive);
        u8 io_32bit             = drive->io_32bit;
@@ -301,8 +279,6 @@ void ata_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
        }
 }
 
-EXPORT_SYMBOL(ata_output_data);
-
 /*
  * The following routines are mainly used by the ATAPI drivers.
  *
@@ -311,7 +287,7 @@ EXPORT_SYMBOL(ata_output_data);
  * extra byte allocated for the buffer.
  */
 
-void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
+static void atapi_input_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
 {
        ide_hwif_t *hwif = HWIF(drive);
 
@@ -328,9 +304,7 @@ void atapi_input_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
                hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
 }
 
-EXPORT_SYMBOL(atapi_input_bytes);
-
-void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
+static void atapi_output_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
 {
        ide_hwif_t *hwif = HWIF(drive);
 
@@ -347,7 +321,15 @@ void atapi_output_bytes (ide_drive_t *drive, void *buffer, u32 bytecount)
                hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
 }
 
-EXPORT_SYMBOL(atapi_output_bytes);
+void default_hwif_transport(ide_hwif_t *hwif)
+{
+       hwif->ata_input_data            = ata_input_data;
+       hwif->ata_output_data           = ata_output_data;
+       hwif->atapi_input_bytes         = atapi_input_bytes;
+       hwif->atapi_output_bytes        = atapi_output_bytes;
+}
+
+EXPORT_SYMBOL(default_hwif_transport);
 
 /*
  * Beginning of Taskfile OPCODE Library and feature sets.
@@ -449,6 +431,7 @@ void ide_fix_driveid (struct hd_driveid *id)
 #endif
 }
 
+/* FIXME: exported for use by the USB storage (isd200.c) code only */
 EXPORT_SYMBOL(ide_fix_driveid);
 
 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
@@ -588,7 +571,7 @@ int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 b
                                        break;
 
                                local_irq_restore(flags);
-                               *startstop = DRIVER(drive)->error(drive, "status timeout", stat);
+                               *startstop = ide_error(drive, "status timeout", stat);
                                return 1;
                        }
                }
@@ -606,7 +589,7 @@ int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 b
                if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
                        return 0;
        }
-       *startstop = DRIVER(drive)->error(drive, "status error", stat);
+       *startstop = ide_error(drive, "status error", stat);
        return 1;
 }
 
@@ -618,44 +601,15 @@ EXPORT_SYMBOL(ide_wait_stat);
  */
 u8 eighty_ninty_three (ide_drive_t *drive)
 {
-#if 0
-       if (!HWIF(drive)->udma_four)
+       if(HWIF(drive)->udma_four == 0)
+               return 0;
+       if (!(drive->id->hw_config & 0x6000))
                return 0;
-
-       if (drive->id->major_rev_num) {
-               int hssbd = 0;
-               int i;
-               /*
-                * Determine highest Supported SPEC
-                */
-               for (i=1; i<=15; i++)
-                       if (drive->id->major_rev_num & (1<<i))
-                               hssbd++;
-
-               switch (hssbd) {
-                       case 7:
-                       case 6:
-                       case 5:
-               /* ATA-4 and older do not support above Ultra 33 */
-                       default:
-                               return 0;
-               }
-       }
-
-       return ((u8) (
-#ifndef CONFIG_IDEDMA_IVB
-               (drive->id->hw_config & 0x4000) &&
-#endif /* CONFIG_IDEDMA_IVB */
-                (drive->id->hw_config & 0x6000)) ? 1 : 0);
-
-#else
-
-       return ((u8) ((HWIF(drive)->udma_four) &&
 #ifndef CONFIG_IDEDMA_IVB
-                       (drive->id->hw_config & 0x4000) &&
+       if(!(drive->id->hw_config & 0x4000))
+               return 0;
 #endif /* CONFIG_IDEDMA_IVB */
-                       (drive->id->hw_config & 0x6000)) ? 1 : 0);
-#endif
+       return 1;
 }
 
 EXPORT_SYMBOL(eighty_ninty_three);
@@ -685,8 +639,6 @@ int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
        return 0;
 }
 
-EXPORT_SYMBOL(ide_ata66_check);
-
 /*
  * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
  * 1 : Safe to update drive->id DMA registers.
@@ -705,9 +657,8 @@ int set_transfer (ide_drive_t *drive, ide_task_t *args)
        return 0;
 }
 
-EXPORT_SYMBOL(set_transfer);
-
-u8 ide_auto_reduce_xfer (ide_drive_t *drive)
+#ifdef CONFIG_BLK_DEV_IDEDMA
+static u8 ide_auto_reduce_xfer (ide_drive_t *drive)
 {
        if (!drive->crc_count)
                return drive->current_speed;
@@ -731,8 +682,7 @@ u8 ide_auto_reduce_xfer (ide_drive_t *drive)
                default:                return XFER_PIO_4;
        }
 }
-
-EXPORT_SYMBOL(ide_auto_reduce_xfer);
+#endif /* CONFIG_BLK_DEV_IDEDMA */
 
 /*
  * Update the 
@@ -767,7 +717,7 @@ int ide_driveid_update (ide_drive_t *drive)
        SELECT_MASK(drive, 1);
        if (IDE_CONTROL_REG)
                hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
-       ide_delay_50ms();
+       msleep(50);
        hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
        timeout = jiffies + WAIT_WORSTCASE;
        do {
@@ -775,9 +725,9 @@ int ide_driveid_update (ide_drive_t *drive)
                        SELECT_MASK(drive, 0);
                        return 0;       /* drive timed-out */
                }
-               ide_delay_50ms();       /* give drive a breather */
+               msleep(50);     /* give drive a breather */
        } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
-       ide_delay_50ms();       /* wait for IRQ and DRQ_STAT */
+       msleep(50);     /* wait for IRQ and DRQ_STAT */
        if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
                SELECT_MASK(drive, 0);
                printk("%s: CHECK for good STATUS\n", drive->name);
@@ -807,8 +757,6 @@ int ide_driveid_update (ide_drive_t *drive)
 #endif
 }
 
-EXPORT_SYMBOL(ide_driveid_update);
-
 /*
  * Similar to ide_wait_stat(), except it never calls ide_error internally.
  * This is a kludge to handle the new ide_config_drive_speed() function,
@@ -819,6 +767,11 @@ EXPORT_SYMBOL(ide_driveid_update);
  * It is gone..........
  *
  * const char *msg == consider adding for verbose errors.
+ *
+ * Beware. If we timed out from a series of CRC errors and the timer
+ * expiry caused a switch to PIO mode and we take an IRQ as the drive times
+ * out about the same moment we may be entering this function with a
+ * pending interrupt. 
  */
 int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
 {
@@ -827,7 +780,7 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
        u8 stat;
 
 //     while (HWGROUP(drive)->busy)
-//             ide_delay_50ms();
+//             msleep(50);
 
 #ifdef CONFIG_BLK_DEV_IDEDMA
        if (hwif->ide_dma_check)         /* check if host supports DMA */
@@ -836,20 +789,20 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
 
        /*
         * 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).
-        */
-        /*
-         * Select the drive, and issue the SETFEATURES command
+        * attempt to set_geometry and recalibrate, We can't
+        * do that here as we may be in the IRQ handler already
+        *
+         * Select the drive, and issue the SETFEATURES command in
+         * polled mode.
          */
        disable_irq_nosync(hwif->irq);
        
        /*
-        *      FIXME: we race against the running IRQ here if
+        *      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.
+        *      disable_irq() we hang on the error path. Instead we
+        *      must let the core code know the hwif is doing a polling
+        *      recovery.
         */
         
        udelay(1);
@@ -860,23 +813,43 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
                hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
        hwif->OUTB(speed, IDE_NSECTOR_REG);
        hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
-       hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
-       if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
+       hwif->OUTBSYNC(drive, WIN_SETFEATURES, IDE_COMMAND_REG);
+       /* The status bits are not valid for 400nS */
+       udelay(1);
+       
+       /* Drive status is now valid which means we can allow interrupts
+          to occur as they will see the drive as busy and will not
+          interfere erroneously. IRQ's for this drive will also be off
+          providing control and quirks allow for it */
+          
+       if ((IDE_CONTROL_REG) && drive->quirk_list == 2)
                hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
        udelay(1);
+       
+       /*
+        * Tell the interrupt layer that we are doing polled recovery.
+        * Eventually this should use the same mechanism do_reset does
+        * internally.
+        */
+        
+       hwif->polling = 1;
+       
        /*
         * Wait for drive to become non-BUSY
         */
        if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
-               unsigned long flags, timeout;
-               local_irq_set(flags);
+               unsigned long timeout;
+               /* FIXME */
+/*             spin_unlock_irq(&ide_lock); */
                timeout = jiffies + WAIT_CMD;
                while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
                        if (time_after(jiffies, timeout))
                                break;
                }
-               local_irq_restore(flags);
+/*             spin_lock_irq(&ide_lock); */
        }
+       
+       hwif->polling = 0;
 
        /*
         * Allow status to settle, then read it again.
@@ -948,7 +921,7 @@ EXPORT_SYMBOL(ide_config_drive_speed);
  *
  * See also ide_execute_command
  */
-void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
+static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
                      unsigned int timeout, ide_expiry_t *expiry)
 {
        ide_hwgroup_t *hwgroup = HWGROUP(drive);
@@ -964,8 +937,6 @@ void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
        add_timer(&hwgroup->timer);
 }
 
-EXPORT_SYMBOL(__ide_set_handler);
-
 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
                      unsigned int timeout, ide_expiry_t *expiry)
 {
@@ -1048,14 +1019,14 @@ static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
                        return ide_started;
                }
                /* end of polling */
-               hwgroup->poll_timeout = 0;
+               hwgroup->polling = 0;
                printk("%s: ATAPI reset timed-out, status=0x%02x\n",
                                drive->name, stat);
                /* do it the old fashioned way */
                return do_reset1(drive, 1);
        }
        /* done polling */
-       hwgroup->poll_timeout = 0;
+       hwgroup->polling = 0;
        return ide_stopped;
 }
 
@@ -1096,7 +1067,6 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
                        drive->failures = 0;
                } else {
                        drive->failures++;
-#if FANCY_STATUS_DUMPS
                        printk("master: ");
                        switch (tmp & 0x7f) {
                                case 1: printk("passed");
@@ -1114,12 +1084,9 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
                        if (tmp & 0x80)
                                printk("; slave: failed");
                        printk("\n");
-#else
-                       printk("failed\n");
-#endif /* FANCY_STATUS_DUMPS */
                }
        }
-       hwgroup->poll_timeout = 0;      /* done polling */
+       hwgroup->polling = 0;   /* done polling */
        return ide_stopped;
 }
 
@@ -1136,9 +1103,27 @@ static void check_dma_crc(ide_drive_t *drive)
 #endif
 }
 
-void pre_reset (ide_drive_t *drive)
+static void ide_disk_pre_reset(ide_drive_t *drive)
 {
-       DRIVER(drive)->pre_reset(drive);
+       int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
+
+       drive->special.all = 0;
+       drive->special.b.set_geometry = legacy;
+       drive->special.b.recalibrate  = legacy;
+       if (OK_TO_RESET_CONTROLLER)
+               drive->mult_count = 0;
+       if (!drive->keep_settings && !drive->using_dma)
+               drive->mult_req = 0;
+       if (drive->mult_req != drive->mult_count)
+               drive->special.b.set_multmode = 1;
+}
+
+static void pre_reset(ide_drive_t *drive)
+{
+       if (drive->media == ide_disk)
+               ide_disk_pre_reset(drive);
+       else
+               drive->post_reset = 1;
 
        if (!drive->keep_settings) {
                if (drive->using_dma) {
@@ -1192,8 +1177,10 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
                pre_reset(drive);
                SELECT_DRIVE(drive);
                udelay (20);
-               hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
+               hwif->OUTBSYNC(drive, WIN_SRST, IDE_COMMAND_REG);
+               ndelay(400);
                hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
+               hwgroup->polling = 1;
                __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
                spin_unlock_irqrestore(&ide_lock, flags);
                return ide_started;
@@ -1234,6 +1221,7 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
        /* more than enough time */
        udelay(10);
        hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
+       hwgroup->polling = 1;
        __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
 
        /*