block: Add bio_end_sector()
[linux-3.10.git] / drivers / s390 / block / dcssblk.c
index b21caf1..12d08b4 100644 (file)
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/blkdev.h>
-#include <asm/extmem.h>
-#include <asm/io.h>
 #include <linux/completion.h>
 #include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <asm/extmem.h>
+#include <asm/io.h>
 
 #define DCSSBLK_NAME "dcssblk"
 #define DCSSBLK_MINORS_PER_DISK 1
 
 static int dcssblk_open(struct block_device *bdev, fmode_t mode);
 static int dcssblk_release(struct gendisk *disk, fmode_t mode);
-static int dcssblk_make_request(struct request_queue *q, struct bio *bio);
+static void dcssblk_make_request(struct request_queue *q, struct bio *bio);
 static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
                                 void **kaddr, unsigned long *pfn);
 
 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
 
 static int dcssblk_major;
-static struct block_device_operations dcssblk_devops = {
+static const struct block_device_operations dcssblk_devops = {
        .owner          = THIS_MODULE,
        .open           = dcssblk_open,
        .release        = dcssblk_release,
@@ -68,23 +69,9 @@ static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *a
                                  size_t count);
 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
                                  size_t count);
-static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
-                                 size_t count);
-static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
-static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
-                                 size_t count);
-static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
-static ssize_t dcssblk_seglist_show(struct device *dev,
-                               struct device_attribute *attr,
-                               char *buf);
 
 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
-static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
-                  dcssblk_save_store);
-static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
-                  dcssblk_shared_store);
-static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
 
 static struct device *dcssblk_root_dev;
 
@@ -415,6 +402,8 @@ out:
        up_write(&dcssblk_devices_sem);
        return rc;
 }
+static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
+                  dcssblk_shared_store);
 
 /*
  * device attribute for save operation on current copy
@@ -475,6 +464,8 @@ dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char
        up_write(&dcssblk_devices_sem);
        return count;
 }
+static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
+                  dcssblk_save_store);
 
 /*
  * device attribute for showing all segments in a device
@@ -501,6 +492,21 @@ dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
        up_read(&dcssblk_devices_sem);
        return i;
 }
+static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
+
+static struct attribute *dcssblk_dev_attrs[] = {
+       &dev_attr_shared.attr,
+       &dev_attr_save.attr,
+       &dev_attr_seglist.attr,
+       NULL,
+};
+static struct attribute_group dcssblk_dev_attr_group = {
+       .attrs = dcssblk_dev_attrs,
+};
+static const struct attribute_group *dcssblk_dev_attr_groups[] = {
+       &dcssblk_dev_attr_group,
+       NULL,
+};
 
 /*
  * device attribute for adding devices
@@ -589,6 +595,7 @@ dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char
 
        dev_set_name(&dev_info->dev, dev_info->segment_name);
        dev_info->dev.release = dcssblk_release_segment;
+       dev_info->dev.groups = dcssblk_dev_attr_groups;
        INIT_LIST_HEAD(&dev_info->lh);
        dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
        if (dev_info->gd == NULL) {
@@ -636,21 +643,10 @@ dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char
         * register the device
         */
        rc = device_register(&dev_info->dev);
-       if (rc) {
-               module_put(THIS_MODULE);
-               goto dev_list_del;
-       }
-       get_device(&dev_info->dev);
-       rc = device_create_file(&dev_info->dev, &dev_attr_shared);
        if (rc)
-               goto unregister_dev;
-       rc = device_create_file(&dev_info->dev, &dev_attr_save);
-       if (rc)
-               goto unregister_dev;
-       rc = device_create_file(&dev_info->dev, &dev_attr_seglist);
-       if (rc)
-               goto unregister_dev;
+               goto put_dev;
 
+       get_device(&dev_info->dev);
        add_disk(dev_info->gd);
 
        switch (dev_info->segment_type) {
@@ -667,12 +663,11 @@ dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char
        rc = count;
        goto out;
 
-unregister_dev:
+put_dev:
        list_del(&dev_info->lh);
        blk_cleanup_queue(dev_info->dcssblk_queue);
        dev_info->gd->queue = NULL;
        put_disk(dev_info->gd);
-       device_unregister(&dev_info->dev);
        list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
                segment_unload(seg_info->segment_name);
        }
@@ -813,7 +808,7 @@ out:
        return rc;
 }
 
-static int
+static void
 dcssblk_make_request(struct request_queue *q, struct bio *bio)
 {
        struct dcssblk_dev_info *dev_info;
@@ -831,8 +826,7 @@ dcssblk_make_request(struct request_queue *q, struct bio *bio)
        if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
                /* Request is not page-aligned. */
                goto fail;
-       if (((bio->bi_size >> 9) + bio->bi_sector)
-                       > get_capacity(bio->bi_bdev->bd_disk)) {
+       if (bio_end_sector(bio) > get_capacity(bio->bi_bdev->bd_disk)) {
                /* Request beyond end of DCSS segment. */
                goto fail;
        }
@@ -870,10 +864,9 @@ dcssblk_make_request(struct request_queue *q, struct bio *bio)
                bytes_done += bvec->bv_len;
        }
        bio_endio(bio, 0);
-       return 0;
+       return;
 fail:
        bio_io_error(bio);
-       return 0;
 }
 
 static int
