From 172a3ead06b5a730d4aee98a59edd341e31ac151 Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Wed, 22 Feb 2012 14:11:24 +0100
Subject: [PATCH 008/109] scsi: pass cdb already to scsi_req_new

RH-Author: Paolo Bonzini <pbonzini@redhat.com>
Message-id: <1329919979-20948-8-git-send-email-pbonzini@redhat.com>
Patchwork-id: 37488
O-Subject: [RHEL 6.3 qemu-kvm PATCH v2 007/102] scsi: pass cdb already to scsi_req_new
Bugzilla: 782029
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>

Right now the CDB is not passed to the SCSIBus until scsi_req_enqueue.
Passing it to scsi_req_new will let scsi_req_new dispatch common requests
through different reqops.

Moving the memcpy to scsi_req_new is a hack that will go away as
soon as scsi_req_new will also take care of the parsing.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry picked from c39ce112b60ffafbaf700853e32bea74cbb2c148)

Conflicts:
	hw/esp.c (not merged because it's not on x86)
	hw/lsi53c895a.c (not merged because we disable it)
	hw/spapr_vscsi.c (absent in RHEL6)
---
 hw/scsi-bus.c |   13 ++++++++-----
 hw/scsi.h     |    4 ++--
 hw/usb-msd.c  |    4 ++--
 3 files changed, 12 insertions(+), 9 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/scsi-bus.c |   13 ++++++++-----
 hw/scsi.h     |    4 ++--
 hw/usb-msd.c  |    4 ++--
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
index e8a95ed..ab7a502 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -153,9 +153,12 @@ SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
 }
 
 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
-                          void *hba_private)
+                          uint8_t *buf, void *hba_private)
 {
-    return d->info->alloc_req(d, tag, lun, hba_private);
+    SCSIRequest *req;
+    req = d->info->alloc_req(d, tag, lun, hba_private);
+    memcpy(req->cmd.buf, buf, 16);
+    return req;
 }
 
 uint8_t *scsi_req_get_buf(SCSIRequest *req)
@@ -190,7 +193,7 @@ void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
     req->sense_len = 18;
 }
 
-int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
+int32_t scsi_req_enqueue(SCSIRequest *req)
 {
     int32_t rc;
 
@@ -200,7 +203,7 @@ int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
     QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
 
     scsi_req_ref(req);
-    rc = req->ops->send_command(req, buf);
+    rc = req->ops->send_command(req, req->cmd.buf);
     scsi_req_unref(req);
     return rc;
 }
@@ -432,7 +435,7 @@ int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
     if (rc != 0)
         return rc;
 
-    memcpy(req->cmd.buf, buf, req->cmd.len);
+    assert(buf == req->cmd.buf);
     scsi_req_xfer_mode(req);
     req->cmd.lba = scsi_req_lba(req);
     trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
diff --git a/hw/scsi.h b/hw/scsi.h
index ba157be..b82fbef 100644
--- a/hw/scsi.h
+++ b/hw/scsi.h
@@ -158,8 +158,8 @@ int scsi_sense_valid(SCSISense sense);
 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
                             uint32_t lun, void *hba_private);
 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
-                          void *hba_private);
-int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
+                          uint8_t *buf, void *hba_private);
+int32_t scsi_req_enqueue(SCSIRequest *req);
 void scsi_req_free(SCSIRequest *req);
 SCSIRequest *scsi_req_ref(SCSIRequest *req);
 void scsi_req_unref(SCSIRequest *req);
diff --git a/hw/usb-msd.c b/hw/usb-msd.c
index eeea203..27721ad 100644
--- a/hw/usb-msd.c
+++ b/hw/usb-msd.c
@@ -395,8 +395,8 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
                     s->tag, cbw.flags, cbw.cmd_len, s->data_len);
             s->residue = 0;
             s->scsi_len = 0;
-            s->req = scsi_req_new(s->scsi_dev, s->tag, 0, NULL);
-            scsi_req_enqueue(s->req, cbw.cmd);
+            s->req = scsi_req_new(s->scsi_dev, s->tag, 0, cbw.cmd, NULL);
+            scsi_req_enqueue(s->req);
             /* ??? Should check that USB and SCSI data transfer
                directions match.  */
             if (s->mode != USB_MSDM_CSW && s->residue == 0) {
-- 
1.7.7.6

