qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC PATCH v2 02/13] scsi: support parsing of SAM logical u


From: Paolo Bonzini
Subject: [Qemu-devel] [RFC PATCH v2 02/13] scsi: support parsing of SAM logical unit numbers
Date: Mon, 6 Jun 2011 18:04:11 +0200

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 the current (admittedly incorrect) code switched from
a "bus X, target 0, lun 0" representation while OpenFirmware is
running (because OF prefers access mode 0, which places bus numbers
in the top byte) to "bus 0, target Y, lun 0" while Linux is running
(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 <address@hidden>
---
 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 3918662..3b80541 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -741,3 +741,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 8d60c3a..4ba1801 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 194db23..038b9e4 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)
 {
@@ -642,20 +635,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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]