Merge branch 'master' into for-next
[linux-flexiantxendom0-natty.git] / drivers / scsi / scsi_sysfs.c
index 5a06505..8dc1f00 100644 (file)
@@ -7,9 +7,11 @@
  */
 
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/device.h>
+#include <linux/pm_runtime.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_device.h>
@@ -249,6 +251,7 @@ shost_rd_attr(host_busy, "%hu\n");
 shost_rd_attr(cmd_per_lun, "%hd\n");
 shost_rd_attr(can_queue, "%hd\n");
 shost_rd_attr(sg_tablesize, "%hu\n");
+shost_rd_attr(sg_prot_tablesize, "%hu\n");
 shost_rd_attr(unchecked_isa_dma, "%d\n");
 shost_rd_attr(prot_capabilities, "%u\n");
 shost_rd_attr(prot_guard_type, "%hd\n");
@@ -260,6 +263,7 @@ static struct attribute *scsi_sysfs_shost_attrs[] = {
        &dev_attr_cmd_per_lun.attr,
        &dev_attr_can_queue.attr,
        &dev_attr_sg_tablesize.attr,
+       &dev_attr_sg_prot_tablesize.attr,
        &dev_attr_unchecked_isa_dma.attr,
        &dev_attr_proc_name.attr,
        &dev_attr_scan.attr,
@@ -375,57 +379,13 @@ static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
        return 0;
 }
 
-static int scsi_bus_suspend(struct device * dev, pm_message_t state)
-{
-       struct device_driver *drv;
-       struct scsi_device *sdev;
-       int err;
-
-       if (dev->type != &scsi_dev_type)
-               return 0;
-
-       drv = dev->driver;
-       sdev = to_scsi_device(dev);
-
-       err = scsi_device_quiesce(sdev);
-       if (err)
-               return err;
-
-       if (drv && drv->suspend) {
-               err = drv->suspend(dev, state);
-               if (err)
-                       return err;
-       }
-
-       return 0;
-}
-
-static int scsi_bus_resume(struct device * dev)
-{
-       struct device_driver *drv;
-       struct scsi_device *sdev;
-       int err = 0;
-
-       if (dev->type != &scsi_dev_type)
-               return 0;
-
-       drv = dev->driver;
-       sdev = to_scsi_device(dev);
-
-       if (drv && drv->resume)
-               err = drv->resume(dev);
-
-       scsi_device_resume(sdev);
-
-       return err;
-}
-
 struct bus_type scsi_bus_type = {
         .name          = "scsi",
         .match         = scsi_bus_match,
        .uevent         = scsi_bus_uevent,
-       .suspend        = scsi_bus_suspend,
-       .resume         = scsi_bus_resume,
+#ifdef CONFIG_PM_OPS
+       .pm             = &scsi_bus_pm_ops,
+#endif
 };
 EXPORT_SYMBOL_GPL(scsi_bus_type);
 
@@ -473,7 +433,7 @@ static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
 
 
 /*
- * sdev_rd_attr: create a function and attribute variable for a
+ * sdev_rw_attr: create a function and attribute variable for a
  * read/write field.
  */
 #define sdev_rw_attr(field, format_string)                             \
@@ -485,7 +445,7 @@ sdev_store_##field (struct device *dev, struct device_attribute *attr,      \
 {                                                                      \
        struct scsi_device *sdev;                                       \
        sdev = to_scsi_device(dev);                                     \
-       snscanf (buf, 20, format_string, &sdev->field);                 \
+       sscanf (buf, format_string, &sdev->field);                      \
        return count;                                                   \
 }                                                                      \
 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
@@ -850,14 +810,15 @@ static int scsi_target_add(struct scsi_target *starget)
        error = device_add(&starget->dev);
        if (error) {
                dev_err(&starget->dev, "target device_add failed, error %d\n", error);
-               get_device(&starget->dev);
-               scsi_target_reap(starget);
-               put_device(&starget->dev);
                return error;
        }
        transport_add_device(&starget->dev);
        starget->state = STARGET_RUNNING;
 
+       pm_runtime_set_active(&starget->dev);
+       pm_runtime_enable(&starget->dev);
+       device_enable_async_suspend(&starget->dev);
+
        return 0;
 }
 
@@ -878,7 +839,8 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
        struct request_queue *rq = sdev->request_queue;
        struct scsi_target *starget = sdev->sdev_target;
 
-       if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
+       error = scsi_device_set_state(sdev, SDEV_RUNNING);
+       if (error)
                return error;
 
        error = scsi_target_add(starget);
@@ -886,16 +848,33 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
                return error;
 
        transport_configure_device(&starget->dev);
+
+       device_enable_async_suspend(&sdev->sdev_gendev);
+       scsi_autopm_get_target(starget);
+       pm_runtime_set_active(&sdev->sdev_gendev);
+       pm_runtime_forbid(&sdev->sdev_gendev);
+       pm_runtime_enable(&sdev->sdev_gendev);
+       scsi_autopm_put_target(starget);
+
+       /* The following call will keep sdev active indefinitely, until
+        * its driver does a corresponding scsi_autopm_pm_device().  Only
+        * drivers supporting autosuspend will do this.
+        */
+       scsi_autopm_get_device(sdev);
+
        error = device_add(&sdev->sdev_gendev);
        if (error) {
-               printk(KERN_INFO "error 1\n");
-               goto out_remove;
+               sdev_printk(KERN_INFO, sdev,
+                               "failed to add device: %d\n", error);
+               return error;
        }
+       device_enable_async_suspend(&sdev->sdev_dev);
        error = device_add(&sdev->sdev_dev);
        if (error) {
-               printk(KERN_INFO "error 2\n");
+               sdev_printk(KERN_INFO, sdev,
+                               "failed to add class device: %d\n", error);
                device_del(&sdev->sdev_gendev);
-               goto out_remove;
+               return error;
        }
        transport_add_device(&sdev->sdev_gendev);
        sdev->is_visible = 1;
@@ -910,14 +889,14 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
        else
                error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
        if (error)
-               goto out_remove;
+               return error;
 
        if (sdev->host->hostt->change_queue_type)
                error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
        else
                error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
        if (error)
-               goto out_remove;
+               return error;
 
        error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
 
@@ -933,16 +912,11 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
                        error = device_create_file(&sdev->sdev_gendev,
                                        sdev->host->hostt->sdev_attrs[i]);
                        if (error)
-                               goto out_remove;
+                               return error;
                }
        }
 
-       return 0;
-
- out_remove:
-       __scsi_remove_device(sdev);
        return error;
-
 }
 
 void __scsi_remove_device(struct scsi_device *sdev)
@@ -992,10 +966,11 @@ static void __scsi_remove_target(struct scsi_target *starget)
        list_for_each_entry(sdev, &shost->__devices, siblings) {
                if (sdev->channel != starget->channel ||
                    sdev->id != starget->id ||
-                   sdev->sdev_state == SDEV_DEL)
+                   scsi_device_get(sdev))
                        continue;
                spin_unlock_irqrestore(shost->host_lock, flags);
                scsi_remove_device(sdev);
+               scsi_device_put(sdev);
                spin_lock_irqsave(shost->host_lock, flags);
                goto restart;
        }