Skip to contentSkip to conversation

: Add proper CDB passthrough for SG_IO backstores

PATCH In review Nicholas A. Bellinger 3 participants 6 messages May 18, 2010 – May 31, 2010

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

PATCH 1/2 : Add proper CDB passthrough for SG_IO backstores

From: Nicholas Bellinger <[email protected]>

This patch updates usr/bs_sg.c:bs_sg_cmd_submit() to process incoming scsi-generic IO of type SG_DXFER_TO_DEV and SG_DXFER_FROM_DEV using a single new struct scsi_cmnd->sg_iovec set using existing scsi_cmd.h scsi_get_[out,in]_[buffer,length]() macros.

It also makes set_cmd_failed() properly return SAM_STAT_CHECK_CONDITION, which is expected by the bs_sg_cmd_submit() caller in usr/scsi.c:scsi_cmd_perform()

Finally, this patch adds struct backingstore_template->bs_passthrough=1 that is used by bs_sg.c here and the next patch to hand off struct scsi_cmd for CDB passthrough.

---
 usr/bs_sg.c     |   31 ++++++++++++++++++++++++-------
 usr/scsi_cmnd.h |    4 ++++
 usr/tgtd.h      |    1 +
 3 files changed, 29 insertions(+), 7 deletions(-)
usr/bs_sg.c +24 -7
index 8baa480..9696392 100644
@@ -82,7 +82,7 @@ static int graceful_write(int fd, void *p_write, int to_write)
 	return 0;
 }
 
-static void set_cmd_failed(struct scsi_cmd *cmd)
+static int set_cmd_failed(struct scsi_cmd *cmd)
 {
 	int result = SAM_STAT_CHECK_CONDITION;
 	uint16_t asc = ASC_READ_ERROR;
@@ -90,11 +90,14 @@ static void set_cmd_failed(struct scsi_cmd *cmd)
 
 	scsi_set_result(cmd, result);
 	sense_data_build(cmd, key, asc);
+
+	return result;
 }
 
 static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 {
 	struct scsi_lu *dev = cmd->dev;
+	struct sg_iovec *iov = &cmd->sg_iovec;
 	int fd = dev->fd;
 	struct sg_io_hdr io_hdr;
 	int err = 0;
@@ -104,14 +107,27 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 	io_hdr.cmd_len = cmd->scb_len;
 	io_hdr.cmdp = cmd->scb;
 
+	if (scsi_get_data_dir(cmd) == DATA_BIDIRECTIONAL) {
+		eprintf("Unable to process DATA_BIDIRECTIONAL to"
+			" SG_IO backstore\n");
+		return set_cmd_failed(cmd);
+	}
+
 	if (scsi_get_data_dir(cmd) == DATA_WRITE) {
 		io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
-		io_hdr.dxfer_len = scsi_get_out_length(cmd);
-		io_hdr.dxferp = (void *)scsi_get_out_buffer(cmd);
-	} else {
+		io_hdr.dxfer_len = iov->iov_len = scsi_get_out_length(cmd);
+		iov->iov_base = (void *)scsi_get_out_buffer(cmd);
+		io_hdr.dxferp = &cmd->sg_iovec;
+		io_hdr.iovec_count = 1;
+	} else if (scsi_get_data_dir(cmd) == DATA_READ) {
 		io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
-		io_hdr.dxfer_len = scsi_get_in_length(cmd);
-		io_hdr.dxferp = (void *)scsi_get_in_buffer(cmd);
+		io_hdr.dxfer_len = iov->iov_len = scsi_get_in_length(cmd);
+		iov->iov_base = (void *)scsi_get_in_buffer(cmd);
+		io_hdr.dxferp = &cmd->sg_iovec;
+		io_hdr.iovec_count = 1;
+	} else {
+		io_hdr.dxfer_direction = SG_DXFER_NONE;
+		io_hdr.dxfer_len = 0;
 	}
 	io_hdr.mx_sb_len = sizeof(cmd->sense_buffer);
 	io_hdr.sbp = cmd->sense_buffer;
@@ -125,7 +141,7 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 		set_cmd_async(cmd);
 	else {
 		eprintf("failed to start cmd 0x%p\n", cmd);
-		set_cmd_failed(cmd);
+		return set_cmd_failed(cmd);
 	}
 	return 0;
 }
