diff options
-rw-r--r-- | drivers/target/target_core_iblock.c | 50 | ||||
-rw-r--r-- | drivers/target/target_core_sbc.c | 62 | ||||
-rw-r--r-- | include/target/target_core_backend.h | 2 |
3 files changed, 50 insertions, 64 deletions
diff --git a/drivers/target/target_core_iblock.c b/drivers/target/target_core_iblock.c index ee70cc9..8a12e29 100644 --- a/drivers/target/target_core_iblock.c +++ b/drivers/target/target_core_iblock.c @@ -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> @@ -318,13 +319,52 @@ static int iblock_execute_sync_cache(struct se_cmd *cmd) 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; + unsigned char *cdb = &cmd->t_task_cdb[0]; + sector_t lba; + unsigned int size = cmd->data_length, range; + int ret = 0, offset; + unsigned short dl, bd_dl; + + /* First UNMAP block descriptor starts at 8 byte offset */ + offset = 8; + size -= 8; + dl = get_unaligned_be16(&cdb[0]); + bd_dl = get_unaligned_be16(&cdb[2]); + + buf = transport_kmap_data_sg(cmd); + + ptr = &buf[offset]; + pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu" + " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); + + while (size) { + 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); + + 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) @@ -687,6 +727,7 @@ 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) @@ -706,7 +747,6 @@ static struct se_subsystem_api iblock_template = { .create_virtdevice = iblock_create_virtdevice, .free_device = iblock_free_device, .parse_cdb = iblock_parse_cdb, - .do_discard = iblock_do_discard, .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, diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c index 146ca37..a9dd946 100644 --- a/drivers/target/target_core_sbc.c +++ b/drivers/target/target_core_sbc.c @@ -99,63 +99,6 @@ static int sbc_emulate_readcapacity_16(struct se_cmd *cmd) return 0; } -/* - * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support. - * Note this is not used for TCM/pSCSI passthrough - */ -static int sbc_emulate_unmap(struct se_cmd *cmd) -{ - struct se_device *dev = cmd->se_dev; - unsigned char *buf, *ptr = NULL; - unsigned char *cdb = &cmd->t_task_cdb[0]; - sector_t lba; - unsigned int size = cmd->data_length, range; - int ret = 0, offset; - unsigned short dl, bd_dl; - - if (!dev->transport->do_discard) { - pr_err("UNMAP emulation not supported for: %s\n", - dev->transport->name); - cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; - return -ENOSYS; - } - - /* First UNMAP block descriptor starts at 8 byte offset */ - offset = 8; - size -= 8; - dl = get_unaligned_be16(&cdb[0]); - bd_dl = get_unaligned_be16(&cdb[2]); - - buf = transport_kmap_data_sg(cmd); - - ptr = &buf[offset]; - pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu" - " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); - - while (size) { - 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); - - ret = dev->transport->do_discard(dev, lba, range); - if (ret < 0) { - pr_err("blkdev_issue_discard() failed: %d\n", - ret); - goto err; - } - - ptr += 16; - size -= 16; - } - -err: - transport_kunmap_data_sg(cmd); - if (!ret) - target_complete_cmd(cmd, GOOD); - return ret; -} - int spc_get_write_same_sectors(struct se_cmd *cmd) { u32 num_blocks; @@ -533,8 +476,11 @@ int sbc_parse_cdb(struct se_cmd *cmd, struct spc_ops *ops) cmd->execute_cmd = ops->execute_sync_cache; break; case UNMAP: + if (!ops->execute_unmap) + goto out_unsupported_cdb; + size = get_unaligned_be16(&cdb[7]); - cmd->execute_cmd = sbc_emulate_unmap; + cmd->execute_cmd = ops->execute_unmap; break; case WRITE_SAME_16: if (!ops->execute_write_same) diff --git a/include/target/target_core_backend.h b/include/target/target_core_backend.h index fbc6a1b..f1405d33 100644 --- a/include/target/target_core_backend.h +++ b/include/target/target_core_backend.h @@ -26,7 +26,6 @@ struct se_subsystem_api { int (*transport_complete)(struct se_cmd *cmd, struct scatterlist *); int (*parse_cdb)(struct se_cmd *cmd); - int (*do_discard)(struct se_device *, sector_t, u32); ssize_t (*check_configfs_dev_params)(struct se_hba *, struct se_subsystem_dev *); ssize_t (*set_configfs_dev_params)(struct se_hba *, @@ -43,6 +42,7 @@ struct spc_ops { int (*execute_rw)(struct se_cmd *cmd); int (*execute_sync_cache)(struct se_cmd *cmd); int (*execute_write_same)(struct se_cmd *cmd); + int (*execute_unmap)(struct se_cmd *cmd); }; int transport_subsystem_register(struct se_subsystem_api *); |