]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/ide/ide-disk.c
dz.c: Resource management
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-disk.c
index a063957e9adda1108c5cca512f1fffb5e027b210..3c69822507e2a59216e8d2690362c961a940579b 100644 (file)
@@ -1,10 +1,9 @@
 /*
- *  linux/drivers/ide/ide-disk.c       Version 1.18    Mar 05, 2003
- *
- *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
- *  Copyright (C) 1998-2002  Linux ATA Development
- *                             Andre Hedrick <andre@linux-ide.org>
- *  Copyright (C) 2003      Red Hat <alan@redhat.com>
+ *  Copyright (C) 1994-1998       Linus Torvalds & authors (see below)
+ *  Copyright (C) 1998-2002       Linux ATA Development
+ *                                   Andre Hedrick <andre@linux-ide.org>
+ *  Copyright (C) 2003            Red Hat <alan@redhat.com>
+ *  Copyright (C) 2003-2005, 2007  Bartlomiej Zolnierkiewicz
  */
 
 /*
@@ -129,6 +128,50 @@ static int lba_capacity_is_ok (struct hd_driveid *id)
        return 0;       /* lba_capacity value may be bad */
 }
 
+static const u8 ide_rw_cmds[] = {
+       WIN_MULTREAD,
+       WIN_MULTWRITE,
+       WIN_MULTREAD_EXT,
+       WIN_MULTWRITE_EXT,
+       WIN_READ,
+       WIN_WRITE,
+       WIN_READ_EXT,
+       WIN_WRITE_EXT,
+       WIN_READDMA,
+       WIN_WRITEDMA,
+       WIN_READDMA_EXT,
+       WIN_WRITEDMA_EXT,
+};
+
+static const u8 ide_data_phases[] = {
+       TASKFILE_MULTI_IN,
+       TASKFILE_MULTI_OUT,
+       TASKFILE_IN,
+       TASKFILE_OUT,
+       TASKFILE_IN_DMA,
+       TASKFILE_OUT_DMA,
+};
+
+static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
+{
+       u8 index, lba48, write;
+
+       lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
+       write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
+
+       if (dma)
+               index = drive->vdma ? 4 : 8;
+       else
+               index = drive->mult_count ? 0 : 4;
+
+       task->tf.command = ide_rw_cmds[index + lba48 + write];
+
+       if (dma)
+               index = 8; /* fixup index */
+
+       task->data_phase = ide_data_phases[index / 2 + write];
+}
+
 /*
  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
  * using LBA if supported, or CHS otherwise, to address sectors.
@@ -139,9 +182,9 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
        unsigned int dma        = drive->using_dma;
        u16 nsectors            = (u16)rq->nr_sectors;
        u8 lba48                = (drive->addressing == 1) ? 1 : 0;
-       u8 command              = WIN_NOP;
        ide_task_t              task;
        struct ide_taskfile     *tf = &task.tf;
+       ide_startstop_t         rc;
 
        if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
                if (block + rq->nr_sectors > 1ULL << 28)
@@ -157,7 +200,7 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
 
        memset(&task, 0, sizeof(task));
        task.tf_flags = IDE_TFLAG_NO_SELECT_MASK;  /* FIXME? */
-       task.tf_flags |= (IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE);
+       task.tf_flags |= (IDE_TFLAG_TF | IDE_TFLAG_DEVICE);
 
        if (drive->select.b.lba) {
                if (lba48) {
@@ -175,13 +218,8 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
                        tf->lbal   = (u8) block;
                        tf->lbam   = (u8)(block >>  8);
                        tf->lbah   = (u8)(block >> 16);
-#ifdef DEBUG
-                       printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
-                               drive->name, tf->hob_nsect, tf->nsect,
-                               tf->hob_lbah, tf->hob_lbam, tf->hob_lbal,
-                               tf->lbah, tf->lbam, tf->lbal);
-#endif
-                       task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
+
+                       task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
                } else {
                        tf->nsect  = nsectors & 0xff;
                        tf->lbal   = block;
@@ -205,53 +243,26 @@ static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
                tf->device = head;
        }
 