@@ -238,6 +254,7 @@ static int bs_sg_cmd_done(struct scsi_cmd *cmd)
 static struct backingstore_template sg_bst = {
 	.bs_name		= "sg",
 	.bs_datasize		= 0,
+	.bs_passthrough		= 1,
 	.bs_open		= bs_sg_open,
 	.bs_close		= bs_sg_close,
 	.bs_cmd_submit		= bs_sg_cmd_submit,
usr/scsi_cmnd.h +4 -0
index 011f3e6..776ffb3 100644
@@ -17,6 +17,8 @@ struct scsi_data_buffer {
 	uint64_t buffer;
 };
 
+#include <scsi/sg.h>
+
 struct scsi_cmd {
 	struct target *c_target;
 	/* linked it_nexus->cmd_hash_list */
@@ -31,6 +33,8 @@ struct scsi_cmd {
 	enum data_direction data_dir;
 	struct scsi_data_buffer in_sdb;
 	struct scsi_data_buffer out_sdb;
+	/* used for bs_sg.c:bs_sg_cmd_submit() */
+	struct sg_iovec sg_iovec;
 
 	uint64_t cmd_itn_id;
 	uint64_t offset;
usr/tgtd.h +1 -0
index 3323a9b..4f26a29 100644
@@ -117,6 +117,7 @@ struct device_type_template {
 struct backingstore_template {
 	const char *bs_name;
 	int bs_datasize;
+	int bs_passthrough;
 	int (*bs_open)(struct scsi_lu *dev, char *path, int *fd, uint64_t *size);
 	void (*bs_close)(struct scsi_lu *dev);
 	int (*bs_init)(struct scsi_lu *dev);
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 9 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch updates usr/bs_sg.c:bs_sg_cmd_submit() to process incoming scsi-generic IO of type SG_DXFER_TO_DEV and SG_DXFER_FROM_DEV using a single new struct scsi_cmnd->sg_iovec set using existing scsi_cmd.h scsi_get_[out,in]_[buffer,length]() macros.

It also makes set_cmd_failed() properly return SAM_STAT_CHECK_CONDITION, which is expected by the bs_sg_cmd_submit() caller in usr/scsi.c:scsi_cmd_perform()

Quoted 6 lines

Finally, this patch adds struct backingstore_template->bs_passthrough=1 that is used by bs_sg.c here and the next patch to hand off struct scsi_cmd for CDB passthrough.

---
 usr/bs_sg.c     |   31 ++++++++++++++++++++++++-------

Why not add a new bs_bsg.c backing store that goes through BSG and not SG and do it properly for all type of targets and commands?

Just a thought.

Quoted 33 lines
 usr/scsi_cmnd.h |    4 ++++
 usr/tgtd.h      |    1 +
 3 files changed, 29 insertions(+), 7 deletions(-)
usr/bs_sg.c +4 -1
index 8baa480..9696392 100644
@@ -82,7 +82,7 @@ static int graceful_write(int fd, void *p_write, int to_write)
 	return 0;
 }
 
-static void set_cmd_failed(struct scsi_cmd *cmd)
+static int set_cmd_failed(struct scsi_cmd *cmd)
 {
 	int result = SAM_STAT_CHECK_CONDITION;
 	uint16_t asc = ASC_READ_ERROR;
@@ -90,11 +90,14 @@ static void set_cmd_failed(struct scsi_cmd *cmd)
 
 	scsi_set_result(cmd, result);
 	sense_data_build(cmd, key, asc);
+
+	return result;
 }
 
 static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 {
 	struct scsi_lu *dev = cmd->dev;
+	struct sg_iovec *iov = &cmd->sg_iovec;
 	int fd = dev->fd;
 	struct sg_io_hdr io_hdr;
 	int err = 0;

usr/bs_sg.c static int bs_sg_cmd_submit(struct scsi_cmd *cmd) · line 110

Quoted 4 lines
usr/bs_sg.c +1 -0
@@ -104,14 +107,27 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 	io_hdr.cmd_len = cmd->scb_len;
 	io_hdr.cmdp = cmd->scb;
 
+	if (scsi_get_data_dir(cmd) == DATA_BIDIRECTIONAL) {

Using bsg just really cleans this all function up. IN_CMD => IN_HDR
OUT_CMD => OUT_HDR

no ifs and buts. The tgt's cmd looks like struct sg_io_v4 as if they are twins ;-)

usr/bs_sg.c static int bs_sg_cmd_submit(struct scsi_cmd *cmd) · line 110

Quoted 9 lines
usr/bs_sg.c +5 -2
+		eprintf("Unable to process DATA_BIDIRECTIONAL to"
+			" SG_IO backstore\n");
+		return set_cmd_failed(cmd);
+	}
+
 	if (scsi_get_data_dir(cmd) == DATA_WRITE) {
 		io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
-		io_hdr.dxfer_len = scsi_get_out_length(cmd);
-		io_hdr.dxferp = (void *)scsi_get_out_buffer(cmd);

dxferp is already void* why do you need the cast?

Quoted 9 lines
usr/bs_sg.c +5 -3
-	} else {
+		io_hdr.dxfer_len = iov->iov_len = scsi_get_out_length(cmd);
+		iov->iov_base = (void *)scsi_get_out_buffer(cmd);
+		io_hdr.dxferp = &cmd->sg_iovec;
+		io_hdr.iovec_count = 1;
+	} else if (scsi_get_data_dir(cmd) == DATA_READ) {
 		io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
-		io_hdr.dxfer_len = scsi_get_in_length(cmd);
-		io_hdr.dxferp = (void *)scsi_get_in_buffer(cmd);

usr/bs_sg.c static int bs_sg_cmd_submit(struct scsi_cmd *cmd) · line 133

Quoted 10 lines
usr/bs_sg.c +7 -0
+		io_hdr.dxfer_len = iov->iov_len = scsi_get_in_length(cmd);
+		iov->iov_base = (void *)scsi_get_in_buffer(cmd);
+		io_hdr.dxferp = &cmd->sg_iovec;
+		io_hdr.iovec_count = 1;
+	} else {
+		io_hdr.dxfer_direction = SG_DXFER_NONE;
+		io_hdr.dxfer_len = 0;
 	}
 	io_hdr.mx_sb_len = sizeof(cmd->sense_buffer);
 	io_hdr.sbp = cmd->sense_buffer;

You might want to look into the new SG_FLAG_Q_AT_TAIL but this is available only in new Kernels so it'll be a little hack

Quoted 10 lines
usr/bs_sg.c +1 -1
@@ -125,7 +141,7 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 		set_cmd_async(cmd);
 	else {
 		eprintf("failed to start cmd 0x%p\n", cmd);
-		set_cmd_failed(cmd);
+		return set_cmd_failed(cmd);
 	}
 	return 0;
 }

usr/bs_sg.c static int bs_sg_cmd_done(struct scsi_cmd *cmd) · line 260

Quoted 7 lines
usr/bs_sg.c +1 -0
@@ -238,6 +254,7 @@ static int bs_sg_cmd_done(struct scsi_cmd *cmd)
 static struct backingstore_template sg_bst = {
 	.bs_name		= "sg",
 	.bs_datasize		= 0,
+	.bs_passthrough		= 1,
 	.bs_open		= bs_sg_open,
 	.bs_close		= bs_sg_close,
 	.bs_cmd_submit		= bs_sg_cmd_submit,

And again the Second patch should just fold into this one. And the final disposition will just set the .bs_cmd_submit to the proper one.

[BTW if you want to keep struct backingstore_template sg_bst const (Which it is not, but
should be) then just have two of them and switch between them]

Boaz

Quoted 34 lines
usr/scsi_cmnd.h +4 -0
index 011f3e6..776ffb3 100644
@@ -17,6 +17,8 @@ struct scsi_data_buffer {
 	uint64_t buffer;
 };
 
+#include <scsi/sg.h>
+
 struct scsi_cmd {
 	struct target *c_target;
 	/* linked it_nexus->cmd_hash_list */
@@ -31,6 +33,8 @@ struct scsi_cmd {
 	enum data_direction data_dir;
 	struct scsi_data_buffer in_sdb;
 	struct scsi_data_buffer out_sdb;
+	/* used for bs_sg.c:bs_sg_cmd_submit() */
+	struct sg_iovec sg_iovec;
 
 	uint64_t cmd_itn_id;
 	uint64_t offset;
usr/tgtd.h +1 -0
index 3323a9b..4f26a29 100644
@@ -117,6 +117,7 @@ struct device_type_template {
 struct backingstore_template {
 	const char *bs_name;
 	int bs_datasize;
+	int bs_passthrough;
 	int (*bs_open)(struct scsi_lu *dev, char *path, int *fd, uint64_t *size);
 	void (*bs_close)(struct scsi_lu *dev);
 	int (*bs_init)(struct scsi_lu *dev);
#2 lore raw
Quoted 13 lines · Boaz Harrosh
Quoted 12 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch updates usr/bs_sg.c:bs_sg_cmd_submit() to process incoming scsi-generic IO of type SG_DXFER_TO_DEV and SG_DXFER_FROM_DEV using a single new struct scsi_cmnd->sg_iovec set using existing scsi_cmd.h scsi_get_[out,in]_[buffer,length]() macros.

It also makes set_cmd_failed() properly return SAM_STAT_CHECK_CONDITION, which is expected by the bs_sg_cmd_submit() caller in usr/scsi.c:scsi_cmd_perform()

Finally, this patch adds struct backingstore_template->bs_passthrough=1 that is used by bs_sg.c here and the next patch to hand off struct scsi_cmd for CDB passthrough.

Quoted 8 lines
Quoted 3 lines
---
 usr/bs_sg.c     |   31 ++++++++++++++++++++++++-------

Why not add a new bs_bsg.c backing store that goes through BSG and not SG and do it properly for all type of targets and commands?

Just a thought.

Greetings Boaz,

I completely agree with you here and think that BSG is the next logical direction for expanding our target capabilities and for adding some manner of I_T nexus auto-configuration between STGT userspace fabric modules <-> TCM_Loop multi-fabric target port LUNs. That said, I will start groking BSG and look at adding a new bs_bsg backstore for STGT soon for extended user -> kernel passthrough ops soon.

Also, the BSG direction is going to make alot of sense with the QEMU-KVM
HBA Emulation <-> TCM_Loop efforts too. However I believe at least for
megraid_sas that TMRs are handled in the driver for LDs and not processed directly by the 8708EM2 PowerPC Core firmware. ;)

Best,

--nab

#3 lore raw
In reply to Boaz Harrosh
Quoted 12 lines · On Sun, 30 May 2010 12:36:18 +0300 Boaz Harrosh <[email protected]> wrote:
Quoted 12 lines
usr/bs_sg.c +1 -1
@@ -125,7 +141,7 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 		set_cmd_async(cmd);
 	else {
 		eprintf("failed to start cmd 0x%p\n", cmd);
-		set_cmd_failed(cmd);
+		return set_cmd_failed(cmd);
 	}
 	return 0;
 }
@@ -238,6 +254,7 @@ static int bs_sg_cmd_done(struct scsi_cmd *cmd)
 static struct backingstore_template sg_bst = {
 	.bs_name		= "sg",
Quoted 8 lines

usr/bs_sg.c static int bs_sg_cmd_done(struct scsi_cmd *cmd) · line 260

Quoted 5 lines
usr/bs_sg.c +1 -0
 	.bs_datasize		= 0,
+	.bs_passthrough		= 1,
 	.bs_open		= bs_sg_open,
 	.bs_close		= bs_sg_close,
 	.bs_cmd_submit		= bs_sg_cmd_submit,

And again the Second patch should just fold into this one. And the final disposition will just set the .bs_cmd_submit to the proper one.

Hmm, that means this patchset adds new I/O code with sg (or bsg). That's not passthrough..

I think that passthrough needs to overwrite some of struct device_type_template and modify target_cmd_queue().

#4 lore raw
Quoted 24 lines · FUJITA Tomonori
Quoted 12 lines · On Sun, 30 May 2010 12:36:18 +0300 Boaz Harrosh <[email protected]> wrote:
Quoted 12 lines
usr/bs_sg.c +1 -1
@@ -125,7 +141,7 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 		set_cmd_async(cmd);
 	else {
 		eprintf("failed to start cmd 0x%p\n", cmd);
-		set_cmd_failed(cmd);
+		return set_cmd_failed(cmd);
 	}
 	return 0;
 }
@@ -238,6 +254,7 @@ static int bs_sg_cmd_done(struct scsi_cmd *cmd)
 static struct backingstore_template sg_bst = {
 	.bs_name		= "sg",
Quoted 8 lines

usr/bs_sg.c static int bs_sg_cmd_done(struct scsi_cmd *cmd) · line 260

Quoted 5 lines
usr/bs_sg.c +1 -0
 	.bs_datasize		= 0,
+	.bs_passthrough		= 1,
 	.bs_open		= bs_sg_open,
 	.bs_close		= bs_sg_close,
 	.bs_cmd_submit		= bs_sg_cmd_submit,

And again the Second patch should just fold into this one. And the final disposition will just set the .bs_cmd_submit to the proper one.

Quoted 5 lines

Hmm, that means this patchset adds new I/O code with sg (or bsg). That's not passthrough..

I think that passthrough needs to overwrite some of struct device_type_template and modify target_cmd_queue().

Indeed. Expecting the passthrough to bypass the STGT port and CDB emulation at the top of scsi_cmd_perform() would probably make sense AFAICT.

So that said, at which spot in target_cmd_queue() would it make sense for incoming struct scsi_cmd to be handled off for passthrough..?

Best,

--nab

#5 lore raw
Quoted 31 lines · On Sun, 30 May 2010 21:06:40 -0700 "Nicholas A. Bellinger"…
Quoted 24 lines · FUJITA Tomonori
Quoted 12 lines · On Sun, 30 May 2010 12:36:18 +0300 Boaz Harrosh <[email protected]> wrote:
Quoted 12 lines
usr/bs_sg.c +1 -1
@@ -125,7 +141,7 @@ static int bs_sg_cmd_submit(struct scsi_cmd *cmd)
 		set_cmd_async(cmd);
 	else {
 		eprintf("failed to start cmd 0x%p\n", cmd);
-		set_cmd_failed(cmd);
+		return set_cmd_failed(cmd);
 	}
 	return 0;
 }
@@ -238,6 +254,7 @@ static int bs_sg_cmd_done(struct scsi_cmd *cmd)
 static struct backingstore_template sg_bst = {
 	.bs_name		= "sg",
Quoted 8 lines

usr/bs_sg.c static int bs_sg_cmd_done(struct scsi_cmd *cmd) · line 260

Quoted 5 lines
usr/bs_sg.c +1 -0
 	.bs_datasize		= 0,
+	.bs_passthrough		= 1,
 	.bs_open		= bs_sg_open,
 	.bs_close		= bs_sg_close,
 	.bs_cmd_submit		= bs_sg_cmd_submit,

And again the Second patch should just fold into this one. And the final disposition will just set the .bs_cmd_submit to the proper one.

Quoted 5 lines

Hmm, that means this patchset adds new I/O code with sg (or bsg). That's not passthrough..

I think that passthrough needs to overwrite some of struct device_type_template and modify target_cmd_queue().

Quoted 6 lines

Indeed. Expecting the passthrough to bypass the STGT port and CDB emulation at the top of scsi_cmd_perform() would probably make sense AFAICT.

So that said, at which spot in target_cmd_queue() would it make sense for incoming struct scsi_cmd to be handled off for passthrough..?

Yeah, surely, scsi_cmd_perform() is too late since we need to avoid cmd_enabled() checking. So you need to modify target_cmd_queue() or modify cmd_enabled() to return true for passthrough commands at all times.

Another issue is that you need to skip some checking (such as UA) in scsi_cmd_perform(). I don't have a clear idea how we can implement passthough support cleanly. I guess that we need to try some implementations.