Merge branch 'bp-remove-pc-buf' into for-next
[pandora-kernel.git] / drivers / ide / ide-tape.c
index 9ca2665..bb71e1e 100644 (file)
@@ -240,18 +240,27 @@ static struct class *idetape_sysfs_class;
 
 static void ide_tape_release(struct device *);
 
-static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
+static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
+
+static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
+                                        unsigned int i)
 {
        struct ide_tape_obj *tape = NULL;
 
        mutex_lock(&idetape_ref_mutex);
-       tape = ide_drv_g(disk, ide_tape_obj);
+
+       if (cdev)
+               tape = idetape_devs[i];
+       else
+               tape = ide_drv_g(disk, ide_tape_obj);
+
        if (tape) {
                if (ide_device_get(tape->drive))
                        tape = NULL;
                else
                        get_device(&tape->dev);
        }
+
        mutex_unlock(&idetape_ref_mutex);
        return tape;
 }
@@ -266,24 +275,6 @@ static void ide_tape_put(struct ide_tape_obj *tape)
        mutex_unlock(&idetape_ref_mutex);
 }
 
-/*
- * The variables below are used for the character device interface. Additional
- * state variables are defined in our ide_drive_t structure.
- */
-static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
-
-static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
-{
-       struct ide_tape_obj *tape = NULL;
-
-       mutex_lock(&idetape_ref_mutex);
-       tape = idetape_devs[i];
-       if (tape)
-               get_device(&tape->dev);
-       mutex_unlock(&idetape_ref_mutex);
-       return tape;
-}
-
 /*
  * called on each failed packet command retry to analyze the request sense. We
  * currently do not utilize this information.
@@ -631,15 +622,15 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
 
        if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
            (rq->cmd[13] & REQ_IDETAPE_PC2) == 0)
-               set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
+               drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
 
        if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
-               set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
+               drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
                drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
        }
 
-       if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) &&
-           (stat & ATA_DSC) == 0) {
+       if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
+           !(stat & ATA_DSC)) {
                if (postponed_rq == NULL) {
                        tape->dsc_polling_start = jiffies;
                        tape->dsc_poll_freq = tape->best_dsc_rw_freq;
@@ -659,7 +650,9 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
                        tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
                idetape_postpone_request(drive);
                return ide_stopped;
-       }
+       } else
+               drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
+
        if (rq->cmd[13] & REQ_IDETAPE_READ) {
                pc = &tape->queued_pc;
                ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
@@ -719,7 +712,7 @@ static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
        int load_attempted = 0;
 
        /* Wait for the tape to become ready */
-       set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
+       set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
        timeout += jiffies;
        while (time_before(jiffies, timeout)) {
                if (ide_do_test_unit_ready(drive, disk) == 0)
@@ -779,7 +772,8 @@ static int ide_tape_read_position(ide_drive_t *drive)
                if (buf[0] & 0x4) {
                        printk(KERN_INFO "ide-tape: Block location is unknown"
                                         "to the tape\n");
-                       clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
+                       clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
+                                 &drive->atapi_flags);
                        return -1;
                } else {
                        debug_log(DBG_SENSE, "Block Location - %u\n",
@@ -787,7 +781,8 @@ static int ide_tape_read_position(ide_drive_t *drive)
 
                        tape->partition = buf[1];
                        tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
-                       set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags);
+                       set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
+                               &drive->atapi_flags);
                }
        }
 
@@ -813,7 +808,7 @@ static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
        if (tape->chrdev_dir != IDETAPE_DIR_READ)
                return;
 
-       clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
+       clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
        tape->valid = 0;
        if (tape->buf != NULL) {
                kfree(tape->buf);
@@ -887,6 +882,7 @@ static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
        rq->cmd_type = REQ_TYPE_SPECIAL;
        rq->cmd[13] = cmd;
        rq->rq_disk = tape->disk;
+       rq->__sector = tape->first_frame;
 
        if (size) {
                ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
@@ -1106,7 +1102,8 @@ static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
 
        if (tape->chrdev_dir == IDETAPE_DIR_READ) {
                tape->valid = 0;
-               if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
+               if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
+                                      &drive->atapi_flags))
                        ++count;
                ide_tape_discard_merge_buffer(drive, 0);
        }
