Skip to contentSkip to conversation

scsi: target: Set correct residual data

PATCH RESEND Applied Anastasia Kovaleva 2 participants 7 messages Dec 3, 2020 – Jan 29, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover scsi: target: Set correct residual data

Hi Martin,
Please apply the changes to 5.11/scsi-queue at your earliest convenience.

The series changes the behavior of the target in regard to processing commands with overflow/underflow in accordance with the standarts.

The target driver used to process the DMA_TO_DEVICE commands with residuals (in particular, WRITE command) incorrectly. The target response contained neither a residual count, nor an OVERFLOW/UNDERFLOW bit. Such behavior did not comply with the RFC 7143. Also the returned ASC and ASCQ were incorrect according to FCP-4, and residuals were not set for the 4Kn devices.

This patches fix the major inconsistances in processing these kind of commands.

This patch series has been tested with a modified libiscsi testing library.
The link to the pull request:
https://github.com/sahlberg/libiscsi/pull/345

Write10Residuals, Write12Residuals, Write16Residuals tests have passed.

Thanks,
Anastasia

Anastasia Kovaleva (2):
scsi: target: core: Signal WRITE residuals
scsi: target: core: Change ASCQ for residual write

Roman Bolshakov (1):
scsi: target: core: Set residuals for 4Kn devices

 drivers/target/target_core_transport.c | 53 +++++++++++++-------------
 include/target/target_core_base.h      |  1 +
 2 files changed, 28 insertions(+), 26 deletions(-)
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw

PATCH RESEND 1/3 scsi: target: core: Set residuals for 4Kn devices

From: Roman Bolshakov <[email protected]>

TCM always fails SBC commands with residuals for 4Kn devices when the command is processed by sbc_parse_cdb(). That prevents residual signalling to the transport driver because residual kind and residual amount aren't set. It also makes residual handling different from 512-byte formatted devices - if there are residuals 512-byte LUN would proceed with command execution while 4K-byte LUN would fail.

Based-on: https://patchwork.kernel.org/project/target-devel/patch/[email protected]/

---
 drivers/target/target_core_transport.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/target/target_core_transport.c +0 -11
index fca4bd079d02..f27a7d6cc1e0 100644
@@ -1332,17 +1332,6 @@ target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
 				return TCM_INVALID_CDB_FIELD;
 			}
 		}
-		/*
-		 * Reject READ_* or WRITE_* with overflow/underflow for
-		 * type SCF_SCSI_DATA_CDB.
-		 */
-		if (dev->dev_attrib.block_size != 512)  {
-			pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
-				" CDB on non 512-byte sector setup subsystem"
-				" plugin: %s\n", dev->transport->name);
-			/* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
-			return TCM_INVALID_CDB_FIELD;
-		}
 		/*
 		 * For the overflow case keep the existing fabric provided
 		 * ->data_length.  Otherwise for the underflow case, reset
Signature · 1 line
2.24.3 (Apple Git-128)
#2 lore raw

PATCH RESEND 2/3 scsi: target: core: Signal WRITE residuals

In reply to Anastasia Kovaleva

According to RFC 7143 11.4.5.2.:

If SPDTL > EDTL for a task, iSCSI Overflow MUST be signaled in the SCSI Response PDU as specified in Section 11.4.5.1. The Residual Count MUST be set to the numerical value of (SPDTL - EDTL).

If SPDTL < EDTL for a task, iSCSI Underflow MUST be signaled in the SCSI Response PDU as specified in Section 11.4.5.1. The Residual Count MUST be set to the numerical value of (EDTL - SPDTL).

libiscsi has residual write tests that check residual kind and residual amount and all of them (Write10Residuals, Write12Residuals, Write16Residuals) currently fail.

One of the reasons why they fail is because target completes write commands with INVALID FIELD IN CDB before setting the Overflow/Underflow bit and residual amount.

Set the Overflow/Underflow bit and the residual amount before failing a write to comply with RFC 7143.

---
 drivers/target/target_core_transport.c | 34 +++++++++++++++-----------
 1 file changed, 20 insertions(+), 14 deletions(-)
drivers/target/target_core_transport.c +20 -14
index f27a7d6cc1e0..eb51298cab4f 100644
@@ -1313,6 +1313,26 @@ target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
 			" %u does not match SCSI CDB Length: %u for SAM Opcode:"
 			" 0x%02x\n", cmd->se_tfo->fabric_name,
 				cmd->data_length, size, cmd->t_task_cdb[0]);
+		/*
+		 * For READ command for the overflow case keep the existing
+		 * fabric provided ->data_length. Otherwise for the underflow
+		 * case, reset ->data_length to the smaller SCSI expected data
+		 * transfer length.
+		 */
+		if (size > cmd->data_length) {
+			cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
+			cmd->residual_count = (size - cmd->data_length);
+		} else {
+			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
+			cmd->residual_count = (cmd->data_length - size);
+			/*
+			 * Do not truncate ->data_length for WRITE command to
+			 * dump all payload
+			 */
+			if (cmd->data_direction == DMA_FROM_DEVICE) {
+				cmd->data_length = size;
+			}
+		}
 
 		if (cmd->data_direction == DMA_TO_DEVICE) {
 			if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
@@ -1332,20 +1352,6 @@ target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
 				return TCM_INVALID_CDB_FIELD;
 			}
 		}
