qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [Qemu-devel] [RFC PATCH 2/6] scsi: support parsing of SAM logical unit numbers
Date: Fri, 20 May 2011 17:03:33 +0200	[thread overview]
Message-ID: <1305903817-25476-3-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1305903817-25476-1-git-send-email-pbonzini@redhat.com>

SAM logical unit numbers are complicated beasts that can address
multiple levels of buses and targets before finally reaching
logical units.  Begin supporting them by correctly parsing vSCSI
LUNs.

Note that with the current (admittedly incorrect) code OpenFirmware
thought the devices were at "bus X, target 0, lun 0" (because OF
prefers access mode 0, which places bus numbers in the top byte),
while Linux thought it was "bus 0, target Y, lun 0" (because Linux
uses access mode 2, which places target numbers in the top byte).
With this patch, everything consistently uses the former notation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/scsi-bus.c    |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/scsi-defs.h   |   22 +++++++++++
 hw/scsi.h        |    7 +++
 hw/spapr_vscsi.c |   18 ++-------
 4 files changed, 142 insertions(+), 14 deletions(-)

diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
index 2f0ffda..70b1092 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -718,3 +718,112 @@ static char *scsibus_get_fw_dev_path(DeviceState *dev)
 
     return strdup(path);
 }
+
+/* Decode the bus and level parts of a LUN, as defined in the SCSI architecture
+   model.  If false is returned, the LUN could not be parsed.  If true
+   is return, "*bus" and "*target" identify the next two steps in the
+   hierarchical LUN.
+
+   "*lun" can be used with scsi_get_lun to continue the parsing.  */
+static bool scsi_decode_level(uint64_t sam_lun, int *bus, int *target,
+                              uint64_t *lun)
+{
+    switch (sam_lun >> 62) {
+    case ADDR_PERIPHERAL_DEVICE:
+        *bus = (sam_lun >> 56) & 0x3f;
+        if (*bus) {
+            /* The TARGET OR LUN field selects a target; walk the next
+               16-bits to find the LUN.  */
+            *target = (sam_lun >> 48) & 0xff;
+            *lun = sam_lun << 16;
+        } else {
+            /* The TARGET OR LUN field selects a LUN on the current
+               node, identified by bus 0.  */
+            *target = 0;
+            *lun = (sam_lun & 0xff000000000000LL) | (1LL << 62);
+        }
+        return true;
+    case ADDR_LOGICAL_UNIT:
+        *bus = (sam_lun >> 53) & 7;
+        *target = (sam_lun >> 56) & 0x3f;
+        *lun = (sam_lun & 0x1f000000000000LL) | (1LL << 62);
+        return true;
+    case ADDR_FLAT_SPACE:
+        *bus = 0;
+        *target = 0;
+        *lun = sam_lun;
+        return true;
+    case ADDR_LOGICAL_UNIT_EXT:
+        if ((sam_lun >> 56) == ADDR_WELL_KNOWN_LUN ||
+            (sam_lun >> 56) == ADDR_FLAT_SPACE_EXT) {
+            *bus = 0;
+            *target = 0;
+            *lun = sam_lun;
+            return true;
+        }
+        return false;
+    }
+    abort();
+}
+
+/* Extract a single-level LUN number from a LUN, as specified in the
+   SCSI architecture model.  Return -1 if this is not possible because
+   the LUN includes a bus or target component.  */
+static int scsi_get_lun(uint64_t sam_lun)
+{
+    int bus, target;
+
+retry:
+    switch (sam_lun >> 62) {
+    case ADDR_PERIPHERAL_DEVICE:
+    case ADDR_LOGICAL_UNIT:
+        scsi_decode_level(sam_lun, &bus, &target, &sam_lun);
+        if (bus || target) {
+            return LUN_INVALID;
+        }
+        goto retry;
+
+    case ADDR_FLAT_SPACE:
+        return (sam_lun >> 48) & 0x3fff;
+    case ADDR_LOGICAL_UNIT_EXT:
+        if ((sam_lun >> 56) == ADDR_WELL_KNOWN_LUN) {
+            return LUN_WLUN_BASE | ((sam_lun >> 48) & 0xff);
+        }
+        if ((sam_lun >> 56) == ADDR_FLAT_SPACE_EXT) {
+            return (sam_lun >> 32) & 0xffffff;
+        }
+        return LUN_INVALID;
+    }
+    abort();
+}
+
+/* Extract bus and target from the given LUN and use it to identify a
+   SCSIDevice from a SCSIBus.  Right now, only 1 target per bus is
+   supported.  In the future a SCSIDevice could host its own SCSIBus,
+   in an alternation of devices that select a bus (target ports) and
+   devices that select a target (initiator ports).  */
+SCSIDevice *scsi_decode_lun(SCSIBus *sbus, uint64_t sam_lun, int *lun)
+{
+    int bus, target, decoded_lun;
+    uint64_t next_lun;
+
+    if (!scsi_decode_level(sam_lun, &bus, &target, &next_lun)) {
+         /* Unsupported LUN format.  */
+         return NULL;
+    }
+    if (bus >= sbus->ndev || (bus == 0 && target > 0)) {
+        /* Out of range.  */
+        return NULL;
+    }
+    if (target != 0) {
+        /* Only one target for now.  */
+        return NULL;
+    }
+
+    decoded_lun = scsi_get_lun(next_lun);
+    if (decoded_lun != LUN_INVALID) {
+        *lun = decoded_lun;
+        return sbus->devs[bus];
+    }
+    return NULL;
+}
diff --git a/hw/scsi-defs.h b/hw/scsi-defs.h
index 413cce0..66dfd4a 100644
--- a/hw/scsi-defs.h
+++ b/hw/scsi-defs.h
@@ -164,3 +164,25 @@
 #define TYPE_ENCLOSURE	    0x0d    /* Enclosure Services Device */
 #define TYPE_NO_LUN         0x7f
 