@@ -1161,7 +1158,7 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
        debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
 
        if (tape->chrdev_dir != IDETAPE_DIR_READ) {
-               if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags))
+               if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
                        if (count > tape->blk_size &&
                            (count % tape->blk_size) == 0)
                                tape->user_bs_factor = count / tape->blk_size;
@@ -1177,7 +1174,8 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
                /* refill if staging buffer is empty */
                if (!tape->valid) {
                        /* If we are at a filemark, nothing more to read */
-                       if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
+                       if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
+                                    &drive->atapi_flags))
                                break;
                        /* read */
                        if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
@@ -1195,7 +1193,7 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
                done += todo;
        }
 
-       if (!done && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
+       if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
                debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
 
                idetape_space_over_filemarks(drive, MTFSF, 1);
@@ -1329,7 +1327,8 @@ static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
                ide_tape_discard_merge_buffer(drive, 0);
                retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
                if (!retval)
-                       clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags);
+                       clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
+                                 &drive->atapi_flags);
                return retval;
        case MTNOP:
                ide_tape_discard_merge_buffer(drive, 0);
@@ -1351,9 +1350,11 @@ static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
                            mt_count % tape->blk_size)
                                return -EIO;
                        tape->user_bs_factor = mt_count / tape->blk_size;
-                       clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
+                       clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
+                                 &drive->atapi_flags);
                } else
-                       set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags);
+                       set_bit(ilog2(IDE_AFLAG_DETECT_BS),
+                               &drive->atapi_flags);
                return 0;
        case MTSEEK:
                ide_tape_discard_merge_buffer(drive, 0);
@@ -1480,7 +1481,7 @@ static int idetape_chrdev_open(struct inode *inode, struct file *filp)
                return -ENXIO;
 
        lock_kernel();
-       tape = ide_tape_chrdev_get(i);
+       tape = ide_tape_get(NULL, true, i);
        if (!tape) {
                unlock_kernel();
                return -ENXIO;
@@ -1499,20 +1500,20 @@ static int idetape_chrdev_open(struct inode *inode, struct file *filp)
 
        filp->private_data = tape;
 
-       if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) {
+       if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
                retval = -EBUSY;
                goto out_put_tape;
        }
 
        retval = idetape_wait_ready(drive, 60 * HZ);
        if (retval) {
-               clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
+               clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
                printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
                goto out_put_tape;
        }
 
-       ide_tape_read_position(drive);
-       if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags))
+       idetape_read_position(drive);
+       if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
                (void)idetape_rewind_tape(drive);
 
        /* Read block size and write protect status from drive. */
@@ -1528,7 +1529,7 @@ static int idetape_chrdev_open(struct inode *inode, struct file *filp)
        if (tape->write_prot) {
                if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
                    (filp->f_flags & O_ACCMODE) == O_RDWR) {
-                       clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
+                       clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
                        retval = -EROFS;
                        goto out_put_tape;
                }
@@ -1585,15 +1586,17 @@ static int idetape_chrdev_release(struct inode *inode, struct file *filp)
                        ide_tape_discard_merge_buffer(drive, 1);
        }
 
-       if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags))
+       if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
+                                   &drive->atapi_flags))
                (void) idetape_rewind_tape(drive);
+
        if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
                if (tape->door_locked == DOOR_LOCKED) {
                        if (!ide_set_media_lock(drive, tape->disk, 0))
                                tape->door_locked = DOOR_UNLOCKED;
                }
        }
-       clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags);
+       clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
        ide_tape_put(tape);
        unlock_kernel();
        return 0;
@@ -1896,7 +1899,7 @@ static const struct file_operations idetape_fops = {
 
 static int idetape_open(struct block_device *bdev, fmode_t mode)
 {
-       struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk);
+       struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0);
 
        if (!tape)
                return -ENXIO;