@@ -940,11 +933,95 @@ dcssblk_check_params(void)
 }
 
 /*
+ * Suspend / Resume
+ */
+static int dcssblk_freeze(struct device *dev)
+{
+       struct dcssblk_dev_info *dev_info;
+       int rc = 0;
+
+       list_for_each_entry(dev_info, &dcssblk_devices, lh) {
+               switch (dev_info->segment_type) {
+                       case SEG_TYPE_SR:
+                       case SEG_TYPE_ER:
+                       case SEG_TYPE_SC:
+                               if (!dev_info->is_shared)
+                                       rc = -EINVAL;
+                               break;
+                       default:
+                               rc = -EINVAL;
+                               break;
+               }
+               if (rc)
+                       break;
+       }
+       if (rc)
+               pr_err("Suspending the system failed because DCSS device %s "
+                      "is writable\n",
+                      dev_info->segment_name);
+       return rc;
+}
+
+static int dcssblk_restore(struct device *dev)
+{
+       struct dcssblk_dev_info *dev_info;
+       struct segment_info *entry;
+       unsigned long start, end;
+       int rc = 0;
+
+       list_for_each_entry(dev_info, &dcssblk_devices, lh) {
+               list_for_each_entry(entry, &dev_info->seg_list, lh) {
+                       segment_unload(entry->segment_name);
+                       rc = segment_load(entry->segment_name, SEGMENT_SHARED,
+                                         &start, &end);
+                       if (rc < 0) {
+// TODO in_use check ?
+                               segment_warning(rc, entry->segment_name);
+                               goto out_panic;
+                       }
+                       if (start != entry->start || end != entry->end) {
+                               pr_err("The address range of DCSS %s changed "
+                                      "while the system was suspended\n",
+                                      entry->segment_name);
+                               goto out_panic;
+                       }
+               }
+       }
+       return 0;
+out_panic:
+       panic("fatal dcssblk resume error\n");
+}
+
+static int dcssblk_thaw(struct device *dev)
+{
+       return 0;
+}
+
+static const struct dev_pm_ops dcssblk_pm_ops = {
+       .freeze         = dcssblk_freeze,
+       .thaw           = dcssblk_thaw,
+       .restore        = dcssblk_restore,
+};
+
+static struct platform_driver dcssblk_pdrv = {
+       .driver = {
+               .name   = "dcssblk",
+               .owner  = THIS_MODULE,
+               .pm     = &dcssblk_pm_ops,
+       },
+};
+
+static struct platform_device *dcssblk_pdev;
+
+
+/*
  * The init/exit functions.
  */
 static void __exit
 dcssblk_exit(void)
 {
+       platform_device_unregister(dcssblk_pdev);
+       platform_driver_unregister(&dcssblk_pdrv);
        root_device_unregister(dcssblk_root_dev);
        unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
 }
@@ -954,30 +1031,44 @@ dcssblk_init(void)
 {
        int rc;
 
-       dcssblk_root_dev = root_device_register("dcssblk");
-       if (IS_ERR(dcssblk_root_dev))
-               return PTR_ERR(dcssblk_root_dev);
-       rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
-       if (rc) {
-               root_device_unregister(dcssblk_root_dev);
+       rc = platform_driver_register(&dcssblk_pdrv);
+       if (rc)
                return rc;
+
+       dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
+                                                       0);
+       if (IS_ERR(dcssblk_pdev)) {
+               rc = PTR_ERR(dcssblk_pdev);
+               goto out_pdrv;
        }
-       rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
-       if (rc) {
-               root_device_unregister(dcssblk_root_dev);
-               return rc;
+
+       dcssblk_root_dev = root_device_register("dcssblk");
+       if (IS_ERR(dcssblk_root_dev)) {
+               rc = PTR_ERR(dcssblk_root_dev);
+               goto out_pdev;
        }
+       rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
+       if (rc)
+               goto out_root;
+       rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
+       if (rc)
+               goto out_root;
        rc = register_blkdev(0, DCSSBLK_NAME);
-       if (rc < 0) {
-               root_device_unregister(dcssblk_root_dev);
-               return rc;
-       }
+       if (rc < 0)
+               goto out_root;
        dcssblk_major = rc;
        init_rwsem(&dcssblk_devices_sem);
 
        dcssblk_check_params();
-
        return 0;
+
+out_root:
+       root_device_unregister(dcssblk_root_dev);
+out_pdev:
+       platform_device_unregister(dcssblk_pdev);
+out_pdrv:
+       platform_driver_unregister(&dcssblk_pdrv);
+       return rc;
 }
 
 module_init(dcssblk_init);