+/*
+ *      SCSI addressing methods (bits 62-63 of the LUN).
+ */
+#define ADDR_PERIPHERAL_DEVICE	0
+#define ADDR_FLAT_SPACE		1
+#define ADDR_LOGICAL_UNIT	2
+#define ADDR_LOGICAL_UNIT_EXT	3
+
+/*
+ *      SCSI extended addressing methods (bits 56-63 of the LUN).
+ */
+#define ADDR_WELL_KNOWN_LUN	0xc1
+#define ADDR_FLAT_SPACE_EXT	0xd2
+
+/*
+ *      SCSI well known LUNs (byte 1)
+ */
+#define WLUN_REPORT_LUNS         1
+#define WLUN_ACCESS_CONTROLS     2
+#define WLUN_TARGET_LOG_PAGES    3
+#define WLUN_SECURITY_PROTOCOL   4
+#define WLUN_MANAGEMENT_PROTOCOL 5
diff --git a/hw/scsi.h b/hw/scsi.h
index 93e9d35..aa75b82 100644
--- a/hw/scsi.h
+++ b/hw/scsi.h
@@ -21,6 +21,12 @@ enum SCSIXferMode {
     SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
 };
 
+enum SCSILun {
+    LUN_INVALID = -256,
+    LUN_WLUN_BASE = -256,
+    LUN_WLUN_MASK = 255
+};
+
 typedef struct SCSISense {
     uint8_t key;
     uint8_t asc;
@@ -137,6 +143,7 @@ extern const struct SCSISense sense_code_LUN_FAILURE;
 
 int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
 int scsi_sense_valid(SCSISense sense);
+SCSIDevice *scsi_decode_lun(SCSIBus *sbus, uint64_t sam_lun, int *lun);
 
 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun);
diff --git a/hw/spapr_vscsi.c b/hw/spapr_vscsi.c
index b195e06..ee88ff6 100644
--- a/hw/spapr_vscsi.c
+++ b/hw/spapr_vscsi.c
@@ -139,13 +139,6 @@ static vscsi_req *vscsi_find_req(VSCSIState *s, SCSIRequest *req)
     return &s->reqs[tag];
 }
 
-static void vscsi_decode_id_lun(uint64_t srp_lun, int *id, int *lun)
-{
-    /* XXX Figure that one out properly ! This is crackpot */
-    *id = (srp_lun >> 56) & 0x7f;
-    *lun = (srp_lun >> 48) & 0xff;
-}
-
 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
                          uint64_t length, uint8_t format)
 {
@@ -645,20 +638,17 @@ static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
 {
     union srp_iu *srp = &req->iu.srp;
     SCSIDevice *sdev;
-    int n, id, lun;
+    int n, lun;
 
-    vscsi_decode_id_lun(be64_to_cpu(srp->cmd.lun), &id, &lun);
-
-    /* Qemu vs. linux issue with LUNs to be sorted out ... */
-    sdev = (id < 8 && lun < 16) ? s->bus.devs[id] : NULL;
+    sdev = scsi_decode_lun(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
     if (!sdev) {
-        dprintf("VSCSI: Command for id %d with no drive\n", id);
         if (srp->cmd.cdb[0] == INQUIRY) {
             vscsi_inquiry_no_target(s, req);
         } else {
             vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
             vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
-        } return 1;
+        }
+        return 1;
     }
 
     req->lun = lun;
-- 
1.7.4.4

  parent reply	other threads:[~2011-05-20 15:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-20 15:03 [Qemu-devel] [RFC PATCH 0/6] SCSI series part 2, rewrite LUN parsing Paolo Bonzini
2011-05-20 15:03 ` [Qemu-devel] [RFC PATCH 1/6] scsi: ignore LUN field in the CDB Paolo Bonzini
2011-05-20 16:15   ` Christoph Hellwig
2011-05-20 15:03 ` Paolo Bonzini [this message]
2011-05-25 13:05   ` [Qemu-devel] [RFC PATCH 2/6] scsi: support parsing of SAM logical unit numbers Christoph Hellwig
2011-05-20 15:03 ` [Qemu-devel] [RFC PATCH 3/6] scsi-generic: allow customization of the lun Paolo Bonzini
2011-05-25 13:10   ` Christoph Hellwig
2011-05-25 15:20     ` Paolo Bonzini
2011-05-27 13:04       ` Christoph Hellwig
2011-05-27 13:31         ` Paolo Bonzini
2011-05-20 15:03 ` [Qemu-devel] [RFC PATCH 4/6] scsi-disk: " Paolo Bonzini
2011-05-25 13:13   ` Christoph Hellwig
2011-05-20 15:03 ` [Qemu-devel] [RFC PATCH 5/6] scsi: let a SCSIDevice have children devices Paolo Bonzini
2011-05-20 15:03 ` [Qemu-devel] [RFC PATCH 6/6] scsi: add walking of hierarchical LUNs Paolo Bonzini
2011-05-20 16:14 ` [Qemu-devel] [RFC PATCH 0/6] SCSI series part 2, rewrite LUN parsing Christoph Hellwig
2011-05-20 17:37   ` Paolo Bonzini
2011-05-25 13:17     ` Christoph Hellwig
2011-05-25 15:08       ` 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=1305903817-25476-3-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --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).