-		/*
-		 * For the overflow case keep the existing fabric provided
-		 * ->data_length.  Otherwise for the underflow case, reset
-		 * ->data_length to the smaller SCSI expected data transfer
-		 * length.
-		 */
-		if (size > cmd->data_length) {
-			cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
-			cmd->residual_count = (size - cmd->data_length);
-		} else {
-			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
-			cmd->residual_count = (cmd->data_length - size);
-			cmd->data_length = size;
-		}
 	}
 
 	return target_check_max_data_sg_nents(cmd, dev, size);
Signature · 1 line
2.24.3 (Apple Git-128)
#3 lore raw

PATCH RESEND 3/3 scsi: target: core: Change ASCQ for residual write

In reply to Anastasia Kovaleva

According to FCP-4 (9.4.2):

If the command requested that data beyond the length specified by the FCP_DL field be transferred, then the device server shall set the FCP_RESID_OVER bit (see 9.5.8) to one in the FCP_RSP IU and:

a) process the command normally except that data beyond the FCP_DL count shall not be requested or transferred;

b) transfer no data and return CHECK CONDITION status with the sense key set to ILLEGAL REQUEST and the additional sense code set to INVALID FIELD IN COMMAND INFORMATION UNIT; or

c) may transfer data and return CHECK CONDITION status with the sense key set to ABORTED COMMAND and the additional sense code set to INVALID FIELD IN COMMAND INFORMATION UNIT.

TCM follows b) and transfers no data for residual writes but returns INVALID FIELD IN CDB instead of INVALID FIELD IN COMMAND INFORMATION UNIT.

Change the ASCQ to INVALID FIELD IN COMMAND INFORMATION UNIT to meet the standart.

---
 drivers/target/target_core_transport.c | 8 +++++++-
 include/target/target_core_base.h      | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/target/target_core_transport.c +7 -1
index eb51298cab4f..94cee131f5b7 100644
@@ -1338,7 +1338,7 @@ target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
 			if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
 				pr_err_ratelimited("Rejecting underflow/overflow"
 						   " for WRITE data CDB\n");
-				return TCM_INVALID_CDB_FIELD;
+				return TCM_INVALID_FIELD_IN_COMMAND_IU;
 			}
 			/*
 			 * Some fabric drivers like iscsi-target still expect to
@@ -1879,6 +1879,7 @@ void transport_generic_request_failure(struct se_cmd *cmd,
 	case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE:
 	case TCM_TOO_MANY_SEGMENT_DESCS:
 	case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE:
+	case TCM_INVALID_FIELD_IN_COMMAND_IU:
 		break;
 	case TCM_OUT_OF_RESOURCES:
 		cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
@@ -3205,6 +3206,11 @@ static const struct sense_detail sense_detail_table[] = {
 		.asc = 0x55,
 		.ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */
 	},
