aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbstroesser@ts.fujitsu.com <bstroesser@ts.fujitsu.com>2018-05-24 12:49:41 -0400
committerMartin K. Petersen <martin.petersen@oracle.com>2018-06-18 21:02:52 -0400
commit6c3796d130ed2860489885a934dcb7bb334d5eb0 (patch)
treededd0e60785b14c5b90139556f725999a4d25422
parentce397d215ccd07b8ae3f71db689aedb85d56ab40 (diff)
scsi: target: tcmu: add read length support
Generally target core and TCMUser seem to work fine for tape devices and media changers. But there is at least one situation where TCMUser is not able to support sequential access device emulation correctly. The situation is when an initiator sends a SCSI READ CDB with a length that is greater than the length of the tape block to read. We can distinguish two subcases: A) The initiator sent the READ CDB with the SILI bit being set. In this case the sequential access device has to transfer the data from the tape block (only the length of the tape block) and transmit a good status. The current interface between TCMUser and the userspace does not support reduction of the read data size by the userspace program. The patch below fixes this subcase by allowing the userspace program to specify a reduced data size in read direction. B) The initiator sent the READ CDB with the SILI bit not being set. In this case the sequential access device has to transfer the data from the tape block as in A), but additionally has to transmit CHECK CONDITION with the ILI bit set and NO SENSE in the sensebytes. The information field in the sensebytes must contain the residual count. With the below patch a user space program can specify the real read data length and appropriate sensebytes. TCMUser then uses the se_cmd flag SCF_TREAT_READ_AS_NORMAL, to force target core to transmit the real data size and the sensebytes. Note: the flag SCF_TREAT_READ_AS_NORMAL is introduced by Lee Duncan's patch "[PATCH v4] target: transport should handle st FM/EOM/ILI reads" from Tue, 15 May 2018 18:25:24 -0700. Signed-off-by: Bodo Stroesser <bstroesser@ts.fujitsu.com> Acked-by: Mike Christie <mchristi@redhat.com> Reviewed-by: Lee Duncan <lduncan@suse.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--drivers/target/target_core_user.c44
-rw-r--r--include/uapi/linux/target_core_user.h4
2 files changed, 39 insertions, 9 deletions
diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c
index 7f96dfa32b9c..d8dc3d22051f 100644
--- a/drivers/target/target_core_user.c
+++ b/drivers/target/target_core_user.c
@@ -656,7 +656,7 @@ static void scatter_data_area(struct tcmu_dev *udev,
656} 656}
657 657
658static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, 658static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
659 bool bidi) 659 bool bidi, uint32_t read_len)
660{ 660{
661 struct se_cmd *se_cmd = cmd->se_cmd; 661 struct se_cmd *se_cmd = cmd->se_cmd;
662 int i, dbi; 662 int i, dbi;
@@ -689,7 +689,7 @@ static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
689 for_each_sg(data_sg, sg, data_nents, i) { 689 for_each_sg(data_sg, sg, data_nents, i) {
690 int sg_remaining = sg->length; 690 int sg_remaining = sg->length;
691 to = kmap_atomic(sg_page(sg)) + sg->offset; 691 to = kmap_atomic(sg_page(sg)) + sg->offset;
692 while (sg_remaining > 0) { 692 while (sg_remaining > 0 && read_len > 0) {
693 if (block_remaining == 0) { 693 if (block_remaining == 0) {
694 if (from) 694 if (from)
695 kunmap_atomic(from); 695 kunmap_atomic(from);
@@ -701,6 +701,8 @@ static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
701 } 701 }
702 copy_bytes = min_t(size_t, sg_remaining, 702 copy_bytes = min_t(size_t, sg_remaining,
703 block_remaining); 703 block_remaining);
704 if (read_len < copy_bytes)
705 copy_bytes = read_len;
704 offset = DATA_BLOCK_SIZE - block_remaining; 706 offset = DATA_BLOCK_SIZE - block_remaining;
705 tcmu_flush_dcache_range(from, copy_bytes); 707 tcmu_flush_dcache_range(from, copy_bytes);
706 memcpy(to + sg->length - sg_remaining, from + offset, 708 memcpy(to + sg->length - sg_remaining, from + offset,
@@ -708,8 +710,11 @@ static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
708 710
709 sg_remaining -= copy_bytes; 711 sg_remaining -= copy_bytes;
710 block_remaining -= copy_bytes; 712 block_remaining -= copy_bytes;
713 read_len -= copy_bytes;
711 } 714 }
712 kunmap_atomic(to - sg->offset); 715 kunmap_atomic(to - sg->offset);
716 if (read_len == 0)
717 break;
713 } 718 }
714 if (from) 719 if (from)
715 kunmap_atomic(from); 720 kunmap_atomic(from);
@@ -1042,6 +1047,8 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *
1042{ 1047{
1043 struct se_cmd *se_cmd = cmd->se_cmd; 1048 struct se_cmd *se_cmd = cmd->se_cmd;
1044 struct tcmu_dev *udev = cmd->tcmu_dev; 1049 struct tcmu_dev *udev = cmd->tcmu_dev;
1050 bool read_len_valid = false;
1051 uint32_t read_len = se_cmd->data_length;
1045 1052
1046 /* 1053 /*
1047 * cmd has been completed already from timeout, just reclaim 1054 * cmd has been completed already from timeout, just reclaim
@@ -1056,13 +1063,28 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *
1056 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", 1063 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
1057 cmd->se_cmd); 1064 cmd->se_cmd);
1058 entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; 1065 entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
1059 } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { 1066 goto done;
1067 }
1068
1069 if (se_cmd->data_direction == DMA_FROM_DEVICE &&
1070 (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) {
1071 read_len_valid = true;
1072 if (entry->rsp.read_len < read_len)
1073 read_len = entry->rsp.read_len;
1074 }
1075
1076 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1060 transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); 1077 transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
1061 } else if (se_cmd->se_cmd_flags & SCF_BIDI) { 1078 if (!read_len_valid )
1079 goto done;
1080 else
1081 se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
1082 }
1083 if (se_cmd->se_cmd_flags & SCF_BIDI) {
1062 /* Get Data-In buffer before clean up */ 1084 /* Get Data-In buffer before clean up */
1063 gather_data_area(udev, cmd, true); 1085 gather_data_area(udev, cmd, true, read_len);
1064 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { 1086 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
1065 gather_data_area(udev, cmd, false); 1087 gather_data_area(udev, cmd, false, read_len);
1066 } else if (se_cmd->data_direction == DMA_TO_DEVICE) { 1088 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1067 /* TODO: */ 1089 /* TODO: */
1068 } else if (se_cmd->data_direction != DMA_NONE) { 1090 } else if (se_cmd->data_direction != DMA_NONE) {
@@ -1070,7 +1092,13 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *
1070 se_cmd->data_direction); 1092 se_cmd->data_direction);
1071 } 1093 }
1072 1094
1073 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); 1095done:
1096 if (read_len_valid) {
1097 pr_debug("read_len = %d\n", read_len);
1098 target_complete_cmd_with_length(cmd->se_cmd,
1099 entry->rsp.scsi_status, read_len);
1100 } else
1101 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
1074 1102
1075out: 1103out:
1076 cmd->se_cmd = NULL; 1104 cmd->se_cmd = NULL;
@@ -1740,7 +1768,7 @@ static int tcmu_configure_device(struct se_device *dev)
1740 /* Initialise the mailbox of the ring buffer */ 1768 /* Initialise the mailbox of the ring buffer */
1741 mb = udev->mb_addr; 1769 mb = udev->mb_addr;
1742 mb->version = TCMU_MAILBOX_VERSION; 1770 mb->version = TCMU_MAILBOX_VERSION;
1743 mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC; 1771 mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | TCMU_MAILBOX_FLAG_CAP_READ_LEN;
1744 mb->cmdr_off = CMDR_OFF; 1772 mb->cmdr_off = CMDR_OFF;
1745 mb->cmdr_size = udev->cmdr_size; 1773 mb->cmdr_size = udev->cmdr_size;
1746 1774
diff --git a/include/uapi/linux/target_core_user.h b/include/uapi/linux/target_core_user.h
index 6e299349b158..b7b57967d90f 100644
--- a/include/uapi/linux/target_core_user.h
+++ b/include/uapi/linux/target_core_user.h
@@ -44,6 +44,7 @@
44#define TCMU_MAILBOX_VERSION 2 44#define TCMU_MAILBOX_VERSION 2
45#define ALIGN_SIZE 64 /* Should be enough for most CPUs */ 45#define ALIGN_SIZE 64 /* Should be enough for most CPUs */
46#define TCMU_MAILBOX_FLAG_CAP_OOOC (1 << 0) /* Out-of-order completions */ 46#define TCMU_MAILBOX_FLAG_CAP_OOOC (1 << 0) /* Out-of-order completions */
47#define TCMU_MAILBOX_FLAG_CAP_READ_LEN (1 << 1) /* Read data length */
47 48
48struct tcmu_mailbox { 49struct tcmu_mailbox {
49 __u16 version; 50 __u16 version;
@@ -71,6 +72,7 @@ struct tcmu_cmd_entry_hdr {
71 __u16 cmd_id; 72 __u16 cmd_id;
72 __u8 kflags; 73 __u8 kflags;
73#define TCMU_UFLAG_UNKNOWN_OP 0x1 74#define TCMU_UFLAG_UNKNOWN_OP 0x1
75#define TCMU_UFLAG_READ_LEN 0x2
74 __u8 uflags; 76 __u8 uflags;
75 77
76} __packed; 78} __packed;
@@ -119,7 +121,7 @@ struct tcmu_cmd_entry {
119 __u8 scsi_status; 121 __u8 scsi_status;
120 __u8 __pad1; 122 __u8 __pad1;
121 __u16 __pad2; 123 __u16 __pad2;
122 __u32 __pad3; 124 __u32 read_len;
123 char sense_buffer[TCMU_SENSE_BUFFERSIZE]; 125 char sense_buffer[TCMU_SENSE_BUFFERSIZE];
124 } rsp; 126 } rsp;
125 }; 127 };