]> Pileus Git - ~andy/linux/blobdiff - drivers/scsi/sd.c
[SCSI] always handle REQ_BLOCK_PC requests in common code
[~andy/linux] / drivers / scsi / sd.c
index 2eefc9eb5da6271e811a30ff9dbbe9797323785e..930db398d107f4b1a7310ee399a6ab7e692b89b6 100644 (file)
@@ -49,6 +49,7 @@
 #include <linux/blkpg.h>
 #include <linux/kref.h>
 #include <linux/delay.h>
+#include <linux/mutex.h>
 #include <asm/uaccess.h>
 
 #include <scsi/scsi.h>
@@ -102,6 +103,7 @@ struct scsi_disk {
        u8              write_prot;
        unsigned        WCE : 1;        /* state of disk WCE bit */
        unsigned        RCD : 1;        /* state of disk RCD bit, unused */
+       unsigned        DPOFUA : 1;     /* state of disk DPOFUA bit */
 };
 
 static DEFINE_IDR(sd_index_idr);
@@ -110,7 +112,7 @@ static DEFINE_SPINLOCK(sd_index_lock);
 /* This semaphore is used to mediate the 0->1 reference get in the
  * face of object destruction (i.e. we can't allow a get on an
  * object after last put) */
-static DECLARE_MUTEX(sd_ref_sem);
+static DEFINE_MUTEX(sd_ref_mutex);
 
 static int sd_revalidate_disk(struct gendisk *disk);
 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
@@ -192,9 +194,9 @@ static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
 {
        struct scsi_disk *sdkp;
 
-       down(&sd_ref_sem);
+       mutex_lock(&sd_ref_mutex);
        sdkp = __scsi_disk_get(disk);
-       up(&sd_ref_sem);
+       mutex_unlock(&sd_ref_mutex);
        return sdkp;
 }
 
@@ -202,11 +204,11 @@ static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
 {
        struct scsi_disk *sdkp;
 
-       down(&sd_ref_sem);
+       mutex_lock(&sd_ref_mutex);
        sdkp = dev_get_drvdata(dev);
        if (sdkp)
                sdkp = __scsi_disk_get(sdkp->disk);
-       up(&sd_ref_sem);
+       mutex_unlock(&sd_ref_mutex);
        return sdkp;
 }
 