+	[TCM_INVALID_FIELD_IN_COMMAND_IU] = {
+		.key = ILLEGAL_REQUEST,
+		.asc = 0x0e,
+		.ascq = 0x03, /* INVALID FIELD IN COMMAND INFORMATION UNIT */
+	},
 };
 
 /**
include/target/target_core_base.h +1 -0
index 63dd12124139..54dcc0eb25fa 100644
@@ -187,6 +187,7 @@ enum tcm_sense_reason_table {
 	TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE	= R(0x1c),
 	TCM_INSUFFICIENT_REGISTRATION_RESOURCES	= R(0x1d),
 	TCM_LUN_BUSY				= R(0x1e),
+	TCM_INVALID_FIELD_IN_COMMAND_IU         = R(0x1f),
 #undef R
 };
 
Signature · 1 line
2.24.3 (Apple Git-128)
#4 lore raw
In reply to Anastasia Kovaleva

Hi,

Could you please tell me if there are any objections to these changes? These patches are applicable to 5.12/scsi-queue.

Thanks,
Anastasia

On 03.12.2020, 11:21, "Anastasia Kovaleva" <[email protected]> wrote:

Hi Martin,
Please apply the changes to 5.11/scsi-queue at your earliest convenience.

The series changes the behavior of the target in regard to processing commands with overflow/underflow in accordance with the standarts.

The target driver used to process the DMA_TO_DEVICE commands with residuals (in particular, WRITE command) incorrectly. The target response contained neither a residual count, nor an OVERFLOW/UNDERFLOW bit. Such behavior did not comply with the RFC 7143. Also the returned ASC and ASCQ were incorrect according to FCP-4, and residuals were not set for the 4Kn devices.

This patches fix the major inconsistances in processing these kind of commands.

This patch series has been tested with a modified libiscsi testing library.
The link to the pull request:
https://github.com/sahlberg/libiscsi/pull/345

Write10Residuals, Write12Residuals, Write16Residuals tests have passed.

Thanks,
Anastasia

Anastasia Kovaleva (2):
scsi: target: core: Signal WRITE residuals
scsi: target: core: Change ASCQ for residual write

Roman Bolshakov (1):
scsi: target: core: Set residuals for 4Kn devices

drivers/target/target_core_transport.c | 53 +++++++++++++-------------
include/target/target_core_base.h | 1 +
2 files changed, 28 insertions(+), 26 deletions(-)

--
2.24.3 (Apple Git-128)

#5 lore raw
In reply to Anastasia Kovaleva

Anastasia,

Quoted 2 lines

The series changes the behavior of the target in regard to processing commands with overflow/underflow in accordance with the standarts.

Applied to 5.12/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#6 lore raw
In reply to Anastasia Kovaleva
Quoted 6 lines · Anastasia Kovaleva

Please apply the changes to 5.11/scsi-queue at your earliest convenience.

The series changes the behavior of the target in regard to processing commands with overflow/underflow in accordance with the standarts.

Quoted 8 lines

The target driver used to process the DMA_TO_DEVICE commands with residuals (in particular, WRITE command) incorrectly. The target response contained neither a residual count, nor an OVERFLOW/UNDERFLOW bit. Such behavior did not comply with the RFC 7143. Also the returned ASC and ASCQ were incorrect according to FCP-4, and residuals were not set for the 4Kn devices.

[...]

Applied to 5.12/scsi-queue, thanks!

[1/3] scsi: target: core: Set residuals for 4Kn devices

      https://git.kernel.org/mkp/scsi/c/eb90e45542b5
[2/3] scsi: target: core: Signal WRITE residuals
      https://git.kernel.org/mkp/scsi/c/cc0b6ad72e18
[3/3] scsi: target: core: Change ASCQ for residual write
      https://git.kernel.org/mkp/scsi/c/ead0ffc95a89
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering