IPoIB/cm: Fix performance regression on Mellanox
[safe/jmp/linux-2.6] / drivers / scsi / sd.c
index 12e18bb..3d8c9cb 100644 (file)
@@ -82,6 +82,9 @@ MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
+MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
+MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
+MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
 
 static DEFINE_IDR(sd_index_idr);
 static DEFINE_SPINLOCK(sd_index_lock);
@@ -147,6 +150,20 @@ static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
        return count;
 }
 
+static ssize_t sd_store_manage_start_stop(struct class_device *cdev,
+                                         const char *buf, size_t count)
+{
+       struct scsi_disk *sdkp = to_scsi_disk(cdev);
+       struct scsi_device *sdp = sdkp->device;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EACCES;
+
+       sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
+
+       return count;
+}
+
 static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
                                      size_t count)
 {
@@ -179,6 +196,14 @@ static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
        return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
 }
 
+static ssize_t sd_show_manage_start_stop(struct class_device *cdev, char *buf)
+{
+       struct scsi_disk *sdkp = to_scsi_disk(cdev);
+       struct scsi_device *sdp = sdkp->device;
+
+       return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
+}
+
 static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
 {
        struct scsi_disk *sdkp = to_scsi_disk(cdev);
@@ -192,6 +217,8 @@ static struct class_device_attribute sd_disk_attrs[] = {
        __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
        __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
               sd_store_allow_restart),
+       __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
+              sd_store_manage_start_stop),
        __ATTR_NULL,
 };
 
@@ -208,6 +235,8 @@ static struct scsi_driver sd_template = {
                .name           = "sd",
                .probe          = sd_probe,
                .remove         = sd_remove,
+               .suspend        = sd_suspend,
+               .resume         = sd_resume,
                .shutdown       = sd_shutdown,
        },
        .rescan                 = sd_rescan,
@@ -766,7 +795,9 @@ static int sd_sync_cache(struct scsi_disk *sdkp)
                        sd_print_sense_hdr(sdkp, &sshdr);
        }
 
-       return res;
+       if (res)
+               return -EIO;
+       return 0;
 }
 
 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
@@ -1705,6 +1736,31 @@ static void scsi_disk_release(struct class_device *cdev)
        kfree(sdkp);
 }
 
+static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
+{
+       unsigned char cmd[6] = { START_STOP };  /* START_VALID */
+       struct scsi_sense_hdr sshdr;
+       struct scsi_device *sdp = sdkp->device;
+       int res;
+
+       if (start)
+               cmd[4] |= 1;    /* START */
+
+       if (!scsi_device_online(sdp))
+               return -ENODEV;
+
+       res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
+                              SD_TIMEOUT, SD_MAX_RETRIES);
+       if (res) {
+               sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
+               sd_print_result(sdkp, res);
+               if (driver_byte(res) & DRIVER_SENSE)
+                       sd_print_sense_hdr(sdkp, &sshdr);
+       }
+
+       return res;
+}
+
 /*
  * Send a SYNCHRONIZE CACHE instruction down to the device through
  * the normal SCSI command structure.  Wait for the command to
@@ -1721,9 +1777,57 @@ static void sd_shutdown(struct device *dev)
                sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
                sd_sync_cache(sdkp);
        }
+
+       if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
+               sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
+               sd_start_stop_device(sdkp, 0);
+       }
+
        scsi_disk_put(sdkp);
 }
 
+static int sd_suspend(struct device *dev, pm_message_t mesg)
+{
+       struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
+       int ret = 0;
+
+       if (!sdkp)
+               return 0;       /* this can happen */
+
+       if (sdkp->WCE) {
+               sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
+               ret = sd_sync_cache(sdkp);
+               if (ret)
+                       goto done;
+       }
+
+       if (mesg.event == PM_EVENT_SUSPEND &&
+           sdkp->device->manage_start_stop) {
+               sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
+               ret = sd_start_stop_device(sdkp, 0);
+       }
+
+done:
+       scsi_disk_put(sdkp);
+       return ret;
+}
+
+static int sd_resume(struct device *dev)
+{
+       struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
+       int ret = 0;
+
+       if (!sdkp->device->manage_start_stop)
+               goto done;
+
+       sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
+       ret = sd_start_stop_device(sdkp, 1);
+
+done:
+       scsi_disk_put(sdkp);
+       return ret;
+}
+
 /**
  *     init_sd - entry point for this driver (both when built in or when
  *     a module).