Merge tag 'soc2' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[deliverable/linux.git] / drivers / target / target_core_iblock.c
index c2d4ccbb7133bf09b8e68c13d5c68fdb6ffd78b0..76db75e836ede701c2aed6090a212fdf1a08ad10 100644 (file)
@@ -40,6 +40,7 @@
 #include <linux/module.h>
 #include <scsi/scsi.h>
 #include <scsi/scsi_host.h>
+#include <asm/unaligned.h>
 
 #include <target/target_core_base.h>
 #include <target/target_core_backend.h>
@@ -296,7 +297,7 @@ static void iblock_end_io_flush(struct bio *bio, int err)
  * Implement SYCHRONIZE CACHE.  Note that we can't handle lba ranges and must
  * always flush the whole cache.
  */
-static void iblock_emulate_sync_cache(struct se_cmd *cmd)
+static int iblock_execute_sync_cache(struct se_cmd *cmd)
 {
        struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
        int immed = (cmd->t_task_cdb[1] & 0x2);
@@ -315,15 +316,89 @@ static void iblock_emulate_sync_cache(struct se_cmd *cmd)
        if (!immed)
                bio->bi_private = cmd;
        submit_bio(WRITE_FLUSH, bio);
+       return 0;
 }
 
-static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range)
+static int iblock_execute_unmap(struct se_cmd *cmd)
 {
+       struct se_device *dev = cmd->se_dev;
        struct iblock_dev *ibd = dev->dev_ptr;
-       struct block_device *bd = ibd->ibd_bd;
-       int barrier = 0;
+       unsigned char *buf, *ptr = NULL;
+       sector_t lba;
+       int size = cmd->data_length;
+       u32 range;
+       int ret = 0;
+       int dl, bd_dl;
+
+       buf = transport_kmap_data_sg(cmd);
+
+       dl = get_unaligned_be16(&buf[0]);
+       bd_dl = get_unaligned_be16(&buf[2]);
+
+       size = min(size - 8, bd_dl);
+       if (size / 16 > dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
+               cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
+               ret = -EINVAL;
+               goto err;
+       }
+
+       /* First UNMAP block descriptor starts at 8 byte offset */
+       ptr = &buf[8];
+       pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
+               " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
+
+       while (size >= 16) {
+               lba = get_unaligned_be64(&ptr[0]);
+               range = get_unaligned_be32(&ptr[8]);
+               pr_debug("UNMAP: Using lba: %llu and range: %u\n",
+                                (unsigned long long)lba, range);
+
+               if (range > dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count) {
+                       cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
+                       ret = -EINVAL;
+                       goto err;
+               }
+
+               if (lba + range > dev->transport->get_blocks(dev) + 1) {
+                       cmd->scsi_sense_reason = TCM_ADDRESS_OUT_OF_RANGE;
+                       ret = -EINVAL;
+                       goto err;
+               }
+
+               ret = blkdev_issue_discard(ibd->ibd_bd, lba, range,
+                                          GFP_KERNEL, 0);
+               if (ret < 0) {
+                       pr_err("blkdev_issue_discard() failed: %d\n",
+                                       ret);
+                       goto err;
+               }
+
+               ptr += 16;
+               size -= 16;
+       }
 
-       return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier);
+err:
+       transport_kunmap_data_sg(cmd);
+       if (!ret)
+               target_complete_cmd(cmd, GOOD);
+       return ret;
+}
+
+static int iblock_execute_write_same(struct se_cmd *cmd)
+{
+       struct iblock_dev *ibd = cmd->se_dev->dev_ptr;
+       int ret;
+
+       ret = blkdev_issue_discard(ibd->ibd_bd, cmd->t_task_lba,
+                                  spc_get_write_same_sectors(cmd), GFP_KERNEL,
+                                  0);
+       if (ret < 0) {
+               pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
+               return ret;
+       }
+
+       target_complete_cmd(cmd, GOOD);
+       return 0;
 }
 
 enum {
@@ -514,9 +589,11 @@ static void iblock_submit_bios(struct bio_list *list, int rw)
        blk_finish_plug(&plug);
 }
 
-static int iblock_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
-               u32 sgl_nents, enum dma_data_direction data_direction)
+static int iblock_execute_rw(struct se_cmd *cmd)
 {
+       struct scatterlist *sgl = cmd->t_data_sg;
+       u32 sgl_nents = cmd->t_data_nents;
+       enum dma_data_direction data_direction = cmd->data_direction;
        struct se_device *dev = cmd->se_dev;
        struct iblock_req *ibr;
        struct bio *bio;
@@ -663,6 +740,18 @@ static void iblock_bio_done(struct bio *bio, int err)
        iblock_complete_cmd(cmd);
 }
 
+static struct spc_ops iblock_spc_ops = {
+       .execute_rw             = iblock_execute_rw,
+       .execute_sync_cache     = iblock_execute_sync_cache,
+       .execute_write_same     = iblock_execute_write_same,
+       .execute_unmap          = iblock_execute_unmap,
+};
+
+static int iblock_parse_cdb(struct se_cmd *cmd)
+{
+       return sbc_parse_cdb(cmd, &iblock_spc_ops);
+}
+
 static struct se_subsystem_api iblock_template = {
        .name                   = "iblock",
        .owner                  = THIS_MODULE,
@@ -674,10 +763,7 @@ static struct se_subsystem_api iblock_template = {
        .allocate_virtdevice    = iblock_allocate_virtdevice,
        .create_virtdevice      = iblock_create_virtdevice,
        .free_device            = iblock_free_device,
-       .parse_cdb              = sbc_parse_cdb,
-       .execute_cmd            = iblock_execute_cmd,
-       .do_discard             = iblock_do_discard,
-       .do_sync_cache          = iblock_emulate_sync_cache,
+       .parse_cdb              = iblock_parse_cdb,
        .check_configfs_dev_params = iblock_check_configfs_dev_params,
        .set_configfs_dev_params = iblock_set_configfs_dev_params,
        .show_configfs_dev_params = iblock_show_configfs_dev_params,
This page took 0.025606 seconds and 5 git commands to generate.