@@ -214,10 +216,10 @@ static void scsi_disk_put(struct scsi_disk *sdkp)
 {
        struct scsi_device *sdev = sdkp->device;
 
-       down(&sd_ref_sem);
+       mutex_lock(&sd_ref_mutex);
        kref_put(&sdkp->kref, scsi_disk_release);
        scsi_device_put(sdev);
-       up(&sd_ref_sem);
+       mutex_unlock(&sd_ref_mutex);
 }
 
 /**
@@ -230,34 +232,12 @@ static void scsi_disk_put(struct scsi_disk *sdkp)
  **/
 static int sd_init_command(struct scsi_cmnd * SCpnt)
 {
-       unsigned int this_count, timeout;
-       struct gendisk *disk;
-       sector_t block;
        struct scsi_device *sdp = SCpnt->device;
        struct request *rq = SCpnt->request;
-
-       timeout = sdp->timeout;
-
-       /*
-        * SG_IO from block layer already setup, just copy cdb basically
-        */
-       if (blk_pc_request(rq)) {
-               scsi_setup_blk_pc_cmnd(SCpnt);
-               if (rq->timeout)
-                       timeout = rq->timeout;
-
-               goto queue;
-       }
-
-       /*
-        * we only do REQ_CMD and REQ_BLOCK_PC
-        */
-       if (!blk_fs_request(rq))
-               return 0;
-
-       disk = rq->rq_disk;
-       block = rq->sector;
-       this_count = SCpnt->request_bufflen >> 9;
+       struct gendisk *disk = rq->rq_disk;
+       sector_t block = rq->sector;
+       unsigned int this_count = SCpnt->request_bufflen >> 9;
+       unsigned int timeout = sdp->timeout;
 
        SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
                            "count=%d\n", disk->disk_name,
@@ -343,6 +323,7 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
        
        if (block > 0xffffffff) {
                SCpnt->cmnd[0] += READ_16 - READ_6;
+               SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
                SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
                SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
                SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
@@ -362,6 +343,7 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
                        this_count = 0xffff;
 
                SCpnt->cmnd[0] += READ_10 - READ_6;
+               SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
                SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
                SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
                SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
@@ -370,6 +352,17 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
                SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
                SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
        } else {
+               if (unlikely(blk_fua_rq(rq))) {
+                       /*
+                        * This happens only if this drive failed
+                        * 10byte rw command with ILLEGAL_REQUEST
+                        * during operation and thus turned off
+                        * use_10_for_rw.
+                        */
+                       printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n");
+                       return 0;
+               }
+
                SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
                SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
                SCpnt->cmnd[3] = (unsigned char) block & 0xff;
@@ -387,8 +380,6 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
        SCpnt->transfersize = sdp->sector_size;
        SCpnt->underflow = this_count << 9;
        SCpnt->allowed = SD_MAX_RETRIES;
-
-queue:
        SCpnt->timeout_per_command = timeout;
 
        /*
@@ -513,7 +504,7 @@ static int sd_release(struct inode *inode, struct file *filp)
        return 0;
 }
 
-static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *loc)
+static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
 {
        struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
        struct scsi_device *sdp = sdkp->device;
@@ -531,15 +522,9 @@ static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *
        else
                scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
 
-       if (put_user(diskinfo[0], &loc->heads))
-               return -EFAULT;
-       if (put_user(diskinfo[1], &loc->sectors))
-               return -EFAULT;
-       if (put_user(diskinfo[2], &loc->cylinders))
-               return -EFAULT;
-       if (put_user((unsigned)get_start_sect(bdev),
-                    (unsigned long __user *)&loc->start))
-               return -EFAULT;
+       geo->heads = diskinfo[0];
+       geo->sectors = diskinfo[1];
+       geo->cylinders = diskinfo[2];
        return 0;
 }
 
@@ -579,12 +564,6 @@ static int sd_ioctl(struct inode * inode, struct file * filp,
        if (!scsi_block_when_processing_errors(sdp) || !error)
                return error;
 
-       if (cmd == HDIO_GETGEO) {
-               if (!arg)
-                       return -EINVAL;
-               return sd_hdio_getgeo(bdev, p);
-       }
-
        /*
         * Send SCSI addressing ioctls directly to mid level, send other
         * ioctls to block level and then onto mid level if they can't be
@@ -786,6 +765,7 @@ static struct block_device_operations sd_fops = {
        .open                   = sd_open,
        .release                = sd_release,
        .ioctl                  = sd_ioctl,
+       .getgeo                 = sd_getgeo,
 #ifdef CONFIG_COMPAT
        .compat_ioctl           = sd_compat_ioctl,
 #endif
@@ -833,15 +813,7 @@ static void sd_rw_intr(struct scsi_cmnd * SCpnt)
           relatively rare error condition, no care is taken to avoid
           unnecessary additional work such as memcpy's that could be avoided.
         */
-
-       /* 
-        * If SG_IO from block layer then set good_bytes to stop retries;
-        * else if errors, check them, and if necessary prepare for
-        * (partial) retries.
-        */
-       if (blk_pc_request(SCpnt->request))
-               good_bytes = this_count;
-       else if (driver_byte(result) != 0 &&
+       if (driver_byte(result) != 0 &&
                 sense_valid && !sense_deferred) {
                switch (sshdr.sense_key) {
                case MEDIUM_ERROR:
@@ -1395,10 +1367,18 @@ sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
                        sdkp->RCD = 0;
                }
 
+               sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
+               if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
+                       printk(KERN_NOTICE "SCSI device %s: uses "
+                              "READ/WRITE(6), disabling FUA\n", diskname);
+                       sdkp->DPOFUA = 0;
+               }
+
                ct =  sdkp->RCD + 2*sdkp->WCE;
 
-               printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n",
-                      diskname, types[ct]);
+               printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
+                      diskname, types[ct],
+                      sdkp->DPOFUA ? " w/ FUA" : "");
 
                return;
        }
@@ -1475,7 +1455,8 @@ static int sd_revalidate_disk(struct gendisk *disk)
         * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
         */
        if (sdkp->WCE)
-               ordered = QUEUE_ORDERED_DRAIN_FLUSH;
+               ordered = sdkp->DPOFUA
+                       ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
        else
                ordered = QUEUE_ORDERED_DRAIN;
 
@@ -1623,10 +1604,10 @@ static int sd_remove(struct device *dev)
        del_gendisk(sdkp->disk);
        sd_shutdown(dev);
 
-       down(&sd_ref_sem);
+       mutex_lock(&sd_ref_mutex);
        dev_set_drvdata(dev, NULL);
        kref_put(&sdkp->kref, scsi_disk_release);
-       up(&sd_ref_sem);
+       mutex_unlock(&sd_ref_mutex);
 
        return 0;
 }
@@ -1635,7 +1616,7 @@ static int sd_remove(struct device *dev)
  *     scsi_disk_release - Called to free the scsi_disk structure
  *     @kref: pointer to embedded kref
  *
- *     sd_ref_sem must be held entering this routine.  Because it is
+ *     sd_ref_mutex must be held entering this routine.  Because it is
  *     called on last put, you should always use the scsi_disk_get()
  *     scsi_disk_put() helpers which manipulate the semaphore directly
  *     and never do a direct kref_put().