falconide/q40ide: add ->atapi_*put_bytes and ->ata_*put_data methods (take 2)
[safe/jmp/linux-2.6] / drivers / ide / ide-taskfile.c
index 9404650..7f6bfd3 100644 (file)
@@ -1,11 +1,9 @@
 /*
- * linux/drivers/ide/ide-taskfile.c    Version 0.38    March 05, 2003
- *
- *  Copyright (C) 2000-2002    Michael Cornwell <cornwell@acm.org>
- *  Copyright (C) 2000-2002    Andre Hedrick <andre@linux-ide.org>
- *  Copyright (C) 2001-2002    Klaus Smolin
+ *  Copyright (C) 2000-2002       Michael Cornwell <cornwell@acm.org>
+ *  Copyright (C) 2000-2002       Andre Hedrick <andre@linux-ide.org>
+ *  Copyright (C) 2001-2002       Klaus Smolin
  *                                     IBM Storage Technology Division
- *  Copyright (C) 2003-2004    Bartlomiej Zolnierkiewicz
+ *  Copyright (C) 2003-2004, 2007  Bartlomiej Zolnierkiewicz
  *
  *  The big the bad and the ugly.
  */
@@ -38,6 +36,7 @@
 void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
 {
        ide_hwif_t *hwif = drive->hwif;
+       struct ide_io_ports *io_ports = &hwif->io_ports;
        struct ide_taskfile *tf = &task->tf;
        u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
 
@@ -61,32 +60,33 @@ void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
                SELECT_MASK(drive, 0);
 
        if (task->tf_flags & IDE_TFLAG_OUT_DATA)
-               hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
+               hwif->OUTW((tf->hob_data << 8) | tf->data, io_ports->data_addr);
 
        if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
-               hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
+               hwif->OUTB(tf->hob_feature, io_ports->feature_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
-               hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
+               hwif->OUTB(tf->hob_nsect, io_ports->nsect_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
-               hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
+               hwif->OUTB(tf->hob_lbal, io_ports->lbal_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
-               hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
+               hwif->OUTB(tf->hob_lbam, io_ports->lbam_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
-               hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
+               hwif->OUTB(tf->hob_lbah, io_ports->lbah_addr);
 
        if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
-               hwif->OUTB(tf->feature, IDE_FEATURE_REG);
+               hwif->OUTB(tf->feature, io_ports->feature_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
-               hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
+               hwif->OUTB(tf->nsect, io_ports->nsect_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
-               hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
+               hwif->OUTB(tf->lbal, io_ports->lbal_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
-               hwif->OUTB(tf->lbam, IDE_LCYL_REG);
+               hwif->OUTB(tf->lbam, io_ports->lbam_addr);
        if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
-               hwif->OUTB(tf->lbah, IDE_HCYL_REG);
+               hwif->OUTB(tf->lbah, io_ports->lbah_addr);
 
        if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
-               hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
+               hwif->OUTB((tf->device & HIHI) | drive->select.all,
+                          io_ports->device_addr);
 }
 
 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
@@ -135,6 +135,7 @@ ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
        ide_hwif_t *hwif        = HWIF(drive);
        struct ide_taskfile *tf = &task->tf;
        ide_handler_t *handler = NULL;
+       const struct ide_dma_ops *dma_ops = hwif->dma_ops;
 
        if (task->data_phase == TASKFILE_MULTI_IN ||
            task->data_phase == TASKFILE_MULTI_OUT) {
@@ -154,7 +155,7 @@ ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
        switch (task->data_phase) {
        case TASKFILE_MULTI_OUT:
        case TASKFILE_OUT:
-               hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
+               hwif->OUTBSYNC(drive, tf->command, hwif->io_ports.command_addr);
                ndelay(400);    /* FIXME */
                return pre_task_out_intr(drive, task->rq);
        case TASKFILE_MULTI_IN:
@@ -177,10 +178,10 @@ ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
                return ide_started;
        default:
                if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
-                   hwif->dma_setup(drive))
+                   dma_ops->dma_setup(drive))
                        return ide_stopped;
-               hwif->dma_exec_cmd(drive, tf->command);
-               hwif->dma_start(drive);
+               dma_ops->dma_exec_cmd(drive, tf->command);
+               dma_ops->dma_start(drive);
                return ide_started;
        }
 }
@@ -191,12 +192,11 @@ EXPORT_SYMBOL_GPL(do_rw_taskfile);
  */
 static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
 {
-       ide_hwif_t *hwif = HWIF(drive);
-       u8 stat;
+       u8 stat = ide_read_status(drive);
 
-       if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
+       if (OK_STAT(stat, READY_STAT, BAD_STAT))
                drive->mult_count = drive->mult_req;
-       else {
+       else {
                drive->mult_req = drive->mult_count = 0;
                drive->special.b.recalibrate = 1;
                (void) ide_dump_status(drive, "set_multmode", stat);
@@ -209,11 +209,10 @@ static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
  */
 static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
 {
-       ide_hwif_t *hwif = HWIF(drive);
        int retries = 5;
        u8 stat;
 
-       while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
+       while (((stat = ide_read_status(drive)) & BUSY_STAT) && retries--)
                udelay(10);
 
        if (OK_STAT(stat, READY_STAT, BAD_STAT))
@@ -232,10 +231,9 @@ static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
  */
 static ide_startstop_t recal_intr(ide_drive_t *drive)
 {
-       ide_hwif_t *hwif = HWIF(drive);
-       u8 stat;
+       u8 stat = ide_read_status(drive);
 
-       if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
+       if (!OK_STAT(stat, READY_STAT, BAD_STAT))
                return ide_error(drive, "recal_intr", stat);
        return ide_stopped;
 }
@@ -246,23 +244,23 @@ static ide_startstop_t recal_intr(ide_drive_t *drive)
 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
 {
        ide_task_t *args        = HWGROUP(drive)->rq->special;
-       ide_hwif_t *hwif        = HWIF(drive);
        u8 stat;
 
        local_irq_enable_in_hardirq();
-       if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
+       stat = ide_read_status(drive);
+
+       if (!OK_STAT(stat, READY_STAT, BAD_STAT))
                return ide_error(drive, "task_no_data_intr", stat);
                /* calls ide_end_drive_cmd */
-       }
+
        if (args)
-               ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
+               ide_end_drive_cmd(drive, stat, ide_read_error(drive));
 
        return ide_stopped;
 }
 
-u8 wait_drive_not_busy(ide_drive_t *drive)
+static u8 wait_drive_not_busy(ide_drive_t *drive)
 {
-       ide_hwif_t *hwif = HWIF(drive);
        int retries;
        u8 stat;
 
@@ -271,7 +269,9 @@ u8 wait_drive_not_busy(ide_drive_t *drive)
         * This can take up to 10 usec, but we will wait max 1 ms.
         */
        for (retries = 0; retries < 100; retries++) {
-               if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
+               stat = ide_read_status(drive);
+
+               if (stat & BUSY_STAT)
                        udelay(10);
                else
                        break;
@@ -283,7 +283,8 @@ u8 wait_drive_not_busy(ide_drive_t *drive)
        return stat;
 }
 
-static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
+static void ide_pio_sector(ide_drive_t *drive, struct request *rq,
+                          unsigned int write)
 {
        ide_hwif_t *hwif = drive->hwif;
        struct scatterlist *sg = hwif->sg_table;
@@ -323,9 +324,9 @@ static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
 
        /* do the actual data transfer */
        if (write)
-               hwif->ata_output_data(drive, buf, SECTOR_WORDS);
+               hwif->ata_output_data(drive, rq, buf, SECTOR_WORDS);
        else
-               hwif->ata_input_data(drive, buf, SECTOR_WORDS);
+               hwif->ata_input_data(drive, rq, buf, SECTOR_WORDS);
 
        kunmap_atomic(buf, KM_BIO_SRC_IRQ);
 #ifdef CONFIG_HIGHMEM
@@ -333,13 +334,14 @@ static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
 #endif
 }
 
-static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
+static void ide_pio_multi(ide_drive_t *drive, struct request *rq,
+                         unsigned int write)
 {
        unsigned int nsect;
 
        nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
        while (nsect--)
-               ide_pio_sector(drive, write);
+               ide_pio_sector(drive, rq, write);
 }
 
 static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
@@ -362,10 +364,10 @@ static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
        switch (drive->hwif->data_phase) {
        case TASKFILE_MULTI_IN:
        case TASKFILE_MULTI_OUT:
-               ide_pio_multi(drive, write);
+               ide_pio_multi(drive, rq, write);
                break;
        default:
-               ide_pio_sector(drive, write);
+               ide_pio_sector(drive, rq, write);
                break;
        }
 
@@ -410,7 +412,7 @@ static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
 void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
 {
        if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
-               u8 err = drive->hwif->INB(IDE_ERROR_REG);
+               u8 err = ide_read_error(drive);
 
                ide_end_drive_cmd(drive, stat, err);
                return;
@@ -426,30 +428,48 @@ void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
 }
 
 /*
+ * We got an interrupt on a task_in case, but no errors and no DRQ.
+ *
+ * It might be a spurious irq (shared irq), but it might be a
+ * command that had no output.
+ */
+static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
+{
+       /* Command all done? */
+       if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
+               task_end_request(drive, rq, stat);
+               return ide_stopped;
+       }
+
+       /* Assume it was a spurious irq */
+       ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
+       return ide_started;
+}
+
+/*
  * Handler for command with PIO data-in phase (Read/Read Multiple).
  */
 static ide_startstop_t task_in_intr(ide_drive_t *drive)
 {
        ide_hwif_t *hwif = drive->hwif;
        struct request *rq = HWGROUP(drive)->rq;
-       u8 stat = hwif->INB(IDE_STATUS_REG);
-
-       /* new way for dealing with premature shared PCI interrupts */
-       if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
-               if (stat & (ERR_STAT | DRQ_STAT))
-                       return task_error(drive, rq, __FUNCTION__, stat);
-               /* No data yet, so wait for another IRQ. */
-               ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
-               return ide_started;
-       }
+       u8 stat = ide_read_status(drive);
+
+       /* Error? */
+       if (stat & ERR_STAT)
+               return task_error(drive, rq, __func__, stat);
+
+       /* Didn't want any data? Odd. */
+       if (!(stat & DRQ_STAT))
+               return task_in_unexpected(drive, rq, stat);
 
        ide_pio_datablock(drive, rq, 0);
 
-       /* If it was the last datablock check status and finish transfer. */
+       /* Are we done? Check status and finish transfer. */
        if (!hwif->nleft) {
                stat = wait_drive_not_busy(drive);
                if (!OK_STAT(stat, 0, BAD_STAT))
-                       return task_error(drive, rq, __FUNCTION__, stat);
+                       return task_error(drive, rq, __func__, stat);
                task_end_request(drive, rq, stat);
                return ide_stopped;
        }
@@ -467,14 +487,14 @@ static ide_startstop_t task_out_intr (ide_drive_t *drive)
 {
        ide_hwif_t *hwif = drive->hwif;
        struct request *rq = HWGROUP(drive)->rq;
-       u8 stat = hwif->INB(IDE_STATUS_REG);
+       u8 stat = ide_read_status(drive);
 
        if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
-               return task_error(drive, rq, __FUNCTION__, stat);
+               return task_error(drive, rq, __func__, stat);
 
        /* Deal with unexpected ATA data phase. */
        if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
-               return task_error(drive, rq, __FUNCTION__, stat);
+               return task_error(drive, rq, __func__, stat);
 
        if (!hwif->nleft) {
                task_end_request(drive, rq, stat);
@@ -657,7 +677,7 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
                                /* (hs): give up if multcount is not set */
                                printk(KERN_ERR "%s: %s Multimode Write " \
                                        "multcount is not set\n",
-                                       drive->name, __FUNCTION__);
+                                       drive->name, __func__);
                                err = -EPERM;
                                goto abort;
                        }
@@ -674,7 +694,7 @@ int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
                                /* (hs): give up if multcount is not set */
                                printk(KERN_ERR "%s: %s Multimode Read failure " \
                                        "multcount is not set\n",
-                                       drive->name, __FUNCTION__);
+                                       drive->name, __func__);
                                err = -EPERM;
                                goto abort;
                        }
@@ -750,32 +770,14 @@ abort:
 }
 #endif
 
-int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
-{
-       struct request rq;
-       u8 buffer[4];
-
-       if (!buf)
-               buf = buffer;
-       memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
-       ide_init_drive_cmd(&rq);
-       rq.cmd_type = REQ_TYPE_ATA_CMD;
-       rq.buffer = buf;
-       *buf++ = cmd;
-       *buf++ = nsect;
-       *buf++ = feature;
-       *buf++ = sectors;
-       return ide_do_drive_cmd(drive, &rq, ide_wait);
-}
-
 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
 {
-       int err = 0;
-       u8 args[4], *argbuf = args;
-       u8 xfer_rate = 0;
-       int argsize = 4;
+       u8 *buf = NULL;
+       int bufsize = 0, err = 0;
+       u8 args[4], xfer_rate = 0;
        ide_task_t tfargs;
        struct ide_taskfile *tf = &tfargs.tf;
+       struct hd_driveid *id = drive->id;
 
        if (NULL == (void *) arg) {
                struct request rq;
@@ -791,23 +793,45 @@ int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
 
        memset(&tfargs, 0, sizeof(ide_task_t));
        tf->feature = args[2];
-       tf->nsect   = args[3];
-       tf->lbal    = args[1];
+       if (args[0] == WIN_SMART) {
+               tf->nsect = args[3];
+               tf->lbal  = args[1];
+               tf->lbam  = 0x4f;
+               tf->lbah  = 0xc2;
+               tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
+       } else {
+               tf->nsect = args[1];
+               tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
+                                 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
+       }
        tf->command = args[0];
+       tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
 
        if (args[3]) {
-               argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
-               argbuf = kzalloc(argsize, GFP_KERNEL);
-               if (argbuf == NULL)
+               tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
+               bufsize = SECTOR_WORDS * 4 * args[3];
+               buf = kzalloc(bufsize, GFP_KERNEL);
+               if (buf == NULL)
                        return -ENOMEM;
        }
-       if (set_transfer(drive, &tfargs)) {
+
+       if (tf->command == WIN_SETFEATURES &&
+           tf->feature == SETFEATURES_XFER &&
+           tf->nsect >= XFER_SW_DMA_0 &&
+           (id->dma_ultra || id->dma_mword || id->dma_1word)) {
                xfer_rate = args[1];
-               if (ide_ata66_check(drive, &tfargs))
+               if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
+                       printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
+                                           "be set\n", drive->name);
                        goto abort;
+               }
        }
 
-       err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
+       err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
+
+       args[0] = tf->status;
+       args[1] = tf->error;
+       args[2] = tf->nsect;
 
        if (!err && xfer_rate) {
                /* active-retuning-calls future */
@@ -815,10 +839,13 @@ int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
                ide_driveid_update(drive);
        }
 abort:
-       if (copy_to_user((void __user *)arg, argbuf, argsize))
+       if (copy_to_user((void __user *)arg, &args, 4))
                err = -EFAULT;
-       if (argsize > 4)
-               kfree(argbuf);
+       if (buf) {
+               if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
+                       err = -EFAULT;
+               kfree(buf);
+       }
        return err;
 }