qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 10/16] scsi: move request parsing to common code
Date: Wed,  3 Aug 2011 10:49:13 +0200	[thread overview]
Message-ID: <1312361359-15445-11-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1312361359-15445-1-git-send-email-pbonzini@redhat.com>

Also introduce the first occurrence of "independent" SCSIReqOps,
to handle invalid commands in common code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/scsi-bus.c     |   53 ++++++++++++++++++++++++++++++++++++++---------------
 hw/scsi-disk.c    |    5 -----
 hw/scsi-generic.c |    9 ---------
 hw/scsi.h         |    1 -
 4 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
index 07d8704..01dbe02 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -7,6 +7,7 @@
 #include "trace.h"
 
 static char *scsibus_get_fw_dev_path(DeviceState *dev);
+static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
 static int scsi_build_sense(uint8_t *in_buf, int in_len,
                             uint8_t *buf, int len, bool fixed);
 
@@ -134,6 +135,20 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
     return res;
 }
 
+/* SCSIReqOps implementation for invalid commands.  */
+
+static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
+{
+    scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
+    scsi_req_complete(req, CHECK_CONDITION);
+    return 0;
+}
+
+struct SCSIReqOps reqops_invalid_opcode = {
+    .size         = sizeof(SCSIRequest),
+    .send_command = scsi_invalid_command
+};
+
 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
                             uint32_t lun, void *hba_private)
 {
@@ -157,8 +172,22 @@ SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
                           uint8_t *buf, void *hba_private)
 {
     SCSIRequest *req;
-    req = d->info->alloc_req(d, tag, lun, hba_private);
-    memcpy(req->cmd.buf, buf, 16);
+    SCSICommand cmd;
+
+    if (scsi_req_parse(&cmd, d, buf) != 0) {
+        trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
+        req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
+    } else {
+        trace_scsi_req_parsed(d->id, lun, tag, buf[0],
+                              cmd.mode, cmd.xfer);
+        if (req->cmd.lba != -1) {
+            trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
+                                      cmd.lba);
+        }
+        req = d->info->alloc_req(d, tag, lun, hba_private);
+    }
+
+    req->cmd = cmd;
     return req;
 }
 
@@ -426,27 +455,21 @@ static uint64_t scsi_cmd_lba(SCSICommand *cmd)
     return lba;
 }
 
-int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
+int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
 {
     int rc;
 
-    if (req->dev->type == TYPE_TAPE) {
-        rc = scsi_req_stream_length(&req->cmd, req->dev, buf);
+    if (dev->type == TYPE_TAPE) {
+        rc = scsi_req_stream_length(cmd, dev, buf);
     } else {
-        rc = scsi_req_length(&req->cmd, req->dev, buf);
+        rc = scsi_req_length(cmd, dev, buf);
     }
     if (rc != 0)
         return rc;
 
-    assert(buf == req->cmd.buf);
-    scsi_cmd_xfer_mode(&req->cmd);
-    req->cmd.lba = scsi_cmd_lba(&req->cmd);
-    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
-                          req->cmd.mode, req->cmd.xfer);
-    if (req->cmd.lba != -1) {
-        trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
-                              req->cmd.lba);
-    }
+    memcpy(cmd->buf, buf, cmd->len);
+    scsi_cmd_xfer_mode(cmd);
+    cmd->lba = scsi_cmd_lba(cmd);
     return 0;
 }
 
diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index ceb0e0a..64aa141 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -974,11 +974,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     outbuf = (uint8_t *)r->iov.iov_base;
     DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
 
