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 13/15] virtio-scsi: add basic SCSI bus operation
Date: Mon, 16 Jan 2012 17:30:55 +0100	[thread overview]
Message-ID: <1326731457-9056-14-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1326731457-9056-1-git-send-email-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/virtio-scsi.c |  110 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 97 insertions(+), 13 deletions(-)

diff --git a/hw/virtio-scsi.c b/hw/virtio-scsi.c
index b34c14f..21264a1 100644
--- a/hw/virtio-scsi.c
+++ b/hw/virtio-scsi.c
@@ -128,6 +128,7 @@ typedef struct {
     DeviceState *qdev;
     VirtIOSCSIConf *conf;
 
+    SCSIBus bus;
     VirtQueue *ctrl_vq;
     VirtQueue *event_vq;
     VirtQueue *cmd_vq;
@@ -156,6 +157,22 @@ typedef struct VirtIOSCSIReq {
     } resp;
 } VirtIOSCSIReq;
 
+static inline int virtio_scsi_get_lun(uint8_t *lun)
+{
+    return ((lun[2] << 8) | lun[3]) & 0x3FFF;
+}
+
+static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
+{
+    if (lun[0] != 1) {
+        return NULL;
+    }
+    if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
+        return NULL;
+    }
+    return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
+}
+
 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
 {
     VirtIOSCSI *s = req->dev;
@@ -240,7 +257,41 @@ static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
     }
 }
 
-static void virtio_scsi_fail_cmd_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
+static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
+                                         int32_t resid)
+{
+    VirtIOSCSIReq *req = r->hba_private;
+
+    req->resp.cmd->response = VIRTIO_SCSI_S_OK;
+    req->resp.cmd->status = status;
+    if (req->resp.cmd->status == GOOD) {
+        req->resp.cmd->resid = resid;
+    } else {
+       req->resp.cmd->resid = 0;
+       scsi_req_get_sense(r, req->resp.cmd->sense, VIRTIO_SCSI_SENSE_SIZE);
+    }
+    virtio_scsi_complete_req(req);
+}
+
+static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
+{
+    VirtIOSCSIReq *req = r->hba_private;
+
+    return &req->qsgl;
+}
+
+static void virtio_scsi_request_cancelled(SCSIRequest *r)
+{
+    VirtIOSCSIReq *req = r->hba_private;
+
+    if (!req) {
+        return;
+    }
+    req->resp.cmd->response = VIRTIO_SCSI_S_ABORTED;
+    virtio_scsi_complete_req(req);
+}
+
+static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 {
     req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE;
     virtio_scsi_complete_req(req);
@@ -250,8 +301,10 @@ static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
     VirtIOSCSIReq *req;
+    int n;
 
     while ((req = virtio_scsi_pop_req(s, vq))) {
+        SCSIDevice *d;
         int out_size, in_size;
         if (req->elem.out_num < 1 || req->elem.in_num < 1) {
             virtio_scsi_bad_req();
@@ -265,21 +318,36 @@ static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
         }
 
         if (req->elem.out_num > 1 && req->elem.in_num > 1) {
-            virtio_scsi_fail_cmd_req(s, req);
+            virtio_scsi_fail_cmd_req(req);
             continue;
         }
 
-        req->resp.cmd->resid = 0;
-        req->resp.cmd->status_qualifier = 0;
-        req->resp.cmd->status = CHECK_CONDITION;
-        req->resp.cmd->sense_len = 4;
-        req->resp.cmd->sense[0] = 0xf0; /* Fixed format current sense */
-        req->resp.cmd->sense[1] = ILLEGAL_REQUEST;
-        req->resp.cmd->sense[2] = 0x20;
-        req->resp.cmd->sense[3] = 0x00;
-        req->resp.cmd->response = VIRTIO_SCSI_S_OK;
-
-        virtio_scsi_complete_req(req);
+        d = virtio_scsi_device_find(s, req->req.cmd->lun);
+        if (!d) {
+            req->resp.cmd->response = VIRTIO_SCSI_S_BAD_TARGET;
+            virtio_scsi_complete_req(req);
+            continue;
+        }
+        req->sreq = scsi_req_new(d, req->req.cmd->tag,
+                                 virtio_scsi_get_lun(req->req.cmd->lun),
+                                 req->req.cmd->cdb, req);
+
+        if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
+            int req_mode =
+                (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
+
+            if (req->sreq->cmd.mode != req_mode ||
+                req->sreq->cmd.xfer > req->qsgl.size) {
+                req->resp.cmd->response = VIRTIO_SCSI_S_OVERRUN;
+                virtio_scsi_complete_req(req);
+                continue;
+            }
+        }
+
+        n = scsi_req_enqueue(req->sreq);
+        if (n) {
+            scsi_req_continue(req->sreq);
+        }
     }
 }
 