-       ide_tf_load(drive, &task);
-
-       if (dma) {
-               if (!hwif->dma_setup(drive)) {
-                       if (rq_data_dir(rq)) {
-                               command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
-                               if (drive->vdma)
-                                       command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
-                       } else {
-                               command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
-                               if (drive->vdma)
-                                       command = lba48 ? WIN_READ_EXT: WIN_READ;
-                       }
-                       hwif->dma_exec_cmd(drive, command);
-                       hwif->dma_start(drive);
-                       return ide_started;
-               }
-               /* fallback to PIO */
-               ide_init_sg_cmd(drive, rq);
-       }
-
-       if (rq_data_dir(rq) == READ) {
-
-               if (drive->mult_count) {
-                       hwif->data_phase = TASKFILE_MULTI_IN;
-                       command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
-               } else {
-                       hwif->data_phase = TASKFILE_IN;
-                       command = lba48 ? WIN_READ_EXT : WIN_READ;
-               }
+       if (rq_data_dir(rq))
+               task.tf_flags |= IDE_TFLAG_WRITE;
 
-               ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
-               return ide_started;
-       } else {
-               if (drive->mult_count) {
-                       hwif->data_phase = TASKFILE_MULTI_OUT;
-                       command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
-               } else {
-                       hwif->data_phase = TASKFILE_OUT;
-                       command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
-               }
+       ide_tf_set_cmd(drive, &task, dma);
+       if (!dma)
+               hwif->data_phase = task.data_phase;
+       task.rq = rq;
 
-               hwif->OUTBSYNC(drive, command, IDE_COMMAND_REG);
-               ndelay(400);    /* FIXME */
+       rc = do_rw_taskfile(drive, &task);
 
-               return pre_task_out_intr(drive, rq);
+       if (rc == ide_stopped && dma) {
+               /* fallback to PIO */
+               task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
+               ide_tf_set_cmd(drive, &task, 0);
+               hwif->data_phase = task.data_phase;
+               ide_init_sg_cmd(drive, rq);
+               rc = do_rw_taskfile(drive, &task);
        }
+
+       return rc;
 }
 
 /*
@@ -302,25 +313,16 @@ static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
        else
                tf->command = WIN_READ_NATIVE_MAX;
        tf->device  = ATA_LBA;
-       args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        if (lba48)
-               args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
+               args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
        /* submit command request */
        ide_no_data_taskfile(drive, &args);
 
        /* if OK, compute maximum address value */
-       if ((tf->status & 0x01) == 0) {
-               u32 high, low;
+       if ((tf->status & 0x01) == 0)
+               addr = ide_get_lba_addr(tf, lba48) + 1;
 
-               if (lba48)
-                       high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
-                               tf->hob_lbal;
-               else
-                       high = tf->device & 0xf;
-               low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
-               addr = ((__u64)high << 24) | low;
-               addr++; /* since the return value is (maxlba - 1), we add 1 */
-       }
        return addr;
 }
 
@@ -350,24 +352,15 @@ static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
                tf->command  = WIN_SET_MAX;
        }
        tf->device |= ATA_LBA;
-       args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        if (lba48)
-               args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_OUT_HOB);
+               args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
        /* submit command request */
        ide_no_data_taskfile(drive, &args);
        /* if OK, compute maximum address value */
-       if ((tf->status & 0x01) == 0) {
-               u32 high, low;
+       if ((tf->status & 0x01) == 0)
+               addr_set = ide_get_lba_addr(tf, lba48) + 1;
 
-               if (lba48)
-                       high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
-                               tf->hob_lbal;
-               else
-                       high = tf->device & 0xf;
-               low  = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
-               addr_set = ((__u64)high << 24) | low;
-               addr_set++;
-       }
        return addr_set;
 }
 
@@ -501,7 +494,7 @@ static int smart_enable(ide_drive_t *drive)
        tf->lbam    = SMART_LCYL_PASS;
        tf->lbah    = SMART_HCYL_PASS;
        tf->command = WIN_SMART;
-       args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        return ide_no_data_taskfile(drive, &args);
 }
 
@@ -516,7 +509,7 @@ static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
        tf->lbam    = SMART_LCYL_PASS;
        tf->lbah    = SMART_HCYL_PASS;
        tf->command = WIN_SMART;
-       args.tf_flags   = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags   = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        args.data_phase = TASKFILE_IN;
        (void) smart_enable(drive);
        return ide_raw_taskfile(drive, &args, buf, 1);
@@ -626,8 +619,10 @@ static int set_multcount(ide_drive_t *drive, int arg)
 
        if (drive->special.b.set_multmode)
                return -EBUSY;
+
        ide_init_drive_cmd (&rq);