-    if (scsi_req_parse(&r->req, buf) != 0) {
-        BADF("Unsupported command length, command %x\n", command);
-        scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
-        return 0;
-    }
 #ifdef DEBUG_SCSI
     {
         int i;
diff --git a/hw/scsi-generic.c b/hw/scsi-generic.c
index 699831a..e91c32e 100644
--- a/hw/scsi-generic.c
+++ b/hw/scsi-generic.c
@@ -84,10 +84,6 @@ static void scsi_command_complete(void *opaque, int ret)
         case -EDOM:
             status = TASK_SET_FULL;
             break;
-        case -EINVAL:
-            status = CHECK_CONDITION;
-            scsi_req_build_sense(&r->req, SENSE_CODE(INVALID_FIELD));
-            break;
         case -ENOMEM:
             status = CHECK_CONDITION;
             scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE));
@@ -298,11 +294,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd)
         return 0;
     }
 
-    if (-1 == scsi_req_parse(&r->req, cmd)) {
-        BADF("Unsupported command length, command %x\n", cmd[0]);
-        scsi_command_complete(r, -EINVAL);
-        return 0;
-    }
     scsi_req_fixup(&r->req);
 
     DPRINTF("Command: lun=%d tag=0x%x len %zd data=0x%02x", lun, tag,
diff --git a/hw/scsi.h b/hw/scsi.h
index 5a1bbe2..dec814a 100644
--- a/hw/scsi.h
+++ b/hw/scsi.h
@@ -165,7 +165,6 @@ SCSIRequest *scsi_req_ref(SCSIRequest *req);
 void scsi_req_unref(SCSIRequest *req);
 
 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
-int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
 void scsi_req_print(SCSIRequest *req);
 void scsi_req_continue(SCSIRequest *req);
 void scsi_req_data(SCSIRequest *req, int len);
-- 
1.7.6

  parent reply	other threads:[~2011-08-03  8:49 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-03  8:49 [Qemu-devel] [PATCH 00/16] SCSI sense and target request overhaul Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 01/16] scsi-disk: no need to call scsi_req_data on a short read Paolo Bonzini
2011-08-03 16:32   ` Christoph Hellwig
2011-08-04 13:35   ` Stefan Hajnoczi
2011-08-04 13:46     ` Kevin Wolf
2011-08-03  8:49 ` [Qemu-devel] [PATCH 02/16] vscsi: always use get_sense Paolo Bonzini
2011-08-03 16:32   ` Christoph Hellwig
2011-08-03  8:49 ` [Qemu-devel] [PATCH 03/16] scsi: pass status when completing Paolo Bonzini
2011-08-03 16:34   ` Christoph Hellwig
2011-08-03  8:49 ` [Qemu-devel] [PATCH 04/16] scsi: move sense handling to generic code Paolo Bonzini
2011-08-03 16:34   ` Christoph Hellwig
2011-08-03  8:49 ` [Qemu-devel] [PATCH 05/16] scsi: introduce SCSIReqOps Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 06/16] scsi: move request-related callbacks from SCSIDeviceInfo to SCSIReqOps Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 07/16] scsi: pass cdb already to scsi_req_new Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 08/16] scsi: introduce SCSICommand Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 09/16] scsi: push lun field to SCSIDevice Paolo Bonzini
2011-08-03  8:49 ` Paolo Bonzini [this message]
2011-08-12 16:12   ` [Qemu-devel] [PATCH 10/16] scsi: move request parsing to common code Peter Maydell
2011-08-12 17:05     ` Paolo Bonzini
2011-08-12 16:55   ` Peter Maydell
2011-08-12 17:11     ` Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 11/16] scsi: move handling of REPORT LUNS and invalid LUNs " Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 12/16] scsi: move handling of REQUEST SENSE " Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 13/16] scsi: add a bunch more common sense codes Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 14/16] scsi: add support for unit attention conditions Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 15/16] scsi: report unit attention on reset Paolo Bonzini
2011-08-03  8:49 ` [Qemu-devel] [PATCH 16/16] scsi: add special traces for common commands Paolo Bonzini
2011-08-12 13:49 ` [Qemu-devel] [PATCH 00/16] SCSI sense and target request overhaul Anthony Liguori
2011-08-12 15:48   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1312361359-15445-11-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).