@@ -331,6 +399,17 @@ static void virtio_scsi_reset(VirtIODevice *vdev)
     s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
 }
 
+static struct SCSIBusInfo virtio_scsi_scsi_info = {
+    .tcq = true,
+    .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
+    .max_target = VIRTIO_SCSI_MAX_TARGET,
+    .max_lun = VIRTIO_SCSI_MAX_LUN,
+
+    .complete = virtio_scsi_command_complete,
+    .cancel = virtio_scsi_request_cancelled,
+    .get_sg_list = virtio_scsi_get_sg_list,
+};
+
 VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
 {
     VirtIOSCSI *s;
@@ -355,6 +434,11 @@ VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
     s->cmd_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
                                    virtio_scsi_handle_cmd);
 
+    scsi_bus_new(&s->bus, dev, &virtio_scsi_scsi_info);
+    if (!dev->hotplugged) {
+        scsi_bus_legacy_handle_cmdline(&s->bus);
+    }
+
     /* TODO savevm */
 
     return &s->vdev;
-- 
1.7.7.1

  parent reply	other threads:[~2012-01-16 16:31 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-16 16:30 [Qemu-devel] [PATCH v2 00/15] SCSI s/g + SCSI migration + virtio-scsi Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 01/15] dma-helpers: make QEMUSGList target independent Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 02/15] dma-helpers: add dma_buf_read and dma_buf_write Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 03/15] dma-helpers: add accounting wrappers Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 04/15] ahci: use new DMA helpers Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 05/15] scsi: pass residual amount to command_complete Paolo Bonzini
2012-02-10 11:44   ` Stefan Hajnoczi
2012-02-10 11:58     ` Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 06/15] scsi: add scatter/gather functionality Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 07/15] scsi-disk: enable " Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 08/15] scsi: add SCSIDevice vmstate definitions Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 09/15] scsi-generic: add migration support Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 10/15] scsi-disk: " Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 11/15] virtio-scsi: Add virtio-scsi stub device Paolo Bonzini
2012-02-10 12:41   ` Stefan Hajnoczi
2012-02-10 12:46     ` Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 12/15] virtio-scsi: Add basic request processing infrastructure Paolo Bonzini
2012-01-16 16:30 ` Paolo Bonzini [this message]
2012-01-16 16:30 ` [Qemu-devel] [PATCH 14/15] virtio-scsi: process control queue requests Paolo Bonzini
2012-01-16 16:30 ` [Qemu-devel] [PATCH 15/15] virtio-scsi: add migration support Paolo Bonzini
2012-01-18  7:39 ` [Qemu-devel] [PATCH v2 00/15] SCSI s/g + SCSI migration + virtio-scsi Hu Tao
2012-01-19 15:53   ` Paolo Bonzini
2012-01-23 13:36     ` Paolo Bonzini
2012-01-30  9:33     ` Hu Tao
2012-01-31 12:36       ` Paolo Bonzini
2012-02-09  5:46         ` Hu Tao
2012-02-09  7:13           ` Paolo Bonzini
2012-02-09  8:08             ` Hu Tao
2012-02-10 14:01 ` Stefan Hajnoczi
2012-02-10 14:15   ` 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=1326731457-9056-14-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).