-       rq.cmd_type = REQ_TYPE_ATA_CMD;
+       rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
+
        drive->mult_req = arg;
        drive->special.b.set_multmode = 1;
        (void) ide_do_drive_cmd (drive, &rq, ide_wait);
@@ -695,7 +690,7 @@ static int write_cache(ide_drive_t *drive, int arg)
                args.tf.feature = arg ?
                        SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
                args.tf.command = WIN_SETFEATURES;
-               args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+               args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
                err = ide_no_data_taskfile(drive, &args);
                if (err == 0)
                        drive->wcache = arg;
@@ -715,7 +710,7 @@ static int do_idedisk_flushcache (ide_drive_t *drive)
                args.tf.command = WIN_FLUSH_CACHE_EXT;
        else
                args.tf.command = WIN_FLUSH_CACHE;
-       args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        return ide_no_data_taskfile(drive, &args);
 }
 
@@ -730,7 +725,7 @@ static int set_acoustic (ide_drive_t *drive, int arg)
        args.tf.feature = arg ? SETFEATURES_EN_AAM : SETFEATURES_DIS_AAM;
        args.tf.nsect   = arg;
        args.tf.command = WIN_SETFEATURES;
-       args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
+       args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
        ide_no_data_taskfile(drive, &args);
        drive->acoustic = arg;
        return 0;
@@ -767,7 +762,6 @@ static void idedisk_add_settings(ide_drive_t *drive)
        ide_add_setting(drive,  "bios_head",    SETTING_RW,     TYPE_BYTE,      0,      255,                    1,      1,      &drive->bios_head,      NULL);
        ide_add_setting(drive,  "bios_sect",    SETTING_RW,     TYPE_BYTE,      0,      63,                     1,      1,      &drive->bios_sect,      NULL);
        ide_add_setting(drive,  "address",      SETTING_RW,     TYPE_BYTE,      0,      2,                      1,      1,      &drive->addressing,     set_lba_addressing);
-       ide_add_setting(drive,  "bswap",        SETTING_READ,   TYPE_BYTE,      0,      1,                      1,      1,      &drive->bswap,          NULL);
        ide_add_setting(drive,  "multcount",    SETTING_RW,     TYPE_BYTE,      0,      id->max_multsect,       1,      1,      &drive->mult_count,     set_multcount);
        ide_add_setting(drive,  "nowerr",       SETTING_RW,     TYPE_BYTE,      0,      1,                      1,      1,      &drive->nowerr,         set_nowerr);
        ide_add_setting(drive,  "lun",          SETTING_RW,     TYPE_INT,       0,      7,                      1,      1,      &drive->lun,            NULL);
@@ -976,6 +970,17 @@ static ide_driver_t idedisk_driver = {
 #endif
 };
 
+static int idedisk_set_doorlock(ide_drive_t *drive, int on)
+{
+       ide_task_t task;
+
+       memset(&task, 0, sizeof(task));
+       task.tf.command = on ? WIN_DOORLOCK : WIN_DOORUNLOCK;
+       task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
+
+       return ide_no_data_taskfile(drive, &task);
+}
+
 static int idedisk_open(struct inode *inode, struct file *filp)
 {
        struct gendisk *disk = inode->i_bdev->bd_disk;
@@ -990,17 +995,13 @@ static int idedisk_open(struct inode *inode, struct file *filp)
        idkp->openers++;
 
        if (drive->removable && idkp->openers == 1) {
-               ide_task_t args;
-               memset(&args, 0, sizeof(ide_task_t));
-               args.tf.command = WIN_DOORLOCK;
-               args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
                check_disk_change(inode->i_bdev);
                /*
                 * Ignore the return code from door_lock,
                 * since the open() has already succeeded,
                 * and the door_lock is irrelevant at this point.
                 */
-               if (drive->doorlocking && ide_no_data_taskfile(drive, &args))
+               if (drive->doorlocking && idedisk_set_doorlock(drive, 1))
                        drive->doorlocking = 0;
        }
        return 0;
@@ -1016,11 +1017,7 @@ static int idedisk_release(struct inode *inode, struct file *filp)
                ide_cacheflush_p(drive);
 
        if (drive->removable && idkp->openers == 1) {
-               ide_task_t args;
-               memset(&args, 0, sizeof(ide_task_t));
-               args.tf.command = WIN_DOORUNLOCK;
-               args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
-               if (drive->doorlocking && ide_no_data_taskfile(drive, &args))
+               if (drive->doorlocking && idedisk_set_doorlock(drive, 0))
                        drive->doorlocking = 0;
        }